]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/nouveau/nouveau_bo.c
drm/nouveau: Avoid potential race between nouveau_fence_update() and context takedown.
[mirror_ubuntu-bionic-kernel.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
30#include "drmP.h"
31
32#include "nouveau_drm.h"
33#include "nouveau_drv.h"
34#include "nouveau_dma.h"
35
a510604d 36#include <linux/log2.h>
5a0e3ad6 37#include <linux/slab.h>
a510604d 38
6ee73861
BS
39static void
40nouveau_bo_del_ttm(struct ttm_buffer_object *bo)
41{
42 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
a0af9add 43 struct drm_device *dev = dev_priv->dev;
6ee73861
BS
44 struct nouveau_bo *nvbo = nouveau_bo(bo);
45
6ee73861
BS
46 if (unlikely(nvbo->gem))
47 DRM_ERROR("bo %p still attached to GEM object\n", bo);
48
a5cf68b0 49 nv10_mem_put_tile_region(dev, nvbo->tile, NULL);
6ee73861
BS
50 kfree(nvbo);
51}
52
a0af9add
FJ
53static void
54nouveau_bo_fixup_align(struct drm_device *dev,
55 uint32_t tile_mode, uint32_t tile_flags,
56 int *align, int *size)
57{
58 struct drm_nouveau_private *dev_priv = dev->dev_private;
59
60 /*
61 * Some of the tile_flags have a periodic structure of N*4096 bytes,
eb1dba0e
MM
62 * align to to that as well as the page size. Align the size to the
63 * appropriate boundaries. This does imply that sizes are rounded up
64 * 3-7 pages, so be aware of this and do not waste memory by allocating
65 * many small buffers.
a0af9add
FJ
66 */
67 if (dev_priv->card_type == NV_50) {
a76fb4e8 68 uint32_t block_size = dev_priv->vram_size >> 15;
a510604d
MM
69 int i;
70
a0af9add
FJ
71 switch (tile_flags) {
72 case 0x1800:
73 case 0x2800:
74 case 0x4800:
75 case 0x7a00:
a510604d 76 if (is_power_of_2(block_size)) {
a510604d
MM
77 for (i = 1; i < 10; i++) {
78 *align = 12 * i * block_size;
79 if (!(*align % 65536))
80 break;
81 }
a0af9add 82 } else {
a510604d
MM
83 for (i = 1; i < 10; i++) {
84 *align = 8 * i * block_size;
85 if (!(*align % 65536))
86 break;
87 }
a0af9add 88 }
eb1dba0e 89 *size = roundup(*size, *align);
a0af9add
FJ
90 break;
91 default:
92 break;
93 }
94
95 } else {
96 if (tile_mode) {
97 if (dev_priv->chipset >= 0x40) {
98 *align = 65536;
99 *size = roundup(*size, 64 * tile_mode);
100
101 } else if (dev_priv->chipset >= 0x30) {
102 *align = 32768;
103 *size = roundup(*size, 64 * tile_mode);
104
105 } else if (dev_priv->chipset >= 0x20) {
106 *align = 16384;
107 *size = roundup(*size, 64 * tile_mode);
108
109 } else if (dev_priv->chipset >= 0x10) {
110 *align = 16384;
111 *size = roundup(*size, 32 * tile_mode);
112 }
113 }
114 }
115
1c7059e4
MM
116 /* ALIGN works only on powers of two. */
117 *size = roundup(*size, PAGE_SIZE);
a0af9add
FJ
118
119 if (dev_priv->card_type == NV_50) {
1c7059e4 120 *size = roundup(*size, 65536);
a0af9add
FJ
121 *align = max(65536, *align);
122 }
123}
124
6ee73861
BS
125int
126nouveau_bo_new(struct drm_device *dev, struct nouveau_channel *chan,
127 int size, int align, uint32_t flags, uint32_t tile_mode,
128 uint32_t tile_flags, bool no_vm, bool mappable,
129 struct nouveau_bo **pnvbo)
130{
131 struct drm_nouveau_private *dev_priv = dev->dev_private;
132 struct nouveau_bo *nvbo;
8dea4a19 133 int ret = 0;
6ee73861
BS
134
135 nvbo = kzalloc(sizeof(struct nouveau_bo), GFP_KERNEL);
136 if (!nvbo)
137 return -ENOMEM;
138 INIT_LIST_HEAD(&nvbo->head);
139 INIT_LIST_HEAD(&nvbo->entry);
140 nvbo->mappable = mappable;
141 nvbo->no_vm = no_vm;
142 nvbo->tile_mode = tile_mode;
143 nvbo->tile_flags = tile_flags;
699ddfd9 144 nvbo->bo.bdev = &dev_priv->ttm.bdev;
6ee73861 145
f13b3263
FJ
146 nouveau_bo_fixup_align(dev, tile_mode, nouveau_bo_tile_layout(nvbo),
147 &align, &size);
6ee73861
BS
148 align >>= PAGE_SHIFT;
149
78ad0f7b 150 nouveau_bo_placement_set(nvbo, flags, 0);
6ee73861
BS
151
152 nvbo->channel = chan;
6ee73861
BS
153 ret = ttm_bo_init(&dev_priv->ttm.bdev, &nvbo->bo, size,
154 ttm_bo_type_device, &nvbo->placement, align, 0,
155 false, NULL, size, nouveau_bo_del_ttm);
6ee73861
BS
156 if (ret) {
157 /* ttm will call nouveau_bo_del_ttm if it fails.. */
158 return ret;
159 }
90af89b9 160 nvbo->channel = NULL;
6ee73861 161
6ee73861
BS
162 *pnvbo = nvbo;
163 return 0;
164}
165
78ad0f7b
FJ
166static void
167set_placement_list(uint32_t *pl, unsigned *n, uint32_t type, uint32_t flags)
168{
169 *n = 0;
170
171 if (type & TTM_PL_FLAG_VRAM)
172 pl[(*n)++] = TTM_PL_FLAG_VRAM | flags;
173 if (type & TTM_PL_FLAG_TT)
174 pl[(*n)++] = TTM_PL_FLAG_TT | flags;
175 if (type & TTM_PL_FLAG_SYSTEM)
176 pl[(*n)++] = TTM_PL_FLAG_SYSTEM | flags;
177}
178
699ddfd9
FJ
179static void
180set_placement_range(struct nouveau_bo *nvbo, uint32_t type)
181{
182 struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
183
184 if (dev_priv->card_type == NV_10 &&
185 nvbo->tile_mode && (type & TTM_PL_FLAG_VRAM)) {
186 /*
187 * Make sure that the color and depth buffers are handled
188 * by independent memory controller units. Up to a 9x
189 * speed up when alpha-blending and depth-test are enabled
190 * at the same time.
191 */
192 int vram_pages = dev_priv->vram_size >> PAGE_SHIFT;
193
194 if (nvbo->tile_flags & NOUVEAU_GEM_TILE_ZETA) {
195 nvbo->placement.fpfn = vram_pages / 2;
196 nvbo->placement.lpfn = ~0;
197 } else {
198 nvbo->placement.fpfn = 0;
199 nvbo->placement.lpfn = vram_pages / 2;
200 }
201 }
202}
203
6ee73861 204void
78ad0f7b 205nouveau_bo_placement_set(struct nouveau_bo *nvbo, uint32_t type, uint32_t busy)
6ee73861 206{
78ad0f7b
FJ
207 struct ttm_placement *pl = &nvbo->placement;
208 uint32_t flags = TTM_PL_MASK_CACHING |
209 (nvbo->pin_refcnt ? TTM_PL_FLAG_NO_EVICT : 0);
210
211 pl->placement = nvbo->placements;
212 set_placement_list(nvbo->placements, &pl->num_placement,
213 type, flags);
214
215 pl->busy_placement = nvbo->busy_placements;
216 set_placement_list(nvbo->busy_placements, &pl->num_busy_placement,
217 type | busy, flags);
699ddfd9
FJ
218
219 set_placement_range(nvbo, type);
6ee73861
BS
220}
221
222int
223nouveau_bo_pin(struct nouveau_bo *nvbo, uint32_t memtype)
224{
225 struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
226 struct ttm_buffer_object *bo = &nvbo->bo;
78ad0f7b 227 int ret;
6ee73861
BS
228
229 if (nvbo->pin_refcnt && !(memtype & (1 << bo->mem.mem_type))) {
230 NV_ERROR(nouveau_bdev(bo->bdev)->dev,
231 "bo %p pinned elsewhere: 0x%08x vs 0x%08x\n", bo,
232 1 << bo->mem.mem_type, memtype);
233 return -EINVAL;
234 }
235
236 if (nvbo->pin_refcnt++)
237 return 0;
238
239 ret = ttm_bo_reserve(bo, false, false, false, 0);
240 if (ret)
241 goto out;
242
78ad0f7b 243 nouveau_bo_placement_set(nvbo, memtype, 0);
6ee73861 244
7a45d764 245 ret = nouveau_bo_validate(nvbo, false, false, false);
6ee73861
BS
246 if (ret == 0) {
247 switch (bo->mem.mem_type) {
248 case TTM_PL_VRAM:
249 dev_priv->fb_aper_free -= bo->mem.size;
250 break;
251 case TTM_PL_TT:
252 dev_priv->gart_info.aper_free -= bo->mem.size;
253 break;
254 default:
255 break;
256 }
257 }
258 ttm_bo_unreserve(bo);
259out:
260 if (unlikely(ret))
261 nvbo->pin_refcnt--;
262 return ret;
263}
264
265int
266nouveau_bo_unpin(struct nouveau_bo *nvbo)
267{
268 struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
269 struct ttm_buffer_object *bo = &nvbo->bo;
78ad0f7b 270 int ret;
6ee73861
BS
271
272 if (--nvbo->pin_refcnt)
273 return 0;
274
275 ret = ttm_bo_reserve(bo, false, false, false, 0);
276 if (ret)
277 return ret;
278
78ad0f7b 279 nouveau_bo_placement_set(nvbo, bo->mem.placement, 0);
6ee73861 280
7a45d764 281 ret = nouveau_bo_validate(nvbo, false, false, false);
6ee73861
BS
282 if (ret == 0) {
283 switch (bo->mem.mem_type) {
284 case TTM_PL_VRAM:
285 dev_priv->fb_aper_free += bo->mem.size;
286 break;
287 case TTM_PL_TT:
288 dev_priv->gart_info.aper_free += bo->mem.size;
289 break;
290 default:
291 break;
292 }
293 }
294
295 ttm_bo_unreserve(bo);
296 return ret;
297}
298
299int
300nouveau_bo_map(struct nouveau_bo *nvbo)
301{
302 int ret;
303
304 ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
305 if (ret)
306 return ret;
307
308 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages, &nvbo->kmap);
309 ttm_bo_unreserve(&nvbo->bo);
310 return ret;
311}
312
313void
314nouveau_bo_unmap(struct nouveau_bo *nvbo)
315{
9d59e8a1
BS
316 if (nvbo)
317 ttm_bo_kunmap(&nvbo->kmap);
6ee73861
BS
318}
319
7a45d764
BS
320int
321nouveau_bo_validate(struct nouveau_bo *nvbo, bool interruptible,
322 bool no_wait_reserve, bool no_wait_gpu)
323{
324 int ret;
325
326 ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement, interruptible,
327 no_wait_reserve, no_wait_gpu);
328 if (ret)
329 return ret;
330
331 return 0;
332}
333
6ee73861
BS
334u16
335nouveau_bo_rd16(struct nouveau_bo *nvbo, unsigned index)
336{
337 bool is_iomem;
338 u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
339 mem = &mem[index];
340 if (is_iomem)
341 return ioread16_native((void __force __iomem *)mem);
342 else
343 return *mem;
344}
345
346void
347nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val)
348{
349 bool is_iomem;
350 u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
351 mem = &mem[index];
352 if (is_iomem)
353 iowrite16_native(val, (void __force __iomem *)mem);
354 else
355 *mem = val;
356}
357
358u32
359nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index)
360{
361 bool is_iomem;
362 u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
363 mem = &mem[index];
364 if (is_iomem)
365 return ioread32_native((void __force __iomem *)mem);
366 else
367 return *mem;
368}
369
370void
371nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val)
372{
373 bool is_iomem;
374 u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
375 mem = &mem[index];
376 if (is_iomem)
377 iowrite32_native(val, (void __force __iomem *)mem);
378 else
379 *mem = val;
380}
381
382static struct ttm_backend *
383nouveau_bo_create_ttm_backend_entry(struct ttm_bo_device *bdev)
384{
385 struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
386 struct drm_device *dev = dev_priv->dev;
387
388 switch (dev_priv->gart_info.type) {
b694dfb2 389#if __OS_HAS_AGP
6ee73861
BS
390 case NOUVEAU_GART_AGP:
391 return ttm_agp_backend_init(bdev, dev->agp->bridge);
b694dfb2 392#endif
6ee73861
BS
393 case NOUVEAU_GART_SGDMA:
394 return nouveau_sgdma_init_ttm(dev);
395 default:
396 NV_ERROR(dev, "Unknown GART type %d\n",
397 dev_priv->gart_info.type);
398 break;
399 }
400
401 return NULL;
402}
403
404static int
405nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
406{
407 /* We'll do this from user space. */
408 return 0;
409}
410
411static int
412nouveau_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
413 struct ttm_mem_type_manager *man)
414{
415 struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
416 struct drm_device *dev = dev_priv->dev;
417
418 switch (type) {
419 case TTM_PL_SYSTEM:
420 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
421 man->available_caching = TTM_PL_MASK_CACHING;
422 man->default_caching = TTM_PL_FLAG_CACHED;
423 break;
424 case TTM_PL_VRAM:
d961db75 425 man->func = &ttm_bo_manager_func;
6ee73861 426 man->flags = TTM_MEMTYPE_FLAG_FIXED |
f32f02fd 427 TTM_MEMTYPE_FLAG_MAPPABLE;
6ee73861
BS
428 man->available_caching = TTM_PL_FLAG_UNCACHED |
429 TTM_PL_FLAG_WC;
430 man->default_caching = TTM_PL_FLAG_WC;
fbd2895e
BS
431 if (dev_priv->card_type == NV_50)
432 man->gpu_offset = 0x40000000;
433 else
434 man->gpu_offset = 0;
6ee73861
BS
435 break;
436 case TTM_PL_TT:
d961db75 437 man->func = &ttm_bo_manager_func;
6ee73861
BS
438 switch (dev_priv->gart_info.type) {
439 case NOUVEAU_GART_AGP:
f32f02fd 440 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
a3d487ea
FJ
441 man->available_caching = TTM_PL_FLAG_UNCACHED |
442 TTM_PL_FLAG_WC;
443 man->default_caching = TTM_PL_FLAG_WC;
6ee73861
BS
444 break;
445 case NOUVEAU_GART_SGDMA:
446 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE |
447 TTM_MEMTYPE_FLAG_CMA;
448 man->available_caching = TTM_PL_MASK_CACHING;
449 man->default_caching = TTM_PL_FLAG_CACHED;
450 break;
451 default:
452 NV_ERROR(dev, "Unknown GART type: %d\n",
453 dev_priv->gart_info.type);
454 return -EINVAL;
455 }
6ee73861
BS
456 man->gpu_offset = dev_priv->vm_gart_base;
457 break;
458 default:
459 NV_ERROR(dev, "Unsupported memory type %u\n", (unsigned)type);
460 return -EINVAL;
461 }
462 return 0;
463}
464
465static void
466nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
467{
468 struct nouveau_bo *nvbo = nouveau_bo(bo);
469
470 switch (bo->mem.mem_type) {
22fbd538 471 case TTM_PL_VRAM:
78ad0f7b
FJ
472 nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_TT,
473 TTM_PL_FLAG_SYSTEM);
22fbd538 474 break;
6ee73861 475 default:
78ad0f7b 476 nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_SYSTEM, 0);
6ee73861
BS
477 break;
478 }
22fbd538
FJ
479
480 *pl = nvbo->placement;
6ee73861
BS
481}
482
483
484/* GPU-assisted copy using NV_MEMORY_TO_MEMORY_FORMAT, can access
485 * TTM_PL_{VRAM,TT} directly.
486 */
a0af9add 487
6ee73861
BS
488static int
489nouveau_bo_move_accel_cleanup(struct nouveau_channel *chan,
9d87fa21
JG
490 struct nouveau_bo *nvbo, bool evict,
491 bool no_wait_reserve, bool no_wait_gpu,
6ee73861
BS
492 struct ttm_mem_reg *new_mem)
493{
494 struct nouveau_fence *fence = NULL;
495 int ret;
496
497 ret = nouveau_fence_new(chan, &fence, true);
498 if (ret)
499 return ret;
500
64798817 501 ret = ttm_bo_move_accel_cleanup(&nvbo->bo, fence, NULL, evict,
311ab694 502 no_wait_reserve, no_wait_gpu, new_mem);
382d62e5 503 nouveau_fence_unref(&fence);
6ee73861
BS
504 return ret;
505}
506
507static inline uint32_t
f1ab0cc9
BS
508nouveau_bo_mem_ctxdma(struct ttm_buffer_object *bo,
509 struct nouveau_channel *chan, struct ttm_mem_reg *mem)
6ee73861 510{
f1ab0cc9
BS
511 struct nouveau_bo *nvbo = nouveau_bo(bo);
512
513 if (nvbo->no_vm) {
6ee73861
BS
514 if (mem->mem_type == TTM_PL_TT)
515 return NvDmaGART;
516 return NvDmaVRAM;
517 }
518
519 if (mem->mem_type == TTM_PL_TT)
520 return chan->gart_handle;
521 return chan->vram_handle;
522}
523
524static int
f1ab0cc9
BS
525nv50_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
526 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
6ee73861 527{
6ee73861 528 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
f1ab0cc9
BS
529 struct nouveau_bo *nvbo = nouveau_bo(bo);
530 u64 length = (new_mem->num_pages << PAGE_SHIFT);
531 u64 src_offset, dst_offset;
6ee73861
BS
532 int ret;
533
d961db75
BS
534 src_offset = old_mem->start << PAGE_SHIFT;
535 dst_offset = new_mem->start << PAGE_SHIFT;
f1ab0cc9
BS
536 if (!nvbo->no_vm) {
537 if (old_mem->mem_type == TTM_PL_VRAM)
6ee73861 538 src_offset += dev_priv->vm_vram_base;
6ee73861 539 else
f1ab0cc9
BS
540 src_offset += dev_priv->vm_gart_base;
541
542 if (new_mem->mem_type == TTM_PL_VRAM)
6ee73861 543 dst_offset += dev_priv->vm_vram_base;
f1ab0cc9
BS
544 else
545 dst_offset += dev_priv->vm_gart_base;
6ee73861
BS
546 }
547
548 ret = RING_SPACE(chan, 3);
549 if (ret)
550 return ret;
6ee73861 551
f1ab0cc9
BS
552 BEGIN_RING(chan, NvSubM2MF, 0x0184, 2);
553 OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem));
554 OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem));
555
556 while (length) {
557 u32 amount, stride, height;
558
5220b3c1
BS
559 amount = min(length, (u64)(4 * 1024 * 1024));
560 stride = 16 * 4;
f1ab0cc9
BS
561 height = amount / stride;
562
f13b3263
FJ
563 if (new_mem->mem_type == TTM_PL_VRAM &&
564 nouveau_bo_tile_layout(nvbo)) {
f1ab0cc9
BS
565 ret = RING_SPACE(chan, 8);
566 if (ret)
567 return ret;
568
569 BEGIN_RING(chan, NvSubM2MF, 0x0200, 7);
570 OUT_RING (chan, 0);
5220b3c1 571 OUT_RING (chan, 0);
f1ab0cc9
BS
572 OUT_RING (chan, stride);
573 OUT_RING (chan, height);
574 OUT_RING (chan, 1);
575 OUT_RING (chan, 0);
576 OUT_RING (chan, 0);
577 } else {
578 ret = RING_SPACE(chan, 2);
579 if (ret)
580 return ret;
581
582 BEGIN_RING(chan, NvSubM2MF, 0x0200, 1);
583 OUT_RING (chan, 1);
584 }
f13b3263
FJ
585 if (old_mem->mem_type == TTM_PL_VRAM &&
586 nouveau_bo_tile_layout(nvbo)) {
f1ab0cc9
BS
587 ret = RING_SPACE(chan, 8);
588 if (ret)
589 return ret;
590
591 BEGIN_RING(chan, NvSubM2MF, 0x021c, 7);
592 OUT_RING (chan, 0);
5220b3c1 593 OUT_RING (chan, 0);
f1ab0cc9
BS
594 OUT_RING (chan, stride);
595 OUT_RING (chan, height);
596 OUT_RING (chan, 1);
597 OUT_RING (chan, 0);
598 OUT_RING (chan, 0);
599 } else {
600 ret = RING_SPACE(chan, 2);
601 if (ret)
602 return ret;
603
604 BEGIN_RING(chan, NvSubM2MF, 0x021c, 1);
605 OUT_RING (chan, 1);
606 }
607
608 ret = RING_SPACE(chan, 14);
6ee73861
BS
609 if (ret)
610 return ret;
f1ab0cc9
BS
611
612 BEGIN_RING(chan, NvSubM2MF, 0x0238, 2);
613 OUT_RING (chan, upper_32_bits(src_offset));
614 OUT_RING (chan, upper_32_bits(dst_offset));
615 BEGIN_RING(chan, NvSubM2MF, 0x030c, 8);
616 OUT_RING (chan, lower_32_bits(src_offset));
617 OUT_RING (chan, lower_32_bits(dst_offset));
618 OUT_RING (chan, stride);
619 OUT_RING (chan, stride);
620 OUT_RING (chan, stride);
621 OUT_RING (chan, height);
622 OUT_RING (chan, 0x00000101);
623 OUT_RING (chan, 0x00000000);
624 BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
625 OUT_RING (chan, 0);
626
627 length -= amount;
628 src_offset += amount;
629 dst_offset += amount;
6ee73861
BS
630 }
631
f1ab0cc9
BS
632 return 0;
633}
634
635static int
636nv04_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
637 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
638{
d961db75
BS
639 u32 src_offset = old_mem->start << PAGE_SHIFT;
640 u32 dst_offset = new_mem->start << PAGE_SHIFT;
f1ab0cc9
BS
641 u32 page_count = new_mem->num_pages;
642 int ret;
643
644 ret = RING_SPACE(chan, 3);
645 if (ret)
646 return ret;
647
648 BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_DMA_SOURCE, 2);
649 OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem));
650 OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem));
651
6ee73861
BS
652 page_count = new_mem->num_pages;
653 while (page_count) {
654 int line_count = (page_count > 2047) ? 2047 : page_count;
655
6ee73861
BS
656 ret = RING_SPACE(chan, 11);
657 if (ret)
658 return ret;
f1ab0cc9 659
6ee73861
BS
660 BEGIN_RING(chan, NvSubM2MF,
661 NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
f1ab0cc9
BS
662 OUT_RING (chan, src_offset);
663 OUT_RING (chan, dst_offset);
664 OUT_RING (chan, PAGE_SIZE); /* src_pitch */
665 OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
666 OUT_RING (chan, PAGE_SIZE); /* line_length */
667 OUT_RING (chan, line_count);
668 OUT_RING (chan, 0x00000101);
669 OUT_RING (chan, 0x00000000);
6ee73861 670 BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
f1ab0cc9 671 OUT_RING (chan, 0);
6ee73861
BS
672
673 page_count -= line_count;
674 src_offset += (PAGE_SIZE * line_count);
675 dst_offset += (PAGE_SIZE * line_count);
676 }
677
f1ab0cc9
BS
678 return 0;
679}
680
681static int
682nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict, bool intr,
683 bool no_wait_reserve, bool no_wait_gpu,
684 struct ttm_mem_reg *new_mem)
685{
686 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
687 struct nouveau_bo *nvbo = nouveau_bo(bo);
688 struct nouveau_channel *chan;
689 int ret;
690
691 chan = nvbo->channel;
6a6b73f2 692 if (!chan || nvbo->no_vm) {
f1ab0cc9 693 chan = dev_priv->channel;
e419cf09 694 mutex_lock_nested(&chan->mutex, NOUVEAU_KCHANNEL_MUTEX);
6a6b73f2 695 }
f1ab0cc9
BS
696
697 if (dev_priv->card_type < NV_50)
698 ret = nv04_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
699 else
700 ret = nv50_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
6a6b73f2
BS
701 if (ret == 0) {
702 ret = nouveau_bo_move_accel_cleanup(chan, nvbo, evict,
703 no_wait_reserve,
704 no_wait_gpu, new_mem);
705 }
f1ab0cc9 706
6a6b73f2
BS
707 if (chan == dev_priv->channel)
708 mutex_unlock(&chan->mutex);
709 return ret;
6ee73861
BS
710}
711
712static int
713nouveau_bo_move_flipd(struct ttm_buffer_object *bo, bool evict, bool intr,
9d87fa21
JG
714 bool no_wait_reserve, bool no_wait_gpu,
715 struct ttm_mem_reg *new_mem)
6ee73861
BS
716{
717 u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
718 struct ttm_placement placement;
719 struct ttm_mem_reg tmp_mem;
720 int ret;
721
722 placement.fpfn = placement.lpfn = 0;
723 placement.num_placement = placement.num_busy_placement = 1;
77e2b5ed 724 placement.placement = placement.busy_placement = &placement_memtype;
6ee73861
BS
725
726 tmp_mem = *new_mem;
727 tmp_mem.mm_node = NULL;
9d87fa21 728 ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
6ee73861
BS
729 if (ret)
730 return ret;
731
732 ret = ttm_tt_bind(bo->ttm, &tmp_mem);
733 if (ret)
734 goto out;
735
9d87fa21 736 ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_reserve, no_wait_gpu, &tmp_mem);
6ee73861
BS
737 if (ret)
738 goto out;
739
9d87fa21 740 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
6ee73861 741out:
42311ff9 742 ttm_bo_mem_put(bo, &tmp_mem);
6ee73861
BS
743 return ret;
744}
745
746static int
747nouveau_bo_move_flips(struct ttm_buffer_object *bo, bool evict, bool intr,
9d87fa21
JG
748 bool no_wait_reserve, bool no_wait_gpu,
749 struct ttm_mem_reg *new_mem)
6ee73861
BS
750{
751 u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
752 struct ttm_placement placement;
753 struct ttm_mem_reg tmp_mem;
754 int ret;
755
756 placement.fpfn = placement.lpfn = 0;
757 placement.num_placement = placement.num_busy_placement = 1;
77e2b5ed 758 placement.placement = placement.busy_placement = &placement_memtype;
6ee73861
BS
759
760 tmp_mem = *new_mem;
761 tmp_mem.mm_node = NULL;
9d87fa21 762 ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
6ee73861
BS
763 if (ret)
764 return ret;
765
9d87fa21 766 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, &tmp_mem);
6ee73861
BS
767 if (ret)
768 goto out;
769
9d87fa21 770 ret = nouveau_bo_move_m2mf(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
6ee73861
BS
771 if (ret)
772 goto out;
773
774out:
42311ff9 775 ttm_bo_mem_put(bo, &tmp_mem);
6ee73861
BS
776 return ret;
777}
778
779static int
a0af9add
FJ
780nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem,
781 struct nouveau_tile_reg **new_tile)
6ee73861
BS
782{
783 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
6ee73861 784 struct drm_device *dev = dev_priv->dev;
a0af9add
FJ
785 struct nouveau_bo *nvbo = nouveau_bo(bo);
786 uint64_t offset;
6ee73861
BS
787 int ret;
788
a0af9add
FJ
789 if (nvbo->no_vm || new_mem->mem_type != TTM_PL_VRAM) {
790 /* Nothing to do. */
791 *new_tile = NULL;
792 return 0;
793 }
794
d961db75 795 offset = new_mem->start << PAGE_SHIFT;
6ee73861 796
a0af9add 797 if (dev_priv->card_type == NV_50) {
6ee73861
BS
798 ret = nv50_mem_vm_bind_linear(dev,
799 offset + dev_priv->vm_vram_base,
f13b3263
FJ
800 new_mem->size,
801 nouveau_bo_tile_layout(nvbo),
6ee73861
BS
802 offset);
803 if (ret)
804 return ret;
a0af9add
FJ
805
806 } else if (dev_priv->card_type >= NV_10) {
807 *new_tile = nv10_mem_set_tiling(dev, offset, new_mem->size,
a5cf68b0
FJ
808 nvbo->tile_mode,
809 nvbo->tile_flags);
6ee73861
BS
810 }
811
a0af9add
FJ
812 return 0;
813}
814
815static void
816nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo,
817 struct nouveau_tile_reg *new_tile,
818 struct nouveau_tile_reg **old_tile)
819{
820 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
821 struct drm_device *dev = dev_priv->dev;
822
823 if (dev_priv->card_type >= NV_10 &&
824 dev_priv->card_type < NV_50) {
a5cf68b0 825 nv10_mem_put_tile_region(dev, *old_tile, bo->sync_obj);
a0af9add
FJ
826 *old_tile = new_tile;
827 }
828}
829
830static int
831nouveau_bo_move(struct ttm_buffer_object *bo, bool evict, bool intr,
9d87fa21
JG
832 bool no_wait_reserve, bool no_wait_gpu,
833 struct ttm_mem_reg *new_mem)
a0af9add
FJ
834{
835 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
836 struct nouveau_bo *nvbo = nouveau_bo(bo);
837 struct ttm_mem_reg *old_mem = &bo->mem;
838 struct nouveau_tile_reg *new_tile = NULL;
839 int ret = 0;
840
841 ret = nouveau_bo_vm_bind(bo, new_mem, &new_tile);
842 if (ret)
843 return ret;
844
a0af9add 845 /* Fake bo copy. */
6ee73861
BS
846 if (old_mem->mem_type == TTM_PL_SYSTEM && !bo->ttm) {
847 BUG_ON(bo->mem.mm_node != NULL);
848 bo->mem = *new_mem;
849 new_mem->mm_node = NULL;
a0af9add 850 goto out;
6ee73861
BS
851 }
852
b8a6a804
BS
853 /* Software copy if the card isn't up and running yet. */
854 if (!dev_priv->channel) {
855 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
856 goto out;
857 }
858
a0af9add
FJ
859 /* Hardware assisted copy. */
860 if (new_mem->mem_type == TTM_PL_SYSTEM)
9d87fa21 861 ret = nouveau_bo_move_flipd(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
a0af9add 862 else if (old_mem->mem_type == TTM_PL_SYSTEM)
9d87fa21 863 ret = nouveau_bo_move_flips(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
a0af9add 864 else
9d87fa21 865 ret = nouveau_bo_move_m2mf(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
6ee73861 866
a0af9add
FJ
867 if (!ret)
868 goto out;
869
870 /* Fallback to software copy. */
9d87fa21 871 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
a0af9add
FJ
872
873out:
874 if (ret)
875 nouveau_bo_vm_cleanup(bo, NULL, &new_tile);
876 else
877 nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile);
878
879 return ret;
6ee73861
BS
880}
881
882static int
883nouveau_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp)
884{
885 return 0;
886}
887
f32f02fd
JG
888static int
889nouveau_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
890{
891 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
892 struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
893 struct drm_device *dev = dev_priv->dev;
894
895 mem->bus.addr = NULL;
896 mem->bus.offset = 0;
897 mem->bus.size = mem->num_pages << PAGE_SHIFT;
898 mem->bus.base = 0;
899 mem->bus.is_iomem = false;
900 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
901 return -EINVAL;
902 switch (mem->mem_type) {
903 case TTM_PL_SYSTEM:
904 /* System memory */
905 return 0;
906 case TTM_PL_TT:
907#if __OS_HAS_AGP
908 if (dev_priv->gart_info.type == NOUVEAU_GART_AGP) {
d961db75 909 mem->bus.offset = mem->start << PAGE_SHIFT;
f32f02fd
JG
910 mem->bus.base = dev_priv->gart_info.aper_base;
911 mem->bus.is_iomem = true;
912 }
913#endif
914 break;
915 case TTM_PL_VRAM:
d961db75 916 mem->bus.offset = mem->start << PAGE_SHIFT;
01d73a69 917 mem->bus.base = pci_resource_start(dev->pdev, 1);
f32f02fd
JG
918 mem->bus.is_iomem = true;
919 break;
920 default:
921 return -EINVAL;
922 }
923 return 0;
924}
925
926static void
927nouveau_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
928{
929}
930
931static int
932nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
933{
e1429b4c
BS
934 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
935 struct nouveau_bo *nvbo = nouveau_bo(bo);
936
937 /* as long as the bo isn't in vram, and isn't tiled, we've got
938 * nothing to do here.
939 */
940 if (bo->mem.mem_type != TTM_PL_VRAM) {
f13b3263
FJ
941 if (dev_priv->card_type < NV_50 ||
942 !nouveau_bo_tile_layout(nvbo))
e1429b4c
BS
943 return 0;
944 }
945
946 /* make sure bo is in mappable vram */
d961db75 947 if (bo->mem.start + bo->mem.num_pages < dev_priv->fb_mappable_pages)
e1429b4c
BS
948 return 0;
949
950
951 nvbo->placement.fpfn = 0;
952 nvbo->placement.lpfn = dev_priv->fb_mappable_pages;
953 nouveau_bo_placement_set(nvbo, TTM_PL_VRAM, 0);
7a45d764 954 return nouveau_bo_validate(nvbo, false, true, false);
f32f02fd
JG
955}
956
332b242f
FJ
957void
958nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence)
959{
23c45e8e 960 struct nouveau_fence *old_fence;
332b242f
FJ
961
962 if (likely(fence))
23c45e8e 963 nouveau_fence_ref(fence);
332b242f 964
23c45e8e
FJ
965 spin_lock(&nvbo->bo.bdev->fence_lock);
966 old_fence = nvbo->bo.sync_obj;
967 nvbo->bo.sync_obj = fence;
332b242f 968 spin_unlock(&nvbo->bo.bdev->fence_lock);
23c45e8e
FJ
969
970 nouveau_fence_unref(&old_fence);
332b242f
FJ
971}
972
6ee73861
BS
973struct ttm_bo_driver nouveau_bo_driver = {
974 .create_ttm_backend_entry = nouveau_bo_create_ttm_backend_entry,
975 .invalidate_caches = nouveau_bo_invalidate_caches,
976 .init_mem_type = nouveau_bo_init_mem_type,
977 .evict_flags = nouveau_bo_evict_flags,
978 .move = nouveau_bo_move,
979 .verify_access = nouveau_bo_verify_access,
382d62e5
MS
980 .sync_obj_signaled = __nouveau_fence_signalled,
981 .sync_obj_wait = __nouveau_fence_wait,
982 .sync_obj_flush = __nouveau_fence_flush,
983 .sync_obj_unref = __nouveau_fence_unref,
984 .sync_obj_ref = __nouveau_fence_ref,
f32f02fd
JG
985 .fault_reserve_notify = &nouveau_ttm_fault_reserve_notify,
986 .io_mem_reserve = &nouveau_ttm_io_mem_reserve,
987 .io_mem_free = &nouveau_ttm_io_mem_free,
6ee73861
BS
988};
989