dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / gpu / drm / msm / msm_gem_vma.c
blob49c04829cf34412ae0b07f6358f596e3b11eae58
1 /*
2 * Copyright (C) 2016 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "msm_drv.h"
19 #include "msm_gem.h"
20 #include "msm_mmu.h"
22 static void
23 msm_gem_address_space_destroy(struct kref *kref)
25 struct msm_gem_address_space *aspace = container_of(kref,
26 struct msm_gem_address_space, kref);
28 drm_mm_takedown(&aspace->mm);
29 if (aspace->mmu)
30 aspace->mmu->funcs->destroy(aspace->mmu);
31 kfree(aspace);
35 void msm_gem_address_space_put(struct msm_gem_address_space *aspace)
37 if (aspace)
38 kref_put(&aspace->kref, msm_gem_address_space_destroy);
41 /* Actually unmap memory for the vma */
42 void msm_gem_purge_vma(struct msm_gem_address_space *aspace,
43 struct msm_gem_vma *vma)
45 unsigned size = vma->node.size << PAGE_SHIFT;
47 /* Print a message if we try to purge a vma in use */
48 if (WARN_ON(vma->inuse > 0))
49 return;
51 /* Don't do anything if the memory isn't mapped */
52 if (!vma->mapped)
53 return;
55 if (aspace->mmu)
56 aspace->mmu->funcs->unmap(aspace->mmu, vma->iova, size);
58 vma->mapped = false;
61 /* Remove reference counts for the mapping */
62 void msm_gem_unmap_vma(struct msm_gem_address_space *aspace,
63 struct msm_gem_vma *vma)
65 if (!WARN_ON(!vma->iova))
66 vma->inuse--;
69 int
70 msm_gem_map_vma(struct msm_gem_address_space *aspace,
71 struct msm_gem_vma *vma, int prot,
72 struct sg_table *sgt, int npages)
74 unsigned size = npages << PAGE_SHIFT;
75 int ret = 0;
77 if (WARN_ON(!vma->iova))
78 return -EINVAL;
80 /* Increase the usage counter */
81 vma->inuse++;
83 if (vma->mapped)
84 return 0;
86 vma->mapped = true;
88 if (aspace->mmu)
89 ret = aspace->mmu->funcs->map(aspace->mmu, vma->iova, sgt,
90 size, prot);
92 if (ret)
93 vma->mapped = false;
95 return ret;
98 /* Close an iova. Warn if it is still in use */
99 void msm_gem_close_vma(struct msm_gem_address_space *aspace,
100 struct msm_gem_vma *vma)
102 if (WARN_ON(vma->inuse > 0 || vma->mapped))
103 return;
105 spin_lock(&aspace->lock);
106 if (vma->iova)
107 drm_mm_remove_node(&vma->node);
108 spin_unlock(&aspace->lock);
110 vma->iova = 0;
112 msm_gem_address_space_put(aspace);
115 /* Initialize a new vma and allocate an iova for it */
116 int msm_gem_init_vma(struct msm_gem_address_space *aspace,
117 struct msm_gem_vma *vma, int npages)
119 int ret;
121 if (WARN_ON(vma->iova))
122 return -EBUSY;
124 spin_lock(&aspace->lock);
125 ret = drm_mm_insert_node(&aspace->mm, &vma->node, npages);
126 spin_unlock(&aspace->lock);
128 if (ret)
129 return ret;
131 vma->iova = vma->node.start << PAGE_SHIFT;
132 vma->mapped = false;
134 kref_get(&aspace->kref);
136 return 0;
140 struct msm_gem_address_space *
141 msm_gem_address_space_create(struct device *dev, struct iommu_domain *domain,
142 const char *name)
144 struct msm_gem_address_space *aspace;
145 u64 size = domain->geometry.aperture_end -
146 domain->geometry.aperture_start;
148 aspace = kzalloc(sizeof(*aspace), GFP_KERNEL);
149 if (!aspace)
150 return ERR_PTR(-ENOMEM);
152 spin_lock_init(&aspace->lock);
153 aspace->name = name;
154 aspace->mmu = msm_iommu_new(dev, domain);
156 drm_mm_init(&aspace->mm, (domain->geometry.aperture_start >> PAGE_SHIFT),
157 size >> PAGE_SHIFT);
159 kref_init(&aspace->kref);
161 return aspace;
164 struct msm_gem_address_space *
165 msm_gem_address_space_create_a2xx(struct device *dev, struct msm_gpu *gpu,
166 const char *name, uint64_t va_start, uint64_t va_end)
168 struct msm_gem_address_space *aspace;
169 u64 size = va_end - va_start;
171 aspace = kzalloc(sizeof(*aspace), GFP_KERNEL);
172 if (!aspace)
173 return ERR_PTR(-ENOMEM);
175 spin_lock_init(&aspace->lock);
176 aspace->name = name;
177 aspace->mmu = msm_gpummu_new(dev, gpu);
179 drm_mm_init(&aspace->mm, (va_start >> PAGE_SHIFT),
180 size >> PAGE_SHIFT);
182 kref_init(&aspace->kref);
184 return aspace;