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>
31 #define EXPECTED_FLAGS (VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP)
33 #define use_dma(io) ((io) != -1)
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
43 * Note: this is only safe if the mm semaphore is held when called.
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)
49 unsigned long pfn, len, remapped = 0;
52 /* We rely on prevalidation of the io-mapping to skip track_pfn(). */
53 GEM_BUG_ON((vma->vm_flags & EXPECTED_FLAGS) != EXPECTED_FLAGS);
56 flush_cache_range(vma, addr, size);
59 if (use_dma(iobase)) {
62 pfn = (sg_dma_address(sgl) + iobase) >> PAGE_SHIFT;
63 len = sg_dma_len(sgl);
65 pfn = page_to_pfn(sg_page(sgl));
69 err = remap_pfn_range(vma, addr + remapped, pfn, len,
74 } while ((sgl = __sg_next(sgl)));
77 zap_vma_ptes(vma, addr, remapped);