kvm_virtio.c 9.1 KB

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