]> git.saurik.com Git - apple/xnu.git/blame - iokit/IOKit/IOLib.h
xnu-123.5.tar.gz
[apple/xnu.git] / iokit / IOKit / IOLib.h
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*
23 * Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
24 *
25 * HISTORY
26 *
27 */
28
29#ifndef __IOKIT_IOLIB_H
30#define __IOKIT_IOLIB_H
31
32#ifndef KERNEL
33#error IOLib.h is for kernel use only
34#endif
35
36#ifndef IOKIT_DEPRECATED
37#define IOKIT_DEPRECATED 1
38#endif
39
40#include <IOKit/system.h>
41
42#include <IOKit/IOReturn.h>
43#include <IOKit/IOTypes.h>
44#include <IOKit/IOLocks.h>
45
46#include <libkern/OSAtomic.h>
47
48#ifdef __cplusplus
49extern "C" {
50#endif
51
52#include <kern/thread_call.h>
53#include <kern/clock.h>
54/*
55 * min/max macros.
56 */
57
58#define min(a,b) ((a) < (b) ? (a) : (b))
59#define max(a,b) ((a) > (b) ? (a) : (b))
60
61/*
62 * These are opaque to the user.
63 */
64typedef thread_t IOThread;
65typedef void (*IOThreadFunc)(void *argument);
66
67/*
68 * Memory allocation functions.
69 */
70
71/*! @function IOMalloc
72 @abstract Allocates general purpose, wired memory in the kernel map.
73 @discussion This is a general purpose utility to allocate memory in the kernel. There are no alignment guarantees given on the returned memory, and alignment may vary depending on the kernel configuration. This function may block and so should not be called from interrupt level or while a simple lock is held.
74 @param size Size of the memory requested.
75 @result Pointer to the allocated memory, or zero on failure. */
76
77void * IOMalloc(vm_size_t size);
78
79/*! @function IOFree
80 @abstract Frees memory allocated with IOMalloc.
81 @discussion This function frees memory allocated with IOMalloc, it may block and so should not be called from interrupt level or while a simple lock is held.
82 @param address Pointer to the allocated memory.
83 @param size Size of the memory allocated. */
84
85void IOFree(void * address, vm_size_t size);
86
87/*! @function IOMallocAligned
88 @abstract Allocates wired memory in the kernel map, with an alignment restriction.
89 @discussion This is a utility to allocate memory in the kernel, with an alignment restriction which is specified as a byte count. This function may block and so should not be called from interrupt level or while a simple lock is held.
90 @param size Size of the memory requested.
91 @param alignment Byte count of the alignment for the memory. For example, pass 256 to get memory allocated at an address with bit 0-7 zero.
92 @result Pointer to the allocated memory, or zero on failure. */
93
94void * IOMallocAligned(vm_size_t size, vm_offset_t alignment);
95
96/*! @function IOFreeAligned
97 @abstract Frees memory allocated with IOMallocAligned.
98 @discussion This function frees memory allocated with IOMallocAligned, it may block and so should not be called from interrupt level or while a simple lock is held.
99 @param address Pointer to the allocated memory.
100 @param size Size of the memory allocated. */
101
102void IOFreeAligned(void * address, vm_size_t size);
103
104/*! @function IOMallocContiguous
105 @abstract Allocates wired memory in the kernel map, with an alignment restriction and physically contiguous.
106 @discussion This is a utility to allocate memory in the kernel, with an alignment restriction which is specified as a byte count, and will allocate only physically contiguous memory. The request may fail if memory is fragmented, and may cause large amounts of paging activity. This function may block and so should not be called from interrupt level or while a simple lock is held.
107 @param size Size of the memory requested.
108 @param alignment Byte count of the alignment for the memory. For example, pass 256 to get memory allocated at an address with bits 0-7 zero.
109 @param physicalAddress IOMallocContiguous returns the physical address of the allocated memory here, if physicalAddress is a non-zero pointer.
110 @result Virtual address of the allocated memory, or zero on failure. */
111
112void * IOMallocContiguous(vm_size_t size, vm_size_t alignment,
113 IOPhysicalAddress * physicalAddress);
114
115/*! @function IOFreeContiguous
116 @abstract Frees memory allocated with IOMallocContiguous.
117 @discussion This function frees memory allocated with IOMallocContiguous, it may block and so should not be called from interrupt level or while a simple lock is held.
118 @param address Virtual address of the allocated memory.
119 @param size Size of the memory allocated. */
120
121void IOFreeContiguous(void * address, vm_size_t size);
122
123
124/*! @function IOMallocPageable
125 @abstract Allocates pageable memory in the kernel map.
126 @discussion This is a utility to allocate pageable memory in the kernel. This function may block and so should not be called from interrupt level or while a simple lock is held.
127 @param size Size of the memory requested.
128 @param alignment Byte count of the alignment for the memory. For example, pass 256 to get memory allocated at an address with bits 0-7 zero.
129 @result Pointer to the allocated memory, or zero on failure. */
130
131void * IOMallocPageable(vm_size_t size, vm_size_t alignment);
132
133/*! @function IOFreePageable
134 @abstract Frees memory allocated with IOMallocPageable.
135 @discussion This function frees memory allocated with IOMallocPageable, it may block and so should not be called from interrupt level or while a simple lock is held.
136 @param address Virtual address of the allocated memory.
137 @param size Size of the memory allocated. */
138
139void IOFreePageable(void * address, vm_size_t size);
140
141/*
142 * Typed memory allocation macros. Both may block.
143 */
144#define IONew(type,number) (type*)IOMalloc(sizeof(type) * (number) )
145#define IODelete(ptr,type,number) IOFree( (ptr) , sizeof(type) * (number) )
146
147/*! @function IOSetProcessorCacheMode
148 @abstract Sets the processor cache mode for mapped memory.
149 @discussion This function sets the cache mode of an already mapped & wired memory range. Note this may not be supported on I/O mappings or shared memory - it is far preferable to set the cache mode as mappings are created with the IOMemoryDescriptor::map method.
150 @param task Task the memory is mapped into.
151 @param address Virtual address of the memory.
152 @param length Length of the range to set.
153 @param cacheMode A constant from IOTypes.h, <br>
154 kIOMapDefaultCache to inhibit the cache in I/O areas, kIOMapCopybackCache in general purpose RAM.<br>
155 kIOMapInhibitCache, kIOMapWriteThruCache, kIOMapCopybackCache to set the appropriate caching.<br>
156 @result An IOReturn code.*/
157
158IOReturn IOSetProcessorCacheMode( task_t task, IOVirtualAddress address,
159 IOByteCount length, IOOptionBits cacheMode );
160
161/*! @function IOFlushProcessorCache
162 @abstract Flushes the processor cache for mapped memory.
163 @discussion This function flushes the processor cache of an already mapped memory range. Note in most cases it is preferable to use IOMemoryDescriptor::prepare and complete to manage cache coherency since they are aware of the architecture's requirements. Flushing the processor cache is not required for coherency in most situations.
164 @param task Task the memory is mapped into.
165 @param address Virtual address of the memory.
166 @param length Length of the range to set.
167 @result An IOReturn code. */
168
169IOReturn IOFlushProcessorCache( task_t task, IOVirtualAddress address,
170 IOByteCount length );
171
172/*! @function IOThreadSelf
173 @abstract Returns the osfmk identifier for the currently running thread.
174 @discussion This function returns the current thread (a pointer to the currently active osfmk thread_shuttle). */
175
176#define IOThreadSelf() (current_thread())
177
178/*! @function IOCreateThread
179 @abstract Create a kernel thread.
180 @discussion This function creates a kernel thread, and passes the caller supplied argument to the new thread.
181 @param function A C-function pointer where the thread will begin execution.
182 @param argument Caller specified data to be passed to the new thread.
183 @result An IOThread identifier for the new thread, equivalent to an osfmk thread_t. */
184
185IOThread IOCreateThread(IOThreadFunc function, void *argument);
186
187/*! @function IOExitThread
188 @abstract Terminate exceution of current thread.
189 @discussion This function destroys the currently running thread, and does not return. */
190
191volatile void IOExitThread();
192
193/*! @function IOSleep
194 @abstract Sleep the calling thread for a number of milliseconds.
195 @discussion This function blocks the calling thread for at least the number of specified milliseconds, giving time to other processes.
196 @param milliseconds The integer number of milliseconds to wait. */
197
198void IOSleep(unsigned milliseconds);
199
200/*! @function IODelay
201 @abstract Spin delay for a number of microseconds.
202 @discussion This function spins to delay for at least the number of specified microseconds. Since the CPU is busy spinning no time is made available to other processes; this method of delay should be used only for short periods. Also, the AbsoluteTime based APIs of kern/clock.h provide finer grained and lower cost delays.
203 @param microseconds The integer number of microseconds to spin wait. */
204
205void IODelay(unsigned microseconds);
206
207/*! @function IOLog
208 @abstract Log a message to console in text mode, and /var/log/system.log.
209 @discussion This function allows a driver to log diagnostic information to the screen during verbose boots, and to a log file found at /var/log/system.log. IOLog should not be called from interrupt context.
210 @param format A printf() style format string (see printf() documentation).
211 @param other arguments described by the format string. */
212
213void IOLog(const char *format, ...)
214__attribute__((format(printf, 1, 2)));
215
216void kprintf(const char *format, ...);
217
218/*
219 * Convert a integer constant (typically a #define or enum) to a string
220 * via an array of IONamedValue.
221 */
222const char *IOFindNameForValue(int value,
223 const IONamedValue *namedValueArray);
224
225/*
226 * Convert a string to an int via an array of IONamedValue. Returns
227 * kIOReturnSuccess of string found, else returns kIOReturnBadArgument.
228 */
229IOReturn IOFindValueForName(const char *string,
230 const IONamedValue *regValueArray,
231 int *value); /* RETURNED */
232
233/*! @function Debugger
234 @abstract Enter the kernel debugger.
235 @discussion This function freezes the kernel and enters the builtin debugger. It may not be possible to exit the debugger without a second machine.
236 @param reason A C-string to describe why the debugger is being entered. */
237
238void Debugger(const char * reason);
239
240struct OSDictionary * IOBSDNameMatching( const char * name );
241struct OSDictionary * IOOFPathMatching( const char * path, char * buf, int maxLen );
242
243/*
244 * Convert between size and a power-of-two alignment.
245 */
246IOAlignment IOSizeToAlignment(unsigned int size);
247unsigned int IOAlignmentToSize(IOAlignment align);
248
249/*
250 * Multiply and divide routines for IOFixed datatype.
251 */
252
253static inline IOFixed IOFixedMultiply(IOFixed a, IOFixed b)
254{
255 return (IOFixed)((((SInt64) a) * ((SInt64) b)) >> 16);
256}
257
258static inline IOFixed IOFixedDivide(IOFixed a, IOFixed b)
259{
260 return (IOFixed)((((SInt64) a) << 16) / ((SInt64) b));
261}
262
263/*
264 * IORound and IOTrunc convenience functions, in the spirit
265 * of vm's round_page() and trunc_page().
266 */
267#define IORound(value,multiple) \
268 ((((value) + (multiple) - 1) / (multiple)) * (multiple))
269
270#define IOTrunc(value,multiple) \
271 (((value) / (multiple)) * (multiple));
272
273
274#if IOKIT_DEPRECATED
275
276/* The following API is deprecated */
277
278#undef eieio
279#define eieio() \
280 OSSynchronizeIO()
281
282void IOPanic(const char *reason);
283
284/* The AbsoluteTime clock API exported by kern/clock.h
285 should be used for high resolution timing. */
286
287void IOGetTime( mach_timespec_t * clock_time);
288
289extern mach_timespec_t IOZeroTvalspec;
290
291#endif /* IOKIT_DEPRECATED */
292
293#ifdef __cplusplus
294} /* extern "C" */
295#endif
296
297#endif /* !__IOKIT_IOLIB_H */