]> git.saurik.com Git - apple/xnu.git/blame - iokit/IOKit/IOLib.h
xnu-1699.32.7.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 *
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.
88 @param address Pointer to the allocated memory.
89 @param size Size of the memory allocated. */
90
91void IOFree(void * address, vm_size_t size);
92
93/*! @function IOMallocAligned
94 @abstract Allocates wired memory in the kernel map, with an alignment restriction.
95 @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.
96 @param size Size of the memory requested.
97 @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.
98 @result Pointer to the allocated memory, or zero on failure. */
99
100void * IOMallocAligned(vm_size_t size, vm_offset_t alignment);
101
102/*! @function IOFreeAligned
103 @abstract Frees memory allocated with IOMallocAligned.
104 @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.
105 @param address Pointer to the allocated memory.
106 @param size Size of the memory allocated. */
107
108void IOFreeAligned(void * address, vm_size_t size);
109
110/*! @function IOMallocContiguous
b0d623f7 111 @abstract Deprecated - use IOBufferMemoryDescriptor. Allocates wired memory in the kernel map, with an alignment restriction and physically contiguous.
1c79356b
A
112 @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.
113 @param size Size of the memory requested.
114 @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 115 @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
116 @result Virtual address of the allocated memory, or zero on failure. */
117
118void * IOMallocContiguous(vm_size_t size, vm_size_t alignment,
b0d623f7 119 IOPhysicalAddress * physicalAddress) __attribute__((deprecated));
1c79356b
A
120
121/*! @function IOFreeContiguous
b0d623f7 122 @abstract Deprecated - use IOBufferMemoryDescriptor. Frees memory allocated with IOMallocContiguous.
1c79356b
A
123 @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.
124 @param address Virtual address of the allocated memory.
125 @param size Size of the memory allocated. */
126
b0d623f7 127void IOFreeContiguous(void * address, vm_size_t size) __attribute__((deprecated));
1c79356b
A
128
129
130/*! @function IOMallocPageable
131 @abstract Allocates pageable memory in the kernel map.
132 @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.
133 @param size Size of the memory requested.
134 @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.
135 @result Pointer to the allocated memory, or zero on failure. */
136
137void * IOMallocPageable(vm_size_t size, vm_size_t alignment);
138
139/*! @function IOFreePageable
140 @abstract Frees memory allocated with IOMallocPageable.
141 @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.
142 @param address Virtual address of the allocated memory.
143 @param size Size of the memory allocated. */
144
145void IOFreePageable(void * address, vm_size_t size);
146
147/*
148 * Typed memory allocation macros. Both may block.
149 */
150#define IONew(type,number) (type*)IOMalloc(sizeof(type) * (number) )
151#define IODelete(ptr,type,number) IOFree( (ptr) , sizeof(type) * (number) )
152
55e303ae
A
153/////////////////////////////////////////////////////////////////////////////
154//
155//
156// These functions are now implemented in IOMapper.cpp
157//
158//
159/////////////////////////////////////////////////////////////////////////////
160
161/*! @function IOMappedRead8
162 @abstract Read one byte from the desired "Physical" IOSpace address.
163 @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.
164 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
165 @result Data contained at that location */
166
167UInt8 IOMappedRead8(IOPhysicalAddress address);
168
169/*! @function IOMappedRead16
170 @abstract Read two bytes from the desired "Physical" IOSpace address.
171 @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.
172 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
173 @result Data contained at that location */
174
175UInt16 IOMappedRead16(IOPhysicalAddress address);
176
177/*! @function IOMappedRead32
178 @abstract Read four bytes from the desired "Physical" IOSpace address.
179 @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.
180 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
181 @result Data contained at that location */
182
183UInt32 IOMappedRead32(IOPhysicalAddress address);
184
185/*! @function IOMappedRead64
186 @abstract Read eight bytes from the desired "Physical" IOSpace address.
187 @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.
188 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
189 @result Data contained at that location */
190
191UInt64 IOMappedRead64(IOPhysicalAddress address);
192
193/*! @function IOMappedWrite8
194 @abstract Write one byte to the desired "Physical" IOSpace address.
195 @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.
196 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
197 @param value Data to be writen to the desired location */
198
199void IOMappedWrite8(IOPhysicalAddress address, UInt8 value);
200
201/*! @function IOMappedWrite16
202 @abstract Write two bytes to the desired "Physical" IOSpace address.
203 @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.
204 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
205 @param value Data to be writen to the desired location */
206
207void IOMappedWrite16(IOPhysicalAddress address, UInt16 value);
208
209/*! @function IOMappedWrite32
210 @abstract Write four bytes to the desired "Physical" IOSpace address.
211 @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.
212 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
213 @param value Data to be writen to the desired location */
214
215void IOMappedWrite32(IOPhysicalAddress address, UInt32 value);
216
217/*! @function IOMappedWrite64
218 @abstract Write eight bytes to the desired "Physical" IOSpace address.
219 @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.
220 @param address The desired address, as returned by IOMemoryDescriptor::getPhysicalSegment.
221 @param value Data to be writen to the desired location */
222
223void IOMappedWrite64(IOPhysicalAddress address, UInt64 value);
224
6d2010ae 225/* This function is deprecated. Cache settings may be set for allocated memory with the IOBufferMemoryDescriptor api. */
1c79356b
A
226
227IOReturn IOSetProcessorCacheMode( task_t task, IOVirtualAddress address,
6d2010ae 228 IOByteCount length, IOOptionBits cacheMode ) __attribute__((deprecated));
1c79356b
A
229
230/*! @function IOFlushProcessorCache
231 @abstract Flushes the processor cache for mapped memory.
232 @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.
233 @param task Task the memory is mapped into.
234 @param address Virtual address of the memory.
235 @param length Length of the range to set.
236 @result An IOReturn code. */
237
238IOReturn IOFlushProcessorCache( task_t task, IOVirtualAddress address,
239 IOByteCount length );
240
241/*! @function IOThreadSelf
242 @abstract Returns the osfmk identifier for the currently running thread.
243 @discussion This function returns the current thread (a pointer to the currently active osfmk thread_shuttle). */
244
245#define IOThreadSelf() (current_thread())
246
247/*! @function IOCreateThread
b0d623f7 248 @abstract Deprecated function - use kernel_thread_start(). Create a kernel thread.
2d21ac55 249 @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
250 @param function A C-function pointer where the thread will begin execution.
251 @param argument Caller specified data to be passed to the new thread.
252 @result An IOThread identifier for the new thread, equivalent to an osfmk thread_t. */
253
b0d623f7 254IOThread IOCreateThread(IOThreadFunc function, void *argument) __attribute__((deprecated));
1c79356b
A
255
256/*! @function IOExitThread
b0d623f7 257 @abstract Deprecated function - use thread_terminate(). Terminate execution of current thread.
1c79356b
A
258 @discussion This function destroys the currently running thread, and does not return. */
259
b0d623f7 260void IOExitThread(void) __attribute__((deprecated));
1c79356b
A
261
262/*! @function IOSleep
263 @abstract Sleep the calling thread for a number of milliseconds.
264 @discussion This function blocks the calling thread for at least the number of specified milliseconds, giving time to other processes.
265 @param milliseconds The integer number of milliseconds to wait. */
266
267void IOSleep(unsigned milliseconds);
268
269/*! @function IODelay
270 @abstract Spin delay for a number of microseconds.
271 @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.
272 @param microseconds The integer number of microseconds to spin wait. */
273
274void IODelay(unsigned microseconds);
275
2d21ac55
A
276/*! @function IOPause
277 @abstract Spin delay for a number of nanoseconds.
278 @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.
279 @param microseconds The integer number of nanoseconds to spin wait. */
280
281void IOPause(unsigned nanoseconds);
282
1c79356b
A
283/*! @function IOLog
284 @abstract Log a message to console in text mode, and /var/log/system.log.
285 @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 286 @param format A printf() style format string (see printf(3) documentation).
1c79356b
A
287 @param other arguments described by the format string. */
288
289void IOLog(const char *format, ...)
290__attribute__((format(printf, 1, 2)));
291
b0d623f7
A
292/*! @function IOLogv
293 @abstract Log a message to console in text mode, and /var/log/system.log.
294 @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.
295 @param format A printf() style format string (see printf(3) documentation).
296 @param ap stdarg(3) style variable arguments. */
297
298void IOLogv(const char *format, va_list ap);
299
91447636
A
300#ifndef _FN_KPRINTF
301#define _FN_KPRINTF
1c79356b 302void kprintf(const char *format, ...);
91447636
A
303#endif
304#ifndef _FN_KPRINTF_DECLARED
305#define _FN_KPRINTF_DECLARED
306#endif
1c79356b
A
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);
b0d623f7
A
329#if __LP64__
330#define IOPanic(reason) panic("%s", reason)
331#else
332void IOPanic(const char *reason) __attribute__((deprecated));
333#endif
1c79356b 334
6d2010ae
A
335#ifdef __cplusplus
336class OSDictionary;
337#endif
338
339#ifdef __cplusplus
340OSDictionary *
341#else
342struct OSDictionary *
343#endif
344IOBSDNameMatching( const char * name );
345
346#ifdef __cplusplus
347OSDictionary *
348#else
349struct OSDictionary *
350#endif
351IOOFPathMatching( const char * path, char * buf, int maxLen );
1c79356b
A
352
353/*
354 * Convert between size and a power-of-two alignment.
355 */
356IOAlignment IOSizeToAlignment(unsigned int size);
357unsigned int IOAlignmentToSize(IOAlignment align);
358
359/*
360 * Multiply and divide routines for IOFixed datatype.
361 */
362
363static inline IOFixed IOFixedMultiply(IOFixed a, IOFixed b)
364{
365 return (IOFixed)((((SInt64) a) * ((SInt64) b)) >> 16);
366}
367
368static inline IOFixed IOFixedDivide(IOFixed a, IOFixed b)
369{
370 return (IOFixed)((((SInt64) a) << 16) / ((SInt64) b));
371}
372
373/*
374 * IORound and IOTrunc convenience functions, in the spirit
375 * of vm's round_page() and trunc_page().
376 */
377#define IORound(value,multiple) \
378 ((((value) + (multiple) - 1) / (multiple)) * (multiple))
379
380#define IOTrunc(value,multiple) \
381 (((value) / (multiple)) * (multiple));
382
383
b0d623f7 384#if defined(__APPLE_API_OBSOLETE)
1c79356b
A
385
386/* The following API is deprecated */
387
0b4e3aa0 388/* The API exported by kern/clock.h
1c79356b
A
389 should be used for high resolution timing. */
390
b0d623f7
A
391void IOGetTime( mach_timespec_t * clock_time) __attribute__((deprecated));
392
393#if !defined(__LP64__)
394
395#undef eieio
396#define eieio() \
397 OSSynchronizeIO()
1c79356b
A
398
399extern mach_timespec_t IOZeroTvalspec;
400
b0d623f7
A
401#endif /* !defined(__LP64__) */
402
9bccf70c 403#endif /* __APPLE_API_OBSOLETE */
1c79356b 404
55e303ae 405__END_DECLS
1c79356b
A
406
407#endif /* !__IOKIT_IOLIB_H */