2 * Copyright 2018 Red Hat Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
22 #include "nouveau_dmem.h"
23 #include "nouveau_drv.h"
24 #include "nouveau_chan.h"
25 #include "nouveau_dma.h"
26 #include "nouveau_mem.h"
27 #include "nouveau_bo.h"
29 #include <nvif/class.h>
30 #include <nvif/object.h>
31 #include <nvif/if500b.h>
32 #include <nvif/if900b.h>
34 #include <linux/sched/mm.h>
35 #include <linux/hmm.h>
38 * FIXME: this is ugly right now we are using TTM to allocate vram and we pin
39 * it in vram while in use. We likely want to overhaul memory management for
40 * nouveau to be more page like (not necessarily with system page size but a
41 * bigger page size) at lowest level and have some shim layer on top that would
42 * provide the same functionality as TTM.
44 #define DMEM_CHUNK_SIZE (2UL << 20)
45 #define DMEM_CHUNK_NPAGES (DMEM_CHUNK_SIZE >> PAGE_SHIFT)
47 struct nouveau_migrate;
55 typedef int (*nouveau_migrate_copy_t)(struct nouveau_drm *drm, u64 npages,
56 enum nouveau_aper, u64 dst_addr,
57 enum nouveau_aper, u64 src_addr);
59 struct nouveau_dmem_chunk {
60 struct list_head list;
61 struct nouveau_bo *bo;
62 struct nouveau_drm *drm;
63 unsigned long pfn_first;
64 unsigned long callocated;
65 unsigned long bitmap[BITS_TO_LONGS(DMEM_CHUNK_NPAGES)];
70 struct nouveau_dmem_migrate {
71 nouveau_migrate_copy_t copy_func;
72 struct nouveau_channel *chan;
76 struct hmm_devmem *devmem;
77 struct nouveau_dmem_migrate migrate;
78 struct list_head chunk_free;
79 struct list_head chunk_full;
80 struct list_head chunk_empty;
84 struct nouveau_migrate_hmem {
85 struct scatterlist *sg;
86 struct nouveau_mem mem;
91 struct nouveau_dmem_fault {
92 struct nouveau_drm *drm;
93 struct nouveau_fence *fence;
94 struct nouveau_migrate_hmem hmem;
97 struct nouveau_migrate {
98 struct vm_area_struct *vma;
99 struct nouveau_drm *drm;
100 struct nouveau_fence *fence;
101 unsigned long npages;
102 struct nouveau_migrate_hmem hmem;
106 nouveau_migrate_hmem_fini(struct nouveau_drm *drm,
107 struct nouveau_migrate_hmem *hmem)
109 struct nvif_vmm *vmm = &drm->client.vmm.vmm;
111 nouveau_mem_fini(&hmem->mem);
112 nvif_vmm_put(vmm, &hmem->vma);
115 dma_unmap_sg_attrs(drm->dev->dev, hmem->sg,
116 hmem->npages, DMA_BIDIRECTIONAL,
117 DMA_ATTR_SKIP_CPU_SYNC);
124 nouveau_migrate_hmem_init(struct nouveau_drm *drm,
125 struct nouveau_migrate_hmem *hmem,
126 unsigned long npages,
127 const unsigned long *pfns)
129 struct nvif_vmm *vmm = &drm->client.vmm.vmm;
133 hmem->sg = kzalloc(npages * sizeof(*hmem->sg), GFP_KERNEL);
134 if (hmem->sg == NULL)
137 for (i = 0, hmem->npages = 0; hmem->npages < npages; ++i) {
140 if (!pfns[i] || pfns[i] == MIGRATE_PFN_ERROR)
143 page = migrate_pfn_to_page(pfns[i]);
149 sg_set_page(&hmem->sg[hmem->npages], page, PAGE_SIZE, 0);
152 sg_mark_end(&hmem->sg[hmem->npages - 1]);
154 i = dma_map_sg_attrs(drm->dev->dev, hmem->sg, hmem->npages,
155 DMA_BIDIRECTIONAL, DMA_ATTR_SKIP_CPU_SYNC);
156 if (i != hmem->npages) {
161 ret = nouveau_mem_sgl(&hmem->mem, &drm->client,
162 hmem->npages, hmem->sg);
166 ret = nvif_vmm_get(vmm, LAZY, false, hmem->mem.mem.page,
167 0, hmem->mem.mem.size, &hmem->vma);
171 ret = nouveau_mem_map(&hmem->mem, vmm, &hmem->vma);
178 nouveau_migrate_hmem_fini(drm, hmem);
184 nouveau_dmem_free(struct hmm_devmem *devmem, struct page *page)
186 struct nouveau_dmem_chunk *chunk;
187 struct nouveau_drm *drm;
190 chunk = (void *)hmm_devmem_page_get_drvdata(page);
191 idx = page_to_pfn(page) - chunk->pfn_first;
197 * This is really a bad example, we need to overhaul nouveau memory
198 * management to be more page focus and allow lighter locking scheme
199 * to be use in the process.
201 spin_lock(&chunk->lock);
202 clear_bit(idx, chunk->bitmap);
203 WARN_ON(!chunk->callocated);
206 * FIXME when chunk->callocated reach 0 we should add the chunk to
207 * a reclaim list so that it can be freed in case of memory pressure.
209 spin_unlock(&chunk->lock);
213 nouveau_dmem_fault_alloc_and_copy(struct vm_area_struct *vma,
214 const unsigned long *src_pfns,
215 unsigned long *dst_pfns,
220 struct nouveau_dmem_fault *fault = private;
221 struct nouveau_drm *drm = fault->drm;
222 unsigned long addr, i, c, npages = 0;
223 nouveau_migrate_copy_t copy;
227 /* First allocate new memory */
228 for (addr = start, i = 0; addr < end; addr += PAGE_SIZE, i++) {
229 struct page *dpage, *spage;
232 spage = migrate_pfn_to_page(src_pfns[i]);
233 if (!spage || !(src_pfns[i] & MIGRATE_PFN_MIGRATE))
236 dpage = hmm_vma_alloc_locked_page(vma, addr);
238 dst_pfns[i] = MIGRATE_PFN_ERROR;
242 dst_pfns[i] = migrate_pfn(page_to_pfn(dpage)) |
247 /* Create scatter list FIXME: get rid of scatter list */
248 ret = nouveau_migrate_hmem_init(drm, &fault->hmem, npages, dst_pfns);
252 /* Copy things over */
253 copy = drm->dmem->migrate.copy_func;
254 for (addr = start, i = c = 0; addr < end; addr += PAGE_SIZE, i++) {
255 struct nouveau_dmem_chunk *chunk;
256 struct page *spage, *dpage;
257 u64 src_addr, dst_addr;
259 dpage = migrate_pfn_to_page(dst_pfns[i]);
260 if (!dpage || dst_pfns[i] == MIGRATE_PFN_ERROR)
263 dst_addr = fault->hmem.vma.addr + (c << PAGE_SHIFT);
266 spage = migrate_pfn_to_page(src_pfns[i]);
267 if (!spage || !(src_pfns[i] & MIGRATE_PFN_MIGRATE)) {
268 dst_pfns[i] = MIGRATE_PFN_ERROR;
273 chunk = (void *)hmm_devmem_page_get_drvdata(spage);
274 src_addr = page_to_pfn(spage) - chunk->pfn_first;
275 src_addr = (src_addr << PAGE_SHIFT) + chunk->vma.addr;
277 ret = copy(drm, 1, NOUVEAU_APER_VIRT, dst_addr,
278 NOUVEAU_APER_VIRT, src_addr);
280 dst_pfns[i] = MIGRATE_PFN_ERROR;
286 nouveau_fence_new(drm->dmem->migrate.chan, false, &fault->fence);
291 for (addr = start, i = 0; addr < end; addr += PAGE_SIZE, ++i) {
294 if (!dst_pfns[i] || dst_pfns[i] == MIGRATE_PFN_ERROR)
297 page = migrate_pfn_to_page(dst_pfns[i]);
298 dst_pfns[i] = MIGRATE_PFN_ERROR;
306 void nouveau_dmem_fault_finalize_and_map(struct vm_area_struct *vma,
307 const unsigned long *src_pfns,
308 const unsigned long *dst_pfns,
313 struct nouveau_dmem_fault *fault = private;
314 struct nouveau_drm *drm = fault->drm;
317 nouveau_fence_wait(fault->fence, true, false);
318 nouveau_fence_unref(&fault->fence);
321 * FIXME wait for channel to be IDLE before calling finalizing
322 * the hmem object below (nouveau_migrate_hmem_fini()).
325 nouveau_migrate_hmem_fini(drm, &fault->hmem);
328 static const struct migrate_vma_ops nouveau_dmem_fault_migrate_ops = {
329 .alloc_and_copy = nouveau_dmem_fault_alloc_and_copy,
330 .finalize_and_map = nouveau_dmem_fault_finalize_and_map,
334 nouveau_dmem_fault(struct hmm_devmem *devmem,
335 struct vm_area_struct *vma,
337 const struct page *page,
341 struct drm_device *drm_dev = dev_get_drvdata(devmem->device);
342 unsigned long src[1] = {0}, dst[1] = {0};
343 struct nouveau_dmem_fault fault = {0};
349 * FIXME what we really want is to find some heuristic to migrate more
350 * than just one page on CPU fault. When such fault happens it is very
351 * likely that more surrounding page will CPU fault too.
353 fault.drm = nouveau_drm(drm_dev);
354 ret = migrate_vma(&nouveau_dmem_fault_migrate_ops, vma, addr,
355 addr + PAGE_SIZE, src, dst, &fault);
357 return VM_FAULT_SIGBUS;
359 if (dst[0] == MIGRATE_PFN_ERROR)
360 return VM_FAULT_SIGBUS;
365 static const struct hmm_devmem_ops
366 nouveau_dmem_devmem_ops = {
367 .free = nouveau_dmem_free,
368 .fault = nouveau_dmem_fault,
372 nouveau_dmem_chunk_alloc(struct nouveau_drm *drm)
374 struct nvif_vmm *vmm = &drm->client.vmm.vmm;
375 struct nouveau_dmem_chunk *chunk;
378 if (drm->dmem == NULL)
381 mutex_lock(&drm->dmem->mutex);
382 chunk = list_first_entry_or_null(&drm->dmem->chunk_empty,
383 struct nouveau_dmem_chunk,
386 mutex_unlock(&drm->dmem->mutex);
390 list_del(&chunk->list);
391 mutex_unlock(&drm->dmem->mutex);
393 ret = nvif_vmm_get(vmm, LAZY, false, 12, 0,
394 DMEM_CHUNK_SIZE, &chunk->vma);
398 ret = nouveau_bo_new(&drm->client, DMEM_CHUNK_SIZE, 0,
399 TTM_PL_FLAG_VRAM, 0, 0, NULL, NULL,
404 ret = nouveau_bo_pin(chunk->bo, TTM_PL_FLAG_VRAM, false);
406 nouveau_bo_ref(NULL, &chunk->bo);
410 ret = nouveau_mem_map(nouveau_mem(&chunk->bo->bo.mem), vmm, &chunk->vma);
412 nouveau_bo_unpin(chunk->bo);
413 nouveau_bo_ref(NULL, &chunk->bo);
417 bitmap_zero(chunk->bitmap, DMEM_CHUNK_NPAGES);
418 spin_lock_init(&chunk->lock);
421 mutex_lock(&drm->dmem->mutex);
423 list_add(&chunk->list, &drm->dmem->chunk_empty);
425 list_add_tail(&chunk->list, &drm->dmem->chunk_empty);
426 mutex_unlock(&drm->dmem->mutex);
431 static struct nouveau_dmem_chunk *
432 nouveau_dmem_chunk_first_free_locked(struct nouveau_drm *drm)
434 struct nouveau_dmem_chunk *chunk;
436 chunk = list_first_entry_or_null(&drm->dmem->chunk_free,
437 struct nouveau_dmem_chunk,
442 chunk = list_first_entry_or_null(&drm->dmem->chunk_empty,
443 struct nouveau_dmem_chunk,
452 nouveau_dmem_pages_alloc(struct nouveau_drm *drm,
453 unsigned long npages,
454 unsigned long *pages)
456 struct nouveau_dmem_chunk *chunk;
460 memset(pages, 0xff, npages * sizeof(*pages));
462 mutex_lock(&drm->dmem->mutex);
463 for (c = 0; c < npages;) {
466 chunk = nouveau_dmem_chunk_first_free_locked(drm);
468 mutex_unlock(&drm->dmem->mutex);
469 ret = nouveau_dmem_chunk_alloc(drm);
478 spin_lock(&chunk->lock);
479 i = find_first_zero_bit(chunk->bitmap, DMEM_CHUNK_NPAGES);
480 while (i < DMEM_CHUNK_NPAGES && c < npages) {
481 pages[c] = chunk->pfn_first + i;
482 set_bit(i, chunk->bitmap);
486 i = find_next_zero_bit(chunk->bitmap,
487 DMEM_CHUNK_NPAGES, i);
489 spin_unlock(&chunk->lock);
491 mutex_unlock(&drm->dmem->mutex);
497 nouveau_dmem_page_alloc_locked(struct nouveau_drm *drm)
499 unsigned long pfns[1];
503 /* FIXME stop all the miss-match API ... */
504 ret = nouveau_dmem_pages_alloc(drm, 1, pfns);
508 page = pfn_to_page(pfns[0]);
515 nouveau_dmem_page_free_locked(struct nouveau_drm *drm, struct page *page)
522 nouveau_dmem_resume(struct nouveau_drm *drm)
524 struct nouveau_dmem_chunk *chunk;
527 if (drm->dmem == NULL)
530 mutex_lock(&drm->dmem->mutex);
531 list_for_each_entry (chunk, &drm->dmem->chunk_free, list) {
532 ret = nouveau_bo_pin(chunk->bo, TTM_PL_FLAG_VRAM, false);
533 /* FIXME handle pin failure */
536 list_for_each_entry (chunk, &drm->dmem->chunk_full, list) {
537 ret = nouveau_bo_pin(chunk->bo, TTM_PL_FLAG_VRAM, false);
538 /* FIXME handle pin failure */
541 list_for_each_entry (chunk, &drm->dmem->chunk_empty, list) {
542 ret = nouveau_bo_pin(chunk->bo, TTM_PL_FLAG_VRAM, false);
543 /* FIXME handle pin failure */
546 mutex_unlock(&drm->dmem->mutex);
550 nouveau_dmem_suspend(struct nouveau_drm *drm)
552 struct nouveau_dmem_chunk *chunk;
554 if (drm->dmem == NULL)
557 mutex_lock(&drm->dmem->mutex);
558 list_for_each_entry (chunk, &drm->dmem->chunk_free, list) {
559 nouveau_bo_unpin(chunk->bo);
561 list_for_each_entry (chunk, &drm->dmem->chunk_full, list) {
562 nouveau_bo_unpin(chunk->bo);
564 list_for_each_entry (chunk, &drm->dmem->chunk_empty, list) {
565 nouveau_bo_unpin(chunk->bo);
567 mutex_unlock(&drm->dmem->mutex);
571 nouveau_dmem_fini(struct nouveau_drm *drm)
573 struct nvif_vmm *vmm = &drm->client.vmm.vmm;
574 struct nouveau_dmem_chunk *chunk, *tmp;
576 if (drm->dmem == NULL)
579 mutex_lock(&drm->dmem->mutex);
581 WARN_ON(!list_empty(&drm->dmem->chunk_free));
582 WARN_ON(!list_empty(&drm->dmem->chunk_full));
584 list_for_each_entry_safe (chunk, tmp, &drm->dmem->chunk_empty, list) {
586 nouveau_bo_unpin(chunk->bo);
587 nouveau_bo_ref(NULL, &chunk->bo);
589 nvif_vmm_put(vmm, &chunk->vma);
590 list_del(&chunk->list);
594 mutex_unlock(&drm->dmem->mutex);
598 nvc0b5_migrate_copy(struct nouveau_drm *drm, u64 npages,
599 enum nouveau_aper dst_aper, u64 dst_addr,
600 enum nouveau_aper src_aper, u64 src_addr)
602 struct nouveau_channel *chan = drm->dmem->migrate.chan;
603 u32 launch_dma = (1 << 9) /* MULTI_LINE_ENABLE. */ |
604 (1 << 8) /* DST_MEMORY_LAYOUT_PITCH. */ |
605 (1 << 7) /* SRC_MEMORY_LAYOUT_PITCH. */ |
606 (1 << 2) /* FLUSH_ENABLE_TRUE. */ |
607 (2 << 0) /* DATA_TRANSFER_TYPE_NON_PIPELINED. */;
610 ret = RING_SPACE(chan, 13);
614 if (src_aper != NOUVEAU_APER_VIRT) {
616 case NOUVEAU_APER_VRAM:
617 BEGIN_IMC0(chan, NvSubCopy, 0x0260, 0);
619 case NOUVEAU_APER_HOST:
620 BEGIN_IMC0(chan, NvSubCopy, 0x0260, 1);
625 launch_dma |= 0x00001000; /* SRC_TYPE_PHYSICAL. */
628 if (dst_aper != NOUVEAU_APER_VIRT) {
630 case NOUVEAU_APER_VRAM:
631 BEGIN_IMC0(chan, NvSubCopy, 0x0264, 0);
633 case NOUVEAU_APER_HOST:
634 BEGIN_IMC0(chan, NvSubCopy, 0x0264, 1);
639 launch_dma |= 0x00002000; /* DST_TYPE_PHYSICAL. */
642 BEGIN_NVC0(chan, NvSubCopy, 0x0400, 8);
643 OUT_RING (chan, upper_32_bits(src_addr));
644 OUT_RING (chan, lower_32_bits(src_addr));
645 OUT_RING (chan, upper_32_bits(dst_addr));
646 OUT_RING (chan, lower_32_bits(dst_addr));
647 OUT_RING (chan, PAGE_SIZE);
648 OUT_RING (chan, PAGE_SIZE);
649 OUT_RING (chan, PAGE_SIZE);
650 OUT_RING (chan, npages);
651 BEGIN_NVC0(chan, NvSubCopy, 0x0300, 1);
652 OUT_RING (chan, launch_dma);
657 nouveau_dmem_migrate_init(struct nouveau_drm *drm)
659 switch (drm->ttm.copy.oclass) {
660 case PASCAL_DMA_COPY_A:
661 case PASCAL_DMA_COPY_B:
662 case VOLTA_DMA_COPY_A:
663 case TURING_DMA_COPY_A:
664 drm->dmem->migrate.copy_func = nvc0b5_migrate_copy;
665 drm->dmem->migrate.chan = drm->ttm.chan;
674 nouveau_dmem_init(struct nouveau_drm *drm)
676 struct device *device = drm->dev->dev;
677 unsigned long i, size;
680 /* This only make sense on PASCAL or newer */
681 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_PASCAL)
684 if (!(drm->dmem = kzalloc(sizeof(*drm->dmem), GFP_KERNEL)))
687 mutex_init(&drm->dmem->mutex);
688 INIT_LIST_HEAD(&drm->dmem->chunk_free);
689 INIT_LIST_HEAD(&drm->dmem->chunk_full);
690 INIT_LIST_HEAD(&drm->dmem->chunk_empty);
692 size = ALIGN(drm->client.device.info.ram_user, DMEM_CHUNK_SIZE);
694 /* Initialize migration dma helpers before registering memory */
695 ret = nouveau_dmem_migrate_init(drm);
703 * FIXME we need some kind of policy to decide how much VRAM we
704 * want to register with HMM. For now just register everything
705 * and latter if we want to do thing like over commit then we
706 * could revisit this.
708 drm->dmem->devmem = hmm_devmem_add(&nouveau_dmem_devmem_ops,
710 if (drm->dmem->devmem == NULL) {
716 for (i = 0; i < (size / DMEM_CHUNK_SIZE); ++i) {
717 struct nouveau_dmem_chunk *chunk;
721 chunk = kzalloc(sizeof(*chunk), GFP_KERNEL);
723 nouveau_dmem_fini(drm);
728 chunk->pfn_first = drm->dmem->devmem->pfn_first;
729 chunk->pfn_first += (i * DMEM_CHUNK_NPAGES);
730 list_add_tail(&chunk->list, &drm->dmem->chunk_empty);
732 page = pfn_to_page(chunk->pfn_first);
733 for (j = 0; j < DMEM_CHUNK_NPAGES; ++j, ++page) {
734 hmm_devmem_page_set_drvdata(page, (long)chunk);
738 NV_INFO(drm, "DMEM: registered %ldMB of device memory\n", size >> 20);
742 nouveau_dmem_migrate_alloc_and_copy(struct vm_area_struct *vma,
743 const unsigned long *src_pfns,
744 unsigned long *dst_pfns,
749 struct nouveau_migrate *migrate = private;
750 struct nouveau_drm *drm = migrate->drm;
751 unsigned long addr, i, c, npages = 0;
752 nouveau_migrate_copy_t copy;
755 /* First allocate new memory */
756 for (addr = start, i = 0; addr < end; addr += PAGE_SIZE, i++) {
757 struct page *dpage, *spage;
760 spage = migrate_pfn_to_page(src_pfns[i]);
761 if (!spage || !(src_pfns[i] & MIGRATE_PFN_MIGRATE))
764 dpage = nouveau_dmem_page_alloc_locked(drm);
768 dst_pfns[i] = migrate_pfn(page_to_pfn(dpage)) |
777 /* Create scatter list FIXME: get rid of scatter list */
778 ret = nouveau_migrate_hmem_init(drm, &migrate->hmem, npages, src_pfns);
782 /* Copy things over */
783 copy = drm->dmem->migrate.copy_func;
784 for (addr = start, i = c = 0; addr < end; addr += PAGE_SIZE, i++) {
785 struct nouveau_dmem_chunk *chunk;
786 struct page *spage, *dpage;
787 u64 src_addr, dst_addr;
789 dpage = migrate_pfn_to_page(dst_pfns[i]);
790 if (!dpage || dst_pfns[i] == MIGRATE_PFN_ERROR)
793 chunk = (void *)hmm_devmem_page_get_drvdata(dpage);
794 dst_addr = page_to_pfn(dpage) - chunk->pfn_first;
795 dst_addr = (dst_addr << PAGE_SHIFT) + chunk->vma.addr;
797 spage = migrate_pfn_to_page(src_pfns[i]);
798 if (!spage || !(src_pfns[i] & MIGRATE_PFN_MIGRATE)) {
799 nouveau_dmem_page_free_locked(drm, dpage);
804 src_addr = migrate->hmem.vma.addr + (c << PAGE_SHIFT);
807 ret = copy(drm, 1, NOUVEAU_APER_VIRT, dst_addr,
808 NOUVEAU_APER_VIRT, src_addr);
810 nouveau_dmem_page_free_locked(drm, dpage);
816 nouveau_fence_new(drm->dmem->migrate.chan, false, &migrate->fence);
821 for (addr = start, i = 0; addr < end; addr += PAGE_SIZE, ++i) {
824 if (!dst_pfns[i] || dst_pfns[i] == MIGRATE_PFN_ERROR)
827 page = migrate_pfn_to_page(dst_pfns[i]);
828 dst_pfns[i] = MIGRATE_PFN_ERROR;
836 void nouveau_dmem_migrate_finalize_and_map(struct vm_area_struct *vma,
837 const unsigned long *src_pfns,
838 const unsigned long *dst_pfns,
843 struct nouveau_migrate *migrate = private;
844 struct nouveau_drm *drm = migrate->drm;
846 if (migrate->fence) {
847 nouveau_fence_wait(migrate->fence, true, false);
848 nouveau_fence_unref(&migrate->fence);
851 * FIXME wait for channel to be IDLE before finalizing
852 * the hmem object below (nouveau_migrate_hmem_fini()) ?
855 nouveau_migrate_hmem_fini(drm, &migrate->hmem);
858 * FIXME optimization: update GPU page table to point to newly
863 static const struct migrate_vma_ops nouveau_dmem_migrate_ops = {
864 .alloc_and_copy = nouveau_dmem_migrate_alloc_and_copy,
865 .finalize_and_map = nouveau_dmem_migrate_finalize_and_map,
869 nouveau_dmem_migrate_vma(struct nouveau_drm *drm,
870 struct vm_area_struct *vma,
874 unsigned long *src_pfns, *dst_pfns, npages;
875 struct nouveau_migrate migrate = {0};
876 unsigned long i, c, max;
879 npages = (end - start) >> PAGE_SHIFT;
880 max = min(SG_MAX_SINGLE_ALLOC, npages);
881 src_pfns = kzalloc(sizeof(long) * max, GFP_KERNEL);
882 if (src_pfns == NULL)
884 dst_pfns = kzalloc(sizeof(long) * max, GFP_KERNEL);
885 if (dst_pfns == NULL) {
892 migrate.npages = npages;
893 for (i = 0; i < npages; i += c) {
896 c = min(SG_MAX_SINGLE_ALLOC, npages);
897 next = start + (c << PAGE_SHIFT);
898 ret = migrate_vma(&nouveau_dmem_migrate_ops, vma, start,
899 next, src_pfns, dst_pfns, &migrate);
912 nouveau_dmem_page(struct nouveau_drm *drm, struct page *page)
914 if (!is_device_private_page(page))
917 if (drm->dmem->devmem != page->pgmap->data)
924 nouveau_dmem_convert_pfn(struct nouveau_drm *drm,
925 struct hmm_range *range)
927 unsigned long i, npages;
929 npages = (range->end - range->start) >> PAGE_SHIFT;
930 for (i = 0; i < npages; ++i) {
931 struct nouveau_dmem_chunk *chunk;
935 page = hmm_pfn_to_page(range, range->pfns[i]);
939 if (!(range->pfns[i] & range->flags[HMM_PFN_DEVICE_PRIVATE])) {
943 if (!nouveau_dmem_page(drm, page)) {
944 WARN(1, "Some unknown device memory !\n");
949 chunk = (void *)hmm_devmem_page_get_drvdata(page);
950 addr = page_to_pfn(page) - chunk->pfn_first;
951 addr = (addr + chunk->bo->bo.mem.start) << PAGE_SHIFT;
953 range->pfns[i] &= ((1UL << range->pfn_shift) - 1);
954 range->pfns[i] |= (addr >> PAGE_SHIFT) << range->pfn_shift;