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