mmap.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * mmap.c — generic PCI resource mmap helper
  3. *
  4. * Copyright © 2017 Amazon.com, Inc. or its affiliates.
  5. *
  6. * Author: David Woodhouse <dwmw2@infradead.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/mm.h>
  14. #include <linux/pci.h>
  15. #ifdef ARCH_GENERIC_PCI_MMAP_RESOURCE
  16. /*
  17. * Modern setup: generic pci_mmap_resource_range(), and implement the legacy
  18. * pci_mmap_page_range() (if needed) as a wrapper round it.
  19. */
  20. #ifdef HAVE_PCI_MMAP
  21. int pci_mmap_page_range(struct pci_dev *pdev, int bar,
  22. struct vm_area_struct *vma,
  23. enum pci_mmap_state mmap_state, int write_combine)
  24. {
  25. resource_size_t start, end;
  26. pci_resource_to_user(pdev, bar, &pdev->resource[bar], &start, &end);
  27. /* Adjust vm_pgoff to be the offset within the resource */
  28. vma->vm_pgoff -= start >> PAGE_SHIFT;
  29. return pci_mmap_resource_range(pdev, bar, vma, mmap_state,
  30. write_combine);
  31. }
  32. #endif
  33. static const struct vm_operations_struct pci_phys_vm_ops = {
  34. #ifdef CONFIG_HAVE_IOREMAP_PROT
  35. .access = generic_access_phys,
  36. #endif
  37. };
  38. int pci_mmap_resource_range(struct pci_dev *pdev, int bar,
  39. struct vm_area_struct *vma,
  40. enum pci_mmap_state mmap_state, int write_combine)
  41. {
  42. unsigned long size;
  43. int ret;
  44. size = ((pci_resource_len(pdev, bar) - 1) >> PAGE_SHIFT) + 1;
  45. if (vma->vm_pgoff + vma_pages(vma) > size)
  46. return -EINVAL;
  47. if (write_combine)
  48. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  49. else
  50. vma->vm_page_prot = pgprot_device(vma->vm_page_prot);
  51. if (mmap_state == pci_mmap_io) {
  52. ret = pci_iobar_pfn(pdev, bar, vma);
  53. if (ret)
  54. return ret;
  55. } else
  56. vma->vm_pgoff += (pci_resource_start(pdev, bar) >> PAGE_SHIFT);
  57. vma->vm_ops = &pci_phys_vm_ops;
  58. return io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  59. vma->vm_end - vma->vm_start,
  60. vma->vm_page_prot);
  61. }
  62. #elif defined(HAVE_PCI_MMAP) /* && !ARCH_GENERIC_PCI_MMAP_RESOURCE */
  63. /*
  64. * Legacy setup: Impement pci_mmap_resource_range() as a wrapper around
  65. * the architecture's pci_mmap_page_range(), converting to "user visible"
  66. * addresses as necessary.
  67. */
  68. int pci_mmap_resource_range(struct pci_dev *pdev, int bar,
  69. struct vm_area_struct *vma,
  70. enum pci_mmap_state mmap_state, int write_combine)
  71. {
  72. resource_size_t start, end;
  73. /*
  74. * pci_mmap_page_range() expects the same kind of entry as coming
  75. * from /proc/bus/pci/ which is a "user visible" value. If this is
  76. * different from the resource itself, arch will do necessary fixup.
  77. */
  78. pci_resource_to_user(pdev, bar, &pdev->resource[bar], &start, &end);
  79. vma->vm_pgoff += start >> PAGE_SHIFT;
  80. return pci_mmap_page_range(pdev, bar, vma, mmap_state, write_combine);
  81. }
  82. #endif