2 * Copyright (C) 2008 Ben Skeggs.
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:
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.
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.
27 #include "nouveau_drv.h"
28 #include "nouveau_dma.h"
29 #include "nouveau_fence.h"
30 #include "nouveau_abi16.h"
32 #include "nouveau_ttm.h"
33 #include "nouveau_gem.h"
34 #include "nouveau_mem.h"
35 #include "nouveau_vmm.h"
37 #include <nvif/class.h>
40 nouveau_gem_object_del(struct drm_gem_object *gem)
42 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
43 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
44 struct ttm_buffer_object *bo = &nvbo->bo;
45 struct device *dev = drm->dev->dev;
48 ret = pm_runtime_get_sync(dev);
49 if (WARN_ON(ret < 0 && ret != -EACCES)) {
50 pm_runtime_put_autosuspend(dev);
54 if (gem->import_attach)
55 drm_prime_gem_destroy(gem, nvbo->bo.sg);
57 drm_gem_object_release(gem);
59 /* reset filp so nouveau_bo_del_ttm() can test for it */
63 pm_runtime_mark_last_busy(dev);
64 pm_runtime_put_autosuspend(dev);
68 nouveau_gem_object_open(struct drm_gem_object *gem, struct drm_file *file_priv)
70 struct nouveau_cli *cli = nouveau_cli(file_priv);
71 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
72 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
73 struct device *dev = drm->dev->dev;
74 struct nouveau_vma *vma;
77 if (cli->vmm.vmm.object.oclass < NVIF_CLASS_VMM_NV50)
80 ret = ttm_bo_reserve(&nvbo->bo, false, false, NULL);
84 ret = pm_runtime_get_sync(dev);
85 if (ret < 0 && ret != -EACCES) {
86 pm_runtime_put_autosuspend(dev);
90 ret = nouveau_vma_new(nvbo, &cli->vmm, &vma);
91 pm_runtime_mark_last_busy(dev);
92 pm_runtime_put_autosuspend(dev);
94 ttm_bo_unreserve(&nvbo->bo);
98 struct nouveau_gem_object_unmap {
99 struct nouveau_cli_work work;
100 struct nouveau_vma *vma;
104 nouveau_gem_object_delete(struct nouveau_vma *vma)
106 nouveau_fence_unref(&vma->fence);
107 nouveau_vma_del(&vma);
111 nouveau_gem_object_delete_work(struct nouveau_cli_work *w)
113 struct nouveau_gem_object_unmap *work =
114 container_of(w, typeof(*work), work);
115 nouveau_gem_object_delete(work->vma);
120 nouveau_gem_object_unmap(struct nouveau_bo *nvbo, struct nouveau_vma *vma)
122 struct dma_fence *fence = vma->fence ? &vma->fence->base : NULL;
123 struct nouveau_gem_object_unmap *work;
125 list_del_init(&vma->head);
128 nouveau_gem_object_delete(vma);
132 if (!(work = kmalloc(sizeof(*work), GFP_KERNEL))) {
133 WARN_ON(dma_fence_wait_timeout(fence, false, 2 * HZ) <= 0);
134 nouveau_gem_object_delete(vma);
138 work->work.func = nouveau_gem_object_delete_work;
140 nouveau_cli_work_queue(vma->vmm->cli, fence, &work->work);
144 nouveau_gem_object_close(struct drm_gem_object *gem, struct drm_file *file_priv)
146 struct nouveau_cli *cli = nouveau_cli(file_priv);
147 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
148 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
149 struct device *dev = drm->dev->dev;
150 struct nouveau_vma *vma;
153 if (cli->vmm.vmm.object.oclass < NVIF_CLASS_VMM_NV50)
156 ret = ttm_bo_reserve(&nvbo->bo, false, false, NULL);
160 vma = nouveau_vma_find(nvbo, &cli->vmm);
162 if (--vma->refs == 0) {
163 ret = pm_runtime_get_sync(dev);
164 if (!WARN_ON(ret < 0 && ret != -EACCES)) {
165 nouveau_gem_object_unmap(nvbo, vma);
166 pm_runtime_mark_last_busy(dev);
167 pm_runtime_put_autosuspend(dev);
171 ttm_bo_unreserve(&nvbo->bo);
175 nouveau_gem_new(struct nouveau_cli *cli, u64 size, int align, uint32_t domain,
176 uint32_t tile_mode, uint32_t tile_flags,
177 struct nouveau_bo **pnvbo)
179 struct nouveau_drm *drm = cli->drm;
180 struct nouveau_bo *nvbo;
184 if (domain & NOUVEAU_GEM_DOMAIN_VRAM)
185 flags |= TTM_PL_FLAG_VRAM;
186 if (domain & NOUVEAU_GEM_DOMAIN_GART)
187 flags |= TTM_PL_FLAG_TT;
188 if (!flags || domain & NOUVEAU_GEM_DOMAIN_CPU)
189 flags |= TTM_PL_FLAG_SYSTEM;
191 if (domain & NOUVEAU_GEM_DOMAIN_COHERENT)
192 flags |= TTM_PL_FLAG_UNCACHED;
194 ret = nouveau_bo_new(cli, size, align, flags, tile_mode,
195 tile_flags, NULL, NULL, pnvbo);
200 /* we restrict allowed domains on nv50+ to only the types
201 * that were requested at creation time. not possibly on
202 * earlier chips without busting the ABI.
204 nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_VRAM |
205 NOUVEAU_GEM_DOMAIN_GART;
206 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA)
207 nvbo->valid_domains &= domain;
209 /* Initialize the embedded gem-object. We return a single gem-reference
210 * to the caller, instead of a normal nouveau_bo ttm reference. */
211 ret = drm_gem_object_init(drm->dev, &nvbo->gem, nvbo->bo.mem.size);
213 nouveau_bo_ref(NULL, pnvbo);
217 nvbo->bo.persistent_swap_storage = nvbo->gem.filp;
222 nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem,
223 struct drm_nouveau_gem_info *rep)
225 struct nouveau_cli *cli = nouveau_cli(file_priv);
226 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
227 struct nouveau_vma *vma;
229 if (is_power_of_2(nvbo->valid_domains))
230 rep->domain = nvbo->valid_domains;
231 else if (nvbo->bo.mem.mem_type == TTM_PL_TT)
232 rep->domain = NOUVEAU_GEM_DOMAIN_GART;
234 rep->domain = NOUVEAU_GEM_DOMAIN_VRAM;
235 rep->offset = nvbo->bo.offset;
236 if (cli->vmm.vmm.object.oclass >= NVIF_CLASS_VMM_NV50) {
237 vma = nouveau_vma_find(nvbo, &cli->vmm);
241 rep->offset = vma->addr;
244 rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
245 rep->map_handle = drm_vma_node_offset_addr(&nvbo->bo.vma_node);
246 rep->tile_mode = nvbo->mode;
247 rep->tile_flags = nvbo->contig ? 0 : NOUVEAU_GEM_TILE_NONCONTIG;
248 if (cli->device.info.family >= NV_DEVICE_INFO_V0_FERMI)
249 rep->tile_flags |= nvbo->kind << 8;
251 if (cli->device.info.family >= NV_DEVICE_INFO_V0_TESLA)
252 rep->tile_flags |= nvbo->kind << 8 | nvbo->comp << 16;
254 rep->tile_flags |= nvbo->zeta;
259 nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
260 struct drm_file *file_priv)
262 struct nouveau_cli *cli = nouveau_cli(file_priv);
263 struct drm_nouveau_gem_new *req = data;
264 struct nouveau_bo *nvbo = NULL;
267 ret = nouveau_gem_new(cli, req->info.size, req->align,
268 req->info.domain, req->info.tile_mode,
269 req->info.tile_flags, &nvbo);
273 ret = drm_gem_handle_create(file_priv, &nvbo->gem, &req->info.handle);
275 ret = nouveau_gem_info(file_priv, &nvbo->gem, &req->info);
277 drm_gem_handle_delete(file_priv, req->info.handle);
280 /* drop reference from allocate - handle holds it now */
281 drm_gem_object_put_unlocked(&nvbo->gem);
286 nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains,
287 uint32_t write_domains, uint32_t valid_domains)
289 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
290 struct ttm_buffer_object *bo = &nvbo->bo;
291 uint32_t domains = valid_domains & nvbo->valid_domains &
292 (write_domains ? write_domains : read_domains);
293 uint32_t pref_flags = 0, valid_flags = 0;
298 if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
299 valid_flags |= TTM_PL_FLAG_VRAM;
301 if (valid_domains & NOUVEAU_GEM_DOMAIN_GART)
302 valid_flags |= TTM_PL_FLAG_TT;
304 if ((domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
305 bo->mem.mem_type == TTM_PL_VRAM)
306 pref_flags |= TTM_PL_FLAG_VRAM;
308 else if ((domains & NOUVEAU_GEM_DOMAIN_GART) &&
309 bo->mem.mem_type == TTM_PL_TT)
310 pref_flags |= TTM_PL_FLAG_TT;
312 else if (domains & NOUVEAU_GEM_DOMAIN_VRAM)
313 pref_flags |= TTM_PL_FLAG_VRAM;
316 pref_flags |= TTM_PL_FLAG_TT;
318 nouveau_bo_placement_set(nvbo, pref_flags, valid_flags);
324 struct list_head list;
325 struct ww_acquire_ctx ticket;
329 validate_fini_no_ticket(struct validate_op *op, struct nouveau_fence *fence,
330 struct drm_nouveau_gem_pushbuf_bo *pbbo)
332 struct nouveau_bo *nvbo;
333 struct drm_nouveau_gem_pushbuf_bo *b;
335 while (!list_empty(&op->list)) {
336 nvbo = list_entry(op->list.next, struct nouveau_bo, entry);
337 b = &pbbo[nvbo->pbbo_index];
340 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
341 struct nouveau_vma *vma;
343 nouveau_bo_fence(nvbo, fence, !!b->write_domains);
345 if (drm->client.vmm.vmm.object.oclass >= NVIF_CLASS_VMM_NV50) {
346 vma = (void *)(unsigned long)b->user_priv;
347 nouveau_fence_unref(&vma->fence);
348 dma_fence_get(&fence->base);
353 if (unlikely(nvbo->validate_mapped)) {
354 ttm_bo_kunmap(&nvbo->kmap);
355 nvbo->validate_mapped = false;
358 list_del(&nvbo->entry);
359 nvbo->reserved_by = NULL;
360 ttm_bo_unreserve(&nvbo->bo);
361 drm_gem_object_put_unlocked(&nvbo->gem);
366 validate_fini(struct validate_op *op, struct nouveau_fence *fence,
367 struct drm_nouveau_gem_pushbuf_bo *pbbo)
369 validate_fini_no_ticket(op, fence, pbbo);
370 ww_acquire_fini(&op->ticket);
374 validate_init(struct nouveau_channel *chan, struct drm_file *file_priv,
375 struct drm_nouveau_gem_pushbuf_bo *pbbo,
376 int nr_buffers, struct validate_op *op)
378 struct nouveau_cli *cli = nouveau_cli(file_priv);
380 int ret = -EINVAL, i;
381 struct nouveau_bo *res_bo = NULL;
382 LIST_HEAD(gart_list);
383 LIST_HEAD(vram_list);
384 LIST_HEAD(both_list);
386 ww_acquire_init(&op->ticket, &reservation_ww_class);
388 if (++trycnt > 100000) {
389 NV_PRINTK(err, cli, "%s failed and gave up.\n", __func__);
393 for (i = 0; i < nr_buffers; i++) {
394 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
395 struct drm_gem_object *gem;
396 struct nouveau_bo *nvbo;
398 gem = drm_gem_object_lookup(file_priv, b->handle);
400 NV_PRINTK(err, cli, "Unknown handle 0x%08x\n", b->handle);
404 nvbo = nouveau_gem_object(gem);
405 if (nvbo == res_bo) {
407 drm_gem_object_put_unlocked(gem);
411 if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
412 NV_PRINTK(err, cli, "multiple instances of buffer %d on "
413 "validation list\n", b->handle);
414 drm_gem_object_put_unlocked(gem);
419 ret = ttm_bo_reserve(&nvbo->bo, true, false, &op->ticket);
421 list_splice_tail_init(&vram_list, &op->list);
422 list_splice_tail_init(&gart_list, &op->list);
423 list_splice_tail_init(&both_list, &op->list);
424 validate_fini_no_ticket(op, NULL, NULL);
425 if (unlikely(ret == -EDEADLK)) {
426 ret = ttm_bo_reserve_slowpath(&nvbo->bo, true,
432 if (ret != -ERESTARTSYS)
433 NV_PRINTK(err, cli, "fail reserve\n");
438 if (cli->vmm.vmm.object.oclass >= NVIF_CLASS_VMM_NV50) {
439 struct nouveau_vmm *vmm = &cli->vmm;
440 struct nouveau_vma *vma = nouveau_vma_find(nvbo, vmm);
442 NV_PRINTK(err, cli, "vma not found!\n");
447 b->user_priv = (uint64_t)(unsigned long)vma;
449 b->user_priv = (uint64_t)(unsigned long)nvbo;
452 nvbo->reserved_by = file_priv;
453 nvbo->pbbo_index = i;
454 if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
455 (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
456 list_add_tail(&nvbo->entry, &both_list);
458 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
459 list_add_tail(&nvbo->entry, &vram_list);
461 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
462 list_add_tail(&nvbo->entry, &gart_list);
464 NV_PRINTK(err, cli, "invalid valid domains: 0x%08x\n",
466 list_add_tail(&nvbo->entry, &both_list);
474 ww_acquire_done(&op->ticket);
475 list_splice_tail(&vram_list, &op->list);
476 list_splice_tail(&gart_list, &op->list);
477 list_splice_tail(&both_list, &op->list);
479 validate_fini(op, NULL, NULL);
485 validate_list(struct nouveau_channel *chan, struct nouveau_cli *cli,
486 struct list_head *list, struct drm_nouveau_gem_pushbuf_bo *pbbo,
487 uint64_t user_pbbo_ptr)
489 struct nouveau_drm *drm = chan->drm;
490 struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
491 (void __force __user *)(uintptr_t)user_pbbo_ptr;
492 struct nouveau_bo *nvbo;
495 list_for_each_entry(nvbo, list, entry) {
496 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
498 ret = nouveau_gem_set_domain(&nvbo->gem, b->read_domains,
502 NV_PRINTK(err, cli, "fail set_domain\n");
506 ret = nouveau_bo_validate(nvbo, true, false);
508 if (ret != -ERESTARTSYS)
509 NV_PRINTK(err, cli, "fail ttm_validate\n");
513 ret = nouveau_fence_sync(nvbo, chan, !!b->write_domains, true);
515 if (ret != -ERESTARTSYS)
516 NV_PRINTK(err, cli, "fail post-validate sync\n");
520 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
521 if (nvbo->bo.offset == b->presumed.offset &&
522 ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
523 b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
524 (nvbo->bo.mem.mem_type == TTM_PL_TT &&
525 b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART)))
528 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
529 b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART;
531 b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM;
532 b->presumed.offset = nvbo->bo.offset;
533 b->presumed.valid = 0;
536 if (copy_to_user(&upbbo[nvbo->pbbo_index].presumed,
537 &b->presumed, sizeof(b->presumed)))
546 nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
547 struct drm_file *file_priv,
548 struct drm_nouveau_gem_pushbuf_bo *pbbo,
549 uint64_t user_buffers, int nr_buffers,
550 struct validate_op *op, int *apply_relocs)
552 struct nouveau_cli *cli = nouveau_cli(file_priv);
555 INIT_LIST_HEAD(&op->list);
560 ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
562 if (ret != -ERESTARTSYS)
563 NV_PRINTK(err, cli, "validate_init\n");
567 ret = validate_list(chan, cli, &op->list, pbbo, user_buffers);
568 if (unlikely(ret < 0)) {
569 if (ret != -ERESTARTSYS)
570 NV_PRINTK(err, cli, "validating bo list\n");
571 validate_fini(op, NULL, NULL);
585 u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
588 void __user *userptr = (void __force __user *)(uintptr_t)user;
592 mem = kvmalloc(size, GFP_KERNEL);
594 return ERR_PTR(-ENOMEM);
596 if (copy_from_user(mem, userptr, size)) {
598 return ERR_PTR(-EFAULT);
605 nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli,
606 struct drm_nouveau_gem_pushbuf *req,
607 struct drm_nouveau_gem_pushbuf_bo *bo)
609 struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
613 reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
615 return PTR_ERR(reloc);
617 for (i = 0; i < req->nr_relocs; i++) {
618 struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
619 struct drm_nouveau_gem_pushbuf_bo *b;
620 struct nouveau_bo *nvbo;
623 if (unlikely(r->bo_index >= req->nr_buffers)) {
624 NV_PRINTK(err, cli, "reloc bo index invalid\n");
629 b = &bo[r->bo_index];
630 if (b->presumed.valid)
633 if (unlikely(r->reloc_bo_index >= req->nr_buffers)) {
634 NV_PRINTK(err, cli, "reloc container bo index invalid\n");
638 nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv;
640 if (unlikely(r->reloc_bo_offset + 4 >
641 nvbo->bo.mem.num_pages << PAGE_SHIFT)) {
642 NV_PRINTK(err, cli, "reloc outside of bo\n");
647 if (!nvbo->kmap.virtual) {
648 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
651 NV_PRINTK(err, cli, "failed kmap for reloc\n");
654 nvbo->validate_mapped = true;
657 if (r->flags & NOUVEAU_GEM_RELOC_LOW)
658 data = b->presumed.offset + r->data;
660 if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
661 data = (b->presumed.offset + r->data) >> 32;
665 if (r->flags & NOUVEAU_GEM_RELOC_OR) {
666 if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
672 ret = ttm_bo_wait(&nvbo->bo, false, false);
674 NV_PRINTK(err, cli, "reloc wait_idle failed: %d\n", ret);
678 nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
686 nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
687 struct drm_file *file_priv)
689 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
690 struct nouveau_cli *cli = nouveau_cli(file_priv);
691 struct nouveau_abi16_chan *temp;
692 struct nouveau_drm *drm = nouveau_drm(dev);
693 struct drm_nouveau_gem_pushbuf *req = data;
694 struct drm_nouveau_gem_pushbuf_push *push;
695 struct drm_nouveau_gem_pushbuf_bo *bo;
696 struct nouveau_channel *chan = NULL;
697 struct validate_op op;
698 struct nouveau_fence *fence = NULL;
699 int i, j, ret = 0, do_reloc = 0;
701 if (unlikely(!abi16))
704 list_for_each_entry(temp, &abi16->channels, head) {
705 if (temp->chan->chid == req->channel) {
712 return nouveau_abi16_put(abi16, -ENOENT);
714 req->vram_available = drm->gem.vram_available;
715 req->gart_available = drm->gem.gart_available;
716 if (unlikely(req->nr_push == 0))
719 if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
720 NV_PRINTK(err, cli, "pushbuf push count exceeds limit: %d max %d\n",
721 req->nr_push, NOUVEAU_GEM_MAX_PUSH);
722 return nouveau_abi16_put(abi16, -EINVAL);
725 if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
726 NV_PRINTK(err, cli, "pushbuf bo count exceeds limit: %d max %d\n",
727 req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
728 return nouveau_abi16_put(abi16, -EINVAL);
731 if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
732 NV_PRINTK(err, cli, "pushbuf reloc count exceeds limit: %d max %d\n",
733 req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
734 return nouveau_abi16_put(abi16, -EINVAL);
737 push = u_memcpya(req->push, req->nr_push, sizeof(*push));
739 return nouveau_abi16_put(abi16, PTR_ERR(push));
741 bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
744 return nouveau_abi16_put(abi16, PTR_ERR(bo));
747 /* Ensure all push buffers are on validate list */
748 for (i = 0; i < req->nr_push; i++) {
749 if (push[i].bo_index >= req->nr_buffers) {
750 NV_PRINTK(err, cli, "push %d buffer not in list\n", i);
756 /* Validate buffer list */
757 ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
758 req->nr_buffers, &op, &do_reloc);
760 if (ret != -ERESTARTSYS)
761 NV_PRINTK(err, cli, "validate: %d\n", ret);
765 /* Apply any relocations that are required */
767 ret = nouveau_gem_pushbuf_reloc_apply(cli, req, bo);
769 NV_PRINTK(err, cli, "reloc apply: %d\n", ret);
774 if (chan->dma.ib_max) {
775 ret = nouveau_dma_wait(chan, req->nr_push + 1, 16);
777 NV_PRINTK(err, cli, "nv50cal_space: %d\n", ret);
781 for (i = 0; i < req->nr_push; i++) {
782 struct nouveau_vma *vma = (void *)(unsigned long)
783 bo[push[i].bo_index].user_priv;
785 nv50_dma_push(chan, vma->addr + push[i].offset,
789 if (drm->client.device.info.chipset >= 0x25) {
790 ret = RING_SPACE(chan, req->nr_push * 2);
792 NV_PRINTK(err, cli, "cal_space: %d\n", ret);
796 for (i = 0; i < req->nr_push; i++) {
797 struct nouveau_bo *nvbo = (void *)(unsigned long)
798 bo[push[i].bo_index].user_priv;
800 OUT_RING(chan, (nvbo->bo.offset + push[i].offset) | 2);
804 ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
806 NV_PRINTK(err, cli, "jmp_space: %d\n", ret);
810 for (i = 0; i < req->nr_push; i++) {
811 struct nouveau_bo *nvbo = (void *)(unsigned long)
812 bo[push[i].bo_index].user_priv;
815 cmd = chan->push.addr + ((chan->dma.cur + 2) << 2);
817 if (unlikely(cmd != req->suffix0)) {
818 if (!nvbo->kmap.virtual) {
819 ret = ttm_bo_kmap(&nvbo->bo, 0,
827 nvbo->validate_mapped = true;
830 nouveau_bo_wr32(nvbo, (push[i].offset +
831 push[i].length - 8) / 4, cmd);
834 OUT_RING(chan, 0x20000000 |
835 (nvbo->bo.offset + push[i].offset));
837 for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
842 ret = nouveau_fence_new(chan, false, &fence);
844 NV_PRINTK(err, cli, "error fencing pushbuf: %d\n", ret);
850 validate_fini(&op, fence, bo);
851 nouveau_fence_unref(&fence);
858 if (chan->dma.ib_max) {
859 req->suffix0 = 0x00000000;
860 req->suffix1 = 0x00000000;
862 if (drm->client.device.info.chipset >= 0x25) {
863 req->suffix0 = 0x00020000;
864 req->suffix1 = 0x00000000;
866 req->suffix0 = 0x20000000 |
867 (chan->push.addr + ((chan->dma.cur + 2) << 2));
868 req->suffix1 = 0x00000000;
871 return nouveau_abi16_put(abi16, ret);
875 nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
876 struct drm_file *file_priv)
878 struct drm_nouveau_gem_cpu_prep *req = data;
879 struct drm_gem_object *gem;
880 struct nouveau_bo *nvbo;
881 bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
882 bool write = !!(req->flags & NOUVEAU_GEM_CPU_PREP_WRITE);
886 gem = drm_gem_object_lookup(file_priv, req->handle);
889 nvbo = nouveau_gem_object(gem);
891 lret = reservation_object_wait_timeout_rcu(nvbo->bo.resv, write, true,
892 no_wait ? 0 : 30 * HZ);
900 nouveau_bo_sync_for_cpu(nvbo);
901 drm_gem_object_put_unlocked(gem);
907 nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
908 struct drm_file *file_priv)
910 struct drm_nouveau_gem_cpu_fini *req = data;
911 struct drm_gem_object *gem;
912 struct nouveau_bo *nvbo;
914 gem = drm_gem_object_lookup(file_priv, req->handle);
917 nvbo = nouveau_gem_object(gem);
919 nouveau_bo_sync_for_device(nvbo);
920 drm_gem_object_put_unlocked(gem);
925 nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
926 struct drm_file *file_priv)
928 struct drm_nouveau_gem_info *req = data;
929 struct drm_gem_object *gem;
932 gem = drm_gem_object_lookup(file_priv, req->handle);
936 ret = nouveau_gem_info(file_priv, gem, req);
937 drm_gem_object_put_unlocked(gem);