kvm_virtio.c 12 KB

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