]> git.saurik.com Git - apple/xnu.git/blame - iokit/IOKit/IOLib.h
xnu-2782.40.9.tar.gz
[apple/xnu.git] / iokit / IOKit / IOLib.h
CommitLineData
1c79356b 1/*
316670eb 2 * Copyright (c) 1998-2011 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 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.
8f6c56a5 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.
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
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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
b0d623f7 42#include <stdarg.h>
55e303ae
A
43#include <sys/cdefs.h>
44
9bccf70c 45#include <sys/appleapiopts.h>
1c79356b
A
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
55e303ae 55__BEGIN_DECLS
1c79356b
A
56
57#include <kern/thread_call.h>
58#include <kern/clock.h>
0b4e3aa0 59
1c79356b
A
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 */
70typedef thread_t IOThread;
71typedef 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
83void * 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.
316670eb
A
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 */
1c79356b
A
92
93void 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
102void * 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
110void IOFreeAligned(void * address, vm_size_t size);
111
112/*! @function IOMallocContiguous
b0d623f7 113 @abstract Deprecated - use IOBufferMemoryDescriptor. Allocates wired memory in the kernel map, with an alignment restriction and physically contiguous.
1c79356b
A
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.
0c530ab8 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.
1c79356b
A
118 @result Virtual address of the allocated memory, or zero on failure. */
119
120void * IOMallocContiguous(vm_size_t size, vm_size_t alignment,
b0d623f7 121 IOPhysicalAddress * physicalAddress) __attribute__((deprecated));
1c79356b
A
122
123/*! @function IOFreeContiguous
b0d623f7 124 @abstract Deprecated - use IOBufferMemoryDescriptor. Frees memory allocated with IOMallocContiguous.
1c79356b
A
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
b0d623f7 129void IOFreeContiguous(void * address, vm_size_t size) __attribute__((deprecated));
1c79356b
A
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
139void * 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
147void IOFreePageable(void * address, vm_size_t size);
148
149/*
150 * Typed memory allocation macros. Both may block.
151 */
c18c124e
A
152#define IONew(type,number) \
153( ((number) != 0 && ((vm_size_t) ((sizeof(type) * (number) / (number))) != sizeof(type)) /* overflow check 21532969 */ \
154? 0 \
155: ((type*)IOMalloc(sizeof(type) * (number)))) )
1c79356b
A
156#define IODelete(ptr,type,number) IOFree( (ptr) , sizeof(type) * (number) )
157
55e303ae
A
158/////////////////////////////////////////////////////////////////////////////
159//
160//
161// These functions are now implemented in IOMapper.cpp
162//
163//
164/////////////////////////////////////////////////////////////////////////////
165
166/*! @function IOMappedRead8
167 @abstract Read one byte from the desired "Physical" IOSpace address.
168 @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.
169 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
170 @result Data contained at that location */
171
172UInt8 IOMappedRead8(IOPhysicalAddress address);
173
174/*! @function IOMappedRead16
175 @abstract Read two bytes from the desired "Physical" IOSpace address.
176 @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.
177 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
178 @result Data contained at that location */
179
180UInt16 IOMappedRead16(IOPhysicalAddress address);
181
182/*! @function IOMappedRead32
183 @abstract Read four bytes from the desired "Physical" IOSpace address.
184 @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.
185 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
186 @result Data contained at that location */
187
188UInt32 IOMappedRead32(IOPhysicalAddress address);
189
190/*! @function IOMappedRead64
191 @abstract Read eight bytes from the desired "Physical" IOSpace address.
192 @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.
193 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
194 @result Data contained at that location */
195
196UInt64 IOMappedRead64(IOPhysicalAddress address);
197
198/*! @function IOMappedWrite8
199 @abstract Write one byte to the desired "Physical" IOSpace address.
200 @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.
201 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
202 @param value Data to be writen to the desired location */
203
204void IOMappedWrite8(IOPhysicalAddress address, UInt8 value);
205
206/*! @function IOMappedWrite16
207 @abstract Write two bytes to the desired "Physical" IOSpace address.
208 @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.
209 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
210 @param value Data to be writen to the desired location */
211
212void IOMappedWrite16(IOPhysicalAddress address, UInt16 value);
213
214/*! @function IOMappedWrite32
215 @abstract Write four bytes to the desired "Physical" IOSpace address.
216 @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.
217 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
218 @param value Data to be writen to the desired location */
219
220void IOMappedWrite32(IOPhysicalAddress address, UInt32 value);
221
222/*! @function IOMappedWrite64
223 @abstract Write eight bytes to the desired "Physical" IOSpace address.
224 @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.
225 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
226 @param value Data to be writen to the desired location */
227
228void IOMappedWrite64(IOPhysicalAddress address, UInt64 value);
229
6d2010ae 230/* This function is deprecated. Cache settings may be set for allocated memory with the IOBufferMemoryDescriptor api. */
1c79356b
A
231
232IOReturn IOSetProcessorCacheMode( task_t task, IOVirtualAddress address,
6d2010ae 233 IOByteCount length, IOOptionBits cacheMode ) __attribute__((deprecated));
1c79356b
A
234
235/*! @function IOFlushProcessorCache
236 @abstract Flushes the processor cache for mapped memory.
237 @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.
238 @param task Task the memory is mapped into.
239 @param address Virtual address of the memory.
240 @param length Length of the range to set.
241 @result An IOReturn code. */
242
243IOReturn IOFlushProcessorCache( task_t task, IOVirtualAddress address,
244 IOByteCount length );
245
246/*! @function IOThreadSelf
247 @abstract Returns the osfmk identifier for the currently running thread.
248 @discussion This function returns the current thread (a pointer to the currently active osfmk thread_shuttle). */
249
250#define IOThreadSelf() (current_thread())
251
252/*! @function IOCreateThread
b0d623f7 253 @abstract Deprecated function - use kernel_thread_start(). Create a kernel thread.
2d21ac55 254 @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.
1c79356b
A
255 @param function A C-function pointer where the thread will begin execution.
256 @param argument Caller specified data to be passed to the new thread.
257 @result An IOThread identifier for the new thread, equivalent to an osfmk thread_t. */
258
b0d623f7 259IOThread IOCreateThread(IOThreadFunc function, void *argument) __attribute__((deprecated));
1c79356b
A
260
261/*! @function IOExitThread
b0d623f7 262 @abstract Deprecated function - use thread_terminate(). Terminate execution of current thread.
1c79356b
A
263 @discussion This function destroys the currently running thread, and does not return. */
264
b0d623f7 265void IOExitThread(void) __attribute__((deprecated));
1c79356b
A
266
267/*! @function IOSleep
268 @abstract Sleep the calling thread for a number of milliseconds.
269 @discussion This function blocks the calling thread for at least the number of specified milliseconds, giving time to other processes.
270 @param milliseconds The integer number of milliseconds to wait. */
271
272void IOSleep(unsigned milliseconds);
273
274/*! @function IODelay
275 @abstract Spin delay for a number of microseconds.
276 @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.
277 @param microseconds The integer number of microseconds to spin wait. */
278
279void IODelay(unsigned microseconds);
280
2d21ac55
A
281/*! @function IOPause
282 @abstract Spin delay for a number of nanoseconds.
283 @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.
284 @param microseconds The integer number of nanoseconds to spin wait. */
285
286void IOPause(unsigned nanoseconds);
287
1c79356b
A
288/*! @function IOLog
289 @abstract Log a message to console in text mode, and /var/log/system.log.
290 @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.
b0d623f7 291 @param format A printf() style format string (see printf(3) documentation).
1c79356b
A
292 @param other arguments described by the format string. */
293
294void IOLog(const char *format, ...)
295__attribute__((format(printf, 1, 2)));
296
b0d623f7
A
297/*! @function IOLogv
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. IOLogv should not be called from interrupt context.
300 @param format A printf() style format string (see printf(3) documentation).
301 @param ap stdarg(3) style variable arguments. */
302
303void IOLogv(const char *format, va_list ap);
304
91447636
A
305#ifndef _FN_KPRINTF
306#define _FN_KPRINTF
1c79356b 307void kprintf(const char *format, ...);
91447636
A
308#endif
309#ifndef _FN_KPRINTF_DECLARED
310#define _FN_KPRINTF_DECLARED
311#endif
1c79356b
A
312
313/*
314 * Convert a integer constant (typically a #define or enum) to a string
315 * via an array of IONamedValue.
316 */
317const char *IOFindNameForValue(int value,
318 const IONamedValue *namedValueArray);
319
320/*
321 * Convert a string to an int via an array of IONamedValue. Returns
322 * kIOReturnSuccess of string found, else returns kIOReturnBadArgument.
323 */
324IOReturn IOFindValueForName(const char *string,
325 const IONamedValue *regValueArray,
326 int *value); /* RETURNED */
327
328/*! @function Debugger
329 @abstract Enter the kernel debugger.
330 @discussion This function freezes the kernel and enters the builtin debugger. It may not be possible to exit the debugger without a second machine.
331 @param reason A C-string to describe why the debugger is being entered. */
332
333void Debugger(const char * reason);
b0d623f7
A
334#if __LP64__
335#define IOPanic(reason) panic("%s", reason)
336#else
337void IOPanic(const char *reason) __attribute__((deprecated));
338#endif
1c79356b 339
6d2010ae
A
340#ifdef __cplusplus
341class OSDictionary;
342#endif
343
344#ifdef __cplusplus
345OSDictionary *
346#else
347struct OSDictionary *
348#endif
349IOBSDNameMatching( const char * name );
350
351#ifdef __cplusplus
352OSDictionary *
353#else
354struct OSDictionary *
355#endif
316670eb 356IOOFPathMatching( const char * path, char * buf, int maxLen ) __attribute__((deprecated));
1c79356b
A
357
358/*
359 * Convert between size and a power-of-two alignment.
360 */
361IOAlignment IOSizeToAlignment(unsigned int size);
362unsigned int IOAlignmentToSize(IOAlignment align);
363
364/*
365 * Multiply and divide routines for IOFixed datatype.
366 */
367
368static inline IOFixed IOFixedMultiply(IOFixed a, IOFixed b)
369{
370 return (IOFixed)((((SInt64) a) * ((SInt64) b)) >> 16);
371}
372
373static inline IOFixed IOFixedDivide(IOFixed a, IOFixed b)
374{
375 return (IOFixed)((((SInt64) a) << 16) / ((SInt64) b));
376}
377
378/*
379 * IORound and IOTrunc convenience functions, in the spirit
380 * of vm's round_page() and trunc_page().
381 */
382#define IORound(value,multiple) \
383 ((((value) + (multiple) - 1) / (multiple)) * (multiple))
384
385#define IOTrunc(value,multiple) \
386 (((value) / (multiple)) * (multiple));
387
388
b0d623f7 389#if defined(__APPLE_API_OBSOLETE)
1c79356b
A
390
391/* The following API is deprecated */
392
0b4e3aa0 393/* The API exported by kern/clock.h
1c79356b
A
394 should be used for high resolution timing. */
395
b0d623f7
A
396void IOGetTime( mach_timespec_t * clock_time) __attribute__((deprecated));
397
398#if !defined(__LP64__)
399
400#undef eieio
401#define eieio() \
402 OSSynchronizeIO()
1c79356b
A
403
404extern mach_timespec_t IOZeroTvalspec;
405
b0d623f7
A
406#endif /* !defined(__LP64__) */
407
9bccf70c 408#endif /* __APPLE_API_OBSOLETE */
1c79356b 409
55e303ae 410__END_DECLS
1c79356b
A
411
412#endif /* !__IOKIT_IOLIB_H */