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