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_vma_del(&vma
);
106 nouveau_gem_object_delete_work(struct nouveau_cli_work
*w
)
108 struct nouveau_gem_object_unmap
*work
=
109 container_of(w
, typeof(*work
), work
);
110 nouveau_gem_object_delete(work
->vma
);
115 nouveau_gem_object_unmap(struct nouveau_bo
*nvbo
, struct nouveau_vma
*vma
)
117 const bool mapped
= nvbo
->bo
.mem
.mem_type
!= TTM_PL_SYSTEM
;
118 struct reservation_object
*resv
= nvbo
->bo
.resv
;
119 struct reservation_object_list
*fobj
;
120 struct nouveau_gem_object_unmap
*work
;
121 struct dma_fence
*fence
= NULL
;
123 fobj
= reservation_object_get_list(resv
);
125 list_del_init(&vma
->head
);
127 if (fobj
&& fobj
->shared_count
> 1)
128 ttm_bo_wait(&nvbo
->bo
, false, false);
129 else if (fobj
&& fobj
->shared_count
== 1)
130 fence
= rcu_dereference_protected(fobj
->shared
[0],
131 reservation_object_held(resv
));
133 fence
= reservation_object_get_excl(nvbo
->bo
.resv
);
135 if (!fence
|| !mapped
) {
136 nouveau_gem_object_delete(vma
);
140 if (!(work
= kmalloc(sizeof(*work
), GFP_KERNEL
))) {
141 WARN_ON(dma_fence_wait_timeout(fence
, false, 2 * HZ
) <= 0);
142 nouveau_gem_object_delete(vma
);
146 work
->work
.func
= nouveau_gem_object_delete_work
;
148 nouveau_cli_work_queue(vma
->vmm
->cli
, fence
, &work
->work
);
152 nouveau_gem_object_close(struct drm_gem_object
*gem
, struct drm_file
*file_priv
)
154 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
155 struct nouveau_bo
*nvbo
= nouveau_gem_object(gem
);
156 struct nouveau_drm
*drm
= nouveau_bdev(nvbo
->bo
.bdev
);
157 struct device
*dev
= drm
->dev
->dev
;
158 struct nouveau_vma
*vma
;
161 if (cli
->vmm
.vmm
.object
.oclass
< NVIF_CLASS_VMM_NV50
)
164 ret
= ttm_bo_reserve(&nvbo
->bo
, false, false, NULL
);
168 vma
= nouveau_vma_find(nvbo
, &cli
->vmm
);
170 if (--vma
->refs
== 0) {
171 ret
= pm_runtime_get_sync(dev
);
172 if (!WARN_ON(ret
< 0 && ret
!= -EACCES
)) {
173 nouveau_gem_object_unmap(nvbo
, vma
);
174 pm_runtime_mark_last_busy(dev
);
175 pm_runtime_put_autosuspend(dev
);
179 ttm_bo_unreserve(&nvbo
->bo
);
183 nouveau_gem_new(struct nouveau_cli
*cli
, u64 size
, int align
, uint32_t domain
,
184 uint32_t tile_mode
, uint32_t tile_flags
,
185 struct nouveau_bo
**pnvbo
)
187 struct nouveau_drm
*drm
= cli
->drm
;
188 struct nouveau_bo
*nvbo
;
192 if (domain
& NOUVEAU_GEM_DOMAIN_VRAM
)
193 flags
|= TTM_PL_FLAG_VRAM
;
194 if (domain
& NOUVEAU_GEM_DOMAIN_GART
)
195 flags
|= TTM_PL_FLAG_TT
;
196 if (!flags
|| domain
& NOUVEAU_GEM_DOMAIN_CPU
)
197 flags
|= TTM_PL_FLAG_SYSTEM
;
199 if (domain
& NOUVEAU_GEM_DOMAIN_COHERENT
)
200 flags
|= TTM_PL_FLAG_UNCACHED
;
202 ret
= nouveau_bo_new(cli
, size
, align
, flags
, tile_mode
,
203 tile_flags
, NULL
, NULL
, pnvbo
);
208 /* we restrict allowed domains on nv50+ to only the types
209 * that were requested at creation time. not possibly on
210 * earlier chips without busting the ABI.
212 nvbo
->valid_domains
= NOUVEAU_GEM_DOMAIN_VRAM
|
213 NOUVEAU_GEM_DOMAIN_GART
;
214 if (drm
->client
.device
.info
.family
>= NV_DEVICE_INFO_V0_TESLA
)
215 nvbo
->valid_domains
&= domain
;
217 /* Initialize the embedded gem-object. We return a single gem-reference
218 * to the caller, instead of a normal nouveau_bo ttm reference. */
219 ret
= drm_gem_object_init(drm
->dev
, &nvbo
->gem
, nvbo
->bo
.mem
.size
);
221 nouveau_bo_ref(NULL
, pnvbo
);
225 nvbo
->bo
.persistent_swap_storage
= nvbo
->gem
.filp
;
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_vma
*vma
;
237 if (is_power_of_2(nvbo
->valid_domains
))
238 rep
->domain
= nvbo
->valid_domains
;
239 else if (nvbo
->bo
.mem
.mem_type
== TTM_PL_TT
)
240 rep
->domain
= NOUVEAU_GEM_DOMAIN_GART
;
242 rep
->domain
= NOUVEAU_GEM_DOMAIN_VRAM
;
243 rep
->offset
= nvbo
->bo
.offset
;
244 if (cli
->vmm
.vmm
.object
.oclass
>= NVIF_CLASS_VMM_NV50
) {
245 vma
= nouveau_vma_find(nvbo
, &cli
->vmm
);
249 rep
->offset
= vma
->addr
;
252 rep
->size
= nvbo
->bo
.mem
.num_pages
<< PAGE_SHIFT
;
253 rep
->map_handle
= drm_vma_node_offset_addr(&nvbo
->bo
.vma_node
);
254 rep
->tile_mode
= nvbo
->mode
;
255 rep
->tile_flags
= nvbo
->contig
? 0 : NOUVEAU_GEM_TILE_NONCONTIG
;
256 if (cli
->device
.info
.family
>= NV_DEVICE_INFO_V0_FERMI
)
257 rep
->tile_flags
|= nvbo
->kind
<< 8;
259 if (cli
->device
.info
.family
>= NV_DEVICE_INFO_V0_TESLA
)
260 rep
->tile_flags
|= nvbo
->kind
<< 8 | nvbo
->comp
<< 16;
262 rep
->tile_flags
|= nvbo
->zeta
;
267 nouveau_gem_ioctl_new(struct drm_device
*dev
, void *data
,
268 struct drm_file
*file_priv
)
270 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
271 struct drm_nouveau_gem_new
*req
= data
;
272 struct nouveau_bo
*nvbo
= NULL
;
275 ret
= nouveau_gem_new(cli
, req
->info
.size
, req
->align
,
276 req
->info
.domain
, req
->info
.tile_mode
,
277 req
->info
.tile_flags
, &nvbo
);
281 ret
= drm_gem_handle_create(file_priv
, &nvbo
->gem
, &req
->info
.handle
);
283 ret
= nouveau_gem_info(file_priv
, &nvbo
->gem
, &req
->info
);
285 drm_gem_handle_delete(file_priv
, req
->info
.handle
);
288 /* drop reference from allocate - handle holds it now */
289 drm_gem_object_unreference_unlocked(&nvbo
->gem
);
294 nouveau_gem_set_domain(struct drm_gem_object
*gem
, uint32_t read_domains
,
295 uint32_t write_domains
, uint32_t valid_domains
)
297 struct nouveau_bo
*nvbo
= nouveau_gem_object(gem
);
298 struct ttm_buffer_object
*bo
= &nvbo
->bo
;
299 uint32_t domains
= valid_domains
& nvbo
->valid_domains
&
300 (write_domains
? write_domains
: read_domains
);
301 uint32_t pref_flags
= 0, valid_flags
= 0;
306 if (valid_domains
& NOUVEAU_GEM_DOMAIN_VRAM
)
307 valid_flags
|= TTM_PL_FLAG_VRAM
;
309 if (valid_domains
& NOUVEAU_GEM_DOMAIN_GART
)
310 valid_flags
|= TTM_PL_FLAG_TT
;
312 if ((domains
& NOUVEAU_GEM_DOMAIN_VRAM
) &&
313 bo
->mem
.mem_type
== TTM_PL_VRAM
)
314 pref_flags
|= TTM_PL_FLAG_VRAM
;
316 else if ((domains
& NOUVEAU_GEM_DOMAIN_GART
) &&
317 bo
->mem
.mem_type
== TTM_PL_TT
)
318 pref_flags
|= TTM_PL_FLAG_TT
;
320 else if (domains
& NOUVEAU_GEM_DOMAIN_VRAM
)
321 pref_flags
|= TTM_PL_FLAG_VRAM
;
324 pref_flags
|= TTM_PL_FLAG_TT
;
326 nouveau_bo_placement_set(nvbo
, pref_flags
, valid_flags
);
332 struct list_head list
;
333 struct ww_acquire_ctx ticket
;
337 validate_fini_no_ticket(struct validate_op
*op
, struct nouveau_fence
*fence
,
338 struct drm_nouveau_gem_pushbuf_bo
*pbbo
)
340 struct nouveau_bo
*nvbo
;
341 struct drm_nouveau_gem_pushbuf_bo
*b
;
343 while (!list_empty(&op
->list
)) {
344 nvbo
= list_entry(op
->list
.next
, struct nouveau_bo
, entry
);
345 b
= &pbbo
[nvbo
->pbbo_index
];
348 nouveau_bo_fence(nvbo
, fence
, !!b
->write_domains
);
350 if (unlikely(nvbo
->validate_mapped
)) {
351 ttm_bo_kunmap(&nvbo
->kmap
);
352 nvbo
->validate_mapped
= false;
355 list_del(&nvbo
->entry
);
356 nvbo
->reserved_by
= NULL
;
357 ttm_bo_unreserve(&nvbo
->bo
);
358 drm_gem_object_unreference_unlocked(&nvbo
->gem
);
363 validate_fini(struct validate_op
*op
, struct nouveau_fence
*fence
,
364 struct drm_nouveau_gem_pushbuf_bo
*pbbo
)
366 validate_fini_no_ticket(op
, fence
, pbbo
);
367 ww_acquire_fini(&op
->ticket
);
371 validate_init(struct nouveau_channel
*chan
, struct drm_file
*file_priv
,
372 struct drm_nouveau_gem_pushbuf_bo
*pbbo
,
373 int nr_buffers
, struct validate_op
*op
)
375 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
377 int ret
= -EINVAL
, i
;
378 struct nouveau_bo
*res_bo
= NULL
;
379 LIST_HEAD(gart_list
);
380 LIST_HEAD(vram_list
);
381 LIST_HEAD(both_list
);
383 ww_acquire_init(&op
->ticket
, &reservation_ww_class
);
385 if (++trycnt
> 100000) {
386 NV_PRINTK(err
, cli
, "%s failed and gave up.\n", __func__
);
390 for (i
= 0; i
< nr_buffers
; i
++) {
391 struct drm_nouveau_gem_pushbuf_bo
*b
= &pbbo
[i
];
392 struct drm_gem_object
*gem
;
393 struct nouveau_bo
*nvbo
;
395 gem
= drm_gem_object_lookup(file_priv
, b
->handle
);
397 NV_PRINTK(err
, cli
, "Unknown handle 0x%08x\n", b
->handle
);
401 nvbo
= nouveau_gem_object(gem
);
402 if (nvbo
== res_bo
) {
404 drm_gem_object_unreference_unlocked(gem
);
408 if (nvbo
->reserved_by
&& nvbo
->reserved_by
== file_priv
) {
409 NV_PRINTK(err
, cli
, "multiple instances of buffer %d on "
410 "validation list\n", b
->handle
);
411 drm_gem_object_unreference_unlocked(gem
);
416 ret
= ttm_bo_reserve(&nvbo
->bo
, true, false, &op
->ticket
);
418 list_splice_tail_init(&vram_list
, &op
->list
);
419 list_splice_tail_init(&gart_list
, &op
->list
);
420 list_splice_tail_init(&both_list
, &op
->list
);
421 validate_fini_no_ticket(op
, NULL
, NULL
);
422 if (unlikely(ret
== -EDEADLK
)) {
423 ret
= ttm_bo_reserve_slowpath(&nvbo
->bo
, true,
429 if (ret
!= -ERESTARTSYS
)
430 NV_PRINTK(err
, cli
, "fail reserve\n");
435 b
->user_priv
= (uint64_t)(unsigned long)nvbo
;
436 nvbo
->reserved_by
= file_priv
;
437 nvbo
->pbbo_index
= i
;
438 if ((b
->valid_domains
& NOUVEAU_GEM_DOMAIN_VRAM
) &&
439 (b
->valid_domains
& NOUVEAU_GEM_DOMAIN_GART
))
440 list_add_tail(&nvbo
->entry
, &both_list
);
442 if (b
->valid_domains
& NOUVEAU_GEM_DOMAIN_VRAM
)
443 list_add_tail(&nvbo
->entry
, &vram_list
);
445 if (b
->valid_domains
& NOUVEAU_GEM_DOMAIN_GART
)
446 list_add_tail(&nvbo
->entry
, &gart_list
);
448 NV_PRINTK(err
, cli
, "invalid valid domains: 0x%08x\n",
450 list_add_tail(&nvbo
->entry
, &both_list
);
458 ww_acquire_done(&op
->ticket
);
459 list_splice_tail(&vram_list
, &op
->list
);
460 list_splice_tail(&gart_list
, &op
->list
);
461 list_splice_tail(&both_list
, &op
->list
);
463 validate_fini(op
, NULL
, NULL
);
469 validate_list(struct nouveau_channel
*chan
, struct nouveau_cli
*cli
,
470 struct list_head
*list
, struct drm_nouveau_gem_pushbuf_bo
*pbbo
,
471 uint64_t user_pbbo_ptr
)
473 struct nouveau_drm
*drm
= chan
->drm
;
474 struct drm_nouveau_gem_pushbuf_bo __user
*upbbo
=
475 (void __force __user
*)(uintptr_t)user_pbbo_ptr
;
476 struct nouveau_bo
*nvbo
;
479 list_for_each_entry(nvbo
, list
, entry
) {
480 struct drm_nouveau_gem_pushbuf_bo
*b
= &pbbo
[nvbo
->pbbo_index
];
482 ret
= nouveau_gem_set_domain(&nvbo
->gem
, b
->read_domains
,
486 NV_PRINTK(err
, cli
, "fail set_domain\n");
490 ret
= nouveau_bo_validate(nvbo
, true, false);
492 if (ret
!= -ERESTARTSYS
)
493 NV_PRINTK(err
, cli
, "fail ttm_validate\n");
497 ret
= nouveau_fence_sync(nvbo
, chan
, !!b
->write_domains
, true);
499 if (ret
!= -ERESTARTSYS
)
500 NV_PRINTK(err
, cli
, "fail post-validate sync\n");
504 if (drm
->client
.device
.info
.family
< NV_DEVICE_INFO_V0_TESLA
) {
505 if (nvbo
->bo
.offset
== b
->presumed
.offset
&&
506 ((nvbo
->bo
.mem
.mem_type
== TTM_PL_VRAM
&&
507 b
->presumed
.domain
& NOUVEAU_GEM_DOMAIN_VRAM
) ||
508 (nvbo
->bo
.mem
.mem_type
== TTM_PL_TT
&&
509 b
->presumed
.domain
& NOUVEAU_GEM_DOMAIN_GART
)))
512 if (nvbo
->bo
.mem
.mem_type
== TTM_PL_TT
)
513 b
->presumed
.domain
= NOUVEAU_GEM_DOMAIN_GART
;
515 b
->presumed
.domain
= NOUVEAU_GEM_DOMAIN_VRAM
;
516 b
->presumed
.offset
= nvbo
->bo
.offset
;
517 b
->presumed
.valid
= 0;
520 if (copy_to_user(&upbbo
[nvbo
->pbbo_index
].presumed
,
521 &b
->presumed
, sizeof(b
->presumed
)))
530 nouveau_gem_pushbuf_validate(struct nouveau_channel
*chan
,
531 struct drm_file
*file_priv
,
532 struct drm_nouveau_gem_pushbuf_bo
*pbbo
,
533 uint64_t user_buffers
, int nr_buffers
,
534 struct validate_op
*op
, int *apply_relocs
)
536 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
539 INIT_LIST_HEAD(&op
->list
);
544 ret
= validate_init(chan
, file_priv
, pbbo
, nr_buffers
, op
);
546 if (ret
!= -ERESTARTSYS
)
547 NV_PRINTK(err
, cli
, "validate_init\n");
551 ret
= validate_list(chan
, cli
, &op
->list
, pbbo
, user_buffers
);
552 if (unlikely(ret
< 0)) {
553 if (ret
!= -ERESTARTSYS
)
554 NV_PRINTK(err
, cli
, "validating bo list\n");
555 validate_fini(op
, NULL
, NULL
);
569 u_memcpya(uint64_t user
, unsigned nmemb
, unsigned size
)
572 void __user
*userptr
= (void __force __user
*)(uintptr_t)user
;
576 mem
= kvmalloc(size
, GFP_KERNEL
);
578 return ERR_PTR(-ENOMEM
);
580 if (copy_from_user(mem
, userptr
, size
)) {
582 return ERR_PTR(-EFAULT
);
589 nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli
*cli
,
590 struct drm_nouveau_gem_pushbuf
*req
,
591 struct drm_nouveau_gem_pushbuf_bo
*bo
)
593 struct drm_nouveau_gem_pushbuf_reloc
*reloc
= NULL
;
597 reloc
= u_memcpya(req
->relocs
, req
->nr_relocs
, sizeof(*reloc
));
599 return PTR_ERR(reloc
);
601 for (i
= 0; i
< req
->nr_relocs
; i
++) {
602 struct drm_nouveau_gem_pushbuf_reloc
*r
= &reloc
[i
];
603 struct drm_nouveau_gem_pushbuf_bo
*b
;
604 struct nouveau_bo
*nvbo
;
607 if (unlikely(r
->bo_index
> req
->nr_buffers
)) {
608 NV_PRINTK(err
, cli
, "reloc bo index invalid\n");
613 b
= &bo
[r
->bo_index
];
614 if (b
->presumed
.valid
)
617 if (unlikely(r
->reloc_bo_index
> req
->nr_buffers
)) {
618 NV_PRINTK(err
, cli
, "reloc container bo index invalid\n");
622 nvbo
= (void *)(unsigned long)bo
[r
->reloc_bo_index
].user_priv
;
624 if (unlikely(r
->reloc_bo_offset
+ 4 >
625 nvbo
->bo
.mem
.num_pages
<< PAGE_SHIFT
)) {
626 NV_PRINTK(err
, cli
, "reloc outside of bo\n");
631 if (!nvbo
->kmap
.virtual) {
632 ret
= ttm_bo_kmap(&nvbo
->bo
, 0, nvbo
->bo
.mem
.num_pages
,
635 NV_PRINTK(err
, cli
, "failed kmap for reloc\n");
638 nvbo
->validate_mapped
= true;
641 if (r
->flags
& NOUVEAU_GEM_RELOC_LOW
)
642 data
= b
->presumed
.offset
+ r
->data
;
644 if (r
->flags
& NOUVEAU_GEM_RELOC_HIGH
)
645 data
= (b
->presumed
.offset
+ r
->data
) >> 32;
649 if (r
->flags
& NOUVEAU_GEM_RELOC_OR
) {
650 if (b
->presumed
.domain
== NOUVEAU_GEM_DOMAIN_GART
)
656 ret
= ttm_bo_wait(&nvbo
->bo
, false, false);
658 NV_PRINTK(err
, cli
, "reloc wait_idle failed: %d\n", ret
);
662 nouveau_bo_wr32(nvbo
, r
->reloc_bo_offset
>> 2, data
);
670 nouveau_gem_ioctl_pushbuf(struct drm_device
*dev
, void *data
,
671 struct drm_file
*file_priv
)
673 struct nouveau_abi16
*abi16
= nouveau_abi16_get(file_priv
);
674 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
675 struct nouveau_abi16_chan
*temp
;
676 struct nouveau_drm
*drm
= nouveau_drm(dev
);
677 struct drm_nouveau_gem_pushbuf
*req
= data
;
678 struct drm_nouveau_gem_pushbuf_push
*push
;
679 struct drm_nouveau_gem_pushbuf_bo
*bo
;
680 struct nouveau_channel
*chan
= NULL
;
681 struct validate_op op
;
682 struct nouveau_fence
*fence
= NULL
;
683 int i
, j
, ret
= 0, do_reloc
= 0;
685 if (unlikely(!abi16
))
688 list_for_each_entry(temp
, &abi16
->channels
, head
) {
689 if (temp
->chan
->chid
== req
->channel
) {
696 return nouveau_abi16_put(abi16
, -ENOENT
);
698 req
->vram_available
= drm
->gem
.vram_available
;
699 req
->gart_available
= drm
->gem
.gart_available
;
700 if (unlikely(req
->nr_push
== 0))
703 if (unlikely(req
->nr_push
> NOUVEAU_GEM_MAX_PUSH
)) {
704 NV_PRINTK(err
, cli
, "pushbuf push count exceeds limit: %d max %d\n",
705 req
->nr_push
, NOUVEAU_GEM_MAX_PUSH
);
706 return nouveau_abi16_put(abi16
, -EINVAL
);
709 if (unlikely(req
->nr_buffers
> NOUVEAU_GEM_MAX_BUFFERS
)) {
710 NV_PRINTK(err
, cli
, "pushbuf bo count exceeds limit: %d max %d\n",
711 req
->nr_buffers
, NOUVEAU_GEM_MAX_BUFFERS
);
712 return nouveau_abi16_put(abi16
, -EINVAL
);
715 if (unlikely(req
->nr_relocs
> NOUVEAU_GEM_MAX_RELOCS
)) {
716 NV_PRINTK(err
, cli
, "pushbuf reloc count exceeds limit: %d max %d\n",
717 req
->nr_relocs
, NOUVEAU_GEM_MAX_RELOCS
);
718 return nouveau_abi16_put(abi16
, -EINVAL
);
721 push
= u_memcpya(req
->push
, req
->nr_push
, sizeof(*push
));
723 return nouveau_abi16_put(abi16
, PTR_ERR(push
));
725 bo
= u_memcpya(req
->buffers
, req
->nr_buffers
, sizeof(*bo
));
728 return nouveau_abi16_put(abi16
, PTR_ERR(bo
));
731 /* Ensure all push buffers are on validate list */
732 for (i
= 0; i
< req
->nr_push
; i
++) {
733 if (push
[i
].bo_index
>= req
->nr_buffers
) {
734 NV_PRINTK(err
, cli
, "push %d buffer not in list\n", i
);
740 /* Validate buffer list */
741 ret
= nouveau_gem_pushbuf_validate(chan
, file_priv
, bo
, req
->buffers
,
742 req
->nr_buffers
, &op
, &do_reloc
);
744 if (ret
!= -ERESTARTSYS
)
745 NV_PRINTK(err
, cli
, "validate: %d\n", ret
);
749 /* Apply any relocations that are required */
751 ret
= nouveau_gem_pushbuf_reloc_apply(cli
, req
, bo
);
753 NV_PRINTK(err
, cli
, "reloc apply: %d\n", ret
);
758 if (chan
->dma
.ib_max
) {
759 ret
= nouveau_dma_wait(chan
, req
->nr_push
+ 1, 16);
761 NV_PRINTK(err
, cli
, "nv50cal_space: %d\n", ret
);
765 for (i
= 0; i
< req
->nr_push
; i
++) {
766 struct nouveau_bo
*nvbo
= (void *)(unsigned long)
767 bo
[push
[i
].bo_index
].user_priv
;
769 nv50_dma_push(chan
, nvbo
, push
[i
].offset
,
773 if (drm
->client
.device
.info
.chipset
>= 0x25) {
774 ret
= RING_SPACE(chan
, req
->nr_push
* 2);
776 NV_PRINTK(err
, cli
, "cal_space: %d\n", ret
);
780 for (i
= 0; i
< req
->nr_push
; i
++) {
781 struct nouveau_bo
*nvbo
= (void *)(unsigned long)
782 bo
[push
[i
].bo_index
].user_priv
;
784 OUT_RING(chan
, (nvbo
->bo
.offset
+ push
[i
].offset
) | 2);
788 ret
= RING_SPACE(chan
, req
->nr_push
* (2 + NOUVEAU_DMA_SKIPS
));
790 NV_PRINTK(err
, cli
, "jmp_space: %d\n", ret
);
794 for (i
= 0; i
< req
->nr_push
; i
++) {
795 struct nouveau_bo
*nvbo
= (void *)(unsigned long)
796 bo
[push
[i
].bo_index
].user_priv
;
799 cmd
= chan
->push
.addr
+ ((chan
->dma
.cur
+ 2) << 2);
801 if (unlikely(cmd
!= req
->suffix0
)) {
802 if (!nvbo
->kmap
.virtual) {
803 ret
= ttm_bo_kmap(&nvbo
->bo
, 0,
811 nvbo
->validate_mapped
= true;
814 nouveau_bo_wr32(nvbo
, (push
[i
].offset
+
815 push
[i
].length
- 8) / 4, cmd
);
818 OUT_RING(chan
, 0x20000000 |
819 (nvbo
->bo
.offset
+ push
[i
].offset
));
821 for (j
= 0; j
< NOUVEAU_DMA_SKIPS
; j
++)
826 ret
= nouveau_fence_new(chan
, false, &fence
);
828 NV_PRINTK(err
, cli
, "error fencing pushbuf: %d\n", ret
);
834 validate_fini(&op
, fence
, bo
);
835 nouveau_fence_unref(&fence
);
842 if (chan
->dma
.ib_max
) {
843 req
->suffix0
= 0x00000000;
844 req
->suffix1
= 0x00000000;
846 if (drm
->client
.device
.info
.chipset
>= 0x25) {
847 req
->suffix0
= 0x00020000;
848 req
->suffix1
= 0x00000000;
850 req
->suffix0
= 0x20000000 |
851 (chan
->push
.addr
+ ((chan
->dma
.cur
+ 2) << 2));
852 req
->suffix1
= 0x00000000;
855 return nouveau_abi16_put(abi16
, ret
);
859 nouveau_gem_ioctl_cpu_prep(struct drm_device
*dev
, void *data
,
860 struct drm_file
*file_priv
)
862 struct drm_nouveau_gem_cpu_prep
*req
= data
;
863 struct drm_gem_object
*gem
;
864 struct nouveau_bo
*nvbo
;
865 bool no_wait
= !!(req
->flags
& NOUVEAU_GEM_CPU_PREP_NOWAIT
);
866 bool write
= !!(req
->flags
& NOUVEAU_GEM_CPU_PREP_WRITE
);
870 gem
= drm_gem_object_lookup(file_priv
, req
->handle
);
873 nvbo
= nouveau_gem_object(gem
);
875 lret
= reservation_object_wait_timeout_rcu(nvbo
->bo
.resv
, write
, true,
876 no_wait
? 0 : 30 * HZ
);
884 nouveau_bo_sync_for_cpu(nvbo
);
885 drm_gem_object_unreference_unlocked(gem
);
891 nouveau_gem_ioctl_cpu_fini(struct drm_device
*dev
, void *data
,
892 struct drm_file
*file_priv
)
894 struct drm_nouveau_gem_cpu_fini
*req
= data
;
895 struct drm_gem_object
*gem
;
896 struct nouveau_bo
*nvbo
;
898 gem
= drm_gem_object_lookup(file_priv
, req
->handle
);
901 nvbo
= nouveau_gem_object(gem
);
903 nouveau_bo_sync_for_device(nvbo
);
904 drm_gem_object_unreference_unlocked(gem
);
909 nouveau_gem_ioctl_info(struct drm_device
*dev
, void *data
,
910 struct drm_file
*file_priv
)
912 struct drm_nouveau_gem_info
*req
= data
;
913 struct drm_gem_object
*gem
;
916 gem
= drm_gem_object_lookup(file_priv
, req
->handle
);
920 ret
= nouveau_gem_info(file_priv
, gem
, req
);
921 drm_gem_object_unreference_unlocked(gem
);