]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blobdiff - drivers/misc/vmw_vmci/vmci_guest.c
VMCI: dma dg: register dummy IRQ handlers for DMA datagrams
[mirror_ubuntu-jammy-kernel.git] / drivers / misc / vmw_vmci / vmci_guest.c
index 1018dc77269d47b3079b0c9be6426295b5489fc5..acef19c562b32260b9c50fc4222a7b4b11f70704 100644 (file)
@@ -45,6 +45,7 @@ static u32 vm_context_id = VMCI_INVALID_ID;
 struct vmci_guest_device {
        struct device *dev;     /* PCI device we are attached to */
        void __iomem *iobase;
+       void __iomem *mmio_base;
 
        bool exclusive_vectors;
 
@@ -89,6 +90,21 @@ u32 vmci_get_vm_context_id(void)
        return vm_context_id;
 }
 
+static unsigned int vmci_read_reg(struct vmci_guest_device *dev, u32 reg)
+{
+       if (dev->mmio_base != NULL)
+               return readl(dev->mmio_base + reg);
+       return ioread32(dev->iobase + reg);
+}
+
+static void vmci_write_reg(struct vmci_guest_device *dev, u32 val, u32 reg)
+{
+       if (dev->mmio_base != NULL)
+               writel(val, dev->mmio_base + reg);
+       else
+               iowrite32(val, dev->iobase + reg);
+}
+
 /*
  * VM to hypervisor call mechanism. We use the standard VMware naming
  * convention since shared code is calling this function as well.
@@ -116,7 +132,7 @@ int vmci_send_datagram(struct vmci_datagram *dg)
        if (vmci_dev_g) {
                iowrite8_rep(vmci_dev_g->iobase + VMCI_DATA_OUT_ADDR,
                             dg, VMCI_DG_SIZE(dg));
-               result = ioread32(vmci_dev_g->iobase + VMCI_RESULT_LOW_ADDR);
+               result = vmci_read_reg(vmci_dev_g, VMCI_RESULT_LOW_ADDR);
        } else {
                result = VMCI_ERROR_UNAVAILABLE;
        }
@@ -384,7 +400,7 @@ static irqreturn_t vmci_interrupt(int irq, void *_dev)
                unsigned int icr;
 
                /* Acknowledge interrupt and determine what needs doing. */
-               icr = ioread32(dev->iobase + VMCI_ICR_ADDR);
+               icr = vmci_read_reg(dev, VMCI_ICR_ADDR);
                if (icr == 0 || icr == ~0)
                        return IRQ_NONE;
 
@@ -398,6 +414,9 @@ static irqreturn_t vmci_interrupt(int irq, void *_dev)
                        icr &= ~VMCI_ICR_NOTIFICATION;
                }
 
+               if (icr & VMCI_ICR_DMA_DATAGRAM)
+                       icr &= ~VMCI_ICR_DMA_DATAGRAM;
+
                if (icr != 0)
                        dev_warn(dev->dev,
                                 "Ignoring unknown interrupt cause (%d)\n",
@@ -422,6 +441,16 @@ static irqreturn_t vmci_interrupt_bm(int irq, void *_dev)
        return IRQ_HANDLED;
 }
 
+/*
+ * Interrupt handler for MSI-X interrupt vector VMCI_INTR_DMA_DATAGRAM,
+ * which is for the completion of a DMA datagram send or receive operation.
+ * Will only get called if we are using MSI-X with exclusive vectors.
+ */
+static irqreturn_t vmci_interrupt_dma_datagram(int irq, void *_dev)
+{
+       return IRQ_HANDLED;
+}
+
 /*
  * Most of the initialization at module load time is done here.
  */
@@ -429,7 +458,9 @@ static int vmci_guest_probe_device(struct pci_dev *pdev,
                                   const struct pci_device_id *id)
 {
        struct vmci_guest_device *vmci_dev;
-       void __iomem *iobase;
+       void __iomem *iobase = NULL;
+       void __iomem *mmio_base = NULL;
+       unsigned int num_irq_vectors;
        unsigned int capabilities;
        unsigned int caps_in_use;
        unsigned long cmd;
@@ -445,16 +476,29 @@ static int vmci_guest_probe_device(struct pci_dev *pdev,
                return error;
        }
 
-       error = pcim_iomap_regions(pdev, 1 << 0, KBUILD_MODNAME);
-       if (error) {
-               dev_err(&pdev->dev, "Failed to reserve/map IO regions\n");
-               return error;
-       }
+       /*
+        * The VMCI device with mmio access to registers requests 256KB
+        * for BAR1. If present, driver will use new VMCI device
+        * functionality for register access and datagram send/recv.
+        */
 
-       iobase = pcim_iomap_table(pdev)[0];
+       if (pci_resource_len(pdev, 1) == VMCI_WITH_MMIO_ACCESS_BAR_SIZE) {
+               dev_info(&pdev->dev, "MMIO register access is available\n");
+               mmio_base = pci_iomap_range(pdev, 1, VMCI_MMIO_ACCESS_OFFSET,
+                                           VMCI_MMIO_ACCESS_SIZE);
+               /* If the map fails, we fall back to IOIO access. */
+               if (!mmio_base)
+                       dev_warn(&pdev->dev, "Failed to map MMIO register access\n");
+       }
 
-       dev_info(&pdev->dev, "Found VMCI PCI device at %#lx, irq %u\n",
-                (unsigned long)iobase, pdev->irq);
+       if (!mmio_base) {
+               error = pcim_iomap_regions(pdev, BIT(0), KBUILD_MODNAME);
+               if (error) {
+                       dev_err(&pdev->dev, "Failed to reserve/map IO regions\n");
+                       return error;
+               }
+               iobase = pcim_iomap_table(pdev)[0];
+       }
 
        vmci_dev = devm_kzalloc(&pdev->dev, sizeof(*vmci_dev), GFP_KERNEL);
        if (!vmci_dev) {
@@ -466,6 +510,7 @@ static int vmci_guest_probe_device(struct pci_dev *pdev,
        vmci_dev->dev = &pdev->dev;
        vmci_dev->exclusive_vectors = false;
        vmci_dev->iobase = iobase;
+       vmci_dev->mmio_base = mmio_base;
 
        tasklet_init(&vmci_dev->datagram_tasklet,
                     vmci_dispatch_dgs, (unsigned long)vmci_dev);
@@ -490,7 +535,7 @@ static int vmci_guest_probe_device(struct pci_dev *pdev,
         *
         * Right now, we need datagrams. There are no fallbacks.
         */
-       capabilities = ioread32(vmci_dev->iobase + VMCI_CAPS_ADDR);
+       capabilities = vmci_read_reg(vmci_dev, VMCI_CAPS_ADDR);
        if (!(capabilities & VMCI_CAPS_DATAGRAM)) {
                dev_err(&pdev->dev, "Device does not support datagrams\n");
                error = -ENXIO;
@@ -531,10 +576,25 @@ static int vmci_guest_probe_device(struct pci_dev *pdev,
                }
        }
 
+       if (mmio_base != NULL) {
+               if (capabilities & VMCI_CAPS_DMA_DATAGRAM) {
+                       caps_in_use |= VMCI_CAPS_DMA_DATAGRAM;
+               } else {
+                       dev_err(&pdev->dev,
+                               "Missing capability: VMCI_CAPS_DMA_DATAGRAM\n");
+                       error = -ENXIO;
+                       goto err_free_data_buffer;
+               }
+       }
+
        dev_info(&pdev->dev, "Using capabilities 0x%x\n", caps_in_use);
 
        /* Let the host know which capabilities we intend to use. */
-       iowrite32(caps_in_use, vmci_dev->iobase + VMCI_CAPS_ADDR);
+       vmci_write_reg(vmci_dev, caps_in_use, VMCI_CAPS_ADDR);
+
+       /* Let the device know the size for pages passed down. */
+       if (caps_in_use & VMCI_CAPS_DMA_DATAGRAM)
+               vmci_write_reg(vmci_dev, PAGE_SHIFT, VMCI_GUEST_PAGE_SHIFT);
 
        /* Set up global device so that we can start sending datagrams */
        spin_lock_irq(&vmci_dev_spinlock);
@@ -581,8 +641,12 @@ static int vmci_guest_probe_device(struct pci_dev *pdev,
         * Enable interrupts.  Try MSI-X first, then MSI, and then fallback on
         * legacy interrupts.
         */
-       error = pci_alloc_irq_vectors(pdev, VMCI_MAX_INTRS, VMCI_MAX_INTRS,
-                       PCI_IRQ_MSIX);
+       if (vmci_dev->mmio_base != NULL)
+               num_irq_vectors = VMCI_MAX_INTRS;
+       else
+               num_irq_vectors = VMCI_MAX_INTRS_NOTIFICATION;
+       error = pci_alloc_irq_vectors(pdev, num_irq_vectors, num_irq_vectors,
+                                     PCI_IRQ_MSIX);
        if (error < 0) {
                error = pci_alloc_irq_vectors(pdev, 1, 1,
                                PCI_IRQ_MSIX | PCI_IRQ_MSI | PCI_IRQ_LEGACY);
@@ -620,6 +684,17 @@ static int vmci_guest_probe_device(struct pci_dev *pdev,
                                pci_irq_vector(pdev, 1), error);
                        goto err_free_irq;
                }
+               if (caps_in_use & VMCI_CAPS_DMA_DATAGRAM) {
+                       error = request_irq(pci_irq_vector(pdev, 2),
+                                           vmci_interrupt_dma_datagram,
+                                           0, KBUILD_MODNAME, vmci_dev);
+                       if (error) {
+                               dev_err(&pdev->dev,
+                                       "Failed to allocate irq %u: %d\n",
+                                       pci_irq_vector(pdev, 2), error);
+                               goto err_free_bm_irq;
+                       }
+               }
        }
 
        dev_dbg(&pdev->dev, "Registered device\n");
@@ -630,17 +705,20 @@ static int vmci_guest_probe_device(struct pci_dev *pdev,
        cmd = VMCI_IMR_DATAGRAM;
        if (caps_in_use & VMCI_CAPS_NOTIFICATIONS)
                cmd |= VMCI_IMR_NOTIFICATION;
-       iowrite32(cmd, vmci_dev->iobase + VMCI_IMR_ADDR);
+       if (caps_in_use & VMCI_CAPS_DMA_DATAGRAM)
+               cmd |= VMCI_IMR_DMA_DATAGRAM;
+       vmci_write_reg(vmci_dev, cmd, VMCI_IMR_ADDR);
 
        /* Enable interrupts. */
-       iowrite32(VMCI_CONTROL_INT_ENABLE,
-                 vmci_dev->iobase + VMCI_CONTROL_ADDR);
+       vmci_write_reg(vmci_dev, VMCI_CONTROL_INT_ENABLE, VMCI_CONTROL_ADDR);
 
        pci_set_drvdata(pdev, vmci_dev);
 
        vmci_call_vsock_callback(false);
        return 0;
 
+err_free_bm_irq:
+       free_irq(pci_irq_vector(pdev, 1), vmci_dev);
 err_free_irq:
        free_irq(pci_irq_vector(pdev, 0), vmci_dev);
        tasklet_kill(&vmci_dev->datagram_tasklet);
@@ -657,8 +735,7 @@ err_disable_msi:
 
 err_remove_bitmap:
        if (vmci_dev->notification_bitmap) {
-               iowrite32(VMCI_CONTROL_RESET,
-                         vmci_dev->iobase + VMCI_CONTROL_ADDR);
+               vmci_write_reg(vmci_dev, VMCI_CONTROL_RESET, VMCI_CONTROL_ADDR);
                dma_free_coherent(&pdev->dev, PAGE_SIZE,
                                  vmci_dev->notification_bitmap,
                                  vmci_dev->notification_base);
@@ -700,15 +777,18 @@ static void vmci_guest_remove_device(struct pci_dev *pdev)
        spin_unlock_irq(&vmci_dev_spinlock);
 
        dev_dbg(&pdev->dev, "Resetting vmci device\n");
-       iowrite32(VMCI_CONTROL_RESET, vmci_dev->iobase + VMCI_CONTROL_ADDR);
+       vmci_write_reg(vmci_dev, VMCI_CONTROL_RESET, VMCI_CONTROL_ADDR);
 
        /*
         * Free IRQ and then disable MSI/MSI-X as appropriate.  For
         * MSI-X, we might have multiple vectors, each with their own
         * IRQ, which we must free too.
         */
-       if (vmci_dev->exclusive_vectors)
+       if (vmci_dev->exclusive_vectors) {
                free_irq(pci_irq_vector(pdev, 1), vmci_dev);
+               if (vmci_dev->mmio_base != NULL)
+                       free_irq(pci_irq_vector(pdev, 2), vmci_dev);
+       }
        free_irq(pci_irq_vector(pdev, 0), vmci_dev);
        pci_free_irq_vectors(pdev);