2 * Copyright 2007 Dave Airlied
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
25 * Authors: Dave Airlied <airlied@linux.ie>
26 * Ben Skeggs <darktama@iinet.net.au>
27 * Jeremy Kolb <jkolb@brandeis.edu>
32 #include "nouveau_drm.h"
33 #include "nouveau_drv.h"
34 #include "nouveau_dma.h"
35 #include "nouveau_mm.h"
36 #include "nouveau_vm.h"
38 #include <linux/log2.h>
39 #include <linux/slab.h>
42 nouveau_bo_del_ttm(struct ttm_buffer_object
*bo
)
44 struct drm_nouveau_private
*dev_priv
= nouveau_bdev(bo
->bdev
);
45 struct drm_device
*dev
= dev_priv
->dev
;
46 struct nouveau_bo
*nvbo
= nouveau_bo(bo
);
48 if (unlikely(nvbo
->gem
))
49 DRM_ERROR("bo %p still attached to GEM object\n", bo
);
51 nv10_mem_put_tile_region(dev
, nvbo
->tile
, NULL
);
53 nouveau_vm_unmap(&nvbo
->vma
);
54 nouveau_vm_put(&nvbo
->vma
);
60 nouveau_bo_fixup_align(struct nouveau_bo
*nvbo
, u32 flags
,
61 int *align
, int *size
, int *page_shift
)
63 struct drm_nouveau_private
*dev_priv
= nouveau_bdev(nvbo
->bo
.bdev
);
65 if (dev_priv
->card_type
< NV_50
) {
66 if (nvbo
->tile_mode
) {
67 if (dev_priv
->chipset
>= 0x40) {
69 *size
= roundup(*size
, 64 * nvbo
->tile_mode
);
71 } else if (dev_priv
->chipset
>= 0x30) {
73 *size
= roundup(*size
, 64 * nvbo
->tile_mode
);
75 } else if (dev_priv
->chipset
>= 0x20) {
77 *size
= roundup(*size
, 64 * nvbo
->tile_mode
);
79 } else if (dev_priv
->chipset
>= 0x10) {
81 *size
= roundup(*size
, 32 * nvbo
->tile_mode
);
85 if (likely(dev_priv
->chan_vm
)) {
86 if (!(flags
& TTM_PL_FLAG_TT
) && *size
> 256 * 1024)
87 *page_shift
= dev_priv
->chan_vm
->lpg_shift
;
89 *page_shift
= dev_priv
->chan_vm
->spg_shift
;
94 *size
= roundup(*size
, (1 << *page_shift
));
95 *align
= max((1 << *page_shift
), *align
);
98 *size
= roundup(*size
, PAGE_SIZE
);
102 nouveau_bo_new(struct drm_device
*dev
, struct nouveau_channel
*chan
,
103 int size
, int align
, uint32_t flags
, uint32_t tile_mode
,
104 uint32_t tile_flags
, struct nouveau_bo
**pnvbo
)
106 struct drm_nouveau_private
*dev_priv
= dev
->dev_private
;
107 struct nouveau_bo
*nvbo
;
108 int ret
= 0, page_shift
= 0;
110 nvbo
= kzalloc(sizeof(struct nouveau_bo
), GFP_KERNEL
);
113 INIT_LIST_HEAD(&nvbo
->head
);
114 INIT_LIST_HEAD(&nvbo
->entry
);
115 nvbo
->tile_mode
= tile_mode
;
116 nvbo
->tile_flags
= tile_flags
;
117 nvbo
->bo
.bdev
= &dev_priv
->ttm
.bdev
;
119 nouveau_bo_fixup_align(nvbo
, flags
, &align
, &size
, &page_shift
);
120 align
>>= PAGE_SHIFT
;
122 if (dev_priv
->chan_vm
) {
123 ret
= nouveau_vm_get(dev_priv
->chan_vm
, size
, page_shift
,
124 NV_MEM_ACCESS_RW
, &nvbo
->vma
);
131 nvbo
->bo
.mem
.num_pages
= size
>> PAGE_SHIFT
;
132 nouveau_bo_placement_set(nvbo
, flags
, 0);
134 nvbo
->channel
= chan
;
135 ret
= ttm_bo_init(&dev_priv
->ttm
.bdev
, &nvbo
->bo
, size
,
136 ttm_bo_type_device
, &nvbo
->placement
, align
, 0,
137 false, NULL
, size
, nouveau_bo_del_ttm
);
139 /* ttm will call nouveau_bo_del_ttm if it fails.. */
142 nvbo
->channel
= NULL
;
145 nvbo
->bo
.offset
= nvbo
->vma
.offset
;
151 set_placement_list(uint32_t *pl
, unsigned *n
, uint32_t type
, uint32_t flags
)
155 if (type
& TTM_PL_FLAG_VRAM
)
156 pl
[(*n
)++] = TTM_PL_FLAG_VRAM
| flags
;
157 if (type
& TTM_PL_FLAG_TT
)
158 pl
[(*n
)++] = TTM_PL_FLAG_TT
| flags
;
159 if (type
& TTM_PL_FLAG_SYSTEM
)
160 pl
[(*n
)++] = TTM_PL_FLAG_SYSTEM
| flags
;
164 set_placement_range(struct nouveau_bo
*nvbo
, uint32_t type
)
166 struct drm_nouveau_private
*dev_priv
= nouveau_bdev(nvbo
->bo
.bdev
);
167 int vram_pages
= dev_priv
->vram_size
>> PAGE_SHIFT
;
169 if (dev_priv
->card_type
== NV_10
&&
170 nvbo
->tile_mode
&& (type
& TTM_PL_FLAG_VRAM
) &&
171 nvbo
->bo
.mem
.num_pages
< vram_pages
/ 2) {
173 * Make sure that the color and depth buffers are handled
174 * by independent memory controller units. Up to a 9x
175 * speed up when alpha-blending and depth-test are enabled
178 if (nvbo
->tile_flags
& NOUVEAU_GEM_TILE_ZETA
) {
179 nvbo
->placement
.fpfn
= vram_pages
/ 2;
180 nvbo
->placement
.lpfn
= ~0;
182 nvbo
->placement
.fpfn
= 0;
183 nvbo
->placement
.lpfn
= vram_pages
/ 2;
189 nouveau_bo_placement_set(struct nouveau_bo
*nvbo
, uint32_t type
, uint32_t busy
)
191 struct ttm_placement
*pl
= &nvbo
->placement
;
192 uint32_t flags
= TTM_PL_MASK_CACHING
|
193 (nvbo
->pin_refcnt
? TTM_PL_FLAG_NO_EVICT
: 0);
195 pl
->placement
= nvbo
->placements
;
196 set_placement_list(nvbo
->placements
, &pl
->num_placement
,
199 pl
->busy_placement
= nvbo
->busy_placements
;
200 set_placement_list(nvbo
->busy_placements
, &pl
->num_busy_placement
,
203 set_placement_range(nvbo
, type
);
207 nouveau_bo_pin(struct nouveau_bo
*nvbo
, uint32_t memtype
)
209 struct drm_nouveau_private
*dev_priv
= nouveau_bdev(nvbo
->bo
.bdev
);
210 struct ttm_buffer_object
*bo
= &nvbo
->bo
;
213 if (nvbo
->pin_refcnt
&& !(memtype
& (1 << bo
->mem
.mem_type
))) {
214 NV_ERROR(nouveau_bdev(bo
->bdev
)->dev
,
215 "bo %p pinned elsewhere: 0x%08x vs 0x%08x\n", bo
,
216 1 << bo
->mem
.mem_type
, memtype
);
220 if (nvbo
->pin_refcnt
++)
223 ret
= ttm_bo_reserve(bo
, false, false, false, 0);
227 nouveau_bo_placement_set(nvbo
, memtype
, 0);
229 ret
= nouveau_bo_validate(nvbo
, false, false, false);
231 switch (bo
->mem
.mem_type
) {
233 dev_priv
->fb_aper_free
-= bo
->mem
.size
;
236 dev_priv
->gart_info
.aper_free
-= bo
->mem
.size
;
242 ttm_bo_unreserve(bo
);
250 nouveau_bo_unpin(struct nouveau_bo
*nvbo
)
252 struct drm_nouveau_private
*dev_priv
= nouveau_bdev(nvbo
->bo
.bdev
);
253 struct ttm_buffer_object
*bo
= &nvbo
->bo
;
256 if (--nvbo
->pin_refcnt
)
259 ret
= ttm_bo_reserve(bo
, false, false, false, 0);
263 nouveau_bo_placement_set(nvbo
, bo
->mem
.placement
, 0);
265 ret
= nouveau_bo_validate(nvbo
, false, false, false);
267 switch (bo
->mem
.mem_type
) {
269 dev_priv
->fb_aper_free
+= bo
->mem
.size
;
272 dev_priv
->gart_info
.aper_free
+= bo
->mem
.size
;
279 ttm_bo_unreserve(bo
);
284 nouveau_bo_map(struct nouveau_bo
*nvbo
)
288 ret
= ttm_bo_reserve(&nvbo
->bo
, false, false, false, 0);
292 ret
= ttm_bo_kmap(&nvbo
->bo
, 0, nvbo
->bo
.mem
.num_pages
, &nvbo
->kmap
);
293 ttm_bo_unreserve(&nvbo
->bo
);
298 nouveau_bo_unmap(struct nouveau_bo
*nvbo
)
301 ttm_bo_kunmap(&nvbo
->kmap
);
305 nouveau_bo_validate(struct nouveau_bo
*nvbo
, bool interruptible
,
306 bool no_wait_reserve
, bool no_wait_gpu
)
310 ret
= ttm_bo_validate(&nvbo
->bo
, &nvbo
->placement
, interruptible
,
311 no_wait_reserve
, no_wait_gpu
);
316 nvbo
->bo
.offset
= nvbo
->vma
.offset
;
321 nouveau_bo_rd16(struct nouveau_bo
*nvbo
, unsigned index
)
324 u16
*mem
= ttm_kmap_obj_virtual(&nvbo
->kmap
, &is_iomem
);
327 return ioread16_native((void __force __iomem
*)mem
);
333 nouveau_bo_wr16(struct nouveau_bo
*nvbo
, unsigned index
, u16 val
)
336 u16
*mem
= ttm_kmap_obj_virtual(&nvbo
->kmap
, &is_iomem
);
339 iowrite16_native(val
, (void __force __iomem
*)mem
);
345 nouveau_bo_rd32(struct nouveau_bo
*nvbo
, unsigned index
)
348 u32
*mem
= ttm_kmap_obj_virtual(&nvbo
->kmap
, &is_iomem
);
351 return ioread32_native((void __force __iomem
*)mem
);
357 nouveau_bo_wr32(struct nouveau_bo
*nvbo
, unsigned index
, u32 val
)
360 u32
*mem
= ttm_kmap_obj_virtual(&nvbo
->kmap
, &is_iomem
);
363 iowrite32_native(val
, (void __force __iomem
*)mem
);
368 static struct ttm_backend
*
369 nouveau_bo_create_ttm_backend_entry(struct ttm_bo_device
*bdev
)
371 struct drm_nouveau_private
*dev_priv
= nouveau_bdev(bdev
);
372 struct drm_device
*dev
= dev_priv
->dev
;
374 switch (dev_priv
->gart_info
.type
) {
376 case NOUVEAU_GART_AGP
:
377 return ttm_agp_backend_init(bdev
, dev
->agp
->bridge
);
379 case NOUVEAU_GART_PDMA
:
380 case NOUVEAU_GART_HW
:
381 return nouveau_sgdma_init_ttm(dev
);
383 NV_ERROR(dev
, "Unknown GART type %d\n",
384 dev_priv
->gart_info
.type
);
392 nouveau_bo_invalidate_caches(struct ttm_bo_device
*bdev
, uint32_t flags
)
394 /* We'll do this from user space. */
399 nouveau_bo_init_mem_type(struct ttm_bo_device
*bdev
, uint32_t type
,
400 struct ttm_mem_type_manager
*man
)
402 struct drm_nouveau_private
*dev_priv
= nouveau_bdev(bdev
);
403 struct drm_device
*dev
= dev_priv
->dev
;
407 man
->flags
= TTM_MEMTYPE_FLAG_MAPPABLE
;
408 man
->available_caching
= TTM_PL_MASK_CACHING
;
409 man
->default_caching
= TTM_PL_FLAG_CACHED
;
412 if (dev_priv
->card_type
>= NV_50
) {
413 man
->func
= &nouveau_vram_manager
;
414 man
->io_reserve_fastpath
= false;
415 man
->use_io_reserve_lru
= true;
417 man
->func
= &ttm_bo_manager_func
;
419 man
->flags
= TTM_MEMTYPE_FLAG_FIXED
|
420 TTM_MEMTYPE_FLAG_MAPPABLE
;
421 man
->available_caching
= TTM_PL_FLAG_UNCACHED
|
423 man
->default_caching
= TTM_PL_FLAG_WC
;
426 if (dev_priv
->card_type
>= NV_50
)
427 man
->func
= &nouveau_gart_manager
;
429 man
->func
= &ttm_bo_manager_func
;
430 switch (dev_priv
->gart_info
.type
) {
431 case NOUVEAU_GART_AGP
:
432 man
->flags
= TTM_MEMTYPE_FLAG_MAPPABLE
;
433 man
->available_caching
= TTM_PL_FLAG_UNCACHED
|
435 man
->default_caching
= TTM_PL_FLAG_WC
;
437 case NOUVEAU_GART_PDMA
:
438 case NOUVEAU_GART_HW
:
439 man
->flags
= TTM_MEMTYPE_FLAG_MAPPABLE
|
440 TTM_MEMTYPE_FLAG_CMA
;
441 man
->available_caching
= TTM_PL_MASK_CACHING
;
442 man
->default_caching
= TTM_PL_FLAG_CACHED
;
443 man
->gpu_offset
= dev_priv
->gart_info
.aper_base
;
446 NV_ERROR(dev
, "Unknown GART type: %d\n",
447 dev_priv
->gart_info
.type
);
452 NV_ERROR(dev
, "Unsupported memory type %u\n", (unsigned)type
);
459 nouveau_bo_evict_flags(struct ttm_buffer_object
*bo
, struct ttm_placement
*pl
)
461 struct nouveau_bo
*nvbo
= nouveau_bo(bo
);
463 switch (bo
->mem
.mem_type
) {
465 nouveau_bo_placement_set(nvbo
, TTM_PL_FLAG_TT
,
469 nouveau_bo_placement_set(nvbo
, TTM_PL_FLAG_SYSTEM
, 0);
473 *pl
= nvbo
->placement
;
477 /* GPU-assisted copy using NV_MEMORY_TO_MEMORY_FORMAT, can access
478 * TTM_PL_{VRAM,TT} directly.
482 nouveau_bo_move_accel_cleanup(struct nouveau_channel
*chan
,
483 struct nouveau_bo
*nvbo
, bool evict
,
484 bool no_wait_reserve
, bool no_wait_gpu
,
485 struct ttm_mem_reg
*new_mem
)
487 struct nouveau_fence
*fence
= NULL
;
490 ret
= nouveau_fence_new(chan
, &fence
, true);
494 ret
= ttm_bo_move_accel_cleanup(&nvbo
->bo
, fence
, NULL
, evict
,
495 no_wait_reserve
, no_wait_gpu
, new_mem
);
496 nouveau_fence_unref(&fence
);
501 nvc0_bo_move_m2mf(struct nouveau_channel
*chan
, struct ttm_buffer_object
*bo
,
502 struct ttm_mem_reg
*old_mem
, struct ttm_mem_reg
*new_mem
)
504 struct nouveau_mem
*old_node
= old_mem
->mm_node
;
505 struct nouveau_mem
*new_node
= new_mem
->mm_node
;
506 struct nouveau_bo
*nvbo
= nouveau_bo(bo
);
507 u32 page_count
= new_mem
->num_pages
;
508 u64 src_offset
, dst_offset
;
511 src_offset
= old_node
->tmp_vma
.offset
;
512 if (new_node
->tmp_vma
.node
)
513 dst_offset
= new_node
->tmp_vma
.offset
;
515 dst_offset
= nvbo
->vma
.offset
;
517 page_count
= new_mem
->num_pages
;
519 int line_count
= (page_count
> 2047) ? 2047 : page_count
;
521 ret
= RING_SPACE(chan
, 12);
525 BEGIN_NVC0(chan
, 2, NvSubM2MF
, 0x0238, 2);
526 OUT_RING (chan
, upper_32_bits(dst_offset
));
527 OUT_RING (chan
, lower_32_bits(dst_offset
));
528 BEGIN_NVC0(chan
, 2, NvSubM2MF
, 0x030c, 6);
529 OUT_RING (chan
, upper_32_bits(src_offset
));
530 OUT_RING (chan
, lower_32_bits(src_offset
));
531 OUT_RING (chan
, PAGE_SIZE
); /* src_pitch */
532 OUT_RING (chan
, PAGE_SIZE
); /* dst_pitch */
533 OUT_RING (chan
, PAGE_SIZE
); /* line_length */
534 OUT_RING (chan
, line_count
);
535 BEGIN_NVC0(chan
, 2, NvSubM2MF
, 0x0300, 1);
536 OUT_RING (chan
, 0x00100110);
538 page_count
-= line_count
;
539 src_offset
+= (PAGE_SIZE
* line_count
);
540 dst_offset
+= (PAGE_SIZE
* line_count
);
547 nv50_bo_move_m2mf(struct nouveau_channel
*chan
, struct ttm_buffer_object
*bo
,
548 struct ttm_mem_reg
*old_mem
, struct ttm_mem_reg
*new_mem
)
550 struct nouveau_mem
*old_node
= old_mem
->mm_node
;
551 struct nouveau_mem
*new_node
= new_mem
->mm_node
;
552 struct nouveau_bo
*nvbo
= nouveau_bo(bo
);
553 u64 length
= (new_mem
->num_pages
<< PAGE_SHIFT
);
554 u64 src_offset
, dst_offset
;
557 src_offset
= old_node
->tmp_vma
.offset
;
558 if (new_node
->tmp_vma
.node
)
559 dst_offset
= new_node
->tmp_vma
.offset
;
561 dst_offset
= nvbo
->vma
.offset
;
564 u32 amount
, stride
, height
;
566 amount
= min(length
, (u64
)(4 * 1024 * 1024));
568 height
= amount
/ stride
;
570 if (new_mem
->mem_type
== TTM_PL_VRAM
&&
571 nouveau_bo_tile_layout(nvbo
)) {
572 ret
= RING_SPACE(chan
, 8);
576 BEGIN_RING(chan
, NvSubM2MF
, 0x0200, 7);
579 OUT_RING (chan
, stride
);
580 OUT_RING (chan
, height
);
585 ret
= RING_SPACE(chan
, 2);
589 BEGIN_RING(chan
, NvSubM2MF
, 0x0200, 1);
592 if (old_mem
->mem_type
== TTM_PL_VRAM
&&
593 nouveau_bo_tile_layout(nvbo
)) {
594 ret
= RING_SPACE(chan
, 8);
598 BEGIN_RING(chan
, NvSubM2MF
, 0x021c, 7);
601 OUT_RING (chan
, stride
);
602 OUT_RING (chan
, height
);
607 ret
= RING_SPACE(chan
, 2);
611 BEGIN_RING(chan
, NvSubM2MF
, 0x021c, 1);
615 ret
= RING_SPACE(chan
, 14);
619 BEGIN_RING(chan
, NvSubM2MF
, 0x0238, 2);
620 OUT_RING (chan
, upper_32_bits(src_offset
));
621 OUT_RING (chan
, upper_32_bits(dst_offset
));
622 BEGIN_RING(chan
, NvSubM2MF
, 0x030c, 8);
623 OUT_RING (chan
, lower_32_bits(src_offset
));
624 OUT_RING (chan
, lower_32_bits(dst_offset
));
625 OUT_RING (chan
, stride
);
626 OUT_RING (chan
, stride
);
627 OUT_RING (chan
, stride
);
628 OUT_RING (chan
, height
);
629 OUT_RING (chan
, 0x00000101);
630 OUT_RING (chan
, 0x00000000);
631 BEGIN_RING(chan
, NvSubM2MF
, NV_MEMORY_TO_MEMORY_FORMAT_NOP
, 1);
635 src_offset
+= amount
;
636 dst_offset
+= amount
;
642 static inline uint32_t
643 nouveau_bo_mem_ctxdma(struct ttm_buffer_object
*bo
,
644 struct nouveau_channel
*chan
, struct ttm_mem_reg
*mem
)
646 if (mem
->mem_type
== TTM_PL_TT
)
647 return chan
->gart_handle
;
648 return chan
->vram_handle
;
652 nv04_bo_move_m2mf(struct nouveau_channel
*chan
, struct ttm_buffer_object
*bo
,
653 struct ttm_mem_reg
*old_mem
, struct ttm_mem_reg
*new_mem
)
655 u32 src_offset
= old_mem
->start
<< PAGE_SHIFT
;
656 u32 dst_offset
= new_mem
->start
<< PAGE_SHIFT
;
657 u32 page_count
= new_mem
->num_pages
;
660 ret
= RING_SPACE(chan
, 3);
664 BEGIN_RING(chan
, NvSubM2MF
, NV_MEMORY_TO_MEMORY_FORMAT_DMA_SOURCE
, 2);
665 OUT_RING (chan
, nouveau_bo_mem_ctxdma(bo
, chan
, old_mem
));
666 OUT_RING (chan
, nouveau_bo_mem_ctxdma(bo
, chan
, new_mem
));
668 page_count
= new_mem
->num_pages
;
670 int line_count
= (page_count
> 2047) ? 2047 : page_count
;
672 ret
= RING_SPACE(chan
, 11);
676 BEGIN_RING(chan
, NvSubM2MF
,
677 NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN
, 8);
678 OUT_RING (chan
, src_offset
);
679 OUT_RING (chan
, dst_offset
);
680 OUT_RING (chan
, PAGE_SIZE
); /* src_pitch */
681 OUT_RING (chan
, PAGE_SIZE
); /* dst_pitch */
682 OUT_RING (chan
, PAGE_SIZE
); /* line_length */
683 OUT_RING (chan
, line_count
);
684 OUT_RING (chan
, 0x00000101);
685 OUT_RING (chan
, 0x00000000);
686 BEGIN_RING(chan
, NvSubM2MF
, NV_MEMORY_TO_MEMORY_FORMAT_NOP
, 1);
689 page_count
-= line_count
;
690 src_offset
+= (PAGE_SIZE
* line_count
);
691 dst_offset
+= (PAGE_SIZE
* line_count
);
698 nouveau_bo_move_m2mf(struct ttm_buffer_object
*bo
, int evict
, bool intr
,
699 bool no_wait_reserve
, bool no_wait_gpu
,
700 struct ttm_mem_reg
*new_mem
)
702 struct drm_nouveau_private
*dev_priv
= nouveau_bdev(bo
->bdev
);
703 struct nouveau_bo
*nvbo
= nouveau_bo(bo
);
704 struct ttm_mem_reg
*old_mem
= &bo
->mem
;
705 struct nouveau_channel
*chan
;
708 chan
= nvbo
->channel
;
710 chan
= dev_priv
->channel
;
711 mutex_lock_nested(&chan
->mutex
, NOUVEAU_KCHANNEL_MUTEX
);
714 /* create temporary vma for old memory, this will get cleaned
715 * up after ttm destroys the ttm_mem_reg
717 if (dev_priv
->card_type
>= NV_50
) {
718 struct nouveau_mem
*node
= old_mem
->mm_node
;
719 if (!node
->tmp_vma
.node
) {
720 u32 page_shift
= nvbo
->vma
.node
->type
;
721 if (old_mem
->mem_type
== TTM_PL_TT
)
722 page_shift
= nvbo
->vma
.vm
->spg_shift
;
724 ret
= nouveau_vm_get(chan
->vm
,
725 old_mem
->num_pages
<< PAGE_SHIFT
,
726 page_shift
, NV_MEM_ACCESS_RO
,
732 if (old_mem
->mem_type
== TTM_PL_VRAM
)
733 nouveau_vm_map(&node
->tmp_vma
, node
);
735 nouveau_vm_map_sg(&node
->tmp_vma
, 0,
736 old_mem
->num_pages
<< PAGE_SHIFT
,
741 if (dev_priv
->card_type
< NV_50
)
742 ret
= nv04_bo_move_m2mf(chan
, bo
, &bo
->mem
, new_mem
);
744 if (dev_priv
->card_type
< NV_C0
)
745 ret
= nv50_bo_move_m2mf(chan
, bo
, &bo
->mem
, new_mem
);
747 ret
= nvc0_bo_move_m2mf(chan
, bo
, &bo
->mem
, new_mem
);
749 ret
= nouveau_bo_move_accel_cleanup(chan
, nvbo
, evict
,
751 no_wait_gpu
, new_mem
);
755 if (chan
== dev_priv
->channel
)
756 mutex_unlock(&chan
->mutex
);
761 nouveau_bo_move_flipd(struct ttm_buffer_object
*bo
, bool evict
, bool intr
,
762 bool no_wait_reserve
, bool no_wait_gpu
,
763 struct ttm_mem_reg
*new_mem
)
765 struct drm_nouveau_private
*dev_priv
= nouveau_bdev(bo
->bdev
);
766 u32 placement_memtype
= TTM_PL_FLAG_TT
| TTM_PL_MASK_CACHING
;
767 struct ttm_placement placement
;
768 struct ttm_mem_reg tmp_mem
;
771 placement
.fpfn
= placement
.lpfn
= 0;
772 placement
.num_placement
= placement
.num_busy_placement
= 1;
773 placement
.placement
= placement
.busy_placement
= &placement_memtype
;
776 tmp_mem
.mm_node
= NULL
;
777 ret
= ttm_bo_mem_space(bo
, &placement
, &tmp_mem
, intr
, no_wait_reserve
, no_wait_gpu
);
781 ret
= ttm_tt_bind(bo
->ttm
, &tmp_mem
);
785 if (dev_priv
->card_type
>= NV_50
) {
786 struct nouveau_bo
*nvbo
= nouveau_bo(bo
);
787 struct nouveau_mem
*node
= tmp_mem
.mm_node
;
788 struct nouveau_vma
*vma
= &nvbo
->vma
;
789 if (vma
->node
->type
!= vma
->vm
->spg_shift
)
790 vma
= &node
->tmp_vma
;
791 nouveau_vm_map_sg(vma
, 0, tmp_mem
.num_pages
<< PAGE_SHIFT
,
795 ret
= nouveau_bo_move_m2mf(bo
, true, intr
, no_wait_reserve
, no_wait_gpu
, &tmp_mem
);
797 if (dev_priv
->card_type
>= NV_50
) {
798 struct nouveau_bo
*nvbo
= nouveau_bo(bo
);
799 nouveau_vm_unmap(&nvbo
->vma
);
805 ret
= ttm_bo_move_ttm(bo
, true, no_wait_reserve
, no_wait_gpu
, new_mem
);
807 ttm_bo_mem_put(bo
, &tmp_mem
);
812 nouveau_bo_move_flips(struct ttm_buffer_object
*bo
, bool evict
, bool intr
,
813 bool no_wait_reserve
, bool no_wait_gpu
,
814 struct ttm_mem_reg
*new_mem
)
816 u32 placement_memtype
= TTM_PL_FLAG_TT
| TTM_PL_MASK_CACHING
;
817 struct ttm_placement placement
;
818 struct ttm_mem_reg tmp_mem
;
821 placement
.fpfn
= placement
.lpfn
= 0;
822 placement
.num_placement
= placement
.num_busy_placement
= 1;
823 placement
.placement
= placement
.busy_placement
= &placement_memtype
;
826 tmp_mem
.mm_node
= NULL
;
827 ret
= ttm_bo_mem_space(bo
, &placement
, &tmp_mem
, intr
, no_wait_reserve
, no_wait_gpu
);
831 ret
= ttm_bo_move_ttm(bo
, true, no_wait_reserve
, no_wait_gpu
, &tmp_mem
);
835 ret
= nouveau_bo_move_m2mf(bo
, true, intr
, no_wait_reserve
, no_wait_gpu
, new_mem
);
840 ttm_bo_mem_put(bo
, &tmp_mem
);
845 nouveau_bo_move_ntfy(struct ttm_buffer_object
*bo
, struct ttm_mem_reg
*new_mem
)
847 struct drm_nouveau_private
*dev_priv
= nouveau_bdev(bo
->bdev
);
848 struct nouveau_mem
*node
= new_mem
->mm_node
;
849 struct nouveau_bo
*nvbo
= nouveau_bo(bo
);
850 struct nouveau_vma
*vma
= &nvbo
->vma
;
851 struct nouveau_vm
*vm
= vma
->vm
;
853 if (dev_priv
->card_type
< NV_50
)
856 switch (new_mem
->mem_type
) {
858 nouveau_vm_map(vma
, node
);
861 if (vma
->node
->type
!= vm
->spg_shift
) {
862 nouveau_vm_unmap(vma
);
863 vma
= &node
->tmp_vma
;
865 nouveau_vm_map_sg(vma
, 0, new_mem
->num_pages
<< PAGE_SHIFT
,
869 nouveau_vm_unmap(&nvbo
->vma
);
875 nouveau_bo_vm_bind(struct ttm_buffer_object
*bo
, struct ttm_mem_reg
*new_mem
,
876 struct nouveau_tile_reg
**new_tile
)
878 struct drm_nouveau_private
*dev_priv
= nouveau_bdev(bo
->bdev
);
879 struct drm_device
*dev
= dev_priv
->dev
;
880 struct nouveau_bo
*nvbo
= nouveau_bo(bo
);
881 u64 offset
= new_mem
->start
<< PAGE_SHIFT
;
884 if (new_mem
->mem_type
!= TTM_PL_VRAM
)
887 if (dev_priv
->card_type
>= NV_10
) {
888 *new_tile
= nv10_mem_set_tiling(dev
, offset
, new_mem
->size
,
897 nouveau_bo_vm_cleanup(struct ttm_buffer_object
*bo
,
898 struct nouveau_tile_reg
*new_tile
,
899 struct nouveau_tile_reg
**old_tile
)
901 struct drm_nouveau_private
*dev_priv
= nouveau_bdev(bo
->bdev
);
902 struct drm_device
*dev
= dev_priv
->dev
;
904 nv10_mem_put_tile_region(dev
, *old_tile
, bo
->sync_obj
);
905 *old_tile
= new_tile
;
909 nouveau_bo_move(struct ttm_buffer_object
*bo
, bool evict
, bool intr
,
910 bool no_wait_reserve
, bool no_wait_gpu
,
911 struct ttm_mem_reg
*new_mem
)
913 struct drm_nouveau_private
*dev_priv
= nouveau_bdev(bo
->bdev
);
914 struct nouveau_bo
*nvbo
= nouveau_bo(bo
);
915 struct ttm_mem_reg
*old_mem
= &bo
->mem
;
916 struct nouveau_tile_reg
*new_tile
= NULL
;
919 if (dev_priv
->card_type
< NV_50
) {
920 ret
= nouveau_bo_vm_bind(bo
, new_mem
, &new_tile
);
926 if (old_mem
->mem_type
== TTM_PL_SYSTEM
&& !bo
->ttm
) {
927 BUG_ON(bo
->mem
.mm_node
!= NULL
);
929 new_mem
->mm_node
= NULL
;
933 /* Software copy if the card isn't up and running yet. */
934 if (!dev_priv
->channel
) {
935 ret
= ttm_bo_move_memcpy(bo
, evict
, no_wait_reserve
, no_wait_gpu
, new_mem
);
939 /* Hardware assisted copy. */
940 if (new_mem
->mem_type
== TTM_PL_SYSTEM
)
941 ret
= nouveau_bo_move_flipd(bo
, evict
, intr
, no_wait_reserve
, no_wait_gpu
, new_mem
);
942 else if (old_mem
->mem_type
== TTM_PL_SYSTEM
)
943 ret
= nouveau_bo_move_flips(bo
, evict
, intr
, no_wait_reserve
, no_wait_gpu
, new_mem
);
945 ret
= nouveau_bo_move_m2mf(bo
, evict
, intr
, no_wait_reserve
, no_wait_gpu
, new_mem
);
950 /* Fallback to software copy. */
951 ret
= ttm_bo_move_memcpy(bo
, evict
, no_wait_reserve
, no_wait_gpu
, new_mem
);
954 if (dev_priv
->card_type
< NV_50
) {
956 nouveau_bo_vm_cleanup(bo
, NULL
, &new_tile
);
958 nouveau_bo_vm_cleanup(bo
, new_tile
, &nvbo
->tile
);
965 nouveau_bo_verify_access(struct ttm_buffer_object
*bo
, struct file
*filp
)
971 nouveau_ttm_io_mem_reserve(struct ttm_bo_device
*bdev
, struct ttm_mem_reg
*mem
)
973 struct ttm_mem_type_manager
*man
= &bdev
->man
[mem
->mem_type
];
974 struct drm_nouveau_private
*dev_priv
= nouveau_bdev(bdev
);
975 struct drm_device
*dev
= dev_priv
->dev
;
978 mem
->bus
.addr
= NULL
;
980 mem
->bus
.size
= mem
->num_pages
<< PAGE_SHIFT
;
982 mem
->bus
.is_iomem
= false;
983 if (!(man
->flags
& TTM_MEMTYPE_FLAG_MAPPABLE
))
985 switch (mem
->mem_type
) {
991 if (dev_priv
->gart_info
.type
== NOUVEAU_GART_AGP
) {
992 mem
->bus
.offset
= mem
->start
<< PAGE_SHIFT
;
993 mem
->bus
.base
= dev_priv
->gart_info
.aper_base
;
994 mem
->bus
.is_iomem
= true;
1000 struct nouveau_mem
*node
= mem
->mm_node
;
1003 if (!dev_priv
->bar1_vm
) {
1004 mem
->bus
.offset
= mem
->start
<< PAGE_SHIFT
;
1005 mem
->bus
.base
= pci_resource_start(dev
->pdev
, 1);
1006 mem
->bus
.is_iomem
= true;
1010 if (dev_priv
->card_type
== NV_C0
)
1011 page_shift
= node
->page_shift
;
1015 ret
= nouveau_vm_get(dev_priv
->bar1_vm
, mem
->bus
.size
,
1016 page_shift
, NV_MEM_ACCESS_RW
,
1021 nouveau_vm_map(&node
->bar_vma
, node
);
1023 nouveau_vm_put(&node
->bar_vma
);
1027 mem
->bus
.offset
= node
->bar_vma
.offset
;
1028 if (dev_priv
->card_type
== NV_50
) /*XXX*/
1029 mem
->bus
.offset
-= 0x0020000000ULL
;
1030 mem
->bus
.base
= pci_resource_start(dev
->pdev
, 1);
1031 mem
->bus
.is_iomem
= true;
1041 nouveau_ttm_io_mem_free(struct ttm_bo_device
*bdev
, struct ttm_mem_reg
*mem
)
1043 struct drm_nouveau_private
*dev_priv
= nouveau_bdev(bdev
);
1044 struct nouveau_mem
*node
= mem
->mm_node
;
1046 if (!dev_priv
->bar1_vm
|| mem
->mem_type
!= TTM_PL_VRAM
)
1049 if (!node
->bar_vma
.node
)
1052 nouveau_vm_unmap(&node
->bar_vma
);
1053 nouveau_vm_put(&node
->bar_vma
);
1057 nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object
*bo
)
1059 struct drm_nouveau_private
*dev_priv
= nouveau_bdev(bo
->bdev
);
1060 struct nouveau_bo
*nvbo
= nouveau_bo(bo
);
1062 /* as long as the bo isn't in vram, and isn't tiled, we've got
1063 * nothing to do here.
1065 if (bo
->mem
.mem_type
!= TTM_PL_VRAM
) {
1066 if (dev_priv
->card_type
< NV_50
||
1067 !nouveau_bo_tile_layout(nvbo
))
1071 /* make sure bo is in mappable vram */
1072 if (bo
->mem
.start
+ bo
->mem
.num_pages
< dev_priv
->fb_mappable_pages
)
1076 nvbo
->placement
.fpfn
= 0;
1077 nvbo
->placement
.lpfn
= dev_priv
->fb_mappable_pages
;
1078 nouveau_bo_placement_set(nvbo
, TTM_PL_VRAM
, 0);
1079 return nouveau_bo_validate(nvbo
, false, true, false);
1083 nouveau_bo_fence(struct nouveau_bo
*nvbo
, struct nouveau_fence
*fence
)
1085 struct nouveau_fence
*old_fence
;
1088 nouveau_fence_ref(fence
);
1090 spin_lock(&nvbo
->bo
.bdev
->fence_lock
);
1091 old_fence
= nvbo
->bo
.sync_obj
;
1092 nvbo
->bo
.sync_obj
= fence
;
1093 spin_unlock(&nvbo
->bo
.bdev
->fence_lock
);
1095 nouveau_fence_unref(&old_fence
);
1098 struct ttm_bo_driver nouveau_bo_driver
= {
1099 .create_ttm_backend_entry
= nouveau_bo_create_ttm_backend_entry
,
1100 .invalidate_caches
= nouveau_bo_invalidate_caches
,
1101 .init_mem_type
= nouveau_bo_init_mem_type
,
1102 .evict_flags
= nouveau_bo_evict_flags
,
1103 .move_notify
= nouveau_bo_move_ntfy
,
1104 .move
= nouveau_bo_move
,
1105 .verify_access
= nouveau_bo_verify_access
,
1106 .sync_obj_signaled
= __nouveau_fence_signalled
,
1107 .sync_obj_wait
= __nouveau_fence_wait
,
1108 .sync_obj_flush
= __nouveau_fence_flush
,
1109 .sync_obj_unref
= __nouveau_fence_unref
,
1110 .sync_obj_ref
= __nouveau_fence_ref
,
1111 .fault_reserve_notify
= &nouveau_ttm_fault_reserve_notify
,
1112 .io_mem_reserve
= &nouveau_ttm_io_mem_reserve
,
1113 .io_mem_free
= &nouveau_ttm_io_mem_free
,