Chameleon

Chameleon Svn Source Tree

Root/trunk/i386/include/IOKit/IOLib.h

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

Archive Download this file

Revision: 881