1c1dac77fSApple OSS Distributions /*
2e13b1fa5SApple OSS Distributions  * Copyright (c) 1998-2006 Apple Computer, Inc. All rights reserved.
3c1dac77fSApple OSS Distributions  *
4e13b1fa5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5c1dac77fSApple OSS Distributions  *
6e13b1fa5SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7e13b1fa5SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8e13b1fa5SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9e13b1fa5SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10e13b1fa5SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11e13b1fa5SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12e13b1fa5SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13e13b1fa5SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14c1dac77fSApple OSS Distributions  *
15e13b1fa5SApple OSS Distributions  * Please obtain a copy of the License at
16e13b1fa5SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17e13b1fa5SApple OSS Distributions  *
18e13b1fa5SApple OSS Distributions  * The Original Code and all software distributed under the License are
19e13b1fa5SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20c1dac77fSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21c1dac77fSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22e13b1fa5SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23e13b1fa5SApple OSS Distributions  * Please see the License for the specific language governing rights and
24e13b1fa5SApple OSS Distributions  * limitations under the License.
25c1dac77fSApple OSS Distributions  *
26e13b1fa5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27c1dac77fSApple OSS Distributions  */
28c1dac77fSApple OSS Distributions 
29c1dac77fSApple OSS Distributions #include <IOKit/IODeviceTreeSupport.h>
30c1dac77fSApple OSS Distributions #include <libkern/c++/OSContainers.h>
31bb611c8fSApple OSS Distributions #include <libkern/c++/OSSharedPtr.h>
32c1dac77fSApple OSS Distributions #include <IOKit/IODeviceMemory.h>
33c1dac77fSApple OSS Distributions #include <IOKit/IOService.h>
34c1dac77fSApple OSS Distributions #include <IOKit/IOCatalogue.h>
35c1dac77fSApple OSS Distributions 
36c1dac77fSApple OSS Distributions #include <IOKit/IOLib.h>
37c1dac77fSApple OSS Distributions #include <IOKit/IOKitKeys.h>
38c1dac77fSApple OSS Distributions 
39368ad365SApple OSS Distributions #include <pexpert/device_tree.h>
40368ad365SApple OSS Distributions 
4176e12aa3SApple OSS Distributions #if __arm64__
4276e12aa3SApple OSS Distributions typedef UInt64  dtptr_t;
4376e12aa3SApple OSS Distributions #else
4488cc0b97SApple OSS Distributions typedef UInt32  dtptr_t;
4576e12aa3SApple OSS Distributions #endif
4688cc0b97SApple OSS Distributions 
47c1dac77fSApple OSS Distributions #include <machine/machine_routines.h>
48c1dac77fSApple OSS Distributions 
49855239e5SApple OSS Distributions extern "C" {
50855239e5SApple OSS Distributions int IODTGetLoaderInfo( const char *key, void **infoAddr, int *infosize );
51855239e5SApple OSS Distributions void IODTFreeLoaderInfo( const char *key, void *infoAddr, int infoSize );
52855239e5SApple OSS Distributions int IODTGetDefault(const char *key, void *infoAddr, unsigned int infoSize );
53c1dac77fSApple OSS Distributions }
54c1dac77fSApple OSS Distributions 
55c1dac77fSApple OSS Distributions #include <IOKit/assert.h>
56c1dac77fSApple OSS Distributions 
578149afccSApple OSS Distributions #define IODTSUPPORTDEBUG 0
58c1dac77fSApple OSS Distributions 
59e6231be0SApple OSS Distributions struct IODTPersistent {
60e6231be0SApple OSS Distributions 	IODTCompareAddressCellFunc  compareFunc;
61e6231be0SApple OSS Distributions };
62e6231be0SApple OSS Distributions 
63e6231be0SApple OSS Distributions struct IODTResolvers {
64e6231be0SApple OSS Distributions 	unsigned int     alloc;
65e6231be0SApple OSS Distributions 	unsigned int     count;
66e6231be0SApple OSS Distributions 	IOLock         * lock;
67e6231be0SApple OSS Distributions 	IODTPersistent * resolvers;
68e6231be0SApple OSS Distributions };
69e6231be0SApple OSS Distributions 
70c1dac77fSApple OSS Distributions const IORegistryPlane * gIODTPlane;
71c1dac77fSApple OSS Distributions 
72c1dac77fSApple OSS Distributions static OSArray *    gIODTPHandles;
73c1dac77fSApple OSS Distributions static OSArray *    gIODTPHandleMap;
74e6231be0SApple OSS Distributions 
75e6231be0SApple OSS Distributions static IODTResolvers *  gIODTResolvers;
76c1dac77fSApple OSS Distributions 
77c1dac77fSApple OSS Distributions const OSSymbol *        gIODTNameKey;
78c1dac77fSApple OSS Distributions const OSSymbol *        gIODTUnitKey;
79c1dac77fSApple OSS Distributions const OSSymbol *        gIODTCompatibleKey;
80c1dac77fSApple OSS Distributions const OSSymbol *        gIODTTypeKey;
81c1dac77fSApple OSS Distributions const OSSymbol *        gIODTModelKey;
82bb611c8fSApple OSS Distributions const OSSymbol *        gIODTBridgeModelKey;
830f3703acSApple OSS Distributions const OSSymbol *        gIODTTargetTypeKey;
84c1dac77fSApple OSS Distributions 
85c1dac77fSApple OSS Distributions const OSSymbol *        gIODTSizeCellKey;
86c1dac77fSApple OSS Distributions const OSSymbol *        gIODTAddressCellKey;
87c1dac77fSApple OSS Distributions const OSSymbol *        gIODTRangeKey;
88c1dac77fSApple OSS Distributions 
89c1dac77fSApple OSS Distributions const OSSymbol *        gIODTPersistKey;
90c1dac77fSApple OSS Distributions 
91c1dac77fSApple OSS Distributions const OSSymbol *        gIODTDefaultInterruptController;
92c1dac77fSApple OSS Distributions const OSSymbol *        gIODTAAPLInterruptsKey;
93c1dac77fSApple OSS Distributions const OSSymbol *        gIODTPHandleKey;
94c1dac77fSApple OSS Distributions const OSSymbol *        gIODTInterruptCellKey;
95c1dac77fSApple OSS Distributions const OSSymbol *        gIODTInterruptParentKey;
96c1dac77fSApple OSS Distributions const OSSymbol *        gIODTNWInterruptMappingKey;
97c1dac77fSApple OSS Distributions 
981031c584SApple OSS Distributions const OSData *          gIODTAssociatedServiceKey;
991031c584SApple OSS Distributions 
10014e3d835SApple OSS Distributions OSDictionary   *        gIODTSharedInterrupts;
101c1dac77fSApple OSS Distributions 
102c1dac77fSApple OSS Distributions static IORegistryEntry * MakeReferenceTable( DTEntry dtEntry, bool copy );
103c1dac77fSApple OSS Distributions static void AddPHandle( IORegistryEntry * regEntry );
104c1dac77fSApple OSS Distributions static void FreePhysicalMemory( vm_offset_t * range );
10514e3d835SApple OSS Distributions static bool IODTMapInterruptsSharing( IORegistryEntry * regEntry, OSDictionary * allInts );
106c1dac77fSApple OSS Distributions 
107bb611c8fSApple OSS Distributions // FIXME: Implementation of this function is hidden from the static analyzer.
108bb611c8fSApple OSS Distributions // The analyzer doesn't know that the registry holds retains, and gets confused
109bb611c8fSApple OSS Distributions // about releases after calls to 'attachToParent'.
110bb611c8fSApple OSS Distributions // Feel free to remove the #ifndef and address the warning!
111bb611c8fSApple OSS Distributions #ifndef __clang_analyzer__
112c1dac77fSApple OSS Distributions IORegistryEntry *
IODeviceTreeAlloc(void * dtTop)113c1dac77fSApple OSS Distributions IODeviceTreeAlloc( void * dtTop )
114c1dac77fSApple OSS Distributions {
115c1dac77fSApple OSS Distributions 	IORegistryEntry *           parent;
116c1dac77fSApple OSS Distributions 	IORegistryEntry *           child;
117c1dac77fSApple OSS Distributions 	IORegistryIterator *        regIter;
11876e12aa3SApple OSS Distributions 	OpaqueDTEntryIterator       iter;
119c1dac77fSApple OSS Distributions 	DTEntry                     dtChild;
120c1dac77fSApple OSS Distributions 	DTEntry                     mapEntry;
121c1dac77fSApple OSS Distributions 	OSArray *                   stack;
122c1dac77fSApple OSS Distributions 	OSData *                    prop;
12314e3d835SApple OSS Distributions 	OSDictionary *              allInts;
124c1dac77fSApple OSS Distributions 	vm_offset_t *               dtMap;
125e13b1fa5SApple OSS Distributions 	unsigned int                propSize;
126c1dac77fSApple OSS Distributions 	bool                        intMap;
127bb611c8fSApple OSS Distributions 	bool                        foundDTNode;
128c1dac77fSApple OSS Distributions 	bool                        freeDT;
1291031c584SApple OSS Distributions 	char                        exBootArg[64];
1301031c584SApple OSS Distributions 	const char *                found;
131c1dac77fSApple OSS Distributions 
132c1dac77fSApple OSS Distributions 	gIODTPlane = IORegistryEntry::makePlane( kIODeviceTreePlane );
133c1dac77fSApple OSS Distributions 
134c1dac77fSApple OSS Distributions 	gIODTNameKey                = OSSymbol::withCStringNoCopy( "name" );
135c1dac77fSApple OSS Distributions 	gIODTUnitKey                = OSSymbol::withCStringNoCopy( "AAPL,unit-string" );
136c1dac77fSApple OSS Distributions 	gIODTCompatibleKey  = OSSymbol::withCStringNoCopy( "compatible" );
137c1dac77fSApple OSS Distributions 	gIODTTypeKey                = OSSymbol::withCStringNoCopy( "device_type" );
138c1dac77fSApple OSS Distributions 	gIODTModelKey               = OSSymbol::withCStringNoCopy( "model" );
139bb611c8fSApple OSS Distributions 	gIODTBridgeModelKey         = OSSymbol::withCStringNoCopy( "bridge-model" );
1400f3703acSApple OSS Distributions 	gIODTTargetTypeKey          = OSSymbol::withCStringNoCopy( "target-type" );
141c1dac77fSApple OSS Distributions 	gIODTSizeCellKey    = OSSymbol::withCStringNoCopy( "#size-cells" );
142c1dac77fSApple OSS Distributions 	gIODTAddressCellKey = OSSymbol::withCStringNoCopy( "#address-cells" );
143c1dac77fSApple OSS Distributions 	gIODTRangeKey               = OSSymbol::withCStringNoCopy( "ranges" );
144c1dac77fSApple OSS Distributions 	gIODTPersistKey             = OSSymbol::withCStringNoCopy( "IODTPersist" );
1451031c584SApple OSS Distributions 	gIODTAssociatedServiceKey   = OSData::withBytesNoCopy((void *) kIODTAssociatedServiceKey, sizeof(kIODTAssociatedServiceKey));
1461031c584SApple OSS Distributions 
147c1dac77fSApple OSS Distributions 
148c1dac77fSApple OSS Distributions 	assert(    gIODTPlane && gIODTCompatibleKey
149c1dac77fSApple OSS Distributions 	    && gIODTTypeKey && gIODTModelKey
150c1dac77fSApple OSS Distributions 	    && gIODTSizeCellKey && gIODTAddressCellKey && gIODTRangeKey
151c1dac77fSApple OSS Distributions 	    && gIODTPersistKey );
152c1dac77fSApple OSS Distributions 
153c1dac77fSApple OSS Distributions 	gIODTDefaultInterruptController
154c1dac77fSApple OSS Distributions 	        = OSSymbol::withCStringNoCopy("IOPrimaryInterruptController");
155c1dac77fSApple OSS Distributions 	gIODTNWInterruptMappingKey
156c1dac77fSApple OSS Distributions 	        = OSSymbol::withCStringNoCopy("IONWInterrupts");
157c1dac77fSApple OSS Distributions 
158c1dac77fSApple OSS Distributions 	gIODTAAPLInterruptsKey
159c1dac77fSApple OSS Distributions 	        = OSSymbol::withCStringNoCopy("AAPL,interrupts");
160c1dac77fSApple OSS Distributions 	gIODTPHandleKey
161c1dac77fSApple OSS Distributions 	        = OSSymbol::withCStringNoCopy("AAPL,phandle");
162c1dac77fSApple OSS Distributions 
163c1dac77fSApple OSS Distributions 	gIODTInterruptParentKey
164c1dac77fSApple OSS Distributions 	        = OSSymbol::withCStringNoCopy("interrupt-parent");
165c1dac77fSApple OSS Distributions 
166c1dac77fSApple OSS Distributions 	gIODTPHandles       = OSArray::withCapacity( 1 );
167c1dac77fSApple OSS Distributions 	gIODTPHandleMap     = OSArray::withCapacity( 1 );
16876e12aa3SApple OSS Distributions 
169e6231be0SApple OSS Distributions 	gIODTResolvers            = zalloc_permanent_type(IODTResolvers);
170e6231be0SApple OSS Distributions 	gIODTResolvers->count     = 0;
171e6231be0SApple OSS Distributions 	gIODTResolvers->alloc     = 2;
172e6231be0SApple OSS Distributions 	gIODTResolvers->resolvers = IONewZero(IODTPersistent, gIODTResolvers->alloc);
173e6231be0SApple OSS Distributions 	gIODTResolvers->lock      = IOLockAlloc();
174c1dac77fSApple OSS Distributions 
1751031c584SApple OSS Distributions 	if (!PE_parse_boot_argn("exp", exBootArg, sizeof(exBootArg))) {
1761031c584SApple OSS Distributions 		exBootArg[0] = '\0';
1771031c584SApple OSS Distributions 	}
1781031c584SApple OSS Distributions 
179c1dac77fSApple OSS Distributions 	gIODTInterruptCellKey
180c1dac77fSApple OSS Distributions 	        = OSSymbol::withCStringNoCopy("#interrupt-cells");
181c1dac77fSApple OSS Distributions 
182c1dac77fSApple OSS Distributions 	assert(    gIODTDefaultInterruptController && gIODTNWInterruptMappingKey
183c1dac77fSApple OSS Distributions 	    && gIODTAAPLInterruptsKey
184c1dac77fSApple OSS Distributions 	    && gIODTPHandleKey && gIODTInterruptParentKey
185e6231be0SApple OSS Distributions 	    && gIODTPHandles && gIODTPHandleMap && gIODTInterruptCellKey
186e6231be0SApple OSS Distributions 	    && gIODTResolvers && gIODTResolvers->lock && gIODTResolvers->resolvers
187c1dac77fSApple OSS Distributions 	    );
188c1dac77fSApple OSS Distributions 
189bb611c8fSApple OSS Distributions 	foundDTNode = (kSuccess == SecureDTLookupEntry( NULL, "/chosen/memory-map", &mapEntry ))
190bb611c8fSApple OSS Distributions 	    && (kSuccess == SecureDTGetProperty( mapEntry,
191bb611c8fSApple OSS Distributions 	    "DeviceTree", (void const **) &dtMap, &propSize ))
1923ca3bd55SApple OSS Distributions 	    && ((2 * sizeof(uint32_t)) == propSize);
193c1dac77fSApple OSS Distributions 
194bb611c8fSApple OSS Distributions 	freeDT = foundDTNode && !SecureDTIsLockedDown();
195bb611c8fSApple OSS Distributions 
196c1dac77fSApple OSS Distributions 	parent = MakeReferenceTable((DTEntry)dtTop, freeDT );
197c1dac77fSApple OSS Distributions 
198368ad365SApple OSS Distributions 	stack = OSArray::withObjects((const OSObject **) &parent, 1, 10 );
199bb611c8fSApple OSS Distributions 	SecureDTInitEntryIterator((DTEntry)dtTop, &iter );
200c1dac77fSApple OSS Distributions 
201c1dac77fSApple OSS Distributions 	do {
202c1dac77fSApple OSS Distributions 		parent = (IORegistryEntry *)stack->getObject( stack->getCount() - 1);
203c1dac77fSApple OSS Distributions 		//parent->release();
204c1dac77fSApple OSS Distributions 		stack->removeObject( stack->getCount() - 1);
205c1dac77fSApple OSS Distributions 
206bb611c8fSApple OSS Distributions 		while (kSuccess == SecureDTIterateEntries( &iter, &dtChild)) {
207c1dac77fSApple OSS Distributions 			child = MakeReferenceTable( dtChild, freeDT );
208c1dac77fSApple OSS Distributions 			child->attachToParent( parent, gIODTPlane);
209c1dac77fSApple OSS Distributions 
210c1dac77fSApple OSS Distributions 			AddPHandle( child );
2111031c584SApple OSS Distributions 			// E.g. exp=sgx:3 or exp=sgx:3,5
2121031c584SApple OSS Distributions 			if ((found = strnstr(exBootArg, child->getName(), sizeof(exBootArg)))) {
2131031c584SApple OSS Distributions 				child->setProperty(gIOExclaveAssignedKey, kOSBooleanTrue);
2141031c584SApple OSS Distributions 				uint32_t ep = 0;
2151031c584SApple OSS Distributions 				uint32_t edk_ep = 0;
2161031c584SApple OSS Distributions 				found += strlen(child->getName());
2171031c584SApple OSS Distributions 				if (':' == *found) {
2181031c584SApple OSS Distributions 					char *end;
2191031c584SApple OSS Distributions 					ep = (uint32_t) strtol(found + 1, &end, 0);
2201031c584SApple OSS Distributions 					// Check for optional edk endpoint
2211031c584SApple OSS Distributions 					if (',' == *end) {
2221031c584SApple OSS Distributions 						edk_ep = (uint32_t) strtol(end + 1, &end, 0);
2231031c584SApple OSS Distributions 						child->setProperty("exclave-edk-endpoint", &edk_ep, sizeof(edk_ep));
2241031c584SApple OSS Distributions 					}
2251031c584SApple OSS Distributions 				}
2261031c584SApple OSS Distributions 				child->setProperty("exclave-endpoint", &ep, sizeof(ep));
2271031c584SApple OSS Distributions 			}
228c1dac77fSApple OSS Distributions 
229bb611c8fSApple OSS Distributions 			if (kSuccess == SecureDTEnterEntry( &iter, dtChild)) {
230c1dac77fSApple OSS Distributions 				stack->setObject( parent);
231c1dac77fSApple OSS Distributions 				parent = child;
232c1dac77fSApple OSS Distributions 			}
233c1dac77fSApple OSS Distributions 			// only registry holds retain
234c1dac77fSApple OSS Distributions 			child->release();
235c1dac77fSApple OSS Distributions 		}
236c1dac77fSApple OSS Distributions 	} while (stack->getCount()
237bb611c8fSApple OSS Distributions 	    && (kSuccess == SecureDTExitEntry( &iter, &dtChild)));
238c1dac77fSApple OSS Distributions 
239c1dac77fSApple OSS Distributions 	stack->release();
240bb611c8fSApple OSS Distributions 	assert(kSuccess != SecureDTExitEntry(&iter, &dtChild));
241c1dac77fSApple OSS Distributions 
242c1dac77fSApple OSS Distributions 	// parent is now root of the created tree
243c1dac77fSApple OSS Distributions 
244c1dac77fSApple OSS Distributions 	// make root name first compatible entry (purely cosmetic)
245c1dac77fSApple OSS Distributions 	if ((prop = (OSData *) parent->getProperty( gIODTCompatibleKey))) {
246c1dac77fSApple OSS Distributions 		parent->setName( parent->getName(), gIODTPlane );
247c1dac77fSApple OSS Distributions 		parent->setName((const char *) prop->getBytesNoCopy());
248c1dac77fSApple OSS Distributions 	}
249c1dac77fSApple OSS Distributions 
250c1dac77fSApple OSS Distributions 	// attach tree to meta root
251c1dac77fSApple OSS Distributions 	parent->attachToParent( IORegistryEntry::getRegistryRoot(), gIODTPlane);
252c1dac77fSApple OSS Distributions 	parent->release();
253c1dac77fSApple OSS Distributions 
254c1dac77fSApple OSS Distributions 	if (freeDT) {
255c1dac77fSApple OSS Distributions 		// free original device tree
256bb611c8fSApple OSS Distributions 		SecureDTInit(NULL, 0);
257c1dac77fSApple OSS Distributions 		IODTFreeLoaderInfo( "DeviceTree",
2583ca3bd55SApple OSS Distributions 		    (void *)dtMap[0], (int) round_page(dtMap[1]));
259c1dac77fSApple OSS Distributions 	}
260c1dac77fSApple OSS Distributions 
261c1dac77fSApple OSS Distributions 	// adjust tree
26214e3d835SApple OSS Distributions 
26314e3d835SApple OSS Distributions 	gIODTSharedInterrupts = OSDictionary::withCapacity(4);
26414e3d835SApple OSS Distributions 	allInts = OSDictionary::withCapacity(4);
265c1dac77fSApple OSS Distributions 	intMap = false;
266c1dac77fSApple OSS Distributions 	regIter = IORegistryIterator::iterateOver( gIODTPlane,
267c1dac77fSApple OSS Distributions 	    kIORegistryIterateRecursively );
26814e3d835SApple OSS Distributions 	assert( regIter && allInts && gIODTSharedInterrupts );
26914e3d835SApple OSS Distributions 	if (regIter && allInts && gIODTSharedInterrupts) {
270c1dac77fSApple OSS Distributions 		while ((child = regIter->getNextObject())) {
27114e3d835SApple OSS Distributions 			IODTMapInterruptsSharing( child, allInts );
272a5e72196SApple OSS Distributions 			if (!intMap && child->getProperty( gIODTInterruptParentKey)) {
273c1dac77fSApple OSS Distributions 				intMap = true;
274a5e72196SApple OSS Distributions 			}
275a5e72196SApple OSS Distributions 			if (!strcmp("sep", child->getName())
276a5e72196SApple OSS Distributions 			    || !strcmp("aop", child->getName())
277a5e72196SApple OSS Distributions 			    || !strcmp("disp0", child->getName())) {
278a5e72196SApple OSS Distributions 				uint32_t aotFlags = 1;
279a5e72196SApple OSS Distributions 				child->setProperty("aot-power", &aotFlags, sizeof(aotFlags));
280a5e72196SApple OSS Distributions 			}
281c1dac77fSApple OSS Distributions 		}
282c1dac77fSApple OSS Distributions 		regIter->release();
283c1dac77fSApple OSS Distributions 	}
284c1dac77fSApple OSS Distributions 
28514e3d835SApple OSS Distributions #if IODTSUPPORTDEBUG
28614e3d835SApple OSS Distributions 	parent->setProperty("allInts", allInts);
28714e3d835SApple OSS Distributions 	parent->setProperty("sharedInts", gIODTSharedInterrupts);
28814e3d835SApple OSS Distributions 
28914e3d835SApple OSS Distributions 	regIter = IORegistryIterator::iterateOver( gIODTPlane,
29014e3d835SApple OSS Distributions 	    kIORegistryIterateRecursively );
29114e3d835SApple OSS Distributions 	if (regIter) {
29214e3d835SApple OSS Distributions 		while ((child = regIter->getNextObject())) {
29314e3d835SApple OSS Distributions 			OSArray *
29414e3d835SApple OSS Distributions 			    array = OSDynamicCast(OSArray, child->getProperty( gIOInterruptSpecifiersKey ));
295a5e72196SApple OSS Distributions 			for (UInt32 i = 0; array && (i < array->getCount()); i++) {
29614e3d835SApple OSS Distributions 				IOOptionBits options;
29714e3d835SApple OSS Distributions 				IOReturn ret = IODTGetInterruptOptions( child, i, &options );
298a5e72196SApple OSS Distributions 				if ((ret != kIOReturnSuccess) || options) {
29914e3d835SApple OSS Distributions 					IOLog("%s[%ld] %ld (%x)\n", child->getName(), i, options, ret);
30014e3d835SApple OSS Distributions 				}
30114e3d835SApple OSS Distributions 			}
302a5e72196SApple OSS Distributions 		}
30314e3d835SApple OSS Distributions 		regIter->release();
30414e3d835SApple OSS Distributions 	}
30514e3d835SApple OSS Distributions #endif
30614e3d835SApple OSS Distributions 
30714e3d835SApple OSS Distributions 	allInts->release();
30814e3d835SApple OSS Distributions 
309a5e72196SApple OSS Distributions 	if (intMap) {
310c1dac77fSApple OSS Distributions 		// set a key in the root to indicate we found NW interrupt mapping
311c1dac77fSApple OSS Distributions 		parent->setProperty( gIODTNWInterruptMappingKey,
312c1dac77fSApple OSS Distributions 		    (OSObject *) gIODTNWInterruptMappingKey );
313c1dac77fSApple OSS Distributions 	}
314c1dac77fSApple OSS Distributions 
315a5e72196SApple OSS Distributions 	return parent;
316a5e72196SApple OSS Distributions }
317bb611c8fSApple OSS Distributions #endif
318a5e72196SApple OSS Distributions 
319a5e72196SApple OSS Distributions int
IODTGetLoaderInfo(const char * key,void ** infoAddr,int * infoSize)320a5e72196SApple OSS Distributions IODTGetLoaderInfo( const char *key, void **infoAddr, int *infoSize )
321c1dac77fSApple OSS Distributions {
322c1dac77fSApple OSS Distributions 	IORegistryEntry             *chosen;
323c1dac77fSApple OSS Distributions 	OSData                              *propObj;
32488cc0b97SApple OSS Distributions 	dtptr_t                             *propPtr;
325c1dac77fSApple OSS Distributions 	unsigned int                propSize;
32676e12aa3SApple OSS Distributions 	int ret = -1;
327c1dac77fSApple OSS Distributions 
328c1dac77fSApple OSS Distributions 	chosen = IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane );
329a5e72196SApple OSS Distributions 	if (chosen == NULL) {
330a5e72196SApple OSS Distributions 		return -1;
331a5e72196SApple OSS Distributions 	}
332c1dac77fSApple OSS Distributions 
333c1dac77fSApple OSS Distributions 	propObj = OSDynamicCast( OSData, chosen->getProperty(key));
334a5e72196SApple OSS Distributions 	if (propObj == NULL) {
335a5e72196SApple OSS Distributions 		goto cleanup;
336a5e72196SApple OSS Distributions 	}
337c1dac77fSApple OSS Distributions 
338c1dac77fSApple OSS Distributions 	propSize = propObj->getLength();
339a5e72196SApple OSS Distributions 	if (propSize != (2 * sizeof(dtptr_t))) {
340a5e72196SApple OSS Distributions 		goto cleanup;
341a5e72196SApple OSS Distributions 	}
342c1dac77fSApple OSS Distributions 
34388cc0b97SApple OSS Distributions 	propPtr = (dtptr_t *)propObj->getBytesNoCopy();
344a5e72196SApple OSS Distributions 	if (propPtr == NULL) {
345a5e72196SApple OSS Distributions 		goto cleanup;
346a5e72196SApple OSS Distributions 	}
347c1dac77fSApple OSS Distributions 
348186b8fceSApple OSS Distributions 	*infoAddr = (void *)(uintptr_t) (propPtr[0]);
349186b8fceSApple OSS Distributions 	*infoSize = (int)               (propPtr[1]);
350c1dac77fSApple OSS Distributions 
35176e12aa3SApple OSS Distributions 	ret = 0;
35276e12aa3SApple OSS Distributions 
35376e12aa3SApple OSS Distributions cleanup:
35476e12aa3SApple OSS Distributions 	chosen->release();
35576e12aa3SApple OSS Distributions 
35676e12aa3SApple OSS Distributions 	return ret;
357c1dac77fSApple OSS Distributions }
358c1dac77fSApple OSS Distributions 
359a5e72196SApple OSS Distributions void
IODTFreeLoaderInfo(const char * key,void * infoAddr,int infoSize)360a5e72196SApple OSS Distributions IODTFreeLoaderInfo( const char *key, void *infoAddr, int infoSize )
361c1dac77fSApple OSS Distributions {
362c1dac77fSApple OSS Distributions 	vm_offset_t                 range[2];
363c1dac77fSApple OSS Distributions 	IORegistryEntry             *chosen;
364c1dac77fSApple OSS Distributions 
365c1dac77fSApple OSS Distributions 	range[0] = (vm_offset_t)infoAddr;
366c1dac77fSApple OSS Distributions 	range[1] = (vm_offset_t)infoSize;
367c1dac77fSApple OSS Distributions 	FreePhysicalMemory( range );
368c1dac77fSApple OSS Distributions 
369a5e72196SApple OSS Distributions 	if (key != NULL) {
370c1dac77fSApple OSS Distributions 		chosen = IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane );
371a5e72196SApple OSS Distributions 		if (chosen != NULL) {
372c1dac77fSApple OSS Distributions 			chosen->removeProperty(key);
37376e12aa3SApple OSS Distributions 			chosen->release();
374c1dac77fSApple OSS Distributions 		}
375c1dac77fSApple OSS Distributions 	}
376c1dac77fSApple OSS Distributions }
377c1dac77fSApple OSS Distributions 
378a5e72196SApple OSS Distributions int
IODTGetDefault(const char * key,void * infoAddr,unsigned int infoSize)379a5e72196SApple OSS Distributions IODTGetDefault(const char *key, void *infoAddr, unsigned int infoSize )
380855239e5SApple OSS Distributions {
381855239e5SApple OSS Distributions 	IORegistryEntry             *defaults;
382855239e5SApple OSS Distributions 	OSData                      *defaultObj;
383855239e5SApple OSS Distributions 	unsigned int                defaultSize;
384855239e5SApple OSS Distributions 
385855239e5SApple OSS Distributions 	defaults = IORegistryEntry::fromPath( "/defaults", gIODTPlane );
386a5e72196SApple OSS Distributions 	if (defaults == NULL) {
387a5e72196SApple OSS Distributions 		return -1;
388a5e72196SApple OSS Distributions 	}
389855239e5SApple OSS Distributions 
390855239e5SApple OSS Distributions 	defaultObj = OSDynamicCast( OSData, defaults->getProperty(key));
391e6231be0SApple OSS Distributions 
392a5e72196SApple OSS Distributions 	if (defaultObj == NULL) {
393e6231be0SApple OSS Distributions 		defaults->release();
394a5e72196SApple OSS Distributions 		return -1;
395a5e72196SApple OSS Distributions 	}
396855239e5SApple OSS Distributions 
397855239e5SApple OSS Distributions 	defaultSize = defaultObj->getLength();
398a5e72196SApple OSS Distributions 	if (defaultSize > infoSize) {
399e6231be0SApple OSS Distributions 		defaults->release();
400a5e72196SApple OSS Distributions 		return -1;
401a5e72196SApple OSS Distributions 	}
402855239e5SApple OSS Distributions 
403855239e5SApple OSS Distributions 	memcpy( infoAddr, defaultObj->getBytesNoCopy(), defaultSize );
404855239e5SApple OSS Distributions 
405e6231be0SApple OSS Distributions 	defaults->release();
406855239e5SApple OSS Distributions 	return 0;
407855239e5SApple OSS Distributions }
408855239e5SApple OSS Distributions 
409a5e72196SApple OSS Distributions static void
FreePhysicalMemory(vm_offset_t * range)410a5e72196SApple OSS Distributions FreePhysicalMemory( vm_offset_t * range )
411c1dac77fSApple OSS Distributions {
412c1dac77fSApple OSS Distributions 	vm_offset_t virt;
413c1dac77fSApple OSS Distributions 
414c1dac77fSApple OSS Distributions 	virt = ml_static_ptovirt( range[0] );
415c1dac77fSApple OSS Distributions 	if (virt) {
416c1dac77fSApple OSS Distributions 		ml_static_mfree( virt, range[1] );
417c1dac77fSApple OSS Distributions 	}
418c1dac77fSApple OSS Distributions }
419c1dac77fSApple OSS Distributions 
420c1dac77fSApple OSS Distributions static IORegistryEntry *
MakeReferenceTable(DTEntry dtEntry,bool copy)421c1dac77fSApple OSS Distributions MakeReferenceTable( DTEntry dtEntry, bool copy )
422c1dac77fSApple OSS Distributions {
423c1dac77fSApple OSS Distributions 	IORegistryEntry             *regEntry;
424c1dac77fSApple OSS Distributions 	OSDictionary                *propTable;
425c1dac77fSApple OSS Distributions 	const OSSymbol              *nameKey;
426c1dac77fSApple OSS Distributions 	OSData                              *data;
427c1dac77fSApple OSS Distributions 	const OSSymbol              *sym;
42876e12aa3SApple OSS Distributions 	OpaqueDTPropertyIterator    dtIter;
429bb611c8fSApple OSS Distributions 	void const                  *prop;
430e13b1fa5SApple OSS Distributions 	unsigned int                propSize;
431bb611c8fSApple OSS Distributions 	char const                                      *name;
432c1dac77fSApple OSS Distributions 	char                                location[32];
433c1dac77fSApple OSS Distributions 	bool                                noLocation = true;
43476e12aa3SApple OSS Distributions 	bool                                kernelOnly;
435c1dac77fSApple OSS Distributions 
436c1dac77fSApple OSS Distributions 	regEntry = new IOService;
437c1dac77fSApple OSS Distributions 
438c1dac77fSApple OSS Distributions 	if (regEntry && (false == regEntry->init())) {
439c1dac77fSApple OSS Distributions 		regEntry->release();
440a5e72196SApple OSS Distributions 		regEntry = NULL;
441c1dac77fSApple OSS Distributions 	}
442c1dac77fSApple OSS Distributions 
443c1dac77fSApple OSS Distributions 	if (regEntry &&
444bb611c8fSApple OSS Distributions 	    (kSuccess == SecureDTInitPropertyIterator( dtEntry, &dtIter))) {
445bb611c8fSApple OSS Distributions 		kernelOnly = (kSuccess == SecureDTGetProperty(dtEntry, "kernel-only", &prop, &propSize));
446c1dac77fSApple OSS Distributions 		propTable = regEntry->getPropertyTable();
447c1dac77fSApple OSS Distributions 
448bb611c8fSApple OSS Distributions 		while (kSuccess == SecureDTIterateProperties( &dtIter, &name)) {
449bb611c8fSApple OSS Distributions 			if (kSuccess != SecureDTGetProperty( dtEntry, name, &prop, &propSize )) {
450c1dac77fSApple OSS Distributions 				continue;
451a5e72196SApple OSS Distributions 			}
452c1dac77fSApple OSS Distributions 
453c1dac77fSApple OSS Distributions 			if (copy) {
454c1dac77fSApple OSS Distributions 				nameKey = OSSymbol::withCString(name);
455c1dac77fSApple OSS Distributions 				data = OSData::withBytes(prop, propSize);
456c1dac77fSApple OSS Distributions 			} else {
457c1dac77fSApple OSS Distributions 				nameKey = OSSymbol::withCStringNoCopy(name);
458bb611c8fSApple OSS Distributions 				/* There is no OSDataConst or other way to indicate
459bb611c8fSApple OSS Distributions 				 * that the OSData is actually immutable. But CTRR
460bb611c8fSApple OSS Distributions 				 * will catch any write attempts. */
461bb611c8fSApple OSS Distributions 				data = OSData::withBytesNoCopy((void**)(uintptr_t)prop, propSize);
462c1dac77fSApple OSS Distributions 			}
463c1dac77fSApple OSS Distributions 			assert( nameKey && data );
464c1dac77fSApple OSS Distributions 
465*94d3b452SApple OSS Distributions #if DEVELOPMENT || DEBUG
466*94d3b452SApple OSS Distributions #pragma unused(kernelOnly)
467*94d3b452SApple OSS Distributions #else
468a5e72196SApple OSS Distributions 			if (kernelOnly) {
46976e12aa3SApple OSS Distributions 				data->setSerializable(false);
470a5e72196SApple OSS Distributions 			}
471*94d3b452SApple OSS Distributions #endif
47276e12aa3SApple OSS Distributions 
473c1dac77fSApple OSS Distributions 			propTable->setObject( nameKey, data);
474c1dac77fSApple OSS Distributions 			data->release();
475c1dac77fSApple OSS Distributions 			nameKey->release();
476c1dac77fSApple OSS Distributions 
477c1dac77fSApple OSS Distributions 			if (nameKey == gIODTNameKey) {
478a5e72196SApple OSS Distributions 				if (copy) {
479c1dac77fSApple OSS Distributions 					sym = OSSymbol::withCString((const char *) prop);
480a5e72196SApple OSS Distributions 				} else {
481c1dac77fSApple OSS Distributions 					sym = OSSymbol::withCStringNoCopy((const char *) prop);
482a5e72196SApple OSS Distributions 				}
483c1dac77fSApple OSS Distributions 				regEntry->setName( sym );
484c1dac77fSApple OSS Distributions 				sym->release();
485c1dac77fSApple OSS Distributions 			} else if (nameKey == gIODTUnitKey) {
486c1dac77fSApple OSS Distributions 				// all OF strings are null terminated... except this one
487a5e72196SApple OSS Distributions 				if (propSize >= (int) sizeof(location)) {
488c1dac77fSApple OSS Distributions 					propSize = sizeof(location) - 1;
489a5e72196SApple OSS Distributions 				}
490c1dac77fSApple OSS Distributions 				strncpy( location, (const char *) prop, propSize );
491c1dac77fSApple OSS Distributions 				location[propSize] = 0;
492c1dac77fSApple OSS Distributions 				regEntry->setLocation( location );
493c1dac77fSApple OSS Distributions 				propTable->removeObject( gIODTUnitKey );
494c1dac77fSApple OSS Distributions 				noLocation = false;
495e13b1fa5SApple OSS Distributions 			} else if (noLocation && (!strncmp(name, "reg", sizeof("reg")))) {
496c1dac77fSApple OSS Distributions 				// default location - override later
4973ca3bd55SApple OSS Distributions 				snprintf(location, sizeof(location), "%X", *((uint32_t *) prop));
498c1dac77fSApple OSS Distributions 				regEntry->setLocation( location );
499c1dac77fSApple OSS Distributions 			}
500c1dac77fSApple OSS Distributions 		}
501c1dac77fSApple OSS Distributions 	}
502c1dac77fSApple OSS Distributions 
503a5e72196SApple OSS Distributions 	return regEntry;
504c1dac77fSApple OSS Distributions }
505c1dac77fSApple OSS Distributions 
506a5e72196SApple OSS Distributions static void
AddPHandle(IORegistryEntry * regEntry)507a5e72196SApple OSS Distributions AddPHandle( IORegistryEntry * regEntry )
508c1dac77fSApple OSS Distributions {
509c1dac77fSApple OSS Distributions 	OSData *    data;
510c1dac77fSApple OSS Distributions 
511c1dac77fSApple OSS Distributions 	if (regEntry->getProperty( gIODTInterruptCellKey)
512c1dac77fSApple OSS Distributions 	    && (data = OSDynamicCast( OSData, regEntry->getProperty( gIODTPHandleKey )))) {
513c1dac77fSApple OSS Distributions 		// a possible interrupt-parent
514c1dac77fSApple OSS Distributions 		gIODTPHandles->setObject( data );
515c1dac77fSApple OSS Distributions 		gIODTPHandleMap->setObject( regEntry );
516c1dac77fSApple OSS Distributions 	}
517c1dac77fSApple OSS Distributions }
518c1dac77fSApple OSS Distributions 
519e6231be0SApple OSS Distributions static LIBKERN_RETURNS_NOT_RETAINED IORegistryEntry *
FindPHandle(UInt32 phandle)520a5e72196SApple OSS Distributions FindPHandle( UInt32 phandle )
521c1dac77fSApple OSS Distributions {
522c1dac77fSApple OSS Distributions 	OSData                      *data;
523a5e72196SApple OSS Distributions 	IORegistryEntry *regEntry = NULL;
524c1dac77fSApple OSS Distributions 	int                         i;
525c1dac77fSApple OSS Distributions 
5268149afccSApple OSS Distributions 	for (i = 0; (data = (OSData *)gIODTPHandles->getObject( i )); i++) {
527c1dac77fSApple OSS Distributions 		if (phandle == *((UInt32 *)data->getBytesNoCopy())) {
528c1dac77fSApple OSS Distributions 			regEntry = (IORegistryEntry *)
529c1dac77fSApple OSS Distributions 			    gIODTPHandleMap->getObject( i );
530c1dac77fSApple OSS Distributions 			break;
531c1dac77fSApple OSS Distributions 		}
532c1dac77fSApple OSS Distributions 	}
533c1dac77fSApple OSS Distributions 
534a5e72196SApple OSS Distributions 	return regEntry;
535c1dac77fSApple OSS Distributions }
536c1dac77fSApple OSS Distributions 
537a5e72196SApple OSS Distributions static bool
GetUInt32(IORegistryEntry * regEntry,const OSSymbol * name,UInt32 * value)538a5e72196SApple OSS Distributions GetUInt32( IORegistryEntry * regEntry, const OSSymbol * name,
539c1dac77fSApple OSS Distributions     UInt32 * value )
540c1dac77fSApple OSS Distributions {
54188cc0b97SApple OSS Distributions 	OSObject * obj;
542c1dac77fSApple OSS Distributions 	OSData   * data;
54388cc0b97SApple OSS Distributions 	bool       result;
544c1dac77fSApple OSS Distributions 
545a5e72196SApple OSS Distributions 	if (!(obj = regEntry->copyProperty(name))) {
546a5e72196SApple OSS Distributions 		return false;
547c1dac77fSApple OSS Distributions 	}
548c1dac77fSApple OSS Distributions 
549a5e72196SApple OSS Distributions 	result = ((data = OSDynamicCast(OSData, obj)) && (sizeof(UInt32) == data->getLength()));
550a5e72196SApple OSS Distributions 	if (result) {
551a5e72196SApple OSS Distributions 		*value = *((UInt32 *) data->getBytesNoCopy());
552a5e72196SApple OSS Distributions 	}
553a5e72196SApple OSS Distributions 
554a5e72196SApple OSS Distributions 	obj->release();
555a5e72196SApple OSS Distributions 	return result;
556a5e72196SApple OSS Distributions }
557a5e72196SApple OSS Distributions 
558a5e72196SApple OSS Distributions static IORegistryEntry *
IODTFindInterruptParent(IORegistryEntry * regEntry,IOItemCount index)559a5e72196SApple OSS Distributions IODTFindInterruptParent( IORegistryEntry * regEntry, IOItemCount index )
560c1dac77fSApple OSS Distributions {
561c1dac77fSApple OSS Distributions 	IORegistryEntry *   parent;
562c1dac77fSApple OSS Distributions 	UInt32              phandle;
5633ca3bd55SApple OSS Distributions 	OSData          *   data;
5643ca3bd55SApple OSS Distributions 	unsigned int        len;
565c1dac77fSApple OSS Distributions 
5663ca3bd55SApple OSS Distributions 	if ((data = OSDynamicCast( OSData, regEntry->getProperty( gIODTInterruptParentKey )))
5673ca3bd55SApple OSS Distributions 	    && (sizeof(UInt32) <= (len = data->getLength()))) {
568a5e72196SApple OSS Distributions 		if (((index + 1) * sizeof(UInt32)) > len) {
5693ca3bd55SApple OSS Distributions 			index = 0;
570a5e72196SApple OSS Distributions 		}
5713ca3bd55SApple OSS Distributions 		phandle = ((UInt32 *) data->getBytesNoCopy())[index];
572c1dac77fSApple OSS Distributions 		parent = FindPHandle( phandle );
573a5e72196SApple OSS Distributions 	} else if (NULL == regEntry->getProperty( "interrupt-controller")) {
574c1dac77fSApple OSS Distributions 		parent = regEntry->getParentEntry( gIODTPlane);
575a5e72196SApple OSS Distributions 	} else {
576a5e72196SApple OSS Distributions 		parent = NULL;
577c1dac77fSApple OSS Distributions 	}
578c1dac77fSApple OSS Distributions 
579a5e72196SApple OSS Distributions 	return parent;
580a5e72196SApple OSS Distributions }
581a5e72196SApple OSS Distributions 
582a5e72196SApple OSS Distributions const OSSymbol *
IODTInterruptControllerName(IORegistryEntry * regEntry)583a5e72196SApple OSS Distributions IODTInterruptControllerName( IORegistryEntry * regEntry )
584c1dac77fSApple OSS Distributions {
585c1dac77fSApple OSS Distributions 	const OSSymbol      *sym;
586c1dac77fSApple OSS Distributions 	UInt32              phandle;
587c1dac77fSApple OSS Distributions 	bool                ok;
588c1dac77fSApple OSS Distributions 	char                buf[48];
589c1dac77fSApple OSS Distributions 
590c1dac77fSApple OSS Distributions 	ok = GetUInt32( regEntry, gIODTPHandleKey, &phandle);
591c1dac77fSApple OSS Distributions 	assert( ok );
592c1dac77fSApple OSS Distributions 
593c1dac77fSApple OSS Distributions 	if (ok) {
5943ca3bd55SApple OSS Distributions 		snprintf(buf, sizeof(buf), "IOInterruptController%08X", (uint32_t)phandle);
595c1dac77fSApple OSS Distributions 		sym = OSSymbol::withCString( buf );
596a5e72196SApple OSS Distributions 	} else {
597a5e72196SApple OSS Distributions 		sym = NULL;
598a5e72196SApple OSS Distributions 	}
599c1dac77fSApple OSS Distributions 
600a5e72196SApple OSS Distributions 	return sym;
601c1dac77fSApple OSS Distributions }
602c1dac77fSApple OSS Distributions 
603c1dac77fSApple OSS Distributions #define unexpected(a) { kprintf("unexpected %s:%d\n", __FILE__, __LINE__); a; }
604c1dac77fSApple OSS Distributions 
605a5e72196SApple OSS Distributions static void
IODTGetICellCounts(IORegistryEntry * regEntry,UInt32 * iCellCount,UInt32 * aCellCount)606a5e72196SApple OSS Distributions IODTGetICellCounts( IORegistryEntry * regEntry,
607c1dac77fSApple OSS Distributions     UInt32 * iCellCount, UInt32 * aCellCount)
608c1dac77fSApple OSS Distributions {
609a5e72196SApple OSS Distributions 	if (!GetUInt32( regEntry, gIODTInterruptCellKey, iCellCount)) {
610c1dac77fSApple OSS Distributions 		unexpected( *iCellCount = 1 );
611a5e72196SApple OSS Distributions 	}
612a5e72196SApple OSS Distributions 	if (!GetUInt32( regEntry, gIODTAddressCellKey, aCellCount)) {
613c1dac77fSApple OSS Distributions 		*aCellCount = 0;
614c1dac77fSApple OSS Distributions 	}
615a5e72196SApple OSS Distributions }
616c1dac77fSApple OSS Distributions 
617a5e72196SApple OSS Distributions static UInt32
IODTMapOneInterrupt(IORegistryEntry * regEntry,UInt32 * intSpec,UInt32 index,LIBKERN_RETURNS_RETAINED OSData ** spec,LIBKERN_RETURNS_RETAINED const OSSymbol ** controller)618a5e72196SApple OSS Distributions IODTMapOneInterrupt( IORegistryEntry * regEntry, UInt32 * intSpec, UInt32 index,
619a5e72196SApple OSS Distributions     LIBKERN_RETURNS_RETAINED OSData ** spec,
620a5e72196SApple OSS Distributions     LIBKERN_RETURNS_RETAINED const OSSymbol ** controller )
621c1dac77fSApple OSS Distributions {
622a5e72196SApple OSS Distributions 	IORegistryEntry *parent = NULL;
623c1dac77fSApple OSS Distributions 	OSData                      *data;
624c1dac77fSApple OSS Distributions 	UInt32                      *addrCmp;
625c1dac77fSApple OSS Distributions 	UInt32                      *maskCmp;
626c1dac77fSApple OSS Distributions 	UInt32                      *map;
627c1dac77fSApple OSS Distributions 	UInt32                      *endMap;
628c1dac77fSApple OSS Distributions 	UInt32                      acells, icells, pacells, picells, cell;
6298149afccSApple OSS Distributions 	UInt32                      i, original_icells;
630c1dac77fSApple OSS Distributions 	bool                        cmp, ok = false;
631c1dac77fSApple OSS Distributions 
6323ca3bd55SApple OSS Distributions 	parent = IODTFindInterruptParent( regEntry, index );
6338149afccSApple OSS Distributions 	IODTGetICellCounts( parent, &icells, &acells );
634a5e72196SApple OSS Distributions 	addrCmp = NULL;
6358149afccSApple OSS Distributions 	if (acells) {
6368149afccSApple OSS Distributions 		data = OSDynamicCast( OSData, regEntry->getProperty( "reg" ));
637a5e72196SApple OSS Distributions 		if (data && (data->getLength() >= (acells * sizeof(UInt32)))) {
6388149afccSApple OSS Distributions 			addrCmp = (UInt32 *) data->getBytesNoCopy();
6398149afccSApple OSS Distributions 		}
640a5e72196SApple OSS Distributions 	}
6418149afccSApple OSS Distributions 	original_icells = icells;
6428149afccSApple OSS Distributions 	regEntry = parent;
6438149afccSApple OSS Distributions 
644c1dac77fSApple OSS Distributions 	do {
6458149afccSApple OSS Distributions #if IODTSUPPORTDEBUG
6468149afccSApple OSS Distributions 		kprintf("IODTMapOneInterrupt: current regEntry name %s\n", regEntry->getName());
6478149afccSApple OSS Distributions 		kprintf("acells - icells: ");
648a5e72196SApple OSS Distributions 		for (i = 0; i < acells; i++) {
649a5e72196SApple OSS Distributions 			kprintf("0x%08X ", addrCmp[i]);
650a5e72196SApple OSS Distributions 		}
6518149afccSApple OSS Distributions 		kprintf("- ");
652a5e72196SApple OSS Distributions 		for (i = 0; i < icells; i++) {
653a5e72196SApple OSS Distributions 			kprintf("0x%08X ", intSpec[i]);
654a5e72196SApple OSS Distributions 		}
6558149afccSApple OSS Distributions 		kprintf("\n");
6568149afccSApple OSS Distributions #endif
6578149afccSApple OSS Distributions 
658c1dac77fSApple OSS Distributions 		if (parent && (data = OSDynamicCast( OSData,
659c1dac77fSApple OSS Distributions 		    regEntry->getProperty( "interrupt-controller")))) {
660c1dac77fSApple OSS Distributions 			// found a controller - don't want to follow cascaded controllers
661a5e72196SApple OSS Distributions 			parent = NULL;
662c1dac77fSApple OSS Distributions 			*spec = OSData::withBytesNoCopy((void *) intSpec,
663c1dac77fSApple OSS Distributions 			    icells * sizeof(UInt32));
664c1dac77fSApple OSS Distributions 			*controller = IODTInterruptControllerName( regEntry );
665c1dac77fSApple OSS Distributions 			ok = (*spec && *controller);
666c1dac77fSApple OSS Distributions 		} else if (parent && (data = OSDynamicCast( OSData,
667c1dac77fSApple OSS Distributions 		    regEntry->getProperty( "interrupt-map")))) {
668c1dac77fSApple OSS Distributions 			// interrupt-map
669c1dac77fSApple OSS Distributions 			map = (UInt32 *) data->getBytesNoCopy();
670c1dac77fSApple OSS Distributions 			endMap = map + (data->getLength() / sizeof(UInt32));
671c1dac77fSApple OSS Distributions 			data = OSDynamicCast( OSData, regEntry->getProperty( "interrupt-map-mask" ));
672a5e72196SApple OSS Distributions 			if (data && (data->getLength() >= ((acells + icells) * sizeof(UInt32)))) {
673c1dac77fSApple OSS Distributions 				maskCmp = (UInt32 *) data->getBytesNoCopy();
674a5e72196SApple OSS Distributions 			} else {
675a5e72196SApple OSS Distributions 				maskCmp = NULL;
676a5e72196SApple OSS Distributions 			}
677c1dac77fSApple OSS Distributions 
6788149afccSApple OSS Distributions #if IODTSUPPORTDEBUG
6798149afccSApple OSS Distributions 			if (maskCmp) {
6808149afccSApple OSS Distributions 				kprintf("        maskCmp: ");
6818149afccSApple OSS Distributions 				for (i = 0; i < acells + icells; i++) {
682a5e72196SApple OSS Distributions 					if (i == acells) {
6838149afccSApple OSS Distributions 						kprintf("- ");
684a5e72196SApple OSS Distributions 					}
6858149afccSApple OSS Distributions 					kprintf("0x%08X ", maskCmp[i]);
6868149afccSApple OSS Distributions 				}
6878149afccSApple OSS Distributions 				kprintf("\n");
6888149afccSApple OSS Distributions 				kprintf("         masked: ");
6898149afccSApple OSS Distributions 				for (i = 0; i < acells + icells; i++) {
690a5e72196SApple OSS Distributions 					if (i == acells) {
6918149afccSApple OSS Distributions 						kprintf("- ");
692a5e72196SApple OSS Distributions 					}
6938149afccSApple OSS Distributions 					kprintf("0x%08X ", ((i < acells) ? addrCmp[i] : intSpec[i - acells]) & maskCmp[i]);
6948149afccSApple OSS Distributions 				}
6958149afccSApple OSS Distributions 				kprintf("\n");
696a5e72196SApple OSS Distributions 			} else {
6978149afccSApple OSS Distributions 				kprintf("no maskCmp\n");
698a5e72196SApple OSS Distributions 			}
6998149afccSApple OSS Distributions #endif
700c1dac77fSApple OSS Distributions 			do {
7018149afccSApple OSS Distributions #if IODTSUPPORTDEBUG
7028149afccSApple OSS Distributions 				kprintf("            map: ");
7038149afccSApple OSS Distributions 				for (i = 0; i < acells + icells; i++) {
704a5e72196SApple OSS Distributions 					if (i == acells) {
7058149afccSApple OSS Distributions 						kprintf("- ");
706a5e72196SApple OSS Distributions 					}
7078149afccSApple OSS Distributions 					kprintf("0x%08X ", map[i]);
7088149afccSApple OSS Distributions 				}
7098149afccSApple OSS Distributions 				kprintf("\n");
7108149afccSApple OSS Distributions #endif
7118149afccSApple OSS Distributions 				for (i = 0, cmp = true; cmp && (i < (acells + icells)); i++) {
712c1dac77fSApple OSS Distributions 					cell = (i < acells) ? addrCmp[i] : intSpec[i - acells];
713a5e72196SApple OSS Distributions 					if (maskCmp) {
714c1dac77fSApple OSS Distributions 						cell &= maskCmp[i];
715a5e72196SApple OSS Distributions 					}
716c1dac77fSApple OSS Distributions 					cmp = (cell == map[i]);
717c1dac77fSApple OSS Distributions 				}
718c1dac77fSApple OSS Distributions 
719c1dac77fSApple OSS Distributions 				map += acells + icells;
720a5e72196SApple OSS Distributions 				if (NULL == (parent = FindPHandle( *(map++)))) {
721c1dac77fSApple OSS Distributions 					unexpected(break);
722a5e72196SApple OSS Distributions 				}
723c1dac77fSApple OSS Distributions 
724c1dac77fSApple OSS Distributions 				IODTGetICellCounts( parent, &picells, &pacells );
7258149afccSApple OSS Distributions 				if (cmp) {
7268149afccSApple OSS Distributions 					addrCmp = map;
7278149afccSApple OSS Distributions 					intSpec = map + pacells;
7288149afccSApple OSS Distributions 					regEntry = parent;
7298149afccSApple OSS Distributions 				} else {
730c1dac77fSApple OSS Distributions 					map += pacells + picells;
731c1dac77fSApple OSS Distributions 				}
732c1dac77fSApple OSS Distributions 			} while (!cmp && (map < endMap));
733a5e72196SApple OSS Distributions 			if (!cmp) {
734a5e72196SApple OSS Distributions 				parent = NULL;
735a5e72196SApple OSS Distributions 			}
7368149afccSApple OSS Distributions 		}
737c1dac77fSApple OSS Distributions 
738c1dac77fSApple OSS Distributions 		if (parent) {
739c1dac77fSApple OSS Distributions 			IODTGetICellCounts( parent, &icells, &acells );
740c1dac77fSApple OSS Distributions 			regEntry = parent;
741c1dac77fSApple OSS Distributions 		}
742c1dac77fSApple OSS Distributions 	} while (parent);
743c1dac77fSApple OSS Distributions 
744a5e72196SApple OSS Distributions 	return ok ? original_icells : 0;
745c1dac77fSApple OSS Distributions }
746c1dac77fSApple OSS Distributions 
747a5e72196SApple OSS Distributions IOReturn
IODTGetInterruptOptions(IORegistryEntry * regEntry,int source,IOOptionBits * options)748a5e72196SApple OSS Distributions IODTGetInterruptOptions( IORegistryEntry * regEntry, int source, IOOptionBits * options )
74914e3d835SApple OSS Distributions {
75014e3d835SApple OSS Distributions 	OSArray *   controllers;
75114e3d835SApple OSS Distributions 	OSArray *   specifiers;
75214e3d835SApple OSS Distributions 	OSArray *   shared;
75314e3d835SApple OSS Distributions 	OSObject *  spec;
75414e3d835SApple OSS Distributions 	OSObject *  oneSpec;
75514e3d835SApple OSS Distributions 
75614e3d835SApple OSS Distributions 	*options = 0;
75714e3d835SApple OSS Distributions 
75814e3d835SApple OSS Distributions 	controllers = OSDynamicCast(OSArray, regEntry->getProperty(gIOInterruptControllersKey));
75914e3d835SApple OSS Distributions 	specifiers  = OSDynamicCast(OSArray, regEntry->getProperty(gIOInterruptSpecifiersKey));
76014e3d835SApple OSS Distributions 
761a5e72196SApple OSS Distributions 	if (!controllers || !specifiers) {
762a5e72196SApple OSS Distributions 		return kIOReturnNoInterrupt;
763a5e72196SApple OSS Distributions 	}
76414e3d835SApple OSS Distributions 
76514e3d835SApple OSS Distributions 	shared = (OSArray *) gIODTSharedInterrupts->getObject(
76614e3d835SApple OSS Distributions 		(const OSSymbol *) controllers->getObject(source));
767a5e72196SApple OSS Distributions 	if (!shared) {
768a5e72196SApple OSS Distributions 		return kIOReturnSuccess;
769a5e72196SApple OSS Distributions 	}
77014e3d835SApple OSS Distributions 
77114e3d835SApple OSS Distributions 	spec = specifiers->getObject(source);
772a5e72196SApple OSS Distributions 	if (!spec) {
773a5e72196SApple OSS Distributions 		return kIOReturnNoInterrupt;
774a5e72196SApple OSS Distributions 	}
77514e3d835SApple OSS Distributions 
77614e3d835SApple OSS Distributions 	for (unsigned int i = 0;
77714e3d835SApple OSS Distributions 	    (oneSpec = shared->getObject(i))
77814e3d835SApple OSS Distributions 	    && (!oneSpec->isEqualTo(spec));
779a5e72196SApple OSS Distributions 	    i++) {
78014e3d835SApple OSS Distributions 	}
78114e3d835SApple OSS Distributions 
782a5e72196SApple OSS Distributions 	if (oneSpec) {
783a5e72196SApple OSS Distributions 		*options = kIODTInterruptShared;
784a5e72196SApple OSS Distributions 	}
785a5e72196SApple OSS Distributions 
786a5e72196SApple OSS Distributions 	return kIOReturnSuccess;
787a5e72196SApple OSS Distributions }
788a5e72196SApple OSS Distributions 
789a5e72196SApple OSS Distributions static bool
IODTMapInterruptsSharing(IORegistryEntry * regEntry,OSDictionary * allInts)790a5e72196SApple OSS Distributions IODTMapInterruptsSharing( IORegistryEntry * regEntry, OSDictionary * allInts )
791c1dac77fSApple OSS Distributions {
792c1dac77fSApple OSS Distributions 	IORegistryEntry *   parent;
793c1dac77fSApple OSS Distributions 	OSData *            local;
794c1dac77fSApple OSS Distributions 	OSData *            local2;
795c1dac77fSApple OSS Distributions 	UInt32 *            localBits;
796c1dac77fSApple OSS Distributions 	UInt32 *            localEnd;
7973ca3bd55SApple OSS Distributions 	IOItemCount         index;
798e6231be0SApple OSS Distributions 	OSData *            map = NULL;
79914e3d835SApple OSS Distributions 	OSObject *          oneMap;
800c1dac77fSApple OSS Distributions 	OSArray *           mapped;
80114e3d835SApple OSS Distributions 	OSArray *           controllerInts;
802a5e72196SApple OSS Distributions 	const OSSymbol *    controller = NULL;
803c1dac77fSApple OSS Distributions 	OSArray *           controllers;
804c1dac77fSApple OSS Distributions 	UInt32              skip = 1;
805c1dac77fSApple OSS Distributions 	bool                ok, nw;
806c1dac77fSApple OSS Distributions 
807a5e72196SApple OSS Distributions 	nw = (NULL == (local = OSDynamicCast( OSData,
808c1dac77fSApple OSS Distributions 	    regEntry->getProperty( gIODTAAPLInterruptsKey))));
809a5e72196SApple OSS Distributions 	if (nw && (NULL == (local = OSDynamicCast( OSData,
810a5e72196SApple OSS Distributions 	    regEntry->getProperty( "interrupts"))))) {
811a5e72196SApple OSS Distributions 		return true;  // nothing to see here
812a5e72196SApple OSS Distributions 	}
813c1dac77fSApple OSS Distributions 	if (nw && (parent = regEntry->getParentEntry( gIODTPlane))) {
814c1dac77fSApple OSS Distributions 		// check for bridges on old world
815c1dac77fSApple OSS Distributions 		if ((local2 = OSDynamicCast( OSData,
816c1dac77fSApple OSS Distributions 		    parent->getProperty( gIODTAAPLInterruptsKey)))) {
817c1dac77fSApple OSS Distributions 			local = local2;
818c1dac77fSApple OSS Distributions 			nw = false;
819c1dac77fSApple OSS Distributions 		}
820c1dac77fSApple OSS Distributions 	}
821c1dac77fSApple OSS Distributions 
822c1dac77fSApple OSS Distributions 	localBits = (UInt32 *) local->getBytesNoCopy();
823c1dac77fSApple OSS Distributions 	localEnd = localBits + (local->getLength() / sizeof(UInt32));
8243ca3bd55SApple OSS Distributions 	index = 0;
825c1dac77fSApple OSS Distributions 	mapped = OSArray::withCapacity( 1 );
826c1dac77fSApple OSS Distributions 	controllers = OSArray::withCapacity( 1 );
827c1dac77fSApple OSS Distributions 
828c1dac77fSApple OSS Distributions 	ok = (mapped && controllers);
829c1dac77fSApple OSS Distributions 
830a5e72196SApple OSS Distributions 	if (ok) {
831a5e72196SApple OSS Distributions 		do {
832c1dac77fSApple OSS Distributions 			if (nw) {
8333ca3bd55SApple OSS Distributions 				skip = IODTMapOneInterrupt( regEntry, localBits, index, &map, &controller );
834c1dac77fSApple OSS Distributions 				if (0 == skip) {
835c1dac77fSApple OSS Distributions 					IOLog("%s: error mapping interrupt[%d]\n",
836c1dac77fSApple OSS Distributions 					    regEntry->getName(), mapped->getCount());
837e6231be0SApple OSS Distributions 					OSSafeReleaseNULL(map);
838e6231be0SApple OSS Distributions 					OSSafeReleaseNULL(controller);
839c1dac77fSApple OSS Distributions 					break;
840c1dac77fSApple OSS Distributions 				}
841c1dac77fSApple OSS Distributions 			} else {
842c1dac77fSApple OSS Distributions 				map = OSData::withData( local, mapped->getCount() * sizeof(UInt32),
843c1dac77fSApple OSS Distributions 				    sizeof(UInt32));
844c1dac77fSApple OSS Distributions 				controller = gIODTDefaultInterruptController;
845fad439e7SApple OSS Distributions 				controller->retain();
846c1dac77fSApple OSS Distributions 			}
847c1dac77fSApple OSS Distributions 
8483ca3bd55SApple OSS Distributions 			index++;
849c1dac77fSApple OSS Distributions 			localBits += skip;
850c1dac77fSApple OSS Distributions 			mapped->setObject( map );
85114e3d835SApple OSS Distributions 			controllers->setObject( controller );
85214e3d835SApple OSS Distributions 
853a5e72196SApple OSS Distributions 			if (allInts) {
85414e3d835SApple OSS Distributions 				controllerInts = (OSArray *) allInts->getObject( controller );
855a5e72196SApple OSS Distributions 				if (controllerInts) {
856a5e72196SApple OSS Distributions 					for (unsigned int i = 0; (oneMap = controllerInts->getObject(i)); i++) {
857a5e72196SApple OSS Distributions 						if (map->isEqualTo(oneMap)) {
85814e3d835SApple OSS Distributions 							controllerInts = (OSArray *) gIODTSharedInterrupts->getObject( controller );
859a5e72196SApple OSS Distributions 							if (controllerInts) {
86014e3d835SApple OSS Distributions 								controllerInts->setObject(map);
861a5e72196SApple OSS Distributions 							} else {
86214e3d835SApple OSS Distributions 								controllerInts = OSArray::withObjects((const OSObject **) &map, 1, 4 );
863a5e72196SApple OSS Distributions 								if (controllerInts) {
86414e3d835SApple OSS Distributions 									gIODTSharedInterrupts->setObject( controller, controllerInts );
86514e3d835SApple OSS Distributions 									controllerInts->release();
86614e3d835SApple OSS Distributions 								}
86714e3d835SApple OSS Distributions 							}
86814e3d835SApple OSS Distributions 							break;
86914e3d835SApple OSS Distributions 						}
87014e3d835SApple OSS Distributions 					}
871a5e72196SApple OSS Distributions 					if (!oneMap) {
87214e3d835SApple OSS Distributions 						controllerInts->setObject(map);
87314e3d835SApple OSS Distributions 					}
874a5e72196SApple OSS Distributions 				} else {
87514e3d835SApple OSS Distributions 					controllerInts = OSArray::withObjects((const OSObject **) &map, 1, 16 );
876a5e72196SApple OSS Distributions 					if (controllerInts) {
87714e3d835SApple OSS Distributions 						allInts->setObject( controller, controllerInts );
87814e3d835SApple OSS Distributions 						controllerInts->release();
87914e3d835SApple OSS Distributions 					}
88014e3d835SApple OSS Distributions 				}
88114e3d835SApple OSS Distributions 			}
88214e3d835SApple OSS Distributions 
883e6231be0SApple OSS Distributions 			OSSafeReleaseNULL(map);
884e6231be0SApple OSS Distributions 			OSSafeReleaseNULL(controller);
885c1dac77fSApple OSS Distributions 		} while (localBits < localEnd);
886a5e72196SApple OSS Distributions 	}
887c1dac77fSApple OSS Distributions 
888c1dac77fSApple OSS Distributions 	ok &= (localBits == localEnd);
889c1dac77fSApple OSS Distributions 
890c1dac77fSApple OSS Distributions 	if (ok) {
891c1dac77fSApple OSS Distributions 		// store results
892c1dac77fSApple OSS Distributions 		ok  = regEntry->setProperty( gIOInterruptControllersKey, controllers);
893c1dac77fSApple OSS Distributions 		ok &= regEntry->setProperty( gIOInterruptSpecifiersKey, mapped);
894c1dac77fSApple OSS Distributions 	}
895c1dac77fSApple OSS Distributions 
896a5e72196SApple OSS Distributions 	if (controllers) {
897c1dac77fSApple OSS Distributions 		controllers->release();
898a5e72196SApple OSS Distributions 	}
899a5e72196SApple OSS Distributions 	if (mapped) {
900c1dac77fSApple OSS Distributions 		mapped->release();
901c1dac77fSApple OSS Distributions 	}
902c1dac77fSApple OSS Distributions 
903a5e72196SApple OSS Distributions 	return ok;
904a5e72196SApple OSS Distributions }
905a5e72196SApple OSS Distributions 
906a5e72196SApple OSS Distributions bool
IODTMapInterrupts(IORegistryEntry * regEntry)907a5e72196SApple OSS Distributions IODTMapInterrupts( IORegistryEntry * regEntry )
90814e3d835SApple OSS Distributions {
909a5e72196SApple OSS Distributions 	return IODTMapInterruptsSharing( regEntry, NULL );
91014e3d835SApple OSS Distributions }
91114e3d835SApple OSS Distributions 
912c1dac77fSApple OSS Distributions /*
913c1dac77fSApple OSS Distributions  */
914c1dac77fSApple OSS Distributions 
91588cc0b97SApple OSS Distributions static bool
CompareKey(OSString * key,const IORegistryEntry * table,const OSSymbol * propName,LIBKERN_RETURNS_RETAINED OSString ** matchingName)916c1dac77fSApple OSS Distributions CompareKey( OSString * key,
91788cc0b97SApple OSS Distributions     const IORegistryEntry * table, const OSSymbol * propName,
918a5e72196SApple OSS Distributions     LIBKERN_RETURNS_RETAINED OSString ** matchingName )
919c1dac77fSApple OSS Distributions {
920c1dac77fSApple OSS Distributions 	OSObject            *prop;
921c1dac77fSApple OSS Distributions 	OSData                      *data;
922c1dac77fSApple OSS Distributions 	OSString            *string;
923c1dac77fSApple OSS Distributions 	const char          *ckey;
924c1dac77fSApple OSS Distributions 	UInt32                      keyLen;
925a3bb9fccSApple OSS Distributions 	UInt32          nlen;
926c1dac77fSApple OSS Distributions 	const char          *names;
927c1dac77fSApple OSS Distributions 	const char          *lastName;
928c1dac77fSApple OSS Distributions 	bool                        wild;
929c1dac77fSApple OSS Distributions 	bool                        matched;
930a5e72196SApple OSS Distributions 	const char          *result = NULL;
931c1dac77fSApple OSS Distributions 
932a5e72196SApple OSS Distributions 	if (NULL == (prop = table->copyProperty( propName ))) {
933a5e72196SApple OSS Distributions 		return 0;
934a5e72196SApple OSS Distributions 	}
935c1dac77fSApple OSS Distributions 
936c1dac77fSApple OSS Distributions 	if ((data = OSDynamicCast( OSData, prop ))) {
937c1dac77fSApple OSS Distributions 		names = (const char *) data->getBytesNoCopy();
938c1dac77fSApple OSS Distributions 		lastName = names + data->getLength();
939c1dac77fSApple OSS Distributions 	} else if ((string = OSDynamicCast( OSString, prop ))) {
940c1dac77fSApple OSS Distributions 		names = string->getCStringNoCopy();
941c1dac77fSApple OSS Distributions 		lastName = names + string->getLength() + 1;
942a5e72196SApple OSS Distributions 	} else {
943a5e72196SApple OSS Distributions 		names = NULL;
944a5e72196SApple OSS Distributions 	}
945c1dac77fSApple OSS Distributions 
94688cc0b97SApple OSS Distributions 	if (names) {
947c1dac77fSApple OSS Distributions 		ckey = key->getCStringNoCopy();
948c1dac77fSApple OSS Distributions 		keyLen = key->getLength();
949c1dac77fSApple OSS Distributions 		wild = ('*' == key->getChar( keyLen - 1 ));
950c1dac77fSApple OSS Distributions 
951c1dac77fSApple OSS Distributions 		do {
952c1dac77fSApple OSS Distributions 			// for each name in the property
953bb611c8fSApple OSS Distributions 			nlen = (unsigned int) strnlen(names, lastName - names);
954a5e72196SApple OSS Distributions 			if (wild) {
955a3bb9fccSApple OSS Distributions 				matched = ((nlen >= (keyLen - 1)) && (0 == strncmp(ckey, names, keyLen - 1)));
956a5e72196SApple OSS Distributions 			} else {
957a3bb9fccSApple OSS Distributions 				matched = (keyLen == nlen) && (0 == strncmp(ckey, names, keyLen));
958a5e72196SApple OSS Distributions 			}
959c1dac77fSApple OSS Distributions 
960a5e72196SApple OSS Distributions 			if (matched) {
961c1dac77fSApple OSS Distributions 				result = names;
962a5e72196SApple OSS Distributions 			}
963c1dac77fSApple OSS Distributions 
964a3bb9fccSApple OSS Distributions 			names = names + nlen + 1;
965c1dac77fSApple OSS Distributions 		} while ((names < lastName) && (false == matched));
96688cc0b97SApple OSS Distributions 	}
967c1dac77fSApple OSS Distributions 
968a5e72196SApple OSS Distributions 	if (result && matchingName) {
969a5e72196SApple OSS Distributions 		*matchingName = OSString::withCString( result );
970a5e72196SApple OSS Distributions 	}
97188cc0b97SApple OSS Distributions 
972a5e72196SApple OSS Distributions 	if (prop) {
973a5e72196SApple OSS Distributions 		prop->release();
974a5e72196SApple OSS Distributions 	}
97588cc0b97SApple OSS Distributions 
976a5e72196SApple OSS Distributions 	return result != NULL;
977c1dac77fSApple OSS Distributions }
978c1dac77fSApple OSS Distributions 
979c1dac77fSApple OSS Distributions 
980a5e72196SApple OSS Distributions bool
IODTCompareNubName(const IORegistryEntry * regEntry,OSString * name,OSString ** matchingName)981a5e72196SApple OSS Distributions IODTCompareNubName( const IORegistryEntry * regEntry,
982c1dac77fSApple OSS Distributions     OSString * name, OSString ** matchingName )
983c1dac77fSApple OSS Distributions {
984c1dac77fSApple OSS Distributions 	bool matched;
985c1dac77fSApple OSS Distributions 
98688cc0b97SApple OSS Distributions 	matched = CompareKey( name, regEntry, gIODTNameKey, matchingName)
98788cc0b97SApple OSS Distributions 	    || CompareKey( name, regEntry, gIODTCompatibleKey, matchingName)
98888cc0b97SApple OSS Distributions 	    || CompareKey( name, regEntry, gIODTTypeKey, matchingName)
98988cc0b97SApple OSS Distributions 	    || CompareKey( name, regEntry, gIODTModelKey, matchingName);
990c1dac77fSApple OSS Distributions 
991a5e72196SApple OSS Distributions 	return matched;
992c1dac77fSApple OSS Distributions }
993c1dac77fSApple OSS Distributions 
994a5e72196SApple OSS Distributions bool
IODTCompareNubName(const IORegistryEntry * regEntry,OSString * name,OSSharedPtr<OSString> & matchingName)995bb611c8fSApple OSS Distributions IODTCompareNubName( const IORegistryEntry * regEntry,
996bb611c8fSApple OSS Distributions     OSString * name, OSSharedPtr<OSString>& matchingName )
997bb611c8fSApple OSS Distributions {
998bb611c8fSApple OSS Distributions 	OSString* matchingNameRaw = NULL;
999bb611c8fSApple OSS Distributions 	bool result = IODTCompareNubName(regEntry, name, &matchingNameRaw);
1000bb611c8fSApple OSS Distributions 	matchingName.reset(matchingNameRaw, OSNoRetain);
1001bb611c8fSApple OSS Distributions 	return result;
1002bb611c8fSApple OSS Distributions }
1003bb611c8fSApple OSS Distributions 
1004bb611c8fSApple OSS Distributions bool
IODTMatchNubWithKeys(IORegistryEntry * regEntry,const char * keys)1005a5e72196SApple OSS Distributions IODTMatchNubWithKeys( IORegistryEntry * regEntry,
1006c1dac77fSApple OSS Distributions     const char * keys )
1007c1dac77fSApple OSS Distributions {
1008c1dac77fSApple OSS Distributions 	OSObject    *obj;
1009c1dac77fSApple OSS Distributions 	bool                result = false;
1010c1dac77fSApple OSS Distributions 
1011a5e72196SApple OSS Distributions 	obj = OSUnserialize( keys, NULL );
1012c1dac77fSApple OSS Distributions 
1013c1dac77fSApple OSS Distributions 	if (obj) {
1014c1dac77fSApple OSS Distributions 		result = regEntry->compareNames( obj );
1015c1dac77fSApple OSS Distributions 		obj->release();
1016c1dac77fSApple OSS Distributions 	}
10173ca3bd55SApple OSS Distributions #if DEBUG
1018a5e72196SApple OSS Distributions 	else {
1019a5e72196SApple OSS Distributions 		IOLog("Couldn't unserialize %s\n", keys );
1020a5e72196SApple OSS Distributions 	}
1021c1dac77fSApple OSS Distributions #endif
1022c1dac77fSApple OSS Distributions 
1023a5e72196SApple OSS Distributions 	return result;
1024c1dac77fSApple OSS Distributions }
1025c1dac77fSApple OSS Distributions 
10261031c584SApple OSS Distributions LIBKERN_RETURNS_RETAINED OSCollectionIterator *
IODTFindMatchingEntries(IORegistryEntry * from,IOOptionBits options,const char * keys)1027a5e72196SApple OSS Distributions IODTFindMatchingEntries( IORegistryEntry * from,
1028c1dac77fSApple OSS Distributions     IOOptionBits options, const char * keys )
1029c1dac77fSApple OSS Distributions {
1030a5e72196SApple OSS Distributions 	OSSet                                       *result = NULL;
1031c1dac77fSApple OSS Distributions 	IORegistryEntry                     *next;
1032c1dac77fSApple OSS Distributions 	IORegistryIterator          *iter;
1033c1dac77fSApple OSS Distributions 	OSCollectionIterator        *cIter;
1034c1dac77fSApple OSS Distributions 	bool                                        cmp;
1035c1dac77fSApple OSS Distributions 	bool                                        minus = options & kIODTExclusive;
1036c1dac77fSApple OSS Distributions 
1037c1dac77fSApple OSS Distributions 
1038c1dac77fSApple OSS Distributions 	iter = IORegistryIterator::iterateOver( from, gIODTPlane,
1039c1dac77fSApple OSS Distributions 	    (options & kIODTRecursive) ? kIORegistryIterateRecursively : 0 );
1040c1dac77fSApple OSS Distributions 	if (iter) {
1041fad439e7SApple OSS Distributions 		do {
1042a5e72196SApple OSS Distributions 			if (result) {
1043fad439e7SApple OSS Distributions 				result->release();
1044a5e72196SApple OSS Distributions 			}
1045fad439e7SApple OSS Distributions 			result = OSSet::withCapacity( 3 );
1046a5e72196SApple OSS Distributions 			if (!result) {
1047fad439e7SApple OSS Distributions 				break;
1048a5e72196SApple OSS Distributions 			}
1049fad439e7SApple OSS Distributions 
1050fad439e7SApple OSS Distributions 			iter->reset();
1051c1dac77fSApple OSS Distributions 			while ((next = iter->getNextObject())) {
1052c1dac77fSApple OSS Distributions 				// Look for existence of a debug property to skip
10531031c584SApple OSS Distributions 				if (next->propertyExists("AAPL,ignore")) {
1054c1dac77fSApple OSS Distributions 					continue;
1055a5e72196SApple OSS Distributions 				}
10561031c584SApple OSS Distributions 				if (next->propertyHasValue(gIODTTypeKey, gIODTAssociatedServiceKey)) {
10571031c584SApple OSS Distributions 					continue;
10581031c584SApple OSS Distributions 				}
1059c1dac77fSApple OSS Distributions 				if (keys) {
1060c1dac77fSApple OSS Distributions 					cmp = IODTMatchNubWithKeys( next, keys );
1061c1dac77fSApple OSS Distributions 					if ((minus && (false == cmp))
1062a5e72196SApple OSS Distributions 					    || ((false == minus) && (false != cmp))) {
1063c1dac77fSApple OSS Distributions 						result->setObject( next);
1064a5e72196SApple OSS Distributions 					}
1065a5e72196SApple OSS Distributions 				} else {
1066c1dac77fSApple OSS Distributions 					result->setObject( next);
1067c1dac77fSApple OSS Distributions 				}
1068a5e72196SApple OSS Distributions 			}
1069fad439e7SApple OSS Distributions 		} while (!iter->isValid());
1070fad439e7SApple OSS Distributions 
1071c1dac77fSApple OSS Distributions 		iter->release();
1072c1dac77fSApple OSS Distributions 	}
1073c1dac77fSApple OSS Distributions 
1074c1dac77fSApple OSS Distributions 	cIter = OSCollectionIterator::withCollection( result);
1075a5e72196SApple OSS Distributions 	if (result) {
1076a5e72196SApple OSS Distributions 		result->release();
1077a5e72196SApple OSS Distributions 	}
1078c1dac77fSApple OSS Distributions 
1079a5e72196SApple OSS Distributions 	return cIter;
1080c1dac77fSApple OSS Distributions }
1081c1dac77fSApple OSS Distributions 
1082c1dac77fSApple OSS Distributions 
1083a5e72196SApple OSS Distributions void
IODTSetResolving(IORegistryEntry * regEntry,IODTCompareAddressCellFunc compareFunc,IODTNVLocationFunc locationFunc __unused)1084a5e72196SApple OSS Distributions IODTSetResolving( IORegistryEntry *        regEntry,
1085c1dac77fSApple OSS Distributions     IODTCompareAddressCellFunc      compareFunc,
108676e12aa3SApple OSS Distributions     IODTNVLocationFunc              locationFunc __unused )
1087c1dac77fSApple OSS Distributions {
108876e12aa3SApple OSS Distributions 	IODTPersistent * entry;
1089e6231be0SApple OSS Distributions 	IODTPersistent * newResolvers;
109076e12aa3SApple OSS Distributions 	OSNumber       * num;
1091e6231be0SApple OSS Distributions 	unsigned int     index;
1092c1dac77fSApple OSS Distributions 
1093e6231be0SApple OSS Distributions 	IOLockLock(gIODTResolvers->lock);
109476e12aa3SApple OSS Distributions 
1095e6231be0SApple OSS Distributions 	entry = gIODTResolvers->resolvers;
1096e6231be0SApple OSS Distributions 	for (index = 0; index < gIODTResolvers->count; index++) {
1097a5e72196SApple OSS Distributions 		if (compareFunc == entry->compareFunc) {
1098a5e72196SApple OSS Distributions 			break;
1099a5e72196SApple OSS Distributions 		}
110076e12aa3SApple OSS Distributions 		entry++;
110176e12aa3SApple OSS Distributions 	}
1102e6231be0SApple OSS Distributions 
1103e6231be0SApple OSS Distributions 	if (index == gIODTResolvers->count) {
1104e6231be0SApple OSS Distributions 		if (gIODTResolvers->alloc == gIODTResolvers->count) {
1105e6231be0SApple OSS Distributions 			if (__improbable(os_mul_overflow(gIODTResolvers->alloc, 2,
1106e6231be0SApple OSS Distributions 			    &gIODTResolvers->alloc))) {
1107e6231be0SApple OSS Distributions 				panic("IODTSetResolving - gIODTResolvers alloc overflows");
110876e12aa3SApple OSS Distributions 			}
1109c1dac77fSApple OSS Distributions 
1110e6231be0SApple OSS Distributions 			newResolvers = IONewZero(IODTPersistent, gIODTResolvers->alloc);
1111e6231be0SApple OSS Distributions 			if (__improbable(!newResolvers)) {
1112e6231be0SApple OSS Distributions 				panic("IODTSetResolving - could not allocate new resolvers");
1113e6231be0SApple OSS Distributions 			}
1114e6231be0SApple OSS Distributions 
1115e6231be0SApple OSS Distributions 			bcopy(gIODTResolvers->resolvers, newResolvers,
1116e6231be0SApple OSS Distributions 			    sizeof(gIODTResolvers->resolvers[0]) * gIODTResolvers->count);
1117e6231be0SApple OSS Distributions 
1118e6231be0SApple OSS Distributions 			IODelete(gIODTResolvers->resolvers, IODTPersistent,
1119e6231be0SApple OSS Distributions 			    gIODTResolvers->count);
1120e6231be0SApple OSS Distributions 			gIODTResolvers->resolvers = newResolvers;
1121e6231be0SApple OSS Distributions 		}
1122e6231be0SApple OSS Distributions 
1123e6231be0SApple OSS Distributions 		entry = &gIODTResolvers->resolvers[gIODTResolvers->count];
1124e6231be0SApple OSS Distributions 		entry->compareFunc = compareFunc;
1125e6231be0SApple OSS Distributions 		gIODTResolvers->count++;
1126e6231be0SApple OSS Distributions 	}
1127e6231be0SApple OSS Distributions 
1128e6231be0SApple OSS Distributions 	IOLockUnlock(gIODTResolvers->lock);
112976e12aa3SApple OSS Distributions 
113076e12aa3SApple OSS Distributions 	num = OSNumber::withNumber(index, 32);
113176e12aa3SApple OSS Distributions 	regEntry->setProperty(gIODTPersistKey, num);
113276e12aa3SApple OSS Distributions 	OSSafeReleaseNULL(num);
113376e12aa3SApple OSS Distributions 
11348149afccSApple OSS Distributions 	return;
1135c1dac77fSApple OSS Distributions }
1136c1dac77fSApple OSS Distributions 
113776e12aa3SApple OSS Distributions #if  defined(__arm64__)
1138a5e72196SApple OSS Distributions static SInt64
DefaultCompare(UInt32 cellCount,UInt32 left[],UInt32 right[])1139a5e72196SApple OSS Distributions DefaultCompare( UInt32 cellCount, UInt32 left[], UInt32 right[] )
114076e12aa3SApple OSS Distributions {
114176e12aa3SApple OSS Distributions 	SInt64 diff = 0;
114276e12aa3SApple OSS Distributions 
114376e12aa3SApple OSS Distributions 	if (cellCount == 2) {
114476e12aa3SApple OSS Distributions 		diff = IOPhysical32(left[1], left[0]) - IOPhysical32(right[1], right[0]);
114576e12aa3SApple OSS Distributions 	} else if (cellCount == 1) {
114676e12aa3SApple OSS Distributions 		diff = (left[0] - right[0]);
114776e12aa3SApple OSS Distributions 	} else {
114876e12aa3SApple OSS Distributions 		panic("DefaultCompare only knows how to handle 1 or 2 cells.");
114976e12aa3SApple OSS Distributions 	}
115076e12aa3SApple OSS Distributions 
115176e12aa3SApple OSS Distributions 	return diff;
115276e12aa3SApple OSS Distributions }
11535c2921b0SApple OSS Distributions #elif defined(__i386__) || defined(__x86_64__)
1154a5e72196SApple OSS Distributions static SInt32
DefaultCompare(UInt32 cellCount,UInt32 left[],UInt32 right[])1155a5e72196SApple OSS Distributions DefaultCompare( UInt32 cellCount, UInt32 left[], UInt32 right[] )
1156c1dac77fSApple OSS Distributions {
1157c1dac77fSApple OSS Distributions 	cellCount--;
1158a5e72196SApple OSS Distributions 	return left[cellCount] - right[cellCount];
1159c1dac77fSApple OSS Distributions }
1160186b8fceSApple OSS Distributions #else
1161186b8fceSApple OSS Distributions #error Unknown architecture.
1162186b8fceSApple OSS Distributions #endif
1163186b8fceSApple OSS Distributions 
1164a5e72196SApple OSS Distributions static void
AddLengthToCells(UInt32 numCells,UInt32 * cells,UInt64 offset)1165a5e72196SApple OSS Distributions AddLengthToCells( UInt32 numCells, UInt32 *cells, UInt64 offset)
1166186b8fceSApple OSS Distributions {
1167a5e72196SApple OSS Distributions 	if (numCells == 1) {
1168186b8fceSApple OSS Distributions 		cells[0] += (UInt32)offset;
1169a5e72196SApple OSS Distributions 	} else {
11705c2921b0SApple OSS Distributions #if defined(__arm64__)
117176e12aa3SApple OSS Distributions 		UInt64 sum = cells[numCells - 2] + offset;
117276e12aa3SApple OSS Distributions 		cells[numCells - 2] = (UInt32)sum;
117376e12aa3SApple OSS Distributions 		if (sum > UINT32_MAX) {
117476e12aa3SApple OSS Distributions 			cells[numCells - 1] += (UInt32)(sum >> 32);
117576e12aa3SApple OSS Distributions 		}
117676e12aa3SApple OSS Distributions #else
1177186b8fceSApple OSS Distributions 		UInt64 sum = cells[numCells - 1] + offset;
1178186b8fceSApple OSS Distributions 		cells[numCells - 1] = (UInt32)sum;
1179186b8fceSApple OSS Distributions 		if (sum > UINT32_MAX) {
1180186b8fceSApple OSS Distributions 			cells[numCells - 2] += (UInt32)(sum >> 32);
1181186b8fceSApple OSS Distributions 		}
118276e12aa3SApple OSS Distributions #endif
1183186b8fceSApple OSS Distributions 	}
1184186b8fceSApple OSS Distributions }
1185186b8fceSApple OSS Distributions 
1186a5e72196SApple OSS Distributions static IOPhysicalAddress
CellsValue(UInt32 numCells,UInt32 * cells)1187a5e72196SApple OSS Distributions CellsValue( UInt32 numCells, UInt32 *cells)
1188186b8fceSApple OSS Distributions {
1189186b8fceSApple OSS Distributions 	if (numCells == 1) {
1190186b8fceSApple OSS Distributions 		return IOPhysical32( 0, cells[0] );
1191186b8fceSApple OSS Distributions 	} else {
119276e12aa3SApple OSS Distributions #if defined(__arm64__) || defined(arm)
119376e12aa3SApple OSS Distributions 		return IOPhysical32( cells[numCells - 1], cells[numCells - 2] );
119476e12aa3SApple OSS Distributions #else
1195186b8fceSApple OSS Distributions 		return IOPhysical32( cells[numCells - 2], cells[numCells - 1] );
119676e12aa3SApple OSS Distributions #endif
1197186b8fceSApple OSS Distributions 	}
1198186b8fceSApple OSS Distributions }
1199c1dac77fSApple OSS Distributions 
1200a5e72196SApple OSS Distributions void
IODTGetCellCounts(IORegistryEntry * regEntry,UInt32 * sizeCount,UInt32 * addressCount)1201a5e72196SApple OSS Distributions IODTGetCellCounts( IORegistryEntry * regEntry,
1202c1dac77fSApple OSS Distributions     UInt32 * sizeCount, UInt32 * addressCount)
1203c1dac77fSApple OSS Distributions {
1204a5e72196SApple OSS Distributions 	if (!GetUInt32( regEntry, gIODTSizeCellKey, sizeCount)) {
1205c1dac77fSApple OSS Distributions 		*sizeCount = 1;
1206a5e72196SApple OSS Distributions 	}
1207a5e72196SApple OSS Distributions 	if (!GetUInt32( regEntry, gIODTAddressCellKey, addressCount)) {
1208c1dac77fSApple OSS Distributions 		*addressCount = 2;
1209a5e72196SApple OSS Distributions 	}
12108149afccSApple OSS Distributions 	return;
1211c1dac77fSApple OSS Distributions }
1212c1dac77fSApple OSS Distributions 
1213c1dac77fSApple OSS Distributions // Given addr & len cells from our child, find it in our ranges property, then
1214c1dac77fSApple OSS Distributions // look in our parent to resolve the base of the range for us.
1215c1dac77fSApple OSS Distributions 
1216c1dac77fSApple OSS Distributions // Range[]: child-addr  our-addr  child-len
1217c1dac77fSApple OSS Distributions // #cells:    child       ours     child
1218c1dac77fSApple OSS Distributions 
1219a5e72196SApple OSS Distributions bool
IODTResolveAddressCell(IORegistryEntry * startEntry,UInt32 cellsIn[],IOPhysicalAddress * phys,IOPhysicalLength * lenOut)1220a5e72196SApple OSS Distributions IODTResolveAddressCell( IORegistryEntry * startEntry,
1221c1dac77fSApple OSS Distributions     UInt32 cellsIn[],
1222186b8fceSApple OSS Distributions     IOPhysicalAddress * phys, IOPhysicalLength * lenOut )
1223c1dac77fSApple OSS Distributions {
1224e6231be0SApple OSS Distributions 	IORegistryEntry     * parent = NULL;
122576e12aa3SApple OSS Distributions 	IORegistryEntry * regEntry;
1226c1dac77fSApple OSS Distributions 	OSData          * prop;
122776e12aa3SApple OSS Distributions 	OSNumber    * num;
1228e6231be0SApple OSS Distributions 	unsigned int  index;
1229c1dac77fSApple OSS Distributions 	// cells in addresses at regEntry
1230c1dac77fSApple OSS Distributions 	UInt32              sizeCells, addressCells;
1231c1dac77fSApple OSS Distributions 	// cells in addresses below regEntry
1232c1dac77fSApple OSS Distributions 	UInt32              childSizeCells, childAddressCells;
1233c1dac77fSApple OSS Distributions 	UInt32              childCells;
1234186b8fceSApple OSS Distributions 	UInt32              cell[8], propLen;
1235d0c1fef6SApple OSS Distributions 	UInt64              offset = 0;
1236d0c1fef6SApple OSS Distributions 	UInt32              endCell[8];
1237c1dac77fSApple OSS Distributions 	UInt32              *range;
1238e13b1fa5SApple OSS Distributions 	UInt32              *lookRange;
1239e13b1fa5SApple OSS Distributions 	UInt32              *startRange;
1240c1dac77fSApple OSS Distributions 	UInt32              *endRanges;
1241c1dac77fSApple OSS Distributions 	bool                ok = true;
1242d0c1fef6SApple OSS Distributions 	SInt64              diff, diff2, endDiff;
1243186b8fceSApple OSS Distributions 	UInt64              len, rangeLen;
1244c1dac77fSApple OSS Distributions 
1245c1dac77fSApple OSS Distributions 	IODTCompareAddressCellFunc  compare;
1246c1dac77fSApple OSS Distributions 
124776e12aa3SApple OSS Distributions 	regEntry = startEntry;
1248e6231be0SApple OSS Distributions 	regEntry->retain();
1249c1dac77fSApple OSS Distributions 	IODTGetCellCounts( regEntry, &childSizeCells, &childAddressCells );
1250c1dac77fSApple OSS Distributions 	childCells = childAddressCells + childSizeCells;
1251c1dac77fSApple OSS Distributions 
1252a5e72196SApple OSS Distributions 	if (childCells > sizeof(cell) / sizeof(cell[0])) {
1253d0c1fef6SApple OSS Distributions 		panic("IODTResolveAddressCell: Invalid device tree (%u,%u)", (uint32_t)childAddressCells, (uint32_t)childSizeCells);
1254a5e72196SApple OSS Distributions 	}
1255d0c1fef6SApple OSS Distributions 
1256e13b1fa5SApple OSS Distributions 	bcopy( cellsIn, cell, sizeof(UInt32) * childCells );
1257186b8fceSApple OSS Distributions 	*lenOut = CellsValue( childSizeCells, cellsIn + childAddressCells );
1258c1dac77fSApple OSS Distributions 
1259a5e72196SApple OSS Distributions 	do{
1260c1dac77fSApple OSS Distributions 		prop = OSDynamicCast( OSData, regEntry->getProperty( gIODTRangeKey ));
1261a5e72196SApple OSS Distributions 		if (NULL == prop) {
1262c1dac77fSApple OSS Distributions 			/* end of the road */
1263186b8fceSApple OSS Distributions 			*phys = CellsValue( childAddressCells, cell );
1264d0c1fef6SApple OSS Distributions 			*phys += offset;
1265c1dac77fSApple OSS Distributions 			break;
1266c1dac77fSApple OSS Distributions 		}
1267c1dac77fSApple OSS Distributions 
126876e12aa3SApple OSS Distributions 		parent = regEntry->copyParentEntry( gIODTPlane );
1269c1dac77fSApple OSS Distributions 		IODTGetCellCounts( parent, &sizeCells, &addressCells );
1270c1dac77fSApple OSS Distributions 
1271186b8fceSApple OSS Distributions 		if ((propLen = prop->getLength())) {
1272c1dac77fSApple OSS Distributions 			// search
1273e13b1fa5SApple OSS Distributions 			startRange = (UInt32 *) prop->getBytesNoCopy();
1274e13b1fa5SApple OSS Distributions 			range = startRange;
1275186b8fceSApple OSS Distributions 			endRanges = range + (propLen / sizeof(UInt32));
1276c1dac77fSApple OSS Distributions 
127776e12aa3SApple OSS Distributions 			compare = NULL;
127876e12aa3SApple OSS Distributions 			num = OSDynamicCast(OSNumber, regEntry->getProperty(gIODTPersistKey));
1279a5e72196SApple OSS Distributions 			if (num) {
1280e6231be0SApple OSS Distributions 				IOLockLock(gIODTResolvers->lock);
128176e12aa3SApple OSS Distributions 				index = num->unsigned32BitValue();
1282e6231be0SApple OSS Distributions 				if (index < gIODTResolvers->count) {
1283e6231be0SApple OSS Distributions 					compare = gIODTResolvers->resolvers[index].compareFunc;
128476e12aa3SApple OSS Distributions 				}
1285e6231be0SApple OSS Distributions 				IOLockUnlock(gIODTResolvers->lock);
128676e12aa3SApple OSS Distributions 			}
128776e12aa3SApple OSS Distributions 
128876e12aa3SApple OSS Distributions 			if (!compare && (addressCells == childAddressCells)) {
1289c1dac77fSApple OSS Distributions 				compare = DefaultCompare;
129076e12aa3SApple OSS Distributions 			}
129176e12aa3SApple OSS Distributions 			if (!compare) {
1292d0c1fef6SApple OSS Distributions 				panic("There is no mixed comparison function yet...");
1293d0c1fef6SApple OSS Distributions 			}
1294c1dac77fSApple OSS Distributions 
1295c1dac77fSApple OSS Distributions 			for (ok = false;
1296c1dac77fSApple OSS Distributions 			    range < endRanges;
1297c1dac77fSApple OSS Distributions 			    range += (childCells + addressCells)) {
1298e13b1fa5SApple OSS Distributions 				// is cell start within range?
1299c1dac77fSApple OSS Distributions 				diff = (*compare)( childAddressCells, cell, range );
1300e13b1fa5SApple OSS Distributions 
1301a5e72196SApple OSS Distributions 				if (childAddressCells > sizeof(endCell) / sizeof(endCell[0])) {
1302d0c1fef6SApple OSS Distributions 					panic("IODTResolveAddressCell: Invalid device tree (%u)", (uint32_t)childAddressCells);
1303a5e72196SApple OSS Distributions 				}
1304d0c1fef6SApple OSS Distributions 
1305e13b1fa5SApple OSS Distributions 				bcopy(range, endCell, childAddressCells * sizeof(UInt32));
1306d0c1fef6SApple OSS Distributions 
1307186b8fceSApple OSS Distributions 				rangeLen = CellsValue(childSizeCells, range + childAddressCells + addressCells);
1308186b8fceSApple OSS Distributions 				AddLengthToCells(childAddressCells, endCell, rangeLen);
1309d0c1fef6SApple OSS Distributions 
1310e13b1fa5SApple OSS Distributions 				diff2 = (*compare)( childAddressCells, cell, endCell );
1311e13b1fa5SApple OSS Distributions 
1312186b8fceSApple OSS Distributions 				// if start of cell < start of range, or end of range >= start of cell, skip
1313a5e72196SApple OSS Distributions 				if ((diff < 0) || (diff2 >= 0)) {
1314c1dac77fSApple OSS Distributions 					continue;
1315a5e72196SApple OSS Distributions 				}
13168149afccSApple OSS Distributions 
1317186b8fceSApple OSS Distributions 				len = CellsValue(childSizeCells, cell + childAddressCells);
1318186b8fceSApple OSS Distributions 				ok = (0 == len);
1319186b8fceSApple OSS Distributions 
1320a5e72196SApple OSS Distributions 				if (!ok) {
1321e13b1fa5SApple OSS Distributions 					// search for cell end
1322e13b1fa5SApple OSS Distributions 					bcopy(cell, endCell, childAddressCells * sizeof(UInt32));
1323d0c1fef6SApple OSS Distributions 
1324186b8fceSApple OSS Distributions 					AddLengthToCells(childAddressCells, endCell, len - 1);
1325186b8fceSApple OSS Distributions 
1326186b8fceSApple OSS Distributions 					for (lookRange = startRange;
1327e13b1fa5SApple OSS Distributions 					    lookRange < endRanges;
1328a5e72196SApple OSS Distributions 					    lookRange += (childCells + addressCells)) {
1329186b8fceSApple OSS Distributions 						// make sure end of cell >= range start
1330e13b1fa5SApple OSS Distributions 						endDiff = (*compare)( childAddressCells, endCell, lookRange );
1331a5e72196SApple OSS Distributions 						if (endDiff < 0) {
1332c1dac77fSApple OSS Distributions 							continue;
1333a5e72196SApple OSS Distributions 						}
1334186b8fceSApple OSS Distributions 
1335186b8fceSApple OSS Distributions 						UInt64 rangeStart = CellsValue(addressCells, range + childAddressCells);
1336186b8fceSApple OSS Distributions 						UInt64 lookRangeStart = CellsValue(addressCells, lookRange + childAddressCells);
1337a5e72196SApple OSS Distributions 						if ((endDiff - len + 1 + lookRangeStart) == (diff + rangeStart)) {
1338c1dac77fSApple OSS Distributions 							ok = true;
1339c1dac77fSApple OSS Distributions 							break;
1340c1dac77fSApple OSS Distributions 						}
1341e13b1fa5SApple OSS Distributions 					}
1342a5e72196SApple OSS Distributions 					if (!ok) {
1343e13b1fa5SApple OSS Distributions 						continue;
1344e13b1fa5SApple OSS Distributions 					}
1345a5e72196SApple OSS Distributions 				}
1346e13b1fa5SApple OSS Distributions 				offset += diff;
1347e13b1fa5SApple OSS Distributions 				break;
1348e13b1fa5SApple OSS Distributions 			}
1349c1dac77fSApple OSS Distributions 
1350a5e72196SApple OSS Distributions 			if (addressCells + sizeCells > sizeof(cell) / sizeof(cell[0])) {
1351d0c1fef6SApple OSS Distributions 				panic("IODTResolveAddressCell: Invalid device tree (%u, %u)", (uint32_t)addressCells, (uint32_t)sizeCells);
1352a5e72196SApple OSS Distributions 			}
1353d0c1fef6SApple OSS Distributions 
1354c1dac77fSApple OSS Distributions 			// Get the physical start of the range from our parent
1355e13b1fa5SApple OSS Distributions 			bcopy( range + childAddressCells, cell, sizeof(UInt32) * addressCells );
1356e13b1fa5SApple OSS Distributions 			bzero( cell + addressCells, sizeof(UInt32) * sizeCells );
1357c1dac77fSApple OSS Distributions 		} /* else zero length range => pass thru to parent */
1358c1dac77fSApple OSS Distributions 
1359e6231be0SApple OSS Distributions 		OSSafeReleaseNULL(regEntry);
1360c1dac77fSApple OSS Distributions 		regEntry                = parent;
1361e6231be0SApple OSS Distributions 		parent = NULL;
1362c1dac77fSApple OSS Distributions 		childSizeCells          = sizeCells;
1363c1dac77fSApple OSS Distributions 		childAddressCells       = addressCells;
1364c1dac77fSApple OSS Distributions 		childCells              = childAddressCells + childSizeCells;
1365a5e72196SApple OSS Distributions 	}while (ok && regEntry);
1366c1dac77fSApple OSS Distributions 
1367e6231be0SApple OSS Distributions 	OSSafeReleaseNULL(regEntry);
1368e6231be0SApple OSS Distributions 
1369a5e72196SApple OSS Distributions 	return ok;
1370c1dac77fSApple OSS Distributions }
1371c1dac77fSApple OSS Distributions 
1372c1dac77fSApple OSS Distributions 
1373a5e72196SApple OSS Distributions OSArray *
IODTResolveAddressing(IORegistryEntry * regEntry,const char * addressPropertyName,IODeviceMemory * parent)1374a5e72196SApple OSS Distributions IODTResolveAddressing( IORegistryEntry * regEntry,
1375c1dac77fSApple OSS Distributions     const char * addressPropertyName,
1376c1dac77fSApple OSS Distributions     IODeviceMemory * parent )
1377c1dac77fSApple OSS Distributions {
1378c1dac77fSApple OSS Distributions 	IORegistryEntry             *parentEntry;
1379c1dac77fSApple OSS Distributions 	OSData                              *addressProperty;
1380c1dac77fSApple OSS Distributions 	UInt32                              sizeCells, addressCells, cells;
1381c1dac77fSApple OSS Distributions 	int                                 i, num;
1382c1dac77fSApple OSS Distributions 	UInt32                              *reg;
1383c1dac77fSApple OSS Distributions 	IOPhysicalAddress   phys;
1384c1dac77fSApple OSS Distributions 	IOPhysicalLength    len;
1385c1dac77fSApple OSS Distributions 	OSArray                             *array;
1386c1dac77fSApple OSS Distributions 
1387a5e72196SApple OSS Distributions 	array = NULL;
1388a5e72196SApple OSS Distributions 	do{
138976e12aa3SApple OSS Distributions 		parentEntry = regEntry->copyParentEntry( gIODTPlane );
1390c1dac77fSApple OSS Distributions 		addressProperty = (OSData *) regEntry->getProperty( addressPropertyName );
1391a5e72196SApple OSS Distributions 		if ((NULL == addressProperty) || (NULL == parentEntry)) {
1392a5e72196SApple OSS Distributions 			break;
1393a5e72196SApple OSS Distributions 		}
1394c1dac77fSApple OSS Distributions 
1395c1dac77fSApple OSS Distributions 		IODTGetCellCounts( parentEntry, &sizeCells, &addressCells );
1396a5e72196SApple OSS Distributions 		if (0 == sizeCells) {
1397a5e72196SApple OSS Distributions 			break;
1398a5e72196SApple OSS Distributions 		}
1399c1dac77fSApple OSS Distributions 
1400c1dac77fSApple OSS Distributions 		cells = sizeCells + addressCells;
1401c1dac77fSApple OSS Distributions 		reg = (UInt32 *) addressProperty->getBytesNoCopy();
1402c1dac77fSApple OSS Distributions 		num = addressProperty->getLength() / (4 * cells);
1403c1dac77fSApple OSS Distributions 
1404c1dac77fSApple OSS Distributions 		array = OSArray::withCapacity( 1 );
1405a5e72196SApple OSS Distributions 		if (NULL == array) {
1406a5e72196SApple OSS Distributions 			break;
1407a5e72196SApple OSS Distributions 		}
1408c1dac77fSApple OSS Distributions 
1409c1dac77fSApple OSS Distributions 		for (i = 0; i < num; i++) {
1410c1dac77fSApple OSS Distributions 			if (IODTResolveAddressCell( parentEntry, reg, &phys, &len )) {
1411e6231be0SApple OSS Distributions 				IODeviceMemory *range;
1412a5e72196SApple OSS Distributions 				range = NULL;
1413a5e72196SApple OSS Distributions 				if (parent) {
1414c1dac77fSApple OSS Distributions 					range = IODeviceMemory::withSubRange( parent,
1415a5e72196SApple OSS Distributions 					    phys - parent->getPhysicalSegment(0, NULL, kIOMemoryMapperNone), len );
1416a5e72196SApple OSS Distributions 				}
1417a5e72196SApple OSS Distributions 				if (NULL == range) {
1418c1dac77fSApple OSS Distributions 					range = IODeviceMemory::withRange( phys, len );
1419a5e72196SApple OSS Distributions 				}
1420a5e72196SApple OSS Distributions 				if (range) {
1421c1dac77fSApple OSS Distributions 					array->setObject( range );
1422e6231be0SApple OSS Distributions 					OSSafeReleaseNULL(range);
1423c1dac77fSApple OSS Distributions 				}
1424a5e72196SApple OSS Distributions 			}
1425c1dac77fSApple OSS Distributions 			reg += cells;
1426c1dac77fSApple OSS Distributions 		}
1427c1dac77fSApple OSS Distributions 
1428c1dac77fSApple OSS Distributions 		regEntry->setProperty( gIODeviceMemoryKey, array);
1429c1dac77fSApple OSS Distributions 		array->release(); /* ??? */
1430a5e72196SApple OSS Distributions 	}while (false);
143176e12aa3SApple OSS Distributions 
143276e12aa3SApple OSS Distributions 	OSSafeReleaseNULL(parentEntry);
1433c1dac77fSApple OSS Distributions 
1434a5e72196SApple OSS Distributions 	return array;
1435c1dac77fSApple OSS Distributions }
1436c1dac77fSApple OSS Distributions 
1437a5e72196SApple OSS Distributions OSData *
IODTFindSlotName(IORegistryEntry * regEntry,UInt32 deviceNumber)1438a5e72196SApple OSS Distributions IODTFindSlotName( IORegistryEntry * regEntry, UInt32 deviceNumber )
1439c1dac77fSApple OSS Distributions {
1440c1dac77fSApple OSS Distributions 	IORegistryEntry             *parent;
1441c1dac77fSApple OSS Distributions 	OSData                              *data;
1442a5e72196SApple OSS Distributions 	OSData                              *ret = NULL;
1443c1dac77fSApple OSS Distributions 	UInt32                              *bits;
1444c1dac77fSApple OSS Distributions 	UInt32                              i;
1445bb611c8fSApple OSS Distributions 	UInt32              nlen;
1446c1dac77fSApple OSS Distributions 	char                                *names;
1447c1dac77fSApple OSS Distributions 	char                                *lastName;
1448c1dac77fSApple OSS Distributions 	UInt32                              mask;
1449c1dac77fSApple OSS Distributions 
1450c1dac77fSApple OSS Distributions 	data = (OSData *) regEntry->getProperty("AAPL,slot-name");
1451a5e72196SApple OSS Distributions 	if (data) {
1452a5e72196SApple OSS Distributions 		return data;
1453a5e72196SApple OSS Distributions 	}
145476e12aa3SApple OSS Distributions 
1455a5e72196SApple OSS Distributions 	do{
145676e12aa3SApple OSS Distributions 		parent = regEntry->copyParentEntry( gIODTPlane );
1457a5e72196SApple OSS Distributions 		if (!parent) {
1458a5e72196SApple OSS Distributions 			break;
1459a5e72196SApple OSS Distributions 		}
146076e12aa3SApple OSS Distributions 
1461c1dac77fSApple OSS Distributions 		data = OSDynamicCast( OSData, parent->getProperty("slot-names"));
1462a5e72196SApple OSS Distributions 		if (!data) {
1463a5e72196SApple OSS Distributions 			break;
1464a5e72196SApple OSS Distributions 		}
1465a5e72196SApple OSS Distributions 		if (data->getLength() <= 4) {
1466a5e72196SApple OSS Distributions 			break;
1467a5e72196SApple OSS Distributions 		}
1468c1dac77fSApple OSS Distributions 
1469c1dac77fSApple OSS Distributions 		bits = (UInt32 *) data->getBytesNoCopy();
1470c1dac77fSApple OSS Distributions 		mask = *bits;
1471a5e72196SApple OSS Distributions 		if ((0 == (mask & (1 << deviceNumber)))) {
1472a5e72196SApple OSS Distributions 			break;
1473a5e72196SApple OSS Distributions 		}
1474c1dac77fSApple OSS Distributions 
1475c1dac77fSApple OSS Distributions 		names = (char *)(bits + 1);
1476c1dac77fSApple OSS Distributions 		lastName = names + (data->getLength() - 4);
1477c1dac77fSApple OSS Distributions 
14788149afccSApple OSS Distributions 		for (i = 0; (i <= deviceNumber) && (names < lastName); i++) {
1479c1dac77fSApple OSS Distributions 			if (mask & (1 << i)) {
1480bb611c8fSApple OSS Distributions 				nlen = 1 + ((unsigned int) strnlen(names, lastName - names));
1481c1dac77fSApple OSS Distributions 				if (i == deviceNumber) {
1482a3bb9fccSApple OSS Distributions 					data = OSData::withBytesNoCopy(names, nlen);
1483c1dac77fSApple OSS Distributions 					if (data) {
1484c1dac77fSApple OSS Distributions 						regEntry->setProperty("AAPL,slot-name", data);
1485c1dac77fSApple OSS Distributions 						ret = data;
1486c1dac77fSApple OSS Distributions 						data->release();
1487c1dac77fSApple OSS Distributions 					}
1488a5e72196SApple OSS Distributions 				} else {
1489a3bb9fccSApple OSS Distributions 					names += nlen;
1490c1dac77fSApple OSS Distributions 				}
1491c1dac77fSApple OSS Distributions 			}
149276e12aa3SApple OSS Distributions 		}
1493a5e72196SApple OSS Distributions 	}while (false);
149476e12aa3SApple OSS Distributions 
149576e12aa3SApple OSS Distributions 	OSSafeReleaseNULL(parent);
1496c1dac77fSApple OSS Distributions 
1497a5e72196SApple OSS Distributions 	return ret;
1498c1dac77fSApple OSS Distributions }
149914e3d835SApple OSS Distributions 
1500a5e72196SApple OSS Distributions extern "C" IOReturn
IONDRVLibrariesInitialize(IOService * provider)1501a5e72196SApple OSS Distributions IONDRVLibrariesInitialize( IOService * provider )
150214e3d835SApple OSS Distributions {
1503a5e72196SApple OSS Distributions 	return kIOReturnUnsupported;
150414e3d835SApple OSS Distributions }
1505