avocado/ppc_405.py: check TCG accel in test_ppc_ref405ep()
[qemu.git] / hw / s390x / s390-pci-vfio.c
blob6f80a47e2990085c16c12b9a310acc15179905b3
1 /*
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
9 * directory.
12 #include "qemu/osdep.h"
14 #include <sys/ioctl.h>
15 #include <linux/vfio.h>
16 #include <linux/vfio_zdev.h>
18 #include "trace.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);
35 assert(avail);
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.
42 retry:
43 info->argsz = argsz;
45 if (ioctl(fd, VFIO_IOMMU_GET_INFO, info)) {
46 return false;
49 if (info->argsz > argsz) {
50 argsz = info->argsz;
51 info = g_realloc(info, argsz);
52 goto retry;
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)
62 S390PCIDMACount *cnt;
63 uint32_t avail;
64 VFIOPCIDevice *vpdev = container_of(pbdev->pdev, VFIOPCIDevice, pdev);
65 int id;
67 assert(vpdev);
69 id = vpdev->vbasedev.group->container->fd;
71 if (!s390_pci_update_dma_avail(id, &avail)) {
72 return NULL;
75 QTAILQ_FOREACH(cnt, &s->zpci_dma_limit, link) {
76 if (cnt->id == id) {
77 cnt->users++;
78 return cnt;
82 cnt = g_new0(S390PCIDMACount, 1);
83 cnt->id = id;
84 cnt->users = 1;
85 cnt->avail = avail;
86 QTAILQ_INSERT_TAIL(&s->zpci_dma_limit, cnt, link);
87 return cnt;
90 void s390_pci_end_dma_count(S390pciState *s, S390PCIDMACount *cnt)
92 assert(cnt);
94 cnt->users--;
95 if (cnt->users == 0) {
96 QTAILQ_REMOVE(&s->zpci_dma_limit, cnt, link);
100 static void s390_pci_read_base(S390PCIBusDevice *pbdev,
101 struct vfio_device_info *info)
103 struct vfio_info_cap_header *hdr;
104 struct vfio_device_info_cap_zpci_base *cap;
105 VFIOPCIDevice *vpci = container_of(pbdev->pdev, VFIOPCIDevice, pdev);
107 hdr = vfio_get_device_info_cap(info, VFIO_DEVICE_INFO_CAP_ZPCI_BASE);
109 /* If capability not provided, just leave the defaults in place */
110 if (hdr == NULL) {
111 trace_s390_pci_clp_cap(vpci->vbasedev.name,
112 VFIO_DEVICE_INFO_CAP_ZPCI_BASE);
113 return;
115 cap = (void *) hdr;
117 pbdev->zpci_fn.sdma = cap->start_dma;
118 pbdev->zpci_fn.edma = cap->end_dma;
119 pbdev->zpci_fn.pchid = cap->pchid;
120 pbdev->zpci_fn.vfn = cap->vfn;
121 pbdev->zpci_fn.pfgid = cap->gid;
122 /* The following values remain 0 until we support other FMB formats */
123 pbdev->zpci_fn.fmbl = 0;
124 pbdev->zpci_fn.pft = 0;
127 static void s390_pci_read_group(S390PCIBusDevice *pbdev,
128 struct vfio_device_info *info)
130 struct vfio_info_cap_header *hdr;
131 struct vfio_device_info_cap_zpci_group *cap;
132 ClpRspQueryPciGrp *resgrp;
133 VFIOPCIDevice *vpci = container_of(pbdev->pdev, VFIOPCIDevice, pdev);
135 hdr = vfio_get_device_info_cap(info, VFIO_DEVICE_INFO_CAP_ZPCI_GROUP);
137 /* If capability not provided, just use the default group */
138 if (hdr == NULL) {
139 trace_s390_pci_clp_cap(vpci->vbasedev.name,
140 VFIO_DEVICE_INFO_CAP_ZPCI_GROUP);
141 pbdev->zpci_fn.pfgid = ZPCI_DEFAULT_FN_GRP;
142 pbdev->pci_group = s390_group_find(ZPCI_DEFAULT_FN_GRP);
143 return;
145 cap = (void *) hdr;
147 /* See if the PCI group is already defined, create if not */
148 pbdev->pci_group = s390_group_find(pbdev->zpci_fn.pfgid);
150 if (!pbdev->pci_group) {
151 pbdev->pci_group = s390_group_create(pbdev->zpci_fn.pfgid);
153 resgrp = &pbdev->pci_group->zpci_group;
154 if (cap->flags & VFIO_DEVICE_INFO_ZPCI_FLAG_REFRESH) {
155 resgrp->fr = 1;
157 resgrp->dasm = cap->dasm;
158 resgrp->msia = cap->msi_addr;
159 resgrp->mui = cap->mui;
160 resgrp->i = cap->noi;
161 resgrp->maxstbl = cap->maxstbl;
162 resgrp->version = cap->version;
163 resgrp->dtsm = ZPCI_DTSM;
167 static void s390_pci_read_util(S390PCIBusDevice *pbdev,
168 struct vfio_device_info *info)
170 struct vfio_info_cap_header *hdr;
171 struct vfio_device_info_cap_zpci_util *cap;
172 VFIOPCIDevice *vpci = container_of(pbdev->pdev, VFIOPCIDevice, pdev);
174 hdr = vfio_get_device_info_cap(info, VFIO_DEVICE_INFO_CAP_ZPCI_UTIL);
176 /* If capability not provided, just leave the defaults in place */
177 if (hdr == NULL) {
178 trace_s390_pci_clp_cap(vpci->vbasedev.name,
179 VFIO_DEVICE_INFO_CAP_ZPCI_UTIL);
180 return;
182 cap = (void *) hdr;
184 if (cap->size > CLP_UTIL_STR_LEN) {
185 trace_s390_pci_clp_cap_size(vpci->vbasedev.name, cap->size,
186 VFIO_DEVICE_INFO_CAP_ZPCI_UTIL);
187 return;
190 pbdev->zpci_fn.flags |= CLP_RSP_QPCI_MASK_UTIL;
191 memcpy(pbdev->zpci_fn.util_str, cap->util_str, CLP_UTIL_STR_LEN);
194 static void s390_pci_read_pfip(S390PCIBusDevice *pbdev,
195 struct vfio_device_info *info)
197 struct vfio_info_cap_header *hdr;
198 struct vfio_device_info_cap_zpci_pfip *cap;
199 VFIOPCIDevice *vpci = container_of(pbdev->pdev, VFIOPCIDevice, pdev);
201 hdr = vfio_get_device_info_cap(info, VFIO_DEVICE_INFO_CAP_ZPCI_PFIP);
203 /* If capability not provided, just leave the defaults in place */
204 if (hdr == NULL) {
205 trace_s390_pci_clp_cap(vpci->vbasedev.name,
206 VFIO_DEVICE_INFO_CAP_ZPCI_PFIP);
207 return;
209 cap = (void *) hdr;
211 if (cap->size > CLP_PFIP_NR_SEGMENTS) {
212 trace_s390_pci_clp_cap_size(vpci->vbasedev.name, cap->size,
213 VFIO_DEVICE_INFO_CAP_ZPCI_PFIP);
214 return;
217 memcpy(pbdev->zpci_fn.pfip, cap->pfip, CLP_PFIP_NR_SEGMENTS);
221 * This function will issue the VFIO_DEVICE_GET_INFO ioctl and look for
222 * capabilities that contain information about CLP features provided by the
223 * underlying host.
224 * On entry, defaults have already been placed into the guest CLP response
225 * buffers. On exit, defaults will have been overwritten for any CLP features
226 * found in the capability chain; defaults will remain for any CLP features not
227 * found in the chain.
229 void s390_pci_get_clp_info(S390PCIBusDevice *pbdev)
231 g_autofree struct vfio_device_info *info = NULL;
232 VFIOPCIDevice *vfio_pci;
233 uint32_t argsz;
234 int fd;
236 argsz = sizeof(*info);
237 info = g_malloc0(argsz);
239 vfio_pci = container_of(pbdev->pdev, VFIOPCIDevice, pdev);
240 fd = vfio_pci->vbasedev.fd;
243 * If the specified argsz is not large enough to contain all capabilities
244 * it will be updated upon return from the ioctl. Retry until we have
245 * a big enough buffer to hold the entire capability chain. On error,
246 * just exit and rely on CLP defaults.
248 retry:
249 info->argsz = argsz;
251 if (ioctl(fd, VFIO_DEVICE_GET_INFO, info)) {
252 trace_s390_pci_clp_dev_info(vfio_pci->vbasedev.name);
253 return;
256 if (info->argsz > argsz) {
257 argsz = info->argsz;
258 info = g_realloc(info, argsz);
259 goto retry;
263 * Find the CLP features provided and fill in the guest CLP responses.
264 * Always call s390_pci_read_base first as information from this could
265 * determine which function group is used in s390_pci_read_group.
266 * For any feature not found, the default values will remain in the CLP
267 * response.
269 s390_pci_read_base(pbdev, info);
270 s390_pci_read_group(pbdev, info);
271 s390_pci_read_util(pbdev, info);
272 s390_pci_read_pfip(pbdev, info);
274 return;