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 typedef UInt32 dtptr_t
;
42 #include <machine/machine_routines.h>
46 int IODTGetLoaderInfo( const char *key
, void **infoAddr
, int *infosize
);
47 void IODTFreeLoaderInfo( const char *key
, void *infoAddr
, int infoSize
);
48 int IODTGetDefault(const char *key
, void *infoAddr
, unsigned int infoSize
);
52 #include <IOKit/assert.h>
54 #define IODTSUPPORTDEBUG 0
56 const IORegistryPlane
* gIODTPlane
;
58 static OSArray
* gIODTPHandles
;
59 static OSArray
* gIODTPHandleMap
;
61 const OSSymbol
* gIODTNameKey
;
62 const OSSymbol
* gIODTUnitKey
;
63 const OSSymbol
* gIODTCompatibleKey
;
64 const OSSymbol
* gIODTTypeKey
;
65 const OSSymbol
* gIODTModelKey
;
66 const OSSymbol
* gIODTTargetTypeKey
;
68 const OSSymbol
* gIODTSizeCellKey
;
69 const OSSymbol
* gIODTAddressCellKey
;
70 const OSSymbol
* gIODTRangeKey
;
72 const OSSymbol
* gIODTPersistKey
;
74 const OSSymbol
* gIODTDefaultInterruptController
;
75 const OSSymbol
* gIODTAAPLInterruptsKey
;
76 const OSSymbol
* gIODTPHandleKey
;
77 const OSSymbol
* gIODTInterruptCellKey
;
78 const OSSymbol
* gIODTInterruptParentKey
;
79 const OSSymbol
* gIODTNWInterruptMappingKey
;
81 OSDictionary
* gIODTSharedInterrupts
;
83 static IORegistryEntry
* MakeReferenceTable( DTEntry dtEntry
, bool copy
);
84 static void AddPHandle( IORegistryEntry
* regEntry
);
85 static void FreePhysicalMemory( vm_offset_t
* range
);
86 static bool IODTMapInterruptsSharing( IORegistryEntry
* regEntry
, OSDictionary
* allInts
);
89 IODeviceTreeAlloc( void * dtTop
)
91 IORegistryEntry
* parent
;
92 IORegistryEntry
* child
;
93 IORegistryIterator
* regIter
;
99 OSDictionary
* allInts
;
101 unsigned int propSize
;
105 gIODTPlane
= IORegistryEntry::makePlane( kIODeviceTreePlane
);
107 gIODTNameKey
= OSSymbol::withCStringNoCopy( "name" );
108 gIODTUnitKey
= OSSymbol::withCStringNoCopy( "AAPL,unit-string" );
109 gIODTCompatibleKey
= OSSymbol::withCStringNoCopy( "compatible" );
110 gIODTTypeKey
= OSSymbol::withCStringNoCopy( "device_type" );
111 gIODTModelKey
= OSSymbol::withCStringNoCopy( "model" );
112 gIODTTargetTypeKey
= OSSymbol::withCStringNoCopy( "target-type" );
113 gIODTSizeCellKey
= OSSymbol::withCStringNoCopy( "#size-cells" );
114 gIODTAddressCellKey
= OSSymbol::withCStringNoCopy( "#address-cells" );
115 gIODTRangeKey
= OSSymbol::withCStringNoCopy( "ranges" );
116 gIODTPersistKey
= OSSymbol::withCStringNoCopy( "IODTPersist" );
118 assert( gIODTPlane
&& gIODTCompatibleKey
119 && gIODTTypeKey
&& gIODTModelKey
120 && gIODTSizeCellKey
&& gIODTAddressCellKey
&& gIODTRangeKey
121 && gIODTPersistKey
);
123 gIODTDefaultInterruptController
124 = OSSymbol::withCStringNoCopy("IOPrimaryInterruptController");
125 gIODTNWInterruptMappingKey
126 = OSSymbol::withCStringNoCopy("IONWInterrupts");
128 gIODTAAPLInterruptsKey
129 = OSSymbol::withCStringNoCopy("AAPL,interrupts");
131 = OSSymbol::withCStringNoCopy("AAPL,phandle");
133 gIODTInterruptParentKey
134 = OSSymbol::withCStringNoCopy("interrupt-parent");
136 gIODTPHandles
= OSArray::withCapacity( 1 );
137 gIODTPHandleMap
= OSArray::withCapacity( 1 );
139 gIODTInterruptCellKey
140 = OSSymbol::withCStringNoCopy("#interrupt-cells");
142 assert( gIODTDefaultInterruptController
&& gIODTNWInterruptMappingKey
143 && gIODTAAPLInterruptsKey
144 && gIODTPHandleKey
&& gIODTInterruptParentKey
145 && gIODTPHandles
&& gIODTPHandleMap
146 && gIODTInterruptCellKey
149 freeDT
= (kSuccess
== DTLookupEntry( 0, "/chosen/memory-map", &mapEntry
))
150 && (kSuccess
== DTGetProperty( mapEntry
,
151 "DeviceTree", (void **) &dtMap
, &propSize
))
152 && ((2 * sizeof(uint32_t)) == propSize
);
154 parent
= MakeReferenceTable( (DTEntry
)dtTop
, freeDT
);
156 stack
= OSArray::withObjects( (const OSObject
**) &parent
, 1, 10 );
157 DTCreateEntryIterator( (DTEntry
)dtTop
, &iter
);
160 parent
= (IORegistryEntry
*)stack
->getObject( stack
->getCount() - 1);
162 stack
->removeObject( stack
->getCount() - 1);
164 while( kSuccess
== DTIterateEntries( iter
, &dtChild
) ) {
166 child
= MakeReferenceTable( dtChild
, freeDT
);
167 child
->attachToParent( parent
, gIODTPlane
);
171 if( kSuccess
== DTEnterEntry( iter
, dtChild
)) {
172 stack
->setObject( parent
);
175 // only registry holds retain
179 } while( stack
->getCount()
180 && (kSuccess
== DTExitEntry( iter
, &dtChild
)));
183 DTDisposeEntryIterator( iter
);
185 // parent is now root of the created tree
187 // make root name first compatible entry (purely cosmetic)
188 if( (prop
= (OSData
*) parent
->getProperty( gIODTCompatibleKey
))) {
189 parent
->setName( parent
->getName(), gIODTPlane
);
190 parent
->setName( (const char *) prop
->getBytesNoCopy() );
193 // attach tree to meta root
194 parent
->attachToParent( IORegistryEntry::getRegistryRoot(), gIODTPlane
);
198 // free original device tree
200 IODTFreeLoaderInfo( "DeviceTree",
201 (void *)dtMap
[0], (int) round_page(dtMap
[1]) );
206 gIODTSharedInterrupts
= OSDictionary::withCapacity(4);
207 allInts
= OSDictionary::withCapacity(4);
209 regIter
= IORegistryIterator::iterateOver( gIODTPlane
,
210 kIORegistryIterateRecursively
);
211 assert( regIter
&& allInts
&& gIODTSharedInterrupts
);
212 if( regIter
&& allInts
&& gIODTSharedInterrupts
) {
213 while( (child
= regIter
->getNextObject())) {
214 IODTMapInterruptsSharing( child
, allInts
);
215 if( !intMap
&& child
->getProperty( gIODTInterruptParentKey
))
223 parent
->setProperty("allInts", allInts
);
224 parent
->setProperty("sharedInts", gIODTSharedInterrupts
);
226 regIter
= IORegistryIterator::iterateOver( gIODTPlane
,
227 kIORegistryIterateRecursively
);
229 while( (child
= regIter
->getNextObject())) {
231 array
= OSDynamicCast(OSArray
, child
->getProperty( gIOInterruptSpecifiersKey
));
232 for( UInt32 i
= 0; array
&& (i
< array
->getCount()); i
++)
234 IOOptionBits options
;
235 IOReturn ret
= IODTGetInterruptOptions( child
, i
, &options
);
236 if( (ret
!= kIOReturnSuccess
) || options
)
237 IOLog("%s[%ld] %ld (%x)\n", child
->getName(), i
, options
, ret
);
247 // set a key in the root to indicate we found NW interrupt mapping
248 parent
->setProperty( gIODTNWInterruptMappingKey
,
249 (OSObject
*) gIODTNWInterruptMappingKey
);
254 int IODTGetLoaderInfo( const char *key
, void **infoAddr
, int *infoSize
)
256 IORegistryEntry
*chosen
;
259 unsigned int propSize
;
262 chosen
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
);
263 if ( chosen
== 0 ) return -1;
265 propObj
= OSDynamicCast( OSData
, chosen
->getProperty(key
) );
266 if ( propObj
== 0 ) goto cleanup
;
268 propSize
= propObj
->getLength();
269 if ( propSize
!= (2 * sizeof(dtptr_t
)) ) goto cleanup
;
271 propPtr
= (dtptr_t
*)propObj
->getBytesNoCopy();
272 if ( propPtr
== 0 ) goto cleanup
;
274 *infoAddr
= (void *)(uintptr_t) (propPtr
[0]);
275 *infoSize
= (int) (propPtr
[1]);
285 void IODTFreeLoaderInfo( const char *key
, void *infoAddr
, int infoSize
)
287 vm_offset_t range
[2];
288 IORegistryEntry
*chosen
;
290 range
[0] = (vm_offset_t
)infoAddr
;
291 range
[1] = (vm_offset_t
)infoSize
;
292 FreePhysicalMemory( range
);
295 chosen
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
);
297 chosen
->removeProperty(key
);
303 int IODTGetDefault(const char *key
, void *infoAddr
, unsigned int infoSize
)
305 IORegistryEntry
*defaults
;
307 unsigned int defaultSize
;
309 defaults
= IORegistryEntry::fromPath( "/defaults", gIODTPlane
);
310 if ( defaults
== 0 ) return -1;
312 defaultObj
= OSDynamicCast( OSData
, defaults
->getProperty(key
) );
313 if ( defaultObj
== 0 ) return -1;
315 defaultSize
= defaultObj
->getLength();
316 if ( defaultSize
> infoSize
) return -1;
318 memcpy( infoAddr
, defaultObj
->getBytesNoCopy(), defaultSize
);
323 static void FreePhysicalMemory( vm_offset_t
* range
)
327 virt
= ml_static_ptovirt( range
[0] );
329 ml_static_mfree( virt
, range
[1] );
333 static IORegistryEntry
*
334 MakeReferenceTable( DTEntry dtEntry
, bool copy
)
336 IORegistryEntry
*regEntry
;
337 OSDictionary
*propTable
;
338 const OSSymbol
*nameKey
;
341 DTPropertyIterator dtIter
;
343 unsigned int propSize
;
346 bool noLocation
= true;
349 regEntry
= new IOService
;
351 if( regEntry
&& (false == regEntry
->init())) {
357 (kSuccess
== DTCreatePropertyIterator( dtEntry
, &dtIter
))) {
359 kernelOnly
= (kSuccess
== DTGetProperty(dtEntry
, "kernel-only", &prop
, &propSize
));
360 propTable
= regEntry
->getPropertyTable();
362 while( kSuccess
== DTIterateProperties( dtIter
, &name
)) {
364 if( kSuccess
!= DTGetProperty( dtEntry
, name
, &prop
, &propSize
))
368 nameKey
= OSSymbol::withCString(name
);
369 data
= OSData::withBytes(prop
, propSize
);
371 nameKey
= OSSymbol::withCStringNoCopy(name
);
372 data
= OSData::withBytesNoCopy(prop
, propSize
);
374 assert( nameKey
&& data
);
377 data
->setSerializable(false);
379 propTable
->setObject( nameKey
, data
);
383 if( nameKey
== gIODTNameKey
) {
385 sym
= OSSymbol::withCString( (const char *) prop
);
387 sym
= OSSymbol::withCStringNoCopy( (const char *) prop
);
388 regEntry
->setName( sym
);
391 } else if( nameKey
== gIODTUnitKey
) {
392 // all OF strings are null terminated... except this one
393 if( propSize
>= (int) sizeof(location
))
394 propSize
= sizeof(location
) - 1;
395 strncpy( location
, (const char *) prop
, propSize
);
396 location
[ propSize
] = 0;
397 regEntry
->setLocation( location
);
398 propTable
->removeObject( gIODTUnitKey
);
401 } else if(noLocation
&& (!strncmp(name
, "reg", sizeof("reg")))) {
402 // default location - override later
403 snprintf(location
, sizeof(location
), "%X", *((uint32_t *) prop
));
404 regEntry
->setLocation( location
);
407 DTDisposePropertyIterator( dtIter
);
413 static void AddPHandle( IORegistryEntry
* regEntry
)
417 if( regEntry
->getProperty( gIODTInterruptCellKey
)
418 && (data
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTPHandleKey
)))) {
419 // a possible interrupt-parent
420 gIODTPHandles
->setObject( data
);
421 gIODTPHandleMap
->setObject( regEntry
);
425 static IORegistryEntry
* FindPHandle( UInt32 phandle
)
428 IORegistryEntry
*regEntry
= 0;
431 for( i
= 0; (data
= (OSData
*)gIODTPHandles
->getObject( i
)); i
++ ) {
432 if( phandle
== *((UInt32
*)data
->getBytesNoCopy())) {
433 regEntry
= (IORegistryEntry
*)
434 gIODTPHandleMap
->getObject( i
);
442 static bool GetUInt32( IORegistryEntry
* regEntry
, const OSSymbol
* name
,
449 if (!(obj
= regEntry
->copyProperty(name
))) return (false);
451 result
= ((data
= OSDynamicCast(OSData
, obj
)) && (sizeof(UInt32
) == data
->getLength()));
452 if (result
) *value
= *((UInt32
*) data
->getBytesNoCopy());
458 static IORegistryEntry
* IODTFindInterruptParent( IORegistryEntry
* regEntry
, IOItemCount index
)
460 IORegistryEntry
* parent
;
465 if( (data
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTInterruptParentKey
)))
466 && (sizeof(UInt32
) <= (len
= data
->getLength()))) {
467 if (((index
+ 1) * sizeof(UInt32
)) > len
)
469 phandle
= ((UInt32
*) data
->getBytesNoCopy())[index
];
470 parent
= FindPHandle( phandle
);
472 } else if( 0 == regEntry
->getProperty( "interrupt-controller"))
473 parent
= regEntry
->getParentEntry( gIODTPlane
);
480 const OSSymbol
* IODTInterruptControllerName( IORegistryEntry
* regEntry
)
487 ok
= GetUInt32( regEntry
, gIODTPHandleKey
, &phandle
);
491 snprintf(buf
, sizeof(buf
), "IOInterruptController%08X", (uint32_t)phandle
);
492 sym
= OSSymbol::withCString( buf
);
499 #define unexpected(a) { kprintf("unexpected %s:%d\n", __FILE__, __LINE__); a; }
501 static void IODTGetICellCounts( IORegistryEntry
* regEntry
,
502 UInt32
* iCellCount
, UInt32
* aCellCount
)
504 if( !GetUInt32( regEntry
, gIODTInterruptCellKey
, iCellCount
))
505 unexpected( *iCellCount
= 1 );
506 if( !GetUInt32( regEntry
, gIODTAddressCellKey
, aCellCount
))
510 static UInt32
IODTMapOneInterrupt( IORegistryEntry
* regEntry
, UInt32
* intSpec
, UInt32 index
,
511 OSData
** spec
, const OSSymbol
** controller
)
513 IORegistryEntry
*parent
= 0;
519 UInt32 acells
, icells
, pacells
, picells
, cell
;
520 UInt32 i
, original_icells
;
521 bool cmp
, ok
= false;
523 parent
= IODTFindInterruptParent( regEntry
, index
);
524 IODTGetICellCounts( parent
, &icells
, &acells
);
527 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "reg" ));
528 if( data
&& (data
->getLength() >= (acells
* sizeof(UInt32
))))
529 addrCmp
= (UInt32
*) data
->getBytesNoCopy();
531 original_icells
= icells
;
536 kprintf ("IODTMapOneInterrupt: current regEntry name %s\n", regEntry
->getName());
537 kprintf ("acells - icells: ");
538 for (i
= 0; i
< acells
; i
++) kprintf ("0x%08X ", addrCmp
[i
]);
540 for (i
= 0; i
< icells
; i
++) kprintf ("0x%08X ", intSpec
[i
]);
544 if( parent
&& (data
= OSDynamicCast( OSData
,
545 regEntry
->getProperty( "interrupt-controller")))) {
546 // found a controller - don't want to follow cascaded controllers
548 *spec
= OSData::withBytesNoCopy( (void *) intSpec
,
549 icells
* sizeof(UInt32
));
550 *controller
= IODTInterruptControllerName( regEntry
);
551 ok
= (*spec
&& *controller
);
552 } else if( parent
&& (data
= OSDynamicCast( OSData
,
553 regEntry
->getProperty( "interrupt-map")))) {
555 map
= (UInt32
*) data
->getBytesNoCopy();
556 endMap
= map
+ (data
->getLength() / sizeof(UInt32
));
557 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "interrupt-map-mask" ));
558 if( data
&& (data
->getLength() >= ((acells
+ icells
) * sizeof(UInt32
))))
559 maskCmp
= (UInt32
*) data
->getBytesNoCopy();
565 kprintf (" maskCmp: ");
566 for (i
= 0; i
< acells
+ icells
; i
++) {
569 kprintf ("0x%08X ", maskCmp
[i
]);
572 kprintf (" masked: ");
573 for (i
= 0; i
< acells
+ icells
; i
++) {
576 kprintf ("0x%08X ", ((i
< acells
) ? addrCmp
[i
] : intSpec
[i
-acells
]) & maskCmp
[i
]);
580 kprintf ("no maskCmp\n");
585 for (i
= 0; i
< acells
+ icells
; i
++) {
588 kprintf ("0x%08X ", map
[i
]);
592 for( i
= 0, cmp
= true; cmp
&& (i
< (acells
+ icells
)); i
++) {
593 cell
= (i
< acells
) ? addrCmp
[i
] : intSpec
[ i
- acells
];
596 cmp
= (cell
== map
[i
]);
599 map
+= acells
+ icells
;
600 if( 0 == (parent
= FindPHandle( *(map
++) )))
603 IODTGetICellCounts( parent
, &picells
, &pacells
);
606 intSpec
= map
+ pacells
;
609 map
+= pacells
+ picells
;
611 } while( !cmp
&& (map
< endMap
) );
617 IODTGetICellCounts( parent
, &icells
, &acells
);
623 return( ok
? original_icells
: 0 );
626 IOReturn
IODTGetInterruptOptions( IORegistryEntry
* regEntry
, int source
, IOOptionBits
* options
)
628 OSArray
* controllers
;
629 OSArray
* specifiers
;
636 controllers
= OSDynamicCast(OSArray
, regEntry
->getProperty(gIOInterruptControllersKey
));
637 specifiers
= OSDynamicCast(OSArray
, regEntry
->getProperty(gIOInterruptSpecifiersKey
));
639 if( !controllers
|| !specifiers
)
640 return (kIOReturnNoInterrupt
);
642 shared
= (OSArray
*) gIODTSharedInterrupts
->getObject(
643 (const OSSymbol
*) controllers
->getObject(source
) );
645 return (kIOReturnSuccess
);
647 spec
= specifiers
->getObject(source
);
649 return (kIOReturnNoInterrupt
);
651 for (unsigned int i
= 0;
652 (oneSpec
= shared
->getObject(i
))
653 && (!oneSpec
->isEqualTo(spec
));
657 *options
= kIODTInterruptShared
;
659 return (kIOReturnSuccess
);
662 static bool IODTMapInterruptsSharing( IORegistryEntry
* regEntry
, OSDictionary
* allInts
)
664 IORegistryEntry
* parent
;
673 OSArray
* controllerInts
;
674 const OSSymbol
* controller
= 0;
675 OSArray
* controllers
;
679 nw
= (0 == (local
= OSDynamicCast( OSData
,
680 regEntry
->getProperty( gIODTAAPLInterruptsKey
))));
681 if( nw
&& (0 == (local
= OSDynamicCast( OSData
,
682 regEntry
->getProperty( "interrupts")))))
683 return( true ); // nothing to see here
685 if( nw
&& (parent
= regEntry
->getParentEntry( gIODTPlane
))) {
686 // check for bridges on old world
687 if( (local2
= OSDynamicCast( OSData
,
688 parent
->getProperty( gIODTAAPLInterruptsKey
)))) {
694 localBits
= (UInt32
*) local
->getBytesNoCopy();
695 localEnd
= localBits
+ (local
->getLength() / sizeof(UInt32
));
697 mapped
= OSArray::withCapacity( 1 );
698 controllers
= OSArray::withCapacity( 1 );
700 ok
= (mapped
&& controllers
);
704 skip
= IODTMapOneInterrupt( regEntry
, localBits
, index
, &map
, &controller
);
706 IOLog("%s: error mapping interrupt[%d]\n",
707 regEntry
->getName(), mapped
->getCount());
711 map
= OSData::withData( local
, mapped
->getCount() * sizeof(UInt32
),
713 controller
= gIODTDefaultInterruptController
;
714 controller
->retain();
719 mapped
->setObject( map
);
720 controllers
->setObject( controller
);
724 controllerInts
= (OSArray
*) allInts
->getObject( controller
);
727 for (unsigned int i
= 0; (oneMap
= controllerInts
->getObject(i
)); i
++)
729 if (map
->isEqualTo(oneMap
))
731 controllerInts
= (OSArray
*) gIODTSharedInterrupts
->getObject( controller
);
733 controllerInts
->setObject(map
);
736 controllerInts
= OSArray::withObjects( (const OSObject
**) &map
, 1, 4 );
739 gIODTSharedInterrupts
->setObject( controller
, controllerInts
);
740 controllerInts
->release();
747 controllerInts
->setObject(map
);
751 controllerInts
= OSArray::withObjects( (const OSObject
**) &map
, 1, 16 );
754 allInts
->setObject( controller
, controllerInts
);
755 controllerInts
->release();
761 controller
->release();
763 } while( localBits
< localEnd
);
765 ok
&= (localBits
== localEnd
);
769 ok
= regEntry
->setProperty( gIOInterruptControllersKey
, controllers
);
770 ok
&= regEntry
->setProperty( gIOInterruptSpecifiersKey
, mapped
);
774 controllers
->release();
781 bool IODTMapInterrupts( IORegistryEntry
* regEntry
)
783 return( IODTMapInterruptsSharing( regEntry
, 0 ));
790 CompareKey( OSString
* key
,
791 const IORegistryEntry
* table
, const OSSymbol
* propName
,
792 OSString
** matchingName
)
801 const char *lastName
;
804 const char *result
= 0;
806 if( 0 == (prop
= table
->copyProperty( propName
))) return( 0 );
808 if( (data
= OSDynamicCast( OSData
, prop
))) {
809 names
= (const char *) data
->getBytesNoCopy();
810 lastName
= names
+ data
->getLength();
811 } else if( (string
= OSDynamicCast( OSString
, prop
))) {
812 names
= string
->getCStringNoCopy();
813 lastName
= names
+ string
->getLength() + 1;
817 ckey
= key
->getCStringNoCopy();
818 keyLen
= key
->getLength();
819 wild
= ('*' == key
->getChar( keyLen
- 1 ));
822 // for each name in the property
823 nlen
= strnlen(names
, lastName
- names
);
825 matched
= ((nlen
>= (keyLen
- 1)) && (0 == strncmp(ckey
, names
, keyLen
- 1)));
827 matched
= (keyLen
== nlen
) && (0 == strncmp(ckey
, names
, keyLen
));
832 names
= names
+ nlen
+ 1;
834 } while( (names
< lastName
) && (false == matched
));
837 if (result
&& matchingName
) *matchingName
= OSString::withCString( result
);
839 if (prop
) prop
->release();
841 return (result
!= 0);
845 bool IODTCompareNubName( const IORegistryEntry
* regEntry
,
846 OSString
* name
, OSString
** matchingName
)
850 matched
= CompareKey( name
, regEntry
, gIODTNameKey
, matchingName
)
851 || CompareKey( name
, regEntry
, gIODTCompatibleKey
, matchingName
)
852 || CompareKey( name
, regEntry
, gIODTTypeKey
, matchingName
)
853 || CompareKey( name
, regEntry
, gIODTModelKey
, matchingName
);
858 bool IODTMatchNubWithKeys( IORegistryEntry
* regEntry
,
864 obj
= OSUnserialize( keys
, 0 );
867 result
= regEntry
->compareNames( obj
);
871 else IOLog("Couldn't unserialize %s\n", keys
);
877 OSCollectionIterator
* IODTFindMatchingEntries( IORegistryEntry
* from
,
878 IOOptionBits options
, const char * keys
)
881 IORegistryEntry
*next
;
882 IORegistryIterator
*iter
;
883 OSCollectionIterator
*cIter
;
885 bool minus
= options
& kIODTExclusive
;
888 iter
= IORegistryIterator::iterateOver( from
, gIODTPlane
,
889 (options
& kIODTRecursive
) ? kIORegistryIterateRecursively
: 0 );
896 result
= OSSet::withCapacity( 3 );
901 while( (next
= iter
->getNextObject())) {
903 // Look for existence of a debug property to skip
904 if( next
->getProperty("AAPL,ignore"))
908 cmp
= IODTMatchNubWithKeys( next
, keys
);
909 if( (minus
&& (false == cmp
))
910 || ((false == minus
) && (false != cmp
)) )
911 result
->setObject( next
);
913 result
->setObject( next
);
915 } while( !iter
->isValid());
920 cIter
= OSCollectionIterator::withCollection( result
);
921 if (result
) result
->release();
927 struct IODTPersistent
{
928 IODTCompareAddressCellFunc compareFunc
;
929 IODTNVLocationFunc locationFunc
;
932 void IODTSetResolving( IORegistryEntry
* regEntry
,
933 IODTCompareAddressCellFunc compareFunc
,
934 IODTNVLocationFunc locationFunc
)
936 IODTPersistent persist
;
939 persist
.compareFunc
= compareFunc
;
940 persist
.locationFunc
= locationFunc
;
941 prop
= OSData::withBytes( &persist
, sizeof(persist
));
945 prop
->setSerializable(false);
946 regEntry
->setProperty( gIODTPersistKey
, prop
);
951 #if defined(__arm__) || defined(__i386__) || defined(__x86_64__)
952 static SInt32
DefaultCompare( UInt32 cellCount
, UInt32 left
[], UInt32 right
[] )
955 return( left
[ cellCount
] - right
[ cellCount
] );
958 #error Unknown architecture.
961 static void AddLengthToCells( UInt32 numCells
, UInt32
*cells
, UInt64 offset
)
965 cells
[0] += (UInt32
)offset
;
968 UInt64 sum
= cells
[numCells
- 1] + offset
;
969 cells
[numCells
- 1] = (UInt32
)sum
;
970 if (sum
> UINT32_MAX
) {
971 cells
[numCells
- 2] += (UInt32
)(sum
>> 32);
976 static IOPhysicalAddress
CellsValue( UInt32 numCells
, UInt32
*cells
)
979 return IOPhysical32( 0, cells
[0] );
981 return IOPhysical32( cells
[numCells
- 2], cells
[numCells
- 1] );
985 void IODTGetCellCounts( IORegistryEntry
* regEntry
,
986 UInt32
* sizeCount
, UInt32
* addressCount
)
988 if( !GetUInt32( regEntry
, gIODTSizeCellKey
, sizeCount
))
990 if( !GetUInt32( regEntry
, gIODTAddressCellKey
, addressCount
))
995 // Given addr & len cells from our child, find it in our ranges property, then
996 // look in our parent to resolve the base of the range for us.
998 // Range[]: child-addr our-addr child-len
999 // #cells: child ours child
1001 bool IODTResolveAddressCell( IORegistryEntry
* regEntry
,
1003 IOPhysicalAddress
* phys
, IOPhysicalLength
* lenOut
)
1005 IORegistryEntry
*parent
;
1007 // cells in addresses at regEntry
1008 UInt32 sizeCells
, addressCells
;
1009 // cells in addresses below regEntry
1010 UInt32 childSizeCells
, childAddressCells
;
1012 UInt32 cell
[ 8 ], propLen
;
1014 UInt32 endCell
[ 8 ];
1020 SInt64 diff
, diff2
, endDiff
;
1021 UInt64 len
, rangeLen
;
1023 IODTPersistent
*persist
;
1024 IODTCompareAddressCellFunc compare
;
1026 IODTGetCellCounts( regEntry
, &childSizeCells
, &childAddressCells
);
1027 childCells
= childAddressCells
+ childSizeCells
;
1029 if (childCells
> sizeof(cell
)/sizeof(cell
[0]))
1030 panic("IODTResolveAddressCell: Invalid device tree (%u,%u)", (uint32_t)childAddressCells
, (uint32_t)childSizeCells
);
1032 bcopy( cellsIn
, cell
, sizeof(UInt32
) * childCells
);
1033 *lenOut
= CellsValue( childSizeCells
, cellsIn
+ childAddressCells
);
1037 prop
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTRangeKey
));
1039 /* end of the road */
1040 *phys
= CellsValue( childAddressCells
, cell
);
1045 parent
= regEntry
->getParentEntry( gIODTPlane
);
1046 IODTGetCellCounts( parent
, &sizeCells
, &addressCells
);
1048 if( (propLen
= prop
->getLength())) {
1050 startRange
= (UInt32
*) prop
->getBytesNoCopy();
1052 endRanges
= range
+ (propLen
/ sizeof(UInt32
));
1054 prop
= (OSData
*) regEntry
->getProperty( gIODTPersistKey
);
1056 persist
= (IODTPersistent
*) prop
->getBytesNoCopy();
1057 compare
= persist
->compareFunc
;
1058 } else if (addressCells
== childAddressCells
) {
1059 compare
= DefaultCompare
;
1061 panic("There is no mixed comparison function yet...");
1066 range
+= (childCells
+ addressCells
) ) {
1068 // is cell start within range?
1069 diff
= (*compare
)( childAddressCells
, cell
, range
);
1071 if (childAddressCells
> sizeof(endCell
)/sizeof(endCell
[0]))
1072 panic("IODTResolveAddressCell: Invalid device tree (%u)", (uint32_t)childAddressCells
);
1074 bcopy(range
, endCell
, childAddressCells
* sizeof(UInt32
));
1076 rangeLen
= CellsValue(childSizeCells
, range
+ childAddressCells
+ addressCells
);
1077 AddLengthToCells(childAddressCells
, endCell
, rangeLen
);
1079 diff2
= (*compare
)( childAddressCells
, cell
, endCell
);
1081 // if start of cell < start of range, or end of range >= start of cell, skip
1082 if ((diff
< 0) || (diff2
>= 0))
1085 len
= CellsValue(childSizeCells
, cell
+ childAddressCells
);
1090 // search for cell end
1091 bcopy(cell
, endCell
, childAddressCells
* sizeof(UInt32
));
1093 AddLengthToCells(childAddressCells
, endCell
, len
- 1);
1095 for( lookRange
= startRange
;
1096 lookRange
< endRanges
;
1097 lookRange
+= (childCells
+ addressCells
) )
1099 // make sure end of cell >= range start
1100 endDiff
= (*compare
)( childAddressCells
, endCell
, lookRange
);
1104 UInt64 rangeStart
= CellsValue(addressCells
, range
+ childAddressCells
);
1105 UInt64 lookRangeStart
= CellsValue(addressCells
, lookRange
+ childAddressCells
);
1106 if ((endDiff
- len
+ 1 + lookRangeStart
) == (diff
+ rangeStart
))
1119 if (addressCells
+ sizeCells
> sizeof(cell
)/sizeof(cell
[0]))
1120 panic("IODTResolveAddressCell: Invalid device tree (%u, %u)", (uint32_t)addressCells
, (uint32_t)sizeCells
);
1122 // Get the physical start of the range from our parent
1123 bcopy( range
+ childAddressCells
, cell
, sizeof(UInt32
) * addressCells
);
1124 bzero( cell
+ addressCells
, sizeof(UInt32
) * sizeCells
);
1126 } /* else zero length range => pass thru to parent */
1129 childSizeCells
= sizeCells
;
1130 childAddressCells
= addressCells
;
1131 childCells
= childAddressCells
+ childSizeCells
;
1133 while( ok
&& regEntry
);
1139 OSArray
* IODTResolveAddressing( IORegistryEntry
* regEntry
,
1140 const char * addressPropertyName
,
1141 IODeviceMemory
* parent
)
1143 IORegistryEntry
*parentEntry
;
1144 OSData
*addressProperty
;
1145 UInt32 sizeCells
, addressCells
, cells
;
1148 IOPhysicalAddress phys
;
1149 IOPhysicalLength len
;
1151 IODeviceMemory
*range
;
1153 parentEntry
= regEntry
->getParentEntry( gIODTPlane
);
1154 addressProperty
= (OSData
*) regEntry
->getProperty( addressPropertyName
);
1155 if( (0 == addressProperty
) || (0 == parentEntry
))
1158 IODTGetCellCounts( parentEntry
, &sizeCells
, &addressCells
);
1162 cells
= sizeCells
+ addressCells
;
1163 reg
= (UInt32
*) addressProperty
->getBytesNoCopy();
1164 num
= addressProperty
->getLength() / (4 * cells
);
1166 array
= OSArray::withCapacity( 1 );
1170 for( i
= 0; i
< num
; i
++) {
1171 if( IODTResolveAddressCell( parentEntry
, reg
, &phys
, &len
)) {
1174 range
= IODeviceMemory::withSubRange( parent
,
1175 phys
- parent
->getPhysicalSegment(0, 0, kIOMemoryMapperNone
), len
);
1177 range
= IODeviceMemory::withRange( phys
, len
);
1179 array
->setObject( range
);
1184 regEntry
->setProperty( gIODeviceMemoryKey
, array
);
1185 array
->release(); /* ??? */
1190 static void IODTGetNVLocation(
1191 IORegistryEntry
* parent
,
1192 IORegistryEntry
* regEntry
,
1193 UInt8
* busNum
, UInt8
* deviceNum
, UInt8
* functionNum
)
1197 IODTPersistent
*persist
;
1200 prop
= (OSData
*) parent
->getProperty( gIODTPersistKey
);
1202 persist
= (IODTPersistent
*) prop
->getBytesNoCopy();
1203 (*persist
->locationFunc
)( regEntry
, busNum
, deviceNum
, functionNum
);
1205 prop
= (OSData
*) regEntry
->getProperty( "reg" );
1208 cell
= (UInt32
*) prop
->getBytesNoCopy();
1210 *deviceNum
= 0x1f & (cell
[ 0 ] >> 24);
1220 * Try to make the same messed up descriptor as Mac OS
1223 IOReturn
IODTMakeNVDescriptor( IORegistryEntry
* regEntry
,
1224 IONVRAMDescriptor
* hdr
)
1226 IORegistryEntry
*parent
;
1228 UInt32 bridgeDevices
;
1236 for(level
= 0, bridgeDevices
= 0;
1237 (parent
= regEntry
->getParentEntry( gIODTPlane
)) && (level
< 7); level
++ ) {
1239 IODTGetNVLocation( parent
, regEntry
,
1240 &busNum
, &deviceNum
, &functionNum
);
1242 bridgeDevices
|= ((deviceNum
& 0x1f) << ((level
- 1) * 5));
1244 hdr
->busNum
= busNum
;
1245 hdr
->deviceNum
= deviceNum
;
1246 hdr
->functionNum
= functionNum
;
1250 hdr
->bridgeCount
= level
- 2;
1251 hdr
->bridgeDevices
= bridgeDevices
;
1253 return( kIOReturnSuccess
);
1256 OSData
* IODTFindSlotName( IORegistryEntry
* regEntry
, UInt32 deviceNumber
)
1258 IORegistryEntry
*parent
;
1268 data
= (OSData
*) regEntry
->getProperty("AAPL,slot-name");
1271 parent
= regEntry
->getParentEntry( gIODTPlane
);
1274 data
= OSDynamicCast( OSData
, parent
->getProperty("slot-names"));
1277 if( data
->getLength() <= 4)
1280 bits
= (UInt32
*) data
->getBytesNoCopy();
1282 if( (0 == (mask
& (1 << deviceNumber
))))
1285 names
= (char *)(bits
+ 1);
1286 lastName
= names
+ (data
->getLength() - 4);
1288 for( i
= 0; (i
<= deviceNumber
) && (names
< lastName
); i
++ ) {
1290 if( mask
& (1 << i
)) {
1291 nlen
= 1 + strnlen(names
, lastName
- names
);
1292 if( i
== deviceNumber
) {
1293 data
= OSData::withBytesNoCopy(names
, nlen
);
1295 regEntry
->setProperty("AAPL,slot-name", data
);
1307 extern "C" IOReturn
IONDRVLibrariesInitialize( IOService
* provider
)
1309 return( kIOReturnUnsupported
);