kvm_virtio.c 12 KB

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