| 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/ipl/s390-pci-clp.h" |
| 21 | #include "hw/s390x/s390-pci-vfio.h" |
| 22 | #include "hw/vfio/pci.h" |
| 23 | #include "hw/vfio/vfio-container-legacy.h" |
| 24 | #include "hw/vfio/vfio-helpers.h" |
| 25 | #include "exec/target_page.h" |
| 26 | |
| 27 | /* |
| 28 | * Get the current DMA available count from vfio. Returns true if vfio is |
| 29 | * limiting DMA requests, false otherwise. The current available count read |
| 30 | * from vfio is returned in avail. |
| 31 | */ |
| 32 | bool s390_pci_update_dma_avail(int fd, unsigned int *avail) |
| 33 | { |
| 34 | uint32_t argsz = sizeof(struct vfio_iommu_type1_info); |
| 35 | g_autofree struct vfio_iommu_type1_info *info = g_malloc0(argsz); |
| 36 | |
| 37 | assert(avail); |
| 38 | |
| 39 | /* |
| 40 | * If the specified argsz is not large enough to contain all capabilities |
| 41 | * it will be updated upon return from the ioctl. Retry until we have |
| 42 | * a big enough buffer to hold the entire capability chain. |
| 43 | */ |
| 44 | retry: |
| 45 | info->argsz = argsz; |
| 46 | |
| 47 | if (ioctl(fd, VFIO_IOMMU_GET_INFO, info)) { |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | if (info->argsz > argsz) { |
| 52 | argsz = info->argsz; |
| 53 | info = g_realloc(info, argsz); |
| 54 | goto retry; |
| 55 | } |
| 56 | |
| 57 | /* If the capability exists, update with the current value */ |
| 58 | return vfio_get_info_dma_avail(info, avail); |
| 59 | } |
| 60 | |
| 61 | S390PCIDMACount *s390_pci_start_dma_count(S390pciState *s, |
| 62 | S390PCIBusDevice *pbdev) |
| 63 | { |
| 64 | S390PCIDMACount *cnt; |
| 65 | uint32_t avail; |
| 66 | VFIOPCIDevice *vpdev = VFIO_PCI_DEVICE(pbdev->pdev); |
| 67 | int id; |
| 68 | |
| 69 | assert(vpdev); |
| 70 | |
| 71 | if (!vpdev->vbasedev.group) { |
| 72 | return NULL; |
| 73 | } |
| 74 | |
| 75 | id = vpdev->vbasedev.group->container->fd; |
| 76 | |
| 77 | if (!s390_pci_update_dma_avail(id, &avail)) { |
| 78 | return NULL; |
| 79 | } |
| 80 | |
| 81 | QTAILQ_FOREACH(cnt, &s->zpci_dma_limit, link) { |
| 82 | if (cnt->id == id) { |
| 83 | cnt->users++; |
| 84 | return cnt; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | cnt = g_new0(S390PCIDMACount, 1); |
| 89 | cnt->id = id; |
| 90 | cnt->users = 1; |
| 91 | cnt->avail = avail; |
| 92 | QTAILQ_INSERT_TAIL(&s->zpci_dma_limit, cnt, link); |
| 93 | pbdev->iommu->max_dma_limit = avail; |
| 94 | return cnt; |
| 95 | } |
| 96 | |
| 97 | void s390_pci_end_dma_count(S390pciState *s, S390PCIDMACount *cnt) |
| 98 | { |
| 99 | assert(cnt); |
| 100 | |
| 101 | cnt->users--; |
| 102 | if (cnt->users == 0) { |
| 103 | QTAILQ_REMOVE(&s->zpci_dma_limit, cnt, link); |
| 104 | g_free(cnt); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | static void s390_pci_read_base(S390PCIBusDevice *pbdev, |
| 109 | struct vfio_device_info *info) |
| 110 | { |
| 111 | struct vfio_info_cap_header *hdr; |
| 112 | struct vfio_device_info_cap_zpci_base *cap; |
| 113 | VFIOPCIDevice *vpci = VFIO_PCI_DEVICE(pbdev->pdev); |
| 114 | uint64_t vfio_size; |
| 115 | |
| 116 | hdr = vfio_get_device_info_cap(info, VFIO_DEVICE_INFO_CAP_ZPCI_BASE); |
| 117 | |
| 118 | /* If capability not provided, just leave the defaults in place */ |
| 119 | if (hdr == NULL) { |
| 120 | trace_s390_pci_clp_cap(vpci->vbasedev.name, |
| 121 | VFIO_DEVICE_INFO_CAP_ZPCI_BASE); |
| 122 | return; |
| 123 | } |
| 124 | cap = (void *) hdr; |
| 125 | |
| 126 | pbdev->zpci_fn.sdma = cap->start_dma; |
| 127 | pbdev->zpci_fn.edma = cap->end_dma; |
| 128 | pbdev->zpci_fn.pchid = cap->pchid; |
| 129 | pbdev->zpci_fn.vfn = cap->vfn; |
| 130 | pbdev->zpci_fn.pfgid = cap->gid; |
| 131 | /* The following values remain 0 until we support other FMB formats */ |
| 132 | pbdev->zpci_fn.fmbl = 0; |
| 133 | pbdev->zpci_fn.pft = 0; |
| 134 | /* Store function type separately for type-specific behavior */ |
| 135 | pbdev->pft = cap->pft; |
| 136 | |
| 137 | /* |
| 138 | * If the device is a passthrough ISM device, disallow relaxed |
| 139 | * translation. |
| 140 | */ |
| 141 | if (pbdev->pft == ZPCI_PFT_ISM) { |
| 142 | pbdev->rtr_avail = false; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * If appropriate, reduce the size of the supported DMA aperture reported |
| 147 | * to the guest based upon the vfio DMA limit. This is applicable for |
| 148 | * devices that are guaranteed to not use relaxed translation. If the |
| 149 | * device is capable of relaxed translation then we must advertise the |
| 150 | * full aperture. In this case, if translation is used then we will |
| 151 | * rely on the vfio DMA limit counting and use RPCIT CC1 / status 16 |
| 152 | * to request that the guest free DMA mappings as necessary. |
| 153 | */ |
| 154 | if (!pbdev->rtr_avail) { |
| 155 | vfio_size = pbdev->iommu->max_dma_limit << qemu_target_page_bits(); |
| 156 | if (vfio_size > 0 && vfio_size < cap->end_dma - cap->start_dma + 1) { |
| 157 | pbdev->zpci_fn.edma = cap->start_dma + vfio_size - 1; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | static bool get_host_fh(S390PCIBusDevice *pbdev, struct vfio_device_info *info, |
| 163 | uint32_t *fh) |
| 164 | { |
| 165 | struct vfio_info_cap_header *hdr; |
| 166 | struct vfio_device_info_cap_zpci_base *cap; |
| 167 | VFIOPCIDevice *vpci = VFIO_PCI_DEVICE(pbdev->pdev); |
| 168 | |
| 169 | hdr = vfio_get_device_info_cap(info, VFIO_DEVICE_INFO_CAP_ZPCI_BASE); |
| 170 | |
| 171 | /* Can only get the host fh with version 2 or greater */ |
| 172 | if (hdr == NULL || hdr->version < 2) { |
| 173 | trace_s390_pci_clp_cap(vpci->vbasedev.name, |
| 174 | VFIO_DEVICE_INFO_CAP_ZPCI_BASE); |
| 175 | return false; |
| 176 | } |
| 177 | cap = (void *) hdr; |
| 178 | |
| 179 | *fh = cap->fh; |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | static void s390_pci_read_group(S390PCIBusDevice *pbdev, |
| 184 | struct vfio_device_info *info) |
| 185 | { |
| 186 | struct vfio_info_cap_header *hdr; |
| 187 | struct vfio_device_info_cap_zpci_group *cap; |
| 188 | S390pciState *s = s390_get_phb(); |
| 189 | ClpRspQueryPciGrp *resgrp; |
| 190 | VFIOPCIDevice *vpci = VFIO_PCI_DEVICE(pbdev->pdev); |
| 191 | uint8_t start_gid = pbdev->zpci_fn.pfgid; |
| 192 | |
| 193 | hdr = vfio_get_device_info_cap(info, VFIO_DEVICE_INFO_CAP_ZPCI_GROUP); |
| 194 | |
| 195 | /* |
| 196 | * If capability not provided or the underlying hostdev is simulated, just |
| 197 | * use the default group. |
| 198 | */ |
| 199 | if (hdr == NULL || pbdev->zpci_fn.pfgid >= ZPCI_SIM_GRP_START) { |
| 200 | trace_s390_pci_clp_cap(vpci->vbasedev.name, |
| 201 | VFIO_DEVICE_INFO_CAP_ZPCI_GROUP); |
| 202 | pbdev->zpci_fn.pfgid = ZPCI_DEFAULT_FN_GRP; |
| 203 | pbdev->pci_group = s390_group_find(ZPCI_DEFAULT_FN_GRP); |
| 204 | return; |
| 205 | } |
| 206 | cap = (void *) hdr; |
| 207 | |
| 208 | /* |
| 209 | * For an intercept device, let's use an existing simulated group if one |
| 210 | * one was already created for other intercept devices in this group. |
| 211 | * If not, create a new simulated group if any are still available. |
| 212 | * If all else fails, just fall back on the default group. |
| 213 | */ |
| 214 | if (!pbdev->interp) { |
| 215 | pbdev->pci_group = s390_group_find_host_sim(pbdev->zpci_fn.pfgid); |
| 216 | if (pbdev->pci_group) { |
| 217 | /* Use existing simulated group */ |
| 218 | pbdev->zpci_fn.pfgid = pbdev->pci_group->id; |
| 219 | return; |
| 220 | } else { |
| 221 | if (s->next_sim_grp == ZPCI_DEFAULT_FN_GRP) { |
| 222 | /* All out of simulated groups, use default */ |
| 223 | trace_s390_pci_clp_cap(vpci->vbasedev.name, |
| 224 | VFIO_DEVICE_INFO_CAP_ZPCI_GROUP); |
| 225 | pbdev->zpci_fn.pfgid = ZPCI_DEFAULT_FN_GRP; |
| 226 | pbdev->pci_group = s390_group_find(ZPCI_DEFAULT_FN_GRP); |
| 227 | return; |
| 228 | } else { |
| 229 | /* We can assign a new simulated group */ |
| 230 | pbdev->zpci_fn.pfgid = s->next_sim_grp; |
| 231 | s->next_sim_grp++; |
| 232 | /* Fall through to create the new sim group using CLP info */ |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /* See if the PCI group is already defined, create if not */ |
| 238 | pbdev->pci_group = s390_group_find(pbdev->zpci_fn.pfgid); |
| 239 | |
| 240 | if (!pbdev->pci_group) { |
| 241 | pbdev->pci_group = s390_group_create(pbdev->zpci_fn.pfgid, start_gid); |
| 242 | |
| 243 | resgrp = &pbdev->pci_group->zpci_group; |
| 244 | if (pbdev->rtr_avail) { |
| 245 | resgrp->fr |= CLP_RSP_QPCIG_MASK_RTR; |
| 246 | } |
| 247 | if (cap->flags & VFIO_DEVICE_INFO_ZPCI_FLAG_REFRESH) { |
| 248 | resgrp->fr |= CLP_RSP_QPCIG_MASK_REFRESH; |
| 249 | } |
| 250 | resgrp->dasm = cap->dasm; |
| 251 | resgrp->msia = cap->msi_addr; |
| 252 | resgrp->mui = cap->mui; |
| 253 | resgrp->i = cap->noi; |
| 254 | if (pbdev->interp && hdr->version >= 2) { |
| 255 | resgrp->maxstbl = cap->imaxstbl; |
| 256 | } else { |
| 257 | resgrp->maxstbl = cap->maxstbl; |
| 258 | } |
| 259 | resgrp->version = cap->version; |
| 260 | resgrp->dtsm = ZPCI_DTSM; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | static void s390_pci_read_util(S390PCIBusDevice *pbdev, |
| 265 | struct vfio_device_info *info) |
| 266 | { |
| 267 | struct vfio_info_cap_header *hdr; |
| 268 | struct vfio_device_info_cap_zpci_util *cap; |
| 269 | VFIOPCIDevice *vpci = VFIO_PCI_DEVICE(pbdev->pdev); |
| 270 | |
| 271 | hdr = vfio_get_device_info_cap(info, VFIO_DEVICE_INFO_CAP_ZPCI_UTIL); |
| 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_UTIL); |
| 277 | return; |
| 278 | } |
| 279 | cap = (void *) hdr; |
| 280 | |
| 281 | if (cap->size > CLP_UTIL_STR_LEN) { |
| 282 | trace_s390_pci_clp_cap_size(vpci->vbasedev.name, cap->size, |
| 283 | VFIO_DEVICE_INFO_CAP_ZPCI_UTIL); |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | pbdev->zpci_fn.flags |= CLP_RSP_QPCI_MASK_UTIL; |
| 288 | memcpy(pbdev->zpci_fn.util_str, cap->util_str, CLP_UTIL_STR_LEN); |
| 289 | } |
| 290 | |
| 291 | static void s390_pci_read_pfip(S390PCIBusDevice *pbdev, |
| 292 | struct vfio_device_info *info) |
| 293 | { |
| 294 | struct vfio_info_cap_header *hdr; |
| 295 | struct vfio_device_info_cap_zpci_pfip *cap; |
| 296 | VFIOPCIDevice *vpci = VFIO_PCI_DEVICE(pbdev->pdev); |
| 297 | |
| 298 | hdr = vfio_get_device_info_cap(info, VFIO_DEVICE_INFO_CAP_ZPCI_PFIP); |
| 299 | |
| 300 | /* If capability not provided, just leave the defaults in place */ |
| 301 | if (hdr == NULL) { |
| 302 | trace_s390_pci_clp_cap(vpci->vbasedev.name, |
| 303 | VFIO_DEVICE_INFO_CAP_ZPCI_PFIP); |
| 304 | return; |
| 305 | } |
| 306 | cap = (void *) hdr; |
| 307 | |
| 308 | if (cap->size > CLP_PFIP_NR_SEGMENTS) { |
| 309 | trace_s390_pci_clp_cap_size(vpci->vbasedev.name, cap->size, |
| 310 | VFIO_DEVICE_INFO_CAP_ZPCI_PFIP); |
| 311 | return; |
| 312 | } |
| 313 | |
| 314 | memcpy(pbdev->zpci_fn.pfip, cap->pfip, CLP_PFIP_NR_SEGMENTS); |
| 315 | } |
| 316 | |
| 317 | static struct vfio_device_info *get_device_info(S390PCIBusDevice *pbdev) |
| 318 | { |
| 319 | VFIOPCIDevice *vfio_pci = VFIO_PCI_DEVICE(pbdev->pdev); |
| 320 | |
| 321 | return vfio_get_device_info(vfio_pci->vbasedev.fd); |
| 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); |
| 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); |
| 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 | } |