io.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * {read,write}{b,w,l,q} based on arch/arm64/include/asm/io.h
  3. * which was based on arch/arm/include/io.h
  4. *
  5. * Copyright (C) 1996-2000 Russell King
  6. * Copyright (C) 2012 ARM Ltd.
  7. * Copyright (C) 2014 Regents of the University of California
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation, version 2.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #ifndef _ASM_RISCV_IO_H
  19. #define _ASM_RISCV_IO_H
  20. #ifdef CONFIG_MMU
  21. extern void __iomem *ioremap(phys_addr_t offset, unsigned long size);
  22. /*
  23. * The RISC-V ISA doesn't yet specify how to query or modify PMAs, so we can't
  24. * change the properties of memory regions. This should be fixed by the
  25. * upcoming platform spec.
  26. */
  27. #define ioremap_nocache(addr, size) ioremap((addr), (size))
  28. #define ioremap_wc(addr, size) ioremap((addr), (size))
  29. #define ioremap_wt(addr, size) ioremap((addr), (size))
  30. extern void iounmap(void __iomem *addr);
  31. #endif /* CONFIG_MMU */
  32. /* Generic IO read/write. These perform native-endian accesses. */
  33. #define __raw_writeb __raw_writeb
  34. static inline void __raw_writeb(u8 val, volatile void __iomem *addr)
  35. {
  36. asm volatile("sb %0, 0(%1)" : : "r" (val), "r" (addr));
  37. }
  38. #define __raw_writew __raw_writew
  39. static inline void __raw_writew(u16 val, volatile void __iomem *addr)
  40. {
  41. asm volatile("sh %0, 0(%1)" : : "r" (val), "r" (addr));
  42. }
  43. #define __raw_writel __raw_writel
  44. static inline void __raw_writel(u32 val, volatile void __iomem *addr)
  45. {
  46. asm volatile("sw %0, 0(%1)" : : "r" (val), "r" (addr));
  47. }
  48. #ifdef CONFIG_64BIT
  49. #define __raw_writeq __raw_writeq
  50. static inline void __raw_writeq(u64 val, volatile void __iomem *addr)
  51. {
  52. asm volatile("sd %0, 0(%1)" : : "r" (val), "r" (addr));
  53. }
  54. #endif
  55. #define __raw_readb __raw_readb
  56. static inline u8 __raw_readb(const volatile void __iomem *addr)
  57. {
  58. u8 val;
  59. asm volatile("lb %0, 0(%1)" : "=r" (val) : "r" (addr));
  60. return val;
  61. }
  62. #define __raw_readw __raw_readw
  63. static inline u16 __raw_readw(const volatile void __iomem *addr)
  64. {
  65. u16 val;
  66. asm volatile("lh %0, 0(%1)" : "=r" (val) : "r" (addr));
  67. return val;
  68. }
  69. #define __raw_readl __raw_readl
  70. static inline u32 __raw_readl(const volatile void __iomem *addr)
  71. {
  72. u32 val;
  73. asm volatile("lw %0, 0(%1)" : "=r" (val) : "r" (addr));
  74. return val;
  75. }
  76. #ifdef CONFIG_64BIT
  77. #define __raw_readq __raw_readq
  78. static inline u64 __raw_readq(const volatile void __iomem *addr)
  79. {
  80. u64 val;
  81. asm volatile("ld %0, 0(%1)" : "=r" (val) : "r" (addr));
  82. return val;
  83. }
  84. #endif
  85. /*
  86. * FIXME: I'm flip-flopping on whether or not we should keep this or enforce
  87. * the ordering with I/O on spinlocks like PowerPC does. The worry is that
  88. * drivers won't get this correct, but I also don't want to introduce a fence
  89. * into the lock code that otherwise only uses AMOs (and is essentially defined
  90. * by the ISA to be correct). For now I'm leaving this here: "o,w" is
  91. * sufficient to ensure that all writes to the device have completed before the
  92. * write to the spinlock is allowed to commit. I surmised this from reading
  93. * "ACQUIRES VS I/O ACCESSES" in memory-barriers.txt.
  94. */
  95. #define mmiowb() __asm__ __volatile__ ("fence o,w" : : : "memory");
  96. /*
  97. * Unordered I/O memory access primitives. These are even more relaxed than
  98. * the relaxed versions, as they don't even order accesses between successive
  99. * operations to the I/O regions.
  100. */
  101. #define readb_cpu(c) ({ u8 __r = __raw_readb(c); __r; })
  102. #define readw_cpu(c) ({ u16 __r = le16_to_cpu((__force __le16)__raw_readw(c)); __r; })
  103. #define readl_cpu(c) ({ u32 __r = le32_to_cpu((__force __le32)__raw_readl(c)); __r; })
  104. #define writeb_cpu(v,c) ((void)__raw_writeb((v),(c)))
  105. #define writew_cpu(v,c) ((void)__raw_writew((__force u16)cpu_to_le16(v),(c)))
  106. #define writel_cpu(v,c) ((void)__raw_writel((__force u32)cpu_to_le32(v),(c)))
  107. #ifdef CONFIG_64BIT
  108. #define readq_cpu(c) ({ u64 __r = le64_to_cpu((__force __le64)__raw_readq(c)); __r; })
  109. #define writeq_cpu(v,c) ((void)__raw_writeq((__force u64)cpu_to_le64(v),(c)))
  110. #endif
  111. /*
  112. * Relaxed I/O memory access primitives. These follow the Device memory
  113. * ordering rules but do not guarantee any ordering relative to Normal memory
  114. * accesses. These are defined to order the indicated access (either a read or
  115. * write) with all other I/O memory accesses. Since the platform specification
  116. * defines that all I/O regions are strongly ordered on channel 2, no explicit
  117. * fences are required to enforce this ordering.
  118. */
  119. /* FIXME: These are now the same as asm-generic */
  120. #define __io_rbr() do {} while (0)
  121. #define __io_rar() do {} while (0)
  122. #define __io_rbw() do {} while (0)
  123. #define __io_raw() do {} while (0)
  124. #define readb_relaxed(c) ({ u8 __v; __io_rbr(); __v = readb_cpu(c); __io_rar(); __v; })
  125. #define readw_relaxed(c) ({ u16 __v; __io_rbr(); __v = readw_cpu(c); __io_rar(); __v; })
  126. #define readl_relaxed(c) ({ u32 __v; __io_rbr(); __v = readl_cpu(c); __io_rar(); __v; })
  127. #define writeb_relaxed(v,c) ({ __io_rbw(); writeb_cpu((v),(c)); __io_raw(); })
  128. #define writew_relaxed(v,c) ({ __io_rbw(); writew_cpu((v),(c)); __io_raw(); })
  129. #define writel_relaxed(v,c) ({ __io_rbw(); writel_cpu((v),(c)); __io_raw(); })
  130. #ifdef CONFIG_64BIT
  131. #define readq_relaxed(c) ({ u64 __v; __io_rbr(); __v = readq_cpu(c); __io_rar(); __v; })
  132. #define writeq_relaxed(v,c) ({ __io_rbw(); writeq_cpu((v),(c)); __io_raw(); })
  133. #endif
  134. /*
  135. * I/O memory access primitives. Reads are ordered relative to any
  136. * following Normal memory access. Writes are ordered relative to any prior
  137. * Normal memory access. The memory barriers here are necessary as RISC-V
  138. * doesn't define any ordering between the memory space and the I/O space.
  139. */
  140. #define __io_br() do {} while (0)
  141. #define __io_ar() __asm__ __volatile__ ("fence i,r" : : : "memory");
  142. #define __io_bw() __asm__ __volatile__ ("fence w,o" : : : "memory");
  143. #define __io_aw() do {} while (0)
  144. #define readb(c) ({ u8 __v; __io_br(); __v = readb_cpu(c); __io_ar(); __v; })
  145. #define readw(c) ({ u16 __v; __io_br(); __v = readw_cpu(c); __io_ar(); __v; })
  146. #define readl(c) ({ u32 __v; __io_br(); __v = readl_cpu(c); __io_ar(); __v; })
  147. #define writeb(v,c) ({ __io_bw(); writeb_cpu((v),(c)); __io_aw(); })
  148. #define writew(v,c) ({ __io_bw(); writew_cpu((v),(c)); __io_aw(); })
  149. #define writel(v,c) ({ __io_bw(); writel_cpu((v),(c)); __io_aw(); })
  150. #ifdef CONFIG_64BIT
  151. #define readq(c) ({ u64 __v; __io_br(); __v = readq_cpu(c); __io_ar(); __v; })
  152. #define writeq(v,c) ({ __io_bw(); writeq_cpu((v),(c)); __io_aw(); })
  153. #endif
  154. /*
  155. * Emulation routines for the port-mapped IO space used by some PCI drivers.
  156. * These are defined as being "fully synchronous", but also "not guaranteed to
  157. * be fully ordered with respect to other memory and I/O operations". We're
  158. * going to be on the safe side here and just make them:
  159. * - Fully ordered WRT each other, by bracketing them with two fences. The
  160. * outer set contains both I/O so inX is ordered with outX, while the inner just
  161. * needs the type of the access (I for inX and O for outX).
  162. * - Ordered in the same manner as readX/writeX WRT memory by subsuming their
  163. * fences.
  164. * - Ordered WRT timer reads, so udelay and friends don't get elided by the
  165. * implementation.
  166. * Note that there is no way to actually enforce that outX is a non-posted
  167. * operation on RISC-V, but hopefully the timer ordering constraint is
  168. * sufficient to ensure this works sanely on controllers that support I/O
  169. * writes.
  170. */
  171. #define __io_pbr() __asm__ __volatile__ ("fence io,i" : : : "memory");
  172. #define __io_par() __asm__ __volatile__ ("fence i,ior" : : : "memory");
  173. #define __io_pbw() __asm__ __volatile__ ("fence iow,o" : : : "memory");
  174. #define __io_paw() __asm__ __volatile__ ("fence o,io" : : : "memory");
  175. #define inb(c) ({ u8 __v; __io_pbr(); __v = readb_cpu((void*)(PCI_IOBASE + (c))); __io_par(); __v; })
  176. #define inw(c) ({ u16 __v; __io_pbr(); __v = readw_cpu((void*)(PCI_IOBASE + (c))); __io_par(); __v; })
  177. #define inl(c) ({ u32 __v; __io_pbr(); __v = readl_cpu((void*)(PCI_IOBASE + (c))); __io_par(); __v; })
  178. #define outb(v,c) ({ __io_pbw(); writeb_cpu((v),(void*)(PCI_IOBASE + (c))); __io_paw(); })
  179. #define outw(v,c) ({ __io_pbw(); writew_cpu((v),(void*)(PCI_IOBASE + (c))); __io_paw(); })
  180. #define outl(v,c) ({ __io_pbw(); writel_cpu((v),(void*)(PCI_IOBASE + (c))); __io_paw(); })
  181. #ifdef CONFIG_64BIT
  182. #define inq(c) ({ u64 __v; __io_pbr(); __v = readq_cpu((void*)(c)); __io_par(); __v; })
  183. #define outq(v,c) ({ __io_pbw(); writeq_cpu((v),(void*)(c)); __io_paw(); })
  184. #endif
  185. /*
  186. * Accesses from a single hart to a single I/O address must be ordered. This
  187. * allows us to use the raw read macros, but we still need to fence before and
  188. * after the block to ensure ordering WRT other macros. These are defined to
  189. * perform host-endian accesses so we use __raw instead of __cpu.
  190. */
  191. #define __io_reads_ins(port, ctype, len, bfence, afence) \
  192. static inline void __ ## port ## len(const volatile void __iomem *addr, \
  193. void *buffer, \
  194. unsigned int count) \
  195. { \
  196. bfence; \
  197. if (count) { \
  198. ctype *buf = buffer; \
  199. \
  200. do { \
  201. ctype x = __raw_read ## len(addr); \
  202. *buf++ = x; \
  203. } while (--count); \
  204. } \
  205. afence; \
  206. }
  207. #define __io_writes_outs(port, ctype, len, bfence, afence) \
  208. static inline void __ ## port ## len(volatile void __iomem *addr, \
  209. const void *buffer, \
  210. unsigned int count) \
  211. { \
  212. bfence; \
  213. if (count) { \
  214. const ctype *buf = buffer; \
  215. \
  216. do { \
  217. __raw_writeq(*buf++, addr); \
  218. } while (--count); \
  219. } \
  220. afence; \
  221. }
  222. __io_reads_ins(reads, u8, b, __io_br(), __io_ar())
  223. __io_reads_ins(reads, u16, w, __io_br(), __io_ar())
  224. __io_reads_ins(reads, u32, l, __io_br(), __io_ar())
  225. #define readsb(addr, buffer, count) __readsb(addr, buffer, count)
  226. #define readsw(addr, buffer, count) __readsw(addr, buffer, count)
  227. #define readsl(addr, buffer, count) __readsl(addr, buffer, count)
  228. __io_reads_ins(ins, u8, b, __io_pbr(), __io_par())
  229. __io_reads_ins(ins, u16, w, __io_pbr(), __io_par())
  230. __io_reads_ins(ins, u32, l, __io_pbr(), __io_par())
  231. #define insb(addr, buffer, count) __insb((void __iomem *)addr, buffer, count)
  232. #define insw(addr, buffer, count) __insw((void __iomem *)addr, buffer, count)
  233. #define insl(addr, buffer, count) __insl((void __iomem *)addr, buffer, count)
  234. __io_writes_outs(writes, u8, b, __io_bw(), __io_aw())
  235. __io_writes_outs(writes, u16, w, __io_bw(), __io_aw())
  236. __io_writes_outs(writes, u32, l, __io_bw(), __io_aw())
  237. #define writesb(addr, buffer, count) __writesb(addr, buffer, count)
  238. #define writesw(addr, buffer, count) __writesw(addr, buffer, count)
  239. #define writesl(addr, buffer, count) __writesl(addr, buffer, count)
  240. __io_writes_outs(outs, u8, b, __io_pbw(), __io_paw())
  241. __io_writes_outs(outs, u16, w, __io_pbw(), __io_paw())
  242. __io_writes_outs(outs, u32, l, __io_pbw(), __io_paw())
  243. #define outsb(addr, buffer, count) __outsb((void __iomem *)addr, buffer, count)
  244. #define outsw(addr, buffer, count) __outsw((void __iomem *)addr, buffer, count)
  245. #define outsl(addr, buffer, count) __outsl((void __iomem *)addr, buffer, count)
  246. #ifdef CONFIG_64BIT
  247. __io_reads_ins(reads, u64, q, __io_br(), __io_ar())
  248. #define readsq(addr, buffer, count) __readsq(addr, buffer, count)
  249. __io_reads_ins(ins, u64, q, __io_pbr(), __io_par())
  250. #define insq(addr, buffer, count) __insq((void __iomem *)addr, buffer, count)
  251. __io_writes_outs(writes, u64, q, __io_bw(), __io_aw())
  252. #define writesq(addr, buffer, count) __writesq(addr, buffer, count)
  253. __io_writes_outs(outs, u64, q, __io_pbr(), __io_paw())
  254. #define outsq(addr, buffer, count) __outsq((void __iomem *)addr, buffer, count)
  255. #endif
  256. #include <asm-generic/io.h>
  257. #endif /* _ASM_RISCV_IO_H */