kvm_virtio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. #include <linux/init.h>
  13. #include <linux/bootmem.h>
  14. #include <linux/err.h>
  15. #include <linux/virtio.h>
  16. #include <linux/virtio_config.h>
  17. #include <linux/slab.h>
  18. #include <linux/virtio_console.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/virtio_ring.h>
  21. #include <linux/pfn.h>
  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. #define VIRTIO_SUBCODE_64 0x0D00
  28. /*
  29. * The pointer to our (page) of device descriptions.
  30. */
  31. static void *kvm_devices;
  32. struct work_struct hotplug_work;
  33. struct kvm_device {
  34. struct virtio_device vdev;
  35. struct kvm_device_desc *desc;
  36. };
  37. #define to_kvmdev(vd) container_of(vd, struct kvm_device, vdev)
  38. /*
  39. * memory layout:
  40. * - kvm_device_descriptor
  41. * struct kvm_device_desc
  42. * - configuration
  43. * struct kvm_vqconfig
  44. * - feature bits
  45. * - config space
  46. */
  47. static struct kvm_vqconfig *kvm_vq_config(const struct kvm_device_desc *desc)
  48. {
  49. return (struct kvm_vqconfig *)(desc + 1);
  50. }
  51. static u8 *kvm_vq_features(const struct kvm_device_desc *desc)
  52. {
  53. return (u8 *)(kvm_vq_config(desc) + desc->num_vq);
  54. }
  55. static u8 *kvm_vq_configspace(const struct kvm_device_desc *desc)
  56. {
  57. return kvm_vq_features(desc) + desc->feature_len * 2;
  58. }
  59. /*
  60. * The total size of the config page used by this device (incl. desc)
  61. */
  62. static unsigned desc_size(const struct kvm_device_desc *desc)
  63. {
  64. return sizeof(*desc)
  65. + desc->num_vq * sizeof(struct kvm_vqconfig)
  66. + desc->feature_len * 2
  67. + desc->config_len;
  68. }
  69. /* This gets the device's feature bits. */
  70. static u32 kvm_get_features(struct virtio_device *vdev)
  71. {
  72. unsigned int i;
  73. u32 features = 0;
  74. struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
  75. u8 *in_features = kvm_vq_features(desc);
  76. for (i = 0; i < min(desc->feature_len * 8, 32); i++)
  77. if (in_features[i / 8] & (1 << (i % 8)))
  78. features |= (1 << i);
  79. return features;
  80. }
  81. static void kvm_finalize_features(struct virtio_device *vdev)
  82. {
  83. unsigned int i, bits;
  84. struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
  85. /* Second half of bitmap is features we accept. */
  86. u8 *out_features = kvm_vq_features(desc) + desc->feature_len;
  87. /* Give virtio_ring a chance to accept features. */
  88. vring_transport_features(vdev);
  89. memset(out_features, 0, desc->feature_len);
  90. bits = min_t(unsigned, desc->feature_len, sizeof(vdev->features)) * 8;
  91. for (i = 0; i < bits; i++) {
  92. if (test_bit(i, vdev->features))
  93. out_features[i / 8] |= (1 << (i % 8));
  94. }
  95. }
  96. /*
  97. * Reading and writing elements in config space
  98. */
  99. static void kvm_get(struct virtio_device *vdev, unsigned int offset,
  100. void *buf, unsigned len)
  101. {
  102. struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
  103. BUG_ON(offset + len > desc->config_len);
  104. memcpy(buf, kvm_vq_configspace(desc) + offset, len);
  105. }
  106. static void kvm_set(struct virtio_device *vdev, unsigned int offset,
  107. const void *buf, unsigned len)
  108. {
  109. struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
  110. BUG_ON(offset + len > desc->config_len);
  111. memcpy(kvm_vq_configspace(desc) + offset, buf, len);
  112. }
  113. /*
  114. * The operations to get and set the status word just access
  115. * the status field of the device descriptor. set_status will also
  116. * make a hypercall to the host, to tell about status changes
  117. */
  118. static u8 kvm_get_status(struct virtio_device *vdev)
  119. {
  120. return to_kvmdev(vdev)->desc->status;
  121. }
  122. static void kvm_set_status(struct virtio_device *vdev, u8 status)
  123. {
  124. BUG_ON(!status);
  125. to_kvmdev(vdev)->desc->status = status;
  126. kvm_hypercall1(KVM_S390_VIRTIO_SET_STATUS,
  127. (unsigned long) to_kvmdev(vdev)->desc);
  128. }
  129. /*
  130. * To reset the device, we use the KVM_VIRTIO_RESET hypercall, using the
  131. * descriptor address. The Host will zero the status and all the
  132. * features.
  133. */
  134. static void kvm_reset(struct virtio_device *vdev)
  135. {
  136. kvm_hypercall1(KVM_S390_VIRTIO_RESET,
  137. (unsigned long) to_kvmdev(vdev)->desc);
  138. }
  139. /*
  140. * When the virtio_ring code wants to notify the Host, it calls us here and we
  141. * make a hypercall. We hand the address of the virtqueue so the Host
  142. * knows which virtqueue we're talking about.
  143. */
  144. static void kvm_notify(struct virtqueue *vq)
  145. {
  146. struct kvm_vqconfig *config = vq->priv;
  147. kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, config->address);
  148. }
  149. /*
  150. * This routine finds the first virtqueue described in the configuration of
  151. * this device and sets it up.
  152. */
  153. static struct virtqueue *kvm_find_vq(struct virtio_device *vdev,
  154. unsigned index,
  155. void (*callback)(struct virtqueue *vq),
  156. const char *name)
  157. {
  158. struct kvm_device *kdev = to_kvmdev(vdev);
  159. struct kvm_vqconfig *config;
  160. struct virtqueue *vq;
  161. int err;
  162. if (index >= kdev->desc->num_vq)
  163. return ERR_PTR(-ENOENT);
  164. config = kvm_vq_config(kdev->desc)+index;
  165. err = vmem_add_mapping(config->address,
  166. vring_size(config->num,
  167. KVM_S390_VIRTIO_RING_ALIGN));
  168. if (err)
  169. goto out;
  170. vq = vring_new_virtqueue(config->num, KVM_S390_VIRTIO_RING_ALIGN,
  171. vdev, (void *) config->address,
  172. kvm_notify, callback, name);
  173. if (!vq) {
  174. err = -ENOMEM;
  175. goto unmap;
  176. }
  177. /*
  178. * register a callback token
  179. * The host will sent this via the external interrupt parameter
  180. */
  181. config->token = (u64) vq;
  182. vq->priv = config;
  183. return vq;
  184. unmap:
  185. vmem_remove_mapping(config->address,
  186. vring_size(config->num,
  187. KVM_S390_VIRTIO_RING_ALIGN));
  188. out:
  189. return ERR_PTR(err);
  190. }
  191. static void kvm_del_vq(struct virtqueue *vq)
  192. {
  193. struct kvm_vqconfig *config = vq->priv;
  194. vring_del_virtqueue(vq);
  195. vmem_remove_mapping(config->address,
  196. vring_size(config->num,
  197. KVM_S390_VIRTIO_RING_ALIGN));
  198. }
  199. static void kvm_del_vqs(struct virtio_device *vdev)
  200. {
  201. struct virtqueue *vq, *n;
  202. list_for_each_entry_safe(vq, n, &vdev->vqs, list)
  203. kvm_del_vq(vq);
  204. }
  205. static int kvm_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  206. struct virtqueue *vqs[],
  207. vq_callback_t *callbacks[],
  208. const char *names[])
  209. {
  210. struct kvm_device *kdev = to_kvmdev(vdev);
  211. int i;
  212. /* We must have this many virtqueues. */
  213. if (nvqs > kdev->desc->num_vq)
  214. return -ENOENT;
  215. for (i = 0; i < nvqs; ++i) {
  216. vqs[i] = kvm_find_vq(vdev, i, callbacks[i], names[i]);
  217. if (IS_ERR(vqs[i]))
  218. goto error;
  219. }
  220. return 0;
  221. error:
  222. kvm_del_vqs(vdev);
  223. return PTR_ERR(vqs[i]);
  224. }
  225. /*
  226. * The config ops structure as defined by virtio config
  227. */
  228. static struct virtio_config_ops kvm_vq_configspace_ops = {
  229. .get_features = kvm_get_features,
  230. .finalize_features = kvm_finalize_features,
  231. .get = kvm_get,
  232. .set = kvm_set,
  233. .get_status = kvm_get_status,
  234. .set_status = kvm_set_status,
  235. .reset = kvm_reset,
  236. .find_vqs = kvm_find_vqs,
  237. .del_vqs = kvm_del_vqs,
  238. };
  239. /*
  240. * The root device for the kvm virtio devices.
  241. * This makes them appear as /sys/devices/kvm_s390/0,1,2 not /sys/devices/0,1,2.
  242. */
  243. static struct device *kvm_root;
  244. /*
  245. * adds a new device and register it with virtio
  246. * appropriate drivers are loaded by the device model
  247. */
  248. static void add_kvm_device(struct kvm_device_desc *d, unsigned int offset)
  249. {
  250. struct kvm_device *kdev;
  251. kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
  252. if (!kdev) {
  253. printk(KERN_EMERG "Cannot allocate kvm dev %u type %u\n",
  254. offset, d->type);
  255. return;
  256. }
  257. kdev->vdev.dev.parent = kvm_root;
  258. kdev->vdev.id.device = d->type;
  259. kdev->vdev.config = &kvm_vq_configspace_ops;
  260. kdev->desc = d;
  261. if (register_virtio_device(&kdev->vdev) != 0) {
  262. printk(KERN_ERR "Failed to register kvm device %u type %u\n",
  263. offset, d->type);
  264. kfree(kdev);
  265. }
  266. }
  267. /*
  268. * scan_devices() simply iterates through the device page.
  269. * The type 0 is reserved to mean "end of devices".
  270. */
  271. static void scan_devices(void)
  272. {
  273. unsigned int i;
  274. struct kvm_device_desc *d;
  275. for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
  276. d = kvm_devices + i;
  277. if (d->type == 0)
  278. break;
  279. add_kvm_device(d, i);
  280. }
  281. }
  282. /*
  283. * match for a kvm device with a specific desc pointer
  284. */
  285. static int match_desc(struct device *dev, void *data)
  286. {
  287. if ((ulong)to_kvmdev(dev_to_virtio(dev))->desc == (ulong)data)
  288. return 1;
  289. return 0;
  290. }
  291. /*
  292. * hotplug_device tries to find changes in the device page.
  293. */
  294. static void hotplug_devices(struct work_struct *dummy)
  295. {
  296. unsigned int i;
  297. struct kvm_device_desc *d;
  298. struct device *dev;
  299. for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
  300. d = kvm_devices + i;
  301. /* end of list */
  302. if (d->type == 0)
  303. break;
  304. /* device already exists */
  305. dev = device_find_child(kvm_root, d, match_desc);
  306. if (dev) {
  307. /* XXX check for hotplug remove */
  308. put_device(dev);
  309. continue;
  310. }
  311. /* new device */
  312. printk(KERN_INFO "Adding new virtio device %p\n", d);
  313. add_kvm_device(d, i);
  314. }
  315. }
  316. /*
  317. * we emulate the request_irq behaviour on top of s390 extints
  318. */
  319. static void kvm_extint_handler(unsigned int ext_int_code,
  320. unsigned int param32, unsigned long param64)
  321. {
  322. struct virtqueue *vq;
  323. u16 subcode;
  324. u32 param;
  325. subcode = ext_int_code >> 16;
  326. if ((subcode & 0xff00) != VIRTIO_SUBCODE_64)
  327. return;
  328. /* The LSB might be overloaded, we have to mask it */
  329. vq = (struct virtqueue *)(param64 & ~1UL);
  330. /* We use ext_params to decide what this interrupt means */
  331. param = param32 & VIRTIO_PARAM_MASK;
  332. switch (param) {
  333. case VIRTIO_PARAM_CONFIG_CHANGED:
  334. {
  335. struct virtio_driver *drv;
  336. drv = container_of(vq->vdev->dev.driver,
  337. struct virtio_driver, driver);
  338. if (drv->config_changed)
  339. drv->config_changed(vq->vdev);
  340. break;
  341. }
  342. case VIRTIO_PARAM_DEV_ADD:
  343. schedule_work(&hotplug_work);
  344. break;
  345. case VIRTIO_PARAM_VRING_INTERRUPT:
  346. default:
  347. vring_interrupt(0, vq);
  348. break;
  349. }
  350. }
  351. /*
  352. * Init function for virtio
  353. * devices are in a single page above top of "normal" mem
  354. */
  355. static int __init kvm_devices_init(void)
  356. {
  357. int rc;
  358. if (!MACHINE_IS_KVM)
  359. return -ENODEV;
  360. kvm_root = root_device_register("kvm_s390");
  361. if (IS_ERR(kvm_root)) {
  362. rc = PTR_ERR(kvm_root);
  363. printk(KERN_ERR "Could not register kvm_s390 root device");
  364. return rc;
  365. }
  366. rc = vmem_add_mapping(real_memory_size, PAGE_SIZE);
  367. if (rc) {
  368. root_device_unregister(kvm_root);
  369. return rc;
  370. }
  371. kvm_devices = (void *) real_memory_size;
  372. INIT_WORK(&hotplug_work, hotplug_devices);
  373. ctl_set_bit(0, 9);
  374. register_external_interrupt(0x2603, kvm_extint_handler);
  375. scan_devices();
  376. return 0;
  377. }
  378. /* code for early console output with virtio_console */
  379. static __init int early_put_chars(u32 vtermno, const char *buf, int count)
  380. {
  381. char scratch[17];
  382. unsigned int len = count;
  383. if (len > sizeof(scratch) - 1)
  384. len = sizeof(scratch) - 1;
  385. scratch[len] = '\0';
  386. memcpy(scratch, buf, len);
  387. kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, __pa(scratch));
  388. return len;
  389. }
  390. static int __init s390_virtio_console_init(void)
  391. {
  392. if (!MACHINE_IS_KVM)
  393. return -ENODEV;
  394. return virtio_cons_early_init(early_put_chars);
  395. }
  396. console_initcall(s390_virtio_console_init);
  397. /*
  398. * We do this after core stuff, but before the drivers.
  399. */
  400. postcore_initcall(kvm_devices_init);