xref: /xnu-10063.101.15/iokit/Kernel/IODeviceTreeSupport.cpp (revision 94d3b452840153a99b38a3a9659680b2a006908e)
1*94d3b452SApple OSS Distributions /*
2*94d3b452SApple OSS Distributions  * Copyright (c) 1998-2006 Apple Computer, Inc. All rights reserved.
3*94d3b452SApple OSS Distributions  *
4*94d3b452SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*94d3b452SApple OSS Distributions  *
6*94d3b452SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*94d3b452SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*94d3b452SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*94d3b452SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*94d3b452SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*94d3b452SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*94d3b452SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*94d3b452SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*94d3b452SApple OSS Distributions  *
15*94d3b452SApple OSS Distributions  * Please obtain a copy of the License at
16*94d3b452SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*94d3b452SApple OSS Distributions  *
18*94d3b452SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*94d3b452SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*94d3b452SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*94d3b452SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*94d3b452SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*94d3b452SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*94d3b452SApple OSS Distributions  * limitations under the License.
25*94d3b452SApple OSS Distributions  *
26*94d3b452SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*94d3b452SApple OSS Distributions  */
28*94d3b452SApple OSS Distributions 
29*94d3b452SApple OSS Distributions #include <IOKit/IODeviceTreeSupport.h>
30*94d3b452SApple OSS Distributions #include <libkern/c++/OSContainers.h>
31*94d3b452SApple OSS Distributions #include <libkern/c++/OSSharedPtr.h>
32*94d3b452SApple OSS Distributions #include <IOKit/IODeviceMemory.h>
33*94d3b452SApple OSS Distributions #include <IOKit/IOService.h>
34*94d3b452SApple OSS Distributions #include <IOKit/IOCatalogue.h>
35*94d3b452SApple OSS Distributions 
36*94d3b452SApple OSS Distributions #include <IOKit/IOLib.h>
37*94d3b452SApple OSS Distributions #include <IOKit/IOKitKeys.h>
38*94d3b452SApple OSS Distributions 
39*94d3b452SApple OSS Distributions #include <pexpert/device_tree.h>
40*94d3b452SApple OSS Distributions 
41*94d3b452SApple OSS Distributions #if __arm64__
42*94d3b452SApple OSS Distributions typedef UInt64  dtptr_t;
43*94d3b452SApple OSS Distributions #else
44*94d3b452SApple OSS Distributions typedef UInt32  dtptr_t;
45*94d3b452SApple OSS Distributions #endif
46*94d3b452SApple OSS Distributions 
47*94d3b452SApple OSS Distributions #include <machine/machine_routines.h>
48*94d3b452SApple OSS Distributions 
49*94d3b452SApple OSS Distributions extern "C" {
50*94d3b452SApple OSS Distributions int IODTGetLoaderInfo( const char *key, void **infoAddr, int *infosize );
51*94d3b452SApple OSS Distributions void IODTFreeLoaderInfo( const char *key, void *infoAddr, int infoSize );
52*94d3b452SApple OSS Distributions int IODTGetDefault(const char *key, void *infoAddr, unsigned int infoSize );
53*94d3b452SApple OSS Distributions }
54*94d3b452SApple OSS Distributions 
55*94d3b452SApple OSS Distributions #include <IOKit/assert.h>
56*94d3b452SApple OSS Distributions 
57*94d3b452SApple OSS Distributions #define IODTSUPPORTDEBUG 0
58*94d3b452SApple OSS Distributions 
59*94d3b452SApple OSS Distributions struct IODTPersistent {
60*94d3b452SApple OSS Distributions 	IODTCompareAddressCellFunc  compareFunc;
61*94d3b452SApple OSS Distributions };
62*94d3b452SApple OSS Distributions 
63*94d3b452SApple OSS Distributions struct IODTResolvers {
64*94d3b452SApple OSS Distributions 	unsigned int     alloc;
65*94d3b452SApple OSS Distributions 	unsigned int     count;
66*94d3b452SApple OSS Distributions 	IOLock         * lock;
67*94d3b452SApple OSS Distributions 	IODTPersistent * resolvers;
68*94d3b452SApple OSS Distributions };
69*94d3b452SApple OSS Distributions 
70*94d3b452SApple OSS Distributions const IORegistryPlane * gIODTPlane;
71*94d3b452SApple OSS Distributions 
72*94d3b452SApple OSS Distributions static OSArray *    gIODTPHandles;
73*94d3b452SApple OSS Distributions static OSArray *    gIODTPHandleMap;
74*94d3b452SApple OSS Distributions 
75*94d3b452SApple OSS Distributions static IODTResolvers *  gIODTResolvers;
76*94d3b452SApple OSS Distributions 
77*94d3b452SApple OSS Distributions const OSSymbol *        gIODTNameKey;
78*94d3b452SApple OSS Distributions const OSSymbol *        gIODTUnitKey;
79*94d3b452SApple OSS Distributions const OSSymbol *        gIODTCompatibleKey;
80*94d3b452SApple OSS Distributions const OSSymbol *        gIODTTypeKey;
81*94d3b452SApple OSS Distributions const OSSymbol *        gIODTModelKey;
82*94d3b452SApple OSS Distributions const OSSymbol *        gIODTBridgeModelKey;
83*94d3b452SApple OSS Distributions const OSSymbol *        gIODTTargetTypeKey;
84*94d3b452SApple OSS Distributions 
85*94d3b452SApple OSS Distributions const OSSymbol *        gIODTSizeCellKey;
86*94d3b452SApple OSS Distributions const OSSymbol *        gIODTAddressCellKey;
87*94d3b452SApple OSS Distributions const OSSymbol *        gIODTRangeKey;
88*94d3b452SApple OSS Distributions 
89*94d3b452SApple OSS Distributions const OSSymbol *        gIODTPersistKey;
90*94d3b452SApple OSS Distributions 
91*94d3b452SApple OSS Distributions const OSSymbol *        gIODTDefaultInterruptController;
92*94d3b452SApple OSS Distributions const OSSymbol *        gIODTAAPLInterruptsKey;
93*94d3b452SApple OSS Distributions const OSSymbol *        gIODTPHandleKey;
94*94d3b452SApple OSS Distributions const OSSymbol *        gIODTInterruptCellKey;
95*94d3b452SApple OSS Distributions const OSSymbol *        gIODTInterruptParentKey;
96*94d3b452SApple OSS Distributions const OSSymbol *        gIODTNWInterruptMappingKey;
97*94d3b452SApple OSS Distributions 
98*94d3b452SApple OSS Distributions const OSData *          gIODTAssociatedServiceKey;
99*94d3b452SApple OSS Distributions 
100*94d3b452SApple OSS Distributions OSDictionary   *        gIODTSharedInterrupts;
101*94d3b452SApple OSS Distributions 
102*94d3b452SApple OSS Distributions static IORegistryEntry * MakeReferenceTable( DTEntry dtEntry, bool copy );
103*94d3b452SApple OSS Distributions static void AddPHandle( IORegistryEntry * regEntry );
104*94d3b452SApple OSS Distributions static void FreePhysicalMemory( vm_offset_t * range );
105*94d3b452SApple OSS Distributions static bool IODTMapInterruptsSharing( IORegistryEntry * regEntry, OSDictionary * allInts );
106*94d3b452SApple OSS Distributions 
107*94d3b452SApple OSS Distributions // FIXME: Implementation of this function is hidden from the static analyzer.
108*94d3b452SApple OSS Distributions // The analyzer doesn't know that the registry holds retains, and gets confused
109*94d3b452SApple OSS Distributions // about releases after calls to 'attachToParent'.
110*94d3b452SApple OSS Distributions // Feel free to remove the #ifndef and address the warning!
111*94d3b452SApple OSS Distributions #ifndef __clang_analyzer__
112*94d3b452SApple OSS Distributions IORegistryEntry *
IODeviceTreeAlloc(void * dtTop)113*94d3b452SApple OSS Distributions IODeviceTreeAlloc( void * dtTop )
114*94d3b452SApple OSS Distributions {
115*94d3b452SApple OSS Distributions 	IORegistryEntry *           parent;
116*94d3b452SApple OSS Distributions 	IORegistryEntry *           child;
117*94d3b452SApple OSS Distributions 	IORegistryIterator *        regIter;
118*94d3b452SApple OSS Distributions 	OpaqueDTEntryIterator       iter;
119*94d3b452SApple OSS Distributions 	DTEntry                     dtChild;
120*94d3b452SApple OSS Distributions 	DTEntry                     mapEntry;
121*94d3b452SApple OSS Distributions 	OSArray *                   stack;
122*94d3b452SApple OSS Distributions 	OSData *                    prop;
123*94d3b452SApple OSS Distributions 	OSDictionary *              allInts;
124*94d3b452SApple OSS Distributions 	vm_offset_t *               dtMap;
125*94d3b452SApple OSS Distributions 	unsigned int                propSize;
126*94d3b452SApple OSS Distributions 	bool                        intMap;
127*94d3b452SApple OSS Distributions 	bool                        foundDTNode;
128*94d3b452SApple OSS Distributions 	bool                        freeDT;
129*94d3b452SApple OSS Distributions 	char                        exBootArg[64];
130*94d3b452SApple OSS Distributions 	const char *                found;
131*94d3b452SApple OSS Distributions 
132*94d3b452SApple OSS Distributions 	gIODTPlane = IORegistryEntry::makePlane( kIODeviceTreePlane );
133*94d3b452SApple OSS Distributions 
134*94d3b452SApple OSS Distributions 	gIODTNameKey                = OSSymbol::withCStringNoCopy( "name" );
135*94d3b452SApple OSS Distributions 	gIODTUnitKey                = OSSymbol::withCStringNoCopy( "AAPL,unit-string" );
136*94d3b452SApple OSS Distributions 	gIODTCompatibleKey  = OSSymbol::withCStringNoCopy( "compatible" );
137*94d3b452SApple OSS Distributions 	gIODTTypeKey                = OSSymbol::withCStringNoCopy( "device_type" );
138*94d3b452SApple OSS Distributions 	gIODTModelKey               = OSSymbol::withCStringNoCopy( "model" );
139*94d3b452SApple OSS Distributions 	gIODTBridgeModelKey         = OSSymbol::withCStringNoCopy( "bridge-model" );
140*94d3b452SApple OSS Distributions 	gIODTTargetTypeKey          = OSSymbol::withCStringNoCopy( "target-type" );
141*94d3b452SApple OSS Distributions 	gIODTSizeCellKey    = OSSymbol::withCStringNoCopy( "#size-cells" );
142*94d3b452SApple OSS Distributions 	gIODTAddressCellKey = OSSymbol::withCStringNoCopy( "#address-cells" );
143*94d3b452SApple OSS Distributions 	gIODTRangeKey               = OSSymbol::withCStringNoCopy( "ranges" );
144*94d3b452SApple OSS Distributions 	gIODTPersistKey             = OSSymbol::withCStringNoCopy( "IODTPersist" );
145*94d3b452SApple OSS Distributions 	gIODTAssociatedServiceKey   = OSData::withBytesNoCopy((void *) kIODTAssociatedServiceKey, sizeof(kIODTAssociatedServiceKey));
146*94d3b452SApple OSS Distributions 
147*94d3b452SApple OSS Distributions 
148*94d3b452SApple OSS Distributions 	assert(    gIODTPlane && gIODTCompatibleKey
149*94d3b452SApple OSS Distributions 	    && gIODTTypeKey && gIODTModelKey
150*94d3b452SApple OSS Distributions 	    && gIODTSizeCellKey && gIODTAddressCellKey && gIODTRangeKey
151*94d3b452SApple OSS Distributions 	    && gIODTPersistKey );
152*94d3b452SApple OSS Distributions 
153*94d3b452SApple OSS Distributions 	gIODTDefaultInterruptController
154*94d3b452SApple OSS Distributions 	        = OSSymbol::withCStringNoCopy("IOPrimaryInterruptController");
155*94d3b452SApple OSS Distributions 	gIODTNWInterruptMappingKey
156*94d3b452SApple OSS Distributions 	        = OSSymbol::withCStringNoCopy("IONWInterrupts");
157*94d3b452SApple OSS Distributions 
158*94d3b452SApple OSS Distributions 	gIODTAAPLInterruptsKey
159*94d3b452SApple OSS Distributions 	        = OSSymbol::withCStringNoCopy("AAPL,interrupts");
160*94d3b452SApple OSS Distributions 	gIODTPHandleKey
161*94d3b452SApple OSS Distributions 	        = OSSymbol::withCStringNoCopy("AAPL,phandle");
162*94d3b452SApple OSS Distributions 
163*94d3b452SApple OSS Distributions 	gIODTInterruptParentKey
164*94d3b452SApple OSS Distributions 	        = OSSymbol::withCStringNoCopy("interrupt-parent");
165*94d3b452SApple OSS Distributions 
166*94d3b452SApple OSS Distributions 	gIODTPHandles       = OSArray::withCapacity( 1 );
167*94d3b452SApple OSS Distributions 	gIODTPHandleMap     = OSArray::withCapacity( 1 );
168*94d3b452SApple OSS Distributions 
169*94d3b452SApple OSS Distributions 	gIODTResolvers            = zalloc_permanent_type(IODTResolvers);
170*94d3b452SApple OSS Distributions 	gIODTResolvers->count     = 0;
171*94d3b452SApple OSS Distributions 	gIODTResolvers->alloc     = 2;
172*94d3b452SApple OSS Distributions 	gIODTResolvers->resolvers = IONewZero(IODTPersistent, gIODTResolvers->alloc);
173*94d3b452SApple OSS Distributions 	gIODTResolvers->lock      = IOLockAlloc();
174*94d3b452SApple OSS Distributions 
175*94d3b452SApple OSS Distributions 	if (!PE_parse_boot_argn("exp", exBootArg, sizeof(exBootArg))) {
176*94d3b452SApple OSS Distributions 		exBootArg[0] = '\0';
177*94d3b452SApple OSS Distributions 	}
178*94d3b452SApple OSS Distributions 
179*94d3b452SApple OSS Distributions 	gIODTInterruptCellKey
180*94d3b452SApple OSS Distributions 	        = OSSymbol::withCStringNoCopy("#interrupt-cells");
181*94d3b452SApple OSS Distributions 
182*94d3b452SApple OSS Distributions 	assert(    gIODTDefaultInterruptController && gIODTNWInterruptMappingKey
183*94d3b452SApple OSS Distributions 	    && gIODTAAPLInterruptsKey
184*94d3b452SApple OSS Distributions 	    && gIODTPHandleKey && gIODTInterruptParentKey
185*94d3b452SApple OSS Distributions 	    && gIODTPHandles && gIODTPHandleMap && gIODTInterruptCellKey
186*94d3b452SApple OSS Distributions 	    && gIODTResolvers && gIODTResolvers->lock && gIODTResolvers->resolvers
187*94d3b452SApple OSS Distributions 	    );
188*94d3b452SApple OSS Distributions 
189*94d3b452SApple OSS Distributions 	foundDTNode = (kSuccess == SecureDTLookupEntry( NULL, "/chosen/memory-map", &mapEntry ))
190*94d3b452SApple OSS Distributions 	    && (kSuccess == SecureDTGetProperty( mapEntry,
191*94d3b452SApple OSS Distributions 	    "DeviceTree", (void const **) &dtMap, &propSize ))
192*94d3b452SApple OSS Distributions 	    && ((2 * sizeof(uint32_t)) == propSize);
193*94d3b452SApple OSS Distributions 
194*94d3b452SApple OSS Distributions 	freeDT = foundDTNode && !SecureDTIsLockedDown();
195*94d3b452SApple OSS Distributions 
196*94d3b452SApple OSS Distributions 	parent = MakeReferenceTable((DTEntry)dtTop, freeDT );
197*94d3b452SApple OSS Distributions 
198*94d3b452SApple OSS Distributions 	stack = OSArray::withObjects((const OSObject **) &parent, 1, 10 );
199*94d3b452SApple OSS Distributions 	SecureDTInitEntryIterator((DTEntry)dtTop, &iter );
200*94d3b452SApple OSS Distributions 
201*94d3b452SApple OSS Distributions 	do {
202*94d3b452SApple OSS Distributions 		parent = (IORegistryEntry *)stack->getObject( stack->getCount() - 1);
203*94d3b452SApple OSS Distributions 		//parent->release();
204*94d3b452SApple OSS Distributions 		stack->removeObject( stack->getCount() - 1);
205*94d3b452SApple OSS Distributions 
206*94d3b452SApple OSS Distributions 		while (kSuccess == SecureDTIterateEntries( &iter, &dtChild)) {
207*94d3b452SApple OSS Distributions 			child = MakeReferenceTable( dtChild, freeDT );
208*94d3b452SApple OSS Distributions 			child->attachToParent( parent, gIODTPlane);
209*94d3b452SApple OSS Distributions 
210*94d3b452SApple OSS Distributions 			AddPHandle( child );
211*94d3b452SApple OSS Distributions 			// E.g. exp=sgx:3 or exp=sgx:3,5
212*94d3b452SApple OSS Distributions 			if ((found = strnstr(exBootArg, child->getName(), sizeof(exBootArg)))) {
213*94d3b452SApple OSS Distributions 				child->setProperty(gIOExclaveAssignedKey, kOSBooleanTrue);
214*94d3b452SApple OSS Distributions 				uint32_t ep = 0;
215*94d3b452SApple OSS Distributions 				uint32_t edk_ep = 0;
216*94d3b452SApple OSS Distributions 				found += strlen(child->getName());
217*94d3b452SApple OSS Distributions 				if (':' == *found) {
218*94d3b452SApple OSS Distributions 					char *end;
219*94d3b452SApple OSS Distributions 					ep = (uint32_t) strtol(found + 1, &end, 0);
220*94d3b452SApple OSS Distributions 					// Check for optional edk endpoint
221*94d3b452SApple OSS Distributions 					if (',' == *end) {
222*94d3b452SApple OSS Distributions 						edk_ep = (uint32_t) strtol(end + 1, &end, 0);
223*94d3b452SApple OSS Distributions 						child->setProperty("exclave-edk-endpoint", &edk_ep, sizeof(edk_ep));
224*94d3b452SApple OSS Distributions 					}
225*94d3b452SApple OSS Distributions 				}
226*94d3b452SApple OSS Distributions 				child->setProperty("exclave-endpoint", &ep, sizeof(ep));
227*94d3b452SApple OSS Distributions 			}
228*94d3b452SApple OSS Distributions 
229*94d3b452SApple OSS Distributions 			if (kSuccess == SecureDTEnterEntry( &iter, dtChild)) {
230*94d3b452SApple OSS Distributions 				stack->setObject( parent);
231*94d3b452SApple OSS Distributions 				parent = child;
232*94d3b452SApple OSS Distributions 			}
233*94d3b452SApple OSS Distributions 			// only registry holds retain
234*94d3b452SApple OSS Distributions 			child->release();
235*94d3b452SApple OSS Distributions 		}
236*94d3b452SApple OSS Distributions 	} while (stack->getCount()
237*94d3b452SApple OSS Distributions 	    && (kSuccess == SecureDTExitEntry( &iter, &dtChild)));
238*94d3b452SApple OSS Distributions 
239*94d3b452SApple OSS Distributions 	stack->release();
240*94d3b452SApple OSS Distributions 	assert(kSuccess != SecureDTExitEntry(&iter, &dtChild));
241*94d3b452SApple OSS Distributions 
242*94d3b452SApple OSS Distributions 	// parent is now root of the created tree
243*94d3b452SApple OSS Distributions 
244*94d3b452SApple OSS Distributions 	// make root name first compatible entry (purely cosmetic)
245*94d3b452SApple OSS Distributions 	if ((prop = (OSData *) parent->getProperty( gIODTCompatibleKey))) {
246*94d3b452SApple OSS Distributions 		parent->setName( parent->getName(), gIODTPlane );
247*94d3b452SApple OSS Distributions 		parent->setName((const char *) prop->getBytesNoCopy());
248*94d3b452SApple OSS Distributions 	}
249*94d3b452SApple OSS Distributions 
250*94d3b452SApple OSS Distributions 	// attach tree to meta root
251*94d3b452SApple OSS Distributions 	parent->attachToParent( IORegistryEntry::getRegistryRoot(), gIODTPlane);
252*94d3b452SApple OSS Distributions 	parent->release();
253*94d3b452SApple OSS Distributions 
254*94d3b452SApple OSS Distributions 	if (freeDT) {
255*94d3b452SApple OSS Distributions 		// free original device tree
256*94d3b452SApple OSS Distributions 		SecureDTInit(NULL, 0);
257*94d3b452SApple OSS Distributions 		IODTFreeLoaderInfo( "DeviceTree",
258*94d3b452SApple OSS Distributions 		    (void *)dtMap[0], (int) round_page(dtMap[1]));
259*94d3b452SApple OSS Distributions 	}
260*94d3b452SApple OSS Distributions 
261*94d3b452SApple OSS Distributions 	// adjust tree
262*94d3b452SApple OSS Distributions 
263*94d3b452SApple OSS Distributions 	gIODTSharedInterrupts = OSDictionary::withCapacity(4);
264*94d3b452SApple OSS Distributions 	allInts = OSDictionary::withCapacity(4);
265*94d3b452SApple OSS Distributions 	intMap = false;
266*94d3b452SApple OSS Distributions 	regIter = IORegistryIterator::iterateOver( gIODTPlane,
267*94d3b452SApple OSS Distributions 	    kIORegistryIterateRecursively );
268*94d3b452SApple OSS Distributions 	assert( regIter && allInts && gIODTSharedInterrupts );
269*94d3b452SApple OSS Distributions 	if (regIter && allInts && gIODTSharedInterrupts) {
270*94d3b452SApple OSS Distributions 		while ((child = regIter->getNextObject())) {
271*94d3b452SApple OSS Distributions 			IODTMapInterruptsSharing( child, allInts );
272*94d3b452SApple OSS Distributions 			if (!intMap && child->getProperty( gIODTInterruptParentKey)) {
273*94d3b452SApple OSS Distributions 				intMap = true;
274*94d3b452SApple OSS Distributions 			}
275*94d3b452SApple OSS Distributions 			if (!strcmp("sep", child->getName())
276*94d3b452SApple OSS Distributions 			    || !strcmp("aop", child->getName())
277*94d3b452SApple OSS Distributions 			    || !strcmp("disp0", child->getName())) {
278*94d3b452SApple OSS Distributions 				uint32_t aotFlags = 1;
279*94d3b452SApple OSS Distributions 				child->setProperty("aot-power", &aotFlags, sizeof(aotFlags));
280*94d3b452SApple OSS Distributions 			}
281*94d3b452SApple OSS Distributions 		}
282*94d3b452SApple OSS Distributions 		regIter->release();
283*94d3b452SApple OSS Distributions 	}
284*94d3b452SApple OSS Distributions 
285*94d3b452SApple OSS Distributions #if IODTSUPPORTDEBUG
286*94d3b452SApple OSS Distributions 	parent->setProperty("allInts", allInts);
287*94d3b452SApple OSS Distributions 	parent->setProperty("sharedInts", gIODTSharedInterrupts);
288*94d3b452SApple OSS Distributions 
289*94d3b452SApple OSS Distributions 	regIter = IORegistryIterator::iterateOver( gIODTPlane,
290*94d3b452SApple OSS Distributions 	    kIORegistryIterateRecursively );
291*94d3b452SApple OSS Distributions 	if (regIter) {
292*94d3b452SApple OSS Distributions 		while ((child = regIter->getNextObject())) {
293*94d3b452SApple OSS Distributions 			OSArray *
294*94d3b452SApple OSS Distributions 			    array = OSDynamicCast(OSArray, child->getProperty( gIOInterruptSpecifiersKey ));
295*94d3b452SApple OSS Distributions 			for (UInt32 i = 0; array && (i < array->getCount()); i++) {
296*94d3b452SApple OSS Distributions 				IOOptionBits options;
297*94d3b452SApple OSS Distributions 				IOReturn ret = IODTGetInterruptOptions( child, i, &options );
298*94d3b452SApple OSS Distributions 				if ((ret != kIOReturnSuccess) || options) {
299*94d3b452SApple OSS Distributions 					IOLog("%s[%ld] %ld (%x)\n", child->getName(), i, options, ret);
300*94d3b452SApple OSS Distributions 				}
301*94d3b452SApple OSS Distributions 			}
302*94d3b452SApple OSS Distributions 		}
303*94d3b452SApple OSS Distributions 		regIter->release();
304*94d3b452SApple OSS Distributions 	}
305*94d3b452SApple OSS Distributions #endif
306*94d3b452SApple OSS Distributions 
307*94d3b452SApple OSS Distributions 	allInts->release();
308*94d3b452SApple OSS Distributions 
309*94d3b452SApple OSS Distributions 	if (intMap) {
310*94d3b452SApple OSS Distributions 		// set a key in the root to indicate we found NW interrupt mapping
311*94d3b452SApple OSS Distributions 		parent->setProperty( gIODTNWInterruptMappingKey,
312*94d3b452SApple OSS Distributions 		    (OSObject *) gIODTNWInterruptMappingKey );
313*94d3b452SApple OSS Distributions 	}
314*94d3b452SApple OSS Distributions 
315*94d3b452SApple OSS Distributions 	return parent;
316*94d3b452SApple OSS Distributions }
317*94d3b452SApple OSS Distributions #endif
318*94d3b452SApple OSS Distributions 
319*94d3b452SApple OSS Distributions int
IODTGetLoaderInfo(const char * key,void ** infoAddr,int * infoSize)320*94d3b452SApple OSS Distributions IODTGetLoaderInfo( const char *key, void **infoAddr, int *infoSize )
321*94d3b452SApple OSS Distributions {
322*94d3b452SApple OSS Distributions 	IORegistryEntry             *chosen;
323*94d3b452SApple OSS Distributions 	OSData                              *propObj;
324*94d3b452SApple OSS Distributions 	dtptr_t                             *propPtr;
325*94d3b452SApple OSS Distributions 	unsigned int                propSize;
326*94d3b452SApple OSS Distributions 	int ret = -1;
327*94d3b452SApple OSS Distributions 
328*94d3b452SApple OSS Distributions 	chosen = IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane );
329*94d3b452SApple OSS Distributions 	if (chosen == NULL) {
330*94d3b452SApple OSS Distributions 		return -1;
331*94d3b452SApple OSS Distributions 	}
332*94d3b452SApple OSS Distributions 
333*94d3b452SApple OSS Distributions 	propObj = OSDynamicCast( OSData, chosen->getProperty(key));
334*94d3b452SApple OSS Distributions 	if (propObj == NULL) {
335*94d3b452SApple OSS Distributions 		goto cleanup;
336*94d3b452SApple OSS Distributions 	}
337*94d3b452SApple OSS Distributions 
338*94d3b452SApple OSS Distributions 	propSize = propObj->getLength();
339*94d3b452SApple OSS Distributions 	if (propSize != (2 * sizeof(dtptr_t))) {
340*94d3b452SApple OSS Distributions 		goto cleanup;
341*94d3b452SApple OSS Distributions 	}
342*94d3b452SApple OSS Distributions 
343*94d3b452SApple OSS Distributions 	propPtr = (dtptr_t *)propObj->getBytesNoCopy();
344*94d3b452SApple OSS Distributions 	if (propPtr == NULL) {
345*94d3b452SApple OSS Distributions 		goto cleanup;
346*94d3b452SApple OSS Distributions 	}
347*94d3b452SApple OSS Distributions 
348*94d3b452SApple OSS Distributions 	*infoAddr = (void *)(uintptr_t) (propPtr[0]);
349*94d3b452SApple OSS Distributions 	*infoSize = (int)               (propPtr[1]);
350*94d3b452SApple OSS Distributions 
351*94d3b452SApple OSS Distributions 	ret = 0;
352*94d3b452SApple OSS Distributions 
353*94d3b452SApple OSS Distributions cleanup:
354*94d3b452SApple OSS Distributions 	chosen->release();
355*94d3b452SApple OSS Distributions 
356*94d3b452SApple OSS Distributions 	return ret;
357*94d3b452SApple OSS Distributions }
358*94d3b452SApple OSS Distributions 
359*94d3b452SApple OSS Distributions void
IODTFreeLoaderInfo(const char * key,void * infoAddr,int infoSize)360*94d3b452SApple OSS Distributions IODTFreeLoaderInfo( const char *key, void *infoAddr, int infoSize )
361*94d3b452SApple OSS Distributions {
362*94d3b452SApple OSS Distributions 	vm_offset_t                 range[2];
363*94d3b452SApple OSS Distributions 	IORegistryEntry             *chosen;
364*94d3b452SApple OSS Distributions 
365*94d3b452SApple OSS Distributions 	range[0] = (vm_offset_t)infoAddr;
366*94d3b452SApple OSS Distributions 	range[1] = (vm_offset_t)infoSize;
367*94d3b452SApple OSS Distributions 	FreePhysicalMemory( range );
368*94d3b452SApple OSS Distributions 
369*94d3b452SApple OSS Distributions 	if (key != NULL) {
370*94d3b452SApple OSS Distributions 		chosen = IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane );
371*94d3b452SApple OSS Distributions 		if (chosen != NULL) {
372*94d3b452SApple OSS Distributions 			chosen->removeProperty(key);
373*94d3b452SApple OSS Distributions 			chosen->release();
374*94d3b452SApple OSS Distributions 		}
375*94d3b452SApple OSS Distributions 	}
376*94d3b452SApple OSS Distributions }
377*94d3b452SApple OSS Distributions 
378*94d3b452SApple OSS Distributions int
IODTGetDefault(const char * key,void * infoAddr,unsigned int infoSize)379*94d3b452SApple OSS Distributions IODTGetDefault(const char *key, void *infoAddr, unsigned int infoSize )
380*94d3b452SApple OSS Distributions {
381*94d3b452SApple OSS Distributions 	IORegistryEntry             *defaults;
382*94d3b452SApple OSS Distributions 	OSData                      *defaultObj;
383*94d3b452SApple OSS Distributions 	unsigned int                defaultSize;
384*94d3b452SApple OSS Distributions 
385*94d3b452SApple OSS Distributions 	defaults = IORegistryEntry::fromPath( "/defaults", gIODTPlane );
386*94d3b452SApple OSS Distributions 	if (defaults == NULL) {
387*94d3b452SApple OSS Distributions 		return -1;
388*94d3b452SApple OSS Distributions 	}
389*94d3b452SApple OSS Distributions 
390*94d3b452SApple OSS Distributions 	defaultObj = OSDynamicCast( OSData, defaults->getProperty(key));
391*94d3b452SApple OSS Distributions 
392*94d3b452SApple OSS Distributions 	if (defaultObj == NULL) {
393*94d3b452SApple OSS Distributions 		defaults->release();
394*94d3b452SApple OSS Distributions 		return -1;
395*94d3b452SApple OSS Distributions 	}
396*94d3b452SApple OSS Distributions 
397*94d3b452SApple OSS Distributions 	defaultSize = defaultObj->getLength();
398*94d3b452SApple OSS Distributions 	if (defaultSize > infoSize) {
399*94d3b452SApple OSS Distributions 		defaults->release();
400*94d3b452SApple OSS Distributions 		return -1;
401*94d3b452SApple OSS Distributions 	}
402*94d3b452SApple OSS Distributions 
403*94d3b452SApple OSS Distributions 	memcpy( infoAddr, defaultObj->getBytesNoCopy(), defaultSize );
404*94d3b452SApple OSS Distributions 
405*94d3b452SApple OSS Distributions 	defaults->release();
406*94d3b452SApple OSS Distributions 	return 0;
407*94d3b452SApple OSS Distributions }
408*94d3b452SApple OSS Distributions 
409*94d3b452SApple OSS Distributions static void
FreePhysicalMemory(vm_offset_t * range)410*94d3b452SApple OSS Distributions FreePhysicalMemory( vm_offset_t * range )
411*94d3b452SApple OSS Distributions {
412*94d3b452SApple OSS Distributions 	vm_offset_t virt;
413*94d3b452SApple OSS Distributions 
414*94d3b452SApple OSS Distributions 	virt = ml_static_ptovirt( range[0] );
415*94d3b452SApple OSS Distributions 	if (virt) {
416*94d3b452SApple OSS Distributions 		ml_static_mfree( virt, range[1] );
417*94d3b452SApple OSS Distributions 	}
418*94d3b452SApple OSS Distributions }
419*94d3b452SApple OSS Distributions 
420*94d3b452SApple OSS Distributions static IORegistryEntry *
MakeReferenceTable(DTEntry dtEntry,bool copy)421*94d3b452SApple OSS Distributions MakeReferenceTable( DTEntry dtEntry, bool copy )
422*94d3b452SApple OSS Distributions {
423*94d3b452SApple OSS Distributions 	IORegistryEntry             *regEntry;
424*94d3b452SApple OSS Distributions 	OSDictionary                *propTable;
425*94d3b452SApple OSS Distributions 	const OSSymbol              *nameKey;
426*94d3b452SApple OSS Distributions 	OSData                              *data;
427*94d3b452SApple OSS Distributions 	const OSSymbol              *sym;
428*94d3b452SApple OSS Distributions 	OpaqueDTPropertyIterator    dtIter;
429*94d3b452SApple OSS Distributions 	void const                  *prop;
430*94d3b452SApple OSS Distributions 	unsigned int                propSize;
431*94d3b452SApple OSS Distributions 	char const                                      *name;
432*94d3b452SApple OSS Distributions 	char                                location[32];
433*94d3b452SApple OSS Distributions 	bool                                noLocation = true;
434*94d3b452SApple OSS Distributions 	bool                                kernelOnly;
435*94d3b452SApple OSS Distributions 
436*94d3b452SApple OSS Distributions 	regEntry = new IOService;
437*94d3b452SApple OSS Distributions 
438*94d3b452SApple OSS Distributions 	if (regEntry && (false == regEntry->init())) {
439*94d3b452SApple OSS Distributions 		regEntry->release();
440*94d3b452SApple OSS Distributions 		regEntry = NULL;
441*94d3b452SApple OSS Distributions 	}
442*94d3b452SApple OSS Distributions 
443*94d3b452SApple OSS Distributions 	if (regEntry &&
444*94d3b452SApple OSS Distributions 	    (kSuccess == SecureDTInitPropertyIterator( dtEntry, &dtIter))) {
445*94d3b452SApple OSS Distributions 		kernelOnly = (kSuccess == SecureDTGetProperty(dtEntry, "kernel-only", &prop, &propSize));
446*94d3b452SApple OSS Distributions 		propTable = regEntry->getPropertyTable();
447*94d3b452SApple OSS Distributions 
448*94d3b452SApple OSS Distributions 		while (kSuccess == SecureDTIterateProperties( &dtIter, &name)) {
449*94d3b452SApple OSS Distributions 			if (kSuccess != SecureDTGetProperty( dtEntry, name, &prop, &propSize )) {
450*94d3b452SApple OSS Distributions 				continue;
451*94d3b452SApple OSS Distributions 			}
452*94d3b452SApple OSS Distributions 
453*94d3b452SApple OSS Distributions 			if (copy) {
454*94d3b452SApple OSS Distributions 				nameKey = OSSymbol::withCString(name);
455*94d3b452SApple OSS Distributions 				data = OSData::withBytes(prop, propSize);
456*94d3b452SApple OSS Distributions 			} else {
457*94d3b452SApple OSS Distributions 				nameKey = OSSymbol::withCStringNoCopy(name);
458*94d3b452SApple OSS Distributions 				/* There is no OSDataConst or other way to indicate
459*94d3b452SApple OSS Distributions 				 * that the OSData is actually immutable. But CTRR
460*94d3b452SApple OSS Distributions 				 * will catch any write attempts. */
461*94d3b452SApple OSS Distributions 				data = OSData::withBytesNoCopy((void**)(uintptr_t)prop, propSize);
462*94d3b452SApple OSS Distributions 			}
463*94d3b452SApple OSS Distributions 			assert( nameKey && data );
464*94d3b452SApple OSS Distributions 
465*94d3b452SApple OSS Distributions #if DEVELOPMENT || DEBUG
466*94d3b452SApple OSS Distributions #pragma unused(kernelOnly)
467*94d3b452SApple OSS Distributions #else
468*94d3b452SApple OSS Distributions 			if (kernelOnly) {
469*94d3b452SApple OSS Distributions 				data->setSerializable(false);
470*94d3b452SApple OSS Distributions 			}
471*94d3b452SApple OSS Distributions #endif
472*94d3b452SApple OSS Distributions 
473*94d3b452SApple OSS Distributions 			propTable->setObject( nameKey, data);
474*94d3b452SApple OSS Distributions 			data->release();
475*94d3b452SApple OSS Distributions 			nameKey->release();
476*94d3b452SApple OSS Distributions 
477*94d3b452SApple OSS Distributions 			if (nameKey == gIODTNameKey) {
478*94d3b452SApple OSS Distributions 				if (copy) {
479*94d3b452SApple OSS Distributions 					sym = OSSymbol::withCString((const char *) prop);
480*94d3b452SApple OSS Distributions 				} else {
481*94d3b452SApple OSS Distributions 					sym = OSSymbol::withCStringNoCopy((const char *) prop);
482*94d3b452SApple OSS Distributions 				}
483*94d3b452SApple OSS Distributions 				regEntry->setName( sym );
484*94d3b452SApple OSS Distributions 				sym->release();
485*94d3b452SApple OSS Distributions 			} else if (nameKey == gIODTUnitKey) {
486*94d3b452SApple OSS Distributions 				// all OF strings are null terminated... except this one
487*94d3b452SApple OSS Distributions 				if (propSize >= (int) sizeof(location)) {
488*94d3b452SApple OSS Distributions 					propSize = sizeof(location) - 1;
489*94d3b452SApple OSS Distributions 				}
490*94d3b452SApple OSS Distributions 				strncpy( location, (const char *) prop, propSize );
491*94d3b452SApple OSS Distributions 				location[propSize] = 0;
492*94d3b452SApple OSS Distributions 				regEntry->setLocation( location );
493*94d3b452SApple OSS Distributions 				propTable->removeObject( gIODTUnitKey );
494*94d3b452SApple OSS Distributions 				noLocation = false;
495*94d3b452SApple OSS Distributions 			} else if (noLocation && (!strncmp(name, "reg", sizeof("reg")))) {
496*94d3b452SApple OSS Distributions 				// default location - override later
497*94d3b452SApple OSS Distributions 				snprintf(location, sizeof(location), "%X", *((uint32_t *) prop));
498*94d3b452SApple OSS Distributions 				regEntry->setLocation( location );
499*94d3b452SApple OSS Distributions 			}
500*94d3b452SApple OSS Distributions 		}
501*94d3b452SApple OSS Distributions 	}
502*94d3b452SApple OSS Distributions 
503*94d3b452SApple OSS Distributions 	return regEntry;
504*94d3b452SApple OSS Distributions }
505*94d3b452SApple OSS Distributions 
506*94d3b452SApple OSS Distributions static void
AddPHandle(IORegistryEntry * regEntry)507*94d3b452SApple OSS Distributions AddPHandle( IORegistryEntry * regEntry )
508*94d3b452SApple OSS Distributions {
509*94d3b452SApple OSS Distributions 	OSData *    data;
510*94d3b452SApple OSS Distributions 
511*94d3b452SApple OSS Distributions 	if (regEntry->getProperty( gIODTInterruptCellKey)
512*94d3b452SApple OSS Distributions 	    && (data = OSDynamicCast( OSData, regEntry->getProperty( gIODTPHandleKey )))) {
513*94d3b452SApple OSS Distributions 		// a possible interrupt-parent
514*94d3b452SApple OSS Distributions 		gIODTPHandles->setObject( data );
515*94d3b452SApple OSS Distributions 		gIODTPHandleMap->setObject( regEntry );
516*94d3b452SApple OSS Distributions 	}
517*94d3b452SApple OSS Distributions }
518*94d3b452SApple OSS Distributions 
519*94d3b452SApple OSS Distributions static LIBKERN_RETURNS_NOT_RETAINED IORegistryEntry *
FindPHandle(UInt32 phandle)520*94d3b452SApple OSS Distributions FindPHandle( UInt32 phandle )
521*94d3b452SApple OSS Distributions {
522*94d3b452SApple OSS Distributions 	OSData                      *data;
523*94d3b452SApple OSS Distributions 	IORegistryEntry *regEntry = NULL;
524*94d3b452SApple OSS Distributions 	int                         i;
525*94d3b452SApple OSS Distributions 
526*94d3b452SApple OSS Distributions 	for (i = 0; (data = (OSData *)gIODTPHandles->getObject( i )); i++) {
527*94d3b452SApple OSS Distributions 		if (phandle == *((UInt32 *)data->getBytesNoCopy())) {
528*94d3b452SApple OSS Distributions 			regEntry = (IORegistryEntry *)
529*94d3b452SApple OSS Distributions 			    gIODTPHandleMap->getObject( i );
530*94d3b452SApple OSS Distributions 			break;
531*94d3b452SApple OSS Distributions 		}
532*94d3b452SApple OSS Distributions 	}
533*94d3b452SApple OSS Distributions 
534*94d3b452SApple OSS Distributions 	return regEntry;
535*94d3b452SApple OSS Distributions }
536*94d3b452SApple OSS Distributions 
537*94d3b452SApple OSS Distributions static bool
GetUInt32(IORegistryEntry * regEntry,const OSSymbol * name,UInt32 * value)538*94d3b452SApple OSS Distributions GetUInt32( IORegistryEntry * regEntry, const OSSymbol * name,
539*94d3b452SApple OSS Distributions     UInt32 * value )
540*94d3b452SApple OSS Distributions {
541*94d3b452SApple OSS Distributions 	OSObject * obj;
542*94d3b452SApple OSS Distributions 	OSData   * data;
543*94d3b452SApple OSS Distributions 	bool       result;
544*94d3b452SApple OSS Distributions 
545*94d3b452SApple OSS Distributions 	if (!(obj = regEntry->copyProperty(name))) {
546*94d3b452SApple OSS Distributions 		return false;
547*94d3b452SApple OSS Distributions 	}
548*94d3b452SApple OSS Distributions 
549*94d3b452SApple OSS Distributions 	result = ((data = OSDynamicCast(OSData, obj)) && (sizeof(UInt32) == data->getLength()));
550*94d3b452SApple OSS Distributions 	if (result) {
551*94d3b452SApple OSS Distributions 		*value = *((UInt32 *) data->getBytesNoCopy());
552*94d3b452SApple OSS Distributions 	}
553*94d3b452SApple OSS Distributions 
554*94d3b452SApple OSS Distributions 	obj->release();
555*94d3b452SApple OSS Distributions 	return result;
556*94d3b452SApple OSS Distributions }
557*94d3b452SApple OSS Distributions 
558*94d3b452SApple OSS Distributions static IORegistryEntry *
IODTFindInterruptParent(IORegistryEntry * regEntry,IOItemCount index)559*94d3b452SApple OSS Distributions IODTFindInterruptParent( IORegistryEntry * regEntry, IOItemCount index )
560*94d3b452SApple OSS Distributions {
561*94d3b452SApple OSS Distributions 	IORegistryEntry *   parent;
562*94d3b452SApple OSS Distributions 	UInt32              phandle;
563*94d3b452SApple OSS Distributions 	OSData          *   data;
564*94d3b452SApple OSS Distributions 	unsigned int        len;
565*94d3b452SApple OSS Distributions 
566*94d3b452SApple OSS Distributions 	if ((data = OSDynamicCast( OSData, regEntry->getProperty( gIODTInterruptParentKey )))
567*94d3b452SApple OSS Distributions 	    && (sizeof(UInt32) <= (len = data->getLength()))) {
568*94d3b452SApple OSS Distributions 		if (((index + 1) * sizeof(UInt32)) > len) {
569*94d3b452SApple OSS Distributions 			index = 0;
570*94d3b452SApple OSS Distributions 		}
571*94d3b452SApple OSS Distributions 		phandle = ((UInt32 *) data->getBytesNoCopy())[index];
572*94d3b452SApple OSS Distributions 		parent = FindPHandle( phandle );
573*94d3b452SApple OSS Distributions 	} else if (NULL == regEntry->getProperty( "interrupt-controller")) {
574*94d3b452SApple OSS Distributions 		parent = regEntry->getParentEntry( gIODTPlane);
575*94d3b452SApple OSS Distributions 	} else {
576*94d3b452SApple OSS Distributions 		parent = NULL;
577*94d3b452SApple OSS Distributions 	}
578*94d3b452SApple OSS Distributions 
579*94d3b452SApple OSS Distributions 	return parent;
580*94d3b452SApple OSS Distributions }
581*94d3b452SApple OSS Distributions 
582*94d3b452SApple OSS Distributions const OSSymbol *
IODTInterruptControllerName(IORegistryEntry * regEntry)583*94d3b452SApple OSS Distributions IODTInterruptControllerName( IORegistryEntry * regEntry )
584*94d3b452SApple OSS Distributions {
585*94d3b452SApple OSS Distributions 	const OSSymbol      *sym;
586*94d3b452SApple OSS Distributions 	UInt32              phandle;
587*94d3b452SApple OSS Distributions 	bool                ok;
588*94d3b452SApple OSS Distributions 	char                buf[48];
589*94d3b452SApple OSS Distributions 
590*94d3b452SApple OSS Distributions 	ok = GetUInt32( regEntry, gIODTPHandleKey, &phandle);
591*94d3b452SApple OSS Distributions 	assert( ok );
592*94d3b452SApple OSS Distributions 
593*94d3b452SApple OSS Distributions 	if (ok) {
594*94d3b452SApple OSS Distributions 		snprintf(buf, sizeof(buf), "IOInterruptController%08X", (uint32_t)phandle);
595*94d3b452SApple OSS Distributions 		sym = OSSymbol::withCString( buf );
596*94d3b452SApple OSS Distributions 	} else {
597*94d3b452SApple OSS Distributions 		sym = NULL;
598*94d3b452SApple OSS Distributions 	}
599*94d3b452SApple OSS Distributions 
600*94d3b452SApple OSS Distributions 	return sym;
601*94d3b452SApple OSS Distributions }
602*94d3b452SApple OSS Distributions 
603*94d3b452SApple OSS Distributions #define unexpected(a) { kprintf("unexpected %s:%d\n", __FILE__, __LINE__); a; }
604*94d3b452SApple OSS Distributions 
605*94d3b452SApple OSS Distributions static void
IODTGetICellCounts(IORegistryEntry * regEntry,UInt32 * iCellCount,UInt32 * aCellCount)606*94d3b452SApple OSS Distributions IODTGetICellCounts( IORegistryEntry * regEntry,
607*94d3b452SApple OSS Distributions     UInt32 * iCellCount, UInt32 * aCellCount)
608*94d3b452SApple OSS Distributions {
609*94d3b452SApple OSS Distributions 	if (!GetUInt32( regEntry, gIODTInterruptCellKey, iCellCount)) {
610*94d3b452SApple OSS Distributions 		unexpected( *iCellCount = 1 );
611*94d3b452SApple OSS Distributions 	}
612*94d3b452SApple OSS Distributions 	if (!GetUInt32( regEntry, gIODTAddressCellKey, aCellCount)) {
613*94d3b452SApple OSS Distributions 		*aCellCount = 0;
614*94d3b452SApple OSS Distributions 	}
615*94d3b452SApple OSS Distributions }
616*94d3b452SApple OSS Distributions 
617*94d3b452SApple OSS Distributions static UInt32
IODTMapOneInterrupt(IORegistryEntry * regEntry,UInt32 * intSpec,UInt32 index,LIBKERN_RETURNS_RETAINED OSData ** spec,LIBKERN_RETURNS_RETAINED const OSSymbol ** controller)618*94d3b452SApple OSS Distributions IODTMapOneInterrupt( IORegistryEntry * regEntry, UInt32 * intSpec, UInt32 index,
619*94d3b452SApple OSS Distributions     LIBKERN_RETURNS_RETAINED OSData ** spec,
620*94d3b452SApple OSS Distributions     LIBKERN_RETURNS_RETAINED const OSSymbol ** controller )
621*94d3b452SApple OSS Distributions {
622*94d3b452SApple OSS Distributions 	IORegistryEntry *parent = NULL;
623*94d3b452SApple OSS Distributions 	OSData                      *data;
624*94d3b452SApple OSS Distributions 	UInt32                      *addrCmp;
625*94d3b452SApple OSS Distributions 	UInt32                      *maskCmp;
626*94d3b452SApple OSS Distributions 	UInt32                      *map;
627*94d3b452SApple OSS Distributions 	UInt32                      *endMap;
628*94d3b452SApple OSS Distributions 	UInt32                      acells, icells, pacells, picells, cell;
629*94d3b452SApple OSS Distributions 	UInt32                      i, original_icells;
630*94d3b452SApple OSS Distributions 	bool                        cmp, ok = false;
631*94d3b452SApple OSS Distributions 
632*94d3b452SApple OSS Distributions 	parent = IODTFindInterruptParent( regEntry, index );
633*94d3b452SApple OSS Distributions 	IODTGetICellCounts( parent, &icells, &acells );
634*94d3b452SApple OSS Distributions 	addrCmp = NULL;
635*94d3b452SApple OSS Distributions 	if (acells) {
636*94d3b452SApple OSS Distributions 		data = OSDynamicCast( OSData, regEntry->getProperty( "reg" ));
637*94d3b452SApple OSS Distributions 		if (data && (data->getLength() >= (acells * sizeof(UInt32)))) {
638*94d3b452SApple OSS Distributions 			addrCmp = (UInt32 *) data->getBytesNoCopy();
639*94d3b452SApple OSS Distributions 		}
640*94d3b452SApple OSS Distributions 	}
641*94d3b452SApple OSS Distributions 	original_icells = icells;
642*94d3b452SApple OSS Distributions 	regEntry = parent;
643*94d3b452SApple OSS Distributions 
644*94d3b452SApple OSS Distributions 	do {
645*94d3b452SApple OSS Distributions #if IODTSUPPORTDEBUG
646*94d3b452SApple OSS Distributions 		kprintf("IODTMapOneInterrupt: current regEntry name %s\n", regEntry->getName());
647*94d3b452SApple OSS Distributions 		kprintf("acells - icells: ");
648*94d3b452SApple OSS Distributions 		for (i = 0; i < acells; i++) {
649*94d3b452SApple OSS Distributions 			kprintf("0x%08X ", addrCmp[i]);
650*94d3b452SApple OSS Distributions 		}
651*94d3b452SApple OSS Distributions 		kprintf("- ");
652*94d3b452SApple OSS Distributions 		for (i = 0; i < icells; i++) {
653*94d3b452SApple OSS Distributions 			kprintf("0x%08X ", intSpec[i]);
654*94d3b452SApple OSS Distributions 		}
655*94d3b452SApple OSS Distributions 		kprintf("\n");
656*94d3b452SApple OSS Distributions #endif
657*94d3b452SApple OSS Distributions 
658*94d3b452SApple OSS Distributions 		if (parent && (data = OSDynamicCast( OSData,
659*94d3b452SApple OSS Distributions 		    regEntry->getProperty( "interrupt-controller")))) {
660*94d3b452SApple OSS Distributions 			// found a controller - don't want to follow cascaded controllers
661*94d3b452SApple OSS Distributions 			parent = NULL;
662*94d3b452SApple OSS Distributions 			*spec = OSData::withBytesNoCopy((void *) intSpec,
663*94d3b452SApple OSS Distributions 			    icells * sizeof(UInt32));
664*94d3b452SApple OSS Distributions 			*controller = IODTInterruptControllerName( regEntry );
665*94d3b452SApple OSS Distributions 			ok = (*spec && *controller);
666*94d3b452SApple OSS Distributions 		} else if (parent && (data = OSDynamicCast( OSData,
667*94d3b452SApple OSS Distributions 		    regEntry->getProperty( "interrupt-map")))) {
668*94d3b452SApple OSS Distributions 			// interrupt-map
669*94d3b452SApple OSS Distributions 			map = (UInt32 *) data->getBytesNoCopy();
670*94d3b452SApple OSS Distributions 			endMap = map + (data->getLength() / sizeof(UInt32));
671*94d3b452SApple OSS Distributions 			data = OSDynamicCast( OSData, regEntry->getProperty( "interrupt-map-mask" ));
672*94d3b452SApple OSS Distributions 			if (data && (data->getLength() >= ((acells + icells) * sizeof(UInt32)))) {
673*94d3b452SApple OSS Distributions 				maskCmp = (UInt32 *) data->getBytesNoCopy();
674*94d3b452SApple OSS Distributions 			} else {
675*94d3b452SApple OSS Distributions 				maskCmp = NULL;
676*94d3b452SApple OSS Distributions 			}
677*94d3b452SApple OSS Distributions 
678*94d3b452SApple OSS Distributions #if IODTSUPPORTDEBUG
679*94d3b452SApple OSS Distributions 			if (maskCmp) {
680*94d3b452SApple OSS Distributions 				kprintf("        maskCmp: ");
681*94d3b452SApple OSS Distributions 				for (i = 0; i < acells + icells; i++) {
682*94d3b452SApple OSS Distributions 					if (i == acells) {
683*94d3b452SApple OSS Distributions 						kprintf("- ");
684*94d3b452SApple OSS Distributions 					}
685*94d3b452SApple OSS Distributions 					kprintf("0x%08X ", maskCmp[i]);
686*94d3b452SApple OSS Distributions 				}
687*94d3b452SApple OSS Distributions 				kprintf("\n");
688*94d3b452SApple OSS Distributions 				kprintf("         masked: ");
689*94d3b452SApple OSS Distributions 				for (i = 0; i < acells + icells; i++) {
690*94d3b452SApple OSS Distributions 					if (i == acells) {
691*94d3b452SApple OSS Distributions 						kprintf("- ");
692*94d3b452SApple OSS Distributions 					}
693*94d3b452SApple OSS Distributions 					kprintf("0x%08X ", ((i < acells) ? addrCmp[i] : intSpec[i - acells]) & maskCmp[i]);
694*94d3b452SApple OSS Distributions 				}
695*94d3b452SApple OSS Distributions 				kprintf("\n");
696*94d3b452SApple OSS Distributions 			} else {
697*94d3b452SApple OSS Distributions 				kprintf("no maskCmp\n");
698*94d3b452SApple OSS Distributions 			}
699*94d3b452SApple OSS Distributions #endif
700*94d3b452SApple OSS Distributions 			do {
701*94d3b452SApple OSS Distributions #if IODTSUPPORTDEBUG
702*94d3b452SApple OSS Distributions 				kprintf("            map: ");
703*94d3b452SApple OSS Distributions 				for (i = 0; i < acells + icells; i++) {
704*94d3b452SApple OSS Distributions 					if (i == acells) {
705*94d3b452SApple OSS Distributions 						kprintf("- ");
706*94d3b452SApple OSS Distributions 					}
707*94d3b452SApple OSS Distributions 					kprintf("0x%08X ", map[i]);
708*94d3b452SApple OSS Distributions 				}
709*94d3b452SApple OSS Distributions 				kprintf("\n");
710*94d3b452SApple OSS Distributions #endif
711*94d3b452SApple OSS Distributions 				for (i = 0, cmp = true; cmp && (i < (acells + icells)); i++) {
712*94d3b452SApple OSS Distributions 					cell = (i < acells) ? addrCmp[i] : intSpec[i - acells];
713*94d3b452SApple OSS Distributions 					if (maskCmp) {
714*94d3b452SApple OSS Distributions 						cell &= maskCmp[i];
715*94d3b452SApple OSS Distributions 					}
716*94d3b452SApple OSS Distributions 					cmp = (cell == map[i]);
717*94d3b452SApple OSS Distributions 				}
718*94d3b452SApple OSS Distributions 
719*94d3b452SApple OSS Distributions 				map += acells + icells;
720*94d3b452SApple OSS Distributions 				if (NULL == (parent = FindPHandle( *(map++)))) {
721*94d3b452SApple OSS Distributions 					unexpected(break);
722*94d3b452SApple OSS Distributions 				}
723*94d3b452SApple OSS Distributions 
724*94d3b452SApple OSS Distributions 				IODTGetICellCounts( parent, &picells, &pacells );
725*94d3b452SApple OSS Distributions 				if (cmp) {
726*94d3b452SApple OSS Distributions 					addrCmp = map;
727*94d3b452SApple OSS Distributions 					intSpec = map + pacells;
728*94d3b452SApple OSS Distributions 					regEntry = parent;
729*94d3b452SApple OSS Distributions 				} else {
730*94d3b452SApple OSS Distributions 					map += pacells + picells;
731*94d3b452SApple OSS Distributions 				}
732*94d3b452SApple OSS Distributions 			} while (!cmp && (map < endMap));
733*94d3b452SApple OSS Distributions 			if (!cmp) {
734*94d3b452SApple OSS Distributions 				parent = NULL;
735*94d3b452SApple OSS Distributions 			}
736*94d3b452SApple OSS Distributions 		}
737*94d3b452SApple OSS Distributions 
738*94d3b452SApple OSS Distributions 		if (parent) {
739*94d3b452SApple OSS Distributions 			IODTGetICellCounts( parent, &icells, &acells );
740*94d3b452SApple OSS Distributions 			regEntry = parent;
741*94d3b452SApple OSS Distributions 		}
742*94d3b452SApple OSS Distributions 	} while (parent);
743*94d3b452SApple OSS Distributions 
744*94d3b452SApple OSS Distributions 	return ok ? original_icells : 0;
745*94d3b452SApple OSS Distributions }
746*94d3b452SApple OSS Distributions 
747*94d3b452SApple OSS Distributions IOReturn
IODTGetInterruptOptions(IORegistryEntry * regEntry,int source,IOOptionBits * options)748*94d3b452SApple OSS Distributions IODTGetInterruptOptions( IORegistryEntry * regEntry, int source, IOOptionBits * options )
749*94d3b452SApple OSS Distributions {
750*94d3b452SApple OSS Distributions 	OSArray *   controllers;
751*94d3b452SApple OSS Distributions 	OSArray *   specifiers;
752*94d3b452SApple OSS Distributions 	OSArray *   shared;
753*94d3b452SApple OSS Distributions 	OSObject *  spec;
754*94d3b452SApple OSS Distributions 	OSObject *  oneSpec;
755*94d3b452SApple OSS Distributions 
756*94d3b452SApple OSS Distributions 	*options = 0;
757*94d3b452SApple OSS Distributions 
758*94d3b452SApple OSS Distributions 	controllers = OSDynamicCast(OSArray, regEntry->getProperty(gIOInterruptControllersKey));
759*94d3b452SApple OSS Distributions 	specifiers  = OSDynamicCast(OSArray, regEntry->getProperty(gIOInterruptSpecifiersKey));
760*94d3b452SApple OSS Distributions 
761*94d3b452SApple OSS Distributions 	if (!controllers || !specifiers) {
762*94d3b452SApple OSS Distributions 		return kIOReturnNoInterrupt;
763*94d3b452SApple OSS Distributions 	}
764*94d3b452SApple OSS Distributions 
765*94d3b452SApple OSS Distributions 	shared = (OSArray *) gIODTSharedInterrupts->getObject(
766*94d3b452SApple OSS Distributions 		(const OSSymbol *) controllers->getObject(source));
767*94d3b452SApple OSS Distributions 	if (!shared) {
768*94d3b452SApple OSS Distributions 		return kIOReturnSuccess;
769*94d3b452SApple OSS Distributions 	}
770*94d3b452SApple OSS Distributions 
771*94d3b452SApple OSS Distributions 	spec = specifiers->getObject(source);
772*94d3b452SApple OSS Distributions 	if (!spec) {
773*94d3b452SApple OSS Distributions 		return kIOReturnNoInterrupt;
774*94d3b452SApple OSS Distributions 	}
775*94d3b452SApple OSS Distributions 
776*94d3b452SApple OSS Distributions 	for (unsigned int i = 0;
777*94d3b452SApple OSS Distributions 	    (oneSpec = shared->getObject(i))
778*94d3b452SApple OSS Distributions 	    && (!oneSpec->isEqualTo(spec));
779*94d3b452SApple OSS Distributions 	    i++) {
780*94d3b452SApple OSS Distributions 	}
781*94d3b452SApple OSS Distributions 
782*94d3b452SApple OSS Distributions 	if (oneSpec) {
783*94d3b452SApple OSS Distributions 		*options = kIODTInterruptShared;
784*94d3b452SApple OSS Distributions 	}
785*94d3b452SApple OSS Distributions 
786*94d3b452SApple OSS Distributions 	return kIOReturnSuccess;
787*94d3b452SApple OSS Distributions }
788*94d3b452SApple OSS Distributions 
789*94d3b452SApple OSS Distributions static bool
IODTMapInterruptsSharing(IORegistryEntry * regEntry,OSDictionary * allInts)790*94d3b452SApple OSS Distributions IODTMapInterruptsSharing( IORegistryEntry * regEntry, OSDictionary * allInts )
791*94d3b452SApple OSS Distributions {
792*94d3b452SApple OSS Distributions 	IORegistryEntry *   parent;
793*94d3b452SApple OSS Distributions 	OSData *            local;
794*94d3b452SApple OSS Distributions 	OSData *            local2;
795*94d3b452SApple OSS Distributions 	UInt32 *            localBits;
796*94d3b452SApple OSS Distributions 	UInt32 *            localEnd;
797*94d3b452SApple OSS Distributions 	IOItemCount         index;
798*94d3b452SApple OSS Distributions 	OSData *            map = NULL;
799*94d3b452SApple OSS Distributions 	OSObject *          oneMap;
800*94d3b452SApple OSS Distributions 	OSArray *           mapped;
801*94d3b452SApple OSS Distributions 	OSArray *           controllerInts;
802*94d3b452SApple OSS Distributions 	const OSSymbol *    controller = NULL;
803*94d3b452SApple OSS Distributions 	OSArray *           controllers;
804*94d3b452SApple OSS Distributions 	UInt32              skip = 1;
805*94d3b452SApple OSS Distributions 	bool                ok, nw;
806*94d3b452SApple OSS Distributions 
807*94d3b452SApple OSS Distributions 	nw = (NULL == (local = OSDynamicCast( OSData,
808*94d3b452SApple OSS Distributions 	    regEntry->getProperty( gIODTAAPLInterruptsKey))));
809*94d3b452SApple OSS Distributions 	if (nw && (NULL == (local = OSDynamicCast( OSData,
810*94d3b452SApple OSS Distributions 	    regEntry->getProperty( "interrupts"))))) {
811*94d3b452SApple OSS Distributions 		return true;  // nothing to see here
812*94d3b452SApple OSS Distributions 	}
813*94d3b452SApple OSS Distributions 	if (nw && (parent = regEntry->getParentEntry( gIODTPlane))) {
814*94d3b452SApple OSS Distributions 		// check for bridges on old world
815*94d3b452SApple OSS Distributions 		if ((local2 = OSDynamicCast( OSData,
816*94d3b452SApple OSS Distributions 		    parent->getProperty( gIODTAAPLInterruptsKey)))) {
817*94d3b452SApple OSS Distributions 			local = local2;
818*94d3b452SApple OSS Distributions 			nw = false;
819*94d3b452SApple OSS Distributions 		}
820*94d3b452SApple OSS Distributions 	}
821*94d3b452SApple OSS Distributions 
822*94d3b452SApple OSS Distributions 	localBits = (UInt32 *) local->getBytesNoCopy();
823*94d3b452SApple OSS Distributions 	localEnd = localBits + (local->getLength() / sizeof(UInt32));
824*94d3b452SApple OSS Distributions 	index = 0;
825*94d3b452SApple OSS Distributions 	mapped = OSArray::withCapacity( 1 );
826*94d3b452SApple OSS Distributions 	controllers = OSArray::withCapacity( 1 );
827*94d3b452SApple OSS Distributions 
828*94d3b452SApple OSS Distributions 	ok = (mapped && controllers);
829*94d3b452SApple OSS Distributions 
830*94d3b452SApple OSS Distributions 	if (ok) {
831*94d3b452SApple OSS Distributions 		do {
832*94d3b452SApple OSS Distributions 			if (nw) {
833*94d3b452SApple OSS Distributions 				skip = IODTMapOneInterrupt( regEntry, localBits, index, &map, &controller );
834*94d3b452SApple OSS Distributions 				if (0 == skip) {
835*94d3b452SApple OSS Distributions 					IOLog("%s: error mapping interrupt[%d]\n",
836*94d3b452SApple OSS Distributions 					    regEntry->getName(), mapped->getCount());
837*94d3b452SApple OSS Distributions 					OSSafeReleaseNULL(map);
838*94d3b452SApple OSS Distributions 					OSSafeReleaseNULL(controller);
839*94d3b452SApple OSS Distributions 					break;
840*94d3b452SApple OSS Distributions 				}
841*94d3b452SApple OSS Distributions 			} else {
842*94d3b452SApple OSS Distributions 				map = OSData::withData( local, mapped->getCount() * sizeof(UInt32),
843*94d3b452SApple OSS Distributions 				    sizeof(UInt32));
844*94d3b452SApple OSS Distributions 				controller = gIODTDefaultInterruptController;
845*94d3b452SApple OSS Distributions 				controller->retain();
846*94d3b452SApple OSS Distributions 			}
847*94d3b452SApple OSS Distributions 
848*94d3b452SApple OSS Distributions 			index++;
849*94d3b452SApple OSS Distributions 			localBits += skip;
850*94d3b452SApple OSS Distributions 			mapped->setObject( map );
851*94d3b452SApple OSS Distributions 			controllers->setObject( controller );
852*94d3b452SApple OSS Distributions 
853*94d3b452SApple OSS Distributions 			if (allInts) {
854*94d3b452SApple OSS Distributions 				controllerInts = (OSArray *) allInts->getObject( controller );
855*94d3b452SApple OSS Distributions 				if (controllerInts) {
856*94d3b452SApple OSS Distributions 					for (unsigned int i = 0; (oneMap = controllerInts->getObject(i)); i++) {
857*94d3b452SApple OSS Distributions 						if (map->isEqualTo(oneMap)) {
858*94d3b452SApple OSS Distributions 							controllerInts = (OSArray *) gIODTSharedInterrupts->getObject( controller );
859*94d3b452SApple OSS Distributions 							if (controllerInts) {
860*94d3b452SApple OSS Distributions 								controllerInts->setObject(map);
861*94d3b452SApple OSS Distributions 							} else {
862*94d3b452SApple OSS Distributions 								controllerInts = OSArray::withObjects((const OSObject **) &map, 1, 4 );
863*94d3b452SApple OSS Distributions 								if (controllerInts) {
864*94d3b452SApple OSS Distributions 									gIODTSharedInterrupts->setObject( controller, controllerInts );
865*94d3b452SApple OSS Distributions 									controllerInts->release();
866*94d3b452SApple OSS Distributions 								}
867*94d3b452SApple OSS Distributions 							}
868*94d3b452SApple OSS Distributions 							break;
869*94d3b452SApple OSS Distributions 						}
870*94d3b452SApple OSS Distributions 					}
871*94d3b452SApple OSS Distributions 					if (!oneMap) {
872*94d3b452SApple OSS Distributions 						controllerInts->setObject(map);
873*94d3b452SApple OSS Distributions 					}
874*94d3b452SApple OSS Distributions 				} else {
875*94d3b452SApple OSS Distributions 					controllerInts = OSArray::withObjects((const OSObject **) &map, 1, 16 );
876*94d3b452SApple OSS Distributions 					if (controllerInts) {
877*94d3b452SApple OSS Distributions 						allInts->setObject( controller, controllerInts );
878*94d3b452SApple OSS Distributions 						controllerInts->release();
879*94d3b452SApple OSS Distributions 					}
880*94d3b452SApple OSS Distributions 				}
881*94d3b452SApple OSS Distributions 			}
882*94d3b452SApple OSS Distributions 
883*94d3b452SApple OSS Distributions 			OSSafeReleaseNULL(map);
884*94d3b452SApple OSS Distributions 			OSSafeReleaseNULL(controller);
885*94d3b452SApple OSS Distributions 		} while (localBits < localEnd);
886*94d3b452SApple OSS Distributions 	}
887*94d3b452SApple OSS Distributions 
888*94d3b452SApple OSS Distributions 	ok &= (localBits == localEnd);
889*94d3b452SApple OSS Distributions 
890*94d3b452SApple OSS Distributions 	if (ok) {
891*94d3b452SApple OSS Distributions 		// store results
892*94d3b452SApple OSS Distributions 		ok  = regEntry->setProperty( gIOInterruptControllersKey, controllers);
893*94d3b452SApple OSS Distributions 		ok &= regEntry->setProperty( gIOInterruptSpecifiersKey, mapped);
894*94d3b452SApple OSS Distributions 	}
895*94d3b452SApple OSS Distributions 
896*94d3b452SApple OSS Distributions 	if (controllers) {
897*94d3b452SApple OSS Distributions 		controllers->release();
898*94d3b452SApple OSS Distributions 	}
899*94d3b452SApple OSS Distributions 	if (mapped) {
900*94d3b452SApple OSS Distributions 		mapped->release();
901*94d3b452SApple OSS Distributions 	}
902*94d3b452SApple OSS Distributions 
903*94d3b452SApple OSS Distributions 	return ok;
904*94d3b452SApple OSS Distributions }
905*94d3b452SApple OSS Distributions 
906*94d3b452SApple OSS Distributions bool
IODTMapInterrupts(IORegistryEntry * regEntry)907*94d3b452SApple OSS Distributions IODTMapInterrupts( IORegistryEntry * regEntry )
908*94d3b452SApple OSS Distributions {
909*94d3b452SApple OSS Distributions 	return IODTMapInterruptsSharing( regEntry, NULL );
910*94d3b452SApple OSS Distributions }
911*94d3b452SApple OSS Distributions 
912*94d3b452SApple OSS Distributions /*
913*94d3b452SApple OSS Distributions  */
914*94d3b452SApple OSS Distributions 
915*94d3b452SApple OSS Distributions static bool
CompareKey(OSString * key,const IORegistryEntry * table,const OSSymbol * propName,LIBKERN_RETURNS_RETAINED OSString ** matchingName)916*94d3b452SApple OSS Distributions CompareKey( OSString * key,
917*94d3b452SApple OSS Distributions     const IORegistryEntry * table, const OSSymbol * propName,
918*94d3b452SApple OSS Distributions     LIBKERN_RETURNS_RETAINED OSString ** matchingName )
919*94d3b452SApple OSS Distributions {
920*94d3b452SApple OSS Distributions 	OSObject            *prop;
921*94d3b452SApple OSS Distributions 	OSData                      *data;
922*94d3b452SApple OSS Distributions 	OSString            *string;
923*94d3b452SApple OSS Distributions 	const char          *ckey;
924*94d3b452SApple OSS Distributions 	UInt32                      keyLen;
925*94d3b452SApple OSS Distributions 	UInt32          nlen;
926*94d3b452SApple OSS Distributions 	const char          *names;
927*94d3b452SApple OSS Distributions 	const char          *lastName;
928*94d3b452SApple OSS Distributions 	bool                        wild;
929*94d3b452SApple OSS Distributions 	bool                        matched;
930*94d3b452SApple OSS Distributions 	const char          *result = NULL;
931*94d3b452SApple OSS Distributions 
932*94d3b452SApple OSS Distributions 	if (NULL == (prop = table->copyProperty( propName ))) {
933*94d3b452SApple OSS Distributions 		return 0;
934*94d3b452SApple OSS Distributions 	}
935*94d3b452SApple OSS Distributions 
936*94d3b452SApple OSS Distributions 	if ((data = OSDynamicCast( OSData, prop ))) {
937*94d3b452SApple OSS Distributions 		names = (const char *) data->getBytesNoCopy();
938*94d3b452SApple OSS Distributions 		lastName = names + data->getLength();
939*94d3b452SApple OSS Distributions 	} else if ((string = OSDynamicCast( OSString, prop ))) {
940*94d3b452SApple OSS Distributions 		names = string->getCStringNoCopy();
941*94d3b452SApple OSS Distributions 		lastName = names + string->getLength() + 1;
942*94d3b452SApple OSS Distributions 	} else {
943*94d3b452SApple OSS Distributions 		names = NULL;
944*94d3b452SApple OSS Distributions 	}
945*94d3b452SApple OSS Distributions 
946*94d3b452SApple OSS Distributions 	if (names) {
947*94d3b452SApple OSS Distributions 		ckey = key->getCStringNoCopy();
948*94d3b452SApple OSS Distributions 		keyLen = key->getLength();
949*94d3b452SApple OSS Distributions 		wild = ('*' == key->getChar( keyLen - 1 ));
950*94d3b452SApple OSS Distributions 
951*94d3b452SApple OSS Distributions 		do {
952*94d3b452SApple OSS Distributions 			// for each name in the property
953*94d3b452SApple OSS Distributions 			nlen = (unsigned int) strnlen(names, lastName - names);
954*94d3b452SApple OSS Distributions 			if (wild) {
955*94d3b452SApple OSS Distributions 				matched = ((nlen >= (keyLen - 1)) && (0 == strncmp(ckey, names, keyLen - 1)));
956*94d3b452SApple OSS Distributions 			} else {
957*94d3b452SApple OSS Distributions 				matched = (keyLen == nlen) && (0 == strncmp(ckey, names, keyLen));
958*94d3b452SApple OSS Distributions 			}
959*94d3b452SApple OSS Distributions 
960*94d3b452SApple OSS Distributions 			if (matched) {
961*94d3b452SApple OSS Distributions 				result = names;
962*94d3b452SApple OSS Distributions 			}
963*94d3b452SApple OSS Distributions 
964*94d3b452SApple OSS Distributions 			names = names + nlen + 1;
965*94d3b452SApple OSS Distributions 		} while ((names < lastName) && (false == matched));
966*94d3b452SApple OSS Distributions 	}
967*94d3b452SApple OSS Distributions 
968*94d3b452SApple OSS Distributions 	if (result && matchingName) {
969*94d3b452SApple OSS Distributions 		*matchingName = OSString::withCString( result );
970*94d3b452SApple OSS Distributions 	}
971*94d3b452SApple OSS Distributions 
972*94d3b452SApple OSS Distributions 	if (prop) {
973*94d3b452SApple OSS Distributions 		prop->release();
974*94d3b452SApple OSS Distributions 	}
975*94d3b452SApple OSS Distributions 
976*94d3b452SApple OSS Distributions 	return result != NULL;
977*94d3b452SApple OSS Distributions }
978*94d3b452SApple OSS Distributions 
979*94d3b452SApple OSS Distributions 
980*94d3b452SApple OSS Distributions bool
IODTCompareNubName(const IORegistryEntry * regEntry,OSString * name,OSString ** matchingName)981*94d3b452SApple OSS Distributions IODTCompareNubName( const IORegistryEntry * regEntry,
982*94d3b452SApple OSS Distributions     OSString * name, OSString ** matchingName )
983*94d3b452SApple OSS Distributions {
984*94d3b452SApple OSS Distributions 	bool matched;
985*94d3b452SApple OSS Distributions 
986*94d3b452SApple OSS Distributions 	matched = CompareKey( name, regEntry, gIODTNameKey, matchingName)
987*94d3b452SApple OSS Distributions 	    || CompareKey( name, regEntry, gIODTCompatibleKey, matchingName)
988*94d3b452SApple OSS Distributions 	    || CompareKey( name, regEntry, gIODTTypeKey, matchingName)
989*94d3b452SApple OSS Distributions 	    || CompareKey( name, regEntry, gIODTModelKey, matchingName);
990*94d3b452SApple OSS Distributions 
991*94d3b452SApple OSS Distributions 	return matched;
992*94d3b452SApple OSS Distributions }
993*94d3b452SApple OSS Distributions 
994*94d3b452SApple OSS Distributions bool
IODTCompareNubName(const IORegistryEntry * regEntry,OSString * name,OSSharedPtr<OSString> & matchingName)995*94d3b452SApple OSS Distributions IODTCompareNubName( const IORegistryEntry * regEntry,
996*94d3b452SApple OSS Distributions     OSString * name, OSSharedPtr<OSString>& matchingName )
997*94d3b452SApple OSS Distributions {
998*94d3b452SApple OSS Distributions 	OSString* matchingNameRaw = NULL;
999*94d3b452SApple OSS Distributions 	bool result = IODTCompareNubName(regEntry, name, &matchingNameRaw);
1000*94d3b452SApple OSS Distributions 	matchingName.reset(matchingNameRaw, OSNoRetain);
1001*94d3b452SApple OSS Distributions 	return result;
1002*94d3b452SApple OSS Distributions }
1003*94d3b452SApple OSS Distributions 
1004*94d3b452SApple OSS Distributions bool
IODTMatchNubWithKeys(IORegistryEntry * regEntry,const char * keys)1005*94d3b452SApple OSS Distributions IODTMatchNubWithKeys( IORegistryEntry * regEntry,
1006*94d3b452SApple OSS Distributions     const char * keys )
1007*94d3b452SApple OSS Distributions {
1008*94d3b452SApple OSS Distributions 	OSObject    *obj;
1009*94d3b452SApple OSS Distributions 	bool                result = false;
1010*94d3b452SApple OSS Distributions 
1011*94d3b452SApple OSS Distributions 	obj = OSUnserialize( keys, NULL );
1012*94d3b452SApple OSS Distributions 
1013*94d3b452SApple OSS Distributions 	if (obj) {
1014*94d3b452SApple OSS Distributions 		result = regEntry->compareNames( obj );
1015*94d3b452SApple OSS Distributions 		obj->release();
1016*94d3b452SApple OSS Distributions 	}
1017*94d3b452SApple OSS Distributions #if DEBUG
1018*94d3b452SApple OSS Distributions 	else {
1019*94d3b452SApple OSS Distributions 		IOLog("Couldn't unserialize %s\n", keys );
1020*94d3b452SApple OSS Distributions 	}
1021*94d3b452SApple OSS Distributions #endif
1022*94d3b452SApple OSS Distributions 
1023*94d3b452SApple OSS Distributions 	return result;
1024*94d3b452SApple OSS Distributions }
1025*94d3b452SApple OSS Distributions 
1026*94d3b452SApple OSS Distributions LIBKERN_RETURNS_RETAINED OSCollectionIterator *
IODTFindMatchingEntries(IORegistryEntry * from,IOOptionBits options,const char * keys)1027*94d3b452SApple OSS Distributions IODTFindMatchingEntries( IORegistryEntry * from,
1028*94d3b452SApple OSS Distributions     IOOptionBits options, const char * keys )
1029*94d3b452SApple OSS Distributions {
1030*94d3b452SApple OSS Distributions 	OSSet                                       *result = NULL;
1031*94d3b452SApple OSS Distributions 	IORegistryEntry                     *next;
1032*94d3b452SApple OSS Distributions 	IORegistryIterator          *iter;
1033*94d3b452SApple OSS Distributions 	OSCollectionIterator        *cIter;
1034*94d3b452SApple OSS Distributions 	bool                                        cmp;
1035*94d3b452SApple OSS Distributions 	bool                                        minus = options & kIODTExclusive;
1036*94d3b452SApple OSS Distributions 
1037*94d3b452SApple OSS Distributions 
1038*94d3b452SApple OSS Distributions 	iter = IORegistryIterator::iterateOver( from, gIODTPlane,
1039*94d3b452SApple OSS Distributions 	    (options & kIODTRecursive) ? kIORegistryIterateRecursively : 0 );
1040*94d3b452SApple OSS Distributions 	if (iter) {
1041*94d3b452SApple OSS Distributions 		do {
1042*94d3b452SApple OSS Distributions 			if (result) {
1043*94d3b452SApple OSS Distributions 				result->release();
1044*94d3b452SApple OSS Distributions 			}
1045*94d3b452SApple OSS Distributions 			result = OSSet::withCapacity( 3 );
1046*94d3b452SApple OSS Distributions 			if (!result) {
1047*94d3b452SApple OSS Distributions 				break;
1048*94d3b452SApple OSS Distributions 			}
1049*94d3b452SApple OSS Distributions 
1050*94d3b452SApple OSS Distributions 			iter->reset();
1051*94d3b452SApple OSS Distributions 			while ((next = iter->getNextObject())) {
1052*94d3b452SApple OSS Distributions 				// Look for existence of a debug property to skip
1053*94d3b452SApple OSS Distributions 				if (next->propertyExists("AAPL,ignore")) {
1054*94d3b452SApple OSS Distributions 					continue;
1055*94d3b452SApple OSS Distributions 				}
1056*94d3b452SApple OSS Distributions 				if (next->propertyHasValue(gIODTTypeKey, gIODTAssociatedServiceKey)) {
1057*94d3b452SApple OSS Distributions 					continue;
1058*94d3b452SApple OSS Distributions 				}
1059*94d3b452SApple OSS Distributions 				if (keys) {
1060*94d3b452SApple OSS Distributions 					cmp = IODTMatchNubWithKeys( next, keys );
1061*94d3b452SApple OSS Distributions 					if ((minus && (false == cmp))
1062*94d3b452SApple OSS Distributions 					    || ((false == minus) && (false != cmp))) {
1063*94d3b452SApple OSS Distributions 						result->setObject( next);
1064*94d3b452SApple OSS Distributions 					}
1065*94d3b452SApple OSS Distributions 				} else {
1066*94d3b452SApple OSS Distributions 					result->setObject( next);
1067*94d3b452SApple OSS Distributions 				}
1068*94d3b452SApple OSS Distributions 			}
1069*94d3b452SApple OSS Distributions 		} while (!iter->isValid());
1070*94d3b452SApple OSS Distributions 
1071*94d3b452SApple OSS Distributions 		iter->release();
1072*94d3b452SApple OSS Distributions 	}
1073*94d3b452SApple OSS Distributions 
1074*94d3b452SApple OSS Distributions 	cIter = OSCollectionIterator::withCollection( result);
1075*94d3b452SApple OSS Distributions 	if (result) {
1076*94d3b452SApple OSS Distributions 		result->release();
1077*94d3b452SApple OSS Distributions 	}
1078*94d3b452SApple OSS Distributions 
1079*94d3b452SApple OSS Distributions 	return cIter;
1080*94d3b452SApple OSS Distributions }
1081*94d3b452SApple OSS Distributions 
1082*94d3b452SApple OSS Distributions 
1083*94d3b452SApple OSS Distributions void
IODTSetResolving(IORegistryEntry * regEntry,IODTCompareAddressCellFunc compareFunc,IODTNVLocationFunc locationFunc __unused)1084*94d3b452SApple OSS Distributions IODTSetResolving( IORegistryEntry *        regEntry,
1085*94d3b452SApple OSS Distributions     IODTCompareAddressCellFunc      compareFunc,
1086*94d3b452SApple OSS Distributions     IODTNVLocationFunc              locationFunc __unused )
1087*94d3b452SApple OSS Distributions {
1088*94d3b452SApple OSS Distributions 	IODTPersistent * entry;
1089*94d3b452SApple OSS Distributions 	IODTPersistent * newResolvers;
1090*94d3b452SApple OSS Distributions 	OSNumber       * num;
1091*94d3b452SApple OSS Distributions 	unsigned int     index;
1092*94d3b452SApple OSS Distributions 
1093*94d3b452SApple OSS Distributions 	IOLockLock(gIODTResolvers->lock);
1094*94d3b452SApple OSS Distributions 
1095*94d3b452SApple OSS Distributions 	entry = gIODTResolvers->resolvers;
1096*94d3b452SApple OSS Distributions 	for (index = 0; index < gIODTResolvers->count; index++) {
1097*94d3b452SApple OSS Distributions 		if (compareFunc == entry->compareFunc) {
1098*94d3b452SApple OSS Distributions 			break;
1099*94d3b452SApple OSS Distributions 		}
1100*94d3b452SApple OSS Distributions 		entry++;
1101*94d3b452SApple OSS Distributions 	}
1102*94d3b452SApple OSS Distributions 
1103*94d3b452SApple OSS Distributions 	if (index == gIODTResolvers->count) {
1104*94d3b452SApple OSS Distributions 		if (gIODTResolvers->alloc == gIODTResolvers->count) {
1105*94d3b452SApple OSS Distributions 			if (__improbable(os_mul_overflow(gIODTResolvers->alloc, 2,
1106*94d3b452SApple OSS Distributions 			    &gIODTResolvers->alloc))) {
1107*94d3b452SApple OSS Distributions 				panic("IODTSetResolving - gIODTResolvers alloc overflows");
1108*94d3b452SApple OSS Distributions 			}
1109*94d3b452SApple OSS Distributions 
1110*94d3b452SApple OSS Distributions 			newResolvers = IONewZero(IODTPersistent, gIODTResolvers->alloc);
1111*94d3b452SApple OSS Distributions 			if (__improbable(!newResolvers)) {
1112*94d3b452SApple OSS Distributions 				panic("IODTSetResolving - could not allocate new resolvers");
1113*94d3b452SApple OSS Distributions 			}
1114*94d3b452SApple OSS Distributions 
1115*94d3b452SApple OSS Distributions 			bcopy(gIODTResolvers->resolvers, newResolvers,
1116*94d3b452SApple OSS Distributions 			    sizeof(gIODTResolvers->resolvers[0]) * gIODTResolvers->count);
1117*94d3b452SApple OSS Distributions 
1118*94d3b452SApple OSS Distributions 			IODelete(gIODTResolvers->resolvers, IODTPersistent,
1119*94d3b452SApple OSS Distributions 			    gIODTResolvers->count);
1120*94d3b452SApple OSS Distributions 			gIODTResolvers->resolvers = newResolvers;
1121*94d3b452SApple OSS Distributions 		}
1122*94d3b452SApple OSS Distributions 
1123*94d3b452SApple OSS Distributions 		entry = &gIODTResolvers->resolvers[gIODTResolvers->count];
1124*94d3b452SApple OSS Distributions 		entry->compareFunc = compareFunc;
1125*94d3b452SApple OSS Distributions 		gIODTResolvers->count++;
1126*94d3b452SApple OSS Distributions 	}
1127*94d3b452SApple OSS Distributions 
1128*94d3b452SApple OSS Distributions 	IOLockUnlock(gIODTResolvers->lock);
1129*94d3b452SApple OSS Distributions 
1130*94d3b452SApple OSS Distributions 	num = OSNumber::withNumber(index, 32);
1131*94d3b452SApple OSS Distributions 	regEntry->setProperty(gIODTPersistKey, num);
1132*94d3b452SApple OSS Distributions 	OSSafeReleaseNULL(num);
1133*94d3b452SApple OSS Distributions 
1134*94d3b452SApple OSS Distributions 	return;
1135*94d3b452SApple OSS Distributions }
1136*94d3b452SApple OSS Distributions 
1137*94d3b452SApple OSS Distributions #if  defined(__arm64__)
1138*94d3b452SApple OSS Distributions static SInt64
DefaultCompare(UInt32 cellCount,UInt32 left[],UInt32 right[])1139*94d3b452SApple OSS Distributions DefaultCompare( UInt32 cellCount, UInt32 left[], UInt32 right[] )
1140*94d3b452SApple OSS Distributions {
1141*94d3b452SApple OSS Distributions 	SInt64 diff = 0;
1142*94d3b452SApple OSS Distributions 
1143*94d3b452SApple OSS Distributions 	if (cellCount == 2) {
1144*94d3b452SApple OSS Distributions 		diff = IOPhysical32(left[1], left[0]) - IOPhysical32(right[1], right[0]);
1145*94d3b452SApple OSS Distributions 	} else if (cellCount == 1) {
1146*94d3b452SApple OSS Distributions 		diff = (left[0] - right[0]);
1147*94d3b452SApple OSS Distributions 	} else {
1148*94d3b452SApple OSS Distributions 		panic("DefaultCompare only knows how to handle 1 or 2 cells.");
1149*94d3b452SApple OSS Distributions 	}
1150*94d3b452SApple OSS Distributions 
1151*94d3b452SApple OSS Distributions 	return diff;
1152*94d3b452SApple OSS Distributions }
1153*94d3b452SApple OSS Distributions #elif defined(__i386__) || defined(__x86_64__)
1154*94d3b452SApple OSS Distributions static SInt32
DefaultCompare(UInt32 cellCount,UInt32 left[],UInt32 right[])1155*94d3b452SApple OSS Distributions DefaultCompare( UInt32 cellCount, UInt32 left[], UInt32 right[] )
1156*94d3b452SApple OSS Distributions {
1157*94d3b452SApple OSS Distributions 	cellCount--;
1158*94d3b452SApple OSS Distributions 	return left[cellCount] - right[cellCount];
1159*94d3b452SApple OSS Distributions }
1160*94d3b452SApple OSS Distributions #else
1161*94d3b452SApple OSS Distributions #error Unknown architecture.
1162*94d3b452SApple OSS Distributions #endif
1163*94d3b452SApple OSS Distributions 
1164*94d3b452SApple OSS Distributions static void
AddLengthToCells(UInt32 numCells,UInt32 * cells,UInt64 offset)1165*94d3b452SApple OSS Distributions AddLengthToCells( UInt32 numCells, UInt32 *cells, UInt64 offset)
1166*94d3b452SApple OSS Distributions {
1167*94d3b452SApple OSS Distributions 	if (numCells == 1) {
1168*94d3b452SApple OSS Distributions 		cells[0] += (UInt32)offset;
1169*94d3b452SApple OSS Distributions 	} else {
1170*94d3b452SApple OSS Distributions #if defined(__arm64__)
1171*94d3b452SApple OSS Distributions 		UInt64 sum = cells[numCells - 2] + offset;
1172*94d3b452SApple OSS Distributions 		cells[numCells - 2] = (UInt32)sum;
1173*94d3b452SApple OSS Distributions 		if (sum > UINT32_MAX) {
1174*94d3b452SApple OSS Distributions 			cells[numCells - 1] += (UInt32)(sum >> 32);
1175*94d3b452SApple OSS Distributions 		}
1176*94d3b452SApple OSS Distributions #else
1177*94d3b452SApple OSS Distributions 		UInt64 sum = cells[numCells - 1] + offset;
1178*94d3b452SApple OSS Distributions 		cells[numCells - 1] = (UInt32)sum;
1179*94d3b452SApple OSS Distributions 		if (sum > UINT32_MAX) {
1180*94d3b452SApple OSS Distributions 			cells[numCells - 2] += (UInt32)(sum >> 32);
1181*94d3b452SApple OSS Distributions 		}
1182*94d3b452SApple OSS Distributions #endif
1183*94d3b452SApple OSS Distributions 	}
1184*94d3b452SApple OSS Distributions }
1185*94d3b452SApple OSS Distributions 
1186*94d3b452SApple OSS Distributions static IOPhysicalAddress
CellsValue(UInt32 numCells,UInt32 * cells)1187*94d3b452SApple OSS Distributions CellsValue( UInt32 numCells, UInt32 *cells)
1188*94d3b452SApple OSS Distributions {
1189*94d3b452SApple OSS Distributions 	if (numCells == 1) {
1190*94d3b452SApple OSS Distributions 		return IOPhysical32( 0, cells[0] );
1191*94d3b452SApple OSS Distributions 	} else {
1192*94d3b452SApple OSS Distributions #if defined(__arm64__) || defined(arm)
1193*94d3b452SApple OSS Distributions 		return IOPhysical32( cells[numCells - 1], cells[numCells - 2] );
1194*94d3b452SApple OSS Distributions #else
1195*94d3b452SApple OSS Distributions 		return IOPhysical32( cells[numCells - 2], cells[numCells - 1] );
1196*94d3b452SApple OSS Distributions #endif
1197*94d3b452SApple OSS Distributions 	}
1198*94d3b452SApple OSS Distributions }
1199*94d3b452SApple OSS Distributions 
1200*94d3b452SApple OSS Distributions void
IODTGetCellCounts(IORegistryEntry * regEntry,UInt32 * sizeCount,UInt32 * addressCount)1201*94d3b452SApple OSS Distributions IODTGetCellCounts( IORegistryEntry * regEntry,
1202*94d3b452SApple OSS Distributions     UInt32 * sizeCount, UInt32 * addressCount)
1203*94d3b452SApple OSS Distributions {
1204*94d3b452SApple OSS Distributions 	if (!GetUInt32( regEntry, gIODTSizeCellKey, sizeCount)) {
1205*94d3b452SApple OSS Distributions 		*sizeCount = 1;
1206*94d3b452SApple OSS Distributions 	}
1207*94d3b452SApple OSS Distributions 	if (!GetUInt32( regEntry, gIODTAddressCellKey, addressCount)) {
1208*94d3b452SApple OSS Distributions 		*addressCount = 2;
1209*94d3b452SApple OSS Distributions 	}
1210*94d3b452SApple OSS Distributions 	return;
1211*94d3b452SApple OSS Distributions }
1212*94d3b452SApple OSS Distributions 
1213*94d3b452SApple OSS Distributions // Given addr & len cells from our child, find it in our ranges property, then
1214*94d3b452SApple OSS Distributions // look in our parent to resolve the base of the range for us.
1215*94d3b452SApple OSS Distributions 
1216*94d3b452SApple OSS Distributions // Range[]: child-addr  our-addr  child-len
1217*94d3b452SApple OSS Distributions // #cells:    child       ours     child
1218*94d3b452SApple OSS Distributions 
1219*94d3b452SApple OSS Distributions bool
IODTResolveAddressCell(IORegistryEntry * startEntry,UInt32 cellsIn[],IOPhysicalAddress * phys,IOPhysicalLength * lenOut)1220*94d3b452SApple OSS Distributions IODTResolveAddressCell( IORegistryEntry * startEntry,
1221*94d3b452SApple OSS Distributions     UInt32 cellsIn[],
1222*94d3b452SApple OSS Distributions     IOPhysicalAddress * phys, IOPhysicalLength * lenOut )
1223*94d3b452SApple OSS Distributions {
1224*94d3b452SApple OSS Distributions 	IORegistryEntry     * parent = NULL;
1225*94d3b452SApple OSS Distributions 	IORegistryEntry * regEntry;
1226*94d3b452SApple OSS Distributions 	OSData          * prop;
1227*94d3b452SApple OSS Distributions 	OSNumber    * num;
1228*94d3b452SApple OSS Distributions 	unsigned int  index;
1229*94d3b452SApple OSS Distributions 	// cells in addresses at regEntry
1230*94d3b452SApple OSS Distributions 	UInt32              sizeCells, addressCells;
1231*94d3b452SApple OSS Distributions 	// cells in addresses below regEntry
1232*94d3b452SApple OSS Distributions 	UInt32              childSizeCells, childAddressCells;
1233*94d3b452SApple OSS Distributions 	UInt32              childCells;
1234*94d3b452SApple OSS Distributions 	UInt32              cell[8], propLen;
1235*94d3b452SApple OSS Distributions 	UInt64              offset = 0;
1236*94d3b452SApple OSS Distributions 	UInt32              endCell[8];
1237*94d3b452SApple OSS Distributions 	UInt32              *range;
1238*94d3b452SApple OSS Distributions 	UInt32              *lookRange;
1239*94d3b452SApple OSS Distributions 	UInt32              *startRange;
1240*94d3b452SApple OSS Distributions 	UInt32              *endRanges;
1241*94d3b452SApple OSS Distributions 	bool                ok = true;
1242*94d3b452SApple OSS Distributions 	SInt64              diff, diff2, endDiff;
1243*94d3b452SApple OSS Distributions 	UInt64              len, rangeLen;
1244*94d3b452SApple OSS Distributions 
1245*94d3b452SApple OSS Distributions 	IODTCompareAddressCellFunc  compare;
1246*94d3b452SApple OSS Distributions 
1247*94d3b452SApple OSS Distributions 	regEntry = startEntry;
1248*94d3b452SApple OSS Distributions 	regEntry->retain();
1249*94d3b452SApple OSS Distributions 	IODTGetCellCounts( regEntry, &childSizeCells, &childAddressCells );
1250*94d3b452SApple OSS Distributions 	childCells = childAddressCells + childSizeCells;
1251*94d3b452SApple OSS Distributions 
1252*94d3b452SApple OSS Distributions 	if (childCells > sizeof(cell) / sizeof(cell[0])) {
1253*94d3b452SApple OSS Distributions 		panic("IODTResolveAddressCell: Invalid device tree (%u,%u)", (uint32_t)childAddressCells, (uint32_t)childSizeCells);
1254*94d3b452SApple OSS Distributions 	}
1255*94d3b452SApple OSS Distributions 
1256*94d3b452SApple OSS Distributions 	bcopy( cellsIn, cell, sizeof(UInt32) * childCells );
1257*94d3b452SApple OSS Distributions 	*lenOut = CellsValue( childSizeCells, cellsIn + childAddressCells );
1258*94d3b452SApple OSS Distributions 
1259*94d3b452SApple OSS Distributions 	do{
1260*94d3b452SApple OSS Distributions 		prop = OSDynamicCast( OSData, regEntry->getProperty( gIODTRangeKey ));
1261*94d3b452SApple OSS Distributions 		if (NULL == prop) {
1262*94d3b452SApple OSS Distributions 			/* end of the road */
1263*94d3b452SApple OSS Distributions 			*phys = CellsValue( childAddressCells, cell );
1264*94d3b452SApple OSS Distributions 			*phys += offset;
1265*94d3b452SApple OSS Distributions 			break;
1266*94d3b452SApple OSS Distributions 		}
1267*94d3b452SApple OSS Distributions 
1268*94d3b452SApple OSS Distributions 		parent = regEntry->copyParentEntry( gIODTPlane );
1269*94d3b452SApple OSS Distributions 		IODTGetCellCounts( parent, &sizeCells, &addressCells );
1270*94d3b452SApple OSS Distributions 
1271*94d3b452SApple OSS Distributions 		if ((propLen = prop->getLength())) {
1272*94d3b452SApple OSS Distributions 			// search
1273*94d3b452SApple OSS Distributions 			startRange = (UInt32 *) prop->getBytesNoCopy();
1274*94d3b452SApple OSS Distributions 			range = startRange;
1275*94d3b452SApple OSS Distributions 			endRanges = range + (propLen / sizeof(UInt32));
1276*94d3b452SApple OSS Distributions 
1277*94d3b452SApple OSS Distributions 			compare = NULL;
1278*94d3b452SApple OSS Distributions 			num = OSDynamicCast(OSNumber, regEntry->getProperty(gIODTPersistKey));
1279*94d3b452SApple OSS Distributions 			if (num) {
1280*94d3b452SApple OSS Distributions 				IOLockLock(gIODTResolvers->lock);
1281*94d3b452SApple OSS Distributions 				index = num->unsigned32BitValue();
1282*94d3b452SApple OSS Distributions 				if (index < gIODTResolvers->count) {
1283*94d3b452SApple OSS Distributions 					compare = gIODTResolvers->resolvers[index].compareFunc;
1284*94d3b452SApple OSS Distributions 				}
1285*94d3b452SApple OSS Distributions 				IOLockUnlock(gIODTResolvers->lock);
1286*94d3b452SApple OSS Distributions 			}
1287*94d3b452SApple OSS Distributions 
1288*94d3b452SApple OSS Distributions 			if (!compare && (addressCells == childAddressCells)) {
1289*94d3b452SApple OSS Distributions 				compare = DefaultCompare;
1290*94d3b452SApple OSS Distributions 			}
1291*94d3b452SApple OSS Distributions 			if (!compare) {
1292*94d3b452SApple OSS Distributions 				panic("There is no mixed comparison function yet...");
1293*94d3b452SApple OSS Distributions 			}
1294*94d3b452SApple OSS Distributions 
1295*94d3b452SApple OSS Distributions 			for (ok = false;
1296*94d3b452SApple OSS Distributions 			    range < endRanges;
1297*94d3b452SApple OSS Distributions 			    range += (childCells + addressCells)) {
1298*94d3b452SApple OSS Distributions 				// is cell start within range?
1299*94d3b452SApple OSS Distributions 				diff = (*compare)( childAddressCells, cell, range );
1300*94d3b452SApple OSS Distributions 
1301*94d3b452SApple OSS Distributions 				if (childAddressCells > sizeof(endCell) / sizeof(endCell[0])) {
1302*94d3b452SApple OSS Distributions 					panic("IODTResolveAddressCell: Invalid device tree (%u)", (uint32_t)childAddressCells);
1303*94d3b452SApple OSS Distributions 				}
1304*94d3b452SApple OSS Distributions 
1305*94d3b452SApple OSS Distributions 				bcopy(range, endCell, childAddressCells * sizeof(UInt32));
1306*94d3b452SApple OSS Distributions 
1307*94d3b452SApple OSS Distributions 				rangeLen = CellsValue(childSizeCells, range + childAddressCells + addressCells);
1308*94d3b452SApple OSS Distributions 				AddLengthToCells(childAddressCells, endCell, rangeLen);
1309*94d3b452SApple OSS Distributions 
1310*94d3b452SApple OSS Distributions 				diff2 = (*compare)( childAddressCells, cell, endCell );
1311*94d3b452SApple OSS Distributions 
1312*94d3b452SApple OSS Distributions 				// if start of cell < start of range, or end of range >= start of cell, skip
1313*94d3b452SApple OSS Distributions 				if ((diff < 0) || (diff2 >= 0)) {
1314*94d3b452SApple OSS Distributions 					continue;
1315*94d3b452SApple OSS Distributions 				}
1316*94d3b452SApple OSS Distributions 
1317*94d3b452SApple OSS Distributions 				len = CellsValue(childSizeCells, cell + childAddressCells);
1318*94d3b452SApple OSS Distributions 				ok = (0 == len);
1319*94d3b452SApple OSS Distributions 
1320*94d3b452SApple OSS Distributions 				if (!ok) {
1321*94d3b452SApple OSS Distributions 					// search for cell end
1322*94d3b452SApple OSS Distributions 					bcopy(cell, endCell, childAddressCells * sizeof(UInt32));
1323*94d3b452SApple OSS Distributions 
1324*94d3b452SApple OSS Distributions 					AddLengthToCells(childAddressCells, endCell, len - 1);
1325*94d3b452SApple OSS Distributions 
1326*94d3b452SApple OSS Distributions 					for (lookRange = startRange;
1327*94d3b452SApple OSS Distributions 					    lookRange < endRanges;
1328*94d3b452SApple OSS Distributions 					    lookRange += (childCells + addressCells)) {
1329*94d3b452SApple OSS Distributions 						// make sure end of cell >= range start
1330*94d3b452SApple OSS Distributions 						endDiff = (*compare)( childAddressCells, endCell, lookRange );
1331*94d3b452SApple OSS Distributions 						if (endDiff < 0) {
1332*94d3b452SApple OSS Distributions 							continue;
1333*94d3b452SApple OSS Distributions 						}
1334*94d3b452SApple OSS Distributions 
1335*94d3b452SApple OSS Distributions 						UInt64 rangeStart = CellsValue(addressCells, range + childAddressCells);
1336*94d3b452SApple OSS Distributions 						UInt64 lookRangeStart = CellsValue(addressCells, lookRange + childAddressCells);
1337*94d3b452SApple OSS Distributions 						if ((endDiff - len + 1 + lookRangeStart) == (diff + rangeStart)) {
1338*94d3b452SApple OSS Distributions 							ok = true;
1339*94d3b452SApple OSS Distributions 							break;
1340*94d3b452SApple OSS Distributions 						}
1341*94d3b452SApple OSS Distributions 					}
1342*94d3b452SApple OSS Distributions 					if (!ok) {
1343*94d3b452SApple OSS Distributions 						continue;
1344*94d3b452SApple OSS Distributions 					}
1345*94d3b452SApple OSS Distributions 				}
1346*94d3b452SApple OSS Distributions 				offset += diff;
1347*94d3b452SApple OSS Distributions 				break;
1348*94d3b452SApple OSS Distributions 			}
1349*94d3b452SApple OSS Distributions 
1350*94d3b452SApple OSS Distributions 			if (addressCells + sizeCells > sizeof(cell) / sizeof(cell[0])) {
1351*94d3b452SApple OSS Distributions 				panic("IODTResolveAddressCell: Invalid device tree (%u, %u)", (uint32_t)addressCells, (uint32_t)sizeCells);
1352*94d3b452SApple OSS Distributions 			}
1353*94d3b452SApple OSS Distributions 
1354*94d3b452SApple OSS Distributions 			// Get the physical start of the range from our parent
1355*94d3b452SApple OSS Distributions 			bcopy( range + childAddressCells, cell, sizeof(UInt32) * addressCells );
1356*94d3b452SApple OSS Distributions 			bzero( cell + addressCells, sizeof(UInt32) * sizeCells );
1357*94d3b452SApple OSS Distributions 		} /* else zero length range => pass thru to parent */
1358*94d3b452SApple OSS Distributions 
1359*94d3b452SApple OSS Distributions 		OSSafeReleaseNULL(regEntry);
1360*94d3b452SApple OSS Distributions 		regEntry                = parent;
1361*94d3b452SApple OSS Distributions 		parent = NULL;
1362*94d3b452SApple OSS Distributions 		childSizeCells          = sizeCells;
1363*94d3b452SApple OSS Distributions 		childAddressCells       = addressCells;
1364*94d3b452SApple OSS Distributions 		childCells              = childAddressCells + childSizeCells;
1365*94d3b452SApple OSS Distributions 	}while (ok && regEntry);
1366*94d3b452SApple OSS Distributions 
1367*94d3b452SApple OSS Distributions 	OSSafeReleaseNULL(regEntry);
1368*94d3b452SApple OSS Distributions 
1369*94d3b452SApple OSS Distributions 	return ok;
1370*94d3b452SApple OSS Distributions }
1371*94d3b452SApple OSS Distributions 
1372*94d3b452SApple OSS Distributions 
1373*94d3b452SApple OSS Distributions OSArray *
IODTResolveAddressing(IORegistryEntry * regEntry,const char * addressPropertyName,IODeviceMemory * parent)1374*94d3b452SApple OSS Distributions IODTResolveAddressing( IORegistryEntry * regEntry,
1375*94d3b452SApple OSS Distributions     const char * addressPropertyName,
1376*94d3b452SApple OSS Distributions     IODeviceMemory * parent )
1377*94d3b452SApple OSS Distributions {
1378*94d3b452SApple OSS Distributions 	IORegistryEntry             *parentEntry;
1379*94d3b452SApple OSS Distributions 	OSData                              *addressProperty;
1380*94d3b452SApple OSS Distributions 	UInt32                              sizeCells, addressCells, cells;
1381*94d3b452SApple OSS Distributions 	int                                 i, num;
1382*94d3b452SApple OSS Distributions 	UInt32                              *reg;
1383*94d3b452SApple OSS Distributions 	IOPhysicalAddress   phys;
1384*94d3b452SApple OSS Distributions 	IOPhysicalLength    len;
1385*94d3b452SApple OSS Distributions 	OSArray                             *array;
1386*94d3b452SApple OSS Distributions 
1387*94d3b452SApple OSS Distributions 	array = NULL;
1388*94d3b452SApple OSS Distributions 	do{
1389*94d3b452SApple OSS Distributions 		parentEntry = regEntry->copyParentEntry( gIODTPlane );
1390*94d3b452SApple OSS Distributions 		addressProperty = (OSData *) regEntry->getProperty( addressPropertyName );
1391*94d3b452SApple OSS Distributions 		if ((NULL == addressProperty) || (NULL == parentEntry)) {
1392*94d3b452SApple OSS Distributions 			break;
1393*94d3b452SApple OSS Distributions 		}
1394*94d3b452SApple OSS Distributions 
1395*94d3b452SApple OSS Distributions 		IODTGetCellCounts( parentEntry, &sizeCells, &addressCells );
1396*94d3b452SApple OSS Distributions 		if (0 == sizeCells) {
1397*94d3b452SApple OSS Distributions 			break;
1398*94d3b452SApple OSS Distributions 		}
1399*94d3b452SApple OSS Distributions 
1400*94d3b452SApple OSS Distributions 		cells = sizeCells + addressCells;
1401*94d3b452SApple OSS Distributions 		reg = (UInt32 *) addressProperty->getBytesNoCopy();
1402*94d3b452SApple OSS Distributions 		num = addressProperty->getLength() / (4 * cells);
1403*94d3b452SApple OSS Distributions 
1404*94d3b452SApple OSS Distributions 		array = OSArray::withCapacity( 1 );
1405*94d3b452SApple OSS Distributions 		if (NULL == array) {
1406*94d3b452SApple OSS Distributions 			break;
1407*94d3b452SApple OSS Distributions 		}
1408*94d3b452SApple OSS Distributions 
1409*94d3b452SApple OSS Distributions 		for (i = 0; i < num; i++) {
1410*94d3b452SApple OSS Distributions 			if (IODTResolveAddressCell( parentEntry, reg, &phys, &len )) {
1411*94d3b452SApple OSS Distributions 				IODeviceMemory *range;
1412*94d3b452SApple OSS Distributions 				range = NULL;
1413*94d3b452SApple OSS Distributions 				if (parent) {
1414*94d3b452SApple OSS Distributions 					range = IODeviceMemory::withSubRange( parent,
1415*94d3b452SApple OSS Distributions 					    phys - parent->getPhysicalSegment(0, NULL, kIOMemoryMapperNone), len );
1416*94d3b452SApple OSS Distributions 				}
1417*94d3b452SApple OSS Distributions 				if (NULL == range) {
1418*94d3b452SApple OSS Distributions 					range = IODeviceMemory::withRange( phys, len );
1419*94d3b452SApple OSS Distributions 				}
1420*94d3b452SApple OSS Distributions 				if (range) {
1421*94d3b452SApple OSS Distributions 					array->setObject( range );
1422*94d3b452SApple OSS Distributions 					OSSafeReleaseNULL(range);
1423*94d3b452SApple OSS Distributions 				}
1424*94d3b452SApple OSS Distributions 			}
1425*94d3b452SApple OSS Distributions 			reg += cells;
1426*94d3b452SApple OSS Distributions 		}
1427*94d3b452SApple OSS Distributions 
1428*94d3b452SApple OSS Distributions 		regEntry->setProperty( gIODeviceMemoryKey, array);
1429*94d3b452SApple OSS Distributions 		array->release(); /* ??? */
1430*94d3b452SApple OSS Distributions 	}while (false);
1431*94d3b452SApple OSS Distributions 
1432*94d3b452SApple OSS Distributions 	OSSafeReleaseNULL(parentEntry);
1433*94d3b452SApple OSS Distributions 
1434*94d3b452SApple OSS Distributions 	return array;
1435*94d3b452SApple OSS Distributions }
1436*94d3b452SApple OSS Distributions 
1437*94d3b452SApple OSS Distributions OSData *
IODTFindSlotName(IORegistryEntry * regEntry,UInt32 deviceNumber)1438*94d3b452SApple OSS Distributions IODTFindSlotName( IORegistryEntry * regEntry, UInt32 deviceNumber )
1439*94d3b452SApple OSS Distributions {
1440*94d3b452SApple OSS Distributions 	IORegistryEntry             *parent;
1441*94d3b452SApple OSS Distributions 	OSData                              *data;
1442*94d3b452SApple OSS Distributions 	OSData                              *ret = NULL;
1443*94d3b452SApple OSS Distributions 	UInt32                              *bits;
1444*94d3b452SApple OSS Distributions 	UInt32                              i;
1445*94d3b452SApple OSS Distributions 	UInt32              nlen;
1446*94d3b452SApple OSS Distributions 	char                                *names;
1447*94d3b452SApple OSS Distributions 	char                                *lastName;
1448*94d3b452SApple OSS Distributions 	UInt32                              mask;
1449*94d3b452SApple OSS Distributions 
1450*94d3b452SApple OSS Distributions 	data = (OSData *) regEntry->getProperty("AAPL,slot-name");
1451*94d3b452SApple OSS Distributions 	if (data) {
1452*94d3b452SApple OSS Distributions 		return data;
1453*94d3b452SApple OSS Distributions 	}
1454*94d3b452SApple OSS Distributions 
1455*94d3b452SApple OSS Distributions 	do{
1456*94d3b452SApple OSS Distributions 		parent = regEntry->copyParentEntry( gIODTPlane );
1457*94d3b452SApple OSS Distributions 		if (!parent) {
1458*94d3b452SApple OSS Distributions 			break;
1459*94d3b452SApple OSS Distributions 		}
1460*94d3b452SApple OSS Distributions 
1461*94d3b452SApple OSS Distributions 		data = OSDynamicCast( OSData, parent->getProperty("slot-names"));
1462*94d3b452SApple OSS Distributions 		if (!data) {
1463*94d3b452SApple OSS Distributions 			break;
1464*94d3b452SApple OSS Distributions 		}
1465*94d3b452SApple OSS Distributions 		if (data->getLength() <= 4) {
1466*94d3b452SApple OSS Distributions 			break;
1467*94d3b452SApple OSS Distributions 		}
1468*94d3b452SApple OSS Distributions 
1469*94d3b452SApple OSS Distributions 		bits = (UInt32 *) data->getBytesNoCopy();
1470*94d3b452SApple OSS Distributions 		mask = *bits;
1471*94d3b452SApple OSS Distributions 		if ((0 == (mask & (1 << deviceNumber)))) {
1472*94d3b452SApple OSS Distributions 			break;
1473*94d3b452SApple OSS Distributions 		}
1474*94d3b452SApple OSS Distributions 
1475*94d3b452SApple OSS Distributions 		names = (char *)(bits + 1);
1476*94d3b452SApple OSS Distributions 		lastName = names + (data->getLength() - 4);
1477*94d3b452SApple OSS Distributions 
1478*94d3b452SApple OSS Distributions 		for (i = 0; (i <= deviceNumber) && (names < lastName); i++) {
1479*94d3b452SApple OSS Distributions 			if (mask & (1 << i)) {
1480*94d3b452SApple OSS Distributions 				nlen = 1 + ((unsigned int) strnlen(names, lastName - names));
1481*94d3b452SApple OSS Distributions 				if (i == deviceNumber) {
1482*94d3b452SApple OSS Distributions 					data = OSData::withBytesNoCopy(names, nlen);
1483*94d3b452SApple OSS Distributions 					if (data) {
1484*94d3b452SApple OSS Distributions 						regEntry->setProperty("AAPL,slot-name", data);
1485*94d3b452SApple OSS Distributions 						ret = data;
1486*94d3b452SApple OSS Distributions 						data->release();
1487*94d3b452SApple OSS Distributions 					}
1488*94d3b452SApple OSS Distributions 				} else {
1489*94d3b452SApple OSS Distributions 					names += nlen;
1490*94d3b452SApple OSS Distributions 				}
1491*94d3b452SApple OSS Distributions 			}
1492*94d3b452SApple OSS Distributions 		}
1493*94d3b452SApple OSS Distributions 	}while (false);
1494*94d3b452SApple OSS Distributions 
1495*94d3b452SApple OSS Distributions 	OSSafeReleaseNULL(parent);
1496*94d3b452SApple OSS Distributions 
1497*94d3b452SApple OSS Distributions 	return ret;
1498*94d3b452SApple OSS Distributions }
1499*94d3b452SApple OSS Distributions 
1500*94d3b452SApple OSS Distributions extern "C" IOReturn
IONDRVLibrariesInitialize(IOService * provider)1501*94d3b452SApple OSS Distributions IONDRVLibrariesInitialize( IOService * provider )
1502*94d3b452SApple OSS Distributions {
1503*94d3b452SApple OSS Distributions 	return kIOReturnUnsupported;
1504*94d3b452SApple OSS Distributions }
1505