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/dma-fence-array.h> 18 #include <linux/reservation.h> 19 #include <linux/sync_file.h> 20 #include "etnaviv_cmdbuf.h" 21 #include "etnaviv_drv.h" 22 #include "etnaviv_gpu.h" 23 #include "etnaviv_gem.h" 24 #include "etnaviv_perfmon.h" 25 26 /* 27 * Cmdstream submission: 28 */ 29 30 #define BO_INVALID_FLAGS ~(ETNA_SUBMIT_BO_READ | ETNA_SUBMIT_BO_WRITE) 31 /* make sure these don't conflict w/ ETNAVIV_SUBMIT_BO_x */ 32 #define BO_LOCKED 0x4000 33 #define BO_PINNED 0x2000 34 35 static struct etnaviv_gem_submit *submit_create(struct drm_device *dev, 36 struct etnaviv_gpu *gpu, size_t nr_bos, size_t nr_pmrs) 37 { 38 struct etnaviv_gem_submit *submit; 39 size_t sz = size_vstruct(nr_bos, sizeof(submit->bos[0]), sizeof(*submit)); 40 41 submit = kzalloc(sz, GFP_KERNEL); 42 if (!submit) 43 return NULL; 44 45 submit->pmrs = kcalloc(nr_pmrs, sizeof(struct etnaviv_perfmon_request), 46 GFP_KERNEL); 47 if (!submit->pmrs) { 48 kfree(submit); 49 return NULL; 50 } 51 submit->nr_pmrs = nr_pmrs; 52 53 submit->gpu = gpu; 54 kref_init(&submit->refcount); 55 56 return submit; 57 } 58 59 static int submit_lookup_objects(struct etnaviv_gem_submit *submit, 60 struct drm_file *file, struct drm_etnaviv_gem_submit_bo *submit_bos, 61 unsigned nr_bos) 62 { 63 struct drm_etnaviv_gem_submit_bo *bo; 64 unsigned i; 65 int ret = 0; 66 67 spin_lock(&file->table_lock); 68 69 for (i = 0, bo = submit_bos; i < nr_bos; i++, bo++) { 70 struct drm_gem_object *obj; 71 72 if (bo->flags & BO_INVALID_FLAGS) { 73 DRM_ERROR("invalid flags: %x\n", bo->flags); 74 ret = -EINVAL; 75 goto out_unlock; 76 } 77 78 submit->bos[i].flags = bo->flags; 79 80 /* normally use drm_gem_object_lookup(), but for bulk lookup 81 * all under single table_lock just hit object_idr directly: 82 */ 83 obj = idr_find(&file->object_idr, bo->handle); 84 if (!obj) { 85 DRM_ERROR("invalid handle %u at index %u\n", 86 bo->handle, i); 87 ret = -EINVAL; 88 goto out_unlock; 89 } 90 91 /* 92 * Take a refcount on the object. The file table lock 93 * prevents the object_idr's refcount on this being dropped. 94 */ 95 drm_gem_object_get(obj); 96 97 submit->bos[i].obj = to_etnaviv_bo(obj); 98 } 99 100 out_unlock: 101 submit->nr_bos = i; 102 spin_unlock(&file->table_lock); 103 104 return ret; 105 } 106 107 static void submit_unlock_object(struct etnaviv_gem_submit *submit, int i) 108 { 109 if (submit->bos[i].flags & BO_LOCKED) { 110 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj; 111 112 ww_mutex_unlock(&etnaviv_obj->resv->lock); 113 submit->bos[i].flags &= ~BO_LOCKED; 114 } 115 } 116 117 static int submit_lock_objects(struct etnaviv_gem_submit *submit, 118 struct ww_acquire_ctx *ticket) 119 { 120 int contended, slow_locked = -1, i, ret = 0; 121 122 retry: 123 for (i = 0; i < submit->nr_bos; i++) { 124 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj; 125 126 if (slow_locked == i) 127 slow_locked = -1; 128 129 contended = i; 130 131 if (!(submit->bos[i].flags & BO_LOCKED)) { 132 ret = ww_mutex_lock_interruptible(&etnaviv_obj->resv->lock, 133 ticket); 134 if (ret == -EALREADY) 135 DRM_ERROR("BO at index %u already on submit list\n", 136 i); 137 if (ret) 138 goto fail; 139 submit->bos[i].flags |= BO_LOCKED; 140 } 141 } 142 143 ww_acquire_done(ticket); 144 145 return 0; 146 147 fail: 148 for (; i >= 0; i--) 149 submit_unlock_object(submit, i); 150 151 if (slow_locked > 0) 152 submit_unlock_object(submit, slow_locked); 153 154 if (ret == -EDEADLK) { 155 struct etnaviv_gem_object *etnaviv_obj; 156 157 etnaviv_obj = submit->bos[contended].obj; 158 159 /* we lost out in a seqno race, lock and retry.. */ 160 ret = ww_mutex_lock_slow_interruptible(&etnaviv_obj->resv->lock, 161 ticket); 162 if (!ret) { 163 submit->bos[contended].flags |= BO_LOCKED; 164 slow_locked = contended; 165 goto retry; 166 } 167 } 168 169 return ret; 170 } 171 172 static int submit_fence_sync(const struct etnaviv_gem_submit *submit) 173 { 174 unsigned int context = submit->gpu->fence_context; 175 int i, ret = 0; 176 177 for (i = 0; i < submit->nr_bos; i++) { 178 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj; 179 bool write = submit->bos[i].flags & ETNA_SUBMIT_BO_WRITE; 180 bool explicit = !!(submit->flags & ETNA_SUBMIT_NO_IMPLICIT); 181 182 ret = etnaviv_gpu_fence_sync_obj(etnaviv_obj, context, write, 183 explicit); 184 if (ret) 185 break; 186 } 187 188 if (submit->flags & ETNA_SUBMIT_FENCE_FD_IN) { 189 /* 190 * Wait if the fence is from a foreign context, or if the fence 191 * array contains any fence from a foreign context. 192 */ 193 if (!dma_fence_match_context(submit->in_fence, context)) 194 ret = dma_fence_wait(submit->in_fence, true); 195 } 196 197 return ret; 198 } 199 200 static void submit_attach_object_fences(struct etnaviv_gem_submit *submit) 201 { 202 int i; 203 204 for (i = 0; i < submit->nr_bos; i++) { 205 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj; 206 207 if (submit->bos[i].flags & ETNA_SUBMIT_BO_WRITE) 208 reservation_object_add_excl_fence(etnaviv_obj->resv, 209 submit->out_fence); 210 else 211 reservation_object_add_shared_fence(etnaviv_obj->resv, 212 submit->out_fence); 213 214 submit_unlock_object(submit, i); 215 } 216 } 217 218 static int submit_pin_objects(struct etnaviv_gem_submit *submit) 219 { 220 int i, ret = 0; 221 222 for (i = 0; i < submit->nr_bos; i++) { 223 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj; 224 struct etnaviv_vram_mapping *mapping; 225 226 mapping = etnaviv_gem_mapping_get(&etnaviv_obj->base, 227 submit->gpu); 228 if (IS_ERR(mapping)) { 229 ret = PTR_ERR(mapping); 230 break; 231 } 232 atomic_inc(&etnaviv_obj->gpu_active); 233 234 submit->bos[i].flags |= BO_PINNED; 235 submit->bos[i].mapping = mapping; 236 } 237 238 return ret; 239 } 240 241 static int submit_bo(struct etnaviv_gem_submit *submit, u32 idx, 242 struct etnaviv_gem_submit_bo **bo) 243 { 244 if (idx >= submit->nr_bos) { 245 DRM_ERROR("invalid buffer index: %u (out of %u)\n", 246 idx, submit->nr_bos); 247 return -EINVAL; 248 } 249 250 *bo = &submit->bos[idx]; 251 252 return 0; 253 } 254 255 /* process the reloc's and patch up the cmdstream as needed: */ 256 static int submit_reloc(struct etnaviv_gem_submit *submit, void *stream, 257 u32 size, const struct drm_etnaviv_gem_submit_reloc *relocs, 258 u32 nr_relocs) 259 { 260 u32 i, last_offset = 0; 261 u32 *ptr = stream; 262 int ret; 263 264 for (i = 0; i < nr_relocs; i++) { 265 const struct drm_etnaviv_gem_submit_reloc *r = relocs + i; 266 struct etnaviv_gem_submit_bo *bo; 267 u32 off; 268 269 if (unlikely(r->flags)) { 270 DRM_ERROR("invalid reloc flags\n"); 271 return -EINVAL; 272 } 273 274 if (r->submit_offset % 4) { 275 DRM_ERROR("non-aligned reloc offset: %u\n", 276 r->submit_offset); 277 return -EINVAL; 278 } 279 280 /* offset in dwords: */ 281 off = r->submit_offset / 4; 282 283 if ((off >= size ) || 284 (off < last_offset)) { 285 DRM_ERROR("invalid offset %u at reloc %u\n", off, i); 286 return -EINVAL; 287 } 288 289 ret = submit_bo(submit, r->reloc_idx, &bo); 290 if (ret) 291 return ret; 292 293 if (r->reloc_offset > bo->obj->base.size - sizeof(*ptr)) { 294 DRM_ERROR("relocation %u outside object\n", i); 295 return -EINVAL; 296 } 297 298 ptr[off] = bo->mapping->iova + r->reloc_offset; 299 300 last_offset = off; 301 } 302 303 return 0; 304 } 305 306 static int submit_perfmon_validate(struct etnaviv_gem_submit *submit, 307 u32 exec_state, const struct drm_etnaviv_gem_submit_pmr *pmrs) 308 { 309 u32 i; 310 311 for (i = 0; i < submit->nr_pmrs; i++) { 312 const struct drm_etnaviv_gem_submit_pmr *r = pmrs + i; 313 struct etnaviv_gem_submit_bo *bo; 314 int ret; 315 316 ret = submit_bo(submit, r->read_idx, &bo); 317 if (ret) 318 return ret; 319 320 /* at offset 0 a sequence number gets stored used for userspace sync */ 321 if (r->read_offset == 0) { 322 DRM_ERROR("perfmon request: offset is 0"); 323 return -EINVAL; 324 } 325 326 if (r->read_offset >= bo->obj->base.size - sizeof(u32)) { 327 DRM_ERROR("perfmon request: offset %u outside object", i); 328 return -EINVAL; 329 } 330 331 if (r->flags & ~(ETNA_PM_PROCESS_PRE | ETNA_PM_PROCESS_POST)) { 332 DRM_ERROR("perfmon request: flags are not valid"); 333 return -EINVAL; 334 } 335 336 if (etnaviv_pm_req_validate(r, exec_state)) { 337 DRM_ERROR("perfmon request: domain or signal not valid"); 338 return -EINVAL; 339 } 340 341 submit->pmrs[i].flags = r->flags; 342 submit->pmrs[i].domain = r->domain; 343 submit->pmrs[i].signal = r->signal; 344 submit->pmrs[i].sequence = r->sequence; 345 submit->pmrs[i].offset = r->read_offset; 346 submit->pmrs[i].bo_vma = etnaviv_gem_vmap(&bo->obj->base); 347 } 348 349 return 0; 350 } 351 352 static void submit_cleanup(struct kref *kref) 353 { 354 struct etnaviv_gem_submit *submit = 355 container_of(kref, struct etnaviv_gem_submit, refcount); 356 unsigned i; 357 358 if (submit->runtime_resumed) 359 pm_runtime_put_autosuspend(submit->gpu->dev); 360 361 if (submit->cmdbuf.suballoc) 362 etnaviv_cmdbuf_free(&submit->cmdbuf); 363 364 for (i = 0; i < submit->nr_bos; i++) { 365 struct etnaviv_gem_object *etnaviv_obj = submit->bos[i].obj; 366 367 /* unpin all objects */ 368 if (submit->bos[i].flags & BO_PINNED) { 369 etnaviv_gem_mapping_unreference(submit->bos[i].mapping); 370 atomic_dec(&etnaviv_obj->gpu_active); 371 submit->bos[i].mapping = NULL; 372 submit->bos[i].flags &= ~BO_PINNED; 373 } 374 375 /* if the GPU submit failed, objects might still be locked */ 376 submit_unlock_object(submit, i); 377 drm_gem_object_put_unlocked(&etnaviv_obj->base); 378 } 379 380 wake_up_all(&submit->gpu->fence_event); 381 382 if (submit->in_fence) 383 dma_fence_put(submit->in_fence); 384 if (submit->out_fence) 385 dma_fence_put(submit->out_fence); 386 kfree(submit->pmrs); 387 kfree(submit); 388 } 389 390 void etnaviv_submit_put(struct etnaviv_gem_submit *submit) 391 { 392 kref_put(&submit->refcount, submit_cleanup); 393 } 394 395 int etnaviv_ioctl_gem_submit(struct drm_device *dev, void *data, 396 struct drm_file *file) 397 { 398 struct etnaviv_drm_private *priv = dev->dev_private; 399 struct drm_etnaviv_gem_submit *args = data; 400 struct drm_etnaviv_gem_submit_reloc *relocs; 401 struct drm_etnaviv_gem_submit_pmr *pmrs; 402 struct drm_etnaviv_gem_submit_bo *bos; 403 struct etnaviv_gem_submit *submit; 404 struct etnaviv_gpu *gpu; 405 struct sync_file *sync_file = NULL; 406 struct ww_acquire_ctx ticket; 407 int out_fence_fd = -1; 408 void *stream; 409 int ret; 410 411 if (args->pipe >= ETNA_MAX_PIPES) 412 return -EINVAL; 413 414 gpu = priv->gpu[args->pipe]; 415 if (!gpu) 416 return -ENXIO; 417 418 if (args->stream_size % 4) { 419 DRM_ERROR("non-aligned cmdstream buffer size: %u\n", 420 args->stream_size); 421 return -EINVAL; 422 } 423 424 if (args->exec_state != ETNA_PIPE_3D && 425 args->exec_state != ETNA_PIPE_2D && 426 args->exec_state != ETNA_PIPE_VG) { 427 DRM_ERROR("invalid exec_state: 0x%x\n", args->exec_state); 428 return -EINVAL; 429 } 430 431 if (args->flags & ~ETNA_SUBMIT_FLAGS) { 432 DRM_ERROR("invalid flags: 0x%x\n", args->flags); 433 return -EINVAL; 434 } 435 436 /* 437 * Copy the command submission and bo array to kernel space in 438 * one go, and do this outside of any locks. 439 */ 440 bos = kvmalloc_array(args->nr_bos, sizeof(*bos), GFP_KERNEL); 441 relocs = kvmalloc_array(args->nr_relocs, sizeof(*relocs), GFP_KERNEL); 442 pmrs = kvmalloc_array(args->nr_pmrs, sizeof(*pmrs), GFP_KERNEL); 443 stream = kvmalloc_array(1, args->stream_size, GFP_KERNEL); 444 if (!bos || !relocs || !pmrs || !stream) { 445 ret = -ENOMEM; 446 goto err_submit_cmds; 447 } 448 449 ret = copy_from_user(bos, u64_to_user_ptr(args->bos), 450 args->nr_bos * sizeof(*bos)); 451 if (ret) { 452 ret = -EFAULT; 453 goto err_submit_cmds; 454 } 455 456 ret = copy_from_user(relocs, u64_to_user_ptr(args->relocs), 457 args->nr_relocs * sizeof(*relocs)); 458 if (ret) { 459 ret = -EFAULT; 460 goto err_submit_cmds; 461 } 462 463 ret = copy_from_user(pmrs, u64_to_user_ptr(args->pmrs), 464 args->nr_pmrs * sizeof(*pmrs)); 465 if (ret) { 466 ret = -EFAULT; 467 goto err_submit_cmds; 468 } 469 470 ret = copy_from_user(stream, u64_to_user_ptr(args->stream), 471 args->stream_size); 472 if (ret) { 473 ret = -EFAULT; 474 goto err_submit_cmds; 475 } 476 477 if (args->flags & ETNA_SUBMIT_FENCE_FD_OUT) { 478 out_fence_fd = get_unused_fd_flags(O_CLOEXEC); 479 if (out_fence_fd < 0) { 480 ret = out_fence_fd; 481 goto err_submit_cmds; 482 } 483 } 484 485 ww_acquire_init(&ticket, &reservation_ww_class); 486 487 submit = submit_create(dev, gpu, args->nr_bos, args->nr_pmrs); 488 if (!submit) { 489 ret = -ENOMEM; 490 goto err_submit_ww_acquire; 491 } 492 493 ret = etnaviv_cmdbuf_init(gpu->cmdbuf_suballoc, &submit->cmdbuf, 494 ALIGN(args->stream_size, 8) + 8); 495 if (ret) 496 goto err_submit_objects; 497 498 submit->cmdbuf.ctx = file->driver_priv; 499 submit->exec_state = args->exec_state; 500 submit->flags = args->flags; 501 502 ret = submit_lookup_objects(submit, file, bos, args->nr_bos); 503 if (ret) 504 goto err_submit_objects; 505 506 ret = submit_lock_objects(submit, &ticket); 507 if (ret) 508 goto err_submit_objects; 509 510 if (!etnaviv_cmd_validate_one(gpu, stream, args->stream_size / 4, 511 relocs, args->nr_relocs)) { 512 ret = -EINVAL; 513 goto err_submit_objects; 514 } 515 516 if (args->flags & ETNA_SUBMIT_FENCE_FD_IN) { 517 submit->in_fence = sync_file_get_fence(args->fence_fd); 518 if (!submit->in_fence) { 519 ret = -EINVAL; 520 goto err_submit_objects; 521 } 522 } 523 524 ret = submit_fence_sync(submit); 525 if (ret) 526 goto err_submit_objects; 527 528 ret = submit_pin_objects(submit); 529 if (ret) 530 goto err_submit_objects; 531 532 ret = submit_reloc(submit, stream, args->stream_size / 4, 533 relocs, args->nr_relocs); 534 if (ret) 535 goto err_submit_objects; 536 537 ret = submit_perfmon_validate(submit, args->exec_state, pmrs); 538 if (ret) 539 goto err_submit_objects; 540 541 memcpy(submit->cmdbuf.vaddr, stream, args->stream_size); 542 submit->cmdbuf.user_size = ALIGN(args->stream_size, 8); 543 544 ret = etnaviv_gpu_submit(gpu, submit); 545 if (ret) 546 goto err_submit_objects; 547 548 submit_attach_object_fences(submit); 549 550 if (args->flags & ETNA_SUBMIT_FENCE_FD_OUT) { 551 /* 552 * This can be improved: ideally we want to allocate the sync 553 * file before kicking off the GPU job and just attach the 554 * fence to the sync file here, eliminating the ENOMEM 555 * possibility at this stage. 556 */ 557 sync_file = sync_file_create(submit->out_fence); 558 if (!sync_file) { 559 ret = -ENOMEM; 560 goto err_submit_objects; 561 } 562 fd_install(out_fence_fd, sync_file->file); 563 } 564 565 args->fence_fd = out_fence_fd; 566 args->fence = submit->out_fence->seqno; 567 568 err_submit_objects: 569 etnaviv_submit_put(submit); 570 571 err_submit_ww_acquire: 572 ww_acquire_fini(&ticket); 573 574 err_submit_cmds: 575 if (ret && (out_fence_fd >= 0)) 576 put_unused_fd(out_fence_fd); 577 if (stream) 578 kvfree(stream); 579 if (bos) 580 kvfree(bos); 581 if (relocs) 582 kvfree(relocs); 583 if (pmrs) 584 kvfree(pmrs); 585 586 return ret; 587 } 588