xref: /qemu/hw/intc/s390_flic_kvm.c (revision 1497c1606615b0b08d1b1f78afd1dcf2585879c0)
1 /*
2  * QEMU S390x KVM floating interrupt controller (flic)
3  *
4  * Copyright 2014 IBM Corp.
5  * Author(s): Jens Freimann <jfrei@linux.vnet.ibm.com>
6  *            Cornelia Huck <cornelia.huck@de.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 "qemu-common.h"
15 #include "cpu.h"
16 #include <sys/ioctl.h>
17 #include "qemu/error-report.h"
18 #include "qapi/error.h"
19 #include "hw/sysbus.h"
20 #include "sysemu/kvm.h"
21 #include "hw/s390x/s390_flic.h"
22 #include "hw/s390x/adapter.h"
23 #include "trace.h"
24 
25 #define FLIC_SAVE_INITIAL_SIZE getpagesize()
26 #define FLIC_FAILED (-1UL)
27 #define FLIC_SAVEVM_VERSION 1
28 
29 typedef struct KVMS390FLICState {
30     S390FLICState parent_obj;
31 
32     uint32_t fd;
33     bool clear_io_supported;
34 } KVMS390FLICState;
35 
36 DeviceState *s390_flic_kvm_create(void)
37 {
38     DeviceState *dev = NULL;
39 
40     if (kvm_enabled()) {
41         dev = qdev_create(NULL, TYPE_KVM_S390_FLIC);
42         object_property_add_child(qdev_get_machine(), TYPE_KVM_S390_FLIC,
43                                   OBJECT(dev), NULL);
44     }
45     return dev;
46 }
47 
48 /**
49  * flic_get_all_irqs - store all pending irqs in buffer
50  * @buf: pointer to buffer which is passed to kernel
51  * @len: length of buffer
52  * @flic: pointer to flic device state
53  *
54  * Returns: -ENOMEM if buffer is too small,
55  * -EINVAL if attr.group is invalid,
56  * -EFAULT if copying to userspace failed,
57  * on success return number of stored interrupts
58  */
59 static int flic_get_all_irqs(KVMS390FLICState *flic,
60                              void *buf, int len)
61 {
62     struct kvm_device_attr attr = {
63         .group = KVM_DEV_FLIC_GET_ALL_IRQS,
64         .addr = (uint64_t) buf,
65         .attr = len,
66     };
67     int rc;
68 
69     rc = ioctl(flic->fd, KVM_GET_DEVICE_ATTR, &attr);
70 
71     return rc == -1 ? -errno : rc;
72 }
73 
74 static void flic_enable_pfault(KVMS390FLICState *flic)
75 {
76     struct kvm_device_attr attr = {
77         .group = KVM_DEV_FLIC_APF_ENABLE,
78     };
79     int rc;
80 
81     rc = ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr);
82 
83     if (rc) {
84         fprintf(stderr, "flic: couldn't enable pfault\n");
85     }
86 }
87 
88 static void flic_disable_wait_pfault(KVMS390FLICState *flic)
89 {
90     struct kvm_device_attr attr = {
91         .group = KVM_DEV_FLIC_APF_DISABLE_WAIT,
92     };
93     int rc;
94 
95     rc = ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr);
96 
97     if (rc) {
98         fprintf(stderr, "flic: couldn't disable pfault\n");
99     }
100 }
101 
102 /** flic_enqueue_irqs - returns 0 on success
103  * @buf: pointer to buffer which is passed to kernel
104  * @len: length of buffer
105  * @flic: pointer to flic device state
106  *
107  * Returns: -EINVAL if attr.group is unknown
108  */
109 static int flic_enqueue_irqs(void *buf, uint64_t len,
110                             KVMS390FLICState *flic)
111 {
112     int rc;
113     struct kvm_device_attr attr = {
114         .group = KVM_DEV_FLIC_ENQUEUE,
115         .addr = (uint64_t) buf,
116         .attr = len,
117     };
118 
119     rc = ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr);
120 
121     return rc ? -errno : 0;
122 }
123 
124 int kvm_s390_inject_flic(struct kvm_s390_irq *irq)
125 {
126     static KVMS390FLICState *flic;
127 
128     if (unlikely(!flic)) {
129         flic = KVM_S390_FLIC(s390_get_flic());
130     }
131     return flic_enqueue_irqs(irq, sizeof(*irq), flic);
132 }
133 
134 static int kvm_s390_clear_io_flic(S390FLICState *fs, uint16_t subchannel_id,
135                            uint16_t subchannel_nr)
136 {
137     KVMS390FLICState *flic = KVM_S390_FLIC(fs);
138     int rc;
139     uint32_t sid = subchannel_id << 16 | subchannel_nr;
140     struct kvm_device_attr attr = {
141         .group = KVM_DEV_FLIC_CLEAR_IO_IRQ,
142         .addr = (uint64_t) &sid,
143         .attr = sizeof(sid),
144     };
145     if (unlikely(!flic->clear_io_supported)) {
146         return -ENOSYS;
147     }
148     rc = ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr);
149     return rc ? -errno : 0;
150 }
151 
152 /**
153  * __get_all_irqs - store all pending irqs in buffer
154  * @flic: pointer to flic device state
155  * @buf: pointer to pointer to a buffer
156  * @len: length of buffer
157  *
158  * Returns: return value of flic_get_all_irqs
159  * Note: Retry and increase buffer size until flic_get_all_irqs
160  * either returns a value >= 0 or a negative error code.
161  * -ENOMEM is an exception, which means the buffer is too small
162  * and we should try again. Other negative error codes can be
163  * -EFAULT and -EINVAL which we ignore at this point
164  */
165 static int __get_all_irqs(KVMS390FLICState *flic,
166                           void **buf, int len)
167 {
168     int r;
169 
170     do {
171         /* returns -ENOMEM if buffer is too small and number
172          * of queued interrupts on success */
173         r = flic_get_all_irqs(flic, *buf, len);
174         if (r >= 0) {
175             break;
176         }
177         len *= 2;
178         *buf = g_try_realloc(*buf, len);
179         if (!buf) {
180             return -ENOMEM;
181         }
182     } while (r == -ENOMEM && len <= KVM_S390_FLIC_MAX_BUFFER);
183 
184     return r;
185 }
186 
187 static int kvm_s390_register_io_adapter(S390FLICState *fs, uint32_t id,
188                                         uint8_t isc, bool swap,
189                                         bool is_maskable, uint8_t flags)
190 {
191     struct kvm_s390_io_adapter adapter = {
192         .id = id,
193         .isc = isc,
194         .maskable = is_maskable,
195         .swap = swap,
196         .flags = flags,
197     };
198     KVMS390FLICState *flic = KVM_S390_FLIC(fs);
199     int r;
200     struct kvm_device_attr attr = {
201         .group = KVM_DEV_FLIC_ADAPTER_REGISTER,
202         .addr = (uint64_t)&adapter,
203     };
204 
205     if (!kvm_gsi_routing_enabled()) {
206         /* nothing to do */
207         return 0;
208     }
209 
210     r = ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr);
211 
212     return r ? -errno : 0;
213 }
214 
215 static int kvm_s390_io_adapter_map(S390FLICState *fs, uint32_t id,
216                                    uint64_t map_addr, bool do_map)
217 {
218     struct kvm_s390_io_adapter_req req = {
219         .id = id,
220         .type = do_map ? KVM_S390_IO_ADAPTER_MAP : KVM_S390_IO_ADAPTER_UNMAP,
221         .addr = map_addr,
222     };
223     struct kvm_device_attr attr = {
224         .group = KVM_DEV_FLIC_ADAPTER_MODIFY,
225         .addr = (uint64_t)&req,
226     };
227     KVMS390FLICState *flic = KVM_S390_FLIC(fs);
228     int r;
229 
230     if (!kvm_gsi_routing_enabled()) {
231         /* nothing to do */
232         return 0;
233     }
234 
235     r = ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr);
236     return r ? -errno : 0;
237 }
238 
239 static int kvm_s390_add_adapter_routes(S390FLICState *fs,
240                                        AdapterRoutes *routes)
241 {
242     int ret, i;
243     uint64_t ind_offset = routes->adapter.ind_offset;
244 
245     for (i = 0; i < routes->num_routes; i++) {
246         ret = kvm_irqchip_add_adapter_route(kvm_state, &routes->adapter);
247         if (ret < 0) {
248             goto out_undo;
249         }
250         routes->gsi[i] = ret;
251         routes->adapter.ind_offset++;
252     }
253     kvm_irqchip_commit_routes(kvm_state);
254 
255     /* Restore passed-in structure to original state. */
256     routes->adapter.ind_offset = ind_offset;
257     return 0;
258 out_undo:
259     while (--i >= 0) {
260         kvm_irqchip_release_virq(kvm_state, routes->gsi[i]);
261         routes->gsi[i] = -1;
262     }
263     routes->adapter.ind_offset = ind_offset;
264     return ret;
265 }
266 
267 static void kvm_s390_release_adapter_routes(S390FLICState *fs,
268                                             AdapterRoutes *routes)
269 {
270     int i;
271 
272     for (i = 0; i < routes->num_routes; i++) {
273         if (routes->gsi[i] >= 0) {
274             kvm_irqchip_release_virq(kvm_state, routes->gsi[i]);
275             routes->gsi[i] = -1;
276         }
277     }
278 }
279 
280 /**
281  * kvm_flic_save - Save pending floating interrupts
282  * @f: QEMUFile containing migration state
283  * @opaque: pointer to flic device state
284  * @size: ignored
285  *
286  * Note: Pass buf and len to kernel. Start with one page and
287  * increase until buffer is sufficient or maxium size is
288  * reached
289  */
290 static int kvm_flic_save(QEMUFile *f, void *opaque, size_t size,
291                          VMStateField *field, QJSON *vmdesc)
292 {
293     KVMS390FLICState *flic = opaque;
294     int len = FLIC_SAVE_INITIAL_SIZE;
295     void *buf;
296     int count;
297     int r = 0;
298 
299     flic_disable_wait_pfault((struct KVMS390FLICState *) opaque);
300 
301     buf = g_try_malloc0(len);
302     if (!buf) {
303         /* Storing FLIC_FAILED into the count field here will cause the
304          * target system to fail when attempting to load irqs from the
305          * migration state */
306         error_report("flic: couldn't allocate memory");
307         qemu_put_be64(f, FLIC_FAILED);
308         return -ENOMEM;
309     }
310 
311     count = __get_all_irqs(flic, &buf, len);
312     if (count < 0) {
313         error_report("flic: couldn't retrieve irqs from kernel, rc %d",
314                      count);
315         /* Storing FLIC_FAILED into the count field here will cause the
316          * target system to fail when attempting to load irqs from the
317          * migration state */
318         qemu_put_be64(f, FLIC_FAILED);
319         r = count;
320     } else {
321         qemu_put_be64(f, count);
322         qemu_put_buffer(f, (uint8_t *) buf,
323                         count * sizeof(struct kvm_s390_irq));
324     }
325     g_free(buf);
326 
327     return r;
328 }
329 
330 /**
331  * kvm_flic_load - Load pending floating interrupts
332  * @f: QEMUFile containing migration state
333  * @opaque: pointer to flic device state
334  * @size: ignored
335  *
336  * Returns: value of flic_enqueue_irqs, -EINVAL on error
337  * Note: Do nothing when no interrupts where stored
338  * in QEMUFile
339  */
340 static int kvm_flic_load(QEMUFile *f, void *opaque, size_t size,
341                          VMStateField *field)
342 {
343     uint64_t len = 0;
344     uint64_t count = 0;
345     void *buf = NULL;
346     int r = 0;
347 
348     flic_enable_pfault((struct KVMS390FLICState *) opaque);
349 
350     count = qemu_get_be64(f);
351     len = count * sizeof(struct kvm_s390_irq);
352     if (count == FLIC_FAILED) {
353         r = -EINVAL;
354         goto out;
355     }
356     if (count == 0) {
357         r = 0;
358         goto out;
359     }
360     buf = g_try_malloc0(len);
361     if (!buf) {
362         r = -ENOMEM;
363         goto out;
364     }
365 
366     if (qemu_get_buffer(f, (uint8_t *) buf, len) != len) {
367         r = -EINVAL;
368         goto out_free;
369     }
370     r = flic_enqueue_irqs(buf, len, (struct KVMS390FLICState *) opaque);
371 
372 out_free:
373     g_free(buf);
374 out:
375     return r;
376 }
377 
378 static const VMStateDescription kvm_s390_flic_vmstate = {
379     .name = "s390-flic",
380     .version_id = FLIC_SAVEVM_VERSION,
381     .minimum_version_id = FLIC_SAVEVM_VERSION,
382     .fields = (VMStateField[]) {
383         {
384             .name = "irqs",
385             .info = &(const VMStateInfo) {
386                 .name = "irqs",
387                 .get = kvm_flic_load,
388                 .put = kvm_flic_save,
389             },
390             .flags = VMS_SINGLE,
391         },
392         VMSTATE_END_OF_LIST()
393     }
394 };
395 
396 typedef struct KVMS390FLICStateClass {
397     S390FLICStateClass parent_class;
398     DeviceRealize parent_realize;
399 } KVMS390FLICStateClass;
400 
401 #define KVM_S390_FLIC_GET_CLASS(obj) \
402     OBJECT_GET_CLASS(KVMS390FLICStateClass, (obj), TYPE_KVM_S390_FLIC)
403 
404 #define KVM_S390_FLIC_CLASS(klass) \
405     OBJECT_CLASS_CHECK(KVMS390FLICStateClass, (klass), TYPE_KVM_S390_FLIC)
406 
407 static void kvm_s390_flic_realize(DeviceState *dev, Error **errp)
408 {
409     KVMS390FLICState *flic_state = KVM_S390_FLIC(dev);
410     struct kvm_create_device cd = {0};
411     struct kvm_device_attr test_attr = {0};
412     int ret;
413     Error *errp_local = NULL;
414 
415     KVM_S390_FLIC_GET_CLASS(dev)->parent_realize(dev, &errp_local);
416     if (errp_local) {
417         goto fail;
418     }
419     flic_state->fd = -1;
420     if (!kvm_check_extension(kvm_state, KVM_CAP_DEVICE_CTRL)) {
421         error_setg_errno(&errp_local, errno, "KVM is missing capability"
422                          " KVM_CAP_DEVICE_CTRL");
423         trace_flic_no_device_api(errno);
424         goto fail;
425     }
426 
427     cd.type = KVM_DEV_TYPE_FLIC;
428     ret = kvm_vm_ioctl(kvm_state, KVM_CREATE_DEVICE, &cd);
429     if (ret < 0) {
430         error_setg_errno(&errp_local, errno, "Creating the KVM device failed");
431         trace_flic_create_device(errno);
432         goto fail;
433     }
434     flic_state->fd = cd.fd;
435 
436     /* Check clear_io_irq support */
437     test_attr.group = KVM_DEV_FLIC_CLEAR_IO_IRQ;
438     flic_state->clear_io_supported = !ioctl(flic_state->fd,
439                                             KVM_HAS_DEVICE_ATTR, test_attr);
440 
441     return;
442 fail:
443     error_propagate(errp, errp_local);
444 }
445 
446 static void kvm_s390_flic_reset(DeviceState *dev)
447 {
448     KVMS390FLICState *flic = KVM_S390_FLIC(dev);
449     struct kvm_device_attr attr = {
450         .group = KVM_DEV_FLIC_CLEAR_IRQS,
451     };
452     int rc = 0;
453 
454     if (flic->fd == -1) {
455         return;
456     }
457 
458     flic_disable_wait_pfault(flic);
459 
460     rc = ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr);
461     if (rc) {
462         trace_flic_reset_failed(errno);
463     }
464 
465     flic_enable_pfault(flic);
466 }
467 
468 static void kvm_s390_flic_class_init(ObjectClass *oc, void *data)
469 {
470     DeviceClass *dc = DEVICE_CLASS(oc);
471     S390FLICStateClass *fsc = S390_FLIC_COMMON_CLASS(oc);
472 
473     KVM_S390_FLIC_CLASS(oc)->parent_realize = dc->realize;
474     dc->realize = kvm_s390_flic_realize;
475     dc->vmsd = &kvm_s390_flic_vmstate;
476     dc->reset = kvm_s390_flic_reset;
477     fsc->register_io_adapter = kvm_s390_register_io_adapter;
478     fsc->io_adapter_map = kvm_s390_io_adapter_map;
479     fsc->add_adapter_routes = kvm_s390_add_adapter_routes;
480     fsc->release_adapter_routes = kvm_s390_release_adapter_routes;
481     fsc->clear_io_irq = kvm_s390_clear_io_flic;
482 }
483 
484 static const TypeInfo kvm_s390_flic_info = {
485     .name          = TYPE_KVM_S390_FLIC,
486     .parent        = TYPE_S390_FLIC_COMMON,
487     .instance_size = sizeof(KVMS390FLICState),
488     .class_size    = sizeof(KVMS390FLICStateClass),
489     .class_init    = kvm_s390_flic_class_init,
490 };
491 
492 static void kvm_s390_flic_register_types(void)
493 {
494     type_register_static(&kvm_s390_flic_info);
495 }
496 
497 type_init(kvm_s390_flic_register_types)
498