2 * kvm_virtio.c - 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/init.h>
14 #include <linux/bootmem.h>
15 #include <linux/err.h>
16 #include <linux/virtio.h>
17 #include <linux/virtio_config.h>
18 #include <linux/slab.h>
19 #include <linux/virtio_console.h>
20 #include <linux/interrupt.h>
21 #include <linux/virtio_ring.h>
22 #include <linux/pfn.h>
24 #include <asm/kvm_para.h>
25 #include <asm/kvm_virtio.h>
26 #include <asm/setup.h>
27 #include <asm/s390_ext.h>
29 #define VIRTIO_SUBCODE_64 0x0D00
32 * The pointer to our (page) of device descriptions.
34 static void *kvm_devices;
35 struct work_struct hotplug_work;
38 struct virtio_device vdev;
39 struct kvm_device_desc *desc;
42 #define to_kvmdev(vd) container_of(vd, struct kvm_device, vdev)
46 * - kvm_device_descriptor
47 * struct kvm_device_desc
53 static struct kvm_vqconfig *kvm_vq_config(const struct kvm_device_desc *desc)
55 return (struct kvm_vqconfig *)(desc + 1);
58 static u8 *kvm_vq_features(const struct kvm_device_desc *desc)
60 return (u8 *)(kvm_vq_config(desc) + desc->num_vq);
63 static u8 *kvm_vq_configspace(const struct kvm_device_desc *desc)
65 return kvm_vq_features(desc) + desc->feature_len * 2;
69 * The total size of the config page used by this device (incl. desc)
71 static unsigned desc_size(const struct kvm_device_desc *desc)
74 + desc->num_vq * sizeof(struct kvm_vqconfig)
75 + desc->feature_len * 2
79 /* This gets the device's feature bits. */
80 static u32 kvm_get_features(struct virtio_device *vdev)
84 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
85 u8 *in_features = kvm_vq_features(desc);
87 for (i = 0; i < min(desc->feature_len * 8, 32); i++)
88 if (in_features[i / 8] & (1 << (i % 8)))
93 static void kvm_finalize_features(struct virtio_device *vdev)
96 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
97 /* Second half of bitmap is features we accept. */
98 u8 *out_features = kvm_vq_features(desc) + desc->feature_len;
100 /* Give virtio_ring a chance to accept features. */
101 vring_transport_features(vdev);
103 memset(out_features, 0, desc->feature_len);
104 bits = min_t(unsigned, desc->feature_len, sizeof(vdev->features)) * 8;
105 for (i = 0; i < bits; i++) {
106 if (test_bit(i, vdev->features))
107 out_features[i / 8] |= (1 << (i % 8));
112 * Reading and writing elements in config space
114 static void kvm_get(struct virtio_device *vdev, unsigned int offset,
115 void *buf, unsigned len)
117 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
119 BUG_ON(offset + len > desc->config_len);
120 memcpy(buf, kvm_vq_configspace(desc) + offset, len);
123 static void kvm_set(struct virtio_device *vdev, unsigned int offset,
124 const void *buf, unsigned len)
126 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
128 BUG_ON(offset + len > desc->config_len);
129 memcpy(kvm_vq_configspace(desc) + offset, buf, len);
133 * The operations to get and set the status word just access
134 * the status field of the device descriptor. set_status will also
135 * make a hypercall to the host, to tell about status changes
137 static u8 kvm_get_status(struct virtio_device *vdev)
139 return to_kvmdev(vdev)->desc->status;
142 static void kvm_set_status(struct virtio_device *vdev, u8 status)
145 to_kvmdev(vdev)->desc->status = status;
146 kvm_hypercall1(KVM_S390_VIRTIO_SET_STATUS,
147 (unsigned long) to_kvmdev(vdev)->desc);
151 * To reset the device, we use the KVM_VIRTIO_RESET hypercall, using the
152 * descriptor address. The Host will zero the status and all the
155 static void kvm_reset(struct virtio_device *vdev)
157 kvm_hypercall1(KVM_S390_VIRTIO_RESET,
158 (unsigned long) to_kvmdev(vdev)->desc);
162 * When the virtio_ring code wants to notify the Host, it calls us here and we
163 * make a hypercall. We hand the address of the virtqueue so the Host
164 * knows which virtqueue we're talking about.
166 static void kvm_notify(struct virtqueue *vq)
168 struct kvm_vqconfig *config = vq->priv;
170 kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, config->address);
174 * This routine finds the first virtqueue described in the configuration of
175 * this device and sets it up.
177 static struct virtqueue *kvm_find_vq(struct virtio_device *vdev,
179 void (*callback)(struct virtqueue *vq),
182 struct kvm_device *kdev = to_kvmdev(vdev);
183 struct kvm_vqconfig *config;
184 struct virtqueue *vq;
187 if (index >= kdev->desc->num_vq)
188 return ERR_PTR(-ENOENT);
190 config = kvm_vq_config(kdev->desc)+index;
192 err = vmem_add_mapping(config->address,
193 vring_size(config->num,
194 KVM_S390_VIRTIO_RING_ALIGN));
198 vq = vring_new_virtqueue(config->num, KVM_S390_VIRTIO_RING_ALIGN,
199 vdev, (void *) config->address,
200 kvm_notify, callback, name);
207 * register a callback token
208 * The host will sent this via the external interrupt parameter
210 config->token = (u64) vq;
215 vmem_remove_mapping(config->address,
216 vring_size(config->num,
217 KVM_S390_VIRTIO_RING_ALIGN));
222 static void kvm_del_vq(struct virtqueue *vq)
224 struct kvm_vqconfig *config = vq->priv;
226 vring_del_virtqueue(vq);
227 vmem_remove_mapping(config->address,
228 vring_size(config->num,
229 KVM_S390_VIRTIO_RING_ALIGN));
232 static void kvm_del_vqs(struct virtio_device *vdev)
234 struct virtqueue *vq, *n;
236 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
240 static int kvm_find_vqs(struct virtio_device *vdev, unsigned nvqs,
241 struct virtqueue *vqs[],
242 vq_callback_t *callbacks[],
245 struct kvm_device *kdev = to_kvmdev(vdev);
248 /* We must have this many virtqueues. */
249 if (nvqs > kdev->desc->num_vq)
252 for (i = 0; i < nvqs; ++i) {
253 vqs[i] = kvm_find_vq(vdev, i, callbacks[i], names[i]);
261 return PTR_ERR(vqs[i]);
265 * The config ops structure as defined by virtio config
267 static struct virtio_config_ops kvm_vq_configspace_ops = {
268 .get_features = kvm_get_features,
269 .finalize_features = kvm_finalize_features,
272 .get_status = kvm_get_status,
273 .set_status = kvm_set_status,
275 .find_vqs = kvm_find_vqs,
276 .del_vqs = kvm_del_vqs,
280 * The root device for the kvm virtio devices.
281 * This makes them appear as /sys/devices/kvm_s390/0,1,2 not /sys/devices/0,1,2.
283 static struct device *kvm_root;
286 * adds a new device and register it with virtio
287 * appropriate drivers are loaded by the device model
289 static void add_kvm_device(struct kvm_device_desc *d, unsigned int offset)
291 struct kvm_device *kdev;
293 kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
295 printk(KERN_EMERG "Cannot allocate kvm dev %u type %u\n",
300 kdev->vdev.dev.parent = kvm_root;
301 kdev->vdev.id.device = d->type;
302 kdev->vdev.config = &kvm_vq_configspace_ops;
305 if (register_virtio_device(&kdev->vdev) != 0) {
306 printk(KERN_ERR "Failed to register kvm device %u type %u\n",
313 * scan_devices() simply iterates through the device page.
314 * The type 0 is reserved to mean "end of devices".
316 static void scan_devices(void)
319 struct kvm_device_desc *d;
321 for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
327 add_kvm_device(d, i);
332 * match for a kvm device with a specific desc pointer
334 static int match_desc(struct device *dev, void *data)
336 if ((ulong)to_kvmdev(dev_to_virtio(dev))->desc == (ulong)data)
343 * hotplug_device tries to find changes in the device page.
345 static void hotplug_devices(struct work_struct *dummy)
348 struct kvm_device_desc *d;
351 for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
358 /* device already exists */
359 dev = device_find_child(kvm_root, d, match_desc);
361 /* XXX check for hotplug remove */
367 printk(KERN_INFO "Adding new virtio device %p\n", d);
368 add_kvm_device(d, i);
373 * we emulate the request_irq behaviour on top of s390 extints
375 static void kvm_extint_handler(unsigned int ext_int_code,
376 unsigned int param32, unsigned long param64)
378 struct virtqueue *vq;
382 subcode = ext_int_code >> 16;
383 if ((subcode & 0xff00) != VIRTIO_SUBCODE_64)
386 /* The LSB might be overloaded, we have to mask it */
387 vq = (struct virtqueue *)(param64 & ~1UL);
389 /* We use ext_params to decide what this interrupt means */
390 param = param32 & VIRTIO_PARAM_MASK;
393 case VIRTIO_PARAM_CONFIG_CHANGED:
395 struct virtio_driver *drv;
396 drv = container_of(vq->vdev->dev.driver,
397 struct virtio_driver, driver);
398 if (drv->config_changed)
399 drv->config_changed(vq->vdev);
403 case VIRTIO_PARAM_DEV_ADD:
404 schedule_work(&hotplug_work);
406 case VIRTIO_PARAM_VRING_INTERRUPT:
408 vring_interrupt(0, vq);
414 * Init function for virtio
415 * devices are in a single page above top of "normal" mem
417 static int __init kvm_devices_init(void)
424 kvm_root = root_device_register("kvm_s390");
425 if (IS_ERR(kvm_root)) {
426 rc = PTR_ERR(kvm_root);
427 printk(KERN_ERR "Could not register kvm_s390 root device");
431 rc = vmem_add_mapping(real_memory_size, PAGE_SIZE);
433 root_device_unregister(kvm_root);
437 kvm_devices = (void *) real_memory_size;
439 INIT_WORK(&hotplug_work, hotplug_devices);
442 register_external_interrupt(0x2603, kvm_extint_handler);
448 /* code for early console output with virtio_console */
449 static __init int early_put_chars(u32 vtermno, const char *buf, int count)
452 unsigned int len = count;
454 if (len > sizeof(scratch) - 1)
455 len = sizeof(scratch) - 1;
457 memcpy(scratch, buf, len);
458 kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, __pa(scratch));
462 static int __init s390_virtio_console_init(void)
466 return virtio_cons_early_init(early_put_chars);
468 console_initcall(s390_virtio_console_init);
472 * We do this after core stuff, but before the drivers.
474 postcore_initcall(kvm_devices_init);