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