1c58305afSChris Wilson /*
2c58305afSChris Wilson * Copyright © 2014 Intel Corporation
3c58305afSChris Wilson *
4c58305afSChris Wilson * Permission is hereby granted, free of charge, to any person obtaining a
5c58305afSChris Wilson * copy of this software and associated documentation files (the "Software"),
6c58305afSChris Wilson * to deal in the Software without restriction, including without limitation
7c58305afSChris Wilson * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8c58305afSChris Wilson * and/or sell copies of the Software, and to permit persons to whom the
9c58305afSChris Wilson * Software is furnished to do so, subject to the following conditions:
10c58305afSChris Wilson *
11c58305afSChris Wilson * The above copyright notice and this permission notice (including the next
12c58305afSChris Wilson * paragraph) shall be included in all copies or substantial portions of the
13c58305afSChris Wilson * Software.
14c58305afSChris Wilson *
15c58305afSChris Wilson * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16c58305afSChris Wilson * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17c58305afSChris Wilson * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18c58305afSChris Wilson * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19c58305afSChris Wilson * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20c58305afSChris Wilson * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21c58305afSChris Wilson * IN THE SOFTWARE.
22c58305afSChris Wilson *
23c58305afSChris Wilson */
24c58305afSChris Wilson
25c58305afSChris Wilson #include <linux/mm.h>
26c58305afSChris Wilson #include <linux/io-mapping.h>
27c58305afSChris Wilson
28c58305afSChris Wilson
29c58305afSChris Wilson #include "i915_drv.h"
30*67c430bbSSiva Mullati #include "i915_mm.h"
31c58305afSChris Wilson
32293837b9SLinus Torvalds struct remap_pfn {
33293837b9SLinus Torvalds struct mm_struct *mm;
34293837b9SLinus Torvalds unsigned long pfn;
35293837b9SLinus Torvalds pgprot_t prot;
36293837b9SLinus Torvalds
37293837b9SLinus Torvalds struct sgt_iter sgt;
38293837b9SLinus Torvalds resource_size_t iobase;
39293837b9SLinus Torvalds };
40c58305afSChris Wilson
414e598fadSAbdiel Janulgue #define use_dma(io) ((io) != -1)
424e598fadSAbdiel Janulgue
sgt_pfn(const struct remap_pfn * r)43293837b9SLinus Torvalds static inline unsigned long sgt_pfn(const struct remap_pfn *r)
44293837b9SLinus Torvalds {
45293837b9SLinus Torvalds if (use_dma(r->iobase))
46293837b9SLinus Torvalds return (r->sgt.dma + r->sgt.curr + r->iobase) >> PAGE_SHIFT;
47293837b9SLinus Torvalds else
48293837b9SLinus Torvalds return r->sgt.pfn + (r->sgt.curr >> PAGE_SHIFT);
49293837b9SLinus Torvalds }
50293837b9SLinus Torvalds
remap_sg(pte_t * pte,unsigned long addr,void * data)51293837b9SLinus Torvalds static int remap_sg(pte_t *pte, unsigned long addr, void *data)
52293837b9SLinus Torvalds {
53293837b9SLinus Torvalds struct remap_pfn *r = data;
54293837b9SLinus Torvalds
55293837b9SLinus Torvalds if (GEM_WARN_ON(!r->sgt.sgp))
56293837b9SLinus Torvalds return -EINVAL;
57293837b9SLinus Torvalds
58293837b9SLinus Torvalds /* Special PTE are not associated with any struct page */
59293837b9SLinus Torvalds set_pte_at(r->mm, addr, pte,
60293837b9SLinus Torvalds pte_mkspecial(pfn_pte(sgt_pfn(r), r->prot)));
61293837b9SLinus Torvalds r->pfn++; /* track insertions in case we need to unwind later */
62293837b9SLinus Torvalds
63293837b9SLinus Torvalds r->sgt.curr += PAGE_SIZE;
64293837b9SLinus Torvalds if (r->sgt.curr >= r->sgt.max)
65293837b9SLinus Torvalds r->sgt = __sgt_iter(__sg_next(r->sgt.sgp), use_dma(r->iobase));
66293837b9SLinus Torvalds
67293837b9SLinus Torvalds return 0;
68293837b9SLinus Torvalds }
69293837b9SLinus Torvalds
70*67c430bbSSiva Mullati #define EXPECTED_FLAGS (VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP)
71*67c430bbSSiva Mullati
72*67c430bbSSiva Mullati #if IS_ENABLED(CONFIG_X86)
remap_pfn(pte_t * pte,unsigned long addr,void * data)73*67c430bbSSiva Mullati static int remap_pfn(pte_t *pte, unsigned long addr, void *data)
74*67c430bbSSiva Mullati {
75*67c430bbSSiva Mullati struct remap_pfn *r = data;
76*67c430bbSSiva Mullati
77*67c430bbSSiva Mullati /* Special PTE are not associated with any struct page */
78*67c430bbSSiva Mullati set_pte_at(r->mm, addr, pte, pte_mkspecial(pfn_pte(r->pfn, r->prot)));
79*67c430bbSSiva Mullati r->pfn++;
80*67c430bbSSiva Mullati
81*67c430bbSSiva Mullati return 0;
82*67c430bbSSiva Mullati }
83*67c430bbSSiva Mullati
840e4fe0c9SMatthew Auld /**
850e4fe0c9SMatthew Auld * remap_io_mapping - remap an IO mapping to userspace
860e4fe0c9SMatthew Auld * @vma: user vma to map to
870e4fe0c9SMatthew Auld * @addr: target user address to start at
880e4fe0c9SMatthew Auld * @pfn: physical address of kernel memory
890e4fe0c9SMatthew Auld * @size: size of map area
900e4fe0c9SMatthew Auld * @iomap: the source io_mapping
910e4fe0c9SMatthew Auld *
920e4fe0c9SMatthew Auld * Note: this is only safe if the mm semaphore is held when called.
930e4fe0c9SMatthew Auld */
remap_io_mapping(struct vm_area_struct * vma,unsigned long addr,unsigned long pfn,unsigned long size,struct io_mapping * iomap)940e4fe0c9SMatthew Auld int remap_io_mapping(struct vm_area_struct *vma,
950e4fe0c9SMatthew Auld unsigned long addr, unsigned long pfn, unsigned long size,
960e4fe0c9SMatthew Auld struct io_mapping *iomap)
970e4fe0c9SMatthew Auld {
980e4fe0c9SMatthew Auld struct remap_pfn r;
990e4fe0c9SMatthew Auld int err;
1000e4fe0c9SMatthew Auld
1010e4fe0c9SMatthew Auld GEM_BUG_ON((vma->vm_flags & EXPECTED_FLAGS) != EXPECTED_FLAGS);
1020e4fe0c9SMatthew Auld
1030e4fe0c9SMatthew Auld /* We rely on prevalidation of the io-mapping to skip track_pfn(). */
1040e4fe0c9SMatthew Auld r.mm = vma->vm_mm;
1050e4fe0c9SMatthew Auld r.pfn = pfn;
1060e4fe0c9SMatthew Auld r.prot = __pgprot((pgprot_val(iomap->prot) & _PAGE_CACHE_MASK) |
1070e4fe0c9SMatthew Auld (pgprot_val(vma->vm_page_prot) & ~_PAGE_CACHE_MASK));
1080e4fe0c9SMatthew Auld
1090e4fe0c9SMatthew Auld err = apply_to_page_range(r.mm, addr, size, remap_pfn, &r);
1100e4fe0c9SMatthew Auld if (unlikely(err)) {
1110e4fe0c9SMatthew Auld zap_vma_ptes(vma, addr, (r.pfn - pfn) << PAGE_SHIFT);
1120e4fe0c9SMatthew Auld return err;
1130e4fe0c9SMatthew Auld }
1140e4fe0c9SMatthew Auld
1150e4fe0c9SMatthew Auld return 0;
1160e4fe0c9SMatthew Auld }
117*67c430bbSSiva Mullati #endif
118293837b9SLinus Torvalds
1191764b992SAbdiel Janulgue /**
1204e598fadSAbdiel Janulgue * remap_io_sg - remap an IO mapping to userspace
1211764b992SAbdiel Janulgue * @vma: user vma to map to
1221764b992SAbdiel Janulgue * @addr: target user address to start at
1231764b992SAbdiel Janulgue * @size: size of map area
1241764b992SAbdiel Janulgue * @sgl: Start sg entry
1254e598fadSAbdiel Janulgue * @iobase: Use stored dma address offset by this address or pfn if -1
1261764b992SAbdiel Janulgue *
1271764b992SAbdiel Janulgue * Note: this is only safe if the mm semaphore is held when called.
1281764b992SAbdiel Janulgue */
remap_io_sg(struct vm_area_struct * vma,unsigned long addr,unsigned long size,struct scatterlist * sgl,resource_size_t iobase)1294e598fadSAbdiel Janulgue int remap_io_sg(struct vm_area_struct *vma,
1301764b992SAbdiel Janulgue unsigned long addr, unsigned long size,
1314e598fadSAbdiel Janulgue struct scatterlist *sgl, resource_size_t iobase)
1321764b992SAbdiel Janulgue {
133293837b9SLinus Torvalds struct remap_pfn r = {
134293837b9SLinus Torvalds .mm = vma->vm_mm,
135293837b9SLinus Torvalds .prot = vma->vm_page_prot,
136293837b9SLinus Torvalds .sgt = __sgt_iter(sgl, use_dma(iobase)),
137293837b9SLinus Torvalds .iobase = iobase,
138293837b9SLinus Torvalds };
1391764b992SAbdiel Janulgue int err;
1401764b992SAbdiel Janulgue
1411764b992SAbdiel Janulgue /* We rely on prevalidation of the io-mapping to skip track_pfn(). */
1421764b992SAbdiel Janulgue GEM_BUG_ON((vma->vm_flags & EXPECTED_FLAGS) != EXPECTED_FLAGS);
1431764b992SAbdiel Janulgue
1444e598fadSAbdiel Janulgue if (!use_dma(iobase))
1451764b992SAbdiel Janulgue flush_cache_range(vma, addr, size);
1464e598fadSAbdiel Janulgue
147293837b9SLinus Torvalds err = apply_to_page_range(r.mm, addr, size, remap_sg, &r);
148293837b9SLinus Torvalds if (unlikely(err)) {
149293837b9SLinus Torvalds zap_vma_ptes(vma, addr, r.pfn << PAGE_SHIFT);
150293837b9SLinus Torvalds return err;
1511764b992SAbdiel Janulgue }
1521764b992SAbdiel Janulgue
153293837b9SLinus Torvalds return 0;
1541764b992SAbdiel Janulgue }
155