PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / s390 / kvm / kvm_virtio.c
blob1abd0db29915bf69b8618b7c267dad552573a74d
1 /*
2 * virtio for kvm on s390
4 * Copyright IBM Corp. 2008
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.
10 * Author(s): Christian Borntraeger <borntraeger@de.ibm.com>
13 #include <linux/kernel_stat.h>
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>
19 #include <linux/slab.h>
20 #include <linux/virtio_console.h>
21 #include <linux/interrupt.h>
22 #include <linux/virtio_ring.h>
23 #include <linux/export.h>
24 #include <linux/pfn.h>
25 #include <asm/io.h>
26 #include <asm/kvm_para.h>
27 #include <asm/kvm_virtio.h>
28 #include <asm/sclp.h>
29 #include <asm/setup.h>
30 #include <asm/irq.h>
32 #define VIRTIO_SUBCODE_64 0x0D00
35 * The pointer to our (page) of device descriptions.
37 static void *kvm_devices;
38 static struct work_struct hotplug_work;
40 struct kvm_device {
41 struct virtio_device vdev;
42 struct kvm_device_desc *desc;
45 #define to_kvmdev(vd) container_of(vd, struct kvm_device, vdev)
48 * memory layout:
49 * - kvm_device_descriptor
50 * struct kvm_device_desc
51 * - configuration
52 * struct kvm_vqconfig
53 * - feature bits
54 * - config space
56 static struct kvm_vqconfig *kvm_vq_config(const struct kvm_device_desc *desc)
58 return (struct kvm_vqconfig *)(desc + 1);
61 static u8 *kvm_vq_features(const struct kvm_device_desc *desc)
63 return (u8 *)(kvm_vq_config(desc) + desc->num_vq);
66 static u8 *kvm_vq_configspace(const struct kvm_device_desc *desc)
68 return kvm_vq_features(desc) + desc->feature_len * 2;
72 * The total size of the config page used by this device (incl. desc)
74 static unsigned desc_size(const struct kvm_device_desc *desc)
76 return sizeof(*desc)
77 + desc->num_vq * sizeof(struct kvm_vqconfig)
78 + desc->feature_len * 2
79 + desc->config_len;
82 /* This gets the device's feature bits. */
83 static u32 kvm_get_features(struct virtio_device *vdev)
85 unsigned int i;
86 u32 features = 0;
87 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
88 u8 *in_features = kvm_vq_features(desc);
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;
96 static void kvm_finalize_features(struct virtio_device *vdev)
98 unsigned int i, bits;
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;
103 /* Give virtio_ring a chance to accept features. */
104 vring_transport_features(vdev);
106 memset(out_features, 0, desc->feature_len);
107 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))
110 out_features[i / 8] |= (1 << (i % 8));
115 * Reading and writing elements in config space
117 static void kvm_get(struct virtio_device *vdev, unsigned int offset,
118 void *buf, unsigned len)
120 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
122 BUG_ON(offset + len > desc->config_len);
123 memcpy(buf, kvm_vq_configspace(desc) + offset, len);
126 static void kvm_set(struct virtio_device *vdev, unsigned int offset,
127 const void *buf, unsigned len)
129 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
131 BUG_ON(offset + len > desc->config_len);
132 memcpy(kvm_vq_configspace(desc) + offset, buf, len);
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
140 static u8 kvm_get_status(struct virtio_device *vdev)
142 return to_kvmdev(vdev)->desc->status;
145 static void kvm_set_status(struct virtio_device *vdev, u8 status)
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);
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.
158 static void kvm_reset(struct virtio_device *vdev)
160 kvm_hypercall1(KVM_S390_VIRTIO_RESET,
161 (unsigned long) to_kvmdev(vdev)->desc);
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.
169 static bool kvm_notify(struct virtqueue *vq)
171 long rc;
172 struct kvm_vqconfig *config = vq->priv;
174 rc = kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, config->address);
175 if (rc < 0)
176 return false;
177 return true;
181 * This routine finds the first virtqueue described in the configuration of
182 * this device and sets it up.
184 static struct virtqueue *kvm_find_vq(struct virtio_device *vdev,
185 unsigned index,
186 void (*callback)(struct virtqueue *vq),
187 const char *name)
189 struct kvm_device *kdev = to_kvmdev(vdev);
190 struct kvm_vqconfig *config;
191 struct virtqueue *vq;
192 int err;
194 if (index >= kdev->desc->num_vq)
195 return ERR_PTR(-ENOENT);
197 if (!name)
198 return NULL;
200 config = kvm_vq_config(kdev->desc)+index;
202 err = vmem_add_mapping(config->address,
203 vring_size(config->num,
204 KVM_S390_VIRTIO_RING_ALIGN));
205 if (err)
206 goto out;
208 vq = vring_new_virtqueue(index, config->num, KVM_S390_VIRTIO_RING_ALIGN,
209 vdev, true, (void *) config->address,
210 kvm_notify, callback, name);
211 if (!vq) {
212 err = -ENOMEM;
213 goto unmap;
217 * register a callback token
218 * The host will sent this via the external interrupt parameter
220 config->token = (u64) vq;
222 vq->priv = config;
223 return vq;
224 unmap:
225 vmem_remove_mapping(config->address,
226 vring_size(config->num,
227 KVM_S390_VIRTIO_RING_ALIGN));
228 out:
229 return ERR_PTR(err);
232 static void kvm_del_vq(struct virtqueue *vq)
234 struct kvm_vqconfig *config = vq->priv;
236 vring_del_virtqueue(vq);
237 vmem_remove_mapping(config->address,
238 vring_size(config->num,
239 KVM_S390_VIRTIO_RING_ALIGN));
242 static void kvm_del_vqs(struct virtio_device *vdev)
244 struct virtqueue *vq, *n;
246 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
247 kvm_del_vq(vq);
250 static int kvm_find_vqs(struct virtio_device *vdev, unsigned nvqs,
251 struct virtqueue *vqs[],
252 vq_callback_t *callbacks[],
253 const char *names[])
255 struct kvm_device *kdev = to_kvmdev(vdev);
256 int i;
258 /* We must have this many virtqueues. */
259 if (nvqs > kdev->desc->num_vq)
260 return -ENOENT;
262 for (i = 0; i < nvqs; ++i) {
263 vqs[i] = kvm_find_vq(vdev, i, callbacks[i], names[i]);
264 if (IS_ERR(vqs[i]))
265 goto error;
267 return 0;
269 error:
270 kvm_del_vqs(vdev);
271 return PTR_ERR(vqs[i]);
274 static const char *kvm_bus_name(struct virtio_device *vdev)
276 return "";
280 * The config ops structure as defined by virtio config
282 static const struct virtio_config_ops kvm_vq_configspace_ops = {
283 .get_features = kvm_get_features,
284 .finalize_features = kvm_finalize_features,
285 .get = kvm_get,
286 .set = kvm_set,
287 .get_status = kvm_get_status,
288 .set_status = kvm_set_status,
289 .reset = kvm_reset,
290 .find_vqs = kvm_find_vqs,
291 .del_vqs = kvm_del_vqs,
292 .bus_name = kvm_bus_name,
296 * The root device for the kvm virtio devices.
297 * This makes them appear as /sys/devices/kvm_s390/0,1,2 not /sys/devices/0,1,2.
299 static struct device *kvm_root;
302 * adds a new device and register it with virtio
303 * appropriate drivers are loaded by the device model
305 static void add_kvm_device(struct kvm_device_desc *d, unsigned int offset)
307 struct kvm_device *kdev;
309 kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
310 if (!kdev) {
311 printk(KERN_EMERG "Cannot allocate kvm dev %u type %u\n",
312 offset, d->type);
313 return;
316 kdev->vdev.dev.parent = kvm_root;
317 kdev->vdev.id.device = d->type;
318 kdev->vdev.config = &kvm_vq_configspace_ops;
319 kdev->desc = d;
321 if (register_virtio_device(&kdev->vdev) != 0) {
322 printk(KERN_ERR "Failed to register kvm device %u type %u\n",
323 offset, d->type);
324 kfree(kdev);
329 * scan_devices() simply iterates through the device page.
330 * The type 0 is reserved to mean "end of devices".
332 static void scan_devices(void)
334 unsigned int i;
335 struct kvm_device_desc *d;
337 for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
338 d = kvm_devices + i;
340 if (d->type == 0)
341 break;
343 add_kvm_device(d, i);
348 * match for a kvm device with a specific desc pointer
350 static int match_desc(struct device *dev, void *data)
352 struct virtio_device *vdev = dev_to_virtio(dev);
353 struct kvm_device *kdev = to_kvmdev(vdev);
355 return kdev->desc == data;
359 * hotplug_device tries to find changes in the device page.
361 static void hotplug_devices(struct work_struct *dummy)
363 unsigned int i;
364 struct kvm_device_desc *d;
365 struct device *dev;
367 for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
368 d = kvm_devices + i;
370 /* end of list */
371 if (d->type == 0)
372 break;
374 /* device already exists */
375 dev = device_find_child(kvm_root, d, match_desc);
376 if (dev) {
377 /* XXX check for hotplug remove */
378 put_device(dev);
379 continue;
382 /* new device */
383 printk(KERN_INFO "Adding new virtio device %p\n", d);
384 add_kvm_device(d, i);
389 * we emulate the request_irq behaviour on top of s390 extints
391 static void kvm_extint_handler(struct ext_code ext_code,
392 unsigned int param32, unsigned long param64)
394 struct virtqueue *vq;
395 u32 param;
397 if ((ext_code.subcode & 0xff00) != VIRTIO_SUBCODE_64)
398 return;
399 inc_irq_stat(IRQEXT_VRT);
401 /* The LSB might be overloaded, we have to mask it */
402 vq = (struct virtqueue *)(param64 & ~1UL);
404 /* We use ext_params to decide what this interrupt means */
405 param = param32 & VIRTIO_PARAM_MASK;
407 switch (param) {
408 case VIRTIO_PARAM_CONFIG_CHANGED:
410 struct virtio_driver *drv;
411 drv = container_of(vq->vdev->dev.driver,
412 struct virtio_driver, driver);
413 if (drv->config_changed)
414 drv->config_changed(vq->vdev);
416 break;
418 case VIRTIO_PARAM_DEV_ADD:
419 schedule_work(&hotplug_work);
420 break;
421 case VIRTIO_PARAM_VRING_INTERRUPT:
422 default:
423 vring_interrupt(0, vq);
424 break;
429 * For s390-virtio, we expect a page above main storage containing
430 * the virtio configuration. Try to actually load from this area
431 * in order to figure out if the host provides this page.
433 static int __init test_devices_support(unsigned long addr)
435 int ret = -EIO;
437 asm volatile(
438 "0: lura 0,%1\n"
439 "1: xgr %0,%0\n"
440 "2:\n"
441 EX_TABLE(0b,2b)
442 EX_TABLE(1b,2b)
443 : "+d" (ret)
444 : "a" (addr)
445 : "0", "cc");
446 return ret;
449 * Init function for virtio
450 * devices are in a single page above top of "normal" + standby mem
452 static int __init kvm_devices_init(void)
454 int rc;
455 unsigned long total_memory_size = sclp_get_rzm() * sclp_get_rnmax();
457 if (!MACHINE_IS_KVM)
458 return -ENODEV;
460 if (test_devices_support(total_memory_size) < 0)
461 return -ENODEV;
463 rc = vmem_add_mapping(total_memory_size, PAGE_SIZE);
464 if (rc)
465 return rc;
467 kvm_devices = (void *) total_memory_size;
469 kvm_root = root_device_register("kvm_s390");
470 if (IS_ERR(kvm_root)) {
471 rc = PTR_ERR(kvm_root);
472 printk(KERN_ERR "Could not register kvm_s390 root device");
473 vmem_remove_mapping(total_memory_size, PAGE_SIZE);
474 return rc;
477 INIT_WORK(&hotplug_work, hotplug_devices);
479 irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL);
480 register_external_interrupt(0x2603, kvm_extint_handler);
482 scan_devices();
483 return 0;
486 /* code for early console output with virtio_console */
487 static __init int early_put_chars(u32 vtermno, const char *buf, int count)
489 char scratch[17];
490 unsigned int len = count;
492 if (len > sizeof(scratch) - 1)
493 len = sizeof(scratch) - 1;
494 scratch[len] = '\0';
495 memcpy(scratch, buf, len);
496 kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, __pa(scratch));
497 return len;
500 static int __init s390_virtio_console_init(void)
502 if (sclp_has_vt220() || sclp_has_linemode())
503 return -ENODEV;
504 return virtio_cons_early_init(early_put_chars);
506 console_initcall(s390_virtio_console_init);
510 * We do this after core stuff, but before the drivers.
512 postcore_initcall(kvm_devices_init);