hw/ppc: Drop useless CONFIG_KVM ifdefery
[qemu/ar7.git] / hw / vfio / ccw.c
blob03a2becb3ec98095b8068627b017ac4abc6eb6a3
1 /*
2 * vfio based subchannel assignment support
4 * Copyright 2017 IBM Corp.
5 * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com>
6 * Xiao Feng Ren <renxiaof@linux.vnet.ibm.com>
7 * Pierre Morel <pmorel@linux.vnet.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or (at
10 * your option) any later version. See the COPYING file in the top-level
11 * directory.
14 #include "qemu/osdep.h"
15 #include <linux/vfio.h>
16 #include <linux/vfio_ccw.h>
17 #include <sys/ioctl.h>
19 #include "qapi/error.h"
20 #include "hw/sysbus.h"
21 #include "hw/vfio/vfio.h"
22 #include "hw/vfio/vfio-common.h"
23 #include "hw/s390x/s390-ccw.h"
24 #include "hw/s390x/vfio-ccw.h"
25 #include "hw/s390x/ccw-device.h"
26 #include "exec/address-spaces.h"
27 #include "qemu/error-report.h"
28 #include "qemu/module.h"
30 struct VFIOCCWDevice {
31 S390CCWDevice cdev;
32 VFIODevice vdev;
33 uint64_t io_region_size;
34 uint64_t io_region_offset;
35 struct ccw_io_region *io_region;
36 EventNotifier io_notifier;
37 bool force_orb_pfch;
38 bool warned_orb_pfch;
41 static inline void warn_once_pfch(VFIOCCWDevice *vcdev, SubchDev *sch,
42 const char *msg)
44 warn_report_once_cond(&vcdev->warned_orb_pfch,
45 "vfio-ccw (devno %x.%x.%04x): %s",
46 sch->cssid, sch->ssid, sch->devno, msg);
49 static void vfio_ccw_compute_needs_reset(VFIODevice *vdev)
51 vdev->needs_reset = false;
55 * We don't need vfio_hot_reset_multi and vfio_eoi operations for
56 * vfio_ccw device now.
58 struct VFIODeviceOps vfio_ccw_ops = {
59 .vfio_compute_needs_reset = vfio_ccw_compute_needs_reset,
62 static IOInstEnding vfio_ccw_handle_request(SubchDev *sch)
64 S390CCWDevice *cdev = sch->driver_data;
65 VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev);
66 struct ccw_io_region *region = vcdev->io_region;
67 int ret;
69 if (!(sch->orb.ctrl0 & ORB_CTRL0_MASK_PFCH)) {
70 if (!(vcdev->force_orb_pfch)) {
71 warn_once_pfch(vcdev, sch, "requires PFCH flag set");
72 sch_gen_unit_exception(sch);
73 css_inject_io_interrupt(sch);
74 return IOINST_CC_EXPECTED;
75 } else {
76 sch->orb.ctrl0 |= ORB_CTRL0_MASK_PFCH;
77 warn_once_pfch(vcdev, sch, "PFCH flag forced");
81 QEMU_BUILD_BUG_ON(sizeof(region->orb_area) != sizeof(ORB));
82 QEMU_BUILD_BUG_ON(sizeof(region->scsw_area) != sizeof(SCSW));
83 QEMU_BUILD_BUG_ON(sizeof(region->irb_area) != sizeof(IRB));
85 memset(region, 0, sizeof(*region));
87 memcpy(region->orb_area, &sch->orb, sizeof(ORB));
88 memcpy(region->scsw_area, &sch->curr_status.scsw, sizeof(SCSW));
90 again:
91 ret = pwrite(vcdev->vdev.fd, region,
92 vcdev->io_region_size, vcdev->io_region_offset);
93 if (ret != vcdev->io_region_size) {
94 if (errno == EAGAIN) {
95 goto again;
97 error_report("vfio-ccw: wirte I/O region failed with errno=%d", errno);
98 ret = -errno;
99 } else {
100 ret = region->ret_code;
102 switch (ret) {
103 case 0:
104 return IOINST_CC_EXPECTED;
105 case -EBUSY:
106 return IOINST_CC_BUSY;
107 case -ENODEV:
108 case -EACCES:
109 return IOINST_CC_NOT_OPERATIONAL;
110 case -EFAULT:
111 default:
112 sch_gen_unit_exception(sch);
113 css_inject_io_interrupt(sch);
114 return IOINST_CC_EXPECTED;
118 static void vfio_ccw_reset(DeviceState *dev)
120 CcwDevice *ccw_dev = DO_UPCAST(CcwDevice, parent_obj, dev);
121 S390CCWDevice *cdev = DO_UPCAST(S390CCWDevice, parent_obj, ccw_dev);
122 VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev);
124 ioctl(vcdev->vdev.fd, VFIO_DEVICE_RESET);
127 static void vfio_ccw_io_notifier_handler(void *opaque)
129 VFIOCCWDevice *vcdev = opaque;
130 struct ccw_io_region *region = vcdev->io_region;
131 S390CCWDevice *cdev = S390_CCW_DEVICE(vcdev);
132 CcwDevice *ccw_dev = CCW_DEVICE(cdev);
133 SubchDev *sch = ccw_dev->sch;
134 SCHIB *schib = &sch->curr_status;
135 SCSW s;
136 IRB irb;
137 int size;
139 if (!event_notifier_test_and_clear(&vcdev->io_notifier)) {
140 return;
143 size = pread(vcdev->vdev.fd, region, vcdev->io_region_size,
144 vcdev->io_region_offset);
145 if (size == -1) {
146 switch (errno) {
147 case ENODEV:
148 /* Generate a deferred cc 3 condition. */
149 schib->scsw.flags |= SCSW_FLAGS_MASK_CC;
150 schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL;
151 schib->scsw.ctrl |= (SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND);
152 goto read_err;
153 case EFAULT:
154 /* Memory problem, generate channel data check. */
155 schib->scsw.ctrl &= ~SCSW_ACTL_START_PEND;
156 schib->scsw.cstat = SCSW_CSTAT_DATA_CHECK;
157 schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL;
158 schib->scsw.ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY |
159 SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND;
160 goto read_err;
161 default:
162 /* Error, generate channel program check. */
163 schib->scsw.ctrl &= ~SCSW_ACTL_START_PEND;
164 schib->scsw.cstat = SCSW_CSTAT_PROG_CHECK;
165 schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL;
166 schib->scsw.ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY |
167 SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND;
168 goto read_err;
170 } else if (size != vcdev->io_region_size) {
171 /* Information transfer error, generate channel-control check. */
172 schib->scsw.ctrl &= ~SCSW_ACTL_START_PEND;
173 schib->scsw.cstat = SCSW_CSTAT_CHN_CTRL_CHK;
174 schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL;
175 schib->scsw.ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY |
176 SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND;
177 goto read_err;
180 memcpy(&irb, region->irb_area, sizeof(IRB));
182 /* Update control block via irb. */
183 s = schib->scsw;
184 copy_scsw_to_guest(&s, &irb.scsw);
185 schib->scsw = s;
187 /* If a uint check is pending, copy sense data. */
188 if ((schib->scsw.dstat & SCSW_DSTAT_UNIT_CHECK) &&
189 (schib->pmcw.chars & PMCW_CHARS_MASK_CSENSE)) {
190 memcpy(sch->sense_data, irb.ecw, sizeof(irb.ecw));
193 read_err:
194 css_inject_io_interrupt(sch);
197 static void vfio_ccw_register_io_notifier(VFIOCCWDevice *vcdev, Error **errp)
199 VFIODevice *vdev = &vcdev->vdev;
200 struct vfio_irq_info *irq_info;
201 struct vfio_irq_set *irq_set;
202 size_t argsz;
203 int32_t *pfd;
205 if (vdev->num_irqs < VFIO_CCW_IO_IRQ_INDEX + 1) {
206 error_setg(errp, "vfio: unexpected number of io irqs %u",
207 vdev->num_irqs);
208 return;
211 argsz = sizeof(*irq_info);
212 irq_info = g_malloc0(argsz);
213 irq_info->index = VFIO_CCW_IO_IRQ_INDEX;
214 irq_info->argsz = argsz;
215 if (ioctl(vdev->fd, VFIO_DEVICE_GET_IRQ_INFO,
216 irq_info) < 0 || irq_info->count < 1) {
217 error_setg_errno(errp, errno, "vfio: Error getting irq info");
218 goto out_free_info;
221 if (event_notifier_init(&vcdev->io_notifier, 0)) {
222 error_setg_errno(errp, errno,
223 "vfio: Unable to init event notifier for IO");
224 goto out_free_info;
227 argsz = sizeof(*irq_set) + sizeof(*pfd);
228 irq_set = g_malloc0(argsz);
229 irq_set->argsz = argsz;
230 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
231 VFIO_IRQ_SET_ACTION_TRIGGER;
232 irq_set->index = VFIO_CCW_IO_IRQ_INDEX;
233 irq_set->start = 0;
234 irq_set->count = 1;
235 pfd = (int32_t *) &irq_set->data;
237 *pfd = event_notifier_get_fd(&vcdev->io_notifier);
238 qemu_set_fd_handler(*pfd, vfio_ccw_io_notifier_handler, NULL, vcdev);
239 if (ioctl(vdev->fd, VFIO_DEVICE_SET_IRQS, irq_set)) {
240 error_setg(errp, "vfio: Failed to set up io notification");
241 qemu_set_fd_handler(*pfd, NULL, NULL, vcdev);
242 event_notifier_cleanup(&vcdev->io_notifier);
245 g_free(irq_set);
247 out_free_info:
248 g_free(irq_info);
251 static void vfio_ccw_unregister_io_notifier(VFIOCCWDevice *vcdev)
253 struct vfio_irq_set *irq_set;
254 size_t argsz;
255 int32_t *pfd;
257 argsz = sizeof(*irq_set) + sizeof(*pfd);
258 irq_set = g_malloc0(argsz);
259 irq_set->argsz = argsz;
260 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
261 VFIO_IRQ_SET_ACTION_TRIGGER;
262 irq_set->index = VFIO_CCW_IO_IRQ_INDEX;
263 irq_set->start = 0;
264 irq_set->count = 1;
265 pfd = (int32_t *) &irq_set->data;
266 *pfd = -1;
268 if (ioctl(vcdev->vdev.fd, VFIO_DEVICE_SET_IRQS, irq_set)) {
269 error_report("vfio: Failed to de-assign device io fd: %m");
272 qemu_set_fd_handler(event_notifier_get_fd(&vcdev->io_notifier),
273 NULL, NULL, vcdev);
274 event_notifier_cleanup(&vcdev->io_notifier);
276 g_free(irq_set);
279 static void vfio_ccw_get_region(VFIOCCWDevice *vcdev, Error **errp)
281 VFIODevice *vdev = &vcdev->vdev;
282 struct vfio_region_info *info;
283 int ret;
285 /* Sanity check device */
286 if (!(vdev->flags & VFIO_DEVICE_FLAGS_CCW)) {
287 error_setg(errp, "vfio: Um, this isn't a vfio-ccw device");
288 return;
291 if (vdev->num_regions < VFIO_CCW_CONFIG_REGION_INDEX + 1) {
292 error_setg(errp, "vfio: Unexpected number of the I/O region %u",
293 vdev->num_regions);
294 return;
297 ret = vfio_get_region_info(vdev, VFIO_CCW_CONFIG_REGION_INDEX, &info);
298 if (ret) {
299 error_setg_errno(errp, -ret, "vfio: Error getting config info");
300 return;
303 vcdev->io_region_size = info->size;
304 if (sizeof(*vcdev->io_region) != vcdev->io_region_size) {
305 error_setg(errp, "vfio: Unexpected size of the I/O region");
306 g_free(info);
307 return;
310 vcdev->io_region_offset = info->offset;
311 vcdev->io_region = g_malloc0(info->size);
313 g_free(info);
316 static void vfio_ccw_put_region(VFIOCCWDevice *vcdev)
318 g_free(vcdev->io_region);
321 static void vfio_ccw_put_device(VFIOCCWDevice *vcdev)
323 g_free(vcdev->vdev.name);
324 vfio_put_base_device(&vcdev->vdev);
327 static void vfio_ccw_get_device(VFIOGroup *group, VFIOCCWDevice *vcdev,
328 Error **errp)
330 char *name = g_strdup_printf("%x.%x.%04x", vcdev->cdev.hostid.cssid,
331 vcdev->cdev.hostid.ssid,
332 vcdev->cdev.hostid.devid);
333 VFIODevice *vbasedev;
335 QLIST_FOREACH(vbasedev, &group->device_list, next) {
336 if (strcmp(vbasedev->name, name) == 0) {
337 error_setg(errp, "vfio: subchannel %s has already been attached",
338 name);
339 goto out_err;
344 * All vfio-ccw devices are believed to operate in a way compatible with
345 * memory ballooning, ie. pages pinned in the host are in the current
346 * working set of the guest driver and therefore never overlap with pages
347 * available to the guest balloon driver. This needs to be set before
348 * vfio_get_device() for vfio common to handle the balloon inhibitor.
350 vcdev->vdev.balloon_allowed = true;
352 if (vfio_get_device(group, vcdev->cdev.mdevid, &vcdev->vdev, errp)) {
353 goto out_err;
356 vcdev->vdev.ops = &vfio_ccw_ops;
357 vcdev->vdev.type = VFIO_DEVICE_TYPE_CCW;
358 vcdev->vdev.name = name;
359 vcdev->vdev.dev = &vcdev->cdev.parent_obj.parent_obj;
361 return;
363 out_err:
364 g_free(name);
367 static VFIOGroup *vfio_ccw_get_group(S390CCWDevice *cdev, Error **errp)
369 char *tmp, group_path[PATH_MAX];
370 ssize_t len;
371 int groupid;
373 tmp = g_strdup_printf("/sys/bus/css/devices/%x.%x.%04x/%s/iommu_group",
374 cdev->hostid.cssid, cdev->hostid.ssid,
375 cdev->hostid.devid, cdev->mdevid);
376 len = readlink(tmp, group_path, sizeof(group_path));
377 g_free(tmp);
379 if (len <= 0 || len >= sizeof(group_path)) {
380 error_setg(errp, "vfio: no iommu_group found");
381 return NULL;
384 group_path[len] = 0;
386 if (sscanf(basename(group_path), "%d", &groupid) != 1) {
387 error_setg(errp, "vfio: failed to read %s", group_path);
388 return NULL;
391 return vfio_get_group(groupid, &address_space_memory, errp);
394 static void vfio_ccw_realize(DeviceState *dev, Error **errp)
396 VFIOGroup *group;
397 CcwDevice *ccw_dev = DO_UPCAST(CcwDevice, parent_obj, dev);
398 S390CCWDevice *cdev = DO_UPCAST(S390CCWDevice, parent_obj, ccw_dev);
399 VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev);
400 S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(cdev);
401 Error *err = NULL;
403 /* Call the class init function for subchannel. */
404 if (cdc->realize) {
405 cdc->realize(cdev, vcdev->vdev.sysfsdev, &err);
406 if (err) {
407 goto out_err_propagate;
411 group = vfio_ccw_get_group(cdev, &err);
412 if (!group) {
413 goto out_group_err;
416 vfio_ccw_get_device(group, vcdev, &err);
417 if (err) {
418 goto out_device_err;
421 vfio_ccw_get_region(vcdev, &err);
422 if (err) {
423 goto out_region_err;
426 vfio_ccw_register_io_notifier(vcdev, &err);
427 if (err) {
428 goto out_notifier_err;
431 return;
433 out_notifier_err:
434 vfio_ccw_put_region(vcdev);
435 out_region_err:
436 vfio_ccw_put_device(vcdev);
437 out_device_err:
438 vfio_put_group(group);
439 out_group_err:
440 if (cdc->unrealize) {
441 cdc->unrealize(cdev, NULL);
443 out_err_propagate:
444 error_propagate(errp, err);
447 static void vfio_ccw_unrealize(DeviceState *dev, Error **errp)
449 CcwDevice *ccw_dev = DO_UPCAST(CcwDevice, parent_obj, dev);
450 S390CCWDevice *cdev = DO_UPCAST(S390CCWDevice, parent_obj, ccw_dev);
451 VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev);
452 S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(cdev);
453 VFIOGroup *group = vcdev->vdev.group;
455 vfio_ccw_unregister_io_notifier(vcdev);
456 vfio_ccw_put_region(vcdev);
457 vfio_ccw_put_device(vcdev);
458 vfio_put_group(group);
460 if (cdc->unrealize) {
461 cdc->unrealize(cdev, errp);
465 static Property vfio_ccw_properties[] = {
466 DEFINE_PROP_STRING("sysfsdev", VFIOCCWDevice, vdev.sysfsdev),
467 DEFINE_PROP_BOOL("force-orb-pfch", VFIOCCWDevice, force_orb_pfch, false),
468 DEFINE_PROP_END_OF_LIST(),
471 static const VMStateDescription vfio_ccw_vmstate = {
472 .name = "vfio-ccw",
473 .unmigratable = 1,
476 static void vfio_ccw_class_init(ObjectClass *klass, void *data)
478 DeviceClass *dc = DEVICE_CLASS(klass);
479 S390CCWDeviceClass *cdc = S390_CCW_DEVICE_CLASS(klass);
481 dc->props = vfio_ccw_properties;
482 dc->vmsd = &vfio_ccw_vmstate;
483 dc->desc = "VFIO-based subchannel assignment";
484 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
485 dc->realize = vfio_ccw_realize;
486 dc->unrealize = vfio_ccw_unrealize;
487 dc->reset = vfio_ccw_reset;
489 cdc->handle_request = vfio_ccw_handle_request;
492 static const TypeInfo vfio_ccw_info = {
493 .name = TYPE_VFIO_CCW,
494 .parent = TYPE_S390_CCW,
495 .instance_size = sizeof(VFIOCCWDevice),
496 .class_init = vfio_ccw_class_init,
499 static void register_vfio_ccw_type(void)
501 type_register_static(&vfio_ccw_info);
504 type_init(register_vfio_ccw_type)