]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/s390/kvm/kvm_virtio.c
d41f234bb2c28445361f3027cd39bb2542c93b81
[linux-2.6-omap-h63xx.git] / drivers / s390 / kvm / kvm_virtio.c
1 /*
2  * kvm_virtio.c - 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/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/virtio_console.h>
19 #include <linux/interrupt.h>
20 #include <linux/virtio_ring.h>
21 #include <linux/pfn.h>
22 #include <asm/io.h>
23 #include <asm/kvm_para.h>
24 #include <asm/kvm_virtio.h>
25 #include <asm/setup.h>
26 #include <asm/s390_ext.h>
27
28 #define VIRTIO_SUBCODE_64 0x0D00
29
30 /*
31  * The pointer to our (page) of device descriptions.
32  */
33 static void *kvm_devices;
34
35 struct kvm_device {
36         struct virtio_device vdev;
37         struct kvm_device_desc *desc;
38 };
39
40 #define to_kvmdev(vd) container_of(vd, struct kvm_device, vdev)
41
42 /*
43  * memory layout:
44  * - kvm_device_descriptor
45  *        struct kvm_device_desc
46  * - configuration
47  *        struct kvm_vqconfig
48  * - feature bits
49  * - config space
50  */
51 static struct kvm_vqconfig *kvm_vq_config(const struct kvm_device_desc *desc)
52 {
53         return (struct kvm_vqconfig *)(desc + 1);
54 }
55
56 static u8 *kvm_vq_features(const struct kvm_device_desc *desc)
57 {
58         return (u8 *)(kvm_vq_config(desc) + desc->num_vq);
59 }
60
61 static u8 *kvm_vq_configspace(const struct kvm_device_desc *desc)
62 {
63         return kvm_vq_features(desc) + desc->feature_len * 2;
64 }
65
66 /*
67  * The total size of the config page used by this device (incl. desc)
68  */
69 static unsigned desc_size(const struct kvm_device_desc *desc)
70 {
71         return sizeof(*desc)
72                 + desc->num_vq * sizeof(struct kvm_vqconfig)
73                 + desc->feature_len * 2
74                 + desc->config_len;
75 }
76
77 /* This gets the device's feature bits. */
78 static u32 kvm_get_features(struct virtio_device *vdev)
79 {
80         unsigned int i;
81         u32 features = 0;
82         struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
83         u8 *in_features = kvm_vq_features(desc);
84
85         for (i = 0; i < min(desc->feature_len * 8, 32); i++)
86                 if (in_features[i / 8] & (1 << (i % 8)))
87                         features |= (1 << i);
88         return features;
89 }
90
91 static void kvm_set_features(struct virtio_device *vdev, u32 features)
92 {
93         unsigned int i;
94         struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
95         /* Second half of bitmap is features we accept. */
96         u8 *out_features = kvm_vq_features(desc) + desc->feature_len;
97
98         memset(out_features, 0, desc->feature_len);
99         for (i = 0; i < min(desc->feature_len * 8, 32); i++) {
100                 if (features & (1 << i))
101                         out_features[i / 8] |= (1 << (i % 8));
102         }
103 }
104
105 /*
106  * Reading and writing elements in config space
107  */
108 static void kvm_get(struct virtio_device *vdev, unsigned int offset,
109                    void *buf, unsigned len)
110 {
111         struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
112
113         BUG_ON(offset + len > desc->config_len);
114         memcpy(buf, kvm_vq_configspace(desc) + offset, len);
115 }
116
117 static void kvm_set(struct virtio_device *vdev, unsigned int offset,
118                    const 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(kvm_vq_configspace(desc) + offset, buf, len);
124 }
125
126 /*
127  * The operations to get and set the status word just access
128  * the status field of the device descriptor. set_status will also
129  * make a hypercall to the host, to tell about status changes
130  */
131 static u8 kvm_get_status(struct virtio_device *vdev)
132 {
133         return to_kvmdev(vdev)->desc->status;
134 }
135
136 static void kvm_set_status(struct virtio_device *vdev, u8 status)
137 {
138         BUG_ON(!status);
139         to_kvmdev(vdev)->desc->status = status;
140         kvm_hypercall1(KVM_S390_VIRTIO_SET_STATUS,
141                        (unsigned long) to_kvmdev(vdev)->desc);
142 }
143
144 /*
145  * To reset the device, we use the KVM_VIRTIO_RESET hypercall, using the
146  * descriptor address. The Host will zero the status and all the
147  * features.
148  */
149 static void kvm_reset(struct virtio_device *vdev)
150 {
151         kvm_hypercall1(KVM_S390_VIRTIO_RESET,
152                        (unsigned long) to_kvmdev(vdev)->desc);
153 }
154
155 /*
156  * When the virtio_ring code wants to notify the Host, it calls us here and we
157  * make a hypercall.  We hand the address  of the virtqueue so the Host
158  * knows which virtqueue we're talking about.
159  */
160 static void kvm_notify(struct virtqueue *vq)
161 {
162         struct kvm_vqconfig *config = vq->priv;
163
164         kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, config->address);
165 }
166
167 /*
168  * This routine finds the first virtqueue described in the configuration of
169  * this device and sets it up.
170  */
171 static struct virtqueue *kvm_find_vq(struct virtio_device *vdev,
172                                     unsigned index,
173                                     void (*callback)(struct virtqueue *vq))
174 {
175         struct kvm_device *kdev = to_kvmdev(vdev);
176         struct kvm_vqconfig *config;
177         struct virtqueue *vq;
178         int err;
179
180         if (index >= kdev->desc->num_vq)
181                 return ERR_PTR(-ENOENT);
182
183         config = kvm_vq_config(kdev->desc)+index;
184
185         err = vmem_add_mapping(config->address,
186                                vring_size(config->num, PAGE_SIZE));
187         if (err)
188                 goto out;
189
190         vq = vring_new_virtqueue(config->num, vdev, (void *) config->address,
191                                  kvm_notify, callback);
192         if (!vq) {
193                 err = -ENOMEM;
194                 goto unmap;
195         }
196
197         /*
198          * register a callback token
199          * The host will sent this via the external interrupt parameter
200          */
201         config->token = (u64) vq;
202
203         vq->priv = config;
204         return vq;
205 unmap:
206         vmem_remove_mapping(config->address,
207                             vring_size(config->num, PAGE_SIZE));
208 out:
209         return ERR_PTR(err);
210 }
211
212 static void kvm_del_vq(struct virtqueue *vq)
213 {
214         struct kvm_vqconfig *config = vq->priv;
215
216         vring_del_virtqueue(vq);
217         vmem_remove_mapping(config->address,
218                             vring_size(config->num, PAGE_SIZE));
219 }
220
221 /*
222  * The config ops structure as defined by virtio config
223  */
224 static struct virtio_config_ops kvm_vq_configspace_ops = {
225         .get_features = kvm_get_features,
226         .set_features = kvm_set_features,
227         .get = kvm_get,
228         .set = kvm_set,
229         .get_status = kvm_get_status,
230         .set_status = kvm_set_status,
231         .reset = kvm_reset,
232         .find_vq = kvm_find_vq,
233         .del_vq = kvm_del_vq,
234 };
235
236 /*
237  * The root device for the kvm virtio devices.
238  * This makes them appear as /sys/devices/kvm_s390/0,1,2 not /sys/devices/0,1,2.
239  */
240 static struct device kvm_root = {
241         .parent = NULL,
242         .bus_id = "kvm_s390",
243 };
244
245 /*
246  * adds a new device and register it with virtio
247  * appropriate drivers are loaded by the device model
248  */
249 static void add_kvm_device(struct kvm_device_desc *d, unsigned int offset)
250 {
251         struct kvm_device *kdev;
252
253         kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
254         if (!kdev) {
255                 printk(KERN_EMERG "Cannot allocate kvm dev %u type %u\n",
256                        offset, d->type);
257                 return;
258         }
259
260         kdev->vdev.dev.parent = &kvm_root;
261         kdev->vdev.id.device = d->type;
262         kdev->vdev.config = &kvm_vq_configspace_ops;
263         kdev->desc = d;
264
265         if (register_virtio_device(&kdev->vdev) != 0) {
266                 printk(KERN_ERR "Failed to register kvm device %u type %u\n",
267                        offset, d->type);
268                 kfree(kdev);
269         }
270 }
271
272 /*
273  * scan_devices() simply iterates through the device page.
274  * The type 0 is reserved to mean "end of devices".
275  */
276 static void scan_devices(void)
277 {
278         unsigned int i;
279         struct kvm_device_desc *d;
280
281         for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
282                 d = kvm_devices + i;
283
284                 if (d->type == 0)
285                         break;
286
287                 add_kvm_device(d, i);
288         }
289 }
290
291 /*
292  * we emulate the request_irq behaviour on top of s390 extints
293  */
294 static void kvm_extint_handler(u16 code)
295 {
296         void *data = (void *) *(long *) __LC_PFAULT_INTPARM;
297         u16 subcode = S390_lowcore.cpu_addr;
298
299         if ((subcode & 0xff00) != VIRTIO_SUBCODE_64)
300                 return;
301
302         vring_interrupt(0, data);
303 }
304
305 /*
306  * Init function for virtio
307  * devices are in a single page above top of "normal" mem
308  */
309 static int __init kvm_devices_init(void)
310 {
311         int rc;
312
313         if (!MACHINE_IS_KVM)
314                 return -ENODEV;
315
316         rc = device_register(&kvm_root);
317         if (rc) {
318                 printk(KERN_ERR "Could not register kvm_s390 root device");
319                 return rc;
320         }
321
322         rc = vmem_add_mapping(PFN_PHYS(max_pfn), PAGE_SIZE);
323         if (rc) {
324                 device_unregister(&kvm_root);
325                 return rc;
326         }
327
328         kvm_devices = (void *) PFN_PHYS(max_pfn);
329
330         ctl_set_bit(0, 9);
331         register_external_interrupt(0x2603, kvm_extint_handler);
332
333         scan_devices();
334         return 0;
335 }
336
337 /* code for early console output with virtio_console */
338 static __init int early_put_chars(u32 vtermno, const char *buf, int count)
339 {
340         char scratch[17];
341         unsigned int len = count;
342
343         if (len > sizeof(scratch) - 1)
344                 len = sizeof(scratch) - 1;
345         scratch[len] = '\0';
346         memcpy(scratch, buf, len);
347         kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, __pa(scratch));
348         return len;
349 }
350
351 void s390_virtio_console_init(void)
352 {
353         virtio_cons_early_init(early_put_chars);
354 }
355
356 /*
357  * We do this after core stuff, but before the drivers.
358  */
359 postcore_initcall(kvm_devices_init);