drm/ttm: use new move interface for known system->ttm moves
[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
ce65b874 346set_placement_list(struct ttm_place *pl, unsigned *n, uint32_t domain)
78ad0f7b
FJ
347{
348 *n = 0;
349
48e07c23
CK
350 if (domain & NOUVEAU_GEM_DOMAIN_VRAM) {
351 pl[*n].mem_type = TTM_PL_VRAM;
ce65b874 352 pl[*n].flags = 0;
5839172f 353 (*n)++;
48e07c23
CK
354 }
355 if (domain & NOUVEAU_GEM_DOMAIN_GART) {
356 pl[*n].mem_type = TTM_PL_TT;
ce65b874 357 pl[*n].flags = 0;
5839172f 358 (*n)++;
48e07c23
CK
359 }
360 if (domain & NOUVEAU_GEM_DOMAIN_CPU) {
361 pl[*n].mem_type = TTM_PL_SYSTEM;
ce65b874 362 pl[(*n)++].flags = 0;
48e07c23 363 }
78ad0f7b
FJ
364}
365
699ddfd9 366static void
81b61579 367set_placement_range(struct nouveau_bo *nvbo, uint32_t domain)
699ddfd9 368{
ebb945a9 369 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
1167c6bc 370 u32 vram_pages = drm->client.device.info.ram_size >> PAGE_SHIFT;
f1217ed0 371 unsigned i, fpfn, lpfn;
699ddfd9 372
1167c6bc 373 if (drm->client.device.info.family == NV_DEVICE_INFO_V0_CELSIUS &&
81b61579 374 nvbo->mode && (domain & NOUVEAU_GEM_DOMAIN_VRAM) &&
4beb116a 375 nvbo->bo.mem.num_pages < vram_pages / 4) {
699ddfd9
FJ
376 /*
377 * Make sure that the color and depth buffers are handled
378 * by independent memory controller units. Up to a 9x
379 * speed up when alpha-blending and depth-test are enabled
380 * at the same time.
381 */
7760a2e3 382 if (nvbo->zeta) {
f1217ed0
CK
383 fpfn = vram_pages / 2;
384 lpfn = ~0;
699ddfd9 385 } else {
f1217ed0
CK
386 fpfn = 0;
387 lpfn = vram_pages / 2;
388 }
389 for (i = 0; i < nvbo->placement.num_placement; ++i) {
390 nvbo->placements[i].fpfn = fpfn;
391 nvbo->placements[i].lpfn = lpfn;
392 }
393 for (i = 0; i < nvbo->placement.num_busy_placement; ++i) {
394 nvbo->busy_placements[i].fpfn = fpfn;
395 nvbo->busy_placements[i].lpfn = lpfn;
699ddfd9
FJ
396 }
397 }
398}
399
6ee73861 400void
81b61579
CK
401nouveau_bo_placement_set(struct nouveau_bo *nvbo, uint32_t domain,
402 uint32_t busy)
6ee73861 403{
78ad0f7b 404 struct ttm_placement *pl = &nvbo->placement;
78ad0f7b
FJ
405
406 pl->placement = nvbo->placements;
ce65b874 407 set_placement_list(nvbo->placements, &pl->num_placement, domain);
78ad0f7b
FJ
408
409 pl->busy_placement = nvbo->busy_placements;
ce65b874
CK
410 set_placement_list(nvbo->busy_placements, &pl->num_busy_placement,
411 domain | busy);
699ddfd9 412
81b61579 413 set_placement_range(nvbo, domain);
6ee73861
BS
414}
415
416int
81b61579 417nouveau_bo_pin(struct nouveau_bo *nvbo, uint32_t domain, bool contig)
6ee73861 418{
ebb945a9 419 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
6ee73861 420 struct ttm_buffer_object *bo = &nvbo->bo;
ad76b3f7 421 bool force = false, evict = false;
78ad0f7b 422 int ret;
6ee73861 423
dfd5e50e 424 ret = ttm_bo_reserve(bo, false, false, NULL);
0ae6d7bc 425 if (ret)
50ab2e52 426 return ret;
0ae6d7bc 427
1167c6bc 428 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA &&
81b61579 429 domain == NOUVEAU_GEM_DOMAIN_VRAM && contig) {
7760a2e3
BS
430 if (!nvbo->contig) {
431 nvbo->contig = true;
ad76b3f7 432 force = true;
7760a2e3 433 evict = true;
ad76b3f7 434 }
6ee73861
BS
435 }
436
6797cea1 437 if (nvbo->bo.pin_count) {
81b61579
CK
438 bool error = evict;
439
440 switch (bo->mem.mem_type) {
441 case TTM_PL_VRAM:
442 error |= !(domain & NOUVEAU_GEM_DOMAIN_VRAM);
443 break;
444 case TTM_PL_TT:
445 error |= !(domain & NOUVEAU_GEM_DOMAIN_GART);
446 default:
447 break;
448 }
449
450 if (error) {
ad76b3f7
BS
451 NV_ERROR(drm, "bo %p pinned elsewhere: "
452 "0x%08x vs 0x%08x\n", bo,
81b61579 453 bo->mem.mem_type, domain);
ad76b3f7
BS
454 ret = -EBUSY;
455 }
6797cea1 456 ttm_bo_pin(&nvbo->bo);
50ab2e52 457 goto out;
ad76b3f7
BS
458 }
459
460 if (evict) {
81b61579 461 nouveau_bo_placement_set(nvbo, NOUVEAU_GEM_DOMAIN_GART, 0);
ad76b3f7
BS
462 ret = nouveau_bo_validate(nvbo, false, false);
463 if (ret)
464 goto out;
465 }
6ee73861 466
81b61579 467 nouveau_bo_placement_set(nvbo, domain, 0);
97a875cb 468 ret = nouveau_bo_validate(nvbo, false, false);
6aac6ced
BS
469 if (ret)
470 goto out;
6797cea1
CK
471
472 ttm_bo_pin(&nvbo->bo);
6aac6ced
BS
473
474 switch (bo->mem.mem_type) {
475 case TTM_PL_VRAM:
476 drm->gem.vram_available -= bo->mem.size;
477 break;
478 case TTM_PL_TT:
479 drm->gem.gart_available -= bo->mem.size;
480 break;
481 default:
482 break;
6ee73861 483 }
5be5a15a 484
6ee73861 485out:
ad76b3f7 486 if (force && ret)
7760a2e3 487 nvbo->contig = false;
0ae6d7bc 488 ttm_bo_unreserve(bo);
6ee73861
BS
489 return ret;
490}
491
492int
493nouveau_bo_unpin(struct nouveau_bo *nvbo)
494{
ebb945a9 495 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
6ee73861 496 struct ttm_buffer_object *bo = &nvbo->bo;
6797cea1 497 int ret;
6ee73861 498
dfd5e50e 499 ret = ttm_bo_reserve(bo, false, false, NULL);
6ee73861
BS
500 if (ret)
501 return ret;
502
6797cea1
CK
503 ttm_bo_unpin(&nvbo->bo);
504 if (!nvbo->bo.pin_count) {
6ee73861
BS
505 switch (bo->mem.mem_type) {
506 case TTM_PL_VRAM:
ebb945a9 507 drm->gem.vram_available += bo->mem.size;
6ee73861
BS
508 break;
509 case TTM_PL_TT:
ebb945a9 510 drm->gem.gart_available += bo->mem.size;
6ee73861
BS
511 break;
512 default:
513 break;
514 }
515 }
516
517 ttm_bo_unreserve(bo);
6797cea1 518 return 0;
6ee73861
BS
519}
520
521int
522nouveau_bo_map(struct nouveau_bo *nvbo)
523{
524 int ret;
525
dfd5e50e 526 ret = ttm_bo_reserve(&nvbo->bo, false, false, NULL);
6ee73861
BS
527 if (ret)
528 return ret;
529
36a471ba 530 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages, &nvbo->kmap);
c3a0c771 531
6ee73861
BS
532 ttm_bo_unreserve(&nvbo->bo);
533 return ret;
534}
535
536void
537nouveau_bo_unmap(struct nouveau_bo *nvbo)
538{
c3a0c771
AC
539 if (!nvbo)
540 return;
541
36a471ba 542 ttm_bo_kunmap(&nvbo->kmap);
6ee73861
BS
543}
544
b22870ba
AC
545void
546nouveau_bo_sync_for_device(struct nouveau_bo *nvbo)
547{
548 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
b22870ba
AC
549 struct ttm_dma_tt *ttm_dma = (struct ttm_dma_tt *)nvbo->bo.ttm;
550 int i;
551
552 if (!ttm_dma)
553 return;
554
555 /* Don't waste time looping if the object is coherent */
556 if (nvbo->force_coherent)
557 return;
558
559 for (i = 0; i < ttm_dma->ttm.num_pages; i++)
359088d5
BS
560 dma_sync_single_for_device(drm->dev->dev,
561 ttm_dma->dma_address[i],
26c9e8ef 562 PAGE_SIZE, DMA_TO_DEVICE);
b22870ba
AC
563}
564
565void
566nouveau_bo_sync_for_cpu(struct nouveau_bo *nvbo)
567{
568 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
b22870ba
AC
569 struct ttm_dma_tt *ttm_dma = (struct ttm_dma_tt *)nvbo->bo.ttm;
570 int i;
571
572 if (!ttm_dma)
573 return;
574
575 /* Don't waste time looping if the object is coherent */
576 if (nvbo->force_coherent)
577 return;
578
579 for (i = 0; i < ttm_dma->ttm.num_pages; i++)
359088d5 580 dma_sync_single_for_cpu(drm->dev->dev, ttm_dma->dma_address[i],
26c9e8ef 581 PAGE_SIZE, DMA_FROM_DEVICE);
b22870ba
AC
582}
583
141b15e5
CK
584void nouveau_bo_add_io_reserve_lru(struct ttm_buffer_object *bo)
585{
586 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
587 struct nouveau_bo *nvbo = nouveau_bo(bo);
588
589 mutex_lock(&drm->ttm.io_reserve_mutex);
590 list_move_tail(&nvbo->io_reserve_lru, &drm->ttm.io_reserve_lru);
591 mutex_unlock(&drm->ttm.io_reserve_mutex);
592}
593
594void nouveau_bo_del_io_reserve_lru(struct ttm_buffer_object *bo)
595{
596 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
597 struct nouveau_bo *nvbo = nouveau_bo(bo);
598
599 mutex_lock(&drm->ttm.io_reserve_mutex);
600 list_del_init(&nvbo->io_reserve_lru);
601 mutex_unlock(&drm->ttm.io_reserve_mutex);
602}
603
7a45d764
BS
604int
605nouveau_bo_validate(struct nouveau_bo *nvbo, bool interruptible,
97a875cb 606 bool no_wait_gpu)
7a45d764 607{
19be5570 608 struct ttm_operation_ctx ctx = { interruptible, no_wait_gpu };
7a45d764
BS
609 int ret;
610
19be5570 611 ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement, &ctx);
7a45d764
BS
612 if (ret)
613 return ret;
614
b22870ba
AC
615 nouveau_bo_sync_for_device(nvbo);
616
7a45d764
BS
617 return 0;
618}
619
6ee73861
BS
620void
621nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val)
622{
623 bool is_iomem;
624 u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
c3a0c771 625
36a471ba 626 mem += index;
c3a0c771 627
6ee73861
BS
628 if (is_iomem)
629 iowrite16_native(val, (void __force __iomem *)mem);
630 else
631 *mem = val;
632}
633
634u32
635nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index)
636{
637 bool is_iomem;
638 u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
c3a0c771 639
36a471ba 640 mem += index;
c3a0c771 641
6ee73861
BS
642 if (is_iomem)
643 return ioread32_native((void __force __iomem *)mem);
644 else
645 return *mem;
646}
647
648void
649nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val)
650{
651 bool is_iomem;
652 u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
c3a0c771 653
36a471ba 654 mem += index;
c3a0c771 655
6ee73861
BS
656 if (is_iomem)
657 iowrite32_native(val, (void __force __iomem *)mem);
658 else
659 *mem = val;
660}
661
649bf3ca 662static struct ttm_tt *
dde5da23 663nouveau_ttm_tt_create(struct ttm_buffer_object *bo, uint32_t page_flags)
6ee73861 664{
a7fb8a23 665#if IS_ENABLED(CONFIG_AGP)
dde5da23 666 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
6ee73861 667
340b0e7c 668 if (drm->agp.bridge) {
dde5da23 669 return ttm_agp_tt_create(bo, drm->agp.bridge, page_flags);
6ee73861 670 }
df1b4b91 671#endif
6ee73861 672
dde5da23 673 return nouveau_sgdma_create_ttm(bo, page_flags);
6ee73861
BS
674}
675
8635784a
DA
676static int
677nouveau_ttm_tt_bind(struct ttm_bo_device *bdev, struct ttm_tt *ttm,
678 struct ttm_resource *reg)
679{
680#if IS_ENABLED(CONFIG_AGP)
681 struct nouveau_drm *drm = nouveau_bdev(bdev);
0b988ca1
DA
682#endif
683 if (!reg)
684 return -EINVAL;
685#if IS_ENABLED(CONFIG_AGP)
8635784a 686 if (drm->agp.bridge)
48efa57e 687 return ttm_agp_bind(ttm, reg);
8635784a
DA
688#endif
689 return nouveau_sgdma_bind(bdev, ttm, reg);
690}
691
692static void
693nouveau_ttm_tt_unbind(struct ttm_bo_device *bdev, struct ttm_tt *ttm)
694{
695#if IS_ENABLED(CONFIG_AGP)
696 struct nouveau_drm *drm = nouveau_bdev(bdev);
697
698 if (drm->agp.bridge) {
48efa57e 699 ttm_agp_unbind(ttm);
8635784a
DA
700 return;
701 }
702#endif
703 nouveau_sgdma_unbind(bdev, ttm);
704}
705
6ee73861
BS
706static void
707nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
708{
709 struct nouveau_bo *nvbo = nouveau_bo(bo);
710
711 switch (bo->mem.mem_type) {
22fbd538 712 case TTM_PL_VRAM:
81b61579
CK
713 nouveau_bo_placement_set(nvbo, NOUVEAU_GEM_DOMAIN_GART,
714 NOUVEAU_GEM_DOMAIN_CPU);
22fbd538 715 break;
6ee73861 716 default:
81b61579 717 nouveau_bo_placement_set(nvbo, NOUVEAU_GEM_DOMAIN_CPU, 0);
6ee73861
BS
718 break;
719 }
22fbd538
FJ
720
721 *pl = nvbo->placement;
6ee73861
BS
722}
723
d2f96666 724static int
3c57d85d 725nouveau_bo_move_prep(struct nouveau_drm *drm, struct ttm_buffer_object *bo,
2966141a 726 struct ttm_resource *reg)
d2f96666 727{
9ce523cc
BS
728 struct nouveau_mem *old_mem = nouveau_mem(&bo->mem);
729 struct nouveau_mem *new_mem = nouveau_mem(reg);
d7722134 730 struct nvif_vmm *vmm = &drm->client.vmm.vmm;
d2f96666
BS
731 int ret;
732
d7722134
BS
733 ret = nvif_vmm_get(vmm, LAZY, false, old_mem->mem.page, 0,
734 old_mem->mem.size, &old_mem->vma[0]);
d2f96666
BS
735 if (ret)
736 return ret;
737
d7722134
BS
738 ret = nvif_vmm_get(vmm, LAZY, false, new_mem->mem.page, 0,
739 new_mem->mem.size, &old_mem->vma[1]);
740 if (ret)
741 goto done;
3c57d85d 742
9ce523cc
BS
743 ret = nouveau_mem_map(old_mem, vmm, &old_mem->vma[0]);
744 if (ret)
745 goto done;
746
747 ret = nouveau_mem_map(new_mem, vmm, &old_mem->vma[1]);
748done:
749 if (ret) {
d7722134
BS
750 nvif_vmm_put(vmm, &old_mem->vma[1]);
751 nvif_vmm_put(vmm, &old_mem->vma[0]);
9ce523cc 752 }
d2f96666
BS
753 return 0;
754}
755
f1ab0cc9 756static int
54661867
DA
757nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict,
758 struct ttm_operation_ctx *ctx,
759 struct ttm_resource *new_reg)
f1ab0cc9 760{
ebb945a9 761 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
1934a2ad 762 struct nouveau_channel *chan = drm->ttm.chan;
a01ca78c 763 struct nouveau_cli *cli = (void *)chan->user.client;
35b8141b 764 struct nouveau_fence *fence;
f1ab0cc9
BS
765 int ret;
766
d2f96666 767 /* create temporary vmas for the transfer and attach them to the
be83cd4e 768 * old nvkm_mem node, these will get cleaned up after ttm has
2966141a 769 * destroyed the ttm_resource
3425df48 770 */
1167c6bc 771 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
605f9ccd 772 ret = nouveau_bo_move_prep(drm, bo, new_reg);
d2f96666 773 if (ret)
3c57d85d 774 return ret;
3425df48
BS
775 }
776
0ad72863 777 mutex_lock_nested(&cli->mutex, SINGLE_DEPTH_NESTING);
54661867 778 ret = nouveau_fence_sync(nouveau_bo(bo), chan, true, ctx->interruptible);
6a6b73f2 779 if (ret == 0) {
605f9ccd 780 ret = drm->ttm.move(chan, bo, &bo->mem, new_reg);
35b8141b
BS
781 if (ret == 0) {
782 ret = nouveau_fence_new(chan, false, &fence);
783 if (ret == 0) {
f2c24b83
ML
784 ret = ttm_bo_move_accel_cleanup(bo,
785 &fence->base,
e46f468f 786 evict, false,
605f9ccd 787 new_reg);
35b8141b
BS
788 nouveau_fence_unref(&fence);
789 }
790 }
6a6b73f2 791 }
0ad72863 792 mutex_unlock(&cli->mutex);
6a6b73f2 793 return ret;
6ee73861
BS
794}
795
d1b167e1 796void
49981046 797nouveau_bo_move_init(struct nouveau_drm *drm)
d1b167e1 798{
72ecb0a6 799 static const struct _method_table {
d1b167e1 800 const char *name;
1a46098e 801 int engine;
315a8b2e 802 s32 oclass;
d1b167e1
BS
803 int (*exec)(struct nouveau_channel *,
804 struct ttm_buffer_object *,
2966141a 805 struct ttm_resource *, struct ttm_resource *);
d1b167e1
BS
806 int (*init)(struct nouveau_channel *, u32 handle);
807 } _methods[] = {
c36322d2
BS
808 { "COPY", 4, 0xc5b5, nve0_bo_move_copy, nve0_bo_move_init },
809 { "GRCE", 0, 0xc5b5, nve0_bo_move_copy, nvc0_bo_move_init },
6e1f34e3
BS
810 { "COPY", 4, 0xc3b5, nve0_bo_move_copy, nve0_bo_move_init },
811 { "GRCE", 0, 0xc3b5, nve0_bo_move_copy, nvc0_bo_move_init },
146cfe24
BS
812 { "COPY", 4, 0xc1b5, nve0_bo_move_copy, nve0_bo_move_init },
813 { "GRCE", 0, 0xc1b5, nve0_bo_move_copy, nvc0_bo_move_init },
8e7e1586
BS
814 { "COPY", 4, 0xc0b5, nve0_bo_move_copy, nve0_bo_move_init },
815 { "GRCE", 0, 0xc0b5, nve0_bo_move_copy, nvc0_bo_move_init },
990b4547
BS
816 { "COPY", 4, 0xb0b5, nve0_bo_move_copy, nve0_bo_move_init },
817 { "GRCE", 0, 0xb0b5, nve0_bo_move_copy, nvc0_bo_move_init },
00fc6f6f 818 { "COPY", 4, 0xa0b5, nve0_bo_move_copy, nve0_bo_move_init },
49981046 819 { "GRCE", 0, 0xa0b5, nve0_bo_move_copy, nvc0_bo_move_init },
1a46098e
BS
820 { "COPY1", 5, 0x90b8, nvc0_bo_move_copy, nvc0_bo_move_init },
821 { "COPY0", 4, 0x90b5, nvc0_bo_move_copy, nvc0_bo_move_init },
822 { "COPY", 0, 0x85b5, nva3_bo_move_copy, nv50_bo_move_init },
823 { "CRYPT", 0, 0x74c1, nv84_bo_move_exec, nv50_bo_move_init },
824 { "M2MF", 0, 0x9039, nvc0_bo_move_m2mf, nvc0_bo_move_init },
825 { "M2MF", 0, 0x5039, nv50_bo_move_m2mf, nv50_bo_move_init },
826 { "M2MF", 0, 0x0039, nv04_bo_move_m2mf, nv04_bo_move_init },
5490e5df 827 {},
72ecb0a6
JJ
828 };
829 const struct _method_table *mthd = _methods;
d1b167e1
BS
830 const char *name = "CPU";
831 int ret;
832
833 do {
49981046 834 struct nouveau_channel *chan;
ebb945a9 835
00fc6f6f 836 if (mthd->engine)
49981046
BS
837 chan = drm->cechan;
838 else
839 chan = drm->channel;
840 if (chan == NULL)
841 continue;
842
9ac596a4 843 ret = nvif_object_ctor(&chan->user, "ttmBoMove",
0ad72863
BS
844 mthd->oclass | (mthd->engine << 16),
845 mthd->oclass, NULL, 0,
846 &drm->ttm.copy);
d1b167e1 847 if (ret == 0) {
0ad72863 848 ret = mthd->init(chan, drm->ttm.copy.handle);
ebb945a9 849 if (ret) {
9ac596a4 850 nvif_object_dtor(&drm->ttm.copy);
ebb945a9 851 continue;
d1b167e1 852 }
ebb945a9
BS
853
854 drm->ttm.move = mthd->exec;
1bb3f6a2 855 drm->ttm.chan = chan;
ebb945a9
BS
856 name = mthd->name;
857 break;
d1b167e1
BS
858 }
859 } while ((++mthd)->exec);
860
ebb945a9 861 NV_INFO(drm, "MM: using %s for buffer copies\n", name);
d1b167e1
BS
862}
863
6ee73861 864static int
54661867
DA
865nouveau_bo_move_flipd(struct ttm_buffer_object *bo, bool evict,
866 struct ttm_operation_ctx *ctx,
867 struct ttm_resource *new_reg)
6ee73861 868{
f1217ed0
CK
869 struct ttm_place placement_memtype = {
870 .fpfn = 0,
871 .lpfn = 0,
48e07c23 872 .mem_type = TTM_PL_TT,
ce65b874 873 .flags = 0
f1217ed0 874 };
6ee73861 875 struct ttm_placement placement;
2966141a 876 struct ttm_resource tmp_reg;
6ee73861
BS
877 int ret;
878
6ee73861 879 placement.num_placement = placement.num_busy_placement = 1;
77e2b5ed 880 placement.placement = placement.busy_placement = &placement_memtype;
6ee73861 881
605f9ccd
BS
882 tmp_reg = *new_reg;
883 tmp_reg.mm_node = NULL;
54661867 884 ret = ttm_bo_mem_space(bo, &placement, &tmp_reg, ctx);
6ee73861
BS
885 if (ret)
886 return ret;
887
54661867 888 ret = ttm_tt_populate(bo->bdev, bo->ttm, ctx);
2040ec97
DA
889 if (ret)
890 goto out;
891
cae515f4 892 ret = nouveau_ttm_tt_bind(bo->bdev, bo->ttm, &tmp_reg);
6ee73861
BS
893 if (ret)
894 goto out;
895
54661867 896 ret = nouveau_bo_move_m2mf(bo, true, ctx, &tmp_reg);
6ee73861
BS
897 if (ret)
898 goto out;
899
54661867 900 ret = ttm_bo_move_ttm(bo, ctx, new_reg);
6ee73861 901out:
b2458726 902 ttm_resource_free(bo, &tmp_reg);
6ee73861
BS
903 return ret;
904}
905
906static int
54661867
DA
907nouveau_bo_move_flips(struct ttm_buffer_object *bo, bool evict,
908 struct ttm_operation_ctx *ctx,
909 struct ttm_resource *new_reg)
6ee73861 910{
f1217ed0
CK
911 struct ttm_place placement_memtype = {
912 .fpfn = 0,
913 .lpfn = 0,
48e07c23 914 .mem_type = TTM_PL_TT,
ce65b874 915 .flags = 0
f1217ed0 916 };
6ee73861 917 struct ttm_placement placement;
2966141a 918 struct ttm_resource tmp_reg;
6ee73861
BS
919 int ret;
920
6ee73861 921 placement.num_placement = placement.num_busy_placement = 1;
77e2b5ed 922 placement.placement = placement.busy_placement = &placement_memtype;
6ee73861 923
605f9ccd
BS
924 tmp_reg = *new_reg;
925 tmp_reg.mm_node = NULL;
54661867 926 ret = ttm_bo_mem_space(bo, &placement, &tmp_reg, ctx);
6ee73861
BS
927 if (ret)
928 return ret;
929
a07e32bd 930 ret = ttm_bo_move_to_new_tt_mem(bo, ctx, &tmp_reg);
6ee73861
BS
931 if (ret)
932 goto out;
933
a07e32bd 934 ttm_bo_assign_mem(bo, &tmp_reg);
54661867 935 ret = nouveau_bo_move_m2mf(bo, true, ctx, new_reg);
6ee73861
BS
936 if (ret)
937 goto out;
938
939out:
b2458726 940 ttm_resource_free(bo, &tmp_reg);
6ee73861
BS
941 return ret;
942}
943
a4154bbf 944static void
66257db7 945nouveau_bo_move_ntfy(struct ttm_buffer_object *bo, bool evict,
2966141a 946 struct ttm_resource *new_reg)
a4154bbf 947{
9ce523cc 948 struct nouveau_mem *mem = new_reg ? nouveau_mem(new_reg) : NULL;
a4154bbf 949 struct nouveau_bo *nvbo = nouveau_bo(bo);
24e8375b 950 struct nouveau_vma *vma;
fd2871af 951
9f1feed2
BS
952 /* ttm can now (stupidly) pass the driver bos it didn't create... */
953 if (bo->destroy != nouveau_bo_del_ttm)
954 return;
955
141b15e5
CK
956 nouveau_bo_del_io_reserve_lru(bo);
957
a48296ab 958 if (mem && new_reg->mem_type != TTM_PL_SYSTEM &&
9ce523cc 959 mem->mem.page == nvbo->page) {
a48296ab 960 list_for_each_entry(vma, &nvbo->vma_list, head) {
24e8375b 961 nouveau_vma_map(vma, mem);
a48296ab
BS
962 }
963 } else {
964 list_for_each_entry(vma, &nvbo->vma_list, head) {
10dcab3e 965 WARN_ON(ttm_bo_wait(bo, false, false));
24e8375b 966 nouveau_vma_unmap(vma);
fd2871af 967 }
a4154bbf 968 }
0dc9b286
ND
969
970 if (new_reg) {
971 if (new_reg->mm_node)
972 nvbo->offset = (new_reg->start << PAGE_SHIFT);
973 else
974 nvbo->offset = 0;
975 }
976
a4154bbf
BS
977}
978
6ee73861 979static int
2966141a 980nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_resource *new_reg,
ebb945a9 981 struct nouveau_drm_tile **new_tile)
6ee73861 982{
ebb945a9
BS
983 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
984 struct drm_device *dev = drm->dev;
a0af9add 985 struct nouveau_bo *nvbo = nouveau_bo(bo);
605f9ccd 986 u64 offset = new_reg->start << PAGE_SHIFT;
6ee73861 987
a4154bbf 988 *new_tile = NULL;
605f9ccd 989 if (new_reg->mem_type != TTM_PL_VRAM)
a0af9add 990 return 0;
a0af9add 991
1167c6bc 992 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_CELSIUS) {
605f9ccd 993 *new_tile = nv10_bo_set_tiling(dev, offset, new_reg->size,
7760a2e3 994 nvbo->mode, nvbo->zeta);
6ee73861
BS
995 }
996
a0af9add
FJ
997 return 0;
998}
999
1000static void
1001nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo,
ebb945a9
BS
1002 struct nouveau_drm_tile *new_tile,
1003 struct nouveau_drm_tile **old_tile)
a0af9add 1004{
ebb945a9
BS
1005 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
1006 struct drm_device *dev = drm->dev;
52791eee 1007 struct dma_fence *fence = dma_resv_get_excl(bo->base.resv);
a0af9add 1008
f2c24b83 1009 nv10_bo_put_tile_region(dev, *old_tile, fence);
a4154bbf 1010 *old_tile = new_tile;
a0af9add
FJ
1011}
1012
1013static int
2823f4f0
CK
1014nouveau_bo_move(struct ttm_buffer_object *bo, bool evict,
1015 struct ttm_operation_ctx *ctx,
2966141a 1016 struct ttm_resource *new_reg)
a0af9add 1017{
ebb945a9 1018 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
a0af9add 1019 struct nouveau_bo *nvbo = nouveau_bo(bo);
2966141a 1020 struct ttm_resource *old_reg = &bo->mem;
ebb945a9 1021 struct nouveau_drm_tile *new_tile = NULL;
a0af9add
FJ
1022 int ret = 0;
1023
0ef1ed81 1024 ret = ttm_bo_wait_ctx(bo, ctx);
88932a7b
CK
1025 if (ret)
1026 return ret;
1027
6797cea1 1028 if (nvbo->bo.pin_count)
5be5a15a
AC
1029 NV_WARN(drm, "Moving pinned object %p!\n", nvbo);
1030
1167c6bc 1031 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
605f9ccd 1032 ret = nouveau_bo_vm_bind(bo, new_reg, &new_tile);
a4154bbf
BS
1033 if (ret)
1034 return ret;
1035 }
a0af9add 1036
a0af9add 1037 /* Fake bo copy. */
605f9ccd 1038 if (old_reg->mem_type == TTM_PL_SYSTEM && !bo->ttm) {
ecfe6953 1039 ttm_bo_move_null(bo, new_reg);
a0af9add 1040 goto out;
6ee73861
BS
1041 }
1042
12b68474
DA
1043 if (old_reg->mem_type == TTM_PL_SYSTEM &&
1044 new_reg->mem_type == TTM_PL_TT) {
1045 ttm_bo_move_null(bo, new_reg);
1046 goto out;
1047 }
1048
1049 if (old_reg->mem_type == TTM_PL_TT &&
1050 new_reg->mem_type == TTM_PL_SYSTEM) {
1051 ret = ttm_bo_move_ttm(bo, ctx, new_reg);
1052 goto out;
1053 }
1054
a0af9add 1055 /* Hardware assisted copy. */
cef9e99e 1056 if (drm->ttm.move) {
605f9ccd 1057 if (new_reg->mem_type == TTM_PL_SYSTEM)
54661867
DA
1058 ret = nouveau_bo_move_flipd(bo, evict, ctx,
1059 new_reg);
605f9ccd 1060 else if (old_reg->mem_type == TTM_PL_SYSTEM)
54661867
DA
1061 ret = nouveau_bo_move_flips(bo, evict, ctx,
1062 new_reg);
cef9e99e 1063 else
54661867
DA
1064 ret = nouveau_bo_move_m2mf(bo, evict, ctx,
1065 new_reg);
cef9e99e
BS
1066 if (!ret)
1067 goto out;
1068 }
a0af9add
FJ
1069
1070 /* Fallback to software copy. */
8117713d 1071 ret = ttm_bo_move_memcpy(bo, ctx, new_reg);
a0af9add
FJ
1072
1073out:
1167c6bc 1074 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
a4154bbf
BS
1075 if (ret)
1076 nouveau_bo_vm_cleanup(bo, NULL, &new_tile);
1077 else
1078 nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile);
1079 }
a0af9add
FJ
1080
1081 return ret;
6ee73861
BS
1082}
1083
1084static int
1085nouveau_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp)
1086{
acb46527
DH
1087 struct nouveau_bo *nvbo = nouveau_bo(bo);
1088
f8659be8 1089 return drm_vma_node_verify_access(&nvbo->bo.base.vma_node,
d9a1f0b4 1090 filp->private_data);
6ee73861
BS
1091}
1092
141b15e5
CK
1093static void
1094nouveau_ttm_io_mem_free_locked(struct nouveau_drm *drm,
1095 struct ttm_resource *reg)
1096{
1097 struct nouveau_mem *mem = nouveau_mem(reg);
1098
1099 if (drm->client.mem->oclass >= NVIF_CLASS_MEM_NV50) {
1100 switch (reg->mem_type) {
1101 case TTM_PL_TT:
1102 if (mem->kind)
1103 nvif_object_unmap_handle(&mem->mem.object);
1104 break;
1105 case TTM_PL_VRAM:
1106 nvif_object_unmap_handle(&mem->mem.object);
1107 break;
1108 default:
1109 break;
1110 }
1111 }
1112}
1113
f32f02fd 1114static int
2966141a 1115nouveau_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_resource *reg)
f32f02fd 1116{
ebb945a9 1117 struct nouveau_drm *drm = nouveau_bdev(bdev);
1167c6bc 1118 struct nvkm_device *device = nvxx_device(&drm->client.device);
9ce523cc 1119 struct nouveau_mem *mem = nouveau_mem(reg);
1cf65c45
CK
1120 struct nvif_mmu *mmu = &drm->client.mmu;
1121 const u8 type = mmu->type[drm->ttm.type_vram].type;
141b15e5 1122 int ret;
f32f02fd 1123
141b15e5
CK
1124 mutex_lock(&drm->ttm.io_reserve_mutex);
1125retry:
605f9ccd 1126 switch (reg->mem_type) {
f32f02fd
JG
1127 case TTM_PL_SYSTEM:
1128 /* System memory */
141b15e5
CK
1129 ret = 0;
1130 goto out;
f32f02fd 1131 case TTM_PL_TT:
a7fb8a23 1132#if IS_ENABLED(CONFIG_AGP)
340b0e7c 1133 if (drm->agp.bridge) {
54d04ea8
CK
1134 reg->bus.offset = (reg->start << PAGE_SHIFT) +
1135 drm->agp.base;
605f9ccd 1136 reg->bus.is_iomem = !drm->agp.cma;
1cf65c45 1137 reg->bus.caching = ttm_write_combined;
f32f02fd
JG
1138 }
1139#endif
141b15e5
CK
1140 if (drm->client.mem->oclass < NVIF_CLASS_MEM_NV50 ||
1141 !mem->kind) {
a5540906 1142 /* untiled */
141b15e5 1143 ret = 0;
a5540906 1144 break;
141b15e5 1145 }
f6e7393e 1146 fallthrough; /* tiled memory */
f32f02fd 1147 case TTM_PL_VRAM:
54d04ea8
CK
1148 reg->bus.offset = (reg->start << PAGE_SHIFT) +
1149 device->func->resource_addr(device, 1);
605f9ccd 1150 reg->bus.is_iomem = true;
1cf65c45
CK
1151
1152 /* Some BARs do not support being ioremapped WC */
1153 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA &&
1154 type & NVIF_MEM_UNCACHED)
1155 reg->bus.caching = ttm_uncached;
1156 else
1157 reg->bus.caching = ttm_write_combined;
1158
d7722134
BS
1159 if (drm->client.mem->oclass >= NVIF_CLASS_MEM_NV50) {
1160 union {
1161 struct nv50_mem_map_v0 nv50;
1162 struct gf100_mem_map_v0 gf100;
1163 } args;
1164 u64 handle, length;
1165 u32 argc = 0;
d7722134
BS
1166
1167 switch (mem->mem.object.oclass) {
1168 case NVIF_CLASS_MEM_NV50:
1169 args.nv50.version = 0;
1170 args.nv50.ro = 0;
1171 args.nv50.kind = mem->kind;
1172 args.nv50.comp = mem->comp;
b554b12a 1173 argc = sizeof(args.nv50);
d7722134
BS
1174 break;
1175 case NVIF_CLASS_MEM_GF100:
1176 args.gf100.version = 0;
1177 args.gf100.ro = 0;
1178 args.gf100.kind = mem->kind;
b554b12a 1179 argc = sizeof(args.gf100);
d7722134
BS
1180 break;
1181 default:
1182 WARN_ON(1);
1183 break;
1184 }
1185
1186 ret = nvif_object_map_handle(&mem->mem.object,
b554b12a 1187 &args, argc,
d7722134 1188 &handle, &length);
8a39db76
BS
1189 if (ret != 1) {
1190 if (WARN_ON(ret == 0))
141b15e5
CK
1191 ret = -EINVAL;
1192 goto out;
8a39db76 1193 }
d7722134 1194
d7722134 1195 reg->bus.offset = handle;
141b15e5 1196 ret = 0;
f869ef88 1197 }
f32f02fd
JG
1198 break;
1199 default:
141b15e5 1200 ret = -EINVAL;
f32f02fd 1201 }
141b15e5
CK
1202
1203out:
1204 if (ret == -ENOSPC) {
1205 struct nouveau_bo *nvbo;
1206
1207 nvbo = list_first_entry_or_null(&drm->ttm.io_reserve_lru,
1208 typeof(*nvbo),
1209 io_reserve_lru);
1210 if (nvbo) {
1211 list_del_init(&nvbo->io_reserve_lru);
1212 drm_vma_node_unmap(&nvbo->bo.base.vma_node,
1213 bdev->dev_mapping);
1214 nouveau_ttm_io_mem_free_locked(drm, &nvbo->bo.mem);
1215 goto retry;
1216 }
1217
1218 }
1219 mutex_unlock(&drm->ttm.io_reserve_mutex);
1220 return ret;
f32f02fd
JG
1221}
1222
1223static void
2966141a 1224nouveau_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_resource *reg)
f32f02fd 1225{
d7722134 1226 struct nouveau_drm *drm = nouveau_bdev(bdev);
f869ef88 1227
141b15e5
CK
1228 mutex_lock(&drm->ttm.io_reserve_mutex);
1229 nouveau_ttm_io_mem_free_locked(drm, reg);
1230 mutex_unlock(&drm->ttm.io_reserve_mutex);
f32f02fd
JG
1231}
1232
76fe313a 1233vm_fault_t nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
f32f02fd 1234{
ebb945a9 1235 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
e1429b4c 1236 struct nouveau_bo *nvbo = nouveau_bo(bo);
1167c6bc 1237 struct nvkm_device *device = nvxx_device(&drm->client.device);
7e8820fe 1238 u32 mappable = device->func->resource_size(device, 1) >> PAGE_SHIFT;
f1217ed0 1239 int i, ret;
e1429b4c
BS
1240
1241 /* as long as the bo isn't in vram, and isn't tiled, we've got
1242 * nothing to do here.
1243 */
1244 if (bo->mem.mem_type != TTM_PL_VRAM) {
1167c6bc 1245 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA ||
7760a2e3 1246 !nvbo->kind)
e1429b4c 1247 return 0;
a5540906 1248
76fe313a
CK
1249 if (bo->mem.mem_type != TTM_PL_SYSTEM)
1250 return 0;
1251
1252 nouveau_bo_placement_set(nvbo, NOUVEAU_GEM_DOMAIN_GART, 0);
1253
1254 } else {
1255 /* make sure bo is in mappable vram */
1256 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA ||
1257 bo->mem.start + bo->mem.num_pages < mappable)
1258 return 0;
a5540906 1259
76fe313a
CK
1260 for (i = 0; i < nvbo->placement.num_placement; ++i) {
1261 nvbo->placements[i].fpfn = 0;
1262 nvbo->placements[i].lpfn = mappable;
a5540906 1263 }
e1429b4c 1264
76fe313a
CK
1265 for (i = 0; i < nvbo->placement.num_busy_placement; ++i) {
1266 nvbo->busy_placements[i].fpfn = 0;
1267 nvbo->busy_placements[i].lpfn = mappable;
1268 }
e1429b4c 1269
76fe313a 1270 nouveau_bo_placement_set(nvbo, NOUVEAU_GEM_DOMAIN_VRAM, 0);
f1217ed0
CK
1271 }
1272
76fe313a
CK
1273 ret = nouveau_bo_validate(nvbo, false, false);
1274 if (unlikely(ret == -EBUSY || ret == -ERESTARTSYS))
1275 return VM_FAULT_NOPAGE;
1276 else if (unlikely(ret))
1277 return VM_FAULT_SIGBUS;
e1429b4c 1278
76fe313a
CK
1279 ttm_bo_move_to_lru_tail_unlocked(bo);
1280 return 0;
f32f02fd
JG
1281}
1282
3230cfc3 1283static int
0a667b50
DA
1284nouveau_ttm_tt_populate(struct ttm_bo_device *bdev,
1285 struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
3230cfc3 1286{
8e7e7052 1287 struct ttm_dma_tt *ttm_dma = (void *)ttm;
ebb945a9 1288 struct nouveau_drm *drm;
359088d5 1289 struct device *dev;
22b33e8e 1290 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
3230cfc3 1291
7eec9151 1292 if (ttm_tt_is_populated(ttm))
3230cfc3
KRW
1293 return 0;
1294
22b33e8e
DA
1295 if (slave && ttm->sg) {
1296 /* make userspace faulting work */
1297 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
1298 ttm_dma->dma_address, ttm->num_pages);
7eec9151 1299 ttm_tt_set_populated(ttm);
22b33e8e
DA
1300 return 0;
1301 }
1302
0a667b50 1303 drm = nouveau_bdev(bdev);
359088d5 1304 dev = drm->dev->dev;
3230cfc3 1305
a7fb8a23 1306#if IS_ENABLED(CONFIG_AGP)
340b0e7c 1307 if (drm->agp.bridge) {
43482554 1308 return ttm_pool_populate(ttm, ctx);
dea7e0ac
JG
1309 }
1310#endif
1311
9bcd38de 1312#if IS_ENABLED(CONFIG_SWIOTLB) && IS_ENABLED(CONFIG_X86)
3230cfc3 1313 if (swiotlb_nr_tbl()) {
d0cef9fa 1314 return ttm_dma_populate((void *)ttm, dev, ctx);
3230cfc3
KRW
1315 }
1316#endif
bd549d35 1317 return ttm_populate_and_map_pages(dev, ttm_dma, ctx);
3230cfc3
KRW
1318}
1319
1320static void
0a667b50
DA
1321nouveau_ttm_tt_unpopulate(struct ttm_bo_device *bdev,
1322 struct ttm_tt *ttm)
3230cfc3 1323{
8e7e7052 1324 struct ttm_dma_tt *ttm_dma = (void *)ttm;
ebb945a9 1325 struct nouveau_drm *drm;
359088d5 1326 struct device *dev;
22b33e8e
DA
1327 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
1328
1329 if (slave)
1330 return;
3230cfc3 1331
0a667b50 1332 drm = nouveau_bdev(bdev);
359088d5 1333 dev = drm->dev->dev;
3230cfc3 1334
a7fb8a23 1335#if IS_ENABLED(CONFIG_AGP)
340b0e7c 1336 if (drm->agp.bridge) {
43482554 1337 ttm_pool_unpopulate(ttm);
dea7e0ac
JG
1338 return;
1339 }
1340#endif
1341
9bcd38de 1342#if IS_ENABLED(CONFIG_SWIOTLB) && IS_ENABLED(CONFIG_X86)
3230cfc3 1343 if (swiotlb_nr_tbl()) {
359088d5 1344 ttm_dma_unpopulate((void *)ttm, dev);
3230cfc3
KRW
1345 return;
1346 }
1347#endif
1348
bd549d35 1349 ttm_unmap_and_unpopulate_pages(dev, ttm_dma);
3230cfc3
KRW
1350}
1351
8635784a
DA
1352static void
1353nouveau_ttm_tt_destroy(struct ttm_bo_device *bdev,
1354 struct ttm_tt *ttm)
1355{
1356#if IS_ENABLED(CONFIG_AGP)
1357 struct nouveau_drm *drm = nouveau_bdev(bdev);
1358 if (drm->agp.bridge) {
37bff654 1359 ttm_agp_unbind(ttm);
7626168f 1360 ttm_tt_destroy_common(bdev, ttm);
48efa57e 1361 ttm_agp_destroy(ttm);
8635784a
DA
1362 return;
1363 }
1364#endif
1365 nouveau_sgdma_destroy(bdev, ttm);
1366}
1367
875ac34a 1368void
809e9447 1369nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence, bool exclusive)
875ac34a 1370{
52791eee 1371 struct dma_resv *resv = nvbo->bo.base.resv;
bdaf7ddf 1372
809e9447 1373 if (exclusive)
52791eee 1374 dma_resv_add_excl_fence(resv, &fence->base);
809e9447 1375 else if (fence)
52791eee 1376 dma_resv_add_shared_fence(resv, &fence->base);
875ac34a
BS
1377}
1378
6ee73861 1379struct ttm_bo_driver nouveau_bo_driver = {
649bf3ca 1380 .ttm_tt_create = &nouveau_ttm_tt_create,
3230cfc3
KRW
1381 .ttm_tt_populate = &nouveau_ttm_tt_populate,
1382 .ttm_tt_unpopulate = &nouveau_ttm_tt_unpopulate,
8635784a
DA
1383 .ttm_tt_bind = &nouveau_ttm_tt_bind,
1384 .ttm_tt_unbind = &nouveau_ttm_tt_unbind,
1385 .ttm_tt_destroy = &nouveau_ttm_tt_destroy,
a2ab19fe 1386 .eviction_valuable = ttm_bo_eviction_valuable,
6ee73861 1387 .evict_flags = nouveau_bo_evict_flags,
a4154bbf 1388 .move_notify = nouveau_bo_move_ntfy,
6ee73861
BS
1389 .move = nouveau_bo_move,
1390 .verify_access = nouveau_bo_verify_access,
f32f02fd
JG
1391 .io_mem_reserve = &nouveau_ttm_io_mem_reserve,
1392 .io_mem_free = &nouveau_ttm_io_mem_free,
6ee73861 1393};