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