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
))
52 if (gem
->import_attach
)
53 drm_prime_gem_destroy(gem
, nvbo
->bo
.sg
);
55 drm_gem_object_release(gem
);
57 /* reset filp so nouveau_bo_del_ttm() can test for it */
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_vma
*vma
;
75 if (cli
->vmm
.vmm
.object
.oclass
< NVIF_CLASS_VMM_NV50
)
78 ret
= ttm_bo_reserve(&nvbo
->bo
, false, false, NULL
);
82 ret
= pm_runtime_get_sync(dev
);
83 if (ret
< 0 && ret
!= -EACCES
)
86 ret
= nouveau_vma_new(nvbo
, &cli
->vmm
, &vma
);
87 pm_runtime_mark_last_busy(dev
);
88 pm_runtime_put_autosuspend(dev
);
90 ttm_bo_unreserve(&nvbo
->bo
);
94 struct nouveau_gem_object_unmap
{
95 struct nouveau_cli_work work
;
96 struct nouveau_vma
*vma
;
100 nouveau_gem_object_delete(struct nouveau_vma
*vma
)
102 nouveau_fence_unref(&vma
->fence
);
103 nouveau_vma_del(&vma
);
107 nouveau_gem_object_delete_work(struct nouveau_cli_work
*w
)
109 struct nouveau_gem_object_unmap
*work
=
110 container_of(w
, typeof(*work
), work
);
111 nouveau_gem_object_delete(work
->vma
);
116 nouveau_gem_object_unmap(struct nouveau_bo
*nvbo
, struct nouveau_vma
*vma
)
118 struct dma_fence
*fence
= vma
->fence
? &vma
->fence
->base
: NULL
;
119 struct nouveau_gem_object_unmap
*work
;
121 list_del_init(&vma
->head
);
124 nouveau_gem_object_delete(vma
);
128 if (!(work
= kmalloc(sizeof(*work
), GFP_KERNEL
))) {
129 WARN_ON(dma_fence_wait_timeout(fence
, false, 2 * HZ
) <= 0);
130 nouveau_gem_object_delete(vma
);
134 work
->work
.func
= nouveau_gem_object_delete_work
;
136 nouveau_cli_work_queue(vma
->vmm
->cli
, fence
, &work
->work
);
140 nouveau_gem_object_close(struct drm_gem_object
*gem
, struct drm_file
*file_priv
)
142 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
143 struct nouveau_bo
*nvbo
= nouveau_gem_object(gem
);
144 struct nouveau_drm
*drm
= nouveau_bdev(nvbo
->bo
.bdev
);
145 struct device
*dev
= drm
->dev
->dev
;
146 struct nouveau_vma
*vma
;
149 if (cli
->vmm
.vmm
.object
.oclass
< NVIF_CLASS_VMM_NV50
)
152 ret
= ttm_bo_reserve(&nvbo
->bo
, false, false, NULL
);
156 vma
= nouveau_vma_find(nvbo
, &cli
->vmm
);
158 if (--vma
->refs
== 0) {
159 ret
= pm_runtime_get_sync(dev
);
160 if (!WARN_ON(ret
< 0 && ret
!= -EACCES
)) {
161 nouveau_gem_object_unmap(nvbo
, vma
);
162 pm_runtime_mark_last_busy(dev
);
163 pm_runtime_put_autosuspend(dev
);
167 ttm_bo_unreserve(&nvbo
->bo
);
171 nouveau_gem_new(struct nouveau_cli
*cli
, u64 size
, int align
, uint32_t domain
,
172 uint32_t tile_mode
, uint32_t tile_flags
,
173 struct nouveau_bo
**pnvbo
)
175 struct nouveau_drm
*drm
= cli
->drm
;
176 struct nouveau_bo
*nvbo
;
180 if (domain
& NOUVEAU_GEM_DOMAIN_VRAM
)
181 flags
|= TTM_PL_FLAG_VRAM
;
182 if (domain
& NOUVEAU_GEM_DOMAIN_GART
)
183 flags
|= TTM_PL_FLAG_TT
;
184 if (!flags
|| domain
& NOUVEAU_GEM_DOMAIN_CPU
)
185 flags
|= TTM_PL_FLAG_SYSTEM
;
187 if (domain
& NOUVEAU_GEM_DOMAIN_COHERENT
)
188 flags
|= TTM_PL_FLAG_UNCACHED
;
190 ret
= nouveau_bo_new(cli
, size
, align
, flags
, tile_mode
,
191 tile_flags
, NULL
, NULL
, pnvbo
);
196 /* we restrict allowed domains on nv50+ to only the types
197 * that were requested at creation time. not possibly on
198 * earlier chips without busting the ABI.
200 nvbo
->valid_domains
= NOUVEAU_GEM_DOMAIN_VRAM
|
201 NOUVEAU_GEM_DOMAIN_GART
;
202 if (drm
->client
.device
.info
.family
>= NV_DEVICE_INFO_V0_TESLA
)
203 nvbo
->valid_domains
&= domain
;
205 /* Initialize the embedded gem-object. We return a single gem-reference
206 * to the caller, instead of a normal nouveau_bo ttm reference. */
207 ret
= drm_gem_object_init(drm
->dev
, &nvbo
->gem
, nvbo
->bo
.mem
.size
);
209 nouveau_bo_ref(NULL
, pnvbo
);
213 nvbo
->bo
.persistent_swap_storage
= nvbo
->gem
.filp
;
218 nouveau_gem_info(struct drm_file
*file_priv
, struct drm_gem_object
*gem
,
219 struct drm_nouveau_gem_info
*rep
)
221 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
222 struct nouveau_bo
*nvbo
= nouveau_gem_object(gem
);
223 struct nouveau_vma
*vma
;
225 if (is_power_of_2(nvbo
->valid_domains
))
226 rep
->domain
= nvbo
->valid_domains
;
227 else if (nvbo
->bo
.mem
.mem_type
== TTM_PL_TT
)
228 rep
->domain
= NOUVEAU_GEM_DOMAIN_GART
;
230 rep
->domain
= NOUVEAU_GEM_DOMAIN_VRAM
;
231 rep
->offset
= nvbo
->bo
.offset
;
232 if (cli
->vmm
.vmm
.object
.oclass
>= NVIF_CLASS_VMM_NV50
) {
233 vma
= nouveau_vma_find(nvbo
, &cli
->vmm
);
237 rep
->offset
= vma
->addr
;
240 rep
->size
= nvbo
->bo
.mem
.num_pages
<< PAGE_SHIFT
;
241 rep
->map_handle
= drm_vma_node_offset_addr(&nvbo
->bo
.vma_node
);
242 rep
->tile_mode
= nvbo
->mode
;
243 rep
->tile_flags
= nvbo
->contig
? 0 : NOUVEAU_GEM_TILE_NONCONTIG
;
244 if (cli
->device
.info
.family
>= NV_DEVICE_INFO_V0_FERMI
)
245 rep
->tile_flags
|= nvbo
->kind
<< 8;
247 if (cli
->device
.info
.family
>= NV_DEVICE_INFO_V0_TESLA
)
248 rep
->tile_flags
|= nvbo
->kind
<< 8 | nvbo
->comp
<< 16;
250 rep
->tile_flags
|= nvbo
->zeta
;
255 nouveau_gem_ioctl_new(struct drm_device
*dev
, void *data
,
256 struct drm_file
*file_priv
)
258 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
259 struct drm_nouveau_gem_new
*req
= data
;
260 struct nouveau_bo
*nvbo
= NULL
;
263 ret
= nouveau_gem_new(cli
, req
->info
.size
, req
->align
,
264 req
->info
.domain
, req
->info
.tile_mode
,
265 req
->info
.tile_flags
, &nvbo
);
269 ret
= drm_gem_handle_create(file_priv
, &nvbo
->gem
, &req
->info
.handle
);
271 ret
= nouveau_gem_info(file_priv
, &nvbo
->gem
, &req
->info
);
273 drm_gem_handle_delete(file_priv
, req
->info
.handle
);
276 /* drop reference from allocate - handle holds it now */
277 drm_gem_object_put_unlocked(&nvbo
->gem
);
282 nouveau_gem_set_domain(struct drm_gem_object
*gem
, uint32_t read_domains
,
283 uint32_t write_domains
, uint32_t valid_domains
)
285 struct nouveau_bo
*nvbo
= nouveau_gem_object(gem
);
286 struct ttm_buffer_object
*bo
= &nvbo
->bo
;
287 uint32_t domains
= valid_domains
& nvbo
->valid_domains
&
288 (write_domains
? write_domains
: read_domains
);
289 uint32_t pref_flags
= 0, valid_flags
= 0;
294 if (valid_domains
& NOUVEAU_GEM_DOMAIN_VRAM
)
295 valid_flags
|= TTM_PL_FLAG_VRAM
;
297 if (valid_domains
& NOUVEAU_GEM_DOMAIN_GART
)
298 valid_flags
|= TTM_PL_FLAG_TT
;
300 if ((domains
& NOUVEAU_GEM_DOMAIN_VRAM
) &&
301 bo
->mem
.mem_type
== TTM_PL_VRAM
)
302 pref_flags
|= TTM_PL_FLAG_VRAM
;
304 else if ((domains
& NOUVEAU_GEM_DOMAIN_GART
) &&
305 bo
->mem
.mem_type
== TTM_PL_TT
)
306 pref_flags
|= TTM_PL_FLAG_TT
;
308 else if (domains
& NOUVEAU_GEM_DOMAIN_VRAM
)
309 pref_flags
|= TTM_PL_FLAG_VRAM
;
312 pref_flags
|= TTM_PL_FLAG_TT
;
314 nouveau_bo_placement_set(nvbo
, pref_flags
, valid_flags
);
320 struct list_head list
;
321 struct ww_acquire_ctx ticket
;
325 validate_fini_no_ticket(struct validate_op
*op
, struct nouveau_fence
*fence
,
326 struct drm_nouveau_gem_pushbuf_bo
*pbbo
)
328 struct nouveau_bo
*nvbo
;
329 struct drm_nouveau_gem_pushbuf_bo
*b
;
331 while (!list_empty(&op
->list
)) {
332 nvbo
= list_entry(op
->list
.next
, struct nouveau_bo
, entry
);
333 b
= &pbbo
[nvbo
->pbbo_index
];
336 struct nouveau_drm
*drm
= nouveau_bdev(nvbo
->bo
.bdev
);
337 struct nouveau_vma
*vma
;
339 nouveau_bo_fence(nvbo
, fence
, !!b
->write_domains
);
341 if (drm
->client
.vmm
.vmm
.object
.oclass
>= NVIF_CLASS_VMM_NV50
) {
342 vma
= (void *)(unsigned long)b
->user_priv
;
343 nouveau_fence_unref(&vma
->fence
);
344 dma_fence_get(&fence
->base
);
349 if (unlikely(nvbo
->validate_mapped
)) {
350 ttm_bo_kunmap(&nvbo
->kmap
);
351 nvbo
->validate_mapped
= false;
354 list_del(&nvbo
->entry
);
355 nvbo
->reserved_by
= NULL
;
356 ttm_bo_unreserve(&nvbo
->bo
);
357 drm_gem_object_put_unlocked(&nvbo
->gem
);
362 validate_fini(struct validate_op
*op
, struct nouveau_fence
*fence
,
363 struct drm_nouveau_gem_pushbuf_bo
*pbbo
)
365 validate_fini_no_ticket(op
, fence
, pbbo
);
366 ww_acquire_fini(&op
->ticket
);
370 validate_init(struct nouveau_channel
*chan
, struct drm_file
*file_priv
,
371 struct drm_nouveau_gem_pushbuf_bo
*pbbo
,
372 int nr_buffers
, struct validate_op
*op
)
374 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
376 int ret
= -EINVAL
, i
;
377 struct nouveau_bo
*res_bo
= NULL
;
378 LIST_HEAD(gart_list
);
379 LIST_HEAD(vram_list
);
380 LIST_HEAD(both_list
);
382 ww_acquire_init(&op
->ticket
, &reservation_ww_class
);
384 if (++trycnt
> 100000) {
385 NV_PRINTK(err
, cli
, "%s failed and gave up.\n", __func__
);
389 for (i
= 0; i
< nr_buffers
; i
++) {
390 struct drm_nouveau_gem_pushbuf_bo
*b
= &pbbo
[i
];
391 struct drm_gem_object
*gem
;
392 struct nouveau_bo
*nvbo
;
394 gem
= drm_gem_object_lookup(file_priv
, b
->handle
);
396 NV_PRINTK(err
, cli
, "Unknown handle 0x%08x\n", b
->handle
);
400 nvbo
= nouveau_gem_object(gem
);
401 if (nvbo
== res_bo
) {
403 drm_gem_object_put_unlocked(gem
);
407 if (nvbo
->reserved_by
&& nvbo
->reserved_by
== file_priv
) {
408 NV_PRINTK(err
, cli
, "multiple instances of buffer %d on "
409 "validation list\n", b
->handle
);
410 drm_gem_object_put_unlocked(gem
);
415 ret
= ttm_bo_reserve(&nvbo
->bo
, true, false, &op
->ticket
);
417 list_splice_tail_init(&vram_list
, &op
->list
);
418 list_splice_tail_init(&gart_list
, &op
->list
);
419 list_splice_tail_init(&both_list
, &op
->list
);
420 validate_fini_no_ticket(op
, NULL
, NULL
);
421 if (unlikely(ret
== -EDEADLK
)) {
422 ret
= ttm_bo_reserve_slowpath(&nvbo
->bo
, true,
428 if (ret
!= -ERESTARTSYS
)
429 NV_PRINTK(err
, cli
, "fail reserve\n");
434 if (cli
->vmm
.vmm
.object
.oclass
>= NVIF_CLASS_VMM_NV50
) {
435 struct nouveau_vmm
*vmm
= &cli
->vmm
;
436 struct nouveau_vma
*vma
= nouveau_vma_find(nvbo
, vmm
);
438 NV_PRINTK(err
, cli
, "vma not found!\n");
443 b
->user_priv
= (uint64_t)(unsigned long)vma
;
445 b
->user_priv
= (uint64_t)(unsigned long)nvbo
;
448 nvbo
->reserved_by
= file_priv
;
449 nvbo
->pbbo_index
= i
;
450 if ((b
->valid_domains
& NOUVEAU_GEM_DOMAIN_VRAM
) &&
451 (b
->valid_domains
& NOUVEAU_GEM_DOMAIN_GART
))
452 list_add_tail(&nvbo
->entry
, &both_list
);
454 if (b
->valid_domains
& NOUVEAU_GEM_DOMAIN_VRAM
)
455 list_add_tail(&nvbo
->entry
, &vram_list
);
457 if (b
->valid_domains
& NOUVEAU_GEM_DOMAIN_GART
)
458 list_add_tail(&nvbo
->entry
, &gart_list
);
460 NV_PRINTK(err
, cli
, "invalid valid domains: 0x%08x\n",
462 list_add_tail(&nvbo
->entry
, &both_list
);
470 ww_acquire_done(&op
->ticket
);
471 list_splice_tail(&vram_list
, &op
->list
);
472 list_splice_tail(&gart_list
, &op
->list
);
473 list_splice_tail(&both_list
, &op
->list
);
475 validate_fini(op
, NULL
, NULL
);
481 validate_list(struct nouveau_channel
*chan
, struct nouveau_cli
*cli
,
482 struct list_head
*list
, struct drm_nouveau_gem_pushbuf_bo
*pbbo
,
483 uint64_t user_pbbo_ptr
)
485 struct nouveau_drm
*drm
= chan
->drm
;
486 struct drm_nouveau_gem_pushbuf_bo __user
*upbbo
=
487 (void __force __user
*)(uintptr_t)user_pbbo_ptr
;
488 struct nouveau_bo
*nvbo
;
491 list_for_each_entry(nvbo
, list
, entry
) {
492 struct drm_nouveau_gem_pushbuf_bo
*b
= &pbbo
[nvbo
->pbbo_index
];
494 ret
= nouveau_gem_set_domain(&nvbo
->gem
, b
->read_domains
,
498 NV_PRINTK(err
, cli
, "fail set_domain\n");
502 ret
= nouveau_bo_validate(nvbo
, true, false);
504 if (ret
!= -ERESTARTSYS
)
505 NV_PRINTK(err
, cli
, "fail ttm_validate\n");
509 ret
= nouveau_fence_sync(nvbo
, chan
, !!b
->write_domains
, true);
511 if (ret
!= -ERESTARTSYS
)
512 NV_PRINTK(err
, cli
, "fail post-validate sync\n");
516 if (drm
->client
.device
.info
.family
< NV_DEVICE_INFO_V0_TESLA
) {
517 if (nvbo
->bo
.offset
== b
->presumed
.offset
&&
518 ((nvbo
->bo
.mem
.mem_type
== TTM_PL_VRAM
&&
519 b
->presumed
.domain
& NOUVEAU_GEM_DOMAIN_VRAM
) ||
520 (nvbo
->bo
.mem
.mem_type
== TTM_PL_TT
&&
521 b
->presumed
.domain
& NOUVEAU_GEM_DOMAIN_GART
)))
524 if (nvbo
->bo
.mem
.mem_type
== TTM_PL_TT
)
525 b
->presumed
.domain
= NOUVEAU_GEM_DOMAIN_GART
;
527 b
->presumed
.domain
= NOUVEAU_GEM_DOMAIN_VRAM
;
528 b
->presumed
.offset
= nvbo
->bo
.offset
;
529 b
->presumed
.valid
= 0;
532 if (copy_to_user(&upbbo
[nvbo
->pbbo_index
].presumed
,
533 &b
->presumed
, sizeof(b
->presumed
)))
542 nouveau_gem_pushbuf_validate(struct nouveau_channel
*chan
,
543 struct drm_file
*file_priv
,
544 struct drm_nouveau_gem_pushbuf_bo
*pbbo
,
545 uint64_t user_buffers
, int nr_buffers
,
546 struct validate_op
*op
, int *apply_relocs
)
548 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
551 INIT_LIST_HEAD(&op
->list
);
556 ret
= validate_init(chan
, file_priv
, pbbo
, nr_buffers
, op
);
558 if (ret
!= -ERESTARTSYS
)
559 NV_PRINTK(err
, cli
, "validate_init\n");
563 ret
= validate_list(chan
, cli
, &op
->list
, pbbo
, user_buffers
);
564 if (unlikely(ret
< 0)) {
565 if (ret
!= -ERESTARTSYS
)
566 NV_PRINTK(err
, cli
, "validating bo list\n");
567 validate_fini(op
, NULL
, NULL
);
581 u_memcpya(uint64_t user
, unsigned nmemb
, unsigned size
)
584 void __user
*userptr
= (void __force __user
*)(uintptr_t)user
;
588 mem
= kvmalloc(size
, GFP_KERNEL
);
590 return ERR_PTR(-ENOMEM
);
592 if (copy_from_user(mem
, userptr
, size
)) {
594 return ERR_PTR(-EFAULT
);
601 nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli
*cli
,
602 struct drm_nouveau_gem_pushbuf
*req
,
603 struct drm_nouveau_gem_pushbuf_bo
*bo
)
605 struct drm_nouveau_gem_pushbuf_reloc
*reloc
= NULL
;
609 reloc
= u_memcpya(req
->relocs
, req
->nr_relocs
, sizeof(*reloc
));
611 return PTR_ERR(reloc
);
613 for (i
= 0; i
< req
->nr_relocs
; i
++) {
614 struct drm_nouveau_gem_pushbuf_reloc
*r
= &reloc
[i
];
615 struct drm_nouveau_gem_pushbuf_bo
*b
;
616 struct nouveau_bo
*nvbo
;
619 if (unlikely(r
->bo_index
>= req
->nr_buffers
)) {
620 NV_PRINTK(err
, cli
, "reloc bo index invalid\n");
625 b
= &bo
[r
->bo_index
];
626 if (b
->presumed
.valid
)
629 if (unlikely(r
->reloc_bo_index
>= req
->nr_buffers
)) {
630 NV_PRINTK(err
, cli
, "reloc container bo index invalid\n");
634 nvbo
= (void *)(unsigned long)bo
[r
->reloc_bo_index
].user_priv
;
636 if (unlikely(r
->reloc_bo_offset
+ 4 >
637 nvbo
->bo
.mem
.num_pages
<< PAGE_SHIFT
)) {
638 NV_PRINTK(err
, cli
, "reloc outside of bo\n");
643 if (!nvbo
->kmap
.virtual) {
644 ret
= ttm_bo_kmap(&nvbo
->bo
, 0, nvbo
->bo
.mem
.num_pages
,
647 NV_PRINTK(err
, cli
, "failed kmap for reloc\n");
650 nvbo
->validate_mapped
= true;
653 if (r
->flags
& NOUVEAU_GEM_RELOC_LOW
)
654 data
= b
->presumed
.offset
+ r
->data
;
656 if (r
->flags
& NOUVEAU_GEM_RELOC_HIGH
)
657 data
= (b
->presumed
.offset
+ r
->data
) >> 32;
661 if (r
->flags
& NOUVEAU_GEM_RELOC_OR
) {
662 if (b
->presumed
.domain
== NOUVEAU_GEM_DOMAIN_GART
)
668 ret
= ttm_bo_wait(&nvbo
->bo
, false, false);
670 NV_PRINTK(err
, cli
, "reloc wait_idle failed: %d\n", ret
);
674 nouveau_bo_wr32(nvbo
, r
->reloc_bo_offset
>> 2, data
);
682 nouveau_gem_ioctl_pushbuf(struct drm_device
*dev
, void *data
,
683 struct drm_file
*file_priv
)
685 struct nouveau_abi16
*abi16
= nouveau_abi16_get(file_priv
);
686 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
687 struct nouveau_abi16_chan
*temp
;
688 struct nouveau_drm
*drm
= nouveau_drm(dev
);
689 struct drm_nouveau_gem_pushbuf
*req
= data
;
690 struct drm_nouveau_gem_pushbuf_push
*push
;
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
;
695 int i
, j
, ret
= 0, do_reloc
= 0;
697 if (unlikely(!abi16
))
700 list_for_each_entry(temp
, &abi16
->channels
, head
) {
701 if (temp
->chan
->chid
== req
->channel
) {
708 return nouveau_abi16_put(abi16
, -ENOENT
);
710 req
->vram_available
= drm
->gem
.vram_available
;
711 req
->gart_available
= drm
->gem
.gart_available
;
712 if (unlikely(req
->nr_push
== 0))
715 if (unlikely(req
->nr_push
> NOUVEAU_GEM_MAX_PUSH
)) {
716 NV_PRINTK(err
, cli
, "pushbuf push count exceeds limit: %d max %d\n",
717 req
->nr_push
, NOUVEAU_GEM_MAX_PUSH
);
718 return nouveau_abi16_put(abi16
, -EINVAL
);
721 if (unlikely(req
->nr_buffers
> NOUVEAU_GEM_MAX_BUFFERS
)) {
722 NV_PRINTK(err
, cli
, "pushbuf bo count exceeds limit: %d max %d\n",
723 req
->nr_buffers
, NOUVEAU_GEM_MAX_BUFFERS
);
724 return nouveau_abi16_put(abi16
, -EINVAL
);
727 if (unlikely(req
->nr_relocs
> NOUVEAU_GEM_MAX_RELOCS
)) {
728 NV_PRINTK(err
, cli
, "pushbuf reloc count exceeds limit: %d max %d\n",
729 req
->nr_relocs
, NOUVEAU_GEM_MAX_RELOCS
);
730 return nouveau_abi16_put(abi16
, -EINVAL
);
733 push
= u_memcpya(req
->push
, req
->nr_push
, sizeof(*push
));
735 return nouveau_abi16_put(abi16
, PTR_ERR(push
));
737 bo
= u_memcpya(req
->buffers
, req
->nr_buffers
, sizeof(*bo
));
740 return nouveau_abi16_put(abi16
, PTR_ERR(bo
));
743 /* Ensure all push buffers are on validate list */
744 for (i
= 0; i
< req
->nr_push
; i
++) {
745 if (push
[i
].bo_index
>= req
->nr_buffers
) {
746 NV_PRINTK(err
, cli
, "push %d buffer not in list\n", i
);
752 /* Validate buffer list */
753 ret
= nouveau_gem_pushbuf_validate(chan
, file_priv
, bo
, req
->buffers
,
754 req
->nr_buffers
, &op
, &do_reloc
);
756 if (ret
!= -ERESTARTSYS
)
757 NV_PRINTK(err
, cli
, "validate: %d\n", ret
);
761 /* Apply any relocations that are required */
763 ret
= nouveau_gem_pushbuf_reloc_apply(cli
, req
, bo
);
765 NV_PRINTK(err
, cli
, "reloc apply: %d\n", ret
);
770 if (chan
->dma
.ib_max
) {
771 ret
= nouveau_dma_wait(chan
, req
->nr_push
+ 1, 16);
773 NV_PRINTK(err
, cli
, "nv50cal_space: %d\n", ret
);
777 for (i
= 0; i
< req
->nr_push
; i
++) {
778 struct nouveau_vma
*vma
= (void *)(unsigned long)
779 bo
[push
[i
].bo_index
].user_priv
;
781 nv50_dma_push(chan
, vma
->addr
+ push
[i
].offset
,
785 if (drm
->client
.device
.info
.chipset
>= 0x25) {
786 ret
= RING_SPACE(chan
, req
->nr_push
* 2);
788 NV_PRINTK(err
, cli
, "cal_space: %d\n", ret
);
792 for (i
= 0; i
< req
->nr_push
; i
++) {
793 struct nouveau_bo
*nvbo
= (void *)(unsigned long)
794 bo
[push
[i
].bo_index
].user_priv
;
796 OUT_RING(chan
, (nvbo
->bo
.offset
+ push
[i
].offset
) | 2);
800 ret
= RING_SPACE(chan
, req
->nr_push
* (2 + NOUVEAU_DMA_SKIPS
));
802 NV_PRINTK(err
, cli
, "jmp_space: %d\n", ret
);
806 for (i
= 0; i
< req
->nr_push
; i
++) {
807 struct nouveau_bo
*nvbo
= (void *)(unsigned long)
808 bo
[push
[i
].bo_index
].user_priv
;
811 cmd
= chan
->push
.addr
+ ((chan
->dma
.cur
+ 2) << 2);
813 if (unlikely(cmd
!= req
->suffix0
)) {
814 if (!nvbo
->kmap
.virtual) {
815 ret
= ttm_bo_kmap(&nvbo
->bo
, 0,
823 nvbo
->validate_mapped
= true;
826 nouveau_bo_wr32(nvbo
, (push
[i
].offset
+
827 push
[i
].length
- 8) / 4, cmd
);
830 OUT_RING(chan
, 0x20000000 |
831 (nvbo
->bo
.offset
+ push
[i
].offset
));
833 for (j
= 0; j
< NOUVEAU_DMA_SKIPS
; j
++)
838 ret
= nouveau_fence_new(chan
, false, &fence
);
840 NV_PRINTK(err
, cli
, "error fencing pushbuf: %d\n", ret
);
846 validate_fini(&op
, fence
, bo
);
847 nouveau_fence_unref(&fence
);
854 if (chan
->dma
.ib_max
) {
855 req
->suffix0
= 0x00000000;
856 req
->suffix1
= 0x00000000;
858 if (drm
->client
.device
.info
.chipset
>= 0x25) {
859 req
->suffix0
= 0x00020000;
860 req
->suffix1
= 0x00000000;
862 req
->suffix0
= 0x20000000 |
863 (chan
->push
.addr
+ ((chan
->dma
.cur
+ 2) << 2));
864 req
->suffix1
= 0x00000000;
867 return nouveau_abi16_put(abi16
, ret
);
871 nouveau_gem_ioctl_cpu_prep(struct drm_device
*dev
, void *data
,
872 struct drm_file
*file_priv
)
874 struct drm_nouveau_gem_cpu_prep
*req
= data
;
875 struct drm_gem_object
*gem
;
876 struct nouveau_bo
*nvbo
;
877 bool no_wait
= !!(req
->flags
& NOUVEAU_GEM_CPU_PREP_NOWAIT
);
878 bool write
= !!(req
->flags
& NOUVEAU_GEM_CPU_PREP_WRITE
);
882 gem
= drm_gem_object_lookup(file_priv
, req
->handle
);
885 nvbo
= nouveau_gem_object(gem
);
887 lret
= reservation_object_wait_timeout_rcu(nvbo
->bo
.resv
, write
, true,
888 no_wait
? 0 : 30 * HZ
);
896 nouveau_bo_sync_for_cpu(nvbo
);
897 drm_gem_object_put_unlocked(gem
);
903 nouveau_gem_ioctl_cpu_fini(struct drm_device
*dev
, void *data
,
904 struct drm_file
*file_priv
)
906 struct drm_nouveau_gem_cpu_fini
*req
= data
;
907 struct drm_gem_object
*gem
;
908 struct nouveau_bo
*nvbo
;
910 gem
= drm_gem_object_lookup(file_priv
, req
->handle
);
913 nvbo
= nouveau_gem_object(gem
);
915 nouveau_bo_sync_for_device(nvbo
);
916 drm_gem_object_put_unlocked(gem
);
921 nouveau_gem_ioctl_info(struct drm_device
*dev
, void *data
,
922 struct drm_file
*file_priv
)
924 struct drm_nouveau_gem_info
*req
= data
;
925 struct drm_gem_object
*gem
;
928 gem
= drm_gem_object_lookup(file_priv
, req
->handle
);
932 ret
= nouveau_gem_info(file_priv
, gem
, req
);
933 drm_gem_object_put_unlocked(gem
);