blob: e399029b68c5bbdc5ae613c681217c9c554c0f9f [file] [log] [blame]
Keith Packard9663f2e2008-10-30 19:38:18 -07001/*
2 * Copyright © 2008 Keith Packard <keithp@keithp.com>
3 *
4 * This file is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
16 */
17
18#ifndef _LINUX_IO_MAPPING_H
19#define _LINUX_IO_MAPPING_H
20
21#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Paul Gortmaker187f1882011-11-23 20:12:59 -050023#include <linux/bug.h>
Dan Williams2584cf82015-08-10 23:07:05 -040024#include <linux/io.h>
Keith Packard9663f2e2008-10-30 19:38:18 -070025#include <asm/page.h>
Keith Packard9663f2e2008-10-30 19:38:18 -070026
27/*
28 * The io_mapping mechanism provides an abstraction for mapping
29 * individual pages from an io device to the CPU in an efficient fashion.
30 *
Paul Bolle395cf962011-08-15 02:02:26 +020031 * See Documentation/io-mapping.txt
Keith Packard9663f2e2008-10-30 19:38:18 -070032 */
33
Keith Packarde5beae12008-11-03 18:21:45 +010034#ifdef CONFIG_HAVE_ATOMIC_IOMAP
Keith Packard9663f2e2008-10-30 19:38:18 -070035
Dave Airlie31ce4bf2010-08-12 11:47:50 +100036#include <asm/iomap.h>
37
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080038struct io_mapping {
39 resource_size_t base;
40 unsigned long size;
41 pgprot_t prot;
42};
43
Keith Packarde5beae12008-11-03 18:21:45 +010044/*
45 * For small address space machines, mapping large objects
46 * into the kernel virtual space isn't practical. Where
47 * available, use fixmap support to dynamically map pages
48 * of the object at run time.
49 */
Keith Packard9663f2e2008-10-30 19:38:18 -070050
Keith Packard9663f2e2008-10-30 19:38:18 -070051static inline struct io_mapping *
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080052io_mapping_create_wc(resource_size_t base, unsigned long size)
Keith Packard9663f2e2008-10-30 19:38:18 -070053{
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080054 struct io_mapping *iomap;
Venkatesh Pallipadi9e36fda2009-07-10 09:57:35 -070055 pgprot_t prot;
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080056
57 iomap = kmalloc(sizeof(*iomap), GFP_KERNEL);
58 if (!iomap)
Venkatesh Pallipadi9e36fda2009-07-10 09:57:35 -070059 goto out_err;
60
61 if (iomap_create_wc(base, size, &prot))
62 goto out_free;
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080063
64 iomap->base = base;
65 iomap->size = size;
Venkatesh Pallipadi9e36fda2009-07-10 09:57:35 -070066 iomap->prot = prot;
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080067 return iomap;
Venkatesh Pallipadi9e36fda2009-07-10 09:57:35 -070068
69out_free:
70 kfree(iomap);
71out_err:
72 return NULL;
Keith Packard9663f2e2008-10-30 19:38:18 -070073}
74
75static inline void
76io_mapping_free(struct io_mapping *mapping)
77{
Venkatesh Pallipadi9e36fda2009-07-10 09:57:35 -070078 iomap_free(mapping->base, mapping->size);
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080079 kfree(mapping);
Keith Packard9663f2e2008-10-30 19:38:18 -070080}
81
82/* Atomic map/unmap */
Francisco Jerez29bc17e2010-09-04 22:56:44 +020083static inline void __iomem *
Chris Wilsonfca3ec02010-08-04 14:34:24 +010084io_mapping_map_atomic_wc(struct io_mapping *mapping,
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070085 unsigned long offset)
Keith Packard9663f2e2008-10-30 19:38:18 -070086{
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -080087 resource_size_t phys_addr;
88 unsigned long pfn;
89
90 BUG_ON(offset >= mapping->size);
91 phys_addr = mapping->base + offset;
92 pfn = (unsigned long) (phys_addr >> PAGE_SHIFT);
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070093 return iomap_atomic_prot_pfn(pfn, mapping->prot);
Keith Packard9663f2e2008-10-30 19:38:18 -070094}
95
96static inline void
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070097io_mapping_unmap_atomic(void __iomem *vaddr)
Keith Packard9663f2e2008-10-30 19:38:18 -070098{
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -070099 iounmap_atomic(vaddr);
Keith Packard9663f2e2008-10-30 19:38:18 -0700100}
101
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200102static inline void __iomem *
Keith Packard9663f2e2008-10-30 19:38:18 -0700103io_mapping_map_wc(struct io_mapping *mapping, unsigned long offset)
104{
Pallipadi, Venkatesh5ce04e32009-03-01 08:53:27 -0800105 resource_size_t phys_addr;
106
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -0800107 BUG_ON(offset >= mapping->size);
Pallipadi, Venkatesh5ce04e32009-03-01 08:53:27 -0800108 phys_addr = mapping->base + offset;
109
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -0800110 return ioremap_wc(phys_addr, PAGE_SIZE);
Keith Packard9663f2e2008-10-30 19:38:18 -0700111}
112
113static inline void
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200114io_mapping_unmap(void __iomem *vaddr)
Keith Packard9663f2e2008-10-30 19:38:18 -0700115{
116 iounmap(vaddr);
117}
Keith Packarde5beae12008-11-03 18:21:45 +0100118
119#else
120
Daniel Vetter24dd85f2011-09-28 11:57:23 +0200121#include <linux/uaccess.h>
122
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -0800123/* this struct isn't actually defined anywhere */
124struct io_mapping;
125
Keith Packarde5beae12008-11-03 18:21:45 +0100126/* Create the io_mapping object*/
127static inline struct io_mapping *
Venkatesh Pallipadi4ab0d472009-02-24 17:35:12 -0800128io_mapping_create_wc(resource_size_t base, unsigned long size)
Keith Packarde5beae12008-11-03 18:21:45 +0100129{
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200130 return (struct io_mapping __force *) ioremap_wc(base, size);
Keith Packarde5beae12008-11-03 18:21:45 +0100131}
132
133static inline void
134io_mapping_free(struct io_mapping *mapping)
135{
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200136 iounmap((void __force __iomem *) mapping);
Keith Packarde5beae12008-11-03 18:21:45 +0100137}
138
139/* Atomic map/unmap */
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200140static inline void __iomem *
Chris Wilsonfca3ec02010-08-04 14:34:24 +0100141io_mapping_map_atomic_wc(struct io_mapping *mapping,
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700142 unsigned long offset)
Keith Packarde5beae12008-11-03 18:21:45 +0100143{
David Hildenbrand2cb7c9c2015-05-11 17:52:09 +0200144 preempt_disable();
Daniel Vetter24dd85f2011-09-28 11:57:23 +0200145 pagefault_disable();
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200146 return ((char __force __iomem *) mapping) + offset;
Keith Packarde5beae12008-11-03 18:21:45 +0100147}
148
149static inline void
Peter Zijlstra3e4d3af2010-10-26 14:21:51 -0700150io_mapping_unmap_atomic(void __iomem *vaddr)
Keith Packarde5beae12008-11-03 18:21:45 +0100151{
Daniel Vetter24dd85f2011-09-28 11:57:23 +0200152 pagefault_enable();
David Hildenbrand2cb7c9c2015-05-11 17:52:09 +0200153 preempt_enable();
Keith Packarde5beae12008-11-03 18:21:45 +0100154}
155
156/* Non-atomic map/unmap */
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200157static inline void __iomem *
Keith Packarde5beae12008-11-03 18:21:45 +0100158io_mapping_map_wc(struct io_mapping *mapping, unsigned long offset)
159{
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200160 return ((char __force __iomem *) mapping) + offset;
Keith Packarde5beae12008-11-03 18:21:45 +0100161}
162
163static inline void
Francisco Jerez29bc17e2010-09-04 22:56:44 +0200164io_mapping_unmap(void __iomem *vaddr)
Keith Packarde5beae12008-11-03 18:21:45 +0100165{
166}
167
168#endif /* HAVE_ATOMIC_IOMAP */
Keith Packard9663f2e2008-10-30 19:38:18 -0700169
170#endif /* _LINUX_IO_MAPPING_H */