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 u64 align; 111 int ret; 112 113 /* So far we pin the BO in the MMU for its lifetime, so use 114 * shmem's helper for getting a lifetime sgt. 115 */ 116 sgt = drm_gem_shmem_get_pages_sgt(&bo->base); 117 if (IS_ERR(sgt)) 118 return PTR_ERR(sgt); 119 120 if (!v3d->gemfs) 121 align = SZ_4K; 122 else if (obj->size >= SZ_1M) 123 align = SZ_1M; 124 else if (obj->size >= SZ_64K) 125 align = SZ_64K; 126 else 127 align = SZ_4K; 128 129 spin_lock(&v3d->mm_lock); 130 /* Allocate the object's space in the GPU's page tables. 131 * Inserting PTEs will happen later, but the offset is for the 132 * lifetime of the BO. 133 */ 134 ret = drm_mm_insert_node_generic(&v3d->mm, &bo->node, 135 obj->size >> V3D_MMU_PAGE_SHIFT, 136 align >> V3D_MMU_PAGE_SHIFT, 0, 0); 137 spin_unlock(&v3d->mm_lock); 138 if (ret) 139 return ret; 140 141 /* Track stats for /debug/dri/n/bo_stats. */ 142 mutex_lock(&v3d->bo_lock); 143 v3d->bo_stats.num_allocated++; 144 v3d->bo_stats.pages_allocated += obj->size >> V3D_MMU_PAGE_SHIFT; 145 mutex_unlock(&v3d->bo_lock); 146 147 v3d_mmu_insert_ptes(bo); 148 149 return 0; 150 } 151 152 struct v3d_bo *v3d_bo_create(struct drm_device *dev, struct drm_file *file_priv, 153 size_t unaligned_size) 154 { 155 struct drm_gem_shmem_object *shmem_obj; 156 struct v3d_dev *v3d = to_v3d_dev(dev); 157 struct v3d_bo *bo; 158 int ret; 159 160 shmem_obj = drm_gem_shmem_create_with_mnt(dev, unaligned_size, 161 v3d->gemfs); 162 if (IS_ERR(shmem_obj)) 163 return ERR_CAST(shmem_obj); 164 bo = to_v3d_bo(&shmem_obj->base); 165 bo->vaddr = NULL; 166 167 ret = v3d_bo_create_finish(&shmem_obj->base); 168 if (ret) 169 goto free_obj; 170 171 return bo; 172 173 free_obj: 174 drm_gem_shmem_free(shmem_obj); 175 return ERR_PTR(ret); 176 } 177 178 struct drm_gem_object * 179 v3d_prime_import_sg_table(struct drm_device *dev, 180 struct dma_buf_attachment *attach, 181 struct sg_table *sgt) 182 { 183 struct drm_gem_object *obj; 184 int ret; 185 186 obj = drm_gem_shmem_prime_import_sg_table(dev, attach, sgt); 187 if (IS_ERR(obj)) 188 return obj; 189 190 ret = v3d_bo_create_finish(obj); 191 if (ret) { 192 drm_gem_shmem_free(&to_v3d_bo(obj)->base); 193 return ERR_PTR(ret); 194 } 195 196 return obj; 197 } 198 199 void v3d_get_bo_vaddr(struct v3d_bo *bo) 200 { 201 struct drm_gem_shmem_object *obj = &bo->base; 202 203 bo->vaddr = vmap(obj->pages, obj->base.size >> PAGE_SHIFT, VM_MAP, 204 pgprot_writecombine(PAGE_KERNEL)); 205 } 206 207 void v3d_put_bo_vaddr(struct v3d_bo *bo) 208 { 209 vunmap(bo->vaddr); 210 bo->vaddr = NULL; 211 } 212 213 int v3d_create_bo_ioctl(struct drm_device *dev, void *data, 214 struct drm_file *file_priv) 215 { 216 struct drm_v3d_create_bo *args = data; 217 struct v3d_bo *bo = NULL; 218 int ret; 219 220 if (args->flags != 0) { 221 DRM_INFO("unknown create_bo flags: %d\n", args->flags); 222 return -EINVAL; 223 } 224 225 bo = v3d_bo_create(dev, file_priv, PAGE_ALIGN(args->size)); 226 if (IS_ERR(bo)) 227 return PTR_ERR(bo); 228 229 args->offset = bo->node.start << V3D_MMU_PAGE_SHIFT; 230 231 ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle); 232 drm_gem_object_put(&bo->base.base); 233 234 return ret; 235 } 236 237 int v3d_mmap_bo_ioctl(struct drm_device *dev, void *data, 238 struct drm_file *file_priv) 239 { 240 struct drm_v3d_mmap_bo *args = data; 241 struct drm_gem_object *gem_obj; 242 243 if (args->flags != 0) { 244 DRM_INFO("unknown mmap_bo flags: %d\n", args->flags); 245 return -EINVAL; 246 } 247 248 gem_obj = drm_gem_object_lookup(file_priv, args->handle); 249 if (!gem_obj) { 250 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle); 251 return -ENOENT; 252 } 253 254 args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node); 255 drm_gem_object_put(gem_obj); 256 257 return 0; 258 } 259 260 int v3d_get_bo_offset_ioctl(struct drm_device *dev, void *data, 261 struct drm_file *file_priv) 262 { 263 struct drm_v3d_get_bo_offset *args = data; 264 struct drm_gem_object *gem_obj; 265 struct v3d_bo *bo; 266 267 gem_obj = drm_gem_object_lookup(file_priv, args->handle); 268 if (!gem_obj) { 269 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle); 270 return -ENOENT; 271 } 272 bo = to_v3d_bo(gem_obj); 273 274 args->offset = bo->node.start << V3D_MMU_PAGE_SHIFT; 275 276 drm_gem_object_put(gem_obj); 277 return 0; 278 } 279 280 int 281 v3d_wait_bo_ioctl(struct drm_device *dev, void *data, 282 struct drm_file *file_priv) 283 { 284 int ret; 285 struct drm_v3d_wait_bo *args = data; 286 ktime_t start = ktime_get(); 287 u64 delta_ns; 288 unsigned long timeout_jiffies = 289 nsecs_to_jiffies_timeout(args->timeout_ns); 290 291 if (args->pad != 0) 292 return -EINVAL; 293 294 ret = drm_gem_dma_resv_wait(file_priv, args->handle, 295 true, timeout_jiffies); 296 297 /* Decrement the user's timeout, in case we got interrupted 298 * such that the ioctl will be restarted. 299 */ 300 delta_ns = ktime_to_ns(ktime_sub(ktime_get(), start)); 301 if (delta_ns < args->timeout_ns) 302 args->timeout_ns -= delta_ns; 303 else 304 args->timeout_ns = 0; 305 306 /* Asked to wait beyond the jiffy/scheduler precision? */ 307 if (ret == -ETIME && args->timeout_ns) 308 ret = -EAGAIN; 309 310 return ret; 311 } 312