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