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"
17 #include "qemu-common.h"
19 #include "s390-pci-bus.h"
20 #include "s390-pci-inst.h"
21 #include "hw/pci/pci_bus.h"
22 #include "hw/pci/pci_bridge.h"
23 #include "hw/pci/msi.h"
24 #include "qemu/error-report.h"
26 #ifndef DEBUG_S390PCI_BUS
27 #define DEBUG_S390PCI_BUS 0
30 #define DPRINTF(fmt, ...) \
32 if (DEBUG_S390PCI_BUS) { \
33 fprintf(stderr, "S390pci-bus: " fmt, ## __VA_ARGS__); \
37 S390pciState
*s390_get_phb(void)
39 static S390pciState
*phb
;
42 phb
= S390_PCI_HOST_BRIDGE(
43 object_resolve_path(TYPE_S390_PCI_HOST_BRIDGE
, NULL
));
50 int pci_chsc_sei_nt2_get_event(void *res
)
52 ChscSeiNt2Res
*nt2_res
= (ChscSeiNt2Res
*)res
;
56 SeiContainer
*sei_cont
;
57 S390pciState
*s
= s390_get_phb();
59 sei_cont
= QTAILQ_FIRST(&s
->pending_sei
);
61 QTAILQ_REMOVE(&s
->pending_sei
, sei_cont
, link
);
63 nt2_res
->cc
= sei_cont
->cc
;
64 nt2_res
->length
= cpu_to_be16(sizeof(ChscSeiNt2Res
));
65 switch (sei_cont
->cc
) {
66 case 1: /* error event */
67 eccdf
= (PciCcdfErr
*)nt2_res
->ccdf
;
68 eccdf
->fid
= cpu_to_be32(sei_cont
->fid
);
69 eccdf
->fh
= cpu_to_be32(sei_cont
->fh
);
70 eccdf
->e
= cpu_to_be32(sei_cont
->e
);
71 eccdf
->faddr
= cpu_to_be64(sei_cont
->faddr
);
72 eccdf
->pec
= cpu_to_be16(sei_cont
->pec
);
74 case 2: /* availability event */
75 accdf
= (PciCcdfAvail
*)nt2_res
->ccdf
;
76 accdf
->fid
= cpu_to_be32(sei_cont
->fid
);
77 accdf
->fh
= cpu_to_be32(sei_cont
->fh
);
78 accdf
->pec
= cpu_to_be16(sei_cont
->pec
);
90 int pci_chsc_sei_nt2_have_event(void)
92 S390pciState
*s
= s390_get_phb();
94 return !QTAILQ_EMPTY(&s
->pending_sei
);
97 S390PCIBusDevice
*s390_pci_find_next_avail_dev(S390pciState
*s
,
98 S390PCIBusDevice
*pbdev
)
100 S390PCIBusDevice
*ret
= pbdev
? QTAILQ_NEXT(pbdev
, link
) :
101 QTAILQ_FIRST(&s
->zpci_devs
);
103 while (ret
&& ret
->state
== ZPCI_FS_RESERVED
) {
104 ret
= QTAILQ_NEXT(ret
, link
);
110 S390PCIBusDevice
*s390_pci_find_dev_by_fid(S390pciState
*s
, uint32_t fid
)
112 S390PCIBusDevice
*pbdev
;
114 QTAILQ_FOREACH(pbdev
, &s
->zpci_devs
, link
) {
115 if (pbdev
->fid
== fid
) {
123 void s390_pci_sclp_configure(SCCB
*sccb
)
125 IoaCfgSccb
*psccb
= (IoaCfgSccb
*)sccb
;
126 S390PCIBusDevice
*pbdev
= s390_pci_find_dev_by_fid(s390_get_phb(),
127 be32_to_cpu(psccb
->aid
));
131 DPRINTF("sclp config no dev found\n");
132 rc
= SCLP_RC_ADAPTER_ID_NOT_RECOGNIZED
;
136 switch (pbdev
->state
) {
137 case ZPCI_FS_RESERVED
:
138 rc
= SCLP_RC_ADAPTER_IN_RESERVED_STATE
;
140 case ZPCI_FS_STANDBY
:
141 pbdev
->state
= ZPCI_FS_DISABLED
;
142 rc
= SCLP_RC_NORMAL_COMPLETION
;
145 rc
= SCLP_RC_NO_ACTION_REQUIRED
;
148 psccb
->header
.response_code
= cpu_to_be16(rc
);
151 static void s390_pci_perform_unplug(S390PCIBusDevice
*pbdev
)
153 HotplugHandler
*hotplug_ctrl
;
155 /* Unplug the PCI device */
157 hotplug_ctrl
= qdev_get_hotplug_handler(DEVICE(pbdev
->pdev
));
158 hotplug_handler_unplug(hotplug_ctrl
, DEVICE(pbdev
->pdev
),
162 /* Unplug the zPCI device */
163 hotplug_ctrl
= qdev_get_hotplug_handler(DEVICE(pbdev
));
164 hotplug_handler_unplug(hotplug_ctrl
, DEVICE(pbdev
), &error_abort
);
167 void s390_pci_sclp_deconfigure(SCCB
*sccb
)
169 IoaCfgSccb
*psccb
= (IoaCfgSccb
*)sccb
;
170 S390PCIBusDevice
*pbdev
= s390_pci_find_dev_by_fid(s390_get_phb(),
171 be32_to_cpu(psccb
->aid
));
175 DPRINTF("sclp deconfig no dev found\n");
176 rc
= SCLP_RC_ADAPTER_ID_NOT_RECOGNIZED
;
180 switch (pbdev
->state
) {
181 case ZPCI_FS_RESERVED
:
182 rc
= SCLP_RC_ADAPTER_IN_RESERVED_STATE
;
184 case ZPCI_FS_STANDBY
:
185 rc
= SCLP_RC_NO_ACTION_REQUIRED
;
188 if (pbdev
->summary_ind
) {
189 pci_dereg_irqs(pbdev
);
191 if (pbdev
->iommu
->enabled
) {
192 pci_dereg_ioat(pbdev
->iommu
);
194 pbdev
->state
= ZPCI_FS_STANDBY
;
195 rc
= SCLP_RC_NORMAL_COMPLETION
;
197 if (pbdev
->unplug_requested
) {
198 s390_pci_perform_unplug(pbdev
);
202 psccb
->header
.response_code
= cpu_to_be16(rc
);
205 static S390PCIBusDevice
*s390_pci_find_dev_by_uid(S390pciState
*s
, uint16_t uid
)
207 S390PCIBusDevice
*pbdev
;
209 QTAILQ_FOREACH(pbdev
, &s
->zpci_devs
, link
) {
210 if (pbdev
->uid
== uid
) {
218 S390PCIBusDevice
*s390_pci_find_dev_by_target(S390pciState
*s
,
221 S390PCIBusDevice
*pbdev
;
227 QTAILQ_FOREACH(pbdev
, &s
->zpci_devs
, link
) {
228 if (!strcmp(pbdev
->target
, target
)) {
236 static S390PCIBusDevice
*s390_pci_find_dev_by_pci(S390pciState
*s
,
239 S390PCIBusDevice
*pbdev
;
245 QTAILQ_FOREACH(pbdev
, &s
->zpci_devs
, link
) {
246 if (pbdev
->pdev
== pci_dev
) {
254 S390PCIBusDevice
*s390_pci_find_dev_by_idx(S390pciState
*s
, uint32_t idx
)
256 return g_hash_table_lookup(s
->zpci_table
, &idx
);
259 S390PCIBusDevice
*s390_pci_find_dev_by_fh(S390pciState
*s
, uint32_t fh
)
261 uint32_t idx
= FH_MASK_INDEX
& fh
;
262 S390PCIBusDevice
*pbdev
= s390_pci_find_dev_by_idx(s
, idx
);
264 if (pbdev
&& pbdev
->fh
== fh
) {
271 static void s390_pci_generate_event(uint8_t cc
, uint16_t pec
, uint32_t fh
,
272 uint32_t fid
, uint64_t faddr
, uint32_t e
)
274 SeiContainer
*sei_cont
;
275 S390pciState
*s
= s390_get_phb();
277 sei_cont
= g_new0(SeiContainer
, 1);
282 sei_cont
->faddr
= faddr
;
285 QTAILQ_INSERT_TAIL(&s
->pending_sei
, sei_cont
, link
);
286 css_generate_css_crws(0);
289 static void s390_pci_generate_plug_event(uint16_t pec
, uint32_t fh
,
292 s390_pci_generate_event(2, pec
, fh
, fid
, 0, 0);
295 void s390_pci_generate_error_event(uint16_t pec
, uint32_t fh
, uint32_t fid
,
296 uint64_t faddr
, uint32_t e
)
298 s390_pci_generate_event(1, pec
, fh
, fid
, faddr
, e
);
301 static void s390_pci_set_irq(void *opaque
, int irq
, int level
)
306 static int s390_pci_map_irq(PCIDevice
*pci_dev
, int irq_num
)
312 static uint64_t s390_pci_get_table_origin(uint64_t iota
)
314 return iota
& ~ZPCI_IOTA_RTTO_FLAG
;
317 static unsigned int calc_rtx(dma_addr_t ptr
)
319 return ((unsigned long) ptr
>> ZPCI_RT_SHIFT
) & ZPCI_INDEX_MASK
;
322 static unsigned int calc_sx(dma_addr_t ptr
)
324 return ((unsigned long) ptr
>> ZPCI_ST_SHIFT
) & ZPCI_INDEX_MASK
;
327 static unsigned int calc_px(dma_addr_t ptr
)
329 return ((unsigned long) ptr
>> PAGE_SHIFT
) & ZPCI_PT_MASK
;
332 static uint64_t get_rt_sto(uint64_t entry
)
334 return ((entry
& ZPCI_TABLE_TYPE_MASK
) == ZPCI_TABLE_TYPE_RTX
)
335 ? (entry
& ZPCI_RTE_ADDR_MASK
)
339 static uint64_t get_st_pto(uint64_t entry
)
341 return ((entry
& ZPCI_TABLE_TYPE_MASK
) == ZPCI_TABLE_TYPE_SX
)
342 ? (entry
& ZPCI_STE_ADDR_MASK
)
346 static bool rt_entry_isvalid(uint64_t entry
)
348 return (entry
& ZPCI_TABLE_VALID_MASK
) == ZPCI_TABLE_VALID
;
351 static bool pt_entry_isvalid(uint64_t entry
)
353 return (entry
& ZPCI_PTE_VALID_MASK
) == ZPCI_PTE_VALID
;
356 static bool entry_isprotected(uint64_t entry
)
358 return (entry
& ZPCI_TABLE_PROT_MASK
) == ZPCI_TABLE_PROTECTED
;
361 /* ett is expected table type, -1 page table, 0 segment table, 1 region table */
362 static uint64_t get_table_index(uint64_t iova
, int8_t ett
)
366 return calc_px(iova
);
368 return calc_sx(iova
);
370 return calc_rtx(iova
);
376 static bool entry_isvalid(uint64_t entry
, int8_t ett
)
380 return pt_entry_isvalid(entry
);
383 return rt_entry_isvalid(entry
);
389 /* Return true if address translation is done */
390 static bool translate_iscomplete(uint64_t entry
, int8_t ett
)
394 return (entry
& ZPCI_TABLE_FC
) ? true : false;
402 static uint64_t get_frame_size(int8_t ett
)
416 static uint64_t get_next_table_origin(uint64_t entry
, int8_t ett
)
420 return entry
& ZPCI_PTE_ADDR_MASK
;
422 return get_st_pto(entry
);
424 return get_rt_sto(entry
);
431 * table_translate: do translation within one table and return the following
434 * @entry: the entry being translated, the result is stored in this.
435 * @to: the address of table origin.
436 * @ett: expected table type, 1 region table, 0 segment table and -1 page table.
439 static uint64_t table_translate(S390IOTLBEntry
*entry
, uint64_t to
, int8_t ett
,
442 uint64_t tx
, te
, nto
= 0;
445 tx
= get_table_index(entry
->iova
, ett
);
446 te
= address_space_ldq(&address_space_memory
, to
+ tx
* sizeof(uint64_t),
447 MEMTXATTRS_UNSPECIFIED
, NULL
);
450 err
= ERR_EVENT_INVALTE
;
454 if (!entry_isvalid(te
, ett
)) {
455 entry
->perm
&= IOMMU_NONE
;
459 if (ett
== ZPCI_ETT_RT
&& ((te
& ZPCI_TABLE_LEN_RTX
) != ZPCI_TABLE_LEN_RTX
460 || te
& ZPCI_TABLE_OFFSET_MASK
)) {
461 err
= ERR_EVENT_INVALTL
;
465 nto
= get_next_table_origin(te
, ett
);
471 if (entry_isprotected(te
)) {
472 entry
->perm
&= IOMMU_RO
;
474 entry
->perm
&= IOMMU_RW
;
477 if (translate_iscomplete(te
, ett
)) {
480 entry
->translated_addr
= te
& ZPCI_PTE_ADDR_MASK
;
483 entry
->translated_addr
= (te
& ZPCI_SFAA_MASK
) |
484 (entry
->iova
& ~ZPCI_SFAA_MASK
);
491 entry
->perm
= IOMMU_NONE
;
494 entry
->len
= get_frame_size(ett
);
498 uint16_t s390_guest_io_table_walk(uint64_t g_iota
, hwaddr addr
,
499 S390IOTLBEntry
*entry
)
501 uint64_t to
= s390_pci_get_table_origin(g_iota
);
505 entry
->iova
= addr
& PAGE_MASK
;
506 entry
->translated_addr
= 0;
507 entry
->perm
= IOMMU_RW
;
509 if (entry_isprotected(g_iota
)) {
510 entry
->perm
&= IOMMU_RO
;
514 to
= table_translate(entry
, to
, ett
--, &error
);
520 static IOMMUTLBEntry
s390_translate_iommu(IOMMUMemoryRegion
*mr
, hwaddr addr
,
521 IOMMUAccessFlags flag
, int iommu_idx
)
523 S390PCIIOMMU
*iommu
= container_of(mr
, S390PCIIOMMU
, iommu_mr
);
524 S390IOTLBEntry
*entry
;
525 uint64_t iova
= addr
& PAGE_MASK
;
527 IOMMUTLBEntry ret
= {
528 .target_as
= &address_space_memory
,
530 .translated_addr
= 0,
531 .addr_mask
= ~(hwaddr
)0,
535 switch (iommu
->pbdev
->state
) {
536 case ZPCI_FS_ENABLED
:
537 case ZPCI_FS_BLOCKED
:
538 if (!iommu
->enabled
) {
546 DPRINTF("iommu trans addr 0x%" PRIx64
"\n", addr
);
548 if (addr
< iommu
->pba
|| addr
> iommu
->pal
) {
549 error
= ERR_EVENT_OORANGE
;
553 entry
= g_hash_table_lookup(iommu
->iotlb
, &iova
);
555 ret
.iova
= entry
->iova
;
556 ret
.translated_addr
= entry
->translated_addr
;
557 ret
.addr_mask
= entry
->len
- 1;
558 ret
.perm
= entry
->perm
;
561 ret
.addr_mask
= ~PAGE_MASK
;
562 ret
.perm
= IOMMU_NONE
;
565 if (flag
!= IOMMU_NONE
&& !(flag
& ret
.perm
)) {
566 error
= ERR_EVENT_TPROTE
;
570 iommu
->pbdev
->state
= ZPCI_FS_ERROR
;
571 s390_pci_generate_error_event(error
, iommu
->pbdev
->fh
,
572 iommu
->pbdev
->fid
, addr
, 0);
577 static void s390_pci_iommu_replay(IOMMUMemoryRegion
*iommu
,
578 IOMMUNotifier
*notifier
)
580 /* It's impossible to plug a pci device on s390x that already has iommu
581 * mappings which need to be replayed, that is due to the "one iommu per
582 * zpci device" construct. But when we support migration of vfio-pci
583 * devices in future, we need to revisit this.
588 static S390PCIIOMMU
*s390_pci_get_iommu(S390pciState
*s
, PCIBus
*bus
,
591 uint64_t key
= (uintptr_t)bus
;
592 S390PCIIOMMUTable
*table
= g_hash_table_lookup(s
->iommu_table
, &key
);
596 table
= g_new0(S390PCIIOMMUTable
, 1);
598 g_hash_table_insert(s
->iommu_table
, &table
->key
, table
);
601 iommu
= table
->iommu
[PCI_SLOT(devfn
)];
603 iommu
= S390_PCI_IOMMU(object_new(TYPE_S390_PCI_IOMMU
));
605 char *mr_name
= g_strdup_printf("iommu-root-%02x:%02x.%01x",
609 char *as_name
= g_strdup_printf("iommu-pci-%02x:%02x.%01x",
613 memory_region_init(&iommu
->mr
, OBJECT(iommu
), mr_name
, UINT64_MAX
);
614 address_space_init(&iommu
->as
, &iommu
->mr
, as_name
);
615 iommu
->iotlb
= g_hash_table_new_full(g_int64_hash
, g_int64_equal
,
617 table
->iommu
[PCI_SLOT(devfn
)] = iommu
;
626 static AddressSpace
*s390_pci_dma_iommu(PCIBus
*bus
, void *opaque
, int devfn
)
628 S390pciState
*s
= opaque
;
629 S390PCIIOMMU
*iommu
= s390_pci_get_iommu(s
, bus
, devfn
);
634 static uint8_t set_ind_atomic(uint64_t ind_loc
, uint8_t to_be_set
)
636 uint8_t ind_old
, ind_new
;
640 ind_addr
= cpu_physical_memory_map(ind_loc
, &len
, 1);
642 s390_pci_generate_error_event(ERR_EVENT_AIRERR
, 0, 0, 0, 0);
647 ind_new
= ind_old
| to_be_set
;
648 } while (atomic_cmpxchg(ind_addr
, ind_old
, ind_new
) != ind_old
);
649 cpu_physical_memory_unmap(ind_addr
, len
, 1, len
);
654 static void s390_msi_ctrl_write(void *opaque
, hwaddr addr
, uint64_t data
,
657 S390PCIBusDevice
*pbdev
= opaque
;
658 uint32_t vec
= data
& ZPCI_MSI_VEC_MASK
;
663 DPRINTF("write_msix data 0x%" PRIx64
" idx %d vec 0x%x\n", data
,
666 if (pbdev
->state
!= ZPCI_FS_ENABLED
) {
670 ind_bit
= pbdev
->routes
.adapter
.ind_offset
;
671 sum_bit
= pbdev
->routes
.adapter
.summary_offset
;
673 set_ind_atomic(pbdev
->routes
.adapter
.ind_addr
+ (ind_bit
+ vec
) / 8,
674 0x80 >> ((ind_bit
+ vec
) % 8));
675 if (!set_ind_atomic(pbdev
->routes
.adapter
.summary_addr
+ sum_bit
/ 8,
676 0x80 >> (sum_bit
% 8))) {
677 css_adapter_interrupt(CSS_IO_ADAPTER_PCI
, pbdev
->isc
);
681 static uint64_t s390_msi_ctrl_read(void *opaque
, hwaddr addr
, unsigned size
)
686 static const MemoryRegionOps s390_msi_ctrl_ops
= {
687 .write
= s390_msi_ctrl_write
,
688 .read
= s390_msi_ctrl_read
,
689 .endianness
= DEVICE_LITTLE_ENDIAN
,
692 void s390_pci_iommu_enable(S390PCIIOMMU
*iommu
)
694 char *name
= g_strdup_printf("iommu-s390-%04x", iommu
->pbdev
->uid
);
695 memory_region_init_iommu(&iommu
->iommu_mr
, sizeof(iommu
->iommu_mr
),
696 TYPE_S390_IOMMU_MEMORY_REGION
, OBJECT(&iommu
->mr
),
697 name
, iommu
->pal
- iommu
->pba
+ 1);
698 iommu
->enabled
= true;
699 memory_region_add_subregion(&iommu
->mr
, 0, MEMORY_REGION(&iommu
->iommu_mr
));
703 void s390_pci_iommu_disable(S390PCIIOMMU
*iommu
)
705 iommu
->enabled
= false;
706 g_hash_table_remove_all(iommu
->iotlb
);
707 memory_region_del_subregion(&iommu
->mr
, MEMORY_REGION(&iommu
->iommu_mr
));
708 object_unparent(OBJECT(&iommu
->iommu_mr
));
711 static void s390_pci_iommu_free(S390pciState
*s
, PCIBus
*bus
, int32_t devfn
)
713 uint64_t key
= (uintptr_t)bus
;
714 S390PCIIOMMUTable
*table
= g_hash_table_lookup(s
->iommu_table
, &key
);
715 S390PCIIOMMU
*iommu
= table
? table
->iommu
[PCI_SLOT(devfn
)] : NULL
;
717 if (!table
|| !iommu
) {
721 table
->iommu
[PCI_SLOT(devfn
)] = NULL
;
722 g_hash_table_destroy(iommu
->iotlb
);
723 address_space_destroy(&iommu
->as
);
724 object_unparent(OBJECT(&iommu
->mr
));
725 object_unparent(OBJECT(iommu
));
726 object_unref(OBJECT(iommu
));
729 static void s390_pcihost_realize(DeviceState
*dev
, Error
**errp
)
733 PCIHostState
*phb
= PCI_HOST_BRIDGE(dev
);
734 S390pciState
*s
= S390_PCI_HOST_BRIDGE(dev
);
735 Error
*local_err
= NULL
;
737 DPRINTF("host_init\n");
739 b
= pci_register_root_bus(dev
, NULL
, s390_pci_set_irq
, s390_pci_map_irq
,
740 NULL
, get_system_memory(), get_system_io(), 0,
742 pci_setup_iommu(b
, s390_pci_dma_iommu
, s
);
745 qbus_set_hotplug_handler(bus
, dev
, &local_err
);
747 error_propagate(errp
, local_err
);
752 s
->bus
= S390_PCI_BUS(qbus_create(TYPE_S390_PCI_BUS
, dev
, NULL
));
753 qbus_set_hotplug_handler(BUS(s
->bus
), dev
, &local_err
);
755 error_propagate(errp
, local_err
);
759 s
->iommu_table
= g_hash_table_new_full(g_int64_hash
, g_int64_equal
,
761 s
->zpci_table
= g_hash_table_new_full(g_int_hash
, g_int_equal
, NULL
, NULL
);
763 QTAILQ_INIT(&s
->pending_sei
);
764 QTAILQ_INIT(&s
->zpci_devs
);
766 css_register_io_adapters(CSS_IO_ADAPTER_PCI
, true, false,
767 S390_ADAPTER_SUPPRESSIBLE
, &local_err
);
768 error_propagate(errp
, local_err
);
771 static int s390_pci_msix_init(S390PCIBusDevice
*pbdev
)
778 pos
= pci_find_capability(pbdev
->pdev
, PCI_CAP_ID_MSIX
);
783 ctrl
= pci_host_config_read_common(pbdev
->pdev
, pos
+ PCI_MSIX_FLAGS
,
784 pci_config_size(pbdev
->pdev
), sizeof(ctrl
));
785 table
= pci_host_config_read_common(pbdev
->pdev
, pos
+ PCI_MSIX_TABLE
,
786 pci_config_size(pbdev
->pdev
), sizeof(table
));
787 pba
= pci_host_config_read_common(pbdev
->pdev
, pos
+ PCI_MSIX_PBA
,
788 pci_config_size(pbdev
->pdev
), sizeof(pba
));
790 pbdev
->msix
.table_bar
= table
& PCI_MSIX_FLAGS_BIRMASK
;
791 pbdev
->msix
.table_offset
= table
& ~PCI_MSIX_FLAGS_BIRMASK
;
792 pbdev
->msix
.pba_bar
= pba
& PCI_MSIX_FLAGS_BIRMASK
;
793 pbdev
->msix
.pba_offset
= pba
& ~PCI_MSIX_FLAGS_BIRMASK
;
794 pbdev
->msix
.entries
= (ctrl
& PCI_MSIX_FLAGS_QSIZE
) + 1;
796 name
= g_strdup_printf("msix-s390-%04x", pbdev
->uid
);
797 memory_region_init_io(&pbdev
->msix_notify_mr
, OBJECT(pbdev
),
798 &s390_msi_ctrl_ops
, pbdev
, name
, PAGE_SIZE
);
799 memory_region_add_subregion(&pbdev
->iommu
->mr
, ZPCI_MSI_ADDR
,
800 &pbdev
->msix_notify_mr
);
806 static void s390_pci_msix_free(S390PCIBusDevice
*pbdev
)
808 memory_region_del_subregion(&pbdev
->iommu
->mr
, &pbdev
->msix_notify_mr
);
809 object_unparent(OBJECT(&pbdev
->msix_notify_mr
));
812 static S390PCIBusDevice
*s390_pci_device_new(S390pciState
*s
,
813 const char *target
, Error
**errp
)
815 Error
*local_err
= NULL
;
818 dev
= qdev_try_create(BUS(s
->bus
), TYPE_S390_PCI_DEVICE
);
820 error_setg(errp
, "zPCI device could not be created");
824 object_property_set_str(OBJECT(dev
), target
, "target", &local_err
);
826 object_unparent(OBJECT(dev
));
827 error_propagate_prepend(errp
, local_err
,
828 "zPCI device could not be created: ");
831 object_property_set_bool(OBJECT(dev
), true, "realized", &local_err
);
833 object_unparent(OBJECT(dev
));
834 error_propagate_prepend(errp
, local_err
,
835 "zPCI device could not be created: ");
839 return S390_PCI_DEVICE(dev
);
842 static bool s390_pci_alloc_idx(S390pciState
*s
, S390PCIBusDevice
*pbdev
)
847 while (s390_pci_find_dev_by_idx(s
, idx
)) {
848 idx
= (idx
+ 1) & FH_MASK_INDEX
;
849 if (idx
== s
->next_idx
) {
858 static void s390_pcihost_pre_plug(HotplugHandler
*hotplug_dev
, DeviceState
*dev
,
861 S390pciState
*s
= S390_PCI_HOST_BRIDGE(hotplug_dev
);
863 if (!s390_has_feat(S390_FEAT_ZPCI
)) {
864 warn_report("Plugging a PCI/zPCI device without the 'zpci' CPU "
865 "feature enabled; the guest will not be able to see/use "
869 if (object_dynamic_cast(OBJECT(dev
), TYPE_PCI_DEVICE
)) {
870 PCIDevice
*pdev
= PCI_DEVICE(dev
);
872 if (pdev
->cap_present
& QEMU_PCI_CAP_MULTIFUNCTION
) {
873 error_setg(errp
, "multifunction not supported in s390");
876 } else if (object_dynamic_cast(OBJECT(dev
), TYPE_S390_PCI_DEVICE
)) {
877 S390PCIBusDevice
*pbdev
= S390_PCI_DEVICE(dev
);
879 if (!s390_pci_alloc_idx(s
, pbdev
)) {
880 error_setg(errp
, "no slot for plugging zpci device");
886 static void s390_pci_update_subordinate(PCIDevice
*dev
, uint32_t nr
)
890 pci_default_write_config(dev
, PCI_SUBORDINATE_BUS
, nr
, 1);
891 while (!pci_bus_is_root(pci_get_bus(dev
))) {
892 dev
= pci_get_bus(dev
)->parent_dev
;
894 old_nr
= pci_default_read_config(dev
, PCI_SUBORDINATE_BUS
, 1);
896 pci_default_write_config(dev
, PCI_SUBORDINATE_BUS
, nr
, 1);
901 static void s390_pcihost_plug(HotplugHandler
*hotplug_dev
, DeviceState
*dev
,
904 S390pciState
*s
= S390_PCI_HOST_BRIDGE(hotplug_dev
);
905 PCIDevice
*pdev
= NULL
;
906 S390PCIBusDevice
*pbdev
= NULL
;
908 if (object_dynamic_cast(OBJECT(dev
), TYPE_PCI_BRIDGE
)) {
909 PCIBridge
*pb
= PCI_BRIDGE(dev
);
911 pdev
= PCI_DEVICE(dev
);
912 pci_bridge_map_irq(pb
, dev
->id
, s390_pci_map_irq
);
913 pci_setup_iommu(&pb
->sec_bus
, s390_pci_dma_iommu
, s
);
915 qbus_set_hotplug_handler(BUS(&pb
->sec_bus
), DEVICE(s
), errp
);
917 if (dev
->hotplugged
) {
918 pci_default_write_config(pdev
, PCI_PRIMARY_BUS
,
919 pci_dev_bus_num(pdev
), 1);
921 pci_default_write_config(pdev
, PCI_SECONDARY_BUS
, s
->bus_no
, 1);
923 s390_pci_update_subordinate(pdev
, s
->bus_no
);
925 } else if (object_dynamic_cast(OBJECT(dev
), TYPE_PCI_DEVICE
)) {
926 pdev
= PCI_DEVICE(dev
);
929 /* In the case the PCI device does not define an id */
930 /* we generate one based on the PCI address */
931 dev
->id
= g_strdup_printf("auto_%02x:%02x.%01x",
932 pci_dev_bus_num(pdev
),
933 PCI_SLOT(pdev
->devfn
),
934 PCI_FUNC(pdev
->devfn
));
937 pbdev
= s390_pci_find_dev_by_target(s
, dev
->id
);
939 pbdev
= s390_pci_device_new(s
, dev
->id
, errp
);
945 if (object_dynamic_cast(OBJECT(dev
), "vfio-pci")) {
946 pbdev
->fh
|= FH_SHM_VFIO
;
948 pbdev
->fh
|= FH_SHM_EMUL
;
952 pbdev
->iommu
= s390_pci_get_iommu(s
, pci_get_bus(pdev
), pdev
->devfn
);
953 pbdev
->iommu
->pbdev
= pbdev
;
954 pbdev
->state
= ZPCI_FS_DISABLED
;
956 if (s390_pci_msix_init(pbdev
)) {
957 error_setg(errp
, "MSI-X support is mandatory "
958 "in the S390 architecture");
962 if (dev
->hotplugged
) {
963 s390_pci_generate_plug_event(HP_EVENT_TO_CONFIGURED
,
964 pbdev
->fh
, pbdev
->fid
);
966 } else if (object_dynamic_cast(OBJECT(dev
), TYPE_S390_PCI_DEVICE
)) {
967 pbdev
= S390_PCI_DEVICE(dev
);
969 /* the allocated idx is actually getting used */
970 s
->next_idx
= (pbdev
->idx
+ 1) & FH_MASK_INDEX
;
971 pbdev
->fh
= pbdev
->idx
;
972 QTAILQ_INSERT_TAIL(&s
->zpci_devs
, pbdev
, link
);
973 g_hash_table_insert(s
->zpci_table
, &pbdev
->idx
, pbdev
);
975 g_assert_not_reached();
979 static void s390_pcihost_unplug(HotplugHandler
*hotplug_dev
, DeviceState
*dev
,
982 S390pciState
*s
= S390_PCI_HOST_BRIDGE(hotplug_dev
);
983 S390PCIBusDevice
*pbdev
= NULL
;
985 if (object_dynamic_cast(OBJECT(dev
), TYPE_PCI_DEVICE
)) {
986 PCIDevice
*pci_dev
= PCI_DEVICE(dev
);
990 pbdev
= s390_pci_find_dev_by_pci(s
, PCI_DEVICE(dev
));
993 s390_pci_generate_plug_event(HP_EVENT_STANDBY_TO_RESERVED
,
994 pbdev
->fh
, pbdev
->fid
);
995 bus
= pci_get_bus(pci_dev
);
996 devfn
= pci_dev
->devfn
;
997 object_unparent(OBJECT(pci_dev
));
999 s390_pci_msix_free(pbdev
);
1000 s390_pci_iommu_free(s
, bus
, devfn
);
1002 pbdev
->state
= ZPCI_FS_RESERVED
;
1003 } else if (object_dynamic_cast(OBJECT(dev
), TYPE_S390_PCI_DEVICE
)) {
1004 pbdev
= S390_PCI_DEVICE(dev
);
1006 QTAILQ_REMOVE(&s
->zpci_devs
, pbdev
, link
);
1007 g_hash_table_remove(s
->zpci_table
, &pbdev
->idx
);
1008 object_unparent(OBJECT(pbdev
));
1012 static void s390_pcihost_unplug_request(HotplugHandler
*hotplug_dev
,
1016 S390pciState
*s
= S390_PCI_HOST_BRIDGE(hotplug_dev
);
1017 S390PCIBusDevice
*pbdev
;
1019 if (object_dynamic_cast(OBJECT(dev
), TYPE_PCI_BRIDGE
)) {
1020 error_setg(errp
, "PCI bridge hot unplug currently not supported");
1021 } else if (object_dynamic_cast(OBJECT(dev
), TYPE_PCI_DEVICE
)) {
1023 * Redirect the unplug request to the zPCI device and remember that
1024 * we've checked the PCI device already (to prevent endless recursion).
1026 pbdev
= s390_pci_find_dev_by_pci(s
, PCI_DEVICE(dev
));
1028 pbdev
->pci_unplug_request_processed
= true;
1029 qdev_unplug(DEVICE(pbdev
), errp
);
1030 } else if (object_dynamic_cast(OBJECT(dev
), TYPE_S390_PCI_DEVICE
)) {
1031 pbdev
= S390_PCI_DEVICE(dev
);
1034 * If unplug was initially requested for the zPCI device, we
1035 * first have to redirect to the PCI device, which will in return
1036 * redirect back to us after performing its checks (if the request
1037 * is not blocked, e.g. because it's a PCI bridge).
1039 if (pbdev
->pdev
&& !pbdev
->pci_unplug_request_processed
) {
1040 qdev_unplug(DEVICE(pbdev
->pdev
), errp
);
1043 pbdev
->pci_unplug_request_processed
= false;
1045 switch (pbdev
->state
) {
1046 case ZPCI_FS_STANDBY
:
1047 case ZPCI_FS_RESERVED
:
1048 s390_pci_perform_unplug(pbdev
);
1052 * Allow to send multiple requests, e.g. if the guest crashed
1053 * before releasing the device, we would not be able to send
1054 * another request to the same VM (e.g. fresh OS).
1056 pbdev
->unplug_requested
= true;
1057 s390_pci_generate_plug_event(HP_EVENT_DECONFIGURE_REQUEST
,
1058 pbdev
->fh
, pbdev
->fid
);
1061 g_assert_not_reached();
1065 static void s390_pci_enumerate_bridge(PCIBus
*bus
, PCIDevice
*pdev
,
1068 S390pciState
*s
= opaque
;
1069 PCIBus
*sec_bus
= NULL
;
1071 if ((pci_default_read_config(pdev
, PCI_HEADER_TYPE
, 1) !=
1072 PCI_HEADER_TYPE_BRIDGE
)) {
1077 pci_default_write_config(pdev
, PCI_PRIMARY_BUS
, pci_dev_bus_num(pdev
), 1);
1078 pci_default_write_config(pdev
, PCI_SECONDARY_BUS
, s
->bus_no
, 1);
1079 pci_default_write_config(pdev
, PCI_SUBORDINATE_BUS
, s
->bus_no
, 1);
1081 sec_bus
= pci_bridge_get_sec_bus(PCI_BRIDGE(pdev
));
1086 /* Assign numbers to all child bridges. The last is the highest number. */
1087 pci_for_each_device(sec_bus
, pci_bus_num(sec_bus
),
1088 s390_pci_enumerate_bridge
, s
);
1089 pci_default_write_config(pdev
, PCI_SUBORDINATE_BUS
, s
->bus_no
, 1);
1092 static void s390_pcihost_reset(DeviceState
*dev
)
1094 S390pciState
*s
= S390_PCI_HOST_BRIDGE(dev
);
1095 PCIBus
*bus
= s
->parent_obj
.bus
;
1096 S390PCIBusDevice
*pbdev
, *next
;
1098 /* Process all pending unplug requests */
1099 QTAILQ_FOREACH_SAFE(pbdev
, &s
->zpci_devs
, link
, next
) {
1100 if (pbdev
->unplug_requested
) {
1101 if (pbdev
->summary_ind
) {
1102 pci_dereg_irqs(pbdev
);
1104 if (pbdev
->iommu
->enabled
) {
1105 pci_dereg_ioat(pbdev
->iommu
);
1107 pbdev
->state
= ZPCI_FS_STANDBY
;
1108 s390_pci_perform_unplug(pbdev
);
1113 * When resetting a PCI bridge, the assigned numbers are set to 0. So
1114 * on every system reset, we also have to reassign numbers.
1117 pci_for_each_device(bus
, pci_bus_num(bus
), s390_pci_enumerate_bridge
, s
);
1120 static void s390_pcihost_class_init(ObjectClass
*klass
, void *data
)
1122 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1123 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
1125 dc
->reset
= s390_pcihost_reset
;
1126 dc
->realize
= s390_pcihost_realize
;
1127 hc
->pre_plug
= s390_pcihost_pre_plug
;
1128 hc
->plug
= s390_pcihost_plug
;
1129 hc
->unplug_request
= s390_pcihost_unplug_request
;
1130 hc
->unplug
= s390_pcihost_unplug
;
1131 msi_nonbroken
= true;
1134 static const TypeInfo s390_pcihost_info
= {
1135 .name
= TYPE_S390_PCI_HOST_BRIDGE
,
1136 .parent
= TYPE_PCI_HOST_BRIDGE
,
1137 .instance_size
= sizeof(S390pciState
),
1138 .class_init
= s390_pcihost_class_init
,
1139 .interfaces
= (InterfaceInfo
[]) {
1140 { TYPE_HOTPLUG_HANDLER
},
1145 static const TypeInfo s390_pcibus_info
= {
1146 .name
= TYPE_S390_PCI_BUS
,
1148 .instance_size
= sizeof(S390PCIBus
),
1151 static uint16_t s390_pci_generate_uid(S390pciState
*s
)
1157 if (!s390_pci_find_dev_by_uid(s
, uid
)) {
1160 } while (uid
< ZPCI_MAX_UID
);
1162 return UID_UNDEFINED
;
1165 static uint32_t s390_pci_generate_fid(S390pciState
*s
, Error
**errp
)
1170 if (!s390_pci_find_dev_by_fid(s
, fid
)) {
1173 } while (fid
++ != ZPCI_MAX_FID
);
1175 error_setg(errp
, "no free fid could be found");
1179 static void s390_pci_device_realize(DeviceState
*dev
, Error
**errp
)
1181 S390PCIBusDevice
*zpci
= S390_PCI_DEVICE(dev
);
1182 S390pciState
*s
= s390_get_phb();
1184 if (!zpci
->target
) {
1185 error_setg(errp
, "target must be defined");
1189 if (s390_pci_find_dev_by_target(s
, zpci
->target
)) {
1190 error_setg(errp
, "target %s already has an associated zpci device",
1195 if (zpci
->uid
== UID_UNDEFINED
) {
1196 zpci
->uid
= s390_pci_generate_uid(s
);
1198 error_setg(errp
, "no free uid could be found");
1201 } else if (s390_pci_find_dev_by_uid(s
, zpci
->uid
)) {
1202 error_setg(errp
, "uid %u already in use", zpci
->uid
);
1206 if (!zpci
->fid_defined
) {
1207 Error
*local_error
= NULL
;
1209 zpci
->fid
= s390_pci_generate_fid(s
, &local_error
);
1211 error_propagate(errp
, local_error
);
1214 } else if (s390_pci_find_dev_by_fid(s
, zpci
->fid
)) {
1215 error_setg(errp
, "fid %u already in use", zpci
->fid
);
1219 zpci
->state
= ZPCI_FS_RESERVED
;
1220 zpci
->fmb
.format
= ZPCI_FMB_FORMAT
;
1223 static void s390_pci_device_reset(DeviceState
*dev
)
1225 S390PCIBusDevice
*pbdev
= S390_PCI_DEVICE(dev
);
1227 switch (pbdev
->state
) {
1228 case ZPCI_FS_RESERVED
:
1230 case ZPCI_FS_STANDBY
:
1233 pbdev
->fh
&= ~FH_MASK_ENABLE
;
1234 pbdev
->state
= ZPCI_FS_DISABLED
;
1238 if (pbdev
->summary_ind
) {
1239 pci_dereg_irqs(pbdev
);
1241 if (pbdev
->iommu
->enabled
) {
1242 pci_dereg_ioat(pbdev
->iommu
);
1245 fmb_timer_free(pbdev
);
1248 static void s390_pci_get_fid(Object
*obj
, Visitor
*v
, const char *name
,
1249 void *opaque
, Error
**errp
)
1251 Property
*prop
= opaque
;
1252 uint32_t *ptr
= qdev_get_prop_ptr(DEVICE(obj
), prop
);
1254 visit_type_uint32(v
, name
, ptr
, errp
);
1257 static void s390_pci_set_fid(Object
*obj
, Visitor
*v
, const char *name
,
1258 void *opaque
, Error
**errp
)
1260 DeviceState
*dev
= DEVICE(obj
);
1261 S390PCIBusDevice
*zpci
= S390_PCI_DEVICE(obj
);
1262 Property
*prop
= opaque
;
1263 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
1265 if (dev
->realized
) {
1266 qdev_prop_set_after_realize(dev
, name
, errp
);
1270 visit_type_uint32(v
, name
, ptr
, errp
);
1271 zpci
->fid_defined
= true;
1274 static const PropertyInfo s390_pci_fid_propinfo
= {
1276 .get
= s390_pci_get_fid
,
1277 .set
= s390_pci_set_fid
,
1280 #define DEFINE_PROP_S390_PCI_FID(_n, _s, _f) \
1281 DEFINE_PROP(_n, _s, _f, s390_pci_fid_propinfo, uint32_t)
1283 static Property s390_pci_device_properties
[] = {
1284 DEFINE_PROP_UINT16("uid", S390PCIBusDevice
, uid
, UID_UNDEFINED
),
1285 DEFINE_PROP_S390_PCI_FID("fid", S390PCIBusDevice
, fid
),
1286 DEFINE_PROP_STRING("target", S390PCIBusDevice
, target
),
1287 DEFINE_PROP_END_OF_LIST(),
1290 static const VMStateDescription s390_pci_device_vmstate
= {
1291 .name
= TYPE_S390_PCI_DEVICE
,
1293 * TODO: add state handling here, so migration works at least with
1294 * emulated pci devices on s390x
1299 static void s390_pci_device_class_init(ObjectClass
*klass
, void *data
)
1301 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1303 dc
->desc
= "zpci device";
1304 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
1305 dc
->reset
= s390_pci_device_reset
;
1306 dc
->bus_type
= TYPE_S390_PCI_BUS
;
1307 dc
->realize
= s390_pci_device_realize
;
1308 dc
->props
= s390_pci_device_properties
;
1309 dc
->vmsd
= &s390_pci_device_vmstate
;
1312 static const TypeInfo s390_pci_device_info
= {
1313 .name
= TYPE_S390_PCI_DEVICE
,
1314 .parent
= TYPE_DEVICE
,
1315 .instance_size
= sizeof(S390PCIBusDevice
),
1316 .class_init
= s390_pci_device_class_init
,
1319 static TypeInfo s390_pci_iommu_info
= {
1320 .name
= TYPE_S390_PCI_IOMMU
,
1321 .parent
= TYPE_OBJECT
,
1322 .instance_size
= sizeof(S390PCIIOMMU
),
1325 static void s390_iommu_memory_region_class_init(ObjectClass
*klass
, void *data
)
1327 IOMMUMemoryRegionClass
*imrc
= IOMMU_MEMORY_REGION_CLASS(klass
);
1329 imrc
->translate
= s390_translate_iommu
;
1330 imrc
->replay
= s390_pci_iommu_replay
;
1333 static const TypeInfo s390_iommu_memory_region_info
= {
1334 .parent
= TYPE_IOMMU_MEMORY_REGION
,
1335 .name
= TYPE_S390_IOMMU_MEMORY_REGION
,
1336 .class_init
= s390_iommu_memory_region_class_init
,
1339 static void s390_pci_register_types(void)
1341 type_register_static(&s390_pcihost_info
);
1342 type_register_static(&s390_pcibus_info
);
1343 type_register_static(&s390_pci_device_info
);
1344 type_register_static(&s390_pci_iommu_info
);
1345 type_register_static(&s390_iommu_memory_region_info
);
1348 type_init(s390_pci_register_types
)