1 // SPDX-License-Identifier: GPL-2.0
3 * Generic PCI resource mmap helper
5 * Copyright © 2017 Amazon.com, Inc. or its affiliates.
7 * Author: David Woodhouse <dwmw2@infradead.org>
10 #include <linux/kernel.h>
12 #include <linux/pci.h>
14 #ifdef ARCH_GENERIC_PCI_MMAP_RESOURCE
16 static const struct vm_operations_struct pci_phys_vm_ops = {
17 #ifdef CONFIG_HAVE_IOREMAP_PROT
18 .access = generic_access_phys,
22 int pci_mmap_resource_range(struct pci_dev *pdev, int bar,
23 struct vm_area_struct *vma,
24 enum pci_mmap_state mmap_state, int write_combine)
29 size = ((pci_resource_len(pdev, bar) - 1) >> PAGE_SHIFT) + 1;
30 if (vma->vm_pgoff + vma_pages(vma) > size)
34 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
36 vma->vm_page_prot = pgprot_device(vma->vm_page_prot);
38 if (mmap_state == pci_mmap_io) {
39 ret = pci_iobar_pfn(pdev, bar, vma);
43 vma->vm_pgoff += (pci_resource_start(pdev, bar) >> PAGE_SHIFT);
45 vma->vm_ops = &pci_phys_vm_ops;
47 return io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
48 vma->vm_end - vma->vm_start,