Commit | Line | Data |
---|---|---|
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 | ||
052ff461 | 13 | #include <linux/kernel_stat.h> |
e976a2b9 CB |
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> | |
5a0e3ad6 | 19 | #include <linux/slab.h> |
faeba830 | 20 | #include <linux/virtio_console.h> |
e976a2b9 CB |
21 | #include <linux/interrupt.h> |
22 | #include <linux/virtio_ring.h> | |
3a4c5d59 | 23 | #include <linux/export.h> |
17f34580 | 24 | #include <linux/pfn.h> |
e976a2b9 CB |
25 | #include <asm/io.h> |
26 | #include <asm/kvm_para.h> | |
27 | #include <asm/kvm_virtio.h> | |
28 | #include <asm/setup.h> | |
052ff461 | 29 | #include <asm/irq.h> |
e976a2b9 CB |
30 | |
31 | #define VIRTIO_SUBCODE_64 0x0D00 | |
32 | ||
33 | /* | |
34 | * The pointer to our (page) of device descriptions. | |
35 | */ | |
36 | static void *kvm_devices; | |
cefa33e2 | 37 | struct work_struct hotplug_work; |
e976a2b9 | 38 | |
e976a2b9 CB |
39 | struct 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 | */ | |
55 | static struct kvm_vqconfig *kvm_vq_config(const struct kvm_device_desc *desc) | |
56 | { | |
57 | return (struct kvm_vqconfig *)(desc + 1); | |
58 | } | |
59 | ||
60 | static u8 *kvm_vq_features(const struct kvm_device_desc *desc) | |
61 | { | |
62 | return (u8 *)(kvm_vq_config(desc) + desc->num_vq); | |
63 | } | |
64 | ||
65 | static 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 | */ | |
73 | static 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 | ||
5ca9fd54 HC |
81 | /* This gets the device's feature bits. */ |
82 | static u32 kvm_get_features(struct virtio_device *vdev) | |
e976a2b9 | 83 | { |
5ca9fd54 HC |
84 | unsigned int i; |
85 | u32 features = 0; | |
e976a2b9 | 86 | struct kvm_device_desc *desc = to_kvmdev(vdev)->desc; |
5ca9fd54 | 87 | u8 *in_features = kvm_vq_features(desc); |
e976a2b9 | 88 | |
5ca9fd54 HC |
89 | 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 | } | |
e976a2b9 | 94 | |
c624896e | 95 | static void kvm_finalize_features(struct virtio_device *vdev) |
5ca9fd54 | 96 | { |
c624896e | 97 | unsigned int i, bits; |
5ca9fd54 HC |
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; | |
e976a2b9 | 101 | |
e34f8725 RR |
102 | /* Give virtio_ring a chance to accept features. */ |
103 | vring_transport_features(vdev); | |
104 | ||
5ca9fd54 | 105 | memset(out_features, 0, desc->feature_len); |
c624896e RR |
106 | bits = min_t(unsigned, desc->feature_len, sizeof(vdev->features)) * 8; |
107 | for (i = 0; i < bits; i++) { | |
108 | if (test_bit(i, vdev->features)) | |
5ca9fd54 HC |
109 | out_features[i / 8] |= (1 << (i % 8)); |
110 | } | |
e976a2b9 CB |
111 | } |
112 | ||
113 | /* | |
114 | * Reading and writing elements in config space | |
115 | */ | |
116 | static void kvm_get(struct virtio_device *vdev, unsigned int offset, | |
117 | 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(buf, kvm_vq_configspace(desc) + offset, len); | |
123 | } | |
124 | ||
125 | static void kvm_set(struct virtio_device *vdev, unsigned int offset, | |
126 | const void *buf, unsigned len) | |
127 | { | |
128 | struct kvm_device_desc *desc = to_kvmdev(vdev)->desc; | |
129 | ||
130 | BUG_ON(offset + len > desc->config_len); | |
131 | memcpy(kvm_vq_configspace(desc) + offset, buf, len); | |
132 | } | |
133 | ||
134 | /* | |
135 | * The operations to get and set the status word just access | |
136 | * the status field of the device descriptor. set_status will also | |
137 | * make a hypercall to the host, to tell about status changes | |
138 | */ | |
139 | static u8 kvm_get_status(struct virtio_device *vdev) | |
140 | { | |
141 | return to_kvmdev(vdev)->desc->status; | |
142 | } | |
143 | ||
144 | static void kvm_set_status(struct virtio_device *vdev, u8 status) | |
145 | { | |
146 | BUG_ON(!status); | |
147 | to_kvmdev(vdev)->desc->status = status; | |
148 | kvm_hypercall1(KVM_S390_VIRTIO_SET_STATUS, | |
149 | (unsigned long) to_kvmdev(vdev)->desc); | |
150 | } | |
151 | ||
152 | /* | |
153 | * To reset the device, we use the KVM_VIRTIO_RESET hypercall, using the | |
154 | * descriptor address. The Host will zero the status and all the | |
155 | * features. | |
156 | */ | |
157 | static void kvm_reset(struct virtio_device *vdev) | |
158 | { | |
159 | kvm_hypercall1(KVM_S390_VIRTIO_RESET, | |
160 | (unsigned long) to_kvmdev(vdev)->desc); | |
161 | } | |
162 | ||
163 | /* | |
164 | * When the virtio_ring code wants to notify the Host, it calls us here and we | |
165 | * make a hypercall. We hand the address of the virtqueue so the Host | |
166 | * knows which virtqueue we're talking about. | |
167 | */ | |
168 | static void kvm_notify(struct virtqueue *vq) | |
169 | { | |
170 | struct kvm_vqconfig *config = vq->priv; | |
171 | ||
172 | kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, config->address); | |
173 | } | |
174 | ||
175 | /* | |
176 | * This routine finds the first virtqueue described in the configuration of | |
177 | * this device and sets it up. | |
178 | */ | |
179 | static struct virtqueue *kvm_find_vq(struct virtio_device *vdev, | |
9499f5e7 RR |
180 | unsigned index, |
181 | void (*callback)(struct virtqueue *vq), | |
182 | const char *name) | |
e976a2b9 CB |
183 | { |
184 | struct kvm_device *kdev = to_kvmdev(vdev); | |
185 | struct kvm_vqconfig *config; | |
186 | struct virtqueue *vq; | |
187 | int err; | |
188 | ||
189 | if (index >= kdev->desc->num_vq) | |
190 | return ERR_PTR(-ENOENT); | |
191 | ||
192 | config = kvm_vq_config(kdev->desc)+index; | |
193 | ||
17f34580 | 194 | err = vmem_add_mapping(config->address, |
db405988 RR |
195 | vring_size(config->num, |
196 | KVM_S390_VIRTIO_RING_ALIGN)); | |
17f34580 | 197 | if (err) |
e976a2b9 | 198 | goto out; |
e976a2b9 | 199 | |
87c7d57c RR |
200 | vq = vring_new_virtqueue(config->num, KVM_S390_VIRTIO_RING_ALIGN, |
201 | vdev, (void *) config->address, | |
9499f5e7 | 202 | kvm_notify, callback, name); |
e976a2b9 CB |
203 | if (!vq) { |
204 | err = -ENOMEM; | |
205 | goto unmap; | |
206 | } | |
207 | ||
208 | /* | |
209 | * register a callback token | |
210 | * The host will sent this via the external interrupt parameter | |
211 | */ | |
212 | config->token = (u64) vq; | |
213 | ||
214 | vq->priv = config; | |
215 | return vq; | |
216 | unmap: | |
17f34580 | 217 | vmem_remove_mapping(config->address, |
db405988 RR |
218 | vring_size(config->num, |
219 | KVM_S390_VIRTIO_RING_ALIGN)); | |
e976a2b9 CB |
220 | out: |
221 | return ERR_PTR(err); | |
222 | } | |
223 | ||
224 | static void kvm_del_vq(struct virtqueue *vq) | |
225 | { | |
226 | struct kvm_vqconfig *config = vq->priv; | |
227 | ||
228 | vring_del_virtqueue(vq); | |
17f34580 | 229 | vmem_remove_mapping(config->address, |
db405988 RR |
230 | vring_size(config->num, |
231 | KVM_S390_VIRTIO_RING_ALIGN)); | |
e976a2b9 CB |
232 | } |
233 | ||
d2a7ddda MT |
234 | static void kvm_del_vqs(struct virtio_device *vdev) |
235 | { | |
236 | struct virtqueue *vq, *n; | |
237 | ||
238 | list_for_each_entry_safe(vq, n, &vdev->vqs, list) | |
239 | kvm_del_vq(vq); | |
240 | } | |
241 | ||
242 | static int kvm_find_vqs(struct virtio_device *vdev, unsigned nvqs, | |
243 | struct virtqueue *vqs[], | |
244 | vq_callback_t *callbacks[], | |
245 | const char *names[]) | |
246 | { | |
247 | struct kvm_device *kdev = to_kvmdev(vdev); | |
248 | int i; | |
249 | ||
250 | /* We must have this many virtqueues. */ | |
251 | if (nvqs > kdev->desc->num_vq) | |
252 | return -ENOENT; | |
253 | ||
254 | for (i = 0; i < nvqs; ++i) { | |
255 | vqs[i] = kvm_find_vq(vdev, i, callbacks[i], names[i]); | |
256 | if (IS_ERR(vqs[i])) | |
257 | goto error; | |
258 | } | |
259 | return 0; | |
260 | ||
261 | error: | |
262 | kvm_del_vqs(vdev); | |
263 | return PTR_ERR(vqs[i]); | |
264 | } | |
265 | ||
e976a2b9 CB |
266 | /* |
267 | * The config ops structure as defined by virtio config | |
268 | */ | |
269 | static struct virtio_config_ops kvm_vq_configspace_ops = { | |
5ca9fd54 | 270 | .get_features = kvm_get_features, |
c624896e | 271 | .finalize_features = kvm_finalize_features, |
e976a2b9 CB |
272 | .get = kvm_get, |
273 | .set = kvm_set, | |
274 | .get_status = kvm_get_status, | |
275 | .set_status = kvm_set_status, | |
276 | .reset = kvm_reset, | |
d2a7ddda MT |
277 | .find_vqs = kvm_find_vqs, |
278 | .del_vqs = kvm_del_vqs, | |
e976a2b9 CB |
279 | }; |
280 | ||
281 | /* | |
282 | * The root device for the kvm virtio devices. | |
283 | * This makes them appear as /sys/devices/kvm_s390/0,1,2 not /sys/devices/0,1,2. | |
284 | */ | |
37f1c012 | 285 | static struct device *kvm_root; |
e976a2b9 CB |
286 | |
287 | /* | |
288 | * adds a new device and register it with virtio | |
289 | * appropriate drivers are loaded by the device model | |
290 | */ | |
b769f579 | 291 | static void add_kvm_device(struct kvm_device_desc *d, unsigned int offset) |
e976a2b9 CB |
292 | { |
293 | struct kvm_device *kdev; | |
294 | ||
295 | kdev = kzalloc(sizeof(*kdev), GFP_KERNEL); | |
296 | if (!kdev) { | |
b769f579 RR |
297 | printk(KERN_EMERG "Cannot allocate kvm dev %u type %u\n", |
298 | offset, d->type); | |
e976a2b9 CB |
299 | return; |
300 | } | |
301 | ||
37f1c012 | 302 | kdev->vdev.dev.parent = kvm_root; |
e976a2b9 CB |
303 | kdev->vdev.id.device = d->type; |
304 | kdev->vdev.config = &kvm_vq_configspace_ops; | |
305 | kdev->desc = d; | |
306 | ||
307 | if (register_virtio_device(&kdev->vdev) != 0) { | |
b769f579 RR |
308 | printk(KERN_ERR "Failed to register kvm device %u type %u\n", |
309 | offset, d->type); | |
e976a2b9 CB |
310 | kfree(kdev); |
311 | } | |
312 | } | |
313 | ||
314 | /* | |
315 | * scan_devices() simply iterates through the device page. | |
316 | * The type 0 is reserved to mean "end of devices". | |
317 | */ | |
318 | static void scan_devices(void) | |
319 | { | |
320 | unsigned int i; | |
321 | struct kvm_device_desc *d; | |
322 | ||
323 | for (i = 0; i < PAGE_SIZE; i += desc_size(d)) { | |
324 | d = kvm_devices + i; | |
325 | ||
326 | if (d->type == 0) | |
327 | break; | |
328 | ||
b769f579 | 329 | add_kvm_device(d, i); |
e976a2b9 CB |
330 | } |
331 | } | |
332 | ||
cefa33e2 AG |
333 | /* |
334 | * match for a kvm device with a specific desc pointer | |
335 | */ | |
336 | static int match_desc(struct device *dev, void *data) | |
337 | { | |
338 | if ((ulong)to_kvmdev(dev_to_virtio(dev))->desc == (ulong)data) | |
339 | return 1; | |
340 | ||
341 | return 0; | |
342 | } | |
343 | ||
344 | /* | |
345 | * hotplug_device tries to find changes in the device page. | |
346 | */ | |
347 | static void hotplug_devices(struct work_struct *dummy) | |
348 | { | |
349 | unsigned int i; | |
350 | struct kvm_device_desc *d; | |
351 | struct device *dev; | |
352 | ||
353 | for (i = 0; i < PAGE_SIZE; i += desc_size(d)) { | |
354 | d = kvm_devices + i; | |
355 | ||
356 | /* end of list */ | |
357 | if (d->type == 0) | |
358 | break; | |
359 | ||
360 | /* device already exists */ | |
361 | dev = device_find_child(kvm_root, d, match_desc); | |
362 | if (dev) { | |
363 | /* XXX check for hotplug remove */ | |
364 | put_device(dev); | |
365 | continue; | |
366 | } | |
367 | ||
368 | /* new device */ | |
369 | printk(KERN_INFO "Adding new virtio device %p\n", d); | |
370 | add_kvm_device(d, i); | |
371 | } | |
372 | } | |
373 | ||
e976a2b9 CB |
374 | /* |
375 | * we emulate the request_irq behaviour on top of s390 extints | |
376 | */ | |
f6649a7e MS |
377 | static void kvm_extint_handler(unsigned int ext_int_code, |
378 | unsigned int param32, unsigned long param64) | |
e976a2b9 | 379 | { |
be3c5832 CB |
380 | struct virtqueue *vq; |
381 | u16 subcode; | |
fc678d67 | 382 | u32 param; |
e976a2b9 | 383 | |
f6649a7e | 384 | subcode = ext_int_code >> 16; |
e976a2b9 CB |
385 | if ((subcode & 0xff00) != VIRTIO_SUBCODE_64) |
386 | return; | |
a9851832 | 387 | kstat_cpu(smp_processor_id()).irqs[EXTINT_VRT]++; |
e976a2b9 | 388 | |
be3c5832 | 389 | /* The LSB might be overloaded, we have to mask it */ |
f6649a7e | 390 | vq = (struct virtqueue *)(param64 & ~1UL); |
be3c5832 | 391 | |
fc678d67 | 392 | /* We use ext_params to decide what this interrupt means */ |
f6649a7e | 393 | param = param32 & VIRTIO_PARAM_MASK; |
be3c5832 | 394 | |
fc678d67 AG |
395 | switch (param) { |
396 | case VIRTIO_PARAM_CONFIG_CHANGED: | |
397 | { | |
be3c5832 CB |
398 | struct virtio_driver *drv; |
399 | drv = container_of(vq->vdev->dev.driver, | |
400 | struct virtio_driver, driver); | |
401 | if (drv->config_changed) | |
402 | drv->config_changed(vq->vdev); | |
fc678d67 AG |
403 | |
404 | break; | |
405 | } | |
cefa33e2 AG |
406 | case VIRTIO_PARAM_DEV_ADD: |
407 | schedule_work(&hotplug_work); | |
408 | break; | |
fc678d67 AG |
409 | case VIRTIO_PARAM_VRING_INTERRUPT: |
410 | default: | |
be3c5832 | 411 | vring_interrupt(0, vq); |
fc678d67 AG |
412 | break; |
413 | } | |
e976a2b9 CB |
414 | } |
415 | ||
416 | /* | |
417 | * Init function for virtio | |
418 | * devices are in a single page above top of "normal" mem | |
419 | */ | |
420 | static int __init kvm_devices_init(void) | |
421 | { | |
422 | int rc; | |
423 | ||
424 | if (!MACHINE_IS_KVM) | |
425 | return -ENODEV; | |
426 | ||
035da16f | 427 | kvm_root = root_device_register("kvm_s390"); |
37f1c012 CH |
428 | if (IS_ERR(kvm_root)) { |
429 | rc = PTR_ERR(kvm_root); | |
e976a2b9 CB |
430 | printk(KERN_ERR "Could not register kvm_s390 root device"); |
431 | return rc; | |
432 | } | |
433 | ||
cc835f78 | 434 | rc = vmem_add_mapping(real_memory_size, PAGE_SIZE); |
17f34580 | 435 | if (rc) { |
035da16f | 436 | root_device_unregister(kvm_root); |
17f34580 | 437 | return rc; |
e976a2b9 CB |
438 | } |
439 | ||
cc835f78 | 440 | kvm_devices = (void *) real_memory_size; |
e976a2b9 | 441 | |
cefa33e2 AG |
442 | INIT_WORK(&hotplug_work, hotplug_devices); |
443 | ||
df7997ab | 444 | service_subclass_irq_register(); |
e976a2b9 CB |
445 | register_external_interrupt(0x2603, kvm_extint_handler); |
446 | ||
447 | scan_devices(); | |
448 | return 0; | |
449 | } | |
450 | ||
faeba830 CB |
451 | /* code for early console output with virtio_console */ |
452 | static __init int early_put_chars(u32 vtermno, const char *buf, int count) | |
453 | { | |
454 | char scratch[17]; | |
455 | unsigned int len = count; | |
456 | ||
457 | if (len > sizeof(scratch) - 1) | |
458 | len = sizeof(scratch) - 1; | |
459 | scratch[len] = '\0'; | |
460 | memcpy(scratch, buf, len); | |
461 | kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, __pa(scratch)); | |
462 | return len; | |
463 | } | |
464 | ||
c4de0c1a | 465 | static int __init s390_virtio_console_init(void) |
faeba830 | 466 | { |
c4de0c1a HB |
467 | if (!MACHINE_IS_KVM) |
468 | return -ENODEV; | |
469 | return virtio_cons_early_init(early_put_chars); | |
faeba830 | 470 | } |
c4de0c1a HB |
471 | console_initcall(s390_virtio_console_init); |
472 | ||
faeba830 | 473 | |
e976a2b9 CB |
474 | /* |
475 | * We do this after core stuff, but before the drivers. | |
476 | */ | |
477 | postcore_initcall(kvm_devices_init); |