io.h 12 KB

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