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