2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
29 * 23 Nov 98 sdouglas, created from IODeviceTreeBus.m, & MacOS exp mgr.
30 * 05 Apr 99 sdouglas, add interrupt mapping.
34 #include <IOKit/IODeviceTreeSupport.h>
35 #include <libkern/c++/OSContainers.h>
36 #include <IOKit/IODeviceMemory.h>
37 #include <IOKit/IOService.h>
38 #include <IOKit/IOCatalogue.h>
40 #include <IOKit/IOLib.h>
41 #include <IOKit/IOKitKeys.h>
43 #include <pexpert/device_tree.h>
46 #include <machine/machine_routines.h>
47 void DTInit( void * data
);
49 int IODTGetLoaderInfo( char *key
, void **infoAddr
, int *infosize
);
50 void IODTFreeLoaderInfo( char *key
, void *infoAddr
, int infoSize
);
53 #include <IOKit/assert.h>
55 #define IODTSUPPORTDEBUG 0
57 const IORegistryPlane
* gIODTPlane
;
59 static OSArray
* gIODTPHandles
;
60 static OSArray
* gIODTPHandleMap
;
62 const OSSymbol
* gIODTNameKey
;
63 const OSSymbol
* gIODTUnitKey
;
64 const OSSymbol
* gIODTCompatibleKey
;
65 const OSSymbol
* gIODTTypeKey
;
66 const OSSymbol
* gIODTModelKey
;
68 const OSSymbol
* gIODTSizeCellKey
;
69 const OSSymbol
* gIODTAddressCellKey
;
70 const OSSymbol
* gIODTRangeKey
;
72 const OSSymbol
* gIODTPersistKey
;
74 const OSSymbol
* gIODTDefaultInterruptController
;
75 const OSSymbol
* gIODTAAPLInterruptsKey
;
76 const OSSymbol
* gIODTPHandleKey
;
77 const OSSymbol
* gIODTInterruptCellKey
;
78 const OSSymbol
* gIODTInterruptParentKey
;
79 const OSSymbol
* gIODTNWInterruptMappingKey
;
82 static IORegistryEntry
* MakeReferenceTable( DTEntry dtEntry
, bool copy
);
83 static void AddPHandle( IORegistryEntry
* regEntry
);
84 static void FreePhysicalMemory( vm_offset_t
* range
);
87 IODeviceTreeAlloc( void * dtTop
)
89 IORegistryEntry
*parent
;
90 IORegistryEntry
*child
;
91 IORegistryIterator
*regIter
;
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 regIter
= IORegistryIterator::iterateOver( gIODTPlane
,
204 kIORegistryIterateRecursively
);
207 while( (child
= regIter
->getNextObject())) {
208 IODTMapInterrupts( child
);
209 if( !intMap
&& child
->getProperty( gIODTInterruptParentKey
))
212 // Look for a "driver,AAPL,MacOSX,PowerPC" property.
213 if( (obj
= child
->getProperty( "driver,AAPL,MacOSX,PowerPC"))) {
214 gIOCatalogue
->addExtensionsFromArchive((OSData
*)obj
);
215 child
->removeProperty( "driver,AAPL,MacOSX,PowerPC");
218 // some gross pruning
219 child
->removeProperty( "lanLib,AAPL,MacOS,PowerPC");
221 if( (obj
= child
->getProperty( "driver,AAPL,MacOS,PowerPC"))) {
223 if( (0 == (prop
= (OSData
*)child
->getProperty( gIODTTypeKey
)))
224 || (strcmp( "display", (char *) prop
->getBytesNoCopy())) ) {
225 child
->removeProperty( "driver,AAPL,MacOS,PowerPC");
233 // set a key in the root to indicate we found NW interrupt mapping
234 parent
->setProperty( gIODTNWInterruptMappingKey
,
235 (OSObject
*) gIODTNWInterruptMappingKey
);
240 int IODTGetLoaderInfo( char *key
, void **infoAddr
, int *infoSize
)
242 IORegistryEntry
*chosen
;
244 unsigned int *propPtr
;
245 unsigned int propSize
;
247 chosen
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
);
248 if ( chosen
== 0 ) return -1;
250 propObj
= OSDynamicCast( OSData
, chosen
->getProperty(key
) );
251 if ( propObj
== 0 ) return -1;
253 propSize
= propObj
->getLength();
254 if ( propSize
!= (2 * sizeof(UInt32
)) ) return -1;
256 propPtr
= (unsigned int *)propObj
->getBytesNoCopy();
257 if ( propPtr
== 0 ) return -1;
259 *infoAddr
= (void *)propPtr
[0] ;
260 *infoSize
= (int) propPtr
[1];
265 void IODTFreeLoaderInfo( char *key
, void *infoAddr
, int infoSize
)
267 vm_offset_t range
[2];
268 IORegistryEntry
*chosen
;
270 range
[0] = (vm_offset_t
)infoAddr
;
271 range
[1] = (vm_offset_t
)infoSize
;
272 FreePhysicalMemory( range
);
275 chosen
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
);
277 chosen
->removeProperty(key
);
282 static void FreePhysicalMemory( vm_offset_t
* range
)
286 virt
= ml_static_ptovirt( range
[0] );
288 ml_static_mfree( virt
, range
[1] );
292 static IORegistryEntry
*
293 MakeReferenceTable( DTEntry dtEntry
, bool copy
)
295 IORegistryEntry
*regEntry
;
296 OSDictionary
*propTable
;
297 const OSSymbol
*nameKey
;
300 DTPropertyIterator dtIter
;
305 bool noLocation
= true;
307 regEntry
= new IOService
;
309 if( regEntry
&& (false == regEntry
->init())) {
315 (kSuccess
== DTCreatePropertyIterator( dtEntry
, &dtIter
))) {
317 propTable
= regEntry
->getPropertyTable();
319 while( kSuccess
== DTIterateProperties( dtIter
, &name
)) {
321 if( kSuccess
!= DTGetProperty( dtEntry
, name
, &prop
, &propSize
))
325 nameKey
= OSSymbol::withCString(name
);
326 data
= OSData::withBytes(prop
, propSize
);
328 nameKey
= OSSymbol::withCStringNoCopy(name
);
329 data
= OSData::withBytesNoCopy(prop
, propSize
);
331 assert( nameKey
&& data
);
333 propTable
->setObject( nameKey
, data
);
337 if( nameKey
== gIODTNameKey
) {
339 sym
= OSSymbol::withCString( (const char *) prop
);
341 sym
= OSSymbol::withCStringNoCopy( (const char *) prop
);
342 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;
468 parent
= IODTFindInterruptParent( regEntry
);
469 IODTGetICellCounts( parent
, &icells
, &acells
);
472 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "reg" ));
473 if( data
&& (data
->getLength() >= (acells
* sizeof( UInt32
))))
474 addrCmp
= (UInt32
*) data
->getBytesNoCopy();
476 original_icells
= icells
;
481 kprintf ("IODTMapOneInterrupt: current regEntry name %s\n", regEntry
->getName());
482 kprintf ("acells - icells: ");
483 for (i
= 0; i
< acells
; i
++) kprintf ("0x%08X ", addrCmp
[i
]);
485 for (i
= 0; i
< icells
; i
++) kprintf ("0x%08X ", intSpec
[i
]);
489 if( parent
&& (data
= OSDynamicCast( OSData
,
490 regEntry
->getProperty( "interrupt-controller")))) {
491 // found a controller - don't want to follow cascaded controllers
493 *spec
= OSData::withBytesNoCopy( (void *) intSpec
,
494 icells
* sizeof( UInt32
));
495 *controller
= IODTInterruptControllerName( regEntry
);
496 ok
= (*spec
&& *controller
);
497 } else if( parent
&& (data
= OSDynamicCast( OSData
,
498 regEntry
->getProperty( "interrupt-map")))) {
500 map
= (UInt32
*) data
->getBytesNoCopy();
501 endMap
= map
+ (data
->getLength() / sizeof(UInt32
));
502 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "interrupt-map-mask" ));
503 if( data
&& (data
->getLength() >= ((acells
+ icells
) * sizeof( UInt32
))))
504 maskCmp
= (UInt32
*) data
->getBytesNoCopy();
510 kprintf (" maskCmp: ");
511 for (i
= 0; i
< acells
+ icells
; i
++) {
514 kprintf ("0x%08X ", maskCmp
[i
]);
517 kprintf (" masked: ");
518 for (i
= 0; i
< acells
+ icells
; i
++) {
521 kprintf ("0x%08X ", ((i
< acells
) ? addrCmp
[i
] : intSpec
[i
-acells
]) & maskCmp
[i
]);
525 kprintf ("no maskCmp\n");
530 for (i
= 0; i
< acells
+ icells
; i
++) {
533 kprintf ("0x%08X ", map
[i
]);
537 for( i
= 0, cmp
= true; cmp
&& (i
< (acells
+ icells
)); i
++) {
538 cell
= (i
< acells
) ? addrCmp
[i
] : intSpec
[ i
- acells
];
541 cmp
= (cell
== map
[i
]);
544 map
+= acells
+ icells
;
545 if( 0 == (parent
= FindPHandle( *(map
++) )))
548 IODTGetICellCounts( parent
, &picells
, &pacells
);
551 intSpec
= map
+ pacells
;
554 map
+= pacells
+ picells
;
556 } while( !cmp
&& (map
< endMap
) );
562 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 );
754 result
= OSSet::withCapacity( 3 );
759 while( (next
= iter
->getNextObject())) {
761 // Look for existence of a debug property to skip
762 if( next
->getProperty("AAPL,ignore"))
766 cmp
= IODTMatchNubWithKeys( next
, keys
);
767 if( (minus
&& (false == cmp
))
768 || ((false == minus
) && (false != cmp
)) )
769 result
->setObject( next
);
771 result
->setObject( next
);
773 } while( !iter
->isValid());
778 cIter
= OSCollectionIterator::withCollection( result
);
785 struct IODTPersistent
{
786 IODTCompareAddressCellFunc compareFunc
;
787 IODTNVLocationFunc locationFunc
;
790 void IODTSetResolving( IORegistryEntry
* regEntry
,
791 IODTCompareAddressCellFunc compareFunc
,
792 IODTNVLocationFunc locationFunc
)
794 IODTPersistent persist
;
797 persist
.compareFunc
= compareFunc
;
798 persist
.locationFunc
= locationFunc
;
799 prop
= OSData::withBytes( &persist
, sizeof( persist
));
803 regEntry
->setProperty( gIODTPersistKey
, prop
);
808 static SInt32
DefaultCompare( UInt32 cellCount
, UInt32 left
[], UInt32 right
[] )
811 return( left
[ cellCount
] - right
[ cellCount
] );
815 void IODTGetCellCounts( IORegistryEntry
* regEntry
,
816 UInt32
* sizeCount
, UInt32
* addressCount
)
818 if( !GetUInt32( regEntry
, gIODTSizeCellKey
, sizeCount
))
820 if( !GetUInt32( regEntry
, gIODTAddressCellKey
, addressCount
))
825 // Given addr & len cells from our child, find it in our ranges property, then
826 // look in our parent to resolve the base of the range for us.
828 // Range[]: child-addr our-addr child-len
829 // #cells: child ours child
831 bool IODTResolveAddressCell( IORegistryEntry
* regEntry
,
833 IOPhysicalAddress
* phys
, IOPhysicalLength
* len
)
835 IORegistryEntry
*parent
;
837 // cells in addresses at regEntry
838 UInt32 sizeCells
, addressCells
;
839 // cells in addresses below regEntry
840 UInt32 childSizeCells
, childAddressCells
;
842 UInt32 cell
[ 5 ], offset
= 0, length
;
848 IODTPersistent
*persist
;
849 IODTCompareAddressCellFunc compare
;
851 IODTGetCellCounts( regEntry
, &childSizeCells
, &childAddressCells
);
852 childCells
= childAddressCells
+ childSizeCells
;
854 bcopy( cellsIn
, cell
, 4 * childCells
);
855 if( childSizeCells
> 1)
856 *len
= IOPhysical32( cellsIn
[ childAddressCells
],
857 cellsIn
[ childAddressCells
+ 1 ] );
859 *len
= IOPhysical32( 0, cellsIn
[ childAddressCells
] );
862 prop
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTRangeKey
));
864 /* end of the road */
865 *phys
= IOPhysical32( 0, cell
[ childAddressCells
- 1 ] + offset
);
869 parent
= regEntry
->getParentEntry( gIODTPlane
);
870 IODTGetCellCounts( parent
, &sizeCells
, &addressCells
);
872 if( (length
= prop
->getLength())) {
874 range
= (UInt32
*) prop
->getBytesNoCopy();
875 endRanges
= range
+ (length
/ 4);
877 prop
= (OSData
*) regEntry
->getProperty( gIODTPersistKey
);
879 persist
= (IODTPersistent
*) prop
->getBytesNoCopy();
880 compare
= persist
->compareFunc
;
882 compare
= DefaultCompare
;
886 range
+= (childCells
+ addressCells
) ) {
888 // is cell >= range start?
889 diff
= (*compare
)( childAddressCells
, cell
, range
);
893 // is cell + size <= range end?
894 if( (diff
+ cell
[ childCells
- 1 ])
895 > range
[ childCells
+ addressCells
- 1 ])
903 // Get the physical start of the range from our parent
904 bcopy( range
+ childAddressCells
, cell
, 4 * addressCells
);
905 bzero( cell
+ addressCells
, 4 * sizeCells
);
907 } /* else zero length range => pass thru to parent */
910 childSizeCells
= sizeCells
;
911 childAddressCells
= addressCells
;
912 childCells
= childAddressCells
+ childSizeCells
;
914 } while( ok
&& regEntry
);
920 OSArray
* IODTResolveAddressing( IORegistryEntry
* regEntry
,
921 const char * addressPropertyName
,
922 IODeviceMemory
* parent
)
924 IORegistryEntry
*parentEntry
;
925 OSData
*addressProperty
;
926 UInt32 sizeCells
, addressCells
, cells
;
929 IOPhysicalAddress phys
;
930 IOPhysicalLength len
;
932 IODeviceMemory
*range
;
934 parentEntry
= regEntry
->getParentEntry( gIODTPlane
);
935 addressProperty
= (OSData
*) regEntry
->getProperty( addressPropertyName
);
936 if( (0 == addressProperty
) || (0 == parentEntry
))
939 IODTGetCellCounts( parentEntry
, &sizeCells
, &addressCells
);
943 cells
= sizeCells
+ addressCells
;
944 reg
= (UInt32
*) addressProperty
->getBytesNoCopy();
945 num
= addressProperty
->getLength() / (4 * cells
);
947 array
= OSArray::withCapacity( 1 );
951 for( i
= 0; i
< num
; i
++) {
952 if( IODTResolveAddressCell( parentEntry
, reg
, &phys
, &len
)) {
955 range
= IODeviceMemory::withSubRange( parent
,
956 phys
- parent
->getPhysicalAddress(), len
);
958 range
= IODeviceMemory::withRange( phys
, len
);
960 array
->setObject( range
);
965 regEntry
->setProperty( gIODeviceMemoryKey
, array
);
966 array
->release(); /* ??? */
971 static void IODTGetNVLocation(
972 IORegistryEntry
* parent
,
973 IORegistryEntry
* regEntry
,
974 UInt8
* busNum
, UInt8
* deviceNum
, UInt8
* functionNum
)
978 IODTPersistent
*persist
;
981 prop
= (OSData
*) parent
->getProperty( gIODTPersistKey
);
983 persist
= (IODTPersistent
*) prop
->getBytesNoCopy();
984 (*persist
->locationFunc
)( regEntry
, busNum
, deviceNum
, functionNum
);
986 prop
= (OSData
*) regEntry
->getProperty( "reg" );
989 cell
= (UInt32
*) prop
->getBytesNoCopy();
991 *deviceNum
= 0x1f & (cell
[ 0 ] >> 24);
1001 * Try to make the same messed up descriptor as Mac OS
1004 IOReturn
IODTMakeNVDescriptor( IORegistryEntry
* regEntry
,
1005 IONVRAMDescriptor
* hdr
)
1007 IORegistryEntry
*parent
;
1009 UInt32 bridgeDevices
;
1017 for(level
= 0, bridgeDevices
= 0;
1018 (parent
= regEntry
->getParentEntry( gIODTPlane
)) && (level
< 7); level
++ ) {
1020 IODTGetNVLocation( parent
, regEntry
,
1021 &busNum
, &deviceNum
, &functionNum
);
1023 bridgeDevices
|= ((deviceNum
& 0x1f) << ((level
- 1) * 5));
1025 hdr
->busNum
= busNum
;
1026 hdr
->deviceNum
= deviceNum
;
1027 hdr
->functionNum
= functionNum
;
1031 hdr
->bridgeCount
= level
- 2;
1032 hdr
->bridgeDevices
= bridgeDevices
;
1034 return( kIOReturnSuccess
);
1037 OSData
* IODTFindSlotName( IORegistryEntry
* regEntry
, UInt32 deviceNumber
)
1039 IORegistryEntry
*parent
;
1048 data
= (OSData
*) regEntry
->getProperty("AAPL,slot-name");
1051 parent
= regEntry
->getParentEntry( gIODTPlane
);
1054 data
= OSDynamicCast( OSData
, parent
->getProperty("slot-names"));
1057 if( data
->getLength() <= 4)
1060 bits
= (UInt32
*) data
->getBytesNoCopy();
1062 if( (0 == (mask
& (1 << deviceNumber
))))
1065 names
= (char *)(bits
+ 1);
1066 lastName
= names
+ (data
->getLength() - 4);
1068 for( i
= 0; (i
<= deviceNumber
) && (names
< lastName
); i
++ ) {
1070 if( mask
& (1 << i
)) {
1071 if( i
== deviceNumber
) {
1072 data
= OSData::withBytesNoCopy( names
, 1 + strlen( names
));
1074 regEntry
->setProperty("AAPL,slot-name", data
);
1079 names
+= 1 + strlen( names
);