2 * Copyright (c) 1998-2000 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
;
95 OSDictionary
* allInts
;
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_32(dtMap
[1]) );
201 gIODTSharedInterrupts
= OSDictionary::withCapacity(4);
202 allInts
= OSDictionary::withCapacity(4);
204 regIter
= IORegistryIterator::iterateOver( gIODTPlane
,
205 kIORegistryIterateRecursively
);
206 assert( regIter
&& allInts
&& gIODTSharedInterrupts
);
207 if( regIter
&& allInts
&& gIODTSharedInterrupts
) {
208 while( (child
= regIter
->getNextObject())) {
209 IODTMapInterruptsSharing( child
, allInts
);
210 if( !intMap
&& child
->getProperty( gIODTInterruptParentKey
))
213 // Look for a "driver,AAPL,MacOSX,PowerPC" property.
214 if( (obj
= child
->getProperty( "driver,AAPL,MacOSX,PowerPC"))) {
215 gIOCatalogue
->addExtensionsFromArchive((OSData
*)obj
);
216 child
->removeProperty( "driver,AAPL,MacOSX,PowerPC");
219 // some gross pruning
220 child
->removeProperty( "lanLib,AAPL,MacOS,PowerPC");
222 if( (obj
= child
->getProperty( "driver,AAPL,MacOS,PowerPC"))) {
224 if( (0 == (prop
= (OSData
*)child
->getProperty( gIODTTypeKey
)))
225 || (strcmp( "display", (char *) prop
->getBytesNoCopy())) ) {
226 child
->removeProperty( "driver,AAPL,MacOS,PowerPC");
234 parent
->setProperty("allInts", allInts
);
235 parent
->setProperty("sharedInts", gIODTSharedInterrupts
);
237 regIter
= IORegistryIterator::iterateOver( gIODTPlane
,
238 kIORegistryIterateRecursively
);
240 while( (child
= regIter
->getNextObject())) {
242 array
= OSDynamicCast(OSArray
, child
->getProperty( gIOInterruptSpecifiersKey
));
243 for( UInt32 i
= 0; array
&& (i
< array
->getCount()); i
++)
245 IOOptionBits options
;
246 IOReturn ret
= IODTGetInterruptOptions( child
, i
, &options
);
247 if( (ret
!= kIOReturnSuccess
) || options
)
248 IOLog("%s[%ld] %ld (%x)\n", child
->getName(), i
, options
, ret
);
258 // set a key in the root to indicate we found NW interrupt mapping
259 parent
->setProperty( gIODTNWInterruptMappingKey
,
260 (OSObject
*) gIODTNWInterruptMappingKey
);
265 int IODTGetLoaderInfo( char *key
, void **infoAddr
, int *infoSize
)
267 IORegistryEntry
*chosen
;
269 unsigned int *propPtr
;
270 unsigned int propSize
;
272 chosen
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
);
273 if ( chosen
== 0 ) return -1;
275 propObj
= OSDynamicCast( OSData
, chosen
->getProperty(key
) );
276 if ( propObj
== 0 ) return -1;
278 propSize
= propObj
->getLength();
279 if ( propSize
!= (2 * sizeof(UInt32
)) ) return -1;
281 propPtr
= (unsigned int *)propObj
->getBytesNoCopy();
282 if ( propPtr
== 0 ) return -1;
284 *infoAddr
= (void *)propPtr
[0] ;
285 *infoSize
= (int) propPtr
[1];
290 void IODTFreeLoaderInfo( char *key
, void *infoAddr
, int infoSize
)
292 vm_offset_t range
[2];
293 IORegistryEntry
*chosen
;
295 range
[0] = (vm_offset_t
)infoAddr
;
296 range
[1] = (vm_offset_t
)infoSize
;
297 FreePhysicalMemory( range
);
300 chosen
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
);
302 chosen
->removeProperty(key
);
307 static void FreePhysicalMemory( vm_offset_t
* range
)
311 virt
= ml_static_ptovirt( range
[0] );
313 ml_static_mfree( virt
, range
[1] );
317 static IORegistryEntry
*
318 MakeReferenceTable( DTEntry dtEntry
, bool copy
)
320 IORegistryEntry
*regEntry
;
321 OSDictionary
*propTable
;
322 const OSSymbol
*nameKey
;
325 DTPropertyIterator dtIter
;
330 bool noLocation
= true;
332 regEntry
= new IOService
;
334 if( regEntry
&& (false == regEntry
->init())) {
340 (kSuccess
== DTCreatePropertyIterator( dtEntry
, &dtIter
))) {
342 propTable
= regEntry
->getPropertyTable();
344 while( kSuccess
== DTIterateProperties( dtIter
, &name
)) {
346 if( kSuccess
!= DTGetProperty( dtEntry
, name
, &prop
, &propSize
))
350 nameKey
= OSSymbol::withCString(name
);
351 data
= OSData::withBytes(prop
, propSize
);
353 nameKey
= OSSymbol::withCStringNoCopy(name
);
354 data
= OSData::withBytesNoCopy(prop
, propSize
);
356 assert( nameKey
&& data
);
358 propTable
->setObject( nameKey
, data
);
362 if( nameKey
== gIODTNameKey
) {
364 sym
= OSSymbol::withCString( (const char *) prop
);
366 sym
= OSSymbol::withCStringNoCopy( (const char *) prop
);
367 regEntry
->setName( sym
);
370 } else if( nameKey
== gIODTUnitKey
) {
371 // all OF strings are null terminated... except this one
372 if( propSize
>= (int) sizeof( location
))
373 propSize
= sizeof( location
) - 1;
374 strncpy( location
, (const char *) prop
, propSize
);
375 location
[ propSize
] = 0;
376 regEntry
->setLocation( location
);
377 propTable
->removeObject( gIODTUnitKey
);
380 } else if( noLocation
&& (0 == strcmp( name
, "reg"))) {
381 // default location - override later
382 sprintf( location
, "%lX", *((UInt32
*) prop
) );
383 regEntry
->setLocation( location
);
386 DTDisposePropertyIterator( dtIter
);
392 static void AddPHandle( IORegistryEntry
* regEntry
)
396 if( regEntry
->getProperty( gIODTInterruptCellKey
)
397 && (data
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTPHandleKey
)))) {
398 // a possible interrupt-parent
399 gIODTPHandles
->setObject( data
);
400 gIODTPHandleMap
->setObject( regEntry
);
404 static IORegistryEntry
* FindPHandle( UInt32 phandle
)
407 IORegistryEntry
*regEntry
= 0;
410 for( i
= 0; (data
= (OSData
*)gIODTPHandles
->getObject( i
)); i
++ ) {
411 if( phandle
== *((UInt32
*)data
->getBytesNoCopy())) {
412 regEntry
= (IORegistryEntry
*)
413 gIODTPHandleMap
->getObject( i
);
421 static bool GetUInt32( IORegistryEntry
* regEntry
, const OSSymbol
* name
,
426 if( (data
= OSDynamicCast( OSData
, regEntry
->getProperty( name
)))
427 && (4 == data
->getLength())) {
428 *value
= *((UInt32
*) data
->getBytesNoCopy());
434 IORegistryEntry
* IODTFindInterruptParent( IORegistryEntry
* regEntry
)
436 IORegistryEntry
* parent
;
439 if( GetUInt32( regEntry
, gIODTInterruptParentKey
, &phandle
))
440 parent
= FindPHandle( phandle
);
442 else if( 0 == regEntry
->getProperty( "interrupt-controller"))
443 parent
= regEntry
->getParentEntry( gIODTPlane
);
450 const OSSymbol
* IODTInterruptControllerName( IORegistryEntry
* regEntry
)
457 ok
= GetUInt32( regEntry
, gIODTPHandleKey
, &phandle
);
461 sprintf( buf
, "IOInterruptController%08lX", phandle
);
462 sym
= OSSymbol::withCString( buf
);
469 #define unexpected(a) { kprintf("unexpected %s:%d\n", __FILE__, __LINE__); a; }
471 static void IODTGetICellCounts( IORegistryEntry
* regEntry
,
472 UInt32
* iCellCount
, UInt32
* aCellCount
)
474 if( !GetUInt32( regEntry
, gIODTInterruptCellKey
, iCellCount
))
475 unexpected( *iCellCount
= 1 );
476 if( !GetUInt32( regEntry
, gIODTAddressCellKey
, aCellCount
))
480 UInt32
IODTMapOneInterrupt( IORegistryEntry
* regEntry
, UInt32
* intSpec
,
481 OSData
** spec
, const OSSymbol
** controller
)
483 IORegistryEntry
*parent
= 0;
489 UInt32 acells
, icells
, pacells
, picells
, cell
;
490 UInt32 i
, original_icells
;
491 bool cmp
, ok
= false;
493 parent
= IODTFindInterruptParent( regEntry
);
494 IODTGetICellCounts( parent
, &icells
, &acells
);
497 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "reg" ));
498 if( data
&& (data
->getLength() >= (acells
* sizeof( UInt32
))))
499 addrCmp
= (UInt32
*) data
->getBytesNoCopy();
501 original_icells
= icells
;
506 kprintf ("IODTMapOneInterrupt: current regEntry name %s\n", regEntry
->getName());
507 kprintf ("acells - icells: ");
508 for (i
= 0; i
< acells
; i
++) kprintf ("0x%08X ", addrCmp
[i
]);
510 for (i
= 0; i
< icells
; i
++) kprintf ("0x%08X ", intSpec
[i
]);
514 if( parent
&& (data
= OSDynamicCast( OSData
,
515 regEntry
->getProperty( "interrupt-controller")))) {
516 // found a controller - don't want to follow cascaded controllers
518 *spec
= OSData::withBytesNoCopy( (void *) intSpec
,
519 icells
* sizeof( UInt32
));
520 *controller
= IODTInterruptControllerName( regEntry
);
521 ok
= (*spec
&& *controller
);
522 } else if( parent
&& (data
= OSDynamicCast( OSData
,
523 regEntry
->getProperty( "interrupt-map")))) {
525 map
= (UInt32
*) data
->getBytesNoCopy();
526 endMap
= map
+ (data
->getLength() / sizeof(UInt32
));
527 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "interrupt-map-mask" ));
528 if( data
&& (data
->getLength() >= ((acells
+ icells
) * sizeof( UInt32
))))
529 maskCmp
= (UInt32
*) data
->getBytesNoCopy();
535 kprintf (" maskCmp: ");
536 for (i
= 0; i
< acells
+ icells
; i
++) {
539 kprintf ("0x%08X ", maskCmp
[i
]);
542 kprintf (" masked: ");
543 for (i
= 0; i
< acells
+ icells
; i
++) {
546 kprintf ("0x%08X ", ((i
< acells
) ? addrCmp
[i
] : intSpec
[i
-acells
]) & maskCmp
[i
]);
550 kprintf ("no maskCmp\n");
555 for (i
= 0; i
< acells
+ icells
; i
++) {
558 kprintf ("0x%08X ", map
[i
]);
562 for( i
= 0, cmp
= true; cmp
&& (i
< (acells
+ icells
)); i
++) {
563 cell
= (i
< acells
) ? addrCmp
[i
] : intSpec
[ i
- acells
];
566 cmp
= (cell
== map
[i
]);
569 map
+= acells
+ icells
;
570 if( 0 == (parent
= FindPHandle( *(map
++) )))
573 IODTGetICellCounts( parent
, &picells
, &pacells
);
576 intSpec
= map
+ pacells
;
579 map
+= pacells
+ picells
;
581 } while( !cmp
&& (map
< endMap
) );
587 IODTGetICellCounts( parent
, &icells
, &acells
);
593 return( ok
? original_icells
: 0 );
596 IOReturn
IODTGetInterruptOptions( IORegistryEntry
* regEntry
, int source
, IOOptionBits
* options
)
598 OSArray
* controllers
;
599 OSArray
* specifiers
;
606 controllers
= OSDynamicCast(OSArray
, regEntry
->getProperty(gIOInterruptControllersKey
));
607 specifiers
= OSDynamicCast(OSArray
, regEntry
->getProperty(gIOInterruptSpecifiersKey
));
609 if( !controllers
|| !specifiers
)
610 return (kIOReturnNoInterrupt
);
612 shared
= (OSArray
*) gIODTSharedInterrupts
->getObject(
613 (const OSSymbol
*) controllers
->getObject(source
) );
615 return (kIOReturnSuccess
);
617 spec
= specifiers
->getObject(source
);
619 return (kIOReturnNoInterrupt
);
621 for (unsigned int i
= 0;
622 (oneSpec
= shared
->getObject(i
))
623 && (!oneSpec
->isEqualTo(spec
));
627 *options
= kIODTInterruptShared
;
629 return (kIOReturnSuccess
);
632 static bool IODTMapInterruptsSharing( IORegistryEntry
* regEntry
, OSDictionary
* allInts
)
634 IORegistryEntry
* parent
;
642 OSArray
* controllerInts
;
643 const OSSymbol
* controller
;
644 OSArray
* controllers
;
648 nw
= (0 == (local
= OSDynamicCast( OSData
,
649 regEntry
->getProperty( gIODTAAPLInterruptsKey
))));
650 if( nw
&& (0 == (local
= OSDynamicCast( OSData
,
651 regEntry
->getProperty( "interrupts")))))
652 return( true ); // nothing to see here
654 if( nw
&& (parent
= regEntry
->getParentEntry( gIODTPlane
))) {
655 // check for bridges on old world
656 if( (local2
= OSDynamicCast( OSData
,
657 parent
->getProperty( gIODTAAPLInterruptsKey
)))) {
663 localBits
= (UInt32
*) local
->getBytesNoCopy();
664 localEnd
= localBits
+ (local
->getLength() / sizeof( UInt32
));
665 mapped
= OSArray::withCapacity( 1 );
666 controllers
= OSArray::withCapacity( 1 );
668 ok
= (mapped
&& controllers
);
672 skip
= IODTMapOneInterrupt( regEntry
, localBits
, &map
, &controller
);
674 IOLog("%s: error mapping interrupt[%d]\n",
675 regEntry
->getName(), mapped
->getCount());
679 map
= OSData::withData( local
, mapped
->getCount() * sizeof( UInt32
),
681 controller
= gIODTDefaultInterruptController
;
682 controller
->retain();
686 mapped
->setObject( map
);
687 controllers
->setObject( controller
);
691 controllerInts
= (OSArray
*) allInts
->getObject( controller
);
694 for (unsigned int i
= 0; (oneMap
= controllerInts
->getObject(i
)); i
++)
696 if (map
->isEqualTo(oneMap
))
698 controllerInts
= (OSArray
*) gIODTSharedInterrupts
->getObject( controller
);
700 controllerInts
->setObject(map
);
703 controllerInts
= OSArray::withObjects( (const OSObject
**) &map
, 1, 4 );
706 gIODTSharedInterrupts
->setObject( controller
, controllerInts
);
707 controllerInts
->release();
714 controllerInts
->setObject(map
);
718 controllerInts
= OSArray::withObjects( (const OSObject
**) &map
, 1, 16 );
721 allInts
->setObject( controller
, controllerInts
);
722 controllerInts
->release();
728 controller
->release();
730 } while( localBits
< localEnd
);
732 ok
&= (localBits
== localEnd
);
736 ok
= regEntry
->setProperty( gIOInterruptControllersKey
, controllers
);
737 ok
&= regEntry
->setProperty( gIOInterruptSpecifiersKey
, mapped
);
741 controllers
->release();
748 bool IODTMapInterrupts( IORegistryEntry
* regEntry
)
750 return( IODTMapInterruptsSharing( regEntry
, 0 ));
757 CompareKey( OSString
* key
,
758 const IORegistryEntry
* table
, const OSSymbol
* propName
)
766 const char *lastName
;
769 const char *result
= 0;
771 if( 0 == (prop
= table
->getProperty( propName
)))
774 if( (data
= OSDynamicCast( OSData
, prop
))) {
775 names
= (const char *) data
->getBytesNoCopy();
776 lastName
= names
+ data
->getLength();
777 } else if( (string
= OSDynamicCast( OSString
, prop
))) {
778 names
= string
->getCStringNoCopy();
779 lastName
= names
+ string
->getLength() + 1;
783 ckey
= key
->getCStringNoCopy();
784 keyLen
= key
->getLength();
785 wild
= ('*' == key
->getChar( keyLen
- 1 ));
788 // for each name in the property
790 matched
= (0 == strncmp( ckey
, names
, keyLen
- 1 ));
792 matched
= (keyLen
== strlen( names
))
793 && (0 == strncmp( ckey
, names
, keyLen
));
798 names
= names
+ strlen( names
) + 1;
800 } while( (names
< lastName
) && (false == matched
));
806 bool IODTCompareNubName( const IORegistryEntry
* regEntry
,
807 OSString
* name
, OSString
** matchingName
)
812 matched
= (0 != (result
= CompareKey( name
, regEntry
, gIODTNameKey
)))
813 || (0 != (result
= CompareKey( name
, regEntry
, gIODTCompatibleKey
)))
814 || (0 != (result
= CompareKey( name
, regEntry
, gIODTTypeKey
)))
815 || (0 != (result
= CompareKey( name
, regEntry
, gIODTModelKey
)));
817 if( result
&& matchingName
)
818 *matchingName
= OSString::withCString( result
);
820 return( result
!= 0 );
823 bool IODTMatchNubWithKeys( IORegistryEntry
* regEntry
,
829 obj
= OSUnserialize( keys
, 0 );
832 result
= regEntry
->compareNames( obj
);
836 else IOLog("Couldn't unserialize %s\n", keys
);
842 OSCollectionIterator
* IODTFindMatchingEntries( IORegistryEntry
* from
,
843 IOOptionBits options
, const char * keys
)
846 IORegistryEntry
*next
;
847 IORegistryIterator
*iter
;
848 OSCollectionIterator
*cIter
;
850 bool minus
= options
& kIODTExclusive
;
853 iter
= IORegistryIterator::iterateOver( from
, gIODTPlane
,
854 (options
& kIODTRecursive
) ? kIORegistryIterateRecursively
: 0 );
861 result
= OSSet::withCapacity( 3 );
866 while( (next
= iter
->getNextObject())) {
868 // Look for existence of a debug property to skip
869 if( next
->getProperty("AAPL,ignore"))
873 cmp
= IODTMatchNubWithKeys( next
, keys
);
874 if( (minus
&& (false == cmp
))
875 || ((false == minus
) && (false != cmp
)) )
876 result
->setObject( next
);
878 result
->setObject( next
);
880 } while( !iter
->isValid());
885 cIter
= OSCollectionIterator::withCollection( result
);
892 struct IODTPersistent
{
893 IODTCompareAddressCellFunc compareFunc
;
894 IODTNVLocationFunc locationFunc
;
897 void IODTSetResolving( IORegistryEntry
* regEntry
,
898 IODTCompareAddressCellFunc compareFunc
,
899 IODTNVLocationFunc locationFunc
)
901 IODTPersistent persist
;
904 persist
.compareFunc
= compareFunc
;
905 persist
.locationFunc
= locationFunc
;
906 prop
= OSData::withBytes( &persist
, sizeof( persist
));
910 regEntry
->setProperty( gIODTPersistKey
, prop
);
915 static SInt32
DefaultCompare( UInt32 cellCount
, UInt32 left
[], UInt32 right
[] )
918 return( left
[ cellCount
] - right
[ cellCount
] );
921 void IODTGetCellCounts( IORegistryEntry
* regEntry
,
922 UInt32
* sizeCount
, UInt32
* addressCount
)
924 if( !GetUInt32( regEntry
, gIODTSizeCellKey
, sizeCount
))
926 if( !GetUInt32( regEntry
, gIODTAddressCellKey
, addressCount
))
931 // Given addr & len cells from our child, find it in our ranges property, then
932 // look in our parent to resolve the base of the range for us.
934 // Range[]: child-addr our-addr child-len
935 // #cells: child ours child
937 bool IODTResolveAddressCell( IORegistryEntry
* regEntry
,
939 IOPhysicalAddress
* phys
, IOPhysicalLength
* len
)
941 IORegistryEntry
*parent
;
943 // cells in addresses at regEntry
944 UInt32 sizeCells
, addressCells
;
945 // cells in addresses below regEntry
946 UInt32 childSizeCells
, childAddressCells
;
948 UInt32 cell
[ 5 ], offset
= 0, length
;
955 SInt32 diff
, endDiff
;
957 IODTPersistent
*persist
;
958 IODTCompareAddressCellFunc compare
;
960 IODTGetCellCounts( regEntry
, &childSizeCells
, &childAddressCells
);
961 childCells
= childAddressCells
+ childSizeCells
;
963 bcopy( cellsIn
, cell
, 4 * childCells
);
964 if( childSizeCells
> 1)
965 *len
= IOPhysical32( cellsIn
[ childAddressCells
],
966 cellsIn
[ childAddressCells
+ 1 ] );
968 *len
= IOPhysical32( 0, cellsIn
[ childAddressCells
] );
972 prop
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTRangeKey
));
974 /* end of the road */
975 *phys
= IOPhysical32( 0, cell
[ childAddressCells
- 1 ] + offset
);
979 parent
= regEntry
->getParentEntry( gIODTPlane
);
980 IODTGetCellCounts( parent
, &sizeCells
, &addressCells
);
982 if( (length
= prop
->getLength())) {
984 startRange
= (UInt32
*) prop
->getBytesNoCopy();
986 endRanges
= range
+ (length
/ 4);
988 prop
= (OSData
*) regEntry
->getProperty( gIODTPersistKey
);
990 persist
= (IODTPersistent
*) prop
->getBytesNoCopy();
991 compare
= persist
->compareFunc
;
993 compare
= DefaultCompare
;
997 range
+= (childCells
+ addressCells
) ) {
999 // is cell start >= range start?
1000 diff
= (*compare
)( childAddressCells
, cell
, range
);
1004 ok
= (0 == cell
[childCells
- 1]);
1007 // search for cell end
1008 bcopy(cell
, endCell
, childAddressCells
* sizeof(UInt32
));
1009 endCell
[childAddressCells
- 1] += cell
[childCells
- 1] - 1;
1010 lookRange
= startRange
;
1012 lookRange
< endRanges
;
1013 lookRange
+= (childCells
+ addressCells
) )
1015 // is cell >= range start?
1016 endDiff
= (*compare
)( childAddressCells
, endCell
, lookRange
);
1019 if ((endDiff
- cell
[childCells
- 1] + 1 + lookRange
[childAddressCells
+ addressCells
- 1])
1020 == (diff
+ range
[childAddressCells
+ addressCells
- 1]))
1033 // Get the physical start of the range from our parent
1034 bcopy( range
+ childAddressCells
, cell
, 4 * addressCells
);
1035 bzero( cell
+ addressCells
, 4 * sizeCells
);
1037 } /* else zero length range => pass thru to parent */
1040 childSizeCells
= sizeCells
;
1041 childAddressCells
= addressCells
;
1042 childCells
= childAddressCells
+ childSizeCells
;
1044 while( ok
&& regEntry
);
1050 OSArray
* IODTResolveAddressing( IORegistryEntry
* regEntry
,
1051 const char * addressPropertyName
,
1052 IODeviceMemory
* parent
)
1054 IORegistryEntry
*parentEntry
;
1055 OSData
*addressProperty
;
1056 UInt32 sizeCells
, addressCells
, cells
;
1059 IOPhysicalAddress phys
;
1060 IOPhysicalLength len
;
1062 IODeviceMemory
*range
;
1064 parentEntry
= regEntry
->getParentEntry( gIODTPlane
);
1065 addressProperty
= (OSData
*) regEntry
->getProperty( addressPropertyName
);
1066 if( (0 == addressProperty
) || (0 == parentEntry
))
1069 IODTGetCellCounts( parentEntry
, &sizeCells
, &addressCells
);
1073 cells
= sizeCells
+ addressCells
;
1074 reg
= (UInt32
*) addressProperty
->getBytesNoCopy();
1075 num
= addressProperty
->getLength() / (4 * cells
);
1077 array
= OSArray::withCapacity( 1 );
1081 for( i
= 0; i
< num
; i
++) {
1082 if( IODTResolveAddressCell( parentEntry
, reg
, &phys
, &len
)) {
1085 range
= IODeviceMemory::withSubRange( parent
,
1086 phys
- parent
->getPhysicalAddress(), len
);
1088 range
= IODeviceMemory::withRange( phys
, len
);
1090 array
->setObject( range
);
1095 regEntry
->setProperty( gIODeviceMemoryKey
, array
);
1096 array
->release(); /* ??? */
1101 static void IODTGetNVLocation(
1102 IORegistryEntry
* parent
,
1103 IORegistryEntry
* regEntry
,
1104 UInt8
* busNum
, UInt8
* deviceNum
, UInt8
* functionNum
)
1108 IODTPersistent
*persist
;
1111 prop
= (OSData
*) parent
->getProperty( gIODTPersistKey
);
1113 persist
= (IODTPersistent
*) prop
->getBytesNoCopy();
1114 (*persist
->locationFunc
)( regEntry
, busNum
, deviceNum
, functionNum
);
1116 prop
= (OSData
*) regEntry
->getProperty( "reg" );
1119 cell
= (UInt32
*) prop
->getBytesNoCopy();
1121 *deviceNum
= 0x1f & (cell
[ 0 ] >> 24);
1131 * Try to make the same messed up descriptor as Mac OS
1134 IOReturn
IODTMakeNVDescriptor( IORegistryEntry
* regEntry
,
1135 IONVRAMDescriptor
* hdr
)
1137 IORegistryEntry
*parent
;
1139 UInt32 bridgeDevices
;
1147 for(level
= 0, bridgeDevices
= 0;
1148 (parent
= regEntry
->getParentEntry( gIODTPlane
)) && (level
< 7); level
++ ) {
1150 IODTGetNVLocation( parent
, regEntry
,
1151 &busNum
, &deviceNum
, &functionNum
);
1153 bridgeDevices
|= ((deviceNum
& 0x1f) << ((level
- 1) * 5));
1155 hdr
->busNum
= busNum
;
1156 hdr
->deviceNum
= deviceNum
;
1157 hdr
->functionNum
= functionNum
;
1161 hdr
->bridgeCount
= level
- 2;
1162 hdr
->bridgeDevices
= bridgeDevices
;
1164 return( kIOReturnSuccess
);
1167 OSData
* IODTFindSlotName( IORegistryEntry
* regEntry
, UInt32 deviceNumber
)
1169 IORegistryEntry
*parent
;
1178 data
= (OSData
*) regEntry
->getProperty("AAPL,slot-name");
1181 parent
= regEntry
->getParentEntry( gIODTPlane
);
1184 data
= OSDynamicCast( OSData
, parent
->getProperty("slot-names"));
1187 if( data
->getLength() <= 4)
1190 bits
= (UInt32
*) data
->getBytesNoCopy();
1192 if( (0 == (mask
& (1 << deviceNumber
))))
1195 names
= (char *)(bits
+ 1);
1196 lastName
= names
+ (data
->getLength() - 4);
1198 for( i
= 0; (i
<= deviceNumber
) && (names
< lastName
); i
++ ) {
1200 if( mask
& (1 << i
)) {
1201 if( i
== deviceNumber
) {
1202 data
= OSData::withBytesNoCopy( names
, 1 + strlen( names
));
1204 regEntry
->setProperty("AAPL,slot-name", data
);
1209 names
+= 1 + strlen( names
);
1216 extern "C" IOReturn
IONDRVLibrariesInitialize( IOService
* provider
)
1218 return( kIOReturnUnsupported
);