2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <IOKit/IODeviceTreeSupport.h>
25 #include <libkern/c++/OSContainers.h>
26 #include <IOKit/IODeviceMemory.h>
27 #include <IOKit/IOService.h>
28 #include <IOKit/IOCatalogue.h>
30 #include <IOKit/IOLib.h>
31 #include <IOKit/IOKitKeys.h>
33 #include <pexpert/device_tree.h>
36 #include <machine/machine_routines.h>
37 void DTInit( void * data
);
39 int IODTGetLoaderInfo( char *key
, void **infoAddr
, int *infosize
);
40 void IODTFreeLoaderInfo( char *key
, void *infoAddr
, int infoSize
);
43 #include <IOKit/assert.h>
45 #define IODTSUPPORTDEBUG 0
47 const IORegistryPlane
* gIODTPlane
;
49 static OSArray
* gIODTPHandles
;
50 static OSArray
* gIODTPHandleMap
;
52 const OSSymbol
* gIODTNameKey
;
53 const OSSymbol
* gIODTUnitKey
;
54 const OSSymbol
* gIODTCompatibleKey
;
55 const OSSymbol
* gIODTTypeKey
;
56 const OSSymbol
* gIODTModelKey
;
58 const OSSymbol
* gIODTSizeCellKey
;
59 const OSSymbol
* gIODTAddressCellKey
;
60 const OSSymbol
* gIODTRangeKey
;
62 const OSSymbol
* gIODTPersistKey
;
64 const OSSymbol
* gIODTDefaultInterruptController
;
65 const OSSymbol
* gIODTAAPLInterruptsKey
;
66 const OSSymbol
* gIODTPHandleKey
;
67 const OSSymbol
* gIODTInterruptCellKey
;
68 const OSSymbol
* gIODTInterruptParentKey
;
69 const OSSymbol
* gIODTNWInterruptMappingKey
;
71 OSDictionary
* gIODTSharedInterrupts
;
73 static IORegistryEntry
* MakeReferenceTable( DTEntry dtEntry
, bool copy
);
74 static void AddPHandle( IORegistryEntry
* regEntry
);
75 static void FreePhysicalMemory( vm_offset_t
* range
);
76 static bool IODTMapInterruptsSharing( IORegistryEntry
* regEntry
, OSDictionary
* allInts
);
79 IODeviceTreeAlloc( void * dtTop
)
81 IORegistryEntry
* parent
;
82 IORegistryEntry
* child
;
83 IORegistryIterator
* regIter
;
90 OSDictionary
* allInts
;
96 gIODTPlane
= IORegistryEntry::makePlane( kIODeviceTreePlane
);
98 gIODTNameKey
= OSSymbol::withCStringNoCopy( "name" );
99 gIODTUnitKey
= OSSymbol::withCStringNoCopy( "AAPL,unit-string" );
100 gIODTCompatibleKey
= OSSymbol::withCStringNoCopy( "compatible" );
101 gIODTTypeKey
= OSSymbol::withCStringNoCopy( "device_type" );
102 gIODTModelKey
= OSSymbol::withCStringNoCopy( "model" );
103 gIODTSizeCellKey
= OSSymbol::withCStringNoCopy( "#size-cells" );
104 gIODTAddressCellKey
= OSSymbol::withCStringNoCopy( "#address-cells" );
105 gIODTRangeKey
= OSSymbol::withCStringNoCopy( "ranges" );
106 gIODTPersistKey
= OSSymbol::withCStringNoCopy( "IODTPersist" );
108 assert( gIODTPlane
&& gIODTCompatibleKey
109 && gIODTTypeKey
&& gIODTModelKey
110 && gIODTSizeCellKey
&& gIODTAddressCellKey
&& gIODTRangeKey
111 && gIODTPersistKey
);
113 gIODTDefaultInterruptController
114 = OSSymbol::withCStringNoCopy("IOPrimaryInterruptController");
115 gIODTNWInterruptMappingKey
116 = OSSymbol::withCStringNoCopy("IONWInterrupts");
118 gIODTAAPLInterruptsKey
119 = OSSymbol::withCStringNoCopy("AAPL,interrupts");
121 = OSSymbol::withCStringNoCopy("AAPL,phandle");
123 gIODTInterruptParentKey
124 = OSSymbol::withCStringNoCopy("interrupt-parent");
126 gIODTPHandles
= OSArray::withCapacity( 1 );
127 gIODTPHandleMap
= OSArray::withCapacity( 1 );
129 gIODTInterruptCellKey
130 = OSSymbol::withCStringNoCopy("#interrupt-cells");
132 assert( gIODTDefaultInterruptController
&& gIODTNWInterruptMappingKey
133 && gIODTAAPLInterruptsKey
134 && gIODTPHandleKey
&& gIODTInterruptParentKey
135 && gIODTPHandles
&& gIODTPHandleMap
136 && gIODTInterruptCellKey
139 freeDT
= (kSuccess
== DTLookupEntry( 0, "/chosen/memory-map", &mapEntry
))
140 && (kSuccess
== DTGetProperty( mapEntry
,
141 "DeviceTree", (void **) &dtMap
, &propSize
))
142 && ((2 * sizeof( vm_offset_t
)) == propSize
);
144 parent
= MakeReferenceTable( (DTEntry
)dtTop
, freeDT
);
146 stack
= OSArray::withObjects( (const OSObject
**) &parent
, 1, 10 );
147 DTCreateEntryIterator( (DTEntry
)dtTop
, &iter
);
150 parent
= (IORegistryEntry
*)stack
->getObject( stack
->getCount() - 1);
152 stack
->removeObject( stack
->getCount() - 1);
154 while( kSuccess
== DTIterateEntries( iter
, &dtChild
) ) {
156 child
= MakeReferenceTable( dtChild
, freeDT
);
157 child
->attachToParent( parent
, gIODTPlane
);
161 if( kSuccess
== DTEnterEntry( iter
, dtChild
)) {
162 stack
->setObject( parent
);
165 // only registry holds retain
169 } while( stack
->getCount()
170 && (kSuccess
== DTExitEntry( iter
, &dtChild
)));
173 DTDisposeEntryIterator( iter
);
175 // parent is now root of the created tree
177 // make root name first compatible entry (purely cosmetic)
178 if( (prop
= (OSData
*) parent
->getProperty( gIODTCompatibleKey
))) {
179 parent
->setName( parent
->getName(), gIODTPlane
);
180 parent
->setName( (const char *) prop
->getBytesNoCopy() );
183 // attach tree to meta root
184 parent
->attachToParent( IORegistryEntry::getRegistryRoot(), gIODTPlane
);
188 // free original device tree
190 IODTFreeLoaderInfo( "DeviceTree",
191 (void *)dtMap
[0], round_page_32(dtMap
[1]) );
196 gIODTSharedInterrupts
= OSDictionary::withCapacity(4);
197 allInts
= OSDictionary::withCapacity(4);
199 regIter
= IORegistryIterator::iterateOver( gIODTPlane
,
200 kIORegistryIterateRecursively
);
201 assert( regIter
&& allInts
&& gIODTSharedInterrupts
);
202 if( regIter
&& allInts
&& gIODTSharedInterrupts
) {
203 while( (child
= regIter
->getNextObject())) {
204 IODTMapInterruptsSharing( child
, allInts
);
205 if( !intMap
&& child
->getProperty( gIODTInterruptParentKey
))
208 // Look for a "driver,AAPL,MacOSX,PowerPC" property.
209 if( (obj
= child
->getProperty( "driver,AAPL,MacOSX,PowerPC"))) {
210 gIOCatalogue
->addExtensionsFromArchive((OSData
*)obj
);
211 child
->removeProperty( "driver,AAPL,MacOSX,PowerPC");
214 // some gross pruning
215 child
->removeProperty( "lanLib,AAPL,MacOS,PowerPC");
217 if( (obj
= child
->getProperty( "driver,AAPL,MacOS,PowerPC"))) {
219 if( (0 == (prop
= (OSData
*)child
->getProperty( gIODTTypeKey
)))
220 || (strcmp( "display", (char *) prop
->getBytesNoCopy())) ) {
221 child
->removeProperty( "driver,AAPL,MacOS,PowerPC");
229 parent
->setProperty("allInts", allInts
);
230 parent
->setProperty("sharedInts", gIODTSharedInterrupts
);
232 regIter
= IORegistryIterator::iterateOver( gIODTPlane
,
233 kIORegistryIterateRecursively
);
235 while( (child
= regIter
->getNextObject())) {
237 array
= OSDynamicCast(OSArray
, child
->getProperty( gIOInterruptSpecifiersKey
));
238 for( UInt32 i
= 0; array
&& (i
< array
->getCount()); i
++)
240 IOOptionBits options
;
241 IOReturn ret
= IODTGetInterruptOptions( child
, i
, &options
);
242 if( (ret
!= kIOReturnSuccess
) || options
)
243 IOLog("%s[%ld] %ld (%x)\n", child
->getName(), i
, options
, ret
);
253 // set a key in the root to indicate we found NW interrupt mapping
254 parent
->setProperty( gIODTNWInterruptMappingKey
,
255 (OSObject
*) gIODTNWInterruptMappingKey
);
260 int IODTGetLoaderInfo( char *key
, void **infoAddr
, int *infoSize
)
262 IORegistryEntry
*chosen
;
264 unsigned int *propPtr
;
265 unsigned int propSize
;
267 chosen
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
);
268 if ( chosen
== 0 ) return -1;
270 propObj
= OSDynamicCast( OSData
, chosen
->getProperty(key
) );
271 if ( propObj
== 0 ) return -1;
273 propSize
= propObj
->getLength();
274 if ( propSize
!= (2 * sizeof(UInt32
)) ) return -1;
276 propPtr
= (unsigned int *)propObj
->getBytesNoCopy();
277 if ( propPtr
== 0 ) return -1;
279 *infoAddr
= (void *)propPtr
[0] ;
280 *infoSize
= (int) propPtr
[1];
285 void IODTFreeLoaderInfo( char *key
, void *infoAddr
, int infoSize
)
287 vm_offset_t range
[2];
288 IORegistryEntry
*chosen
;
290 range
[0] = (vm_offset_t
)infoAddr
;
291 range
[1] = (vm_offset_t
)infoSize
;
292 FreePhysicalMemory( range
);
295 chosen
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
);
297 chosen
->removeProperty(key
);
302 static void FreePhysicalMemory( vm_offset_t
* range
)
306 virt
= ml_static_ptovirt( range
[0] );
308 ml_static_mfree( virt
, range
[1] );
312 static IORegistryEntry
*
313 MakeReferenceTable( DTEntry dtEntry
, bool copy
)
315 IORegistryEntry
*regEntry
;
316 OSDictionary
*propTable
;
317 const OSSymbol
*nameKey
;
320 DTPropertyIterator dtIter
;
325 bool noLocation
= true;
327 regEntry
= new IOService
;
329 if( regEntry
&& (false == regEntry
->init())) {
335 (kSuccess
== DTCreatePropertyIterator( dtEntry
, &dtIter
))) {
337 propTable
= regEntry
->getPropertyTable();
339 while( kSuccess
== DTIterateProperties( dtIter
, &name
)) {
341 if( kSuccess
!= DTGetProperty( dtEntry
, name
, &prop
, &propSize
))
345 nameKey
= OSSymbol::withCString(name
);
346 data
= OSData::withBytes(prop
, propSize
);
348 nameKey
= OSSymbol::withCStringNoCopy(name
);
349 data
= OSData::withBytesNoCopy(prop
, propSize
);
351 assert( nameKey
&& data
);
353 propTable
->setObject( nameKey
, data
);
357 if( nameKey
== gIODTNameKey
) {
359 sym
= OSSymbol::withCString( (const char *) prop
);
361 sym
= OSSymbol::withCStringNoCopy( (const char *) prop
);
362 regEntry
->setName( sym
);
365 } else if( nameKey
== gIODTUnitKey
) {
366 // all OF strings are null terminated... except this one
367 if( propSize
>= (int) sizeof( location
))
368 propSize
= sizeof( location
) - 1;
369 strncpy( location
, (const char *) prop
, propSize
);
370 location
[ propSize
] = 0;
371 regEntry
->setLocation( location
);
372 propTable
->removeObject( gIODTUnitKey
);
375 } else if( noLocation
&& (0 == strcmp( name
, "reg"))) {
376 // default location - override later
377 sprintf( location
, "%lX", *((UInt32
*) prop
) );
378 regEntry
->setLocation( location
);
381 DTDisposePropertyIterator( dtIter
);
387 static void AddPHandle( IORegistryEntry
* regEntry
)
391 if( regEntry
->getProperty( gIODTInterruptCellKey
)
392 && (data
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTPHandleKey
)))) {
393 // a possible interrupt-parent
394 gIODTPHandles
->setObject( data
);
395 gIODTPHandleMap
->setObject( regEntry
);
399 static IORegistryEntry
* FindPHandle( UInt32 phandle
)
402 IORegistryEntry
*regEntry
= 0;
405 for( i
= 0; (data
= (OSData
*)gIODTPHandles
->getObject( i
)); i
++ ) {
406 if( phandle
== *((UInt32
*)data
->getBytesNoCopy())) {
407 regEntry
= (IORegistryEntry
*)
408 gIODTPHandleMap
->getObject( i
);
416 static bool GetUInt32( IORegistryEntry
* regEntry
, const OSSymbol
* name
,
421 if( (data
= OSDynamicCast( OSData
, regEntry
->getProperty( name
)))
422 && (4 == data
->getLength())) {
423 *value
= *((UInt32
*) data
->getBytesNoCopy());
429 IORegistryEntry
* IODTFindInterruptParent( IORegistryEntry
* regEntry
)
431 IORegistryEntry
* parent
;
434 if( GetUInt32( regEntry
, gIODTInterruptParentKey
, &phandle
))
435 parent
= FindPHandle( phandle
);
437 else if( 0 == regEntry
->getProperty( "interrupt-controller"))
438 parent
= regEntry
->getParentEntry( gIODTPlane
);
445 const OSSymbol
* IODTInterruptControllerName( IORegistryEntry
* regEntry
)
452 ok
= GetUInt32( regEntry
, gIODTPHandleKey
, &phandle
);
456 sprintf( buf
, "IOInterruptController%08lX", phandle
);
457 sym
= OSSymbol::withCString( buf
);
464 #define unexpected(a) { kprintf("unexpected %s:%d\n", __FILE__, __LINE__); a; }
466 static void IODTGetICellCounts( IORegistryEntry
* regEntry
,
467 UInt32
* iCellCount
, UInt32
* aCellCount
)
469 if( !GetUInt32( regEntry
, gIODTInterruptCellKey
, iCellCount
))
470 unexpected( *iCellCount
= 1 );
471 if( !GetUInt32( regEntry
, gIODTAddressCellKey
, aCellCount
))
475 UInt32
IODTMapOneInterrupt( IORegistryEntry
* regEntry
, UInt32
* intSpec
,
476 OSData
** spec
, const OSSymbol
** controller
)
478 IORegistryEntry
*parent
= 0;
484 UInt32 acells
, icells
, pacells
, picells
, cell
;
485 UInt32 i
, original_icells
;
486 bool cmp
, ok
= false;
488 parent
= IODTFindInterruptParent( regEntry
);
489 IODTGetICellCounts( parent
, &icells
, &acells
);
492 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "reg" ));
493 if( data
&& (data
->getLength() >= (acells
* sizeof( UInt32
))))
494 addrCmp
= (UInt32
*) data
->getBytesNoCopy();
496 original_icells
= icells
;
501 kprintf ("IODTMapOneInterrupt: current regEntry name %s\n", regEntry
->getName());
502 kprintf ("acells - icells: ");
503 for (i
= 0; i
< acells
; i
++) kprintf ("0x%08X ", addrCmp
[i
]);
505 for (i
= 0; i
< icells
; i
++) kprintf ("0x%08X ", intSpec
[i
]);
509 if( parent
&& (data
= OSDynamicCast( OSData
,
510 regEntry
->getProperty( "interrupt-controller")))) {
511 // found a controller - don't want to follow cascaded controllers
513 *spec
= OSData::withBytesNoCopy( (void *) intSpec
,
514 icells
* sizeof( UInt32
));
515 *controller
= IODTInterruptControllerName( regEntry
);
516 ok
= (*spec
&& *controller
);
517 } else if( parent
&& (data
= OSDynamicCast( OSData
,
518 regEntry
->getProperty( "interrupt-map")))) {
520 map
= (UInt32
*) data
->getBytesNoCopy();
521 endMap
= map
+ (data
->getLength() / sizeof(UInt32
));
522 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "interrupt-map-mask" ));
523 if( data
&& (data
->getLength() >= ((acells
+ icells
) * sizeof( UInt32
))))
524 maskCmp
= (UInt32
*) data
->getBytesNoCopy();
530 kprintf (" maskCmp: ");
531 for (i
= 0; i
< acells
+ icells
; i
++) {
534 kprintf ("0x%08X ", maskCmp
[i
]);
537 kprintf (" masked: ");
538 for (i
= 0; i
< acells
+ icells
; i
++) {
541 kprintf ("0x%08X ", ((i
< acells
) ? addrCmp
[i
] : intSpec
[i
-acells
]) & maskCmp
[i
]);
545 kprintf ("no maskCmp\n");
550 for (i
= 0; i
< acells
+ icells
; i
++) {
553 kprintf ("0x%08X ", map
[i
]);
557 for( i
= 0, cmp
= true; cmp
&& (i
< (acells
+ icells
)); i
++) {
558 cell
= (i
< acells
) ? addrCmp
[i
] : intSpec
[ i
- acells
];
561 cmp
= (cell
== map
[i
]);
564 map
+= acells
+ icells
;
565 if( 0 == (parent
= FindPHandle( *(map
++) )))
568 IODTGetICellCounts( parent
, &picells
, &pacells
);
571 intSpec
= map
+ pacells
;
574 map
+= pacells
+ picells
;
576 } while( !cmp
&& (map
< endMap
) );
582 IODTGetICellCounts( parent
, &icells
, &acells
);
588 return( ok
? original_icells
: 0 );
591 IOReturn
IODTGetInterruptOptions( IORegistryEntry
* regEntry
, int source
, IOOptionBits
* options
)
593 OSArray
* controllers
;
594 OSArray
* specifiers
;
601 controllers
= OSDynamicCast(OSArray
, regEntry
->getProperty(gIOInterruptControllersKey
));
602 specifiers
= OSDynamicCast(OSArray
, regEntry
->getProperty(gIOInterruptSpecifiersKey
));
604 if( !controllers
|| !specifiers
)
605 return (kIOReturnNoInterrupt
);
607 shared
= (OSArray
*) gIODTSharedInterrupts
->getObject(
608 (const OSSymbol
*) controllers
->getObject(source
) );
610 return (kIOReturnSuccess
);
612 spec
= specifiers
->getObject(source
);
614 return (kIOReturnNoInterrupt
);
616 for (unsigned int i
= 0;
617 (oneSpec
= shared
->getObject(i
))
618 && (!oneSpec
->isEqualTo(spec
));
622 *options
= kIODTInterruptShared
;
624 return (kIOReturnSuccess
);
627 static bool IODTMapInterruptsSharing( IORegistryEntry
* regEntry
, OSDictionary
* allInts
)
629 IORegistryEntry
* parent
;
637 OSArray
* controllerInts
;
638 const OSSymbol
* controller
;
639 OSArray
* controllers
;
643 nw
= (0 == (local
= OSDynamicCast( OSData
,
644 regEntry
->getProperty( gIODTAAPLInterruptsKey
))));
645 if( nw
&& (0 == (local
= OSDynamicCast( OSData
,
646 regEntry
->getProperty( "interrupts")))))
647 return( true ); // nothing to see here
649 if( nw
&& (parent
= regEntry
->getParentEntry( gIODTPlane
))) {
650 // check for bridges on old world
651 if( (local2
= OSDynamicCast( OSData
,
652 parent
->getProperty( gIODTAAPLInterruptsKey
)))) {
658 localBits
= (UInt32
*) local
->getBytesNoCopy();
659 localEnd
= localBits
+ (local
->getLength() / sizeof( UInt32
));
660 mapped
= OSArray::withCapacity( 1 );
661 controllers
= OSArray::withCapacity( 1 );
663 ok
= (mapped
&& controllers
);
667 skip
= IODTMapOneInterrupt( regEntry
, localBits
, &map
, &controller
);
669 IOLog("%s: error mapping interrupt[%d]\n",
670 regEntry
->getName(), mapped
->getCount());
674 map
= OSData::withData( local
, mapped
->getCount() * sizeof( UInt32
),
676 controller
= gIODTDefaultInterruptController
;
677 controller
->retain();
681 mapped
->setObject( map
);
682 controllers
->setObject( controller
);
686 controllerInts
= (OSArray
*) allInts
->getObject( controller
);
689 for (unsigned int i
= 0; (oneMap
= controllerInts
->getObject(i
)); i
++)
691 if (map
->isEqualTo(oneMap
))
693 controllerInts
= (OSArray
*) gIODTSharedInterrupts
->getObject( controller
);
695 controllerInts
->setObject(map
);
698 controllerInts
= OSArray::withObjects( (const OSObject
**) &map
, 1, 4 );
701 gIODTSharedInterrupts
->setObject( controller
, controllerInts
);
702 controllerInts
->release();
709 controllerInts
->setObject(map
);
713 controllerInts
= OSArray::withObjects( (const OSObject
**) &map
, 1, 16 );
716 allInts
->setObject( controller
, controllerInts
);
717 controllerInts
->release();
723 controller
->release();
725 } while( localBits
< localEnd
);
727 ok
&= (localBits
== localEnd
);
731 ok
= regEntry
->setProperty( gIOInterruptControllersKey
, controllers
);
732 ok
&= regEntry
->setProperty( gIOInterruptSpecifiersKey
, mapped
);
736 controllers
->release();
743 bool IODTMapInterrupts( IORegistryEntry
* regEntry
)
745 return( IODTMapInterruptsSharing( regEntry
, 0 ));
752 CompareKey( OSString
* key
,
753 const IORegistryEntry
* table
, const OSSymbol
* propName
)
761 const char *lastName
;
764 const char *result
= 0;
766 if( 0 == (prop
= table
->getProperty( propName
)))
769 if( (data
= OSDynamicCast( OSData
, prop
))) {
770 names
= (const char *) data
->getBytesNoCopy();
771 lastName
= names
+ data
->getLength();
772 } else if( (string
= OSDynamicCast( OSString
, prop
))) {
773 names
= string
->getCStringNoCopy();
774 lastName
= names
+ string
->getLength() + 1;
778 ckey
= key
->getCStringNoCopy();
779 keyLen
= key
->getLength();
780 wild
= ('*' == key
->getChar( keyLen
- 1 ));
783 // for each name in the property
785 matched
= (0 == strncmp( ckey
, names
, keyLen
- 1 ));
787 matched
= (keyLen
== strlen( names
))
788 && (0 == strncmp( ckey
, names
, keyLen
));
793 names
= names
+ strlen( names
) + 1;
795 } while( (names
< lastName
) && (false == matched
));
801 bool IODTCompareNubName( const IORegistryEntry
* regEntry
,
802 OSString
* name
, OSString
** matchingName
)
807 matched
= (0 != (result
= CompareKey( name
, regEntry
, gIODTNameKey
)))
808 || (0 != (result
= CompareKey( name
, regEntry
, gIODTCompatibleKey
)))
809 || (0 != (result
= CompareKey( name
, regEntry
, gIODTTypeKey
)))
810 || (0 != (result
= CompareKey( name
, regEntry
, gIODTModelKey
)));
812 if( result
&& matchingName
)
813 *matchingName
= OSString::withCString( result
);
815 return( result
!= 0 );
818 bool IODTMatchNubWithKeys( IORegistryEntry
* regEntry
,
824 obj
= OSUnserialize( keys
, 0 );
827 result
= regEntry
->compareNames( obj
);
831 else IOLog("Couldn't unserialize %s\n", keys
);
837 OSCollectionIterator
* IODTFindMatchingEntries( IORegistryEntry
* from
,
838 IOOptionBits options
, const char * keys
)
841 IORegistryEntry
*next
;
842 IORegistryIterator
*iter
;
843 OSCollectionIterator
*cIter
;
845 bool minus
= options
& kIODTExclusive
;
848 iter
= IORegistryIterator::iterateOver( from
, gIODTPlane
,
849 (options
& kIODTRecursive
) ? kIORegistryIterateRecursively
: 0 );
856 result
= OSSet::withCapacity( 3 );
861 while( (next
= iter
->getNextObject())) {
863 // Look for existence of a debug property to skip
864 if( next
->getProperty("AAPL,ignore"))
868 cmp
= IODTMatchNubWithKeys( next
, keys
);
869 if( (minus
&& (false == cmp
))
870 || ((false == minus
) && (false != cmp
)) )
871 result
->setObject( next
);
873 result
->setObject( next
);
875 } while( !iter
->isValid());
880 cIter
= OSCollectionIterator::withCollection( result
);
887 struct IODTPersistent
{
888 IODTCompareAddressCellFunc compareFunc
;
889 IODTNVLocationFunc locationFunc
;
892 void IODTSetResolving( IORegistryEntry
* regEntry
,
893 IODTCompareAddressCellFunc compareFunc
,
894 IODTNVLocationFunc locationFunc
)
896 IODTPersistent persist
;
899 persist
.compareFunc
= compareFunc
;
900 persist
.locationFunc
= locationFunc
;
901 prop
= OSData::withBytes( &persist
, sizeof( persist
));
905 regEntry
->setProperty( gIODTPersistKey
, prop
);
910 static SInt32
DefaultCompare( UInt32 cellCount
, UInt32 left
[], UInt32 right
[] )
913 return( left
[ cellCount
] - right
[ cellCount
] );
916 void IODTGetCellCounts( IORegistryEntry
* regEntry
,
917 UInt32
* sizeCount
, UInt32
* addressCount
)
919 if( !GetUInt32( regEntry
, gIODTSizeCellKey
, sizeCount
))
921 if( !GetUInt32( regEntry
, gIODTAddressCellKey
, addressCount
))
926 // Given addr & len cells from our child, find it in our ranges property, then
927 // look in our parent to resolve the base of the range for us.
929 // Range[]: child-addr our-addr child-len
930 // #cells: child ours child
932 bool IODTResolveAddressCell( IORegistryEntry
* regEntry
,
934 IOPhysicalAddress
* phys
, IOPhysicalLength
* len
)
936 IORegistryEntry
*parent
;
938 // cells in addresses at regEntry
939 UInt32 sizeCells
, addressCells
;
940 // cells in addresses below regEntry
941 UInt32 childSizeCells
, childAddressCells
;
943 UInt32 cell
[ 5 ], offset
= 0, length
;
950 SInt32 diff
, endDiff
;
952 IODTPersistent
*persist
;
953 IODTCompareAddressCellFunc compare
;
955 IODTGetCellCounts( regEntry
, &childSizeCells
, &childAddressCells
);
956 childCells
= childAddressCells
+ childSizeCells
;
958 bcopy( cellsIn
, cell
, 4 * childCells
);
959 if( childSizeCells
> 1)
960 *len
= IOPhysical32( cellsIn
[ childAddressCells
],
961 cellsIn
[ childAddressCells
+ 1 ] );
963 *len
= IOPhysical32( 0, cellsIn
[ childAddressCells
] );
967 prop
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTRangeKey
));
969 /* end of the road */
970 *phys
= IOPhysical32( 0, cell
[ childAddressCells
- 1 ] + offset
);
974 parent
= regEntry
->getParentEntry( gIODTPlane
);
975 IODTGetCellCounts( parent
, &sizeCells
, &addressCells
);
977 if( (length
= prop
->getLength())) {
979 startRange
= (UInt32
*) prop
->getBytesNoCopy();
981 endRanges
= range
+ (length
/ 4);
983 prop
= (OSData
*) regEntry
->getProperty( gIODTPersistKey
);
985 persist
= (IODTPersistent
*) prop
->getBytesNoCopy();
986 compare
= persist
->compareFunc
;
988 compare
= DefaultCompare
;
992 range
+= (childCells
+ addressCells
) ) {
994 // is cell start >= range start?
995 diff
= (*compare
)( childAddressCells
, cell
, range
);
999 ok
= (0 == cell
[childCells
- 1]);
1002 // search for cell end
1003 bcopy(cell
, endCell
, childAddressCells
* sizeof(UInt32
));
1004 endCell
[childAddressCells
- 1] += cell
[childCells
- 1] - 1;
1005 lookRange
= startRange
;
1007 lookRange
< endRanges
;
1008 lookRange
+= (childCells
+ addressCells
) )
1010 // is cell >= range start?
1011 endDiff
= (*compare
)( childAddressCells
, endCell
, lookRange
);
1014 if ((endDiff
- cell
[childCells
- 1] + 1 + lookRange
[childAddressCells
+ addressCells
- 1])
1015 == (diff
+ range
[childAddressCells
+ addressCells
- 1]))
1028 // Get the physical start of the range from our parent
1029 bcopy( range
+ childAddressCells
, cell
, 4 * addressCells
);
1030 bzero( cell
+ addressCells
, 4 * sizeCells
);
1032 } /* else zero length range => pass thru to parent */
1035 childSizeCells
= sizeCells
;
1036 childAddressCells
= addressCells
;
1037 childCells
= childAddressCells
+ childSizeCells
;
1039 while( ok
&& regEntry
);
1045 OSArray
* IODTResolveAddressing( IORegistryEntry
* regEntry
,
1046 const char * addressPropertyName
,
1047 IODeviceMemory
* parent
)
1049 IORegistryEntry
*parentEntry
;
1050 OSData
*addressProperty
;
1051 UInt32 sizeCells
, addressCells
, cells
;
1054 IOPhysicalAddress phys
;
1055 IOPhysicalLength len
;
1057 IODeviceMemory
*range
;
1059 parentEntry
= regEntry
->getParentEntry( gIODTPlane
);
1060 addressProperty
= (OSData
*) regEntry
->getProperty( addressPropertyName
);
1061 if( (0 == addressProperty
) || (0 == parentEntry
))
1064 IODTGetCellCounts( parentEntry
, &sizeCells
, &addressCells
);
1068 cells
= sizeCells
+ addressCells
;
1069 reg
= (UInt32
*) addressProperty
->getBytesNoCopy();
1070 num
= addressProperty
->getLength() / (4 * cells
);
1072 array
= OSArray::withCapacity( 1 );
1076 for( i
= 0; i
< num
; i
++) {
1077 if( IODTResolveAddressCell( parentEntry
, reg
, &phys
, &len
)) {
1080 range
= IODeviceMemory::withSubRange( parent
,
1081 phys
- parent
->getPhysicalAddress(), len
);
1083 range
= IODeviceMemory::withRange( phys
, len
);
1085 array
->setObject( range
);
1090 regEntry
->setProperty( gIODeviceMemoryKey
, array
);
1091 array
->release(); /* ??? */
1096 static void IODTGetNVLocation(
1097 IORegistryEntry
* parent
,
1098 IORegistryEntry
* regEntry
,
1099 UInt8
* busNum
, UInt8
* deviceNum
, UInt8
* functionNum
)
1103 IODTPersistent
*persist
;
1106 prop
= (OSData
*) parent
->getProperty( gIODTPersistKey
);
1108 persist
= (IODTPersistent
*) prop
->getBytesNoCopy();
1109 (*persist
->locationFunc
)( regEntry
, busNum
, deviceNum
, functionNum
);
1111 prop
= (OSData
*) regEntry
->getProperty( "reg" );
1114 cell
= (UInt32
*) prop
->getBytesNoCopy();
1116 *deviceNum
= 0x1f & (cell
[ 0 ] >> 24);
1126 * Try to make the same messed up descriptor as Mac OS
1129 IOReturn
IODTMakeNVDescriptor( IORegistryEntry
* regEntry
,
1130 IONVRAMDescriptor
* hdr
)
1132 IORegistryEntry
*parent
;
1134 UInt32 bridgeDevices
;
1142 for(level
= 0, bridgeDevices
= 0;
1143 (parent
= regEntry
->getParentEntry( gIODTPlane
)) && (level
< 7); level
++ ) {
1145 IODTGetNVLocation( parent
, regEntry
,
1146 &busNum
, &deviceNum
, &functionNum
);
1148 bridgeDevices
|= ((deviceNum
& 0x1f) << ((level
- 1) * 5));
1150 hdr
->busNum
= busNum
;
1151 hdr
->deviceNum
= deviceNum
;
1152 hdr
->functionNum
= functionNum
;
1156 hdr
->bridgeCount
= level
- 2;
1157 hdr
->bridgeDevices
= bridgeDevices
;
1159 return( kIOReturnSuccess
);
1162 OSData
* IODTFindSlotName( IORegistryEntry
* regEntry
, UInt32 deviceNumber
)
1164 IORegistryEntry
*parent
;
1173 data
= (OSData
*) regEntry
->getProperty("AAPL,slot-name");
1176 parent
= regEntry
->getParentEntry( gIODTPlane
);
1179 data
= OSDynamicCast( OSData
, parent
->getProperty("slot-names"));
1182 if( data
->getLength() <= 4)
1185 bits
= (UInt32
*) data
->getBytesNoCopy();
1187 if( (0 == (mask
& (1 << deviceNumber
))))
1190 names
= (char *)(bits
+ 1);
1191 lastName
= names
+ (data
->getLength() - 4);
1193 for( i
= 0; (i
<= deviceNumber
) && (names
< lastName
); i
++ ) {
1195 if( mask
& (1 << i
)) {
1196 if( i
== deviceNumber
) {
1197 data
= OSData::withBytesNoCopy( names
, 1 + strlen( names
));
1199 regEntry
->setProperty("AAPL,slot-name", data
);
1204 names
+= 1 + strlen( names
);
1211 extern "C" IOReturn
IONDRVLibrariesInitialize( IOService
* provider
)
1213 return( kIOReturnUnsupported
);