xref: /openbmc/linux/drivers/gpu/drm/rockchip/rockchip_drm_gem.c (revision b97d6790d03b763eca08847a9a5869a4291b9f9a)
19c92ab61SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
22048e328SMark Yao /*
32048e328SMark Yao  * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
42048e328SMark Yao  * Author:Mark Yao <mark.yao@rock-chips.com>
52048e328SMark Yao  */
62048e328SMark Yao 
76fd0bfe2SHaixia Shi #include <linux/dma-buf.h>
838f993b7STomasz Figa #include <linux/iommu.h>
99590a99cSKrzysztof Kozlowski #include <linux/vmalloc.h>
102048e328SMark Yao 
11c2156ccdSSam Ravnborg #include <drm/drm.h>
1245b64fd9SThomas Zimmermann #include <drm/drm_fb_helper.h>
13c2156ccdSSam Ravnborg #include <drm/drm_gem.h>
144a83c26aSDanilo Krummrich #include <drm/drm_gem_dma_helper.h>
15c2156ccdSSam Ravnborg #include <drm/drm_prime.h>
16c2156ccdSSam Ravnborg #include <drm/drm_vma_manager.h>
17c2156ccdSSam Ravnborg 
182048e328SMark Yao #include "rockchip_drm_drv.h"
192048e328SMark Yao #include "rockchip_drm_gem.h"
202048e328SMark Yao 
rockchip_gem_iommu_map(struct rockchip_gem_object * rk_obj)2138f993b7STomasz Figa static int rockchip_gem_iommu_map(struct rockchip_gem_object *rk_obj)
2238f993b7STomasz Figa {
2338f993b7STomasz Figa 	struct drm_device *drm = rk_obj->base.dev;
2438f993b7STomasz Figa 	struct rockchip_drm_private *private = drm->dev_private;
2538f993b7STomasz Figa 	int prot = IOMMU_READ | IOMMU_WRITE;
2638f993b7STomasz Figa 	ssize_t ret;
2738f993b7STomasz Figa 
2838f993b7STomasz Figa 	mutex_lock(&private->mm_lock);
2938f993b7STomasz Figa 	ret = drm_mm_insert_node_generic(&private->mm, &rk_obj->mm,
3038f993b7STomasz Figa 					 rk_obj->base.size, PAGE_SIZE,
3138f993b7STomasz Figa 					 0, 0);
3238f993b7STomasz Figa 	mutex_unlock(&private->mm_lock);
332d7b5637SCaesar Wang 
3438f993b7STomasz Figa 	if (ret < 0) {
3538f993b7STomasz Figa 		DRM_ERROR("out of I/O virtual memory: %zd\n", ret);
3638f993b7STomasz Figa 		return ret;
3738f993b7STomasz Figa 	}
3838f993b7STomasz Figa 
3938f993b7STomasz Figa 	rk_obj->dma_addr = rk_obj->mm.start;
4038f993b7STomasz Figa 
4182c245b5SMarek Szyprowski 	ret = iommu_map_sgtable(private->domain, rk_obj->dma_addr, rk_obj->sgt,
4282c245b5SMarek Szyprowski 				prot);
43*40a8d7e4SDan Carpenter 	if (ret < (ssize_t)rk_obj->base.size) {
4438f993b7STomasz Figa 		DRM_ERROR("failed to map buffer: size=%zd request_size=%zd\n",
4538f993b7STomasz Figa 			  ret, rk_obj->base.size);
4638f993b7STomasz Figa 		ret = -ENOMEM;
4738f993b7STomasz Figa 		goto err_remove_node;
4838f993b7STomasz Figa 	}
4938f993b7STomasz Figa 
5038f993b7STomasz Figa 	rk_obj->size = ret;
5138f993b7STomasz Figa 
5238f993b7STomasz Figa 	return 0;
5338f993b7STomasz Figa 
5438f993b7STomasz Figa err_remove_node:
552d7b5637SCaesar Wang 	mutex_lock(&private->mm_lock);
5638f993b7STomasz Figa 	drm_mm_remove_node(&rk_obj->mm);
572d7b5637SCaesar Wang 	mutex_unlock(&private->mm_lock);
5838f993b7STomasz Figa 
5938f993b7STomasz Figa 	return ret;
6038f993b7STomasz Figa }
6138f993b7STomasz Figa 
rockchip_gem_iommu_unmap(struct rockchip_gem_object * rk_obj)6238f993b7STomasz Figa static int rockchip_gem_iommu_unmap(struct rockchip_gem_object *rk_obj)
6338f993b7STomasz Figa {
6438f993b7STomasz Figa 	struct drm_device *drm = rk_obj->base.dev;
6538f993b7STomasz Figa 	struct rockchip_drm_private *private = drm->dev_private;
6638f993b7STomasz Figa 
6738f993b7STomasz Figa 	iommu_unmap(private->domain, rk_obj->dma_addr, rk_obj->size);
6838f993b7STomasz Figa 
6938f993b7STomasz Figa 	mutex_lock(&private->mm_lock);
7038f993b7STomasz Figa 
7138f993b7STomasz Figa 	drm_mm_remove_node(&rk_obj->mm);
7238f993b7STomasz Figa 
7338f993b7STomasz Figa 	mutex_unlock(&private->mm_lock);
7438f993b7STomasz Figa 
7538f993b7STomasz Figa 	return 0;
7638f993b7STomasz Figa }
7738f993b7STomasz Figa 
rockchip_gem_get_pages(struct rockchip_gem_object * rk_obj)7838f993b7STomasz Figa static int rockchip_gem_get_pages(struct rockchip_gem_object *rk_obj)
7938f993b7STomasz Figa {
8038f993b7STomasz Figa 	struct drm_device *drm = rk_obj->base.dev;
8138f993b7STomasz Figa 	int ret, i;
8238f993b7STomasz Figa 	struct scatterlist *s;
8338f993b7STomasz Figa 
8438f993b7STomasz Figa 	rk_obj->pages = drm_gem_get_pages(&rk_obj->base);
8538f993b7STomasz Figa 	if (IS_ERR(rk_obj->pages))
8638f993b7STomasz Figa 		return PTR_ERR(rk_obj->pages);
8738f993b7STomasz Figa 
8838f993b7STomasz Figa 	rk_obj->num_pages = rk_obj->base.size >> PAGE_SHIFT;
8938f993b7STomasz Figa 
90707d561fSGerd Hoffmann 	rk_obj->sgt = drm_prime_pages_to_sg(rk_obj->base.dev,
91707d561fSGerd Hoffmann 					    rk_obj->pages, rk_obj->num_pages);
9238f993b7STomasz Figa 	if (IS_ERR(rk_obj->sgt)) {
9338f993b7STomasz Figa 		ret = PTR_ERR(rk_obj->sgt);
9438f993b7STomasz Figa 		goto err_put_pages;
9538f993b7STomasz Figa 	}
9638f993b7STomasz Figa 
9738f993b7STomasz Figa 	/*
9838f993b7STomasz Figa 	 * Fake up the SG table so that dma_sync_sg_for_device() can be used
9938f993b7STomasz Figa 	 * to flush the pages associated with it.
10038f993b7STomasz Figa 	 *
10138f993b7STomasz Figa 	 * TODO: Replace this by drm_clflush_sg() once it can be implemented
10238f993b7STomasz Figa 	 * without relying on symbols that are not exported.
10338f993b7STomasz Figa 	 */
10482c245b5SMarek Szyprowski 	for_each_sgtable_sg(rk_obj->sgt, s, i)
10538f993b7STomasz Figa 		sg_dma_address(s) = sg_phys(s);
10638f993b7STomasz Figa 
10782c245b5SMarek Szyprowski 	dma_sync_sgtable_for_device(drm->dev, rk_obj->sgt, DMA_TO_DEVICE);
10838f993b7STomasz Figa 
10938f993b7STomasz Figa 	return 0;
11038f993b7STomasz Figa 
11138f993b7STomasz Figa err_put_pages:
11238f993b7STomasz Figa 	drm_gem_put_pages(&rk_obj->base, rk_obj->pages, false, false);
11338f993b7STomasz Figa 	return ret;
11438f993b7STomasz Figa }
11538f993b7STomasz Figa 
rockchip_gem_put_pages(struct rockchip_gem_object * rk_obj)11638f993b7STomasz Figa static void rockchip_gem_put_pages(struct rockchip_gem_object *rk_obj)
11738f993b7STomasz Figa {
11838f993b7STomasz Figa 	sg_free_table(rk_obj->sgt);
11938f993b7STomasz Figa 	kfree(rk_obj->sgt);
12038f993b7STomasz Figa 	drm_gem_put_pages(&rk_obj->base, rk_obj->pages, true, true);
12138f993b7STomasz Figa }
12238f993b7STomasz Figa 
rockchip_gem_alloc_iommu(struct rockchip_gem_object * rk_obj,bool alloc_kmap)12338f993b7STomasz Figa static int rockchip_gem_alloc_iommu(struct rockchip_gem_object *rk_obj,
12438f993b7STomasz Figa 				    bool alloc_kmap)
12538f993b7STomasz Figa {
12638f993b7STomasz Figa 	int ret;
12738f993b7STomasz Figa 
12838f993b7STomasz Figa 	ret = rockchip_gem_get_pages(rk_obj);
12938f993b7STomasz Figa 	if (ret < 0)
13038f993b7STomasz Figa 		return ret;
13138f993b7STomasz Figa 
13238f993b7STomasz Figa 	ret = rockchip_gem_iommu_map(rk_obj);
13338f993b7STomasz Figa 	if (ret < 0)
13438f993b7STomasz Figa 		goto err_free;
13538f993b7STomasz Figa 
13638f993b7STomasz Figa 	if (alloc_kmap) {
13738f993b7STomasz Figa 		rk_obj->kvaddr = vmap(rk_obj->pages, rk_obj->num_pages, VM_MAP,
13838f993b7STomasz Figa 				      pgprot_writecombine(PAGE_KERNEL));
13938f993b7STomasz Figa 		if (!rk_obj->kvaddr) {
14038f993b7STomasz Figa 			DRM_ERROR("failed to vmap() buffer\n");
14138f993b7STomasz Figa 			ret = -ENOMEM;
14238f993b7STomasz Figa 			goto err_unmap;
14338f993b7STomasz Figa 		}
14438f993b7STomasz Figa 	}
14538f993b7STomasz Figa 
14638f993b7STomasz Figa 	return 0;
14738f993b7STomasz Figa 
14838f993b7STomasz Figa err_unmap:
14938f993b7STomasz Figa 	rockchip_gem_iommu_unmap(rk_obj);
15038f993b7STomasz Figa err_free:
15138f993b7STomasz Figa 	rockchip_gem_put_pages(rk_obj);
15238f993b7STomasz Figa 
15338f993b7STomasz Figa 	return ret;
15438f993b7STomasz Figa }
15538f993b7STomasz Figa 
rockchip_gem_alloc_dma(struct rockchip_gem_object * rk_obj,bool alloc_kmap)15638f993b7STomasz Figa static int rockchip_gem_alloc_dma(struct rockchip_gem_object *rk_obj,
157f76c83b5SDaniel Kurtz 				  bool alloc_kmap)
1582048e328SMark Yao {
1592048e328SMark Yao 	struct drm_gem_object *obj = &rk_obj->base;
1602048e328SMark Yao 	struct drm_device *drm = obj->dev;
1612048e328SMark Yao 
16200085f1eSKrzysztof Kozlowski 	rk_obj->dma_attrs = DMA_ATTR_WRITE_COMBINE;
1632048e328SMark Yao 
164f76c83b5SDaniel Kurtz 	if (!alloc_kmap)
16500085f1eSKrzysztof Kozlowski 		rk_obj->dma_attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
166f76c83b5SDaniel Kurtz 
1672048e328SMark Yao 	rk_obj->kvaddr = dma_alloc_attrs(drm->dev, obj->size,
1682048e328SMark Yao 					 &rk_obj->dma_addr, GFP_KERNEL,
16900085f1eSKrzysztof Kozlowski 					 rk_obj->dma_attrs);
1704b9a90c0SDaniel Kurtz 	if (!rk_obj->kvaddr) {
171913bb40aSBrian Norris 		DRM_ERROR("failed to allocate %zu byte dma buffer", obj->size);
1724b9a90c0SDaniel Kurtz 		return -ENOMEM;
1732048e328SMark Yao 	}
1742048e328SMark Yao 
1752048e328SMark Yao 	return 0;
1762048e328SMark Yao }
1772048e328SMark Yao 
rockchip_gem_alloc_buf(struct rockchip_gem_object * rk_obj,bool alloc_kmap)17838f993b7STomasz Figa static int rockchip_gem_alloc_buf(struct rockchip_gem_object *rk_obj,
17938f993b7STomasz Figa 				  bool alloc_kmap)
18038f993b7STomasz Figa {
18138f993b7STomasz Figa 	struct drm_gem_object *obj = &rk_obj->base;
18238f993b7STomasz Figa 	struct drm_device *drm = obj->dev;
18338f993b7STomasz Figa 	struct rockchip_drm_private *private = drm->dev_private;
18438f993b7STomasz Figa 
18538f993b7STomasz Figa 	if (private->domain)
18638f993b7STomasz Figa 		return rockchip_gem_alloc_iommu(rk_obj, alloc_kmap);
18738f993b7STomasz Figa 	else
18838f993b7STomasz Figa 		return rockchip_gem_alloc_dma(rk_obj, alloc_kmap);
18938f993b7STomasz Figa }
19038f993b7STomasz Figa 
rockchip_gem_free_iommu(struct rockchip_gem_object * rk_obj)19138f993b7STomasz Figa static void rockchip_gem_free_iommu(struct rockchip_gem_object *rk_obj)
19238f993b7STomasz Figa {
19338f993b7STomasz Figa 	vunmap(rk_obj->kvaddr);
19438f993b7STomasz Figa 	rockchip_gem_iommu_unmap(rk_obj);
19538f993b7STomasz Figa 	rockchip_gem_put_pages(rk_obj);
19638f993b7STomasz Figa }
19738f993b7STomasz Figa 
rockchip_gem_free_dma(struct rockchip_gem_object * rk_obj)19838f993b7STomasz Figa static void rockchip_gem_free_dma(struct rockchip_gem_object *rk_obj)
1992048e328SMark Yao {
2002048e328SMark Yao 	struct drm_gem_object *obj = &rk_obj->base;
2012048e328SMark Yao 	struct drm_device *drm = obj->dev;
2022048e328SMark Yao 
2032048e328SMark Yao 	dma_free_attrs(drm->dev, obj->size, rk_obj->kvaddr, rk_obj->dma_addr,
20400085f1eSKrzysztof Kozlowski 		       rk_obj->dma_attrs);
2052048e328SMark Yao }
2062048e328SMark Yao 
rockchip_gem_free_buf(struct rockchip_gem_object * rk_obj)20738f993b7STomasz Figa static void rockchip_gem_free_buf(struct rockchip_gem_object *rk_obj)
20841315b79SDaniel Kurtz {
20938f993b7STomasz Figa 	if (rk_obj->pages)
21038f993b7STomasz Figa 		rockchip_gem_free_iommu(rk_obj);
21138f993b7STomasz Figa 	else
21238f993b7STomasz Figa 		rockchip_gem_free_dma(rk_obj);
21338f993b7STomasz Figa }
21438f993b7STomasz Figa 
rockchip_drm_gem_object_mmap_iommu(struct drm_gem_object * obj,struct vm_area_struct * vma)21538f993b7STomasz Figa static int rockchip_drm_gem_object_mmap_iommu(struct drm_gem_object *obj,
21638f993b7STomasz Figa 					      struct vm_area_struct *vma)
21738f993b7STomasz Figa {
21838f993b7STomasz Figa 	struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
2192f69b3c8SSouptick Joarder 	unsigned int count = obj->size >> PAGE_SHIFT;
220f2a44dd0SThomas Meyer 	unsigned long user_count = vma_pages(vma);
22138f993b7STomasz Figa 
22238f993b7STomasz Figa 	if (user_count == 0)
22338f993b7STomasz Figa 		return -ENXIO;
22438f993b7STomasz Figa 
2252f69b3c8SSouptick Joarder 	return vm_map_pages(vma, rk_obj->pages, count);
22638f993b7STomasz Figa }
22738f993b7STomasz Figa 
rockchip_drm_gem_object_mmap_dma(struct drm_gem_object * obj,struct vm_area_struct * vma)22838f993b7STomasz Figa static int rockchip_drm_gem_object_mmap_dma(struct drm_gem_object *obj,
22938f993b7STomasz Figa 					    struct vm_area_struct *vma)
23038f993b7STomasz Figa {
23141315b79SDaniel Kurtz 	struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
23241315b79SDaniel Kurtz 	struct drm_device *drm = obj->dev;
23341315b79SDaniel Kurtz 
23438f993b7STomasz Figa 	return dma_mmap_attrs(drm->dev, vma, rk_obj->kvaddr, rk_obj->dma_addr,
23538f993b7STomasz Figa 			      obj->size, rk_obj->dma_attrs);
23638f993b7STomasz Figa }
23738f993b7STomasz Figa 
rockchip_drm_gem_object_mmap(struct drm_gem_object * obj,struct vm_area_struct * vma)23838f993b7STomasz Figa static int rockchip_drm_gem_object_mmap(struct drm_gem_object *obj,
23938f993b7STomasz Figa 					struct vm_area_struct *vma)
24038f993b7STomasz Figa {
24138f993b7STomasz Figa 	int ret;
24238f993b7STomasz Figa 	struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
24338f993b7STomasz Figa 
24441315b79SDaniel Kurtz 	/*
245f8b53070SThomas Zimmermann 	 * Set vm_pgoff (used as a fake buffer offset by DRM) to 0 and map the
246f8b53070SThomas Zimmermann 	 * whole buffer from the start.
247f8b53070SThomas Zimmermann 	 */
248f8b53070SThomas Zimmermann 	vma->vm_pgoff = 0;
249f8b53070SThomas Zimmermann 
250f8b53070SThomas Zimmermann 	/*
25138f993b7STomasz Figa 	 * We allocated a struct page table for rk_obj, so clear
25241315b79SDaniel Kurtz 	 * VM_PFNMAP flag that was set by drm_gem_mmap_obj()/drm_gem_mmap().
25341315b79SDaniel Kurtz 	 */
2541c71222eSSuren Baghdasaryan 	vm_flags_mod(vma, VM_IO | VM_DONTEXPAND | VM_DONTDUMP, VM_PFNMAP);
25541315b79SDaniel Kurtz 
256f8b53070SThomas Zimmermann 	vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
257f8b53070SThomas Zimmermann 	vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
258f8b53070SThomas Zimmermann 
25938f993b7STomasz Figa 	if (rk_obj->pages)
26038f993b7STomasz Figa 		ret = rockchip_drm_gem_object_mmap_iommu(obj, vma);
26138f993b7STomasz Figa 	else
26238f993b7STomasz Figa 		ret = rockchip_drm_gem_object_mmap_dma(obj, vma);
26338f993b7STomasz Figa 
26441315b79SDaniel Kurtz 	return ret;
26541315b79SDaniel Kurtz }
26641315b79SDaniel Kurtz 
rockchip_gem_release_object(struct rockchip_gem_object * rk_obj)26738f993b7STomasz Figa static void rockchip_gem_release_object(struct rockchip_gem_object *rk_obj)
26838f993b7STomasz Figa {
26938f993b7STomasz Figa 	drm_gem_object_release(&rk_obj->base);
27038f993b7STomasz Figa 	kfree(rk_obj);
27138f993b7STomasz Figa }
27238f993b7STomasz Figa 
2730d590af3SThomas Zimmermann static const struct drm_gem_object_funcs rockchip_gem_object_funcs = {
2740d590af3SThomas Zimmermann 	.free = rockchip_gem_free_object,
2750d590af3SThomas Zimmermann 	.get_sg_table = rockchip_gem_prime_get_sg_table,
2760d590af3SThomas Zimmermann 	.vmap = rockchip_gem_prime_vmap,
2770d590af3SThomas Zimmermann 	.vunmap	= rockchip_gem_prime_vunmap,
278f8b53070SThomas Zimmermann 	.mmap = rockchip_drm_gem_object_mmap,
2794a83c26aSDanilo Krummrich 	.vm_ops = &drm_gem_dma_vm_ops,
2800d590af3SThomas Zimmermann };
2810d590af3SThomas Zimmermann 
282c7337670SBen Dooks static struct rockchip_gem_object *
rockchip_gem_alloc_object(struct drm_device * drm,unsigned int size)2836fd0bfe2SHaixia Shi 	rockchip_gem_alloc_object(struct drm_device *drm, unsigned int size)
2842048e328SMark Yao {
2852048e328SMark Yao 	struct rockchip_gem_object *rk_obj;
2862048e328SMark Yao 	struct drm_gem_object *obj;
2872048e328SMark Yao 
2882048e328SMark Yao 	size = round_up(size, PAGE_SIZE);
2892048e328SMark Yao 
2902048e328SMark Yao 	rk_obj = kzalloc(sizeof(*rk_obj), GFP_KERNEL);
2912048e328SMark Yao 	if (!rk_obj)
2922048e328SMark Yao 		return ERR_PTR(-ENOMEM);
2932048e328SMark Yao 
2942048e328SMark Yao 	obj = &rk_obj->base;
2952048e328SMark Yao 
2960d590af3SThomas Zimmermann 	obj->funcs = &rockchip_gem_object_funcs;
2970d590af3SThomas Zimmermann 
29838f993b7STomasz Figa 	drm_gem_object_init(drm, obj, size);
2992048e328SMark Yao 
3006fd0bfe2SHaixia Shi 	return rk_obj;
3016fd0bfe2SHaixia Shi }
3026fd0bfe2SHaixia Shi 
3036fd0bfe2SHaixia Shi struct rockchip_gem_object *
rockchip_gem_create_object(struct drm_device * drm,unsigned int size,bool alloc_kmap)3046fd0bfe2SHaixia Shi rockchip_gem_create_object(struct drm_device *drm, unsigned int size,
3056fd0bfe2SHaixia Shi 			   bool alloc_kmap)
3066fd0bfe2SHaixia Shi {
3076fd0bfe2SHaixia Shi 	struct rockchip_gem_object *rk_obj;
3086fd0bfe2SHaixia Shi 	int ret;
3096fd0bfe2SHaixia Shi 
3106fd0bfe2SHaixia Shi 	rk_obj = rockchip_gem_alloc_object(drm, size);
3116fd0bfe2SHaixia Shi 	if (IS_ERR(rk_obj))
3126fd0bfe2SHaixia Shi 		return rk_obj;
3136fd0bfe2SHaixia Shi 
314f76c83b5SDaniel Kurtz 	ret = rockchip_gem_alloc_buf(rk_obj, alloc_kmap);
3152048e328SMark Yao 	if (ret)
3162048e328SMark Yao 		goto err_free_rk_obj;
3172048e328SMark Yao 
3182048e328SMark Yao 	return rk_obj;
3192048e328SMark Yao 
3202048e328SMark Yao err_free_rk_obj:
32138f993b7STomasz Figa 	rockchip_gem_release_object(rk_obj);
3222048e328SMark Yao 	return ERR_PTR(ret);
3232048e328SMark Yao }
3242048e328SMark Yao 
3252048e328SMark Yao /*
3260d590af3SThomas Zimmermann  * rockchip_gem_free_object - (struct drm_gem_object_funcs)->free
327fcb1e57fSDaniel Vetter  * callback function
3282048e328SMark Yao  */
rockchip_gem_free_object(struct drm_gem_object * obj)3292048e328SMark Yao void rockchip_gem_free_object(struct drm_gem_object *obj)
3302048e328SMark Yao {
3316fd0bfe2SHaixia Shi 	struct drm_device *drm = obj->dev;
3326fd0bfe2SHaixia Shi 	struct rockchip_drm_private *private = drm->dev_private;
3336fd0bfe2SHaixia Shi 	struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
3342048e328SMark Yao 
3356fd0bfe2SHaixia Shi 	if (obj->import_attach) {
3366fd0bfe2SHaixia Shi 		if (private->domain) {
3376fd0bfe2SHaixia Shi 			rockchip_gem_iommu_unmap(rk_obj);
3386fd0bfe2SHaixia Shi 		} else {
33982c245b5SMarek Szyprowski 			dma_unmap_sgtable(drm->dev, rk_obj->sgt,
34082c245b5SMarek Szyprowski 					  DMA_BIDIRECTIONAL, 0);
3416fd0bfe2SHaixia Shi 		}
3426fd0bfe2SHaixia Shi 		drm_prime_gem_destroy(obj, rk_obj->sgt);
3436fd0bfe2SHaixia Shi 	} else {
3442048e328SMark Yao 		rockchip_gem_free_buf(rk_obj);
3456fd0bfe2SHaixia Shi 	}
3462048e328SMark Yao 
34738f993b7STomasz Figa 	rockchip_gem_release_object(rk_obj);
3482048e328SMark Yao }
3492048e328SMark Yao 
3502048e328SMark Yao /*
3512048e328SMark Yao  * rockchip_gem_create_with_handle - allocate an object with the given
3522048e328SMark Yao  * size and create a gem handle on it
3532048e328SMark Yao  *
3542048e328SMark Yao  * returns a struct rockchip_gem_object* on success or ERR_PTR values
3552048e328SMark Yao  * on failure.
3562048e328SMark Yao  */
3572048e328SMark Yao static struct rockchip_gem_object *
rockchip_gem_create_with_handle(struct drm_file * file_priv,struct drm_device * drm,unsigned int size,unsigned int * handle)3582048e328SMark Yao rockchip_gem_create_with_handle(struct drm_file *file_priv,
3592048e328SMark Yao 				struct drm_device *drm, unsigned int size,
3602048e328SMark Yao 				unsigned int *handle)
3612048e328SMark Yao {
3622048e328SMark Yao 	struct rockchip_gem_object *rk_obj;
3632048e328SMark Yao 	struct drm_gem_object *obj;
364ab78c74cSJohn Keeping 	bool is_framebuffer;
3652048e328SMark Yao 	int ret;
3662048e328SMark Yao 
367ab78c74cSJohn Keeping 	is_framebuffer = drm->fb_helper && file_priv == drm->fb_helper->client.file;
368ab78c74cSJohn Keeping 
369ab78c74cSJohn Keeping 	rk_obj = rockchip_gem_create_object(drm, size, is_framebuffer);
3702048e328SMark Yao 	if (IS_ERR(rk_obj))
3712048e328SMark Yao 		return ERR_CAST(rk_obj);
3722048e328SMark Yao 
3732048e328SMark Yao 	obj = &rk_obj->base;
3742048e328SMark Yao 
3752048e328SMark Yao 	/*
3762048e328SMark Yao 	 * allocate a id of idr table where the obj is registered
3772048e328SMark Yao 	 * and handle has the id what user can see.
3782048e328SMark Yao 	 */
3792048e328SMark Yao 	ret = drm_gem_handle_create(file_priv, obj, handle);
3802048e328SMark Yao 	if (ret)
3812048e328SMark Yao 		goto err_handle_create;
3822048e328SMark Yao 
3832048e328SMark Yao 	/* drop reference from allocate - handle holds it now. */
384b430fe08SEmil Velikov 	drm_gem_object_put(obj);
3852048e328SMark Yao 
3862048e328SMark Yao 	return rk_obj;
3872048e328SMark Yao 
3882048e328SMark Yao err_handle_create:
3892048e328SMark Yao 	rockchip_gem_free_object(obj);
3902048e328SMark Yao 
3912048e328SMark Yao 	return ERR_PTR(ret);
3922048e328SMark Yao }
3932048e328SMark Yao 
3942048e328SMark Yao /*
3952048e328SMark Yao  * rockchip_gem_dumb_create - (struct drm_driver)->dumb_create callback
3962048e328SMark Yao  * function
3972048e328SMark Yao  *
3982048e328SMark Yao  * This aligns the pitch and size arguments to the minimum required. wrap
3992048e328SMark Yao  * this into your own function if you need bigger alignment.
4002048e328SMark Yao  */
rockchip_gem_dumb_create(struct drm_file * file_priv,struct drm_device * dev,struct drm_mode_create_dumb * args)4012048e328SMark Yao int rockchip_gem_dumb_create(struct drm_file *file_priv,
4022048e328SMark Yao 			     struct drm_device *dev,
4032048e328SMark Yao 			     struct drm_mode_create_dumb *args)
4042048e328SMark Yao {
4052048e328SMark Yao 	struct rockchip_gem_object *rk_obj;
4062048e328SMark Yao 	int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
4072048e328SMark Yao 
4082048e328SMark Yao 	/*
4092048e328SMark Yao 	 * align to 64 bytes since Mali requires it.
4102048e328SMark Yao 	 */
411e3c4abdbSMark Yao 	args->pitch = ALIGN(min_pitch, 64);
4122048e328SMark Yao 	args->size = args->pitch * args->height;
4132048e328SMark Yao 
4142048e328SMark Yao 	rk_obj = rockchip_gem_create_with_handle(file_priv, dev, args->size,
4152048e328SMark Yao 						 &args->handle);
4162048e328SMark Yao 
4172048e328SMark Yao 	return PTR_ERR_OR_ZERO(rk_obj);
4182048e328SMark Yao }
4192048e328SMark Yao 
4202048e328SMark Yao /*
4212048e328SMark Yao  * Allocate a sg_table for this GEM object.
4222048e328SMark Yao  * Note: Both the table's contents, and the sg_table itself must be freed by
4232048e328SMark Yao  *       the caller.
4242048e328SMark Yao  * Returns a pointer to the newly allocated sg_table, or an ERR_PTR() error.
4252048e328SMark Yao  */
rockchip_gem_prime_get_sg_table(struct drm_gem_object * obj)4262048e328SMark Yao struct sg_table *rockchip_gem_prime_get_sg_table(struct drm_gem_object *obj)
4272048e328SMark Yao {
4282048e328SMark Yao 	struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
4292048e328SMark Yao 	struct drm_device *drm = obj->dev;
4302048e328SMark Yao 	struct sg_table *sgt;
4312048e328SMark Yao 	int ret;
4322048e328SMark Yao 
43338f993b7STomasz Figa 	if (rk_obj->pages)
434707d561fSGerd Hoffmann 		return drm_prime_pages_to_sg(obj->dev, rk_obj->pages, rk_obj->num_pages);
43538f993b7STomasz Figa 
4362048e328SMark Yao 	sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
4372048e328SMark Yao 	if (!sgt)
4382048e328SMark Yao 		return ERR_PTR(-ENOMEM);
4392048e328SMark Yao 
4402048e328SMark Yao 	ret = dma_get_sgtable_attrs(drm->dev, sgt, rk_obj->kvaddr,
4412048e328SMark Yao 				    rk_obj->dma_addr, obj->size,
44200085f1eSKrzysztof Kozlowski 				    rk_obj->dma_attrs);
4432048e328SMark Yao 	if (ret) {
4442048e328SMark Yao 		DRM_ERROR("failed to allocate sgt, %d\n", ret);
4452048e328SMark Yao 		kfree(sgt);
4462048e328SMark Yao 		return ERR_PTR(ret);
4472048e328SMark Yao 	}
4482048e328SMark Yao 
4492048e328SMark Yao 	return sgt;
4502048e328SMark Yao }
4512048e328SMark Yao 
4526fd0bfe2SHaixia Shi static int
rockchip_gem_iommu_map_sg(struct drm_device * drm,struct dma_buf_attachment * attach,struct sg_table * sg,struct rockchip_gem_object * rk_obj)4536fd0bfe2SHaixia Shi rockchip_gem_iommu_map_sg(struct drm_device *drm,
4546fd0bfe2SHaixia Shi 			  struct dma_buf_attachment *attach,
4556fd0bfe2SHaixia Shi 			  struct sg_table *sg,
4566fd0bfe2SHaixia Shi 			  struct rockchip_gem_object *rk_obj)
4576fd0bfe2SHaixia Shi {
4586fd0bfe2SHaixia Shi 	rk_obj->sgt = sg;
4596fd0bfe2SHaixia Shi 	return rockchip_gem_iommu_map(rk_obj);
4606fd0bfe2SHaixia Shi }
4616fd0bfe2SHaixia Shi 
4626fd0bfe2SHaixia Shi static int
rockchip_gem_dma_map_sg(struct drm_device * drm,struct dma_buf_attachment * attach,struct sg_table * sg,struct rockchip_gem_object * rk_obj)4636fd0bfe2SHaixia Shi rockchip_gem_dma_map_sg(struct drm_device *drm,
4646fd0bfe2SHaixia Shi 			struct dma_buf_attachment *attach,
4656fd0bfe2SHaixia Shi 			struct sg_table *sg,
4666fd0bfe2SHaixia Shi 			struct rockchip_gem_object *rk_obj)
4676fd0bfe2SHaixia Shi {
46882c245b5SMarek Szyprowski 	int err = dma_map_sgtable(drm->dev, sg, DMA_BIDIRECTIONAL, 0);
46982c245b5SMarek Szyprowski 	if (err)
47082c245b5SMarek Szyprowski 		return err;
4716fd0bfe2SHaixia Shi 
4726a8f15c6SMarek Szyprowski 	if (drm_prime_get_contiguous_size(sg) < attach->dmabuf->size) {
4736fd0bfe2SHaixia Shi 		DRM_ERROR("failed to map sg_table to contiguous linear address.\n");
47482c245b5SMarek Szyprowski 		dma_unmap_sgtable(drm->dev, sg, DMA_BIDIRECTIONAL, 0);
4756fd0bfe2SHaixia Shi 		return -EINVAL;
4766fd0bfe2SHaixia Shi 	}
4776fd0bfe2SHaixia Shi 
4786fd0bfe2SHaixia Shi 	rk_obj->dma_addr = sg_dma_address(sg->sgl);
4796fd0bfe2SHaixia Shi 	rk_obj->sgt = sg;
4806fd0bfe2SHaixia Shi 	return 0;
4816fd0bfe2SHaixia Shi }
4826fd0bfe2SHaixia Shi 
4836fd0bfe2SHaixia Shi struct drm_gem_object *
rockchip_gem_prime_import_sg_table(struct drm_device * drm,struct dma_buf_attachment * attach,struct sg_table * sg)4846fd0bfe2SHaixia Shi rockchip_gem_prime_import_sg_table(struct drm_device *drm,
4856fd0bfe2SHaixia Shi 				   struct dma_buf_attachment *attach,
4866fd0bfe2SHaixia Shi 				   struct sg_table *sg)
4876fd0bfe2SHaixia Shi {
4886fd0bfe2SHaixia Shi 	struct rockchip_drm_private *private = drm->dev_private;
4896fd0bfe2SHaixia Shi 	struct rockchip_gem_object *rk_obj;
4906fd0bfe2SHaixia Shi 	int ret;
4916fd0bfe2SHaixia Shi 
4926fd0bfe2SHaixia Shi 	rk_obj = rockchip_gem_alloc_object(drm, attach->dmabuf->size);
4936fd0bfe2SHaixia Shi 	if (IS_ERR(rk_obj))
4946fd0bfe2SHaixia Shi 		return ERR_CAST(rk_obj);
4956fd0bfe2SHaixia Shi 
4966fd0bfe2SHaixia Shi 	if (private->domain)
4976fd0bfe2SHaixia Shi 		ret = rockchip_gem_iommu_map_sg(drm, attach, sg, rk_obj);
4986fd0bfe2SHaixia Shi 	else
4996fd0bfe2SHaixia Shi 		ret = rockchip_gem_dma_map_sg(drm, attach, sg, rk_obj);
5006fd0bfe2SHaixia Shi 
5016fd0bfe2SHaixia Shi 	if (ret < 0) {
5026fd0bfe2SHaixia Shi 		DRM_ERROR("failed to import sg table: %d\n", ret);
5036fd0bfe2SHaixia Shi 		goto err_free_rk_obj;
5046fd0bfe2SHaixia Shi 	}
5056fd0bfe2SHaixia Shi 
5066fd0bfe2SHaixia Shi 	return &rk_obj->base;
5076fd0bfe2SHaixia Shi 
5086fd0bfe2SHaixia Shi err_free_rk_obj:
5096fd0bfe2SHaixia Shi 	rockchip_gem_release_object(rk_obj);
5106fd0bfe2SHaixia Shi 	return ERR_PTR(ret);
5116fd0bfe2SHaixia Shi }
5126fd0bfe2SHaixia Shi 
rockchip_gem_prime_vmap(struct drm_gem_object * obj,struct iosys_map * map)5137938f421SLucas De Marchi int rockchip_gem_prime_vmap(struct drm_gem_object *obj, struct iosys_map *map)
5142048e328SMark Yao {
51538f993b7STomasz Figa 	struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
51638f993b7STomasz Figa 
51738f993b7STomasz Figa 	if (rk_obj->pages) {
5180020d4cfSJohn Keeping 		void *vaddr;
5190020d4cfSJohn Keeping 
5200020d4cfSJohn Keeping 		if (rk_obj->kvaddr)
5210020d4cfSJohn Keeping 			vaddr = rk_obj->kvaddr;
5220020d4cfSJohn Keeping 		else
5230020d4cfSJohn Keeping 			vaddr = vmap(rk_obj->pages, rk_obj->num_pages, VM_MAP,
52449a3f51dSThomas Zimmermann 				     pgprot_writecombine(PAGE_KERNEL));
5250020d4cfSJohn Keeping 
52649a3f51dSThomas Zimmermann 		if (!vaddr)
52749a3f51dSThomas Zimmermann 			return -ENOMEM;
5287938f421SLucas De Marchi 		iosys_map_set_vaddr(map, vaddr);
52949a3f51dSThomas Zimmermann 		return 0;
53049a3f51dSThomas Zimmermann 	}
53149a3f51dSThomas Zimmermann 
53249a3f51dSThomas Zimmermann 	if (rk_obj->dma_attrs & DMA_ATTR_NO_KERNEL_MAPPING)
53349a3f51dSThomas Zimmermann 		return -ENOMEM;
5347938f421SLucas De Marchi 	iosys_map_set_vaddr(map, rk_obj->kvaddr);
53549a3f51dSThomas Zimmermann 
53649a3f51dSThomas Zimmermann 	return 0;
53749a3f51dSThomas Zimmermann }
53849a3f51dSThomas Zimmermann 
rockchip_gem_prime_vunmap(struct drm_gem_object * obj,struct iosys_map * map)5397938f421SLucas De Marchi void rockchip_gem_prime_vunmap(struct drm_gem_object *obj,
5407938f421SLucas De Marchi 			       struct iosys_map *map)
54149a3f51dSThomas Zimmermann {
54249a3f51dSThomas Zimmermann 	struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
54349a3f51dSThomas Zimmermann 
54449a3f51dSThomas Zimmermann 	if (rk_obj->pages) {
5450020d4cfSJohn Keeping 		if (map->vaddr != rk_obj->kvaddr)
54649a3f51dSThomas Zimmermann 			vunmap(map->vaddr);
54738f993b7STomasz Figa 		return;
54838f993b7STomasz Figa 	}
54938f993b7STomasz Figa 
55038f993b7STomasz Figa 	/* Nothing to do if allocated by DMA mapping API. */
5512048e328SMark Yao }
552