1 /* 2 * VFIO device 3 * 4 * Copyright Red Hat, Inc. 2012 5 * 6 * Authors: 7 * Alex Williamson <alex.williamson@redhat.com> 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2. See 10 * the COPYING file in the top-level directory. 11 * 12 * Based on qemu-kvm device-assignment: 13 * Adapted for KVM by Qumranet. 14 * Copyright (c) 2007, Neocleus, Alex Novik (alex@neocleus.com) 15 * Copyright (c) 2007, Neocleus, Guy Zana (guy@neocleus.com) 16 * Copyright (C) 2008, Qumranet, Amit Shah (amit.shah@qumranet.com) 17 * Copyright (C) 2008, Red Hat, Amit Shah (amit.shah@redhat.com) 18 * Copyright (C) 2008, IBM, Muli Ben-Yehuda (muli@il.ibm.com) 19 */ 20 21 #include "qemu/osdep.h" 22 #include <sys/ioctl.h> 23 24 #include "hw/vfio/vfio-device.h" 25 #include "hw/vfio/pci.h" 26 #include "hw/hw.h" 27 #include "trace.h" 28 #include "qapi/error.h" 29 #include "qemu/error-report.h" 30 #include "qemu/units.h" 31 #include "monitor/monitor.h" 32 #include "vfio-helpers.h" 33 34 VFIODeviceList vfio_device_list = 35 QLIST_HEAD_INITIALIZER(vfio_device_list); 36 37 /* 38 * We want to differentiate hot reset of multiple in-use devices vs 39 * hot reset of a single in-use device. VFIO_DEVICE_RESET will already 40 * handle the case of doing hot resets when there is only a single 41 * device per bus. The in-use here refers to how many VFIODevices are 42 * affected. A hot reset that affects multiple devices, but only a 43 * single in-use device, means that we can call it from our bus 44 * ->reset() callback since the extent is effectively a single 45 * device. This allows us to make use of it in the hotplug path. When 46 * there are multiple in-use devices, we can only trigger the hot 47 * reset during a system reset and thus from our reset handler. We 48 * separate _one vs _multi here so that we don't overlap and do a 49 * double reset on the system reset path where both our reset handler 50 * and ->reset() callback are used. Calling _one() will only do a hot 51 * reset for the one in-use devices case, calling _multi() will do 52 * nothing if a _one() would have been sufficient. 53 */ 54 void vfio_device_reset_handler(void *opaque) 55 { 56 VFIODevice *vbasedev; 57 58 trace_vfio_device_reset_handler(); 59 QLIST_FOREACH(vbasedev, &vfio_device_list, global_next) { 60 if (vbasedev->dev->realized) { 61 vbasedev->ops->vfio_compute_needs_reset(vbasedev); 62 } 63 } 64 65 QLIST_FOREACH(vbasedev, &vfio_device_list, global_next) { 66 if (vbasedev->dev->realized && vbasedev->needs_reset) { 67 vbasedev->ops->vfio_hot_reset_multi(vbasedev); 68 } 69 } 70 } 71 72 /* 73 * Common VFIO interrupt disable 74 */ 75 void vfio_device_irq_disable(VFIODevice *vbasedev, int index) 76 { 77 struct vfio_irq_set irq_set = { 78 .argsz = sizeof(irq_set), 79 .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_TRIGGER, 80 .index = index, 81 .start = 0, 82 .count = 0, 83 }; 84 85 ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, &irq_set); 86 } 87 88 void vfio_device_irq_unmask(VFIODevice *vbasedev, int index) 89 { 90 struct vfio_irq_set irq_set = { 91 .argsz = sizeof(irq_set), 92 .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK, 93 .index = index, 94 .start = 0, 95 .count = 1, 96 }; 97 98 ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, &irq_set); 99 } 100 101 void vfio_device_irq_mask(VFIODevice *vbasedev, int index) 102 { 103 struct vfio_irq_set irq_set = { 104 .argsz = sizeof(irq_set), 105 .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_MASK, 106 .index = index, 107 .start = 0, 108 .count = 1, 109 }; 110 111 ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, &irq_set); 112 } 113 114 static inline const char *action_to_str(int action) 115 { 116 switch (action) { 117 case VFIO_IRQ_SET_ACTION_MASK: 118 return "MASK"; 119 case VFIO_IRQ_SET_ACTION_UNMASK: 120 return "UNMASK"; 121 case VFIO_IRQ_SET_ACTION_TRIGGER: 122 return "TRIGGER"; 123 default: 124 return "UNKNOWN ACTION"; 125 } 126 } 127 128 static const char *index_to_str(VFIODevice *vbasedev, int index) 129 { 130 if (vbasedev->type != VFIO_DEVICE_TYPE_PCI) { 131 return NULL; 132 } 133 134 switch (index) { 135 case VFIO_PCI_INTX_IRQ_INDEX: 136 return "INTX"; 137 case VFIO_PCI_MSI_IRQ_INDEX: 138 return "MSI"; 139 case VFIO_PCI_MSIX_IRQ_INDEX: 140 return "MSIX"; 141 case VFIO_PCI_ERR_IRQ_INDEX: 142 return "ERR"; 143 case VFIO_PCI_REQ_IRQ_INDEX: 144 return "REQ"; 145 default: 146 return NULL; 147 } 148 } 149 150 bool vfio_device_irq_set_signaling(VFIODevice *vbasedev, int index, int subindex, 151 int action, int fd, Error **errp) 152 { 153 ERRP_GUARD(); 154 g_autofree struct vfio_irq_set *irq_set = NULL; 155 int argsz; 156 const char *name; 157 int32_t *pfd; 158 159 argsz = sizeof(*irq_set) + sizeof(*pfd); 160 161 irq_set = g_malloc0(argsz); 162 irq_set->argsz = argsz; 163 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | action; 164 irq_set->index = index; 165 irq_set->start = subindex; 166 irq_set->count = 1; 167 pfd = (int32_t *)&irq_set->data; 168 *pfd = fd; 169 170 if (!ioctl(vbasedev->fd, VFIO_DEVICE_SET_IRQS, irq_set)) { 171 return true; 172 } 173 174 error_setg_errno(errp, errno, "VFIO_DEVICE_SET_IRQS failure"); 175 176 name = index_to_str(vbasedev, index); 177 if (name) { 178 error_prepend(errp, "%s-%d: ", name, subindex); 179 } else { 180 error_prepend(errp, "index %d-%d: ", index, subindex); 181 } 182 error_prepend(errp, 183 "Failed to %s %s eventfd signaling for interrupt ", 184 fd < 0 ? "tear down" : "set up", action_to_str(action)); 185 return false; 186 } 187 188 int vfio_device_get_region_info(VFIODevice *vbasedev, int index, 189 struct vfio_region_info **info) 190 { 191 size_t argsz = sizeof(struct vfio_region_info); 192 193 *info = g_malloc0(argsz); 194 195 (*info)->index = index; 196 retry: 197 (*info)->argsz = argsz; 198 199 if (ioctl(vbasedev->fd, VFIO_DEVICE_GET_REGION_INFO, *info)) { 200 g_free(*info); 201 *info = NULL; 202 return -errno; 203 } 204 205 if ((*info)->argsz > argsz) { 206 argsz = (*info)->argsz; 207 *info = g_realloc(*info, argsz); 208 209 goto retry; 210 } 211 212 return 0; 213 } 214 215 int vfio_device_get_region_info_type(VFIODevice *vbasedev, uint32_t type, 216 uint32_t subtype, struct vfio_region_info **info) 217 { 218 int i; 219 220 for (i = 0; i < vbasedev->num_regions; i++) { 221 struct vfio_info_cap_header *hdr; 222 struct vfio_region_info_cap_type *cap_type; 223 224 if (vfio_device_get_region_info(vbasedev, i, info)) { 225 continue; 226 } 227 228 hdr = vfio_get_region_info_cap(*info, VFIO_REGION_INFO_CAP_TYPE); 229 if (!hdr) { 230 g_free(*info); 231 continue; 232 } 233 234 cap_type = container_of(hdr, struct vfio_region_info_cap_type, header); 235 236 trace_vfio_device_get_region_info_type(vbasedev->name, i, 237 cap_type->type, cap_type->subtype); 238 239 if (cap_type->type == type && cap_type->subtype == subtype) { 240 return 0; 241 } 242 243 g_free(*info); 244 } 245 246 *info = NULL; 247 return -ENODEV; 248 } 249 250 bool vfio_device_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type) 251 { 252 g_autofree struct vfio_region_info *info = NULL; 253 bool ret = false; 254 255 if (!vfio_device_get_region_info(vbasedev, region, &info)) { 256 if (vfio_get_region_info_cap(info, cap_type)) { 257 ret = true; 258 } 259 } 260 261 return ret; 262 } 263 264 bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp) 265 { 266 ERRP_GUARD(); 267 struct stat st; 268 269 if (vbasedev->fd < 0) { 270 if (stat(vbasedev->sysfsdev, &st) < 0) { 271 error_setg_errno(errp, errno, "no such host device"); 272 error_prepend(errp, VFIO_MSG_PREFIX, vbasedev->sysfsdev); 273 return false; 274 } 275 /* User may specify a name, e.g: VFIO platform device */ 276 if (!vbasedev->name) { 277 vbasedev->name = g_path_get_basename(vbasedev->sysfsdev); 278 } 279 } else { 280 if (!vbasedev->iommufd) { 281 error_setg(errp, "Use FD passing only with iommufd backend"); 282 return false; 283 } 284 /* 285 * Give a name with fd so any function printing out vbasedev->name 286 * will not break. 287 */ 288 if (!vbasedev->name) { 289 vbasedev->name = g_strdup_printf("VFIO_FD%d", vbasedev->fd); 290 } 291 } 292 293 return true; 294 } 295 296 void vfio_device_set_fd(VFIODevice *vbasedev, const char *str, Error **errp) 297 { 298 ERRP_GUARD(); 299 int fd = monitor_fd_param(monitor_cur(), str, errp); 300 301 if (fd < 0) { 302 error_prepend(errp, "Could not parse remote object fd %s:", str); 303 return; 304 } 305 vbasedev->fd = fd; 306 } 307 308 void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops, 309 DeviceState *dev, bool ram_discard) 310 { 311 vbasedev->type = type; 312 vbasedev->ops = ops; 313 vbasedev->dev = dev; 314 vbasedev->fd = -1; 315 316 vbasedev->ram_block_discard_allowed = ram_discard; 317 } 318 319 int vfio_device_get_aw_bits(VFIODevice *vdev) 320 { 321 /* 322 * iova_ranges is a sorted list. For old kernels that support 323 * VFIO but not support query of iova ranges, iova_ranges is NULL, 324 * in this case HOST_IOMMU_DEVICE_CAP_AW_BITS_MAX(64) is returned. 325 */ 326 GList *l = g_list_last(vdev->bcontainer->iova_ranges); 327 328 if (l) { 329 Range *range = l->data; 330 return range_get_last_bit(range) + 1; 331 } 332 333 return HOST_IOMMU_DEVICE_CAP_AW_BITS_MAX; 334 } 335 336 bool vfio_device_is_mdev(VFIODevice *vbasedev) 337 { 338 g_autofree char *subsys = NULL; 339 g_autofree char *tmp = NULL; 340 341 if (!vbasedev->sysfsdev) { 342 return false; 343 } 344 345 tmp = g_strdup_printf("%s/subsystem", vbasedev->sysfsdev); 346 subsys = realpath(tmp, NULL); 347 return subsys && (strcmp(subsys, "/sys/bus/mdev") == 0); 348 } 349 350 bool vfio_device_hiod_create_and_realize(VFIODevice *vbasedev, 351 const char *typename, Error **errp) 352 { 353 HostIOMMUDevice *hiod; 354 355 if (vbasedev->mdev) { 356 return true; 357 } 358 359 hiod = HOST_IOMMU_DEVICE(object_new(typename)); 360 361 if (!HOST_IOMMU_DEVICE_GET_CLASS(hiod)->realize(hiod, vbasedev, errp)) { 362 object_unref(hiod); 363 return false; 364 } 365 366 vbasedev->hiod = hiod; 367 return true; 368 } 369 370 VFIODevice *vfio_get_vfio_device(Object *obj) 371 { 372 if (object_dynamic_cast(obj, TYPE_VFIO_PCI)) { 373 return &VFIO_PCI(obj)->vbasedev; 374 } else { 375 return NULL; 376 } 377 } 378 379 bool vfio_device_attach(char *name, VFIODevice *vbasedev, 380 AddressSpace *as, Error **errp) 381 { 382 const VFIOIOMMUClass *ops = 383 VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_LEGACY)); 384 385 if (vbasedev->iommufd) { 386 ops = VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD)); 387 } 388 389 assert(ops); 390 391 return ops->attach_device(name, vbasedev, as, errp); 392 } 393 394 void vfio_device_detach(VFIODevice *vbasedev) 395 { 396 if (!vbasedev->bcontainer) { 397 return; 398 } 399 VFIO_IOMMU_GET_CLASS(vbasedev->bcontainer)->detach_device(vbasedev); 400 } 401