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