1 /* 2 * s390 vfio-pci interfaces 3 * 4 * Copyright 2020 IBM Corp. 5 * Author(s): Matthew Rosato <mjrosato@linux.ibm.com> 6 * 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. 10 */ 11 12 #include "qemu/osdep.h" 13 14 #include <sys/ioctl.h> 15 #include <linux/vfio.h> 16 #include <linux/vfio_zdev.h> 17 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" 24 25 /* 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. 29 */ 30 bool s390_pci_update_dma_avail(int fd, unsigned int *avail) 31 { 32 uint32_t argsz = sizeof(struct vfio_iommu_type1_info); 33 g_autofree struct vfio_iommu_type1_info *info = g_malloc0(argsz); 34 35 assert(avail); 36 37 /* 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. 41 */ 42 retry: 43 info->argsz = argsz; 44 45 if (ioctl(fd, VFIO_IOMMU_GET_INFO, info)) { 46 return false; 47 } 48 49 if (info->argsz > argsz) { 50 argsz = info->argsz; 51 info = g_realloc(info, argsz); 52 goto retry; 53 } 54 55 /* If the capability exists, update with the current value */ 56 return vfio_get_info_dma_avail(info, avail); 57 } 58 59 S390PCIDMACount *s390_pci_start_dma_count(S390pciState *s, 60 S390PCIBusDevice *pbdev) 61 { 62 S390PCIDMACount *cnt; 63 uint32_t avail; 64 VFIOPCIDevice *vpdev = container_of(pbdev->pdev, VFIOPCIDevice, pdev); 65 int id; 66 67 assert(vpdev); 68 69 id = vpdev->vbasedev.group->container->fd; 70 71 if (!s390_pci_update_dma_avail(id, &avail)) { 72 return NULL; 73 } 74 75 QTAILQ_FOREACH(cnt, &s->zpci_dma_limit, link) { 76 if (cnt->id == id) { 77 cnt->users++; 78 return cnt; 79 } 80 } 81 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 pbdev->iommu->max_dma_limit = avail; 88 return cnt; 89 } 90 91 void s390_pci_end_dma_count(S390pciState *s, S390PCIDMACount *cnt) 92 { 93 assert(cnt); 94 95 cnt->users--; 96 if (cnt->users == 0) { 97 QTAILQ_REMOVE(&s->zpci_dma_limit, cnt, link); 98 } 99 } 100 101 static void s390_pci_read_base(S390PCIBusDevice *pbdev, 102 struct vfio_device_info *info) 103 { 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); 107 uint64_t vfio_size; 108 109 hdr = vfio_get_device_info_cap(info, VFIO_DEVICE_INFO_CAP_ZPCI_BASE); 110 111 /* If capability not provided, just leave the defaults in place */ 112 if (hdr == NULL) { 113 trace_s390_pci_clp_cap(vpci->vbasedev.name, 114 VFIO_DEVICE_INFO_CAP_ZPCI_BASE); 115 return; 116 } 117 cap = (void *) hdr; 118 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 128 /* 129 * If appropriate, reduce the size of the supported DMA aperture reported 130 * to the guest based upon the vfio DMA limit. 131 */ 132 vfio_size = pbdev->iommu->max_dma_limit << TARGET_PAGE_BITS; 133 if (vfio_size < (cap->end_dma - cap->start_dma + 1)) { 134 pbdev->zpci_fn.edma = cap->start_dma + vfio_size - 1; 135 } 136 } 137 138 static bool get_host_fh(S390PCIBusDevice *pbdev, struct vfio_device_info *info, 139 uint32_t *fh) 140 { 141 struct vfio_info_cap_header *hdr; 142 struct vfio_device_info_cap_zpci_base *cap; 143 VFIOPCIDevice *vpci = container_of(pbdev->pdev, VFIOPCIDevice, pdev); 144 145 hdr = vfio_get_device_info_cap(info, VFIO_DEVICE_INFO_CAP_ZPCI_BASE); 146 147 /* Can only get the host fh with version 2 or greater */ 148 if (hdr == NULL || hdr->version < 2) { 149 trace_s390_pci_clp_cap(vpci->vbasedev.name, 150 VFIO_DEVICE_INFO_CAP_ZPCI_BASE); 151 return false; 152 } 153 cap = (void *) hdr; 154 155 *fh = cap->fh; 156 return true; 157 } 158 159 static void s390_pci_read_group(S390PCIBusDevice *pbdev, 160 struct vfio_device_info *info) 161 { 162 struct vfio_info_cap_header *hdr; 163 struct vfio_device_info_cap_zpci_group *cap; 164 S390pciState *s = s390_get_phb(); 165 ClpRspQueryPciGrp *resgrp; 166 VFIOPCIDevice *vpci = container_of(pbdev->pdev, VFIOPCIDevice, pdev); 167 uint8_t start_gid = pbdev->zpci_fn.pfgid; 168 169 hdr = vfio_get_device_info_cap(info, VFIO_DEVICE_INFO_CAP_ZPCI_GROUP); 170 171 /* 172 * If capability not provided or the underlying hostdev is simulated, just 173 * use the default group. 174 */ 175 if (hdr == NULL || pbdev->zpci_fn.pfgid >= ZPCI_SIM_GRP_START) { 176 trace_s390_pci_clp_cap(vpci->vbasedev.name, 177 VFIO_DEVICE_INFO_CAP_ZPCI_GROUP); 178 pbdev->zpci_fn.pfgid = ZPCI_DEFAULT_FN_GRP; 179 pbdev->pci_group = s390_group_find(ZPCI_DEFAULT_FN_GRP); 180 return; 181 } 182 cap = (void *) hdr; 183 184 /* 185 * For an intercept device, let's use an existing simulated group if one 186 * one was already created for other intercept devices in this group. 187 * If not, create a new simulated group if any are still available. 188 * If all else fails, just fall back on the default group. 189 */ 190 if (!pbdev->interp) { 191 pbdev->pci_group = s390_group_find_host_sim(pbdev->zpci_fn.pfgid); 192 if (pbdev->pci_group) { 193 /* Use existing simulated group */ 194 pbdev->zpci_fn.pfgid = pbdev->pci_group->id; 195 return; 196 } else { 197 if (s->next_sim_grp == ZPCI_DEFAULT_FN_GRP) { 198 /* All out of simulated groups, use default */ 199 trace_s390_pci_clp_cap(vpci->vbasedev.name, 200 VFIO_DEVICE_INFO_CAP_ZPCI_GROUP); 201 pbdev->zpci_fn.pfgid = ZPCI_DEFAULT_FN_GRP; 202 pbdev->pci_group = s390_group_find(ZPCI_DEFAULT_FN_GRP); 203 return; 204 } else { 205 /* We can assign a new simulated group */ 206 pbdev->zpci_fn.pfgid = s->next_sim_grp; 207 s->next_sim_grp++; 208 /* Fall through to create the new sim group using CLP info */ 209 } 210 } 211 } 212 213 /* See if the PCI group is already defined, create if not */ 214 pbdev->pci_group = s390_group_find(pbdev->zpci_fn.pfgid); 215 216 if (!pbdev->pci_group) { 217 pbdev->pci_group = s390_group_create(pbdev->zpci_fn.pfgid, start_gid); 218 219 resgrp = &pbdev->pci_group->zpci_group; 220 if (cap->flags & VFIO_DEVICE_INFO_ZPCI_FLAG_REFRESH) { 221 resgrp->fr = 1; 222 } 223 resgrp->dasm = cap->dasm; 224 resgrp->msia = cap->msi_addr; 225 resgrp->mui = cap->mui; 226 resgrp->i = cap->noi; 227 if (pbdev->interp && hdr->version >= 2) { 228 resgrp->maxstbl = cap->imaxstbl; 229 } else { 230 resgrp->maxstbl = cap->maxstbl; 231 } 232 resgrp->version = cap->version; 233 resgrp->dtsm = ZPCI_DTSM; 234 } 235 } 236 237 static void s390_pci_read_util(S390PCIBusDevice *pbdev, 238 struct vfio_device_info *info) 239 { 240 struct vfio_info_cap_header *hdr; 241 struct vfio_device_info_cap_zpci_util *cap; 242 VFIOPCIDevice *vpci = container_of(pbdev->pdev, VFIOPCIDevice, pdev); 243 244 hdr = vfio_get_device_info_cap(info, VFIO_DEVICE_INFO_CAP_ZPCI_UTIL); 245 246 /* If capability not provided, just leave the defaults in place */ 247 if (hdr == NULL) { 248 trace_s390_pci_clp_cap(vpci->vbasedev.name, 249 VFIO_DEVICE_INFO_CAP_ZPCI_UTIL); 250 return; 251 } 252 cap = (void *) hdr; 253 254 if (cap->size > CLP_UTIL_STR_LEN) { 255 trace_s390_pci_clp_cap_size(vpci->vbasedev.name, cap->size, 256 VFIO_DEVICE_INFO_CAP_ZPCI_UTIL); 257 return; 258 } 259 260 pbdev->zpci_fn.flags |= CLP_RSP_QPCI_MASK_UTIL; 261 memcpy(pbdev->zpci_fn.util_str, cap->util_str, CLP_UTIL_STR_LEN); 262 } 263 264 static void s390_pci_read_pfip(S390PCIBusDevice *pbdev, 265 struct vfio_device_info *info) 266 { 267 struct vfio_info_cap_header *hdr; 268 struct vfio_device_info_cap_zpci_pfip *cap; 269 VFIOPCIDevice *vpci = container_of(pbdev->pdev, VFIOPCIDevice, pdev); 270 271 hdr = vfio_get_device_info_cap(info, VFIO_DEVICE_INFO_CAP_ZPCI_PFIP); 272 273 /* If capability not provided, just leave the defaults in place */ 274 if (hdr == NULL) { 275 trace_s390_pci_clp_cap(vpci->vbasedev.name, 276 VFIO_DEVICE_INFO_CAP_ZPCI_PFIP); 277 return; 278 } 279 cap = (void *) hdr; 280 281 if (cap->size > CLP_PFIP_NR_SEGMENTS) { 282 trace_s390_pci_clp_cap_size(vpci->vbasedev.name, cap->size, 283 VFIO_DEVICE_INFO_CAP_ZPCI_PFIP); 284 return; 285 } 286 287 memcpy(pbdev->zpci_fn.pfip, cap->pfip, CLP_PFIP_NR_SEGMENTS); 288 } 289 290 static struct vfio_device_info *get_device_info(S390PCIBusDevice *pbdev, 291 uint32_t argsz) 292 { 293 struct vfio_device_info *info = g_malloc0(argsz); 294 VFIOPCIDevice *vfio_pci; 295 int fd; 296 297 vfio_pci = container_of(pbdev->pdev, VFIOPCIDevice, pdev); 298 fd = vfio_pci->vbasedev.fd; 299 300 /* 301 * If the specified argsz is not large enough to contain all capabilities 302 * it will be updated upon return from the ioctl. Retry until we have 303 * a big enough buffer to hold the entire capability chain. On error, 304 * just exit and rely on CLP defaults. 305 */ 306 retry: 307 info->argsz = argsz; 308 309 if (ioctl(fd, VFIO_DEVICE_GET_INFO, info)) { 310 trace_s390_pci_clp_dev_info(vfio_pci->vbasedev.name); 311 g_free(info); 312 return NULL; 313 } 314 315 if (info->argsz > argsz) { 316 argsz = info->argsz; 317 info = g_realloc(info, argsz); 318 goto retry; 319 } 320 321 return info; 322 } 323 324 /* 325 * Get the host function handle from the vfio CLP capabilities chain. Returns 326 * true if a fh value was placed into the provided buffer. Returns false 327 * if a fh could not be obtained (ioctl failed or capability version does 328 * not include the fh) 329 */ 330 bool s390_pci_get_host_fh(S390PCIBusDevice *pbdev, uint32_t *fh) 331 { 332 g_autofree struct vfio_device_info *info = NULL; 333 334 assert(fh); 335 336 info = get_device_info(pbdev, sizeof(*info)); 337 if (!info) { 338 return false; 339 } 340 341 return get_host_fh(pbdev, info, fh); 342 } 343 344 /* 345 * This function will issue the VFIO_DEVICE_GET_INFO ioctl and look for 346 * capabilities that contain information about CLP features provided by the 347 * underlying host. 348 * On entry, defaults have already been placed into the guest CLP response 349 * buffers. On exit, defaults will have been overwritten for any CLP features 350 * found in the capability chain; defaults will remain for any CLP features not 351 * found in the chain. 352 */ 353 void s390_pci_get_clp_info(S390PCIBusDevice *pbdev) 354 { 355 g_autofree struct vfio_device_info *info = NULL; 356 357 info = get_device_info(pbdev, sizeof(*info)); 358 if (!info) { 359 return; 360 } 361 362 /* 363 * Find the CLP features provided and fill in the guest CLP responses. 364 * Always call s390_pci_read_base first as information from this could 365 * determine which function group is used in s390_pci_read_group. 366 * For any feature not found, the default values will remain in the CLP 367 * response. 368 */ 369 s390_pci_read_base(pbdev, info); 370 s390_pci_read_group(pbdev, info); 371 s390_pci_read_util(pbdev, info); 372 s390_pci_read_pfip(pbdev, info); 373 374 return; 375 } 376