blob: e38e5d306faf577f9ccc9bfb25a75a1f29c4785e [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>
Christian Borntraegerfaeba8302008-06-20 15:24:18 +020018#include <linux/virtio_console.h>
Christian Borntraegere976a2b2008-03-25 18:47:46 +010019#include <linux/interrupt.h>
20#include <linux/virtio_ring.h>
Heiko Carstens17f34582008-04-30 13:38:47 +020021#include <linux/pfn.h>
Christian Borntraegere976a2b2008-03-25 18:47:46 +010022#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 */
33static void *kvm_devices;
34
Christian Borntraegere976a2b2008-03-25 18:47:46 +010035struct 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 */
51static struct kvm_vqconfig *kvm_vq_config(const struct kvm_device_desc *desc)
52{
53 return (struct kvm_vqconfig *)(desc + 1);
54}
55
56static u8 *kvm_vq_features(const struct kvm_device_desc *desc)
57{
58 return (u8 *)(kvm_vq_config(desc) + desc->num_vq);
59}
60
61static 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 */
69static 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
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030077/* This gets the device's feature bits. */
78static u32 kvm_get_features(struct virtio_device *vdev)
Christian Borntraegere976a2b2008-03-25 18:47:46 +010079{
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030080 unsigned int i;
81 u32 features = 0;
Christian Borntraegere976a2b2008-03-25 18:47:46 +010082 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030083 u8 *in_features = kvm_vq_features(desc);
Christian Borntraegere976a2b2008-03-25 18:47:46 +010084
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030085 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}
Christian Borntraegere976a2b2008-03-25 18:47:46 +010090
Rusty Russellc6248962008-07-25 12:06:07 -050091static void kvm_finalize_features(struct virtio_device *vdev)
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030092{
Rusty Russellc6248962008-07-25 12:06:07 -050093 unsigned int i, bits;
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030094 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;
Christian Borntraegere976a2b2008-03-25 18:47:46 +010097
Rusty Russelle34f8722008-07-25 12:06:13 -050098 /* Give virtio_ring a chance to accept features. */
99 vring_transport_features(vdev);
100
Heiko Carstens5ca9fd52008-05-06 17:38:30 +0300101 memset(out_features, 0, desc->feature_len);
Rusty Russellc6248962008-07-25 12:06:07 -0500102 bits = min_t(unsigned, desc->feature_len, sizeof(vdev->features)) * 8;
103 for (i = 0; i < bits; i++) {
104 if (test_bit(i, vdev->features))
Heiko Carstens5ca9fd52008-05-06 17:38:30 +0300105 out_features[i / 8] |= (1 << (i % 8));
106 }
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100107}
108
109/*
110 * Reading and writing elements in config space
111 */
112static void kvm_get(struct virtio_device *vdev, unsigned int offset,
113 void *buf, unsigned len)
114{
115 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
116
117 BUG_ON(offset + len > desc->config_len);
118 memcpy(buf, kvm_vq_configspace(desc) + offset, len);
119}
120
121static void kvm_set(struct virtio_device *vdev, unsigned int offset,
122 const void *buf, unsigned len)
123{
124 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
125
126 BUG_ON(offset + len > desc->config_len);
127 memcpy(kvm_vq_configspace(desc) + offset, buf, len);
128}
129
130/*
131 * The operations to get and set the status word just access
132 * the status field of the device descriptor. set_status will also
133 * make a hypercall to the host, to tell about status changes
134 */
135static u8 kvm_get_status(struct virtio_device *vdev)
136{
137 return to_kvmdev(vdev)->desc->status;
138}
139
140static void kvm_set_status(struct virtio_device *vdev, u8 status)
141{
142 BUG_ON(!status);
143 to_kvmdev(vdev)->desc->status = status;
144 kvm_hypercall1(KVM_S390_VIRTIO_SET_STATUS,
145 (unsigned long) to_kvmdev(vdev)->desc);
146}
147
148/*
149 * To reset the device, we use the KVM_VIRTIO_RESET hypercall, using the
150 * descriptor address. The Host will zero the status and all the
151 * features.
152 */
153static void kvm_reset(struct virtio_device *vdev)
154{
155 kvm_hypercall1(KVM_S390_VIRTIO_RESET,
156 (unsigned long) to_kvmdev(vdev)->desc);
157}
158
159/*
160 * When the virtio_ring code wants to notify the Host, it calls us here and we
161 * make a hypercall. We hand the address of the virtqueue so the Host
162 * knows which virtqueue we're talking about.
163 */
164static void kvm_notify(struct virtqueue *vq)
165{
166 struct kvm_vqconfig *config = vq->priv;
167
168 kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, config->address);
169}
170
171/*
172 * This routine finds the first virtqueue described in the configuration of
173 * this device and sets it up.
174 */
175static struct virtqueue *kvm_find_vq(struct virtio_device *vdev,
Rusty Russell9499f5e2009-06-12 22:16:35 -0600176 unsigned index,
177 void (*callback)(struct virtqueue *vq),
178 const char *name)
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100179{
180 struct kvm_device *kdev = to_kvmdev(vdev);
181 struct kvm_vqconfig *config;
182 struct virtqueue *vq;
183 int err;
184
185 if (index >= kdev->desc->num_vq)
186 return ERR_PTR(-ENOENT);
187
188 config = kvm_vq_config(kdev->desc)+index;
189
Heiko Carstens17f34582008-04-30 13:38:47 +0200190 err = vmem_add_mapping(config->address,
Rusty Russelldb405982008-12-30 09:26:02 -0600191 vring_size(config->num,
192 KVM_S390_VIRTIO_RING_ALIGN));
Heiko Carstens17f34582008-04-30 13:38:47 +0200193 if (err)
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100194 goto out;
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100195
Rusty Russell87c7d572008-12-30 09:26:03 -0600196 vq = vring_new_virtqueue(config->num, KVM_S390_VIRTIO_RING_ALIGN,
197 vdev, (void *) config->address,
Rusty Russell9499f5e2009-06-12 22:16:35 -0600198 kvm_notify, callback, name);
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100199 if (!vq) {
200 err = -ENOMEM;
201 goto unmap;
202 }
203
204 /*
205 * register a callback token
206 * The host will sent this via the external interrupt parameter
207 */
208 config->token = (u64) vq;
209
210 vq->priv = config;
211 return vq;
212unmap:
Heiko Carstens17f34582008-04-30 13:38:47 +0200213 vmem_remove_mapping(config->address,
Rusty Russelldb405982008-12-30 09:26:02 -0600214 vring_size(config->num,
215 KVM_S390_VIRTIO_RING_ALIGN));
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100216out:
217 return ERR_PTR(err);
218}
219
220static void kvm_del_vq(struct virtqueue *vq)
221{
222 struct kvm_vqconfig *config = vq->priv;
223
224 vring_del_virtqueue(vq);
Heiko Carstens17f34582008-04-30 13:38:47 +0200225 vmem_remove_mapping(config->address,
Rusty Russelldb405982008-12-30 09:26:02 -0600226 vring_size(config->num,
227 KVM_S390_VIRTIO_RING_ALIGN));
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100228}
229
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600230static void kvm_del_vqs(struct virtio_device *vdev)
231{
232 struct virtqueue *vq, *n;
233
234 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
235 kvm_del_vq(vq);
236}
237
238static int kvm_find_vqs(struct virtio_device *vdev, unsigned nvqs,
239 struct virtqueue *vqs[],
240 vq_callback_t *callbacks[],
241 const char *names[])
242{
243 struct kvm_device *kdev = to_kvmdev(vdev);
244 int i;
245
246 /* We must have this many virtqueues. */
247 if (nvqs > kdev->desc->num_vq)
248 return -ENOENT;
249
250 for (i = 0; i < nvqs; ++i) {
251 vqs[i] = kvm_find_vq(vdev, i, callbacks[i], names[i]);
252 if (IS_ERR(vqs[i]))
253 goto error;
254 }
255 return 0;
256
257error:
258 kvm_del_vqs(vdev);
259 return PTR_ERR(vqs[i]);
260}
261
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100262/*
263 * The config ops structure as defined by virtio config
264 */
265static struct virtio_config_ops kvm_vq_configspace_ops = {
Heiko Carstens5ca9fd52008-05-06 17:38:30 +0300266 .get_features = kvm_get_features,
Rusty Russellc6248962008-07-25 12:06:07 -0500267 .finalize_features = kvm_finalize_features,
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100268 .get = kvm_get,
269 .set = kvm_set,
270 .get_status = kvm_get_status,
271 .set_status = kvm_set_status,
272 .reset = kvm_reset,
Michael S. Tsirkind2a7ddd2009-06-12 22:16:36 -0600273 .find_vqs = kvm_find_vqs,
274 .del_vqs = kvm_del_vqs,
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100275};
276
277/*
278 * The root device for the kvm virtio devices.
279 * This makes them appear as /sys/devices/kvm_s390/0,1,2 not /sys/devices/0,1,2.
280 */
Cornelia Huck37f1c012008-10-10 21:33:12 +0200281static struct device *kvm_root;
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100282
283/*
284 * adds a new device and register it with virtio
285 * appropriate drivers are loaded by the device model
286 */
Rusty Russellb769f572008-05-30 15:09:42 -0500287static void add_kvm_device(struct kvm_device_desc *d, unsigned int offset)
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100288{
289 struct kvm_device *kdev;
290
291 kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
292 if (!kdev) {
Rusty Russellb769f572008-05-30 15:09:42 -0500293 printk(KERN_EMERG "Cannot allocate kvm dev %u type %u\n",
294 offset, d->type);
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100295 return;
296 }
297
Cornelia Huck37f1c012008-10-10 21:33:12 +0200298 kdev->vdev.dev.parent = kvm_root;
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100299 kdev->vdev.id.device = d->type;
300 kdev->vdev.config = &kvm_vq_configspace_ops;
301 kdev->desc = d;
302
303 if (register_virtio_device(&kdev->vdev) != 0) {
Rusty Russellb769f572008-05-30 15:09:42 -0500304 printk(KERN_ERR "Failed to register kvm device %u type %u\n",
305 offset, d->type);
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100306 kfree(kdev);
307 }
308}
309
310/*
311 * scan_devices() simply iterates through the device page.
312 * The type 0 is reserved to mean "end of devices".
313 */
314static void scan_devices(void)
315{
316 unsigned int i;
317 struct kvm_device_desc *d;
318
319 for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
320 d = kvm_devices + i;
321
322 if (d->type == 0)
323 break;
324
Rusty Russellb769f572008-05-30 15:09:42 -0500325 add_kvm_device(d, i);
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100326 }
327}
328
329/*
330 * we emulate the request_irq behaviour on top of s390 extints
331 */
332static void kvm_extint_handler(u16 code)
333{
Christian Borntraegerbe3c5832008-11-18 22:44:13 +0100334 struct virtqueue *vq;
335 u16 subcode;
336 int config_changed;
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100337
Christian Borntraegerbe3c5832008-11-18 22:44:13 +0100338 subcode = S390_lowcore.cpu_addr;
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100339 if ((subcode & 0xff00) != VIRTIO_SUBCODE_64)
340 return;
341
Christian Borntraegerbe3c5832008-11-18 22:44:13 +0100342 /* The LSB might be overloaded, we have to mask it */
343 vq = (struct virtqueue *) ((*(long *) __LC_PFAULT_INTPARM) & ~1UL);
344
345 /* We use the LSB of extparam, to decide, if this interrupt is a config
346 * change or a "standard" interrupt */
347 config_changed = (*(int *) __LC_EXT_PARAMS & 1);
348
349 if (config_changed) {
350 struct virtio_driver *drv;
351 drv = container_of(vq->vdev->dev.driver,
352 struct virtio_driver, driver);
353 if (drv->config_changed)
354 drv->config_changed(vq->vdev);
355 } else
356 vring_interrupt(0, vq);
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100357}
358
359/*
360 * Init function for virtio
361 * devices are in a single page above top of "normal" mem
362 */
363static int __init kvm_devices_init(void)
364{
365 int rc;
366
367 if (!MACHINE_IS_KVM)
368 return -ENODEV;
369
Mark McLoughlin035da162008-12-15 12:58:29 +0000370 kvm_root = root_device_register("kvm_s390");
Cornelia Huck37f1c012008-10-10 21:33:12 +0200371 if (IS_ERR(kvm_root)) {
372 rc = PTR_ERR(kvm_root);
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100373 printk(KERN_ERR "Could not register kvm_s390 root device");
374 return rc;
375 }
376
Christian Borntraegercc835f782008-11-14 18:18:02 +0100377 rc = vmem_add_mapping(real_memory_size, PAGE_SIZE);
Heiko Carstens17f34582008-04-30 13:38:47 +0200378 if (rc) {
Mark McLoughlin035da162008-12-15 12:58:29 +0000379 root_device_unregister(kvm_root);
Heiko Carstens17f34582008-04-30 13:38:47 +0200380 return rc;
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100381 }
382
Christian Borntraegercc835f782008-11-14 18:18:02 +0100383 kvm_devices = (void *) real_memory_size;
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100384
385 ctl_set_bit(0, 9);
386 register_external_interrupt(0x2603, kvm_extint_handler);
387
388 scan_devices();
389 return 0;
390}
391
Christian Borntraegerfaeba8302008-06-20 15:24:18 +0200392/* code for early console output with virtio_console */
393static __init int early_put_chars(u32 vtermno, const char *buf, int count)
394{
395 char scratch[17];
396 unsigned int len = count;
397
398 if (len > sizeof(scratch) - 1)
399 len = sizeof(scratch) - 1;
400 scratch[len] = '\0';
401 memcpy(scratch, buf, len);
402 kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, __pa(scratch));
403 return len;
404}
405
Heiko Carstens26f746f2008-08-01 16:39:22 +0200406void __init s390_virtio_console_init(void)
Christian Borntraegerfaeba8302008-06-20 15:24:18 +0200407{
408 virtio_cons_early_init(early_put_chars);
409}
410
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100411/*
412 * We do this after core stuff, but before the drivers.
413 */
414postcore_initcall(kvm_devices_init);