kvm_virtio.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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/interrupt.h>
  18. #include <linux/virtio_ring.h>
  19. #include <asm/io.h>
  20. #include <asm/kvm_para.h>
  21. #include <asm/kvm_virtio.h>
  22. #include <asm/setup.h>
  23. #include <asm/s390_ext.h>
  24. #define VIRTIO_SUBCODE_64 0x0D00
  25. /*
  26. * The pointer to our (page) of device descriptions.
  27. */
  28. static void *kvm_devices;
  29. /*
  30. * Unique numbering for kvm devices.
  31. */
  32. static unsigned int dev_index;
  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. /*
  70. * This tests (and acknowleges) a feature bit.
  71. */
  72. static bool kvm_feature(struct virtio_device *vdev, unsigned fbit)
  73. {
  74. struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
  75. u8 *features;
  76. if (fbit / 8 > desc->feature_len)
  77. return false;
  78. features = kvm_vq_features(desc);
  79. if (!(features[fbit / 8] & (1 << (fbit % 8))))
  80. return false;
  81. /*
  82. * We set the matching bit in the other half of the bitmap to tell the
  83. * Host we want to use this feature.
  84. */
  85. features[desc->feature_len + fbit / 8] |= (1 << (fbit % 8));
  86. return true;
  87. }
  88. /*
  89. * Reading and writing elements in config space
  90. */
  91. static void kvm_get(struct virtio_device *vdev, unsigned int offset,
  92. void *buf, unsigned len)
  93. {
  94. struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
  95. BUG_ON(offset + len > desc->config_len);
  96. memcpy(buf, kvm_vq_configspace(desc) + offset, len);
  97. }
  98. static void kvm_set(struct virtio_device *vdev, unsigned int offset,
  99. const void *buf, unsigned len)
  100. {
  101. struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
  102. BUG_ON(offset + len > desc->config_len);
  103. memcpy(kvm_vq_configspace(desc) + offset, buf, len);
  104. }
  105. /*
  106. * The operations to get and set the status word just access
  107. * the status field of the device descriptor. set_status will also
  108. * make a hypercall to the host, to tell about status changes
  109. */
  110. static u8 kvm_get_status(struct virtio_device *vdev)
  111. {
  112. return to_kvmdev(vdev)->desc->status;
  113. }
  114. static void kvm_set_status(struct virtio_device *vdev, u8 status)
  115. {
  116. BUG_ON(!status);
  117. to_kvmdev(vdev)->desc->status = status;
  118. kvm_hypercall1(KVM_S390_VIRTIO_SET_STATUS,
  119. (unsigned long) to_kvmdev(vdev)->desc);
  120. }
  121. /*
  122. * To reset the device, we use the KVM_VIRTIO_RESET hypercall, using the
  123. * descriptor address. The Host will zero the status and all the
  124. * features.
  125. */
  126. static void kvm_reset(struct virtio_device *vdev)
  127. {
  128. kvm_hypercall1(KVM_S390_VIRTIO_RESET,
  129. (unsigned long) to_kvmdev(vdev)->desc);
  130. }
  131. /*
  132. * When the virtio_ring code wants to notify the Host, it calls us here and we
  133. * make a hypercall. We hand the address of the virtqueue so the Host
  134. * knows which virtqueue we're talking about.
  135. */
  136. static void kvm_notify(struct virtqueue *vq)
  137. {
  138. struct kvm_vqconfig *config = vq->priv;
  139. kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, config->address);
  140. }
  141. /*
  142. * This routine finds the first virtqueue described in the configuration of
  143. * this device and sets it up.
  144. */
  145. static struct virtqueue *kvm_find_vq(struct virtio_device *vdev,
  146. unsigned index,
  147. void (*callback)(struct virtqueue *vq))
  148. {
  149. struct kvm_device *kdev = to_kvmdev(vdev);
  150. struct kvm_vqconfig *config;
  151. struct virtqueue *vq;
  152. int err;
  153. if (index >= kdev->desc->num_vq)
  154. return ERR_PTR(-ENOENT);
  155. config = kvm_vq_config(kdev->desc)+index;
  156. if (add_shared_memory(config->address,
  157. vring_size(config->num, PAGE_SIZE))) {
  158. err = -ENOMEM;
  159. goto out;
  160. }
  161. vq = vring_new_virtqueue(config->num, vdev, (void *) config->address,
  162. kvm_notify, callback);
  163. if (!vq) {
  164. err = -ENOMEM;
  165. goto unmap;
  166. }
  167. /*
  168. * register a callback token
  169. * The host will sent this via the external interrupt parameter
  170. */
  171. config->token = (u64) vq;
  172. vq->priv = config;
  173. return vq;
  174. unmap:
  175. remove_shared_memory(config->address, vring_size(config->num,
  176. PAGE_SIZE));
  177. out:
  178. return ERR_PTR(err);
  179. }
  180. static void kvm_del_vq(struct virtqueue *vq)
  181. {
  182. struct kvm_vqconfig *config = vq->priv;
  183. vring_del_virtqueue(vq);
  184. remove_shared_memory(config->address,
  185. vring_size(config->num, PAGE_SIZE));
  186. }
  187. /*
  188. * The config ops structure as defined by virtio config
  189. */
  190. static struct virtio_config_ops kvm_vq_configspace_ops = {
  191. .feature = kvm_feature,
  192. .get = kvm_get,
  193. .set = kvm_set,
  194. .get_status = kvm_get_status,
  195. .set_status = kvm_set_status,
  196. .reset = kvm_reset,
  197. .find_vq = kvm_find_vq,
  198. .del_vq = kvm_del_vq,
  199. };
  200. /*
  201. * The root device for the kvm virtio devices.
  202. * This makes them appear as /sys/devices/kvm_s390/0,1,2 not /sys/devices/0,1,2.
  203. */
  204. static struct device kvm_root = {
  205. .parent = NULL,
  206. .bus_id = "kvm_s390",
  207. };
  208. /*
  209. * adds a new device and register it with virtio
  210. * appropriate drivers are loaded by the device model
  211. */
  212. static void add_kvm_device(struct kvm_device_desc *d)
  213. {
  214. struct kvm_device *kdev;
  215. kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
  216. if (!kdev) {
  217. printk(KERN_EMERG "Cannot allocate kvm dev %u\n",
  218. dev_index++);
  219. return;
  220. }
  221. kdev->vdev.dev.parent = &kvm_root;
  222. kdev->vdev.index = dev_index++;
  223. kdev->vdev.id.device = d->type;
  224. kdev->vdev.config = &kvm_vq_configspace_ops;
  225. kdev->desc = d;
  226. if (register_virtio_device(&kdev->vdev) != 0) {
  227. printk(KERN_ERR "Failed to register kvm device %u\n",
  228. kdev->vdev.index);
  229. kfree(kdev);
  230. }
  231. }
  232. /*
  233. * scan_devices() simply iterates through the device page.
  234. * The type 0 is reserved to mean "end of devices".
  235. */
  236. static void scan_devices(void)
  237. {
  238. unsigned int i;
  239. struct kvm_device_desc *d;
  240. for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
  241. d = kvm_devices + i;
  242. if (d->type == 0)
  243. break;
  244. add_kvm_device(d);
  245. }
  246. }
  247. /*
  248. * we emulate the request_irq behaviour on top of s390 extints
  249. */
  250. static void kvm_extint_handler(u16 code)
  251. {
  252. void *data = (void *) *(long *) __LC_PFAULT_INTPARM;
  253. u16 subcode = S390_lowcore.cpu_addr;
  254. if ((subcode & 0xff00) != VIRTIO_SUBCODE_64)
  255. return;
  256. vring_interrupt(0, data);
  257. }
  258. /*
  259. * Init function for virtio
  260. * devices are in a single page above top of "normal" mem
  261. */
  262. static int __init kvm_devices_init(void)
  263. {
  264. int rc;
  265. if (!MACHINE_IS_KVM)
  266. return -ENODEV;
  267. rc = device_register(&kvm_root);
  268. if (rc) {
  269. printk(KERN_ERR "Could not register kvm_s390 root device");
  270. return rc;
  271. }
  272. if (add_shared_memory((max_pfn) << PAGE_SHIFT, PAGE_SIZE)) {
  273. device_unregister(&kvm_root);
  274. return -ENOMEM;
  275. }
  276. kvm_devices = (void *) (max_pfn << PAGE_SHIFT);
  277. ctl_set_bit(0, 9);
  278. register_external_interrupt(0x2603, kvm_extint_handler);
  279. scan_devices();
  280. return 0;
  281. }
  282. /*
  283. * We do this after core stuff, but before the drivers.
  284. */
  285. postcore_initcall(kvm_devices_init);