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 uint32_t argsz
= sizeof(struct vfio_iommu_type1_info
);
33 g_autofree
struct vfio_iommu_type1_info
*info
= g_malloc0(argsz
);
38 * If the specified argsz is not large enough to contain all capabilities
39 * it will be updated upon return from the ioctl. Retry until we have
40 * a big enough buffer to hold the entire capability chain.
45 if (ioctl(fd
, VFIO_IOMMU_GET_INFO
, info
)) {
49 if (info
->argsz
> argsz
) {
51 info
= g_realloc(info
, argsz
);
55 /* If the capability exists, update with the current value */
56 return vfio_get_info_dma_avail(info
, avail
);
59 S390PCIDMACount
*s390_pci_start_dma_count(S390pciState
*s
,
60 S390PCIBusDevice
*pbdev
)
64 VFIOPCIDevice
*vpdev
= container_of(pbdev
->pdev
, VFIOPCIDevice
, pdev
);
69 id
= vpdev
->vbasedev
.group
->container
->fd
;
71 if (!s390_pci_update_dma_avail(id
, &avail
)) {
75 QTAILQ_FOREACH(cnt
, &s
->zpci_dma_limit
, link
) {
82 cnt
= g_new0(S390PCIDMACount
, 1);
86 QTAILQ_INSERT_TAIL(&s
->zpci_dma_limit
, cnt
, link
);
87 pbdev
->iommu
->max_dma_limit
= avail
;
91 void s390_pci_end_dma_count(S390pciState
*s
, S390PCIDMACount
*cnt
)
96 if (cnt
->users
== 0) {
97 QTAILQ_REMOVE(&s
->zpci_dma_limit
, cnt
, link
);
101 static void s390_pci_read_base(S390PCIBusDevice
*pbdev
,
102 struct vfio_device_info
*info
)
104 struct vfio_info_cap_header
*hdr
;
105 struct vfio_device_info_cap_zpci_base
*cap
;
106 VFIOPCIDevice
*vpci
= container_of(pbdev
->pdev
, VFIOPCIDevice
, pdev
);
109 hdr
= vfio_get_device_info_cap(info
, VFIO_DEVICE_INFO_CAP_ZPCI_BASE
);
111 /* If capability not provided, just leave the defaults in place */
113 trace_s390_pci_clp_cap(vpci
->vbasedev
.name
,
114 VFIO_DEVICE_INFO_CAP_ZPCI_BASE
);
119 pbdev
->zpci_fn
.sdma
= cap
->start_dma
;
120 pbdev
->zpci_fn
.edma
= cap
->end_dma
;
121 pbdev
->zpci_fn
.pchid
= cap
->pchid
;
122 pbdev
->zpci_fn
.vfn
= cap
->vfn
;
123 pbdev
->zpci_fn
.pfgid
= cap
->gid
;
124 /* The following values remain 0 until we support other FMB formats */
125 pbdev
->zpci_fn
.fmbl
= 0;
126 pbdev
->zpci_fn
.pft
= 0;
127 /* Store function type separately for type-specific behavior */
128 pbdev
->pft
= cap
->pft
;
131 * If appropriate, reduce the size of the supported DMA aperture reported
132 * to the guest based upon the vfio DMA limit.
134 vfio_size
= pbdev
->iommu
->max_dma_limit
<< TARGET_PAGE_BITS
;
135 if (vfio_size
< (cap
->end_dma
- cap
->start_dma
+ 1)) {
136 pbdev
->zpci_fn
.edma
= cap
->start_dma
+ vfio_size
- 1;
140 static bool get_host_fh(S390PCIBusDevice
*pbdev
, struct vfio_device_info
*info
,
143 struct vfio_info_cap_header
*hdr
;
144 struct vfio_device_info_cap_zpci_base
*cap
;
145 VFIOPCIDevice
*vpci
= container_of(pbdev
->pdev
, VFIOPCIDevice
, pdev
);
147 hdr
= vfio_get_device_info_cap(info
, VFIO_DEVICE_INFO_CAP_ZPCI_BASE
);
149 /* Can only get the host fh with version 2 or greater */
150 if (hdr
== NULL
|| hdr
->version
< 2) {
151 trace_s390_pci_clp_cap(vpci
->vbasedev
.name
,
152 VFIO_DEVICE_INFO_CAP_ZPCI_BASE
);
161 static void s390_pci_read_group(S390PCIBusDevice
*pbdev
,
162 struct vfio_device_info
*info
)
164 struct vfio_info_cap_header
*hdr
;
165 struct vfio_device_info_cap_zpci_group
*cap
;
166 S390pciState
*s
= s390_get_phb();
167 ClpRspQueryPciGrp
*resgrp
;
168 VFIOPCIDevice
*vpci
= container_of(pbdev
->pdev
, VFIOPCIDevice
, pdev
);
169 uint8_t start_gid
= pbdev
->zpci_fn
.pfgid
;
171 hdr
= vfio_get_device_info_cap(info
, VFIO_DEVICE_INFO_CAP_ZPCI_GROUP
);
174 * If capability not provided or the underlying hostdev is simulated, just
175 * use the default group.
177 if (hdr
== NULL
|| pbdev
->zpci_fn
.pfgid
>= ZPCI_SIM_GRP_START
) {
178 trace_s390_pci_clp_cap(vpci
->vbasedev
.name
,
179 VFIO_DEVICE_INFO_CAP_ZPCI_GROUP
);
180 pbdev
->zpci_fn
.pfgid
= ZPCI_DEFAULT_FN_GRP
;
181 pbdev
->pci_group
= s390_group_find(ZPCI_DEFAULT_FN_GRP
);
187 * For an intercept device, let's use an existing simulated group if one
188 * one was already created for other intercept devices in this group.
189 * If not, create a new simulated group if any are still available.
190 * If all else fails, just fall back on the default group.
192 if (!pbdev
->interp
) {
193 pbdev
->pci_group
= s390_group_find_host_sim(pbdev
->zpci_fn
.pfgid
);
194 if (pbdev
->pci_group
) {
195 /* Use existing simulated group */
196 pbdev
->zpci_fn
.pfgid
= pbdev
->pci_group
->id
;
199 if (s
->next_sim_grp
== ZPCI_DEFAULT_FN_GRP
) {
200 /* All out of simulated groups, use default */
201 trace_s390_pci_clp_cap(vpci
->vbasedev
.name
,
202 VFIO_DEVICE_INFO_CAP_ZPCI_GROUP
);
203 pbdev
->zpci_fn
.pfgid
= ZPCI_DEFAULT_FN_GRP
;
204 pbdev
->pci_group
= s390_group_find(ZPCI_DEFAULT_FN_GRP
);
207 /* We can assign a new simulated group */
208 pbdev
->zpci_fn
.pfgid
= s
->next_sim_grp
;
210 /* Fall through to create the new sim group using CLP info */
215 /* See if the PCI group is already defined, create if not */
216 pbdev
->pci_group
= s390_group_find(pbdev
->zpci_fn
.pfgid
);
218 if (!pbdev
->pci_group
) {
219 pbdev
->pci_group
= s390_group_create(pbdev
->zpci_fn
.pfgid
, start_gid
);
221 resgrp
= &pbdev
->pci_group
->zpci_group
;
222 if (cap
->flags
& VFIO_DEVICE_INFO_ZPCI_FLAG_REFRESH
) {
225 resgrp
->dasm
= cap
->dasm
;
226 resgrp
->msia
= cap
->msi_addr
;
227 resgrp
->mui
= cap
->mui
;
228 resgrp
->i
= cap
->noi
;
229 if (pbdev
->interp
&& hdr
->version
>= 2) {
230 resgrp
->maxstbl
= cap
->imaxstbl
;
232 resgrp
->maxstbl
= cap
->maxstbl
;
234 resgrp
->version
= cap
->version
;
235 resgrp
->dtsm
= ZPCI_DTSM
;
239 static void s390_pci_read_util(S390PCIBusDevice
*pbdev
,
240 struct vfio_device_info
*info
)
242 struct vfio_info_cap_header
*hdr
;
243 struct vfio_device_info_cap_zpci_util
*cap
;
244 VFIOPCIDevice
*vpci
= container_of(pbdev
->pdev
, VFIOPCIDevice
, pdev
);
246 hdr
= vfio_get_device_info_cap(info
, VFIO_DEVICE_INFO_CAP_ZPCI_UTIL
);
248 /* If capability not provided, just leave the defaults in place */
250 trace_s390_pci_clp_cap(vpci
->vbasedev
.name
,
251 VFIO_DEVICE_INFO_CAP_ZPCI_UTIL
);
256 if (cap
->size
> CLP_UTIL_STR_LEN
) {
257 trace_s390_pci_clp_cap_size(vpci
->vbasedev
.name
, cap
->size
,
258 VFIO_DEVICE_INFO_CAP_ZPCI_UTIL
);
262 pbdev
->zpci_fn
.flags
|= CLP_RSP_QPCI_MASK_UTIL
;
263 memcpy(pbdev
->zpci_fn
.util_str
, cap
->util_str
, CLP_UTIL_STR_LEN
);
266 static void s390_pci_read_pfip(S390PCIBusDevice
*pbdev
,
267 struct vfio_device_info
*info
)
269 struct vfio_info_cap_header
*hdr
;
270 struct vfio_device_info_cap_zpci_pfip
*cap
;
271 VFIOPCIDevice
*vpci
= container_of(pbdev
->pdev
, VFIOPCIDevice
, pdev
);
273 hdr
= vfio_get_device_info_cap(info
, VFIO_DEVICE_INFO_CAP_ZPCI_PFIP
);
275 /* If capability not provided, just leave the defaults in place */
277 trace_s390_pci_clp_cap(vpci
->vbasedev
.name
,
278 VFIO_DEVICE_INFO_CAP_ZPCI_PFIP
);
283 if (cap
->size
> CLP_PFIP_NR_SEGMENTS
) {
284 trace_s390_pci_clp_cap_size(vpci
->vbasedev
.name
, cap
->size
,
285 VFIO_DEVICE_INFO_CAP_ZPCI_PFIP
);
289 memcpy(pbdev
->zpci_fn
.pfip
, cap
->pfip
, CLP_PFIP_NR_SEGMENTS
);
292 static struct vfio_device_info
*get_device_info(S390PCIBusDevice
*pbdev
,
295 struct vfio_device_info
*info
= g_malloc0(argsz
);
296 VFIOPCIDevice
*vfio_pci
;
299 vfio_pci
= container_of(pbdev
->pdev
, VFIOPCIDevice
, pdev
);
300 fd
= vfio_pci
->vbasedev
.fd
;
303 * If the specified argsz is not large enough to contain all capabilities
304 * it will be updated upon return from the ioctl. Retry until we have
305 * a big enough buffer to hold the entire capability chain. On error,
306 * just exit and rely on CLP defaults.
311 if (ioctl(fd
, VFIO_DEVICE_GET_INFO
, info
)) {
312 trace_s390_pci_clp_dev_info(vfio_pci
->vbasedev
.name
);
317 if (info
->argsz
> argsz
) {
319 info
= g_realloc(info
, argsz
);
327 * Get the host function handle from the vfio CLP capabilities chain. Returns
328 * true if a fh value was placed into the provided buffer. Returns false
329 * if a fh could not be obtained (ioctl failed or capability version does
330 * not include the fh)
332 bool s390_pci_get_host_fh(S390PCIBusDevice
*pbdev
, uint32_t *fh
)
334 g_autofree
struct vfio_device_info
*info
= NULL
;
338 info
= get_device_info(pbdev
, sizeof(*info
));
343 return get_host_fh(pbdev
, info
, fh
);
347 * This function will issue the VFIO_DEVICE_GET_INFO ioctl and look for
348 * capabilities that contain information about CLP features provided by the
350 * On entry, defaults have already been placed into the guest CLP response
351 * buffers. On exit, defaults will have been overwritten for any CLP features
352 * found in the capability chain; defaults will remain for any CLP features not
353 * found in the chain.
355 void s390_pci_get_clp_info(S390PCIBusDevice
*pbdev
)
357 g_autofree
struct vfio_device_info
*info
= NULL
;
359 info
= get_device_info(pbdev
, sizeof(*info
));
365 * Find the CLP features provided and fill in the guest CLP responses.
366 * Always call s390_pci_read_base first as information from this could
367 * determine which function group is used in s390_pci_read_group.
368 * For any feature not found, the default values will remain in the CLP
371 s390_pci_read_base(pbdev
, info
);
372 s390_pci_read_group(pbdev
, info
);
373 s390_pci_read_util(pbdev
, info
);
374 s390_pci_read_pfip(pbdev
, info
);