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