blob: 6711e65764b5e7a4482564014ac6aa0e78189242 [file] [log] [blame]
Christian Borntraegere976a2b2008-03-25 18:47:46 +01001/*
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02002 * virtio for kvm on s390
Christian Borntraegere976a2b2008-03-25 18:47:46 +01003 *
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
Heiko Carstens052ff462011-01-05 12:47:28 +010013#include <linux/kernel_stat.h>
Christian Borntraegere976a2b2008-03-25 18:47:46 +010014#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Christian Borntraegerfaeba8302008-06-20 15:24:18 +020020#include <linux/virtio_console.h>
Christian Borntraegere976a2b2008-03-25 18:47:46 +010021#include <linux/interrupt.h>
22#include <linux/virtio_ring.h>
Heiko Carstens3a4c5d52011-07-30 09:25:15 +020023#include <linux/export.h>
Heiko Carstens17f34582008-04-30 13:38:47 +020024#include <linux/pfn.h>
Christian Borntraegere976a2b2008-03-25 18:47:46 +010025#include <asm/io.h>
26#include <asm/kvm_para.h>
27#include <asm/kvm_virtio.h>
Heinz Graalfscd183452012-06-11 16:06:59 +020028#include <asm/sclp.h>
Christian Borntraegere976a2b2008-03-25 18:47:46 +010029#include <asm/setup.h>
Heiko Carstens052ff462011-01-05 12:47:28 +010030#include <asm/irq.h>
Christian Borntraegere976a2b2008-03-25 18:47:46 +010031
32#define VIRTIO_SUBCODE_64 0x0D00
33
34/*
35 * The pointer to our (page) of device descriptions.
36 */
37static void *kvm_devices;
Martin Schwidefskyc4736d92011-10-30 15:17:11 +010038static struct work_struct hotplug_work;
Christian Borntraegere976a2b2008-03-25 18:47:46 +010039
Christian Borntraegere976a2b2008-03-25 18:47:46 +010040struct kvm_device {
41 struct virtio_device vdev;
42 struct kvm_device_desc *desc;
43};
44
45#define to_kvmdev(vd) container_of(vd, struct kvm_device, vdev)
46
47/*
48 * memory layout:
49 * - kvm_device_descriptor
50 * struct kvm_device_desc
51 * - configuration
52 * struct kvm_vqconfig
53 * - feature bits
54 * - config space
55 */
56static struct kvm_vqconfig *kvm_vq_config(const struct kvm_device_desc *desc)
57{
58 return (struct kvm_vqconfig *)(desc + 1);
59}
60
61static u8 *kvm_vq_features(const struct kvm_device_desc *desc)
62{
63 return (u8 *)(kvm_vq_config(desc) + desc->num_vq);
64}
65
66static u8 *kvm_vq_configspace(const struct kvm_device_desc *desc)
67{
68 return kvm_vq_features(desc) + desc->feature_len * 2;
69}
70
71/*
72 * The total size of the config page used by this device (incl. desc)
73 */
74static unsigned desc_size(const struct kvm_device_desc *desc)
75{
76 return sizeof(*desc)
77 + desc->num_vq * sizeof(struct kvm_vqconfig)
78 + desc->feature_len * 2
79 + desc->config_len;
80}
81
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030082/* This gets the device's feature bits. */
83static u32 kvm_get_features(struct virtio_device *vdev)
Christian Borntraegere976a2b2008-03-25 18:47:46 +010084{
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030085 unsigned int i;
86 u32 features = 0;
Christian Borntraegere976a2b2008-03-25 18:47:46 +010087 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030088 u8 *in_features = kvm_vq_features(desc);
Christian Borntraegere976a2b2008-03-25 18:47:46 +010089
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030090 for (i = 0; i < min(desc->feature_len * 8, 32); i++)
91 if (in_features[i / 8] & (1 << (i % 8)))
92 features |= (1 << i);
93 return features;
94}
Christian Borntraegere976a2b2008-03-25 18:47:46 +010095
Rusty Russellc6248962008-07-25 12:06:07 -050096static void kvm_finalize_features(struct virtio_device *vdev)
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030097{
Rusty Russellc6248962008-07-25 12:06:07 -050098 unsigned int i, bits;
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030099 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;
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100102
Rusty Russelle34f8722008-07-25 12:06:13 -0500103 /* Give virtio_ring a chance to accept features. */
104 vring_transport_features(vdev);
105
Heiko Carstens5ca9fd52008-05-06 17:38:30 +0300106 memset(out_features, 0, desc->feature_len);
Rusty Russellc6248962008-07-25 12:06:07 -0500107 bits = min_t(unsigned, desc->feature_len, sizeof(vdev->features)) * 8;
108 for (i = 0; i < bits; i++) {
109 if (test_bit(i, vdev->features))
Heiko Carstens5ca9fd52008-05-06 17:38:30 +0300110 out_features[i / 8] |= (1 << (i % 8));
111 }
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100112}
113
114/*
115 * Reading and writing elements in config space
116 */
117static void kvm_get(struct virtio_device *vdev, unsigned int offset,
118 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(buf, kvm_vq_configspace(desc) + offset, len);
124}
125
126static void kvm_set(struct virtio_device *vdev, unsigned int offset,
127 const void *buf, unsigned len)
128{
129 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
130
131 BUG_ON(offset + len > desc->config_len);
132 memcpy(kvm_vq_configspace(desc) + offset, buf, len);
133}
134
135/*
136 * The operations to get and set the status word just access
137 * the status field of the device descriptor. set_status will also
138 * make a hypercall to the host, to tell about status changes
139 */
140static u8 kvm_get_status(struct virtio_device *vdev)
141{
142 return to_kvmdev(vdev)->desc->status;
143}
144
145static void kvm_set_status(struct virtio_device *vdev, u8 status)
146{
147 BUG_ON(!status);
148 to_kvmdev(vdev)->desc->status = status;
149 kvm_hypercall1(KVM_S390_VIRTIO_SET_STATUS,
150 (unsigned long) to_kvmdev(vdev)->desc);
151}
152
153/*
154 * To reset the device, we use the KVM_VIRTIO_RESET hypercall, using the
155 * descriptor address. The Host will zero the status and all the
156 * features.
157 */
158static void kvm_reset(struct virtio_device *vdev)
159{
160 kvm_hypercall1(KVM_S390_VIRTIO_RESET,
161 (unsigned long) to_kvmdev(vdev)->desc);
162}
163
164/*
165 * When the virtio_ring code wants to notify the Host, it calls us here and we
166 * make a hypercall. We hand the address of the virtqueue so the Host
167 * knows which virtqueue we're talking about.
168 */
169static void kvm_notify(struct virtqueue *vq)
170{
171 struct kvm_vqconfig *config = vq->priv;
172
173 kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, config->address);
174}
175
176/*
177 * This routine finds the first virtqueue described in the configuration of
178 * this device and sets it up.
179 */
180static struct virtqueue *kvm_find_vq(struct virtio_device *vdev,
Rusty Russell9499f5e2009-06-12 22:16:35 -0600181 unsigned index,
182 void (*callback)(struct virtqueue *vq),
183 const char *name)
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100184{
185 struct kvm_device *kdev = to_kvmdev(vdev);
186 struct kvm_vqconfig *config;
187 struct virtqueue *vq;
188 int err;
189
190 if (index >= kdev->desc->num_vq)
191 return ERR_PTR(-ENOENT);
192
Michael S. Tsirkin6457f1262012-09-05 21:47:45 +0300193 if (!name)
194 return NULL;
195
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100196 config = kvm_vq_config(kdev->desc)+index;
197
Heiko Carstens17f34582008-04-30 13:38:47 +0200198 err = vmem_add_mapping(config->address,
Rusty Russelldb405982008-12-30 09:26:02 -0600199 vring_size(config->num,
200 KVM_S390_VIRTIO_RING_ALIGN));
Heiko Carstens17f34582008-04-30 13:38:47 +0200201 if (err)
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100202 goto out;
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100203
Jason Wang17bb6d42012-08-28 13:54:13 +0200204 vq = vring_new_virtqueue(index, config->num, KVM_S390_VIRTIO_RING_ALIGN,
Rusty Russell7b21e342012-01-12 15:44:42 +1030205 vdev, true, (void *) config->address,
Rusty Russell9499f5e2009-06-12 22:16:35 -0600206 kvm_notify, callback, name);
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100207 if (!vq) {
208 err = -ENOMEM;
209 goto unmap;
210 }
211
212 /*
213 * register a callback token
214 * The host will sent this via the external interrupt parameter
215 */
216 config->token = (u64) vq;
217
218 vq->priv = config;
219 return vq;
220unmap:
Heiko Carstens17f34582008-04-30 13:38:47 +0200221 vmem_remove_mapping(config->address,
Rusty Russelldb405982008-12-30 09:26:02 -0600222 vring_size(config->num,
223 KVM_S390_VIRTIO_RING_ALIGN));
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100224out:
225 return ERR_PTR(err);
226}
227
228static void kvm_del_vq(struct virtqueue *vq)
229{
230 struct kvm_vqconfig *config = vq->priv;
231
232 vring_del_virtqueue(vq);
Heiko Carstens17f34582008-04-30 13:38:47 +0200233 vmem_remove_mapping(config->address,
Rusty Russelldb405982008-12-30 09:26:02 -0600234 vring_size(config->num,
235 KVM_S390_VIRTIO_RING_ALIGN));
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100236}
237
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600238static void kvm_del_vqs(struct virtio_device *vdev)
239{
240 struct virtqueue *vq, *n;
241
242 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
243 kvm_del_vq(vq);
244}
245
246static int kvm_find_vqs(struct virtio_device *vdev, unsigned nvqs,
247 struct virtqueue *vqs[],
248 vq_callback_t *callbacks[],
249 const char *names[])
250{
251 struct kvm_device *kdev = to_kvmdev(vdev);
252 int i;
253
254 /* We must have this many virtqueues. */
255 if (nvqs > kdev->desc->num_vq)
256 return -ENOENT;
257
258 for (i = 0; i < nvqs; ++i) {
259 vqs[i] = kvm_find_vq(vdev, i, callbacks[i], names[i]);
260 if (IS_ERR(vqs[i]))
261 goto error;
262 }
263 return 0;
264
265error:
266 kvm_del_vqs(vdev);
267 return PTR_ERR(vqs[i]);
268}
269
Rick Jones66846042011-11-14 14:17:08 +0000270static const char *kvm_bus_name(struct virtio_device *vdev)
271{
272 return "";
273}
274
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100275/*
276 * The config ops structure as defined by virtio config
277 */
Stephen Hemminger93503932013-02-10 15:57:38 +1030278static const struct virtio_config_ops kvm_vq_configspace_ops = {
Heiko Carstens5ca9fd52008-05-06 17:38:30 +0300279 .get_features = kvm_get_features,
Rusty Russellc6248962008-07-25 12:06:07 -0500280 .finalize_features = kvm_finalize_features,
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100281 .get = kvm_get,
282 .set = kvm_set,
283 .get_status = kvm_get_status,
284 .set_status = kvm_set_status,
285 .reset = kvm_reset,
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600286 .find_vqs = kvm_find_vqs,
287 .del_vqs = kvm_del_vqs,
Rick Jones66846042011-11-14 14:17:08 +0000288 .bus_name = kvm_bus_name,
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100289};
290
291/*
292 * The root device for the kvm virtio devices.
293 * This makes them appear as /sys/devices/kvm_s390/0,1,2 not /sys/devices/0,1,2.
294 */
Cornelia Huck37f1c012008-10-10 21:33:12 +0200295static struct device *kvm_root;
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100296
297/*
298 * adds a new device and register it with virtio
299 * appropriate drivers are loaded by the device model
300 */
Rusty Russellb769f572008-05-30 15:09:42 -0500301static void add_kvm_device(struct kvm_device_desc *d, unsigned int offset)
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100302{
303 struct kvm_device *kdev;
304
305 kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
306 if (!kdev) {
Rusty Russellb769f572008-05-30 15:09:42 -0500307 printk(KERN_EMERG "Cannot allocate kvm dev %u type %u\n",
308 offset, d->type);
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100309 return;
310 }
311
Cornelia Huck37f1c012008-10-10 21:33:12 +0200312 kdev->vdev.dev.parent = kvm_root;
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100313 kdev->vdev.id.device = d->type;
314 kdev->vdev.config = &kvm_vq_configspace_ops;
315 kdev->desc = d;
316
317 if (register_virtio_device(&kdev->vdev) != 0) {
Rusty Russellb769f572008-05-30 15:09:42 -0500318 printk(KERN_ERR "Failed to register kvm device %u type %u\n",
319 offset, d->type);
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100320 kfree(kdev);
321 }
322}
323
324/*
325 * scan_devices() simply iterates through the device page.
326 * The type 0 is reserved to mean "end of devices".
327 */
328static void scan_devices(void)
329{
330 unsigned int i;
331 struct kvm_device_desc *d;
332
333 for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
334 d = kvm_devices + i;
335
336 if (d->type == 0)
337 break;
338
Rusty Russellb769f572008-05-30 15:09:42 -0500339 add_kvm_device(d, i);
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100340 }
341}
342
343/*
Alexander Grafcefa33e2010-08-24 15:48:51 +0200344 * match for a kvm device with a specific desc pointer
345 */
346static int match_desc(struct device *dev, void *data)
347{
Martin Schwidefsky7bf40742011-10-30 15:17:17 +0100348 struct virtio_device *vdev = dev_to_virtio(dev);
349 struct kvm_device *kdev = to_kvmdev(vdev);
Alexander Grafcefa33e2010-08-24 15:48:51 +0200350
Martin Schwidefsky7bf40742011-10-30 15:17:17 +0100351 return kdev->desc == data;
Alexander Grafcefa33e2010-08-24 15:48:51 +0200352}
353
354/*
355 * hotplug_device tries to find changes in the device page.
356 */
357static void hotplug_devices(struct work_struct *dummy)
358{
359 unsigned int i;
360 struct kvm_device_desc *d;
361 struct device *dev;
362
363 for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
364 d = kvm_devices + i;
365
366 /* end of list */
367 if (d->type == 0)
368 break;
369
370 /* device already exists */
371 dev = device_find_child(kvm_root, d, match_desc);
372 if (dev) {
373 /* XXX check for hotplug remove */
374 put_device(dev);
375 continue;
376 }
377
378 /* new device */
379 printk(KERN_INFO "Adding new virtio device %p\n", d);
380 add_kvm_device(d, i);
381 }
382}
383
384/*
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100385 * we emulate the request_irq behaviour on top of s390 extints
386 */
Heiko Carstensfde15c32012-03-11 11:59:31 -0400387static void kvm_extint_handler(struct ext_code ext_code,
Martin Schwidefskyf6649a72010-10-25 16:10:38 +0200388 unsigned int param32, unsigned long param64)
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100389{
Christian Borntraegerbe3c5832008-11-18 22:44:13 +0100390 struct virtqueue *vq;
Alexander Graffc678d62010-08-24 15:48:50 +0200391 u32 param;
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100392
Heiko Carstensfde15c32012-03-11 11:59:31 -0400393 if ((ext_code.subcode & 0xff00) != VIRTIO_SUBCODE_64)
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100394 return;
Heiko Carstens420f42e2013-01-02 15:18:18 +0100395 inc_irq_stat(IRQEXT_VRT);
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100396
Christian Borntraegerbe3c5832008-11-18 22:44:13 +0100397 /* The LSB might be overloaded, we have to mask it */
Martin Schwidefskyf6649a72010-10-25 16:10:38 +0200398 vq = (struct virtqueue *)(param64 & ~1UL);
Christian Borntraegerbe3c5832008-11-18 22:44:13 +0100399
Alexander Graffc678d62010-08-24 15:48:50 +0200400 /* We use ext_params to decide what this interrupt means */
Martin Schwidefskyf6649a72010-10-25 16:10:38 +0200401 param = param32 & VIRTIO_PARAM_MASK;
Christian Borntraegerbe3c5832008-11-18 22:44:13 +0100402
Alexander Graffc678d62010-08-24 15:48:50 +0200403 switch (param) {
404 case VIRTIO_PARAM_CONFIG_CHANGED:
405 {
Christian Borntraegerbe3c5832008-11-18 22:44:13 +0100406 struct virtio_driver *drv;
407 drv = container_of(vq->vdev->dev.driver,
408 struct virtio_driver, driver);
409 if (drv->config_changed)
410 drv->config_changed(vq->vdev);
Alexander Graffc678d62010-08-24 15:48:50 +0200411
412 break;
413 }
Alexander Grafcefa33e2010-08-24 15:48:51 +0200414 case VIRTIO_PARAM_DEV_ADD:
415 schedule_work(&hotplug_work);
416 break;
Alexander Graffc678d62010-08-24 15:48:50 +0200417 case VIRTIO_PARAM_VRING_INTERRUPT:
418 default:
Christian Borntraegerbe3c5832008-11-18 22:44:13 +0100419 vring_interrupt(0, vq);
Alexander Graffc678d62010-08-24 15:48:50 +0200420 break;
421 }
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100422}
423
424/*
Cornelia Huck55c171a2012-12-14 17:02:16 +0100425 * For s390-virtio, we expect a page above main storage containing
426 * the virtio configuration. Try to actually load from this area
427 * in order to figure out if the host provides this page.
428 */
429static int __init test_devices_support(unsigned long addr)
430{
431 int ret = -EIO;
432
433 asm volatile(
434 "0: lura 0,%1\n"
435 "1: xgr %0,%0\n"
436 "2:\n"
437 EX_TABLE(0b,2b)
438 EX_TABLE(1b,2b)
439 : "+d" (ret)
440 : "a" (addr)
441 : "0", "cc");
442 return ret;
443}
444/*
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100445 * Init function for virtio
446 * devices are in a single page above top of "normal" mem
447 */
448static int __init kvm_devices_init(void)
449{
450 int rc;
451
452 if (!MACHINE_IS_KVM)
453 return -ENODEV;
454
Cornelia Huck55c171a2012-12-14 17:02:16 +0100455 if (test_devices_support(real_memory_size) < 0)
456 return -ENODEV;
457
458 rc = vmem_add_mapping(real_memory_size, PAGE_SIZE);
459 if (rc)
460 return rc;
461
462 kvm_devices = (void *) real_memory_size;
463
Mark McLoughlin035da162008-12-15 12:58:29 +0000464 kvm_root = root_device_register("kvm_s390");
Cornelia Huck37f1c012008-10-10 21:33:12 +0200465 if (IS_ERR(kvm_root)) {
466 rc = PTR_ERR(kvm_root);
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100467 printk(KERN_ERR "Could not register kvm_s390 root device");
Cornelia Huck55c171a2012-12-14 17:02:16 +0100468 vmem_remove_mapping(real_memory_size, PAGE_SIZE);
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100469 return rc;
470 }
471
Alexander Grafcefa33e2010-08-24 15:48:51 +0200472 INIT_WORK(&hotplug_work, hotplug_devices);
473
Heiko Carstensdf7997a2011-05-26 09:48:23 +0200474 service_subclass_irq_register();
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100475 register_external_interrupt(0x2603, kvm_extint_handler);
476
477 scan_devices();
478 return 0;
479}
480
Christian Borntraegerfaeba8302008-06-20 15:24:18 +0200481/* code for early console output with virtio_console */
482static __init int early_put_chars(u32 vtermno, const char *buf, int count)
483{
484 char scratch[17];
485 unsigned int len = count;
486
487 if (len > sizeof(scratch) - 1)
488 len = sizeof(scratch) - 1;
489 scratch[len] = '\0';
490 memcpy(scratch, buf, len);
491 kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, __pa(scratch));
492 return len;
493}
494
Hendrik Bruecknerc4de0c12009-09-11 10:28:56 +0200495static int __init s390_virtio_console_init(void)
Christian Borntraegerfaeba8302008-06-20 15:24:18 +0200496{
Heinz Graalfscd183452012-06-11 16:06:59 +0200497 if (sclp_has_vt220() || sclp_has_linemode())
Hendrik Bruecknerc4de0c12009-09-11 10:28:56 +0200498 return -ENODEV;
499 return virtio_cons_early_init(early_put_chars);
Christian Borntraegerfaeba8302008-06-20 15:24:18 +0200500}
Hendrik Bruecknerc4de0c12009-09-11 10:28:56 +0200501console_initcall(s390_virtio_console_init);
502
Christian Borntraegerfaeba8302008-06-20 15:24:18 +0200503
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100504/*
505 * We do this after core stuff, but before the drivers.
506 */
507postcore_initcall(kvm_devices_init);