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 <DeviceTree.h>
42 #include <machine/machine_routines.h>
43 void DTInit( void * data
);
45 int IODTGetLoaderInfo( char *key
, void **infoAddr
, int *infosize
);
46 void IODTFreeLoaderInfo( char *key
, void *infoAddr
, int infoSize
);
49 #include <IOKit/assert.h>
51 #define IODTSUPPORTDEBUG 0
53 const IORegistryPlane
* gIODTPlane
;
55 static OSArray
* gIODTPHandles
;
56 static OSArray
* gIODTPHandleMap
;
58 const OSSymbol
* gIODTNameKey
;
59 const OSSymbol
* gIODTUnitKey
;
60 const OSSymbol
* gIODTCompatibleKey
;
61 const OSSymbol
* gIODTTypeKey
;
62 const OSSymbol
* gIODTModelKey
;
64 const OSSymbol
* gIODTSizeCellKey
;
65 const OSSymbol
* gIODTAddressCellKey
;
66 const OSSymbol
* gIODTRangeKey
;
68 const OSSymbol
* gIODTPersistKey
;
70 const OSSymbol
* gIODTDefaultInterruptController
;
71 const OSSymbol
* gIODTAAPLInterruptsKey
;
72 const OSSymbol
* gIODTPHandleKey
;
73 const OSSymbol
* gIODTInterruptCellKey
;
74 const OSSymbol
* gIODTInterruptParentKey
;
75 const OSSymbol
* gIODTNWInterruptMappingKey
;
78 static IORegistryEntry
* MakeReferenceTable( DTEntry dtEntry
, bool copy
);
79 static void AddPHandle( IORegistryEntry
* regEntry
);
80 static void FreePhysicalMemory( vm_offset_t
* range
);
83 IODeviceTreeAlloc( void * dtTop
)
85 IORegistryEntry
*parent
;
86 IORegistryEntry
*child
;
87 IORegistryIterator
*regIter
;
99 IOLog("IODeviceTreeSupport ");
101 gIODTPlane
= IORegistryEntry::makePlane( kIODeviceTreePlane
);
103 gIODTNameKey
= OSSymbol::withCStringNoCopy( "name" );
104 gIODTUnitKey
= OSSymbol::withCStringNoCopy( "AAPL,unit-string" );
105 gIODTCompatibleKey
= OSSymbol::withCStringNoCopy( "compatible" );
106 gIODTTypeKey
= OSSymbol::withCStringNoCopy( "device_type" );
107 gIODTModelKey
= OSSymbol::withCStringNoCopy( "model" );
108 gIODTSizeCellKey
= OSSymbol::withCStringNoCopy( "#size-cells" );
109 gIODTAddressCellKey
= OSSymbol::withCStringNoCopy( "#address-cells" );
110 gIODTRangeKey
= OSSymbol::withCStringNoCopy( "ranges" );
111 gIODTPersistKey
= OSSymbol::withCStringNoCopy( "IODTPersist" );
113 assert( gIODTPlane
&& gIODTCompatibleKey
114 && gIODTTypeKey
&& gIODTModelKey
115 && gIODTSizeCellKey
&& gIODTAddressCellKey
&& gIODTRangeKey
116 && gIODTPersistKey
);
118 gIODTDefaultInterruptController
119 = OSSymbol::withCStringNoCopy("IOPrimaryInterruptController");
120 gIODTNWInterruptMappingKey
121 = OSSymbol::withCStringNoCopy("IONWInterrupts");
123 gIODTAAPLInterruptsKey
124 = OSSymbol::withCStringNoCopy("AAPL,interrupts");
126 = OSSymbol::withCStringNoCopy("AAPL,phandle");
128 gIODTInterruptParentKey
129 = OSSymbol::withCStringNoCopy("interrupt-parent");
131 gIODTPHandles
= OSArray::withCapacity( 1 );
132 gIODTPHandleMap
= OSArray::withCapacity( 1 );
134 gIODTInterruptCellKey
135 = OSSymbol::withCStringNoCopy("#interrupt-cells");
137 assert( gIODTDefaultInterruptController
&& gIODTNWInterruptMappingKey
138 && gIODTAAPLInterruptsKey
139 && gIODTPHandleKey
&& gIODTInterruptParentKey
140 && gIODTPHandles
&& gIODTPHandleMap
141 && gIODTInterruptCellKey
144 freeDT
= (kSuccess
== DTLookupEntry( 0, "/chosen/memory-map", &mapEntry
))
145 && (kSuccess
== DTGetProperty( mapEntry
,
146 "DeviceTree", (void **) &dtMap
, &propSize
))
147 && ((2 * sizeof( vm_offset_t
)) == propSize
);
149 parent
= MakeReferenceTable( (DTEntry
)dtTop
, freeDT
);
151 stack
= OSArray::withObjects( & (const OSObject
*) parent
, 1, 10 );
152 DTCreateEntryIterator( (DTEntry
)dtTop
, &iter
);
155 parent
= (IORegistryEntry
*)stack
->getObject( stack
->getCount() - 1);
157 stack
->removeObject( stack
->getCount() - 1);
159 while( kSuccess
== DTIterateEntries( iter
, &dtChild
) ) {
161 child
= MakeReferenceTable( dtChild
, freeDT
);
162 child
->attachToParent( parent
, gIODTPlane
);
166 if( kSuccess
== DTEnterEntry( iter
, dtChild
)) {
167 stack
->setObject( parent
);
170 // only registry holds retain
174 } while( stack
->getCount()
175 && (kSuccess
== DTExitEntry( iter
, &dtChild
)));
178 DTDisposeEntryIterator( iter
);
180 // parent is now root of the created tree
182 // make root name first compatible entry (purely cosmetic)
183 if( (prop
= (OSData
*) parent
->getProperty( gIODTCompatibleKey
))) {
184 parent
->setName( parent
->getName(), gIODTPlane
);
185 parent
->setName( (const char *) prop
->getBytesNoCopy() );
188 // attach tree to meta root
189 parent
->attachToParent( IORegistryEntry::getRegistryRoot(), gIODTPlane
);
193 // free original device tree
195 IODTFreeLoaderInfo( "DeviceTree",
196 (void *)dtMap
[0], round_page(dtMap
[1]) );
201 regIter
= IORegistryIterator::iterateOver( gIODTPlane
,
202 kIORegistryIterateRecursively
);
205 while( (child
= regIter
->getNextObject())) {
206 IODTMapInterrupts( child
);
207 if( !intMap
&& child
->getProperty( gIODTInterruptParentKey
))
210 // Look for a "driver,AAPL,MacOSX,PowerPC" property.
211 if( (obj
= child
->getProperty( "driver,AAPL,MacOSX,PowerPC"))) {
212 gIOCatalogue
->addExtensionsFromArchive((OSData
*)obj
);
214 child
->removeProperty( "driver,AAPL,MacOSX,PowerPC");
217 // some gross pruning
218 child
->removeProperty( "lanLib,AAPL,MacOS,PowerPC");
220 if( (obj
= child
->getProperty( "driver,AAPL,MacOS,PowerPC"))) {
222 if( (0 == (prop
= (OSData
*)child
->getProperty( gIODTTypeKey
)))
223 || (strcmp( "display", (char *) prop
->getBytesNoCopy())) ) {
224 child
->removeProperty( "driver,AAPL,MacOS,PowerPC");
232 // set a key in the root to indicate we found NW interrupt mapping
233 parent
->setProperty( gIODTNWInterruptMappingKey
,
234 (OSObject
*) gIODTNWInterruptMappingKey
);
241 int IODTGetLoaderInfo( char *key
, void **infoAddr
, int *infoSize
)
243 IORegistryEntry
*chosen
;
245 unsigned int *propPtr
;
246 unsigned int propSize
;
248 chosen
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
);
249 if ( chosen
== 0 ) return -1;
251 propObj
= OSDynamicCast( OSData
, chosen
->getProperty(key
) );
252 if ( propObj
== 0 ) return -1;
254 propSize
= propObj
->getLength();
255 if ( propSize
!= (2 * sizeof(UInt32
)) ) return -1;
257 propPtr
= (unsigned int *)propObj
->getBytesNoCopy();
258 if ( propPtr
== 0 ) return -1;
260 *infoAddr
= (void *)propPtr
[0] ;
261 *infoSize
= (int) propPtr
[1];
266 void IODTFreeLoaderInfo( char *key
, void *infoAddr
, int infoSize
)
268 vm_offset_t range
[2];
269 IORegistryEntry
*chosen
;
271 range
[0] = (vm_offset_t
)infoAddr
;
272 range
[1] = (vm_offset_t
)infoSize
;
273 FreePhysicalMemory( range
);
276 chosen
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
);
278 chosen
->removeProperty(key
);
283 static void FreePhysicalMemory( vm_offset_t
* range
)
287 virt
= ml_static_ptovirt( range
[0] );
289 ml_static_mfree( virt
, range
[1] );
293 static IORegistryEntry
*
294 MakeReferenceTable( DTEntry dtEntry
, bool copy
)
296 IORegistryEntry
*regEntry
;
297 OSDictionary
*propTable
;
298 const OSSymbol
*nameKey
;
301 DTPropertyIterator dtIter
;
306 bool noLocation
= true;
308 regEntry
= new IOService
;
310 if( regEntry
&& (false == regEntry
->init())) {
316 (kSuccess
== DTCreatePropertyIterator( dtEntry
, &dtIter
))) {
318 propTable
= regEntry
->getPropertyTable();
320 while( kSuccess
== DTIterateProperties( dtIter
, &name
)) {
322 if( kSuccess
!= DTGetProperty( dtEntry
, name
, &prop
, &propSize
))
326 nameKey
= OSSymbol::withCString(name
);
327 data
= OSData::withBytes(prop
, propSize
);
329 nameKey
= OSSymbol::withCStringNoCopy(name
);
330 data
= OSData::withBytesNoCopy(prop
, propSize
);
332 assert( nameKey
&& data
);
334 propTable
->setObject( nameKey
, data
);
338 if( nameKey
== gIODTNameKey
) {
340 sym
= OSSymbol::withCString( (const char *) prop
);
342 sym
= OSSymbol::withCStringNoCopy( (const char *) prop
);
343 regEntry
->setName( sym
);
345 } else if( nameKey
== gIODTUnitKey
) {
346 // all OF strings are null terminated... except this one
347 if( propSize
>= (int) sizeof( location
))
348 propSize
= sizeof( location
) - 1;
349 strncpy( location
, (const char *) prop
, propSize
);
350 location
[ propSize
] = 0;
351 regEntry
->setLocation( location
);
352 propTable
->removeObject( gIODTUnitKey
);
355 } else if( noLocation
&& (0 == strcmp( name
, "reg"))) {
356 // default location - override later
357 sprintf( location
, "%lX", *((UInt32
*) prop
) );
358 regEntry
->setLocation( location
);
361 DTDisposePropertyIterator( dtIter
);
367 static void AddPHandle( IORegistryEntry
* regEntry
)
371 if( regEntry
->getProperty( gIODTInterruptCellKey
)
372 && (data
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTPHandleKey
)))) {
373 // a possible interrupt-parent
374 gIODTPHandles
->setObject( data
);
375 gIODTPHandleMap
->setObject( regEntry
);
379 static IORegistryEntry
* FindPHandle( UInt32 phandle
)
382 IORegistryEntry
*regEntry
= 0;
385 for( i
= 0; (data
= (OSData
*)gIODTPHandles
->getObject( i
)); i
++ ) {
386 if( phandle
== *((UInt32
*)data
->getBytesNoCopy())) {
387 regEntry
= (IORegistryEntry
*)
388 gIODTPHandleMap
->getObject( i
);
396 static bool GetUInt32( IORegistryEntry
* regEntry
, const OSSymbol
* name
,
401 if( (data
= OSDynamicCast( OSData
, regEntry
->getProperty( name
)))
402 && (4 == data
->getLength())) {
403 *value
= *((UInt32
*) data
->getBytesNoCopy());
409 IORegistryEntry
* IODTFindInterruptParent( IORegistryEntry
* regEntry
)
411 IORegistryEntry
* parent
;
414 if( GetUInt32( regEntry
, gIODTInterruptParentKey
, &phandle
))
415 parent
= FindPHandle( phandle
);
417 else if( 0 == regEntry
->getProperty( "interrupt-controller"))
418 parent
= regEntry
->getParentEntry( gIODTPlane
);
425 const OSSymbol
* IODTInterruptControllerName( IORegistryEntry
* regEntry
)
432 ok
= GetUInt32( regEntry
, gIODTPHandleKey
, &phandle
);
436 sprintf( buf
, "IOInterruptController%08lX", phandle
);
437 sym
= OSSymbol::withCString( buf
);
444 #define unexpected(a) { kprintf("unexpected %s:%d\n", __FILE__, __LINE__); a; }
446 static void IODTGetICellCounts( IORegistryEntry
* regEntry
,
447 UInt32
* iCellCount
, UInt32
* aCellCount
)
449 if( !GetUInt32( regEntry
, gIODTInterruptCellKey
, iCellCount
))
450 unexpected( *iCellCount
= 1 );
451 if( !GetUInt32( regEntry
, gIODTAddressCellKey
, aCellCount
))
455 UInt32
IODTMapOneInterrupt( IORegistryEntry
* regEntry
, UInt32
* intSpec
,
456 OSData
** spec
, const OSSymbol
** controller
)
458 IORegistryEntry
*parent
= 0;
464 UInt32 acells
, icells
, pacells
, picells
, cell
;
465 UInt32 i
, original_icells
;
466 bool cmp
, ok
= false;
469 parent
= IODTFindInterruptParent( regEntry
);
470 IODTGetICellCounts( parent
, &icells
, &acells
);
473 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "reg" ));
474 if( data
&& (data
->getLength() >= (acells
* sizeof( UInt32
))))
475 addrCmp
= (UInt32
*) data
->getBytesNoCopy();
477 original_icells
= icells
;
482 kprintf ("IODTMapOneInterrupt: current regEntry name %s\n", regEntry
->getName());
483 kprintf ("acells - icells: ");
484 for (i
= 0; i
< acells
; i
++) kprintf ("0x%08X ", addrCmp
[i
]);
486 for (i
= 0; i
< icells
; i
++) kprintf ("0x%08X ", intSpec
[i
]);
490 if( parent
&& (data
= OSDynamicCast( OSData
,
491 regEntry
->getProperty( "interrupt-controller")))) {
492 // found a controller - don't want to follow cascaded controllers
494 *spec
= OSData::withBytesNoCopy( (void *) intSpec
,
495 icells
* sizeof( UInt32
));
496 *controller
= IODTInterruptControllerName( regEntry
);
497 ok
= (*spec
&& *controller
);
498 } else if( parent
&& (data
= OSDynamicCast( OSData
,
499 regEntry
->getProperty( "interrupt-map")))) {
501 map
= (UInt32
*) data
->getBytesNoCopy();
502 endMap
= map
+ (data
->getLength() / sizeof(UInt32
));
503 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "interrupt-map-mask" ));
504 if( data
&& (data
->getLength() >= ((acells
+ icells
) * sizeof( UInt32
))))
505 maskCmp
= (UInt32
*) data
->getBytesNoCopy();
511 kprintf (" maskCmp: ");
512 for (i
= 0; i
< acells
+ icells
; i
++) {
515 kprintf ("0x%08X ", maskCmp
[i
]);
518 kprintf (" masked: ");
519 for (i
= 0; i
< acells
+ icells
; i
++) {
522 kprintf ("0x%08X ", ((i
< acells
) ? addrCmp
[i
] : intSpec
[i
-acells
]) & maskCmp
[i
]);
526 kprintf ("no maskCmp\n");
531 for (i
= 0; i
< acells
+ icells
; i
++) {
534 kprintf ("0x%08X ", map
[i
]);
538 for( i
= 0, cmp
= true; cmp
&& (i
< (acells
+ icells
)); i
++) {
539 cell
= (i
< acells
) ? addrCmp
[i
] : intSpec
[ i
- acells
];
542 cmp
= (cell
== map
[i
]);
545 map
+= acells
+ icells
;
546 if( 0 == (parent
= FindPHandle( *(map
++) )))
549 IODTGetICellCounts( parent
, &picells
, &pacells
);
552 intSpec
= map
+ pacells
;
555 map
+= pacells
+ picells
;
557 } while( !cmp
&& (map
< endMap
) );
563 IODTGetICellCounts( parent
, &icells
, &acells
);
568 return( ok
? original_icells
: 0 );
571 bool IODTMapInterrupts( IORegistryEntry
* regEntry
)
573 IORegistryEntry
*parent
;
580 const OSSymbol
*controller
;
581 OSArray
*controllers
;
585 nw
= (0 == (local
= OSDynamicCast( OSData
,
586 regEntry
->getProperty( gIODTAAPLInterruptsKey
))));
587 if( nw
&& (0 == (local
= OSDynamicCast( OSData
,
588 regEntry
->getProperty( "interrupts")))))
589 return( true ); // nothing to see here
591 if( nw
&& (parent
= regEntry
->getParentEntry( gIODTPlane
))) {
592 // check for bridges on old world
593 if( (local2
= OSDynamicCast( OSData
,
594 parent
->getProperty( gIODTAAPLInterruptsKey
)))) {
600 localBits
= (UInt32
*) local
->getBytesNoCopy();
601 localEnd
= localBits
+ (local
->getLength() / sizeof( UInt32
));
602 mapped
= OSArray::withCapacity( 1 );
603 controllers
= OSArray::withCapacity( 1 );
605 ok
= (mapped
&& controllers
);
609 skip
= IODTMapOneInterrupt( regEntry
, localBits
, &map
, &controller
);
611 IOLog("%s: error mapping interrupt[%d]\n",
612 regEntry
->getName(), mapped
->getCount());
616 map
= OSData::withData( local
, mapped
->getCount() * sizeof( UInt32
),
618 controller
= gIODTDefaultInterruptController
;
619 controller
->retain();
623 mapped
->setObject( map
);
625 controllers
->setObject( (OSObject
*) controller
);
626 controller
->release();
628 } while( localBits
< localEnd
);
630 ok
&= (localBits
== localEnd
);
634 ok
= regEntry
->setProperty( gIOInterruptControllersKey
, controllers
);
635 ok
&= regEntry
->setProperty( gIOInterruptSpecifiersKey
, mapped
);
639 controllers
->release();
650 CompareKey( OSString
* key
,
651 const IORegistryEntry
* table
, const OSSymbol
* propName
)
659 const char *lastName
;
662 const char *result
= 0;
664 if( 0 == (prop
= table
->getProperty( propName
)))
667 if( (data
= OSDynamicCast( OSData
, prop
))) {
668 names
= (const char *) data
->getBytesNoCopy();
669 lastName
= names
+ data
->getLength();
670 } else if( (string
= OSDynamicCast( OSString
, prop
))) {
671 names
= string
->getCStringNoCopy();
672 lastName
= names
+ string
->getLength() + 1;
676 ckey
= key
->getCStringNoCopy();
677 keyLen
= key
->getLength();
678 wild
= ('*' == key
->getChar( keyLen
- 1 ));
681 // for each name in the property
683 matched
= (0 == strncmp( ckey
, names
, keyLen
- 1 ));
685 matched
= (keyLen
== strlen( names
))
686 && (0 == strncmp( ckey
, names
, keyLen
));
691 names
= names
+ strlen( names
) + 1;
693 } while( (names
< lastName
) && (false == matched
));
699 bool IODTCompareNubName( const IORegistryEntry
* regEntry
,
700 OSString
* name
, OSString
** matchingName
)
705 matched
= (0 != (result
= CompareKey( name
, regEntry
, gIODTNameKey
)))
706 || (0 != (result
= CompareKey( name
, regEntry
, gIODTCompatibleKey
)))
707 || (0 != (result
= CompareKey( name
, regEntry
, gIODTTypeKey
)))
708 || (0 != (result
= CompareKey( name
, regEntry
, gIODTModelKey
)));
710 if( result
&& matchingName
)
711 *matchingName
= OSString::withCString( result
);
713 return( result
!= 0 );
716 bool IODTMatchNubWithKeys( IORegistryEntry
* regEntry
,
722 obj
= OSUnserialize( keys
, 0 );
725 result
= regEntry
->compareNames( obj
);
729 else IOLog("Couldn't unserialize %s\n", keys
);
735 OSCollectionIterator
* IODTFindMatchingEntries( IORegistryEntry
* from
,
736 IOOptionBits options
, const char * keys
)
739 IORegistryEntry
*next
;
740 IORegistryIterator
*iter
;
741 OSCollectionIterator
*cIter
;
743 bool minus
= options
& kIODTExclusive
;
746 iter
= IORegistryIterator::iterateOver( from
, gIODTPlane
,
747 (options
& kIODTRecursive
) ? kIORegistryIterateRecursively
: 0 );
753 result
= OSSet::withCapacity( 3 );
758 while( (next
= iter
->getNextObject())) {
760 // Look for existence of a debug property to skip
761 if( next
->getProperty("AAPL,ignore"))
765 cmp
= IODTMatchNubWithKeys( next
, keys
);
766 if( (minus
&& (false == cmp
))
767 || ((false == minus
) && (false != cmp
)) )
768 result
->setObject( next
);
770 result
->setObject( next
);
772 } while( !iter
->isValid());
776 cIter
= OSCollectionIterator::withCollection( result
);
783 struct IODTPersistent
{
784 IODTCompareAddressCellFunc compareFunc
;
785 IODTNVLocationFunc locationFunc
;
788 void IODTSetResolving( IORegistryEntry
* regEntry
,
789 IODTCompareAddressCellFunc compareFunc
,
790 IODTNVLocationFunc locationFunc
)
792 IODTPersistent persist
;
795 persist
.compareFunc
= compareFunc
;
796 persist
.locationFunc
= locationFunc
;
797 prop
= OSData::withBytes( &persist
, sizeof( persist
));
801 regEntry
->setProperty( gIODTPersistKey
, prop
);
806 static SInt32
DefaultCompare( UInt32 cellCount
, UInt32 left
[], UInt32 right
[] )
809 return( left
[ cellCount
] - right
[ cellCount
] );
813 void IODTGetCellCounts( IORegistryEntry
* regEntry
,
814 UInt32
* sizeCount
, UInt32
* addressCount
)
816 if( !GetUInt32( regEntry
, gIODTSizeCellKey
, sizeCount
))
818 if( !GetUInt32( regEntry
, gIODTAddressCellKey
, addressCount
))
823 // Given addr & len cells from our child, find it in our ranges property, then
824 // look in our parent to resolve the base of the range for us.
826 // Range[]: child-addr our-addr child-len
827 // #cells: child ours child
829 bool IODTResolveAddressCell( IORegistryEntry
* regEntry
,
831 IOPhysicalAddress
* phys
, IOPhysicalLength
* len
)
833 IORegistryEntry
*parent
;
835 // cells in addresses at regEntry
836 UInt32 sizeCells
, addressCells
;
837 // cells in addresses below regEntry
838 UInt32 childSizeCells
, childAddressCells
;
840 UInt32 cell
[ 5 ], offset
= 0, length
;
846 IODTPersistent
*persist
;
847 IODTCompareAddressCellFunc compare
;
849 IODTGetCellCounts( regEntry
, &childSizeCells
, &childAddressCells
);
850 childCells
= childAddressCells
+ childSizeCells
;
852 bcopy( cellsIn
, cell
, 4 * childCells
);
853 if( childSizeCells
> 1)
854 *len
= IOPhysical32( cellsIn
[ childAddressCells
],
855 cellsIn
[ childAddressCells
+ 1 ] );
857 *len
= IOPhysical32( 0, cellsIn
[ childAddressCells
] );
860 prop
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTRangeKey
));
862 /* end of the road */
863 *phys
= IOPhysical32( 0, cell
[ childAddressCells
- 1 ] + offset
);
867 parent
= regEntry
->getParentEntry( gIODTPlane
);
868 IODTGetCellCounts( parent
, &sizeCells
, &addressCells
);
870 if( (length
= prop
->getLength())) {
872 range
= (UInt32
*) prop
->getBytesNoCopy();
873 endRanges
= range
+ (length
/ 4);
875 prop
= (OSData
*) regEntry
->getProperty( gIODTPersistKey
);
877 persist
= (IODTPersistent
*) prop
->getBytesNoCopy();
878 compare
= persist
->compareFunc
;
880 compare
= DefaultCompare
;
884 range
+= (childCells
+ addressCells
) ) {
886 // is cell >= range start?
887 diff
= (*compare
)( childAddressCells
, cell
, range
);
891 // is cell + size <= range end?
892 if( (diff
+ cell
[ childCells
- 1 ])
893 > range
[ childCells
+ addressCells
- 1 ])
901 // Get the physical start of the range from our parent
902 bcopy( range
+ childAddressCells
, cell
, 4 * addressCells
);
903 bzero( cell
+ addressCells
, 4 * sizeCells
);
905 } /* else zero length range => pass thru to parent */
908 childSizeCells
= sizeCells
;
909 childAddressCells
= addressCells
;
910 childCells
= childAddressCells
+ childSizeCells
;
912 } while( ok
&& regEntry
);
918 OSArray
* IODTResolveAddressing( IORegistryEntry
* regEntry
,
919 const char * addressPropertyName
,
920 IODeviceMemory
* parent
)
922 IORegistryEntry
*parentEntry
;
923 OSData
*addressProperty
;
924 UInt32 sizeCells
, addressCells
, cells
;
927 IOPhysicalAddress phys
;
928 IOPhysicalLength len
;
930 IODeviceMemory
*range
;
932 parentEntry
= regEntry
->getParentEntry( gIODTPlane
);
933 addressProperty
= (OSData
*) regEntry
->getProperty( addressPropertyName
);
934 if( (0 == addressProperty
) || (0 == parentEntry
))
937 IODTGetCellCounts( parentEntry
, &sizeCells
, &addressCells
);
941 cells
= sizeCells
+ addressCells
;
942 reg
= (UInt32
*) addressProperty
->getBytesNoCopy();
943 num
= addressProperty
->getLength() / (4 * cells
);
945 array
= OSArray::withCapacity( 1 );
949 for( i
= 0; i
< num
; i
++) {
950 if( IODTResolveAddressCell( parentEntry
, reg
, &phys
, &len
)) {
953 range
= IODeviceMemory::withSubRange( parent
,
954 phys
- parent
->getPhysicalAddress(), len
);
956 range
= IODeviceMemory::withRange( phys
, len
);
958 array
->setObject( range
);
963 regEntry
->setProperty( gIODeviceMemoryKey
, array
);
964 array
->release(); /* ??? */
969 static void IODTGetNVLocation(
970 IORegistryEntry
* parent
,
971 IORegistryEntry
* regEntry
,
972 UInt8
* busNum
, UInt8
* deviceNum
, UInt8
* functionNum
)
976 IODTPersistent
*persist
;
979 prop
= (OSData
*) parent
->getProperty( gIODTPersistKey
);
981 persist
= (IODTPersistent
*) prop
->getBytesNoCopy();
982 (*persist
->locationFunc
)( regEntry
, busNum
, deviceNum
, functionNum
);
984 prop
= (OSData
*) regEntry
->getProperty( "reg" );
987 cell
= (UInt32
*) prop
->getBytesNoCopy();
989 *deviceNum
= 0x1f & (cell
[ 0 ] >> 24);
999 * Try to make the same messed up descriptor as Mac OS
1002 IOReturn
IODTMakeNVDescriptor( IORegistryEntry
* regEntry
,
1003 IONVRAMDescriptor
* hdr
)
1005 IORegistryEntry
*parent
;
1007 UInt32 bridgeDevices
;
1015 for(level
= 0, bridgeDevices
= 0;
1016 (parent
= regEntry
->getParentEntry( gIODTPlane
)) && (level
< 7); level
++ ) {
1018 IODTGetNVLocation( parent
, regEntry
,
1019 &busNum
, &deviceNum
, &functionNum
);
1021 bridgeDevices
|= ((deviceNum
& 0x1f) << ((level
- 1) * 5));
1023 hdr
->busNum
= busNum
;
1024 hdr
->deviceNum
= deviceNum
;
1025 hdr
->functionNum
= functionNum
;
1029 hdr
->bridgeCount
= level
- 2;
1030 hdr
->bridgeDevices
= bridgeDevices
;
1032 return( kIOReturnSuccess
);
1035 OSData
* IODTFindSlotName( IORegistryEntry
* regEntry
, UInt32 deviceNumber
)
1037 IORegistryEntry
*parent
;
1046 data
= (OSData
*) regEntry
->getProperty("AAPL,slot-name");
1049 parent
= regEntry
->getParentEntry( gIODTPlane
);
1052 data
= OSDynamicCast( OSData
, parent
->getProperty("slot-names"));
1055 if( data
->getLength() <= 4)
1058 bits
= (UInt32
*) data
->getBytesNoCopy();
1060 if( (0 == (mask
& (1 << deviceNumber
))))
1063 names
= (char *)(bits
+ 1);
1064 lastName
= names
+ (data
->getLength() - 4);
1066 for( i
= 0; (i
<= deviceNumber
) && (names
< lastName
); i
++ ) {
1068 if( mask
& (1 << i
)) {
1069 if( i
== deviceNumber
) {
1070 data
= OSData::withBytesNoCopy( names
, 1 + strlen( names
));
1072 regEntry
->setProperty("AAPL,slot-name", data
);
1077 names
+= 1 + strlen( names
);