blob: 6e125e9b4187994f6a98481a70a4fe6b3f2c4298 [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>
Keith Packard9663f2e2008-10-30 19:38:18 -070013#include <asm/page.h>
Keith Packard9663f2e2008-10-30 19:38:18 -070014
15/*
16 * The io_mapping mechanism provides an abstraction for mapping
17 * individual pages from an io device to the CPU in an efficient fashion.
18 *
Paul Bolle395cf962011-08-15 02:02:26 +020019 * See Documentation/io-mapping.txt
Keith Packard9663f2e2008-10-30 19:38:18 -070020 */
21
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080022struct io_mapping {
23 resource_size_t base;
24 unsigned long size;
25 pgprot_t prot;
Chris Wilsoncafaf142016-08-19 16:54:26 +010026 void __iomem *iomem;
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080027};
28
Chris Wilsoncafaf142016-08-19 16:54:26 +010029#ifdef CONFIG_HAVE_ATOMIC_IOMAP
30
31#include <asm/iomap.h>
Keith Packarde5beae12008-11-03 18:21:45 +010032/*
33 * For small address space machines, mapping large objects
34 * into the kernel virtual space isn't practical. Where
35 * available, use fixmap support to dynamically map pages
36 * of the object at run time.
37 */
Keith Packard9663f2e2008-10-30 19:38:18 -070038
Keith Packard9663f2e2008-10-30 19:38:18 -070039static inline struct io_mapping *
Chris Wilsoncafaf142016-08-19 16:54:26 +010040io_mapping_init_wc(struct io_mapping *iomap,
41 resource_size_t base,
42 unsigned long size)
Keith Packard9663f2e2008-10-30 19:38:18 -070043{
Venkatesh Pallipadi9e36fda02009-07-10 09:57:35 -070044 pgprot_t prot;
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080045
Venkatesh Pallipadi9e36fda02009-07-10 09:57:35 -070046 if (iomap_create_wc(base, size, &prot))
Chris Wilsoncafaf142016-08-19 16:54:26 +010047 return NULL;
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080048
49 iomap->base = base;
50 iomap->size = size;
Venkatesh Pallipadi9e36fda02009-07-10 09:57:35 -070051 iomap->prot = prot;
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080052 return iomap;
Keith Packard9663f2e2008-10-30 19:38:18 -070053}
54
55static inline void
Chris Wilsoncafaf142016-08-19 16:54:26 +010056io_mapping_fini(struct io_mapping *mapping)
Keith Packard9663f2e2008-10-30 19:38:18 -070057{
Venkatesh Pallipadi9e36fda02009-07-10 09:57:35 -070058 iomap_free(mapping->base, mapping->size);
Keith Packard9663f2e2008-10-30 19:38:18 -070059}
60
61/* Atomic map/unmap */
Francisco Jerez29bc17e2010-09-04 22:56:44 +020062static inline void __iomem *
Chris Wilsonfca3ec02010-08-04 14:34:24 +010063io_mapping_map_atomic_wc(struct io_mapping *mapping,
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070064 unsigned long offset)
Keith Packard9663f2e2008-10-30 19:38:18 -070065{
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080066 resource_size_t phys_addr;
67 unsigned long pfn;
68
69 BUG_ON(offset >= mapping->size);
70 phys_addr = mapping->base + offset;
71 pfn = (unsigned long) (phys_addr >> PAGE_SHIFT);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070072 return iomap_atomic_prot_pfn(pfn, 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>
Chris Wilsonbcaaa0c2016-08-23 16:50:24 +0100103#include <asm/pgtable.h>
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -0800104
Keith Packarde5beae12008-11-03 18:21:45 +0100105/* Create the io_mapping object*/
106static inline struct io_mapping *
Chris Wilsoncafaf142016-08-19 16:54:26 +0100107io_mapping_init_wc(struct io_mapping *iomap,
108 resource_size_t base,
109 unsigned long size)
Keith Packarde5beae12008-11-03 18:21:45 +0100110{
Chris Wilsoncafaf142016-08-19 16:54:26 +0100111 iomap->base = base;
112 iomap->size = size;
113 iomap->iomem = ioremap_wc(base, size);
Daniel Vetter35124382016-08-23 22:15:02 +0200114#if defined(pgprot_noncached_wc) /* archs can't agree on a name ... */
115 iomap->prot = pgprot_noncached_wc(PAGE_KERNEL);
116#elif defined(pgprot_writecombine)
Chris Wilsonbcaaa0c2016-08-23 16:50:24 +0100117 iomap->prot = pgprot_writecombine(PAGE_KERNEL);
Daniel Vetter35124382016-08-23 22:15:02 +0200118#else
119 iomap->prot = pgprot_noncached(PAGE_KERNEL);
120#endif
Chris Wilsoncafaf142016-08-19 16:54:26 +0100121
122 return iomap;
Keith Packarde5beae12008-11-03 18:21:45 +0100123}
124
125static inline void
Chris Wilsoncafaf142016-08-19 16:54:26 +0100126io_mapping_fini(struct io_mapping *mapping)
Keith Packarde5beae12008-11-03 18:21:45 +0100127{
Chris Wilsoncafaf142016-08-19 16:54:26 +0100128 iounmap(mapping->iomem);
129}
130
131/* Non-atomic map/unmap */
132static inline void __iomem *
133io_mapping_map_wc(struct io_mapping *mapping,
134 unsigned long offset,
135 unsigned long size)
136{
137 return mapping->iomem + offset;
138}
139
140static inline void
141io_mapping_unmap(void __iomem *vaddr)
142{
Keith Packarde5beae12008-11-03 18:21:45 +0100143}
144
145/* Atomic map/unmap */
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200146static inline void __iomem *
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100147io_mapping_map_atomic_wc(struct io_mapping *mapping,
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700148 unsigned long offset)
Keith Packarde5beae12008-11-03 18:21:45 +0100149{
David Hildenbrand2cb7c9c2015-05-11 17:52:09 +0200150 preempt_disable();
Daniel Vetter24dd85f2011-09-28 11:57:23 +0200151 pagefault_disable();
Chris Wilsoncafaf142016-08-19 16:54:26 +0100152 return io_mapping_map_wc(mapping, offset, PAGE_SIZE);
Keith Packarde5beae12008-11-03 18:21:45 +0100153}
154
155static inline void
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700156io_mapping_unmap_atomic(void __iomem *vaddr)
Keith Packarde5beae12008-11-03 18:21:45 +0100157{
Chris Wilsoncafaf142016-08-19 16:54:26 +0100158 io_mapping_unmap(vaddr);
Daniel Vetter24dd85f2011-09-28 11:57:23 +0200159 pagefault_enable();
David Hildenbrand2cb7c9c2015-05-11 17:52:09 +0200160 preempt_enable();
Keith Packarde5beae12008-11-03 18:21:45 +0100161}
162
Chris Wilsoncafaf142016-08-19 16:54:26 +0100163#endif /* HAVE_ATOMIC_IOMAP */
164
165static inline struct io_mapping *
166io_mapping_create_wc(resource_size_t base,
167 unsigned long size)
Keith Packarde5beae12008-11-03 18:21:45 +0100168{
Chris Wilsoncafaf142016-08-19 16:54:26 +0100169 struct io_mapping *iomap;
170
171 iomap = kmalloc(sizeof(*iomap), GFP_KERNEL);
172 if (!iomap)
173 return NULL;
174
175 if (!io_mapping_init_wc(iomap, base, size)) {
176 kfree(iomap);
177 return NULL;
178 }
179
180 return iomap;
Keith Packarde5beae12008-11-03 18:21:45 +0100181}
182
183static inline void
Chris Wilsoncafaf142016-08-19 16:54:26 +0100184io_mapping_free(struct io_mapping *iomap)
Keith Packarde5beae12008-11-03 18:21:45 +0100185{
Chris Wilsoncafaf142016-08-19 16:54:26 +0100186 io_mapping_fini(iomap);
187 kfree(iomap);
Keith Packarde5beae12008-11-03 18:21:45 +0100188}
189
Keith Packard9663f2e2008-10-30 19:38:18 -0700190#endif /* _LINUX_IO_MAPPING_H */