Skip to contentSkip to conversation

asm-generic: provide phys_to_virt() and virt_to_phys()

PATCH Applied Antony Pavlov 2 participants 2 messages Jul 26, 2022 – Aug 8, 2022

Subsystems (5)
Arm (32-bit) x86 MIPS RISC-V +1 more subsystems
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 asm-generic: provide phys_to_virt() and virt_to_phys()

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.

---
 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(-)
arch/arm/include/asm/io.h +0 -10
index 1c0f522d1d4..486b1429502 100644
@@ -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 */
arch/mips/include/asm/io.h +2 -0
index 0d228aa0f51..a3acbd7017c 100644
@@ -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)) {
arch/riscv/include/asm/io.h +0 -10
index 978f6443407..76d66c0a3b8 100644
@@ -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 */
arch/sandbox/include/asm/io.h +0 -10
index 71cf50cd9a0..eec279b8883 100644
@@ -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 */
arch/x86/include/asm/io.h +0 -10
index 41c5cb3c14f..d4b5c269190 100644
@@ -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 */
include/asm-generic/io.h +20 -0
index 47f8c3ec1be..e41b4df4036 100644
@@ -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
Signature · 1 line
2.36.1
Applied
#1 lore raw
Quoted 12 lines · Antony Pavlov

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.

Quoted 8 lines
---
 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(-)

Applied, thanks

Sascha

Quoted 134 lines
arch/arm/include/asm/io.h +0 -10
index 1c0f522d1d4..486b1429502 100644
@@ -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 */
arch/mips/include/asm/io.h +2 -0
index 0d228aa0f51..a3acbd7017c 100644
@@ -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)) {
arch/riscv/include/asm/io.h +0 -10
index 978f6443407..76d66c0a3b8 100644
@@ -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 */
arch/sandbox/include/asm/io.h +0 -10
index 71cf50cd9a0..eec279b8883 100644
@@ -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 */
arch/x86/include/asm/io.h +0 -10
index 41c5cb3c14f..d4b5c269190 100644
@@ -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 */
include/asm-generic/io.h +20 -0
index 47f8c3ec1be..e41b4df4036 100644
@@ -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
Signature · 1 line
2.36.1
Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |