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