drm/nouveau/ttm: tidy up creation of temporary buffer move vmas
[deliverable/linux.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
ebb945a9 30#include <core/engine.h>
3e2b756b 31#include <linux/swiotlb.h>
6ee73861 32
ebb945a9
BS
33#include <subdev/fb.h>
34#include <subdev/vm.h>
35#include <subdev/bar.h>
36
37#include "nouveau_drm.h"
6ee73861 38#include "nouveau_dma.h"
d375e7d5 39#include "nouveau_fence.h"
6ee73861 40
ebb945a9
BS
41#include "nouveau_bo.h"
42#include "nouveau_ttm.h"
43#include "nouveau_gem.h"
a510604d 44
bc9e7b9a
BS
45/*
46 * NV10-NV40 tiling helpers
47 */
48
49static void
ebb945a9
BS
50nv10_bo_update_tile_region(struct drm_device *dev, struct nouveau_drm_tile *reg,
51 u32 addr, u32 size, u32 pitch, u32 flags)
bc9e7b9a 52{
77145f1c 53 struct nouveau_drm *drm = nouveau_drm(dev);
ebb945a9
BS
54 int i = reg - drm->tile.reg;
55 struct nouveau_fb *pfb = nouveau_fb(drm->device);
56 struct nouveau_fb_tile *tile = &pfb->tile.region[i];
57 struct nouveau_engine *engine;
bc9e7b9a 58
ebb945a9 59 nouveau_fence_unref(&reg->fence);
bc9e7b9a
BS
60
61 if (tile->pitch)
ebb945a9 62 pfb->tile.fini(pfb, i, tile);
bc9e7b9a
BS
63
64 if (pitch)
ebb945a9 65 pfb->tile.init(pfb, i, addr, size, pitch, flags, tile);
bc9e7b9a 66
ebb945a9 67 pfb->tile.prog(pfb, i, tile);
bc9e7b9a 68
ebb945a9
BS
69 if ((engine = nouveau_engine(pfb, NVDEV_ENGINE_GR)))
70 engine->tile_prog(engine, i);
71 if ((engine = nouveau_engine(pfb, NVDEV_ENGINE_MPEG)))
72 engine->tile_prog(engine, i);
bc9e7b9a
BS
73}
74
ebb945a9 75static struct nouveau_drm_tile *
bc9e7b9a
BS
76nv10_bo_get_tile_region(struct drm_device *dev, int i)
77{
77145f1c 78 struct nouveau_drm *drm = nouveau_drm(dev);
ebb945a9 79 struct nouveau_drm_tile *tile = &drm->tile.reg[i];
bc9e7b9a 80
ebb945a9 81 spin_lock(&drm->tile.lock);
bc9e7b9a
BS
82
83 if (!tile->used &&
84 (!tile->fence || nouveau_fence_done(tile->fence)))
85 tile->used = true;
86 else
87 tile = NULL;
88
ebb945a9 89 spin_unlock(&drm->tile.lock);
bc9e7b9a
BS
90 return tile;
91}
92
93static void
ebb945a9
BS
94nv10_bo_put_tile_region(struct drm_device *dev, struct nouveau_drm_tile *tile,
95 struct nouveau_fence *fence)
bc9e7b9a 96{
77145f1c 97 struct nouveau_drm *drm = nouveau_drm(dev);
bc9e7b9a
BS
98
99 if (tile) {
ebb945a9 100 spin_lock(&drm->tile.lock);
5d216f60 101 tile->fence = nouveau_fence_ref(fence);
bc9e7b9a 102 tile->used = false;
ebb945a9 103 spin_unlock(&drm->tile.lock);
bc9e7b9a
BS
104 }
105}
106
ebb945a9
BS
107static struct nouveau_drm_tile *
108nv10_bo_set_tiling(struct drm_device *dev, u32 addr,
109 u32 size, u32 pitch, u32 flags)
bc9e7b9a 110{
77145f1c 111 struct nouveau_drm *drm = nouveau_drm(dev);
ebb945a9
BS
112 struct nouveau_fb *pfb = nouveau_fb(drm->device);
113 struct nouveau_drm_tile *tile, *found = NULL;
bc9e7b9a
BS
114 int i;
115
ebb945a9 116 for (i = 0; i < pfb->tile.regions; i++) {
bc9e7b9a
BS
117 tile = nv10_bo_get_tile_region(dev, i);
118
119 if (pitch && !found) {
120 found = tile;
121 continue;
122
ebb945a9 123 } else if (tile && pfb->tile.region[i].pitch) {
bc9e7b9a
BS
124 /* Kill an unused tile region. */
125 nv10_bo_update_tile_region(dev, tile, 0, 0, 0, 0);
126 }
127
128 nv10_bo_put_tile_region(dev, tile, NULL);
129 }
130
131 if (found)
132 nv10_bo_update_tile_region(dev, found, addr, size,
133 pitch, flags);
134 return found;
135}
136
6ee73861
BS
137static void
138nouveau_bo_del_ttm(struct ttm_buffer_object *bo)
139{
ebb945a9
BS
140 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
141 struct drm_device *dev = drm->dev;
6ee73861
BS
142 struct nouveau_bo *nvbo = nouveau_bo(bo);
143
55fb74ad 144 if (unlikely(nvbo->gem.filp))
6ee73861 145 DRM_ERROR("bo %p still attached to GEM object\n", bo);
4f385599 146 WARN_ON(nvbo->pin_refcnt > 0);
bc9e7b9a 147 nv10_bo_put_tile_region(dev, nvbo->tile, NULL);
6ee73861
BS
148 kfree(nvbo);
149}
150
a0af9add 151static void
db5c8e29 152nouveau_bo_fixup_align(struct nouveau_bo *nvbo, u32 flags,
f91bac5b 153 int *align, int *size)
a0af9add 154{
ebb945a9
BS
155 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
156 struct nouveau_device *device = nv_device(drm->device);
a0af9add 157
ebb945a9 158 if (device->card_type < NV_50) {
bfd83aca 159 if (nvbo->tile_mode) {
ebb945a9 160 if (device->chipset >= 0x40) {
a0af9add 161 *align = 65536;
bfd83aca 162 *size = roundup(*size, 64 * nvbo->tile_mode);
a0af9add 163
ebb945a9 164 } else if (device->chipset >= 0x30) {
a0af9add 165 *align = 32768;
bfd83aca 166 *size = roundup(*size, 64 * nvbo->tile_mode);
a0af9add 167
ebb945a9 168 } else if (device->chipset >= 0x20) {
a0af9add 169 *align = 16384;
bfd83aca 170 *size = roundup(*size, 64 * nvbo->tile_mode);
a0af9add 171
ebb945a9 172 } else if (device->chipset >= 0x10) {
a0af9add 173 *align = 16384;
bfd83aca 174 *size = roundup(*size, 32 * nvbo->tile_mode);
a0af9add
FJ
175 }
176 }
bfd83aca 177 } else {
f91bac5b
BS
178 *size = roundup(*size, (1 << nvbo->page_shift));
179 *align = max((1 << nvbo->page_shift), *align);
a0af9add
FJ
180 }
181
1c7059e4 182 *size = roundup(*size, PAGE_SIZE);
a0af9add
FJ
183}
184
6ee73861 185int
7375c95b
BS
186nouveau_bo_new(struct drm_device *dev, int size, int align,
187 uint32_t flags, uint32_t tile_mode, uint32_t tile_flags,
22b33e8e 188 struct sg_table *sg,
7375c95b 189 struct nouveau_bo **pnvbo)
6ee73861 190{
77145f1c 191 struct nouveau_drm *drm = nouveau_drm(dev);
6ee73861 192 struct nouveau_bo *nvbo;
57de4ba9 193 size_t acc_size;
f91bac5b 194 int ret;
22b33e8e 195 int type = ttm_bo_type_device;
35095f75
ML
196 int lpg_shift = 12;
197 int max_size;
198
199 if (drm->client.base.vm)
200 lpg_shift = drm->client.base.vm->vmm->lpg_shift;
201 max_size = INT_MAX & ~((1 << lpg_shift) - 1);
0108bc80
ML
202
203 if (size <= 0 || size > max_size) {
204 nv_warn(drm, "skipped size %x\n", (u32)size);
205 return -EINVAL;
206 }
22b33e8e
DA
207
208 if (sg)
209 type = ttm_bo_type_sg;
6ee73861
BS
210
211 nvbo = kzalloc(sizeof(struct nouveau_bo), GFP_KERNEL);
212 if (!nvbo)
213 return -ENOMEM;
214 INIT_LIST_HEAD(&nvbo->head);
215 INIT_LIST_HEAD(&nvbo->entry);
fd2871af 216 INIT_LIST_HEAD(&nvbo->vma_list);
6ee73861
BS
217 nvbo->tile_mode = tile_mode;
218 nvbo->tile_flags = tile_flags;
ebb945a9 219 nvbo->bo.bdev = &drm->ttm.bdev;
6ee73861 220
f91bac5b 221 nvbo->page_shift = 12;
ebb945a9 222 if (drm->client.base.vm) {
f91bac5b 223 if (!(flags & TTM_PL_FLAG_TT) && size > 256 * 1024)
ebb945a9 224 nvbo->page_shift = drm->client.base.vm->vmm->lpg_shift;
f91bac5b
BS
225 }
226
227 nouveau_bo_fixup_align(nvbo, flags, &align, &size);
fd2871af
BS
228 nvbo->bo.mem.num_pages = size >> PAGE_SHIFT;
229 nouveau_bo_placement_set(nvbo, flags, 0);
6ee73861 230
ebb945a9 231 acc_size = ttm_bo_dma_acc_size(&drm->ttm.bdev, size,
57de4ba9
JG
232 sizeof(struct nouveau_bo));
233
ebb945a9 234 ret = ttm_bo_init(&drm->ttm.bdev, &nvbo->bo, size,
22b33e8e 235 type, &nvbo->placement,
0b91c4a1 236 align >> PAGE_SHIFT, false, NULL, acc_size, sg,
fd2871af 237 nouveau_bo_del_ttm);
6ee73861
BS
238 if (ret) {
239 /* ttm will call nouveau_bo_del_ttm if it fails.. */
240 return ret;
241 }
242
6ee73861
BS
243 *pnvbo = nvbo;
244 return 0;
245}
246
78ad0f7b
FJ
247static void
248set_placement_list(uint32_t *pl, unsigned *n, uint32_t type, uint32_t flags)
249{
250 *n = 0;
251
252 if (type & TTM_PL_FLAG_VRAM)
253 pl[(*n)++] = TTM_PL_FLAG_VRAM | flags;
254 if (type & TTM_PL_FLAG_TT)
255 pl[(*n)++] = TTM_PL_FLAG_TT | flags;
256 if (type & TTM_PL_FLAG_SYSTEM)
257 pl[(*n)++] = TTM_PL_FLAG_SYSTEM | flags;
258}
259
699ddfd9
FJ
260static void
261set_placement_range(struct nouveau_bo *nvbo, uint32_t type)
262{
ebb945a9
BS
263 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
264 struct nouveau_fb *pfb = nouveau_fb(drm->device);
dceef5d8 265 u32 vram_pages = pfb->ram->size >> PAGE_SHIFT;
699ddfd9 266
4a0ff754
IM
267 if ((nv_device(drm->device)->card_type == NV_10 ||
268 nv_device(drm->device)->card_type == NV_11) &&
812f219a 269 nvbo->tile_mode && (type & TTM_PL_FLAG_VRAM) &&
4beb116a 270 nvbo->bo.mem.num_pages < vram_pages / 4) {
699ddfd9
FJ
271 /*
272 * Make sure that the color and depth buffers are handled
273 * by independent memory controller units. Up to a 9x
274 * speed up when alpha-blending and depth-test are enabled
275 * at the same time.
276 */
699ddfd9
FJ
277 if (nvbo->tile_flags & NOUVEAU_GEM_TILE_ZETA) {
278 nvbo->placement.fpfn = vram_pages / 2;
279 nvbo->placement.lpfn = ~0;
280 } else {
281 nvbo->placement.fpfn = 0;
282 nvbo->placement.lpfn = vram_pages / 2;
283 }
284 }
285}
286
6ee73861 287void
78ad0f7b 288nouveau_bo_placement_set(struct nouveau_bo *nvbo, uint32_t type, uint32_t busy)
6ee73861 289{
78ad0f7b
FJ
290 struct ttm_placement *pl = &nvbo->placement;
291 uint32_t flags = TTM_PL_MASK_CACHING |
292 (nvbo->pin_refcnt ? TTM_PL_FLAG_NO_EVICT : 0);
293
294 pl->placement = nvbo->placements;
295 set_placement_list(nvbo->placements, &pl->num_placement,
296 type, flags);
297
298 pl->busy_placement = nvbo->busy_placements;
299 set_placement_list(nvbo->busy_placements, &pl->num_busy_placement,
300 type | busy, flags);
699ddfd9
FJ
301
302 set_placement_range(nvbo, type);
6ee73861
BS
303}
304
305int
306nouveau_bo_pin(struct nouveau_bo *nvbo, uint32_t memtype)
307{
ebb945a9 308 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
6ee73861 309 struct ttm_buffer_object *bo = &nvbo->bo;
78ad0f7b 310 int ret;
6ee73861 311
0ae6d7bc
DV
312 ret = ttm_bo_reserve(bo, false, false, false, 0);
313 if (ret)
314 goto out;
315
6ee73861 316 if (nvbo->pin_refcnt && !(memtype & (1 << bo->mem.mem_type))) {
ebb945a9 317 NV_ERROR(drm, "bo %p pinned elsewhere: 0x%08x vs 0x%08x\n", bo,
6ee73861 318 1 << bo->mem.mem_type, memtype);
0ae6d7bc
DV
319 ret = -EINVAL;
320 goto out;
6ee73861
BS
321 }
322
323 if (nvbo->pin_refcnt++)
6ee73861
BS
324 goto out;
325
78ad0f7b 326 nouveau_bo_placement_set(nvbo, memtype, 0);
6ee73861 327
97a875cb 328 ret = nouveau_bo_validate(nvbo, false, false);
6ee73861
BS
329 if (ret == 0) {
330 switch (bo->mem.mem_type) {
331 case TTM_PL_VRAM:
ebb945a9 332 drm->gem.vram_available -= bo->mem.size;
6ee73861
BS
333 break;
334 case TTM_PL_TT:
ebb945a9 335 drm->gem.gart_available -= bo->mem.size;
6ee73861
BS
336 break;
337 default:
338 break;
339 }
340 }
6ee73861 341out:
0ae6d7bc 342 ttm_bo_unreserve(bo);
6ee73861
BS
343 return ret;
344}
345
346int
347nouveau_bo_unpin(struct nouveau_bo *nvbo)
348{
ebb945a9 349 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
6ee73861 350 struct ttm_buffer_object *bo = &nvbo->bo;
4f385599 351 int ret, ref;
6ee73861 352
6ee73861
BS
353 ret = ttm_bo_reserve(bo, false, false, false, 0);
354 if (ret)
355 return ret;
356
4f385599
ML
357 ref = --nvbo->pin_refcnt;
358 WARN_ON_ONCE(ref < 0);
359 if (ref)
0ae6d7bc
DV
360 goto out;
361
78ad0f7b 362 nouveau_bo_placement_set(nvbo, bo->mem.placement, 0);
6ee73861 363
97a875cb 364 ret = nouveau_bo_validate(nvbo, false, false);
6ee73861
BS
365 if (ret == 0) {
366 switch (bo->mem.mem_type) {
367 case TTM_PL_VRAM:
ebb945a9 368 drm->gem.vram_available += bo->mem.size;
6ee73861
BS
369 break;
370 case TTM_PL_TT:
ebb945a9 371 drm->gem.gart_available += bo->mem.size;
6ee73861
BS
372 break;
373 default:
374 break;
375 }
376 }
377
0ae6d7bc 378out:
6ee73861
BS
379 ttm_bo_unreserve(bo);
380 return ret;
381}
382
383int
384nouveau_bo_map(struct nouveau_bo *nvbo)
385{
386 int ret;
387
388 ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
389 if (ret)
390 return ret;
391
392 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages, &nvbo->kmap);
393 ttm_bo_unreserve(&nvbo->bo);
394 return ret;
395}
396
397void
398nouveau_bo_unmap(struct nouveau_bo *nvbo)
399{
9d59e8a1
BS
400 if (nvbo)
401 ttm_bo_kunmap(&nvbo->kmap);
6ee73861
BS
402}
403
7a45d764
BS
404int
405nouveau_bo_validate(struct nouveau_bo *nvbo, bool interruptible,
97a875cb 406 bool no_wait_gpu)
7a45d764
BS
407{
408 int ret;
409
97a875cb
ML
410 ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement,
411 interruptible, no_wait_gpu);
7a45d764
BS
412 if (ret)
413 return ret;
414
415 return 0;
416}
417
6ee73861
BS
418u16
419nouveau_bo_rd16(struct nouveau_bo *nvbo, unsigned index)
420{
421 bool is_iomem;
422 u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
423 mem = &mem[index];
424 if (is_iomem)
425 return ioread16_native((void __force __iomem *)mem);
426 else
427 return *mem;
428}
429
430void
431nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val)
432{
433 bool is_iomem;
434 u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
435 mem = &mem[index];
436 if (is_iomem)
437 iowrite16_native(val, (void __force __iomem *)mem);
438 else
439 *mem = val;
440}
441
442u32
443nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index)
444{
445 bool is_iomem;
446 u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
447 mem = &mem[index];
448 if (is_iomem)
449 return ioread32_native((void __force __iomem *)mem);
450 else
451 return *mem;
452}
453
454void
455nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val)
456{
457 bool is_iomem;
458 u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
459 mem = &mem[index];
460 if (is_iomem)
461 iowrite32_native(val, (void __force __iomem *)mem);
462 else
463 *mem = val;
464}
465
649bf3ca 466static struct ttm_tt *
ebb945a9
BS
467nouveau_ttm_tt_create(struct ttm_bo_device *bdev, unsigned long size,
468 uint32_t page_flags, struct page *dummy_read)
6ee73861 469{
df1b4b91 470#if __OS_HAS_AGP
ebb945a9
BS
471 struct nouveau_drm *drm = nouveau_bdev(bdev);
472 struct drm_device *dev = drm->dev;
6ee73861 473
ebb945a9
BS
474 if (drm->agp.stat == ENABLED) {
475 return ttm_agp_tt_create(bdev, dev->agp->bridge, size,
476 page_flags, dummy_read);
6ee73861 477 }
df1b4b91 478#endif
6ee73861 479
ebb945a9 480 return nouveau_sgdma_create_ttm(bdev, size, page_flags, dummy_read);
6ee73861
BS
481}
482
483static int
484nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
485{
486 /* We'll do this from user space. */
487 return 0;
488}
489
490static int
491nouveau_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
492 struct ttm_mem_type_manager *man)
493{
ebb945a9 494 struct nouveau_drm *drm = nouveau_bdev(bdev);
6ee73861
BS
495
496 switch (type) {
497 case TTM_PL_SYSTEM:
498 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
499 man->available_caching = TTM_PL_MASK_CACHING;
500 man->default_caching = TTM_PL_FLAG_CACHED;
501 break;
502 case TTM_PL_VRAM:
ebb945a9 503 if (nv_device(drm->device)->card_type >= NV_50) {
573a2a37 504 man->func = &nouveau_vram_manager;
f869ef88
BS
505 man->io_reserve_fastpath = false;
506 man->use_io_reserve_lru = true;
507 } else {
573a2a37 508 man->func = &ttm_bo_manager_func;
f869ef88 509 }
6ee73861 510 man->flags = TTM_MEMTYPE_FLAG_FIXED |
f32f02fd 511 TTM_MEMTYPE_FLAG_MAPPABLE;
6ee73861
BS
512 man->available_caching = TTM_PL_FLAG_UNCACHED |
513 TTM_PL_FLAG_WC;
514 man->default_caching = TTM_PL_FLAG_WC;
6ee73861
BS
515 break;
516 case TTM_PL_TT:
ebb945a9 517 if (nv_device(drm->device)->card_type >= NV_50)
26c0c9e3 518 man->func = &nouveau_gart_manager;
3863c9bc 519 else
ebb945a9 520 if (drm->agp.stat != ENABLED)
3863c9bc 521 man->func = &nv04_gart_manager;
26c0c9e3
BS
522 else
523 man->func = &ttm_bo_manager_func;
ebb945a9
BS
524
525 if (drm->agp.stat == ENABLED) {
f32f02fd 526 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
a3d487ea
FJ
527 man->available_caching = TTM_PL_FLAG_UNCACHED |
528 TTM_PL_FLAG_WC;
529 man->default_caching = TTM_PL_FLAG_WC;
ebb945a9 530 } else {
6ee73861
BS
531 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE |
532 TTM_MEMTYPE_FLAG_CMA;
533 man->available_caching = TTM_PL_MASK_CACHING;
534 man->default_caching = TTM_PL_FLAG_CACHED;
6ee73861 535 }
ebb945a9 536
6ee73861
BS
537 break;
538 default:
6ee73861
BS
539 return -EINVAL;
540 }
541 return 0;
542}
543
544static void
545nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
546{
547 struct nouveau_bo *nvbo = nouveau_bo(bo);
548
549 switch (bo->mem.mem_type) {
22fbd538 550 case TTM_PL_VRAM:
78ad0f7b
FJ
551 nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_TT,
552 TTM_PL_FLAG_SYSTEM);
22fbd538 553 break;
6ee73861 554 default:
78ad0f7b 555 nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_SYSTEM, 0);
6ee73861
BS
556 break;
557 }
22fbd538
FJ
558
559 *pl = nvbo->placement;
6ee73861
BS
560}
561
562
563/* GPU-assisted copy using NV_MEMORY_TO_MEMORY_FORMAT, can access
564 * TTM_PL_{VRAM,TT} directly.
565 */
a0af9add 566
6ee73861
BS
567static int
568nouveau_bo_move_accel_cleanup(struct nouveau_channel *chan,
9d87fa21 569 struct nouveau_bo *nvbo, bool evict,
97a875cb 570 bool no_wait_gpu, struct ttm_mem_reg *new_mem)
6ee73861
BS
571{
572 struct nouveau_fence *fence = NULL;
573 int ret;
574
264ce192 575 ret = nouveau_fence_new(chan, false, &fence);
6ee73861
BS
576 if (ret)
577 return ret;
578
b03640b1 579 ret = ttm_bo_move_accel_cleanup(&nvbo->bo, fence, evict,
97a875cb 580 no_wait_gpu, new_mem);
382d62e5 581 nouveau_fence_unref(&fence);
6ee73861
BS
582 return ret;
583}
584
49981046
BS
585static int
586nve0_bo_move_init(struct nouveau_channel *chan, u32 handle)
587{
588 int ret = RING_SPACE(chan, 2);
589 if (ret == 0) {
590 BEGIN_NVC0(chan, NvSubCopy, 0x0000, 1);
00fc6f6f 591 OUT_RING (chan, handle & 0x0000ffff);
49981046
BS
592 FIRE_RING (chan);
593 }
594 return ret;
595}
596
c6b7e895
BS
597static int
598nve0_bo_move_copy(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
599 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
600{
601 struct nouveau_mem *node = old_mem->mm_node;
602 int ret = RING_SPACE(chan, 10);
603 if (ret == 0) {
6d597027 604 BEGIN_NVC0(chan, NvSubCopy, 0x0400, 8);
c6b7e895
BS
605 OUT_RING (chan, upper_32_bits(node->vma[0].offset));
606 OUT_RING (chan, lower_32_bits(node->vma[0].offset));
607 OUT_RING (chan, upper_32_bits(node->vma[1].offset));
608 OUT_RING (chan, lower_32_bits(node->vma[1].offset));
609 OUT_RING (chan, PAGE_SIZE);
610 OUT_RING (chan, PAGE_SIZE);
611 OUT_RING (chan, PAGE_SIZE);
612 OUT_RING (chan, new_mem->num_pages);
6d597027 613 BEGIN_IMC0(chan, NvSubCopy, 0x0300, 0x0386);
c6b7e895
BS
614 }
615 return ret;
616}
617
d1b167e1
BS
618static int
619nvc0_bo_move_init(struct nouveau_channel *chan, u32 handle)
620{
621 int ret = RING_SPACE(chan, 2);
622 if (ret == 0) {
623 BEGIN_NVC0(chan, NvSubCopy, 0x0000, 1);
624 OUT_RING (chan, handle);
625 }
626 return ret;
627}
628
1a46098e
BS
629static int
630nvc0_bo_move_copy(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
631 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
632{
633 struct nouveau_mem *node = old_mem->mm_node;
634 u64 src_offset = node->vma[0].offset;
635 u64 dst_offset = node->vma[1].offset;
636 u32 page_count = new_mem->num_pages;
637 int ret;
638
639 page_count = new_mem->num_pages;
640 while (page_count) {
641 int line_count = (page_count > 8191) ? 8191 : page_count;
642
643 ret = RING_SPACE(chan, 11);
644 if (ret)
645 return ret;
646
647 BEGIN_NVC0(chan, NvSubCopy, 0x030c, 8);
648 OUT_RING (chan, upper_32_bits(src_offset));
649 OUT_RING (chan, lower_32_bits(src_offset));
650 OUT_RING (chan, upper_32_bits(dst_offset));
651 OUT_RING (chan, lower_32_bits(dst_offset));
652 OUT_RING (chan, PAGE_SIZE);
653 OUT_RING (chan, PAGE_SIZE);
654 OUT_RING (chan, PAGE_SIZE);
655 OUT_RING (chan, line_count);
656 BEGIN_NVC0(chan, NvSubCopy, 0x0300, 1);
657 OUT_RING (chan, 0x00000110);
658
659 page_count -= line_count;
660 src_offset += (PAGE_SIZE * line_count);
661 dst_offset += (PAGE_SIZE * line_count);
662 }
663
664 return 0;
665}
666
183720b8
BS
667static int
668nvc0_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
669 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
670{
d2f96666
BS
671 struct nouveau_mem *node = old_mem->mm_node;
672 u64 src_offset = node->vma[0].offset;
673 u64 dst_offset = node->vma[1].offset;
183720b8
BS
674 u32 page_count = new_mem->num_pages;
675 int ret;
676
183720b8
BS
677 page_count = new_mem->num_pages;
678 while (page_count) {
679 int line_count = (page_count > 2047) ? 2047 : page_count;
680
681 ret = RING_SPACE(chan, 12);
682 if (ret)
683 return ret;
684
d1b167e1 685 BEGIN_NVC0(chan, NvSubCopy, 0x0238, 2);
183720b8
BS
686 OUT_RING (chan, upper_32_bits(dst_offset));
687 OUT_RING (chan, lower_32_bits(dst_offset));
d1b167e1 688 BEGIN_NVC0(chan, NvSubCopy, 0x030c, 6);
183720b8
BS
689 OUT_RING (chan, upper_32_bits(src_offset));
690 OUT_RING (chan, lower_32_bits(src_offset));
691 OUT_RING (chan, PAGE_SIZE); /* src_pitch */
692 OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
693 OUT_RING (chan, PAGE_SIZE); /* line_length */
694 OUT_RING (chan, line_count);
d1b167e1 695 BEGIN_NVC0(chan, NvSubCopy, 0x0300, 1);
183720b8
BS
696 OUT_RING (chan, 0x00100110);
697
698 page_count -= line_count;
699 src_offset += (PAGE_SIZE * line_count);
700 dst_offset += (PAGE_SIZE * line_count);
701 }
702
703 return 0;
704}
705
fdf53241
BS
706static int
707nva3_bo_move_copy(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
708 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
709{
710 struct nouveau_mem *node = old_mem->mm_node;
711 u64 src_offset = node->vma[0].offset;
712 u64 dst_offset = node->vma[1].offset;
713 u32 page_count = new_mem->num_pages;
714 int ret;
715
716 page_count = new_mem->num_pages;
717 while (page_count) {
718 int line_count = (page_count > 8191) ? 8191 : page_count;
719
720 ret = RING_SPACE(chan, 11);
721 if (ret)
722 return ret;
723
724 BEGIN_NV04(chan, NvSubCopy, 0x030c, 8);
725 OUT_RING (chan, upper_32_bits(src_offset));
726 OUT_RING (chan, lower_32_bits(src_offset));
727 OUT_RING (chan, upper_32_bits(dst_offset));
728 OUT_RING (chan, lower_32_bits(dst_offset));
729 OUT_RING (chan, PAGE_SIZE);
730 OUT_RING (chan, PAGE_SIZE);
731 OUT_RING (chan, PAGE_SIZE);
732 OUT_RING (chan, line_count);
733 BEGIN_NV04(chan, NvSubCopy, 0x0300, 1);
734 OUT_RING (chan, 0x00000110);
735
736 page_count -= line_count;
737 src_offset += (PAGE_SIZE * line_count);
738 dst_offset += (PAGE_SIZE * line_count);
739 }
740
741 return 0;
742}
743
5490e5df
BS
744static int
745nv98_bo_move_exec(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
746 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
747{
748 struct nouveau_mem *node = old_mem->mm_node;
749 int ret = RING_SPACE(chan, 7);
750 if (ret == 0) {
751 BEGIN_NV04(chan, NvSubCopy, 0x0320, 6);
752 OUT_RING (chan, upper_32_bits(node->vma[0].offset));
753 OUT_RING (chan, lower_32_bits(node->vma[0].offset));
754 OUT_RING (chan, upper_32_bits(node->vma[1].offset));
755 OUT_RING (chan, lower_32_bits(node->vma[1].offset));
756 OUT_RING (chan, 0x00000000 /* COPY */);
757 OUT_RING (chan, new_mem->num_pages << PAGE_SHIFT);
758 }
759 return ret;
760}
761
4c193d25
BS
762static int
763nv84_bo_move_exec(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
764 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
765{
766 struct nouveau_mem *node = old_mem->mm_node;
767 int ret = RING_SPACE(chan, 7);
768 if (ret == 0) {
769 BEGIN_NV04(chan, NvSubCopy, 0x0304, 6);
770 OUT_RING (chan, new_mem->num_pages << PAGE_SHIFT);
771 OUT_RING (chan, upper_32_bits(node->vma[0].offset));
772 OUT_RING (chan, lower_32_bits(node->vma[0].offset));
773 OUT_RING (chan, upper_32_bits(node->vma[1].offset));
774 OUT_RING (chan, lower_32_bits(node->vma[1].offset));
775 OUT_RING (chan, 0x00000000 /* MODE_COPY, QUERY_NONE */);
776 }
777 return ret;
778}
779
d1b167e1
BS
780static int
781nv50_bo_move_init(struct nouveau_channel *chan, u32 handle)
782{
ebb945a9 783 int ret = RING_SPACE(chan, 6);
d1b167e1 784 if (ret == 0) {
ebb945a9
BS
785 BEGIN_NV04(chan, NvSubCopy, 0x0000, 1);
786 OUT_RING (chan, handle);
787 BEGIN_NV04(chan, NvSubCopy, 0x0180, 3);
788 OUT_RING (chan, NvNotify0);
789 OUT_RING (chan, NvDmaFB);
790 OUT_RING (chan, NvDmaFB);
d1b167e1
BS
791 }
792
793 return ret;
794}
795
6ee73861 796static int
f1ab0cc9
BS
797nv50_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
798 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
6ee73861 799{
d2f96666 800 struct nouveau_mem *node = old_mem->mm_node;
f1ab0cc9 801 u64 length = (new_mem->num_pages << PAGE_SHIFT);
d2f96666
BS
802 u64 src_offset = node->vma[0].offset;
803 u64 dst_offset = node->vma[1].offset;
ce8f7699
ML
804 int src_tiled = !!node->memtype;
805 int dst_tiled = !!((struct nouveau_mem *)new_mem->mm_node)->memtype;
6ee73861
BS
806 int ret;
807
f1ab0cc9
BS
808 while (length) {
809 u32 amount, stride, height;
810
ce8f7699
ML
811 ret = RING_SPACE(chan, 18 + 6 * (src_tiled + dst_tiled));
812 if (ret)
813 return ret;
814
5220b3c1
BS
815 amount = min(length, (u64)(4 * 1024 * 1024));
816 stride = 16 * 4;
f1ab0cc9
BS
817 height = amount / stride;
818
ce8f7699 819 if (src_tiled) {
d1b167e1 820 BEGIN_NV04(chan, NvSubCopy, 0x0200, 7);
f1ab0cc9 821 OUT_RING (chan, 0);
5220b3c1 822 OUT_RING (chan, 0);
f1ab0cc9
BS
823 OUT_RING (chan, stride);
824 OUT_RING (chan, height);
825 OUT_RING (chan, 1);
826 OUT_RING (chan, 0);
827 OUT_RING (chan, 0);
828 } else {
d1b167e1 829 BEGIN_NV04(chan, NvSubCopy, 0x0200, 1);
f1ab0cc9
BS
830 OUT_RING (chan, 1);
831 }
ce8f7699 832 if (dst_tiled) {
d1b167e1 833 BEGIN_NV04(chan, NvSubCopy, 0x021c, 7);
f1ab0cc9 834 OUT_RING (chan, 0);
5220b3c1 835 OUT_RING (chan, 0);
f1ab0cc9
BS
836 OUT_RING (chan, stride);
837 OUT_RING (chan, height);
838 OUT_RING (chan, 1);
839 OUT_RING (chan, 0);
840 OUT_RING (chan, 0);
841 } else {
d1b167e1 842 BEGIN_NV04(chan, NvSubCopy, 0x021c, 1);
f1ab0cc9
BS
843 OUT_RING (chan, 1);
844 }
845
d1b167e1 846 BEGIN_NV04(chan, NvSubCopy, 0x0238, 2);
f1ab0cc9
BS
847 OUT_RING (chan, upper_32_bits(src_offset));
848 OUT_RING (chan, upper_32_bits(dst_offset));
d1b167e1 849 BEGIN_NV04(chan, NvSubCopy, 0x030c, 8);
f1ab0cc9
BS
850 OUT_RING (chan, lower_32_bits(src_offset));
851 OUT_RING (chan, lower_32_bits(dst_offset));
852 OUT_RING (chan, stride);
853 OUT_RING (chan, stride);
854 OUT_RING (chan, stride);
855 OUT_RING (chan, height);
856 OUT_RING (chan, 0x00000101);
857 OUT_RING (chan, 0x00000000);
d1b167e1 858 BEGIN_NV04(chan, NvSubCopy, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
f1ab0cc9
BS
859 OUT_RING (chan, 0);
860
861 length -= amount;
862 src_offset += amount;
863 dst_offset += amount;
6ee73861
BS
864 }
865
f1ab0cc9
BS
866 return 0;
867}
868
d1b167e1
BS
869static int
870nv04_bo_move_init(struct nouveau_channel *chan, u32 handle)
871{
ebb945a9 872 int ret = RING_SPACE(chan, 4);
d1b167e1 873 if (ret == 0) {
ebb945a9
BS
874 BEGIN_NV04(chan, NvSubCopy, 0x0000, 1);
875 OUT_RING (chan, handle);
876 BEGIN_NV04(chan, NvSubCopy, 0x0180, 1);
877 OUT_RING (chan, NvNotify0);
d1b167e1
BS
878 }
879
880 return ret;
881}
882
a6704788
BS
883static inline uint32_t
884nouveau_bo_mem_ctxdma(struct ttm_buffer_object *bo,
885 struct nouveau_channel *chan, struct ttm_mem_reg *mem)
886{
887 if (mem->mem_type == TTM_PL_TT)
ebb945a9
BS
888 return NvDmaTT;
889 return NvDmaFB;
a6704788
BS
890}
891
f1ab0cc9
BS
892static int
893nv04_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
894 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
895{
d961db75
BS
896 u32 src_offset = old_mem->start << PAGE_SHIFT;
897 u32 dst_offset = new_mem->start << PAGE_SHIFT;
f1ab0cc9
BS
898 u32 page_count = new_mem->num_pages;
899 int ret;
900
901 ret = RING_SPACE(chan, 3);
902 if (ret)
903 return ret;
904
d1b167e1 905 BEGIN_NV04(chan, NvSubCopy, NV_MEMORY_TO_MEMORY_FORMAT_DMA_SOURCE, 2);
f1ab0cc9
BS
906 OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem));
907 OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem));
908
6ee73861
BS
909 page_count = new_mem->num_pages;
910 while (page_count) {
911 int line_count = (page_count > 2047) ? 2047 : page_count;
912
6ee73861
BS
913 ret = RING_SPACE(chan, 11);
914 if (ret)
915 return ret;
f1ab0cc9 916
d1b167e1 917 BEGIN_NV04(chan, NvSubCopy,
6ee73861 918 NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
f1ab0cc9
BS
919 OUT_RING (chan, src_offset);
920 OUT_RING (chan, dst_offset);
921 OUT_RING (chan, PAGE_SIZE); /* src_pitch */
922 OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
923 OUT_RING (chan, PAGE_SIZE); /* line_length */
924 OUT_RING (chan, line_count);
925 OUT_RING (chan, 0x00000101);
926 OUT_RING (chan, 0x00000000);
d1b167e1 927 BEGIN_NV04(chan, NvSubCopy, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
f1ab0cc9 928 OUT_RING (chan, 0);
6ee73861
BS
929
930 page_count -= line_count;
931 src_offset += (PAGE_SIZE * line_count);
932 dst_offset += (PAGE_SIZE * line_count);
933 }
934
f1ab0cc9
BS
935 return 0;
936}
937
d2f96666 938static int
3c57d85d
BS
939nouveau_bo_move_prep(struct nouveau_drm *drm, struct ttm_buffer_object *bo,
940 struct ttm_mem_reg *mem)
d2f96666 941{
3c57d85d
BS
942 struct nouveau_mem *old_node = bo->mem.mm_node;
943 struct nouveau_mem *new_node = mem->mm_node;
944 u64 size = (u64)mem->num_pages << PAGE_SHIFT;
d2f96666
BS
945 int ret;
946
3c57d85d
BS
947 ret = nouveau_vm_get(nv_client(drm)->vm, size, old_node->page_shift,
948 NV_MEM_ACCESS_RW, &old_node->vma[0]);
d2f96666
BS
949 if (ret)
950 return ret;
951
3c57d85d
BS
952 ret = nouveau_vm_get(nv_client(drm)->vm, size, new_node->page_shift,
953 NV_MEM_ACCESS_RW, &old_node->vma[1]);
954 if (ret) {
955 nouveau_vm_put(&old_node->vma[0]);
956 return ret;
957 }
958
959 nouveau_vm_map(&old_node->vma[0], old_node);
960 nouveau_vm_map(&old_node->vma[1], new_node);
d2f96666
BS
961 return 0;
962}
963
f1ab0cc9
BS
964static int
965nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict, bool intr,
97a875cb 966 bool no_wait_gpu, struct ttm_mem_reg *new_mem)
f1ab0cc9 967{
ebb945a9 968 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
1934a2ad 969 struct nouveau_channel *chan = drm->ttm.chan;
f1ab0cc9
BS
970 int ret;
971
d2f96666
BS
972 /* create temporary vmas for the transfer and attach them to the
973 * old nouveau_mem node, these will get cleaned up after ttm has
974 * destroyed the ttm_mem_reg
3425df48 975 */
ebb945a9 976 if (nv_device(drm->device)->card_type >= NV_50) {
3c57d85d 977 ret = nouveau_bo_move_prep(drm, bo, new_mem);
d2f96666 978 if (ret)
3c57d85d 979 return ret;
3425df48
BS
980 }
981
3c57d85d
BS
982 mutex_lock_nested(&chan->cli->mutex, SINGLE_DEPTH_NESTING);
983
ebb945a9 984 ret = drm->ttm.move(chan, bo, &bo->mem, new_mem);
6a6b73f2 985 if (ret == 0) {
3c57d85d 986 struct nouveau_bo *nvbo = nouveau_bo(bo);
6a6b73f2 987 ret = nouveau_bo_move_accel_cleanup(chan, nvbo, evict,
6a6b73f2
BS
988 no_wait_gpu, new_mem);
989 }
f1ab0cc9 990
ebb945a9 991 mutex_unlock(&chan->cli->mutex);
6a6b73f2 992 return ret;
6ee73861
BS
993}
994
d1b167e1 995void
49981046 996nouveau_bo_move_init(struct nouveau_drm *drm)
d1b167e1 997{
d1b167e1
BS
998 static const struct {
999 const char *name;
1a46098e 1000 int engine;
d1b167e1
BS
1001 u32 oclass;
1002 int (*exec)(struct nouveau_channel *,
1003 struct ttm_buffer_object *,
1004 struct ttm_mem_reg *, struct ttm_mem_reg *);
1005 int (*init)(struct nouveau_channel *, u32 handle);
1006 } _methods[] = {
00fc6f6f 1007 { "COPY", 4, 0xa0b5, nve0_bo_move_copy, nve0_bo_move_init },
49981046 1008 { "GRCE", 0, 0xa0b5, nve0_bo_move_copy, nvc0_bo_move_init },
1a46098e
BS
1009 { "COPY1", 5, 0x90b8, nvc0_bo_move_copy, nvc0_bo_move_init },
1010 { "COPY0", 4, 0x90b5, nvc0_bo_move_copy, nvc0_bo_move_init },
1011 { "COPY", 0, 0x85b5, nva3_bo_move_copy, nv50_bo_move_init },
1012 { "CRYPT", 0, 0x74c1, nv84_bo_move_exec, nv50_bo_move_init },
1013 { "M2MF", 0, 0x9039, nvc0_bo_move_m2mf, nvc0_bo_move_init },
1014 { "M2MF", 0, 0x5039, nv50_bo_move_m2mf, nv50_bo_move_init },
1015 { "M2MF", 0, 0x0039, nv04_bo_move_m2mf, nv04_bo_move_init },
5490e5df 1016 {},
1a46098e 1017 { "CRYPT", 0, 0x88b4, nv98_bo_move_exec, nv50_bo_move_init },
d1b167e1
BS
1018 }, *mthd = _methods;
1019 const char *name = "CPU";
1020 int ret;
1021
1022 do {
ebb945a9 1023 struct nouveau_object *object;
49981046 1024 struct nouveau_channel *chan;
1a46098e 1025 u32 handle = (mthd->engine << 16) | mthd->oclass;
ebb945a9 1026
00fc6f6f 1027 if (mthd->engine)
49981046
BS
1028 chan = drm->cechan;
1029 else
1030 chan = drm->channel;
1031 if (chan == NULL)
1032 continue;
1033
1034 ret = nouveau_object_new(nv_object(drm), chan->handle, handle,
ebb945a9 1035 mthd->oclass, NULL, 0, &object);
d1b167e1 1036 if (ret == 0) {
1a46098e 1037 ret = mthd->init(chan, handle);
ebb945a9 1038 if (ret) {
49981046 1039 nouveau_object_del(nv_object(drm),
ebb945a9
BS
1040 chan->handle, handle);
1041 continue;
d1b167e1 1042 }
ebb945a9
BS
1043
1044 drm->ttm.move = mthd->exec;
1bb3f6a2 1045 drm->ttm.chan = chan;
ebb945a9
BS
1046 name = mthd->name;
1047 break;
d1b167e1
BS
1048 }
1049 } while ((++mthd)->exec);
1050
ebb945a9 1051 NV_INFO(drm, "MM: using %s for buffer copies\n", name);
d1b167e1
BS
1052}
1053
6ee73861
BS
1054static int
1055nouveau_bo_move_flipd(struct ttm_buffer_object *bo, bool evict, bool intr,
97a875cb 1056 bool no_wait_gpu, struct ttm_mem_reg *new_mem)
6ee73861
BS
1057{
1058 u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
1059 struct ttm_placement placement;
1060 struct ttm_mem_reg tmp_mem;
1061 int ret;
1062
1063 placement.fpfn = placement.lpfn = 0;
1064 placement.num_placement = placement.num_busy_placement = 1;
77e2b5ed 1065 placement.placement = placement.busy_placement = &placement_memtype;
6ee73861
BS
1066
1067 tmp_mem = *new_mem;
1068 tmp_mem.mm_node = NULL;
97a875cb 1069 ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_gpu);
6ee73861
BS
1070 if (ret)
1071 return ret;
1072
1073 ret = ttm_tt_bind(bo->ttm, &tmp_mem);
1074 if (ret)
1075 goto out;
1076
97a875cb 1077 ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_gpu, &tmp_mem);
6ee73861
BS
1078 if (ret)
1079 goto out;
1080
97a875cb 1081 ret = ttm_bo_move_ttm(bo, true, no_wait_gpu, new_mem);
6ee73861 1082out:
42311ff9 1083 ttm_bo_mem_put(bo, &tmp_mem);
6ee73861
BS
1084 return ret;
1085}
1086
1087static int
1088nouveau_bo_move_flips(struct ttm_buffer_object *bo, bool evict, bool intr,
97a875cb 1089 bool no_wait_gpu, struct ttm_mem_reg *new_mem)
6ee73861
BS
1090{
1091 u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
1092 struct ttm_placement placement;
1093 struct ttm_mem_reg tmp_mem;
1094 int ret;
1095
1096 placement.fpfn = placement.lpfn = 0;
1097 placement.num_placement = placement.num_busy_placement = 1;
77e2b5ed 1098 placement.placement = placement.busy_placement = &placement_memtype;
6ee73861
BS
1099
1100 tmp_mem = *new_mem;
1101 tmp_mem.mm_node = NULL;
97a875cb 1102 ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_gpu);
6ee73861
BS
1103 if (ret)
1104 return ret;
1105
97a875cb 1106 ret = ttm_bo_move_ttm(bo, true, no_wait_gpu, &tmp_mem);
6ee73861
BS
1107 if (ret)
1108 goto out;
1109
97a875cb 1110 ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_gpu, new_mem);
6ee73861
BS
1111 if (ret)
1112 goto out;
1113
1114out:
42311ff9 1115 ttm_bo_mem_put(bo, &tmp_mem);
6ee73861
BS
1116 return ret;
1117}
1118
a4154bbf
BS
1119static void
1120nouveau_bo_move_ntfy(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem)
1121{
a4154bbf 1122 struct nouveau_bo *nvbo = nouveau_bo(bo);
fd2871af
BS
1123 struct nouveau_vma *vma;
1124
9f1feed2
BS
1125 /* ttm can now (stupidly) pass the driver bos it didn't create... */
1126 if (bo->destroy != nouveau_bo_del_ttm)
1127 return;
1128
fd2871af 1129 list_for_each_entry(vma, &nvbo->vma_list, head) {
2e2cfbe6
BS
1130 if (new_mem && new_mem->mem_type != TTM_PL_SYSTEM &&
1131 (new_mem->mem_type == TTM_PL_VRAM ||
1132 nvbo->page_shift != vma->vm->vmm->lpg_shift)) {
fd2871af 1133 nouveau_vm_map(vma, new_mem->mm_node);
fd2871af
BS
1134 } else {
1135 nouveau_vm_unmap(vma);
1136 }
a4154bbf
BS
1137 }
1138}
1139
6ee73861 1140static int
a0af9add 1141nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem,
ebb945a9 1142 struct nouveau_drm_tile **new_tile)
6ee73861 1143{
ebb945a9
BS
1144 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
1145 struct drm_device *dev = drm->dev;
a0af9add 1146 struct nouveau_bo *nvbo = nouveau_bo(bo);
a4154bbf 1147 u64 offset = new_mem->start << PAGE_SHIFT;
6ee73861 1148
a4154bbf
BS
1149 *new_tile = NULL;
1150 if (new_mem->mem_type != TTM_PL_VRAM)
a0af9add 1151 return 0;
a0af9add 1152
ebb945a9 1153 if (nv_device(drm->device)->card_type >= NV_10) {
bc9e7b9a 1154 *new_tile = nv10_bo_set_tiling(dev, offset, new_mem->size,
a5cf68b0
FJ
1155 nvbo->tile_mode,
1156 nvbo->tile_flags);
6ee73861
BS
1157 }
1158
a0af9add
FJ
1159 return 0;
1160}
1161
1162static void
1163nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo,
ebb945a9
BS
1164 struct nouveau_drm_tile *new_tile,
1165 struct nouveau_drm_tile **old_tile)
a0af9add 1166{
ebb945a9
BS
1167 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
1168 struct drm_device *dev = drm->dev;
a0af9add 1169
bc9e7b9a 1170 nv10_bo_put_tile_region(dev, *old_tile, bo->sync_obj);
a4154bbf 1171 *old_tile = new_tile;
a0af9add
FJ
1172}
1173
1174static int
1175nouveau_bo_move(struct ttm_buffer_object *bo, bool evict, bool intr,
97a875cb 1176 bool no_wait_gpu, struct ttm_mem_reg *new_mem)
a0af9add 1177{
ebb945a9 1178 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
a0af9add
FJ
1179 struct nouveau_bo *nvbo = nouveau_bo(bo);
1180 struct ttm_mem_reg *old_mem = &bo->mem;
ebb945a9 1181 struct nouveau_drm_tile *new_tile = NULL;
a0af9add
FJ
1182 int ret = 0;
1183
ebb945a9 1184 if (nv_device(drm->device)->card_type < NV_50) {
a4154bbf
BS
1185 ret = nouveau_bo_vm_bind(bo, new_mem, &new_tile);
1186 if (ret)
1187 return ret;
1188 }
a0af9add 1189
a0af9add 1190 /* Fake bo copy. */
6ee73861
BS
1191 if (old_mem->mem_type == TTM_PL_SYSTEM && !bo->ttm) {
1192 BUG_ON(bo->mem.mm_node != NULL);
1193 bo->mem = *new_mem;
1194 new_mem->mm_node = NULL;
a0af9add 1195 goto out;
6ee73861
BS
1196 }
1197
d1b167e1 1198 /* CPU copy if we have no accelerated method available */
ebb945a9 1199 if (!drm->ttm.move) {
97a875cb 1200 ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, new_mem);
b8a6a804
BS
1201 goto out;
1202 }
1203
a0af9add
FJ
1204 /* Hardware assisted copy. */
1205 if (new_mem->mem_type == TTM_PL_SYSTEM)
97a875cb
ML
1206 ret = nouveau_bo_move_flipd(bo, evict, intr,
1207 no_wait_gpu, new_mem);
a0af9add 1208 else if (old_mem->mem_type == TTM_PL_SYSTEM)
97a875cb
ML
1209 ret = nouveau_bo_move_flips(bo, evict, intr,
1210 no_wait_gpu, new_mem);
a0af9add 1211 else
97a875cb
ML
1212 ret = nouveau_bo_move_m2mf(bo, evict, intr,
1213 no_wait_gpu, new_mem);
6ee73861 1214
a0af9add
FJ
1215 if (!ret)
1216 goto out;
1217
1218 /* Fallback to software copy. */
97a875cb 1219 ret = ttm_bo_move_memcpy(bo, evict, no_wait_gpu, new_mem);
a0af9add
FJ
1220
1221out:
ebb945a9 1222 if (nv_device(drm->device)->card_type < NV_50) {
a4154bbf
BS
1223 if (ret)
1224 nouveau_bo_vm_cleanup(bo, NULL, &new_tile);
1225 else
1226 nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile);
1227 }
a0af9add
FJ
1228
1229 return ret;
6ee73861
BS
1230}
1231
1232static int
1233nouveau_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp)
1234{
acb46527
DH
1235 struct nouveau_bo *nvbo = nouveau_bo(bo);
1236
55fb74ad 1237 return drm_vma_node_verify_access(&nvbo->gem.vma_node, filp);
6ee73861
BS
1238}
1239
f32f02fd
JG
1240static int
1241nouveau_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1242{
1243 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
ebb945a9 1244 struct nouveau_drm *drm = nouveau_bdev(bdev);
a5540906 1245 struct nouveau_mem *node = mem->mm_node;
ebb945a9 1246 struct drm_device *dev = drm->dev;
f869ef88 1247 int ret;
f32f02fd
JG
1248
1249 mem->bus.addr = NULL;
1250 mem->bus.offset = 0;
1251 mem->bus.size = mem->num_pages << PAGE_SHIFT;
1252 mem->bus.base = 0;
1253 mem->bus.is_iomem = false;
1254 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
1255 return -EINVAL;
1256 switch (mem->mem_type) {
1257 case TTM_PL_SYSTEM:
1258 /* System memory */
1259 return 0;
1260 case TTM_PL_TT:
1261#if __OS_HAS_AGP
ebb945a9 1262 if (drm->agp.stat == ENABLED) {
d961db75 1263 mem->bus.offset = mem->start << PAGE_SHIFT;
ebb945a9 1264 mem->bus.base = drm->agp.base;
eda85d6a 1265 mem->bus.is_iomem = !dev->agp->cant_use_aperture;
f32f02fd
JG
1266 }
1267#endif
a5540906
ML
1268 if (!node->memtype)
1269 /* untiled */
1270 break;
1271 /* fallthrough, tiled memory */
f32f02fd 1272 case TTM_PL_VRAM:
3863c9bc
BS
1273 mem->bus.offset = mem->start << PAGE_SHIFT;
1274 mem->bus.base = pci_resource_start(dev->pdev, 1);
1275 mem->bus.is_iomem = true;
ebb945a9
BS
1276 if (nv_device(drm->device)->card_type >= NV_50) {
1277 struct nouveau_bar *bar = nouveau_bar(drm->device);
8984e046 1278
ebb945a9 1279 ret = bar->umap(bar, node, NV_MEM_ACCESS_RW,
3863c9bc
BS
1280 &node->bar_vma);
1281 if (ret)
1282 return ret;
f869ef88 1283
3863c9bc 1284 mem->bus.offset = node->bar_vma.offset;
f869ef88 1285 }
f32f02fd
JG
1286 break;
1287 default:
1288 return -EINVAL;
1289 }
1290 return 0;
1291}
1292
1293static void
1294nouveau_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1295{
ebb945a9
BS
1296 struct nouveau_drm *drm = nouveau_bdev(bdev);
1297 struct nouveau_bar *bar = nouveau_bar(drm->device);
d5f42394 1298 struct nouveau_mem *node = mem->mm_node;
f869ef88 1299
d5f42394 1300 if (!node->bar_vma.node)
f869ef88
BS
1301 return;
1302
ebb945a9 1303 bar->unmap(bar, &node->bar_vma);
f32f02fd
JG
1304}
1305
1306static int
1307nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
1308{
ebb945a9 1309 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
e1429b4c 1310 struct nouveau_bo *nvbo = nouveau_bo(bo);
ebb945a9
BS
1311 struct nouveau_device *device = nv_device(drm->device);
1312 u32 mappable = pci_resource_len(device->pdev, 1) >> PAGE_SHIFT;
a5540906 1313 int ret;
e1429b4c
BS
1314
1315 /* as long as the bo isn't in vram, and isn't tiled, we've got
1316 * nothing to do here.
1317 */
1318 if (bo->mem.mem_type != TTM_PL_VRAM) {
ebb945a9 1319 if (nv_device(drm->device)->card_type < NV_50 ||
f13b3263 1320 !nouveau_bo_tile_layout(nvbo))
e1429b4c 1321 return 0;
a5540906
ML
1322
1323 if (bo->mem.mem_type == TTM_PL_SYSTEM) {
1324 nouveau_bo_placement_set(nvbo, TTM_PL_TT, 0);
1325
1326 ret = nouveau_bo_validate(nvbo, false, false);
1327 if (ret)
1328 return ret;
1329 }
1330 return 0;
e1429b4c
BS
1331 }
1332
1333 /* make sure bo is in mappable vram */
a5540906
ML
1334 if (nv_device(drm->device)->card_type >= NV_50 ||
1335 bo->mem.start + bo->mem.num_pages < mappable)
e1429b4c
BS
1336 return 0;
1337
1338
1339 nvbo->placement.fpfn = 0;
ebb945a9 1340 nvbo->placement.lpfn = mappable;
c284815d 1341 nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_VRAM, 0);
97a875cb 1342 return nouveau_bo_validate(nvbo, false, false);
f32f02fd
JG
1343}
1344
3230cfc3
KRW
1345static int
1346nouveau_ttm_tt_populate(struct ttm_tt *ttm)
1347{
8e7e7052 1348 struct ttm_dma_tt *ttm_dma = (void *)ttm;
ebb945a9 1349 struct nouveau_drm *drm;
3230cfc3
KRW
1350 struct drm_device *dev;
1351 unsigned i;
1352 int r;
22b33e8e 1353 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
3230cfc3
KRW
1354
1355 if (ttm->state != tt_unpopulated)
1356 return 0;
1357
22b33e8e
DA
1358 if (slave && ttm->sg) {
1359 /* make userspace faulting work */
1360 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
1361 ttm_dma->dma_address, ttm->num_pages);
1362 ttm->state = tt_unbound;
1363 return 0;
1364 }
1365
ebb945a9
BS
1366 drm = nouveau_bdev(ttm->bdev);
1367 dev = drm->dev;
3230cfc3 1368
dea7e0ac 1369#if __OS_HAS_AGP
ebb945a9 1370 if (drm->agp.stat == ENABLED) {
dea7e0ac
JG
1371 return ttm_agp_tt_populate(ttm);
1372 }
1373#endif
1374
3230cfc3
KRW
1375#ifdef CONFIG_SWIOTLB
1376 if (swiotlb_nr_tbl()) {
8e7e7052 1377 return ttm_dma_populate((void *)ttm, dev->dev);
3230cfc3
KRW
1378 }
1379#endif
1380
1381 r = ttm_pool_populate(ttm);
1382 if (r) {
1383 return r;
1384 }
1385
1386 for (i = 0; i < ttm->num_pages; i++) {
8e7e7052 1387 ttm_dma->dma_address[i] = pci_map_page(dev->pdev, ttm->pages[i],
3230cfc3
KRW
1388 0, PAGE_SIZE,
1389 PCI_DMA_BIDIRECTIONAL);
8e7e7052 1390 if (pci_dma_mapping_error(dev->pdev, ttm_dma->dma_address[i])) {
3230cfc3 1391 while (--i) {
8e7e7052 1392 pci_unmap_page(dev->pdev, ttm_dma->dma_address[i],
3230cfc3 1393 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
8e7e7052 1394 ttm_dma->dma_address[i] = 0;
3230cfc3
KRW
1395 }
1396 ttm_pool_unpopulate(ttm);
1397 return -EFAULT;
1398 }
1399 }
1400 return 0;
1401}
1402
1403static void
1404nouveau_ttm_tt_unpopulate(struct ttm_tt *ttm)
1405{
8e7e7052 1406 struct ttm_dma_tt *ttm_dma = (void *)ttm;
ebb945a9 1407 struct nouveau_drm *drm;
3230cfc3
KRW
1408 struct drm_device *dev;
1409 unsigned i;
22b33e8e
DA
1410 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
1411
1412 if (slave)
1413 return;
3230cfc3 1414
ebb945a9
BS
1415 drm = nouveau_bdev(ttm->bdev);
1416 dev = drm->dev;
3230cfc3 1417
dea7e0ac 1418#if __OS_HAS_AGP
ebb945a9 1419 if (drm->agp.stat == ENABLED) {
dea7e0ac
JG
1420 ttm_agp_tt_unpopulate(ttm);
1421 return;
1422 }
1423#endif
1424
3230cfc3
KRW
1425#ifdef CONFIG_SWIOTLB
1426 if (swiotlb_nr_tbl()) {
8e7e7052 1427 ttm_dma_unpopulate((void *)ttm, dev->dev);
3230cfc3
KRW
1428 return;
1429 }
1430#endif
1431
1432 for (i = 0; i < ttm->num_pages; i++) {
8e7e7052
JG
1433 if (ttm_dma->dma_address[i]) {
1434 pci_unmap_page(dev->pdev, ttm_dma->dma_address[i],
3230cfc3
KRW
1435 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
1436 }
1437 }
1438
1439 ttm_pool_unpopulate(ttm);
1440}
1441
875ac34a
BS
1442void
1443nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence)
1444{
5d216f60 1445 struct nouveau_fence *new_fence = nouveau_fence_ref(fence);
875ac34a
BS
1446 struct nouveau_fence *old_fence = NULL;
1447
875ac34a
BS
1448 spin_lock(&nvbo->bo.bdev->fence_lock);
1449 old_fence = nvbo->bo.sync_obj;
5d216f60 1450 nvbo->bo.sync_obj = new_fence;
875ac34a
BS
1451 spin_unlock(&nvbo->bo.bdev->fence_lock);
1452
1453 nouveau_fence_unref(&old_fence);
1454}
1455
1456static void
1457nouveau_bo_fence_unref(void **sync_obj)
1458{
1459 nouveau_fence_unref((struct nouveau_fence **)sync_obj);
1460}
1461
1462static void *
1463nouveau_bo_fence_ref(void *sync_obj)
1464{
1465 return nouveau_fence_ref(sync_obj);
1466}
1467
1468static bool
dedfdffd 1469nouveau_bo_fence_signalled(void *sync_obj)
875ac34a 1470{
d375e7d5 1471 return nouveau_fence_done(sync_obj);
875ac34a
BS
1472}
1473
1474static int
dedfdffd 1475nouveau_bo_fence_wait(void *sync_obj, bool lazy, bool intr)
875ac34a
BS
1476{
1477 return nouveau_fence_wait(sync_obj, lazy, intr);
1478}
1479
1480static int
dedfdffd 1481nouveau_bo_fence_flush(void *sync_obj)
875ac34a
BS
1482{
1483 return 0;
1484}
1485
6ee73861 1486struct ttm_bo_driver nouveau_bo_driver = {
649bf3ca 1487 .ttm_tt_create = &nouveau_ttm_tt_create,
3230cfc3
KRW
1488 .ttm_tt_populate = &nouveau_ttm_tt_populate,
1489 .ttm_tt_unpopulate = &nouveau_ttm_tt_unpopulate,
6ee73861
BS
1490 .invalidate_caches = nouveau_bo_invalidate_caches,
1491 .init_mem_type = nouveau_bo_init_mem_type,
1492 .evict_flags = nouveau_bo_evict_flags,
a4154bbf 1493 .move_notify = nouveau_bo_move_ntfy,
6ee73861
BS
1494 .move = nouveau_bo_move,
1495 .verify_access = nouveau_bo_verify_access,
875ac34a
BS
1496 .sync_obj_signaled = nouveau_bo_fence_signalled,
1497 .sync_obj_wait = nouveau_bo_fence_wait,
1498 .sync_obj_flush = nouveau_bo_fence_flush,
1499 .sync_obj_unref = nouveau_bo_fence_unref,
1500 .sync_obj_ref = nouveau_bo_fence_ref,
f32f02fd
JG
1501 .fault_reserve_notify = &nouveau_ttm_fault_reserve_notify,
1502 .io_mem_reserve = &nouveau_ttm_io_mem_reserve,
1503 .io_mem_free = &nouveau_ttm_io_mem_free,
6ee73861
BS
1504};
1505
fd2871af
BS
1506struct nouveau_vma *
1507nouveau_bo_vma_find(struct nouveau_bo *nvbo, struct nouveau_vm *vm)
1508{
1509 struct nouveau_vma *vma;
1510 list_for_each_entry(vma, &nvbo->vma_list, head) {
1511 if (vma->vm == vm)
1512 return vma;
1513 }
1514
1515 return NULL;
1516}
1517
1518int
1519nouveau_bo_vma_add(struct nouveau_bo *nvbo, struct nouveau_vm *vm,
1520 struct nouveau_vma *vma)
1521{
1522 const u32 size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
fd2871af
BS
1523 int ret;
1524
1525 ret = nouveau_vm_get(vm, size, nvbo->page_shift,
1526 NV_MEM_ACCESS_RW, vma);
1527 if (ret)
1528 return ret;
1529
2e2cfbe6
BS
1530 if ( nvbo->bo.mem.mem_type != TTM_PL_SYSTEM &&
1531 (nvbo->bo.mem.mem_type == TTM_PL_VRAM ||
1532 nvbo->page_shift != vma->vm->vmm->lpg_shift))
fd2871af 1533 nouveau_vm_map(vma, nvbo->bo.mem.mm_node);
fd2871af
BS
1534
1535 list_add_tail(&vma->head, &nvbo->vma_list);
2fd3db6f 1536 vma->refcount = 1;
fd2871af
BS
1537 return 0;
1538}
1539
1540void
1541nouveau_bo_vma_del(struct nouveau_bo *nvbo, struct nouveau_vma *vma)
1542{
1543 if (vma->node) {
c4c7044f 1544 if (nvbo->bo.mem.mem_type != TTM_PL_SYSTEM)
fd2871af 1545 nouveau_vm_unmap(vma);
fd2871af
BS
1546 nouveau_vm_put(vma);
1547 list_del(&vma->head);
1548 }
1549}
This page took 0.331755 seconds and 5 git commands to generate.