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