kvm_virtio.c 8.3 KB

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