blob: c75e4d3d8833fd0f77f0e61ca1014826afdf63bc [file] [log] [blame]
Thomas Gleixner775c8a32019-06-04 10:11:37 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Keith Packard9663f2e2008-10-30 19:38:18 -07002/*
3 * Copyright © 2008 Keith Packard <keithp@keithp.com>
Keith Packard9663f2e2008-10-30 19:38:18 -07004 */
5
6#ifndef _LINUX_IO_MAPPING_H
7#define _LINUX_IO_MAPPING_H
8
9#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -050011#include <linux/bug.h>
Dan Williams2584cf82015-08-10 23:07:05 -040012#include <linux/io.h>
Mike Rapoport65fddcf2020-06-08 21:32:42 -070013#include <linux/pgtable.h>
Keith Packard9663f2e2008-10-30 19:38:18 -070014#include <asm/page.h>
Keith Packard9663f2e2008-10-30 19:38:18 -070015
16/*
17 * The io_mapping mechanism provides an abstraction for mapping
18 * individual pages from an io device to the CPU in an efficient fashion.
19 *
Mauro Carvalho Chehab7d3d3252020-03-11 12:51:17 +010020 * See Documentation/driver-api/io-mapping.rst
Keith Packard9663f2e2008-10-30 19:38:18 -070021 */
22
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080023struct io_mapping {
24 resource_size_t base;
25 unsigned long size;
26 pgprot_t prot;
Chris Wilsoncafaf142016-08-19 16:54:26 +010027 void __iomem *iomem;
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080028};
29
Chris Wilsoncafaf142016-08-19 16:54:26 +010030#ifdef CONFIG_HAVE_ATOMIC_IOMAP
31
Andy Shevchenko2b755622020-01-30 22:17:32 -080032#include <linux/pfn.h>
Chris Wilsoncafaf142016-08-19 16:54:26 +010033#include <asm/iomap.h>
Keith Packarde5beae12008-11-03 18:21:45 +010034/*
35 * For small address space machines, mapping large objects
36 * into the kernel virtual space isn't practical. Where
37 * available, use fixmap support to dynamically map pages
38 * of the object at run time.
39 */
Keith Packard9663f2e2008-10-30 19:38:18 -070040
Keith Packard9663f2e2008-10-30 19:38:18 -070041static inline struct io_mapping *
Chris Wilsoncafaf142016-08-19 16:54:26 +010042io_mapping_init_wc(struct io_mapping *iomap,
43 resource_size_t base,
44 unsigned long size)
Keith Packard9663f2e2008-10-30 19:38:18 -070045{
Venkatesh Pallipadi9e36fda02009-07-10 09:57:35 -070046 pgprot_t prot;
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080047
Venkatesh Pallipadi9e36fda02009-07-10 09:57:35 -070048 if (iomap_create_wc(base, size, &prot))
Chris Wilsoncafaf142016-08-19 16:54:26 +010049 return NULL;
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080050
51 iomap->base = base;
52 iomap->size = size;
Venkatesh Pallipadi9e36fda02009-07-10 09:57:35 -070053 iomap->prot = prot;
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080054 return iomap;
Keith Packard9663f2e2008-10-30 19:38:18 -070055}
56
57static inline void
Chris Wilsoncafaf142016-08-19 16:54:26 +010058io_mapping_fini(struct io_mapping *mapping)
Keith Packard9663f2e2008-10-30 19:38:18 -070059{
Venkatesh Pallipadi9e36fda02009-07-10 09:57:35 -070060 iomap_free(mapping->base, mapping->size);
Keith Packard9663f2e2008-10-30 19:38:18 -070061}
62
63/* Atomic map/unmap */
Francisco Jerez29bc17e2010-09-04 22:56:44 +020064static inline void __iomem *
Chris Wilsonfca3ec02010-08-04 14:34:24 +010065io_mapping_map_atomic_wc(struct io_mapping *mapping,
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070066 unsigned long offset)
Keith Packard9663f2e2008-10-30 19:38:18 -070067{
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080068 resource_size_t phys_addr;
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080069
70 BUG_ON(offset >= mapping->size);
71 phys_addr = mapping->base + offset;
Andy Shevchenko2b755622020-01-30 22:17:32 -080072 return iomap_atomic_prot_pfn(PHYS_PFN(phys_addr), mapping->prot);
Keith Packard9663f2e2008-10-30 19:38:18 -070073}
74
75static inline void
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070076io_mapping_unmap_atomic(void __iomem *vaddr)
Keith Packard9663f2e2008-10-30 19:38:18 -070077{
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070078 iounmap_atomic(vaddr);
Keith Packard9663f2e2008-10-30 19:38:18 -070079}
80
Francisco Jerez29bc17e2010-09-04 22:56:44 +020081static inline void __iomem *
Chris Wilsond8dab002016-04-28 09:56:37 +010082io_mapping_map_wc(struct io_mapping *mapping,
83 unsigned long offset,
84 unsigned long size)
Keith Packard9663f2e2008-10-30 19:38:18 -070085{
Pallipadi, Venkatesh5ce04e32009-03-01 08:53:27 -080086 resource_size_t phys_addr;
87
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080088 BUG_ON(offset >= mapping->size);
Pallipadi, Venkatesh5ce04e32009-03-01 08:53:27 -080089 phys_addr = mapping->base + offset;
90
Chris Wilsond8dab002016-04-28 09:56:37 +010091 return ioremap_wc(phys_addr, size);
Keith Packard9663f2e2008-10-30 19:38:18 -070092}
93
94static inline void
Francisco Jerez29bc17e2010-09-04 22:56:44 +020095io_mapping_unmap(void __iomem *vaddr)
Keith Packard9663f2e2008-10-30 19:38:18 -070096{
97 iounmap(vaddr);
98}
Keith Packarde5beae12008-11-03 18:21:45 +010099
100#else
101
Daniel Vetter24dd85f2011-09-28 11:57:23 +0200102#include <linux/uaccess.h>
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -0800103
Keith Packarde5beae12008-11-03 18:21:45 +0100104/* Create the io_mapping object*/
105static inline struct io_mapping *
Chris Wilsoncafaf142016-08-19 16:54:26 +0100106io_mapping_init_wc(struct io_mapping *iomap,
107 resource_size_t base,
108 unsigned long size)
Keith Packarde5beae12008-11-03 18:21:45 +0100109{
Michael J. Ruhle0b3e0b2020-07-23 21:15:46 -0700110 iomap->iomem = ioremap_wc(base, size);
111 if (!iomap->iomem)
112 return NULL;
113
Chris Wilsoncafaf142016-08-19 16:54:26 +0100114 iomap->base = base;
115 iomap->size = size;
Daniel Vetter35124382016-08-23 22:15:02 +0200116#if defined(pgprot_noncached_wc) /* archs can't agree on a name ... */
117 iomap->prot = pgprot_noncached_wc(PAGE_KERNEL);
118#elif defined(pgprot_writecombine)
Chris Wilsonbcaaa0c2016-08-23 16:50:24 +0100119 iomap->prot = pgprot_writecombine(PAGE_KERNEL);
Daniel Vetter35124382016-08-23 22:15:02 +0200120#else
121 iomap->prot = pgprot_noncached(PAGE_KERNEL);
122#endif
Chris Wilsoncafaf142016-08-19 16:54:26 +0100123
124 return iomap;
Keith Packarde5beae12008-11-03 18:21:45 +0100125}
126
127static inline void
Chris Wilsoncafaf142016-08-19 16:54:26 +0100128io_mapping_fini(struct io_mapping *mapping)
Keith Packarde5beae12008-11-03 18:21:45 +0100129{
Chris Wilsoncafaf142016-08-19 16:54:26 +0100130 iounmap(mapping->iomem);
131}
132
133/* Non-atomic map/unmap */
134static inline void __iomem *
135io_mapping_map_wc(struct io_mapping *mapping,
136 unsigned long offset,
137 unsigned long size)
138{
139 return mapping->iomem + offset;
140}
141
142static inline void
143io_mapping_unmap(void __iomem *vaddr)
144{
Keith Packarde5beae12008-11-03 18:21:45 +0100145}
146
147/* Atomic map/unmap */
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200148static inline void __iomem *
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100149io_mapping_map_atomic_wc(struct io_mapping *mapping,
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700150 unsigned long offset)
Keith Packarde5beae12008-11-03 18:21:45 +0100151{
David Hildenbrand2cb7c9c2015-05-11 17:52:09 +0200152 preempt_disable();
Daniel Vetter24dd85f2011-09-28 11:57:23 +0200153 pagefault_disable();
Chris Wilsoncafaf142016-08-19 16:54:26 +0100154 return io_mapping_map_wc(mapping, offset, PAGE_SIZE);
Keith Packarde5beae12008-11-03 18:21:45 +0100155}
156
157static inline void
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700158io_mapping_unmap_atomic(void __iomem *vaddr)
Keith Packarde5beae12008-11-03 18:21:45 +0100159{
Chris Wilsoncafaf142016-08-19 16:54:26 +0100160 io_mapping_unmap(vaddr);
Daniel Vetter24dd85f2011-09-28 11:57:23 +0200161 pagefault_enable();
David Hildenbrand2cb7c9c2015-05-11 17:52:09 +0200162 preempt_enable();
Keith Packarde5beae12008-11-03 18:21:45 +0100163}
164
Chris Wilsoncafaf142016-08-19 16:54:26 +0100165#endif /* HAVE_ATOMIC_IOMAP */
166
167static inline struct io_mapping *
168io_mapping_create_wc(resource_size_t base,
169 unsigned long size)
Keith Packarde5beae12008-11-03 18:21:45 +0100170{
Chris Wilsoncafaf142016-08-19 16:54:26 +0100171 struct io_mapping *iomap;
172
173 iomap = kmalloc(sizeof(*iomap), GFP_KERNEL);
174 if (!iomap)
175 return NULL;
176
177 if (!io_mapping_init_wc(iomap, base, size)) {
178 kfree(iomap);
179 return NULL;
180 }
181
182 return iomap;
Keith Packarde5beae12008-11-03 18:21:45 +0100183}
184
185static inline void
Chris Wilsoncafaf142016-08-19 16:54:26 +0100186io_mapping_free(struct io_mapping *iomap)
Keith Packarde5beae12008-11-03 18:21:45 +0100187{
Chris Wilsoncafaf142016-08-19 16:54:26 +0100188 io_mapping_fini(iomap);
189 kfree(iomap);
Keith Packarde5beae12008-11-03 18:21:45 +0100190}
191
Keith Packard9663f2e2008-10-30 19:38:18 -0700192#endif /* _LINUX_IO_MAPPING_H */