iomap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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/kmsan-checks.h>
  10. #include <linux/export.h>
  11. /*
  12. * Read/write from/to an (offsettable) iomem cookie. It might be a PIO
  13. * access or a MMIO access, these functions don't care. The info is
  14. * encoded in the hardware mapping set up by the mapping functions
  15. * (or the cookie itself, depending on implementation and hw).
  16. *
  17. * The generic routines don't assume any hardware mappings, and just
  18. * encode the PIO/MMIO as part of the cookie. They coldly assume that
  19. * the MMIO IO mappings are not in the low address range.
  20. *
  21. * Architectures for which this is not true can't use this generic
  22. * implementation and should do their own copy.
  23. */
  24. #ifndef HAVE_ARCH_PIO_SIZE
  25. /*
  26. * We encode the physical PIO addresses (0-0xffff) into the
  27. * pointer by offsetting them with a constant (0x10000) and
  28. * assuming that all the low addresses are always PIO. That means
  29. * we can do some sanity checks on the low bits, and don't
  30. * need to just take things for granted.
  31. */
  32. #define PIO_OFFSET 0x10000UL
  33. #define PIO_MASK 0x0ffffUL
  34. #define PIO_RESERVED 0x40000UL
  35. #endif
  36. static void bad_io_access(unsigned long port, const char *access)
  37. {
  38. static int count = 10;
  39. if (count) {
  40. count--;
  41. WARN(1, KERN_ERR "Bad IO access at port %#lx (%s)\n", port, access);
  42. }
  43. }
  44. /*
  45. * Ugly macros are a way of life.
  46. */
  47. #define IO_COND(addr, is_pio, is_mmio) do { \
  48. unsigned long port = (unsigned long __force)addr; \
  49. if (port >= PIO_RESERVED) { \
  50. is_mmio; \
  51. } else if (port > PIO_OFFSET) { \
  52. port &= PIO_MASK; \
  53. is_pio; \
  54. } else \
  55. bad_io_access(port, #is_pio ); \
  56. } while (0)
  57. #ifndef pio_read16be
  58. #define pio_read16be(port) swab16(inw(port))
  59. #define pio_read32be(port) swab32(inl(port))
  60. #endif
  61. #ifndef mmio_read16be
  62. #define mmio_read16be(addr) swab16(readw(addr))
  63. #define mmio_read32be(addr) swab32(readl(addr))
  64. #define mmio_read64be(addr) swab64(readq(addr))
  65. #endif
  66. /*
  67. * Here and below, we apply __no_kmsan_checks to functions reading data from
  68. * hardware, to ensure that KMSAN marks their return values as initialized.
  69. */
  70. __no_kmsan_checks
  71. unsigned int ioread8(const void __iomem *addr)
  72. {
  73. IO_COND(addr, return inb(port), return readb(addr));
  74. return 0xff;
  75. }
  76. __no_kmsan_checks
  77. unsigned int ioread16(const void __iomem *addr)
  78. {
  79. IO_COND(addr, return inw(port), return readw(addr));
  80. return 0xffff;
  81. }
  82. __no_kmsan_checks
  83. unsigned int ioread16be(const void __iomem *addr)
  84. {
  85. IO_COND(addr, return pio_read16be(port), return mmio_read16be(addr));
  86. return 0xffff;
  87. }
  88. __no_kmsan_checks
  89. unsigned int ioread32(const void __iomem *addr)
  90. {
  91. IO_COND(addr, return inl(port), return readl(addr));
  92. return 0xffffffff;
  93. }
  94. __no_kmsan_checks
  95. unsigned int ioread32be(const void __iomem *addr)
  96. {
  97. IO_COND(addr, return pio_read32be(port), return mmio_read32be(addr));
  98. return 0xffffffff;
  99. }
  100. EXPORT_SYMBOL(ioread8);
  101. EXPORT_SYMBOL(ioread16);
  102. EXPORT_SYMBOL(ioread16be);
  103. EXPORT_SYMBOL(ioread32);
  104. EXPORT_SYMBOL(ioread32be);
  105. #ifdef readq
  106. static u64 pio_read64_lo_hi(unsigned long port)
  107. {
  108. u64 lo, hi;
  109. lo = inl(port);
  110. hi = inl(port + sizeof(u32));
  111. return lo | (hi << 32);
  112. }
  113. static u64 pio_read64_hi_lo(unsigned long port)
  114. {
  115. u64 lo, hi;
  116. hi = inl(port + sizeof(u32));
  117. lo = inl(port);
  118. return lo | (hi << 32);
  119. }
  120. static u64 pio_read64be_lo_hi(unsigned long port)
  121. {
  122. u64 lo, hi;
  123. lo = pio_read32be(port + sizeof(u32));
  124. hi = pio_read32be(port);
  125. return lo | (hi << 32);
  126. }
  127. static u64 pio_read64be_hi_lo(unsigned long port)
  128. {
  129. u64 lo, hi;
  130. hi = pio_read32be(port);
  131. lo = pio_read32be(port + sizeof(u32));
  132. return lo | (hi << 32);
  133. }
  134. __no_kmsan_checks
  135. u64 ioread64_lo_hi(const void __iomem *addr)
  136. {
  137. IO_COND(addr, return pio_read64_lo_hi(port), return readq(addr));
  138. return 0xffffffffffffffffULL;
  139. }
  140. __no_kmsan_checks
  141. u64 ioread64_hi_lo(const void __iomem *addr)
  142. {
  143. IO_COND(addr, return pio_read64_hi_lo(port), return readq(addr));
  144. return 0xffffffffffffffffULL;
  145. }
  146. __no_kmsan_checks
  147. u64 ioread64be_lo_hi(const void __iomem *addr)
  148. {
  149. IO_COND(addr, return pio_read64be_lo_hi(port),
  150. return mmio_read64be(addr));
  151. return 0xffffffffffffffffULL;
  152. }
  153. __no_kmsan_checks
  154. u64 ioread64be_hi_lo(const void __iomem *addr)
  155. {
  156. IO_COND(addr, return pio_read64be_hi_lo(port),
  157. return mmio_read64be(addr));
  158. return 0xffffffffffffffffULL;
  159. }
  160. EXPORT_SYMBOL(ioread64_lo_hi);
  161. EXPORT_SYMBOL(ioread64_hi_lo);
  162. EXPORT_SYMBOL(ioread64be_lo_hi);
  163. EXPORT_SYMBOL(ioread64be_hi_lo);
  164. #endif /* readq */
  165. #ifndef pio_write16be
  166. #define pio_write16be(val,port) outw(swab16(val),port)
  167. #define pio_write32be(val,port) outl(swab32(val),port)
  168. #endif
  169. #ifndef mmio_write16be
  170. #define mmio_write16be(val,port) writew(swab16(val),port)
  171. #define mmio_write32be(val,port) writel(swab32(val),port)
  172. #define mmio_write64be(val,port) writeq(swab64(val),port)
  173. #endif
  174. void iowrite8(u8 val, void __iomem *addr)
  175. {
  176. /* Make sure uninitialized memory isn't copied to devices. */
  177. kmsan_check_memory(&val, sizeof(val));
  178. IO_COND(addr, outb(val,port), writeb(val, addr));
  179. }
  180. void iowrite16(u16 val, void __iomem *addr)
  181. {
  182. /* Make sure uninitialized memory isn't copied to devices. */
  183. kmsan_check_memory(&val, sizeof(val));
  184. IO_COND(addr, outw(val,port), writew(val, addr));
  185. }
  186. void iowrite16be(u16 val, void __iomem *addr)
  187. {
  188. /* Make sure uninitialized memory isn't copied to devices. */
  189. kmsan_check_memory(&val, sizeof(val));
  190. IO_COND(addr, pio_write16be(val,port), mmio_write16be(val, addr));
  191. }
  192. void iowrite32(u32 val, void __iomem *addr)
  193. {
  194. /* Make sure uninitialized memory isn't copied to devices. */
  195. kmsan_check_memory(&val, sizeof(val));
  196. IO_COND(addr, outl(val,port), writel(val, addr));
  197. }
  198. void iowrite32be(u32 val, void __iomem *addr)
  199. {
  200. /* Make sure uninitialized memory isn't copied to devices. */
  201. kmsan_check_memory(&val, sizeof(val));
  202. IO_COND(addr, pio_write32be(val,port), mmio_write32be(val, addr));
  203. }
  204. EXPORT_SYMBOL(iowrite8);
  205. EXPORT_SYMBOL(iowrite16);
  206. EXPORT_SYMBOL(iowrite16be);
  207. EXPORT_SYMBOL(iowrite32);
  208. EXPORT_SYMBOL(iowrite32be);
  209. #ifdef writeq
  210. static void pio_write64_lo_hi(u64 val, unsigned long port)
  211. {
  212. outl(val, port);
  213. outl(val >> 32, port + sizeof(u32));
  214. }
  215. static void pio_write64_hi_lo(u64 val, unsigned long port)
  216. {
  217. outl(val >> 32, port + sizeof(u32));
  218. outl(val, port);
  219. }
  220. static void pio_write64be_lo_hi(u64 val, unsigned long port)
  221. {
  222. pio_write32be(val, port + sizeof(u32));
  223. pio_write32be(val >> 32, port);
  224. }
  225. static void pio_write64be_hi_lo(u64 val, unsigned long port)
  226. {
  227. pio_write32be(val >> 32, port);
  228. pio_write32be(val, port + sizeof(u32));
  229. }
  230. void iowrite64_lo_hi(u64 val, void __iomem *addr)
  231. {
  232. /* Make sure uninitialized memory isn't copied to devices. */
  233. kmsan_check_memory(&val, sizeof(val));
  234. IO_COND(addr, pio_write64_lo_hi(val, port),
  235. writeq(val, addr));
  236. }
  237. void iowrite64_hi_lo(u64 val, void __iomem *addr)
  238. {
  239. /* Make sure uninitialized memory isn't copied to devices. */
  240. kmsan_check_memory(&val, sizeof(val));
  241. IO_COND(addr, pio_write64_hi_lo(val, port),
  242. writeq(val, addr));
  243. }
  244. void iowrite64be_lo_hi(u64 val, void __iomem *addr)
  245. {
  246. /* Make sure uninitialized memory isn't copied to devices. */
  247. kmsan_check_memory(&val, sizeof(val));
  248. IO_COND(addr, pio_write64be_lo_hi(val, port),
  249. mmio_write64be(val, addr));
  250. }
  251. void iowrite64be_hi_lo(u64 val, void __iomem *addr)
  252. {
  253. /* Make sure uninitialized memory isn't copied to devices. */
  254. kmsan_check_memory(&val, sizeof(val));
  255. IO_COND(addr, pio_write64be_hi_lo(val, port),
  256. mmio_write64be(val, addr));
  257. }
  258. EXPORT_SYMBOL(iowrite64_lo_hi);
  259. EXPORT_SYMBOL(iowrite64_hi_lo);
  260. EXPORT_SYMBOL(iowrite64be_lo_hi);
  261. EXPORT_SYMBOL(iowrite64be_hi_lo);
  262. #endif /* readq */
  263. /*
  264. * These are the "repeat MMIO read/write" functions.
  265. * Note the "__raw" accesses, since we don't want to
  266. * convert to CPU byte order. We write in "IO byte
  267. * order" (we also don't have IO barriers).
  268. */
  269. #ifndef mmio_insb
  270. static inline void mmio_insb(const void __iomem *addr, u8 *dst, int count)
  271. {
  272. while (--count >= 0) {
  273. u8 data = __raw_readb(addr);
  274. *dst = data;
  275. dst++;
  276. }
  277. }
  278. static inline void mmio_insw(const void __iomem *addr, u16 *dst, int count)
  279. {
  280. while (--count >= 0) {
  281. u16 data = __raw_readw(addr);
  282. *dst = data;
  283. dst++;
  284. }
  285. }
  286. static inline void mmio_insl(const void __iomem *addr, u32 *dst, int count)
  287. {
  288. while (--count >= 0) {
  289. u32 data = __raw_readl(addr);
  290. *dst = data;
  291. dst++;
  292. }
  293. }
  294. #endif
  295. #ifndef mmio_outsb
  296. static inline void mmio_outsb(void __iomem *addr, const u8 *src, int count)
  297. {
  298. while (--count >= 0) {
  299. __raw_writeb(*src, addr);
  300. src++;
  301. }
  302. }
  303. static inline void mmio_outsw(void __iomem *addr, const u16 *src, int count)
  304. {
  305. while (--count >= 0) {
  306. __raw_writew(*src, addr);
  307. src++;
  308. }
  309. }
  310. static inline void mmio_outsl(void __iomem *addr, const u32 *src, int count)
  311. {
  312. while (--count >= 0) {
  313. __raw_writel(*src, addr);
  314. src++;
  315. }
  316. }
  317. #endif
  318. void ioread8_rep(const void __iomem *addr, void *dst, unsigned long count)
  319. {
  320. IO_COND(addr, insb(port,dst,count), mmio_insb(addr, dst, count));
  321. /* KMSAN must treat values read from devices as initialized. */
  322. kmsan_unpoison_memory(dst, count);
  323. }
  324. void ioread16_rep(const void __iomem *addr, void *dst, unsigned long count)
  325. {
  326. IO_COND(addr, insw(port,dst,count), mmio_insw(addr, dst, count));
  327. /* KMSAN must treat values read from devices as initialized. */
  328. kmsan_unpoison_memory(dst, count * 2);
  329. }
  330. void ioread32_rep(const void __iomem *addr, void *dst, unsigned long count)
  331. {
  332. IO_COND(addr, insl(port,dst,count), mmio_insl(addr, dst, count));
  333. /* KMSAN must treat values read from devices as initialized. */
  334. kmsan_unpoison_memory(dst, count * 4);
  335. }
  336. EXPORT_SYMBOL(ioread8_rep);
  337. EXPORT_SYMBOL(ioread16_rep);
  338. EXPORT_SYMBOL(ioread32_rep);
  339. void iowrite8_rep(void __iomem *addr, const void *src, unsigned long count)
  340. {
  341. /* Make sure uninitialized memory isn't copied to devices. */
  342. kmsan_check_memory(src, count);
  343. IO_COND(addr, outsb(port, src, count), mmio_outsb(addr, src, count));
  344. }
  345. void iowrite16_rep(void __iomem *addr, const void *src, unsigned long count)
  346. {
  347. /* Make sure uninitialized memory isn't copied to devices. */
  348. kmsan_check_memory(src, count * 2);
  349. IO_COND(addr, outsw(port, src, count), mmio_outsw(addr, src, count));
  350. }
  351. void iowrite32_rep(void __iomem *addr, const void *src, unsigned long count)
  352. {
  353. /* Make sure uninitialized memory isn't copied to devices. */
  354. kmsan_check_memory(src, count * 4);
  355. IO_COND(addr, outsl(port, src,count), mmio_outsl(addr, src, count));
  356. }
  357. EXPORT_SYMBOL(iowrite8_rep);
  358. EXPORT_SYMBOL(iowrite16_rep);
  359. EXPORT_SYMBOL(iowrite32_rep);
  360. #ifdef CONFIG_HAS_IOPORT_MAP
  361. /* Create a virtual mapping cookie for an IO port range */
  362. void __iomem *ioport_map(unsigned long port, unsigned int nr)
  363. {
  364. if (port > PIO_MASK)
  365. return NULL;
  366. return (void __iomem *) (unsigned long) (port + PIO_OFFSET);
  367. }
  368. void ioport_unmap(void __iomem *addr)
  369. {
  370. /* Nothing to do */
  371. }
  372. EXPORT_SYMBOL(ioport_map);
  373. EXPORT_SYMBOL(ioport_unmap);
  374. #endif /* CONFIG_HAS_IOPORT_MAP */
  375. #ifdef CONFIG_PCI
  376. /* Hide the details if this is a MMIO or PIO address space and just do what
  377. * you expect in the correct way. */
  378. void pci_iounmap(struct pci_dev *dev, void __iomem * addr)
  379. {
  380. IO_COND(addr, /* nothing */, iounmap(addr));
  381. }
  382. EXPORT_SYMBOL(pci_iounmap);
  383. #endif /* CONFIG_PCI */