2 * virtio for kvm on s390
4 * Copyright IBM Corp. 2008
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.
10 * Author(s): Christian Borntraeger <borntraeger@de.ibm.com>
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>
26 #include <asm/kvm_para.h>
27 #include <asm/kvm_virtio.h>
29 #include <asm/setup.h>
32 #define VIRTIO_SUBCODE_64 0x0D00
35 * The pointer to our (page) of device descriptions.
37 static void *kvm_devices;
38 static struct work_struct hotplug_work;
41 struct virtio_device vdev;
42 struct kvm_device_desc *desc;
45 #define to_kvmdev(vd) container_of(vd, struct kvm_device, vdev)
49 * - kvm_device_descriptor
50 * struct kvm_device_desc
56 static struct kvm_vqconfig *kvm_vq_config(const struct kvm_device_desc *desc)
58 return (struct kvm_vqconfig *)(desc + 1);
61 static u8 *kvm_vq_features(const struct kvm_device_desc *desc)
63 return (u8 *)(kvm_vq_config(desc) + desc->num_vq);
66 static u8 *kvm_vq_configspace(const struct kvm_device_desc *desc)
68 return kvm_vq_features(desc) + desc->feature_len * 2;
72 * The total size of the config page used by this device (incl. desc)
74 static unsigned desc_size(const struct kvm_device_desc *desc)
77 + desc->num_vq * sizeof(struct kvm_vqconfig)
78 + desc->feature_len * 2
82 /* This gets the device's feature bits. */
83 static u64 kvm_get_features(struct virtio_device *vdev)
87 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
88 u8 *in_features = kvm_vq_features(desc);
90 for (i = 0; i < min(desc->feature_len * 8, 32); i++)
91 if (in_features[i / 8] & (1 << (i % 8)))
96 static int kvm_finalize_features(struct virtio_device *vdev)
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;
103 /* Give virtio_ring a chance to accept features. */
104 vring_transport_features(vdev);
106 /* Make sure we don't have any features > 32 bits! */
107 BUG_ON((u32)vdev->features != vdev->features);
109 memset(out_features, 0, desc->feature_len);
110 bits = min_t(unsigned, desc->feature_len, sizeof(vdev->features)) * 8;
111 for (i = 0; i < bits; i++) {
112 if (__virtio_test_bit(vdev, i))
113 out_features[i / 8] |= (1 << (i % 8));
120 * Reading and writing elements in config space
122 static void kvm_get(struct virtio_device *vdev, unsigned int offset,
123 void *buf, unsigned len)
125 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
127 BUG_ON(offset + len > desc->config_len);
128 memcpy(buf, kvm_vq_configspace(desc) + offset, len);
131 static void kvm_set(struct virtio_device *vdev, unsigned int offset,
132 const void *buf, unsigned len)
134 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
136 BUG_ON(offset + len > desc->config_len);
137 memcpy(kvm_vq_configspace(desc) + offset, buf, len);
141 * The operations to get and set the status word just access
142 * the status field of the device descriptor. set_status will also
143 * make a hypercall to the host, to tell about status changes
145 static u8 kvm_get_status(struct virtio_device *vdev)
147 return to_kvmdev(vdev)->desc->status;
150 static void kvm_set_status(struct virtio_device *vdev, u8 status)
153 to_kvmdev(vdev)->desc->status = status;
154 kvm_hypercall1(KVM_S390_VIRTIO_SET_STATUS,
155 (unsigned long) to_kvmdev(vdev)->desc);
159 * To reset the device, we use the KVM_VIRTIO_RESET hypercall, using the
160 * descriptor address. The Host will zero the status and all the
163 static void kvm_reset(struct virtio_device *vdev)
165 kvm_hypercall1(KVM_S390_VIRTIO_RESET,
166 (unsigned long) to_kvmdev(vdev)->desc);
170 * When the virtio_ring code wants to notify the Host, it calls us here and we
171 * make a hypercall. We hand the address of the virtqueue so the Host
172 * knows which virtqueue we're talking about.
174 static bool kvm_notify(struct virtqueue *vq)
177 struct kvm_vqconfig *config = vq->priv;
179 rc = kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, config->address);
186 * This routine finds the first virtqueue described in the configuration of
187 * this device and sets it up.
189 static struct virtqueue *kvm_find_vq(struct virtio_device *vdev,
191 void (*callback)(struct virtqueue *vq),
192 const char *name, bool ctx)
194 struct kvm_device *kdev = to_kvmdev(vdev);
195 struct kvm_vqconfig *config;
196 struct virtqueue *vq;
199 if (index >= kdev->desc->num_vq)
200 return ERR_PTR(-ENOENT);
205 config = kvm_vq_config(kdev->desc)+index;
207 err = vmem_add_mapping(config->address,
208 vring_size(config->num,
209 KVM_S390_VIRTIO_RING_ALIGN));
213 vq = vring_new_virtqueue(index, config->num, KVM_S390_VIRTIO_RING_ALIGN,
214 vdev, true, ctx, (void *) config->address,
215 kvm_notify, callback, name);
222 * register a callback token
223 * The host will sent this via the external interrupt parameter
225 config->token = (u64) vq;
230 vmem_remove_mapping(config->address,
231 vring_size(config->num,
232 KVM_S390_VIRTIO_RING_ALIGN));
237 static void kvm_del_vq(struct virtqueue *vq)
239 struct kvm_vqconfig *config = vq->priv;
241 vring_del_virtqueue(vq);
242 vmem_remove_mapping(config->address,
243 vring_size(config->num,
244 KVM_S390_VIRTIO_RING_ALIGN));
247 static void kvm_del_vqs(struct virtio_device *vdev)
249 struct virtqueue *vq, *n;
251 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
255 static int kvm_find_vqs(struct virtio_device *vdev, unsigned nvqs,
256 struct virtqueue *vqs[],
257 vq_callback_t *callbacks[],
258 const char * const names[],
260 struct irq_affinity *desc)
262 struct kvm_device *kdev = to_kvmdev(vdev);
265 /* We must have this many virtqueues. */
266 if (nvqs > kdev->desc->num_vq)
269 for (i = 0; i < nvqs; ++i) {
270 vqs[i] = kvm_find_vq(vdev, i, callbacks[i], names[i],
271 ctx ? ctx[i] : false);
279 return PTR_ERR(vqs[i]);
282 static const char *kvm_bus_name(struct virtio_device *vdev)
288 * The config ops structure as defined by virtio config
290 static const struct virtio_config_ops kvm_vq_configspace_ops = {
291 .get_features = kvm_get_features,
292 .finalize_features = kvm_finalize_features,
295 .get_status = kvm_get_status,
296 .set_status = kvm_set_status,
298 .find_vqs = kvm_find_vqs,
299 .del_vqs = kvm_del_vqs,
300 .bus_name = kvm_bus_name,
304 * The root device for the kvm virtio devices.
305 * This makes them appear as /sys/devices/kvm_s390/0,1,2 not /sys/devices/0,1,2.
307 static struct device *kvm_root;
310 * adds a new device and register it with virtio
311 * appropriate drivers are loaded by the device model
313 static void add_kvm_device(struct kvm_device_desc *d, unsigned int offset)
315 struct kvm_device *kdev;
317 kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
319 printk(KERN_EMERG "Cannot allocate kvm dev %u type %u\n",
324 kdev->vdev.dev.parent = kvm_root;
325 kdev->vdev.id.device = d->type;
326 kdev->vdev.config = &kvm_vq_configspace_ops;
329 if (register_virtio_device(&kdev->vdev) != 0) {
330 printk(KERN_ERR "Failed to register kvm device %u type %u\n",
337 * scan_devices() simply iterates through the device page.
338 * The type 0 is reserved to mean "end of devices".
340 static void scan_devices(void)
343 struct kvm_device_desc *d;
345 for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
351 add_kvm_device(d, i);
356 * match for a kvm device with a specific desc pointer
358 static int match_desc(struct device *dev, void *data)
360 struct virtio_device *vdev = dev_to_virtio(dev);
361 struct kvm_device *kdev = to_kvmdev(vdev);
363 return kdev->desc == data;
367 * hotplug_device tries to find changes in the device page.
369 static void hotplug_devices(struct work_struct *dummy)
372 struct kvm_device_desc *d;
375 for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
382 /* device already exists */
383 dev = device_find_child(kvm_root, d, match_desc);
385 /* XXX check for hotplug remove */
391 printk(KERN_INFO "Adding new virtio device %p\n", d);
392 add_kvm_device(d, i);
397 * we emulate the request_irq behaviour on top of s390 extints
399 static void kvm_extint_handler(struct ext_code ext_code,
400 unsigned int param32, unsigned long param64)
402 struct virtqueue *vq;
405 if ((ext_code.subcode & 0xff00) != VIRTIO_SUBCODE_64)
407 inc_irq_stat(IRQEXT_VRT);
409 /* The LSB might be overloaded, we have to mask it */
410 vq = (struct virtqueue *)(param64 & ~1UL);
412 /* We use ext_params to decide what this interrupt means */
413 param = param32 & VIRTIO_PARAM_MASK;
416 case VIRTIO_PARAM_CONFIG_CHANGED:
417 virtio_config_changed(vq->vdev);
419 case VIRTIO_PARAM_DEV_ADD:
420 schedule_work(&hotplug_work);
422 case VIRTIO_PARAM_VRING_INTERRUPT:
424 vring_interrupt(0, vq);
430 * For s390-virtio, we expect a page above main storage containing
431 * the virtio configuration. Try to actually load from this area
432 * in order to figure out if the host provides this page.
434 static int __init test_devices_support(unsigned long addr)
450 * Init function for virtio
451 * devices are in a single page above top of "normal" + standby mem
453 static int __init kvm_devices_init(void)
456 unsigned long total_memory_size = sclp.rzm * sclp.rnmax;
461 if (test_devices_support(total_memory_size) < 0)
464 pr_warn("The s390-virtio transport is deprecated. Please switch to a modern host providing virtio-ccw.\n");
466 rc = vmem_add_mapping(total_memory_size, PAGE_SIZE);
470 kvm_devices = (void *) total_memory_size;
472 kvm_root = root_device_register("kvm_s390");
473 if (IS_ERR(kvm_root)) {
474 rc = PTR_ERR(kvm_root);
475 printk(KERN_ERR "Could not register kvm_s390 root device");
476 vmem_remove_mapping(total_memory_size, PAGE_SIZE);
480 INIT_WORK(&hotplug_work, hotplug_devices);
482 irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL);
483 register_external_irq(EXT_IRQ_CP_SERVICE, kvm_extint_handler);
489 /* code for early console output with virtio_console */
490 static int early_put_chars(u32 vtermno, const char *buf, int count)
493 unsigned int len = count;
495 if (len > sizeof(scratch) - 1)
496 len = sizeof(scratch) - 1;
498 memcpy(scratch, buf, len);
499 kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, __pa(scratch));
503 static int __init s390_virtio_console_init(void)
505 if (sclp.has_vt220 || sclp.has_linemode)
507 return virtio_cons_early_init(early_put_chars);
509 console_initcall(s390_virtio_console_init);
513 * We do this after core stuff, but before the drivers.
515 postcore_initcall(kvm_devices_init);