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.
29 #include "nouveau_drv.h"
30 #include "nouveau_drm.h"
31 #include "nouveau_dma.h"
33 #define nouveau_gem_pushbuf_sync(chan) 0
36 nouveau_gem_object_new(struct drm_gem_object
*gem
)
42 nouveau_gem_object_del(struct drm_gem_object
*gem
)
44 struct nouveau_bo
*nvbo
= gem
->driver_private
;
45 struct ttm_buffer_object
*bo
= &nvbo
->bo
;
51 if (unlikely(nvbo
->cpu_filp
))
52 ttm_bo_synccpu_write_release(bo
);
54 if (unlikely(nvbo
->pin_refcnt
)) {
56 nouveau_bo_unpin(nvbo
);
63 nouveau_gem_new(struct drm_device
*dev
, struct nouveau_channel
*chan
,
64 int size
, int align
, uint32_t flags
, uint32_t tile_mode
,
65 uint32_t tile_flags
, bool no_vm
, bool mappable
,
66 struct nouveau_bo
**pnvbo
)
68 struct nouveau_bo
*nvbo
;
71 ret
= nouveau_bo_new(dev
, chan
, size
, align
, flags
, tile_mode
,
72 tile_flags
, no_vm
, mappable
, pnvbo
);
77 nvbo
->gem
= drm_gem_object_alloc(dev
, nvbo
->bo
.mem
.size
);
79 nouveau_bo_ref(NULL
, pnvbo
);
83 nvbo
->bo
.persistant_swap_storage
= nvbo
->gem
->filp
;
84 nvbo
->gem
->driver_private
= nvbo
;
89 nouveau_gem_info(struct drm_gem_object
*gem
, struct drm_nouveau_gem_info
*rep
)
91 struct nouveau_bo
*nvbo
= nouveau_gem_object(gem
);
93 if (nvbo
->bo
.mem
.mem_type
== TTM_PL_TT
)
94 rep
->domain
= NOUVEAU_GEM_DOMAIN_GART
;
96 rep
->domain
= NOUVEAU_GEM_DOMAIN_VRAM
;
98 rep
->size
= nvbo
->bo
.mem
.num_pages
<< PAGE_SHIFT
;
99 rep
->offset
= nvbo
->bo
.offset
;
100 rep
->map_handle
= nvbo
->mappable
? nvbo
->bo
.addr_space_offset
: 0;
101 rep
->tile_mode
= nvbo
->tile_mode
;
102 rep
->tile_flags
= nvbo
->tile_flags
;
107 nouveau_gem_tile_flags_valid(struct drm_device
*dev
, uint32_t tile_flags
) {
108 switch (tile_flags
) {
119 NV_ERROR(dev
, "bad page flags: 0x%08x\n", tile_flags
);
127 nouveau_gem_ioctl_new(struct drm_device
*dev
, void *data
,
128 struct drm_file
*file_priv
)
130 struct drm_nouveau_private
*dev_priv
= dev
->dev_private
;
131 struct drm_nouveau_gem_new
*req
= data
;
132 struct nouveau_bo
*nvbo
= NULL
;
133 struct nouveau_channel
*chan
= NULL
;
137 NOUVEAU_CHECK_INITIALISED_WITH_RETURN
;
139 if (unlikely(dev_priv
->ttm
.bdev
.dev_mapping
== NULL
))
140 dev_priv
->ttm
.bdev
.dev_mapping
= dev_priv
->dev
->dev_mapping
;
142 if (req
->channel_hint
) {
143 NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(req
->channel_hint
,
147 if (req
->info
.domain
& NOUVEAU_GEM_DOMAIN_VRAM
)
148 flags
|= TTM_PL_FLAG_VRAM
;
149 if (req
->info
.domain
& NOUVEAU_GEM_DOMAIN_GART
)
150 flags
|= TTM_PL_FLAG_TT
;
151 if (!flags
|| req
->info
.domain
& NOUVEAU_GEM_DOMAIN_CPU
)
152 flags
|= TTM_PL_FLAG_SYSTEM
;
154 if (!nouveau_gem_tile_flags_valid(dev
, req
->info
.tile_flags
))
157 ret
= nouveau_gem_new(dev
, chan
, req
->info
.size
, req
->align
, flags
,
158 req
->info
.tile_mode
, req
->info
.tile_flags
, false,
159 (req
->info
.domain
& NOUVEAU_GEM_DOMAIN_MAPPABLE
),
164 ret
= nouveau_gem_info(nvbo
->gem
, &req
->info
);
168 ret
= drm_gem_handle_create(file_priv
, nvbo
->gem
, &req
->info
.handle
);
170 mutex_lock(&dev
->struct_mutex
);
171 drm_gem_object_handle_unreference(nvbo
->gem
);
172 mutex_unlock(&dev
->struct_mutex
);
175 drm_gem_object_unreference(nvbo
->gem
);
180 nouveau_gem_set_domain(struct drm_gem_object
*gem
, uint32_t read_domains
,
181 uint32_t write_domains
, uint32_t valid_domains
)
183 struct nouveau_bo
*nvbo
= gem
->driver_private
;
184 struct ttm_buffer_object
*bo
= &nvbo
->bo
;
187 if (!valid_domains
|| (!read_domains
&& !write_domains
))
191 if ((valid_domains
& NOUVEAU_GEM_DOMAIN_VRAM
) &&
192 (write_domains
& NOUVEAU_GEM_DOMAIN_VRAM
))
193 flags
= TTM_PL_FLAG_VRAM
;
195 if ((valid_domains
& NOUVEAU_GEM_DOMAIN_GART
) &&
196 (write_domains
& NOUVEAU_GEM_DOMAIN_GART
))
197 flags
= TTM_PL_FLAG_TT
;
201 if ((valid_domains
& NOUVEAU_GEM_DOMAIN_VRAM
) &&
202 (read_domains
& NOUVEAU_GEM_DOMAIN_VRAM
) &&
203 bo
->mem
.mem_type
== TTM_PL_VRAM
)
204 flags
= TTM_PL_FLAG_VRAM
;
206 if ((valid_domains
& NOUVEAU_GEM_DOMAIN_GART
) &&
207 (read_domains
& NOUVEAU_GEM_DOMAIN_GART
) &&
208 bo
->mem
.mem_type
== TTM_PL_TT
)
209 flags
= TTM_PL_FLAG_TT
;
211 if ((valid_domains
& NOUVEAU_GEM_DOMAIN_VRAM
) &&
212 (read_domains
& NOUVEAU_GEM_DOMAIN_VRAM
))
213 flags
= TTM_PL_FLAG_VRAM
;
215 flags
= TTM_PL_FLAG_TT
;
218 nouveau_bo_placement_set(nvbo
, flags
);
223 struct nouveau_fence
*fence
;
224 struct list_head vram_list
;
225 struct list_head gart_list
;
226 struct list_head both_list
;
230 validate_fini_list(struct list_head
*list
, struct nouveau_fence
*fence
)
232 struct list_head
*entry
, *tmp
;
233 struct nouveau_bo
*nvbo
;
235 list_for_each_safe(entry
, tmp
, list
) {
236 nvbo
= list_entry(entry
, struct nouveau_bo
, entry
);
238 struct nouveau_fence
*prev_fence
;
240 spin_lock(&nvbo
->bo
.lock
);
241 prev_fence
= nvbo
->bo
.sync_obj
;
242 nvbo
->bo
.sync_obj
= nouveau_fence_ref(fence
);
243 spin_unlock(&nvbo
->bo
.lock
);
244 nouveau_fence_unref((void *)&prev_fence
);
247 list_del(&nvbo
->entry
);
248 nvbo
->reserved_by
= NULL
;
249 ttm_bo_unreserve(&nvbo
->bo
);
250 drm_gem_object_unreference(nvbo
->gem
);
255 validate_fini(struct validate_op
*op
, bool success
)
257 struct nouveau_fence
*fence
= op
->fence
;
259 if (unlikely(!success
))
262 validate_fini_list(&op
->vram_list
, op
->fence
);
263 validate_fini_list(&op
->gart_list
, op
->fence
);
264 validate_fini_list(&op
->both_list
, op
->fence
);
265 nouveau_fence_unref((void *)&fence
);
269 validate_init(struct nouveau_channel
*chan
, struct drm_file
*file_priv
,
270 struct drm_nouveau_gem_pushbuf_bo
*pbbo
,
271 int nr_buffers
, struct validate_op
*op
)
273 struct drm_device
*dev
= chan
->dev
;
274 struct drm_nouveau_private
*dev_priv
= dev
->dev_private
;
279 sequence
= atomic_add_return(1, &dev_priv
->ttm
.validate_sequence
);
281 if (++trycnt
> 100000) {
282 NV_ERROR(dev
, "%s failed and gave up.\n", __func__
);
286 for (i
= 0; i
< nr_buffers
; i
++) {
287 struct drm_nouveau_gem_pushbuf_bo
*b
= &pbbo
[i
];
288 struct drm_gem_object
*gem
;
289 struct nouveau_bo
*nvbo
;
291 gem
= drm_gem_object_lookup(dev
, file_priv
, b
->handle
);
293 NV_ERROR(dev
, "Unknown handle 0x%08x\n", b
->handle
);
294 validate_fini(op
, NULL
);
297 nvbo
= gem
->driver_private
;
299 if (nvbo
->reserved_by
&& nvbo
->reserved_by
== file_priv
) {
300 NV_ERROR(dev
, "multiple instances of buffer %d on "
301 "validation list\n", b
->handle
);
302 validate_fini(op
, NULL
);
306 ret
= ttm_bo_reserve(&nvbo
->bo
, false, false, true, sequence
);
308 validate_fini(op
, NULL
);
310 ret
= ttm_bo_wait_unreserved(&nvbo
->bo
, false);
311 drm_gem_object_unreference(gem
);
317 nvbo
->reserved_by
= file_priv
;
318 nvbo
->pbbo_index
= i
;
319 if ((b
->valid_domains
& NOUVEAU_GEM_DOMAIN_VRAM
) &&
320 (b
->valid_domains
& NOUVEAU_GEM_DOMAIN_GART
))
321 list_add_tail(&nvbo
->entry
, &op
->both_list
);
323 if (b
->valid_domains
& NOUVEAU_GEM_DOMAIN_VRAM
)
324 list_add_tail(&nvbo
->entry
, &op
->vram_list
);
326 if (b
->valid_domains
& NOUVEAU_GEM_DOMAIN_GART
)
327 list_add_tail(&nvbo
->entry
, &op
->gart_list
);
329 NV_ERROR(dev
, "invalid valid domains: 0x%08x\n",
331 validate_fini(op
, NULL
);
335 if (unlikely(atomic_read(&nvbo
->bo
.cpu_writers
) > 0)) {
336 validate_fini(op
, NULL
);
338 if (nvbo
->cpu_filp
== file_priv
) {
339 NV_ERROR(dev
, "bo %p mapped by process trying "
340 "to validate it!\n", nvbo
);
344 ret
= ttm_bo_wait_cpu(&nvbo
->bo
, false);
345 if (ret
== -ERESTART
)
357 validate_list(struct nouveau_channel
*chan
, struct list_head
*list
,
358 struct drm_nouveau_gem_pushbuf_bo
*pbbo
, uint64_t user_pbbo_ptr
)
360 struct drm_nouveau_gem_pushbuf_bo __user
*upbbo
=
361 (void __force __user
*)(uintptr_t)user_pbbo_ptr
;
362 struct nouveau_bo
*nvbo
;
365 list_for_each_entry(nvbo
, list
, entry
) {
366 struct drm_nouveau_gem_pushbuf_bo
*b
= &pbbo
[nvbo
->pbbo_index
];
367 struct nouveau_fence
*prev_fence
= nvbo
->bo
.sync_obj
;
369 if (prev_fence
&& nouveau_fence_channel(prev_fence
) != chan
) {
370 spin_lock(&nvbo
->bo
.lock
);
371 ret
= ttm_bo_wait(&nvbo
->bo
, false, false, false);
372 spin_unlock(&nvbo
->bo
.lock
);
377 ret
= nouveau_gem_set_domain(nvbo
->gem
, b
->read_domains
,
383 nvbo
->channel
= chan
;
384 ret
= ttm_bo_validate(&nvbo
->bo
, &nvbo
->placement
,
386 nvbo
->channel
= NULL
;
390 if (nvbo
->bo
.offset
== b
->presumed_offset
&&
391 ((nvbo
->bo
.mem
.mem_type
== TTM_PL_VRAM
&&
392 b
->presumed_domain
& NOUVEAU_GEM_DOMAIN_VRAM
) ||
393 (nvbo
->bo
.mem
.mem_type
== TTM_PL_TT
&&
394 b
->presumed_domain
& NOUVEAU_GEM_DOMAIN_GART
)))
397 if (nvbo
->bo
.mem
.mem_type
== TTM_PL_TT
)
398 b
->presumed_domain
= NOUVEAU_GEM_DOMAIN_GART
;
400 b
->presumed_domain
= NOUVEAU_GEM_DOMAIN_VRAM
;
401 b
->presumed_offset
= nvbo
->bo
.offset
;
405 if (DRM_COPY_TO_USER(&upbbo
[nvbo
->pbbo_index
], b
, sizeof(*b
)))
413 nouveau_gem_pushbuf_validate(struct nouveau_channel
*chan
,
414 struct drm_file
*file_priv
,
415 struct drm_nouveau_gem_pushbuf_bo
*pbbo
,
416 uint64_t user_buffers
, int nr_buffers
,
417 struct validate_op
*op
, int *apply_relocs
)
421 INIT_LIST_HEAD(&op
->vram_list
);
422 INIT_LIST_HEAD(&op
->gart_list
);
423 INIT_LIST_HEAD(&op
->both_list
);
425 ret
= nouveau_fence_new(chan
, &op
->fence
, false);
432 ret
= validate_init(chan
, file_priv
, pbbo
, nr_buffers
, op
);
436 ret
= validate_list(chan
, &op
->vram_list
, pbbo
, user_buffers
);
437 if (unlikely(ret
< 0)) {
438 validate_fini(op
, NULL
);
443 ret
= validate_list(chan
, &op
->gart_list
, pbbo
, user_buffers
);
444 if (unlikely(ret
< 0)) {
445 validate_fini(op
, NULL
);
450 ret
= validate_list(chan
, &op
->both_list
, pbbo
, user_buffers
);
451 if (unlikely(ret
< 0)) {
452 validate_fini(op
, NULL
);
457 *apply_relocs
= relocs
;
462 u_memcpya(uint64_t user
, unsigned nmemb
, unsigned size
)
465 void __user
*userptr
= (void __force __user
*)(uintptr_t)user
;
467 mem
= kmalloc(nmemb
* size
, GFP_KERNEL
);
469 return ERR_PTR(-ENOMEM
);
471 if (DRM_COPY_FROM_USER(mem
, userptr
, nmemb
* size
)) {
473 return ERR_PTR(-EFAULT
);
480 nouveau_gem_pushbuf_reloc_apply(struct nouveau_channel
*chan
, int nr_bo
,
481 struct drm_nouveau_gem_pushbuf_bo
*bo
,
482 int nr_relocs
, uint64_t ptr_relocs
,
483 int nr_dwords
, int first_dword
,
484 uint32_t *pushbuf
, bool is_iomem
)
486 struct drm_nouveau_gem_pushbuf_reloc
*reloc
= NULL
;
487 struct drm_device
*dev
= chan
->dev
;
490 reloc
= u_memcpya(ptr_relocs
, nr_relocs
, sizeof(*reloc
));
492 return PTR_ERR(reloc
);
494 for (i
= 0; i
< nr_relocs
; i
++) {
495 struct drm_nouveau_gem_pushbuf_reloc
*r
= &reloc
[i
];
496 struct drm_nouveau_gem_pushbuf_bo
*b
;
499 if (r
->bo_index
>= nr_bo
|| r
->reloc_index
< first_dword
||
500 r
->reloc_index
>= first_dword
+ nr_dwords
) {
501 NV_ERROR(dev
, "Bad relocation %d\n", i
);
502 NV_ERROR(dev
, " bo: %d max %d\n", r
->bo_index
, nr_bo
);
503 NV_ERROR(dev
, " id: %d max %d\n", r
->reloc_index
, nr_dwords
);
508 b
= &bo
[r
->bo_index
];
512 if (r
->flags
& NOUVEAU_GEM_RELOC_LOW
)
513 data
= b
->presumed_offset
+ r
->data
;
515 if (r
->flags
& NOUVEAU_GEM_RELOC_HIGH
)
516 data
= (b
->presumed_offset
+ r
->data
) >> 32;
520 if (r
->flags
& NOUVEAU_GEM_RELOC_OR
) {
521 if (b
->presumed_domain
== NOUVEAU_GEM_DOMAIN_GART
)
528 iowrite32_native(data
, (void __force __iomem
*)
529 &pushbuf
[r
->reloc_index
]);
531 pushbuf
[r
->reloc_index
] = data
;
539 nouveau_gem_ioctl_pushbuf(struct drm_device
*dev
, void *data
,
540 struct drm_file
*file_priv
)
542 struct drm_nouveau_gem_pushbuf
*req
= data
;
543 struct drm_nouveau_gem_pushbuf_bo
*bo
= NULL
;
544 struct nouveau_channel
*chan
;
545 struct validate_op op
;
546 uint32_t *pushbuf
= NULL
;
547 int ret
= 0, do_reloc
= 0, i
;
549 NOUVEAU_CHECK_INITIALISED_WITH_RETURN
;
550 NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(req
->channel
, file_priv
, chan
);
552 if (req
->nr_dwords
>= chan
->dma
.max
||
553 req
->nr_buffers
> NOUVEAU_GEM_MAX_BUFFERS
||
554 req
->nr_relocs
> NOUVEAU_GEM_MAX_RELOCS
) {
555 NV_ERROR(dev
, "Pushbuf config exceeds limits:\n");
556 NV_ERROR(dev
, " dwords : %d max %d\n", req
->nr_dwords
,
558 NV_ERROR(dev
, " buffers: %d max %d\n", req
->nr_buffers
,
559 NOUVEAU_GEM_MAX_BUFFERS
);
560 NV_ERROR(dev
, " relocs : %d max %d\n", req
->nr_relocs
,
561 NOUVEAU_GEM_MAX_RELOCS
);
565 pushbuf
= u_memcpya(req
->dwords
, req
->nr_dwords
, sizeof(uint32_t));
567 return PTR_ERR(pushbuf
);
569 bo
= u_memcpya(req
->buffers
, req
->nr_buffers
, sizeof(*bo
));
575 mutex_lock(&dev
->struct_mutex
);
577 /* Validate buffer list */
578 ret
= nouveau_gem_pushbuf_validate(chan
, file_priv
, bo
, req
->buffers
,
579 req
->nr_buffers
, &op
, &do_reloc
);
583 /* Apply any relocations that are required */
585 ret
= nouveau_gem_pushbuf_reloc_apply(chan
, req
->nr_buffers
,
594 /* Emit push buffer to the hw
596 ret
= RING_SPACE(chan
, req
->nr_dwords
);
600 OUT_RINGp(chan
, pushbuf
, req
->nr_dwords
);
602 ret
= nouveau_fence_emit(op
.fence
);
604 NV_ERROR(dev
, "error fencing pushbuf: %d\n", ret
);
609 if (nouveau_gem_pushbuf_sync(chan
)) {
610 ret
= nouveau_fence_wait(op
.fence
, NULL
, false, false);
612 for (i
= 0; i
< req
->nr_dwords
; i
++)
613 NV_ERROR(dev
, "0x%08x\n", pushbuf
[i
]);
614 NV_ERROR(dev
, "^^ above push buffer is fail :(\n");
619 validate_fini(&op
, ret
== 0);
620 mutex_unlock(&dev
->struct_mutex
);
626 #define PUSHBUF_CAL (dev_priv->card_type >= NV_20)
629 nouveau_gem_ioctl_pushbuf_call(struct drm_device
*dev
, void *data
,
630 struct drm_file
*file_priv
)
632 struct drm_nouveau_private
*dev_priv
= dev
->dev_private
;
633 struct drm_nouveau_gem_pushbuf_call
*req
= data
;
634 struct drm_nouveau_gem_pushbuf_bo
*bo
= NULL
;
635 struct nouveau_channel
*chan
;
636 struct drm_gem_object
*gem
;
637 struct nouveau_bo
*pbbo
;
638 struct validate_op op
;
639 int i
, ret
= 0, do_reloc
= 0;
641 NOUVEAU_CHECK_INITIALISED_WITH_RETURN
;
642 NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(req
->channel
, file_priv
, chan
);
644 if (unlikely(req
->handle
== 0))
647 if (req
->nr_buffers
> NOUVEAU_GEM_MAX_BUFFERS
||
648 req
->nr_relocs
> NOUVEAU_GEM_MAX_RELOCS
) {
649 NV_ERROR(dev
, "Pushbuf config exceeds limits:\n");
650 NV_ERROR(dev
, " buffers: %d max %d\n", req
->nr_buffers
,
651 NOUVEAU_GEM_MAX_BUFFERS
);
652 NV_ERROR(dev
, " relocs : %d max %d\n", req
->nr_relocs
,
653 NOUVEAU_GEM_MAX_RELOCS
);
657 bo
= u_memcpya(req
->buffers
, req
->nr_buffers
, sizeof(*bo
));
661 mutex_lock(&dev
->struct_mutex
);
663 /* Validate buffer list */
664 ret
= nouveau_gem_pushbuf_validate(chan
, file_priv
, bo
, req
->buffers
,
665 req
->nr_buffers
, &op
, &do_reloc
);
667 NV_ERROR(dev
, "validate: %d\n", ret
);
671 /* Validate DMA push buffer */
672 gem
= drm_gem_object_lookup(dev
, file_priv
, req
->handle
);
674 NV_ERROR(dev
, "Unknown pb handle 0x%08x\n", req
->handle
);
678 pbbo
= nouveau_gem_object(gem
);
680 ret
= ttm_bo_reserve(&pbbo
->bo
, false, false, true,
681 chan
->fence
.sequence
);
683 NV_ERROR(dev
, "resv pb: %d\n", ret
);
684 drm_gem_object_unreference(gem
);
688 nouveau_bo_placement_set(pbbo
, 1 << chan
->pushbuf_bo
->bo
.mem
.mem_type
);
689 ret
= ttm_bo_validate(&pbbo
->bo
, &pbbo
->placement
, false, false);
691 NV_ERROR(dev
, "validate pb: %d\n", ret
);
692 ttm_bo_unreserve(&pbbo
->bo
);
693 drm_gem_object_unreference(gem
);
697 list_add_tail(&pbbo
->entry
, &op
.both_list
);
699 /* If presumed return address doesn't match, we need to map the
700 * push buffer and fix it..
705 if (chan
->dma
.free
< 4 + NOUVEAU_DMA_SKIPS
) {
706 ret
= nouveau_dma_wait(chan
, 4 + NOUVEAU_DMA_SKIPS
);
708 NV_ERROR(dev
, "jmp_space: %d\n", ret
);
713 retaddy
= chan
->pushbuf_base
+ ((chan
->dma
.cur
+ 2) << 2);
714 retaddy
|= 0x20000000;
715 if (retaddy
!= req
->suffix0
) {
716 req
->suffix0
= retaddy
;
721 /* Apply any relocations that are required */
725 ret
= ttm_bo_kmap(&pbbo
->bo
, 0, pbbo
->bo
.mem
.num_pages
,
728 NV_ERROR(dev
, "kmap pb: %d\n", ret
);
732 pbvirt
= ttm_kmap_obj_virtual(&pbbo
->kmap
, &is_iomem
);
733 ret
= nouveau_gem_pushbuf_reloc_apply(chan
, req
->nr_buffers
, bo
,
741 nouveau_bo_wr32(pbbo
,
742 req
->offset
/ 4 + req
->nr_dwords
- 2,
746 ttm_bo_kunmap(&pbbo
->kmap
);
748 NV_ERROR(dev
, "reloc apply: %d\n", ret
);
754 ret
= RING_SPACE(chan
, 2);
756 NV_ERROR(dev
, "cal_space: %d\n", ret
);
759 OUT_RING(chan
, ((pbbo
->bo
.mem
.mm_node
->start
<< PAGE_SHIFT
) +
763 ret
= RING_SPACE(chan
, 2 + NOUVEAU_DMA_SKIPS
);
765 NV_ERROR(dev
, "jmp_space: %d\n", ret
);
768 OUT_RING(chan
, ((pbbo
->bo
.mem
.mm_node
->start
<< PAGE_SHIFT
) +
769 req
->offset
) | 0x20000000);
772 /* Space the jumps apart with NOPs. */
773 for (i
= 0; i
< NOUVEAU_DMA_SKIPS
; i
++)
777 ret
= nouveau_fence_emit(op
.fence
);
779 NV_ERROR(dev
, "error fencing pushbuf: %d\n", ret
);
785 validate_fini(&op
, ret
== 0);
786 mutex_unlock(&dev
->struct_mutex
);
791 req
->suffix0
= 0x00020000;
792 req
->suffix1
= 0x00000000;
794 req
->suffix0
= 0x20000000 |
795 (chan
->pushbuf_base
+ ((chan
->dma
.cur
+ 2) << 2));
796 req
->suffix1
= 0x00000000;
803 nouveau_gem_ioctl_pushbuf_call2(struct drm_device
*dev
, void *data
,
804 struct drm_file
*file_priv
)
806 struct drm_nouveau_private
*dev_priv
= dev
->dev_private
;
807 struct drm_nouveau_gem_pushbuf_call
*req
= data
;
809 req
->vram_available
= dev_priv
->fb_aper_free
;
810 req
->gart_available
= dev_priv
->gart_info
.aper_free
;
812 return nouveau_gem_ioctl_pushbuf_call(dev
, data
, file_priv
);
815 static inline uint32_t
816 domain_to_ttm(struct nouveau_bo
*nvbo
, uint32_t domain
)
820 if (domain
& NOUVEAU_GEM_DOMAIN_VRAM
)
821 flags
|= TTM_PL_FLAG_VRAM
;
822 if (domain
& NOUVEAU_GEM_DOMAIN_GART
)
823 flags
|= TTM_PL_FLAG_TT
;
829 nouveau_gem_ioctl_pin(struct drm_device
*dev
, void *data
,
830 struct drm_file
*file_priv
)
832 struct drm_nouveau_gem_pin
*req
= data
;
833 struct drm_gem_object
*gem
;
834 struct nouveau_bo
*nvbo
;
837 NOUVEAU_CHECK_INITIALISED_WITH_RETURN
;
839 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
840 NV_ERROR(dev
, "pin only allowed without kernel modesetting\n");
844 if (!DRM_SUSER(DRM_CURPROC
))
847 gem
= drm_gem_object_lookup(dev
, file_priv
, req
->handle
);
850 nvbo
= nouveau_gem_object(gem
);
852 ret
= nouveau_bo_pin(nvbo
, domain_to_ttm(nvbo
, req
->domain
));
856 req
->offset
= nvbo
->bo
.offset
;
857 if (nvbo
->bo
.mem
.mem_type
== TTM_PL_TT
)
858 req
->domain
= NOUVEAU_GEM_DOMAIN_GART
;
860 req
->domain
= NOUVEAU_GEM_DOMAIN_VRAM
;
863 mutex_lock(&dev
->struct_mutex
);
864 drm_gem_object_unreference(gem
);
865 mutex_unlock(&dev
->struct_mutex
);
871 nouveau_gem_ioctl_unpin(struct drm_device
*dev
, void *data
,
872 struct drm_file
*file_priv
)
874 struct drm_nouveau_gem_pin
*req
= data
;
875 struct drm_gem_object
*gem
;
878 NOUVEAU_CHECK_INITIALISED_WITH_RETURN
;
880 if (drm_core_check_feature(dev
, DRIVER_MODESET
))
883 gem
= drm_gem_object_lookup(dev
, file_priv
, req
->handle
);
887 ret
= nouveau_bo_unpin(nouveau_gem_object(gem
));
889 mutex_lock(&dev
->struct_mutex
);
890 drm_gem_object_unreference(gem
);
891 mutex_unlock(&dev
->struct_mutex
);
897 nouveau_gem_ioctl_cpu_prep(struct drm_device
*dev
, void *data
,
898 struct drm_file
*file_priv
)
900 struct drm_nouveau_gem_cpu_prep
*req
= data
;
901 struct drm_gem_object
*gem
;
902 struct nouveau_bo
*nvbo
;
903 bool no_wait
= !!(req
->flags
& NOUVEAU_GEM_CPU_PREP_NOWAIT
);
906 NOUVEAU_CHECK_INITIALISED_WITH_RETURN
;
908 gem
= drm_gem_object_lookup(dev
, file_priv
, req
->handle
);
911 nvbo
= nouveau_gem_object(gem
);
913 if (nvbo
->cpu_filp
) {
914 if (nvbo
->cpu_filp
== file_priv
)
917 ret
= ttm_bo_wait_cpu(&nvbo
->bo
, no_wait
);
918 if (ret
== -ERESTART
)
924 if (req
->flags
& NOUVEAU_GEM_CPU_PREP_NOBLOCK
) {
925 ret
= ttm_bo_wait(&nvbo
->bo
, false, false, no_wait
);
927 ret
= ttm_bo_synccpu_write_grab(&nvbo
->bo
, no_wait
);
928 if (ret
== -ERESTART
)
932 nvbo
->cpu_filp
= file_priv
;
936 mutex_lock(&dev
->struct_mutex
);
937 drm_gem_object_unreference(gem
);
938 mutex_unlock(&dev
->struct_mutex
);
943 nouveau_gem_ioctl_cpu_fini(struct drm_device
*dev
, void *data
,
944 struct drm_file
*file_priv
)
946 struct drm_nouveau_gem_cpu_prep
*req
= data
;
947 struct drm_gem_object
*gem
;
948 struct nouveau_bo
*nvbo
;
951 NOUVEAU_CHECK_INITIALISED_WITH_RETURN
;
953 gem
= drm_gem_object_lookup(dev
, file_priv
, req
->handle
);
956 nvbo
= nouveau_gem_object(gem
);
958 if (nvbo
->cpu_filp
!= file_priv
)
960 nvbo
->cpu_filp
= NULL
;
962 ttm_bo_synccpu_write_release(&nvbo
->bo
);
966 mutex_lock(&dev
->struct_mutex
);
967 drm_gem_object_unreference(gem
);
968 mutex_unlock(&dev
->struct_mutex
);
973 nouveau_gem_ioctl_info(struct drm_device
*dev
, void *data
,
974 struct drm_file
*file_priv
)
976 struct drm_nouveau_gem_info
*req
= data
;
977 struct drm_gem_object
*gem
;
980 NOUVEAU_CHECK_INITIALISED_WITH_RETURN
;
982 gem
= drm_gem_object_lookup(dev
, file_priv
, req
->handle
);
986 ret
= nouveau_gem_info(gem
, req
);
987 mutex_lock(&dev
->struct_mutex
);
988 drm_gem_object_unreference(gem
);
989 mutex_unlock(&dev
->struct_mutex
);