1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2016 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
12 msm_gem_address_space_destroy(struct kref
*kref
)
14 struct msm_gem_address_space
*aspace
= container_of(kref
,
15 struct msm_gem_address_space
, kref
);
17 drm_mm_takedown(&aspace
->mm
);
19 aspace
->mmu
->funcs
->destroy(aspace
->mmu
);
24 void msm_gem_address_space_put(struct msm_gem_address_space
*aspace
)
27 kref_put(&aspace
->kref
, msm_gem_address_space_destroy
);
30 /* Actually unmap memory for the vma */
31 void msm_gem_purge_vma(struct msm_gem_address_space
*aspace
,
32 struct msm_gem_vma
*vma
)
34 unsigned size
= vma
->node
.size
<< PAGE_SHIFT
;
36 /* Print a message if we try to purge a vma in use */
37 if (WARN_ON(vma
->inuse
> 0))
40 /* Don't do anything if the memory isn't mapped */
45 aspace
->mmu
->funcs
->unmap(aspace
->mmu
, vma
->iova
, size
);
50 /* Remove reference counts for the mapping */
51 void msm_gem_unmap_vma(struct msm_gem_address_space
*aspace
,
52 struct msm_gem_vma
*vma
)
54 if (!WARN_ON(!vma
->iova
))
59 msm_gem_map_vma(struct msm_gem_address_space
*aspace
,
60 struct msm_gem_vma
*vma
, int prot
,
61 struct sg_table
*sgt
, int npages
)
63 unsigned size
= npages
<< PAGE_SHIFT
;
66 if (WARN_ON(!vma
->iova
))
69 /* Increase the usage counter */
77 if (aspace
&& aspace
->mmu
)
78 ret
= aspace
->mmu
->funcs
->map(aspace
->mmu
, vma
->iova
, sgt
,
87 /* Close an iova. Warn if it is still in use */
88 void msm_gem_close_vma(struct msm_gem_address_space
*aspace
,
89 struct msm_gem_vma
*vma
)
91 if (WARN_ON(vma
->inuse
> 0 || vma
->mapped
))
94 spin_lock(&aspace
->lock
);
96 drm_mm_remove_node(&vma
->node
);
97 spin_unlock(&aspace
->lock
);
101 msm_gem_address_space_put(aspace
);
104 /* Initialize a new vma and allocate an iova for it */
105 int msm_gem_init_vma(struct msm_gem_address_space
*aspace
,
106 struct msm_gem_vma
*vma
, int npages
)
110 if (WARN_ON(vma
->iova
))
113 spin_lock(&aspace
->lock
);
114 ret
= drm_mm_insert_node(&aspace
->mm
, &vma
->node
, npages
);
115 spin_unlock(&aspace
->lock
);
120 vma
->iova
= vma
->node
.start
<< PAGE_SHIFT
;
123 kref_get(&aspace
->kref
);
129 struct msm_gem_address_space
*
130 msm_gem_address_space_create(struct device
*dev
, struct iommu_domain
*domain
,
133 struct msm_gem_address_space
*aspace
;
134 u64 size
= domain
->geometry
.aperture_end
-
135 domain
->geometry
.aperture_start
;
137 aspace
= kzalloc(sizeof(*aspace
), GFP_KERNEL
);
139 return ERR_PTR(-ENOMEM
);
141 spin_lock_init(&aspace
->lock
);
143 aspace
->mmu
= msm_iommu_new(dev
, domain
);
145 drm_mm_init(&aspace
->mm
, (domain
->geometry
.aperture_start
>> PAGE_SHIFT
),
148 kref_init(&aspace
->kref
);
153 struct msm_gem_address_space
*
154 msm_gem_address_space_create_a2xx(struct device
*dev
, struct msm_gpu
*gpu
,
155 const char *name
, uint64_t va_start
, uint64_t va_end
)
157 struct msm_gem_address_space
*aspace
;
158 u64 size
= va_end
- va_start
;
160 aspace
= kzalloc(sizeof(*aspace
), GFP_KERNEL
);
162 return ERR_PTR(-ENOMEM
);
164 spin_lock_init(&aspace
->lock
);
166 aspace
->mmu
= msm_gpummu_new(dev
, gpu
);
168 drm_mm_init(&aspace
->mm
, (va_start
>> PAGE_SHIFT
),
171 kref_init(&aspace
->kref
);