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