-
Notifications
You must be signed in to change notification settings - Fork 134
/
phb3.c
5063 lines (4357 loc) · 148 KB
/
phb3.c
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
// SPDX-License-Identifier: Apache-2.0
/*
* PHB3: PCI Host Bridge 3, in POWER8
*
* Copyright 2013-2019 IBM Corp.
*/
#include <skiboot.h>
#include <io.h>
#include <timebase.h>
#include <pci-cfg.h>
#include <pci.h>
#include <pci-slot.h>
#include <vpd.h>
#include <interrupts.h>
#include <opal.h>
#include <cpu.h>
#include <device.h>
#include <ccan/str/str.h>
#include <ccan/array_size/array_size.h>
#include <xscom.h>
#include <affinity.h>
#include <phb3.h>
#include <phb3-regs.h>
#include <phb3-capp.h>
#include <capp.h>
#include <fsp.h>
#include <chip.h>
#include <chiptod.h>
/* Enable this to disable error interrupts for debug purposes */
#undef DISABLE_ERR_INTS
static void phb3_init_hw(struct phb3 *p, bool first_init);
#define PHBDBG(p, fmt, a...) prlog(PR_DEBUG, "PHB#%04x: " fmt, \
(p)->phb.opal_id, ## a)
#define PHBINF(p, fmt, a...) prlog(PR_INFO, "PHB#%04x: " fmt, \
(p)->phb.opal_id, ## a)
#define PHBERR(p, fmt, a...) prlog(PR_ERR, "PHB#%04x: " fmt, \
(p)->phb.opal_id, ## a)
#define PE_CAPP_EN 0x9013c03
#define PE_REG_OFFSET(p) \
((PHB3_IS_NAPLES(p) && (p)->index) ? 0x40 : 0x0)
/* Helper to select an IODA table entry */
static inline void phb3_ioda_sel(struct phb3 *p, uint32_t table,
uint32_t addr, bool autoinc)
{
out_be64(p->regs + PHB_IODA_ADDR,
(autoinc ? PHB_IODA_AD_AUTOINC : 0) |
SETFIELD(PHB_IODA_AD_TSEL, 0ul, table) |
SETFIELD(PHB_IODA_AD_TADR, 0ul, addr));
}
static void phb3_eeh_dump_regs(struct phb3 *p,
struct OpalIoPhb3ErrorData *regs);
/* Check if AIB is fenced via PBCQ NFIR */
static bool phb3_fenced(struct phb3 *p)
{
uint64_t nfir;
/* We still probably has crazy xscom */
xscom_read(p->chip_id, p->pe_xscom + 0x0, &nfir);
if (nfir & PPC_BIT(16)) {
p->flags |= PHB3_AIB_FENCED;
phb3_eeh_dump_regs(p, NULL);
return true;
}
return false;
}
static int64_t phb3_pcicfg_rc_pref_window(void *dev __unused,
struct pci_cfg_reg_filter *pcrf,
uint32_t offset, uint32_t len,
uint32_t *data, bool write)
{
uint8_t *pdata;
uint32_t i;
/* Cache whatever we received */
if (write) {
pdata = &pcrf->data[offset - pcrf->start];
for (i = 0; i < len; i++, pdata++)
*pdata = (uint8_t)(*data >> (8 * i));
return OPAL_SUCCESS;
}
/* Return whatever we cached */
*data = 0;
pdata = &pcrf->data[offset - pcrf->start + len - 1];
for (i = len; i > 0; i--, pdata--) {
*data = (*data) << 8;
if (offset + i == PCI_CFG_PREF_MEM_BASE) {
*data |= ((*pdata & 0xf0) | 0x1);
continue;
}
*data |= *pdata;
}
return OPAL_SUCCESS;
}
/*
* Configuration space access
*
* The PHB lock is assumed to be already held
*/
static int64_t phb3_pcicfg_check(struct phb3 *p, uint32_t bdfn,
uint32_t offset, uint32_t size,
uint8_t *pe)
{
uint32_t sm = size - 1;
if (offset > 0xfff || bdfn > 0xffff)
return OPAL_PARAMETER;
if (offset & sm)
return OPAL_PARAMETER;
/* The root bus only has a device at 0 and we get into an
* error state if we try to probe beyond that, so let's
* avoid that and just return an error to Linux
*/
if (PCI_BUS_NUM(bdfn) == 0 && (bdfn & 0xff))
return OPAL_HARDWARE;
/* Check PHB state */
if (p->broken)
return OPAL_HARDWARE;
/* Fetch the PE# from cache */
*pe = p->rte_cache[bdfn];
return OPAL_SUCCESS;
}
static void phb3_link_update(struct phb *phb, uint16_t data)
{
struct phb3 *p = phb_to_phb3(phb);
uint32_t new_spd, new_wid;
uint32_t old_spd, old_wid;
uint16_t old_data;
uint64_t lreg;
int i;
/* Read the old speed and width */
pci_cfg_read16(phb, 0, 0x5a, &old_data);
/* Decode the register values */
new_spd = data & PCICAP_EXP_LSTAT_SPEED;
new_wid = (data & PCICAP_EXP_LSTAT_WIDTH) >> 4;
old_spd = old_data & PCICAP_EXP_LSTAT_SPEED;
old_wid = (old_data & PCICAP_EXP_LSTAT_WIDTH) >> 4;
/* Apply maximums */
if (new_wid > 16)
new_wid = 16;
if (new_wid < 1)
new_wid = 1;
if (new_spd > 3)
new_spd = 3;
if (new_spd < 1)
new_spd = 1;
PHBINF(p, "Link change request: speed %d->%d, width %d->%d\n",
old_spd, new_spd, old_wid, new_wid);
/* Check if width needs to be changed */
if (old_wid != new_wid) {
PHBINF(p, "Changing width...\n");
lreg = in_be64(p->regs + PHB_PCIE_LINK_MANAGEMENT);
lreg = SETFIELD(PHB_PCIE_LM_TGT_LINK_WIDTH, lreg, new_wid);
lreg |= PHB_PCIE_LM_CHG_LINK_WIDTH;
out_be64(p->regs + PHB_PCIE_LINK_MANAGEMENT, lreg);
for (i=0; i<10;i++) {
lreg = in_be64(p->regs + PHB_PCIE_LINK_MANAGEMENT);
if (lreg & PHB_PCIE_LM_DL_WCHG_PENDING)
break;
time_wait_ms_nopoll(1);
}
if (!(lreg & PHB_PCIE_LM_DL_WCHG_PENDING))
PHBINF(p, "Timeout waiting for speed change start\n");
for (i=0; i<100;i++) {
lreg = in_be64(p->regs + PHB_PCIE_LINK_MANAGEMENT);
if (!(lreg & PHB_PCIE_LM_DL_WCHG_PENDING))
break;
time_wait_ms_nopoll(1);
}
if (lreg & PHB_PCIE_LM_DL_WCHG_PENDING)
PHBINF(p, "Timeout waiting for speed change end\n");
}
/* Check if speed needs to be changed */
if (old_spd != new_spd) {
PHBINF(p, "Changing speed...\n");
lreg = in_be64(p->regs + PHB_PCIE_LINK_MANAGEMENT);
if (lreg & PPC_BIT(19)) {
uint16_t lctl2;
PHBINF(p, " Bit19 set ! working around...\n");
pci_cfg_read16(phb, 0, 0x78, &lctl2);
PHBINF(p, " LCTL2=%04x\n", lctl2);
lctl2 &= ~PCICAP_EXP_LCTL2_HWAUTSPDIS;
pci_cfg_write16(phb, 0, 0x78, lctl2);
}
lreg = in_be64(p->regs + PHB_PCIE_LINK_MANAGEMENT);
lreg = SETFIELD(PHB_PCIE_LM_TGT_SPEED, lreg, new_spd);
lreg |= PHB_PCIE_LM_CHG_SPEED;
out_be64(p->regs + PHB_PCIE_LINK_MANAGEMENT, lreg);
}
}
static int64_t phb3_pcicfg_rc_link_speed(void *dev,
struct pci_cfg_reg_filter *pcrf __unused,
uint32_t offset, uint32_t len,
uint32_t *data, bool write)
{
struct pci_device *pd = dev;
/* Hack for link speed changes. We intercept attempts at writing
* the link control/status register
*/
if (write && len == 4 && offset == 0x58) {
phb3_link_update(pd->phb, (*data) >> 16);
return OPAL_SUCCESS;
}
if (write && len == 2 && offset == 0x5a) {
phb3_link_update(pd->phb, *(uint16_t *)data);
return OPAL_SUCCESS;
}
return OPAL_PARTIAL;
}
#define PHB3_PCI_CFG_READ(size, type) \
static int64_t phb3_pcicfg_read##size(struct phb *phb, uint32_t bdfn, \
uint32_t offset, type *data) \
{ \
struct phb3 *p = phb_to_phb3(phb); \
uint64_t addr, val64; \
int64_t rc; \
uint8_t pe; \
bool use_asb = false; \
\
/* Initialize data in case of error */ \
*data = (type)0xffffffff; \
\
rc = phb3_pcicfg_check(p, bdfn, offset, sizeof(type), &pe); \
if (rc) \
return rc; \
\
if (p->flags & PHB3_AIB_FENCED) { \
if (!(p->flags & PHB3_CFG_USE_ASB)) \
return OPAL_HARDWARE; \
use_asb = true; \
} else if ((p->flags & PHB3_CFG_BLOCKED) && bdfn != 0) { \
return OPAL_HARDWARE; \
} \
\
rc = pci_handle_cfg_filters(phb, bdfn, offset, sizeof(type), \
(uint32_t *)data, false); \
if (rc != OPAL_PARTIAL) \
return rc; \
\
addr = PHB_CA_ENABLE; \
addr = SETFIELD(PHB_CA_BDFN, addr, bdfn); \
addr = SETFIELD(PHB_CA_REG, addr, offset); \
addr = SETFIELD(PHB_CA_PE, addr, pe); \
if (use_asb) { \
phb3_write_reg_asb(p, PHB_CONFIG_ADDRESS, addr); \
sync(); \
val64 = bswap_64(phb3_read_reg_asb(p, PHB_CONFIG_DATA)); \
*data = (type)(val64 >> (8 * (offset & (4 - sizeof(type))))); \
} else { \
out_be64(p->regs + PHB_CONFIG_ADDRESS, addr); \
*data = in_le##size(p->regs + PHB_CONFIG_DATA + \
(offset & (4 - sizeof(type)))); \
} \
\
return OPAL_SUCCESS; \
}
#define PHB3_PCI_CFG_WRITE(size, type) \
static int64_t phb3_pcicfg_write##size(struct phb *phb, uint32_t bdfn, \
uint32_t offset, type data) \
{ \
struct phb3 *p = phb_to_phb3(phb); \
uint64_t addr, val64 = 0; \
int64_t rc; \
uint8_t pe; \
bool use_asb = false; \
\
rc = phb3_pcicfg_check(p, bdfn, offset, sizeof(type), &pe); \
if (rc) \
return rc; \
\
if (p->flags & PHB3_AIB_FENCED) { \
if (!(p->flags & PHB3_CFG_USE_ASB)) \
return OPAL_HARDWARE; \
use_asb = true; \
} else if ((p->flags & PHB3_CFG_BLOCKED) && bdfn != 0) { \
return OPAL_HARDWARE; \
} \
\
rc = pci_handle_cfg_filters(phb, bdfn, offset, sizeof(type), \
(uint32_t *)&data, true); \
if (rc != OPAL_PARTIAL) \
return rc; \
\
addr = PHB_CA_ENABLE; \
addr = SETFIELD(PHB_CA_BDFN, addr, bdfn); \
addr = SETFIELD(PHB_CA_REG, addr, offset); \
addr = SETFIELD(PHB_CA_PE, addr, pe); \
if (use_asb) { \
val64 = data; \
val64 = bswap_64(val64 << 8 * (offset & (4 - sizeof(type)))); \
phb3_write_reg_asb(p, PHB_CONFIG_ADDRESS, addr); \
sync(); \
phb3_write_reg_asb(p, PHB_CONFIG_DATA, val64); \
} else { \
out_be64(p->regs + PHB_CONFIG_ADDRESS, addr); \
out_le##size(p->regs + PHB_CONFIG_DATA + \
(offset & (4 - sizeof(type))), data); \
} \
\
return OPAL_SUCCESS; \
}
PHB3_PCI_CFG_READ(8, u8)
PHB3_PCI_CFG_READ(16, u16)
PHB3_PCI_CFG_READ(32, u32)
PHB3_PCI_CFG_WRITE(8, u8)
PHB3_PCI_CFG_WRITE(16, u16)
PHB3_PCI_CFG_WRITE(32, u32)
static uint8_t phb3_choose_bus(struct phb *phb __unused,
struct pci_device *bridge __unused,
uint8_t candidate, uint8_t *max_bus __unused,
bool *use_max)
{
/* Use standard bus number selection */
*use_max = false;
return candidate;
}
static int64_t phb3_get_reserved_pe_number(struct phb *phb __unused)
{
return PHB3_RESERVED_PE_NUM;
}
static inline void phb3_enable_ecrc(struct phb *phb, bool enable)
{
struct phb3 *p = phb_to_phb3(phb);
uint32_t ctl;
if (p->aercap <= 0)
return;
pci_cfg_read32(phb, 0, p->aercap + PCIECAP_AER_CAPCTL, &ctl);
if (enable) {
ctl |= (PCIECAP_AER_CAPCTL_ECRCG_EN |
PCIECAP_AER_CAPCTL_ECRCC_EN);
} else {
ctl &= ~(PCIECAP_AER_CAPCTL_ECRCG_EN |
PCIECAP_AER_CAPCTL_ECRCC_EN);
}
pci_cfg_write32(phb, 0, p->aercap + PCIECAP_AER_CAPCTL, ctl);
}
static void phb3_root_port_init(struct phb *phb, struct pci_device *dev,
int ecap, int aercap)
{
struct phb3 *p = phb_to_phb3(phb);
uint16_t bdfn = dev->bdfn;
uint16_t val16;
uint32_t val32;
/* Use PHB's callback so that the UTL events will be masked
* or unmasked when the link is down or up.
*/
if (dev->slot && dev->slot->ops.prepare_link_change &&
phb->slot && phb->slot->ops.prepare_link_change)
dev->slot->ops.prepare_link_change =
phb->slot->ops.prepare_link_change;
/* Mask UTL link down event if root slot supports surprise
* hotplug as the event should be handled by hotplug driver
* instead of EEH subsystem.
*/
if (dev->slot && dev->slot->surprise_pluggable)
out_be64(p->regs + UTL_PCIE_PORT_IRQ_EN, 0xad42800000000000UL);
/* Enable SERR and parity checking */
pci_cfg_read16(phb, bdfn, PCI_CFG_CMD, &val16);
val16 |= (PCI_CFG_CMD_SERR_EN | PCI_CFG_CMD_PERR_RESP);
pci_cfg_write16(phb, bdfn, PCI_CFG_CMD, val16);
/* Enable reporting various errors */
if (!ecap) return;
pci_cfg_read16(phb, bdfn, ecap + PCICAP_EXP_DEVCTL, &val16);
val16 |= (PCICAP_EXP_DEVCTL_CE_REPORT |
PCICAP_EXP_DEVCTL_NFE_REPORT |
PCICAP_EXP_DEVCTL_FE_REPORT |
PCICAP_EXP_DEVCTL_UR_REPORT);
pci_cfg_write16(phb, bdfn, ecap + PCICAP_EXP_DEVCTL, val16);
if (!aercap) return;
/* Mask various unrecoverable errors. The link surprise down
* event should be masked when its PCI slot support surprise
* hotplug. The link surprise down event should be handled by
* PCI hotplug driver instead of EEH subsystem.
*/
pci_cfg_read32(phb, bdfn, aercap + PCIECAP_AER_UE_MASK, &val32);
val32 |= (PCIECAP_AER_UE_MASK_POISON_TLP |
PCIECAP_AER_UE_MASK_COMPL_TIMEOUT |
PCIECAP_AER_UE_MASK_COMPL_ABORT |
PCIECAP_AER_UE_MASK_ECRC);
if (dev->slot && dev->slot->surprise_pluggable)
val32 |= PCIECAP_AER_UE_MASK_SURPRISE_DOWN;
pci_cfg_write32(phb, bdfn, aercap + PCIECAP_AER_UE_MASK, val32);
/* Report various unrecoverable errors as fatal errors */
pci_cfg_read32(phb, bdfn, aercap + PCIECAP_AER_UE_SEVERITY, &val32);
val32 |= (PCIECAP_AER_UE_SEVERITY_DLLP |
PCIECAP_AER_UE_SEVERITY_SURPRISE_DOWN |
PCIECAP_AER_UE_SEVERITY_FLOW_CTL_PROT |
PCIECAP_AER_UE_SEVERITY_UNEXP_COMPL |
PCIECAP_AER_UE_SEVERITY_RECV_OVFLOW |
PCIECAP_AER_UE_SEVERITY_MALFORMED_TLP);
pci_cfg_write32(phb, bdfn, aercap + PCIECAP_AER_UE_SEVERITY, val32);
/* Mask various recoverable errors */
pci_cfg_read32(phb, bdfn, aercap + PCIECAP_AER_CE_MASK, &val32);
val32 |= PCIECAP_AER_CE_MASK_ADV_NONFATAL;
pci_cfg_write32(phb, bdfn, aercap + PCIECAP_AER_CE_MASK, val32);
/* Enable ECRC check */
phb3_enable_ecrc(phb, true);
/* Enable all error reporting */
pci_cfg_read32(phb, bdfn, aercap + PCIECAP_AER_RERR_CMD, &val32);
val32 |= (PCIECAP_AER_RERR_CMD_FE |
PCIECAP_AER_RERR_CMD_NFE |
PCIECAP_AER_RERR_CMD_CE);
pci_cfg_write32(phb, bdfn, aercap + PCIECAP_AER_RERR_CMD, val32);
}
static void phb3_switch_port_init(struct phb *phb,
struct pci_device *dev,
int ecap, int aercap)
{
struct phb3 *p = phb_to_phb3(phb);
uint16_t bdfn = dev->bdfn;
uint16_t val16;
uint32_t val32;
/* Enable SERR and parity checking and disable INTx */
pci_cfg_read16(phb, bdfn, PCI_CFG_CMD, &val16);
val16 |= (PCI_CFG_CMD_PERR_RESP |
PCI_CFG_CMD_SERR_EN |
PCI_CFG_CMD_INTx_DIS);
pci_cfg_write16(phb, bdfn, PCI_CFG_CMD, val16);
/* Disable partity error and enable system error */
pci_cfg_read16(phb, bdfn, PCI_CFG_BRCTL, &val16);
val16 &= ~PCI_CFG_BRCTL_PERR_RESP_EN;
val16 |= PCI_CFG_BRCTL_SERR_EN;
pci_cfg_write16(phb, bdfn, PCI_CFG_BRCTL, val16);
/* Enable reporting various errors */
if (!ecap) return;
pci_cfg_read16(phb, bdfn, ecap + PCICAP_EXP_DEVCTL, &val16);
val16 |= (PCICAP_EXP_DEVCTL_CE_REPORT |
PCICAP_EXP_DEVCTL_NFE_REPORT |
PCICAP_EXP_DEVCTL_FE_REPORT);
/* HW279570 - Disable reporting of correctable errors */
val16 &= ~PCICAP_EXP_DEVCTL_CE_REPORT;
pci_cfg_write16(phb, bdfn, ecap + PCICAP_EXP_DEVCTL, val16);
/* Unmask all unrecoverable errors for upstream port. For
* downstream port, the surprise link down is masked because
* it should be handled by hotplug driver instead of EEH
* subsystem.
*/
if (!aercap) return;
if (dev->dev_type == PCIE_TYPE_SWITCH_DNPORT &&
dev->slot && dev->slot->surprise_pluggable)
pci_cfg_write32(phb, bdfn, aercap + PCIECAP_AER_UE_MASK,
PCIECAP_AER_UE_MASK_SURPRISE_DOWN);
else
pci_cfg_write32(phb, bdfn, aercap + PCIECAP_AER_UE_MASK, 0x0);
/* Severity of unrecoverable errors */
if (dev->dev_type == PCIE_TYPE_SWITCH_UPPORT)
val32 = (PCIECAP_AER_UE_SEVERITY_DLLP |
PCIECAP_AER_UE_SEVERITY_SURPRISE_DOWN |
PCIECAP_AER_UE_SEVERITY_FLOW_CTL_PROT |
PCIECAP_AER_UE_SEVERITY_RECV_OVFLOW |
PCIECAP_AER_UE_SEVERITY_MALFORMED_TLP |
PCIECAP_AER_UE_SEVERITY_INTERNAL);
else
val32 = (PCIECAP_AER_UE_SEVERITY_FLOW_CTL_PROT |
PCIECAP_AER_UE_SEVERITY_INTERNAL);
pci_cfg_write32(phb, bdfn, aercap + PCIECAP_AER_UE_SEVERITY, val32);
/*
* Mask various correctable errors
*
* On Murano and Venice DD1.0 we disable emission of corrected
* error messages to the PHB completely to workaround errata
* HW257476 causing the loss of tags.
*/
if (p->rev < PHB3_REV_MURANO_DD20)
val32 = 0xffffffff;
else
val32 = PCIECAP_AER_CE_MASK_ADV_NONFATAL;
pci_cfg_write32(phb, bdfn, aercap + PCIECAP_AER_CE_MASK, val32);
/* Enable ECRC generation and disable ECRC check */
pci_cfg_read32(phb, bdfn, aercap + PCIECAP_AER_CAPCTL, &val32);
val32 |= PCIECAP_AER_CAPCTL_ECRCG_EN;
val32 &= ~PCIECAP_AER_CAPCTL_ECRCC_EN;
pci_cfg_write32(phb, bdfn, aercap + PCIECAP_AER_CAPCTL, val32);
}
static void phb3_endpoint_init(struct phb *phb,
struct pci_device *dev,
int ecap, int aercap)
{
struct phb3 *p = phb_to_phb3(phb);
uint16_t bdfn = dev->bdfn;
uint16_t val16;
uint32_t val32;
/* Enable SERR and parity checking */
pci_cfg_read16(phb, bdfn, PCI_CFG_CMD, &val16);
val16 |= (PCI_CFG_CMD_PERR_RESP |
PCI_CFG_CMD_SERR_EN);
pci_cfg_write16(phb, bdfn, PCI_CFG_CMD, val16);
/* Enable reporting various errors */
if (!ecap) return;
pci_cfg_read16(phb, bdfn, ecap + PCICAP_EXP_DEVCTL, &val16);
val16 &= ~PCICAP_EXP_DEVCTL_CE_REPORT;
val16 |= (PCICAP_EXP_DEVCTL_NFE_REPORT |
PCICAP_EXP_DEVCTL_FE_REPORT |
PCICAP_EXP_DEVCTL_UR_REPORT);
/* HW279570 - Disable reporting of correctable errors */
val16 &= ~PCICAP_EXP_DEVCTL_CE_REPORT;
pci_cfg_write16(phb, bdfn, ecap + PCICAP_EXP_DEVCTL, val16);
/*
* On Murano and Venice DD1.0 we disable emission of corrected
* error messages to the PHB completely to workaround errata
* HW257476 causing the loss of tags.
*/
if (p->rev < PHB3_REV_MURANO_DD20)
pci_cfg_write32(phb, bdfn, aercap + PCIECAP_AER_CE_MASK,
0xffffffff);
/* Enable ECRC generation and check */
pci_cfg_read32(phb, bdfn, aercap + PCIECAP_AER_CAPCTL, &val32);
val32 |= (PCIECAP_AER_CAPCTL_ECRCG_EN |
PCIECAP_AER_CAPCTL_ECRCC_EN);
pci_cfg_write32(phb, bdfn, aercap + PCIECAP_AER_CAPCTL, val32);
}
static int64_t phb3_pcicfg_no_dstate(void *dev __unused,
struct pci_cfg_reg_filter *pcrf,
uint32_t offset, uint32_t len __unused,
uint32_t *data __unused, bool write)
{
uint32_t loff = offset - pcrf->start;
/* Disable D-state change on children of the PHB. For now we
* simply block all writes to the PM control/status
*/
if (write && loff >= 4 && loff < 6)
return OPAL_SUCCESS;
return OPAL_PARTIAL;
}
static void phb3_check_device_quirks(struct phb *phb, struct pci_device *dev)
{
struct phb3 *p = phb_to_phb3(phb);
if (dev->primary_bus != 0 &&
dev->primary_bus != 1)
return;
if (dev->primary_bus == 1) {
u64 modectl;
/*
* For these adapters, if they are directly under the PHB, we
* adjust the disable_wr_scope_group bit for performances
*
* 15b3:1003 Mellanox Travis3-EN (CX3)
* 15b3:1011 Mellanox HydePark (ConnectIB)
* 15b3:1013 Mellanox GlacierPark (CX4)
*/
xscom_read(p->chip_id, p->pe_xscom + 0x0b, &modectl);
if (PCI_VENDOR_ID(dev->vdid) == 0x15b3 &&
(PCI_DEVICE_ID(dev->vdid) == 0x1003 ||
PCI_DEVICE_ID(dev->vdid) == 0x1011 ||
PCI_DEVICE_ID(dev->vdid) == 0x1013))
modectl |= PPC_BIT(14);
else
modectl &= ~PPC_BIT(14);
xscom_write(p->chip_id, p->pe_xscom + 0x0b, modectl);
/*
* Naples has a problem with D-states at least on Mellanox CX4,
* disable changing D-state on Naples like we do it for PHB4.
*/
if (PHB3_IS_NAPLES(p) &&
pci_has_cap(dev, PCI_CFG_CAP_ID_PM, false)) {
pci_add_cfg_reg_filter(dev,
pci_cap(dev, PCI_CFG_CAP_ID_PM, false),
8,
PCI_REG_FLAG_WRITE,
phb3_pcicfg_no_dstate);
}
} else if (dev->primary_bus == 0) {
/*
* Emulate the prefetchable window of the root port
* when the corresponding HW registers are readonly.
*
* 1014:03dc Root port on P8/P8E/P8NVL
*/
if (PCI_VENDOR_ID(dev->vdid) == 0x1014 &&
PCI_DEVICE_ID(dev->vdid) == 0x03dc) {
uint32_t pref_hi, tmp;
pci_cfg_read32(phb, dev->bdfn,
PCI_CFG_PREF_MEM_BASE_U32, &pref_hi);
pci_cfg_write32(phb, dev->bdfn,
PCI_CFG_PREF_MEM_BASE_U32, ~pref_hi);
pci_cfg_read32(phb, dev->bdfn,
PCI_CFG_PREF_MEM_BASE_U32, &tmp);
pci_cfg_write32(phb, dev->bdfn,
PCI_CFG_PREF_MEM_BASE_U32, pref_hi);
if (tmp == pref_hi)
pci_add_cfg_reg_filter(dev,
PCI_CFG_PREF_MEM_BASE_U32, 12,
PCI_REG_FLAG_READ | PCI_REG_FLAG_WRITE,
phb3_pcicfg_rc_pref_window);
/* Add filter to control link speed */
pci_add_cfg_reg_filter(dev,
0x58, 4,
PCI_REG_FLAG_WRITE,
phb3_pcicfg_rc_link_speed);
}
}
}
static inline int phb3_should_disable_ecrc(struct pci_device *pd)
{
/*
* When we have PMC PCIe switch, we need disable ECRC on root port.
* Otherwise, the adapters behind the switch downstream ports might
* not probed successfully.
*/
if (pd->vdid == 0x854611f8)
return true;
return false;
}
static int phb3_device_init(struct phb *phb,
struct pci_device *dev,
void *data)
{
struct phb3 *p = phb_to_phb3(phb);
int ecap, aercap;
/* Some special adapter tweaks for devices directly under the PHB */
phb3_check_device_quirks(phb, dev);
/* Common initialization for the device */
pci_device_init(phb, dev);
ecap = pci_cap(dev, PCI_CFG_CAP_ID_EXP, false);
aercap = pci_cap(dev, PCIECAP_ID_AER, true);
if (dev->dev_type == PCIE_TYPE_ROOT_PORT)
phb3_root_port_init(phb, dev, ecap, aercap);
else if (dev->dev_type == PCIE_TYPE_SWITCH_UPPORT ||
dev->dev_type == PCIE_TYPE_SWITCH_DNPORT)
phb3_switch_port_init(phb, dev, ecap, aercap);
else
phb3_endpoint_init(phb, dev, ecap, aercap);
/*
* Check if we need disable ECRC functionality on root port. It
* only happens when PCI topology changes, meaning it's skipped
* when reinitializing PCI device after EEH reset.
*/
if (!data && phb3_should_disable_ecrc(dev)) {
if (p->no_ecrc_devs++ == 0)
phb3_enable_ecrc(phb, false);
}
return 0;
}
static void phb3_device_remove(struct phb *phb, struct pci_device *pd)
{
struct phb3 *p = phb_to_phb3(phb);
if (!phb3_should_disable_ecrc(pd) || p->no_ecrc_devs == 0)
return;
if (--p->no_ecrc_devs == 0)
phb3_enable_ecrc(phb, true);
}
static int64_t phb3_pci_reinit(struct phb *phb, uint64_t scope, uint64_t data)
{
struct pci_device *pd;
uint16_t bdfn = data;
int ret;
if (scope != OPAL_REINIT_PCI_DEV)
return OPAL_PARAMETER;
pd = pci_find_dev(phb, bdfn);
if (!pd)
return OPAL_PARAMETER;
ret = phb3_device_init(phb, pd, pd);
if (ret)
return OPAL_HARDWARE;
return OPAL_SUCCESS;
}
/* Clear IODA cache tables */
static void phb3_init_ioda_cache(struct phb3 *p)
{
uint32_t i;
uint64_t *data64;
/*
* RTT and PELTV. RTE should be 0xFF's to indicate
* invalid PE# for the corresponding RID.
*
* Note: Instead we set all RTE entries to 0x00 to
* work around a problem where PE lookups might be
* done before Linux has established valid PE's
* (during PCI probing). We can revisit that once/if
* Linux has been fixed to always setup valid PEs.
*
* The value 0x00 corresponds to the default PE# Linux
* uses to check for config space freezes before it
* has assigned PE# to busses.
*
* WARNING: Additionally, we need to be careful, there's
* a HW issue, if we get an MSI on an RTT entry that is
* FF, things will go bad. We need to ensure we don't
* ever let a live FF RTT even temporarily when resetting
* for EEH etc... (HW278969).
*/
for (i = 0; i < ARRAY_SIZE(p->rte_cache); i++)
p->rte_cache[i] = PHB3_RESERVED_PE_NUM;
memset(p->peltv_cache, 0x0, sizeof(p->peltv_cache));
/* Disable all LSI */
for (i = 0; i < ARRAY_SIZE(p->lxive_cache); i++) {
data64 = &p->lxive_cache[i];
*data64 = SETFIELD(IODA2_LXIVT_PRIORITY, 0ul, 0xff);
*data64 = SETFIELD(IODA2_LXIVT_SERVER, *data64, 0x0);
}
/* Diable all MSI */
for (i = 0; i < ARRAY_SIZE(p->ive_cache); i++) {
data64 = &p->ive_cache[i];
*data64 = SETFIELD(IODA2_IVT_PRIORITY, 0ul, 0xff);
*data64 = SETFIELD(IODA2_IVT_SERVER, *data64, 0x0);
}
/* Clear TVT */
memset(p->tve_cache, 0x0, sizeof(p->tve_cache));
/* Clear M32 domain */
memset(p->m32d_cache, 0x0, sizeof(p->m32d_cache));
/* Clear M64 domain */
memset(p->m64b_cache, 0x0, sizeof(p->m64b_cache));
}
/* phb3_ioda_reset - Reset the IODA tables
*
* @purge: If true, the cache is cleared and the cleared values
* are applied to HW. If false, the cached values are
* applied to HW
*
* This reset the IODA tables in the PHB. It is called at
* initialization time, on PHB reset, and can be called
* explicitly from OPAL
*/
static int64_t phb3_ioda_reset(struct phb *phb, bool purge)
{
struct phb3 *p = phb_to_phb3(phb);
uint64_t server, prio;
uint64_t *pdata64, data64;
uint32_t i;
if (purge) {
prlog(PR_DEBUG, "PHB%x: Purging all IODA tables...\n",
p->phb.opal_id);
phb3_init_ioda_cache(p);
}
/* Init_27..28 - LIXVT */
phb3_ioda_sel(p, IODA2_TBL_LXIVT, 0, true);
for (i = 0; i < ARRAY_SIZE(p->lxive_cache); i++) {
data64 = p->lxive_cache[i];
server = GETFIELD(IODA2_LXIVT_SERVER, data64);
prio = GETFIELD(IODA2_LXIVT_PRIORITY, data64);
data64 = SETFIELD(IODA2_LXIVT_SERVER, data64, server);
data64 = SETFIELD(IODA2_LXIVT_PRIORITY, data64, prio);
out_be64(p->regs + PHB_IODA_DATA0, data64);
}
/* Init_29..30 - MRT */
phb3_ioda_sel(p, IODA2_TBL_MRT, 0, true);
for (i = 0; i < 8; i++)
out_be64(p->regs + PHB_IODA_DATA0, 0);
/* Init_31..32 - TVT */
phb3_ioda_sel(p, IODA2_TBL_TVT, 0, true);
for (i = 0; i < ARRAY_SIZE(p->tve_cache); i++)
out_be64(p->regs + PHB_IODA_DATA0, p->tve_cache[i]);
/* Init_33..34 - M64BT */
phb3_ioda_sel(p, IODA2_TBL_M64BT, 0, true);
for (i = 0; i < ARRAY_SIZE(p->m64b_cache); i++)
out_be64(p->regs + PHB_IODA_DATA0, p->m64b_cache[i]);
/* Init_35..36 - M32DT */
phb3_ioda_sel(p, IODA2_TBL_M32DT, 0, true);
for (i = 0; i < ARRAY_SIZE(p->m32d_cache); i++)
out_be64(p->regs + PHB_IODA_DATA0, p->m32d_cache[i]);
/* Load RTE, PELTV */
if (p->tbl_rtt)
memcpy((void *)p->tbl_rtt, p->rte_cache, RTT_TABLE_SIZE);
if (p->tbl_peltv)
memcpy((void *)p->tbl_peltv, p->peltv_cache, PELTV_TABLE_SIZE);
/* Load IVT */
if (p->tbl_ivt) {
pdata64 = (uint64_t *)p->tbl_ivt;
for (i = 0; i < IVT_TABLE_ENTRIES; i++)
pdata64[i * IVT_TABLE_STRIDE] = p->ive_cache[i];
}
/* Invalidate RTE, IVE, TCE cache */
out_be64(p->regs + PHB_RTC_INVALIDATE, PHB_RTC_INVALIDATE_ALL);
out_be64(p->regs + PHB_IVC_INVALIDATE, PHB_IVC_INVALIDATE_ALL);
out_be64(p->regs + PHB_TCE_KILL, PHB_TCE_KILL_ALL);
/* Clear RBA */
if (p->rev >= PHB3_REV_MURANO_DD20) {
phb3_ioda_sel(p, IODA2_TBL_RBA, 0, true);
for (i = 0; i < 32; i++)
out_be64(p->regs + PHB_IODA_DATA0, 0x0ul);
}
/* Clear PEST & PEEV */
for (i = 0; i < PHB3_MAX_PE_NUM; i++) {
uint64_t pesta, pestb;
phb3_ioda_sel(p, IODA2_TBL_PESTA, i, false);
pesta = in_be64(p->regs + PHB_IODA_DATA0);
out_be64(p->regs + PHB_IODA_DATA0, 0);
phb3_ioda_sel(p, IODA2_TBL_PESTB, i, false);
pestb = in_be64(p->regs + PHB_IODA_DATA0);
out_be64(p->regs + PHB_IODA_DATA0, 0);
if ((pesta & IODA2_PESTA_MMIO_FROZEN) ||
(pestb & IODA2_PESTB_DMA_STOPPED))
PHBDBG(p, "Frozen PE#%x (%s - %s)\n",
i, (pesta & IODA2_PESTA_MMIO_FROZEN) ? "DMA" : "",
(pestb & IODA2_PESTB_DMA_STOPPED) ? "MMIO" : "");
}
phb3_ioda_sel(p, IODA2_TBL_PEEV, 0, true);
for (i = 0; i < 4; i++)
out_be64(p->regs + PHB_IODA_DATA0, 0);
return OPAL_SUCCESS;
}
/*
* Clear anything we have in PAPR Error Injection registers. Though
* the spec says the PAPR error injection should be one-shot without
* the "sticky" bit. However, that's false according to the experiments
* I had. So we have to clear it at appropriate point in kernel to
* avoid endless frozen PE.
*/
static int64_t phb3_papr_errinjct_reset(struct phb *phb)
{
struct phb3 *p = phb_to_phb3(phb);
out_be64(p->regs + PHB_PAPR_ERR_INJ_CTL, 0x0ul);
out_be64(p->regs + PHB_PAPR_ERR_INJ_ADDR, 0x0ul);
out_be64(p->regs + PHB_PAPR_ERR_INJ_MASK, 0x0ul);
return OPAL_SUCCESS;
}
static int64_t phb3_set_phb_mem_window(struct phb *phb,
uint16_t window_type,
uint16_t window_num,
uint64_t addr,
uint64_t __unused pci_addr,
uint64_t size)
{
struct phb3 *p = phb_to_phb3(phb);
uint64_t data64;
/*
* By design, PHB3 doesn't support IODT any more.
* Besides, we can't enable M32 BAR as well. So
* the function is used to do M64 mapping and each
* BAR is supposed to be shared by all PEs.
*/
switch (window_type) {
case OPAL_IO_WINDOW_TYPE:
case OPAL_M32_WINDOW_TYPE:
return OPAL_UNSUPPORTED;
case OPAL_M64_WINDOW_TYPE:
if (window_num >= 16)
return OPAL_PARAMETER;
data64 = p->m64b_cache[window_num];
if (data64 & IODA2_M64BT_SINGLE_PE) {
if ((addr & 0x1FFFFFFul) ||
(size & 0x1FFFFFFul))
return OPAL_PARAMETER;
} else {
if ((addr & 0xFFFFFul) ||
(size & 0xFFFFFul))
return OPAL_PARAMETER;
}
/* size should be 2^N */
if (!size || size & (size-1))
return OPAL_PARAMETER;
/* address should be size aligned */
if (addr & (size - 1))
return OPAL_PARAMETER;
break;
default:
return OPAL_PARAMETER;
}
if (data64 & IODA2_M64BT_SINGLE_PE) {
data64 = SETFIELD(IODA2_M64BT_SINGLE_BASE, data64,
addr >> 25);
data64 = SETFIELD(IODA2_M64BT_SINGLE_MASK, data64,
0x20000000 - (size >> 25));
} else {
data64 = SETFIELD(IODA2_M64BT_BASE, data64,
addr >> 20);
data64 = SETFIELD(IODA2_M64BT_MASK, data64,
0x40000000 - (size >> 20));
}
p->m64b_cache[window_num] = data64;
return OPAL_SUCCESS;
}
/*
* For one specific M64 BAR, it can be shared by all PEs,
* or owned by single PE exclusively.
*/
static int64_t phb3_phb_mmio_enable(struct phb *phb,
uint16_t window_type,
uint16_t window_num,
uint16_t enable)
{
struct phb3 *p = phb_to_phb3(phb);
uint64_t data64, base, mask;
/*
* By design, PHB3 doesn't support IODT any more.
* Besides, we can't enable M32 BAR as well. So
* the function is used to do M64 mapping and each
* BAR is supposed to be shared by all PEs.
*/
switch (window_type) {
case OPAL_IO_WINDOW_TYPE: