[PATCH] asm-generic: provide phys_to_virt() and virt_to_phys()

Antony Pavlov antonynpavlov at gmail.com
Tue Jul 26 03:24:12 PDT 2022


The arm, riscv, sandbox and x86 architectures use
just the same phys_to_virt()/virt_to_phys() implementation.
Only the mips architecture has its own special implementation.

So we can move phys_to_virt() and virt_to_phys()
generic implementation to include/asm-generic/io.h.

Use override functions way introduced in the 9216efafc52ff99e
("asm-generic/io.h: Reconcile I/O accessor overrides")
linux kernel commit.

Signed-off-by: Antony Pavlov <antonynpavlov at gmail.com>
---
 arch/arm/include/asm/io.h     | 10 ----------
 arch/mips/include/asm/io.h    |  2 ++
 arch/riscv/include/asm/io.h   | 10 ----------
 arch/sandbox/include/asm/io.h | 10 ----------
 arch/x86/include/asm/io.h     | 10 ----------
 include/asm-generic/io.h      | 20 ++++++++++++++++++++
 6 files changed, 22 insertions(+), 40 deletions(-)

diff --git a/arch/arm/include/asm/io.h b/arch/arm/include/asm/io.h
index 1c0f522d1d4..486b1429502 100644
--- a/arch/arm/include/asm/io.h
+++ b/arch/arm/include/asm/io.h
@@ -18,14 +18,4 @@ extern void memcpy_fromio(void *, const volatile void __iomem *, size_t);
 extern void memcpy_toio(volatile void __iomem *, const void *, size_t);
 extern void memset_io(volatile void __iomem *, int, size_t);
 
-static inline void *phys_to_virt(unsigned long phys)
-{
-	return (void *)phys;
-}
-
-static inline unsigned long virt_to_phys(volatile void *mem)
-{
-	return (unsigned long)mem;
-}
-
 #endif	/* __ASM_ARM_IO_H */
diff --git a/arch/mips/include/asm/io.h b/arch/mips/include/asm/io.h
index 0d228aa0f51..a3acbd7017c 100644
--- a/arch/mips/include/asm/io.h
+++ b/arch/mips/include/asm/io.h
@@ -27,6 +27,7 @@ void dma_inv_range(unsigned long, unsigned long);
  *     The returned physical address is the physical (CPU) mapping for
  *     the memory address given.
  */
+#define virt_to_phys virt_to_phys
 static inline unsigned long virt_to_phys(const void *address)
 {
 	return (unsigned long)CPHYSADDR(address);
@@ -39,6 +40,7 @@ static inline unsigned long virt_to_phys(const void *address)
  *     The returned virtual address is a current CPU mapping for
  *     the memory address given.
  */
+#define phys_to_virt phys_to_virt
 static inline void *phys_to_virt(unsigned long address)
 {
 	if (IS_ENABLED(CONFIG_MMU)) {
diff --git a/arch/riscv/include/asm/io.h b/arch/riscv/include/asm/io.h
index 978f6443407..76d66c0a3b8 100644
--- a/arch/riscv/include/asm/io.h
+++ b/arch/riscv/include/asm/io.h
@@ -7,14 +7,4 @@
 
 #include <asm-generic/io.h>
 
-static inline void *phys_to_virt(unsigned long phys)
-{
-	return (void *)phys;
-}
-
-static inline unsigned long virt_to_phys(volatile void *mem)
-{
-	return (unsigned long)mem;
-}
-
 #endif /* __ASM_RISCV_IO_H */
diff --git a/arch/sandbox/include/asm/io.h b/arch/sandbox/include/asm/io.h
index 71cf50cd9a0..eec279b8883 100644
--- a/arch/sandbox/include/asm/io.h
+++ b/arch/sandbox/include/asm/io.h
@@ -11,14 +11,4 @@ extern unsigned char __pci_iobase[IO_SPACE_LIMIT];
 
 #include <asm-generic/io.h>
 
-static inline void *phys_to_virt(unsigned long phys)
-{
-       return (void *)phys;
-}
-
-static inline unsigned long virt_to_phys(volatile void *mem)
-{
-       return (unsigned long)mem;
-}
-
 #endif /* __ASM_SANDBOX_IO_H */
diff --git a/arch/x86/include/asm/io.h b/arch/x86/include/asm/io.h
index 41c5cb3c14f..d4b5c269190 100644
--- a/arch/x86/include/asm/io.h
+++ b/arch/x86/include/asm/io.h
@@ -84,16 +84,6 @@ static inline void io_delay(void)
 	inb(0x80);
 }
 
-static inline void *phys_to_virt(unsigned long phys)
-{
-	return (void *)phys;
-}
-
-static inline unsigned long virt_to_phys(volatile void *mem)
-{
-	return (unsigned long)mem;
-}
-
 #include <asm-generic/io.h>
 
 #endif	/* __ASM_X86_IO_H */
diff --git a/include/asm-generic/io.h b/include/asm-generic/io.h
index 47f8c3ec1be..e41b4df4036 100644
--- a/include/asm-generic/io.h
+++ b/include/asm-generic/io.h
@@ -421,6 +421,26 @@ static inline void iowrite64be(u64 value, volatile void __iomem *addr)
 #endif
 #endif /* CONFIG_64BIT */
 
+/*
+ * Change virtual addresses to physical addresses and vv.
+ * These are pretty trivial
+ */
+#ifndef virt_to_phys
+#define virt_to_phys virt_to_phys
+static inline unsigned long virt_to_phys(volatile void *mem)
+{
+	return (unsigned long)mem;
+}
+#endif
+
+#ifndef phys_to_virt
+#define phys_to_virt phys_to_virt
+static inline void *phys_to_virt(unsigned long phys)
+{
+	return (void *)phys;
+}
+#endif
+
 #ifndef IOMEM
 #define IOMEM(addr)	((void __force __iomem *)(addr))
 #endif
-- 
2.36.1




More information about the barebox mailing list