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/msi.h"
23 #include "qemu/error-report.h"
25 /* #define DEBUG_S390PCI_BUS */
26 #ifdef DEBUG_S390PCI_BUS
27 #define DPRINTF(fmt, ...) \
28 do { fprintf(stderr, "S390pci-bus: " fmt, ## __VA_ARGS__); } while (0)
30 #define DPRINTF(fmt, ...) \
34 static S390pciState
*s390_get_phb(void)
36 static S390pciState
*phb
;
39 phb
= S390_PCI_HOST_BRIDGE(
40 object_resolve_path(TYPE_S390_PCI_HOST_BRIDGE
, NULL
));
47 int chsc_sei_nt2_get_event(void *res
)
49 ChscSeiNt2Res
*nt2_res
= (ChscSeiNt2Res
*)res
;
53 SeiContainer
*sei_cont
;
54 S390pciState
*s
= s390_get_phb();
56 sei_cont
= QTAILQ_FIRST(&s
->pending_sei
);
58 QTAILQ_REMOVE(&s
->pending_sei
, sei_cont
, link
);
60 nt2_res
->cc
= sei_cont
->cc
;
61 nt2_res
->length
= cpu_to_be16(sizeof(ChscSeiNt2Res
));
62 switch (sei_cont
->cc
) {
63 case 1: /* error event */
64 eccdf
= (PciCcdfErr
*)nt2_res
->ccdf
;
65 eccdf
->fid
= cpu_to_be32(sei_cont
->fid
);
66 eccdf
->fh
= cpu_to_be32(sei_cont
->fh
);
67 eccdf
->e
= cpu_to_be32(sei_cont
->e
);
68 eccdf
->faddr
= cpu_to_be64(sei_cont
->faddr
);
69 eccdf
->pec
= cpu_to_be16(sei_cont
->pec
);
71 case 2: /* availability event */
72 accdf
= (PciCcdfAvail
*)nt2_res
->ccdf
;
73 accdf
->fid
= cpu_to_be32(sei_cont
->fid
);
74 accdf
->fh
= cpu_to_be32(sei_cont
->fh
);
75 accdf
->pec
= cpu_to_be16(sei_cont
->pec
);
87 int chsc_sei_nt2_have_event(void)
89 S390pciState
*s
= s390_get_phb();
91 return !QTAILQ_EMPTY(&s
->pending_sei
);
94 S390PCIBusDevice
*s390_pci_find_next_avail_dev(S390PCIBusDevice
*pbdev
)
97 S390PCIBusDevice
*dev
= NULL
;
98 S390pciState
*s
= s390_get_phb();
101 idx
= (pbdev
->fh
& FH_MASK_INDEX
) + 1;
104 for (; idx
< PCI_SLOT_MAX
; idx
++) {
106 if (dev
&& dev
->state
!= ZPCI_FS_RESERVED
) {
114 S390PCIBusDevice
*s390_pci_find_dev_by_fid(uint32_t fid
)
116 S390PCIBusDevice
*pbdev
;
118 S390pciState
*s
= s390_get_phb();
120 for (i
= 0; i
< PCI_SLOT_MAX
; i
++) {
122 if (pbdev
&& pbdev
->fid
== fid
) {
130 void s390_pci_sclp_configure(SCCB
*sccb
)
132 PciCfgSccb
*psccb
= (PciCfgSccb
*)sccb
;
133 S390PCIBusDevice
*pbdev
= s390_pci_find_dev_by_fid(be32_to_cpu(psccb
->aid
));
136 if (be16_to_cpu(sccb
->h
.length
) < 16) {
137 rc
= SCLP_RC_INSUFFICIENT_SCCB_LENGTH
;
142 DPRINTF("sclp config no dev found\n");
143 rc
= SCLP_RC_ADAPTER_ID_NOT_RECOGNIZED
;
147 switch (pbdev
->state
) {
148 case ZPCI_FS_RESERVED
:
149 rc
= SCLP_RC_ADAPTER_IN_RESERVED_STATE
;
151 case ZPCI_FS_STANDBY
:
152 pbdev
->state
= ZPCI_FS_DISABLED
;
153 rc
= SCLP_RC_NORMAL_COMPLETION
;
156 rc
= SCLP_RC_NO_ACTION_REQUIRED
;
159 psccb
->header
.response_code
= cpu_to_be16(rc
);
162 void s390_pci_sclp_deconfigure(SCCB
*sccb
)
164 PciCfgSccb
*psccb
= (PciCfgSccb
*)sccb
;
165 S390PCIBusDevice
*pbdev
= s390_pci_find_dev_by_fid(be32_to_cpu(psccb
->aid
));
168 if (be16_to_cpu(sccb
->h
.length
) < 16) {
169 rc
= SCLP_RC_INSUFFICIENT_SCCB_LENGTH
;
174 DPRINTF("sclp deconfig no dev found\n");
175 rc
= SCLP_RC_ADAPTER_ID_NOT_RECOGNIZED
;
179 switch (pbdev
->state
) {
180 case ZPCI_FS_RESERVED
:
181 rc
= SCLP_RC_ADAPTER_IN_RESERVED_STATE
;
183 case ZPCI_FS_STANDBY
:
184 rc
= SCLP_RC_NO_ACTION_REQUIRED
;
187 if (pbdev
->summary_ind
) {
188 pci_dereg_irqs(pbdev
);
190 if (pbdev
->iommu_enabled
) {
191 pci_dereg_ioat(pbdev
);
193 pbdev
->state
= ZPCI_FS_STANDBY
;
194 rc
= SCLP_RC_NORMAL_COMPLETION
;
196 if (pbdev
->release_timer
) {
197 qdev_unplug(DEVICE(pbdev
->pdev
), NULL
);
201 psccb
->header
.response_code
= cpu_to_be16(rc
);
204 static S390PCIBusDevice
*s390_pci_find_dev_by_uid(uint16_t uid
)
207 S390PCIBusDevice
*pbdev
;
208 S390pciState
*s
= s390_get_phb();
210 for (i
= 0; i
< PCI_SLOT_MAX
; i
++) {
216 if (pbdev
->uid
== uid
) {
224 static S390PCIBusDevice
*s390_pci_find_dev_by_target(const char *target
)
227 S390PCIBusDevice
*pbdev
;
228 S390pciState
*s
= s390_get_phb();
234 for (i
= 0; i
< PCI_SLOT_MAX
; i
++) {
240 if (!strcmp(pbdev
->target
, target
)) {
248 S390PCIBusDevice
*s390_pci_find_dev_by_idx(uint32_t idx
)
250 S390pciState
*s
= s390_get_phb();
252 return s
->pbdev
[idx
& FH_MASK_INDEX
];
255 S390PCIBusDevice
*s390_pci_find_dev_by_fh(uint32_t fh
)
257 S390pciState
*s
= s390_get_phb();
258 S390PCIBusDevice
*pbdev
;
260 pbdev
= s
->pbdev
[fh
& FH_MASK_INDEX
];
261 if (pbdev
&& pbdev
->fh
== fh
) {
268 static void s390_pci_generate_event(uint8_t cc
, uint16_t pec
, uint32_t fh
,
269 uint32_t fid
, uint64_t faddr
, uint32_t e
)
271 SeiContainer
*sei_cont
;
272 S390pciState
*s
= s390_get_phb();
274 sei_cont
= g_malloc0(sizeof(SeiContainer
));
279 sei_cont
->faddr
= faddr
;
282 QTAILQ_INSERT_TAIL(&s
->pending_sei
, sei_cont
, link
);
283 css_generate_css_crws(0);
286 static void s390_pci_generate_plug_event(uint16_t pec
, uint32_t fh
,
289 s390_pci_generate_event(2, pec
, fh
, fid
, 0, 0);
292 void s390_pci_generate_error_event(uint16_t pec
, uint32_t fh
, uint32_t fid
,
293 uint64_t faddr
, uint32_t e
)
295 s390_pci_generate_event(1, pec
, fh
, fid
, faddr
, e
);
298 static void s390_pci_set_irq(void *opaque
, int irq
, int level
)
303 static int s390_pci_map_irq(PCIDevice
*pci_dev
, int irq_num
)
309 static uint64_t s390_pci_get_table_origin(uint64_t iota
)
311 return iota
& ~ZPCI_IOTA_RTTO_FLAG
;
314 static unsigned int calc_rtx(dma_addr_t ptr
)
316 return ((unsigned long) ptr
>> ZPCI_RT_SHIFT
) & ZPCI_INDEX_MASK
;
319 static unsigned int calc_sx(dma_addr_t ptr
)
321 return ((unsigned long) ptr
>> ZPCI_ST_SHIFT
) & ZPCI_INDEX_MASK
;
324 static unsigned int calc_px(dma_addr_t ptr
)
326 return ((unsigned long) ptr
>> PAGE_SHIFT
) & ZPCI_PT_MASK
;
329 static uint64_t get_rt_sto(uint64_t entry
)
331 return ((entry
& ZPCI_TABLE_TYPE_MASK
) == ZPCI_TABLE_TYPE_RTX
)
332 ? (entry
& ZPCI_RTE_ADDR_MASK
)
336 static uint64_t get_st_pto(uint64_t entry
)
338 return ((entry
& ZPCI_TABLE_TYPE_MASK
) == ZPCI_TABLE_TYPE_SX
)
339 ? (entry
& ZPCI_STE_ADDR_MASK
)
343 static uint64_t s390_guest_io_table_walk(uint64_t guest_iota
,
344 uint64_t guest_dma_address
)
346 uint64_t sto_a
, pto_a
, px_a
;
347 uint64_t sto
, pto
, pte
;
348 uint32_t rtx
, sx
, px
;
350 rtx
= calc_rtx(guest_dma_address
);
351 sx
= calc_sx(guest_dma_address
);
352 px
= calc_px(guest_dma_address
);
354 sto_a
= guest_iota
+ rtx
* sizeof(uint64_t);
355 sto
= address_space_ldq(&address_space_memory
, sto_a
,
356 MEMTXATTRS_UNSPECIFIED
, NULL
);
357 sto
= get_rt_sto(sto
);
363 pto_a
= sto
+ sx
* sizeof(uint64_t);
364 pto
= address_space_ldq(&address_space_memory
, pto_a
,
365 MEMTXATTRS_UNSPECIFIED
, NULL
);
366 pto
= get_st_pto(pto
);
372 px_a
= pto
+ px
* sizeof(uint64_t);
373 pte
= address_space_ldq(&address_space_memory
, px_a
,
374 MEMTXATTRS_UNSPECIFIED
, NULL
);
380 static IOMMUTLBEntry
s390_translate_iommu(MemoryRegion
*iommu
, hwaddr addr
,
385 S390PCIBusDevice
*pbdev
= container_of(iommu
, S390PCIBusDevice
, iommu_mr
);
386 IOMMUTLBEntry ret
= {
387 .target_as
= &address_space_memory
,
389 .translated_addr
= 0,
390 .addr_mask
= ~(hwaddr
)0,
394 switch (pbdev
->state
) {
395 case ZPCI_FS_ENABLED
:
396 case ZPCI_FS_BLOCKED
:
397 if (!pbdev
->iommu_enabled
) {
405 DPRINTF("iommu trans addr 0x%" PRIx64
"\n", addr
);
407 if (addr
< pbdev
->pba
|| addr
> pbdev
->pal
) {
411 pte
= s390_guest_io_table_walk(s390_pci_get_table_origin(pbdev
->g_iota
),
417 flags
= pte
& ZPCI_PTE_FLAG_MASK
;
419 ret
.translated_addr
= pte
& ZPCI_PTE_ADDR_MASK
;
420 ret
.addr_mask
= 0xfff;
422 if (flags
& ZPCI_PTE_INVALID
) {
423 ret
.perm
= IOMMU_NONE
;
431 static const MemoryRegionIOMMUOps s390_iommu_ops
= {
432 .translate
= s390_translate_iommu
,
435 static AddressSpace
*s390_pci_dma_iommu(PCIBus
*bus
, void *opaque
, int devfn
)
437 S390pciState
*s
= opaque
;
439 return &s
->iommu
[PCI_SLOT(devfn
)]->as
;
442 static uint8_t set_ind_atomic(uint64_t ind_loc
, uint8_t to_be_set
)
444 uint8_t ind_old
, ind_new
;
448 ind_addr
= cpu_physical_memory_map(ind_loc
, &len
, 1);
450 s390_pci_generate_error_event(ERR_EVENT_AIRERR
, 0, 0, 0, 0);
455 ind_new
= ind_old
| to_be_set
;
456 } while (atomic_cmpxchg(ind_addr
, ind_old
, ind_new
) != ind_old
);
457 cpu_physical_memory_unmap(ind_addr
, len
, 1, len
);
462 static void s390_msi_ctrl_write(void *opaque
, hwaddr addr
, uint64_t data
,
465 S390PCIBusDevice
*pbdev
= opaque
;
466 uint32_t idx
= data
>> ZPCI_MSI_VEC_BITS
;
467 uint32_t vec
= data
& ZPCI_MSI_VEC_MASK
;
472 DPRINTF("write_msix data 0x%" PRIx64
" idx %d vec 0x%x\n", data
, idx
, vec
);
475 e
|= (vec
<< ERR_EVENT_MVN_OFFSET
);
476 s390_pci_generate_error_event(ERR_EVENT_NOMSI
, idx
, 0, addr
, e
);
480 if (pbdev
->state
!= ZPCI_FS_ENABLED
) {
484 ind_bit
= pbdev
->routes
.adapter
.ind_offset
;
485 sum_bit
= pbdev
->routes
.adapter
.summary_offset
;
487 set_ind_atomic(pbdev
->routes
.adapter
.ind_addr
+ (ind_bit
+ vec
) / 8,
488 0x80 >> ((ind_bit
+ vec
) % 8));
489 if (!set_ind_atomic(pbdev
->routes
.adapter
.summary_addr
+ sum_bit
/ 8,
490 0x80 >> (sum_bit
% 8))) {
491 css_adapter_interrupt(pbdev
->isc
);
495 static uint64_t s390_msi_ctrl_read(void *opaque
, hwaddr addr
, unsigned size
)
500 static const MemoryRegionOps s390_msi_ctrl_ops
= {
501 .write
= s390_msi_ctrl_write
,
502 .read
= s390_msi_ctrl_read
,
503 .endianness
= DEVICE_LITTLE_ENDIAN
,
506 void s390_pci_iommu_enable(S390PCIBusDevice
*pbdev
)
508 memory_region_init_iommu(&pbdev
->iommu_mr
, OBJECT(&pbdev
->iommu
->mr
),
509 &s390_iommu_ops
, "iommu-s390", pbdev
->pal
+ 1);
510 memory_region_add_subregion(&pbdev
->iommu
->mr
, 0, &pbdev
->iommu_mr
);
511 pbdev
->iommu_enabled
= true;
514 void s390_pci_iommu_disable(S390PCIBusDevice
*pbdev
)
516 memory_region_del_subregion(&pbdev
->iommu
->mr
, &pbdev
->iommu_mr
);
517 object_unparent(OBJECT(&pbdev
->iommu_mr
));
518 pbdev
->iommu_enabled
= false;
521 static void s390_pcihost_init_as(S390pciState
*s
)
526 for (i
= 0; i
< PCI_SLOT_MAX
; i
++) {
527 iommu
= g_malloc0(sizeof(S390PCIIOMMU
));
528 memory_region_init(&iommu
->mr
, OBJECT(s
),
529 "iommu-root-s390", UINT64_MAX
);
530 address_space_init(&iommu
->as
, &iommu
->mr
, "iommu-pci");
536 static int s390_pcihost_init(SysBusDevice
*dev
)
540 PCIHostState
*phb
= PCI_HOST_BRIDGE(dev
);
541 S390pciState
*s
= S390_PCI_HOST_BRIDGE(dev
);
543 DPRINTF("host_init\n");
545 b
= pci_register_bus(DEVICE(dev
), NULL
,
546 s390_pci_set_irq
, s390_pci_map_irq
, NULL
,
547 get_system_memory(), get_system_io(), 0, 64,
549 s390_pcihost_init_as(s
);
550 pci_setup_iommu(b
, s390_pci_dma_iommu
, s
);
553 qbus_set_hotplug_handler(bus
, DEVICE(dev
), NULL
);
556 s
->bus
= S390_PCI_BUS(qbus_create(TYPE_S390_PCI_BUS
, DEVICE(s
), NULL
));
557 qbus_set_hotplug_handler(BUS(s
->bus
), DEVICE(s
), NULL
);
559 QTAILQ_INIT(&s
->pending_sei
);
563 static int s390_pci_setup_msix(S390PCIBusDevice
*pbdev
)
569 pos
= pci_find_capability(pbdev
->pdev
, PCI_CAP_ID_MSIX
);
571 pbdev
->msix
.available
= false;
575 ctrl
= pci_host_config_read_common(pbdev
->pdev
, pos
+ PCI_MSIX_FLAGS
,
576 pci_config_size(pbdev
->pdev
), sizeof(ctrl
));
577 table
= pci_host_config_read_common(pbdev
->pdev
, pos
+ PCI_MSIX_TABLE
,
578 pci_config_size(pbdev
->pdev
), sizeof(table
));
579 pba
= pci_host_config_read_common(pbdev
->pdev
, pos
+ PCI_MSIX_PBA
,
580 pci_config_size(pbdev
->pdev
), sizeof(pba
));
582 pbdev
->msix
.table_bar
= table
& PCI_MSIX_FLAGS_BIRMASK
;
583 pbdev
->msix
.table_offset
= table
& ~PCI_MSIX_FLAGS_BIRMASK
;
584 pbdev
->msix
.pba_bar
= pba
& PCI_MSIX_FLAGS_BIRMASK
;
585 pbdev
->msix
.pba_offset
= pba
& ~PCI_MSIX_FLAGS_BIRMASK
;
586 pbdev
->msix
.entries
= (ctrl
& PCI_MSIX_FLAGS_QSIZE
) + 1;
587 pbdev
->msix
.available
= true;
591 static void s390_pci_msix_init(S390PCIBusDevice
*pbdev
)
595 name
= g_strdup_printf("msix-s390-%04x", pbdev
->uid
);
597 memory_region_init_io(&pbdev
->msix_notify_mr
, OBJECT(pbdev
),
598 &s390_msi_ctrl_ops
, pbdev
, name
, PAGE_SIZE
);
599 memory_region_add_subregion(&pbdev
->iommu
->mr
, ZPCI_MSI_ADDR
,
600 &pbdev
->msix_notify_mr
);
605 static void s390_pci_msix_free(S390PCIBusDevice
*pbdev
)
607 memory_region_del_subregion(&pbdev
->iommu
->mr
, &pbdev
->msix_notify_mr
);
608 object_unparent(OBJECT(&pbdev
->msix_notify_mr
));
611 static S390PCIBusDevice
*s390_pci_device_new(const char *target
)
613 DeviceState
*dev
= NULL
;
614 S390pciState
*s
= s390_get_phb();
616 dev
= qdev_try_create(BUS(s
->bus
), TYPE_S390_PCI_DEVICE
);
621 qdev_prop_set_string(dev
, "target", target
);
622 qdev_init_nofail(dev
);
624 return S390_PCI_DEVICE(dev
);
627 static void s390_pcihost_hot_plug(HotplugHandler
*hotplug_dev
,
628 DeviceState
*dev
, Error
**errp
)
630 PCIDevice
*pdev
= NULL
;
631 S390PCIBusDevice
*pbdev
= NULL
;
632 S390pciState
*s
= s390_get_phb();
634 if (object_dynamic_cast(OBJECT(dev
), TYPE_PCI_DEVICE
)) {
635 pdev
= PCI_DEVICE(dev
);
638 /* In the case the PCI device does not define an id */
639 /* we generate one based on the PCI address */
640 dev
->id
= g_strdup_printf("auto_%02x:%02x.%01x",
641 pci_bus_num(pdev
->bus
),
642 PCI_SLOT(pdev
->devfn
),
643 PCI_FUNC(pdev
->devfn
));
646 pbdev
= s390_pci_find_dev_by_target(dev
->id
);
648 pbdev
= s390_pci_device_new(dev
->id
);
650 error_setg(errp
, "create zpci device failed");
655 if (object_dynamic_cast(OBJECT(dev
), "vfio-pci")) {
656 pbdev
->fh
|= FH_SHM_VFIO
;
658 pbdev
->fh
|= FH_SHM_EMUL
;
662 pbdev
->iommu
= s
->iommu
[PCI_SLOT(pdev
->devfn
)];
663 pbdev
->state
= ZPCI_FS_STANDBY
;
665 s390_pci_msix_init(pbdev
);
666 s390_pci_setup_msix(pbdev
);
668 if (dev
->hotplugged
) {
669 s390_pci_generate_plug_event(HP_EVENT_RESERVED_TO_STANDBY
,
670 pbdev
->fh
, pbdev
->fid
);
672 } else if (object_dynamic_cast(OBJECT(dev
), TYPE_S390_PCI_DEVICE
)) {
675 pbdev
= S390_PCI_DEVICE(dev
);
676 for (idx
= 0; idx
< PCI_SLOT_MAX
; idx
++) {
677 if (!s
->pbdev
[idx
]) {
678 s
->pbdev
[idx
] = pbdev
;
684 error_setg(errp
, "no slot for plugging zpci device");
688 static void s390_pcihost_timer_cb(void *opaque
)
690 S390PCIBusDevice
*pbdev
= opaque
;
692 if (pbdev
->summary_ind
) {
693 pci_dereg_irqs(pbdev
);
695 if (pbdev
->iommu_enabled
) {
696 pci_dereg_ioat(pbdev
);
699 pbdev
->state
= ZPCI_FS_STANDBY
;
700 s390_pci_generate_plug_event(HP_EVENT_CONFIGURED_TO_STBRES
,
701 pbdev
->fh
, pbdev
->fid
);
702 qdev_unplug(DEVICE(pbdev
), NULL
);
705 static void s390_pcihost_hot_unplug(HotplugHandler
*hotplug_dev
,
706 DeviceState
*dev
, Error
**errp
)
709 PCIDevice
*pci_dev
= NULL
;
710 S390PCIBusDevice
*pbdev
= NULL
;
711 S390pciState
*s
= s390_get_phb();
713 if (object_dynamic_cast(OBJECT(dev
), TYPE_PCI_DEVICE
)) {
714 pci_dev
= PCI_DEVICE(dev
);
716 for (i
= 0 ; i
< PCI_SLOT_MAX
; i
++) {
717 if (s
->pbdev
[i
] && s
->pbdev
[i
]->pdev
== pci_dev
) {
722 assert(pbdev
!= NULL
);
723 } else if (object_dynamic_cast(OBJECT(dev
), TYPE_S390_PCI_DEVICE
)) {
724 pbdev
= S390_PCI_DEVICE(dev
);
725 pci_dev
= pbdev
->pdev
;
728 switch (pbdev
->state
) {
729 case ZPCI_FS_RESERVED
:
731 case ZPCI_FS_STANDBY
:
734 s390_pci_generate_plug_event(HP_EVENT_DECONFIGURE_REQUEST
,
735 pbdev
->fh
, pbdev
->fid
);
736 pbdev
->release_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
737 s390_pcihost_timer_cb
,
739 timer_mod(pbdev
->release_timer
,
740 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + HOT_UNPLUG_TIMEOUT
);
744 if (pbdev
->release_timer
&& timer_pending(pbdev
->release_timer
)) {
745 timer_del(pbdev
->release_timer
);
746 timer_free(pbdev
->release_timer
);
747 pbdev
->release_timer
= NULL
;
750 s390_pci_generate_plug_event(HP_EVENT_STANDBY_TO_RESERVED
,
751 pbdev
->fh
, pbdev
->fid
);
752 object_unparent(OBJECT(pci_dev
));
753 s390_pci_msix_free(pbdev
);
755 pbdev
->state
= ZPCI_FS_RESERVED
;
758 s
->pbdev
[pbdev
->fh
& FH_MASK_INDEX
] = NULL
;
759 object_unparent(OBJECT(pbdev
));
762 static void s390_pcihost_class_init(ObjectClass
*klass
, void *data
)
764 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
765 DeviceClass
*dc
= DEVICE_CLASS(klass
);
766 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
768 dc
->cannot_instantiate_with_device_add_yet
= true;
769 k
->init
= s390_pcihost_init
;
770 hc
->plug
= s390_pcihost_hot_plug
;
771 hc
->unplug
= s390_pcihost_hot_unplug
;
772 msi_nonbroken
= true;
775 static const TypeInfo s390_pcihost_info
= {
776 .name
= TYPE_S390_PCI_HOST_BRIDGE
,
777 .parent
= TYPE_PCI_HOST_BRIDGE
,
778 .instance_size
= sizeof(S390pciState
),
779 .class_init
= s390_pcihost_class_init
,
780 .interfaces
= (InterfaceInfo
[]) {
781 { TYPE_HOTPLUG_HANDLER
},
786 static const TypeInfo s390_pcibus_info
= {
787 .name
= TYPE_S390_PCI_BUS
,
789 .instance_size
= sizeof(S390PCIBus
),
792 static uint16_t s390_pci_generate_uid(void)
798 if (!s390_pci_find_dev_by_uid(uid
)) {
801 } while (uid
< ZPCI_MAX_UID
);
803 return UID_UNDEFINED
;
806 static uint32_t s390_pci_generate_fid(Error
**errp
)
811 if (!s390_pci_find_dev_by_fid(fid
)) {
814 } while (fid
++ != ZPCI_MAX_FID
);
816 error_setg(errp
, "no free fid could be found");
820 static void s390_pci_device_realize(DeviceState
*dev
, Error
**errp
)
822 S390PCIBusDevice
*zpci
= S390_PCI_DEVICE(dev
);
825 error_setg(errp
, "target must be defined");
829 if (s390_pci_find_dev_by_target(zpci
->target
)) {
830 error_setg(errp
, "target %s already has an associated zpci device",
835 if (zpci
->uid
== UID_UNDEFINED
) {
836 zpci
->uid
= s390_pci_generate_uid();
838 error_setg(errp
, "no free uid could be found");
841 } else if (s390_pci_find_dev_by_uid(zpci
->uid
)) {
842 error_setg(errp
, "uid %u already in use", zpci
->uid
);
846 if (!zpci
->fid_defined
) {
847 Error
*local_error
= NULL
;
849 zpci
->fid
= s390_pci_generate_fid(&local_error
);
851 error_propagate(errp
, local_error
);
854 } else if (s390_pci_find_dev_by_fid(zpci
->fid
)) {
855 error_setg(errp
, "fid %u already in use", zpci
->fid
);
859 zpci
->state
= ZPCI_FS_RESERVED
;
862 static void s390_pci_device_reset(DeviceState
*dev
)
864 S390PCIBusDevice
*pbdev
= S390_PCI_DEVICE(dev
);
866 switch (pbdev
->state
) {
867 case ZPCI_FS_RESERVED
:
869 case ZPCI_FS_STANDBY
:
872 pbdev
->fh
&= ~FH_MASK_ENABLE
;
873 pbdev
->state
= ZPCI_FS_DISABLED
;
877 if (pbdev
->summary_ind
) {
878 pci_dereg_irqs(pbdev
);
880 if (pbdev
->iommu_enabled
) {
881 pci_dereg_ioat(pbdev
);
887 static void s390_pci_get_fid(Object
*obj
, Visitor
*v
, const char *name
,
888 void *opaque
, Error
**errp
)
890 Property
*prop
= opaque
;
891 uint32_t *ptr
= qdev_get_prop_ptr(DEVICE(obj
), prop
);
893 visit_type_uint32(v
, name
, ptr
, errp
);
896 static void s390_pci_set_fid(Object
*obj
, Visitor
*v
, const char *name
,
897 void *opaque
, Error
**errp
)
899 DeviceState
*dev
= DEVICE(obj
);
900 S390PCIBusDevice
*zpci
= S390_PCI_DEVICE(obj
);
901 Property
*prop
= opaque
;
902 uint32_t *ptr
= qdev_get_prop_ptr(dev
, prop
);
905 qdev_prop_set_after_realize(dev
, name
, errp
);
909 visit_type_uint32(v
, name
, ptr
, errp
);
910 zpci
->fid_defined
= true;
913 static PropertyInfo s390_pci_fid_propinfo
= {
915 .get
= s390_pci_get_fid
,
916 .set
= s390_pci_set_fid
,
919 #define DEFINE_PROP_S390_PCI_FID(_n, _s, _f) \
920 DEFINE_PROP(_n, _s, _f, s390_pci_fid_propinfo, uint32_t)
922 static Property s390_pci_device_properties
[] = {
923 DEFINE_PROP_UINT16("uid", S390PCIBusDevice
, uid
, UID_UNDEFINED
),
924 DEFINE_PROP_S390_PCI_FID("fid", S390PCIBusDevice
, fid
),
925 DEFINE_PROP_STRING("target", S390PCIBusDevice
, target
),
926 DEFINE_PROP_END_OF_LIST(),
929 static void s390_pci_device_class_init(ObjectClass
*klass
, void *data
)
931 DeviceClass
*dc
= DEVICE_CLASS(klass
);
933 dc
->desc
= "zpci device";
934 dc
->reset
= s390_pci_device_reset
;
935 dc
->bus_type
= TYPE_S390_PCI_BUS
;
936 dc
->realize
= s390_pci_device_realize
;
937 dc
->props
= s390_pci_device_properties
;
940 static const TypeInfo s390_pci_device_info
= {
941 .name
= TYPE_S390_PCI_DEVICE
,
942 .parent
= TYPE_DEVICE
,
943 .instance_size
= sizeof(S390PCIBusDevice
),
944 .class_init
= s390_pci_device_class_init
,
947 static void s390_pci_register_types(void)
949 type_register_static(&s390_pcihost_info
);
950 type_register_static(&s390_pcibus_info
);
951 type_register_static(&s390_pci_device_info
);
954 type_init(s390_pci_register_types
)