xref: /openbmc/linux/drivers/gpu/drm/i915/i915_mm.c (revision 03638e62)
1 /*
2  * Copyright © 2014 Intel Corporation
3  *
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:
10  *
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
13  * Software.
14  *
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
21  * IN THE SOFTWARE.
22  *
23  */
24 
25 #include <linux/mm.h>
26 #include <linux/io-mapping.h>
27 
28 #include <asm/pgtable.h>
29 
30 #include "i915_drv.h"
31 
32 struct remap_pfn {
33 	struct mm_struct *mm;
34 	unsigned long pfn;
35 	pgprot_t prot;
36 };
37 
38 static int remap_pfn(pte_t *pte, unsigned long addr, void *data)
39 {
40 	struct remap_pfn *r = data;
41 
42 	/* Special PTE are not associated with any struct page */
43 	set_pte_at(r->mm, addr, pte, pte_mkspecial(pfn_pte(r->pfn, r->prot)));
44 	r->pfn++;
45 
46 	return 0;
47 }
48 
49 /**
50  * remap_io_mapping - remap an IO mapping to userspace
51  * @vma: user vma to map to
52  * @addr: target user address to start at
53  * @pfn: physical address of kernel memory
54  * @size: size of map area
55  * @iomap: the source io_mapping
56  *
57  *  Note: this is only safe if the mm semaphore is held when called.
58  */
59 int remap_io_mapping(struct vm_area_struct *vma,
60 		     unsigned long addr, unsigned long pfn, unsigned long size,
61 		     struct io_mapping *iomap)
62 {
63 	struct remap_pfn r;
64 	int err;
65 
66 	GEM_BUG_ON((vma->vm_flags &
67 		    (VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP)) !=
68 		   (VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP));
69 
70 	/* We rely on prevalidation of the io-mapping to skip track_pfn(). */
71 	r.mm = vma->vm_mm;
72 	r.pfn = pfn;
73 	r.prot = __pgprot((pgprot_val(iomap->prot) & _PAGE_CACHE_MASK) |
74 			  (pgprot_val(vma->vm_page_prot) & ~_PAGE_CACHE_MASK));
75 
76 	err = apply_to_page_range(r.mm, addr, size, remap_pfn, &r);
77 	if (unlikely(err)) {
78 		zap_vma_ptes(vma, addr, (r.pfn - pfn) << PAGE_SHIFT);
79 		return err;
80 	}
81 
82 	return 0;
83 }
84