drm/ttm: use caching instead of placement for ttm_io_prot
[linux-2.6-block.git] / drivers / gpu / drm / nouveau / nouveau_bo.c
CommitLineData
6ee73861
BS
1/*
2 * Copyright 2007 Dave Airlied
3 * All Rights Reserved.
4 *
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:
11 *
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
14 * Software.
15 *
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.
23 */
24/*
25 * Authors: Dave Airlied <airlied@linux.ie>
26 * Ben Skeggs <darktama@iinet.net.au>
27 * Jeremy Kolb <jkolb@brandeis.edu>
28 */
29
fdb751ef 30#include <linux/dma-mapping.h>
3e2b756b 31#include <linux/swiotlb.h>
6ee73861 32
4dc28134 33#include "nouveau_drv.h"
8b9d5d63 34#include "nouveau_chan.h"
d375e7d5 35#include "nouveau_fence.h"
6ee73861 36
ebb945a9
BS
37#include "nouveau_bo.h"
38#include "nouveau_ttm.h"
39#include "nouveau_gem.h"
9ce523cc 40#include "nouveau_mem.h"
24e8375b 41#include "nouveau_vmm.h"
a510604d 42
d7722134
BS
43#include <nvif/class.h>
44#include <nvif/if500b.h>
45#include <nvif/if900b.h>
46
cae515f4
DA
47static int nouveau_ttm_tt_bind(struct ttm_bo_device *bdev, struct ttm_tt *ttm,
48 struct ttm_resource *reg);
49
bc9e7b9a
BS
50/*
51 * NV10-NV40 tiling helpers
52 */
53
54static void
ebb945a9
BS
55nv10_bo_update_tile_region(struct drm_device *dev, struct nouveau_drm_tile *reg,
56 u32 addr, u32 size, u32 pitch, u32 flags)
bc9e7b9a 57{
77145f1c 58 struct nouveau_drm *drm = nouveau_drm(dev);
ebb945a9 59 int i = reg - drm->tile.reg;
359088d5 60 struct nvkm_fb *fb = nvxx_fb(&drm->client.device);
b1e4553c 61 struct nvkm_fb_tile *tile = &fb->tile.region[i];
bc9e7b9a 62
ebb945a9 63 nouveau_fence_unref(&reg->fence);
bc9e7b9a
BS
64
65 if (tile->pitch)
03c8952f 66 nvkm_fb_tile_fini(fb, i, tile);
bc9e7b9a
BS
67
68 if (pitch)
03c8952f 69 nvkm_fb_tile_init(fb, i, addr, size, pitch, flags, tile);
bc9e7b9a 70
03c8952f 71 nvkm_fb_tile_prog(fb, i, tile);
bc9e7b9a
BS
72}
73
ebb945a9 74static struct nouveau_drm_tile *
bc9e7b9a
BS
75nv10_bo_get_tile_region(struct drm_device *dev, int i)
76{
77145f1c 77 struct nouveau_drm *drm = nouveau_drm(dev);
ebb945a9 78 struct nouveau_drm_tile *tile = &drm->tile.reg[i];
bc9e7b9a 79
ebb945a9 80 spin_lock(&drm->tile.lock);
bc9e7b9a
BS
81
82 if (!tile->used &&
83 (!tile->fence || nouveau_fence_done(tile->fence)))
84 tile->used = true;
85 else
86 tile = NULL;
87
ebb945a9 88 spin_unlock(&drm->tile.lock);
bc9e7b9a
BS
89 return tile;
90}
91
92static void
ebb945a9 93nv10_bo_put_tile_region(struct drm_device *dev, struct nouveau_drm_tile *tile,
f54d1867 94 struct dma_fence *fence)
bc9e7b9a 95{
77145f1c 96 struct nouveau_drm *drm = nouveau_drm(dev);
bc9e7b9a
BS
97
98 if (tile) {
ebb945a9 99 spin_lock(&drm->tile.lock);
f54d1867 100 tile->fence = (struct nouveau_fence *)dma_fence_get(fence);
bc9e7b9a 101 tile->used = false;
ebb945a9 102 spin_unlock(&drm->tile.lock);
bc9e7b9a
BS
103 }
104}
105
ebb945a9
BS
106static struct nouveau_drm_tile *
107nv10_bo_set_tiling(struct drm_device *dev, u32 addr,
7760a2e3 108 u32 size, u32 pitch, u32 zeta)
bc9e7b9a 109{
77145f1c 110 struct nouveau_drm *drm = nouveau_drm(dev);
1167c6bc 111 struct nvkm_fb *fb = nvxx_fb(&drm->client.device);
ebb945a9 112 struct nouveau_drm_tile *tile, *found = NULL;
bc9e7b9a
BS
113 int i;
114
b1e4553c 115 for (i = 0; i < fb->tile.regions; i++) {
bc9e7b9a
BS
116 tile = nv10_bo_get_tile_region(dev, i);
117
118 if (pitch && !found) {
119 found = tile;
120 continue;
121
b1e4553c 122 } else if (tile && fb->tile.region[i].pitch) {
bc9e7b9a
BS
123 /* Kill an unused tile region. */
124 nv10_bo_update_tile_region(dev, tile, 0, 0, 0, 0);
125 }
126
127 nv10_bo_put_tile_region(dev, tile, NULL);
128 }
129
130 if (found)
7760a2e3 131 nv10_bo_update_tile_region(dev, found, addr, size, pitch, zeta);
bc9e7b9a
BS
132 return found;
133}
134
6ee73861
BS
135static void
136nouveau_bo_del_ttm(struct ttm_buffer_object *bo)
137{
ebb945a9
BS
138 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
139 struct drm_device *dev = drm->dev;
6ee73861
BS
140 struct nouveau_bo *nvbo = nouveau_bo(bo);
141
6797cea1 142 WARN_ON(nvbo->bo.pin_count > 0);
141b15e5 143 nouveau_bo_del_io_reserve_lru(bo);
bc9e7b9a 144 nv10_bo_put_tile_region(dev, nvbo->tile, NULL);
641f53c0
TR
145
146 /*
147 * If nouveau_bo_new() allocated this buffer, the GEM object was never
148 * initialized, so don't attempt to release it.
149 */
150 if (bo->base.dev)
151 drm_gem_object_release(&bo->base);
152
6ee73861
BS
153 kfree(nvbo);
154}
155
4d8b3d34
BS
156static inline u64
157roundup_64(u64 x, u32 y)
158{
159 x += y - 1;
160 do_div(x, y);
161 return x * y;
162}
163
a0af9add 164static void
81b61579 165nouveau_bo_fixup_align(struct nouveau_bo *nvbo, int *align, u64 *size)
a0af9add 166{
ebb945a9 167 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
1167c6bc 168 struct nvif_device *device = &drm->client.device;
a0af9add 169
967e7bde 170 if (device->info.family < NV_DEVICE_INFO_V0_TESLA) {
7760a2e3 171 if (nvbo->mode) {
967e7bde 172 if (device->info.chipset >= 0x40) {
a0af9add 173 *align = 65536;
7760a2e3 174 *size = roundup_64(*size, 64 * nvbo->mode);
a0af9add 175
967e7bde 176 } else if (device->info.chipset >= 0x30) {
a0af9add 177 *align = 32768;
7760a2e3 178 *size = roundup_64(*size, 64 * nvbo->mode);
a0af9add 179
967e7bde 180 } else if (device->info.chipset >= 0x20) {
a0af9add 181 *align = 16384;
7760a2e3 182 *size = roundup_64(*size, 64 * nvbo->mode);
a0af9add 183
967e7bde 184 } else if (device->info.chipset >= 0x10) {
a0af9add 185 *align = 16384;
7760a2e3 186 *size = roundup_64(*size, 32 * nvbo->mode);
a0af9add
FJ
187 }
188 }
bfd83aca 189 } else {
7760a2e3
BS
190 *size = roundup_64(*size, (1 << nvbo->page));
191 *align = max((1 << nvbo->page), *align);
a0af9add
FJ
192 }
193
4d8b3d34 194 *size = roundup_64(*size, PAGE_SIZE);
a0af9add
FJ
195}
196
019cbd4a 197struct nouveau_bo *
81b61579 198nouveau_bo_alloc(struct nouveau_cli *cli, u64 *size, int *align, u32 domain,
9ca7f796 199 u32 tile_mode, u32 tile_flags)
6ee73861 200{
e75c091b 201 struct nouveau_drm *drm = cli->drm;
6ee73861 202 struct nouveau_bo *nvbo;
a220dd73 203 struct nvif_mmu *mmu = &cli->mmu;
bfe91afa 204 struct nvif_vmm *vmm = cli->svm.cli ? &cli->svm.vmm : &cli->vmm.vmm;
019cbd4a 205 int i, pi = -1;
0108bc80 206
9ca7f796
TR
207 if (!*size) {
208 NV_WARN(drm, "skipped size %016llx\n", *size);
019cbd4a 209 return ERR_PTR(-EINVAL);
0108bc80 210 }
22b33e8e 211
6ee73861
BS
212 nvbo = kzalloc(sizeof(struct nouveau_bo), GFP_KERNEL);
213 if (!nvbo)
019cbd4a 214 return ERR_PTR(-ENOMEM);
6ee73861
BS
215 INIT_LIST_HEAD(&nvbo->head);
216 INIT_LIST_HEAD(&nvbo->entry);
fd2871af 217 INIT_LIST_HEAD(&nvbo->vma_list);
ebb945a9 218 nvbo->bo.bdev = &drm->ttm.bdev;
6ee73861 219
acb16cfa
BS
220 /* This is confusing, and doesn't actually mean we want an uncached
221 * mapping, but is what NOUVEAU_GEM_DOMAIN_COHERENT gets translated
222 * into in nouveau_gem_new().
223 */
81b61579 224 if (domain & NOUVEAU_GEM_DOMAIN_COHERENT) {
acb16cfa
BS
225 /* Determine if we can get a cache-coherent map, forcing
226 * uncached mapping if we can't.
227 */
74a39954 228 if (!nouveau_drm_use_coherent_gpu_mapping(drm))
acb16cfa
BS
229 nvbo->force_coherent = true;
230 }
c3a0c771 231
7760a2e3
BS
232 if (cli->device.info.family >= NV_DEVICE_INFO_V0_FERMI) {
233 nvbo->kind = (tile_flags & 0x0000ff00) >> 8;
a220dd73
BS
234 if (!nvif_mmu_kind_valid(mmu, nvbo->kind)) {
235 kfree(nvbo);
019cbd4a 236 return ERR_PTR(-EINVAL);
a220dd73
BS
237 }
238
239 nvbo->comp = mmu->kind[nvbo->kind] != nvbo->kind;
7760a2e3
BS
240 } else
241 if (cli->device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
242 nvbo->kind = (tile_flags & 0x00007f00) >> 8;
243 nvbo->comp = (tile_flags & 0x00030000) >> 16;
a220dd73
BS
244 if (!nvif_mmu_kind_valid(mmu, nvbo->kind)) {
245 kfree(nvbo);
019cbd4a 246 return ERR_PTR(-EINVAL);
a220dd73 247 }
7760a2e3
BS
248 } else {
249 nvbo->zeta = (tile_flags & 0x00000007);
250 }
251 nvbo->mode = tile_mode;
252 nvbo->contig = !(tile_flags & NOUVEAU_GEM_TILE_NONCONTIG);
253
7dc6a446
BS
254 /* Determine the desirable target GPU page size for the buffer. */
255 for (i = 0; i < vmm->page_nr; i++) {
256 /* Because we cannot currently allow VMM maps to fail
257 * during buffer migration, we need to determine page
258 * size for the buffer up-front, and pre-allocate its
259 * page tables.
260 *
261 * Skip page sizes that can't support needed domains.
262 */
263 if (cli->device.info.family > NV_DEVICE_INFO_V0_CURIE &&
81b61579 264 (domain & NOUVEAU_GEM_DOMAIN_VRAM) && !vmm->page[i].vram)
7dc6a446 265 continue;
81b61579 266 if ((domain & NOUVEAU_GEM_DOMAIN_GART) &&
f29f18eb 267 (!vmm->page[i].host || vmm->page[i].shift > PAGE_SHIFT))
7dc6a446
BS
268 continue;
269
270 /* Select this page size if it's the first that supports
271 * the potential memory domains, or when it's compatible
272 * with the requested compression settings.
273 */
274 if (pi < 0 || !nvbo->comp || vmm->page[i].comp)
275 pi = i;
276
277 /* Stop once the buffer is larger than the current page size. */
9ca7f796 278 if (*size >= 1ULL << vmm->page[i].shift)
7dc6a446
BS
279 break;
280 }
281
282 if (WARN_ON(pi < 0))
019cbd4a 283 return ERR_PTR(-EINVAL);
7dc6a446
BS
284
285 /* Disable compression if suitable settings couldn't be found. */
286 if (nvbo->comp && !vmm->page[pi].comp) {
287 if (mmu->object.oclass >= NVIF_CLASS_MMU_GF100)
288 nvbo->kind = mmu->kind[nvbo->kind];
289 nvbo->comp = 0;
f91bac5b 290 }
7dc6a446 291 nvbo->page = vmm->page[pi].shift;
f91bac5b 292
81b61579 293 nouveau_bo_fixup_align(nvbo, align, size);
9ca7f796 294
019cbd4a
TR
295 return nvbo;
296}
297
298int
81b61579 299nouveau_bo_init(struct nouveau_bo *nvbo, u64 size, int align, u32 domain,
019cbd4a
TR
300 struct sg_table *sg, struct dma_resv *robj)
301{
302 int type = sg ? ttm_bo_type_sg : ttm_bo_type_device;
303 size_t acc_size;
304 int ret;
305
306 acc_size = ttm_bo_dma_acc_size(nvbo->bo.bdev, size, sizeof(*nvbo));
307
fd2871af 308 nvbo->bo.mem.num_pages = size >> PAGE_SHIFT;
81b61579 309 nouveau_bo_placement_set(nvbo, domain, 0);
141b15e5 310 INIT_LIST_HEAD(&nvbo->io_reserve_lru);
6ee73861 311
019cbd4a
TR
312 ret = ttm_bo_init(nvbo->bo.bdev, &nvbo->bo, size, type,
313 &nvbo->placement, align >> PAGE_SHIFT, false,
314 acc_size, sg, robj, nouveau_bo_del_ttm);
6ee73861
BS
315 if (ret) {
316 /* ttm will call nouveau_bo_del_ttm if it fails.. */
317 return ret;
318 }
319
019cbd4a
TR
320 return 0;
321}
322
323int
324nouveau_bo_new(struct nouveau_cli *cli, u64 size, int align,
81b61579 325 uint32_t domain, uint32_t tile_mode, uint32_t tile_flags,
019cbd4a
TR
326 struct sg_table *sg, struct dma_resv *robj,
327 struct nouveau_bo **pnvbo)
328{
329 struct nouveau_bo *nvbo;
330 int ret;
331
81b61579 332 nvbo = nouveau_bo_alloc(cli, &size, &align, domain, tile_mode,
9ca7f796 333 tile_flags);
019cbd4a
TR
334 if (IS_ERR(nvbo))
335 return PTR_ERR(nvbo);
336
81b61579 337 ret = nouveau_bo_init(nvbo, size, align, domain, sg, robj);
019cbd4a
TR
338 if (ret)
339 return ret;
340
6ee73861
BS
341 *pnvbo = nvbo;
342 return 0;
343}
344
78ad0f7b 345static void
5839172f
CK
346set_placement_list(struct nouveau_drm *drm, struct ttm_place *pl, unsigned *n,
347 uint32_t domain, uint32_t flags)
78ad0f7b
FJ
348{
349 *n = 0;
350
48e07c23 351 if (domain & NOUVEAU_GEM_DOMAIN_VRAM) {
5839172f
CK
352 struct nvif_mmu *mmu = &drm->client.mmu;
353 const u8 type = mmu->type[drm->ttm.type_vram].type;
354
48e07c23 355 pl[*n].mem_type = TTM_PL_VRAM;
5839172f
CK
356 pl[*n].flags = flags & ~TTM_PL_FLAG_CACHED;
357
358 /* Some BARs do not support being ioremapped WC */
359 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA &&
360 type & NVIF_MEM_UNCACHED)
361 pl[*n].flags &= ~TTM_PL_FLAG_WC;
362
363 (*n)++;
48e07c23
CK
364 }
365 if (domain & NOUVEAU_GEM_DOMAIN_GART) {
366 pl[*n].mem_type = TTM_PL_TT;
5839172f
CK
367 pl[*n].flags = flags;
368
369 if (drm->agp.bridge)
370 pl[*n].flags &= ~TTM_PL_FLAG_CACHED;
371
372 (*n)++;
48e07c23
CK
373 }
374 if (domain & NOUVEAU_GEM_DOMAIN_CPU) {
375 pl[*n].mem_type = TTM_PL_SYSTEM;
376 pl[(*n)++].flags = flags;
377 }
78ad0f7b
FJ
378}
379
699ddfd9 380static void
81b61579 381set_placement_range(struct nouveau_bo *nvbo, uint32_t domain)
699ddfd9 382{
ebb945a9 383 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
1167c6bc 384 u32 vram_pages = drm->client.device.info.ram_size >> PAGE_SHIFT;
f1217ed0 385 unsigned i, fpfn, lpfn;
699ddfd9 386
1167c6bc 387 if (drm->client.device.info.family == NV_DEVICE_INFO_V0_CELSIUS &&
81b61579 388 nvbo->mode && (domain & NOUVEAU_GEM_DOMAIN_VRAM) &&
4beb116a 389 nvbo->bo.mem.num_pages < vram_pages / 4) {
699ddfd9
FJ
390 /*
391 * Make sure that the color and depth buffers are handled
392 * by independent memory controller units. Up to a 9x
393 * speed up when alpha-blending and depth-test are enabled
394 * at the same time.
395 */
7760a2e3 396 if (nvbo->zeta) {
f1217ed0
CK
397 fpfn = vram_pages / 2;
398 lpfn = ~0;
699ddfd9 399 } else {
f1217ed0
CK
400 fpfn = 0;
401 lpfn = vram_pages / 2;
402 }
403 for (i = 0; i < nvbo->placement.num_placement; ++i) {
404 nvbo->placements[i].fpfn = fpfn;
405 nvbo->placements[i].lpfn = lpfn;
406 }
407 for (i = 0; i < nvbo->placement.num_busy_placement; ++i) {
408 nvbo->busy_placements[i].fpfn = fpfn;
409 nvbo->busy_placements[i].lpfn = lpfn;
699ddfd9
FJ
410 }
411 }
412}
413
6ee73861 414void
81b61579
CK
415nouveau_bo_placement_set(struct nouveau_bo *nvbo, uint32_t domain,
416 uint32_t busy)
6ee73861 417{
5839172f 418 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
78ad0f7b 419 struct ttm_placement *pl = &nvbo->placement;
6797cea1
CK
420 uint32_t flags = nvbo->force_coherent ? TTM_PL_FLAG_UNCACHED :
421 TTM_PL_MASK_CACHING;
78ad0f7b
FJ
422
423 pl->placement = nvbo->placements;
5839172f 424 set_placement_list(drm, nvbo->placements, &pl->num_placement,
81b61579 425 domain, flags);
78ad0f7b
FJ
426
427 pl->busy_placement = nvbo->busy_placements;
5839172f 428 set_placement_list(drm, nvbo->busy_placements, &pl->num_busy_placement,
81b61579 429 domain | busy, flags);
699ddfd9 430
81b61579 431 set_placement_range(nvbo, domain);
6ee73861
BS
432}
433
434int
81b61579 435nouveau_bo_pin(struct nouveau_bo *nvbo, uint32_t domain, bool contig)
6ee73861 436{
ebb945a9 437 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
6ee73861 438 struct ttm_buffer_object *bo = &nvbo->bo;
ad76b3f7 439 bool force = false, evict = false;
78ad0f7b 440 int ret;
6ee73861 441
dfd5e50e 442 ret = ttm_bo_reserve(bo, false, false, NULL);
0ae6d7bc 443 if (ret)
50ab2e52 444 return ret;
0ae6d7bc 445
1167c6bc 446 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA &&
81b61579 447 domain == NOUVEAU_GEM_DOMAIN_VRAM && contig) {
7760a2e3
BS
448 if (!nvbo->contig) {
449 nvbo->contig = true;
ad76b3f7 450 force = true;
7760a2e3 451 evict = true;
ad76b3f7 452 }
6ee73861
BS
453 }
454
6797cea1 455 if (nvbo->bo.pin_count) {
81b61579
CK
456 bool error = evict;
457
458 switch (bo->mem.mem_type) {
459 case TTM_PL_VRAM:
460 error |= !(domain & NOUVEAU_GEM_DOMAIN_VRAM);
461 break;
462 case TTM_PL_TT:
463 error |= !(domain & NOUVEAU_GEM_DOMAIN_GART);
464 default:
465 break;
466 }
467
468 if (error) {
ad76b3f7
BS
469 NV_ERROR(drm, "bo %p pinned elsewhere: "
470 "0x%08x vs 0x%08x\n", bo,
81b61579 471 bo->mem.mem_type, domain);
ad76b3f7
BS
472 ret = -EBUSY;
473 }
6797cea1 474 ttm_bo_pin(&nvbo->bo);
50ab2e52 475 goto out;
ad76b3f7
BS
476 }
477
478 if (evict) {
81b61579 479 nouveau_bo_placement_set(nvbo, NOUVEAU_GEM_DOMAIN_GART, 0);
ad76b3f7
BS
480 ret = nouveau_bo_validate(nvbo, false, false);
481 if (ret)
482 goto out;
483 }
6ee73861 484
81b61579 485 nouveau_bo_placement_set(nvbo, domain, 0);
97a875cb 486 ret = nouveau_bo_validate(nvbo, false, false);
6aac6ced
BS
487 if (ret)
488 goto out;
6797cea1
CK
489
490 ttm_bo_pin(&nvbo->bo);
6aac6ced
BS
491
492 switch (bo->mem.mem_type) {
493 case TTM_PL_VRAM:
494 drm->gem.vram_available -= bo->mem.size;
495 break;
496 case TTM_PL_TT:
497 drm->gem.gart_available -= bo->mem.size;
498 break;
499 default:
500 break;
6ee73861 501 }
5be5a15a 502
6ee73861 503out:
ad76b3f7 504 if (force && ret)
7760a2e3 505 nvbo->contig = false;
0ae6d7bc 506 ttm_bo_unreserve(bo);
6ee73861
BS
507 return ret;
508}
509
510int
511nouveau_bo_unpin(struct nouveau_bo *nvbo)
512{
ebb945a9 513 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
6ee73861 514 struct ttm_buffer_object *bo = &nvbo->bo;
6797cea1 515 int ret;
6ee73861 516
dfd5e50e 517 ret = ttm_bo_reserve(bo, false, false, NULL);
6ee73861
BS
518 if (ret)
519 return ret;
520
6797cea1
CK
521 ttm_bo_unpin(&nvbo->bo);
522 if (!nvbo->bo.pin_count) {
6ee73861
BS
523 switch (bo->mem.mem_type) {
524 case TTM_PL_VRAM:
ebb945a9 525 drm->gem.vram_available += bo->mem.size;
6ee73861
BS
526 break;
527 case TTM_PL_TT:
ebb945a9 528 drm->gem.gart_available += bo->mem.size;
6ee73861
BS
529 break;
530 default:
531 break;
532 }
533 }
534
535 ttm_bo_unreserve(bo);
6797cea1 536 return 0;
6ee73861
BS
537}
538
539int
540nouveau_bo_map(struct nouveau_bo *nvbo)
541{
542 int ret;
543
dfd5e50e 544 ret = ttm_bo_reserve(&nvbo->bo, false, false, NULL);
6ee73861
BS
545 if (ret)
546 return ret;
547
36a471ba 548 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages, &nvbo->kmap);
c3a0c771 549
6ee73861
BS
550 ttm_bo_unreserve(&nvbo->bo);
551 return ret;
552}
553
554void
555nouveau_bo_unmap(struct nouveau_bo *nvbo)
556{
c3a0c771
AC
557 if (!nvbo)
558 return;
559
36a471ba 560 ttm_bo_kunmap(&nvbo->kmap);
6ee73861
BS
561}
562
b22870ba
AC
563void
564nouveau_bo_sync_for_device(struct nouveau_bo *nvbo)
565{
566 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
b22870ba
AC
567 struct ttm_dma_tt *ttm_dma = (struct ttm_dma_tt *)nvbo->bo.ttm;
568 int i;
569
570 if (!ttm_dma)
571 return;
572
573 /* Don't waste time looping if the object is coherent */
574 if (nvbo->force_coherent)
575 return;
576
577 for (i = 0; i < ttm_dma->ttm.num_pages; i++)
359088d5
BS
578 dma_sync_single_for_device(drm->dev->dev,
579 ttm_dma->dma_address[i],
26c9e8ef 580 PAGE_SIZE, DMA_TO_DEVICE);
b22870ba
AC
581}
582
583void
584nouveau_bo_sync_for_cpu(struct nouveau_bo *nvbo)
585{
586 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
b22870ba
AC
587 struct ttm_dma_tt *ttm_dma = (struct ttm_dma_tt *)nvbo->bo.ttm;
588 int i;
589
590 if (!ttm_dma)
591 return;
592
593 /* Don't waste time looping if the object is coherent */
594 if (nvbo->force_coherent)
595 return;
596
597 for (i = 0; i < ttm_dma->ttm.num_pages; i++)
359088d5 598 dma_sync_single_for_cpu(drm->dev->dev, ttm_dma->dma_address[i],
26c9e8ef 599 PAGE_SIZE, DMA_FROM_DEVICE);
b22870ba
AC
600}
601
141b15e5
CK
602void nouveau_bo_add_io_reserve_lru(struct ttm_buffer_object *bo)
603{
604 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
605 struct nouveau_bo *nvbo = nouveau_bo(bo);
606
607 mutex_lock(&drm->ttm.io_reserve_mutex);
608 list_move_tail(&nvbo->io_reserve_lru, &drm->ttm.io_reserve_lru);
609 mutex_unlock(&drm->ttm.io_reserve_mutex);
610}
611
612void nouveau_bo_del_io_reserve_lru(struct ttm_buffer_object *bo)
613{
614 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
615 struct nouveau_bo *nvbo = nouveau_bo(bo);
616
617 mutex_lock(&drm->ttm.io_reserve_mutex);
618 list_del_init(&nvbo->io_reserve_lru);
619 mutex_unlock(&drm->ttm.io_reserve_mutex);
620}
621
7a45d764
BS
622int
623nouveau_bo_validate(struct nouveau_bo *nvbo, bool interruptible,
97a875cb 624 bool no_wait_gpu)
7a45d764 625{
19be5570 626 struct ttm_operation_ctx ctx = { interruptible, no_wait_gpu };
7a45d764
BS
627 int ret;
628
19be5570 629 ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement, &ctx);
7a45d764
BS
630 if (ret)
631 return ret;
632
b22870ba
AC
633 nouveau_bo_sync_for_device(nvbo);
634
7a45d764
BS
635 return 0;
636}
637
6ee73861
BS
638void
639nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val)
640{
641 bool is_iomem;
642 u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
c3a0c771 643
36a471ba 644 mem += index;
c3a0c771 645
6ee73861
BS
646 if (is_iomem)
647 iowrite16_native(val, (void __force __iomem *)mem);
648 else
649 *mem = val;
650}
651
652u32
653nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index)
654{
655 bool is_iomem;
656 u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
c3a0c771 657
36a471ba 658 mem += index;
c3a0c771 659
6ee73861
BS
660 if (is_iomem)
661 return ioread32_native((void __force __iomem *)mem);
662 else
663 return *mem;
664}
665
666void
667nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val)
668{
669 bool is_iomem;
670 u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
c3a0c771 671
36a471ba 672 mem += index;
c3a0c771 673
6ee73861
BS
674 if (is_iomem)
675 iowrite32_native(val, (void __force __iomem *)mem);
676 else
677 *mem = val;
678}
679
649bf3ca 680static struct ttm_tt *
dde5da23 681nouveau_ttm_tt_create(struct ttm_buffer_object *bo, uint32_t page_flags)
6ee73861 682{
a7fb8a23 683#if IS_ENABLED(CONFIG_AGP)
dde5da23 684 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
6ee73861 685
340b0e7c 686 if (drm->agp.bridge) {
dde5da23 687 return ttm_agp_tt_create(bo, drm->agp.bridge, page_flags);
6ee73861 688 }
df1b4b91 689#endif
6ee73861 690
dde5da23 691 return nouveau_sgdma_create_ttm(bo, page_flags);
6ee73861
BS
692}
693
8635784a
DA
694static int
695nouveau_ttm_tt_bind(struct ttm_bo_device *bdev, struct ttm_tt *ttm,
696 struct ttm_resource *reg)
697{
698#if IS_ENABLED(CONFIG_AGP)
699 struct nouveau_drm *drm = nouveau_bdev(bdev);
0b988ca1
DA
700#endif
701 if (!reg)
702 return -EINVAL;
703#if IS_ENABLED(CONFIG_AGP)
8635784a 704 if (drm->agp.bridge)
48efa57e 705 return ttm_agp_bind(ttm, reg);
8635784a
DA
706#endif
707 return nouveau_sgdma_bind(bdev, ttm, reg);
708}
709
710static void
711nouveau_ttm_tt_unbind(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
712{
713#if IS_ENABLED(CONFIG_AGP)
714 struct nouveau_drm *drm = nouveau_bdev(bdev);
715
716 if (drm->agp.bridge) {
48efa57e 717 ttm_agp_unbind(ttm);
8635784a
DA
718 return;
719 }
720#endif
721 nouveau_sgdma_unbind(bdev, ttm);
722}
723
6ee73861
BS
724static void
725nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
726{
727 struct nouveau_bo *nvbo = nouveau_bo(bo);
728
729 switch (bo->mem.mem_type) {
22fbd538 730 case TTM_PL_VRAM:
81b61579
CK
731 nouveau_bo_placement_set(nvbo, NOUVEAU_GEM_DOMAIN_GART,
732 NOUVEAU_GEM_DOMAIN_CPU);
22fbd538 733 break;
6ee73861 734 default:
81b61579 735 nouveau_bo_placement_set(nvbo, NOUVEAU_GEM_DOMAIN_CPU, 0);
6ee73861
BS
736 break;
737 }
22fbd538
FJ
738
739 *pl = nvbo->placement;
6ee73861
BS
740}
741
d2f96666 742static int
3c57d85d 743nouveau_bo_move_prep(struct nouveau_drm *drm, struct ttm_buffer_object *bo,
2966141a 744 struct ttm_resource *reg)
d2f96666 745{
9ce523cc
BS
746 struct nouveau_mem *old_mem = nouveau_mem(&bo->mem);
747 struct nouveau_mem *new_mem = nouveau_mem(reg);
d7722134 748 struct nvif_vmm *vmm = &drm->client.vmm.vmm;
d2f96666
BS
749 int ret;
750
d7722134
BS
751 ret = nvif_vmm_get(vmm, LAZY, false, old_mem->mem.page, 0,
752 old_mem->mem.size, &old_mem->vma[0]);
d2f96666
BS
753 if (ret)
754 return ret;
755
d7722134
BS
756 ret = nvif_vmm_get(vmm, LAZY, false, new_mem->mem.page, 0,
757 new_mem->mem.size, &old_mem->vma[1]);
758 if (ret)
759 goto done;
3c57d85d 760
9ce523cc
BS
761 ret = nouveau_mem_map(old_mem, vmm, &old_mem->vma[0]);
762 if (ret)
763 goto done;
764
765 ret = nouveau_mem_map(new_mem, vmm, &old_mem->vma[1]);
766done:
767 if (ret) {
d7722134
BS
768 nvif_vmm_put(vmm, &old_mem->vma[1]);
769 nvif_vmm_put(vmm, &old_mem->vma[0]);
9ce523cc 770 }
d2f96666
BS
771 return 0;
772}
773
f1ab0cc9 774static int
54661867
DA
775nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict,
776 struct ttm_operation_ctx *ctx,
777 struct ttm_resource *new_reg)
f1ab0cc9 778{
ebb945a9 779 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
1934a2ad 780 struct nouveau_channel *chan = drm->ttm.chan;
a01ca78c 781 struct nouveau_cli *cli = (void *)chan->user.client;
35b8141b 782 struct nouveau_fence *fence;
f1ab0cc9
BS
783 int ret;
784
d2f96666 785 /* create temporary vmas for the transfer and attach them to the
be83cd4e 786 * old nvkm_mem node, these will get cleaned up after ttm has
2966141a 787 * destroyed the ttm_resource
3425df48 788 */
1167c6bc 789 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
605f9ccd 790 ret = nouveau_bo_move_prep(drm, bo, new_reg);
d2f96666 791 if (ret)
3c57d85d 792 return ret;
3425df48
BS
793 }
794
0ad72863 795 mutex_lock_nested(&cli->mutex, SINGLE_DEPTH_NESTING);
54661867 796 ret = nouveau_fence_sync(nouveau_bo(bo), chan, true, ctx->interruptible);
6a6b73f2 797 if (ret == 0) {
605f9ccd 798 ret = drm->ttm.move(chan, bo, &bo->mem, new_reg);
35b8141b
BS
799 if (ret == 0) {
800 ret = nouveau_fence_new(chan, false, &fence);
801 if (ret == 0) {
f2c24b83
ML
802 ret = ttm_bo_move_accel_cleanup(bo,
803 &fence->base,
e46f468f 804 evict, false,
605f9ccd 805 new_reg);
35b8141b
BS
806 nouveau_fence_unref(&fence);
807 }
808 }
6a6b73f2 809 }
0ad72863 810 mutex_unlock(&cli->mutex);
6a6b73f2 811 return ret;
6ee73861
BS
812}
813
d1b167e1 814void
49981046 815nouveau_bo_move_init(struct nouveau_drm *drm)
d1b167e1 816{
72ecb0a6 817 static const struct _method_table {
d1b167e1 818 const char *name;
1a46098e 819 int engine;
315a8b2e 820 s32 oclass;
d1b167e1
BS
821 int (*exec)(struct nouveau_channel *,
822 struct ttm_buffer_object *,
2966141a 823 struct ttm_resource *, struct ttm_resource *);
d1b167e1
BS
824 int (*init)(struct nouveau_channel *, u32 handle);
825 } _methods[] = {
c36322d2
BS
826 { "COPY", 4, 0xc5b5, nve0_bo_move_copy, nve0_bo_move_init },
827 { "GRCE", 0, 0xc5b5, nve0_bo_move_copy, nvc0_bo_move_init },
6e1f34e3
BS
828 { "COPY", 4, 0xc3b5, nve0_bo_move_copy, nve0_bo_move_init },
829 { "GRCE", 0, 0xc3b5, nve0_bo_move_copy, nvc0_bo_move_init },
146cfe24
BS
830 { "COPY", 4, 0xc1b5, nve0_bo_move_copy, nve0_bo_move_init },
831 { "GRCE", 0, 0xc1b5, nve0_bo_move_copy, nvc0_bo_move_init },
8e7e1586
BS
832 { "COPY", 4, 0xc0b5, nve0_bo_move_copy, nve0_bo_move_init },
833 { "GRCE", 0, 0xc0b5, nve0_bo_move_copy, nvc0_bo_move_init },
990b4547
BS
834 { "COPY", 4, 0xb0b5, nve0_bo_move_copy, nve0_bo_move_init },
835 { "GRCE", 0, 0xb0b5, nve0_bo_move_copy, nvc0_bo_move_init },
00fc6f6f 836 { "COPY", 4, 0xa0b5, nve0_bo_move_copy, nve0_bo_move_init },
49981046 837 { "GRCE", 0, 0xa0b5, nve0_bo_move_copy, nvc0_bo_move_init },
1a46098e
BS
838 { "COPY1", 5, 0x90b8, nvc0_bo_move_copy, nvc0_bo_move_init },
839 { "COPY0", 4, 0x90b5, nvc0_bo_move_copy, nvc0_bo_move_init },
840 { "COPY", 0, 0x85b5, nva3_bo_move_copy, nv50_bo_move_init },
841 { "CRYPT", 0, 0x74c1, nv84_bo_move_exec, nv50_bo_move_init },
842 { "M2MF", 0, 0x9039, nvc0_bo_move_m2mf, nvc0_bo_move_init },
843 { "M2MF", 0, 0x5039, nv50_bo_move_m2mf, nv50_bo_move_init },
844 { "M2MF", 0, 0x0039, nv04_bo_move_m2mf, nv04_bo_move_init },
5490e5df 845 {},
72ecb0a6
JJ
846 };
847 const struct _method_table *mthd = _methods;
d1b167e1
BS
848 const char *name = "CPU";
849 int ret;
850
851 do {
49981046 852 struct nouveau_channel *chan;
ebb945a9 853
00fc6f6f 854 if (mthd->engine)
49981046
BS
855 chan = drm->cechan;
856 else
857 chan = drm->channel;
858 if (chan == NULL)
859 continue;
860
9ac596a4 861 ret = nvif_object_ctor(&chan->user, "ttmBoMove",
0ad72863
BS
862 mthd->oclass | (mthd->engine << 16),
863 mthd->oclass, NULL, 0,
864 &drm->ttm.copy);
d1b167e1 865 if (ret == 0) {
0ad72863 866 ret = mthd->init(chan, drm->ttm.copy.handle);
ebb945a9 867 if (ret) {
9ac596a4 868 nvif_object_dtor(&drm->ttm.copy);
ebb945a9 869 continue;
d1b167e1 870 }
ebb945a9
BS
871
872 drm->ttm.move = mthd->exec;
1bb3f6a2 873 drm->ttm.chan = chan;
ebb945a9
BS
874 name = mthd->name;
875 break;
d1b167e1
BS
876 }
877 } while ((++mthd)->exec);
878
ebb945a9 879 NV_INFO(drm, "MM: using %s for buffer copies\n", name);
d1b167e1
BS
880}
881
6ee73861 882static int
54661867
DA
883nouveau_bo_move_flipd(struct ttm_buffer_object *bo, bool evict,
884 struct ttm_operation_ctx *ctx,
885 struct ttm_resource *new_reg)
6ee73861 886{
f1217ed0
CK
887 struct ttm_place placement_memtype = {
888 .fpfn = 0,
889 .lpfn = 0,
48e07c23
CK
890 .mem_type = TTM_PL_TT,
891 .flags = TTM_PL_MASK_CACHING
f1217ed0 892 };
6ee73861 893 struct ttm_placement placement;
2966141a 894 struct ttm_resource tmp_reg;
6ee73861
BS
895 int ret;
896
6ee73861 897 placement.num_placement = placement.num_busy_placement = 1;
77e2b5ed 898 placement.placement = placement.busy_placement = &placement_memtype;
6ee73861 899
605f9ccd
BS
900 tmp_reg = *new_reg;
901 tmp_reg.mm_node = NULL;
54661867 902 ret = ttm_bo_mem_space(bo, &placement, &tmp_reg, ctx);
6ee73861
BS
903 if (ret)
904 return ret;
905
54661867 906 ret = ttm_tt_populate(bo->bdev, bo->ttm, ctx);
2040ec97
DA
907 if (ret)
908 goto out;
909
cae515f4 910 ret = nouveau_ttm_tt_bind(bo->bdev, bo->ttm, &tmp_reg);
6ee73861
BS
911 if (ret)
912 goto out;
913
54661867 914 ret = nouveau_bo_move_m2mf(bo, true, ctx, &tmp_reg);
6ee73861
BS
915 if (ret)
916 goto out;
917
54661867 918 ret = ttm_bo_move_ttm(bo, ctx, new_reg);
6ee73861 919out:
b2458726 920 ttm_resource_free(bo, &tmp_reg);
6ee73861
BS
921 return ret;
922}
923
924static int
54661867
DA
925nouveau_bo_move_flips(struct ttm_buffer_object *bo, bool evict,
926 struct ttm_operation_ctx *ctx,
927 struct ttm_resource *new_reg)
6ee73861 928{
f1217ed0
CK
929 struct ttm_place placement_memtype = {
930 .fpfn = 0,
931 .lpfn = 0,
48e07c23
CK
932 .mem_type = TTM_PL_TT,
933 .flags = TTM_PL_MASK_CACHING
f1217ed0 934 };
6ee73861 935 struct ttm_placement placement;
2966141a 936 struct ttm_resource tmp_reg;
6ee73861
BS
937 int ret;
938
6ee73861 939 placement.num_placement = placement.num_busy_placement = 1;
77e2b5ed 940 placement.placement = placement.busy_placement = &placement_memtype;
6ee73861 941
605f9ccd
BS
942 tmp_reg = *new_reg;
943 tmp_reg.mm_node = NULL;
54661867 944 ret = ttm_bo_mem_space(bo, &placement, &tmp_reg, ctx);
6ee73861
BS
945 if (ret)
946 return ret;
947
54661867 948 ret = ttm_bo_move_ttm(bo, ctx, &tmp_reg);
6ee73861
BS
949 if (ret)
950 goto out;
951
54661867 952 ret = nouveau_bo_move_m2mf(bo, true, ctx, new_reg);
6ee73861
BS
953 if (ret)
954 goto out;
955
956out:
b2458726 957 ttm_resource_free(bo, &tmp_reg);
6ee73861
BS
958 return ret;
959}
960
a4154bbf 961static void
66257db7 962nouveau_bo_move_ntfy(struct ttm_buffer_object *bo, bool evict,
2966141a 963 struct ttm_resource *new_reg)
a4154bbf 964{
9ce523cc 965 struct nouveau_mem *mem = new_reg ? nouveau_mem(new_reg) : NULL;
a4154bbf 966 struct nouveau_bo *nvbo = nouveau_bo(bo);
24e8375b 967 struct nouveau_vma *vma;
fd2871af 968
9f1feed2
BS
969 /* ttm can now (stupidly) pass the driver bos it didn't create... */
970 if (bo->destroy != nouveau_bo_del_ttm)
971 return;
972
141b15e5
CK
973 nouveau_bo_del_io_reserve_lru(bo);
974
a48296ab 975 if (mem && new_reg->mem_type != TTM_PL_SYSTEM &&
9ce523cc 976 mem->mem.page == nvbo->page) {
a48296ab 977 list_for_each_entry(vma, &nvbo->vma_list, head) {
24e8375b 978 nouveau_vma_map(vma, mem);
a48296ab
BS
979 }
980 } else {
981 list_for_each_entry(vma, &nvbo->vma_list, head) {
10dcab3e 982 WARN_ON(ttm_bo_wait(bo, false, false));
24e8375b 983 nouveau_vma_unmap(vma);
fd2871af 984 }
a4154bbf 985 }
0dc9b286
ND
986
987 if (new_reg) {
988 if (new_reg->mm_node)
989 nvbo->offset = (new_reg->start << PAGE_SHIFT);
990 else
991 nvbo->offset = 0;
992 }
993
a4154bbf
BS
994}
995
6ee73861 996static int
2966141a 997nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_resource *new_reg,
ebb945a9 998 struct nouveau_drm_tile **new_tile)
6ee73861 999{
ebb945a9
BS
1000 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
1001 struct drm_device *dev = drm->dev;
a0af9add 1002 struct nouveau_bo *nvbo = nouveau_bo(bo);
605f9ccd 1003 u64 offset = new_reg->start << PAGE_SHIFT;
6ee73861 1004
a4154bbf 1005 *new_tile = NULL;
605f9ccd 1006 if (new_reg->mem_type != TTM_PL_VRAM)
a0af9add 1007 return 0;
a0af9add 1008
1167c6bc 1009 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_CELSIUS) {
605f9ccd 1010 *new_tile = nv10_bo_set_tiling(dev, offset, new_reg->size,
7760a2e3 1011 nvbo->mode, nvbo->zeta);
6ee73861
BS
1012 }
1013
a0af9add
FJ
1014 return 0;
1015}
1016
1017static void
1018nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo,
ebb945a9
BS
1019 struct nouveau_drm_tile *new_tile,
1020 struct nouveau_drm_tile **old_tile)
a0af9add 1021{
ebb945a9
BS
1022 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
1023 struct drm_device *dev = drm->dev;
52791eee 1024 struct dma_fence *fence = dma_resv_get_excl(bo->base.resv);
a0af9add 1025
f2c24b83 1026 nv10_bo_put_tile_region(dev, *old_tile, fence);
a4154bbf 1027 *old_tile = new_tile;
a0af9add
FJ
1028}
1029
1030static int
2823f4f0
CK
1031nouveau_bo_move(struct ttm_buffer_object *bo, bool evict,
1032 struct ttm_operation_ctx *ctx,
2966141a 1033 struct ttm_resource *new_reg)
a0af9add 1034{
ebb945a9 1035 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
a0af9add 1036 struct nouveau_bo *nvbo = nouveau_bo(bo);
2966141a 1037 struct ttm_resource *old_reg = &bo->mem;
ebb945a9 1038 struct nouveau_drm_tile *new_tile = NULL;
a0af9add
FJ
1039 int ret = 0;
1040
0ef1ed81 1041 ret = ttm_bo_wait_ctx(bo, ctx);
88932a7b
CK
1042 if (ret)
1043 return ret;
1044
6797cea1 1045 if (nvbo->bo.pin_count)
5be5a15a
AC
1046 NV_WARN(drm, "Moving pinned object %p!\n", nvbo);
1047
1167c6bc 1048 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
605f9ccd 1049 ret = nouveau_bo_vm_bind(bo, new_reg, &new_tile);
a4154bbf
BS
1050 if (ret)
1051 return ret;
1052 }
a0af9add 1053
a0af9add 1054 /* Fake bo copy. */
605f9ccd 1055 if (old_reg->mem_type == TTM_PL_SYSTEM && !bo->ttm) {
ecfe6953 1056 ttm_bo_move_null(bo, new_reg);
a0af9add 1057 goto out;
6ee73861
BS
1058 }
1059
12b68474
DA
1060 if (old_reg->mem_type == TTM_PL_SYSTEM &&
1061 new_reg->mem_type == TTM_PL_TT) {
1062 ttm_bo_move_null(bo, new_reg);
1063 goto out;
1064 }
1065
1066 if (old_reg->mem_type == TTM_PL_TT &&
1067 new_reg->mem_type == TTM_PL_SYSTEM) {
1068 ret = ttm_bo_move_ttm(bo, ctx, new_reg);
1069 goto out;
1070 }
1071
a0af9add 1072 /* Hardware assisted copy. */
cef9e99e 1073 if (drm->ttm.move) {
605f9ccd 1074 if (new_reg->mem_type == TTM_PL_SYSTEM)
54661867
DA
1075 ret = nouveau_bo_move_flipd(bo, evict, ctx,
1076 new_reg);
605f9ccd 1077 else if (old_reg->mem_type == TTM_PL_SYSTEM)
54661867
DA
1078 ret = nouveau_bo_move_flips(bo, evict, ctx,
1079 new_reg);
cef9e99e 1080 else
54661867
DA
1081 ret = nouveau_bo_move_m2mf(bo, evict, ctx,
1082 new_reg);
cef9e99e
BS
1083 if (!ret)
1084 goto out;
1085 }
a0af9add
FJ
1086
1087 /* Fallback to software copy. */
8117713d 1088 ret = ttm_bo_move_memcpy(bo, ctx, new_reg);
a0af9add
FJ
1089
1090out:
1167c6bc 1091 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
a4154bbf
BS
1092 if (ret)
1093 nouveau_bo_vm_cleanup(bo, NULL, &new_tile);
1094 else
1095 nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile);
1096 }
a0af9add
FJ
1097
1098 return ret;
6ee73861
BS
1099}
1100
1101static int
1102nouveau_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp)
1103{
acb46527
DH
1104 struct nouveau_bo *nvbo = nouveau_bo(bo);
1105
f8659be8 1106 return drm_vma_node_verify_access(&nvbo->bo.base.vma_node,
d9a1f0b4 1107 filp->private_data);
6ee73861
BS
1108}
1109
141b15e5
CK
1110static void
1111nouveau_ttm_io_mem_free_locked(struct nouveau_drm *drm,
1112 struct ttm_resource *reg)
1113{
1114 struct nouveau_mem *mem = nouveau_mem(reg);
1115
1116 if (drm->client.mem->oclass >= NVIF_CLASS_MEM_NV50) {
1117 switch (reg->mem_type) {
1118 case TTM_PL_TT:
1119 if (mem->kind)
1120 nvif_object_unmap_handle(&mem->mem.object);
1121 break;
1122 case TTM_PL_VRAM:
1123 nvif_object_unmap_handle(&mem->mem.object);
1124 break;
1125 default:
1126 break;
1127 }
1128 }
1129}
1130
f32f02fd 1131static int
2966141a 1132nouveau_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_resource *reg)
f32f02fd 1133{
ebb945a9 1134 struct nouveau_drm *drm = nouveau_bdev(bdev);
1167c6bc 1135 struct nvkm_device *device = nvxx_device(&drm->client.device);
9ce523cc 1136 struct nouveau_mem *mem = nouveau_mem(reg);
1cf65c45
CK
1137 struct nvif_mmu *mmu = &drm->client.mmu;
1138 const u8 type = mmu->type[drm->ttm.type_vram].type;
141b15e5 1139 int ret;
f32f02fd 1140
141b15e5
CK
1141 mutex_lock(&drm->ttm.io_reserve_mutex);
1142retry:
605f9ccd 1143 switch (reg->mem_type) {
f32f02fd
JG
1144 case TTM_PL_SYSTEM:
1145 /* System memory */
141b15e5
CK
1146 ret = 0;
1147 goto out;
f32f02fd 1148 case TTM_PL_TT:
a7fb8a23 1149#if IS_ENABLED(CONFIG_AGP)
340b0e7c 1150 if (drm->agp.bridge) {
54d04ea8
CK
1151 reg->bus.offset = (reg->start << PAGE_SHIFT) +
1152 drm->agp.base;
605f9ccd 1153 reg->bus.is_iomem = !drm->agp.cma;
1cf65c45 1154 reg->bus.caching = ttm_write_combined;
f32f02fd
JG
1155 }
1156#endif
141b15e5
CK
1157 if (drm->client.mem->oclass < NVIF_CLASS_MEM_NV50 ||
1158 !mem->kind) {
a5540906 1159 /* untiled */
141b15e5 1160 ret = 0;
a5540906 1161 break;
141b15e5 1162 }
f6e7393e 1163 fallthrough; /* tiled memory */
f32f02fd 1164 case TTM_PL_VRAM:
54d04ea8
CK
1165 reg->bus.offset = (reg->start << PAGE_SHIFT) +
1166 device->func->resource_addr(device, 1);
605f9ccd 1167 reg->bus.is_iomem = true;
1cf65c45
CK
1168
1169 /* Some BARs do not support being ioremapped WC */
1170 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA &&
1171 type & NVIF_MEM_UNCACHED)
1172 reg->bus.caching = ttm_uncached;
1173 else
1174 reg->bus.caching = ttm_write_combined;
1175
d7722134
BS
1176 if (drm->client.mem->oclass >= NVIF_CLASS_MEM_NV50) {
1177 union {
1178 struct nv50_mem_map_v0 nv50;
1179 struct gf100_mem_map_v0 gf100;
1180 } args;
1181 u64 handle, length;
1182 u32 argc = 0;
d7722134
BS
1183
1184 switch (mem->mem.object.oclass) {
1185 case NVIF_CLASS_MEM_NV50:
1186 args.nv50.version = 0;
1187 args.nv50.ro = 0;
1188 args.nv50.kind = mem->kind;
1189 args.nv50.comp = mem->comp;
b554b12a 1190 argc = sizeof(args.nv50);
d7722134
BS
1191 break;
1192 case NVIF_CLASS_MEM_GF100:
1193 args.gf100.version = 0;
1194 args.gf100.ro = 0;
1195 args.gf100.kind = mem->kind;
b554b12a 1196 argc = sizeof(args.gf100);
d7722134
BS
1197 break;
1198 default:
1199 WARN_ON(1);
1200 break;
1201 }
1202
1203 ret = nvif_object_map_handle(&mem->mem.object,
b554b12a 1204 &args, argc,
d7722134 1205 &handle, &length);
8a39db76
BS
1206 if (ret != 1) {
1207 if (WARN_ON(ret == 0))
141b15e5
CK
1208 ret = -EINVAL;
1209 goto out;
8a39db76 1210 }
d7722134 1211
d7722134 1212 reg->bus.offset = handle;
141b15e5 1213 ret = 0;
f869ef88 1214 }
f32f02fd
JG
1215 break;
1216 default:
141b15e5 1217 ret = -EINVAL;
f32f02fd 1218 }
141b15e5
CK
1219
1220out:
1221 if (ret == -ENOSPC) {
1222 struct nouveau_bo *nvbo;
1223
1224 nvbo = list_first_entry_or_null(&drm->ttm.io_reserve_lru,
1225 typeof(*nvbo),
1226 io_reserve_lru);
1227 if (nvbo) {
1228 list_del_init(&nvbo->io_reserve_lru);
1229 drm_vma_node_unmap(&nvbo->bo.base.vma_node,
1230 bdev->dev_mapping);
1231 nouveau_ttm_io_mem_free_locked(drm, &nvbo->bo.mem);
1232 goto retry;
1233 }
1234
1235 }
1236 mutex_unlock(&drm->ttm.io_reserve_mutex);
1237 return ret;
f32f02fd
JG
1238}
1239
1240static void
2966141a 1241nouveau_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_resource *reg)
f32f02fd 1242{
d7722134 1243 struct nouveau_drm *drm = nouveau_bdev(bdev);
f869ef88 1244
141b15e5
CK
1245 mutex_lock(&drm->ttm.io_reserve_mutex);
1246 nouveau_ttm_io_mem_free_locked(drm, reg);
1247 mutex_unlock(&drm->ttm.io_reserve_mutex);
f32f02fd
JG
1248}
1249
76fe313a 1250vm_fault_t nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
f32f02fd 1251{
ebb945a9 1252 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
e1429b4c 1253 struct nouveau_bo *nvbo = nouveau_bo(bo);
1167c6bc 1254 struct nvkm_device *device = nvxx_device(&drm->client.device);
7e8820fe 1255 u32 mappable = device->func->resource_size(device, 1) >> PAGE_SHIFT;
f1217ed0 1256 int i, ret;
e1429b4c
BS
1257
1258 /* as long as the bo isn't in vram, and isn't tiled, we've got
1259 * nothing to do here.
1260 */
1261 if (bo->mem.mem_type != TTM_PL_VRAM) {
1167c6bc 1262 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA ||
7760a2e3 1263 !nvbo->kind)
e1429b4c 1264 return 0;
a5540906 1265
76fe313a
CK
1266 if (bo->mem.mem_type != TTM_PL_SYSTEM)
1267 return 0;
1268
1269 nouveau_bo_placement_set(nvbo, NOUVEAU_GEM_DOMAIN_GART, 0);
1270
1271 } else {
1272 /* make sure bo is in mappable vram */
1273 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA ||
1274 bo->mem.start + bo->mem.num_pages < mappable)
1275 return 0;
a5540906 1276
76fe313a
CK
1277 for (i = 0; i < nvbo->placement.num_placement; ++i) {
1278 nvbo->placements[i].fpfn = 0;
1279 nvbo->placements[i].lpfn = mappable;
a5540906 1280 }
e1429b4c 1281
76fe313a
CK
1282 for (i = 0; i < nvbo->placement.num_busy_placement; ++i) {
1283 nvbo->busy_placements[i].fpfn = 0;
1284 nvbo->busy_placements[i].lpfn = mappable;
1285 }
e1429b4c 1286
76fe313a 1287 nouveau_bo_placement_set(nvbo, NOUVEAU_GEM_DOMAIN_VRAM, 0);
f1217ed0
CK
1288 }
1289
76fe313a
CK
1290 ret = nouveau_bo_validate(nvbo, false, false);
1291 if (unlikely(ret == -EBUSY || ret == -ERESTARTSYS))
1292 return VM_FAULT_NOPAGE;
1293 else if (unlikely(ret))
1294 return VM_FAULT_SIGBUS;
e1429b4c 1295
76fe313a
CK
1296 ttm_bo_move_to_lru_tail_unlocked(bo);
1297 return 0;
f32f02fd
JG
1298}
1299
3230cfc3 1300static int
0a667b50
DA
1301nouveau_ttm_tt_populate(struct ttm_bo_device *bdev,
1302 struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
3230cfc3 1303{
8e7e7052 1304 struct ttm_dma_tt *ttm_dma = (void *)ttm;
ebb945a9 1305 struct nouveau_drm *drm;
359088d5 1306 struct device *dev;
22b33e8e 1307 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
3230cfc3 1308
7eec9151 1309 if (ttm_tt_is_populated(ttm))
3230cfc3
KRW
1310 return 0;
1311
22b33e8e
DA
1312 if (slave && ttm->sg) {
1313 /* make userspace faulting work */
1314 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
1315 ttm_dma->dma_address, ttm->num_pages);
7eec9151 1316 ttm_tt_set_populated(ttm);
22b33e8e
DA
1317 return 0;
1318 }
1319
0a667b50 1320 drm = nouveau_bdev(bdev);
359088d5 1321 dev = drm->dev->dev;
3230cfc3 1322
a7fb8a23 1323#if IS_ENABLED(CONFIG_AGP)
340b0e7c 1324 if (drm->agp.bridge) {
43482554 1325 return ttm_pool_populate(ttm, ctx);
dea7e0ac
JG
1326 }
1327#endif
1328
9bcd38de 1329#if IS_ENABLED(CONFIG_SWIOTLB) && IS_ENABLED(CONFIG_X86)
3230cfc3 1330 if (swiotlb_nr_tbl()) {
d0cef9fa 1331 return ttm_dma_populate((void *)ttm, dev, ctx);
3230cfc3
KRW
1332 }
1333#endif
bd549d35 1334 return ttm_populate_and_map_pages(dev, ttm_dma, ctx);
3230cfc3
KRW
1335}
1336
1337static void
0a667b50
DA
1338nouveau_ttm_tt_unpopulate(struct ttm_bo_device *bdev,
1339 struct ttm_tt *ttm)
3230cfc3 1340{
8e7e7052 1341 struct ttm_dma_tt *ttm_dma = (void *)ttm;
ebb945a9 1342 struct nouveau_drm *drm;
359088d5 1343 struct device *dev;
22b33e8e
DA
1344 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
1345
1346 if (slave)
1347 return;
3230cfc3 1348
0a667b50 1349 drm = nouveau_bdev(bdev);
359088d5 1350 dev = drm->dev->dev;
3230cfc3 1351
a7fb8a23 1352#if IS_ENABLED(CONFIG_AGP)
340b0e7c 1353 if (drm->agp.bridge) {
43482554 1354 ttm_pool_unpopulate(ttm);
dea7e0ac
JG
1355 return;
1356 }
1357#endif
1358
9bcd38de 1359#if IS_ENABLED(CONFIG_SWIOTLB) && IS_ENABLED(CONFIG_X86)
3230cfc3 1360 if (swiotlb_nr_tbl()) {
359088d5 1361 ttm_dma_unpopulate((void *)ttm, dev);
3230cfc3
KRW
1362 return;
1363 }
1364#endif
1365
bd549d35 1366 ttm_unmap_and_unpopulate_pages(dev, ttm_dma);
3230cfc3
KRW
1367}
1368
8635784a
DA
1369static void
1370nouveau_ttm_tt_destroy(struct ttm_bo_device *bdev,
1371 struct ttm_tt *ttm)
1372{
1373#if IS_ENABLED(CONFIG_AGP)
1374 struct nouveau_drm *drm = nouveau_bdev(bdev);
1375 if (drm->agp.bridge) {
37bff654 1376 ttm_agp_unbind(ttm);
7626168f 1377 ttm_tt_destroy_common(bdev, ttm);
48efa57e 1378 ttm_agp_destroy(ttm);
8635784a
DA
1379 return;
1380 }
1381#endif
1382 nouveau_sgdma_destroy(bdev, ttm);
1383}
1384
875ac34a 1385void
809e9447 1386nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence, bool exclusive)
875ac34a 1387{
52791eee 1388 struct dma_resv *resv = nvbo->bo.base.resv;
bdaf7ddf 1389
809e9447 1390 if (exclusive)
52791eee 1391 dma_resv_add_excl_fence(resv, &fence->base);
809e9447 1392 else if (fence)
52791eee 1393 dma_resv_add_shared_fence(resv, &fence->base);
875ac34a
BS
1394}
1395
6ee73861 1396struct ttm_bo_driver nouveau_bo_driver = {
649bf3ca 1397 .ttm_tt_create = &nouveau_ttm_tt_create,
3230cfc3
KRW
1398 .ttm_tt_populate = &nouveau_ttm_tt_populate,
1399 .ttm_tt_unpopulate = &nouveau_ttm_tt_unpopulate,
8635784a
DA
1400 .ttm_tt_bind = &nouveau_ttm_tt_bind,
1401 .ttm_tt_unbind = &nouveau_ttm_tt_unbind,
1402 .ttm_tt_destroy = &nouveau_ttm_tt_destroy,
a2ab19fe 1403 .eviction_valuable = ttm_bo_eviction_valuable,
6ee73861 1404 .evict_flags = nouveau_bo_evict_flags,
a4154bbf 1405 .move_notify = nouveau_bo_move_ntfy,
6ee73861
BS
1406 .move = nouveau_bo_move,
1407 .verify_access = nouveau_bo_verify_access,
f32f02fd
JG
1408 .io_mem_reserve = &nouveau_ttm_io_mem_reserve,
1409 .io_mem_free = &nouveau_ttm_io_mem_free,
6ee73861 1410};