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 (!strcmp("sep", child
->getName())
225 || !strcmp("aop", child
->getName())
226 || !strcmp("disp0", child
->getName())) {
227 uint32_t aotFlags
= 1;
228 child
->setProperty("aot-power", &aotFlags
, sizeof(aotFlags
));
235 parent
->setProperty("allInts", allInts
);
236 parent
->setProperty("sharedInts", gIODTSharedInterrupts
);
238 regIter
= IORegistryIterator::iterateOver( gIODTPlane
,
239 kIORegistryIterateRecursively
);
241 while ((child
= regIter
->getNextObject())) {
243 array
= OSDynamicCast(OSArray
, child
->getProperty( gIOInterruptSpecifiersKey
));
244 for (UInt32 i
= 0; array
&& (i
< array
->getCount()); i
++) {
245 IOOptionBits options
;
246 IOReturn ret
= IODTGetInterruptOptions( child
, i
, &options
);
247 if ((ret
!= kIOReturnSuccess
) || options
) {
248 IOLog("%s[%ld] %ld (%x)\n", child
->getName(), i
, options
, ret
);
259 // set a key in the root to indicate we found NW interrupt mapping
260 parent
->setProperty( gIODTNWInterruptMappingKey
,
261 (OSObject
*) gIODTNWInterruptMappingKey
);
268 IODTGetLoaderInfo( const char *key
, void **infoAddr
, int *infoSize
)
270 IORegistryEntry
*chosen
;
273 unsigned int propSize
;
276 chosen
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
);
277 if (chosen
== NULL
) {
281 propObj
= OSDynamicCast( OSData
, chosen
->getProperty(key
));
282 if (propObj
== NULL
) {
286 propSize
= propObj
->getLength();
287 if (propSize
!= (2 * sizeof(dtptr_t
))) {
291 propPtr
= (dtptr_t
*)propObj
->getBytesNoCopy();
292 if (propPtr
== NULL
) {
296 *infoAddr
= (void *)(uintptr_t) (propPtr
[0]);
297 *infoSize
= (int) (propPtr
[1]);
308 IODTFreeLoaderInfo( const char *key
, void *infoAddr
, int infoSize
)
310 vm_offset_t range
[2];
311 IORegistryEntry
*chosen
;
313 range
[0] = (vm_offset_t
)infoAddr
;
314 range
[1] = (vm_offset_t
)infoSize
;
315 FreePhysicalMemory( range
);
318 chosen
= IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane
);
319 if (chosen
!= NULL
) {
320 chosen
->removeProperty(key
);
327 IODTGetDefault(const char *key
, void *infoAddr
, unsigned int infoSize
)
329 IORegistryEntry
*defaults
;
331 unsigned int defaultSize
;
333 defaults
= IORegistryEntry::fromPath( "/defaults", gIODTPlane
);
334 if (defaults
== NULL
) {
338 defaultObj
= OSDynamicCast( OSData
, defaults
->getProperty(key
));
339 if (defaultObj
== NULL
) {
343 defaultSize
= defaultObj
->getLength();
344 if (defaultSize
> infoSize
) {
348 memcpy( infoAddr
, defaultObj
->getBytesNoCopy(), defaultSize
);
354 FreePhysicalMemory( vm_offset_t
* range
)
358 virt
= ml_static_ptovirt( range
[0] );
360 ml_static_mfree( virt
, range
[1] );
364 static IORegistryEntry
*
365 MakeReferenceTable( DTEntry dtEntry
, bool copy
)
367 IORegistryEntry
*regEntry
;
368 OSDictionary
*propTable
;
369 const OSSymbol
*nameKey
;
372 OpaqueDTPropertyIterator dtIter
;
374 unsigned int propSize
;
377 bool noLocation
= true;
380 regEntry
= new IOService
;
382 if (regEntry
&& (false == regEntry
->init())) {
388 (kSuccess
== DTInitPropertyIterator( dtEntry
, &dtIter
))) {
389 kernelOnly
= (kSuccess
== DTGetProperty(dtEntry
, "kernel-only", &prop
, &propSize
));
390 propTable
= regEntry
->getPropertyTable();
392 while (kSuccess
== DTIterateProperties( &dtIter
, &name
)) {
393 if (kSuccess
!= DTGetProperty( dtEntry
, name
, &prop
, &propSize
)) {
398 nameKey
= OSSymbol::withCString(name
);
399 data
= OSData::withBytes(prop
, propSize
);
401 nameKey
= OSSymbol::withCStringNoCopy(name
);
402 data
= OSData::withBytesNoCopy(prop
, propSize
);
404 assert( nameKey
&& data
);
407 data
->setSerializable(false);
410 propTable
->setObject( nameKey
, data
);
414 if (nameKey
== gIODTNameKey
) {
416 sym
= OSSymbol::withCString((const char *) prop
);
418 sym
= OSSymbol::withCStringNoCopy((const char *) prop
);
420 regEntry
->setName( sym
);
422 } else if (nameKey
== gIODTUnitKey
) {
423 // all OF strings are null terminated... except this one
424 if (propSize
>= (int) sizeof(location
)) {
425 propSize
= sizeof(location
) - 1;
427 strncpy( location
, (const char *) prop
, propSize
);
428 location
[propSize
] = 0;
429 regEntry
->setLocation( location
);
430 propTable
->removeObject( gIODTUnitKey
);
432 } else if (noLocation
&& (!strncmp(name
, "reg", sizeof("reg")))) {
433 // default location - override later
434 snprintf(location
, sizeof(location
), "%X", *((uint32_t *) prop
));
435 regEntry
->setLocation( location
);
444 AddPHandle( IORegistryEntry
* regEntry
)
448 if (regEntry
->getProperty( gIODTInterruptCellKey
)
449 && (data
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTPHandleKey
)))) {
450 // a possible interrupt-parent
451 gIODTPHandles
->setObject( data
);
452 gIODTPHandleMap
->setObject( regEntry
);
456 static IORegistryEntry
*
457 FindPHandle( UInt32 phandle
)
460 IORegistryEntry
*regEntry
= NULL
;
463 for (i
= 0; (data
= (OSData
*)gIODTPHandles
->getObject( i
)); i
++) {
464 if (phandle
== *((UInt32
*)data
->getBytesNoCopy())) {
465 regEntry
= (IORegistryEntry
*)
466 gIODTPHandleMap
->getObject( i
);
475 GetUInt32( IORegistryEntry
* regEntry
, const OSSymbol
* name
,
482 if (!(obj
= regEntry
->copyProperty(name
))) {
486 result
= ((data
= OSDynamicCast(OSData
, obj
)) && (sizeof(UInt32
) == data
->getLength()));
488 *value
= *((UInt32
*) data
->getBytesNoCopy());
495 static IORegistryEntry
*
496 IODTFindInterruptParent( IORegistryEntry
* regEntry
, IOItemCount index
)
498 IORegistryEntry
* parent
;
503 if ((data
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTInterruptParentKey
)))
504 && (sizeof(UInt32
) <= (len
= data
->getLength()))) {
505 if (((index
+ 1) * sizeof(UInt32
)) > len
) {
508 phandle
= ((UInt32
*) data
->getBytesNoCopy())[index
];
509 parent
= FindPHandle( phandle
);
510 } else if (NULL
== regEntry
->getProperty( "interrupt-controller")) {
511 parent
= regEntry
->getParentEntry( gIODTPlane
);
520 IODTInterruptControllerName( IORegistryEntry
* regEntry
)
527 ok
= GetUInt32( regEntry
, gIODTPHandleKey
, &phandle
);
531 snprintf(buf
, sizeof(buf
), "IOInterruptController%08X", (uint32_t)phandle
);
532 sym
= OSSymbol::withCString( buf
);
540 #define unexpected(a) { kprintf("unexpected %s:%d\n", __FILE__, __LINE__); a; }
543 IODTGetICellCounts( IORegistryEntry
* regEntry
,
544 UInt32
* iCellCount
, UInt32
* aCellCount
)
546 if (!GetUInt32( regEntry
, gIODTInterruptCellKey
, iCellCount
)) {
547 unexpected( *iCellCount
= 1 );
549 if (!GetUInt32( regEntry
, gIODTAddressCellKey
, aCellCount
)) {
555 IODTMapOneInterrupt( IORegistryEntry
* regEntry
, UInt32
* intSpec
, UInt32 index
,
556 LIBKERN_RETURNS_RETAINED OSData
** spec
,
557 LIBKERN_RETURNS_RETAINED
const OSSymbol
** controller
)
559 IORegistryEntry
*parent
= NULL
;
565 UInt32 acells
, icells
, pacells
, picells
, cell
;
566 UInt32 i
, original_icells
;
567 bool cmp
, ok
= false;
569 parent
= IODTFindInterruptParent( regEntry
, index
);
570 IODTGetICellCounts( parent
, &icells
, &acells
);
573 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "reg" ));
574 if (data
&& (data
->getLength() >= (acells
* sizeof(UInt32
)))) {
575 addrCmp
= (UInt32
*) data
->getBytesNoCopy();
578 original_icells
= icells
;
583 kprintf("IODTMapOneInterrupt: current regEntry name %s\n", regEntry
->getName());
584 kprintf("acells - icells: ");
585 for (i
= 0; i
< acells
; i
++) {
586 kprintf("0x%08X ", addrCmp
[i
]);
589 for (i
= 0; i
< icells
; i
++) {
590 kprintf("0x%08X ", intSpec
[i
]);
595 if (parent
&& (data
= OSDynamicCast( OSData
,
596 regEntry
->getProperty( "interrupt-controller")))) {
597 // found a controller - don't want to follow cascaded controllers
599 *spec
= OSData::withBytesNoCopy((void *) intSpec
,
600 icells
* sizeof(UInt32
));
601 *controller
= IODTInterruptControllerName( regEntry
);
602 ok
= (*spec
&& *controller
);
603 } else if (parent
&& (data
= OSDynamicCast( OSData
,
604 regEntry
->getProperty( "interrupt-map")))) {
606 map
= (UInt32
*) data
->getBytesNoCopy();
607 endMap
= map
+ (data
->getLength() / sizeof(UInt32
));
608 data
= OSDynamicCast( OSData
, regEntry
->getProperty( "interrupt-map-mask" ));
609 if (data
&& (data
->getLength() >= ((acells
+ icells
) * sizeof(UInt32
)))) {
610 maskCmp
= (UInt32
*) data
->getBytesNoCopy();
617 kprintf(" maskCmp: ");
618 for (i
= 0; i
< acells
+ icells
; i
++) {
622 kprintf("0x%08X ", maskCmp
[i
]);
625 kprintf(" masked: ");
626 for (i
= 0; i
< acells
+ icells
; i
++) {
630 kprintf("0x%08X ", ((i
< acells
) ? addrCmp
[i
] : intSpec
[i
- acells
]) & maskCmp
[i
]);
634 kprintf("no maskCmp\n");
640 for (i
= 0; i
< acells
+ icells
; i
++) {
644 kprintf("0x%08X ", map
[i
]);
648 for (i
= 0, cmp
= true; cmp
&& (i
< (acells
+ icells
)); i
++) {
649 cell
= (i
< acells
) ? addrCmp
[i
] : intSpec
[i
- acells
];
653 cmp
= (cell
== map
[i
]);
656 map
+= acells
+ icells
;
657 if (NULL
== (parent
= FindPHandle( *(map
++)))) {
661 IODTGetICellCounts( parent
, &picells
, &pacells
);
664 intSpec
= map
+ pacells
;
667 map
+= pacells
+ picells
;
669 } while (!cmp
&& (map
< endMap
));
676 IODTGetICellCounts( parent
, &icells
, &acells
);
681 return ok
? original_icells
: 0;
685 IODTGetInterruptOptions( IORegistryEntry
* regEntry
, int source
, IOOptionBits
* options
)
687 OSArray
* controllers
;
688 OSArray
* specifiers
;
695 controllers
= OSDynamicCast(OSArray
, regEntry
->getProperty(gIOInterruptControllersKey
));
696 specifiers
= OSDynamicCast(OSArray
, regEntry
->getProperty(gIOInterruptSpecifiersKey
));
698 if (!controllers
|| !specifiers
) {
699 return kIOReturnNoInterrupt
;
702 shared
= (OSArray
*) gIODTSharedInterrupts
->getObject(
703 (const OSSymbol
*) controllers
->getObject(source
));
705 return kIOReturnSuccess
;
708 spec
= specifiers
->getObject(source
);
710 return kIOReturnNoInterrupt
;
713 for (unsigned int i
= 0;
714 (oneSpec
= shared
->getObject(i
))
715 && (!oneSpec
->isEqualTo(spec
));
720 *options
= kIODTInterruptShared
;
723 return kIOReturnSuccess
;
727 IODTMapInterruptsSharing( IORegistryEntry
* regEntry
, OSDictionary
* allInts
)
729 IORegistryEntry
* parent
;
738 OSArray
* controllerInts
;
739 const OSSymbol
* controller
= NULL
;
740 OSArray
* controllers
;
744 nw
= (NULL
== (local
= OSDynamicCast( OSData
,
745 regEntry
->getProperty( gIODTAAPLInterruptsKey
))));
746 if (nw
&& (NULL
== (local
= OSDynamicCast( OSData
,
747 regEntry
->getProperty( "interrupts"))))) {
748 return true; // nothing to see here
750 if (nw
&& (parent
= regEntry
->getParentEntry( gIODTPlane
))) {
751 // check for bridges on old world
752 if ((local2
= OSDynamicCast( OSData
,
753 parent
->getProperty( gIODTAAPLInterruptsKey
)))) {
759 localBits
= (UInt32
*) local
->getBytesNoCopy();
760 localEnd
= localBits
+ (local
->getLength() / sizeof(UInt32
));
762 mapped
= OSArray::withCapacity( 1 );
763 controllers
= OSArray::withCapacity( 1 );
765 ok
= (mapped
&& controllers
);
770 skip
= IODTMapOneInterrupt( regEntry
, localBits
, index
, &map
, &controller
);
772 IOLog("%s: error mapping interrupt[%d]\n",
773 regEntry
->getName(), mapped
->getCount());
777 map
= OSData::withData( local
, mapped
->getCount() * sizeof(UInt32
),
779 controller
= gIODTDefaultInterruptController
;
780 controller
->retain();
785 mapped
->setObject( map
);
786 controllers
->setObject( controller
);
789 controllerInts
= (OSArray
*) allInts
->getObject( controller
);
790 if (controllerInts
) {
791 for (unsigned int i
= 0; (oneMap
= controllerInts
->getObject(i
)); i
++) {
792 if (map
->isEqualTo(oneMap
)) {
793 controllerInts
= (OSArray
*) gIODTSharedInterrupts
->getObject( controller
);
794 if (controllerInts
) {
795 controllerInts
->setObject(map
);
797 controllerInts
= OSArray::withObjects((const OSObject
**) &map
, 1, 4 );
798 if (controllerInts
) {
799 gIODTSharedInterrupts
->setObject( controller
, controllerInts
);
800 controllerInts
->release();
807 controllerInts
->setObject(map
);
810 controllerInts
= OSArray::withObjects((const OSObject
**) &map
, 1, 16 );
811 if (controllerInts
) {
812 allInts
->setObject( controller
, controllerInts
);
813 controllerInts
->release();
819 controller
->release();
820 } while (localBits
< localEnd
);
823 ok
&= (localBits
== localEnd
);
827 ok
= regEntry
->setProperty( gIOInterruptControllersKey
, controllers
);
828 ok
&= regEntry
->setProperty( gIOInterruptSpecifiersKey
, mapped
);
832 controllers
->release();
842 IODTMapInterrupts( IORegistryEntry
* regEntry
)
844 return IODTMapInterruptsSharing( regEntry
, NULL
);
851 CompareKey( OSString
* key
,
852 const IORegistryEntry
* table
, const OSSymbol
* propName
,
853 LIBKERN_RETURNS_RETAINED OSString
** matchingName
)
862 const char *lastName
;
865 const char *result
= NULL
;
867 if (NULL
== (prop
= table
->copyProperty( propName
))) {
871 if ((data
= OSDynamicCast( OSData
, prop
))) {
872 names
= (const char *) data
->getBytesNoCopy();
873 lastName
= names
+ data
->getLength();
874 } else if ((string
= OSDynamicCast( OSString
, prop
))) {
875 names
= string
->getCStringNoCopy();
876 lastName
= names
+ string
->getLength() + 1;
882 ckey
= key
->getCStringNoCopy();
883 keyLen
= key
->getLength();
884 wild
= ('*' == key
->getChar( keyLen
- 1 ));
887 // for each name in the property
888 nlen
= strnlen(names
, lastName
- names
);
890 matched
= ((nlen
>= (keyLen
- 1)) && (0 == strncmp(ckey
, names
, keyLen
- 1)));
892 matched
= (keyLen
== nlen
) && (0 == strncmp(ckey
, names
, keyLen
));
899 names
= names
+ nlen
+ 1;
900 } while ((names
< lastName
) && (false == matched
));
903 if (result
&& matchingName
) {
904 *matchingName
= OSString::withCString( result
);
911 return result
!= NULL
;
916 IODTCompareNubName( const IORegistryEntry
* regEntry
,
917 OSString
* name
, OSString
** matchingName
)
921 matched
= CompareKey( name
, regEntry
, gIODTNameKey
, matchingName
)
922 || CompareKey( name
, regEntry
, gIODTCompatibleKey
, matchingName
)
923 || CompareKey( name
, regEntry
, gIODTTypeKey
, matchingName
)
924 || CompareKey( name
, regEntry
, gIODTModelKey
, matchingName
);
930 IODTMatchNubWithKeys( IORegistryEntry
* regEntry
,
936 obj
= OSUnserialize( keys
, NULL
);
939 result
= regEntry
->compareNames( obj
);
944 IOLog("Couldn't unserialize %s\n", keys
);
951 OSCollectionIterator
*
952 IODTFindMatchingEntries( IORegistryEntry
* from
,
953 IOOptionBits options
, const char * keys
)
955 OSSet
*result
= NULL
;
956 IORegistryEntry
*next
;
957 IORegistryIterator
*iter
;
958 OSCollectionIterator
*cIter
;
960 bool minus
= options
& kIODTExclusive
;
963 iter
= IORegistryIterator::iterateOver( from
, gIODTPlane
,
964 (options
& kIODTRecursive
) ? kIORegistryIterateRecursively
: 0 );
970 result
= OSSet::withCapacity( 3 );
976 while ((next
= iter
->getNextObject())) {
977 // Look for existence of a debug property to skip
978 if (next
->getProperty("AAPL,ignore")) {
983 cmp
= IODTMatchNubWithKeys( next
, keys
);
984 if ((minus
&& (false == cmp
))
985 || ((false == minus
) && (false != cmp
))) {
986 result
->setObject( next
);
989 result
->setObject( next
);
992 } while (!iter
->isValid());
997 cIter
= OSCollectionIterator::withCollection( result
);
1006 struct IODTPersistent
{
1007 IODTCompareAddressCellFunc compareFunc
;
1011 IODTSetResolving( IORegistryEntry
* regEntry
,
1012 IODTCompareAddressCellFunc compareFunc
,
1013 IODTNVLocationFunc locationFunc __unused
)
1015 IODTPersistent persist
;
1016 IODTPersistent
* entry
;
1018 unsigned int index
, count
;
1020 IOLockLock(gIODTResolversLock
);
1022 count
= (gIODTResolvers
->getLength() / sizeof(IODTPersistent
));
1023 entry
= (typeof(entry
))gIODTResolvers
->getBytesNoCopy();
1024 for (index
= 0; index
< count
; index
++) {
1025 if (compareFunc
== entry
->compareFunc
) {
1030 if (index
== count
) {
1031 persist
.compareFunc
= compareFunc
;
1032 if (!gIODTResolvers
->appendBytes(&persist
, sizeof(IODTPersistent
))) {
1033 panic("IODTSetResolving");
1037 IOLockUnlock(gIODTResolversLock
);
1039 num
= OSNumber::withNumber(index
, 32);
1040 regEntry
->setProperty(gIODTPersistKey
, num
);
1041 OSSafeReleaseNULL(num
);
1046 #if defined(__arm64__)
1048 DefaultCompare( UInt32 cellCount
, UInt32 left
[], UInt32 right
[] )
1052 if (cellCount
== 2) {
1053 diff
= IOPhysical32(left
[1], left
[0]) - IOPhysical32(right
[1], right
[0]);
1054 } else if (cellCount
== 1) {
1055 diff
= (left
[0] - right
[0]);
1057 panic("DefaultCompare only knows how to handle 1 or 2 cells.");
1062 #elif defined(__arm__) || defined(__i386__) || defined(__x86_64__)
1064 DefaultCompare( UInt32 cellCount
, UInt32 left
[], UInt32 right
[] )
1067 return left
[cellCount
] - right
[cellCount
];
1070 #error Unknown architecture.
1074 AddLengthToCells( UInt32 numCells
, UInt32
*cells
, UInt64 offset
)
1076 if (numCells
== 1) {
1077 cells
[0] += (UInt32
)offset
;
1079 #if defined(__arm64__) || defined(__arm__)
1080 UInt64 sum
= cells
[numCells
- 2] + offset
;
1081 cells
[numCells
- 2] = (UInt32
)sum
;
1082 if (sum
> UINT32_MAX
) {
1083 cells
[numCells
- 1] += (UInt32
)(sum
>> 32);
1086 UInt64 sum
= cells
[numCells
- 1] + offset
;
1087 cells
[numCells
- 1] = (UInt32
)sum
;
1088 if (sum
> UINT32_MAX
) {
1089 cells
[numCells
- 2] += (UInt32
)(sum
>> 32);
1095 static IOPhysicalAddress
1096 CellsValue( UInt32 numCells
, UInt32
*cells
)
1098 if (numCells
== 1) {
1099 return IOPhysical32( 0, cells
[0] );
1101 #if defined(__arm64__) || defined(arm)
1102 return IOPhysical32( cells
[numCells
- 1], cells
[numCells
- 2] );
1104 return IOPhysical32( cells
[numCells
- 2], cells
[numCells
- 1] );
1110 IODTGetCellCounts( IORegistryEntry
* regEntry
,
1111 UInt32
* sizeCount
, UInt32
* addressCount
)
1113 if (!GetUInt32( regEntry
, gIODTSizeCellKey
, sizeCount
)) {
1116 if (!GetUInt32( regEntry
, gIODTAddressCellKey
, addressCount
)) {
1122 // Given addr & len cells from our child, find it in our ranges property, then
1123 // look in our parent to resolve the base of the range for us.
1125 // Range[]: child-addr our-addr child-len
1126 // #cells: child ours child
1129 IODTResolveAddressCell( IORegistryEntry
* startEntry
,
1131 IOPhysicalAddress
* phys
, IOPhysicalLength
* lenOut
)
1133 IORegistryEntry
* parent
;
1134 IORegistryEntry
* regEntry
;
1137 unsigned int index
, count
;
1138 // cells in addresses at regEntry
1139 UInt32 sizeCells
, addressCells
;
1140 // cells in addresses below regEntry
1141 UInt32 childSizeCells
, childAddressCells
;
1143 UInt32 cell
[8], propLen
;
1151 SInt64 diff
, diff2
, endDiff
;
1152 UInt64 len
, rangeLen
;
1154 IODTPersistent
*persist
;
1155 IODTCompareAddressCellFunc compare
;
1157 regEntry
= startEntry
;
1158 IODTGetCellCounts( regEntry
, &childSizeCells
, &childAddressCells
);
1159 childCells
= childAddressCells
+ childSizeCells
;
1161 if (childCells
> sizeof(cell
) / sizeof(cell
[0])) {
1162 panic("IODTResolveAddressCell: Invalid device tree (%u,%u)", (uint32_t)childAddressCells
, (uint32_t)childSizeCells
);
1165 bcopy( cellsIn
, cell
, sizeof(UInt32
) * childCells
);
1166 *lenOut
= CellsValue( childSizeCells
, cellsIn
+ childAddressCells
);
1169 prop
= OSDynamicCast( OSData
, regEntry
->getProperty( gIODTRangeKey
));
1171 /* end of the road */
1172 *phys
= CellsValue( childAddressCells
, cell
);
1174 if (regEntry
!= startEntry
) {
1175 regEntry
->release();
1180 parent
= regEntry
->copyParentEntry( gIODTPlane
);
1181 IODTGetCellCounts( parent
, &sizeCells
, &addressCells
);
1183 if ((propLen
= prop
->getLength())) {
1185 startRange
= (UInt32
*) prop
->getBytesNoCopy();
1187 endRanges
= range
+ (propLen
/ sizeof(UInt32
));
1190 num
= OSDynamicCast(OSNumber
, regEntry
->getProperty(gIODTPersistKey
));
1192 IOLockLock(gIODTResolversLock
);
1193 index
= num
->unsigned32BitValue();
1194 count
= gIODTResolvers
->getLength() / sizeof(IODTPersistent
);
1195 if (index
< count
) {
1196 persist
= ((IODTPersistent
*) gIODTResolvers
->getBytesNoCopy()) + index
;
1197 compare
= persist
->compareFunc
;
1199 IOLockUnlock(gIODTResolversLock
);
1202 if (!compare
&& (addressCells
== childAddressCells
)) {
1203 compare
= DefaultCompare
;
1206 panic("There is no mixed comparison function yet...");
1211 range
+= (childCells
+ addressCells
)) {
1212 // is cell start within range?
1213 diff
= (*compare
)( childAddressCells
, cell
, range
);
1215 if (childAddressCells
> sizeof(endCell
) / sizeof(endCell
[0])) {
1216 panic("IODTResolveAddressCell: Invalid device tree (%u)", (uint32_t)childAddressCells
);
1219 bcopy(range
, endCell
, childAddressCells
* sizeof(UInt32
));
1221 rangeLen
= CellsValue(childSizeCells
, range
+ childAddressCells
+ addressCells
);
1222 AddLengthToCells(childAddressCells
, endCell
, rangeLen
);
1224 diff2
= (*compare
)( childAddressCells
, cell
, endCell
);
1226 // if start of cell < start of range, or end of range >= start of cell, skip
1227 if ((diff
< 0) || (diff2
>= 0)) {
1231 len
= CellsValue(childSizeCells
, cell
+ childAddressCells
);
1235 // search for cell end
1236 bcopy(cell
, endCell
, childAddressCells
* sizeof(UInt32
));
1238 AddLengthToCells(childAddressCells
, endCell
, len
- 1);
1240 for (lookRange
= startRange
;
1241 lookRange
< endRanges
;
1242 lookRange
+= (childCells
+ addressCells
)) {
1243 // make sure end of cell >= range start
1244 endDiff
= (*compare
)( childAddressCells
, endCell
, lookRange
);
1249 UInt64 rangeStart
= CellsValue(addressCells
, range
+ childAddressCells
);
1250 UInt64 lookRangeStart
= CellsValue(addressCells
, lookRange
+ childAddressCells
);
1251 if ((endDiff
- len
+ 1 + lookRangeStart
) == (diff
+ rangeStart
)) {
1264 if (addressCells
+ sizeCells
> sizeof(cell
) / sizeof(cell
[0])) {
1265 panic("IODTResolveAddressCell: Invalid device tree (%u, %u)", (uint32_t)addressCells
, (uint32_t)sizeCells
);
1268 // Get the physical start of the range from our parent
1269 bcopy( range
+ childAddressCells
, cell
, sizeof(UInt32
) * addressCells
);
1270 bzero( cell
+ addressCells
, sizeof(UInt32
) * sizeCells
);
1271 } /* else zero length range => pass thru to parent */
1273 if (regEntry
!= startEntry
) {
1274 regEntry
->release();
1277 childSizeCells
= sizeCells
;
1278 childAddressCells
= addressCells
;
1279 childCells
= childAddressCells
+ childSizeCells
;
1280 }while (ok
&& regEntry
);
1287 IODTResolveAddressing( IORegistryEntry
* regEntry
,
1288 const char * addressPropertyName
,
1289 IODeviceMemory
* parent
)
1291 IORegistryEntry
*parentEntry
;
1292 OSData
*addressProperty
;
1293 UInt32 sizeCells
, addressCells
, cells
;
1296 IOPhysicalAddress phys
;
1297 IOPhysicalLength len
;
1299 IODeviceMemory
*range
;
1303 parentEntry
= regEntry
->copyParentEntry( gIODTPlane
);
1304 addressProperty
= (OSData
*) regEntry
->getProperty( addressPropertyName
);
1305 if ((NULL
== addressProperty
) || (NULL
== parentEntry
)) {
1309 IODTGetCellCounts( parentEntry
, &sizeCells
, &addressCells
);
1310 if (0 == sizeCells
) {
1314 cells
= sizeCells
+ addressCells
;
1315 reg
= (UInt32
*) addressProperty
->getBytesNoCopy();
1316 num
= addressProperty
->getLength() / (4 * cells
);
1318 array
= OSArray::withCapacity( 1 );
1319 if (NULL
== array
) {
1323 for (i
= 0; i
< num
; i
++) {
1324 if (IODTResolveAddressCell( parentEntry
, reg
, &phys
, &len
)) {
1327 range
= IODeviceMemory::withSubRange( parent
,
1328 phys
- parent
->getPhysicalSegment(0, NULL
, kIOMemoryMapperNone
), len
);
1330 if (NULL
== range
) {
1331 range
= IODeviceMemory::withRange( phys
, len
);
1334 array
->setObject( range
);
1340 regEntry
->setProperty( gIODeviceMemoryKey
, array
);
1341 array
->release(); /* ??? */
1344 OSSafeReleaseNULL(parentEntry
);
1350 IODTFindSlotName( IORegistryEntry
* regEntry
, UInt32 deviceNumber
)
1352 IORegistryEntry
*parent
;
1362 data
= (OSData
*) regEntry
->getProperty("AAPL,slot-name");
1368 parent
= regEntry
->copyParentEntry( gIODTPlane
);
1373 data
= OSDynamicCast( OSData
, parent
->getProperty("slot-names"));
1377 if (data
->getLength() <= 4) {
1381 bits
= (UInt32
*) data
->getBytesNoCopy();
1383 if ((0 == (mask
& (1 << deviceNumber
)))) {
1387 names
= (char *)(bits
+ 1);
1388 lastName
= names
+ (data
->getLength() - 4);
1390 for (i
= 0; (i
<= deviceNumber
) && (names
< lastName
); i
++) {
1391 if (mask
& (1 << i
)) {
1392 nlen
= 1 + strnlen(names
, lastName
- names
);
1393 if (i
== deviceNumber
) {
1394 data
= OSData::withBytesNoCopy(names
, nlen
);
1396 regEntry
->setProperty("AAPL,slot-name", data
);
1407 OSSafeReleaseNULL(parent
);
1413 IONDRVLibrariesInitialize( IOService
* provider
)
1415 return kIOReturnUnsupported
;