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