-
Notifications
You must be signed in to change notification settings - Fork 4
/
IOPCIBridge.cpp
7001 lines (5967 loc) · 220 KB
/
IOPCIBridge.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1998-2021 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* The contents of this file constitute Original Code as defined in and
* are subject to the Apple Public Source License Version 1.1 (the
* "License"). You may not use this file except in compliance with the
* License. Please obtain a copy of the License at
* http://www.apple.com/publicsource and read it before using this file.
*
* This Original Code and all software distributed under the License are
* distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
* License for the specific language governing rights and limitations
* under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
#include <IOKit/system.h>
#include <IOKit/IOCommandGate.h>
#include <IOKit/pci/IOPCIPrivate.h>
#include <IOKit/pci/IOPCIBridge.h>
#include <IOKit/pci/IOAGPDevice.h>
#include <IOKit/pci/IOPCIConfigurator.h>
#if ACPI_SUPPORT
#include <IOKit/acpi/IOACPIPlatformDevice.h>
#include "AppleVTD.h"
#elif TARGET_CPU_ARM64 || TARGET_CPU_ARM
#include <IOKit/dart/IODARTKeys.h>
#include <IOKit/IOMapper.h>
#endif
#include <IOKit/IODeviceTreeSupport.h>
#include <IOKit/IOSubMemoryDescriptor.h>
#include <IOKit/IORangeAllocator.h>
#include <IOKit/IOPlatformExpert.h>
#include <IOKit/pwr_mgt/IOPMPrivate.h>
#include <IOKit/pwr_mgt/IOPowerConnection.h>
#include <IOKit/IOLib.h>
#include <IOKit/IOKitKeys.h>
#include <IOKit/IOKitKeysPrivate.h>
#include <IOKit/IOMessage.h>
#include <IOKit/assert.h>
#include <IOKit/IOCatalogue.h>
#include <IOKit/IOFilterInterruptEventSource.h>
#include <IOKit/IOTimerEventSource.h>
#include <IOKit/IOPolledInterface.h>
#include <IOKit/IOUserClient.h>
#include <libkern/c++/OSContainers.h>
#include <libkern/OSKextLib.h>
#define TARGET_OS_HAS_THUNDERBOLT __has_include(<IOKit/thunderbolt/IOThunderboltPort.h>)
#if TARGET_OS_HAS_THUNDERBOLT
#include <IOKit/thunderbolt/IOThunderboltPort.h>
#endif
extern "C"
{
#include <machine/machine_routines.h>
};
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef kIOPolledInterfaceActiveKey
#define kIOPolledInterfaceActiveKey "IOPolledInterfaceActive"
#endif
// #define DEADTEST "UPS0"
// #define DEFERTEST 1
enum { kAERISRNum = 4 };
enum { kIOPCIEventNum = 8 };
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
__exported_push
__kpi_unavailable const OSSymbol * gIOPCITunnelIDKey;
__kpi_unavailable const OSSymbol * gIOPCITunnelControllerKey;
__kpi_unavailable const OSSymbol * gIOPCITunnelledKey;
__kpi_unavailable const OSSymbol * gIOPCIHPTypeKey;
__kpi_unavailable const OSSymbol * gIOPCIThunderboltKey;
__kpi_unavailable const OSSymbol * gIOPCIHotplugCapableKey;
__kpi_unavailable const OSSymbol * gIOPCITunnelL1EnableKey;
__kpi_unavailable const OSSymbol * gIOPCIExpressLinkStatusKey;
__kpi_unavailable const OSSymbol * gIOPlatformDeviceMessageKey;
__kpi_unavailable const OSSymbol * gIOPlatformDeviceASPMEnableKey;
__kpi_unavailable const OSSymbol * gIOPlatformSetDeviceInterruptsKey;
__kpi_unavailable const OSSymbol * gIOPlatformResolvePCIInterruptKey;
__kpi_unavailable const OSSymbol * gIOPlatformFreeDeviceResourcesKey;
__kpi_unavailable const OSSymbol * gIOPlatformGetMessagedInterruptControllerKey;
__kpi_unavailable const OSSymbol * gIOPlatformGetMessagedInterruptAddressKey;
__kpi_unavailable const OSSymbol * gIOPlatformDeviceRelocatedKey;
__kpi_unavailable const OSSymbol * gIOPolledInterfaceActiveKey;
__kpi_unavailable const OSSymbol * gIOPCIDeviceHiddenKey;
__kpi_unavailable const OSSymbol * gIOPCIDeviceChangedKey;
__kpi_unavailable const OSSymbol * gIOPCILinkUpTimeoutKey;
__exported_pop
uint32_t gIOPCIFlags = 0
| 0*kIOPCIConfiguratorFPBEnable
| kIOPCIConfiguratorPFM64
| kIOPCIConfiguratorCheckTunnel
| kIOPCIConfiguratorTBMSIEnable
#if ACPI_SUPPORT
| 0*kIOPCIConfiguratorDeviceMap
#else
| kIOPCIConfiguratorAER
#endif
// | kIOPCIConfiguratorTBUSBCPanics
// | kIOPCIConfiguratorDeepIdle
// | kIOPCIConfiguratorNoSplay
// | kIOPCIConfiguratorNoTB
// | kIOPCIConfiguratorIOLog | kIOPCIConfiguratorKPrintf
;
#define DLOG(fmt, args...) \
do { \
if ((gIOPCIFlags & kIOPCIConfiguratorIOLog) && !ml_at_interrupt_context()) \
IOLog(fmt, ## args); \
if (gIOPCIFlags & kIOPCIConfiguratorKPrintf) \
kprintf(fmt, ## args); \
} while(0)
#if ACPI_SUPPORT
extern IOPCIHostBridgeData *gBridgeData;
#endif
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
enum
{
// data link change, hot plug, presence detect change
kSlotControlEnables = ((1 << 12) | (1 << 5) | (1 << 3) | (1 << 0))
};
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
struct IOPCIAERISREntry
{
uint32_t source;
uint32_t status;
};
struct IOPCIAERRoot
{
IOPCIAERISREntry * fISRErrors;
uint8_t fAERReadIndex;
uint8_t fAERWriteIndex;
};
#undef super
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#define super IOService
OSDefineMetaClassAndStructors(IOPCIHostBridgeData, super);
bool IOPCIHostBridgeData::init(void)
{
uint32_t debug;
_wakeCount = 0x100000001ULL;
_allPCI2PCIBridgeState = 0;
_allPCI2PCIBridgesLock = IOSimpleLockAlloc();
_eventSourceLock = IORecursiveLockAlloc();
queue_init(&_allPCIDeviceRestoreQ);
queue_init(&_eventSourceQueue);
_wakeReasonLock = IOLockAlloc();
_configWorkLoop = IOWorkLoop::workLoop();
thread_set_thread_name(_configWorkLoop->getThread(), "IOPCIConfigurator");
// pci-workloop-timeout-ms is passed to IOWorkLoop::setMaximumLockTime(); see IOWorkLoop.h
// for more details.
uint64_t workloopTimeout;
if (!PE_parse_boot_argn("pci-workloop-timeout-ms", &workloopTimeout, sizeof(workloopTimeout)))
workloopTimeout = 0;
if (workloopTimeout)
{
uint64_t maxLockTime;
clock_interval_to_absolutetime_interval(workloopTimeout, kMillisecondScale, &maxLockTime);
_configWorkLoop->setMaximumLockTime(maxLockTime, 0);
}
_waitingPauseSet = OSSet::withCapacity(4);
_pausedSet = OSSet::withCapacity(4);
_probeSet = OSSet::withCapacity(4);
_publishSet = OSSet::withCapacity(4);
_isUSBCSystem = false;
_tunnelSleep = 0;
_tunnelWait = 0;
#if ACPI_SUPPORT
_vtdInterruptsInstalled = false;
#else
_powerChildrenLock = IOLockAlloc();
if (_powerChildrenLock == NULL)
{
panic("Cannot allocate _powerChildrenLock");
}
_powerChildren = OSSet::withCapacity(20);
if (_powerChildren == NULL) {
panic("Cannot allocate _powerChildren");
}
_activePowerChildren = OSSet::withCapacity(20);
if (_activePowerChildren == NULL) {
panic ("Cannot allocate _activePowerChildren");
}
#endif
_aspmDefault = 0xFFFF;
if (PE_parse_boot_argn("pci-aspm-default", &debug, sizeof(debug)))
_aspmDefault = debug;
_l1ssOverride = 0xFFFFFFFF;
if (PE_parse_boot_argn("pci-l1ss-override", &debug, sizeof(debug)))
_l1ssOverride = debug & 0xF;
_systemActive = true;
IOService::getPMRootDomain()->registerInterest(gIOPriorityPowerStateInterest, &IOPCIHostBridgeData::systemPowerChange, 0, this);
if (IOService::getPMRootDomain()->getProperty(kIOPMDeepIdleSupportedKey))
{
if (!(PE_parse_boot_argn("acpi", &debug, sizeof(debug)) && (0x10000 & debug)))
{
gIOPCIFlags |= kIOPCIConfiguratorDeepIdle;
}
}
// Create a power assertion and timer. These will be enabled upon request
// by a bridge or when a descendant of the host bridge is published.
_powerAssertion = kIOPMUndefinedDriverAssertionID;
char pmAssertionString[128] = { 0 };
snprintf(pmAssertionString, sizeof(pmAssertionString), "com.apple.pci.hostBridge.preventSleep");
_powerAssertion = getPMRootDomain()->createPMAssertion(kIOPMDriverAssertionCPUBit, kIOPMDriverAssertionLevelOff, this, pmAssertionString);
assert(_powerAssertion != kIOPMUndefinedDriverAssertionID);
_powerAssertionTimer = IOTimerEventSource::timerEventSource(this, OSMemberFunctionCast(IOTimerEventSource::Action, this, &IOPCIHostBridgeData::powerAssertionTimeout));
assert(_powerAssertionTimer != NULL);
_configurator = OSTypeAlloc(IOPCIConfigurator);
if (!_configurator || !_configurator->init(_configWorkLoop, gIOPCIFlags))
{
panic("!IOPCIConfigurator");
}
return super::init();
}
void IOPCIHostBridgeData::free(void)
{
if(_powerAssertion != kIOPMUndefinedDriverAssertionID)
{
getPMRootDomain()->releasePMAssertion(_powerAssertion);
_powerAssertion = kIOPMUndefinedDriverAssertionID;
}
if (_powerAssertionTimer)
{
_powerAssertionTimer->cancelTimeout();
_configWorkLoop->removeEventSource(_powerAssertionTimer);
OSSafeReleaseNULL(_powerAssertionTimer);
}
if (_allPCI2PCIBridgesLock)
{
IOSimpleLockFree(_allPCI2PCIBridgesLock);
_allPCI2PCIBridgesLock = nullptr;
}
if (_eventSourceLock)
{
IORecursiveLockFree(_eventSourceLock);
_eventSourceLock = nullptr;
}
if (_wakeReasonLock)
{
IOLockFree(_wakeReasonLock);
_wakeReasonLock = nullptr;
}
OSSafeReleaseNULL(_configurator);
OSSafeReleaseNULL(_configWorkLoop);
OSSafeReleaseNULL(_waitingPauseSet);
OSSafeReleaseNULL(_pausedSet);
OSSafeReleaseNULL(_probeSet);
OSSafeReleaseNULL(_publishSet);
#if !ACPI_SUPPORT
OSSafeReleaseNULL(_powerChildren);
OSSafeReleaseNULL(_activePowerChildren);
IOLockFree(_powerChildrenLock);
#endif
super::free();
}
void IOPCIHostBridgeData::lockWakeReasonLock(void)
{
IOLockLock(_wakeReasonLock);
}
void IOPCIHostBridgeData::unlockWakeReasonLock(void)
{
IOLockUnlock(_wakeReasonLock);
}
void IOPCIHostBridgeData::tunnelSleepIncrement(const char * deviceName, bool increment)
{
lockWakeReasonLock();
if (increment)
{
_tunnelSleep++;
DLOG("%s: tunnel sleep %d\n", deviceName, _tunnelSleep);
}
else
{
DLOG("%s: tunnel wake %d\n", deviceName, _tunnelSleep);
_tunnelSleep--;
if (_tunnelWait && !--_tunnelWait)
{
IOLockWakeup(_wakeReasonLock, &_tunnelWait, false);
}
}
unlockWakeReasonLock();
}
void IOPCIHostBridgeData::tunnelsWait(IOPCIDevice * device)
{
lockWakeReasonLock();
DLOG("%s: tunnel stall(%d, %d)\n", device->getName(), _tunnelWait, _tunnelSleep);
if (_tunnelWait)
{
IOLockSleep(_wakeReasonLock, &_tunnelWait, THREAD_UNINT);
DLOG("%s: tunnels done\n", device->getName());
}
unlockWakeReasonLock();
}
IOReturn IOPCIHostBridgeData::addPCIEPowerChild(IOService *theChild)
{
#if !ACPI_SUPPORT
IOPCIDevice *childDevice = OSDynamicCast(IOPCIDevice, theChild);
IOPCIBridge *childBridge = OSDynamicCast(IOPCIBridge, theChild);
if (childBridge || childDevice)
{
IOPCIAddressSpace space;
IOLockLock(_powerChildrenLock);
_powerChildren->setObject(theChild);
_activePowerChildren->setObject(theChild);
IOLockUnlock(_powerChildrenLock);
if (childBridge)
{
space = childBridge->getBridgeSpace();
}
else
{
space = childDevice->space;
}
theChild->registerInterestedDriver(this);
DLOG("%s: %p %s %u:%u:%u (%s): now we have %d/%d children\n", __PRETTY_FUNCTION__, theChild, theChild->getName(),
space.s.busNum, space.s.deviceNum, space.s.functionNum,
childBridge ? "bridge" : "device",
_activePowerChildren->getCount(), _powerChildren->getCount());
}
else
{
DLOG("%s: %p %s: is not a PCIe device or bridge, so no monitoring is intended\n", __PRETTY_FUNCTION__, theChild, theChild->getName());
}
#endif
return kIOReturnSuccess;
}
IOReturn IOPCIHostBridgeData::removePCIEPowerChild(IOPowerConnection *theChild)
{
#if !ACPI_SUPPORT
IOService *childPower = OSDynamicCast(IOService, theChild->getChildEntry(gIOPowerPlane));
if (childPower)
{
childPower->deRegisterInterestedDriver(this);
}
IOLockLock(_powerChildrenLock);
_powerChildren->removeObject(childPower);
_activePowerChildren->removeObject(childPower);
IOLockUnlock(_powerChildrenLock);
DLOG("%s: %p %s: now we have %d/%d children\n", __PRETTY_FUNCTION__, childPower, childPower->getName(), _activePowerChildren->getCount(), _powerChildren->getCount());
#endif
return kIOReturnSuccess;
}
IOReturn IOPCIHostBridgeData::powerStateDidChangeTo(IOPMPowerFlags capabilities, unsigned long stateNumber, IOService *whatDevice)
{
#if !ACPI_SUPPORT
IOLockLock(_powerChildrenLock);
if (_powerChildren->member(whatDevice))
{
switch (stateNumber) {
case kIOPCIDeviceOnState:
_activePowerChildren->setObject(whatDevice);
break;
default:
_activePowerChildren->removeObject(whatDevice);
break;
}
}
IOLockUnlock(_powerChildrenLock);
DLOG("%s: %p %s -> state %lu: now we have %d/%d children\n", __PRETTY_FUNCTION__, whatDevice, whatDevice->getName(), stateNumber, _activePowerChildren->getCount(), _powerChildren->getCount());
#endif
return super::powerStateDidChangeTo(capabilities, stateNumber, whatDevice);
}
static bool isDescendant(IOService *newService, IOService *device)
{
IORegistryEntry *entry = OSDynamicCast(IORegistryEntry, newService);
if (entry == NULL)
{
return NULL;
}
entry = entry->getParentEntry(gIOServicePlane);
while (entry && (entry != device))
{
entry = entry->getParentEntry(gIOServicePlane);
}
return (entry != NULL);
}
#define kIOPCIHostBridgeMatchTimeoutMS (5 * 1000)
// Prevent system sleep prior to probing and while descendants match, to ensure enumeration occurs
// outside of system power state transitions. Use a kIOPCIHostBridgeMatchTimeoutMS (arbitrary)
// timeout to release the assertion and restart the timer each time a descendant is published.
void IOPCIHostBridgeData::restartPowerAssertionTimer(void)
{
_configWorkLoop->runActionBlock(^IOReturn
{
// If the power assertion is on, reset the timeout to kIOPCIHostBridgeMatchTimeoutMS from now
if(getPMRootDomain()->getPMAssertionLevel(_powerAssertion) == kIOPMDriverAssertionLevelOn)
{
DLOG("[%s()] Restarting the host bridge's power assertion timeout\n", __func__);
_powerAssertionTimer->cancelTimeout();
_powerAssertionTimer->setTimeoutMS(kIOPCIHostBridgeMatchTimeoutMS);
}
// If power assertion is off, enable it and start the timer
else
{
DLOG("[%s()] Enabling the host bridge's PM assertion\n", __func__);
getPMRootDomain()->setPMAssertionLevel(_powerAssertion, kIOPMDriverAssertionLevelOn);
_configWorkLoop->addEventSource(_powerAssertionTimer);
_powerAssertionTimer->enable();
_powerAssertionTimer->setTimeoutMS(kIOPCIHostBridgeMatchTimeoutMS);
}
return kIOReturnSuccess;
});
return true;
}
void IOPCIHostBridgeData::disablePowerAssertion(void)
{
DLOG("[%s()] Disabling the host bridge's PM assertion\n", __func__);
getPMRootDomain()->setPMAssertionLevel(_powerAssertion, kIOPMDriverAssertionLevelOff);
_powerAssertionTimer->cancelTimeout();
_powerAssertionTimer->disable();
_configWorkLoop->removeEventSource(_powerAssertionTimer);
}
void IOPCIHostBridgeData::powerAssertionTimeout(IOTimerEventSource* timer __unused)
{
DLOG("[%s()] device %s's PM assertion (%p) timeout fired\n", __func__, getName(), _powerAssertion);
disablePowerAssertion();
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#undef super
#define super IOPCIBridge
OSDefineMetaClassAndAbstractStructors(IOPCIHostBridge, super);
IOService* IOPCIHostBridge::probe(IOService * provider, SInt32 *score)
{
super::probe(provider, score);
#if ACPI_SUPPORT
InitSharedBridgeData();
bridgeData = gBridgeData;
gBridgeData->retain();
IOSimpleLockLock(gBridgeData->_allPCI2PCIBridgesLock);
if (gBridgeData->_vtdInterruptsInstalled == false)
{
gBridgeData->_vtdInterruptsInstalled = true;
AppleVTD::installInterrupts();
}
IOSimpleLockUnlock(gBridgeData->_allPCI2PCIBridgesLock);
#else
bridgeData = OSTypeAlloc(IOPCIHostBridgeData);
if (bridgeData == NULL || bridgeData->init() == false)
{
panic("Failed to initialize host bridge data structure");
return NULL;
}
#endif
return this;
}
bool IOPCIHostBridge::start(IOService *provider)
{
if (!super::start(provider))
return (false);
// Create a publish notifier with an empty matching dictionary, so all
// services match. The callbacks will determine whether this IOPCIHostBridge
// is an ancestor of the published service.
OSDictionary* matchingDictionary = OSDictionary::withCapacity(1);
assert(matchingDictionary);
_publishNotifier = addMatchingNotification(gIOPublishNotification, matchingDictionary,
OSMemberFunctionCast(IOServiceMatchingNotificationHandler,
this,
&IOPCIHostBridge::childPublished),
this, 0, INT_MIN);
OSSafeReleaseNULL(matchingDictionary);
return true;
}
void IOPCIHostBridge::free(void)
{
if (_publishNotifier != NULL)
{
_publishNotifier->remove();
_publishNotifier = NULL;
}
OSSafeReleaseNULL(bridgeData);
super::free();
}
bool IOPCIHostBridge::configure(IOService * provider)
{
reserved->hostBridgeData = bridgeData;
reserved->hostBridgeData->retain();
return super::configure(provider);
}
IOReturn IOPCI2PCIBridge::addPowerChild(IOService *theChild)
{
#if !ACPI_SUPPORT
IOPCIHostBridgeData *vars = ((IOPCIBridge*)this)->reserved->hostBridgeData;
vars->addPCIEPowerChild(theChild);
#endif
return IOPCIBridge::addPowerChild(theChild);
}
IOReturn IOPCI2PCIBridge::removePowerChild(IOPowerConnection * theChild)
{
#if !ACPI_SUPPORT
IOPCIHostBridgeData *vars = ((IOPCIBridge*)this)->reserved->hostBridgeData;
vars->removePCIEPowerChild(theChild);
#endif
return IOPCIBridge::removePowerChild(theChild);
}
bool IOPCIHostBridge::allChildrenPoweredOn(void)
{
#if ACPI_SUPPORT
return true;
#else
IOPCIHostBridgeData *vars = reserved->hostBridgeData;
bool result = true;
IOLockLock(vars->_powerChildrenLock);
result = (vars->_powerChildren->getCount() == vars->_activePowerChildren->getCount());
IOLockUnlock(vars->_powerChildrenLock);
return result;
#endif
}
bool IOPCIHostBridge::childPublished(void* refcon __unused, IOService* newService, IONotifier* notifier __unused)
{
if (!isDescendant(newService, this))
{
return true;
}
DLOG("[%s()] device %s's child %s published\n", __func__, getName(), newService->getName());
bridgeData->restartPowerAssertionTimer();
return true;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#undef super
#define super IOService
OSDefineMetaClassAndAbstractStructorsWithInit( IOPCIBridge, IOService, IOPCIBridge::initialize() )
OSMetaClassDefineReservedUsed(IOPCIBridge, 0);
OSMetaClassDefineReservedUsed(IOPCIBridge, 1);
OSMetaClassDefineReservedUsed(IOPCIBridge, 2);
OSMetaClassDefineReservedUsed(IOPCIBridge, 3);
OSMetaClassDefineReservedUsed(IOPCIBridge, 4);
OSMetaClassDefineReservedUsed(IOPCIBridge, 5);
OSMetaClassDefineReservedUsed(IOPCIBridge, 6);
OSMetaClassDefineReservedUsed(IOPCIBridge, 7);
OSMetaClassDefineReservedUsed(IOPCIBridge, 8);
OSMetaClassDefineReservedUsed(IOPCIBridge, 9);
OSMetaClassDefineReservedUsed(IOPCIBridge, 10);
OSMetaClassDefineReservedUsed(IOPCIBridge, 11);
OSMetaClassDefineReservedUsed(IOPCIBridge, 12);
OSMetaClassDefineReservedUsed(IOPCIBridge, 13);
OSMetaClassDefineReservedUnused(IOPCIBridge, 14);
OSMetaClassDefineReservedUnused(IOPCIBridge, 15);
OSMetaClassDefineReservedUnused(IOPCIBridge, 16);
OSMetaClassDefineReservedUnused(IOPCIBridge, 17);
OSMetaClassDefineReservedUnused(IOPCIBridge, 18);
OSMetaClassDefineReservedUnused(IOPCIBridge, 19);
OSMetaClassDefineReservedUnused(IOPCIBridge, 20);
OSMetaClassDefineReservedUnused(IOPCIBridge, 21);
OSMetaClassDefineReservedUnused(IOPCIBridge, 22);
OSMetaClassDefineReservedUnused(IOPCIBridge, 23);
OSMetaClassDefineReservedUnused(IOPCIBridge, 24);
OSMetaClassDefineReservedUnused(IOPCIBridge, 25);
OSMetaClassDefineReservedUnused(IOPCIBridge, 26);
OSMetaClassDefineReservedUnused(IOPCIBridge, 27);
OSMetaClassDefineReservedUnused(IOPCIBridge, 28);
OSMetaClassDefineReservedUnused(IOPCIBridge, 29);
OSMetaClassDefineReservedUnused(IOPCIBridge, 30);
OSMetaClassDefineReservedUnused(IOPCIBridge, 31);
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef kIOPlatformDeviceMessageKey
#define kIOPlatformDeviceMessageKey "IOPlatformDeviceMessage"
#endif
#ifndef kIOPlatformSetDeviceInterruptsKey
#define kIOPlatformSetDeviceInterruptsKey "SetDeviceInterrupts"
#endif
#ifndef kIOPlatformResolvePCIInterruptKey
#define kIOPlatformResolvePCIInterruptKey "ResolvePCIInterrupt"
#endif
#ifndef kIOPlatformFreeDeviceResourcesKey
#define kIOPlatformFreeDeviceResourcesKey "IOPlatformFreeDeviceResources"
#endif
#ifndef kIOPlatformGetMessagedInterruptAddressKey
#define kIOPlatformGetMessagedInterruptAddressKey "GetMessagedInterruptAddress"
#endif
#ifndef kIOPlatformGetMessagedInterruptControllerKey
#define kIOPlatformGetMessagedInterruptControllerKey "GetMessagedInterruptController"
#endif
#ifndef kIOPlatformDeviceRelocatedKey
#define kIOPlatformDeviceRelocatedKey "IOPlatformDeviceRelocatedKey"
#endif
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void IOPCIBridge::initialize(void)
{
uint32_t debug;
gIOPlatformDeviceMessageKey
= OSSymbol::withCStringNoCopy(kIOPlatformDeviceMessageKey);
gIOPlatformDeviceASPMEnableKey
= OSSymbol::withCStringNoCopy(kIOPlatformDeviceASPMEnableKey);
gIOPlatformSetDeviceInterruptsKey
= OSSymbol::withCStringNoCopy(kIOPlatformSetDeviceInterruptsKey);
gIOPlatformResolvePCIInterruptKey
= OSSymbol::withCStringNoCopy(kIOPlatformResolvePCIInterruptKey);
gIOPlatformFreeDeviceResourcesKey
= OSSymbol::withCStringNoCopy(kIOPlatformFreeDeviceResourcesKey);
gIOPlatformDeviceRelocatedKey
= OSSymbol::withCStringNoCopy(kIOPlatformDeviceRelocatedKey);
gIOPCIDeviceChangedKey
= OSSymbol::withCStringNoCopy(kIOPCIDeviceChangedKey);
gIOPCILinkUpTimeoutKey
= OSSymbol::withCStringNoCopy(kIOPCILinkUpTimeoutKey);
gIOPlatformGetMessagedInterruptAddressKey
= OSSymbol::withCStringNoCopy(kIOPlatformGetMessagedInterruptAddressKey);
gIOPlatformGetMessagedInterruptControllerKey
= OSSymbol::withCStringNoCopy(kIOPlatformGetMessagedInterruptControllerKey);
#if ACPI_SUPPORT
gIOPCIPSMethods[kIOPCIDeviceOffState] = OSSymbol::withCStringNoCopy("_PS3");
gIOPCIPSMethods[kIOPCIDeviceDozeState] = OSSymbol::withCStringNoCopy("RPS3");
gIOPCIPSMethods[kIOPCIDeviceOnState] = OSSymbol::withCStringNoCopy("_PS0");
gIOPCIACPIPlane = IORegistryEntry::getPlane("IOACPIPlane");
gIOPCIFlags |= kIOPCIConfiguratorMapInterrupts;
#endif
gIOPCIFlags |= kIOPCIConfiguratorUsePause;
if (PE_parse_boot_argn("pci", &debug, sizeof(debug)))
gIOPCIFlags |= debug;
if (PE_parse_boot_argn("npci", &debug, sizeof(debug)))
gIOPCIFlags &= ~debug;
gIOPCITunnelIDKey = OSSymbol::withCStringNoCopy(kIOPCITunnelIDKey);
gIOPCITunnelControllerKey = OSSymbol::withCStringNoCopy(kIOPCITunnelControllerIDKey);
gIOPCITunnelledKey = OSSymbol::withCStringNoCopy(kIOPCITunnelledKey);
gIOPCIHPTypeKey = OSSymbol::withCStringNoCopy(kIOPCIHPTypeKey);
gIOPCITunnelL1EnableKey = OSSymbol::withCStringNoCopy(kIOPCITunnelL1EnableKey);
gIOPCIExpressLinkStatusKey = OSSymbol::withCStringNoCopy(kIOPCIExpressLinkStatusKey);
gIOPCIThunderboltKey = OSSymbol::withCStringNoCopy("PCI-Thunderbolt");
gIOPCIHotplugCapableKey = OSSymbol::withCStringNoCopy("PCIHotplugCapable");
gIOPolledInterfaceActiveKey = OSSymbol::withCStringNoCopy(kIOPolledInterfaceActiveKey);
gIOPCIDeviceHiddenKey = OSSymbol::withCStringNoCopy(kIOPCIDeviceHiddenKey);
}
//*********************************************************************************
IOWorkLoop * IOPCIBridge::getConfiguratorWorkLoop(void) const
{
IOPCIHostBridge *selfBridge = OSDynamicCast(IOPCIHostBridge, this);
IOPCIBridge *self = (IOPCIBridge*)this;
if (selfBridge)
{
// We are the host bridge, so use own instance as ExpansionData may not be ready yet.
return selfBridge->bridgeData->_configWorkLoop;
}
else
{
assert(self != NULL);
assert(self->reserved->hostBridgeData != NULL);
return self->reserved->hostBridgeData->_configWorkLoop;
}
}
//*********************************************************************************
// Returns true if the system has booted, is powered, and is not undergoing a power-state transition
bool IOPCIHostBridgeData::systemActive(void)
{
return _systemActive;
}
IOReturn IOPCIHostBridgeData::systemPowerChange(void * target, void * refCon,
UInt32 messageType, IOService * service,
void * messageArgument, vm_size_t argSize)
{
IOPCIHostBridgeData *self = (IOPCIHostBridgeData*) refCon;
switch (messageType)
{
case kIOMessageSystemCapabilityChange:
{
IOPMSystemCapabilityChangeParameters * params = (typeof params) messageArgument;
if (params->changeFlags & kIOPMSystemCapabilityDidChange)
{
// CPU capability did change from off to on
if (((params->fromCapabilities & kIOPMSystemCapabilityCPU) == 0) &&
(params->toCapabilities & kIOPMSystemCapabilityCPU))
{
self->finishMachineState(0);
self->_systemActive = true;
}
#if !ACPI_SUPPORT
// CPU capability did change from on to off
else if ((params->fromCapabilities & kIOPMSystemCapabilityCPU) &&
((params->toCapabilities & kIOPMSystemCapabilityCPU) == 0))
{
self->_wakeCount++;
}
#endif
}
// CPU capability will change from on to off
if ( (params->changeFlags & kIOPMSystemCapabilityWillChange)
&& ((params->fromCapabilities & kIOPMSystemCapabilityCPU))
&& ((params->toCapabilities & kIOPMSystemCapabilityCPU) == 0)) {
self->_systemActive = false;
}
break;
}
case kIOMessageSystemWillRestart:
case kIOMessageSystemWillPowerOff:
self->_systemActive = false;
break;
case kIOMessageSystemHasPoweredOn:
self->_systemActive = true;
break;
case kIOMessageSystemWillSleep:
self->_systemActive = false;
break;
case kIOMessageSystemWillNotSleep:
self->_systemActive = true;
break;
}
return (kIOReturnSuccess);
}
//*********************************************************************************
void IOPCI2PCIBridge::systemWillShutdown(IOOptionBits specifier)
{
if (kIOPCIDeviceOnState == fPowerState) disableBridgeInterrupts();
super::systemWillShutdown(specifier);
}
//*********************************************************************************
IOReturn IOPCIBridge::configOp(configOpParams *params)
{
IOReturn ret = kIOReturnSuccess;
IOPCIDevice *probeDevice = nullptr;
OSSet * changed = nullptr;
IOPCIDevice * next = nullptr;
uint32_t state;
IOPCIHostBridgeData *vars = reserved->hostBridgeData;
if (params->op == kConfigOpPaused)
{
probeDevice = OSDynamicCast(IOPCIDevice, params->device);
assert(probeDevice);
}
// Make sure we are running in gate.
if (!vars->_configWorkLoop->inGate())
{
return (vars->_configWorkLoop->runAction(
OSMemberFunctionCast(IOCommandGate::Action, this, &IOPCIBridge::configOp),
this, params));
}
if (kConfigOpScan != params->op)
{
ret = vars->_configurator->configOp(params->device, params->op, params->result, params->arg);
if (kIOReturnSuccess != ret) return (ret);
next = (IOPCIDevice *) params->device;
if (kConfigOpTerminated == params->op)
{
vars->_waitingPauseSet->removeObject(next);
vars->_pausedSet->removeObject(next);
vars->_probeSet->removeObject(next);
vars->_publishSet->removeObject(next);
}
else if (kConfigOpTestPause == params->op)
{
if (vars->_waitingPauseSet->setObject(next))
{
next->changePowerStateToPriv(kIOPCIDevicePausedState);
next->powerOverrideOnPriv();
}
}
else if (kConfigOpUnpaused == params->op)
{
vars->_pausedSet->removeObject(next);
}
if (params->op != kConfigOpPaused)
{
params->op = 0;
}
else
{
params->op = 0;
DLOG("configOp:->pause: %s(0x%qx)\n", params->device->getName(), params->device->getRegistryEntryID());
if (vars->_waitingPauseSet->containsObject(params->device))
{
vars->_pausedSet->setObject(params->device);
vars->_waitingPauseSet->removeObject(params->device);
if (!vars->_waitingPauseSet->getCount())
{
params->op = kConfigOpRealloc;
}
}
}
}
while (params->op)
{
ret = vars->_configurator->configOp(params->device, params->op, &changed);
params->op = 0;
if (kIOReturnSuccess != ret) break;
if (!changed) break;
while ((next = (IOPCIDevice *) changed->getAnyObject()))
{
ret = vars->_configurator->configOp(next, kConfigOpGetState, &state);
if (kIOReturnSuccess == ret)
{
if (kPCIDeviceStateDead & state)
{
DLOG("configOp:->dead: %s(0x%qx), 0x%x\n", next->getName(), next->getRegistryEntryID(), state);
next->terminate();
}
else if (kPCIDeviceStateRequestPause & state)
{
DLOG("configOp:->pause: %s(0x%qx), 0x%x\n", next->getName(), next->getRegistryEntryID(), state);
if (vars->_waitingPauseSet->setObject(next))
{
next->changePowerStateToPriv(kIOPCIDevicePausedState);
next->powerOverrideOnPriv();
}
}
else
{
DLOG("configOp:->probe: %s(0x%qx), 0x%x\n", next->getName(), next->getRegistryEntryID(), state);
vars->_probeSet->setObject(next);
}
}
changed->removeObject(next);
}
changed->release();
}
if (!vars->_waitingPauseSet->getCount())
{
vars->_pausedSet->iterateObjects(^bool(OSObject *object)
{
IOPCIDevice *next = OSDynamicCast(IOPCIDevice, object);
DLOG("configOp:<-unpause: %s(0x%qx)\n", next->getName(), next->getRegistryEntryID());
if (2 != next->reserved->pauseFlags)
{
next->changePowerStateToPriv(kIOPCIDeviceOnState);
next->powerOverrideOffPriv();
}
next->reserved->pauseFlags = 0;
return false;
});
while ((next = (IOPCIDevice *) vars->_probeSet->getAnyObject()))
{
DLOG("configOp:<-probe: %s(0x%qx), pm %d\n", next->getName(), next->getRegistryEntryID(), next->reserved->pciPMState);
DLOG("[%s()] Adding %s (%u:%u:%u) to the publish set\n", __func__, next->getName(), next->getBusNumber(), next->getDeviceNumber(), next->getFunctionNumber());
vars->_publishSet->setObject(next);
if (kIOPCIDeviceOnState == next->reserved->pciPMState && next != probeDevice) deferredProbe(next);
else next->reserved->needsProbe = true;
vars->_probeSet->removeObject(next);
}
}
return (ret);
}
//*********************************************************************************
void IOPCIBridge::deferredProbe(IOPCIDevice * device)
{
IOService * client;
IOPCIBridge * bridge;
client = device->copyClientWithCategory(gIODefaultMatchCategoryKey);
if ((bridge = OSDynamicCast(IOPCIBridge, client)))
{
DLOG("configOp:<-probe: %s(0x%qx)\n", device->getName(), device->getRegistryEntryID());
// If IOPCIBridge::start() hasn't initialized the ivars needed for probeBus(), then there's no need to it here, it will run during start().
if (atomic_load(&bridge->reserved->readyToProbe))
{
bridge->probeBus(device, bridge->firstBusNum());
}
else
{
DLOG("Device %s not started, skipping probe\n", device->getName());
}
}
if (client) client->release();
device->reserved->needsProbe = false;
}