1 /* 2 * Copyright (C) 2015 Etnaviv Project 3 * 4 * This program is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 as published by 6 * the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 * more details. 12 * 13 * You should have received a copy of the GNU General Public License along with 14 * this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16 17 #include <linux/reservation.h> 18 #include "etnaviv_drv.h" 19 #include "etnaviv_gpu.h" 20 #include "etnaviv_gem.h" 21 22 /* 23 * Cmdstream submission: 24 */ 25 26 #define BO_INVALID_FLAGS ~(ETNA_SUBMIT_BO_READ | ETNA_SUBMIT_BO_WRITE) 27 /* make sure these don't conflict w/ ETNAVIV_SUBMIT_BO_x */ 28 #define BO_LOCKED 0x4000 29 #define BO_PINNED 0x2000 30 31 static struct etnaviv_gem_submit *submit_create(struct drm_device *dev, 32 struct etnaviv_gpu *gpu, size_t nr) 33 { 34 struct etnaviv_gem_submit *submit; 35 size_t sz = size_vstruct(nr, sizeof(submit->bos[0]), sizeof(*submit)); 36 37 submit = kmalloc(sz, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY); 38 if (submit) { 39 submit->dev = dev; 40 submit->gpu = gpu; 41 42 /* initially, until copy_from_user() and bo lookup succeeds: */ 43 submit->nr_bos = 0; 44 45 ww_acquire_init(&submit->ticket, &reservation_ww_class); 46 } 47 48 return submit; 49 } 50 51 static int submit_lookup_objects(struct etnaviv_gem_submit *submit, 52 struct drm_file *file, struct drm_etnaviv_gem_submit_bo *submit_bos, 53 unsigned nr_bos) 54 { 55 struct drm_etnaviv_gem_submit_bo *bo; 56 unsigned i; 57 int ret = 0; 58 59 spin_lock(&file->table_lock); 60 61 for (i = 0, bo = submit_bos; i < nr_bos; i++, bo++) { 62 struct drm_gem_object *obj; 63 64 if (bo->flags & BO_INVALID_FLAGS) { 65 DRM_ERROR("invalid flags: %x\n", bo->flags); 66 ret = -EINVAL; 67 goto out_unlock; 68 } 69 70 submit->bos[i].flags = bo->flags; 71 72 /* normally use drm_gem_object_lookup(), but for bulk lookup 73 * all under single table_lock just hit object_idr directly: 74 */ 75 obj = idr_find(&file->object_idr, bo->handle); 76 if (!obj) { 77 DRM_ERROR("invalid handle %u at index %u\n", 78 bo->handle, i); 79 ret = -EINVAL; 80 goto out_unlock; 81 } 82 83 /* 84 * Take a refcount on the object. The file table lock 85 * prevents the object_idr's refcount on this being dropped. 86 */ 87 drm_gem_object_reference(obj); 88 89 submit->bos[i].obj = to_etnaviv_bo(obj); 90 } 91 92 out_unlock: 93 submit->nr_bos = i; 94 spin_unlock(&file->table_lock); 95 96 return ret; 97 } 98 99 static void submit_unlock_object(struct etnaviv_gem_submit *submit, int i) 100 { 101 if (submit->bos[i].flags & BO_LOCKED) { 102 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj; 103 104 ww_mutex_unlock(&etnaviv_obj->resv->lock); 105 submit->bos[i].flags &= ~BO_LOCKED; 106 } 107 } 108 109 static int submit_lock_objects(struct etnaviv_gem_submit *submit) 110 { 111 int contended, slow_locked = -1, i, ret = 0; 112 113 retry: 114 for (i = 0; i < submit->nr_bos; i++) { 115 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj; 116 117 if (slow_locked == i) 118 slow_locked = -1; 119 120 contended = i; 121 122 if (!(submit->bos[i].flags & BO_LOCKED)) { 123 ret = ww_mutex_lock_interruptible(&etnaviv_obj->resv->lock, 124 &submit->ticket); 125 if (ret == -EALREADY) 126 DRM_ERROR("BO at index %u already on submit list\n", 127 i); 128 if (ret) 129 goto fail; 130 submit->bos[i].flags |= BO_LOCKED; 131 } 132 } 133 134 ww_acquire_done(&submit->ticket); 135 136 return 0; 137 138 fail: 139 for (; i >= 0; i--) 140 submit_unlock_object(submit, i); 141 142 if (slow_locked > 0) 143 submit_unlock_object(submit, slow_locked); 144 145 if (ret == -EDEADLK) { 146 struct etnaviv_gem_object *etnaviv_obj; 147 148 etnaviv_obj = submit->bos[contended].obj; 149 150 /* we lost out in a seqno race, lock and retry.. */ 151 ret = ww_mutex_lock_slow_interruptible(&etnaviv_obj->resv->lock, 152 &submit->ticket); 153 if (!ret) { 154 submit->bos[contended].flags |= BO_LOCKED; 155 slow_locked = contended; 156 goto retry; 157 } 158 } 159 160 return ret; 161 } 162 163 static int submit_fence_sync(const struct etnaviv_gem_submit *submit) 164 { 165 unsigned int context = submit->gpu->fence_context; 166 int i, ret = 0; 167 168 for (i = 0; i < submit->nr_bos; i++) { 169 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj; 170 bool write = submit->bos[i].flags & ETNA_SUBMIT_BO_WRITE; 171 172 ret = etnaviv_gpu_fence_sync_obj(etnaviv_obj, context, write); 173 if (ret) 174 break; 175 } 176 177 return ret; 178 } 179 180 static void submit_unpin_objects(struct etnaviv_gem_submit *submit) 181 { 182 int i; 183 184 for (i = 0; i < submit->nr_bos; i++) { 185 if (submit->bos[i].flags & BO_PINNED) 186 etnaviv_gem_mapping_unreference(submit->bos[i].mapping); 187 188 submit->bos[i].mapping = NULL; 189 submit->bos[i].flags &= ~BO_PINNED; 190 } 191 } 192 193 static int submit_pin_objects(struct etnaviv_gem_submit *submit) 194 { 195 int i, ret = 0; 196 197 for (i = 0; i < submit->nr_bos; i++) { 198 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj; 199 struct etnaviv_vram_mapping *mapping; 200 201 mapping = etnaviv_gem_mapping_get(&etnaviv_obj->base, 202 submit->gpu); 203 if (IS_ERR(mapping)) { 204 ret = PTR_ERR(mapping); 205 break; 206 } 207 208 submit->bos[i].flags |= BO_PINNED; 209 submit->bos[i].mapping = mapping; 210 } 211 212 return ret; 213 } 214 215 static int submit_bo(struct etnaviv_gem_submit *submit, u32 idx, 216 struct etnaviv_gem_submit_bo **bo) 217 { 218 if (idx >= submit->nr_bos) { 219 DRM_ERROR("invalid buffer index: %u (out of %u)\n", 220 idx, submit->nr_bos); 221 return -EINVAL; 222 } 223 224 *bo = &submit->bos[idx]; 225 226 return 0; 227 } 228 229 /* process the reloc's and patch up the cmdstream as needed: */ 230 static int submit_reloc(struct etnaviv_gem_submit *submit, void *stream, 231 u32 size, const struct drm_etnaviv_gem_submit_reloc *relocs, 232 u32 nr_relocs) 233 { 234 u32 i, last_offset = 0; 235 u32 *ptr = stream; 236 int ret; 237 238 for (i = 0; i < nr_relocs; i++) { 239 const struct drm_etnaviv_gem_submit_reloc *r = relocs + i; 240 struct etnaviv_gem_submit_bo *bo; 241 u32 off; 242 243 if (unlikely(r->flags)) { 244 DRM_ERROR("invalid reloc flags\n"); 245 return -EINVAL; 246 } 247 248 if (r->submit_offset % 4) { 249 DRM_ERROR("non-aligned reloc offset: %u\n", 250 r->submit_offset); 251 return -EINVAL; 252 } 253 254 /* offset in dwords: */ 255 off = r->submit_offset / 4; 256 257 if ((off >= size ) || 258 (off < last_offset)) { 259 DRM_ERROR("invalid offset %u at reloc %u\n", off, i); 260 return -EINVAL; 261 } 262 263 ret = submit_bo(submit, r->reloc_idx, &bo); 264 if (ret) 265 return ret; 266 267 if (r->reloc_offset >= bo->obj->base.size - sizeof(*ptr)) { 268 DRM_ERROR("relocation %u outside object", i); 269 return -EINVAL; 270 } 271 272 ptr[off] = bo->mapping->iova + r->reloc_offset; 273 274 last_offset = off; 275 } 276 277 return 0; 278 } 279 280 static void submit_cleanup(struct etnaviv_gem_submit *submit) 281 { 282 unsigned i; 283 284 for (i = 0; i < submit->nr_bos; i++) { 285 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj; 286 287 submit_unlock_object(submit, i); 288 drm_gem_object_unreference_unlocked(&etnaviv_obj->base); 289 } 290 291 ww_acquire_fini(&submit->ticket); 292 kfree(submit); 293 } 294 295 int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data, 296 struct drm_file *file) 297 { 298 struct etnaviv_drm_private *priv = dev->dev_private; 299 struct drm_etnaviv_gem_submit *args = data; 300 struct drm_etnaviv_gem_submit_reloc *relocs; 301 struct drm_etnaviv_gem_submit_bo *bos; 302 struct etnaviv_gem_submit *submit; 303 struct etnaviv_cmdbuf *cmdbuf; 304 struct etnaviv_gpu *gpu; 305 void *stream; 306 int ret; 307 308 if (args->pipe >= ETNA_MAX_PIPES) 309 return -EINVAL; 310 311 gpu = priv->gpu[args->pipe]; 312 if (!gpu) 313 return -ENXIO; 314 315 if (args->stream_size % 4) { 316 DRM_ERROR("non-aligned cmdstream buffer size: %u\n", 317 args->stream_size); 318 return -EINVAL; 319 } 320 321 if (args->exec_state != ETNA_PIPE_3D && 322 args->exec_state != ETNA_PIPE_2D && 323 args->exec_state != ETNA_PIPE_VG) { 324 DRM_ERROR("invalid exec_state: 0x%x\n", args->exec_state); 325 return -EINVAL; 326 } 327 328 /* 329 * Copy the command submission and bo array to kernel space in 330 * one go, and do this outside of any locks. 331 */ 332 bos = drm_malloc_ab(args->nr_bos, sizeof(*bos)); 333 relocs = drm_malloc_ab(args->nr_relocs, sizeof(*relocs)); 334 stream = drm_malloc_ab(1, args->stream_size); 335 cmdbuf = etnaviv_gpu_cmdbuf_new(gpu, ALIGN(args->stream_size, 8) + 8, 336 args->nr_bos); 337 if (!bos || !relocs || !stream || !cmdbuf) { 338 ret = -ENOMEM; 339 goto err_submit_cmds; 340 } 341 342 cmdbuf->exec_state = args->exec_state; 343 cmdbuf->ctx = file->driver_priv; 344 345 ret = copy_from_user(bos, u64_to_user_ptr(args->bos), 346 args->nr_bos * sizeof(*bos)); 347 if (ret) { 348 ret = -EFAULT; 349 goto err_submit_cmds; 350 } 351 352 ret = copy_from_user(relocs, u64_to_user_ptr(args->relocs), 353 args->nr_relocs * sizeof(*relocs)); 354 if (ret) { 355 ret = -EFAULT; 356 goto err_submit_cmds; 357 } 358 359 ret = copy_from_user(stream, u64_to_user_ptr(args->stream), 360 args->stream_size); 361 if (ret) { 362 ret = -EFAULT; 363 goto err_submit_cmds; 364 } 365 366 submit = submit_create(dev, gpu, args->nr_bos); 367 if (!submit) { 368 ret = -ENOMEM; 369 goto err_submit_cmds; 370 } 371 372 ret = submit_lookup_objects(submit, file, bos, args->nr_bos); 373 if (ret) 374 goto err_submit_objects; 375 376 ret = submit_lock_objects(submit); 377 if (ret) 378 goto err_submit_objects; 379 380 if (!etnaviv_cmd_validate_one(gpu, stream, args->stream_size / 4, 381 relocs, args->nr_relocs)) { 382 ret = -EINVAL; 383 goto err_submit_objects; 384 } 385 386 ret = submit_fence_sync(submit); 387 if (ret) 388 goto err_submit_objects; 389 390 ret = submit_pin_objects(submit); 391 if (ret) 392 goto out; 393 394 ret = submit_reloc(submit, stream, args->stream_size / 4, 395 relocs, args->nr_relocs); 396 if (ret) 397 goto out; 398 399 memcpy(cmdbuf->vaddr, stream, args->stream_size); 400 cmdbuf->user_size = ALIGN(args->stream_size, 8); 401 402 ret = etnaviv_gpu_submit(gpu, submit, cmdbuf); 403 if (ret == 0) 404 cmdbuf = NULL; 405 406 args->fence = submit->fence; 407 408 out: 409 submit_unpin_objects(submit); 410 411 /* 412 * If we're returning -EAGAIN, it may be due to the userptr code 413 * wanting to run its workqueue outside of any locks. Flush our 414 * workqueue to ensure that it is run in a timely manner. 415 */ 416 if (ret == -EAGAIN) 417 flush_workqueue(priv->wq); 418 419 err_submit_objects: 420 submit_cleanup(submit); 421 422 err_submit_cmds: 423 /* if we still own the cmdbuf */ 424 if (cmdbuf) 425 etnaviv_gpu_cmdbuf_free(cmdbuf); 426 if (stream) 427 drm_free_large(stream); 428 if (bos) 429 drm_free_large(bos); 430 if (relocs) 431 drm_free_large(relocs); 432 433 return ret; 434 } 435