Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) Rockchip Electronics Co.Ltd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Author: Felix Zeng <felix.zeng@rock-chips.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/version.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/rk-dma-heap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #if KERNEL_VERSION(5, 10, 0) <= LINUX_VERSION_CODE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/dma-map-ops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include "rknpu_drv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include "rknpu_ioctl.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include "rknpu_mem.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) int rknpu_mem_create_ioctl(struct rknpu_device *rknpu_dev, unsigned long data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	struct rknpu_mem_create args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	int ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	struct dma_buf_attachment *attachment;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	struct sg_table *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	struct scatterlist *sgl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	dma_addr_t phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct dma_buf *dmabuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct page **pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	struct rknpu_mem_object *rknpu_obj = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	int i, fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	unsigned int length, page_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	if (unlikely(copy_from_user(&args, (struct rknpu_mem_create *)data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 				    sizeof(struct rknpu_mem_create)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		LOG_ERROR("%s: copy_from_user failed\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	if (args.flags & RKNPU_MEM_NON_CONTIGUOUS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		LOG_ERROR("%s: malloc iommu memory unsupported in current!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 			  __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	rknpu_obj = kzalloc(sizeof(*rknpu_obj), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	if (!rknpu_obj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	if (args.handle > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		fd = args.handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		dmabuf = dma_buf_get(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		if (IS_ERR(dmabuf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 			ret = PTR_ERR(dmabuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 			goto err_free_obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		rknpu_obj->dmabuf = dmabuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		rknpu_obj->owner = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		/* Start test kernel alloc/free dma buf */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		dmabuf = rk_dma_heap_buffer_alloc(rknpu_dev->heap, args.size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 						  O_CLOEXEC | O_RDWR, 0x0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 						  dev_name(rknpu_dev->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		if (IS_ERR(dmabuf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			ret = PTR_ERR(dmabuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			goto err_free_obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		rknpu_obj->dmabuf = dmabuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		rknpu_obj->owner = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		fd = dma_buf_fd(dmabuf, O_CLOEXEC | O_RDWR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 			ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 			goto err_free_dma_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	attachment = dma_buf_attach(dmabuf, rknpu_dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	if (IS_ERR(attachment)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		ret = PTR_ERR(attachment);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		goto err_free_dma_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	table = dma_buf_map_attachment(attachment, DMA_BIDIRECTIONAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	if (IS_ERR(table)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		dma_buf_detach(dmabuf, attachment);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		ret = PTR_ERR(table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		goto err_free_dma_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	for_each_sgtable_sg(table, sgl, i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		phys = sg_dma_address(sgl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		page = sg_page(sgl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		length = sg_dma_len(sgl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		LOG_DEBUG("%s, %d, phys: %pad, length: %u\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			  __LINE__, &phys, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	page_count = length >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	pages = kmalloc_array(page_count, sizeof(struct page), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (!pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		goto err_detach_dma_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	for (i = 0; i < page_count; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		pages[i] = &page[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	rknpu_obj->kv_addr = vmap(pages, page_count, VM_MAP, PAGE_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	if (!rknpu_obj->kv_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		goto err_free_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	rknpu_obj->size = PAGE_ALIGN(args.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	rknpu_obj->dma_addr = phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	rknpu_obj->sgt = table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	args.size = rknpu_obj->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	args.obj_addr = (__u64)(uintptr_t)rknpu_obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	args.dma_addr = rknpu_obj->dma_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	args.handle = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	LOG_DEBUG(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		"args.handle: %d, args.size: %lld, rknpu_obj: %#llx, rknpu_obj->dma_addr: %#llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		args.handle, args.size, (__u64)(uintptr_t)rknpu_obj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		(__u64)rknpu_obj->dma_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (unlikely(copy_to_user((struct rknpu_mem_create *)data, &args,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 				  sizeof(struct rknpu_mem_create)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		LOG_ERROR("%s: copy_to_user failed\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		goto err_unmap_kv_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	kfree(pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	dma_buf_unmap_attachment(attachment, table, DMA_BIDIRECTIONAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	dma_buf_detach(dmabuf, attachment);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) err_unmap_kv_addr:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	vunmap(rknpu_obj->kv_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	rknpu_obj->kv_addr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) err_free_pages:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	kfree(pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) err_detach_dma_buf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	dma_buf_unmap_attachment(attachment, table, DMA_BIDIRECTIONAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	dma_buf_detach(dmabuf, attachment);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) err_free_dma_buf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (rknpu_obj->owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		rk_dma_heap_buffer_free(dmabuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		dma_buf_put(dmabuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) err_free_obj:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	kfree(rknpu_obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) int rknpu_mem_destroy_ioctl(struct rknpu_device *rknpu_dev, unsigned long data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	struct rknpu_mem_object *rknpu_obj = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	struct rknpu_mem_destroy args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	struct dma_buf *dmabuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	int ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	if (unlikely(copy_from_user(&args, (struct rknpu_mem_destroy *)data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 				    sizeof(struct rknpu_mem_destroy)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		LOG_ERROR("%s: copy_from_user failed\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	if (!kern_addr_valid(args.obj_addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		LOG_ERROR("%s: invalid obj_addr: %#llx\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 			  (__u64)(uintptr_t)args.obj_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	rknpu_obj = (struct rknpu_mem_object *)(uintptr_t)args.obj_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	dmabuf = rknpu_obj->dmabuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	LOG_DEBUG(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		"free args.handle: %d, rknpu_obj: %#llx, rknpu_obj->dma_addr: %#llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		args.handle, (__u64)(uintptr_t)rknpu_obj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		(__u64)rknpu_obj->dma_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	vunmap(rknpu_obj->kv_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	rknpu_obj->kv_addr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	if (!rknpu_obj->owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		dma_buf_put(dmabuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	kfree(rknpu_obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) int rknpu_mem_sync_ioctl(struct rknpu_device *rknpu_dev, unsigned long data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	struct rknpu_mem_object *rknpu_obj = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	struct rknpu_mem_sync args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	struct dma_buf *dmabuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	int ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	if (unlikely(copy_from_user(&args, (struct rknpu_mem_sync *)data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 				    sizeof(struct rknpu_mem_sync)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		LOG_ERROR("%s: copy_from_user failed\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	if (!kern_addr_valid(args.obj_addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		LOG_ERROR("%s: invalid obj_addr: %#llx\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 			  (__u64)(uintptr_t)args.obj_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	rknpu_obj = (struct rknpu_mem_object *)(uintptr_t)args.obj_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	dmabuf = rknpu_obj->dmabuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	if (args.flags & RKNPU_MEM_SYNC_TO_DEVICE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		dmabuf->ops->end_cpu_access_partial(dmabuf, DMA_TO_DEVICE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 						    args.offset, args.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	if (args.flags & RKNPU_MEM_SYNC_FROM_DEVICE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		dmabuf->ops->begin_cpu_access_partial(dmabuf, DMA_FROM_DEVICE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 						      args.offset, args.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }