kvm_virtio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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/export.h>
  23. #include <linux/pfn.h>
  24. #include <asm/io.h>
  25. #include <asm/kvm_para.h>
  26. #include <asm/kvm_virtio.h>
  27. #include <asm/setup.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. static 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, true, (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. static const char *kvm_bus_name(struct virtio_device *vdev)
  228. {
  229. return "";
  230. }
  231. /*
  232. * The config ops structure as defined by virtio config
  233. */
  234. static struct virtio_config_ops kvm_vq_configspace_ops = {
  235. .get_features = kvm_get_features,
  236. .finalize_features = kvm_finalize_features,
  237. .get = kvm_get,
  238. .set = kvm_set,
  239. .get_status = kvm_get_status,
  240. .set_status = kvm_set_status,
  241. .reset = kvm_reset,
  242. .find_vqs = kvm_find_vqs,
  243. .del_vqs = kvm_del_vqs,
  244. .bus_name = kvm_bus_name,
  245. };
  246. /*
  247. * The root device for the kvm virtio devices.
  248. * This makes them appear as /sys/devices/kvm_s390/0,1,2 not /sys/devices/0,1,2.
  249. */
  250. static struct device *kvm_root;
  251. /*
  252. * adds a new device and register it with virtio
  253. * appropriate drivers are loaded by the device model
  254. */
  255. static void add_kvm_device(struct kvm_device_desc *d, unsigned int offset)
  256. {
  257. struct kvm_device *kdev;
  258. kdev = kzalloc(sizeof(*kdev), GFP_KERNEL);
  259. if (!kdev) {
  260. printk(KERN_EMERG "Cannot allocate kvm dev %u type %u\n",
  261. offset, d->type);
  262. return;
  263. }
  264. kdev->vdev.dev.parent = kvm_root;
  265. kdev->vdev.id.device = d->type;
  266. kdev->vdev.config = &kvm_vq_configspace_ops;
  267. kdev->desc = d;
  268. if (register_virtio_device(&kdev->vdev) != 0) {
  269. printk(KERN_ERR "Failed to register kvm device %u type %u\n",
  270. offset, d->type);
  271. kfree(kdev);
  272. }
  273. }
  274. /*
  275. * scan_devices() simply iterates through the device page.
  276. * The type 0 is reserved to mean "end of devices".
  277. */
  278. static void scan_devices(void)
  279. {
  280. unsigned int i;
  281. struct kvm_device_desc *d;
  282. for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
  283. d = kvm_devices + i;
  284. if (d->type == 0)
  285. break;
  286. add_kvm_device(d, i);
  287. }
  288. }
  289. /*
  290. * match for a kvm device with a specific desc pointer
  291. */
  292. static int match_desc(struct device *dev, void *data)
  293. {
  294. struct virtio_device *vdev = dev_to_virtio(dev);
  295. struct kvm_device *kdev = to_kvmdev(vdev);
  296. return kdev->desc == data;
  297. }
  298. /*
  299. * hotplug_device tries to find changes in the device page.
  300. */
  301. static void hotplug_devices(struct work_struct *dummy)
  302. {
  303. unsigned int i;
  304. struct kvm_device_desc *d;
  305. struct device *dev;
  306. for (i = 0; i < PAGE_SIZE; i += desc_size(d)) {
  307. d = kvm_devices + i;
  308. /* end of list */
  309. if (d->type == 0)
  310. break;
  311. /* device already exists */
  312. dev = device_find_child(kvm_root, d, match_desc);
  313. if (dev) {
  314. /* XXX check for hotplug remove */
  315. put_device(dev);
  316. continue;
  317. }
  318. /* new device */
  319. printk(KERN_INFO "Adding new virtio device %p\n", d);
  320. add_kvm_device(d, i);
  321. }
  322. }
  323. /*
  324. * we emulate the request_irq behaviour on top of s390 extints
  325. */
  326. static void kvm_extint_handler(struct ext_code ext_code,
  327. unsigned int param32, unsigned long param64)
  328. {
  329. struct virtqueue *vq;
  330. u32 param;
  331. if ((ext_code.subcode & 0xff00) != VIRTIO_SUBCODE_64)
  332. return;
  333. kstat_cpu(smp_processor_id()).irqs[EXTINT_VRT]++;
  334. /* The LSB might be overloaded, we have to mask it */
  335. vq = (struct virtqueue *)(param64 & ~1UL);
  336. /* We use ext_params to decide what this interrupt means */
  337. param = param32 & VIRTIO_PARAM_MASK;
  338. switch (param) {
  339. case VIRTIO_PARAM_CONFIG_CHANGED:
  340. {
  341. struct virtio_driver *drv;
  342. drv = container_of(vq->vdev->dev.driver,
  343. struct virtio_driver, driver);
  344. if (drv->config_changed)
  345. drv->config_changed(vq->vdev);
  346. break;
  347. }
  348. case VIRTIO_PARAM_DEV_ADD:
  349. schedule_work(&hotplug_work);
  350. break;
  351. case VIRTIO_PARAM_VRING_INTERRUPT:
  352. default:
  353. vring_interrupt(0, vq);
  354. break;
  355. }
  356. }
  357. /*
  358. * For s390-virtio, we expect a page above main storage containing
  359. * the virtio configuration. Try to actually load from this area
  360. * in order to figure out if the host provides this page.
  361. */
  362. static int __init test_devices_support(unsigned long addr)
  363. {
  364. int ret = -EIO;
  365. asm volatile(
  366. "0: lura 0,%1\n"
  367. "1: xgr %0,%0\n"
  368. "2:\n"
  369. EX_TABLE(0b,2b)
  370. EX_TABLE(1b,2b)
  371. : "+d" (ret)
  372. : "a" (addr)
  373. : "0", "cc");
  374. return ret;
  375. }
  376. /*
  377. * Init function for virtio
  378. * devices are in a single page above top of "normal" mem
  379. */
  380. static int __init kvm_devices_init(void)
  381. {
  382. int rc;
  383. if (!MACHINE_IS_KVM)
  384. return -ENODEV;
  385. if (test_devices_support(real_memory_size) < 0)
  386. return -ENODEV;
  387. rc = vmem_add_mapping(real_memory_size, PAGE_SIZE);
  388. if (rc)
  389. return rc;
  390. kvm_devices = (void *) real_memory_size;
  391. kvm_root = root_device_register("kvm_s390");
  392. if (IS_ERR(kvm_root)) {
  393. rc = PTR_ERR(kvm_root);
  394. printk(KERN_ERR "Could not register kvm_s390 root device");
  395. vmem_remove_mapping(real_memory_size, PAGE_SIZE);
  396. return rc;
  397. }
  398. INIT_WORK(&hotplug_work, hotplug_devices);
  399. service_subclass_irq_register();
  400. register_external_interrupt(0x2603, kvm_extint_handler);
  401. scan_devices();
  402. return 0;
  403. }
  404. /* code for early console output with virtio_console */
  405. static __init int early_put_chars(u32 vtermno, const char *buf, int count)
  406. {
  407. char scratch[17];
  408. unsigned int len = count;
  409. if (len > sizeof(scratch) - 1)
  410. len = sizeof(scratch) - 1;
  411. scratch[len] = '\0';
  412. memcpy(scratch, buf, len);
  413. kvm_hypercall1(KVM_S390_VIRTIO_NOTIFY, __pa(scratch));
  414. return len;
  415. }
  416. static int __init s390_virtio_console_init(void)
  417. {
  418. if (!MACHINE_IS_KVM)
  419. return -ENODEV;
  420. return virtio_cons_early_init(early_put_chars);
  421. }
  422. console_initcall(s390_virtio_console_init);
  423. /*
  424. * We do this after core stuff, but before the drivers.
  425. */
  426. postcore_initcall(kvm_devices_init);