]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/s390/kvm/kvm_virtio.c
Merge commit '900cfa46191a7d87cf1891924cb90499287fd235'; branches 'timers/nohz',...
[mirror_ubuntu-artful-kernel.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>
18#include <linux/interrupt.h>
19#include <linux/virtio_ring.h>
17f34580 20#include <linux/pfn.h>
e976a2b9
CB
21#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
e976a2b9
CB
34struct kvm_device {
35 struct virtio_device vdev;
36 struct kvm_device_desc *desc;
37};
38
39#define to_kvmdev(vd) container_of(vd, struct kvm_device, vdev)
40
41/*
42 * memory layout:
43 * - kvm_device_descriptor
44 * struct kvm_device_desc
45 * - configuration
46 * struct kvm_vqconfig
47 * - feature bits
48 * - config space
49 */
50static struct kvm_vqconfig *kvm_vq_config(const struct kvm_device_desc *desc)
51{
52 return (struct kvm_vqconfig *)(desc + 1);
53}
54
55static u8 *kvm_vq_features(const struct kvm_device_desc *desc)
56{
57 return (u8 *)(kvm_vq_config(desc) + desc->num_vq);
58}
59
60static u8 *kvm_vq_configspace(const struct kvm_device_desc *desc)
61{
62 return kvm_vq_features(desc) + desc->feature_len * 2;
63}
64
65/*
66 * The total size of the config page used by this device (incl. desc)
67 */
68static unsigned desc_size(const struct kvm_device_desc *desc)
69{
70 return sizeof(*desc)
71 + desc->num_vq * sizeof(struct kvm_vqconfig)
72 + desc->feature_len * 2
73 + desc->config_len;
74}
75
5ca9fd54
HC
76/* This gets the device's feature bits. */
77static u32 kvm_get_features(struct virtio_device *vdev)
e976a2b9 78{
5ca9fd54
HC
79 unsigned int i;
80 u32 features = 0;
e976a2b9 81 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
5ca9fd54 82 u8 *in_features = kvm_vq_features(desc);
e976a2b9 83
5ca9fd54
HC
84 for (i = 0; i < min(desc->feature_len * 8, 32); i++)
85 if (in_features[i / 8] & (1 << (i % 8)))
86 features |= (1 << i);
87 return features;
88}
e976a2b9 89
5ca9fd54
HC
90static void kvm_set_features(struct virtio_device *vdev, u32 features)
91{
92 unsigned int i;
93 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
94 /* Second half of bitmap is features we accept. */
95 u8 *out_features = kvm_vq_features(desc) + desc->feature_len;
e976a2b9 96
5ca9fd54
HC
97 memset(out_features, 0, desc->feature_len);
98 for (i = 0; i < min(desc->feature_len * 8, 32); i++) {
99 if (features & (1 << i))
100 out_features[i / 8] |= (1 << (i % 8));
101 }
e976a2b9
CB
102}
103
104/*
105 * Reading and writing elements in config space
106 */
107static void kvm_get(struct virtio_device *vdev, unsigned int offset,
108 void *buf, unsigned len)
109{
110 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
111
112 BUG_ON(offset + len > desc->config_len);
113 memcpy(buf, kvm_vq_configspace(desc) + offset, len);
114}
115
116static void kvm_set(struct virtio_device *vdev, unsigned int offset,
117 const void *buf, unsigned len)
118{
119 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
120
121 BUG_ON(offset + len > desc->config_len);
122 memcpy(kvm_vq_configspace(desc) + offset, buf, len);
123}
124
125/*
126 * The operations to get and set the status word just access
127 * the status field of the device descriptor. set_status will also
128 * make a hypercall to the host, to tell about status changes
129 */
130static u8 kvm_get_status(struct virtio_device *vdev)
131{
132 return to_kvmdev(vdev)->desc->status;
133}
134
135static void kvm_set_status(struct virtio_device *vdev, u8 status)
136{
137 BUG_ON(!status);
138 to_kvmdev(vdev)->desc->status = status;
139 kvm_hypercall1(KVM_S390_VIRTIO_SET_STATUS,
140 (unsigned long) to_kvmdev(vdev)->desc);
141}
142
143/*
144 * To reset the device, we use the KVM_VIRTIO_RESET hypercall, using the
145 * descriptor address. The Host will zero the status and all the
146 * features.
147 */
148static void kvm_reset(struct virtio_device *vdev)
149{
150 kvm_hypercall1(KVM_S390_VIRTIO_RESET,
151 (unsigned long) to_kvmdev(vdev)->desc);
152}
153
154/*
155 * When the virtio_ring code wants to notify the Host, it calls us here and we
156 * make a hypercall. We hand the address of the virtqueue so the Host
157 * knows which virtqueue we're talking about.
158 */
159static void kvm_notify(struct virtqueue *vq)
160{
161 struct kvm_vqconfig *config = vq->priv;
162
163 kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, config->address);
164}
165
166/*
167 * This routine finds the first virtqueue described in the configuration of
168 * this device and sets it up.
169 */
170static struct virtqueue *kvm_find_vq(struct virtio_device *vdev,
171 unsigned index,
172 void (*callback)(struct virtqueue *vq))
173{
174 struct kvm_device *kdev = to_kvmdev(vdev);
175 struct kvm_vqconfig *config;
176 struct virtqueue *vq;
177 int err;
178
179 if (index >= kdev->desc->num_vq)
180 return ERR_PTR(-ENOENT);
181
182 config = kvm_vq_config(kdev->desc)+index;
183
17f34580
HC
184 err = vmem_add_mapping(config->address,
185 vring_size(config->num, PAGE_SIZE));
186 if (err)
e976a2b9 187 goto out;
e976a2b9
CB
188
189 vq = vring_new_virtqueue(config->num, vdev, (void *) config->address,
190 kvm_notify, callback);
191 if (!vq) {
192 err = -ENOMEM;
193 goto unmap;
194 }
195
196 /*
197 * register a callback token
198 * The host will sent this via the external interrupt parameter
199 */
200 config->token = (u64) vq;
201
202 vq->priv = config;
203 return vq;
204unmap:
17f34580
HC
205 vmem_remove_mapping(config->address,
206 vring_size(config->num, PAGE_SIZE));
e976a2b9
CB
207out:
208 return ERR_PTR(err);
209}
210
211static void kvm_del_vq(struct virtqueue *vq)
212{
213 struct kvm_vqconfig *config = vq->priv;
214
215 vring_del_virtqueue(vq);
17f34580
HC
216 vmem_remove_mapping(config->address,
217 vring_size(config->num, PAGE_SIZE));
e976a2b9
CB
218}
219
220/*
221 * The config ops structure as defined by virtio config
222 */
223static struct virtio_config_ops kvm_vq_configspace_ops = {
5ca9fd54
HC
224 .get_features = kvm_get_features,
225 .set_features = kvm_set_features,
e976a2b9
CB
226 .get = kvm_get,
227 .set = kvm_set,
228 .get_status = kvm_get_status,
229 .set_status = kvm_set_status,
230 .reset = kvm_reset,
231 .find_vq = kvm_find_vq,
232 .del_vq = kvm_del_vq,
233};
234
235/*
236 * The root device for the kvm virtio devices.
237 * This makes them appear as /sys/devices/kvm_s390/0,1,2 not /sys/devices/0,1,2.
238 */
239static struct device kvm_root = {
240 .parent = NULL,
241 .bus_id = "kvm_s390",
242};
243
244/*
245 * adds a new device and register it with virtio
246 * appropriate drivers are loaded by the device model
247 */
b769f579 248static void add_kvm_device(struct kvm_device_desc *d, unsigned int offset)
e976a2b9
CB
249{
250 struct kvm_device *kdev;
251
252 kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
253 if (!kdev) {
b769f579
RR
254 printk(KERN_EMERG "Cannot allocate kvm dev %u type %u\n",
255 offset, d->type);
e976a2b9
CB
256 return;
257 }
258
259 kdev->vdev.dev.parent = &kvm_root;
e976a2b9
CB
260 kdev->vdev.id.device = d->type;
261 kdev->vdev.config = &kvm_vq_configspace_ops;
262 kdev->desc = d;
263
264 if (register_virtio_device(&kdev->vdev) != 0) {
b769f579
RR
265 printk(KERN_ERR "Failed to register kvm device %u type %u\n",
266 offset, d->type);
e976a2b9
CB
267 kfree(kdev);
268 }
269}
270
271/*
272 * scan_devices() simply iterates through the device page.
273 * The type 0 is reserved to mean "end of devices".
274 */
275static void scan_devices(void)
276{
277 unsigned int i;
278 struct kvm_device_desc *d;
279
280 for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
281 d = kvm_devices + i;
282
283 if (d->type == 0)
284 break;
285
b769f579 286 add_kvm_device(d, i);
e976a2b9
CB
287 }
288}
289
290/*
291 * we emulate the request_irq behaviour on top of s390 extints
292 */
293static void kvm_extint_handler(u16 code)
294{
295 void *data = (void *) *(long *) __LC_PFAULT_INTPARM;
296 u16 subcode = S390_lowcore.cpu_addr;
297
298 if ((subcode & 0xff00) != VIRTIO_SUBCODE_64)
299 return;
300
301 vring_interrupt(0, data);
302}
303
304/*
305 * Init function for virtio
306 * devices are in a single page above top of "normal" mem
307 */
308static int __init kvm_devices_init(void)
309{
310 int rc;
311
312 if (!MACHINE_IS_KVM)
313 return -ENODEV;
314
315 rc = device_register(&kvm_root);
316 if (rc) {
317 printk(KERN_ERR "Could not register kvm_s390 root device");
318 return rc;
319 }
320
17f34580
HC
321 rc = vmem_add_mapping(PFN_PHYS(max_pfn), PAGE_SIZE);
322 if (rc) {
e976a2b9 323 device_unregister(&kvm_root);
17f34580 324 return rc;
e976a2b9
CB
325 }
326
17f34580 327 kvm_devices = (void *) PFN_PHYS(max_pfn);
e976a2b9
CB
328
329 ctl_set_bit(0, 9);
330 register_external_interrupt(0x2603, kvm_extint_handler);
331
332 scan_devices();
333 return 0;
334}
335
336/*
337 * We do this after core stuff, but before the drivers.
338 */
339postcore_initcall(kvm_devices_init);