19ae1329eSCédric Le Goater /*
29ae1329eSCédric Le Goater * QEMU PowerPC PowerNV (POWER8) PHB3 model
39ae1329eSCédric Le Goater *
49ae1329eSCédric Le Goater * Copyright (c) 2014-2020, IBM Corporation.
59ae1329eSCédric Le Goater *
69ae1329eSCédric Le Goater * This code is licensed under the GPL version 2 or later. See the
79ae1329eSCédric Le Goater * COPYING file in the top-level directory.
89ae1329eSCédric Le Goater */
99ae1329eSCédric Le Goater #include "qemu/osdep.h"
109ae1329eSCédric Le Goater #include "qemu/log.h"
119ae1329eSCédric Le Goater #include "qapi/visitor.h"
129ae1329eSCédric Le Goater #include "qapi/error.h"
139ae1329eSCédric Le Goater #include "hw/pci-host/pnv_phb3_regs.h"
141f5d6b2aSDaniel Henrique Barboza #include "hw/pci-host/pnv_phb.h"
159ae1329eSCédric Le Goater #include "hw/pci-host/pnv_phb3.h"
169ae1329eSCédric Le Goater #include "hw/pci/pcie_host.h"
179ae1329eSCédric Le Goater #include "hw/pci/pcie_port.h"
189ae1329eSCédric Le Goater #include "hw/ppc/pnv.h"
192c6fe2e2SMarkus Armbruster #include "hw/ppc/pnv_chip.h"
209ae1329eSCédric Le Goater #include "hw/irq.h"
219ae1329eSCédric Le Goater #include "hw/qdev-properties.h"
22db1015e9SEduardo Habkost #include "qom/object.h"
23a71cd51eSCédric Le Goater #include "sysemu/sysemu.h"
249ae1329eSCédric Le Goater
259ae1329eSCédric Le Goater #define phb3_error(phb, fmt, ...) \
269ae1329eSCédric Le Goater qemu_log_mask(LOG_GUEST_ERROR, "phb3[%d:%d]: " fmt "\n", \
279ae1329eSCédric Le Goater (phb)->chip_id, (phb)->phb_id, ## __VA_ARGS__)
289ae1329eSCédric Le Goater
pnv_phb3_find_cfg_dev(PnvPHB3 * phb)299ae1329eSCédric Le Goater static PCIDevice *pnv_phb3_find_cfg_dev(PnvPHB3 *phb)
309ae1329eSCédric Le Goater {
311f5d6b2aSDaniel Henrique Barboza PCIHostState *pci = PCI_HOST_BRIDGE(phb->phb_base);
329ae1329eSCédric Le Goater uint64_t addr = phb->regs[PHB_CONFIG_ADDRESS >> 3];
339ae1329eSCédric Le Goater uint8_t bus, devfn;
349ae1329eSCédric Le Goater
359ae1329eSCédric Le Goater if (!(addr >> 63)) {
369ae1329eSCédric Le Goater return NULL;
379ae1329eSCédric Le Goater }
389ae1329eSCédric Le Goater bus = (addr >> 52) & 0xff;
399ae1329eSCédric Le Goater devfn = (addr >> 44) & 0xff;
409ae1329eSCédric Le Goater
419ae1329eSCédric Le Goater return pci_find_device(pci->bus, bus, devfn);
429ae1329eSCédric Le Goater }
439ae1329eSCédric Le Goater
449ae1329eSCédric Le Goater /*
459ae1329eSCédric Le Goater * The CONFIG_DATA register expects little endian accesses, but as the
469ae1329eSCédric Le Goater * region is big endian, we have to swap the value.
479ae1329eSCédric Le Goater */
pnv_phb3_config_write(PnvPHB3 * phb,unsigned off,unsigned size,uint64_t val)489ae1329eSCédric Le Goater static void pnv_phb3_config_write(PnvPHB3 *phb, unsigned off,
499ae1329eSCédric Le Goater unsigned size, uint64_t val)
509ae1329eSCédric Le Goater {
519ae1329eSCédric Le Goater uint32_t cfg_addr, limit;
529ae1329eSCédric Le Goater PCIDevice *pdev;
539ae1329eSCédric Le Goater
549ae1329eSCédric Le Goater pdev = pnv_phb3_find_cfg_dev(phb);
559ae1329eSCédric Le Goater if (!pdev) {
569ae1329eSCédric Le Goater return;
579ae1329eSCédric Le Goater }
589ae1329eSCédric Le Goater cfg_addr = (phb->regs[PHB_CONFIG_ADDRESS >> 3] >> 32) & 0xffc;
599ae1329eSCédric Le Goater cfg_addr |= off;
609ae1329eSCédric Le Goater limit = pci_config_size(pdev);
619ae1329eSCédric Le Goater if (limit <= cfg_addr) {
629ae1329eSCédric Le Goater /*
639ae1329eSCédric Le Goater * conventional pci device can be behind pcie-to-pci bridge.
649ae1329eSCédric Le Goater * 256 <= addr < 4K has no effects.
659ae1329eSCédric Le Goater */
669ae1329eSCédric Le Goater return;
679ae1329eSCédric Le Goater }
689ae1329eSCédric Le Goater switch (size) {
699ae1329eSCédric Le Goater case 1:
709ae1329eSCédric Le Goater break;
719ae1329eSCédric Le Goater case 2:
729ae1329eSCédric Le Goater val = bswap16(val);
739ae1329eSCédric Le Goater break;
749ae1329eSCédric Le Goater case 4:
759ae1329eSCédric Le Goater val = bswap32(val);
769ae1329eSCédric Le Goater break;
779ae1329eSCédric Le Goater default:
789ae1329eSCédric Le Goater g_assert_not_reached();
799ae1329eSCédric Le Goater }
809ae1329eSCédric Le Goater pci_host_config_write_common(pdev, cfg_addr, limit, val, size);
819ae1329eSCédric Le Goater }
829ae1329eSCédric Le Goater
pnv_phb3_config_read(PnvPHB3 * phb,unsigned off,unsigned size)839ae1329eSCédric Le Goater static uint64_t pnv_phb3_config_read(PnvPHB3 *phb, unsigned off,
849ae1329eSCédric Le Goater unsigned size)
859ae1329eSCédric Le Goater {
869ae1329eSCédric Le Goater uint32_t cfg_addr, limit;
879ae1329eSCédric Le Goater PCIDevice *pdev;
889ae1329eSCédric Le Goater uint64_t val;
899ae1329eSCédric Le Goater
909ae1329eSCédric Le Goater pdev = pnv_phb3_find_cfg_dev(phb);
919ae1329eSCédric Le Goater if (!pdev) {
929ae1329eSCédric Le Goater return ~0ull;
939ae1329eSCédric Le Goater }
949ae1329eSCédric Le Goater cfg_addr = (phb->regs[PHB_CONFIG_ADDRESS >> 3] >> 32) & 0xffc;
959ae1329eSCédric Le Goater cfg_addr |= off;
969ae1329eSCédric Le Goater limit = pci_config_size(pdev);
979ae1329eSCédric Le Goater if (limit <= cfg_addr) {
989ae1329eSCédric Le Goater /*
999ae1329eSCédric Le Goater * conventional pci device can be behind pcie-to-pci bridge.
1009ae1329eSCédric Le Goater * 256 <= addr < 4K has no effects.
1019ae1329eSCédric Le Goater */
1029ae1329eSCédric Le Goater return ~0ull;
1039ae1329eSCédric Le Goater }
1049ae1329eSCédric Le Goater val = pci_host_config_read_common(pdev, cfg_addr, limit, size);
1059ae1329eSCédric Le Goater switch (size) {
1069ae1329eSCédric Le Goater case 1:
1079ae1329eSCédric Le Goater return val;
1089ae1329eSCédric Le Goater case 2:
1099ae1329eSCédric Le Goater return bswap16(val);
1109ae1329eSCédric Le Goater case 4:
1119ae1329eSCédric Le Goater return bswap32(val);
1129ae1329eSCédric Le Goater default:
1139ae1329eSCédric Le Goater g_assert_not_reached();
1149ae1329eSCédric Le Goater }
1159ae1329eSCédric Le Goater }
1169ae1329eSCédric Le Goater
pnv_phb3_check_m32(PnvPHB3 * phb)1179ae1329eSCédric Le Goater static void pnv_phb3_check_m32(PnvPHB3 *phb)
1189ae1329eSCédric Le Goater {
1199ae1329eSCédric Le Goater uint64_t base, start, size;
1209ae1329eSCédric Le Goater MemoryRegion *parent;
1219ae1329eSCédric Le Goater PnvPBCQState *pbcq = &phb->pbcq;
1229ae1329eSCédric Le Goater
1239ae1329eSCédric Le Goater if (memory_region_is_mapped(&phb->mr_m32)) {
1249ae1329eSCédric Le Goater memory_region_del_subregion(phb->mr_m32.container, &phb->mr_m32);
1259ae1329eSCédric Le Goater }
1269ae1329eSCédric Le Goater
1279ae1329eSCédric Le Goater if (!(phb->regs[PHB_PHB3_CONFIG >> 3] & PHB_PHB3C_M32_EN)) {
1289ae1329eSCédric Le Goater return;
1299ae1329eSCédric Le Goater }
1309ae1329eSCédric Le Goater
1319ae1329eSCédric Le Goater /* Grab geometry from registers */
1329ae1329eSCédric Le Goater base = phb->regs[PHB_M32_BASE_ADDR >> 3];
1339ae1329eSCédric Le Goater start = phb->regs[PHB_M32_START_ADDR >> 3];
1349ae1329eSCédric Le Goater size = ~(phb->regs[PHB_M32_BASE_MASK >> 3] | 0xfffc000000000000ull) + 1;
1359ae1329eSCédric Le Goater
1369ae1329eSCédric Le Goater /* Check if it matches an enabled MMIO region in the PBCQ */
1379ae1329eSCédric Le Goater if (memory_region_is_mapped(&pbcq->mmbar0) &&
1389ae1329eSCédric Le Goater base >= pbcq->mmio0_base &&
1399ae1329eSCédric Le Goater (base + size) <= (pbcq->mmio0_base + pbcq->mmio0_size)) {
1409ae1329eSCédric Le Goater parent = &pbcq->mmbar0;
1419ae1329eSCédric Le Goater base -= pbcq->mmio0_base;
1429ae1329eSCédric Le Goater } else if (memory_region_is_mapped(&pbcq->mmbar1) &&
1439ae1329eSCédric Le Goater base >= pbcq->mmio1_base &&
1449ae1329eSCédric Le Goater (base + size) <= (pbcq->mmio1_base + pbcq->mmio1_size)) {
1459ae1329eSCédric Le Goater parent = &pbcq->mmbar1;
1469ae1329eSCédric Le Goater base -= pbcq->mmio1_base;
1479ae1329eSCédric Le Goater } else {
1489ae1329eSCédric Le Goater return;
1499ae1329eSCédric Le Goater }
1509ae1329eSCédric Le Goater
1519ae1329eSCédric Le Goater /* Create alias */
1529ae1329eSCédric Le Goater memory_region_init_alias(&phb->mr_m32, OBJECT(phb), "phb3-m32",
1539ae1329eSCédric Le Goater &phb->pci_mmio, start, size);
1549ae1329eSCédric Le Goater memory_region_add_subregion(parent, base, &phb->mr_m32);
1559ae1329eSCédric Le Goater }
1569ae1329eSCédric Le Goater
pnv_phb3_check_m64(PnvPHB3 * phb,uint32_t index)1579ae1329eSCédric Le Goater static void pnv_phb3_check_m64(PnvPHB3 *phb, uint32_t index)
1589ae1329eSCédric Le Goater {
1599ae1329eSCédric Le Goater uint64_t base, start, size, m64;
1609ae1329eSCédric Le Goater MemoryRegion *parent;
1619ae1329eSCédric Le Goater PnvPBCQState *pbcq = &phb->pbcq;
1629ae1329eSCédric Le Goater
1639ae1329eSCédric Le Goater if (memory_region_is_mapped(&phb->mr_m64[index])) {
1649ae1329eSCédric Le Goater /* Should we destroy it in RCU friendly way... ? */
1659ae1329eSCédric Le Goater memory_region_del_subregion(phb->mr_m64[index].container,
1669ae1329eSCédric Le Goater &phb->mr_m64[index]);
1679ae1329eSCédric Le Goater }
1689ae1329eSCédric Le Goater
1699ae1329eSCédric Le Goater /* Get table entry */
1709ae1329eSCédric Le Goater m64 = phb->ioda_M64BT[index];
1719ae1329eSCédric Le Goater
1729ae1329eSCédric Le Goater if (!(m64 & IODA2_M64BT_ENABLE)) {
1739ae1329eSCédric Le Goater return;
1749ae1329eSCédric Le Goater }
1759ae1329eSCédric Le Goater
1769ae1329eSCédric Le Goater /* Grab geometry from registers */
1779ae1329eSCédric Le Goater base = GETFIELD(IODA2_M64BT_BASE, m64) << 20;
1789ae1329eSCédric Le Goater if (m64 & IODA2_M64BT_SINGLE_PE) {
1799ae1329eSCédric Le Goater base &= ~0x1ffffffull;
1809ae1329eSCédric Le Goater }
1819ae1329eSCédric Le Goater size = GETFIELD(IODA2_M64BT_MASK, m64) << 20;
1829ae1329eSCédric Le Goater size |= 0xfffc000000000000ull;
1839ae1329eSCédric Le Goater size = ~size + 1;
1849ae1329eSCédric Le Goater start = base | (phb->regs[PHB_M64_UPPER_BITS >> 3]);
1859ae1329eSCédric Le Goater
1869ae1329eSCédric Le Goater /* Check if it matches an enabled MMIO region in the PBCQ */
1879ae1329eSCédric Le Goater if (memory_region_is_mapped(&pbcq->mmbar0) &&
1889ae1329eSCédric Le Goater base >= pbcq->mmio0_base &&
1899ae1329eSCédric Le Goater (base + size) <= (pbcq->mmio0_base + pbcq->mmio0_size)) {
1909ae1329eSCédric Le Goater parent = &pbcq->mmbar0;
1919ae1329eSCédric Le Goater base -= pbcq->mmio0_base;
1929ae1329eSCédric Le Goater } else if (memory_region_is_mapped(&pbcq->mmbar1) &&
1939ae1329eSCédric Le Goater base >= pbcq->mmio1_base &&
1949ae1329eSCédric Le Goater (base + size) <= (pbcq->mmio1_base + pbcq->mmio1_size)) {
1959ae1329eSCédric Le Goater parent = &pbcq->mmbar1;
1969ae1329eSCédric Le Goater base -= pbcq->mmio1_base;
1979ae1329eSCédric Le Goater } else {
1989ae1329eSCédric Le Goater return;
1999ae1329eSCédric Le Goater }
2009ae1329eSCédric Le Goater
2019ae1329eSCédric Le Goater /* Create alias */
2029ae1329eSCédric Le Goater memory_region_init_alias(&phb->mr_m64[index], OBJECT(phb), "phb3-m64",
2039ae1329eSCédric Le Goater &phb->pci_mmio, start, size);
2049ae1329eSCédric Le Goater memory_region_add_subregion(parent, base, &phb->mr_m64[index]);
2059ae1329eSCédric Le Goater }
2069ae1329eSCédric Le Goater
pnv_phb3_check_all_m64s(PnvPHB3 * phb)2079ae1329eSCédric Le Goater static void pnv_phb3_check_all_m64s(PnvPHB3 *phb)
2089ae1329eSCédric Le Goater {
2099ae1329eSCédric Le Goater uint64_t i;
2109ae1329eSCédric Le Goater
2119ae1329eSCédric Le Goater for (i = 0; i < PNV_PHB3_NUM_M64; i++) {
2129ae1329eSCédric Le Goater pnv_phb3_check_m64(phb, i);
2139ae1329eSCédric Le Goater }
2149ae1329eSCédric Le Goater }
2159ae1329eSCédric Le Goater
pnv_phb3_lxivt_write(PnvPHB3 * phb,unsigned idx,uint64_t val)2169ae1329eSCédric Le Goater static void pnv_phb3_lxivt_write(PnvPHB3 *phb, unsigned idx, uint64_t val)
2179ae1329eSCédric Le Goater {
2189ae1329eSCédric Le Goater uint8_t server, prio;
2199ae1329eSCédric Le Goater
2209ae1329eSCédric Le Goater phb->ioda_LXIVT[idx] = val & (IODA2_LXIVT_SERVER |
2219ae1329eSCédric Le Goater IODA2_LXIVT_PRIORITY |
2229ae1329eSCédric Le Goater IODA2_LXIVT_NODE_ID);
2239ae1329eSCédric Le Goater server = GETFIELD(IODA2_LXIVT_SERVER, val);
2249ae1329eSCédric Le Goater prio = GETFIELD(IODA2_LXIVT_PRIORITY, val);
2259ae1329eSCédric Le Goater
2269ae1329eSCédric Le Goater /*
2279ae1329eSCédric Le Goater * The low order 2 bits are the link pointer (Type II interrupts).
2289ae1329eSCédric Le Goater * Shift back to get a valid IRQ server.
2299ae1329eSCédric Le Goater */
2309ae1329eSCédric Le Goater server >>= 2;
2319ae1329eSCédric Le Goater
2329ae1329eSCédric Le Goater ics_write_xive(&phb->lsis, idx, server, prio, prio);
2339ae1329eSCédric Le Goater }
2349ae1329eSCédric Le Goater
pnv_phb3_ioda_access(PnvPHB3 * phb,unsigned * out_table,unsigned * out_idx)2359ae1329eSCédric Le Goater static uint64_t *pnv_phb3_ioda_access(PnvPHB3 *phb,
2369ae1329eSCédric Le Goater unsigned *out_table, unsigned *out_idx)
2379ae1329eSCédric Le Goater {
2389ae1329eSCédric Le Goater uint64_t adreg = phb->regs[PHB_IODA_ADDR >> 3];
2399ae1329eSCédric Le Goater unsigned int index = GETFIELD(PHB_IODA_AD_TADR, adreg);
2409ae1329eSCédric Le Goater unsigned int table = GETFIELD(PHB_IODA_AD_TSEL, adreg);
2419ae1329eSCédric Le Goater unsigned int mask;
2429ae1329eSCédric Le Goater uint64_t *tptr = NULL;
2439ae1329eSCédric Le Goater
2449ae1329eSCédric Le Goater switch (table) {
2459ae1329eSCédric Le Goater case IODA2_TBL_LIST:
2469ae1329eSCédric Le Goater tptr = phb->ioda_LIST;
2479ae1329eSCédric Le Goater mask = 7;
2489ae1329eSCédric Le Goater break;
2499ae1329eSCédric Le Goater case IODA2_TBL_LXIVT:
2509ae1329eSCédric Le Goater tptr = phb->ioda_LXIVT;
2519ae1329eSCédric Le Goater mask = 7;
2529ae1329eSCédric Le Goater break;
2539ae1329eSCédric Le Goater case IODA2_TBL_IVC_CAM:
2549ae1329eSCédric Le Goater case IODA2_TBL_RBA:
2559ae1329eSCédric Le Goater mask = 31;
2569ae1329eSCédric Le Goater break;
2579ae1329eSCédric Le Goater case IODA2_TBL_RCAM:
2589ae1329eSCédric Le Goater mask = 63;
2599ae1329eSCédric Le Goater break;
2609ae1329eSCédric Le Goater case IODA2_TBL_MRT:
2619ae1329eSCédric Le Goater mask = 7;
2629ae1329eSCédric Le Goater break;
2639ae1329eSCédric Le Goater case IODA2_TBL_PESTA:
2649ae1329eSCédric Le Goater case IODA2_TBL_PESTB:
2659ae1329eSCédric Le Goater mask = 255;
2669ae1329eSCédric Le Goater break;
2679ae1329eSCédric Le Goater case IODA2_TBL_TVT:
2689ae1329eSCédric Le Goater tptr = phb->ioda_TVT;
2699ae1329eSCédric Le Goater mask = 511;
2709ae1329eSCédric Le Goater break;
2719ae1329eSCédric Le Goater case IODA2_TBL_TCAM:
2729ae1329eSCédric Le Goater case IODA2_TBL_TDR:
2739ae1329eSCédric Le Goater mask = 63;
2749ae1329eSCédric Le Goater break;
2759ae1329eSCédric Le Goater case IODA2_TBL_M64BT:
2769ae1329eSCédric Le Goater tptr = phb->ioda_M64BT;
2779ae1329eSCédric Le Goater mask = 15;
2789ae1329eSCédric Le Goater break;
2799ae1329eSCédric Le Goater case IODA2_TBL_M32DT:
2809ae1329eSCédric Le Goater tptr = phb->ioda_MDT;
2819ae1329eSCédric Le Goater mask = 255;
2829ae1329eSCédric Le Goater break;
2839ae1329eSCédric Le Goater case IODA2_TBL_PEEV:
2849ae1329eSCédric Le Goater tptr = phb->ioda_PEEV;
2859ae1329eSCédric Le Goater mask = 3;
2869ae1329eSCédric Le Goater break;
2879ae1329eSCédric Le Goater default:
2889ae1329eSCédric Le Goater phb3_error(phb, "invalid IODA table %d", table);
2899ae1329eSCédric Le Goater return NULL;
2909ae1329eSCédric Le Goater }
2919ae1329eSCédric Le Goater index &= mask;
2929ae1329eSCédric Le Goater if (out_idx) {
2939ae1329eSCédric Le Goater *out_idx = index;
2949ae1329eSCédric Le Goater }
2959ae1329eSCédric Le Goater if (out_table) {
2969ae1329eSCédric Le Goater *out_table = table;
2979ae1329eSCédric Le Goater }
2989ae1329eSCédric Le Goater if (tptr) {
2999ae1329eSCédric Le Goater tptr += index;
3009ae1329eSCédric Le Goater }
3019ae1329eSCédric Le Goater if (adreg & PHB_IODA_AD_AUTOINC) {
3029ae1329eSCédric Le Goater index = (index + 1) & mask;
3039ae1329eSCédric Le Goater adreg = SETFIELD(PHB_IODA_AD_TADR, adreg, index);
3049ae1329eSCédric Le Goater }
3059ae1329eSCédric Le Goater phb->regs[PHB_IODA_ADDR >> 3] = adreg;
3069ae1329eSCédric Le Goater return tptr;
3079ae1329eSCédric Le Goater }
3089ae1329eSCédric Le Goater
pnv_phb3_ioda_read(PnvPHB3 * phb)3099ae1329eSCédric Le Goater static uint64_t pnv_phb3_ioda_read(PnvPHB3 *phb)
3109ae1329eSCédric Le Goater {
3119ae1329eSCédric Le Goater unsigned table;
3129ae1329eSCédric Le Goater uint64_t *tptr;
3139ae1329eSCédric Le Goater
3149ae1329eSCédric Le Goater tptr = pnv_phb3_ioda_access(phb, &table, NULL);
3159ae1329eSCédric Le Goater if (!tptr) {
3169ae1329eSCédric Le Goater /* Return 0 on unsupported tables, not ff's */
3179ae1329eSCédric Le Goater return 0;
3189ae1329eSCédric Le Goater }
3199ae1329eSCédric Le Goater return *tptr;
3209ae1329eSCédric Le Goater }
3219ae1329eSCédric Le Goater
pnv_phb3_ioda_write(PnvPHB3 * phb,uint64_t val)3229ae1329eSCédric Le Goater static void pnv_phb3_ioda_write(PnvPHB3 *phb, uint64_t val)
3239ae1329eSCédric Le Goater {
3249ae1329eSCédric Le Goater unsigned table, idx;
3259ae1329eSCédric Le Goater uint64_t *tptr;
3269ae1329eSCédric Le Goater
3279ae1329eSCédric Le Goater tptr = pnv_phb3_ioda_access(phb, &table, &idx);
3289ae1329eSCédric Le Goater if (!tptr) {
3299ae1329eSCédric Le Goater return;
3309ae1329eSCédric Le Goater }
3319ae1329eSCédric Le Goater
3329ae1329eSCédric Le Goater /* Handle side effects */
3339ae1329eSCédric Le Goater switch (table) {
3349ae1329eSCédric Le Goater case IODA2_TBL_LXIVT:
3359ae1329eSCédric Le Goater pnv_phb3_lxivt_write(phb, idx, val);
3369ae1329eSCédric Le Goater break;
3379ae1329eSCédric Le Goater case IODA2_TBL_M64BT:
3389ae1329eSCédric Le Goater *tptr = val;
3399ae1329eSCédric Le Goater pnv_phb3_check_m64(phb, idx);
3409ae1329eSCédric Le Goater break;
3419ae1329eSCédric Le Goater default:
3429ae1329eSCédric Le Goater *tptr = val;
3439ae1329eSCédric Le Goater }
3449ae1329eSCédric Le Goater }
3459ae1329eSCédric Le Goater
3469ae1329eSCédric Le Goater /*
3479ae1329eSCédric Le Goater * This is called whenever the PHB LSI, MSI source ID register or
3489ae1329eSCédric Le Goater * the PBCQ irq filters are written.
3499ae1329eSCédric Le Goater */
pnv_phb3_remap_irqs(PnvPHB3 * phb)3509ae1329eSCédric Le Goater void pnv_phb3_remap_irqs(PnvPHB3 *phb)
3519ae1329eSCédric Le Goater {
3529ae1329eSCédric Le Goater ICSState *ics = &phb->lsis;
3539ae1329eSCédric Le Goater uint32_t local, global, count, mask, comp;
3549ae1329eSCédric Le Goater uint64_t baren;
3559ae1329eSCédric Le Goater PnvPBCQState *pbcq = &phb->pbcq;
3569ae1329eSCédric Le Goater
3579ae1329eSCédric Le Goater /*
3589ae1329eSCédric Le Goater * First check if we are enabled. Unlike real HW we don't separate
3599ae1329eSCédric Le Goater * TX and RX so we enable if both are set
3609ae1329eSCédric Le Goater */
3619ae1329eSCédric Le Goater baren = pbcq->nest_regs[PBCQ_NEST_BAR_EN];
3629ae1329eSCédric Le Goater if (!(baren & PBCQ_NEST_BAR_EN_IRSN_RX) ||
3639ae1329eSCédric Le Goater !(baren & PBCQ_NEST_BAR_EN_IRSN_TX)) {
3649ae1329eSCédric Le Goater ics->offset = 0;
3659ae1329eSCédric Le Goater return;
3669ae1329eSCédric Le Goater }
3679ae1329eSCédric Le Goater
3689ae1329eSCédric Le Goater /* Grab local LSI source ID */
3699ae1329eSCédric Le Goater local = GETFIELD(PHB_LSI_SRC_ID, phb->regs[PHB_LSI_SOURCE_ID >> 3]) << 3;
3709ae1329eSCédric Le Goater
3719ae1329eSCédric Le Goater /* Grab global one and compare */
3729ae1329eSCédric Le Goater global = GETFIELD(PBCQ_NEST_LSI_SRC,
3739ae1329eSCédric Le Goater pbcq->nest_regs[PBCQ_NEST_LSI_SRC_ID]) << 3;
3749ae1329eSCédric Le Goater if (global != local) {
3759ae1329eSCédric Le Goater /*
3769ae1329eSCédric Le Goater * This happens during initialization, let's come back when we
3779ae1329eSCédric Le Goater * are properly configured
3789ae1329eSCédric Le Goater */
3799ae1329eSCédric Le Goater ics->offset = 0;
3809ae1329eSCédric Le Goater return;
3819ae1329eSCédric Le Goater }
3829ae1329eSCédric Le Goater
3839ae1329eSCédric Le Goater /* Get the base on the powerbus */
3849ae1329eSCédric Le Goater comp = GETFIELD(PBCQ_NEST_IRSN_COMP,
3859ae1329eSCédric Le Goater pbcq->nest_regs[PBCQ_NEST_IRSN_COMPARE]);
3869ae1329eSCédric Le Goater mask = GETFIELD(PBCQ_NEST_IRSN_COMP,
3879ae1329eSCédric Le Goater pbcq->nest_regs[PBCQ_NEST_IRSN_MASK]);
3889ae1329eSCédric Le Goater count = ((~mask) + 1) & 0x7ffff;
3899ae1329eSCédric Le Goater phb->total_irq = count;
3909ae1329eSCédric Le Goater
3919ae1329eSCédric Le Goater /* Sanity checks */
3929ae1329eSCédric Le Goater if ((global + PNV_PHB3_NUM_LSI) > count) {
3939ae1329eSCédric Le Goater phb3_error(phb, "LSIs out of reach: LSI base=%d total irq=%d", global,
3949ae1329eSCédric Le Goater count);
3959ae1329eSCédric Le Goater }
3969ae1329eSCédric Le Goater
3979ae1329eSCédric Le Goater if (count > 2048) {
3989ae1329eSCédric Le Goater phb3_error(phb, "More interrupts than supported: %d", count);
3999ae1329eSCédric Le Goater }
4009ae1329eSCédric Le Goater
4019ae1329eSCédric Le Goater if ((comp & mask) != comp) {
4029ae1329eSCédric Le Goater phb3_error(phb, "IRQ compare bits not in mask: comp=0x%x mask=0x%x",
4039ae1329eSCédric Le Goater comp, mask);
4049ae1329eSCédric Le Goater comp &= mask;
4059ae1329eSCédric Le Goater }
4069ae1329eSCédric Le Goater /* Setup LSI offset */
4079ae1329eSCédric Le Goater ics->offset = comp + global;
4089ae1329eSCédric Le Goater
4099ae1329eSCédric Le Goater /* Setup MSI offset */
4109ae1329eSCédric Le Goater pnv_phb3_msi_update_config(&phb->msis, comp, count - PNV_PHB3_NUM_LSI);
4119ae1329eSCédric Le Goater }
4129ae1329eSCédric Le Goater
pnv_phb3_lsi_src_id_write(PnvPHB3 * phb,uint64_t val)4139ae1329eSCédric Le Goater static void pnv_phb3_lsi_src_id_write(PnvPHB3 *phb, uint64_t val)
4149ae1329eSCédric Le Goater {
4159ae1329eSCédric Le Goater /* Sanitize content */
4169ae1329eSCédric Le Goater val &= PHB_LSI_SRC_ID;
4179ae1329eSCédric Le Goater phb->regs[PHB_LSI_SOURCE_ID >> 3] = val;
4189ae1329eSCédric Le Goater pnv_phb3_remap_irqs(phb);
4199ae1329eSCédric Le Goater }
4209ae1329eSCédric Le Goater
pnv_phb3_rtc_invalidate(PnvPHB3 * phb,uint64_t val)4219ae1329eSCédric Le Goater static void pnv_phb3_rtc_invalidate(PnvPHB3 *phb, uint64_t val)
4229ae1329eSCédric Le Goater {
4239ae1329eSCédric Le Goater PnvPhb3DMASpace *ds;
4249ae1329eSCédric Le Goater
4259ae1329eSCédric Le Goater /* Always invalidate all for now ... */
4269ae1329eSCédric Le Goater QLIST_FOREACH(ds, &phb->dma_spaces, list) {
4279ae1329eSCédric Le Goater ds->pe_num = PHB_INVALID_PE;
4289ae1329eSCédric Le Goater }
4299ae1329eSCédric Le Goater }
4309ae1329eSCédric Le Goater
4319ae1329eSCédric Le Goater
pnv_phb3_update_msi_regions(PnvPhb3DMASpace * ds)4329ae1329eSCédric Le Goater static void pnv_phb3_update_msi_regions(PnvPhb3DMASpace *ds)
4339ae1329eSCédric Le Goater {
4349ae1329eSCédric Le Goater uint64_t cfg = ds->phb->regs[PHB_PHB3_CONFIG >> 3];
4359ae1329eSCédric Le Goater
4369ae1329eSCédric Le Goater if (cfg & PHB_PHB3C_32BIT_MSI_EN) {
4379ae1329eSCédric Le Goater if (!memory_region_is_mapped(&ds->msi32_mr)) {
4389ae1329eSCédric Le Goater memory_region_add_subregion(MEMORY_REGION(&ds->dma_mr),
4399ae1329eSCédric Le Goater 0xffff0000, &ds->msi32_mr);
4409ae1329eSCédric Le Goater }
4419ae1329eSCédric Le Goater } else {
4429ae1329eSCédric Le Goater if (memory_region_is_mapped(&ds->msi32_mr)) {
4439ae1329eSCédric Le Goater memory_region_del_subregion(MEMORY_REGION(&ds->dma_mr),
4449ae1329eSCédric Le Goater &ds->msi32_mr);
4459ae1329eSCédric Le Goater }
4469ae1329eSCédric Le Goater }
4479ae1329eSCédric Le Goater
4489ae1329eSCédric Le Goater if (cfg & PHB_PHB3C_64BIT_MSI_EN) {
4499ae1329eSCédric Le Goater if (!memory_region_is_mapped(&ds->msi64_mr)) {
4509ae1329eSCédric Le Goater memory_region_add_subregion(MEMORY_REGION(&ds->dma_mr),
4519ae1329eSCédric Le Goater (1ull << 60), &ds->msi64_mr);
4529ae1329eSCédric Le Goater }
4539ae1329eSCédric Le Goater } else {
4549ae1329eSCédric Le Goater if (memory_region_is_mapped(&ds->msi64_mr)) {
4559ae1329eSCédric Le Goater memory_region_del_subregion(MEMORY_REGION(&ds->dma_mr),
4569ae1329eSCédric Le Goater &ds->msi64_mr);
4579ae1329eSCédric Le Goater }
4589ae1329eSCédric Le Goater }
4599ae1329eSCédric Le Goater }
4609ae1329eSCédric Le Goater
pnv_phb3_update_all_msi_regions(PnvPHB3 * phb)4619ae1329eSCédric Le Goater static void pnv_phb3_update_all_msi_regions(PnvPHB3 *phb)
4629ae1329eSCédric Le Goater {
4639ae1329eSCédric Le Goater PnvPhb3DMASpace *ds;
4649ae1329eSCédric Le Goater
4659ae1329eSCédric Le Goater QLIST_FOREACH(ds, &phb->dma_spaces, list) {
4669ae1329eSCédric Le Goater pnv_phb3_update_msi_regions(ds);
4679ae1329eSCédric Le Goater }
4689ae1329eSCédric Le Goater }
4699ae1329eSCédric Le Goater
pnv_phb3_reg_write(void * opaque,hwaddr off,uint64_t val,unsigned size)4709ae1329eSCédric Le Goater void pnv_phb3_reg_write(void *opaque, hwaddr off, uint64_t val, unsigned size)
4719ae1329eSCédric Le Goater {
4729ae1329eSCédric Le Goater PnvPHB3 *phb = opaque;
4739ae1329eSCédric Le Goater bool changed;
4749ae1329eSCédric Le Goater
4759ae1329eSCédric Le Goater /* Special case configuration data */
4769ae1329eSCédric Le Goater if ((off & 0xfffc) == PHB_CONFIG_DATA) {
4779ae1329eSCédric Le Goater pnv_phb3_config_write(phb, off & 0x3, size, val);
4789ae1329eSCédric Le Goater return;
4799ae1329eSCédric Le Goater }
4809ae1329eSCédric Le Goater
4819ae1329eSCédric Le Goater /* Other registers are 64-bit only */
4829ae1329eSCédric Le Goater if (size != 8 || off & 0x7) {
4839ae1329eSCédric Le Goater phb3_error(phb, "Invalid register access, offset: 0x%"PRIx64" size: %d",
4849ae1329eSCédric Le Goater off, size);
4859ae1329eSCédric Le Goater return;
4869ae1329eSCédric Le Goater }
4879ae1329eSCédric Le Goater
4889ae1329eSCédric Le Goater /* Handle masking & filtering */
4899ae1329eSCédric Le Goater switch (off) {
4909ae1329eSCédric Le Goater case PHB_M64_UPPER_BITS:
4919ae1329eSCédric Le Goater val &= 0xfffc000000000000ull;
4929ae1329eSCédric Le Goater break;
4939ae1329eSCédric Le Goater case PHB_Q_DMA_R:
4949ae1329eSCédric Le Goater /*
4959ae1329eSCédric Le Goater * This is enough logic to make SW happy but we aren't actually
4969ae1329eSCédric Le Goater * quiescing the DMAs
4979ae1329eSCédric Le Goater */
4989ae1329eSCédric Le Goater if (val & PHB_Q_DMA_R_AUTORESET) {
4999ae1329eSCédric Le Goater val = 0;
5009ae1329eSCédric Le Goater } else {
5019ae1329eSCédric Le Goater val &= PHB_Q_DMA_R_QUIESCE_DMA;
5029ae1329eSCédric Le Goater }
5039ae1329eSCédric Le Goater break;
5049ae1329eSCédric Le Goater /* LEM stuff */
5059ae1329eSCédric Le Goater case PHB_LEM_FIR_AND_MASK:
5069ae1329eSCédric Le Goater phb->regs[PHB_LEM_FIR_ACCUM >> 3] &= val;
5079ae1329eSCédric Le Goater return;
5089ae1329eSCédric Le Goater case PHB_LEM_FIR_OR_MASK:
5099ae1329eSCédric Le Goater phb->regs[PHB_LEM_FIR_ACCUM >> 3] |= val;
5109ae1329eSCédric Le Goater return;
5119ae1329eSCédric Le Goater case PHB_LEM_ERROR_AND_MASK:
5129ae1329eSCédric Le Goater phb->regs[PHB_LEM_ERROR_MASK >> 3] &= val;
5139ae1329eSCédric Le Goater return;
5149ae1329eSCédric Le Goater case PHB_LEM_ERROR_OR_MASK:
5159ae1329eSCédric Le Goater phb->regs[PHB_LEM_ERROR_MASK >> 3] |= val;
5169ae1329eSCédric Le Goater return;
5179ae1329eSCédric Le Goater case PHB_LEM_WOF:
5189ae1329eSCédric Le Goater val = 0;
5199ae1329eSCédric Le Goater break;
5209ae1329eSCédric Le Goater }
5219ae1329eSCédric Le Goater
5229ae1329eSCédric Le Goater /* Record whether it changed */
5239ae1329eSCédric Le Goater changed = phb->regs[off >> 3] != val;
5249ae1329eSCédric Le Goater
5259ae1329eSCédric Le Goater /* Store in register cache first */
5269ae1329eSCédric Le Goater phb->regs[off >> 3] = val;
5279ae1329eSCédric Le Goater
5289ae1329eSCédric Le Goater /* Handle side effects */
5299ae1329eSCédric Le Goater switch (off) {
5309ae1329eSCédric Le Goater case PHB_PHB3_CONFIG:
5319ae1329eSCédric Le Goater if (changed) {
5329ae1329eSCédric Le Goater pnv_phb3_update_all_msi_regions(phb);
5339ae1329eSCédric Le Goater }
5349ae1329eSCédric Le Goater /* fall through */
5359ae1329eSCédric Le Goater case PHB_M32_BASE_ADDR:
5369ae1329eSCédric Le Goater case PHB_M32_BASE_MASK:
5379ae1329eSCédric Le Goater case PHB_M32_START_ADDR:
5389ae1329eSCédric Le Goater if (changed) {
5399ae1329eSCédric Le Goater pnv_phb3_check_m32(phb);
5409ae1329eSCédric Le Goater }
5419ae1329eSCédric Le Goater break;
5429ae1329eSCédric Le Goater case PHB_M64_UPPER_BITS:
5439ae1329eSCédric Le Goater if (changed) {
5449ae1329eSCédric Le Goater pnv_phb3_check_all_m64s(phb);
5459ae1329eSCédric Le Goater }
5469ae1329eSCédric Le Goater break;
5479ae1329eSCédric Le Goater case PHB_LSI_SOURCE_ID:
5489ae1329eSCédric Le Goater if (changed) {
5499ae1329eSCédric Le Goater pnv_phb3_lsi_src_id_write(phb, val);
5509ae1329eSCédric Le Goater }
5519ae1329eSCédric Le Goater break;
5529ae1329eSCédric Le Goater
5539ae1329eSCédric Le Goater /* IODA table accesses */
5549ae1329eSCédric Le Goater case PHB_IODA_DATA0:
5559ae1329eSCédric Le Goater pnv_phb3_ioda_write(phb, val);
5569ae1329eSCédric Le Goater break;
5579ae1329eSCédric Le Goater
5589ae1329eSCédric Le Goater /* RTC invalidation */
5599ae1329eSCédric Le Goater case PHB_RTC_INVALIDATE:
5609ae1329eSCédric Le Goater pnv_phb3_rtc_invalidate(phb, val);
5619ae1329eSCédric Le Goater break;
5629ae1329eSCédric Le Goater
5639ae1329eSCédric Le Goater /* FFI request */
5649ae1329eSCédric Le Goater case PHB_FFI_REQUEST:
5659ae1329eSCédric Le Goater pnv_phb3_msi_ffi(&phb->msis, val);
5669ae1329eSCédric Le Goater break;
5679ae1329eSCédric Le Goater
5689ae1329eSCédric Le Goater /* Silent simple writes */
5699ae1329eSCédric Le Goater case PHB_CONFIG_ADDRESS:
5709ae1329eSCédric Le Goater case PHB_IODA_ADDR:
5719ae1329eSCédric Le Goater case PHB_TCE_KILL:
5729ae1329eSCédric Le Goater case PHB_TCE_SPEC_CTL:
5739ae1329eSCédric Le Goater case PHB_PEST_BAR:
5749ae1329eSCédric Le Goater case PHB_PELTV_BAR:
5759ae1329eSCédric Le Goater case PHB_RTT_BAR:
5769ae1329eSCédric Le Goater case PHB_RBA_BAR:
5779ae1329eSCédric Le Goater case PHB_IVT_BAR:
5789ae1329eSCédric Le Goater case PHB_FFI_LOCK:
5799ae1329eSCédric Le Goater case PHB_LEM_FIR_ACCUM:
5809ae1329eSCédric Le Goater case PHB_LEM_ERROR_MASK:
5819ae1329eSCédric Le Goater case PHB_LEM_ACTION0:
5829ae1329eSCédric Le Goater case PHB_LEM_ACTION1:
5839ae1329eSCédric Le Goater break;
5849ae1329eSCédric Le Goater
5859ae1329eSCédric Le Goater /* Noise on anything else */
5869ae1329eSCédric Le Goater default:
5879ae1329eSCédric Le Goater qemu_log_mask(LOG_UNIMP, "phb3: reg_write 0x%"PRIx64"=%"PRIx64"\n",
5889ae1329eSCédric Le Goater off, val);
5899ae1329eSCédric Le Goater }
5909ae1329eSCédric Le Goater }
5919ae1329eSCédric Le Goater
pnv_phb3_reg_read(void * opaque,hwaddr off,unsigned size)5929ae1329eSCédric Le Goater uint64_t pnv_phb3_reg_read(void *opaque, hwaddr off, unsigned size)
5939ae1329eSCédric Le Goater {
5949ae1329eSCédric Le Goater PnvPHB3 *phb = opaque;
5951f5d6b2aSDaniel Henrique Barboza PCIHostState *pci = PCI_HOST_BRIDGE(phb->phb_base);
5969ae1329eSCédric Le Goater uint64_t val;
5979ae1329eSCédric Le Goater
5989ae1329eSCédric Le Goater if ((off & 0xfffc) == PHB_CONFIG_DATA) {
5999ae1329eSCédric Le Goater return pnv_phb3_config_read(phb, off & 0x3, size);
6009ae1329eSCédric Le Goater }
6019ae1329eSCédric Le Goater
6029ae1329eSCédric Le Goater /* Other registers are 64-bit only */
6039ae1329eSCédric Le Goater if (size != 8 || off & 0x7) {
6049ae1329eSCédric Le Goater phb3_error(phb, "Invalid register access, offset: 0x%"PRIx64" size: %d",
6059ae1329eSCédric Le Goater off, size);
6069ae1329eSCédric Le Goater return ~0ull;
6079ae1329eSCédric Le Goater }
6089ae1329eSCédric Le Goater
6099ae1329eSCédric Le Goater /* Default read from cache */
6109ae1329eSCédric Le Goater val = phb->regs[off >> 3];
6119ae1329eSCédric Le Goater
6129ae1329eSCédric Le Goater switch (off) {
6139ae1329eSCédric Le Goater /* Simulate venice DD2.0 */
6149ae1329eSCédric Le Goater case PHB_VERSION:
6159ae1329eSCédric Le Goater return 0x000000a300000005ull;
6169ae1329eSCédric Le Goater case PHB_PCIE_SYSTEM_CONFIG:
6179ae1329eSCédric Le Goater return 0x441100fc30000000;
6189ae1329eSCédric Le Goater
6199ae1329eSCédric Le Goater /* IODA table accesses */
6209ae1329eSCédric Le Goater case PHB_IODA_DATA0:
6219ae1329eSCédric Le Goater return pnv_phb3_ioda_read(phb);
6229ae1329eSCédric Le Goater
6239ae1329eSCédric Le Goater /* Link training always appears trained */
6249ae1329eSCédric Le Goater case PHB_PCIE_DLP_TRAIN_CTL:
6259ae1329eSCédric Le Goater if (!pci_find_device(pci->bus, 1, 0)) {
6269ae1329eSCédric Le Goater return 0;
6279ae1329eSCédric Le Goater }
6289ae1329eSCédric Le Goater return PHB_PCIE_DLP_INBAND_PRESENCE | PHB_PCIE_DLP_TC_DL_LINKACT;
6299ae1329eSCédric Le Goater
6309ae1329eSCédric Le Goater /* FFI Lock */
6319ae1329eSCédric Le Goater case PHB_FFI_LOCK:
6329ae1329eSCédric Le Goater /* Set lock and return previous value */
6339ae1329eSCédric Le Goater phb->regs[off >> 3] |= PHB_FFI_LOCK_STATE;
6349ae1329eSCédric Le Goater return val;
6359ae1329eSCédric Le Goater
6369ae1329eSCédric Le Goater /* DMA read sync: make it look like it's complete */
6379ae1329eSCédric Le Goater case PHB_DMARD_SYNC:
6389ae1329eSCédric Le Goater return PHB_DMARD_SYNC_COMPLETE;
6399ae1329eSCédric Le Goater
6409ae1329eSCédric Le Goater /* Silent simple reads */
6419ae1329eSCédric Le Goater case PHB_PHB3_CONFIG:
6429ae1329eSCédric Le Goater case PHB_M32_BASE_ADDR:
6439ae1329eSCédric Le Goater case PHB_M32_BASE_MASK:
6449ae1329eSCédric Le Goater case PHB_M32_START_ADDR:
6459ae1329eSCédric Le Goater case PHB_CONFIG_ADDRESS:
6469ae1329eSCédric Le Goater case PHB_IODA_ADDR:
6479ae1329eSCédric Le Goater case PHB_RTC_INVALIDATE:
6489ae1329eSCédric Le Goater case PHB_TCE_KILL:
6499ae1329eSCédric Le Goater case PHB_TCE_SPEC_CTL:
6509ae1329eSCédric Le Goater case PHB_PEST_BAR:
6519ae1329eSCédric Le Goater case PHB_PELTV_BAR:
6529ae1329eSCédric Le Goater case PHB_RTT_BAR:
6539ae1329eSCédric Le Goater case PHB_RBA_BAR:
6549ae1329eSCédric Le Goater case PHB_IVT_BAR:
6559ae1329eSCédric Le Goater case PHB_M64_UPPER_BITS:
6569ae1329eSCédric Le Goater case PHB_LEM_FIR_ACCUM:
6579ae1329eSCédric Le Goater case PHB_LEM_ERROR_MASK:
6589ae1329eSCédric Le Goater case PHB_LEM_ACTION0:
6599ae1329eSCédric Le Goater case PHB_LEM_ACTION1:
6609ae1329eSCédric Le Goater break;
6619ae1329eSCédric Le Goater
6629ae1329eSCédric Le Goater /* Noise on anything else */
6639ae1329eSCédric Le Goater default:
6649ae1329eSCédric Le Goater qemu_log_mask(LOG_UNIMP, "phb3: reg_read 0x%"PRIx64"=%"PRIx64"\n",
6659ae1329eSCédric Le Goater off, val);
6669ae1329eSCédric Le Goater }
6679ae1329eSCédric Le Goater return val;
6689ae1329eSCédric Le Goater }
6699ae1329eSCédric Le Goater
6709ae1329eSCédric Le Goater static const MemoryRegionOps pnv_phb3_reg_ops = {
6719ae1329eSCédric Le Goater .read = pnv_phb3_reg_read,
6729ae1329eSCédric Le Goater .write = pnv_phb3_reg_write,
6739ae1329eSCédric Le Goater .valid.min_access_size = 1,
6749ae1329eSCédric Le Goater .valid.max_access_size = 8,
6759ae1329eSCédric Le Goater .impl.min_access_size = 1,
6769ae1329eSCédric Le Goater .impl.max_access_size = 8,
6779ae1329eSCédric Le Goater .endianness = DEVICE_BIG_ENDIAN,
6789ae1329eSCédric Le Goater };
6799ae1329eSCédric Le Goater
pnv_phb3_map_irq(PCIDevice * pci_dev,int irq_num)6809ae1329eSCédric Le Goater static int pnv_phb3_map_irq(PCIDevice *pci_dev, int irq_num)
6819ae1329eSCédric Le Goater {
6829ae1329eSCédric Le Goater /* Check that out properly ... */
6839ae1329eSCédric Le Goater return irq_num & 3;
6849ae1329eSCédric Le Goater }
6859ae1329eSCédric Le Goater
pnv_phb3_set_irq(void * opaque,int irq_num,int level)6869ae1329eSCédric Le Goater static void pnv_phb3_set_irq(void *opaque, int irq_num, int level)
6879ae1329eSCédric Le Goater {
6889ae1329eSCédric Le Goater PnvPHB3 *phb = opaque;
6899ae1329eSCédric Le Goater
6909ae1329eSCédric Le Goater /* LSI only ... */
6919ae1329eSCédric Le Goater if (irq_num > 3) {
6929ae1329eSCédric Le Goater phb3_error(phb, "Unknown IRQ to set %d", irq_num);
6939ae1329eSCédric Le Goater }
6949ae1329eSCédric Le Goater qemu_set_irq(phb->qirqs[irq_num], level);
6959ae1329eSCédric Le Goater }
6969ae1329eSCédric Le Goater
pnv_phb3_resolve_pe(PnvPhb3DMASpace * ds)6979ae1329eSCédric Le Goater static bool pnv_phb3_resolve_pe(PnvPhb3DMASpace *ds)
6989ae1329eSCédric Le Goater {
6999ae1329eSCédric Le Goater uint64_t rtt, addr;
7009ae1329eSCédric Le Goater uint16_t rte;
7019ae1329eSCédric Le Goater int bus_num;
7029ae1329eSCédric Le Goater
7039ae1329eSCédric Le Goater /* Already resolved ? */
7049ae1329eSCédric Le Goater if (ds->pe_num != PHB_INVALID_PE) {
7059ae1329eSCédric Le Goater return true;
7069ae1329eSCédric Le Goater }
7079ae1329eSCédric Le Goater
7089ae1329eSCédric Le Goater /* We need to lookup the RTT */
7099ae1329eSCédric Le Goater rtt = ds->phb->regs[PHB_RTT_BAR >> 3];
7109ae1329eSCédric Le Goater if (!(rtt & PHB_RTT_BAR_ENABLE)) {
7119ae1329eSCédric Le Goater phb3_error(ds->phb, "DMA with RTT BAR disabled !");
7129ae1329eSCédric Le Goater /* Set error bits ? fence ? ... */
7139ae1329eSCédric Le Goater return false;
7149ae1329eSCédric Le Goater }
7159ae1329eSCédric Le Goater
7169ae1329eSCédric Le Goater /* Read RTE */
7179ae1329eSCédric Le Goater bus_num = pci_bus_num(ds->bus);
7189ae1329eSCédric Le Goater addr = rtt & PHB_RTT_BASE_ADDRESS_MASK;
7199ae1329eSCédric Le Goater addr += 2 * ((bus_num << 8) | ds->devfn);
720ba06fe8aSPhilippe Mathieu-Daudé if (dma_memory_read(&address_space_memory, addr, &rte,
721ba06fe8aSPhilippe Mathieu-Daudé sizeof(rte), MEMTXATTRS_UNSPECIFIED)) {
7229ae1329eSCédric Le Goater phb3_error(ds->phb, "Failed to read RTT entry at 0x%"PRIx64, addr);
7239ae1329eSCédric Le Goater /* Set error bits ? fence ? ... */
7249ae1329eSCédric Le Goater return false;
7259ae1329eSCédric Le Goater }
7269ae1329eSCédric Le Goater rte = be16_to_cpu(rte);
7279ae1329eSCédric Le Goater
7289ae1329eSCédric Le Goater /* Fail upon reading of invalid PE# */
7299ae1329eSCédric Le Goater if (rte >= PNV_PHB3_NUM_PE) {
7309ae1329eSCédric Le Goater phb3_error(ds->phb, "RTE for RID 0x%x invalid (%04x", ds->devfn, rte);
7319ae1329eSCédric Le Goater /* Set error bits ? fence ? ... */
7329ae1329eSCédric Le Goater return false;
7339ae1329eSCédric Le Goater }
7349ae1329eSCédric Le Goater ds->pe_num = rte;
7359ae1329eSCédric Le Goater return true;
7369ae1329eSCédric Le Goater }
7379ae1329eSCédric Le Goater
pnv_phb3_translate_tve(PnvPhb3DMASpace * ds,hwaddr addr,bool is_write,uint64_t tve,IOMMUTLBEntry * tlb)7389ae1329eSCédric Le Goater static void pnv_phb3_translate_tve(PnvPhb3DMASpace *ds, hwaddr addr,
7399ae1329eSCédric Le Goater bool is_write, uint64_t tve,
7409ae1329eSCédric Le Goater IOMMUTLBEntry *tlb)
7419ae1329eSCédric Le Goater {
7429ae1329eSCédric Le Goater uint64_t tta = GETFIELD(IODA2_TVT_TABLE_ADDR, tve);
7439ae1329eSCédric Le Goater int32_t lev = GETFIELD(IODA2_TVT_NUM_LEVELS, tve);
7449ae1329eSCédric Le Goater uint32_t tts = GETFIELD(IODA2_TVT_TCE_TABLE_SIZE, tve);
7459ae1329eSCédric Le Goater uint32_t tps = GETFIELD(IODA2_TVT_IO_PSIZE, tve);
7469ae1329eSCédric Le Goater PnvPHB3 *phb = ds->phb;
7479ae1329eSCédric Le Goater
7489ae1329eSCédric Le Goater /* Invalid levels */
7499ae1329eSCédric Le Goater if (lev > 4) {
7509ae1329eSCédric Le Goater phb3_error(phb, "Invalid #levels in TVE %d", lev);
7519ae1329eSCédric Le Goater return;
7529ae1329eSCédric Le Goater }
7539ae1329eSCédric Le Goater
7549ae1329eSCédric Le Goater /* IO Page Size of 0 means untranslated, else use TCEs */
7559ae1329eSCédric Le Goater if (tps == 0) {
7569ae1329eSCédric Le Goater /*
7579ae1329eSCédric Le Goater * We only support non-translate in top window.
7589ae1329eSCédric Le Goater *
7599ae1329eSCédric Le Goater * TODO: Venice/Murano support it on bottom window above 4G and
760f1c0cff8SMichael Tokarev * Naples supports it on everything
7619ae1329eSCédric Le Goater */
7629ae1329eSCédric Le Goater if (!(tve & PPC_BIT(51))) {
7639ae1329eSCédric Le Goater phb3_error(phb, "xlate for invalid non-translate TVE");
7649ae1329eSCédric Le Goater return;
7659ae1329eSCédric Le Goater }
7669ae1329eSCédric Le Goater /* TODO: Handle boundaries */
7679ae1329eSCédric Le Goater
7689ae1329eSCédric Le Goater /* Use 4k pages like q35 ... for now */
7699ae1329eSCédric Le Goater tlb->iova = addr & 0xfffffffffffff000ull;
7709ae1329eSCédric Le Goater tlb->translated_addr = addr & 0x0003fffffffff000ull;
7719ae1329eSCédric Le Goater tlb->addr_mask = 0xfffull;
7729ae1329eSCédric Le Goater tlb->perm = IOMMU_RW;
7739ae1329eSCédric Le Goater } else {
7749ae1329eSCédric Le Goater uint32_t tce_shift, tbl_shift, sh;
7759ae1329eSCédric Le Goater uint64_t base, taddr, tce, tce_mask;
7769ae1329eSCédric Le Goater
7779ae1329eSCédric Le Goater /* TVE disabled ? */
7789ae1329eSCédric Le Goater if (tts == 0) {
7799ae1329eSCédric Le Goater phb3_error(phb, "xlate for invalid translated TVE");
7809ae1329eSCédric Le Goater return;
7819ae1329eSCédric Le Goater }
7829ae1329eSCédric Le Goater
7839ae1329eSCédric Le Goater /* Address bits per bottom level TCE entry */
7849ae1329eSCédric Le Goater tce_shift = tps + 11;
7859ae1329eSCédric Le Goater
7869ae1329eSCédric Le Goater /* Address bits per table level */
7879ae1329eSCédric Le Goater tbl_shift = tts + 8;
7889ae1329eSCédric Le Goater
7899ae1329eSCédric Le Goater /* Top level table base address */
7909ae1329eSCédric Le Goater base = tta << 12;
7919ae1329eSCédric Le Goater
7929ae1329eSCédric Le Goater /* Total shift to first level */
7939ae1329eSCédric Le Goater sh = tbl_shift * lev + tce_shift;
7949ae1329eSCédric Le Goater
7959ae1329eSCédric Le Goater /* TODO: Multi-level untested */
79683d2bea6SDaniel Henrique Barboza do {
79783d2bea6SDaniel Henrique Barboza lev--;
79883d2bea6SDaniel Henrique Barboza
7999ae1329eSCédric Le Goater /* Grab the TCE address */
8009ae1329eSCédric Le Goater taddr = base | (((addr >> sh) & ((1ul << tbl_shift) - 1)) << 3);
8019ae1329eSCédric Le Goater if (dma_memory_read(&address_space_memory, taddr, &tce,
802ba06fe8aSPhilippe Mathieu-Daudé sizeof(tce), MEMTXATTRS_UNSPECIFIED)) {
8039ae1329eSCédric Le Goater phb3_error(phb, "Failed to read TCE at 0x%"PRIx64, taddr);
8049ae1329eSCédric Le Goater return;
8059ae1329eSCédric Le Goater }
8069ae1329eSCédric Le Goater tce = be64_to_cpu(tce);
8079ae1329eSCédric Le Goater
8089ae1329eSCédric Le Goater /* Check permission for indirect TCE */
8099ae1329eSCédric Le Goater if ((lev >= 0) && !(tce & 3)) {
8109ae1329eSCédric Le Goater phb3_error(phb, "Invalid indirect TCE at 0x%"PRIx64, taddr);
8119ae1329eSCédric Le Goater phb3_error(phb, " xlate %"PRIx64":%c TVE=%"PRIx64, addr,
8129ae1329eSCédric Le Goater is_write ? 'W' : 'R', tve);
8139ae1329eSCédric Le Goater phb3_error(phb, " tta=%"PRIx64" lev=%d tts=%d tps=%d",
8149ae1329eSCédric Le Goater tta, lev, tts, tps);
8159ae1329eSCédric Le Goater return;
8169ae1329eSCédric Le Goater }
8179ae1329eSCédric Le Goater sh -= tbl_shift;
8189ae1329eSCédric Le Goater base = tce & ~0xfffull;
81983d2bea6SDaniel Henrique Barboza } while (lev >= 0);
8209ae1329eSCédric Le Goater
8219ae1329eSCédric Le Goater /* We exit the loop with TCE being the final TCE */
8229ae1329eSCédric Le Goater if ((is_write & !(tce & 2)) || ((!is_write) && !(tce & 1))) {
8239ae1329eSCédric Le Goater phb3_error(phb, "TCE access fault at 0x%"PRIx64, taddr);
8249ae1329eSCédric Le Goater phb3_error(phb, " xlate %"PRIx64":%c TVE=%"PRIx64, addr,
8259ae1329eSCédric Le Goater is_write ? 'W' : 'R', tve);
8269ae1329eSCédric Le Goater phb3_error(phb, " tta=%"PRIx64" lev=%d tts=%d tps=%d",
8279ae1329eSCédric Le Goater tta, lev, tts, tps);
82850c8e11aSFrederic Barrat return;
8299ae1329eSCédric Le Goater }
83050c8e11aSFrederic Barrat tce_mask = ~((1ull << tce_shift) - 1);
83150c8e11aSFrederic Barrat tlb->iova = addr & tce_mask;
83250c8e11aSFrederic Barrat tlb->translated_addr = tce & tce_mask;
83350c8e11aSFrederic Barrat tlb->addr_mask = ~tce_mask;
83450c8e11aSFrederic Barrat tlb->perm = tce & 3;
8359ae1329eSCédric Le Goater }
8369ae1329eSCédric Le Goater }
8379ae1329eSCédric Le Goater
pnv_phb3_translate_iommu(IOMMUMemoryRegion * iommu,hwaddr addr,IOMMUAccessFlags flag,int iommu_idx)8389ae1329eSCédric Le Goater static IOMMUTLBEntry pnv_phb3_translate_iommu(IOMMUMemoryRegion *iommu,
8399ae1329eSCédric Le Goater hwaddr addr,
8409ae1329eSCédric Le Goater IOMMUAccessFlags flag,
8419ae1329eSCédric Le Goater int iommu_idx)
8429ae1329eSCédric Le Goater {
8439ae1329eSCédric Le Goater PnvPhb3DMASpace *ds = container_of(iommu, PnvPhb3DMASpace, dma_mr);
8449ae1329eSCédric Le Goater int tve_sel;
8459ae1329eSCédric Le Goater uint64_t tve, cfg;
8469ae1329eSCédric Le Goater IOMMUTLBEntry ret = {
8479ae1329eSCédric Le Goater .target_as = &address_space_memory,
8489ae1329eSCédric Le Goater .iova = addr,
8499ae1329eSCédric Le Goater .translated_addr = 0,
8509ae1329eSCédric Le Goater .addr_mask = ~(hwaddr)0,
8519ae1329eSCédric Le Goater .perm = IOMMU_NONE,
8529ae1329eSCédric Le Goater };
8539ae1329eSCédric Le Goater PnvPHB3 *phb = ds->phb;
8549ae1329eSCédric Le Goater
8559ae1329eSCédric Le Goater /* Resolve PE# */
8569ae1329eSCédric Le Goater if (!pnv_phb3_resolve_pe(ds)) {
8579ae1329eSCédric Le Goater phb3_error(phb, "Failed to resolve PE# for bus @%p (%d) devfn 0x%x",
8589ae1329eSCédric Le Goater ds->bus, pci_bus_num(ds->bus), ds->devfn);
8599ae1329eSCédric Le Goater return ret;
8609ae1329eSCédric Le Goater }
8619ae1329eSCédric Le Goater
8629ae1329eSCédric Le Goater /* Check top bits */
8639ae1329eSCédric Le Goater switch (addr >> 60) {
8649ae1329eSCédric Le Goater case 00:
8659ae1329eSCédric Le Goater /* DMA or 32-bit MSI ? */
8669ae1329eSCédric Le Goater cfg = ds->phb->regs[PHB_PHB3_CONFIG >> 3];
8679ae1329eSCédric Le Goater if ((cfg & PHB_PHB3C_32BIT_MSI_EN) &&
8689ae1329eSCédric Le Goater ((addr & 0xffffffffffff0000ull) == 0xffff0000ull)) {
8699ae1329eSCédric Le Goater phb3_error(phb, "xlate on 32-bit MSI region");
8709ae1329eSCédric Le Goater return ret;
8719ae1329eSCédric Le Goater }
8729ae1329eSCédric Le Goater /* Choose TVE XXX Use PHB3 Control Register */
8739ae1329eSCédric Le Goater tve_sel = (addr >> 59) & 1;
8749ae1329eSCédric Le Goater tve = ds->phb->ioda_TVT[ds->pe_num * 2 + tve_sel];
8759ae1329eSCédric Le Goater pnv_phb3_translate_tve(ds, addr, flag & IOMMU_WO, tve, &ret);
8769ae1329eSCédric Le Goater break;
8779ae1329eSCédric Le Goater case 01:
8789ae1329eSCédric Le Goater phb3_error(phb, "xlate on 64-bit MSI region");
8799ae1329eSCédric Le Goater break;
8809ae1329eSCédric Le Goater default:
8819ae1329eSCédric Le Goater phb3_error(phb, "xlate on unsupported address 0x%"PRIx64, addr);
8829ae1329eSCédric Le Goater }
8839ae1329eSCédric Le Goater return ret;
8849ae1329eSCédric Le Goater }
8859ae1329eSCédric Le Goater
8869ae1329eSCédric Le Goater #define TYPE_PNV_PHB3_IOMMU_MEMORY_REGION "pnv-phb3-iommu-memory-region"
DECLARE_INSTANCE_CHECKER(IOMMUMemoryRegion,PNV_PHB3_IOMMU_MEMORY_REGION,TYPE_PNV_PHB3_IOMMU_MEMORY_REGION)8878110fa1dSEduardo Habkost DECLARE_INSTANCE_CHECKER(IOMMUMemoryRegion, PNV_PHB3_IOMMU_MEMORY_REGION,
8888110fa1dSEduardo Habkost TYPE_PNV_PHB3_IOMMU_MEMORY_REGION)
8899ae1329eSCédric Le Goater
8909ae1329eSCédric Le Goater static void pnv_phb3_iommu_memory_region_class_init(ObjectClass *klass,
8919ae1329eSCédric Le Goater void *data)
8929ae1329eSCédric Le Goater {
8939ae1329eSCédric Le Goater IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_CLASS(klass);
8949ae1329eSCédric Le Goater
8959ae1329eSCédric Le Goater imrc->translate = pnv_phb3_translate_iommu;
8969ae1329eSCédric Le Goater }
8979ae1329eSCédric Le Goater
8989ae1329eSCédric Le Goater static const TypeInfo pnv_phb3_iommu_memory_region_info = {
8999ae1329eSCédric Le Goater .parent = TYPE_IOMMU_MEMORY_REGION,
9009ae1329eSCédric Le Goater .name = TYPE_PNV_PHB3_IOMMU_MEMORY_REGION,
9019ae1329eSCédric Le Goater .class_init = pnv_phb3_iommu_memory_region_class_init,
9029ae1329eSCédric Le Goater };
9039ae1329eSCédric Le Goater
9049ae1329eSCédric Le Goater /*
9059ae1329eSCédric Le Goater * MSI/MSIX memory region implementation.
9069ae1329eSCédric Le Goater * The handler handles both MSI and MSIX.
9079ae1329eSCédric Le Goater */
pnv_phb3_msi_write(void * opaque,hwaddr addr,uint64_t data,unsigned size)9089ae1329eSCédric Le Goater static void pnv_phb3_msi_write(void *opaque, hwaddr addr,
9099ae1329eSCédric Le Goater uint64_t data, unsigned size)
9109ae1329eSCédric Le Goater {
9119ae1329eSCédric Le Goater PnvPhb3DMASpace *ds = opaque;
9129ae1329eSCédric Le Goater
9139ae1329eSCédric Le Goater /* Resolve PE# */
9149ae1329eSCédric Le Goater if (!pnv_phb3_resolve_pe(ds)) {
9159ae1329eSCédric Le Goater phb3_error(ds->phb, "Failed to resolve PE# for bus @%p (%d) devfn 0x%x",
9169ae1329eSCédric Le Goater ds->bus, pci_bus_num(ds->bus), ds->devfn);
9179ae1329eSCédric Le Goater return;
9189ae1329eSCédric Le Goater }
9199ae1329eSCédric Le Goater
9209ae1329eSCédric Le Goater pnv_phb3_msi_send(&ds->phb->msis, addr, data, ds->pe_num);
9219ae1329eSCédric Le Goater }
9229ae1329eSCédric Le Goater
9239ae1329eSCédric Le Goater /* There is no .read as the read result is undefined by PCI spec */
pnv_phb3_msi_read(void * opaque,hwaddr addr,unsigned size)9249ae1329eSCédric Le Goater static uint64_t pnv_phb3_msi_read(void *opaque, hwaddr addr, unsigned size)
9259ae1329eSCédric Le Goater {
9269ae1329eSCédric Le Goater PnvPhb3DMASpace *ds = opaque;
9279ae1329eSCédric Le Goater
9289ae1329eSCédric Le Goater phb3_error(ds->phb, "invalid read @ 0x%" HWADDR_PRIx, addr);
9299ae1329eSCédric Le Goater return -1;
9309ae1329eSCédric Le Goater }
9319ae1329eSCédric Le Goater
9329ae1329eSCédric Le Goater static const MemoryRegionOps pnv_phb3_msi_ops = {
9339ae1329eSCédric Le Goater .read = pnv_phb3_msi_read,
9349ae1329eSCédric Le Goater .write = pnv_phb3_msi_write,
9359ae1329eSCédric Le Goater .endianness = DEVICE_LITTLE_ENDIAN
9369ae1329eSCédric Le Goater };
9379ae1329eSCédric Le Goater
pnv_phb3_dma_iommu(PCIBus * bus,void * opaque,int devfn)9389ae1329eSCédric Le Goater static AddressSpace *pnv_phb3_dma_iommu(PCIBus *bus, void *opaque, int devfn)
9399ae1329eSCédric Le Goater {
9409ae1329eSCédric Le Goater PnvPHB3 *phb = opaque;
9419ae1329eSCédric Le Goater PnvPhb3DMASpace *ds;
9429ae1329eSCédric Le Goater
9439ae1329eSCédric Le Goater QLIST_FOREACH(ds, &phb->dma_spaces, list) {
9449ae1329eSCédric Le Goater if (ds->bus == bus && ds->devfn == devfn) {
9459ae1329eSCédric Le Goater break;
9469ae1329eSCédric Le Goater }
9479ae1329eSCédric Le Goater }
9489ae1329eSCédric Le Goater
9499ae1329eSCédric Le Goater if (ds == NULL) {
950b21e2380SMarkus Armbruster ds = g_new0(PnvPhb3DMASpace, 1);
9519ae1329eSCédric Le Goater ds->bus = bus;
9529ae1329eSCédric Le Goater ds->devfn = devfn;
9539ae1329eSCédric Le Goater ds->pe_num = PHB_INVALID_PE;
9549ae1329eSCédric Le Goater ds->phb = phb;
9559ae1329eSCédric Le Goater memory_region_init_iommu(&ds->dma_mr, sizeof(ds->dma_mr),
9569ae1329eSCédric Le Goater TYPE_PNV_PHB3_IOMMU_MEMORY_REGION,
9579ae1329eSCédric Le Goater OBJECT(phb), "phb3_iommu", UINT64_MAX);
9589ae1329eSCédric Le Goater address_space_init(&ds->dma_as, MEMORY_REGION(&ds->dma_mr),
9599ae1329eSCédric Le Goater "phb3_iommu");
9609ae1329eSCédric Le Goater memory_region_init_io(&ds->msi32_mr, OBJECT(phb), &pnv_phb3_msi_ops,
9619ae1329eSCédric Le Goater ds, "msi32", 0x10000);
9629ae1329eSCédric Le Goater memory_region_init_io(&ds->msi64_mr, OBJECT(phb), &pnv_phb3_msi_ops,
9639ae1329eSCédric Le Goater ds, "msi64", 0x100000);
9649ae1329eSCédric Le Goater pnv_phb3_update_msi_regions(ds);
9659ae1329eSCédric Le Goater
9669ae1329eSCédric Le Goater QLIST_INSERT_HEAD(&phb->dma_spaces, ds, list);
9679ae1329eSCédric Le Goater }
9689ae1329eSCédric Le Goater return &ds->dma_as;
9699ae1329eSCédric Le Goater }
9709ae1329eSCédric Le Goater
971*ba7d12ebSYi Liu static PCIIOMMUOps pnv_phb3_iommu_ops = {
972*ba7d12ebSYi Liu .get_address_space = pnv_phb3_dma_iommu,
973*ba7d12ebSYi Liu };
974*ba7d12ebSYi Liu
pnv_phb3_instance_init(Object * obj)9759ae1329eSCédric Le Goater static void pnv_phb3_instance_init(Object *obj)
9769ae1329eSCédric Le Goater {
9779ae1329eSCédric Le Goater PnvPHB3 *phb = PNV_PHB3(obj);
9789ae1329eSCédric Le Goater
9799ae1329eSCédric Le Goater QLIST_INIT(&phb->dma_spaces);
9809ae1329eSCédric Le Goater
9819ae1329eSCédric Le Goater /* LSI sources */
9829fc7fc4dSMarkus Armbruster object_initialize_child(obj, "lsi", &phb->lsis, TYPE_ICS);
9839ae1329eSCédric Le Goater
9849ae1329eSCédric Le Goater /* Default init ... will be fixed by HW inits */
9859ae1329eSCédric Le Goater phb->lsis.offset = 0;
9869ae1329eSCédric Le Goater
9879ae1329eSCédric Le Goater /* MSI sources */
9889fc7fc4dSMarkus Armbruster object_initialize_child(obj, "msi", &phb->msis, TYPE_PHB3_MSI);
9899ae1329eSCédric Le Goater
9909ae1329eSCédric Le Goater /* Power Bus Common Queue */
9919fc7fc4dSMarkus Armbruster object_initialize_child(obj, "pbcq", &phb->pbcq, TYPE_PNV_PBCQ);
9929ae1329eSCédric Le Goater
9939ae1329eSCédric Le Goater }
9949ae1329eSCédric Le Goater
pnv_phb3_bus_init(DeviceState * dev,PnvPHB3 * phb)99591bcee71SDaniel Henrique Barboza void pnv_phb3_bus_init(DeviceState *dev, PnvPHB3 *phb)
99691bcee71SDaniel Henrique Barboza {
99791bcee71SDaniel Henrique Barboza PCIHostState *pci = PCI_HOST_BRIDGE(dev);
99891bcee71SDaniel Henrique Barboza
99991bcee71SDaniel Henrique Barboza /*
100091bcee71SDaniel Henrique Barboza * PHB3 doesn't support IO space. However, qemu gets very upset if
100191bcee71SDaniel Henrique Barboza * we don't have an IO region to anchor IO BARs onto so we just
100291bcee71SDaniel Henrique Barboza * initialize one which we never hook up to anything
100391bcee71SDaniel Henrique Barboza */
100491bcee71SDaniel Henrique Barboza memory_region_init(&phb->pci_io, OBJECT(phb), "pci-io", 0x10000);
100591bcee71SDaniel Henrique Barboza memory_region_init(&phb->pci_mmio, OBJECT(phb), "pci-mmio",
100691bcee71SDaniel Henrique Barboza PCI_MMIO_TOTAL_SIZE);
100791bcee71SDaniel Henrique Barboza
100891bcee71SDaniel Henrique Barboza pci->bus = pci_register_root_bus(dev,
100991bcee71SDaniel Henrique Barboza dev->id ? dev->id : NULL,
101091bcee71SDaniel Henrique Barboza pnv_phb3_set_irq, pnv_phb3_map_irq, phb,
101191bcee71SDaniel Henrique Barboza &phb->pci_mmio, &phb->pci_io,
101291bcee71SDaniel Henrique Barboza 0, 4, TYPE_PNV_PHB3_ROOT_BUS);
101391bcee71SDaniel Henrique Barboza
10148ec1e4f1SDaniel Henrique Barboza object_property_set_int(OBJECT(pci->bus), "phb-id", phb->phb_id,
10158ec1e4f1SDaniel Henrique Barboza &error_abort);
10168ec1e4f1SDaniel Henrique Barboza object_property_set_int(OBJECT(pci->bus), "chip-id", phb->chip_id,
10178ec1e4f1SDaniel Henrique Barboza &error_abort);
10188ec1e4f1SDaniel Henrique Barboza
1019*ba7d12ebSYi Liu pci_setup_iommu(pci->bus, &pnv_phb3_iommu_ops, phb);
102091bcee71SDaniel Henrique Barboza }
102191bcee71SDaniel Henrique Barboza
pnv_phb3_realize(DeviceState * dev,Error ** errp)10229ae1329eSCédric Le Goater static void pnv_phb3_realize(DeviceState *dev, Error **errp)
10239ae1329eSCédric Le Goater {
10249ae1329eSCédric Le Goater PnvPHB3 *phb = PNV_PHB3(dev);
10259ae1329eSCédric Le Goater PnvMachineState *pnv = PNV_MACHINE(qdev_get_machine());
10269ae1329eSCédric Le Goater int i;
10279ae1329eSCédric Le Goater
1028a8fa95c7SCédric Le Goater if (phb->phb_id >= PNV_CHIP_GET_CLASS(phb->chip)->num_phbs) {
10299ae1329eSCédric Le Goater error_setg(errp, "invalid PHB index: %d", phb->phb_id);
10309ae1329eSCédric Le Goater return;
10319ae1329eSCédric Le Goater }
10329ae1329eSCédric Le Goater
10339ae1329eSCédric Le Goater /* LSI sources */
10345325cc34SMarkus Armbruster object_property_set_link(OBJECT(&phb->lsis), "xics", OBJECT(pnv),
10359ae1329eSCédric Le Goater &error_abort);
10365325cc34SMarkus Armbruster object_property_set_int(OBJECT(&phb->lsis), "nr-irqs", PNV_PHB3_NUM_LSI,
10379ae1329eSCédric Le Goater &error_abort);
1038668f62ecSMarkus Armbruster if (!qdev_realize(DEVICE(&phb->lsis), NULL, errp)) {
10399ae1329eSCédric Le Goater return;
10409ae1329eSCédric Le Goater }
10419ae1329eSCédric Le Goater
10429ae1329eSCédric Le Goater for (i = 0; i < phb->lsis.nr_irqs; i++) {
10439ae1329eSCédric Le Goater ics_set_irq_type(&phb->lsis, i, true);
10449ae1329eSCédric Le Goater }
10459ae1329eSCédric Le Goater
10469ae1329eSCédric Le Goater phb->qirqs = qemu_allocate_irqs(ics_set_irq, &phb->lsis, phb->lsis.nr_irqs);
10479ae1329eSCédric Le Goater
10489ae1329eSCédric Le Goater /* MSI sources */
10495325cc34SMarkus Armbruster object_property_set_link(OBJECT(&phb->msis), "phb", OBJECT(phb),
10509ae1329eSCédric Le Goater &error_abort);
10515325cc34SMarkus Armbruster object_property_set_link(OBJECT(&phb->msis), "xics", OBJECT(pnv),
10529ae1329eSCédric Le Goater &error_abort);
10535325cc34SMarkus Armbruster object_property_set_int(OBJECT(&phb->msis), "nr-irqs", PHB3_MAX_MSI,
10549ae1329eSCédric Le Goater &error_abort);
1055668f62ecSMarkus Armbruster if (!qdev_realize(DEVICE(&phb->msis), NULL, errp)) {
10569ae1329eSCédric Le Goater return;
10579ae1329eSCédric Le Goater }
10589ae1329eSCédric Le Goater
10599ae1329eSCédric Le Goater /* Power Bus Common Queue */
10605325cc34SMarkus Armbruster object_property_set_link(OBJECT(&phb->pbcq), "phb", OBJECT(phb),
10619ae1329eSCédric Le Goater &error_abort);
1062668f62ecSMarkus Armbruster if (!qdev_realize(DEVICE(&phb->pbcq), NULL, errp)) {
10639ae1329eSCédric Le Goater return;
10649ae1329eSCédric Le Goater }
10659ae1329eSCédric Le Goater
10669ae1329eSCédric Le Goater /* Controller Registers */
10679ae1329eSCédric Le Goater memory_region_init_io(&phb->mr_regs, OBJECT(phb), &pnv_phb3_reg_ops, phb,
10689ae1329eSCédric Le Goater "phb3-regs", 0x1000);
10699ae1329eSCédric Le Goater }
10709ae1329eSCédric Le Goater
pnv_phb3_update_regions(PnvPHB3 * phb)10719ae1329eSCédric Le Goater void pnv_phb3_update_regions(PnvPHB3 *phb)
10729ae1329eSCédric Le Goater {
10739ae1329eSCédric Le Goater PnvPBCQState *pbcq = &phb->pbcq;
10749ae1329eSCédric Le Goater
10759ae1329eSCédric Le Goater /* Unmap first always */
10769ae1329eSCédric Le Goater if (memory_region_is_mapped(&phb->mr_regs)) {
10779ae1329eSCédric Le Goater memory_region_del_subregion(&pbcq->phbbar, &phb->mr_regs);
10789ae1329eSCédric Le Goater }
10799ae1329eSCédric Le Goater
10809ae1329eSCédric Le Goater /* Map registers if enabled */
10819ae1329eSCédric Le Goater if (memory_region_is_mapped(&pbcq->phbbar)) {
10829ae1329eSCédric Le Goater /* TODO: We should use the PHB BAR 2 register but we don't ... */
10839ae1329eSCédric Le Goater memory_region_add_subregion(&pbcq->phbbar, 0, &phb->mr_regs);
10849ae1329eSCédric Le Goater }
10859ae1329eSCédric Le Goater
10869ae1329eSCédric Le Goater /* Check/update m32 */
10879ae1329eSCédric Le Goater if (memory_region_is_mapped(&phb->mr_m32)) {
10889ae1329eSCédric Le Goater pnv_phb3_check_m32(phb);
10899ae1329eSCédric Le Goater }
10909ae1329eSCédric Le Goater pnv_phb3_check_all_m64s(phb);
10919ae1329eSCédric Le Goater }
10929ae1329eSCédric Le Goater
10939ae1329eSCédric Le Goater static Property pnv_phb3_properties[] = {
10949ae1329eSCédric Le Goater DEFINE_PROP_UINT32("index", PnvPHB3, phb_id, 0),
10959ae1329eSCédric Le Goater DEFINE_PROP_UINT32("chip-id", PnvPHB3, chip_id, 0),
10962c4d3a50SCédric Le Goater DEFINE_PROP_LINK("chip", PnvPHB3, chip, TYPE_PNV_CHIP, PnvChip *),
10971f5d6b2aSDaniel Henrique Barboza DEFINE_PROP_LINK("phb-base", PnvPHB3, phb_base, TYPE_PNV_PHB, PnvPHB *),
10989ae1329eSCédric Le Goater DEFINE_PROP_END_OF_LIST(),
10999ae1329eSCédric Le Goater };
11009ae1329eSCédric Le Goater
pnv_phb3_class_init(ObjectClass * klass,void * data)11019ae1329eSCédric Le Goater static void pnv_phb3_class_init(ObjectClass *klass, void *data)
11029ae1329eSCédric Le Goater {
11039ae1329eSCédric Le Goater DeviceClass *dc = DEVICE_CLASS(klass);
11049ae1329eSCédric Le Goater
11059ae1329eSCédric Le Goater dc->realize = pnv_phb3_realize;
11069ae1329eSCédric Le Goater device_class_set_props(dc, pnv_phb3_properties);
11079c10d86fSCédric Le Goater dc->user_creatable = false;
11089ae1329eSCédric Le Goater }
11099ae1329eSCédric Le Goater
11109ae1329eSCédric Le Goater static const TypeInfo pnv_phb3_type_info = {
11119ae1329eSCédric Le Goater .name = TYPE_PNV_PHB3,
11121f5d6b2aSDaniel Henrique Barboza .parent = TYPE_DEVICE,
11139ae1329eSCédric Le Goater .instance_size = sizeof(PnvPHB3),
11149ae1329eSCédric Le Goater .class_init = pnv_phb3_class_init,
11159ae1329eSCédric Le Goater .instance_init = pnv_phb3_instance_init,
11169ae1329eSCédric Le Goater };
11179ae1329eSCédric Le Goater
pnv_phb3_root_bus_get_prop(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)11188ec1e4f1SDaniel Henrique Barboza static void pnv_phb3_root_bus_get_prop(Object *obj, Visitor *v,
11198ec1e4f1SDaniel Henrique Barboza const char *name,
11208ec1e4f1SDaniel Henrique Barboza void *opaque, Error **errp)
11218ec1e4f1SDaniel Henrique Barboza {
11228ec1e4f1SDaniel Henrique Barboza PnvPHB3RootBus *bus = PNV_PHB3_ROOT_BUS(obj);
11238ec1e4f1SDaniel Henrique Barboza uint64_t value = 0;
11248ec1e4f1SDaniel Henrique Barboza
11258ec1e4f1SDaniel Henrique Barboza if (strcmp(name, "phb-id") == 0) {
11268ec1e4f1SDaniel Henrique Barboza value = bus->phb_id;
11278ec1e4f1SDaniel Henrique Barboza } else {
11288ec1e4f1SDaniel Henrique Barboza value = bus->chip_id;
11298ec1e4f1SDaniel Henrique Barboza }
11308ec1e4f1SDaniel Henrique Barboza
11318ec1e4f1SDaniel Henrique Barboza visit_type_size(v, name, &value, errp);
11328ec1e4f1SDaniel Henrique Barboza }
11338ec1e4f1SDaniel Henrique Barboza
pnv_phb3_root_bus_set_prop(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)11348ec1e4f1SDaniel Henrique Barboza static void pnv_phb3_root_bus_set_prop(Object *obj, Visitor *v,
11358ec1e4f1SDaniel Henrique Barboza const char *name,
11368ec1e4f1SDaniel Henrique Barboza void *opaque, Error **errp)
11378ec1e4f1SDaniel Henrique Barboza
11388ec1e4f1SDaniel Henrique Barboza {
11398ec1e4f1SDaniel Henrique Barboza PnvPHB3RootBus *bus = PNV_PHB3_ROOT_BUS(obj);
11408ec1e4f1SDaniel Henrique Barboza uint64_t value;
11418ec1e4f1SDaniel Henrique Barboza
11428ec1e4f1SDaniel Henrique Barboza if (!visit_type_size(v, name, &value, errp)) {
11438ec1e4f1SDaniel Henrique Barboza return;
11448ec1e4f1SDaniel Henrique Barboza }
11458ec1e4f1SDaniel Henrique Barboza
11468ec1e4f1SDaniel Henrique Barboza if (strcmp(name, "phb-id") == 0) {
11478ec1e4f1SDaniel Henrique Barboza bus->phb_id = value;
11488ec1e4f1SDaniel Henrique Barboza } else {
11498ec1e4f1SDaniel Henrique Barboza bus->chip_id = value;
11508ec1e4f1SDaniel Henrique Barboza }
11518ec1e4f1SDaniel Henrique Barboza }
11528ec1e4f1SDaniel Henrique Barboza
pnv_phb3_root_bus_class_init(ObjectClass * klass,void * data)11539ae1329eSCédric Le Goater static void pnv_phb3_root_bus_class_init(ObjectClass *klass, void *data)
11549ae1329eSCédric Le Goater {
11559ae1329eSCédric Le Goater BusClass *k = BUS_CLASS(klass);
11569ae1329eSCédric Le Goater
11578ec1e4f1SDaniel Henrique Barboza object_class_property_add(klass, "phb-id", "int",
11588ec1e4f1SDaniel Henrique Barboza pnv_phb3_root_bus_get_prop,
11598ec1e4f1SDaniel Henrique Barboza pnv_phb3_root_bus_set_prop,
11608ec1e4f1SDaniel Henrique Barboza NULL, NULL);
11618ec1e4f1SDaniel Henrique Barboza
11628ec1e4f1SDaniel Henrique Barboza object_class_property_add(klass, "chip-id", "int",
11638ec1e4f1SDaniel Henrique Barboza pnv_phb3_root_bus_get_prop,
11648ec1e4f1SDaniel Henrique Barboza pnv_phb3_root_bus_set_prop,
11658ec1e4f1SDaniel Henrique Barboza NULL, NULL);
11668ec1e4f1SDaniel Henrique Barboza
11679ae1329eSCédric Le Goater /*
11689ae1329eSCédric Le Goater * PHB3 has only a single root complex. Enforce the limit on the
11699ae1329eSCédric Le Goater * parent bus
11709ae1329eSCédric Le Goater */
11719ae1329eSCédric Le Goater k->max_dev = 1;
11729ae1329eSCédric Le Goater }
11739ae1329eSCédric Le Goater
11749ae1329eSCédric Le Goater static const TypeInfo pnv_phb3_root_bus_info = {
11759ae1329eSCédric Le Goater .name = TYPE_PNV_PHB3_ROOT_BUS,
11769ae1329eSCédric Le Goater .parent = TYPE_PCIE_BUS,
117790865af7SXuzhou Cheng .instance_size = sizeof(PnvPHB3RootBus),
11789ae1329eSCédric Le Goater .class_init = pnv_phb3_root_bus_class_init,
11799ae1329eSCédric Le Goater };
11809ae1329eSCédric Le Goater
pnv_phb3_register_types(void)11819ae1329eSCédric Le Goater static void pnv_phb3_register_types(void)
11829ae1329eSCédric Le Goater {
11839ae1329eSCédric Le Goater type_register_static(&pnv_phb3_root_bus_info);
11849ae1329eSCédric Le Goater type_register_static(&pnv_phb3_type_info);
11859ae1329eSCédric Le Goater type_register_static(&pnv_phb3_iommu_memory_region_info);
11869ae1329eSCédric Le Goater }
11879ae1329eSCédric Le Goater
11889ae1329eSCédric Le Goater type_init(pnv_phb3_register_types)
1189