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