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( NULL
, "/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
)) {
224 #if !(defined(RC_HIDE_N144) || defined(RC_HIDE_N146))
225 if (!strcmp("sep", child
->getName())
226 || !strcmp("aop", child
->getName())
227 || !strcmp("disp0", child
->getName())) {
228 uint32_t aotFlags
= 1;
229 child
->setProperty("aot-power", &aotFlags
, sizeof(aotFlags
));
231 #endif /* !(defined(RC_HIDE_N144) || defined(RC_HIDE_N146)) */
237 parent
->setProperty("allInts", allInts
);
238 parent
->setProperty("sharedInts", gIODTSharedInterrupts
);
240 regIter
= IORegistryIterator::iterateOver( gIODTPlane
,
241 kIORegistryIterateRecursively
);
243 while ((child
= regIter
->getNextObject())) {
245 array
= OSDynamicCast(OSArray
, child
->getProperty( gIOInterruptSpecifiersKey
));
246 for (UInt32 i
= 0; array
&& (i
< array
->getCount()); i
++) {
247 IOOptionBits options
;
248 IOReturn ret
= IODTGetInterruptOptions( child
, i
, &options
);
249 if ((ret
!= kIOReturnSuccess
) || options
) {
250 IOLog("%s[%ld] %ld (%x)\n", child
->getName(), i
, options
, ret
);
261 // set a key in the root to indicate we found NW interrupt mapping
262 parent
->setProperty( gIODTNWInterruptMappingKey
,
263 (OSObject
*) gIODTNWInterruptMappingKey
);
270 IODTGetLoaderInfo( const char *key
, void **infoAddr
, int *infoSize
)
272 IORegistryEntry
*chosen
;
275 unsigned int propSize
;
278 chosen
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
);
279 if (chosen
== NULL
) {
283 propObj
= OSDynamicCast( OSData
, chosen
->getProperty(key
));
284 if (propObj
== NULL
) {
288 propSize
= propObj
->getLength();
289 if (propSize
!= (2 * sizeof(dtptr_t
))) {
293 propPtr
= (dtptr_t
*)propObj
->getBytesNoCopy();
294 if (propPtr
== NULL
) {
298 *infoAddr
= (void *)(uintptr_t) (propPtr
[0]);
299 *infoSize
= (int) (propPtr
[1]);
310 IODTFreeLoaderInfo( const char *key
, void *infoAddr
, int infoSize
)
312 vm_offset_t range
[2];
313 IORegistryEntry
*chosen
;
315 range
[0] = (vm_offset_t
)infoAddr
;
316 range
[1] = (vm_offset_t
)infoSize
;
317 FreePhysicalMemory( range
);
320 chosen
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
);
321 if (chosen
!= NULL
) {
322 chosen
->removeProperty(key
);
329 IODTGetDefault(const char *key
, void *infoAddr
, unsigned int infoSize
)
331 IORegistryEntry
*defaults
;
333 unsigned int defaultSize
;
335 defaults
= IORegistryEntry::fromPath( "/defaults", gIODTPlane
);
336 if (defaults
== NULL
) {
340 defaultObj
= OSDynamicCast( OSData
, defaults
->getProperty(key
));
341 if (defaultObj
== NULL
) {
345 defaultSize
= defaultObj
->getLength();
346 if (defaultSize
> infoSize
) {
350 memcpy( infoAddr
, defaultObj
->getBytesNoCopy(), defaultSize
);
356 FreePhysicalMemory( vm_offset_t
* range
)
360 virt
= ml_static_ptovirt( range
[0] );
362 ml_static_mfree( virt
, range
[1] );
366 static IORegistryEntry
*
367 MakeReferenceTable( DTEntry dtEntry
, bool copy
)
369 IORegistryEntry
*regEntry
;
370 OSDictionary
*propTable
;
371 const OSSymbol
*nameKey
;
374 OpaqueDTPropertyIterator dtIter
;
376 unsigned int propSize
;
379 bool noLocation
= true;
382 regEntry
= new IOService
;
384 if (regEntry
&& (false == regEntry
->init())) {
390 (kSuccess
== DTInitPropertyIterator( dtEntry
, &dtIter
))) {
391 kernelOnly
= (kSuccess
== DTGetProperty(dtEntry
, "kernel-only", &prop
, &propSize
));
392 propTable
= regEntry
->getPropertyTable();
394 while (kSuccess
== DTIterateProperties( &dtIter
, &name
)) {
395 if (kSuccess
!= DTGetProperty( dtEntry
, name
, &prop
, &propSize
)) {
400 nameKey
= OSSymbol::withCString(name
);
401 data
= OSData::withBytes(prop
, propSize
);
403 nameKey
= OSSymbol::withCStringNoCopy(name
);
404 data
= OSData::withBytesNoCopy(prop
, propSize
);
406 assert( nameKey
&& data
);
409 data
->setSerializable(false);
412 propTable
->setObject( nameKey
, data
);
416 if (nameKey
== gIODTNameKey
) {
418 sym
= OSSymbol::withCString((const char *) prop
);
420 sym
= OSSymbol::withCStringNoCopy((const char *) prop
);
422 regEntry
->setName( sym
);
424 } else if (nameKey
== gIODTUnitKey
) {
425 // all OF strings are null terminated... except this one
426 if (propSize
>= (int) sizeof(location
)) {
427 propSize
= sizeof(location
) - 1;
429 strncpy( location
, (const char *) prop
, propSize
);
430 location
[propSize
] = 0;
431 regEntry
->setLocation( location
);
432 propTable
->removeObject( gIODTUnitKey
);
434 } else if (noLocation
&& (!strncmp(name
, "reg", sizeof("reg")))) {
435 // default location - override later
436 snprintf(location
, sizeof(location
), "%X", *((uint32_t *) prop
));
437 regEntry
->setLocation( location
);
446 AddPHandle( IORegistryEntry
* regEntry
)
450 if (regEntry
->getProperty( gIODTInterruptCellKey
)
451 && (data
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTPHandleKey
)))) {
452 // a possible interrupt-parent
453 gIODTPHandles
->setObject( data
);
454 gIODTPHandleMap
->setObject( regEntry
);
458 static IORegistryEntry
*
459 FindPHandle( UInt32 phandle
)
462 IORegistryEntry
*regEntry
= NULL
;
465 for (i
= 0; (data
= (OSData
*)gIODTPHandles
->getObject( i
)); i
++) {
466 if (phandle
== *((UInt32
*)data
->getBytesNoCopy())) {
467 regEntry
= (IORegistryEntry
*)
468 gIODTPHandleMap
->getObject( i
);
477 GetUInt32( IORegistryEntry
* regEntry
, const OSSymbol
* name
,
484 if (!(obj
= regEntry
->copyProperty(name
))) {
488 result
= ((data
= OSDynamicCast(OSData
, obj
)) && (sizeof(UInt32
) == data
->getLength()));
490 *value
= *((UInt32
*) data
->getBytesNoCopy());
497 static IORegistryEntry
*
498 IODTFindInterruptParent( IORegistryEntry
* regEntry
, IOItemCount index
)
500 IORegistryEntry
* parent
;
505 if ((data
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTInterruptParentKey
)))
506 && (sizeof(UInt32
) <= (len
= data
->getLength()))) {
507 if (((index
+ 1) * sizeof(UInt32
)) > len
) {
510 phandle
= ((UInt32
*) data
->getBytesNoCopy())[index
];
511 parent
= FindPHandle( phandle
);
512 } else if (NULL
== regEntry
->getProperty( "interrupt-controller")) {
513 parent
= regEntry
->getParentEntry( gIODTPlane
);
522 IODTInterruptControllerName( IORegistryEntry
* regEntry
)
529 ok
= GetUInt32( regEntry
, gIODTPHandleKey
, &phandle
);
533 snprintf(buf
, sizeof(buf
), "IOInterruptController%08X", (uint32_t)phandle
);
534 sym
= OSSymbol::withCString( buf
);
542 #define unexpected(a) { kprintf("unexpected %s:%d\n", __FILE__, __LINE__); a; }
545 IODTGetICellCounts( IORegistryEntry
* regEntry
,
546 UInt32
* iCellCount
, UInt32
* aCellCount
)
548 if (!GetUInt32( regEntry
, gIODTInterruptCellKey
, iCellCount
)) {
549 unexpected( *iCellCount
= 1 );
551 if (!GetUInt32( regEntry
, gIODTAddressCellKey
, aCellCount
)) {
557 IODTMapOneInterrupt( IORegistryEntry
* regEntry
, UInt32
* intSpec
, UInt32 index
,
558 LIBKERN_RETURNS_RETAINED OSData
** spec
,
559 LIBKERN_RETURNS_RETAINED
const OSSymbol
** controller
)
561 IORegistryEntry
*parent
= NULL
;
567 UInt32 acells
, icells
, pacells
, picells
, cell
;
568 UInt32 i
, original_icells
;
569 bool cmp
, ok
= false;
571 parent
= IODTFindInterruptParent( regEntry
, index
);
572 IODTGetICellCounts( parent
, &icells
, &acells
);
575 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "reg" ));
576 if (data
&& (data
->getLength() >= (acells
* sizeof(UInt32
)))) {
577 addrCmp
= (UInt32
*) data
->getBytesNoCopy();
580 original_icells
= icells
;
585 kprintf("IODTMapOneInterrupt: current regEntry name %s\n", regEntry
->getName());
586 kprintf("acells - icells: ");
587 for (i
= 0; i
< acells
; i
++) {
588 kprintf("0x%08X ", addrCmp
[i
]);
591 for (i
= 0; i
< icells
; i
++) {
592 kprintf("0x%08X ", intSpec
[i
]);
597 if (parent
&& (data
= OSDynamicCast( OSData
,
598 regEntry
->getProperty( "interrupt-controller")))) {
599 // found a controller - don't want to follow cascaded controllers
601 *spec
= OSData::withBytesNoCopy((void *) intSpec
,
602 icells
* sizeof(UInt32
));
603 *controller
= IODTInterruptControllerName( regEntry
);
604 ok
= (*spec
&& *controller
);
605 } else if (parent
&& (data
= OSDynamicCast( OSData
,
606 regEntry
->getProperty( "interrupt-map")))) {
608 map
= (UInt32
*) data
->getBytesNoCopy();
609 endMap
= map
+ (data
->getLength() / sizeof(UInt32
));
610 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "interrupt-map-mask" ));
611 if (data
&& (data
->getLength() >= ((acells
+ icells
) * sizeof(UInt32
)))) {
612 maskCmp
= (UInt32
*) data
->getBytesNoCopy();
619 kprintf(" maskCmp: ");
620 for (i
= 0; i
< acells
+ icells
; i
++) {
624 kprintf("0x%08X ", maskCmp
[i
]);
627 kprintf(" masked: ");
628 for (i
= 0; i
< acells
+ icells
; i
++) {
632 kprintf("0x%08X ", ((i
< acells
) ? addrCmp
[i
] : intSpec
[i
- acells
]) & maskCmp
[i
]);
636 kprintf("no maskCmp\n");
642 for (i
= 0; i
< acells
+ icells
; i
++) {
646 kprintf("0x%08X ", map
[i
]);
650 for (i
= 0, cmp
= true; cmp
&& (i
< (acells
+ icells
)); i
++) {
651 cell
= (i
< acells
) ? addrCmp
[i
] : intSpec
[i
- acells
];
655 cmp
= (cell
== map
[i
]);
658 map
+= acells
+ icells
;
659 if (NULL
== (parent
= FindPHandle( *(map
++)))) {
663 IODTGetICellCounts( parent
, &picells
, &pacells
);
666 intSpec
= map
+ pacells
;
669 map
+= pacells
+ picells
;
671 } while (!cmp
&& (map
< endMap
));
678 IODTGetICellCounts( parent
, &icells
, &acells
);
683 return ok
? original_icells
: 0;
687 IODTGetInterruptOptions( IORegistryEntry
* regEntry
, int source
, IOOptionBits
* options
)
689 OSArray
* controllers
;
690 OSArray
* specifiers
;
697 controllers
= OSDynamicCast(OSArray
, regEntry
->getProperty(gIOInterruptControllersKey
));
698 specifiers
= OSDynamicCast(OSArray
, regEntry
->getProperty(gIOInterruptSpecifiersKey
));
700 if (!controllers
|| !specifiers
) {
701 return kIOReturnNoInterrupt
;
704 shared
= (OSArray
*) gIODTSharedInterrupts
->getObject(
705 (const OSSymbol
*) controllers
->getObject(source
));
707 return kIOReturnSuccess
;
710 spec
= specifiers
->getObject(source
);
712 return kIOReturnNoInterrupt
;
715 for (unsigned int i
= 0;
716 (oneSpec
= shared
->getObject(i
))
717 && (!oneSpec
->isEqualTo(spec
));
722 *options
= kIODTInterruptShared
;
725 return kIOReturnSuccess
;
729 IODTMapInterruptsSharing( IORegistryEntry
* regEntry
, OSDictionary
* allInts
)
731 IORegistryEntry
* parent
;
740 OSArray
* controllerInts
;
741 const OSSymbol
* controller
= NULL
;
742 OSArray
* controllers
;
746 nw
= (NULL
== (local
= OSDynamicCast( OSData
,
747 regEntry
->getProperty( gIODTAAPLInterruptsKey
))));
748 if (nw
&& (NULL
== (local
= OSDynamicCast( OSData
,
749 regEntry
->getProperty( "interrupts"))))) {
750 return true; // nothing to see here
752 if (nw
&& (parent
= regEntry
->getParentEntry( gIODTPlane
))) {
753 // check for bridges on old world
754 if ((local2
= OSDynamicCast( OSData
,
755 parent
->getProperty( gIODTAAPLInterruptsKey
)))) {
761 localBits
= (UInt32
*) local
->getBytesNoCopy();
762 localEnd
= localBits
+ (local
->getLength() / sizeof(UInt32
));
764 mapped
= OSArray::withCapacity( 1 );
765 controllers
= OSArray::withCapacity( 1 );
767 ok
= (mapped
&& controllers
);
772 skip
= IODTMapOneInterrupt( regEntry
, localBits
, index
, &map
, &controller
);
774 IOLog("%s: error mapping interrupt[%d]\n",
775 regEntry
->getName(), mapped
->getCount());
779 map
= OSData::withData( local
, mapped
->getCount() * sizeof(UInt32
),
781 controller
= gIODTDefaultInterruptController
;
782 controller
->retain();
787 mapped
->setObject( map
);
788 controllers
->setObject( controller
);
791 controllerInts
= (OSArray
*) allInts
->getObject( controller
);
792 if (controllerInts
) {
793 for (unsigned int i
= 0; (oneMap
= controllerInts
->getObject(i
)); i
++) {
794 if (map
->isEqualTo(oneMap
)) {
795 controllerInts
= (OSArray
*) gIODTSharedInterrupts
->getObject( controller
);
796 if (controllerInts
) {
797 controllerInts
->setObject(map
);
799 controllerInts
= OSArray::withObjects((const OSObject
**) &map
, 1, 4 );
800 if (controllerInts
) {
801 gIODTSharedInterrupts
->setObject( controller
, controllerInts
);
802 controllerInts
->release();
809 controllerInts
->setObject(map
);
812 controllerInts
= OSArray::withObjects((const OSObject
**) &map
, 1, 16 );
813 if (controllerInts
) {
814 allInts
->setObject( controller
, controllerInts
);
815 controllerInts
->release();
821 controller
->release();
822 } while (localBits
< localEnd
);
825 ok
&= (localBits
== localEnd
);
829 ok
= regEntry
->setProperty( gIOInterruptControllersKey
, controllers
);
830 ok
&= regEntry
->setProperty( gIOInterruptSpecifiersKey
, mapped
);
834 controllers
->release();
844 IODTMapInterrupts( IORegistryEntry
* regEntry
)
846 return IODTMapInterruptsSharing( regEntry
, NULL
);
853 CompareKey( OSString
* key
,
854 const IORegistryEntry
* table
, const OSSymbol
* propName
,
855 LIBKERN_RETURNS_RETAINED OSString
** matchingName
)
864 const char *lastName
;
867 const char *result
= NULL
;
869 if (NULL
== (prop
= table
->copyProperty( propName
))) {
873 if ((data
= OSDynamicCast( OSData
, prop
))) {
874 names
= (const char *) data
->getBytesNoCopy();
875 lastName
= names
+ data
->getLength();
876 } else if ((string
= OSDynamicCast( OSString
, prop
))) {
877 names
= string
->getCStringNoCopy();
878 lastName
= names
+ string
->getLength() + 1;
884 ckey
= key
->getCStringNoCopy();
885 keyLen
= key
->getLength();
886 wild
= ('*' == key
->getChar( keyLen
- 1 ));
889 // for each name in the property
890 nlen
= strnlen(names
, lastName
- names
);
892 matched
= ((nlen
>= (keyLen
- 1)) && (0 == strncmp(ckey
, names
, keyLen
- 1)));
894 matched
= (keyLen
== nlen
) && (0 == strncmp(ckey
, names
, keyLen
));
901 names
= names
+ nlen
+ 1;
902 } while ((names
< lastName
) && (false == matched
));
905 if (result
&& matchingName
) {
906 *matchingName
= OSString::withCString( result
);
913 return result
!= NULL
;
918 IODTCompareNubName( const IORegistryEntry
* regEntry
,
919 OSString
* name
, OSString
** matchingName
)
923 matched
= CompareKey( name
, regEntry
, gIODTNameKey
, matchingName
)
924 || CompareKey( name
, regEntry
, gIODTCompatibleKey
, matchingName
)
925 || CompareKey( name
, regEntry
, gIODTTypeKey
, matchingName
)
926 || CompareKey( name
, regEntry
, gIODTModelKey
, matchingName
);
932 IODTMatchNubWithKeys( IORegistryEntry
* regEntry
,
938 obj
= OSUnserialize( keys
, NULL
);
941 result
= regEntry
->compareNames( obj
);
946 IOLog("Couldn't unserialize %s\n", keys
);
953 OSCollectionIterator
*
954 IODTFindMatchingEntries( IORegistryEntry
* from
,
955 IOOptionBits options
, const char * keys
)
957 OSSet
*result
= NULL
;
958 IORegistryEntry
*next
;
959 IORegistryIterator
*iter
;
960 OSCollectionIterator
*cIter
;
962 bool minus
= options
& kIODTExclusive
;
965 iter
= IORegistryIterator::iterateOver( from
, gIODTPlane
,
966 (options
& kIODTRecursive
) ? kIORegistryIterateRecursively
: 0 );
972 result
= OSSet::withCapacity( 3 );
978 while ((next
= iter
->getNextObject())) {
979 // Look for existence of a debug property to skip
980 if (next
->getProperty("AAPL,ignore")) {
985 cmp
= IODTMatchNubWithKeys( next
, keys
);
986 if ((minus
&& (false == cmp
))
987 || ((false == minus
) && (false != cmp
))) {
988 result
->setObject( next
);
991 result
->setObject( next
);
994 } while (!iter
->isValid());
999 cIter
= OSCollectionIterator::withCollection( result
);
1008 struct IODTPersistent
{
1009 IODTCompareAddressCellFunc compareFunc
;
1013 IODTSetResolving( IORegistryEntry
* regEntry
,
1014 IODTCompareAddressCellFunc compareFunc
,
1015 IODTNVLocationFunc locationFunc __unused
)
1017 IODTPersistent persist
;
1018 IODTPersistent
* entry
;
1020 unsigned int index
, count
;
1022 IOLockLock(gIODTResolversLock
);
1024 count
= (gIODTResolvers
->getLength() / sizeof(IODTPersistent
));
1025 entry
= (typeof(entry
))gIODTResolvers
->getBytesNoCopy();
1026 for (index
= 0; index
< count
; index
++) {
1027 if (compareFunc
== entry
->compareFunc
) {
1032 if (index
== count
) {
1033 persist
.compareFunc
= compareFunc
;
1034 if (!gIODTResolvers
->appendBytes(&persist
, sizeof(IODTPersistent
))) {
1035 panic("IODTSetResolving");
1039 IOLockUnlock(gIODTResolversLock
);
1041 num
= OSNumber::withNumber(index
, 32);
1042 regEntry
->setProperty(gIODTPersistKey
, num
);
1043 OSSafeReleaseNULL(num
);
1048 #if defined(__arm64__)
1050 DefaultCompare( UInt32 cellCount
, UInt32 left
[], UInt32 right
[] )
1054 if (cellCount
== 2) {
1055 diff
= IOPhysical32(left
[1], left
[0]) - IOPhysical32(right
[1], right
[0]);
1056 } else if (cellCount
== 1) {
1057 diff
= (left
[0] - right
[0]);
1059 panic("DefaultCompare only knows how to handle 1 or 2 cells.");
1064 #elif defined(__arm__) || defined(__i386__) || defined(__x86_64__)
1066 DefaultCompare( UInt32 cellCount
, UInt32 left
[], UInt32 right
[] )
1069 return left
[cellCount
] - right
[cellCount
];
1072 #error Unknown architecture.
1076 AddLengthToCells( UInt32 numCells
, UInt32
*cells
, UInt64 offset
)
1078 if (numCells
== 1) {
1079 cells
[0] += (UInt32
)offset
;
1081 #if defined(__arm64__) || defined(__arm__)
1082 UInt64 sum
= cells
[numCells
- 2] + offset
;
1083 cells
[numCells
- 2] = (UInt32
)sum
;
1084 if (sum
> UINT32_MAX
) {
1085 cells
[numCells
- 1] += (UInt32
)(sum
>> 32);
1088 UInt64 sum
= cells
[numCells
- 1] + offset
;
1089 cells
[numCells
- 1] = (UInt32
)sum
;
1090 if (sum
> UINT32_MAX
) {
1091 cells
[numCells
- 2] += (UInt32
)(sum
>> 32);
1097 static IOPhysicalAddress
1098 CellsValue( UInt32 numCells
, UInt32
*cells
)
1100 if (numCells
== 1) {
1101 return IOPhysical32( 0, cells
[0] );
1103 #if defined(__arm64__) || defined(arm)
1104 return IOPhysical32( cells
[numCells
- 1], cells
[numCells
- 2] );
1106 return IOPhysical32( cells
[numCells
- 2], cells
[numCells
- 1] );
1112 IODTGetCellCounts( IORegistryEntry
* regEntry
,
1113 UInt32
* sizeCount
, UInt32
* addressCount
)
1115 if (!GetUInt32( regEntry
, gIODTSizeCellKey
, sizeCount
)) {
1118 if (!GetUInt32( regEntry
, gIODTAddressCellKey
, addressCount
)) {
1124 // Given addr & len cells from our child, find it in our ranges property, then
1125 // look in our parent to resolve the base of the range for us.
1127 // Range[]: child-addr our-addr child-len
1128 // #cells: child ours child
1131 IODTResolveAddressCell( IORegistryEntry
* startEntry
,
1133 IOPhysicalAddress
* phys
, IOPhysicalLength
* lenOut
)
1135 IORegistryEntry
* parent
;
1136 IORegistryEntry
* regEntry
;
1139 unsigned int index
, count
;
1140 // cells in addresses at regEntry
1141 UInt32 sizeCells
, addressCells
;
1142 // cells in addresses below regEntry
1143 UInt32 childSizeCells
, childAddressCells
;
1145 UInt32 cell
[8], propLen
;
1153 SInt64 diff
, diff2
, endDiff
;
1154 UInt64 len
, rangeLen
;
1156 IODTPersistent
*persist
;
1157 IODTCompareAddressCellFunc compare
;
1159 regEntry
= startEntry
;
1160 IODTGetCellCounts( regEntry
, &childSizeCells
, &childAddressCells
);
1161 childCells
= childAddressCells
+ childSizeCells
;
1163 if (childCells
> sizeof(cell
) / sizeof(cell
[0])) {
1164 panic("IODTResolveAddressCell: Invalid device tree (%u,%u)", (uint32_t)childAddressCells
, (uint32_t)childSizeCells
);
1167 bcopy( cellsIn
, cell
, sizeof(UInt32
) * childCells
);
1168 *lenOut
= CellsValue( childSizeCells
, cellsIn
+ childAddressCells
);
1171 prop
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTRangeKey
));
1173 /* end of the road */
1174 *phys
= CellsValue( childAddressCells
, cell
);
1176 if (regEntry
!= startEntry
) {
1177 regEntry
->release();
1182 parent
= regEntry
->copyParentEntry( gIODTPlane
);
1183 IODTGetCellCounts( parent
, &sizeCells
, &addressCells
);
1185 if ((propLen
= prop
->getLength())) {
1187 startRange
= (UInt32
*) prop
->getBytesNoCopy();
1189 endRanges
= range
+ (propLen
/ sizeof(UInt32
));
1192 num
= OSDynamicCast(OSNumber
, regEntry
->getProperty(gIODTPersistKey
));
1194 IOLockLock(gIODTResolversLock
);
1195 index
= num
->unsigned32BitValue();
1196 count
= gIODTResolvers
->getLength() / sizeof(IODTPersistent
);
1197 if (index
< count
) {
1198 persist
= ((IODTPersistent
*) gIODTResolvers
->getBytesNoCopy()) + index
;
1199 compare
= persist
->compareFunc
;
1201 IOLockUnlock(gIODTResolversLock
);
1204 if (!compare
&& (addressCells
== childAddressCells
)) {
1205 compare
= DefaultCompare
;
1208 panic("There is no mixed comparison function yet...");
1213 range
+= (childCells
+ addressCells
)) {
1214 // is cell start within range?
1215 diff
= (*compare
)( childAddressCells
, cell
, range
);
1217 if (childAddressCells
> sizeof(endCell
) / sizeof(endCell
[0])) {
1218 panic("IODTResolveAddressCell: Invalid device tree (%u)", (uint32_t)childAddressCells
);
1221 bcopy(range
, endCell
, childAddressCells
* sizeof(UInt32
));
1223 rangeLen
= CellsValue(childSizeCells
, range
+ childAddressCells
+ addressCells
);
1224 AddLengthToCells(childAddressCells
, endCell
, rangeLen
);
1226 diff2
= (*compare
)( childAddressCells
, cell
, endCell
);
1228 // if start of cell < start of range, or end of range >= start of cell, skip
1229 if ((diff
< 0) || (diff2
>= 0)) {
1233 len
= CellsValue(childSizeCells
, cell
+ childAddressCells
);
1237 // search for cell end
1238 bcopy(cell
, endCell
, childAddressCells
* sizeof(UInt32
));
1240 AddLengthToCells(childAddressCells
, endCell
, len
- 1);
1242 for (lookRange
= startRange
;
1243 lookRange
< endRanges
;
1244 lookRange
+= (childCells
+ addressCells
)) {
1245 // make sure end of cell >= range start
1246 endDiff
= (*compare
)( childAddressCells
, endCell
, lookRange
);
1251 UInt64 rangeStart
= CellsValue(addressCells
, range
+ childAddressCells
);
1252 UInt64 lookRangeStart
= CellsValue(addressCells
, lookRange
+ childAddressCells
);
1253 if ((endDiff
- len
+ 1 + lookRangeStart
) == (diff
+ rangeStart
)) {
1266 if (addressCells
+ sizeCells
> sizeof(cell
) / sizeof(cell
[0])) {
1267 panic("IODTResolveAddressCell: Invalid device tree (%u, %u)", (uint32_t)addressCells
, (uint32_t)sizeCells
);
1270 // Get the physical start of the range from our parent
1271 bcopy( range
+ childAddressCells
, cell
, sizeof(UInt32
) * addressCells
);
1272 bzero( cell
+ addressCells
, sizeof(UInt32
) * sizeCells
);
1273 } /* else zero length range => pass thru to parent */
1275 if (regEntry
!= startEntry
) {
1276 regEntry
->release();
1279 childSizeCells
= sizeCells
;
1280 childAddressCells
= addressCells
;
1281 childCells
= childAddressCells
+ childSizeCells
;
1282 }while (ok
&& regEntry
);
1289 IODTResolveAddressing( IORegistryEntry
* regEntry
,
1290 const char * addressPropertyName
,
1291 IODeviceMemory
* parent
)
1293 IORegistryEntry
*parentEntry
;
1294 OSData
*addressProperty
;
1295 UInt32 sizeCells
, addressCells
, cells
;
1298 IOPhysicalAddress phys
;
1299 IOPhysicalLength len
;
1301 IODeviceMemory
*range
;
1305 parentEntry
= regEntry
->copyParentEntry( gIODTPlane
);
1306 addressProperty
= (OSData
*) regEntry
->getProperty( addressPropertyName
);
1307 if ((NULL
== addressProperty
) || (NULL
== parentEntry
)) {
1311 IODTGetCellCounts( parentEntry
, &sizeCells
, &addressCells
);
1312 if (0 == sizeCells
) {
1316 cells
= sizeCells
+ addressCells
;
1317 reg
= (UInt32
*) addressProperty
->getBytesNoCopy();
1318 num
= addressProperty
->getLength() / (4 * cells
);
1320 array
= OSArray::withCapacity( 1 );
1321 if (NULL
== array
) {
1325 for (i
= 0; i
< num
; i
++) {
1326 if (IODTResolveAddressCell( parentEntry
, reg
, &phys
, &len
)) {
1329 range
= IODeviceMemory::withSubRange( parent
,
1330 phys
- parent
->getPhysicalSegment(0, NULL
, kIOMemoryMapperNone
), len
);
1332 if (NULL
== range
) {
1333 range
= IODeviceMemory::withRange( phys
, len
);
1336 array
->setObject( range
);
1342 regEntry
->setProperty( gIODeviceMemoryKey
, array
);
1343 array
->release(); /* ??? */
1346 OSSafeReleaseNULL(parentEntry
);
1352 IODTFindSlotName( IORegistryEntry
* regEntry
, UInt32 deviceNumber
)
1354 IORegistryEntry
*parent
;
1364 data
= (OSData
*) regEntry
->getProperty("AAPL,slot-name");
1370 parent
= regEntry
->copyParentEntry( gIODTPlane
);
1375 data
= OSDynamicCast( OSData
, parent
->getProperty("slot-names"));
1379 if (data
->getLength() <= 4) {
1383 bits
= (UInt32
*) data
->getBytesNoCopy();
1385 if ((0 == (mask
& (1 << deviceNumber
)))) {
1389 names
= (char *)(bits
+ 1);
1390 lastName
= names
+ (data
->getLength() - 4);
1392 for (i
= 0; (i
<= deviceNumber
) && (names
< lastName
); i
++) {
1393 if (mask
& (1 << i
)) {
1394 nlen
= 1 + strnlen(names
, lastName
- names
);
1395 if (i
== deviceNumber
) {
1396 data
= OSData::withBytesNoCopy(names
, nlen
);
1398 regEntry
->setProperty("AAPL,slot-name", data
);
1409 OSSafeReleaseNULL(parent
);
1415 IONDRVLibrariesInitialize( IOService
* provider
)
1417 return kIOReturnUnsupported
;