2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
27 * 17-Apr-91 Portions from libIO.m, Doug Mitchell at NeXT.
32 #include <IOKit/system.h>
33 #include <mach/sync_policy.h>
34 #include <machine/machine_routines.h>
35 #include <libkern/c++/OSCPPDebug.h>
37 #include <IOKit/assert.h>
39 #include <IOKit/IOReturn.h>
40 #include <IOKit/IOLib.h>
41 #include <IOKit/IOKitDebug.h>
43 mach_timespec_t IOZeroTvalspec
= { 0, 0 };
46 * Static variables for this module.
49 static IOThreadFunc threadArgFcn
;
50 static void * threadArgArg
;
51 static lock_t
* threadArgLock
;
54 enum { kIOMaxPageableMaps
= 16 };
55 enum { kIOPageableMapSize
= 16 * 1024 * 1024 };
56 enum { kIOPageableMaxMapSize
= 64 * 1024 * 1024 };
67 IOMapData maps
[ kIOMaxPageableMaps
];
69 } gIOKitPageableSpace
;
76 static bool libInitialized
;
81 threadArgLock
= lock_alloc( true, NULL
, NULL
);
83 gIOKitPageableSpace
.maps
[0].address
= 0;
84 ret
= kmem_suballoc(kernel_map
,
85 &gIOKitPageableSpace
.maps
[0].address
,
89 &gIOKitPageableSpace
.maps
[0].map
);
90 if (ret
!= KERN_SUCCESS
)
91 panic("failed to allocate iokit pageable map\n");
93 gIOKitPageableSpace
.lock
= mutex_alloc( 0 );
94 gIOKitPageableSpace
.maps
[0].end
= gIOKitPageableSpace
.maps
[0].address
+ kIOPageableMapSize
;
95 gIOKitPageableSpace
.hint
= 0;
96 gIOKitPageableSpace
.count
= 1;
98 libInitialized
= true;
101 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
104 * We pass an argument to a new thread by saving fcn and arg in some
105 * locked variables and starting the thread at ioThreadStart(). This
106 * function retrives fcn and arg and makes the appropriate call.
110 static void ioThreadStart( void )
117 lock_done( threadArgLock
);
124 IOThread
IOCreateThread(IOThreadFunc fcn
, void *arg
)
128 lock_write( threadArgLock
);
132 thread
= kernel_thread( kernel_task
, ioThreadStart
);
138 volatile void IOExitThread()
140 (void) thread_terminate(current_act());
143 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
146 void * IOMalloc(vm_size_t size
)
150 address
= (void *)kalloc(size
);
153 debug_iomalloc_size
+= size
;
158 void IOFree(void * address
, vm_size_t size
)
161 kfree((vm_offset_t
)address
, size
);
163 debug_iomalloc_size
-= size
;
168 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
170 void * IOMallocAligned(vm_size_t size
, vm_size_t alignment
)
173 vm_address_t address
;
174 vm_address_t allocationAddress
;
175 vm_size_t adjustedSize
;
176 vm_offset_t alignMask
;
183 alignMask
= alignment
- 1;
184 adjustedSize
= size
+ sizeof(vm_size_t
) + sizeof(vm_address_t
);
186 if (adjustedSize
>= page_size
) {
188 kr
= kernel_memory_allocate(kernel_map
, &address
,
189 size
, alignMask
, KMA_KOBJECT
);
190 if (KERN_SUCCESS
!= kr
) {
191 IOLog("Failed %08x, %08x\n", size
, alignment
);
197 adjustedSize
+= alignMask
;
198 allocationAddress
= (vm_address_t
) kalloc(adjustedSize
);
200 if (allocationAddress
) {
201 address
= (allocationAddress
+ alignMask
202 + (sizeof(vm_size_t
) + sizeof(vm_address_t
)))
205 *((vm_size_t
*)(address
- sizeof(vm_size_t
)
206 - sizeof(vm_address_t
))) = adjustedSize
;
207 *((vm_address_t
*)(address
- sizeof(vm_address_t
)))
213 assert(0 == (address
& alignMask
));
217 debug_iomalloc_size
+= size
;
220 return (void *) address
;
223 void IOFreeAligned(void * address
, vm_size_t size
)
225 vm_address_t allocationAddress
;
226 vm_size_t adjustedSize
;
233 adjustedSize
= size
+ sizeof(vm_size_t
) + sizeof(vm_address_t
);
234 if (adjustedSize
>= page_size
) {
236 kmem_free( kernel_map
, (vm_address_t
) address
, size
);
239 adjustedSize
= *((vm_size_t
*)( (vm_address_t
) address
240 - sizeof(vm_address_t
) - sizeof(vm_size_t
)));
241 allocationAddress
= *((vm_address_t
*)( (vm_address_t
) address
242 - sizeof(vm_address_t
) ));
244 kfree((vm_offset_t
) allocationAddress
, adjustedSize
);
248 debug_iomalloc_size
-= size
;
252 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
254 void * IOMallocContiguous(vm_size_t size
, vm_size_t alignment
,
255 IOPhysicalAddress
* physicalAddress
)
258 vm_address_t address
;
259 vm_address_t allocationAddress
;
260 vm_size_t adjustedSize
;
261 vm_offset_t alignMask
;
268 alignMask
= alignment
- 1;
269 adjustedSize
= (2 * size
) + sizeof(vm_size_t
) + sizeof(vm_address_t
);
271 if (adjustedSize
>= page_size
) {
273 kr
= kmem_alloc_contig(kernel_map
, &address
, size
,
274 alignMask
, KMA_KOBJECT
);
275 if (KERN_SUCCESS
!= kr
)
280 adjustedSize
+= alignMask
;
281 allocationAddress
= (vm_address_t
)
282 kalloc(adjustedSize
);
283 if (allocationAddress
) {
285 address
= (allocationAddress
+ alignMask
286 + (sizeof(vm_size_t
) + sizeof(vm_address_t
)))
289 if (atop(address
) != atop(address
+ size
- 1))
290 address
= round_page(address
);
292 *((vm_size_t
*)(address
- sizeof(vm_size_t
)
293 - sizeof(vm_address_t
))) = adjustedSize
;
294 *((vm_address_t
*)(address
- sizeof(vm_address_t
)))
300 if( address
&& physicalAddress
)
301 *physicalAddress
= (IOPhysicalAddress
) pmap_extract( kernel_pmap
,
304 assert(0 == (address
& alignMask
));
308 debug_iomalloc_size
+= size
;
311 return (void *) address
;
314 void IOFreeContiguous(void * address
, vm_size_t size
)
316 vm_address_t allocationAddress
;
317 vm_size_t adjustedSize
;
324 adjustedSize
= (2 * size
) + sizeof(vm_size_t
) + sizeof(vm_address_t
);
325 if (adjustedSize
>= page_size
) {
327 kmem_free( kernel_map
, (vm_address_t
) address
, size
);
330 adjustedSize
= *((vm_size_t
*)( (vm_address_t
) address
331 - sizeof(vm_address_t
) - sizeof(vm_size_t
)));
332 allocationAddress
= *((vm_address_t
*)( (vm_address_t
) address
333 - sizeof(vm_address_t
) ));
335 kfree((vm_offset_t
) allocationAddress
, adjustedSize
);
339 debug_iomalloc_size
-= size
;
343 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
345 typedef kern_return_t (*IOIteratePageableMapsCallback
)(vm_map_t map
, void * ref
);
347 kern_return_t
IOIteratePageableMaps(vm_size_t size
,
348 IOIteratePageableMapsCallback callback
, void * ref
)
350 kern_return_t kr
= kIOReturnNotReady
;
357 if (size
> kIOPageableMaxMapSize
)
358 return( kIOReturnBadArgument
);
361 index
= gIOKitPageableSpace
.hint
;
362 attempts
= gIOKitPageableSpace
.count
;
364 kr
= (*callback
)(gIOKitPageableSpace
.maps
[index
].map
, ref
);
365 if( KERN_SUCCESS
== kr
) {
366 gIOKitPageableSpace
.hint
= index
;
372 index
= gIOKitPageableSpace
.count
- 1;
374 if( KERN_SUCCESS
== kr
)
377 mutex_lock( gIOKitPageableSpace
.lock
);
379 index
= gIOKitPageableSpace
.count
;
380 if( index
>= (kIOMaxPageableMaps
- 1)) {
381 mutex_unlock( gIOKitPageableSpace
.lock
);
385 if( size
< kIOPageableMapSize
)
386 segSize
= kIOPageableMapSize
;
391 kr
= kmem_suballoc(kernel_map
,
397 if( KERN_SUCCESS
!= kr
) {
398 mutex_unlock( gIOKitPageableSpace
.lock
);
402 gIOKitPageableSpace
.maps
[index
].map
= map
;
403 gIOKitPageableSpace
.maps
[index
].address
= min
;
404 gIOKitPageableSpace
.maps
[index
].end
= min
+ segSize
;
405 gIOKitPageableSpace
.hint
= index
;
406 gIOKitPageableSpace
.count
= index
+ 1;
408 mutex_unlock( gIOKitPageableSpace
.lock
);
415 struct IOMallocPageableRef
417 vm_address_t address
;
421 static kern_return_t
IOMallocPageableCallback(vm_map_t map
, void * _ref
)
423 struct IOMallocPageableRef
* ref
= (struct IOMallocPageableRef
*) _ref
;
426 kr
= kmem_alloc_pageable( map
, &ref
->address
, ref
->size
);
431 void * IOMallocPageable(vm_size_t size
, vm_size_t alignment
)
433 kern_return_t kr
= kIOReturnNotReady
;
434 struct IOMallocPageableRef ref
;
436 if (alignment
> page_size
)
438 if (size
> kIOPageableMaxMapSize
)
442 kr
= IOIteratePageableMaps( size
, &IOMallocPageableCallback
, &ref
);
443 if( kIOReturnSuccess
!= kr
)
448 debug_iomalloc_size
+= round_page(size
);
451 return( (void *) ref
.address
);
454 vm_map_t
IOPageableMapForAddress( vm_address_t address
)
459 for( index
= 0; index
< gIOKitPageableSpace
.count
; index
++) {
460 if( (address
>= gIOKitPageableSpace
.maps
[index
].address
)
461 && (address
< gIOKitPageableSpace
.maps
[index
].end
) ) {
462 map
= gIOKitPageableSpace
.maps
[index
].map
;
467 IOPanic("IOPageableMapForAddress: null");
472 void IOFreePageable(void * address
, vm_size_t size
)
476 map
= IOPageableMapForAddress( (vm_address_t
) address
);
478 kmem_free( map
, (vm_offset_t
) address
, size
);
481 debug_iomalloc_size
-= round_page(size
);
485 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
487 extern kern_return_t
IOMapPages(vm_map_t map
, vm_offset_t va
, vm_offset_t pa
,
488 vm_size_t length
, unsigned int options
);
490 IOReturn
IOSetProcessorCacheMode( task_t task
, IOVirtualAddress address
,
491 IOByteCount length
, IOOptionBits cacheMode
)
493 IOReturn ret
= kIOReturnSuccess
;
494 vm_offset_t physAddr
;
496 if( task
!= kernel_task
)
497 return( kIOReturnUnsupported
);
499 length
= round_page(address
+ length
) - trunc_page( address
);
500 address
= trunc_page( address
);
503 cacheMode
= (cacheMode
<< kIOMapCacheShift
) & kIOMapCacheMask
;
505 while( (kIOReturnSuccess
== ret
) && (length
> 0) ) {
507 physAddr
= pmap_extract( kernel_pmap
, address
);
509 ret
= IOMapPages( get_task_map(task
), address
, physAddr
, page_size
, cacheMode
);
511 ret
= kIOReturnVMError
;
520 IOReturn
IOFlushProcessorCache( task_t task
, IOVirtualAddress address
,
523 if( task
!= kernel_task
)
524 return( kIOReturnUnsupported
);
527 flush_dcache( (vm_offset_t
) address
, (unsigned) length
, false );
530 return( kIOReturnSuccess
);
533 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
535 SInt32
OSKernelStackRemaining( void )
539 stack
= (((SInt32
) &stack
) & (KERNEL_STACK_SIZE
- 1));
544 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
546 void IOSleep(unsigned milliseconds
)
550 assert_wait_timeout(milliseconds
, THREAD_INTERRUPTIBLE
);
551 wait_result
= thread_block((void (*)(void))0);
552 if (wait_result
!= THREAD_TIMED_OUT
)
553 thread_cancel_timer();
557 * Spin for indicated number of microseconds.
559 void IODelay(unsigned microseconds
)
561 extern void delay(int usec
);
566 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
568 void IOLog(const char *format
, ...)
571 extern void conslog_putc(char);
572 extern void logwakeup();
574 va_start(ap
, format
);
575 _doprnt(format
, &ap
, conslog_putc
, 16);
579 void IOPanic(const char *reason
)
584 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
587 * Convert a integer constant (typically a #define or enum) to a string.
589 static char noValue
[80]; // that's pretty
591 const char *IOFindNameForValue(int value
, const IONamedValue
*regValueArray
)
593 for( ; regValueArray
->name
; regValueArray
++) {
594 if(regValueArray
->value
== value
)
595 return(regValueArray
->name
);
597 sprintf(noValue
, "0x%x (UNDEFINED)", value
);
598 return((const char *)noValue
);
601 IOReturn
IOFindValueForName(const char *string
,
602 const IONamedValue
*regValueArray
,
605 for( ; regValueArray
->name
; regValueArray
++) {
606 if(!strcmp(regValueArray
->name
, string
)) {
607 *value
= regValueArray
->value
;
608 return kIOReturnSuccess
;
611 return kIOReturnBadArgument
;
614 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
616 IOAlignment
IOSizeToAlignment(unsigned int size
)
619 const int intsize
= sizeof(unsigned int) * 8;
621 for (shift
= 1; shift
< intsize
; shift
++) {
622 if (size
& 0x80000000)
623 return (IOAlignment
)(intsize
- shift
);
629 unsigned int IOAlignmentToSize(IOAlignment align
)
633 for (size
= 1; align
; align
--) {
639 IOReturn
IONDRVLibrariesInitialize( void )
641 return( kIOReturnUnsupported
);