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