]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/s390/virtio/kvm_virtio.c
Merge branch 'WIP.x86/boot' into x86/boot, to pick up ready branch
[mirror_ubuntu-bionic-kernel.git] / drivers / s390 / virtio / kvm_virtio.c
CommitLineData
e976a2b9 1/*
a53c8fab 2 * virtio for kvm on s390
e976a2b9
CB
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
052ff461 13#include <linux/kernel_stat.h>
e976a2b9
CB
14#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>
5a0e3ad6 19#include <linux/slab.h>
faeba830 20#include <linux/virtio_console.h>
e976a2b9
CB
21#include <linux/interrupt.h>
22#include <linux/virtio_ring.h>
3a4c5d59 23#include <linux/export.h>
17f34580 24#include <linux/pfn.h>
e976a2b9
CB
25#include <asm/io.h>
26#include <asm/kvm_para.h>
27#include <asm/kvm_virtio.h>
cd183459 28#include <asm/sclp.h>
e976a2b9 29#include <asm/setup.h>
052ff461 30#include <asm/irq.h>
e976a2b9
CB
31
32#define VIRTIO_SUBCODE_64 0x0D00
33
34/*
35 * The pointer to our (page) of device descriptions.
36 */
37static void *kvm_devices;
c4736d96 38static struct work_struct hotplug_work;
e976a2b9 39
e976a2b9
CB
40struct 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
5ca9fd54 82/* This gets the device's feature bits. */
d0254773 83static u64 kvm_get_features(struct virtio_device *vdev)
e976a2b9 84{
5ca9fd54
HC
85 unsigned int i;
86 u32 features = 0;
e976a2b9 87 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
5ca9fd54 88 u8 *in_features = kvm_vq_features(desc);
e976a2b9 89
5ca9fd54
HC
90 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}
e976a2b9 95
5c609a5e 96static int kvm_finalize_features(struct virtio_device *vdev)
5ca9fd54 97{
c624896e 98 unsigned int i, bits;
5ca9fd54
HC
99 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;
e976a2b9 102
e34f8725
RR
103 /* Give virtio_ring a chance to accept features. */
104 vring_transport_features(vdev);
105
93d389f8
MT
106 /* Make sure we don't have any features > 32 bits! */
107 BUG_ON((u32)vdev->features != vdev->features);
108
5ca9fd54 109 memset(out_features, 0, desc->feature_len);
c624896e
RR
110 bits = min_t(unsigned, desc->feature_len, sizeof(vdev->features)) * 8;
111 for (i = 0; i < bits; i++) {
e16e12be 112 if (__virtio_test_bit(vdev, i))
5ca9fd54
HC
113 out_features[i / 8] |= (1 << (i % 8));
114 }
5c609a5e
MT
115
116 return 0;
e976a2b9
CB
117}
118
119/*
120 * Reading and writing elements in config space
121 */
122static void kvm_get(struct virtio_device *vdev, unsigned int offset,
123 void *buf, unsigned len)
124{
125 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
126
127 BUG_ON(offset + len > desc->config_len);
128 memcpy(buf, kvm_vq_configspace(desc) + offset, len);
129}
130
131static void kvm_set(struct virtio_device *vdev, unsigned int offset,
132 const void *buf, unsigned len)
133{
134 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
135
136 BUG_ON(offset + len > desc->config_len);
137 memcpy(kvm_vq_configspace(desc) + offset, buf, len);
138}
139
140/*
141 * The operations to get and set the status word just access
142 * the status field of the device descriptor. set_status will also
143 * make a hypercall to the host, to tell about status changes
144 */
145static u8 kvm_get_status(struct virtio_device *vdev)
146{
147 return to_kvmdev(vdev)->desc->status;
148}
149
150static void kvm_set_status(struct virtio_device *vdev, u8 status)
151{
152 BUG_ON(!status);
153 to_kvmdev(vdev)->desc->status = status;
154 kvm_hypercall1(KVM_S390_VIRTIO_SET_STATUS,
155 (unsigned long) to_kvmdev(vdev)->desc);
156}
157
158/*
159 * To reset the device, we use the KVM_VIRTIO_RESET hypercall, using the
160 * descriptor address. The Host will zero the status and all the
161 * features.
162 */
163static void kvm_reset(struct virtio_device *vdev)
164{
165 kvm_hypercall1(KVM_S390_VIRTIO_RESET,
166 (unsigned long) to_kvmdev(vdev)->desc);
167}
168
169/*
170 * When the virtio_ring code wants to notify the Host, it calls us here and we
171 * make a hypercall. We hand the address of the virtqueue so the Host
172 * knows which virtqueue we're talking about.
173 */
46f9c2b9 174static bool kvm_notify(struct virtqueue *vq)
e976a2b9 175{
46f9c2b9 176 long rc;
e976a2b9
CB
177 struct kvm_vqconfig *config = vq->priv;
178
46f9c2b9
HG
179 rc = kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, config->address);
180 if (rc < 0)
181 return false;
182 return true;
e976a2b9
CB
183}
184
185/*
186 * This routine finds the first virtqueue described in the configuration of
187 * this device and sets it up.
188 */
189static struct virtqueue *kvm_find_vq(struct virtio_device *vdev,
9499f5e7
RR
190 unsigned index,
191 void (*callback)(struct virtqueue *vq),
192 const char *name)
e976a2b9
CB
193{
194 struct kvm_device *kdev = to_kvmdev(vdev);
195 struct kvm_vqconfig *config;
196 struct virtqueue *vq;
197 int err;
198
199 if (index >= kdev->desc->num_vq)
200 return ERR_PTR(-ENOENT);
201
6457f126
MT
202 if (!name)
203 return NULL;
204
e976a2b9
CB
205 config = kvm_vq_config(kdev->desc)+index;
206
17f34580 207 err = vmem_add_mapping(config->address,
db405988
RR
208 vring_size(config->num,
209 KVM_S390_VIRTIO_RING_ALIGN));
17f34580 210 if (err)
e976a2b9 211 goto out;
e976a2b9 212
17bb6d40 213 vq = vring_new_virtqueue(index, config->num, KVM_S390_VIRTIO_RING_ALIGN,
7b21e34f 214 vdev, true, (void *) config->address,
9499f5e7 215 kvm_notify, callback, name);
e976a2b9
CB
216 if (!vq) {
217 err = -ENOMEM;
218 goto unmap;
219 }
220
221 /*
222 * register a callback token
223 * The host will sent this via the external interrupt parameter
224 */
225 config->token = (u64) vq;
226
227 vq->priv = config;
228 return vq;
229unmap:
17f34580 230 vmem_remove_mapping(config->address,
db405988
RR
231 vring_size(config->num,
232 KVM_S390_VIRTIO_RING_ALIGN));
e976a2b9
CB
233out:
234 return ERR_PTR(err);
235}
236
237static void kvm_del_vq(struct virtqueue *vq)
238{
239 struct kvm_vqconfig *config = vq->priv;
240
241 vring_del_virtqueue(vq);
17f34580 242 vmem_remove_mapping(config->address,
db405988
RR
243 vring_size(config->num,
244 KVM_S390_VIRTIO_RING_ALIGN));
e976a2b9
CB
245}
246
d2a7ddda
MT
247static void kvm_del_vqs(struct virtio_device *vdev)
248{
249 struct virtqueue *vq, *n;
250
251 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
252 kvm_del_vq(vq);
253}
254
255static int kvm_find_vqs(struct virtio_device *vdev, unsigned nvqs,
256 struct virtqueue *vqs[],
257 vq_callback_t *callbacks[],
fb5e31d9
CH
258 const char * const names[],
259 struct irq_affinity *desc)
d2a7ddda
MT
260{
261 struct kvm_device *kdev = to_kvmdev(vdev);
262 int i;
263
264 /* We must have this many virtqueues. */
265 if (nvqs > kdev->desc->num_vq)
266 return -ENOENT;
267
268 for (i = 0; i < nvqs; ++i) {
269 vqs[i] = kvm_find_vq(vdev, i, callbacks[i], names[i]);
270 if (IS_ERR(vqs[i]))
271 goto error;
272 }
273 return 0;
274
275error:
276 kvm_del_vqs(vdev);
277 return PTR_ERR(vqs[i]);
278}
279
66846048
RJ
280static const char *kvm_bus_name(struct virtio_device *vdev)
281{
282 return "";
283}
284
e976a2b9
CB
285/*
286 * The config ops structure as defined by virtio config
287 */
93503932 288static const struct virtio_config_ops kvm_vq_configspace_ops = {
5ca9fd54 289 .get_features = kvm_get_features,
c624896e 290 .finalize_features = kvm_finalize_features,
e976a2b9
CB
291 .get = kvm_get,
292 .set = kvm_set,
293 .get_status = kvm_get_status,
294 .set_status = kvm_set_status,
295 .reset = kvm_reset,
d2a7ddda
MT
296 .find_vqs = kvm_find_vqs,
297 .del_vqs = kvm_del_vqs,
66846048 298 .bus_name = kvm_bus_name,
e976a2b9
CB
299};
300
301/*
302 * The root device for the kvm virtio devices.
303 * This makes them appear as /sys/devices/kvm_s390/0,1,2 not /sys/devices/0,1,2.
304 */
37f1c012 305static struct device *kvm_root;
e976a2b9
CB
306
307/*
308 * adds a new device and register it with virtio
309 * appropriate drivers are loaded by the device model
310 */
b769f579 311static void add_kvm_device(struct kvm_device_desc *d, unsigned int offset)
e976a2b9
CB
312{
313 struct kvm_device *kdev;
314
315 kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
316 if (!kdev) {
b769f579
RR
317 printk(KERN_EMERG "Cannot allocate kvm dev %u type %u\n",
318 offset, d->type);
e976a2b9
CB
319 return;
320 }
321
37f1c012 322 kdev->vdev.dev.parent = kvm_root;
e976a2b9
CB
323 kdev->vdev.id.device = d->type;
324 kdev->vdev.config = &kvm_vq_configspace_ops;
325 kdev->desc = d;
326
327 if (register_virtio_device(&kdev->vdev) != 0) {
b769f579
RR
328 printk(KERN_ERR "Failed to register kvm device %u type %u\n",
329 offset, d->type);
e976a2b9
CB
330 kfree(kdev);
331 }
332}
333
334/*
335 * scan_devices() simply iterates through the device page.
336 * The type 0 is reserved to mean "end of devices".
337 */
338static void scan_devices(void)
339{
340 unsigned int i;
341 struct kvm_device_desc *d;
342
343 for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
344 d = kvm_devices + i;
345
346 if (d->type == 0)
347 break;
348
b769f579 349 add_kvm_device(d, i);
e976a2b9
CB
350 }
351}
352
cefa33e2
AG
353/*
354 * match for a kvm device with a specific desc pointer
355 */
356static int match_desc(struct device *dev, void *data)
357{
7bf4074d
MS
358 struct virtio_device *vdev = dev_to_virtio(dev);
359 struct kvm_device *kdev = to_kvmdev(vdev);
cefa33e2 360
7bf4074d 361 return kdev->desc == data;
cefa33e2
AG
362}
363
364/*
365 * hotplug_device tries to find changes in the device page.
366 */
367static void hotplug_devices(struct work_struct *dummy)
368{
369 unsigned int i;
370 struct kvm_device_desc *d;
371 struct device *dev;
372
373 for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
374 d = kvm_devices + i;
375
376 /* end of list */
377 if (d->type == 0)
378 break;
379
380 /* device already exists */
381 dev = device_find_child(kvm_root, d, match_desc);
382 if (dev) {
383 /* XXX check for hotplug remove */
384 put_device(dev);
385 continue;
386 }
387
388 /* new device */
389 printk(KERN_INFO "Adding new virtio device %p\n", d);
390 add_kvm_device(d, i);
391 }
392}
393
e976a2b9
CB
394/*
395 * we emulate the request_irq behaviour on top of s390 extints
396 */
fde15c3a 397static void kvm_extint_handler(struct ext_code ext_code,
f6649a7e 398 unsigned int param32, unsigned long param64)
e976a2b9 399{
be3c5832 400 struct virtqueue *vq;
fc678d67 401 u32 param;
e976a2b9 402
fde15c3a 403 if ((ext_code.subcode & 0xff00) != VIRTIO_SUBCODE_64)
e976a2b9 404 return;
420f42ec 405 inc_irq_stat(IRQEXT_VRT);
e976a2b9 406
be3c5832 407 /* The LSB might be overloaded, we have to mask it */
f6649a7e 408 vq = (struct virtqueue *)(param64 & ~1UL);
be3c5832 409
fc678d67 410 /* We use ext_params to decide what this interrupt means */
f6649a7e 411 param = param32 & VIRTIO_PARAM_MASK;
be3c5832 412
fc678d67
AG
413 switch (param) {
414 case VIRTIO_PARAM_CONFIG_CHANGED:
016c98c6 415 virtio_config_changed(vq->vdev);
fc678d67 416 break;
cefa33e2
AG
417 case VIRTIO_PARAM_DEV_ADD:
418 schedule_work(&hotplug_work);
419 break;
fc678d67
AG
420 case VIRTIO_PARAM_VRING_INTERRUPT:
421 default:
be3c5832 422 vring_interrupt(0, vq);
fc678d67
AG
423 break;
424 }
e976a2b9
CB
425}
426
55c171a6
CH
427/*
428 * For s390-virtio, we expect a page above main storage containing
429 * the virtio configuration. Try to actually load from this area
430 * in order to figure out if the host provides this page.
431 */
432static int __init test_devices_support(unsigned long addr)
433{
434 int ret = -EIO;
435
436 asm volatile(
437 "0: lura 0,%1\n"
438 "1: xgr %0,%0\n"
439 "2:\n"
440 EX_TABLE(0b,2b)
441 EX_TABLE(1b,2b)
442 : "+d" (ret)
443 : "a" (addr)
444 : "0", "cc");
445 return ret;
446}
e976a2b9
CB
447/*
448 * Init function for virtio
3188bf6b 449 * devices are in a single page above top of "normal" + standby mem
e976a2b9
CB
450 */
451static int __init kvm_devices_init(void)
452{
453 int rc;
37c5f6c8 454 unsigned long total_memory_size = sclp.rzm * sclp.rnmax;
e976a2b9
CB
455
456 if (!MACHINE_IS_KVM)
457 return -ENODEV;
458
3188bf6b 459 if (test_devices_support(total_memory_size) < 0)
55c171a6
CH
460 return -ENODEV;
461
3b2fbb3f
CH
462 pr_warn("The s390-virtio transport is deprecated. Please switch to a modern host providing virtio-ccw.\n");
463
3188bf6b 464 rc = vmem_add_mapping(total_memory_size, PAGE_SIZE);
55c171a6
CH
465 if (rc)
466 return rc;
467
3188bf6b 468 kvm_devices = (void *) total_memory_size;
55c171a6 469
035da16f 470 kvm_root = root_device_register("kvm_s390");
37f1c012
CH
471 if (IS_ERR(kvm_root)) {
472 rc = PTR_ERR(kvm_root);
e976a2b9 473 printk(KERN_ERR "Could not register kvm_s390 root device");
3188bf6b 474 vmem_remove_mapping(total_memory_size, PAGE_SIZE);
e976a2b9
CB
475 return rc;
476 }
477
cefa33e2
AG
478 INIT_WORK(&hotplug_work, hotplug_devices);
479
82003c3e 480 irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL);
1dad093b 481 register_external_irq(EXT_IRQ_CP_SERVICE, kvm_extint_handler);
e976a2b9
CB
482
483 scan_devices();
484 return 0;
485}
486
faeba830 487/* code for early console output with virtio_console */
2ab0d56a 488static int early_put_chars(u32 vtermno, const char *buf, int count)
faeba830
CB
489{
490 char scratch[17];
491 unsigned int len = count;
492
493 if (len > sizeof(scratch) - 1)
494 len = sizeof(scratch) - 1;
495 scratch[len] = '\0';
496 memcpy(scratch, buf, len);
497 kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, __pa(scratch));
498 return len;
499}
500
c4de0c1a 501static int __init s390_virtio_console_init(void)
faeba830 502{
37c5f6c8 503 if (sclp.has_vt220 || sclp.has_linemode)
c4de0c1a
HB
504 return -ENODEV;
505 return virtio_cons_early_init(early_put_chars);
faeba830 506}
c4de0c1a
HB
507console_initcall(s390_virtio_console_init);
508
faeba830 509
e976a2b9
CB
510/*
511 * We do this after core stuff, but before the drivers.
512 */
513postcore_initcall(kvm_devices_init);