kvm_virtio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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/kernel_stat.h>
  13. #include <linux/init.h>
  14. #include <linux/bootmem.h>
  15. #include <linux/err.h>
  16. #include <linux/virtio.h>
  17. #include <linux/virtio_config.h>
  18. #include <linux/slab.h>
  19. #include <linux/virtio_console.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/virtio_ring.h>
  22. #include <linux/pfn.h>
  23. #include <asm/io.h>
  24. #include <asm/kvm_para.h>
  25. #include <asm/kvm_virtio.h>
  26. #include <asm/setup.h>
  27. #include <asm/s390_ext.h>
  28. #include <asm/irq.h>
  29. #define VIRTIO_SUBCODE_64 0x0D00
  30. /*
  31. * The pointer to our (page) of device descriptions.
  32. */
  33. static void *kvm_devices;
  34. struct work_struct hotplug_work;
  35. struct kvm_device {
  36. struct virtio_device vdev;
  37. struct kvm_device_desc *desc;
  38. };
  39. #define to_kvmdev(vd) container_of(vd, struct kvm_device, vdev)
  40. /*
  41. * memory layout:
  42. * - kvm_device_descriptor
  43. * struct kvm_device_desc
  44. * - configuration
  45. * struct kvm_vqconfig
  46. * - feature bits
  47. * - config space
  48. */
  49. static struct kvm_vqconfig *kvm_vq_config(const struct kvm_device_desc *desc)
  50. {
  51. return (struct kvm_vqconfig *)(desc + 1);
  52. }
  53. static u8 *kvm_vq_features(const struct kvm_device_desc *desc)
  54. {
  55. return (u8 *)(kvm_vq_config(desc) + desc->num_vq);
  56. }
  57. static u8 *kvm_vq_configspace(const struct kvm_device_desc *desc)
  58. {
  59. return kvm_vq_features(desc) + desc->feature_len * 2;
  60. }
  61. /*
  62. * The total size of the config page used by this device (incl. desc)
  63. */
  64. static unsigned desc_size(const struct kvm_device_desc *desc)
  65. {
  66. return sizeof(*desc)
  67. + desc->num_vq * sizeof(struct kvm_vqconfig)
  68. + desc->feature_len * 2
  69. + desc->config_len;
  70. }
  71. /* This gets the device's feature bits. */
  72. static u32 kvm_get_features(struct virtio_device *vdev)
  73. {
  74. unsigned int i;
  75. u32 features = 0;
  76. struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
  77. u8 *in_features = kvm_vq_features(desc);
  78. for (i = 0; i < min(desc->feature_len * 8, 32); i++)
  79. if (in_features[i / 8] & (1 << (i % 8)))
  80. features |= (1 << i);
  81. return features;
  82. }
  83. static void kvm_finalize_features(struct virtio_device *vdev)
  84. {
  85. unsigned int i, bits;
  86. struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
  87. /* Second half of bitmap is features we accept. */
  88. u8 *out_features = kvm_vq_features(desc) + desc->feature_len;
  89. /* Give virtio_ring a chance to accept features. */
  90. vring_transport_features(vdev);
  91. memset(out_features, 0, desc->feature_len);
  92. bits = min_t(unsigned, desc->feature_len, sizeof(vdev->features)) * 8;
  93. for (i = 0; i < bits; i++) {
  94. if (test_bit(i, vdev->features))
  95. out_features[i / 8] |= (1 << (i % 8));
  96. }
  97. }
  98. /*
  99. * Reading and writing elements in config space
  100. */
  101. static void kvm_get(struct virtio_device *vdev, unsigned int offset,
  102. 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(buf, kvm_vq_configspace(desc) + offset, len);
  107. }
  108. static void kvm_set(struct virtio_device *vdev, unsigned int offset,
  109. const void *buf, unsigned len)
  110. {
  111. struct kvm_device_desc *desc = to_kvmdev(vdev)->desc;
  112. BUG_ON(offset + len > desc->config_len);
  113. memcpy(kvm_vq_configspace(desc) + offset, buf, len);
  114. }
  115. /*
  116. * The operations to get and set the status word just access
  117. * the status field of the device descriptor. set_status will also
  118. * make a hypercall to the host, to tell about status changes
  119. */
  120. static u8 kvm_get_status(struct virtio_device *vdev)
  121. {
  122. return to_kvmdev(vdev)->desc->status;
  123. }
  124. static void kvm_set_status(struct virtio_device *vdev, u8 status)
  125. {
  126. BUG_ON(!status);
  127. to_kvmdev(vdev)->desc->status = status;
  128. kvm_hypercall1(KVM_S390_VIRTIO_SET_STATUS,
  129. (unsigned long) to_kvmdev(vdev)->desc);
  130. }
  131. /*
  132. * To reset the device, we use the KVM_VIRTIO_RESET hypercall, using the
  133. * descriptor address. The Host will zero the status and all the
  134. * features.
  135. */
  136. static void kvm_reset(struct virtio_device *vdev)
  137. {
  138. kvm_hypercall1(KVM_S390_VIRTIO_RESET,
  139. (unsigned long) to_kvmdev(vdev)->desc);
  140. }
  141. /*
  142. * When the virtio_ring code wants to notify the Host, it calls us here and we
  143. * make a hypercall. We hand the address of the virtqueue so the Host
  144. * knows which virtqueue we're talking about.
  145. */
  146. static void kvm_notify(struct virtqueue *vq)
  147. {
  148. struct kvm_vqconfig *config = vq->priv;
  149. kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, config->address);
  150. }
  151. /*
  152. * This routine finds the first virtqueue described in the configuration of
  153. * this device and sets it up.
  154. */
  155. static struct virtqueue *kvm_find_vq(struct virtio_device *vdev,
  156. unsigned index,
  157. void (*callback)(struct virtqueue *vq),
  158. const char *name)
  159. {
  160. struct kvm_device *kdev = to_kvmdev(vdev);
  161. struct kvm_vqconfig *config;
  162. struct virtqueue *vq;
  163. int err;
  164. if (index >= kdev->desc->num_vq)
  165. return ERR_PTR(-ENOENT);
  166. config = kvm_vq_config(kdev->desc)+index;
  167. err = vmem_add_mapping(config->address,
  168. vring_size(config->num,
  169. KVM_S390_VIRTIO_RING_ALIGN));
  170. if (err)
  171. goto out;
  172. vq = vring_new_virtqueue(config->num, KVM_S390_VIRTIO_RING_ALIGN,
  173. vdev, (void *) config->address,
  174. kvm_notify, callback, name);
  175. if (!vq) {
  176. err = -ENOMEM;
  177. goto unmap;
  178. }
  179. /*
  180. * register a callback token
  181. * The host will sent this via the external interrupt parameter
  182. */
  183. config->token = (u64) vq;
  184. vq->priv = config;
  185. return vq;
  186. unmap:
  187. vmem_remove_mapping(config->address,
  188. vring_size(config->num,
  189. KVM_S390_VIRTIO_RING_ALIGN));
  190. out:
  191. return ERR_PTR(err);
  192. }
  193. static void kvm_del_vq(struct virtqueue *vq)
  194. {
  195. struct kvm_vqconfig *config = vq->priv;
  196. vring_del_virtqueue(vq);
  197. vmem_remove_mapping(config->address,
  198. vring_size(config->num,
  199. KVM_S390_VIRTIO_RING_ALIGN));
  200. }
  201. static void kvm_del_vqs(struct virtio_device *vdev)
  202. {
  203. struct virtqueue *vq, *n;
  204. list_for_each_entry_safe(vq, n, &vdev->vqs, list)
  205. kvm_del_vq(vq);
  206. }
  207. static int kvm_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  208. struct virtqueue *vqs[],
  209. vq_callback_t *callbacks[],
  210. const char *names[])
  211. {
  212. struct kvm_device *kdev = to_kvmdev(vdev);
  213. int i;
  214. /* We must have this many virtqueues. */
  215. if (nvqs > kdev->desc->num_vq)
  216. return -ENOENT;
  217. for (i = 0; i < nvqs; ++i) {
  218. vqs[i] = kvm_find_vq(vdev, i, callbacks[i], names[i]);
  219. if (IS_ERR(vqs[i]))
  220. goto error;
  221. }
  222. return 0;
  223. error:
  224. kvm_del_vqs(vdev);
  225. return PTR_ERR(vqs[i]);
  226. }
  227. /*
  228. * The config ops structure as defined by virtio config
  229. */
  230. static struct virtio_config_ops kvm_vq_configspace_ops = {
  231. .get_features = kvm_get_features,
  232. .finalize_features = kvm_finalize_features,
  233. .get = kvm_get,
  234. .set = kvm_set,
  235. .get_status = kvm_get_status,
  236. .set_status = kvm_set_status,
  237. .reset = kvm_reset,
  238. .find_vqs = kvm_find_vqs,
  239. .del_vqs = kvm_del_vqs,
  240. };
  241. /*
  242. * The root device for the kvm virtio devices.
  243. * This makes them appear as /sys/devices/kvm_s390/0,1,2 not /sys/devices/0,1,2.
  244. */
  245. static struct device *kvm_root;
  246. /*
  247. * adds a new device and register it with virtio
  248. * appropriate drivers are loaded by the device model
  249. */
  250. static void add_kvm_device(struct kvm_device_desc *d, unsigned int offset)
  251. {
  252. struct kvm_device *kdev;
  253. kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
  254. if (!kdev) {
  255. printk(KERN_EMERG "Cannot allocate kvm dev %u type %u\n",
  256. offset, d->type);
  257. return;
  258. }
  259. kdev->vdev.dev.parent = kvm_root;
  260. kdev->vdev.id.device = d->type;
  261. kdev->vdev.config = &kvm_vq_configspace_ops;
  262. kdev->desc = d;
  263. if (register_virtio_device(&kdev->vdev) != 0) {
  264. printk(KERN_ERR "Failed to register kvm device %u type %u\n",
  265. offset, d->type);
  266. kfree(kdev);
  267. }
  268. }
  269. /*
  270. * scan_devices() simply iterates through the device page.
  271. * The type 0 is reserved to mean "end of devices".
  272. */
  273. static void scan_devices(void)
  274. {
  275. unsigned int i;
  276. struct kvm_device_desc *d;
  277. for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
  278. d = kvm_devices + i;
  279. if (d->type == 0)
  280. break;
  281. add_kvm_device(d, i);
  282. }
  283. }
  284. /*
  285. * match for a kvm device with a specific desc pointer
  286. */
  287. static int match_desc(struct device *dev, void *data)
  288. {
  289. if ((ulong)to_kvmdev(dev_to_virtio(dev))->desc == (ulong)data)
  290. return 1;
  291. return 0;
  292. }
  293. /*
  294. * hotplug_device tries to find changes in the device page.
  295. */
  296. static void hotplug_devices(struct work_struct *dummy)
  297. {
  298. unsigned int i;
  299. struct kvm_device_desc *d;
  300. struct device *dev;
  301. for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
  302. d = kvm_devices + i;
  303. /* end of list */
  304. if (d->type == 0)
  305. break;
  306. /* device already exists */
  307. dev = device_find_child(kvm_root, d, match_desc);
  308. if (dev) {
  309. /* XXX check for hotplug remove */
  310. put_device(dev);
  311. continue;
  312. }
  313. /* new device */
  314. printk(KERN_INFO "Adding new virtio device %p\n", d);
  315. add_kvm_device(d, i);
  316. }
  317. }
  318. /*
  319. * we emulate the request_irq behaviour on top of s390 extints
  320. */
  321. static void kvm_extint_handler(unsigned int ext_int_code,
  322. unsigned int param32, unsigned long param64)
  323. {
  324. struct virtqueue *vq;
  325. u16 subcode;
  326. u32 param;
  327. kstat_cpu(smp_processor_id()).irqs[EXTINT_VRT]++;
  328. subcode = ext_int_code >> 16;
  329. if ((subcode & 0xff00) != VIRTIO_SUBCODE_64)
  330. return;
  331. /* The LSB might be overloaded, we have to mask it */
  332. vq = (struct virtqueue *)(param64 & ~1UL);
  333. /* We use ext_params to decide what this interrupt means */
  334. param = param32 & VIRTIO_PARAM_MASK;
  335. switch (param) {
  336. case VIRTIO_PARAM_CONFIG_CHANGED:
  337. {
  338. struct virtio_driver *drv;
  339. drv = container_of(vq->vdev->dev.driver,
  340. struct virtio_driver, driver);
  341. if (drv->config_changed)
  342. drv->config_changed(vq->vdev);
  343. break;
  344. }
  345. case VIRTIO_PARAM_DEV_ADD:
  346. schedule_work(&hotplug_work);
  347. break;
  348. case VIRTIO_PARAM_VRING_INTERRUPT:
  349. default:
  350. vring_interrupt(0, vq);
  351. break;
  352. }
  353. }
  354. /*
  355. * Init function for virtio
  356. * devices are in a single page above top of "normal" mem
  357. */
  358. static int __init kvm_devices_init(void)
  359. {
  360. int rc;
  361. if (!MACHINE_IS_KVM)
  362. return -ENODEV;
  363. kvm_root = root_device_register("kvm_s390");
  364. if (IS_ERR(kvm_root)) {
  365. rc = PTR_ERR(kvm_root);
  366. printk(KERN_ERR "Could not register kvm_s390 root device");
  367. return rc;
  368. }
  369. rc = vmem_add_mapping(real_memory_size, PAGE_SIZE);
  370. if (rc) {
  371. root_device_unregister(kvm_root);
  372. return rc;
  373. }
  374. kvm_devices = (void *) real_memory_size;
  375. INIT_WORK(&hotplug_work, hotplug_devices);
  376. ctl_set_bit(0, 9);
  377. register_external_interrupt(0x2603, kvm_extint_handler);
  378. scan_devices();
  379. return 0;
  380. }
  381. /* code for early console output with virtio_console */
  382. static __init int early_put_chars(u32 vtermno, const char *buf, int count)
  383. {
  384. char scratch[17];
  385. unsigned int len = count;
  386. if (len > sizeof(scratch) - 1)
  387. len = sizeof(scratch) - 1;
  388. scratch[len] = '\0';
  389. memcpy(scratch, buf, len);
  390. kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, __pa(scratch));
  391. return len;
  392. }
  393. static int __init s390_virtio_console_init(void)
  394. {
  395. if (!MACHINE_IS_KVM)
  396. return -ENODEV;
  397. return virtio_cons_early_init(early_put_chars);
  398. }
  399. console_initcall(s390_virtio_console_init);
  400. /*
  401. * We do this after core stuff, but before the drivers.
  402. */
  403. postcore_initcall(kvm_devices_init);