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