mmap.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. if (mmap_state == pci_mmap_io)
  44. return -EINVAL;
  45. size = ((pci_resource_len(pdev, bar) - 1) >> PAGE_SHIFT) + 1;
  46. if (vma->vm_pgoff + vma_pages(vma) > size)
  47. return -EINVAL;
  48. if (write_combine)
  49. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  50. else
  51. vma->vm_page_prot = pgprot_device(vma->vm_page_prot);
  52. vma->vm_pgoff += (pci_resource_start(pdev, bar) >> PAGE_SHIFT);
  53. vma->vm_ops = &pci_phys_vm_ops;
  54. return io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  55. vma->vm_end - vma->vm_start,
  56. vma->vm_page_prot);
  57. }
  58. #elif defined(HAVE_PCI_MMAP) /* && !ARCH_GENERIC_PCI_MMAP_RESOURCE */
  59. /*
  60. * Legacy setup: Impement pci_mmap_resource_range() as a wrapper around
  61. * the architecture's pci_mmap_page_range(), converting to "user visible"
  62. * addresses as necessary.
  63. */
  64. int pci_mmap_resource_range(struct pci_dev *pdev, int bar,
  65. struct vm_area_struct *vma,
  66. enum pci_mmap_state mmap_state, int write_combine)
  67. {
  68. resource_size_t start, end;
  69. /*
  70. * pci_mmap_page_range() expects the same kind of entry as coming
  71. * from /proc/bus/pci/ which is a "user visible" value. If this is
  72. * different from the resource itself, arch will do necessary fixup.
  73. */
  74. pci_resource_to_user(pdev, bar, &pdev->resource[bar], &start, &end);
  75. vma->vm_pgoff += start >> PAGE_SHIFT;
  76. return pci_mmap_page_range(pdev, bar, vma, mmap_state, write_combine);
  77. }
  78. #endif