2 * drivers/video/tegra/nvmap/nvmap_ioctl.c
4 * User-space interface to nvmap
6 * Copyright (c) 2011-2014, NVIDIA CORPORATION. All rights reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #define pr_fmt(fmt) "nvmap: %s() " fmt, __func__
25 #include <linux/dma-mapping.h>
26 #include <linux/export.h>
28 #include <linux/kernel.h>
29 #include <linux/slab.h>
30 #include <linux/uaccess.h>
31 #include <linux/nvmap.h>
32 #include <linux/vmalloc.h>
34 #include <asm/memory.h>
36 #include <trace/events/nvmap.h>
38 #include "nvmap_ioctl.h"
39 #include "nvmap_priv.h"
41 #include <linux/list.h>
43 static ssize_t rw_handle(struct nvmap_client *client, struct nvmap_handle *h,
44 int is_read, unsigned long h_offs,
45 unsigned long sys_addr, unsigned long h_stride,
46 unsigned long sys_stride, unsigned long elem_size,
49 struct nvmap_handle *unmarshal_user_handle(__u32 handle)
51 struct nvmap_handle *h;
53 h = nvmap_get_id_from_dmabuf_fd(NULL, (int)handle);
60 * marshal_id/unmarshal_id are for get_id/handle_from_id.
61 * These are added to support using Fd's for handle.
64 static __u32 marshal_id(struct nvmap_handle *handle)
66 return (__u32)((uintptr_t)handle >> 2);
69 static struct nvmap_handle *unmarshal_id(__u32 id)
71 uintptr_t h = ((id << 2) | PAGE_OFFSET);
73 return (struct nvmap_handle *)h;
76 static __u32 marshal_id(struct nvmap_handle *handle)
78 return (uintptr_t)handle;
81 static struct nvmap_handle *unmarshal_id(__u32 id)
83 return (struct nvmap_handle *)id;
87 struct nvmap_handle *__nvmap_ref_to_id(struct nvmap_handle_ref *ref)
89 if (!virt_addr_valid(ref))
94 int nvmap_ioctl_pinop(struct file *filp, bool is_pin, void __user *arg,
98 struct nvmap_pin_handle_32 op32;
99 __u32 __user *output32 = NULL;
101 struct nvmap_pin_handle op;
102 struct nvmap_handle *h;
103 struct nvmap_handle *on_stack[16];
104 struct nvmap_handle **refs;
105 unsigned long __user *output;
111 if (copy_from_user(&op32, arg, sizeof(op32)))
113 op.handles = (__u32 *)(uintptr_t)op32.handles;
114 op.count = op32.count;
117 if (copy_from_user(&op, arg, sizeof(op)))
124 size_t bytes = op.count * sizeof(*refs); /* kcalloc below will catch overflow. */
126 if (op.count > ARRAY_SIZE(on_stack))
127 refs = kcalloc(op.count, sizeof(*refs), GFP_KERNEL);
134 if (!access_ok(VERIFY_READ, op.handles, bytes)) {
139 for (i = 0; i < op.count; i++) {
141 if (__get_user(handle, &op.handles[i])) {
145 refs[i] = unmarshal_user_handle(handle);
154 /* Yes, we're storing a u32 in a pointer */
155 on_stack[0] = unmarshal_user_handle((u32)(uintptr_t)op.handles);
162 trace_nvmap_ioctl_pinop(filp->private_data, is_pin, op.count, refs);
164 err = nvmap_pin_ids(filp->private_data, op.count, refs);
166 nvmap_unpin_ids(filp->private_data, op.count, refs);
168 /* skip the output stage on unpin */
172 /* it is guaranteed that if nvmap_pin_ids returns 0 that
173 * all of the handle_ref objects are valid, so dereferencing
174 * directly here is safe */
178 output32 = (__u32 *)(uintptr_t)op.addr;
180 struct nvmap_pin_handle_32 __user *tmp = arg;
181 output32 = &tmp->addr;
192 struct nvmap_pin_handle __user *tmp = arg;
193 output = (unsigned long *)&tmp->addr;
200 for (i = 0; i < op.count && !err; i++) {
204 if (h->heap_pgalloc && h->pgalloc.contig)
205 addr = page_to_phys(h->pgalloc.pages[0]);
206 else if (h->heap_pgalloc)
207 addr = sg_dma_address(
208 ((struct sg_table *)h->attachment->priv)->sgl);
210 addr = h->carveout->base;
214 err = put_user((__u32)addr, &output32[i]);
217 err = put_user(addr, &output[i]);
221 nvmap_unpin_ids(filp->private_data, op.count, refs);
224 if (refs != on_stack)
230 int nvmap_ioctl_getid(struct file *filp, void __user *arg)
232 struct nvmap_client *client = filp->private_data;
233 struct nvmap_create_handle op;
234 struct nvmap_handle *h = NULL;
236 if (copy_from_user(&op, arg, sizeof(op)))
239 h = unmarshal_user_handle(op.handle);
243 h = nvmap_handle_get(h);
248 op.id = marshal_id(h);
249 if (client == h->owner)
254 return copy_to_user(arg, &op, sizeof(op)) ? -EFAULT : 0;
257 static int nvmap_share_release(struct inode *inode, struct file *file)
259 struct nvmap_handle *h = file->private_data;
265 static int nvmap_share_mmap(struct file *file, struct vm_area_struct *vma)
267 /* unsupported operation */
268 WARN(1, "mmap is not supported on fd, which shares nvmap handle");
272 const struct file_operations nvmap_fd_fops = {
273 .owner = THIS_MODULE,
274 .release = nvmap_share_release,
275 .mmap = nvmap_share_mmap,
278 int nvmap_ioctl_getfd(struct file *filp, void __user *arg)
280 struct nvmap_handle *handle;
281 struct nvmap_create_handle op;
282 struct nvmap_client *client = filp->private_data;
284 if (copy_from_user(&op, arg, sizeof(op)))
287 handle = unmarshal_user_handle(op.handle);
291 op.fd = nvmap_get_dmabuf_fd(client, handle);
295 if (copy_to_user(arg, &op, sizeof(op))) {
302 int nvmap_ioctl_alloc(struct file *filp, void __user *arg)
304 struct nvmap_alloc_handle op;
305 struct nvmap_client *client = filp->private_data;
306 struct nvmap_handle *handle;
308 if (copy_from_user(&op, arg, sizeof(op)))
311 handle = unmarshal_user_handle(op.handle);
315 if (op.align & (op.align - 1))
318 /* user-space handles are aligned to page boundaries, to prevent
320 op.align = max_t(size_t, op.align, PAGE_SIZE);
321 #if defined(CONFIG_NVMAP_FORCE_ZEROED_USER_PAGES)
322 op.flags |= NVMAP_HANDLE_ZEROED_PAGES;
325 return nvmap_alloc_handle(client, handle, op.heap_mask, op.align,
327 op.flags & (~NVMAP_HANDLE_KIND_SPECIFIED));
330 int nvmap_ioctl_alloc_kind(struct file *filp, void __user *arg)
332 struct nvmap_alloc_kind_handle op;
333 struct nvmap_client *client = filp->private_data;
334 struct nvmap_handle *handle;
336 if (copy_from_user(&op, arg, sizeof(op)))
339 handle = unmarshal_user_handle(op.handle);
343 if (op.align & (op.align - 1))
346 /* user-space handles are aligned to page boundaries, to prevent
348 op.align = max_t(size_t, op.align, PAGE_SIZE);
349 #if defined(CONFIG_NVMAP_FORCE_ZEROED_USER_PAGES)
350 op.flags |= NVMAP_HANDLE_ZEROED_PAGES;
353 return nvmap_alloc_handle(client, handle,
360 int nvmap_create_fd(struct nvmap_handle *h)
364 fd = __nvmap_dmabuf_fd(h->dmabuf, O_CLOEXEC);
367 pr_err("Out of file descriptors");
370 /* __nvmap_dmabuf_fd() associates fd with dma_buf->file *.
371 * fd close drops one ref count on dmabuf->file *.
372 * to balance ref count, ref count dma_buf.
374 get_dma_buf(h->dmabuf);
378 int nvmap_ioctl_create(struct file *filp, unsigned int cmd, void __user *arg)
380 struct nvmap_create_handle op;
381 struct nvmap_handle_ref *ref = NULL;
382 struct nvmap_client *client = filp->private_data;
386 if (copy_from_user(&op, arg, sizeof(op)))
392 if (cmd == NVMAP_IOC_CREATE) {
393 ref = nvmap_create_handle(client, PAGE_ALIGN(op.size));
395 ref->handle->orig_size = op.size;
396 } else if (cmd == NVMAP_IOC_FROM_ID) {
397 ref = nvmap_duplicate_handle(client, unmarshal_id(op.id), 0);
398 } else if (cmd == NVMAP_IOC_FROM_FD) {
399 ref = nvmap_create_handle_from_fd(client, op.fd);
407 fd = nvmap_create_fd(ref->handle);
413 if (copy_to_user(arg, &op, sizeof(op))) {
415 nvmap_free_handle(client, __nvmap_ref_to_id(ref));
423 int nvmap_map_into_caller_ptr(struct file *filp, void __user *arg, bool is32)
425 struct nvmap_client *client = filp->private_data;
426 struct nvmap_map_caller op;
428 struct nvmap_map_caller_32 op32;
430 struct nvmap_vma_priv *priv;
431 struct vm_area_struct *vma;
432 struct nvmap_handle *h = NULL;
437 if (copy_from_user(&op32, arg, sizeof(op32)))
439 op.handle = op32.handle;
440 op.offset = op32.offset;
441 op.length = op32.length;
442 op.flags = op32.length;
446 if (copy_from_user(&op, arg, sizeof(op)))
449 h = unmarshal_user_handle(op.handle);
454 h = nvmap_handle_get(h);
464 trace_nvmap_map_into_caller_ptr(client, h, op.offset,
465 op.length, op.flags);
466 down_read(¤t->mm->mmap_sem);
468 vma = find_vma(current->mm, op.addr);
474 if (op.offset & ~PAGE_MASK) {
479 if (op.offset >= h->size || op.length > h->size - op.offset) {
480 err = -EADDRNOTAVAIL;
484 /* the VMA must exactly match the requested mapping operation, and the
485 * VMA that is targetted must have been created by this driver
487 if ((vma->vm_start != op.addr) || !is_nvmap_vma(vma) ||
488 (vma->vm_end-vma->vm_start != op.length)) {
493 /* verify that each mmap() system call creates a unique VMA */
494 if (vma->vm_private_data)
497 if (!h->heap_pgalloc && (h->carveout->base & ~PAGE_MASK)) {
502 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
508 vma->vm_flags |= (h->heap_pgalloc ? 0 : VM_PFNMAP);
510 priv->offs = op.offset;
511 vma->vm_private_data = priv;
512 vma->vm_page_prot = nvmap_pgprot(h, vma->vm_page_prot);
516 up_read(¤t->mm->mmap_sem);
523 int nvmap_ioctl_get_param(struct file *filp, void __user *arg, bool is32)
526 struct nvmap_handle_param_32 __user *uarg32 = arg;
528 struct nvmap_handle_param __user *uarg = arg;
529 struct nvmap_handle_param op;
530 struct nvmap_client *client = filp->private_data;
531 struct nvmap_handle_ref *ref;
532 struct nvmap_handle *h;
537 /* This is safe because the incoming value of result doesn't matter */
539 if (copy_from_user(&op, arg,
540 sizeof(struct nvmap_handle_param_32)))
544 if (copy_from_user(&op, arg, sizeof(op)))
547 h = unmarshal_user_handle(op.handle);
551 h = nvmap_handle_get(h);
555 nvmap_ref_lock(client);
556 ref = __nvmap_validate_locked(client, h);
557 if (IS_ERR_OR_NULL(ref)) {
558 err = ref ? PTR_ERR(ref) : -EINVAL;
562 err = nvmap_get_handle_param(client, ref, op.param, &result);
566 err = put_user((__u32)result, &uarg32->result);
569 err = put_user((unsigned long)result, &uarg->result);
572 nvmap_ref_unlock(client);
577 int nvmap_ioctl_rw_handle(struct file *filp, int is_read, void __user *arg,
580 struct nvmap_client *client = filp->private_data;
581 struct nvmap_rw_handle __user *uarg = arg;
582 struct nvmap_rw_handle op;
584 struct nvmap_rw_handle_32 __user *uarg32 = arg;
585 struct nvmap_rw_handle_32 op32;
587 struct nvmap_handle *h;
593 if (copy_from_user(&op32, arg, sizeof(op32)))
596 op.handle = op32.handle;
597 op.offset = op32.offset;
598 op.elem_size = op32.elem_size;
599 op.hmem_stride = op32.hmem_stride;
600 op.user_stride = op32.user_stride;
601 op.count = op32.count;
604 if (copy_from_user(&op, arg, sizeof(op)))
607 h = unmarshal_user_handle(op.handle);
608 if (!h || !op.addr || !op.count || !op.elem_size)
611 h = nvmap_handle_get(h);
616 trace_nvmap_ioctl_rw_handle(client, h, is_read, op.offset,
617 op.addr, op.hmem_stride,
618 op.user_stride, op.elem_size, op.count);
619 copied = rw_handle(client, h, is_read, op.offset,
620 (unsigned long)op.addr, op.hmem_stride,
621 op.user_stride, op.elem_size, op.count);
627 } else if (copied < (op.count * op.elem_size))
632 __put_user(copied, &uarg32->count);
635 __put_user(copied, &uarg->count);
642 static int __nvmap_cache_maint(struct nvmap_client *client,
643 struct nvmap_cache_op *op)
645 struct vm_area_struct *vma;
646 struct nvmap_vma_priv *priv;
647 struct nvmap_handle *handle;
652 handle = unmarshal_user_handle(op->handle);
653 if (!handle || !op->addr || op->op < NVMAP_CACHE_OP_WB ||
654 op->op > NVMAP_CACHE_OP_WB_INV)
657 down_read(¤t->mm->mmap_sem);
659 vma = find_vma(current->active_mm, (unsigned long)op->addr);
660 if (!vma || !is_nvmap_vma(vma) ||
661 (ulong)op->addr < vma->vm_start ||
662 (ulong)op->addr >= vma->vm_end ||
663 op->len > vma->vm_end - (ulong)op->addr) {
664 err = -EADDRNOTAVAIL;
668 priv = (struct nvmap_vma_priv *)vma->vm_private_data;
670 if (priv->handle != handle) {
675 start = (unsigned long)op->addr - vma->vm_start +
676 (vma->vm_pgoff << PAGE_SHIFT);
677 end = start + op->len;
679 err = __nvmap_do_cache_maint(client, priv->handle, start, end, op->op,
682 up_read(¤t->mm->mmap_sem);
686 int nvmap_ioctl_cache_maint(struct file *filp, void __user *arg, bool is32)
688 struct nvmap_client *client = filp->private_data;
689 struct nvmap_cache_op op;
691 struct nvmap_cache_op_32 op32;
696 if (copy_from_user(&op32, arg, sizeof(op32)))
699 op.handle = op32.handle;
704 if (copy_from_user(&op, arg, sizeof(op)))
707 return __nvmap_cache_maint(client, &op);
710 int nvmap_ioctl_free(struct file *filp, unsigned long arg)
712 struct nvmap_client *client = filp->private_data;
717 nvmap_free_handle_user_id(client, arg);
718 return sys_close(arg);
721 static void inner_cache_maint(unsigned int op, void *vaddr, size_t size)
723 if (op == NVMAP_CACHE_OP_WB_INV)
724 dmac_flush_range(vaddr, vaddr + size);
725 else if (op == NVMAP_CACHE_OP_INV)
726 dmac_map_area(vaddr, size, DMA_FROM_DEVICE);
728 dmac_map_area(vaddr, size, DMA_TO_DEVICE);
731 static void outer_cache_maint(unsigned int op, phys_addr_t paddr, size_t size)
733 if (op == NVMAP_CACHE_OP_WB_INV)
734 outer_flush_range(paddr, paddr + size);
735 else if (op == NVMAP_CACHE_OP_INV)
736 outer_inv_range(paddr, paddr + size);
738 outer_clean_range(paddr, paddr + size);
741 static void heap_page_cache_maint(
742 struct nvmap_handle *h, unsigned long start, unsigned long end,
743 unsigned int op, bool inner, bool outer, pte_t **pte,
744 unsigned long kaddr, pgprot_t prot, bool clean_only_dirty)
746 if (h->userflags & NVMAP_HANDLE_CACHE_SYNC) {
748 * zap user VA->PA mappings so that any access to the pages
749 * will result in a fault and can be marked dirty
751 nvmap_handle_mkclean(h, start, end-start);
752 nvmap_zap_handle(h, start, end - start);
755 #ifdef NVMAP_LAZY_VFREE
761 /* mutex lock protection is not necessary as it is
762 * already increased in __nvmap_do_cache_maint to
763 * protect from migrations.
765 nvmap_kmaps_inc_no_lock(h);
766 pages = nvmap_pages(h->pgalloc.pages,
767 h->size >> PAGE_SHIFT);
769 goto per_page_cache_maint;
770 vaddr = vm_map_ram(pages,
771 h->size >> PAGE_SHIFT, -1, prot);
773 (h->size >> PAGE_SHIFT) * sizeof(*pages));
775 if (vaddr && atomic_long_cmpxchg(&h->vaddr, 0, (long)vaddr)) {
777 vm_unmap_ram(vaddr, h->size >> PAGE_SHIFT);
780 /* Fast inner cache maintenance using single mapping */
781 inner_cache_maint(op, h->vaddr + start, end - start);
784 /* Skip per-page inner maintenance in loop below */
788 per_page_cache_maint:
793 while (start < end) {
800 page = nvmap_to_page(h->pgalloc.pages[start >> PAGE_SHIFT]);
801 next = min(((start + PAGE_SIZE) & PAGE_MASK), end);
802 off = start & ~PAGE_MASK;
804 paddr = page_to_phys(page) + off;
807 void *vaddr = (void *)kaddr + off;
810 set_pte_at(&init_mm, kaddr, *pte,
811 pfn_pte(__phys_to_pfn(paddr), prot));
812 nvmap_flush_tlb_kernel_page(kaddr);
813 inner_cache_maint(op, vaddr, size);
817 outer_cache_maint(op, paddr, size);
822 #if defined(CONFIG_NVMAP_OUTER_CACHE_MAINT_BY_SET_WAYS)
823 static bool fast_cache_maint_outer(unsigned long start,
824 unsigned long end, unsigned int op)
827 if (end - start >= cache_maint_outer_threshold) {
828 if (op == NVMAP_CACHE_OP_WB_INV) {
832 if (op == NVMAP_CACHE_OP_WB) {
841 static inline bool fast_cache_maint_outer(unsigned long start,
842 unsigned long end, unsigned int op)
848 #if defined(CONFIG_NVMAP_CACHE_MAINT_BY_SET_WAYS)
849 static inline bool can_fast_cache_maint(struct nvmap_handle *h,
851 unsigned long end, unsigned int op)
853 if ((op == NVMAP_CACHE_OP_INV) ||
854 ((end - start) < cache_maint_inner_threshold))
859 static inline bool can_fast_cache_maint(struct nvmap_handle *h,
861 unsigned long end, unsigned int op)
867 static bool fast_cache_maint(struct nvmap_handle *h,
869 unsigned long end, unsigned int op,
870 bool clean_only_dirty)
872 if (!can_fast_cache_maint(h, start, end, op))
875 if (h->userflags & NVMAP_HANDLE_CACHE_SYNC) {
876 nvmap_handle_mkclean(h, 0, h->size);
877 nvmap_zap_handle(h, 0, h->size);
880 if (op == NVMAP_CACHE_OP_WB_INV)
881 inner_flush_cache_all();
882 else if (op == NVMAP_CACHE_OP_WB)
883 inner_clean_cache_all();
885 /* outer maintenance */
886 if (h->flags != NVMAP_HANDLE_INNER_CACHEABLE) {
887 if(!fast_cache_maint_outer(start, end, op))
889 if (h->heap_pgalloc) {
890 heap_page_cache_maint(h, start,
891 end, op, false, true, NULL, 0, 0,
896 pstart = start + h->carveout->base;
897 outer_cache_maint(op, pstart, end - start);
904 struct cache_maint_op {
908 struct nvmap_handle *h;
911 bool clean_only_dirty;
914 static int do_cache_maint(struct cache_maint_op *cache_work)
919 phys_addr_t pstart = cache_work->start;
920 phys_addr_t pend = cache_work->end;
923 struct nvmap_handle *h = cache_work->h;
924 struct nvmap_client *client;
925 unsigned int op = cache_work->op;
931 if (can_fast_cache_maint(h, pstart, pend, op))
932 nvmap_stats_inc(NS_CFLUSH_DONE, cache_maint_inner_threshold);
934 nvmap_stats_inc(NS_CFLUSH_DONE, pend - pstart);
935 trace_nvmap_cache_maint(client, h, pstart, pend, op, pend - pstart);
936 trace_nvmap_cache_flush(pend - pstart,
937 nvmap_stats_read(NS_ALLOC),
938 nvmap_stats_read(NS_CFLUSH_RQ),
939 nvmap_stats_read(NS_CFLUSH_DONE));
942 if (h->flags == NVMAP_HANDLE_UNCACHEABLE ||
943 h->flags == NVMAP_HANDLE_WRITE_COMBINE || pstart == pend)
946 if (fast_cache_maint(h, pstart, pend, op, cache_work->clean_only_dirty))
949 prot = nvmap_pgprot(h, PG_PROT_KERNEL);
950 pte = nvmap_alloc_pte(h->dev, (void **)&kaddr);
957 if (h->heap_pgalloc) {
958 heap_page_cache_maint(h, pstart, pend, op, true,
959 (h->flags == NVMAP_HANDLE_INNER_CACHEABLE) ?
962 cache_work->clean_only_dirty);
966 if (pstart > h->size || pend > h->size) {
967 pr_warn("cache maintenance outside handle\n");
972 pstart += h->carveout->base;
973 pend += h->carveout->base;
976 while (loop < pend) {
977 phys_addr_t next = (loop + PAGE_SIZE) & PAGE_MASK;
978 void *base = (void *)kaddr + (loop & ~PAGE_MASK);
979 next = min(next, pend);
981 set_pte_at(&init_mm, kaddr, *pte,
982 pfn_pte(__phys_to_pfn(loop), prot));
983 nvmap_flush_tlb_kernel_page(kaddr);
985 inner_cache_maint(op, base, next - loop);
989 if (h->flags != NVMAP_HANDLE_INNER_CACHEABLE)
990 outer_cache_maint(op, pstart, pend - pstart);
994 nvmap_free_pte(h->dev, pte);
998 int __nvmap_do_cache_maint(struct nvmap_client *client,
999 struct nvmap_handle *h,
1000 unsigned long start, unsigned long end,
1001 unsigned int op, bool clean_only_dirty)
1004 struct cache_maint_op cache_op;
1006 h = nvmap_handle_get(h);
1011 if (op == NVMAP_CACHE_OP_INV)
1012 op = NVMAP_CACHE_OP_WB_INV;
1014 /* clean only dirty is applicable only for Write Back operation */
1015 if (op != NVMAP_CACHE_OP_WB)
1016 clean_only_dirty = false;
1019 cache_op.start = start;
1022 cache_op.inner = h->flags == NVMAP_HANDLE_CACHEABLE ||
1023 h->flags == NVMAP_HANDLE_INNER_CACHEABLE;
1024 cache_op.outer = h->flags == NVMAP_HANDLE_CACHEABLE;
1025 cache_op.clean_only_dirty = clean_only_dirty;
1027 nvmap_stats_inc(NS_CFLUSH_RQ, end - start);
1028 err = do_cache_maint(&cache_op);
1030 nvmap_handle_put(h);
1034 static int rw_handle_page(struct nvmap_handle *h, int is_read,
1035 unsigned long start, unsigned long rw_addr,
1036 unsigned long bytes, unsigned long kaddr, pte_t *pte)
1038 pgprot_t prot = nvmap_pgprot(h, PG_PROT_KERNEL);
1039 unsigned long end = start + bytes;
1042 while (!err && start < end) {
1043 struct page *page = NULL;
1048 if (!h->heap_pgalloc) {
1049 phys = h->carveout->base + start;
1052 nvmap_to_page(h->pgalloc.pages[start >> PAGE_SHIFT]);
1055 phys = page_to_phys(page) + (start & ~PAGE_MASK);
1058 set_pte_at(&init_mm, kaddr, pte,
1059 pfn_pte(__phys_to_pfn(phys), prot));
1060 nvmap_flush_tlb_kernel_page(kaddr);
1062 src = (void *)kaddr + (phys & ~PAGE_MASK);
1063 phys = PAGE_SIZE - (phys & ~PAGE_MASK);
1064 count = min_t(size_t, end - start, phys);
1067 err = copy_to_user((void *)rw_addr, src, count);
1069 err = copy_from_user(src, (void *)rw_addr, count);
1084 static ssize_t rw_handle(struct nvmap_client *client, struct nvmap_handle *h,
1085 int is_read, unsigned long h_offs,
1086 unsigned long sys_addr, unsigned long h_stride,
1087 unsigned long sys_stride, unsigned long elem_size,
1088 unsigned long count)
1101 if (elem_size == h_stride && elem_size == sys_stride) {
1103 h_stride = elem_size;
1104 sys_stride = elem_size;
1108 pte = nvmap_alloc_pte(nvmap_dev, &addr);
1110 return PTR_ERR(pte);
1113 if (h_offs + elem_size > h->size) {
1114 nvmap_warn(client, "read/write outside of handle\n");
1119 __nvmap_do_cache_maint(client, h, h_offs,
1120 h_offs + elem_size, NVMAP_CACHE_OP_INV, false);
1122 ret = rw_handle_page(h, is_read, h_offs, sys_addr,
1123 elem_size, (unsigned long)addr, *pte);
1129 __nvmap_do_cache_maint(client, h, h_offs,
1130 h_offs + elem_size, NVMAP_CACHE_OP_WB_INV,
1133 copied += elem_size;
1134 sys_addr += sys_stride;
1138 nvmap_free_pte(nvmap_dev, pte);
1139 return ret ?: copied;
1142 int nvmap_ioctl_cache_maint_list(struct file *filp, void __user *arg,
1143 bool is_reserve_ioctl)
1145 struct nvmap_cache_op_list op;
1149 struct nvmap_handle **refs;
1152 if (copy_from_user(&op, arg, sizeof(op)))
1158 if (!access_ok(VERIFY_READ, op.handles, op.nr * sizeof(u32)))
1161 if (!access_ok(VERIFY_READ, op.offsets, op.nr * sizeof(u32)))
1164 if (!access_ok(VERIFY_READ, op.sizes, op.nr * sizeof(u32)))
1167 if (!op.offsets || !op.sizes)
1170 refs = kcalloc(op.nr, sizeof(*refs), GFP_KERNEL);
1175 handle_ptr = (u32 *)(uintptr_t)op.handles;
1176 offset_ptr = (u32 *)(uintptr_t)op.offsets;
1177 size_ptr = (u32 *)(uintptr_t)op.sizes;
1179 for (i = 0; i < op.nr; i++) {
1182 if (copy_from_user(&handle, &handle_ptr[i], sizeof(handle))) {
1187 refs[i] = unmarshal_user_handle(handle);
1194 if (is_reserve_ioctl)
1195 err = nvmap_reserve_pages(refs, offset_ptr, size_ptr,
1198 err = nvmap_do_cache_maint_list(refs, offset_ptr, size_ptr,