1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Generic I/O functions.
5 * Copyright (c) 2016 Imagination Technologies Ltd.
8 #ifndef __ASM_GENERIC_IO_H__
9 #define __ASM_GENERIC_IO_H__
12 * This file should be included at the end of each architecture-specific
13 * asm/io.h such that we may provide generic implementations without
14 * conflicting with architecture-specific code.
20 * phys_to_virt() - Return a virtual address mapped to a given physical address
21 * @paddr: the physical address
23 * Returns a virtual address which the CPU can access that maps to the physical
24 * address @paddr. This should only be used where it is known that no dynamic
25 * mapping is required. In general, map_physmem should be used instead.
27 * Returns: a virtual address which maps to @paddr
30 static inline void *phys_to_virt(phys_addr_t paddr)
32 return (void *)(unsigned long)paddr;
37 * virt_to_phys() - Return the physical address that a virtual address maps to
38 * @vaddr: the virtual address
40 * Returns the physical address which the CPU-accessible virtual address @vaddr
43 * Returns: the physical address which @vaddr maps to
46 static inline phys_addr_t virt_to_phys(void *vaddr)
48 return (phys_addr_t)((unsigned long)vaddr);
53 * Flags for use with map_physmem() & unmap_physmem(). Architectures need not
54 * support all of these, in which case they will be defined as zero here &
55 * ignored. Callers that may run on multiple architectures should therefore
56 * treat them as hints rather than requirements.
59 # define MAP_NOCACHE 0 /* Produce an uncached mapping */
62 # define MAP_WRCOMBINE 0 /* Allow write-combining on the mapping */
65 # define MAP_WRBACK 0 /* Map using write-back caching */
68 # define MAP_WRTHROUGH 0 /* Map using write-through caching */
72 * map_physmem() - Return a virtual address mapped to a given physical address
73 * @paddr: the physical address
74 * @len: the length of the required mapping
75 * @flags: flags affecting the type of mapping
77 * Return a virtual address through which the CPU may access the memory at
78 * physical address @paddr. The mapping will be valid for at least @len bytes,
79 * and may be affected by flags passed to the @flags argument. This function
80 * may create new mappings, so should generally be paired with a matching call
81 * to unmap_physmem once the caller is finished with the memory in question.
83 * Returns: a virtual address suitably mapped to @paddr
86 static inline void *map_physmem(phys_addr_t paddr, unsigned long len,
89 return phys_to_virt(paddr);
94 * unmap_physmem() - Remove mappings created by a prior call to map_physmem()
95 * @vaddr: the virtual address which map_physmem() previously returned
96 * @flags: flags matching those originally passed to map_physmem()
98 * Unmap memory which was previously mapped by a call to map_physmem(). If
99 * map_physmem() dynamically created a mapping for the memory in question then
100 * unmap_physmem() will remove that mapping.
102 #ifndef unmap_physmem
103 static inline void unmap_physmem(void *vaddr, unsigned long flags)
109 * __raw_{read,write}{b,w,l,q}() access memory in native endianness.
111 * On some architectures memory mapped IO needs to be accessed differently.
112 * On the simple architectures, we just read/write the memory location
117 #define __raw_readb __raw_readb
118 static inline u8 __raw_readb(const volatile void __iomem *addr)
120 return *(const volatile u8 __force *)addr;
125 #define __raw_readw __raw_readw
126 static inline u16 __raw_readw(const volatile void __iomem *addr)
128 return *(const volatile u16 __force *)addr;
133 #define __raw_readl __raw_readl
134 static inline u32 __raw_readl(const volatile void __iomem *addr)
136 return *(const volatile u32 __force *)addr;
142 #define __raw_readq __raw_readq
143 static inline u64 __raw_readq(const volatile void __iomem *addr)
145 return *(const volatile u64 __force *)addr;
148 #endif /* CONFIG_64BIT */
151 #define __raw_writeb __raw_writeb
152 static inline void __raw_writeb(u8 value, volatile void __iomem *addr)
154 *(volatile u8 __force *)addr = value;
159 #define __raw_writew __raw_writew
160 static inline void __raw_writew(u16 value, volatile void __iomem *addr)
162 *(volatile u16 __force *)addr = value;
167 #define __raw_writel __raw_writel
168 static inline void __raw_writel(u32 value, volatile void __iomem *addr)
170 *(volatile u32 __force *)addr = value;
176 #define __raw_writeq __raw_writeq
177 static inline void __raw_writeq(u64 value, volatile void __iomem *addr)
179 *(volatile u64 __force *)addr = value;
182 #endif /* CONFIG_64BIT */
185 * {read,write}s{b,w,l,q}() repeatedly access the same memory address in
186 * native endianness in 8-, 16-, 32- or 64-bit chunks (@count times).
189 #define readsb readsb
190 static inline void readsb(const volatile void __iomem *addr, void *buffer,
197 u8 x = __raw_readb(addr);
205 #define readsw readsw
206 static inline void readsw(const volatile void __iomem *addr, void *buffer,
213 u16 x = __raw_readw(addr);
221 #define readsl readsl
222 static inline void readsl(const volatile void __iomem *addr, void *buffer,
229 u32 x = __raw_readl(addr);
238 #define readsq readsq
239 static inline void readsq(const volatile void __iomem *addr, void *buffer,
246 u64 x = __raw_readq(addr);
252 #endif /* CONFIG_64BIT */
255 #define writesb writesb
256 static inline void writesb(volatile void __iomem *addr, const void *buffer,
260 const u8 *buf = buffer;
263 __raw_writeb(*buf++, addr);
270 #define writesw writesw
271 static inline void writesw(volatile void __iomem *addr, const void *buffer,
275 const u16 *buf = buffer;
278 __raw_writew(*buf++, addr);
285 #define writesl writesl
286 static inline void writesl(volatile void __iomem *addr, const void *buffer,
290 const u32 *buf = buffer;
293 __raw_writel(*buf++, addr);
301 #define writesq writesq
302 static inline void writesq(volatile void __iomem *addr, const void *buffer,
306 const u64 *buf = buffer;
309 __raw_writeq(*buf++, addr);
314 #endif /* CONFIG_64BIT */
317 #define PCI_IOBASE ((void __iomem *)0)
321 * {in,out}s{b,w,l}{,_p}() are variants of the above that repeatedly access a
322 * single I/O port multiple times.
327 static inline void insb(unsigned long addr, void *buffer, unsigned int count)
329 readsb(PCI_IOBASE + addr, buffer, count);
335 static inline void insw(unsigned long addr, void *buffer, unsigned int count)
337 readsw(PCI_IOBASE + addr, buffer, count);
343 static inline void insl(unsigned long addr, void *buffer, unsigned int count)
345 readsl(PCI_IOBASE + addr, buffer, count);
351 static inline void outsb(unsigned long addr, const void *buffer,
354 writesb(PCI_IOBASE + addr, buffer, count);
360 static inline void outsw(unsigned long addr, const void *buffer,
363 writesw(PCI_IOBASE + addr, buffer, count);
369 static inline void outsl(unsigned long addr, const void *buffer,
372 writesl(PCI_IOBASE + addr, buffer, count);
377 #define ioread8_rep ioread8_rep
378 static inline void ioread8_rep(const volatile void __iomem *addr, void *buffer,
381 readsb(addr, buffer, count);
386 #define ioread16_rep ioread16_rep
387 static inline void ioread16_rep(const volatile void __iomem *addr,
388 void *buffer, unsigned int count)
390 readsw(addr, buffer, count);
395 #define ioread32_rep ioread32_rep
396 static inline void ioread32_rep(const volatile void __iomem *addr,
397 void *buffer, unsigned int count)
399 readsl(addr, buffer, count);
405 #define ioread64_rep ioread64_rep
406 static inline void ioread64_rep(const volatile void __iomem *addr,
407 void *buffer, unsigned int count)
409 readsq(addr, buffer, count);
412 #endif /* CONFIG_64BIT */
415 #define iowrite8_rep iowrite8_rep
416 static inline void iowrite8_rep(volatile void __iomem *addr,
420 writesb(addr, buffer, count);
424 #ifndef iowrite16_rep
425 #define iowrite16_rep iowrite16_rep
426 static inline void iowrite16_rep(volatile void __iomem *addr,
430 writesw(addr, buffer, count);
434 #ifndef iowrite32_rep
435 #define iowrite32_rep iowrite32_rep
436 static inline void iowrite32_rep(volatile void __iomem *addr,
440 writesl(addr, buffer, count);
445 #ifndef iowrite64_rep
446 #define iowrite64_rep iowrite64_rep
447 static inline void iowrite64_rep(volatile void __iomem *addr,
451 writesq(addr, buffer, count);
454 #endif /* CONFIG_64BIT */
456 #endif /* !__ASSEMBLY__ */
457 #endif /* __ASM_GENERIC_IO_H__ */