Skip to content

Commit 7c566bb

Browse files
committed
asm-generic/io.h: Add a non-posted variant of ioremap()
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>
1 parent 86332e9 commit 7c566bb

7 files changed

Lines changed: 60 additions & 1 deletion

File tree

‎Documentation/driver-api/driver-model/devres.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ IOMAP
309309
devm_ioremap()
310310
devm_ioremap_uc()
311311
devm_ioremap_wc()
312+
devm_ioremap_np()
312313
devm_ioremap_resource() : checks resource, requests memory region, ioremaps
313314
devm_ioremap_resource_wc()
314315
devm_platform_ioremap_resource() : calls devm_ioremap_resource() for platform device

‎arch/sparc/include/asm/io_64.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,10 @@ static inline void __iomem *ioremap(unsigned long offset, unsigned long size)
409409
#define ioremap_uc(X,Y) ioremap((X),(Y))
410410
#define ioremap_wc(X,Y) ioremap((X),(Y))
411411
#define ioremap_wt(X,Y) ioremap((X),(Y))
412+
static inline void __iomem *ioremap_np(unsigned long offset, unsigned long size)
413+
{
414+
return NULL;
415+
}
412416

413417
static inline void iounmap(volatile void __iomem *addr)
414418
{

‎include/asm-generic/io.h‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,9 @@ static inline void *phys_to_virt(unsigned long address)
942942
*
943943
* ioremap_wc() and ioremap_wt() can provide more relaxed caching attributes
944944
* for specific drivers if the architecture choses to implement them. If they
945-
* are not implemented we fall back to plain ioremap.
945+
* are not implemented we fall back to plain ioremap. Conversely, ioremap_np()
946+
* can provide stricter non-posted write semantics if the architecture
947+
* implements them.
946948
*/
947949
#ifndef CONFIG_MMU
948950
#ifndef ioremap
@@ -993,6 +995,24 @@ static inline void __iomem *ioremap_uc(phys_addr_t offset, size_t size)
993995
{
994996
return NULL;
995997
}
998+
999+
/*
1000+
* ioremap_np needs an explicit architecture implementation, as it
1001+
* requests stronger semantics than regular ioremap(). Portable drivers
1002+
* should instead use one of the higher-level abstractions, like
1003+
* devm_ioremap_resource(), to choose the correct variant for any given
1004+
* device and bus. Portable drivers with a good reason to want non-posted
1005+
* write semantics should always provide an ioremap() fallback in case
1006+
* ioremap_np() is not available.
1007+
*/
1008+
#ifndef ioremap_np
1009+
#define ioremap_np ioremap_np
1010+
static inline void __iomem *ioremap_np(phys_addr_t offset, size_t size)
1011+
{
1012+
return NULL;
1013+
}
1014+
#endif
1015+
9961016
#endif
9971017

9981018
#ifdef CONFIG_HAS_IOPORT_MAP

‎include/asm-generic/iomap.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ extern void ioport_unmap(void __iomem *);
101101
#define ioremap_wt ioremap
102102
#endif
103103

104+
#ifndef ARCH_HAS_IOREMAP_NP
105+
/* See the comment in asm-generic/io.h about ioremap_np(). */
106+
#define ioremap_np ioremap_np
107+
static inline void __iomem *ioremap_np(phys_addr_t offset, size_t size)
108+
{
109+
return NULL;
110+
}
111+
#endif
112+
104113
#ifdef CONFIG_PCI
105114
/* Destroy a virtual mapping cookie for a PCI BAR (memory or IO) */
106115
struct pci_dev;

‎include/linux/io.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ void __iomem *devm_ioremap_uc(struct device *dev, resource_size_t offset,
6868
resource_size_t size);
6969
void __iomem *devm_ioremap_wc(struct device *dev, resource_size_t offset,
7070
resource_size_t size);
71+
void __iomem *devm_ioremap_np(struct device *dev, resource_size_t offset,
72+
resource_size_t size);
7173
void devm_iounmap(struct device *dev, void __iomem *addr);
7274
int check_signature(const volatile void __iomem *io_addr,
7375
const unsigned char *signature, int length);

‎include/linux/ioport.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ struct resource {
108108
#define IORESOURCE_MEM_32BIT (3<<3)
109109
#define IORESOURCE_MEM_SHADOWABLE (1<<5) /* dup: IORESOURCE_SHADOWABLE */
110110
#define IORESOURCE_MEM_EXPANSIONROM (1<<6)
111+
#define IORESOURCE_MEM_NONPOSTED (1<<7)
111112

112113
/* PnP I/O specific bits (IORESOURCE_BITS) */
113114
#define IORESOURCE_IO_16BIT_ADDR (1<<0)

‎lib/devres.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ enum devm_ioremap_type {
1010
DEVM_IOREMAP = 0,
1111
DEVM_IOREMAP_UC,
1212
DEVM_IOREMAP_WC,
13+
DEVM_IOREMAP_NP,
1314
};
1415

1516
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,
4243
case DEVM_IOREMAP_WC:
4344
addr = ioremap_wc(offset, size);
4445
break;
46+
case DEVM_IOREMAP_NP:
47+
addr = ioremap_np(offset, size);
48+
break;
4549
}
4650

4751
if (addr) {
@@ -98,6 +102,21 @@ void __iomem *devm_ioremap_wc(struct device *dev, resource_size_t offset,
98102
}
99103
EXPORT_SYMBOL(devm_ioremap_wc);
100104

105+
/**
106+
* devm_ioremap_np - Managed ioremap_np()
107+
* @dev: Generic device to remap IO address for
108+
* @offset: Resource address to map
109+
* @size: Size of map
110+
*
111+
* Managed ioremap_np(). Map is automatically unmapped on driver detach.
112+
*/
113+
void __iomem *devm_ioremap_np(struct device *dev, resource_size_t offset,
114+
resource_size_t size)
115+
{
116+
return __devm_ioremap(dev, offset, size, DEVM_IOREMAP_NP);
117+
}
118+
EXPORT_SYMBOL(devm_ioremap_np);
119+
101120
/**
102121
* devm_iounmap - Managed iounmap()
103122
* @dev: Generic device to unmap for
@@ -128,6 +147,9 @@ __devm_ioremap_resource(struct device *dev, const struct resource *res,
128147
return IOMEM_ERR_PTR(-EINVAL);
129148
}
130149

150+
if (type == DEVM_IOREMAP && res->flags & IORESOURCE_MEM_NONPOSTED)
151+
type = DEVM_IOREMAP_NP;
152+
131153
size = resource_size(res);
132154

133155
if (res->name)

0 commit comments

Comments
 (0)