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 <drm/drm_gem_ttm_helper.h>
29 #include "nouveau_drv.h"
30 #include "nouveau_dma.h"
31 #include "nouveau_fence.h"
32 #include "nouveau_abi16.h"
34 #include "nouveau_ttm.h"
35 #include "nouveau_gem.h"
36 #include "nouveau_mem.h"
37 #include "nouveau_vmm.h"
39 #include <nvif/class.h>
40 #include <nvif/push206e.h>
43 nouveau_gem_object_del(struct drm_gem_object
*gem
)
45 struct nouveau_bo
*nvbo
= nouveau_gem_object(gem
);
46 struct nouveau_drm
*drm
= nouveau_bdev(nvbo
->bo
.bdev
);
47 struct device
*dev
= drm
->dev
->dev
;
50 ret
= pm_runtime_get_sync(dev
);
51 if (WARN_ON(ret
< 0 && ret
!= -EACCES
)) {
52 pm_runtime_put_autosuspend(dev
);
56 if (gem
->import_attach
)
57 drm_prime_gem_destroy(gem
, nvbo
->bo
.sg
);
59 ttm_bo_put(&nvbo
->bo
);
61 pm_runtime_mark_last_busy(dev
);
62 pm_runtime_put_autosuspend(dev
);
66 nouveau_gem_object_open(struct drm_gem_object
*gem
, struct drm_file
*file_priv
)
68 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
69 struct nouveau_bo
*nvbo
= nouveau_gem_object(gem
);
70 struct nouveau_drm
*drm
= nouveau_bdev(nvbo
->bo
.bdev
);
71 struct device
*dev
= drm
->dev
->dev
;
72 struct nouveau_vmm
*vmm
= cli
->svm
.cli
? &cli
->svm
: &cli
->vmm
;
73 struct nouveau_vma
*vma
;
76 if (vmm
->vmm
.object
.oclass
< NVIF_CLASS_VMM_NV50
)
79 ret
= ttm_bo_reserve(&nvbo
->bo
, false, false, NULL
);
83 ret
= pm_runtime_get_sync(dev
);
84 if (ret
< 0 && ret
!= -EACCES
) {
85 pm_runtime_put_autosuspend(dev
);
89 ret
= nouveau_vma_new(nvbo
, vmm
, &vma
);
90 pm_runtime_mark_last_busy(dev
);
91 pm_runtime_put_autosuspend(dev
);
93 ttm_bo_unreserve(&nvbo
->bo
);
97 struct nouveau_gem_object_unmap
{
98 struct nouveau_cli_work work
;
99 struct nouveau_vma
*vma
;
103 nouveau_gem_object_delete(struct nouveau_vma
*vma
)
105 nouveau_fence_unref(&vma
->fence
);
106 nouveau_vma_del(&vma
);
110 nouveau_gem_object_delete_work(struct nouveau_cli_work
*w
)
112 struct nouveau_gem_object_unmap
*work
=
113 container_of(w
, typeof(*work
), work
);
114 nouveau_gem_object_delete(work
->vma
);
119 nouveau_gem_object_unmap(struct nouveau_bo
*nvbo
, struct nouveau_vma
*vma
)
121 struct dma_fence
*fence
= vma
->fence
? &vma
->fence
->base
: NULL
;
122 struct nouveau_gem_object_unmap
*work
;
124 list_del_init(&vma
->head
);
127 nouveau_gem_object_delete(vma
);
131 if (!(work
= kmalloc(sizeof(*work
), GFP_KERNEL
))) {
132 WARN_ON(dma_fence_wait_timeout(fence
, false, 2 * HZ
) <= 0);
133 nouveau_gem_object_delete(vma
);
137 work
->work
.func
= nouveau_gem_object_delete_work
;
139 nouveau_cli_work_queue(vma
->vmm
->cli
, fence
, &work
->work
);
143 nouveau_gem_object_close(struct drm_gem_object
*gem
, struct drm_file
*file_priv
)
145 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
146 struct nouveau_bo
*nvbo
= nouveau_gem_object(gem
);
147 struct nouveau_drm
*drm
= nouveau_bdev(nvbo
->bo
.bdev
);
148 struct device
*dev
= drm
->dev
->dev
;
149 struct nouveau_vmm
*vmm
= cli
->svm
.cli
? &cli
->svm
: & cli
->vmm
;
150 struct nouveau_vma
*vma
;
153 if (vmm
->vmm
.object
.oclass
< NVIF_CLASS_VMM_NV50
)
156 ret
= ttm_bo_reserve(&nvbo
->bo
, false, false, NULL
);
160 vma
= nouveau_vma_find(nvbo
, 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
);
168 pm_runtime_put_autosuspend(dev
);
171 ttm_bo_unreserve(&nvbo
->bo
);
174 const struct drm_gem_object_funcs nouveau_gem_object_funcs
= {
175 .free
= nouveau_gem_object_del
,
176 .open
= nouveau_gem_object_open
,
177 .close
= nouveau_gem_object_close
,
178 .pin
= nouveau_gem_prime_pin
,
179 .unpin
= nouveau_gem_prime_unpin
,
180 .get_sg_table
= nouveau_gem_prime_get_sg_table
,
181 .vmap
= drm_gem_ttm_vmap
,
182 .vunmap
= drm_gem_ttm_vunmap
,
186 nouveau_gem_new(struct nouveau_cli
*cli
, u64 size
, int align
, uint32_t domain
,
187 uint32_t tile_mode
, uint32_t tile_flags
,
188 struct nouveau_bo
**pnvbo
)
190 struct nouveau_drm
*drm
= cli
->drm
;
191 struct nouveau_bo
*nvbo
;
194 if (!(domain
& (NOUVEAU_GEM_DOMAIN_VRAM
| NOUVEAU_GEM_DOMAIN_GART
)))
195 domain
|= NOUVEAU_GEM_DOMAIN_CPU
;
197 nvbo
= nouveau_bo_alloc(cli
, &size
, &align
, domain
, tile_mode
,
200 return PTR_ERR(nvbo
);
202 nvbo
->bo
.base
.funcs
= &nouveau_gem_object_funcs
;
204 /* Initialize the embedded gem-object. We return a single gem-reference
205 * to the caller, instead of a normal nouveau_bo ttm reference. */
206 ret
= drm_gem_object_init(drm
->dev
, &nvbo
->bo
.base
, size
);
208 drm_gem_object_release(&nvbo
->bo
.base
);
213 ret
= nouveau_bo_init(nvbo
, size
, align
, domain
, NULL
, NULL
);
215 nouveau_bo_ref(NULL
, &nvbo
);
219 /* we restrict allowed domains on nv50+ to only the types
220 * that were requested at creation time. not possibly on
221 * earlier chips without busting the ABI.
223 nvbo
->valid_domains
= NOUVEAU_GEM_DOMAIN_VRAM
|
224 NOUVEAU_GEM_DOMAIN_GART
;
225 if (drm
->client
.device
.info
.family
>= NV_DEVICE_INFO_V0_TESLA
)
226 nvbo
->valid_domains
&= domain
;
233 nouveau_gem_info(struct drm_file
*file_priv
, struct drm_gem_object
*gem
,
234 struct drm_nouveau_gem_info
*rep
)
236 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
237 struct nouveau_bo
*nvbo
= nouveau_gem_object(gem
);
238 struct nouveau_vmm
*vmm
= cli
->svm
.cli
? &cli
->svm
: &cli
->vmm
;
239 struct nouveau_vma
*vma
;
241 if (is_power_of_2(nvbo
->valid_domains
))
242 rep
->domain
= nvbo
->valid_domains
;
243 else if (nvbo
->bo
.mem
.mem_type
== TTM_PL_TT
)
244 rep
->domain
= NOUVEAU_GEM_DOMAIN_GART
;
246 rep
->domain
= NOUVEAU_GEM_DOMAIN_VRAM
;
247 rep
->offset
= nvbo
->offset
;
248 if (vmm
->vmm
.object
.oclass
>= NVIF_CLASS_VMM_NV50
) {
249 vma
= nouveau_vma_find(nvbo
, vmm
);
253 rep
->offset
= vma
->addr
;
256 rep
->size
= nvbo
->bo
.mem
.num_pages
<< PAGE_SHIFT
;
257 rep
->map_handle
= drm_vma_node_offset_addr(&nvbo
->bo
.base
.vma_node
);
258 rep
->tile_mode
= nvbo
->mode
;
259 rep
->tile_flags
= nvbo
->contig
? 0 : NOUVEAU_GEM_TILE_NONCONTIG
;
260 if (cli
->device
.info
.family
>= NV_DEVICE_INFO_V0_FERMI
)
261 rep
->tile_flags
|= nvbo
->kind
<< 8;
263 if (cli
->device
.info
.family
>= NV_DEVICE_INFO_V0_TESLA
)
264 rep
->tile_flags
|= nvbo
->kind
<< 8 | nvbo
->comp
<< 16;
266 rep
->tile_flags
|= nvbo
->zeta
;
271 nouveau_gem_ioctl_new(struct drm_device
*dev
, void *data
,
272 struct drm_file
*file_priv
)
274 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
275 struct drm_nouveau_gem_new
*req
= data
;
276 struct nouveau_bo
*nvbo
= NULL
;
279 ret
= nouveau_gem_new(cli
, req
->info
.size
, req
->align
,
280 req
->info
.domain
, req
->info
.tile_mode
,
281 req
->info
.tile_flags
, &nvbo
);
285 ret
= drm_gem_handle_create(file_priv
, &nvbo
->bo
.base
,
288 ret
= nouveau_gem_info(file_priv
, &nvbo
->bo
.base
, &req
->info
);
290 drm_gem_handle_delete(file_priv
, req
->info
.handle
);
293 /* drop reference from allocate - handle holds it now */
294 drm_gem_object_put(&nvbo
->bo
.base
);
299 nouveau_gem_set_domain(struct drm_gem_object
*gem
, uint32_t read_domains
,
300 uint32_t write_domains
, uint32_t valid_domains
)
302 struct nouveau_bo
*nvbo
= nouveau_gem_object(gem
);
303 struct ttm_buffer_object
*bo
= &nvbo
->bo
;
304 uint32_t domains
= valid_domains
& nvbo
->valid_domains
&
305 (write_domains
? write_domains
: read_domains
);
306 uint32_t pref_domains
= 0;;
311 valid_domains
&= ~(NOUVEAU_GEM_DOMAIN_VRAM
| NOUVEAU_GEM_DOMAIN_GART
);
313 if ((domains
& NOUVEAU_GEM_DOMAIN_VRAM
) &&
314 bo
->mem
.mem_type
== TTM_PL_VRAM
)
315 pref_domains
|= NOUVEAU_GEM_DOMAIN_VRAM
;
317 else if ((domains
& NOUVEAU_GEM_DOMAIN_GART
) &&
318 bo
->mem
.mem_type
== TTM_PL_TT
)
319 pref_domains
|= NOUVEAU_GEM_DOMAIN_GART
;
321 else if (domains
& NOUVEAU_GEM_DOMAIN_VRAM
)
322 pref_domains
|= NOUVEAU_GEM_DOMAIN_VRAM
;
325 pref_domains
|= NOUVEAU_GEM_DOMAIN_GART
;
327 nouveau_bo_placement_set(nvbo
, pref_domains
, valid_domains
);
333 struct list_head list
;
334 struct ww_acquire_ctx ticket
;
338 validate_fini_no_ticket(struct validate_op
*op
, struct nouveau_channel
*chan
,
339 struct nouveau_fence
*fence
,
340 struct drm_nouveau_gem_pushbuf_bo
*pbbo
)
342 struct nouveau_bo
*nvbo
;
343 struct drm_nouveau_gem_pushbuf_bo
*b
;
345 while (!list_empty(&op
->list
)) {
346 nvbo
= list_entry(op
->list
.next
, struct nouveau_bo
, entry
);
347 b
= &pbbo
[nvbo
->pbbo_index
];
350 nouveau_bo_fence(nvbo
, fence
, !!b
->write_domains
);
352 if (chan
->vmm
->vmm
.object
.oclass
>= NVIF_CLASS_VMM_NV50
) {
353 struct nouveau_vma
*vma
=
354 (void *)(unsigned long)b
->user_priv
;
355 nouveau_fence_unref(&vma
->fence
);
356 dma_fence_get(&fence
->base
);
361 if (unlikely(nvbo
->validate_mapped
)) {
362 ttm_bo_kunmap(&nvbo
->kmap
);
363 nvbo
->validate_mapped
= false;
366 list_del(&nvbo
->entry
);
367 nvbo
->reserved_by
= NULL
;
368 ttm_bo_unreserve(&nvbo
->bo
);
369 drm_gem_object_put(&nvbo
->bo
.base
);
374 validate_fini(struct validate_op
*op
, struct nouveau_channel
*chan
,
375 struct nouveau_fence
*fence
,
376 struct drm_nouveau_gem_pushbuf_bo
*pbbo
)
378 validate_fini_no_ticket(op
, chan
, fence
, pbbo
);
379 ww_acquire_fini(&op
->ticket
);
383 validate_init(struct nouveau_channel
*chan
, struct drm_file
*file_priv
,
384 struct drm_nouveau_gem_pushbuf_bo
*pbbo
,
385 int nr_buffers
, struct validate_op
*op
)
387 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
389 int ret
= -EINVAL
, i
;
390 struct nouveau_bo
*res_bo
= NULL
;
391 LIST_HEAD(gart_list
);
392 LIST_HEAD(vram_list
);
393 LIST_HEAD(both_list
);
395 ww_acquire_init(&op
->ticket
, &reservation_ww_class
);
397 if (++trycnt
> 100000) {
398 NV_PRINTK(err
, cli
, "%s failed and gave up.\n", __func__
);
402 for (i
= 0; i
< nr_buffers
; i
++) {
403 struct drm_nouveau_gem_pushbuf_bo
*b
= &pbbo
[i
];
404 struct drm_gem_object
*gem
;
405 struct nouveau_bo
*nvbo
;
407 gem
= drm_gem_object_lookup(file_priv
, b
->handle
);
409 NV_PRINTK(err
, cli
, "Unknown handle 0x%08x\n", b
->handle
);
413 nvbo
= nouveau_gem_object(gem
);
414 if (nvbo
== res_bo
) {
416 drm_gem_object_put(gem
);
420 if (nvbo
->reserved_by
&& nvbo
->reserved_by
== file_priv
) {
421 NV_PRINTK(err
, cli
, "multiple instances of buffer %d on "
422 "validation list\n", b
->handle
);
423 drm_gem_object_put(gem
);
428 ret
= ttm_bo_reserve(&nvbo
->bo
, true, false, &op
->ticket
);
430 list_splice_tail_init(&vram_list
, &op
->list
);
431 list_splice_tail_init(&gart_list
, &op
->list
);
432 list_splice_tail_init(&both_list
, &op
->list
);
433 validate_fini_no_ticket(op
, chan
, NULL
, NULL
);
434 if (unlikely(ret
== -EDEADLK
)) {
435 ret
= ttm_bo_reserve_slowpath(&nvbo
->bo
, true,
441 if (ret
!= -ERESTARTSYS
)
442 NV_PRINTK(err
, cli
, "fail reserve\n");
447 if (chan
->vmm
->vmm
.object
.oclass
>= NVIF_CLASS_VMM_NV50
) {
448 struct nouveau_vmm
*vmm
= chan
->vmm
;
449 struct nouveau_vma
*vma
= nouveau_vma_find(nvbo
, vmm
);
451 NV_PRINTK(err
, cli
, "vma not found!\n");
456 b
->user_priv
= (uint64_t)(unsigned long)vma
;
458 b
->user_priv
= (uint64_t)(unsigned long)nvbo
;
461 nvbo
->reserved_by
= file_priv
;
462 nvbo
->pbbo_index
= i
;
463 if ((b
->valid_domains
& NOUVEAU_GEM_DOMAIN_VRAM
) &&
464 (b
->valid_domains
& NOUVEAU_GEM_DOMAIN_GART
))
465 list_add_tail(&nvbo
->entry
, &both_list
);
467 if (b
->valid_domains
& NOUVEAU_GEM_DOMAIN_VRAM
)
468 list_add_tail(&nvbo
->entry
, &vram_list
);
470 if (b
->valid_domains
& NOUVEAU_GEM_DOMAIN_GART
)
471 list_add_tail(&nvbo
->entry
, &gart_list
);
473 NV_PRINTK(err
, cli
, "invalid valid domains: 0x%08x\n",
475 list_add_tail(&nvbo
->entry
, &both_list
);
483 ww_acquire_done(&op
->ticket
);
484 list_splice_tail(&vram_list
, &op
->list
);
485 list_splice_tail(&gart_list
, &op
->list
);
486 list_splice_tail(&both_list
, &op
->list
);
488 validate_fini(op
, chan
, NULL
, NULL
);
494 validate_list(struct nouveau_channel
*chan
, struct nouveau_cli
*cli
,
495 struct list_head
*list
, struct drm_nouveau_gem_pushbuf_bo
*pbbo
)
497 struct nouveau_drm
*drm
= chan
->drm
;
498 struct nouveau_bo
*nvbo
;
501 list_for_each_entry(nvbo
, list
, entry
) {
502 struct drm_nouveau_gem_pushbuf_bo
*b
= &pbbo
[nvbo
->pbbo_index
];
504 ret
= nouveau_gem_set_domain(&nvbo
->bo
.base
, b
->read_domains
,
508 NV_PRINTK(err
, cli
, "fail set_domain\n");
512 ret
= nouveau_bo_validate(nvbo
, true, false);
514 if (ret
!= -ERESTARTSYS
)
515 NV_PRINTK(err
, cli
, "fail ttm_validate\n");
519 ret
= nouveau_fence_sync(nvbo
, chan
, !!b
->write_domains
, true);
521 if (ret
!= -ERESTARTSYS
)
522 NV_PRINTK(err
, cli
, "fail post-validate sync\n");
526 if (drm
->client
.device
.info
.family
< NV_DEVICE_INFO_V0_TESLA
) {
527 if (nvbo
->offset
== b
->presumed
.offset
&&
528 ((nvbo
->bo
.mem
.mem_type
== TTM_PL_VRAM
&&
529 b
->presumed
.domain
& NOUVEAU_GEM_DOMAIN_VRAM
) ||
530 (nvbo
->bo
.mem
.mem_type
== TTM_PL_TT
&&
531 b
->presumed
.domain
& NOUVEAU_GEM_DOMAIN_GART
)))
534 if (nvbo
->bo
.mem
.mem_type
== TTM_PL_TT
)
535 b
->presumed
.domain
= NOUVEAU_GEM_DOMAIN_GART
;
537 b
->presumed
.domain
= NOUVEAU_GEM_DOMAIN_VRAM
;
538 b
->presumed
.offset
= nvbo
->offset
;
539 b
->presumed
.valid
= 0;
548 nouveau_gem_pushbuf_validate(struct nouveau_channel
*chan
,
549 struct drm_file
*file_priv
,
550 struct drm_nouveau_gem_pushbuf_bo
*pbbo
,
552 struct validate_op
*op
, bool *apply_relocs
)
554 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
557 INIT_LIST_HEAD(&op
->list
);
562 ret
= validate_init(chan
, file_priv
, pbbo
, nr_buffers
, op
);
564 if (ret
!= -ERESTARTSYS
)
565 NV_PRINTK(err
, cli
, "validate_init\n");
569 ret
= validate_list(chan
, cli
, &op
->list
, pbbo
);
570 if (unlikely(ret
< 0)) {
571 if (ret
!= -ERESTARTSYS
)
572 NV_PRINTK(err
, cli
, "validating bo list\n");
573 validate_fini(op
, chan
, NULL
, NULL
);
575 } else if (ret
> 0) {
576 *apply_relocs
= true;
589 u_memcpya(uint64_t user
, unsigned nmemb
, unsigned size
)
592 void __user
*userptr
= (void __force __user
*)(uintptr_t)user
;
596 mem
= kvmalloc(size
, GFP_KERNEL
);
598 return ERR_PTR(-ENOMEM
);
600 if (copy_from_user(mem
, userptr
, size
)) {
602 return ERR_PTR(-EFAULT
);
609 nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli
*cli
,
610 struct drm_nouveau_gem_pushbuf
*req
,
611 struct drm_nouveau_gem_pushbuf_reloc
*reloc
,
612 struct drm_nouveau_gem_pushbuf_bo
*bo
)
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
);
685 nouveau_gem_ioctl_pushbuf(struct drm_device
*dev
, void *data
,
686 struct drm_file
*file_priv
)
688 struct nouveau_abi16
*abi16
= nouveau_abi16_get(file_priv
);
689 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
690 struct nouveau_abi16_chan
*temp
;
691 struct nouveau_drm
*drm
= nouveau_drm(dev
);
692 struct drm_nouveau_gem_pushbuf
*req
= data
;
693 struct drm_nouveau_gem_pushbuf_push
*push
;
694 struct drm_nouveau_gem_pushbuf_reloc
*reloc
= NULL
;
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
;
700 bool do_reloc
= false, sync
= false;
702 if (unlikely(!abi16
))
705 list_for_each_entry(temp
, &abi16
->channels
, head
) {
706 if (temp
->chan
->chid
== req
->channel
) {
713 return nouveau_abi16_put(abi16
, -ENOENT
);
714 if (unlikely(atomic_read(&chan
->killed
)))
715 return nouveau_abi16_put(abi16
, -ENODEV
);
717 sync
= req
->vram_available
& NOUVEAU_GEM_PUSHBUF_SYNC
;
719 req
->vram_available
= drm
->gem
.vram_available
;
720 req
->gart_available
= drm
->gem
.gart_available
;
721 if (unlikely(req
->nr_push
== 0))
724 if (unlikely(req
->nr_push
> NOUVEAU_GEM_MAX_PUSH
)) {
725 NV_PRINTK(err
, cli
, "pushbuf push count exceeds limit: %d max %d\n",
726 req
->nr_push
, NOUVEAU_GEM_MAX_PUSH
);
727 return nouveau_abi16_put(abi16
, -EINVAL
);
730 if (unlikely(req
->nr_buffers
> NOUVEAU_GEM_MAX_BUFFERS
)) {
731 NV_PRINTK(err
, cli
, "pushbuf bo count exceeds limit: %d max %d\n",
732 req
->nr_buffers
, NOUVEAU_GEM_MAX_BUFFERS
);
733 return nouveau_abi16_put(abi16
, -EINVAL
);
736 if (unlikely(req
->nr_relocs
> NOUVEAU_GEM_MAX_RELOCS
)) {
737 NV_PRINTK(err
, cli
, "pushbuf reloc count exceeds limit: %d max %d\n",
738 req
->nr_relocs
, NOUVEAU_GEM_MAX_RELOCS
);
739 return nouveau_abi16_put(abi16
, -EINVAL
);
742 push
= u_memcpya(req
->push
, req
->nr_push
, sizeof(*push
));
744 return nouveau_abi16_put(abi16
, PTR_ERR(push
));
746 bo
= u_memcpya(req
->buffers
, req
->nr_buffers
, sizeof(*bo
));
749 return nouveau_abi16_put(abi16
, PTR_ERR(bo
));
752 /* Ensure all push buffers are on validate list */
753 for (i
= 0; i
< req
->nr_push
; i
++) {
754 if (push
[i
].bo_index
>= req
->nr_buffers
) {
755 NV_PRINTK(err
, cli
, "push %d buffer not in list\n", i
);
761 /* Validate buffer list */
763 ret
= nouveau_gem_pushbuf_validate(chan
, file_priv
, bo
,
764 req
->nr_buffers
, &op
, &do_reloc
);
766 if (ret
!= -ERESTARTSYS
)
767 NV_PRINTK(err
, cli
, "validate: %d\n", ret
);
771 /* Apply any relocations that are required */
774 validate_fini(&op
, chan
, NULL
, bo
);
775 reloc
= u_memcpya(req
->relocs
, req
->nr_relocs
, sizeof(*reloc
));
777 ret
= PTR_ERR(reloc
);
784 ret
= nouveau_gem_pushbuf_reloc_apply(cli
, req
, reloc
, bo
);
786 NV_PRINTK(err
, cli
, "reloc apply: %d\n", ret
);
791 if (chan
->dma
.ib_max
) {
792 ret
= nouveau_dma_wait(chan
, req
->nr_push
+ 1, 16);
794 NV_PRINTK(err
, cli
, "nv50cal_space: %d\n", ret
);
798 for (i
= 0; i
< req
->nr_push
; i
++) {
799 struct nouveau_vma
*vma
= (void *)(unsigned long)
800 bo
[push
[i
].bo_index
].user_priv
;
802 nv50_dma_push(chan
, vma
->addr
+ push
[i
].offset
,
806 if (drm
->client
.device
.info
.chipset
>= 0x25) {
807 ret
= PUSH_WAIT(chan
->chan
.push
, req
->nr_push
* 2);
809 NV_PRINTK(err
, cli
, "cal_space: %d\n", ret
);
813 for (i
= 0; i
< req
->nr_push
; i
++) {
814 struct nouveau_bo
*nvbo
= (void *)(unsigned long)
815 bo
[push
[i
].bo_index
].user_priv
;
817 PUSH_CALL(chan
->chan
.push
, nvbo
->offset
+ push
[i
].offset
);
818 PUSH_DATA(chan
->chan
.push
, 0);
821 ret
= PUSH_WAIT(chan
->chan
.push
, req
->nr_push
* (2 + NOUVEAU_DMA_SKIPS
));
823 NV_PRINTK(err
, cli
, "jmp_space: %d\n", ret
);
827 for (i
= 0; i
< req
->nr_push
; i
++) {
828 struct nouveau_bo
*nvbo
= (void *)(unsigned long)
829 bo
[push
[i
].bo_index
].user_priv
;
832 cmd
= chan
->push
.addr
+ ((chan
->dma
.cur
+ 2) << 2);
834 if (unlikely(cmd
!= req
->suffix0
)) {
835 if (!nvbo
->kmap
.virtual) {
836 ret
= ttm_bo_kmap(&nvbo
->bo
, 0,
844 nvbo
->validate_mapped
= true;
847 nouveau_bo_wr32(nvbo
, (push
[i
].offset
+
848 push
[i
].length
- 8) / 4, cmd
);
851 PUSH_JUMP(chan
->chan
.push
, nvbo
->offset
+ push
[i
].offset
);
852 PUSH_DATA(chan
->chan
.push
, 0);
853 for (j
= 0; j
< NOUVEAU_DMA_SKIPS
; j
++)
854 PUSH_DATA(chan
->chan
.push
, 0);
858 ret
= nouveau_fence_new(chan
, false, &fence
);
860 NV_PRINTK(err
, cli
, "error fencing pushbuf: %d\n", ret
);
866 if (!(ret
= nouveau_fence_wait(fence
, false, false))) {
867 if ((ret
= dma_fence_get_status(&fence
->base
)) == 1)
873 validate_fini(&op
, chan
, fence
, bo
);
874 nouveau_fence_unref(&fence
);
877 struct drm_nouveau_gem_pushbuf_bo __user
*upbbo
=
878 u64_to_user_ptr(req
->buffers
);
880 for (i
= 0; i
< req
->nr_buffers
; i
++) {
881 if (bo
[i
].presumed
.valid
)
884 if (copy_to_user(&upbbo
[i
].presumed
, &bo
[i
].presumed
,
885 sizeof(bo
[i
].presumed
))) {
898 if (chan
->dma
.ib_max
) {
899 req
->suffix0
= 0x00000000;
900 req
->suffix1
= 0x00000000;
902 if (drm
->client
.device
.info
.chipset
>= 0x25) {
903 req
->suffix0
= 0x00020000;
904 req
->suffix1
= 0x00000000;
906 req
->suffix0
= 0x20000000 |
907 (chan
->push
.addr
+ ((chan
->dma
.cur
+ 2) << 2));
908 req
->suffix1
= 0x00000000;
911 return nouveau_abi16_put(abi16
, ret
);
915 nouveau_gem_ioctl_cpu_prep(struct drm_device
*dev
, void *data
,
916 struct drm_file
*file_priv
)
918 struct drm_nouveau_gem_cpu_prep
*req
= data
;
919 struct drm_gem_object
*gem
;
920 struct nouveau_bo
*nvbo
;
921 bool no_wait
= !!(req
->flags
& NOUVEAU_GEM_CPU_PREP_NOWAIT
);
922 bool write
= !!(req
->flags
& NOUVEAU_GEM_CPU_PREP_WRITE
);
926 gem
= drm_gem_object_lookup(file_priv
, req
->handle
);
929 nvbo
= nouveau_gem_object(gem
);
931 lret
= dma_resv_wait_timeout_rcu(nvbo
->bo
.base
.resv
, write
, true,
932 no_wait
? 0 : 30 * HZ
);
940 nouveau_bo_sync_for_cpu(nvbo
);
941 drm_gem_object_put(gem
);
947 nouveau_gem_ioctl_cpu_fini(struct drm_device
*dev
, void *data
,
948 struct drm_file
*file_priv
)
950 struct drm_nouveau_gem_cpu_fini
*req
= data
;
951 struct drm_gem_object
*gem
;
952 struct nouveau_bo
*nvbo
;
954 gem
= drm_gem_object_lookup(file_priv
, req
->handle
);
957 nvbo
= nouveau_gem_object(gem
);
959 nouveau_bo_sync_for_device(nvbo
);
960 drm_gem_object_put(gem
);
965 nouveau_gem_ioctl_info(struct drm_device
*dev
, void *data
,
966 struct drm_file
*file_priv
)
968 struct drm_nouveau_gem_info
*req
= data
;
969 struct drm_gem_object
*gem
;
972 gem
= drm_gem_object_lookup(file_priv
, req
->handle
);
976 ret
= nouveau_gem_info(file_priv
, gem
, req
);
977 drm_gem_object_put(gem
);