2 * Copyright (c) 1998-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <IOKit/IODeviceTreeSupport.h>
30 #include <libkern/c++/OSContainers.h>
31 #include <IOKit/IODeviceMemory.h>
32 #include <IOKit/IOService.h>
33 #include <IOKit/IOCatalogue.h>
35 #include <IOKit/IOLib.h>
36 #include <IOKit/IOKitKeys.h>
38 #include <pexpert/device_tree.h>
41 #include <machine/machine_routines.h>
42 void DTInit( void * data
);
44 int IODTGetLoaderInfo( char *key
, void **infoAddr
, int *infosize
);
45 void IODTFreeLoaderInfo( char *key
, void *infoAddr
, int infoSize
);
48 #include <IOKit/assert.h>
50 #define IODTSUPPORTDEBUG 0
52 const IORegistryPlane
* gIODTPlane
;
54 static OSArray
* gIODTPHandles
;
55 static OSArray
* gIODTPHandleMap
;
57 const OSSymbol
* gIODTNameKey
;
58 const OSSymbol
* gIODTUnitKey
;
59 const OSSymbol
* gIODTCompatibleKey
;
60 const OSSymbol
* gIODTTypeKey
;
61 const OSSymbol
* gIODTModelKey
;
63 const OSSymbol
* gIODTSizeCellKey
;
64 const OSSymbol
* gIODTAddressCellKey
;
65 const OSSymbol
* gIODTRangeKey
;
67 const OSSymbol
* gIODTPersistKey
;
69 const OSSymbol
* gIODTDefaultInterruptController
;
70 const OSSymbol
* gIODTAAPLInterruptsKey
;
71 const OSSymbol
* gIODTPHandleKey
;
72 const OSSymbol
* gIODTInterruptCellKey
;
73 const OSSymbol
* gIODTInterruptParentKey
;
74 const OSSymbol
* gIODTNWInterruptMappingKey
;
76 OSDictionary
* gIODTSharedInterrupts
;
78 static IORegistryEntry
* MakeReferenceTable( DTEntry dtEntry
, bool copy
);
79 static void AddPHandle( IORegistryEntry
* regEntry
);
80 static void FreePhysicalMemory( vm_offset_t
* range
);
81 static bool IODTMapInterruptsSharing( IORegistryEntry
* regEntry
, OSDictionary
* allInts
);
84 IODeviceTreeAlloc( void * dtTop
)
86 IORegistryEntry
* parent
;
87 IORegistryEntry
* child
;
88 IORegistryIterator
* regIter
;
94 OSDictionary
* allInts
;
96 unsigned int propSize
;
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(uint32_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], (int) round_page(dtMap
[1]) );
200 gIODTSharedInterrupts
= OSDictionary::withCapacity(4);
201 allInts
= OSDictionary::withCapacity(4);
203 regIter
= IORegistryIterator::iterateOver( gIODTPlane
,
204 kIORegistryIterateRecursively
);
205 assert( regIter
&& allInts
&& gIODTSharedInterrupts
);
206 if( regIter
&& allInts
&& gIODTSharedInterrupts
) {
207 while( (child
= regIter
->getNextObject())) {
208 IODTMapInterruptsSharing( child
, allInts
);
209 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 || (strncmp("display", (char *)prop
->getBytesNoCopy(), sizeof("display"))) ) {
228 child
->removeProperty( "driver,AAPL,MacOS,PowerPC");
237 parent
->setProperty("allInts", allInts
);
238 parent
->setProperty("sharedInts", gIODTSharedInterrupts
);
240 regIter
= IORegistryIterator::iterateOver( gIODTPlane
,
241 kIORegistryIterateRecursively
);
243 while( (child
= regIter
->getNextObject())) {
245 array
= OSDynamicCast(OSArray
, child
->getProperty( gIOInterruptSpecifiersKey
));
246 for( UInt32 i
= 0; array
&& (i
< array
->getCount()); i
++)
248 IOOptionBits options
;
249 IOReturn ret
= IODTGetInterruptOptions( child
, i
, &options
);
250 if( (ret
!= kIOReturnSuccess
) || options
)
251 IOLog("%s[%ld] %ld (%x)\n", child
->getName(), i
, options
, ret
);
261 // set a key in the root to indicate we found NW interrupt mapping
262 parent
->setProperty( gIODTNWInterruptMappingKey
,
263 (OSObject
*) gIODTNWInterruptMappingKey
);
268 int IODTGetLoaderInfo( char *key
, void **infoAddr
, int *infoSize
)
270 IORegistryEntry
*chosen
;
272 unsigned int *propPtr
;
273 unsigned int propSize
;
275 chosen
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
);
276 if ( chosen
== 0 ) return -1;
278 propObj
= OSDynamicCast( OSData
, chosen
->getProperty(key
) );
279 if ( propObj
== 0 ) return -1;
281 propSize
= propObj
->getLength();
282 if ( propSize
!= (2 * sizeof(UInt32
)) ) return -1;
284 propPtr
= (unsigned int *)propObj
->getBytesNoCopy();
285 if ( propPtr
== 0 ) return -1;
287 *infoAddr
= (void *)propPtr
[0] ;
288 *infoSize
= (int) propPtr
[1];
293 void IODTFreeLoaderInfo( char *key
, void *infoAddr
, int infoSize
)
295 vm_offset_t range
[2];
296 IORegistryEntry
*chosen
;
298 range
[0] = (vm_offset_t
)infoAddr
;
299 range
[1] = (vm_offset_t
)infoSize
;
300 FreePhysicalMemory( range
);
303 chosen
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
);
305 chosen
->removeProperty(key
);
310 static void FreePhysicalMemory( vm_offset_t
* range
)
314 virt
= ml_static_ptovirt( range
[0] );
316 ml_static_mfree( virt
, range
[1] );
320 static IORegistryEntry
*
321 MakeReferenceTable( DTEntry dtEntry
, bool copy
)
323 IORegistryEntry
*regEntry
;
324 OSDictionary
*propTable
;
325 const OSSymbol
*nameKey
;
328 DTPropertyIterator dtIter
;
330 unsigned int propSize
;
333 bool noLocation
= true;
335 regEntry
= new IOService
;
337 if( regEntry
&& (false == regEntry
->init())) {
343 (kSuccess
== DTCreatePropertyIterator( dtEntry
, &dtIter
))) {
345 propTable
= regEntry
->getPropertyTable();
347 while( kSuccess
== DTIterateProperties( dtIter
, &name
)) {
349 if( kSuccess
!= DTGetProperty( dtEntry
, name
, &prop
, &propSize
))
353 nameKey
= OSSymbol::withCString(name
);
354 data
= OSData::withBytes(prop
, propSize
);
356 nameKey
= OSSymbol::withCStringNoCopy(name
);
357 data
= OSData::withBytesNoCopy(prop
, propSize
);
359 assert( nameKey
&& data
);
361 propTable
->setObject( nameKey
, data
);
365 if( nameKey
== gIODTNameKey
) {
367 sym
= OSSymbol::withCString( (const char *) prop
);
369 sym
= OSSymbol::withCStringNoCopy( (const char *) prop
);
370 regEntry
->setName( sym
);
373 } else if( nameKey
== gIODTUnitKey
) {
374 // all OF strings are null terminated... except this one
375 if( propSize
>= (int) sizeof(location
))
376 propSize
= sizeof(location
) - 1;
377 strncpy( location
, (const char *) prop
, propSize
);
378 location
[ propSize
] = 0;
379 regEntry
->setLocation( location
);
380 propTable
->removeObject( gIODTUnitKey
);
383 } else if(noLocation
&& (!strncmp(name
, "reg", sizeof("reg")))) {
384 // default location - override later
385 snprintf(location
, sizeof(location
), "%X", *((uint32_t *) prop
));
386 regEntry
->setLocation( location
);
389 DTDisposePropertyIterator( dtIter
);
395 static void AddPHandle( IORegistryEntry
* regEntry
)
399 if( regEntry
->getProperty( gIODTInterruptCellKey
)
400 && (data
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTPHandleKey
)))) {
401 // a possible interrupt-parent
402 gIODTPHandles
->setObject( data
);
403 gIODTPHandleMap
->setObject( regEntry
);
407 static IORegistryEntry
* FindPHandle( UInt32 phandle
)
410 IORegistryEntry
*regEntry
= 0;
413 for( i
= 0; (data
= (OSData
*)gIODTPHandles
->getObject( i
)); i
++ ) {
414 if( phandle
== *((UInt32
*)data
->getBytesNoCopy())) {
415 regEntry
= (IORegistryEntry
*)
416 gIODTPHandleMap
->getObject( i
);
424 static bool GetUInt32( IORegistryEntry
* regEntry
, const OSSymbol
* name
,
429 if( (data
= OSDynamicCast( OSData
, regEntry
->getProperty( name
)))
430 && (4 == data
->getLength())) {
431 *value
= *((UInt32
*) data
->getBytesNoCopy());
437 static IORegistryEntry
* IODTFindInterruptParent( IORegistryEntry
* regEntry
, IOItemCount index
)
439 IORegistryEntry
* parent
;
444 if( (data
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTInterruptParentKey
)))
445 && (sizeof(UInt32
) <= (len
= data
->getLength()))) {
446 if (((index
+ 1) * sizeof(UInt32
)) > len
)
448 phandle
= ((UInt32
*) data
->getBytesNoCopy())[index
];
449 parent
= FindPHandle( phandle
);
451 } else if( 0 == regEntry
->getProperty( "interrupt-controller"))
452 parent
= regEntry
->getParentEntry( gIODTPlane
);
459 const OSSymbol
* IODTInterruptControllerName( IORegistryEntry
* regEntry
)
466 ok
= GetUInt32( regEntry
, gIODTPHandleKey
, &phandle
);
470 snprintf(buf
, sizeof(buf
), "IOInterruptController%08X", (uint32_t)phandle
);
471 sym
= OSSymbol::withCString( buf
);
478 #define unexpected(a) { kprintf("unexpected %s:%d\n", __FILE__, __LINE__); a; }
480 static void IODTGetICellCounts( IORegistryEntry
* regEntry
,
481 UInt32
* iCellCount
, UInt32
* aCellCount
)
483 if( !GetUInt32( regEntry
, gIODTInterruptCellKey
, iCellCount
))
484 unexpected( *iCellCount
= 1 );
485 if( !GetUInt32( regEntry
, gIODTAddressCellKey
, aCellCount
))
489 static UInt32
IODTMapOneInterrupt( IORegistryEntry
* regEntry
, UInt32
* intSpec
, UInt32 index
,
490 OSData
** spec
, const OSSymbol
** controller
)
492 IORegistryEntry
*parent
= 0;
498 UInt32 acells
, icells
, pacells
, picells
, cell
;
499 UInt32 i
, original_icells
;
500 bool cmp
, ok
= false;
502 parent
= IODTFindInterruptParent( regEntry
, index
);
503 IODTGetICellCounts( parent
, &icells
, &acells
);
506 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "reg" ));
507 if( data
&& (data
->getLength() >= (acells
* sizeof(UInt32
))))
508 addrCmp
= (UInt32
*) data
->getBytesNoCopy();
510 original_icells
= icells
;
515 kprintf ("IODTMapOneInterrupt: current regEntry name %s\n", regEntry
->getName());
516 kprintf ("acells - icells: ");
517 for (i
= 0; i
< acells
; i
++) kprintf ("0x%08X ", addrCmp
[i
]);
519 for (i
= 0; i
< icells
; i
++) kprintf ("0x%08X ", intSpec
[i
]);
523 if( parent
&& (data
= OSDynamicCast( OSData
,
524 regEntry
->getProperty( "interrupt-controller")))) {
525 // found a controller - don't want to follow cascaded controllers
527 *spec
= OSData::withBytesNoCopy( (void *) intSpec
,
528 icells
* sizeof(UInt32
));
529 *controller
= IODTInterruptControllerName( regEntry
);
530 ok
= (*spec
&& *controller
);
531 } else if( parent
&& (data
= OSDynamicCast( OSData
,
532 regEntry
->getProperty( "interrupt-map")))) {
534 map
= (UInt32
*) data
->getBytesNoCopy();
535 endMap
= map
+ (data
->getLength() / sizeof(UInt32
));
536 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "interrupt-map-mask" ));
537 if( data
&& (data
->getLength() >= ((acells
+ icells
) * sizeof(UInt32
))))
538 maskCmp
= (UInt32
*) data
->getBytesNoCopy();
544 kprintf (" maskCmp: ");
545 for (i
= 0; i
< acells
+ icells
; i
++) {
548 kprintf ("0x%08X ", maskCmp
[i
]);
551 kprintf (" masked: ");
552 for (i
= 0; i
< acells
+ icells
; i
++) {
555 kprintf ("0x%08X ", ((i
< acells
) ? addrCmp
[i
] : intSpec
[i
-acells
]) & maskCmp
[i
]);
559 kprintf ("no maskCmp\n");
564 for (i
= 0; i
< acells
+ icells
; i
++) {
567 kprintf ("0x%08X ", map
[i
]);
571 for( i
= 0, cmp
= true; cmp
&& (i
< (acells
+ icells
)); i
++) {
572 cell
= (i
< acells
) ? addrCmp
[i
] : intSpec
[ i
- acells
];
575 cmp
= (cell
== map
[i
]);
578 map
+= acells
+ icells
;
579 if( 0 == (parent
= FindPHandle( *(map
++) )))
582 IODTGetICellCounts( parent
, &picells
, &pacells
);
585 intSpec
= map
+ pacells
;
588 map
+= pacells
+ picells
;
590 } while( !cmp
&& (map
< endMap
) );
596 IODTGetICellCounts( parent
, &icells
, &acells
);
602 return( ok
? original_icells
: 0 );
605 IOReturn
IODTGetInterruptOptions( IORegistryEntry
* regEntry
, int source
, IOOptionBits
* options
)
607 OSArray
* controllers
;
608 OSArray
* specifiers
;
615 controllers
= OSDynamicCast(OSArray
, regEntry
->getProperty(gIOInterruptControllersKey
));
616 specifiers
= OSDynamicCast(OSArray
, regEntry
->getProperty(gIOInterruptSpecifiersKey
));
618 if( !controllers
|| !specifiers
)
619 return (kIOReturnNoInterrupt
);
621 shared
= (OSArray
*) gIODTSharedInterrupts
->getObject(
622 (const OSSymbol
*) controllers
->getObject(source
) );
624 return (kIOReturnSuccess
);
626 spec
= specifiers
->getObject(source
);
628 return (kIOReturnNoInterrupt
);
630 for (unsigned int i
= 0;
631 (oneSpec
= shared
->getObject(i
))
632 && (!oneSpec
->isEqualTo(spec
));
636 *options
= kIODTInterruptShared
;
638 return (kIOReturnSuccess
);
641 static bool IODTMapInterruptsSharing( IORegistryEntry
* regEntry
, OSDictionary
* allInts
)
643 IORegistryEntry
* parent
;
652 OSArray
* controllerInts
;
653 const OSSymbol
* controller
= 0;
654 OSArray
* controllers
;
658 nw
= (0 == (local
= OSDynamicCast( OSData
,
659 regEntry
->getProperty( gIODTAAPLInterruptsKey
))));
660 if( nw
&& (0 == (local
= OSDynamicCast( OSData
,
661 regEntry
->getProperty( "interrupts")))))
662 return( true ); // nothing to see here
664 if( nw
&& (parent
= regEntry
->getParentEntry( gIODTPlane
))) {
665 // check for bridges on old world
666 if( (local2
= OSDynamicCast( OSData
,
667 parent
->getProperty( gIODTAAPLInterruptsKey
)))) {
673 localBits
= (UInt32
*) local
->getBytesNoCopy();
674 localEnd
= localBits
+ (local
->getLength() / sizeof(UInt32
));
676 mapped
= OSArray::withCapacity( 1 );
677 controllers
= OSArray::withCapacity( 1 );
679 ok
= (mapped
&& controllers
);
683 skip
= IODTMapOneInterrupt( regEntry
, localBits
, index
, &map
, &controller
);
685 IOLog("%s: error mapping interrupt[%d]\n",
686 regEntry
->getName(), mapped
->getCount());
690 map
= OSData::withData( local
, mapped
->getCount() * sizeof(UInt32
),
692 controller
= gIODTDefaultInterruptController
;
693 controller
->retain();
698 mapped
->setObject( map
);
699 controllers
->setObject( controller
);
703 controllerInts
= (OSArray
*) allInts
->getObject( controller
);
706 for (unsigned int i
= 0; (oneMap
= controllerInts
->getObject(i
)); i
++)
708 if (map
->isEqualTo(oneMap
))
710 controllerInts
= (OSArray
*) gIODTSharedInterrupts
->getObject( controller
);
712 controllerInts
->setObject(map
);
715 controllerInts
= OSArray::withObjects( (const OSObject
**) &map
, 1, 4 );
718 gIODTSharedInterrupts
->setObject( controller
, controllerInts
);
719 controllerInts
->release();
726 controllerInts
->setObject(map
);
730 controllerInts
= OSArray::withObjects( (const OSObject
**) &map
, 1, 16 );
733 allInts
->setObject( controller
, controllerInts
);
734 controllerInts
->release();
740 controller
->release();
742 } while( localBits
< localEnd
);
744 ok
&= (localBits
== localEnd
);
748 ok
= regEntry
->setProperty( gIOInterruptControllersKey
, controllers
);
749 ok
&= regEntry
->setProperty( gIOInterruptSpecifiersKey
, mapped
);
753 controllers
->release();
760 bool IODTMapInterrupts( IORegistryEntry
* regEntry
)
762 return( IODTMapInterruptsSharing( regEntry
, 0 ));
769 CompareKey( OSString
* key
,
770 const IORegistryEntry
* table
, const OSSymbol
* propName
)
778 const char *lastName
;
781 const char *result
= 0;
783 if( 0 == (prop
= table
->getProperty( propName
)))
786 if( (data
= OSDynamicCast( OSData
, prop
))) {
787 names
= (const char *) data
->getBytesNoCopy();
788 lastName
= names
+ data
->getLength();
789 } else if( (string
= OSDynamicCast( OSString
, prop
))) {
790 names
= string
->getCStringNoCopy();
791 lastName
= names
+ string
->getLength() + 1;
795 ckey
= key
->getCStringNoCopy();
796 keyLen
= key
->getLength();
797 wild
= ('*' == key
->getChar( keyLen
- 1 ));
800 // for each name in the property
802 matched
= (0 == strncmp( ckey
, names
, keyLen
- 1 ));
804 matched
= (keyLen
== strlen( names
))
805 && (0 == strncmp( ckey
, names
, keyLen
));
810 names
= names
+ strlen( names
) + 1;
812 } while( (names
< lastName
) && (false == matched
));
818 bool IODTCompareNubName( const IORegistryEntry
* regEntry
,
819 OSString
* name
, OSString
** matchingName
)
824 matched
= (0 != (result
= CompareKey( name
, regEntry
, gIODTNameKey
)))
825 || (0 != (result
= CompareKey( name
, regEntry
, gIODTCompatibleKey
)))
826 || (0 != (result
= CompareKey( name
, regEntry
, gIODTTypeKey
)))
827 || (0 != (result
= CompareKey( name
, regEntry
, gIODTModelKey
)));
829 if( result
&& matchingName
)
830 *matchingName
= OSString::withCString( result
);
832 return( result
!= 0 );
835 bool IODTMatchNubWithKeys( IORegistryEntry
* regEntry
,
841 obj
= OSUnserialize( keys
, 0 );
844 result
= regEntry
->compareNames( obj
);
848 else IOLog("Couldn't unserialize %s\n", keys
);
854 OSCollectionIterator
* IODTFindMatchingEntries( IORegistryEntry
* from
,
855 IOOptionBits options
, const char * keys
)
858 IORegistryEntry
*next
;
859 IORegistryIterator
*iter
;
860 OSCollectionIterator
*cIter
;
862 bool minus
= options
& kIODTExclusive
;
865 iter
= IORegistryIterator::iterateOver( from
, gIODTPlane
,
866 (options
& kIODTRecursive
) ? kIORegistryIterateRecursively
: 0 );
873 result
= OSSet::withCapacity( 3 );
878 while( (next
= iter
->getNextObject())) {
880 // Look for existence of a debug property to skip
881 if( next
->getProperty("AAPL,ignore"))
885 cmp
= IODTMatchNubWithKeys( next
, keys
);
886 if( (minus
&& (false == cmp
))
887 || ((false == minus
) && (false != cmp
)) )
888 result
->setObject( next
);
890 result
->setObject( next
);
892 } while( !iter
->isValid());
897 cIter
= OSCollectionIterator::withCollection( result
);
904 struct IODTPersistent
{
905 IODTCompareAddressCellFunc compareFunc
;
906 IODTNVLocationFunc locationFunc
;
909 void IODTSetResolving( IORegistryEntry
* regEntry
,
910 IODTCompareAddressCellFunc compareFunc
,
911 IODTNVLocationFunc locationFunc
)
913 IODTPersistent persist
;
916 persist
.compareFunc
= compareFunc
;
917 persist
.locationFunc
= locationFunc
;
918 prop
= OSData::withBytes( &persist
, sizeof(persist
));
922 regEntry
->setProperty( gIODTPersistKey
, prop
);
927 static SInt32
DefaultCompare( UInt32 cellCount
, UInt32 left
[], UInt32 right
[] )
930 return( left
[ cellCount
] - right
[ cellCount
] );
933 void IODTGetCellCounts( IORegistryEntry
* regEntry
,
934 UInt32
* sizeCount
, UInt32
* addressCount
)
936 if( !GetUInt32( regEntry
, gIODTSizeCellKey
, sizeCount
))
938 if( !GetUInt32( regEntry
, gIODTAddressCellKey
, addressCount
))
943 // Given addr & len cells from our child, find it in our ranges property, then
944 // look in our parent to resolve the base of the range for us.
946 // Range[]: child-addr our-addr child-len
947 // #cells: child ours child
949 bool IODTResolveAddressCell( IORegistryEntry
* regEntry
,
951 IOPhysicalAddress
* phys
, IOPhysicalLength
* len
)
953 IORegistryEntry
*parent
;
955 // cells in addresses at regEntry
956 UInt32 sizeCells
, addressCells
;
957 // cells in addresses below regEntry
958 UInt32 childSizeCells
, childAddressCells
;
960 UInt32 cell
[ 5 ], offset
= 0, length
;
967 SInt32 diff
, diff2
, endDiff
;
969 IODTPersistent
*persist
;
970 IODTCompareAddressCellFunc compare
;
972 IODTGetCellCounts( regEntry
, &childSizeCells
, &childAddressCells
);
973 childCells
= childAddressCells
+ childSizeCells
;
975 bcopy( cellsIn
, cell
, sizeof(UInt32
) * childCells
);
976 if( childSizeCells
> 1)
977 *len
= IOPhysical32( cellsIn
[ childAddressCells
],
978 cellsIn
[ childAddressCells
+ 1 ] );
980 *len
= IOPhysical32( 0, cellsIn
[ childAddressCells
] );
984 prop
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTRangeKey
));
986 /* end of the road */
987 *phys
= IOPhysical32( 0, cell
[ childAddressCells
- 1 ] + offset
);
991 parent
= regEntry
->getParentEntry( gIODTPlane
);
992 IODTGetCellCounts( parent
, &sizeCells
, &addressCells
);
994 if( (length
= prop
->getLength())) {
996 startRange
= (UInt32
*) prop
->getBytesNoCopy();
998 endRanges
= range
+ (length
/ sizeof(UInt32
));
1000 prop
= (OSData
*) regEntry
->getProperty( gIODTPersistKey
);
1002 persist
= (IODTPersistent
*) prop
->getBytesNoCopy();
1003 compare
= persist
->compareFunc
;
1005 compare
= DefaultCompare
;
1009 range
+= (childCells
+ addressCells
) ) {
1011 // is cell start within range?
1012 diff
= (*compare
)( childAddressCells
, cell
, range
);
1014 bcopy(range
, endCell
, childAddressCells
* sizeof(UInt32
));
1015 endCell
[childAddressCells
- 1] += range
[childCells
+ addressCells
- 1];
1016 diff2
= (*compare
)( childAddressCells
, cell
, endCell
);
1018 if ((diff
< 0) || (diff2
>= 0))
1021 ok
= (0 == cell
[childCells
- 1]);
1024 // search for cell end
1025 bcopy(cell
, endCell
, childAddressCells
* sizeof(UInt32
));
1026 endCell
[childAddressCells
- 1] += cell
[childCells
- 1] - 1;
1027 lookRange
= startRange
;
1029 lookRange
< endRanges
;
1030 lookRange
+= (childCells
+ addressCells
) )
1032 // is cell >= range start?
1033 endDiff
= (*compare
)( childAddressCells
, endCell
, lookRange
);
1036 if ((endDiff
- cell
[childCells
- 1] + 1 + lookRange
[childAddressCells
+ addressCells
- 1])
1037 == (diff
+ range
[childAddressCells
+ addressCells
- 1]))
1050 // Get the physical start of the range from our parent
1051 bcopy( range
+ childAddressCells
, cell
, sizeof(UInt32
) * addressCells
);
1052 bzero( cell
+ addressCells
, sizeof(UInt32
) * sizeCells
);
1054 } /* else zero length range => pass thru to parent */
1057 childSizeCells
= sizeCells
;
1058 childAddressCells
= addressCells
;
1059 childCells
= childAddressCells
+ childSizeCells
;
1061 while( ok
&& regEntry
);
1067 OSArray
* IODTResolveAddressing( IORegistryEntry
* regEntry
,
1068 const char * addressPropertyName
,
1069 IODeviceMemory
* parent
)
1071 IORegistryEntry
*parentEntry
;
1072 OSData
*addressProperty
;
1073 UInt32 sizeCells
, addressCells
, cells
;
1076 IOPhysicalAddress phys
;
1077 IOPhysicalLength len
;
1079 IODeviceMemory
*range
;
1081 parentEntry
= regEntry
->getParentEntry( gIODTPlane
);
1082 addressProperty
= (OSData
*) regEntry
->getProperty( addressPropertyName
);
1083 if( (0 == addressProperty
) || (0 == parentEntry
))
1086 IODTGetCellCounts( parentEntry
, &sizeCells
, &addressCells
);
1090 cells
= sizeCells
+ addressCells
;
1091 reg
= (UInt32
*) addressProperty
->getBytesNoCopy();
1092 num
= addressProperty
->getLength() / (4 * cells
);
1094 array
= OSArray::withCapacity( 1 );
1098 for( i
= 0; i
< num
; i
++) {
1099 if( IODTResolveAddressCell( parentEntry
, reg
, &phys
, &len
)) {
1102 range
= IODeviceMemory::withSubRange( parent
,
1103 phys
- parent
->getPhysicalSegment(0, 0, kIOMemoryMapperNone
), len
);
1105 range
= IODeviceMemory::withRange( phys
, len
);
1107 array
->setObject( range
);
1112 regEntry
->setProperty( gIODeviceMemoryKey
, array
);
1113 array
->release(); /* ??? */
1118 static void IODTGetNVLocation(
1119 IORegistryEntry
* parent
,
1120 IORegistryEntry
* regEntry
,
1121 UInt8
* busNum
, UInt8
* deviceNum
, UInt8
* functionNum
)
1125 IODTPersistent
*persist
;
1128 prop
= (OSData
*) parent
->getProperty( gIODTPersistKey
);
1130 persist
= (IODTPersistent
*) prop
->getBytesNoCopy();
1131 (*persist
->locationFunc
)( regEntry
, busNum
, deviceNum
, functionNum
);
1133 prop
= (OSData
*) regEntry
->getProperty( "reg" );
1136 cell
= (UInt32
*) prop
->getBytesNoCopy();
1138 *deviceNum
= 0x1f & (cell
[ 0 ] >> 24);
1148 * Try to make the same messed up descriptor as Mac OS
1151 IOReturn
IODTMakeNVDescriptor( IORegistryEntry
* regEntry
,
1152 IONVRAMDescriptor
* hdr
)
1154 IORegistryEntry
*parent
;
1156 UInt32 bridgeDevices
;
1164 for(level
= 0, bridgeDevices
= 0;
1165 (parent
= regEntry
->getParentEntry( gIODTPlane
)) && (level
< 7); level
++ ) {
1167 IODTGetNVLocation( parent
, regEntry
,
1168 &busNum
, &deviceNum
, &functionNum
);
1170 bridgeDevices
|= ((deviceNum
& 0x1f) << ((level
- 1) * 5));
1172 hdr
->busNum
= busNum
;
1173 hdr
->deviceNum
= deviceNum
;
1174 hdr
->functionNum
= functionNum
;
1178 hdr
->bridgeCount
= level
- 2;
1179 hdr
->bridgeDevices
= bridgeDevices
;
1181 return( kIOReturnSuccess
);
1184 OSData
* IODTFindSlotName( IORegistryEntry
* regEntry
, UInt32 deviceNumber
)
1186 IORegistryEntry
*parent
;
1195 data
= (OSData
*) regEntry
->getProperty("AAPL,slot-name");
1198 parent
= regEntry
->getParentEntry( gIODTPlane
);
1201 data
= OSDynamicCast( OSData
, parent
->getProperty("slot-names"));
1204 if( data
->getLength() <= 4)
1207 bits
= (UInt32
*) data
->getBytesNoCopy();
1209 if( (0 == (mask
& (1 << deviceNumber
))))
1212 names
= (char *)(bits
+ 1);
1213 lastName
= names
+ (data
->getLength() - 4);
1215 for( i
= 0; (i
<= deviceNumber
) && (names
< lastName
); i
++ ) {
1217 if( mask
& (1 << i
)) {
1218 if( i
== deviceNumber
) {
1219 data
= OSData::withBytesNoCopy( names
, 1 + strlen( names
));
1221 regEntry
->setProperty("AAPL,slot-name", data
);
1226 names
+= 1 + strlen( names
);
1233 extern "C" IOReturn
IONDRVLibrariesInitialize( IOService
* provider
)
1235 return( kIOReturnUnsupported
);