1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * Copyright 2006 PathScale, Inc.  All Rights Reserved.
4 */
5
6#ifndef _LINUX_IO_H
7#define _LINUX_IO_H
8
9#include <linux/types.h>
10#include <linux/init.h>
11#include <linux/bug.h>
12#include <linux/err.h>
13#include <asm/io.h>
14#include <asm/page.h>
15
16struct device;
17struct resource;
18
19__visible void __iowrite32_copy(void __iomem *to, const void *from, size_t count);
20void __ioread32_copy(void *to, const void __iomem *from, size_t count);
21void __iowrite64_copy(void __iomem *to, const void *from, size_t count);
22
23#ifdef CONFIG_MMU
24int ioremap_page_range(unsigned long addr, unsigned long end,
25		       phys_addr_t phys_addr, pgprot_t prot);
26int vmap_page_range(unsigned long addr, unsigned long end,
27		    phys_addr_t phys_addr, pgprot_t prot);
28#else
29static inline int ioremap_page_range(unsigned long addr, unsigned long end,
30				     phys_addr_t phys_addr, pgprot_t prot)
31{
32	return 0;
33}
34static inline int vmap_page_range(unsigned long addr, unsigned long end,
35				  phys_addr_t phys_addr, pgprot_t prot)
36{
37	return 0;
38}
39#endif
40
41/*
42 * Managed iomap interface
43 */
44#ifdef CONFIG_HAS_IOPORT_MAP
45void __iomem * devm_ioport_map(struct device *dev, unsigned long port,
46			       unsigned int nr);
47void devm_ioport_unmap(struct device *dev, void __iomem *addr);
48#else
49static inline void __iomem *devm_ioport_map(struct device *dev,
50					     unsigned long port,
51					     unsigned int nr)
52{
53	return NULL;
54}
55
56static inline void devm_ioport_unmap(struct device *dev, void __iomem *addr)
57{
58}
59#endif
60
61#define IOMEM_ERR_PTR(err) (__force void __iomem *)ERR_PTR(err)
62
63void __iomem *devm_ioremap(struct device *dev, resource_size_t offset,
64			   resource_size_t size);
65void __iomem *devm_ioremap_uc(struct device *dev, resource_size_t offset,
66				   resource_size_t size);
67void __iomem *devm_ioremap_wc(struct device *dev, resource_size_t offset,
68				   resource_size_t size);
69void devm_iounmap(struct device *dev, void __iomem *addr);
70int check_signature(const volatile void __iomem *io_addr,
71			const unsigned char *signature, int length);
72void devm_ioremap_release(struct device *dev, void *res);
73
74void *devm_memremap(struct device *dev, resource_size_t offset,
75		size_t size, unsigned long flags);
76void devm_memunmap(struct device *dev, void *addr);
77
78/* architectures can override this */
79pgprot_t __init early_memremap_pgprot_adjust(resource_size_t phys_addr,
80					unsigned long size, pgprot_t prot);
81
82
83#ifdef CONFIG_PCI
84/*
85 * The PCI specifications (Rev 3.0, 3.2.5 "Transaction Ordering and
86 * Posting") mandate non-posted configuration transactions. This default
87 * implementation attempts to use the ioremap_np() API to provide this
88 * on arches that support it, and falls back to ioremap() on those that
89 * don't. Overriding this function is deprecated; arches that properly
90 * support non-posted accesses should implement ioremap_np() instead, which
91 * this default implementation can then use to return mappings compliant with
92 * the PCI specification.
93 */
94#ifndef pci_remap_cfgspace
95#define pci_remap_cfgspace pci_remap_cfgspace
96static inline void __iomem *pci_remap_cfgspace(phys_addr_t offset,
97					       size_t size)
98{
99	return ioremap_np(offset, size) ?: ioremap(offset, size);
100}
101#endif
102#endif
103
104/*
105 * Some systems do not have legacy ISA devices.
106 * /dev/port is not a valid interface on these systems.
107 * So for those archs, <asm/io.h> should define the following symbol.
108 */
109#ifndef arch_has_dev_port
110#define arch_has_dev_port()     (1)
111#endif
112
113/*
114 * Some systems (x86 without PAT) have a somewhat reliable way to mark a
115 * physical address range such that uncached mappings will actually
116 * end up write-combining.  This facility should be used in conjunction
117 * with pgprot_writecombine, ioremap-wc, or set_memory_wc, since it has
118 * no effect if the per-page mechanisms are functional.
119 * (On x86 without PAT, these functions manipulate MTRRs.)
120 *
121 * arch_phys_del_wc(0) or arch_phys_del_wc(any error code) is guaranteed
122 * to have no effect.
123 */
124#ifndef arch_phys_wc_add
125static inline int __must_check arch_phys_wc_add(unsigned long base,
126						unsigned long size)
127{
128	return 0;  /* It worked (i.e. did nothing). */
129}
130
131static inline void arch_phys_wc_del(int handle)
132{
133}
134
135#define arch_phys_wc_add arch_phys_wc_add
136#ifndef arch_phys_wc_index
137static inline int arch_phys_wc_index(int handle)
138{
139	return -1;
140}
141#define arch_phys_wc_index arch_phys_wc_index
142#endif
143#endif
144
145int devm_arch_phys_wc_add(struct device *dev, unsigned long base, unsigned long size);
146
147enum {
148	/* See memremap() kernel-doc for usage description... */
149	MEMREMAP_WB = 1 << 0,
150	MEMREMAP_WT = 1 << 1,
151	MEMREMAP_WC = 1 << 2,
152	MEMREMAP_ENC = 1 << 3,
153	MEMREMAP_DEC = 1 << 4,
154};
155
156void *memremap(resource_size_t offset, size_t size, unsigned long flags);
157void memunmap(void *addr);
158
159/*
160 * On x86 PAT systems we have memory tracking that keeps track of
161 * the allowed mappings on memory ranges. This tracking works for
162 * all the in-kernel mapping APIs (ioremap*), but where the user
163 * wishes to map a range from a physical device into user memory
164 * the tracking won't be updated. This API is to be used by
165 * drivers which remap physical device pages into userspace,
166 * and wants to make sure they are mapped WC and not UC.
167 */
168#ifndef arch_io_reserve_memtype_wc
169static inline int arch_io_reserve_memtype_wc(resource_size_t base,
170					     resource_size_t size)
171{
172	return 0;
173}
174
175static inline void arch_io_free_memtype_wc(resource_size_t base,
176					   resource_size_t size)
177{
178}
179#endif
180
181int devm_arch_io_reserve_memtype_wc(struct device *dev, resource_size_t start,
182				    resource_size_t size);
183
184#endif /* _LINUX_IO_H */
185