]> git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IODeviceTreeSupport.cpp
xnu-517.12.7.tar.gz
[apple/xnu.git] / iokit / Kernel / IODeviceTreeSupport.cpp
1 /*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
24 *
25 * HISTORY
26 * 23 Nov 98 sdouglas, created from IODeviceTreeBus.m, & MacOS exp mgr.
27 * 05 Apr 99 sdouglas, add interrupt mapping.
28 *
29 */
30
31 #include <IOKit/IODeviceTreeSupport.h>
32 #include <libkern/c++/OSContainers.h>
33 #include <IOKit/IODeviceMemory.h>
34 #include <IOKit/IOService.h>
35 #include <IOKit/IOCatalogue.h>
36
37 #include <IOKit/IOLib.h>
38 #include <IOKit/IOKitKeys.h>
39
40 #include <pexpert/device_tree.h>
41
42 extern "C" {
43 #include <machine/machine_routines.h>
44 void DTInit( void * data );
45
46 int IODTGetLoaderInfo( char *key, void **infoAddr, int *infosize );
47 void IODTFreeLoaderInfo( char *key, void *infoAddr, int infoSize );
48 }
49
50 #include <IOKit/assert.h>
51
52 #define IODTSUPPORTDEBUG 0
53
54 const IORegistryPlane * gIODTPlane;
55
56 static OSArray * gIODTPHandles;
57 static OSArray * gIODTPHandleMap;
58
59 const OSSymbol * gIODTNameKey;
60 const OSSymbol * gIODTUnitKey;
61 const OSSymbol * gIODTCompatibleKey;
62 const OSSymbol * gIODTTypeKey;
63 const OSSymbol * gIODTModelKey;
64
65 const OSSymbol * gIODTSizeCellKey;
66 const OSSymbol * gIODTAddressCellKey;
67 const OSSymbol * gIODTRangeKey;
68
69 const OSSymbol * gIODTPersistKey;
70
71 const OSSymbol * gIODTDefaultInterruptController;
72 const OSSymbol * gIODTAAPLInterruptsKey;
73 const OSSymbol * gIODTPHandleKey;
74 const OSSymbol * gIODTInterruptCellKey;
75 const OSSymbol * gIODTInterruptParentKey;
76 const OSSymbol * gIODTNWInterruptMappingKey;
77
78
79 static IORegistryEntry * MakeReferenceTable( DTEntry dtEntry, bool copy );
80 static void AddPHandle( IORegistryEntry * regEntry );
81 static void FreePhysicalMemory( vm_offset_t * range );
82
83 IORegistryEntry *
84 IODeviceTreeAlloc( void * dtTop )
85 {
86 IORegistryEntry *parent;
87 IORegistryEntry *child;
88 IORegistryIterator *regIter;
89 DTEntryIterator iter;
90 DTEntry dtChild;
91 DTEntry mapEntry;
92 OSArray *stack;
93 OSData *prop;
94 OSObject *obj;
95 vm_offset_t *dtMap;
96 int propSize;
97 bool intMap;
98 bool freeDT;
99
100 gIODTPlane = IORegistryEntry::makePlane( kIODeviceTreePlane );
101
102 gIODTNameKey = OSSymbol::withCStringNoCopy( "name" );
103 gIODTUnitKey = OSSymbol::withCStringNoCopy( "AAPL,unit-string" );
104 gIODTCompatibleKey = OSSymbol::withCStringNoCopy( "compatible" );
105 gIODTTypeKey = OSSymbol::withCStringNoCopy( "device_type" );
106 gIODTModelKey = OSSymbol::withCStringNoCopy( "model" );
107 gIODTSizeCellKey = OSSymbol::withCStringNoCopy( "#size-cells" );
108 gIODTAddressCellKey = OSSymbol::withCStringNoCopy( "#address-cells" );
109 gIODTRangeKey = OSSymbol::withCStringNoCopy( "ranges" );
110 gIODTPersistKey = OSSymbol::withCStringNoCopy( "IODTPersist" );
111
112 assert( gIODTPlane && gIODTCompatibleKey
113 && gIODTTypeKey && gIODTModelKey
114 && gIODTSizeCellKey && gIODTAddressCellKey && gIODTRangeKey
115 && gIODTPersistKey );
116
117 gIODTDefaultInterruptController
118 = OSSymbol::withCStringNoCopy("IOPrimaryInterruptController");
119 gIODTNWInterruptMappingKey
120 = OSSymbol::withCStringNoCopy("IONWInterrupts");
121
122 gIODTAAPLInterruptsKey
123 = OSSymbol::withCStringNoCopy("AAPL,interrupts");
124 gIODTPHandleKey
125 = OSSymbol::withCStringNoCopy("AAPL,phandle");
126
127 gIODTInterruptParentKey
128 = OSSymbol::withCStringNoCopy("interrupt-parent");
129
130 gIODTPHandles = OSArray::withCapacity( 1 );
131 gIODTPHandleMap = OSArray::withCapacity( 1 );
132
133 gIODTInterruptCellKey
134 = OSSymbol::withCStringNoCopy("#interrupt-cells");
135
136 assert( gIODTDefaultInterruptController && gIODTNWInterruptMappingKey
137 && gIODTAAPLInterruptsKey
138 && gIODTPHandleKey && gIODTInterruptParentKey
139 && gIODTPHandles && gIODTPHandleMap
140 && gIODTInterruptCellKey
141 );
142
143 freeDT = (kSuccess == DTLookupEntry( 0, "/chosen/memory-map", &mapEntry ))
144 && (kSuccess == DTGetProperty( mapEntry,
145 "DeviceTree", (void **) &dtMap, &propSize ))
146 && ((2 * sizeof( vm_offset_t)) == propSize);
147
148 parent = MakeReferenceTable( (DTEntry)dtTop, freeDT );
149
150 stack = OSArray::withObjects( (const OSObject **) &parent, 1, 10 );
151 DTCreateEntryIterator( (DTEntry)dtTop, &iter );
152
153 do {
154 parent = (IORegistryEntry *)stack->getObject( stack->getCount() - 1);
155 //parent->release();
156 stack->removeObject( stack->getCount() - 1);
157
158 while( kSuccess == DTIterateEntries( iter, &dtChild) ) {
159
160 child = MakeReferenceTable( dtChild, freeDT );
161 child->attachToParent( parent, gIODTPlane);
162
163 AddPHandle( child );
164
165 if( kSuccess == DTEnterEntry( iter, dtChild)) {
166 stack->setObject( parent);
167 parent = child;
168 }
169 // only registry holds retain
170 child->release();
171 }
172
173 } while( stack->getCount()
174 && (kSuccess == DTExitEntry( iter, &dtChild)));
175
176 stack->release();
177 DTDisposeEntryIterator( iter);
178
179 // parent is now root of the created tree
180
181 // make root name first compatible entry (purely cosmetic)
182 if( (prop = (OSData *) parent->getProperty( gIODTCompatibleKey))) {
183 parent->setName( parent->getName(), gIODTPlane );
184 parent->setName( (const char *) prop->getBytesNoCopy() );
185 }
186
187 // attach tree to meta root
188 parent->attachToParent( IORegistryEntry::getRegistryRoot(), gIODTPlane);
189 parent->release();
190
191 if( freeDT ) {
192 // free original device tree
193 DTInit(0);
194 IODTFreeLoaderInfo( "DeviceTree",
195 (void *)dtMap[0], round_page_32(dtMap[1]) );
196 }
197
198 // adjust tree
199 intMap = false;
200 regIter = IORegistryIterator::iterateOver( gIODTPlane,
201 kIORegistryIterateRecursively );
202 assert( regIter );
203 if( regIter) {
204 while( (child = regIter->getNextObject())) {
205 IODTMapInterrupts( child );
206 if( !intMap && child->getProperty( gIODTInterruptParentKey))
207 intMap = true;
208
209 // Look for a "driver,AAPL,MacOSX,PowerPC" property.
210 if( (obj = child->getProperty( "driver,AAPL,MacOSX,PowerPC"))) {
211 gIOCatalogue->addExtensionsFromArchive((OSData *)obj);
212 child->removeProperty( "driver,AAPL,MacOSX,PowerPC");
213 }
214
215 // some gross pruning
216 child->removeProperty( "lanLib,AAPL,MacOS,PowerPC");
217
218 if( (obj = child->getProperty( "driver,AAPL,MacOS,PowerPC"))) {
219
220 if( (0 == (prop = (OSData *)child->getProperty( gIODTTypeKey )))
221 || (strcmp( "display", (char *) prop->getBytesNoCopy())) ) {
222 child->removeProperty( "driver,AAPL,MacOS,PowerPC");
223 }
224 }
225 }
226 regIter->release();
227 }
228
229 if( intMap)
230 // set a key in the root to indicate we found NW interrupt mapping
231 parent->setProperty( gIODTNWInterruptMappingKey,
232 (OSObject *) gIODTNWInterruptMappingKey );
233
234 return( parent);
235 }
236
237 int IODTGetLoaderInfo( char *key, void **infoAddr, int *infoSize )
238 {
239 IORegistryEntry *chosen;
240 OSData *propObj;
241 unsigned int *propPtr;
242 unsigned int propSize;
243
244 chosen = IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane );
245 if ( chosen == 0 ) return -1;
246
247 propObj = OSDynamicCast( OSData, chosen->getProperty(key) );
248 if ( propObj == 0 ) return -1;
249
250 propSize = propObj->getLength();
251 if ( propSize != (2 * sizeof(UInt32)) ) return -1;
252
253 propPtr = (unsigned int *)propObj->getBytesNoCopy();
254 if ( propPtr == 0 ) return -1;
255
256 *infoAddr = (void *)propPtr[0] ;
257 *infoSize = (int) propPtr[1];
258
259 return 0;
260 }
261
262 void IODTFreeLoaderInfo( char *key, void *infoAddr, int infoSize )
263 {
264 vm_offset_t range[2];
265 IORegistryEntry *chosen;
266
267 range[0] = (vm_offset_t)infoAddr;
268 range[1] = (vm_offset_t)infoSize;
269 FreePhysicalMemory( range );
270
271 if ( key != 0 ) {
272 chosen = IORegistryEntry::fromPath( "/chosen/memory-map", gIODTPlane );
273 if ( chosen != 0 ) {
274 chosen->removeProperty(key);
275 }
276 }
277 }
278
279 static void FreePhysicalMemory( vm_offset_t * range )
280 {
281 vm_offset_t virt;
282
283 virt = ml_static_ptovirt( range[0] );
284 if( virt) {
285 ml_static_mfree( virt, range[1] );
286 }
287 }
288
289 static IORegistryEntry *
290 MakeReferenceTable( DTEntry dtEntry, bool copy )
291 {
292 IORegistryEntry *regEntry;
293 OSDictionary *propTable;
294 const OSSymbol *nameKey;
295 OSData *data;
296 const OSSymbol *sym;
297 DTPropertyIterator dtIter;
298 void *prop;
299 int propSize;
300 char *name;
301 char location[ 32 ];
302 bool noLocation = true;
303
304 regEntry = new IOService;
305
306 if( regEntry && (false == regEntry->init())) {
307 regEntry->release();
308 regEntry = 0;
309 }
310
311 if( regEntry &&
312 (kSuccess == DTCreatePropertyIterator( dtEntry, &dtIter))) {
313
314 propTable = regEntry->getPropertyTable();
315
316 while( kSuccess == DTIterateProperties( dtIter, &name)) {
317
318 if( kSuccess != DTGetProperty( dtEntry, name, &prop, &propSize ))
319 continue;
320
321 if( copy) {
322 nameKey = OSSymbol::withCString(name);
323 data = OSData::withBytes(prop, propSize);
324 } else {
325 nameKey = OSSymbol::withCStringNoCopy(name);
326 data = OSData::withBytesNoCopy(prop, propSize);
327 }
328 assert( nameKey && data );
329
330 propTable->setObject( nameKey, data);
331 data->release();
332 nameKey->release();
333
334 if( nameKey == gIODTNameKey ) {
335 if( copy)
336 sym = OSSymbol::withCString( (const char *) prop);
337 else
338 sym = OSSymbol::withCStringNoCopy( (const char *) prop);
339 regEntry->setName( sym );
340 sym->release();
341
342 } else if( nameKey == gIODTUnitKey ) {
343 // all OF strings are null terminated... except this one
344 if( propSize >= (int) sizeof( location))
345 propSize = sizeof( location) - 1;
346 strncpy( location, (const char *) prop, propSize );
347 location[ propSize ] = 0;
348 regEntry->setLocation( location );
349 propTable->removeObject( gIODTUnitKey );
350 noLocation = false;
351
352 } else if( noLocation && (0 == strcmp( name, "reg"))) {
353 // default location - override later
354 sprintf( location, "%lX", *((UInt32 *) prop) );
355 regEntry->setLocation( location );
356 }
357 }
358 DTDisposePropertyIterator( dtIter);
359 }
360
361 return( regEntry);
362 }
363
364 static void AddPHandle( IORegistryEntry * regEntry )
365 {
366 OSData * data;
367
368 if( regEntry->getProperty( gIODTInterruptCellKey)
369 && (data = OSDynamicCast( OSData, regEntry->getProperty( gIODTPHandleKey )))) {
370 // a possible interrupt-parent
371 gIODTPHandles->setObject( data );
372 gIODTPHandleMap->setObject( regEntry );
373 }
374 }
375
376 static IORegistryEntry * FindPHandle( UInt32 phandle )
377 {
378 OSData *data;
379 IORegistryEntry *regEntry = 0;
380 int i;
381
382 for( i = 0; (data = (OSData *)gIODTPHandles->getObject( i )); i++ ) {
383 if( phandle == *((UInt32 *)data->getBytesNoCopy())) {
384 regEntry = (IORegistryEntry *)
385 gIODTPHandleMap->getObject( i );
386 break;
387 }
388 }
389
390 return( regEntry );
391 }
392
393 static bool GetUInt32( IORegistryEntry * regEntry, const OSSymbol * name,
394 UInt32 * value )
395 {
396 OSData *data;
397
398 if( (data = OSDynamicCast( OSData, regEntry->getProperty( name )))
399 && (4 == data->getLength())) {
400 *value = *((UInt32 *) data->getBytesNoCopy());
401 return( true );
402 } else
403 return( false );
404 }
405
406 IORegistryEntry * IODTFindInterruptParent( IORegistryEntry * regEntry )
407 {
408 IORegistryEntry * parent;
409 UInt32 phandle;
410
411 if( GetUInt32( regEntry, gIODTInterruptParentKey, &phandle))
412 parent = FindPHandle( phandle );
413
414 else if( 0 == regEntry->getProperty( "interrupt-controller"))
415 parent = regEntry->getParentEntry( gIODTPlane);
416 else
417 parent = 0;
418
419 return( parent );
420 }
421
422 const OSSymbol * IODTInterruptControllerName( IORegistryEntry * regEntry )
423 {
424 const OSSymbol *sym;
425 UInt32 phandle;
426 bool ok;
427 char buf[48];
428
429 ok = GetUInt32( regEntry, gIODTPHandleKey, &phandle);
430 assert( ok );
431
432 if( ok) {
433 sprintf( buf, "IOInterruptController%08lX", phandle);
434 sym = OSSymbol::withCString( buf );
435 } else
436 sym = 0;
437
438 return( sym );
439 }
440
441 #define unexpected(a) { kprintf("unexpected %s:%d\n", __FILE__, __LINE__); a; }
442
443 static void IODTGetICellCounts( IORegistryEntry * regEntry,
444 UInt32 * iCellCount, UInt32 * aCellCount)
445 {
446 if( !GetUInt32( regEntry, gIODTInterruptCellKey, iCellCount))
447 unexpected( *iCellCount = 1 );
448 if( !GetUInt32( regEntry, gIODTAddressCellKey, aCellCount))
449 *aCellCount = 0;
450 }
451
452 UInt32 IODTMapOneInterrupt( IORegistryEntry * regEntry, UInt32 * intSpec,
453 OSData ** spec, const OSSymbol ** controller )
454 {
455 IORegistryEntry *parent = 0;
456 OSData *data;
457 UInt32 *addrCmp;
458 UInt32 *maskCmp;
459 UInt32 *map;
460 UInt32 *endMap;
461 UInt32 acells, icells, pacells, picells, cell;
462 UInt32 i, original_icells;
463 bool cmp, ok = false;
464
465 parent = IODTFindInterruptParent( regEntry );
466 IODTGetICellCounts( parent, &icells, &acells );
467 addrCmp = 0;
468 if( acells) {
469 data = OSDynamicCast( OSData, regEntry->getProperty( "reg" ));
470 if( data && (data->getLength() >= (acells * sizeof( UInt32))))
471 addrCmp = (UInt32 *) data->getBytesNoCopy();
472 }
473 original_icells = icells;
474 regEntry = parent;
475
476 do {
477 #if IODTSUPPORTDEBUG
478 kprintf ("IODTMapOneInterrupt: current regEntry name %s\n", regEntry->getName());
479 kprintf ("acells - icells: ");
480 for (i = 0; i < acells; i++) kprintf ("0x%08X ", addrCmp[i]);
481 kprintf ("- ");
482 for (i = 0; i < icells; i++) kprintf ("0x%08X ", intSpec[i]);
483 kprintf ("\n");
484 #endif
485
486 if( parent && (data = OSDynamicCast( OSData,
487 regEntry->getProperty( "interrupt-controller")))) {
488 // found a controller - don't want to follow cascaded controllers
489 parent = 0;
490 *spec = OSData::withBytesNoCopy( (void *) intSpec,
491 icells * sizeof( UInt32));
492 *controller = IODTInterruptControllerName( regEntry );
493 ok = (*spec && *controller);
494 } else if( parent && (data = OSDynamicCast( OSData,
495 regEntry->getProperty( "interrupt-map")))) {
496 // interrupt-map
497 map = (UInt32 *) data->getBytesNoCopy();
498 endMap = map + (data->getLength() / sizeof(UInt32));
499 data = OSDynamicCast( OSData, regEntry->getProperty( "interrupt-map-mask" ));
500 if( data && (data->getLength() >= ((acells + icells) * sizeof( UInt32))))
501 maskCmp = (UInt32 *) data->getBytesNoCopy();
502 else
503 maskCmp = 0;
504
505 #if IODTSUPPORTDEBUG
506 if (maskCmp) {
507 kprintf (" maskCmp: ");
508 for (i = 0; i < acells + icells; i++) {
509 if (i == acells)
510 kprintf ("- ");
511 kprintf ("0x%08X ", maskCmp[i]);
512 }
513 kprintf ("\n");
514 kprintf (" masked: ");
515 for (i = 0; i < acells + icells; i++) {
516 if (i == acells)
517 kprintf ("- ");
518 kprintf ("0x%08X ", ((i < acells) ? addrCmp[i] : intSpec[i-acells]) & maskCmp[i]);
519 }
520 kprintf ("\n");
521 } else
522 kprintf ("no maskCmp\n");
523 #endif
524 do {
525 #if IODTSUPPORTDEBUG
526 kprintf (" map: ");
527 for (i = 0; i < acells + icells; i++) {
528 if (i == acells)
529 kprintf ("- ");
530 kprintf ("0x%08X ", map[i]);
531 }
532 kprintf ("\n");
533 #endif
534 for( i = 0, cmp = true; cmp && (i < (acells + icells)); i++) {
535 cell = (i < acells) ? addrCmp[i] : intSpec[ i - acells ];
536 if( maskCmp)
537 cell &= maskCmp[i];
538 cmp = (cell == map[i]);
539 }
540
541 map += acells + icells;
542 if( 0 == (parent = FindPHandle( *(map++) )))
543 unexpected(break);
544
545 IODTGetICellCounts( parent, &picells, &pacells );
546 if( cmp) {
547 addrCmp = map;
548 intSpec = map + pacells;
549 regEntry = parent;
550 } else {
551 map += pacells + picells;
552 }
553 } while( !cmp && (map < endMap) );
554 if (!cmp)
555 parent = 0;
556 }
557
558 if( parent) {
559 IODTGetICellCounts( parent, &icells, &acells );
560 regEntry = parent;
561 }
562
563 } while( parent);
564
565 return( ok ? original_icells : 0 );
566 }
567
568 bool IODTMapInterrupts( IORegistryEntry * regEntry )
569 {
570 IORegistryEntry *parent;
571 OSData *local;
572 OSData *local2;
573 UInt32 *localBits;
574 UInt32 *localEnd;
575 OSData *map;
576 OSArray *mapped;
577 const OSSymbol *controller;
578 OSArray *controllers;
579 UInt32 skip = 1;
580 bool ok, nw;
581
582 nw = (0 == (local = OSDynamicCast( OSData,
583 regEntry->getProperty( gIODTAAPLInterruptsKey))));
584 if( nw && (0 == (local = OSDynamicCast( OSData,
585 regEntry->getProperty( "interrupts")))))
586 return( true ); // nothing to see here
587
588 if( nw && (parent = regEntry->getParentEntry( gIODTPlane))) {
589 // check for bridges on old world
590 if( (local2 = OSDynamicCast( OSData,
591 parent->getProperty( gIODTAAPLInterruptsKey)))) {
592 local = local2;
593 nw = false;
594 }
595 }
596
597 localBits = (UInt32 *) local->getBytesNoCopy();
598 localEnd = localBits + (local->getLength() / sizeof( UInt32));
599 mapped = OSArray::withCapacity( 1 );
600 controllers = OSArray::withCapacity( 1 );
601
602 ok = (mapped && controllers);
603
604 if( ok) do {
605 if( nw) {
606 skip = IODTMapOneInterrupt( regEntry, localBits, &map, &controller );
607 if( 0 == skip) {
608 IOLog("%s: error mapping interrupt[%d]\n",
609 regEntry->getName(), mapped->getCount());
610 break;
611 }
612 } else {
613 map = OSData::withData( local, mapped->getCount() * sizeof( UInt32),
614 sizeof( UInt32));
615 controller = gIODTDefaultInterruptController;
616 controller->retain();
617 }
618
619 localBits += skip;
620 mapped->setObject( map );
621 map->release();
622 controllers->setObject( (OSObject *) controller );
623 controller->release();
624
625 } while( localBits < localEnd);
626
627 ok &= (localBits == localEnd);
628
629 if( ok ) {
630 // store results
631 ok = regEntry->setProperty( gIOInterruptControllersKey, controllers);
632 ok &= regEntry->setProperty( gIOInterruptSpecifiersKey, mapped);
633 }
634
635 if( controllers)
636 controllers->release();
637 if( mapped)
638 mapped->release();
639
640 return( ok );
641 }
642
643 /*
644 */
645
646 static const char *
647 CompareKey( OSString * key,
648 const IORegistryEntry * table, const OSSymbol * propName )
649 {
650 OSObject *prop;
651 OSData *data;
652 OSString *string;
653 const char *ckey;
654 UInt32 keyLen;
655 const char *names;
656 const char *lastName;
657 bool wild;
658 bool matched;
659 const char *result = 0;
660
661 if( 0 == (prop = table->getProperty( propName )))
662 return( 0 );
663
664 if( (data = OSDynamicCast( OSData, prop ))) {
665 names = (const char *) data->getBytesNoCopy();
666 lastName = names + data->getLength();
667 } else if( (string = OSDynamicCast( OSString, prop ))) {
668 names = string->getCStringNoCopy();
669 lastName = names + string->getLength() + 1;
670 } else
671 return( 0 );
672
673 ckey = key->getCStringNoCopy();
674 keyLen = key->getLength();
675 wild = ('*' == key->getChar( keyLen - 1 ));
676
677 do {
678 // for each name in the property
679 if( wild)
680 matched = (0 == strncmp( ckey, names, keyLen - 1 ));
681 else
682 matched = (keyLen == strlen( names ))
683 && (0 == strncmp( ckey, names, keyLen ));
684
685 if( matched)
686 result = names;
687
688 names = names + strlen( names) + 1;
689
690 } while( (names < lastName) && (false == matched));
691
692 return( result);
693 }
694
695
696 bool IODTCompareNubName( const IORegistryEntry * regEntry,
697 OSString * name, OSString ** matchingName )
698 {
699 const char *result;
700 bool matched;
701
702 matched = (0 != (result = CompareKey( name, regEntry, gIODTNameKey)))
703 || (0 != (result = CompareKey( name, regEntry, gIODTCompatibleKey)))
704 || (0 != (result = CompareKey( name, regEntry, gIODTTypeKey)))
705 || (0 != (result = CompareKey( name, regEntry, gIODTModelKey)));
706
707 if( result && matchingName)
708 *matchingName = OSString::withCString( result );
709
710 return( result != 0 );
711 }
712
713 bool IODTMatchNubWithKeys( IORegistryEntry * regEntry,
714 const char * keys )
715 {
716 OSObject *obj;
717 bool result = false;
718
719 obj = OSUnserialize( keys, 0 );
720
721 if( obj) {
722 result = regEntry->compareNames( obj );
723 obj->release();
724 }
725 #ifdef DEBUG
726 else IOLog("Couldn't unserialize %s\n", keys );
727 #endif
728
729 return( result );
730 }
731
732 OSCollectionIterator * IODTFindMatchingEntries( IORegistryEntry * from,
733 IOOptionBits options, const char * keys )
734 {
735 OSSet *result = 0;
736 IORegistryEntry *next;
737 IORegistryIterator *iter;
738 OSCollectionIterator *cIter;
739 bool cmp;
740 bool minus = options & kIODTExclusive;
741
742
743 iter = IORegistryIterator::iterateOver( from, gIODTPlane,
744 (options & kIODTRecursive) ? kIORegistryIterateRecursively : 0 );
745 if( iter) {
746
747 do {
748
749 if( result)
750 result->release();
751 result = OSSet::withCapacity( 3 );
752 if( !result)
753 break;
754
755 iter->reset();
756 while( (next = iter->getNextObject())) {
757
758 // Look for existence of a debug property to skip
759 if( next->getProperty("AAPL,ignore"))
760 continue;
761
762 if( keys) {
763 cmp = IODTMatchNubWithKeys( next, keys );
764 if( (minus && (false == cmp))
765 || ((false == minus) && (false != cmp)) )
766 result->setObject( next);
767 } else
768 result->setObject( next);
769 }
770 } while( !iter->isValid());
771
772 iter->release();
773 }
774
775 cIter = OSCollectionIterator::withCollection( result);
776 result->release();
777
778 return( cIter);
779 }
780
781
782 struct IODTPersistent {
783 IODTCompareAddressCellFunc compareFunc;
784 IODTNVLocationFunc locationFunc;
785 };
786
787 void IODTSetResolving( IORegistryEntry * regEntry,
788 IODTCompareAddressCellFunc compareFunc,
789 IODTNVLocationFunc locationFunc )
790 {
791 IODTPersistent persist;
792 OSData *prop;
793
794 persist.compareFunc = compareFunc;
795 persist.locationFunc = locationFunc;
796 prop = OSData::withBytes( &persist, sizeof( persist));
797 if( !prop)
798 return;
799
800 regEntry->setProperty( gIODTPersistKey, prop);
801 prop->release();
802 return;
803 }
804
805 static SInt32 DefaultCompare( UInt32 cellCount, UInt32 left[], UInt32 right[] )
806 {
807 cellCount--;
808 return( left[ cellCount ] - right[ cellCount ] );
809 }
810
811
812 void IODTGetCellCounts( IORegistryEntry * regEntry,
813 UInt32 * sizeCount, UInt32 * addressCount)
814 {
815 if( !GetUInt32( regEntry, gIODTSizeCellKey, sizeCount))
816 *sizeCount = 1;
817 if( !GetUInt32( regEntry, gIODTAddressCellKey, addressCount))
818 *addressCount = 2;
819 return;
820 }
821
822 // Given addr & len cells from our child, find it in our ranges property, then
823 // look in our parent to resolve the base of the range for us.
824
825 // Range[]: child-addr our-addr child-len
826 // #cells: child ours child
827
828 bool IODTResolveAddressCell( IORegistryEntry * regEntry,
829 UInt32 cellsIn[],
830 IOPhysicalAddress * phys, IOPhysicalLength * len )
831 {
832 IORegistryEntry *parent;
833 OSData *prop;
834 // cells in addresses at regEntry
835 UInt32 sizeCells, addressCells;
836 // cells in addresses below regEntry
837 UInt32 childSizeCells, childAddressCells;
838 UInt32 childCells;
839 UInt32 cell[ 5 ], offset = 0, length;
840 UInt32 *range;
841 UInt32 *endRanges;
842 bool ok = true;
843 SInt32 diff;
844
845 IODTPersistent *persist;
846 IODTCompareAddressCellFunc compare;
847
848 IODTGetCellCounts( regEntry, &childSizeCells, &childAddressCells );
849 childCells = childAddressCells + childSizeCells;
850
851 bcopy( cellsIn, cell, 4 * childCells );
852 if( childSizeCells > 1)
853 *len = IOPhysical32( cellsIn[ childAddressCells ],
854 cellsIn[ childAddressCells + 1 ] );
855 else
856 *len = IOPhysical32( 0, cellsIn[ childAddressCells ] );
857
858 do {
859 prop = OSDynamicCast( OSData, regEntry->getProperty( gIODTRangeKey ));
860 if( 0 == prop) {
861 /* end of the road */
862 *phys = IOPhysical32( 0, cell[ childAddressCells - 1 ] + offset);
863 break;
864 }
865
866 parent = regEntry->getParentEntry( gIODTPlane );
867 IODTGetCellCounts( parent, &sizeCells, &addressCells );
868
869 if( (length = prop->getLength())) {
870 // search
871 range = (UInt32 *) prop->getBytesNoCopy();
872 endRanges = range + (length / 4);
873
874 prop = (OSData *) regEntry->getProperty( gIODTPersistKey );
875 if( prop) {
876 persist = (IODTPersistent *) prop->getBytesNoCopy();
877 compare = persist->compareFunc;
878 } else
879 compare = DefaultCompare;
880
881 for( ok = false;
882 range < endRanges;
883 range += (childCells + addressCells) ) {
884
885 // is cell >= range start?
886 diff = (*compare)( childAddressCells, cell, range );
887 if( diff < 0)
888 continue;
889
890 // is cell + size <= range end?
891 if( (diff + cell[ childCells - 1 ])
892 > range[ childCells + addressCells - 1 ])
893 continue;
894
895 offset += diff;
896 ok = true;
897 break;
898 }
899
900 // Get the physical start of the range from our parent
901 bcopy( range + childAddressCells, cell, 4 * addressCells );
902 bzero( cell + addressCells, 4 * sizeCells );
903
904 } /* else zero length range => pass thru to parent */
905
906 regEntry = parent;
907 childSizeCells = sizeCells;
908 childAddressCells = addressCells;
909 childCells = childAddressCells + childSizeCells;
910
911 } while( ok && regEntry);
912
913 return( ok);
914 }
915
916
917 OSArray * IODTResolveAddressing( IORegistryEntry * regEntry,
918 const char * addressPropertyName,
919 IODeviceMemory * parent )
920 {
921 IORegistryEntry *parentEntry;
922 OSData *addressProperty;
923 UInt32 sizeCells, addressCells, cells;
924 int i, num;
925 UInt32 *reg;
926 IOPhysicalAddress phys;
927 IOPhysicalLength len;
928 OSArray *array;
929 IODeviceMemory *range;
930
931 parentEntry = regEntry->getParentEntry( gIODTPlane );
932 addressProperty = (OSData *) regEntry->getProperty( addressPropertyName );
933 if( (0 == addressProperty) || (0 == parentEntry))
934 return( 0);
935
936 IODTGetCellCounts( parentEntry, &sizeCells, &addressCells );
937 if( 0 == sizeCells)
938 return( 0);
939
940 cells = sizeCells + addressCells;
941 reg = (UInt32 *) addressProperty->getBytesNoCopy();
942 num = addressProperty->getLength() / (4 * cells);
943
944 array = OSArray::withCapacity( 1 );
945 if( 0 == array)
946 return( 0);
947
948 for( i = 0; i < num; i++) {
949 if( IODTResolveAddressCell( parentEntry, reg, &phys, &len )) {
950 range = 0;
951 if( parent)
952 range = IODeviceMemory::withSubRange( parent,
953 phys - parent->getPhysicalAddress(), len );
954 if( 0 == range)
955 range = IODeviceMemory::withRange( phys, len );
956 if( range)
957 array->setObject( range );
958 }
959 reg += cells;
960 }
961
962 regEntry->setProperty( gIODeviceMemoryKey, array);
963 array->release(); /* ??? */
964
965 return( array);
966 }
967
968 static void IODTGetNVLocation(
969 IORegistryEntry * parent,
970 IORegistryEntry * regEntry,
971 UInt8 * busNum, UInt8 * deviceNum, UInt8 * functionNum )
972 {
973
974 OSData *prop;
975 IODTPersistent *persist;
976 UInt32 *cell;
977
978 prop = (OSData *) parent->getProperty( gIODTPersistKey );
979 if( prop) {
980 persist = (IODTPersistent *) prop->getBytesNoCopy();
981 (*persist->locationFunc)( regEntry, busNum, deviceNum, functionNum );
982 } else {
983 prop = (OSData *) regEntry->getProperty( "reg" );
984 *functionNum = 0;
985 if( prop) {
986 cell = (UInt32 *) prop->getBytesNoCopy();
987 *busNum = 3;
988 *deviceNum = 0x1f & (cell[ 0 ] >> 24);
989 } else {
990 *busNum = 0;
991 *deviceNum = 0;
992 }
993 }
994 return;
995 }
996
997 /*
998 * Try to make the same messed up descriptor as Mac OS
999 */
1000
1001 IOReturn IODTMakeNVDescriptor( IORegistryEntry * regEntry,
1002 IONVRAMDescriptor * hdr )
1003 {
1004 IORegistryEntry *parent;
1005 UInt32 level;
1006 UInt32 bridgeDevices;
1007 UInt8 busNum;
1008 UInt8 deviceNum;
1009 UInt8 functionNum;
1010
1011 hdr->format = 1;
1012 hdr->marker = 0;
1013
1014 for(level = 0, bridgeDevices = 0;
1015 (parent = regEntry->getParentEntry( gIODTPlane )) && (level < 7); level++ ) {
1016
1017 IODTGetNVLocation( parent, regEntry,
1018 &busNum, &deviceNum, &functionNum );
1019 if( level)
1020 bridgeDevices |= ((deviceNum & 0x1f) << ((level - 1) * 5));
1021 else {
1022 hdr->busNum = busNum;
1023 hdr->deviceNum = deviceNum;
1024 hdr->functionNum = functionNum;
1025 }
1026 regEntry = parent;
1027 }
1028 hdr->bridgeCount = level - 2;
1029 hdr->bridgeDevices = bridgeDevices;
1030
1031 return( kIOReturnSuccess );
1032 }
1033
1034 OSData * IODTFindSlotName( IORegistryEntry * regEntry, UInt32 deviceNumber )
1035 {
1036 IORegistryEntry *parent;
1037 OSData *data;
1038 OSData *ret = 0;
1039 UInt32 *bits;
1040 UInt32 i;
1041 char *names;
1042 char *lastName;
1043 UInt32 mask;
1044
1045 data = (OSData *) regEntry->getProperty("AAPL,slot-name");
1046 if( data)
1047 return( data);
1048 parent = regEntry->getParentEntry( gIODTPlane );
1049 if( !parent)
1050 return( 0 );
1051 data = OSDynamicCast( OSData, parent->getProperty("slot-names"));
1052 if( !data)
1053 return( 0 );
1054 if( data->getLength() <= 4)
1055 return( 0 );
1056
1057 bits = (UInt32 *) data->getBytesNoCopy();
1058 mask = *bits;
1059 if( (0 == (mask & (1 << deviceNumber))))
1060 return( 0 );
1061
1062 names = (char *)(bits + 1);
1063 lastName = names + (data->getLength() - 4);
1064
1065 for( i = 0; (i <= deviceNumber) && (names < lastName); i++ ) {
1066
1067 if( mask & (1 << i)) {
1068 if( i == deviceNumber) {
1069 data = OSData::withBytesNoCopy( names, 1 + strlen( names));
1070 if( data) {
1071 regEntry->setProperty("AAPL,slot-name", data);
1072 ret = data;
1073 data->release();
1074 }
1075 } else
1076 names += 1 + strlen( names);
1077 }
1078 }
1079
1080 return( ret );
1081 }