xref: /openbmc/linux/drivers/gpu/drm/msm/msm_gem_vma.c (revision fc2f0756)
1caab277bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2667ce33eSRob Clark /*
3667ce33eSRob Clark  * Copyright (C) 2016 Red Hat
4667ce33eSRob Clark  * Author: Rob Clark <robdclark@gmail.com>
5667ce33eSRob Clark  */
6667ce33eSRob Clark 
7667ce33eSRob Clark #include "msm_drv.h"
895d1deb0SRob Clark #include "msm_fence.h"
9667ce33eSRob Clark #include "msm_gem.h"
10667ce33eSRob Clark #include "msm_mmu.h"
11667ce33eSRob Clark 
12ee546cd3SJordan Crouse static void
13ee546cd3SJordan Crouse msm_gem_address_space_destroy(struct kref *kref)
14ee546cd3SJordan Crouse {
15ee546cd3SJordan Crouse 	struct msm_gem_address_space *aspace = container_of(kref,
16ee546cd3SJordan Crouse 			struct msm_gem_address_space, kref);
17ee546cd3SJordan Crouse 
18ee546cd3SJordan Crouse 	drm_mm_takedown(&aspace->mm);
19ee546cd3SJordan Crouse 	if (aspace->mmu)
20ee546cd3SJordan Crouse 		aspace->mmu->funcs->destroy(aspace->mmu);
2125faf2f2SRob Clark 	put_pid(aspace->pid);
22ee546cd3SJordan Crouse 	kfree(aspace);
23ee546cd3SJordan Crouse }
24ee546cd3SJordan Crouse 
25ee546cd3SJordan Crouse 
26ee546cd3SJordan Crouse void msm_gem_address_space_put(struct msm_gem_address_space *aspace)
27ee546cd3SJordan Crouse {
28ee546cd3SJordan Crouse 	if (aspace)
29ee546cd3SJordan Crouse 		kref_put(&aspace->kref, msm_gem_address_space_destroy);
30ee546cd3SJordan Crouse }
31ee546cd3SJordan Crouse 
32933415e2SJordan Crouse struct msm_gem_address_space *
33933415e2SJordan Crouse msm_gem_address_space_get(struct msm_gem_address_space *aspace)
34933415e2SJordan Crouse {
35933415e2SJordan Crouse 	if (!IS_ERR_OR_NULL(aspace))
36933415e2SJordan Crouse 		kref_get(&aspace->kref);
37933415e2SJordan Crouse 
38933415e2SJordan Crouse 	return aspace;
39933415e2SJordan Crouse }
40933415e2SJordan Crouse 
41ca35ab2aSRob Clark bool msm_gem_vma_inuse(struct msm_gem_vma *vma)
42ca35ab2aSRob Clark {
4395d1deb0SRob Clark 	if (vma->inuse > 0)
4495d1deb0SRob Clark 		return true;
4595d1deb0SRob Clark 
4695d1deb0SRob Clark 	while (vma->fence_mask) {
4795d1deb0SRob Clark 		unsigned idx = ffs(vma->fence_mask) - 1;
4895d1deb0SRob Clark 
4995d1deb0SRob Clark 		if (!msm_fence_completed(vma->fctx[idx], vma->fence[idx]))
5095d1deb0SRob Clark 			return true;
5195d1deb0SRob Clark 
5295d1deb0SRob Clark 		vma->fence_mask &= ~BIT(idx);
5395d1deb0SRob Clark 	}
5495d1deb0SRob Clark 
5595d1deb0SRob Clark 	return false;
56ca35ab2aSRob Clark }
57ca35ab2aSRob Clark 
587ad0e8cfSJordan Crouse /* Actually unmap memory for the vma */
59*fc2f0756SRob Clark void msm_gem_vma_purge(struct msm_gem_vma *vma)
60667ce33eSRob Clark {
61*fc2f0756SRob Clark 	struct msm_gem_address_space *aspace = vma->aspace;
622ee4b5d2SRob Clark 	unsigned size = vma->node.size;
637ad0e8cfSJordan Crouse 
647ad0e8cfSJordan Crouse 	/* Print a message if we try to purge a vma in use */
65b4d329c4SRob Clark 	GEM_WARN_ON(msm_gem_vma_inuse(vma));
66667ce33eSRob Clark 
677ad0e8cfSJordan Crouse 	/* Don't do anything if the memory isn't mapped */
687ad0e8cfSJordan Crouse 	if (!vma->mapped)
697ad0e8cfSJordan Crouse 		return;
707ad0e8cfSJordan Crouse 
7170dc51b4SJordan Crouse 	aspace->mmu->funcs->unmap(aspace->mmu, vma->iova, size);
727ad0e8cfSJordan Crouse 
737ad0e8cfSJordan Crouse 	vma->mapped = false;
74667ce33eSRob Clark }
75667ce33eSRob Clark 
767ad0e8cfSJordan Crouse /* Remove reference counts for the mapping */
77*fc2f0756SRob Clark void msm_gem_vma_unpin(struct msm_gem_vma *vma)
787ad0e8cfSJordan Crouse {
7995d1deb0SRob Clark 	if (GEM_WARN_ON(!vma->inuse))
8095d1deb0SRob Clark 		return;
818e30fa32SRob Clark 	if (!GEM_WARN_ON(!vma->iova))
827ad0e8cfSJordan Crouse 		vma->inuse--;
83667ce33eSRob Clark }
84667ce33eSRob Clark 
8595d1deb0SRob Clark /* Replace pin reference with fence: */
86*fc2f0756SRob Clark void msm_gem_vma_unpin_fenced(struct msm_gem_vma *vma, struct msm_fence_context *fctx)
8795d1deb0SRob Clark {
8895d1deb0SRob Clark 	vma->fctx[fctx->index] = fctx;
8995d1deb0SRob Clark 	vma->fence[fctx->index] = fctx->last_fence;
9095d1deb0SRob Clark 	vma->fence_mask |= BIT(fctx->index);
91*fc2f0756SRob Clark 	msm_gem_vma_unpin(vma);
9295d1deb0SRob Clark }
9395d1deb0SRob Clark 
9495d1deb0SRob Clark /* Map and pin vma: */
95667ce33eSRob Clark int
96*fc2f0756SRob Clark msm_gem_vma_map(struct msm_gem_vma *vma, int prot,
972ee4b5d2SRob Clark 		struct sg_table *sgt, int size)
98667ce33eSRob Clark {
99*fc2f0756SRob Clark 	struct msm_gem_address_space *aspace = vma->aspace;
100*fc2f0756SRob Clark 	int ret;
101667ce33eSRob Clark 
1028e30fa32SRob Clark 	if (GEM_WARN_ON(!vma->iova))
103c0ee9794SJordan Crouse 		return -EINVAL;
104c0ee9794SJordan Crouse 
1057ad0e8cfSJordan Crouse 	/* Increase the usage counter */
1067ad0e8cfSJordan Crouse 	vma->inuse++;
1077ad0e8cfSJordan Crouse 
108c0ee9794SJordan Crouse 	if (vma->mapped)
109667ce33eSRob Clark 		return 0;
110c0ee9794SJordan Crouse 
111c0ee9794SJordan Crouse 	vma->mapped = true;
112c0ee9794SJordan Crouse 
113*fc2f0756SRob Clark 	if (!aspace)
114*fc2f0756SRob Clark 		return 0;
115*fc2f0756SRob Clark 
116*fc2f0756SRob Clark 	ret = aspace->mmu->funcs->map(aspace->mmu, vma->iova, sgt, size, prot);
117c0ee9794SJordan Crouse 
11863ca94adSAkhil P Oommen 	if (ret) {
119c0ee9794SJordan Crouse 		vma->mapped = false;
12063ca94adSAkhil P Oommen 		vma->inuse--;
12163ca94adSAkhil P Oommen 	}
122c0ee9794SJordan Crouse 
123c0ee9794SJordan Crouse 	return ret;
1240e08270aSSushmita Susheelendra }
125667ce33eSRob Clark 
1267ad0e8cfSJordan Crouse /* Close an iova.  Warn if it is still in use */
127*fc2f0756SRob Clark void msm_gem_vma_close(struct msm_gem_vma *vma)
1287ad0e8cfSJordan Crouse {
129*fc2f0756SRob Clark 	struct msm_gem_address_space *aspace = vma->aspace;
130*fc2f0756SRob Clark 
131b4d329c4SRob Clark 	GEM_WARN_ON(msm_gem_vma_inuse(vma) || vma->mapped);
1327ad0e8cfSJordan Crouse 
1337ad0e8cfSJordan Crouse 	spin_lock(&aspace->lock);
1347ad0e8cfSJordan Crouse 	if (vma->iova)
1357ad0e8cfSJordan Crouse 		drm_mm_remove_node(&vma->node);
1367ad0e8cfSJordan Crouse 	spin_unlock(&aspace->lock);
1377ad0e8cfSJordan Crouse 
1387ad0e8cfSJordan Crouse 	vma->iova = 0;
1397ad0e8cfSJordan Crouse 
1407ad0e8cfSJordan Crouse 	msm_gem_address_space_put(aspace);
1417ad0e8cfSJordan Crouse }
1427ad0e8cfSJordan Crouse 
143*fc2f0756SRob Clark struct msm_gem_vma *msm_gem_vma_new(struct msm_gem_address_space *aspace)
144*fc2f0756SRob Clark {
145*fc2f0756SRob Clark 	struct msm_gem_vma *vma;
146*fc2f0756SRob Clark 
147*fc2f0756SRob Clark 	vma = kzalloc(sizeof(*vma), GFP_KERNEL);
148*fc2f0756SRob Clark 	if (!vma)
149*fc2f0756SRob Clark 		return NULL;
150*fc2f0756SRob Clark 
151*fc2f0756SRob Clark 	vma->aspace = aspace;
152*fc2f0756SRob Clark 
153*fc2f0756SRob Clark 	return vma;
154*fc2f0756SRob Clark }
155*fc2f0756SRob Clark 
156c0ee9794SJordan Crouse /* Initialize a new vma and allocate an iova for it */
157*fc2f0756SRob Clark int msm_gem_vma_init(struct msm_gem_vma *vma, int size,
158d3b8877eSJonathan Marek 		u64 range_start, u64 range_end)
159c0ee9794SJordan Crouse {
160*fc2f0756SRob Clark 	struct msm_gem_address_space *aspace = vma->aspace;
161c0ee9794SJordan Crouse 	int ret;
162c0ee9794SJordan Crouse 
163*fc2f0756SRob Clark 	if (GEM_WARN_ON(!aspace))
164*fc2f0756SRob Clark 		return -EINVAL;
165*fc2f0756SRob Clark 
1668e30fa32SRob Clark 	if (GEM_WARN_ON(vma->iova))
167c0ee9794SJordan Crouse 		return -EBUSY;
168c0ee9794SJordan Crouse 
169c0ee9794SJordan Crouse 	spin_lock(&aspace->lock);
1702ee4b5d2SRob Clark 	ret = drm_mm_insert_node_in_range(&aspace->mm, &vma->node,
1712ee4b5d2SRob Clark 					  size, PAGE_SIZE, 0,
1722ee4b5d2SRob Clark 					  range_start, range_end, 0);
1730e08270aSSushmita Susheelendra 	spin_unlock(&aspace->lock);
1740e08270aSSushmita Susheelendra 
175667ce33eSRob Clark 	if (ret)
176667ce33eSRob Clark 		return ret;
177667ce33eSRob Clark 
1782ee4b5d2SRob Clark 	vma->iova = vma->node.start;
179c0ee9794SJordan Crouse 	vma->mapped = false;
180667ce33eSRob Clark 
181ee546cd3SJordan Crouse 	kref_get(&aspace->kref);
182667ce33eSRob Clark 
183c0ee9794SJordan Crouse 	return 0;
184667ce33eSRob Clark }
185667ce33eSRob Clark 
186667ce33eSRob Clark struct msm_gem_address_space *
187ccac7ce3SJordan Crouse msm_gem_address_space_create(struct msm_mmu *mmu, const char *name,
188ccac7ce3SJordan Crouse 		u64 va_start, u64 size)
189667ce33eSRob Clark {
190667ce33eSRob Clark 	struct msm_gem_address_space *aspace;
191ccac7ce3SJordan Crouse 
192ccac7ce3SJordan Crouse 	if (IS_ERR(mmu))
193ccac7ce3SJordan Crouse 		return ERR_CAST(mmu);
194667ce33eSRob Clark 
195667ce33eSRob Clark 	aspace = kzalloc(sizeof(*aspace), GFP_KERNEL);
196667ce33eSRob Clark 	if (!aspace)
197667ce33eSRob Clark 		return ERR_PTR(-ENOMEM);
198667ce33eSRob Clark 
1990e08270aSSushmita Susheelendra 	spin_lock_init(&aspace->lock);
200667ce33eSRob Clark 	aspace->name = name;
201ccac7ce3SJordan Crouse 	aspace->mmu = mmu;
202a636a0ffSRob Clark 	aspace->va_start = va_start;
203a636a0ffSRob Clark 	aspace->va_size  = size;
204667ce33eSRob Clark 
2052ee4b5d2SRob Clark 	drm_mm_init(&aspace->mm, va_start, size);
206c2052a4eSJonathan Marek 
207c2052a4eSJonathan Marek 	kref_init(&aspace->kref);
208c2052a4eSJonathan Marek 
209c2052a4eSJonathan Marek 	return aspace;
210c2052a4eSJonathan Marek }
211