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>
30 #include <core/engine.h>
32 #include <subdev/fb.h>
33 #include <subdev/vm.h>
34 #include <subdev/bar.h>
36 #include "nouveau_drm.h"
37 #include "nouveau_dma.h"
38 #include "nouveau_fence.h"
40 #include "nouveau_bo.h"
41 #include "nouveau_ttm.h"
42 #include "nouveau_gem.h"
45 * NV10-NV40 tiling helpers
49 nv10_bo_update_tile_region(struct drm_device *dev, struct nouveau_drm_tile *reg,
50 u32 addr, u32 size, u32 pitch, u32 flags)
52 struct nouveau_drm *drm = nouveau_drm(dev);
53 int i = reg - drm->tile.reg;
54 struct nouveau_fb *pfb = nouveau_fb(drm->device);
55 struct nouveau_fb_tile *tile = &pfb->tile.region[i];
56 struct nouveau_engine *engine;
58 nouveau_fence_unref(®->fence);
61 pfb->tile.fini(pfb, i, tile);
64 pfb->tile.init(pfb, i, addr, size, pitch, flags, tile);
66 pfb->tile.prog(pfb, i, tile);
68 if ((engine = nouveau_engine(pfb, NVDEV_ENGINE_GR)))
69 engine->tile_prog(engine, i);
70 if ((engine = nouveau_engine(pfb, NVDEV_ENGINE_MPEG)))
71 engine->tile_prog(engine, i);
74 static struct nouveau_drm_tile *
75 nv10_bo_get_tile_region(struct drm_device *dev, int i)
77 struct nouveau_drm *drm = nouveau_drm(dev);
78 struct nouveau_drm_tile *tile = &drm->tile.reg[i];
80 spin_lock(&drm->tile.lock);
83 (!tile->fence || nouveau_fence_done(tile->fence)))
88 spin_unlock(&drm->tile.lock);
93 nv10_bo_put_tile_region(struct drm_device *dev, struct nouveau_drm_tile *tile,
94 struct nouveau_fence *fence)
96 struct nouveau_drm *drm = nouveau_drm(dev);
99 spin_lock(&drm->tile.lock);
101 /* Mark it as pending. */
103 nouveau_fence_ref(fence);
107 spin_unlock(&drm->tile.lock);
111 static struct nouveau_drm_tile *
112 nv10_bo_set_tiling(struct drm_device *dev, u32 addr,
113 u32 size, u32 pitch, u32 flags)
115 struct nouveau_drm *drm = nouveau_drm(dev);
116 struct nouveau_fb *pfb = nouveau_fb(drm->device);
117 struct nouveau_drm_tile *tile, *found = NULL;
120 for (i = 0; i < pfb->tile.regions; i++) {
121 tile = nv10_bo_get_tile_region(dev, i);
123 if (pitch && !found) {
127 } else if (tile && pfb->tile.region[i].pitch) {
128 /* Kill an unused tile region. */
129 nv10_bo_update_tile_region(dev, tile, 0, 0, 0, 0);
132 nv10_bo_put_tile_region(dev, tile, NULL);
136 nv10_bo_update_tile_region(dev, found, addr, size,
142 nouveau_bo_del_ttm(struct ttm_buffer_object *bo)
144 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
145 struct drm_device *dev = drm->dev;
146 struct nouveau_bo *nvbo = nouveau_bo(bo);
148 if (unlikely(nvbo->gem))
149 DRM_ERROR("bo %p still attached to GEM object\n", bo);
150 nv10_bo_put_tile_region(dev, nvbo->tile, NULL);
155 nouveau_bo_fixup_align(struct nouveau_bo *nvbo, u32 flags,
156 int *align, int *size)
158 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
159 struct nouveau_device *device = nv_device(drm->device);
161 if (device->card_type < NV_50) {
162 if (nvbo->tile_mode) {
163 if (device->chipset >= 0x40) {
165 *size = roundup(*size, 64 * nvbo->tile_mode);
167 } else if (device->chipset >= 0x30) {
169 *size = roundup(*size, 64 * nvbo->tile_mode);
171 } else if (device->chipset >= 0x20) {
173 *size = roundup(*size, 64 * nvbo->tile_mode);
175 } else if (device->chipset >= 0x10) {
177 *size = roundup(*size, 32 * nvbo->tile_mode);
181 *size = roundup(*size, (1 << nvbo->page_shift));
182 *align = max((1 << nvbo->page_shift), *align);
185 *size = roundup(*size, PAGE_SIZE);
189 nouveau_bo_new(struct drm_device *dev, int size, int align,
190 uint32_t flags, uint32_t tile_mode, uint32_t tile_flags,
192 struct nouveau_bo **pnvbo)
194 struct nouveau_drm *drm = nouveau_drm(dev);
195 struct nouveau_bo *nvbo;
198 int type = ttm_bo_type_device;
201 type = ttm_bo_type_sg;
203 nvbo = kzalloc(sizeof(struct nouveau_bo), GFP_KERNEL);
206 INIT_LIST_HEAD(&nvbo->head);
207 INIT_LIST_HEAD(&nvbo->entry);
208 INIT_LIST_HEAD(&nvbo->vma_list);
209 nvbo->tile_mode = tile_mode;
210 nvbo->tile_flags = tile_flags;
211 nvbo->bo.bdev = &drm->ttm.bdev;
213 nvbo->page_shift = 12;
214 if (drm->client.base.vm) {
215 if (!(flags & TTM_PL_FLAG_TT) && size > 256 * 1024)
216 nvbo->page_shift = drm->client.base.vm->vmm->lpg_shift;
219 nouveau_bo_fixup_align(nvbo, flags, &align, &size);
220 nvbo->bo.mem.num_pages = size >> PAGE_SHIFT;
221 nouveau_bo_placement_set(nvbo, flags, 0);
223 acc_size = ttm_bo_dma_acc_size(&drm->ttm.bdev, size,
224 sizeof(struct nouveau_bo));
226 ret = ttm_bo_init(&drm->ttm.bdev, &nvbo->bo, size,
227 type, &nvbo->placement,
228 align >> PAGE_SHIFT, false, NULL, acc_size, sg,
231 /* ttm will call nouveau_bo_del_ttm if it fails.. */
240 set_placement_list(uint32_t *pl, unsigned *n, uint32_t type, uint32_t flags)
244 if (type & TTM_PL_FLAG_VRAM)
245 pl[(*n)++] = TTM_PL_FLAG_VRAM | flags;
246 if (type & TTM_PL_FLAG_TT)
247 pl[(*n)++] = TTM_PL_FLAG_TT | flags;
248 if (type & TTM_PL_FLAG_SYSTEM)
249 pl[(*n)++] = TTM_PL_FLAG_SYSTEM | flags;
253 set_placement_range(struct nouveau_bo *nvbo, uint32_t type)
255 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
256 struct nouveau_fb *pfb = nouveau_fb(drm->device);
257 u32 vram_pages = pfb->ram.size >> PAGE_SHIFT;
259 if (nv_device(drm->device)->card_type == NV_10 &&
260 nvbo->tile_mode && (type & TTM_PL_FLAG_VRAM) &&
261 nvbo->bo.mem.num_pages < vram_pages / 4) {
263 * Make sure that the color and depth buffers are handled
264 * by independent memory controller units. Up to a 9x
265 * speed up when alpha-blending and depth-test are enabled
268 if (nvbo->tile_flags & NOUVEAU_GEM_TILE_ZETA) {
269 nvbo->placement.fpfn = vram_pages / 2;
270 nvbo->placement.lpfn = ~0;
272 nvbo->placement.fpfn = 0;
273 nvbo->placement.lpfn = vram_pages / 2;
279 nouveau_bo_placement_set(struct nouveau_bo *nvbo, uint32_t type, uint32_t busy)
281 struct ttm_placement *pl = &nvbo->placement;
282 uint32_t flags = TTM_PL_MASK_CACHING |
283 (nvbo->pin_refcnt ? TTM_PL_FLAG_NO_EVICT : 0);
285 pl->placement = nvbo->placements;
286 set_placement_list(nvbo->placements, &pl->num_placement,
289 pl->busy_placement = nvbo->busy_placements;
290 set_placement_list(nvbo->busy_placements, &pl->num_busy_placement,
293 set_placement_range(nvbo, type);
297 nouveau_bo_pin(struct nouveau_bo *nvbo, uint32_t memtype)
299 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
300 struct ttm_buffer_object *bo = &nvbo->bo;
303 if (nvbo->pin_refcnt && !(memtype & (1 << bo->mem.mem_type))) {
304 NV_ERROR(drm, "bo %p pinned elsewhere: 0x%08x vs 0x%08x\n", bo,
305 1 << bo->mem.mem_type, memtype);
309 if (nvbo->pin_refcnt++)
312 ret = ttm_bo_reserve(bo, false, false, false, 0);
316 nouveau_bo_placement_set(nvbo, memtype, 0);
318 ret = nouveau_bo_validate(nvbo, false, false);
320 switch (bo->mem.mem_type) {
322 drm->gem.vram_available -= bo->mem.size;
325 drm->gem.gart_available -= bo->mem.size;
331 ttm_bo_unreserve(bo);
339 nouveau_bo_unpin(struct nouveau_bo *nvbo)
341 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
342 struct ttm_buffer_object *bo = &nvbo->bo;
345 if (--nvbo->pin_refcnt)
348 ret = ttm_bo_reserve(bo, false, false, false, 0);
352 nouveau_bo_placement_set(nvbo, bo->mem.placement, 0);
354 ret = nouveau_bo_validate(nvbo, false, false);
356 switch (bo->mem.mem_type) {
358 drm->gem.vram_available += bo->mem.size;
361 drm->gem.gart_available += bo->mem.size;
368 ttm_bo_unreserve(bo);
373 nouveau_bo_map(struct nouveau_bo *nvbo)
377 ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
381 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages, &nvbo->kmap);
382 ttm_bo_unreserve(&nvbo->bo);
387 nouveau_bo_unmap(struct nouveau_bo *nvbo)
390 ttm_bo_kunmap(&nvbo->kmap);
394 nouveau_bo_validate(struct nouveau_bo *nvbo, bool interruptible,
399 ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement,
400 interruptible, no_wait_gpu);
408 nouveau_bo_rd16(struct nouveau_bo *nvbo, unsigned index)
411 u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
414 return ioread16_native((void __force __iomem *)mem);
420 nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val)
423 u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
426 iowrite16_native(val, (void __force __iomem *)mem);
432 nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index)
435 u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
438 return ioread32_native((void __force __iomem *)mem);
444 nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val)
447 u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
450 iowrite32_native(val, (void __force __iomem *)mem);
455 static struct ttm_tt *
456 nouveau_ttm_tt_create(struct ttm_bo_device *bdev, unsigned long size,
457 uint32_t page_flags, struct page *dummy_read)
460 struct nouveau_drm *drm = nouveau_bdev(bdev);
461 struct drm_device *dev = drm->dev;
463 if (drm->agp.stat == ENABLED) {
464 return ttm_agp_tt_create(bdev, dev->agp->bridge, size,
465 page_flags, dummy_read);
469 return nouveau_sgdma_create_ttm(bdev, size, page_flags, dummy_read);
473 nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
475 /* We'll do this from user space. */
480 nouveau_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
481 struct ttm_mem_type_manager *man)
483 struct nouveau_drm *drm = nouveau_bdev(bdev);
487 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
488 man->available_caching = TTM_PL_MASK_CACHING;
489 man->default_caching = TTM_PL_FLAG_CACHED;
492 if (nv_device(drm->device)->card_type >= NV_50) {
493 man->func = &nouveau_vram_manager;
494 man->io_reserve_fastpath = false;
495 man->use_io_reserve_lru = true;
497 man->func = &ttm_bo_manager_func;
499 man->flags = TTM_MEMTYPE_FLAG_FIXED |
500 TTM_MEMTYPE_FLAG_MAPPABLE;
501 man->available_caching = TTM_PL_FLAG_UNCACHED |
503 man->default_caching = TTM_PL_FLAG_WC;
506 if (nv_device(drm->device)->card_type >= NV_50)
507 man->func = &nouveau_gart_manager;
509 if (drm->agp.stat != ENABLED)
510 man->func = &nv04_gart_manager;
512 man->func = &ttm_bo_manager_func;
514 if (drm->agp.stat == ENABLED) {
515 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
516 man->available_caching = TTM_PL_FLAG_UNCACHED |
518 man->default_caching = TTM_PL_FLAG_WC;
520 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE |
521 TTM_MEMTYPE_FLAG_CMA;
522 man->available_caching = TTM_PL_MASK_CACHING;
523 man->default_caching = TTM_PL_FLAG_CACHED;
534 nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
536 struct nouveau_bo *nvbo = nouveau_bo(bo);
538 switch (bo->mem.mem_type) {
540 nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_TT,
544 nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_SYSTEM, 0);
548 *pl = nvbo->placement;
552 /* GPU-assisted copy using NV_MEMORY_TO_MEMORY_FORMAT, can access
553 * TTM_PL_{VRAM,TT} directly.
557 nouveau_bo_move_accel_cleanup(struct nouveau_channel *chan,
558 struct nouveau_bo *nvbo, bool evict,
559 bool no_wait_gpu, struct ttm_mem_reg *new_mem)
561 struct nouveau_fence *fence = NULL;
564 ret = nouveau_fence_new(chan, &fence);
568 ret = ttm_bo_move_accel_cleanup(&nvbo->bo, fence, evict,
569 no_wait_gpu, new_mem);
570 nouveau_fence_unref(&fence);
575 nve0_bo_move_init(struct nouveau_channel *chan, u32 handle)
577 int ret = RING_SPACE(chan, 2);
579 BEGIN_NVC0(chan, NvSubCopy, 0x0000, 1);
580 OUT_RING (chan, handle);
587 nve0_bo_move_copy(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
588 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
590 struct nouveau_mem *node = old_mem->mm_node;
591 int ret = RING_SPACE(chan, 10);
593 BEGIN_NVC0(chan, NvSubCopy, 0x0400, 8);
594 OUT_RING (chan, upper_32_bits(node->vma[0].offset));
595 OUT_RING (chan, lower_32_bits(node->vma[0].offset));
596 OUT_RING (chan, upper_32_bits(node->vma[1].offset));
597 OUT_RING (chan, lower_32_bits(node->vma[1].offset));
598 OUT_RING (chan, PAGE_SIZE);
599 OUT_RING (chan, PAGE_SIZE);
600 OUT_RING (chan, PAGE_SIZE);
601 OUT_RING (chan, new_mem->num_pages);
602 BEGIN_IMC0(chan, NvSubCopy, 0x0300, 0x0386);
608 nvc0_bo_move_init(struct nouveau_channel *chan, u32 handle)
610 int ret = RING_SPACE(chan, 2);
612 BEGIN_NVC0(chan, NvSubCopy, 0x0000, 1);
613 OUT_RING (chan, handle);
619 nvc0_bo_move_copy(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
620 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
622 struct nouveau_mem *node = old_mem->mm_node;
623 u64 src_offset = node->vma[0].offset;
624 u64 dst_offset = node->vma[1].offset;
625 u32 page_count = new_mem->num_pages;
628 page_count = new_mem->num_pages;
630 int line_count = (page_count > 8191) ? 8191 : page_count;
632 ret = RING_SPACE(chan, 11);
636 BEGIN_NVC0(chan, NvSubCopy, 0x030c, 8);
637 OUT_RING (chan, upper_32_bits(src_offset));
638 OUT_RING (chan, lower_32_bits(src_offset));
639 OUT_RING (chan, upper_32_bits(dst_offset));
640 OUT_RING (chan, lower_32_bits(dst_offset));
641 OUT_RING (chan, PAGE_SIZE);
642 OUT_RING (chan, PAGE_SIZE);
643 OUT_RING (chan, PAGE_SIZE);
644 OUT_RING (chan, line_count);
645 BEGIN_NVC0(chan, NvSubCopy, 0x0300, 1);
646 OUT_RING (chan, 0x00000110);
648 page_count -= line_count;
649 src_offset += (PAGE_SIZE * line_count);
650 dst_offset += (PAGE_SIZE * line_count);
657 nvc0_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
658 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
660 struct nouveau_mem *node = old_mem->mm_node;
661 u64 src_offset = node->vma[0].offset;
662 u64 dst_offset = node->vma[1].offset;
663 u32 page_count = new_mem->num_pages;
666 page_count = new_mem->num_pages;
668 int line_count = (page_count > 2047) ? 2047 : page_count;
670 ret = RING_SPACE(chan, 12);
674 BEGIN_NVC0(chan, NvSubCopy, 0x0238, 2);
675 OUT_RING (chan, upper_32_bits(dst_offset));
676 OUT_RING (chan, lower_32_bits(dst_offset));
677 BEGIN_NVC0(chan, NvSubCopy, 0x030c, 6);
678 OUT_RING (chan, upper_32_bits(src_offset));
679 OUT_RING (chan, lower_32_bits(src_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 BEGIN_NVC0(chan, NvSubCopy, 0x0300, 1);
685 OUT_RING (chan, 0x00100110);
687 page_count -= line_count;
688 src_offset += (PAGE_SIZE * line_count);
689 dst_offset += (PAGE_SIZE * line_count);
696 nva3_bo_move_copy(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
697 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
699 struct nouveau_mem *node = old_mem->mm_node;
700 u64 src_offset = node->vma[0].offset;
701 u64 dst_offset = node->vma[1].offset;
702 u32 page_count = new_mem->num_pages;
705 page_count = new_mem->num_pages;
707 int line_count = (page_count > 8191) ? 8191 : page_count;
709 ret = RING_SPACE(chan, 11);
713 BEGIN_NV04(chan, NvSubCopy, 0x030c, 8);
714 OUT_RING (chan, upper_32_bits(src_offset));
715 OUT_RING (chan, lower_32_bits(src_offset));
716 OUT_RING (chan, upper_32_bits(dst_offset));
717 OUT_RING (chan, lower_32_bits(dst_offset));
718 OUT_RING (chan, PAGE_SIZE);
719 OUT_RING (chan, PAGE_SIZE);
720 OUT_RING (chan, PAGE_SIZE);
721 OUT_RING (chan, line_count);
722 BEGIN_NV04(chan, NvSubCopy, 0x0300, 1);
723 OUT_RING (chan, 0x00000110);
725 page_count -= line_count;
726 src_offset += (PAGE_SIZE * line_count);
727 dst_offset += (PAGE_SIZE * line_count);
734 nv98_bo_move_exec(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
735 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
737 struct nouveau_mem *node = old_mem->mm_node;
738 int ret = RING_SPACE(chan, 7);
740 BEGIN_NV04(chan, NvSubCopy, 0x0320, 6);
741 OUT_RING (chan, upper_32_bits(node->vma[0].offset));
742 OUT_RING (chan, lower_32_bits(node->vma[0].offset));
743 OUT_RING (chan, upper_32_bits(node->vma[1].offset));
744 OUT_RING (chan, lower_32_bits(node->vma[1].offset));
745 OUT_RING (chan, 0x00000000 /* COPY */);
746 OUT_RING (chan, new_mem->num_pages << PAGE_SHIFT);
752 nv84_bo_move_exec(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
753 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
755 struct nouveau_mem *node = old_mem->mm_node;
756 int ret = RING_SPACE(chan, 7);
758 BEGIN_NV04(chan, NvSubCopy, 0x0304, 6);
759 OUT_RING (chan, new_mem->num_pages << PAGE_SHIFT);
760 OUT_RING (chan, upper_32_bits(node->vma[0].offset));
761 OUT_RING (chan, lower_32_bits(node->vma[0].offset));
762 OUT_RING (chan, upper_32_bits(node->vma[1].offset));
763 OUT_RING (chan, lower_32_bits(node->vma[1].offset));
764 OUT_RING (chan, 0x00000000 /* MODE_COPY, QUERY_NONE */);
770 nv50_bo_move_init(struct nouveau_channel *chan, u32 handle)
772 int ret = RING_SPACE(chan, 6);
774 BEGIN_NV04(chan, NvSubCopy, 0x0000, 1);
775 OUT_RING (chan, handle);
776 BEGIN_NV04(chan, NvSubCopy, 0x0180, 3);
777 OUT_RING (chan, NvNotify0);
778 OUT_RING (chan, NvDmaFB);
779 OUT_RING (chan, NvDmaFB);
786 nv50_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
787 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
789 struct nouveau_mem *node = old_mem->mm_node;
790 struct nouveau_bo *nvbo = nouveau_bo(bo);
791 u64 length = (new_mem->num_pages << PAGE_SHIFT);
792 u64 src_offset = node->vma[0].offset;
793 u64 dst_offset = node->vma[1].offset;
797 u32 amount, stride, height;
799 amount = min(length, (u64)(4 * 1024 * 1024));
801 height = amount / stride;
803 if (new_mem->mem_type == TTM_PL_VRAM &&
804 nouveau_bo_tile_layout(nvbo)) {
805 ret = RING_SPACE(chan, 8);
809 BEGIN_NV04(chan, NvSubCopy, 0x0200, 7);
812 OUT_RING (chan, stride);
813 OUT_RING (chan, height);
818 ret = RING_SPACE(chan, 2);
822 BEGIN_NV04(chan, NvSubCopy, 0x0200, 1);
825 if (old_mem->mem_type == TTM_PL_VRAM &&
826 nouveau_bo_tile_layout(nvbo)) {
827 ret = RING_SPACE(chan, 8);
831 BEGIN_NV04(chan, NvSubCopy, 0x021c, 7);
834 OUT_RING (chan, stride);
835 OUT_RING (chan, height);
840 ret = RING_SPACE(chan, 2);
844 BEGIN_NV04(chan, NvSubCopy, 0x021c, 1);
848 ret = RING_SPACE(chan, 14);
852 BEGIN_NV04(chan, NvSubCopy, 0x0238, 2);
853 OUT_RING (chan, upper_32_bits(src_offset));
854 OUT_RING (chan, upper_32_bits(dst_offset));
855 BEGIN_NV04(chan, NvSubCopy, 0x030c, 8);
856 OUT_RING (chan, lower_32_bits(src_offset));
857 OUT_RING (chan, lower_32_bits(dst_offset));
858 OUT_RING (chan, stride);
859 OUT_RING (chan, stride);
860 OUT_RING (chan, stride);
861 OUT_RING (chan, height);
862 OUT_RING (chan, 0x00000101);
863 OUT_RING (chan, 0x00000000);
864 BEGIN_NV04(chan, NvSubCopy, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
868 src_offset += amount;
869 dst_offset += amount;
876 nv04_bo_move_init(struct nouveau_channel *chan, u32 handle)
878 int ret = RING_SPACE(chan, 4);
880 BEGIN_NV04(chan, NvSubCopy, 0x0000, 1);
881 OUT_RING (chan, handle);
882 BEGIN_NV04(chan, NvSubCopy, 0x0180, 1);
883 OUT_RING (chan, NvNotify0);
889 static inline uint32_t
890 nouveau_bo_mem_ctxdma(struct ttm_buffer_object *bo,
891 struct nouveau_channel *chan, struct ttm_mem_reg *mem)
893 if (mem->mem_type == TTM_PL_TT)
899 nv04_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
900 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
902 u32 src_offset = old_mem->start << PAGE_SHIFT;
903 u32 dst_offset = new_mem->start << PAGE_SHIFT;
904 u32 page_count = new_mem->num_pages;
907 ret = RING_SPACE(chan, 3);
911 BEGIN_NV04(chan, NvSubCopy, NV_MEMORY_TO_MEMORY_FORMAT_DMA_SOURCE, 2);
912 OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem));
913 OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem));
915 page_count = new_mem->num_pages;
917 int line_count = (page_count > 2047) ? 2047 : page_count;
919 ret = RING_SPACE(chan, 11);
923 BEGIN_NV04(chan, NvSubCopy,
924 NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
925 OUT_RING (chan, src_offset);
926 OUT_RING (chan, dst_offset);
927 OUT_RING (chan, PAGE_SIZE); /* src_pitch */
928 OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
929 OUT_RING (chan, PAGE_SIZE); /* line_length */
930 OUT_RING (chan, line_count);
931 OUT_RING (chan, 0x00000101);
932 OUT_RING (chan, 0x00000000);
933 BEGIN_NV04(chan, NvSubCopy, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
936 page_count -= line_count;
937 src_offset += (PAGE_SIZE * line_count);
938 dst_offset += (PAGE_SIZE * line_count);
945 nouveau_vma_getmap(struct nouveau_channel *chan, struct nouveau_bo *nvbo,
946 struct ttm_mem_reg *mem, struct nouveau_vma *vma)
948 struct nouveau_mem *node = mem->mm_node;
951 ret = nouveau_vm_get(nv_client(chan->cli)->vm, mem->num_pages <<
952 PAGE_SHIFT, node->page_shift,
953 NV_MEM_ACCESS_RW, vma);
957 if (mem->mem_type == TTM_PL_VRAM)
958 nouveau_vm_map(vma, node);
960 nouveau_vm_map_sg(vma, 0, mem->num_pages << PAGE_SHIFT, node);
966 nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict, bool intr,
967 bool no_wait_gpu, struct ttm_mem_reg *new_mem)
969 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
970 struct nouveau_channel *chan = chan = drm->channel;
971 struct nouveau_bo *nvbo = nouveau_bo(bo);
972 struct ttm_mem_reg *old_mem = &bo->mem;
975 mutex_lock(&chan->cli->mutex);
977 /* create temporary vmas for the transfer and attach them to the
978 * old nouveau_mem node, these will get cleaned up after ttm has
979 * destroyed the ttm_mem_reg
981 if (nv_device(drm->device)->card_type >= NV_50) {
982 struct nouveau_mem *node = old_mem->mm_node;
984 ret = nouveau_vma_getmap(chan, nvbo, old_mem, &node->vma[0]);
988 ret = nouveau_vma_getmap(chan, nvbo, new_mem, &node->vma[1]);
993 ret = drm->ttm.move(chan, bo, &bo->mem, new_mem);
995 ret = nouveau_bo_move_accel_cleanup(chan, nvbo, evict,
996 no_wait_gpu, new_mem);
1000 mutex_unlock(&chan->cli->mutex);
1005 nouveau_bo_move_init(struct nouveau_drm *drm)
1007 static const struct {
1011 int (*exec)(struct nouveau_channel *,
1012 struct ttm_buffer_object *,
1013 struct ttm_mem_reg *, struct ttm_mem_reg *);
1014 int (*init)(struct nouveau_channel *, u32 handle);
1016 { "COPY", 0, 0xa0b5, nve0_bo_move_copy, nve0_bo_move_init },
1017 { "GRCE", 0, 0xa0b5, nve0_bo_move_copy, nvc0_bo_move_init },
1018 { "COPY1", 5, 0x90b8, nvc0_bo_move_copy, nvc0_bo_move_init },
1019 { "COPY0", 4, 0x90b5, nvc0_bo_move_copy, nvc0_bo_move_init },
1020 { "COPY", 0, 0x85b5, nva3_bo_move_copy, nv50_bo_move_init },
1021 { "CRYPT", 0, 0x74c1, nv84_bo_move_exec, nv50_bo_move_init },
1022 { "M2MF", 0, 0x9039, nvc0_bo_move_m2mf, nvc0_bo_move_init },
1023 { "M2MF", 0, 0x5039, nv50_bo_move_m2mf, nv50_bo_move_init },
1024 { "M2MF", 0, 0x0039, nv04_bo_move_m2mf, nv04_bo_move_init },
1026 { "CRYPT", 0, 0x88b4, nv98_bo_move_exec, nv50_bo_move_init },
1027 }, *mthd = _methods;
1028 const char *name = "CPU";
1032 struct nouveau_object *object;
1033 struct nouveau_channel *chan;
1034 u32 handle = (mthd->engine << 16) | mthd->oclass;
1036 if (mthd->init == nve0_bo_move_init)
1039 chan = drm->channel;
1043 ret = nouveau_object_new(nv_object(drm), chan->handle, handle,
1044 mthd->oclass, NULL, 0, &object);
1046 ret = mthd->init(chan, handle);
1048 nouveau_object_del(nv_object(drm),
1049 chan->handle, handle);
1053 drm->ttm.move = mthd->exec;
1057 } while ((++mthd)->exec);
1059 NV_INFO(drm, "MM: using %s for buffer copies\n", name);
1063 nouveau_bo_move_flipd(struct ttm_buffer_object *bo, bool evict, bool intr,
1064 bool no_wait_gpu, struct ttm_mem_reg *new_mem)
1066 u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
1067 struct ttm_placement placement;
1068 struct ttm_mem_reg tmp_mem;
1071 placement.fpfn = placement.lpfn = 0;
1072 placement.num_placement = placement.num_busy_placement = 1;
1073 placement.placement = placement.busy_placement = &placement_memtype;
1076 tmp_mem.mm_node = NULL;
1077 ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_gpu);
1081 ret = ttm_tt_bind(bo->ttm, &tmp_mem);
1085 ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_gpu, &tmp_mem);
1089 ret = ttm_bo_move_ttm(bo, true, no_wait_gpu, new_mem);
1091 ttm_bo_mem_put(bo, &tmp_mem);
1096 nouveau_bo_move_flips(struct ttm_buffer_object *bo, bool evict, bool intr,
1097 bool no_wait_gpu, struct ttm_mem_reg *new_mem)
1099 u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
1100 struct ttm_placement placement;
1101 struct ttm_mem_reg tmp_mem;
1104 placement.fpfn = placement.lpfn = 0;
1105 placement.num_placement = placement.num_busy_placement = 1;
1106 placement.placement = placement.busy_placement = &placement_memtype;
1109 tmp_mem.mm_node = NULL;
1110 ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_gpu);
1114 ret = ttm_bo_move_ttm(bo, true, no_wait_gpu, &tmp_mem);
1118 ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_gpu, new_mem);
1123 ttm_bo_mem_put(bo, &tmp_mem);
1128 nouveau_bo_move_ntfy(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem)
1130 struct nouveau_bo *nvbo = nouveau_bo(bo);
1131 struct nouveau_vma *vma;
1133 /* ttm can now (stupidly) pass the driver bos it didn't create... */
1134 if (bo->destroy != nouveau_bo_del_ttm)
1137 list_for_each_entry(vma, &nvbo->vma_list, head) {
1138 if (new_mem && new_mem->mem_type == TTM_PL_VRAM) {
1139 nouveau_vm_map(vma, new_mem->mm_node);
1141 if (new_mem && new_mem->mem_type == TTM_PL_TT &&
1142 nvbo->page_shift == vma->vm->vmm->spg_shift) {
1143 if (((struct nouveau_mem *)new_mem->mm_node)->sg)
1144 nouveau_vm_map_sg_table(vma, 0, new_mem->
1145 num_pages << PAGE_SHIFT,
1148 nouveau_vm_map_sg(vma, 0, new_mem->
1149 num_pages << PAGE_SHIFT,
1152 nouveau_vm_unmap(vma);
1158 nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem,
1159 struct nouveau_drm_tile **new_tile)
1161 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
1162 struct drm_device *dev = drm->dev;
1163 struct nouveau_bo *nvbo = nouveau_bo(bo);
1164 u64 offset = new_mem->start << PAGE_SHIFT;
1167 if (new_mem->mem_type != TTM_PL_VRAM)
1170 if (nv_device(drm->device)->card_type >= NV_10) {
1171 *new_tile = nv10_bo_set_tiling(dev, offset, new_mem->size,
1180 nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo,
1181 struct nouveau_drm_tile *new_tile,
1182 struct nouveau_drm_tile **old_tile)
1184 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
1185 struct drm_device *dev = drm->dev;
1187 nv10_bo_put_tile_region(dev, *old_tile, bo->sync_obj);
1188 *old_tile = new_tile;
1192 nouveau_bo_move(struct ttm_buffer_object *bo, bool evict, bool intr,
1193 bool no_wait_gpu, struct ttm_mem_reg *new_mem)
1195 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
1196 struct nouveau_bo *nvbo = nouveau_bo(bo);
1197 struct ttm_mem_reg *old_mem = &bo->mem;
1198 struct nouveau_drm_tile *new_tile = NULL;
1201 if (nv_device(drm->device)->card_type < NV_50) {
1202 ret = nouveau_bo_vm_bind(bo, new_mem, &new_tile);
1208 if (old_mem->mem_type == TTM_PL_SYSTEM && !bo->ttm) {
1209 BUG_ON(bo->mem.mm_node != NULL);
1211 new_mem->mm_node = NULL;
1215 /* CPU copy if we have no accelerated method available */
1216 if (!drm->ttm.move) {
1217 ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, new_mem);
1221 /* Hardware assisted copy. */
1222 if (new_mem->mem_type == TTM_PL_SYSTEM)
1223 ret = nouveau_bo_move_flipd(bo, evict, intr,
1224 no_wait_gpu, new_mem);
1225 else if (old_mem->mem_type == TTM_PL_SYSTEM)
1226 ret = nouveau_bo_move_flips(bo, evict, intr,
1227 no_wait_gpu, new_mem);
1229 ret = nouveau_bo_move_m2mf(bo, evict, intr,
1230 no_wait_gpu, new_mem);
1235 /* Fallback to software copy. */
1236 ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, new_mem);
1239 if (nv_device(drm->device)->card_type < NV_50) {
1241 nouveau_bo_vm_cleanup(bo, NULL, &new_tile);
1243 nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile);
1250 nouveau_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp)
1256 nouveau_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1258 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
1259 struct nouveau_drm *drm = nouveau_bdev(bdev);
1260 struct drm_device *dev = drm->dev;
1263 mem->bus.addr = NULL;
1264 mem->bus.offset = 0;
1265 mem->bus.size = mem->num_pages << PAGE_SHIFT;
1267 mem->bus.is_iomem = false;
1268 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
1270 switch (mem->mem_type) {
1276 if (drm->agp.stat == ENABLED) {
1277 mem->bus.offset = mem->start << PAGE_SHIFT;
1278 mem->bus.base = drm->agp.base;
1279 mem->bus.is_iomem = true;
1284 mem->bus.offset = mem->start << PAGE_SHIFT;
1285 mem->bus.base = pci_resource_start(dev->pdev, 1);
1286 mem->bus.is_iomem = true;
1287 if (nv_device(drm->device)->card_type >= NV_50) {
1288 struct nouveau_bar *bar = nouveau_bar(drm->device);
1289 struct nouveau_mem *node = mem->mm_node;
1291 ret = bar->umap(bar, node, NV_MEM_ACCESS_RW,
1296 mem->bus.offset = node->bar_vma.offset;
1306 nouveau_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1308 struct nouveau_drm *drm = nouveau_bdev(bdev);
1309 struct nouveau_bar *bar = nouveau_bar(drm->device);
1310 struct nouveau_mem *node = mem->mm_node;
1312 if (!node->bar_vma.node)
1315 bar->unmap(bar, &node->bar_vma);
1319 nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
1321 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
1322 struct nouveau_bo *nvbo = nouveau_bo(bo);
1323 struct nouveau_device *device = nv_device(drm->device);
1324 u32 mappable = pci_resource_len(device->pdev, 1) >> PAGE_SHIFT;
1326 /* as long as the bo isn't in vram, and isn't tiled, we've got
1327 * nothing to do here.
1329 if (bo->mem.mem_type != TTM_PL_VRAM) {
1330 if (nv_device(drm->device)->card_type < NV_50 ||
1331 !nouveau_bo_tile_layout(nvbo))
1335 /* make sure bo is in mappable vram */
1336 if (bo->mem.start + bo->mem.num_pages < mappable)
1340 nvbo->placement.fpfn = 0;
1341 nvbo->placement.lpfn = mappable;
1342 nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_VRAM, 0);
1343 return nouveau_bo_validate(nvbo, false, false);
1347 nouveau_ttm_tt_populate(struct ttm_tt *ttm)
1349 struct ttm_dma_tt *ttm_dma = (void *)ttm;
1350 struct nouveau_drm *drm;
1351 struct drm_device *dev;
1354 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
1356 if (ttm->state != tt_unpopulated)
1359 if (slave && ttm->sg) {
1360 /* make userspace faulting work */
1361 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
1362 ttm_dma->dma_address, ttm->num_pages);
1363 ttm->state = tt_unbound;
1367 drm = nouveau_bdev(ttm->bdev);
1371 if (drm->agp.stat == ENABLED) {
1372 return ttm_agp_tt_populate(ttm);
1376 #ifdef CONFIG_SWIOTLB
1377 if (swiotlb_nr_tbl()) {
1378 return ttm_dma_populate((void *)ttm, dev->dev);
1382 r = ttm_pool_populate(ttm);
1387 for (i = 0; i < ttm->num_pages; i++) {
1388 ttm_dma->dma_address[i] = pci_map_page(dev->pdev, ttm->pages[i],
1390 PCI_DMA_BIDIRECTIONAL);
1391 if (pci_dma_mapping_error(dev->pdev, ttm_dma->dma_address[i])) {
1393 pci_unmap_page(dev->pdev, ttm_dma->dma_address[i],
1394 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
1395 ttm_dma->dma_address[i] = 0;
1397 ttm_pool_unpopulate(ttm);
1405 nouveau_ttm_tt_unpopulate(struct ttm_tt *ttm)
1407 struct ttm_dma_tt *ttm_dma = (void *)ttm;
1408 struct nouveau_drm *drm;
1409 struct drm_device *dev;
1411 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
1416 drm = nouveau_bdev(ttm->bdev);
1420 if (drm->agp.stat == ENABLED) {
1421 ttm_agp_tt_unpopulate(ttm);
1426 #ifdef CONFIG_SWIOTLB
1427 if (swiotlb_nr_tbl()) {
1428 ttm_dma_unpopulate((void *)ttm, dev->dev);
1433 for (i = 0; i < ttm->num_pages; i++) {
1434 if (ttm_dma->dma_address[i]) {
1435 pci_unmap_page(dev->pdev, ttm_dma->dma_address[i],
1436 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
1440 ttm_pool_unpopulate(ttm);
1444 nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence)
1446 struct nouveau_fence *old_fence = NULL;
1449 nouveau_fence_ref(fence);
1451 spin_lock(&nvbo->bo.bdev->fence_lock);
1452 old_fence = nvbo->bo.sync_obj;
1453 nvbo->bo.sync_obj = fence;
1454 spin_unlock(&nvbo->bo.bdev->fence_lock);
1456 nouveau_fence_unref(&old_fence);
1460 nouveau_bo_fence_unref(void **sync_obj)
1462 nouveau_fence_unref((struct nouveau_fence **)sync_obj);
1466 nouveau_bo_fence_ref(void *sync_obj)
1468 return nouveau_fence_ref(sync_obj);
1472 nouveau_bo_fence_signalled(void *sync_obj)
1474 return nouveau_fence_done(sync_obj);
1478 nouveau_bo_fence_wait(void *sync_obj, bool lazy, bool intr)
1480 return nouveau_fence_wait(sync_obj, lazy, intr);
1484 nouveau_bo_fence_flush(void *sync_obj)
1489 struct ttm_bo_driver nouveau_bo_driver = {
1490 .ttm_tt_create = &nouveau_ttm_tt_create,
1491 .ttm_tt_populate = &nouveau_ttm_tt_populate,
1492 .ttm_tt_unpopulate = &nouveau_ttm_tt_unpopulate,
1493 .invalidate_caches = nouveau_bo_invalidate_caches,
1494 .init_mem_type = nouveau_bo_init_mem_type,
1495 .evict_flags = nouveau_bo_evict_flags,
1496 .move_notify = nouveau_bo_move_ntfy,
1497 .move = nouveau_bo_move,
1498 .verify_access = nouveau_bo_verify_access,
1499 .sync_obj_signaled = nouveau_bo_fence_signalled,
1500 .sync_obj_wait = nouveau_bo_fence_wait,
1501 .sync_obj_flush = nouveau_bo_fence_flush,
1502 .sync_obj_unref = nouveau_bo_fence_unref,
1503 .sync_obj_ref = nouveau_bo_fence_ref,
1504 .fault_reserve_notify = &nouveau_ttm_fault_reserve_notify,
1505 .io_mem_reserve = &nouveau_ttm_io_mem_reserve,
1506 .io_mem_free = &nouveau_ttm_io_mem_free,
1509 struct nouveau_vma *
1510 nouveau_bo_vma_find(struct nouveau_bo *nvbo, struct nouveau_vm *vm)
1512 struct nouveau_vma *vma;
1513 list_for_each_entry(vma, &nvbo->vma_list, head) {
1522 nouveau_bo_vma_add(struct nouveau_bo *nvbo, struct nouveau_vm *vm,
1523 struct nouveau_vma *vma)
1525 const u32 size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
1526 struct nouveau_mem *node = nvbo->bo.mem.mm_node;
1529 ret = nouveau_vm_get(vm, size, nvbo->page_shift,
1530 NV_MEM_ACCESS_RW, vma);
1534 if (nvbo->bo.mem.mem_type == TTM_PL_VRAM)
1535 nouveau_vm_map(vma, nvbo->bo.mem.mm_node);
1536 else if (nvbo->bo.mem.mem_type == TTM_PL_TT) {
1538 nouveau_vm_map_sg_table(vma, 0, size, node);
1540 nouveau_vm_map_sg(vma, 0, size, node);
1543 list_add_tail(&vma->head, &nvbo->vma_list);
1549 nouveau_bo_vma_del(struct nouveau_bo *nvbo, struct nouveau_vma *vma)
1552 if (nvbo->bo.mem.mem_type != TTM_PL_SYSTEM) {
1553 spin_lock(&nvbo->bo.bdev->fence_lock);
1554 ttm_bo_wait(&nvbo->bo, false, false, false);
1555 spin_unlock(&nvbo->bo.bdev->fence_lock);
1556 nouveau_vm_unmap(vma);
1559 nouveau_vm_put(vma);
1560 list_del(&vma->head);