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