Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright (C) 2016 Red Hat
  4 * Author: Rob Clark <robdclark@gmail.com>
 
 
 
 
 
 
 
 
 
 
 
 
  5 */
  6
  7#include "msm_drv.h"
  8#include "msm_gem.h"
  9#include "msm_mmu.h"
 10
 11static void
 12msm_gem_address_space_destroy(struct kref *kref)
 
 13{
 14	struct msm_gem_address_space *aspace = container_of(kref,
 15			struct msm_gem_address_space, kref);
 16
 17	drm_mm_takedown(&aspace->mm);
 18	if (aspace->mmu)
 19		aspace->mmu->funcs->destroy(aspace->mmu);
 20	kfree(aspace);
 21}
 22
 23
 24void msm_gem_address_space_put(struct msm_gem_address_space *aspace)
 25{
 26	if (aspace)
 27		kref_put(&aspace->kref, msm_gem_address_space_destroy);
 28}
 29
 30/* Actually unmap memory for the vma */
 31void msm_gem_purge_vma(struct msm_gem_address_space *aspace,
 32		struct msm_gem_vma *vma)
 33{
 34	unsigned size = vma->node.size << PAGE_SHIFT;
 35
 36	/* Print a message if we try to purge a vma in use */
 37	if (WARN_ON(vma->inuse > 0))
 38		return;
 39
 40	/* Don't do anything if the memory isn't mapped */
 41	if (!vma->mapped)
 42		return;
 43
 44	if (aspace->mmu)
 45		aspace->mmu->funcs->unmap(aspace->mmu, vma->iova, size);
 46
 47	vma->mapped = false;
 48}
 49
 50/* Remove reference counts for the mapping */
 51void msm_gem_unmap_vma(struct msm_gem_address_space *aspace,
 52		struct msm_gem_vma *vma)
 53{
 54	if (!WARN_ON(!vma->iova))
 55		vma->inuse--;
 56}
 57
 58int
 59msm_gem_map_vma(struct msm_gem_address_space *aspace,
 60		struct msm_gem_vma *vma, int prot,
 61		struct sg_table *sgt, int npages)
 62{
 63	unsigned size = npages << PAGE_SHIFT;
 64	int ret = 0;
 65
 66	if (WARN_ON(!vma->iova))
 67		return -EINVAL;
 68
 69	/* Increase the usage counter */
 70	vma->inuse++;
 71
 72	if (vma->mapped)
 73		return 0;
 74
 75	vma->mapped = true;
 76
 77	if (aspace && aspace->mmu)
 78		ret = aspace->mmu->funcs->map(aspace->mmu, vma->iova, sgt,
 79				size, prot);
 80
 81	if (ret)
 82		vma->mapped = false;
 83
 84	return ret;
 85}
 86
 87/* Close an iova.  Warn if it is still in use */
 88void msm_gem_close_vma(struct msm_gem_address_space *aspace,
 89		struct msm_gem_vma *vma)
 90{
 91	if (WARN_ON(vma->inuse > 0 || vma->mapped))
 92		return;
 93
 94	spin_lock(&aspace->lock);
 95	if (vma->iova)
 96		drm_mm_remove_node(&vma->node);
 97	spin_unlock(&aspace->lock);
 98
 99	vma->iova = 0;
 
 
 
 
100
101	msm_gem_address_space_put(aspace);
102}
103
104/* Initialize a new vma and allocate an iova for it */
105int msm_gem_init_vma(struct msm_gem_address_space *aspace,
106		struct msm_gem_vma *vma, int npages,
107		u64 range_start, u64 range_end)
108{
109	int ret;
110
111	if (WARN_ON(vma->iova))
112		return -EBUSY;
113
114	spin_lock(&aspace->lock);
115	ret = drm_mm_insert_node_in_range(&aspace->mm, &vma->node, npages, 0,
116		0, range_start, range_end, 0);
117	spin_unlock(&aspace->lock);
118
119	if (ret)
120		return ret;
121
122	vma->iova = vma->node.start << PAGE_SHIFT;
123	vma->mapped = false;
124
125	kref_get(&aspace->kref);
126
127	return 0;
128}
129
130struct msm_gem_address_space *
131msm_gem_address_space_create(struct msm_mmu *mmu, const char *name,
132		u64 va_start, u64 size)
133{
134	struct msm_gem_address_space *aspace;
135
136	if (IS_ERR(mmu))
137		return ERR_CAST(mmu);
138
139	aspace = kzalloc(sizeof(*aspace), GFP_KERNEL);
140	if (!aspace)
141		return ERR_PTR(-ENOMEM);
142
143	spin_lock_init(&aspace->lock);
144	aspace->name = name;
145	aspace->mmu = mmu;
146
147	drm_mm_init(&aspace->mm, va_start >> PAGE_SHIFT, size >> PAGE_SHIFT);
148
149	kref_init(&aspace->kref);
 
150
151	return aspace;
152}
v4.10.11
 
 1/*
 2 * Copyright (C) 2016 Red Hat
 3 * Author: Rob Clark <robdclark@gmail.com>
 4 *
 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.
 8 *
 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.
13 *
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/>.
16 */
17
18#include "msm_drv.h"
19#include "msm_gem.h"
20#include "msm_mmu.h"
21
22void
23msm_gem_unmap_vma(struct msm_gem_address_space *aspace,
24		struct msm_gem_vma *vma, struct sg_table *sgt)
25{
26	if (!vma->iova)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27		return;
28
29	if (aspace->mmu) {
30		unsigned size = vma->node.size << PAGE_SHIFT;
31		aspace->mmu->funcs->unmap(aspace->mmu, vma->iova, sgt, size);
32	}
 
 
33
34	drm_mm_remove_node(&vma->node);
 
35
36	vma->iova = 0;
 
 
 
 
 
37}
38
39int
40msm_gem_map_vma(struct msm_gem_address_space *aspace,
41		struct msm_gem_vma *vma, struct sg_table *sgt, int npages)
 
42{
43	int ret;
 
 
 
 
 
 
 
44
45	if (WARN_ON(drm_mm_node_allocated(&vma->node)))
46		return 0;
47
48	ret = drm_mm_insert_node(&aspace->mm, &vma->node, npages,
49			0, DRM_MM_SEARCH_DEFAULT);
 
 
 
 
50	if (ret)
51		return ret;
 
 
 
 
 
 
 
 
 
 
52
53	vma->iova = vma->node.start << PAGE_SHIFT;
 
 
 
54
55	if (aspace->mmu) {
56		unsigned size = npages << PAGE_SHIFT;
57		ret = aspace->mmu->funcs->map(aspace->mmu, vma->iova, sgt,
58				size, IOMMU_READ | IOMMU_WRITE);
59	}
60
61	return ret;
62}
63
64void
65msm_gem_address_space_destroy(struct msm_gem_address_space *aspace)
 
 
66{
67	drm_mm_takedown(&aspace->mm);
68	if (aspace->mmu)
69		aspace->mmu->funcs->destroy(aspace->mmu);
70	kfree(aspace);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71}
72
73struct msm_gem_address_space *
74msm_gem_address_space_create(struct device *dev, struct iommu_domain *domain,
75		const char *name)
76{
77	struct msm_gem_address_space *aspace;
78
 
 
 
79	aspace = kzalloc(sizeof(*aspace), GFP_KERNEL);
80	if (!aspace)
81		return ERR_PTR(-ENOMEM);
82
 
83	aspace->name = name;
84	aspace->mmu = msm_iommu_new(dev, domain);
 
 
85
86	drm_mm_init(&aspace->mm, (domain->geometry.aperture_start >> PAGE_SHIFT),
87			(domain->geometry.aperture_end >> PAGE_SHIFT) - 1);
88
89	return aspace;
90}