blob: 375aeeaf9ea5e9b9a97e47e6e0ee5a4ba73f78b3 [file] [log] [blame]
Christian Borntraegere976a2b2008-03-25 18:47:46 +01001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Christian Borntraegerfaeba8302008-06-20 15:24:18 +020019#include <linux/virtio_console.h>
Christian Borntraegere976a2b2008-03-25 18:47:46 +010020#include <linux/interrupt.h>
21#include <linux/virtio_ring.h>
Heiko Carstens17f34582008-04-30 13:38:47 +020022#include <linux/pfn.h>
Christian Borntraegere976a2b2008-03-25 18:47:46 +010023#include <asm/io.h>
24#include <asm/kvm_para.h>
25#include <asm/kvm_virtio.h>
26#include <asm/setup.h>
27#include <asm/s390_ext.h>
28
29#define VIRTIO_SUBCODE_64 0x0D00
30
31/*
32 * The pointer to our (page) of device descriptions.
33 */
34static void *kvm_devices;
Alexander Grafcefa33e2010-08-24 15:48:51 +020035struct work_struct hotplug_work;
Christian Borntraegere976a2b2008-03-25 18:47:46 +010036
Christian Borntraegere976a2b2008-03-25 18:47:46 +010037struct kvm_device {
38 struct virtio_device vdev;
39 struct kvm_device_desc *desc;
40};
41
42#define to_kvmdev(vd) container_of(vd, struct kvm_device, vdev)
43
44/*
45 * memory layout:
46 * - kvm_device_descriptor
47 * struct kvm_device_desc
48 * - configuration
49 * struct kvm_vqconfig
50 * - feature bits
51 * - config space
52 */
53static struct kvm_vqconfig *kvm_vq_config(const struct kvm_device_desc *desc)
54{
55 return (struct kvm_vqconfig *)(desc + 1);
56}
57
58static u8 *kvm_vq_features(const struct kvm_device_desc *desc)
59{
60 return (u8 *)(kvm_vq_config(desc) + desc->num_vq);
61}
62
63static u8 *kvm_vq_configspace(const struct kvm_device_desc *desc)
64{
65 return kvm_vq_features(desc) + desc->feature_len * 2;
66}
67
68/*
69 * The total size of the config page used by this device (incl. desc)
70 */
71static unsigned desc_size(const struct kvm_device_desc *desc)
72{
73 return sizeof(*desc)
74 + desc->num_vq * sizeof(struct kvm_vqconfig)
75 + desc->feature_len * 2
76 + desc->config_len;
77}
78
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030079/* This gets the device's feature bits. */
80static u32 kvm_get_features(struct virtio_device *vdev)
Christian Borntraegere976a2b2008-03-25 18:47:46 +010081{
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030082 unsigned int i;
83 u32 features = 0;
Christian Borntraegere976a2b2008-03-25 18:47:46 +010084 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030085 u8 *in_features = kvm_vq_features(desc);
Christian Borntraegere976a2b2008-03-25 18:47:46 +010086
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030087 for (i = 0; i < min(desc->feature_len * 8, 32); i++)
88 if (in_features[i / 8] & (1 << (i % 8)))
89 features |= (1 << i);
90 return features;
91}
Christian Borntraegere976a2b2008-03-25 18:47:46 +010092
Rusty Russellc6248962008-07-25 12:06:07 -050093static void kvm_finalize_features(struct virtio_device *vdev)
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030094{
Rusty Russellc6248962008-07-25 12:06:07 -050095 unsigned int i, bits;
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030096 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;
Christian Borntraegere976a2b2008-03-25 18:47:46 +010099
Rusty Russelle34f8722008-07-25 12:06:13 -0500100 /* Give virtio_ring a chance to accept features. */
101 vring_transport_features(vdev);
102
Heiko Carstens5ca9fd52008-05-06 17:38:30 +0300103 memset(out_features, 0, desc->feature_len);
Rusty Russellc6248962008-07-25 12:06:07 -0500104 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))
Heiko Carstens5ca9fd52008-05-06 17:38:30 +0300107 out_features[i / 8] |= (1 << (i % 8));
108 }
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100109}
110
111/*
112 * Reading and writing elements in config space
113 */
114static void kvm_get(struct virtio_device *vdev, unsigned int offset,
115 void *buf, unsigned len)
116{
117 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
118
119 BUG_ON(offset + len > desc->config_len);
120 memcpy(buf, kvm_vq_configspace(desc) + offset, len);
121}
122
123static void kvm_set(struct virtio_device *vdev, unsigned int offset,
124 const void *buf, unsigned len)
125{
126 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
127
128 BUG_ON(offset + len > desc->config_len);
129 memcpy(kvm_vq_configspace(desc) + offset, buf, len);
130}
131
132/*
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
136 */
137static u8 kvm_get_status(struct virtio_device *vdev)
138{
139 return to_kvmdev(vdev)->desc->status;
140}
141
142static void kvm_set_status(struct virtio_device *vdev, u8 status)
143{
144 BUG_ON(!status);
145 to_kvmdev(vdev)->desc->status = status;
146 kvm_hypercall1(KVM_S390_VIRTIO_SET_STATUS,
147 (unsigned long) to_kvmdev(vdev)->desc);
148}
149
150/*
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
153 * features.
154 */
155static void kvm_reset(struct virtio_device *vdev)
156{
157 kvm_hypercall1(KVM_S390_VIRTIO_RESET,
158 (unsigned long) to_kvmdev(vdev)->desc);
159}
160
161/*
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.
165 */
166static void kvm_notify(struct virtqueue *vq)
167{
168 struct kvm_vqconfig *config = vq->priv;
169
170 kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, config->address);
171}
172
173/*
174 * This routine finds the first virtqueue described in the configuration of
175 * this device and sets it up.
176 */
177static struct virtqueue *kvm_find_vq(struct virtio_device *vdev,
Rusty Russell9499f5e2009-06-12 22:16:35 -0600178 unsigned index,
179 void (*callback)(struct virtqueue *vq),
180 const char *name)
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100181{
182 struct kvm_device *kdev = to_kvmdev(vdev);
183 struct kvm_vqconfig *config;
184 struct virtqueue *vq;
185 int err;
186
187 if (index >= kdev->desc->num_vq)
188 return ERR_PTR(-ENOENT);
189
190 config = kvm_vq_config(kdev->desc)+index;
191
Heiko Carstens17f34582008-04-30 13:38:47 +0200192 err = vmem_add_mapping(config->address,
Rusty Russelldb405982008-12-30 09:26:02 -0600193 vring_size(config->num,
194 KVM_S390_VIRTIO_RING_ALIGN));
Heiko Carstens17f34582008-04-30 13:38:47 +0200195 if (err)
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100196 goto out;
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100197
Rusty Russell87c7d572008-12-30 09:26:03 -0600198 vq = vring_new_virtqueue(config->num, KVM_S390_VIRTIO_RING_ALIGN,
199 vdev, (void *) config->address,
Rusty Russell9499f5e2009-06-12 22:16:35 -0600200 kvm_notify, callback, name);
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100201 if (!vq) {
202 err = -ENOMEM;
203 goto unmap;
204 }
205
206 /*
207 * register a callback token
208 * The host will sent this via the external interrupt parameter
209 */
210 config->token = (u64) vq;
211
212 vq->priv = config;
213 return vq;
214unmap:
Heiko Carstens17f34582008-04-30 13:38:47 +0200215 vmem_remove_mapping(config->address,
Rusty Russelldb405982008-12-30 09:26:02 -0600216 vring_size(config->num,
217 KVM_S390_VIRTIO_RING_ALIGN));
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100218out:
219 return ERR_PTR(err);
220}
221
222static void kvm_del_vq(struct virtqueue *vq)
223{
224 struct kvm_vqconfig *config = vq->priv;
225
226 vring_del_virtqueue(vq);
Heiko Carstens17f34582008-04-30 13:38:47 +0200227 vmem_remove_mapping(config->address,
Rusty Russelldb405982008-12-30 09:26:02 -0600228 vring_size(config->num,
229 KVM_S390_VIRTIO_RING_ALIGN));
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100230}
231
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600232static void kvm_del_vqs(struct virtio_device *vdev)
233{
234 struct virtqueue *vq, *n;
235
236 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
237 kvm_del_vq(vq);
238}
239
240static int kvm_find_vqs(struct virtio_device *vdev, unsigned nvqs,
241 struct virtqueue *vqs[],
242 vq_callback_t *callbacks[],
243 const char *names[])
244{
245 struct kvm_device *kdev = to_kvmdev(vdev);
246 int i;
247
248 /* We must have this many virtqueues. */
249 if (nvqs > kdev->desc->num_vq)
250 return -ENOENT;
251
252 for (i = 0; i < nvqs; ++i) {
253 vqs[i] = kvm_find_vq(vdev, i, callbacks[i], names[i]);
254 if (IS_ERR(vqs[i]))
255 goto error;
256 }
257 return 0;
258
259error:
260 kvm_del_vqs(vdev);
261 return PTR_ERR(vqs[i]);
262}
263
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100264/*
265 * The config ops structure as defined by virtio config
266 */
267static struct virtio_config_ops kvm_vq_configspace_ops = {
Heiko Carstens5ca9fd52008-05-06 17:38:30 +0300268 .get_features = kvm_get_features,
Rusty Russellc6248962008-07-25 12:06:07 -0500269 .finalize_features = kvm_finalize_features,
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100270 .get = kvm_get,
271 .set = kvm_set,
272 .get_status = kvm_get_status,
273 .set_status = kvm_set_status,
274 .reset = kvm_reset,
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600275 .find_vqs = kvm_find_vqs,
276 .del_vqs = kvm_del_vqs,
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100277};
278
279/*
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.
282 */
Cornelia Huck37f1c012008-10-10 21:33:12 +0200283static struct device *kvm_root;
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100284
285/*
286 * adds a new device and register it with virtio
287 * appropriate drivers are loaded by the device model
288 */
Rusty Russellb769f572008-05-30 15:09:42 -0500289static void add_kvm_device(struct kvm_device_desc *d, unsigned int offset)
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100290{
291 struct kvm_device *kdev;
292
293 kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
294 if (!kdev) {
Rusty Russellb769f572008-05-30 15:09:42 -0500295 printk(KERN_EMERG "Cannot allocate kvm dev %u type %u\n",
296 offset, d->type);
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100297 return;
298 }
299
Cornelia Huck37f1c012008-10-10 21:33:12 +0200300 kdev->vdev.dev.parent = kvm_root;
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100301 kdev->vdev.id.device = d->type;
302 kdev->vdev.config = &kvm_vq_configspace_ops;
303 kdev->desc = d;
304
305 if (register_virtio_device(&kdev->vdev) != 0) {
Rusty Russellb769f572008-05-30 15:09:42 -0500306 printk(KERN_ERR "Failed to register kvm device %u type %u\n",
307 offset, d->type);
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100308 kfree(kdev);
309 }
310}
311
312/*
313 * scan_devices() simply iterates through the device page.
314 * The type 0 is reserved to mean "end of devices".
315 */
316static void scan_devices(void)
317{
318 unsigned int i;
319 struct kvm_device_desc *d;
320
321 for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
322 d = kvm_devices + i;
323
324 if (d->type == 0)
325 break;
326
Rusty Russellb769f572008-05-30 15:09:42 -0500327 add_kvm_device(d, i);
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100328 }
329}
330
331/*
Alexander Grafcefa33e2010-08-24 15:48:51 +0200332 * match for a kvm device with a specific desc pointer
333 */
334static int match_desc(struct device *dev, void *data)
335{
336 if ((ulong)to_kvmdev(dev_to_virtio(dev))->desc == (ulong)data)
337 return 1;
338
339 return 0;
340}
341
342/*
343 * hotplug_device tries to find changes in the device page.
344 */
345static void hotplug_devices(struct work_struct *dummy)
346{
347 unsigned int i;
348 struct kvm_device_desc *d;
349 struct device *dev;
350
351 for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
352 d = kvm_devices + i;
353
354 /* end of list */
355 if (d->type == 0)
356 break;
357
358 /* device already exists */
359 dev = device_find_child(kvm_root, d, match_desc);
360 if (dev) {
361 /* XXX check for hotplug remove */
362 put_device(dev);
363 continue;
364 }
365
366 /* new device */
367 printk(KERN_INFO "Adding new virtio device %p\n", d);
368 add_kvm_device(d, i);
369 }
370}
371
372/*
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100373 * we emulate the request_irq behaviour on top of s390 extints
374 */
Martin Schwidefskyf6649a72010-10-25 16:10:38 +0200375static void kvm_extint_handler(unsigned int ext_int_code,
376 unsigned int param32, unsigned long param64)
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100377{
Christian Borntraegerbe3c5832008-11-18 22:44:13 +0100378 struct virtqueue *vq;
379 u16 subcode;
Alexander Graffc678d62010-08-24 15:48:50 +0200380 u32 param;
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100381
Martin Schwidefskyf6649a72010-10-25 16:10:38 +0200382 subcode = ext_int_code >> 16;
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100383 if ((subcode & 0xff00) != VIRTIO_SUBCODE_64)
384 return;
385
Christian Borntraegerbe3c5832008-11-18 22:44:13 +0100386 /* The LSB might be overloaded, we have to mask it */
Martin Schwidefskyf6649a72010-10-25 16:10:38 +0200387 vq = (struct virtqueue *)(param64 & ~1UL);
Christian Borntraegerbe3c5832008-11-18 22:44:13 +0100388
Alexander Graffc678d62010-08-24 15:48:50 +0200389 /* We use ext_params to decide what this interrupt means */
Martin Schwidefskyf6649a72010-10-25 16:10:38 +0200390 param = param32 & VIRTIO_PARAM_MASK;
Christian Borntraegerbe3c5832008-11-18 22:44:13 +0100391
Alexander Graffc678d62010-08-24 15:48:50 +0200392 switch (param) {
393 case VIRTIO_PARAM_CONFIG_CHANGED:
394 {
Christian Borntraegerbe3c5832008-11-18 22:44:13 +0100395 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);
Alexander Graffc678d62010-08-24 15:48:50 +0200400
401 break;
402 }
Alexander Grafcefa33e2010-08-24 15:48:51 +0200403 case VIRTIO_PARAM_DEV_ADD:
404 schedule_work(&hotplug_work);
405 break;
Alexander Graffc678d62010-08-24 15:48:50 +0200406 case VIRTIO_PARAM_VRING_INTERRUPT:
407 default:
Christian Borntraegerbe3c5832008-11-18 22:44:13 +0100408 vring_interrupt(0, vq);
Alexander Graffc678d62010-08-24 15:48:50 +0200409 break;
410 }
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100411}
412
413/*
414 * Init function for virtio
415 * devices are in a single page above top of "normal" mem
416 */
417static int __init kvm_devices_init(void)
418{
419 int rc;
420
421 if (!MACHINE_IS_KVM)
422 return -ENODEV;
423
Mark McLoughlin035da162008-12-15 12:58:29 +0000424 kvm_root = root_device_register("kvm_s390");
Cornelia Huck37f1c012008-10-10 21:33:12 +0200425 if (IS_ERR(kvm_root)) {
426 rc = PTR_ERR(kvm_root);
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100427 printk(KERN_ERR "Could not register kvm_s390 root device");
428 return rc;
429 }
430
Christian Borntraegercc835f782008-11-14 18:18:02 +0100431 rc = vmem_add_mapping(real_memory_size, PAGE_SIZE);
Heiko Carstens17f34582008-04-30 13:38:47 +0200432 if (rc) {
Mark McLoughlin035da162008-12-15 12:58:29 +0000433 root_device_unregister(kvm_root);
Heiko Carstens17f34582008-04-30 13:38:47 +0200434 return rc;
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100435 }
436
Christian Borntraegercc835f782008-11-14 18:18:02 +0100437 kvm_devices = (void *) real_memory_size;
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100438
Alexander Grafcefa33e2010-08-24 15:48:51 +0200439 INIT_WORK(&hotplug_work, hotplug_devices);
440
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100441 ctl_set_bit(0, 9);
442 register_external_interrupt(0x2603, kvm_extint_handler);
443
444 scan_devices();
445 return 0;
446}
447
Christian Borntraegerfaeba8302008-06-20 15:24:18 +0200448/* code for early console output with virtio_console */
449static __init int early_put_chars(u32 vtermno, const char *buf, int count)
450{
451 char scratch[17];
452 unsigned int len = count;
453
454 if (len > sizeof(scratch) - 1)
455 len = sizeof(scratch) - 1;
456 scratch[len] = '\0';
457 memcpy(scratch, buf, len);
458 kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, __pa(scratch));
459 return len;
460}
461
Hendrik Bruecknerc4de0c12009-09-11 10:28:56 +0200462static int __init s390_virtio_console_init(void)
Christian Borntraegerfaeba8302008-06-20 15:24:18 +0200463{
Hendrik Bruecknerc4de0c12009-09-11 10:28:56 +0200464 if (!MACHINE_IS_KVM)
465 return -ENODEV;
466 return virtio_cons_early_init(early_put_chars);
Christian Borntraegerfaeba8302008-06-20 15:24:18 +0200467}
Hendrik Bruecknerc4de0c12009-09-11 10:28:56 +0200468console_initcall(s390_virtio_console_init);
469
Christian Borntraegerfaeba8302008-06-20 15:24:18 +0200470
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100471/*
472 * We do this after core stuff, but before the drivers.
473 */
474postcore_initcall(kvm_devices_init);