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