blob: 9f55ce6f3c7817380f87a429ae74d0f071fa3047 [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>
18#include <linux/interrupt.h>
19#include <linux/virtio_ring.h>
Heiko Carstens17f34582008-04-30 13:38:47 +020020#include <linux/pfn.h>
Christian Borntraegere976a2b2008-03-25 18:47:46 +010021#include <asm/io.h>
22#include <asm/kvm_para.h>
23#include <asm/kvm_virtio.h>
24#include <asm/setup.h>
25#include <asm/s390_ext.h>
26
27#define VIRTIO_SUBCODE_64 0x0D00
28
29/*
30 * The pointer to our (page) of device descriptions.
31 */
32static void *kvm_devices;
33
34/*
35 * Unique numbering for kvm devices.
36 */
37static unsigned int dev_index;
38
39struct kvm_device {
40 struct virtio_device vdev;
41 struct kvm_device_desc *desc;
42};
43
44#define to_kvmdev(vd) container_of(vd, struct kvm_device, vdev)
45
46/*
47 * memory layout:
48 * - kvm_device_descriptor
49 * struct kvm_device_desc
50 * - configuration
51 * struct kvm_vqconfig
52 * - feature bits
53 * - config space
54 */
55static struct kvm_vqconfig *kvm_vq_config(const struct kvm_device_desc *desc)
56{
57 return (struct kvm_vqconfig *)(desc + 1);
58}
59
60static u8 *kvm_vq_features(const struct kvm_device_desc *desc)
61{
62 return (u8 *)(kvm_vq_config(desc) + desc->num_vq);
63}
64
65static u8 *kvm_vq_configspace(const struct kvm_device_desc *desc)
66{
67 return kvm_vq_features(desc) + desc->feature_len * 2;
68}
69
70/*
71 * The total size of the config page used by this device (incl. desc)
72 */
73static unsigned desc_size(const struct kvm_device_desc *desc)
74{
75 return sizeof(*desc)
76 + desc->num_vq * sizeof(struct kvm_vqconfig)
77 + desc->feature_len * 2
78 + desc->config_len;
79}
80
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030081/* This gets the device's feature bits. */
82static u32 kvm_get_features(struct virtio_device *vdev)
Christian Borntraegere976a2b2008-03-25 18:47:46 +010083{
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030084 unsigned int i;
85 u32 features = 0;
Christian Borntraegere976a2b2008-03-25 18:47:46 +010086 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030087 u8 *in_features = kvm_vq_features(desc);
Christian Borntraegere976a2b2008-03-25 18:47:46 +010088
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030089 for (i = 0; i < min(desc->feature_len * 8, 32); i++)
90 if (in_features[i / 8] & (1 << (i % 8)))
91 features |= (1 << i);
92 return features;
93}
Christian Borntraegere976a2b2008-03-25 18:47:46 +010094
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030095static void kvm_set_features(struct virtio_device *vdev, u32 features)
96{
97 unsigned int i;
98 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
99 /* Second half of bitmap is features we accept. */
100 u8 *out_features = kvm_vq_features(desc) + desc->feature_len;
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100101
Heiko Carstens5ca9fd52008-05-06 17:38:30 +0300102 memset(out_features, 0, desc->feature_len);
103 for (i = 0; i < min(desc->feature_len * 8, 32); i++) {
104 if (features & (1 << i))
105 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,
176 unsigned index,
177 void (*callback)(struct virtqueue *vq))
178{
179 struct kvm_device *kdev = to_kvmdev(vdev);
180 struct kvm_vqconfig *config;
181 struct virtqueue *vq;
182 int err;
183
184 if (index >= kdev->desc->num_vq)
185 return ERR_PTR(-ENOENT);
186
187 config = kvm_vq_config(kdev->desc)+index;
188
Heiko Carstens17f34582008-04-30 13:38:47 +0200189 err = vmem_add_mapping(config->address,
190 vring_size(config->num, PAGE_SIZE));
191 if (err)
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100192 goto out;
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100193
194 vq = vring_new_virtqueue(config->num, vdev, (void *) config->address,
195 kvm_notify, callback);
196 if (!vq) {
197 err = -ENOMEM;
198 goto unmap;
199 }
200
201 /*
202 * register a callback token
203 * The host will sent this via the external interrupt parameter
204 */
205 config->token = (u64) vq;
206
207 vq->priv = config;
208 return vq;
209unmap:
Heiko Carstens17f34582008-04-30 13:38:47 +0200210 vmem_remove_mapping(config->address,
211 vring_size(config->num, PAGE_SIZE));
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100212out:
213 return ERR_PTR(err);
214}
215
216static void kvm_del_vq(struct virtqueue *vq)
217{
218 struct kvm_vqconfig *config = vq->priv;
219
220 vring_del_virtqueue(vq);
Heiko Carstens17f34582008-04-30 13:38:47 +0200221 vmem_remove_mapping(config->address,
222 vring_size(config->num, PAGE_SIZE));
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100223}
224
225/*
226 * The config ops structure as defined by virtio config
227 */
228static struct virtio_config_ops kvm_vq_configspace_ops = {
Heiko Carstens5ca9fd52008-05-06 17:38:30 +0300229 .get_features = kvm_get_features,
230 .set_features = kvm_set_features,
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100231 .get = kvm_get,
232 .set = kvm_set,
233 .get_status = kvm_get_status,
234 .set_status = kvm_set_status,
235 .reset = kvm_reset,
236 .find_vq = kvm_find_vq,
237 .del_vq = kvm_del_vq,
238};
239
240/*
241 * The root device for the kvm virtio devices.
242 * This makes them appear as /sys/devices/kvm_s390/0,1,2 not /sys/devices/0,1,2.
243 */
244static struct device kvm_root = {
245 .parent = NULL,
246 .bus_id = "kvm_s390",
247};
248
249/*
250 * adds a new device and register it with virtio
251 * appropriate drivers are loaded by the device model
252 */
253static void add_kvm_device(struct kvm_device_desc *d)
254{
255 struct kvm_device *kdev;
256
257 kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
258 if (!kdev) {
259 printk(KERN_EMERG "Cannot allocate kvm dev %u\n",
260 dev_index++);
261 return;
262 }
263
264 kdev->vdev.dev.parent = &kvm_root;
265 kdev->vdev.index = dev_index++;
266 kdev->vdev.id.device = d->type;
267 kdev->vdev.config = &kvm_vq_configspace_ops;
268 kdev->desc = d;
269
270 if (register_virtio_device(&kdev->vdev) != 0) {
271 printk(KERN_ERR "Failed to register kvm device %u\n",
272 kdev->vdev.index);
273 kfree(kdev);
274 }
275}
276
277/*
278 * scan_devices() simply iterates through the device page.
279 * The type 0 is reserved to mean "end of devices".
280 */
281static void scan_devices(void)
282{
283 unsigned int i;
284 struct kvm_device_desc *d;
285
286 for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
287 d = kvm_devices + i;
288
289 if (d->type == 0)
290 break;
291
292 add_kvm_device(d);
293 }
294}
295
296/*
297 * we emulate the request_irq behaviour on top of s390 extints
298 */
299static void kvm_extint_handler(u16 code)
300{
301 void *data = (void *) *(long *) __LC_PFAULT_INTPARM;
302 u16 subcode = S390_lowcore.cpu_addr;
303
304 if ((subcode & 0xff00) != VIRTIO_SUBCODE_64)
305 return;
306
307 vring_interrupt(0, data);
308}
309
310/*
311 * Init function for virtio
312 * devices are in a single page above top of "normal" mem
313 */
314static int __init kvm_devices_init(void)
315{
316 int rc;
317
318 if (!MACHINE_IS_KVM)
319 return -ENODEV;
320
321 rc = device_register(&kvm_root);
322 if (rc) {
323 printk(KERN_ERR "Could not register kvm_s390 root device");
324 return rc;
325 }
326
Heiko Carstens17f34582008-04-30 13:38:47 +0200327 rc = vmem_add_mapping(PFN_PHYS(max_pfn), PAGE_SIZE);
328 if (rc) {
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100329 device_unregister(&kvm_root);
Heiko Carstens17f34582008-04-30 13:38:47 +0200330 return rc;
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100331 }
332
Heiko Carstens17f34582008-04-30 13:38:47 +0200333 kvm_devices = (void *) PFN_PHYS(max_pfn);
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100334
335 ctl_set_bit(0, 9);
336 register_external_interrupt(0x2603, kvm_extint_handler);
337
338 scan_devices();
339 return 0;
340}
341
342/*
343 * We do this after core stuff, but before the drivers.
344 */
345postcore_initcall(kvm_devices_init);