2 * QEMU PowerPC PowerNV (POWER8) PHB3 model
4 * Copyright (c) 2014-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 "hw/pci-host/pnv_phb3_regs.h"
14 #include "hw/pci-host/pnv_phb.h"
15 #include "hw/pci-host/pnv_phb3.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_chip.h"
21 #include "hw/qdev-properties.h"
22 #include "qom/object.h"
23 #include "sysemu/sysemu.h"
25 #define phb3_error(phb, fmt, ...) \
26 qemu_log_mask(LOG_GUEST_ERROR, "phb3[%d:%d]: " fmt "\n", \
27 (phb)->chip_id, (phb)->phb_id, ## __VA_ARGS__)
29 static PCIDevice
*pnv_phb3_find_cfg_dev(PnvPHB3
*phb
)
31 PCIHostState
*pci
= PCI_HOST_BRIDGE(phb
->phb_base
);
32 uint64_t addr
= phb
->regs
[PHB_CONFIG_ADDRESS
>> 3];
38 bus
= (addr
>> 52) & 0xff;
39 devfn
= (addr
>> 44) & 0xff;
41 return pci_find_device(pci
->bus
, bus
, devfn
);
45 * The CONFIG_DATA register expects little endian accesses, but as the
46 * region is big endian, we have to swap the value.
48 static void pnv_phb3_config_write(PnvPHB3
*phb
, unsigned off
,
49 unsigned size
, uint64_t val
)
51 uint32_t cfg_addr
, limit
;
54 pdev
= pnv_phb3_find_cfg_dev(phb
);
58 cfg_addr
= (phb
->regs
[PHB_CONFIG_ADDRESS
>> 3] >> 32) & 0xffc;
60 limit
= pci_config_size(pdev
);
61 if (limit
<= cfg_addr
) {
63 * conventional pci device can be behind pcie-to-pci bridge.
64 * 256 <= addr < 4K has no effects.
78 g_assert_not_reached();
80 pci_host_config_write_common(pdev
, cfg_addr
, limit
, val
, size
);
83 static uint64_t pnv_phb3_config_read(PnvPHB3
*phb
, unsigned off
,
86 uint32_t cfg_addr
, limit
;
90 pdev
= pnv_phb3_find_cfg_dev(phb
);
94 cfg_addr
= (phb
->regs
[PHB_CONFIG_ADDRESS
>> 3] >> 32) & 0xffc;
96 limit
= pci_config_size(pdev
);
97 if (limit
<= cfg_addr
) {
99 * conventional pci device can be behind pcie-to-pci bridge.
100 * 256 <= addr < 4K has no effects.
104 val
= pci_host_config_read_common(pdev
, cfg_addr
, limit
, size
);
113 g_assert_not_reached();
117 static void pnv_phb3_check_m32(PnvPHB3
*phb
)
119 uint64_t base
, start
, size
;
120 MemoryRegion
*parent
;
121 PnvPBCQState
*pbcq
= &phb
->pbcq
;
123 if (memory_region_is_mapped(&phb
->mr_m32
)) {
124 memory_region_del_subregion(phb
->mr_m32
.container
, &phb
->mr_m32
);
127 if (!(phb
->regs
[PHB_PHB3_CONFIG
>> 3] & PHB_PHB3C_M32_EN
)) {
131 /* Grab geometry from registers */
132 base
= phb
->regs
[PHB_M32_BASE_ADDR
>> 3];
133 start
= phb
->regs
[PHB_M32_START_ADDR
>> 3];
134 size
= ~(phb
->regs
[PHB_M32_BASE_MASK
>> 3] | 0xfffc000000000000ull
) + 1;
136 /* Check if it matches an enabled MMIO region in the PBCQ */
137 if (memory_region_is_mapped(&pbcq
->mmbar0
) &&
138 base
>= pbcq
->mmio0_base
&&
139 (base
+ size
) <= (pbcq
->mmio0_base
+ pbcq
->mmio0_size
)) {
140 parent
= &pbcq
->mmbar0
;
141 base
-= pbcq
->mmio0_base
;
142 } else if (memory_region_is_mapped(&pbcq
->mmbar1
) &&
143 base
>= pbcq
->mmio1_base
&&
144 (base
+ size
) <= (pbcq
->mmio1_base
+ pbcq
->mmio1_size
)) {
145 parent
= &pbcq
->mmbar1
;
146 base
-= pbcq
->mmio1_base
;
152 memory_region_init_alias(&phb
->mr_m32
, OBJECT(phb
), "phb3-m32",
153 &phb
->pci_mmio
, start
, size
);
154 memory_region_add_subregion(parent
, base
, &phb
->mr_m32
);
157 static void pnv_phb3_check_m64(PnvPHB3
*phb
, uint32_t index
)
159 uint64_t base
, start
, size
, m64
;
160 MemoryRegion
*parent
;
161 PnvPBCQState
*pbcq
= &phb
->pbcq
;
163 if (memory_region_is_mapped(&phb
->mr_m64
[index
])) {
164 /* Should we destroy it in RCU friendly way... ? */
165 memory_region_del_subregion(phb
->mr_m64
[index
].container
,
166 &phb
->mr_m64
[index
]);
169 /* Get table entry */
170 m64
= phb
->ioda_M64BT
[index
];
172 if (!(m64
& IODA2_M64BT_ENABLE
)) {
176 /* Grab geometry from registers */
177 base
= GETFIELD(IODA2_M64BT_BASE
, m64
) << 20;
178 if (m64
& IODA2_M64BT_SINGLE_PE
) {
179 base
&= ~0x1ffffffull
;
181 size
= GETFIELD(IODA2_M64BT_MASK
, m64
) << 20;
182 size
|= 0xfffc000000000000ull
;
184 start
= base
| (phb
->regs
[PHB_M64_UPPER_BITS
>> 3]);
186 /* Check if it matches an enabled MMIO region in the PBCQ */
187 if (memory_region_is_mapped(&pbcq
->mmbar0
) &&
188 base
>= pbcq
->mmio0_base
&&
189 (base
+ size
) <= (pbcq
->mmio0_base
+ pbcq
->mmio0_size
)) {
190 parent
= &pbcq
->mmbar0
;
191 base
-= pbcq
->mmio0_base
;
192 } else if (memory_region_is_mapped(&pbcq
->mmbar1
) &&
193 base
>= pbcq
->mmio1_base
&&
194 (base
+ size
) <= (pbcq
->mmio1_base
+ pbcq
->mmio1_size
)) {
195 parent
= &pbcq
->mmbar1
;
196 base
-= pbcq
->mmio1_base
;
202 memory_region_init_alias(&phb
->mr_m64
[index
], OBJECT(phb
), "phb3-m64",
203 &phb
->pci_mmio
, start
, size
);
204 memory_region_add_subregion(parent
, base
, &phb
->mr_m64
[index
]);
207 static void pnv_phb3_check_all_m64s(PnvPHB3
*phb
)
211 for (i
= 0; i
< PNV_PHB3_NUM_M64
; i
++) {
212 pnv_phb3_check_m64(phb
, i
);
216 static void pnv_phb3_lxivt_write(PnvPHB3
*phb
, unsigned idx
, uint64_t val
)
218 uint8_t server
, prio
;
220 phb
->ioda_LXIVT
[idx
] = val
& (IODA2_LXIVT_SERVER
|
221 IODA2_LXIVT_PRIORITY
|
222 IODA2_LXIVT_NODE_ID
);
223 server
= GETFIELD(IODA2_LXIVT_SERVER
, val
);
224 prio
= GETFIELD(IODA2_LXIVT_PRIORITY
, val
);
227 * The low order 2 bits are the link pointer (Type II interrupts).
228 * Shift back to get a valid IRQ server.
232 ics_write_xive(&phb
->lsis
, idx
, server
, prio
, prio
);
235 static uint64_t *pnv_phb3_ioda_access(PnvPHB3
*phb
,
236 unsigned *out_table
, unsigned *out_idx
)
238 uint64_t adreg
= phb
->regs
[PHB_IODA_ADDR
>> 3];
239 unsigned int index
= GETFIELD(PHB_IODA_AD_TADR
, adreg
);
240 unsigned int table
= GETFIELD(PHB_IODA_AD_TSEL
, adreg
);
242 uint64_t *tptr
= NULL
;
246 tptr
= phb
->ioda_LIST
;
249 case IODA2_TBL_LXIVT
:
250 tptr
= phb
->ioda_LXIVT
;
253 case IODA2_TBL_IVC_CAM
:
263 case IODA2_TBL_PESTA
:
264 case IODA2_TBL_PESTB
:
268 tptr
= phb
->ioda_TVT
;
275 case IODA2_TBL_M64BT
:
276 tptr
= phb
->ioda_M64BT
;
279 case IODA2_TBL_M32DT
:
280 tptr
= phb
->ioda_MDT
;
284 tptr
= phb
->ioda_PEEV
;
288 phb3_error(phb
, "invalid IODA table %d", table
);
301 if (adreg
& PHB_IODA_AD_AUTOINC
) {
302 index
= (index
+ 1) & mask
;
303 adreg
= SETFIELD(PHB_IODA_AD_TADR
, adreg
, index
);
305 phb
->regs
[PHB_IODA_ADDR
>> 3] = adreg
;
309 static uint64_t pnv_phb3_ioda_read(PnvPHB3
*phb
)
314 tptr
= pnv_phb3_ioda_access(phb
, &table
, NULL
);
316 /* Return 0 on unsupported tables, not ff's */
322 static void pnv_phb3_ioda_write(PnvPHB3
*phb
, uint64_t val
)
327 tptr
= pnv_phb3_ioda_access(phb
, &table
, &idx
);
332 /* Handle side effects */
334 case IODA2_TBL_LXIVT
:
335 pnv_phb3_lxivt_write(phb
, idx
, val
);
337 case IODA2_TBL_M64BT
:
339 pnv_phb3_check_m64(phb
, idx
);
347 * This is called whenever the PHB LSI, MSI source ID register or
348 * the PBCQ irq filters are written.
350 void pnv_phb3_remap_irqs(PnvPHB3
*phb
)
352 ICSState
*ics
= &phb
->lsis
;
353 uint32_t local
, global
, count
, mask
, comp
;
355 PnvPBCQState
*pbcq
= &phb
->pbcq
;
358 * First check if we are enabled. Unlike real HW we don't separate
359 * TX and RX so we enable if both are set
361 baren
= pbcq
->nest_regs
[PBCQ_NEST_BAR_EN
];
362 if (!(baren
& PBCQ_NEST_BAR_EN_IRSN_RX
) ||
363 !(baren
& PBCQ_NEST_BAR_EN_IRSN_TX
)) {
368 /* Grab local LSI source ID */
369 local
= GETFIELD(PHB_LSI_SRC_ID
, phb
->regs
[PHB_LSI_SOURCE_ID
>> 3]) << 3;
371 /* Grab global one and compare */
372 global
= GETFIELD(PBCQ_NEST_LSI_SRC
,
373 pbcq
->nest_regs
[PBCQ_NEST_LSI_SRC_ID
]) << 3;
374 if (global
!= local
) {
376 * This happens during initialization, let's come back when we
377 * are properly configured
383 /* Get the base on the powerbus */
384 comp
= GETFIELD(PBCQ_NEST_IRSN_COMP
,
385 pbcq
->nest_regs
[PBCQ_NEST_IRSN_COMPARE
]);
386 mask
= GETFIELD(PBCQ_NEST_IRSN_COMP
,
387 pbcq
->nest_regs
[PBCQ_NEST_IRSN_MASK
]);
388 count
= ((~mask
) + 1) & 0x7ffff;
389 phb
->total_irq
= count
;
392 if ((global
+ PNV_PHB3_NUM_LSI
) > count
) {
393 phb3_error(phb
, "LSIs out of reach: LSI base=%d total irq=%d", global
,
398 phb3_error(phb
, "More interrupts than supported: %d", count
);
401 if ((comp
& mask
) != comp
) {
402 phb3_error(phb
, "IRQ compare bits not in mask: comp=0x%x mask=0x%x",
406 /* Setup LSI offset */
407 ics
->offset
= comp
+ global
;
409 /* Setup MSI offset */
410 pnv_phb3_msi_update_config(&phb
->msis
, comp
, count
- PNV_PHB3_NUM_LSI
);
413 static void pnv_phb3_lsi_src_id_write(PnvPHB3
*phb
, uint64_t val
)
415 /* Sanitize content */
416 val
&= PHB_LSI_SRC_ID
;
417 phb
->regs
[PHB_LSI_SOURCE_ID
>> 3] = val
;
418 pnv_phb3_remap_irqs(phb
);
421 static void pnv_phb3_rtc_invalidate(PnvPHB3
*phb
, uint64_t val
)
425 /* Always invalidate all for now ... */
426 QLIST_FOREACH(ds
, &phb
->dma_spaces
, list
) {
427 ds
->pe_num
= PHB_INVALID_PE
;
432 static void pnv_phb3_update_msi_regions(PnvPhb3DMASpace
*ds
)
434 uint64_t cfg
= ds
->phb
->regs
[PHB_PHB3_CONFIG
>> 3];
436 if (cfg
& PHB_PHB3C_32BIT_MSI_EN
) {
437 if (!memory_region_is_mapped(&ds
->msi32_mr
)) {
438 memory_region_add_subregion(MEMORY_REGION(&ds
->dma_mr
),
439 0xffff0000, &ds
->msi32_mr
);
442 if (memory_region_is_mapped(&ds
->msi32_mr
)) {
443 memory_region_del_subregion(MEMORY_REGION(&ds
->dma_mr
),
448 if (cfg
& PHB_PHB3C_64BIT_MSI_EN
) {
449 if (!memory_region_is_mapped(&ds
->msi64_mr
)) {
450 memory_region_add_subregion(MEMORY_REGION(&ds
->dma_mr
),
451 (1ull << 60), &ds
->msi64_mr
);
454 if (memory_region_is_mapped(&ds
->msi64_mr
)) {
455 memory_region_del_subregion(MEMORY_REGION(&ds
->dma_mr
),
461 static void pnv_phb3_update_all_msi_regions(PnvPHB3
*phb
)
465 QLIST_FOREACH(ds
, &phb
->dma_spaces
, list
) {
466 pnv_phb3_update_msi_regions(ds
);
470 void pnv_phb3_reg_write(void *opaque
, hwaddr off
, uint64_t val
, unsigned size
)
472 PnvPHB3
*phb
= opaque
;
475 /* Special case configuration data */
476 if ((off
& 0xfffc) == PHB_CONFIG_DATA
) {
477 pnv_phb3_config_write(phb
, off
& 0x3, size
, val
);
481 /* Other registers are 64-bit only */
482 if (size
!= 8 || off
& 0x7) {
483 phb3_error(phb
, "Invalid register access, offset: 0x%"PRIx64
" size: %d",
488 /* Handle masking & filtering */
490 case PHB_M64_UPPER_BITS
:
491 val
&= 0xfffc000000000000ull
;
495 * This is enough logic to make SW happy but we aren't actually
498 if (val
& PHB_Q_DMA_R_AUTORESET
) {
501 val
&= PHB_Q_DMA_R_QUIESCE_DMA
;
505 case PHB_LEM_FIR_AND_MASK
:
506 phb
->regs
[PHB_LEM_FIR_ACCUM
>> 3] &= val
;
508 case PHB_LEM_FIR_OR_MASK
:
509 phb
->regs
[PHB_LEM_FIR_ACCUM
>> 3] |= val
;
511 case PHB_LEM_ERROR_AND_MASK
:
512 phb
->regs
[PHB_LEM_ERROR_MASK
>> 3] &= val
;
514 case PHB_LEM_ERROR_OR_MASK
:
515 phb
->regs
[PHB_LEM_ERROR_MASK
>> 3] |= val
;
522 /* Record whether it changed */
523 changed
= phb
->regs
[off
>> 3] != val
;
525 /* Store in register cache first */
526 phb
->regs
[off
>> 3] = val
;
528 /* Handle side effects */
530 case PHB_PHB3_CONFIG
:
532 pnv_phb3_update_all_msi_regions(phb
);
535 case PHB_M32_BASE_ADDR
:
536 case PHB_M32_BASE_MASK
:
537 case PHB_M32_START_ADDR
:
539 pnv_phb3_check_m32(phb
);
542 case PHB_M64_UPPER_BITS
:
544 pnv_phb3_check_all_m64s(phb
);
547 case PHB_LSI_SOURCE_ID
:
549 pnv_phb3_lsi_src_id_write(phb
, val
);
553 /* IODA table accesses */
555 pnv_phb3_ioda_write(phb
, val
);
558 /* RTC invalidation */
559 case PHB_RTC_INVALIDATE
:
560 pnv_phb3_rtc_invalidate(phb
, val
);
564 case PHB_FFI_REQUEST
:
565 pnv_phb3_msi_ffi(&phb
->msis
, val
);
568 /* Silent simple writes */
569 case PHB_CONFIG_ADDRESS
:
572 case PHB_TCE_SPEC_CTL
:
579 case PHB_LEM_FIR_ACCUM
:
580 case PHB_LEM_ERROR_MASK
:
581 case PHB_LEM_ACTION0
:
582 case PHB_LEM_ACTION1
:
585 /* Noise on anything else */
587 qemu_log_mask(LOG_UNIMP
, "phb3: reg_write 0x%"PRIx64
"=%"PRIx64
"\n",
592 uint64_t pnv_phb3_reg_read(void *opaque
, hwaddr off
, unsigned size
)
594 PnvPHB3
*phb
= opaque
;
595 PCIHostState
*pci
= PCI_HOST_BRIDGE(phb
->phb_base
);
598 if ((off
& 0xfffc) == PHB_CONFIG_DATA
) {
599 return pnv_phb3_config_read(phb
, off
& 0x3, size
);
602 /* Other registers are 64-bit only */
603 if (size
!= 8 || off
& 0x7) {
604 phb3_error(phb
, "Invalid register access, offset: 0x%"PRIx64
" size: %d",
609 /* Default read from cache */
610 val
= phb
->regs
[off
>> 3];
613 /* Simulate venice DD2.0 */
615 return 0x000000a300000005ull
;
616 case PHB_PCIE_SYSTEM_CONFIG
:
617 return 0x441100fc30000000;
619 /* IODA table accesses */
621 return pnv_phb3_ioda_read(phb
);
623 /* Link training always appears trained */
624 case PHB_PCIE_DLP_TRAIN_CTL
:
625 if (!pci_find_device(pci
->bus
, 1, 0)) {
628 return PHB_PCIE_DLP_INBAND_PRESENCE
| PHB_PCIE_DLP_TC_DL_LINKACT
;
632 /* Set lock and return previous value */
633 phb
->regs
[off
>> 3] |= PHB_FFI_LOCK_STATE
;
636 /* DMA read sync: make it look like it's complete */
638 return PHB_DMARD_SYNC_COMPLETE
;
640 /* Silent simple reads */
641 case PHB_PHB3_CONFIG
:
642 case PHB_M32_BASE_ADDR
:
643 case PHB_M32_BASE_MASK
:
644 case PHB_M32_START_ADDR
:
645 case PHB_CONFIG_ADDRESS
:
647 case PHB_RTC_INVALIDATE
:
649 case PHB_TCE_SPEC_CTL
:
655 case PHB_M64_UPPER_BITS
:
656 case PHB_LEM_FIR_ACCUM
:
657 case PHB_LEM_ERROR_MASK
:
658 case PHB_LEM_ACTION0
:
659 case PHB_LEM_ACTION1
:
662 /* Noise on anything else */
664 qemu_log_mask(LOG_UNIMP
, "phb3: reg_read 0x%"PRIx64
"=%"PRIx64
"\n",
670 static const MemoryRegionOps pnv_phb3_reg_ops
= {
671 .read
= pnv_phb3_reg_read
,
672 .write
= pnv_phb3_reg_write
,
673 .valid
.min_access_size
= 1,
674 .valid
.max_access_size
= 8,
675 .impl
.min_access_size
= 1,
676 .impl
.max_access_size
= 8,
677 .endianness
= DEVICE_BIG_ENDIAN
,
680 static int pnv_phb3_map_irq(PCIDevice
*pci_dev
, int irq_num
)
682 /* Check that out properly ... */
686 static void pnv_phb3_set_irq(void *opaque
, int irq_num
, int level
)
688 PnvPHB3
*phb
= opaque
;
692 phb3_error(phb
, "Unknown IRQ to set %d", irq_num
);
694 qemu_set_irq(phb
->qirqs
[irq_num
], level
);
697 static bool pnv_phb3_resolve_pe(PnvPhb3DMASpace
*ds
)
703 /* Already resolved ? */
704 if (ds
->pe_num
!= PHB_INVALID_PE
) {
708 /* We need to lookup the RTT */
709 rtt
= ds
->phb
->regs
[PHB_RTT_BAR
>> 3];
710 if (!(rtt
& PHB_RTT_BAR_ENABLE
)) {
711 phb3_error(ds
->phb
, "DMA with RTT BAR disabled !");
712 /* Set error bits ? fence ? ... */
717 bus_num
= pci_bus_num(ds
->bus
);
718 addr
= rtt
& PHB_RTT_BASE_ADDRESS_MASK
;
719 addr
+= 2 * ((bus_num
<< 8) | ds
->devfn
);
720 if (dma_memory_read(&address_space_memory
, addr
, &rte
,
721 sizeof(rte
), MEMTXATTRS_UNSPECIFIED
)) {
722 phb3_error(ds
->phb
, "Failed to read RTT entry at 0x%"PRIx64
, addr
);
723 /* Set error bits ? fence ? ... */
726 rte
= be16_to_cpu(rte
);
728 /* Fail upon reading of invalid PE# */
729 if (rte
>= PNV_PHB3_NUM_PE
) {
730 phb3_error(ds
->phb
, "RTE for RID 0x%x invalid (%04x", ds
->devfn
, rte
);
731 /* Set error bits ? fence ? ... */
738 static void pnv_phb3_translate_tve(PnvPhb3DMASpace
*ds
, hwaddr addr
,
739 bool is_write
, uint64_t tve
,
742 uint64_t tta
= GETFIELD(IODA2_TVT_TABLE_ADDR
, tve
);
743 int32_t lev
= GETFIELD(IODA2_TVT_NUM_LEVELS
, tve
);
744 uint32_t tts
= GETFIELD(IODA2_TVT_TCE_TABLE_SIZE
, tve
);
745 uint32_t tps
= GETFIELD(IODA2_TVT_IO_PSIZE
, tve
);
746 PnvPHB3
*phb
= ds
->phb
;
750 phb3_error(phb
, "Invalid #levels in TVE %d", lev
);
754 /* IO Page Size of 0 means untranslated, else use TCEs */
757 * We only support non-translate in top window.
759 * TODO: Venice/Murano support it on bottom window above 4G and
760 * Naples suports it on everything
762 if (!(tve
& PPC_BIT(51))) {
763 phb3_error(phb
, "xlate for invalid non-translate TVE");
766 /* TODO: Handle boundaries */
768 /* Use 4k pages like q35 ... for now */
769 tlb
->iova
= addr
& 0xfffffffffffff000ull
;
770 tlb
->translated_addr
= addr
& 0x0003fffffffff000ull
;
771 tlb
->addr_mask
= 0xfffull
;
772 tlb
->perm
= IOMMU_RW
;
774 uint32_t tce_shift
, tbl_shift
, sh
;
775 uint64_t base
, taddr
, tce
, tce_mask
;
779 phb3_error(phb
, "xlate for invalid translated TVE");
783 /* Address bits per bottom level TCE entry */
784 tce_shift
= tps
+ 11;
786 /* Address bits per table level */
789 /* Top level table base address */
792 /* Total shift to first level */
793 sh
= tbl_shift
* lev
+ tce_shift
;
795 /* TODO: Multi-level untested */
799 /* Grab the TCE address */
800 taddr
= base
| (((addr
>> sh
) & ((1ul << tbl_shift
) - 1)) << 3);
801 if (dma_memory_read(&address_space_memory
, taddr
, &tce
,
802 sizeof(tce
), MEMTXATTRS_UNSPECIFIED
)) {
803 phb3_error(phb
, "Failed to read TCE at 0x%"PRIx64
, taddr
);
806 tce
= be64_to_cpu(tce
);
808 /* Check permission for indirect TCE */
809 if ((lev
>= 0) && !(tce
& 3)) {
810 phb3_error(phb
, "Invalid indirect TCE at 0x%"PRIx64
, taddr
);
811 phb3_error(phb
, " xlate %"PRIx64
":%c TVE=%"PRIx64
, addr
,
812 is_write
? 'W' : 'R', tve
);
813 phb3_error(phb
, " tta=%"PRIx64
" lev=%d tts=%d tps=%d",
818 base
= tce
& ~0xfffull
;
821 /* We exit the loop with TCE being the final TCE */
822 if ((is_write
& !(tce
& 2)) || ((!is_write
) && !(tce
& 1))) {
823 phb3_error(phb
, "TCE access fault at 0x%"PRIx64
, taddr
);
824 phb3_error(phb
, " xlate %"PRIx64
":%c TVE=%"PRIx64
, addr
,
825 is_write
? 'W' : 'R', tve
);
826 phb3_error(phb
, " tta=%"PRIx64
" lev=%d tts=%d tps=%d",
830 tce_mask
= ~((1ull << tce_shift
) - 1);
831 tlb
->iova
= addr
& tce_mask
;
832 tlb
->translated_addr
= tce
& tce_mask
;
833 tlb
->addr_mask
= ~tce_mask
;
838 static IOMMUTLBEntry
pnv_phb3_translate_iommu(IOMMUMemoryRegion
*iommu
,
840 IOMMUAccessFlags flag
,
843 PnvPhb3DMASpace
*ds
= container_of(iommu
, PnvPhb3DMASpace
, dma_mr
);
846 IOMMUTLBEntry ret
= {
847 .target_as
= &address_space_memory
,
849 .translated_addr
= 0,
850 .addr_mask
= ~(hwaddr
)0,
853 PnvPHB3
*phb
= ds
->phb
;
856 if (!pnv_phb3_resolve_pe(ds
)) {
857 phb3_error(phb
, "Failed to resolve PE# for bus @%p (%d) devfn 0x%x",
858 ds
->bus
, pci_bus_num(ds
->bus
), ds
->devfn
);
863 switch (addr
>> 60) {
865 /* DMA or 32-bit MSI ? */
866 cfg
= ds
->phb
->regs
[PHB_PHB3_CONFIG
>> 3];
867 if ((cfg
& PHB_PHB3C_32BIT_MSI_EN
) &&
868 ((addr
& 0xffffffffffff0000ull
) == 0xffff0000ull
)) {
869 phb3_error(phb
, "xlate on 32-bit MSI region");
872 /* Choose TVE XXX Use PHB3 Control Register */
873 tve_sel
= (addr
>> 59) & 1;
874 tve
= ds
->phb
->ioda_TVT
[ds
->pe_num
* 2 + tve_sel
];
875 pnv_phb3_translate_tve(ds
, addr
, flag
& IOMMU_WO
, tve
, &ret
);
878 phb3_error(phb
, "xlate on 64-bit MSI region");
881 phb3_error(phb
, "xlate on unsupported address 0x%"PRIx64
, addr
);
886 #define TYPE_PNV_PHB3_IOMMU_MEMORY_REGION "pnv-phb3-iommu-memory-region"
887 DECLARE_INSTANCE_CHECKER(IOMMUMemoryRegion
, PNV_PHB3_IOMMU_MEMORY_REGION
,
888 TYPE_PNV_PHB3_IOMMU_MEMORY_REGION
)
890 static void pnv_phb3_iommu_memory_region_class_init(ObjectClass
*klass
,
893 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_CLASS(klass
);
895 imrc
->translate
= pnv_phb3_translate_iommu
;
898 static const TypeInfo pnv_phb3_iommu_memory_region_info
= {
899 .parent
= TYPE_IOMMU_MEMORY_REGION
,
900 .name
= TYPE_PNV_PHB3_IOMMU_MEMORY_REGION
,
901 .class_init
= pnv_phb3_iommu_memory_region_class_init
,
905 * MSI/MSIX memory region implementation.
906 * The handler handles both MSI and MSIX.
908 static void pnv_phb3_msi_write(void *opaque
, hwaddr addr
,
909 uint64_t data
, unsigned size
)
911 PnvPhb3DMASpace
*ds
= opaque
;
914 if (!pnv_phb3_resolve_pe(ds
)) {
915 phb3_error(ds
->phb
, "Failed to resolve PE# for bus @%p (%d) devfn 0x%x",
916 ds
->bus
, pci_bus_num(ds
->bus
), ds
->devfn
);
920 pnv_phb3_msi_send(&ds
->phb
->msis
, addr
, data
, ds
->pe_num
);
923 /* There is no .read as the read result is undefined by PCI spec */
924 static uint64_t pnv_phb3_msi_read(void *opaque
, hwaddr addr
, unsigned size
)
926 PnvPhb3DMASpace
*ds
= opaque
;
928 phb3_error(ds
->phb
, "invalid read @ 0x%" HWADDR_PRIx
, addr
);
932 static const MemoryRegionOps pnv_phb3_msi_ops
= {
933 .read
= pnv_phb3_msi_read
,
934 .write
= pnv_phb3_msi_write
,
935 .endianness
= DEVICE_LITTLE_ENDIAN
938 static AddressSpace
*pnv_phb3_dma_iommu(PCIBus
*bus
, void *opaque
, int devfn
)
940 PnvPHB3
*phb
= opaque
;
943 QLIST_FOREACH(ds
, &phb
->dma_spaces
, list
) {
944 if (ds
->bus
== bus
&& ds
->devfn
== devfn
) {
950 ds
= g_new0(PnvPhb3DMASpace
, 1);
953 ds
->pe_num
= PHB_INVALID_PE
;
955 memory_region_init_iommu(&ds
->dma_mr
, sizeof(ds
->dma_mr
),
956 TYPE_PNV_PHB3_IOMMU_MEMORY_REGION
,
957 OBJECT(phb
), "phb3_iommu", UINT64_MAX
);
958 address_space_init(&ds
->dma_as
, MEMORY_REGION(&ds
->dma_mr
),
960 memory_region_init_io(&ds
->msi32_mr
, OBJECT(phb
), &pnv_phb3_msi_ops
,
961 ds
, "msi32", 0x10000);
962 memory_region_init_io(&ds
->msi64_mr
, OBJECT(phb
), &pnv_phb3_msi_ops
,
963 ds
, "msi64", 0x100000);
964 pnv_phb3_update_msi_regions(ds
);
966 QLIST_INSERT_HEAD(&phb
->dma_spaces
, ds
, list
);
971 static void pnv_phb3_instance_init(Object
*obj
)
973 PnvPHB3
*phb
= PNV_PHB3(obj
);
975 QLIST_INIT(&phb
->dma_spaces
);
978 object_initialize_child(obj
, "lsi", &phb
->lsis
, TYPE_ICS
);
980 /* Default init ... will be fixed by HW inits */
981 phb
->lsis
.offset
= 0;
984 object_initialize_child(obj
, "msi", &phb
->msis
, TYPE_PHB3_MSI
);
986 /* Power Bus Common Queue */
987 object_initialize_child(obj
, "pbcq", &phb
->pbcq
, TYPE_PNV_PBCQ
);
991 void pnv_phb3_bus_init(DeviceState
*dev
, PnvPHB3
*phb
)
993 PCIHostState
*pci
= PCI_HOST_BRIDGE(dev
);
996 * PHB3 doesn't support IO space. However, qemu gets very upset if
997 * we don't have an IO region to anchor IO BARs onto so we just
998 * initialize one which we never hook up to anything
1000 memory_region_init(&phb
->pci_io
, OBJECT(phb
), "pci-io", 0x10000);
1001 memory_region_init(&phb
->pci_mmio
, OBJECT(phb
), "pci-mmio",
1002 PCI_MMIO_TOTAL_SIZE
);
1004 pci
->bus
= pci_register_root_bus(dev
,
1005 dev
->id
? dev
->id
: NULL
,
1006 pnv_phb3_set_irq
, pnv_phb3_map_irq
, phb
,
1007 &phb
->pci_mmio
, &phb
->pci_io
,
1008 0, 4, TYPE_PNV_PHB3_ROOT_BUS
);
1010 object_property_set_int(OBJECT(pci
->bus
), "phb-id", phb
->phb_id
,
1012 object_property_set_int(OBJECT(pci
->bus
), "chip-id", phb
->chip_id
,
1015 pci_setup_iommu(pci
->bus
, pnv_phb3_dma_iommu
, phb
);
1018 static void pnv_phb3_realize(DeviceState
*dev
, Error
**errp
)
1020 PnvPHB3
*phb
= PNV_PHB3(dev
);
1021 PnvMachineState
*pnv
= PNV_MACHINE(qdev_get_machine());
1024 if (phb
->phb_id
>= PNV_CHIP_GET_CLASS(phb
->chip
)->num_phbs
) {
1025 error_setg(errp
, "invalid PHB index: %d", phb
->phb_id
);
1030 object_property_set_link(OBJECT(&phb
->lsis
), "xics", OBJECT(pnv
),
1032 object_property_set_int(OBJECT(&phb
->lsis
), "nr-irqs", PNV_PHB3_NUM_LSI
,
1034 if (!qdev_realize(DEVICE(&phb
->lsis
), NULL
, errp
)) {
1038 for (i
= 0; i
< phb
->lsis
.nr_irqs
; i
++) {
1039 ics_set_irq_type(&phb
->lsis
, i
, true);
1042 phb
->qirqs
= qemu_allocate_irqs(ics_set_irq
, &phb
->lsis
, phb
->lsis
.nr_irqs
);
1045 object_property_set_link(OBJECT(&phb
->msis
), "phb", OBJECT(phb
),
1047 object_property_set_link(OBJECT(&phb
->msis
), "xics", OBJECT(pnv
),
1049 object_property_set_int(OBJECT(&phb
->msis
), "nr-irqs", PHB3_MAX_MSI
,
1051 if (!qdev_realize(DEVICE(&phb
->msis
), NULL
, errp
)) {
1055 /* Power Bus Common Queue */
1056 object_property_set_link(OBJECT(&phb
->pbcq
), "phb", OBJECT(phb
),
1058 if (!qdev_realize(DEVICE(&phb
->pbcq
), NULL
, errp
)) {
1062 /* Controller Registers */
1063 memory_region_init_io(&phb
->mr_regs
, OBJECT(phb
), &pnv_phb3_reg_ops
, phb
,
1064 "phb3-regs", 0x1000);
1067 void pnv_phb3_update_regions(PnvPHB3
*phb
)
1069 PnvPBCQState
*pbcq
= &phb
->pbcq
;
1071 /* Unmap first always */
1072 if (memory_region_is_mapped(&phb
->mr_regs
)) {
1073 memory_region_del_subregion(&pbcq
->phbbar
, &phb
->mr_regs
);
1076 /* Map registers if enabled */
1077 if (memory_region_is_mapped(&pbcq
->phbbar
)) {
1078 /* TODO: We should use the PHB BAR 2 register but we don't ... */
1079 memory_region_add_subregion(&pbcq
->phbbar
, 0, &phb
->mr_regs
);
1082 /* Check/update m32 */
1083 if (memory_region_is_mapped(&phb
->mr_m32
)) {
1084 pnv_phb3_check_m32(phb
);
1086 pnv_phb3_check_all_m64s(phb
);
1089 static Property pnv_phb3_properties
[] = {
1090 DEFINE_PROP_UINT32("index", PnvPHB3
, phb_id
, 0),
1091 DEFINE_PROP_UINT32("chip-id", PnvPHB3
, chip_id
, 0),
1092 DEFINE_PROP_LINK("chip", PnvPHB3
, chip
, TYPE_PNV_CHIP
, PnvChip
*),
1093 DEFINE_PROP_LINK("phb-base", PnvPHB3
, phb_base
, TYPE_PNV_PHB
, PnvPHB
*),
1094 DEFINE_PROP_END_OF_LIST(),
1097 static void pnv_phb3_class_init(ObjectClass
*klass
, void *data
)
1099 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1101 dc
->realize
= pnv_phb3_realize
;
1102 device_class_set_props(dc
, pnv_phb3_properties
);
1103 dc
->user_creatable
= false;
1106 static const TypeInfo pnv_phb3_type_info
= {
1107 .name
= TYPE_PNV_PHB3
,
1108 .parent
= TYPE_DEVICE
,
1109 .instance_size
= sizeof(PnvPHB3
),
1110 .class_init
= pnv_phb3_class_init
,
1111 .instance_init
= pnv_phb3_instance_init
,
1114 static void pnv_phb3_root_bus_get_prop(Object
*obj
, Visitor
*v
,
1116 void *opaque
, Error
**errp
)
1118 PnvPHB3RootBus
*bus
= PNV_PHB3_ROOT_BUS(obj
);
1121 if (strcmp(name
, "phb-id") == 0) {
1122 value
= bus
->phb_id
;
1124 value
= bus
->chip_id
;
1127 visit_type_size(v
, name
, &value
, errp
);
1130 static void pnv_phb3_root_bus_set_prop(Object
*obj
, Visitor
*v
,
1132 void *opaque
, Error
**errp
)
1135 PnvPHB3RootBus
*bus
= PNV_PHB3_ROOT_BUS(obj
);
1138 if (!visit_type_size(v
, name
, &value
, errp
)) {
1142 if (strcmp(name
, "phb-id") == 0) {
1143 bus
->phb_id
= value
;
1145 bus
->chip_id
= value
;
1149 static void pnv_phb3_root_bus_class_init(ObjectClass
*klass
, void *data
)
1151 BusClass
*k
= BUS_CLASS(klass
);
1153 object_class_property_add(klass
, "phb-id", "int",
1154 pnv_phb3_root_bus_get_prop
,
1155 pnv_phb3_root_bus_set_prop
,
1158 object_class_property_add(klass
, "chip-id", "int",
1159 pnv_phb3_root_bus_get_prop
,
1160 pnv_phb3_root_bus_set_prop
,
1164 * PHB3 has only a single root complex. Enforce the limit on the
1170 static const TypeInfo pnv_phb3_root_bus_info
= {
1171 .name
= TYPE_PNV_PHB3_ROOT_BUS
,
1172 .parent
= TYPE_PCIE_BUS
,
1173 .instance_size
= sizeof(PnvPHB3RootBus
),
1174 .class_init
= pnv_phb3_root_bus_class_init
,
1177 static void pnv_phb3_register_types(void)
1179 type_register_static(&pnv_phb3_root_bus_info
);
1180 type_register_static(&pnv_phb3_type_info
);
1181 type_register_static(&pnv_phb3_iommu_memory_region_info
);
1184 type_init(pnv_phb3_register_types
)