io.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * arch/arm/include/asm/io.h
  4. *
  5. * Copyright (C) 1996-2000 Russell King
  6. *
  7. * Modifications:
  8. * 16-Sep-1996 RMK Inlined the inx/outx functions & optimised for both
  9. * constant addresses and variable addresses.
  10. * 04-Dec-1997 RMK Moved a lot of this stuff to the new architecture
  11. * specific IO header files.
  12. * 27-Mar-1999 PJB Second parameter of memcpy_toio is const..
  13. * 04-Apr-1999 PJB Added check_signature.
  14. * 12-Dec-1999 RMK More cleanups
  15. * 18-Jun-2000 RMK Removed virt_to_* and friends definitions
  16. * 05-Oct-2004 BJD Moved memory string functions to use void __iomem
  17. */
  18. #ifndef __ASM_ARM_IO_H
  19. #define __ASM_ARM_IO_H
  20. #ifdef __KERNEL__
  21. #include <linux/string.h>
  22. #include <linux/types.h>
  23. #include <asm/byteorder.h>
  24. #include <asm/memory.h>
  25. #include <asm-generic/pci_iomap.h>
  26. /*
  27. * ISA I/O bus memory addresses are 1:1 with the physical address.
  28. */
  29. #define isa_virt_to_bus virt_to_phys
  30. #define isa_bus_to_virt phys_to_virt
  31. /*
  32. * Atomic MMIO-wide IO modify
  33. */
  34. extern void atomic_io_modify(void __iomem *reg, u32 mask, u32 set);
  35. extern void atomic_io_modify_relaxed(void __iomem *reg, u32 mask, u32 set);
  36. /*
  37. * Generic IO read/write. These perform native-endian accesses. Note
  38. * that some architectures will want to re-define __raw_{read,write}w.
  39. */
  40. void __raw_writesb(volatile void __iomem *addr, const void *data, int bytelen);
  41. void __raw_writesw(volatile void __iomem *addr, const void *data, int wordlen);
  42. void __raw_writesl(volatile void __iomem *addr, const void *data, int longlen);
  43. void __raw_readsb(const volatile void __iomem *addr, void *data, int bytelen);
  44. void __raw_readsw(const volatile void __iomem *addr, void *data, int wordlen);
  45. void __raw_readsl(const volatile void __iomem *addr, void *data, int longlen);
  46. #if __LINUX_ARM_ARCH__ < 6
  47. /*
  48. * Half-word accesses are problematic with RiscPC due to limitations of
  49. * the bus. Rather than special-case the machine, just let the compiler
  50. * generate the access for CPUs prior to ARMv6.
  51. */
  52. #define __raw_readw(a) (__chk_io_ptr(a), *(volatile unsigned short __force *)(a))
  53. #define __raw_writew(v,a) ((void)(__chk_io_ptr(a), *(volatile unsigned short __force *)(a) = (v)))
  54. #else
  55. /*
  56. * When running under a hypervisor, we want to avoid I/O accesses with
  57. * writeback addressing modes as these incur a significant performance
  58. * overhead (the address generation must be emulated in software).
  59. */
  60. #define __raw_writew __raw_writew
  61. static inline void __raw_writew(u16 val, volatile void __iomem *addr)
  62. {
  63. asm volatile("strh %1, %0"
  64. : : "Q" (*(volatile u16 __force *)addr), "r" (val));
  65. }
  66. #define __raw_readw __raw_readw
  67. static inline u16 __raw_readw(const volatile void __iomem *addr)
  68. {
  69. u16 val;
  70. asm volatile("ldrh %0, %1"
  71. : "=r" (val)
  72. : "Q" (*(volatile u16 __force *)addr));
  73. return val;
  74. }
  75. #endif
  76. #define __raw_writeb __raw_writeb
  77. static inline void __raw_writeb(u8 val, volatile void __iomem *addr)
  78. {
  79. asm volatile("strb %1, %0"
  80. : : "Qo" (*(volatile u8 __force *)addr), "r" (val));
  81. }
  82. #define __raw_writel __raw_writel
  83. static inline void __raw_writel(u32 val, volatile void __iomem *addr)
  84. {
  85. asm volatile("str %1, %0"
  86. : : "Qo" (*(volatile u32 __force *)addr), "r" (val));
  87. }
  88. #define __raw_readb __raw_readb
  89. static inline u8 __raw_readb(const volatile void __iomem *addr)
  90. {
  91. u8 val;
  92. asm volatile("ldrb %0, %1"
  93. : "=r" (val)
  94. : "Qo" (*(volatile u8 __force *)addr));
  95. return val;
  96. }
  97. #define __raw_readl __raw_readl
  98. static inline u32 __raw_readl(const volatile void __iomem *addr)
  99. {
  100. u32 val;
  101. asm volatile("ldr %0, %1"
  102. : "=r" (val)
  103. : "Qo" (*(volatile u32 __force *)addr));
  104. return val;
  105. }
  106. /*
  107. * Architecture ioremap implementation.
  108. */
  109. #define MT_DEVICE 0
  110. #define MT_DEVICE_NONSHARED 1
  111. #define MT_DEVICE_CACHED 2
  112. #define MT_DEVICE_WC 3
  113. /*
  114. * types 4 onwards can be found in asm/mach/map.h and are undefined
  115. * for ioremap
  116. */
  117. /*
  118. * __arm_ioremap takes CPU physical address.
  119. * __arm_ioremap_pfn takes a Page Frame Number and an offset into that page
  120. * The _caller variety takes a __builtin_return_address(0) value for
  121. * /proc/vmalloc to use - and should only be used in non-inline functions.
  122. */
  123. extern void __iomem *__arm_ioremap_caller(phys_addr_t, size_t, unsigned int,
  124. void *);
  125. extern void __iomem *__arm_ioremap_pfn(unsigned long, unsigned long, size_t, unsigned int);
  126. extern void __iomem *__arm_ioremap_exec(phys_addr_t, size_t, bool cached);
  127. void __arm_iomem_set_ro(void __iomem *ptr, size_t size);
  128. extern void __iomem * (*arch_ioremap_caller)(phys_addr_t, size_t,
  129. unsigned int, void *);
  130. /*
  131. * Bad read/write accesses...
  132. */
  133. extern void __readwrite_bug(const char *fn);
  134. /*
  135. * A typesafe __io() helper
  136. */
  137. static inline void __iomem *__typesafe_io(unsigned long addr)
  138. {
  139. return (void __iomem *)addr;
  140. }
  141. #define IOMEM(x) ((void __force __iomem *)(x))
  142. /* IO barriers */
  143. #ifdef CONFIG_ARM_DMA_MEM_BUFFERABLE
  144. #include <asm/barrier.h>
  145. #define __iormb() rmb()
  146. #define __iowmb() wmb()
  147. #else
  148. #define __iormb() do { } while (0)
  149. #define __iowmb() do { } while (0)
  150. #endif
  151. /* PCI fixed i/o mapping */
  152. #define PCI_IO_VIRT_BASE 0xfee00000
  153. #define PCI_IOBASE ((void __iomem *)PCI_IO_VIRT_BASE)
  154. #if defined(CONFIG_PCI) || IS_ENABLED(CONFIG_PCMCIA)
  155. void pci_ioremap_set_mem_type(int mem_type);
  156. #else
  157. static inline void pci_ioremap_set_mem_type(int mem_type) {}
  158. #endif
  159. struct resource;
  160. #define pci_remap_iospace pci_remap_iospace
  161. int pci_remap_iospace(const struct resource *res, phys_addr_t phys_addr);
  162. /*
  163. * PCI configuration space mapping function.
  164. *
  165. * The PCI specification does not allow configuration write
  166. * transactions to be posted. Add an arch specific
  167. * pci_remap_cfgspace() definition that is implemented
  168. * through strongly ordered memory mappings.
  169. */
  170. #define pci_remap_cfgspace pci_remap_cfgspace
  171. void __iomem *pci_remap_cfgspace(resource_size_t res_cookie, size_t size);
  172. /*
  173. * Now, pick up the machine-defined IO definitions
  174. */
  175. #ifdef CONFIG_NEED_MACH_IO_H
  176. #include <mach/io.h>
  177. #else
  178. #if IS_ENABLED(CONFIG_PCMCIA) || defined(CONFIG_PCI)
  179. #define IO_SPACE_LIMIT ((resource_size_t)0xfffff)
  180. #else
  181. #define IO_SPACE_LIMIT ((resource_size_t)0)
  182. #endif
  183. #define __io(a) __typesafe_io(PCI_IO_VIRT_BASE + ((a) & IO_SPACE_LIMIT))
  184. #endif
  185. /*
  186. * IO port access primitives
  187. * -------------------------
  188. *
  189. * The ARM doesn't have special IO access instructions; all IO is memory
  190. * mapped. Note that these are defined to perform little endian accesses
  191. * only. Their primary purpose is to access PCI and ISA peripherals.
  192. *
  193. * Note that for a big endian machine, this implies that the following
  194. * big endian mode connectivity is in place, as described by numerous
  195. * ARM documents:
  196. *
  197. * PCI: D0-D7 D8-D15 D16-D23 D24-D31
  198. * ARM: D24-D31 D16-D23 D8-D15 D0-D7
  199. *
  200. * The machine specific io.h include defines __io to translate an "IO"
  201. * address to a memory address.
  202. *
  203. * Note that we prevent GCC re-ordering or caching values in expressions
  204. * by introducing sequence points into the in*() definitions. Note that
  205. * __raw_* do not guarantee this behaviour.
  206. *
  207. * The {in,out}[bwl] macros are for emulating x86-style PCI/ISA IO space.
  208. */
  209. #ifdef __io
  210. #define outb(v,p) ({ __iowmb(); __raw_writeb(v,__io(p)); })
  211. #define outw(v,p) ({ __iowmb(); __raw_writew((__force __u16) \
  212. cpu_to_le16(v),__io(p)); })
  213. #define outl(v,p) ({ __iowmb(); __raw_writel((__force __u32) \
  214. cpu_to_le32(v),__io(p)); })
  215. #define inb(p) ({ __u8 __v = __raw_readb(__io(p)); __iormb(); __v; })
  216. #define inw(p) ({ __u16 __v = le16_to_cpu((__force __le16) \
  217. __raw_readw(__io(p))); __iormb(); __v; })
  218. #define inl(p) ({ __u32 __v = le32_to_cpu((__force __le32) \
  219. __raw_readl(__io(p))); __iormb(); __v; })
  220. #define outsb(p,d,l) __raw_writesb(__io(p),d,l)
  221. #define outsw(p,d,l) __raw_writesw(__io(p),d,l)
  222. #define outsl(p,d,l) __raw_writesl(__io(p),d,l)
  223. #define insb(p,d,l) __raw_readsb(__io(p),d,l)
  224. #define insw(p,d,l) __raw_readsw(__io(p),d,l)
  225. #define insl(p,d,l) __raw_readsl(__io(p),d,l)
  226. #endif
  227. /*
  228. * String version of IO memory access ops:
  229. */
  230. extern void _memcpy_fromio(void *, const volatile void __iomem *, size_t);
  231. extern void _memcpy_toio(volatile void __iomem *, const void *, size_t);
  232. extern void _memset_io(volatile void __iomem *, int, size_t);
  233. /*
  234. * Memory access primitives
  235. * ------------------------
  236. *
  237. * These perform PCI memory accesses via an ioremap region. They don't
  238. * take an address as such, but a cookie.
  239. *
  240. * Again, these are defined to perform little endian accesses. See the
  241. * IO port primitives for more information.
  242. */
  243. #ifndef readl
  244. #define readb_relaxed(c) ({ u8 __r = __raw_readb(c); __r; })
  245. #define readw_relaxed(c) ({ u16 __r = le16_to_cpu((__force __le16) \
  246. __raw_readw(c)); __r; })
  247. #define readl_relaxed(c) ({ u32 __r = le32_to_cpu((__force __le32) \
  248. __raw_readl(c)); __r; })
  249. #define writeb_relaxed(v,c) __raw_writeb(v,c)
  250. #define writew_relaxed(v,c) __raw_writew((__force u16) cpu_to_le16(v),c)
  251. #define writel_relaxed(v,c) __raw_writel((__force u32) cpu_to_le32(v),c)
  252. #define readb(c) ({ u8 __v = readb_relaxed(c); __iormb(); __v; })
  253. #define readw(c) ({ u16 __v = readw_relaxed(c); __iormb(); __v; })
  254. #define readl(c) ({ u32 __v = readl_relaxed(c); __iormb(); __v; })
  255. #define writeb(v,c) ({ __iowmb(); writeb_relaxed(v,c); })
  256. #define writew(v,c) ({ __iowmb(); writew_relaxed(v,c); })
  257. #define writel(v,c) ({ __iowmb(); writel_relaxed(v,c); })
  258. #define readsb(p,d,l) __raw_readsb(p,d,l)
  259. #define readsw(p,d,l) __raw_readsw(p,d,l)
  260. #define readsl(p,d,l) __raw_readsl(p,d,l)
  261. #define writesb(p,d,l) __raw_writesb(p,d,l)
  262. #define writesw(p,d,l) __raw_writesw(p,d,l)
  263. #define writesl(p,d,l) __raw_writesl(p,d,l)
  264. #ifndef __ARMBE__
  265. static inline void memset_io(volatile void __iomem *dst, unsigned c,
  266. size_t count)
  267. {
  268. extern void mmioset(void *, unsigned int, size_t);
  269. mmioset((void __force *)dst, c, count);
  270. }
  271. #define memset_io(dst,c,count) memset_io(dst,c,count)
  272. static inline void memcpy_fromio(void *to, const volatile void __iomem *from,
  273. size_t count)
  274. {
  275. extern void mmiocpy(void *, const void *, size_t);
  276. mmiocpy(to, (const void __force *)from, count);
  277. }
  278. #define memcpy_fromio(to,from,count) memcpy_fromio(to,from,count)
  279. static inline void memcpy_toio(volatile void __iomem *to, const void *from,
  280. size_t count)
  281. {
  282. extern void mmiocpy(void *, const void *, size_t);
  283. mmiocpy((void __force *)to, from, count);
  284. }
  285. #define memcpy_toio(to,from,count) memcpy_toio(to,from,count)
  286. #else
  287. #define memset_io(c,v,l) _memset_io(c,(v),(l))
  288. #define memcpy_fromio(a,c,l) _memcpy_fromio((a),c,(l))
  289. #define memcpy_toio(c,a,l) _memcpy_toio(c,(a),(l))
  290. #endif
  291. #endif /* readl */
  292. /*
  293. * ioremap() and friends.
  294. *
  295. * ioremap() takes a resource address, and size. Due to the ARM memory
  296. * types, it is important to use the correct ioremap() function as each
  297. * mapping has specific properties.
  298. *
  299. * Function Memory type Cacheability Cache hint
  300. * ioremap() Device n/a n/a
  301. * ioremap_cache() Normal Writeback Read allocate
  302. * ioremap_wc() Normal Non-cacheable n/a
  303. * ioremap_wt() Normal Non-cacheable n/a
  304. *
  305. * All device mappings have the following properties:
  306. * - no access speculation
  307. * - no repetition (eg, on return from an exception)
  308. * - number, order and size of accesses are maintained
  309. * - unaligned accesses are "unpredictable"
  310. * - writes may be delayed before they hit the endpoint device
  311. *
  312. * All normal memory mappings have the following properties:
  313. * - reads can be repeated with no side effects
  314. * - repeated reads return the last value written
  315. * - reads can fetch additional locations without side effects
  316. * - writes can be repeated (in certain cases) with no side effects
  317. * - writes can be merged before accessing the target
  318. * - unaligned accesses can be supported
  319. * - ordering is not guaranteed without explicit dependencies or barrier
  320. * instructions
  321. * - writes may be delayed before they hit the endpoint memory
  322. *
  323. * The cache hint is only a performance hint: CPUs may alias these hints.
  324. * Eg, a CPU not implementing read allocate but implementing write allocate
  325. * will provide a write allocate mapping instead.
  326. */
  327. void __iomem *ioremap(resource_size_t res_cookie, size_t size);
  328. #define ioremap ioremap
  329. /*
  330. * Do not use ioremap_cache for mapping memory. Use memremap instead.
  331. */
  332. void __iomem *ioremap_cache(resource_size_t res_cookie, size_t size);
  333. #define ioremap_cache ioremap_cache
  334. void __iomem *ioremap_wc(resource_size_t res_cookie, size_t size);
  335. #define ioremap_wc ioremap_wc
  336. #define ioremap_wt ioremap_wc
  337. void iounmap(volatile void __iomem *io_addr);
  338. #define iounmap iounmap
  339. void *arch_memremap_wb(phys_addr_t phys_addr, size_t size);
  340. #define arch_memremap_wb arch_memremap_wb
  341. /*
  342. * io{read,write}{16,32}be() macros
  343. */
  344. #define ioread16be(p) ({ __u16 __v = be16_to_cpu((__force __be16)__raw_readw(p)); __iormb(); __v; })
  345. #define ioread32be(p) ({ __u32 __v = be32_to_cpu((__force __be32)__raw_readl(p)); __iormb(); __v; })
  346. #define iowrite16be(v,p) ({ __iowmb(); __raw_writew((__force __u16)cpu_to_be16(v), p); })
  347. #define iowrite32be(v,p) ({ __iowmb(); __raw_writel((__force __u32)cpu_to_be32(v), p); })
  348. #ifndef ioport_map
  349. #define ioport_map ioport_map
  350. extern void __iomem *ioport_map(unsigned long port, unsigned int nr);
  351. #endif
  352. #ifndef ioport_unmap
  353. #define ioport_unmap ioport_unmap
  354. extern void ioport_unmap(void __iomem *addr);
  355. #endif
  356. struct pci_dev;
  357. #define pci_iounmap pci_iounmap
  358. extern void pci_iounmap(struct pci_dev *dev, void __iomem *addr);
  359. /*
  360. * Convert a physical pointer to a virtual kernel pointer for /dev/mem
  361. * access
  362. */
  363. #define xlate_dev_mem_ptr(p) __va(p)
  364. #include <asm-generic/io.h>
  365. #ifdef CONFIG_MMU
  366. #define ARCH_HAS_VALID_PHYS_ADDR_RANGE
  367. extern int valid_phys_addr_range(phys_addr_t addr, size_t size);
  368. extern int valid_mmap_phys_addr_range(unsigned long pfn, size_t size);
  369. extern bool arch_memremap_can_ram_remap(resource_size_t offset, size_t size,
  370. unsigned long flags);
  371. #define arch_memremap_can_ram_remap arch_memremap_can_ram_remap
  372. #endif
  373. /*
  374. * Register ISA memory and port locations for glibc iopl/inb/outb
  375. * emulation.
  376. */
  377. extern void register_isa_ports(unsigned int mmio, unsigned int io,
  378. unsigned int io_shift);
  379. #endif /* __KERNEL__ */
  380. #endif /* __ASM_ARM_IO_H */