kvm_virtio.c 11.1 KB
Newer Older
1
/*
2
 * virtio for kvm on s390
3 4 5 6 7 8 9 10 11 12
 *
 * Copyright IBM Corp. 2008
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License (version 2 only)
 * as published by the Free Software Foundation.
 *
 *    Author(s): Christian Borntraeger <borntraeger@de.ibm.com>
 */

13
#include <linux/kernel_stat.h>
14 15 16 17 18
#include <linux/init.h>
#include <linux/bootmem.h>
#include <linux/err.h>
#include <linux/virtio.h>
#include <linux/virtio_config.h>
19
#include <linux/slab.h>
20
#include <linux/virtio_console.h>
21 22
#include <linux/interrupt.h>
#include <linux/virtio_ring.h>
23
#include <linux/export.h>
24
#include <linux/pfn.h>
25 26 27
#include <asm/io.h>
#include <asm/kvm_para.h>
#include <asm/kvm_virtio.h>
28
#include <asm/sclp.h>
29
#include <asm/setup.h>
30
#include <asm/irq.h>
31 32 33 34 35 36 37

#define VIRTIO_SUBCODE_64 0x0D00

/*
 * The pointer to our (page) of device descriptions.
 */
static void *kvm_devices;
38
static struct work_struct hotplug_work;
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

struct kvm_device {
	struct virtio_device vdev;
	struct kvm_device_desc *desc;
};

#define to_kvmdev(vd) container_of(vd, struct kvm_device, vdev)

/*
 * memory layout:
 * - kvm_device_descriptor
 *        struct kvm_device_desc
 * - configuration
 *        struct kvm_vqconfig
 * - feature bits
 * - config space
 */
static struct kvm_vqconfig *kvm_vq_config(const struct kvm_device_desc *desc)
{
	return (struct kvm_vqconfig *)(desc + 1);
}

static u8 *kvm_vq_features(const struct kvm_device_desc *desc)
{
	return (u8 *)(kvm_vq_config(desc) + desc->num_vq);
}

static u8 *kvm_vq_configspace(const struct kvm_device_desc *desc)
{
	return kvm_vq_features(desc) + desc->feature_len * 2;
}

/*
 * The total size of the config page used by this device (incl. desc)
 */
static unsigned desc_size(const struct kvm_device_desc *desc)
{
	return sizeof(*desc)
		+ desc->num_vq * sizeof(struct kvm_vqconfig)
		+ desc->feature_len * 2
		+ desc->config_len;
}

82 83
/* This gets the device's feature bits. */
static u32 kvm_get_features(struct virtio_device *vdev)
84
{
85 86
	unsigned int i;
	u32 features = 0;
87
	struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
88
	u8 *in_features = kvm_vq_features(desc);
89

90 91 92 93 94
	for (i = 0; i < min(desc->feature_len * 8, 32); i++)
		if (in_features[i / 8] & (1 << (i % 8)))
			features |= (1 << i);
	return features;
}
95

96
static void kvm_finalize_features(struct virtio_device *vdev)
97
{
98
	unsigned int i, bits;
99 100 101
	struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
	/* Second half of bitmap is features we accept. */
	u8 *out_features = kvm_vq_features(desc) + desc->feature_len;
102

103 104 105
	/* Give virtio_ring a chance to accept features. */
	vring_transport_features(vdev);

106
	memset(out_features, 0, desc->feature_len);
107 108 109
	bits = min_t(unsigned, desc->feature_len, sizeof(vdev->features)) * 8;
	for (i = 0; i < bits; i++) {
		if (test_bit(i, vdev->features))
110 111
			out_features[i / 8] |= (1 << (i % 8));
	}
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
}

/*
 * Reading and writing elements in config space
 */
static void kvm_get(struct virtio_device *vdev, unsigned int offset,
		   void *buf, unsigned len)
{
	struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;

	BUG_ON(offset + len > desc->config_len);
	memcpy(buf, kvm_vq_configspace(desc) + offset, len);
}

static void kvm_set(struct virtio_device *vdev, unsigned int offset,
		   const void *buf, unsigned len)
{
	struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;

	BUG_ON(offset + len > desc->config_len);
	memcpy(kvm_vq_configspace(desc) + offset, buf, len);
}

/*
 * The operations to get and set the status word just access
 * the status field of the device descriptor. set_status will also
 * make a hypercall to the host, to tell about status changes
 */
static u8 kvm_get_status(struct virtio_device *vdev)
{
	return to_kvmdev(vdev)->desc->status;
}

static void kvm_set_status(struct virtio_device *vdev, u8 status)
{
	BUG_ON(!status);
	to_kvmdev(vdev)->desc->status = status;
	kvm_hypercall1(KVM_S390_VIRTIO_SET_STATUS,
		       (unsigned long) to_kvmdev(vdev)->desc);
}

/*
 * To reset the device, we use the KVM_VIRTIO_RESET hypercall, using the
 * descriptor address. The Host will zero the status and all the
 * features.
 */
static void kvm_reset(struct virtio_device *vdev)
{
	kvm_hypercall1(KVM_S390_VIRTIO_RESET,
		       (unsigned long) to_kvmdev(vdev)->desc);
}

/*
 * When the virtio_ring code wants to notify the Host, it calls us here and we
 * make a hypercall.  We hand the address  of the virtqueue so the Host
 * knows which virtqueue we're talking about.
 */
static void kvm_notify(struct virtqueue *vq)
{
	struct kvm_vqconfig *config = vq->priv;

	kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, config->address);
}

/*
 * This routine finds the first virtqueue described in the configuration of
 * this device and sets it up.
 */
static struct virtqueue *kvm_find_vq(struct virtio_device *vdev,
181 182 183
				     unsigned index,
				     void (*callback)(struct virtqueue *vq),
				     const char *name)
184 185 186 187 188 189 190 191 192
{
	struct kvm_device *kdev = to_kvmdev(vdev);
	struct kvm_vqconfig *config;
	struct virtqueue *vq;
	int err;

	if (index >= kdev->desc->num_vq)
		return ERR_PTR(-ENOENT);

193 194 195
	if (!name)
		return NULL;

196 197
	config = kvm_vq_config(kdev->desc)+index;

198
	err = vmem_add_mapping(config->address,
199 200
			       vring_size(config->num,
					  KVM_S390_VIRTIO_RING_ALIGN));
201
	if (err)
202 203
		goto out;

204
	vq = vring_new_virtqueue(index, config->num, KVM_S390_VIRTIO_RING_ALIGN,
205
				 vdev, true, (void *) config->address,
206
				 kvm_notify, callback, name);
207 208 209 210 211 212 213 214 215 216 217 218 219 220
	if (!vq) {
		err = -ENOMEM;
		goto unmap;
	}

	/*
	 * register a callback token
	 * The host will sent this via the external interrupt parameter
	 */
	config->token = (u64) vq;

	vq->priv = config;
	return vq;
unmap:
221
	vmem_remove_mapping(config->address,
222 223
			    vring_size(config->num,
				       KVM_S390_VIRTIO_RING_ALIGN));
224 225 226 227 228 229 230 231 232
out:
	return ERR_PTR(err);
}

static void kvm_del_vq(struct virtqueue *vq)
{
	struct kvm_vqconfig *config = vq->priv;

	vring_del_virtqueue(vq);
233
	vmem_remove_mapping(config->address,
234 235
			    vring_size(config->num,
				       KVM_S390_VIRTIO_RING_ALIGN));
236 237
}

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
static void kvm_del_vqs(struct virtio_device *vdev)
{
	struct virtqueue *vq, *n;

	list_for_each_entry_safe(vq, n, &vdev->vqs, list)
		kvm_del_vq(vq);
}

static int kvm_find_vqs(struct virtio_device *vdev, unsigned nvqs,
			struct virtqueue *vqs[],
			vq_callback_t *callbacks[],
			const char *names[])
{
	struct kvm_device *kdev = to_kvmdev(vdev);
	int i;

	/* We must have this many virtqueues. */
	if (nvqs > kdev->desc->num_vq)
		return -ENOENT;

	for (i = 0; i < nvqs; ++i) {
		vqs[i] = kvm_find_vq(vdev, i, callbacks[i], names[i]);
		if (IS_ERR(vqs[i]))
			goto error;
	}
	return 0;

error:
	kvm_del_vqs(vdev);
	return PTR_ERR(vqs[i]);
}

270 271 272 273 274
static const char *kvm_bus_name(struct virtio_device *vdev)
{
	return "";
}

275 276 277 278
/*
 * The config ops structure as defined by virtio config
 */
static struct virtio_config_ops kvm_vq_configspace_ops = {
279
	.get_features = kvm_get_features,
280
	.finalize_features = kvm_finalize_features,
281 282 283 284 285
	.get = kvm_get,
	.set = kvm_set,
	.get_status = kvm_get_status,
	.set_status = kvm_set_status,
	.reset = kvm_reset,
286 287
	.find_vqs = kvm_find_vqs,
	.del_vqs = kvm_del_vqs,
288
	.bus_name = kvm_bus_name,
289 290 291 292 293 294
};

/*
 * The root device for the kvm virtio devices.
 * This makes them appear as /sys/devices/kvm_s390/0,1,2 not /sys/devices/0,1,2.
 */
295
static struct device *kvm_root;
296 297 298 299 300

/*
 * adds a new device and register it with virtio
 * appropriate drivers are loaded by the device model
 */
301
static void add_kvm_device(struct kvm_device_desc *d, unsigned int offset)
302 303 304 305 306
{
	struct kvm_device *kdev;

	kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
	if (!kdev) {
307 308
		printk(KERN_EMERG "Cannot allocate kvm dev %u type %u\n",
		       offset, d->type);
309 310 311
		return;
	}

312
	kdev->vdev.dev.parent = kvm_root;
313 314 315 316 317
	kdev->vdev.id.device = d->type;
	kdev->vdev.config = &kvm_vq_configspace_ops;
	kdev->desc = d;

	if (register_virtio_device(&kdev->vdev) != 0) {
318 319
		printk(KERN_ERR "Failed to register kvm device %u type %u\n",
		       offset, d->type);
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
		kfree(kdev);
	}
}

/*
 * scan_devices() simply iterates through the device page.
 * The type 0 is reserved to mean "end of devices".
 */
static void scan_devices(void)
{
	unsigned int i;
	struct kvm_device_desc *d;

	for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
		d = kvm_devices + i;

		if (d->type == 0)
			break;

339
		add_kvm_device(d, i);
340 341 342
	}
}

343 344 345 346 347
/*
 * match for a kvm device with a specific desc pointer
 */
static int match_desc(struct device *dev, void *data)
{
348 349
	struct virtio_device *vdev = dev_to_virtio(dev);
	struct kvm_device *kdev = to_kvmdev(vdev);
350

351
	return kdev->desc == data;
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
}

/*
 * hotplug_device tries to find changes in the device page.
 */
static void hotplug_devices(struct work_struct *dummy)
{
	unsigned int i;
	struct kvm_device_desc *d;
	struct device *dev;

	for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
		d = kvm_devices + i;

		/* end of list */
		if (d->type == 0)
			break;

		/* device already exists */
		dev = device_find_child(kvm_root, d, match_desc);
		if (dev) {
			/* XXX check for hotplug remove */
			put_device(dev);
			continue;
		}

		/* new device */
		printk(KERN_INFO "Adding new virtio device %p\n", d);
		add_kvm_device(d, i);
	}
}

384 385 386
/*
 * we emulate the request_irq behaviour on top of s390 extints
 */
387
static void kvm_extint_handler(struct ext_code ext_code,
388
			       unsigned int param32, unsigned long param64)
389
{
390
	struct virtqueue *vq;
391
	u32 param;
392

393
	if ((ext_code.subcode & 0xff00) != VIRTIO_SUBCODE_64)
394
		return;
395
	inc_irq_stat(IRQEXT_VRT);
396

397
	/* The LSB might be overloaded, we have to mask it */
398
	vq = (struct virtqueue *)(param64 & ~1UL);
399

400
	/* We use ext_params to decide what this interrupt means */
401
	param = param32 & VIRTIO_PARAM_MASK;
402

403 404 405
	switch (param) {
	case VIRTIO_PARAM_CONFIG_CHANGED:
	{
406 407 408 409 410
		struct virtio_driver *drv;
		drv = container_of(vq->vdev->dev.driver,
				   struct virtio_driver, driver);
		if (drv->config_changed)
			drv->config_changed(vq->vdev);
411 412 413

		break;
	}
414 415 416
	case VIRTIO_PARAM_DEV_ADD:
		schedule_work(&hotplug_work);
		break;
417 418
	case VIRTIO_PARAM_VRING_INTERRUPT:
	default:
419
		vring_interrupt(0, vq);
420 421
		break;
	}
422 423 424 425 426 427 428 429 430 431 432 433 434
}

/*
 * Init function for virtio
 * devices are in a single page above top of "normal" mem
 */
static int __init kvm_devices_init(void)
{
	int rc;

	if (!MACHINE_IS_KVM)
		return -ENODEV;

M
Mark McLoughlin 已提交
435
	kvm_root = root_device_register("kvm_s390");
436 437
	if (IS_ERR(kvm_root)) {
		rc = PTR_ERR(kvm_root);
438 439 440 441
		printk(KERN_ERR "Could not register kvm_s390 root device");
		return rc;
	}

442
	rc = vmem_add_mapping(real_memory_size, PAGE_SIZE);
443
	if (rc) {
M
Mark McLoughlin 已提交
444
		root_device_unregister(kvm_root);
445
		return rc;
446 447
	}

448
	kvm_devices = (void *) real_memory_size;
449

450 451
	INIT_WORK(&hotplug_work, hotplug_devices);

452
	service_subclass_irq_register();
453 454 455 456 457 458
	register_external_interrupt(0x2603, kvm_extint_handler);

	scan_devices();
	return 0;
}

459 460 461 462 463 464 465 466 467 468 469 470 471 472
/* code for early console output with virtio_console */
static __init int early_put_chars(u32 vtermno, const char *buf, int count)
{
	char scratch[17];
	unsigned int len = count;

	if (len > sizeof(scratch) - 1)
		len = sizeof(scratch) - 1;
	scratch[len] = '\0';
	memcpy(scratch, buf, len);
	kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, __pa(scratch));
	return len;
}

473
static int __init s390_virtio_console_init(void)
474
{
475
	if (sclp_has_vt220() || sclp_has_linemode())
476 477
		return -ENODEV;
	return virtio_cons_early_init(early_put_chars);
478
}
479 480
console_initcall(s390_virtio_console_init);

481

482 483 484 485
/*
 * We do this after core stuff, but before the drivers.
 */
postcore_initcall(kvm_devices_init);