kvm_virtio.c 11 KB

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