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>
38 #include <nvif/push206e.h>
41 nouveau_gem_object_del(struct drm_gem_object *gem)
43 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
44 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
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 ttm_bo_put(&nvbo->bo);
59 pm_runtime_mark_last_busy(dev);
60 pm_runtime_put_autosuspend(dev);
64 nouveau_gem_object_open(struct drm_gem_object *gem, struct drm_file *file_priv)
66 struct nouveau_cli *cli = nouveau_cli(file_priv);
67 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
68 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
69 struct device *dev = drm->dev->dev;
70 struct nouveau_vmm *vmm = cli->svm.cli ? &cli->svm : &cli->vmm;
71 struct nouveau_vma *vma;
74 if (vmm->vmm.object.oclass < NVIF_CLASS_VMM_NV50)
77 ret = ttm_bo_reserve(&nvbo->bo, false, false, NULL);
81 ret = pm_runtime_get_sync(dev);
82 if (ret < 0 && ret != -EACCES) {
83 pm_runtime_put_autosuspend(dev);
87 ret = nouveau_vma_new(nvbo, vmm, &vma);
88 pm_runtime_mark_last_busy(dev);
89 pm_runtime_put_autosuspend(dev);
91 ttm_bo_unreserve(&nvbo->bo);
95 struct nouveau_gem_object_unmap {
96 struct nouveau_cli_work work;
97 struct nouveau_vma *vma;
101 nouveau_gem_object_delete(struct nouveau_vma *vma)
103 nouveau_fence_unref(&vma->fence);
104 nouveau_vma_del(&vma);
108 nouveau_gem_object_delete_work(struct nouveau_cli_work *w)
110 struct nouveau_gem_object_unmap *work =
111 container_of(w, typeof(*work), work);
112 nouveau_gem_object_delete(work->vma);
117 nouveau_gem_object_unmap(struct nouveau_bo *nvbo, struct nouveau_vma *vma)
119 struct dma_fence *fence = vma->fence ? &vma->fence->base : NULL;
120 struct nouveau_gem_object_unmap *work;
122 list_del_init(&vma->head);
125 nouveau_gem_object_delete(vma);
129 if (!(work = kmalloc(sizeof(*work), GFP_KERNEL))) {
130 WARN_ON(dma_fence_wait_timeout(fence, false, 2 * HZ) <= 0);
131 nouveau_gem_object_delete(vma);
135 work->work.func = nouveau_gem_object_delete_work;
137 nouveau_cli_work_queue(vma->vmm->cli, fence, &work->work);
141 nouveau_gem_object_close(struct drm_gem_object *gem, struct drm_file *file_priv)
143 struct nouveau_cli *cli = nouveau_cli(file_priv);
144 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
145 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
146 struct device *dev = drm->dev->dev;
147 struct nouveau_vmm *vmm = cli->svm.cli ? &cli->svm : & cli->vmm;
148 struct nouveau_vma *vma;
151 if (vmm->vmm.object.oclass < NVIF_CLASS_VMM_NV50)
154 ret = ttm_bo_reserve(&nvbo->bo, false, false, NULL);
158 vma = nouveau_vma_find(nvbo, vmm);
160 if (--vma->refs == 0) {
161 ret = pm_runtime_get_sync(dev);
162 if (!WARN_ON(ret < 0 && ret != -EACCES)) {
163 nouveau_gem_object_unmap(nvbo, vma);
164 pm_runtime_mark_last_busy(dev);
166 pm_runtime_put_autosuspend(dev);
169 ttm_bo_unreserve(&nvbo->bo);
172 const struct drm_gem_object_funcs nouveau_gem_object_funcs = {
173 .free = nouveau_gem_object_del,
174 .open = nouveau_gem_object_open,
175 .close = nouveau_gem_object_close,
176 .pin = nouveau_gem_prime_pin,
177 .unpin = nouveau_gem_prime_unpin,
178 .get_sg_table = nouveau_gem_prime_get_sg_table,
179 .vmap = nouveau_gem_prime_vmap,
180 .vunmap = nouveau_gem_prime_vunmap,
184 nouveau_gem_new(struct nouveau_cli *cli, u64 size, int align, uint32_t domain,
185 uint32_t tile_mode, uint32_t tile_flags,
186 struct nouveau_bo **pnvbo)
188 struct nouveau_drm *drm = cli->drm;
189 struct nouveau_bo *nvbo;
192 if (!(domain & (NOUVEAU_GEM_DOMAIN_VRAM | NOUVEAU_GEM_DOMAIN_GART)))
193 domain |= NOUVEAU_GEM_DOMAIN_CPU;
195 nvbo = nouveau_bo_alloc(cli, &size, &align, domain, tile_mode,
198 return PTR_ERR(nvbo);
200 nvbo->bo.base.funcs = &nouveau_gem_object_funcs;
202 /* Initialize the embedded gem-object. We return a single gem-reference
203 * to the caller, instead of a normal nouveau_bo ttm reference. */
204 ret = drm_gem_object_init(drm->dev, &nvbo->bo.base, size);
206 nouveau_bo_ref(NULL, &nvbo);
210 ret = nouveau_bo_init(nvbo, size, align, domain, NULL, NULL);
212 nouveau_bo_ref(NULL, &nvbo);
216 /* we restrict allowed domains on nv50+ to only the types
217 * that were requested at creation time. not possibly on
218 * earlier chips without busting the ABI.
220 nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_VRAM |
221 NOUVEAU_GEM_DOMAIN_GART;
222 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA)
223 nvbo->valid_domains &= domain;
230 nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem,
231 struct drm_nouveau_gem_info *rep)
233 struct nouveau_cli *cli = nouveau_cli(file_priv);
234 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
235 struct nouveau_vmm *vmm = cli->svm.cli ? &cli->svm : &cli->vmm;
236 struct nouveau_vma *vma;
238 if (is_power_of_2(nvbo->valid_domains))
239 rep->domain = nvbo->valid_domains;
240 else if (nvbo->bo.mem.mem_type == TTM_PL_TT)
241 rep->domain = NOUVEAU_GEM_DOMAIN_GART;
243 rep->domain = NOUVEAU_GEM_DOMAIN_VRAM;
244 rep->offset = nvbo->offset;
245 if (vmm->vmm.object.oclass >= NVIF_CLASS_VMM_NV50) {
246 vma = nouveau_vma_find(nvbo, vmm);
250 rep->offset = vma->addr;
253 rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
254 rep->map_handle = drm_vma_node_offset_addr(&nvbo->bo.base.vma_node);
255 rep->tile_mode = nvbo->mode;
256 rep->tile_flags = nvbo->contig ? 0 : NOUVEAU_GEM_TILE_NONCONTIG;
257 if (cli->device.info.family >= NV_DEVICE_INFO_V0_FERMI)
258 rep->tile_flags |= nvbo->kind << 8;
260 if (cli->device.info.family >= NV_DEVICE_INFO_V0_TESLA)
261 rep->tile_flags |= nvbo->kind << 8 | nvbo->comp << 16;
263 rep->tile_flags |= nvbo->zeta;
268 nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
269 struct drm_file *file_priv)
271 struct nouveau_cli *cli = nouveau_cli(file_priv);
272 struct drm_nouveau_gem_new *req = data;
273 struct nouveau_bo *nvbo = NULL;
276 ret = nouveau_gem_new(cli, req->info.size, req->align,
277 req->info.domain, req->info.tile_mode,
278 req->info.tile_flags, &nvbo);
282 ret = drm_gem_handle_create(file_priv, &nvbo->bo.base,
285 ret = nouveau_gem_info(file_priv, &nvbo->bo.base, &req->info);
287 drm_gem_handle_delete(file_priv, req->info.handle);
290 /* drop reference from allocate - handle holds it now */
291 drm_gem_object_put(&nvbo->bo.base);
296 nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains,
297 uint32_t write_domains, uint32_t valid_domains)
299 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
300 struct ttm_buffer_object *bo = &nvbo->bo;
301 uint32_t domains = valid_domains & nvbo->valid_domains &
302 (write_domains ? write_domains : read_domains);
303 uint32_t pref_domains = 0;;
308 valid_domains &= ~(NOUVEAU_GEM_DOMAIN_VRAM | NOUVEAU_GEM_DOMAIN_GART);
310 if ((domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
311 bo->mem.mem_type == TTM_PL_VRAM)
312 pref_domains |= NOUVEAU_GEM_DOMAIN_VRAM;
314 else if ((domains & NOUVEAU_GEM_DOMAIN_GART) &&
315 bo->mem.mem_type == TTM_PL_TT)
316 pref_domains |= NOUVEAU_GEM_DOMAIN_GART;
318 else if (domains & NOUVEAU_GEM_DOMAIN_VRAM)
319 pref_domains |= NOUVEAU_GEM_DOMAIN_VRAM;
322 pref_domains |= NOUVEAU_GEM_DOMAIN_GART;
324 nouveau_bo_placement_set(nvbo, pref_domains, valid_domains);
330 struct list_head list;
331 struct ww_acquire_ctx ticket;
335 validate_fini_no_ticket(struct validate_op *op, struct nouveau_channel *chan,
336 struct nouveau_fence *fence,
337 struct drm_nouveau_gem_pushbuf_bo *pbbo)
339 struct nouveau_bo *nvbo;
340 struct drm_nouveau_gem_pushbuf_bo *b;
342 while (!list_empty(&op->list)) {
343 nvbo = list_entry(op->list.next, struct nouveau_bo, entry);
344 b = &pbbo[nvbo->pbbo_index];
347 nouveau_bo_fence(nvbo, fence, !!b->write_domains);
349 if (chan->vmm->vmm.object.oclass >= NVIF_CLASS_VMM_NV50) {
350 struct nouveau_vma *vma =
351 (void *)(unsigned long)b->user_priv;
352 nouveau_fence_unref(&vma->fence);
353 dma_fence_get(&fence->base);
358 if (unlikely(nvbo->validate_mapped)) {
359 ttm_bo_kunmap(&nvbo->kmap);
360 nvbo->validate_mapped = false;
363 list_del(&nvbo->entry);
364 nvbo->reserved_by = NULL;
365 ttm_bo_unreserve(&nvbo->bo);
366 drm_gem_object_put(&nvbo->bo.base);
371 validate_fini(struct validate_op *op, struct nouveau_channel *chan,
372 struct nouveau_fence *fence,
373 struct drm_nouveau_gem_pushbuf_bo *pbbo)
375 validate_fini_no_ticket(op, chan, fence, pbbo);
376 ww_acquire_fini(&op->ticket);
380 validate_init(struct nouveau_channel *chan, struct drm_file *file_priv,
381 struct drm_nouveau_gem_pushbuf_bo *pbbo,
382 int nr_buffers, struct validate_op *op)
384 struct nouveau_cli *cli = nouveau_cli(file_priv);
386 int ret = -EINVAL, i;
387 struct nouveau_bo *res_bo = NULL;
388 LIST_HEAD(gart_list);
389 LIST_HEAD(vram_list);
390 LIST_HEAD(both_list);
392 ww_acquire_init(&op->ticket, &reservation_ww_class);
394 if (++trycnt > 100000) {
395 NV_PRINTK(err, cli, "%s failed and gave up.\n", __func__);
399 for (i = 0; i < nr_buffers; i++) {
400 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i];
401 struct drm_gem_object *gem;
402 struct nouveau_bo *nvbo;
404 gem = drm_gem_object_lookup(file_priv, b->handle);
406 NV_PRINTK(err, cli, "Unknown handle 0x%08x\n", b->handle);
410 nvbo = nouveau_gem_object(gem);
411 if (nvbo == res_bo) {
413 drm_gem_object_put(gem);
417 if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
418 NV_PRINTK(err, cli, "multiple instances of buffer %d on "
419 "validation list\n", b->handle);
420 drm_gem_object_put(gem);
425 ret = ttm_bo_reserve(&nvbo->bo, true, false, &op->ticket);
427 list_splice_tail_init(&vram_list, &op->list);
428 list_splice_tail_init(&gart_list, &op->list);
429 list_splice_tail_init(&both_list, &op->list);
430 validate_fini_no_ticket(op, chan, NULL, NULL);
431 if (unlikely(ret == -EDEADLK)) {
432 ret = ttm_bo_reserve_slowpath(&nvbo->bo, true,
438 if (ret != -ERESTARTSYS)
439 NV_PRINTK(err, cli, "fail reserve\n");
444 if (chan->vmm->vmm.object.oclass >= NVIF_CLASS_VMM_NV50) {
445 struct nouveau_vmm *vmm = chan->vmm;
446 struct nouveau_vma *vma = nouveau_vma_find(nvbo, vmm);
448 NV_PRINTK(err, cli, "vma not found!\n");
453 b->user_priv = (uint64_t)(unsigned long)vma;
455 b->user_priv = (uint64_t)(unsigned long)nvbo;
458 nvbo->reserved_by = file_priv;
459 nvbo->pbbo_index = i;
460 if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) &&
461 (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART))
462 list_add_tail(&nvbo->entry, &both_list);
464 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
465 list_add_tail(&nvbo->entry, &vram_list);
467 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
468 list_add_tail(&nvbo->entry, &gart_list);
470 NV_PRINTK(err, cli, "invalid valid domains: 0x%08x\n",
472 list_add_tail(&nvbo->entry, &both_list);
480 ww_acquire_done(&op->ticket);
481 list_splice_tail(&vram_list, &op->list);
482 list_splice_tail(&gart_list, &op->list);
483 list_splice_tail(&both_list, &op->list);
485 validate_fini(op, chan, NULL, NULL);
491 validate_list(struct nouveau_channel *chan, struct nouveau_cli *cli,
492 struct list_head *list, struct drm_nouveau_gem_pushbuf_bo *pbbo)
494 struct nouveau_drm *drm = chan->drm;
495 struct nouveau_bo *nvbo;
498 list_for_each_entry(nvbo, list, entry) {
499 struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index];
501 ret = nouveau_gem_set_domain(&nvbo->bo.base, b->read_domains,
505 NV_PRINTK(err, cli, "fail set_domain\n");
509 ret = nouveau_bo_validate(nvbo, true, false);
511 if (ret != -ERESTARTSYS)
512 NV_PRINTK(err, cli, "fail ttm_validate\n");
516 ret = nouveau_fence_sync(nvbo, chan, !!b->write_domains, true);
518 if (ret != -ERESTARTSYS)
519 NV_PRINTK(err, cli, "fail post-validate sync\n");
523 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
524 if (nvbo->offset == b->presumed.offset &&
525 ((nvbo->bo.mem.mem_type == TTM_PL_VRAM &&
526 b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) ||
527 (nvbo->bo.mem.mem_type == TTM_PL_TT &&
528 b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART)))
531 if (nvbo->bo.mem.mem_type == TTM_PL_TT)
532 b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART;
534 b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM;
535 b->presumed.offset = nvbo->offset;
536 b->presumed.valid = 0;
545 nouveau_gem_pushbuf_validate(struct nouveau_channel *chan,
546 struct drm_file *file_priv,
547 struct drm_nouveau_gem_pushbuf_bo *pbbo,
549 struct validate_op *op, bool *apply_relocs)
551 struct nouveau_cli *cli = nouveau_cli(file_priv);
554 INIT_LIST_HEAD(&op->list);
559 ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
561 if (ret != -ERESTARTSYS)
562 NV_PRINTK(err, cli, "validate_init\n");
566 ret = validate_list(chan, cli, &op->list, pbbo);
567 if (unlikely(ret < 0)) {
568 if (ret != -ERESTARTSYS)
569 NV_PRINTK(err, cli, "validating bo list\n");
570 validate_fini(op, chan, NULL, NULL);
584 u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
587 void __user *userptr = (void __force __user *)(uintptr_t)user;
591 mem = kvmalloc(size, GFP_KERNEL);
593 return ERR_PTR(-ENOMEM);
595 if (copy_from_user(mem, userptr, size)) {
597 return ERR_PTR(-EFAULT);
604 nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli,
605 struct drm_nouveau_gem_pushbuf *req,
606 struct drm_nouveau_gem_pushbuf_reloc *reloc,
607 struct drm_nouveau_gem_pushbuf_bo *bo)
612 for (i = 0; i < req->nr_relocs; i++) {
613 struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
614 struct drm_nouveau_gem_pushbuf_bo *b;
615 struct nouveau_bo *nvbo;
618 if (unlikely(r->bo_index >= req->nr_buffers)) {
619 NV_PRINTK(err, cli, "reloc bo index invalid\n");
624 b = &bo[r->bo_index];
625 if (b->presumed.valid)
628 if (unlikely(r->reloc_bo_index >= req->nr_buffers)) {
629 NV_PRINTK(err, cli, "reloc container bo index invalid\n");
633 nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv;
635 if (unlikely(r->reloc_bo_offset + 4 >
636 nvbo->bo.mem.num_pages << PAGE_SHIFT)) {
637 NV_PRINTK(err, cli, "reloc outside of bo\n");
642 if (!nvbo->kmap.virtual) {
643 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages,
646 NV_PRINTK(err, cli, "failed kmap for reloc\n");
649 nvbo->validate_mapped = true;
652 if (r->flags & NOUVEAU_GEM_RELOC_LOW)
653 data = b->presumed.offset + r->data;
655 if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
656 data = (b->presumed.offset + r->data) >> 32;
660 if (r->flags & NOUVEAU_GEM_RELOC_OR) {
661 if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
667 ret = ttm_bo_wait(&nvbo->bo, false, false);
669 NV_PRINTK(err, cli, "reloc wait_idle failed: %d\n", ret);
673 nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
681 nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
682 struct drm_file *file_priv)
684 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
685 struct nouveau_cli *cli = nouveau_cli(file_priv);
686 struct nouveau_abi16_chan *temp;
687 struct nouveau_drm *drm = nouveau_drm(dev);
688 struct drm_nouveau_gem_pushbuf *req = data;
689 struct drm_nouveau_gem_pushbuf_push *push;
690 struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
691 struct drm_nouveau_gem_pushbuf_bo *bo;
692 struct nouveau_channel *chan = NULL;
693 struct validate_op op;
694 struct nouveau_fence *fence = NULL;
696 bool do_reloc = false, sync = false;
698 if (unlikely(!abi16))
701 list_for_each_entry(temp, &abi16->channels, head) {
702 if (temp->chan->chid == req->channel) {
709 return nouveau_abi16_put(abi16, -ENOENT);
710 if (unlikely(atomic_read(&chan->killed)))
711 return nouveau_abi16_put(abi16, -ENODEV);
713 sync = req->vram_available & NOUVEAU_GEM_PUSHBUF_SYNC;
715 req->vram_available = drm->gem.vram_available;
716 req->gart_available = drm->gem.gart_available;
717 if (unlikely(req->nr_push == 0))
720 if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
721 NV_PRINTK(err, cli, "pushbuf push count exceeds limit: %d max %d\n",
722 req->nr_push, NOUVEAU_GEM_MAX_PUSH);
723 return nouveau_abi16_put(abi16, -EINVAL);
726 if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
727 NV_PRINTK(err, cli, "pushbuf bo count exceeds limit: %d max %d\n",
728 req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
729 return nouveau_abi16_put(abi16, -EINVAL);
732 if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
733 NV_PRINTK(err, cli, "pushbuf reloc count exceeds limit: %d max %d\n",
734 req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
735 return nouveau_abi16_put(abi16, -EINVAL);
738 push = u_memcpya(req->push, req->nr_push, sizeof(*push));
740 return nouveau_abi16_put(abi16, PTR_ERR(push));
742 bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
745 return nouveau_abi16_put(abi16, PTR_ERR(bo));
748 /* Ensure all push buffers are on validate list */
749 for (i = 0; i < req->nr_push; i++) {
750 if (push[i].bo_index >= req->nr_buffers) {
751 NV_PRINTK(err, cli, "push %d buffer not in list\n", i);
757 /* Validate buffer list */
759 ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo,
760 req->nr_buffers, &op, &do_reloc);
762 if (ret != -ERESTARTSYS)
763 NV_PRINTK(err, cli, "validate: %d\n", ret);
767 /* Apply any relocations that are required */
770 validate_fini(&op, chan, NULL, bo);
771 reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
773 ret = PTR_ERR(reloc);
780 ret = nouveau_gem_pushbuf_reloc_apply(cli, req, reloc, bo);
782 NV_PRINTK(err, cli, "reloc apply: %d\n", ret);
787 if (chan->dma.ib_max) {
788 ret = nouveau_dma_wait(chan, req->nr_push + 1, 16);
790 NV_PRINTK(err, cli, "nv50cal_space: %d\n", ret);
794 for (i = 0; i < req->nr_push; i++) {
795 struct nouveau_vma *vma = (void *)(unsigned long)
796 bo[push[i].bo_index].user_priv;
798 nv50_dma_push(chan, vma->addr + push[i].offset,
802 if (drm->client.device.info.chipset >= 0x25) {
803 ret = PUSH_WAIT(chan->chan.push, req->nr_push * 2);
805 NV_PRINTK(err, cli, "cal_space: %d\n", ret);
809 for (i = 0; i < req->nr_push; i++) {
810 struct nouveau_bo *nvbo = (void *)(unsigned long)
811 bo[push[i].bo_index].user_priv;
813 PUSH_CALL(chan->chan.push, nvbo->offset + push[i].offset);
814 PUSH_DATA(chan->chan.push, 0);
817 ret = PUSH_WAIT(chan->chan.push, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
819 NV_PRINTK(err, cli, "jmp_space: %d\n", ret);
823 for (i = 0; i < req->nr_push; i++) {
824 struct nouveau_bo *nvbo = (void *)(unsigned long)
825 bo[push[i].bo_index].user_priv;
828 cmd = chan->push.addr + ((chan->dma.cur + 2) << 2);
830 if (unlikely(cmd != req->suffix0)) {
831 if (!nvbo->kmap.virtual) {
832 ret = ttm_bo_kmap(&nvbo->bo, 0,
840 nvbo->validate_mapped = true;
843 nouveau_bo_wr32(nvbo, (push[i].offset +
844 push[i].length - 8) / 4, cmd);
847 PUSH_JUMP(chan->chan.push, nvbo->offset + push[i].offset);
848 PUSH_DATA(chan->chan.push, 0);
849 for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
850 PUSH_DATA(chan->chan.push, 0);
854 ret = nouveau_fence_new(chan, false, &fence);
856 NV_PRINTK(err, cli, "error fencing pushbuf: %d\n", ret);
862 if (!(ret = nouveau_fence_wait(fence, false, false))) {
863 if ((ret = dma_fence_get_status(&fence->base)) == 1)
869 validate_fini(&op, chan, fence, bo);
870 nouveau_fence_unref(&fence);
873 struct drm_nouveau_gem_pushbuf_bo __user *upbbo =
874 u64_to_user_ptr(req->buffers);
876 for (i = 0; i < req->nr_buffers; i++) {
877 if (bo[i].presumed.valid)
880 if (copy_to_user(&upbbo[i].presumed, &bo[i].presumed,
881 sizeof(bo[i].presumed))) {
893 if (chan->dma.ib_max) {
894 req->suffix0 = 0x00000000;
895 req->suffix1 = 0x00000000;
897 if (drm->client.device.info.chipset >= 0x25) {
898 req->suffix0 = 0x00020000;
899 req->suffix1 = 0x00000000;
901 req->suffix0 = 0x20000000 |
902 (chan->push.addr + ((chan->dma.cur + 2) << 2));
903 req->suffix1 = 0x00000000;
906 return nouveau_abi16_put(abi16, ret);
910 nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data,
911 struct drm_file *file_priv)
913 struct drm_nouveau_gem_cpu_prep *req = data;
914 struct drm_gem_object *gem;
915 struct nouveau_bo *nvbo;
916 bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT);
917 bool write = !!(req->flags & NOUVEAU_GEM_CPU_PREP_WRITE);
921 gem = drm_gem_object_lookup(file_priv, req->handle);
924 nvbo = nouveau_gem_object(gem);
926 lret = dma_resv_wait_timeout_rcu(nvbo->bo.base.resv, write, true,
927 no_wait ? 0 : 30 * HZ);
935 nouveau_bo_sync_for_cpu(nvbo);
936 drm_gem_object_put(gem);
942 nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
943 struct drm_file *file_priv)
945 struct drm_nouveau_gem_cpu_fini *req = data;
946 struct drm_gem_object *gem;
947 struct nouveau_bo *nvbo;
949 gem = drm_gem_object_lookup(file_priv, req->handle);
952 nvbo = nouveau_gem_object(gem);
954 nouveau_bo_sync_for_device(nvbo);
955 drm_gem_object_put(gem);
960 nouveau_gem_ioctl_info(struct drm_device *dev, void *data,
961 struct drm_file *file_priv)
963 struct drm_nouveau_gem_info *req = data;
964 struct drm_gem_object *gem;
967 gem = drm_gem_object_lookup(file_priv, req->handle);
971 ret = nouveau_gem_info(file_priv, gem, req);
972 drm_gem_object_put(gem);