iomap.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. /*
  11. * Read/write from/to an (offsettable) iomem cookie. It might be a PIO
  12. * access or a MMIO access, these functions don't care. The info is
  13. * encoded in the hardware mapping set up by the mapping functions
  14. * (or the cookie itself, depending on implementation and hw).
  15. *
  16. * The generic routines don't assume any hardware mappings, and just
  17. * encode the PIO/MMIO as part of the cookie. They coldly assume that
  18. * the MMIO IO mappings are not in the low address range.
  19. *
  20. * Architectures for which this is not true can't use this generic
  21. * implementation and should do their own copy.
  22. */
  23. #ifndef HAVE_ARCH_PIO_SIZE
  24. /*
  25. * We encode the physical PIO addresses (0-0xffff) into the
  26. * pointer by offsetting them with a constant (0x10000) and
  27. * assuming that all the low addresses are always PIO. That means
  28. * we can do some sanity checks on the low bits, and don't
  29. * need to just take things for granted.
  30. */
  31. #define PIO_OFFSET 0x10000UL
  32. #define PIO_MASK 0x0ffffUL
  33. #define PIO_RESERVED 0x40000UL
  34. #endif
  35. static void bad_io_access(unsigned long port, const char *access)
  36. {
  37. static int count = 10;
  38. if (count) {
  39. count--;
  40. WARN(1, KERN_ERR "Bad IO access at port %#lx (%s)\n", port, access);
  41. }
  42. }
  43. /*
  44. * Ugly macros are a way of life.
  45. */
  46. #define IO_COND(addr, is_pio, is_mmio) do { \
  47. unsigned long port = (unsigned long __force)addr; \
  48. if (port >= PIO_RESERVED) { \
  49. is_mmio; \
  50. } else if (port > PIO_OFFSET) { \
  51. port &= PIO_MASK; \
  52. is_pio; \
  53. } else \
  54. bad_io_access(port, #is_pio ); \
  55. } while (0)
  56. #ifndef pio_read16be
  57. #define pio_read16be(port) swab16(inw(port))
  58. #define pio_read32be(port) swab32(inl(port))
  59. #endif
  60. #ifndef mmio_read16be
  61. #define mmio_read16be(addr) be16_to_cpu(__raw_readw(addr))
  62. #define mmio_read32be(addr) be32_to_cpu(__raw_readl(addr))
  63. #endif
  64. unsigned int ioread8(void __iomem *addr)
  65. {
  66. IO_COND(addr, return inb(port), return readb(addr));
  67. return 0xff;
  68. }
  69. unsigned int ioread16(void __iomem *addr)
  70. {
  71. IO_COND(addr, return inw(port), return readw(addr));
  72. return 0xffff;
  73. }
  74. unsigned int ioread16be(void __iomem *addr)
  75. {
  76. IO_COND(addr, return pio_read16be(port), return mmio_read16be(addr));
  77. return 0xffff;
  78. }
  79. unsigned int ioread32(void __iomem *addr)
  80. {
  81. IO_COND(addr, return inl(port), return readl(addr));
  82. return 0xffffffff;
  83. }
  84. unsigned int ioread32be(void __iomem *addr)
  85. {
  86. IO_COND(addr, return pio_read32be(port), return mmio_read32be(addr));
  87. return 0xffffffff;
  88. }
  89. EXPORT_SYMBOL(ioread8);
  90. EXPORT_SYMBOL(ioread16);
  91. EXPORT_SYMBOL(ioread16be);
  92. EXPORT_SYMBOL(ioread32);
  93. EXPORT_SYMBOL(ioread32be);
  94. #ifndef pio_write16be
  95. #define pio_write16be(val,port) outw(swab16(val),port)
  96. #define pio_write32be(val,port) outl(swab32(val),port)
  97. #endif
  98. #ifndef mmio_write16be
  99. #define mmio_write16be(val,port) __raw_writew(be16_to_cpu(val),port)
  100. #define mmio_write32be(val,port) __raw_writel(be32_to_cpu(val),port)
  101. #endif
  102. void iowrite8(u8 val, void __iomem *addr)
  103. {
  104. IO_COND(addr, outb(val,port), writeb(val, addr));
  105. }
  106. void iowrite16(u16 val, void __iomem *addr)
  107. {
  108. IO_COND(addr, outw(val,port), writew(val, addr));
  109. }
  110. void iowrite16be(u16 val, void __iomem *addr)
  111. {
  112. IO_COND(addr, pio_write16be(val,port), mmio_write16be(val, addr));
  113. }
  114. void iowrite32(u32 val, void __iomem *addr)
  115. {
  116. IO_COND(addr, outl(val,port), writel(val, addr));
  117. }
  118. void iowrite32be(u32 val, void __iomem *addr)
  119. {
  120. IO_COND(addr, pio_write32be(val,port), mmio_write32be(val, addr));
  121. }
  122. EXPORT_SYMBOL(iowrite8);
  123. EXPORT_SYMBOL(iowrite16);
  124. EXPORT_SYMBOL(iowrite16be);
  125. EXPORT_SYMBOL(iowrite32);
  126. EXPORT_SYMBOL(iowrite32be);
  127. /*
  128. * These are the "repeat MMIO read/write" functions.
  129. * Note the "__raw" accesses, since we don't want to
  130. * convert to CPU byte order. We write in "IO byte
  131. * order" (we also don't have IO barriers).
  132. */
  133. #ifndef mmio_insb
  134. static inline void mmio_insb(void __iomem *addr, u8 *dst, int count)
  135. {
  136. while (--count >= 0) {
  137. u8 data = __raw_readb(addr);
  138. *dst = data;
  139. dst++;
  140. }
  141. }
  142. static inline void mmio_insw(void __iomem *addr, u16 *dst, int count)
  143. {
  144. while (--count >= 0) {
  145. u16 data = __raw_readw(addr);
  146. *dst = data;
  147. dst++;
  148. }
  149. }
  150. static inline void mmio_insl(void __iomem *addr, u32 *dst, int count)
  151. {
  152. while (--count >= 0) {
  153. u32 data = __raw_readl(addr);
  154. *dst = data;
  155. dst++;
  156. }
  157. }
  158. #endif
  159. #ifndef mmio_outsb
  160. static inline void mmio_outsb(void __iomem *addr, const u8 *src, int count)
  161. {
  162. while (--count >= 0) {
  163. __raw_writeb(*src, addr);
  164. src++;
  165. }
  166. }
  167. static inline void mmio_outsw(void __iomem *addr, const u16 *src, int count)
  168. {
  169. while (--count >= 0) {
  170. __raw_writew(*src, addr);
  171. src++;
  172. }
  173. }
  174. static inline void mmio_outsl(void __iomem *addr, const u32 *src, int count)
  175. {
  176. while (--count >= 0) {
  177. __raw_writel(*src, addr);
  178. src++;
  179. }
  180. }
  181. #endif
  182. void ioread8_rep(void __iomem *addr, void *dst, unsigned long count)
  183. {
  184. IO_COND(addr, insb(port,dst,count), mmio_insb(addr, dst, count));
  185. }
  186. void ioread16_rep(void __iomem *addr, void *dst, unsigned long count)
  187. {
  188. IO_COND(addr, insw(port,dst,count), mmio_insw(addr, dst, count));
  189. }
  190. void ioread32_rep(void __iomem *addr, void *dst, unsigned long count)
  191. {
  192. IO_COND(addr, insl(port,dst,count), mmio_insl(addr, dst, count));
  193. }
  194. EXPORT_SYMBOL(ioread8_rep);
  195. EXPORT_SYMBOL(ioread16_rep);
  196. EXPORT_SYMBOL(ioread32_rep);
  197. void iowrite8_rep(void __iomem *addr, const void *src, unsigned long count)
  198. {
  199. IO_COND(addr, outsb(port, src, count), mmio_outsb(addr, src, count));
  200. }
  201. void iowrite16_rep(void __iomem *addr, const void *src, unsigned long count)
  202. {
  203. IO_COND(addr, outsw(port, src, count), mmio_outsw(addr, src, count));
  204. }
  205. void iowrite32_rep(void __iomem *addr, const void *src, unsigned long count)
  206. {
  207. IO_COND(addr, outsl(port, src,count), mmio_outsl(addr, src, count));
  208. }
  209. EXPORT_SYMBOL(iowrite8_rep);
  210. EXPORT_SYMBOL(iowrite16_rep);
  211. EXPORT_SYMBOL(iowrite32_rep);
  212. #ifdef CONFIG_HAS_IOPORT_MAP
  213. /* Create a virtual mapping cookie for an IO port range */
  214. void __iomem *ioport_map(unsigned long port, unsigned int nr)
  215. {
  216. if (port > PIO_MASK)
  217. return NULL;
  218. return (void __iomem *) (unsigned long) (port + PIO_OFFSET);
  219. }
  220. void ioport_unmap(void __iomem *addr)
  221. {
  222. /* Nothing to do */
  223. }
  224. EXPORT_SYMBOL(ioport_map);
  225. EXPORT_SYMBOL(ioport_unmap);
  226. #endif /* CONFIG_HAS_IOPORT_MAP */
  227. #ifdef CONFIG_PCI
  228. /* Hide the details if this is a MMIO or PIO address space and just do what
  229. * you expect in the correct way. */
  230. void pci_iounmap(struct pci_dev *dev, void __iomem * addr)
  231. {
  232. IO_COND(addr, /* nothing */, iounmap(addr));
  233. }
  234. EXPORT_SYMBOL(pci_iounmap);
  235. #endif /* CONFIG_PCI */