2 * QEMU PowerPC PowerNV (POWER9) PHB4 model
4 * Copyright (c) 2018-2020, IBM Corporation.
6 * This code is licensed under the GPL version 2 or later. See the
7 * COPYING file in the top-level directory.
9 #include "qemu/osdep.h"
11 #include "qapi/visitor.h"
12 #include "qapi/error.h"
13 #include "target/ppc/cpu.h"
14 #include "hw/pci-host/pnv_phb4_regs.h"
15 #include "hw/pci-host/pnv_phb4.h"
16 #include "hw/pci/pcie_host.h"
17 #include "hw/pci/pcie_port.h"
18 #include "hw/ppc/pnv.h"
19 #include "hw/ppc/pnv_xscom.h"
21 #include "hw/qdev-properties.h"
22 #include "qom/object.h"
25 #define phb_error(phb, fmt, ...) \
26 qemu_log_mask(LOG_GUEST_ERROR, "phb4[%d:%d]: " fmt "\n", \
27 (phb)->chip_id, (phb)->phb_id, ## __VA_ARGS__)
29 #define phb_pec_error(pec, fmt, ...) \
30 qemu_log_mask(LOG_GUEST_ERROR, "phb4_pec[%d:%d]: " fmt "\n", \
31 (pec)->chip_id, (pec)->index, ## __VA_ARGS__)
33 static PCIDevice
*pnv_phb4_find_cfg_dev(PnvPHB4
*phb
)
35 PCIHostState
*pci
= PCI_HOST_BRIDGE(phb
->phb_base
);
36 uint64_t addr
= phb
->regs
[PHB_CONFIG_ADDRESS
>> 3];
42 bus
= (addr
>> 52) & 0xff;
43 devfn
= (addr
>> 44) & 0xff;
45 /* We don't access the root complex this way */
46 if (bus
== 0 && devfn
== 0) {
49 return pci_find_device(pci
->bus
, bus
, devfn
);
53 * The CONFIG_DATA register expects little endian accesses, but as the
54 * region is big endian, we have to swap the value.
56 static void pnv_phb4_config_write(PnvPHB4
*phb
, unsigned off
,
57 unsigned size
, uint64_t val
)
59 uint32_t cfg_addr
, limit
;
62 pdev
= pnv_phb4_find_cfg_dev(phb
);
66 cfg_addr
= (phb
->regs
[PHB_CONFIG_ADDRESS
>> 3] >> 32) & 0xffc;
68 limit
= pci_config_size(pdev
);
69 if (limit
<= cfg_addr
) {
71 * conventional pci device can be behind pcie-to-pci bridge.
72 * 256 <= addr < 4K has no effects.
86 g_assert_not_reached();
88 pci_host_config_write_common(pdev
, cfg_addr
, limit
, val
, size
);
91 static uint64_t pnv_phb4_config_read(PnvPHB4
*phb
, unsigned off
,
94 uint32_t cfg_addr
, limit
;
98 pdev
= pnv_phb4_find_cfg_dev(phb
);
102 cfg_addr
= (phb
->regs
[PHB_CONFIG_ADDRESS
>> 3] >> 32) & 0xffc;
104 limit
= pci_config_size(pdev
);
105 if (limit
<= cfg_addr
) {
107 * conventional pci device can be behind pcie-to-pci bridge.
108 * 256 <= addr < 4K has no effects.
112 val
= pci_host_config_read_common(pdev
, cfg_addr
, limit
, size
);
121 g_assert_not_reached();
126 * Root complex register accesses are memory mapped.
128 static void pnv_phb4_rc_config_write(PnvPHB4
*phb
, unsigned off
,
129 unsigned size
, uint64_t val
)
131 PCIHostState
*pci
= PCI_HOST_BRIDGE(phb
->phb_base
);
135 phb_error(phb
, "rc_config_write invalid size %d", size
);
139 pdev
= pci_find_device(pci
->bus
, 0, 0);
141 phb_error(phb
, "rc_config_write device not found");
145 pci_host_config_write_common(pdev
, off
, PHB_RC_CONFIG_SIZE
,
149 static uint64_t pnv_phb4_rc_config_read(PnvPHB4
*phb
, unsigned off
,
152 PCIHostState
*pci
= PCI_HOST_BRIDGE(phb
->phb_base
);
157 phb_error(phb
, "rc_config_read invalid size %d", size
);
161 pdev
= pci_find_device(pci
->bus
, 0, 0);
163 phb_error(phb
, "rc_config_read device not found");
167 val
= pci_host_config_read_common(pdev
, off
, PHB_RC_CONFIG_SIZE
, 4);
171 static void pnv_phb4_check_mbt(PnvPHB4
*phb
, uint32_t index
)
173 uint64_t base
, start
, size
, mbe0
, mbe1
;
174 MemoryRegion
*parent
;
178 if (memory_region_is_mapped(&phb
->mr_mmio
[index
])) {
179 /* Should we destroy it in RCU friendly way... ? */
180 memory_region_del_subregion(phb
->mr_mmio
[index
].container
,
181 &phb
->mr_mmio
[index
]);
184 /* Get table entry */
185 mbe0
= phb
->ioda_MBT
[(index
<< 1)];
186 mbe1
= phb
->ioda_MBT
[(index
<< 1) + 1];
188 if (!(mbe0
& IODA3_MBT0_ENABLE
)) {
192 /* Grab geometry from registers */
193 base
= GETFIELD(IODA3_MBT0_BASE_ADDR
, mbe0
) << 12;
194 size
= GETFIELD(IODA3_MBT1_MASK
, mbe1
) << 12;
195 size
|= 0xff00000000000000ull
;
198 /* Calculate PCI side start address based on M32/M64 window type */
199 if (mbe0
& IODA3_MBT0_TYPE_M32
) {
200 start
= phb
->regs
[PHB_M32_START_ADDR
>> 3];
201 if ((start
+ size
) > 0x100000000ull
) {
202 phb_error(phb
, "M32 set beyond 4GB boundary !");
203 size
= 0x100000000 - start
;
206 start
= base
| (phb
->regs
[PHB_M64_UPPER_BITS
>> 3]);
209 /* TODO: Figure out how to implement/decode AOMASK */
211 /* Check if it matches an enabled MMIO region in the PEC stack */
212 if (memory_region_is_mapped(&phb
->mmbar0
) &&
213 base
>= phb
->mmio0_base
&&
214 (base
+ size
) <= (phb
->mmio0_base
+ phb
->mmio0_size
)) {
215 parent
= &phb
->mmbar0
;
216 base
-= phb
->mmio0_base
;
217 } else if (memory_region_is_mapped(&phb
->mmbar1
) &&
218 base
>= phb
->mmio1_base
&&
219 (base
+ size
) <= (phb
->mmio1_base
+ phb
->mmio1_size
)) {
220 parent
= &phb
->mmbar1
;
221 base
-= phb
->mmio1_base
;
223 phb_error(phb
, "PHB MBAR %d out of parent bounds", index
);
227 /* Create alias (better name ?) */
228 snprintf(name
, sizeof(name
), "phb4-mbar%d", index
);
229 memory_region_init_alias(&phb
->mr_mmio
[index
], OBJECT(phb
), name
,
230 &phb
->pci_mmio
, start
, size
);
231 memory_region_add_subregion(parent
, base
, &phb
->mr_mmio
[index
]);
234 static void pnv_phb4_check_all_mbt(PnvPHB4
*phb
)
237 uint32_t num_windows
= phb
->big_phb
? PNV_PHB4_MAX_MMIO_WINDOWS
:
238 PNV_PHB4_MIN_MMIO_WINDOWS
;
240 for (i
= 0; i
< num_windows
; i
++) {
241 pnv_phb4_check_mbt(phb
, i
);
245 static uint64_t *pnv_phb4_ioda_access(PnvPHB4
*phb
,
246 unsigned *out_table
, unsigned *out_idx
)
248 uint64_t adreg
= phb
->regs
[PHB_IODA_ADDR
>> 3];
249 unsigned int index
= GETFIELD(PHB_IODA_AD_TADR
, adreg
);
250 unsigned int table
= GETFIELD(PHB_IODA_AD_TSEL
, adreg
);
252 uint64_t *tptr
= NULL
;
256 tptr
= phb
->ioda_LIST
;
260 tptr
= phb
->ioda_MIST
;
261 mask
= phb
->big_phb
? PNV_PHB4_MAX_MIST
: (PNV_PHB4_MAX_MIST
>> 1);
265 mask
= phb
->big_phb
? 127 : 63;
268 mask
= phb
->big_phb
? 15 : 7;
270 case IODA3_TBL_PESTA
:
271 case IODA3_TBL_PESTB
:
272 mask
= phb
->big_phb
? PNV_PHB4_MAX_PEs
: (PNV_PHB4_MAX_PEs
>> 1);
276 tptr
= phb
->ioda_TVT
;
277 mask
= phb
->big_phb
? PNV_PHB4_MAX_TVEs
: (PNV_PHB4_MAX_TVEs
>> 1);
282 mask
= phb
->big_phb
? 1023 : 511;
285 tptr
= phb
->ioda_MBT
;
286 mask
= phb
->big_phb
? PNV_PHB4_MAX_MBEs
: (PNV_PHB4_MAX_MBEs
>> 1);
290 tptr
= phb
->ioda_MDT
;
291 mask
= phb
->big_phb
? PNV_PHB4_MAX_PEs
: (PNV_PHB4_MAX_PEs
>> 1);
295 tptr
= phb
->ioda_PEEV
;
296 mask
= phb
->big_phb
? PNV_PHB4_MAX_PEEVs
: (PNV_PHB4_MAX_PEEVs
>> 1);
300 phb_error(phb
, "invalid IODA table %d", table
);
313 if (adreg
& PHB_IODA_AD_AUTOINC
) {
314 index
= (index
+ 1) & mask
;
315 adreg
= SETFIELD(PHB_IODA_AD_TADR
, adreg
, index
);
318 phb
->regs
[PHB_IODA_ADDR
>> 3] = adreg
;
322 static uint64_t pnv_phb4_ioda_read(PnvPHB4
*phb
)
327 tptr
= pnv_phb4_ioda_access(phb
, &table
, &idx
);
329 /* Special PESTA case */
330 if (table
== IODA3_TBL_PESTA
) {
331 return ((uint64_t)(phb
->ioda_PEST_AB
[idx
] & 1)) << 63;
332 } else if (table
== IODA3_TBL_PESTB
) {
333 return ((uint64_t)(phb
->ioda_PEST_AB
[idx
] & 2)) << 62;
335 /* Return 0 on unsupported tables, not ff's */
341 static void pnv_phb4_ioda_write(PnvPHB4
*phb
, uint64_t val
)
346 tptr
= pnv_phb4_ioda_access(phb
, &table
, &idx
);
348 /* Special PESTA case */
349 if (table
== IODA3_TBL_PESTA
) {
350 phb
->ioda_PEST_AB
[idx
] &= ~1;
351 phb
->ioda_PEST_AB
[idx
] |= (val
>> 63) & 1;
352 } else if (table
== IODA3_TBL_PESTB
) {
353 phb
->ioda_PEST_AB
[idx
] &= ~2;
354 phb
->ioda_PEST_AB
[idx
] |= (val
>> 62) & 2;
359 /* Handle side effects */
363 case IODA3_TBL_MIST
: {
364 /* Special mask for MIST partial write */
365 uint64_t adreg
= phb
->regs
[PHB_IODA_ADDR
>> 3];
366 uint32_t mmask
= GETFIELD(PHB_IODA_AD_MIST_PWV
, adreg
);
372 v
&= 0x0000ffffffffffffull
;
373 v
|= 0xcfff000000000000ull
& val
;
376 v
&= 0xffff0000ffffffffull
;
377 v
|= 0x0000cfff00000000ull
& val
;
380 v
&= 0xffffffff0000ffffull
;
381 v
|= 0x00000000cfff0000ull
& val
;
384 v
&= 0xffffffffffff0000ull
;
385 v
|= 0x000000000000cfffull
& val
;
393 /* Copy across the valid bit to the other half */
394 phb
->ioda_MBT
[idx
^ 1] &= 0x7fffffffffffffffull
;
395 phb
->ioda_MBT
[idx
^ 1] |= 0x8000000000000000ull
& val
;
397 /* Update mappings */
398 pnv_phb4_check_mbt(phb
, idx
>> 1);
405 static void pnv_phb4_rtc_invalidate(PnvPHB4
*phb
, uint64_t val
)
409 /* Always invalidate all for now ... */
410 QLIST_FOREACH(ds
, &phb
->dma_spaces
, list
) {
411 ds
->pe_num
= PHB_INVALID_PE
;
415 static void pnv_phb4_update_msi_regions(PnvPhb4DMASpace
*ds
)
417 uint64_t cfg
= ds
->phb
->regs
[PHB_PHB4_CONFIG
>> 3];
419 if (cfg
& PHB_PHB4C_32BIT_MSI_EN
) {
420 if (!memory_region_is_mapped(MEMORY_REGION(&ds
->msi32_mr
))) {
421 memory_region_add_subregion(MEMORY_REGION(&ds
->dma_mr
),
422 0xffff0000, &ds
->msi32_mr
);
425 if (memory_region_is_mapped(MEMORY_REGION(&ds
->msi32_mr
))) {
426 memory_region_del_subregion(MEMORY_REGION(&ds
->dma_mr
),
431 if (cfg
& PHB_PHB4C_64BIT_MSI_EN
) {
432 if (!memory_region_is_mapped(MEMORY_REGION(&ds
->msi64_mr
))) {
433 memory_region_add_subregion(MEMORY_REGION(&ds
->dma_mr
),
434 (1ull << 60), &ds
->msi64_mr
);
437 if (memory_region_is_mapped(MEMORY_REGION(&ds
->msi64_mr
))) {
438 memory_region_del_subregion(MEMORY_REGION(&ds
->dma_mr
),
444 static void pnv_phb4_update_all_msi_regions(PnvPHB4
*phb
)
448 QLIST_FOREACH(ds
, &phb
->dma_spaces
, list
) {
449 pnv_phb4_update_msi_regions(ds
);
453 static void pnv_phb4_update_xsrc(PnvPHB4
*phb
)
455 int shift
, flags
, i
, lsi_base
;
456 XiveSource
*xsrc
= &phb
->xsrc
;
458 /* The XIVE source characteristics can be set at run time */
459 if (phb
->regs
[PHB_CTRLR
>> 3] & PHB_CTRLR_IRQ_PGSZ_64K
) {
460 shift
= XIVE_ESB_64K
;
464 if (phb
->regs
[PHB_CTRLR
>> 3] & PHB_CTRLR_IRQ_STORE_EOI
) {
465 flags
= XIVE_SRC_STORE_EOI
;
471 * When the PQ disable configuration bit is set, the check on the
472 * PQ state bits is disabled on the PHB side (for MSI only) and it
473 * is performed on the IC side instead.
475 if (phb
->regs
[PHB_CTRLR
>> 3] & PHB_CTRLR_IRQ_PQ_DISABLE
) {
476 flags
|= XIVE_SRC_PQ_DISABLE
;
479 phb
->xsrc
.esb_shift
= shift
;
480 phb
->xsrc
.esb_flags
= flags
;
482 lsi_base
= GETFIELD(PHB_LSI_SRC_ID
, phb
->regs
[PHB_LSI_SOURCE_ID
>> 3]);
485 /* TODO: handle reset values of PHB_LSI_SRC_ID */
490 /* TODO: need a xive_source_irq_reset_lsi() */
491 bitmap_zero(xsrc
->lsi_map
, xsrc
->nr_irqs
);
493 for (i
= 0; i
< xsrc
->nr_irqs
; i
++) {
494 bool msi
= (i
< lsi_base
|| i
>= (lsi_base
+ 8));
496 xive_source_irq_set_lsi(xsrc
, i
);
501 static void pnv_phb4_reg_write(void *opaque
, hwaddr off
, uint64_t val
,
504 PnvPHB4
*phb
= PNV_PHB4(opaque
);
507 /* Special case outbound configuration data */
508 if ((off
& 0xfffc) == PHB_CONFIG_DATA
) {
509 pnv_phb4_config_write(phb
, off
& 0x3, size
, val
);
513 /* Special case RC configuration space */
514 if ((off
& 0xf800) == PHB_RC_CONFIG_BASE
) {
515 pnv_phb4_rc_config_write(phb
, off
& 0x7ff, size
, val
);
519 /* Other registers are 64-bit only */
520 if (size
!= 8 || off
& 0x7) {
521 phb_error(phb
, "Invalid register access, offset: 0x%"PRIx64
" size: %d",
528 case PHB_LSI_SOURCE_ID
:
529 val
&= PHB_LSI_SRC_ID
;
531 case PHB_M64_UPPER_BITS
:
532 val
&= 0xff00000000000000ull
;
536 /* Clear top 3 bits which HW does to indicate successful queuing */
537 val
&= ~(PHB_TCE_KILL_ALL
| PHB_TCE_KILL_PE
| PHB_TCE_KILL_ONE
);
541 * This is enough logic to make SW happy but we aren't
542 * actually quiescing the DMAs
544 if (val
& PHB_Q_DMA_R_AUTORESET
) {
547 val
&= PHB_Q_DMA_R_QUIESCE_DMA
;
551 case PHB_LEM_FIR_AND_MASK
:
552 phb
->regs
[PHB_LEM_FIR_ACCUM
>> 3] &= val
;
554 case PHB_LEM_FIR_OR_MASK
:
555 phb
->regs
[PHB_LEM_FIR_ACCUM
>> 3] |= val
;
557 case PHB_LEM_ERROR_AND_MASK
:
558 phb
->regs
[PHB_LEM_ERROR_MASK
>> 3] &= val
;
560 case PHB_LEM_ERROR_OR_MASK
:
561 phb
->regs
[PHB_LEM_ERROR_MASK
>> 3] |= val
;
566 /* TODO: More regs ..., maybe create a table with masks... */
568 /* Read only registers */
569 case PHB_CPU_LOADSTORE_STATUS
:
570 case PHB_ETU_ERR_SUMMARY
:
571 case PHB_PHB4_GEN_CAP
:
572 case PHB_PHB4_TCE_CAP
:
573 case PHB_PHB4_IRQ_CAP
:
574 case PHB_PHB4_EEH_CAP
:
578 /* Record whether it changed */
579 changed
= phb
->regs
[off
>> 3] != val
;
581 /* Store in register cache first */
582 phb
->regs
[off
>> 3] = val
;
584 /* Handle side effects */
586 case PHB_PHB4_CONFIG
:
588 pnv_phb4_update_all_msi_regions(phb
);
591 case PHB_M32_START_ADDR
:
592 case PHB_M64_UPPER_BITS
:
594 pnv_phb4_check_all_mbt(phb
);
598 /* IODA table accesses */
600 pnv_phb4_ioda_write(phb
, val
);
603 /* RTC invalidation */
604 case PHB_RTC_INVALIDATE
:
605 pnv_phb4_rtc_invalidate(phb
, val
);
608 /* PHB Control (Affects XIVE source) */
610 case PHB_LSI_SOURCE_ID
:
611 pnv_phb4_update_xsrc(phb
);
614 /* Silent simple writes */
616 case PHB_CONFIG_ADDRESS
:
619 case PHB_TCE_SPEC_CTL
:
623 case PHB_LEM_FIR_ACCUM
:
624 case PHB_LEM_ERROR_MASK
:
625 case PHB_LEM_ACTION0
:
626 case PHB_LEM_ACTION1
:
627 case PHB_TCE_TAG_ENABLE
:
628 case PHB_INT_NOTIFY_ADDR
:
629 case PHB_INT_NOTIFY_INDEX
:
633 /* Noise on anything else */
635 qemu_log_mask(LOG_UNIMP
, "phb4: reg_write 0x%"PRIx64
"=%"PRIx64
"\n",
640 static uint64_t pnv_phb4_reg_read(void *opaque
, hwaddr off
, unsigned size
)
642 PnvPHB4
*phb
= PNV_PHB4(opaque
);
645 if ((off
& 0xfffc) == PHB_CONFIG_DATA
) {
646 return pnv_phb4_config_read(phb
, off
& 0x3, size
);
649 /* Special case RC configuration space */
650 if ((off
& 0xf800) == PHB_RC_CONFIG_BASE
) {
651 return pnv_phb4_rc_config_read(phb
, off
& 0x7ff, size
);
654 /* Other registers are 64-bit only */
655 if (size
!= 8 || off
& 0x7) {
656 phb_error(phb
, "Invalid register access, offset: 0x%"PRIx64
" size: %d",
661 /* Default read from cache */
662 val
= phb
->regs
[off
>> 3];
666 return PNV_PHB4_PEC_GET_CLASS(phb
->pec
)->version
;
669 case PHB_PHB4_GEN_CAP
:
670 return 0xe4b8000000000000ull
;
671 case PHB_PHB4_TCE_CAP
:
672 return phb
->big_phb
? 0x4008440000000400ull
: 0x2008440000000200ull
;
673 case PHB_PHB4_IRQ_CAP
:
674 return phb
->big_phb
? 0x0800000000001000ull
: 0x0800000000000800ull
;
675 case PHB_PHB4_EEH_CAP
:
676 return phb
->big_phb
? 0x2000000000000000ull
: 0x1000000000000000ull
;
678 /* IODA table accesses */
680 return pnv_phb4_ioda_read(phb
);
682 /* Link training always appears trained */
683 case PHB_PCIE_DLP_TRAIN_CTL
:
684 /* TODO: Do something sensible with speed ? */
685 return PHB_PCIE_DLP_INBAND_PRESENCE
| PHB_PCIE_DLP_TL_LINKACT
;
687 /* DMA read sync: make it look like it's complete */
689 return PHB_DMARD_SYNC_COMPLETE
;
691 /* Silent simple reads */
692 case PHB_LSI_SOURCE_ID
:
693 case PHB_CPU_LOADSTORE_STATUS
:
695 case PHB_PHB4_CONFIG
:
696 case PHB_M32_START_ADDR
:
697 case PHB_CONFIG_ADDRESS
:
699 case PHB_RTC_INVALIDATE
:
701 case PHB_TCE_SPEC_CTL
:
705 case PHB_M64_UPPER_BITS
:
707 case PHB_LEM_FIR_ACCUM
:
708 case PHB_LEM_ERROR_MASK
:
709 case PHB_LEM_ACTION0
:
710 case PHB_LEM_ACTION1
:
711 case PHB_TCE_TAG_ENABLE
:
712 case PHB_INT_NOTIFY_ADDR
:
713 case PHB_INT_NOTIFY_INDEX
:
715 case PHB_ETU_ERR_SUMMARY
:
718 /* Noise on anything else */
720 qemu_log_mask(LOG_UNIMP
, "phb4: reg_read 0x%"PRIx64
"=%"PRIx64
"\n",
726 static const MemoryRegionOps pnv_phb4_reg_ops
= {
727 .read
= pnv_phb4_reg_read
,
728 .write
= pnv_phb4_reg_write
,
729 .valid
.min_access_size
= 1,
730 .valid
.max_access_size
= 8,
731 .impl
.min_access_size
= 1,
732 .impl
.max_access_size
= 8,
733 .endianness
= DEVICE_BIG_ENDIAN
,
736 static uint64_t pnv_phb4_xscom_read(void *opaque
, hwaddr addr
, unsigned size
)
738 PnvPHB4
*phb
= PNV_PHB4(opaque
);
739 uint32_t reg
= addr
>> 3;
744 case PHB_SCOM_HV_IND_ADDR
:
745 return phb
->scom_hv_ind_addr_reg
;
747 case PHB_SCOM_HV_IND_DATA
:
748 if (!(phb
->scom_hv_ind_addr_reg
& PHB_SCOM_HV_IND_ADDR_VALID
)) {
749 phb_error(phb
, "Invalid indirect address");
752 size
= (phb
->scom_hv_ind_addr_reg
& PHB_SCOM_HV_IND_ADDR_4B
) ? 4 : 8;
753 offset
= GETFIELD(PHB_SCOM_HV_IND_ADDR_ADDR
, phb
->scom_hv_ind_addr_reg
);
754 val
= pnv_phb4_reg_read(phb
, offset
, size
);
755 if (phb
->scom_hv_ind_addr_reg
& PHB_SCOM_HV_IND_ADDR_AUTOINC
) {
758 phb
->scom_hv_ind_addr_reg
= SETFIELD(PHB_SCOM_HV_IND_ADDR_ADDR
,
759 phb
->scom_hv_ind_addr_reg
,
763 case PHB_SCOM_ETU_LEM_FIR
:
764 case PHB_SCOM_ETU_LEM_FIR_AND
:
765 case PHB_SCOM_ETU_LEM_FIR_OR
:
766 case PHB_SCOM_ETU_LEM_FIR_MSK
:
767 case PHB_SCOM_ETU_LEM_ERR_MSK_AND
:
768 case PHB_SCOM_ETU_LEM_ERR_MSK_OR
:
769 case PHB_SCOM_ETU_LEM_ACT0
:
770 case PHB_SCOM_ETU_LEM_ACT1
:
771 case PHB_SCOM_ETU_LEM_WOF
:
772 offset
= ((reg
- PHB_SCOM_ETU_LEM_FIR
) << 3) + PHB_LEM_FIR_ACCUM
;
773 return pnv_phb4_reg_read(phb
, offset
, size
);
774 case PHB_SCOM_ETU_PMON_CONFIG
:
775 case PHB_SCOM_ETU_PMON_CTR0
:
776 case PHB_SCOM_ETU_PMON_CTR1
:
777 case PHB_SCOM_ETU_PMON_CTR2
:
778 case PHB_SCOM_ETU_PMON_CTR3
:
779 offset
= ((reg
- PHB_SCOM_ETU_PMON_CONFIG
) << 3) + PHB_PERFMON_CONFIG
;
780 return pnv_phb4_reg_read(phb
, offset
, size
);
783 qemu_log_mask(LOG_UNIMP
, "phb4: xscom_read 0x%"HWADDR_PRIx
"\n", addr
);
788 static void pnv_phb4_xscom_write(void *opaque
, hwaddr addr
,
789 uint64_t val
, unsigned size
)
791 PnvPHB4
*phb
= PNV_PHB4(opaque
);
792 uint32_t reg
= addr
>> 3;
796 case PHB_SCOM_HV_IND_ADDR
:
797 phb
->scom_hv_ind_addr_reg
= val
& 0xe000000000001fff;
799 case PHB_SCOM_HV_IND_DATA
:
800 if (!(phb
->scom_hv_ind_addr_reg
& PHB_SCOM_HV_IND_ADDR_VALID
)) {
801 phb_error(phb
, "Invalid indirect address");
804 size
= (phb
->scom_hv_ind_addr_reg
& PHB_SCOM_HV_IND_ADDR_4B
) ? 4 : 8;
805 offset
= GETFIELD(PHB_SCOM_HV_IND_ADDR_ADDR
, phb
->scom_hv_ind_addr_reg
);
806 pnv_phb4_reg_write(phb
, offset
, val
, size
);
807 if (phb
->scom_hv_ind_addr_reg
& PHB_SCOM_HV_IND_ADDR_AUTOINC
) {
810 phb
->scom_hv_ind_addr_reg
= SETFIELD(PHB_SCOM_HV_IND_ADDR_ADDR
,
811 phb
->scom_hv_ind_addr_reg
,
815 case PHB_SCOM_ETU_LEM_FIR
:
816 case PHB_SCOM_ETU_LEM_FIR_AND
:
817 case PHB_SCOM_ETU_LEM_FIR_OR
:
818 case PHB_SCOM_ETU_LEM_FIR_MSK
:
819 case PHB_SCOM_ETU_LEM_ERR_MSK_AND
:
820 case PHB_SCOM_ETU_LEM_ERR_MSK_OR
:
821 case PHB_SCOM_ETU_LEM_ACT0
:
822 case PHB_SCOM_ETU_LEM_ACT1
:
823 case PHB_SCOM_ETU_LEM_WOF
:
824 offset
= ((reg
- PHB_SCOM_ETU_LEM_FIR
) << 3) + PHB_LEM_FIR_ACCUM
;
825 pnv_phb4_reg_write(phb
, offset
, val
, size
);
827 case PHB_SCOM_ETU_PMON_CONFIG
:
828 case PHB_SCOM_ETU_PMON_CTR0
:
829 case PHB_SCOM_ETU_PMON_CTR1
:
830 case PHB_SCOM_ETU_PMON_CTR2
:
831 case PHB_SCOM_ETU_PMON_CTR3
:
832 offset
= ((reg
- PHB_SCOM_ETU_PMON_CONFIG
) << 3) + PHB_PERFMON_CONFIG
;
833 pnv_phb4_reg_write(phb
, offset
, val
, size
);
836 qemu_log_mask(LOG_UNIMP
, "phb4: xscom_write 0x%"HWADDR_PRIx
837 "=%"PRIx64
"\n", addr
, val
);
841 const MemoryRegionOps pnv_phb4_xscom_ops
= {
842 .read
= pnv_phb4_xscom_read
,
843 .write
= pnv_phb4_xscom_write
,
844 .valid
.min_access_size
= 8,
845 .valid
.max_access_size
= 8,
846 .impl
.min_access_size
= 8,
847 .impl
.max_access_size
= 8,
848 .endianness
= DEVICE_BIG_ENDIAN
,
851 static uint64_t pnv_pec_stk_nest_xscom_read(void *opaque
, hwaddr addr
,
854 PnvPHB4
*phb
= PNV_PHB4(opaque
);
855 uint32_t reg
= addr
>> 3;
857 /* All registers are read-able */
858 return phb
->nest_regs
[reg
];
862 * Return the 'stack_no' of a PHB4. 'stack_no' is the order
863 * the PHB4 occupies in the PEC. This is the reverse of what
864 * pnv_phb4_pec_get_phb_id() does.
866 * E.g. a phb with phb_id = 4 and pec->index = 1 (PEC1) will
867 * be the second phb (stack_no = 1) of the PEC.
869 static int pnv_phb4_get_phb_stack_no(PnvPHB4
*phb
)
871 PnvPhb4PecState
*pec
= phb
->pec
;
872 PnvPhb4PecClass
*pecc
= PNV_PHB4_PEC_GET_CLASS(pec
);
873 int index
= pec
->index
;
874 int stack_no
= phb
->phb_id
;
877 stack_no
-= pecc
->num_phbs
[index
];
883 static void pnv_phb4_update_regions(PnvPHB4
*phb
)
885 /* Unmap first always */
886 if (memory_region_is_mapped(&phb
->mr_regs
)) {
887 memory_region_del_subregion(&phb
->phbbar
, &phb
->mr_regs
);
889 if (memory_region_is_mapped(&phb
->xsrc
.esb_mmio
)) {
890 memory_region_del_subregion(&phb
->intbar
, &phb
->xsrc
.esb_mmio
);
893 /* Map registers if enabled */
894 if (memory_region_is_mapped(&phb
->phbbar
)) {
895 memory_region_add_subregion(&phb
->phbbar
, 0, &phb
->mr_regs
);
898 /* Map ESB if enabled */
899 if (memory_region_is_mapped(&phb
->intbar
)) {
900 memory_region_add_subregion(&phb
->intbar
, 0, &phb
->xsrc
.esb_mmio
);
903 /* Check/update m32 */
904 pnv_phb4_check_all_mbt(phb
);
907 static void pnv_pec_phb_update_map(PnvPHB4
*phb
)
909 PnvPhb4PecState
*pec
= phb
->pec
;
910 MemoryRegion
*sysmem
= get_system_memory();
911 uint64_t bar_en
= phb
->nest_regs
[PEC_NEST_STK_BAR_EN
];
912 int stack_no
= pnv_phb4_get_phb_stack_no(phb
);
913 uint64_t bar
, mask
, size
;
917 * NOTE: This will really not work well if those are remapped
918 * after the PHB has created its sub regions. We could do better
919 * if we had a way to resize regions but we don't really care
920 * that much in practice as the stuff below really only happens
921 * once early during boot
925 if (memory_region_is_mapped(&phb
->mmbar0
) &&
926 !(bar_en
& PEC_NEST_STK_BAR_EN_MMIO0
)) {
927 memory_region_del_subregion(sysmem
, &phb
->mmbar0
);
929 if (memory_region_is_mapped(&phb
->mmbar1
) &&
930 !(bar_en
& PEC_NEST_STK_BAR_EN_MMIO1
)) {
931 memory_region_del_subregion(sysmem
, &phb
->mmbar1
);
933 if (memory_region_is_mapped(&phb
->phbbar
) &&
934 !(bar_en
& PEC_NEST_STK_BAR_EN_PHB
)) {
935 memory_region_del_subregion(sysmem
, &phb
->phbbar
);
937 if (memory_region_is_mapped(&phb
->intbar
) &&
938 !(bar_en
& PEC_NEST_STK_BAR_EN_INT
)) {
939 memory_region_del_subregion(sysmem
, &phb
->intbar
);
943 pnv_phb4_update_regions(phb
);
946 if (!memory_region_is_mapped(&phb
->mmbar0
) &&
947 (bar_en
& PEC_NEST_STK_BAR_EN_MMIO0
)) {
948 bar
= phb
->nest_regs
[PEC_NEST_STK_MMIO_BAR0
] >> 8;
949 mask
= phb
->nest_regs
[PEC_NEST_STK_MMIO_BAR0_MASK
];
950 size
= ((~mask
) >> 8) + 1;
951 snprintf(name
, sizeof(name
), "pec-%d.%d-phb-%d-mmio0",
952 pec
->chip_id
, pec
->index
, stack_no
);
953 memory_region_init(&phb
->mmbar0
, OBJECT(phb
), name
, size
);
954 memory_region_add_subregion(sysmem
, bar
, &phb
->mmbar0
);
955 phb
->mmio0_base
= bar
;
956 phb
->mmio0_size
= size
;
958 if (!memory_region_is_mapped(&phb
->mmbar1
) &&
959 (bar_en
& PEC_NEST_STK_BAR_EN_MMIO1
)) {
960 bar
= phb
->nest_regs
[PEC_NEST_STK_MMIO_BAR1
] >> 8;
961 mask
= phb
->nest_regs
[PEC_NEST_STK_MMIO_BAR1_MASK
];
962 size
= ((~mask
) >> 8) + 1;
963 snprintf(name
, sizeof(name
), "pec-%d.%d-phb-%d-mmio1",
964 pec
->chip_id
, pec
->index
, stack_no
);
965 memory_region_init(&phb
->mmbar1
, OBJECT(phb
), name
, size
);
966 memory_region_add_subregion(sysmem
, bar
, &phb
->mmbar1
);
967 phb
->mmio1_base
= bar
;
968 phb
->mmio1_size
= size
;
970 if (!memory_region_is_mapped(&phb
->phbbar
) &&
971 (bar_en
& PEC_NEST_STK_BAR_EN_PHB
)) {
972 bar
= phb
->nest_regs
[PEC_NEST_STK_PHB_REGS_BAR
] >> 8;
973 size
= PNV_PHB4_NUM_REGS
<< 3;
974 snprintf(name
, sizeof(name
), "pec-%d.%d-phb-%d",
975 pec
->chip_id
, pec
->index
, stack_no
);
976 memory_region_init(&phb
->phbbar
, OBJECT(phb
), name
, size
);
977 memory_region_add_subregion(sysmem
, bar
, &phb
->phbbar
);
979 if (!memory_region_is_mapped(&phb
->intbar
) &&
980 (bar_en
& PEC_NEST_STK_BAR_EN_INT
)) {
981 bar
= phb
->nest_regs
[PEC_NEST_STK_INT_BAR
] >> 8;
982 size
= PNV_PHB4_MAX_INTs
<< 16;
983 snprintf(name
, sizeof(name
), "pec-%d.%d-phb-%d-int",
984 phb
->pec
->chip_id
, phb
->pec
->index
, stack_no
);
985 memory_region_init(&phb
->intbar
, OBJECT(phb
), name
, size
);
986 memory_region_add_subregion(sysmem
, bar
, &phb
->intbar
);
990 pnv_phb4_update_regions(phb
);
993 static void pnv_pec_stk_nest_xscom_write(void *opaque
, hwaddr addr
,
994 uint64_t val
, unsigned size
)
996 PnvPHB4
*phb
= PNV_PHB4(opaque
);
997 PnvPhb4PecState
*pec
= phb
->pec
;
998 uint32_t reg
= addr
>> 3;
1001 case PEC_NEST_STK_PCI_NEST_FIR
:
1002 phb
->nest_regs
[PEC_NEST_STK_PCI_NEST_FIR
] = val
& PPC_BITMASK(0, 27);
1004 case PEC_NEST_STK_PCI_NEST_FIR_CLR
:
1005 phb
->nest_regs
[PEC_NEST_STK_PCI_NEST_FIR
] &= val
;
1007 case PEC_NEST_STK_PCI_NEST_FIR_SET
:
1008 phb
->nest_regs
[PEC_NEST_STK_PCI_NEST_FIR
] |= val
;
1010 case PEC_NEST_STK_PCI_NEST_FIR_MSK
:
1011 phb
->nest_regs
[PEC_NEST_STK_PCI_NEST_FIR_MSK
] = val
&
1014 case PEC_NEST_STK_PCI_NEST_FIR_MSKC
:
1015 phb
->nest_regs
[PEC_NEST_STK_PCI_NEST_FIR_MSK
] &= val
;
1017 case PEC_NEST_STK_PCI_NEST_FIR_MSKS
:
1018 phb
->nest_regs
[PEC_NEST_STK_PCI_NEST_FIR_MSK
] |= val
;
1020 case PEC_NEST_STK_PCI_NEST_FIR_ACT0
:
1021 case PEC_NEST_STK_PCI_NEST_FIR_ACT1
:
1022 phb
->nest_regs
[reg
] = val
& PPC_BITMASK(0, 27);
1024 case PEC_NEST_STK_PCI_NEST_FIR_WOF
:
1025 phb
->nest_regs
[reg
] = 0;
1027 case PEC_NEST_STK_ERR_REPORT_0
:
1028 case PEC_NEST_STK_ERR_REPORT_1
:
1029 case PEC_NEST_STK_PBCQ_GNRL_STATUS
:
1032 case PEC_NEST_STK_PBCQ_MODE
:
1033 phb
->nest_regs
[reg
] = val
& PPC_BITMASK(0, 7);
1035 case PEC_NEST_STK_MMIO_BAR0
:
1036 case PEC_NEST_STK_MMIO_BAR0_MASK
:
1037 case PEC_NEST_STK_MMIO_BAR1
:
1038 case PEC_NEST_STK_MMIO_BAR1_MASK
:
1039 if (phb
->nest_regs
[PEC_NEST_STK_BAR_EN
] &
1040 (PEC_NEST_STK_BAR_EN_MMIO0
|
1041 PEC_NEST_STK_BAR_EN_MMIO1
)) {
1042 phb_pec_error(pec
, "Changing enabled BAR unsupported");
1044 phb
->nest_regs
[reg
] = val
& PPC_BITMASK(0, 39);
1046 case PEC_NEST_STK_PHB_REGS_BAR
:
1047 if (phb
->nest_regs
[PEC_NEST_STK_BAR_EN
] & PEC_NEST_STK_BAR_EN_PHB
) {
1048 phb_pec_error(pec
, "Changing enabled BAR unsupported");
1050 phb
->nest_regs
[reg
] = val
& PPC_BITMASK(0, 41);
1052 case PEC_NEST_STK_INT_BAR
:
1053 if (phb
->nest_regs
[PEC_NEST_STK_BAR_EN
] & PEC_NEST_STK_BAR_EN_INT
) {
1054 phb_pec_error(pec
, "Changing enabled BAR unsupported");
1056 phb
->nest_regs
[reg
] = val
& PPC_BITMASK(0, 27);
1058 case PEC_NEST_STK_BAR_EN
:
1059 phb
->nest_regs
[reg
] = val
& PPC_BITMASK(0, 3);
1060 pnv_pec_phb_update_map(phb
);
1062 case PEC_NEST_STK_DATA_FRZ_TYPE
:
1063 /* Not used for now */
1064 phb
->nest_regs
[reg
] = val
& PPC_BITMASK(0, 27);
1066 case PEC_NEST_STK_PBCQ_SPARSE_PAGE
:
1067 phb
->nest_regs
[reg
] = val
& PPC_BITMASK(3, 5);
1069 case PEC_NEST_STK_PBCQ_CACHE_INJ
:
1070 phb
->nest_regs
[reg
] = val
& PPC_BITMASK(0, 7);
1073 qemu_log_mask(LOG_UNIMP
, "phb4_pec: nest_xscom_write 0x%"HWADDR_PRIx
1074 "=%"PRIx64
"\n", addr
, val
);
1078 static const MemoryRegionOps pnv_pec_stk_nest_xscom_ops
= {
1079 .read
= pnv_pec_stk_nest_xscom_read
,
1080 .write
= pnv_pec_stk_nest_xscom_write
,
1081 .valid
.min_access_size
= 8,
1082 .valid
.max_access_size
= 8,
1083 .impl
.min_access_size
= 8,
1084 .impl
.max_access_size
= 8,
1085 .endianness
= DEVICE_BIG_ENDIAN
,
1088 static uint64_t pnv_pec_stk_pci_xscom_read(void *opaque
, hwaddr addr
,
1091 PnvPHB4
*phb
= PNV_PHB4(opaque
);
1092 uint32_t reg
= addr
>> 3;
1094 /* All registers are read-able */
1095 return phb
->pci_regs
[reg
];
1098 static void pnv_pec_stk_pci_xscom_write(void *opaque
, hwaddr addr
,
1099 uint64_t val
, unsigned size
)
1101 PnvPHB4
*phb
= PNV_PHB4(opaque
);
1102 uint32_t reg
= addr
>> 3;
1104 case PEC_PCI_STK_PCI_FIR
:
1105 phb
->pci_regs
[reg
] = val
& PPC_BITMASK(0, 5);
1107 case PEC_PCI_STK_PCI_FIR_CLR
:
1108 phb
->pci_regs
[PEC_PCI_STK_PCI_FIR
] &= val
;
1110 case PEC_PCI_STK_PCI_FIR_SET
:
1111 phb
->pci_regs
[PEC_PCI_STK_PCI_FIR
] |= val
;
1113 case PEC_PCI_STK_PCI_FIR_MSK
:
1114 phb
->pci_regs
[reg
] = val
& PPC_BITMASK(0, 5);
1116 case PEC_PCI_STK_PCI_FIR_MSKC
:
1117 phb
->pci_regs
[PEC_PCI_STK_PCI_FIR_MSK
] &= val
;
1119 case PEC_PCI_STK_PCI_FIR_MSKS
:
1120 phb
->pci_regs
[PEC_PCI_STK_PCI_FIR_MSK
] |= val
;
1122 case PEC_PCI_STK_PCI_FIR_ACT0
:
1123 case PEC_PCI_STK_PCI_FIR_ACT1
:
1124 phb
->pci_regs
[reg
] = val
& PPC_BITMASK(0, 5);
1126 case PEC_PCI_STK_PCI_FIR_WOF
:
1127 phb
->pci_regs
[reg
] = 0;
1129 case PEC_PCI_STK_ETU_RESET
:
1130 phb
->pci_regs
[reg
] = val
& PPC_BIT(0);
1131 /* TODO: Implement reset */
1133 case PEC_PCI_STK_PBAIB_ERR_REPORT
:
1135 case PEC_PCI_STK_PBAIB_TX_CMD_CRED
:
1136 phb
->pci_regs
[reg
] = val
&
1137 ((PPC_BITMASK(0, 2) | PPC_BITMASK(10, 18)
1138 | PPC_BITMASK(26, 34) | PPC_BITMASK(41, 50)
1139 | PPC_BITMASK(58, 63)));
1141 case PEC_PCI_STK_PBAIB_TX_DAT_CRED
:
1142 phb
->pci_regs
[reg
] = val
& (PPC_BITMASK(33, 34) | PPC_BITMASK(44, 47));
1145 qemu_log_mask(LOG_UNIMP
, "phb4_pec_stk: pci_xscom_write 0x%"HWADDR_PRIx
1146 "=%"PRIx64
"\n", addr
, val
);
1150 static const MemoryRegionOps pnv_pec_stk_pci_xscom_ops
= {
1151 .read
= pnv_pec_stk_pci_xscom_read
,
1152 .write
= pnv_pec_stk_pci_xscom_write
,
1153 .valid
.min_access_size
= 8,
1154 .valid
.max_access_size
= 8,
1155 .impl
.min_access_size
= 8,
1156 .impl
.max_access_size
= 8,
1157 .endianness
= DEVICE_BIG_ENDIAN
,
1160 static int pnv_phb4_map_irq(PCIDevice
*pci_dev
, int irq_num
)
1162 /* Check that out properly ... */
1166 static void pnv_phb4_set_irq(void *opaque
, int irq_num
, int level
)
1168 PnvPHB4
*phb
= PNV_PHB4(opaque
);
1173 phb_error(phb
, "IRQ %x is not an LSI", irq_num
);
1175 lsi_base
= GETFIELD(PHB_LSI_SRC_ID
, phb
->regs
[PHB_LSI_SOURCE_ID
>> 3]);
1177 qemu_set_irq(phb
->qirqs
[lsi_base
+ irq_num
], level
);
1180 static bool pnv_phb4_resolve_pe(PnvPhb4DMASpace
*ds
)
1187 /* Already resolved ? */
1188 if (ds
->pe_num
!= PHB_INVALID_PE
) {
1192 /* We need to lookup the RTT */
1193 rtt
= ds
->phb
->regs
[PHB_RTT_BAR
>> 3];
1194 if (!(rtt
& PHB_RTT_BAR_ENABLE
)) {
1195 phb_error(ds
->phb
, "DMA with RTT BAR disabled !");
1196 /* Set error bits ? fence ? ... */
1201 bus_num
= pci_bus_num(ds
->bus
);
1202 addr
= rtt
& PHB_RTT_BASE_ADDRESS_MASK
;
1203 addr
+= 2 * PCI_BUILD_BDF(bus_num
, ds
->devfn
);
1204 if (dma_memory_read(&address_space_memory
, addr
, &rte
,
1205 sizeof(rte
), MEMTXATTRS_UNSPECIFIED
)) {
1206 phb_error(ds
->phb
, "Failed to read RTT entry at 0x%"PRIx64
, addr
);
1207 /* Set error bits ? fence ? ... */
1210 rte
= be16_to_cpu(rte
);
1212 /* Fail upon reading of invalid PE# */
1213 num_PEs
= ds
->phb
->big_phb
? PNV_PHB4_MAX_PEs
: (PNV_PHB4_MAX_PEs
>> 1);
1214 if (rte
>= num_PEs
) {
1215 phb_error(ds
->phb
, "RTE for RID 0x%x invalid (%04x", ds
->devfn
, rte
);
1222 static void pnv_phb4_translate_tve(PnvPhb4DMASpace
*ds
, hwaddr addr
,
1223 bool is_write
, uint64_t tve
,
1226 uint64_t tta
= GETFIELD(IODA3_TVT_TABLE_ADDR
, tve
);
1227 int32_t lev
= GETFIELD(IODA3_TVT_NUM_LEVELS
, tve
);
1228 uint32_t tts
= GETFIELD(IODA3_TVT_TCE_TABLE_SIZE
, tve
);
1229 uint32_t tps
= GETFIELD(IODA3_TVT_IO_PSIZE
, tve
);
1231 /* Invalid levels */
1233 phb_error(ds
->phb
, "Invalid #levels in TVE %d", lev
);
1239 phb_error(ds
->phb
, "Access to invalid TVE");
1243 /* IO Page Size of 0 means untranslated, else use TCEs */
1245 /* TODO: Handle boundaries */
1247 /* Use 4k pages like q35 ... for now */
1248 tlb
->iova
= addr
& 0xfffffffffffff000ull
;
1249 tlb
->translated_addr
= addr
& 0x0003fffffffff000ull
;
1250 tlb
->addr_mask
= 0xfffull
;
1251 tlb
->perm
= IOMMU_RW
;
1253 uint32_t tce_shift
, tbl_shift
, sh
;
1254 uint64_t base
, taddr
, tce
, tce_mask
;
1256 /* Address bits per bottom level TCE entry */
1257 tce_shift
= tps
+ 11;
1259 /* Address bits per table level */
1260 tbl_shift
= tts
+ 8;
1262 /* Top level table base address */
1265 /* Total shift to first level */
1266 sh
= tbl_shift
* lev
+ tce_shift
;
1268 /* TODO: Limit to support IO page sizes */
1270 /* TODO: Multi-level untested */
1274 /* Grab the TCE address */
1275 taddr
= base
| (((addr
>> sh
) & ((1ul << tbl_shift
) - 1)) << 3);
1276 if (dma_memory_read(&address_space_memory
, taddr
, &tce
,
1277 sizeof(tce
), MEMTXATTRS_UNSPECIFIED
)) {
1278 phb_error(ds
->phb
, "Failed to read TCE at 0x%"PRIx64
, taddr
);
1281 tce
= be64_to_cpu(tce
);
1283 /* Check permission for indirect TCE */
1284 if ((lev
>= 0) && !(tce
& 3)) {
1285 phb_error(ds
->phb
, "Invalid indirect TCE at 0x%"PRIx64
, taddr
);
1286 phb_error(ds
->phb
, " xlate %"PRIx64
":%c TVE=%"PRIx64
, addr
,
1287 is_write
? 'W' : 'R', tve
);
1288 phb_error(ds
->phb
, " tta=%"PRIx64
" lev=%d tts=%d tps=%d",
1289 tta
, lev
, tts
, tps
);
1293 base
= tce
& ~0xfffull
;
1296 /* We exit the loop with TCE being the final TCE */
1297 if ((is_write
& !(tce
& 2)) || ((!is_write
) && !(tce
& 1))) {
1298 phb_error(ds
->phb
, "TCE access fault at 0x%"PRIx64
, taddr
);
1299 phb_error(ds
->phb
, " xlate %"PRIx64
":%c TVE=%"PRIx64
, addr
,
1300 is_write
? 'W' : 'R', tve
);
1301 phb_error(ds
->phb
, " tta=%"PRIx64
" lev=%d tts=%d tps=%d",
1302 tta
, lev
, tts
, tps
);
1305 tce_mask
= ~((1ull << tce_shift
) - 1);
1306 tlb
->iova
= addr
& tce_mask
;
1307 tlb
->translated_addr
= tce
& tce_mask
;
1308 tlb
->addr_mask
= ~tce_mask
;
1309 tlb
->perm
= tce
& 3;
1313 static IOMMUTLBEntry
pnv_phb4_translate_iommu(IOMMUMemoryRegion
*iommu
,
1315 IOMMUAccessFlags flag
,
1318 PnvPhb4DMASpace
*ds
= container_of(iommu
, PnvPhb4DMASpace
, dma_mr
);
1321 IOMMUTLBEntry ret
= {
1322 .target_as
= &address_space_memory
,
1324 .translated_addr
= 0,
1325 .addr_mask
= ~(hwaddr
)0,
1330 if (!pnv_phb4_resolve_pe(ds
)) {
1331 phb_error(ds
->phb
, "Failed to resolve PE# for bus @%p (%d) devfn 0x%x",
1332 ds
->bus
, pci_bus_num(ds
->bus
), ds
->devfn
);
1336 /* Check top bits */
1337 switch (addr
>> 60) {
1339 /* DMA or 32-bit MSI ? */
1340 cfg
= ds
->phb
->regs
[PHB_PHB4_CONFIG
>> 3];
1341 if ((cfg
& PHB_PHB4C_32BIT_MSI_EN
) &&
1342 ((addr
& 0xffffffffffff0000ull
) == 0xffff0000ull
)) {
1343 phb_error(ds
->phb
, "xlate on 32-bit MSI region");
1346 /* Choose TVE XXX Use PHB4 Control Register */
1347 tve_sel
= (addr
>> 59) & 1;
1348 tve
= ds
->phb
->ioda_TVT
[ds
->pe_num
* 2 + tve_sel
];
1349 pnv_phb4_translate_tve(ds
, addr
, flag
& IOMMU_WO
, tve
, &ret
);
1352 phb_error(ds
->phb
, "xlate on 64-bit MSI region");
1355 phb_error(ds
->phb
, "xlate on unsupported address 0x%"PRIx64
, addr
);
1360 #define TYPE_PNV_PHB4_IOMMU_MEMORY_REGION "pnv-phb4-iommu-memory-region"
1361 DECLARE_INSTANCE_CHECKER(IOMMUMemoryRegion
, PNV_PHB4_IOMMU_MEMORY_REGION
,
1362 TYPE_PNV_PHB4_IOMMU_MEMORY_REGION
)
1364 static void pnv_phb4_iommu_memory_region_class_init(ObjectClass
*klass
,
1367 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_CLASS(klass
);
1369 imrc
->translate
= pnv_phb4_translate_iommu
;
1372 static const TypeInfo pnv_phb4_iommu_memory_region_info
= {
1373 .parent
= TYPE_IOMMU_MEMORY_REGION
,
1374 .name
= TYPE_PNV_PHB4_IOMMU_MEMORY_REGION
,
1375 .class_init
= pnv_phb4_iommu_memory_region_class_init
,
1379 * Return the index/phb-id of a PHB4 that belongs to a
1380 * pec->stacks[stack_index] stack.
1382 int pnv_phb4_pec_get_phb_id(PnvPhb4PecState
*pec
, int stack_index
)
1384 PnvPhb4PecClass
*pecc
= PNV_PHB4_PEC_GET_CLASS(pec
);
1385 int index
= pec
->index
;
1389 offset
+= pecc
->num_phbs
[index
];
1392 return offset
+ stack_index
;
1396 * MSI/MSIX memory region implementation.
1397 * The handler handles both MSI and MSIX.
1399 static void pnv_phb4_msi_write(void *opaque
, hwaddr addr
,
1400 uint64_t data
, unsigned size
)
1402 PnvPhb4DMASpace
*ds
= opaque
;
1403 PnvPHB4
*phb
= ds
->phb
;
1405 uint32_t src
= ((addr
>> 4) & 0xffff) | (data
& 0x1f);
1408 if (!pnv_phb4_resolve_pe(ds
)) {
1409 phb_error(phb
, "Failed to resolve PE# for bus @%p (%d) devfn 0x%x",
1410 ds
->bus
, pci_bus_num(ds
->bus
), ds
->devfn
);
1414 /* TODO: Check it doesn't collide with LSIs */
1415 if (src
>= phb
->xsrc
.nr_irqs
) {
1416 phb_error(phb
, "MSI %d out of bounds", src
);
1420 /* TODO: check PE/MSI assignment */
1422 qemu_irq_pulse(phb
->qirqs
[src
]);
1425 /* There is no .read as the read result is undefined by PCI spec */
1426 static uint64_t pnv_phb4_msi_read(void *opaque
, hwaddr addr
, unsigned size
)
1428 PnvPhb4DMASpace
*ds
= opaque
;
1430 phb_error(ds
->phb
, "Invalid MSI read @ 0x%" HWADDR_PRIx
, addr
);
1434 static const MemoryRegionOps pnv_phb4_msi_ops
= {
1435 .read
= pnv_phb4_msi_read
,
1436 .write
= pnv_phb4_msi_write
,
1437 .endianness
= DEVICE_LITTLE_ENDIAN
1440 static PnvPhb4DMASpace
*pnv_phb4_dma_find(PnvPHB4
*phb
, PCIBus
*bus
, int devfn
)
1442 PnvPhb4DMASpace
*ds
;
1444 QLIST_FOREACH(ds
, &phb
->dma_spaces
, list
) {
1445 if (ds
->bus
== bus
&& ds
->devfn
== devfn
) {
1452 static AddressSpace
*pnv_phb4_dma_iommu(PCIBus
*bus
, void *opaque
, int devfn
)
1454 PnvPHB4
*phb
= opaque
;
1455 PnvPhb4DMASpace
*ds
;
1458 ds
= pnv_phb4_dma_find(phb
, bus
, devfn
);
1461 ds
= g_new0(PnvPhb4DMASpace
, 1);
1464 ds
->pe_num
= PHB_INVALID_PE
;
1466 snprintf(name
, sizeof(name
), "phb4-%d.%d-iommu", phb
->chip_id
,
1468 memory_region_init_iommu(&ds
->dma_mr
, sizeof(ds
->dma_mr
),
1469 TYPE_PNV_PHB4_IOMMU_MEMORY_REGION
,
1470 OBJECT(phb
), name
, UINT64_MAX
);
1471 address_space_init(&ds
->dma_as
, MEMORY_REGION(&ds
->dma_mr
),
1473 memory_region_init_io(&ds
->msi32_mr
, OBJECT(phb
), &pnv_phb4_msi_ops
,
1474 ds
, "msi32", 0x10000);
1475 memory_region_init_io(&ds
->msi64_mr
, OBJECT(phb
), &pnv_phb4_msi_ops
,
1476 ds
, "msi64", 0x100000);
1477 pnv_phb4_update_msi_regions(ds
);
1479 QLIST_INSERT_HEAD(&phb
->dma_spaces
, ds
, list
);
1484 static void pnv_phb4_xscom_realize(PnvPHB4
*phb
)
1486 PnvPhb4PecState
*pec
= phb
->pec
;
1487 PnvPhb4PecClass
*pecc
= PNV_PHB4_PEC_GET_CLASS(pec
);
1488 int stack_no
= pnv_phb4_get_phb_stack_no(phb
);
1489 uint32_t pec_nest_base
;
1490 uint32_t pec_pci_base
;
1495 /* Initialize the XSCOM regions for the stack registers */
1496 snprintf(name
, sizeof(name
), "xscom-pec-%d.%d-nest-phb-%d",
1497 pec
->chip_id
, pec
->index
, stack_no
);
1498 pnv_xscom_region_init(&phb
->nest_regs_mr
, OBJECT(phb
),
1499 &pnv_pec_stk_nest_xscom_ops
, phb
, name
,
1500 PHB4_PEC_NEST_STK_REGS_COUNT
);
1502 snprintf(name
, sizeof(name
), "xscom-pec-%d.%d-pci-phb-%d",
1503 pec
->chip_id
, pec
->index
, stack_no
);
1504 pnv_xscom_region_init(&phb
->pci_regs_mr
, OBJECT(phb
),
1505 &pnv_pec_stk_pci_xscom_ops
, phb
, name
,
1506 PHB4_PEC_PCI_STK_REGS_COUNT
);
1508 /* PHB pass-through */
1509 snprintf(name
, sizeof(name
), "xscom-pec-%d.%d-phb-%d",
1510 pec
->chip_id
, pec
->index
, stack_no
);
1511 pnv_xscom_region_init(&phb
->phb_regs_mr
, OBJECT(phb
),
1512 &pnv_phb4_xscom_ops
, phb
, name
, 0x40);
1514 pec_nest_base
= pecc
->xscom_nest_base(pec
);
1515 pec_pci_base
= pecc
->xscom_pci_base(pec
);
1517 /* Populate the XSCOM address space. */
1518 pnv_xscom_add_subregion(pec
->chip
,
1519 pec_nest_base
+ 0x40 * (stack_no
+ 1),
1520 &phb
->nest_regs_mr
);
1521 pnv_xscom_add_subregion(pec
->chip
,
1522 pec_pci_base
+ 0x40 * (stack_no
+ 1),
1524 pnv_xscom_add_subregion(pec
->chip
,
1525 pec_pci_base
+ PNV9_XSCOM_PEC_PCI_STK0
+
1530 static PCIIOMMUOps pnv_phb4_iommu_ops
= {
1531 .get_address_space
= pnv_phb4_dma_iommu
,
1534 static void pnv_phb4_instance_init(Object
*obj
)
1536 PnvPHB4
*phb
= PNV_PHB4(obj
);
1538 QLIST_INIT(&phb
->dma_spaces
);
1540 /* XIVE interrupt source object */
1541 object_initialize_child(obj
, "source", &phb
->xsrc
, TYPE_XIVE_SOURCE
);
1544 void pnv_phb4_bus_init(DeviceState
*dev
, PnvPHB4
*phb
)
1546 PCIHostState
*pci
= PCI_HOST_BRIDGE(dev
);
1550 * PHB4 doesn't support IO space. However, qemu gets very upset if
1551 * we don't have an IO region to anchor IO BARs onto so we just
1552 * initialize one which we never hook up to anything
1554 snprintf(name
, sizeof(name
), "phb4-%d.%d-pci-io", phb
->chip_id
,
1556 memory_region_init(&phb
->pci_io
, OBJECT(phb
), name
, 0x10000);
1558 snprintf(name
, sizeof(name
), "phb4-%d.%d-pci-mmio", phb
->chip_id
,
1560 memory_region_init(&phb
->pci_mmio
, OBJECT(phb
), name
,
1561 PCI_MMIO_TOTAL_SIZE
);
1563 pci
->bus
= pci_register_root_bus(dev
, dev
->id
? dev
->id
: NULL
,
1564 pnv_phb4_set_irq
, pnv_phb4_map_irq
, phb
,
1565 &phb
->pci_mmio
, &phb
->pci_io
,
1566 0, 4, TYPE_PNV_PHB4_ROOT_BUS
);
1568 object_property_set_int(OBJECT(pci
->bus
), "phb-id", phb
->phb_id
,
1570 object_property_set_int(OBJECT(pci
->bus
), "chip-id", phb
->chip_id
,
1573 pci_setup_iommu(pci
->bus
, &pnv_phb4_iommu_ops
, phb
);
1574 pci
->bus
->flags
|= PCI_BUS_EXTENDED_CONFIG_SPACE
;
1577 static void pnv_phb4_realize(DeviceState
*dev
, Error
**errp
)
1579 PnvPHB4
*phb
= PNV_PHB4(dev
);
1580 XiveSource
*xsrc
= &phb
->xsrc
;
1584 /* Set the "big_phb" flag */
1585 phb
->big_phb
= phb
->phb_id
== 0 || phb
->phb_id
== 3;
1587 /* Controller Registers */
1588 snprintf(name
, sizeof(name
), "phb4-%d.%d-regs", phb
->chip_id
,
1590 memory_region_init_io(&phb
->mr_regs
, OBJECT(phb
), &pnv_phb4_reg_ops
, phb
,
1593 /* Setup XIVE Source */
1595 nr_irqs
= PNV_PHB4_MAX_INTs
;
1597 nr_irqs
= PNV_PHB4_MAX_INTs
>> 1;
1599 object_property_set_int(OBJECT(xsrc
), "nr-irqs", nr_irqs
, &error_fatal
);
1600 object_property_set_link(OBJECT(xsrc
), "xive", OBJECT(phb
), &error_fatal
);
1601 if (!qdev_realize(DEVICE(xsrc
), NULL
, errp
)) {
1605 pnv_phb4_update_xsrc(phb
);
1607 phb
->qirqs
= qemu_allocate_irqs(xive_source_set_irq
, xsrc
, xsrc
->nr_irqs
);
1609 pnv_phb4_xscom_realize(phb
);
1613 * Address base trigger mode (POWER10)
1615 * Trigger directly the IC ESB page
1617 static void pnv_phb4_xive_notify_abt(PnvPHB4
*phb
, uint32_t srcno
,
1620 uint64_t notif_port
= phb
->regs
[PHB_INT_NOTIFY_ADDR
>> 3];
1621 uint64_t data
= 0; /* trigger data : don't care */
1626 if (notif_port
& PHB_INT_NOTIFY_ADDR_64K
) {
1632 /* Compute the address of the IC ESB management page */
1633 addr
= (notif_port
& ~PHB_INT_NOTIFY_ADDR_64K
);
1634 addr
|= (1ull << (esb_shift
+ 1)) * srcno
;
1635 addr
|= (1ull << esb_shift
);
1638 * When the PQ state bits are checked on the PHB, the associated
1639 * PQ state bits on the IC should be ignored. Use the unconditional
1640 * trigger offset to inject a trigger on the IC. This is always
1644 addr
|= XIVE_ESB_INJECT
;
1647 trace_pnv_phb4_xive_notify_ic(addr
, data
);
1649 address_space_stq_be(&address_space_memory
, addr
, data
,
1650 MEMTXATTRS_UNSPECIFIED
, &result
);
1651 if (result
!= MEMTX_OK
) {
1652 phb_error(phb
, "trigger failed @%"HWADDR_PRIx
"\n", addr
);
1657 static void pnv_phb4_xive_notify_ic(PnvPHB4
*phb
, uint32_t srcno
,
1660 uint64_t notif_port
= phb
->regs
[PHB_INT_NOTIFY_ADDR
>> 3];
1661 uint32_t offset
= phb
->regs
[PHB_INT_NOTIFY_INDEX
>> 3];
1662 uint64_t data
= offset
| srcno
;
1666 data
|= XIVE_TRIGGER_PQ
;
1669 trace_pnv_phb4_xive_notify_ic(notif_port
, data
);
1671 address_space_stq_be(&address_space_memory
, notif_port
, data
,
1672 MEMTXATTRS_UNSPECIFIED
, &result
);
1673 if (result
!= MEMTX_OK
) {
1674 phb_error(phb
, "trigger failed @%"HWADDR_PRIx
"\n", notif_port
);
1679 static void pnv_phb4_xive_notify(XiveNotifier
*xf
, uint32_t srcno
,
1682 PnvPHB4
*phb
= PNV_PHB4(xf
);
1684 if (phb
->regs
[PHB_CTRLR
>> 3] & PHB_CTRLR_IRQ_ABT_MODE
) {
1685 pnv_phb4_xive_notify_abt(phb
, srcno
, pq_checked
);
1687 pnv_phb4_xive_notify_ic(phb
, srcno
, pq_checked
);
1691 static Property pnv_phb4_properties
[] = {
1692 DEFINE_PROP_UINT32("index", PnvPHB4
, phb_id
, 0),
1693 DEFINE_PROP_UINT32("chip-id", PnvPHB4
, chip_id
, 0),
1694 DEFINE_PROP_LINK("pec", PnvPHB4
, pec
, TYPE_PNV_PHB4_PEC
,
1696 DEFINE_PROP_LINK("phb-base", PnvPHB4
, phb_base
, TYPE_PNV_PHB
, PnvPHB
*),
1697 DEFINE_PROP_END_OF_LIST(),
1700 static void pnv_phb4_class_init(ObjectClass
*klass
, void *data
)
1702 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1703 XiveNotifierClass
*xfc
= XIVE_NOTIFIER_CLASS(klass
);
1705 dc
->realize
= pnv_phb4_realize
;
1706 device_class_set_props(dc
, pnv_phb4_properties
);
1707 dc
->user_creatable
= false;
1709 xfc
->notify
= pnv_phb4_xive_notify
;
1712 static const TypeInfo pnv_phb4_type_info
= {
1713 .name
= TYPE_PNV_PHB4
,
1714 .parent
= TYPE_DEVICE
,
1715 .instance_init
= pnv_phb4_instance_init
,
1716 .instance_size
= sizeof(PnvPHB4
),
1717 .class_init
= pnv_phb4_class_init
,
1718 .interfaces
= (InterfaceInfo
[]) {
1719 { TYPE_XIVE_NOTIFIER
},
1724 static const TypeInfo pnv_phb5_type_info
= {
1725 .name
= TYPE_PNV_PHB5
,
1726 .parent
= TYPE_PNV_PHB4
,
1727 .instance_size
= sizeof(PnvPHB4
),
1730 static void pnv_phb4_root_bus_get_prop(Object
*obj
, Visitor
*v
,
1732 void *opaque
, Error
**errp
)
1734 PnvPHB4RootBus
*bus
= PNV_PHB4_ROOT_BUS(obj
);
1737 if (strcmp(name
, "phb-id") == 0) {
1738 value
= bus
->phb_id
;
1740 value
= bus
->chip_id
;
1743 visit_type_size(v
, name
, &value
, errp
);
1746 static void pnv_phb4_root_bus_set_prop(Object
*obj
, Visitor
*v
,
1748 void *opaque
, Error
**errp
)
1751 PnvPHB4RootBus
*bus
= PNV_PHB4_ROOT_BUS(obj
);
1754 if (!visit_type_size(v
, name
, &value
, errp
)) {
1758 if (strcmp(name
, "phb-id") == 0) {
1759 bus
->phb_id
= value
;
1761 bus
->chip_id
= value
;
1765 static void pnv_phb4_root_bus_class_init(ObjectClass
*klass
, void *data
)
1767 BusClass
*k
= BUS_CLASS(klass
);
1769 object_class_property_add(klass
, "phb-id", "int",
1770 pnv_phb4_root_bus_get_prop
,
1771 pnv_phb4_root_bus_set_prop
,
1774 object_class_property_add(klass
, "chip-id", "int",
1775 pnv_phb4_root_bus_get_prop
,
1776 pnv_phb4_root_bus_set_prop
,
1780 * PHB4 has only a single root complex. Enforce the limit on the
1786 static const TypeInfo pnv_phb4_root_bus_info
= {
1787 .name
= TYPE_PNV_PHB4_ROOT_BUS
,
1788 .parent
= TYPE_PCIE_BUS
,
1789 .instance_size
= sizeof(PnvPHB4RootBus
),
1790 .class_init
= pnv_phb4_root_bus_class_init
,
1793 static void pnv_phb4_register_types(void)
1795 type_register_static(&pnv_phb4_root_bus_info
);
1796 type_register_static(&pnv_phb4_type_info
);
1797 type_register_static(&pnv_phb5_type_info
);
1798 type_register_static(&pnv_phb4_iommu_memory_region_info
);
1801 type_init(pnv_phb4_register_types
);
1803 void pnv_phb4_pic_print_info(PnvPHB4
*phb
, GString
*buf
)
1805 uint64_t notif_port
=
1806 phb
->regs
[PHB_INT_NOTIFY_ADDR
>> 3] & ~PHB_INT_NOTIFY_ADDR_64K
;
1807 uint32_t offset
= phb
->regs
[PHB_INT_NOTIFY_INDEX
>> 3];
1808 bool abt
= !!(phb
->regs
[PHB_CTRLR
>> 3] & PHB_CTRLR_IRQ_ABT_MODE
);
1810 g_string_append_printf(buf
,
1811 "PHB4[%x:%x] Source %08x .. %08x "
1812 "%s @%"HWADDR_PRIx
"\n",
1813 phb
->chip_id
, phb
->phb_id
,
1814 offset
, offset
+ phb
->xsrc
.nr_irqs
- 1,
1817 xive_source_pic_print_info(&phb
->xsrc
, 0, buf
);