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 <sys/ioctl.h> 13 #include <linux/vfio.h> 14 #include <linux/vfio_zdev.h> 15 16 #include "qemu/osdep.h" 17 #include "trace.h" 18 #include "hw/s390x/s390-pci-bus.h" 19 #include "hw/s390x/s390-pci-clp.h" 20 #include "hw/s390x/s390-pci-vfio.h" 21 #include "hw/vfio/pci.h" 22 #include "hw/vfio/vfio-common.h" 23 24 /* 25 * Get the current DMA available count from vfio. Returns true if vfio is 26 * limiting DMA requests, false otherwise. The current available count read 27 * from vfio is returned in avail. 28 */ 29 bool s390_pci_update_dma_avail(int fd, unsigned int *avail) 30 { 31 g_autofree struct vfio_iommu_type1_info *info; 32 uint32_t argsz; 33 34 assert(avail); 35 36 argsz = sizeof(struct vfio_iommu_type1_info); 37 info = g_malloc0(argsz); 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 = container_of(pbdev->pdev, VFIOPCIDevice, pdev); 67 int id; 68 69 assert(vpdev); 70 71 id = vpdev->vbasedev.group->container->fd; 72 73 if (!s390_pci_update_dma_avail(id, &avail)) { 74 return NULL; 75 } 76 77 QTAILQ_FOREACH(cnt, &s->zpci_dma_limit, link) { 78 if (cnt->id == id) { 79 cnt->users++; 80 return cnt; 81 } 82 } 83 84 cnt = g_new0(S390PCIDMACount, 1); 85 cnt->id = id; 86 cnt->users = 1; 87 cnt->avail = avail; 88 QTAILQ_INSERT_TAIL(&s->zpci_dma_limit, cnt, link); 89 return cnt; 90 } 91 92 void s390_pci_end_dma_count(S390pciState *s, S390PCIDMACount *cnt) 93 { 94 assert(cnt); 95 96 cnt->users--; 97 if (cnt->users == 0) { 98 QTAILQ_REMOVE(&s->zpci_dma_limit, cnt, link); 99 } 100 } 101 102 static void s390_pci_read_base(S390PCIBusDevice *pbdev, 103 struct vfio_device_info *info) 104 { 105 struct vfio_info_cap_header *hdr; 106 struct vfio_device_info_cap_zpci_base *cap; 107 VFIOPCIDevice *vpci = container_of(pbdev->pdev, VFIOPCIDevice, pdev); 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 static void s390_pci_read_group(S390PCIBusDevice *pbdev, 130 struct vfio_device_info *info) 131 { 132 struct vfio_info_cap_header *hdr; 133 struct vfio_device_info_cap_zpci_group *cap; 134 ClpRspQueryPciGrp *resgrp; 135 VFIOPCIDevice *vpci = container_of(pbdev->pdev, VFIOPCIDevice, pdev); 136 137 hdr = vfio_get_device_info_cap(info, VFIO_DEVICE_INFO_CAP_ZPCI_GROUP); 138 139 /* If capability not provided, just use the default group */ 140 if (hdr == NULL) { 141 trace_s390_pci_clp_cap(vpci->vbasedev.name, 142 VFIO_DEVICE_INFO_CAP_ZPCI_GROUP); 143 pbdev->zpci_fn.pfgid = ZPCI_DEFAULT_FN_GRP; 144 pbdev->pci_group = s390_group_find(ZPCI_DEFAULT_FN_GRP); 145 return; 146 } 147 cap = (void *) hdr; 148 149 /* See if the PCI group is already defined, create if not */ 150 pbdev->pci_group = s390_group_find(pbdev->zpci_fn.pfgid); 151 152 if (!pbdev->pci_group) { 153 pbdev->pci_group = s390_group_create(pbdev->zpci_fn.pfgid); 154 155 resgrp = &pbdev->pci_group->zpci_group; 156 if (cap->flags & VFIO_DEVICE_INFO_ZPCI_FLAG_REFRESH) { 157 resgrp->fr = 1; 158 } 159 stq_p(&resgrp->dasm, cap->dasm); 160 stq_p(&resgrp->msia, cap->msi_addr); 161 stw_p(&resgrp->mui, cap->mui); 162 stw_p(&resgrp->i, cap->noi); 163 stw_p(&resgrp->maxstbl, cap->maxstbl); 164 stb_p(&resgrp->version, cap->version); 165 } 166 } 167 168 static void s390_pci_read_util(S390PCIBusDevice *pbdev, 169 struct vfio_device_info *info) 170 { 171 struct vfio_info_cap_header *hdr; 172 struct vfio_device_info_cap_zpci_util *cap; 173 VFIOPCIDevice *vpci = container_of(pbdev->pdev, VFIOPCIDevice, pdev); 174 175 hdr = vfio_get_device_info_cap(info, VFIO_DEVICE_INFO_CAP_ZPCI_UTIL); 176 177 /* If capability not provided, just leave the defaults in place */ 178 if (hdr == NULL) { 179 trace_s390_pci_clp_cap(vpci->vbasedev.name, 180 VFIO_DEVICE_INFO_CAP_ZPCI_UTIL); 181 return; 182 } 183 cap = (void *) hdr; 184 185 if (cap->size > CLP_UTIL_STR_LEN) { 186 trace_s390_pci_clp_cap_size(vpci->vbasedev.name, cap->size, 187 VFIO_DEVICE_INFO_CAP_ZPCI_UTIL); 188 return; 189 } 190 191 pbdev->zpci_fn.flags |= CLP_RSP_QPCI_MASK_UTIL; 192 memcpy(pbdev->zpci_fn.util_str, cap->util_str, CLP_UTIL_STR_LEN); 193 } 194 195 static void s390_pci_read_pfip(S390PCIBusDevice *pbdev, 196 struct vfio_device_info *info) 197 { 198 struct vfio_info_cap_header *hdr; 199 struct vfio_device_info_cap_zpci_pfip *cap; 200 VFIOPCIDevice *vpci = container_of(pbdev->pdev, VFIOPCIDevice, pdev); 201 202 hdr = vfio_get_device_info_cap(info, VFIO_DEVICE_INFO_CAP_ZPCI_PFIP); 203 204 /* If capability not provided, just leave the defaults in place */ 205 if (hdr == NULL) { 206 trace_s390_pci_clp_cap(vpci->vbasedev.name, 207 VFIO_DEVICE_INFO_CAP_ZPCI_PFIP); 208 return; 209 } 210 cap = (void *) hdr; 211 212 if (cap->size > CLP_PFIP_NR_SEGMENTS) { 213 trace_s390_pci_clp_cap_size(vpci->vbasedev.name, cap->size, 214 VFIO_DEVICE_INFO_CAP_ZPCI_PFIP); 215 return; 216 } 217 218 memcpy(pbdev->zpci_fn.pfip, cap->pfip, CLP_PFIP_NR_SEGMENTS); 219 } 220 221 /* 222 * This function will issue the VFIO_DEVICE_GET_INFO ioctl and look for 223 * capabilities that contain information about CLP features provided by the 224 * underlying host. 225 * On entry, defaults have already been placed into the guest CLP response 226 * buffers. On exit, defaults will have been overwritten for any CLP features 227 * found in the capability chain; defaults will remain for any CLP features not 228 * found in the chain. 229 */ 230 void s390_pci_get_clp_info(S390PCIBusDevice *pbdev) 231 { 232 g_autofree struct vfio_device_info *info; 233 VFIOPCIDevice *vfio_pci; 234 uint32_t argsz; 235 int fd; 236 237 argsz = sizeof(*info); 238 info = g_malloc0(argsz); 239 240 vfio_pci = container_of(pbdev->pdev, VFIOPCIDevice, pdev); 241 fd = vfio_pci->vbasedev.fd; 242 243 /* 244 * If the specified argsz is not large enough to contain all capabilities 245 * it will be updated upon return from the ioctl. Retry until we have 246 * a big enough buffer to hold the entire capability chain. On error, 247 * just exit and rely on CLP defaults. 248 */ 249 retry: 250 info->argsz = argsz; 251 252 if (ioctl(fd, VFIO_DEVICE_GET_INFO, info)) { 253 trace_s390_pci_clp_dev_info(vfio_pci->vbasedev.name); 254 return; 255 } 256 257 if (info->argsz > argsz) { 258 argsz = info->argsz; 259 info = g_realloc(info, argsz); 260 goto retry; 261 } 262 263 /* 264 * Find the CLP features provided and fill in the guest CLP responses. 265 * Always call s390_pci_read_base first as information from this could 266 * determine which function group is used in s390_pci_read_group. 267 * For any feature not found, the default values will remain in the CLP 268 * response. 269 */ 270 s390_pci_read_base(pbdev, info); 271 s390_pci_read_group(pbdev, info); 272 s390_pci_read_util(pbdev, info); 273 s390_pci_read_pfip(pbdev, info); 274 275 return; 276 } 277