2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
32 #ifndef __IOKIT_IOLIB_H
33 #define __IOKIT_IOLIB_H
36 #error IOLib.h is for kernel use only
39 #include <sys/cdefs.h>
41 #include <sys/appleapiopts.h>
43 #include <IOKit/system.h>
45 #include <IOKit/IOReturn.h>
46 #include <IOKit/IOTypes.h>
47 #include <IOKit/IOLocks.h>
49 #include <libkern/OSAtomic.h>
53 #include <kern/thread_call.h>
54 #include <kern/clock.h>
60 #define min(a,b) ((a) < (b) ? (a) : (b))
61 #define max(a,b) ((a) > (b) ? (a) : (b))
64 * These are opaque to the user.
66 typedef thread_t IOThread
;
67 typedef void (*IOThreadFunc
)(void *argument
);
70 * Memory allocation functions.
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. */
79 void * IOMalloc(vm_size_t size
);
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. */
87 void IOFree(void * address
, vm_size_t size
);
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. */
96 void * IOMallocAligned(vm_size_t size
, vm_offset_t alignment
);
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. */
104 void IOFreeAligned(void * address
, vm_size_t size
);
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. */
114 void * IOMallocContiguous(vm_size_t size
, vm_size_t alignment
,
115 IOPhysicalAddress
* physicalAddress
);
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. */
123 void IOFreeContiguous(void * address
, vm_size_t size
);
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. */
133 void * IOMallocPageable(vm_size_t size
, vm_size_t alignment
);
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. */
141 void IOFreePageable(void * address
, vm_size_t size
);
144 * Typed memory allocation macros. Both may block.
146 #define IONew(type,number) (type*)IOMalloc(sizeof(type) * (number) )
147 #define IODelete(ptr,type,number) IOFree( (ptr) , sizeof(type) * (number) )
149 /////////////////////////////////////////////////////////////////////////////
152 // These functions are now implemented in IOMapper.cpp
155 /////////////////////////////////////////////////////////////////////////////
157 /*! @function IOMappedRead8
158 @abstract Read one byte from the desired "Physical" IOSpace address.
159 @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.
160 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
161 @result Data contained at that location */
163 UInt8
IOMappedRead8(IOPhysicalAddress address
);
165 /*! @function IOMappedRead16
166 @abstract Read two bytes from the desired "Physical" IOSpace address.
167 @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.
168 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
169 @result Data contained at that location */
171 UInt16
IOMappedRead16(IOPhysicalAddress address
);
173 /*! @function IOMappedRead32
174 @abstract Read four bytes from the desired "Physical" IOSpace address.
175 @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.
176 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
177 @result Data contained at that location */
179 UInt32
IOMappedRead32(IOPhysicalAddress address
);
181 /*! @function IOMappedRead64
182 @abstract Read eight bytes from the desired "Physical" IOSpace address.
183 @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.
184 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
185 @result Data contained at that location */
187 UInt64
IOMappedRead64(IOPhysicalAddress address
);
189 /*! @function IOMappedWrite8
190 @abstract Write one byte to the desired "Physical" IOSpace address.
191 @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.
192 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
193 @param value Data to be writen to the desired location */
195 void IOMappedWrite8(IOPhysicalAddress address
, UInt8 value
);
197 /*! @function IOMappedWrite16
198 @abstract Write two bytes to the desired "Physical" IOSpace address.
199 @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.
200 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
201 @param value Data to be writen to the desired location */
203 void IOMappedWrite16(IOPhysicalAddress address
, UInt16 value
);
205 /*! @function IOMappedWrite32
206 @abstract Write four bytes to the desired "Physical" IOSpace address.
207 @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.
208 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
209 @param value Data to be writen to the desired location */
211 void IOMappedWrite32(IOPhysicalAddress address
, UInt32 value
);
213 /*! @function IOMappedWrite64
214 @abstract Write eight bytes to the desired "Physical" IOSpace address.
215 @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.
216 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
217 @param value Data to be writen to the desired location */
219 void IOMappedWrite64(IOPhysicalAddress address
, UInt64 value
);
221 /*! @function IOSetProcessorCacheMode
222 @abstract Sets the processor cache mode for mapped memory.
223 @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.
224 @param task Task the memory is mapped into.
225 @param address Virtual address of the memory.
226 @param length Length of the range to set.
227 @param cacheMode A constant from IOTypes.h, <br>
228 kIOMapDefaultCache to inhibit the cache in I/O areas, kIOMapCopybackCache in general purpose RAM.<br>
229 kIOMapInhibitCache, kIOMapWriteThruCache, kIOMapCopybackCache to set the appropriate caching.<br>
230 @result An IOReturn code.*/
232 IOReturn
IOSetProcessorCacheMode( task_t task
, IOVirtualAddress address
,
233 IOByteCount length
, IOOptionBits cacheMode
);
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. */
243 IOReturn
IOFlushProcessorCache( task_t task
, IOVirtualAddress address
,
244 IOByteCount length
);
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). */
250 #define IOThreadSelf() (current_thread())
252 /*! @function IOCreateThread
253 @abstract Create a kernel thread.
254 @discussion This function creates a kernel thread, and passes the caller supplied argument to the new thread.
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. */
259 IOThread
IOCreateThread(IOThreadFunc function
, void *argument
);
261 /*! @function IOExitThread
262 @abstract Terminate exceution of current thread.
263 @discussion This function destroys the currently running thread, and does not return. */
265 volatile void IOExitThread();
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. */
272 void IOSleep(unsigned milliseconds
);
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. */
279 void IODelay(unsigned microseconds
);
282 @abstract Log a message to console in text mode, and /var/log/system.log.
283 @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.
284 @param format A printf() style format string (see printf() documentation).
285 @param other arguments described by the format string. */
287 void IOLog(const char *format
, ...)
288 __attribute__((format(printf
, 1, 2)));
290 void kprintf(const char *format
, ...);
293 * Convert a integer constant (typically a #define or enum) to a string
294 * via an array of IONamedValue.
296 const char *IOFindNameForValue(int value
,
297 const IONamedValue
*namedValueArray
);
300 * Convert a string to an int via an array of IONamedValue. Returns
301 * kIOReturnSuccess of string found, else returns kIOReturnBadArgument.
303 IOReturn
IOFindValueForName(const char *string
,
304 const IONamedValue
*regValueArray
,
305 int *value
); /* RETURNED */
307 /*! @function Debugger
308 @abstract Enter the kernel debugger.
309 @discussion This function freezes the kernel and enters the builtin debugger. It may not be possible to exit the debugger without a second machine.
310 @param reason A C-string to describe why the debugger is being entered. */
312 void Debugger(const char * reason
);
314 struct OSDictionary
* IOBSDNameMatching( const char * name
);
315 struct OSDictionary
* IOOFPathMatching( const char * path
, char * buf
, int maxLen
);
318 * Convert between size and a power-of-two alignment.
320 IOAlignment
IOSizeToAlignment(unsigned int size
);
321 unsigned int IOAlignmentToSize(IOAlignment align
);
324 * Multiply and divide routines for IOFixed datatype.
327 static inline IOFixed
IOFixedMultiply(IOFixed a
, IOFixed b
)
329 return (IOFixed
)((((SInt64
) a
) * ((SInt64
) b
)) >> 16);
332 static inline IOFixed
IOFixedDivide(IOFixed a
, IOFixed b
)
334 return (IOFixed
)((((SInt64
) a
) << 16) / ((SInt64
) b
));
338 * IORound and IOTrunc convenience functions, in the spirit
339 * of vm's round_page() and trunc_page().
341 #define IORound(value,multiple) \
342 ((((value) + (multiple) - 1) / (multiple)) * (multiple))
344 #define IOTrunc(value,multiple) \
345 (((value) / (multiple)) * (multiple));
348 #ifdef __APPLE_API_OBSOLETE
350 /* The following API is deprecated */
356 void IOPanic(const char *reason
);
358 /* The API exported by kern/clock.h
359 should be used for high resolution timing. */
361 void IOGetTime( mach_timespec_t
* clock_time
);
363 extern mach_timespec_t IOZeroTvalspec
;
365 #endif /* __APPLE_API_OBSOLETE */
369 #endif /* !__IOKIT_IOLIB_H */