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
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "qemu-common.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"
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
)
68 QLIST_FOREACH(sphb
, &spapr
->phbs
, list
) {
69 if (sphb
->buid
!= buid
) {
78 PCIDevice
*spapr_pci_find_dev(sPAPRMachineState
*spapr
, uint64_t buid
,
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;
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
,
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
);
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
);
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
,
132 uint32_t nret
, target_ulong rets
)
137 if ((nargs
!= 4) || (nret
!= 2)) {
138 rtas_st(rets
, 0, RTAS_OUT_HW_ERROR
);
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
,
152 uint32_t nret
, target_ulong rets
)
156 if ((nargs
!= 2) || (nret
!= 2)) {
157 rtas_st(rets
, 0, RTAS_OUT_HW_ERROR
);
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
)
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
);
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
);
189 pci_host_config_write_common(pci_dev
, addr
, pci_config_size(pci_dev
),
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
,
198 uint32_t nret
, target_ulong rets
)
201 uint32_t val
, size
, addr
;
203 if ((nargs
!= 5) || (nret
!= 1)) {
204 rtas_st(rets
, 0, RTAS_OUT_HW_ERROR
);
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
,
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
);
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
)
248 MSIMessage msg
= { .address
= addr
, .data
= first_irq
};
251 msi_set_message(pdev
, msg
);
252 trace_spapr_pci_msi_setup(pdev
->name
, 0, msg
.address
);
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
);
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
,
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
;
281 int *config_addr_key
;
285 /* Fins sPAPRPHBState */
286 phb
= spapr_pci_find_phb(spapr
, buid
);
288 pdev
= spapr_pci_find_dev(spapr
, buid
, config_addr
);
291 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
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
;
302 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
306 case RTAS_CHANGE_MSI_FN
:
307 if (msi_present(pdev
)) {
308 ret_intr_type
= RTAS_TYPE_MSI
;
310 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
314 case RTAS_CHANGE_MSIX_FN
:
315 if (msix_present(pdev
)) {
316 ret_intr_type
= RTAS_TYPE_MSIX
;
318 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
323 error_report("rtas_ibm_change_msi(%u) is not implemented", func
);
324 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
328 msi
= (spapr_pci_msi
*) g_hash_table_lookup(phb
->msi
, &config_addr
);
333 trace_spapr_pci_msi("Releasing wrong config", config_addr
);
334 rtas_st(rets
, 0, RTAS_OUT_HW_ERROR
);
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
);
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
;
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 */
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
);
374 irq
= 0; /* to avoid misleading trace */
379 if (smc
->legacy_irq_allocation
) {
380 irq
= spapr_irq_find(spapr
, req_num
, ret_intr_type
== RTAS_TYPE_MSI
,
383 irq
= spapr_irq_msi_alloc(spapr
, req_num
,
384 ret_intr_type
== RTAS_TYPE_MSI
, &err
);
387 error_reportf_err(err
, "Can't allocate MSIs for device %x: ",
389 rtas_st(rets
, 0, RTAS_OUT_HW_ERROR
);
393 for (i
= 0; i
< req_num
; i
++) {
394 spapr_irq_claim(spapr
, irq
+ i
, false, &err
);
396 error_reportf_err(err
, "Can't allocate MSIs for device %x: ",
398 rtas_st(rets
, 0, RTAS_OUT_HW_ERROR
);
403 /* Release previous MSIs */
405 if (!smc
->legacy_irq_allocation
) {
406 spapr_irq_msi_free(spapr
, msi
->first_irq
, msi
->num
);
408 spapr_irq_free(spapr
, msi
->first_irq
, msi
->num
);
409 g_hash_table_remove(phb
->msi
, &config_addr
);
412 /* Setup MSI/MSIX vectors in the device (via cfgspace or MSIX BAR) */
413 spapr_msi_setmsg(pdev
, SPAPR_PCI_MSI_WINDOW
, ret_intr_type
== RTAS_TYPE_MSIX
,
416 /* Add MSI device to cache */
417 msi
= g_new(spapr_pci_msi
, 1);
418 msi
->first_irq
= irq
;
420 config_addr_key
= g_new(int, 1);
421 *config_addr_key
= config_addr
;
422 g_hash_table_insert(phb
->msi
, config_addr_key
, msi
);
425 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
426 rtas_st(rets
, 1, req_num
);
427 rtas_st(rets
, 2, ++seq_num
);
429 rtas_st(rets
, 3, ret_intr_type
);
432 trace_spapr_pci_rtas_ibm_change_msi(config_addr
, func
, req_num
, irq
);
435 static void rtas_ibm_query_interrupt_source_number(PowerPCCPU
*cpu
,
436 sPAPRMachineState
*spapr
,
443 uint32_t config_addr
= rtas_ld(args
, 0);
444 uint64_t buid
= rtas_ldq(args
, 1);
445 unsigned int intr_src_num
= -1, ioa_intr_num
= rtas_ld(args
, 3);
446 sPAPRPHBState
*phb
= NULL
;
447 PCIDevice
*pdev
= NULL
;
450 /* Find sPAPRPHBState */
451 phb
= spapr_pci_find_phb(spapr
, buid
);
453 pdev
= spapr_pci_find_dev(spapr
, buid
, config_addr
);
456 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
460 /* Find device descriptor and start IRQ */
461 msi
= (spapr_pci_msi
*) g_hash_table_lookup(phb
->msi
, &config_addr
);
462 if (!msi
|| !msi
->first_irq
|| !msi
->num
|| (ioa_intr_num
>= msi
->num
)) {
463 trace_spapr_pci_msi("Failed to return vector", config_addr
);
464 rtas_st(rets
, 0, RTAS_OUT_HW_ERROR
);
467 intr_src_num
= msi
->first_irq
+ ioa_intr_num
;
468 trace_spapr_pci_rtas_ibm_query_interrupt_source_number(ioa_intr_num
,
471 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
472 rtas_st(rets
, 1, intr_src_num
);
473 rtas_st(rets
, 2, 1);/* 0 == level; 1 == edge */
476 static void rtas_ibm_set_eeh_option(PowerPCCPU
*cpu
,
477 sPAPRMachineState
*spapr
,
478 uint32_t token
, uint32_t nargs
,
479 target_ulong args
, uint32_t nret
,
483 uint32_t addr
, option
;
487 if ((nargs
!= 4) || (nret
!= 1)) {
488 goto param_error_exit
;
491 buid
= rtas_ldq(args
, 1);
492 addr
= rtas_ld(args
, 0);
493 option
= rtas_ld(args
, 3);
495 sphb
= spapr_pci_find_phb(spapr
, buid
);
497 goto param_error_exit
;
500 if (!spapr_phb_eeh_available(sphb
)) {
501 goto param_error_exit
;
504 ret
= spapr_phb_vfio_eeh_set_option(sphb
, addr
, option
);
505 rtas_st(rets
, 0, ret
);
509 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
512 static void rtas_ibm_get_config_addr_info2(PowerPCCPU
*cpu
,
513 sPAPRMachineState
*spapr
,
514 uint32_t token
, uint32_t nargs
,
515 target_ulong args
, uint32_t nret
,
520 uint32_t addr
, option
;
523 if ((nargs
!= 4) || (nret
!= 2)) {
524 goto param_error_exit
;
527 buid
= rtas_ldq(args
, 1);
528 sphb
= spapr_pci_find_phb(spapr
, buid
);
530 goto param_error_exit
;
533 if (!spapr_phb_eeh_available(sphb
)) {
534 goto param_error_exit
;
538 * We always have PE address of form "00BB0001". "BB"
539 * represents the bus number of PE's primary bus.
541 option
= rtas_ld(args
, 3);
543 case RTAS_GET_PE_ADDR
:
544 addr
= rtas_ld(args
, 0);
545 pdev
= spapr_pci_find_dev(spapr
, buid
, addr
);
547 goto param_error_exit
;
550 rtas_st(rets
, 1, (pci_bus_num(pci_get_bus(pdev
)) << 16) + 1);
552 case RTAS_GET_PE_MODE
:
553 rtas_st(rets
, 1, RTAS_PE_MODE_SHARED
);
556 goto param_error_exit
;
559 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
563 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
566 static void rtas_ibm_read_slot_reset_state2(PowerPCCPU
*cpu
,
567 sPAPRMachineState
*spapr
,
568 uint32_t token
, uint32_t nargs
,
569 target_ulong args
, uint32_t nret
,
576 if ((nargs
!= 3) || (nret
!= 4 && nret
!= 5)) {
577 goto param_error_exit
;
580 buid
= rtas_ldq(args
, 1);
581 sphb
= spapr_pci_find_phb(spapr
, buid
);
583 goto param_error_exit
;
586 if (!spapr_phb_eeh_available(sphb
)) {
587 goto param_error_exit
;
590 ret
= spapr_phb_vfio_eeh_get_state(sphb
, &state
);
591 rtas_st(rets
, 0, ret
);
592 if (ret
!= RTAS_OUT_SUCCESS
) {
596 rtas_st(rets
, 1, state
);
597 rtas_st(rets
, 2, RTAS_EEH_SUPPORT
);
598 rtas_st(rets
, 3, RTAS_EEH_PE_UNAVAIL_INFO
);
600 rtas_st(rets
, 4, RTAS_EEH_PE_RECOVER_INFO
);
605 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
608 static void rtas_ibm_set_slot_reset(PowerPCCPU
*cpu
,
609 sPAPRMachineState
*spapr
,
610 uint32_t token
, uint32_t nargs
,
611 target_ulong args
, uint32_t nret
,
619 if ((nargs
!= 4) || (nret
!= 1)) {
620 goto param_error_exit
;
623 buid
= rtas_ldq(args
, 1);
624 option
= rtas_ld(args
, 3);
625 sphb
= spapr_pci_find_phb(spapr
, buid
);
627 goto param_error_exit
;
630 if (!spapr_phb_eeh_available(sphb
)) {
631 goto param_error_exit
;
634 ret
= spapr_phb_vfio_eeh_reset(sphb
, option
);
635 rtas_st(rets
, 0, ret
);
639 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
642 static void rtas_ibm_configure_pe(PowerPCCPU
*cpu
,
643 sPAPRMachineState
*spapr
,
644 uint32_t token
, uint32_t nargs
,
645 target_ulong args
, uint32_t nret
,
652 if ((nargs
!= 3) || (nret
!= 1)) {
653 goto param_error_exit
;
656 buid
= rtas_ldq(args
, 1);
657 sphb
= spapr_pci_find_phb(spapr
, buid
);
659 goto param_error_exit
;
662 if (!spapr_phb_eeh_available(sphb
)) {
663 goto param_error_exit
;
666 ret
= spapr_phb_vfio_eeh_configure(sphb
);
667 rtas_st(rets
, 0, ret
);
671 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
674 /* To support it later */
675 static void rtas_ibm_slot_error_detail(PowerPCCPU
*cpu
,
676 sPAPRMachineState
*spapr
,
677 uint32_t token
, uint32_t nargs
,
678 target_ulong args
, uint32_t nret
,
685 if ((nargs
!= 8) || (nret
!= 1)) {
686 goto param_error_exit
;
689 buid
= rtas_ldq(args
, 1);
690 sphb
= spapr_pci_find_phb(spapr
, buid
);
692 goto param_error_exit
;
695 if (!spapr_phb_eeh_available(sphb
)) {
696 goto param_error_exit
;
699 option
= rtas_ld(args
, 7);
701 case RTAS_SLOT_TEMP_ERR_LOG
:
702 case RTAS_SLOT_PERM_ERR_LOG
:
705 goto param_error_exit
;
708 /* We don't have error log yet */
709 rtas_st(rets
, 0, RTAS_OUT_NO_ERRORS_FOUND
);
713 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
716 static int pci_spapr_swizzle(int slot
, int pin
)
718 return (slot
+ pin
) % PCI_NUM_PINS
;
721 static int pci_spapr_map_irq(PCIDevice
*pci_dev
, int irq_num
)
724 * Here we need to convert pci_dev + irq_num to some unique value
725 * which is less than number of IRQs on the specific bus (4). We
726 * use standard PCI swizzling, that is (slot number + pin number)
729 return pci_spapr_swizzle(PCI_SLOT(pci_dev
->devfn
), irq_num
);
732 static void pci_spapr_set_irq(void *opaque
, int irq_num
, int level
)
735 * Here we use the number returned by pci_spapr_map_irq to find a
736 * corresponding qemu_irq.
738 sPAPRPHBState
*phb
= opaque
;
740 trace_spapr_pci_lsi_set(phb
->dtbusname
, irq_num
, phb
->lsi_table
[irq_num
].irq
);
741 qemu_set_irq(spapr_phb_lsi_qirq(phb
, irq_num
), level
);
744 static PCIINTxRoute
spapr_route_intx_pin_to_irq(void *opaque
, int pin
)
746 sPAPRPHBState
*sphb
= SPAPR_PCI_HOST_BRIDGE(opaque
);
749 route
.mode
= PCI_INTX_ENABLED
;
750 route
.irq
= sphb
->lsi_table
[pin
].irq
;
756 * MSI/MSIX memory region implementation.
757 * The handler handles both MSI and MSIX.
758 * The vector number is encoded in least bits in data.
760 static void spapr_msi_write(void *opaque
, hwaddr addr
,
761 uint64_t data
, unsigned size
)
763 sPAPRMachineState
*spapr
= SPAPR_MACHINE(qdev_get_machine());
766 trace_spapr_pci_msi_write(addr
, data
, irq
);
768 qemu_irq_pulse(spapr_qirq(spapr
, irq
));
771 static const MemoryRegionOps spapr_msi_ops
= {
772 /* There is no .read as the read result is undefined by PCI spec */
774 .write
= spapr_msi_write
,
775 .endianness
= DEVICE_LITTLE_ENDIAN
781 static AddressSpace
*spapr_pci_dma_iommu(PCIBus
*bus
, void *opaque
, int devfn
)
783 sPAPRPHBState
*phb
= opaque
;
785 return &phb
->iommu_as
;
788 static char *spapr_phb_vfio_get_loc_code(sPAPRPHBState
*sphb
, PCIDevice
*pdev
)
790 char *path
= NULL
, *buf
= NULL
, *host
= NULL
;
792 /* Get the PCI VFIO host id */
793 host
= object_property_get_str(OBJECT(pdev
), "host", NULL
);
798 /* Construct the path of the file that will give us the DT location */
799 path
= g_strdup_printf("/sys/bus/pci/devices/%s/devspec", host
);
801 if (!g_file_get_contents(path
, &buf
, NULL
, NULL
)) {
806 /* Construct and read from host device tree the loc-code */
807 path
= g_strdup_printf("/proc/device-tree%s/ibm,loc-code", buf
);
809 if (!g_file_get_contents(path
, &buf
, NULL
, NULL
)) {
819 static char *spapr_phb_get_loc_code(sPAPRPHBState
*sphb
, PCIDevice
*pdev
)
822 const char *devtype
= "qemu";
823 uint32_t busnr
= pci_bus_num(PCI_BUS(qdev_get_parent_bus(DEVICE(pdev
))));
825 if (object_dynamic_cast(OBJECT(pdev
), "vfio-pci")) {
826 buf
= spapr_phb_vfio_get_loc_code(sphb
, pdev
);
833 * For emulated devices and VFIO-failure case, make up
836 buf
= g_strdup_printf("%s_%s:%04x:%02x:%02x.%x",
837 devtype
, pdev
->name
, sphb
->index
, busnr
,
838 PCI_SLOT(pdev
->devfn
), PCI_FUNC(pdev
->devfn
));
842 /* Macros to operate with address in OF binding to PCI */
843 #define b_x(x, p, l) (((x) & ((1<<(l))-1)) << (p))
844 #define b_n(x) b_x((x), 31, 1) /* 0 if relocatable */
845 #define b_p(x) b_x((x), 30, 1) /* 1 if prefetchable */
846 #define b_t(x) b_x((x), 29, 1) /* 1 if the address is aliased */
847 #define b_ss(x) b_x((x), 24, 2) /* the space code */
848 #define b_bbbbbbbb(x) b_x((x), 16, 8) /* bus number */
849 #define b_ddddd(x) b_x((x), 11, 5) /* device number */
850 #define b_fff(x) b_x((x), 8, 3) /* function number */
851 #define b_rrrrrrrr(x) b_x((x), 0, 8) /* register number */
853 /* for 'reg'/'assigned-addresses' OF properties */
854 #define RESOURCE_CELLS_SIZE 2
855 #define RESOURCE_CELLS_ADDRESS 3
857 typedef struct ResourceFields
{
863 } QEMU_PACKED ResourceFields
;
865 typedef struct ResourceProps
{
866 ResourceFields reg
[8];
867 ResourceFields assigned
[7];
869 uint32_t assigned_len
;
872 /* fill in the 'reg'/'assigned-resources' OF properties for
873 * a PCI device. 'reg' describes resource requirements for a
874 * device's IO/MEM regions, 'assigned-addresses' describes the
875 * actual resource assignments.
877 * the properties are arrays of ('phys-addr', 'size') pairs describing
878 * the addressable regions of the PCI device, where 'phys-addr' is a
879 * RESOURCE_CELLS_ADDRESS-tuple of 32-bit integers corresponding to
880 * (phys.hi, phys.mid, phys.lo), and 'size' is a
881 * RESOURCE_CELLS_SIZE-tuple corresponding to (size.hi, size.lo).
883 * phys.hi = 0xYYXXXXZZ, where:
888 * ||| + 00 if configuration space
889 * ||| + 01 if IO region,
890 * ||| + 10 if 32-bit MEM region
891 * ||| + 11 if 64-bit MEM region
893 * ||+------ for non-relocatable IO: 1 if aliased
894 * || for relocatable IO: 1 if below 64KB
895 * || for MEM: 1 if below 1MB
896 * |+------- 1 if region is prefetchable
897 * +-------- 1 if region is non-relocatable
898 * 0xXXXX = bbbbbbbb dddddfff, encoding bus, slot, and function
900 * 0xZZ = rrrrrrrr, the register number of the BAR corresponding
903 * phys.mid and phys.lo correspond respectively to the hi/lo portions
904 * of the actual address of the region.
906 * how the phys-addr/size values are used differ slightly between
907 * 'reg' and 'assigned-addresses' properties. namely, 'reg' has
908 * an additional description for the config space region of the
909 * device, and in the case of QEMU has n=0 and phys.mid=phys.lo=0
910 * to describe the region as relocatable, with an address-mapping
911 * that corresponds directly to the PHB's address space for the
912 * resource. 'assigned-addresses' always has n=1 set with an absolute
913 * address assigned for the resource. in general, 'assigned-addresses'
914 * won't be populated, since addresses for PCI devices are generally
915 * unmapped initially and left to the guest to assign.
917 * note also that addresses defined in these properties are, at least
918 * for PAPR guests, relative to the PHBs IO/MEM windows, and
919 * correspond directly to the addresses in the BARs.
921 * in accordance with PCI Bus Binding to Open Firmware,
922 * IEEE Std 1275-1994, section 4.1.1, as implemented by PAPR+ v2.7,
925 static void populate_resource_props(PCIDevice
*d
, ResourceProps
*rp
)
927 int bus_num
= pci_bus_num(PCI_BUS(qdev_get_parent_bus(DEVICE(d
))));
928 uint32_t dev_id
= (b_bbbbbbbb(bus_num
) |
929 b_ddddd(PCI_SLOT(d
->devfn
)) |
930 b_fff(PCI_FUNC(d
->devfn
)));
931 ResourceFields
*reg
, *assigned
;
932 int i
, reg_idx
= 0, assigned_idx
= 0;
934 /* config space region */
935 reg
= &rp
->reg
[reg_idx
++];
936 reg
->phys_hi
= cpu_to_be32(dev_id
);
942 for (i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
943 if (!d
->io_regions
[i
].size
) {
947 reg
= &rp
->reg
[reg_idx
++];
949 reg
->phys_hi
= cpu_to_be32(dev_id
| b_rrrrrrrr(pci_bar(d
, i
)));
950 if (d
->io_regions
[i
].type
& PCI_BASE_ADDRESS_SPACE_IO
) {
951 reg
->phys_hi
|= cpu_to_be32(b_ss(1));
952 } else if (d
->io_regions
[i
].type
& PCI_BASE_ADDRESS_MEM_TYPE_64
) {
953 reg
->phys_hi
|= cpu_to_be32(b_ss(3));
955 reg
->phys_hi
|= cpu_to_be32(b_ss(2));
959 reg
->size_hi
= cpu_to_be32(d
->io_regions
[i
].size
>> 32);
960 reg
->size_lo
= cpu_to_be32(d
->io_regions
[i
].size
);
962 if (d
->io_regions
[i
].addr
== PCI_BAR_UNMAPPED
) {
966 assigned
= &rp
->assigned
[assigned_idx
++];
967 assigned
->phys_hi
= cpu_to_be32(reg
->phys_hi
| b_n(1));
968 assigned
->phys_mid
= cpu_to_be32(d
->io_regions
[i
].addr
>> 32);
969 assigned
->phys_lo
= cpu_to_be32(d
->io_regions
[i
].addr
);
970 assigned
->size_hi
= reg
->size_hi
;
971 assigned
->size_lo
= reg
->size_lo
;
974 rp
->reg_len
= reg_idx
* sizeof(ResourceFields
);
975 rp
->assigned_len
= assigned_idx
* sizeof(ResourceFields
);
978 typedef struct PCIClass PCIClass
;
979 typedef struct PCISubClass PCISubClass
;
980 typedef struct PCIIFace PCIIFace
;
990 const PCIIFace
*iface
;
995 const PCISubClass
*subc
;
998 static const PCISubClass undef_subclass
[] = {
999 { PCI_CLASS_NOT_DEFINED_VGA
, "display", NULL
},
1000 { 0xFF, NULL
, NULL
},
1003 static const PCISubClass mass_subclass
[] = {
1004 { PCI_CLASS_STORAGE_SCSI
, "scsi", NULL
},
1005 { PCI_CLASS_STORAGE_IDE
, "ide", NULL
},
1006 { PCI_CLASS_STORAGE_FLOPPY
, "fdc", NULL
},
1007 { PCI_CLASS_STORAGE_IPI
, "ipi", NULL
},
1008 { PCI_CLASS_STORAGE_RAID
, "raid", NULL
},
1009 { PCI_CLASS_STORAGE_ATA
, "ata", NULL
},
1010 { PCI_CLASS_STORAGE_SATA
, "sata", NULL
},
1011 { PCI_CLASS_STORAGE_SAS
, "sas", NULL
},
1012 { 0xFF, NULL
, NULL
},
1015 static const PCISubClass net_subclass
[] = {
1016 { PCI_CLASS_NETWORK_ETHERNET
, "ethernet", NULL
},
1017 { PCI_CLASS_NETWORK_TOKEN_RING
, "token-ring", NULL
},
1018 { PCI_CLASS_NETWORK_FDDI
, "fddi", NULL
},
1019 { PCI_CLASS_NETWORK_ATM
, "atm", NULL
},
1020 { PCI_CLASS_NETWORK_ISDN
, "isdn", NULL
},
1021 { PCI_CLASS_NETWORK_WORLDFIP
, "worldfip", NULL
},
1022 { PCI_CLASS_NETWORK_PICMG214
, "picmg", NULL
},
1023 { 0xFF, NULL
, NULL
},
1026 static const PCISubClass displ_subclass
[] = {
1027 { PCI_CLASS_DISPLAY_VGA
, "vga", NULL
},
1028 { PCI_CLASS_DISPLAY_XGA
, "xga", NULL
},
1029 { PCI_CLASS_DISPLAY_3D
, "3d-controller", NULL
},
1030 { 0xFF, NULL
, NULL
},
1033 static const PCISubClass media_subclass
[] = {
1034 { PCI_CLASS_MULTIMEDIA_VIDEO
, "video", NULL
},
1035 { PCI_CLASS_MULTIMEDIA_AUDIO
, "sound", NULL
},
1036 { PCI_CLASS_MULTIMEDIA_PHONE
, "telephony", NULL
},
1037 { 0xFF, NULL
, NULL
},
1040 static const PCISubClass mem_subclass
[] = {
1041 { PCI_CLASS_MEMORY_RAM
, "memory", NULL
},
1042 { PCI_CLASS_MEMORY_FLASH
, "flash", NULL
},
1043 { 0xFF, NULL
, NULL
},
1046 static const PCISubClass bridg_subclass
[] = {
1047 { PCI_CLASS_BRIDGE_HOST
, "host", NULL
},
1048 { PCI_CLASS_BRIDGE_ISA
, "isa", NULL
},
1049 { PCI_CLASS_BRIDGE_EISA
, "eisa", NULL
},
1050 { PCI_CLASS_BRIDGE_MC
, "mca", NULL
},
1051 { PCI_CLASS_BRIDGE_PCI
, "pci", NULL
},
1052 { PCI_CLASS_BRIDGE_PCMCIA
, "pcmcia", NULL
},
1053 { PCI_CLASS_BRIDGE_NUBUS
, "nubus", NULL
},
1054 { PCI_CLASS_BRIDGE_CARDBUS
, "cardbus", NULL
},
1055 { PCI_CLASS_BRIDGE_RACEWAY
, "raceway", NULL
},
1056 { PCI_CLASS_BRIDGE_PCI_SEMITP
, "semi-transparent-pci", NULL
},
1057 { PCI_CLASS_BRIDGE_IB_PCI
, "infiniband", NULL
},
1058 { 0xFF, NULL
, NULL
},
1061 static const PCISubClass comm_subclass
[] = {
1062 { PCI_CLASS_COMMUNICATION_SERIAL
, "serial", NULL
},
1063 { PCI_CLASS_COMMUNICATION_PARALLEL
, "parallel", NULL
},
1064 { PCI_CLASS_COMMUNICATION_MULTISERIAL
, "multiport-serial", NULL
},
1065 { PCI_CLASS_COMMUNICATION_MODEM
, "modem", NULL
},
1066 { PCI_CLASS_COMMUNICATION_GPIB
, "gpib", NULL
},
1067 { PCI_CLASS_COMMUNICATION_SC
, "smart-card", NULL
},
1068 { 0xFF, NULL
, NULL
, },
1071 static const PCIIFace pic_iface
[] = {
1072 { PCI_CLASS_SYSTEM_PIC_IOAPIC
, "io-apic" },
1073 { PCI_CLASS_SYSTEM_PIC_IOXAPIC
, "io-xapic" },
1077 static const PCISubClass sys_subclass
[] = {
1078 { PCI_CLASS_SYSTEM_PIC
, "interrupt-controller", pic_iface
},
1079 { PCI_CLASS_SYSTEM_DMA
, "dma-controller", NULL
},
1080 { PCI_CLASS_SYSTEM_TIMER
, "timer", NULL
},
1081 { PCI_CLASS_SYSTEM_RTC
, "rtc", NULL
},
1082 { PCI_CLASS_SYSTEM_PCI_HOTPLUG
, "hot-plug-controller", NULL
},
1083 { PCI_CLASS_SYSTEM_SDHCI
, "sd-host-controller", NULL
},
1084 { 0xFF, NULL
, NULL
},
1087 static const PCISubClass inp_subclass
[] = {
1088 { PCI_CLASS_INPUT_KEYBOARD
, "keyboard", NULL
},
1089 { PCI_CLASS_INPUT_PEN
, "pen", NULL
},
1090 { PCI_CLASS_INPUT_MOUSE
, "mouse", NULL
},
1091 { PCI_CLASS_INPUT_SCANNER
, "scanner", NULL
},
1092 { PCI_CLASS_INPUT_GAMEPORT
, "gameport", NULL
},
1093 { 0xFF, NULL
, NULL
},
1096 static const PCISubClass dock_subclass
[] = {
1097 { PCI_CLASS_DOCKING_GENERIC
, "dock", NULL
},
1098 { 0xFF, NULL
, NULL
},
1101 static const PCISubClass cpu_subclass
[] = {
1102 { PCI_CLASS_PROCESSOR_PENTIUM
, "pentium", NULL
},
1103 { PCI_CLASS_PROCESSOR_POWERPC
, "powerpc", NULL
},
1104 { PCI_CLASS_PROCESSOR_MIPS
, "mips", NULL
},
1105 { PCI_CLASS_PROCESSOR_CO
, "co-processor", NULL
},
1106 { 0xFF, NULL
, NULL
},
1109 static const PCIIFace usb_iface
[] = {
1110 { PCI_CLASS_SERIAL_USB_UHCI
, "usb-uhci" },
1111 { PCI_CLASS_SERIAL_USB_OHCI
, "usb-ohci", },
1112 { PCI_CLASS_SERIAL_USB_EHCI
, "usb-ehci" },
1113 { PCI_CLASS_SERIAL_USB_XHCI
, "usb-xhci" },
1114 { PCI_CLASS_SERIAL_USB_UNKNOWN
, "usb-unknown" },
1115 { PCI_CLASS_SERIAL_USB_DEVICE
, "usb-device" },
1119 static const PCISubClass ser_subclass
[] = {
1120 { PCI_CLASS_SERIAL_FIREWIRE
, "firewire", NULL
},
1121 { PCI_CLASS_SERIAL_ACCESS
, "access-bus", NULL
},
1122 { PCI_CLASS_SERIAL_SSA
, "ssa", NULL
},
1123 { PCI_CLASS_SERIAL_USB
, "usb", usb_iface
},
1124 { PCI_CLASS_SERIAL_FIBER
, "fibre-channel", NULL
},
1125 { PCI_CLASS_SERIAL_SMBUS
, "smb", NULL
},
1126 { PCI_CLASS_SERIAL_IB
, "infiniband", NULL
},
1127 { PCI_CLASS_SERIAL_IPMI
, "ipmi", NULL
},
1128 { PCI_CLASS_SERIAL_SERCOS
, "sercos", NULL
},
1129 { PCI_CLASS_SERIAL_CANBUS
, "canbus", NULL
},
1130 { 0xFF, NULL
, NULL
},
1133 static const PCISubClass wrl_subclass
[] = {
1134 { PCI_CLASS_WIRELESS_IRDA
, "irda", NULL
},
1135 { PCI_CLASS_WIRELESS_CIR
, "consumer-ir", NULL
},
1136 { PCI_CLASS_WIRELESS_RF_CONTROLLER
, "rf-controller", NULL
},
1137 { PCI_CLASS_WIRELESS_BLUETOOTH
, "bluetooth", NULL
},
1138 { PCI_CLASS_WIRELESS_BROADBAND
, "broadband", NULL
},
1139 { 0xFF, NULL
, NULL
},
1142 static const PCISubClass sat_subclass
[] = {
1143 { PCI_CLASS_SATELLITE_TV
, "satellite-tv", NULL
},
1144 { PCI_CLASS_SATELLITE_AUDIO
, "satellite-audio", NULL
},
1145 { PCI_CLASS_SATELLITE_VOICE
, "satellite-voice", NULL
},
1146 { PCI_CLASS_SATELLITE_DATA
, "satellite-data", NULL
},
1147 { 0xFF, NULL
, NULL
},
1150 static const PCISubClass crypt_subclass
[] = {
1151 { PCI_CLASS_CRYPT_NETWORK
, "network-encryption", NULL
},
1152 { PCI_CLASS_CRYPT_ENTERTAINMENT
,
1153 "entertainment-encryption", NULL
},
1154 { 0xFF, NULL
, NULL
},
1157 static const PCISubClass spc_subclass
[] = {
1158 { PCI_CLASS_SP_DPIO
, "dpio", NULL
},
1159 { PCI_CLASS_SP_PERF
, "counter", NULL
},
1160 { PCI_CLASS_SP_SYNCH
, "measurement", NULL
},
1161 { PCI_CLASS_SP_MANAGEMENT
, "management-card", NULL
},
1162 { 0xFF, NULL
, NULL
},
1165 static const PCIClass pci_classes
[] = {
1166 { "legacy-device", undef_subclass
},
1167 { "mass-storage", mass_subclass
},
1168 { "network", net_subclass
},
1169 { "display", displ_subclass
, },
1170 { "multimedia-device", media_subclass
},
1171 { "memory-controller", mem_subclass
},
1172 { "unknown-bridge", bridg_subclass
},
1173 { "communication-controller", comm_subclass
},
1174 { "system-peripheral", sys_subclass
},
1175 { "input-controller", inp_subclass
},
1176 { "docking-station", dock_subclass
},
1177 { "cpu", cpu_subclass
},
1178 { "serial-bus", ser_subclass
},
1179 { "wireless-controller", wrl_subclass
},
1180 { "intelligent-io", NULL
},
1181 { "satellite-device", sat_subclass
},
1182 { "encryption", crypt_subclass
},
1183 { "data-processing-controller", spc_subclass
},
1186 static const char *pci_find_device_name(uint8_t class, uint8_t subclass
,
1189 const PCIClass
*pclass
;
1190 const PCISubClass
*psubclass
;
1191 const PCIIFace
*piface
;
1194 if (class >= ARRAY_SIZE(pci_classes
)) {
1198 pclass
= pci_classes
+ class;
1199 name
= pclass
->name
;
1201 if (pclass
->subc
== NULL
) {
1205 psubclass
= pclass
->subc
;
1206 while ((psubclass
->subclass
& 0xff) != 0xff) {
1207 if ((psubclass
->subclass
& 0xff) == subclass
) {
1208 name
= psubclass
->name
;
1214 piface
= psubclass
->iface
;
1215 if (piface
== NULL
) {
1218 while ((piface
->iface
& 0xff) != 0xff) {
1219 if ((piface
->iface
& 0xff) == iface
) {
1220 name
= piface
->name
;
1229 static gchar
*pci_get_node_name(PCIDevice
*dev
)
1231 int slot
= PCI_SLOT(dev
->devfn
);
1232 int func
= PCI_FUNC(dev
->devfn
);
1233 uint32_t ccode
= pci_default_read_config(dev
, PCI_CLASS_PROG
, 3);
1236 name
= pci_find_device_name((ccode
>> 16) & 0xff, (ccode
>> 8) & 0xff,
1240 return g_strdup_printf("%s@%x,%x", name
, slot
, func
);
1242 return g_strdup_printf("%s@%x", name
, slot
);
1246 static uint32_t spapr_phb_get_pci_drc_index(sPAPRPHBState
*phb
,
1249 static void spapr_populate_pci_child_dt(PCIDevice
*dev
, void *fdt
, int offset
,
1250 sPAPRPHBState
*sphb
)
1253 bool is_bridge
= false;
1256 uint32_t drc_index
= spapr_phb_get_pci_drc_index(sphb
, dev
);
1257 uint32_t ccode
= pci_default_read_config(dev
, PCI_CLASS_PROG
, 3);
1258 uint32_t max_msi
, max_msix
;
1260 if (pci_default_read_config(dev
, PCI_HEADER_TYPE
, 1) ==
1261 PCI_HEADER_TYPE_BRIDGE
) {
1265 /* in accordance with PAPR+ v2.7 13.6.3, Table 181 */
1266 _FDT(fdt_setprop_cell(fdt
, offset
, "vendor-id",
1267 pci_default_read_config(dev
, PCI_VENDOR_ID
, 2)));
1268 _FDT(fdt_setprop_cell(fdt
, offset
, "device-id",
1269 pci_default_read_config(dev
, PCI_DEVICE_ID
, 2)));
1270 _FDT(fdt_setprop_cell(fdt
, offset
, "revision-id",
1271 pci_default_read_config(dev
, PCI_REVISION_ID
, 1)));
1272 _FDT(fdt_setprop_cell(fdt
, offset
, "class-code", ccode
));
1273 if (pci_default_read_config(dev
, PCI_INTERRUPT_PIN
, 1)) {
1274 _FDT(fdt_setprop_cell(fdt
, offset
, "interrupts",
1275 pci_default_read_config(dev
, PCI_INTERRUPT_PIN
, 1)));
1279 _FDT(fdt_setprop_cell(fdt
, offset
, "min-grant",
1280 pci_default_read_config(dev
, PCI_MIN_GNT
, 1)));
1281 _FDT(fdt_setprop_cell(fdt
, offset
, "max-latency",
1282 pci_default_read_config(dev
, PCI_MAX_LAT
, 1)));
1285 if (pci_default_read_config(dev
, PCI_SUBSYSTEM_ID
, 2)) {
1286 _FDT(fdt_setprop_cell(fdt
, offset
, "subsystem-id",
1287 pci_default_read_config(dev
, PCI_SUBSYSTEM_ID
, 2)));
1290 if (pci_default_read_config(dev
, PCI_SUBSYSTEM_VENDOR_ID
, 2)) {
1291 _FDT(fdt_setprop_cell(fdt
, offset
, "subsystem-vendor-id",
1292 pci_default_read_config(dev
, PCI_SUBSYSTEM_VENDOR_ID
, 2)));
1295 _FDT(fdt_setprop_cell(fdt
, offset
, "cache-line-size",
1296 pci_default_read_config(dev
, PCI_CACHE_LINE_SIZE
, 1)));
1298 /* the following fdt cells are masked off the pci status register */
1299 pci_status
= pci_default_read_config(dev
, PCI_STATUS
, 2);
1300 _FDT(fdt_setprop_cell(fdt
, offset
, "devsel-speed",
1301 PCI_STATUS_DEVSEL_MASK
& pci_status
));
1303 if (pci_status
& PCI_STATUS_FAST_BACK
) {
1304 _FDT(fdt_setprop(fdt
, offset
, "fast-back-to-back", NULL
, 0));
1306 if (pci_status
& PCI_STATUS_66MHZ
) {
1307 _FDT(fdt_setprop(fdt
, offset
, "66mhz-capable", NULL
, 0));
1309 if (pci_status
& PCI_STATUS_UDF
) {
1310 _FDT(fdt_setprop(fdt
, offset
, "udf-supported", NULL
, 0));
1313 _FDT(fdt_setprop_string(fdt
, offset
, "name",
1314 pci_find_device_name((ccode
>> 16) & 0xff,
1315 (ccode
>> 8) & 0xff,
1318 buf
= spapr_phb_get_loc_code(sphb
, dev
);
1319 _FDT(fdt_setprop_string(fdt
, offset
, "ibm,loc-code", buf
));
1323 _FDT(fdt_setprop_cell(fdt
, offset
, "ibm,my-drc-index", drc_index
));
1326 _FDT(fdt_setprop_cell(fdt
, offset
, "#address-cells",
1327 RESOURCE_CELLS_ADDRESS
));
1328 _FDT(fdt_setprop_cell(fdt
, offset
, "#size-cells",
1329 RESOURCE_CELLS_SIZE
));
1331 if (msi_present(dev
)) {
1332 max_msi
= msi_nr_vectors_allocated(dev
);
1334 _FDT(fdt_setprop_cell(fdt
, offset
, "ibm,req#msi", max_msi
));
1337 if (msix_present(dev
)) {
1338 max_msix
= dev
->msix_entries_nr
;
1340 _FDT(fdt_setprop_cell(fdt
, offset
, "ibm,req#msi-x", max_msix
));
1344 populate_resource_props(dev
, &rp
);
1345 _FDT(fdt_setprop(fdt
, offset
, "reg", (uint8_t *)rp
.reg
, rp
.reg_len
));
1346 _FDT(fdt_setprop(fdt
, offset
, "assigned-addresses",
1347 (uint8_t *)rp
.assigned
, rp
.assigned_len
));
1349 if (sphb
->pcie_ecs
&& pci_is_express(dev
)) {
1350 _FDT(fdt_setprop_cell(fdt
, offset
, "ibm,pci-config-space-type", 0x1));
1354 /* create OF node for pci device and required OF DT properties */
1355 static int spapr_create_pci_child_dt(sPAPRPHBState
*phb
, PCIDevice
*dev
,
1356 void *fdt
, int node_offset
)
1361 nodename
= pci_get_node_name(dev
);
1362 _FDT(offset
= fdt_add_subnode(fdt
, node_offset
, nodename
));
1365 spapr_populate_pci_child_dt(dev
, fdt
, offset
, phb
);
1370 /* Callback to be called during DRC release. */
1371 void spapr_phb_remove_pci_device_cb(DeviceState
*dev
)
1373 HotplugHandler
*hotplug_ctrl
= qdev_get_hotplug_handler(dev
);
1375 hotplug_handler_unplug(hotplug_ctrl
, dev
, &error_abort
);
1378 static sPAPRDRConnector
*spapr_phb_get_pci_func_drc(sPAPRPHBState
*phb
,
1382 return spapr_drc_by_id(TYPE_SPAPR_DRC_PCI
,
1383 (phb
->index
<< 16) | (busnr
<< 8) | devfn
);
1386 static sPAPRDRConnector
*spapr_phb_get_pci_drc(sPAPRPHBState
*phb
,
1389 uint32_t busnr
= pci_bus_num(PCI_BUS(qdev_get_parent_bus(DEVICE(pdev
))));
1390 return spapr_phb_get_pci_func_drc(phb
, busnr
, pdev
->devfn
);
1393 static uint32_t spapr_phb_get_pci_drc_index(sPAPRPHBState
*phb
,
1396 sPAPRDRConnector
*drc
= spapr_phb_get_pci_drc(phb
, pdev
);
1402 return spapr_drc_index(drc
);
1405 static void spapr_pci_plug(HotplugHandler
*plug_handler
,
1406 DeviceState
*plugged_dev
, Error
**errp
)
1408 sPAPRPHBState
*phb
= SPAPR_PCI_HOST_BRIDGE(DEVICE(plug_handler
));
1409 PCIDevice
*pdev
= PCI_DEVICE(plugged_dev
);
1410 sPAPRDRConnector
*drc
= spapr_phb_get_pci_drc(phb
, pdev
);
1411 Error
*local_err
= NULL
;
1412 PCIBus
*bus
= PCI_BUS(qdev_get_parent_bus(DEVICE(pdev
)));
1413 uint32_t slotnr
= PCI_SLOT(pdev
->devfn
);
1415 int fdt_start_offset
, fdt_size
;
1417 /* if DR is disabled we don't need to do anything in the case of
1418 * hotplug or coldplug callbacks
1420 if (!phb
->dr_enabled
) {
1421 /* if this is a hotplug operation initiated by the user
1422 * we need to let them know it's not enabled
1424 if (plugged_dev
->hotplugged
) {
1425 error_setg(&local_err
, QERR_BUS_NO_HOTPLUG
,
1426 object_get_typename(OBJECT(phb
)));
1433 /* Following the QEMU convention used for PCIe multifunction
1434 * hotplug, we do not allow functions to be hotplugged to a
1435 * slot that already has function 0 present
1437 if (plugged_dev
->hotplugged
&& bus
->devices
[PCI_DEVFN(slotnr
, 0)] &&
1438 PCI_FUNC(pdev
->devfn
) != 0) {
1439 error_setg(&local_err
, "PCI: slot %d function 0 already ocuppied by %s,"
1440 " additional functions can no longer be exposed to guest.",
1441 slotnr
, bus
->devices
[PCI_DEVFN(slotnr
, 0)]->name
);
1445 fdt
= create_device_tree(&fdt_size
);
1446 fdt_start_offset
= spapr_create_pci_child_dt(phb
, pdev
, fdt
, 0);
1448 spapr_drc_attach(drc
, DEVICE(pdev
), fdt
, fdt_start_offset
, &local_err
);
1453 /* If this is function 0, signal hotplug for all the device functions.
1454 * Otherwise defer sending the hotplug event.
1456 if (!spapr_drc_hotplugged(plugged_dev
)) {
1457 spapr_drc_reset(drc
);
1458 } else if (PCI_FUNC(pdev
->devfn
) == 0) {
1461 for (i
= 0; i
< 8; i
++) {
1462 sPAPRDRConnector
*func_drc
;
1463 sPAPRDRConnectorClass
*func_drck
;
1464 sPAPRDREntitySense state
;
1466 func_drc
= spapr_phb_get_pci_func_drc(phb
, pci_bus_num(bus
),
1467 PCI_DEVFN(slotnr
, i
));
1468 func_drck
= SPAPR_DR_CONNECTOR_GET_CLASS(func_drc
);
1469 state
= func_drck
->dr_entity_sense(func_drc
);
1471 if (state
== SPAPR_DR_ENTITY_SENSE_PRESENT
) {
1472 spapr_hotplug_req_add_by_index(func_drc
);
1479 error_propagate(errp
, local_err
);
1484 static void spapr_pci_unplug(HotplugHandler
*plug_handler
,
1485 DeviceState
*plugged_dev
, Error
**errp
)
1487 /* some version guests do not wait for completion of a device
1488 * cleanup (generally done asynchronously by the kernel) before
1489 * signaling to QEMU that the device is safe, but instead sleep
1490 * for some 'safe' period of time. unfortunately on a busy host
1491 * this sleep isn't guaranteed to be long enough, resulting in
1492 * bad things like IRQ lines being left asserted during final
1493 * device removal. to deal with this we call reset just prior
1494 * to finalizing the device, which will put the device back into
1495 * an 'idle' state, as the device cleanup code expects.
1497 pci_device_reset(PCI_DEVICE(plugged_dev
));
1498 object_unparent(OBJECT(plugged_dev
));
1501 static void spapr_pci_unplug_request(HotplugHandler
*plug_handler
,
1502 DeviceState
*plugged_dev
, Error
**errp
)
1504 sPAPRPHBState
*phb
= SPAPR_PCI_HOST_BRIDGE(DEVICE(plug_handler
));
1505 PCIDevice
*pdev
= PCI_DEVICE(plugged_dev
);
1506 sPAPRDRConnector
*drc
= spapr_phb_get_pci_drc(phb
, pdev
);
1508 if (!phb
->dr_enabled
) {
1509 error_setg(errp
, QERR_BUS_NO_HOTPLUG
,
1510 object_get_typename(OBJECT(phb
)));
1515 g_assert(drc
->dev
== plugged_dev
);
1517 if (!spapr_drc_unplug_requested(drc
)) {
1518 PCIBus
*bus
= PCI_BUS(qdev_get_parent_bus(DEVICE(pdev
)));
1519 uint32_t slotnr
= PCI_SLOT(pdev
->devfn
);
1520 sPAPRDRConnector
*func_drc
;
1521 sPAPRDRConnectorClass
*func_drck
;
1522 sPAPRDREntitySense state
;
1525 /* ensure any other present functions are pending unplug */
1526 if (PCI_FUNC(pdev
->devfn
) == 0) {
1527 for (i
= 1; i
< 8; i
++) {
1528 func_drc
= spapr_phb_get_pci_func_drc(phb
, pci_bus_num(bus
),
1529 PCI_DEVFN(slotnr
, i
));
1530 func_drck
= SPAPR_DR_CONNECTOR_GET_CLASS(func_drc
);
1531 state
= func_drck
->dr_entity_sense(func_drc
);
1532 if (state
== SPAPR_DR_ENTITY_SENSE_PRESENT
1533 && !spapr_drc_unplug_requested(func_drc
)) {
1535 "PCI: slot %d, function %d still present. "
1536 "Must unplug all non-0 functions first.",
1543 spapr_drc_detach(drc
);
1545 /* if this isn't func 0, defer unplug event. otherwise signal removal
1546 * for all present functions
1548 if (PCI_FUNC(pdev
->devfn
) == 0) {
1549 for (i
= 7; i
>= 0; i
--) {
1550 func_drc
= spapr_phb_get_pci_func_drc(phb
, pci_bus_num(bus
),
1551 PCI_DEVFN(slotnr
, i
));
1552 func_drck
= SPAPR_DR_CONNECTOR_GET_CLASS(func_drc
);
1553 state
= func_drck
->dr_entity_sense(func_drc
);
1554 if (state
== SPAPR_DR_ENTITY_SENSE_PRESENT
) {
1555 spapr_hotplug_req_remove_by_index(func_drc
);
1562 static void spapr_phb_realize(DeviceState
*dev
, Error
**errp
)
1564 /* We don't use SPAPR_MACHINE() in order to exit gracefully if the user
1565 * tries to add a sPAPR PHB to a non-pseries machine.
1567 sPAPRMachineState
*spapr
=
1568 (sPAPRMachineState
*) object_dynamic_cast(qdev_get_machine(),
1569 TYPE_SPAPR_MACHINE
);
1570 sPAPRMachineClass
*smc
= spapr
? SPAPR_MACHINE_GET_CLASS(spapr
) : NULL
;
1571 SysBusDevice
*s
= SYS_BUS_DEVICE(dev
);
1572 sPAPRPHBState
*sphb
= SPAPR_PCI_HOST_BRIDGE(s
);
1573 PCIHostState
*phb
= PCI_HOST_BRIDGE(s
);
1577 uint64_t msi_window_size
= 4096;
1578 sPAPRTCETable
*tcet
;
1579 const unsigned windows_supported
=
1580 sphb
->ddw_enabled
? SPAPR_PCI_DMA_MAX_WINDOWS
: 1;
1583 error_setg(errp
, TYPE_SPAPR_PCI_HOST_BRIDGE
" needs a pseries machine");
1587 if (sphb
->index
!= (uint32_t)-1) {
1588 Error
*local_err
= NULL
;
1590 smc
->phb_placement(spapr
, sphb
->index
,
1591 &sphb
->buid
, &sphb
->io_win_addr
,
1592 &sphb
->mem_win_addr
, &sphb
->mem64_win_addr
,
1593 windows_supported
, sphb
->dma_liobn
, &local_err
);
1595 error_propagate(errp
, local_err
);
1599 error_setg(errp
, "\"index\" for PAPR PHB is mandatory");
1603 if (sphb
->mem64_win_size
!= 0) {
1604 if (sphb
->mem_win_size
> SPAPR_PCI_MEM32_WIN_SIZE
) {
1605 error_setg(errp
, "32-bit memory window of size 0x%"HWADDR_PRIx
1606 " (max 2 GiB)", sphb
->mem_win_size
);
1610 /* 64-bit window defaults to identity mapping */
1611 sphb
->mem64_win_pciaddr
= sphb
->mem64_win_addr
;
1612 } else if (sphb
->mem_win_size
> SPAPR_PCI_MEM32_WIN_SIZE
) {
1614 * For compatibility with old configuration, if no 64-bit MMIO
1615 * window is specified, but the ordinary (32-bit) memory
1616 * window is specified as > 2GiB, we treat it as a 2GiB 32-bit
1617 * window, with a 64-bit MMIO window following on immediately
1620 sphb
->mem64_win_size
= sphb
->mem_win_size
- SPAPR_PCI_MEM32_WIN_SIZE
;
1621 sphb
->mem64_win_addr
= sphb
->mem_win_addr
+ SPAPR_PCI_MEM32_WIN_SIZE
;
1622 sphb
->mem64_win_pciaddr
=
1623 SPAPR_PCI_MEM_WIN_BUS_OFFSET
+ SPAPR_PCI_MEM32_WIN_SIZE
;
1624 sphb
->mem_win_size
= SPAPR_PCI_MEM32_WIN_SIZE
;
1627 if (spapr_pci_find_phb(spapr
, sphb
->buid
)) {
1628 error_setg(errp
, "PCI host bridges must have unique BUIDs");
1632 if (sphb
->numa_node
!= -1 &&
1633 (sphb
->numa_node
>= MAX_NODES
|| !numa_info
[sphb
->numa_node
].present
)) {
1634 error_setg(errp
, "Invalid NUMA node ID for PCI host bridge");
1638 sphb
->dtbusname
= g_strdup_printf("pci@%" PRIx64
, sphb
->buid
);
1640 /* Initialize memory regions */
1641 namebuf
= g_strdup_printf("%s.mmio", sphb
->dtbusname
);
1642 memory_region_init(&sphb
->memspace
, OBJECT(sphb
), namebuf
, UINT64_MAX
);
1645 namebuf
= g_strdup_printf("%s.mmio32-alias", sphb
->dtbusname
);
1646 memory_region_init_alias(&sphb
->mem32window
, OBJECT(sphb
),
1647 namebuf
, &sphb
->memspace
,
1648 SPAPR_PCI_MEM_WIN_BUS_OFFSET
, sphb
->mem_win_size
);
1650 memory_region_add_subregion(get_system_memory(), sphb
->mem_win_addr
,
1651 &sphb
->mem32window
);
1653 if (sphb
->mem64_win_size
!= 0) {
1654 namebuf
= g_strdup_printf("%s.mmio64-alias", sphb
->dtbusname
);
1655 memory_region_init_alias(&sphb
->mem64window
, OBJECT(sphb
),
1656 namebuf
, &sphb
->memspace
,
1657 sphb
->mem64_win_pciaddr
, sphb
->mem64_win_size
);
1660 memory_region_add_subregion(get_system_memory(),
1661 sphb
->mem64_win_addr
,
1662 &sphb
->mem64window
);
1665 /* Initialize IO regions */
1666 namebuf
= g_strdup_printf("%s.io", sphb
->dtbusname
);
1667 memory_region_init(&sphb
->iospace
, OBJECT(sphb
),
1668 namebuf
, SPAPR_PCI_IO_WIN_SIZE
);
1671 namebuf
= g_strdup_printf("%s.io-alias", sphb
->dtbusname
);
1672 memory_region_init_alias(&sphb
->iowindow
, OBJECT(sphb
), namebuf
,
1673 &sphb
->iospace
, 0, SPAPR_PCI_IO_WIN_SIZE
);
1675 memory_region_add_subregion(get_system_memory(), sphb
->io_win_addr
,
1678 bus
= pci_register_root_bus(dev
, NULL
,
1679 pci_spapr_set_irq
, pci_spapr_map_irq
, sphb
,
1680 &sphb
->memspace
, &sphb
->iospace
,
1681 PCI_DEVFN(0, 0), PCI_NUM_PINS
, TYPE_PCI_BUS
);
1683 qbus_set_hotplug_handler(BUS(phb
->bus
), DEVICE(sphb
), NULL
);
1686 * Initialize PHB address space.
1687 * By default there will be at least one subregion for default
1689 * Later the guest might want to create another DMA window
1690 * which will become another memory subregion.
1692 namebuf
= g_strdup_printf("%s.iommu-root", sphb
->dtbusname
);
1693 memory_region_init(&sphb
->iommu_root
, OBJECT(sphb
),
1694 namebuf
, UINT64_MAX
);
1696 address_space_init(&sphb
->iommu_as
, &sphb
->iommu_root
,
1700 * As MSI/MSIX interrupts trigger by writing at MSI/MSIX vectors,
1701 * we need to allocate some memory to catch those writes coming
1702 * from msi_notify()/msix_notify().
1703 * As MSIMessage:addr is going to be the same and MSIMessage:data
1704 * is going to be a VIRQ number, 4 bytes of the MSI MR will only
1707 * For KVM we want to ensure that this memory is a full page so that
1708 * our memory slot is of page size granularity.
1711 if (kvm_enabled()) {
1712 msi_window_size
= getpagesize();
1716 memory_region_init_io(&sphb
->msiwindow
, OBJECT(sphb
), &spapr_msi_ops
, spapr
,
1717 "msi", msi_window_size
);
1718 memory_region_add_subregion(&sphb
->iommu_root
, SPAPR_PCI_MSI_WINDOW
,
1721 pci_setup_iommu(bus
, spapr_pci_dma_iommu
, sphb
);
1723 pci_bus_set_route_irq_fn(bus
, spapr_route_intx_pin_to_irq
);
1725 QLIST_INSERT_HEAD(&spapr
->phbs
, sphb
, list
);
1727 /* Initialize the LSI table */
1728 for (i
= 0; i
< PCI_NUM_PINS
; i
++) {
1729 uint32_t irq
= SPAPR_IRQ_PCI_LSI
+ sphb
->index
* PCI_NUM_PINS
+ i
;
1730 Error
*local_err
= NULL
;
1732 if (smc
->legacy_irq_allocation
) {
1733 irq
= spapr_irq_findone(spapr
, &local_err
);
1735 error_propagate_prepend(errp
, local_err
,
1736 "can't allocate LSIs: ");
1741 spapr_irq_claim(spapr
, irq
, true, &local_err
);
1743 error_propagate_prepend(errp
, local_err
, "can't allocate LSIs: ");
1747 sphb
->lsi_table
[i
].irq
= irq
;
1750 /* allocate connectors for child PCI devices */
1751 if (sphb
->dr_enabled
) {
1752 for (i
= 0; i
< PCI_SLOT_MAX
* 8; i
++) {
1753 spapr_dr_connector_new(OBJECT(phb
), TYPE_SPAPR_DRC_PCI
,
1754 (sphb
->index
<< 16) | i
);
1759 for (i
= 0; i
< windows_supported
; ++i
) {
1760 tcet
= spapr_tce_new_table(DEVICE(sphb
), sphb
->dma_liobn
[i
]);
1762 error_setg(errp
, "Creating window#%d failed for %s",
1763 i
, sphb
->dtbusname
);
1766 memory_region_add_subregion(&sphb
->iommu_root
, 0,
1767 spapr_tce_get_iommu(tcet
));
1770 sphb
->msi
= g_hash_table_new_full(g_int_hash
, g_int_equal
, g_free
, g_free
);
1773 static int spapr_phb_children_reset(Object
*child
, void *opaque
)
1775 DeviceState
*dev
= (DeviceState
*) object_dynamic_cast(child
, TYPE_DEVICE
);
1784 void spapr_phb_dma_reset(sPAPRPHBState
*sphb
)
1787 sPAPRTCETable
*tcet
;
1789 for (i
= 0; i
< SPAPR_PCI_DMA_MAX_WINDOWS
; ++i
) {
1790 tcet
= spapr_tce_find_by_liobn(sphb
->dma_liobn
[i
]);
1792 if (tcet
&& tcet
->nb_table
) {
1793 spapr_tce_table_disable(tcet
);
1797 /* Register default 32bit DMA window */
1798 tcet
= spapr_tce_find_by_liobn(sphb
->dma_liobn
[0]);
1799 spapr_tce_table_enable(tcet
, SPAPR_TCE_PAGE_SHIFT
, sphb
->dma_win_addr
,
1800 sphb
->dma_win_size
>> SPAPR_TCE_PAGE_SHIFT
);
1803 static void spapr_phb_reset(DeviceState
*qdev
)
1805 sPAPRPHBState
*sphb
= SPAPR_PCI_HOST_BRIDGE(qdev
);
1807 spapr_phb_dma_reset(sphb
);
1809 /* Reset the IOMMU state */
1810 object_child_foreach(OBJECT(qdev
), spapr_phb_children_reset
, NULL
);
1812 if (spapr_phb_eeh_available(SPAPR_PCI_HOST_BRIDGE(qdev
))) {
1813 spapr_phb_vfio_reset(qdev
);
1817 static Property spapr_phb_properties
[] = {
1818 DEFINE_PROP_UINT32("index", sPAPRPHBState
, index
, -1),
1819 DEFINE_PROP_UINT64("mem_win_size", sPAPRPHBState
, mem_win_size
,
1820 SPAPR_PCI_MEM32_WIN_SIZE
),
1821 DEFINE_PROP_UINT64("mem64_win_size", sPAPRPHBState
, mem64_win_size
,
1822 SPAPR_PCI_MEM64_WIN_SIZE
),
1823 DEFINE_PROP_UINT64("io_win_size", sPAPRPHBState
, io_win_size
,
1824 SPAPR_PCI_IO_WIN_SIZE
),
1825 DEFINE_PROP_BOOL("dynamic-reconfiguration", sPAPRPHBState
, dr_enabled
,
1827 /* Default DMA window is 0..1GB */
1828 DEFINE_PROP_UINT64("dma_win_addr", sPAPRPHBState
, dma_win_addr
, 0),
1829 DEFINE_PROP_UINT64("dma_win_size", sPAPRPHBState
, dma_win_size
, 0x40000000),
1830 DEFINE_PROP_UINT64("dma64_win_addr", sPAPRPHBState
, dma64_win_addr
,
1831 0x800000000000000ULL
),
1832 DEFINE_PROP_BOOL("ddw", sPAPRPHBState
, ddw_enabled
, true),
1833 DEFINE_PROP_UINT64("pgsz", sPAPRPHBState
, page_size_mask
,
1834 (1ULL << 12) | (1ULL << 16)),
1835 DEFINE_PROP_UINT32("numa_node", sPAPRPHBState
, numa_node
, -1),
1836 DEFINE_PROP_BOOL("pre-2.8-migration", sPAPRPHBState
,
1837 pre_2_8_migration
, false),
1838 DEFINE_PROP_BOOL("pcie-extended-configuration-space", sPAPRPHBState
,
1840 DEFINE_PROP_END_OF_LIST(),
1843 static const VMStateDescription vmstate_spapr_pci_lsi
= {
1844 .name
= "spapr_pci/lsi",
1846 .minimum_version_id
= 1,
1847 .fields
= (VMStateField
[]) {
1848 VMSTATE_UINT32_EQUAL(irq
, struct spapr_pci_lsi
, NULL
),
1850 VMSTATE_END_OF_LIST()
1854 static const VMStateDescription vmstate_spapr_pci_msi
= {
1855 .name
= "spapr_pci/msi",
1857 .minimum_version_id
= 1,
1858 .fields
= (VMStateField
[]) {
1859 VMSTATE_UINT32(key
, spapr_pci_msi_mig
),
1860 VMSTATE_UINT32(value
.first_irq
, spapr_pci_msi_mig
),
1861 VMSTATE_UINT32(value
.num
, spapr_pci_msi_mig
),
1862 VMSTATE_END_OF_LIST()
1866 static int spapr_pci_pre_save(void *opaque
)
1868 sPAPRPHBState
*sphb
= opaque
;
1869 GHashTableIter iter
;
1870 gpointer key
, value
;
1873 if (sphb
->pre_2_8_migration
) {
1874 sphb
->mig_liobn
= sphb
->dma_liobn
[0];
1875 sphb
->mig_mem_win_addr
= sphb
->mem_win_addr
;
1876 sphb
->mig_mem_win_size
= sphb
->mem_win_size
;
1877 sphb
->mig_io_win_addr
= sphb
->io_win_addr
;
1878 sphb
->mig_io_win_size
= sphb
->io_win_size
;
1880 if ((sphb
->mem64_win_size
!= 0)
1881 && (sphb
->mem64_win_addr
1882 == (sphb
->mem_win_addr
+ sphb
->mem_win_size
))) {
1883 sphb
->mig_mem_win_size
+= sphb
->mem64_win_size
;
1887 g_free(sphb
->msi_devs
);
1888 sphb
->msi_devs
= NULL
;
1889 sphb
->msi_devs_num
= g_hash_table_size(sphb
->msi
);
1890 if (!sphb
->msi_devs_num
) {
1893 sphb
->msi_devs
= g_new(spapr_pci_msi_mig
, sphb
->msi_devs_num
);
1895 g_hash_table_iter_init(&iter
, sphb
->msi
);
1896 for (i
= 0; g_hash_table_iter_next(&iter
, &key
, &value
); ++i
) {
1897 sphb
->msi_devs
[i
].key
= *(uint32_t *) key
;
1898 sphb
->msi_devs
[i
].value
= *(spapr_pci_msi
*) value
;
1904 static int spapr_pci_post_load(void *opaque
, int version_id
)
1906 sPAPRPHBState
*sphb
= opaque
;
1907 gpointer key
, value
;
1910 for (i
= 0; i
< sphb
->msi_devs_num
; ++i
) {
1911 key
= g_memdup(&sphb
->msi_devs
[i
].key
,
1912 sizeof(sphb
->msi_devs
[i
].key
));
1913 value
= g_memdup(&sphb
->msi_devs
[i
].value
,
1914 sizeof(sphb
->msi_devs
[i
].value
));
1915 g_hash_table_insert(sphb
->msi
, key
, value
);
1917 g_free(sphb
->msi_devs
);
1918 sphb
->msi_devs
= NULL
;
1919 sphb
->msi_devs_num
= 0;
1924 static bool pre_2_8_migration(void *opaque
, int version_id
)
1926 sPAPRPHBState
*sphb
= opaque
;
1928 return sphb
->pre_2_8_migration
;
1931 static const VMStateDescription vmstate_spapr_pci
= {
1932 .name
= "spapr_pci",
1934 .minimum_version_id
= 2,
1935 .pre_save
= spapr_pci_pre_save
,
1936 .post_load
= spapr_pci_post_load
,
1937 .fields
= (VMStateField
[]) {
1938 VMSTATE_UINT64_EQUAL(buid
, sPAPRPHBState
, NULL
),
1939 VMSTATE_UINT32_TEST(mig_liobn
, sPAPRPHBState
, pre_2_8_migration
),
1940 VMSTATE_UINT64_TEST(mig_mem_win_addr
, sPAPRPHBState
, pre_2_8_migration
),
1941 VMSTATE_UINT64_TEST(mig_mem_win_size
, sPAPRPHBState
, pre_2_8_migration
),
1942 VMSTATE_UINT64_TEST(mig_io_win_addr
, sPAPRPHBState
, pre_2_8_migration
),
1943 VMSTATE_UINT64_TEST(mig_io_win_size
, sPAPRPHBState
, pre_2_8_migration
),
1944 VMSTATE_STRUCT_ARRAY(lsi_table
, sPAPRPHBState
, PCI_NUM_PINS
, 0,
1945 vmstate_spapr_pci_lsi
, struct spapr_pci_lsi
),
1946 VMSTATE_INT32(msi_devs_num
, sPAPRPHBState
),
1947 VMSTATE_STRUCT_VARRAY_ALLOC(msi_devs
, sPAPRPHBState
, msi_devs_num
, 0,
1948 vmstate_spapr_pci_msi
, spapr_pci_msi_mig
),
1949 VMSTATE_END_OF_LIST()
1953 static const char *spapr_phb_root_bus_path(PCIHostState
*host_bridge
,
1956 sPAPRPHBState
*sphb
= SPAPR_PCI_HOST_BRIDGE(host_bridge
);
1958 return sphb
->dtbusname
;
1961 static void spapr_phb_class_init(ObjectClass
*klass
, void *data
)
1963 PCIHostBridgeClass
*hc
= PCI_HOST_BRIDGE_CLASS(klass
);
1964 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1965 HotplugHandlerClass
*hp
= HOTPLUG_HANDLER_CLASS(klass
);
1967 hc
->root_bus_path
= spapr_phb_root_bus_path
;
1968 dc
->realize
= spapr_phb_realize
;
1969 dc
->props
= spapr_phb_properties
;
1970 dc
->reset
= spapr_phb_reset
;
1971 dc
->vmsd
= &vmstate_spapr_pci
;
1972 /* Supported by TYPE_SPAPR_MACHINE */
1973 dc
->user_creatable
= true;
1974 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
1975 hp
->plug
= spapr_pci_plug
;
1976 hp
->unplug
= spapr_pci_unplug
;
1977 hp
->unplug_request
= spapr_pci_unplug_request
;
1980 static const TypeInfo spapr_phb_info
= {
1981 .name
= TYPE_SPAPR_PCI_HOST_BRIDGE
,
1982 .parent
= TYPE_PCI_HOST_BRIDGE
,
1983 .instance_size
= sizeof(sPAPRPHBState
),
1984 .class_init
= spapr_phb_class_init
,
1985 .interfaces
= (InterfaceInfo
[]) {
1986 { TYPE_HOTPLUG_HANDLER
},
1991 typedef struct sPAPRFDT
{
1994 sPAPRPHBState
*sphb
;
1997 static void spapr_populate_pci_devices_dt(PCIBus
*bus
, PCIDevice
*pdev
,
2001 sPAPRFDT
*p
= opaque
;
2005 offset
= spapr_create_pci_child_dt(p
->sphb
, pdev
, p
->fdt
, p
->node_off
);
2007 error_report("Failed to create pci child device tree node");
2011 if ((pci_default_read_config(pdev
, PCI_HEADER_TYPE
, 1) !=
2012 PCI_HEADER_TYPE_BRIDGE
)) {
2016 sec_bus
= pci_bridge_get_sec_bus(PCI_BRIDGE(pdev
));
2022 s_fdt
.node_off
= offset
;
2023 s_fdt
.sphb
= p
->sphb
;
2024 pci_for_each_device_reverse(sec_bus
, pci_bus_num(sec_bus
),
2025 spapr_populate_pci_devices_dt
,
2029 static void spapr_phb_pci_enumerate_bridge(PCIBus
*bus
, PCIDevice
*pdev
,
2032 unsigned int *bus_no
= opaque
;
2033 unsigned int primary
= *bus_no
;
2034 unsigned int subordinate
= 0xff;
2035 PCIBus
*sec_bus
= NULL
;
2037 if ((pci_default_read_config(pdev
, PCI_HEADER_TYPE
, 1) !=
2038 PCI_HEADER_TYPE_BRIDGE
)) {
2043 pci_default_write_config(pdev
, PCI_PRIMARY_BUS
, primary
, 1);
2044 pci_default_write_config(pdev
, PCI_SECONDARY_BUS
, *bus_no
, 1);
2045 pci_default_write_config(pdev
, PCI_SUBORDINATE_BUS
, *bus_no
, 1);
2047 sec_bus
= pci_bridge_get_sec_bus(PCI_BRIDGE(pdev
));
2052 pci_default_write_config(pdev
, PCI_SUBORDINATE_BUS
, subordinate
, 1);
2053 pci_for_each_device(sec_bus
, pci_bus_num(sec_bus
),
2054 spapr_phb_pci_enumerate_bridge
, bus_no
);
2055 pci_default_write_config(pdev
, PCI_SUBORDINATE_BUS
, *bus_no
, 1);
2058 static void spapr_phb_pci_enumerate(sPAPRPHBState
*phb
)
2060 PCIBus
*bus
= PCI_HOST_BRIDGE(phb
)->bus
;
2061 unsigned int bus_no
= 0;
2063 pci_for_each_device(bus
, pci_bus_num(bus
),
2064 spapr_phb_pci_enumerate_bridge
,
2069 int spapr_populate_pci_dt(sPAPRPHBState
*phb
, uint32_t xics_phandle
, void *fdt
,
2072 int bus_off
, i
, j
, ret
;
2074 uint32_t bus_range
[] = { cpu_to_be32(0), cpu_to_be32(0xff) };
2080 } QEMU_PACKED ranges
[] = {
2082 cpu_to_be32(b_ss(1)), cpu_to_be64(0),
2083 cpu_to_be64(phb
->io_win_addr
),
2084 cpu_to_be64(memory_region_size(&phb
->iospace
)),
2087 cpu_to_be32(b_ss(2)), cpu_to_be64(SPAPR_PCI_MEM_WIN_BUS_OFFSET
),
2088 cpu_to_be64(phb
->mem_win_addr
),
2089 cpu_to_be64(phb
->mem_win_size
),
2092 cpu_to_be32(b_ss(3)), cpu_to_be64(phb
->mem64_win_pciaddr
),
2093 cpu_to_be64(phb
->mem64_win_addr
),
2094 cpu_to_be64(phb
->mem64_win_size
),
2097 const unsigned sizeof_ranges
=
2098 (phb
->mem64_win_size
? 3 : 2) * sizeof(ranges
[0]);
2099 uint64_t bus_reg
[] = { cpu_to_be64(phb
->buid
), 0 };
2100 uint32_t interrupt_map_mask
[] = {
2101 cpu_to_be32(b_ddddd(-1)|b_fff(0)), 0x0, 0x0, cpu_to_be32(-1)};
2102 uint32_t interrupt_map
[PCI_SLOT_MAX
* PCI_NUM_PINS
][7];
2103 uint32_t ddw_applicable
[] = {
2104 cpu_to_be32(RTAS_IBM_QUERY_PE_DMA_WINDOW
),
2105 cpu_to_be32(RTAS_IBM_CREATE_PE_DMA_WINDOW
),
2106 cpu_to_be32(RTAS_IBM_REMOVE_PE_DMA_WINDOW
)
2108 uint32_t ddw_extensions
[] = {
2110 cpu_to_be32(RTAS_IBM_RESET_PE_DMA_WINDOW
)
2112 uint32_t associativity
[] = {cpu_to_be32(0x4),
2116 cpu_to_be32(phb
->numa_node
)};
2117 sPAPRTCETable
*tcet
;
2118 PCIBus
*bus
= PCI_HOST_BRIDGE(phb
)->bus
;
2121 /* Start populating the FDT */
2122 nodename
= g_strdup_printf("pci@%" PRIx64
, phb
->buid
);
2123 _FDT(bus_off
= fdt_add_subnode(fdt
, 0, nodename
));
2126 /* Write PHB properties */
2127 _FDT(fdt_setprop_string(fdt
, bus_off
, "device_type", "pci"));
2128 _FDT(fdt_setprop_string(fdt
, bus_off
, "compatible", "IBM,Logical_PHB"));
2129 _FDT(fdt_setprop_cell(fdt
, bus_off
, "#address-cells", 0x3));
2130 _FDT(fdt_setprop_cell(fdt
, bus_off
, "#size-cells", 0x2));
2131 _FDT(fdt_setprop_cell(fdt
, bus_off
, "#interrupt-cells", 0x1));
2132 _FDT(fdt_setprop(fdt
, bus_off
, "used-by-rtas", NULL
, 0));
2133 _FDT(fdt_setprop(fdt
, bus_off
, "bus-range", &bus_range
, sizeof(bus_range
)));
2134 _FDT(fdt_setprop(fdt
, bus_off
, "ranges", &ranges
, sizeof_ranges
));
2135 _FDT(fdt_setprop(fdt
, bus_off
, "reg", &bus_reg
, sizeof(bus_reg
)));
2136 _FDT(fdt_setprop_cell(fdt
, bus_off
, "ibm,pci-config-space-type", 0x1));
2137 _FDT(fdt_setprop_cell(fdt
, bus_off
, "ibm,pe-total-#msi", nr_msis
));
2139 /* Dynamic DMA window */
2140 if (phb
->ddw_enabled
) {
2141 _FDT(fdt_setprop(fdt
, bus_off
, "ibm,ddw-applicable", &ddw_applicable
,
2142 sizeof(ddw_applicable
)));
2143 _FDT(fdt_setprop(fdt
, bus_off
, "ibm,ddw-extensions",
2144 &ddw_extensions
, sizeof(ddw_extensions
)));
2147 /* Advertise NUMA via ibm,associativity */
2148 if (phb
->numa_node
!= -1) {
2149 _FDT(fdt_setprop(fdt
, bus_off
, "ibm,associativity", associativity
,
2150 sizeof(associativity
)));
2153 /* Build the interrupt-map, this must matches what is done
2154 * in pci_spapr_map_irq
2156 _FDT(fdt_setprop(fdt
, bus_off
, "interrupt-map-mask",
2157 &interrupt_map_mask
, sizeof(interrupt_map_mask
)));
2158 for (i
= 0; i
< PCI_SLOT_MAX
; i
++) {
2159 for (j
= 0; j
< PCI_NUM_PINS
; j
++) {
2160 uint32_t *irqmap
= interrupt_map
[i
*PCI_NUM_PINS
+ j
];
2161 int lsi_num
= pci_spapr_swizzle(i
, j
);
2163 irqmap
[0] = cpu_to_be32(b_ddddd(i
)|b_fff(0));
2166 irqmap
[3] = cpu_to_be32(j
+1);
2167 irqmap
[4] = cpu_to_be32(xics_phandle
);
2168 spapr_dt_xics_irq(&irqmap
[5], phb
->lsi_table
[lsi_num
].irq
, true);
2171 /* Write interrupt map */
2172 _FDT(fdt_setprop(fdt
, bus_off
, "interrupt-map", &interrupt_map
,
2173 sizeof(interrupt_map
)));
2175 tcet
= spapr_tce_find_by_liobn(phb
->dma_liobn
[0]);
2179 spapr_dma_dt(fdt
, bus_off
, "ibm,dma-window",
2180 tcet
->liobn
, tcet
->bus_offset
,
2181 tcet
->nb_table
<< tcet
->page_shift
);
2183 /* Walk the bridges and program the bus numbers*/
2184 spapr_phb_pci_enumerate(phb
);
2185 _FDT(fdt_setprop_cell(fdt
, bus_off
, "qemu,phb-enumerated", 0x1));
2187 /* Populate tree nodes with PCI devices attached */
2189 s_fdt
.node_off
= bus_off
;
2191 pci_for_each_device_reverse(bus
, pci_bus_num(bus
),
2192 spapr_populate_pci_devices_dt
,
2195 ret
= spapr_drc_populate_dt(fdt
, bus_off
, OBJECT(phb
),
2196 SPAPR_DR_CONNECTOR_TYPE_PCI
);
2204 void spapr_pci_rtas_init(void)
2206 spapr_rtas_register(RTAS_READ_PCI_CONFIG
, "read-pci-config",
2207 rtas_read_pci_config
);
2208 spapr_rtas_register(RTAS_WRITE_PCI_CONFIG
, "write-pci-config",
2209 rtas_write_pci_config
);
2210 spapr_rtas_register(RTAS_IBM_READ_PCI_CONFIG
, "ibm,read-pci-config",
2211 rtas_ibm_read_pci_config
);
2212 spapr_rtas_register(RTAS_IBM_WRITE_PCI_CONFIG
, "ibm,write-pci-config",
2213 rtas_ibm_write_pci_config
);
2214 if (msi_nonbroken
) {
2215 spapr_rtas_register(RTAS_IBM_QUERY_INTERRUPT_SOURCE_NUMBER
,
2216 "ibm,query-interrupt-source-number",
2217 rtas_ibm_query_interrupt_source_number
);
2218 spapr_rtas_register(RTAS_IBM_CHANGE_MSI
, "ibm,change-msi",
2219 rtas_ibm_change_msi
);
2222 spapr_rtas_register(RTAS_IBM_SET_EEH_OPTION
,
2223 "ibm,set-eeh-option",
2224 rtas_ibm_set_eeh_option
);
2225 spapr_rtas_register(RTAS_IBM_GET_CONFIG_ADDR_INFO2
,
2226 "ibm,get-config-addr-info2",
2227 rtas_ibm_get_config_addr_info2
);
2228 spapr_rtas_register(RTAS_IBM_READ_SLOT_RESET_STATE2
,
2229 "ibm,read-slot-reset-state2",
2230 rtas_ibm_read_slot_reset_state2
);
2231 spapr_rtas_register(RTAS_IBM_SET_SLOT_RESET
,
2232 "ibm,set-slot-reset",
2233 rtas_ibm_set_slot_reset
);
2234 spapr_rtas_register(RTAS_IBM_CONFIGURE_PE
,
2236 rtas_ibm_configure_pe
);
2237 spapr_rtas_register(RTAS_IBM_SLOT_ERROR_DETAIL
,
2238 "ibm,slot-error-detail",
2239 rtas_ibm_slot_error_detail
);
2242 static void spapr_pci_register_types(void)
2244 type_register_static(&spapr_phb_info
);
2247 type_init(spapr_pci_register_types
)
2249 static int spapr_switch_one_vga(DeviceState
*dev
, void *opaque
)
2251 bool be
= *(bool *)opaque
;
2253 if (object_dynamic_cast(OBJECT(dev
), "VGA")
2254 || object_dynamic_cast(OBJECT(dev
), "secondary-vga")) {
2255 object_property_set_bool(OBJECT(dev
), be
, "big-endian-framebuffer",
2261 void spapr_pci_switch_vga(bool big_endian
)
2263 sPAPRMachineState
*spapr
= SPAPR_MACHINE(qdev_get_machine());
2264 sPAPRPHBState
*sphb
;
2267 * For backward compatibility with existing guests, we switch
2268 * the endianness of the VGA controller when changing the guest
2271 QLIST_FOREACH(sphb
, &spapr
->phbs
, list
) {
2272 BusState
*bus
= &PCI_HOST_BRIDGE(sphb
)->bus
->qbus
;
2273 qbus_walk_children(bus
, spapr_switch_one_vga
, NULL
, NULL
, NULL
,