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 typedef UInt64 dtptr_t
;
43 typedef UInt32 dtptr_t
;
46 #include <machine/machine_routines.h>
49 int IODTGetLoaderInfo( const char *key
, void **infoAddr
, int *infosize
);
50 void IODTFreeLoaderInfo( const char *key
, void *infoAddr
, int infoSize
);
51 int IODTGetDefault(const char *key
, void *infoAddr
, unsigned int infoSize
);
54 #include <IOKit/assert.h>
56 #define IODTSUPPORTDEBUG 0
58 const IORegistryPlane
* gIODTPlane
;
60 static OSArray
* gIODTPHandles
;
61 static OSArray
* gIODTPHandleMap
;
62 static OSData
* gIODTResolvers
;
64 const OSSymbol
* gIODTNameKey
;
65 const OSSymbol
* gIODTUnitKey
;
66 const OSSymbol
* gIODTCompatibleKey
;
67 const OSSymbol
* gIODTTypeKey
;
68 const OSSymbol
* gIODTModelKey
;
69 const OSSymbol
* gIODTTargetTypeKey
;
71 const OSSymbol
* gIODTSizeCellKey
;
72 const OSSymbol
* gIODTAddressCellKey
;
73 const OSSymbol
* gIODTRangeKey
;
75 const OSSymbol
* gIODTPersistKey
;
77 const OSSymbol
* gIODTDefaultInterruptController
;
78 const OSSymbol
* gIODTAAPLInterruptsKey
;
79 const OSSymbol
* gIODTPHandleKey
;
80 const OSSymbol
* gIODTInterruptCellKey
;
81 const OSSymbol
* gIODTInterruptParentKey
;
82 const OSSymbol
* gIODTNWInterruptMappingKey
;
84 OSDictionary
* gIODTSharedInterrupts
;
86 static IOLock
* gIODTResolversLock
;
88 static IORegistryEntry
* MakeReferenceTable( DTEntry dtEntry
, bool copy
);
89 static void AddPHandle( IORegistryEntry
* regEntry
);
90 static void FreePhysicalMemory( vm_offset_t
* range
);
91 static bool IODTMapInterruptsSharing( IORegistryEntry
* regEntry
, OSDictionary
* allInts
);
94 IODeviceTreeAlloc( void * dtTop
)
96 IORegistryEntry
* parent
;
97 IORegistryEntry
* child
;
98 IORegistryIterator
* regIter
;
99 OpaqueDTEntryIterator iter
;
104 OSDictionary
* allInts
;
106 unsigned int propSize
;
110 gIODTPlane
= IORegistryEntry::makePlane( kIODeviceTreePlane
);
112 gIODTNameKey
= OSSymbol::withCStringNoCopy( "name" );
113 gIODTUnitKey
= OSSymbol::withCStringNoCopy( "AAPL,unit-string" );
114 gIODTCompatibleKey
= OSSymbol::withCStringNoCopy( "compatible" );
115 gIODTTypeKey
= OSSymbol::withCStringNoCopy( "device_type" );
116 gIODTModelKey
= OSSymbol::withCStringNoCopy( "model" );
117 gIODTTargetTypeKey
= OSSymbol::withCStringNoCopy( "target-type" );
118 gIODTSizeCellKey
= OSSymbol::withCStringNoCopy( "#size-cells" );
119 gIODTAddressCellKey
= OSSymbol::withCStringNoCopy( "#address-cells" );
120 gIODTRangeKey
= OSSymbol::withCStringNoCopy( "ranges" );
121 gIODTPersistKey
= OSSymbol::withCStringNoCopy( "IODTPersist" );
123 assert( gIODTPlane
&& gIODTCompatibleKey
124 && gIODTTypeKey
&& gIODTModelKey
125 && gIODTSizeCellKey
&& gIODTAddressCellKey
&& gIODTRangeKey
126 && gIODTPersistKey
);
128 gIODTDefaultInterruptController
129 = OSSymbol::withCStringNoCopy("IOPrimaryInterruptController");
130 gIODTNWInterruptMappingKey
131 = OSSymbol::withCStringNoCopy("IONWInterrupts");
133 gIODTAAPLInterruptsKey
134 = OSSymbol::withCStringNoCopy("AAPL,interrupts");
136 = OSSymbol::withCStringNoCopy("AAPL,phandle");
138 gIODTInterruptParentKey
139 = OSSymbol::withCStringNoCopy("interrupt-parent");
141 gIODTPHandles
= OSArray::withCapacity( 1 );
142 gIODTPHandleMap
= OSArray::withCapacity( 1 );
143 gIODTResolvers
= OSData::withCapacity(16);
145 gIODTResolversLock
= IOLockAlloc();
147 gIODTInterruptCellKey
148 = OSSymbol::withCStringNoCopy("#interrupt-cells");
150 assert( gIODTDefaultInterruptController
&& gIODTNWInterruptMappingKey
151 && gIODTAAPLInterruptsKey
152 && gIODTPHandleKey
&& gIODTInterruptParentKey
153 && gIODTPHandles
&& gIODTPHandleMap
&& gIODTResolvers
&& gIODTResolversLock
154 && gIODTInterruptCellKey
157 freeDT
= (kSuccess
== DTLookupEntry( 0, "/chosen/memory-map", &mapEntry
))
158 && (kSuccess
== DTGetProperty( mapEntry
,
159 "DeviceTree", (void **) &dtMap
, &propSize
))
160 && ((2 * sizeof(uint32_t)) == propSize
);
162 parent
= MakeReferenceTable((DTEntry
)dtTop
, freeDT
);
164 stack
= OSArray::withObjects((const OSObject
**) &parent
, 1, 10 );
165 DTInitEntryIterator((DTEntry
)dtTop
, &iter
);
168 parent
= (IORegistryEntry
*)stack
->getObject( stack
->getCount() - 1);
170 stack
->removeObject( stack
->getCount() - 1);
172 while (kSuccess
== DTIterateEntries( &iter
, &dtChild
)) {
173 child
= MakeReferenceTable( dtChild
, freeDT
);
174 child
->attachToParent( parent
, gIODTPlane
);
178 if (kSuccess
== DTEnterEntry( &iter
, dtChild
)) {
179 stack
->setObject( parent
);
182 // only registry holds retain
185 } while (stack
->getCount()
186 && (kSuccess
== DTExitEntry( &iter
, &dtChild
)));
189 assert(kSuccess
!= DTExitEntry(&iter
, &dtChild
));
191 // parent is now root of the created tree
193 // make root name first compatible entry (purely cosmetic)
194 if ((prop
= (OSData
*) parent
->getProperty( gIODTCompatibleKey
))) {
195 parent
->setName( parent
->getName(), gIODTPlane
);
196 parent
->setName((const char *) prop
->getBytesNoCopy());
199 // attach tree to meta root
200 parent
->attachToParent( IORegistryEntry::getRegistryRoot(), gIODTPlane
);
204 // free original device tree
206 IODTFreeLoaderInfo( "DeviceTree",
207 (void *)dtMap
[0], (int) round_page(dtMap
[1]));
212 gIODTSharedInterrupts
= OSDictionary::withCapacity(4);
213 allInts
= OSDictionary::withCapacity(4);
215 regIter
= IORegistryIterator::iterateOver( gIODTPlane
,
216 kIORegistryIterateRecursively
);
217 assert( regIter
&& allInts
&& gIODTSharedInterrupts
);
218 if (regIter
&& allInts
&& gIODTSharedInterrupts
) {
219 while ((child
= regIter
->getNextObject())) {
220 IODTMapInterruptsSharing( child
, allInts
);
221 if (!intMap
&& child
->getProperty( gIODTInterruptParentKey
)) {
229 parent
->setProperty("allInts", allInts
);
230 parent
->setProperty("sharedInts", gIODTSharedInterrupts
);
232 regIter
= IORegistryIterator::iterateOver( gIODTPlane
,
233 kIORegistryIterateRecursively
);
235 while ((child
= regIter
->getNextObject())) {
237 array
= OSDynamicCast(OSArray
, child
->getProperty( gIOInterruptSpecifiersKey
));
238 for (UInt32 i
= 0; array
&& (i
< array
->getCount()); i
++) {
239 IOOptionBits options
;
240 IOReturn ret
= IODTGetInterruptOptions( child
, i
, &options
);
241 if ((ret
!= kIOReturnSuccess
) || options
) {
242 IOLog("%s[%ld] %ld (%x)\n", child
->getName(), i
, options
, ret
);
253 // set a key in the root to indicate we found NW interrupt mapping
254 parent
->setProperty( gIODTNWInterruptMappingKey
,
255 (OSObject
*) gIODTNWInterruptMappingKey
);
262 IODTGetLoaderInfo( const char *key
, void **infoAddr
, int *infoSize
)
264 IORegistryEntry
*chosen
;
267 unsigned int propSize
;
270 chosen
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
);
275 propObj
= OSDynamicCast( OSData
, chosen
->getProperty(key
));
280 propSize
= propObj
->getLength();
281 if (propSize
!= (2 * sizeof(dtptr_t
))) {
285 propPtr
= (dtptr_t
*)propObj
->getBytesNoCopy();
290 *infoAddr
= (void *)(uintptr_t) (propPtr
[0]);
291 *infoSize
= (int) (propPtr
[1]);
302 IODTFreeLoaderInfo( const char *key
, void *infoAddr
, int infoSize
)
304 vm_offset_t range
[2];
305 IORegistryEntry
*chosen
;
307 range
[0] = (vm_offset_t
)infoAddr
;
308 range
[1] = (vm_offset_t
)infoSize
;
309 FreePhysicalMemory( range
);
312 chosen
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
);
314 chosen
->removeProperty(key
);
321 IODTGetDefault(const char *key
, void *infoAddr
, unsigned int infoSize
)
323 IORegistryEntry
*defaults
;
325 unsigned int defaultSize
;
327 defaults
= IORegistryEntry::fromPath( "/defaults", gIODTPlane
);
332 defaultObj
= OSDynamicCast( OSData
, defaults
->getProperty(key
));
333 if (defaultObj
== 0) {
337 defaultSize
= defaultObj
->getLength();
338 if (defaultSize
> infoSize
) {
342 memcpy( infoAddr
, defaultObj
->getBytesNoCopy(), defaultSize
);
348 FreePhysicalMemory( vm_offset_t
* range
)
352 virt
= ml_static_ptovirt( range
[0] );
354 ml_static_mfree( virt
, range
[1] );
358 static IORegistryEntry
*
359 MakeReferenceTable( DTEntry dtEntry
, bool copy
)
361 IORegistryEntry
*regEntry
;
362 OSDictionary
*propTable
;
363 const OSSymbol
*nameKey
;
366 OpaqueDTPropertyIterator dtIter
;
368 unsigned int propSize
;
371 bool noLocation
= true;
374 regEntry
= new IOService
;
376 if (regEntry
&& (false == regEntry
->init())) {
382 (kSuccess
== DTInitPropertyIterator( dtEntry
, &dtIter
))) {
383 kernelOnly
= (kSuccess
== DTGetProperty(dtEntry
, "kernel-only", &prop
, &propSize
));
384 propTable
= regEntry
->getPropertyTable();
386 while (kSuccess
== DTIterateProperties( &dtIter
, &name
)) {
387 if (kSuccess
!= DTGetProperty( dtEntry
, name
, &prop
, &propSize
)) {
392 nameKey
= OSSymbol::withCString(name
);
393 data
= OSData::withBytes(prop
, propSize
);
395 nameKey
= OSSymbol::withCStringNoCopy(name
);
396 data
= OSData::withBytesNoCopy(prop
, propSize
);
398 assert( nameKey
&& data
);
401 data
->setSerializable(false);
404 propTable
->setObject( nameKey
, data
);
408 if (nameKey
== gIODTNameKey
) {
410 sym
= OSSymbol::withCString((const char *) prop
);
412 sym
= OSSymbol::withCStringNoCopy((const char *) prop
);
414 regEntry
->setName( sym
);
416 } else if (nameKey
== gIODTUnitKey
) {
417 // all OF strings are null terminated... except this one
418 if (propSize
>= (int) sizeof(location
)) {
419 propSize
= sizeof(location
) - 1;
421 strncpy( location
, (const char *) prop
, propSize
);
422 location
[propSize
] = 0;
423 regEntry
->setLocation( location
);
424 propTable
->removeObject( gIODTUnitKey
);
426 } else if (noLocation
&& (!strncmp(name
, "reg", sizeof("reg")))) {
427 // default location - override later
428 snprintf(location
, sizeof(location
), "%X", *((uint32_t *) prop
));
429 regEntry
->setLocation( location
);
438 AddPHandle( IORegistryEntry
* regEntry
)
442 if (regEntry
->getProperty( gIODTInterruptCellKey
)
443 && (data
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTPHandleKey
)))) {
444 // a possible interrupt-parent
445 gIODTPHandles
->setObject( data
);
446 gIODTPHandleMap
->setObject( regEntry
);
450 static IORegistryEntry
*
451 FindPHandle( UInt32 phandle
)
454 IORegistryEntry
*regEntry
= 0;
457 for (i
= 0; (data
= (OSData
*)gIODTPHandles
->getObject( i
)); i
++) {
458 if (phandle
== *((UInt32
*)data
->getBytesNoCopy())) {
459 regEntry
= (IORegistryEntry
*)
460 gIODTPHandleMap
->getObject( i
);
469 GetUInt32( IORegistryEntry
* regEntry
, const OSSymbol
* name
,
476 if (!(obj
= regEntry
->copyProperty(name
))) {
480 result
= ((data
= OSDynamicCast(OSData
, obj
)) && (sizeof(UInt32
) == data
->getLength()));
482 *value
= *((UInt32
*) data
->getBytesNoCopy());
489 static IORegistryEntry
*
490 IODTFindInterruptParent( IORegistryEntry
* regEntry
, IOItemCount index
)
492 IORegistryEntry
* parent
;
497 if ((data
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTInterruptParentKey
)))
498 && (sizeof(UInt32
) <= (len
= data
->getLength()))) {
499 if (((index
+ 1) * sizeof(UInt32
)) > len
) {
502 phandle
= ((UInt32
*) data
->getBytesNoCopy())[index
];
503 parent
= FindPHandle( phandle
);
504 } else if (0 == regEntry
->getProperty( "interrupt-controller")) {
505 parent
= regEntry
->getParentEntry( gIODTPlane
);
514 IODTInterruptControllerName( IORegistryEntry
* regEntry
)
521 ok
= GetUInt32( regEntry
, gIODTPHandleKey
, &phandle
);
525 snprintf(buf
, sizeof(buf
), "IOInterruptController%08X", (uint32_t)phandle
);
526 sym
= OSSymbol::withCString( buf
);
534 #define unexpected(a) { kprintf("unexpected %s:%d\n", __FILE__, __LINE__); a; }
537 IODTGetICellCounts( IORegistryEntry
* regEntry
,
538 UInt32
* iCellCount
, UInt32
* aCellCount
)
540 if (!GetUInt32( regEntry
, gIODTInterruptCellKey
, iCellCount
)) {
541 unexpected( *iCellCount
= 1 );
543 if (!GetUInt32( regEntry
, gIODTAddressCellKey
, aCellCount
)) {
549 IODTMapOneInterrupt( IORegistryEntry
* regEntry
, UInt32
* intSpec
, UInt32 index
,
550 OSData
** spec
, const OSSymbol
** controller
)
552 IORegistryEntry
*parent
= 0;
558 UInt32 acells
, icells
, pacells
, picells
, cell
;
559 UInt32 i
, original_icells
;
560 bool cmp
, ok
= false;
562 parent
= IODTFindInterruptParent( regEntry
, index
);
563 IODTGetICellCounts( parent
, &icells
, &acells
);
566 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "reg" ));
567 if (data
&& (data
->getLength() >= (acells
* sizeof(UInt32
)))) {
568 addrCmp
= (UInt32
*) data
->getBytesNoCopy();
571 original_icells
= icells
;
576 kprintf("IODTMapOneInterrupt: current regEntry name %s\n", regEntry
->getName());
577 kprintf("acells - icells: ");
578 for (i
= 0; i
< acells
; i
++) {
579 kprintf("0x%08X ", addrCmp
[i
]);
582 for (i
= 0; i
< icells
; i
++) {
583 kprintf("0x%08X ", intSpec
[i
]);
588 if (parent
&& (data
= OSDynamicCast( OSData
,
589 regEntry
->getProperty( "interrupt-controller")))) {
590 // found a controller - don't want to follow cascaded controllers
592 *spec
= OSData::withBytesNoCopy((void *) intSpec
,
593 icells
* sizeof(UInt32
));
594 *controller
= IODTInterruptControllerName( regEntry
);
595 ok
= (*spec
&& *controller
);
596 } else if (parent
&& (data
= OSDynamicCast( OSData
,
597 regEntry
->getProperty( "interrupt-map")))) {
599 map
= (UInt32
*) data
->getBytesNoCopy();
600 endMap
= map
+ (data
->getLength() / sizeof(UInt32
));
601 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "interrupt-map-mask" ));
602 if (data
&& (data
->getLength() >= ((acells
+ icells
) * sizeof(UInt32
)))) {
603 maskCmp
= (UInt32
*) data
->getBytesNoCopy();
610 kprintf(" maskCmp: ");
611 for (i
= 0; i
< acells
+ icells
; i
++) {
615 kprintf("0x%08X ", maskCmp
[i
]);
618 kprintf(" masked: ");
619 for (i
= 0; i
< acells
+ icells
; i
++) {
623 kprintf("0x%08X ", ((i
< acells
) ? addrCmp
[i
] : intSpec
[i
- acells
]) & maskCmp
[i
]);
627 kprintf("no maskCmp\n");
633 for (i
= 0; i
< acells
+ icells
; i
++) {
637 kprintf("0x%08X ", map
[i
]);
641 for (i
= 0, cmp
= true; cmp
&& (i
< (acells
+ icells
)); i
++) {
642 cell
= (i
< acells
) ? addrCmp
[i
] : intSpec
[i
- acells
];
646 cmp
= (cell
== map
[i
]);
649 map
+= acells
+ icells
;
650 if (0 == (parent
= FindPHandle( *(map
++)))) {
654 IODTGetICellCounts( parent
, &picells
, &pacells
);
657 intSpec
= map
+ pacells
;
660 map
+= pacells
+ picells
;
662 } while (!cmp
&& (map
< endMap
));
669 IODTGetICellCounts( parent
, &icells
, &acells
);
674 return ok
? original_icells
: 0;
678 IODTGetInterruptOptions( IORegistryEntry
* regEntry
, int source
, IOOptionBits
* options
)
680 OSArray
* controllers
;
681 OSArray
* specifiers
;
688 controllers
= OSDynamicCast(OSArray
, regEntry
->getProperty(gIOInterruptControllersKey
));
689 specifiers
= OSDynamicCast(OSArray
, regEntry
->getProperty(gIOInterruptSpecifiersKey
));
691 if (!controllers
|| !specifiers
) {
692 return kIOReturnNoInterrupt
;
695 shared
= (OSArray
*) gIODTSharedInterrupts
->getObject(
696 (const OSSymbol
*) controllers
->getObject(source
));
698 return kIOReturnSuccess
;
701 spec
= specifiers
->getObject(source
);
703 return kIOReturnNoInterrupt
;
706 for (unsigned int i
= 0;
707 (oneSpec
= shared
->getObject(i
))
708 && (!oneSpec
->isEqualTo(spec
));
713 *options
= kIODTInterruptShared
;
716 return kIOReturnSuccess
;
720 IODTMapInterruptsSharing( IORegistryEntry
* regEntry
, OSDictionary
* allInts
)
722 IORegistryEntry
* parent
;
731 OSArray
* controllerInts
;
732 const OSSymbol
* controller
= 0;
733 OSArray
* controllers
;
737 nw
= (0 == (local
= OSDynamicCast( OSData
,
738 regEntry
->getProperty( gIODTAAPLInterruptsKey
))));
739 if (nw
&& (0 == (local
= OSDynamicCast( OSData
,
740 regEntry
->getProperty( "interrupts"))))) {
741 return true; // nothing to see here
743 if (nw
&& (parent
= regEntry
->getParentEntry( gIODTPlane
))) {
744 // check for bridges on old world
745 if ((local2
= OSDynamicCast( OSData
,
746 parent
->getProperty( gIODTAAPLInterruptsKey
)))) {
752 localBits
= (UInt32
*) local
->getBytesNoCopy();
753 localEnd
= localBits
+ (local
->getLength() / sizeof(UInt32
));
755 mapped
= OSArray::withCapacity( 1 );
756 controllers
= OSArray::withCapacity( 1 );
758 ok
= (mapped
&& controllers
);
763 skip
= IODTMapOneInterrupt( regEntry
, localBits
, index
, &map
, &controller
);
765 IOLog("%s: error mapping interrupt[%d]\n",
766 regEntry
->getName(), mapped
->getCount());
770 map
= OSData::withData( local
, mapped
->getCount() * sizeof(UInt32
),
772 controller
= gIODTDefaultInterruptController
;
773 controller
->retain();
778 mapped
->setObject( map
);
779 controllers
->setObject( controller
);
782 controllerInts
= (OSArray
*) allInts
->getObject( controller
);
783 if (controllerInts
) {
784 for (unsigned int i
= 0; (oneMap
= controllerInts
->getObject(i
)); i
++) {
785 if (map
->isEqualTo(oneMap
)) {
786 controllerInts
= (OSArray
*) gIODTSharedInterrupts
->getObject( controller
);
787 if (controllerInts
) {
788 controllerInts
->setObject(map
);
790 controllerInts
= OSArray::withObjects((const OSObject
**) &map
, 1, 4 );
791 if (controllerInts
) {
792 gIODTSharedInterrupts
->setObject( controller
, controllerInts
);
793 controllerInts
->release();
800 controllerInts
->setObject(map
);
803 controllerInts
= OSArray::withObjects((const OSObject
**) &map
, 1, 16 );
804 if (controllerInts
) {
805 allInts
->setObject( controller
, controllerInts
);
806 controllerInts
->release();
812 controller
->release();
813 } while (localBits
< localEnd
);
816 ok
&= (localBits
== localEnd
);
820 ok
= regEntry
->setProperty( gIOInterruptControllersKey
, controllers
);
821 ok
&= regEntry
->setProperty( gIOInterruptSpecifiersKey
, mapped
);
825 controllers
->release();
835 IODTMapInterrupts( IORegistryEntry
* regEntry
)
837 return IODTMapInterruptsSharing( regEntry
, 0 );
844 CompareKey( OSString
* key
,
845 const IORegistryEntry
* table
, const OSSymbol
* propName
,
846 OSString
** matchingName
)
855 const char *lastName
;
858 const char *result
= 0;
860 if (0 == (prop
= table
->copyProperty( propName
))) {
864 if ((data
= OSDynamicCast( OSData
, prop
))) {
865 names
= (const char *) data
->getBytesNoCopy();
866 lastName
= names
+ data
->getLength();
867 } else if ((string
= OSDynamicCast( OSString
, prop
))) {
868 names
= string
->getCStringNoCopy();
869 lastName
= names
+ string
->getLength() + 1;
875 ckey
= key
->getCStringNoCopy();
876 keyLen
= key
->getLength();
877 wild
= ('*' == key
->getChar( keyLen
- 1 ));
880 // for each name in the property
881 nlen
= strnlen(names
, lastName
- names
);
883 matched
= ((nlen
>= (keyLen
- 1)) && (0 == strncmp(ckey
, names
, keyLen
- 1)));
885 matched
= (keyLen
== nlen
) && (0 == strncmp(ckey
, names
, keyLen
));
892 names
= names
+ nlen
+ 1;
893 } while ((names
< lastName
) && (false == matched
));
896 if (result
&& matchingName
) {
897 *matchingName
= OSString::withCString( result
);
909 IODTCompareNubName( const IORegistryEntry
* regEntry
,
910 OSString
* name
, OSString
** matchingName
)
914 matched
= CompareKey( name
, regEntry
, gIODTNameKey
, matchingName
)
915 || CompareKey( name
, regEntry
, gIODTCompatibleKey
, matchingName
)
916 || CompareKey( name
, regEntry
, gIODTTypeKey
, matchingName
)
917 || CompareKey( name
, regEntry
, gIODTModelKey
, matchingName
);
923 IODTMatchNubWithKeys( IORegistryEntry
* regEntry
,
929 obj
= OSUnserialize( keys
, 0 );
932 result
= regEntry
->compareNames( obj
);
937 IOLog("Couldn't unserialize %s\n", keys
);
944 OSCollectionIterator
*
945 IODTFindMatchingEntries( IORegistryEntry
* from
,
946 IOOptionBits options
, const char * keys
)
949 IORegistryEntry
*next
;
950 IORegistryIterator
*iter
;
951 OSCollectionIterator
*cIter
;
953 bool minus
= options
& kIODTExclusive
;
956 iter
= IORegistryIterator::iterateOver( from
, gIODTPlane
,
957 (options
& kIODTRecursive
) ? kIORegistryIterateRecursively
: 0 );
963 result
= OSSet::withCapacity( 3 );
969 while ((next
= iter
->getNextObject())) {
970 // Look for existence of a debug property to skip
971 if (next
->getProperty("AAPL,ignore")) {
976 cmp
= IODTMatchNubWithKeys( next
, keys
);
977 if ((minus
&& (false == cmp
))
978 || ((false == minus
) && (false != cmp
))) {
979 result
->setObject( next
);
982 result
->setObject( next
);
985 } while (!iter
->isValid());
990 cIter
= OSCollectionIterator::withCollection( result
);
999 struct IODTPersistent
{
1000 IODTCompareAddressCellFunc compareFunc
;
1004 IODTSetResolving( IORegistryEntry
* regEntry
,
1005 IODTCompareAddressCellFunc compareFunc
,
1006 IODTNVLocationFunc locationFunc __unused
)
1008 IODTPersistent persist
;
1009 IODTPersistent
* entry
;
1011 unsigned int index
, count
;
1013 IOLockLock(gIODTResolversLock
);
1015 count
= (gIODTResolvers
->getLength() / sizeof(IODTPersistent
));
1016 entry
= (typeof(entry
))gIODTResolvers
->getBytesNoCopy();
1017 for (index
= 0; index
< count
; index
++) {
1018 if (compareFunc
== entry
->compareFunc
) {
1023 if (index
== count
) {
1024 persist
.compareFunc
= compareFunc
;
1025 if (!gIODTResolvers
->appendBytes(&persist
, sizeof(IODTPersistent
))) {
1026 panic("IODTSetResolving");
1030 IOLockUnlock(gIODTResolversLock
);
1032 num
= OSNumber::withNumber(index
, 32);
1033 regEntry
->setProperty(gIODTPersistKey
, num
);
1034 OSSafeReleaseNULL(num
);
1039 #if defined(__arm64__)
1041 DefaultCompare( UInt32 cellCount
, UInt32 left
[], UInt32 right
[] )
1045 if (cellCount
== 2) {
1046 diff
= IOPhysical32(left
[1], left
[0]) - IOPhysical32(right
[1], right
[0]);
1047 } else if (cellCount
== 1) {
1048 diff
= (left
[0] - right
[0]);
1050 panic("DefaultCompare only knows how to handle 1 or 2 cells.");
1055 #elif defined(__arm__) || defined(__i386__) || defined(__x86_64__)
1057 DefaultCompare( UInt32 cellCount
, UInt32 left
[], UInt32 right
[] )
1060 return left
[cellCount
] - right
[cellCount
];
1063 #error Unknown architecture.
1067 AddLengthToCells( UInt32 numCells
, UInt32
*cells
, UInt64 offset
)
1069 if (numCells
== 1) {
1070 cells
[0] += (UInt32
)offset
;
1072 #if defined(__arm64__) || defined(__arm__)
1073 UInt64 sum
= cells
[numCells
- 2] + offset
;
1074 cells
[numCells
- 2] = (UInt32
)sum
;
1075 if (sum
> UINT32_MAX
) {
1076 cells
[numCells
- 1] += (UInt32
)(sum
>> 32);
1079 UInt64 sum
= cells
[numCells
- 1] + offset
;
1080 cells
[numCells
- 1] = (UInt32
)sum
;
1081 if (sum
> UINT32_MAX
) {
1082 cells
[numCells
- 2] += (UInt32
)(sum
>> 32);
1088 static IOPhysicalAddress
1089 CellsValue( UInt32 numCells
, UInt32
*cells
)
1091 if (numCells
== 1) {
1092 return IOPhysical32( 0, cells
[0] );
1094 #if defined(__arm64__) || defined(arm)
1095 return IOPhysical32( cells
[numCells
- 1], cells
[numCells
- 2] );
1097 return IOPhysical32( cells
[numCells
- 2], cells
[numCells
- 1] );
1103 IODTGetCellCounts( IORegistryEntry
* regEntry
,
1104 UInt32
* sizeCount
, UInt32
* addressCount
)
1106 if (!GetUInt32( regEntry
, gIODTSizeCellKey
, sizeCount
)) {
1109 if (!GetUInt32( regEntry
, gIODTAddressCellKey
, addressCount
)) {
1115 // Given addr & len cells from our child, find it in our ranges property, then
1116 // look in our parent to resolve the base of the range for us.
1118 // Range[]: child-addr our-addr child-len
1119 // #cells: child ours child
1122 IODTResolveAddressCell( IORegistryEntry
* startEntry
,
1124 IOPhysicalAddress
* phys
, IOPhysicalLength
* lenOut
)
1126 IORegistryEntry
* parent
;
1127 IORegistryEntry
* regEntry
;
1130 unsigned int index
, count
;
1131 // cells in addresses at regEntry
1132 UInt32 sizeCells
, addressCells
;
1133 // cells in addresses below regEntry
1134 UInt32 childSizeCells
, childAddressCells
;
1136 UInt32 cell
[8], propLen
;
1144 SInt64 diff
, diff2
, endDiff
;
1145 UInt64 len
, rangeLen
;
1147 IODTPersistent
*persist
;
1148 IODTCompareAddressCellFunc compare
;
1150 regEntry
= startEntry
;
1151 IODTGetCellCounts( regEntry
, &childSizeCells
, &childAddressCells
);
1152 childCells
= childAddressCells
+ childSizeCells
;
1154 if (childCells
> sizeof(cell
) / sizeof(cell
[0])) {
1155 panic("IODTResolveAddressCell: Invalid device tree (%u,%u)", (uint32_t)childAddressCells
, (uint32_t)childSizeCells
);
1158 bcopy( cellsIn
, cell
, sizeof(UInt32
) * childCells
);
1159 *lenOut
= CellsValue( childSizeCells
, cellsIn
+ childAddressCells
);
1162 prop
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTRangeKey
));
1164 /* end of the road */
1165 *phys
= CellsValue( childAddressCells
, cell
);
1167 if (regEntry
!= startEntry
) {
1168 regEntry
->release();
1173 parent
= regEntry
->copyParentEntry( gIODTPlane
);
1174 IODTGetCellCounts( parent
, &sizeCells
, &addressCells
);
1176 if ((propLen
= prop
->getLength())) {
1178 startRange
= (UInt32
*) prop
->getBytesNoCopy();
1180 endRanges
= range
+ (propLen
/ sizeof(UInt32
));
1183 num
= OSDynamicCast(OSNumber
, regEntry
->getProperty(gIODTPersistKey
));
1185 IOLockLock(gIODTResolversLock
);
1186 index
= num
->unsigned32BitValue();
1187 count
= gIODTResolvers
->getLength() / sizeof(IODTPersistent
);
1188 if (index
< count
) {
1189 persist
= ((IODTPersistent
*) gIODTResolvers
->getBytesNoCopy()) + index
;
1190 compare
= persist
->compareFunc
;
1192 IOLockUnlock(gIODTResolversLock
);
1195 if (!compare
&& (addressCells
== childAddressCells
)) {
1196 compare
= DefaultCompare
;
1199 panic("There is no mixed comparison function yet...");
1204 range
+= (childCells
+ addressCells
)) {
1205 // is cell start within range?
1206 diff
= (*compare
)( childAddressCells
, cell
, range
);
1208 if (childAddressCells
> sizeof(endCell
) / sizeof(endCell
[0])) {
1209 panic("IODTResolveAddressCell: Invalid device tree (%u)", (uint32_t)childAddressCells
);
1212 bcopy(range
, endCell
, childAddressCells
* sizeof(UInt32
));
1214 rangeLen
= CellsValue(childSizeCells
, range
+ childAddressCells
+ addressCells
);
1215 AddLengthToCells(childAddressCells
, endCell
, rangeLen
);
1217 diff2
= (*compare
)( childAddressCells
, cell
, endCell
);
1219 // if start of cell < start of range, or end of range >= start of cell, skip
1220 if ((diff
< 0) || (diff2
>= 0)) {
1224 len
= CellsValue(childSizeCells
, cell
+ childAddressCells
);
1228 // search for cell end
1229 bcopy(cell
, endCell
, childAddressCells
* sizeof(UInt32
));
1231 AddLengthToCells(childAddressCells
, endCell
, len
- 1);
1233 for (lookRange
= startRange
;
1234 lookRange
< endRanges
;
1235 lookRange
+= (childCells
+ addressCells
)) {
1236 // make sure end of cell >= range start
1237 endDiff
= (*compare
)( childAddressCells
, endCell
, lookRange
);
1242 UInt64 rangeStart
= CellsValue(addressCells
, range
+ childAddressCells
);
1243 UInt64 lookRangeStart
= CellsValue(addressCells
, lookRange
+ childAddressCells
);
1244 if ((endDiff
- len
+ 1 + lookRangeStart
) == (diff
+ rangeStart
)) {
1257 if (addressCells
+ sizeCells
> sizeof(cell
) / sizeof(cell
[0])) {
1258 panic("IODTResolveAddressCell: Invalid device tree (%u, %u)", (uint32_t)addressCells
, (uint32_t)sizeCells
);
1261 // Get the physical start of the range from our parent
1262 bcopy( range
+ childAddressCells
, cell
, sizeof(UInt32
) * addressCells
);
1263 bzero( cell
+ addressCells
, sizeof(UInt32
) * sizeCells
);
1264 } /* else zero length range => pass thru to parent */
1266 if (regEntry
!= startEntry
) {
1267 regEntry
->release();
1270 childSizeCells
= sizeCells
;
1271 childAddressCells
= addressCells
;
1272 childCells
= childAddressCells
+ childSizeCells
;
1273 }while (ok
&& regEntry
);
1280 IODTResolveAddressing( IORegistryEntry
* regEntry
,
1281 const char * addressPropertyName
,
1282 IODeviceMemory
* parent
)
1284 IORegistryEntry
*parentEntry
;
1285 OSData
*addressProperty
;
1286 UInt32 sizeCells
, addressCells
, cells
;
1289 IOPhysicalAddress phys
;
1290 IOPhysicalLength len
;
1292 IODeviceMemory
*range
;
1296 parentEntry
= regEntry
->copyParentEntry( gIODTPlane
);
1297 addressProperty
= (OSData
*) regEntry
->getProperty( addressPropertyName
);
1298 if ((0 == addressProperty
) || (0 == parentEntry
)) {
1302 IODTGetCellCounts( parentEntry
, &sizeCells
, &addressCells
);
1303 if (0 == sizeCells
) {
1307 cells
= sizeCells
+ addressCells
;
1308 reg
= (UInt32
*) addressProperty
->getBytesNoCopy();
1309 num
= addressProperty
->getLength() / (4 * cells
);
1311 array
= OSArray::withCapacity( 1 );
1316 for (i
= 0; i
< num
; i
++) {
1317 if (IODTResolveAddressCell( parentEntry
, reg
, &phys
, &len
)) {
1320 range
= IODeviceMemory::withSubRange( parent
,
1321 phys
- parent
->getPhysicalSegment(0, 0, kIOMemoryMapperNone
), len
);
1324 range
= IODeviceMemory::withRange( phys
, len
);
1327 array
->setObject( range
);
1333 regEntry
->setProperty( gIODeviceMemoryKey
, array
);
1334 array
->release(); /* ??? */
1337 OSSafeReleaseNULL(parentEntry
);
1343 IODTFindSlotName( IORegistryEntry
* regEntry
, UInt32 deviceNumber
)
1345 IORegistryEntry
*parent
;
1355 data
= (OSData
*) regEntry
->getProperty("AAPL,slot-name");
1361 parent
= regEntry
->copyParentEntry( gIODTPlane
);
1366 data
= OSDynamicCast( OSData
, parent
->getProperty("slot-names"));
1370 if (data
->getLength() <= 4) {
1374 bits
= (UInt32
*) data
->getBytesNoCopy();
1376 if ((0 == (mask
& (1 << deviceNumber
)))) {
1380 names
= (char *)(bits
+ 1);
1381 lastName
= names
+ (data
->getLength() - 4);
1383 for (i
= 0; (i
<= deviceNumber
) && (names
< lastName
); i
++) {
1384 if (mask
& (1 << i
)) {
1385 nlen
= 1 + strnlen(names
, lastName
- names
);
1386 if (i
== deviceNumber
) {
1387 data
= OSData::withBytesNoCopy(names
, nlen
);
1389 regEntry
->setProperty("AAPL,slot-name", data
);
1400 OSSafeReleaseNULL(parent
);
1406 IONDRVLibrariesInitialize( IOService
* provider
)
1408 return kIOReturnUnsupported
;