GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / s390 / kvm / kvm_virtio.c
blob4e298bc8949d36c84a1fb5cdecbe67360369a7b0
1 /*
2 * kvm_virtio.c - 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/init.h>
14 #include <linux/bootmem.h>
15 #include <linux/err.h>
16 #include <linux/virtio.h>
17 #include <linux/virtio_config.h>
18 #include <linux/slab.h>
19 #include <linux/virtio_console.h>
20 #include <linux/interrupt.h>
21 #include <linux/virtio_ring.h>
22 #include <linux/pfn.h>
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>
29 #define VIRTIO_SUBCODE_64 0x0D00
32 * The pointer to our (page) of device descriptions.
34 static void *kvm_devices;
36 struct kvm_device {
37 struct virtio_device vdev;
38 struct kvm_device_desc *desc;
41 #define to_kvmdev(vd) container_of(vd, struct kvm_device, vdev)
44 * memory layout:
45 * - kvm_device_descriptor
46 * struct kvm_device_desc
47 * - configuration
48 * struct kvm_vqconfig
49 * - feature bits
50 * - config space
52 static struct kvm_vqconfig *kvm_vq_config(const struct kvm_device_desc *desc)
54 return (struct kvm_vqconfig *)(desc + 1);
57 static u8 *kvm_vq_features(const struct kvm_device_desc *desc)
59 return (u8 *)(kvm_vq_config(desc) + desc->num_vq);
62 static u8 *kvm_vq_configspace(const struct kvm_device_desc *desc)
64 return kvm_vq_features(desc) + desc->feature_len * 2;
68 * The total size of the config page used by this device (incl. desc)
70 static unsigned desc_size(const struct kvm_device_desc *desc)
72 return sizeof(*desc)
73 + desc->num_vq * sizeof(struct kvm_vqconfig)
74 + desc->feature_len * 2
75 + desc->config_len;
78 /* This gets the device's feature bits. */
79 static u32 kvm_get_features(struct virtio_device *vdev)
81 unsigned int i;
82 u32 features = 0;
83 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
84 u8 *in_features = kvm_vq_features(desc);
86 for (i = 0; i < min(desc->feature_len * 8, 32); i++)
87 if (in_features[i / 8] & (1 << (i % 8)))
88 features |= (1 << i);
89 return features;
92 static void kvm_finalize_features(struct virtio_device *vdev)
94 unsigned int i, bits;
95 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
96 /* Second half of bitmap is features we accept. */
97 u8 *out_features = kvm_vq_features(desc) + desc->feature_len;
99 /* Give virtio_ring a chance to accept features. */
100 vring_transport_features(vdev);
102 memset(out_features, 0, desc->feature_len);
103 bits = min_t(unsigned, desc->feature_len, sizeof(vdev->features)) * 8;
104 for (i = 0; i < bits; i++) {
105 if (test_bit(i, vdev->features))
106 out_features[i / 8] |= (1 << (i % 8));
111 * Reading and writing elements in config space
113 static void kvm_get(struct virtio_device *vdev, unsigned int offset,
114 void *buf, unsigned len)
116 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
118 BUG_ON(offset + len > desc->config_len);
119 memcpy(buf, kvm_vq_configspace(desc) + offset, len);
122 static void kvm_set(struct virtio_device *vdev, unsigned int offset,
123 const void *buf, unsigned len)
125 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
127 BUG_ON(offset + len > desc->config_len);
128 memcpy(kvm_vq_configspace(desc) + offset, buf, len);
132 * The operations to get and set the status word just access
133 * the status field of the device descriptor. set_status will also
134 * make a hypercall to the host, to tell about status changes
136 static u8 kvm_get_status(struct virtio_device *vdev)
138 return to_kvmdev(vdev)->desc->status;
141 static void kvm_set_status(struct virtio_device *vdev, u8 status)
143 BUG_ON(!status);
144 to_kvmdev(vdev)->desc->status = status;
145 kvm_hypercall1(KVM_S390_VIRTIO_SET_STATUS,
146 (unsigned long) to_kvmdev(vdev)->desc);
150 * To reset the device, we use the KVM_VIRTIO_RESET hypercall, using the
151 * descriptor address. The Host will zero the status and all the
152 * features.
154 static void kvm_reset(struct virtio_device *vdev)
156 kvm_hypercall1(KVM_S390_VIRTIO_RESET,
157 (unsigned long) to_kvmdev(vdev)->desc);
161 * When the virtio_ring code wants to notify the Host, it calls us here and we
162 * make a hypercall. We hand the address of the virtqueue so the Host
163 * knows which virtqueue we're talking about.
165 static void kvm_notify(struct virtqueue *vq)
167 struct kvm_vqconfig *config = vq->priv;
169 kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, config->address);
173 * This routine finds the first virtqueue described in the configuration of
174 * this device and sets it up.
176 static struct virtqueue *kvm_find_vq(struct virtio_device *vdev,
177 unsigned index,
178 void (*callback)(struct virtqueue *vq),
179 const char *name)
181 struct kvm_device *kdev = to_kvmdev(vdev);
182 struct kvm_vqconfig *config;
183 struct virtqueue *vq;
184 int err;
186 if (index >= kdev->desc->num_vq)
187 return ERR_PTR(-ENOENT);
189 config = kvm_vq_config(kdev->desc)+index;
191 err = vmem_add_mapping(config->address,
192 vring_size(config->num,
193 KVM_S390_VIRTIO_RING_ALIGN));
194 if (err)
195 goto out;
197 vq = vring_new_virtqueue(config->num, KVM_S390_VIRTIO_RING_ALIGN,
198 vdev, (void *) config->address,
199 kvm_notify, callback, name);
200 if (!vq) {
201 err = -ENOMEM;
202 goto unmap;
206 * register a callback token
207 * The host will sent this via the external interrupt parameter
209 config->token = (u64) vq;
211 vq->priv = config;
212 return vq;
213 unmap:
214 vmem_remove_mapping(config->address,
215 vring_size(config->num,
216 KVM_S390_VIRTIO_RING_ALIGN));
217 out:
218 return ERR_PTR(err);
221 static void kvm_del_vq(struct virtqueue *vq)
223 struct kvm_vqconfig *config = vq->priv;
225 vring_del_virtqueue(vq);
226 vmem_remove_mapping(config->address,
227 vring_size(config->num,
228 KVM_S390_VIRTIO_RING_ALIGN));
231 static void kvm_del_vqs(struct virtio_device *vdev)
233 struct virtqueue *vq, *n;
235 list_for_each_entry_safe(vq, n, &vdev->vqs, list)
236 kvm_del_vq(vq);
239 static int kvm_find_vqs(struct virtio_device *vdev, unsigned nvqs,
240 struct virtqueue *vqs[],
241 vq_callback_t *callbacks[],
242 const char *names[])
244 struct kvm_device *kdev = to_kvmdev(vdev);
245 int i;
247 /* We must have this many virtqueues. */
248 if (nvqs > kdev->desc->num_vq)
249 return -ENOENT;
251 for (i = 0; i < nvqs; ++i) {
252 vqs[i] = kvm_find_vq(vdev, i, callbacks[i], names[i]);
253 if (IS_ERR(vqs[i]))
254 goto error;
256 return 0;
258 error:
259 kvm_del_vqs(vdev);
260 return PTR_ERR(vqs[i]);
264 * The config ops structure as defined by virtio config
266 static struct virtio_config_ops kvm_vq_configspace_ops = {
267 .get_features = kvm_get_features,
268 .finalize_features = kvm_finalize_features,
269 .get = kvm_get,
270 .set = kvm_set,
271 .get_status = kvm_get_status,
272 .set_status = kvm_set_status,
273 .reset = kvm_reset,
274 .find_vqs = kvm_find_vqs,
275 .del_vqs = kvm_del_vqs,
279 * The root device for the kvm virtio devices.
280 * This makes them appear as /sys/devices/kvm_s390/0,1,2 not /sys/devices/0,1,2.
282 static struct device *kvm_root;
285 * adds a new device and register it with virtio
286 * appropriate drivers are loaded by the device model
288 static void add_kvm_device(struct kvm_device_desc *d, unsigned int offset)
290 struct kvm_device *kdev;
292 kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
293 if (!kdev) {
294 printk(KERN_EMERG "Cannot allocate kvm dev %u type %u\n",
295 offset, d->type);
296 return;
299 kdev->vdev.dev.parent = kvm_root;
300 kdev->vdev.id.device = d->type;
301 kdev->vdev.config = &kvm_vq_configspace_ops;
302 kdev->desc = d;
304 if (register_virtio_device(&kdev->vdev) != 0) {
305 printk(KERN_ERR "Failed to register kvm device %u type %u\n",
306 offset, d->type);
307 kfree(kdev);
312 * scan_devices() simply iterates through the device page.
313 * The type 0 is reserved to mean "end of devices".
315 static void scan_devices(void)
317 unsigned int i;
318 struct kvm_device_desc *d;
320 for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
321 d = kvm_devices + i;
323 if (d->type == 0)
324 break;
326 add_kvm_device(d, i);
331 * we emulate the request_irq behaviour on top of s390 extints
333 static void kvm_extint_handler(u16 code)
335 struct virtqueue *vq;
336 u16 subcode;
337 int config_changed;
339 subcode = S390_lowcore.cpu_addr;
340 if ((subcode & 0xff00) != VIRTIO_SUBCODE_64)
341 return;
343 /* The LSB might be overloaded, we have to mask it */
344 vq = (struct virtqueue *)(S390_lowcore.ext_params2 & ~1UL);
346 /* We use the LSB of extparam, to decide, if this interrupt is a config
347 * change or a "standard" interrupt */
348 config_changed = S390_lowcore.ext_params & 1;
350 if (config_changed) {
351 struct virtio_driver *drv;
352 drv = container_of(vq->vdev->dev.driver,
353 struct virtio_driver, driver);
354 if (drv->config_changed)
355 drv->config_changed(vq->vdev);
356 } else
357 vring_interrupt(0, vq);
361 * Init function for virtio
362 * devices are in a single page above top of "normal" mem
364 static int __init kvm_devices_init(void)
366 int rc;
368 if (!MACHINE_IS_KVM)
369 return -ENODEV;
371 kvm_root = root_device_register("kvm_s390");
372 if (IS_ERR(kvm_root)) {
373 rc = PTR_ERR(kvm_root);
374 printk(KERN_ERR "Could not register kvm_s390 root device");
375 return rc;
378 rc = vmem_add_mapping(real_memory_size, PAGE_SIZE);
379 if (rc) {
380 root_device_unregister(kvm_root);
381 return rc;
384 kvm_devices = (void *) real_memory_size;
386 ctl_set_bit(0, 9);
387 register_external_interrupt(0x2603, kvm_extint_handler);
389 scan_devices();
390 return 0;
393 /* code for early console output with virtio_console */
394 static __init int early_put_chars(u32 vtermno, const char *buf, int count)
396 char scratch[17];
397 unsigned int len = count;
399 if (len > sizeof(scratch) - 1)
400 len = sizeof(scratch) - 1;
401 scratch[len] = '\0';
402 memcpy(scratch, buf, len);
403 kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, __pa(scratch));
404 return len;
407 static int __init s390_virtio_console_init(void)
409 if (!MACHINE_IS_KVM)
410 return -ENODEV;
411 return virtio_cons_early_init(early_put_chars);
413 console_initcall(s390_virtio_console_init);
417 * We do this after core stuff, but before the drivers.
419 postcore_initcall(kvm_devices_init);