1 /* 2 * QEMU PowerPC PowerNV (POWER9) PHB4 model 3 * 4 * Copyright (c) 2018-2020, IBM Corporation. 5 * 6 * This code is licensed under the GPL version 2 or later. See the 7 * COPYING file in the top-level directory. 8 */ 9 #include "qemu/osdep.h" 10 #include "qapi/error.h" 11 #include "qemu/log.h" 12 #include "target/ppc/cpu.h" 13 #include "hw/ppc/fdt.h" 14 #include "hw/pci-host/pnv_phb4_regs.h" 15 #include "hw/pci-host/pnv_phb4.h" 16 #include "hw/ppc/pnv_xscom.h" 17 #include "hw/pci/pci_bridge.h" 18 #include "hw/pci/pci_bus.h" 19 #include "hw/ppc/pnv.h" 20 #include "hw/ppc/pnv_chip.h" 21 #include "hw/qdev-properties.h" 22 #include "sysemu/sysemu.h" 23 24 #include <libfdt.h> 25 26 #define phb_pec_error(pec, fmt, ...) \ 27 qemu_log_mask(LOG_GUEST_ERROR, "phb4_pec[%d:%d]: " fmt "\n", \ 28 (pec)->chip_id, (pec)->index, ## __VA_ARGS__) 29 30 31 static uint64_t pnv_pec_nest_xscom_read(void *opaque, hwaddr addr, 32 unsigned size) 33 { 34 PnvPhb4PecState *pec = PNV_PHB4_PEC(opaque); 35 uint32_t reg = addr >> 3; 36 37 /* TODO: add list of allowed registers and error out if not */ 38 return pec->nest_regs[reg]; 39 } 40 41 static void pnv_pec_nest_xscom_write(void *opaque, hwaddr addr, 42 uint64_t val, unsigned size) 43 { 44 PnvPhb4PecState *pec = PNV_PHB4_PEC(opaque); 45 uint32_t reg = addr >> 3; 46 47 switch (reg) { 48 case PEC_NEST_PBCQ_HW_CONFIG: 49 case PEC_NEST_DROP_PRIO_CTRL: 50 case PEC_NEST_PBCQ_ERR_INJECT: 51 case PEC_NEST_PCI_NEST_CLK_TRACE_CTL: 52 case PEC_NEST_PBCQ_PMON_CTRL: 53 case PEC_NEST_PBCQ_PBUS_ADDR_EXT: 54 case PEC_NEST_PBCQ_PRED_VEC_TIMEOUT: 55 case PEC_NEST_CAPP_CTRL: 56 case PEC_NEST_PBCQ_READ_STK_OVR: 57 case PEC_NEST_PBCQ_WRITE_STK_OVR: 58 case PEC_NEST_PBCQ_STORE_STK_OVR: 59 case PEC_NEST_PBCQ_RETRY_BKOFF_CTRL: 60 pec->nest_regs[reg] = val; 61 break; 62 default: 63 phb_pec_error(pec, "%s @0x%"HWADDR_PRIx"=%"PRIx64"\n", __func__, 64 addr, val); 65 } 66 } 67 68 static const MemoryRegionOps pnv_pec_nest_xscom_ops = { 69 .read = pnv_pec_nest_xscom_read, 70 .write = pnv_pec_nest_xscom_write, 71 .valid.min_access_size = 8, 72 .valid.max_access_size = 8, 73 .impl.min_access_size = 8, 74 .impl.max_access_size = 8, 75 .endianness = DEVICE_BIG_ENDIAN, 76 }; 77 78 static uint64_t pnv_pec_pci_xscom_read(void *opaque, hwaddr addr, 79 unsigned size) 80 { 81 PnvPhb4PecState *pec = PNV_PHB4_PEC(opaque); 82 uint32_t reg = addr >> 3; 83 84 /* TODO: add list of allowed registers and error out if not */ 85 return pec->pci_regs[reg]; 86 } 87 88 static void pnv_pec_pci_xscom_write(void *opaque, hwaddr addr, 89 uint64_t val, unsigned size) 90 { 91 PnvPhb4PecState *pec = PNV_PHB4_PEC(opaque); 92 uint32_t reg = addr >> 3; 93 94 switch (reg) { 95 case PEC_PCI_PBAIB_HW_CONFIG: 96 case PEC_PCI_PBAIB_READ_STK_OVR: 97 pec->pci_regs[reg] = val; 98 break; 99 default: 100 phb_pec_error(pec, "%s @0x%"HWADDR_PRIx"=%"PRIx64"\n", __func__, 101 addr, val); 102 } 103 } 104 105 static const MemoryRegionOps pnv_pec_pci_xscom_ops = { 106 .read = pnv_pec_pci_xscom_read, 107 .write = pnv_pec_pci_xscom_write, 108 .valid.min_access_size = 8, 109 .valid.max_access_size = 8, 110 .impl.min_access_size = 8, 111 .impl.max_access_size = 8, 112 .endianness = DEVICE_BIG_ENDIAN, 113 }; 114 115 PnvPhb4PecState *pnv_phb4_get_pec(PnvChip *chip, PnvPHB *phb, Error **errp) 116 { 117 PnvPhb4PecState *pecs = NULL; 118 int chip_id = phb->chip_id; 119 int index = phb->phb_id; 120 int i, j; 121 122 if (phb->version == 4) { 123 Pnv9Chip *chip9 = PNV9_CHIP(chip); 124 125 pecs = chip9->pecs; 126 } else if (phb->version == 5) { 127 Pnv10Chip *chip10 = PNV10_CHIP(chip); 128 129 pecs = chip10->pecs; 130 } else { 131 g_assert_not_reached(); 132 } 133 134 for (i = 0; i < chip->num_pecs; i++) { 135 /* 136 * For each PEC, check the amount of phbs it supports 137 * and see if the given phb4 index matches an index. 138 */ 139 PnvPhb4PecState *pec = &pecs[i]; 140 141 for (j = 0; j < pec->num_phbs; j++) { 142 if (index == pnv_phb4_pec_get_phb_id(pec, j)) { 143 pec->phbs[j] = phb; 144 return pec; 145 } 146 } 147 } 148 error_setg(errp, 149 "pnv-phb4 chip-id %d index %d didn't match any existing PEC", 150 chip_id, index); 151 152 return NULL; 153 } 154 155 static PnvPHB *pnv_pec_default_phb_realize(PnvPhb4PecState *pec, 156 int stack_no, 157 Error **errp) 158 { 159 PnvPHB *phb = PNV_PHB(qdev_new(TYPE_PNV_PHB)); 160 int phb_id = pnv_phb4_pec_get_phb_id(pec, stack_no); 161 162 object_property_add_child(OBJECT(pec), "phb[*]", OBJECT(phb)); 163 object_property_set_link(OBJECT(phb), "pec", OBJECT(pec), 164 &error_abort); 165 object_property_set_int(OBJECT(phb), "chip-id", pec->chip_id, 166 &error_fatal); 167 object_property_set_int(OBJECT(phb), "index", phb_id, 168 &error_fatal); 169 170 if (!sysbus_realize(SYS_BUS_DEVICE(phb), errp)) { 171 return NULL; 172 } 173 return phb; 174 } 175 176 static void pnv_pec_realize(DeviceState *dev, Error **errp) 177 { 178 PnvPhb4PecState *pec = PNV_PHB4_PEC(dev); 179 PnvPhb4PecClass *pecc = PNV_PHB4_PEC_GET_CLASS(pec); 180 char name[64]; 181 int i; 182 183 if (pec->index >= PNV_CHIP_GET_CLASS(pec->chip)->num_pecs) { 184 error_setg(errp, "invalid PEC index: %d", pec->index); 185 return; 186 } 187 188 pec->num_phbs = pecc->num_phbs[pec->index]; 189 190 /* Create PHBs if running with defaults */ 191 if (defaults_enabled()) { 192 g_assert(pec->num_phbs <= MAX_PHBS_PER_PEC); 193 for (i = 0; i < pec->num_phbs; i++) { 194 pec->phbs[i] = pnv_pec_default_phb_realize(pec, i, errp); 195 } 196 } 197 198 /* Initialize the XSCOM regions for the PEC registers */ 199 snprintf(name, sizeof(name), "xscom-pec-%d.%d-nest", pec->chip_id, 200 pec->index); 201 pnv_xscom_region_init(&pec->nest_regs_mr, OBJECT(dev), 202 &pnv_pec_nest_xscom_ops, pec, name, 203 PHB4_PEC_NEST_REGS_COUNT); 204 205 snprintf(name, sizeof(name), "xscom-pec-%d.%d-pci", pec->chip_id, 206 pec->index); 207 pnv_xscom_region_init(&pec->pci_regs_mr, OBJECT(dev), 208 &pnv_pec_pci_xscom_ops, pec, name, 209 PHB4_PEC_PCI_REGS_COUNT); 210 } 211 212 static int pnv_pec_dt_xscom(PnvXScomInterface *dev, void *fdt, 213 int xscom_offset) 214 { 215 PnvPhb4PecState *pec = PNV_PHB4_PEC(dev); 216 PnvPhb4PecClass *pecc = PNV_PHB4_PEC_GET_CLASS(dev); 217 uint32_t nbase = pecc->xscom_nest_base(pec); 218 uint32_t pbase = pecc->xscom_pci_base(pec); 219 int offset, i; 220 char *name; 221 uint32_t reg[] = { 222 cpu_to_be32(nbase), 223 cpu_to_be32(pecc->xscom_nest_size), 224 cpu_to_be32(pbase), 225 cpu_to_be32(pecc->xscom_pci_size), 226 }; 227 228 name = g_strdup_printf("pbcq@%x", nbase); 229 offset = fdt_add_subnode(fdt, xscom_offset, name); 230 _FDT(offset); 231 g_free(name); 232 233 _FDT((fdt_setprop(fdt, offset, "reg", reg, sizeof(reg)))); 234 235 _FDT((fdt_setprop_cell(fdt, offset, "ibm,pec-index", pec->index))); 236 _FDT((fdt_setprop_cell(fdt, offset, "#address-cells", 1))); 237 _FDT((fdt_setprop_cell(fdt, offset, "#size-cells", 0))); 238 _FDT((fdt_setprop(fdt, offset, "compatible", pecc->compat, 239 pecc->compat_size))); 240 241 for (i = 0; i < pec->num_phbs; i++) { 242 int stk_offset; 243 244 if (!pec->phbs[i]) { 245 continue; 246 } 247 248 name = g_strdup_printf("stack@%x", i); 249 stk_offset = fdt_add_subnode(fdt, offset, name); 250 _FDT(stk_offset); 251 g_free(name); 252 _FDT((fdt_setprop(fdt, stk_offset, "compatible", pecc->stk_compat, 253 pecc->stk_compat_size))); 254 _FDT((fdt_setprop_cell(fdt, stk_offset, "reg", i))); 255 _FDT((fdt_setprop_cell(fdt, stk_offset, "ibm,phb-index", 256 pec->phbs[i]->phb_id))); 257 } 258 259 return 0; 260 } 261 262 static Property pnv_pec_properties[] = { 263 DEFINE_PROP_UINT32("index", PnvPhb4PecState, index, 0), 264 DEFINE_PROP_UINT32("chip-id", PnvPhb4PecState, chip_id, 0), 265 DEFINE_PROP_LINK("chip", PnvPhb4PecState, chip, TYPE_PNV_CHIP, 266 PnvChip *), 267 DEFINE_PROP_END_OF_LIST(), 268 }; 269 270 static uint32_t pnv_pec_xscom_pci_base(PnvPhb4PecState *pec) 271 { 272 return PNV9_XSCOM_PEC_PCI_BASE + 0x1000000 * pec->index; 273 } 274 275 static uint32_t pnv_pec_xscom_nest_base(PnvPhb4PecState *pec) 276 { 277 return PNV9_XSCOM_PEC_NEST_BASE + 0x400 * pec->index; 278 } 279 280 /* 281 * PEC0 -> 1 phb 282 * PEC1 -> 2 phb 283 * PEC2 -> 3 phbs 284 */ 285 static const uint32_t pnv_pec_num_phbs[] = { 1, 2, 3 }; 286 287 static void pnv_pec_class_init(ObjectClass *klass, void *data) 288 { 289 DeviceClass *dc = DEVICE_CLASS(klass); 290 PnvXScomInterfaceClass *xdc = PNV_XSCOM_INTERFACE_CLASS(klass); 291 PnvPhb4PecClass *pecc = PNV_PHB4_PEC_CLASS(klass); 292 static const char compat[] = "ibm,power9-pbcq"; 293 static const char stk_compat[] = "ibm,power9-phb-stack"; 294 295 xdc->dt_xscom = pnv_pec_dt_xscom; 296 297 dc->realize = pnv_pec_realize; 298 device_class_set_props(dc, pnv_pec_properties); 299 dc->user_creatable = false; 300 301 pecc->xscom_nest_base = pnv_pec_xscom_nest_base; 302 pecc->xscom_pci_base = pnv_pec_xscom_pci_base; 303 pecc->xscom_nest_size = PNV9_XSCOM_PEC_NEST_SIZE; 304 pecc->xscom_pci_size = PNV9_XSCOM_PEC_PCI_SIZE; 305 pecc->compat = compat; 306 pecc->compat_size = sizeof(compat); 307 pecc->stk_compat = stk_compat; 308 pecc->stk_compat_size = sizeof(stk_compat); 309 pecc->version = PNV_PHB4_VERSION; 310 pecc->phb_type = TYPE_PNV_PHB4; 311 pecc->num_phbs = pnv_pec_num_phbs; 312 } 313 314 static const TypeInfo pnv_pec_type_info = { 315 .name = TYPE_PNV_PHB4_PEC, 316 .parent = TYPE_DEVICE, 317 .instance_size = sizeof(PnvPhb4PecState), 318 .class_init = pnv_pec_class_init, 319 .class_size = sizeof(PnvPhb4PecClass), 320 .interfaces = (InterfaceInfo[]) { 321 { TYPE_PNV_XSCOM_INTERFACE }, 322 { } 323 } 324 }; 325 326 /* 327 * POWER10 definitions 328 */ 329 330 static uint32_t pnv_phb5_pec_xscom_pci_base(PnvPhb4PecState *pec) 331 { 332 return PNV10_XSCOM_PEC_PCI_BASE + 0x1000000 * pec->index; 333 } 334 335 static uint32_t pnv_phb5_pec_xscom_nest_base(PnvPhb4PecState *pec) 336 { 337 /* index goes down ... */ 338 return PNV10_XSCOM_PEC_NEST_BASE - 0x1000000 * pec->index; 339 } 340 341 /* 342 * PEC0 -> 3 stacks 343 * PEC1 -> 3 stacks 344 */ 345 static const uint32_t pnv_phb5_pec_num_stacks[] = { 3, 3 }; 346 347 static void pnv_phb5_pec_class_init(ObjectClass *klass, void *data) 348 { 349 PnvPhb4PecClass *pecc = PNV_PHB4_PEC_CLASS(klass); 350 static const char compat[] = "ibm,power10-pbcq"; 351 static const char stk_compat[] = "ibm,power10-phb-stack"; 352 353 pecc->xscom_nest_base = pnv_phb5_pec_xscom_nest_base; 354 pecc->xscom_pci_base = pnv_phb5_pec_xscom_pci_base; 355 pecc->xscom_nest_size = PNV10_XSCOM_PEC_NEST_SIZE; 356 pecc->xscom_pci_size = PNV10_XSCOM_PEC_PCI_SIZE; 357 pecc->compat = compat; 358 pecc->compat_size = sizeof(compat); 359 pecc->stk_compat = stk_compat; 360 pecc->stk_compat_size = sizeof(stk_compat); 361 pecc->version = PNV_PHB5_VERSION; 362 pecc->phb_type = TYPE_PNV_PHB5; 363 pecc->num_phbs = pnv_phb5_pec_num_stacks; 364 } 365 366 static const TypeInfo pnv_phb5_pec_type_info = { 367 .name = TYPE_PNV_PHB5_PEC, 368 .parent = TYPE_PNV_PHB4_PEC, 369 .instance_size = sizeof(PnvPhb4PecState), 370 .class_init = pnv_phb5_pec_class_init, 371 .class_size = sizeof(PnvPhb4PecClass), 372 .interfaces = (InterfaceInfo[]) { 373 { TYPE_PNV_XSCOM_INTERFACE }, 374 { } 375 } 376 }; 377 378 static void pnv_pec_register_types(void) 379 { 380 type_register_static(&pnv_pec_type_info); 381 type_register_static(&pnv_phb5_pec_type_info); 382 } 383 384 type_init(pnv_pec_register_types); 385