spapr/drc: Drop spapr_drc_attach() fdt argument
[qemu/ar7.git] / hw / ppc / spapr_pci.c
blobe2bc9fec824b859a03d8b5e452486ffbd066df74
1 /*
2 * QEMU sPAPR PCI host originated from Uninorth PCI host
4 * Copyright (c) 2011 Alexey Kardashevskiy, IBM Corporation.
5 * Copyright (C) 2011 David Gibson, IBM Corporation.
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "qemu-common.h"
28 #include "cpu.h"
29 #include "hw/hw.h"
30 #include "hw/sysbus.h"
31 #include "hw/pci/pci.h"
32 #include "hw/pci/msi.h"
33 #include "hw/pci/msix.h"
34 #include "hw/pci/pci_host.h"
35 #include "hw/ppc/spapr.h"
36 #include "hw/pci-host/spapr.h"
37 #include "exec/address-spaces.h"
38 #include "exec/ram_addr.h"
39 #include <libfdt.h>
40 #include "trace.h"
41 #include "qemu/error-report.h"
42 #include "qapi/qmp/qerror.h"
43 #include "hw/ppc/fdt.h"
44 #include "hw/pci/pci_bridge.h"
45 #include "hw/pci/pci_bus.h"
46 #include "hw/pci/pci_ids.h"
47 #include "hw/ppc/spapr_drc.h"
48 #include "sysemu/device_tree.h"
49 #include "sysemu/kvm.h"
50 #include "sysemu/hostmem.h"
51 #include "sysemu/numa.h"
53 /* Copied from the kernel arch/powerpc/platforms/pseries/msi.c */
54 #define RTAS_QUERY_FN 0
55 #define RTAS_CHANGE_FN 1
56 #define RTAS_RESET_FN 2
57 #define RTAS_CHANGE_MSI_FN 3
58 #define RTAS_CHANGE_MSIX_FN 4
60 /* Interrupt types to return on RTAS_CHANGE_* */
61 #define RTAS_TYPE_MSI 1
62 #define RTAS_TYPE_MSIX 2
64 sPAPRPHBState *spapr_pci_find_phb(sPAPRMachineState *spapr, uint64_t buid)
66 sPAPRPHBState *sphb;
68 QLIST_FOREACH(sphb, &spapr->phbs, list) {
69 if (sphb->buid != buid) {
70 continue;
72 return sphb;
75 return NULL;
78 PCIDevice *spapr_pci_find_dev(sPAPRMachineState *spapr, uint64_t buid,
79 uint32_t config_addr)
81 sPAPRPHBState *sphb = spapr_pci_find_phb(spapr, buid);
82 PCIHostState *phb = PCI_HOST_BRIDGE(sphb);
83 int bus_num = (config_addr >> 16) & 0xFF;
84 int devfn = (config_addr >> 8) & 0xFF;
86 if (!phb) {
87 return NULL;
90 return pci_find_device(phb->bus, bus_num, devfn);
93 static uint32_t rtas_pci_cfgaddr(uint32_t arg)
95 /* This handles the encoding of extended config space addresses */
96 return ((arg >> 20) & 0xf00) | (arg & 0xff);
99 static void finish_read_pci_config(sPAPRMachineState *spapr, uint64_t buid,
100 uint32_t addr, uint32_t size,
101 target_ulong rets)
103 PCIDevice *pci_dev;
104 uint32_t val;
106 if ((size != 1) && (size != 2) && (size != 4)) {
107 /* access must be 1, 2 or 4 bytes */
108 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
109 return;
112 pci_dev = spapr_pci_find_dev(spapr, buid, addr);
113 addr = rtas_pci_cfgaddr(addr);
115 if (!pci_dev || (addr % size) || (addr >= pci_config_size(pci_dev))) {
116 /* Access must be to a valid device, within bounds and
117 * naturally aligned */
118 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
119 return;
122 val = pci_host_config_read_common(pci_dev, addr,
123 pci_config_size(pci_dev), size);
125 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
126 rtas_st(rets, 1, val);
129 static void rtas_ibm_read_pci_config(PowerPCCPU *cpu, sPAPRMachineState *spapr,
130 uint32_t token, uint32_t nargs,
131 target_ulong args,
132 uint32_t nret, target_ulong rets)
134 uint64_t buid;
135 uint32_t size, addr;
137 if ((nargs != 4) || (nret != 2)) {
138 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
139 return;
142 buid = rtas_ldq(args, 1);
143 size = rtas_ld(args, 3);
144 addr = rtas_ld(args, 0);
146 finish_read_pci_config(spapr, buid, addr, size, rets);
149 static void rtas_read_pci_config(PowerPCCPU *cpu, sPAPRMachineState *spapr,
150 uint32_t token, uint32_t nargs,
151 target_ulong args,
152 uint32_t nret, target_ulong rets)
154 uint32_t size, addr;
156 if ((nargs != 2) || (nret != 2)) {
157 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
158 return;
161 size = rtas_ld(args, 1);
162 addr = rtas_ld(args, 0);
164 finish_read_pci_config(spapr, 0, addr, size, rets);
167 static void finish_write_pci_config(sPAPRMachineState *spapr, uint64_t buid,
168 uint32_t addr, uint32_t size,
169 uint32_t val, target_ulong rets)
171 PCIDevice *pci_dev;
173 if ((size != 1) && (size != 2) && (size != 4)) {
174 /* access must be 1, 2 or 4 bytes */
175 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
176 return;
179 pci_dev = spapr_pci_find_dev(spapr, buid, addr);
180 addr = rtas_pci_cfgaddr(addr);
182 if (!pci_dev || (addr % size) || (addr >= pci_config_size(pci_dev))) {
183 /* Access must be to a valid device, within bounds and
184 * naturally aligned */
185 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
186 return;
189 pci_host_config_write_common(pci_dev, addr, pci_config_size(pci_dev),
190 val, size);
192 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
195 static void rtas_ibm_write_pci_config(PowerPCCPU *cpu, sPAPRMachineState *spapr,
196 uint32_t token, uint32_t nargs,
197 target_ulong args,
198 uint32_t nret, target_ulong rets)
200 uint64_t buid;
201 uint32_t val, size, addr;
203 if ((nargs != 5) || (nret != 1)) {
204 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
205 return;
208 buid = rtas_ldq(args, 1);
209 val = rtas_ld(args, 4);
210 size = rtas_ld(args, 3);
211 addr = rtas_ld(args, 0);
213 finish_write_pci_config(spapr, buid, addr, size, val, rets);
216 static void rtas_write_pci_config(PowerPCCPU *cpu, sPAPRMachineState *spapr,
217 uint32_t token, uint32_t nargs,
218 target_ulong args,
219 uint32_t nret, target_ulong rets)
221 uint32_t val, size, addr;
223 if ((nargs != 3) || (nret != 1)) {
224 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
225 return;
229 val = rtas_ld(args, 2);
230 size = rtas_ld(args, 1);
231 addr = rtas_ld(args, 0);
233 finish_write_pci_config(spapr, 0, addr, size, val, rets);
237 * Set MSI/MSIX message data.
238 * This is required for msi_notify()/msix_notify() which
239 * will write at the addresses via spapr_msi_write().
241 * If hwaddr == 0, all entries will have .data == first_irq i.e.
242 * table will be reset.
244 static void spapr_msi_setmsg(PCIDevice *pdev, hwaddr addr, bool msix,
245 unsigned first_irq, unsigned req_num)
247 unsigned i;
248 MSIMessage msg = { .address = addr, .data = first_irq };
250 if (!msix) {
251 msi_set_message(pdev, msg);
252 trace_spapr_pci_msi_setup(pdev->name, 0, msg.address);
253 return;
256 for (i = 0; i < req_num; ++i) {
257 msix_set_message(pdev, i, msg);
258 trace_spapr_pci_msi_setup(pdev->name, i, msg.address);
259 if (addr) {
260 ++msg.data;
265 static void rtas_ibm_change_msi(PowerPCCPU *cpu, sPAPRMachineState *spapr,
266 uint32_t token, uint32_t nargs,
267 target_ulong args, uint32_t nret,
268 target_ulong rets)
270 sPAPRMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
271 uint32_t config_addr = rtas_ld(args, 0);
272 uint64_t buid = rtas_ldq(args, 1);
273 unsigned int func = rtas_ld(args, 3);
274 unsigned int req_num = rtas_ld(args, 4); /* 0 == remove all */
275 unsigned int seq_num = rtas_ld(args, 5);
276 unsigned int ret_intr_type;
277 unsigned int irq, max_irqs = 0;
278 sPAPRPHBState *phb = NULL;
279 PCIDevice *pdev = NULL;
280 spapr_pci_msi *msi;
281 int *config_addr_key;
282 Error *err = NULL;
283 int i;
285 /* Fins sPAPRPHBState */
286 phb = spapr_pci_find_phb(spapr, buid);
287 if (phb) {
288 pdev = spapr_pci_find_dev(spapr, buid, config_addr);
290 if (!phb || !pdev) {
291 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
292 return;
295 switch (func) {
296 case RTAS_CHANGE_FN:
297 if (msi_present(pdev)) {
298 ret_intr_type = RTAS_TYPE_MSI;
299 } else if (msix_present(pdev)) {
300 ret_intr_type = RTAS_TYPE_MSIX;
301 } else {
302 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
303 return;
305 break;
306 case RTAS_CHANGE_MSI_FN:
307 if (msi_present(pdev)) {
308 ret_intr_type = RTAS_TYPE_MSI;
309 } else {
310 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
311 return;
313 break;
314 case RTAS_CHANGE_MSIX_FN:
315 if (msix_present(pdev)) {
316 ret_intr_type = RTAS_TYPE_MSIX;
317 } else {
318 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
319 return;
321 break;
322 default:
323 error_report("rtas_ibm_change_msi(%u) is not implemented", func);
324 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
325 return;
328 msi = (spapr_pci_msi *) g_hash_table_lookup(phb->msi, &config_addr);
330 /* Releasing MSIs */
331 if (!req_num) {
332 if (!msi) {
333 trace_spapr_pci_msi("Releasing wrong config", config_addr);
334 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
335 return;
338 if (!smc->legacy_irq_allocation) {
339 spapr_irq_msi_free(spapr, msi->first_irq, msi->num);
341 spapr_irq_free(spapr, msi->first_irq, msi->num);
342 if (msi_present(pdev)) {
343 spapr_msi_setmsg(pdev, 0, false, 0, 0);
345 if (msix_present(pdev)) {
346 spapr_msi_setmsg(pdev, 0, true, 0, 0);
348 g_hash_table_remove(phb->msi, &config_addr);
350 trace_spapr_pci_msi("Released MSIs", config_addr);
351 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
352 rtas_st(rets, 1, 0);
353 return;
356 /* Enabling MSI */
358 /* Check if the device supports as many IRQs as requested */
359 if (ret_intr_type == RTAS_TYPE_MSI) {
360 max_irqs = msi_nr_vectors_allocated(pdev);
361 } else if (ret_intr_type == RTAS_TYPE_MSIX) {
362 max_irqs = pdev->msix_entries_nr;
364 if (!max_irqs) {
365 error_report("Requested interrupt type %d is not enabled for device %x",
366 ret_intr_type, config_addr);
367 rtas_st(rets, 0, -1); /* Hardware error */
368 return;
370 /* Correct the number if the guest asked for too many */
371 if (req_num > max_irqs) {
372 trace_spapr_pci_msi_retry(config_addr, req_num, max_irqs);
373 req_num = max_irqs;
374 irq = 0; /* to avoid misleading trace */
375 goto out;
378 /* Allocate MSIs */
379 if (smc->legacy_irq_allocation) {
380 irq = spapr_irq_find(spapr, req_num, ret_intr_type == RTAS_TYPE_MSI,
381 &err);
382 } else {
383 irq = spapr_irq_msi_alloc(spapr, req_num,
384 ret_intr_type == RTAS_TYPE_MSI, &err);
386 if (err) {
387 error_reportf_err(err, "Can't allocate MSIs for device %x: ",
388 config_addr);
389 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
390 return;
393 for (i = 0; i < req_num; i++) {
394 spapr_irq_claim(spapr, irq + i, false, &err);
395 if (err) {
396 if (i) {
397 spapr_irq_free(spapr, irq, i);
399 if (!smc->legacy_irq_allocation) {
400 spapr_irq_msi_free(spapr, irq, req_num);
402 error_reportf_err(err, "Can't allocate MSIs for device %x: ",
403 config_addr);
404 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
405 return;
409 /* Release previous MSIs */
410 if (msi) {
411 if (!smc->legacy_irq_allocation) {
412 spapr_irq_msi_free(spapr, msi->first_irq, msi->num);
414 spapr_irq_free(spapr, msi->first_irq, msi->num);
415 g_hash_table_remove(phb->msi, &config_addr);
418 /* Setup MSI/MSIX vectors in the device (via cfgspace or MSIX BAR) */
419 spapr_msi_setmsg(pdev, SPAPR_PCI_MSI_WINDOW, ret_intr_type == RTAS_TYPE_MSIX,
420 irq, req_num);
422 /* Add MSI device to cache */
423 msi = g_new(spapr_pci_msi, 1);
424 msi->first_irq = irq;
425 msi->num = req_num;
426 config_addr_key = g_new(int, 1);
427 *config_addr_key = config_addr;
428 g_hash_table_insert(phb->msi, config_addr_key, msi);
430 out:
431 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
432 rtas_st(rets, 1, req_num);
433 rtas_st(rets, 2, ++seq_num);
434 if (nret > 3) {
435 rtas_st(rets, 3, ret_intr_type);
438 trace_spapr_pci_rtas_ibm_change_msi(config_addr, func, req_num, irq);
441 static void rtas_ibm_query_interrupt_source_number(PowerPCCPU *cpu,
442 sPAPRMachineState *spapr,
443 uint32_t token,
444 uint32_t nargs,
445 target_ulong args,
446 uint32_t nret,
447 target_ulong rets)
449 uint32_t config_addr = rtas_ld(args, 0);
450 uint64_t buid = rtas_ldq(args, 1);
451 unsigned int intr_src_num = -1, ioa_intr_num = rtas_ld(args, 3);
452 sPAPRPHBState *phb = NULL;
453 PCIDevice *pdev = NULL;
454 spapr_pci_msi *msi;
456 /* Find sPAPRPHBState */
457 phb = spapr_pci_find_phb(spapr, buid);
458 if (phb) {
459 pdev = spapr_pci_find_dev(spapr, buid, config_addr);
461 if (!phb || !pdev) {
462 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
463 return;
466 /* Find device descriptor and start IRQ */
467 msi = (spapr_pci_msi *) g_hash_table_lookup(phb->msi, &config_addr);
468 if (!msi || !msi->first_irq || !msi->num || (ioa_intr_num >= msi->num)) {
469 trace_spapr_pci_msi("Failed to return vector", config_addr);
470 rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
471 return;
473 intr_src_num = msi->first_irq + ioa_intr_num;
474 trace_spapr_pci_rtas_ibm_query_interrupt_source_number(ioa_intr_num,
475 intr_src_num);
477 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
478 rtas_st(rets, 1, intr_src_num);
479 rtas_st(rets, 2, 1);/* 0 == level; 1 == edge */
482 static void rtas_ibm_set_eeh_option(PowerPCCPU *cpu,
483 sPAPRMachineState *spapr,
484 uint32_t token, uint32_t nargs,
485 target_ulong args, uint32_t nret,
486 target_ulong rets)
488 sPAPRPHBState *sphb;
489 uint32_t addr, option;
490 uint64_t buid;
491 int ret;
493 if ((nargs != 4) || (nret != 1)) {
494 goto param_error_exit;
497 buid = rtas_ldq(args, 1);
498 addr = rtas_ld(args, 0);
499 option = rtas_ld(args, 3);
501 sphb = spapr_pci_find_phb(spapr, buid);
502 if (!sphb) {
503 goto param_error_exit;
506 if (!spapr_phb_eeh_available(sphb)) {
507 goto param_error_exit;
510 ret = spapr_phb_vfio_eeh_set_option(sphb, addr, option);
511 rtas_st(rets, 0, ret);
512 return;
514 param_error_exit:
515 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
518 static void rtas_ibm_get_config_addr_info2(PowerPCCPU *cpu,
519 sPAPRMachineState *spapr,
520 uint32_t token, uint32_t nargs,
521 target_ulong args, uint32_t nret,
522 target_ulong rets)
524 sPAPRPHBState *sphb;
525 PCIDevice *pdev;
526 uint32_t addr, option;
527 uint64_t buid;
529 if ((nargs != 4) || (nret != 2)) {
530 goto param_error_exit;
533 buid = rtas_ldq(args, 1);
534 sphb = spapr_pci_find_phb(spapr, buid);
535 if (!sphb) {
536 goto param_error_exit;
539 if (!spapr_phb_eeh_available(sphb)) {
540 goto param_error_exit;
544 * We always have PE address of form "00BB0001". "BB"
545 * represents the bus number of PE's primary bus.
547 option = rtas_ld(args, 3);
548 switch (option) {
549 case RTAS_GET_PE_ADDR:
550 addr = rtas_ld(args, 0);
551 pdev = spapr_pci_find_dev(spapr, buid, addr);
552 if (!pdev) {
553 goto param_error_exit;
556 rtas_st(rets, 1, (pci_bus_num(pci_get_bus(pdev)) << 16) + 1);
557 break;
558 case RTAS_GET_PE_MODE:
559 rtas_st(rets, 1, RTAS_PE_MODE_SHARED);
560 break;
561 default:
562 goto param_error_exit;
565 rtas_st(rets, 0, RTAS_OUT_SUCCESS);
566 return;
568 param_error_exit:
569 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
572 static void rtas_ibm_read_slot_reset_state2(PowerPCCPU *cpu,
573 sPAPRMachineState *spapr,
574 uint32_t token, uint32_t nargs,
575 target_ulong args, uint32_t nret,
576 target_ulong rets)
578 sPAPRPHBState *sphb;
579 uint64_t buid;
580 int state, ret;
582 if ((nargs != 3) || (nret != 4 && nret != 5)) {
583 goto param_error_exit;
586 buid = rtas_ldq(args, 1);
587 sphb = spapr_pci_find_phb(spapr, buid);
588 if (!sphb) {
589 goto param_error_exit;
592 if (!spapr_phb_eeh_available(sphb)) {
593 goto param_error_exit;
596 ret = spapr_phb_vfio_eeh_get_state(sphb, &state);
597 rtas_st(rets, 0, ret);
598 if (ret != RTAS_OUT_SUCCESS) {
599 return;
602 rtas_st(rets, 1, state);
603 rtas_st(rets, 2, RTAS_EEH_SUPPORT);
604 rtas_st(rets, 3, RTAS_EEH_PE_UNAVAIL_INFO);
605 if (nret >= 5) {
606 rtas_st(rets, 4, RTAS_EEH_PE_RECOVER_INFO);
608 return;
610 param_error_exit:
611 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
614 static void rtas_ibm_set_slot_reset(PowerPCCPU *cpu,
615 sPAPRMachineState *spapr,
616 uint32_t token, uint32_t nargs,
617 target_ulong args, uint32_t nret,
618 target_ulong rets)
620 sPAPRPHBState *sphb;
621 uint32_t option;
622 uint64_t buid;
623 int ret;
625 if ((nargs != 4) || (nret != 1)) {
626 goto param_error_exit;
629 buid = rtas_ldq(args, 1);
630 option = rtas_ld(args, 3);
631 sphb = spapr_pci_find_phb(spapr, buid);
632 if (!sphb) {
633 goto param_error_exit;
636 if (!spapr_phb_eeh_available(sphb)) {
637 goto param_error_exit;
640 ret = spapr_phb_vfio_eeh_reset(sphb, option);
641 rtas_st(rets, 0, ret);
642 return;
644 param_error_exit:
645 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
648 static void rtas_ibm_configure_pe(PowerPCCPU *cpu,
649 sPAPRMachineState *spapr,
650 uint32_t token, uint32_t nargs,
651 target_ulong args, uint32_t nret,
652 target_ulong rets)
654 sPAPRPHBState *sphb;
655 uint64_t buid;
656 int ret;
658 if ((nargs != 3) || (nret != 1)) {
659 goto param_error_exit;
662 buid = rtas_ldq(args, 1);
663 sphb = spapr_pci_find_phb(spapr, buid);
664 if (!sphb) {
665 goto param_error_exit;
668 if (!spapr_phb_eeh_available(sphb)) {
669 goto param_error_exit;
672 ret = spapr_phb_vfio_eeh_configure(sphb);
673 rtas_st(rets, 0, ret);
674 return;
676 param_error_exit:
677 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
680 /* To support it later */
681 static void rtas_ibm_slot_error_detail(PowerPCCPU *cpu,
682 sPAPRMachineState *spapr,
683 uint32_t token, uint32_t nargs,
684 target_ulong args, uint32_t nret,
685 target_ulong rets)
687 sPAPRPHBState *sphb;
688 int option;
689 uint64_t buid;
691 if ((nargs != 8) || (nret != 1)) {
692 goto param_error_exit;
695 buid = rtas_ldq(args, 1);
696 sphb = spapr_pci_find_phb(spapr, buid);
697 if (!sphb) {
698 goto param_error_exit;
701 if (!spapr_phb_eeh_available(sphb)) {
702 goto param_error_exit;
705 option = rtas_ld(args, 7);
706 switch (option) {
707 case RTAS_SLOT_TEMP_ERR_LOG:
708 case RTAS_SLOT_PERM_ERR_LOG:
709 break;
710 default:
711 goto param_error_exit;
714 /* We don't have error log yet */
715 rtas_st(rets, 0, RTAS_OUT_NO_ERRORS_FOUND);
716 return;
718 param_error_exit:
719 rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
722 static int pci_spapr_swizzle(int slot, int pin)
724 return (slot + pin) % PCI_NUM_PINS;
727 static int pci_spapr_map_irq(PCIDevice *pci_dev, int irq_num)
730 * Here we need to convert pci_dev + irq_num to some unique value
731 * which is less than number of IRQs on the specific bus (4). We
732 * use standard PCI swizzling, that is (slot number + pin number)
733 * % 4.
735 return pci_spapr_swizzle(PCI_SLOT(pci_dev->devfn), irq_num);
738 static void pci_spapr_set_irq(void *opaque, int irq_num, int level)
741 * Here we use the number returned by pci_spapr_map_irq to find a
742 * corresponding qemu_irq.
744 sPAPRPHBState *phb = opaque;
746 trace_spapr_pci_lsi_set(phb->dtbusname, irq_num, phb->lsi_table[irq_num].irq);
747 qemu_set_irq(spapr_phb_lsi_qirq(phb, irq_num), level);
750 static PCIINTxRoute spapr_route_intx_pin_to_irq(void *opaque, int pin)
752 sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(opaque);
753 PCIINTxRoute route;
755 route.mode = PCI_INTX_ENABLED;
756 route.irq = sphb->lsi_table[pin].irq;
758 return route;
762 * MSI/MSIX memory region implementation.
763 * The handler handles both MSI and MSIX.
764 * The vector number is encoded in least bits in data.
766 static void spapr_msi_write(void *opaque, hwaddr addr,
767 uint64_t data, unsigned size)
769 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
770 uint32_t irq = data;
772 trace_spapr_pci_msi_write(addr, data, irq);
774 qemu_irq_pulse(spapr_qirq(spapr, irq));
777 static const MemoryRegionOps spapr_msi_ops = {
778 /* There is no .read as the read result is undefined by PCI spec */
779 .read = NULL,
780 .write = spapr_msi_write,
781 .endianness = DEVICE_LITTLE_ENDIAN
785 * PHB PCI device
787 static AddressSpace *spapr_pci_dma_iommu(PCIBus *bus, void *opaque, int devfn)
789 sPAPRPHBState *phb = opaque;
791 return &phb->iommu_as;
794 static char *spapr_phb_vfio_get_loc_code(sPAPRPHBState *sphb, PCIDevice *pdev)
796 char *path = NULL, *buf = NULL, *host = NULL;
798 /* Get the PCI VFIO host id */
799 host = object_property_get_str(OBJECT(pdev), "host", NULL);
800 if (!host) {
801 goto err_out;
804 /* Construct the path of the file that will give us the DT location */
805 path = g_strdup_printf("/sys/bus/pci/devices/%s/devspec", host);
806 g_free(host);
807 if (!g_file_get_contents(path, &buf, NULL, NULL)) {
808 goto err_out;
810 g_free(path);
812 /* Construct and read from host device tree the loc-code */
813 path = g_strdup_printf("/proc/device-tree%s/ibm,loc-code", buf);
814 g_free(buf);
815 if (!g_file_get_contents(path, &buf, NULL, NULL)) {
816 goto err_out;
818 return buf;
820 err_out:
821 g_free(path);
822 return NULL;
825 static char *spapr_phb_get_loc_code(sPAPRPHBState *sphb, PCIDevice *pdev)
827 char *buf;
828 const char *devtype = "qemu";
829 uint32_t busnr = pci_bus_num(PCI_BUS(qdev_get_parent_bus(DEVICE(pdev))));
831 if (object_dynamic_cast(OBJECT(pdev), "vfio-pci")) {
832 buf = spapr_phb_vfio_get_loc_code(sphb, pdev);
833 if (buf) {
834 return buf;
836 devtype = "vfio";
839 * For emulated devices and VFIO-failure case, make up
840 * the loc-code.
842 buf = g_strdup_printf("%s_%s:%04x:%02x:%02x.%x",
843 devtype, pdev->name, sphb->index, busnr,
844 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
845 return buf;
848 /* Macros to operate with address in OF binding to PCI */
849 #define b_x(x, p, l) (((x) & ((1<<(l))-1)) << (p))
850 #define b_n(x) b_x((x), 31, 1) /* 0 if relocatable */
851 #define b_p(x) b_x((x), 30, 1) /* 1 if prefetchable */
852 #define b_t(x) b_x((x), 29, 1) /* 1 if the address is aliased */
853 #define b_ss(x) b_x((x), 24, 2) /* the space code */
854 #define b_bbbbbbbb(x) b_x((x), 16, 8) /* bus number */
855 #define b_ddddd(x) b_x((x), 11, 5) /* device number */
856 #define b_fff(x) b_x((x), 8, 3) /* function number */
857 #define b_rrrrrrrr(x) b_x((x), 0, 8) /* register number */
859 /* for 'reg'/'assigned-addresses' OF properties */
860 #define RESOURCE_CELLS_SIZE 2
861 #define RESOURCE_CELLS_ADDRESS 3
863 typedef struct ResourceFields {
864 uint32_t phys_hi;
865 uint32_t phys_mid;
866 uint32_t phys_lo;
867 uint32_t size_hi;
868 uint32_t size_lo;
869 } QEMU_PACKED ResourceFields;
871 typedef struct ResourceProps {
872 ResourceFields reg[8];
873 ResourceFields assigned[7];
874 uint32_t reg_len;
875 uint32_t assigned_len;
876 } ResourceProps;
878 /* fill in the 'reg'/'assigned-resources' OF properties for
879 * a PCI device. 'reg' describes resource requirements for a
880 * device's IO/MEM regions, 'assigned-addresses' describes the
881 * actual resource assignments.
883 * the properties are arrays of ('phys-addr', 'size') pairs describing
884 * the addressable regions of the PCI device, where 'phys-addr' is a
885 * RESOURCE_CELLS_ADDRESS-tuple of 32-bit integers corresponding to
886 * (phys.hi, phys.mid, phys.lo), and 'size' is a
887 * RESOURCE_CELLS_SIZE-tuple corresponding to (size.hi, size.lo).
889 * phys.hi = 0xYYXXXXZZ, where:
890 * 0xYY = npt000ss
891 * ||| |
892 * ||| +-- space code
893 * ||| |
894 * ||| + 00 if configuration space
895 * ||| + 01 if IO region,
896 * ||| + 10 if 32-bit MEM region
897 * ||| + 11 if 64-bit MEM region
898 * |||
899 * ||+------ for non-relocatable IO: 1 if aliased
900 * || for relocatable IO: 1 if below 64KB
901 * || for MEM: 1 if below 1MB
902 * |+------- 1 if region is prefetchable
903 * +-------- 1 if region is non-relocatable
904 * 0xXXXX = bbbbbbbb dddddfff, encoding bus, slot, and function
905 * bits respectively
906 * 0xZZ = rrrrrrrr, the register number of the BAR corresponding
907 * to the region
909 * phys.mid and phys.lo correspond respectively to the hi/lo portions
910 * of the actual address of the region.
912 * how the phys-addr/size values are used differ slightly between
913 * 'reg' and 'assigned-addresses' properties. namely, 'reg' has
914 * an additional description for the config space region of the
915 * device, and in the case of QEMU has n=0 and phys.mid=phys.lo=0
916 * to describe the region as relocatable, with an address-mapping
917 * that corresponds directly to the PHB's address space for the
918 * resource. 'assigned-addresses' always has n=1 set with an absolute
919 * address assigned for the resource. in general, 'assigned-addresses'
920 * won't be populated, since addresses for PCI devices are generally
921 * unmapped initially and left to the guest to assign.
923 * note also that addresses defined in these properties are, at least
924 * for PAPR guests, relative to the PHBs IO/MEM windows, and
925 * correspond directly to the addresses in the BARs.
927 * in accordance with PCI Bus Binding to Open Firmware,
928 * IEEE Std 1275-1994, section 4.1.1, as implemented by PAPR+ v2.7,
929 * Appendix C.
931 static void populate_resource_props(PCIDevice *d, ResourceProps *rp)
933 int bus_num = pci_bus_num(PCI_BUS(qdev_get_parent_bus(DEVICE(d))));
934 uint32_t dev_id = (b_bbbbbbbb(bus_num) |
935 b_ddddd(PCI_SLOT(d->devfn)) |
936 b_fff(PCI_FUNC(d->devfn)));
937 ResourceFields *reg, *assigned;
938 int i, reg_idx = 0, assigned_idx = 0;
940 /* config space region */
941 reg = &rp->reg[reg_idx++];
942 reg->phys_hi = cpu_to_be32(dev_id);
943 reg->phys_mid = 0;
944 reg->phys_lo = 0;
945 reg->size_hi = 0;
946 reg->size_lo = 0;
948 for (i = 0; i < PCI_NUM_REGIONS; i++) {
949 if (!d->io_regions[i].size) {
950 continue;
953 reg = &rp->reg[reg_idx++];
955 reg->phys_hi = cpu_to_be32(dev_id | b_rrrrrrrr(pci_bar(d, i)));
956 if (d->io_regions[i].type & PCI_BASE_ADDRESS_SPACE_IO) {
957 reg->phys_hi |= cpu_to_be32(b_ss(1));
958 } else if (d->io_regions[i].type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
959 reg->phys_hi |= cpu_to_be32(b_ss(3));
960 } else {
961 reg->phys_hi |= cpu_to_be32(b_ss(2));
963 reg->phys_mid = 0;
964 reg->phys_lo = 0;
965 reg->size_hi = cpu_to_be32(d->io_regions[i].size >> 32);
966 reg->size_lo = cpu_to_be32(d->io_regions[i].size);
968 if (d->io_regions[i].addr == PCI_BAR_UNMAPPED) {
969 continue;
972 assigned = &rp->assigned[assigned_idx++];
973 assigned->phys_hi = cpu_to_be32(be32_to_cpu(reg->phys_hi) | b_n(1));
974 assigned->phys_mid = cpu_to_be32(d->io_regions[i].addr >> 32);
975 assigned->phys_lo = cpu_to_be32(d->io_regions[i].addr);
976 assigned->size_hi = reg->size_hi;
977 assigned->size_lo = reg->size_lo;
980 rp->reg_len = reg_idx * sizeof(ResourceFields);
981 rp->assigned_len = assigned_idx * sizeof(ResourceFields);
984 typedef struct PCIClass PCIClass;
985 typedef struct PCISubClass PCISubClass;
986 typedef struct PCIIFace PCIIFace;
988 struct PCIIFace {
989 int iface;
990 const char *name;
993 struct PCISubClass {
994 int subclass;
995 const char *name;
996 const PCIIFace *iface;
999 struct PCIClass {
1000 const char *name;
1001 const PCISubClass *subc;
1004 static const PCISubClass undef_subclass[] = {
1005 { PCI_CLASS_NOT_DEFINED_VGA, "display", NULL },
1006 { 0xFF, NULL, NULL },
1009 static const PCISubClass mass_subclass[] = {
1010 { PCI_CLASS_STORAGE_SCSI, "scsi", NULL },
1011 { PCI_CLASS_STORAGE_IDE, "ide", NULL },
1012 { PCI_CLASS_STORAGE_FLOPPY, "fdc", NULL },
1013 { PCI_CLASS_STORAGE_IPI, "ipi", NULL },
1014 { PCI_CLASS_STORAGE_RAID, "raid", NULL },
1015 { PCI_CLASS_STORAGE_ATA, "ata", NULL },
1016 { PCI_CLASS_STORAGE_SATA, "sata", NULL },
1017 { PCI_CLASS_STORAGE_SAS, "sas", NULL },
1018 { 0xFF, NULL, NULL },
1021 static const PCISubClass net_subclass[] = {
1022 { PCI_CLASS_NETWORK_ETHERNET, "ethernet", NULL },
1023 { PCI_CLASS_NETWORK_TOKEN_RING, "token-ring", NULL },
1024 { PCI_CLASS_NETWORK_FDDI, "fddi", NULL },
1025 { PCI_CLASS_NETWORK_ATM, "atm", NULL },
1026 { PCI_CLASS_NETWORK_ISDN, "isdn", NULL },
1027 { PCI_CLASS_NETWORK_WORLDFIP, "worldfip", NULL },
1028 { PCI_CLASS_NETWORK_PICMG214, "picmg", NULL },
1029 { 0xFF, NULL, NULL },
1032 static const PCISubClass displ_subclass[] = {
1033 { PCI_CLASS_DISPLAY_VGA, "vga", NULL },
1034 { PCI_CLASS_DISPLAY_XGA, "xga", NULL },
1035 { PCI_CLASS_DISPLAY_3D, "3d-controller", NULL },
1036 { 0xFF, NULL, NULL },
1039 static const PCISubClass media_subclass[] = {
1040 { PCI_CLASS_MULTIMEDIA_VIDEO, "video", NULL },
1041 { PCI_CLASS_MULTIMEDIA_AUDIO, "sound", NULL },
1042 { PCI_CLASS_MULTIMEDIA_PHONE, "telephony", NULL },
1043 { 0xFF, NULL, NULL },
1046 static const PCISubClass mem_subclass[] = {
1047 { PCI_CLASS_MEMORY_RAM, "memory", NULL },
1048 { PCI_CLASS_MEMORY_FLASH, "flash", NULL },
1049 { 0xFF, NULL, NULL },
1052 static const PCISubClass bridg_subclass[] = {
1053 { PCI_CLASS_BRIDGE_HOST, "host", NULL },
1054 { PCI_CLASS_BRIDGE_ISA, "isa", NULL },
1055 { PCI_CLASS_BRIDGE_EISA, "eisa", NULL },
1056 { PCI_CLASS_BRIDGE_MC, "mca", NULL },
1057 { PCI_CLASS_BRIDGE_PCI, "pci", NULL },
1058 { PCI_CLASS_BRIDGE_PCMCIA, "pcmcia", NULL },
1059 { PCI_CLASS_BRIDGE_NUBUS, "nubus", NULL },
1060 { PCI_CLASS_BRIDGE_CARDBUS, "cardbus", NULL },
1061 { PCI_CLASS_BRIDGE_RACEWAY, "raceway", NULL },
1062 { PCI_CLASS_BRIDGE_PCI_SEMITP, "semi-transparent-pci", NULL },
1063 { PCI_CLASS_BRIDGE_IB_PCI, "infiniband", NULL },
1064 { 0xFF, NULL, NULL },
1067 static const PCISubClass comm_subclass[] = {
1068 { PCI_CLASS_COMMUNICATION_SERIAL, "serial", NULL },
1069 { PCI_CLASS_COMMUNICATION_PARALLEL, "parallel", NULL },
1070 { PCI_CLASS_COMMUNICATION_MULTISERIAL, "multiport-serial", NULL },
1071 { PCI_CLASS_COMMUNICATION_MODEM, "modem", NULL },
1072 { PCI_CLASS_COMMUNICATION_GPIB, "gpib", NULL },
1073 { PCI_CLASS_COMMUNICATION_SC, "smart-card", NULL },
1074 { 0xFF, NULL, NULL, },
1077 static const PCIIFace pic_iface[] = {
1078 { PCI_CLASS_SYSTEM_PIC_IOAPIC, "io-apic" },
1079 { PCI_CLASS_SYSTEM_PIC_IOXAPIC, "io-xapic" },
1080 { 0xFF, NULL },
1083 static const PCISubClass sys_subclass[] = {
1084 { PCI_CLASS_SYSTEM_PIC, "interrupt-controller", pic_iface },
1085 { PCI_CLASS_SYSTEM_DMA, "dma-controller", NULL },
1086 { PCI_CLASS_SYSTEM_TIMER, "timer", NULL },
1087 { PCI_CLASS_SYSTEM_RTC, "rtc", NULL },
1088 { PCI_CLASS_SYSTEM_PCI_HOTPLUG, "hot-plug-controller", NULL },
1089 { PCI_CLASS_SYSTEM_SDHCI, "sd-host-controller", NULL },
1090 { 0xFF, NULL, NULL },
1093 static const PCISubClass inp_subclass[] = {
1094 { PCI_CLASS_INPUT_KEYBOARD, "keyboard", NULL },
1095 { PCI_CLASS_INPUT_PEN, "pen", NULL },
1096 { PCI_CLASS_INPUT_MOUSE, "mouse", NULL },
1097 { PCI_CLASS_INPUT_SCANNER, "scanner", NULL },
1098 { PCI_CLASS_INPUT_GAMEPORT, "gameport", NULL },
1099 { 0xFF, NULL, NULL },
1102 static const PCISubClass dock_subclass[] = {
1103 { PCI_CLASS_DOCKING_GENERIC, "dock", NULL },
1104 { 0xFF, NULL, NULL },
1107 static const PCISubClass cpu_subclass[] = {
1108 { PCI_CLASS_PROCESSOR_PENTIUM, "pentium", NULL },
1109 { PCI_CLASS_PROCESSOR_POWERPC, "powerpc", NULL },
1110 { PCI_CLASS_PROCESSOR_MIPS, "mips", NULL },
1111 { PCI_CLASS_PROCESSOR_CO, "co-processor", NULL },
1112 { 0xFF, NULL, NULL },
1115 static const PCIIFace usb_iface[] = {
1116 { PCI_CLASS_SERIAL_USB_UHCI, "usb-uhci" },
1117 { PCI_CLASS_SERIAL_USB_OHCI, "usb-ohci", },
1118 { PCI_CLASS_SERIAL_USB_EHCI, "usb-ehci" },
1119 { PCI_CLASS_SERIAL_USB_XHCI, "usb-xhci" },
1120 { PCI_CLASS_SERIAL_USB_UNKNOWN, "usb-unknown" },
1121 { PCI_CLASS_SERIAL_USB_DEVICE, "usb-device" },
1122 { 0xFF, NULL },
1125 static const PCISubClass ser_subclass[] = {
1126 { PCI_CLASS_SERIAL_FIREWIRE, "firewire", NULL },
1127 { PCI_CLASS_SERIAL_ACCESS, "access-bus", NULL },
1128 { PCI_CLASS_SERIAL_SSA, "ssa", NULL },
1129 { PCI_CLASS_SERIAL_USB, "usb", usb_iface },
1130 { PCI_CLASS_SERIAL_FIBER, "fibre-channel", NULL },
1131 { PCI_CLASS_SERIAL_SMBUS, "smb", NULL },
1132 { PCI_CLASS_SERIAL_IB, "infiniband", NULL },
1133 { PCI_CLASS_SERIAL_IPMI, "ipmi", NULL },
1134 { PCI_CLASS_SERIAL_SERCOS, "sercos", NULL },
1135 { PCI_CLASS_SERIAL_CANBUS, "canbus", NULL },
1136 { 0xFF, NULL, NULL },
1139 static const PCISubClass wrl_subclass[] = {
1140 { PCI_CLASS_WIRELESS_IRDA, "irda", NULL },
1141 { PCI_CLASS_WIRELESS_CIR, "consumer-ir", NULL },
1142 { PCI_CLASS_WIRELESS_RF_CONTROLLER, "rf-controller", NULL },
1143 { PCI_CLASS_WIRELESS_BLUETOOTH, "bluetooth", NULL },
1144 { PCI_CLASS_WIRELESS_BROADBAND, "broadband", NULL },
1145 { 0xFF, NULL, NULL },
1148 static const PCISubClass sat_subclass[] = {
1149 { PCI_CLASS_SATELLITE_TV, "satellite-tv", NULL },
1150 { PCI_CLASS_SATELLITE_AUDIO, "satellite-audio", NULL },
1151 { PCI_CLASS_SATELLITE_VOICE, "satellite-voice", NULL },
1152 { PCI_CLASS_SATELLITE_DATA, "satellite-data", NULL },
1153 { 0xFF, NULL, NULL },
1156 static const PCISubClass crypt_subclass[] = {
1157 { PCI_CLASS_CRYPT_NETWORK, "network-encryption", NULL },
1158 { PCI_CLASS_CRYPT_ENTERTAINMENT,
1159 "entertainment-encryption", NULL },
1160 { 0xFF, NULL, NULL },
1163 static const PCISubClass spc_subclass[] = {
1164 { PCI_CLASS_SP_DPIO, "dpio", NULL },
1165 { PCI_CLASS_SP_PERF, "counter", NULL },
1166 { PCI_CLASS_SP_SYNCH, "measurement", NULL },
1167 { PCI_CLASS_SP_MANAGEMENT, "management-card", NULL },
1168 { 0xFF, NULL, NULL },
1171 static const PCIClass pci_classes[] = {
1172 { "legacy-device", undef_subclass },
1173 { "mass-storage", mass_subclass },
1174 { "network", net_subclass },
1175 { "display", displ_subclass, },
1176 { "multimedia-device", media_subclass },
1177 { "memory-controller", mem_subclass },
1178 { "unknown-bridge", bridg_subclass },
1179 { "communication-controller", comm_subclass},
1180 { "system-peripheral", sys_subclass },
1181 { "input-controller", inp_subclass },
1182 { "docking-station", dock_subclass },
1183 { "cpu", cpu_subclass },
1184 { "serial-bus", ser_subclass },
1185 { "wireless-controller", wrl_subclass },
1186 { "intelligent-io", NULL },
1187 { "satellite-device", sat_subclass },
1188 { "encryption", crypt_subclass },
1189 { "data-processing-controller", spc_subclass },
1192 static const char *pci_find_device_name(uint8_t class, uint8_t subclass,
1193 uint8_t iface)
1195 const PCIClass *pclass;
1196 const PCISubClass *psubclass;
1197 const PCIIFace *piface;
1198 const char *name;
1200 if (class >= ARRAY_SIZE(pci_classes)) {
1201 return "pci";
1204 pclass = pci_classes + class;
1205 name = pclass->name;
1207 if (pclass->subc == NULL) {
1208 return name;
1211 psubclass = pclass->subc;
1212 while ((psubclass->subclass & 0xff) != 0xff) {
1213 if ((psubclass->subclass & 0xff) == subclass) {
1214 name = psubclass->name;
1215 break;
1217 psubclass++;
1220 piface = psubclass->iface;
1221 if (piface == NULL) {
1222 return name;
1224 while ((piface->iface & 0xff) != 0xff) {
1225 if ((piface->iface & 0xff) == iface) {
1226 name = piface->name;
1227 break;
1229 piface++;
1232 return name;
1235 static gchar *pci_get_node_name(PCIDevice *dev)
1237 int slot = PCI_SLOT(dev->devfn);
1238 int func = PCI_FUNC(dev->devfn);
1239 uint32_t ccode = pci_default_read_config(dev, PCI_CLASS_PROG, 3);
1240 const char *name;
1242 name = pci_find_device_name((ccode >> 16) & 0xff, (ccode >> 8) & 0xff,
1243 ccode & 0xff);
1245 if (func != 0) {
1246 return g_strdup_printf("%s@%x,%x", name, slot, func);
1247 } else {
1248 return g_strdup_printf("%s@%x", name, slot);
1252 static uint32_t spapr_phb_get_pci_drc_index(sPAPRPHBState *phb,
1253 PCIDevice *pdev);
1255 static void spapr_populate_pci_child_dt(PCIDevice *dev, void *fdt, int offset,
1256 sPAPRPHBState *sphb)
1258 ResourceProps rp;
1259 bool is_bridge = false;
1260 int pci_status;
1261 char *buf = NULL;
1262 uint32_t drc_index = spapr_phb_get_pci_drc_index(sphb, dev);
1263 uint32_t ccode = pci_default_read_config(dev, PCI_CLASS_PROG, 3);
1264 uint32_t max_msi, max_msix;
1266 if (pci_default_read_config(dev, PCI_HEADER_TYPE, 1) ==
1267 PCI_HEADER_TYPE_BRIDGE) {
1268 is_bridge = true;
1271 /* in accordance with PAPR+ v2.7 13.6.3, Table 181 */
1272 _FDT(fdt_setprop_cell(fdt, offset, "vendor-id",
1273 pci_default_read_config(dev, PCI_VENDOR_ID, 2)));
1274 _FDT(fdt_setprop_cell(fdt, offset, "device-id",
1275 pci_default_read_config(dev, PCI_DEVICE_ID, 2)));
1276 _FDT(fdt_setprop_cell(fdt, offset, "revision-id",
1277 pci_default_read_config(dev, PCI_REVISION_ID, 1)));
1278 _FDT(fdt_setprop_cell(fdt, offset, "class-code", ccode));
1279 if (pci_default_read_config(dev, PCI_INTERRUPT_PIN, 1)) {
1280 _FDT(fdt_setprop_cell(fdt, offset, "interrupts",
1281 pci_default_read_config(dev, PCI_INTERRUPT_PIN, 1)));
1284 if (!is_bridge) {
1285 _FDT(fdt_setprop_cell(fdt, offset, "min-grant",
1286 pci_default_read_config(dev, PCI_MIN_GNT, 1)));
1287 _FDT(fdt_setprop_cell(fdt, offset, "max-latency",
1288 pci_default_read_config(dev, PCI_MAX_LAT, 1)));
1291 if (pci_default_read_config(dev, PCI_SUBSYSTEM_ID, 2)) {
1292 _FDT(fdt_setprop_cell(fdt, offset, "subsystem-id",
1293 pci_default_read_config(dev, PCI_SUBSYSTEM_ID, 2)));
1296 if (pci_default_read_config(dev, PCI_SUBSYSTEM_VENDOR_ID, 2)) {
1297 _FDT(fdt_setprop_cell(fdt, offset, "subsystem-vendor-id",
1298 pci_default_read_config(dev, PCI_SUBSYSTEM_VENDOR_ID, 2)));
1301 _FDT(fdt_setprop_cell(fdt, offset, "cache-line-size",
1302 pci_default_read_config(dev, PCI_CACHE_LINE_SIZE, 1)));
1304 /* the following fdt cells are masked off the pci status register */
1305 pci_status = pci_default_read_config(dev, PCI_STATUS, 2);
1306 _FDT(fdt_setprop_cell(fdt, offset, "devsel-speed",
1307 PCI_STATUS_DEVSEL_MASK & pci_status));
1309 if (pci_status & PCI_STATUS_FAST_BACK) {
1310 _FDT(fdt_setprop(fdt, offset, "fast-back-to-back", NULL, 0));
1312 if (pci_status & PCI_STATUS_66MHZ) {
1313 _FDT(fdt_setprop(fdt, offset, "66mhz-capable", NULL, 0));
1315 if (pci_status & PCI_STATUS_UDF) {
1316 _FDT(fdt_setprop(fdt, offset, "udf-supported", NULL, 0));
1319 _FDT(fdt_setprop_string(fdt, offset, "name",
1320 pci_find_device_name((ccode >> 16) & 0xff,
1321 (ccode >> 8) & 0xff,
1322 ccode & 0xff)));
1324 buf = spapr_phb_get_loc_code(sphb, dev);
1325 _FDT(fdt_setprop_string(fdt, offset, "ibm,loc-code", buf));
1326 g_free(buf);
1328 if (drc_index) {
1329 _FDT(fdt_setprop_cell(fdt, offset, "ibm,my-drc-index", drc_index));
1332 _FDT(fdt_setprop_cell(fdt, offset, "#address-cells",
1333 RESOURCE_CELLS_ADDRESS));
1334 _FDT(fdt_setprop_cell(fdt, offset, "#size-cells",
1335 RESOURCE_CELLS_SIZE));
1337 if (msi_present(dev)) {
1338 max_msi = msi_nr_vectors_allocated(dev);
1339 if (max_msi) {
1340 _FDT(fdt_setprop_cell(fdt, offset, "ibm,req#msi", max_msi));
1343 if (msix_present(dev)) {
1344 max_msix = dev->msix_entries_nr;
1345 if (max_msix) {
1346 _FDT(fdt_setprop_cell(fdt, offset, "ibm,req#msi-x", max_msix));
1350 populate_resource_props(dev, &rp);
1351 _FDT(fdt_setprop(fdt, offset, "reg", (uint8_t *)rp.reg, rp.reg_len));
1352 _FDT(fdt_setprop(fdt, offset, "assigned-addresses",
1353 (uint8_t *)rp.assigned, rp.assigned_len));
1355 if (sphb->pcie_ecs && pci_is_express(dev)) {
1356 _FDT(fdt_setprop_cell(fdt, offset, "ibm,pci-config-space-type", 0x1));
1360 /* create OF node for pci device and required OF DT properties */
1361 static int spapr_create_pci_child_dt(sPAPRPHBState *phb, PCIDevice *dev,
1362 void *fdt, int node_offset)
1364 int offset;
1365 gchar *nodename;
1367 nodename = pci_get_node_name(dev);
1368 _FDT(offset = fdt_add_subnode(fdt, node_offset, nodename));
1369 g_free(nodename);
1371 spapr_populate_pci_child_dt(dev, fdt, offset, phb);
1373 return offset;
1376 /* Callback to be called during DRC release. */
1377 void spapr_phb_remove_pci_device_cb(DeviceState *dev)
1379 HotplugHandler *hotplug_ctrl = qdev_get_hotplug_handler(dev);
1381 hotplug_handler_unplug(hotplug_ctrl, dev, &error_abort);
1384 static sPAPRDRConnector *spapr_phb_get_pci_func_drc(sPAPRPHBState *phb,
1385 uint32_t busnr,
1386 int32_t devfn)
1388 return spapr_drc_by_id(TYPE_SPAPR_DRC_PCI,
1389 (phb->index << 16) | (busnr << 8) | devfn);
1392 static sPAPRDRConnector *spapr_phb_get_pci_drc(sPAPRPHBState *phb,
1393 PCIDevice *pdev)
1395 uint32_t busnr = pci_bus_num(PCI_BUS(qdev_get_parent_bus(DEVICE(pdev))));
1396 return spapr_phb_get_pci_func_drc(phb, busnr, pdev->devfn);
1399 static uint32_t spapr_phb_get_pci_drc_index(sPAPRPHBState *phb,
1400 PCIDevice *pdev)
1402 sPAPRDRConnector *drc = spapr_phb_get_pci_drc(phb, pdev);
1404 if (!drc) {
1405 return 0;
1408 return spapr_drc_index(drc);
1411 int spapr_pci_dt_populate(sPAPRDRConnector *drc, sPAPRMachineState *spapr,
1412 void *fdt, int *fdt_start_offset, Error **errp)
1414 HotplugHandler *plug_handler = qdev_get_hotplug_handler(drc->dev);
1415 sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(plug_handler);
1416 PCIDevice *pdev = PCI_DEVICE(drc->dev);
1418 *fdt_start_offset = spapr_create_pci_child_dt(sphb, pdev, fdt, 0);
1419 return 0;
1422 static void spapr_pci_plug(HotplugHandler *plug_handler,
1423 DeviceState *plugged_dev, Error **errp)
1425 sPAPRPHBState *phb = SPAPR_PCI_HOST_BRIDGE(DEVICE(plug_handler));
1426 PCIDevice *pdev = PCI_DEVICE(plugged_dev);
1427 sPAPRDRConnector *drc = spapr_phb_get_pci_drc(phb, pdev);
1428 Error *local_err = NULL;
1429 PCIBus *bus = PCI_BUS(qdev_get_parent_bus(DEVICE(pdev)));
1430 uint32_t slotnr = PCI_SLOT(pdev->devfn);
1432 /* if DR is disabled we don't need to do anything in the case of
1433 * hotplug or coldplug callbacks
1435 if (!phb->dr_enabled) {
1436 /* if this is a hotplug operation initiated by the user
1437 * we need to let them know it's not enabled
1439 if (plugged_dev->hotplugged) {
1440 error_setg(&local_err, QERR_BUS_NO_HOTPLUG,
1441 object_get_typename(OBJECT(phb)));
1443 goto out;
1446 g_assert(drc);
1448 /* Following the QEMU convention used for PCIe multifunction
1449 * hotplug, we do not allow functions to be hotplugged to a
1450 * slot that already has function 0 present
1452 if (plugged_dev->hotplugged && bus->devices[PCI_DEVFN(slotnr, 0)] &&
1453 PCI_FUNC(pdev->devfn) != 0) {
1454 error_setg(&local_err, "PCI: slot %d function 0 already ocuppied by %s,"
1455 " additional functions can no longer be exposed to guest.",
1456 slotnr, bus->devices[PCI_DEVFN(slotnr, 0)]->name);
1457 goto out;
1460 spapr_drc_attach(drc, DEVICE(pdev), &local_err);
1461 if (local_err) {
1462 goto out;
1465 /* If this is function 0, signal hotplug for all the device functions.
1466 * Otherwise defer sending the hotplug event.
1468 if (!spapr_drc_hotplugged(plugged_dev)) {
1469 spapr_drc_reset(drc);
1470 } else if (PCI_FUNC(pdev->devfn) == 0) {
1471 int i;
1473 for (i = 0; i < 8; i++) {
1474 sPAPRDRConnector *func_drc;
1475 sPAPRDRConnectorClass *func_drck;
1476 sPAPRDREntitySense state;
1478 func_drc = spapr_phb_get_pci_func_drc(phb, pci_bus_num(bus),
1479 PCI_DEVFN(slotnr, i));
1480 func_drck = SPAPR_DR_CONNECTOR_GET_CLASS(func_drc);
1481 state = func_drck->dr_entity_sense(func_drc);
1483 if (state == SPAPR_DR_ENTITY_SENSE_PRESENT) {
1484 spapr_hotplug_req_add_by_index(func_drc);
1489 out:
1490 if (local_err) {
1491 error_propagate(errp, local_err);
1495 static void spapr_pci_unplug(HotplugHandler *plug_handler,
1496 DeviceState *plugged_dev, Error **errp)
1498 /* some version guests do not wait for completion of a device
1499 * cleanup (generally done asynchronously by the kernel) before
1500 * signaling to QEMU that the device is safe, but instead sleep
1501 * for some 'safe' period of time. unfortunately on a busy host
1502 * this sleep isn't guaranteed to be long enough, resulting in
1503 * bad things like IRQ lines being left asserted during final
1504 * device removal. to deal with this we call reset just prior
1505 * to finalizing the device, which will put the device back into
1506 * an 'idle' state, as the device cleanup code expects.
1508 pci_device_reset(PCI_DEVICE(plugged_dev));
1509 object_unparent(OBJECT(plugged_dev));
1512 static void spapr_pci_unplug_request(HotplugHandler *plug_handler,
1513 DeviceState *plugged_dev, Error **errp)
1515 sPAPRPHBState *phb = SPAPR_PCI_HOST_BRIDGE(DEVICE(plug_handler));
1516 PCIDevice *pdev = PCI_DEVICE(plugged_dev);
1517 sPAPRDRConnector *drc = spapr_phb_get_pci_drc(phb, pdev);
1519 if (!phb->dr_enabled) {
1520 error_setg(errp, QERR_BUS_NO_HOTPLUG,
1521 object_get_typename(OBJECT(phb)));
1522 return;
1525 g_assert(drc);
1526 g_assert(drc->dev == plugged_dev);
1528 if (!spapr_drc_unplug_requested(drc)) {
1529 PCIBus *bus = PCI_BUS(qdev_get_parent_bus(DEVICE(pdev)));
1530 uint32_t slotnr = PCI_SLOT(pdev->devfn);
1531 sPAPRDRConnector *func_drc;
1532 sPAPRDRConnectorClass *func_drck;
1533 sPAPRDREntitySense state;
1534 int i;
1536 /* ensure any other present functions are pending unplug */
1537 if (PCI_FUNC(pdev->devfn) == 0) {
1538 for (i = 1; i < 8; i++) {
1539 func_drc = spapr_phb_get_pci_func_drc(phb, pci_bus_num(bus),
1540 PCI_DEVFN(slotnr, i));
1541 func_drck = SPAPR_DR_CONNECTOR_GET_CLASS(func_drc);
1542 state = func_drck->dr_entity_sense(func_drc);
1543 if (state == SPAPR_DR_ENTITY_SENSE_PRESENT
1544 && !spapr_drc_unplug_requested(func_drc)) {
1545 error_setg(errp,
1546 "PCI: slot %d, function %d still present. "
1547 "Must unplug all non-0 functions first.",
1548 slotnr, i);
1549 return;
1554 spapr_drc_detach(drc);
1556 /* if this isn't func 0, defer unplug event. otherwise signal removal
1557 * for all present functions
1559 if (PCI_FUNC(pdev->devfn) == 0) {
1560 for (i = 7; i >= 0; i--) {
1561 func_drc = spapr_phb_get_pci_func_drc(phb, pci_bus_num(bus),
1562 PCI_DEVFN(slotnr, i));
1563 func_drck = SPAPR_DR_CONNECTOR_GET_CLASS(func_drc);
1564 state = func_drck->dr_entity_sense(func_drc);
1565 if (state == SPAPR_DR_ENTITY_SENSE_PRESENT) {
1566 spapr_hotplug_req_remove_by_index(func_drc);
1573 static void spapr_phb_realize(DeviceState *dev, Error **errp)
1575 /* We don't use SPAPR_MACHINE() in order to exit gracefully if the user
1576 * tries to add a sPAPR PHB to a non-pseries machine.
1578 sPAPRMachineState *spapr =
1579 (sPAPRMachineState *) object_dynamic_cast(qdev_get_machine(),
1580 TYPE_SPAPR_MACHINE);
1581 sPAPRMachineClass *smc = spapr ? SPAPR_MACHINE_GET_CLASS(spapr) : NULL;
1582 SysBusDevice *s = SYS_BUS_DEVICE(dev);
1583 sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(s);
1584 PCIHostState *phb = PCI_HOST_BRIDGE(s);
1585 char *namebuf;
1586 int i;
1587 PCIBus *bus;
1588 uint64_t msi_window_size = 4096;
1589 sPAPRTCETable *tcet;
1590 const unsigned windows_supported =
1591 sphb->ddw_enabled ? SPAPR_PCI_DMA_MAX_WINDOWS : 1;
1593 if (!spapr) {
1594 error_setg(errp, TYPE_SPAPR_PCI_HOST_BRIDGE " needs a pseries machine");
1595 return;
1598 if (sphb->index != (uint32_t)-1) {
1599 Error *local_err = NULL;
1601 smc->phb_placement(spapr, sphb->index,
1602 &sphb->buid, &sphb->io_win_addr,
1603 &sphb->mem_win_addr, &sphb->mem64_win_addr,
1604 windows_supported, sphb->dma_liobn, &local_err);
1605 if (local_err) {
1606 error_propagate(errp, local_err);
1607 return;
1609 } else {
1610 error_setg(errp, "\"index\" for PAPR PHB is mandatory");
1611 return;
1614 if (sphb->mem64_win_size != 0) {
1615 if (sphb->mem_win_size > SPAPR_PCI_MEM32_WIN_SIZE) {
1616 error_setg(errp, "32-bit memory window of size 0x%"HWADDR_PRIx
1617 " (max 2 GiB)", sphb->mem_win_size);
1618 return;
1621 /* 64-bit window defaults to identity mapping */
1622 sphb->mem64_win_pciaddr = sphb->mem64_win_addr;
1623 } else if (sphb->mem_win_size > SPAPR_PCI_MEM32_WIN_SIZE) {
1625 * For compatibility with old configuration, if no 64-bit MMIO
1626 * window is specified, but the ordinary (32-bit) memory
1627 * window is specified as > 2GiB, we treat it as a 2GiB 32-bit
1628 * window, with a 64-bit MMIO window following on immediately
1629 * afterwards
1631 sphb->mem64_win_size = sphb->mem_win_size - SPAPR_PCI_MEM32_WIN_SIZE;
1632 sphb->mem64_win_addr = sphb->mem_win_addr + SPAPR_PCI_MEM32_WIN_SIZE;
1633 sphb->mem64_win_pciaddr =
1634 SPAPR_PCI_MEM_WIN_BUS_OFFSET + SPAPR_PCI_MEM32_WIN_SIZE;
1635 sphb->mem_win_size = SPAPR_PCI_MEM32_WIN_SIZE;
1638 if (spapr_pci_find_phb(spapr, sphb->buid)) {
1639 error_setg(errp, "PCI host bridges must have unique BUIDs");
1640 return;
1643 if (sphb->numa_node != -1 &&
1644 (sphb->numa_node >= MAX_NODES || !numa_info[sphb->numa_node].present)) {
1645 error_setg(errp, "Invalid NUMA node ID for PCI host bridge");
1646 return;
1649 sphb->dtbusname = g_strdup_printf("pci@%" PRIx64, sphb->buid);
1651 /* Initialize memory regions */
1652 namebuf = g_strdup_printf("%s.mmio", sphb->dtbusname);
1653 memory_region_init(&sphb->memspace, OBJECT(sphb), namebuf, UINT64_MAX);
1654 g_free(namebuf);
1656 namebuf = g_strdup_printf("%s.mmio32-alias", sphb->dtbusname);
1657 memory_region_init_alias(&sphb->mem32window, OBJECT(sphb),
1658 namebuf, &sphb->memspace,
1659 SPAPR_PCI_MEM_WIN_BUS_OFFSET, sphb->mem_win_size);
1660 g_free(namebuf);
1661 memory_region_add_subregion(get_system_memory(), sphb->mem_win_addr,
1662 &sphb->mem32window);
1664 if (sphb->mem64_win_size != 0) {
1665 namebuf = g_strdup_printf("%s.mmio64-alias", sphb->dtbusname);
1666 memory_region_init_alias(&sphb->mem64window, OBJECT(sphb),
1667 namebuf, &sphb->memspace,
1668 sphb->mem64_win_pciaddr, sphb->mem64_win_size);
1669 g_free(namebuf);
1671 memory_region_add_subregion(get_system_memory(),
1672 sphb->mem64_win_addr,
1673 &sphb->mem64window);
1676 /* Initialize IO regions */
1677 namebuf = g_strdup_printf("%s.io", sphb->dtbusname);
1678 memory_region_init(&sphb->iospace, OBJECT(sphb),
1679 namebuf, SPAPR_PCI_IO_WIN_SIZE);
1680 g_free(namebuf);
1682 namebuf = g_strdup_printf("%s.io-alias", sphb->dtbusname);
1683 memory_region_init_alias(&sphb->iowindow, OBJECT(sphb), namebuf,
1684 &sphb->iospace, 0, SPAPR_PCI_IO_WIN_SIZE);
1685 g_free(namebuf);
1686 memory_region_add_subregion(get_system_memory(), sphb->io_win_addr,
1687 &sphb->iowindow);
1689 bus = pci_register_root_bus(dev, NULL,
1690 pci_spapr_set_irq, pci_spapr_map_irq, sphb,
1691 &sphb->memspace, &sphb->iospace,
1692 PCI_DEVFN(0, 0), PCI_NUM_PINS, TYPE_PCI_BUS);
1693 phb->bus = bus;
1694 qbus_set_hotplug_handler(BUS(phb->bus), OBJECT(sphb), NULL);
1697 * Initialize PHB address space.
1698 * By default there will be at least one subregion for default
1699 * 32bit DMA window.
1700 * Later the guest might want to create another DMA window
1701 * which will become another memory subregion.
1703 namebuf = g_strdup_printf("%s.iommu-root", sphb->dtbusname);
1704 memory_region_init(&sphb->iommu_root, OBJECT(sphb),
1705 namebuf, UINT64_MAX);
1706 g_free(namebuf);
1707 address_space_init(&sphb->iommu_as, &sphb->iommu_root,
1708 sphb->dtbusname);
1711 * As MSI/MSIX interrupts trigger by writing at MSI/MSIX vectors,
1712 * we need to allocate some memory to catch those writes coming
1713 * from msi_notify()/msix_notify().
1714 * As MSIMessage:addr is going to be the same and MSIMessage:data
1715 * is going to be a VIRQ number, 4 bytes of the MSI MR will only
1716 * be used.
1718 * For KVM we want to ensure that this memory is a full page so that
1719 * our memory slot is of page size granularity.
1721 #ifdef CONFIG_KVM
1722 if (kvm_enabled()) {
1723 msi_window_size = getpagesize();
1725 #endif
1727 memory_region_init_io(&sphb->msiwindow, OBJECT(sphb), &spapr_msi_ops, spapr,
1728 "msi", msi_window_size);
1729 memory_region_add_subregion(&sphb->iommu_root, SPAPR_PCI_MSI_WINDOW,
1730 &sphb->msiwindow);
1732 pci_setup_iommu(bus, spapr_pci_dma_iommu, sphb);
1734 pci_bus_set_route_irq_fn(bus, spapr_route_intx_pin_to_irq);
1736 QLIST_INSERT_HEAD(&spapr->phbs, sphb, list);
1738 /* Initialize the LSI table */
1739 for (i = 0; i < PCI_NUM_PINS; i++) {
1740 uint32_t irq = SPAPR_IRQ_PCI_LSI + sphb->index * PCI_NUM_PINS + i;
1741 Error *local_err = NULL;
1743 if (smc->legacy_irq_allocation) {
1744 irq = spapr_irq_findone(spapr, &local_err);
1745 if (local_err) {
1746 error_propagate_prepend(errp, local_err,
1747 "can't allocate LSIs: ");
1748 return;
1752 spapr_irq_claim(spapr, irq, true, &local_err);
1753 if (local_err) {
1754 error_propagate_prepend(errp, local_err, "can't allocate LSIs: ");
1755 return;
1758 sphb->lsi_table[i].irq = irq;
1761 /* allocate connectors for child PCI devices */
1762 if (sphb->dr_enabled) {
1763 for (i = 0; i < PCI_SLOT_MAX * 8; i++) {
1764 spapr_dr_connector_new(OBJECT(phb), TYPE_SPAPR_DRC_PCI,
1765 (sphb->index << 16) | i);
1769 /* DMA setup */
1770 for (i = 0; i < windows_supported; ++i) {
1771 tcet = spapr_tce_new_table(DEVICE(sphb), sphb->dma_liobn[i]);
1772 if (!tcet) {
1773 error_setg(errp, "Creating window#%d failed for %s",
1774 i, sphb->dtbusname);
1775 return;
1777 memory_region_add_subregion(&sphb->iommu_root, 0,
1778 spapr_tce_get_iommu(tcet));
1781 sphb->msi = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, g_free);
1784 static int spapr_phb_children_reset(Object *child, void *opaque)
1786 DeviceState *dev = (DeviceState *) object_dynamic_cast(child, TYPE_DEVICE);
1788 if (dev) {
1789 device_reset(dev);
1792 return 0;
1795 void spapr_phb_dma_reset(sPAPRPHBState *sphb)
1797 int i;
1798 sPAPRTCETable *tcet;
1800 for (i = 0; i < SPAPR_PCI_DMA_MAX_WINDOWS; ++i) {
1801 tcet = spapr_tce_find_by_liobn(sphb->dma_liobn[i]);
1803 if (tcet && tcet->nb_table) {
1804 spapr_tce_table_disable(tcet);
1808 /* Register default 32bit DMA window */
1809 tcet = spapr_tce_find_by_liobn(sphb->dma_liobn[0]);
1810 spapr_tce_table_enable(tcet, SPAPR_TCE_PAGE_SHIFT, sphb->dma_win_addr,
1811 sphb->dma_win_size >> SPAPR_TCE_PAGE_SHIFT);
1814 static void spapr_phb_reset(DeviceState *qdev)
1816 sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(qdev);
1818 spapr_phb_dma_reset(sphb);
1820 /* Reset the IOMMU state */
1821 object_child_foreach(OBJECT(qdev), spapr_phb_children_reset, NULL);
1823 if (spapr_phb_eeh_available(SPAPR_PCI_HOST_BRIDGE(qdev))) {
1824 spapr_phb_vfio_reset(qdev);
1828 static Property spapr_phb_properties[] = {
1829 DEFINE_PROP_UINT32("index", sPAPRPHBState, index, -1),
1830 DEFINE_PROP_UINT64("mem_win_size", sPAPRPHBState, mem_win_size,
1831 SPAPR_PCI_MEM32_WIN_SIZE),
1832 DEFINE_PROP_UINT64("mem64_win_size", sPAPRPHBState, mem64_win_size,
1833 SPAPR_PCI_MEM64_WIN_SIZE),
1834 DEFINE_PROP_UINT64("io_win_size", sPAPRPHBState, io_win_size,
1835 SPAPR_PCI_IO_WIN_SIZE),
1836 DEFINE_PROP_BOOL("dynamic-reconfiguration", sPAPRPHBState, dr_enabled,
1837 true),
1838 /* Default DMA window is 0..1GB */
1839 DEFINE_PROP_UINT64("dma_win_addr", sPAPRPHBState, dma_win_addr, 0),
1840 DEFINE_PROP_UINT64("dma_win_size", sPAPRPHBState, dma_win_size, 0x40000000),
1841 DEFINE_PROP_UINT64("dma64_win_addr", sPAPRPHBState, dma64_win_addr,
1842 0x800000000000000ULL),
1843 DEFINE_PROP_BOOL("ddw", sPAPRPHBState, ddw_enabled, true),
1844 DEFINE_PROP_UINT64("pgsz", sPAPRPHBState, page_size_mask,
1845 (1ULL << 12) | (1ULL << 16)),
1846 DEFINE_PROP_UINT32("numa_node", sPAPRPHBState, numa_node, -1),
1847 DEFINE_PROP_BOOL("pre-2.8-migration", sPAPRPHBState,
1848 pre_2_8_migration, false),
1849 DEFINE_PROP_BOOL("pcie-extended-configuration-space", sPAPRPHBState,
1850 pcie_ecs, true),
1851 DEFINE_PROP_END_OF_LIST(),
1854 static const VMStateDescription vmstate_spapr_pci_lsi = {
1855 .name = "spapr_pci/lsi",
1856 .version_id = 1,
1857 .minimum_version_id = 1,
1858 .fields = (VMStateField[]) {
1859 VMSTATE_UINT32_EQUAL(irq, struct spapr_pci_lsi, NULL),
1861 VMSTATE_END_OF_LIST()
1865 static const VMStateDescription vmstate_spapr_pci_msi = {
1866 .name = "spapr_pci/msi",
1867 .version_id = 1,
1868 .minimum_version_id = 1,
1869 .fields = (VMStateField []) {
1870 VMSTATE_UINT32(key, spapr_pci_msi_mig),
1871 VMSTATE_UINT32(value.first_irq, spapr_pci_msi_mig),
1872 VMSTATE_UINT32(value.num, spapr_pci_msi_mig),
1873 VMSTATE_END_OF_LIST()
1877 static int spapr_pci_pre_save(void *opaque)
1879 sPAPRPHBState *sphb = opaque;
1880 GHashTableIter iter;
1881 gpointer key, value;
1882 int i;
1884 if (sphb->pre_2_8_migration) {
1885 sphb->mig_liobn = sphb->dma_liobn[0];
1886 sphb->mig_mem_win_addr = sphb->mem_win_addr;
1887 sphb->mig_mem_win_size = sphb->mem_win_size;
1888 sphb->mig_io_win_addr = sphb->io_win_addr;
1889 sphb->mig_io_win_size = sphb->io_win_size;
1891 if ((sphb->mem64_win_size != 0)
1892 && (sphb->mem64_win_addr
1893 == (sphb->mem_win_addr + sphb->mem_win_size))) {
1894 sphb->mig_mem_win_size += sphb->mem64_win_size;
1898 g_free(sphb->msi_devs);
1899 sphb->msi_devs = NULL;
1900 sphb->msi_devs_num = g_hash_table_size(sphb->msi);
1901 if (!sphb->msi_devs_num) {
1902 return 0;
1904 sphb->msi_devs = g_new(spapr_pci_msi_mig, sphb->msi_devs_num);
1906 g_hash_table_iter_init(&iter, sphb->msi);
1907 for (i = 0; g_hash_table_iter_next(&iter, &key, &value); ++i) {
1908 sphb->msi_devs[i].key = *(uint32_t *) key;
1909 sphb->msi_devs[i].value = *(spapr_pci_msi *) value;
1912 return 0;
1915 static int spapr_pci_post_load(void *opaque, int version_id)
1917 sPAPRPHBState *sphb = opaque;
1918 gpointer key, value;
1919 int i;
1921 for (i = 0; i < sphb->msi_devs_num; ++i) {
1922 key = g_memdup(&sphb->msi_devs[i].key,
1923 sizeof(sphb->msi_devs[i].key));
1924 value = g_memdup(&sphb->msi_devs[i].value,
1925 sizeof(sphb->msi_devs[i].value));
1926 g_hash_table_insert(sphb->msi, key, value);
1928 g_free(sphb->msi_devs);
1929 sphb->msi_devs = NULL;
1930 sphb->msi_devs_num = 0;
1932 return 0;
1935 static bool pre_2_8_migration(void *opaque, int version_id)
1937 sPAPRPHBState *sphb = opaque;
1939 return sphb->pre_2_8_migration;
1942 static const VMStateDescription vmstate_spapr_pci = {
1943 .name = "spapr_pci",
1944 .version_id = 2,
1945 .minimum_version_id = 2,
1946 .pre_save = spapr_pci_pre_save,
1947 .post_load = spapr_pci_post_load,
1948 .fields = (VMStateField[]) {
1949 VMSTATE_UINT64_EQUAL(buid, sPAPRPHBState, NULL),
1950 VMSTATE_UINT32_TEST(mig_liobn, sPAPRPHBState, pre_2_8_migration),
1951 VMSTATE_UINT64_TEST(mig_mem_win_addr, sPAPRPHBState, pre_2_8_migration),
1952 VMSTATE_UINT64_TEST(mig_mem_win_size, sPAPRPHBState, pre_2_8_migration),
1953 VMSTATE_UINT64_TEST(mig_io_win_addr, sPAPRPHBState, pre_2_8_migration),
1954 VMSTATE_UINT64_TEST(mig_io_win_size, sPAPRPHBState, pre_2_8_migration),
1955 VMSTATE_STRUCT_ARRAY(lsi_table, sPAPRPHBState, PCI_NUM_PINS, 0,
1956 vmstate_spapr_pci_lsi, struct spapr_pci_lsi),
1957 VMSTATE_INT32(msi_devs_num, sPAPRPHBState),
1958 VMSTATE_STRUCT_VARRAY_ALLOC(msi_devs, sPAPRPHBState, msi_devs_num, 0,
1959 vmstate_spapr_pci_msi, spapr_pci_msi_mig),
1960 VMSTATE_END_OF_LIST()
1964 static const char *spapr_phb_root_bus_path(PCIHostState *host_bridge,
1965 PCIBus *rootbus)
1967 sPAPRPHBState *sphb = SPAPR_PCI_HOST_BRIDGE(host_bridge);
1969 return sphb->dtbusname;
1972 static void spapr_phb_class_init(ObjectClass *klass, void *data)
1974 PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(klass);
1975 DeviceClass *dc = DEVICE_CLASS(klass);
1976 HotplugHandlerClass *hp = HOTPLUG_HANDLER_CLASS(klass);
1978 hc->root_bus_path = spapr_phb_root_bus_path;
1979 dc->realize = spapr_phb_realize;
1980 dc->props = spapr_phb_properties;
1981 dc->reset = spapr_phb_reset;
1982 dc->vmsd = &vmstate_spapr_pci;
1983 /* Supported by TYPE_SPAPR_MACHINE */
1984 dc->user_creatable = true;
1985 set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
1986 hp->plug = spapr_pci_plug;
1987 hp->unplug = spapr_pci_unplug;
1988 hp->unplug_request = spapr_pci_unplug_request;
1991 static const TypeInfo spapr_phb_info = {
1992 .name = TYPE_SPAPR_PCI_HOST_BRIDGE,
1993 .parent = TYPE_PCI_HOST_BRIDGE,
1994 .instance_size = sizeof(sPAPRPHBState),
1995 .class_init = spapr_phb_class_init,
1996 .interfaces = (InterfaceInfo[]) {
1997 { TYPE_HOTPLUG_HANDLER },
2002 typedef struct sPAPRFDT {
2003 void *fdt;
2004 int node_off;
2005 sPAPRPHBState *sphb;
2006 } sPAPRFDT;
2008 static void spapr_populate_pci_devices_dt(PCIBus *bus, PCIDevice *pdev,
2009 void *opaque)
2011 PCIBus *sec_bus;
2012 sPAPRFDT *p = opaque;
2013 int offset;
2014 sPAPRFDT s_fdt;
2016 offset = spapr_create_pci_child_dt(p->sphb, pdev, p->fdt, p->node_off);
2017 if (!offset) {
2018 error_report("Failed to create pci child device tree node");
2019 return;
2022 if ((pci_default_read_config(pdev, PCI_HEADER_TYPE, 1) !=
2023 PCI_HEADER_TYPE_BRIDGE)) {
2024 return;
2027 sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev));
2028 if (!sec_bus) {
2029 return;
2032 s_fdt.fdt = p->fdt;
2033 s_fdt.node_off = offset;
2034 s_fdt.sphb = p->sphb;
2035 pci_for_each_device_reverse(sec_bus, pci_bus_num(sec_bus),
2036 spapr_populate_pci_devices_dt,
2037 &s_fdt);
2040 static void spapr_phb_pci_enumerate_bridge(PCIBus *bus, PCIDevice *pdev,
2041 void *opaque)
2043 unsigned int *bus_no = opaque;
2044 PCIBus *sec_bus = NULL;
2046 if ((pci_default_read_config(pdev, PCI_HEADER_TYPE, 1) !=
2047 PCI_HEADER_TYPE_BRIDGE)) {
2048 return;
2051 (*bus_no)++;
2052 pci_default_write_config(pdev, PCI_PRIMARY_BUS, pci_dev_bus_num(pdev), 1);
2053 pci_default_write_config(pdev, PCI_SECONDARY_BUS, *bus_no, 1);
2054 pci_default_write_config(pdev, PCI_SUBORDINATE_BUS, *bus_no, 1);
2056 sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev));
2057 if (!sec_bus) {
2058 return;
2061 pci_for_each_device(sec_bus, pci_bus_num(sec_bus),
2062 spapr_phb_pci_enumerate_bridge, bus_no);
2063 pci_default_write_config(pdev, PCI_SUBORDINATE_BUS, *bus_no, 1);
2066 static void spapr_phb_pci_enumerate(sPAPRPHBState *phb)
2068 PCIBus *bus = PCI_HOST_BRIDGE(phb)->bus;
2069 unsigned int bus_no = 0;
2071 pci_for_each_device(bus, pci_bus_num(bus),
2072 spapr_phb_pci_enumerate_bridge,
2073 &bus_no);
2077 int spapr_populate_pci_dt(sPAPRPHBState *phb, uint32_t intc_phandle, void *fdt,
2078 uint32_t nr_msis)
2080 int bus_off, i, j, ret;
2081 gchar *nodename;
2082 uint32_t bus_range[] = { cpu_to_be32(0), cpu_to_be32(0xff) };
2083 struct {
2084 uint32_t hi;
2085 uint64_t child;
2086 uint64_t parent;
2087 uint64_t size;
2088 } QEMU_PACKED ranges[] = {
2090 cpu_to_be32(b_ss(1)), cpu_to_be64(0),
2091 cpu_to_be64(phb->io_win_addr),
2092 cpu_to_be64(memory_region_size(&phb->iospace)),
2095 cpu_to_be32(b_ss(2)), cpu_to_be64(SPAPR_PCI_MEM_WIN_BUS_OFFSET),
2096 cpu_to_be64(phb->mem_win_addr),
2097 cpu_to_be64(phb->mem_win_size),
2100 cpu_to_be32(b_ss(3)), cpu_to_be64(phb->mem64_win_pciaddr),
2101 cpu_to_be64(phb->mem64_win_addr),
2102 cpu_to_be64(phb->mem64_win_size),
2105 const unsigned sizeof_ranges =
2106 (phb->mem64_win_size ? 3 : 2) * sizeof(ranges[0]);
2107 uint64_t bus_reg[] = { cpu_to_be64(phb->buid), 0 };
2108 uint32_t interrupt_map_mask[] = {
2109 cpu_to_be32(b_ddddd(-1)|b_fff(0)), 0x0, 0x0, cpu_to_be32(-1)};
2110 uint32_t interrupt_map[PCI_SLOT_MAX * PCI_NUM_PINS][7];
2111 uint32_t ddw_applicable[] = {
2112 cpu_to_be32(RTAS_IBM_QUERY_PE_DMA_WINDOW),
2113 cpu_to_be32(RTAS_IBM_CREATE_PE_DMA_WINDOW),
2114 cpu_to_be32(RTAS_IBM_REMOVE_PE_DMA_WINDOW)
2116 uint32_t ddw_extensions[] = {
2117 cpu_to_be32(1),
2118 cpu_to_be32(RTAS_IBM_RESET_PE_DMA_WINDOW)
2120 uint32_t associativity[] = {cpu_to_be32(0x4),
2121 cpu_to_be32(0x0),
2122 cpu_to_be32(0x0),
2123 cpu_to_be32(0x0),
2124 cpu_to_be32(phb->numa_node)};
2125 sPAPRTCETable *tcet;
2126 PCIBus *bus = PCI_HOST_BRIDGE(phb)->bus;
2127 sPAPRFDT s_fdt;
2129 /* Start populating the FDT */
2130 nodename = g_strdup_printf("pci@%" PRIx64, phb->buid);
2131 _FDT(bus_off = fdt_add_subnode(fdt, 0, nodename));
2132 g_free(nodename);
2134 /* Write PHB properties */
2135 _FDT(fdt_setprop_string(fdt, bus_off, "device_type", "pci"));
2136 _FDT(fdt_setprop_string(fdt, bus_off, "compatible", "IBM,Logical_PHB"));
2137 _FDT(fdt_setprop_cell(fdt, bus_off, "#address-cells", 0x3));
2138 _FDT(fdt_setprop_cell(fdt, bus_off, "#size-cells", 0x2));
2139 _FDT(fdt_setprop_cell(fdt, bus_off, "#interrupt-cells", 0x1));
2140 _FDT(fdt_setprop(fdt, bus_off, "used-by-rtas", NULL, 0));
2141 _FDT(fdt_setprop(fdt, bus_off, "bus-range", &bus_range, sizeof(bus_range)));
2142 _FDT(fdt_setprop(fdt, bus_off, "ranges", &ranges, sizeof_ranges));
2143 _FDT(fdt_setprop(fdt, bus_off, "reg", &bus_reg, sizeof(bus_reg)));
2144 _FDT(fdt_setprop_cell(fdt, bus_off, "ibm,pci-config-space-type", 0x1));
2145 _FDT(fdt_setprop_cell(fdt, bus_off, "ibm,pe-total-#msi", nr_msis));
2147 /* Dynamic DMA window */
2148 if (phb->ddw_enabled) {
2149 _FDT(fdt_setprop(fdt, bus_off, "ibm,ddw-applicable", &ddw_applicable,
2150 sizeof(ddw_applicable)));
2151 _FDT(fdt_setprop(fdt, bus_off, "ibm,ddw-extensions",
2152 &ddw_extensions, sizeof(ddw_extensions)));
2155 /* Advertise NUMA via ibm,associativity */
2156 if (phb->numa_node != -1) {
2157 _FDT(fdt_setprop(fdt, bus_off, "ibm,associativity", associativity,
2158 sizeof(associativity)));
2161 /* Build the interrupt-map, this must matches what is done
2162 * in pci_spapr_map_irq
2164 _FDT(fdt_setprop(fdt, bus_off, "interrupt-map-mask",
2165 &interrupt_map_mask, sizeof(interrupt_map_mask)));
2166 for (i = 0; i < PCI_SLOT_MAX; i++) {
2167 for (j = 0; j < PCI_NUM_PINS; j++) {
2168 uint32_t *irqmap = interrupt_map[i*PCI_NUM_PINS + j];
2169 int lsi_num = pci_spapr_swizzle(i, j);
2171 irqmap[0] = cpu_to_be32(b_ddddd(i)|b_fff(0));
2172 irqmap[1] = 0;
2173 irqmap[2] = 0;
2174 irqmap[3] = cpu_to_be32(j+1);
2175 irqmap[4] = cpu_to_be32(intc_phandle);
2176 spapr_dt_irq(&irqmap[5], phb->lsi_table[lsi_num].irq, true);
2179 /* Write interrupt map */
2180 _FDT(fdt_setprop(fdt, bus_off, "interrupt-map", &interrupt_map,
2181 sizeof(interrupt_map)));
2183 tcet = spapr_tce_find_by_liobn(phb->dma_liobn[0]);
2184 if (!tcet) {
2185 return -1;
2187 spapr_dma_dt(fdt, bus_off, "ibm,dma-window",
2188 tcet->liobn, tcet->bus_offset,
2189 tcet->nb_table << tcet->page_shift);
2191 /* Walk the bridges and program the bus numbers*/
2192 spapr_phb_pci_enumerate(phb);
2193 _FDT(fdt_setprop_cell(fdt, bus_off, "qemu,phb-enumerated", 0x1));
2195 /* Populate tree nodes with PCI devices attached */
2196 s_fdt.fdt = fdt;
2197 s_fdt.node_off = bus_off;
2198 s_fdt.sphb = phb;
2199 pci_for_each_device_reverse(bus, pci_bus_num(bus),
2200 spapr_populate_pci_devices_dt,
2201 &s_fdt);
2203 ret = spapr_drc_populate_dt(fdt, bus_off, OBJECT(phb),
2204 SPAPR_DR_CONNECTOR_TYPE_PCI);
2205 if (ret) {
2206 return ret;
2209 return 0;
2212 void spapr_pci_rtas_init(void)
2214 spapr_rtas_register(RTAS_READ_PCI_CONFIG, "read-pci-config",
2215 rtas_read_pci_config);
2216 spapr_rtas_register(RTAS_WRITE_PCI_CONFIG, "write-pci-config",
2217 rtas_write_pci_config);
2218 spapr_rtas_register(RTAS_IBM_READ_PCI_CONFIG, "ibm,read-pci-config",
2219 rtas_ibm_read_pci_config);
2220 spapr_rtas_register(RTAS_IBM_WRITE_PCI_CONFIG, "ibm,write-pci-config",
2221 rtas_ibm_write_pci_config);
2222 if (msi_nonbroken) {
2223 spapr_rtas_register(RTAS_IBM_QUERY_INTERRUPT_SOURCE_NUMBER,
2224 "ibm,query-interrupt-source-number",
2225 rtas_ibm_query_interrupt_source_number);
2226 spapr_rtas_register(RTAS_IBM_CHANGE_MSI, "ibm,change-msi",
2227 rtas_ibm_change_msi);
2230 spapr_rtas_register(RTAS_IBM_SET_EEH_OPTION,
2231 "ibm,set-eeh-option",
2232 rtas_ibm_set_eeh_option);
2233 spapr_rtas_register(RTAS_IBM_GET_CONFIG_ADDR_INFO2,
2234 "ibm,get-config-addr-info2",
2235 rtas_ibm_get_config_addr_info2);
2236 spapr_rtas_register(RTAS_IBM_READ_SLOT_RESET_STATE2,
2237 "ibm,read-slot-reset-state2",
2238 rtas_ibm_read_slot_reset_state2);
2239 spapr_rtas_register(RTAS_IBM_SET_SLOT_RESET,
2240 "ibm,set-slot-reset",
2241 rtas_ibm_set_slot_reset);
2242 spapr_rtas_register(RTAS_IBM_CONFIGURE_PE,
2243 "ibm,configure-pe",
2244 rtas_ibm_configure_pe);
2245 spapr_rtas_register(RTAS_IBM_SLOT_ERROR_DETAIL,
2246 "ibm,slot-error-detail",
2247 rtas_ibm_slot_error_detail);
2250 static void spapr_pci_register_types(void)
2252 type_register_static(&spapr_phb_info);
2255 type_init(spapr_pci_register_types)
2257 static int spapr_switch_one_vga(DeviceState *dev, void *opaque)
2259 bool be = *(bool *)opaque;
2261 if (object_dynamic_cast(OBJECT(dev), "VGA")
2262 || object_dynamic_cast(OBJECT(dev), "secondary-vga")) {
2263 object_property_set_bool(OBJECT(dev), be, "big-endian-framebuffer",
2264 &error_abort);
2266 return 0;
2269 void spapr_pci_switch_vga(bool big_endian)
2271 sPAPRMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
2272 sPAPRPHBState *sphb;
2275 * For backward compatibility with existing guests, we switch
2276 * the endianness of the VGA controller when changing the guest
2277 * interrupt mode
2279 QLIST_FOREACH(sphb, &spapr->phbs, list) {
2280 BusState *bus = &PCI_HOST_BRIDGE(sphb)->bus->qbus;
2281 qbus_walk_children(bus, spapr_switch_one_vga, NULL, NULL, NULL,
2282 &big_endian);