blob: 5953510e7d5fe35a6d2b35c2b664c30b655ab089 [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>
Christian Borntraegerfaeba8302008-06-20 15:24:18 +020018#include <linux/virtio_console.h>
Christian Borntraegere976a2b2008-03-25 18:47:46 +010019#include <linux/interrupt.h>
20#include <linux/virtio_ring.h>
Heiko Carstens17f34582008-04-30 13:38:47 +020021#include <linux/pfn.h>
Christian Borntraegere976a2b2008-03-25 18:47:46 +010022#include <asm/io.h>
23#include <asm/kvm_para.h>
24#include <asm/kvm_virtio.h>
25#include <asm/setup.h>
26#include <asm/s390_ext.h>
27
28#define VIRTIO_SUBCODE_64 0x0D00
29
30/*
31 * The pointer to our (page) of device descriptions.
32 */
33static void *kvm_devices;
34
Christian Borntraegere976a2b2008-03-25 18:47:46 +010035struct kvm_device {
36 struct virtio_device vdev;
37 struct kvm_device_desc *desc;
38};
39
40#define to_kvmdev(vd) container_of(vd, struct kvm_device, vdev)
41
42/*
43 * memory layout:
44 * - kvm_device_descriptor
45 * struct kvm_device_desc
46 * - configuration
47 * struct kvm_vqconfig
48 * - feature bits
49 * - config space
50 */
51static struct kvm_vqconfig *kvm_vq_config(const struct kvm_device_desc *desc)
52{
53 return (struct kvm_vqconfig *)(desc + 1);
54}
55
56static u8 *kvm_vq_features(const struct kvm_device_desc *desc)
57{
58 return (u8 *)(kvm_vq_config(desc) + desc->num_vq);
59}
60
61static u8 *kvm_vq_configspace(const struct kvm_device_desc *desc)
62{
63 return kvm_vq_features(desc) + desc->feature_len * 2;
64}
65
66/*
67 * The total size of the config page used by this device (incl. desc)
68 */
69static unsigned desc_size(const struct kvm_device_desc *desc)
70{
71 return sizeof(*desc)
72 + desc->num_vq * sizeof(struct kvm_vqconfig)
73 + desc->feature_len * 2
74 + desc->config_len;
75}
76
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030077/* This gets the device's feature bits. */
78static u32 kvm_get_features(struct virtio_device *vdev)
Christian Borntraegere976a2b2008-03-25 18:47:46 +010079{
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030080 unsigned int i;
81 u32 features = 0;
Christian Borntraegere976a2b2008-03-25 18:47:46 +010082 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030083 u8 *in_features = kvm_vq_features(desc);
Christian Borntraegere976a2b2008-03-25 18:47:46 +010084
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030085 for (i = 0; i < min(desc->feature_len * 8, 32); i++)
86 if (in_features[i / 8] & (1 << (i % 8)))
87 features |= (1 << i);
88 return features;
89}
Christian Borntraegere976a2b2008-03-25 18:47:46 +010090
Rusty Russellc6248962008-07-25 12:06:07 -050091static void kvm_finalize_features(struct virtio_device *vdev)
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030092{
Rusty Russellc6248962008-07-25 12:06:07 -050093 unsigned int i, bits;
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030094 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
95 /* Second half of bitmap is features we accept. */
96 u8 *out_features = kvm_vq_features(desc) + desc->feature_len;
Christian Borntraegere976a2b2008-03-25 18:47:46 +010097
Heiko Carstens5ca9fd52008-05-06 17:38:30 +030098 memset(out_features, 0, desc->feature_len);
Rusty Russellc6248962008-07-25 12:06:07 -050099 bits = min_t(unsigned, desc->feature_len, sizeof(vdev->features)) * 8;
100 for (i = 0; i < bits; i++) {
101 if (test_bit(i, vdev->features))
Heiko Carstens5ca9fd52008-05-06 17:38:30 +0300102 out_features[i / 8] |= (1 << (i % 8));
103 }
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100104}
105
106/*
107 * Reading and writing elements in config space
108 */
109static void kvm_get(struct virtio_device *vdev, unsigned int offset,
110 void *buf, unsigned len)
111{
112 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
113
114 BUG_ON(offset + len > desc->config_len);
115 memcpy(buf, kvm_vq_configspace(desc) + offset, len);
116}
117
118static void kvm_set(struct virtio_device *vdev, unsigned int offset,
119 const void *buf, unsigned len)
120{
121 struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
122
123 BUG_ON(offset + len > desc->config_len);
124 memcpy(kvm_vq_configspace(desc) + offset, buf, len);
125}
126
127/*
128 * The operations to get and set the status word just access
129 * the status field of the device descriptor. set_status will also
130 * make a hypercall to the host, to tell about status changes
131 */
132static u8 kvm_get_status(struct virtio_device *vdev)
133{
134 return to_kvmdev(vdev)->desc->status;
135}
136
137static void kvm_set_status(struct virtio_device *vdev, u8 status)
138{
139 BUG_ON(!status);
140 to_kvmdev(vdev)->desc->status = status;
141 kvm_hypercall1(KVM_S390_VIRTIO_SET_STATUS,
142 (unsigned long) to_kvmdev(vdev)->desc);
143}
144
145/*
146 * To reset the device, we use the KVM_VIRTIO_RESET hypercall, using the
147 * descriptor address. The Host will zero the status and all the
148 * features.
149 */
150static void kvm_reset(struct virtio_device *vdev)
151{
152 kvm_hypercall1(KVM_S390_VIRTIO_RESET,
153 (unsigned long) to_kvmdev(vdev)->desc);
154}
155
156/*
157 * When the virtio_ring code wants to notify the Host, it calls us here and we
158 * make a hypercall. We hand the address of the virtqueue so the Host
159 * knows which virtqueue we're talking about.
160 */
161static void kvm_notify(struct virtqueue *vq)
162{
163 struct kvm_vqconfig *config = vq->priv;
164
165 kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, config->address);
166}
167
168/*
169 * This routine finds the first virtqueue described in the configuration of
170 * this device and sets it up.
171 */
172static struct virtqueue *kvm_find_vq(struct virtio_device *vdev,
173 unsigned index,
174 void (*callback)(struct virtqueue *vq))
175{
176 struct kvm_device *kdev = to_kvmdev(vdev);
177 struct kvm_vqconfig *config;
178 struct virtqueue *vq;
179 int err;
180
181 if (index >= kdev->desc->num_vq)
182 return ERR_PTR(-ENOENT);
183
184 config = kvm_vq_config(kdev->desc)+index;
185
Heiko Carstens17f34582008-04-30 13:38:47 +0200186 err = vmem_add_mapping(config->address,
187 vring_size(config->num, PAGE_SIZE));
188 if (err)
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100189 goto out;
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100190
191 vq = vring_new_virtqueue(config->num, vdev, (void *) config->address,
192 kvm_notify, callback);
193 if (!vq) {
194 err = -ENOMEM;
195 goto unmap;
196 }
197
198 /*
199 * register a callback token
200 * The host will sent this via the external interrupt parameter
201 */
202 config->token = (u64) vq;
203
204 vq->priv = config;
205 return vq;
206unmap:
Heiko Carstens17f34582008-04-30 13:38:47 +0200207 vmem_remove_mapping(config->address,
208 vring_size(config->num, PAGE_SIZE));
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100209out:
210 return ERR_PTR(err);
211}
212
213static void kvm_del_vq(struct virtqueue *vq)
214{
215 struct kvm_vqconfig *config = vq->priv;
216
217 vring_del_virtqueue(vq);
Heiko Carstens17f34582008-04-30 13:38:47 +0200218 vmem_remove_mapping(config->address,
219 vring_size(config->num, PAGE_SIZE));
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100220}
221
222/*
223 * The config ops structure as defined by virtio config
224 */
225static struct virtio_config_ops kvm_vq_configspace_ops = {
Heiko Carstens5ca9fd52008-05-06 17:38:30 +0300226 .get_features = kvm_get_features,
Rusty Russellc6248962008-07-25 12:06:07 -0500227 .finalize_features = kvm_finalize_features,
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100228 .get = kvm_get,
229 .set = kvm_set,
230 .get_status = kvm_get_status,
231 .set_status = kvm_set_status,
232 .reset = kvm_reset,
233 .find_vq = kvm_find_vq,
234 .del_vq = kvm_del_vq,
235};
236
237/*
238 * The root device for the kvm virtio devices.
239 * This makes them appear as /sys/devices/kvm_s390/0,1,2 not /sys/devices/0,1,2.
240 */
241static struct device kvm_root = {
242 .parent = NULL,
243 .bus_id = "kvm_s390",
244};
245
246/*
247 * adds a new device and register it with virtio
248 * appropriate drivers are loaded by the device model
249 */
Rusty Russellb769f572008-05-30 15:09:42 -0500250static void add_kvm_device(struct kvm_device_desc *d, unsigned int offset)
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100251{
252 struct kvm_device *kdev;
253
254 kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
255 if (!kdev) {
Rusty Russellb769f572008-05-30 15:09:42 -0500256 printk(KERN_EMERG "Cannot allocate kvm dev %u type %u\n",
257 offset, d->type);
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100258 return;
259 }
260
261 kdev->vdev.dev.parent = &kvm_root;
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100262 kdev->vdev.id.device = d->type;
263 kdev->vdev.config = &kvm_vq_configspace_ops;
264 kdev->desc = d;
265
266 if (register_virtio_device(&kdev->vdev) != 0) {
Rusty Russellb769f572008-05-30 15:09:42 -0500267 printk(KERN_ERR "Failed to register kvm device %u type %u\n",
268 offset, d->type);
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100269 kfree(kdev);
270 }
271}
272
273/*
274 * scan_devices() simply iterates through the device page.
275 * The type 0 is reserved to mean "end of devices".
276 */
277static void scan_devices(void)
278{
279 unsigned int i;
280 struct kvm_device_desc *d;
281
282 for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
283 d = kvm_devices + i;
284
285 if (d->type == 0)
286 break;
287
Rusty Russellb769f572008-05-30 15:09:42 -0500288 add_kvm_device(d, i);
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100289 }
290}
291
292/*
293 * we emulate the request_irq behaviour on top of s390 extints
294 */
295static void kvm_extint_handler(u16 code)
296{
297 void *data = (void *) *(long *) __LC_PFAULT_INTPARM;
298 u16 subcode = S390_lowcore.cpu_addr;
299
300 if ((subcode & 0xff00) != VIRTIO_SUBCODE_64)
301 return;
302
303 vring_interrupt(0, data);
304}
305
306/*
307 * Init function for virtio
308 * devices are in a single page above top of "normal" mem
309 */
310static int __init kvm_devices_init(void)
311{
312 int rc;
313
314 if (!MACHINE_IS_KVM)
315 return -ENODEV;
316
317 rc = device_register(&kvm_root);
318 if (rc) {
319 printk(KERN_ERR "Could not register kvm_s390 root device");
320 return rc;
321 }
322
Heiko Carstens17f34582008-04-30 13:38:47 +0200323 rc = vmem_add_mapping(PFN_PHYS(max_pfn), PAGE_SIZE);
324 if (rc) {
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100325 device_unregister(&kvm_root);
Heiko Carstens17f34582008-04-30 13:38:47 +0200326 return rc;
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100327 }
328
Heiko Carstens17f34582008-04-30 13:38:47 +0200329 kvm_devices = (void *) PFN_PHYS(max_pfn);
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100330
331 ctl_set_bit(0, 9);
332 register_external_interrupt(0x2603, kvm_extint_handler);
333
334 scan_devices();
335 return 0;
336}
337
Christian Borntraegerfaeba8302008-06-20 15:24:18 +0200338/* code for early console output with virtio_console */
339static __init int early_put_chars(u32 vtermno, const char *buf, int count)
340{
341 char scratch[17];
342 unsigned int len = count;
343
344 if (len > sizeof(scratch) - 1)
345 len = sizeof(scratch) - 1;
346 scratch[len] = '\0';
347 memcpy(scratch, buf, len);
348 kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, __pa(scratch));
349 return len;
350}
351
352void s390_virtio_console_init(void)
353{
354 virtio_cons_early_init(early_put_chars);
355}
356
Christian Borntraegere976a2b2008-03-25 18:47:46 +0100357/*
358 * We do this after core stuff, but before the drivers.
359 */
360postcore_initcall(kvm_devices_init);