xref: /openbmc/linux/mm/io-mapping.c (revision 9659281c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include <linux/mm.h>
4 #include <linux/io-mapping.h>
5 
6 /**
7  * io_mapping_map_user - remap an I/O mapping to userspace
8  * @iomap: the source io_mapping
9  * @vma: user vma to map to
10  * @addr: target user address to start at
11  * @pfn: physical address of kernel memory
12  * @size: size of map area
13  *
14  *  Note: this is only safe if the mm semaphore is held when called.
15  */
16 int io_mapping_map_user(struct io_mapping *iomap, struct vm_area_struct *vma,
17 		unsigned long addr, unsigned long pfn, unsigned long size)
18 {
19 	vm_flags_t expected_flags = VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
20 
21 	if (WARN_ON_ONCE((vma->vm_flags & expected_flags) != expected_flags))
22 		return -EINVAL;
23 
24 	/* We rely on prevalidation of the io-mapping to skip track_pfn(). */
25 	return remap_pfn_range_notrack(vma, addr, pfn, size,
26 		__pgprot((pgprot_val(iomap->prot) & _PAGE_CACHE_MASK) |
27 			 (pgprot_val(vma->vm_page_prot) & ~_PAGE_CACHE_MASK)));
28 }
29 EXPORT_SYMBOL_GPL(io_mapping_map_user);
30