iomap.c 6.2 KB

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