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