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_ticket(&nvbo
->bo
, &op
->ticket
);
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 if (cli
->vmm
.vmm
.object
.oclass
>= NVIF_CLASS_VMM_NV50
) {
436 struct nouveau_vmm
*vmm
= &cli
->vmm
;
437 struct nouveau_vma
*vma
= nouveau_vma_find(nvbo
, vmm
);
439 NV_PRINTK(err
, cli
, "vma not found!\n");
444 b
->user_priv
= (uint64_t)(unsigned long)vma
;
446 b
->user_priv
= (uint64_t)(unsigned long)nvbo
;
449 nvbo
->reserved_by
= file_priv
;
450 nvbo
->pbbo_index
= i
;
451 if ((b
->valid_domains
& NOUVEAU_GEM_DOMAIN_VRAM
) &&
452 (b
->valid_domains
& NOUVEAU_GEM_DOMAIN_GART
))
453 list_add_tail(&nvbo
->entry
, &both_list
);
455 if (b
->valid_domains
& NOUVEAU_GEM_DOMAIN_VRAM
)
456 list_add_tail(&nvbo
->entry
, &vram_list
);
458 if (b
->valid_domains
& NOUVEAU_GEM_DOMAIN_GART
)
459 list_add_tail(&nvbo
->entry
, &gart_list
);
461 NV_PRINTK(err
, cli
, "invalid valid domains: 0x%08x\n",
463 list_add_tail(&nvbo
->entry
, &both_list
);
471 ww_acquire_done(&op
->ticket
);
472 list_splice_tail(&vram_list
, &op
->list
);
473 list_splice_tail(&gart_list
, &op
->list
);
474 list_splice_tail(&both_list
, &op
->list
);
476 validate_fini(op
, NULL
, NULL
);
482 validate_list(struct nouveau_channel
*chan
, struct nouveau_cli
*cli
,
483 struct list_head
*list
, struct drm_nouveau_gem_pushbuf_bo
*pbbo
,
484 uint64_t user_pbbo_ptr
)
486 struct nouveau_drm
*drm
= chan
->drm
;
487 struct drm_nouveau_gem_pushbuf_bo __user
*upbbo
=
488 (void __force __user
*)(uintptr_t)user_pbbo_ptr
;
489 struct nouveau_bo
*nvbo
;
492 list_for_each_entry(nvbo
, list
, entry
) {
493 struct drm_nouveau_gem_pushbuf_bo
*b
= &pbbo
[nvbo
->pbbo_index
];
495 ret
= nouveau_gem_set_domain(&nvbo
->gem
, b
->read_domains
,
499 NV_PRINTK(err
, cli
, "fail set_domain\n");
503 ret
= nouveau_bo_validate(nvbo
, true, false);
505 if (ret
!= -ERESTARTSYS
)
506 NV_PRINTK(err
, cli
, "fail ttm_validate\n");
510 ret
= nouveau_fence_sync(nvbo
, chan
, !!b
->write_domains
, true);
512 if (ret
!= -ERESTARTSYS
)
513 NV_PRINTK(err
, cli
, "fail post-validate sync\n");
517 if (drm
->client
.device
.info
.family
< NV_DEVICE_INFO_V0_TESLA
) {
518 if (nvbo
->bo
.offset
== b
->presumed
.offset
&&
519 ((nvbo
->bo
.mem
.mem_type
== TTM_PL_VRAM
&&
520 b
->presumed
.domain
& NOUVEAU_GEM_DOMAIN_VRAM
) ||
521 (nvbo
->bo
.mem
.mem_type
== TTM_PL_TT
&&
522 b
->presumed
.domain
& NOUVEAU_GEM_DOMAIN_GART
)))
525 if (nvbo
->bo
.mem
.mem_type
== TTM_PL_TT
)
526 b
->presumed
.domain
= NOUVEAU_GEM_DOMAIN_GART
;
528 b
->presumed
.domain
= NOUVEAU_GEM_DOMAIN_VRAM
;
529 b
->presumed
.offset
= nvbo
->bo
.offset
;
530 b
->presumed
.valid
= 0;
533 if (copy_to_user(&upbbo
[nvbo
->pbbo_index
].presumed
,
534 &b
->presumed
, sizeof(b
->presumed
)))
543 nouveau_gem_pushbuf_validate(struct nouveau_channel
*chan
,
544 struct drm_file
*file_priv
,
545 struct drm_nouveau_gem_pushbuf_bo
*pbbo
,
546 uint64_t user_buffers
, int nr_buffers
,
547 struct validate_op
*op
, int *apply_relocs
)
549 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
552 INIT_LIST_HEAD(&op
->list
);
557 ret
= validate_init(chan
, file_priv
, pbbo
, nr_buffers
, op
);
559 if (ret
!= -ERESTARTSYS
)
560 NV_PRINTK(err
, cli
, "validate_init\n");
564 ret
= validate_list(chan
, cli
, &op
->list
, pbbo
, user_buffers
);
565 if (unlikely(ret
< 0)) {
566 if (ret
!= -ERESTARTSYS
)
567 NV_PRINTK(err
, cli
, "validating bo list\n");
568 validate_fini(op
, NULL
, NULL
);
582 u_memcpya(uint64_t user
, unsigned nmemb
, unsigned size
)
585 void __user
*userptr
= (void __force __user
*)(uintptr_t)user
;
589 mem
= kvmalloc(size
, GFP_KERNEL
);
591 return ERR_PTR(-ENOMEM
);
593 if (copy_from_user(mem
, userptr
, size
)) {
595 return ERR_PTR(-EFAULT
);
602 nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli
*cli
,
603 struct drm_nouveau_gem_pushbuf
*req
,
604 struct drm_nouveau_gem_pushbuf_bo
*bo
)
606 struct drm_nouveau_gem_pushbuf_reloc
*reloc
= NULL
;
610 reloc
= u_memcpya(req
->relocs
, req
->nr_relocs
, sizeof(*reloc
));
612 return PTR_ERR(reloc
);
614 for (i
= 0; i
< req
->nr_relocs
; i
++) {
615 struct drm_nouveau_gem_pushbuf_reloc
*r
= &reloc
[i
];
616 struct drm_nouveau_gem_pushbuf_bo
*b
;
617 struct nouveau_bo
*nvbo
;
620 if (unlikely(r
->bo_index
>= req
->nr_buffers
)) {
621 NV_PRINTK(err
, cli
, "reloc bo index invalid\n");
626 b
= &bo
[r
->bo_index
];
627 if (b
->presumed
.valid
)
630 if (unlikely(r
->reloc_bo_index
>= req
->nr_buffers
)) {
631 NV_PRINTK(err
, cli
, "reloc container bo index invalid\n");
635 nvbo
= (void *)(unsigned long)bo
[r
->reloc_bo_index
].user_priv
;
637 if (unlikely(r
->reloc_bo_offset
+ 4 >
638 nvbo
->bo
.mem
.num_pages
<< PAGE_SHIFT
)) {
639 NV_PRINTK(err
, cli
, "reloc outside of bo\n");
644 if (!nvbo
->kmap
.virtual) {
645 ret
= ttm_bo_kmap(&nvbo
->bo
, 0, nvbo
->bo
.mem
.num_pages
,
648 NV_PRINTK(err
, cli
, "failed kmap for reloc\n");
651 nvbo
->validate_mapped
= true;
654 if (r
->flags
& NOUVEAU_GEM_RELOC_LOW
)
655 data
= b
->presumed
.offset
+ r
->data
;
657 if (r
->flags
& NOUVEAU_GEM_RELOC_HIGH
)
658 data
= (b
->presumed
.offset
+ r
->data
) >> 32;
662 if (r
->flags
& NOUVEAU_GEM_RELOC_OR
) {
663 if (b
->presumed
.domain
== NOUVEAU_GEM_DOMAIN_GART
)
669 ret
= ttm_bo_wait(&nvbo
->bo
, false, false);
671 NV_PRINTK(err
, cli
, "reloc wait_idle failed: %d\n", ret
);
675 nouveau_bo_wr32(nvbo
, r
->reloc_bo_offset
>> 2, data
);
683 nouveau_gem_ioctl_pushbuf(struct drm_device
*dev
, void *data
,
684 struct drm_file
*file_priv
)
686 struct nouveau_abi16
*abi16
= nouveau_abi16_get(file_priv
);
687 struct nouveau_cli
*cli
= nouveau_cli(file_priv
);
688 struct nouveau_abi16_chan
*temp
;
689 struct nouveau_drm
*drm
= nouveau_drm(dev
);
690 struct drm_nouveau_gem_pushbuf
*req
= data
;
691 struct drm_nouveau_gem_pushbuf_push
*push
;
692 struct drm_nouveau_gem_pushbuf_bo
*bo
;
693 struct nouveau_channel
*chan
= NULL
;
694 struct validate_op op
;
695 struct nouveau_fence
*fence
= NULL
;
696 int i
, j
, ret
= 0, do_reloc
= 0;
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
);
711 req
->vram_available
= drm
->gem
.vram_available
;
712 req
->gart_available
= drm
->gem
.gart_available
;
713 if (unlikely(req
->nr_push
== 0))
716 if (unlikely(req
->nr_push
> NOUVEAU_GEM_MAX_PUSH
)) {
717 NV_PRINTK(err
, cli
, "pushbuf push count exceeds limit: %d max %d\n",
718 req
->nr_push
, NOUVEAU_GEM_MAX_PUSH
);
719 return nouveau_abi16_put(abi16
, -EINVAL
);
722 if (unlikely(req
->nr_buffers
> NOUVEAU_GEM_MAX_BUFFERS
)) {
723 NV_PRINTK(err
, cli
, "pushbuf bo count exceeds limit: %d max %d\n",
724 req
->nr_buffers
, NOUVEAU_GEM_MAX_BUFFERS
);
725 return nouveau_abi16_put(abi16
, -EINVAL
);
728 if (unlikely(req
->nr_relocs
> NOUVEAU_GEM_MAX_RELOCS
)) {
729 NV_PRINTK(err
, cli
, "pushbuf reloc count exceeds limit: %d max %d\n",
730 req
->nr_relocs
, NOUVEAU_GEM_MAX_RELOCS
);
731 return nouveau_abi16_put(abi16
, -EINVAL
);
734 push
= u_memcpya(req
->push
, req
->nr_push
, sizeof(*push
));
736 return nouveau_abi16_put(abi16
, PTR_ERR(push
));
738 bo
= u_memcpya(req
->buffers
, req
->nr_buffers
, sizeof(*bo
));
741 return nouveau_abi16_put(abi16
, PTR_ERR(bo
));
744 /* Ensure all push buffers are on validate list */
745 for (i
= 0; i
< req
->nr_push
; i
++) {
746 if (push
[i
].bo_index
>= req
->nr_buffers
) {
747 NV_PRINTK(err
, cli
, "push %d buffer not in list\n", i
);
753 /* Validate buffer list */
754 ret
= nouveau_gem_pushbuf_validate(chan
, file_priv
, bo
, req
->buffers
,
755 req
->nr_buffers
, &op
, &do_reloc
);
757 if (ret
!= -ERESTARTSYS
)
758 NV_PRINTK(err
, cli
, "validate: %d\n", ret
);
762 /* Apply any relocations that are required */
764 ret
= nouveau_gem_pushbuf_reloc_apply(cli
, req
, bo
);
766 NV_PRINTK(err
, cli
, "reloc apply: %d\n", ret
);
771 if (chan
->dma
.ib_max
) {
772 ret
= nouveau_dma_wait(chan
, req
->nr_push
+ 1, 16);
774 NV_PRINTK(err
, cli
, "nv50cal_space: %d\n", ret
);
778 for (i
= 0; i
< req
->nr_push
; i
++) {
779 struct nouveau_vma
*vma
= (void *)(unsigned long)
780 bo
[push
[i
].bo_index
].user_priv
;
782 nv50_dma_push(chan
, vma
->addr
+ push
[i
].offset
,
786 if (drm
->client
.device
.info
.chipset
>= 0x25) {
787 ret
= RING_SPACE(chan
, req
->nr_push
* 2);
789 NV_PRINTK(err
, cli
, "cal_space: %d\n", ret
);
793 for (i
= 0; i
< req
->nr_push
; i
++) {
794 struct nouveau_bo
*nvbo
= (void *)(unsigned long)
795 bo
[push
[i
].bo_index
].user_priv
;
797 OUT_RING(chan
, (nvbo
->bo
.offset
+ push
[i
].offset
) | 2);
801 ret
= RING_SPACE(chan
, req
->nr_push
* (2 + NOUVEAU_DMA_SKIPS
));
803 NV_PRINTK(err
, cli
, "jmp_space: %d\n", ret
);
807 for (i
= 0; i
< req
->nr_push
; i
++) {
808 struct nouveau_bo
*nvbo
= (void *)(unsigned long)
809 bo
[push
[i
].bo_index
].user_priv
;
812 cmd
= chan
->push
.addr
+ ((chan
->dma
.cur
+ 2) << 2);
814 if (unlikely(cmd
!= req
->suffix0
)) {
815 if (!nvbo
->kmap
.virtual) {
816 ret
= ttm_bo_kmap(&nvbo
->bo
, 0,
824 nvbo
->validate_mapped
= true;
827 nouveau_bo_wr32(nvbo
, (push
[i
].offset
+
828 push
[i
].length
- 8) / 4, cmd
);
831 OUT_RING(chan
, 0x20000000 |
832 (nvbo
->bo
.offset
+ push
[i
].offset
));
834 for (j
= 0; j
< NOUVEAU_DMA_SKIPS
; j
++)
839 ret
= nouveau_fence_new(chan
, false, &fence
);
841 NV_PRINTK(err
, cli
, "error fencing pushbuf: %d\n", ret
);
847 validate_fini(&op
, fence
, bo
);
848 nouveau_fence_unref(&fence
);
855 if (chan
->dma
.ib_max
) {
856 req
->suffix0
= 0x00000000;
857 req
->suffix1
= 0x00000000;
859 if (drm
->client
.device
.info
.chipset
>= 0x25) {
860 req
->suffix0
= 0x00020000;
861 req
->suffix1
= 0x00000000;
863 req
->suffix0
= 0x20000000 |
864 (chan
->push
.addr
+ ((chan
->dma
.cur
+ 2) << 2));
865 req
->suffix1
= 0x00000000;
868 return nouveau_abi16_put(abi16
, ret
);
872 nouveau_gem_ioctl_cpu_prep(struct drm_device
*dev
, void *data
,
873 struct drm_file
*file_priv
)
875 struct drm_nouveau_gem_cpu_prep
*req
= data
;
876 struct drm_gem_object
*gem
;
877 struct nouveau_bo
*nvbo
;
878 bool no_wait
= !!(req
->flags
& NOUVEAU_GEM_CPU_PREP_NOWAIT
);
879 bool write
= !!(req
->flags
& NOUVEAU_GEM_CPU_PREP_WRITE
);
883 gem
= drm_gem_object_lookup(file_priv
, req
->handle
);
886 nvbo
= nouveau_gem_object(gem
);
888 lret
= reservation_object_wait_timeout_rcu(nvbo
->bo
.resv
, write
, true,
889 no_wait
? 0 : 30 * HZ
);
897 nouveau_bo_sync_for_cpu(nvbo
);
898 drm_gem_object_unreference_unlocked(gem
);
904 nouveau_gem_ioctl_cpu_fini(struct drm_device
*dev
, void *data
,
905 struct drm_file
*file_priv
)
907 struct drm_nouveau_gem_cpu_fini
*req
= data
;
908 struct drm_gem_object
*gem
;
909 struct nouveau_bo
*nvbo
;
911 gem
= drm_gem_object_lookup(file_priv
, req
->handle
);
914 nvbo
= nouveau_gem_object(gem
);
916 nouveau_bo_sync_for_device(nvbo
);
917 drm_gem_object_unreference_unlocked(gem
);
922 nouveau_gem_ioctl_info(struct drm_device
*dev
, void *data
,
923 struct drm_file
*file_priv
)
925 struct drm_nouveau_gem_info
*req
= data
;
926 struct drm_gem_object
*gem
;
929 gem
= drm_gem_object_lookup(file_priv
, req
->handle
);
933 ret
= nouveau_gem_info(file_priv
, gem
, req
);
934 drm_gem_object_unreference_unlocked(gem
);