1 /* 2 * QEMU sPAPR PCI host originated from Uninorth PCI host 3 * 4 * Copyright (c) 2011 Alexey Kardashevskiy, IBM Corporation. 5 * Copyright (C) 2011 David Gibson, IBM Corporation. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a copy 8 * of this software and associated documentation files (the "Software"), to deal 9 * in the Software without restriction, including without limitation the rights 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 * copies of the Software, and to permit persons to whom the Software is 12 * furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included in 15 * all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 * THE SOFTWARE. 24 */ 25 26 #include "qemu/osdep.h" 27 #include "qapi/error.h" 28 #include "cpu.h" 29 #include "hw/irq.h" 30 #include "hw/sysbus.h" 31 #include "migration/vmstate.h" 32 #include "hw/pci/pci.h" 33 #include "hw/pci/msi.h" 34 #include "hw/pci/msix.h" 35 #include "hw/pci/pci_host.h" 36 #include "hw/ppc/spapr.h" 37 #include "hw/pci-host/spapr.h" 38 #include "exec/address-spaces.h" 39 #include "exec/ram_addr.h" 40 #include <libfdt.h> 41 #include "trace.h" 42 #include "qemu/error-report.h" 43 #include "qemu/module.h" 44 #include "qapi/qmp/qerror.h" 45 #include "hw/ppc/fdt.h" 46 #include "hw/pci/pci_bridge.h" 47 #include "hw/pci/pci_bus.h" 48 #include "hw/pci/pci_ids.h" 49 #include "hw/ppc/spapr_drc.h" 50 #include "hw/qdev-properties.h" 51 #include "sysemu/device_tree.h" 52 #include "sysemu/kvm.h" 53 #include "sysemu/hostmem.h" 54 #include "sysemu/numa.h" 55 56 /* Copied from the kernel arch/powerpc/platforms/pseries/msi.c */ 57 #define RTAS_QUERY_FN 0 58 #define RTAS_CHANGE_FN 1 59 #define RTAS_RESET_FN 2 60 #define RTAS_CHANGE_MSI_FN 3 61 #define RTAS_CHANGE_MSIX_FN 4 62 63 /* Interrupt types to return on RTAS_CHANGE_* */ 64 #define RTAS_TYPE_MSI 1 65 #define RTAS_TYPE_MSIX 2 66 67 SpaprPhbState *spapr_pci_find_phb(SpaprMachineState *spapr, uint64_t buid) 68 { 69 SpaprPhbState *sphb; 70 71 QLIST_FOREACH(sphb, &spapr->phbs, list) { 72 if (sphb->buid != buid) { 73 continue; 74 } 75 return sphb; 76 } 77 78 return NULL; 79 } 80 81 PCIDevice *spapr_pci_find_dev(SpaprMachineState *spapr, uint64_t buid, 82 uint32_t config_addr) 83 { 84 SpaprPhbState *sphb = spapr_pci_find_phb(spapr, buid); 85 PCIHostState *phb = PCI_HOST_BRIDGE(sphb); 86 int bus_num = (config_addr >> 16) & 0xFF; 87 int devfn = (config_addr >> 8) & 0xFF; 88 89 if (!phb) { 90 return NULL; 91 } 92 93 return pci_find_device(phb->bus, bus_num, devfn); 94 } 95 96 static uint32_t rtas_pci_cfgaddr(uint32_t arg) 97 { 98 /* This handles the encoding of extended config space addresses */ 99 return ((arg >> 20) & 0xf00) | (arg & 0xff); 100 } 101 102 static void finish_read_pci_config(SpaprMachineState *spapr, uint64_t buid, 103 uint32_t addr, uint32_t size, 104 target_ulong rets) 105 { 106 PCIDevice *pci_dev; 107 uint32_t val; 108 109 if ((size != 1) && (size != 2) && (size != 4)) { 110 /* access must be 1, 2 or 4 bytes */ 111 rtas_st(rets, 0, RTAS_OUT_HW_ERROR); 112 return; 113 } 114 115 pci_dev = spapr_pci_find_dev(spapr, buid, addr); 116 addr = rtas_pci_cfgaddr(addr); 117 118 if (!pci_dev || (addr % size) || (addr >= pci_config_size(pci_dev))) { 119 /* Access must be to a valid device, within bounds and 120 * naturally aligned */ 121 rtas_st(rets, 0, RTAS_OUT_HW_ERROR); 122 return; 123 } 124 125 val = pci_host_config_read_common(pci_dev, addr, 126 pci_config_size(pci_dev), size); 127 128 rtas_st(rets, 0, RTAS_OUT_SUCCESS); 129 rtas_st(rets, 1, val); 130 } 131 132 static void rtas_ibm_read_pci_config(PowerPCCPU *cpu, SpaprMachineState *spapr, 133 uint32_t token, uint32_t nargs, 134 target_ulong args, 135 uint32_t nret, target_ulong rets) 136 { 137 uint64_t buid; 138 uint32_t size, addr; 139 140 if ((nargs != 4) || (nret != 2)) { 141 rtas_st(rets, 0, RTAS_OUT_HW_ERROR); 142 return; 143 } 144 145 buid = rtas_ldq(args, 1); 146 size = rtas_ld(args, 3); 147 addr = rtas_ld(args, 0); 148 149 finish_read_pci_config(spapr, buid, addr, size, rets); 150 } 151 152 static void rtas_read_pci_config(PowerPCCPU *cpu, SpaprMachineState *spapr, 153 uint32_t token, uint32_t nargs, 154 target_ulong args, 155 uint32_t nret, target_ulong rets) 156 { 157 uint32_t size, addr; 158 159 if ((nargs != 2) || (nret != 2)) { 160 rtas_st(rets, 0, RTAS_OUT_HW_ERROR); 161 return; 162 } 163 164 size = rtas_ld(args, 1); 165 addr = rtas_ld(args, 0); 166 167 finish_read_pci_config(spapr, 0, addr, size, rets); 168 } 169 170 static void finish_write_pci_config(SpaprMachineState *spapr, uint64_t buid, 171 uint32_t addr, uint32_t size, 172 uint32_t val, target_ulong rets) 173 { 174 PCIDevice *pci_dev; 175 176 if ((size != 1) && (size != 2) && (size != 4)) { 177 /* access must be 1, 2 or 4 bytes */ 178 rtas_st(rets, 0, RTAS_OUT_HW_ERROR); 179 return; 180 } 181 182 pci_dev = spapr_pci_find_dev(spapr, buid, addr); 183 addr = rtas_pci_cfgaddr(addr); 184 185 if (!pci_dev || (addr % size) || (addr >= pci_config_size(pci_dev))) { 186 /* Access must be to a valid device, within bounds and 187 * naturally aligned */ 188 rtas_st(rets, 0, RTAS_OUT_HW_ERROR); 189 return; 190 } 191 192 pci_host_config_write_common(pci_dev, addr, pci_config_size(pci_dev), 193 val, size); 194 195 rtas_st(rets, 0, RTAS_OUT_SUCCESS); 196 } 197 198 static void rtas_ibm_write_pci_config(PowerPCCPU *cpu, SpaprMachineState *spapr, 199 uint32_t token, uint32_t nargs, 200 target_ulong args, 201 uint32_t nret, target_ulong rets) 202 { 203 uint64_t buid; 204 uint32_t val, size, addr; 205 206 if ((nargs != 5) || (nret != 1)) { 207 rtas_st(rets, 0, RTAS_OUT_HW_ERROR); 208 return; 209 } 210 211 buid = rtas_ldq(args, 1); 212 val = rtas_ld(args, 4); 213 size = rtas_ld(args, 3); 214 addr = rtas_ld(args, 0); 215 216 finish_write_pci_config(spapr, buid, addr, size, val, rets); 217 } 218 219 static void rtas_write_pci_config(PowerPCCPU *cpu, SpaprMachineState *spapr, 220 uint32_t token, uint32_t nargs, 221 target_ulong args, 222 uint32_t nret, target_ulong rets) 223 { 224 uint32_t val, size, addr; 225 226 if ((nargs != 3) || (nret != 1)) { 227 rtas_st(rets, 0, RTAS_OUT_HW_ERROR); 228 return; 229 } 230 231 232 val = rtas_ld(args, 2); 233 size = rtas_ld(args, 1); 234 addr = rtas_ld(args, 0); 235 236 finish_write_pci_config(spapr, 0, addr, size, val, rets); 237 } 238 239 /* 240 * Set MSI/MSIX message data. 241 * This is required for msi_notify()/msix_notify() which 242 * will write at the addresses via spapr_msi_write(). 243 * 244 * If hwaddr == 0, all entries will have .data == first_irq i.e. 245 * table will be reset. 246 */ 247 static void spapr_msi_setmsg(PCIDevice *pdev, hwaddr addr, bool msix, 248 unsigned first_irq, unsigned req_num) 249 { 250 unsigned i; 251 MSIMessage msg = { .address = addr, .data = first_irq }; 252 253 if (!msix) { 254 msi_set_message(pdev, msg); 255 trace_spapr_pci_msi_setup(pdev->name, 0, msg.address); 256 return; 257 } 258 259 for (i = 0; i < req_num; ++i) { 260 msix_set_message(pdev, i, msg); 261 trace_spapr_pci_msi_setup(pdev->name, i, msg.address); 262 if (addr) { 263 ++msg.data; 264 } 265 } 266 } 267 268 static void rtas_ibm_change_msi(PowerPCCPU *cpu, SpaprMachineState *spapr, 269 uint32_t token, uint32_t nargs, 270 target_ulong args, uint32_t nret, 271 target_ulong rets) 272 { 273 SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr); 274 uint32_t config_addr = rtas_ld(args, 0); 275 uint64_t buid = rtas_ldq(args, 1); 276 unsigned int func = rtas_ld(args, 3); 277 unsigned int req_num = rtas_ld(args, 4); /* 0 == remove all */ 278 unsigned int seq_num = rtas_ld(args, 5); 279 unsigned int ret_intr_type; 280 unsigned int irq, max_irqs = 0; 281 SpaprPhbState *phb = NULL; 282 PCIDevice *pdev = NULL; 283 SpaprPciMsi *msi; 284 int *config_addr_key; 285 Error *err = NULL; 286 int i; 287 288 /* Fins SpaprPhbState */ 289 phb = spapr_pci_find_phb(spapr, buid); 290 if (phb) { 291 pdev = spapr_pci_find_dev(spapr, buid, config_addr); 292 } 293 if (!phb || !pdev) { 294 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 295 return; 296 } 297 298 switch (func) { 299 case RTAS_CHANGE_FN: 300 if (msi_present(pdev)) { 301 ret_intr_type = RTAS_TYPE_MSI; 302 } else if (msix_present(pdev)) { 303 ret_intr_type = RTAS_TYPE_MSIX; 304 } else { 305 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 306 return; 307 } 308 break; 309 case RTAS_CHANGE_MSI_FN: 310 if (msi_present(pdev)) { 311 ret_intr_type = RTAS_TYPE_MSI; 312 } else { 313 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 314 return; 315 } 316 break; 317 case RTAS_CHANGE_MSIX_FN: 318 if (msix_present(pdev)) { 319 ret_intr_type = RTAS_TYPE_MSIX; 320 } else { 321 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 322 return; 323 } 324 break; 325 default: 326 error_report("rtas_ibm_change_msi(%u) is not implemented", func); 327 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 328 return; 329 } 330 331 msi = (SpaprPciMsi *) g_hash_table_lookup(phb->msi, &config_addr); 332 333 /* Releasing MSIs */ 334 if (!req_num) { 335 if (!msi) { 336 trace_spapr_pci_msi("Releasing wrong config", config_addr); 337 rtas_st(rets, 0, RTAS_OUT_HW_ERROR); 338 return; 339 } 340 341 if (msi_present(pdev)) { 342 spapr_msi_setmsg(pdev, 0, false, 0, 0); 343 } 344 if (msix_present(pdev)) { 345 spapr_msi_setmsg(pdev, 0, true, 0, 0); 346 } 347 g_hash_table_remove(phb->msi, &config_addr); 348 349 trace_spapr_pci_msi("Released MSIs", config_addr); 350 rtas_st(rets, 0, RTAS_OUT_SUCCESS); 351 rtas_st(rets, 1, 0); 352 return; 353 } 354 355 /* Enabling MSI */ 356 357 /* Check if the device supports as many IRQs as requested */ 358 if (ret_intr_type == RTAS_TYPE_MSI) { 359 max_irqs = msi_nr_vectors_allocated(pdev); 360 } else if (ret_intr_type == RTAS_TYPE_MSIX) { 361 max_irqs = pdev->msix_entries_nr; 362 } 363 if (!max_irqs) { 364 error_report("Requested interrupt type %d is not enabled for device %x", 365 ret_intr_type, config_addr); 366 rtas_st(rets, 0, -1); /* Hardware error */ 367 return; 368 } 369 /* Correct the number if the guest asked for too many */ 370 if (req_num > max_irqs) { 371 trace_spapr_pci_msi_retry(config_addr, req_num, max_irqs); 372 req_num = max_irqs; 373 irq = 0; /* to avoid misleading trace */ 374 goto out; 375 } 376 377 /* Allocate MSIs */ 378 if (smc->legacy_irq_allocation) { 379 irq = spapr_irq_find(spapr, req_num, ret_intr_type == RTAS_TYPE_MSI, 380 &err); 381 } else { 382 irq = spapr_irq_msi_alloc(spapr, req_num, 383 ret_intr_type == RTAS_TYPE_MSI, &err); 384 } 385 if (err) { 386 error_reportf_err(err, "Can't allocate MSIs for device %x: ", 387 config_addr); 388 rtas_st(rets, 0, RTAS_OUT_HW_ERROR); 389 return; 390 } 391 392 for (i = 0; i < req_num; i++) { 393 spapr_irq_claim(spapr, irq + i, false, &err); 394 if (err) { 395 if (i) { 396 spapr_irq_free(spapr, irq, i); 397 } 398 if (!smc->legacy_irq_allocation) { 399 spapr_irq_msi_free(spapr, irq, req_num); 400 } 401 error_reportf_err(err, "Can't allocate MSIs for device %x: ", 402 config_addr); 403 rtas_st(rets, 0, RTAS_OUT_HW_ERROR); 404 return; 405 } 406 } 407 408 /* Release previous MSIs */ 409 if (msi) { 410 g_hash_table_remove(phb->msi, &config_addr); 411 } 412 413 /* Setup MSI/MSIX vectors in the device (via cfgspace or MSIX BAR) */ 414 spapr_msi_setmsg(pdev, SPAPR_PCI_MSI_WINDOW, ret_intr_type == RTAS_TYPE_MSIX, 415 irq, req_num); 416 417 /* Add MSI device to cache */ 418 msi = g_new(SpaprPciMsi, 1); 419 msi->first_irq = irq; 420 msi->num = req_num; 421 config_addr_key = g_new(int, 1); 422 *config_addr_key = config_addr; 423 g_hash_table_insert(phb->msi, config_addr_key, msi); 424 425 out: 426 rtas_st(rets, 0, RTAS_OUT_SUCCESS); 427 rtas_st(rets, 1, req_num); 428 rtas_st(rets, 2, ++seq_num); 429 if (nret > 3) { 430 rtas_st(rets, 3, ret_intr_type); 431 } 432 433 trace_spapr_pci_rtas_ibm_change_msi(config_addr, func, req_num, irq); 434 } 435 436 static void rtas_ibm_query_interrupt_source_number(PowerPCCPU *cpu, 437 SpaprMachineState *spapr, 438 uint32_t token, 439 uint32_t nargs, 440 target_ulong args, 441 uint32_t nret, 442 target_ulong rets) 443 { 444 uint32_t config_addr = rtas_ld(args, 0); 445 uint64_t buid = rtas_ldq(args, 1); 446 unsigned int intr_src_num = -1, ioa_intr_num = rtas_ld(args, 3); 447 SpaprPhbState *phb = NULL; 448 PCIDevice *pdev = NULL; 449 SpaprPciMsi *msi; 450 451 /* Find SpaprPhbState */ 452 phb = spapr_pci_find_phb(spapr, buid); 453 if (phb) { 454 pdev = spapr_pci_find_dev(spapr, buid, config_addr); 455 } 456 if (!phb || !pdev) { 457 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 458 return; 459 } 460 461 /* Find device descriptor and start IRQ */ 462 msi = (SpaprPciMsi *) g_hash_table_lookup(phb->msi, &config_addr); 463 if (!msi || !msi->first_irq || !msi->num || (ioa_intr_num >= msi->num)) { 464 trace_spapr_pci_msi("Failed to return vector", config_addr); 465 rtas_st(rets, 0, RTAS_OUT_HW_ERROR); 466 return; 467 } 468 intr_src_num = msi->first_irq + ioa_intr_num; 469 trace_spapr_pci_rtas_ibm_query_interrupt_source_number(ioa_intr_num, 470 intr_src_num); 471 472 rtas_st(rets, 0, RTAS_OUT_SUCCESS); 473 rtas_st(rets, 1, intr_src_num); 474 rtas_st(rets, 2, 1);/* 0 == level; 1 == edge */ 475 } 476 477 static void rtas_ibm_set_eeh_option(PowerPCCPU *cpu, 478 SpaprMachineState *spapr, 479 uint32_t token, uint32_t nargs, 480 target_ulong args, uint32_t nret, 481 target_ulong rets) 482 { 483 SpaprPhbState *sphb; 484 uint32_t addr, option; 485 uint64_t buid; 486 int ret; 487 488 if ((nargs != 4) || (nret != 1)) { 489 goto param_error_exit; 490 } 491 492 buid = rtas_ldq(args, 1); 493 addr = rtas_ld(args, 0); 494 option = rtas_ld(args, 3); 495 496 sphb = spapr_pci_find_phb(spapr, buid); 497 if (!sphb) { 498 goto param_error_exit; 499 } 500 501 if (!spapr_phb_eeh_available(sphb)) { 502 goto param_error_exit; 503 } 504 505 ret = spapr_phb_vfio_eeh_set_option(sphb, addr, option); 506 rtas_st(rets, 0, ret); 507 return; 508 509 param_error_exit: 510 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 511 } 512 513 static void rtas_ibm_get_config_addr_info2(PowerPCCPU *cpu, 514 SpaprMachineState *spapr, 515 uint32_t token, uint32_t nargs, 516 target_ulong args, uint32_t nret, 517 target_ulong rets) 518 { 519 SpaprPhbState *sphb; 520 PCIDevice *pdev; 521 uint32_t addr, option; 522 uint64_t buid; 523 524 if ((nargs != 4) || (nret != 2)) { 525 goto param_error_exit; 526 } 527 528 buid = rtas_ldq(args, 1); 529 sphb = spapr_pci_find_phb(spapr, buid); 530 if (!sphb) { 531 goto param_error_exit; 532 } 533 534 if (!spapr_phb_eeh_available(sphb)) { 535 goto param_error_exit; 536 } 537 538 /* 539 * We always have PE address of form "00BB0001". "BB" 540 * represents the bus number of PE's primary bus. 541 */ 542 option = rtas_ld(args, 3); 543 switch (option) { 544 case RTAS_GET_PE_ADDR: 545 addr = rtas_ld(args, 0); 546 pdev = spapr_pci_find_dev(spapr, buid, addr); 547 if (!pdev) { 548 goto param_error_exit; 549 } 550 551 rtas_st(rets, 1, (pci_bus_num(pci_get_bus(pdev)) << 16) + 1); 552 break; 553 case RTAS_GET_PE_MODE: 554 rtas_st(rets, 1, RTAS_PE_MODE_SHARED); 555 break; 556 default: 557 goto param_error_exit; 558 } 559 560 rtas_st(rets, 0, RTAS_OUT_SUCCESS); 561 return; 562 563 param_error_exit: 564 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 565 } 566 567 static void rtas_ibm_read_slot_reset_state2(PowerPCCPU *cpu, 568 SpaprMachineState *spapr, 569 uint32_t token, uint32_t nargs, 570 target_ulong args, uint32_t nret, 571 target_ulong rets) 572 { 573 SpaprPhbState *sphb; 574 uint64_t buid; 575 int state, ret; 576 577 if ((nargs != 3) || (nret != 4 && nret != 5)) { 578 goto param_error_exit; 579 } 580 581 buid = rtas_ldq(args, 1); 582 sphb = spapr_pci_find_phb(spapr, buid); 583 if (!sphb) { 584 goto param_error_exit; 585 } 586 587 if (!spapr_phb_eeh_available(sphb)) { 588 goto param_error_exit; 589 } 590 591 ret = spapr_phb_vfio_eeh_get_state(sphb, &state); 592 rtas_st(rets, 0, ret); 593 if (ret != RTAS_OUT_SUCCESS) { 594 return; 595 } 596 597 rtas_st(rets, 1, state); 598 rtas_st(rets, 2, RTAS_EEH_SUPPORT); 599 rtas_st(rets, 3, RTAS_EEH_PE_UNAVAIL_INFO); 600 if (nret >= 5) { 601 rtas_st(rets, 4, RTAS_EEH_PE_RECOVER_INFO); 602 } 603 return; 604 605 param_error_exit: 606 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 607 } 608 609 static void rtas_ibm_set_slot_reset(PowerPCCPU *cpu, 610 SpaprMachineState *spapr, 611 uint32_t token, uint32_t nargs, 612 target_ulong args, uint32_t nret, 613 target_ulong rets) 614 { 615 SpaprPhbState *sphb; 616 uint32_t option; 617 uint64_t buid; 618 int ret; 619 620 if ((nargs != 4) || (nret != 1)) { 621 goto param_error_exit; 622 } 623 624 buid = rtas_ldq(args, 1); 625 option = rtas_ld(args, 3); 626 sphb = spapr_pci_find_phb(spapr, buid); 627 if (!sphb) { 628 goto param_error_exit; 629 } 630 631 if (!spapr_phb_eeh_available(sphb)) { 632 goto param_error_exit; 633 } 634 635 ret = spapr_phb_vfio_eeh_reset(sphb, option); 636 rtas_st(rets, 0, ret); 637 return; 638 639 param_error_exit: 640 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 641 } 642 643 static void rtas_ibm_configure_pe(PowerPCCPU *cpu, 644 SpaprMachineState *spapr, 645 uint32_t token, uint32_t nargs, 646 target_ulong args, uint32_t nret, 647 target_ulong rets) 648 { 649 SpaprPhbState *sphb; 650 uint64_t buid; 651 int ret; 652 653 if ((nargs != 3) || (nret != 1)) { 654 goto param_error_exit; 655 } 656 657 buid = rtas_ldq(args, 1); 658 sphb = spapr_pci_find_phb(spapr, buid); 659 if (!sphb) { 660 goto param_error_exit; 661 } 662 663 if (!spapr_phb_eeh_available(sphb)) { 664 goto param_error_exit; 665 } 666 667 ret = spapr_phb_vfio_eeh_configure(sphb); 668 rtas_st(rets, 0, ret); 669 return; 670 671 param_error_exit: 672 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 673 } 674 675 /* To support it later */ 676 static void rtas_ibm_slot_error_detail(PowerPCCPU *cpu, 677 SpaprMachineState *spapr, 678 uint32_t token, uint32_t nargs, 679 target_ulong args, uint32_t nret, 680 target_ulong rets) 681 { 682 SpaprPhbState *sphb; 683 int option; 684 uint64_t buid; 685 686 if ((nargs != 8) || (nret != 1)) { 687 goto param_error_exit; 688 } 689 690 buid = rtas_ldq(args, 1); 691 sphb = spapr_pci_find_phb(spapr, buid); 692 if (!sphb) { 693 goto param_error_exit; 694 } 695 696 if (!spapr_phb_eeh_available(sphb)) { 697 goto param_error_exit; 698 } 699 700 option = rtas_ld(args, 7); 701 switch (option) { 702 case RTAS_SLOT_TEMP_ERR_LOG: 703 case RTAS_SLOT_PERM_ERR_LOG: 704 break; 705 default: 706 goto param_error_exit; 707 } 708 709 /* We don't have error log yet */ 710 rtas_st(rets, 0, RTAS_OUT_NO_ERRORS_FOUND); 711 return; 712 713 param_error_exit: 714 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR); 715 } 716 717 static void pci_spapr_set_irq(void *opaque, int irq_num, int level) 718 { 719 /* 720 * Here we use the number returned by pci_swizzle_map_irq_fn to find a 721 * corresponding qemu_irq. 722 */ 723 SpaprPhbState *phb = opaque; 724 SpaprMachineState *spapr = SPAPR_MACHINE(qdev_get_machine()); 725 726 trace_spapr_pci_lsi_set(phb->dtbusname, irq_num, phb->lsi_table[irq_num].irq); 727 qemu_set_irq(spapr_qirq(spapr, phb->lsi_table[irq_num].irq), level); 728 } 729 730 static PCIINTxRoute spapr_route_intx_pin_to_irq(void *opaque, int pin) 731 { 732 SpaprPhbState *sphb = SPAPR_PCI_HOST_BRIDGE(opaque); 733 PCIINTxRoute route; 734 735 route.mode = PCI_INTX_ENABLED; 736 route.irq = sphb->lsi_table[pin].irq; 737 738 return route; 739 } 740 741 /* 742 * MSI/MSIX memory region implementation. 743 * The handler handles both MSI and MSIX. 744 * The vector number is encoded in least bits in data. 745 */ 746 static void spapr_msi_write(void *opaque, hwaddr addr, 747 uint64_t data, unsigned size) 748 { 749 SpaprMachineState *spapr = SPAPR_MACHINE(qdev_get_machine()); 750 uint32_t irq = data; 751 752 trace_spapr_pci_msi_write(addr, data, irq); 753 754 qemu_irq_pulse(spapr_qirq(spapr, irq)); 755 } 756 757 static const MemoryRegionOps spapr_msi_ops = { 758 /* There is no .read as the read result is undefined by PCI spec */ 759 .read = NULL, 760 .write = spapr_msi_write, 761 .endianness = DEVICE_LITTLE_ENDIAN 762 }; 763 764 /* 765 * PHB PCI device 766 */ 767 static AddressSpace *spapr_pci_dma_iommu(PCIBus *bus, void *opaque, int devfn) 768 { 769 SpaprPhbState *phb = opaque; 770 771 return &phb->iommu_as; 772 } 773 774 static char *spapr_phb_vfio_get_loc_code(SpaprPhbState *sphb, PCIDevice *pdev) 775 { 776 char *path = NULL, *buf = NULL, *host = NULL; 777 778 /* Get the PCI VFIO host id */ 779 host = object_property_get_str(OBJECT(pdev), "host", NULL); 780 if (!host) { 781 goto err_out; 782 } 783 784 /* Construct the path of the file that will give us the DT location */ 785 path = g_strdup_printf("/sys/bus/pci/devices/%s/devspec", host); 786 g_free(host); 787 if (!g_file_get_contents(path, &buf, NULL, NULL)) { 788 goto err_out; 789 } 790 g_free(path); 791 792 /* Construct and read from host device tree the loc-code */ 793 path = g_strdup_printf("/proc/device-tree%s/ibm,loc-code", buf); 794 g_free(buf); 795 if (!g_file_get_contents(path, &buf, NULL, NULL)) { 796 goto err_out; 797 } 798 return buf; 799 800 err_out: 801 g_free(path); 802 return NULL; 803 } 804 805 static char *spapr_phb_get_loc_code(SpaprPhbState *sphb, PCIDevice *pdev) 806 { 807 char *buf; 808 const char *devtype = "qemu"; 809 uint32_t busnr = pci_bus_num(PCI_BUS(qdev_get_parent_bus(DEVICE(pdev)))); 810 811 if (object_dynamic_cast(OBJECT(pdev), "vfio-pci")) { 812 buf = spapr_phb_vfio_get_loc_code(sphb, pdev); 813 if (buf) { 814 return buf; 815 } 816 devtype = "vfio"; 817 } 818 /* 819 * For emulated devices and VFIO-failure case, make up 820 * the loc-code. 821 */ 822 buf = g_strdup_printf("%s_%s:%04x:%02x:%02x.%x", 823 devtype, pdev->name, sphb->index, busnr, 824 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)); 825 return buf; 826 } 827 828 /* Macros to operate with address in OF binding to PCI */ 829 #define b_x(x, p, l) (((x) & ((1<<(l))-1)) << (p)) 830 #define b_n(x) b_x((x), 31, 1) /* 0 if relocatable */ 831 #define b_p(x) b_x((x), 30, 1) /* 1 if prefetchable */ 832 #define b_t(x) b_x((x), 29, 1) /* 1 if the address is aliased */ 833 #define b_ss(x) b_x((x), 24, 2) /* the space code */ 834 #define b_bbbbbbbb(x) b_x((x), 16, 8) /* bus number */ 835 #define b_ddddd(x) b_x((x), 11, 5) /* device number */ 836 #define b_fff(x) b_x((x), 8, 3) /* function number */ 837 #define b_rrrrrrrr(x) b_x((x), 0, 8) /* register number */ 838 839 /* for 'reg' OF properties */ 840 #define RESOURCE_CELLS_SIZE 2 841 #define RESOURCE_CELLS_ADDRESS 3 842 843 typedef struct ResourceFields { 844 uint32_t phys_hi; 845 uint32_t phys_mid; 846 uint32_t phys_lo; 847 uint32_t size_hi; 848 uint32_t size_lo; 849 } QEMU_PACKED ResourceFields; 850 851 typedef struct ResourceProps { 852 ResourceFields reg[8]; 853 uint32_t reg_len; 854 } ResourceProps; 855 856 /* fill in the 'reg' OF properties for 857 * a PCI device. 'reg' describes resource requirements for a 858 * device's IO/MEM regions. 859 * 860 * the property is an array of ('phys-addr', 'size') pairs describing 861 * the addressable regions of the PCI device, where 'phys-addr' is a 862 * RESOURCE_CELLS_ADDRESS-tuple of 32-bit integers corresponding to 863 * (phys.hi, phys.mid, phys.lo), and 'size' is a 864 * RESOURCE_CELLS_SIZE-tuple corresponding to (size.hi, size.lo). 865 * 866 * phys.hi = 0xYYXXXXZZ, where: 867 * 0xYY = npt000ss 868 * ||| | 869 * ||| +-- space code 870 * ||| | 871 * ||| + 00 if configuration space 872 * ||| + 01 if IO region, 873 * ||| + 10 if 32-bit MEM region 874 * ||| + 11 if 64-bit MEM region 875 * ||| 876 * ||+------ for non-relocatable IO: 1 if aliased 877 * || for relocatable IO: 1 if below 64KB 878 * || for MEM: 1 if below 1MB 879 * |+------- 1 if region is prefetchable 880 * +-------- 1 if region is non-relocatable 881 * 0xXXXX = bbbbbbbb dddddfff, encoding bus, slot, and function 882 * bits respectively 883 * 0xZZ = rrrrrrrr, the register number of the BAR corresponding 884 * to the region 885 * 886 * phys.mid and phys.lo correspond respectively to the hi/lo portions 887 * of the actual address of the region. 888 * 889 * note also that addresses defined in this property are, at least 890 * for PAPR guests, relative to the PHBs IO/MEM windows, and 891 * correspond directly to the addresses in the BARs. 892 * 893 * in accordance with PCI Bus Binding to Open Firmware, 894 * IEEE Std 1275-1994, section 4.1.1, as implemented by PAPR+ v2.7, 895 * Appendix C. 896 */ 897 static void populate_resource_props(PCIDevice *d, ResourceProps *rp) 898 { 899 int bus_num = pci_bus_num(PCI_BUS(qdev_get_parent_bus(DEVICE(d)))); 900 uint32_t dev_id = (b_bbbbbbbb(bus_num) | 901 b_ddddd(PCI_SLOT(d->devfn)) | 902 b_fff(PCI_FUNC(d->devfn))); 903 ResourceFields *reg; 904 int i, reg_idx = 0; 905 906 /* config space region */ 907 reg = &rp->reg[reg_idx++]; 908 reg->phys_hi = cpu_to_be32(dev_id); 909 reg->phys_mid = 0; 910 reg->phys_lo = 0; 911 reg->size_hi = 0; 912 reg->size_lo = 0; 913 914 for (i = 0; i < PCI_NUM_REGIONS; i++) { 915 if (!d->io_regions[i].size) { 916 continue; 917 } 918 919 reg = &rp->reg[reg_idx++]; 920 921 reg->phys_hi = cpu_to_be32(dev_id | b_rrrrrrrr(pci_bar(d, i))); 922 if (d->io_regions[i].type & PCI_BASE_ADDRESS_SPACE_IO) { 923 reg->phys_hi |= cpu_to_be32(b_ss(1)); 924 } else if (d->io_regions[i].type & PCI_BASE_ADDRESS_MEM_TYPE_64) { 925 reg->phys_hi |= cpu_to_be32(b_ss(3)); 926 } else { 927 reg->phys_hi |= cpu_to_be32(b_ss(2)); 928 } 929 reg->phys_mid = 0; 930 reg->phys_lo = 0; 931 reg->size_hi = cpu_to_be32(d->io_regions[i].size >> 32); 932 reg->size_lo = cpu_to_be32(d->io_regions[i].size); 933 } 934 935 rp->reg_len = reg_idx * sizeof(ResourceFields); 936 } 937 938 typedef struct PCIClass PCIClass; 939 typedef struct PCISubClass PCISubClass; 940 typedef struct PCIIFace PCIIFace; 941 942 struct PCIIFace { 943 int iface; 944 const char *name; 945 }; 946 947 struct PCISubClass { 948 int subclass; 949 const char *name; 950 const PCIIFace *iface; 951 }; 952 953 struct PCIClass { 954 const char *name; 955 const PCISubClass *subc; 956 }; 957 958 static const PCISubClass undef_subclass[] = { 959 { PCI_CLASS_NOT_DEFINED_VGA, "display", NULL }, 960 { 0xFF, NULL, NULL }, 961 }; 962 963 static const PCISubClass mass_subclass[] = { 964 { PCI_CLASS_STORAGE_SCSI, "scsi", NULL }, 965 { PCI_CLASS_STORAGE_IDE, "ide", NULL }, 966 { PCI_CLASS_STORAGE_FLOPPY, "fdc", NULL }, 967 { PCI_CLASS_STORAGE_IPI, "ipi", NULL }, 968 { PCI_CLASS_STORAGE_RAID, "raid", NULL }, 969 { PCI_CLASS_STORAGE_ATA, "ata", NULL }, 970 { PCI_CLASS_STORAGE_SATA, "sata", NULL }, 971 { PCI_CLASS_STORAGE_SAS, "sas", NULL }, 972 { 0xFF, NULL, NULL }, 973 }; 974 975 static const PCISubClass net_subclass[] = { 976 { PCI_CLASS_NETWORK_ETHERNET, "ethernet", NULL }, 977 { PCI_CLASS_NETWORK_TOKEN_RING, "token-ring", NULL }, 978 { PCI_CLASS_NETWORK_FDDI, "fddi", NULL }, 979 { PCI_CLASS_NETWORK_ATM, "atm", NULL }, 980 { PCI_CLASS_NETWORK_ISDN, "isdn", NULL }, 981 { PCI_CLASS_NETWORK_WORLDFIP, "worldfip", NULL }, 982 { PCI_CLASS_NETWORK_PICMG214, "picmg", NULL }, 983 { 0xFF, NULL, NULL }, 984 }; 985 986 static const PCISubClass displ_subclass[] = { 987 { PCI_CLASS_DISPLAY_VGA, "vga", NULL }, 988 { PCI_CLASS_DISPLAY_XGA, "xga", NULL }, 989 { PCI_CLASS_DISPLAY_3D, "3d-controller", NULL }, 990 { 0xFF, NULL, NULL }, 991 }; 992 993 static const PCISubClass media_subclass[] = { 994 { PCI_CLASS_MULTIMEDIA_VIDEO, "video", NULL }, 995 { PCI_CLASS_MULTIMEDIA_AUDIO, "sound", NULL }, 996 { PCI_CLASS_MULTIMEDIA_PHONE, "telephony", NULL }, 997 { 0xFF, NULL, NULL }, 998 }; 999 1000 static const PCISubClass mem_subclass[] = { 1001 { PCI_CLASS_MEMORY_RAM, "memory", NULL }, 1002 { PCI_CLASS_MEMORY_FLASH, "flash", NULL }, 1003 { 0xFF, NULL, NULL }, 1004 }; 1005 1006 static const PCISubClass bridg_subclass[] = { 1007 { PCI_CLASS_BRIDGE_HOST, "host", NULL }, 1008 { PCI_CLASS_BRIDGE_ISA, "isa", NULL }, 1009 { PCI_CLASS_BRIDGE_EISA, "eisa", NULL }, 1010 { PCI_CLASS_BRIDGE_MC, "mca", NULL }, 1011 { PCI_CLASS_BRIDGE_PCI, "pci", NULL }, 1012 { PCI_CLASS_BRIDGE_PCMCIA, "pcmcia", NULL }, 1013 { PCI_CLASS_BRIDGE_NUBUS, "nubus", NULL }, 1014 { PCI_CLASS_BRIDGE_CARDBUS, "cardbus", NULL }, 1015 { PCI_CLASS_BRIDGE_RACEWAY, "raceway", NULL }, 1016 { PCI_CLASS_BRIDGE_PCI_SEMITP, "semi-transparent-pci", NULL }, 1017 { PCI_CLASS_BRIDGE_IB_PCI, "infiniband", NULL }, 1018 { 0xFF, NULL, NULL }, 1019 }; 1020 1021 static const PCISubClass comm_subclass[] = { 1022 { PCI_CLASS_COMMUNICATION_SERIAL, "serial", NULL }, 1023 { PCI_CLASS_COMMUNICATION_PARALLEL, "parallel", NULL }, 1024 { PCI_CLASS_COMMUNICATION_MULTISERIAL, "multiport-serial", NULL }, 1025 { PCI_CLASS_COMMUNICATION_MODEM, "modem", NULL }, 1026 { PCI_CLASS_COMMUNICATION_GPIB, "gpib", NULL }, 1027 { PCI_CLASS_COMMUNICATION_SC, "smart-card", NULL }, 1028 { 0xFF, NULL, NULL, }, 1029 }; 1030 1031 static const PCIIFace pic_iface[] = { 1032 { PCI_CLASS_SYSTEM_PIC_IOAPIC, "io-apic" }, 1033 { PCI_CLASS_SYSTEM_PIC_IOXAPIC, "io-xapic" }, 1034 { 0xFF, NULL }, 1035 }; 1036 1037 static const PCISubClass sys_subclass[] = { 1038 { PCI_CLASS_SYSTEM_PIC, "interrupt-controller", pic_iface }, 1039 { PCI_CLASS_SYSTEM_DMA, "dma-controller", NULL }, 1040 { PCI_CLASS_SYSTEM_TIMER, "timer", NULL }, 1041 { PCI_CLASS_SYSTEM_RTC, "rtc", NULL }, 1042 { PCI_CLASS_SYSTEM_PCI_HOTPLUG, "hot-plug-controller", NULL }, 1043 { PCI_CLASS_SYSTEM_SDHCI, "sd-host-controller", NULL }, 1044 { 0xFF, NULL, NULL }, 1045 }; 1046 1047 static const PCISubClass inp_subclass[] = { 1048 { PCI_CLASS_INPUT_KEYBOARD, "keyboard", NULL }, 1049 { PCI_CLASS_INPUT_PEN, "pen", NULL }, 1050 { PCI_CLASS_INPUT_MOUSE, "mouse", NULL }, 1051 { PCI_CLASS_INPUT_SCANNER, "scanner", NULL }, 1052 { PCI_CLASS_INPUT_GAMEPORT, "gameport", NULL }, 1053 { 0xFF, NULL, NULL }, 1054 }; 1055 1056 static const PCISubClass dock_subclass[] = { 1057 { PCI_CLASS_DOCKING_GENERIC, "dock", NULL }, 1058 { 0xFF, NULL, NULL }, 1059 }; 1060 1061 static const PCISubClass cpu_subclass[] = { 1062 { PCI_CLASS_PROCESSOR_PENTIUM, "pentium", NULL }, 1063 { PCI_CLASS_PROCESSOR_POWERPC, "powerpc", NULL }, 1064 { PCI_CLASS_PROCESSOR_MIPS, "mips", NULL }, 1065 { PCI_CLASS_PROCESSOR_CO, "co-processor", NULL }, 1066 { 0xFF, NULL, NULL }, 1067 }; 1068 1069 static const PCIIFace usb_iface[] = { 1070 { PCI_CLASS_SERIAL_USB_UHCI, "usb-uhci" }, 1071 { PCI_CLASS_SERIAL_USB_OHCI, "usb-ohci", }, 1072 { PCI_CLASS_SERIAL_USB_EHCI, "usb-ehci" }, 1073 { PCI_CLASS_SERIAL_USB_XHCI, "usb-xhci" }, 1074 { PCI_CLASS_SERIAL_USB_UNKNOWN, "usb-unknown" }, 1075 { PCI_CLASS_SERIAL_USB_DEVICE, "usb-device" }, 1076 { 0xFF, NULL }, 1077 }; 1078 1079 static const PCISubClass ser_subclass[] = { 1080 { PCI_CLASS_SERIAL_FIREWIRE, "firewire", NULL }, 1081 { PCI_CLASS_SERIAL_ACCESS, "access-bus", NULL }, 1082 { PCI_CLASS_SERIAL_SSA, "ssa", NULL }, 1083 { PCI_CLASS_SERIAL_USB, "usb", usb_iface }, 1084 { PCI_CLASS_SERIAL_FIBER, "fibre-channel", NULL }, 1085 { PCI_CLASS_SERIAL_SMBUS, "smb", NULL }, 1086 { PCI_CLASS_SERIAL_IB, "infiniband", NULL }, 1087 { PCI_CLASS_SERIAL_IPMI, "ipmi", NULL }, 1088 { PCI_CLASS_SERIAL_SERCOS, "sercos", NULL }, 1089 { PCI_CLASS_SERIAL_CANBUS, "canbus", NULL }, 1090 { 0xFF, NULL, NULL }, 1091 }; 1092 1093 static const PCISubClass wrl_subclass[] = { 1094 { PCI_CLASS_WIRELESS_IRDA, "irda", NULL }, 1095 { PCI_CLASS_WIRELESS_CIR, "consumer-ir", NULL }, 1096 { PCI_CLASS_WIRELESS_RF_CONTROLLER, "rf-controller", NULL }, 1097 { PCI_CLASS_WIRELESS_BLUETOOTH, "bluetooth", NULL }, 1098 { PCI_CLASS_WIRELESS_BROADBAND, "broadband", NULL }, 1099 { 0xFF, NULL, NULL }, 1100 }; 1101 1102 static const PCISubClass sat_subclass[] = { 1103 { PCI_CLASS_SATELLITE_TV, "satellite-tv", NULL }, 1104 { PCI_CLASS_SATELLITE_AUDIO, "satellite-audio", NULL }, 1105 { PCI_CLASS_SATELLITE_VOICE, "satellite-voice", NULL }, 1106 { PCI_CLASS_SATELLITE_DATA, "satellite-data", NULL }, 1107 { 0xFF, NULL, NULL }, 1108 }; 1109 1110 static const PCISubClass crypt_subclass[] = { 1111 { PCI_CLASS_CRYPT_NETWORK, "network-encryption", NULL }, 1112 { PCI_CLASS_CRYPT_ENTERTAINMENT, 1113 "entertainment-encryption", NULL }, 1114 { 0xFF, NULL, NULL }, 1115 }; 1116 1117 static const PCISubClass spc_subclass[] = { 1118 { PCI_CLASS_SP_DPIO, "dpio", NULL }, 1119 { PCI_CLASS_SP_PERF, "counter", NULL }, 1120 { PCI_CLASS_SP_SYNCH, "measurement", NULL }, 1121 { PCI_CLASS_SP_MANAGEMENT, "management-card", NULL }, 1122 { 0xFF, NULL, NULL }, 1123 }; 1124 1125 static const PCIClass pci_classes[] = { 1126 { "legacy-device", undef_subclass }, 1127 { "mass-storage", mass_subclass }, 1128 { "network", net_subclass }, 1129 { "display", displ_subclass, }, 1130 { "multimedia-device", media_subclass }, 1131 { "memory-controller", mem_subclass }, 1132 { "unknown-bridge", bridg_subclass }, 1133 { "communication-controller", comm_subclass}, 1134 { "system-peripheral", sys_subclass }, 1135 { "input-controller", inp_subclass }, 1136 { "docking-station", dock_subclass }, 1137 { "cpu", cpu_subclass }, 1138 { "serial-bus", ser_subclass }, 1139 { "wireless-controller", wrl_subclass }, 1140 { "intelligent-io", NULL }, 1141 { "satellite-device", sat_subclass }, 1142 { "encryption", crypt_subclass }, 1143 { "data-processing-controller", spc_subclass }, 1144 }; 1145 1146 static const char *dt_name_from_class(uint8_t class, uint8_t subclass, 1147 uint8_t iface) 1148 { 1149 const PCIClass *pclass; 1150 const PCISubClass *psubclass; 1151 const PCIIFace *piface; 1152 const char *name; 1153 1154 if (class >= ARRAY_SIZE(pci_classes)) { 1155 return "pci"; 1156 } 1157 1158 pclass = pci_classes + class; 1159 name = pclass->name; 1160 1161 if (pclass->subc == NULL) { 1162 return name; 1163 } 1164 1165 psubclass = pclass->subc; 1166 while ((psubclass->subclass & 0xff) != 0xff) { 1167 if ((psubclass->subclass & 0xff) == subclass) { 1168 name = psubclass->name; 1169 break; 1170 } 1171 psubclass++; 1172 } 1173 1174 piface = psubclass->iface; 1175 if (piface == NULL) { 1176 return name; 1177 } 1178 while ((piface->iface & 0xff) != 0xff) { 1179 if ((piface->iface & 0xff) == iface) { 1180 name = piface->name; 1181 break; 1182 } 1183 piface++; 1184 } 1185 1186 return name; 1187 } 1188 1189 /* 1190 * DRC helper functions 1191 */ 1192 1193 static uint32_t drc_id_from_devfn(SpaprPhbState *phb, 1194 uint8_t chassis, int32_t devfn) 1195 { 1196 return (phb->index << 16) | (chassis << 8) | devfn; 1197 } 1198 1199 static SpaprDrc *drc_from_devfn(SpaprPhbState *phb, 1200 uint8_t chassis, int32_t devfn) 1201 { 1202 return spapr_drc_by_id(TYPE_SPAPR_DRC_PCI, 1203 drc_id_from_devfn(phb, chassis, devfn)); 1204 } 1205 1206 static uint8_t chassis_from_bus(PCIBus *bus, Error **errp) 1207 { 1208 if (pci_bus_is_root(bus)) { 1209 return 0; 1210 } else { 1211 PCIDevice *bridge = pci_bridge_get_device(bus); 1212 1213 return object_property_get_uint(OBJECT(bridge), "chassis_nr", errp); 1214 } 1215 } 1216 1217 static SpaprDrc *drc_from_dev(SpaprPhbState *phb, PCIDevice *dev) 1218 { 1219 Error *local_err = NULL; 1220 uint8_t chassis = chassis_from_bus(pci_get_bus(dev), &local_err); 1221 1222 if (local_err) { 1223 error_report_err(local_err); 1224 return NULL; 1225 } 1226 1227 return drc_from_devfn(phb, chassis, dev->devfn); 1228 } 1229 1230 static void add_drcs(SpaprPhbState *phb, PCIBus *bus, Error **errp) 1231 { 1232 Object *owner; 1233 int i; 1234 uint8_t chassis; 1235 Error *local_err = NULL; 1236 1237 if (!phb->dr_enabled) { 1238 return; 1239 } 1240 1241 chassis = chassis_from_bus(bus, &local_err); 1242 if (local_err) { 1243 error_propagate(errp, local_err); 1244 return; 1245 } 1246 1247 if (pci_bus_is_root(bus)) { 1248 owner = OBJECT(phb); 1249 } else { 1250 owner = OBJECT(pci_bridge_get_device(bus)); 1251 } 1252 1253 for (i = 0; i < PCI_SLOT_MAX * PCI_FUNC_MAX; i++) { 1254 spapr_dr_connector_new(owner, TYPE_SPAPR_DRC_PCI, 1255 drc_id_from_devfn(phb, chassis, i)); 1256 } 1257 } 1258 1259 static void remove_drcs(SpaprPhbState *phb, PCIBus *bus, Error **errp) 1260 { 1261 int i; 1262 uint8_t chassis; 1263 Error *local_err = NULL; 1264 1265 if (!phb->dr_enabled) { 1266 return; 1267 } 1268 1269 chassis = chassis_from_bus(bus, &local_err); 1270 if (local_err) { 1271 error_propagate(errp, local_err); 1272 return; 1273 } 1274 1275 for (i = PCI_SLOT_MAX * PCI_FUNC_MAX - 1; i >= 0; i--) { 1276 SpaprDrc *drc = drc_from_devfn(phb, chassis, i); 1277 1278 if (drc) { 1279 object_unparent(OBJECT(drc)); 1280 } 1281 } 1282 } 1283 1284 typedef struct PciWalkFdt { 1285 void *fdt; 1286 int offset; 1287 SpaprPhbState *sphb; 1288 int err; 1289 } PciWalkFdt; 1290 1291 static int spapr_dt_pci_device(SpaprPhbState *sphb, PCIDevice *dev, 1292 void *fdt, int parent_offset); 1293 1294 static void spapr_dt_pci_device_cb(PCIBus *bus, PCIDevice *pdev, 1295 void *opaque) 1296 { 1297 PciWalkFdt *p = opaque; 1298 int err; 1299 1300 if (p->err) { 1301 /* Something's already broken, don't keep going */ 1302 return; 1303 } 1304 1305 err = spapr_dt_pci_device(p->sphb, pdev, p->fdt, p->offset); 1306 if (err < 0) { 1307 p->err = err; 1308 } 1309 } 1310 1311 /* Augment PCI device node with bridge specific information */ 1312 static int spapr_dt_pci_bus(SpaprPhbState *sphb, PCIBus *bus, 1313 void *fdt, int offset) 1314 { 1315 Object *owner; 1316 PciWalkFdt cbinfo = { 1317 .fdt = fdt, 1318 .offset = offset, 1319 .sphb = sphb, 1320 .err = 0, 1321 }; 1322 int ret; 1323 1324 _FDT(fdt_setprop_cell(fdt, offset, "#address-cells", 1325 RESOURCE_CELLS_ADDRESS)); 1326 _FDT(fdt_setprop_cell(fdt, offset, "#size-cells", 1327 RESOURCE_CELLS_SIZE)); 1328 1329 assert(bus); 1330 pci_for_each_device_reverse(bus, pci_bus_num(bus), 1331 spapr_dt_pci_device_cb, &cbinfo); 1332 if (cbinfo.err) { 1333 return cbinfo.err; 1334 } 1335 1336 if (pci_bus_is_root(bus)) { 1337 owner = OBJECT(sphb); 1338 } else { 1339 owner = OBJECT(pci_bridge_get_device(bus)); 1340 } 1341 1342 ret = spapr_dt_drc(fdt, offset, owner, 1343 SPAPR_DR_CONNECTOR_TYPE_PCI); 1344 if (ret) { 1345 return ret; 1346 } 1347 1348 return offset; 1349 } 1350 1351 /* create OF node for pci device and required OF DT properties */ 1352 static int spapr_dt_pci_device(SpaprPhbState *sphb, PCIDevice *dev, 1353 void *fdt, int parent_offset) 1354 { 1355 int offset; 1356 const gchar *basename; 1357 gchar *nodename; 1358 int slot = PCI_SLOT(dev->devfn); 1359 int func = PCI_FUNC(dev->devfn); 1360 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev); 1361 ResourceProps rp; 1362 SpaprDrc *drc = drc_from_dev(sphb, dev); 1363 uint32_t vendor_id = pci_default_read_config(dev, PCI_VENDOR_ID, 2); 1364 uint32_t device_id = pci_default_read_config(dev, PCI_DEVICE_ID, 2); 1365 uint32_t revision_id = pci_default_read_config(dev, PCI_REVISION_ID, 1); 1366 uint32_t ccode = pci_default_read_config(dev, PCI_CLASS_PROG, 3); 1367 uint32_t irq_pin = pci_default_read_config(dev, PCI_INTERRUPT_PIN, 1); 1368 uint32_t subsystem_id = pci_default_read_config(dev, PCI_SUBSYSTEM_ID, 2); 1369 uint32_t subsystem_vendor_id = 1370 pci_default_read_config(dev, PCI_SUBSYSTEM_VENDOR_ID, 2); 1371 uint32_t cache_line_size = 1372 pci_default_read_config(dev, PCI_CACHE_LINE_SIZE, 1); 1373 uint32_t pci_status = pci_default_read_config(dev, PCI_STATUS, 2); 1374 gchar *loc_code; 1375 1376 basename = dt_name_from_class((ccode >> 16) & 0xff, (ccode >> 8) & 0xff, 1377 ccode & 0xff); 1378 1379 if (func != 0) { 1380 nodename = g_strdup_printf("%s@%x,%x", basename, slot, func); 1381 } else { 1382 nodename = g_strdup_printf("%s@%x", basename, slot); 1383 } 1384 1385 _FDT(offset = fdt_add_subnode(fdt, parent_offset, nodename)); 1386 1387 g_free(nodename); 1388 1389 /* in accordance with PAPR+ v2.7 13.6.3, Table 181 */ 1390 _FDT(fdt_setprop_cell(fdt, offset, "vendor-id", vendor_id)); 1391 _FDT(fdt_setprop_cell(fdt, offset, "device-id", device_id)); 1392 _FDT(fdt_setprop_cell(fdt, offset, "revision-id", revision_id)); 1393 1394 _FDT(fdt_setprop_cell(fdt, offset, "class-code", ccode)); 1395 if (irq_pin) { 1396 _FDT(fdt_setprop_cell(fdt, offset, "interrupts", irq_pin)); 1397 } 1398 1399 if (subsystem_id) { 1400 _FDT(fdt_setprop_cell(fdt, offset, "subsystem-id", subsystem_id)); 1401 } 1402 1403 if (subsystem_vendor_id) { 1404 _FDT(fdt_setprop_cell(fdt, offset, "subsystem-vendor-id", 1405 subsystem_vendor_id)); 1406 } 1407 1408 _FDT(fdt_setprop_cell(fdt, offset, "cache-line-size", cache_line_size)); 1409 1410 1411 /* the following fdt cells are masked off the pci status register */ 1412 _FDT(fdt_setprop_cell(fdt, offset, "devsel-speed", 1413 PCI_STATUS_DEVSEL_MASK & pci_status)); 1414 1415 if (pci_status & PCI_STATUS_FAST_BACK) { 1416 _FDT(fdt_setprop(fdt, offset, "fast-back-to-back", NULL, 0)); 1417 } 1418 if (pci_status & PCI_STATUS_66MHZ) { 1419 _FDT(fdt_setprop(fdt, offset, "66mhz-capable", NULL, 0)); 1420 } 1421 if (pci_status & PCI_STATUS_UDF) { 1422 _FDT(fdt_setprop(fdt, offset, "udf-supported", NULL, 0)); 1423 } 1424 1425 loc_code = spapr_phb_get_loc_code(sphb, dev); 1426 _FDT(fdt_setprop_string(fdt, offset, "ibm,loc-code", loc_code)); 1427 g_free(loc_code); 1428 1429 if (drc) { 1430 _FDT(fdt_setprop_cell(fdt, offset, "ibm,my-drc-index", 1431 spapr_drc_index(drc))); 1432 } 1433 1434 if (msi_present(dev)) { 1435 uint32_t max_msi = msi_nr_vectors_allocated(dev); 1436 if (max_msi) { 1437 _FDT(fdt_setprop_cell(fdt, offset, "ibm,req#msi", max_msi)); 1438 } 1439 } 1440 if (msix_present(dev)) { 1441 uint32_t max_msix = dev->msix_entries_nr; 1442 if (max_msix) { 1443 _FDT(fdt_setprop_cell(fdt, offset, "ibm,req#msi-x", max_msix)); 1444 } 1445 } 1446 1447 populate_resource_props(dev, &rp); 1448 _FDT(fdt_setprop(fdt, offset, "reg", (uint8_t *)rp.reg, rp.reg_len)); 1449 1450 if (sphb->pcie_ecs && pci_is_express(dev)) { 1451 _FDT(fdt_setprop_cell(fdt, offset, "ibm,pci-config-space-type", 0x1)); 1452 } 1453 1454 spapr_phb_nvgpu_populate_pcidev_dt(dev, fdt, offset, sphb); 1455 1456 if (!pc->is_bridge) { 1457 /* Properties only for non-bridges */ 1458 uint32_t min_grant = pci_default_read_config(dev, PCI_MIN_GNT, 1); 1459 uint32_t max_latency = pci_default_read_config(dev, PCI_MAX_LAT, 1); 1460 _FDT(fdt_setprop_cell(fdt, offset, "min-grant", min_grant)); 1461 _FDT(fdt_setprop_cell(fdt, offset, "max-latency", max_latency)); 1462 return offset; 1463 } else { 1464 PCIBus *sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(dev)); 1465 1466 return spapr_dt_pci_bus(sphb, sec_bus, fdt, offset); 1467 } 1468 } 1469 1470 /* Callback to be called during DRC release. */ 1471 void spapr_phb_remove_pci_device_cb(DeviceState *dev) 1472 { 1473 HotplugHandler *hotplug_ctrl = qdev_get_hotplug_handler(dev); 1474 1475 hotplug_handler_unplug(hotplug_ctrl, dev, &error_abort); 1476 object_unparent(OBJECT(dev)); 1477 } 1478 1479 int spapr_pci_dt_populate(SpaprDrc *drc, SpaprMachineState *spapr, 1480 void *fdt, int *fdt_start_offset, Error **errp) 1481 { 1482 HotplugHandler *plug_handler = qdev_get_hotplug_handler(drc->dev); 1483 SpaprPhbState *sphb = SPAPR_PCI_HOST_BRIDGE(plug_handler); 1484 PCIDevice *pdev = PCI_DEVICE(drc->dev); 1485 1486 *fdt_start_offset = spapr_dt_pci_device(sphb, pdev, fdt, 0); 1487 return 0; 1488 } 1489 1490 static void spapr_pci_bridge_plug(SpaprPhbState *phb, 1491 PCIBridge *bridge, 1492 Error **errp) 1493 { 1494 Error *local_err = NULL; 1495 PCIBus *bus = pci_bridge_get_sec_bus(bridge); 1496 1497 add_drcs(phb, bus, &local_err); 1498 if (local_err) { 1499 error_propagate(errp, local_err); 1500 return; 1501 } 1502 } 1503 1504 static void spapr_pci_plug(HotplugHandler *plug_handler, 1505 DeviceState *plugged_dev, Error **errp) 1506 { 1507 SpaprPhbState *phb = SPAPR_PCI_HOST_BRIDGE(DEVICE(plug_handler)); 1508 PCIDevice *pdev = PCI_DEVICE(plugged_dev); 1509 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(plugged_dev); 1510 SpaprDrc *drc = drc_from_dev(phb, pdev); 1511 Error *local_err = NULL; 1512 PCIBus *bus = PCI_BUS(qdev_get_parent_bus(DEVICE(pdev))); 1513 uint32_t slotnr = PCI_SLOT(pdev->devfn); 1514 1515 /* if DR is disabled we don't need to do anything in the case of 1516 * hotplug or coldplug callbacks 1517 */ 1518 if (!phb->dr_enabled) { 1519 /* if this is a hotplug operation initiated by the user 1520 * we need to let them know it's not enabled 1521 */ 1522 if (plugged_dev->hotplugged) { 1523 error_setg(&local_err, QERR_BUS_NO_HOTPLUG, 1524 object_get_typename(OBJECT(phb))); 1525 } 1526 goto out; 1527 } 1528 1529 g_assert(drc); 1530 1531 if (pc->is_bridge) { 1532 spapr_pci_bridge_plug(phb, PCI_BRIDGE(plugged_dev), &local_err); 1533 if (local_err) { 1534 error_propagate(errp, local_err); 1535 return; 1536 } 1537 } 1538 1539 /* Following the QEMU convention used for PCIe multifunction 1540 * hotplug, we do not allow functions to be hotplugged to a 1541 * slot that already has function 0 present 1542 */ 1543 if (plugged_dev->hotplugged && bus->devices[PCI_DEVFN(slotnr, 0)] && 1544 PCI_FUNC(pdev->devfn) != 0) { 1545 error_setg(&local_err, "PCI: slot %d function 0 already ocuppied by %s," 1546 " additional functions can no longer be exposed to guest.", 1547 slotnr, bus->devices[PCI_DEVFN(slotnr, 0)]->name); 1548 goto out; 1549 } 1550 1551 spapr_drc_attach(drc, DEVICE(pdev), &local_err); 1552 if (local_err) { 1553 goto out; 1554 } 1555 1556 /* If this is function 0, signal hotplug for all the device functions. 1557 * Otherwise defer sending the hotplug event. 1558 */ 1559 if (!spapr_drc_hotplugged(plugged_dev)) { 1560 spapr_drc_reset(drc); 1561 } else if (PCI_FUNC(pdev->devfn) == 0) { 1562 int i; 1563 uint8_t chassis = chassis_from_bus(pci_get_bus(pdev), &local_err); 1564 1565 if (local_err) { 1566 error_propagate(errp, local_err); 1567 return; 1568 } 1569 1570 for (i = 0; i < 8; i++) { 1571 SpaprDrc *func_drc; 1572 SpaprDrcClass *func_drck; 1573 SpaprDREntitySense state; 1574 1575 func_drc = drc_from_devfn(phb, chassis, PCI_DEVFN(slotnr, i)); 1576 func_drck = SPAPR_DR_CONNECTOR_GET_CLASS(func_drc); 1577 state = func_drck->dr_entity_sense(func_drc); 1578 1579 if (state == SPAPR_DR_ENTITY_SENSE_PRESENT) { 1580 spapr_hotplug_req_add_by_index(func_drc); 1581 } 1582 } 1583 } 1584 1585 out: 1586 error_propagate(errp, local_err); 1587 } 1588 1589 static void spapr_pci_bridge_unplug(SpaprPhbState *phb, 1590 PCIBridge *bridge, 1591 Error **errp) 1592 { 1593 Error *local_err = NULL; 1594 PCIBus *bus = pci_bridge_get_sec_bus(bridge); 1595 1596 remove_drcs(phb, bus, &local_err); 1597 if (local_err) { 1598 error_propagate(errp, local_err); 1599 return; 1600 } 1601 } 1602 1603 static void spapr_pci_unplug(HotplugHandler *plug_handler, 1604 DeviceState *plugged_dev, Error **errp) 1605 { 1606 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(plugged_dev); 1607 SpaprPhbState *phb = SPAPR_PCI_HOST_BRIDGE(DEVICE(plug_handler)); 1608 1609 /* some version guests do not wait for completion of a device 1610 * cleanup (generally done asynchronously by the kernel) before 1611 * signaling to QEMU that the device is safe, but instead sleep 1612 * for some 'safe' period of time. unfortunately on a busy host 1613 * this sleep isn't guaranteed to be long enough, resulting in 1614 * bad things like IRQ lines being left asserted during final 1615 * device removal. to deal with this we call reset just prior 1616 * to finalizing the device, which will put the device back into 1617 * an 'idle' state, as the device cleanup code expects. 1618 */ 1619 pci_device_reset(PCI_DEVICE(plugged_dev)); 1620 1621 if (pc->is_bridge) { 1622 Error *local_err = NULL; 1623 spapr_pci_bridge_unplug(phb, PCI_BRIDGE(plugged_dev), &local_err); 1624 if (local_err) { 1625 error_propagate(errp, local_err); 1626 } 1627 return; 1628 } 1629 1630 object_property_set_bool(OBJECT(plugged_dev), false, "realized", 1631 &error_abort); 1632 } 1633 1634 static void spapr_pci_unplug_request(HotplugHandler *plug_handler, 1635 DeviceState *plugged_dev, Error **errp) 1636 { 1637 SpaprPhbState *phb = SPAPR_PCI_HOST_BRIDGE(DEVICE(plug_handler)); 1638 PCIDevice *pdev = PCI_DEVICE(plugged_dev); 1639 SpaprDrc *drc = drc_from_dev(phb, pdev); 1640 1641 if (!phb->dr_enabled) { 1642 error_setg(errp, QERR_BUS_NO_HOTPLUG, 1643 object_get_typename(OBJECT(phb))); 1644 return; 1645 } 1646 1647 g_assert(drc); 1648 g_assert(drc->dev == plugged_dev); 1649 1650 if (!spapr_drc_unplug_requested(drc)) { 1651 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(plugged_dev); 1652 uint32_t slotnr = PCI_SLOT(pdev->devfn); 1653 SpaprDrc *func_drc; 1654 SpaprDrcClass *func_drck; 1655 SpaprDREntitySense state; 1656 int i; 1657 Error *local_err = NULL; 1658 uint8_t chassis = chassis_from_bus(pci_get_bus(pdev), &local_err); 1659 1660 if (local_err) { 1661 error_propagate(errp, local_err); 1662 return; 1663 } 1664 1665 if (pc->is_bridge) { 1666 error_setg(errp, "PCI: Hot unplug of PCI bridges not supported"); 1667 return; 1668 } 1669 if (object_property_get_uint(OBJECT(pdev), "nvlink2-tgt", NULL)) { 1670 error_setg(errp, "PCI: Cannot unplug NVLink2 devices"); 1671 return; 1672 } 1673 1674 /* ensure any other present functions are pending unplug */ 1675 if (PCI_FUNC(pdev->devfn) == 0) { 1676 for (i = 1; i < 8; i++) { 1677 func_drc = drc_from_devfn(phb, chassis, PCI_DEVFN(slotnr, i)); 1678 func_drck = SPAPR_DR_CONNECTOR_GET_CLASS(func_drc); 1679 state = func_drck->dr_entity_sense(func_drc); 1680 if (state == SPAPR_DR_ENTITY_SENSE_PRESENT 1681 && !spapr_drc_unplug_requested(func_drc)) { 1682 /* 1683 * Attempting to remove function 0 of a multifunction 1684 * device will will cascade into removing all child 1685 * functions, even if their unplug weren't requested 1686 * beforehand. 1687 */ 1688 spapr_drc_detach(func_drc); 1689 } 1690 } 1691 } 1692 1693 spapr_drc_detach(drc); 1694 1695 /* if this isn't func 0, defer unplug event. otherwise signal removal 1696 * for all present functions 1697 */ 1698 if (PCI_FUNC(pdev->devfn) == 0) { 1699 for (i = 7; i >= 0; i--) { 1700 func_drc = drc_from_devfn(phb, chassis, PCI_DEVFN(slotnr, i)); 1701 func_drck = SPAPR_DR_CONNECTOR_GET_CLASS(func_drc); 1702 state = func_drck->dr_entity_sense(func_drc); 1703 if (state == SPAPR_DR_ENTITY_SENSE_PRESENT) { 1704 spapr_hotplug_req_remove_by_index(func_drc); 1705 } 1706 } 1707 } 1708 } 1709 } 1710 1711 static void spapr_phb_finalizefn(Object *obj) 1712 { 1713 SpaprPhbState *sphb = SPAPR_PCI_HOST_BRIDGE(obj); 1714 1715 g_free(sphb->dtbusname); 1716 sphb->dtbusname = NULL; 1717 } 1718 1719 static void spapr_phb_unrealize(DeviceState *dev) 1720 { 1721 SpaprMachineState *spapr = SPAPR_MACHINE(qdev_get_machine()); 1722 SysBusDevice *s = SYS_BUS_DEVICE(dev); 1723 PCIHostState *phb = PCI_HOST_BRIDGE(s); 1724 SpaprPhbState *sphb = SPAPR_PCI_HOST_BRIDGE(phb); 1725 SpaprTceTable *tcet; 1726 int i; 1727 const unsigned windows_supported = spapr_phb_windows_supported(sphb); 1728 1729 spapr_phb_nvgpu_free(sphb); 1730 1731 if (sphb->msi) { 1732 g_hash_table_unref(sphb->msi); 1733 sphb->msi = NULL; 1734 } 1735 1736 /* 1737 * Remove IO/MMIO subregions and aliases, rest should get cleaned 1738 * via PHB's unrealize->object_finalize 1739 */ 1740 for (i = windows_supported - 1; i >= 0; i--) { 1741 tcet = spapr_tce_find_by_liobn(sphb->dma_liobn[i]); 1742 if (tcet) { 1743 memory_region_del_subregion(&sphb->iommu_root, 1744 spapr_tce_get_iommu(tcet)); 1745 } 1746 } 1747 1748 remove_drcs(sphb, phb->bus, &error_abort); 1749 1750 for (i = PCI_NUM_PINS - 1; i >= 0; i--) { 1751 if (sphb->lsi_table[i].irq) { 1752 spapr_irq_free(spapr, sphb->lsi_table[i].irq, 1); 1753 sphb->lsi_table[i].irq = 0; 1754 } 1755 } 1756 1757 QLIST_REMOVE(sphb, list); 1758 1759 memory_region_del_subregion(&sphb->iommu_root, &sphb->msiwindow); 1760 1761 /* 1762 * An attached PCI device may have memory listeners, eg. VFIO PCI. We have 1763 * unmapped all sections. Remove the listeners now, before destroying the 1764 * address space. 1765 */ 1766 address_space_remove_listeners(&sphb->iommu_as); 1767 address_space_destroy(&sphb->iommu_as); 1768 1769 qbus_set_hotplug_handler(BUS(phb->bus), NULL, &error_abort); 1770 pci_unregister_root_bus(phb->bus); 1771 1772 memory_region_del_subregion(get_system_memory(), &sphb->iowindow); 1773 if (sphb->mem64_win_pciaddr != (hwaddr)-1) { 1774 memory_region_del_subregion(get_system_memory(), &sphb->mem64window); 1775 } 1776 memory_region_del_subregion(get_system_memory(), &sphb->mem32window); 1777 } 1778 1779 static void spapr_phb_destroy_msi(gpointer opaque) 1780 { 1781 SpaprMachineState *spapr = SPAPR_MACHINE(qdev_get_machine()); 1782 SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr); 1783 SpaprPciMsi *msi = opaque; 1784 1785 if (!smc->legacy_irq_allocation) { 1786 spapr_irq_msi_free(spapr, msi->first_irq, msi->num); 1787 } 1788 spapr_irq_free(spapr, msi->first_irq, msi->num); 1789 g_free(msi); 1790 } 1791 1792 static void spapr_phb_realize(DeviceState *dev, Error **errp) 1793 { 1794 /* We don't use SPAPR_MACHINE() in order to exit gracefully if the user 1795 * tries to add a sPAPR PHB to a non-pseries machine. 1796 */ 1797 SpaprMachineState *spapr = 1798 (SpaprMachineState *) object_dynamic_cast(qdev_get_machine(), 1799 TYPE_SPAPR_MACHINE); 1800 SpaprMachineClass *smc = spapr ? SPAPR_MACHINE_GET_CLASS(spapr) : NULL; 1801 SysBusDevice *s = SYS_BUS_DEVICE(dev); 1802 SpaprPhbState *sphb = SPAPR_PCI_HOST_BRIDGE(s); 1803 PCIHostState *phb = PCI_HOST_BRIDGE(s); 1804 MachineState *ms = MACHINE(spapr); 1805 char *namebuf; 1806 int i; 1807 PCIBus *bus; 1808 uint64_t msi_window_size = 4096; 1809 SpaprTceTable *tcet; 1810 const unsigned windows_supported = spapr_phb_windows_supported(sphb); 1811 Error *local_err = NULL; 1812 1813 if (!spapr) { 1814 error_setg(errp, TYPE_SPAPR_PCI_HOST_BRIDGE " needs a pseries machine"); 1815 return; 1816 } 1817 1818 assert(sphb->index != (uint32_t)-1); /* checked in spapr_phb_pre_plug() */ 1819 1820 if (sphb->mem64_win_size != 0) { 1821 if (sphb->mem_win_size > SPAPR_PCI_MEM32_WIN_SIZE) { 1822 error_setg(errp, "32-bit memory window of size 0x%"HWADDR_PRIx 1823 " (max 2 GiB)", sphb->mem_win_size); 1824 return; 1825 } 1826 1827 /* 64-bit window defaults to identity mapping */ 1828 sphb->mem64_win_pciaddr = sphb->mem64_win_addr; 1829 } else if (sphb->mem_win_size > SPAPR_PCI_MEM32_WIN_SIZE) { 1830 /* 1831 * For compatibility with old configuration, if no 64-bit MMIO 1832 * window is specified, but the ordinary (32-bit) memory 1833 * window is specified as > 2GiB, we treat it as a 2GiB 32-bit 1834 * window, with a 64-bit MMIO window following on immediately 1835 * afterwards 1836 */ 1837 sphb->mem64_win_size = sphb->mem_win_size - SPAPR_PCI_MEM32_WIN_SIZE; 1838 sphb->mem64_win_addr = sphb->mem_win_addr + SPAPR_PCI_MEM32_WIN_SIZE; 1839 sphb->mem64_win_pciaddr = 1840 SPAPR_PCI_MEM_WIN_BUS_OFFSET + SPAPR_PCI_MEM32_WIN_SIZE; 1841 sphb->mem_win_size = SPAPR_PCI_MEM32_WIN_SIZE; 1842 } 1843 1844 if (spapr_pci_find_phb(spapr, sphb->buid)) { 1845 SpaprPhbState *s; 1846 1847 error_setg(errp, "PCI host bridges must have unique indexes"); 1848 error_append_hint(errp, "The following indexes are already in use:"); 1849 QLIST_FOREACH(s, &spapr->phbs, list) { 1850 error_append_hint(errp, " %d", s->index); 1851 } 1852 error_append_hint(errp, "\nTry another value for the index property\n"); 1853 return; 1854 } 1855 1856 if (sphb->numa_node != -1 && 1857 (sphb->numa_node >= MAX_NODES || 1858 !ms->numa_state->nodes[sphb->numa_node].present)) { 1859 error_setg(errp, "Invalid NUMA node ID for PCI host bridge"); 1860 return; 1861 } 1862 1863 sphb->dtbusname = g_strdup_printf("pci@%" PRIx64, sphb->buid); 1864 1865 /* Initialize memory regions */ 1866 namebuf = g_strdup_printf("%s.mmio", sphb->dtbusname); 1867 memory_region_init(&sphb->memspace, OBJECT(sphb), namebuf, UINT64_MAX); 1868 g_free(namebuf); 1869 1870 namebuf = g_strdup_printf("%s.mmio32-alias", sphb->dtbusname); 1871 memory_region_init_alias(&sphb->mem32window, OBJECT(sphb), 1872 namebuf, &sphb->memspace, 1873 SPAPR_PCI_MEM_WIN_BUS_OFFSET, sphb->mem_win_size); 1874 g_free(namebuf); 1875 memory_region_add_subregion(get_system_memory(), sphb->mem_win_addr, 1876 &sphb->mem32window); 1877 1878 if (sphb->mem64_win_size != 0) { 1879 namebuf = g_strdup_printf("%s.mmio64-alias", sphb->dtbusname); 1880 memory_region_init_alias(&sphb->mem64window, OBJECT(sphb), 1881 namebuf, &sphb->memspace, 1882 sphb->mem64_win_pciaddr, sphb->mem64_win_size); 1883 g_free(namebuf); 1884 1885 memory_region_add_subregion(get_system_memory(), 1886 sphb->mem64_win_addr, 1887 &sphb->mem64window); 1888 } 1889 1890 /* Initialize IO regions */ 1891 namebuf = g_strdup_printf("%s.io", sphb->dtbusname); 1892 memory_region_init(&sphb->iospace, OBJECT(sphb), 1893 namebuf, SPAPR_PCI_IO_WIN_SIZE); 1894 g_free(namebuf); 1895 1896 namebuf = g_strdup_printf("%s.io-alias", sphb->dtbusname); 1897 memory_region_init_alias(&sphb->iowindow, OBJECT(sphb), namebuf, 1898 &sphb->iospace, 0, SPAPR_PCI_IO_WIN_SIZE); 1899 g_free(namebuf); 1900 memory_region_add_subregion(get_system_memory(), sphb->io_win_addr, 1901 &sphb->iowindow); 1902 1903 bus = pci_register_root_bus(dev, NULL, 1904 pci_spapr_set_irq, pci_swizzle_map_irq_fn, sphb, 1905 &sphb->memspace, &sphb->iospace, 1906 PCI_DEVFN(0, 0), PCI_NUM_PINS, 1907 TYPE_PCI_BUS); 1908 1909 /* 1910 * Despite resembling a vanilla PCI bus in most ways, the PAPR 1911 * para-virtualized PCI bus *does* permit PCI-E extended config 1912 * space access 1913 */ 1914 if (sphb->pcie_ecs) { 1915 bus->flags |= PCI_BUS_EXTENDED_CONFIG_SPACE; 1916 } 1917 phb->bus = bus; 1918 qbus_set_hotplug_handler(BUS(phb->bus), OBJECT(sphb), NULL); 1919 1920 /* 1921 * Initialize PHB address space. 1922 * By default there will be at least one subregion for default 1923 * 32bit DMA window. 1924 * Later the guest might want to create another DMA window 1925 * which will become another memory subregion. 1926 */ 1927 namebuf = g_strdup_printf("%s.iommu-root", sphb->dtbusname); 1928 memory_region_init(&sphb->iommu_root, OBJECT(sphb), 1929 namebuf, UINT64_MAX); 1930 g_free(namebuf); 1931 address_space_init(&sphb->iommu_as, &sphb->iommu_root, 1932 sphb->dtbusname); 1933 1934 /* 1935 * As MSI/MSIX interrupts trigger by writing at MSI/MSIX vectors, 1936 * we need to allocate some memory to catch those writes coming 1937 * from msi_notify()/msix_notify(). 1938 * As MSIMessage:addr is going to be the same and MSIMessage:data 1939 * is going to be a VIRQ number, 4 bytes of the MSI MR will only 1940 * be used. 1941 * 1942 * For KVM we want to ensure that this memory is a full page so that 1943 * our memory slot is of page size granularity. 1944 */ 1945 if (kvm_enabled()) { 1946 msi_window_size = qemu_real_host_page_size; 1947 } 1948 1949 memory_region_init_io(&sphb->msiwindow, OBJECT(sphb), &spapr_msi_ops, spapr, 1950 "msi", msi_window_size); 1951 memory_region_add_subregion(&sphb->iommu_root, SPAPR_PCI_MSI_WINDOW, 1952 &sphb->msiwindow); 1953 1954 pci_setup_iommu(bus, spapr_pci_dma_iommu, sphb); 1955 1956 pci_bus_set_route_irq_fn(bus, spapr_route_intx_pin_to_irq); 1957 1958 QLIST_INSERT_HEAD(&spapr->phbs, sphb, list); 1959 1960 /* Initialize the LSI table */ 1961 for (i = 0; i < PCI_NUM_PINS; i++) { 1962 uint32_t irq = SPAPR_IRQ_PCI_LSI + sphb->index * PCI_NUM_PINS + i; 1963 1964 if (smc->legacy_irq_allocation) { 1965 irq = spapr_irq_findone(spapr, &local_err); 1966 if (local_err) { 1967 error_propagate_prepend(errp, local_err, 1968 "can't allocate LSIs: "); 1969 /* 1970 * Older machines will never support PHB hotplug, ie, this is an 1971 * init only path and QEMU will terminate. No need to rollback. 1972 */ 1973 return; 1974 } 1975 } 1976 1977 spapr_irq_claim(spapr, irq, true, &local_err); 1978 if (local_err) { 1979 error_propagate_prepend(errp, local_err, "can't allocate LSIs: "); 1980 goto unrealize; 1981 } 1982 1983 sphb->lsi_table[i].irq = irq; 1984 } 1985 1986 /* allocate connectors for child PCI devices */ 1987 add_drcs(sphb, phb->bus, &local_err); 1988 if (local_err) { 1989 error_propagate(errp, local_err); 1990 goto unrealize; 1991 } 1992 1993 /* DMA setup */ 1994 for (i = 0; i < windows_supported; ++i) { 1995 tcet = spapr_tce_new_table(DEVICE(sphb), sphb->dma_liobn[i]); 1996 if (!tcet) { 1997 error_setg(errp, "Creating window#%d failed for %s", 1998 i, sphb->dtbusname); 1999 goto unrealize; 2000 } 2001 memory_region_add_subregion(&sphb->iommu_root, 0, 2002 spapr_tce_get_iommu(tcet)); 2003 } 2004 2005 sphb->msi = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, 2006 spapr_phb_destroy_msi); 2007 return; 2008 2009 unrealize: 2010 spapr_phb_unrealize(dev); 2011 } 2012 2013 static int spapr_phb_children_reset(Object *child, void *opaque) 2014 { 2015 DeviceState *dev = (DeviceState *) object_dynamic_cast(child, TYPE_DEVICE); 2016 2017 if (dev) { 2018 device_legacy_reset(dev); 2019 } 2020 2021 return 0; 2022 } 2023 2024 void spapr_phb_dma_reset(SpaprPhbState *sphb) 2025 { 2026 int i; 2027 SpaprTceTable *tcet; 2028 2029 for (i = 0; i < SPAPR_PCI_DMA_MAX_WINDOWS; ++i) { 2030 tcet = spapr_tce_find_by_liobn(sphb->dma_liobn[i]); 2031 2032 if (tcet && tcet->nb_table) { 2033 spapr_tce_table_disable(tcet); 2034 } 2035 } 2036 2037 /* Register default 32bit DMA window */ 2038 tcet = spapr_tce_find_by_liobn(sphb->dma_liobn[0]); 2039 spapr_tce_table_enable(tcet, SPAPR_TCE_PAGE_SHIFT, sphb->dma_win_addr, 2040 sphb->dma_win_size >> SPAPR_TCE_PAGE_SHIFT); 2041 } 2042 2043 static void spapr_phb_reset(DeviceState *qdev) 2044 { 2045 SpaprPhbState *sphb = SPAPR_PCI_HOST_BRIDGE(qdev); 2046 Error *err = NULL; 2047 2048 spapr_phb_dma_reset(sphb); 2049 spapr_phb_nvgpu_free(sphb); 2050 spapr_phb_nvgpu_setup(sphb, &err); 2051 if (err) { 2052 error_report_err(err); 2053 } 2054 2055 /* Reset the IOMMU state */ 2056 object_child_foreach(OBJECT(qdev), spapr_phb_children_reset, NULL); 2057 2058 if (spapr_phb_eeh_available(SPAPR_PCI_HOST_BRIDGE(qdev))) { 2059 spapr_phb_vfio_reset(qdev); 2060 } 2061 2062 g_hash_table_remove_all(sphb->msi); 2063 } 2064 2065 static Property spapr_phb_properties[] = { 2066 DEFINE_PROP_UINT32("index", SpaprPhbState, index, -1), 2067 DEFINE_PROP_UINT64("mem_win_size", SpaprPhbState, mem_win_size, 2068 SPAPR_PCI_MEM32_WIN_SIZE), 2069 DEFINE_PROP_UINT64("mem64_win_size", SpaprPhbState, mem64_win_size, 2070 SPAPR_PCI_MEM64_WIN_SIZE), 2071 DEFINE_PROP_UINT64("io_win_size", SpaprPhbState, io_win_size, 2072 SPAPR_PCI_IO_WIN_SIZE), 2073 DEFINE_PROP_BOOL("dynamic-reconfiguration", SpaprPhbState, dr_enabled, 2074 true), 2075 /* Default DMA window is 0..1GB */ 2076 DEFINE_PROP_UINT64("dma_win_addr", SpaprPhbState, dma_win_addr, 0), 2077 DEFINE_PROP_UINT64("dma_win_size", SpaprPhbState, dma_win_size, 0x40000000), 2078 DEFINE_PROP_UINT64("dma64_win_addr", SpaprPhbState, dma64_win_addr, 2079 0x800000000000000ULL), 2080 DEFINE_PROP_BOOL("ddw", SpaprPhbState, ddw_enabled, true), 2081 DEFINE_PROP_UINT64("pgsz", SpaprPhbState, page_size_mask, 2082 (1ULL << 12) | (1ULL << 16) 2083 | (1ULL << 21) | (1ULL << 24)), 2084 DEFINE_PROP_UINT32("numa_node", SpaprPhbState, numa_node, -1), 2085 DEFINE_PROP_BOOL("pre-2.8-migration", SpaprPhbState, 2086 pre_2_8_migration, false), 2087 DEFINE_PROP_BOOL("pcie-extended-configuration-space", SpaprPhbState, 2088 pcie_ecs, true), 2089 DEFINE_PROP_UINT64("gpa", SpaprPhbState, nv2_gpa_win_addr, 0), 2090 DEFINE_PROP_UINT64("atsd", SpaprPhbState, nv2_atsd_win_addr, 0), 2091 DEFINE_PROP_END_OF_LIST(), 2092 }; 2093 2094 static const VMStateDescription vmstate_spapr_pci_lsi = { 2095 .name = "spapr_pci/lsi", 2096 .version_id = 1, 2097 .minimum_version_id = 1, 2098 .fields = (VMStateField[]) { 2099 VMSTATE_UINT32_EQUAL(irq, SpaprPciLsi, NULL), 2100 2101 VMSTATE_END_OF_LIST() 2102 }, 2103 }; 2104 2105 static const VMStateDescription vmstate_spapr_pci_msi = { 2106 .name = "spapr_pci/msi", 2107 .version_id = 1, 2108 .minimum_version_id = 1, 2109 .fields = (VMStateField []) { 2110 VMSTATE_UINT32(key, SpaprPciMsiMig), 2111 VMSTATE_UINT32(value.first_irq, SpaprPciMsiMig), 2112 VMSTATE_UINT32(value.num, SpaprPciMsiMig), 2113 VMSTATE_END_OF_LIST() 2114 }, 2115 }; 2116 2117 static int spapr_pci_pre_save(void *opaque) 2118 { 2119 SpaprPhbState *sphb = opaque; 2120 GHashTableIter iter; 2121 gpointer key, value; 2122 int i; 2123 2124 if (sphb->pre_2_8_migration) { 2125 sphb->mig_liobn = sphb->dma_liobn[0]; 2126 sphb->mig_mem_win_addr = sphb->mem_win_addr; 2127 sphb->mig_mem_win_size = sphb->mem_win_size; 2128 sphb->mig_io_win_addr = sphb->io_win_addr; 2129 sphb->mig_io_win_size = sphb->io_win_size; 2130 2131 if ((sphb->mem64_win_size != 0) 2132 && (sphb->mem64_win_addr 2133 == (sphb->mem_win_addr + sphb->mem_win_size))) { 2134 sphb->mig_mem_win_size += sphb->mem64_win_size; 2135 } 2136 } 2137 2138 g_free(sphb->msi_devs); 2139 sphb->msi_devs = NULL; 2140 sphb->msi_devs_num = g_hash_table_size(sphb->msi); 2141 if (!sphb->msi_devs_num) { 2142 return 0; 2143 } 2144 sphb->msi_devs = g_new(SpaprPciMsiMig, sphb->msi_devs_num); 2145 2146 g_hash_table_iter_init(&iter, sphb->msi); 2147 for (i = 0; g_hash_table_iter_next(&iter, &key, &value); ++i) { 2148 sphb->msi_devs[i].key = *(uint32_t *) key; 2149 sphb->msi_devs[i].value = *(SpaprPciMsi *) value; 2150 } 2151 2152 return 0; 2153 } 2154 2155 static int spapr_pci_post_load(void *opaque, int version_id) 2156 { 2157 SpaprPhbState *sphb = opaque; 2158 gpointer key, value; 2159 int i; 2160 2161 for (i = 0; i < sphb->msi_devs_num; ++i) { 2162 key = g_memdup(&sphb->msi_devs[i].key, 2163 sizeof(sphb->msi_devs[i].key)); 2164 value = g_memdup(&sphb->msi_devs[i].value, 2165 sizeof(sphb->msi_devs[i].value)); 2166 g_hash_table_insert(sphb->msi, key, value); 2167 } 2168 g_free(sphb->msi_devs); 2169 sphb->msi_devs = NULL; 2170 sphb->msi_devs_num = 0; 2171 2172 return 0; 2173 } 2174 2175 static bool pre_2_8_migration(void *opaque, int version_id) 2176 { 2177 SpaprPhbState *sphb = opaque; 2178 2179 return sphb->pre_2_8_migration; 2180 } 2181 2182 static const VMStateDescription vmstate_spapr_pci = { 2183 .name = "spapr_pci", 2184 .version_id = 2, 2185 .minimum_version_id = 2, 2186 .pre_save = spapr_pci_pre_save, 2187 .post_load = spapr_pci_post_load, 2188 .fields = (VMStateField[]) { 2189 VMSTATE_UINT64_EQUAL(buid, SpaprPhbState, NULL), 2190 VMSTATE_UINT32_TEST(mig_liobn, SpaprPhbState, pre_2_8_migration), 2191 VMSTATE_UINT64_TEST(mig_mem_win_addr, SpaprPhbState, pre_2_8_migration), 2192 VMSTATE_UINT64_TEST(mig_mem_win_size, SpaprPhbState, pre_2_8_migration), 2193 VMSTATE_UINT64_TEST(mig_io_win_addr, SpaprPhbState, pre_2_8_migration), 2194 VMSTATE_UINT64_TEST(mig_io_win_size, SpaprPhbState, pre_2_8_migration), 2195 VMSTATE_STRUCT_ARRAY(lsi_table, SpaprPhbState, PCI_NUM_PINS, 0, 2196 vmstate_spapr_pci_lsi, SpaprPciLsi), 2197 VMSTATE_INT32(msi_devs_num, SpaprPhbState), 2198 VMSTATE_STRUCT_VARRAY_ALLOC(msi_devs, SpaprPhbState, msi_devs_num, 0, 2199 vmstate_spapr_pci_msi, SpaprPciMsiMig), 2200 VMSTATE_END_OF_LIST() 2201 }, 2202 }; 2203 2204 static const char *spapr_phb_root_bus_path(PCIHostState *host_bridge, 2205 PCIBus *rootbus) 2206 { 2207 SpaprPhbState *sphb = SPAPR_PCI_HOST_BRIDGE(host_bridge); 2208 2209 return sphb->dtbusname; 2210 } 2211 2212 static void spapr_phb_class_init(ObjectClass *klass, void *data) 2213 { 2214 PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(klass); 2215 DeviceClass *dc = DEVICE_CLASS(klass); 2216 HotplugHandlerClass *hp = HOTPLUG_HANDLER_CLASS(klass); 2217 2218 hc->root_bus_path = spapr_phb_root_bus_path; 2219 dc->realize = spapr_phb_realize; 2220 dc->unrealize = spapr_phb_unrealize; 2221 device_class_set_props(dc, spapr_phb_properties); 2222 dc->reset = spapr_phb_reset; 2223 dc->vmsd = &vmstate_spapr_pci; 2224 /* Supported by TYPE_SPAPR_MACHINE */ 2225 dc->user_creatable = true; 2226 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories); 2227 hp->plug = spapr_pci_plug; 2228 hp->unplug = spapr_pci_unplug; 2229 hp->unplug_request = spapr_pci_unplug_request; 2230 } 2231 2232 static const TypeInfo spapr_phb_info = { 2233 .name = TYPE_SPAPR_PCI_HOST_BRIDGE, 2234 .parent = TYPE_PCI_HOST_BRIDGE, 2235 .instance_size = sizeof(SpaprPhbState), 2236 .instance_finalize = spapr_phb_finalizefn, 2237 .class_init = spapr_phb_class_init, 2238 .interfaces = (InterfaceInfo[]) { 2239 { TYPE_HOTPLUG_HANDLER }, 2240 { } 2241 } 2242 }; 2243 2244 static void spapr_phb_pci_enumerate_bridge(PCIBus *bus, PCIDevice *pdev, 2245 void *opaque) 2246 { 2247 unsigned int *bus_no = opaque; 2248 PCIBus *sec_bus = NULL; 2249 2250 if ((pci_default_read_config(pdev, PCI_HEADER_TYPE, 1) != 2251 PCI_HEADER_TYPE_BRIDGE)) { 2252 return; 2253 } 2254 2255 (*bus_no)++; 2256 pci_default_write_config(pdev, PCI_PRIMARY_BUS, pci_dev_bus_num(pdev), 1); 2257 pci_default_write_config(pdev, PCI_SECONDARY_BUS, *bus_no, 1); 2258 pci_default_write_config(pdev, PCI_SUBORDINATE_BUS, *bus_no, 1); 2259 2260 sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev)); 2261 if (!sec_bus) { 2262 return; 2263 } 2264 2265 pci_for_each_device(sec_bus, pci_bus_num(sec_bus), 2266 spapr_phb_pci_enumerate_bridge, bus_no); 2267 pci_default_write_config(pdev, PCI_SUBORDINATE_BUS, *bus_no, 1); 2268 } 2269 2270 static void spapr_phb_pci_enumerate(SpaprPhbState *phb) 2271 { 2272 PCIBus *bus = PCI_HOST_BRIDGE(phb)->bus; 2273 unsigned int bus_no = 0; 2274 2275 pci_for_each_device(bus, pci_bus_num(bus), 2276 spapr_phb_pci_enumerate_bridge, 2277 &bus_no); 2278 2279 } 2280 2281 int spapr_dt_phb(SpaprMachineState *spapr, SpaprPhbState *phb, 2282 uint32_t intc_phandle, void *fdt, int *node_offset) 2283 { 2284 int bus_off, i, j, ret; 2285 uint32_t bus_range[] = { cpu_to_be32(0), cpu_to_be32(0xff) }; 2286 struct { 2287 uint32_t hi; 2288 uint64_t child; 2289 uint64_t parent; 2290 uint64_t size; 2291 } QEMU_PACKED ranges[] = { 2292 { 2293 cpu_to_be32(b_ss(1)), cpu_to_be64(0), 2294 cpu_to_be64(phb->io_win_addr), 2295 cpu_to_be64(memory_region_size(&phb->iospace)), 2296 }, 2297 { 2298 cpu_to_be32(b_ss(2)), cpu_to_be64(SPAPR_PCI_MEM_WIN_BUS_OFFSET), 2299 cpu_to_be64(phb->mem_win_addr), 2300 cpu_to_be64(phb->mem_win_size), 2301 }, 2302 { 2303 cpu_to_be32(b_ss(3)), cpu_to_be64(phb->mem64_win_pciaddr), 2304 cpu_to_be64(phb->mem64_win_addr), 2305 cpu_to_be64(phb->mem64_win_size), 2306 }, 2307 }; 2308 const unsigned sizeof_ranges = 2309 (phb->mem64_win_size ? 3 : 2) * sizeof(ranges[0]); 2310 uint64_t bus_reg[] = { cpu_to_be64(phb->buid), 0 }; 2311 uint32_t interrupt_map_mask[] = { 2312 cpu_to_be32(b_ddddd(-1)|b_fff(0)), 0x0, 0x0, cpu_to_be32(-1)}; 2313 uint32_t interrupt_map[PCI_SLOT_MAX * PCI_NUM_PINS][7]; 2314 uint32_t ddw_applicable[] = { 2315 cpu_to_be32(RTAS_IBM_QUERY_PE_DMA_WINDOW), 2316 cpu_to_be32(RTAS_IBM_CREATE_PE_DMA_WINDOW), 2317 cpu_to_be32(RTAS_IBM_REMOVE_PE_DMA_WINDOW) 2318 }; 2319 uint32_t ddw_extensions[] = { 2320 cpu_to_be32(1), 2321 cpu_to_be32(RTAS_IBM_RESET_PE_DMA_WINDOW) 2322 }; 2323 uint32_t associativity[] = {cpu_to_be32(0x4), 2324 cpu_to_be32(0x0), 2325 cpu_to_be32(0x0), 2326 cpu_to_be32(0x0), 2327 cpu_to_be32(phb->numa_node)}; 2328 SpaprTceTable *tcet; 2329 SpaprDrc *drc; 2330 Error *err = NULL; 2331 2332 /* Start populating the FDT */ 2333 _FDT(bus_off = fdt_add_subnode(fdt, 0, phb->dtbusname)); 2334 if (node_offset) { 2335 *node_offset = bus_off; 2336 } 2337 2338 /* Write PHB properties */ 2339 _FDT(fdt_setprop_string(fdt, bus_off, "device_type", "pci")); 2340 _FDT(fdt_setprop_string(fdt, bus_off, "compatible", "IBM,Logical_PHB")); 2341 _FDT(fdt_setprop_cell(fdt, bus_off, "#interrupt-cells", 0x1)); 2342 _FDT(fdt_setprop(fdt, bus_off, "used-by-rtas", NULL, 0)); 2343 _FDT(fdt_setprop(fdt, bus_off, "bus-range", &bus_range, sizeof(bus_range))); 2344 _FDT(fdt_setprop(fdt, bus_off, "ranges", &ranges, sizeof_ranges)); 2345 _FDT(fdt_setprop(fdt, bus_off, "reg", &bus_reg, sizeof(bus_reg))); 2346 _FDT(fdt_setprop_cell(fdt, bus_off, "ibm,pci-config-space-type", 0x1)); 2347 _FDT(fdt_setprop_cell(fdt, bus_off, "ibm,pe-total-#msi", 2348 spapr_irq_nr_msis(spapr))); 2349 2350 /* Dynamic DMA window */ 2351 if (phb->ddw_enabled) { 2352 _FDT(fdt_setprop(fdt, bus_off, "ibm,ddw-applicable", &ddw_applicable, 2353 sizeof(ddw_applicable))); 2354 _FDT(fdt_setprop(fdt, bus_off, "ibm,ddw-extensions", 2355 &ddw_extensions, sizeof(ddw_extensions))); 2356 } 2357 2358 /* Advertise NUMA via ibm,associativity */ 2359 if (phb->numa_node != -1) { 2360 _FDT(fdt_setprop(fdt, bus_off, "ibm,associativity", associativity, 2361 sizeof(associativity))); 2362 } 2363 2364 /* Build the interrupt-map, this must matches what is done 2365 * in pci_swizzle_map_irq_fn 2366 */ 2367 _FDT(fdt_setprop(fdt, bus_off, "interrupt-map-mask", 2368 &interrupt_map_mask, sizeof(interrupt_map_mask))); 2369 for (i = 0; i < PCI_SLOT_MAX; i++) { 2370 for (j = 0; j < PCI_NUM_PINS; j++) { 2371 uint32_t *irqmap = interrupt_map[i*PCI_NUM_PINS + j]; 2372 int lsi_num = pci_swizzle(i, j); 2373 2374 irqmap[0] = cpu_to_be32(b_ddddd(i)|b_fff(0)); 2375 irqmap[1] = 0; 2376 irqmap[2] = 0; 2377 irqmap[3] = cpu_to_be32(j+1); 2378 irqmap[4] = cpu_to_be32(intc_phandle); 2379 spapr_dt_irq(&irqmap[5], phb->lsi_table[lsi_num].irq, true); 2380 } 2381 } 2382 /* Write interrupt map */ 2383 _FDT(fdt_setprop(fdt, bus_off, "interrupt-map", &interrupt_map, 2384 sizeof(interrupt_map))); 2385 2386 tcet = spapr_tce_find_by_liobn(phb->dma_liobn[0]); 2387 if (!tcet) { 2388 return -1; 2389 } 2390 spapr_dma_dt(fdt, bus_off, "ibm,dma-window", 2391 tcet->liobn, tcet->bus_offset, 2392 tcet->nb_table << tcet->page_shift); 2393 2394 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_PHB, phb->index); 2395 if (drc) { 2396 uint32_t drc_index = cpu_to_be32(spapr_drc_index(drc)); 2397 2398 _FDT(fdt_setprop(fdt, bus_off, "ibm,my-drc-index", &drc_index, 2399 sizeof(drc_index))); 2400 } 2401 2402 /* Walk the bridges and program the bus numbers*/ 2403 spapr_phb_pci_enumerate(phb); 2404 _FDT(fdt_setprop_cell(fdt, bus_off, "qemu,phb-enumerated", 0x1)); 2405 2406 /* Walk the bridge and subordinate buses */ 2407 ret = spapr_dt_pci_bus(phb, PCI_HOST_BRIDGE(phb)->bus, fdt, bus_off); 2408 if (ret < 0) { 2409 return ret; 2410 } 2411 2412 spapr_phb_nvgpu_populate_dt(phb, fdt, bus_off, &err); 2413 if (err) { 2414 error_report_err(err); 2415 } 2416 spapr_phb_nvgpu_ram_populate_dt(phb, fdt); 2417 2418 return 0; 2419 } 2420 2421 void spapr_pci_rtas_init(void) 2422 { 2423 spapr_rtas_register(RTAS_READ_PCI_CONFIG, "read-pci-config", 2424 rtas_read_pci_config); 2425 spapr_rtas_register(RTAS_WRITE_PCI_CONFIG, "write-pci-config", 2426 rtas_write_pci_config); 2427 spapr_rtas_register(RTAS_IBM_READ_PCI_CONFIG, "ibm,read-pci-config", 2428 rtas_ibm_read_pci_config); 2429 spapr_rtas_register(RTAS_IBM_WRITE_PCI_CONFIG, "ibm,write-pci-config", 2430 rtas_ibm_write_pci_config); 2431 if (msi_nonbroken) { 2432 spapr_rtas_register(RTAS_IBM_QUERY_INTERRUPT_SOURCE_NUMBER, 2433 "ibm,query-interrupt-source-number", 2434 rtas_ibm_query_interrupt_source_number); 2435 spapr_rtas_register(RTAS_IBM_CHANGE_MSI, "ibm,change-msi", 2436 rtas_ibm_change_msi); 2437 } 2438 2439 spapr_rtas_register(RTAS_IBM_SET_EEH_OPTION, 2440 "ibm,set-eeh-option", 2441 rtas_ibm_set_eeh_option); 2442 spapr_rtas_register(RTAS_IBM_GET_CONFIG_ADDR_INFO2, 2443 "ibm,get-config-addr-info2", 2444 rtas_ibm_get_config_addr_info2); 2445 spapr_rtas_register(RTAS_IBM_READ_SLOT_RESET_STATE2, 2446 "ibm,read-slot-reset-state2", 2447 rtas_ibm_read_slot_reset_state2); 2448 spapr_rtas_register(RTAS_IBM_SET_SLOT_RESET, 2449 "ibm,set-slot-reset", 2450 rtas_ibm_set_slot_reset); 2451 spapr_rtas_register(RTAS_IBM_CONFIGURE_PE, 2452 "ibm,configure-pe", 2453 rtas_ibm_configure_pe); 2454 spapr_rtas_register(RTAS_IBM_SLOT_ERROR_DETAIL, 2455 "ibm,slot-error-detail", 2456 rtas_ibm_slot_error_detail); 2457 } 2458 2459 static void spapr_pci_register_types(void) 2460 { 2461 type_register_static(&spapr_phb_info); 2462 } 2463 2464 type_init(spapr_pci_register_types) 2465 2466 static int spapr_switch_one_vga(DeviceState *dev, void *opaque) 2467 { 2468 bool be = *(bool *)opaque; 2469 2470 if (object_dynamic_cast(OBJECT(dev), "VGA") 2471 || object_dynamic_cast(OBJECT(dev), "secondary-vga")) { 2472 object_property_set_bool(OBJECT(dev), be, "big-endian-framebuffer", 2473 &error_abort); 2474 } 2475 return 0; 2476 } 2477 2478 void spapr_pci_switch_vga(bool big_endian) 2479 { 2480 SpaprMachineState *spapr = SPAPR_MACHINE(qdev_get_machine()); 2481 SpaprPhbState *sphb; 2482 2483 /* 2484 * For backward compatibility with existing guests, we switch 2485 * the endianness of the VGA controller when changing the guest 2486 * interrupt mode 2487 */ 2488 QLIST_FOREACH(sphb, &spapr->phbs, list) { 2489 BusState *bus = &PCI_HOST_BRIDGE(sphb)->bus->qbus; 2490 qbus_walk_children(bus, spapr_switch_one_vga, NULL, NULL, NULL, 2491 &big_endian); 2492 } 2493 } 2494