iomap.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #include "pci.h" /* for pci_bar_index_is_valid() */
  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, len;
  32. unsigned long flags;
  33. if (!pci_bar_index_is_valid(bar))
  34. return NULL;
  35. start = pci_resource_start(dev, bar);
  36. len = pci_resource_len(dev, bar);
  37. flags = pci_resource_flags(dev, bar);
  38. if (len <= offset || !start)
  39. return NULL;
  40. len -= offset;
  41. start += offset;
  42. if (maxlen && len > maxlen)
  43. len = maxlen;
  44. if (flags & IORESOURCE_IO)
  45. return __pci_ioport_map(dev, start, len);
  46. if (flags & IORESOURCE_MEM)
  47. return ioremap(start, len);
  48. /* What? */
  49. return NULL;
  50. }
  51. EXPORT_SYMBOL(pci_iomap_range);
  52. /**
  53. * pci_iomap_wc_range - create a virtual WC mapping cookie for a PCI BAR
  54. * @dev: PCI device that owns the BAR
  55. * @bar: BAR number
  56. * @offset: map memory at the given offset in BAR
  57. * @maxlen: max length of the memory to map
  58. *
  59. * Using this function you will get a __iomem address to your device BAR.
  60. * You can access it using ioread*() and iowrite*(). These functions hide
  61. * the details if this is a MMIO or PIO address space and will just do what
  62. * you expect from them in the correct way. When possible write combining
  63. * is used.
  64. *
  65. * @maxlen specifies the maximum length to map. If you want to get access to
  66. * the complete BAR from offset to the end, pass %0 here.
  67. * */
  68. void __iomem *pci_iomap_wc_range(struct pci_dev *dev,
  69. int bar,
  70. unsigned long offset,
  71. unsigned long maxlen)
  72. {
  73. resource_size_t start, len;
  74. unsigned long flags;
  75. if (!pci_bar_index_is_valid(bar))
  76. return NULL;
  77. start = pci_resource_start(dev, bar);
  78. len = pci_resource_len(dev, bar);
  79. flags = pci_resource_flags(dev, bar);
  80. if (len <= offset || !start)
  81. return NULL;
  82. if (flags & IORESOURCE_IO)
  83. return NULL;
  84. len -= offset;
  85. start += offset;
  86. if (maxlen && len > maxlen)
  87. len = maxlen;
  88. if (flags & IORESOURCE_MEM)
  89. return ioremap_wc(start, len);
  90. /* What? */
  91. return NULL;
  92. }
  93. EXPORT_SYMBOL_GPL(pci_iomap_wc_range);
  94. /**
  95. * pci_iomap - create a virtual mapping cookie for a PCI BAR
  96. * @dev: PCI device that owns the BAR
  97. * @bar: BAR number
  98. * @maxlen: length of the memory to map
  99. *
  100. * Using this function you will get a __iomem address to your device BAR.
  101. * You can access it using ioread*() and iowrite*(). These functions hide
  102. * the details if this is a MMIO or PIO address space and will just do what
  103. * you expect from them in the correct way.
  104. *
  105. * @maxlen specifies the maximum length to map. If you want to get access to
  106. * the complete BAR without checking for its length first, pass %0 here.
  107. * */
  108. void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
  109. {
  110. return pci_iomap_range(dev, bar, 0, maxlen);
  111. }
  112. EXPORT_SYMBOL(pci_iomap);
  113. /**
  114. * pci_iomap_wc - create a virtual WC mapping cookie for a PCI BAR
  115. * @dev: PCI device that owns the BAR
  116. * @bar: BAR number
  117. * @maxlen: length of the memory to map
  118. *
  119. * Using this function you will get a __iomem address to your device BAR.
  120. * You can access it using ioread*() and iowrite*(). These functions hide
  121. * the details if this is a MMIO or PIO address space and will just do what
  122. * you expect from them in the correct way. When possible write combining
  123. * is used.
  124. *
  125. * @maxlen specifies the maximum length to map. If you want to get access to
  126. * the complete BAR without checking for its length first, pass %0 here.
  127. * */
  128. void __iomem *pci_iomap_wc(struct pci_dev *dev, int bar, unsigned long maxlen)
  129. {
  130. return pci_iomap_wc_range(dev, bar, 0, maxlen);
  131. }
  132. EXPORT_SYMBOL_GPL(pci_iomap_wc);
  133. /*
  134. * pci_iounmap() somewhat illogically comes from lib/iomap.c for the
  135. * CONFIG_GENERIC_IOMAP case, because that's the code that knows about
  136. * the different IOMAP ranges.
  137. *
  138. * But if the architecture does not use the generic iomap code, and if
  139. * it has _not_ defined its own private pci_iounmap function, we define
  140. * it here.
  141. *
  142. * NOTE! This default implementation assumes that if the architecture
  143. * support ioport mapping (HAS_IOPORT_MAP), the ioport mapping will
  144. * be fixed to the range [ PCI_IOBASE, PCI_IOBASE+IO_SPACE_LIMIT [,
  145. * and does not need unmapping with 'ioport_unmap()'.
  146. *
  147. * If you have different rules for your architecture, you need to
  148. * implement your own pci_iounmap() that knows the rules for where
  149. * and how IO vs MEM get mapped.
  150. *
  151. * This code is odd, and the ARCH_HAS/ARCH_WANTS #define logic comes
  152. * from legacy <asm-generic/io.h> header file behavior. In particular,
  153. * it would seem to make sense to do the iounmap(p) for the non-IO-space
  154. * case here regardless, but that's not what the old header file code
  155. * did. Probably incorrectly, but this is meant to be bug-for-bug
  156. * compatible.
  157. */
  158. #if defined(ARCH_WANTS_GENERIC_PCI_IOUNMAP)
  159. void pci_iounmap(struct pci_dev *dev, void __iomem *p)
  160. {
  161. #ifdef ARCH_HAS_GENERIC_IOPORT_MAP
  162. uintptr_t start = (uintptr_t) PCI_IOBASE;
  163. uintptr_t addr = (uintptr_t) p;
  164. if (addr >= start && addr < start + IO_SPACE_LIMIT)
  165. return;
  166. #endif
  167. iounmap(p);
  168. }
  169. EXPORT_SYMBOL(pci_iounmap);
  170. #endif /* ARCH_WANTS_GENERIC_PCI_IOUNMAP */