kvm_virtio.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 <linux/pfn.h>
  20. #include <asm/io.h>
  21. #include <asm/kvm_para.h>
  22. #include <asm/kvm_virtio.h>
  23. #include <asm/setup.h>
  24. #include <asm/s390_ext.h>
  25. #define VIRTIO_SUBCODE_64 0x0D00
  26. /*
  27. * The pointer to our (page) of device descriptions.
  28. */
  29. static void *kvm_devices;
  30. /*
  31. * Unique numbering for kvm devices.
  32. */
  33. static unsigned int dev_index;
  34. struct kvm_device {
  35. struct virtio_device vdev;
  36. struct kvm_device_desc *desc;
  37. };
  38. #define to_kvmdev(vd) container_of(vd, struct kvm_device, vdev)
  39. /*
  40. * memory layout:
  41. * - kvm_device_descriptor
  42. * struct kvm_device_desc
  43. * - configuration
  44. * struct kvm_vqconfig
  45. * - feature bits
  46. * - config space
  47. */
  48. static struct kvm_vqconfig *kvm_vq_config(const struct kvm_device_desc *desc)
  49. {
  50. return (struct kvm_vqconfig *)(desc + 1);
  51. }
  52. static u8 *kvm_vq_features(const struct kvm_device_desc *desc)
  53. {
  54. return (u8 *)(kvm_vq_config(desc) + desc->num_vq);
  55. }
  56. static u8 *kvm_vq_configspace(const struct kvm_device_desc *desc)
  57. {
  58. return kvm_vq_features(desc) + desc->feature_len * 2;
  59. }
  60. /*
  61. * The total size of the config page used by this device (incl. desc)
  62. */
  63. static unsigned desc_size(const struct kvm_device_desc *desc)
  64. {
  65. return sizeof(*desc)
  66. + desc->num_vq * sizeof(struct kvm_vqconfig)
  67. + desc->feature_len * 2
  68. + desc->config_len;
  69. }
  70. /*
  71. * This tests (and acknowleges) a feature bit.
  72. */
  73. static bool kvm_feature(struct virtio_device *vdev, unsigned fbit)
  74. {
  75. struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
  76. u8 *features;
  77. if (fbit / 8 > desc->feature_len)
  78. return false;
  79. features = kvm_vq_features(desc);
  80. if (!(features[fbit / 8] & (1 << (fbit % 8))))
  81. return false;
  82. /*
  83. * We set the matching bit in the other half of the bitmap to tell the
  84. * Host we want to use this feature.
  85. */
  86. features[desc->feature_len + fbit / 8] |= (1 << (fbit % 8));
  87. return true;
  88. }
  89. /*
  90. * Reading and writing elements in config space
  91. */
  92. static void kvm_get(struct virtio_device *vdev, unsigned int offset,
  93. void *buf, unsigned len)
  94. {
  95. struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
  96. BUG_ON(offset + len > desc->config_len);
  97. memcpy(buf, kvm_vq_configspace(desc) + offset, len);
  98. }
  99. static void kvm_set(struct virtio_device *vdev, unsigned int offset,
  100. const 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(kvm_vq_configspace(desc) + offset, buf, len);
  105. }
  106. /*
  107. * The operations to get and set the status word just access
  108. * the status field of the device descriptor. set_status will also
  109. * make a hypercall to the host, to tell about status changes
  110. */
  111. static u8 kvm_get_status(struct virtio_device *vdev)
  112. {
  113. return to_kvmdev(vdev)->desc->status;
  114. }
  115. static void kvm_set_status(struct virtio_device *vdev, u8 status)
  116. {
  117. BUG_ON(!status);
  118. to_kvmdev(vdev)->desc->status = status;
  119. kvm_hypercall1(KVM_S390_VIRTIO_SET_STATUS,
  120. (unsigned long) to_kvmdev(vdev)->desc);
  121. }
  122. /*
  123. * To reset the device, we use the KVM_VIRTIO_RESET hypercall, using the
  124. * descriptor address. The Host will zero the status and all the
  125. * features.
  126. */
  127. static void kvm_reset(struct virtio_device *vdev)
  128. {
  129. kvm_hypercall1(KVM_S390_VIRTIO_RESET,
  130. (unsigned long) to_kvmdev(vdev)->desc);
  131. }
  132. /*
  133. * When the virtio_ring code wants to notify the Host, it calls us here and we
  134. * make a hypercall. We hand the address of the virtqueue so the Host
  135. * knows which virtqueue we're talking about.
  136. */
  137. static void kvm_notify(struct virtqueue *vq)
  138. {
  139. struct kvm_vqconfig *config = vq->priv;
  140. kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, config->address);
  141. }
  142. /*
  143. * This routine finds the first virtqueue described in the configuration of
  144. * this device and sets it up.
  145. */
  146. static struct virtqueue *kvm_find_vq(struct virtio_device *vdev,
  147. unsigned index,
  148. void (*callback)(struct virtqueue *vq))
  149. {
  150. struct kvm_device *kdev = to_kvmdev(vdev);
  151. struct kvm_vqconfig *config;
  152. struct virtqueue *vq;
  153. int err;
  154. if (index >= kdev->desc->num_vq)
  155. return ERR_PTR(-ENOENT);
  156. config = kvm_vq_config(kdev->desc)+index;
  157. err = vmem_add_mapping(config->address,
  158. vring_size(config->num, PAGE_SIZE));
  159. if (err)
  160. goto out;
  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. vmem_remove_mapping(config->address,
  176. vring_size(config->num, 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. vmem_remove_mapping(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. rc = vmem_add_mapping(PFN_PHYS(max_pfn), PAGE_SIZE);
  273. if (rc) {
  274. device_unregister(&kvm_root);
  275. return rc;
  276. }
  277. kvm_devices = (void *) PFN_PHYS(max_pfn);
  278. ctl_set_bit(0, 9);
  279. register_external_interrupt(0x2603, kvm_extint_handler);
  280. scan_devices();
  281. return 0;
  282. }
  283. /*
  284. * We do this after core stuff, but before the drivers.
  285. */
  286. postcore_initcall(kvm_devices_init);