2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
26 * 23 Nov 98 sdouglas, created from IODeviceTreeBus.m, & MacOS exp mgr.
27 * 05 Apr 99 sdouglas, add interrupt mapping.
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
;
79 static IORegistryEntry
* MakeReferenceTable( DTEntry dtEntry
, bool copy
);
80 static void AddPHandle( IORegistryEntry
* regEntry
);
81 static void FreePhysicalMemory( vm_offset_t
* range
);
84 IODeviceTreeAlloc( void * dtTop
)
86 IORegistryEntry
*parent
;
87 IORegistryEntry
*child
;
88 IORegistryIterator
*regIter
;
100 gIODTPlane
= IORegistryEntry::makePlane( kIODeviceTreePlane
);
102 gIODTNameKey
= OSSymbol::withCStringNoCopy( "name" );
103 gIODTUnitKey
= OSSymbol::withCStringNoCopy( "AAPL,unit-string" );
104 gIODTCompatibleKey
= OSSymbol::withCStringNoCopy( "compatible" );
105 gIODTTypeKey
= OSSymbol::withCStringNoCopy( "device_type" );
106 gIODTModelKey
= OSSymbol::withCStringNoCopy( "model" );
107 gIODTSizeCellKey
= OSSymbol::withCStringNoCopy( "#size-cells" );
108 gIODTAddressCellKey
= OSSymbol::withCStringNoCopy( "#address-cells" );
109 gIODTRangeKey
= OSSymbol::withCStringNoCopy( "ranges" );
110 gIODTPersistKey
= OSSymbol::withCStringNoCopy( "IODTPersist" );
112 assert( gIODTPlane
&& gIODTCompatibleKey
113 && gIODTTypeKey
&& gIODTModelKey
114 && gIODTSizeCellKey
&& gIODTAddressCellKey
&& gIODTRangeKey
115 && gIODTPersistKey
);
117 gIODTDefaultInterruptController
118 = OSSymbol::withCStringNoCopy("IOPrimaryInterruptController");
119 gIODTNWInterruptMappingKey
120 = OSSymbol::withCStringNoCopy("IONWInterrupts");
122 gIODTAAPLInterruptsKey
123 = OSSymbol::withCStringNoCopy("AAPL,interrupts");
125 = OSSymbol::withCStringNoCopy("AAPL,phandle");
127 gIODTInterruptParentKey
128 = OSSymbol::withCStringNoCopy("interrupt-parent");
130 gIODTPHandles
= OSArray::withCapacity( 1 );
131 gIODTPHandleMap
= OSArray::withCapacity( 1 );
133 gIODTInterruptCellKey
134 = OSSymbol::withCStringNoCopy("#interrupt-cells");
136 assert( gIODTDefaultInterruptController
&& gIODTNWInterruptMappingKey
137 && gIODTAAPLInterruptsKey
138 && gIODTPHandleKey
&& gIODTInterruptParentKey
139 && gIODTPHandles
&& gIODTPHandleMap
140 && gIODTInterruptCellKey
143 freeDT
= (kSuccess
== DTLookupEntry( 0, "/chosen/memory-map", &mapEntry
))
144 && (kSuccess
== DTGetProperty( mapEntry
,
145 "DeviceTree", (void **) &dtMap
, &propSize
))
146 && ((2 * sizeof( vm_offset_t
)) == propSize
);
148 parent
= MakeReferenceTable( (DTEntry
)dtTop
, freeDT
);
150 stack
= OSArray::withObjects( (const OSObject
**) &parent
, 1, 10 );
151 DTCreateEntryIterator( (DTEntry
)dtTop
, &iter
);
154 parent
= (IORegistryEntry
*)stack
->getObject( stack
->getCount() - 1);
156 stack
->removeObject( stack
->getCount() - 1);
158 while( kSuccess
== DTIterateEntries( iter
, &dtChild
) ) {
160 child
= MakeReferenceTable( dtChild
, freeDT
);
161 child
->attachToParent( parent
, gIODTPlane
);
165 if( kSuccess
== DTEnterEntry( iter
, dtChild
)) {
166 stack
->setObject( parent
);
169 // only registry holds retain
173 } while( stack
->getCount()
174 && (kSuccess
== DTExitEntry( iter
, &dtChild
)));
177 DTDisposeEntryIterator( iter
);
179 // parent is now root of the created tree
181 // make root name first compatible entry (purely cosmetic)
182 if( (prop
= (OSData
*) parent
->getProperty( gIODTCompatibleKey
))) {
183 parent
->setName( parent
->getName(), gIODTPlane
);
184 parent
->setName( (const char *) prop
->getBytesNoCopy() );
187 // attach tree to meta root
188 parent
->attachToParent( IORegistryEntry::getRegistryRoot(), gIODTPlane
);
192 // free original device tree
194 IODTFreeLoaderInfo( "DeviceTree",
195 (void *)dtMap
[0], round_page_32(dtMap
[1]) );
200 regIter
= IORegistryIterator::iterateOver( gIODTPlane
,
201 kIORegistryIterateRecursively
);
204 while( (child
= regIter
->getNextObject())) {
205 IODTMapInterrupts( child
);
206 if( !intMap
&& child
->getProperty( gIODTInterruptParentKey
))
209 // Look for a "driver,AAPL,MacOSX,PowerPC" property.
210 if( (obj
= child
->getProperty( "driver,AAPL,MacOSX,PowerPC"))) {
211 gIOCatalogue
->addExtensionsFromArchive((OSData
*)obj
);
212 child
->removeProperty( "driver,AAPL,MacOSX,PowerPC");
215 // some gross pruning
216 child
->removeProperty( "lanLib,AAPL,MacOS,PowerPC");
218 if( (obj
= child
->getProperty( "driver,AAPL,MacOS,PowerPC"))) {
220 if( (0 == (prop
= (OSData
*)child
->getProperty( gIODTTypeKey
)))
221 || (strcmp( "display", (char *) prop
->getBytesNoCopy())) ) {
222 child
->removeProperty( "driver,AAPL,MacOS,PowerPC");
230 // set a key in the root to indicate we found NW interrupt mapping
231 parent
->setProperty( gIODTNWInterruptMappingKey
,
232 (OSObject
*) gIODTNWInterruptMappingKey
);
237 int IODTGetLoaderInfo( char *key
, void **infoAddr
, int *infoSize
)
239 IORegistryEntry
*chosen
;
241 unsigned int *propPtr
;
242 unsigned int propSize
;
244 chosen
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
);
245 if ( chosen
== 0 ) return -1;
247 propObj
= OSDynamicCast( OSData
, chosen
->getProperty(key
) );
248 if ( propObj
== 0 ) return -1;
250 propSize
= propObj
->getLength();
251 if ( propSize
!= (2 * sizeof(UInt32
)) ) return -1;
253 propPtr
= (unsigned int *)propObj
->getBytesNoCopy();
254 if ( propPtr
== 0 ) return -1;
256 *infoAddr
= (void *)propPtr
[0] ;
257 *infoSize
= (int) propPtr
[1];
262 void IODTFreeLoaderInfo( char *key
, void *infoAddr
, int infoSize
)
264 vm_offset_t range
[2];
265 IORegistryEntry
*chosen
;
267 range
[0] = (vm_offset_t
)infoAddr
;
268 range
[1] = (vm_offset_t
)infoSize
;
269 FreePhysicalMemory( range
);
272 chosen
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
);
274 chosen
->removeProperty(key
);
279 static void FreePhysicalMemory( vm_offset_t
* range
)
283 virt
= ml_static_ptovirt( range
[0] );
285 ml_static_mfree( virt
, range
[1] );
289 static IORegistryEntry
*
290 MakeReferenceTable( DTEntry dtEntry
, bool copy
)
292 IORegistryEntry
*regEntry
;
293 OSDictionary
*propTable
;
294 const OSSymbol
*nameKey
;
297 DTPropertyIterator dtIter
;
302 bool noLocation
= true;
304 regEntry
= new IOService
;
306 if( regEntry
&& (false == regEntry
->init())) {
312 (kSuccess
== DTCreatePropertyIterator( dtEntry
, &dtIter
))) {
314 propTable
= regEntry
->getPropertyTable();
316 while( kSuccess
== DTIterateProperties( dtIter
, &name
)) {
318 if( kSuccess
!= DTGetProperty( dtEntry
, name
, &prop
, &propSize
))
322 nameKey
= OSSymbol::withCString(name
);
323 data
= OSData::withBytes(prop
, propSize
);
325 nameKey
= OSSymbol::withCStringNoCopy(name
);
326 data
= OSData::withBytesNoCopy(prop
, propSize
);
328 assert( nameKey
&& data
);
330 propTable
->setObject( nameKey
, data
);
334 if( nameKey
== gIODTNameKey
) {
336 sym
= OSSymbol::withCString( (const char *) prop
);
338 sym
= OSSymbol::withCStringNoCopy( (const char *) prop
);
339 regEntry
->setName( sym
);
342 } else if( nameKey
== gIODTUnitKey
) {
343 // all OF strings are null terminated... except this one
344 if( propSize
>= (int) sizeof( location
))
345 propSize
= sizeof( location
) - 1;
346 strncpy( location
, (const char *) prop
, propSize
);
347 location
[ propSize
] = 0;
348 regEntry
->setLocation( location
);
349 propTable
->removeObject( gIODTUnitKey
);
352 } else if( noLocation
&& (0 == strcmp( name
, "reg"))) {
353 // default location - override later
354 sprintf( location
, "%lX", *((UInt32
*) prop
) );
355 regEntry
->setLocation( location
);
358 DTDisposePropertyIterator( dtIter
);
364 static void AddPHandle( IORegistryEntry
* regEntry
)
368 if( regEntry
->getProperty( gIODTInterruptCellKey
)
369 && (data
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTPHandleKey
)))) {
370 // a possible interrupt-parent
371 gIODTPHandles
->setObject( data
);
372 gIODTPHandleMap
->setObject( regEntry
);
376 static IORegistryEntry
* FindPHandle( UInt32 phandle
)
379 IORegistryEntry
*regEntry
= 0;
382 for( i
= 0; (data
= (OSData
*)gIODTPHandles
->getObject( i
)); i
++ ) {
383 if( phandle
== *((UInt32
*)data
->getBytesNoCopy())) {
384 regEntry
= (IORegistryEntry
*)
385 gIODTPHandleMap
->getObject( i
);
393 static bool GetUInt32( IORegistryEntry
* regEntry
, const OSSymbol
* name
,
398 if( (data
= OSDynamicCast( OSData
, regEntry
->getProperty( name
)))
399 && (4 == data
->getLength())) {
400 *value
= *((UInt32
*) data
->getBytesNoCopy());
406 IORegistryEntry
* IODTFindInterruptParent( IORegistryEntry
* regEntry
)
408 IORegistryEntry
* parent
;
411 if( GetUInt32( regEntry
, gIODTInterruptParentKey
, &phandle
))
412 parent
= FindPHandle( phandle
);
414 else if( 0 == regEntry
->getProperty( "interrupt-controller"))
415 parent
= regEntry
->getParentEntry( gIODTPlane
);
422 const OSSymbol
* IODTInterruptControllerName( IORegistryEntry
* regEntry
)
429 ok
= GetUInt32( regEntry
, gIODTPHandleKey
, &phandle
);
433 sprintf( buf
, "IOInterruptController%08lX", phandle
);
434 sym
= OSSymbol::withCString( buf
);
441 #define unexpected(a) { kprintf("unexpected %s:%d\n", __FILE__, __LINE__); a; }
443 static void IODTGetICellCounts( IORegistryEntry
* regEntry
,
444 UInt32
* iCellCount
, UInt32
* aCellCount
)
446 if( !GetUInt32( regEntry
, gIODTInterruptCellKey
, iCellCount
))
447 unexpected( *iCellCount
= 1 );
448 if( !GetUInt32( regEntry
, gIODTAddressCellKey
, aCellCount
))
452 UInt32
IODTMapOneInterrupt( IORegistryEntry
* regEntry
, UInt32
* intSpec
,
453 OSData
** spec
, const OSSymbol
** controller
)
455 IORegistryEntry
*parent
= 0;
461 UInt32 acells
, icells
, pacells
, picells
, cell
;
462 UInt32 i
, original_icells
;
463 bool cmp
, ok
= false;
465 parent
= IODTFindInterruptParent( regEntry
);
466 IODTGetICellCounts( parent
, &icells
, &acells
);
469 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "reg" ));
470 if( data
&& (data
->getLength() >= (acells
* sizeof( UInt32
))))
471 addrCmp
= (UInt32
*) data
->getBytesNoCopy();
473 original_icells
= icells
;
478 kprintf ("IODTMapOneInterrupt: current regEntry name %s\n", regEntry
->getName());
479 kprintf ("acells - icells: ");
480 for (i
= 0; i
< acells
; i
++) kprintf ("0x%08X ", addrCmp
[i
]);
482 for (i
= 0; i
< icells
; i
++) kprintf ("0x%08X ", intSpec
[i
]);
486 if( parent
&& (data
= OSDynamicCast( OSData
,
487 regEntry
->getProperty( "interrupt-controller")))) {
488 // found a controller - don't want to follow cascaded controllers
490 *spec
= OSData::withBytesNoCopy( (void *) intSpec
,
491 icells
* sizeof( UInt32
));
492 *controller
= IODTInterruptControllerName( regEntry
);
493 ok
= (*spec
&& *controller
);
494 } else if( parent
&& (data
= OSDynamicCast( OSData
,
495 regEntry
->getProperty( "interrupt-map")))) {
497 map
= (UInt32
*) data
->getBytesNoCopy();
498 endMap
= map
+ (data
->getLength() / sizeof(UInt32
));
499 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "interrupt-map-mask" ));
500 if( data
&& (data
->getLength() >= ((acells
+ icells
) * sizeof( UInt32
))))
501 maskCmp
= (UInt32
*) data
->getBytesNoCopy();
507 kprintf (" maskCmp: ");
508 for (i
= 0; i
< acells
+ icells
; i
++) {
511 kprintf ("0x%08X ", maskCmp
[i
]);
514 kprintf (" masked: ");
515 for (i
= 0; i
< acells
+ icells
; i
++) {
518 kprintf ("0x%08X ", ((i
< acells
) ? addrCmp
[i
] : intSpec
[i
-acells
]) & maskCmp
[i
]);
522 kprintf ("no maskCmp\n");
527 for (i
= 0; i
< acells
+ icells
; i
++) {
530 kprintf ("0x%08X ", map
[i
]);
534 for( i
= 0, cmp
= true; cmp
&& (i
< (acells
+ icells
)); i
++) {
535 cell
= (i
< acells
) ? addrCmp
[i
] : intSpec
[ i
- acells
];
538 cmp
= (cell
== map
[i
]);
541 map
+= acells
+ icells
;
542 if( 0 == (parent
= FindPHandle( *(map
++) )))
545 IODTGetICellCounts( parent
, &picells
, &pacells
);
548 intSpec
= map
+ pacells
;
551 map
+= pacells
+ picells
;
553 } while( !cmp
&& (map
< endMap
) );
559 IODTGetICellCounts( parent
, &icells
, &acells
);
565 return( ok
? original_icells
: 0 );
568 bool IODTMapInterrupts( IORegistryEntry
* regEntry
)
570 IORegistryEntry
*parent
;
577 const OSSymbol
*controller
;
578 OSArray
*controllers
;
582 nw
= (0 == (local
= OSDynamicCast( OSData
,
583 regEntry
->getProperty( gIODTAAPLInterruptsKey
))));
584 if( nw
&& (0 == (local
= OSDynamicCast( OSData
,
585 regEntry
->getProperty( "interrupts")))))
586 return( true ); // nothing to see here
588 if( nw
&& (parent
= regEntry
->getParentEntry( gIODTPlane
))) {
589 // check for bridges on old world
590 if( (local2
= OSDynamicCast( OSData
,
591 parent
->getProperty( gIODTAAPLInterruptsKey
)))) {
597 localBits
= (UInt32
*) local
->getBytesNoCopy();
598 localEnd
= localBits
+ (local
->getLength() / sizeof( UInt32
));
599 mapped
= OSArray::withCapacity( 1 );
600 controllers
= OSArray::withCapacity( 1 );
602 ok
= (mapped
&& controllers
);
606 skip
= IODTMapOneInterrupt( regEntry
, localBits
, &map
, &controller
);
608 IOLog("%s: error mapping interrupt[%d]\n",
609 regEntry
->getName(), mapped
->getCount());
613 map
= OSData::withData( local
, mapped
->getCount() * sizeof( UInt32
),
615 controller
= gIODTDefaultInterruptController
;
616 controller
->retain();
620 mapped
->setObject( map
);
622 controllers
->setObject( (OSObject
*) controller
);
623 controller
->release();
625 } while( localBits
< localEnd
);
627 ok
&= (localBits
== localEnd
);
631 ok
= regEntry
->setProperty( gIOInterruptControllersKey
, controllers
);
632 ok
&= regEntry
->setProperty( gIOInterruptSpecifiersKey
, mapped
);
636 controllers
->release();
647 CompareKey( OSString
* key
,
648 const IORegistryEntry
* table
, const OSSymbol
* propName
)
656 const char *lastName
;
659 const char *result
= 0;
661 if( 0 == (prop
= table
->getProperty( propName
)))
664 if( (data
= OSDynamicCast( OSData
, prop
))) {
665 names
= (const char *) data
->getBytesNoCopy();
666 lastName
= names
+ data
->getLength();
667 } else if( (string
= OSDynamicCast( OSString
, prop
))) {
668 names
= string
->getCStringNoCopy();
669 lastName
= names
+ string
->getLength() + 1;
673 ckey
= key
->getCStringNoCopy();
674 keyLen
= key
->getLength();
675 wild
= ('*' == key
->getChar( keyLen
- 1 ));
678 // for each name in the property
680 matched
= (0 == strncmp( ckey
, names
, keyLen
- 1 ));
682 matched
= (keyLen
== strlen( names
))
683 && (0 == strncmp( ckey
, names
, keyLen
));
688 names
= names
+ strlen( names
) + 1;
690 } while( (names
< lastName
) && (false == matched
));
696 bool IODTCompareNubName( const IORegistryEntry
* regEntry
,
697 OSString
* name
, OSString
** matchingName
)
702 matched
= (0 != (result
= CompareKey( name
, regEntry
, gIODTNameKey
)))
703 || (0 != (result
= CompareKey( name
, regEntry
, gIODTCompatibleKey
)))
704 || (0 != (result
= CompareKey( name
, regEntry
, gIODTTypeKey
)))
705 || (0 != (result
= CompareKey( name
, regEntry
, gIODTModelKey
)));
707 if( result
&& matchingName
)
708 *matchingName
= OSString::withCString( result
);
710 return( result
!= 0 );
713 bool IODTMatchNubWithKeys( IORegistryEntry
* regEntry
,
719 obj
= OSUnserialize( keys
, 0 );
722 result
= regEntry
->compareNames( obj
);
726 else IOLog("Couldn't unserialize %s\n", keys
);
732 OSCollectionIterator
* IODTFindMatchingEntries( IORegistryEntry
* from
,
733 IOOptionBits options
, const char * keys
)
736 IORegistryEntry
*next
;
737 IORegistryIterator
*iter
;
738 OSCollectionIterator
*cIter
;
740 bool minus
= options
& kIODTExclusive
;
743 iter
= IORegistryIterator::iterateOver( from
, gIODTPlane
,
744 (options
& kIODTRecursive
) ? kIORegistryIterateRecursively
: 0 );
751 result
= OSSet::withCapacity( 3 );
756 while( (next
= iter
->getNextObject())) {
758 // Look for existence of a debug property to skip
759 if( next
->getProperty("AAPL,ignore"))
763 cmp
= IODTMatchNubWithKeys( next
, keys
);
764 if( (minus
&& (false == cmp
))
765 || ((false == minus
) && (false != cmp
)) )
766 result
->setObject( next
);
768 result
->setObject( next
);
770 } while( !iter
->isValid());
775 cIter
= OSCollectionIterator::withCollection( result
);
782 struct IODTPersistent
{
783 IODTCompareAddressCellFunc compareFunc
;
784 IODTNVLocationFunc locationFunc
;
787 void IODTSetResolving( IORegistryEntry
* regEntry
,
788 IODTCompareAddressCellFunc compareFunc
,
789 IODTNVLocationFunc locationFunc
)
791 IODTPersistent persist
;
794 persist
.compareFunc
= compareFunc
;
795 persist
.locationFunc
= locationFunc
;
796 prop
= OSData::withBytes( &persist
, sizeof( persist
));
800 regEntry
->setProperty( gIODTPersistKey
, prop
);
805 static SInt32
DefaultCompare( UInt32 cellCount
, UInt32 left
[], UInt32 right
[] )
808 return( left
[ cellCount
] - right
[ cellCount
] );
812 void IODTGetCellCounts( IORegistryEntry
* regEntry
,
813 UInt32
* sizeCount
, UInt32
* addressCount
)
815 if( !GetUInt32( regEntry
, gIODTSizeCellKey
, sizeCount
))
817 if( !GetUInt32( regEntry
, gIODTAddressCellKey
, addressCount
))
822 // Given addr & len cells from our child, find it in our ranges property, then
823 // look in our parent to resolve the base of the range for us.
825 // Range[]: child-addr our-addr child-len
826 // #cells: child ours child
828 bool IODTResolveAddressCell( IORegistryEntry
* regEntry
,
830 IOPhysicalAddress
* phys
, IOPhysicalLength
* len
)
832 IORegistryEntry
*parent
;
834 // cells in addresses at regEntry
835 UInt32 sizeCells
, addressCells
;
836 // cells in addresses below regEntry
837 UInt32 childSizeCells
, childAddressCells
;
839 UInt32 cell
[ 5 ], offset
= 0, length
;
845 IODTPersistent
*persist
;
846 IODTCompareAddressCellFunc compare
;
848 IODTGetCellCounts( regEntry
, &childSizeCells
, &childAddressCells
);
849 childCells
= childAddressCells
+ childSizeCells
;
851 bcopy( cellsIn
, cell
, 4 * childCells
);
852 if( childSizeCells
> 1)
853 *len
= IOPhysical32( cellsIn
[ childAddressCells
],
854 cellsIn
[ childAddressCells
+ 1 ] );
856 *len
= IOPhysical32( 0, cellsIn
[ childAddressCells
] );
859 prop
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTRangeKey
));
861 /* end of the road */
862 *phys
= IOPhysical32( 0, cell
[ childAddressCells
- 1 ] + offset
);
866 parent
= regEntry
->getParentEntry( gIODTPlane
);
867 IODTGetCellCounts( parent
, &sizeCells
, &addressCells
);
869 if( (length
= prop
->getLength())) {
871 range
= (UInt32
*) prop
->getBytesNoCopy();
872 endRanges
= range
+ (length
/ 4);
874 prop
= (OSData
*) regEntry
->getProperty( gIODTPersistKey
);
876 persist
= (IODTPersistent
*) prop
->getBytesNoCopy();
877 compare
= persist
->compareFunc
;
879 compare
= DefaultCompare
;
883 range
+= (childCells
+ addressCells
) ) {
885 // is cell >= range start?
886 diff
= (*compare
)( childAddressCells
, cell
, range
);
890 // is cell + size <= range end?
891 if( (diff
+ cell
[ childCells
- 1 ])
892 > range
[ childCells
+ addressCells
- 1 ])
900 // Get the physical start of the range from our parent
901 bcopy( range
+ childAddressCells
, cell
, 4 * addressCells
);
902 bzero( cell
+ addressCells
, 4 * sizeCells
);
904 } /* else zero length range => pass thru to parent */
907 childSizeCells
= sizeCells
;
908 childAddressCells
= addressCells
;
909 childCells
= childAddressCells
+ childSizeCells
;
911 } while( ok
&& regEntry
);
917 OSArray
* IODTResolveAddressing( IORegistryEntry
* regEntry
,
918 const char * addressPropertyName
,
919 IODeviceMemory
* parent
)
921 IORegistryEntry
*parentEntry
;
922 OSData
*addressProperty
;
923 UInt32 sizeCells
, addressCells
, cells
;
926 IOPhysicalAddress phys
;
927 IOPhysicalLength len
;
929 IODeviceMemory
*range
;
931 parentEntry
= regEntry
->getParentEntry( gIODTPlane
);
932 addressProperty
= (OSData
*) regEntry
->getProperty( addressPropertyName
);
933 if( (0 == addressProperty
) || (0 == parentEntry
))
936 IODTGetCellCounts( parentEntry
, &sizeCells
, &addressCells
);
940 cells
= sizeCells
+ addressCells
;
941 reg
= (UInt32
*) addressProperty
->getBytesNoCopy();
942 num
= addressProperty
->getLength() / (4 * cells
);
944 array
= OSArray::withCapacity( 1 );
948 for( i
= 0; i
< num
; i
++) {
949 if( IODTResolveAddressCell( parentEntry
, reg
, &phys
, &len
)) {
952 range
= IODeviceMemory::withSubRange( parent
,
953 phys
- parent
->getPhysicalAddress(), len
);
955 range
= IODeviceMemory::withRange( phys
, len
);
957 array
->setObject( range
);
962 regEntry
->setProperty( gIODeviceMemoryKey
, array
);
963 array
->release(); /* ??? */
968 static void IODTGetNVLocation(
969 IORegistryEntry
* parent
,
970 IORegistryEntry
* regEntry
,
971 UInt8
* busNum
, UInt8
* deviceNum
, UInt8
* functionNum
)
975 IODTPersistent
*persist
;
978 prop
= (OSData
*) parent
->getProperty( gIODTPersistKey
);
980 persist
= (IODTPersistent
*) prop
->getBytesNoCopy();
981 (*persist
->locationFunc
)( regEntry
, busNum
, deviceNum
, functionNum
);
983 prop
= (OSData
*) regEntry
->getProperty( "reg" );
986 cell
= (UInt32
*) prop
->getBytesNoCopy();
988 *deviceNum
= 0x1f & (cell
[ 0 ] >> 24);
998 * Try to make the same messed up descriptor as Mac OS
1001 IOReturn
IODTMakeNVDescriptor( IORegistryEntry
* regEntry
,
1002 IONVRAMDescriptor
* hdr
)
1004 IORegistryEntry
*parent
;
1006 UInt32 bridgeDevices
;
1014 for(level
= 0, bridgeDevices
= 0;
1015 (parent
= regEntry
->getParentEntry( gIODTPlane
)) && (level
< 7); level
++ ) {
1017 IODTGetNVLocation( parent
, regEntry
,
1018 &busNum
, &deviceNum
, &functionNum
);
1020 bridgeDevices
|= ((deviceNum
& 0x1f) << ((level
- 1) * 5));
1022 hdr
->busNum
= busNum
;
1023 hdr
->deviceNum
= deviceNum
;
1024 hdr
->functionNum
= functionNum
;
1028 hdr
->bridgeCount
= level
- 2;
1029 hdr
->bridgeDevices
= bridgeDevices
;
1031 return( kIOReturnSuccess
);
1034 OSData
* IODTFindSlotName( IORegistryEntry
* regEntry
, UInt32 deviceNumber
)
1036 IORegistryEntry
*parent
;
1045 data
= (OSData
*) regEntry
->getProperty("AAPL,slot-name");
1048 parent
= regEntry
->getParentEntry( gIODTPlane
);
1051 data
= OSDynamicCast( OSData
, parent
->getProperty("slot-names"));
1054 if( data
->getLength() <= 4)
1057 bits
= (UInt32
*) data
->getBytesNoCopy();
1059 if( (0 == (mask
& (1 << deviceNumber
))))
1062 names
= (char *)(bits
+ 1);
1063 lastName
= names
+ (data
->getLength() - 4);
1065 for( i
= 0; (i
<= deviceNumber
) && (names
< lastName
); i
++ ) {
1067 if( mask
& (1 << i
)) {
1068 if( i
== deviceNumber
) {
1069 data
= OSData::withBytesNoCopy( names
, 1 + strlen( names
));
1071 regEntry
->setProperty("AAPL,slot-name", data
);
1076 names
+= 1 + strlen( names
);