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 "qemu-common.h"
14 #include "monitor/monitor.h"
15 #include "target/ppc/cpu.h"
16 #include "hw/pci-host/pnv_phb4_regs.h"
17 #include "hw/pci-host/pnv_phb4.h"
18 #include "hw/pci/pcie_host.h"
19 #include "hw/pci/pcie_port.h"
20 #include "hw/ppc/pnv.h"
21 #include "hw/ppc/pnv_xscom.h"
23 #include "hw/qdev-properties.h"
24 #include "qom/object.h"
27 #define phb_error(phb, fmt, ...) \
28 qemu_log_mask(LOG_GUEST_ERROR, "phb4[%d:%d]: " fmt "\n", \
29 (phb)->chip_id, (phb)->phb_id, ## __VA_ARGS__)
31 #define phb_pec_error(pec, fmt, ...) \
32 qemu_log_mask(LOG_GUEST_ERROR, "phb4_pec[%d:%d]: " fmt "\n", \
33 (pec)->chip_id, (pec)->index, ## __VA_ARGS__)
36 * QEMU version of the GETFIELD/SETFIELD macros
38 * These are common with the PnvXive model.
40 static inline uint64_t GETFIELD(uint64_t mask
, uint64_t word
)
42 return (word
& mask
) >> ctz64(mask
);
45 static inline uint64_t SETFIELD(uint64_t mask
, uint64_t word
,
48 return (word
& ~mask
) | ((value
<< ctz64(mask
)) & mask
);
51 static PCIDevice
*pnv_phb4_find_cfg_dev(PnvPHB4
*phb
)
53 PCIHostState
*pci
= PCI_HOST_BRIDGE(phb
);
54 uint64_t addr
= phb
->regs
[PHB_CONFIG_ADDRESS
>> 3];
60 bus
= (addr
>> 52) & 0xff;
61 devfn
= (addr
>> 44) & 0xff;
63 /* We don't access the root complex this way */
64 if (bus
== 0 && devfn
== 0) {
67 return pci_find_device(pci
->bus
, bus
, devfn
);
71 * The CONFIG_DATA register expects little endian accesses, but as the
72 * region is big endian, we have to swap the value.
74 static void pnv_phb4_config_write(PnvPHB4
*phb
, unsigned off
,
75 unsigned size
, uint64_t val
)
77 uint32_t cfg_addr
, limit
;
80 pdev
= pnv_phb4_find_cfg_dev(phb
);
84 cfg_addr
= (phb
->regs
[PHB_CONFIG_ADDRESS
>> 3] >> 32) & 0xffc;
86 limit
= pci_config_size(pdev
);
87 if (limit
<= cfg_addr
) {
89 * conventional pci device can be behind pcie-to-pci bridge.
90 * 256 <= addr < 4K has no effects.
104 g_assert_not_reached();
106 pci_host_config_write_common(pdev
, cfg_addr
, limit
, val
, size
);
109 static uint64_t pnv_phb4_config_read(PnvPHB4
*phb
, unsigned off
,
112 uint32_t cfg_addr
, limit
;
116 pdev
= pnv_phb4_find_cfg_dev(phb
);
120 cfg_addr
= (phb
->regs
[PHB_CONFIG_ADDRESS
>> 3] >> 32) & 0xffc;
122 limit
= pci_config_size(pdev
);
123 if (limit
<= cfg_addr
) {
125 * conventional pci device can be behind pcie-to-pci bridge.
126 * 256 <= addr < 4K has no effects.
130 val
= pci_host_config_read_common(pdev
, cfg_addr
, limit
, size
);
139 g_assert_not_reached();
144 * Root complex register accesses are memory mapped.
146 static void pnv_phb4_rc_config_write(PnvPHB4
*phb
, unsigned off
,
147 unsigned size
, uint64_t val
)
149 PCIHostState
*pci
= PCI_HOST_BRIDGE(phb
);
153 phb_error(phb
, "rc_config_write invalid size %d\n", size
);
157 pdev
= pci_find_device(pci
->bus
, 0, 0);
159 phb_error(phb
, "rc_config_write device not found\n");
163 pci_host_config_write_common(pdev
, off
, PHB_RC_CONFIG_SIZE
,
167 static uint64_t pnv_phb4_rc_config_read(PnvPHB4
*phb
, unsigned off
,
170 PCIHostState
*pci
= PCI_HOST_BRIDGE(phb
);
175 phb_error(phb
, "rc_config_read invalid size %d\n", size
);
179 pdev
= pci_find_device(pci
->bus
, 0, 0);
181 phb_error(phb
, "rc_config_read device not found\n");
185 val
= pci_host_config_read_common(pdev
, off
, PHB_RC_CONFIG_SIZE
, 4);
189 static void pnv_phb4_check_mbt(PnvPHB4
*phb
, uint32_t index
)
191 uint64_t base
, start
, size
, mbe0
, mbe1
;
192 MemoryRegion
*parent
;
196 if (memory_region_is_mapped(&phb
->mr_mmio
[index
])) {
197 /* Should we destroy it in RCU friendly way... ? */
198 memory_region_del_subregion(phb
->mr_mmio
[index
].container
,
199 &phb
->mr_mmio
[index
]);
202 /* Get table entry */
203 mbe0
= phb
->ioda_MBT
[(index
<< 1)];
204 mbe1
= phb
->ioda_MBT
[(index
<< 1) + 1];
206 if (!(mbe0
& IODA3_MBT0_ENABLE
)) {
210 /* Grab geometry from registers */
211 base
= GETFIELD(IODA3_MBT0_BASE_ADDR
, mbe0
) << 12;
212 size
= GETFIELD(IODA3_MBT1_MASK
, mbe1
) << 12;
213 size
|= 0xff00000000000000ull
;
216 /* Calculate PCI side start address based on M32/M64 window type */
217 if (mbe0
& IODA3_MBT0_TYPE_M32
) {
218 start
= phb
->regs
[PHB_M32_START_ADDR
>> 3];
219 if ((start
+ size
) > 0x100000000ull
) {
220 phb_error(phb
, "M32 set beyond 4GB boundary !");
221 size
= 0x100000000 - start
;
224 start
= base
| (phb
->regs
[PHB_M64_UPPER_BITS
>> 3]);
227 /* TODO: Figure out how to implemet/decode AOMASK */
229 /* Check if it matches an enabled MMIO region in the PEC stack */
230 if (memory_region_is_mapped(&phb
->mmbar0
) &&
231 base
>= phb
->mmio0_base
&&
232 (base
+ size
) <= (phb
->mmio0_base
+ phb
->mmio0_size
)) {
233 parent
= &phb
->mmbar0
;
234 base
-= phb
->mmio0_base
;
235 } else if (memory_region_is_mapped(&phb
->mmbar1
) &&
236 base
>= phb
->mmio1_base
&&
237 (base
+ size
) <= (phb
->mmio1_base
+ phb
->mmio1_size
)) {
238 parent
= &phb
->mmbar1
;
239 base
-= phb
->mmio1_base
;
241 phb_error(phb
, "PHB MBAR %d out of parent bounds", index
);
245 /* Create alias (better name ?) */
246 snprintf(name
, sizeof(name
), "phb4-mbar%d", index
);
247 memory_region_init_alias(&phb
->mr_mmio
[index
], OBJECT(phb
), name
,
248 &phb
->pci_mmio
, start
, size
);
249 memory_region_add_subregion(parent
, base
, &phb
->mr_mmio
[index
]);
252 static void pnv_phb4_check_all_mbt(PnvPHB4
*phb
)
255 uint32_t num_windows
= phb
->big_phb
? PNV_PHB4_MAX_MMIO_WINDOWS
:
256 PNV_PHB4_MIN_MMIO_WINDOWS
;
258 for (i
= 0; i
< num_windows
; i
++) {
259 pnv_phb4_check_mbt(phb
, i
);
263 static uint64_t *pnv_phb4_ioda_access(PnvPHB4
*phb
,
264 unsigned *out_table
, unsigned *out_idx
)
266 uint64_t adreg
= phb
->regs
[PHB_IODA_ADDR
>> 3];
267 unsigned int index
= GETFIELD(PHB_IODA_AD_TADR
, adreg
);
268 unsigned int table
= GETFIELD(PHB_IODA_AD_TSEL
, adreg
);
270 uint64_t *tptr
= NULL
;
274 tptr
= phb
->ioda_LIST
;
278 tptr
= phb
->ioda_MIST
;
279 mask
= phb
->big_phb
? PNV_PHB4_MAX_MIST
: (PNV_PHB4_MAX_MIST
>> 1);
283 mask
= phb
->big_phb
? 127 : 63;
286 mask
= phb
->big_phb
? 15 : 7;
288 case IODA3_TBL_PESTA
:
289 case IODA3_TBL_PESTB
:
290 mask
= phb
->big_phb
? PNV_PHB4_MAX_PEs
: (PNV_PHB4_MAX_PEs
>> 1);
294 tptr
= phb
->ioda_TVT
;
295 mask
= phb
->big_phb
? PNV_PHB4_MAX_TVEs
: (PNV_PHB4_MAX_TVEs
>> 1);
300 mask
= phb
->big_phb
? 1023 : 511;
303 tptr
= phb
->ioda_MBT
;
304 mask
= phb
->big_phb
? PNV_PHB4_MAX_MBEs
: (PNV_PHB4_MAX_MBEs
>> 1);
308 tptr
= phb
->ioda_MDT
;
309 mask
= phb
->big_phb
? PNV_PHB4_MAX_PEs
: (PNV_PHB4_MAX_PEs
>> 1);
313 tptr
= phb
->ioda_PEEV
;
314 mask
= phb
->big_phb
? PNV_PHB4_MAX_PEEVs
: (PNV_PHB4_MAX_PEEVs
>> 1);
318 phb_error(phb
, "invalid IODA table %d", table
);
331 if (adreg
& PHB_IODA_AD_AUTOINC
) {
332 index
= (index
+ 1) & mask
;
333 adreg
= SETFIELD(PHB_IODA_AD_TADR
, adreg
, index
);
336 phb
->regs
[PHB_IODA_ADDR
>> 3] = adreg
;
340 static uint64_t pnv_phb4_ioda_read(PnvPHB4
*phb
)
345 tptr
= pnv_phb4_ioda_access(phb
, &table
, &idx
);
347 /* Special PESTA case */
348 if (table
== IODA3_TBL_PESTA
) {
349 return ((uint64_t)(phb
->ioda_PEST_AB
[idx
] & 1)) << 63;
350 } else if (table
== IODA3_TBL_PESTB
) {
351 return ((uint64_t)(phb
->ioda_PEST_AB
[idx
] & 2)) << 62;
353 /* Return 0 on unsupported tables, not ff's */
359 static void pnv_phb4_ioda_write(PnvPHB4
*phb
, uint64_t val
)
364 tptr
= pnv_phb4_ioda_access(phb
, &table
, &idx
);
366 /* Special PESTA case */
367 if (table
== IODA3_TBL_PESTA
) {
368 phb
->ioda_PEST_AB
[idx
] &= ~1;
369 phb
->ioda_PEST_AB
[idx
] |= (val
>> 63) & 1;
370 } else if (table
== IODA3_TBL_PESTB
) {
371 phb
->ioda_PEST_AB
[idx
] &= ~2;
372 phb
->ioda_PEST_AB
[idx
] |= (val
>> 62) & 2;
377 /* Handle side effects */
381 case IODA3_TBL_MIST
: {
382 /* Special mask for MIST partial write */
383 uint64_t adreg
= phb
->regs
[PHB_IODA_ADDR
>> 3];
384 uint32_t mmask
= GETFIELD(PHB_IODA_AD_MIST_PWV
, adreg
);
390 v
&= 0x0000ffffffffffffull
;
391 v
|= 0xcfff000000000000ull
& val
;
394 v
&= 0xffff0000ffffffffull
;
395 v
|= 0x0000cfff00000000ull
& val
;
398 v
&= 0xffffffff0000ffffull
;
399 v
|= 0x00000000cfff0000ull
& val
;
402 v
&= 0xffffffffffff0000ull
;
403 v
|= 0x000000000000cfffull
& val
;
411 /* Copy accross the valid bit to the other half */
412 phb
->ioda_MBT
[idx
^ 1] &= 0x7fffffffffffffffull
;
413 phb
->ioda_MBT
[idx
^ 1] |= 0x8000000000000000ull
& val
;
415 /* Update mappings */
416 pnv_phb4_check_mbt(phb
, idx
>> 1);
423 static void pnv_phb4_rtc_invalidate(PnvPHB4
*phb
, uint64_t val
)
427 /* Always invalidate all for now ... */
428 QLIST_FOREACH(ds
, &phb
->dma_spaces
, list
) {
429 ds
->pe_num
= PHB_INVALID_PE
;
433 static void pnv_phb4_update_msi_regions(PnvPhb4DMASpace
*ds
)
435 uint64_t cfg
= ds
->phb
->regs
[PHB_PHB4_CONFIG
>> 3];
437 if (cfg
& PHB_PHB4C_32BIT_MSI_EN
) {
438 if (!memory_region_is_mapped(MEMORY_REGION(&ds
->msi32_mr
))) {
439 memory_region_add_subregion(MEMORY_REGION(&ds
->dma_mr
),
440 0xffff0000, &ds
->msi32_mr
);
443 if (memory_region_is_mapped(MEMORY_REGION(&ds
->msi32_mr
))) {
444 memory_region_del_subregion(MEMORY_REGION(&ds
->dma_mr
),
449 if (cfg
& PHB_PHB4C_64BIT_MSI_EN
) {
450 if (!memory_region_is_mapped(MEMORY_REGION(&ds
->msi64_mr
))) {
451 memory_region_add_subregion(MEMORY_REGION(&ds
->dma_mr
),
452 (1ull << 60), &ds
->msi64_mr
);
455 if (memory_region_is_mapped(MEMORY_REGION(&ds
->msi64_mr
))) {
456 memory_region_del_subregion(MEMORY_REGION(&ds
->dma_mr
),
462 static void pnv_phb4_update_all_msi_regions(PnvPHB4
*phb
)
466 QLIST_FOREACH(ds
, &phb
->dma_spaces
, list
) {
467 pnv_phb4_update_msi_regions(ds
);
471 static void pnv_phb4_update_xsrc(PnvPHB4
*phb
)
473 int shift
, flags
, i
, lsi_base
;
474 XiveSource
*xsrc
= &phb
->xsrc
;
476 /* The XIVE source characteristics can be set at run time */
477 if (phb
->regs
[PHB_CTRLR
>> 3] & PHB_CTRLR_IRQ_PGSZ_64K
) {
478 shift
= XIVE_ESB_64K
;
482 if (phb
->regs
[PHB_CTRLR
>> 3] & PHB_CTRLR_IRQ_STORE_EOI
) {
483 flags
= XIVE_SRC_STORE_EOI
;
488 phb
->xsrc
.esb_shift
= shift
;
489 phb
->xsrc
.esb_flags
= flags
;
491 lsi_base
= GETFIELD(PHB_LSI_SRC_ID
, phb
->regs
[PHB_LSI_SOURCE_ID
>> 3]);
494 /* TODO: handle reset values of PHB_LSI_SRC_ID */
499 /* TODO: need a xive_source_irq_reset_lsi() */
500 bitmap_zero(xsrc
->lsi_map
, xsrc
->nr_irqs
);
502 for (i
= 0; i
< xsrc
->nr_irqs
; i
++) {
503 bool msi
= (i
< lsi_base
|| i
>= (lsi_base
+ 8));
505 xive_source_irq_set_lsi(xsrc
, i
);
510 static void pnv_phb4_reg_write(void *opaque
, hwaddr off
, uint64_t val
,
513 PnvPHB4
*phb
= PNV_PHB4(opaque
);
516 /* Special case outbound configuration data */
517 if ((off
& 0xfffc) == PHB_CONFIG_DATA
) {
518 pnv_phb4_config_write(phb
, off
& 0x3, size
, val
);
522 /* Special case RC configuration space */
523 if ((off
& 0xf800) == PHB_RC_CONFIG_BASE
) {
524 pnv_phb4_rc_config_write(phb
, off
& 0x7ff, size
, val
);
528 /* Other registers are 64-bit only */
529 if (size
!= 8 || off
& 0x7) {
530 phb_error(phb
, "Invalid register access, offset: 0x%"PRIx64
" size: %d",
537 case PHB_LSI_SOURCE_ID
:
538 val
&= PHB_LSI_SRC_ID
;
540 case PHB_M64_UPPER_BITS
:
541 val
&= 0xff00000000000000ull
;
545 /* Clear top 3 bits which HW does to indicate successful queuing */
546 val
&= ~(PHB_TCE_KILL_ALL
| PHB_TCE_KILL_PE
| PHB_TCE_KILL_ONE
);
550 * This is enough logic to make SW happy but we aren't
551 * actually quiescing the DMAs
553 if (val
& PHB_Q_DMA_R_AUTORESET
) {
556 val
&= PHB_Q_DMA_R_QUIESCE_DMA
;
560 case PHB_LEM_FIR_AND_MASK
:
561 phb
->regs
[PHB_LEM_FIR_ACCUM
>> 3] &= val
;
563 case PHB_LEM_FIR_OR_MASK
:
564 phb
->regs
[PHB_LEM_FIR_ACCUM
>> 3] |= val
;
566 case PHB_LEM_ERROR_AND_MASK
:
567 phb
->regs
[PHB_LEM_ERROR_MASK
>> 3] &= val
;
569 case PHB_LEM_ERROR_OR_MASK
:
570 phb
->regs
[PHB_LEM_ERROR_MASK
>> 3] |= val
;
575 /* TODO: More regs ..., maybe create a table with masks... */
577 /* Read only registers */
578 case PHB_CPU_LOADSTORE_STATUS
:
579 case PHB_ETU_ERR_SUMMARY
:
580 case PHB_PHB4_GEN_CAP
:
581 case PHB_PHB4_TCE_CAP
:
582 case PHB_PHB4_IRQ_CAP
:
583 case PHB_PHB4_EEH_CAP
:
587 /* Record whether it changed */
588 changed
= phb
->regs
[off
>> 3] != val
;
590 /* Store in register cache first */
591 phb
->regs
[off
>> 3] = val
;
593 /* Handle side effects */
595 case PHB_PHB4_CONFIG
:
597 pnv_phb4_update_all_msi_regions(phb
);
600 case PHB_M32_START_ADDR
:
601 case PHB_M64_UPPER_BITS
:
603 pnv_phb4_check_all_mbt(phb
);
607 /* IODA table accesses */
609 pnv_phb4_ioda_write(phb
, val
);
612 /* RTC invalidation */
613 case PHB_RTC_INVALIDATE
:
614 pnv_phb4_rtc_invalidate(phb
, val
);
617 /* PHB Control (Affects XIVE source) */
619 case PHB_LSI_SOURCE_ID
:
620 pnv_phb4_update_xsrc(phb
);
623 /* Silent simple writes */
625 case PHB_CONFIG_ADDRESS
:
628 case PHB_TCE_SPEC_CTL
:
632 case PHB_LEM_FIR_ACCUM
:
633 case PHB_LEM_ERROR_MASK
:
634 case PHB_LEM_ACTION0
:
635 case PHB_LEM_ACTION1
:
636 case PHB_TCE_TAG_ENABLE
:
637 case PHB_INT_NOTIFY_ADDR
:
638 case PHB_INT_NOTIFY_INDEX
:
642 /* Noise on anything else */
644 qemu_log_mask(LOG_UNIMP
, "phb4: reg_write 0x%"PRIx64
"=%"PRIx64
"\n",
649 static uint64_t pnv_phb4_reg_read(void *opaque
, hwaddr off
, unsigned size
)
651 PnvPHB4
*phb
= PNV_PHB4(opaque
);
654 if ((off
& 0xfffc) == PHB_CONFIG_DATA
) {
655 return pnv_phb4_config_read(phb
, off
& 0x3, size
);
658 /* Special case RC configuration space */
659 if ((off
& 0xf800) == PHB_RC_CONFIG_BASE
) {
660 return pnv_phb4_rc_config_read(phb
, off
& 0x7ff, size
);
663 /* Other registers are 64-bit only */
664 if (size
!= 8 || off
& 0x7) {
665 phb_error(phb
, "Invalid register access, offset: 0x%"PRIx64
" size: %d",
670 /* Default read from cache */
671 val
= phb
->regs
[off
>> 3];
675 return PNV_PHB4_PEC_GET_CLASS(phb
->pec
)->version
;
678 case PHB_PHB4_GEN_CAP
:
679 return 0xe4b8000000000000ull
;
680 case PHB_PHB4_TCE_CAP
:
681 return phb
->big_phb
? 0x4008440000000400ull
: 0x2008440000000200ull
;
682 case PHB_PHB4_IRQ_CAP
:
683 return phb
->big_phb
? 0x0800000000001000ull
: 0x0800000000000800ull
;
684 case PHB_PHB4_EEH_CAP
:
685 return phb
->big_phb
? 0x2000000000000000ull
: 0x1000000000000000ull
;
687 /* IODA table accesses */
689 return pnv_phb4_ioda_read(phb
);
691 /* Link training always appears trained */
692 case PHB_PCIE_DLP_TRAIN_CTL
:
693 /* TODO: Do something sensible with speed ? */
694 return PHB_PCIE_DLP_INBAND_PRESENCE
| PHB_PCIE_DLP_TL_LINKACT
;
696 /* DMA read sync: make it look like it's complete */
698 return PHB_DMARD_SYNC_COMPLETE
;
700 /* Silent simple reads */
701 case PHB_LSI_SOURCE_ID
:
702 case PHB_CPU_LOADSTORE_STATUS
:
704 case PHB_PHB4_CONFIG
:
705 case PHB_M32_START_ADDR
:
706 case PHB_CONFIG_ADDRESS
:
708 case PHB_RTC_INVALIDATE
:
710 case PHB_TCE_SPEC_CTL
:
714 case PHB_M64_UPPER_BITS
:
716 case PHB_LEM_FIR_ACCUM
:
717 case PHB_LEM_ERROR_MASK
:
718 case PHB_LEM_ACTION0
:
719 case PHB_LEM_ACTION1
:
720 case PHB_TCE_TAG_ENABLE
:
721 case PHB_INT_NOTIFY_ADDR
:
722 case PHB_INT_NOTIFY_INDEX
:
724 case PHB_ETU_ERR_SUMMARY
:
727 /* Noise on anything else */
729 qemu_log_mask(LOG_UNIMP
, "phb4: reg_read 0x%"PRIx64
"=%"PRIx64
"\n",
735 static const MemoryRegionOps pnv_phb4_reg_ops
= {
736 .read
= pnv_phb4_reg_read
,
737 .write
= pnv_phb4_reg_write
,
738 .valid
.min_access_size
= 1,
739 .valid
.max_access_size
= 8,
740 .impl
.min_access_size
= 1,
741 .impl
.max_access_size
= 8,
742 .endianness
= DEVICE_BIG_ENDIAN
,
745 static uint64_t pnv_phb4_xscom_read(void *opaque
, hwaddr addr
, unsigned size
)
747 PnvPHB4
*phb
= PNV_PHB4(opaque
);
748 uint32_t reg
= addr
>> 3;
753 case PHB_SCOM_HV_IND_ADDR
:
754 return phb
->scom_hv_ind_addr_reg
;
756 case PHB_SCOM_HV_IND_DATA
:
757 if (!(phb
->scom_hv_ind_addr_reg
& PHB_SCOM_HV_IND_ADDR_VALID
)) {
758 phb_error(phb
, "Invalid indirect address");
761 size
= (phb
->scom_hv_ind_addr_reg
& PHB_SCOM_HV_IND_ADDR_4B
) ? 4 : 8;
762 offset
= GETFIELD(PHB_SCOM_HV_IND_ADDR_ADDR
, phb
->scom_hv_ind_addr_reg
);
763 val
= pnv_phb4_reg_read(phb
, offset
, size
);
764 if (phb
->scom_hv_ind_addr_reg
& PHB_SCOM_HV_IND_ADDR_AUTOINC
) {
767 phb
->scom_hv_ind_addr_reg
= SETFIELD(PHB_SCOM_HV_IND_ADDR_ADDR
,
768 phb
->scom_hv_ind_addr_reg
,
772 case PHB_SCOM_ETU_LEM_FIR
:
773 case PHB_SCOM_ETU_LEM_FIR_AND
:
774 case PHB_SCOM_ETU_LEM_FIR_OR
:
775 case PHB_SCOM_ETU_LEM_FIR_MSK
:
776 case PHB_SCOM_ETU_LEM_ERR_MSK_AND
:
777 case PHB_SCOM_ETU_LEM_ERR_MSK_OR
:
778 case PHB_SCOM_ETU_LEM_ACT0
:
779 case PHB_SCOM_ETU_LEM_ACT1
:
780 case PHB_SCOM_ETU_LEM_WOF
:
781 offset
= ((reg
- PHB_SCOM_ETU_LEM_FIR
) << 3) + PHB_LEM_FIR_ACCUM
;
782 return pnv_phb4_reg_read(phb
, offset
, size
);
783 case PHB_SCOM_ETU_PMON_CONFIG
:
784 case PHB_SCOM_ETU_PMON_CTR0
:
785 case PHB_SCOM_ETU_PMON_CTR1
:
786 case PHB_SCOM_ETU_PMON_CTR2
:
787 case PHB_SCOM_ETU_PMON_CTR3
:
788 offset
= ((reg
- PHB_SCOM_ETU_PMON_CONFIG
) << 3) + PHB_PERFMON_CONFIG
;
789 return pnv_phb4_reg_read(phb
, offset
, size
);
792 qemu_log_mask(LOG_UNIMP
, "phb4: xscom_read 0x%"HWADDR_PRIx
"\n", addr
);
797 static void pnv_phb4_xscom_write(void *opaque
, hwaddr addr
,
798 uint64_t val
, unsigned size
)
800 PnvPHB4
*phb
= PNV_PHB4(opaque
);
801 uint32_t reg
= addr
>> 3;
805 case PHB_SCOM_HV_IND_ADDR
:
806 phb
->scom_hv_ind_addr_reg
= val
& 0xe000000000001fff;
808 case PHB_SCOM_HV_IND_DATA
:
809 if (!(phb
->scom_hv_ind_addr_reg
& PHB_SCOM_HV_IND_ADDR_VALID
)) {
810 phb_error(phb
, "Invalid indirect address");
813 size
= (phb
->scom_hv_ind_addr_reg
& PHB_SCOM_HV_IND_ADDR_4B
) ? 4 : 8;
814 offset
= GETFIELD(PHB_SCOM_HV_IND_ADDR_ADDR
, phb
->scom_hv_ind_addr_reg
);
815 pnv_phb4_reg_write(phb
, offset
, val
, size
);
816 if (phb
->scom_hv_ind_addr_reg
& PHB_SCOM_HV_IND_ADDR_AUTOINC
) {
819 phb
->scom_hv_ind_addr_reg
= SETFIELD(PHB_SCOM_HV_IND_ADDR_ADDR
,
820 phb
->scom_hv_ind_addr_reg
,
824 case PHB_SCOM_ETU_LEM_FIR
:
825 case PHB_SCOM_ETU_LEM_FIR_AND
:
826 case PHB_SCOM_ETU_LEM_FIR_OR
:
827 case PHB_SCOM_ETU_LEM_FIR_MSK
:
828 case PHB_SCOM_ETU_LEM_ERR_MSK_AND
:
829 case PHB_SCOM_ETU_LEM_ERR_MSK_OR
:
830 case PHB_SCOM_ETU_LEM_ACT0
:
831 case PHB_SCOM_ETU_LEM_ACT1
:
832 case PHB_SCOM_ETU_LEM_WOF
:
833 offset
= ((reg
- PHB_SCOM_ETU_LEM_FIR
) << 3) + PHB_LEM_FIR_ACCUM
;
834 pnv_phb4_reg_write(phb
, offset
, val
, size
);
836 case PHB_SCOM_ETU_PMON_CONFIG
:
837 case PHB_SCOM_ETU_PMON_CTR0
:
838 case PHB_SCOM_ETU_PMON_CTR1
:
839 case PHB_SCOM_ETU_PMON_CTR2
:
840 case PHB_SCOM_ETU_PMON_CTR3
:
841 offset
= ((reg
- PHB_SCOM_ETU_PMON_CONFIG
) << 3) + PHB_PERFMON_CONFIG
;
842 pnv_phb4_reg_write(phb
, offset
, val
, size
);
845 qemu_log_mask(LOG_UNIMP
, "phb4: xscom_write 0x%"HWADDR_PRIx
846 "=%"PRIx64
"\n", addr
, val
);
850 const MemoryRegionOps pnv_phb4_xscom_ops
= {
851 .read
= pnv_phb4_xscom_read
,
852 .write
= pnv_phb4_xscom_write
,
853 .valid
.min_access_size
= 8,
854 .valid
.max_access_size
= 8,
855 .impl
.min_access_size
= 8,
856 .impl
.max_access_size
= 8,
857 .endianness
= DEVICE_BIG_ENDIAN
,
860 static uint64_t pnv_pec_stk_nest_xscom_read(void *opaque
, hwaddr addr
,
863 PnvPHB4
*phb
= PNV_PHB4(opaque
);
864 uint32_t reg
= addr
>> 3;
866 /* TODO: add list of allowed registers and error out if not */
867 return phb
->nest_regs
[reg
];
871 * Return the 'stack_no' of a PHB4. 'stack_no' is the order
872 * the PHB4 occupies in the PEC. This is the reverse of what
873 * pnv_phb4_pec_get_phb_id() does.
875 * E.g. a phb with phb_id = 4 and pec->index = 1 (PEC1) will
876 * be the second phb (stack_no = 1) of the PEC.
878 static int pnv_phb4_get_phb_stack_no(PnvPHB4
*phb
)
880 PnvPhb4PecState
*pec
= phb
->pec
;
881 PnvPhb4PecClass
*pecc
= PNV_PHB4_PEC_GET_CLASS(pec
);
882 int index
= pec
->index
;
883 int stack_no
= phb
->phb_id
;
886 stack_no
-= pecc
->num_phbs
[index
];
892 static void pnv_phb4_update_regions(PnvPHB4
*phb
)
894 /* Unmap first always */
895 if (memory_region_is_mapped(&phb
->mr_regs
)) {
896 memory_region_del_subregion(&phb
->phbbar
, &phb
->mr_regs
);
898 if (memory_region_is_mapped(&phb
->xsrc
.esb_mmio
)) {
899 memory_region_del_subregion(&phb
->intbar
, &phb
->xsrc
.esb_mmio
);
902 /* Map registers if enabled */
903 if (memory_region_is_mapped(&phb
->phbbar
)) {
904 memory_region_add_subregion(&phb
->phbbar
, 0, &phb
->mr_regs
);
907 /* Map ESB if enabled */
908 if (memory_region_is_mapped(&phb
->intbar
)) {
909 memory_region_add_subregion(&phb
->intbar
, 0, &phb
->xsrc
.esb_mmio
);
912 /* Check/update m32 */
913 pnv_phb4_check_all_mbt(phb
);
916 static void pnv_pec_phb_update_map(PnvPHB4
*phb
)
918 PnvPhb4PecState
*pec
= phb
->pec
;
919 MemoryRegion
*sysmem
= get_system_memory();
920 uint64_t bar_en
= phb
->nest_regs
[PEC_NEST_STK_BAR_EN
];
921 int stack_no
= pnv_phb4_get_phb_stack_no(phb
);
922 uint64_t bar
, mask
, size
;
926 * NOTE: This will really not work well if those are remapped
927 * after the PHB has created its sub regions. We could do better
928 * if we had a way to resize regions but we don't really care
929 * that much in practice as the stuff below really only happens
930 * once early during boot
934 if (memory_region_is_mapped(&phb
->mmbar0
) &&
935 !(bar_en
& PEC_NEST_STK_BAR_EN_MMIO0
)) {
936 memory_region_del_subregion(sysmem
, &phb
->mmbar0
);
938 if (memory_region_is_mapped(&phb
->mmbar1
) &&
939 !(bar_en
& PEC_NEST_STK_BAR_EN_MMIO1
)) {
940 memory_region_del_subregion(sysmem
, &phb
->mmbar1
);
942 if (memory_region_is_mapped(&phb
->phbbar
) &&
943 !(bar_en
& PEC_NEST_STK_BAR_EN_PHB
)) {
944 memory_region_del_subregion(sysmem
, &phb
->phbbar
);
946 if (memory_region_is_mapped(&phb
->intbar
) &&
947 !(bar_en
& PEC_NEST_STK_BAR_EN_INT
)) {
948 memory_region_del_subregion(sysmem
, &phb
->intbar
);
952 pnv_phb4_update_regions(phb
);
955 if (!memory_region_is_mapped(&phb
->mmbar0
) &&
956 (bar_en
& PEC_NEST_STK_BAR_EN_MMIO0
)) {
957 bar
= phb
->nest_regs
[PEC_NEST_STK_MMIO_BAR0
] >> 8;
958 mask
= phb
->nest_regs
[PEC_NEST_STK_MMIO_BAR0_MASK
];
959 size
= ((~mask
) >> 8) + 1;
960 snprintf(name
, sizeof(name
), "pec-%d.%d-phb-%d-mmio0",
961 pec
->chip_id
, pec
->index
, stack_no
);
962 memory_region_init(&phb
->mmbar0
, OBJECT(phb
), name
, size
);
963 memory_region_add_subregion(sysmem
, bar
, &phb
->mmbar0
);
964 phb
->mmio0_base
= bar
;
965 phb
->mmio0_size
= size
;
967 if (!memory_region_is_mapped(&phb
->mmbar1
) &&
968 (bar_en
& PEC_NEST_STK_BAR_EN_MMIO1
)) {
969 bar
= phb
->nest_regs
[PEC_NEST_STK_MMIO_BAR1
] >> 8;
970 mask
= phb
->nest_regs
[PEC_NEST_STK_MMIO_BAR1_MASK
];
971 size
= ((~mask
) >> 8) + 1;
972 snprintf(name
, sizeof(name
), "pec-%d.%d-phb-%d-mmio1",
973 pec
->chip_id
, pec
->index
, stack_no
);
974 memory_region_init(&phb
->mmbar1
, OBJECT(phb
), name
, size
);
975 memory_region_add_subregion(sysmem
, bar
, &phb
->mmbar1
);
976 phb
->mmio1_base
= bar
;
977 phb
->mmio1_size
= size
;
979 if (!memory_region_is_mapped(&phb
->phbbar
) &&
980 (bar_en
& PEC_NEST_STK_BAR_EN_PHB
)) {
981 bar
= phb
->nest_regs
[PEC_NEST_STK_PHB_REGS_BAR
] >> 8;
982 size
= PNV_PHB4_NUM_REGS
<< 3;
983 snprintf(name
, sizeof(name
), "pec-%d.%d-phb-%d",
984 pec
->chip_id
, pec
->index
, stack_no
);
985 memory_region_init(&phb
->phbbar
, OBJECT(phb
), name
, size
);
986 memory_region_add_subregion(sysmem
, bar
, &phb
->phbbar
);
988 if (!memory_region_is_mapped(&phb
->intbar
) &&
989 (bar_en
& PEC_NEST_STK_BAR_EN_INT
)) {
990 bar
= phb
->nest_regs
[PEC_NEST_STK_INT_BAR
] >> 8;
991 size
= PNV_PHB4_MAX_INTs
<< 16;
992 snprintf(name
, sizeof(name
), "pec-%d.%d-phb-%d-int",
993 phb
->pec
->chip_id
, phb
->pec
->index
, stack_no
);
994 memory_region_init(&phb
->intbar
, OBJECT(phb
), name
, size
);
995 memory_region_add_subregion(sysmem
, bar
, &phb
->intbar
);
999 pnv_phb4_update_regions(phb
);
1002 static void pnv_pec_stk_nest_xscom_write(void *opaque
, hwaddr addr
,
1003 uint64_t val
, unsigned size
)
1005 PnvPHB4
*phb
= PNV_PHB4(opaque
);
1006 PnvPhb4PecState
*pec
= phb
->pec
;
1007 uint32_t reg
= addr
>> 3;
1010 case PEC_NEST_STK_PCI_NEST_FIR
:
1011 phb
->nest_regs
[PEC_NEST_STK_PCI_NEST_FIR
] = val
;
1013 case PEC_NEST_STK_PCI_NEST_FIR_CLR
:
1014 phb
->nest_regs
[PEC_NEST_STK_PCI_NEST_FIR
] &= val
;
1016 case PEC_NEST_STK_PCI_NEST_FIR_SET
:
1017 phb
->nest_regs
[PEC_NEST_STK_PCI_NEST_FIR
] |= val
;
1019 case PEC_NEST_STK_PCI_NEST_FIR_MSK
:
1020 phb
->nest_regs
[PEC_NEST_STK_PCI_NEST_FIR_MSK
] = val
;
1022 case PEC_NEST_STK_PCI_NEST_FIR_MSKC
:
1023 phb
->nest_regs
[PEC_NEST_STK_PCI_NEST_FIR_MSK
] &= val
;
1025 case PEC_NEST_STK_PCI_NEST_FIR_MSKS
:
1026 phb
->nest_regs
[PEC_NEST_STK_PCI_NEST_FIR_MSK
] |= val
;
1028 case PEC_NEST_STK_PCI_NEST_FIR_ACT0
:
1029 case PEC_NEST_STK_PCI_NEST_FIR_ACT1
:
1030 phb
->nest_regs
[reg
] = val
;
1032 case PEC_NEST_STK_PCI_NEST_FIR_WOF
:
1033 phb
->nest_regs
[reg
] = 0;
1035 case PEC_NEST_STK_ERR_REPORT_0
:
1036 case PEC_NEST_STK_ERR_REPORT_1
:
1037 case PEC_NEST_STK_PBCQ_GNRL_STATUS
:
1040 case PEC_NEST_STK_PBCQ_MODE
:
1041 phb
->nest_regs
[reg
] = val
& 0xff00000000000000ull
;
1043 case PEC_NEST_STK_MMIO_BAR0
:
1044 case PEC_NEST_STK_MMIO_BAR0_MASK
:
1045 case PEC_NEST_STK_MMIO_BAR1
:
1046 case PEC_NEST_STK_MMIO_BAR1_MASK
:
1047 if (phb
->nest_regs
[PEC_NEST_STK_BAR_EN
] &
1048 (PEC_NEST_STK_BAR_EN_MMIO0
|
1049 PEC_NEST_STK_BAR_EN_MMIO1
)) {
1050 phb_pec_error(pec
, "Changing enabled BAR unsupported\n");
1052 phb
->nest_regs
[reg
] = val
& 0xffffffffff000000ull
;
1054 case PEC_NEST_STK_PHB_REGS_BAR
:
1055 if (phb
->nest_regs
[PEC_NEST_STK_BAR_EN
] & PEC_NEST_STK_BAR_EN_PHB
) {
1056 phb_pec_error(pec
, "Changing enabled BAR unsupported\n");
1058 phb
->nest_regs
[reg
] = val
& 0xffffffffffc00000ull
;
1060 case PEC_NEST_STK_INT_BAR
:
1061 if (phb
->nest_regs
[PEC_NEST_STK_BAR_EN
] & PEC_NEST_STK_BAR_EN_INT
) {
1062 phb_pec_error(pec
, "Changing enabled BAR unsupported\n");
1064 phb
->nest_regs
[reg
] = val
& 0xfffffff000000000ull
;
1066 case PEC_NEST_STK_BAR_EN
:
1067 phb
->nest_regs
[reg
] = val
& 0xf000000000000000ull
;
1068 pnv_pec_phb_update_map(phb
);
1070 case PEC_NEST_STK_DATA_FRZ_TYPE
:
1071 case PEC_NEST_STK_PBCQ_TUN_BAR
:
1072 /* Not used for now */
1073 phb
->nest_regs
[reg
] = val
;
1076 qemu_log_mask(LOG_UNIMP
, "phb4_pec: nest_xscom_write 0x%"HWADDR_PRIx
1077 "=%"PRIx64
"\n", addr
, val
);
1081 static const MemoryRegionOps pnv_pec_stk_nest_xscom_ops
= {
1082 .read
= pnv_pec_stk_nest_xscom_read
,
1083 .write
= pnv_pec_stk_nest_xscom_write
,
1084 .valid
.min_access_size
= 8,
1085 .valid
.max_access_size
= 8,
1086 .impl
.min_access_size
= 8,
1087 .impl
.max_access_size
= 8,
1088 .endianness
= DEVICE_BIG_ENDIAN
,
1091 static uint64_t pnv_pec_stk_pci_xscom_read(void *opaque
, hwaddr addr
,
1094 PnvPHB4
*phb
= PNV_PHB4(opaque
);
1095 uint32_t reg
= addr
>> 3;
1097 /* TODO: add list of allowed registers and error out if not */
1098 return phb
->pci_regs
[reg
];
1101 static void pnv_pec_stk_pci_xscom_write(void *opaque
, hwaddr addr
,
1102 uint64_t val
, unsigned size
)
1104 PnvPHB4
*phb
= PNV_PHB4(opaque
);
1105 uint32_t reg
= addr
>> 3;
1108 case PEC_PCI_STK_PCI_FIR
:
1109 phb
->pci_regs
[reg
] = val
;
1111 case PEC_PCI_STK_PCI_FIR_CLR
:
1112 phb
->pci_regs
[PEC_PCI_STK_PCI_FIR
] &= val
;
1114 case PEC_PCI_STK_PCI_FIR_SET
:
1115 phb
->pci_regs
[PEC_PCI_STK_PCI_FIR
] |= val
;
1117 case PEC_PCI_STK_PCI_FIR_MSK
:
1118 phb
->pci_regs
[reg
] = val
;
1120 case PEC_PCI_STK_PCI_FIR_MSKC
:
1121 phb
->pci_regs
[PEC_PCI_STK_PCI_FIR_MSK
] &= val
;
1123 case PEC_PCI_STK_PCI_FIR_MSKS
:
1124 phb
->pci_regs
[PEC_PCI_STK_PCI_FIR_MSK
] |= val
;
1126 case PEC_PCI_STK_PCI_FIR_ACT0
:
1127 case PEC_PCI_STK_PCI_FIR_ACT1
:
1128 phb
->pci_regs
[reg
] = val
;
1130 case PEC_PCI_STK_PCI_FIR_WOF
:
1131 phb
->pci_regs
[reg
] = 0;
1133 case PEC_PCI_STK_ETU_RESET
:
1134 phb
->pci_regs
[reg
] = val
& 0x8000000000000000ull
;
1135 /* TODO: Implement reset */
1137 case PEC_PCI_STK_PBAIB_ERR_REPORT
:
1139 case PEC_PCI_STK_PBAIB_TX_CMD_CRED
:
1140 case PEC_PCI_STK_PBAIB_TX_DAT_CRED
:
1141 phb
->pci_regs
[reg
] = val
;
1144 qemu_log_mask(LOG_UNIMP
, "phb4_pec_stk: pci_xscom_write 0x%"HWADDR_PRIx
1145 "=%"PRIx64
"\n", addr
, val
);
1149 static const MemoryRegionOps pnv_pec_stk_pci_xscom_ops
= {
1150 .read
= pnv_pec_stk_pci_xscom_read
,
1151 .write
= pnv_pec_stk_pci_xscom_write
,
1152 .valid
.min_access_size
= 8,
1153 .valid
.max_access_size
= 8,
1154 .impl
.min_access_size
= 8,
1155 .impl
.max_access_size
= 8,
1156 .endianness
= DEVICE_BIG_ENDIAN
,
1159 static int pnv_phb4_map_irq(PCIDevice
*pci_dev
, int irq_num
)
1161 /* Check that out properly ... */
1165 static void pnv_phb4_set_irq(void *opaque
, int irq_num
, int level
)
1167 PnvPHB4
*phb
= PNV_PHB4(opaque
);
1172 phb_error(phb
, "IRQ %x is not an LSI", irq_num
);
1174 lsi_base
= GETFIELD(PHB_LSI_SRC_ID
, phb
->regs
[PHB_LSI_SOURCE_ID
>> 3]);
1176 qemu_set_irq(phb
->qirqs
[lsi_base
+ irq_num
], level
);
1179 static bool pnv_phb4_resolve_pe(PnvPhb4DMASpace
*ds
)
1186 /* Already resolved ? */
1187 if (ds
->pe_num
!= PHB_INVALID_PE
) {
1191 /* We need to lookup the RTT */
1192 rtt
= ds
->phb
->regs
[PHB_RTT_BAR
>> 3];
1193 if (!(rtt
& PHB_RTT_BAR_ENABLE
)) {
1194 phb_error(ds
->phb
, "DMA with RTT BAR disabled !");
1195 /* Set error bits ? fence ? ... */
1200 bus_num
= pci_bus_num(ds
->bus
);
1201 addr
= rtt
& PHB_RTT_BASE_ADDRESS_MASK
;
1202 addr
+= 2 * PCI_BUILD_BDF(bus_num
, ds
->devfn
);
1203 if (dma_memory_read(&address_space_memory
, addr
, &rte
,
1204 sizeof(rte
), MEMTXATTRS_UNSPECIFIED
)) {
1205 phb_error(ds
->phb
, "Failed to read RTT entry at 0x%"PRIx64
, addr
);
1206 /* Set error bits ? fence ? ... */
1209 rte
= be16_to_cpu(rte
);
1211 /* Fail upon reading of invalid PE# */
1212 num_PEs
= ds
->phb
->big_phb
? PNV_PHB4_MAX_PEs
: (PNV_PHB4_MAX_PEs
>> 1);
1213 if (rte
>= num_PEs
) {
1214 phb_error(ds
->phb
, "RTE for RID 0x%x invalid (%04x", ds
->devfn
, rte
);
1221 static void pnv_phb4_translate_tve(PnvPhb4DMASpace
*ds
, hwaddr addr
,
1222 bool is_write
, uint64_t tve
,
1225 uint64_t tta
= GETFIELD(IODA3_TVT_TABLE_ADDR
, tve
);
1226 int32_t lev
= GETFIELD(IODA3_TVT_NUM_LEVELS
, tve
);
1227 uint32_t tts
= GETFIELD(IODA3_TVT_TCE_TABLE_SIZE
, tve
);
1228 uint32_t tps
= GETFIELD(IODA3_TVT_IO_PSIZE
, tve
);
1230 /* Invalid levels */
1232 phb_error(ds
->phb
, "Invalid #levels in TVE %d", lev
);
1238 phb_error(ds
->phb
, "Access to invalid TVE");
1242 /* IO Page Size of 0 means untranslated, else use TCEs */
1244 /* TODO: Handle boundaries */
1246 /* Use 4k pages like q35 ... for now */
1247 tlb
->iova
= addr
& 0xfffffffffffff000ull
;
1248 tlb
->translated_addr
= addr
& 0x0003fffffffff000ull
;
1249 tlb
->addr_mask
= 0xfffull
;
1250 tlb
->perm
= IOMMU_RW
;
1252 uint32_t tce_shift
, tbl_shift
, sh
;
1253 uint64_t base
, taddr
, tce
, tce_mask
;
1255 /* Address bits per bottom level TCE entry */
1256 tce_shift
= tps
+ 11;
1258 /* Address bits per table level */
1259 tbl_shift
= tts
+ 8;
1261 /* Top level table base address */
1264 /* Total shift to first level */
1265 sh
= tbl_shift
* lev
+ tce_shift
;
1267 /* TODO: Limit to support IO page sizes */
1269 /* TODO: Multi-level untested */
1273 /* Grab the TCE address */
1274 taddr
= base
| (((addr
>> sh
) & ((1ul << tbl_shift
) - 1)) << 3);
1275 if (dma_memory_read(&address_space_memory
, taddr
, &tce
,
1276 sizeof(tce
), MEMTXATTRS_UNSPECIFIED
)) {
1277 phb_error(ds
->phb
, "Failed to read TCE at 0x%"PRIx64
, taddr
);
1280 tce
= be64_to_cpu(tce
);
1282 /* Check permission for indirect TCE */
1283 if ((lev
>= 0) && !(tce
& 3)) {
1284 phb_error(ds
->phb
, "Invalid indirect TCE at 0x%"PRIx64
, taddr
);
1285 phb_error(ds
->phb
, " xlate %"PRIx64
":%c TVE=%"PRIx64
, addr
,
1286 is_write
? 'W' : 'R', tve
);
1287 phb_error(ds
->phb
, " tta=%"PRIx64
" lev=%d tts=%d tps=%d",
1288 tta
, lev
, tts
, tps
);
1292 base
= tce
& ~0xfffull
;
1295 /* We exit the loop with TCE being the final TCE */
1296 if ((is_write
& !(tce
& 2)) || ((!is_write
) && !(tce
& 1))) {
1297 phb_error(ds
->phb
, "TCE access fault at 0x%"PRIx64
, taddr
);
1298 phb_error(ds
->phb
, " xlate %"PRIx64
":%c TVE=%"PRIx64
, addr
,
1299 is_write
? 'W' : 'R', tve
);
1300 phb_error(ds
->phb
, " tta=%"PRIx64
" lev=%d tts=%d tps=%d",
1301 tta
, lev
, tts
, tps
);
1304 tce_mask
= ~((1ull << tce_shift
) - 1);
1305 tlb
->iova
= addr
& tce_mask
;
1306 tlb
->translated_addr
= tce
& tce_mask
;
1307 tlb
->addr_mask
= ~tce_mask
;
1308 tlb
->perm
= tce
& 3;
1312 static IOMMUTLBEntry
pnv_phb4_translate_iommu(IOMMUMemoryRegion
*iommu
,
1314 IOMMUAccessFlags flag
,
1317 PnvPhb4DMASpace
*ds
= container_of(iommu
, PnvPhb4DMASpace
, dma_mr
);
1320 IOMMUTLBEntry ret
= {
1321 .target_as
= &address_space_memory
,
1323 .translated_addr
= 0,
1324 .addr_mask
= ~(hwaddr
)0,
1329 if (!pnv_phb4_resolve_pe(ds
)) {
1330 phb_error(ds
->phb
, "Failed to resolve PE# for bus @%p (%d) devfn 0x%x",
1331 ds
->bus
, pci_bus_num(ds
->bus
), ds
->devfn
);
1335 /* Check top bits */
1336 switch (addr
>> 60) {
1338 /* DMA or 32-bit MSI ? */
1339 cfg
= ds
->phb
->regs
[PHB_PHB4_CONFIG
>> 3];
1340 if ((cfg
& PHB_PHB4C_32BIT_MSI_EN
) &&
1341 ((addr
& 0xffffffffffff0000ull
) == 0xffff0000ull
)) {
1342 phb_error(ds
->phb
, "xlate on 32-bit MSI region");
1345 /* Choose TVE XXX Use PHB4 Control Register */
1346 tve_sel
= (addr
>> 59) & 1;
1347 tve
= ds
->phb
->ioda_TVT
[ds
->pe_num
* 2 + tve_sel
];
1348 pnv_phb4_translate_tve(ds
, addr
, flag
& IOMMU_WO
, tve
, &ret
);
1351 phb_error(ds
->phb
, "xlate on 64-bit MSI region");
1354 phb_error(ds
->phb
, "xlate on unsupported address 0x%"PRIx64
, addr
);
1359 #define TYPE_PNV_PHB4_IOMMU_MEMORY_REGION "pnv-phb4-iommu-memory-region"
1360 DECLARE_INSTANCE_CHECKER(IOMMUMemoryRegion
, PNV_PHB4_IOMMU_MEMORY_REGION
,
1361 TYPE_PNV_PHB4_IOMMU_MEMORY_REGION
)
1363 static void pnv_phb4_iommu_memory_region_class_init(ObjectClass
*klass
,
1366 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_CLASS(klass
);
1368 imrc
->translate
= pnv_phb4_translate_iommu
;
1371 static const TypeInfo pnv_phb4_iommu_memory_region_info
= {
1372 .parent
= TYPE_IOMMU_MEMORY_REGION
,
1373 .name
= TYPE_PNV_PHB4_IOMMU_MEMORY_REGION
,
1374 .class_init
= pnv_phb4_iommu_memory_region_class_init
,
1378 * Return the index/phb-id of a PHB4 that belongs to a
1379 * pec->stacks[stack_index] stack.
1381 int pnv_phb4_pec_get_phb_id(PnvPhb4PecState
*pec
, int stack_index
)
1383 PnvPhb4PecClass
*pecc
= PNV_PHB4_PEC_GET_CLASS(pec
);
1384 int index
= pec
->index
;
1388 offset
+= pecc
->num_phbs
[index
];
1391 return offset
+ stack_index
;
1395 * MSI/MSIX memory region implementation.
1396 * The handler handles both MSI and MSIX.
1398 static void pnv_phb4_msi_write(void *opaque
, hwaddr addr
,
1399 uint64_t data
, unsigned size
)
1401 PnvPhb4DMASpace
*ds
= opaque
;
1402 PnvPHB4
*phb
= ds
->phb
;
1404 uint32_t src
= ((addr
>> 4) & 0xffff) | (data
& 0x1f);
1407 if (!pnv_phb4_resolve_pe(ds
)) {
1408 phb_error(phb
, "Failed to resolve PE# for bus @%p (%d) devfn 0x%x",
1409 ds
->bus
, pci_bus_num(ds
->bus
), ds
->devfn
);
1413 /* TODO: Check it doesn't collide with LSIs */
1414 if (src
>= phb
->xsrc
.nr_irqs
) {
1415 phb_error(phb
, "MSI %d out of bounds", src
);
1419 /* TODO: check PE/MSI assignement */
1421 qemu_irq_pulse(phb
->qirqs
[src
]);
1424 /* There is no .read as the read result is undefined by PCI spec */
1425 static uint64_t pnv_phb4_msi_read(void *opaque
, hwaddr addr
, unsigned size
)
1427 PnvPhb4DMASpace
*ds
= opaque
;
1429 phb_error(ds
->phb
, "Invalid MSI read @ 0x%" HWADDR_PRIx
, addr
);
1433 static const MemoryRegionOps pnv_phb4_msi_ops
= {
1434 .read
= pnv_phb4_msi_read
,
1435 .write
= pnv_phb4_msi_write
,
1436 .endianness
= DEVICE_LITTLE_ENDIAN
1439 static PnvPhb4DMASpace
*pnv_phb4_dma_find(PnvPHB4
*phb
, PCIBus
*bus
, int devfn
)
1441 PnvPhb4DMASpace
*ds
;
1443 QLIST_FOREACH(ds
, &phb
->dma_spaces
, list
) {
1444 if (ds
->bus
== bus
&& ds
->devfn
== devfn
) {
1451 static AddressSpace
*pnv_phb4_dma_iommu(PCIBus
*bus
, void *opaque
, int devfn
)
1453 PnvPHB4
*phb
= opaque
;
1454 PnvPhb4DMASpace
*ds
;
1457 ds
= pnv_phb4_dma_find(phb
, bus
, devfn
);
1460 ds
= g_malloc0(sizeof(PnvPhb4DMASpace
));
1463 ds
->pe_num
= PHB_INVALID_PE
;
1465 snprintf(name
, sizeof(name
), "phb4-%d.%d-iommu", phb
->chip_id
,
1467 memory_region_init_iommu(&ds
->dma_mr
, sizeof(ds
->dma_mr
),
1468 TYPE_PNV_PHB4_IOMMU_MEMORY_REGION
,
1469 OBJECT(phb
), name
, UINT64_MAX
);
1470 address_space_init(&ds
->dma_as
, MEMORY_REGION(&ds
->dma_mr
),
1472 memory_region_init_io(&ds
->msi32_mr
, OBJECT(phb
), &pnv_phb4_msi_ops
,
1473 ds
, "msi32", 0x10000);
1474 memory_region_init_io(&ds
->msi64_mr
, OBJECT(phb
), &pnv_phb4_msi_ops
,
1475 ds
, "msi64", 0x100000);
1476 pnv_phb4_update_msi_regions(ds
);
1478 QLIST_INSERT_HEAD(&phb
->dma_spaces
, ds
, list
);
1483 static void pnv_phb4_xscom_realize(PnvPHB4
*phb
)
1485 PnvPhb4PecState
*pec
= phb
->pec
;
1486 PnvPhb4PecClass
*pecc
= PNV_PHB4_PEC_GET_CLASS(pec
);
1487 int stack_no
= pnv_phb4_get_phb_stack_no(phb
);
1488 uint32_t pec_nest_base
;
1489 uint32_t pec_pci_base
;
1494 /* Initialize the XSCOM regions for the stack registers */
1495 snprintf(name
, sizeof(name
), "xscom-pec-%d.%d-nest-phb-%d",
1496 pec
->chip_id
, pec
->index
, stack_no
);
1497 pnv_xscom_region_init(&phb
->nest_regs_mr
, OBJECT(phb
),
1498 &pnv_pec_stk_nest_xscom_ops
, phb
, name
,
1499 PHB4_PEC_NEST_STK_REGS_COUNT
);
1501 snprintf(name
, sizeof(name
), "xscom-pec-%d.%d-pci-phb-%d",
1502 pec
->chip_id
, pec
->index
, stack_no
);
1503 pnv_xscom_region_init(&phb
->pci_regs_mr
, OBJECT(phb
),
1504 &pnv_pec_stk_pci_xscom_ops
, phb
, name
,
1505 PHB4_PEC_PCI_STK_REGS_COUNT
);
1507 /* PHB pass-through */
1508 snprintf(name
, sizeof(name
), "xscom-pec-%d.%d-pci-phb-%d",
1509 pec
->chip_id
, pec
->index
, stack_no
);
1510 pnv_xscom_region_init(&phb
->phb_regs_mr
, OBJECT(phb
),
1511 &pnv_phb4_xscom_ops
, phb
, name
, 0x40);
1513 pec_nest_base
= pecc
->xscom_nest_base(pec
);
1514 pec_pci_base
= pecc
->xscom_pci_base(pec
);
1516 /* Populate the XSCOM address space. */
1517 pnv_xscom_add_subregion(pec
->chip
,
1518 pec_nest_base
+ 0x40 * (stack_no
+ 1),
1519 &phb
->nest_regs_mr
);
1520 pnv_xscom_add_subregion(pec
->chip
,
1521 pec_pci_base
+ 0x40 * (stack_no
+ 1),
1523 pnv_xscom_add_subregion(pec
->chip
,
1524 pec_pci_base
+ PNV9_XSCOM_PEC_PCI_STK0
+
1529 static void pnv_phb4_instance_init(Object
*obj
)
1531 PnvPHB4
*phb
= PNV_PHB4(obj
);
1533 QLIST_INIT(&phb
->dma_spaces
);
1535 /* XIVE interrupt source object */
1536 object_initialize_child(obj
, "source", &phb
->xsrc
, TYPE_XIVE_SOURCE
);
1539 static PnvPhb4PecState
*pnv_phb4_get_pec(PnvChip
*chip
, PnvPHB4
*phb
,
1542 Pnv9Chip
*chip9
= PNV9_CHIP(chip
);
1543 int chip_id
= phb
->chip_id
;
1544 int index
= phb
->phb_id
;
1547 for (i
= 0; i
< chip
->num_pecs
; i
++) {
1549 * For each PEC, check the amount of phbs it supports
1550 * and see if the given phb4 index matches an index.
1552 PnvPhb4PecState
*pec
= &chip9
->pecs
[i
];
1554 for (j
= 0; j
< pec
->num_phbs
; j
++) {
1555 if (index
== pnv_phb4_pec_get_phb_id(pec
, j
)) {
1562 "pnv-phb4 chip-id %d index %d didn't match any existing PEC",
1568 static void pnv_phb4_realize(DeviceState
*dev
, Error
**errp
)
1570 PnvPHB4
*phb
= PNV_PHB4(dev
);
1571 PnvMachineState
*pnv
= PNV_MACHINE(qdev_get_machine());
1572 PnvChip
*chip
= pnv_get_chip(pnv
, phb
->chip_id
);
1573 PCIHostState
*pci
= PCI_HOST_BRIDGE(dev
);
1574 XiveSource
*xsrc
= &phb
->xsrc
;
1576 Error
*local_err
= NULL
;
1581 error_setg(errp
, "invalid chip id: %d", phb
->chip_id
);
1585 /* User created PHBs need to be assigned to a PEC */
1587 phb
->pec
= pnv_phb4_get_pec(chip
, phb
, &local_err
);
1589 error_propagate(errp
, local_err
);
1594 /* Reparent the PHB to the chip to build the device tree */
1595 pnv_chip_parent_fixup(chip
, OBJECT(phb
), phb
->phb_id
);
1597 s
= qdev_get_parent_bus(DEVICE(chip
));
1598 if (!qdev_set_parent_bus(DEVICE(phb
), s
, &local_err
)) {
1599 error_propagate(errp
, local_err
);
1603 /* Set the "big_phb" flag */
1604 phb
->big_phb
= phb
->phb_id
== 0 || phb
->phb_id
== 3;
1606 /* Controller Registers */
1607 snprintf(name
, sizeof(name
), "phb4-%d.%d-regs", phb
->chip_id
,
1609 memory_region_init_io(&phb
->mr_regs
, OBJECT(phb
), &pnv_phb4_reg_ops
, phb
,
1613 * PHB4 doesn't support IO space. However, qemu gets very upset if
1614 * we don't have an IO region to anchor IO BARs onto so we just
1615 * initialize one which we never hook up to anything
1618 snprintf(name
, sizeof(name
), "phb4-%d.%d-pci-io", phb
->chip_id
,
1620 memory_region_init(&phb
->pci_io
, OBJECT(phb
), name
, 0x10000);
1622 snprintf(name
, sizeof(name
), "phb4-%d.%d-pci-mmio", phb
->chip_id
,
1624 memory_region_init(&phb
->pci_mmio
, OBJECT(phb
), name
,
1625 PCI_MMIO_TOTAL_SIZE
);
1627 pci
->bus
= pci_register_root_bus(dev
, dev
->id
,
1628 pnv_phb4_set_irq
, pnv_phb4_map_irq
, phb
,
1629 &phb
->pci_mmio
, &phb
->pci_io
,
1630 0, 4, TYPE_PNV_PHB4_ROOT_BUS
);
1631 pci_setup_iommu(pci
->bus
, pnv_phb4_dma_iommu
, phb
);
1632 pci
->bus
->flags
|= PCI_BUS_EXTENDED_CONFIG_SPACE
;
1634 /* Setup XIVE Source */
1636 nr_irqs
= PNV_PHB4_MAX_INTs
;
1638 nr_irqs
= PNV_PHB4_MAX_INTs
>> 1;
1640 object_property_set_int(OBJECT(xsrc
), "nr-irqs", nr_irqs
, &error_fatal
);
1641 object_property_set_link(OBJECT(xsrc
), "xive", OBJECT(phb
), &error_fatal
);
1642 if (!qdev_realize(DEVICE(xsrc
), NULL
, errp
)) {
1646 pnv_phb4_update_xsrc(phb
);
1648 phb
->qirqs
= qemu_allocate_irqs(xive_source_set_irq
, xsrc
, xsrc
->nr_irqs
);
1650 pnv_phb4_xscom_realize(phb
);
1653 static const char *pnv_phb4_root_bus_path(PCIHostState
*host_bridge
,
1656 PnvPHB4
*phb
= PNV_PHB4(host_bridge
);
1658 snprintf(phb
->bus_path
, sizeof(phb
->bus_path
), "00%02x:%02x",
1659 phb
->chip_id
, phb
->phb_id
);
1660 return phb
->bus_path
;
1663 static void pnv_phb4_xive_notify(XiveNotifier
*xf
, uint32_t srcno
)
1665 PnvPHB4
*phb
= PNV_PHB4(xf
);
1666 uint64_t notif_port
= phb
->regs
[PHB_INT_NOTIFY_ADDR
>> 3];
1667 uint32_t offset
= phb
->regs
[PHB_INT_NOTIFY_INDEX
>> 3];
1668 uint64_t data
= XIVE_TRIGGER_PQ
| offset
| srcno
;
1671 trace_pnv_phb4_xive_notify(notif_port
, data
);
1673 address_space_stq_be(&address_space_memory
, notif_port
, data
,
1674 MEMTXATTRS_UNSPECIFIED
, &result
);
1675 if (result
!= MEMTX_OK
) {
1676 phb_error(phb
, "trigger failed @%"HWADDR_PRIx
"\n", notif_port
);
1681 static Property pnv_phb4_properties
[] = {
1682 DEFINE_PROP_UINT32("index", PnvPHB4
, phb_id
, 0),
1683 DEFINE_PROP_UINT32("chip-id", PnvPHB4
, chip_id
, 0),
1684 DEFINE_PROP_LINK("pec", PnvPHB4
, pec
, TYPE_PNV_PHB4_PEC
,
1686 DEFINE_PROP_END_OF_LIST(),
1689 static void pnv_phb4_class_init(ObjectClass
*klass
, void *data
)
1691 PCIHostBridgeClass
*hc
= PCI_HOST_BRIDGE_CLASS(klass
);
1692 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1693 XiveNotifierClass
*xfc
= XIVE_NOTIFIER_CLASS(klass
);
1695 hc
->root_bus_path
= pnv_phb4_root_bus_path
;
1696 dc
->realize
= pnv_phb4_realize
;
1697 device_class_set_props(dc
, pnv_phb4_properties
);
1698 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
1699 dc
->user_creatable
= true;
1701 xfc
->notify
= pnv_phb4_xive_notify
;
1704 static const TypeInfo pnv_phb4_type_info
= {
1705 .name
= TYPE_PNV_PHB4
,
1706 .parent
= TYPE_PCIE_HOST_BRIDGE
,
1707 .instance_init
= pnv_phb4_instance_init
,
1708 .instance_size
= sizeof(PnvPHB4
),
1709 .class_init
= pnv_phb4_class_init
,
1710 .interfaces
= (InterfaceInfo
[]) {
1711 { TYPE_XIVE_NOTIFIER
},
1716 static void pnv_phb4_root_bus_class_init(ObjectClass
*klass
, void *data
)
1718 BusClass
*k
= BUS_CLASS(klass
);
1721 * PHB4 has only a single root complex. Enforce the limit on the
1727 static const TypeInfo pnv_phb4_root_bus_info
= {
1728 .name
= TYPE_PNV_PHB4_ROOT_BUS
,
1729 .parent
= TYPE_PCIE_BUS
,
1730 .class_init
= pnv_phb4_root_bus_class_init
,
1731 .interfaces
= (InterfaceInfo
[]) {
1732 { INTERFACE_PCIE_DEVICE
},
1737 static void pnv_phb4_root_port_reset(DeviceState
*dev
)
1739 PCIERootPortClass
*rpc
= PCIE_ROOT_PORT_GET_CLASS(dev
);
1740 PCIDevice
*d
= PCI_DEVICE(dev
);
1741 uint8_t *conf
= d
->config
;
1743 rpc
->parent_reset(dev
);
1745 pci_byte_test_and_set_mask(conf
+ PCI_IO_BASE
,
1746 PCI_IO_RANGE_MASK
& 0xff);
1747 pci_byte_test_and_clear_mask(conf
+ PCI_IO_LIMIT
,
1748 PCI_IO_RANGE_MASK
& 0xff);
1749 pci_set_word(conf
+ PCI_MEMORY_BASE
, 0);
1750 pci_set_word(conf
+ PCI_MEMORY_LIMIT
, 0xfff0);
1751 pci_set_word(conf
+ PCI_PREF_MEMORY_BASE
, 0x1);
1752 pci_set_word(conf
+ PCI_PREF_MEMORY_LIMIT
, 0xfff1);
1753 pci_set_long(conf
+ PCI_PREF_BASE_UPPER32
, 0x1); /* Hack */
1754 pci_set_long(conf
+ PCI_PREF_LIMIT_UPPER32
, 0xffffffff);
1757 static void pnv_phb4_root_port_realize(DeviceState
*dev
, Error
**errp
)
1759 PCIERootPortClass
*rpc
= PCIE_ROOT_PORT_GET_CLASS(dev
);
1760 PCIDevice
*pci
= PCI_DEVICE(dev
);
1761 PCIBus
*bus
= pci_get_bus(pci
);
1762 PnvPHB4
*phb
= NULL
;
1763 Error
*local_err
= NULL
;
1765 phb
= (PnvPHB4
*) object_dynamic_cast(OBJECT(bus
->qbus
.parent
),
1769 error_setg(errp
, "%s must be connected to pnv-phb4 buses", dev
->id
);
1773 /* Set unique chassis/slot values for the root port */
1774 qdev_prop_set_uint8(&pci
->qdev
, "chassis", phb
->chip_id
);
1775 qdev_prop_set_uint16(&pci
->qdev
, "slot", phb
->phb_id
);
1777 rpc
->parent_realize(dev
, &local_err
);
1779 error_propagate(errp
, local_err
);
1784 static void pnv_phb4_root_port_class_init(ObjectClass
*klass
, void *data
)
1786 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1787 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1788 PCIERootPortClass
*rpc
= PCIE_ROOT_PORT_CLASS(klass
);
1790 dc
->desc
= "IBM PHB4 PCIE Root Port";
1791 dc
->user_creatable
= true;
1793 device_class_set_parent_realize(dc
, pnv_phb4_root_port_realize
,
1794 &rpc
->parent_realize
);
1795 device_class_set_parent_reset(dc
, pnv_phb4_root_port_reset
,
1796 &rpc
->parent_reset
);
1798 k
->vendor_id
= PCI_VENDOR_ID_IBM
;
1799 k
->device_id
= PNV_PHB4_DEVICE_ID
;
1802 rpc
->exp_offset
= 0x48;
1803 rpc
->aer_offset
= 0x100;
1805 dc
->reset
= &pnv_phb4_root_port_reset
;
1808 static const TypeInfo pnv_phb4_root_port_info
= {
1809 .name
= TYPE_PNV_PHB4_ROOT_PORT
,
1810 .parent
= TYPE_PCIE_ROOT_PORT
,
1811 .instance_size
= sizeof(PnvPHB4RootPort
),
1812 .class_init
= pnv_phb4_root_port_class_init
,
1815 static void pnv_phb5_root_port_class_init(ObjectClass
*klass
, void *data
)
1817 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1818 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1820 dc
->desc
= "IBM PHB5 PCIE Root Port";
1821 dc
->user_creatable
= true;
1823 k
->vendor_id
= PCI_VENDOR_ID_IBM
;
1824 k
->device_id
= PNV_PHB5_DEVICE_ID
;
1827 static const TypeInfo pnv_phb5_root_port_info
= {
1828 .name
= TYPE_PNV_PHB5_ROOT_PORT
,
1829 .parent
= TYPE_PNV_PHB4_ROOT_PORT
,
1830 .instance_size
= sizeof(PnvPHB4RootPort
),
1831 .class_init
= pnv_phb5_root_port_class_init
,
1834 static void pnv_phb4_register_types(void)
1836 type_register_static(&pnv_phb4_root_bus_info
);
1837 type_register_static(&pnv_phb5_root_port_info
);
1838 type_register_static(&pnv_phb4_root_port_info
);
1839 type_register_static(&pnv_phb4_type_info
);
1840 type_register_static(&pnv_phb4_iommu_memory_region_info
);
1843 type_init(pnv_phb4_register_types
);
1845 void pnv_phb4_pic_print_info(PnvPHB4
*phb
, Monitor
*mon
)
1847 uint32_t offset
= phb
->regs
[PHB_INT_NOTIFY_INDEX
>> 3];
1849 monitor_printf(mon
, "PHB4[%x:%x] Source %08x .. %08x\n",
1850 phb
->chip_id
, phb
->phb_id
,
1851 offset
, offset
+ phb
->xsrc
.nr_irqs
- 1);
1852 xive_source_pic_print_info(&phb
->xsrc
, 0, mon
);