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>
40 #include <machine/machine_routines.h>
44 int IODTGetLoaderInfo( const char *key
, void **infoAddr
, int *infosize
);
45 void IODTFreeLoaderInfo( const char *key
, void *infoAddr
, int infoSize
);
46 int IODTGetDefault(const char *key
, void *infoAddr
, unsigned int infoSize
);
50 #include <IOKit/assert.h>
52 #define IODTSUPPORTDEBUG 0
54 const IORegistryPlane
* gIODTPlane
;
56 static OSArray
* gIODTPHandles
;
57 static OSArray
* gIODTPHandleMap
;
59 const OSSymbol
* gIODTNameKey
;
60 const OSSymbol
* gIODTUnitKey
;
61 const OSSymbol
* gIODTCompatibleKey
;
62 const OSSymbol
* gIODTTypeKey
;
63 const OSSymbol
* gIODTModelKey
;
64 const OSSymbol
* gIODTTargetTypeKey
;
66 const OSSymbol
* gIODTSizeCellKey
;
67 const OSSymbol
* gIODTAddressCellKey
;
68 const OSSymbol
* gIODTRangeKey
;
70 const OSSymbol
* gIODTPersistKey
;
72 const OSSymbol
* gIODTDefaultInterruptController
;
73 const OSSymbol
* gIODTAAPLInterruptsKey
;
74 const OSSymbol
* gIODTPHandleKey
;
75 const OSSymbol
* gIODTInterruptCellKey
;
76 const OSSymbol
* gIODTInterruptParentKey
;
77 const OSSymbol
* gIODTNWInterruptMappingKey
;
79 OSDictionary
* gIODTSharedInterrupts
;
81 static IORegistryEntry
* MakeReferenceTable( DTEntry dtEntry
, bool copy
);
82 static void AddPHandle( IORegistryEntry
* regEntry
);
83 static void FreePhysicalMemory( vm_offset_t
* range
);
84 static bool IODTMapInterruptsSharing( IORegistryEntry
* regEntry
, OSDictionary
* allInts
);
87 IODeviceTreeAlloc( void * dtTop
)
89 IORegistryEntry
* parent
;
90 IORegistryEntry
* child
;
91 IORegistryIterator
* regIter
;
97 OSDictionary
* allInts
;
99 unsigned int propSize
;
103 gIODTPlane
= IORegistryEntry::makePlane( kIODeviceTreePlane
);
105 gIODTNameKey
= OSSymbol::withCStringNoCopy( "name" );
106 gIODTUnitKey
= OSSymbol::withCStringNoCopy( "AAPL,unit-string" );
107 gIODTCompatibleKey
= OSSymbol::withCStringNoCopy( "compatible" );
108 gIODTTypeKey
= OSSymbol::withCStringNoCopy( "device_type" );
109 gIODTModelKey
= OSSymbol::withCStringNoCopy( "model" );
110 gIODTTargetTypeKey
= OSSymbol::withCStringNoCopy( "target-type" );
111 gIODTSizeCellKey
= OSSymbol::withCStringNoCopy( "#size-cells" );
112 gIODTAddressCellKey
= OSSymbol::withCStringNoCopy( "#address-cells" );
113 gIODTRangeKey
= OSSymbol::withCStringNoCopy( "ranges" );
114 gIODTPersistKey
= OSSymbol::withCStringNoCopy( "IODTPersist" );
116 assert( gIODTPlane
&& gIODTCompatibleKey
117 && gIODTTypeKey
&& gIODTModelKey
118 && gIODTSizeCellKey
&& gIODTAddressCellKey
&& gIODTRangeKey
119 && gIODTPersistKey
);
121 gIODTDefaultInterruptController
122 = OSSymbol::withCStringNoCopy("IOPrimaryInterruptController");
123 gIODTNWInterruptMappingKey
124 = OSSymbol::withCStringNoCopy("IONWInterrupts");
126 gIODTAAPLInterruptsKey
127 = OSSymbol::withCStringNoCopy("AAPL,interrupts");
129 = OSSymbol::withCStringNoCopy("AAPL,phandle");
131 gIODTInterruptParentKey
132 = OSSymbol::withCStringNoCopy("interrupt-parent");
134 gIODTPHandles
= OSArray::withCapacity( 1 );
135 gIODTPHandleMap
= OSArray::withCapacity( 1 );
137 gIODTInterruptCellKey
138 = OSSymbol::withCStringNoCopy("#interrupt-cells");
140 assert( gIODTDefaultInterruptController
&& gIODTNWInterruptMappingKey
141 && gIODTAAPLInterruptsKey
142 && gIODTPHandleKey
&& gIODTInterruptParentKey
143 && gIODTPHandles
&& gIODTPHandleMap
144 && gIODTInterruptCellKey
147 freeDT
= (kSuccess
== DTLookupEntry( 0, "/chosen/memory-map", &mapEntry
))
148 && (kSuccess
== DTGetProperty( mapEntry
,
149 "DeviceTree", (void **) &dtMap
, &propSize
))
150 && ((2 * sizeof(uint32_t)) == propSize
);
152 parent
= MakeReferenceTable( (DTEntry
)dtTop
, freeDT
);
154 stack
= OSArray::withObjects( (const OSObject
**) &parent
, 1, 10 );
155 DTCreateEntryIterator( (DTEntry
)dtTop
, &iter
);
158 parent
= (IORegistryEntry
*)stack
->getObject( stack
->getCount() - 1);
160 stack
->removeObject( stack
->getCount() - 1);
162 while( kSuccess
== DTIterateEntries( iter
, &dtChild
) ) {
164 child
= MakeReferenceTable( dtChild
, freeDT
);
165 child
->attachToParent( parent
, gIODTPlane
);
169 if( kSuccess
== DTEnterEntry( iter
, dtChild
)) {
170 stack
->setObject( parent
);
173 // only registry holds retain
177 } while( stack
->getCount()
178 && (kSuccess
== DTExitEntry( iter
, &dtChild
)));
181 DTDisposeEntryIterator( iter
);
183 // parent is now root of the created tree
185 // make root name first compatible entry (purely cosmetic)
186 if( (prop
= (OSData
*) parent
->getProperty( gIODTCompatibleKey
))) {
187 parent
->setName( parent
->getName(), gIODTPlane
);
188 parent
->setName( (const char *) prop
->getBytesNoCopy() );
191 // attach tree to meta root
192 parent
->attachToParent( IORegistryEntry::getRegistryRoot(), gIODTPlane
);
196 // free original device tree
198 IODTFreeLoaderInfo( "DeviceTree",
199 (void *)dtMap
[0], (int) round_page(dtMap
[1]) );
204 gIODTSharedInterrupts
= OSDictionary::withCapacity(4);
205 allInts
= OSDictionary::withCapacity(4);
207 regIter
= IORegistryIterator::iterateOver( gIODTPlane
,
208 kIORegistryIterateRecursively
);
209 assert( regIter
&& allInts
&& gIODTSharedInterrupts
);
210 if( regIter
&& allInts
&& gIODTSharedInterrupts
) {
211 while( (child
= regIter
->getNextObject())) {
212 IODTMapInterruptsSharing( child
, allInts
);
213 if( !intMap
&& child
->getProperty( gIODTInterruptParentKey
))
221 parent
->setProperty("allInts", allInts
);
222 parent
->setProperty("sharedInts", gIODTSharedInterrupts
);
224 regIter
= IORegistryIterator::iterateOver( gIODTPlane
,
225 kIORegistryIterateRecursively
);
227 while( (child
= regIter
->getNextObject())) {
229 array
= OSDynamicCast(OSArray
, child
->getProperty( gIOInterruptSpecifiersKey
));
230 for( UInt32 i
= 0; array
&& (i
< array
->getCount()); i
++)
232 IOOptionBits options
;
233 IOReturn ret
= IODTGetInterruptOptions( child
, i
, &options
);
234 if( (ret
!= kIOReturnSuccess
) || options
)
235 IOLog("%s[%ld] %ld (%x)\n", child
->getName(), i
, options
, ret
);
245 // set a key in the root to indicate we found NW interrupt mapping
246 parent
->setProperty( gIODTNWInterruptMappingKey
,
247 (OSObject
*) gIODTNWInterruptMappingKey
);
252 int IODTGetLoaderInfo( const char *key
, void **infoAddr
, int *infoSize
)
254 IORegistryEntry
*chosen
;
256 unsigned int *propPtr
;
257 unsigned int propSize
;
259 chosen
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
);
260 if ( chosen
== 0 ) return -1;
262 propObj
= OSDynamicCast( OSData
, chosen
->getProperty(key
) );
263 if ( propObj
== 0 ) return -1;
265 propSize
= propObj
->getLength();
266 if ( propSize
!= (2 * sizeof(UInt32
)) ) return -1;
268 propPtr
= (unsigned int *)propObj
->getBytesNoCopy();
269 if ( propPtr
== 0 ) return -1;
271 *infoAddr
= (void *)(uintptr_t) (propPtr
[0]);
272 *infoSize
= (int) (propPtr
[1]);
277 void IODTFreeLoaderInfo( const char *key
, void *infoAddr
, int infoSize
)
279 vm_offset_t range
[2];
280 IORegistryEntry
*chosen
;
282 range
[0] = (vm_offset_t
)infoAddr
;
283 range
[1] = (vm_offset_t
)infoSize
;
284 FreePhysicalMemory( range
);
287 chosen
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
);
289 chosen
->removeProperty(key
);
294 int IODTGetDefault(const char *key
, void *infoAddr
, unsigned int infoSize
)
296 IORegistryEntry
*defaults
;
298 unsigned int defaultSize
;
300 defaults
= IORegistryEntry::fromPath( "/defaults", gIODTPlane
);
301 if ( defaults
== 0 ) return -1;
303 defaultObj
= OSDynamicCast( OSData
, defaults
->getProperty(key
) );
304 if ( defaultObj
== 0 ) return -1;
306 defaultSize
= defaultObj
->getLength();
307 if ( defaultSize
> infoSize
) return -1;
309 memcpy( infoAddr
, defaultObj
->getBytesNoCopy(), defaultSize
);
314 static void FreePhysicalMemory( vm_offset_t
* range
)
318 virt
= ml_static_ptovirt( range
[0] );
320 ml_static_mfree( virt
, range
[1] );
324 static IORegistryEntry
*
325 MakeReferenceTable( DTEntry dtEntry
, bool copy
)
327 IORegistryEntry
*regEntry
;
328 OSDictionary
*propTable
;
329 const OSSymbol
*nameKey
;
332 DTPropertyIterator dtIter
;
334 unsigned int propSize
;
337 bool noLocation
= true;
339 regEntry
= new IOService
;
341 if( regEntry
&& (false == regEntry
->init())) {
347 (kSuccess
== DTCreatePropertyIterator( dtEntry
, &dtIter
))) {
349 propTable
= regEntry
->getPropertyTable();
351 while( kSuccess
== DTIterateProperties( dtIter
, &name
)) {
353 if( kSuccess
!= DTGetProperty( dtEntry
, name
, &prop
, &propSize
))
357 nameKey
= OSSymbol::withCString(name
);
358 data
= OSData::withBytes(prop
, propSize
);
360 nameKey
= OSSymbol::withCStringNoCopy(name
);
361 data
= OSData::withBytesNoCopy(prop
, propSize
);
363 assert( nameKey
&& data
);
365 propTable
->setObject( nameKey
, data
);
369 if( nameKey
== gIODTNameKey
) {
371 sym
= OSSymbol::withCString( (const char *) prop
);
373 sym
= OSSymbol::withCStringNoCopy( (const char *) prop
);
374 regEntry
->setName( sym
);
377 } else if( nameKey
== gIODTUnitKey
) {
378 // all OF strings are null terminated... except this one
379 if( propSize
>= (int) sizeof(location
))
380 propSize
= sizeof(location
) - 1;
381 strncpy( location
, (const char *) prop
, propSize
);
382 location
[ propSize
] = 0;
383 regEntry
->setLocation( location
);
384 propTable
->removeObject( gIODTUnitKey
);
387 } else if(noLocation
&& (!strncmp(name
, "reg", sizeof("reg")))) {
388 // default location - override later
389 snprintf(location
, sizeof(location
), "%X", *((uint32_t *) prop
));
390 regEntry
->setLocation( location
);
393 DTDisposePropertyIterator( dtIter
);
399 static void AddPHandle( IORegistryEntry
* regEntry
)
403 if( regEntry
->getProperty( gIODTInterruptCellKey
)
404 && (data
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTPHandleKey
)))) {
405 // a possible interrupt-parent
406 gIODTPHandles
->setObject( data
);
407 gIODTPHandleMap
->setObject( regEntry
);
411 static IORegistryEntry
* FindPHandle( UInt32 phandle
)
414 IORegistryEntry
*regEntry
= 0;
417 for( i
= 0; (data
= (OSData
*)gIODTPHandles
->getObject( i
)); i
++ ) {
418 if( phandle
== *((UInt32
*)data
->getBytesNoCopy())) {
419 regEntry
= (IORegistryEntry
*)
420 gIODTPHandleMap
->getObject( i
);
428 static bool GetUInt32( IORegistryEntry
* regEntry
, const OSSymbol
* name
,
433 if( (data
= OSDynamicCast( OSData
, regEntry
->getProperty( name
)))
434 && (4 == data
->getLength())) {
435 *value
= *((UInt32
*) data
->getBytesNoCopy());
441 static IORegistryEntry
* IODTFindInterruptParent( IORegistryEntry
* regEntry
, IOItemCount index
)
443 IORegistryEntry
* parent
;
448 if( (data
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTInterruptParentKey
)))
449 && (sizeof(UInt32
) <= (len
= data
->getLength()))) {
450 if (((index
+ 1) * sizeof(UInt32
)) > len
)
452 phandle
= ((UInt32
*) data
->getBytesNoCopy())[index
];
453 parent
= FindPHandle( phandle
);
455 } else if( 0 == regEntry
->getProperty( "interrupt-controller"))
456 parent
= regEntry
->getParentEntry( gIODTPlane
);
463 const OSSymbol
* IODTInterruptControllerName( IORegistryEntry
* regEntry
)
470 ok
= GetUInt32( regEntry
, gIODTPHandleKey
, &phandle
);
474 snprintf(buf
, sizeof(buf
), "IOInterruptController%08X", (uint32_t)phandle
);
475 sym
= OSSymbol::withCString( buf
);
482 #define unexpected(a) { kprintf("unexpected %s:%d\n", __FILE__, __LINE__); a; }
484 static void IODTGetICellCounts( IORegistryEntry
* regEntry
,
485 UInt32
* iCellCount
, UInt32
* aCellCount
)
487 if( !GetUInt32( regEntry
, gIODTInterruptCellKey
, iCellCount
))
488 unexpected( *iCellCount
= 1 );
489 if( !GetUInt32( regEntry
, gIODTAddressCellKey
, aCellCount
))
493 static UInt32
IODTMapOneInterrupt( IORegistryEntry
* regEntry
, UInt32
* intSpec
, UInt32 index
,
494 OSData
** spec
, const OSSymbol
** controller
)
496 IORegistryEntry
*parent
= 0;
502 UInt32 acells
, icells
, pacells
, picells
, cell
;
503 UInt32 i
, original_icells
;
504 bool cmp
, ok
= false;
506 parent
= IODTFindInterruptParent( regEntry
, index
);
507 IODTGetICellCounts( parent
, &icells
, &acells
);
510 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "reg" ));
511 if( data
&& (data
->getLength() >= (acells
* sizeof(UInt32
))))
512 addrCmp
= (UInt32
*) data
->getBytesNoCopy();
514 original_icells
= icells
;
519 kprintf ("IODTMapOneInterrupt: current regEntry name %s\n", regEntry
->getName());
520 kprintf ("acells - icells: ");
521 for (i
= 0; i
< acells
; i
++) kprintf ("0x%08X ", addrCmp
[i
]);
523 for (i
= 0; i
< icells
; i
++) kprintf ("0x%08X ", intSpec
[i
]);
527 if( parent
&& (data
= OSDynamicCast( OSData
,
528 regEntry
->getProperty( "interrupt-controller")))) {
529 // found a controller - don't want to follow cascaded controllers
531 *spec
= OSData::withBytesNoCopy( (void *) intSpec
,
532 icells
* sizeof(UInt32
));
533 *controller
= IODTInterruptControllerName( regEntry
);
534 ok
= (*spec
&& *controller
);
535 } else if( parent
&& (data
= OSDynamicCast( OSData
,
536 regEntry
->getProperty( "interrupt-map")))) {
538 map
= (UInt32
*) data
->getBytesNoCopy();
539 endMap
= map
+ (data
->getLength() / sizeof(UInt32
));
540 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "interrupt-map-mask" ));
541 if( data
&& (data
->getLength() >= ((acells
+ icells
) * sizeof(UInt32
))))
542 maskCmp
= (UInt32
*) data
->getBytesNoCopy();
548 kprintf (" maskCmp: ");
549 for (i
= 0; i
< acells
+ icells
; i
++) {
552 kprintf ("0x%08X ", maskCmp
[i
]);
555 kprintf (" masked: ");
556 for (i
= 0; i
< acells
+ icells
; i
++) {
559 kprintf ("0x%08X ", ((i
< acells
) ? addrCmp
[i
] : intSpec
[i
-acells
]) & maskCmp
[i
]);
563 kprintf ("no maskCmp\n");
568 for (i
= 0; i
< acells
+ icells
; i
++) {
571 kprintf ("0x%08X ", map
[i
]);
575 for( i
= 0, cmp
= true; cmp
&& (i
< (acells
+ icells
)); i
++) {
576 cell
= (i
< acells
) ? addrCmp
[i
] : intSpec
[ i
- acells
];
579 cmp
= (cell
== map
[i
]);
582 map
+= acells
+ icells
;
583 if( 0 == (parent
= FindPHandle( *(map
++) )))
586 IODTGetICellCounts( parent
, &picells
, &pacells
);
589 intSpec
= map
+ pacells
;
592 map
+= pacells
+ picells
;
594 } while( !cmp
&& (map
< endMap
) );
600 IODTGetICellCounts( parent
, &icells
, &acells
);
606 return( ok
? original_icells
: 0 );
609 IOReturn
IODTGetInterruptOptions( IORegistryEntry
* regEntry
, int source
, IOOptionBits
* options
)
611 OSArray
* controllers
;
612 OSArray
* specifiers
;
619 controllers
= OSDynamicCast(OSArray
, regEntry
->getProperty(gIOInterruptControllersKey
));
620 specifiers
= OSDynamicCast(OSArray
, regEntry
->getProperty(gIOInterruptSpecifiersKey
));
622 if( !controllers
|| !specifiers
)
623 return (kIOReturnNoInterrupt
);
625 shared
= (OSArray
*) gIODTSharedInterrupts
->getObject(
626 (const OSSymbol
*) controllers
->getObject(source
) );
628 return (kIOReturnSuccess
);
630 spec
= specifiers
->getObject(source
);
632 return (kIOReturnNoInterrupt
);
634 for (unsigned int i
= 0;
635 (oneSpec
= shared
->getObject(i
))
636 && (!oneSpec
->isEqualTo(spec
));
640 *options
= kIODTInterruptShared
;
642 return (kIOReturnSuccess
);
645 static bool IODTMapInterruptsSharing( IORegistryEntry
* regEntry
, OSDictionary
* allInts
)
647 IORegistryEntry
* parent
;
656 OSArray
* controllerInts
;
657 const OSSymbol
* controller
= 0;
658 OSArray
* controllers
;
662 nw
= (0 == (local
= OSDynamicCast( OSData
,
663 regEntry
->getProperty( gIODTAAPLInterruptsKey
))));
664 if( nw
&& (0 == (local
= OSDynamicCast( OSData
,
665 regEntry
->getProperty( "interrupts")))))
666 return( true ); // nothing to see here
668 if( nw
&& (parent
= regEntry
->getParentEntry( gIODTPlane
))) {
669 // check for bridges on old world
670 if( (local2
= OSDynamicCast( OSData
,
671 parent
->getProperty( gIODTAAPLInterruptsKey
)))) {
677 localBits
= (UInt32
*) local
->getBytesNoCopy();
678 localEnd
= localBits
+ (local
->getLength() / sizeof(UInt32
));
680 mapped
= OSArray::withCapacity( 1 );
681 controllers
= OSArray::withCapacity( 1 );
683 ok
= (mapped
&& controllers
);
687 skip
= IODTMapOneInterrupt( regEntry
, localBits
, index
, &map
, &controller
);
689 IOLog("%s: error mapping interrupt[%d]\n",
690 regEntry
->getName(), mapped
->getCount());
694 map
= OSData::withData( local
, mapped
->getCount() * sizeof(UInt32
),
696 controller
= gIODTDefaultInterruptController
;
697 controller
->retain();
702 mapped
->setObject( map
);
703 controllers
->setObject( controller
);
707 controllerInts
= (OSArray
*) allInts
->getObject( controller
);
710 for (unsigned int i
= 0; (oneMap
= controllerInts
->getObject(i
)); i
++)
712 if (map
->isEqualTo(oneMap
))
714 controllerInts
= (OSArray
*) gIODTSharedInterrupts
->getObject( controller
);
716 controllerInts
->setObject(map
);
719 controllerInts
= OSArray::withObjects( (const OSObject
**) &map
, 1, 4 );
722 gIODTSharedInterrupts
->setObject( controller
, controllerInts
);
723 controllerInts
->release();
730 controllerInts
->setObject(map
);
734 controllerInts
= OSArray::withObjects( (const OSObject
**) &map
, 1, 16 );
737 allInts
->setObject( controller
, controllerInts
);
738 controllerInts
->release();
744 controller
->release();
746 } while( localBits
< localEnd
);
748 ok
&= (localBits
== localEnd
);
752 ok
= regEntry
->setProperty( gIOInterruptControllersKey
, controllers
);
753 ok
&= regEntry
->setProperty( gIOInterruptSpecifiersKey
, mapped
);
757 controllers
->release();
764 bool IODTMapInterrupts( IORegistryEntry
* regEntry
)
766 return( IODTMapInterruptsSharing( regEntry
, 0 ));
773 CompareKey( OSString
* key
,
774 const IORegistryEntry
* table
, const OSSymbol
* propName
)
783 const char *lastName
;
786 const char *result
= 0;
788 if( 0 == (prop
= table
->getProperty( propName
)))
791 if( (data
= OSDynamicCast( OSData
, prop
))) {
792 names
= (const char *) data
->getBytesNoCopy();
793 lastName
= names
+ data
->getLength();
794 } else if( (string
= OSDynamicCast( OSString
, prop
))) {
795 names
= string
->getCStringNoCopy();
796 lastName
= names
+ string
->getLength() + 1;
800 ckey
= key
->getCStringNoCopy();
801 keyLen
= key
->getLength();
802 wild
= ('*' == key
->getChar( keyLen
- 1 ));
805 // for each name in the property
806 nlen
= strnlen(names
, lastName
- names
);
808 matched
= ((nlen
>= (keyLen
- 1)) && (0 == strncmp(ckey
, names
, keyLen
- 1)));
810 matched
= (keyLen
== nlen
) && (0 == strncmp(ckey
, names
, keyLen
));
815 names
= names
+ nlen
+ 1;
817 } while( (names
< lastName
) && (false == matched
));
823 bool IODTCompareNubName( const IORegistryEntry
* regEntry
,
824 OSString
* name
, OSString
** matchingName
)
829 matched
= (0 != (result
= CompareKey( name
, regEntry
, gIODTNameKey
)))
830 || (0 != (result
= CompareKey( name
, regEntry
, gIODTCompatibleKey
)))
831 || (0 != (result
= CompareKey( name
, regEntry
, gIODTTypeKey
)))
832 || (0 != (result
= CompareKey( name
, regEntry
, gIODTModelKey
)));
834 if( result
&& matchingName
)
835 *matchingName
= OSString::withCString( result
);
837 return( result
!= 0 );
840 bool IODTMatchNubWithKeys( IORegistryEntry
* regEntry
,
846 obj
= OSUnserialize( keys
, 0 );
849 result
= regEntry
->compareNames( obj
);
853 else IOLog("Couldn't unserialize %s\n", keys
);
859 OSCollectionIterator
* IODTFindMatchingEntries( IORegistryEntry
* from
,
860 IOOptionBits options
, const char * keys
)
863 IORegistryEntry
*next
;
864 IORegistryIterator
*iter
;
865 OSCollectionIterator
*cIter
;
867 bool minus
= options
& kIODTExclusive
;
870 iter
= IORegistryIterator::iterateOver( from
, gIODTPlane
,
871 (options
& kIODTRecursive
) ? kIORegistryIterateRecursively
: 0 );
878 result
= OSSet::withCapacity( 3 );
883 while( (next
= iter
->getNextObject())) {
885 // Look for existence of a debug property to skip
886 if( next
->getProperty("AAPL,ignore"))
890 cmp
= IODTMatchNubWithKeys( next
, keys
);
891 if( (minus
&& (false == cmp
))
892 || ((false == minus
) && (false != cmp
)) )
893 result
->setObject( next
);
895 result
->setObject( next
);
897 } while( !iter
->isValid());
902 cIter
= OSCollectionIterator::withCollection( result
);
903 if (result
) result
->release();
909 struct IODTPersistent
{
910 IODTCompareAddressCellFunc compareFunc
;
911 IODTNVLocationFunc locationFunc
;
914 void IODTSetResolving( IORegistryEntry
* regEntry
,
915 IODTCompareAddressCellFunc compareFunc
,
916 IODTNVLocationFunc locationFunc
)
918 IODTPersistent persist
;
921 persist
.compareFunc
= compareFunc
;
922 persist
.locationFunc
= locationFunc
;
923 prop
= OSData::withBytes( &persist
, sizeof(persist
));
927 prop
->setSerializable(false);
928 regEntry
->setProperty( gIODTPersistKey
, prop
);
933 #if defined(__arm__) || defined(__i386__) || defined(__x86_64__)
934 static SInt32
DefaultCompare( UInt32 cellCount
, UInt32 left
[], UInt32 right
[] )
937 return( left
[ cellCount
] - right
[ cellCount
] );
940 #error Unknown architecture.
943 static void AddLengthToCells( UInt32 numCells
, UInt32
*cells
, UInt64 offset
)
947 cells
[0] += (UInt32
)offset
;
950 UInt64 sum
= cells
[numCells
- 1] + offset
;
951 cells
[numCells
- 1] = (UInt32
)sum
;
952 if (sum
> UINT32_MAX
) {
953 cells
[numCells
- 2] += (UInt32
)(sum
>> 32);
958 static IOPhysicalAddress
CellsValue( UInt32 numCells
, UInt32
*cells
)
961 return IOPhysical32( 0, cells
[0] );
963 return IOPhysical32( cells
[numCells
- 2], cells
[numCells
- 1] );
967 void IODTGetCellCounts( IORegistryEntry
* regEntry
,
968 UInt32
* sizeCount
, UInt32
* addressCount
)
970 if( !GetUInt32( regEntry
, gIODTSizeCellKey
, sizeCount
))
972 if( !GetUInt32( regEntry
, gIODTAddressCellKey
, addressCount
))
977 // Given addr & len cells from our child, find it in our ranges property, then
978 // look in our parent to resolve the base of the range for us.
980 // Range[]: child-addr our-addr child-len
981 // #cells: child ours child
983 bool IODTResolveAddressCell( IORegistryEntry
* regEntry
,
985 IOPhysicalAddress
* phys
, IOPhysicalLength
* lenOut
)
987 IORegistryEntry
*parent
;
989 // cells in addresses at regEntry
990 UInt32 sizeCells
, addressCells
;
991 // cells in addresses below regEntry
992 UInt32 childSizeCells
, childAddressCells
;
994 UInt32 cell
[ 8 ], propLen
;
1002 SInt64 diff
, diff2
, endDiff
;
1003 UInt64 len
, rangeLen
;
1005 IODTPersistent
*persist
;
1006 IODTCompareAddressCellFunc compare
;
1008 IODTGetCellCounts( regEntry
, &childSizeCells
, &childAddressCells
);
1009 childCells
= childAddressCells
+ childSizeCells
;
1011 if (childCells
> sizeof(cell
)/sizeof(cell
[0]))
1012 panic("IODTResolveAddressCell: Invalid device tree (%u,%u)", (uint32_t)childAddressCells
, (uint32_t)childSizeCells
);
1014 bcopy( cellsIn
, cell
, sizeof(UInt32
) * childCells
);
1015 *lenOut
= CellsValue( childSizeCells
, cellsIn
+ childAddressCells
);
1019 prop
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTRangeKey
));
1021 /* end of the road */
1022 *phys
= CellsValue( childAddressCells
, cell
);
1027 parent
= regEntry
->getParentEntry( gIODTPlane
);
1028 IODTGetCellCounts( parent
, &sizeCells
, &addressCells
);
1030 if( (propLen
= prop
->getLength())) {
1032 startRange
= (UInt32
*) prop
->getBytesNoCopy();
1034 endRanges
= range
+ (propLen
/ sizeof(UInt32
));
1036 prop
= (OSData
*) regEntry
->getProperty( gIODTPersistKey
);
1038 persist
= (IODTPersistent
*) prop
->getBytesNoCopy();
1039 compare
= persist
->compareFunc
;
1040 } else if (addressCells
== childAddressCells
) {
1041 compare
= DefaultCompare
;
1043 panic("There is no mixed comparison function yet...");
1048 range
+= (childCells
+ addressCells
) ) {
1050 // is cell start within range?
1051 diff
= (*compare
)( childAddressCells
, cell
, range
);
1053 if (childAddressCells
> sizeof(endCell
)/sizeof(endCell
[0]))
1054 panic("IODTResolveAddressCell: Invalid device tree (%u)", (uint32_t)childAddressCells
);
1056 bcopy(range
, endCell
, childAddressCells
* sizeof(UInt32
));
1058 rangeLen
= CellsValue(childSizeCells
, range
+ childAddressCells
+ addressCells
);
1059 AddLengthToCells(childAddressCells
, endCell
, rangeLen
);
1061 diff2
= (*compare
)( childAddressCells
, cell
, endCell
);
1063 // if start of cell < start of range, or end of range >= start of cell, skip
1064 if ((diff
< 0) || (diff2
>= 0))
1067 len
= CellsValue(childSizeCells
, cell
+ childAddressCells
);
1072 // search for cell end
1073 bcopy(cell
, endCell
, childAddressCells
* sizeof(UInt32
));
1075 AddLengthToCells(childAddressCells
, endCell
, len
- 1);
1077 for( lookRange
= startRange
;
1078 lookRange
< endRanges
;
1079 lookRange
+= (childCells
+ addressCells
) )
1081 // make sure end of cell >= range start
1082 endDiff
= (*compare
)( childAddressCells
, endCell
, lookRange
);
1086 UInt64 rangeStart
= CellsValue(addressCells
, range
+ childAddressCells
);
1087 UInt64 lookRangeStart
= CellsValue(addressCells
, lookRange
+ childAddressCells
);
1088 if ((endDiff
- len
+ 1 + lookRangeStart
) == (diff
+ rangeStart
))
1101 if (addressCells
+ sizeCells
> sizeof(cell
)/sizeof(cell
[0]))
1102 panic("IODTResolveAddressCell: Invalid device tree (%u, %u)", (uint32_t)addressCells
, (uint32_t)sizeCells
);
1104 // Get the physical start of the range from our parent
1105 bcopy( range
+ childAddressCells
, cell
, sizeof(UInt32
) * addressCells
);
1106 bzero( cell
+ addressCells
, sizeof(UInt32
) * sizeCells
);
1108 } /* else zero length range => pass thru to parent */
1111 childSizeCells
= sizeCells
;
1112 childAddressCells
= addressCells
;
1113 childCells
= childAddressCells
+ childSizeCells
;
1115 while( ok
&& regEntry
);
1121 OSArray
* IODTResolveAddressing( IORegistryEntry
* regEntry
,
1122 const char * addressPropertyName
,
1123 IODeviceMemory
* parent
)
1125 IORegistryEntry
*parentEntry
;
1126 OSData
*addressProperty
;
1127 UInt32 sizeCells
, addressCells
, cells
;
1130 IOPhysicalAddress phys
;
1131 IOPhysicalLength len
;
1133 IODeviceMemory
*range
;
1135 parentEntry
= regEntry
->getParentEntry( gIODTPlane
);
1136 addressProperty
= (OSData
*) regEntry
->getProperty( addressPropertyName
);
1137 if( (0 == addressProperty
) || (0 == parentEntry
))
1140 IODTGetCellCounts( parentEntry
, &sizeCells
, &addressCells
);
1144 cells
= sizeCells
+ addressCells
;
1145 reg
= (UInt32
*) addressProperty
->getBytesNoCopy();
1146 num
= addressProperty
->getLength() / (4 * cells
);
1148 array
= OSArray::withCapacity( 1 );
1152 for( i
= 0; i
< num
; i
++) {
1153 if( IODTResolveAddressCell( parentEntry
, reg
, &phys
, &len
)) {
1156 range
= IODeviceMemory::withSubRange( parent
,
1157 phys
- parent
->getPhysicalSegment(0, 0, kIOMemoryMapperNone
), len
);
1159 range
= IODeviceMemory::withRange( phys
, len
);
1161 array
->setObject( range
);
1166 regEntry
->setProperty( gIODeviceMemoryKey
, array
);
1167 array
->release(); /* ??? */
1172 static void IODTGetNVLocation(
1173 IORegistryEntry
* parent
,
1174 IORegistryEntry
* regEntry
,
1175 UInt8
* busNum
, UInt8
* deviceNum
, UInt8
* functionNum
)
1179 IODTPersistent
*persist
;
1182 prop
= (OSData
*) parent
->getProperty( gIODTPersistKey
);
1184 persist
= (IODTPersistent
*) prop
->getBytesNoCopy();
1185 (*persist
->locationFunc
)( regEntry
, busNum
, deviceNum
, functionNum
);
1187 prop
= (OSData
*) regEntry
->getProperty( "reg" );
1190 cell
= (UInt32
*) prop
->getBytesNoCopy();
1192 *deviceNum
= 0x1f & (cell
[ 0 ] >> 24);
1202 * Try to make the same messed up descriptor as Mac OS
1205 IOReturn
IODTMakeNVDescriptor( IORegistryEntry
* regEntry
,
1206 IONVRAMDescriptor
* hdr
)
1208 IORegistryEntry
*parent
;
1210 UInt32 bridgeDevices
;
1218 for(level
= 0, bridgeDevices
= 0;
1219 (parent
= regEntry
->getParentEntry( gIODTPlane
)) && (level
< 7); level
++ ) {
1221 IODTGetNVLocation( parent
, regEntry
,
1222 &busNum
, &deviceNum
, &functionNum
);
1224 bridgeDevices
|= ((deviceNum
& 0x1f) << ((level
- 1) * 5));
1226 hdr
->busNum
= busNum
;
1227 hdr
->deviceNum
= deviceNum
;
1228 hdr
->functionNum
= functionNum
;
1232 hdr
->bridgeCount
= level
- 2;
1233 hdr
->bridgeDevices
= bridgeDevices
;
1235 return( kIOReturnSuccess
);
1238 OSData
* IODTFindSlotName( IORegistryEntry
* regEntry
, UInt32 deviceNumber
)
1240 IORegistryEntry
*parent
;
1250 data
= (OSData
*) regEntry
->getProperty("AAPL,slot-name");
1253 parent
= regEntry
->getParentEntry( gIODTPlane
);
1256 data
= OSDynamicCast( OSData
, parent
->getProperty("slot-names"));
1259 if( data
->getLength() <= 4)
1262 bits
= (UInt32
*) data
->getBytesNoCopy();
1264 if( (0 == (mask
& (1 << deviceNumber
))))
1267 names
= (char *)(bits
+ 1);
1268 lastName
= names
+ (data
->getLength() - 4);
1270 for( i
= 0; (i
<= deviceNumber
) && (names
< lastName
); i
++ ) {
1272 if( mask
& (1 << i
)) {
1273 nlen
= 1 + strnlen(names
, lastName
- names
);
1274 if( i
== deviceNumber
) {
1275 data
= OSData::withBytesNoCopy(names
, nlen
);
1277 regEntry
->setProperty("AAPL,slot-name", data
);
1289 extern "C" IOReturn
IONDRVLibrariesInitialize( IOService
* provider
)
1291 return( kIOReturnUnsupported
);