Release 4.7 drivers/s390/virtio/kvm_virtio.c
/*
* virtio for kvm on s390
*
* 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>
*/
#include <linux/kernel_stat.h>
#include <linux/init.h>
#include <linux/bootmem.h>
#include <linux/err.h>
#include <linux/virtio.h>
#include <linux/virtio_config.h>
#include <linux/slab.h>
#include <linux/virtio_console.h>
#include <linux/interrupt.h>
#include <linux/virtio_ring.h>
#include <linux/export.h>
#include <linux/pfn.h>
#include <asm/io.h>
#include <asm/kvm_para.h>
#include <asm/kvm_virtio.h>
#include <asm/sclp.h>
#include <asm/setup.h>
#include <asm/irq.h>
#define VIRTIO_SUBCODE_64 0x0D00
/*
* The pointer to our (page) of device descriptions.
*/
static void *kvm_devices;
static struct work_struct hotplug_work;
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);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
christian borntraeger | christian borntraeger | 26 | 100.00% | 1 | 100.00% |
| Total | 26 | 100.00% | 1 | 100.00% |
static u8 *kvm_vq_features(const struct kvm_device_desc *desc)
{
return (u8 *)(kvm_vq_config(desc) + desc->num_vq);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
christian borntraeger | christian borntraeger | 29 | 100.00% | 1 | 100.00% |
| Total | 29 | 100.00% | 1 | 100.00% |
static u8 *kvm_vq_configspace(const struct kvm_device_desc *desc)
{
return kvm_vq_features(desc) + desc->feature_len * 2;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
christian borntraeger | christian borntraeger | 25 | 100.00% | 1 | 100.00% |
| Total | 25 | 100.00% | 1 | 100.00% |
/*
* 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;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
christian borntraeger | christian borntraeger | 39 | 100.00% | 1 | 100.00% |
| Total | 39 | 100.00% | 1 | 100.00% |
/* This gets the device's feature bits. */
static u64 kvm_get_features(struct virtio_device *vdev)
{
unsigned int i;
u32 features = 0;
struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
u8 *in_features = kvm_vq_features(desc);
for (i = 0; i < min(desc->feature_len * 8, 32); i++)
if (in_features[i / 8] & (1 << (i % 8)))
features |= (1 << i);
return features;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
heiko carstens | heiko carstens | 47 | 50.54% | 1 | 33.33% |
christian borntraeger | christian borntraeger | 45 | 48.39% | 1 | 33.33% |
michael s. tsirkin | michael s. tsirkin | 1 | 1.08% | 1 | 33.33% |
| Total | 93 | 100.00% | 3 | 100.00% |
static int kvm_finalize_features(struct virtio_device *vdev)
{
unsigned int i, bits;
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;
/* Give virtio_ring a chance to accept features. */
vring_transport_features(vdev);
/* Make sure we don't have any features > 32 bits! */
BUG_ON((u32)vdev->features != vdev->features);
memset(out_features, 0, desc->feature_len);
bits = min_t(unsigned, desc->feature_len, sizeof(vdev->features)) * 8;
for (i = 0; i < bits; i++) {
if (__virtio_test_bit(vdev, i))
out_features[i / 8] |= (1 << (i % 8));
}
return 0;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
heiko carstens | heiko carstens | 67 | 48.20% | 1 | 14.29% |
rusty russell | rusty russell | 33 | 23.74% | 2 | 28.57% |
michael s. tsirkin | michael s. tsirkin | 22 | 15.83% | 3 | 42.86% |
christian borntraeger | christian borntraeger | 17 | 12.23% | 1 | 14.29% |
| Total | 139 | 100.00% | 7 | 100.00% |
/*
* 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);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
christian borntraeger | christian borntraeger | 59 | 100.00% | 1 | 100.00% |
| Total | 59 | 100.00% | 1 | 100.00% |
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);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
christian borntraeger | christian borntraeger | 60 | 100.00% | 1 | 100.00% |
| Total | 60 | 100.00% | 1 | 100.00% |
/*
* 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;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
christian borntraeger | christian borntraeger | 21 | 100.00% | 1 | 100.00% |
| Total | 21 | 100.00% | 1 | 100.00% |
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);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
christian borntraeger | christian borntraeger | 47 | 100.00% | 1 | 100.00% |
| Total | 47 | 100.00% | 1 | 100.00% |
/*
* 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);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
christian borntraeger | christian borntraeger | 27 | 100.00% | 1 | 100.00% |
| Total | 27 | 100.00% | 1 | 100.00% |
/*
* 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 bool kvm_notify(struct virtqueue *vq)
{
long rc;
struct kvm_vqconfig *config = vq->priv;
rc = kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, config->address);
if (rc < 0)
return false;
return true;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
christian borntraeger | christian borntraeger | 28 | 60.87% | 1 | 50.00% |
heinz graalfs | heinz graalfs | 18 | 39.13% | 1 | 50.00% |
| Total | 46 | 100.00% | 2 | 100.00% |
/*
* 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,
unsigned index,
void (*callback)(struct virtqueue *vq),
const char *name)
{
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);
if (!name)
return NULL;
config = kvm_vq_config(kdev->desc)+index;
err = vmem_add_mapping(config->address,
vring_size(config->num,
KVM_S390_VIRTIO_RING_ALIGN));
if (err)
goto out;
vq = vring_new_virtqueue(index, config->num, KVM_S390_VIRTIO_RING_ALIGN,
vdev, true, (void *) config->address,
kvm_notify, callback, name);
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:
vmem_remove_mapping(config->address,
vring_size(config->num,
KVM_S390_VIRTIO_RING_ALIGN));
out:
return ERR_PTR(err);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
christian borntraeger | christian borntraeger | 177 | 85.10% | 1 | 12.50% |
rusty russell | rusty russell | 13 | 6.25% | 4 | 50.00% |
heiko carstens | heiko carstens | 8 | 3.85% | 1 | 12.50% |
michael s. tsirkin | michael s. tsirkin | 8 | 3.85% | 1 | 12.50% |
jason wang | jason wang | 2 | 0.96% | 1 | 12.50% |
| Total | 208 | 100.00% | 8 | 100.00% |
static void kvm_del_vq(struct virtqueue *vq)
{
struct kvm_vqconfig *config = vq->priv;
vring_del_virtqueue(vq);
vmem_remove_mapping(config->address,
vring_size(config->num,
KVM_S390_VIRTIO_RING_ALIGN));
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
christian borntraeger | christian borntraeger | 39 | 95.12% | 1 | 33.33% |
rusty russell | rusty russell | 1 | 2.44% | 1 | 33.33% |
heiko carstens | heiko carstens | 1 | 2.44% | 1 | 33.33% |
| Total | 41 | 100.00% | 3 | 100.00% |
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);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
michael s. tsirkin | michael s. tsirkin | 34 | 100.00% | 1 | 100.00% |
| Total | 34 | 100.00% | 1 | 100.00% |
static int kvm_find_vqs(struct virtio_device *vdev, unsigned nvqs,
struct virtqueue *vqs[],
vq_callback_t *callbacks[],
const char * const 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]);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
michael s. tsirkin | michael s. tsirkin | 128 | 99.22% | 1 | 50.00% |
stefan hajnoczi | stefan hajnoczi | 1 | 0.78% | 1 | 50.00% |
| Total | 129 | 100.00% | 2 | 100.00% |
static const char *kvm_bus_name(struct virtio_device *vdev)
{
return "";
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
rick jones | rick jones | 16 | 100.00% | 1 | 100.00% |
| Total | 16 | 100.00% | 1 | 100.00% |
/*
* The config ops structure as defined by virtio config
*/
static const struct virtio_config_ops kvm_vq_configspace_ops = {
.get_features = kvm_get_features,
.finalize_features = kvm_finalize_features,
.get = kvm_get,
.set = kvm_set,
.get_status = kvm_get_status,
.set_status = kvm_set_status,
.reset = kvm_reset,
.find_vqs = kvm_find_vqs,
.del_vqs = kvm_del_vqs,
.bus_name = kvm_bus_name,
};
/*
* 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.
*/
static struct device *kvm_root;
/*
* adds a new device and register it with virtio
* appropriate drivers are loaded by the device model
*/
static void add_kvm_device(struct kvm_device_desc *d, unsigned int offset)
{
struct kvm_device *kdev;
kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
if (!kdev) {
printk(KERN_EMERG "Cannot allocate kvm dev %u type %u\n",
offset, d->type);
return;
}
kdev->vdev.dev.parent = kvm_root;
kdev->vdev.id.device = d->type;
kdev->vdev.config = &kvm_vq_configspace_ops;
kdev->desc = d;
if (register_virtio_device(&kdev->vdev) != 0) {
printk(KERN_ERR "Failed to register kvm device %u type %u\n",
offset, d->type);
kfree(kdev);
}
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
christian borntraeger | christian borntraeger | 106 | 87.60% | 1 | 50.00% |
rusty russell | rusty russell | 15 | 12.40% | 1 | 50.00% |
| Total | 121 | 100.00% | 2 | 100.00% |
/*
* 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;
add_kvm_device(d, i);
}
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
christian borntraeger | christian borntraeger | 56 | 96.55% | 1 | 50.00% |
rusty russell | rusty russell | 2 | 3.45% | 1 | 50.00% |
| Total | 58 | 100.00% | 2 | 100.00% |
/*
* match for a kvm device with a specific desc pointer
*/
static int match_desc(struct device *dev, void *data)
{
struct virtio_device *vdev = dev_to_virtio(dev);
struct kvm_device *kdev = to_kvmdev(vdev);
return kdev->desc == data;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
alexander graf | alexander graf | 25 | 59.52% | 1 | 50.00% |
martin schwidefsky | martin schwidefsky | 17 | 40.48% | 1 | 50.00% |
| Total | 42 | 100.00% | 2 | 100.00% |
/*
* 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);
}
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
alexander graf | alexander graf | 101 | 100.00% | 1 | 100.00% |
| Total | 101 | 100.00% | 1 | 100.00% |
/*
* we emulate the request_irq behaviour on top of s390 extints
*/
static void kvm_extint_handler(struct ext_code ext_code,
unsigned int param32, unsigned long param64)
{
struct virtqueue *vq;
u32 param;
if ((ext_code.subcode & 0xff00) != VIRTIO_SUBCODE_64)
return;
inc_irq_stat(IRQEXT_VRT);
/* The LSB might be overloaded, we have to mask it */
vq = (struct virtqueue *)(param64 & ~1UL);
/* We use ext_params to decide what this interrupt means */
param = param32 & VIRTIO_PARAM_MASK;
switch (param) {
case VIRTIO_PARAM_CONFIG_CHANGED:
virtio_config_changed(vq->vdev);
break;
case VIRTIO_PARAM_DEV_ADD:
schedule_work(&hotplug_work);
break;
case VIRTIO_PARAM_VRING_INTERRUPT:
default:
vring_interrupt(0, vq);
break;
}
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
christian borntraeger | christian borntraeger | 57 | 54.29% | 2 | 22.22% |
alexander graf | alexander graf | 27 | 25.71% | 2 | 22.22% |
martin schwidefsky | martin schwidefsky | 10 | 9.52% | 1 | 11.11% |
heiko carstens | heiko carstens | 10 | 9.52% | 3 | 33.33% |
michael s. tsirkin | michael s. tsirkin | 1 | 0.95% | 1 | 11.11% |
| Total | 105 | 100.00% | 9 | 100.00% |
/*
* For s390-virtio, we expect a page above main storage containing
* the virtio configuration. Try to actually load from this area
* in order to figure out if the host provides this page.
*/
static int __init test_devices_support(unsigned long addr)
{
int ret = -EIO;
asm volatile(
"0: lura 0,%1\n"
"1: xgr %0,%0\n"
"2:\n"
EX_TABLE(0b,2b)
EX_TABLE(1b,2b)
: "+d" (ret)
: "a" (addr)
: "0", "cc");
return ret;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
cornelia huck | cornelia huck | 23 | 100.00% | 1 | 100.00% |
| Total | 23 | 100.00% | 1 | 100.00% |
/*
* Init function for virtio
* devices are in a single page above top of "normal" + standby mem
*/
static int __init kvm_devices_init(void)
{
int rc;
unsigned long total_memory_size = sclp.rzm * sclp.rnmax;
if (!MACHINE_IS_KVM)
return -ENODEV;
if (test_devices_support(total_memory_size) < 0)
return -ENODEV;
rc = vmem_add_mapping(total_memory_size, PAGE_SIZE);
if (rc)
return rc;
kvm_devices = (void *) total_memory_size;
kvm_root = root_device_register("kvm_s390");
if (IS_ERR(kvm_root)) {
rc = PTR_ERR(kvm_root);
printk(KERN_ERR "Could not register kvm_s390 root device");
vmem_remove_mapping(total_memory_size, PAGE_SIZE);
return rc;
}
INIT_WORK(&hotplug_work, hotplug_devices);
irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL);
register_external_irq(EXT_IRQ_CP_SERVICE, kvm_extint_handler);
scan_devices();
return 0;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
christian borntraeger | christian borntraeger | 53 | 39.26% | 1 | 10.00% |
cornelia huck | cornelia huck | 48 | 35.56% | 2 | 20.00% |
nick wang | nick wang | 10 | 7.41% | 1 | 10.00% |
alexander graf | alexander graf | 8 | 5.93% | 1 | 10.00% |
heiko carstens | heiko carstens | 7 | 5.19% | 2 | 20.00% |
david hildenbrand | david hildenbrand | 6 | 4.44% | 1 | 10.00% |
thomas huth | thomas huth | 2 | 1.48% | 1 | 10.00% |
mark mcloughlin | mark mcloughlin | 1 | 0.74% | 1 | 10.00% |
| Total | 135 | 100.00% | 10 | 100.00% |
/* 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;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
christian borntraeger | christian borntraeger | 79 | 100.00% | 1 | 100.00% |
| Total | 79 | 100.00% | 1 | 100.00% |
static int __init s390_virtio_console_init(void)
{
if (sclp.has_vt220 || sclp.has_linemode)
return -ENODEV;
return virtio_cons_early_init(early_put_chars);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
hendrik brueckner | hendrik brueckner | 11 | 37.93% | 1 | 20.00% |
christian borntraeger | christian borntraeger | 10 | 34.48% | 1 | 20.00% |
david hildenbrand | david hildenbrand | 6 | 20.69% | 1 | 20.00% |
heinz graalfs | heinz graalfs | 1 | 3.45% | 1 | 20.00% |
heiko carstens | heiko carstens | 1 | 3.45% | 1 | 20.00% |
| Total | 29 | 100.00% | 5 | 100.00% |
console_initcall(s390_virtio_console_init);
/*
* We do this after core stuff, but before the drivers.
*/
postcore_initcall(kvm_devices_init);
Overall Contributors
| Person | Tokens | Prop | Commits | CommitProp |
christian borntraeger | christian borntraeger | 1134 | 59.19% | 3 | 6.52% |
michael s. tsirkin | michael s. tsirkin | 198 | 10.33% | 7 | 15.22% |
alexander graf | alexander graf | 167 | 8.72% | 2 | 4.35% |
heiko carstens | heiko carstens | 158 | 8.25% | 10 | 21.74% |
cornelia huck | cornelia huck | 73 | 3.81% | 2 | 4.35% |
rusty russell | rusty russell | 66 | 3.44% | 7 | 15.22% |
martin schwidefsky | martin schwidefsky | 28 | 1.46% | 3 | 6.52% |
heinz graalfs | heinz graalfs | 22 | 1.15% | 2 | 4.35% |
rick jones | rick jones | 21 | 1.10% | 1 | 2.17% |
hendrik brueckner | hendrik brueckner | 16 | 0.84% | 1 | 2.17% |
david hildenbrand | david hildenbrand | 12 | 0.63% | 1 | 2.17% |
nick wang | nick wang | 11 | 0.57% | 1 | 2.17% |
tejun heo | tejun heo | 3 | 0.16% | 1 | 2.17% |
thomas huth | thomas huth | 2 | 0.10% | 1 | 2.17% |
jason wang | jason wang | 2 | 0.10% | 1 | 2.17% |
stefan hajnoczi | stefan hajnoczi | 1 | 0.05% | 1 | 2.17% |
mark mcloughlin | mark mcloughlin | 1 | 0.05% | 1 | 2.17% |
stephen hemminger | stephen hemminger | 1 | 0.05% | 1 | 2.17% |
| Total | 1916 | 100.00% | 46 | 100.00% |
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.