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 virt
= ml_static_ptovirt( range
[0] );
315 ml_static_mfree( virt
, range
[1] );
319 static IORegistryEntry
*
320 MakeReferenceTable( DTEntry dtEntry
, bool copy
)
322 IORegistryEntry
*regEntry
;
323 OSDictionary
*propTable
;
324 const OSSymbol
*nameKey
;
327 DTPropertyIterator dtIter
;
332 bool noLocation
= true;
334 regEntry
= new IOService
;
336 if( regEntry
&& (false == regEntry
->init())) {
342 (kSuccess
== DTCreatePropertyIterator( dtEntry
, &dtIter
))) {
344 propTable
= regEntry
->getPropertyTable();
346 while( kSuccess
== DTIterateProperties( dtIter
, &name
)) {
348 if( kSuccess
!= DTGetProperty( dtEntry
, name
, &prop
, &propSize
))
352 nameKey
= OSSymbol::withCString(name
);
353 data
= OSData::withBytes(prop
, propSize
);
355 nameKey
= OSSymbol::withCStringNoCopy(name
);
356 data
= OSData::withBytesNoCopy(prop
, propSize
);
358 assert( nameKey
&& data
);
360 propTable
->setObject( nameKey
, data
);
364 if( nameKey
== gIODTNameKey
) {
366 sym
= OSSymbol::withCString( (const char *) prop
);
368 sym
= OSSymbol::withCStringNoCopy( (const char *) prop
);
369 regEntry
->setName( sym
);
372 } else if( nameKey
== gIODTUnitKey
) {
373 // all OF strings are null terminated... except this one
374 if( propSize
>= (int) sizeof( location
))
375 propSize
= sizeof( location
) - 1;
376 strncpy( location
, (const char *) prop
, propSize
);
377 location
[ propSize
] = 0;
378 regEntry
->setLocation( location
);
379 propTable
->removeObject( gIODTUnitKey
);
382 } else if( noLocation
&& (0 == strcmp( name
, "reg"))) {
383 // default location - override later
384 sprintf( location
, "%lX", *((UInt32
*) prop
) );
385 regEntry
->setLocation( location
);
388 DTDisposePropertyIterator( dtIter
);
394 static void AddPHandle( IORegistryEntry
* regEntry
)
398 if( regEntry
->getProperty( gIODTInterruptCellKey
)
399 && (data
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTPHandleKey
)))) {
400 // a possible interrupt-parent
401 gIODTPHandles
->setObject( data
);
402 gIODTPHandleMap
->setObject( regEntry
);
406 static IORegistryEntry
* FindPHandle( UInt32 phandle
)
409 IORegistryEntry
*regEntry
= 0;
412 for( i
= 0; (data
= (OSData
*)gIODTPHandles
->getObject( i
)); i
++ ) {
413 if( phandle
== *((UInt32
*)data
->getBytesNoCopy())) {
414 regEntry
= (IORegistryEntry
*)
415 gIODTPHandleMap
->getObject( i
);
423 static bool GetUInt32( IORegistryEntry
* regEntry
, const OSSymbol
* name
,
428 if( (data
= OSDynamicCast( OSData
, regEntry
->getProperty( name
)))
429 && (4 == data
->getLength())) {
430 *value
= *((UInt32
*) data
->getBytesNoCopy());
436 IORegistryEntry
* IODTFindInterruptParent( IORegistryEntry
* regEntry
)
438 IORegistryEntry
* parent
;
441 if( GetUInt32( regEntry
, gIODTInterruptParentKey
, &phandle
))
442 parent
= FindPHandle( phandle
);
444 else if( 0 == regEntry
->getProperty( "interrupt-controller"))
445 parent
= regEntry
->getParentEntry( gIODTPlane
);
452 const OSSymbol
* IODTInterruptControllerName( IORegistryEntry
* regEntry
)
459 ok
= GetUInt32( regEntry
, gIODTPHandleKey
, &phandle
);
463 sprintf( buf
, "IOInterruptController%08lX", phandle
);
464 sym
= OSSymbol::withCString( buf
);
471 #define unexpected(a) { kprintf("unexpected %s:%d\n", __FILE__, __LINE__); a; }
473 static void IODTGetICellCounts( IORegistryEntry
* regEntry
,
474 UInt32
* iCellCount
, UInt32
* aCellCount
)
476 if( !GetUInt32( regEntry
, gIODTInterruptCellKey
, iCellCount
))
477 unexpected( *iCellCount
= 1 );
478 if( !GetUInt32( regEntry
, gIODTAddressCellKey
, aCellCount
))
482 UInt32
IODTMapOneInterrupt( IORegistryEntry
* regEntry
, UInt32
* intSpec
,
483 OSData
** spec
, const OSSymbol
** controller
)
485 IORegistryEntry
*parent
= 0;
491 UInt32 acells
, icells
, pacells
, picells
, cell
;
492 UInt32 i
, original_icells
;
493 bool cmp
, ok
= false;
495 parent
= IODTFindInterruptParent( regEntry
);
496 IODTGetICellCounts( parent
, &icells
, &acells
);
499 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "reg" ));
500 if( data
&& (data
->getLength() >= (acells
* sizeof( UInt32
))))
501 addrCmp
= (UInt32
*) data
->getBytesNoCopy();
503 original_icells
= icells
;
508 kprintf ("IODTMapOneInterrupt: current regEntry name %s\n", regEntry
->getName());
509 kprintf ("acells - icells: ");
510 for (i
= 0; i
< acells
; i
++) kprintf ("0x%08X ", addrCmp
[i
]);
512 for (i
= 0; i
< icells
; i
++) kprintf ("0x%08X ", intSpec
[i
]);
516 if( parent
&& (data
= OSDynamicCast( OSData
,
517 regEntry
->getProperty( "interrupt-controller")))) {
518 // found a controller - don't want to follow cascaded controllers
520 *spec
= OSData::withBytesNoCopy( (void *) intSpec
,
521 icells
* sizeof( UInt32
));
522 *controller
= IODTInterruptControllerName( regEntry
);
523 ok
= (*spec
&& *controller
);
524 } else if( parent
&& (data
= OSDynamicCast( OSData
,
525 regEntry
->getProperty( "interrupt-map")))) {
527 map
= (UInt32
*) data
->getBytesNoCopy();
528 endMap
= map
+ (data
->getLength() / sizeof(UInt32
));
529 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "interrupt-map-mask" ));
530 if( data
&& (data
->getLength() >= ((acells
+ icells
) * sizeof( UInt32
))))
531 maskCmp
= (UInt32
*) data
->getBytesNoCopy();
537 kprintf (" maskCmp: ");
538 for (i
= 0; i
< acells
+ icells
; i
++) {
541 kprintf ("0x%08X ", maskCmp
[i
]);
544 kprintf (" masked: ");
545 for (i
= 0; i
< acells
+ icells
; i
++) {
548 kprintf ("0x%08X ", ((i
< acells
) ? addrCmp
[i
] : intSpec
[i
-acells
]) & maskCmp
[i
]);
552 kprintf ("no maskCmp\n");
557 for (i
= 0; i
< acells
+ icells
; i
++) {
560 kprintf ("0x%08X ", map
[i
]);
564 for( i
= 0, cmp
= true; cmp
&& (i
< (acells
+ icells
)); i
++) {
565 cell
= (i
< acells
) ? addrCmp
[i
] : intSpec
[ i
- acells
];
568 cmp
= (cell
== map
[i
]);
571 map
+= acells
+ icells
;
572 if( 0 == (parent
= FindPHandle( *(map
++) )))
575 IODTGetICellCounts( parent
, &picells
, &pacells
);
578 intSpec
= map
+ pacells
;
581 map
+= pacells
+ picells
;
583 } while( !cmp
&& (map
< endMap
) );
589 IODTGetICellCounts( parent
, &icells
, &acells
);
595 return( ok
? original_icells
: 0 );
598 IOReturn
IODTGetInterruptOptions( IORegistryEntry
* regEntry
, int source
, IOOptionBits
* options
)
600 OSArray
* controllers
;
601 OSArray
* specifiers
;
608 controllers
= OSDynamicCast(OSArray
, regEntry
->getProperty(gIOInterruptControllersKey
));
609 specifiers
= OSDynamicCast(OSArray
, regEntry
->getProperty(gIOInterruptSpecifiersKey
));
611 if( !controllers
|| !specifiers
)
612 return (kIOReturnNoInterrupt
);
614 shared
= (OSArray
*) gIODTSharedInterrupts
->getObject(
615 (const OSSymbol
*) controllers
->getObject(source
) );
617 return (kIOReturnSuccess
);
619 spec
= specifiers
->getObject(source
);
621 return (kIOReturnNoInterrupt
);
623 for (unsigned int i
= 0;
624 (oneSpec
= shared
->getObject(i
))
625 && (!oneSpec
->isEqualTo(spec
));
629 *options
= kIODTInterruptShared
;
631 return (kIOReturnSuccess
);
634 static bool IODTMapInterruptsSharing( IORegistryEntry
* regEntry
, OSDictionary
* allInts
)
636 IORegistryEntry
* parent
;
644 OSArray
* controllerInts
;
645 const OSSymbol
* controller
;
646 OSArray
* controllers
;
650 nw
= (0 == (local
= OSDynamicCast( OSData
,
651 regEntry
->getProperty( gIODTAAPLInterruptsKey
))));
652 if( nw
&& (0 == (local
= OSDynamicCast( OSData
,
653 regEntry
->getProperty( "interrupts")))))
654 return( true ); // nothing to see here
656 if( nw
&& (parent
= regEntry
->getParentEntry( gIODTPlane
))) {
657 // check for bridges on old world
658 if( (local2
= OSDynamicCast( OSData
,
659 parent
->getProperty( gIODTAAPLInterruptsKey
)))) {
665 localBits
= (UInt32
*) local
->getBytesNoCopy();
666 localEnd
= localBits
+ (local
->getLength() / sizeof( UInt32
));
667 mapped
= OSArray::withCapacity( 1 );
668 controllers
= OSArray::withCapacity( 1 );
670 ok
= (mapped
&& controllers
);
674 skip
= IODTMapOneInterrupt( regEntry
, localBits
, &map
, &controller
);
676 IOLog("%s: error mapping interrupt[%d]\n",
677 regEntry
->getName(), mapped
->getCount());
681 map
= OSData::withData( local
, mapped
->getCount() * sizeof( UInt32
),
683 controller
= gIODTDefaultInterruptController
;
684 controller
->retain();
688 mapped
->setObject( map
);
689 controllers
->setObject( controller
);
693 controllerInts
= (OSArray
*) allInts
->getObject( controller
);
696 for (unsigned int i
= 0; (oneMap
= controllerInts
->getObject(i
)); i
++)
698 if (map
->isEqualTo(oneMap
))
700 controllerInts
= (OSArray
*) gIODTSharedInterrupts
->getObject( controller
);
702 controllerInts
->setObject(map
);
705 controllerInts
= OSArray::withObjects( (const OSObject
**) &map
, 1, 4 );
708 gIODTSharedInterrupts
->setObject( controller
, controllerInts
);
709 controllerInts
->release();
716 controllerInts
->setObject(map
);
720 controllerInts
= OSArray::withObjects( (const OSObject
**) &map
, 1, 16 );
723 allInts
->setObject( controller
, controllerInts
);
724 controllerInts
->release();
730 controller
->release();
732 } while( localBits
< localEnd
);
734 ok
&= (localBits
== localEnd
);
738 ok
= regEntry
->setProperty( gIOInterruptControllersKey
, controllers
);
739 ok
&= regEntry
->setProperty( gIOInterruptSpecifiersKey
, mapped
);
743 controllers
->release();
750 bool IODTMapInterrupts( IORegistryEntry
* regEntry
)
752 return( IODTMapInterruptsSharing( regEntry
, 0 ));
759 CompareKey( OSString
* key
,
760 const IORegistryEntry
* table
, const OSSymbol
* propName
)
768 const char *lastName
;
771 const char *result
= 0;
773 if( 0 == (prop
= table
->getProperty( propName
)))
776 if( (data
= OSDynamicCast( OSData
, prop
))) {
777 names
= (const char *) data
->getBytesNoCopy();
778 lastName
= names
+ data
->getLength();
779 } else if( (string
= OSDynamicCast( OSString
, prop
))) {
780 names
= string
->getCStringNoCopy();
781 lastName
= names
+ string
->getLength() + 1;
785 ckey
= key
->getCStringNoCopy();
786 keyLen
= key
->getLength();
787 wild
= ('*' == key
->getChar( keyLen
- 1 ));
790 // for each name in the property
792 matched
= (0 == strncmp( ckey
, names
, keyLen
- 1 ));
794 matched
= (keyLen
== strlen( names
))
795 && (0 == strncmp( ckey
, names
, keyLen
));
800 names
= names
+ strlen( names
) + 1;
802 } while( (names
< lastName
) && (false == matched
));
808 bool IODTCompareNubName( const IORegistryEntry
* regEntry
,
809 OSString
* name
, OSString
** matchingName
)
814 matched
= (0 != (result
= CompareKey( name
, regEntry
, gIODTNameKey
)))
815 || (0 != (result
= CompareKey( name
, regEntry
, gIODTCompatibleKey
)))
816 || (0 != (result
= CompareKey( name
, regEntry
, gIODTTypeKey
)))
817 || (0 != (result
= CompareKey( name
, regEntry
, gIODTModelKey
)));
819 if( result
&& matchingName
)
820 *matchingName
= OSString::withCString( result
);
822 return( result
!= 0 );
825 bool IODTMatchNubWithKeys( IORegistryEntry
* regEntry
,
831 obj
= OSUnserialize( keys
, 0 );
834 result
= regEntry
->compareNames( obj
);
838 else IOLog("Couldn't unserialize %s\n", keys
);
844 OSCollectionIterator
* IODTFindMatchingEntries( IORegistryEntry
* from
,
845 IOOptionBits options
, const char * keys
)
848 IORegistryEntry
*next
;
849 IORegistryIterator
*iter
;
850 OSCollectionIterator
*cIter
;
852 bool minus
= options
& kIODTExclusive
;
855 iter
= IORegistryIterator::iterateOver( from
, gIODTPlane
,
856 (options
& kIODTRecursive
) ? kIORegistryIterateRecursively
: 0 );
863 result
= OSSet::withCapacity( 3 );
868 while( (next
= iter
->getNextObject())) {
870 // Look for existence of a debug property to skip
871 if( next
->getProperty("AAPL,ignore"))
875 cmp
= IODTMatchNubWithKeys( next
, keys
);
876 if( (minus
&& (false == cmp
))
877 || ((false == minus
) && (false != cmp
)) )
878 result
->setObject( next
);
880 result
->setObject( next
);
882 } while( !iter
->isValid());
887 cIter
= OSCollectionIterator::withCollection( result
);
894 struct IODTPersistent
{
895 IODTCompareAddressCellFunc compareFunc
;
896 IODTNVLocationFunc locationFunc
;
899 void IODTSetResolving( IORegistryEntry
* regEntry
,
900 IODTCompareAddressCellFunc compareFunc
,
901 IODTNVLocationFunc locationFunc
)
903 IODTPersistent persist
;
906 persist
.compareFunc
= compareFunc
;
907 persist
.locationFunc
= locationFunc
;
908 prop
= OSData::withBytes( &persist
, sizeof( persist
));
912 regEntry
->setProperty( gIODTPersistKey
, prop
);
917 static SInt32
DefaultCompare( UInt32 cellCount
, UInt32 left
[], UInt32 right
[] )
920 return( left
[ cellCount
] - right
[ cellCount
] );
923 void IODTGetCellCounts( IORegistryEntry
* regEntry
,
924 UInt32
* sizeCount
, UInt32
* addressCount
)
926 if( !GetUInt32( regEntry
, gIODTSizeCellKey
, sizeCount
))
928 if( !GetUInt32( regEntry
, gIODTAddressCellKey
, addressCount
))
933 // Given addr & len cells from our child, find it in our ranges property, then
934 // look in our parent to resolve the base of the range for us.
936 // Range[]: child-addr our-addr child-len
937 // #cells: child ours child
939 bool IODTResolveAddressCell( IORegistryEntry
* regEntry
,
941 IOPhysicalAddress
* phys
, IOPhysicalLength
* len
)
943 IORegistryEntry
*parent
;
945 // cells in addresses at regEntry
946 UInt32 sizeCells
, addressCells
;
947 // cells in addresses below regEntry
948 UInt32 childSizeCells
, childAddressCells
;
950 UInt32 cell
[ 5 ], offset
= 0, length
;
957 SInt32 diff
, endDiff
;
959 IODTPersistent
*persist
;
960 IODTCompareAddressCellFunc compare
;
962 IODTGetCellCounts( regEntry
, &childSizeCells
, &childAddressCells
);
963 childCells
= childAddressCells
+ childSizeCells
;
965 bcopy( cellsIn
, cell
, 4 * childCells
);
966 if( childSizeCells
> 1)
967 *len
= IOPhysical32( cellsIn
[ childAddressCells
],
968 cellsIn
[ childAddressCells
+ 1 ] );
970 *len
= IOPhysical32( 0, cellsIn
[ childAddressCells
] );
974 prop
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTRangeKey
));
976 /* end of the road */
977 *phys
= IOPhysical32( 0, cell
[ childAddressCells
- 1 ] + offset
);
981 parent
= regEntry
->getParentEntry( gIODTPlane
);
982 IODTGetCellCounts( parent
, &sizeCells
, &addressCells
);
984 if( (length
= prop
->getLength())) {
986 startRange
= (UInt32
*) prop
->getBytesNoCopy();
988 endRanges
= range
+ (length
/ 4);
990 prop
= (OSData
*) regEntry
->getProperty( gIODTPersistKey
);
992 persist
= (IODTPersistent
*) prop
->getBytesNoCopy();
993 compare
= persist
->compareFunc
;
995 compare
= DefaultCompare
;
999 range
+= (childCells
+ addressCells
) ) {
1001 // is cell start >= range start?
1002 diff
= (*compare
)( childAddressCells
, cell
, range
);
1006 ok
= (0 == cell
[childCells
- 1]);
1009 // search for cell end
1010 bcopy(cell
, endCell
, childAddressCells
* sizeof(UInt32
));
1011 endCell
[childAddressCells
- 1] += cell
[childCells
- 1] - 1;
1012 lookRange
= startRange
;
1014 lookRange
< endRanges
;
1015 lookRange
+= (childCells
+ addressCells
) )
1017 // is cell >= range start?
1018 endDiff
= (*compare
)( childAddressCells
, endCell
, lookRange
);
1021 if ((endDiff
- cell
[childCells
- 1] + 1 + lookRange
[childAddressCells
+ addressCells
- 1])
1022 == (diff
+ range
[childAddressCells
+ addressCells
- 1]))
1035 // Get the physical start of the range from our parent
1036 bcopy( range
+ childAddressCells
, cell
, 4 * addressCells
);
1037 bzero( cell
+ addressCells
, 4 * sizeCells
);
1039 } /* else zero length range => pass thru to parent */
1042 childSizeCells
= sizeCells
;
1043 childAddressCells
= addressCells
;
1044 childCells
= childAddressCells
+ childSizeCells
;
1046 while( ok
&& regEntry
);
1052 OSArray
* IODTResolveAddressing( IORegistryEntry
* regEntry
,
1053 const char * addressPropertyName
,
1054 IODeviceMemory
* parent
)
1056 IORegistryEntry
*parentEntry
;
1057 OSData
*addressProperty
;
1058 UInt32 sizeCells
, addressCells
, cells
;
1061 IOPhysicalAddress phys
;
1062 IOPhysicalLength len
;
1064 IODeviceMemory
*range
;
1066 parentEntry
= regEntry
->getParentEntry( gIODTPlane
);
1067 addressProperty
= (OSData
*) regEntry
->getProperty( addressPropertyName
);
1068 if( (0 == addressProperty
) || (0 == parentEntry
))
1071 IODTGetCellCounts( parentEntry
, &sizeCells
, &addressCells
);
1075 cells
= sizeCells
+ addressCells
;
1076 reg
= (UInt32
*) addressProperty
->getBytesNoCopy();
1077 num
= addressProperty
->getLength() / (4 * cells
);
1079 array
= OSArray::withCapacity( 1 );
1083 for( i
= 0; i
< num
; i
++) {
1084 if( IODTResolveAddressCell( parentEntry
, reg
, &phys
, &len
)) {
1087 range
= IODeviceMemory::withSubRange( parent
,
1088 phys
- parent
->getPhysicalAddress(), len
);
1090 range
= IODeviceMemory::withRange( phys
, len
);
1092 array
->setObject( range
);
1097 regEntry
->setProperty( gIODeviceMemoryKey
, array
);
1098 array
->release(); /* ??? */
1103 static void IODTGetNVLocation(
1104 IORegistryEntry
* parent
,
1105 IORegistryEntry
* regEntry
,
1106 UInt8
* busNum
, UInt8
* deviceNum
, UInt8
* functionNum
)
1110 IODTPersistent
*persist
;
1113 prop
= (OSData
*) parent
->getProperty( gIODTPersistKey
);
1115 persist
= (IODTPersistent
*) prop
->getBytesNoCopy();
1116 (*persist
->locationFunc
)( regEntry
, busNum
, deviceNum
, functionNum
);
1118 prop
= (OSData
*) regEntry
->getProperty( "reg" );
1121 cell
= (UInt32
*) prop
->getBytesNoCopy();
1123 *deviceNum
= 0x1f & (cell
[ 0 ] >> 24);
1133 * Try to make the same messed up descriptor as Mac OS
1136 IOReturn
IODTMakeNVDescriptor( IORegistryEntry
* regEntry
,
1137 IONVRAMDescriptor
* hdr
)
1139 IORegistryEntry
*parent
;
1141 UInt32 bridgeDevices
;
1149 for(level
= 0, bridgeDevices
= 0;
1150 (parent
= regEntry
->getParentEntry( gIODTPlane
)) && (level
< 7); level
++ ) {
1152 IODTGetNVLocation( parent
, regEntry
,
1153 &busNum
, &deviceNum
, &functionNum
);
1155 bridgeDevices
|= ((deviceNum
& 0x1f) << ((level
- 1) * 5));
1157 hdr
->busNum
= busNum
;
1158 hdr
->deviceNum
= deviceNum
;
1159 hdr
->functionNum
= functionNum
;
1163 hdr
->bridgeCount
= level
- 2;
1164 hdr
->bridgeDevices
= bridgeDevices
;
1166 return( kIOReturnSuccess
);
1169 OSData
* IODTFindSlotName( IORegistryEntry
* regEntry
, UInt32 deviceNumber
)
1171 IORegistryEntry
*parent
;
1180 data
= (OSData
*) regEntry
->getProperty("AAPL,slot-name");
1183 parent
= regEntry
->getParentEntry( gIODTPlane
);
1186 data
= OSDynamicCast( OSData
, parent
->getProperty("slot-names"));
1189 if( data
->getLength() <= 4)
1192 bits
= (UInt32
*) data
->getBytesNoCopy();
1194 if( (0 == (mask
& (1 << deviceNumber
))))
1197 names
= (char *)(bits
+ 1);
1198 lastName
= names
+ (data
->getLength() - 4);
1200 for( i
= 0; (i
<= deviceNumber
) && (names
< lastName
); i
++ ) {
1202 if( mask
& (1 << i
)) {
1203 if( i
== deviceNumber
) {
1204 data
= OSData::withBytesNoCopy( names
, 1 + strlen( names
));
1206 regEntry
->setProperty("AAPL,slot-name", data
);
1211 names
+= 1 + strlen( names
);
1218 extern "C" IOReturn
IONDRVLibrariesInitialize( IOService
* provider
)
1220 return( kIOReturnUnsupported
);