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 11 static void 12 msm_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 24 void 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 struct msm_gem_address_space * 31 msm_gem_address_space_get(struct msm_gem_address_space *aspace) 32 { 33 if (!IS_ERR_OR_NULL(aspace)) 34 kref_get(&aspace->kref); 35 36 return aspace; 37 } 38 39 /* Actually unmap memory for the vma */ 40 void msm_gem_purge_vma(struct msm_gem_address_space *aspace, 41 struct msm_gem_vma *vma) 42 { 43 unsigned size = vma->node.size << PAGE_SHIFT; 44 45 /* Print a message if we try to purge a vma in use */ 46 if (WARN_ON(vma->inuse > 0)) 47 return; 48 49 /* Don't do anything if the memory isn't mapped */ 50 if (!vma->mapped) 51 return; 52 53 if (aspace->mmu) 54 aspace->mmu->funcs->unmap(aspace->mmu, vma->iova, size); 55 56 vma->mapped = false; 57 } 58 59 /* Remove reference counts for the mapping */ 60 void msm_gem_unmap_vma(struct msm_gem_address_space *aspace, 61 struct msm_gem_vma *vma) 62 { 63 if (!WARN_ON(!vma->iova)) 64 vma->inuse--; 65 } 66 67 int 68 msm_gem_map_vma(struct msm_gem_address_space *aspace, 69 struct msm_gem_vma *vma, int prot, 70 struct sg_table *sgt, int npages) 71 { 72 unsigned size = npages << PAGE_SHIFT; 73 int ret = 0; 74 75 if (WARN_ON(!vma->iova)) 76 return -EINVAL; 77 78 /* Increase the usage counter */ 79 vma->inuse++; 80 81 if (vma->mapped) 82 return 0; 83 84 vma->mapped = true; 85 86 if (aspace && aspace->mmu) 87 ret = aspace->mmu->funcs->map(aspace->mmu, vma->iova, sgt, 88 size, prot); 89 90 if (ret) 91 vma->mapped = false; 92 93 return ret; 94 } 95 96 /* Close an iova. Warn if it is still in use */ 97 void msm_gem_close_vma(struct msm_gem_address_space *aspace, 98 struct msm_gem_vma *vma) 99 { 100 if (WARN_ON(vma->inuse > 0 || vma->mapped)) 101 return; 102 103 spin_lock(&aspace->lock); 104 if (vma->iova) 105 drm_mm_remove_node(&vma->node); 106 spin_unlock(&aspace->lock); 107 108 vma->iova = 0; 109 110 msm_gem_address_space_put(aspace); 111 } 112 113 /* Initialize a new vma and allocate an iova for it */ 114 int msm_gem_init_vma(struct msm_gem_address_space *aspace, 115 struct msm_gem_vma *vma, int npages, 116 u64 range_start, u64 range_end) 117 { 118 int ret; 119 120 if (WARN_ON(vma->iova)) 121 return -EBUSY; 122 123 spin_lock(&aspace->lock); 124 ret = drm_mm_insert_node_in_range(&aspace->mm, &vma->node, npages, 0, 125 0, range_start, range_end, 0); 126 spin_unlock(&aspace->lock); 127 128 if (ret) 129 return ret; 130 131 vma->iova = vma->node.start << PAGE_SHIFT; 132 vma->mapped = false; 133 134 kref_get(&aspace->kref); 135 136 return 0; 137 } 138 139 struct msm_gem_address_space * 140 msm_gem_address_space_create(struct msm_mmu *mmu, const char *name, 141 u64 va_start, u64 size) 142 { 143 struct msm_gem_address_space *aspace; 144 145 if (IS_ERR(mmu)) 146 return ERR_CAST(mmu); 147 148 aspace = kzalloc(sizeof(*aspace), GFP_KERNEL); 149 if (!aspace) 150 return ERR_PTR(-ENOMEM); 151 152 spin_lock_init(&aspace->lock); 153 aspace->name = name; 154 aspace->mmu = mmu; 155 156 drm_mm_init(&aspace->mm, va_start >> PAGE_SHIFT, size >> PAGE_SHIFT); 157 158 kref_init(&aspace->kref); 159 160 return aspace; 161 } 162