]> Pileus Git - ~andy/linux/blob - drivers/s390/kvm/kvm_virtio.c
Merge tag 'kvm-3.6-1' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[~andy/linux] / drivers / s390 / kvm / kvm_virtio.c
1 /*
2  * virtio for kvm on s390
3  *
4  * Copyright IBM Corp. 2008
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License (version 2 only)
8  * as published by the Free Software Foundation.
9  *
10  *    Author(s): Christian Borntraeger <borntraeger@de.ibm.com>
11  */
12
13 #include <linux/kernel_stat.h>
14 #include <linux/init.h>
15 #include <linux/bootmem.h>
16 #include <linux/err.h>
17 #include <linux/virtio.h>
18 #include <linux/virtio_config.h>
19 #include <linux/slab.h>
20 #include <linux/virtio_console.h>
21 #include <linux/interrupt.h>
22 #include <linux/virtio_ring.h>
23 #include <linux/export.h>
24 #include <linux/pfn.h>
25 #include <asm/io.h>
26 #include <asm/kvm_para.h>
27 #include <asm/kvm_virtio.h>
28 #include <asm/sclp.h>
29 #include <asm/setup.h>
30 #include <asm/irq.h>
31
32 #define VIRTIO_SUBCODE_64 0x0D00
33
34 /*
35  * The pointer to our (page) of device descriptions.
36  */
37 static void *kvm_devices;
38 static struct work_struct hotplug_work;
39
40 struct kvm_device {
41         struct virtio_device vdev;
42         struct kvm_device_desc *desc;
43 };
44
45 #define to_kvmdev(vd) container_of(vd, struct kvm_device, vdev)
46
47 /*
48  * memory layout:
49  * - kvm_device_descriptor
50  *        struct kvm_device_desc
51  * - configuration
52  *        struct kvm_vqconfig
53  * - feature bits
54  * - config space
55  */
56 static struct kvm_vqconfig *kvm_vq_config(const struct kvm_device_desc *desc)
57 {
58         return (struct kvm_vqconfig *)(desc + 1);
59 }
60
61 static u8 *kvm_vq_features(const struct kvm_device_desc *desc)
62 {
63         return (u8 *)(kvm_vq_config(desc) + desc->num_vq);
64 }
65
66 static u8 *kvm_vq_configspace(const struct kvm_device_desc *desc)
67 {
68         return kvm_vq_features(desc) + desc->feature_len * 2;
69 }
70
71 /*
72  * The total size of the config page used by this device (incl. desc)
73  */
74 static unsigned desc_size(const struct kvm_device_desc *desc)
75 {
76         return sizeof(*desc)
77                 + desc->num_vq * sizeof(struct kvm_vqconfig)
78                 + desc->feature_len * 2
79                 + desc->config_len;
80 }
81
82 /* This gets the device's feature bits. */
83 static u32 kvm_get_features(struct virtio_device *vdev)
84 {
85         unsigned int i;
86         u32 features = 0;
87         struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
88         u8 *in_features = kvm_vq_features(desc);
89
90         for (i = 0; i < min(desc->feature_len * 8, 32); i++)
91                 if (in_features[i / 8] & (1 << (i % 8)))
92                         features |= (1 << i);
93         return features;
94 }
95
96 static void kvm_finalize_features(struct virtio_device *vdev)
97 {
98         unsigned int i, bits;
99         struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
100         /* Second half of bitmap is features we accept. */
101         u8 *out_features = kvm_vq_features(desc) + desc->feature_len;
102
103         /* Give virtio_ring a chance to accept features. */
104         vring_transport_features(vdev);
105
106         memset(out_features, 0, desc->feature_len);
107         bits = min_t(unsigned, desc->feature_len, sizeof(vdev->features)) * 8;
108         for (i = 0; i < bits; i++) {
109                 if (test_bit(i, vdev->features))
110                         out_features[i / 8] |= (1 << (i % 8));
111         }
112 }
113
114 /*
115  * Reading and writing elements in config space
116  */
117 static void kvm_get(struct virtio_device *vdev, unsigned int offset,
118                    void *buf, unsigned len)
119 {
120         struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
121
122         BUG_ON(offset + len > desc->config_len);
123         memcpy(buf, kvm_vq_configspace(desc) + offset, len);
124 }
125
126 static void kvm_set(struct virtio_device *vdev, unsigned int offset,
127                    const void *buf, unsigned len)
128 {
129         struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
130
131         BUG_ON(offset + len > desc->config_len);
132         memcpy(kvm_vq_configspace(desc) + offset, buf, len);
133 }
134
135 /*
136  * The operations to get and set the status word just access
137  * the status field of the device descriptor. set_status will also
138  * make a hypercall to the host, to tell about status changes
139  */
140 static u8 kvm_get_status(struct virtio_device *vdev)
141 {
142         return to_kvmdev(vdev)->desc->status;
143 }
144
145 static void kvm_set_status(struct virtio_device *vdev, u8 status)
146 {
147         BUG_ON(!status);
148         to_kvmdev(vdev)->desc->status = status;
149         kvm_hypercall1(KVM_S390_VIRTIO_SET_STATUS,
150                        (unsigned long) to_kvmdev(vdev)->desc);
151 }
152
153 /*
154  * To reset the device, we use the KVM_VIRTIO_RESET hypercall, using the
155  * descriptor address. The Host will zero the status and all the
156  * features.
157  */
158 static void kvm_reset(struct virtio_device *vdev)
159 {
160         kvm_hypercall1(KVM_S390_VIRTIO_RESET,
161                        (unsigned long) to_kvmdev(vdev)->desc);
162 }
163
164 /*
165  * When the virtio_ring code wants to notify the Host, it calls us here and we
166  * make a hypercall.  We hand the address  of the virtqueue so the Host
167  * knows which virtqueue we're talking about.
168  */
169 static void kvm_notify(struct virtqueue *vq)
170 {
171         struct kvm_vqconfig *config = vq->priv;
172
173         kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, config->address);
174 }
175
176 /*
177  * This routine finds the first virtqueue described in the configuration of
178  * this device and sets it up.
179  */
180 static struct virtqueue *kvm_find_vq(struct virtio_device *vdev,
181                                      unsigned index,
182                                      void (*callback)(struct virtqueue *vq),
183                                      const char *name)
184 {
185         struct kvm_device *kdev = to_kvmdev(vdev);
186         struct kvm_vqconfig *config;
187         struct virtqueue *vq;
188         int err;
189
190         if (index >= kdev->desc->num_vq)
191                 return ERR_PTR(-ENOENT);
192
193         config = kvm_vq_config(kdev->desc)+index;
194
195         err = vmem_add_mapping(config->address,
196                                vring_size(config->num,
197                                           KVM_S390_VIRTIO_RING_ALIGN));
198         if (err)
199                 goto out;
200
201         vq = vring_new_virtqueue(config->num, KVM_S390_VIRTIO_RING_ALIGN,
202                                  vdev, true, (void *) config->address,
203                                  kvm_notify, callback, name);
204         if (!vq) {
205                 err = -ENOMEM;
206                 goto unmap;
207         }
208
209         /*
210          * register a callback token
211          * The host will sent this via the external interrupt parameter
212          */
213         config->token = (u64) vq;
214
215         vq->priv = config;
216         return vq;
217 unmap:
218         vmem_remove_mapping(config->address,
219                             vring_size(config->num,
220                                        KVM_S390_VIRTIO_RING_ALIGN));
221 out:
222         return ERR_PTR(err);
223 }
224
225 static void kvm_del_vq(struct virtqueue *vq)
226 {
227         struct kvm_vqconfig *config = vq->priv;
228
229         vring_del_virtqueue(vq);
230         vmem_remove_mapping(config->address,
231                             vring_size(config->num,
232                                        KVM_S390_VIRTIO_RING_ALIGN));
233 }
234
235 static void kvm_del_vqs(struct virtio_device *vdev)
236 {
237         struct virtqueue *vq, *n;
238
239         list_for_each_entry_safe(vq, n, &vdev->vqs, list)
240                 kvm_del_vq(vq);
241 }
242
243 static int kvm_find_vqs(struct virtio_device *vdev, unsigned nvqs,
244                         struct virtqueue *vqs[],
245                         vq_callback_t *callbacks[],
246                         const char *names[])
247 {
248         struct kvm_device *kdev = to_kvmdev(vdev);
249         int i;
250
251         /* We must have this many virtqueues. */
252         if (nvqs > kdev->desc->num_vq)
253                 return -ENOENT;
254
255         for (i = 0; i < nvqs; ++i) {
256                 vqs[i] = kvm_find_vq(vdev, i, callbacks[i], names[i]);
257                 if (IS_ERR(vqs[i]))
258                         goto error;
259         }
260         return 0;
261
262 error:
263         kvm_del_vqs(vdev);
264         return PTR_ERR(vqs[i]);
265 }
266
267 static const char *kvm_bus_name(struct virtio_device *vdev)
268 {
269         return "";
270 }
271
272 /*
273  * The config ops structure as defined by virtio config
274  */
275 static struct virtio_config_ops kvm_vq_configspace_ops = {
276         .get_features = kvm_get_features,
277         .finalize_features = kvm_finalize_features,
278         .get = kvm_get,
279         .set = kvm_set,
280         .get_status = kvm_get_status,
281         .set_status = kvm_set_status,
282         .reset = kvm_reset,
283         .find_vqs = kvm_find_vqs,
284         .del_vqs = kvm_del_vqs,
285         .bus_name = kvm_bus_name,
286 };
287
288 /*
289  * The root device for the kvm virtio devices.
290  * This makes them appear as /sys/devices/kvm_s390/0,1,2 not /sys/devices/0,1,2.
291  */
292 static struct device *kvm_root;
293
294 /*
295  * adds a new device and register it with virtio
296  * appropriate drivers are loaded by the device model
297  */
298 static void add_kvm_device(struct kvm_device_desc *d, unsigned int offset)
299 {
300         struct kvm_device *kdev;
301
302         kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
303         if (!kdev) {
304                 printk(KERN_EMERG "Cannot allocate kvm dev %u type %u\n",
305                        offset, d->type);
306                 return;
307         }
308
309         kdev->vdev.dev.parent = kvm_root;
310         kdev->vdev.id.device = d->type;
311         kdev->vdev.config = &kvm_vq_configspace_ops;
312         kdev->desc = d;
313
314         if (register_virtio_device(&kdev->vdev) != 0) {
315                 printk(KERN_ERR "Failed to register kvm device %u type %u\n",
316                        offset, d->type);
317                 kfree(kdev);
318         }
319 }
320
321 /*
322  * scan_devices() simply iterates through the device page.
323  * The type 0 is reserved to mean "end of devices".
324  */
325 static void scan_devices(void)
326 {
327         unsigned int i;
328         struct kvm_device_desc *d;
329
330         for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
331                 d = kvm_devices + i;
332
333                 if (d->type == 0)
334                         break;
335
336                 add_kvm_device(d, i);
337         }
338 }
339
340 /*
341  * match for a kvm device with a specific desc pointer
342  */
343 static int match_desc(struct device *dev, void *data)
344 {
345         struct virtio_device *vdev = dev_to_virtio(dev);
346         struct kvm_device *kdev = to_kvmdev(vdev);
347
348         return kdev->desc == data;
349 }
350
351 /*
352  * hotplug_device tries to find changes in the device page.
353  */
354 static void hotplug_devices(struct work_struct *dummy)
355 {
356         unsigned int i;
357         struct kvm_device_desc *d;
358         struct device *dev;
359
360         for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
361                 d = kvm_devices + i;
362
363                 /* end of list */
364                 if (d->type == 0)
365                         break;
366
367                 /* device already exists */
368                 dev = device_find_child(kvm_root, d, match_desc);
369                 if (dev) {
370                         /* XXX check for hotplug remove */
371                         put_device(dev);
372                         continue;
373                 }
374
375                 /* new device */
376                 printk(KERN_INFO "Adding new virtio device %p\n", d);
377                 add_kvm_device(d, i);
378         }
379 }
380
381 /*
382  * we emulate the request_irq behaviour on top of s390 extints
383  */
384 static void kvm_extint_handler(struct ext_code ext_code,
385                                unsigned int param32, unsigned long param64)
386 {
387         struct virtqueue *vq;
388         u32 param;
389
390         if ((ext_code.subcode & 0xff00) != VIRTIO_SUBCODE_64)
391                 return;
392         kstat_cpu(smp_processor_id()).irqs[EXTINT_VRT]++;
393
394         /* The LSB might be overloaded, we have to mask it */
395         vq = (struct virtqueue *)(param64 & ~1UL);
396
397         /* We use ext_params to decide what this interrupt means */
398         param = param32 & VIRTIO_PARAM_MASK;
399
400         switch (param) {
401         case VIRTIO_PARAM_CONFIG_CHANGED:
402         {
403                 struct virtio_driver *drv;
404                 drv = container_of(vq->vdev->dev.driver,
405                                    struct virtio_driver, driver);
406                 if (drv->config_changed)
407                         drv->config_changed(vq->vdev);
408
409                 break;
410         }
411         case VIRTIO_PARAM_DEV_ADD:
412                 schedule_work(&hotplug_work);
413                 break;
414         case VIRTIO_PARAM_VRING_INTERRUPT:
415         default:
416                 vring_interrupt(0, vq);
417                 break;
418         }
419 }
420
421 /*
422  * Init function for virtio
423  * devices are in a single page above top of "normal" mem
424  */
425 static int __init kvm_devices_init(void)
426 {
427         int rc;
428
429         if (!MACHINE_IS_KVM)
430                 return -ENODEV;
431
432         kvm_root = root_device_register("kvm_s390");
433         if (IS_ERR(kvm_root)) {
434                 rc = PTR_ERR(kvm_root);
435                 printk(KERN_ERR "Could not register kvm_s390 root device");
436                 return rc;
437         }
438
439         rc = vmem_add_mapping(real_memory_size, PAGE_SIZE);
440         if (rc) {
441                 root_device_unregister(kvm_root);
442                 return rc;
443         }
444
445         kvm_devices = (void *) real_memory_size;
446
447         INIT_WORK(&hotplug_work, hotplug_devices);
448
449         service_subclass_irq_register();
450         register_external_interrupt(0x2603, kvm_extint_handler);
451
452         scan_devices();
453         return 0;
454 }
455
456 /* code for early console output with virtio_console */
457 static __init int early_put_chars(u32 vtermno, const char *buf, int count)
458 {
459         char scratch[17];
460         unsigned int len = count;
461
462         if (len > sizeof(scratch) - 1)
463                 len = sizeof(scratch) - 1;
464         scratch[len] = '\0';
465         memcpy(scratch, buf, len);
466         kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, __pa(scratch));
467         return len;
468 }
469
470 static int __init s390_virtio_console_init(void)
471 {
472         if (sclp_has_vt220() || sclp_has_linemode())
473                 return -ENODEV;
474         return virtio_cons_early_init(early_put_chars);
475 }
476 console_initcall(s390_virtio_console_init);
477
478
479 /*
480  * We do this after core stuff, but before the drivers.
481  */
482 postcore_initcall(kvm_devices_init);