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 <linux/dma-mapping.h>
31 #include <drm/ttm/ttm_tt.h>
33 #include "nouveau_drv.h"
34 #include "nouveau_chan.h"
35 #include "nouveau_fence.h"
37 #include "nouveau_bo.h"
38 #include "nouveau_ttm.h"
39 #include "nouveau_gem.h"
40 #include "nouveau_mem.h"
41 #include "nouveau_vmm.h"
43 #include <nvif/class.h>
44 #include <nvif/if500b.h>
45 #include <nvif/if900b.h>
47 static int nouveau_ttm_tt_bind(struct ttm_device *bdev, struct ttm_tt *ttm,
48 struct ttm_resource *reg);
49 static void nouveau_ttm_tt_unbind(struct ttm_device *bdev, struct ttm_tt *ttm);
52 * NV10-NV40 tiling helpers
56 nv10_bo_update_tile_region(struct drm_device *dev, struct nouveau_drm_tile *reg,
57 u32 addr, u32 size, u32 pitch, u32 flags)
59 struct nouveau_drm *drm = nouveau_drm(dev);
60 int i = reg - drm->tile.reg;
61 struct nvkm_fb *fb = nvxx_fb(&drm->client.device);
62 struct nvkm_fb_tile *tile = &fb->tile.region[i];
64 nouveau_fence_unref(®->fence);
67 nvkm_fb_tile_fini(fb, i, tile);
70 nvkm_fb_tile_init(fb, i, addr, size, pitch, flags, tile);
72 nvkm_fb_tile_prog(fb, i, tile);
75 static struct nouveau_drm_tile *
76 nv10_bo_get_tile_region(struct drm_device *dev, int i)
78 struct nouveau_drm *drm = nouveau_drm(dev);
79 struct nouveau_drm_tile *tile = &drm->tile.reg[i];
81 spin_lock(&drm->tile.lock);
84 (!tile->fence || nouveau_fence_done(tile->fence)))
89 spin_unlock(&drm->tile.lock);
94 nv10_bo_put_tile_region(struct drm_device *dev, struct nouveau_drm_tile *tile,
95 struct dma_fence *fence)
97 struct nouveau_drm *drm = nouveau_drm(dev);
100 spin_lock(&drm->tile.lock);
101 tile->fence = (struct nouveau_fence *)dma_fence_get(fence);
103 spin_unlock(&drm->tile.lock);
107 static struct nouveau_drm_tile *
108 nv10_bo_set_tiling(struct drm_device *dev, u32 addr,
109 u32 size, u32 pitch, u32 zeta)
111 struct nouveau_drm *drm = nouveau_drm(dev);
112 struct nvkm_fb *fb = nvxx_fb(&drm->client.device);
113 struct nouveau_drm_tile *tile, *found = NULL;
116 for (i = 0; i < fb->tile.regions; i++) {
117 tile = nv10_bo_get_tile_region(dev, i);
119 if (pitch && !found) {
123 } else if (tile && fb->tile.region[i].pitch) {
124 /* Kill an unused tile region. */
125 nv10_bo_update_tile_region(dev, tile, 0, 0, 0, 0);
128 nv10_bo_put_tile_region(dev, tile, NULL);
132 nv10_bo_update_tile_region(dev, found, addr, size, pitch, zeta);
137 nouveau_bo_del_ttm(struct ttm_buffer_object *bo)
139 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
140 struct drm_device *dev = drm->dev;
141 struct nouveau_bo *nvbo = nouveau_bo(bo);
143 WARN_ON(nvbo->bo.pin_count > 0);
144 nouveau_bo_del_io_reserve_lru(bo);
145 nv10_bo_put_tile_region(dev, nvbo->tile, NULL);
148 * If nouveau_bo_new() allocated this buffer, the GEM object was never
149 * initialized, so don't attempt to release it.
152 drm_gem_object_release(&bo->base);
154 dma_resv_fini(&bo->base._resv);
160 roundup_64(u64 x, u32 y)
168 nouveau_bo_fixup_align(struct nouveau_bo *nvbo, int *align, u64 *size)
170 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
171 struct nvif_device *device = &drm->client.device;
173 if (device->info.family < NV_DEVICE_INFO_V0_TESLA) {
175 if (device->info.chipset >= 0x40) {
177 *size = roundup_64(*size, 64 * nvbo->mode);
179 } else if (device->info.chipset >= 0x30) {
181 *size = roundup_64(*size, 64 * nvbo->mode);
183 } else if (device->info.chipset >= 0x20) {
185 *size = roundup_64(*size, 64 * nvbo->mode);
187 } else if (device->info.chipset >= 0x10) {
189 *size = roundup_64(*size, 32 * nvbo->mode);
193 *size = roundup_64(*size, (1 << nvbo->page));
194 *align = max((1 << nvbo->page), *align);
197 *size = roundup_64(*size, PAGE_SIZE);
201 nouveau_bo_alloc(struct nouveau_cli *cli, u64 *size, int *align, u32 domain,
202 u32 tile_mode, u32 tile_flags)
204 struct nouveau_drm *drm = cli->drm;
205 struct nouveau_bo *nvbo;
206 struct nvif_mmu *mmu = &cli->mmu;
207 struct nvif_vmm *vmm = cli->svm.cli ? &cli->svm.vmm : &cli->vmm.vmm;
211 NV_WARN(drm, "skipped size %016llx\n", *size);
212 return ERR_PTR(-EINVAL);
215 nvbo = kzalloc(sizeof(struct nouveau_bo), GFP_KERNEL);
217 return ERR_PTR(-ENOMEM);
218 INIT_LIST_HEAD(&nvbo->head);
219 INIT_LIST_HEAD(&nvbo->entry);
220 INIT_LIST_HEAD(&nvbo->vma_list);
221 nvbo->bo.bdev = &drm->ttm.bdev;
223 /* This is confusing, and doesn't actually mean we want an uncached
224 * mapping, but is what NOUVEAU_GEM_DOMAIN_COHERENT gets translated
225 * into in nouveau_gem_new().
227 if (domain & NOUVEAU_GEM_DOMAIN_COHERENT) {
228 /* Determine if we can get a cache-coherent map, forcing
229 * uncached mapping if we can't.
231 if (!nouveau_drm_use_coherent_gpu_mapping(drm))
232 nvbo->force_coherent = true;
235 if (cli->device.info.family >= NV_DEVICE_INFO_V0_FERMI) {
236 nvbo->kind = (tile_flags & 0x0000ff00) >> 8;
237 if (!nvif_mmu_kind_valid(mmu, nvbo->kind)) {
239 return ERR_PTR(-EINVAL);
242 nvbo->comp = mmu->kind[nvbo->kind] != nvbo->kind;
244 if (cli->device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
245 nvbo->kind = (tile_flags & 0x00007f00) >> 8;
246 nvbo->comp = (tile_flags & 0x00030000) >> 16;
247 if (!nvif_mmu_kind_valid(mmu, nvbo->kind)) {
249 return ERR_PTR(-EINVAL);
252 nvbo->zeta = (tile_flags & 0x00000007);
254 nvbo->mode = tile_mode;
255 nvbo->contig = !(tile_flags & NOUVEAU_GEM_TILE_NONCONTIG);
257 /* Determine the desirable target GPU page size for the buffer. */
258 for (i = 0; i < vmm->page_nr; i++) {
259 /* Because we cannot currently allow VMM maps to fail
260 * during buffer migration, we need to determine page
261 * size for the buffer up-front, and pre-allocate its
264 * Skip page sizes that can't support needed domains.
266 if (cli->device.info.family > NV_DEVICE_INFO_V0_CURIE &&
267 (domain & NOUVEAU_GEM_DOMAIN_VRAM) && !vmm->page[i].vram)
269 if ((domain & NOUVEAU_GEM_DOMAIN_GART) &&
270 (!vmm->page[i].host || vmm->page[i].shift > PAGE_SHIFT))
273 /* Select this page size if it's the first that supports
274 * the potential memory domains, or when it's compatible
275 * with the requested compression settings.
277 if (pi < 0 || !nvbo->comp || vmm->page[i].comp)
280 /* Stop once the buffer is larger than the current page size. */
281 if (*size >= 1ULL << vmm->page[i].shift)
285 if (WARN_ON(pi < 0)) {
287 return ERR_PTR(-EINVAL);
290 /* Disable compression if suitable settings couldn't be found. */
291 if (nvbo->comp && !vmm->page[pi].comp) {
292 if (mmu->object.oclass >= NVIF_CLASS_MMU_GF100)
293 nvbo->kind = mmu->kind[nvbo->kind];
296 nvbo->page = vmm->page[pi].shift;
298 nouveau_bo_fixup_align(nvbo, align, size);
304 nouveau_bo_init(struct nouveau_bo *nvbo, u64 size, int align, u32 domain,
305 struct sg_table *sg, struct dma_resv *robj)
307 int type = sg ? ttm_bo_type_sg : ttm_bo_type_device;
310 nouveau_bo_placement_set(nvbo, domain, 0);
311 INIT_LIST_HEAD(&nvbo->io_reserve_lru);
313 ret = ttm_bo_init_validate(nvbo->bo.bdev, &nvbo->bo, type,
314 &nvbo->placement, align >> PAGE_SHIFT, false,
315 sg, robj, nouveau_bo_del_ttm);
317 /* ttm will call nouveau_bo_del_ttm if it fails.. */
325 nouveau_bo_new(struct nouveau_cli *cli, u64 size, int align,
326 uint32_t domain, uint32_t tile_mode, uint32_t tile_flags,
327 struct sg_table *sg, struct dma_resv *robj,
328 struct nouveau_bo **pnvbo)
330 struct nouveau_bo *nvbo;
333 nvbo = nouveau_bo_alloc(cli, &size, &align, domain, tile_mode,
336 return PTR_ERR(nvbo);
338 nvbo->bo.base.size = size;
339 dma_resv_init(&nvbo->bo.base._resv);
340 drm_vma_node_reset(&nvbo->bo.base.vma_node);
342 ret = nouveau_bo_init(nvbo, size, align, domain, sg, robj);
351 set_placement_list(struct ttm_place *pl, unsigned *n, uint32_t domain)
355 if (domain & NOUVEAU_GEM_DOMAIN_VRAM) {
356 pl[*n].mem_type = TTM_PL_VRAM;
360 if (domain & NOUVEAU_GEM_DOMAIN_GART) {
361 pl[*n].mem_type = TTM_PL_TT;
365 if (domain & NOUVEAU_GEM_DOMAIN_CPU) {
366 pl[*n].mem_type = TTM_PL_SYSTEM;
367 pl[(*n)++].flags = 0;
372 set_placement_range(struct nouveau_bo *nvbo, uint32_t domain)
374 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
375 u64 vram_size = drm->client.device.info.ram_size;
376 unsigned i, fpfn, lpfn;
378 if (drm->client.device.info.family == NV_DEVICE_INFO_V0_CELSIUS &&
379 nvbo->mode && (domain & NOUVEAU_GEM_DOMAIN_VRAM) &&
380 nvbo->bo.base.size < vram_size / 4) {
382 * Make sure that the color and depth buffers are handled
383 * by independent memory controller units. Up to a 9x
384 * speed up when alpha-blending and depth-test are enabled
388 fpfn = (vram_size / 2) >> PAGE_SHIFT;
392 lpfn = (vram_size / 2) >> PAGE_SHIFT;
394 for (i = 0; i < nvbo->placement.num_placement; ++i) {
395 nvbo->placements[i].fpfn = fpfn;
396 nvbo->placements[i].lpfn = lpfn;
398 for (i = 0; i < nvbo->placement.num_busy_placement; ++i) {
399 nvbo->busy_placements[i].fpfn = fpfn;
400 nvbo->busy_placements[i].lpfn = lpfn;
406 nouveau_bo_placement_set(struct nouveau_bo *nvbo, uint32_t domain,
409 struct ttm_placement *pl = &nvbo->placement;
411 pl->placement = nvbo->placements;
412 set_placement_list(nvbo->placements, &pl->num_placement, domain);
414 pl->busy_placement = nvbo->busy_placements;
415 set_placement_list(nvbo->busy_placements, &pl->num_busy_placement,
418 set_placement_range(nvbo, domain);
422 nouveau_bo_pin(struct nouveau_bo *nvbo, uint32_t domain, bool contig)
424 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
425 struct ttm_buffer_object *bo = &nvbo->bo;
426 bool force = false, evict = false;
429 ret = ttm_bo_reserve(bo, false, false, NULL);
433 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA &&
434 domain == NOUVEAU_GEM_DOMAIN_VRAM && contig) {
442 if (nvbo->bo.pin_count) {
445 switch (bo->resource->mem_type) {
447 error |= !(domain & NOUVEAU_GEM_DOMAIN_VRAM);
450 error |= !(domain & NOUVEAU_GEM_DOMAIN_GART);
457 NV_ERROR(drm, "bo %p pinned elsewhere: "
458 "0x%08x vs 0x%08x\n", bo,
459 bo->resource->mem_type, domain);
462 ttm_bo_pin(&nvbo->bo);
467 nouveau_bo_placement_set(nvbo, NOUVEAU_GEM_DOMAIN_GART, 0);
468 ret = nouveau_bo_validate(nvbo, false, false);
473 nouveau_bo_placement_set(nvbo, domain, 0);
474 ret = nouveau_bo_validate(nvbo, false, false);
478 ttm_bo_pin(&nvbo->bo);
480 switch (bo->resource->mem_type) {
482 drm->gem.vram_available -= bo->base.size;
485 drm->gem.gart_available -= bo->base.size;
493 nvbo->contig = false;
494 ttm_bo_unreserve(bo);
499 nouveau_bo_unpin(struct nouveau_bo *nvbo)
501 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
502 struct ttm_buffer_object *bo = &nvbo->bo;
505 ret = ttm_bo_reserve(bo, false, false, NULL);
509 ttm_bo_unpin(&nvbo->bo);
510 if (!nvbo->bo.pin_count) {
511 switch (bo->resource->mem_type) {
513 drm->gem.vram_available += bo->base.size;
516 drm->gem.gart_available += bo->base.size;
523 ttm_bo_unreserve(bo);
528 nouveau_bo_map(struct nouveau_bo *nvbo)
532 ret = ttm_bo_reserve(&nvbo->bo, false, false, NULL);
536 ret = ttm_bo_kmap(&nvbo->bo, 0, PFN_UP(nvbo->bo.base.size), &nvbo->kmap);
538 ttm_bo_unreserve(&nvbo->bo);
543 nouveau_bo_unmap(struct nouveau_bo *nvbo)
548 ttm_bo_kunmap(&nvbo->kmap);
552 nouveau_bo_sync_for_device(struct nouveau_bo *nvbo)
554 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
555 struct ttm_tt *ttm_dma = (struct ttm_tt *)nvbo->bo.ttm;
558 if (!ttm_dma || !ttm_dma->dma_address)
560 if (!ttm_dma->pages) {
561 NV_DEBUG(drm, "ttm_dma 0x%p: pages NULL\n", ttm_dma);
565 /* Don't waste time looping if the object is coherent */
566 if (nvbo->force_coherent)
570 while (i < ttm_dma->num_pages) {
571 struct page *p = ttm_dma->pages[i];
572 size_t num_pages = 1;
574 for (j = i + 1; j < ttm_dma->num_pages; ++j) {
575 if (++p != ttm_dma->pages[j])
580 dma_sync_single_for_device(drm->dev->dev,
581 ttm_dma->dma_address[i],
582 num_pages * PAGE_SIZE, DMA_TO_DEVICE);
588 nouveau_bo_sync_for_cpu(struct nouveau_bo *nvbo)
590 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
591 struct ttm_tt *ttm_dma = (struct ttm_tt *)nvbo->bo.ttm;
594 if (!ttm_dma || !ttm_dma->dma_address)
596 if (!ttm_dma->pages) {
597 NV_DEBUG(drm, "ttm_dma 0x%p: pages NULL\n", ttm_dma);
601 /* Don't waste time looping if the object is coherent */
602 if (nvbo->force_coherent)
606 while (i < ttm_dma->num_pages) {
607 struct page *p = ttm_dma->pages[i];
608 size_t num_pages = 1;
610 for (j = i + 1; j < ttm_dma->num_pages; ++j) {
611 if (++p != ttm_dma->pages[j])
617 dma_sync_single_for_cpu(drm->dev->dev, ttm_dma->dma_address[i],
618 num_pages * PAGE_SIZE, DMA_FROM_DEVICE);
623 void nouveau_bo_add_io_reserve_lru(struct ttm_buffer_object *bo)
625 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
626 struct nouveau_bo *nvbo = nouveau_bo(bo);
628 mutex_lock(&drm->ttm.io_reserve_mutex);
629 list_move_tail(&nvbo->io_reserve_lru, &drm->ttm.io_reserve_lru);
630 mutex_unlock(&drm->ttm.io_reserve_mutex);
633 void nouveau_bo_del_io_reserve_lru(struct ttm_buffer_object *bo)
635 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
636 struct nouveau_bo *nvbo = nouveau_bo(bo);
638 mutex_lock(&drm->ttm.io_reserve_mutex);
639 list_del_init(&nvbo->io_reserve_lru);
640 mutex_unlock(&drm->ttm.io_reserve_mutex);
644 nouveau_bo_validate(struct nouveau_bo *nvbo, bool interruptible,
647 struct ttm_operation_ctx ctx = { interruptible, no_wait_gpu };
650 ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement, &ctx);
654 nouveau_bo_sync_for_device(nvbo);
660 nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val)
663 u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
668 iowrite16_native(val, (void __force __iomem *)mem);
674 nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index)
677 u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
682 return ioread32_native((void __force __iomem *)mem);
688 nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val)
691 u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
696 iowrite32_native(val, (void __force __iomem *)mem);
701 static struct ttm_tt *
702 nouveau_ttm_tt_create(struct ttm_buffer_object *bo, uint32_t page_flags)
704 #if IS_ENABLED(CONFIG_AGP)
705 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
707 if (drm->agp.bridge) {
708 return ttm_agp_tt_create(bo, drm->agp.bridge, page_flags);
712 return nouveau_sgdma_create_ttm(bo, page_flags);
716 nouveau_ttm_tt_bind(struct ttm_device *bdev, struct ttm_tt *ttm,
717 struct ttm_resource *reg)
719 #if IS_ENABLED(CONFIG_AGP)
720 struct nouveau_drm *drm = nouveau_bdev(bdev);
724 #if IS_ENABLED(CONFIG_AGP)
726 return ttm_agp_bind(ttm, reg);
728 return nouveau_sgdma_bind(bdev, ttm, reg);
732 nouveau_ttm_tt_unbind(struct ttm_device *bdev, struct ttm_tt *ttm)
734 #if IS_ENABLED(CONFIG_AGP)
735 struct nouveau_drm *drm = nouveau_bdev(bdev);
737 if (drm->agp.bridge) {
742 nouveau_sgdma_unbind(bdev, ttm);
746 nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
748 struct nouveau_bo *nvbo = nouveau_bo(bo);
750 switch (bo->resource->mem_type) {
752 nouveau_bo_placement_set(nvbo, NOUVEAU_GEM_DOMAIN_GART,
753 NOUVEAU_GEM_DOMAIN_CPU);
756 nouveau_bo_placement_set(nvbo, NOUVEAU_GEM_DOMAIN_CPU, 0);
760 *pl = nvbo->placement;
764 nouveau_bo_move_prep(struct nouveau_drm *drm, struct ttm_buffer_object *bo,
765 struct ttm_resource *reg)
767 struct nouveau_mem *old_mem = nouveau_mem(bo->resource);
768 struct nouveau_mem *new_mem = nouveau_mem(reg);
769 struct nvif_vmm *vmm = &drm->client.vmm.vmm;
772 ret = nvif_vmm_get(vmm, LAZY, false, old_mem->mem.page, 0,
773 old_mem->mem.size, &old_mem->vma[0]);
777 ret = nvif_vmm_get(vmm, LAZY, false, new_mem->mem.page, 0,
778 new_mem->mem.size, &old_mem->vma[1]);
782 ret = nouveau_mem_map(old_mem, vmm, &old_mem->vma[0]);
786 ret = nouveau_mem_map(new_mem, vmm, &old_mem->vma[1]);
789 nvif_vmm_put(vmm, &old_mem->vma[1]);
790 nvif_vmm_put(vmm, &old_mem->vma[0]);
796 nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict,
797 struct ttm_operation_ctx *ctx,
798 struct ttm_resource *new_reg)
800 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
801 struct nouveau_channel *chan = drm->ttm.chan;
802 struct nouveau_cli *cli = (void *)chan->user.client;
803 struct nouveau_fence *fence;
806 /* create temporary vmas for the transfer and attach them to the
807 * old nvkm_mem node, these will get cleaned up after ttm has
808 * destroyed the ttm_resource
810 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
811 ret = nouveau_bo_move_prep(drm, bo, new_reg);
816 if (drm_drv_uses_atomic_modeset(drm->dev))
817 mutex_lock(&cli->mutex);
819 mutex_lock_nested(&cli->mutex, SINGLE_DEPTH_NESTING);
820 ret = nouveau_fence_sync(nouveau_bo(bo), chan, true, ctx->interruptible);
822 ret = drm->ttm.move(chan, bo, bo->resource, new_reg);
824 ret = nouveau_fence_new(chan, false, &fence);
826 /* TODO: figure out a better solution here
828 * wait on the fence here explicitly as going through
829 * ttm_bo_move_accel_cleanup somehow doesn't seem to do it.
831 * Without this the operation can timeout and we'll fallback to a
832 * software copy, which might take several minutes to finish.
834 nouveau_fence_wait(fence, false, false);
835 ret = ttm_bo_move_accel_cleanup(bo,
839 nouveau_fence_unref(&fence);
843 mutex_unlock(&cli->mutex);
848 nouveau_bo_move_init(struct nouveau_drm *drm)
850 static const struct _method_table {
854 int (*exec)(struct nouveau_channel *,
855 struct ttm_buffer_object *,
856 struct ttm_resource *, struct ttm_resource *);
857 int (*init)(struct nouveau_channel *, u32 handle);
859 { "COPY", 4, 0xc7b5, nve0_bo_move_copy, nve0_bo_move_init },
860 { "GRCE", 0, 0xc7b5, nve0_bo_move_copy, nvc0_bo_move_init },
861 { "COPY", 4, 0xc6b5, nve0_bo_move_copy, nve0_bo_move_init },
862 { "GRCE", 0, 0xc6b5, nve0_bo_move_copy, nvc0_bo_move_init },
863 { "COPY", 4, 0xc5b5, nve0_bo_move_copy, nve0_bo_move_init },
864 { "GRCE", 0, 0xc5b5, nve0_bo_move_copy, nvc0_bo_move_init },
865 { "COPY", 4, 0xc3b5, nve0_bo_move_copy, nve0_bo_move_init },
866 { "GRCE", 0, 0xc3b5, nve0_bo_move_copy, nvc0_bo_move_init },
867 { "COPY", 4, 0xc1b5, nve0_bo_move_copy, nve0_bo_move_init },
868 { "GRCE", 0, 0xc1b5, nve0_bo_move_copy, nvc0_bo_move_init },
869 { "COPY", 4, 0xc0b5, nve0_bo_move_copy, nve0_bo_move_init },
870 { "GRCE", 0, 0xc0b5, nve0_bo_move_copy, nvc0_bo_move_init },
871 { "COPY", 4, 0xb0b5, nve0_bo_move_copy, nve0_bo_move_init },
872 { "GRCE", 0, 0xb0b5, nve0_bo_move_copy, nvc0_bo_move_init },
873 { "COPY", 4, 0xa0b5, nve0_bo_move_copy, nve0_bo_move_init },
874 { "GRCE", 0, 0xa0b5, nve0_bo_move_copy, nvc0_bo_move_init },
875 { "COPY1", 5, 0x90b8, nvc0_bo_move_copy, nvc0_bo_move_init },
876 { "COPY0", 4, 0x90b5, nvc0_bo_move_copy, nvc0_bo_move_init },
877 { "COPY", 0, 0x85b5, nva3_bo_move_copy, nv50_bo_move_init },
878 { "CRYPT", 0, 0x74c1, nv84_bo_move_exec, nv50_bo_move_init },
879 { "M2MF", 0, 0x9039, nvc0_bo_move_m2mf, nvc0_bo_move_init },
880 { "M2MF", 0, 0x5039, nv50_bo_move_m2mf, nv50_bo_move_init },
881 { "M2MF", 0, 0x0039, nv04_bo_move_m2mf, nv04_bo_move_init },
884 const struct _method_table *mthd = _methods;
885 const char *name = "CPU";
889 struct nouveau_channel *chan;
898 ret = nvif_object_ctor(&chan->user, "ttmBoMove",
899 mthd->oclass | (mthd->engine << 16),
900 mthd->oclass, NULL, 0,
903 ret = mthd->init(chan, drm->ttm.copy.handle);
905 nvif_object_dtor(&drm->ttm.copy);
909 drm->ttm.move = mthd->exec;
910 drm->ttm.chan = chan;
914 } while ((++mthd)->exec);
916 NV_INFO(drm, "MM: using %s for buffer copies\n", name);
919 static void nouveau_bo_move_ntfy(struct ttm_buffer_object *bo,
920 struct ttm_resource *new_reg)
922 struct nouveau_mem *mem = new_reg ? nouveau_mem(new_reg) : NULL;
923 struct nouveau_bo *nvbo = nouveau_bo(bo);
924 struct nouveau_vma *vma;
927 /* ttm can now (stupidly) pass the driver bos it didn't create... */
928 if (bo->destroy != nouveau_bo_del_ttm)
931 nouveau_bo_del_io_reserve_lru(bo);
933 if (mem && new_reg->mem_type != TTM_PL_SYSTEM &&
934 mem->mem.page == nvbo->page) {
935 list_for_each_entry(vma, &nvbo->vma_list, head) {
936 nouveau_vma_map(vma, mem);
939 list_for_each_entry(vma, &nvbo->vma_list, head) {
940 ret = dma_resv_wait_timeout(bo->base.resv,
941 DMA_RESV_USAGE_BOOKKEEP,
944 nouveau_vma_unmap(vma);
949 nvbo->offset = (new_reg->start << PAGE_SHIFT);
954 nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_resource *new_reg,
955 struct nouveau_drm_tile **new_tile)
957 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
958 struct drm_device *dev = drm->dev;
959 struct nouveau_bo *nvbo = nouveau_bo(bo);
960 u64 offset = new_reg->start << PAGE_SHIFT;
963 if (new_reg->mem_type != TTM_PL_VRAM)
966 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_CELSIUS) {
967 *new_tile = nv10_bo_set_tiling(dev, offset, bo->base.size,
968 nvbo->mode, nvbo->zeta);
975 nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo,
976 struct nouveau_drm_tile *new_tile,
977 struct nouveau_drm_tile **old_tile)
979 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
980 struct drm_device *dev = drm->dev;
981 struct dma_fence *fence;
984 ret = dma_resv_get_singleton(bo->base.resv, DMA_RESV_USAGE_WRITE,
987 dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_WRITE,
988 false, MAX_SCHEDULE_TIMEOUT);
990 nv10_bo_put_tile_region(dev, *old_tile, fence);
991 *old_tile = new_tile;
995 nouveau_bo_move(struct ttm_buffer_object *bo, bool evict,
996 struct ttm_operation_ctx *ctx,
997 struct ttm_resource *new_reg,
998 struct ttm_place *hop)
1000 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
1001 struct nouveau_bo *nvbo = nouveau_bo(bo);
1002 struct ttm_resource *old_reg = bo->resource;
1003 struct nouveau_drm_tile *new_tile = NULL;
1007 if (new_reg->mem_type == TTM_PL_TT) {
1008 ret = nouveau_ttm_tt_bind(bo->bdev, bo->ttm, new_reg);
1013 nouveau_bo_move_ntfy(bo, new_reg);
1014 ret = ttm_bo_wait_ctx(bo, ctx);
1018 if (nvbo->bo.pin_count)
1019 NV_WARN(drm, "Moving pinned object %p!\n", nvbo);
1021 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
1022 ret = nouveau_bo_vm_bind(bo, new_reg, &new_tile);
1028 if (!old_reg || (old_reg->mem_type == TTM_PL_SYSTEM &&
1030 ttm_bo_move_null(bo, new_reg);
1034 if (old_reg->mem_type == TTM_PL_SYSTEM &&
1035 new_reg->mem_type == TTM_PL_TT) {
1036 ttm_bo_move_null(bo, new_reg);
1040 if (old_reg->mem_type == TTM_PL_TT &&
1041 new_reg->mem_type == TTM_PL_SYSTEM) {
1042 nouveau_ttm_tt_unbind(bo->bdev, bo->ttm);
1043 ttm_resource_free(bo, &bo->resource);
1044 ttm_bo_assign_mem(bo, new_reg);
1048 /* Hardware assisted copy. */
1049 if (drm->ttm.move) {
1050 if ((old_reg->mem_type == TTM_PL_SYSTEM &&
1051 new_reg->mem_type == TTM_PL_VRAM) ||
1052 (old_reg->mem_type == TTM_PL_VRAM &&
1053 new_reg->mem_type == TTM_PL_SYSTEM)) {
1056 hop->mem_type = TTM_PL_TT;
1060 ret = nouveau_bo_move_m2mf(bo, evict, ctx,
1066 /* Fallback to software copy. */
1067 ret = ttm_bo_move_memcpy(bo, ctx, new_reg);
1071 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
1073 nouveau_bo_vm_cleanup(bo, NULL, &new_tile);
1075 nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile);
1079 nouveau_bo_move_ntfy(bo, bo->resource);
1085 nouveau_ttm_io_mem_free_locked(struct nouveau_drm *drm,
1086 struct ttm_resource *reg)
1088 struct nouveau_mem *mem = nouveau_mem(reg);
1090 if (drm->client.mem->oclass >= NVIF_CLASS_MEM_NV50) {
1091 switch (reg->mem_type) {
1094 nvif_object_unmap_handle(&mem->mem.object);
1097 nvif_object_unmap_handle(&mem->mem.object);
1106 nouveau_ttm_io_mem_reserve(struct ttm_device *bdev, struct ttm_resource *reg)
1108 struct nouveau_drm *drm = nouveau_bdev(bdev);
1109 struct nvkm_device *device = nvxx_device(&drm->client.device);
1110 struct nouveau_mem *mem = nouveau_mem(reg);
1111 struct nvif_mmu *mmu = &drm->client.mmu;
1114 mutex_lock(&drm->ttm.io_reserve_mutex);
1116 switch (reg->mem_type) {
1122 #if IS_ENABLED(CONFIG_AGP)
1123 if (drm->agp.bridge) {
1124 reg->bus.offset = (reg->start << PAGE_SHIFT) +
1126 reg->bus.is_iomem = !drm->agp.cma;
1127 reg->bus.caching = ttm_write_combined;
1130 if (drm->client.mem->oclass < NVIF_CLASS_MEM_NV50 ||
1136 fallthrough; /* tiled memory */
1138 reg->bus.offset = (reg->start << PAGE_SHIFT) +
1139 device->func->resource_addr(device, 1);
1140 reg->bus.is_iomem = true;
1142 /* Some BARs do not support being ioremapped WC */
1143 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA &&
1144 mmu->type[drm->ttm.type_vram].type & NVIF_MEM_UNCACHED)
1145 reg->bus.caching = ttm_uncached;
1147 reg->bus.caching = ttm_write_combined;
1149 if (drm->client.mem->oclass >= NVIF_CLASS_MEM_NV50) {
1151 struct nv50_mem_map_v0 nv50;
1152 struct gf100_mem_map_v0 gf100;
1157 switch (mem->mem.object.oclass) {
1158 case NVIF_CLASS_MEM_NV50:
1159 args.nv50.version = 0;
1161 args.nv50.kind = mem->kind;
1162 args.nv50.comp = mem->comp;
1163 argc = sizeof(args.nv50);
1165 case NVIF_CLASS_MEM_GF100:
1166 args.gf100.version = 0;
1168 args.gf100.kind = mem->kind;
1169 argc = sizeof(args.gf100);
1176 ret = nvif_object_map_handle(&mem->mem.object,
1180 if (WARN_ON(ret == 0))
1185 reg->bus.offset = handle;
1194 if (ret == -ENOSPC) {
1195 struct nouveau_bo *nvbo;
1197 nvbo = list_first_entry_or_null(&drm->ttm.io_reserve_lru,
1201 list_del_init(&nvbo->io_reserve_lru);
1202 drm_vma_node_unmap(&nvbo->bo.base.vma_node,
1204 nouveau_ttm_io_mem_free_locked(drm, nvbo->bo.resource);
1209 mutex_unlock(&drm->ttm.io_reserve_mutex);
1214 nouveau_ttm_io_mem_free(struct ttm_device *bdev, struct ttm_resource *reg)
1216 struct nouveau_drm *drm = nouveau_bdev(bdev);
1218 mutex_lock(&drm->ttm.io_reserve_mutex);
1219 nouveau_ttm_io_mem_free_locked(drm, reg);
1220 mutex_unlock(&drm->ttm.io_reserve_mutex);
1223 vm_fault_t nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
1225 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
1226 struct nouveau_bo *nvbo = nouveau_bo(bo);
1227 struct nvkm_device *device = nvxx_device(&drm->client.device);
1228 u32 mappable = device->func->resource_size(device, 1) >> PAGE_SHIFT;
1231 /* as long as the bo isn't in vram, and isn't tiled, we've got
1232 * nothing to do here.
1234 if (bo->resource->mem_type != TTM_PL_VRAM) {
1235 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA ||
1239 if (bo->resource->mem_type != TTM_PL_SYSTEM)
1242 nouveau_bo_placement_set(nvbo, NOUVEAU_GEM_DOMAIN_GART, 0);
1245 /* make sure bo is in mappable vram */
1246 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA ||
1247 bo->resource->start + PFN_UP(bo->resource->size) < mappable)
1250 for (i = 0; i < nvbo->placement.num_placement; ++i) {
1251 nvbo->placements[i].fpfn = 0;
1252 nvbo->placements[i].lpfn = mappable;
1255 for (i = 0; i < nvbo->placement.num_busy_placement; ++i) {
1256 nvbo->busy_placements[i].fpfn = 0;
1257 nvbo->busy_placements[i].lpfn = mappable;
1260 nouveau_bo_placement_set(nvbo, NOUVEAU_GEM_DOMAIN_VRAM, 0);
1263 ret = nouveau_bo_validate(nvbo, false, false);
1264 if (unlikely(ret == -EBUSY || ret == -ERESTARTSYS))
1265 return VM_FAULT_NOPAGE;
1266 else if (unlikely(ret))
1267 return VM_FAULT_SIGBUS;
1269 ttm_bo_move_to_lru_tail_unlocked(bo);
1274 nouveau_ttm_tt_populate(struct ttm_device *bdev,
1275 struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
1277 struct ttm_tt *ttm_dma = (void *)ttm;
1278 struct nouveau_drm *drm;
1279 bool slave = !!(ttm->page_flags & TTM_TT_FLAG_EXTERNAL);
1281 if (ttm_tt_is_populated(ttm))
1284 if (slave && ttm->sg) {
1285 drm_prime_sg_to_dma_addr_array(ttm->sg, ttm_dma->dma_address,
1290 drm = nouveau_bdev(bdev);
1292 return ttm_pool_alloc(&drm->ttm.bdev.pool, ttm, ctx);
1296 nouveau_ttm_tt_unpopulate(struct ttm_device *bdev,
1299 struct nouveau_drm *drm;
1300 bool slave = !!(ttm->page_flags & TTM_TT_FLAG_EXTERNAL);
1305 nouveau_ttm_tt_unbind(bdev, ttm);
1307 drm = nouveau_bdev(bdev);
1309 return ttm_pool_free(&drm->ttm.bdev.pool, ttm);
1313 nouveau_ttm_tt_destroy(struct ttm_device *bdev,
1316 #if IS_ENABLED(CONFIG_AGP)
1317 struct nouveau_drm *drm = nouveau_bdev(bdev);
1318 if (drm->agp.bridge) {
1319 ttm_agp_destroy(ttm);
1323 nouveau_sgdma_destroy(bdev, ttm);
1327 nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence, bool exclusive)
1329 struct dma_resv *resv = nvbo->bo.base.resv;
1334 dma_resv_add_fence(resv, &fence->base, exclusive ?
1335 DMA_RESV_USAGE_WRITE : DMA_RESV_USAGE_READ);
1339 nouveau_bo_delete_mem_notify(struct ttm_buffer_object *bo)
1341 nouveau_bo_move_ntfy(bo, NULL);
1344 struct ttm_device_funcs nouveau_bo_driver = {
1345 .ttm_tt_create = &nouveau_ttm_tt_create,
1346 .ttm_tt_populate = &nouveau_ttm_tt_populate,
1347 .ttm_tt_unpopulate = &nouveau_ttm_tt_unpopulate,
1348 .ttm_tt_destroy = &nouveau_ttm_tt_destroy,
1349 .eviction_valuable = ttm_bo_eviction_valuable,
1350 .evict_flags = nouveau_bo_evict_flags,
1351 .delete_mem_notify = nouveau_bo_delete_mem_notify,
1352 .move = nouveau_bo_move,
1353 .io_mem_reserve = &nouveau_ttm_io_mem_reserve,
1354 .io_mem_free = &nouveau_ttm_io_mem_free,