1 /* 2 * VFIO based AP matrix device assignment 3 * 4 * Copyright 2018 IBM Corp. 5 * Author(s): Tony Krowiak <akrowiak@linux.ibm.com> 6 * Halil Pasic <pasic@linux.ibm.com> 7 * 8 * This work is licensed under the terms of the GNU GPL, version 2 or (at 9 * your option) any later version. See the COPYING file in the top-level 10 * directory. 11 */ 12 13 #include "qemu/osdep.h" 14 #include <linux/vfio.h> 15 #include <sys/ioctl.h> 16 #include "qapi/error.h" 17 #include "hw/vfio/vfio.h" 18 #include "hw/vfio/vfio-common.h" 19 #include "hw/s390x/ap-device.h" 20 #include "qemu/error-report.h" 21 #include "qemu/event_notifier.h" 22 #include "qemu/main-loop.h" 23 #include "qemu/module.h" 24 #include "qemu/option.h" 25 #include "qemu/config-file.h" 26 #include "kvm/kvm_s390x.h" 27 #include "migration/vmstate.h" 28 #include "hw/qdev-properties.h" 29 #include "hw/s390x/ap-bridge.h" 30 #include "exec/address-spaces.h" 31 #include "qom/object.h" 32 33 #define TYPE_VFIO_AP_DEVICE "vfio-ap" 34 35 struct VFIOAPDevice { 36 APDevice apdev; 37 VFIODevice vdev; 38 EventNotifier req_notifier; 39 }; 40 41 OBJECT_DECLARE_SIMPLE_TYPE(VFIOAPDevice, VFIO_AP_DEVICE) 42 43 static void vfio_ap_compute_needs_reset(VFIODevice *vdev) 44 { 45 vdev->needs_reset = false; 46 } 47 48 /* 49 * We don't need vfio_hot_reset_multi and vfio_eoi operations for 50 * vfio-ap device now. 51 */ 52 struct VFIODeviceOps vfio_ap_ops = { 53 .vfio_compute_needs_reset = vfio_ap_compute_needs_reset, 54 }; 55 56 static void vfio_ap_put_device(VFIOAPDevice *vapdev) 57 { 58 g_free(vapdev->vdev.name); 59 vfio_put_base_device(&vapdev->vdev); 60 } 61 62 static VFIOGroup *vfio_ap_get_group(VFIOAPDevice *vapdev, Error **errp) 63 { 64 GError *gerror = NULL; 65 char *symlink, *group_path; 66 int groupid; 67 68 symlink = g_strdup_printf("%s/iommu_group", vapdev->vdev.sysfsdev); 69 group_path = g_file_read_link(symlink, &gerror); 70 g_free(symlink); 71 72 if (!group_path) { 73 error_setg(errp, "%s: no iommu_group found for %s: %s", 74 TYPE_VFIO_AP_DEVICE, vapdev->vdev.sysfsdev, gerror->message); 75 g_error_free(gerror); 76 return NULL; 77 } 78 79 if (sscanf(basename(group_path), "%d", &groupid) != 1) { 80 error_setg(errp, "vfio: failed to read %s", group_path); 81 g_free(group_path); 82 return NULL; 83 } 84 85 g_free(group_path); 86 87 return vfio_get_group(groupid, &address_space_memory, errp); 88 } 89 90 static void vfio_ap_req_notifier_handler(void *opaque) 91 { 92 VFIOAPDevice *vapdev = opaque; 93 Error *err = NULL; 94 95 if (!event_notifier_test_and_clear(&vapdev->req_notifier)) { 96 return; 97 } 98 99 qdev_unplug(DEVICE(vapdev), &err); 100 101 if (err) { 102 warn_reportf_err(err, VFIO_MSG_PREFIX, vapdev->vdev.name); 103 } 104 } 105 106 static void vfio_ap_register_irq_notifier(VFIOAPDevice *vapdev, 107 unsigned int irq, Error **errp) 108 { 109 int fd; 110 size_t argsz; 111 IOHandler *fd_read; 112 EventNotifier *notifier; 113 struct vfio_irq_info *irq_info; 114 VFIODevice *vdev = &vapdev->vdev; 115 116 switch (irq) { 117 case VFIO_AP_REQ_IRQ_INDEX: 118 notifier = &vapdev->req_notifier; 119 fd_read = vfio_ap_req_notifier_handler; 120 break; 121 default: 122 error_setg(errp, "vfio: Unsupported device irq(%d)", irq); 123 return; 124 } 125 126 if (vdev->num_irqs < irq + 1) { 127 error_setg(errp, "vfio: IRQ %u not available (number of irqs %u)", 128 irq, vdev->num_irqs); 129 return; 130 } 131 132 argsz = sizeof(*irq_info); 133 irq_info = g_malloc0(argsz); 134 irq_info->index = irq; 135 irq_info->argsz = argsz; 136 137 if (ioctl(vdev->fd, VFIO_DEVICE_GET_IRQ_INFO, 138 irq_info) < 0 || irq_info->count < 1) { 139 error_setg_errno(errp, errno, "vfio: Error getting irq info"); 140 goto out_free_info; 141 } 142 143 if (event_notifier_init(notifier, 0)) { 144 error_setg_errno(errp, errno, 145 "vfio: Unable to init event notifier for irq (%d)", 146 irq); 147 goto out_free_info; 148 } 149 150 fd = event_notifier_get_fd(notifier); 151 qemu_set_fd_handler(fd, fd_read, NULL, vapdev); 152 153 if (vfio_set_irq_signaling(vdev, irq, 0, VFIO_IRQ_SET_ACTION_TRIGGER, fd, 154 errp)) { 155 qemu_set_fd_handler(fd, NULL, NULL, vapdev); 156 event_notifier_cleanup(notifier); 157 } 158 159 out_free_info: 160 g_free(irq_info); 161 162 } 163 164 static void vfio_ap_unregister_irq_notifier(VFIOAPDevice *vapdev, 165 unsigned int irq) 166 { 167 Error *err = NULL; 168 EventNotifier *notifier; 169 170 switch (irq) { 171 case VFIO_AP_REQ_IRQ_INDEX: 172 notifier = &vapdev->req_notifier; 173 break; 174 default: 175 error_report("vfio: Unsupported device irq(%d)", irq); 176 return; 177 } 178 179 if (vfio_set_irq_signaling(&vapdev->vdev, irq, 0, 180 VFIO_IRQ_SET_ACTION_TRIGGER, -1, &err)) { 181 warn_reportf_err(err, VFIO_MSG_PREFIX, vapdev->vdev.name); 182 } 183 184 qemu_set_fd_handler(event_notifier_get_fd(notifier), 185 NULL, NULL, vapdev); 186 event_notifier_cleanup(notifier); 187 } 188 189 static void vfio_ap_realize(DeviceState *dev, Error **errp) 190 { 191 int ret; 192 char *mdevid; 193 Error *err = NULL; 194 VFIOGroup *vfio_group; 195 APDevice *apdev = AP_DEVICE(dev); 196 VFIOAPDevice *vapdev = VFIO_AP_DEVICE(apdev); 197 198 vfio_group = vfio_ap_get_group(vapdev, errp); 199 if (!vfio_group) { 200 return; 201 } 202 203 vapdev->vdev.ops = &vfio_ap_ops; 204 vapdev->vdev.type = VFIO_DEVICE_TYPE_AP; 205 mdevid = basename(vapdev->vdev.sysfsdev); 206 vapdev->vdev.name = g_strdup_printf("%s", mdevid); 207 vapdev->vdev.dev = dev; 208 209 /* 210 * vfio-ap devices operate in a way compatible with discarding of 211 * memory in RAM blocks, as no pages are pinned in the host. 212 * This needs to be set before vfio_get_device() for vfio common to 213 * handle ram_block_discard_disable(). 214 */ 215 vapdev->vdev.ram_block_discard_allowed = true; 216 217 ret = vfio_get_device(vfio_group, mdevid, &vapdev->vdev, errp); 218 if (ret) { 219 goto out_get_dev_err; 220 } 221 222 vfio_ap_register_irq_notifier(vapdev, VFIO_AP_REQ_IRQ_INDEX, &err); 223 if (err) { 224 /* 225 * Report this error, but do not make it a failing condition. 226 * Lack of this IRQ in the host does not prevent normal operation. 227 */ 228 error_report_err(err); 229 } 230 231 return; 232 233 out_get_dev_err: 234 vfio_ap_put_device(vapdev); 235 vfio_put_group(vfio_group); 236 } 237 238 static void vfio_ap_unrealize(DeviceState *dev) 239 { 240 APDevice *apdev = AP_DEVICE(dev); 241 VFIOAPDevice *vapdev = VFIO_AP_DEVICE(apdev); 242 VFIOGroup *group = vapdev->vdev.group; 243 244 vfio_ap_unregister_irq_notifier(vapdev, VFIO_AP_REQ_IRQ_INDEX); 245 vfio_ap_put_device(vapdev); 246 vfio_put_group(group); 247 } 248 249 static Property vfio_ap_properties[] = { 250 DEFINE_PROP_STRING("sysfsdev", VFIOAPDevice, vdev.sysfsdev), 251 DEFINE_PROP_END_OF_LIST(), 252 }; 253 254 static void vfio_ap_reset(DeviceState *dev) 255 { 256 int ret; 257 APDevice *apdev = AP_DEVICE(dev); 258 VFIOAPDevice *vapdev = VFIO_AP_DEVICE(apdev); 259 260 ret = ioctl(vapdev->vdev.fd, VFIO_DEVICE_RESET); 261 if (ret) { 262 error_report("%s: failed to reset %s device: %s", __func__, 263 vapdev->vdev.name, strerror(errno)); 264 } 265 } 266 267 static const VMStateDescription vfio_ap_vmstate = { 268 .name = "vfio-ap", 269 .unmigratable = 1, 270 }; 271 272 static void vfio_ap_class_init(ObjectClass *klass, void *data) 273 { 274 DeviceClass *dc = DEVICE_CLASS(klass); 275 276 device_class_set_props(dc, vfio_ap_properties); 277 dc->vmsd = &vfio_ap_vmstate; 278 dc->desc = "VFIO-based AP device assignment"; 279 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 280 dc->realize = vfio_ap_realize; 281 dc->unrealize = vfio_ap_unrealize; 282 dc->hotpluggable = true; 283 dc->reset = vfio_ap_reset; 284 dc->bus_type = TYPE_AP_BUS; 285 } 286 287 static const TypeInfo vfio_ap_info = { 288 .name = TYPE_VFIO_AP_DEVICE, 289 .parent = TYPE_AP_DEVICE, 290 .instance_size = sizeof(VFIOAPDevice), 291 .class_init = vfio_ap_class_init, 292 }; 293 294 static void vfio_ap_type_init(void) 295 { 296 type_register_static(&vfio_ap_info); 297 } 298 299 type_init(vfio_ap_type_init) 300