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