]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/s390/kvm/kvm_virtio.c
[S390] cleanup lowcore access from external interrupts
[net-next-2.6.git] / drivers / s390 / kvm / kvm_virtio.c
CommitLineData
e976a2b9
CB
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>
5a0e3ad6 18#include <linux/slab.h>
faeba830 19#include <linux/virtio_console.h>
e976a2b9
CB
20#include <linux/interrupt.h>
21#include <linux/virtio_ring.h>
17f34580 22#include <linux/pfn.h>
e976a2b9
CB
23#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;
cefa33e2 35struct work_struct hotplug_work;
e976a2b9 36
e976a2b9
CB
37struct 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
5ca9fd54
HC
79/* This gets the device's feature bits. */
80static u32 kvm_get_features(struct virtio_device *vdev)
e976a2b9 81{
5ca9fd54
HC
82 unsigned int i;
83 u32 features = 0;
e976a2b9 84 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
5ca9fd54 85 u8 *in_features = kvm_vq_features(desc);
e976a2b9 86
5ca9fd54
HC
87 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}
e976a2b9 92
c624896e 93static void kvm_finalize_features(struct virtio_device *vdev)
5ca9fd54 94{
c624896e 95 unsigned int i, bits;
5ca9fd54
HC
96 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;
e976a2b9 99
e34f8725
RR
100 /* Give virtio_ring a chance to accept features. */
101 vring_transport_features(vdev);
102
5ca9fd54 103 memset(out_features, 0, desc->feature_len);
c624896e
RR
104 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))
5ca9fd54
HC
107 out_features[i / 8] |= (1 << (i % 8));
108 }
e976a2b9
CB
109}
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,
9499f5e7
RR
178 unsigned index,
179 void (*callback)(struct virtqueue *vq),
180 const char *name)
e976a2b9
CB
181{
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
17f34580 192 err = vmem_add_mapping(config->address,
db405988
RR
193 vring_size(config->num,
194 KVM_S390_VIRTIO_RING_ALIGN));
17f34580 195 if (err)
e976a2b9 196 goto out;
e976a2b9 197
87c7d57c
RR
198 vq = vring_new_virtqueue(config->num, KVM_S390_VIRTIO_RING_ALIGN,
199 vdev, (void *) config->address,
9499f5e7 200 kvm_notify, callback, name);
e976a2b9
CB
201 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:
17f34580 215 vmem_remove_mapping(config->address,
db405988
RR
216 vring_size(config->num,
217 KVM_S390_VIRTIO_RING_ALIGN));
e976a2b9
CB
218out:
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);
17f34580 227 vmem_remove_mapping(config->address,
db405988
RR
228 vring_size(config->num,
229 KVM_S390_VIRTIO_RING_ALIGN));
e976a2b9
CB
230}
231
d2a7ddda
MT
232static 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
e976a2b9
CB
264/*
265 * The config ops structure as defined by virtio config
266 */
267static struct virtio_config_ops kvm_vq_configspace_ops = {
5ca9fd54 268 .get_features = kvm_get_features,
c624896e 269 .finalize_features = kvm_finalize_features,
e976a2b9
CB
270 .get = kvm_get,
271 .set = kvm_set,
272 .get_status = kvm_get_status,
273 .set_status = kvm_set_status,
274 .reset = kvm_reset,
d2a7ddda
MT
275 .find_vqs = kvm_find_vqs,
276 .del_vqs = kvm_del_vqs,
e976a2b9
CB
277};
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 */
37f1c012 283static struct device *kvm_root;
e976a2b9
CB
284
285/*
286 * adds a new device and register it with virtio
287 * appropriate drivers are loaded by the device model
288 */
b769f579 289static void add_kvm_device(struct kvm_device_desc *d, unsigned int offset)
e976a2b9
CB
290{
291 struct kvm_device *kdev;
292
293 kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
294 if (!kdev) {
b769f579
RR
295 printk(KERN_EMERG "Cannot allocate kvm dev %u type %u\n",
296 offset, d->type);
e976a2b9
CB
297 return;
298 }
299
37f1c012 300 kdev->vdev.dev.parent = kvm_root;
e976a2b9
CB
301 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) {
b769f579
RR
306 printk(KERN_ERR "Failed to register kvm device %u type %u\n",
307 offset, d->type);
e976a2b9
CB
308 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
b769f579 327 add_kvm_device(d, i);
e976a2b9
CB
328 }
329}
330
cefa33e2
AG
331/*
332 * 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
e976a2b9
CB
372/*
373 * we emulate the request_irq behaviour on top of s390 extints
374 */
f6649a7e
MS
375static void kvm_extint_handler(unsigned int ext_int_code,
376 unsigned int param32, unsigned long param64)
e976a2b9 377{
be3c5832
CB
378 struct virtqueue *vq;
379 u16 subcode;
fc678d67 380 u32 param;
e976a2b9 381
f6649a7e 382 subcode = ext_int_code >> 16;
e976a2b9
CB
383 if ((subcode & 0xff00) != VIRTIO_SUBCODE_64)
384 return;
385
be3c5832 386 /* The LSB might be overloaded, we have to mask it */
f6649a7e 387 vq = (struct virtqueue *)(param64 & ~1UL);
be3c5832 388
fc678d67 389 /* We use ext_params to decide what this interrupt means */
f6649a7e 390 param = param32 & VIRTIO_PARAM_MASK;
be3c5832 391
fc678d67
AG
392 switch (param) {
393 case VIRTIO_PARAM_CONFIG_CHANGED:
394 {
be3c5832
CB
395 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);
fc678d67
AG
400
401 break;
402 }
cefa33e2
AG
403 case VIRTIO_PARAM_DEV_ADD:
404 schedule_work(&hotplug_work);
405 break;
fc678d67
AG
406 case VIRTIO_PARAM_VRING_INTERRUPT:
407 default:
be3c5832 408 vring_interrupt(0, vq);
fc678d67
AG
409 break;
410 }
e976a2b9
CB
411}
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
035da16f 424 kvm_root = root_device_register("kvm_s390");
37f1c012
CH
425 if (IS_ERR(kvm_root)) {
426 rc = PTR_ERR(kvm_root);
e976a2b9
CB
427 printk(KERN_ERR "Could not register kvm_s390 root device");
428 return rc;
429 }
430
cc835f78 431 rc = vmem_add_mapping(real_memory_size, PAGE_SIZE);
17f34580 432 if (rc) {
035da16f 433 root_device_unregister(kvm_root);
17f34580 434 return rc;
e976a2b9
CB
435 }
436
cc835f78 437 kvm_devices = (void *) real_memory_size;
e976a2b9 438
cefa33e2
AG
439 INIT_WORK(&hotplug_work, hotplug_devices);
440
e976a2b9
CB
441 ctl_set_bit(0, 9);
442 register_external_interrupt(0x2603, kvm_extint_handler);
443
444 scan_devices();
445 return 0;
446}
447
faeba830
CB
448/* 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
c4de0c1a 462static int __init s390_virtio_console_init(void)
faeba830 463{
c4de0c1a
HB
464 if (!MACHINE_IS_KVM)
465 return -ENODEV;
466 return virtio_cons_early_init(early_put_chars);
faeba830 467}
c4de0c1a
HB
468console_initcall(s390_virtio_console_init);
469
faeba830 470
e976a2b9
CB
471/*
472 * We do this after core stuff, but before the drivers.
473 */
474postcore_initcall(kvm_devices_init);