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 /* NOTE: Callers of this utility function must invoke nvmap_handle_put after
50 * using the returned nvmap_handle.
52 struct nvmap_handle *unmarshal_user_handle(__u32 handle)
54 struct nvmap_handle *h;
56 h = nvmap_get_id_from_dmabuf_fd(NULL, (int)handle);
62 struct nvmap_handle *__nvmap_ref_to_id(struct nvmap_handle_ref *ref)
64 if (!virt_addr_valid(ref))
69 int nvmap_ioctl_pinop(struct file *filp, bool is_pin, void __user *arg,
73 struct nvmap_pin_handle_32 op32;
74 __u32 __user *output32 = NULL;
76 struct nvmap_pin_handle op;
77 struct nvmap_handle *h;
78 struct nvmap_handle *on_stack[16];
79 struct nvmap_handle **refs;
80 unsigned long __user *output;
82 u32 i, n_unmarshal_handles = 0;
86 if (copy_from_user(&op32, arg, sizeof(op32)))
88 op.handles = (__u32 *)(uintptr_t)op32.handles;
89 op.count = op32.count;
92 if (copy_from_user(&op, arg, sizeof(op)))
99 size_t bytes = op.count * sizeof(*refs); /* kcalloc below will catch overflow. */
101 if (op.count > ARRAY_SIZE(on_stack))
102 refs = kcalloc(op.count, sizeof(*refs), GFP_KERNEL);
109 if (!access_ok(VERIFY_READ, op.handles, bytes)) {
114 for (i = 0; i < op.count; i++) {
116 if (__get_user(handle, &op.handles[i])) {
120 refs[i] = unmarshal_user_handle(handle);
125 n_unmarshal_handles++;
130 /* Yes, we're storing a u32 in a pointer */
131 on_stack[0] = unmarshal_user_handle((u32)(uintptr_t)op.handles);
136 n_unmarshal_handles++;
139 trace_nvmap_ioctl_pinop(filp->private_data, is_pin, op.count, refs);
141 err = nvmap_pin_ids(filp->private_data, op.count, refs);
143 nvmap_unpin_ids(filp->private_data, op.count, refs);
145 /* skip the output stage on unpin */
149 /* it is guaranteed that if nvmap_pin_ids returns 0 that
150 * all of the handle_ref objects are valid, so dereferencing
151 * directly here is safe */
155 output32 = (__u32 *)(uintptr_t)op.addr;
157 struct nvmap_pin_handle_32 __user *tmp = arg;
158 output32 = &tmp->addr;
169 struct nvmap_pin_handle __user *tmp = arg;
170 output = (unsigned long *)&tmp->addr;
177 for (i = 0; i < op.count && !err; i++) {
181 if (h->heap_pgalloc && h->pgalloc.contig)
182 addr = page_to_phys(h->pgalloc.pages[0]);
183 else if (h->heap_pgalloc)
184 addr = sg_dma_address(
185 ((struct sg_table *)h->attachment->priv)->sgl);
187 addr = h->carveout->base;
191 err = put_user((__u32)addr, &output32[i]);
194 err = put_user(addr, &output[i]);
198 nvmap_unpin_ids(filp->private_data, op.count, refs);
201 for (i = 0; i < n_unmarshal_handles; i++)
202 nvmap_handle_put(refs[i]);
204 if (refs != on_stack)
210 static int nvmap_share_release(struct inode *inode, struct file *file)
212 struct nvmap_handle *h = file->private_data;
218 static int nvmap_share_mmap(struct file *file, struct vm_area_struct *vma)
220 /* unsupported operation */
221 WARN(1, "mmap is not supported on fd, which shares nvmap handle");
225 const struct file_operations nvmap_fd_fops = {
226 .owner = THIS_MODULE,
227 .release = nvmap_share_release,
228 .mmap = nvmap_share_mmap,
231 int nvmap_ioctl_getfd(struct file *filp, void __user *arg)
233 struct nvmap_handle *handle;
234 struct nvmap_create_handle op;
235 struct nvmap_client *client = filp->private_data;
237 if (copy_from_user(&op, arg, sizeof(op)))
240 handle = unmarshal_user_handle(op.handle);
244 op.fd = nvmap_get_dmabuf_fd(client, handle);
245 nvmap_handle_put(handle);
249 if (copy_to_user(arg, &op, sizeof(op))) {
256 int nvmap_ioctl_alloc(struct file *filp, void __user *arg)
258 struct nvmap_alloc_handle op;
259 struct nvmap_client *client = filp->private_data;
260 struct nvmap_handle *handle;
263 if (copy_from_user(&op, arg, sizeof(op)))
266 if (op.align & (op.align - 1))
269 handle = unmarshal_user_handle(op.handle);
273 /* user-space handles are aligned to page boundaries, to prevent
275 op.align = max_t(size_t, op.align, PAGE_SIZE);
276 #if defined(CONFIG_NVMAP_FORCE_ZEROED_USER_PAGES)
277 op.flags |= NVMAP_HANDLE_ZEROED_PAGES;
280 err = nvmap_alloc_handle(client, handle, op.heap_mask, op.align,
282 op.flags & (~NVMAP_HANDLE_KIND_SPECIFIED));
283 nvmap_handle_put(handle);
287 int nvmap_ioctl_alloc_kind(struct file *filp, void __user *arg)
289 struct nvmap_alloc_kind_handle op;
290 struct nvmap_client *client = filp->private_data;
291 struct nvmap_handle *handle;
294 if (copy_from_user(&op, arg, sizeof(op)))
297 if (op.align & (op.align - 1))
300 handle = unmarshal_user_handle(op.handle);
304 /* user-space handles are aligned to page boundaries, to prevent
306 op.align = max_t(size_t, op.align, PAGE_SIZE);
307 #if defined(CONFIG_NVMAP_FORCE_ZEROED_USER_PAGES)
308 op.flags |= NVMAP_HANDLE_ZEROED_PAGES;
311 err = nvmap_alloc_handle(client, handle,
316 nvmap_handle_put(handle);
320 int nvmap_create_fd(struct nvmap_handle *h)
324 fd = __nvmap_dmabuf_fd(h->dmabuf, O_CLOEXEC);
327 pr_err("Out of file descriptors");
330 /* __nvmap_dmabuf_fd() associates fd with dma_buf->file *.
331 * fd close drops one ref count on dmabuf->file *.
332 * to balance ref count, ref count dma_buf.
334 get_dma_buf(h->dmabuf);
338 int nvmap_ioctl_create(struct file *filp, unsigned int cmd, void __user *arg)
340 struct nvmap_create_handle op;
341 struct nvmap_handle_ref *ref = NULL;
342 struct nvmap_client *client = filp->private_data;
346 if (copy_from_user(&op, arg, sizeof(op)))
352 if (cmd == NVMAP_IOC_CREATE) {
353 ref = nvmap_create_handle(client, PAGE_ALIGN(op.size));
355 ref->handle->orig_size = op.size;
356 } else if (cmd == NVMAP_IOC_FROM_FD) {
357 ref = nvmap_create_handle_from_fd(client, op.fd);
365 fd = nvmap_create_fd(ref->handle);
371 if (copy_to_user(arg, &op, sizeof(op))) {
373 nvmap_free_handle(client, __nvmap_ref_to_id(ref));
381 int nvmap_map_into_caller_ptr(struct file *filp, void __user *arg, bool is32)
383 struct nvmap_client *client = filp->private_data;
384 struct nvmap_map_caller op;
386 struct nvmap_map_caller_32 op32;
388 struct nvmap_vma_priv *priv;
389 struct vm_area_struct *vma;
390 struct nvmap_handle *h = NULL;
395 if (copy_from_user(&op32, arg, sizeof(op32)))
397 op.handle = op32.handle;
398 op.offset = op32.offset;
399 op.length = op32.length;
400 op.flags = op32.length;
404 if (copy_from_user(&op, arg, sizeof(op)))
407 h = unmarshal_user_handle(op.handle);
416 trace_nvmap_map_into_caller_ptr(client, h, op.offset,
417 op.length, op.flags);
418 down_read(¤t->mm->mmap_sem);
420 vma = find_vma(current->mm, op.addr);
426 if (op.offset & ~PAGE_MASK) {
431 if (op.offset >= h->size || op.length > h->size - op.offset) {
432 err = -EADDRNOTAVAIL;
436 /* the VMA must exactly match the requested mapping operation, and the
437 * VMA that is targetted must have been created by this driver
439 if ((vma->vm_start != op.addr) || !is_nvmap_vma(vma) ||
440 (vma->vm_end-vma->vm_start != op.length)) {
445 /* verify that each mmap() system call creates a unique VMA */
446 if (vma->vm_private_data)
449 if (!h->heap_pgalloc && (h->carveout->base & ~PAGE_MASK)) {
454 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
460 vma->vm_flags |= (h->heap_pgalloc ? 0 : VM_PFNMAP);
462 priv->offs = op.offset;
463 vma->vm_private_data = priv;
464 vma->vm_page_prot = nvmap_pgprot(h, vma->vm_page_prot);
468 up_read(¤t->mm->mmap_sem);
475 int nvmap_ioctl_get_param(struct file *filp, void __user *arg, bool is32)
478 struct nvmap_handle_param_32 __user *uarg32 = arg;
480 struct nvmap_handle_param __user *uarg = arg;
481 struct nvmap_handle_param op;
482 struct nvmap_client *client = filp->private_data;
483 struct nvmap_handle_ref *ref;
484 struct nvmap_handle *h;
489 /* This is safe because the incoming value of result doesn't matter */
491 if (copy_from_user(&op, arg,
492 sizeof(struct nvmap_handle_param_32)))
496 if (copy_from_user(&op, arg, sizeof(op)))
499 h = unmarshal_user_handle(op.handle);
503 nvmap_ref_lock(client);
504 ref = __nvmap_validate_locked(client, h);
505 if (IS_ERR_OR_NULL(ref)) {
506 err = ref ? PTR_ERR(ref) : -EINVAL;
510 err = nvmap_get_handle_param(client, ref, op.param, &result);
514 err = put_user((__u32)result, &uarg32->result);
517 err = put_user((unsigned long)result, &uarg->result);
520 nvmap_ref_unlock(client);
525 int nvmap_ioctl_rw_handle(struct file *filp, int is_read, void __user *arg,
528 struct nvmap_client *client = filp->private_data;
529 struct nvmap_rw_handle __user *uarg = arg;
530 struct nvmap_rw_handle op;
532 struct nvmap_rw_handle_32 __user *uarg32 = arg;
533 struct nvmap_rw_handle_32 op32;
535 struct nvmap_handle *h;
541 if (copy_from_user(&op32, arg, sizeof(op32)))
544 op.handle = op32.handle;
545 op.offset = op32.offset;
546 op.elem_size = op32.elem_size;
547 op.hmem_stride = op32.hmem_stride;
548 op.user_stride = op32.user_stride;
549 op.count = op32.count;
552 if (copy_from_user(&op, arg, sizeof(op)))
555 if (!op.addr || !op.count || !op.elem_size)
558 h = unmarshal_user_handle(op.handle);
563 trace_nvmap_ioctl_rw_handle(client, h, is_read, op.offset,
564 op.addr, op.hmem_stride,
565 op.user_stride, op.elem_size, op.count);
566 copied = rw_handle(client, h, is_read, op.offset,
567 (unsigned long)op.addr, op.hmem_stride,
568 op.user_stride, op.elem_size, op.count);
574 } else if (copied < (op.count * op.elem_size))
579 __put_user(copied, &uarg32->count);
582 __put_user(copied, &uarg->count);
589 static int __nvmap_cache_maint(struct nvmap_client *client,
590 struct nvmap_cache_op *op)
592 struct vm_area_struct *vma;
593 struct nvmap_vma_priv *priv;
594 struct nvmap_handle *handle;
599 if (!op->addr || op->op < NVMAP_CACHE_OP_WB ||
600 op->op > NVMAP_CACHE_OP_WB_INV)
603 handle = unmarshal_user_handle(op->handle);
607 down_read(¤t->mm->mmap_sem);
609 vma = find_vma(current->active_mm, (unsigned long)op->addr);
610 if (!vma || !is_nvmap_vma(vma) ||
611 (ulong)op->addr < vma->vm_start ||
612 (ulong)op->addr >= vma->vm_end ||
613 op->len > vma->vm_end - (ulong)op->addr) {
614 err = -EADDRNOTAVAIL;
618 priv = (struct nvmap_vma_priv *)vma->vm_private_data;
620 if (priv->handle != handle) {
625 start = (unsigned long)op->addr - vma->vm_start +
626 (vma->vm_pgoff << PAGE_SHIFT);
627 end = start + op->len;
629 err = __nvmap_do_cache_maint(client, priv->handle, start, end, op->op,
632 up_read(¤t->mm->mmap_sem);
633 nvmap_handle_put(handle);
637 int nvmap_ioctl_cache_maint(struct file *filp, void __user *arg, bool is32)
639 struct nvmap_client *client = filp->private_data;
640 struct nvmap_cache_op op;
642 struct nvmap_cache_op_32 op32;
647 if (copy_from_user(&op32, arg, sizeof(op32)))
650 op.handle = op32.handle;
655 if (copy_from_user(&op, arg, sizeof(op)))
658 return __nvmap_cache_maint(client, &op);
661 int nvmap_ioctl_free(struct file *filp, unsigned long arg)
663 struct nvmap_client *client = filp->private_data;
668 nvmap_free_handle_user_id(client, arg);
669 return sys_close(arg);
672 static void inner_cache_maint(unsigned int op, void *vaddr, size_t size)
674 if (op == NVMAP_CACHE_OP_WB_INV)
675 dmac_flush_range(vaddr, vaddr + size);
676 else if (op == NVMAP_CACHE_OP_INV)
677 dmac_map_area(vaddr, size, DMA_FROM_DEVICE);
679 dmac_map_area(vaddr, size, DMA_TO_DEVICE);
682 static void outer_cache_maint(unsigned int op, phys_addr_t paddr, size_t size)
684 if (op == NVMAP_CACHE_OP_WB_INV)
685 outer_flush_range(paddr, paddr + size);
686 else if (op == NVMAP_CACHE_OP_INV)
687 outer_inv_range(paddr, paddr + size);
689 outer_clean_range(paddr, paddr + size);
692 static void heap_page_cache_maint(
693 struct nvmap_handle *h, unsigned long start, unsigned long end,
694 unsigned int op, bool inner, bool outer, pte_t **pte,
695 unsigned long kaddr, pgprot_t prot, bool clean_only_dirty)
697 if (h->userflags & NVMAP_HANDLE_CACHE_SYNC) {
699 * zap user VA->PA mappings so that any access to the pages
700 * will result in a fault and can be marked dirty
702 nvmap_handle_mkclean(h, start, end-start);
703 nvmap_zap_handle(h, start, end - start);
706 #ifdef NVMAP_LAZY_VFREE
712 /* mutex lock protection is not necessary as it is
713 * already increased in __nvmap_do_cache_maint to
714 * protect from migrations.
716 nvmap_kmaps_inc_no_lock(h);
717 pages = nvmap_pages(h->pgalloc.pages,
718 h->size >> PAGE_SHIFT);
720 goto per_page_cache_maint;
721 vaddr = vm_map_ram(pages,
722 h->size >> PAGE_SHIFT, -1, prot);
724 (h->size >> PAGE_SHIFT) * sizeof(*pages));
726 if (vaddr && atomic_long_cmpxchg(&h->vaddr, 0, (long)vaddr)) {
728 vm_unmap_ram(vaddr, h->size >> PAGE_SHIFT);
731 /* Fast inner cache maintenance using single mapping */
732 inner_cache_maint(op, h->vaddr + start, end - start);
735 /* Skip per-page inner maintenance in loop below */
739 per_page_cache_maint:
744 while (start < end) {
751 page = nvmap_to_page(h->pgalloc.pages[start >> PAGE_SHIFT]);
752 next = min(((start + PAGE_SIZE) & PAGE_MASK), end);
753 off = start & ~PAGE_MASK;
755 paddr = page_to_phys(page) + off;
758 void *vaddr = (void *)kaddr + off;
761 set_pte_at(&init_mm, kaddr, *pte,
762 pfn_pte(__phys_to_pfn(paddr), prot));
763 nvmap_flush_tlb_kernel_page(kaddr);
764 inner_cache_maint(op, vaddr, size);
768 outer_cache_maint(op, paddr, size);
773 #if defined(CONFIG_NVMAP_OUTER_CACHE_MAINT_BY_SET_WAYS)
774 static bool fast_cache_maint_outer(unsigned long start,
775 unsigned long end, unsigned int op)
778 if (end - start >= cache_maint_outer_threshold) {
779 if (op == NVMAP_CACHE_OP_WB_INV) {
783 if (op == NVMAP_CACHE_OP_WB) {
792 static inline bool fast_cache_maint_outer(unsigned long start,
793 unsigned long end, unsigned int op)
799 #if defined(CONFIG_NVMAP_CACHE_MAINT_BY_SET_WAYS)
800 static inline bool can_fast_cache_maint(struct nvmap_handle *h,
802 unsigned long end, unsigned int op)
804 if ((op == NVMAP_CACHE_OP_INV) ||
805 ((end - start) < cache_maint_inner_threshold))
810 static inline bool can_fast_cache_maint(struct nvmap_handle *h,
812 unsigned long end, unsigned int op)
818 static bool fast_cache_maint(struct nvmap_handle *h,
820 unsigned long end, unsigned int op,
821 bool clean_only_dirty)
823 if (!can_fast_cache_maint(h, start, end, op))
826 if (h->userflags & NVMAP_HANDLE_CACHE_SYNC) {
827 nvmap_handle_mkclean(h, 0, h->size);
828 nvmap_zap_handle(h, 0, h->size);
831 if (op == NVMAP_CACHE_OP_WB_INV)
832 inner_flush_cache_all();
833 else if (op == NVMAP_CACHE_OP_WB)
834 inner_clean_cache_all();
836 /* outer maintenance */
837 if (h->flags != NVMAP_HANDLE_INNER_CACHEABLE) {
838 if(!fast_cache_maint_outer(start, end, op))
840 if (h->heap_pgalloc) {
841 heap_page_cache_maint(h, start,
842 end, op, false, true, NULL, 0, 0,
847 pstart = start + h->carveout->base;
848 outer_cache_maint(op, pstart, end - start);
855 struct cache_maint_op {
859 struct nvmap_handle *h;
862 bool clean_only_dirty;
865 static int do_cache_maint(struct cache_maint_op *cache_work)
870 phys_addr_t pstart = cache_work->start;
871 phys_addr_t pend = cache_work->end;
874 struct nvmap_handle *h = cache_work->h;
875 struct nvmap_client *client;
876 unsigned int op = cache_work->op;
882 if (can_fast_cache_maint(h, pstart, pend, op))
883 nvmap_stats_inc(NS_CFLUSH_DONE, cache_maint_inner_threshold);
885 nvmap_stats_inc(NS_CFLUSH_DONE, pend - pstart);
886 trace_nvmap_cache_maint(client, h, pstart, pend, op, pend - pstart);
887 trace_nvmap_cache_flush(pend - pstart,
888 nvmap_stats_read(NS_ALLOC),
889 nvmap_stats_read(NS_CFLUSH_RQ),
890 nvmap_stats_read(NS_CFLUSH_DONE));
893 if (h->flags == NVMAP_HANDLE_UNCACHEABLE ||
894 h->flags == NVMAP_HANDLE_WRITE_COMBINE || pstart == pend)
897 if (fast_cache_maint(h, pstart, pend, op, cache_work->clean_only_dirty))
900 prot = nvmap_pgprot(h, PG_PROT_KERNEL);
901 pte = nvmap_alloc_pte(h->dev, (void **)&kaddr);
908 if (h->heap_pgalloc) {
909 heap_page_cache_maint(h, pstart, pend, op, true,
910 (h->flags == NVMAP_HANDLE_INNER_CACHEABLE) ?
913 cache_work->clean_only_dirty);
917 if (pstart > h->size || pend > h->size) {
918 pr_warn("cache maintenance outside handle\n");
923 pstart += h->carveout->base;
924 pend += h->carveout->base;
927 while (loop < pend) {
928 phys_addr_t next = (loop + PAGE_SIZE) & PAGE_MASK;
929 void *base = (void *)kaddr + (loop & ~PAGE_MASK);
930 next = min(next, pend);
932 set_pte_at(&init_mm, kaddr, *pte,
933 pfn_pte(__phys_to_pfn(loop), prot));
934 nvmap_flush_tlb_kernel_page(kaddr);
936 inner_cache_maint(op, base, next - loop);
940 if (h->flags != NVMAP_HANDLE_INNER_CACHEABLE)
941 outer_cache_maint(op, pstart, pend - pstart);
945 nvmap_free_pte(h->dev, pte);
949 int __nvmap_do_cache_maint(struct nvmap_client *client,
950 struct nvmap_handle *h,
951 unsigned long start, unsigned long end,
952 unsigned int op, bool clean_only_dirty)
955 struct cache_maint_op cache_op;
957 h = nvmap_handle_get(h);
962 if (op == NVMAP_CACHE_OP_INV)
963 op = NVMAP_CACHE_OP_WB_INV;
965 /* clean only dirty is applicable only for Write Back operation */
966 if (op != NVMAP_CACHE_OP_WB)
967 clean_only_dirty = false;
970 cache_op.start = start;
973 cache_op.inner = h->flags == NVMAP_HANDLE_CACHEABLE ||
974 h->flags == NVMAP_HANDLE_INNER_CACHEABLE;
975 cache_op.outer = h->flags == NVMAP_HANDLE_CACHEABLE;
976 cache_op.clean_only_dirty = clean_only_dirty;
978 nvmap_stats_inc(NS_CFLUSH_RQ, end - start);
979 err = do_cache_maint(&cache_op);
985 static int rw_handle_page(struct nvmap_handle *h, int is_read,
986 unsigned long start, unsigned long rw_addr,
987 unsigned long bytes, unsigned long kaddr, pte_t *pte)
989 pgprot_t prot = nvmap_pgprot(h, PG_PROT_KERNEL);
990 unsigned long end = start + bytes;
993 while (!err && start < end) {
994 struct page *page = NULL;
999 if (!h->heap_pgalloc) {
1000 phys = h->carveout->base + start;
1003 nvmap_to_page(h->pgalloc.pages[start >> PAGE_SHIFT]);
1006 phys = page_to_phys(page) + (start & ~PAGE_MASK);
1009 set_pte_at(&init_mm, kaddr, pte,
1010 pfn_pte(__phys_to_pfn(phys), prot));
1011 nvmap_flush_tlb_kernel_page(kaddr);
1013 src = (void *)kaddr + (phys & ~PAGE_MASK);
1014 phys = PAGE_SIZE - (phys & ~PAGE_MASK);
1015 count = min_t(size_t, end - start, phys);
1018 err = copy_to_user((void *)rw_addr, src, count);
1020 err = copy_from_user(src, (void *)rw_addr, count);
1035 static ssize_t rw_handle(struct nvmap_client *client, struct nvmap_handle *h,
1036 int is_read, unsigned long h_offs,
1037 unsigned long sys_addr, unsigned long h_stride,
1038 unsigned long sys_stride, unsigned long elem_size,
1039 unsigned long count)
1052 if (elem_size == h_stride && elem_size == sys_stride) {
1054 h_stride = elem_size;
1055 sys_stride = elem_size;
1059 pte = nvmap_alloc_pte(nvmap_dev, &addr);
1061 return PTR_ERR(pte);
1064 if (h_offs + elem_size > h->size) {
1065 nvmap_warn(client, "read/write outside of handle\n");
1070 __nvmap_do_cache_maint(client, h, h_offs,
1071 h_offs + elem_size, NVMAP_CACHE_OP_INV, false);
1073 ret = rw_handle_page(h, is_read, h_offs, sys_addr,
1074 elem_size, (unsigned long)addr, *pte);
1080 __nvmap_do_cache_maint(client, h, h_offs,
1081 h_offs + elem_size, NVMAP_CACHE_OP_WB_INV,
1084 copied += elem_size;
1085 sys_addr += sys_stride;
1089 nvmap_free_pte(nvmap_dev, pte);
1090 return ret ?: copied;
1093 int nvmap_ioctl_cache_maint_list(struct file *filp, void __user *arg,
1094 bool is_reserve_ioctl)
1096 struct nvmap_cache_op_list op;
1100 struct nvmap_handle **refs;
1102 u32 i, n_unmarshal_handles = 0;
1104 if (copy_from_user(&op, arg, sizeof(op)))
1110 if (!access_ok(VERIFY_READ, op.handles, op.nr * sizeof(u32)))
1113 if (!access_ok(VERIFY_READ, op.offsets, op.nr * sizeof(u32)))
1116 if (!access_ok(VERIFY_READ, op.sizes, op.nr * sizeof(u32)))
1119 if (!op.offsets || !op.sizes)
1122 refs = kcalloc(op.nr, sizeof(*refs), GFP_KERNEL);
1127 handle_ptr = (u32 *)(uintptr_t)op.handles;
1128 offset_ptr = (u32 *)(uintptr_t)op.offsets;
1129 size_ptr = (u32 *)(uintptr_t)op.sizes;
1131 for (i = 0; i < op.nr; i++) {
1134 if (copy_from_user(&handle, &handle_ptr[i], sizeof(handle))) {
1139 refs[i] = unmarshal_user_handle(handle);
1144 n_unmarshal_handles++;
1147 if (is_reserve_ioctl)
1148 err = nvmap_reserve_pages(refs, offset_ptr, size_ptr,
1151 err = nvmap_do_cache_maint_list(refs, offset_ptr, size_ptr,
1155 for (i = 0; i < n_unmarshal_handles; i++)
1156 nvmap_handle_put(refs[i]);