Merge branch 'work.file' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[platform/kernel/linux-rpi.git] / drivers / gpu / drm / i915 / i915_mm.c
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
29 #include "i915_drv.h"
30
31 #define EXPECTED_FLAGS (VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP)
32
33 #define use_dma(io) ((io) != -1)
34
35 /**
36  * remap_io_sg - remap an IO mapping to userspace
37  * @vma: user vma to map to
38  * @addr: target user address to start at
39  * @size: size of map area
40  * @sgl: Start sg entry
41  * @iobase: Use stored dma address offset by this address or pfn if -1
42  *
43  *  Note: this is only safe if the mm semaphore is held when called.
44  */
45 int remap_io_sg(struct vm_area_struct *vma,
46                 unsigned long addr, unsigned long size,
47                 struct scatterlist *sgl, resource_size_t iobase)
48 {
49         unsigned long pfn, len, remapped = 0;
50         int err;
51
52         /* We rely on prevalidation of the io-mapping to skip track_pfn(). */
53         GEM_BUG_ON((vma->vm_flags & EXPECTED_FLAGS) != EXPECTED_FLAGS);
54
55         if (!use_dma(iobase))
56                 flush_cache_range(vma, addr, size);
57
58         do {
59                 if (use_dma(iobase)) {
60                         if (!sg_dma_len(sgl))
61                                 break;
62                         pfn = (sg_dma_address(sgl) + iobase) >> PAGE_SHIFT;
63                         len = sg_dma_len(sgl);
64                 } else {
65                         pfn = page_to_pfn(sg_page(sgl));
66                         len = sgl->length;
67                 }
68
69                 err = remap_pfn_range(vma, addr + remapped, pfn, len,
70                                       vma->vm_page_prot);
71                 if (err)
72                         break;
73                 remapped += len;
74         } while ((sgl = __sg_next(sgl)));
75
76         if (err)
77                 zap_vma_ptes(vma, addr, remapped);
78         return err;
79 }