1 // SPDX-License-Identifier: GPL-2.0+ 2 /* Copyright (C) 2015-2018 Broadcom */ 3 4 /** 5 * DOC: V3D GEM BO management support 6 * 7 * Compared to VC4 (V3D 2.x), V3D 3.3 introduces an MMU between the 8 * GPU and the bus, allowing us to use shmem objects for our storage 9 * instead of CMA. 10 * 11 * Physically contiguous objects may still be imported to V3D, but the 12 * driver doesn't allocate physically contiguous objects on its own. 13 * Display engines requiring physically contiguous allocations should 14 * look into Mesa's "renderonly" support (as used by the Mesa pl111 15 * driver) for an example of how to integrate with V3D. 16 * 17 * Long term, we should support evicting pages from the MMU when under 18 * memory pressure (thus the v3d_bo_get_pages() refcounting), but 19 * that's not a high priority since our systems tend to not have swap. 20 */ 21 22 #include <linux/dma-buf.h> 23 #include <linux/pfn_t.h> 24 #include <linux/vmalloc.h> 25 26 #include "v3d_drv.h" 27 #include "uapi/drm/v3d_drm.h" 28 29 /* Called DRM core on the last userspace/kernel unreference of the 30 * BO. 31 */ 32 void v3d_free_object(struct drm_gem_object *obj) 33 { 34 struct v3d_dev *v3d = to_v3d_dev(obj->dev); 35 struct v3d_bo *bo = to_v3d_bo(obj); 36 37 if (bo->vaddr) 38 v3d_put_bo_vaddr(bo); 39 40 v3d_mmu_remove_ptes(bo); 41 42 mutex_lock(&v3d->bo_lock); 43 v3d->bo_stats.num_allocated--; 44 v3d->bo_stats.pages_allocated -= obj->size >> V3D_MMU_PAGE_SHIFT; 45 mutex_unlock(&v3d->bo_lock); 46 47 spin_lock(&v3d->mm_lock); 48 drm_mm_remove_node(&bo->node); 49 spin_unlock(&v3d->mm_lock); 50 51 /* GPU execution may have dirtied any pages in the BO. */ 52 bo->base.pages_mark_dirty_on_put = true; 53 54 drm_gem_shmem_free(&bo->base); 55 } 56 57 static const struct drm_gem_object_funcs v3d_gem_funcs = { 58 .free = v3d_free_object, 59 .print_info = drm_gem_shmem_object_print_info, 60 .pin = drm_gem_shmem_object_pin, 61 .unpin = drm_gem_shmem_object_unpin, 62 .get_sg_table = drm_gem_shmem_object_get_sg_table, 63 .vmap = drm_gem_shmem_object_vmap, 64 .vunmap = drm_gem_shmem_object_vunmap, 65 .mmap = drm_gem_shmem_object_mmap, 66 .vm_ops = &drm_gem_shmem_vm_ops, 67 }; 68 69 /* gem_create_object function for allocating a BO struct and doing 70 * early setup. 71 */ 72 struct drm_gem_object *v3d_create_object(struct drm_device *dev, size_t size) 73 { 74 struct v3d_bo *bo; 75 struct drm_gem_object *obj; 76 77 if (size == 0) 78 return ERR_PTR(-EINVAL); 79 80 bo = kzalloc(sizeof(*bo), GFP_KERNEL); 81 if (!bo) 82 return ERR_PTR(-ENOMEM); 83 obj = &bo->base.base; 84 85 obj->funcs = &v3d_gem_funcs; 86 bo->base.map_wc = true; 87 INIT_LIST_HEAD(&bo->unref_head); 88 89 return &bo->base.base; 90 } 91 92 static int 93 v3d_bo_create_finish(struct drm_gem_object *obj) 94 { 95 struct v3d_dev *v3d = to_v3d_dev(obj->dev); 96 struct v3d_bo *bo = to_v3d_bo(obj); 97 struct sg_table *sgt; 98 int ret; 99 100 /* So far we pin the BO in the MMU for its lifetime, so use 101 * shmem's helper for getting a lifetime sgt. 102 */ 103 sgt = drm_gem_shmem_get_pages_sgt(&bo->base); 104 if (IS_ERR(sgt)) 105 return PTR_ERR(sgt); 106 107 spin_lock(&v3d->mm_lock); 108 /* Allocate the object's space in the GPU's page tables. 109 * Inserting PTEs will happen later, but the offset is for the 110 * lifetime of the BO. 111 */ 112 ret = drm_mm_insert_node_generic(&v3d->mm, &bo->node, 113 obj->size >> V3D_MMU_PAGE_SHIFT, 114 GMP_GRANULARITY >> V3D_MMU_PAGE_SHIFT, 0, 0); 115 spin_unlock(&v3d->mm_lock); 116 if (ret) 117 return ret; 118 119 /* Track stats for /debug/dri/n/bo_stats. */ 120 mutex_lock(&v3d->bo_lock); 121 v3d->bo_stats.num_allocated++; 122 v3d->bo_stats.pages_allocated += obj->size >> V3D_MMU_PAGE_SHIFT; 123 mutex_unlock(&v3d->bo_lock); 124 125 v3d_mmu_insert_ptes(bo); 126 127 return 0; 128 } 129 130 struct v3d_bo *v3d_bo_create(struct drm_device *dev, struct drm_file *file_priv, 131 size_t unaligned_size) 132 { 133 struct drm_gem_shmem_object *shmem_obj; 134 struct v3d_bo *bo; 135 int ret; 136 137 shmem_obj = drm_gem_shmem_create(dev, unaligned_size); 138 if (IS_ERR(shmem_obj)) 139 return ERR_CAST(shmem_obj); 140 bo = to_v3d_bo(&shmem_obj->base); 141 bo->vaddr = NULL; 142 143 ret = v3d_bo_create_finish(&shmem_obj->base); 144 if (ret) 145 goto free_obj; 146 147 return bo; 148 149 free_obj: 150 drm_gem_shmem_free(shmem_obj); 151 return ERR_PTR(ret); 152 } 153 154 struct drm_gem_object * 155 v3d_prime_import_sg_table(struct drm_device *dev, 156 struct dma_buf_attachment *attach, 157 struct sg_table *sgt) 158 { 159 struct drm_gem_object *obj; 160 int ret; 161 162 obj = drm_gem_shmem_prime_import_sg_table(dev, attach, sgt); 163 if (IS_ERR(obj)) 164 return obj; 165 166 ret = v3d_bo_create_finish(obj); 167 if (ret) { 168 drm_gem_shmem_free(&to_v3d_bo(obj)->base); 169 return ERR_PTR(ret); 170 } 171 172 return obj; 173 } 174 175 void v3d_get_bo_vaddr(struct v3d_bo *bo) 176 { 177 struct drm_gem_shmem_object *obj = &bo->base; 178 179 bo->vaddr = vmap(obj->pages, obj->base.size >> PAGE_SHIFT, VM_MAP, 180 pgprot_writecombine(PAGE_KERNEL)); 181 } 182 183 void v3d_put_bo_vaddr(struct v3d_bo *bo) 184 { 185 vunmap(bo->vaddr); 186 bo->vaddr = NULL; 187 } 188 189 int v3d_create_bo_ioctl(struct drm_device *dev, void *data, 190 struct drm_file *file_priv) 191 { 192 struct drm_v3d_create_bo *args = data; 193 struct v3d_bo *bo = NULL; 194 int ret; 195 196 if (args->flags != 0) { 197 DRM_INFO("unknown create_bo flags: %d\n", args->flags); 198 return -EINVAL; 199 } 200 201 bo = v3d_bo_create(dev, file_priv, PAGE_ALIGN(args->size)); 202 if (IS_ERR(bo)) 203 return PTR_ERR(bo); 204 205 args->offset = bo->node.start << V3D_MMU_PAGE_SHIFT; 206 207 ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle); 208 drm_gem_object_put(&bo->base.base); 209 210 return ret; 211 } 212 213 int v3d_mmap_bo_ioctl(struct drm_device *dev, void *data, 214 struct drm_file *file_priv) 215 { 216 struct drm_v3d_mmap_bo *args = data; 217 struct drm_gem_object *gem_obj; 218 219 if (args->flags != 0) { 220 DRM_INFO("unknown mmap_bo flags: %d\n", args->flags); 221 return -EINVAL; 222 } 223 224 gem_obj = drm_gem_object_lookup(file_priv, args->handle); 225 if (!gem_obj) { 226 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle); 227 return -ENOENT; 228 } 229 230 args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node); 231 drm_gem_object_put(gem_obj); 232 233 return 0; 234 } 235 236 int v3d_get_bo_offset_ioctl(struct drm_device *dev, void *data, 237 struct drm_file *file_priv) 238 { 239 struct drm_v3d_get_bo_offset *args = data; 240 struct drm_gem_object *gem_obj; 241 struct v3d_bo *bo; 242 243 gem_obj = drm_gem_object_lookup(file_priv, args->handle); 244 if (!gem_obj) { 245 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle); 246 return -ENOENT; 247 } 248 bo = to_v3d_bo(gem_obj); 249 250 args->offset = bo->node.start << V3D_MMU_PAGE_SHIFT; 251 252 drm_gem_object_put(gem_obj); 253 return 0; 254 } 255 256 int 257 v3d_wait_bo_ioctl(struct drm_device *dev, void *data, 258 struct drm_file *file_priv) 259 { 260 int ret; 261 struct drm_v3d_wait_bo *args = data; 262 ktime_t start = ktime_get(); 263 u64 delta_ns; 264 unsigned long timeout_jiffies = 265 nsecs_to_jiffies_timeout(args->timeout_ns); 266 267 if (args->pad != 0) 268 return -EINVAL; 269 270 ret = drm_gem_dma_resv_wait(file_priv, args->handle, 271 true, timeout_jiffies); 272 273 /* Decrement the user's timeout, in case we got interrupted 274 * such that the ioctl will be restarted. 275 */ 276 delta_ns = ktime_to_ns(ktime_sub(ktime_get(), start)); 277 if (delta_ns < args->timeout_ns) 278 args->timeout_ns -= delta_ns; 279 else 280 args->timeout_ns = 0; 281 282 /* Asked to wait beyond the jiffy/scheduler precision? */ 283 if (ret == -ETIME && args->timeout_ns) 284 ret = -EAGAIN; 285 286 return ret; 287 } 288