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 static enum drm_gem_object_status v3d_gem_status(struct drm_gem_object *obj) 30 { 31 struct v3d_bo *bo = to_v3d_bo(obj); 32 enum drm_gem_object_status res = 0; 33 34 if (bo->base.pages) 35 res |= DRM_GEM_OBJECT_RESIDENT; 36 37 return res; 38 } 39 40 /* Called DRM core on the last userspace/kernel unreference of the 41 * BO. 42 */ 43 void v3d_free_object(struct drm_gem_object *obj) 44 { 45 struct v3d_dev *v3d = to_v3d_dev(obj->dev); 46 struct v3d_bo *bo = to_v3d_bo(obj); 47 48 if (bo->vaddr) 49 v3d_put_bo_vaddr(bo); 50 51 v3d_mmu_remove_ptes(bo); 52 53 mutex_lock(&v3d->bo_lock); 54 v3d->bo_stats.num_allocated--; 55 v3d->bo_stats.pages_allocated -= obj->size >> V3D_MMU_PAGE_SHIFT; 56 mutex_unlock(&v3d->bo_lock); 57 58 spin_lock(&v3d->mm_lock); 59 drm_mm_remove_node(&bo->node); 60 spin_unlock(&v3d->mm_lock); 61 62 /* GPU execution may have dirtied any pages in the BO. */ 63 bo->base.pages_mark_dirty_on_put = true; 64 65 drm_gem_shmem_free(&bo->base); 66 } 67 68 static const struct drm_gem_object_funcs v3d_gem_funcs = { 69 .free = v3d_free_object, 70 .print_info = drm_gem_shmem_object_print_info, 71 .pin = drm_gem_shmem_object_pin, 72 .unpin = drm_gem_shmem_object_unpin, 73 .get_sg_table = drm_gem_shmem_object_get_sg_table, 74 .vmap = drm_gem_shmem_object_vmap, 75 .vunmap = drm_gem_shmem_object_vunmap, 76 .mmap = drm_gem_shmem_object_mmap, 77 .status = v3d_gem_status, 78 .vm_ops = &drm_gem_shmem_vm_ops, 79 }; 80 81 /* gem_create_object function for allocating a BO struct and doing 82 * early setup. 83 */ 84 struct drm_gem_object *v3d_create_object(struct drm_device *dev, size_t size) 85 { 86 struct v3d_bo *bo; 87 struct drm_gem_object *obj; 88 89 if (size == 0) 90 return ERR_PTR(-EINVAL); 91 92 bo = kzalloc(sizeof(*bo), GFP_KERNEL); 93 if (!bo) 94 return ERR_PTR(-ENOMEM); 95 obj = &bo->base.base; 96 97 obj->funcs = &v3d_gem_funcs; 98 bo->base.map_wc = true; 99 INIT_LIST_HEAD(&bo->unref_head); 100 101 return &bo->base.base; 102 } 103 104 static int 105 v3d_bo_create_finish(struct drm_gem_object *obj) 106 { 107 struct v3d_dev *v3d = to_v3d_dev(obj->dev); 108 struct v3d_bo *bo = to_v3d_bo(obj); 109 struct sg_table *sgt; 110 int ret; 111 112 /* So far we pin the BO in the MMU for its lifetime, so use 113 * shmem's helper for getting a lifetime sgt. 114 */ 115 sgt = drm_gem_shmem_get_pages_sgt(&bo->base); 116 if (IS_ERR(sgt)) 117 return PTR_ERR(sgt); 118 119 spin_lock(&v3d->mm_lock); 120 /* Allocate the object's space in the GPU's page tables. 121 * Inserting PTEs will happen later, but the offset is for the 122 * lifetime of the BO. 123 */ 124 ret = drm_mm_insert_node_generic(&v3d->mm, &bo->node, 125 obj->size >> V3D_MMU_PAGE_SHIFT, 126 GMP_GRANULARITY >> V3D_MMU_PAGE_SHIFT, 0, 0); 127 spin_unlock(&v3d->mm_lock); 128 if (ret) 129 return ret; 130 131 /* Track stats for /debug/dri/n/bo_stats. */ 132 mutex_lock(&v3d->bo_lock); 133 v3d->bo_stats.num_allocated++; 134 v3d->bo_stats.pages_allocated += obj->size >> V3D_MMU_PAGE_SHIFT; 135 mutex_unlock(&v3d->bo_lock); 136 137 v3d_mmu_insert_ptes(bo); 138 139 return 0; 140 } 141 142 struct v3d_bo *v3d_bo_create(struct drm_device *dev, struct drm_file *file_priv, 143 size_t unaligned_size) 144 { 145 struct drm_gem_shmem_object *shmem_obj; 146 struct v3d_bo *bo; 147 int ret; 148 149 shmem_obj = drm_gem_shmem_create(dev, unaligned_size); 150 if (IS_ERR(shmem_obj)) 151 return ERR_CAST(shmem_obj); 152 bo = to_v3d_bo(&shmem_obj->base); 153 bo->vaddr = NULL; 154 155 ret = v3d_bo_create_finish(&shmem_obj->base); 156 if (ret) 157 goto free_obj; 158 159 return bo; 160 161 free_obj: 162 drm_gem_shmem_free(shmem_obj); 163 return ERR_PTR(ret); 164 } 165 166 struct drm_gem_object * 167 v3d_prime_import_sg_table(struct drm_device *dev, 168 struct dma_buf_attachment *attach, 169 struct sg_table *sgt) 170 { 171 struct drm_gem_object *obj; 172 int ret; 173 174 obj = drm_gem_shmem_prime_import_sg_table(dev, attach, sgt); 175 if (IS_ERR(obj)) 176 return obj; 177 178 ret = v3d_bo_create_finish(obj); 179 if (ret) { 180 drm_gem_shmem_free(&to_v3d_bo(obj)->base); 181 return ERR_PTR(ret); 182 } 183 184 return obj; 185 } 186 187 void v3d_get_bo_vaddr(struct v3d_bo *bo) 188 { 189 struct drm_gem_shmem_object *obj = &bo->base; 190 191 bo->vaddr = vmap(obj->pages, obj->base.size >> PAGE_SHIFT, VM_MAP, 192 pgprot_writecombine(PAGE_KERNEL)); 193 } 194 195 void v3d_put_bo_vaddr(struct v3d_bo *bo) 196 { 197 vunmap(bo->vaddr); 198 bo->vaddr = NULL; 199 } 200 201 int v3d_create_bo_ioctl(struct drm_device *dev, void *data, 202 struct drm_file *file_priv) 203 { 204 struct drm_v3d_create_bo *args = data; 205 struct v3d_bo *bo = NULL; 206 int ret; 207 208 if (args->flags != 0) { 209 DRM_INFO("unknown create_bo flags: %d\n", args->flags); 210 return -EINVAL; 211 } 212 213 bo = v3d_bo_create(dev, file_priv, PAGE_ALIGN(args->size)); 214 if (IS_ERR(bo)) 215 return PTR_ERR(bo); 216 217 args->offset = bo->node.start << V3D_MMU_PAGE_SHIFT; 218 219 ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle); 220 drm_gem_object_put(&bo->base.base); 221 222 return ret; 223 } 224 225 int v3d_mmap_bo_ioctl(struct drm_device *dev, void *data, 226 struct drm_file *file_priv) 227 { 228 struct drm_v3d_mmap_bo *args = data; 229 struct drm_gem_object *gem_obj; 230 231 if (args->flags != 0) { 232 DRM_INFO("unknown mmap_bo flags: %d\n", args->flags); 233 return -EINVAL; 234 } 235 236 gem_obj = drm_gem_object_lookup(file_priv, args->handle); 237 if (!gem_obj) { 238 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle); 239 return -ENOENT; 240 } 241 242 args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node); 243 drm_gem_object_put(gem_obj); 244 245 return 0; 246 } 247 248 int v3d_get_bo_offset_ioctl(struct drm_device *dev, void *data, 249 struct drm_file *file_priv) 250 { 251 struct drm_v3d_get_bo_offset *args = data; 252 struct drm_gem_object *gem_obj; 253 struct v3d_bo *bo; 254 255 gem_obj = drm_gem_object_lookup(file_priv, args->handle); 256 if (!gem_obj) { 257 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle); 258 return -ENOENT; 259 } 260 bo = to_v3d_bo(gem_obj); 261 262 args->offset = bo->node.start << V3D_MMU_PAGE_SHIFT; 263 264 drm_gem_object_put(gem_obj); 265 return 0; 266 } 267 268 int 269 v3d_wait_bo_ioctl(struct drm_device *dev, void *data, 270 struct drm_file *file_priv) 271 { 272 int ret; 273 struct drm_v3d_wait_bo *args = data; 274 ktime_t start = ktime_get(); 275 u64 delta_ns; 276 unsigned long timeout_jiffies = 277 nsecs_to_jiffies_timeout(args->timeout_ns); 278 279 if (args->pad != 0) 280 return -EINVAL; 281 282 ret = drm_gem_dma_resv_wait(file_priv, args->handle, 283 true, timeout_jiffies); 284 285 /* Decrement the user's timeout, in case we got interrupted 286 * such that the ioctl will be restarted. 287 */ 288 delta_ns = ktime_to_ns(ktime_sub(ktime_get(), start)); 289 if (delta_ns < args->timeout_ns) 290 args->timeout_ns -= delta_ns; 291 else 292 args->timeout_ns = 0; 293 294 /* Asked to wait beyond the jiffy/scheduler precision? */ 295 if (ret == -ETIME && args->timeout_ns) 296 ret = -EAGAIN; 297 298 return ret; 299 } 300