2 * s390 vfio-pci interfaces
4 * Copyright 2020 IBM Corp.
5 * Author(s): Matthew Rosato <mjrosato@linux.ibm.com>
7 * This work is licensed under the terms of the GNU GPL, version 2 or (at
8 * your option) any later version. See the COPYING file in the top-level
12 #include "qemu/osdep.h"
14 #include <sys/ioctl.h>
15 #include <linux/vfio.h>
16 #include <linux/vfio_zdev.h>
19 #include "hw/s390x/s390-pci-bus.h"
20 #include "hw/s390x/s390-pci-clp.h"
21 #include "hw/s390x/s390-pci-vfio.h"
22 #include "hw/vfio/pci.h"
23 #include "hw/vfio/vfio-common.h"
26 * Get the current DMA available count from vfio. Returns true if vfio is
27 * limiting DMA requests, false otherwise. The current available count read
28 * from vfio is returned in avail.
30 bool s390_pci_update_dma_avail(int fd
, unsigned int *avail
)
32 g_autofree
struct vfio_iommu_type1_info
*info
;
37 argsz
= sizeof(struct vfio_iommu_type1_info
);
38 info
= g_malloc0(argsz
);
41 * If the specified argsz is not large enough to contain all capabilities
42 * it will be updated upon return from the ioctl. Retry until we have
43 * a big enough buffer to hold the entire capability chain.
48 if (ioctl(fd
, VFIO_IOMMU_GET_INFO
, info
)) {
52 if (info
->argsz
> argsz
) {
54 info
= g_realloc(info
, argsz
);
58 /* If the capability exists, update with the current value */
59 return vfio_get_info_dma_avail(info
, avail
);
62 S390PCIDMACount
*s390_pci_start_dma_count(S390pciState
*s
,
63 S390PCIBusDevice
*pbdev
)
67 VFIOPCIDevice
*vpdev
= container_of(pbdev
->pdev
, VFIOPCIDevice
, pdev
);
72 id
= vpdev
->vbasedev
.group
->container
->fd
;
74 if (!s390_pci_update_dma_avail(id
, &avail
)) {
78 QTAILQ_FOREACH(cnt
, &s
->zpci_dma_limit
, link
) {
85 cnt
= g_new0(S390PCIDMACount
, 1);
89 QTAILQ_INSERT_TAIL(&s
->zpci_dma_limit
, cnt
, link
);
93 void s390_pci_end_dma_count(S390pciState
*s
, S390PCIDMACount
*cnt
)
98 if (cnt
->users
== 0) {
99 QTAILQ_REMOVE(&s
->zpci_dma_limit
, cnt
, link
);
103 static void s390_pci_read_base(S390PCIBusDevice
*pbdev
,
104 struct vfio_device_info
*info
)
106 struct vfio_info_cap_header
*hdr
;
107 struct vfio_device_info_cap_zpci_base
*cap
;
108 VFIOPCIDevice
*vpci
= container_of(pbdev
->pdev
, VFIOPCIDevice
, pdev
);
110 hdr
= vfio_get_device_info_cap(info
, VFIO_DEVICE_INFO_CAP_ZPCI_BASE
);
112 /* If capability not provided, just leave the defaults in place */
114 trace_s390_pci_clp_cap(vpci
->vbasedev
.name
,
115 VFIO_DEVICE_INFO_CAP_ZPCI_BASE
);
120 pbdev
->zpci_fn
.sdma
= cap
->start_dma
;
121 pbdev
->zpci_fn
.edma
= cap
->end_dma
;
122 pbdev
->zpci_fn
.pchid
= cap
->pchid
;
123 pbdev
->zpci_fn
.vfn
= cap
->vfn
;
124 pbdev
->zpci_fn
.pfgid
= cap
->gid
;
125 /* The following values remain 0 until we support other FMB formats */
126 pbdev
->zpci_fn
.fmbl
= 0;
127 pbdev
->zpci_fn
.pft
= 0;
130 static void s390_pci_read_group(S390PCIBusDevice
*pbdev
,
131 struct vfio_device_info
*info
)
133 struct vfio_info_cap_header
*hdr
;
134 struct vfio_device_info_cap_zpci_group
*cap
;
135 ClpRspQueryPciGrp
*resgrp
;
136 VFIOPCIDevice
*vpci
= container_of(pbdev
->pdev
, VFIOPCIDevice
, pdev
);
138 hdr
= vfio_get_device_info_cap(info
, VFIO_DEVICE_INFO_CAP_ZPCI_GROUP
);
140 /* If capability not provided, just use the default group */
142 trace_s390_pci_clp_cap(vpci
->vbasedev
.name
,
143 VFIO_DEVICE_INFO_CAP_ZPCI_GROUP
);
144 pbdev
->zpci_fn
.pfgid
= ZPCI_DEFAULT_FN_GRP
;
145 pbdev
->pci_group
= s390_group_find(ZPCI_DEFAULT_FN_GRP
);
150 /* See if the PCI group is already defined, create if not */
151 pbdev
->pci_group
= s390_group_find(pbdev
->zpci_fn
.pfgid
);
153 if (!pbdev
->pci_group
) {
154 pbdev
->pci_group
= s390_group_create(pbdev
->zpci_fn
.pfgid
);
156 resgrp
= &pbdev
->pci_group
->zpci_group
;
157 if (cap
->flags
& VFIO_DEVICE_INFO_ZPCI_FLAG_REFRESH
) {
160 resgrp
->dasm
= cap
->dasm
;
161 resgrp
->msia
= cap
->msi_addr
;
162 resgrp
->mui
= cap
->mui
;
163 resgrp
->i
= cap
->noi
;
164 resgrp
->maxstbl
= cap
->maxstbl
;
165 resgrp
->version
= cap
->version
;
169 static void s390_pci_read_util(S390PCIBusDevice
*pbdev
,
170 struct vfio_device_info
*info
)
172 struct vfio_info_cap_header
*hdr
;
173 struct vfio_device_info_cap_zpci_util
*cap
;
174 VFIOPCIDevice
*vpci
= container_of(pbdev
->pdev
, VFIOPCIDevice
, pdev
);
176 hdr
= vfio_get_device_info_cap(info
, VFIO_DEVICE_INFO_CAP_ZPCI_UTIL
);
178 /* If capability not provided, just leave the defaults in place */
180 trace_s390_pci_clp_cap(vpci
->vbasedev
.name
,
181 VFIO_DEVICE_INFO_CAP_ZPCI_UTIL
);
186 if (cap
->size
> CLP_UTIL_STR_LEN
) {
187 trace_s390_pci_clp_cap_size(vpci
->vbasedev
.name
, cap
->size
,
188 VFIO_DEVICE_INFO_CAP_ZPCI_UTIL
);
192 pbdev
->zpci_fn
.flags
|= CLP_RSP_QPCI_MASK_UTIL
;
193 memcpy(pbdev
->zpci_fn
.util_str
, cap
->util_str
, CLP_UTIL_STR_LEN
);
196 static void s390_pci_read_pfip(S390PCIBusDevice
*pbdev
,
197 struct vfio_device_info
*info
)
199 struct vfio_info_cap_header
*hdr
;
200 struct vfio_device_info_cap_zpci_pfip
*cap
;
201 VFIOPCIDevice
*vpci
= container_of(pbdev
->pdev
, VFIOPCIDevice
, pdev
);
203 hdr
= vfio_get_device_info_cap(info
, VFIO_DEVICE_INFO_CAP_ZPCI_PFIP
);
205 /* If capability not provided, just leave the defaults in place */
207 trace_s390_pci_clp_cap(vpci
->vbasedev
.name
,
208 VFIO_DEVICE_INFO_CAP_ZPCI_PFIP
);
213 if (cap
->size
> CLP_PFIP_NR_SEGMENTS
) {
214 trace_s390_pci_clp_cap_size(vpci
->vbasedev
.name
, cap
->size
,
215 VFIO_DEVICE_INFO_CAP_ZPCI_PFIP
);
219 memcpy(pbdev
->zpci_fn
.pfip
, cap
->pfip
, CLP_PFIP_NR_SEGMENTS
);
223 * This function will issue the VFIO_DEVICE_GET_INFO ioctl and look for
224 * capabilities that contain information about CLP features provided by the
226 * On entry, defaults have already been placed into the guest CLP response
227 * buffers. On exit, defaults will have been overwritten for any CLP features
228 * found in the capability chain; defaults will remain for any CLP features not
229 * found in the chain.
231 void s390_pci_get_clp_info(S390PCIBusDevice
*pbdev
)
233 g_autofree
struct vfio_device_info
*info
;
234 VFIOPCIDevice
*vfio_pci
;
238 argsz
= sizeof(*info
);
239 info
= g_malloc0(argsz
);
241 vfio_pci
= container_of(pbdev
->pdev
, VFIOPCIDevice
, pdev
);
242 fd
= vfio_pci
->vbasedev
.fd
;
245 * If the specified argsz is not large enough to contain all capabilities
246 * it will be updated upon return from the ioctl. Retry until we have
247 * a big enough buffer to hold the entire capability chain. On error,
248 * just exit and rely on CLP defaults.
253 if (ioctl(fd
, VFIO_DEVICE_GET_INFO
, info
)) {
254 trace_s390_pci_clp_dev_info(vfio_pci
->vbasedev
.name
);
258 if (info
->argsz
> argsz
) {
260 info
= g_realloc(info
, argsz
);
265 * Find the CLP features provided and fill in the guest CLP responses.
266 * Always call s390_pci_read_base first as information from this could
267 * determine which function group is used in s390_pci_read_group.
268 * For any feature not found, the default values will remain in the CLP
271 s390_pci_read_base(pbdev
, info
);
272 s390_pci_read_group(pbdev
, info
);
273 s390_pci_read_util(pbdev
, info
);
274 s390_pci_read_pfip(pbdev
, info
);