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