virtio_light.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include <linux/acpi.h>
  3. #include <linux/dma-mapping.h>
  4. #include <linux/highmem.h>
  5. #include <linux/interrupt.h>
  6. #include <linux/io.h>
  7. #include <linux/list.h>
  8. #include <linux/module.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/virtio.h>
  13. #include <linux/virtio_config.h>
  14. #include <uapi/linux/virtio_light.h>
  15. #include <linux/virtio_ring.h>
  16. #include <linux/delay.h>
  17. #include <linux/of_irq.h>
  18. /* The alignment to use between consumer and producer parts of vring.
  19. * * Currently hardcoded to the page size. */
  20. #define VIRTIO_LIGHT_VRING_ALIGN PAGE_SIZE
  21. #define to_virtio_light_device(_plat_dev) \
  22. container_of(_plat_dev, struct virtio_light_device, vdev)
  23. struct virtio_light_device {
  24. struct virtio_device vdev;
  25. struct platform_device *pdev;
  26. void __iomem *base;
  27. unsigned long version;
  28. /* a list of queues so we can dispatch IRQs */
  29. spinlock_t lock;
  30. struct list_head virtqueues;
  31. volatile unsigned char __iomem *backend_intr_reg;
  32. volatile unsigned char __iomem *frontend_intr_reg;
  33. u32 enable_intr;
  34. u32 clear_intr;
  35. };
  36. struct virtio_light_vq_info {
  37. /* the actual virtqueue */
  38. struct virtqueue *vq;
  39. /* the list node for the virtqueues list */
  40. struct list_head node;
  41. };
  42. static u8 vm_get_status(struct virtio_device *vdev)
  43. {
  44. struct virtio_light_device *vm_dev = to_virtio_light_device(vdev);
  45. return readl(vm_dev->base + VIRTIO_LIGHT_STATUS) & 0xff;
  46. }
  47. static void vm_set_status(struct virtio_device *vdev, u8 status)
  48. {
  49. struct virtio_light_device *vm_dev = to_virtio_light_device(vdev);
  50. /* We should never be setting status to 0. */
  51. BUG_ON(status == 0);
  52. writel(status, vm_dev->base + VIRTIO_LIGHT_STATUS);
  53. }
  54. static void vm_reset(struct virtio_device *vdev)
  55. {
  56. struct virtio_light_device *vm_dev = to_virtio_light_device(vdev);
  57. /* 0 status means a reset. */
  58. writel(0, vm_dev->base + VIRTIO_LIGHT_STATUS);
  59. }
  60. /* the notify function */
  61. static bool vm_notify(struct virtqueue *vq)
  62. {
  63. struct virtio_light_device *vm_dev = to_virtio_light_device(vq->vdev);
  64. #ifdef CONFIG_SOC_INT_SRC7
  65. writel(1, vm_dev->backend_intr_reg);
  66. #else
  67. /* Write the interrupt register to signal the other end */
  68. writel(1, vm_dev->backend_intr_reg + 0x10);
  69. #endif
  70. return true;
  71. }
  72. /* Notify all virtqueues on an interrupt. */
  73. static irqreturn_t vm_interrupt(int irq, void *opaque)
  74. {
  75. unsigned long flags;
  76. irqreturn_t ret = IRQ_NONE;
  77. struct virtio_light_vq_info *info;
  78. struct virtio_light_device *vm_dev = opaque;
  79. #ifdef CONFIG_SOC_INT_SRC7
  80. writel(0, vm_dev->frontend_intr_reg);
  81. #else
  82. /* clear interrupt */
  83. writel(vm_dev->clear_intr, vm_dev->frontend_intr_reg + 0x4);
  84. #endif
  85. spin_lock_irqsave(&vm_dev->lock, flags);
  86. list_for_each_entry(info, &vm_dev->virtqueues, node)
  87. ret |= vring_interrupt(irq, info->vq);
  88. spin_unlock_irqrestore(&vm_dev->lock, flags);
  89. if (ret == IRQ_NONE)
  90. dev_dbg(&vm_dev->vdev.dev, "vq has no data\n");
  91. return ret;
  92. }
  93. static void vm_del_vq(struct virtqueue *vq)
  94. {
  95. struct virtio_light_device *vm_dev = to_virtio_light_device(vq->vdev);
  96. struct virtio_light_vq_info *info = vq->priv;
  97. unsigned long flags;
  98. unsigned int index = vq->index;
  99. spin_lock_irqsave(&vm_dev->lock, flags);
  100. list_del(&info->node);
  101. spin_unlock_irqrestore(&vm_dev->lock, flags);
  102. writel(0, vm_dev->base + (VIRTIO_LIGHT_QUEUE_PFN + index * 4));
  103. vring_del_virtqueue(vq);
  104. kfree(info);
  105. }
  106. static void vm_del_vqs(struct virtio_device *vdev)
  107. {
  108. struct virtio_light_device *vm_dev = to_virtio_light_device(vdev);
  109. struct virtqueue *vq, *n;
  110. list_for_each_entry_safe(vq, n, &vdev->vqs, list)
  111. vm_del_vq(vq);
  112. free_irq(platform_get_irq(vm_dev->pdev, 0), vm_dev);
  113. }
  114. static struct virtqueue *vm_setup_vq(struct virtio_device *vdev, unsigned int index,
  115. void (*callback)(struct virtqueue *vq),
  116. const char *name, bool ctx)
  117. {
  118. struct virtio_light_device *vm_dev = to_virtio_light_device(vdev);
  119. struct virtio_light_vq_info *info;
  120. struct virtqueue *vq;
  121. unsigned long flags;
  122. unsigned int num;
  123. int err;
  124. u32 q_pfn;
  125. if (!name)
  126. return NULL;
  127. /* Allocate and fill out our active queue description */
  128. info = kmalloc(sizeof(*info), GFP_KERNEL);
  129. if (!info) {
  130. err = -ENOMEM;
  131. goto error_kmalloc;
  132. }
  133. num = readl(vm_dev->base + VIRTIO_LIGHT_QUEUE_SIZE_MAX);
  134. if (num == 0) {
  135. err = -ENOENT;
  136. goto error_new_virtqueue;
  137. }
  138. /* Create the vring */
  139. vq = vring_create_virtqueue(index, num, VIRTIO_LIGHT_VRING_ALIGN,
  140. vdev, true, true, ctx, vm_notify,
  141. callback, name);
  142. if (!vq) {
  143. err = -ENOMEM;
  144. goto error_new_virtqueue;
  145. }
  146. /* Activate the queue */
  147. writel(virtqueue_get_vring_size(vq), vm_dev->base + VIRTIO_LIGHT_QUEUE_SIZE);
  148. q_pfn = virtqueue_get_desc_addr(vq) >> PAGE_SHIFT;
  149. /* Tell virtio queue(index) addr to host */
  150. writel(q_pfn, vm_dev->base + (VIRTIO_LIGHT_QUEUE_PFN + index * 4));
  151. writel(PAGE_SIZE, vm_dev->base + VIRTIO_LIGHT_QUEUE_ALIGN);
  152. vq->priv = info;
  153. info->vq = vq;
  154. spin_lock_irqsave(&vm_dev->lock, flags);
  155. list_add(&info->node, &vm_dev->virtqueues);
  156. spin_unlock_irqrestore(&vm_dev->lock, flags);
  157. return vq;
  158. error_new_virtqueue:
  159. writel(0xdead, vm_dev->base + (VIRTIO_LIGHT_QUEUE_PFN + index * 4));
  160. kfree(info);
  161. error_kmalloc:
  162. return ERR_PTR(err);
  163. }
  164. static void vm_get(struct virtio_device *vdev, unsigned int offset,
  165. void *buf, unsigned int len)
  166. {
  167. struct virtio_light_device *vm_dev = to_virtio_light_device(vdev);
  168. void __iomem *base = vm_dev->base + VIRTIO_LIGHT_CONFIG;
  169. u8 b;
  170. __le16 w;
  171. __le32 l;
  172. if (vm_dev->version == 1) {
  173. u8 *ptr = buf;
  174. int i;
  175. for (i = 0; i < len; i++)
  176. ptr[i] = readb(base + offset + i);
  177. return;
  178. }
  179. switch (len) {
  180. case 1:
  181. b = readb(base + offset);
  182. memcpy(buf, &b, sizeof b);
  183. break;
  184. case 2:
  185. w = cpu_to_le16(readw(base + offset));
  186. memcpy(buf, &w, sizeof w);
  187. break;
  188. case 4:
  189. l = cpu_to_le32(readl(base + offset));
  190. memcpy(buf, &l, sizeof l);
  191. break;
  192. case 8:
  193. l = cpu_to_le32(readl(base + offset));
  194. memcpy(buf, &l, sizeof l);
  195. l = cpu_to_le32(ioread32(base + offset + sizeof l));
  196. memcpy(buf + sizeof l, &l, sizeof l);
  197. break;
  198. default:
  199. BUG();
  200. }
  201. }
  202. static void vm_set(struct virtio_device *vdev, unsigned int offset,
  203. const void *buf, unsigned int len)
  204. {
  205. struct virtio_light_device *vm_dev = to_virtio_light_device(vdev);
  206. void __iomem *base = vm_dev->base + VIRTIO_LIGHT_CONFIG;
  207. u8 b;
  208. __le16 w;
  209. __le32 l;
  210. if (vm_dev->version == 1) {
  211. const u8 *ptr = buf;
  212. int i;
  213. for (i = 0; i < len; i++)
  214. writeb(ptr[i], base + offset + i);
  215. return;
  216. }
  217. switch (len) {
  218. case 1:
  219. memcpy(&b, buf, sizeof b);
  220. writeb(b, base + offset);
  221. break;
  222. case 2:
  223. memcpy(&w, buf, sizeof w);
  224. writew(le16_to_cpu(w), base + offset);
  225. break;
  226. case 4:
  227. memcpy(&l, buf, sizeof l);
  228. writel(le32_to_cpu(l), base + offset);
  229. break;
  230. case 8:
  231. memcpy(&l, buf, sizeof l);
  232. writel(le32_to_cpu(l), base + offset);
  233. memcpy(&l, buf + sizeof l, sizeof l);
  234. writel(le32_to_cpu(l), base + offset + sizeof l);
  235. break;
  236. default:
  237. BUG();
  238. }
  239. }
  240. static int vm_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
  241. struct virtqueue *vqs[],
  242. vq_callback_t *callbacks[],
  243. const char * const names[],
  244. const bool *ctx,
  245. struct irq_affinity *desc)
  246. {
  247. int i, irq, err, queue_idx = 0;
  248. struct virtio_light_device *vm_dev = to_virtio_light_device(vdev);
  249. if (nvqs <= 0)
  250. return -EINVAL;
  251. irq = platform_get_irq(vm_dev->pdev, 0);
  252. if (irq < 0) {
  253. dev_err(&vdev->dev, "get irq resource failed for device %d\n",
  254. vm_dev->vdev.id.device);
  255. }
  256. err = request_irq(irq, vm_interrupt, IRQF_SHARED, dev_name(&vdev->dev), vm_dev);
  257. if (err) {
  258. dev_err(&vdev->dev, "irq register failed for device %d\n",
  259. vm_dev->vdev.id.device);
  260. return err;
  261. }
  262. for (i = 0; i < nvqs; ++i) {
  263. if (!names[i]) {
  264. vqs[i] = NULL;
  265. continue;
  266. }
  267. vqs[i] = vm_setup_vq(vdev, queue_idx++, callbacks[i], names[i],
  268. ctx ? ctx[i] : false);
  269. if (IS_ERR(vqs[i])) {
  270. vm_del_vqs(vdev);
  271. return PTR_ERR(vqs[i]);
  272. }
  273. }
  274. writel(nvqs, vm_dev->base + VIRTIO_LIGHT_QUEUE_NUM);
  275. return 0;
  276. }
  277. static u64 vm_get_features(struct virtio_device *vdev)
  278. {
  279. struct virtio_light_device *vm_dev = to_virtio_light_device(vdev);
  280. u64 features;
  281. features = readl(vm_dev->base + VIRTIO_LIGHT_DEVICE_FEATURES_HIGH);
  282. features <<= 32;
  283. features = readl(vm_dev->base + VIRTIO_LIGHT_DEVICE_FEATURES_LOW);
  284. return features;
  285. }
  286. static int vm_finalize_features(struct virtio_device *vdev)
  287. {
  288. return 0;
  289. }
  290. static const char *vm_bus_name(struct virtio_device *vdev)
  291. {
  292. struct virtio_light_device *vm_dev = to_virtio_light_device(vdev);
  293. return vm_dev->pdev->name;
  294. }
  295. static const struct virtio_config_ops virtio_light_config_ops = {
  296. .get = vm_get,
  297. .set = vm_set,
  298. .get_status = vm_get_status,
  299. .set_status = vm_set_status,
  300. .reset = vm_reset,
  301. .find_vqs = vm_find_vqs,
  302. .del_vqs = vm_del_vqs,
  303. .get_features = vm_get_features,
  304. .finalize_features = vm_finalize_features,
  305. .bus_name = vm_bus_name,
  306. };
  307. static void virtio_light_release_dev(struct device *_d)
  308. {
  309. struct virtio_device *vdev =
  310. container_of(_d, struct virtio_device, dev);
  311. struct virtio_light_device *vm_dev =
  312. container_of(vdev, struct virtio_light_device, vdev);
  313. struct platform_device *pdev = vm_dev->pdev;
  314. devm_kfree(&pdev->dev, vm_dev);
  315. }
  316. /* Platform device */
  317. static int virtio_light_probe(struct platform_device *pdev)
  318. {
  319. struct virtio_light_device *vm_dev;
  320. struct device *dev = &pdev->dev;
  321. unsigned long magic;
  322. int rc;
  323. vm_dev = devm_kzalloc(dev, sizeof(*vm_dev), GFP_KERNEL);
  324. if (!vm_dev)
  325. return -ENOMEM;
  326. vm_dev->vdev.dev.parent = dev;
  327. vm_dev->vdev.dev.release = virtio_light_release_dev;
  328. vm_dev->vdev.config = &virtio_light_config_ops;
  329. vm_dev->pdev = pdev;
  330. INIT_LIST_HEAD(&vm_dev->virtqueues);
  331. spin_lock_init(&vm_dev->lock);
  332. vm_dev->base = devm_platform_ioremap_resource(pdev, 0);
  333. if (IS_ERR(vm_dev->base))
  334. return PTR_ERR(vm_dev->base);
  335. /* Check magic value */
  336. magic = readl(vm_dev->base + VIRTIO_LIGHT_MAGIC_VALUE);
  337. if (magic != ('v' | 'i' << 8 | 'r' << 16 | 't' << 24)) {
  338. dev_warn(&pdev->dev, "Wrong magic value 0x%08lx!\n", magic);
  339. return -ENODEV;
  340. }
  341. /* Check device version */
  342. vm_dev->version = readl(vm_dev->base + VIRTIO_LIGHT_VERSION);
  343. if (vm_dev->version < 1 || vm_dev->version > 2) {
  344. dev_err(&pdev->dev, "Version %ld not supported!\n",
  345. vm_dev->version);
  346. return -ENXIO;
  347. }
  348. vm_dev->vdev.id.device = readl(vm_dev->base + VIRTIO_LIGHT_DEVICE_ID);
  349. if (vm_dev->vdev.id.device == 0) {
  350. /*
  351. * virtio-mmio device with an ID 0 is a (dummy) placeholder
  352. * with no function. End probing now with no error reported.
  353. */
  354. return -ENODEV;
  355. }
  356. vm_dev->vdev.id.vendor = readl(vm_dev->base + VIRTIO_LIGHT_VENDOR_ID);
  357. #ifdef CONFIG_SOC_INT_SRC7
  358. vm_dev->frontend_intr_reg = ioremap(0xFFEF018094, 4);
  359. vm_dev->backend_intr_reg = ioremap(0xFFFF019094, 4);
  360. #else
  361. /* MPW use mailbox as interrupt */
  362. vm_dev->backend_intr_reg = ioremap(0xffffc3b000, 0x100);
  363. vm_dev->frontend_intr_reg = ioremap(0xffefc50000, 0x100);
  364. vm_dev->enable_intr = 1;
  365. vm_dev->clear_intr = 1;
  366. writel(vm_dev->enable_intr, vm_dev->backend_intr_reg + 0xc);
  367. writel(vm_dev->clear_intr, vm_dev->backend_intr_reg + 0x4);
  368. #endif
  369. dev_info(&vm_dev->vdev.dev, "virtio_light: detected a virtual device with id %d\n",
  370. vm_dev->vdev.id.device);
  371. platform_set_drvdata(pdev, vm_dev);
  372. rc = register_virtio_device(&vm_dev->vdev);
  373. if (rc)
  374. put_device(&vm_dev->vdev.dev);
  375. return rc;
  376. }
  377. static int virtio_light_remove(struct platform_device *pdev)
  378. {
  379. struct virtio_light_device *vm_dev = platform_get_drvdata(pdev);
  380. unregister_virtio_device(&vm_dev->vdev);
  381. return 0;
  382. }
  383. /* Platform driver */
  384. static const struct of_device_id virtio_light_match[] = {
  385. { .compatible = "virtio,light", },
  386. {},
  387. };
  388. MODULE_DEVICE_TABLE(of, virtio_light_match);
  389. static struct platform_driver virtio_light_driver = {
  390. .probe = virtio_light_probe,
  391. .remove = virtio_light_remove,
  392. .driver = {
  393. .name = "virtio-light",
  394. .of_match_table = virtio_light_match,
  395. },
  396. };
  397. static int __init virtio_light_init(void)
  398. {
  399. return platform_driver_register(&virtio_light_driver);
  400. }
  401. static void __exit virtio_light_exit(void)
  402. {
  403. platform_driver_unregister(&virtio_light_driver);
  404. }
  405. module_init(virtio_light_init);
  406. module_exit(virtio_light_exit);
  407. MODULE_DESCRIPTION("Platform bus driver for light virtio devices");
  408. MODULE_AUTHOR("Xianting Tian <xianting.tian@linux.alibaba.com>");
  409. MODULE_LICENSE("GPL");