4 * Copyright 2014 IBM Corp.
5 * Author(s): Frank Blaschka <frank.blaschka@de.ibm.com>
6 * Hong Bo Li <lihbbj@cn.ibm.com>
7 * Yi Min Zhao <zyimin@cn.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or (at
10 * your option) any later version. See the COPYING file in the top-level
14 #include "qemu/osdep.h"
15 #include "qapi/error.h"
16 #include "qapi/visitor.h"
18 #include "s390-pci-bus.h"
19 #include "s390-pci-inst.h"
20 #include "hw/pci/pci_bus.h"
21 #include "hw/qdev-properties.h"
22 #include "hw/pci/pci_bridge.h"
23 #include "hw/pci/msi.h"
24 #include "qemu/error-report.h"
25 #include "qemu/module.h"
27 #ifndef DEBUG_S390PCI_BUS
28 #define DEBUG_S390PCI_BUS 0
31 #define DPRINTF(fmt, ...) \
33 if (DEBUG_S390PCI_BUS) { \
34 fprintf(stderr, "S390pci-bus: " fmt, ## __VA_ARGS__); \
38 S390pciState
*s390_get_phb(void)
40 static S390pciState
*phb
;
43 phb
= S390_PCI_HOST_BRIDGE(
44 object_resolve_path(TYPE_S390_PCI_HOST_BRIDGE
, NULL
));
51 int pci_chsc_sei_nt2_get_event(void *res
)
53 ChscSeiNt2Res
*nt2_res
= (ChscSeiNt2Res
*)res
;
57 SeiContainer
*sei_cont
;
58 S390pciState
*s
= s390_get_phb();
60 sei_cont
= QTAILQ_FIRST(&s
->pending_sei
);
62 QTAILQ_REMOVE(&s
->pending_sei
, sei_cont
, link
);
64 nt2_res
->cc
= sei_cont
->cc
;
65 nt2_res
->length
= cpu_to_be16(sizeof(ChscSeiNt2Res
));
66 switch (sei_cont
->cc
) {
67 case 1: /* error event */
68 eccdf
= (PciCcdfErr
*)nt2_res
->ccdf
;
69 eccdf
->fid
= cpu_to_be32(sei_cont
->fid
);
70 eccdf
->fh
= cpu_to_be32(sei_cont
->fh
);
71 eccdf
->e
= cpu_to_be32(sei_cont
->e
);
72 eccdf
->faddr
= cpu_to_be64(sei_cont
->faddr
);
73 eccdf
->pec
= cpu_to_be16(sei_cont
->pec
);
75 case 2: /* availability event */
76 accdf
= (PciCcdfAvail
*)nt2_res
->ccdf
;
77 accdf
->fid
= cpu_to_be32(sei_cont
->fid
);
78 accdf
->fh
= cpu_to_be32(sei_cont
->fh
);
79 accdf
->pec
= cpu_to_be16(sei_cont
->pec
);
91 int pci_chsc_sei_nt2_have_event(void)
93 S390pciState
*s
= s390_get_phb();
95 return !QTAILQ_EMPTY(&s
->pending_sei
);
98 S390PCIBusDevice
*s390_pci_find_next_avail_dev(S390pciState
*s
,
99 S390PCIBusDevice
*pbdev
)
101 S390PCIBusDevice
*ret
= pbdev
? QTAILQ_NEXT(pbdev
, link
) :
102 QTAILQ_FIRST(&s
->zpci_devs
);
104 while (ret
&& ret
->state
== ZPCI_FS_RESERVED
) {
105 ret
= QTAILQ_NEXT(ret
, link
);
111 S390PCIBusDevice
*s390_pci_find_dev_by_fid(S390pciState
*s
, uint32_t fid
)
113 S390PCIBusDevice
*pbdev
;
115 QTAILQ_FOREACH(pbdev
, &s
->zpci_devs
, link
) {
116 if (pbdev
->fid
== fid
) {
124 void s390_pci_sclp_configure(SCCB
*sccb
)
126 IoaCfgSccb
*psccb
= (IoaCfgSccb
*)sccb
;
127 S390PCIBusDevice
*pbdev
= s390_pci_find_dev_by_fid(s390_get_phb(),
128 be32_to_cpu(psccb
->aid
));
132 DPRINTF("sclp config no dev found\n");
133 rc
= SCLP_RC_ADAPTER_ID_NOT_RECOGNIZED
;
137 switch (pbdev
->state
) {
138 case ZPCI_FS_RESERVED
:
139 rc
= SCLP_RC_ADAPTER_IN_RESERVED_STATE
;
141 case ZPCI_FS_STANDBY
:
142 pbdev
->state
= ZPCI_FS_DISABLED
;
143 rc
= SCLP_RC_NORMAL_COMPLETION
;
146 rc
= SCLP_RC_NO_ACTION_REQUIRED
;
149 psccb
->header
.response_code
= cpu_to_be16(rc
);
152 static void s390_pci_perform_unplug(S390PCIBusDevice
*pbdev
)
154 HotplugHandler
*hotplug_ctrl
;
156 /* Unplug the PCI device */
158 DeviceState
*pdev
= DEVICE(pbdev
->pdev
);
160 hotplug_ctrl
= qdev_get_hotplug_handler(pdev
);
161 hotplug_handler_unplug(hotplug_ctrl
, pdev
, &error_abort
);
162 object_unparent(OBJECT(pdev
));
165 /* Unplug the zPCI device */
166 hotplug_ctrl
= qdev_get_hotplug_handler(DEVICE(pbdev
));
167 hotplug_handler_unplug(hotplug_ctrl
, DEVICE(pbdev
), &error_abort
);
168 object_unparent(OBJECT(pbdev
));
171 void s390_pci_sclp_deconfigure(SCCB
*sccb
)
173 IoaCfgSccb
*psccb
= (IoaCfgSccb
*)sccb
;
174 S390PCIBusDevice
*pbdev
= s390_pci_find_dev_by_fid(s390_get_phb(),
175 be32_to_cpu(psccb
->aid
));
179 DPRINTF("sclp deconfig no dev found\n");
180 rc
= SCLP_RC_ADAPTER_ID_NOT_RECOGNIZED
;
184 switch (pbdev
->state
) {
185 case ZPCI_FS_RESERVED
:
186 rc
= SCLP_RC_ADAPTER_IN_RESERVED_STATE
;
188 case ZPCI_FS_STANDBY
:
189 rc
= SCLP_RC_NO_ACTION_REQUIRED
;
192 if (pbdev
->summary_ind
) {
193 pci_dereg_irqs(pbdev
);
195 if (pbdev
->iommu
->enabled
) {
196 pci_dereg_ioat(pbdev
->iommu
);
198 pbdev
->state
= ZPCI_FS_STANDBY
;
199 rc
= SCLP_RC_NORMAL_COMPLETION
;
201 if (pbdev
->unplug_requested
) {
202 s390_pci_perform_unplug(pbdev
);
206 psccb
->header
.response_code
= cpu_to_be16(rc
);
209 static S390PCIBusDevice
*s390_pci_find_dev_by_uid(S390pciState
*s
, uint16_t uid
)
211 S390PCIBusDevice
*pbdev
;
213 QTAILQ_FOREACH(pbdev
, &s
->zpci_devs
, link
) {
214 if (pbdev
->uid
== uid
) {
222 S390PCIBusDevice
*s390_pci_find_dev_by_target(S390pciState
*s
,
225 S390PCIBusDevice
*pbdev
;
231 QTAILQ_FOREACH(pbdev
, &s
->zpci_devs
, link
) {
232 if (!strcmp(pbdev
->target
, target
)) {
240 static S390PCIBusDevice
*s390_pci_find_dev_by_pci(S390pciState
*s
,
243 S390PCIBusDevice
*pbdev
;
249 QTAILQ_FOREACH(pbdev
, &s
->zpci_devs
, link
) {
250 if (pbdev
->pdev
== pci_dev
) {
258 S390PCIBusDevice
*s390_pci_find_dev_by_idx(S390pciState
*s
, uint32_t idx
)
260 return g_hash_table_lookup(s
->zpci_table
, &idx
);
263 S390PCIBusDevice
*s390_pci_find_dev_by_fh(S390pciState
*s
, uint32_t fh
)
265 uint32_t idx
= FH_MASK_INDEX
& fh
;
266 S390PCIBusDevice
*pbdev
= s390_pci_find_dev_by_idx(s
, idx
);
268 if (pbdev
&& pbdev
->fh
== fh
) {
275 static void s390_pci_generate_event(uint8_t cc
, uint16_t pec
, uint32_t fh
,
276 uint32_t fid
, uint64_t faddr
, uint32_t e
)
278 SeiContainer
*sei_cont
;
279 S390pciState
*s
= s390_get_phb();
281 sei_cont
= g_new0(SeiContainer
, 1);
286 sei_cont
->faddr
= faddr
;
289 QTAILQ_INSERT_TAIL(&s
->pending_sei
, sei_cont
, link
);
290 css_generate_css_crws(0);
293 static void s390_pci_generate_plug_event(uint16_t pec
, uint32_t fh
,
296 s390_pci_generate_event(2, pec
, fh
, fid
, 0, 0);
299 void s390_pci_generate_error_event(uint16_t pec
, uint32_t fh
, uint32_t fid
,
300 uint64_t faddr
, uint32_t e
)
302 s390_pci_generate_event(1, pec
, fh
, fid
, faddr
, e
);
305 static void s390_pci_set_irq(void *opaque
, int irq
, int level
)
310 static int s390_pci_map_irq(PCIDevice
*pci_dev
, int irq_num
)
316 static uint64_t s390_pci_get_table_origin(uint64_t iota
)
318 return iota
& ~ZPCI_IOTA_RTTO_FLAG
;
321 static unsigned int calc_rtx(dma_addr_t ptr
)
323 return ((unsigned long) ptr
>> ZPCI_RT_SHIFT
) & ZPCI_INDEX_MASK
;
326 static unsigned int calc_sx(dma_addr_t ptr
)
328 return ((unsigned long) ptr
>> ZPCI_ST_SHIFT
) & ZPCI_INDEX_MASK
;
331 static unsigned int calc_px(dma_addr_t ptr
)
333 return ((unsigned long) ptr
>> PAGE_SHIFT
) & ZPCI_PT_MASK
;
336 static uint64_t get_rt_sto(uint64_t entry
)
338 return ((entry
& ZPCI_TABLE_TYPE_MASK
) == ZPCI_TABLE_TYPE_RTX
)
339 ? (entry
& ZPCI_RTE_ADDR_MASK
)
343 static uint64_t get_st_pto(uint64_t entry
)
345 return ((entry
& ZPCI_TABLE_TYPE_MASK
) == ZPCI_TABLE_TYPE_SX
)
346 ? (entry
& ZPCI_STE_ADDR_MASK
)
350 static bool rt_entry_isvalid(uint64_t entry
)
352 return (entry
& ZPCI_TABLE_VALID_MASK
) == ZPCI_TABLE_VALID
;
355 static bool pt_entry_isvalid(uint64_t entry
)
357 return (entry
& ZPCI_PTE_VALID_MASK
) == ZPCI_PTE_VALID
;
360 static bool entry_isprotected(uint64_t entry
)
362 return (entry
& ZPCI_TABLE_PROT_MASK
) == ZPCI_TABLE_PROTECTED
;
365 /* ett is expected table type, -1 page table, 0 segment table, 1 region table */
366 static uint64_t get_table_index(uint64_t iova
, int8_t ett
)
370 return calc_px(iova
);
372 return calc_sx(iova
);
374 return calc_rtx(iova
);
380 static bool entry_isvalid(uint64_t entry
, int8_t ett
)
384 return pt_entry_isvalid(entry
);
387 return rt_entry_isvalid(entry
);
393 /* Return true if address translation is done */
394 static bool translate_iscomplete(uint64_t entry
, int8_t ett
)
398 return (entry
& ZPCI_TABLE_FC
) ? true : false;
406 static uint64_t get_frame_size(int8_t ett
)
420 static uint64_t get_next_table_origin(uint64_t entry
, int8_t ett
)
424 return entry
& ZPCI_PTE_ADDR_MASK
;
426 return get_st_pto(entry
);
428 return get_rt_sto(entry
);
435 * table_translate: do translation within one table and return the following
438 * @entry: the entry being translated, the result is stored in this.
439 * @to: the address of table origin.
440 * @ett: expected table type, 1 region table, 0 segment table and -1 page table.
443 static uint64_t table_translate(S390IOTLBEntry
*entry
, uint64_t to
, int8_t ett
,
446 uint64_t tx
, te
, nto
= 0;
449 tx
= get_table_index(entry
->iova
, ett
);
450 te
= address_space_ldq(&address_space_memory
, to
+ tx
* sizeof(uint64_t),
451 MEMTXATTRS_UNSPECIFIED
, NULL
);
454 err
= ERR_EVENT_INVALTE
;
458 if (!entry_isvalid(te
, ett
)) {
459 entry
->perm
&= IOMMU_NONE
;
463 if (ett
== ZPCI_ETT_RT
&& ((te
& ZPCI_TABLE_LEN_RTX
) != ZPCI_TABLE_LEN_RTX
464 || te
& ZPCI_TABLE_OFFSET_MASK
)) {
465 err
= ERR_EVENT_INVALTL
;
469 nto
= get_next_table_origin(te
, ett
);
475 if (entry_isprotected(te
)) {
476 entry
->perm
&= IOMMU_RO
;
478 entry
->perm
&= IOMMU_RW
;
481 if (translate_iscomplete(te
, ett
)) {
484 entry
->translated_addr
= te
& ZPCI_PTE_ADDR_MASK
;
487 entry
->translated_addr
= (te
& ZPCI_SFAA_MASK
) |
488 (entry
->iova
& ~ZPCI_SFAA_MASK
);
495 entry
->perm
= IOMMU_NONE
;
498 entry
->len
= get_frame_size(ett
);
502 uint16_t s390_guest_io_table_walk(uint64_t g_iota
, hwaddr addr
,
503 S390IOTLBEntry
*entry
)
505 uint64_t to
= s390_pci_get_table_origin(g_iota
);
509 entry
->iova
= addr
& PAGE_MASK
;
510 entry
->translated_addr
= 0;
511 entry
->perm
= IOMMU_RW
;
513 if (entry_isprotected(g_iota
)) {
514 entry
->perm
&= IOMMU_RO
;
518 to
= table_translate(entry
, to
, ett
--, &error
);
524 static IOMMUTLBEntry
s390_translate_iommu(IOMMUMemoryRegion
*mr
, hwaddr addr
,
525 IOMMUAccessFlags flag
, int iommu_idx
)
527 S390PCIIOMMU
*iommu
= container_of(mr
, S390PCIIOMMU
, iommu_mr
);
528 S390IOTLBEntry
*entry
;
529 uint64_t iova
= addr
& PAGE_MASK
;
531 IOMMUTLBEntry ret
= {
532 .target_as
= &address_space_memory
,
534 .translated_addr
= 0,
535 .addr_mask
= ~(hwaddr
)0,
539 switch (iommu
->pbdev
->state
) {
540 case ZPCI_FS_ENABLED
:
541 case ZPCI_FS_BLOCKED
:
542 if (!iommu
->enabled
) {
550 DPRINTF("iommu trans addr 0x%" PRIx64
"\n", addr
);
552 if (addr
< iommu
->pba
|| addr
> iommu
->pal
) {
553 error
= ERR_EVENT_OORANGE
;
557 entry
= g_hash_table_lookup(iommu
->iotlb
, &iova
);
559 ret
.iova
= entry
->iova
;
560 ret
.translated_addr
= entry
->translated_addr
;
561 ret
.addr_mask
= entry
->len
- 1;
562 ret
.perm
= entry
->perm
;
565 ret
.addr_mask
= ~PAGE_MASK
;
566 ret
.perm
= IOMMU_NONE
;
569 if (flag
!= IOMMU_NONE
&& !(flag
& ret
.perm
)) {
570 error
= ERR_EVENT_TPROTE
;
574 iommu
->pbdev
->state
= ZPCI_FS_ERROR
;
575 s390_pci_generate_error_event(error
, iommu
->pbdev
->fh
,
576 iommu
->pbdev
->fid
, addr
, 0);
581 static void s390_pci_iommu_replay(IOMMUMemoryRegion
*iommu
,
582 IOMMUNotifier
*notifier
)
584 /* It's impossible to plug a pci device on s390x that already has iommu
585 * mappings which need to be replayed, that is due to the "one iommu per
586 * zpci device" construct. But when we support migration of vfio-pci
587 * devices in future, we need to revisit this.
592 static S390PCIIOMMU
*s390_pci_get_iommu(S390pciState
*s
, PCIBus
*bus
,
595 uint64_t key
= (uintptr_t)bus
;
596 S390PCIIOMMUTable
*table
= g_hash_table_lookup(s
->iommu_table
, &key
);
600 table
= g_new0(S390PCIIOMMUTable
, 1);
602 g_hash_table_insert(s
->iommu_table
, &table
->key
, table
);
605 iommu
= table
->iommu
[PCI_SLOT(devfn
)];
607 iommu
= S390_PCI_IOMMU(object_new(TYPE_S390_PCI_IOMMU
));
609 char *mr_name
= g_strdup_printf("iommu-root-%02x:%02x.%01x",
613 char *as_name
= g_strdup_printf("iommu-pci-%02x:%02x.%01x",
617 memory_region_init(&iommu
->mr
, OBJECT(iommu
), mr_name
, UINT64_MAX
);
618 address_space_init(&iommu
->as
, &iommu
->mr
, as_name
);
619 iommu
->iotlb
= g_hash_table_new_full(g_int64_hash
, g_int64_equal
,
621 table
->iommu
[PCI_SLOT(devfn
)] = iommu
;
630 static AddressSpace
*s390_pci_dma_iommu(PCIBus
*bus
, void *opaque
, int devfn
)
632 S390pciState
*s
= opaque
;
633 S390PCIIOMMU
*iommu
= s390_pci_get_iommu(s
, bus
, devfn
);
638 static uint8_t set_ind_atomic(uint64_t ind_loc
, uint8_t to_be_set
)
640 uint8_t ind_old
, ind_new
;
644 ind_addr
= cpu_physical_memory_map(ind_loc
, &len
, 1);
646 s390_pci_generate_error_event(ERR_EVENT_AIRERR
, 0, 0, 0, 0);
651 ind_new
= ind_old
| to_be_set
;
652 } while (atomic_cmpxchg(ind_addr
, ind_old
, ind_new
) != ind_old
);
653 cpu_physical_memory_unmap(ind_addr
, len
, 1, len
);
658 static void s390_msi_ctrl_write(void *opaque
, hwaddr addr
, uint64_t data
,
661 S390PCIBusDevice
*pbdev
= opaque
;
662 uint32_t vec
= data
& ZPCI_MSI_VEC_MASK
;
667 DPRINTF("write_msix data 0x%" PRIx64
" idx %d vec 0x%x\n", data
,
670 if (pbdev
->state
!= ZPCI_FS_ENABLED
) {
674 ind_bit
= pbdev
->routes
.adapter
.ind_offset
;
675 sum_bit
= pbdev
->routes
.adapter
.summary_offset
;
677 set_ind_atomic(pbdev
->routes
.adapter
.ind_addr
+ (ind_bit
+ vec
) / 8,
678 0x80 >> ((ind_bit
+ vec
) % 8));
679 if (!set_ind_atomic(pbdev
->routes
.adapter
.summary_addr
+ sum_bit
/ 8,
680 0x80 >> (sum_bit
% 8))) {
681 css_adapter_interrupt(CSS_IO_ADAPTER_PCI
, pbdev
->isc
);
685 static uint64_t s390_msi_ctrl_read(void *opaque
, hwaddr addr
, unsigned size
)
690 static const MemoryRegionOps s390_msi_ctrl_ops
= {
691 .write
= s390_msi_ctrl_write
,
692 .read
= s390_msi_ctrl_read
,
693 .endianness
= DEVICE_LITTLE_ENDIAN
,
696 void s390_pci_iommu_enable(S390PCIIOMMU
*iommu
)
698 char *name
= g_strdup_printf("iommu-s390-%04x", iommu
->pbdev
->uid
);
699 memory_region_init_iommu(&iommu
->iommu_mr
, sizeof(iommu
->iommu_mr
),
700 TYPE_S390_IOMMU_MEMORY_REGION
, OBJECT(&iommu
->mr
),
701 name
, iommu
->pal
- iommu
->pba
+ 1);
702 iommu
->enabled
= true;
703 memory_region_add_subregion(&iommu
->mr
, 0, MEMORY_REGION(&iommu
->iommu_mr
));
707 void s390_pci_iommu_disable(S390PCIIOMMU
*iommu
)
709 iommu
->enabled
= false;
710 g_hash_table_remove_all(iommu
->iotlb
);
711 memory_region_del_subregion(&iommu
->mr
, MEMORY_REGION(&iommu
->iommu_mr
));
712 object_unparent(OBJECT(&iommu
->iommu_mr
));
715 static void s390_pci_iommu_free(S390pciState
*s
, PCIBus
*bus
, int32_t devfn
)
717 uint64_t key
= (uintptr_t)bus
;
718 S390PCIIOMMUTable
*table
= g_hash_table_lookup(s
->iommu_table
, &key
);
719 S390PCIIOMMU
*iommu
= table
? table
->iommu
[PCI_SLOT(devfn
)] : NULL
;
721 if (!table
|| !iommu
) {
725 table
->iommu
[PCI_SLOT(devfn
)] = NULL
;
726 g_hash_table_destroy(iommu
->iotlb
);
727 address_space_destroy(&iommu
->as
);
728 object_unparent(OBJECT(&iommu
->mr
));
729 object_unparent(OBJECT(iommu
));
730 object_unref(OBJECT(iommu
));
733 static void s390_pcihost_realize(DeviceState
*dev
, Error
**errp
)
737 PCIHostState
*phb
= PCI_HOST_BRIDGE(dev
);
738 S390pciState
*s
= S390_PCI_HOST_BRIDGE(dev
);
739 Error
*local_err
= NULL
;
741 DPRINTF("host_init\n");
743 b
= pci_register_root_bus(dev
, NULL
, s390_pci_set_irq
, s390_pci_map_irq
,
744 NULL
, get_system_memory(), get_system_io(), 0,
746 pci_setup_iommu(b
, s390_pci_dma_iommu
, s
);
749 qbus_set_hotplug_handler(bus
, OBJECT(dev
), &local_err
);
751 error_propagate(errp
, local_err
);
756 s
->bus
= S390_PCI_BUS(qbus_create(TYPE_S390_PCI_BUS
, dev
, NULL
));
757 qbus_set_hotplug_handler(BUS(s
->bus
), OBJECT(dev
), &local_err
);
759 error_propagate(errp
, local_err
);
763 s
->iommu_table
= g_hash_table_new_full(g_int64_hash
, g_int64_equal
,
765 s
->zpci_table
= g_hash_table_new_full(g_int_hash
, g_int_equal
, NULL
, NULL
);
767 QTAILQ_INIT(&s
->pending_sei
);
768 QTAILQ_INIT(&s
->zpci_devs
);
770 css_register_io_adapters(CSS_IO_ADAPTER_PCI
, true, false,
771 S390_ADAPTER_SUPPRESSIBLE
, &local_err
);
772 error_propagate(errp
, local_err
);
775 static int s390_pci_msix_init(S390PCIBusDevice
*pbdev
)
782 pos
= pci_find_capability(pbdev
->pdev
, PCI_CAP_ID_MSIX
);
787 ctrl
= pci_host_config_read_common(pbdev
->pdev
, pos
+ PCI_MSIX_FLAGS
,
788 pci_config_size(pbdev
->pdev
), sizeof(ctrl
));
789 table
= pci_host_config_read_common(pbdev
->pdev
, pos
+ PCI_MSIX_TABLE
,
790 pci_config_size(pbdev
->pdev
), sizeof(table
));
791 pba
= pci_host_config_read_common(pbdev
->pdev
, pos
+ PCI_MSIX_PBA
,
792 pci_config_size(pbdev
->pdev
), sizeof(pba
));
794 pbdev
->msix
.table_bar
= table
& PCI_MSIX_FLAGS_BIRMASK
;
795 pbdev
->msix
.table_offset
= table
& ~PCI_MSIX_FLAGS_BIRMASK
;
796 pbdev
->msix
.pba_bar
= pba
& PCI_MSIX_FLAGS_BIRMASK
;
797 pbdev
->msix
.pba_offset
= pba
& ~PCI_MSIX_FLAGS_BIRMASK
;
798 pbdev
->msix
.entries
= (ctrl
& PCI_MSIX_FLAGS_QSIZE
) + 1;
800 name
= g_strdup_printf("msix-s390-%04x", pbdev
->uid
);
801 memory_region_init_io(&pbdev
->msix_notify_mr
, OBJECT(pbdev
),
802 &s390_msi_ctrl_ops
, pbdev
, name
, PAGE_SIZE
);
803 memory_region_add_subregion(&pbdev
->iommu
->mr
, ZPCI_MSI_ADDR
,
804 &pbdev
->msix_notify_mr
);
810 static void s390_pci_msix_free(S390PCIBusDevice
*pbdev
)
812 memory_region_del_subregion(&pbdev
->iommu
->mr
, &pbdev
->msix_notify_mr
);
813 object_unparent(OBJECT(&pbdev
->msix_notify_mr
));
816 static S390PCIBusDevice
*s390_pci_device_new(S390pciState
*s
,
817 const char *target
, Error
**errp
)
819 Error
*local_err
= NULL
;
822 dev
= qdev_try_create(BUS(s
->bus
), TYPE_S390_PCI_DEVICE
);
824 error_setg(errp
, "zPCI device could not be created");
828 object_property_set_str(OBJECT(dev
), target
, "target", &local_err
);
830 object_unparent(OBJECT(dev
));
831 error_propagate_prepend(errp
, local_err
,
832 "zPCI device could not be created: ");
835 object_property_set_bool(OBJECT(dev
), true, "realized", &local_err
);
837 object_unparent(OBJECT(dev
));
838 error_propagate_prepend(errp
, local_err
,
839 "zPCI device could not be created: ");
843 return S390_PCI_DEVICE(dev
);
846 static bool s390_pci_alloc_idx(S390pciState
*s
, S390PCIBusDevice
*pbdev
)
851 while (s390_pci_find_dev_by_idx(s
, idx
)) {
852 idx
= (idx
+ 1) & FH_MASK_INDEX
;
853 if (idx
== s
->next_idx
) {
862 static void s390_pcihost_pre_plug(HotplugHandler
*hotplug_dev
, DeviceState
*dev
,
865 S390pciState
*s
= S390_PCI_HOST_BRIDGE(hotplug_dev
);
867 if (!s390_has_feat(S390_FEAT_ZPCI
)) {
868 warn_report("Plugging a PCI/zPCI device without the 'zpci' CPU "
869 "feature enabled; the guest will not be able to see/use "
873 if (object_dynamic_cast(OBJECT(dev
), TYPE_PCI_DEVICE
)) {
874 PCIDevice
*pdev
= PCI_DEVICE(dev
);
876 if (pdev
->cap_present
& QEMU_PCI_CAP_MULTIFUNCTION
) {
877 error_setg(errp
, "multifunction not supported in s390");
880 } else if (object_dynamic_cast(OBJECT(dev
), TYPE_S390_PCI_DEVICE
)) {
881 S390PCIBusDevice
*pbdev
= S390_PCI_DEVICE(dev
);
883 if (!s390_pci_alloc_idx(s
, pbdev
)) {
884 error_setg(errp
, "no slot for plugging zpci device");
890 static void s390_pci_update_subordinate(PCIDevice
*dev
, uint32_t nr
)
894 pci_default_write_config(dev
, PCI_SUBORDINATE_BUS
, nr
, 1);
895 while (!pci_bus_is_root(pci_get_bus(dev
))) {
896 dev
= pci_get_bus(dev
)->parent_dev
;
898 old_nr
= pci_default_read_config(dev
, PCI_SUBORDINATE_BUS
, 1);
900 pci_default_write_config(dev
, PCI_SUBORDINATE_BUS
, nr
, 1);
905 static void s390_pcihost_plug(HotplugHandler
*hotplug_dev
, DeviceState
*dev
,
908 S390pciState
*s
= S390_PCI_HOST_BRIDGE(hotplug_dev
);
909 PCIDevice
*pdev
= NULL
;
910 S390PCIBusDevice
*pbdev
= NULL
;
912 if (object_dynamic_cast(OBJECT(dev
), TYPE_PCI_BRIDGE
)) {
913 PCIBridge
*pb
= PCI_BRIDGE(dev
);
915 pdev
= PCI_DEVICE(dev
);
916 pci_bridge_map_irq(pb
, dev
->id
, s390_pci_map_irq
);
917 pci_setup_iommu(&pb
->sec_bus
, s390_pci_dma_iommu
, s
);
919 qbus_set_hotplug_handler(BUS(&pb
->sec_bus
), OBJECT(s
), errp
);
921 if (dev
->hotplugged
) {
922 pci_default_write_config(pdev
, PCI_PRIMARY_BUS
,
923 pci_dev_bus_num(pdev
), 1);
925 pci_default_write_config(pdev
, PCI_SECONDARY_BUS
, s
->bus_no
, 1);
927 s390_pci_update_subordinate(pdev
, s
->bus_no
);
929 } else if (object_dynamic_cast(OBJECT(dev
), TYPE_PCI_DEVICE
)) {
930 pdev
= PCI_DEVICE(dev
);
933 /* In the case the PCI device does not define an id */
934 /* we generate one based on the PCI address */
935 dev
->id
= g_strdup_printf("auto_%02x:%02x.%01x",
936 pci_dev_bus_num(pdev
),
937 PCI_SLOT(pdev
->devfn
),
938 PCI_FUNC(pdev
->devfn
));
941 pbdev
= s390_pci_find_dev_by_target(s
, dev
->id
);
943 pbdev
= s390_pci_device_new(s
, dev
->id
, errp
);
949 if (object_dynamic_cast(OBJECT(dev
), "vfio-pci")) {
950 pbdev
->fh
|= FH_SHM_VFIO
;
952 pbdev
->fh
|= FH_SHM_EMUL
;
956 pbdev
->iommu
= s390_pci_get_iommu(s
, pci_get_bus(pdev
), pdev
->devfn
);
957 pbdev
->iommu
->pbdev
= pbdev
;
958 pbdev
->state
= ZPCI_FS_DISABLED
;
960 if (s390_pci_msix_init(pbdev
)) {
961 error_setg(errp
, "MSI-X support is mandatory "
962 "in the S390 architecture");
966 if (dev
->hotplugged
) {
967 s390_pci_generate_plug_event(HP_EVENT_TO_CONFIGURED
,
968 pbdev
->fh
, pbdev
->fid
);
970 } else if (object_dynamic_cast(OBJECT(dev
), TYPE_S390_PCI_DEVICE
)) {
971 pbdev
= S390_PCI_DEVICE(dev
);
973 /* the allocated idx is actually getting used */
974 s
->next_idx
= (pbdev
->idx
+ 1) & FH_MASK_INDEX
;
975 pbdev
->fh
= pbdev
->idx
;
976 QTAILQ_INSERT_TAIL(&s
->zpci_devs
, pbdev
, link
);
977 g_hash_table_insert(s
->zpci_table
, &pbdev
->idx
, pbdev
);
979 g_assert_not_reached();
983 static void s390_pcihost_unplug(HotplugHandler
*hotplug_dev
, DeviceState
*dev
,
986 S390pciState
*s
= S390_PCI_HOST_BRIDGE(hotplug_dev
);
987 S390PCIBusDevice
*pbdev
= NULL
;
989 if (object_dynamic_cast(OBJECT(dev
), TYPE_PCI_DEVICE
)) {
990 PCIDevice
*pci_dev
= PCI_DEVICE(dev
);
994 pbdev
= s390_pci_find_dev_by_pci(s
, PCI_DEVICE(dev
));
997 s390_pci_generate_plug_event(HP_EVENT_STANDBY_TO_RESERVED
,
998 pbdev
->fh
, pbdev
->fid
);
999 bus
= pci_get_bus(pci_dev
);
1000 devfn
= pci_dev
->devfn
;
1001 object_property_set_bool(OBJECT(dev
), false, "realized", NULL
);
1003 s390_pci_msix_free(pbdev
);
1004 s390_pci_iommu_free(s
, bus
, devfn
);
1006 pbdev
->state
= ZPCI_FS_RESERVED
;
1007 } else if (object_dynamic_cast(OBJECT(dev
), TYPE_S390_PCI_DEVICE
)) {
1008 pbdev
= S390_PCI_DEVICE(dev
);
1010 QTAILQ_REMOVE(&s
->zpci_devs
, pbdev
, link
);
1011 g_hash_table_remove(s
->zpci_table
, &pbdev
->idx
);
1012 object_property_set_bool(OBJECT(dev
), false, "realized", NULL
);
1016 static void s390_pcihost_unplug_request(HotplugHandler
*hotplug_dev
,
1020 S390pciState
*s
= S390_PCI_HOST_BRIDGE(hotplug_dev
);
1021 S390PCIBusDevice
*pbdev
;
1023 if (object_dynamic_cast(OBJECT(dev
), TYPE_PCI_BRIDGE
)) {
1024 error_setg(errp
, "PCI bridge hot unplug currently not supported");
1025 } else if (object_dynamic_cast(OBJECT(dev
), TYPE_PCI_DEVICE
)) {
1027 * Redirect the unplug request to the zPCI device and remember that
1028 * we've checked the PCI device already (to prevent endless recursion).
1030 pbdev
= s390_pci_find_dev_by_pci(s
, PCI_DEVICE(dev
));
1032 pbdev
->pci_unplug_request_processed
= true;
1033 qdev_unplug(DEVICE(pbdev
), errp
);
1034 } else if (object_dynamic_cast(OBJECT(dev
), TYPE_S390_PCI_DEVICE
)) {
1035 pbdev
= S390_PCI_DEVICE(dev
);
1038 * If unplug was initially requested for the zPCI device, we
1039 * first have to redirect to the PCI device, which will in return
1040 * redirect back to us after performing its checks (if the request
1041 * is not blocked, e.g. because it's a PCI bridge).
1043 if (pbdev
->pdev
&& !pbdev
->pci_unplug_request_processed
) {
1044 qdev_unplug(DEVICE(pbdev
->pdev
), errp
);
1047 pbdev
->pci_unplug_request_processed
= false;
1049 switch (pbdev
->state
) {
1050 case ZPCI_FS_STANDBY
:
1051 case ZPCI_FS_RESERVED
:
1052 s390_pci_perform_unplug(pbdev
);
1056 * Allow to send multiple requests, e.g. if the guest crashed
1057 * before releasing the device, we would not be able to send
1058 * another request to the same VM (e.g. fresh OS).
1060 pbdev
->unplug_requested
= true;
1061 s390_pci_generate_plug_event(HP_EVENT_DECONFIGURE_REQUEST
,
1062 pbdev
->fh
, pbdev
->fid
);
1065 g_assert_not_reached();
1069 static void s390_pci_enumerate_bridge(PCIBus
*bus
, PCIDevice
*pdev
,
1072 S390pciState
*s
= opaque
;
1073 PCIBus
*sec_bus
= NULL
;
1075 if ((pci_default_read_config(pdev
, PCI_HEADER_TYPE
, 1) !=
1076 PCI_HEADER_TYPE_BRIDGE
)) {
1081 pci_default_write_config(pdev
, PCI_PRIMARY_BUS
, pci_dev_bus_num(pdev
), 1);
1082 pci_default_write_config(pdev
, PCI_SECONDARY_BUS
, s
->bus_no
, 1);
1083 pci_default_write_config(pdev
, PCI_SUBORDINATE_BUS
, s
->bus_no
, 1);
1085 sec_bus
= pci_bridge_get_sec_bus(PCI_BRIDGE(pdev
));
1090 /* Assign numbers to all child bridges. The last is the highest number. */
1091 pci_for_each_device(sec_bus
, pci_bus_num(sec_bus
),
1092 s390_pci_enumerate_bridge
, s
);
1093 pci_default_write_config(pdev
, PCI_SUBORDINATE_BUS
, s
->bus_no
, 1);
1096 static void s390_pcihost_reset(DeviceState
*dev
)
1098 S390pciState
*s
= S390_PCI_HOST_BRIDGE(dev
);
1099 PCIBus
*bus
= s
->parent_obj
.bus
;
1100 S390PCIBusDevice
*pbdev
, *next
;
1102 /* Process all pending unplug requests */
1103 QTAILQ_FOREACH_SAFE(pbdev
, &s
->zpci_devs
, link
, next
) {
1104 if (pbdev
->unplug_requested
) {
1105 if (pbdev
->summary_ind
) {
1106 pci_dereg_irqs(pbdev
);
1108 if (pbdev
->iommu
->enabled
) {
1109 pci_dereg_ioat(pbdev
->iommu
);
1111 pbdev
->state
= ZPCI_FS_STANDBY
;
1112 s390_pci_perform_unplug(pbdev
);
1117 * When resetting a PCI bridge, the assigned numbers are set to 0. So
1118 * on every system reset, we also have to reassign numbers.
1121 pci_for_each_device(bus
, pci_bus_num(bus
), s390_pci_enumerate_bridge
, s
);
1124 static void s390_pcihost_class_init(ObjectClass
*klass
, void *data
)
1126 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1127 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
1129 dc
->reset
= s390_pcihost_reset
;
1130 dc
->realize
= s390_pcihost_realize
;
1131 hc
->pre_plug
= s390_pcihost_pre_plug
;
1132 hc
->plug
= s390_pcihost_plug
;
1133 hc
->unplug_request
= s390_pcihost_unplug_request
;
1134 hc
->unplug
= s390_pcihost_unplug
;
1135 msi_nonbroken
= true;
1138 static const TypeInfo s390_pcihost_info
= {
1139 .name
= TYPE_S390_PCI_HOST_BRIDGE
,
1140 .parent
= TYPE_PCI_HOST_BRIDGE
,
1141 .instance_size
= sizeof(S390pciState
),
1142 .class_init
= s390_pcihost_class_init
,
1143 .interfaces
= (InterfaceInfo
[]) {
1144 { TYPE_HOTPLUG_HANDLER
},
1149 static const TypeInfo s390_pcibus_info
= {
1150 .name
= TYPE_S390_PCI_BUS
,
1152 .instance_size
= sizeof(S390PCIBus
),
1155 static uint16_t s390_pci_generate_uid(S390pciState
*s
)
1161 if (!s390_pci_find_dev_by_uid(s
, uid
)) {
1164 } while (uid
< ZPCI_MAX_UID
);
1166 return UID_UNDEFINED
;
1169 static uint32_t s390_pci_generate_fid(S390pciState
*s
, Error
**errp
)
1174 if (!s390_pci_find_dev_by_fid(s
, fid
)) {
1177 } while (fid
++ != ZPCI_MAX_FID
);
1179 error_setg(errp
, "no free fid could be found");
1183 static void s390_pci_device_realize(DeviceState
*dev
, Error
**errp
)
1185 S390PCIBusDevice
*zpci
= S390_PCI_DEVICE(dev
);
1186 S390pciState
*s
= s390_get_phb();
1188 if (!zpci
->target
) {
1189 error_setg(errp
, "target must be defined");
1193 if (s390_pci_find_dev_by_target(s
, zpci
->target
)) {
1194 error_setg(errp
, "target %s already has an associated zpci device",
1199 if (zpci
->uid
== UID_UNDEFINED
) {
1200 zpci
->uid
= s390_pci_generate_uid(s
);
1202 error_setg(errp
, "no free uid could be found");
1205 } else if (s390_pci_find_dev_by_uid(s
, zpci
->uid
)) {
1206 error_setg(errp
, "uid %u already in use", zpci
->uid
);
1210 if (!zpci
->fid_defined
) {
1211 Error
*local_error
= NULL
;
1213 zpci
->fid
= s390_pci_generate_fid(s
, &local_error
);
1215 error_propagate(errp
, local_error
);
1218 } else if (s390_pci_find_dev_by_fid(s
, zpci
->fid
)) {
1219 error_setg(errp
, "fid %u already in use", zpci
->fid
);
1223 zpci
->state
= ZPCI_FS_RESERVED
;
1224 zpci
->fmb
.format
= ZPCI_FMB_FORMAT
;
1227 static void s390_pci_device_reset(DeviceState
*dev
)
1229 S390PCIBusDevice
*pbdev
= S390_PCI_DEVICE(dev
);
1231 switch (pbdev
->state
) {
1232 case ZPCI_FS_RESERVED
:
1234 case ZPCI_FS_STANDBY
:
1237 pbdev
->fh
&= ~FH_MASK_ENABLE
;
1238 pbdev
->state
= ZPCI_FS_DISABLED
;
1242 if (pbdev
->summary_ind
) {
1243 pci_dereg_irqs(pbdev
);
1245 if (pbdev
->iommu
->enabled
) {
1246 pci_dereg_ioat(pbdev
->iommu
);
1249 fmb_timer_free(pbdev
);
1252 static void s390_pci_get_fid(Object
*obj
, Visitor
*v
, const char *name
,
1253 void *opaque
, Error
**errp
)
1255 Property
*prop
= opaque
;
1256 uint32_t *ptr
= qdev_get_prop_ptr(DEVICE(obj
), prop
);
1258 visit_type_uint32(v
, name
, ptr
, errp
);
1261 static void s390_pci_set_fid(Object
*obj
, Visitor
*v
, const char *name
,
1262 void *opaque
, Error
**errp
)
1264 DeviceState
*dev
= DEVICE(obj
);
1265 S390PCIBusDevice
*zpci
= S390_PCI_DEVICE(obj
);
1266 Property
*prop
= opaque
;
1267 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
1269 if (dev
->realized
) {
1270 qdev_prop_set_after_realize(dev
, name
, errp
);
1274 visit_type_uint32(v
, name
, ptr
, errp
);
1275 zpci
->fid_defined
= true;
1278 static const PropertyInfo s390_pci_fid_propinfo
= {
1280 .get
= s390_pci_get_fid
,
1281 .set
= s390_pci_set_fid
,
1284 #define DEFINE_PROP_S390_PCI_FID(_n, _s, _f) \
1285 DEFINE_PROP(_n, _s, _f, s390_pci_fid_propinfo, uint32_t)
1287 static Property s390_pci_device_properties
[] = {
1288 DEFINE_PROP_UINT16("uid", S390PCIBusDevice
, uid
, UID_UNDEFINED
),
1289 DEFINE_PROP_S390_PCI_FID("fid", S390PCIBusDevice
, fid
),
1290 DEFINE_PROP_STRING("target", S390PCIBusDevice
, target
),
1291 DEFINE_PROP_END_OF_LIST(),
1294 static const VMStateDescription s390_pci_device_vmstate
= {
1295 .name
= TYPE_S390_PCI_DEVICE
,
1297 * TODO: add state handling here, so migration works at least with
1298 * emulated pci devices on s390x
1303 static void s390_pci_device_class_init(ObjectClass
*klass
, void *data
)
1305 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1307 dc
->desc
= "zpci device";
1308 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1309 dc
->reset
= s390_pci_device_reset
;
1310 dc
->bus_type
= TYPE_S390_PCI_BUS
;
1311 dc
->realize
= s390_pci_device_realize
;
1312 dc
->props
= s390_pci_device_properties
;
1313 dc
->vmsd
= &s390_pci_device_vmstate
;
1316 static const TypeInfo s390_pci_device_info
= {
1317 .name
= TYPE_S390_PCI_DEVICE
,
1318 .parent
= TYPE_DEVICE
,
1319 .instance_size
= sizeof(S390PCIBusDevice
),
1320 .class_init
= s390_pci_device_class_init
,
1323 static TypeInfo s390_pci_iommu_info
= {
1324 .name
= TYPE_S390_PCI_IOMMU
,
1325 .parent
= TYPE_OBJECT
,
1326 .instance_size
= sizeof(S390PCIIOMMU
),
1329 static void s390_iommu_memory_region_class_init(ObjectClass
*klass
, void *data
)
1331 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_CLASS(klass
);
1333 imrc
->translate
= s390_translate_iommu
;
1334 imrc
->replay
= s390_pci_iommu_replay
;
1337 static const TypeInfo s390_iommu_memory_region_info
= {
1338 .parent
= TYPE_IOMMU_MEMORY_REGION
,
1339 .name
= TYPE_S390_IOMMU_MEMORY_REGION
,
1340 .class_init
= s390_iommu_memory_region_class_init
,
1343 static void s390_pci_register_types(void)
1345 type_register_static(&s390_pcihost_info
);
1346 type_register_static(&s390_pcibus_info
);
1347 type_register_static(&s390_pci_device_info
);
1348 type_register_static(&s390_pci_iommu_info
);
1349 type_register_static(&s390_iommu_memory_region_info
);
1352 type_init(s390_pci_register_types
)