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 #if defined (__i386__)
312 virt
= ml_boot_ptovirt( range
[0] );
314 virt
= ml_static_ptovirt( range
[0] );
317 ml_static_mfree( virt
, range
[1] );
321 static IORegistryEntry
*
322 MakeReferenceTable( DTEntry dtEntry
, bool copy
)
324 IORegistryEntry
*regEntry
;
325 OSDictionary
*propTable
;
326 const OSSymbol
*nameKey
;
329 DTPropertyIterator dtIter
;
334 bool noLocation
= true;
336 regEntry
= new IOService
;
338 if( regEntry
&& (false == regEntry
->init())) {
344 (kSuccess
== DTCreatePropertyIterator( dtEntry
, &dtIter
))) {
346 propTable
= regEntry
->getPropertyTable();
348 while( kSuccess
== DTIterateProperties( dtIter
, &name
)) {
350 if( kSuccess
!= DTGetProperty( dtEntry
, name
, &prop
, &propSize
))
354 nameKey
= OSSymbol::withCString(name
);
355 data
= OSData::withBytes(prop
, propSize
);
357 nameKey
= OSSymbol::withCStringNoCopy(name
);
358 data
= OSData::withBytesNoCopy(prop
, propSize
);
360 assert( nameKey
&& data
);
362 propTable
->setObject( nameKey
, data
);
366 if( nameKey
== gIODTNameKey
) {
368 sym
= OSSymbol::withCString( (const char *) prop
);
370 sym
= OSSymbol::withCStringNoCopy( (const char *) prop
);
371 regEntry
->setName( sym
);
374 } else if( nameKey
== gIODTUnitKey
) {
375 // all OF strings are null terminated... except this one
376 if( propSize
>= (int) sizeof( location
))
377 propSize
= sizeof( location
) - 1;
378 strncpy( location
, (const char *) prop
, propSize
);
379 location
[ propSize
] = 0;
380 regEntry
->setLocation( location
);
381 propTable
->removeObject( gIODTUnitKey
);
384 } else if( noLocation
&& (0 == strcmp( name
, "reg"))) {
385 // default location - override later
386 sprintf( location
, "%lX", *((UInt32
*) prop
) );
387 regEntry
->setLocation( location
);
390 DTDisposePropertyIterator( dtIter
);
396 static void AddPHandle( IORegistryEntry
* regEntry
)
400 if( regEntry
->getProperty( gIODTInterruptCellKey
)
401 && (data
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTPHandleKey
)))) {
402 // a possible interrupt-parent
403 gIODTPHandles
->setObject( data
);
404 gIODTPHandleMap
->setObject( regEntry
);
408 static IORegistryEntry
* FindPHandle( UInt32 phandle
)
411 IORegistryEntry
*regEntry
= 0;
414 for( i
= 0; (data
= (OSData
*)gIODTPHandles
->getObject( i
)); i
++ ) {
415 if( phandle
== *((UInt32
*)data
->getBytesNoCopy())) {
416 regEntry
= (IORegistryEntry
*)
417 gIODTPHandleMap
->getObject( i
);
425 static bool GetUInt32( IORegistryEntry
* regEntry
, const OSSymbol
* name
,
430 if( (data
= OSDynamicCast( OSData
, regEntry
->getProperty( name
)))
431 && (4 == data
->getLength())) {
432 *value
= *((UInt32
*) data
->getBytesNoCopy());
438 IORegistryEntry
* IODTFindInterruptParent( IORegistryEntry
* regEntry
)
440 IORegistryEntry
* parent
;
443 if( GetUInt32( regEntry
, gIODTInterruptParentKey
, &phandle
))
444 parent
= FindPHandle( phandle
);
446 else if( 0 == regEntry
->getProperty( "interrupt-controller"))
447 parent
= regEntry
->getParentEntry( gIODTPlane
);
454 const OSSymbol
* IODTInterruptControllerName( IORegistryEntry
* regEntry
)
461 ok
= GetUInt32( regEntry
, gIODTPHandleKey
, &phandle
);
465 sprintf( buf
, "IOInterruptController%08lX", phandle
);
466 sym
= OSSymbol::withCString( buf
);
473 #define unexpected(a) { kprintf("unexpected %s:%d\n", __FILE__, __LINE__); a; }
475 static void IODTGetICellCounts( IORegistryEntry
* regEntry
,
476 UInt32
* iCellCount
, UInt32
* aCellCount
)
478 if( !GetUInt32( regEntry
, gIODTInterruptCellKey
, iCellCount
))
479 unexpected( *iCellCount
= 1 );
480 if( !GetUInt32( regEntry
, gIODTAddressCellKey
, aCellCount
))
484 UInt32
IODTMapOneInterrupt( IORegistryEntry
* regEntry
, UInt32
* intSpec
,
485 OSData
** spec
, const OSSymbol
** controller
)
487 IORegistryEntry
*parent
= 0;
493 UInt32 acells
, icells
, pacells
, picells
, cell
;
494 UInt32 i
, original_icells
;
495 bool cmp
, ok
= false;
497 parent
= IODTFindInterruptParent( regEntry
);
498 IODTGetICellCounts( parent
, &icells
, &acells
);
501 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "reg" ));
502 if( data
&& (data
->getLength() >= (acells
* sizeof( UInt32
))))
503 addrCmp
= (UInt32
*) data
->getBytesNoCopy();
505 original_icells
= icells
;
510 kprintf ("IODTMapOneInterrupt: current regEntry name %s\n", regEntry
->getName());
511 kprintf ("acells - icells: ");
512 for (i
= 0; i
< acells
; i
++) kprintf ("0x%08X ", addrCmp
[i
]);
514 for (i
= 0; i
< icells
; i
++) kprintf ("0x%08X ", intSpec
[i
]);
518 if( parent
&& (data
= OSDynamicCast( OSData
,
519 regEntry
->getProperty( "interrupt-controller")))) {
520 // found a controller - don't want to follow cascaded controllers
522 *spec
= OSData::withBytesNoCopy( (void *) intSpec
,
523 icells
* sizeof( UInt32
));
524 *controller
= IODTInterruptControllerName( regEntry
);
525 ok
= (*spec
&& *controller
);
526 } else if( parent
&& (data
= OSDynamicCast( OSData
,
527 regEntry
->getProperty( "interrupt-map")))) {
529 map
= (UInt32
*) data
->getBytesNoCopy();
530 endMap
= map
+ (data
->getLength() / sizeof(UInt32
));
531 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "interrupt-map-mask" ));
532 if( data
&& (data
->getLength() >= ((acells
+ icells
) * sizeof( UInt32
))))
533 maskCmp
= (UInt32
*) data
->getBytesNoCopy();
539 kprintf (" maskCmp: ");
540 for (i
= 0; i
< acells
+ icells
; i
++) {
543 kprintf ("0x%08X ", maskCmp
[i
]);
546 kprintf (" masked: ");
547 for (i
= 0; i
< acells
+ icells
; i
++) {
550 kprintf ("0x%08X ", ((i
< acells
) ? addrCmp
[i
] : intSpec
[i
-acells
]) & maskCmp
[i
]);
554 kprintf ("no maskCmp\n");
559 for (i
= 0; i
< acells
+ icells
; i
++) {
562 kprintf ("0x%08X ", map
[i
]);
566 for( i
= 0, cmp
= true; cmp
&& (i
< (acells
+ icells
)); i
++) {
567 cell
= (i
< acells
) ? addrCmp
[i
] : intSpec
[ i
- acells
];
570 cmp
= (cell
== map
[i
]);
573 map
+= acells
+ icells
;
574 if( 0 == (parent
= FindPHandle( *(map
++) )))
577 IODTGetICellCounts( parent
, &picells
, &pacells
);
580 intSpec
= map
+ pacells
;
583 map
+= pacells
+ picells
;
585 } while( !cmp
&& (map
< endMap
) );
591 IODTGetICellCounts( parent
, &icells
, &acells
);
597 return( ok
? original_icells
: 0 );
600 IOReturn
IODTGetInterruptOptions( IORegistryEntry
* regEntry
, int source
, IOOptionBits
* options
)
602 OSArray
* controllers
;
603 OSArray
* specifiers
;
610 controllers
= OSDynamicCast(OSArray
, regEntry
->getProperty(gIOInterruptControllersKey
));
611 specifiers
= OSDynamicCast(OSArray
, regEntry
->getProperty(gIOInterruptSpecifiersKey
));
613 if( !controllers
|| !specifiers
)
614 return (kIOReturnNoInterrupt
);
616 shared
= (OSArray
*) gIODTSharedInterrupts
->getObject(
617 (const OSSymbol
*) controllers
->getObject(source
) );
619 return (kIOReturnSuccess
);
621 spec
= specifiers
->getObject(source
);
623 return (kIOReturnNoInterrupt
);
625 for (unsigned int i
= 0;
626 (oneSpec
= shared
->getObject(i
))
627 && (!oneSpec
->isEqualTo(spec
));
631 *options
= kIODTInterruptShared
;
633 return (kIOReturnSuccess
);
636 static bool IODTMapInterruptsSharing( IORegistryEntry
* regEntry
, OSDictionary
* allInts
)
638 IORegistryEntry
* parent
;
646 OSArray
* controllerInts
;
647 const OSSymbol
* controller
;
648 OSArray
* controllers
;
652 nw
= (0 == (local
= OSDynamicCast( OSData
,
653 regEntry
->getProperty( gIODTAAPLInterruptsKey
))));
654 if( nw
&& (0 == (local
= OSDynamicCast( OSData
,
655 regEntry
->getProperty( "interrupts")))))
656 return( true ); // nothing to see here
658 if( nw
&& (parent
= regEntry
->getParentEntry( gIODTPlane
))) {
659 // check for bridges on old world
660 if( (local2
= OSDynamicCast( OSData
,
661 parent
->getProperty( gIODTAAPLInterruptsKey
)))) {
667 localBits
= (UInt32
*) local
->getBytesNoCopy();
668 localEnd
= localBits
+ (local
->getLength() / sizeof( UInt32
));
669 mapped
= OSArray::withCapacity( 1 );
670 controllers
= OSArray::withCapacity( 1 );
672 ok
= (mapped
&& controllers
);
676 skip
= IODTMapOneInterrupt( regEntry
, localBits
, &map
, &controller
);
678 IOLog("%s: error mapping interrupt[%d]\n",
679 regEntry
->getName(), mapped
->getCount());
683 map
= OSData::withData( local
, mapped
->getCount() * sizeof( UInt32
),
685 controller
= gIODTDefaultInterruptController
;
686 controller
->retain();
690 mapped
->setObject( map
);
691 controllers
->setObject( controller
);
695 controllerInts
= (OSArray
*) allInts
->getObject( controller
);
698 for (unsigned int i
= 0; (oneMap
= controllerInts
->getObject(i
)); i
++)
700 if (map
->isEqualTo(oneMap
))
702 controllerInts
= (OSArray
*) gIODTSharedInterrupts
->getObject( controller
);
704 controllerInts
->setObject(map
);
707 controllerInts
= OSArray::withObjects( (const OSObject
**) &map
, 1, 4 );
710 gIODTSharedInterrupts
->setObject( controller
, controllerInts
);
711 controllerInts
->release();
718 controllerInts
->setObject(map
);
722 controllerInts
= OSArray::withObjects( (const OSObject
**) &map
, 1, 16 );
725 allInts
->setObject( controller
, controllerInts
);
726 controllerInts
->release();
732 controller
->release();
734 } while( localBits
< localEnd
);
736 ok
&= (localBits
== localEnd
);
740 ok
= regEntry
->setProperty( gIOInterruptControllersKey
, controllers
);
741 ok
&= regEntry
->setProperty( gIOInterruptSpecifiersKey
, mapped
);
745 controllers
->release();
752 bool IODTMapInterrupts( IORegistryEntry
* regEntry
)
754 return( IODTMapInterruptsSharing( regEntry
, 0 ));
761 CompareKey( OSString
* key
,
762 const IORegistryEntry
* table
, const OSSymbol
* propName
)
770 const char *lastName
;
773 const char *result
= 0;
775 if( 0 == (prop
= table
->getProperty( propName
)))
778 if( (data
= OSDynamicCast( OSData
, prop
))) {
779 names
= (const char *) data
->getBytesNoCopy();
780 lastName
= names
+ data
->getLength();
781 } else if( (string
= OSDynamicCast( OSString
, prop
))) {
782 names
= string
->getCStringNoCopy();
783 lastName
= names
+ string
->getLength() + 1;
787 ckey
= key
->getCStringNoCopy();
788 keyLen
= key
->getLength();
789 wild
= ('*' == key
->getChar( keyLen
- 1 ));
792 // for each name in the property
794 matched
= (0 == strncmp( ckey
, names
, keyLen
- 1 ));
796 matched
= (keyLen
== strlen( names
))
797 && (0 == strncmp( ckey
, names
, keyLen
));
802 names
= names
+ strlen( names
) + 1;
804 } while( (names
< lastName
) && (false == matched
));
810 bool IODTCompareNubName( const IORegistryEntry
* regEntry
,
811 OSString
* name
, OSString
** matchingName
)
816 matched
= (0 != (result
= CompareKey( name
, regEntry
, gIODTNameKey
)))
817 || (0 != (result
= CompareKey( name
, regEntry
, gIODTCompatibleKey
)))
818 || (0 != (result
= CompareKey( name
, regEntry
, gIODTTypeKey
)))
819 || (0 != (result
= CompareKey( name
, regEntry
, gIODTModelKey
)));
821 if( result
&& matchingName
)
822 *matchingName
= OSString::withCString( result
);
824 return( result
!= 0 );
827 bool IODTMatchNubWithKeys( IORegistryEntry
* regEntry
,
833 obj
= OSUnserialize( keys
, 0 );
836 result
= regEntry
->compareNames( obj
);
840 else IOLog("Couldn't unserialize %s\n", keys
);
846 OSCollectionIterator
* IODTFindMatchingEntries( IORegistryEntry
* from
,
847 IOOptionBits options
, const char * keys
)
850 IORegistryEntry
*next
;
851 IORegistryIterator
*iter
;
852 OSCollectionIterator
*cIter
;
854 bool minus
= options
& kIODTExclusive
;
857 iter
= IORegistryIterator::iterateOver( from
, gIODTPlane
,
858 (options
& kIODTRecursive
) ? kIORegistryIterateRecursively
: 0 );
865 result
= OSSet::withCapacity( 3 );
870 while( (next
= iter
->getNextObject())) {
872 // Look for existence of a debug property to skip
873 if( next
->getProperty("AAPL,ignore"))
877 cmp
= IODTMatchNubWithKeys( next
, keys
);
878 if( (minus
&& (false == cmp
))
879 || ((false == minus
) && (false != cmp
)) )
880 result
->setObject( next
);
882 result
->setObject( next
);
884 } while( !iter
->isValid());
889 cIter
= OSCollectionIterator::withCollection( result
);
896 struct IODTPersistent
{
897 IODTCompareAddressCellFunc compareFunc
;
898 IODTNVLocationFunc locationFunc
;
901 void IODTSetResolving( IORegistryEntry
* regEntry
,
902 IODTCompareAddressCellFunc compareFunc
,
903 IODTNVLocationFunc locationFunc
)
905 IODTPersistent persist
;
908 persist
.compareFunc
= compareFunc
;
909 persist
.locationFunc
= locationFunc
;
910 prop
= OSData::withBytes( &persist
, sizeof( persist
));
914 regEntry
->setProperty( gIODTPersistKey
, prop
);
919 static SInt32
DefaultCompare( UInt32 cellCount
, UInt32 left
[], UInt32 right
[] )
922 return( left
[ cellCount
] - right
[ cellCount
] );
925 void IODTGetCellCounts( IORegistryEntry
* regEntry
,
926 UInt32
* sizeCount
, UInt32
* addressCount
)
928 if( !GetUInt32( regEntry
, gIODTSizeCellKey
, sizeCount
))
930 if( !GetUInt32( regEntry
, gIODTAddressCellKey
, addressCount
))
935 // Given addr & len cells from our child, find it in our ranges property, then
936 // look in our parent to resolve the base of the range for us.
938 // Range[]: child-addr our-addr child-len
939 // #cells: child ours child
941 bool IODTResolveAddressCell( IORegistryEntry
* regEntry
,
943 IOPhysicalAddress
* phys
, IOPhysicalLength
* len
)
945 IORegistryEntry
*parent
;
947 // cells in addresses at regEntry
948 UInt32 sizeCells
, addressCells
;
949 // cells in addresses below regEntry
950 UInt32 childSizeCells
, childAddressCells
;
952 UInt32 cell
[ 5 ], offset
= 0, length
;
959 SInt32 diff
, endDiff
;
961 IODTPersistent
*persist
;
962 IODTCompareAddressCellFunc compare
;
964 IODTGetCellCounts( regEntry
, &childSizeCells
, &childAddressCells
);
965 childCells
= childAddressCells
+ childSizeCells
;
967 bcopy( cellsIn
, cell
, 4 * childCells
);
968 if( childSizeCells
> 1)
969 *len
= IOPhysical32( cellsIn
[ childAddressCells
],
970 cellsIn
[ childAddressCells
+ 1 ] );
972 *len
= IOPhysical32( 0, cellsIn
[ childAddressCells
] );
976 prop
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTRangeKey
));
978 /* end of the road */
979 *phys
= IOPhysical32( 0, cell
[ childAddressCells
- 1 ] + offset
);
983 parent
= regEntry
->getParentEntry( gIODTPlane
);
984 IODTGetCellCounts( parent
, &sizeCells
, &addressCells
);
986 if( (length
= prop
->getLength())) {
988 startRange
= (UInt32
*) prop
->getBytesNoCopy();
990 endRanges
= range
+ (length
/ 4);
992 prop
= (OSData
*) regEntry
->getProperty( gIODTPersistKey
);
994 persist
= (IODTPersistent
*) prop
->getBytesNoCopy();
995 compare
= persist
->compareFunc
;
997 compare
= DefaultCompare
;
1001 range
+= (childCells
+ addressCells
) ) {
1003 // is cell start >= range start?
1004 diff
= (*compare
)( childAddressCells
, cell
, range
);
1008 ok
= (0 == cell
[childCells
- 1]);
1011 // search for cell end
1012 bcopy(cell
, endCell
, childAddressCells
* sizeof(UInt32
));
1013 endCell
[childAddressCells
- 1] += cell
[childCells
- 1] - 1;
1014 lookRange
= startRange
;
1016 lookRange
< endRanges
;
1017 lookRange
+= (childCells
+ addressCells
) )
1019 // is cell >= range start?
1020 endDiff
= (*compare
)( childAddressCells
, endCell
, lookRange
);
1023 if ((endDiff
- cell
[childCells
- 1] + 1 + lookRange
[childAddressCells
+ addressCells
- 1])
1024 == (diff
+ range
[childAddressCells
+ addressCells
- 1]))
1037 // Get the physical start of the range from our parent
1038 bcopy( range
+ childAddressCells
, cell
, 4 * addressCells
);
1039 bzero( cell
+ addressCells
, 4 * sizeCells
);
1041 } /* else zero length range => pass thru to parent */
1044 childSizeCells
= sizeCells
;
1045 childAddressCells
= addressCells
;
1046 childCells
= childAddressCells
+ childSizeCells
;
1048 while( ok
&& regEntry
);
1054 OSArray
* IODTResolveAddressing( IORegistryEntry
* regEntry
,
1055 const char * addressPropertyName
,
1056 IODeviceMemory
* parent
)
1058 IORegistryEntry
*parentEntry
;
1059 OSData
*addressProperty
;
1060 UInt32 sizeCells
, addressCells
, cells
;
1063 IOPhysicalAddress phys
;
1064 IOPhysicalLength len
;
1066 IODeviceMemory
*range
;
1068 parentEntry
= regEntry
->getParentEntry( gIODTPlane
);
1069 addressProperty
= (OSData
*) regEntry
->getProperty( addressPropertyName
);
1070 if( (0 == addressProperty
) || (0 == parentEntry
))
1073 IODTGetCellCounts( parentEntry
, &sizeCells
, &addressCells
);
1077 cells
= sizeCells
+ addressCells
;
1078 reg
= (UInt32
*) addressProperty
->getBytesNoCopy();
1079 num
= addressProperty
->getLength() / (4 * cells
);
1081 array
= OSArray::withCapacity( 1 );
1085 for( i
= 0; i
< num
; i
++) {
1086 if( IODTResolveAddressCell( parentEntry
, reg
, &phys
, &len
)) {
1089 range
= IODeviceMemory::withSubRange( parent
,
1090 phys
- parent
->getPhysicalAddress(), len
);
1092 range
= IODeviceMemory::withRange( phys
, len
);
1094 array
->setObject( range
);
1099 regEntry
->setProperty( gIODeviceMemoryKey
, array
);
1100 array
->release(); /* ??? */
1105 static void IODTGetNVLocation(
1106 IORegistryEntry
* parent
,
1107 IORegistryEntry
* regEntry
,
1108 UInt8
* busNum
, UInt8
* deviceNum
, UInt8
* functionNum
)
1112 IODTPersistent
*persist
;
1115 prop
= (OSData
*) parent
->getProperty( gIODTPersistKey
);
1117 persist
= (IODTPersistent
*) prop
->getBytesNoCopy();
1118 (*persist
->locationFunc
)( regEntry
, busNum
, deviceNum
, functionNum
);
1120 prop
= (OSData
*) regEntry
->getProperty( "reg" );
1123 cell
= (UInt32
*) prop
->getBytesNoCopy();
1125 *deviceNum
= 0x1f & (cell
[ 0 ] >> 24);
1135 * Try to make the same messed up descriptor as Mac OS
1138 IOReturn
IODTMakeNVDescriptor( IORegistryEntry
* regEntry
,
1139 IONVRAMDescriptor
* hdr
)
1141 IORegistryEntry
*parent
;
1143 UInt32 bridgeDevices
;
1151 for(level
= 0, bridgeDevices
= 0;
1152 (parent
= regEntry
->getParentEntry( gIODTPlane
)) && (level
< 7); level
++ ) {
1154 IODTGetNVLocation( parent
, regEntry
,
1155 &busNum
, &deviceNum
, &functionNum
);
1157 bridgeDevices
|= ((deviceNum
& 0x1f) << ((level
- 1) * 5));
1159 hdr
->busNum
= busNum
;
1160 hdr
->deviceNum
= deviceNum
;
1161 hdr
->functionNum
= functionNum
;
1165 hdr
->bridgeCount
= level
- 2;
1166 hdr
->bridgeDevices
= bridgeDevices
;
1168 return( kIOReturnSuccess
);
1171 OSData
* IODTFindSlotName( IORegistryEntry
* regEntry
, UInt32 deviceNumber
)
1173 IORegistryEntry
*parent
;
1182 data
= (OSData
*) regEntry
->getProperty("AAPL,slot-name");
1185 parent
= regEntry
->getParentEntry( gIODTPlane
);
1188 data
= OSDynamicCast( OSData
, parent
->getProperty("slot-names"));
1191 if( data
->getLength() <= 4)
1194 bits
= (UInt32
*) data
->getBytesNoCopy();
1196 if( (0 == (mask
& (1 << deviceNumber
))))
1199 names
= (char *)(bits
+ 1);
1200 lastName
= names
+ (data
->getLength() - 4);
1202 for( i
= 0; (i
<= deviceNumber
) && (names
< lastName
); i
++ ) {
1204 if( mask
& (1 << i
)) {
1205 if( i
== deviceNumber
) {
1206 data
= OSData::withBytesNoCopy( names
, 1 + strlen( names
));
1208 regEntry
->setProperty("AAPL,slot-name", data
);
1213 names
+= 1 + strlen( names
);
1220 extern "C" IOReturn
IONDRVLibrariesInitialize( IOService
* provider
)
1222 return( kIOReturnUnsupported
);