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/interrupt.h>
19 #include <linux/virtio_ring.h>
21 #include <asm/kvm_para.h>
22 #include <asm/kvm_virtio.h>
23 #include <asm/setup.h>
24 #include <asm/s390_ext.h>
26 #define VIRTIO_SUBCODE_64 0x0D00
29 * The pointer to our (page) of device descriptions.
31 static void *kvm_devices;
34 * Unique numbering for kvm devices.
36 static unsigned int dev_index;
39 struct virtio_device vdev;
40 struct kvm_device_desc *desc;
43 #define to_kvmdev(vd) container_of(vd, struct kvm_device, vdev)
47 * - kvm_device_descriptor
48 * struct kvm_device_desc
54 static struct kvm_vqconfig *kvm_vq_config(const struct kvm_device_desc *desc)
56 return (struct kvm_vqconfig *)(desc + 1);
59 static u8 *kvm_vq_features(const struct kvm_device_desc *desc)
61 return (u8 *)(kvm_vq_config(desc) + desc->num_vq);
64 static u8 *kvm_vq_configspace(const struct kvm_device_desc *desc)
66 return kvm_vq_features(desc) + desc->feature_len * 2;
70 * The total size of the config page used by this device (incl. desc)
72 static unsigned desc_size(const struct kvm_device_desc *desc)
75 + desc->num_vq * sizeof(struct kvm_vqconfig)
76 + desc->feature_len * 2
81 * This tests (and acknowleges) a feature bit.
83 static bool kvm_feature(struct virtio_device *vdev, unsigned fbit)
85 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
88 if (fbit / 8 > desc->feature_len)
91 features = kvm_vq_features(desc);
92 if (!(features[fbit / 8] & (1 << (fbit % 8))))
96 * We set the matching bit in the other half of the bitmap to tell the
97 * Host we want to use this feature.
99 features[desc->feature_len + fbit / 8] |= (1 << (fbit % 8));
104 * Reading and writing elements in config space
106 static void kvm_get(struct virtio_device *vdev, unsigned int offset,
107 void *buf, unsigned len)
109 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
111 BUG_ON(offset + len > desc->config_len);
112 memcpy(buf, kvm_vq_configspace(desc) + offset, len);
115 static void kvm_set(struct virtio_device *vdev, unsigned int offset,
116 const void *buf, unsigned len)
118 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
120 BUG_ON(offset + len > desc->config_len);
121 memcpy(kvm_vq_configspace(desc) + offset, buf, len);
125 * The operations to get and set the status word just access
126 * the status field of the device descriptor. set_status will also
127 * make a hypercall to the host, to tell about status changes
129 static u8 kvm_get_status(struct virtio_device *vdev)
131 return to_kvmdev(vdev)->desc->status;
134 static void kvm_set_status(struct virtio_device *vdev, u8 status)
137 to_kvmdev(vdev)->desc->status = status;
138 kvm_hypercall1(KVM_S390_VIRTIO_SET_STATUS,
139 (unsigned long) to_kvmdev(vdev)->desc);
143 * To reset the device, we use the KVM_VIRTIO_RESET hypercall, using the
144 * descriptor address. The Host will zero the status and all the
147 static void kvm_reset(struct virtio_device *vdev)
149 kvm_hypercall1(KVM_S390_VIRTIO_RESET,
150 (unsigned long) to_kvmdev(vdev)->desc);
154 * When the virtio_ring code wants to notify the Host, it calls us here and we
155 * make a hypercall. We hand the address of the virtqueue so the Host
156 * knows which virtqueue we're talking about.
158 static void kvm_notify(struct virtqueue *vq)
160 struct kvm_vqconfig *config = vq->priv;
162 kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, config->address);
166 * This routine finds the first virtqueue described in the configuration of
167 * this device and sets it up.
169 static struct virtqueue *kvm_find_vq(struct virtio_device *vdev,
171 void (*callback)(struct virtqueue *vq))
173 struct kvm_device *kdev = to_kvmdev(vdev);
174 struct kvm_vqconfig *config;
175 struct virtqueue *vq;
178 if (index >= kdev->desc->num_vq)
179 return ERR_PTR(-ENOENT);
181 config = kvm_vq_config(kdev->desc)+index;
183 if (add_shared_memory(config->address,
184 vring_size(config->num, PAGE_SIZE))) {
189 vq = vring_new_virtqueue(config->num, vdev, (void *) config->address,
190 kvm_notify, callback);
197 * register a callback token
198 * The host will sent this via the external interrupt parameter
200 config->token = (u64) vq;
205 remove_shared_memory(config->address, vring_size(config->num,
211 static void kvm_del_vq(struct virtqueue *vq)
213 struct kvm_vqconfig *config = vq->priv;
215 vring_del_virtqueue(vq);
216 remove_shared_memory(config->address,
217 vring_size(config->num, PAGE_SIZE));
221 * The config ops structure as defined by virtio config
223 static struct virtio_config_ops kvm_vq_configspace_ops = {
224 .feature = kvm_feature,
227 .get_status = kvm_get_status,
228 .set_status = kvm_set_status,
230 .find_vq = kvm_find_vq,
231 .del_vq = kvm_del_vq,
235 * The root device for the kvm virtio devices.
236 * This makes them appear as /sys/devices/kvm_s390/0,1,2 not /sys/devices/0,1,2.
238 static struct device kvm_root = {
240 .bus_id = "kvm_s390",
244 * adds a new device and register it with virtio
245 * appropriate drivers are loaded by the device model
247 static void add_kvm_device(struct kvm_device_desc *d)
249 struct kvm_device *kdev;
251 kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
253 printk(KERN_EMERG "Cannot allocate kvm dev %u\n",
258 kdev->vdev.dev.parent = &kvm_root;
259 kdev->vdev.index = dev_index++;
260 kdev->vdev.id.device = d->type;
261 kdev->vdev.config = &kvm_vq_configspace_ops;
264 if (register_virtio_device(&kdev->vdev) != 0) {
265 printk(KERN_ERR "Failed to register kvm device %u\n",
272 * scan_devices() simply iterates through the device page.
273 * The type 0 is reserved to mean "end of devices".
275 static void scan_devices(void)
278 struct kvm_device_desc *d;
280 for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
291 * we emulate the request_irq behaviour on top of s390 extints
293 static void kvm_extint_handler(u16 code)
295 void *data = (void *) *(long *) __LC_PFAULT_INTPARM;
296 u16 subcode = S390_lowcore.cpu_addr;
298 if ((subcode & 0xff00) != VIRTIO_SUBCODE_64)
301 vring_interrupt(0, data);
305 * Init function for virtio
306 * devices are in a single page above top of "normal" mem
308 static int __init kvm_devices_init(void)
315 rc = device_register(&kvm_root);
317 printk(KERN_ERR "Could not register kvm_s390 root device");
321 if (add_shared_memory((max_pfn) << PAGE_SHIFT, PAGE_SIZE)) {
322 device_unregister(&kvm_root);
326 kvm_devices = (void *) (max_pfn << PAGE_SHIFT);
329 register_external_interrupt(0x2603, kvm_extint_handler);
336 * We do this after core stuff, but before the drivers.
338 postcore_initcall(kvm_devices_init);