2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 #include <IOKit/IODeviceTreeSupport.h>
32 #include <libkern/c++/OSContainers.h>
33 #include <IOKit/IODeviceMemory.h>
34 #include <IOKit/IOService.h>
35 #include <IOKit/IOCatalogue.h>
37 #include <IOKit/IOLib.h>
38 #include <IOKit/IOKitKeys.h>
40 #include <pexpert/device_tree.h>
43 #include <machine/machine_routines.h>
44 void DTInit( void * data
);
46 int IODTGetLoaderInfo( char *key
, void **infoAddr
, int *infosize
);
47 void IODTFreeLoaderInfo( char *key
, void *infoAddr
, int infoSize
);
50 #include <IOKit/assert.h>
52 #define IODTSUPPORTDEBUG 0
54 const IORegistryPlane
* gIODTPlane
;
56 static OSArray
* gIODTPHandles
;
57 static OSArray
* gIODTPHandleMap
;
59 const OSSymbol
* gIODTNameKey
;
60 const OSSymbol
* gIODTUnitKey
;
61 const OSSymbol
* gIODTCompatibleKey
;
62 const OSSymbol
* gIODTTypeKey
;
63 const OSSymbol
* gIODTModelKey
;
65 const OSSymbol
* gIODTSizeCellKey
;
66 const OSSymbol
* gIODTAddressCellKey
;
67 const OSSymbol
* gIODTRangeKey
;
69 const OSSymbol
* gIODTPersistKey
;
71 const OSSymbol
* gIODTDefaultInterruptController
;
72 const OSSymbol
* gIODTAAPLInterruptsKey
;
73 const OSSymbol
* gIODTPHandleKey
;
74 const OSSymbol
* gIODTInterruptCellKey
;
75 const OSSymbol
* gIODTInterruptParentKey
;
76 const OSSymbol
* gIODTNWInterruptMappingKey
;
78 OSDictionary
* gIODTSharedInterrupts
;
80 static IORegistryEntry
* MakeReferenceTable( DTEntry dtEntry
, bool copy
);
81 static void AddPHandle( IORegistryEntry
* regEntry
);
82 static void FreePhysicalMemory( vm_offset_t
* range
);
83 static bool IODTMapInterruptsSharing( IORegistryEntry
* regEntry
, OSDictionary
* allInts
);
86 IODeviceTreeAlloc( void * dtTop
)
88 IORegistryEntry
* parent
;
89 IORegistryEntry
* child
;
90 IORegistryIterator
* regIter
;
97 OSDictionary
* allInts
;
103 gIODTPlane
= IORegistryEntry::makePlane( kIODeviceTreePlane
);
105 gIODTNameKey
= OSSymbol::withCStringNoCopy( "name" );
106 gIODTUnitKey
= OSSymbol::withCStringNoCopy( "AAPL,unit-string" );
107 gIODTCompatibleKey
= OSSymbol::withCStringNoCopy( "compatible" );
108 gIODTTypeKey
= OSSymbol::withCStringNoCopy( "device_type" );
109 gIODTModelKey
= OSSymbol::withCStringNoCopy( "model" );
110 gIODTSizeCellKey
= OSSymbol::withCStringNoCopy( "#size-cells" );
111 gIODTAddressCellKey
= OSSymbol::withCStringNoCopy( "#address-cells" );
112 gIODTRangeKey
= OSSymbol::withCStringNoCopy( "ranges" );
113 gIODTPersistKey
= OSSymbol::withCStringNoCopy( "IODTPersist" );
115 assert( gIODTPlane
&& gIODTCompatibleKey
116 && gIODTTypeKey
&& gIODTModelKey
117 && gIODTSizeCellKey
&& gIODTAddressCellKey
&& gIODTRangeKey
118 && gIODTPersistKey
);
120 gIODTDefaultInterruptController
121 = OSSymbol::withCStringNoCopy("IOPrimaryInterruptController");
122 gIODTNWInterruptMappingKey
123 = OSSymbol::withCStringNoCopy("IONWInterrupts");
125 gIODTAAPLInterruptsKey
126 = OSSymbol::withCStringNoCopy("AAPL,interrupts");
128 = OSSymbol::withCStringNoCopy("AAPL,phandle");
130 gIODTInterruptParentKey
131 = OSSymbol::withCStringNoCopy("interrupt-parent");
133 gIODTPHandles
= OSArray::withCapacity( 1 );
134 gIODTPHandleMap
= OSArray::withCapacity( 1 );
136 gIODTInterruptCellKey
137 = OSSymbol::withCStringNoCopy("#interrupt-cells");
139 assert( gIODTDefaultInterruptController
&& gIODTNWInterruptMappingKey
140 && gIODTAAPLInterruptsKey
141 && gIODTPHandleKey
&& gIODTInterruptParentKey
142 && gIODTPHandles
&& gIODTPHandleMap
143 && gIODTInterruptCellKey
146 freeDT
= (kSuccess
== DTLookupEntry( 0, "/chosen/memory-map", &mapEntry
))
147 && (kSuccess
== DTGetProperty( mapEntry
,
148 "DeviceTree", (void **) &dtMap
, &propSize
))
149 && ((2 * sizeof( vm_offset_t
)) == propSize
);
151 parent
= MakeReferenceTable( (DTEntry
)dtTop
, freeDT
);
153 stack
= OSArray::withObjects( (const OSObject
**) &parent
, 1, 10 );
154 DTCreateEntryIterator( (DTEntry
)dtTop
, &iter
);
157 parent
= (IORegistryEntry
*)stack
->getObject( stack
->getCount() - 1);
159 stack
->removeObject( stack
->getCount() - 1);
161 while( kSuccess
== DTIterateEntries( iter
, &dtChild
) ) {
163 child
= MakeReferenceTable( dtChild
, freeDT
);
164 child
->attachToParent( parent
, gIODTPlane
);
168 if( kSuccess
== DTEnterEntry( iter
, dtChild
)) {
169 stack
->setObject( parent
);
172 // only registry holds retain
176 } while( stack
->getCount()
177 && (kSuccess
== DTExitEntry( iter
, &dtChild
)));
180 DTDisposeEntryIterator( iter
);
182 // parent is now root of the created tree
184 // make root name first compatible entry (purely cosmetic)
185 if( (prop
= (OSData
*) parent
->getProperty( gIODTCompatibleKey
))) {
186 parent
->setName( parent
->getName(), gIODTPlane
);
187 parent
->setName( (const char *) prop
->getBytesNoCopy() );
190 // attach tree to meta root
191 parent
->attachToParent( IORegistryEntry::getRegistryRoot(), gIODTPlane
);
195 // free original device tree
197 IODTFreeLoaderInfo( "DeviceTree",
198 (void *)dtMap
[0], round_page_32(dtMap
[1]) );
203 gIODTSharedInterrupts
= OSDictionary::withCapacity(4);
204 allInts
= OSDictionary::withCapacity(4);
206 regIter
= IORegistryIterator::iterateOver( gIODTPlane
,
207 kIORegistryIterateRecursively
);
208 assert( regIter
&& allInts
&& gIODTSharedInterrupts
);
209 if( regIter
&& allInts
&& gIODTSharedInterrupts
) {
210 while( (child
= regIter
->getNextObject())) {
211 IODTMapInterruptsSharing( child
, allInts
);
212 if( !intMap
&& child
->getProperty( gIODTInterruptParentKey
))
215 // Look for a "driver,AAPL,MacOSX,PowerPC" property.
216 if( (obj
= child
->getProperty( "driver,AAPL,MacOSX,PowerPC"))) {
217 gIOCatalogue
->addExtensionsFromArchive((OSData
*)obj
);
218 child
->removeProperty( "driver,AAPL,MacOSX,PowerPC");
221 // some gross pruning
222 child
->removeProperty( "lanLib,AAPL,MacOS,PowerPC");
224 if( (obj
= child
->getProperty( "driver,AAPL,MacOS,PowerPC"))) {
226 if( (0 == (prop
= (OSData
*)child
->getProperty( gIODTTypeKey
)))
227 || (strcmp( "display", (char *) prop
->getBytesNoCopy())) ) {
228 child
->removeProperty( "driver,AAPL,MacOS,PowerPC");
236 parent
->setProperty("allInts", allInts
);
237 parent
->setProperty("sharedInts", gIODTSharedInterrupts
);
239 regIter
= IORegistryIterator::iterateOver( gIODTPlane
,
240 kIORegistryIterateRecursively
);
242 while( (child
= regIter
->getNextObject())) {
244 array
= OSDynamicCast(OSArray
, child
->getProperty( gIOInterruptSpecifiersKey
));
245 for( UInt32 i
= 0; array
&& (i
< array
->getCount()); i
++)
247 IOOptionBits options
;
248 IOReturn ret
= IODTGetInterruptOptions( child
, i
, &options
);
249 if( (ret
!= kIOReturnSuccess
) || options
)
250 IOLog("%s[%ld] %ld (%x)\n", child
->getName(), i
, options
, ret
);
260 // set a key in the root to indicate we found NW interrupt mapping
261 parent
->setProperty( gIODTNWInterruptMappingKey
,
262 (OSObject
*) gIODTNWInterruptMappingKey
);
267 int IODTGetLoaderInfo( char *key
, void **infoAddr
, int *infoSize
)
269 IORegistryEntry
*chosen
;
271 unsigned int *propPtr
;
272 unsigned int propSize
;
274 chosen
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
);
275 if ( chosen
== 0 ) return -1;
277 propObj
= OSDynamicCast( OSData
, chosen
->getProperty(key
) );
278 if ( propObj
== 0 ) return -1;
280 propSize
= propObj
->getLength();
281 if ( propSize
!= (2 * sizeof(UInt32
)) ) return -1;
283 propPtr
= (unsigned int *)propObj
->getBytesNoCopy();
284 if ( propPtr
== 0 ) return -1;
286 *infoAddr
= (void *)propPtr
[0] ;
287 *infoSize
= (int) propPtr
[1];
292 void IODTFreeLoaderInfo( char *key
, void *infoAddr
, int infoSize
)
294 vm_offset_t range
[2];
295 IORegistryEntry
*chosen
;
297 range
[0] = (vm_offset_t
)infoAddr
;
298 range
[1] = (vm_offset_t
)infoSize
;
299 FreePhysicalMemory( range
);
302 chosen
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
);
304 chosen
->removeProperty(key
);
309 static void FreePhysicalMemory( vm_offset_t
* range
)
313 #if defined (__i386__)
314 virt
= ml_boot_ptovirt( range
[0] );
316 virt
= ml_static_ptovirt( range
[0] );
319 ml_static_mfree( virt
, range
[1] );
323 static IORegistryEntry
*
324 MakeReferenceTable( DTEntry dtEntry
, bool copy
)
326 IORegistryEntry
*regEntry
;
327 OSDictionary
*propTable
;
328 const OSSymbol
*nameKey
;
331 DTPropertyIterator dtIter
;
336 bool noLocation
= true;
338 regEntry
= new IOService
;
340 if( regEntry
&& (false == regEntry
->init())) {
346 (kSuccess
== DTCreatePropertyIterator( dtEntry
, &dtIter
))) {
348 propTable
= regEntry
->getPropertyTable();
350 while( kSuccess
== DTIterateProperties( dtIter
, &name
)) {
352 if( kSuccess
!= DTGetProperty( dtEntry
, name
, &prop
, &propSize
))
356 nameKey
= OSSymbol::withCString(name
);
357 data
= OSData::withBytes(prop
, propSize
);
359 nameKey
= OSSymbol::withCStringNoCopy(name
);
360 data
= OSData::withBytesNoCopy(prop
, propSize
);
362 assert( nameKey
&& data
);
364 propTable
->setObject( nameKey
, data
);
368 if( nameKey
== gIODTNameKey
) {
370 sym
= OSSymbol::withCString( (const char *) prop
);
372 sym
= OSSymbol::withCStringNoCopy( (const char *) prop
);
373 regEntry
->setName( sym
);
376 } else if( nameKey
== gIODTUnitKey
) {
377 // all OF strings are null terminated... except this one
378 if( propSize
>= (int) sizeof( location
))
379 propSize
= sizeof( location
) - 1;
380 strncpy( location
, (const char *) prop
, propSize
);
381 location
[ propSize
] = 0;
382 regEntry
->setLocation( location
);
383 propTable
->removeObject( gIODTUnitKey
);
386 } else if( noLocation
&& (0 == strcmp( name
, "reg"))) {
387 // default location - override later
388 sprintf( location
, "%lX", *((UInt32
*) prop
) );
389 regEntry
->setLocation( location
);
392 DTDisposePropertyIterator( dtIter
);
398 static void AddPHandle( IORegistryEntry
* regEntry
)
402 if( regEntry
->getProperty( gIODTInterruptCellKey
)
403 && (data
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTPHandleKey
)))) {
404 // a possible interrupt-parent
405 gIODTPHandles
->setObject( data
);
406 gIODTPHandleMap
->setObject( regEntry
);
410 static IORegistryEntry
* FindPHandle( UInt32 phandle
)
413 IORegistryEntry
*regEntry
= 0;
416 for( i
= 0; (data
= (OSData
*)gIODTPHandles
->getObject( i
)); i
++ ) {
417 if( phandle
== *((UInt32
*)data
->getBytesNoCopy())) {
418 regEntry
= (IORegistryEntry
*)
419 gIODTPHandleMap
->getObject( i
);
427 static bool GetUInt32( IORegistryEntry
* regEntry
, const OSSymbol
* name
,
432 if( (data
= OSDynamicCast( OSData
, regEntry
->getProperty( name
)))
433 && (4 == data
->getLength())) {
434 *value
= *((UInt32
*) data
->getBytesNoCopy());
440 IORegistryEntry
* IODTFindInterruptParent( IORegistryEntry
* regEntry
)
442 IORegistryEntry
* parent
;
445 if( GetUInt32( regEntry
, gIODTInterruptParentKey
, &phandle
))
446 parent
= FindPHandle( phandle
);
448 else if( 0 == regEntry
->getProperty( "interrupt-controller"))
449 parent
= regEntry
->getParentEntry( gIODTPlane
);
456 const OSSymbol
* IODTInterruptControllerName( IORegistryEntry
* regEntry
)
463 ok
= GetUInt32( regEntry
, gIODTPHandleKey
, &phandle
);
467 sprintf( buf
, "IOInterruptController%08lX", phandle
);
468 sym
= OSSymbol::withCString( buf
);
475 #define unexpected(a) { kprintf("unexpected %s:%d\n", __FILE__, __LINE__); a; }
477 static void IODTGetICellCounts( IORegistryEntry
* regEntry
,
478 UInt32
* iCellCount
, UInt32
* aCellCount
)
480 if( !GetUInt32( regEntry
, gIODTInterruptCellKey
, iCellCount
))
481 unexpected( *iCellCount
= 1 );
482 if( !GetUInt32( regEntry
, gIODTAddressCellKey
, aCellCount
))
486 UInt32
IODTMapOneInterrupt( IORegistryEntry
* regEntry
, UInt32
* intSpec
,
487 OSData
** spec
, const OSSymbol
** controller
)
489 IORegistryEntry
*parent
= 0;
495 UInt32 acells
, icells
, pacells
, picells
, cell
;
496 UInt32 i
, original_icells
;
497 bool cmp
, ok
= false;
499 parent
= IODTFindInterruptParent( regEntry
);
500 IODTGetICellCounts( parent
, &icells
, &acells
);
503 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "reg" ));
504 if( data
&& (data
->getLength() >= (acells
* sizeof( UInt32
))))
505 addrCmp
= (UInt32
*) data
->getBytesNoCopy();
507 original_icells
= icells
;
512 kprintf ("IODTMapOneInterrupt: current regEntry name %s\n", regEntry
->getName());
513 kprintf ("acells - icells: ");
514 for (i
= 0; i
< acells
; i
++) kprintf ("0x%08X ", addrCmp
[i
]);
516 for (i
= 0; i
< icells
; i
++) kprintf ("0x%08X ", intSpec
[i
]);
520 if( parent
&& (data
= OSDynamicCast( OSData
,
521 regEntry
->getProperty( "interrupt-controller")))) {
522 // found a controller - don't want to follow cascaded controllers
524 *spec
= OSData::withBytesNoCopy( (void *) intSpec
,
525 icells
* sizeof( UInt32
));
526 *controller
= IODTInterruptControllerName( regEntry
);
527 ok
= (*spec
&& *controller
);
528 } else if( parent
&& (data
= OSDynamicCast( OSData
,
529 regEntry
->getProperty( "interrupt-map")))) {
531 map
= (UInt32
*) data
->getBytesNoCopy();
532 endMap
= map
+ (data
->getLength() / sizeof(UInt32
));
533 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "interrupt-map-mask" ));
534 if( data
&& (data
->getLength() >= ((acells
+ icells
) * sizeof( UInt32
))))
535 maskCmp
= (UInt32
*) data
->getBytesNoCopy();
541 kprintf (" maskCmp: ");
542 for (i
= 0; i
< acells
+ icells
; i
++) {
545 kprintf ("0x%08X ", maskCmp
[i
]);
548 kprintf (" masked: ");
549 for (i
= 0; i
< acells
+ icells
; i
++) {
552 kprintf ("0x%08X ", ((i
< acells
) ? addrCmp
[i
] : intSpec
[i
-acells
]) & maskCmp
[i
]);
556 kprintf ("no maskCmp\n");
561 for (i
= 0; i
< acells
+ icells
; i
++) {
564 kprintf ("0x%08X ", map
[i
]);
568 for( i
= 0, cmp
= true; cmp
&& (i
< (acells
+ icells
)); i
++) {
569 cell
= (i
< acells
) ? addrCmp
[i
] : intSpec
[ i
- acells
];
572 cmp
= (cell
== map
[i
]);
575 map
+= acells
+ icells
;
576 if( 0 == (parent
= FindPHandle( *(map
++) )))
579 IODTGetICellCounts( parent
, &picells
, &pacells
);
582 intSpec
= map
+ pacells
;
585 map
+= pacells
+ picells
;
587 } while( !cmp
&& (map
< endMap
) );
593 IODTGetICellCounts( parent
, &icells
, &acells
);
599 return( ok
? original_icells
: 0 );
602 IOReturn
IODTGetInterruptOptions( IORegistryEntry
* regEntry
, int source
, IOOptionBits
* options
)
604 OSArray
* controllers
;
605 OSArray
* specifiers
;
612 controllers
= OSDynamicCast(OSArray
, regEntry
->getProperty(gIOInterruptControllersKey
));
613 specifiers
= OSDynamicCast(OSArray
, regEntry
->getProperty(gIOInterruptSpecifiersKey
));
615 if( !controllers
|| !specifiers
)
616 return (kIOReturnNoInterrupt
);
618 shared
= (OSArray
*) gIODTSharedInterrupts
->getObject(
619 (const OSSymbol
*) controllers
->getObject(source
) );
621 return (kIOReturnSuccess
);
623 spec
= specifiers
->getObject(source
);
625 return (kIOReturnNoInterrupt
);
627 for (unsigned int i
= 0;
628 (oneSpec
= shared
->getObject(i
))
629 && (!oneSpec
->isEqualTo(spec
));
633 *options
= kIODTInterruptShared
;
635 return (kIOReturnSuccess
);
638 static bool IODTMapInterruptsSharing( IORegistryEntry
* regEntry
, OSDictionary
* allInts
)
640 IORegistryEntry
* parent
;
648 OSArray
* controllerInts
;
649 const OSSymbol
* controller
;
650 OSArray
* controllers
;
654 nw
= (0 == (local
= OSDynamicCast( OSData
,
655 regEntry
->getProperty( gIODTAAPLInterruptsKey
))));
656 if( nw
&& (0 == (local
= OSDynamicCast( OSData
,
657 regEntry
->getProperty( "interrupts")))))
658 return( true ); // nothing to see here
660 if( nw
&& (parent
= regEntry
->getParentEntry( gIODTPlane
))) {
661 // check for bridges on old world
662 if( (local2
= OSDynamicCast( OSData
,
663 parent
->getProperty( gIODTAAPLInterruptsKey
)))) {
669 localBits
= (UInt32
*) local
->getBytesNoCopy();
670 localEnd
= localBits
+ (local
->getLength() / sizeof( UInt32
));
671 mapped
= OSArray::withCapacity( 1 );
672 controllers
= OSArray::withCapacity( 1 );
674 ok
= (mapped
&& controllers
);
678 skip
= IODTMapOneInterrupt( regEntry
, localBits
, &map
, &controller
);
680 IOLog("%s: error mapping interrupt[%d]\n",
681 regEntry
->getName(), mapped
->getCount());
685 map
= OSData::withData( local
, mapped
->getCount() * sizeof( UInt32
),
687 controller
= gIODTDefaultInterruptController
;
688 controller
->retain();
692 mapped
->setObject( map
);
693 controllers
->setObject( controller
);
697 controllerInts
= (OSArray
*) allInts
->getObject( controller
);
700 for (unsigned int i
= 0; (oneMap
= controllerInts
->getObject(i
)); i
++)
702 if (map
->isEqualTo(oneMap
))
704 controllerInts
= (OSArray
*) gIODTSharedInterrupts
->getObject( controller
);
706 controllerInts
->setObject(map
);
709 controllerInts
= OSArray::withObjects( (const OSObject
**) &map
, 1, 4 );
712 gIODTSharedInterrupts
->setObject( controller
, controllerInts
);
713 controllerInts
->release();
720 controllerInts
->setObject(map
);
724 controllerInts
= OSArray::withObjects( (const OSObject
**) &map
, 1, 16 );
727 allInts
->setObject( controller
, controllerInts
);
728 controllerInts
->release();
734 controller
->release();
736 } while( localBits
< localEnd
);
738 ok
&= (localBits
== localEnd
);
742 ok
= regEntry
->setProperty( gIOInterruptControllersKey
, controllers
);
743 ok
&= regEntry
->setProperty( gIOInterruptSpecifiersKey
, mapped
);
747 controllers
->release();
754 bool IODTMapInterrupts( IORegistryEntry
* regEntry
)
756 return( IODTMapInterruptsSharing( regEntry
, 0 ));
763 CompareKey( OSString
* key
,
764 const IORegistryEntry
* table
, const OSSymbol
* propName
)
772 const char *lastName
;
775 const char *result
= 0;
777 if( 0 == (prop
= table
->getProperty( propName
)))
780 if( (data
= OSDynamicCast( OSData
, prop
))) {
781 names
= (const char *) data
->getBytesNoCopy();
782 lastName
= names
+ data
->getLength();
783 } else if( (string
= OSDynamicCast( OSString
, prop
))) {
784 names
= string
->getCStringNoCopy();
785 lastName
= names
+ string
->getLength() + 1;
789 ckey
= key
->getCStringNoCopy();
790 keyLen
= key
->getLength();
791 wild
= ('*' == key
->getChar( keyLen
- 1 ));
794 // for each name in the property
796 matched
= (0 == strncmp( ckey
, names
, keyLen
- 1 ));
798 matched
= (keyLen
== strlen( names
))
799 && (0 == strncmp( ckey
, names
, keyLen
));
804 names
= names
+ strlen( names
) + 1;
806 } while( (names
< lastName
) && (false == matched
));
812 bool IODTCompareNubName( const IORegistryEntry
* regEntry
,
813 OSString
* name
, OSString
** matchingName
)
818 matched
= (0 != (result
= CompareKey( name
, regEntry
, gIODTNameKey
)))
819 || (0 != (result
= CompareKey( name
, regEntry
, gIODTCompatibleKey
)))
820 || (0 != (result
= CompareKey( name
, regEntry
, gIODTTypeKey
)))
821 || (0 != (result
= CompareKey( name
, regEntry
, gIODTModelKey
)));
823 if( result
&& matchingName
)
824 *matchingName
= OSString::withCString( result
);
826 return( result
!= 0 );
829 bool IODTMatchNubWithKeys( IORegistryEntry
* regEntry
,
835 obj
= OSUnserialize( keys
, 0 );
838 result
= regEntry
->compareNames( obj
);
842 else IOLog("Couldn't unserialize %s\n", keys
);
848 OSCollectionIterator
* IODTFindMatchingEntries( IORegistryEntry
* from
,
849 IOOptionBits options
, const char * keys
)
852 IORegistryEntry
*next
;
853 IORegistryIterator
*iter
;
854 OSCollectionIterator
*cIter
;
856 bool minus
= options
& kIODTExclusive
;
859 iter
= IORegistryIterator::iterateOver( from
, gIODTPlane
,
860 (options
& kIODTRecursive
) ? kIORegistryIterateRecursively
: 0 );
867 result
= OSSet::withCapacity( 3 );
872 while( (next
= iter
->getNextObject())) {
874 // Look for existence of a debug property to skip
875 if( next
->getProperty("AAPL,ignore"))
879 cmp
= IODTMatchNubWithKeys( next
, keys
);
880 if( (minus
&& (false == cmp
))
881 || ((false == minus
) && (false != cmp
)) )
882 result
->setObject( next
);
884 result
->setObject( next
);
886 } while( !iter
->isValid());
891 cIter
= OSCollectionIterator::withCollection( result
);
898 struct IODTPersistent
{
899 IODTCompareAddressCellFunc compareFunc
;
900 IODTNVLocationFunc locationFunc
;
903 void IODTSetResolving( IORegistryEntry
* regEntry
,
904 IODTCompareAddressCellFunc compareFunc
,
905 IODTNVLocationFunc locationFunc
)
907 IODTPersistent persist
;
910 persist
.compareFunc
= compareFunc
;
911 persist
.locationFunc
= locationFunc
;
912 prop
= OSData::withBytes( &persist
, sizeof( persist
));
916 regEntry
->setProperty( gIODTPersistKey
, prop
);
921 static SInt32
DefaultCompare( UInt32 cellCount
, UInt32 left
[], UInt32 right
[] )
924 return( left
[ cellCount
] - right
[ cellCount
] );
927 void IODTGetCellCounts( IORegistryEntry
* regEntry
,
928 UInt32
* sizeCount
, UInt32
* addressCount
)
930 if( !GetUInt32( regEntry
, gIODTSizeCellKey
, sizeCount
))
932 if( !GetUInt32( regEntry
, gIODTAddressCellKey
, addressCount
))
937 // Given addr & len cells from our child, find it in our ranges property, then
938 // look in our parent to resolve the base of the range for us.
940 // Range[]: child-addr our-addr child-len
941 // #cells: child ours child
943 bool IODTResolveAddressCell( IORegistryEntry
* regEntry
,
945 IOPhysicalAddress
* phys
, IOPhysicalLength
* len
)
947 IORegistryEntry
*parent
;
949 // cells in addresses at regEntry
950 UInt32 sizeCells
, addressCells
;
951 // cells in addresses below regEntry
952 UInt32 childSizeCells
, childAddressCells
;
954 UInt32 cell
[ 5 ], offset
= 0, length
;
961 SInt32 diff
, endDiff
;
963 IODTPersistent
*persist
;
964 IODTCompareAddressCellFunc compare
;
966 IODTGetCellCounts( regEntry
, &childSizeCells
, &childAddressCells
);
967 childCells
= childAddressCells
+ childSizeCells
;
969 bcopy( cellsIn
, cell
, 4 * childCells
);
970 if( childSizeCells
> 1)
971 *len
= IOPhysical32( cellsIn
[ childAddressCells
],
972 cellsIn
[ childAddressCells
+ 1 ] );
974 *len
= IOPhysical32( 0, cellsIn
[ childAddressCells
] );
978 prop
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTRangeKey
));
980 /* end of the road */
981 *phys
= IOPhysical32( 0, cell
[ childAddressCells
- 1 ] + offset
);
985 parent
= regEntry
->getParentEntry( gIODTPlane
);
986 IODTGetCellCounts( parent
, &sizeCells
, &addressCells
);
988 if( (length
= prop
->getLength())) {
990 startRange
= (UInt32
*) prop
->getBytesNoCopy();
992 endRanges
= range
+ (length
/ 4);
994 prop
= (OSData
*) regEntry
->getProperty( gIODTPersistKey
);
996 persist
= (IODTPersistent
*) prop
->getBytesNoCopy();
997 compare
= persist
->compareFunc
;
999 compare
= DefaultCompare
;
1003 range
+= (childCells
+ addressCells
) ) {
1005 // is cell start >= range start?
1006 diff
= (*compare
)( childAddressCells
, cell
, range
);
1010 ok
= (0 == cell
[childCells
- 1]);
1013 // search for cell end
1014 bcopy(cell
, endCell
, childAddressCells
* sizeof(UInt32
));
1015 endCell
[childAddressCells
- 1] += cell
[childCells
- 1] - 1;
1016 lookRange
= startRange
;
1018 lookRange
< endRanges
;
1019 lookRange
+= (childCells
+ addressCells
) )
1021 // is cell >= range start?
1022 endDiff
= (*compare
)( childAddressCells
, endCell
, lookRange
);
1025 if ((endDiff
- cell
[childCells
- 1] + 1 + lookRange
[childAddressCells
+ addressCells
- 1])
1026 == (diff
+ range
[childAddressCells
+ addressCells
- 1]))
1039 // Get the physical start of the range from our parent
1040 bcopy( range
+ childAddressCells
, cell
, 4 * addressCells
);
1041 bzero( cell
+ addressCells
, 4 * sizeCells
);
1043 } /* else zero length range => pass thru to parent */
1046 childSizeCells
= sizeCells
;
1047 childAddressCells
= addressCells
;
1048 childCells
= childAddressCells
+ childSizeCells
;
1050 while( ok
&& regEntry
);
1056 OSArray
* IODTResolveAddressing( IORegistryEntry
* regEntry
,
1057 const char * addressPropertyName
,
1058 IODeviceMemory
* parent
)
1060 IORegistryEntry
*parentEntry
;
1061 OSData
*addressProperty
;
1062 UInt32 sizeCells
, addressCells
, cells
;
1065 IOPhysicalAddress phys
;
1066 IOPhysicalLength len
;
1068 IODeviceMemory
*range
;
1070 parentEntry
= regEntry
->getParentEntry( gIODTPlane
);
1071 addressProperty
= (OSData
*) regEntry
->getProperty( addressPropertyName
);
1072 if( (0 == addressProperty
) || (0 == parentEntry
))
1075 IODTGetCellCounts( parentEntry
, &sizeCells
, &addressCells
);
1079 cells
= sizeCells
+ addressCells
;
1080 reg
= (UInt32
*) addressProperty
->getBytesNoCopy();
1081 num
= addressProperty
->getLength() / (4 * cells
);
1083 array
= OSArray::withCapacity( 1 );
1087 for( i
= 0; i
< num
; i
++) {
1088 if( IODTResolveAddressCell( parentEntry
, reg
, &phys
, &len
)) {
1091 range
= IODeviceMemory::withSubRange( parent
,
1092 phys
- parent
->getPhysicalAddress(), len
);
1094 range
= IODeviceMemory::withRange( phys
, len
);
1096 array
->setObject( range
);
1101 regEntry
->setProperty( gIODeviceMemoryKey
, array
);
1102 array
->release(); /* ??? */
1107 static void IODTGetNVLocation(
1108 IORegistryEntry
* parent
,
1109 IORegistryEntry
* regEntry
,
1110 UInt8
* busNum
, UInt8
* deviceNum
, UInt8
* functionNum
)
1114 IODTPersistent
*persist
;
1117 prop
= (OSData
*) parent
->getProperty( gIODTPersistKey
);
1119 persist
= (IODTPersistent
*) prop
->getBytesNoCopy();
1120 (*persist
->locationFunc
)( regEntry
, busNum
, deviceNum
, functionNum
);
1122 prop
= (OSData
*) regEntry
->getProperty( "reg" );
1125 cell
= (UInt32
*) prop
->getBytesNoCopy();
1127 *deviceNum
= 0x1f & (cell
[ 0 ] >> 24);
1137 * Try to make the same messed up descriptor as Mac OS
1140 IOReturn
IODTMakeNVDescriptor( IORegistryEntry
* regEntry
,
1141 IONVRAMDescriptor
* hdr
)
1143 IORegistryEntry
*parent
;
1145 UInt32 bridgeDevices
;
1153 for(level
= 0, bridgeDevices
= 0;
1154 (parent
= regEntry
->getParentEntry( gIODTPlane
)) && (level
< 7); level
++ ) {
1156 IODTGetNVLocation( parent
, regEntry
,
1157 &busNum
, &deviceNum
, &functionNum
);
1159 bridgeDevices
|= ((deviceNum
& 0x1f) << ((level
- 1) * 5));
1161 hdr
->busNum
= busNum
;
1162 hdr
->deviceNum
= deviceNum
;
1163 hdr
->functionNum
= functionNum
;
1167 hdr
->bridgeCount
= level
- 2;
1168 hdr
->bridgeDevices
= bridgeDevices
;
1170 return( kIOReturnSuccess
);
1173 OSData
* IODTFindSlotName( IORegistryEntry
* regEntry
, UInt32 deviceNumber
)
1175 IORegistryEntry
*parent
;
1184 data
= (OSData
*) regEntry
->getProperty("AAPL,slot-name");
1187 parent
= regEntry
->getParentEntry( gIODTPlane
);
1190 data
= OSDynamicCast( OSData
, parent
->getProperty("slot-names"));
1193 if( data
->getLength() <= 4)
1196 bits
= (UInt32
*) data
->getBytesNoCopy();
1198 if( (0 == (mask
& (1 << deviceNumber
))))
1201 names
= (char *)(bits
+ 1);
1202 lastName
= names
+ (data
->getLength() - 4);
1204 for( i
= 0; (i
<= deviceNumber
) && (names
< lastName
); i
++ ) {
1206 if( mask
& (1 << i
)) {
1207 if( i
== deviceNumber
) {
1208 data
= OSData::withBytesNoCopy( names
, 1 + strlen( names
));
1210 regEntry
->setProperty("AAPL,slot-name", data
);
1215 names
+= 1 + strlen( names
);
1222 extern "C" IOReturn
IONDRVLibrariesInitialize( IOService
* provider
)
1224 return( kIOReturnUnsupported
);