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
;
95 OSDictionary
* allInts
;
97 unsigned int propSize
;
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 || (strncmp("display", (char *)prop
->getBytesNoCopy(), sizeof("display"))) ) {
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
;
331 unsigned int propSize
;
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
&& (!strncmp(name
, "reg", sizeof("reg")))) {
385 // default location - override later
386 snprintf(location
, sizeof(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 static IORegistryEntry
* IODTFindInterruptParent( IORegistryEntry
* regEntry
, IOItemCount index
)
440 IORegistryEntry
* parent
;
445 if( (data
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTInterruptParentKey
)))
446 && (sizeof(UInt32
) <= (len
= data
->getLength()))) {
447 if (((index
+ 1) * sizeof(UInt32
)) > len
)
449 phandle
= ((UInt32
*) data
->getBytesNoCopy())[index
];
450 parent
= FindPHandle( phandle
);
452 } else if( 0 == regEntry
->getProperty( "interrupt-controller"))
453 parent
= regEntry
->getParentEntry( gIODTPlane
);
460 const OSSymbol
* IODTInterruptControllerName( IORegistryEntry
* regEntry
)
467 ok
= GetUInt32( regEntry
, gIODTPHandleKey
, &phandle
);
471 snprintf(buf
, sizeof(buf
), "IOInterruptController%08lX", phandle
);
472 sym
= OSSymbol::withCString( buf
);
479 #define unexpected(a) { kprintf("unexpected %s:%d\n", __FILE__, __LINE__); a; }
481 static void IODTGetICellCounts( IORegistryEntry
* regEntry
,
482 UInt32
* iCellCount
, UInt32
* aCellCount
)
484 if( !GetUInt32( regEntry
, gIODTInterruptCellKey
, iCellCount
))
485 unexpected( *iCellCount
= 1 );
486 if( !GetUInt32( regEntry
, gIODTAddressCellKey
, aCellCount
))
490 static UInt32
IODTMapOneInterrupt( IORegistryEntry
* regEntry
, UInt32
* intSpec
, UInt32 index
,
491 OSData
** spec
, const OSSymbol
** controller
)
493 IORegistryEntry
*parent
= 0;
499 UInt32 acells
, icells
, pacells
, picells
, cell
;
500 UInt32 i
, original_icells
;
501 bool cmp
, ok
= false;
503 parent
= IODTFindInterruptParent( regEntry
, index
);
504 IODTGetICellCounts( parent
, &icells
, &acells
);
507 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "reg" ));
508 if( data
&& (data
->getLength() >= (acells
* sizeof(UInt32
))))
509 addrCmp
= (UInt32
*) data
->getBytesNoCopy();
511 original_icells
= icells
;
516 kprintf ("IODTMapOneInterrupt: current regEntry name %s\n", regEntry
->getName());
517 kprintf ("acells - icells: ");
518 for (i
= 0; i
< acells
; i
++) kprintf ("0x%08X ", addrCmp
[i
]);
520 for (i
= 0; i
< icells
; i
++) kprintf ("0x%08X ", intSpec
[i
]);
524 if( parent
&& (data
= OSDynamicCast( OSData
,
525 regEntry
->getProperty( "interrupt-controller")))) {
526 // found a controller - don't want to follow cascaded controllers
528 *spec
= OSData::withBytesNoCopy( (void *) intSpec
,
529 icells
* sizeof(UInt32
));
530 *controller
= IODTInterruptControllerName( regEntry
);
531 ok
= (*spec
&& *controller
);
532 } else if( parent
&& (data
= OSDynamicCast( OSData
,
533 regEntry
->getProperty( "interrupt-map")))) {
535 map
= (UInt32
*) data
->getBytesNoCopy();
536 endMap
= map
+ (data
->getLength() / sizeof(UInt32
));
537 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "interrupt-map-mask" ));
538 if( data
&& (data
->getLength() >= ((acells
+ icells
) * sizeof(UInt32
))))
539 maskCmp
= (UInt32
*) data
->getBytesNoCopy();
545 kprintf (" maskCmp: ");
546 for (i
= 0; i
< acells
+ icells
; i
++) {
549 kprintf ("0x%08X ", maskCmp
[i
]);
552 kprintf (" masked: ");
553 for (i
= 0; i
< acells
+ icells
; i
++) {
556 kprintf ("0x%08X ", ((i
< acells
) ? addrCmp
[i
] : intSpec
[i
-acells
]) & maskCmp
[i
]);
560 kprintf ("no maskCmp\n");
565 for (i
= 0; i
< acells
+ icells
; i
++) {
568 kprintf ("0x%08X ", map
[i
]);
572 for( i
= 0, cmp
= true; cmp
&& (i
< (acells
+ icells
)); i
++) {
573 cell
= (i
< acells
) ? addrCmp
[i
] : intSpec
[ i
- acells
];
576 cmp
= (cell
== map
[i
]);
579 map
+= acells
+ icells
;
580 if( 0 == (parent
= FindPHandle( *(map
++) )))
583 IODTGetICellCounts( parent
, &picells
, &pacells
);
586 intSpec
= map
+ pacells
;
589 map
+= pacells
+ picells
;
591 } while( !cmp
&& (map
< endMap
) );
597 IODTGetICellCounts( parent
, &icells
, &acells
);
603 return( ok
? original_icells
: 0 );
606 IOReturn
IODTGetInterruptOptions( IORegistryEntry
* regEntry
, int source
, IOOptionBits
* options
)
608 OSArray
* controllers
;
609 OSArray
* specifiers
;
616 controllers
= OSDynamicCast(OSArray
, regEntry
->getProperty(gIOInterruptControllersKey
));
617 specifiers
= OSDynamicCast(OSArray
, regEntry
->getProperty(gIOInterruptSpecifiersKey
));
619 if( !controllers
|| !specifiers
)
620 return (kIOReturnNoInterrupt
);
622 shared
= (OSArray
*) gIODTSharedInterrupts
->getObject(
623 (const OSSymbol
*) controllers
->getObject(source
) );
625 return (kIOReturnSuccess
);
627 spec
= specifiers
->getObject(source
);
629 return (kIOReturnNoInterrupt
);
631 for (unsigned int i
= 0;
632 (oneSpec
= shared
->getObject(i
))
633 && (!oneSpec
->isEqualTo(spec
));
637 *options
= kIODTInterruptShared
;
639 return (kIOReturnSuccess
);
642 static bool IODTMapInterruptsSharing( IORegistryEntry
* regEntry
, OSDictionary
* allInts
)
644 IORegistryEntry
* parent
;
653 OSArray
* controllerInts
;
654 const OSSymbol
* controller
= 0;
655 OSArray
* controllers
;
659 nw
= (0 == (local
= OSDynamicCast( OSData
,
660 regEntry
->getProperty( gIODTAAPLInterruptsKey
))));
661 if( nw
&& (0 == (local
= OSDynamicCast( OSData
,
662 regEntry
->getProperty( "interrupts")))))
663 return( true ); // nothing to see here
665 if( nw
&& (parent
= regEntry
->getParentEntry( gIODTPlane
))) {
666 // check for bridges on old world
667 if( (local2
= OSDynamicCast( OSData
,
668 parent
->getProperty( gIODTAAPLInterruptsKey
)))) {
674 localBits
= (UInt32
*) local
->getBytesNoCopy();
675 localEnd
= localBits
+ (local
->getLength() / sizeof(UInt32
));
677 mapped
= OSArray::withCapacity( 1 );
678 controllers
= OSArray::withCapacity( 1 );
680 ok
= (mapped
&& controllers
);
684 skip
= IODTMapOneInterrupt( regEntry
, localBits
, index
, &map
, &controller
);
686 IOLog("%s: error mapping interrupt[%d]\n",
687 regEntry
->getName(), mapped
->getCount());
691 map
= OSData::withData( local
, mapped
->getCount() * sizeof(UInt32
),
693 controller
= gIODTDefaultInterruptController
;
694 controller
->retain();
699 mapped
->setObject( map
);
700 controllers
->setObject( controller
);
704 controllerInts
= (OSArray
*) allInts
->getObject( controller
);
707 for (unsigned int i
= 0; (oneMap
= controllerInts
->getObject(i
)); i
++)
709 if (map
->isEqualTo(oneMap
))
711 controllerInts
= (OSArray
*) gIODTSharedInterrupts
->getObject( controller
);
713 controllerInts
->setObject(map
);
716 controllerInts
= OSArray::withObjects( (const OSObject
**) &map
, 1, 4 );
719 gIODTSharedInterrupts
->setObject( controller
, controllerInts
);
720 controllerInts
->release();
727 controllerInts
->setObject(map
);
731 controllerInts
= OSArray::withObjects( (const OSObject
**) &map
, 1, 16 );
734 allInts
->setObject( controller
, controllerInts
);
735 controllerInts
->release();
741 controller
->release();
743 } while( localBits
< localEnd
);
745 ok
&= (localBits
== localEnd
);
749 ok
= regEntry
->setProperty( gIOInterruptControllersKey
, controllers
);
750 ok
&= regEntry
->setProperty( gIOInterruptSpecifiersKey
, mapped
);
754 controllers
->release();
761 bool IODTMapInterrupts( IORegistryEntry
* regEntry
)
763 return( IODTMapInterruptsSharing( regEntry
, 0 ));
770 CompareKey( OSString
* key
,
771 const IORegistryEntry
* table
, const OSSymbol
* propName
)
779 const char *lastName
;
782 const char *result
= 0;
784 if( 0 == (prop
= table
->getProperty( propName
)))
787 if( (data
= OSDynamicCast( OSData
, prop
))) {
788 names
= (const char *) data
->getBytesNoCopy();
789 lastName
= names
+ data
->getLength();
790 } else if( (string
= OSDynamicCast( OSString
, prop
))) {
791 names
= string
->getCStringNoCopy();
792 lastName
= names
+ string
->getLength() + 1;
796 ckey
= key
->getCStringNoCopy();
797 keyLen
= key
->getLength();
798 wild
= ('*' == key
->getChar( keyLen
- 1 ));
801 // for each name in the property
803 matched
= (0 == strncmp( ckey
, names
, keyLen
- 1 ));
805 matched
= (keyLen
== strlen( names
))
806 && (0 == strncmp( ckey
, names
, keyLen
));
811 names
= names
+ strlen( names
) + 1;
813 } while( (names
< lastName
) && (false == matched
));
819 bool IODTCompareNubName( const IORegistryEntry
* regEntry
,
820 OSString
* name
, OSString
** matchingName
)
825 matched
= (0 != (result
= CompareKey( name
, regEntry
, gIODTNameKey
)))
826 || (0 != (result
= CompareKey( name
, regEntry
, gIODTCompatibleKey
)))
827 || (0 != (result
= CompareKey( name
, regEntry
, gIODTTypeKey
)))
828 || (0 != (result
= CompareKey( name
, regEntry
, gIODTModelKey
)));
830 if( result
&& matchingName
)
831 *matchingName
= OSString::withCString( result
);
833 return( result
!= 0 );
836 bool IODTMatchNubWithKeys( IORegistryEntry
* regEntry
,
842 obj
= OSUnserialize( keys
, 0 );
845 result
= regEntry
->compareNames( obj
);
849 else IOLog("Couldn't unserialize %s\n", keys
);
855 OSCollectionIterator
* IODTFindMatchingEntries( IORegistryEntry
* from
,
856 IOOptionBits options
, const char * keys
)
859 IORegistryEntry
*next
;
860 IORegistryIterator
*iter
;
861 OSCollectionIterator
*cIter
;
863 bool minus
= options
& kIODTExclusive
;
866 iter
= IORegistryIterator::iterateOver( from
, gIODTPlane
,
867 (options
& kIODTRecursive
) ? kIORegistryIterateRecursively
: 0 );
874 result
= OSSet::withCapacity( 3 );
879 while( (next
= iter
->getNextObject())) {
881 // Look for existence of a debug property to skip
882 if( next
->getProperty("AAPL,ignore"))
886 cmp
= IODTMatchNubWithKeys( next
, keys
);
887 if( (minus
&& (false == cmp
))
888 || ((false == minus
) && (false != cmp
)) )
889 result
->setObject( next
);
891 result
->setObject( next
);
893 } while( !iter
->isValid());
898 cIter
= OSCollectionIterator::withCollection( result
);
905 struct IODTPersistent
{
906 IODTCompareAddressCellFunc compareFunc
;
907 IODTNVLocationFunc locationFunc
;
910 void IODTSetResolving( IORegistryEntry
* regEntry
,
911 IODTCompareAddressCellFunc compareFunc
,
912 IODTNVLocationFunc locationFunc
)
914 IODTPersistent persist
;
917 persist
.compareFunc
= compareFunc
;
918 persist
.locationFunc
= locationFunc
;
919 prop
= OSData::withBytes( &persist
, sizeof(persist
));
923 regEntry
->setProperty( gIODTPersistKey
, prop
);
928 static SInt32
DefaultCompare( UInt32 cellCount
, UInt32 left
[], UInt32 right
[] )
931 return( left
[ cellCount
] - right
[ cellCount
] );
934 void IODTGetCellCounts( IORegistryEntry
* regEntry
,
935 UInt32
* sizeCount
, UInt32
* addressCount
)
937 if( !GetUInt32( regEntry
, gIODTSizeCellKey
, sizeCount
))
939 if( !GetUInt32( regEntry
, gIODTAddressCellKey
, addressCount
))
944 // Given addr & len cells from our child, find it in our ranges property, then
945 // look in our parent to resolve the base of the range for us.
947 // Range[]: child-addr our-addr child-len
948 // #cells: child ours child
950 bool IODTResolveAddressCell( IORegistryEntry
* regEntry
,
952 IOPhysicalAddress
* phys
, IOPhysicalLength
* len
)
954 IORegistryEntry
*parent
;
956 // cells in addresses at regEntry
957 UInt32 sizeCells
, addressCells
;
958 // cells in addresses below regEntry
959 UInt32 childSizeCells
, childAddressCells
;
961 UInt32 cell
[ 5 ], offset
= 0, length
;
968 SInt32 diff
, diff2
, endDiff
;
970 IODTPersistent
*persist
;
971 IODTCompareAddressCellFunc compare
;
973 IODTGetCellCounts( regEntry
, &childSizeCells
, &childAddressCells
);
974 childCells
= childAddressCells
+ childSizeCells
;
976 bcopy( cellsIn
, cell
, sizeof(UInt32
) * childCells
);
977 if( childSizeCells
> 1)
978 *len
= IOPhysical32( cellsIn
[ childAddressCells
],
979 cellsIn
[ childAddressCells
+ 1 ] );
981 *len
= IOPhysical32( 0, cellsIn
[ childAddressCells
] );
985 prop
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTRangeKey
));
987 /* end of the road */
988 *phys
= IOPhysical32( 0, cell
[ childAddressCells
- 1 ] + offset
);
992 parent
= regEntry
->getParentEntry( gIODTPlane
);
993 IODTGetCellCounts( parent
, &sizeCells
, &addressCells
);
995 if( (length
= prop
->getLength())) {
997 startRange
= (UInt32
*) prop
->getBytesNoCopy();
999 endRanges
= range
+ (length
/ sizeof(UInt32
));
1001 prop
= (OSData
*) regEntry
->getProperty( gIODTPersistKey
);
1003 persist
= (IODTPersistent
*) prop
->getBytesNoCopy();
1004 compare
= persist
->compareFunc
;
1006 compare
= DefaultCompare
;
1010 range
+= (childCells
+ addressCells
) ) {
1012 // is cell start within range?
1013 diff
= (*compare
)( childAddressCells
, cell
, range
);
1015 bcopy(range
, endCell
, childAddressCells
* sizeof(UInt32
));
1016 endCell
[childAddressCells
- 1] += range
[childCells
+ addressCells
- 1];
1017 diff2
= (*compare
)( childAddressCells
, cell
, endCell
);
1019 if ((diff
< 0) || (diff2
>= 0))
1022 ok
= (0 == cell
[childCells
- 1]);
1025 // search for cell end
1026 bcopy(cell
, endCell
, childAddressCells
* sizeof(UInt32
));
1027 endCell
[childAddressCells
- 1] += cell
[childCells
- 1] - 1;
1028 lookRange
= startRange
;
1030 lookRange
< endRanges
;
1031 lookRange
+= (childCells
+ addressCells
) )
1033 // is cell >= range start?
1034 endDiff
= (*compare
)( childAddressCells
, endCell
, lookRange
);
1037 if ((endDiff
- cell
[childCells
- 1] + 1 + lookRange
[childAddressCells
+ addressCells
- 1])
1038 == (diff
+ range
[childAddressCells
+ addressCells
- 1]))
1051 // Get the physical start of the range from our parent
1052 bcopy( range
+ childAddressCells
, cell
, sizeof(UInt32
) * addressCells
);
1053 bzero( cell
+ addressCells
, sizeof(UInt32
) * sizeCells
);
1055 } /* else zero length range => pass thru to parent */
1058 childSizeCells
= sizeCells
;
1059 childAddressCells
= addressCells
;
1060 childCells
= childAddressCells
+ childSizeCells
;
1062 while( ok
&& regEntry
);
1068 OSArray
* IODTResolveAddressing( IORegistryEntry
* regEntry
,
1069 const char * addressPropertyName
,
1070 IODeviceMemory
* parent
)
1072 IORegistryEntry
*parentEntry
;
1073 OSData
*addressProperty
;
1074 UInt32 sizeCells
, addressCells
, cells
;
1077 IOPhysicalAddress phys
;
1078 IOPhysicalLength len
;
1080 IODeviceMemory
*range
;
1082 parentEntry
= regEntry
->getParentEntry( gIODTPlane
);
1083 addressProperty
= (OSData
*) regEntry
->getProperty( addressPropertyName
);
1084 if( (0 == addressProperty
) || (0 == parentEntry
))
1087 IODTGetCellCounts( parentEntry
, &sizeCells
, &addressCells
);
1091 cells
= sizeCells
+ addressCells
;
1092 reg
= (UInt32
*) addressProperty
->getBytesNoCopy();
1093 num
= addressProperty
->getLength() / (4 * cells
);
1095 array
= OSArray::withCapacity( 1 );
1099 for( i
= 0; i
< num
; i
++) {
1100 if( IODTResolveAddressCell( parentEntry
, reg
, &phys
, &len
)) {
1103 range
= IODeviceMemory::withSubRange( parent
,
1104 phys
- parent
->getPhysicalAddress(), len
);
1106 range
= IODeviceMemory::withRange( phys
, len
);
1108 array
->setObject( range
);
1113 regEntry
->setProperty( gIODeviceMemoryKey
, array
);
1114 array
->release(); /* ??? */
1119 static void IODTGetNVLocation(
1120 IORegistryEntry
* parent
,
1121 IORegistryEntry
* regEntry
,
1122 UInt8
* busNum
, UInt8
* deviceNum
, UInt8
* functionNum
)
1126 IODTPersistent
*persist
;
1129 prop
= (OSData
*) parent
->getProperty( gIODTPersistKey
);
1131 persist
= (IODTPersistent
*) prop
->getBytesNoCopy();
1132 (*persist
->locationFunc
)( regEntry
, busNum
, deviceNum
, functionNum
);
1134 prop
= (OSData
*) regEntry
->getProperty( "reg" );
1137 cell
= (UInt32
*) prop
->getBytesNoCopy();
1139 *deviceNum
= 0x1f & (cell
[ 0 ] >> 24);
1149 * Try to make the same messed up descriptor as Mac OS
1152 IOReturn
IODTMakeNVDescriptor( IORegistryEntry
* regEntry
,
1153 IONVRAMDescriptor
* hdr
)
1155 IORegistryEntry
*parent
;
1157 UInt32 bridgeDevices
;
1165 for(level
= 0, bridgeDevices
= 0;
1166 (parent
= regEntry
->getParentEntry( gIODTPlane
)) && (level
< 7); level
++ ) {
1168 IODTGetNVLocation( parent
, regEntry
,
1169 &busNum
, &deviceNum
, &functionNum
);
1171 bridgeDevices
|= ((deviceNum
& 0x1f) << ((level
- 1) * 5));
1173 hdr
->busNum
= busNum
;
1174 hdr
->deviceNum
= deviceNum
;
1175 hdr
->functionNum
= functionNum
;
1179 hdr
->bridgeCount
= level
- 2;
1180 hdr
->bridgeDevices
= bridgeDevices
;
1182 return( kIOReturnSuccess
);
1185 OSData
* IODTFindSlotName( IORegistryEntry
* regEntry
, UInt32 deviceNumber
)
1187 IORegistryEntry
*parent
;
1196 data
= (OSData
*) regEntry
->getProperty("AAPL,slot-name");
1199 parent
= regEntry
->getParentEntry( gIODTPlane
);
1202 data
= OSDynamicCast( OSData
, parent
->getProperty("slot-names"));
1205 if( data
->getLength() <= 4)
1208 bits
= (UInt32
*) data
->getBytesNoCopy();
1210 if( (0 == (mask
& (1 << deviceNumber
))))
1213 names
= (char *)(bits
+ 1);
1214 lastName
= names
+ (data
->getLength() - 4);
1216 for( i
= 0; (i
<= deviceNumber
) && (names
< lastName
); i
++ ) {
1218 if( mask
& (1 << i
)) {
1219 if( i
== deviceNumber
) {
1220 data
= OSData::withBytesNoCopy( names
, 1 + strlen( names
));
1222 regEntry
->setProperty("AAPL,slot-name", data
);
1227 names
+= 1 + strlen( names
);
1234 extern "C" IOReturn
IONDRVLibrariesInitialize( IOService
* provider
)
1236 return( kIOReturnUnsupported
);