Christian Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 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 | |
| 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 Borntraeger | faeba83 | 2008-06-20 15:24:18 +0200 | [diff] [blame] | 18 | #include <linux/virtio_console.h> |
Christian Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/virtio_ring.h> |
Heiko Carstens | 17f3458 | 2008-04-30 13:38:47 +0200 | [diff] [blame] | 21 | #include <linux/pfn.h> |
Christian Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 22 | #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 | */ |
| 33 | static void *kvm_devices; |
| 34 | |
Christian Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 35 | struct 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 | */ |
| 51 | static struct kvm_vqconfig *kvm_vq_config(const struct kvm_device_desc *desc) |
| 52 | { |
| 53 | return (struct kvm_vqconfig *)(desc + 1); |
| 54 | } |
| 55 | |
| 56 | static u8 *kvm_vq_features(const struct kvm_device_desc *desc) |
| 57 | { |
| 58 | return (u8 *)(kvm_vq_config(desc) + desc->num_vq); |
| 59 | } |
| 60 | |
| 61 | static 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 | */ |
| 69 | static 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 Carstens | 5ca9fd5 | 2008-05-06 17:38:30 +0300 | [diff] [blame] | 77 | /* This gets the device's feature bits. */ |
| 78 | static u32 kvm_get_features(struct virtio_device *vdev) |
Christian Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 79 | { |
Heiko Carstens | 5ca9fd5 | 2008-05-06 17:38:30 +0300 | [diff] [blame] | 80 | unsigned int i; |
| 81 | u32 features = 0; |
Christian Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 82 | struct kvm_device_desc *desc = to_kvmdev(vdev)->desc; |
Heiko Carstens | 5ca9fd5 | 2008-05-06 17:38:30 +0300 | [diff] [blame] | 83 | u8 *in_features = kvm_vq_features(desc); |
Christian Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 84 | |
Heiko Carstens | 5ca9fd5 | 2008-05-06 17:38:30 +0300 | [diff] [blame] | 85 | 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 Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 90 | |
Rusty Russell | c624896 | 2008-07-25 12:06:07 -0500 | [diff] [blame] | 91 | static void kvm_finalize_features(struct virtio_device *vdev) |
Heiko Carstens | 5ca9fd5 | 2008-05-06 17:38:30 +0300 | [diff] [blame] | 92 | { |
Rusty Russell | c624896 | 2008-07-25 12:06:07 -0500 | [diff] [blame] | 93 | unsigned int i, bits; |
Heiko Carstens | 5ca9fd5 | 2008-05-06 17:38:30 +0300 | [diff] [blame] | 94 | 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 Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 97 | |
Rusty Russell | e34f872 | 2008-07-25 12:06:13 -0500 | [diff] [blame] | 98 | /* Give virtio_ring a chance to accept features. */ |
| 99 | vring_transport_features(vdev); |
| 100 | |
Heiko Carstens | 5ca9fd5 | 2008-05-06 17:38:30 +0300 | [diff] [blame] | 101 | memset(out_features, 0, desc->feature_len); |
Rusty Russell | c624896 | 2008-07-25 12:06:07 -0500 | [diff] [blame] | 102 | bits = min_t(unsigned, desc->feature_len, sizeof(vdev->features)) * 8; |
| 103 | for (i = 0; i < bits; i++) { |
| 104 | if (test_bit(i, vdev->features)) |
Heiko Carstens | 5ca9fd5 | 2008-05-06 17:38:30 +0300 | [diff] [blame] | 105 | out_features[i / 8] |= (1 << (i % 8)); |
| 106 | } |
Christian Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Reading and writing elements in config space |
| 111 | */ |
| 112 | static void kvm_get(struct virtio_device *vdev, unsigned int offset, |
| 113 | void *buf, unsigned len) |
| 114 | { |
| 115 | struct kvm_device_desc *desc = to_kvmdev(vdev)->desc; |
| 116 | |
| 117 | BUG_ON(offset + len > desc->config_len); |
| 118 | memcpy(buf, kvm_vq_configspace(desc) + offset, len); |
| 119 | } |
| 120 | |
| 121 | static void kvm_set(struct virtio_device *vdev, unsigned int offset, |
| 122 | const void *buf, unsigned len) |
| 123 | { |
| 124 | struct kvm_device_desc *desc = to_kvmdev(vdev)->desc; |
| 125 | |
| 126 | BUG_ON(offset + len > desc->config_len); |
| 127 | memcpy(kvm_vq_configspace(desc) + offset, buf, len); |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * The operations to get and set the status word just access |
| 132 | * the status field of the device descriptor. set_status will also |
| 133 | * make a hypercall to the host, to tell about status changes |
| 134 | */ |
| 135 | static u8 kvm_get_status(struct virtio_device *vdev) |
| 136 | { |
| 137 | return to_kvmdev(vdev)->desc->status; |
| 138 | } |
| 139 | |
| 140 | static void kvm_set_status(struct virtio_device *vdev, u8 status) |
| 141 | { |
| 142 | BUG_ON(!status); |
| 143 | to_kvmdev(vdev)->desc->status = status; |
| 144 | kvm_hypercall1(KVM_S390_VIRTIO_SET_STATUS, |
| 145 | (unsigned long) to_kvmdev(vdev)->desc); |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * To reset the device, we use the KVM_VIRTIO_RESET hypercall, using the |
| 150 | * descriptor address. The Host will zero the status and all the |
| 151 | * features. |
| 152 | */ |
| 153 | static void kvm_reset(struct virtio_device *vdev) |
| 154 | { |
| 155 | kvm_hypercall1(KVM_S390_VIRTIO_RESET, |
| 156 | (unsigned long) to_kvmdev(vdev)->desc); |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * When the virtio_ring code wants to notify the Host, it calls us here and we |
| 161 | * make a hypercall. We hand the address of the virtqueue so the Host |
| 162 | * knows which virtqueue we're talking about. |
| 163 | */ |
| 164 | static void kvm_notify(struct virtqueue *vq) |
| 165 | { |
| 166 | struct kvm_vqconfig *config = vq->priv; |
| 167 | |
| 168 | kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, config->address); |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * This routine finds the first virtqueue described in the configuration of |
| 173 | * this device and sets it up. |
| 174 | */ |
| 175 | static struct virtqueue *kvm_find_vq(struct virtio_device *vdev, |
| 176 | unsigned index, |
| 177 | void (*callback)(struct virtqueue *vq)) |
| 178 | { |
| 179 | struct kvm_device *kdev = to_kvmdev(vdev); |
| 180 | struct kvm_vqconfig *config; |
| 181 | struct virtqueue *vq; |
| 182 | int err; |
| 183 | |
| 184 | if (index >= kdev->desc->num_vq) |
| 185 | return ERR_PTR(-ENOENT); |
| 186 | |
| 187 | config = kvm_vq_config(kdev->desc)+index; |
| 188 | |
Heiko Carstens | 17f3458 | 2008-04-30 13:38:47 +0200 | [diff] [blame] | 189 | err = vmem_add_mapping(config->address, |
Rusty Russell | db40598 | 2008-12-30 09:26:02 -0600 | [diff] [blame] | 190 | vring_size(config->num, |
| 191 | KVM_S390_VIRTIO_RING_ALIGN)); |
Heiko Carstens | 17f3458 | 2008-04-30 13:38:47 +0200 | [diff] [blame] | 192 | if (err) |
Christian Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 193 | goto out; |
Christian Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 194 | |
Rusty Russell | 87c7d57 | 2008-12-30 09:26:03 -0600 | [diff] [blame] | 195 | vq = vring_new_virtqueue(config->num, KVM_S390_VIRTIO_RING_ALIGN, |
| 196 | vdev, (void *) config->address, |
Christian Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 197 | kvm_notify, callback); |
| 198 | if (!vq) { |
| 199 | err = -ENOMEM; |
| 200 | goto unmap; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * register a callback token |
| 205 | * The host will sent this via the external interrupt parameter |
| 206 | */ |
| 207 | config->token = (u64) vq; |
| 208 | |
| 209 | vq->priv = config; |
| 210 | return vq; |
| 211 | unmap: |
Heiko Carstens | 17f3458 | 2008-04-30 13:38:47 +0200 | [diff] [blame] | 212 | vmem_remove_mapping(config->address, |
Rusty Russell | db40598 | 2008-12-30 09:26:02 -0600 | [diff] [blame] | 213 | vring_size(config->num, |
| 214 | KVM_S390_VIRTIO_RING_ALIGN)); |
Christian Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 215 | out: |
| 216 | return ERR_PTR(err); |
| 217 | } |
| 218 | |
| 219 | static void kvm_del_vq(struct virtqueue *vq) |
| 220 | { |
| 221 | struct kvm_vqconfig *config = vq->priv; |
| 222 | |
| 223 | vring_del_virtqueue(vq); |
Heiko Carstens | 17f3458 | 2008-04-30 13:38:47 +0200 | [diff] [blame] | 224 | vmem_remove_mapping(config->address, |
Rusty Russell | db40598 | 2008-12-30 09:26:02 -0600 | [diff] [blame] | 225 | vring_size(config->num, |
| 226 | KVM_S390_VIRTIO_RING_ALIGN)); |
Christian Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* |
| 230 | * The config ops structure as defined by virtio config |
| 231 | */ |
| 232 | static struct virtio_config_ops kvm_vq_configspace_ops = { |
Heiko Carstens | 5ca9fd5 | 2008-05-06 17:38:30 +0300 | [diff] [blame] | 233 | .get_features = kvm_get_features, |
Rusty Russell | c624896 | 2008-07-25 12:06:07 -0500 | [diff] [blame] | 234 | .finalize_features = kvm_finalize_features, |
Christian Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 235 | .get = kvm_get, |
| 236 | .set = kvm_set, |
| 237 | .get_status = kvm_get_status, |
| 238 | .set_status = kvm_set_status, |
| 239 | .reset = kvm_reset, |
| 240 | .find_vq = kvm_find_vq, |
| 241 | .del_vq = kvm_del_vq, |
| 242 | }; |
| 243 | |
| 244 | /* |
| 245 | * The root device for the kvm virtio devices. |
| 246 | * This makes them appear as /sys/devices/kvm_s390/0,1,2 not /sys/devices/0,1,2. |
| 247 | */ |
Cornelia Huck | 37f1c01 | 2008-10-10 21:33:12 +0200 | [diff] [blame] | 248 | static struct device *kvm_root; |
Christian Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 249 | |
| 250 | /* |
| 251 | * adds a new device and register it with virtio |
| 252 | * appropriate drivers are loaded by the device model |
| 253 | */ |
Rusty Russell | b769f57 | 2008-05-30 15:09:42 -0500 | [diff] [blame] | 254 | static void add_kvm_device(struct kvm_device_desc *d, unsigned int offset) |
Christian Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 255 | { |
| 256 | struct kvm_device *kdev; |
| 257 | |
| 258 | kdev = kzalloc(sizeof(*kdev), GFP_KERNEL); |
| 259 | if (!kdev) { |
Rusty Russell | b769f57 | 2008-05-30 15:09:42 -0500 | [diff] [blame] | 260 | printk(KERN_EMERG "Cannot allocate kvm dev %u type %u\n", |
| 261 | offset, d->type); |
Christian Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 262 | return; |
| 263 | } |
| 264 | |
Cornelia Huck | 37f1c01 | 2008-10-10 21:33:12 +0200 | [diff] [blame] | 265 | kdev->vdev.dev.parent = kvm_root; |
Christian Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 266 | kdev->vdev.id.device = d->type; |
| 267 | kdev->vdev.config = &kvm_vq_configspace_ops; |
| 268 | kdev->desc = d; |
| 269 | |
| 270 | if (register_virtio_device(&kdev->vdev) != 0) { |
Rusty Russell | b769f57 | 2008-05-30 15:09:42 -0500 | [diff] [blame] | 271 | printk(KERN_ERR "Failed to register kvm device %u type %u\n", |
| 272 | offset, d->type); |
Christian Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 273 | kfree(kdev); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * scan_devices() simply iterates through the device page. |
| 279 | * The type 0 is reserved to mean "end of devices". |
| 280 | */ |
| 281 | static void scan_devices(void) |
| 282 | { |
| 283 | unsigned int i; |
| 284 | struct kvm_device_desc *d; |
| 285 | |
| 286 | for (i = 0; i < PAGE_SIZE; i += desc_size(d)) { |
| 287 | d = kvm_devices + i; |
| 288 | |
| 289 | if (d->type == 0) |
| 290 | break; |
| 291 | |
Rusty Russell | b769f57 | 2008-05-30 15:09:42 -0500 | [diff] [blame] | 292 | add_kvm_device(d, i); |
Christian Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * we emulate the request_irq behaviour on top of s390 extints |
| 298 | */ |
| 299 | static void kvm_extint_handler(u16 code) |
| 300 | { |
Christian Borntraeger | be3c583 | 2008-11-18 22:44:13 +0100 | [diff] [blame] | 301 | struct virtqueue *vq; |
| 302 | u16 subcode; |
| 303 | int config_changed; |
Christian Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 304 | |
Christian Borntraeger | be3c583 | 2008-11-18 22:44:13 +0100 | [diff] [blame] | 305 | subcode = S390_lowcore.cpu_addr; |
Christian Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 306 | if ((subcode & 0xff00) != VIRTIO_SUBCODE_64) |
| 307 | return; |
| 308 | |
Christian Borntraeger | be3c583 | 2008-11-18 22:44:13 +0100 | [diff] [blame] | 309 | /* The LSB might be overloaded, we have to mask it */ |
| 310 | vq = (struct virtqueue *) ((*(long *) __LC_PFAULT_INTPARM) & ~1UL); |
| 311 | |
| 312 | /* We use the LSB of extparam, to decide, if this interrupt is a config |
| 313 | * change or a "standard" interrupt */ |
| 314 | config_changed = (*(int *) __LC_EXT_PARAMS & 1); |
| 315 | |
| 316 | if (config_changed) { |
| 317 | struct virtio_driver *drv; |
| 318 | drv = container_of(vq->vdev->dev.driver, |
| 319 | struct virtio_driver, driver); |
| 320 | if (drv->config_changed) |
| 321 | drv->config_changed(vq->vdev); |
| 322 | } else |
| 323 | vring_interrupt(0, vq); |
Christian Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /* |
| 327 | * Init function for virtio |
| 328 | * devices are in a single page above top of "normal" mem |
| 329 | */ |
| 330 | static int __init kvm_devices_init(void) |
| 331 | { |
| 332 | int rc; |
| 333 | |
| 334 | if (!MACHINE_IS_KVM) |
| 335 | return -ENODEV; |
| 336 | |
Mark McLoughlin | 035da16 | 2008-12-15 12:58:29 +0000 | [diff] [blame] | 337 | kvm_root = root_device_register("kvm_s390"); |
Cornelia Huck | 37f1c01 | 2008-10-10 21:33:12 +0200 | [diff] [blame] | 338 | if (IS_ERR(kvm_root)) { |
| 339 | rc = PTR_ERR(kvm_root); |
Christian Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 340 | printk(KERN_ERR "Could not register kvm_s390 root device"); |
| 341 | return rc; |
| 342 | } |
| 343 | |
Christian Borntraeger | cc835f78 | 2008-11-14 18:18:02 +0100 | [diff] [blame] | 344 | rc = vmem_add_mapping(real_memory_size, PAGE_SIZE); |
Heiko Carstens | 17f3458 | 2008-04-30 13:38:47 +0200 | [diff] [blame] | 345 | if (rc) { |
Mark McLoughlin | 035da16 | 2008-12-15 12:58:29 +0000 | [diff] [blame] | 346 | root_device_unregister(kvm_root); |
Heiko Carstens | 17f3458 | 2008-04-30 13:38:47 +0200 | [diff] [blame] | 347 | return rc; |
Christian Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 348 | } |
| 349 | |
Christian Borntraeger | cc835f78 | 2008-11-14 18:18:02 +0100 | [diff] [blame] | 350 | kvm_devices = (void *) real_memory_size; |
Christian Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 351 | |
| 352 | ctl_set_bit(0, 9); |
| 353 | register_external_interrupt(0x2603, kvm_extint_handler); |
| 354 | |
| 355 | scan_devices(); |
| 356 | return 0; |
| 357 | } |
| 358 | |
Christian Borntraeger | faeba83 | 2008-06-20 15:24:18 +0200 | [diff] [blame] | 359 | /* code for early console output with virtio_console */ |
| 360 | static __init int early_put_chars(u32 vtermno, const char *buf, int count) |
| 361 | { |
| 362 | char scratch[17]; |
| 363 | unsigned int len = count; |
| 364 | |
| 365 | if (len > sizeof(scratch) - 1) |
| 366 | len = sizeof(scratch) - 1; |
| 367 | scratch[len] = '\0'; |
| 368 | memcpy(scratch, buf, len); |
| 369 | kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, __pa(scratch)); |
| 370 | return len; |
| 371 | } |
| 372 | |
Heiko Carstens | 26f746f | 2008-08-01 16:39:22 +0200 | [diff] [blame] | 373 | void __init s390_virtio_console_init(void) |
Christian Borntraeger | faeba83 | 2008-06-20 15:24:18 +0200 | [diff] [blame] | 374 | { |
| 375 | virtio_cons_early_init(early_put_chars); |
| 376 | } |
| 377 | |
Christian Borntraeger | e976a2b | 2008-03-25 18:47:46 +0100 | [diff] [blame] | 378 | /* |
| 379 | * We do this after core stuff, but before the drivers. |
| 380 | */ |
| 381 | postcore_initcall(kvm_devices_init); |