2 * Copyright © 2014 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 #include <linux/io-mapping.h>
38 resource_size_t iobase;
41 #define use_dma(io) ((io) != -1)
43 static inline unsigned long sgt_pfn(const struct remap_pfn *r)
45 if (use_dma(r->iobase))
46 return (r->sgt.dma + r->sgt.curr + r->iobase) >> PAGE_SHIFT;
48 return r->sgt.pfn + (r->sgt.curr >> PAGE_SHIFT);
51 static int remap_sg(pte_t *pte, unsigned long addr, void *data)
53 struct remap_pfn *r = data;
55 if (GEM_WARN_ON(!r->sgt.sgp))
58 /* Special PTE are not associated with any struct page */
59 set_pte_at(r->mm, addr, pte,
60 pte_mkspecial(pfn_pte(sgt_pfn(r), r->prot)));
61 r->pfn++; /* track insertions in case we need to unwind later */
63 r->sgt.curr += PAGE_SIZE;
64 if (r->sgt.curr >= r->sgt.max)
65 r->sgt = __sgt_iter(__sg_next(r->sgt.sgp), use_dma(r->iobase));
70 #define EXPECTED_FLAGS (VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP)
72 #if IS_ENABLED(CONFIG_X86)
73 static int remap_pfn(pte_t *pte, unsigned long addr, void *data)
75 struct remap_pfn *r = data;
77 /* Special PTE are not associated with any struct page */
78 set_pte_at(r->mm, addr, pte, pte_mkspecial(pfn_pte(r->pfn, r->prot)));
85 * remap_io_mapping - remap an IO mapping to userspace
86 * @vma: user vma to map to
87 * @addr: target user address to start at
88 * @pfn: physical address of kernel memory
89 * @size: size of map area
90 * @iomap: the source io_mapping
92 * Note: this is only safe if the mm semaphore is held when called.
94 int remap_io_mapping(struct vm_area_struct *vma,
95 unsigned long addr, unsigned long pfn, unsigned long size,
96 struct io_mapping *iomap)
101 GEM_BUG_ON((vma->vm_flags & EXPECTED_FLAGS) != EXPECTED_FLAGS);
103 /* We rely on prevalidation of the io-mapping to skip track_pfn(). */
106 r.prot = __pgprot((pgprot_val(iomap->prot) & _PAGE_CACHE_MASK) |
107 (pgprot_val(vma->vm_page_prot) & ~_PAGE_CACHE_MASK));
109 err = apply_to_page_range(r.mm, addr, size, remap_pfn, &r);
111 zap_vma_ptes(vma, addr, (r.pfn - pfn) << PAGE_SHIFT);
120 * remap_io_sg - remap an IO mapping to userspace
121 * @vma: user vma to map to
122 * @addr: target user address to start at
123 * @size: size of map area
124 * @sgl: Start sg entry
125 * @offset: offset from the start of the page
126 * @iobase: Use stored dma address offset by this address or pfn if -1
128 * Note: this is only safe if the mm semaphore is held when called.
130 int remap_io_sg(struct vm_area_struct *vma,
131 unsigned long addr, unsigned long size,
132 struct scatterlist *sgl, unsigned long offset,
133 resource_size_t iobase)
135 struct remap_pfn r = {
137 .prot = vma->vm_page_prot,
138 .sgt = __sgt_iter(sgl, use_dma(iobase)),
143 /* We rely on prevalidation of the io-mapping to skip track_pfn(). */
144 GEM_BUG_ON((vma->vm_flags & EXPECTED_FLAGS) != EXPECTED_FLAGS);
146 while (offset >= r.sgt.max >> PAGE_SHIFT) {
147 offset -= r.sgt.max >> PAGE_SHIFT;
148 r.sgt = __sgt_iter(__sg_next(r.sgt.sgp), use_dma(iobase));
152 r.sgt.curr = offset << PAGE_SHIFT;
154 if (!use_dma(iobase))
155 flush_cache_range(vma, addr, size);
157 err = apply_to_page_range(r.mm, addr, size, remap_sg, &r);
159 zap_vma_ptes(vma, addr, r.pfn << PAGE_SHIFT);