2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
29 #ifndef __IOKIT_IOLIB_H
30 #define __IOKIT_IOLIB_H
33 #error IOLib.h is for kernel use only
36 #include <sys/appleapiopts.h>
38 #include <IOKit/system.h>
40 #include <IOKit/IOReturn.h>
41 #include <IOKit/IOTypes.h>
42 #include <IOKit/IOLocks.h>
44 #include <libkern/OSAtomic.h>
50 #include <kern/thread_call.h>
51 #include <kern/clock.h>
57 #define min(a,b) ((a) < (b) ? (a) : (b))
58 #define max(a,b) ((a) > (b) ? (a) : (b))
61 * These are opaque to the user.
63 typedef thread_t IOThread
;
64 typedef void (*IOThreadFunc
)(void *argument
);
67 * Memory allocation functions.
70 /*! @function IOMalloc
71 @abstract Allocates general purpose, wired memory in the kernel map.
72 @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.
73 @param size Size of the memory requested.
74 @result Pointer to the allocated memory, or zero on failure. */
76 void * IOMalloc(vm_size_t size
);
79 @abstract Frees memory allocated with IOMalloc.
80 @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.
81 @param address Pointer to the allocated memory.
82 @param size Size of the memory allocated. */
84 void IOFree(void * address
, vm_size_t size
);
86 /*! @function IOMallocAligned
87 @abstract Allocates wired memory in the kernel map, with an alignment restriction.
88 @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.
89 @param size Size of the memory requested.
90 @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.
91 @result Pointer to the allocated memory, or zero on failure. */
93 void * IOMallocAligned(vm_size_t size
, vm_offset_t alignment
);
95 /*! @function IOFreeAligned
96 @abstract Frees memory allocated with IOMallocAligned.
97 @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.
98 @param address Pointer to the allocated memory.
99 @param size Size of the memory allocated. */
101 void IOFreeAligned(void * address
, vm_size_t size
);
103 /*! @function IOMallocContiguous
104 @abstract Allocates wired memory in the kernel map, with an alignment restriction and physically contiguous.
105 @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.
106 @param size Size of the memory requested.
107 @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.
108 @param physicalAddress IOMallocContiguous returns the physical address of the allocated memory here, if physicalAddress is a non-zero pointer.
109 @result Virtual address of the allocated memory, or zero on failure. */
111 void * IOMallocContiguous(vm_size_t size
, vm_size_t alignment
,
112 IOPhysicalAddress
* physicalAddress
);
114 /*! @function IOFreeContiguous
115 @abstract Frees memory allocated with IOMallocContiguous.
116 @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.
117 @param address Virtual address of the allocated memory.
118 @param size Size of the memory allocated. */
120 void IOFreeContiguous(void * address
, vm_size_t size
);
123 /*! @function IOMallocPageable
124 @abstract Allocates pageable memory in the kernel map.
125 @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.
126 @param size Size of the memory requested.
127 @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.
128 @result Pointer to the allocated memory, or zero on failure. */
130 void * IOMallocPageable(vm_size_t size
, vm_size_t alignment
);
132 /*! @function IOFreePageable
133 @abstract Frees memory allocated with IOMallocPageable.
134 @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.
135 @param address Virtual address of the allocated memory.
136 @param size Size of the memory allocated. */
138 void IOFreePageable(void * address
, vm_size_t size
);
141 * Typed memory allocation macros. Both may block.
143 #define IONew(type,number) (type*)IOMalloc(sizeof(type) * (number) )
144 #define IODelete(ptr,type,number) IOFree( (ptr) , sizeof(type) * (number) )
146 /*! @function IOSetProcessorCacheMode
147 @abstract Sets the processor cache mode for mapped memory.
148 @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.
149 @param task Task the memory is mapped into.
150 @param address Virtual address of the memory.
151 @param length Length of the range to set.
152 @param cacheMode A constant from IOTypes.h, <br>
153 kIOMapDefaultCache to inhibit the cache in I/O areas, kIOMapCopybackCache in general purpose RAM.<br>
154 kIOMapInhibitCache, kIOMapWriteThruCache, kIOMapCopybackCache to set the appropriate caching.<br>
155 @result An IOReturn code.*/
157 IOReturn
IOSetProcessorCacheMode( task_t task
, IOVirtualAddress address
,
158 IOByteCount length
, IOOptionBits cacheMode
);
160 /*! @function IOFlushProcessorCache
161 @abstract Flushes the processor cache for mapped memory.
162 @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.
163 @param task Task the memory is mapped into.
164 @param address Virtual address of the memory.
165 @param length Length of the range to set.
166 @result An IOReturn code. */
168 IOReturn
IOFlushProcessorCache( task_t task
, IOVirtualAddress address
,
169 IOByteCount length
);
171 /*! @function IOThreadSelf
172 @abstract Returns the osfmk identifier for the currently running thread.
173 @discussion This function returns the current thread (a pointer to the currently active osfmk thread_shuttle). */
175 #define IOThreadSelf() (current_thread())
177 /*! @function IOCreateThread
178 @abstract Create a kernel thread.
179 @discussion This function creates a kernel thread, and passes the caller supplied argument to the new thread.
180 @param function A C-function pointer where the thread will begin execution.
181 @param argument Caller specified data to be passed to the new thread.
182 @result An IOThread identifier for the new thread, equivalent to an osfmk thread_t. */
184 IOThread
IOCreateThread(IOThreadFunc function
, void *argument
);
186 /*! @function IOExitThread
187 @abstract Terminate exceution of current thread.
188 @discussion This function destroys the currently running thread, and does not return. */
190 volatile void IOExitThread();
192 /*! @function IOSleep
193 @abstract Sleep the calling thread for a number of milliseconds.
194 @discussion This function blocks the calling thread for at least the number of specified milliseconds, giving time to other processes.
195 @param milliseconds The integer number of milliseconds to wait. */
197 void IOSleep(unsigned milliseconds
);
199 /*! @function IODelay
200 @abstract Spin delay for a number of microseconds.
201 @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.
202 @param microseconds The integer number of microseconds to spin wait. */
204 void IODelay(unsigned microseconds
);
207 @abstract Log a message to console in text mode, and /var/log/system.log.
208 @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.
209 @param format A printf() style format string (see printf() documentation).
210 @param other arguments described by the format string. */
212 void IOLog(const char *format
, ...)
213 __attribute__((format(printf
, 1, 2)));
215 void kprintf(const char *format
, ...);
218 * Convert a integer constant (typically a #define or enum) to a string
219 * via an array of IONamedValue.
221 const char *IOFindNameForValue(int value
,
222 const IONamedValue
*namedValueArray
);
225 * Convert a string to an int via an array of IONamedValue. Returns
226 * kIOReturnSuccess of string found, else returns kIOReturnBadArgument.
228 IOReturn
IOFindValueForName(const char *string
,
229 const IONamedValue
*regValueArray
,
230 int *value
); /* RETURNED */
232 /*! @function Debugger
233 @abstract Enter the kernel debugger.
234 @discussion This function freezes the kernel and enters the builtin debugger. It may not be possible to exit the debugger without a second machine.
235 @param reason A C-string to describe why the debugger is being entered. */
237 void Debugger(const char * reason
);
239 struct OSDictionary
* IOBSDNameMatching( const char * name
);
240 struct OSDictionary
* IOOFPathMatching( const char * path
, char * buf
, int maxLen
);
243 * Convert between size and a power-of-two alignment.
245 IOAlignment
IOSizeToAlignment(unsigned int size
);
246 unsigned int IOAlignmentToSize(IOAlignment align
);
249 * Multiply and divide routines for IOFixed datatype.
252 static inline IOFixed
IOFixedMultiply(IOFixed a
, IOFixed b
)
254 return (IOFixed
)((((SInt64
) a
) * ((SInt64
) b
)) >> 16);
257 static inline IOFixed
IOFixedDivide(IOFixed a
, IOFixed b
)
259 return (IOFixed
)((((SInt64
) a
) << 16) / ((SInt64
) b
));
263 * IORound and IOTrunc convenience functions, in the spirit
264 * of vm's round_page() and trunc_page().
266 #define IORound(value,multiple) \
267 ((((value) + (multiple) - 1) / (multiple)) * (multiple))
269 #define IOTrunc(value,multiple) \
270 (((value) / (multiple)) * (multiple));
273 #ifdef __APPLE_API_OBSOLETE
275 /* The following API is deprecated */
281 void IOPanic(const char *reason
);
283 /* The API exported by kern/clock.h
284 should be used for high resolution timing. */
286 void IOGetTime( mach_timespec_t
* clock_time
);
288 extern mach_timespec_t IOZeroTvalspec
;
290 #endif /* __APPLE_API_OBSOLETE */
296 #endif /* !__IOKIT_IOLIB_H */