iomap.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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) swab16(readw(addr))
  62. #define mmio_read32be(addr) swab32(readl(addr))
  63. #define mmio_read64be(addr) swab64(readq(addr))
  64. #endif
  65. unsigned int ioread8(const void __iomem *addr)
  66. {
  67. IO_COND(addr, return inb(port), return readb(addr));
  68. return 0xff;
  69. }
  70. unsigned int ioread16(const void __iomem *addr)
  71. {
  72. IO_COND(addr, return inw(port), return readw(addr));
  73. return 0xffff;
  74. }
  75. unsigned int ioread16be(const void __iomem *addr)
  76. {
  77. IO_COND(addr, return pio_read16be(port), return mmio_read16be(addr));
  78. return 0xffff;
  79. }
  80. unsigned int ioread32(const void __iomem *addr)
  81. {
  82. IO_COND(addr, return inl(port), return readl(addr));
  83. return 0xffffffff;
  84. }
  85. unsigned int ioread32be(const void __iomem *addr)
  86. {
  87. IO_COND(addr, return pio_read32be(port), return mmio_read32be(addr));
  88. return 0xffffffff;
  89. }
  90. EXPORT_SYMBOL(ioread8);
  91. EXPORT_SYMBOL(ioread16);
  92. EXPORT_SYMBOL(ioread16be);
  93. EXPORT_SYMBOL(ioread32);
  94. EXPORT_SYMBOL(ioread32be);
  95. #ifdef readq
  96. static u64 pio_read64_lo_hi(unsigned long port)
  97. {
  98. u64 lo, hi;
  99. lo = inl(port);
  100. hi = inl(port + sizeof(u32));
  101. return lo | (hi << 32);
  102. }
  103. static u64 pio_read64_hi_lo(unsigned long port)
  104. {
  105. u64 lo, hi;
  106. hi = inl(port + sizeof(u32));
  107. lo = inl(port);
  108. return lo | (hi << 32);
  109. }
  110. static u64 pio_read64be_lo_hi(unsigned long port)
  111. {
  112. u64 lo, hi;
  113. lo = pio_read32be(port + sizeof(u32));
  114. hi = pio_read32be(port);
  115. return lo | (hi << 32);
  116. }
  117. static u64 pio_read64be_hi_lo(unsigned long port)
  118. {
  119. u64 lo, hi;
  120. hi = pio_read32be(port);
  121. lo = pio_read32be(port + sizeof(u32));
  122. return lo | (hi << 32);
  123. }
  124. u64 ioread64_lo_hi(const void __iomem *addr)
  125. {
  126. IO_COND(addr, return pio_read64_lo_hi(port), return readq(addr));
  127. return 0xffffffffffffffffULL;
  128. }
  129. u64 ioread64_hi_lo(const void __iomem *addr)
  130. {
  131. IO_COND(addr, return pio_read64_hi_lo(port), return readq(addr));
  132. return 0xffffffffffffffffULL;
  133. }
  134. u64 ioread64be_lo_hi(const void __iomem *addr)
  135. {
  136. IO_COND(addr, return pio_read64be_lo_hi(port),
  137. return mmio_read64be(addr));
  138. return 0xffffffffffffffffULL;
  139. }
  140. u64 ioread64be_hi_lo(const void __iomem *addr)
  141. {
  142. IO_COND(addr, return pio_read64be_hi_lo(port),
  143. return mmio_read64be(addr));
  144. return 0xffffffffffffffffULL;
  145. }
  146. EXPORT_SYMBOL(ioread64_lo_hi);
  147. EXPORT_SYMBOL(ioread64_hi_lo);
  148. EXPORT_SYMBOL(ioread64be_lo_hi);
  149. EXPORT_SYMBOL(ioread64be_hi_lo);
  150. #endif /* readq */
  151. #ifndef pio_write16be
  152. #define pio_write16be(val,port) outw(swab16(val),port)
  153. #define pio_write32be(val,port) outl(swab32(val),port)
  154. #endif
  155. #ifndef mmio_write16be
  156. #define mmio_write16be(val,port) writew(swab16(val),port)
  157. #define mmio_write32be(val,port) writel(swab32(val),port)
  158. #define mmio_write64be(val,port) writeq(swab64(val),port)
  159. #endif
  160. void iowrite8(u8 val, void __iomem *addr)
  161. {
  162. IO_COND(addr, outb(val,port), writeb(val, addr));
  163. }
  164. void iowrite16(u16 val, void __iomem *addr)
  165. {
  166. IO_COND(addr, outw(val,port), writew(val, addr));
  167. }
  168. void iowrite16be(u16 val, void __iomem *addr)
  169. {
  170. IO_COND(addr, pio_write16be(val,port), mmio_write16be(val, addr));
  171. }
  172. void iowrite32(u32 val, void __iomem *addr)
  173. {
  174. IO_COND(addr, outl(val,port), writel(val, addr));
  175. }
  176. void iowrite32be(u32 val, void __iomem *addr)
  177. {
  178. IO_COND(addr, pio_write32be(val,port), mmio_write32be(val, addr));
  179. }
  180. EXPORT_SYMBOL(iowrite8);
  181. EXPORT_SYMBOL(iowrite16);
  182. EXPORT_SYMBOL(iowrite16be);
  183. EXPORT_SYMBOL(iowrite32);
  184. EXPORT_SYMBOL(iowrite32be);
  185. #ifdef writeq
  186. static void pio_write64_lo_hi(u64 val, unsigned long port)
  187. {
  188. outl(val, port);
  189. outl(val >> 32, port + sizeof(u32));
  190. }
  191. static void pio_write64_hi_lo(u64 val, unsigned long port)
  192. {
  193. outl(val >> 32, port + sizeof(u32));
  194. outl(val, port);
  195. }
  196. static void pio_write64be_lo_hi(u64 val, unsigned long port)
  197. {
  198. pio_write32be(val, port + sizeof(u32));
  199. pio_write32be(val >> 32, port);
  200. }
  201. static void pio_write64be_hi_lo(u64 val, unsigned long port)
  202. {
  203. pio_write32be(val >> 32, port);
  204. pio_write32be(val, port + sizeof(u32));
  205. }
  206. void iowrite64_lo_hi(u64 val, void __iomem *addr)
  207. {
  208. IO_COND(addr, pio_write64_lo_hi(val, port),
  209. writeq(val, addr));
  210. }
  211. void iowrite64_hi_lo(u64 val, void __iomem *addr)
  212. {
  213. IO_COND(addr, pio_write64_hi_lo(val, port),
  214. writeq(val, addr));
  215. }
  216. void iowrite64be_lo_hi(u64 val, void __iomem *addr)
  217. {
  218. IO_COND(addr, pio_write64be_lo_hi(val, port),
  219. mmio_write64be(val, addr));
  220. }
  221. void iowrite64be_hi_lo(u64 val, void __iomem *addr)
  222. {
  223. IO_COND(addr, pio_write64be_hi_lo(val, port),
  224. mmio_write64be(val, addr));
  225. }
  226. EXPORT_SYMBOL(iowrite64_lo_hi);
  227. EXPORT_SYMBOL(iowrite64_hi_lo);
  228. EXPORT_SYMBOL(iowrite64be_lo_hi);
  229. EXPORT_SYMBOL(iowrite64be_hi_lo);
  230. #endif /* readq */
  231. /*
  232. * These are the "repeat MMIO read/write" functions.
  233. * Note the "__raw" accesses, since we don't want to
  234. * convert to CPU byte order. We write in "IO byte
  235. * order" (we also don't have IO barriers).
  236. */
  237. #ifndef mmio_insb
  238. static inline void mmio_insb(const void __iomem *addr, u8 *dst, int count)
  239. {
  240. while (--count >= 0) {
  241. u8 data = __raw_readb(addr);
  242. *dst = data;
  243. dst++;
  244. }
  245. }
  246. static inline void mmio_insw(const void __iomem *addr, u16 *dst, int count)
  247. {
  248. while (--count >= 0) {
  249. u16 data = __raw_readw(addr);
  250. *dst = data;
  251. dst++;
  252. }
  253. }
  254. static inline void mmio_insl(const void __iomem *addr, u32 *dst, int count)
  255. {
  256. while (--count >= 0) {
  257. u32 data = __raw_readl(addr);
  258. *dst = data;
  259. dst++;
  260. }
  261. }
  262. #endif
  263. #ifndef mmio_outsb
  264. static inline void mmio_outsb(void __iomem *addr, const u8 *src, int count)
  265. {
  266. while (--count >= 0) {
  267. __raw_writeb(*src, addr);
  268. src++;
  269. }
  270. }
  271. static inline void mmio_outsw(void __iomem *addr, const u16 *src, int count)
  272. {
  273. while (--count >= 0) {
  274. __raw_writew(*src, addr);
  275. src++;
  276. }
  277. }
  278. static inline void mmio_outsl(void __iomem *addr, const u32 *src, int count)
  279. {
  280. while (--count >= 0) {
  281. __raw_writel(*src, addr);
  282. src++;
  283. }
  284. }
  285. #endif
  286. void ioread8_rep(const void __iomem *addr, void *dst, unsigned long count)
  287. {
  288. IO_COND(addr, insb(port,dst,count), mmio_insb(addr, dst, count));
  289. }
  290. void ioread16_rep(const void __iomem *addr, void *dst, unsigned long count)
  291. {
  292. IO_COND(addr, insw(port,dst,count), mmio_insw(addr, dst, count));
  293. }
  294. void ioread32_rep(const void __iomem *addr, void *dst, unsigned long count)
  295. {
  296. IO_COND(addr, insl(port,dst,count), mmio_insl(addr, dst, count));
  297. }
  298. EXPORT_SYMBOL(ioread8_rep);
  299. EXPORT_SYMBOL(ioread16_rep);
  300. EXPORT_SYMBOL(ioread32_rep);
  301. void iowrite8_rep(void __iomem *addr, const void *src, unsigned long count)
  302. {
  303. IO_COND(addr, outsb(port, src, count), mmio_outsb(addr, src, count));
  304. }
  305. void iowrite16_rep(void __iomem *addr, const void *src, unsigned long count)
  306. {
  307. IO_COND(addr, outsw(port, src, count), mmio_outsw(addr, src, count));
  308. }
  309. void iowrite32_rep(void __iomem *addr, const void *src, unsigned long count)
  310. {
  311. IO_COND(addr, outsl(port, src,count), mmio_outsl(addr, src, count));
  312. }
  313. EXPORT_SYMBOL(iowrite8_rep);
  314. EXPORT_SYMBOL(iowrite16_rep);
  315. EXPORT_SYMBOL(iowrite32_rep);
  316. #ifdef CONFIG_HAS_IOPORT_MAP
  317. /* Create a virtual mapping cookie for an IO port range */
  318. void __iomem *ioport_map(unsigned long port, unsigned int nr)
  319. {
  320. if (port > PIO_MASK)
  321. return NULL;
  322. return (void __iomem *) (unsigned long) (port + PIO_OFFSET);
  323. }
  324. void ioport_unmap(void __iomem *addr)
  325. {
  326. /* Nothing to do */
  327. }
  328. EXPORT_SYMBOL(ioport_map);
  329. EXPORT_SYMBOL(ioport_unmap);
  330. #endif /* CONFIG_HAS_IOPORT_MAP */
  331. #ifdef CONFIG_PCI
  332. /* Hide the details if this is a MMIO or PIO address space and just do what
  333. * you expect in the correct way. */
  334. void pci_iounmap(struct pci_dev *dev, void __iomem * addr)
  335. {
  336. IO_COND(addr, /* nothing */, iounmap(addr));
  337. }
  338. EXPORT_SYMBOL(pci_iounmap);
  339. #endif /* CONFIG_PCI */