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 #include <IOKit/IODeviceTreeSupport.h>
24 #include <libkern/c++/OSContainers.h>
25 #include <IOKit/IODeviceMemory.h>
26 #include <IOKit/IOService.h>
27 #include <IOKit/IOCatalogue.h>
29 #include <IOKit/IOLib.h>
30 #include <IOKit/IOKitKeys.h>
32 #include <pexpert/device_tree.h>
35 #include <machine/machine_routines.h>
36 void DTInit( void * data
);
38 int IODTGetLoaderInfo( char *key
, void **infoAddr
, int *infosize
);
39 void IODTFreeLoaderInfo( char *key
, void *infoAddr
, int infoSize
);
42 #include <IOKit/assert.h>
44 #define IODTSUPPORTDEBUG 0
46 const IORegistryPlane
* gIODTPlane
;
48 static OSArray
* gIODTPHandles
;
49 static OSArray
* gIODTPHandleMap
;
51 const OSSymbol
* gIODTNameKey
;
52 const OSSymbol
* gIODTUnitKey
;
53 const OSSymbol
* gIODTCompatibleKey
;
54 const OSSymbol
* gIODTTypeKey
;
55 const OSSymbol
* gIODTModelKey
;
57 const OSSymbol
* gIODTSizeCellKey
;
58 const OSSymbol
* gIODTAddressCellKey
;
59 const OSSymbol
* gIODTRangeKey
;
61 const OSSymbol
* gIODTPersistKey
;
63 const OSSymbol
* gIODTDefaultInterruptController
;
64 const OSSymbol
* gIODTAAPLInterruptsKey
;
65 const OSSymbol
* gIODTPHandleKey
;
66 const OSSymbol
* gIODTInterruptCellKey
;
67 const OSSymbol
* gIODTInterruptParentKey
;
68 const OSSymbol
* gIODTNWInterruptMappingKey
;
70 OSDictionary
* gIODTSharedInterrupts
;
72 static IORegistryEntry
* MakeReferenceTable( DTEntry dtEntry
, bool copy
);
73 static void AddPHandle( IORegistryEntry
* regEntry
);
74 static void FreePhysicalMemory( vm_offset_t
* range
);
75 static bool IODTMapInterruptsSharing( IORegistryEntry
* regEntry
, OSDictionary
* allInts
);
78 IODeviceTreeAlloc( void * dtTop
)
80 IORegistryEntry
* parent
;
81 IORegistryEntry
* child
;
82 IORegistryIterator
* regIter
;
89 OSDictionary
* allInts
;
95 gIODTPlane
= IORegistryEntry::makePlane( kIODeviceTreePlane
);
97 gIODTNameKey
= OSSymbol::withCStringNoCopy( "name" );
98 gIODTUnitKey
= OSSymbol::withCStringNoCopy( "AAPL,unit-string" );
99 gIODTCompatibleKey
= OSSymbol::withCStringNoCopy( "compatible" );
100 gIODTTypeKey
= OSSymbol::withCStringNoCopy( "device_type" );
101 gIODTModelKey
= OSSymbol::withCStringNoCopy( "model" );
102 gIODTSizeCellKey
= OSSymbol::withCStringNoCopy( "#size-cells" );
103 gIODTAddressCellKey
= OSSymbol::withCStringNoCopy( "#address-cells" );
104 gIODTRangeKey
= OSSymbol::withCStringNoCopy( "ranges" );
105 gIODTPersistKey
= OSSymbol::withCStringNoCopy( "IODTPersist" );
107 assert( gIODTPlane
&& gIODTCompatibleKey
108 && gIODTTypeKey
&& gIODTModelKey
109 && gIODTSizeCellKey
&& gIODTAddressCellKey
&& gIODTRangeKey
110 && gIODTPersistKey
);
112 gIODTDefaultInterruptController
113 = OSSymbol::withCStringNoCopy("IOPrimaryInterruptController");
114 gIODTNWInterruptMappingKey
115 = OSSymbol::withCStringNoCopy("IONWInterrupts");
117 gIODTAAPLInterruptsKey
118 = OSSymbol::withCStringNoCopy("AAPL,interrupts");
120 = OSSymbol::withCStringNoCopy("AAPL,phandle");
122 gIODTInterruptParentKey
123 = OSSymbol::withCStringNoCopy("interrupt-parent");
125 gIODTPHandles
= OSArray::withCapacity( 1 );
126 gIODTPHandleMap
= OSArray::withCapacity( 1 );
128 gIODTInterruptCellKey
129 = OSSymbol::withCStringNoCopy("#interrupt-cells");
131 assert( gIODTDefaultInterruptController
&& gIODTNWInterruptMappingKey
132 && gIODTAAPLInterruptsKey
133 && gIODTPHandleKey
&& gIODTInterruptParentKey
134 && gIODTPHandles
&& gIODTPHandleMap
135 && gIODTInterruptCellKey
138 freeDT
= (kSuccess
== DTLookupEntry( 0, "/chosen/memory-map", &mapEntry
))
139 && (kSuccess
== DTGetProperty( mapEntry
,
140 "DeviceTree", (void **) &dtMap
, &propSize
))
141 && ((2 * sizeof( vm_offset_t
)) == propSize
);
143 parent
= MakeReferenceTable( (DTEntry
)dtTop
, freeDT
);
145 stack
= OSArray::withObjects( (const OSObject
**) &parent
, 1, 10 );
146 DTCreateEntryIterator( (DTEntry
)dtTop
, &iter
);
149 parent
= (IORegistryEntry
*)stack
->getObject( stack
->getCount() - 1);
151 stack
->removeObject( stack
->getCount() - 1);
153 while( kSuccess
== DTIterateEntries( iter
, &dtChild
) ) {
155 child
= MakeReferenceTable( dtChild
, freeDT
);
156 child
->attachToParent( parent
, gIODTPlane
);
160 if( kSuccess
== DTEnterEntry( iter
, dtChild
)) {
161 stack
->setObject( parent
);
164 // only registry holds retain
168 } while( stack
->getCount()
169 && (kSuccess
== DTExitEntry( iter
, &dtChild
)));
172 DTDisposeEntryIterator( iter
);
174 // parent is now root of the created tree
176 // make root name first compatible entry (purely cosmetic)
177 if( (prop
= (OSData
*) parent
->getProperty( gIODTCompatibleKey
))) {
178 parent
->setName( parent
->getName(), gIODTPlane
);
179 parent
->setName( (const char *) prop
->getBytesNoCopy() );
182 // attach tree to meta root
183 parent
->attachToParent( IORegistryEntry::getRegistryRoot(), gIODTPlane
);
187 // free original device tree
189 IODTFreeLoaderInfo( "DeviceTree",
190 (void *)dtMap
[0], round_page_32(dtMap
[1]) );
195 gIODTSharedInterrupts
= OSDictionary::withCapacity(4);
196 allInts
= OSDictionary::withCapacity(4);
198 regIter
= IORegistryIterator::iterateOver( gIODTPlane
,
199 kIORegistryIterateRecursively
);
200 assert( regIter
&& allInts
&& gIODTSharedInterrupts
);
201 if( regIter
&& allInts
&& gIODTSharedInterrupts
) {
202 while( (child
= regIter
->getNextObject())) {
203 IODTMapInterruptsSharing( child
, allInts
);
204 if( !intMap
&& child
->getProperty( gIODTInterruptParentKey
))
207 // Look for a "driver,AAPL,MacOSX,PowerPC" property.
208 if( (obj
= child
->getProperty( "driver,AAPL,MacOSX,PowerPC"))) {
209 gIOCatalogue
->addExtensionsFromArchive((OSData
*)obj
);
210 child
->removeProperty( "driver,AAPL,MacOSX,PowerPC");
213 // some gross pruning
214 child
->removeProperty( "lanLib,AAPL,MacOS,PowerPC");
216 if( (obj
= child
->getProperty( "driver,AAPL,MacOS,PowerPC"))) {
218 if( (0 == (prop
= (OSData
*)child
->getProperty( gIODTTypeKey
)))
219 || (strcmp( "display", (char *) prop
->getBytesNoCopy())) ) {
220 child
->removeProperty( "driver,AAPL,MacOS,PowerPC");
228 parent
->setProperty("allInts", allInts
);
229 parent
->setProperty("sharedInts", gIODTSharedInterrupts
);
231 regIter
= IORegistryIterator::iterateOver( gIODTPlane
,
232 kIORegistryIterateRecursively
);
234 while( (child
= regIter
->getNextObject())) {
236 array
= OSDynamicCast(OSArray
, child
->getProperty( gIOInterruptSpecifiersKey
));
237 for( UInt32 i
= 0; array
&& (i
< array
->getCount()); i
++)
239 IOOptionBits options
;
240 IOReturn ret
= IODTGetInterruptOptions( child
, i
, &options
);
241 if( (ret
!= kIOReturnSuccess
) || options
)
242 IOLog("%s[%ld] %ld (%x)\n", child
->getName(), i
, options
, ret
);
252 // set a key in the root to indicate we found NW interrupt mapping
253 parent
->setProperty( gIODTNWInterruptMappingKey
,
254 (OSObject
*) gIODTNWInterruptMappingKey
);
259 int IODTGetLoaderInfo( char *key
, void **infoAddr
, int *infoSize
)
261 IORegistryEntry
*chosen
;
263 unsigned int *propPtr
;
264 unsigned int propSize
;
266 chosen
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
);
267 if ( chosen
== 0 ) return -1;
269 propObj
= OSDynamicCast( OSData
, chosen
->getProperty(key
) );
270 if ( propObj
== 0 ) return -1;
272 propSize
= propObj
->getLength();
273 if ( propSize
!= (2 * sizeof(UInt32
)) ) return -1;
275 propPtr
= (unsigned int *)propObj
->getBytesNoCopy();
276 if ( propPtr
== 0 ) return -1;
278 *infoAddr
= (void *)propPtr
[0] ;
279 *infoSize
= (int) propPtr
[1];
284 void IODTFreeLoaderInfo( char *key
, void *infoAddr
, int infoSize
)
286 vm_offset_t range
[2];
287 IORegistryEntry
*chosen
;
289 range
[0] = (vm_offset_t
)infoAddr
;
290 range
[1] = (vm_offset_t
)infoSize
;
291 FreePhysicalMemory( range
);
294 chosen
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
);
296 chosen
->removeProperty(key
);
301 static void FreePhysicalMemory( vm_offset_t
* range
)
305 virt
= ml_static_ptovirt( range
[0] );
307 ml_static_mfree( virt
, range
[1] );
311 static IORegistryEntry
*
312 MakeReferenceTable( DTEntry dtEntry
, bool copy
)
314 IORegistryEntry
*regEntry
;
315 OSDictionary
*propTable
;
316 const OSSymbol
*nameKey
;
319 DTPropertyIterator dtIter
;
324 bool noLocation
= true;
326 regEntry
= new IOService
;
328 if( regEntry
&& (false == regEntry
->init())) {
334 (kSuccess
== DTCreatePropertyIterator( dtEntry
, &dtIter
))) {
336 propTable
= regEntry
->getPropertyTable();
338 while( kSuccess
== DTIterateProperties( dtIter
, &name
)) {
340 if( kSuccess
!= DTGetProperty( dtEntry
, name
, &prop
, &propSize
))
344 nameKey
= OSSymbol::withCString(name
);
345 data
= OSData::withBytes(prop
, propSize
);
347 nameKey
= OSSymbol::withCStringNoCopy(name
);
348 data
= OSData::withBytesNoCopy(prop
, propSize
);
350 assert( nameKey
&& data
);
352 propTable
->setObject( nameKey
, data
);
356 if( nameKey
== gIODTNameKey
) {
358 sym
= OSSymbol::withCString( (const char *) prop
);
360 sym
= OSSymbol::withCStringNoCopy( (const char *) prop
);
361 regEntry
->setName( sym
);
364 } else if( nameKey
== gIODTUnitKey
) {
365 // all OF strings are null terminated... except this one
366 if( propSize
>= (int) sizeof( location
))
367 propSize
= sizeof( location
) - 1;
368 strncpy( location
, (const char *) prop
, propSize
);
369 location
[ propSize
] = 0;
370 regEntry
->setLocation( location
);
371 propTable
->removeObject( gIODTUnitKey
);
374 } else if( noLocation
&& (0 == strcmp( name
, "reg"))) {
375 // default location - override later
376 sprintf( location
, "%lX", *((UInt32
*) prop
) );
377 regEntry
->setLocation( location
);
380 DTDisposePropertyIterator( dtIter
);
386 static void AddPHandle( IORegistryEntry
* regEntry
)
390 if( regEntry
->getProperty( gIODTInterruptCellKey
)
391 && (data
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTPHandleKey
)))) {
392 // a possible interrupt-parent
393 gIODTPHandles
->setObject( data
);
394 gIODTPHandleMap
->setObject( regEntry
);
398 static IORegistryEntry
* FindPHandle( UInt32 phandle
)
401 IORegistryEntry
*regEntry
= 0;
404 for( i
= 0; (data
= (OSData
*)gIODTPHandles
->getObject( i
)); i
++ ) {
405 if( phandle
== *((UInt32
*)data
->getBytesNoCopy())) {
406 regEntry
= (IORegistryEntry
*)
407 gIODTPHandleMap
->getObject( i
);
415 static bool GetUInt32( IORegistryEntry
* regEntry
, const OSSymbol
* name
,
420 if( (data
= OSDynamicCast( OSData
, regEntry
->getProperty( name
)))
421 && (4 == data
->getLength())) {
422 *value
= *((UInt32
*) data
->getBytesNoCopy());
428 IORegistryEntry
* IODTFindInterruptParent( IORegistryEntry
* regEntry
)
430 IORegistryEntry
* parent
;
433 if( GetUInt32( regEntry
, gIODTInterruptParentKey
, &phandle
))
434 parent
= FindPHandle( phandle
);
436 else if( 0 == regEntry
->getProperty( "interrupt-controller"))
437 parent
= regEntry
->getParentEntry( gIODTPlane
);
444 const OSSymbol
* IODTInterruptControllerName( IORegistryEntry
* regEntry
)
451 ok
= GetUInt32( regEntry
, gIODTPHandleKey
, &phandle
);
455 sprintf( buf
, "IOInterruptController%08lX", phandle
);
456 sym
= OSSymbol::withCString( buf
);
463 #define unexpected(a) { kprintf("unexpected %s:%d\n", __FILE__, __LINE__); a; }
465 static void IODTGetICellCounts( IORegistryEntry
* regEntry
,
466 UInt32
* iCellCount
, UInt32
* aCellCount
)
468 if( !GetUInt32( regEntry
, gIODTInterruptCellKey
, iCellCount
))
469 unexpected( *iCellCount
= 1 );
470 if( !GetUInt32( regEntry
, gIODTAddressCellKey
, aCellCount
))
474 UInt32
IODTMapOneInterrupt( IORegistryEntry
* regEntry
, UInt32
* intSpec
,
475 OSData
** spec
, const OSSymbol
** controller
)
477 IORegistryEntry
*parent
= 0;
483 UInt32 acells
, icells
, pacells
, picells
, cell
;
484 UInt32 i
, original_icells
;
485 bool cmp
, ok
= false;
487 parent
= IODTFindInterruptParent( regEntry
);
488 IODTGetICellCounts( parent
, &icells
, &acells
);
491 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "reg" ));
492 if( data
&& (data
->getLength() >= (acells
* sizeof( UInt32
))))
493 addrCmp
= (UInt32
*) data
->getBytesNoCopy();
495 original_icells
= icells
;
500 kprintf ("IODTMapOneInterrupt: current regEntry name %s\n", regEntry
->getName());
501 kprintf ("acells - icells: ");
502 for (i
= 0; i
< acells
; i
++) kprintf ("0x%08X ", addrCmp
[i
]);
504 for (i
= 0; i
< icells
; i
++) kprintf ("0x%08X ", intSpec
[i
]);
508 if( parent
&& (data
= OSDynamicCast( OSData
,
509 regEntry
->getProperty( "interrupt-controller")))) {
510 // found a controller - don't want to follow cascaded controllers
512 *spec
= OSData::withBytesNoCopy( (void *) intSpec
,
513 icells
* sizeof( UInt32
));
514 *controller
= IODTInterruptControllerName( regEntry
);
515 ok
= (*spec
&& *controller
);
516 } else if( parent
&& (data
= OSDynamicCast( OSData
,
517 regEntry
->getProperty( "interrupt-map")))) {
519 map
= (UInt32
*) data
->getBytesNoCopy();
520 endMap
= map
+ (data
->getLength() / sizeof(UInt32
));
521 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "interrupt-map-mask" ));
522 if( data
&& (data
->getLength() >= ((acells
+ icells
) * sizeof( UInt32
))))
523 maskCmp
= (UInt32
*) data
->getBytesNoCopy();
529 kprintf (" maskCmp: ");
530 for (i
= 0; i
< acells
+ icells
; i
++) {
533 kprintf ("0x%08X ", maskCmp
[i
]);
536 kprintf (" masked: ");
537 for (i
= 0; i
< acells
+ icells
; i
++) {
540 kprintf ("0x%08X ", ((i
< acells
) ? addrCmp
[i
] : intSpec
[i
-acells
]) & maskCmp
[i
]);
544 kprintf ("no maskCmp\n");
549 for (i
= 0; i
< acells
+ icells
; i
++) {
552 kprintf ("0x%08X ", map
[i
]);
556 for( i
= 0, cmp
= true; cmp
&& (i
< (acells
+ icells
)); i
++) {
557 cell
= (i
< acells
) ? addrCmp
[i
] : intSpec
[ i
- acells
];
560 cmp
= (cell
== map
[i
]);
563 map
+= acells
+ icells
;
564 if( 0 == (parent
= FindPHandle( *(map
++) )))
567 IODTGetICellCounts( parent
, &picells
, &pacells
);
570 intSpec
= map
+ pacells
;
573 map
+= pacells
+ picells
;
575 } while( !cmp
&& (map
< endMap
) );
581 IODTGetICellCounts( parent
, &icells
, &acells
);
587 return( ok
? original_icells
: 0 );
590 IOReturn
IODTGetInterruptOptions( IORegistryEntry
* regEntry
, int source
, IOOptionBits
* options
)
592 OSArray
* controllers
;
593 OSArray
* specifiers
;
600 controllers
= OSDynamicCast(OSArray
, regEntry
->getProperty(gIOInterruptControllersKey
));
601 specifiers
= OSDynamicCast(OSArray
, regEntry
->getProperty(gIOInterruptSpecifiersKey
));
603 if( !controllers
|| !specifiers
)
604 return (kIOReturnNoInterrupt
);
606 shared
= (OSArray
*) gIODTSharedInterrupts
->getObject(
607 (const OSSymbol
*) controllers
->getObject(source
) );
609 return (kIOReturnSuccess
);
611 spec
= specifiers
->getObject(source
);
613 return (kIOReturnNoInterrupt
);
615 for (unsigned int i
= 0;
616 (oneSpec
= shared
->getObject(i
))
617 && (!oneSpec
->isEqualTo(spec
));
621 *options
= kIODTInterruptShared
;
623 return (kIOReturnSuccess
);
626 static bool IODTMapInterruptsSharing( IORegistryEntry
* regEntry
, OSDictionary
* allInts
)
628 IORegistryEntry
* parent
;
636 OSArray
* controllerInts
;
637 const OSSymbol
* controller
;
638 OSArray
* controllers
;
642 nw
= (0 == (local
= OSDynamicCast( OSData
,
643 regEntry
->getProperty( gIODTAAPLInterruptsKey
))));
644 if( nw
&& (0 == (local
= OSDynamicCast( OSData
,
645 regEntry
->getProperty( "interrupts")))))
646 return( true ); // nothing to see here
648 if( nw
&& (parent
= regEntry
->getParentEntry( gIODTPlane
))) {
649 // check for bridges on old world
650 if( (local2
= OSDynamicCast( OSData
,
651 parent
->getProperty( gIODTAAPLInterruptsKey
)))) {
657 localBits
= (UInt32
*) local
->getBytesNoCopy();
658 localEnd
= localBits
+ (local
->getLength() / sizeof( UInt32
));
659 mapped
= OSArray::withCapacity( 1 );
660 controllers
= OSArray::withCapacity( 1 );
662 ok
= (mapped
&& controllers
);
666 skip
= IODTMapOneInterrupt( regEntry
, localBits
, &map
, &controller
);
668 IOLog("%s: error mapping interrupt[%d]\n",
669 regEntry
->getName(), mapped
->getCount());
673 map
= OSData::withData( local
, mapped
->getCount() * sizeof( UInt32
),
675 controller
= gIODTDefaultInterruptController
;
676 controller
->retain();
680 mapped
->setObject( map
);
681 controllers
->setObject( controller
);
685 controllerInts
= (OSArray
*) allInts
->getObject( controller
);
688 for (unsigned int i
= 0; (oneMap
= controllerInts
->getObject(i
)); i
++)
690 if (map
->isEqualTo(oneMap
))
692 controllerInts
= (OSArray
*) gIODTSharedInterrupts
->getObject( controller
);
694 controllerInts
->setObject(map
);
697 controllerInts
= OSArray::withObjects( (const OSObject
**) &map
, 1, 4 );
700 gIODTSharedInterrupts
->setObject( controller
, controllerInts
);
701 controllerInts
->release();
708 controllerInts
->setObject(map
);
712 controllerInts
= OSArray::withObjects( (const OSObject
**) &map
, 1, 16 );
715 allInts
->setObject( controller
, controllerInts
);
716 controllerInts
->release();
722 controller
->release();
724 } while( localBits
< localEnd
);
726 ok
&= (localBits
== localEnd
);
730 ok
= regEntry
->setProperty( gIOInterruptControllersKey
, controllers
);
731 ok
&= regEntry
->setProperty( gIOInterruptSpecifiersKey
, mapped
);
735 controllers
->release();
742 bool IODTMapInterrupts( IORegistryEntry
* regEntry
)
744 return( IODTMapInterruptsSharing( regEntry
, 0 ));
751 CompareKey( OSString
* key
,
752 const IORegistryEntry
* table
, const OSSymbol
* propName
)
760 const char *lastName
;
763 const char *result
= 0;
765 if( 0 == (prop
= table
->getProperty( propName
)))
768 if( (data
= OSDynamicCast( OSData
, prop
))) {
769 names
= (const char *) data
->getBytesNoCopy();
770 lastName
= names
+ data
->getLength();
771 } else if( (string
= OSDynamicCast( OSString
, prop
))) {
772 names
= string
->getCStringNoCopy();
773 lastName
= names
+ string
->getLength() + 1;
777 ckey
= key
->getCStringNoCopy();
778 keyLen
= key
->getLength();
779 wild
= ('*' == key
->getChar( keyLen
- 1 ));
782 // for each name in the property
784 matched
= (0 == strncmp( ckey
, names
, keyLen
- 1 ));
786 matched
= (keyLen
== strlen( names
))
787 && (0 == strncmp( ckey
, names
, keyLen
));
792 names
= names
+ strlen( names
) + 1;
794 } while( (names
< lastName
) && (false == matched
));
800 bool IODTCompareNubName( const IORegistryEntry
* regEntry
,
801 OSString
* name
, OSString
** matchingName
)
806 matched
= (0 != (result
= CompareKey( name
, regEntry
, gIODTNameKey
)))
807 || (0 != (result
= CompareKey( name
, regEntry
, gIODTCompatibleKey
)))
808 || (0 != (result
= CompareKey( name
, regEntry
, gIODTTypeKey
)))
809 || (0 != (result
= CompareKey( name
, regEntry
, gIODTModelKey
)));
811 if( result
&& matchingName
)
812 *matchingName
= OSString::withCString( result
);
814 return( result
!= 0 );
817 bool IODTMatchNubWithKeys( IORegistryEntry
* regEntry
,
823 obj
= OSUnserialize( keys
, 0 );
826 result
= regEntry
->compareNames( obj
);
830 else IOLog("Couldn't unserialize %s\n", keys
);
836 OSCollectionIterator
* IODTFindMatchingEntries( IORegistryEntry
* from
,
837 IOOptionBits options
, const char * keys
)
840 IORegistryEntry
*next
;
841 IORegistryIterator
*iter
;
842 OSCollectionIterator
*cIter
;
844 bool minus
= options
& kIODTExclusive
;
847 iter
= IORegistryIterator::iterateOver( from
, gIODTPlane
,
848 (options
& kIODTRecursive
) ? kIORegistryIterateRecursively
: 0 );
855 result
= OSSet::withCapacity( 3 );
860 while( (next
= iter
->getNextObject())) {
862 // Look for existence of a debug property to skip
863 if( next
->getProperty("AAPL,ignore"))
867 cmp
= IODTMatchNubWithKeys( next
, keys
);
868 if( (minus
&& (false == cmp
))
869 || ((false == minus
) && (false != cmp
)) )
870 result
->setObject( next
);
872 result
->setObject( next
);
874 } while( !iter
->isValid());
879 cIter
= OSCollectionIterator::withCollection( result
);
886 struct IODTPersistent
{
887 IODTCompareAddressCellFunc compareFunc
;
888 IODTNVLocationFunc locationFunc
;
891 void IODTSetResolving( IORegistryEntry
* regEntry
,
892 IODTCompareAddressCellFunc compareFunc
,
893 IODTNVLocationFunc locationFunc
)
895 IODTPersistent persist
;
898 persist
.compareFunc
= compareFunc
;
899 persist
.locationFunc
= locationFunc
;
900 prop
= OSData::withBytes( &persist
, sizeof( persist
));
904 regEntry
->setProperty( gIODTPersistKey
, prop
);
909 static SInt32
DefaultCompare( UInt32 cellCount
, UInt32 left
[], UInt32 right
[] )
912 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
;
949 IODTPersistent
*persist
;
950 IODTCompareAddressCellFunc compare
;
952 IODTGetCellCounts( regEntry
, &childSizeCells
, &childAddressCells
);
953 childCells
= childAddressCells
+ childSizeCells
;
955 bcopy( cellsIn
, cell
, 4 * childCells
);
956 if( childSizeCells
> 1)
957 *len
= IOPhysical32( cellsIn
[ childAddressCells
],
958 cellsIn
[ childAddressCells
+ 1 ] );
960 *len
= IOPhysical32( 0, cellsIn
[ childAddressCells
] );
963 prop
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTRangeKey
));
965 /* end of the road */
966 *phys
= IOPhysical32( 0, cell
[ childAddressCells
- 1 ] + offset
);
970 parent
= regEntry
->getParentEntry( gIODTPlane
);
971 IODTGetCellCounts( parent
, &sizeCells
, &addressCells
);
973 if( (length
= prop
->getLength())) {
975 range
= (UInt32
*) prop
->getBytesNoCopy();
976 endRanges
= range
+ (length
/ 4);
978 prop
= (OSData
*) regEntry
->getProperty( gIODTPersistKey
);
980 persist
= (IODTPersistent
*) prop
->getBytesNoCopy();
981 compare
= persist
->compareFunc
;
983 compare
= DefaultCompare
;
987 range
+= (childCells
+ addressCells
) ) {
989 // is cell >= range start?
990 diff
= (*compare
)( childAddressCells
, cell
, range
);
994 // is cell + size <= range end?
995 if( (diff
+ cell
[ childCells
- 1 ])
996 > range
[ childCells
+ addressCells
- 1 ])
1004 // Get the physical start of the range from our parent
1005 bcopy( range
+ childAddressCells
, cell
, 4 * addressCells
);
1006 bzero( cell
+ addressCells
, 4 * sizeCells
);
1008 } /* else zero length range => pass thru to parent */
1011 childSizeCells
= sizeCells
;
1012 childAddressCells
= addressCells
;
1013 childCells
= childAddressCells
+ childSizeCells
;
1015 } while( ok
&& regEntry
);
1021 OSArray
* IODTResolveAddressing( IORegistryEntry
* regEntry
,
1022 const char * addressPropertyName
,
1023 IODeviceMemory
* parent
)
1025 IORegistryEntry
*parentEntry
;
1026 OSData
*addressProperty
;
1027 UInt32 sizeCells
, addressCells
, cells
;
1030 IOPhysicalAddress phys
;
1031 IOPhysicalLength len
;
1033 IODeviceMemory
*range
;
1035 parentEntry
= regEntry
->getParentEntry( gIODTPlane
);
1036 addressProperty
= (OSData
*) regEntry
->getProperty( addressPropertyName
);
1037 if( (0 == addressProperty
) || (0 == parentEntry
))
1040 IODTGetCellCounts( parentEntry
, &sizeCells
, &addressCells
);
1044 cells
= sizeCells
+ addressCells
;
1045 reg
= (UInt32
*) addressProperty
->getBytesNoCopy();
1046 num
= addressProperty
->getLength() / (4 * cells
);
1048 array
= OSArray::withCapacity( 1 );
1052 for( i
= 0; i
< num
; i
++) {
1053 if( IODTResolveAddressCell( parentEntry
, reg
, &phys
, &len
)) {
1056 range
= IODeviceMemory::withSubRange( parent
,
1057 phys
- parent
->getPhysicalAddress(), len
);
1059 range
= IODeviceMemory::withRange( phys
, len
);
1061 array
->setObject( range
);
1066 regEntry
->setProperty( gIODeviceMemoryKey
, array
);
1067 array
->release(); /* ??? */
1072 static void IODTGetNVLocation(
1073 IORegistryEntry
* parent
,
1074 IORegistryEntry
* regEntry
,
1075 UInt8
* busNum
, UInt8
* deviceNum
, UInt8
* functionNum
)
1079 IODTPersistent
*persist
;
1082 prop
= (OSData
*) parent
->getProperty( gIODTPersistKey
);
1084 persist
= (IODTPersistent
*) prop
->getBytesNoCopy();
1085 (*persist
->locationFunc
)( regEntry
, busNum
, deviceNum
, functionNum
);
1087 prop
= (OSData
*) regEntry
->getProperty( "reg" );
1090 cell
= (UInt32
*) prop
->getBytesNoCopy();
1092 *deviceNum
= 0x1f & (cell
[ 0 ] >> 24);
1102 * Try to make the same messed up descriptor as Mac OS
1105 IOReturn
IODTMakeNVDescriptor( IORegistryEntry
* regEntry
,
1106 IONVRAMDescriptor
* hdr
)
1108 IORegistryEntry
*parent
;
1110 UInt32 bridgeDevices
;
1118 for(level
= 0, bridgeDevices
= 0;
1119 (parent
= regEntry
->getParentEntry( gIODTPlane
)) && (level
< 7); level
++ ) {
1121 IODTGetNVLocation( parent
, regEntry
,
1122 &busNum
, &deviceNum
, &functionNum
);
1124 bridgeDevices
|= ((deviceNum
& 0x1f) << ((level
- 1) * 5));
1126 hdr
->busNum
= busNum
;
1127 hdr
->deviceNum
= deviceNum
;
1128 hdr
->functionNum
= functionNum
;
1132 hdr
->bridgeCount
= level
- 2;
1133 hdr
->bridgeDevices
= bridgeDevices
;
1135 return( kIOReturnSuccess
);
1138 OSData
* IODTFindSlotName( IORegistryEntry
* regEntry
, UInt32 deviceNumber
)
1140 IORegistryEntry
*parent
;
1149 data
= (OSData
*) regEntry
->getProperty("AAPL,slot-name");
1152 parent
= regEntry
->getParentEntry( gIODTPlane
);
1155 data
= OSDynamicCast( OSData
, parent
->getProperty("slot-names"));
1158 if( data
->getLength() <= 4)
1161 bits
= (UInt32
*) data
->getBytesNoCopy();
1163 if( (0 == (mask
& (1 << deviceNumber
))))
1166 names
= (char *)(bits
+ 1);
1167 lastName
= names
+ (data
->getLength() - 4);
1169 for( i
= 0; (i
<= deviceNumber
) && (names
< lastName
); i
++ ) {
1171 if( mask
& (1 << i
)) {
1172 if( i
== deviceNumber
) {
1173 data
= OSData::withBytesNoCopy( names
, 1 + strlen( names
));
1175 regEntry
->setProperty("AAPL,slot-name", data
);
1180 names
+= 1 + strlen( names
);
1187 extern "C" IOReturn
IONDRVLibrariesInitialize( IOService
* provider
)
1189 return( kIOReturnUnsupported
);