io.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Based on arch/arm/include/asm/io.h
  4. *
  5. * Copyright (C) 1996-2000 Russell King
  6. * Copyright (C) 2012 ARM Ltd.
  7. */
  8. #ifndef __ASM_IO_H
  9. #define __ASM_IO_H
  10. #include <linux/types.h>
  11. #include <linux/pgtable.h>
  12. #include <asm/byteorder.h>
  13. #include <asm/barrier.h>
  14. #include <asm/memory.h>
  15. #include <asm/early_ioremap.h>
  16. #include <asm/alternative.h>
  17. #include <asm/cpufeature.h>
  18. /*
  19. * Generic IO read/write. These perform native-endian accesses.
  20. */
  21. #define __raw_writeb __raw_writeb
  22. static __always_inline void __raw_writeb(u8 val, volatile void __iomem *addr)
  23. {
  24. volatile u8 __iomem *ptr = addr;
  25. asm volatile("strb %w0, %1" : : "rZ" (val), "Qo" (*ptr));
  26. }
  27. #define __raw_writew __raw_writew
  28. static __always_inline void __raw_writew(u16 val, volatile void __iomem *addr)
  29. {
  30. volatile u16 __iomem *ptr = addr;
  31. asm volatile("strh %w0, %1" : : "rZ" (val), "Qo" (*ptr));
  32. }
  33. #define __raw_writel __raw_writel
  34. static __always_inline void __raw_writel(u32 val, volatile void __iomem *addr)
  35. {
  36. volatile u32 __iomem *ptr = addr;
  37. asm volatile("str %w0, %1" : : "rZ" (val), "Qo" (*ptr));
  38. }
  39. #define __raw_writeq __raw_writeq
  40. static __always_inline void __raw_writeq(u64 val, volatile void __iomem *addr)
  41. {
  42. volatile u64 __iomem *ptr = addr;
  43. asm volatile("str %x0, %1" : : "rZ" (val), "Qo" (*ptr));
  44. }
  45. #define __raw_readb __raw_readb
  46. static __always_inline u8 __raw_readb(const volatile void __iomem *addr)
  47. {
  48. u8 val;
  49. asm volatile(ALTERNATIVE("ldrb %w0, [%1]",
  50. "ldarb %w0, [%1]",
  51. ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE)
  52. : "=r" (val) : "r" (addr));
  53. return val;
  54. }
  55. #define __raw_readw __raw_readw
  56. static __always_inline u16 __raw_readw(const volatile void __iomem *addr)
  57. {
  58. u16 val;
  59. asm volatile(ALTERNATIVE("ldrh %w0, [%1]",
  60. "ldarh %w0, [%1]",
  61. ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE)
  62. : "=r" (val) : "r" (addr));
  63. return val;
  64. }
  65. #define __raw_readl __raw_readl
  66. static __always_inline u32 __raw_readl(const volatile void __iomem *addr)
  67. {
  68. u32 val;
  69. asm volatile(ALTERNATIVE("ldr %w0, [%1]",
  70. "ldar %w0, [%1]",
  71. ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE)
  72. : "=r" (val) : "r" (addr));
  73. return val;
  74. }
  75. #define __raw_readq __raw_readq
  76. static __always_inline u64 __raw_readq(const volatile void __iomem *addr)
  77. {
  78. u64 val;
  79. asm volatile(ALTERNATIVE("ldr %0, [%1]",
  80. "ldar %0, [%1]",
  81. ARM64_WORKAROUND_DEVICE_LOAD_ACQUIRE)
  82. : "=r" (val) : "r" (addr));
  83. return val;
  84. }
  85. /* IO barriers */
  86. #define __io_ar(v) \
  87. ({ \
  88. unsigned long tmp; \
  89. \
  90. dma_rmb(); \
  91. \
  92. /* \
  93. * Create a dummy control dependency from the IO read to any \
  94. * later instructions. This ensures that a subsequent call to \
  95. * udelay() will be ordered due to the ISB in get_cycles(). \
  96. */ \
  97. asm volatile("eor %0, %1, %1\n" \
  98. "cbnz %0, ." \
  99. : "=r" (tmp) : "r" ((unsigned long)(v)) \
  100. : "memory"); \
  101. })
  102. #define __io_bw() dma_wmb()
  103. #define __io_br(v)
  104. #define __io_aw(v)
  105. /* arm64-specific, don't use in portable drivers */
  106. #define __iormb(v) __io_ar(v)
  107. #define __iowmb() __io_bw()
  108. #define __iomb() dma_mb()
  109. /*
  110. * I/O port access primitives.
  111. */
  112. #define arch_has_dev_port() (1)
  113. #define IO_SPACE_LIMIT (PCI_IO_SIZE - 1)
  114. #define PCI_IOBASE ((void __iomem *)PCI_IO_START)
  115. /*
  116. * String version of I/O memory access operations.
  117. */
  118. extern void __memcpy_fromio(void *, const volatile void __iomem *, size_t);
  119. extern void __memcpy_toio(volatile void __iomem *, const void *, size_t);
  120. extern void __memset_io(volatile void __iomem *, int, size_t);
  121. #define memset_io(c,v,l) __memset_io((c),(v),(l))
  122. #define memcpy_fromio(a,c,l) __memcpy_fromio((a),(c),(l))
  123. #define memcpy_toio(c,a,l) __memcpy_toio((c),(a),(l))
  124. /*
  125. * The ARM64 iowrite implementation is intended to support drivers that want to
  126. * use write combining. For instance PCI drivers using write combining with a 64
  127. * byte __iowrite64_copy() expect to get a 64 byte MemWr TLP on the PCIe bus.
  128. *
  129. * Newer ARM core have sensitive write combining buffers, it is important that
  130. * the stores be contiguous blocks of store instructions. Normal memcpy
  131. * approaches have a very low chance to generate write combining.
  132. *
  133. * Since this is the only API on ARM64 that should be used with write combining
  134. * it also integrates the DGH hint which is supposed to lower the latency to
  135. * emit the large TLP from the CPU.
  136. */
  137. static __always_inline void
  138. __const_memcpy_toio_aligned32(volatile u32 __iomem *to, const u32 *from,
  139. size_t count)
  140. {
  141. switch (count) {
  142. case 8:
  143. asm volatile("str %w0, [%8, #4 * 0]\n"
  144. "str %w1, [%8, #4 * 1]\n"
  145. "str %w2, [%8, #4 * 2]\n"
  146. "str %w3, [%8, #4 * 3]\n"
  147. "str %w4, [%8, #4 * 4]\n"
  148. "str %w5, [%8, #4 * 5]\n"
  149. "str %w6, [%8, #4 * 6]\n"
  150. "str %w7, [%8, #4 * 7]\n"
  151. :
  152. : "rZ"(from[0]), "rZ"(from[1]), "rZ"(from[2]),
  153. "rZ"(from[3]), "rZ"(from[4]), "rZ"(from[5]),
  154. "rZ"(from[6]), "rZ"(from[7]), "r"(to));
  155. break;
  156. case 4:
  157. asm volatile("str %w0, [%4, #4 * 0]\n"
  158. "str %w1, [%4, #4 * 1]\n"
  159. "str %w2, [%4, #4 * 2]\n"
  160. "str %w3, [%4, #4 * 3]\n"
  161. :
  162. : "rZ"(from[0]), "rZ"(from[1]), "rZ"(from[2]),
  163. "rZ"(from[3]), "r"(to));
  164. break;
  165. case 2:
  166. asm volatile("str %w0, [%2, #4 * 0]\n"
  167. "str %w1, [%2, #4 * 1]\n"
  168. :
  169. : "rZ"(from[0]), "rZ"(from[1]), "r"(to));
  170. break;
  171. case 1:
  172. __raw_writel(*from, to);
  173. break;
  174. default:
  175. BUILD_BUG();
  176. }
  177. }
  178. void __iowrite32_copy_full(void __iomem *to, const void *from, size_t count);
  179. static __always_inline void
  180. __iowrite32_copy(void __iomem *to, const void *from, size_t count)
  181. {
  182. if (__builtin_constant_p(count) &&
  183. (count == 8 || count == 4 || count == 2 || count == 1)) {
  184. __const_memcpy_toio_aligned32(to, from, count);
  185. dgh();
  186. } else {
  187. __iowrite32_copy_full(to, from, count);
  188. }
  189. }
  190. #define __iowrite32_copy __iowrite32_copy
  191. static __always_inline void
  192. __const_memcpy_toio_aligned64(volatile u64 __iomem *to, const u64 *from,
  193. size_t count)
  194. {
  195. switch (count) {
  196. case 8:
  197. asm volatile("str %x0, [%8, #8 * 0]\n"
  198. "str %x1, [%8, #8 * 1]\n"
  199. "str %x2, [%8, #8 * 2]\n"
  200. "str %x3, [%8, #8 * 3]\n"
  201. "str %x4, [%8, #8 * 4]\n"
  202. "str %x5, [%8, #8 * 5]\n"
  203. "str %x6, [%8, #8 * 6]\n"
  204. "str %x7, [%8, #8 * 7]\n"
  205. :
  206. : "rZ"(from[0]), "rZ"(from[1]), "rZ"(from[2]),
  207. "rZ"(from[3]), "rZ"(from[4]), "rZ"(from[5]),
  208. "rZ"(from[6]), "rZ"(from[7]), "r"(to));
  209. break;
  210. case 4:
  211. asm volatile("str %x0, [%4, #8 * 0]\n"
  212. "str %x1, [%4, #8 * 1]\n"
  213. "str %x2, [%4, #8 * 2]\n"
  214. "str %x3, [%4, #8 * 3]\n"
  215. :
  216. : "rZ"(from[0]), "rZ"(from[1]), "rZ"(from[2]),
  217. "rZ"(from[3]), "r"(to));
  218. break;
  219. case 2:
  220. asm volatile("str %x0, [%2, #8 * 0]\n"
  221. "str %x1, [%2, #8 * 1]\n"
  222. :
  223. : "rZ"(from[0]), "rZ"(from[1]), "r"(to));
  224. break;
  225. case 1:
  226. __raw_writeq(*from, to);
  227. break;
  228. default:
  229. BUILD_BUG();
  230. }
  231. }
  232. void __iowrite64_copy_full(void __iomem *to, const void *from, size_t count);
  233. static __always_inline void
  234. __iowrite64_copy(void __iomem *to, const void *from, size_t count)
  235. {
  236. if (__builtin_constant_p(count) &&
  237. (count == 8 || count == 4 || count == 2 || count == 1)) {
  238. __const_memcpy_toio_aligned64(to, from, count);
  239. dgh();
  240. } else {
  241. __iowrite64_copy_full(to, from, count);
  242. }
  243. }
  244. #define __iowrite64_copy __iowrite64_copy
  245. /*
  246. * I/O memory mapping functions.
  247. */
  248. typedef int (*ioremap_prot_hook_t)(phys_addr_t phys_addr, size_t size,
  249. pgprot_t *prot);
  250. int arm64_ioremap_prot_hook_register(const ioremap_prot_hook_t hook);
  251. #define ioremap_prot ioremap_prot
  252. #define _PAGE_IOREMAP PROT_DEVICE_nGnRE
  253. #define ioremap_wc(addr, size) \
  254. ioremap_prot((addr), (size), PROT_NORMAL_NC)
  255. #define ioremap_np(addr, size) \
  256. ioremap_prot((addr), (size), PROT_DEVICE_nGnRnE)
  257. /*
  258. * io{read,write}{16,32,64}be() macros
  259. */
  260. #define ioread16be(p) ({ __u16 __v = be16_to_cpu((__force __be16)__raw_readw(p)); __iormb(__v); __v; })
  261. #define ioread32be(p) ({ __u32 __v = be32_to_cpu((__force __be32)__raw_readl(p)); __iormb(__v); __v; })
  262. #define ioread64be(p) ({ __u64 __v = be64_to_cpu((__force __be64)__raw_readq(p)); __iormb(__v); __v; })
  263. #define iowrite16be(v,p) ({ __iowmb(); __raw_writew((__force __u16)cpu_to_be16(v), p); })
  264. #define iowrite32be(v,p) ({ __iowmb(); __raw_writel((__force __u32)cpu_to_be32(v), p); })
  265. #define iowrite64be(v,p) ({ __iowmb(); __raw_writeq((__force __u64)cpu_to_be64(v), p); })
  266. #include <asm-generic/io.h>
  267. #define ioremap_cache ioremap_cache
  268. static inline void __iomem *ioremap_cache(phys_addr_t addr, size_t size)
  269. {
  270. if (pfn_is_map_memory(__phys_to_pfn(addr)))
  271. return (void __iomem *)__phys_to_virt(addr);
  272. return ioremap_prot(addr, size, PROT_NORMAL);
  273. }
  274. /*
  275. * More restrictive address range checking than the default implementation
  276. * (PHYS_OFFSET and PHYS_MASK taken into account).
  277. */
  278. #define ARCH_HAS_VALID_PHYS_ADDR_RANGE
  279. extern int valid_phys_addr_range(phys_addr_t addr, size_t size);
  280. extern int valid_mmap_phys_addr_range(unsigned long pfn, size_t size);
  281. extern bool arch_memremap_can_ram_remap(resource_size_t offset, size_t size,
  282. unsigned long flags);
  283. #define arch_memremap_can_ram_remap arch_memremap_can_ram_remap
  284. #endif /* __ASM_IO_H */