1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020 Intel Corporation
6 #include <linux/module.h>
8 #include <linux/virtio.h>
9 #include <linux/virtio_config.h>
10 #include <linux/logic_iomem.h>
11 #include <linux/irqdomain.h>
12 #include <linux/virtio_pcidev.h>
13 #include <linux/virtio-uml.h>
14 #include <linux/delay.h>
15 #include <linux/msi.h>
16 #include <asm/unaligned.h>
20 #define MAX_MSI_VECTORS 32
21 #define CFG_SPACE_SIZE 4096
23 /* for MSI-X we have a 32-bit payload */
24 #define MAX_IRQ_MSG_SIZE (sizeof(struct virtio_pcidev_msg) + sizeof(u32))
25 #define NUM_IRQ_MSGS 10
27 #define HANDLE_NO_FREE(ptr) ((void *)((unsigned long)(ptr) | 1))
28 #define HANDLE_IS_NO_FREE(ptr) ((unsigned long)(ptr) & 1)
30 struct um_pci_device {
31 struct virtio_device *vdev;
33 /* for now just standard BARs */
34 u8 resptr[PCI_STD_NUM_BARS];
36 struct virtqueue *cmd_vq, *irq_vq;
38 #define UM_PCI_STAT_WAITING 0
44 struct um_pci_device_reg {
45 struct um_pci_device *dev;
49 static struct pci_host_bridge *bridge;
50 static DEFINE_MUTEX(um_pci_mtx);
51 static struct um_pci_device_reg um_pci_devices[MAX_DEVICES];
52 static struct fwnode_handle *um_pci_fwnode;
53 static struct irq_domain *um_pci_inner_domain;
54 static struct irq_domain *um_pci_msi_domain;
55 static unsigned long um_pci_msi_used[BITS_TO_LONGS(MAX_MSI_VECTORS)];
57 #define UM_VIRT_PCI_MAXDELAY 40000
59 struct um_pci_message_buffer {
60 struct virtio_pcidev_msg hdr;
64 static struct um_pci_message_buffer __percpu *um_pci_msg_bufs;
66 static int um_pci_send_cmd(struct um_pci_device *dev,
67 struct virtio_pcidev_msg *cmd,
68 unsigned int cmd_size,
69 const void *extra, unsigned int extra_size,
70 void *out, unsigned int out_size)
72 struct scatterlist out_sg, extra_sg, in_sg;
73 struct scatterlist *sgs_list[] = {
75 [1] = extra ? &extra_sg : &in_sg,
76 [2] = extra ? &in_sg : NULL,
78 struct um_pci_message_buffer *buf;
83 if (WARN_ON(cmd_size < sizeof(*cmd) || cmd_size > sizeof(*buf)))
87 case VIRTIO_PCIDEV_OP_CFG_WRITE:
88 case VIRTIO_PCIDEV_OP_MMIO_WRITE:
89 case VIRTIO_PCIDEV_OP_MMIO_MEMSET:
90 /* in PCI, writes are posted, so don't wait */
99 buf = get_cpu_var(um_pci_msg_bufs);
101 memcpy(buf, cmd, cmd_size);
104 u8 *ncmd = kmalloc(cmd_size + extra_size, GFP_ATOMIC);
107 memcpy(ncmd, cmd, cmd_size);
109 memcpy(ncmd + cmd_size, extra, extra_size);
111 cmd_size += extra_size;
115 /* try without allocating memory */
123 sg_init_one(&out_sg, cmd, cmd_size);
125 sg_init_one(&extra_sg, extra, extra_size);
127 sg_init_one(&in_sg, out, out_size);
129 /* add to internal virtio queue */
130 ret = virtqueue_add_sgs(dev->cmd_vq, sgs_list,
133 posted ? cmd : HANDLE_NO_FREE(cmd),
139 virtqueue_kick(dev->cmd_vq);
144 /* kick and poll for getting a response on the queue */
145 set_bit(UM_PCI_STAT_WAITING, &dev->status);
146 virtqueue_kick(dev->cmd_vq);
149 void *completed = virtqueue_get_buf(dev->cmd_vq, &len);
151 if (completed == HANDLE_NO_FREE(cmd))
154 if (completed && !HANDLE_IS_NO_FREE(completed))
157 if (WARN_ONCE(virtqueue_is_broken(dev->cmd_vq) ||
158 ++delay_count > UM_VIRT_PCI_MAXDELAY,
159 "um virt-pci delay: %d", delay_count)) {
165 clear_bit(UM_PCI_STAT_WAITING, &dev->status);
168 put_cpu_var(um_pci_msg_bufs);
172 static unsigned long um_pci_cfgspace_read(void *priv, unsigned int offset,
175 struct um_pci_device_reg *reg = priv;
176 struct um_pci_device *dev = reg->dev;
177 struct virtio_pcidev_msg hdr = {
178 .op = VIRTIO_PCIDEV_OP_CFG_READ,
182 /* buf->data is maximum size - we may only use parts of it */
183 struct um_pci_message_buffer *buf;
185 unsigned long ret = ULONG_MAX;
186 size_t bytes = sizeof(buf->data);
191 buf = get_cpu_var(um_pci_msg_bufs);
195 memset(data, 0xff, bytes);
206 WARN(1, "invalid config space read size %d\n", size);
210 if (um_pci_send_cmd(dev, &hdr, sizeof(hdr), NULL, 0, data, bytes))
218 ret = le16_to_cpup((void *)data);
221 ret = le32_to_cpup((void *)data);
225 ret = le64_to_cpup((void *)data);
233 put_cpu_var(um_pci_msg_bufs);
237 static void um_pci_cfgspace_write(void *priv, unsigned int offset, int size,
240 struct um_pci_device_reg *reg = priv;
241 struct um_pci_device *dev = reg->dev;
243 struct virtio_pcidev_msg hdr;
244 /* maximum size - we may only use parts of it */
248 .op = VIRTIO_PCIDEV_OP_CFG_WRITE,
259 msg.data[0] = (u8)val;
262 put_unaligned_le16(val, (void *)msg.data);
265 put_unaligned_le32(val, (void *)msg.data);
269 put_unaligned_le64(val, (void *)msg.data);
273 WARN(1, "invalid config space write size %d\n", size);
277 WARN_ON(um_pci_send_cmd(dev, &msg.hdr, sizeof(msg), NULL, 0, NULL, 0));
280 static const struct logic_iomem_ops um_pci_device_cfgspace_ops = {
281 .read = um_pci_cfgspace_read,
282 .write = um_pci_cfgspace_write,
285 static void um_pci_bar_copy_from(void *priv, void *buffer,
286 unsigned int offset, int size)
289 struct um_pci_device *dev = container_of(resptr - *resptr,
290 struct um_pci_device,
292 struct virtio_pcidev_msg hdr = {
293 .op = VIRTIO_PCIDEV_OP_MMIO_READ,
299 memset(buffer, 0xff, size);
301 um_pci_send_cmd(dev, &hdr, sizeof(hdr), NULL, 0, buffer, size);
304 static unsigned long um_pci_bar_read(void *priv, unsigned int offset,
307 /* buf->data is maximum size - we may only use parts of it */
308 struct um_pci_message_buffer *buf;
310 unsigned long ret = ULONG_MAX;
312 buf = get_cpu_var(um_pci_msg_bufs);
324 WARN(1, "invalid config space read size %d\n", size);
328 um_pci_bar_copy_from(priv, data, offset, size);
335 ret = le16_to_cpup((void *)data);
338 ret = le32_to_cpup((void *)data);
342 ret = le64_to_cpup((void *)data);
350 put_cpu_var(um_pci_msg_bufs);
354 static void um_pci_bar_copy_to(void *priv, unsigned int offset,
355 const void *buffer, int size)
358 struct um_pci_device *dev = container_of(resptr - *resptr,
359 struct um_pci_device,
361 struct virtio_pcidev_msg hdr = {
362 .op = VIRTIO_PCIDEV_OP_MMIO_WRITE,
368 um_pci_send_cmd(dev, &hdr, sizeof(hdr), buffer, size, NULL, 0);
371 static void um_pci_bar_write(void *priv, unsigned int offset, int size,
374 /* maximum size - we may only use parts of it */
382 put_unaligned_le16(val, (void *)data);
385 put_unaligned_le32(val, (void *)data);
389 put_unaligned_le64(val, (void *)data);
393 WARN(1, "invalid config space write size %d\n", size);
397 um_pci_bar_copy_to(priv, offset, data, size);
400 static void um_pci_bar_set(void *priv, unsigned int offset, u8 value, int size)
403 struct um_pci_device *dev = container_of(resptr - *resptr,
404 struct um_pci_device,
407 struct virtio_pcidev_msg hdr;
411 .op = VIRTIO_PCIDEV_OP_CFG_WRITE,
419 um_pci_send_cmd(dev, &msg.hdr, sizeof(msg), NULL, 0, NULL, 0);
422 static const struct logic_iomem_ops um_pci_device_bar_ops = {
423 .read = um_pci_bar_read,
424 .write = um_pci_bar_write,
425 .set = um_pci_bar_set,
426 .copy_from = um_pci_bar_copy_from,
427 .copy_to = um_pci_bar_copy_to,
430 static void __iomem *um_pci_map_bus(struct pci_bus *bus, unsigned int devfn,
433 struct um_pci_device_reg *dev;
434 unsigned int busn = bus->number;
439 /* not allowing functions for now ... */
443 if (devfn / 8 >= ARRAY_SIZE(um_pci_devices))
446 dev = &um_pci_devices[devfn / 8];
450 return (void __iomem *)((unsigned long)dev->iomem + where);
453 static struct pci_ops um_pci_ops = {
454 .map_bus = um_pci_map_bus,
455 .read = pci_generic_config_read,
456 .write = pci_generic_config_write,
459 static void um_pci_rescan(void)
461 pci_lock_rescan_remove();
462 pci_rescan_bus(bridge->bus);
463 pci_unlock_rescan_remove();
466 static void um_pci_irq_vq_addbuf(struct virtqueue *vq, void *buf, bool kick)
468 struct scatterlist sg[1];
470 sg_init_one(sg, buf, MAX_IRQ_MSG_SIZE);
471 if (virtqueue_add_inbuf(vq, sg, 1, buf, GFP_ATOMIC))
477 static void um_pci_handle_irq_message(struct virtqueue *vq,
478 struct virtio_pcidev_msg *msg)
480 struct virtio_device *vdev = vq->vdev;
481 struct um_pci_device *dev = vdev->priv;
483 /* we should properly chain interrupts, but on ARCH=um we don't care */
486 case VIRTIO_PCIDEV_OP_INT:
487 generic_handle_irq(dev->irq);
489 case VIRTIO_PCIDEV_OP_MSI:
490 /* our MSI message is just the interrupt number */
491 if (msg->size == sizeof(u32))
492 generic_handle_irq(le32_to_cpup((void *)msg->data));
494 generic_handle_irq(le16_to_cpup((void *)msg->data));
496 case VIRTIO_PCIDEV_OP_PME:
497 /* nothing to do - we already woke up due to the message */
500 dev_err(&vdev->dev, "unexpected virt-pci message %d\n", msg->op);
505 static void um_pci_cmd_vq_cb(struct virtqueue *vq)
507 struct virtio_device *vdev = vq->vdev;
508 struct um_pci_device *dev = vdev->priv;
512 if (test_bit(UM_PCI_STAT_WAITING, &dev->status))
515 while ((cmd = virtqueue_get_buf(vq, &len))) {
516 if (WARN_ON(HANDLE_IS_NO_FREE(cmd)))
522 static void um_pci_irq_vq_cb(struct virtqueue *vq)
524 struct virtio_pcidev_msg *msg;
527 while ((msg = virtqueue_get_buf(vq, &len))) {
528 if (len >= sizeof(*msg))
529 um_pci_handle_irq_message(vq, msg);
531 /* recycle the message buffer */
532 um_pci_irq_vq_addbuf(vq, msg, true);
536 static int um_pci_init_vqs(struct um_pci_device *dev)
538 struct virtqueue *vqs[2];
539 static const char *const names[2] = { "cmd", "irq" };
540 vq_callback_t *cbs[2] = { um_pci_cmd_vq_cb, um_pci_irq_vq_cb };
543 err = virtio_find_vqs(dev->vdev, 2, vqs, cbs, names, NULL);
547 dev->cmd_vq = vqs[0];
548 dev->irq_vq = vqs[1];
550 virtio_device_ready(dev->vdev);
552 for (i = 0; i < NUM_IRQ_MSGS; i++) {
553 void *msg = kzalloc(MAX_IRQ_MSG_SIZE, GFP_KERNEL);
556 um_pci_irq_vq_addbuf(dev->irq_vq, msg, false);
559 virtqueue_kick(dev->irq_vq);
564 static int um_pci_virtio_probe(struct virtio_device *vdev)
566 struct um_pci_device *dev;
570 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
577 mutex_lock(&um_pci_mtx);
578 for (i = 0; i < MAX_DEVICES; i++) {
579 if (um_pci_devices[i].dev)
588 err = um_pci_init_vqs(dev);
592 dev->irq = irq_alloc_desc(numa_node_id());
597 um_pci_devices[free].dev = dev;
600 mutex_unlock(&um_pci_mtx);
602 device_set_wakeup_enable(&vdev->dev, true);
605 * In order to do suspend-resume properly, don't allow VQs
608 virtio_uml_set_no_vq_suspend(vdev, true);
613 virtio_reset_device(vdev);
614 vdev->config->del_vqs(vdev);
616 mutex_unlock(&um_pci_mtx);
621 static void um_pci_virtio_remove(struct virtio_device *vdev)
623 struct um_pci_device *dev = vdev->priv;
626 /* Stop all virtqueues */
627 virtio_reset_device(vdev);
628 vdev->config->del_vqs(vdev);
630 device_set_wakeup_enable(&vdev->dev, false);
632 mutex_lock(&um_pci_mtx);
633 for (i = 0; i < MAX_DEVICES; i++) {
634 if (um_pci_devices[i].dev != dev)
636 um_pci_devices[i].dev = NULL;
637 irq_free_desc(dev->irq);
639 mutex_unlock(&um_pci_mtx);
646 static struct virtio_device_id id_table[] = {
647 { CONFIG_UML_PCI_OVER_VIRTIO_DEVICE_ID, VIRTIO_DEV_ANY_ID },
650 MODULE_DEVICE_TABLE(virtio, id_table);
652 static struct virtio_driver um_pci_virtio_driver = {
653 .driver.name = "virtio-pci",
654 .driver.owner = THIS_MODULE,
655 .id_table = id_table,
656 .probe = um_pci_virtio_probe,
657 .remove = um_pci_virtio_remove,
660 static struct resource virt_cfgspace_resource = {
661 .name = "PCI config space",
662 .start = 0xf0000000 - MAX_DEVICES * CFG_SPACE_SIZE,
663 .end = 0xf0000000 - 1,
664 .flags = IORESOURCE_MEM,
667 static long um_pci_map_cfgspace(unsigned long offset, size_t size,
668 const struct logic_iomem_ops **ops,
671 if (WARN_ON(size > CFG_SPACE_SIZE || offset % CFG_SPACE_SIZE))
674 if (offset / CFG_SPACE_SIZE < MAX_DEVICES) {
675 *ops = &um_pci_device_cfgspace_ops;
676 *priv = &um_pci_devices[offset / CFG_SPACE_SIZE];
680 WARN(1, "cannot map offset 0x%lx/0x%zx\n", offset, size);
684 static const struct logic_iomem_region_ops um_pci_cfgspace_ops = {
685 .map = um_pci_map_cfgspace,
688 static struct resource virt_iomem_resource = {
692 .flags = IORESOURCE_MEM,
695 struct um_pci_map_iomem_data {
696 unsigned long offset;
698 const struct logic_iomem_ops **ops;
703 static int um_pci_map_iomem_walk(struct pci_dev *pdev, void *_data)
705 struct um_pci_map_iomem_data *data = _data;
706 struct um_pci_device_reg *reg = &um_pci_devices[pdev->devfn / 8];
707 struct um_pci_device *dev;
713 for (i = 0; i < ARRAY_SIZE(dev->resptr); i++) {
714 struct resource *r = &pdev->resource[i];
716 if ((r->flags & IORESOURCE_TYPE_BITS) != IORESOURCE_MEM)
720 * must be the whole or part of the resource,
721 * not allowed to only overlap
723 if (data->offset < r->start || data->offset > r->end)
725 if (data->offset + data->size - 1 > r->end)
729 *data->ops = &um_pci_device_bar_ops;
731 *data->priv = &dev->resptr[i];
732 data->ret = data->offset - r->start;
734 /* no need to continue */
741 static long um_pci_map_iomem(unsigned long offset, size_t size,
742 const struct logic_iomem_ops **ops,
745 struct um_pci_map_iomem_data data = {
746 /* we want the full address here */
747 .offset = offset + virt_iomem_resource.start,
754 pci_walk_bus(bridge->bus, um_pci_map_iomem_walk, &data);
758 static const struct logic_iomem_region_ops um_pci_iomem_ops = {
759 .map = um_pci_map_iomem,
762 static void um_pci_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
765 * This is a very low address and not actually valid 'physical' memory
766 * in UML, so we can simply map MSI(-X) vectors to there, it cannot be
767 * legitimately written to by the device in any other way.
768 * We use the (virtual) IRQ number here as the message to simplify the
769 * code that receives the message, where for now we simply trust the
770 * device to send the correct message.
773 msg->address_lo = 0xa0000;
774 msg->data = data->irq;
777 static struct irq_chip um_pci_msi_bottom_irq_chip = {
778 .name = "UM virtio MSI",
779 .irq_compose_msi_msg = um_pci_compose_msi_msg,
782 static int um_pci_inner_domain_alloc(struct irq_domain *domain,
783 unsigned int virq, unsigned int nr_irqs,
788 WARN_ON(nr_irqs != 1);
790 mutex_lock(&um_pci_mtx);
791 bit = find_first_zero_bit(um_pci_msi_used, MAX_MSI_VECTORS);
792 if (bit >= MAX_MSI_VECTORS) {
793 mutex_unlock(&um_pci_mtx);
797 set_bit(bit, um_pci_msi_used);
798 mutex_unlock(&um_pci_mtx);
800 irq_domain_set_info(domain, virq, bit, &um_pci_msi_bottom_irq_chip,
801 domain->host_data, handle_simple_irq,
807 static void um_pci_inner_domain_free(struct irq_domain *domain,
808 unsigned int virq, unsigned int nr_irqs)
810 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
812 mutex_lock(&um_pci_mtx);
814 if (!test_bit(d->hwirq, um_pci_msi_used))
815 pr_err("trying to free unused MSI#%lu\n", d->hwirq);
817 __clear_bit(d->hwirq, um_pci_msi_used);
819 mutex_unlock(&um_pci_mtx);
822 static const struct irq_domain_ops um_pci_inner_domain_ops = {
823 .alloc = um_pci_inner_domain_alloc,
824 .free = um_pci_inner_domain_free,
827 static struct irq_chip um_pci_msi_irq_chip = {
828 .name = "UM virtio PCIe MSI",
829 .irq_mask = pci_msi_mask_irq,
830 .irq_unmask = pci_msi_unmask_irq,
833 static struct msi_domain_info um_pci_msi_domain_info = {
834 .flags = MSI_FLAG_USE_DEF_DOM_OPS |
835 MSI_FLAG_USE_DEF_CHIP_OPS |
837 .chip = &um_pci_msi_irq_chip,
840 static struct resource busn_resource = {
844 .flags = IORESOURCE_BUS,
847 static int um_pci_map_irq(const struct pci_dev *pdev, u8 slot, u8 pin)
849 struct um_pci_device_reg *reg = &um_pci_devices[pdev->devfn / 8];
851 if (WARN_ON(!reg->dev))
854 /* Yes, we map all pins to the same IRQ ... doesn't matter for now. */
855 return reg->dev->irq;
858 void *pci_root_bus_fwnode(struct pci_bus *bus)
860 return um_pci_fwnode;
863 static int __init um_pci_init(void)
867 WARN_ON(logic_iomem_add_region(&virt_cfgspace_resource,
868 &um_pci_cfgspace_ops));
869 WARN_ON(logic_iomem_add_region(&virt_iomem_resource,
872 if (WARN(CONFIG_UML_PCI_OVER_VIRTIO_DEVICE_ID < 0,
873 "No virtio device ID configured for PCI - no PCI support\n"))
876 um_pci_msg_bufs = alloc_percpu(struct um_pci_message_buffer);
877 if (!um_pci_msg_bufs)
880 bridge = pci_alloc_host_bridge(0);
886 um_pci_fwnode = irq_domain_alloc_named_fwnode("um-pci");
887 if (!um_pci_fwnode) {
892 um_pci_inner_domain = __irq_domain_add(um_pci_fwnode, MAX_MSI_VECTORS,
894 &um_pci_inner_domain_ops, NULL);
895 if (!um_pci_inner_domain) {
900 um_pci_msi_domain = pci_msi_create_irq_domain(um_pci_fwnode,
901 &um_pci_msi_domain_info,
902 um_pci_inner_domain);
903 if (!um_pci_msi_domain) {
908 pci_add_resource(&bridge->windows, &virt_iomem_resource);
909 pci_add_resource(&bridge->windows, &busn_resource);
910 bridge->ops = &um_pci_ops;
911 bridge->map_irq = um_pci_map_irq;
913 for (i = 0; i < MAX_DEVICES; i++) {
914 resource_size_t start;
916 start = virt_cfgspace_resource.start + i * CFG_SPACE_SIZE;
917 um_pci_devices[i].iomem = ioremap(start, CFG_SPACE_SIZE);
918 if (WARN(!um_pci_devices[i].iomem, "failed to map %d\n", i)) {
924 err = pci_host_probe(bridge);
928 err = register_virtio_driver(&um_pci_virtio_driver);
933 if (um_pci_inner_domain)
934 irq_domain_remove(um_pci_inner_domain);
936 irq_domain_free_fwnode(um_pci_fwnode);
938 pci_free_resource_list(&bridge->windows);
939 pci_free_host_bridge(bridge);
941 free_percpu(um_pci_msg_bufs);
944 module_init(um_pci_init);
946 static void __exit um_pci_exit(void)
948 unregister_virtio_driver(&um_pci_virtio_driver);
949 irq_domain_remove(um_pci_msi_domain);
950 irq_domain_remove(um_pci_inner_domain);
951 pci_free_resource_list(&bridge->windows);
952 pci_free_host_bridge(bridge);
953 free_percpu(um_pci_msg_bufs);
955 module_exit(um_pci_exit);