asm-generic/io.h: Add a non-posted variant of ioremap()
authorHector Martin <marcan@marcan.st>
Thu, 11 Feb 2021 12:35:46 +0000 (21:35 +0900)
committerHector Martin <marcan@marcan.st>
Thu, 8 Apr 2021 11:18:38 +0000 (20:18 +0900)
ARM64 currently defaults to posted MMIO (nGnRE), but some devices
require the use of non-posted MMIO (nGnRnE). Introduce a new ioremap()
variant to handle this case. ioremap_np() returns NULL on arches that
do not implement this variant.

sparc64 is the only architecture that needs to be touched directly,
because it includes neither of the generic io.h or iomap.h headers.

This adds the IORESOURCE_MEM_NONPOSTED flag, which maps to this
variant and marks a given resource as requiring non-posted mappings.
This is implemented in the resource system because it is a SoC-level
requirement, so existing drivers do not need special-case code to pick
this ioremap variant.

Then this is implemented in devres by introducing devm_ioremap_np(),
and making devm_ioremap_resource() automatically select this variant
when the resource has the IORESOURCE_MEM_NONPOSTED flag set.

Acked-by: Marc Zyngier <maz@kernel.org>
Signed-off-by: Hector Martin <marcan@marcan.st>
Documentation/driver-api/driver-model/devres.rst
arch/sparc/include/asm/io_64.h
include/asm-generic/io.h
include/asm-generic/iomap.h
include/linux/io.h
include/linux/ioport.h
lib/devres.c

index cd8b6e657b94f1da63e88237b1a6690a9fbbb272..2f45877a539d2694021770781ee137de9d882ab7 100644 (file)
@@ -309,6 +309,7 @@ IOMAP
   devm_ioremap()
   devm_ioremap_uc()
   devm_ioremap_wc()
+  devm_ioremap_np()
   devm_ioremap_resource() : checks resource, requests memory region, ioremaps
   devm_ioremap_resource_wc()
   devm_platform_ioremap_resource() : calls devm_ioremap_resource() for platform device
index 9bb27e5c22f159c97fed8410d59dd92c91084e0e..9fbfc9574432aadace3149cd6fafea8eeaedd5a8 100644 (file)
@@ -409,6 +409,10 @@ static inline void __iomem *ioremap(unsigned long offset, unsigned long size)
 #define ioremap_uc(X,Y)                        ioremap((X),(Y))
 #define ioremap_wc(X,Y)                        ioremap((X),(Y))
 #define ioremap_wt(X,Y)                        ioremap((X),(Y))
+static inline void __iomem *ioremap_np(unsigned long offset, unsigned long size)
+{
+       return NULL;
+}
 
 static inline void iounmap(volatile void __iomem *addr)
 {
index c6af40ce03befec1740fd9ab705648e1eba2bf81..082e0c96db6e1b12788f3521d7c84acc04f3bffc 100644 (file)
@@ -942,7 +942,9 @@ static inline void *phys_to_virt(unsigned long address)
  *
  * ioremap_wc() and ioremap_wt() can provide more relaxed caching attributes
  * for specific drivers if the architecture choses to implement them.  If they
- * are not implemented we fall back to plain ioremap.
+ * are not implemented we fall back to plain ioremap. Conversely, ioremap_np()
+ * can provide stricter non-posted write semantics if the architecture
+ * implements them.
  */
 #ifndef CONFIG_MMU
 #ifndef ioremap
@@ -993,6 +995,24 @@ static inline void __iomem *ioremap_uc(phys_addr_t offset, size_t size)
 {
        return NULL;
 }
+
+/*
+ * ioremap_np needs an explicit architecture implementation, as it
+ * requests stronger semantics than regular ioremap(). Portable drivers
+ * should instead use one of the higher-level abstractions, like
+ * devm_ioremap_resource(), to choose the correct variant for any given
+ * device and bus. Portable drivers with a good reason to want non-posted
+ * write semantics should always provide an ioremap() fallback in case
+ * ioremap_np() is not available.
+ */
+#ifndef ioremap_np
+#define ioremap_np ioremap_np
+static inline void __iomem *ioremap_np(phys_addr_t offset, size_t size)
+{
+       return NULL;
+}
+#endif
+
 #endif
 
 #ifdef CONFIG_HAS_IOPORT_MAP
index 6492246649693bdfce927be48bdf700890588121..9b3eb6d86200180d1fe6ff40687c807bb6107bc4 100644 (file)
@@ -101,6 +101,15 @@ extern void ioport_unmap(void __iomem *);
 #define ioremap_wt ioremap
 #endif
 
+#ifndef ARCH_HAS_IOREMAP_NP
+/* See the comment in asm-generic/io.h about ioremap_np(). */
+#define ioremap_np ioremap_np
+static inline void __iomem *ioremap_np(phys_addr_t offset, size_t size)
+{
+       return NULL;
+}
+#endif
+
 #ifdef CONFIG_PCI
 /* Destroy a virtual mapping cookie for a PCI BAR (memory or IO) */
 struct pci_dev;
index 8394c56babc26fa1a4d99cd75126cf6ca2a26b42..d718354ed3e16d33e1d7cac56e7bbac49f07e438 100644 (file)
@@ -68,6 +68,8 @@ void __iomem *devm_ioremap_uc(struct device *dev, resource_size_t offset,
                                   resource_size_t size);
 void __iomem *devm_ioremap_wc(struct device *dev, resource_size_t offset,
                                   resource_size_t size);
+void __iomem *devm_ioremap_np(struct device *dev, resource_size_t offset,
+                                  resource_size_t size);
 void devm_iounmap(struct device *dev, void __iomem *addr);
 int check_signature(const volatile void __iomem *io_addr,
                        const unsigned char *signature, int length);
index 55de385c839cf5648bffc689b2b1aa4253c74f08..1de6c2e40c3248a0cee3e239b4178012b9b21a7b 100644 (file)
@@ -108,6 +108,7 @@ struct resource {
 #define IORESOURCE_MEM_32BIT           (3<<3)
 #define IORESOURCE_MEM_SHADOWABLE      (1<<5)  /* dup: IORESOURCE_SHADOWABLE */
 #define IORESOURCE_MEM_EXPANSIONROM    (1<<6)
+#define IORESOURCE_MEM_NONPOSTED       (1<<7)
 
 /* PnP I/O specific bits (IORESOURCE_BITS) */
 #define IORESOURCE_IO_16BIT_ADDR       (1<<0)
index 2a4ff5d6428860b79ed04001acb87137b6dbc651..4679dbb1bf5f8a82ce3773ad14a5f0e0343aaf8b 100644 (file)
@@ -10,6 +10,7 @@ enum devm_ioremap_type {
        DEVM_IOREMAP = 0,
        DEVM_IOREMAP_UC,
        DEVM_IOREMAP_WC,
+       DEVM_IOREMAP_NP,
 };
 
 void devm_ioremap_release(struct device *dev, void *res)
@@ -42,6 +43,9 @@ static void __iomem *__devm_ioremap(struct device *dev, resource_size_t offset,
        case DEVM_IOREMAP_WC:
                addr = ioremap_wc(offset, size);
                break;
+       case DEVM_IOREMAP_NP:
+               addr = ioremap_np(offset, size);
+               break;
        }
 
        if (addr) {
@@ -98,6 +102,21 @@ void __iomem *devm_ioremap_wc(struct device *dev, resource_size_t offset,
 }
 EXPORT_SYMBOL(devm_ioremap_wc);
 
+/**
+ * devm_ioremap_np - Managed ioremap_np()
+ * @dev: Generic device to remap IO address for
+ * @offset: Resource address to map
+ * @size: Size of map
+ *
+ * Managed ioremap_np().  Map is automatically unmapped on driver detach.
+ */
+void __iomem *devm_ioremap_np(struct device *dev, resource_size_t offset,
+                             resource_size_t size)
+{
+       return __devm_ioremap(dev, offset, size, DEVM_IOREMAP_NP);
+}
+EXPORT_SYMBOL(devm_ioremap_np);
+
 /**
  * devm_iounmap - Managed iounmap()
  * @dev: Generic device to unmap for
@@ -128,6 +147,9 @@ __devm_ioremap_resource(struct device *dev, const struct resource *res,
                return IOMEM_ERR_PTR(-EINVAL);
        }
 
+       if (type == DEVM_IOREMAP && res->flags & IORESOURCE_MEM_NONPOSTED)
+               type = DEVM_IOREMAP_NP;
+
        size = resource_size(res);
 
        if (res->name)