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