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