nouveau_gem.c revision 7375c95b343aa575940704a38482a334ea87ac6c
1/* 2 * Copyright (C) 2008 Ben Skeggs. 3 * All Rights Reserved. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining 6 * a copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sublicense, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the 14 * next paragraph) shall be included in all copies or substantial 15 * portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE 21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 * 25 */ 26#include "drmP.h" 27#include "drm.h" 28 29#include "nouveau_drv.h" 30#include "nouveau_drm.h" 31#include "nouveau_dma.h" 32 33#define nouveau_gem_pushbuf_sync(chan) 0 34 35int 36nouveau_gem_object_new(struct drm_gem_object *gem) 37{ 38 return 0; 39} 40 41void 42nouveau_gem_object_del(struct drm_gem_object *gem) 43{ 44 struct nouveau_bo *nvbo = gem->driver_private; 45 struct ttm_buffer_object *bo = &nvbo->bo; 46 47 if (!nvbo) 48 return; 49 nvbo->gem = NULL; 50 51 if (unlikely(nvbo->pin_refcnt)) { 52 nvbo->pin_refcnt = 1; 53 nouveau_bo_unpin(nvbo); 54 } 55 56 ttm_bo_unref(&bo); 57 58 drm_gem_object_release(gem); 59 kfree(gem); 60} 61 62int 63nouveau_gem_object_open(struct drm_gem_object *gem, struct drm_file *file_priv) 64{ 65 struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv); 66 67 if (!fpriv->vm) 68 return 0; 69 70 return 0; 71} 72 73void 74nouveau_gem_object_close(struct drm_gem_object *gem, struct drm_file *file_priv) 75{ 76 struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv); 77 78 if (!fpriv->vm) 79 return; 80} 81 82int 83nouveau_gem_new(struct drm_device *dev, int size, int align, uint32_t domain, 84 uint32_t tile_mode, uint32_t tile_flags, 85 struct nouveau_bo **pnvbo) 86{ 87 struct drm_nouveau_private *dev_priv = dev->dev_private; 88 struct nouveau_bo *nvbo; 89 u32 flags = 0; 90 int ret; 91 92 if (domain & NOUVEAU_GEM_DOMAIN_VRAM) 93 flags |= TTM_PL_FLAG_VRAM; 94 if (domain & NOUVEAU_GEM_DOMAIN_GART) 95 flags |= TTM_PL_FLAG_TT; 96 if (!flags || domain & NOUVEAU_GEM_DOMAIN_CPU) 97 flags |= TTM_PL_FLAG_SYSTEM; 98 99 ret = nouveau_bo_new(dev, size, align, flags, tile_mode, 100 tile_flags, pnvbo); 101 if (ret) 102 return ret; 103 nvbo = *pnvbo; 104 105 /* we restrict allowed domains on nv50+ to only the types 106 * that were requested at creation time. not possibly on 107 * earlier chips without busting the ABI. 108 */ 109 nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_VRAM | 110 NOUVEAU_GEM_DOMAIN_GART; 111 if (dev_priv->card_type >= NV_50) 112 nvbo->valid_domains &= domain; 113 114 nvbo->gem = drm_gem_object_alloc(dev, nvbo->bo.mem.size); 115 if (!nvbo->gem) { 116 nouveau_bo_ref(NULL, pnvbo); 117 return -ENOMEM; 118 } 119 120 nvbo->bo.persistent_swap_storage = nvbo->gem->filp; 121 nvbo->gem->driver_private = nvbo; 122 return 0; 123} 124 125static int 126nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem, 127 struct drm_nouveau_gem_info *rep) 128{ 129 struct nouveau_fpriv *fpriv = nouveau_fpriv(file_priv); 130 struct nouveau_bo *nvbo = nouveau_gem_object(gem); 131 struct nouveau_vma *vma; 132 133 if (nvbo->bo.mem.mem_type == TTM_PL_TT) 134 rep->domain = NOUVEAU_GEM_DOMAIN_GART; 135 else 136 rep->domain = NOUVEAU_GEM_DOMAIN_VRAM; 137 138 rep->offset = nvbo->bo.offset; 139 if (fpriv->vm) { 140 vma = nouveau_bo_vma_find(nvbo, fpriv->vm); 141 if (!vma) 142 return -EINVAL; 143 144 rep->offset = vma->offset; 145 } 146 147 rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT; 148 rep->map_handle = nvbo->bo.addr_space_offset; 149 rep->tile_mode = nvbo->tile_mode; 150 rep->tile_flags = nvbo->tile_flags; 151 return 0; 152} 153 154int 155nouveau_gem_ioctl_new(struct drm_device *dev, void *data, 156 struct drm_file *file_priv) 157{ 158 struct drm_nouveau_private *dev_priv = dev->dev_private; 159 struct drm_nouveau_gem_new *req = data; 160 struct nouveau_bo *nvbo = NULL; 161 int ret = 0; 162 163 if (unlikely(dev_priv->ttm.bdev.dev_mapping == NULL)) 164 dev_priv->ttm.bdev.dev_mapping = dev_priv->dev->dev_mapping; 165 166 if (!dev_priv->engine.vram.flags_valid(dev, req->info.tile_flags)) { 167 NV_ERROR(dev, "bad page flags: 0x%08x\n", req->info.tile_flags); 168 return -EINVAL; 169 } 170 171 ret = nouveau_gem_new(dev, req->info.size, req->align, 172 req->info.domain, req->info.tile_mode, 173 req->info.tile_flags, &nvbo); 174 if (ret) 175 return ret; 176 177 ret = drm_gem_handle_create(file_priv, nvbo->gem, &req->info.handle); 178 if (ret == 0) { 179 ret = nouveau_gem_info(file_priv, nvbo->gem, &req->info); 180 if (ret) 181 drm_gem_handle_delete(file_priv, req->info.handle); 182 } 183 184 /* drop reference from allocate - handle holds it now */ 185 drm_gem_object_unreference_unlocked(nvbo->gem); 186 return ret; 187} 188 189static int 190nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains, 191 uint32_t write_domains, uint32_t valid_domains) 192{ 193 struct nouveau_bo *nvbo = gem->driver_private; 194 struct ttm_buffer_object *bo = &nvbo->bo; 195 uint32_t domains = valid_domains & nvbo->valid_domains & 196 (write_domains ? write_domains : read_domains); 197 uint32_t pref_flags = 0, valid_flags = 0; 198 199 if (!domains) 200 return -EINVAL; 201 202 if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) 203 valid_flags |= TTM_PL_FLAG_VRAM; 204 205 if (valid_domains & NOUVEAU_GEM_DOMAIN_GART) 206 valid_flags |= TTM_PL_FLAG_TT; 207 208 if ((domains & NOUVEAU_GEM_DOMAIN_VRAM) && 209 bo->mem.mem_type == TTM_PL_VRAM) 210 pref_flags |= TTM_PL_FLAG_VRAM; 211 212 else if ((domains & NOUVEAU_GEM_DOMAIN_GART) && 213 bo->mem.mem_type == TTM_PL_TT) 214 pref_flags |= TTM_PL_FLAG_TT; 215 216 else if (domains & NOUVEAU_GEM_DOMAIN_VRAM) 217 pref_flags |= TTM_PL_FLAG_VRAM; 218 219 else 220 pref_flags |= TTM_PL_FLAG_TT; 221 222 nouveau_bo_placement_set(nvbo, pref_flags, valid_flags); 223 224 return 0; 225} 226 227struct validate_op { 228 struct list_head vram_list; 229 struct list_head gart_list; 230 struct list_head both_list; 231}; 232 233static void 234validate_fini_list(struct list_head *list, struct nouveau_fence *fence) 235{ 236 struct list_head *entry, *tmp; 237 struct nouveau_bo *nvbo; 238 239 list_for_each_safe(entry, tmp, list) { 240 nvbo = list_entry(entry, struct nouveau_bo, entry); 241 242 nouveau_bo_fence(nvbo, fence); 243 244 if (unlikely(nvbo->validate_mapped)) { 245 ttm_bo_kunmap(&nvbo->kmap); 246 nvbo->validate_mapped = false; 247 } 248 249 list_del(&nvbo->entry); 250 nvbo->reserved_by = NULL; 251 ttm_bo_unreserve(&nvbo->bo); 252 drm_gem_object_unreference_unlocked(nvbo->gem); 253 } 254} 255 256static void 257validate_fini(struct validate_op *op, struct nouveau_fence* fence) 258{ 259 validate_fini_list(&op->vram_list, fence); 260 validate_fini_list(&op->gart_list, fence); 261 validate_fini_list(&op->both_list, fence); 262} 263 264static int 265validate_init(struct nouveau_channel *chan, struct drm_file *file_priv, 266 struct drm_nouveau_gem_pushbuf_bo *pbbo, 267 int nr_buffers, struct validate_op *op) 268{ 269 struct drm_device *dev = chan->dev; 270 struct drm_nouveau_private *dev_priv = dev->dev_private; 271 uint32_t sequence; 272 int trycnt = 0; 273 int ret, i; 274 275 sequence = atomic_add_return(1, &dev_priv->ttm.validate_sequence); 276retry: 277 if (++trycnt > 100000) { 278 NV_ERROR(dev, "%s failed and gave up.\n", __func__); 279 return -EINVAL; 280 } 281 282 for (i = 0; i < nr_buffers; i++) { 283 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i]; 284 struct drm_gem_object *gem; 285 struct nouveau_bo *nvbo; 286 287 gem = drm_gem_object_lookup(dev, file_priv, b->handle); 288 if (!gem) { 289 NV_ERROR(dev, "Unknown handle 0x%08x\n", b->handle); 290 validate_fini(op, NULL); 291 return -ENOENT; 292 } 293 nvbo = gem->driver_private; 294 295 if (nvbo->reserved_by && nvbo->reserved_by == file_priv) { 296 NV_ERROR(dev, "multiple instances of buffer %d on " 297 "validation list\n", b->handle); 298 validate_fini(op, NULL); 299 return -EINVAL; 300 } 301 302 ret = ttm_bo_reserve(&nvbo->bo, true, false, true, sequence); 303 if (ret) { 304 validate_fini(op, NULL); 305 if (unlikely(ret == -EAGAIN)) 306 ret = ttm_bo_wait_unreserved(&nvbo->bo, true); 307 drm_gem_object_unreference_unlocked(gem); 308 if (unlikely(ret)) { 309 if (ret != -ERESTARTSYS) 310 NV_ERROR(dev, "fail reserve\n"); 311 return ret; 312 } 313 goto retry; 314 } 315 316 b->user_priv = (uint64_t)(unsigned long)nvbo; 317 nvbo->reserved_by = file_priv; 318 nvbo->pbbo_index = i; 319 if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) && 320 (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)) 321 list_add_tail(&nvbo->entry, &op->both_list); 322 else 323 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) 324 list_add_tail(&nvbo->entry, &op->vram_list); 325 else 326 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART) 327 list_add_tail(&nvbo->entry, &op->gart_list); 328 else { 329 NV_ERROR(dev, "invalid valid domains: 0x%08x\n", 330 b->valid_domains); 331 list_add_tail(&nvbo->entry, &op->both_list); 332 validate_fini(op, NULL); 333 return -EINVAL; 334 } 335 } 336 337 return 0; 338} 339 340static int 341validate_list(struct nouveau_channel *chan, struct list_head *list, 342 struct drm_nouveau_gem_pushbuf_bo *pbbo, uint64_t user_pbbo_ptr) 343{ 344 struct drm_nouveau_private *dev_priv = chan->dev->dev_private; 345 struct drm_nouveau_gem_pushbuf_bo __user *upbbo = 346 (void __force __user *)(uintptr_t)user_pbbo_ptr; 347 struct drm_device *dev = chan->dev; 348 struct nouveau_bo *nvbo; 349 int ret, relocs = 0; 350 351 list_for_each_entry(nvbo, list, entry) { 352 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index]; 353 354 ret = nouveau_fence_sync(nvbo->bo.sync_obj, chan); 355 if (unlikely(ret)) { 356 NV_ERROR(dev, "fail pre-validate sync\n"); 357 return ret; 358 } 359 360 ret = nouveau_gem_set_domain(nvbo->gem, b->read_domains, 361 b->write_domains, 362 b->valid_domains); 363 if (unlikely(ret)) { 364 NV_ERROR(dev, "fail set_domain\n"); 365 return ret; 366 } 367 368 nvbo->channel = (b->read_domains & (1 << 31)) ? NULL : chan; 369 ret = nouveau_bo_validate(nvbo, true, false, false); 370 nvbo->channel = NULL; 371 if (unlikely(ret)) { 372 if (ret != -ERESTARTSYS) 373 NV_ERROR(dev, "fail ttm_validate\n"); 374 return ret; 375 } 376 377 ret = nouveau_fence_sync(nvbo->bo.sync_obj, chan); 378 if (unlikely(ret)) { 379 NV_ERROR(dev, "fail post-validate sync\n"); 380 return ret; 381 } 382 383 if (dev_priv->card_type < NV_50) { 384 if (nvbo->bo.offset == b->presumed.offset && 385 ((nvbo->bo.mem.mem_type == TTM_PL_VRAM && 386 b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) || 387 (nvbo->bo.mem.mem_type == TTM_PL_TT && 388 b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART))) 389 continue; 390 391 if (nvbo->bo.mem.mem_type == TTM_PL_TT) 392 b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART; 393 else 394 b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM; 395 b->presumed.offset = nvbo->bo.offset; 396 b->presumed.valid = 0; 397 relocs++; 398 399 if (DRM_COPY_TO_USER(&upbbo[nvbo->pbbo_index].presumed, 400 &b->presumed, sizeof(b->presumed))) 401 return -EFAULT; 402 } 403 } 404 405 return relocs; 406} 407 408static int 409nouveau_gem_pushbuf_validate(struct nouveau_channel *chan, 410 struct drm_file *file_priv, 411 struct drm_nouveau_gem_pushbuf_bo *pbbo, 412 uint64_t user_buffers, int nr_buffers, 413 struct validate_op *op, int *apply_relocs) 414{ 415 struct drm_device *dev = chan->dev; 416 int ret, relocs = 0; 417 418 INIT_LIST_HEAD(&op->vram_list); 419 INIT_LIST_HEAD(&op->gart_list); 420 INIT_LIST_HEAD(&op->both_list); 421 422 if (nr_buffers == 0) 423 return 0; 424 425 ret = validate_init(chan, file_priv, pbbo, nr_buffers, op); 426 if (unlikely(ret)) { 427 if (ret != -ERESTARTSYS) 428 NV_ERROR(dev, "validate_init\n"); 429 return ret; 430 } 431 432 ret = validate_list(chan, &op->vram_list, pbbo, user_buffers); 433 if (unlikely(ret < 0)) { 434 if (ret != -ERESTARTSYS) 435 NV_ERROR(dev, "validate vram_list\n"); 436 validate_fini(op, NULL); 437 return ret; 438 } 439 relocs += ret; 440 441 ret = validate_list(chan, &op->gart_list, pbbo, user_buffers); 442 if (unlikely(ret < 0)) { 443 if (ret != -ERESTARTSYS) 444 NV_ERROR(dev, "validate gart_list\n"); 445 validate_fini(op, NULL); 446 return ret; 447 } 448 relocs += ret; 449 450 ret = validate_list(chan, &op->both_list, pbbo, user_buffers); 451 if (unlikely(ret < 0)) { 452 if (ret != -ERESTARTSYS) 453 NV_ERROR(dev, "validate both_list\n"); 454 validate_fini(op, NULL); 455 return ret; 456 } 457 relocs += ret; 458 459 *apply_relocs = relocs; 460 return 0; 461} 462 463static inline void * 464u_memcpya(uint64_t user, unsigned nmemb, unsigned size) 465{ 466 void *mem; 467 void __user *userptr = (void __force __user *)(uintptr_t)user; 468 469 mem = kmalloc(nmemb * size, GFP_KERNEL); 470 if (!mem) 471 return ERR_PTR(-ENOMEM); 472 473 if (DRM_COPY_FROM_USER(mem, userptr, nmemb * size)) { 474 kfree(mem); 475 return ERR_PTR(-EFAULT); 476 } 477 478 return mem; 479} 480 481static int 482nouveau_gem_pushbuf_reloc_apply(struct drm_device *dev, 483 struct drm_nouveau_gem_pushbuf *req, 484 struct drm_nouveau_gem_pushbuf_bo *bo) 485{ 486 struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL; 487 int ret = 0; 488 unsigned i; 489 490 reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc)); 491 if (IS_ERR(reloc)) 492 return PTR_ERR(reloc); 493 494 for (i = 0; i < req->nr_relocs; i++) { 495 struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i]; 496 struct drm_nouveau_gem_pushbuf_bo *b; 497 struct nouveau_bo *nvbo; 498 uint32_t data; 499 500 if (unlikely(r->bo_index > req->nr_buffers)) { 501 NV_ERROR(dev, "reloc bo index invalid\n"); 502 ret = -EINVAL; 503 break; 504 } 505 506 b = &bo[r->bo_index]; 507 if (b->presumed.valid) 508 continue; 509 510 if (unlikely(r->reloc_bo_index > req->nr_buffers)) { 511 NV_ERROR(dev, "reloc container bo index invalid\n"); 512 ret = -EINVAL; 513 break; 514 } 515 nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv; 516 517 if (unlikely(r->reloc_bo_offset + 4 > 518 nvbo->bo.mem.num_pages << PAGE_SHIFT)) { 519 NV_ERROR(dev, "reloc outside of bo\n"); 520 ret = -EINVAL; 521 break; 522 } 523 524 if (!nvbo->kmap.virtual) { 525 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages, 526 &nvbo->kmap); 527 if (ret) { 528 NV_ERROR(dev, "failed kmap for reloc\n"); 529 break; 530 } 531 nvbo->validate_mapped = true; 532 } 533 534 if (r->flags & NOUVEAU_GEM_RELOC_LOW) 535 data = b->presumed.offset + r->data; 536 else 537 if (r->flags & NOUVEAU_GEM_RELOC_HIGH) 538 data = (b->presumed.offset + r->data) >> 32; 539 else 540 data = r->data; 541 542 if (r->flags & NOUVEAU_GEM_RELOC_OR) { 543 if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART) 544 data |= r->tor; 545 else 546 data |= r->vor; 547 } 548 549 spin_lock(&nvbo->bo.bdev->fence_lock); 550 ret = ttm_bo_wait(&nvbo->bo, false, false, false); 551 spin_unlock(&nvbo->bo.bdev->fence_lock); 552 if (ret) { 553 NV_ERROR(dev, "reloc wait_idle failed: %d\n", ret); 554 break; 555 } 556 557 nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data); 558 } 559 560 kfree(reloc); 561 return ret; 562} 563 564int 565nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data, 566 struct drm_file *file_priv) 567{ 568 struct drm_nouveau_private *dev_priv = dev->dev_private; 569 struct drm_nouveau_gem_pushbuf *req = data; 570 struct drm_nouveau_gem_pushbuf_push *push; 571 struct drm_nouveau_gem_pushbuf_bo *bo; 572 struct nouveau_channel *chan; 573 struct validate_op op; 574 struct nouveau_fence *fence = NULL; 575 int i, j, ret = 0, do_reloc = 0; 576 577 chan = nouveau_channel_get(file_priv, req->channel); 578 if (IS_ERR(chan)) 579 return PTR_ERR(chan); 580 581 req->vram_available = dev_priv->fb_aper_free; 582 req->gart_available = dev_priv->gart_info.aper_free; 583 if (unlikely(req->nr_push == 0)) 584 goto out_next; 585 586 if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) { 587 NV_ERROR(dev, "pushbuf push count exceeds limit: %d max %d\n", 588 req->nr_push, NOUVEAU_GEM_MAX_PUSH); 589 nouveau_channel_put(&chan); 590 return -EINVAL; 591 } 592 593 if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) { 594 NV_ERROR(dev, "pushbuf bo count exceeds limit: %d max %d\n", 595 req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS); 596 nouveau_channel_put(&chan); 597 return -EINVAL; 598 } 599 600 if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) { 601 NV_ERROR(dev, "pushbuf reloc count exceeds limit: %d max %d\n", 602 req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS); 603 nouveau_channel_put(&chan); 604 return -EINVAL; 605 } 606 607 push = u_memcpya(req->push, req->nr_push, sizeof(*push)); 608 if (IS_ERR(push)) { 609 nouveau_channel_put(&chan); 610 return PTR_ERR(push); 611 } 612 613 bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo)); 614 if (IS_ERR(bo)) { 615 kfree(push); 616 nouveau_channel_put(&chan); 617 return PTR_ERR(bo); 618 } 619 620 /* Mark push buffers as being used on PFIFO, the validation code 621 * will then make sure that if the pushbuf bo moves, that they 622 * happen on the kernel channel, which will in turn cause a sync 623 * to happen before we try and submit the push buffer. 624 */ 625 for (i = 0; i < req->nr_push; i++) { 626 if (push[i].bo_index >= req->nr_buffers) { 627 NV_ERROR(dev, "push %d buffer not in list\n", i); 628 ret = -EINVAL; 629 goto out_prevalid; 630 } 631 632 bo[push[i].bo_index].read_domains |= (1 << 31); 633 } 634 635 /* Validate buffer list */ 636 ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers, 637 req->nr_buffers, &op, &do_reloc); 638 if (ret) { 639 if (ret != -ERESTARTSYS) 640 NV_ERROR(dev, "validate: %d\n", ret); 641 goto out_prevalid; 642 } 643 644 /* Apply any relocations that are required */ 645 if (do_reloc) { 646 ret = nouveau_gem_pushbuf_reloc_apply(dev, req, bo); 647 if (ret) { 648 NV_ERROR(dev, "reloc apply: %d\n", ret); 649 goto out; 650 } 651 } 652 653 if (chan->dma.ib_max) { 654 ret = nouveau_dma_wait(chan, req->nr_push + 1, 6); 655 if (ret) { 656 NV_INFO(dev, "nv50cal_space: %d\n", ret); 657 goto out; 658 } 659 660 for (i = 0; i < req->nr_push; i++) { 661 struct nouveau_bo *nvbo = (void *)(unsigned long) 662 bo[push[i].bo_index].user_priv; 663 664 nv50_dma_push(chan, nvbo, push[i].offset, 665 push[i].length); 666 } 667 } else 668 if (dev_priv->chipset >= 0x25) { 669 ret = RING_SPACE(chan, req->nr_push * 2); 670 if (ret) { 671 NV_ERROR(dev, "cal_space: %d\n", ret); 672 goto out; 673 } 674 675 for (i = 0; i < req->nr_push; i++) { 676 struct nouveau_bo *nvbo = (void *)(unsigned long) 677 bo[push[i].bo_index].user_priv; 678 struct drm_mm_node *mem = nvbo->bo.mem.mm_node; 679 680 OUT_RING(chan, ((mem->start << PAGE_SHIFT) + 681 push[i].offset) | 2); 682 OUT_RING(chan, 0); 683 } 684 } else { 685 ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS)); 686 if (ret) { 687 NV_ERROR(dev, "jmp_space: %d\n", ret); 688 goto out; 689 } 690 691 for (i = 0; i < req->nr_push; i++) { 692 struct nouveau_bo *nvbo = (void *)(unsigned long) 693 bo[push[i].bo_index].user_priv; 694 struct drm_mm_node *mem = nvbo->bo.mem.mm_node; 695 uint32_t cmd; 696 697 cmd = chan->pushbuf_base + ((chan->dma.cur + 2) << 2); 698 cmd |= 0x20000000; 699 if (unlikely(cmd != req->suffix0)) { 700 if (!nvbo->kmap.virtual) { 701 ret = ttm_bo_kmap(&nvbo->bo, 0, 702 nvbo->bo.mem. 703 num_pages, 704 &nvbo->kmap); 705 if (ret) { 706 WIND_RING(chan); 707 goto out; 708 } 709 nvbo->validate_mapped = true; 710 } 711 712 nouveau_bo_wr32(nvbo, (push[i].offset + 713 push[i].length - 8) / 4, cmd); 714 } 715 716 OUT_RING(chan, ((mem->start << PAGE_SHIFT) + 717 push[i].offset) | 0x20000000); 718 OUT_RING(chan, 0); 719 for (j = 0; j < NOUVEAU_DMA_SKIPS; j++) 720 OUT_RING(chan, 0); 721 } 722 } 723 724 ret = nouveau_fence_new(chan, &fence, true); 725 if (ret) { 726 NV_ERROR(dev, "error fencing pushbuf: %d\n", ret); 727 WIND_RING(chan); 728 goto out; 729 } 730 731out: 732 validate_fini(&op, fence); 733 nouveau_fence_unref(&fence); 734 735out_prevalid: 736 kfree(bo); 737 kfree(push); 738 739out_next: 740 if (chan->dma.ib_max) { 741 req->suffix0 = 0x00000000; 742 req->suffix1 = 0x00000000; 743 } else 744 if (dev_priv->chipset >= 0x25) { 745 req->suffix0 = 0x00020000; 746 req->suffix1 = 0x00000000; 747 } else { 748 req->suffix0 = 0x20000000 | 749 (chan->pushbuf_base + ((chan->dma.cur + 2) << 2)); 750 req->suffix1 = 0x00000000; 751 } 752 753 nouveau_channel_put(&chan); 754 return ret; 755} 756 757static inline uint32_t 758domain_to_ttm(struct nouveau_bo *nvbo, uint32_t domain) 759{ 760 uint32_t flags = 0; 761 762 if (domain & NOUVEAU_GEM_DOMAIN_VRAM) 763 flags |= TTM_PL_FLAG_VRAM; 764 if (domain & NOUVEAU_GEM_DOMAIN_GART) 765 flags |= TTM_PL_FLAG_TT; 766 767 return flags; 768} 769 770int 771nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data, 772 struct drm_file *file_priv) 773{ 774 struct drm_nouveau_gem_cpu_prep *req = data; 775 struct drm_gem_object *gem; 776 struct nouveau_bo *nvbo; 777 bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT); 778 int ret = -EINVAL; 779 780 gem = drm_gem_object_lookup(dev, file_priv, req->handle); 781 if (!gem) 782 return -ENOENT; 783 nvbo = nouveau_gem_object(gem); 784 785 spin_lock(&nvbo->bo.bdev->fence_lock); 786 ret = ttm_bo_wait(&nvbo->bo, true, true, no_wait); 787 spin_unlock(&nvbo->bo.bdev->fence_lock); 788 drm_gem_object_unreference_unlocked(gem); 789 return ret; 790} 791 792int 793nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data, 794 struct drm_file *file_priv) 795{ 796 return 0; 797} 798 799int 800nouveau_gem_ioctl_info(struct drm_device *dev, void *data, 801 struct drm_file *file_priv) 802{ 803 struct drm_nouveau_gem_info *req = data; 804 struct drm_gem_object *gem; 805 int ret; 806 807 gem = drm_gem_object_lookup(dev, file_priv, req->handle); 808 if (!gem) 809 return -ENOENT; 810 811 ret = nouveau_gem_info(file_priv, gem, req); 812 drm_gem_object_unreference_unlocked(gem); 813 return ret; 814} 815 816