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