From: Ankit Agrawal Move the code to map the BAR to a separate function. This would be reused by the nvgrace-gpu module. Signed-off-by: Ankit Agrawal --- drivers/vfio/pci/vfio_pci_core.c | 38 ++++++++++++++++++++++---------- include/linux/vfio_pci_core.h | 1 + 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c index 29dcf78905a6..d1ff1c0aa727 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -1717,6 +1717,29 @@ static const struct vm_operations_struct vfio_pci_mmap_ops = { #endif }; +int vfio_pci_core_barmap(struct vfio_pci_core_device *vdev, unsigned int index) +{ + struct pci_dev *pdev = vdev->pdev; + int ret; + + if (vdev->barmap[index]) + return 0; + + ret = pci_request_selected_regions(pdev, + 1 << index, "vfio-pci"); + if (ret) + return ret; + + vdev->barmap[index] = pci_iomap(pdev, index, 0); + if (!vdev->barmap[index]) { + pci_release_selected_regions(pdev, 1 << index); + return -ENOMEM; + } + + return 0; +} +EXPORT_SYMBOL_GPL(vfio_pci_core_barmap); + int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma) { struct vfio_pci_core_device *vdev = @@ -1761,18 +1784,9 @@ int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma * Even though we don't make use of the barmap for the mmap, * we need to request the region and the barmap tracks that. */ - if (!vdev->barmap[index]) { - ret = pci_request_selected_regions(pdev, - 1 << index, "vfio-pci"); - if (ret) - return ret; - - vdev->barmap[index] = pci_iomap(pdev, index, 0); - if (!vdev->barmap[index]) { - pci_release_selected_regions(pdev, 1 << index); - return -ENOMEM; - } - } + ret = vfio_pci_core_barmap(vdev, index); + if (ret) + return ret; vma->vm_private_data = vdev; vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h index a097a66485b4..75f04d613e0c 100644 --- a/include/linux/vfio_pci_core.h +++ b/include/linux/vfio_pci_core.h @@ -121,6 +121,7 @@ ssize_t vfio_pci_core_write(struct vfio_device *core_vdev, const char __user *bu size_t count, loff_t *ppos); vm_fault_t vfio_pci_map_pfn(struct vm_fault *vmf, unsigned long pfn, unsigned int order); +int vfio_pci_core_barmap(struct vfio_pci_core_device *vdev, unsigned int index); int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma); void vfio_pci_core_request(struct vfio_device *core_vdev, unsigned int count); int vfio_pci_core_match(struct vfio_device *core_vdev, char *buf); -- 2.34.1