io.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _TOOLS_ASM_GENERIC_IO_H
  3. #define _TOOLS_ASM_GENERIC_IO_H
  4. #include <asm/barrier.h>
  5. #include <asm/byteorder.h>
  6. #include <linux/compiler.h>
  7. #include <linux/kernel.h>
  8. #include <linux/types.h>
  9. #ifndef mmiowb_set_pending
  10. #define mmiowb_set_pending() do { } while (0)
  11. #endif
  12. #ifndef __io_br
  13. #define __io_br() barrier()
  14. #endif
  15. /* prevent prefetching of coherent DMA data ahead of a dma-complete */
  16. #ifndef __io_ar
  17. #ifdef rmb
  18. #define __io_ar(v) rmb()
  19. #else
  20. #define __io_ar(v) barrier()
  21. #endif
  22. #endif
  23. /* flush writes to coherent DMA data before possibly triggering a DMA read */
  24. #ifndef __io_bw
  25. #ifdef wmb
  26. #define __io_bw() wmb()
  27. #else
  28. #define __io_bw() barrier()
  29. #endif
  30. #endif
  31. /* serialize device access against a spin_unlock, usually handled there. */
  32. #ifndef __io_aw
  33. #define __io_aw() mmiowb_set_pending()
  34. #endif
  35. #ifndef __io_pbw
  36. #define __io_pbw() __io_bw()
  37. #endif
  38. #ifndef __io_paw
  39. #define __io_paw() __io_aw()
  40. #endif
  41. #ifndef __io_pbr
  42. #define __io_pbr() __io_br()
  43. #endif
  44. #ifndef __io_par
  45. #define __io_par(v) __io_ar(v)
  46. #endif
  47. #ifndef _THIS_IP_
  48. #define _THIS_IP_ 0
  49. #endif
  50. static inline void log_write_mmio(u64 val, u8 width, volatile void __iomem *addr,
  51. unsigned long caller_addr, unsigned long caller_addr0) {}
  52. static inline void log_post_write_mmio(u64 val, u8 width, volatile void __iomem *addr,
  53. unsigned long caller_addr, unsigned long caller_addr0) {}
  54. static inline void log_read_mmio(u8 width, const volatile void __iomem *addr,
  55. unsigned long caller_addr, unsigned long caller_addr0) {}
  56. static inline void log_post_read_mmio(u64 val, u8 width, const volatile void __iomem *addr,
  57. unsigned long caller_addr, unsigned long caller_addr0) {}
  58. /*
  59. * __raw_{read,write}{b,w,l,q}() access memory in native endianness.
  60. *
  61. * On some architectures memory mapped IO needs to be accessed differently.
  62. * On the simple architectures, we just read/write the memory location
  63. * directly.
  64. */
  65. #ifndef __raw_readb
  66. #define __raw_readb __raw_readb
  67. static inline u8 __raw_readb(const volatile void __iomem *addr)
  68. {
  69. return *(const volatile u8 __force *)addr;
  70. }
  71. #endif
  72. #ifndef __raw_readw
  73. #define __raw_readw __raw_readw
  74. static inline u16 __raw_readw(const volatile void __iomem *addr)
  75. {
  76. return *(const volatile u16 __force *)addr;
  77. }
  78. #endif
  79. #ifndef __raw_readl
  80. #define __raw_readl __raw_readl
  81. static inline u32 __raw_readl(const volatile void __iomem *addr)
  82. {
  83. return *(const volatile u32 __force *)addr;
  84. }
  85. #endif
  86. #ifndef __raw_readq
  87. #define __raw_readq __raw_readq
  88. static inline u64 __raw_readq(const volatile void __iomem *addr)
  89. {
  90. return *(const volatile u64 __force *)addr;
  91. }
  92. #endif
  93. #ifndef __raw_writeb
  94. #define __raw_writeb __raw_writeb
  95. static inline void __raw_writeb(u8 value, volatile void __iomem *addr)
  96. {
  97. *(volatile u8 __force *)addr = value;
  98. }
  99. #endif
  100. #ifndef __raw_writew
  101. #define __raw_writew __raw_writew
  102. static inline void __raw_writew(u16 value, volatile void __iomem *addr)
  103. {
  104. *(volatile u16 __force *)addr = value;
  105. }
  106. #endif
  107. #ifndef __raw_writel
  108. #define __raw_writel __raw_writel
  109. static inline void __raw_writel(u32 value, volatile void __iomem *addr)
  110. {
  111. *(volatile u32 __force *)addr = value;
  112. }
  113. #endif
  114. #ifndef __raw_writeq
  115. #define __raw_writeq __raw_writeq
  116. static inline void __raw_writeq(u64 value, volatile void __iomem *addr)
  117. {
  118. *(volatile u64 __force *)addr = value;
  119. }
  120. #endif
  121. /*
  122. * {read,write}{b,w,l,q}() access little endian memory and return result in
  123. * native endianness.
  124. */
  125. #ifndef readb
  126. #define readb readb
  127. static inline u8 readb(const volatile void __iomem *addr)
  128. {
  129. u8 val;
  130. log_read_mmio(8, addr, _THIS_IP_, _RET_IP_);
  131. __io_br();
  132. val = __raw_readb(addr);
  133. __io_ar(val);
  134. log_post_read_mmio(val, 8, addr, _THIS_IP_, _RET_IP_);
  135. return val;
  136. }
  137. #endif
  138. #ifndef readw
  139. #define readw readw
  140. static inline u16 readw(const volatile void __iomem *addr)
  141. {
  142. u16 val;
  143. log_read_mmio(16, addr, _THIS_IP_, _RET_IP_);
  144. __io_br();
  145. val = __le16_to_cpu((__le16 __force)__raw_readw(addr));
  146. __io_ar(val);
  147. log_post_read_mmio(val, 16, addr, _THIS_IP_, _RET_IP_);
  148. return val;
  149. }
  150. #endif
  151. #ifndef readl
  152. #define readl readl
  153. static inline u32 readl(const volatile void __iomem *addr)
  154. {
  155. u32 val;
  156. log_read_mmio(32, addr, _THIS_IP_, _RET_IP_);
  157. __io_br();
  158. val = __le32_to_cpu((__le32 __force)__raw_readl(addr));
  159. __io_ar(val);
  160. log_post_read_mmio(val, 32, addr, _THIS_IP_, _RET_IP_);
  161. return val;
  162. }
  163. #endif
  164. #ifndef readq
  165. #define readq readq
  166. static inline u64 readq(const volatile void __iomem *addr)
  167. {
  168. u64 val;
  169. log_read_mmio(64, addr, _THIS_IP_, _RET_IP_);
  170. __io_br();
  171. val = __le64_to_cpu((__le64 __force)__raw_readq(addr));
  172. __io_ar(val);
  173. log_post_read_mmio(val, 64, addr, _THIS_IP_, _RET_IP_);
  174. return val;
  175. }
  176. #endif
  177. #ifndef writeb
  178. #define writeb writeb
  179. static inline void writeb(u8 value, volatile void __iomem *addr)
  180. {
  181. log_write_mmio(value, 8, addr, _THIS_IP_, _RET_IP_);
  182. __io_bw();
  183. __raw_writeb(value, addr);
  184. __io_aw();
  185. log_post_write_mmio(value, 8, addr, _THIS_IP_, _RET_IP_);
  186. }
  187. #endif
  188. #ifndef writew
  189. #define writew writew
  190. static inline void writew(u16 value, volatile void __iomem *addr)
  191. {
  192. log_write_mmio(value, 16, addr, _THIS_IP_, _RET_IP_);
  193. __io_bw();
  194. __raw_writew((u16 __force)cpu_to_le16(value), addr);
  195. __io_aw();
  196. log_post_write_mmio(value, 16, addr, _THIS_IP_, _RET_IP_);
  197. }
  198. #endif
  199. #ifndef writel
  200. #define writel writel
  201. static inline void writel(u32 value, volatile void __iomem *addr)
  202. {
  203. log_write_mmio(value, 32, addr, _THIS_IP_, _RET_IP_);
  204. __io_bw();
  205. __raw_writel((u32 __force)__cpu_to_le32(value), addr);
  206. __io_aw();
  207. log_post_write_mmio(value, 32, addr, _THIS_IP_, _RET_IP_);
  208. }
  209. #endif
  210. #ifndef writeq
  211. #define writeq writeq
  212. static inline void writeq(u64 value, volatile void __iomem *addr)
  213. {
  214. log_write_mmio(value, 64, addr, _THIS_IP_, _RET_IP_);
  215. __io_bw();
  216. __raw_writeq((u64 __force)__cpu_to_le64(value), addr);
  217. __io_aw();
  218. log_post_write_mmio(value, 64, addr, _THIS_IP_, _RET_IP_);
  219. }
  220. #endif
  221. /*
  222. * {read,write}{b,w,l,q}_relaxed() are like the regular version, but
  223. * are not guaranteed to provide ordering against spinlocks or memory
  224. * accesses.
  225. */
  226. #ifndef readb_relaxed
  227. #define readb_relaxed readb_relaxed
  228. static inline u8 readb_relaxed(const volatile void __iomem *addr)
  229. {
  230. u8 val;
  231. log_read_mmio(8, addr, _THIS_IP_, _RET_IP_);
  232. val = __raw_readb(addr);
  233. log_post_read_mmio(val, 8, addr, _THIS_IP_, _RET_IP_);
  234. return val;
  235. }
  236. #endif
  237. #ifndef readw_relaxed
  238. #define readw_relaxed readw_relaxed
  239. static inline u16 readw_relaxed(const volatile void __iomem *addr)
  240. {
  241. u16 val;
  242. log_read_mmio(16, addr, _THIS_IP_, _RET_IP_);
  243. val = __le16_to_cpu((__le16 __force)__raw_readw(addr));
  244. log_post_read_mmio(val, 16, addr, _THIS_IP_, _RET_IP_);
  245. return val;
  246. }
  247. #endif
  248. #ifndef readl_relaxed
  249. #define readl_relaxed readl_relaxed
  250. static inline u32 readl_relaxed(const volatile void __iomem *addr)
  251. {
  252. u32 val;
  253. log_read_mmio(32, addr, _THIS_IP_, _RET_IP_);
  254. val = __le32_to_cpu((__le32 __force)__raw_readl(addr));
  255. log_post_read_mmio(val, 32, addr, _THIS_IP_, _RET_IP_);
  256. return val;
  257. }
  258. #endif
  259. #if defined(readq) && !defined(readq_relaxed)
  260. #define readq_relaxed readq_relaxed
  261. static inline u64 readq_relaxed(const volatile void __iomem *addr)
  262. {
  263. u64 val;
  264. log_read_mmio(64, addr, _THIS_IP_, _RET_IP_);
  265. val = __le64_to_cpu((__le64 __force)__raw_readq(addr));
  266. log_post_read_mmio(val, 64, addr, _THIS_IP_, _RET_IP_);
  267. return val;
  268. }
  269. #endif
  270. #ifndef writeb_relaxed
  271. #define writeb_relaxed writeb_relaxed
  272. static inline void writeb_relaxed(u8 value, volatile void __iomem *addr)
  273. {
  274. log_write_mmio(value, 8, addr, _THIS_IP_, _RET_IP_);
  275. __raw_writeb(value, addr);
  276. log_post_write_mmio(value, 8, addr, _THIS_IP_, _RET_IP_);
  277. }
  278. #endif
  279. #ifndef writew_relaxed
  280. #define writew_relaxed writew_relaxed
  281. static inline void writew_relaxed(u16 value, volatile void __iomem *addr)
  282. {
  283. log_write_mmio(value, 16, addr, _THIS_IP_, _RET_IP_);
  284. __raw_writew((u16 __force)cpu_to_le16(value), addr);
  285. log_post_write_mmio(value, 16, addr, _THIS_IP_, _RET_IP_);
  286. }
  287. #endif
  288. #ifndef writel_relaxed
  289. #define writel_relaxed writel_relaxed
  290. static inline void writel_relaxed(u32 value, volatile void __iomem *addr)
  291. {
  292. log_write_mmio(value, 32, addr, _THIS_IP_, _RET_IP_);
  293. __raw_writel((u32 __force)__cpu_to_le32(value), addr);
  294. log_post_write_mmio(value, 32, addr, _THIS_IP_, _RET_IP_);
  295. }
  296. #endif
  297. #if defined(writeq) && !defined(writeq_relaxed)
  298. #define writeq_relaxed writeq_relaxed
  299. static inline void writeq_relaxed(u64 value, volatile void __iomem *addr)
  300. {
  301. log_write_mmio(value, 64, addr, _THIS_IP_, _RET_IP_);
  302. __raw_writeq((u64 __force)__cpu_to_le64(value), addr);
  303. log_post_write_mmio(value, 64, addr, _THIS_IP_, _RET_IP_);
  304. }
  305. #endif
  306. /*
  307. * {read,write}s{b,w,l,q}() repeatedly access the same memory address in
  308. * native endianness in 8-, 16-, 32- or 64-bit chunks (@count times).
  309. */
  310. #ifndef readsb
  311. #define readsb readsb
  312. static inline void readsb(const volatile void __iomem *addr, void *buffer,
  313. unsigned int count)
  314. {
  315. if (count) {
  316. u8 *buf = buffer;
  317. do {
  318. u8 x = __raw_readb(addr);
  319. *buf++ = x;
  320. } while (--count);
  321. }
  322. }
  323. #endif
  324. #ifndef readsw
  325. #define readsw readsw
  326. static inline void readsw(const volatile void __iomem *addr, void *buffer,
  327. unsigned int count)
  328. {
  329. if (count) {
  330. u16 *buf = buffer;
  331. do {
  332. u16 x = __raw_readw(addr);
  333. *buf++ = x;
  334. } while (--count);
  335. }
  336. }
  337. #endif
  338. #ifndef readsl
  339. #define readsl readsl
  340. static inline void readsl(const volatile void __iomem *addr, void *buffer,
  341. unsigned int count)
  342. {
  343. if (count) {
  344. u32 *buf = buffer;
  345. do {
  346. u32 x = __raw_readl(addr);
  347. *buf++ = x;
  348. } while (--count);
  349. }
  350. }
  351. #endif
  352. #ifndef readsq
  353. #define readsq readsq
  354. static inline void readsq(const volatile void __iomem *addr, void *buffer,
  355. unsigned int count)
  356. {
  357. if (count) {
  358. u64 *buf = buffer;
  359. do {
  360. u64 x = __raw_readq(addr);
  361. *buf++ = x;
  362. } while (--count);
  363. }
  364. }
  365. #endif
  366. #ifndef writesb
  367. #define writesb writesb
  368. static inline void writesb(volatile void __iomem *addr, const void *buffer,
  369. unsigned int count)
  370. {
  371. if (count) {
  372. const u8 *buf = buffer;
  373. do {
  374. __raw_writeb(*buf++, addr);
  375. } while (--count);
  376. }
  377. }
  378. #endif
  379. #ifndef writesw
  380. #define writesw writesw
  381. static inline void writesw(volatile void __iomem *addr, const void *buffer,
  382. unsigned int count)
  383. {
  384. if (count) {
  385. const u16 *buf = buffer;
  386. do {
  387. __raw_writew(*buf++, addr);
  388. } while (--count);
  389. }
  390. }
  391. #endif
  392. #ifndef writesl
  393. #define writesl writesl
  394. static inline void writesl(volatile void __iomem *addr, const void *buffer,
  395. unsigned int count)
  396. {
  397. if (count) {
  398. const u32 *buf = buffer;
  399. do {
  400. __raw_writel(*buf++, addr);
  401. } while (--count);
  402. }
  403. }
  404. #endif
  405. #ifndef writesq
  406. #define writesq writesq
  407. static inline void writesq(volatile void __iomem *addr, const void *buffer,
  408. unsigned int count)
  409. {
  410. if (count) {
  411. const u64 *buf = buffer;
  412. do {
  413. __raw_writeq(*buf++, addr);
  414. } while (--count);
  415. }
  416. }
  417. #endif
  418. #endif /* _TOOLS_ASM_GENERIC_IO_H */