]> git.saurik.com Git - apple/xnu.git/blob - iokit/IOKit/IOLib.h
xnu-201.tar.gz
[apple/xnu.git] / iokit / IOKit / IOLib.h
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
49 extern "C" {
50 #endif
51
52 #include <kern/thread_call.h>
53 #include <kern/clock.h>
54
55 /*
56 * min/max macros.
57 */
58
59 #define min(a,b) ((a) < (b) ? (a) : (b))
60 #define max(a,b) ((a) > (b) ? (a) : (b))
61
62 /*
63 * These are opaque to the user.
64 */
65 typedef thread_t IOThread;
66 typedef void (*IOThreadFunc)(void *argument);
67
68 /*
69 * Memory allocation functions.
70 */
71
72 /*! @function IOMalloc
73 @abstract Allocates general purpose, wired memory in the kernel map.
74 @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.
75 @param size Size of the memory requested.
76 @result Pointer to the allocated memory, or zero on failure. */
77
78 void * IOMalloc(vm_size_t size);
79
80 /*! @function IOFree
81 @abstract Frees memory allocated with IOMalloc.
82 @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.
83 @param address Pointer to the allocated memory.
84 @param size Size of the memory allocated. */
85
86 void IOFree(void * address, vm_size_t size);
87
88 /*! @function IOMallocAligned
89 @abstract Allocates wired memory in the kernel map, with an alignment restriction.
90 @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.
91 @param size Size of the memory requested.
92 @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.
93 @result Pointer to the allocated memory, or zero on failure. */
94
95 void * IOMallocAligned(vm_size_t size, vm_offset_t alignment);
96
97 /*! @function IOFreeAligned
98 @abstract Frees memory allocated with IOMallocAligned.
99 @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.
100 @param address Pointer to the allocated memory.
101 @param size Size of the memory allocated. */
102
103 void IOFreeAligned(void * address, vm_size_t size);
104
105 /*! @function IOMallocContiguous
106 @abstract Allocates wired memory in the kernel map, with an alignment restriction and physically contiguous.
107 @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.
108 @param size Size of the memory requested.
109 @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.
110 @param physicalAddress IOMallocContiguous returns the physical address of the allocated memory here, if physicalAddress is a non-zero pointer.
111 @result Virtual address of the allocated memory, or zero on failure. */
112
113 void * IOMallocContiguous(vm_size_t size, vm_size_t alignment,
114 IOPhysicalAddress * physicalAddress);
115
116 /*! @function IOFreeContiguous
117 @abstract Frees memory allocated with IOMallocContiguous.
118 @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.
119 @param address Virtual address of the allocated memory.
120 @param size Size of the memory allocated. */
121
122 void IOFreeContiguous(void * address, vm_size_t size);
123
124
125 /*! @function IOMallocPageable
126 @abstract Allocates pageable memory in the kernel map.
127 @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.
128 @param size Size of the memory requested.
129 @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.
130 @result Pointer to the allocated memory, or zero on failure. */
131
132 void * IOMallocPageable(vm_size_t size, vm_size_t alignment);
133
134 /*! @function IOFreePageable
135 @abstract Frees memory allocated with IOMallocPageable.
136 @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.
137 @param address Virtual address of the allocated memory.
138 @param size Size of the memory allocated. */
139
140 void IOFreePageable(void * address, vm_size_t size);
141
142 /*
143 * Typed memory allocation macros. Both may block.
144 */
145 #define IONew(type,number) (type*)IOMalloc(sizeof(type) * (number) )
146 #define IODelete(ptr,type,number) IOFree( (ptr) , sizeof(type) * (number) )
147
148 /*! @function IOSetProcessorCacheMode
149 @abstract Sets the processor cache mode for mapped memory.
150 @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.
151 @param task Task the memory is mapped into.
152 @param address Virtual address of the memory.
153 @param length Length of the range to set.
154 @param cacheMode A constant from IOTypes.h, <br>
155 kIOMapDefaultCache to inhibit the cache in I/O areas, kIOMapCopybackCache in general purpose RAM.<br>
156 kIOMapInhibitCache, kIOMapWriteThruCache, kIOMapCopybackCache to set the appropriate caching.<br>
157 @result An IOReturn code.*/
158
159 IOReturn IOSetProcessorCacheMode( task_t task, IOVirtualAddress address,
160 IOByteCount length, IOOptionBits cacheMode );
161
162 /*! @function IOFlushProcessorCache
163 @abstract Flushes the processor cache for mapped memory.
164 @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.
165 @param task Task the memory is mapped into.
166 @param address Virtual address of the memory.
167 @param length Length of the range to set.
168 @result An IOReturn code. */
169
170 IOReturn IOFlushProcessorCache( task_t task, IOVirtualAddress address,
171 IOByteCount length );
172
173 /*! @function IOThreadSelf
174 @abstract Returns the osfmk identifier for the currently running thread.
175 @discussion This function returns the current thread (a pointer to the currently active osfmk thread_shuttle). */
176
177 #define IOThreadSelf() (current_thread())
178
179 /*! @function IOCreateThread
180 @abstract Create a kernel thread.
181 @discussion This function creates a kernel thread, and passes the caller supplied argument to the new thread.
182 @param function A C-function pointer where the thread will begin execution.
183 @param argument Caller specified data to be passed to the new thread.
184 @result An IOThread identifier for the new thread, equivalent to an osfmk thread_t. */
185
186 IOThread IOCreateThread(IOThreadFunc function, void *argument);
187
188 /*! @function IOExitThread
189 @abstract Terminate exceution of current thread.
190 @discussion This function destroys the currently running thread, and does not return. */
191
192 volatile void IOExitThread();
193
194 /*! @function IOSleep
195 @abstract Sleep the calling thread for a number of milliseconds.
196 @discussion This function blocks the calling thread for at least the number of specified milliseconds, giving time to other processes.
197 @param milliseconds The integer number of milliseconds to wait. */
198
199 void IOSleep(unsigned milliseconds);
200
201 /*! @function IODelay
202 @abstract Spin delay for a number of microseconds.
203 @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.
204 @param microseconds The integer number of microseconds to spin wait. */
205
206 void IODelay(unsigned microseconds);
207
208 /*! @function IOLog
209 @abstract Log a message to console in text mode, and /var/log/system.log.
210 @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.
211 @param format A printf() style format string (see printf() documentation).
212 @param other arguments described by the format string. */
213
214 void IOLog(const char *format, ...)
215 __attribute__((format(printf, 1, 2)));
216
217 void kprintf(const char *format, ...);
218
219 /*
220 * Convert a integer constant (typically a #define or enum) to a string
221 * via an array of IONamedValue.
222 */
223 const char *IOFindNameForValue(int value,
224 const IONamedValue *namedValueArray);
225
226 /*
227 * Convert a string to an int via an array of IONamedValue. Returns
228 * kIOReturnSuccess of string found, else returns kIOReturnBadArgument.
229 */
230 IOReturn IOFindValueForName(const char *string,
231 const IONamedValue *regValueArray,
232 int *value); /* RETURNED */
233
234 /*! @function Debugger
235 @abstract Enter the kernel debugger.
236 @discussion This function freezes the kernel and enters the builtin debugger. It may not be possible to exit the debugger without a second machine.
237 @param reason A C-string to describe why the debugger is being entered. */
238
239 void Debugger(const char * reason);
240
241 struct OSDictionary * IOBSDNameMatching( const char * name );
242 struct OSDictionary * IOOFPathMatching( const char * path, char * buf, int maxLen );
243
244 /*
245 * Convert between size and a power-of-two alignment.
246 */
247 IOAlignment IOSizeToAlignment(unsigned int size);
248 unsigned int IOAlignmentToSize(IOAlignment align);
249
250 /*
251 * Multiply and divide routines for IOFixed datatype.
252 */
253
254 static inline IOFixed IOFixedMultiply(IOFixed a, IOFixed b)
255 {
256 return (IOFixed)((((SInt64) a) * ((SInt64) b)) >> 16);
257 }
258
259 static inline IOFixed IOFixedDivide(IOFixed a, IOFixed b)
260 {
261 return (IOFixed)((((SInt64) a) << 16) / ((SInt64) b));
262 }
263
264 /*
265 * IORound and IOTrunc convenience functions, in the spirit
266 * of vm's round_page() and trunc_page().
267 */
268 #define IORound(value,multiple) \
269 ((((value) + (multiple) - 1) / (multiple)) * (multiple))
270
271 #define IOTrunc(value,multiple) \
272 (((value) / (multiple)) * (multiple));
273
274
275 #if IOKIT_DEPRECATED
276
277 /* The following API is deprecated */
278
279 #undef eieio
280 #define eieio() \
281 OSSynchronizeIO()
282
283 void IOPanic(const char *reason);
284
285 /* The API exported by kern/clock.h
286 should be used for high resolution timing. */
287
288 void IOGetTime( mach_timespec_t * clock_time);
289
290 extern mach_timespec_t IOZeroTvalspec;
291
292 #endif /* IOKIT_DEPRECATED */
293
294 #ifdef __cplusplus
295 } /* extern "C" */
296 #endif
297
298 #endif /* !__IOKIT_IOLIB_H */