]> git.saurik.com Git - apple/xnu.git/blob - iokit/IOKit/IOLib.h
xnu-3789.51.2.tar.gz
[apple/xnu.git] / iokit / IOKit / IOLib.h
1 /*
2 * Copyright (c) 1998-2016 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
30 *
31 * HISTORY
32 *
33 */
34
35 #ifndef __IOKIT_IOLIB_H
36 #define __IOKIT_IOLIB_H
37
38 #ifndef KERNEL
39 #error IOLib.h is for kernel use only
40 #endif
41
42 #include <stdarg.h>
43 #include <sys/cdefs.h>
44
45 #include <sys/appleapiopts.h>
46
47 #include <IOKit/system.h>
48
49 #include <IOKit/IOReturn.h>
50 #include <IOKit/IOTypes.h>
51 #include <IOKit/IOLocks.h>
52
53 #include <libkern/OSAtomic.h>
54
55 __BEGIN_DECLS
56
57 #include <kern/thread_call.h>
58 #include <kern/clock.h>
59
60 /*
61 * min/max macros.
62 */
63
64 #define min(a,b) ((a) < (b) ? (a) : (b))
65 #define max(a,b) ((a) > (b) ? (a) : (b))
66
67 /*
68 * These are opaque to the user.
69 */
70 typedef thread_t IOThread;
71 typedef void (*IOThreadFunc)(void *argument);
72
73 /*
74 * Memory allocation functions.
75 */
76
77 /*! @function IOMalloc
78 @abstract Allocates general purpose, wired memory in the kernel map.
79 @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.
80 @param size Size of the memory requested.
81 @result Pointer to the allocated memory, or zero on failure. */
82
83 void * IOMalloc(vm_size_t size);
84
85 /*! @function IOFree
86 @abstract Frees memory allocated with IOMalloc.
87 @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.
88 @param address Pointer to the allocated memory. Must be identical to result
89 @of a prior IOMalloc.
90 @param size Size of the memory allocated. Must be identical to size of
91 @the corresponding IOMalloc */
92
93 void IOFree(void * address, vm_size_t size);
94
95 /*! @function IOMallocAligned
96 @abstract Allocates wired memory in the kernel map, with an alignment restriction.
97 @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.
98 @param size Size of the memory requested.
99 @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.
100 @result Pointer to the allocated memory, or zero on failure. */
101
102 void * IOMallocAligned(vm_size_t size, vm_offset_t alignment);
103
104 /*! @function IOFreeAligned
105 @abstract Frees memory allocated with IOMallocAligned.
106 @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.
107 @param address Pointer to the allocated memory.
108 @param size Size of the memory allocated. */
109
110 void IOFreeAligned(void * address, vm_size_t size);
111
112 /*! @function IOMallocContiguous
113 @abstract Deprecated - use IOBufferMemoryDescriptor. Allocates wired memory in the kernel map, with an alignment restriction and physically contiguous.
114 @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.
115 @param size Size of the memory requested.
116 @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.
117 @param physicalAddress IOMallocContiguous returns the physical address of the allocated memory here, if physicalAddress is a non-zero pointer. The physicalAddress argument is deprecated and should be passed as NULL. To obtain the physical address for a memory buffer, use the IODMACommand class in conjunction with the IOMemoryDescriptor or IOBufferMemoryDescriptor classes.
118 @result Virtual address of the allocated memory, or zero on failure. */
119
120 void * IOMallocContiguous(vm_size_t size, vm_size_t alignment,
121 IOPhysicalAddress * physicalAddress) __attribute__((deprecated));
122
123 /*! @function IOFreeContiguous
124 @abstract Deprecated - use IOBufferMemoryDescriptor. Frees memory allocated with IOMallocContiguous.
125 @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.
126 @param address Virtual address of the allocated memory.
127 @param size Size of the memory allocated. */
128
129 void IOFreeContiguous(void * address, vm_size_t size) __attribute__((deprecated));
130
131
132 /*! @function IOMallocPageable
133 @abstract Allocates pageable memory in the kernel map.
134 @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.
135 @param size Size of the memory requested.
136 @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.
137 @result Pointer to the allocated memory, or zero on failure. */
138
139 void * IOMallocPageable(vm_size_t size, vm_size_t alignment);
140
141 /*! @function IOFreePageable
142 @abstract Frees memory allocated with IOMallocPageable.
143 @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.
144 @param address Virtual address of the allocated memory.
145 @param size Size of the memory allocated. */
146
147 void IOFreePageable(void * address, vm_size_t size);
148
149 /*
150 * Typed memory allocation macros. Both may block.
151 */
152 #define IONew(type,number) \
153 ( ((number) != 0 && ((vm_size_t) ((sizeof(type) * (number) / (number))) != sizeof(type)) /* overflow check 20847256 */ \
154 ? 0 \
155 : ((type*)IOMalloc(sizeof(type) * (number)))) )
156
157 #define IODelete(ptr,type,number) IOFree( (ptr) , sizeof(type) * (number) )
158
159 /////////////////////////////////////////////////////////////////////////////
160 //
161 //
162 // These functions are now implemented in IOMapper.cpp
163 //
164 //
165 /////////////////////////////////////////////////////////////////////////////
166
167 /*! @function IOMappedRead8
168 @abstract Read one byte from the desired "Physical" IOSpace address.
169 @discussion Read one byte from the desired "Physical" IOSpace address. This function allows the developer to read an address returned from any memory descriptor's getPhysicalSegment routine. It can then be used by segmenting a physical page slightly to tag the physical page with its kernel space virtual address.
170 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
171 @result Data contained at that location */
172
173 UInt8 IOMappedRead8(IOPhysicalAddress address);
174
175 /*! @function IOMappedRead16
176 @abstract Read two bytes from the desired "Physical" IOSpace address.
177 @discussion Read two bytes from the desired "Physical" IOSpace address. This function allows the developer to read an address returned from any memory descriptor's getPhysicalSegment routine. It can then be used by segmenting a physical page slightly to tag the physical page with its kernel space virtual address.
178 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
179 @result Data contained at that location */
180
181 UInt16 IOMappedRead16(IOPhysicalAddress address);
182
183 /*! @function IOMappedRead32
184 @abstract Read four bytes from the desired "Physical" IOSpace address.
185 @discussion Read four bytes from the desired "Physical" IOSpace address. This function allows the developer to read an address returned from any memory descriptor's getPhysicalSegment routine. It can then be used by segmenting a physical page slightly to tag the physical page with its kernel space virtual address.
186 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
187 @result Data contained at that location */
188
189 UInt32 IOMappedRead32(IOPhysicalAddress address);
190
191 /*! @function IOMappedRead64
192 @abstract Read eight bytes from the desired "Physical" IOSpace address.
193 @discussion Read eight bytes from the desired "Physical" IOSpace address. This function allows the developer to read an address returned from any memory descriptor's getPhysicalSegment routine. It can then be used by segmenting a physical page slightly to tag the physical page with its kernel space virtual address.
194 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
195 @result Data contained at that location */
196
197 UInt64 IOMappedRead64(IOPhysicalAddress address);
198
199 /*! @function IOMappedWrite8
200 @abstract Write one byte to the desired "Physical" IOSpace address.
201 @discussion Write one byte to the desired "Physical" IOSpace address. This function allows the developer to write to an address returned from any memory descriptor's getPhysicalSegment routine.
202 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
203 @param value Data to be writen to the desired location */
204
205 void IOMappedWrite8(IOPhysicalAddress address, UInt8 value);
206
207 /*! @function IOMappedWrite16
208 @abstract Write two bytes to the desired "Physical" IOSpace address.
209 @discussion Write two bytes to the desired "Physical" IOSpace address. This function allows the developer to write to an address returned from any memory descriptor's getPhysicalSegment routine.
210 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
211 @param value Data to be writen to the desired location */
212
213 void IOMappedWrite16(IOPhysicalAddress address, UInt16 value);
214
215 /*! @function IOMappedWrite32
216 @abstract Write four bytes to the desired "Physical" IOSpace address.
217 @discussion Write four bytes to the desired "Physical" IOSpace address. This function allows the developer to write to an address returned from any memory descriptor's getPhysicalSegment routine.
218 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
219 @param value Data to be writen to the desired location */
220
221 void IOMappedWrite32(IOPhysicalAddress address, UInt32 value);
222
223 /*! @function IOMappedWrite64
224 @abstract Write eight bytes to the desired "Physical" IOSpace address.
225 @discussion Write eight bytes to the desired "Physical" IOSpace address. This function allows the developer to write to an address returned from any memory descriptor's getPhysicalSegment routine.
226 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
227 @param value Data to be writen to the desired location */
228
229 void IOMappedWrite64(IOPhysicalAddress address, UInt64 value);
230
231 /* This function is deprecated. Cache settings may be set for allocated memory with the IOBufferMemoryDescriptor api. */
232
233 IOReturn IOSetProcessorCacheMode( task_t task, IOVirtualAddress address,
234 IOByteCount length, IOOptionBits cacheMode ) __attribute__((deprecated));
235
236 /*! @function IOFlushProcessorCache
237 @abstract Flushes the processor cache for mapped memory.
238 @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.
239 @param task Task the memory is mapped into.
240 @param address Virtual address of the memory.
241 @param length Length of the range to set.
242 @result An IOReturn code. */
243
244 IOReturn IOFlushProcessorCache( task_t task, IOVirtualAddress address,
245 IOByteCount length );
246
247 /*! @function IOThreadSelf
248 @abstract Returns the osfmk identifier for the currently running thread.
249 @discussion This function returns the current thread (a pointer to the currently active osfmk thread_shuttle). */
250
251 #define IOThreadSelf() (current_thread())
252
253 /*! @function IOCreateThread
254 @abstract Deprecated function - use kernel_thread_start(). Create a kernel thread.
255 @discussion This function creates a kernel thread, and passes the caller supplied argument to the new thread. Warning: the value returned by this function is not 100% reliable. There is a race condition where it is possible that the new thread has already terminated before this call returns. Under that circumstance the IOThread returned will be invalid. In general there is little that can be done with this value except compare it against 0. The thread itself can call IOThreadSelf() 100% reliably and that is the prefered mechanism to manipulate the IOThreads state.
256 @param function A C-function pointer where the thread will begin execution.
257 @param argument Caller specified data to be passed to the new thread.
258 @result An IOThread identifier for the new thread, equivalent to an osfmk thread_t. */
259
260 IOThread IOCreateThread(IOThreadFunc function, void *argument) __attribute__((deprecated));
261
262 /*! @function IOExitThread
263 @abstract Deprecated function - use thread_terminate(). Terminate execution of current thread.
264 @discussion This function destroys the currently running thread, and does not return. */
265
266 void IOExitThread(void) __attribute__((deprecated));
267
268 /*! @function IOSleep
269 @abstract Sleep the calling thread for a number of milliseconds.
270 @discussion This function blocks the calling thread for at least the number of specified milliseconds, giving time to other processes.
271 @param milliseconds The integer number of milliseconds to wait. */
272
273 void IOSleep(unsigned milliseconds);
274
275 /*! @function IOSleepWithLeeway
276 @abstract Sleep the calling thread for a number of milliseconds, with a specified leeway the kernel may use for timer coalescing.
277 @discussion This function blocks the calling thread for at least the number of specified milliseconds, giving time to other processes. The kernel may also coalesce any timers involved in the delay, using the leeway given as a guideline.
278 @param intervalMilliseconds The integer number of milliseconds to wait.
279 @param leewayMilliseconds The integer number of milliseconds to use as a timer coalescing guideline. */
280
281 void IOSleepWithLeeway(unsigned intervalMilliseconds, unsigned leewayMilliseconds);
282
283 /*! @function IODelay
284 @abstract Spin delay for a number of microseconds.
285 @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.
286 @param microseconds The integer number of microseconds to spin wait. */
287
288 void IODelay(unsigned microseconds);
289
290 /*! @function IOPause
291 @abstract Spin delay for a number of nanoseconds.
292 @discussion This function spins to delay for at least the number of specified nanoseconds. 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.
293 @param nanoseconds The integer number of nanoseconds to spin wait. */
294
295 void IOPause(unsigned nanoseconds);
296
297 /*! @function IOLog
298 @abstract Log a message to console in text mode, and /var/log/system.log.
299 @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.
300 @param format A printf() style format string (see printf(3) documentation).
301 */
302
303 void IOLog(const char *format, ...)
304 __attribute__((format(printf, 1, 2)));
305
306 /*! @function IOLogv
307 @abstract Log a message to console in text mode, and /var/log/system.log.
308 @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. IOLogv should not be called from interrupt context.
309 @param format A printf() style format string (see printf(3) documentation).
310 @param ap stdarg(3) style variable arguments. */
311
312 void IOLogv(const char *format, va_list ap);
313
314 #ifndef _FN_KPRINTF
315 #define _FN_KPRINTF
316 void kprintf(const char *format, ...);
317 #endif
318 #ifndef _FN_KPRINTF_DECLARED
319 #define _FN_KPRINTF_DECLARED
320 #endif
321
322 /*
323 * Convert a integer constant (typically a #define or enum) to a string
324 * via an array of IONamedValue.
325 */
326 const char *IOFindNameForValue(int value,
327 const IONamedValue *namedValueArray);
328
329 /*
330 * Convert a string to an int via an array of IONamedValue. Returns
331 * kIOReturnSuccess of string found, else returns kIOReturnBadArgument.
332 */
333 IOReturn IOFindValueForName(const char *string,
334 const IONamedValue *regValueArray,
335 int *value); /* RETURNED */
336
337 /*! @function Debugger
338 @abstract Enter the kernel debugger.
339 @discussion This function freezes the kernel and enters the builtin debugger. It may not be possible to exit the debugger without a second machine.
340 @param reason A C-string to describe why the debugger is being entered. */
341
342 void Debugger(const char * reason);
343 #if __LP64__
344 #define IOPanic(reason) panic("%s", reason)
345 #else
346 void IOPanic(const char *reason) __attribute__((deprecated));
347 #endif
348
349 #ifdef __cplusplus
350 class OSDictionary;
351 #endif
352
353 #ifdef __cplusplus
354 OSDictionary *
355 #else
356 struct OSDictionary *
357 #endif
358 IOBSDNameMatching( const char * name );
359
360 #ifdef __cplusplus
361 OSDictionary *
362 #else
363 struct OSDictionary *
364 #endif
365 IOOFPathMatching( const char * path, char * buf, int maxLen ) __attribute__((deprecated));
366
367 /*
368 * Convert between size and a power-of-two alignment.
369 */
370 IOAlignment IOSizeToAlignment(unsigned int size);
371 unsigned int IOAlignmentToSize(IOAlignment align);
372
373 /*
374 * Multiply and divide routines for IOFixed datatype.
375 */
376
377 static inline IOFixed IOFixedMultiply(IOFixed a, IOFixed b)
378 {
379 return (IOFixed)((((SInt64) a) * ((SInt64) b)) >> 16);
380 }
381
382 static inline IOFixed IOFixedDivide(IOFixed a, IOFixed b)
383 {
384 return (IOFixed)((((SInt64) a) << 16) / ((SInt64) b));
385 }
386
387 /*
388 * IORound and IOTrunc convenience functions, in the spirit
389 * of vm's round_page() and trunc_page().
390 */
391 #define IORound(value,multiple) \
392 ((((value) + (multiple) - 1) / (multiple)) * (multiple))
393
394 #define IOTrunc(value,multiple) \
395 (((value) / (multiple)) * (multiple));
396
397
398 #if defined(__APPLE_API_OBSOLETE)
399
400 /* The following API is deprecated */
401
402 /* The API exported by kern/clock.h
403 should be used for high resolution timing. */
404
405 void IOGetTime( mach_timespec_t * clock_time) __attribute__((deprecated));
406
407 #if !defined(__LP64__)
408
409 #undef eieio
410 #define eieio() \
411 OSSynchronizeIO()
412
413 extern mach_timespec_t IOZeroTvalspec;
414
415 #endif /* !defined(__LP64__) */
416
417 #endif /* __APPLE_API_OBSOLETE */
418
419 #if XNU_KERNEL_PRIVATE
420 vm_tag_t
421 IOMemoryTag(vm_map_t map);
422 #endif
423
424 __END_DECLS
425
426 #endif /* !__IOKIT_IOLIB_H */