pci_iomap.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Implement the default iomap interfaces
  4. *
  5. * (C) Copyright 2004 Linus Torvalds
  6. */
  7. #include <linux/pci.h>
  8. #include <linux/io.h>
  9. #include <linux/export.h>
  10. #ifdef CONFIG_PCI
  11. /**
  12. * pci_iomap_range - create a virtual mapping cookie for a PCI BAR
  13. * @dev: PCI device that owns the BAR
  14. * @bar: BAR number
  15. * @offset: map memory at the given offset in BAR
  16. * @maxlen: max length of the memory to map
  17. *
  18. * Using this function you will get a __iomem address to your device BAR.
  19. * You can access it using ioread*() and iowrite*(). These functions hide
  20. * the details if this is a MMIO or PIO address space and will just do what
  21. * you expect from them in the correct way.
  22. *
  23. * @maxlen specifies the maximum length to map. If you want to get access to
  24. * the complete BAR from offset to the end, pass %0 here.
  25. * */
  26. void __iomem *pci_iomap_range(struct pci_dev *dev,
  27. int bar,
  28. unsigned long offset,
  29. unsigned long maxlen)
  30. {
  31. resource_size_t start = pci_resource_start(dev, bar);
  32. resource_size_t len = pci_resource_len(dev, bar);
  33. unsigned long flags = pci_resource_flags(dev, bar);
  34. if (len <= offset || !start)
  35. return NULL;
  36. len -= offset;
  37. start += offset;
  38. if (maxlen && len > maxlen)
  39. len = maxlen;
  40. if (flags & IORESOURCE_IO)
  41. return __pci_ioport_map(dev, start, len);
  42. if (flags & IORESOURCE_MEM)
  43. return ioremap(start, len);
  44. /* What? */
  45. return NULL;
  46. }
  47. EXPORT_SYMBOL(pci_iomap_range);
  48. /**
  49. * pci_iomap_wc_range - create a virtual WC mapping cookie for a PCI BAR
  50. * @dev: PCI device that owns the BAR
  51. * @bar: BAR number
  52. * @offset: map memory at the given offset in BAR
  53. * @maxlen: max length of the memory to map
  54. *
  55. * Using this function you will get a __iomem address to your device BAR.
  56. * You can access it using ioread*() and iowrite*(). These functions hide
  57. * the details if this is a MMIO or PIO address space and will just do what
  58. * you expect from them in the correct way. When possible write combining
  59. * is used.
  60. *
  61. * @maxlen specifies the maximum length to map. If you want to get access to
  62. * the complete BAR from offset to the end, pass %0 here.
  63. * */
  64. void __iomem *pci_iomap_wc_range(struct pci_dev *dev,
  65. int bar,
  66. unsigned long offset,
  67. unsigned long maxlen)
  68. {
  69. resource_size_t start = pci_resource_start(dev, bar);
  70. resource_size_t len = pci_resource_len(dev, bar);
  71. unsigned long flags = pci_resource_flags(dev, bar);
  72. if (flags & IORESOURCE_IO)
  73. return NULL;
  74. if (len <= offset || !start)
  75. return NULL;
  76. len -= offset;
  77. start += offset;
  78. if (maxlen && len > maxlen)
  79. len = maxlen;
  80. if (flags & IORESOURCE_MEM)
  81. return ioremap_wc(start, len);
  82. /* What? */
  83. return NULL;
  84. }
  85. EXPORT_SYMBOL_GPL(pci_iomap_wc_range);
  86. /**
  87. * pci_iomap - create a virtual mapping cookie for a PCI BAR
  88. * @dev: PCI device that owns the BAR
  89. * @bar: BAR number
  90. * @maxlen: length of the memory to map
  91. *
  92. * Using this function you will get a __iomem address to your device BAR.
  93. * You can access it using ioread*() and iowrite*(). These functions hide
  94. * the details if this is a MMIO or PIO address space and will just do what
  95. * you expect from them in the correct way.
  96. *
  97. * @maxlen specifies the maximum length to map. If you want to get access to
  98. * the complete BAR without checking for its length first, pass %0 here.
  99. * */
  100. void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
  101. {
  102. return pci_iomap_range(dev, bar, 0, maxlen);
  103. }
  104. EXPORT_SYMBOL(pci_iomap);
  105. /**
  106. * pci_iomap_wc - create a virtual WC mapping cookie for a PCI BAR
  107. * @dev: PCI device that owns the BAR
  108. * @bar: BAR number
  109. * @maxlen: length of the memory to map
  110. *
  111. * Using this function you will get a __iomem address to your device BAR.
  112. * You can access it using ioread*() and iowrite*(). These functions hide
  113. * the details if this is a MMIO or PIO address space and will just do what
  114. * you expect from them in the correct way. When possible write combining
  115. * is used.
  116. *
  117. * @maxlen specifies the maximum length to map. If you want to get access to
  118. * the complete BAR without checking for its length first, pass %0 here.
  119. * */
  120. void __iomem *pci_iomap_wc(struct pci_dev *dev, int bar, unsigned long maxlen)
  121. {
  122. return pci_iomap_wc_range(dev, bar, 0, maxlen);
  123. }
  124. EXPORT_SYMBOL_GPL(pci_iomap_wc);
  125. #endif /* CONFIG_PCI */