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)];
69 struct nouveau_dmem_migrate {
70 nouveau_migrate_copy_t copy_func;
71 struct nouveau_channel *chan;
75 struct hmm_devmem *devmem;
76 struct nouveau_dmem_migrate migrate;
77 struct list_head chunk_free;
78 struct list_head chunk_full;
79 struct list_head chunk_empty;
83 struct nouveau_dmem_fault {
84 struct nouveau_drm *drm;
85 struct nouveau_fence *fence;
90 struct nouveau_migrate {
91 struct vm_area_struct *vma;
92 struct nouveau_drm *drm;
93 struct nouveau_fence *fence;
100 nouveau_dmem_free(struct hmm_devmem *devmem, struct page *page)
102 struct nouveau_dmem_chunk *chunk;
103 struct nouveau_drm *drm;
106 chunk = (void *)hmm_devmem_page_get_drvdata(page);
107 idx = page_to_pfn(page) - chunk->pfn_first;
113 * This is really a bad example, we need to overhaul nouveau memory
114 * management to be more page focus and allow lighter locking scheme
115 * to be use in the process.
117 spin_lock(&chunk->lock);
118 clear_bit(idx, chunk->bitmap);
119 WARN_ON(!chunk->callocated);
122 * FIXME when chunk->callocated reach 0 we should add the chunk to
123 * a reclaim list so that it can be freed in case of memory pressure.
125 spin_unlock(&chunk->lock);
129 nouveau_dmem_fault_alloc_and_copy(struct vm_area_struct *vma,
130 const unsigned long *src_pfns,
131 unsigned long *dst_pfns,
136 struct nouveau_dmem_fault *fault = private;
137 struct nouveau_drm *drm = fault->drm;
138 struct device *dev = drm->dev->dev;
139 unsigned long addr, i, npages = 0;
140 nouveau_migrate_copy_t copy;
144 /* First allocate new memory */
145 for (addr = start, i = 0; addr < end; addr += PAGE_SIZE, i++) {
146 struct page *dpage, *spage;
149 spage = migrate_pfn_to_page(src_pfns[i]);
150 if (!spage || !(src_pfns[i] & MIGRATE_PFN_MIGRATE))
153 dpage = hmm_vma_alloc_locked_page(vma, addr);
155 dst_pfns[i] = MIGRATE_PFN_ERROR;
159 dst_pfns[i] = migrate_pfn(page_to_pfn(dpage)) |
164 /* Allocate storage for DMA addresses, so we can unmap later. */
165 fault->dma = kmalloc(sizeof(*fault->dma) * npages, GFP_KERNEL);
169 /* Copy things over */
170 copy = drm->dmem->migrate.copy_func;
171 for (addr = start, i = 0; addr < end; addr += PAGE_SIZE, i++) {
172 struct nouveau_dmem_chunk *chunk;
173 struct page *spage, *dpage;
174 u64 src_addr, dst_addr;
176 dpage = migrate_pfn_to_page(dst_pfns[i]);
177 if (!dpage || dst_pfns[i] == MIGRATE_PFN_ERROR)
180 spage = migrate_pfn_to_page(src_pfns[i]);
181 if (!spage || !(src_pfns[i] & MIGRATE_PFN_MIGRATE)) {
182 dst_pfns[i] = MIGRATE_PFN_ERROR;
187 fault->dma[fault->npages] =
188 dma_map_page_attrs(dev, dpage, 0, PAGE_SIZE,
189 PCI_DMA_BIDIRECTIONAL,
190 DMA_ATTR_SKIP_CPU_SYNC);
191 if (dma_mapping_error(dev, fault->dma[fault->npages])) {
192 dst_pfns[i] = MIGRATE_PFN_ERROR;
197 dst_addr = fault->dma[fault->npages++];
199 chunk = (void *)hmm_devmem_page_get_drvdata(spage);
200 src_addr = page_to_pfn(spage) - chunk->pfn_first;
201 src_addr = (src_addr << PAGE_SHIFT) + chunk->bo->bo.offset;
203 ret = copy(drm, 1, NOUVEAU_APER_HOST, dst_addr,
204 NOUVEAU_APER_VRAM, src_addr);
206 dst_pfns[i] = MIGRATE_PFN_ERROR;
212 nouveau_fence_new(drm->dmem->migrate.chan, false, &fault->fence);
217 for (addr = start, i = 0; addr < end; addr += PAGE_SIZE, ++i) {
220 if (!dst_pfns[i] || dst_pfns[i] == MIGRATE_PFN_ERROR)
223 page = migrate_pfn_to_page(dst_pfns[i]);
224 dst_pfns[i] = MIGRATE_PFN_ERROR;
232 void nouveau_dmem_fault_finalize_and_map(struct vm_area_struct *vma,
233 const unsigned long *src_pfns,
234 const unsigned long *dst_pfns,
239 struct nouveau_dmem_fault *fault = private;
240 struct nouveau_drm *drm = fault->drm;
243 nouveau_fence_wait(fault->fence, true, false);
244 nouveau_fence_unref(&fault->fence);
247 * FIXME wait for channel to be IDLE before calling finalizing
248 * the hmem object below (nouveau_migrate_hmem_fini()).
252 while (fault->npages--) {
253 dma_unmap_page(drm->dev->dev, fault->dma[fault->npages],
254 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
259 static const struct migrate_vma_ops nouveau_dmem_fault_migrate_ops = {
260 .alloc_and_copy = nouveau_dmem_fault_alloc_and_copy,
261 .finalize_and_map = nouveau_dmem_fault_finalize_and_map,
265 nouveau_dmem_fault(struct hmm_devmem *devmem,
266 struct vm_area_struct *vma,
268 const struct page *page,
272 struct drm_device *drm_dev = dev_get_drvdata(devmem->device);
273 unsigned long src[1] = {0}, dst[1] = {0};
274 struct nouveau_dmem_fault fault = {0};
280 * FIXME what we really want is to find some heuristic to migrate more
281 * than just one page on CPU fault. When such fault happens it is very
282 * likely that more surrounding page will CPU fault too.
284 fault.drm = nouveau_drm(drm_dev);
285 ret = migrate_vma(&nouveau_dmem_fault_migrate_ops, vma, addr,
286 addr + PAGE_SIZE, src, dst, &fault);
288 return VM_FAULT_SIGBUS;
290 if (dst[0] == MIGRATE_PFN_ERROR)
291 return VM_FAULT_SIGBUS;
296 static const struct hmm_devmem_ops
297 nouveau_dmem_devmem_ops = {
298 .free = nouveau_dmem_free,
299 .fault = nouveau_dmem_fault,
303 nouveau_dmem_chunk_alloc(struct nouveau_drm *drm)
305 struct nouveau_dmem_chunk *chunk;
308 if (drm->dmem == NULL)
311 mutex_lock(&drm->dmem->mutex);
312 chunk = list_first_entry_or_null(&drm->dmem->chunk_empty,
313 struct nouveau_dmem_chunk,
316 mutex_unlock(&drm->dmem->mutex);
320 list_del(&chunk->list);
321 mutex_unlock(&drm->dmem->mutex);
323 ret = nouveau_bo_new(&drm->client, DMEM_CHUNK_SIZE, 0,
324 TTM_PL_FLAG_VRAM, 0, 0, NULL, NULL,
329 ret = nouveau_bo_pin(chunk->bo, TTM_PL_FLAG_VRAM, false);
331 nouveau_bo_ref(NULL, &chunk->bo);
335 bitmap_zero(chunk->bitmap, DMEM_CHUNK_NPAGES);
336 spin_lock_init(&chunk->lock);
339 mutex_lock(&drm->dmem->mutex);
341 list_add(&chunk->list, &drm->dmem->chunk_empty);
343 list_add_tail(&chunk->list, &drm->dmem->chunk_empty);
344 mutex_unlock(&drm->dmem->mutex);
349 static struct nouveau_dmem_chunk *
350 nouveau_dmem_chunk_first_free_locked(struct nouveau_drm *drm)
352 struct nouveau_dmem_chunk *chunk;
354 chunk = list_first_entry_or_null(&drm->dmem->chunk_free,
355 struct nouveau_dmem_chunk,
360 chunk = list_first_entry_or_null(&drm->dmem->chunk_empty,
361 struct nouveau_dmem_chunk,
370 nouveau_dmem_pages_alloc(struct nouveau_drm *drm,
371 unsigned long npages,
372 unsigned long *pages)
374 struct nouveau_dmem_chunk *chunk;
378 memset(pages, 0xff, npages * sizeof(*pages));
380 mutex_lock(&drm->dmem->mutex);
381 for (c = 0; c < npages;) {
384 chunk = nouveau_dmem_chunk_first_free_locked(drm);
386 mutex_unlock(&drm->dmem->mutex);
387 ret = nouveau_dmem_chunk_alloc(drm);
396 spin_lock(&chunk->lock);
397 i = find_first_zero_bit(chunk->bitmap, DMEM_CHUNK_NPAGES);
398 while (i < DMEM_CHUNK_NPAGES && c < npages) {
399 pages[c] = chunk->pfn_first + i;
400 set_bit(i, chunk->bitmap);
404 i = find_next_zero_bit(chunk->bitmap,
405 DMEM_CHUNK_NPAGES, i);
407 spin_unlock(&chunk->lock);
409 mutex_unlock(&drm->dmem->mutex);
415 nouveau_dmem_page_alloc_locked(struct nouveau_drm *drm)
417 unsigned long pfns[1];
421 /* FIXME stop all the miss-match API ... */
422 ret = nouveau_dmem_pages_alloc(drm, 1, pfns);
426 page = pfn_to_page(pfns[0]);
433 nouveau_dmem_page_free_locked(struct nouveau_drm *drm, struct page *page)
440 nouveau_dmem_resume(struct nouveau_drm *drm)
442 struct nouveau_dmem_chunk *chunk;
445 if (drm->dmem == NULL)
448 mutex_lock(&drm->dmem->mutex);
449 list_for_each_entry (chunk, &drm->dmem->chunk_free, list) {
450 ret = nouveau_bo_pin(chunk->bo, TTM_PL_FLAG_VRAM, false);
451 /* FIXME handle pin failure */
454 list_for_each_entry (chunk, &drm->dmem->chunk_full, list) {
455 ret = nouveau_bo_pin(chunk->bo, TTM_PL_FLAG_VRAM, false);
456 /* FIXME handle pin failure */
459 list_for_each_entry (chunk, &drm->dmem->chunk_empty, list) {
460 ret = nouveau_bo_pin(chunk->bo, TTM_PL_FLAG_VRAM, false);
461 /* FIXME handle pin failure */
464 mutex_unlock(&drm->dmem->mutex);
468 nouveau_dmem_suspend(struct nouveau_drm *drm)
470 struct nouveau_dmem_chunk *chunk;
472 if (drm->dmem == NULL)
475 mutex_lock(&drm->dmem->mutex);
476 list_for_each_entry (chunk, &drm->dmem->chunk_free, list) {
477 nouveau_bo_unpin(chunk->bo);
479 list_for_each_entry (chunk, &drm->dmem->chunk_full, list) {
480 nouveau_bo_unpin(chunk->bo);
482 list_for_each_entry (chunk, &drm->dmem->chunk_empty, list) {
483 nouveau_bo_unpin(chunk->bo);
485 mutex_unlock(&drm->dmem->mutex);
489 nouveau_dmem_fini(struct nouveau_drm *drm)
491 struct nouveau_dmem_chunk *chunk, *tmp;
493 if (drm->dmem == NULL)
496 mutex_lock(&drm->dmem->mutex);
498 WARN_ON(!list_empty(&drm->dmem->chunk_free));
499 WARN_ON(!list_empty(&drm->dmem->chunk_full));
501 list_for_each_entry_safe (chunk, tmp, &drm->dmem->chunk_empty, list) {
503 nouveau_bo_unpin(chunk->bo);
504 nouveau_bo_ref(NULL, &chunk->bo);
506 list_del(&chunk->list);
510 mutex_unlock(&drm->dmem->mutex);
514 nvc0b5_migrate_copy(struct nouveau_drm *drm, u64 npages,
515 enum nouveau_aper dst_aper, u64 dst_addr,
516 enum nouveau_aper src_aper, u64 src_addr)
518 struct nouveau_channel *chan = drm->dmem->migrate.chan;
519 u32 launch_dma = (1 << 9) /* MULTI_LINE_ENABLE. */ |
520 (1 << 8) /* DST_MEMORY_LAYOUT_PITCH. */ |
521 (1 << 7) /* SRC_MEMORY_LAYOUT_PITCH. */ |
522 (1 << 2) /* FLUSH_ENABLE_TRUE. */ |
523 (2 << 0) /* DATA_TRANSFER_TYPE_NON_PIPELINED. */;
526 ret = RING_SPACE(chan, 13);
530 if (src_aper != NOUVEAU_APER_VIRT) {
532 case NOUVEAU_APER_VRAM:
533 BEGIN_IMC0(chan, NvSubCopy, 0x0260, 0);
535 case NOUVEAU_APER_HOST:
536 BEGIN_IMC0(chan, NvSubCopy, 0x0260, 1);
541 launch_dma |= 0x00001000; /* SRC_TYPE_PHYSICAL. */
544 if (dst_aper != NOUVEAU_APER_VIRT) {
546 case NOUVEAU_APER_VRAM:
547 BEGIN_IMC0(chan, NvSubCopy, 0x0264, 0);
549 case NOUVEAU_APER_HOST:
550 BEGIN_IMC0(chan, NvSubCopy, 0x0264, 1);
555 launch_dma |= 0x00002000; /* DST_TYPE_PHYSICAL. */
558 BEGIN_NVC0(chan, NvSubCopy, 0x0400, 8);
559 OUT_RING (chan, upper_32_bits(src_addr));
560 OUT_RING (chan, lower_32_bits(src_addr));
561 OUT_RING (chan, upper_32_bits(dst_addr));
562 OUT_RING (chan, lower_32_bits(dst_addr));
563 OUT_RING (chan, PAGE_SIZE);
564 OUT_RING (chan, PAGE_SIZE);
565 OUT_RING (chan, PAGE_SIZE);
566 OUT_RING (chan, npages);
567 BEGIN_NVC0(chan, NvSubCopy, 0x0300, 1);
568 OUT_RING (chan, launch_dma);
573 nouveau_dmem_migrate_init(struct nouveau_drm *drm)
575 switch (drm->ttm.copy.oclass) {
576 case PASCAL_DMA_COPY_A:
577 case PASCAL_DMA_COPY_B:
578 case VOLTA_DMA_COPY_A:
579 case TURING_DMA_COPY_A:
580 drm->dmem->migrate.copy_func = nvc0b5_migrate_copy;
581 drm->dmem->migrate.chan = drm->ttm.chan;
590 nouveau_dmem_init(struct nouveau_drm *drm)
592 struct device *device = drm->dev->dev;
593 unsigned long i, size;
596 /* This only make sense on PASCAL or newer */
597 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_PASCAL)
600 if (!(drm->dmem = kzalloc(sizeof(*drm->dmem), GFP_KERNEL)))
603 mutex_init(&drm->dmem->mutex);
604 INIT_LIST_HEAD(&drm->dmem->chunk_free);
605 INIT_LIST_HEAD(&drm->dmem->chunk_full);
606 INIT_LIST_HEAD(&drm->dmem->chunk_empty);
608 size = ALIGN(drm->client.device.info.ram_user, DMEM_CHUNK_SIZE);
610 /* Initialize migration dma helpers before registering memory */
611 ret = nouveau_dmem_migrate_init(drm);
619 * FIXME we need some kind of policy to decide how much VRAM we
620 * want to register with HMM. For now just register everything
621 * and latter if we want to do thing like over commit then we
622 * could revisit this.
624 drm->dmem->devmem = hmm_devmem_add(&nouveau_dmem_devmem_ops,
626 if (drm->dmem->devmem == NULL) {
632 for (i = 0; i < (size / DMEM_CHUNK_SIZE); ++i) {
633 struct nouveau_dmem_chunk *chunk;
637 chunk = kzalloc(sizeof(*chunk), GFP_KERNEL);
639 nouveau_dmem_fini(drm);
644 chunk->pfn_first = drm->dmem->devmem->pfn_first;
645 chunk->pfn_first += (i * DMEM_CHUNK_NPAGES);
646 list_add_tail(&chunk->list, &drm->dmem->chunk_empty);
648 page = pfn_to_page(chunk->pfn_first);
649 for (j = 0; j < DMEM_CHUNK_NPAGES; ++j, ++page) {
650 hmm_devmem_page_set_drvdata(page, (long)chunk);
654 NV_INFO(drm, "DMEM: registered %ldMB of device memory\n", size >> 20);
658 nouveau_dmem_migrate_alloc_and_copy(struct vm_area_struct *vma,
659 const unsigned long *src_pfns,
660 unsigned long *dst_pfns,
665 struct nouveau_migrate *migrate = private;
666 struct nouveau_drm *drm = migrate->drm;
667 struct device *dev = drm->dev->dev;
668 unsigned long addr, i, npages = 0;
669 nouveau_migrate_copy_t copy;
672 /* First allocate new memory */
673 for (addr = start, i = 0; addr < end; addr += PAGE_SIZE, i++) {
674 struct page *dpage, *spage;
677 spage = migrate_pfn_to_page(src_pfns[i]);
678 if (!spage || !(src_pfns[i] & MIGRATE_PFN_MIGRATE))
681 dpage = nouveau_dmem_page_alloc_locked(drm);
685 dst_pfns[i] = migrate_pfn(page_to_pfn(dpage)) |
694 /* Allocate storage for DMA addresses, so we can unmap later. */
695 migrate->dma = kmalloc(sizeof(*migrate->dma) * npages, GFP_KERNEL);
699 /* Copy things over */
700 copy = drm->dmem->migrate.copy_func;
701 for (addr = start, i = 0; addr < end; addr += PAGE_SIZE, i++) {
702 struct nouveau_dmem_chunk *chunk;
703 struct page *spage, *dpage;
704 u64 src_addr, dst_addr;
706 dpage = migrate_pfn_to_page(dst_pfns[i]);
707 if (!dpage || dst_pfns[i] == MIGRATE_PFN_ERROR)
710 chunk = (void *)hmm_devmem_page_get_drvdata(dpage);
711 dst_addr = page_to_pfn(dpage) - chunk->pfn_first;
712 dst_addr = (dst_addr << PAGE_SHIFT) + chunk->bo->bo.offset;
714 spage = migrate_pfn_to_page(src_pfns[i]);
715 if (!spage || !(src_pfns[i] & MIGRATE_PFN_MIGRATE)) {
716 nouveau_dmem_page_free_locked(drm, dpage);
721 migrate->dma[migrate->dma_nr] =
722 dma_map_page_attrs(dev, spage, 0, PAGE_SIZE,
723 PCI_DMA_BIDIRECTIONAL,
724 DMA_ATTR_SKIP_CPU_SYNC);
725 if (dma_mapping_error(dev, migrate->dma[migrate->dma_nr])) {
726 nouveau_dmem_page_free_locked(drm, dpage);
731 src_addr = migrate->dma[migrate->dma_nr++];
733 ret = copy(drm, 1, NOUVEAU_APER_VRAM, dst_addr,
734 NOUVEAU_APER_HOST, src_addr);
736 nouveau_dmem_page_free_locked(drm, dpage);
742 nouveau_fence_new(drm->dmem->migrate.chan, false, &migrate->fence);
747 for (addr = start, i = 0; addr < end; addr += PAGE_SIZE, ++i) {
750 if (!dst_pfns[i] || dst_pfns[i] == MIGRATE_PFN_ERROR)
753 page = migrate_pfn_to_page(dst_pfns[i]);
754 dst_pfns[i] = MIGRATE_PFN_ERROR;
762 void nouveau_dmem_migrate_finalize_and_map(struct vm_area_struct *vma,
763 const unsigned long *src_pfns,
764 const unsigned long *dst_pfns,
769 struct nouveau_migrate *migrate = private;
770 struct nouveau_drm *drm = migrate->drm;
772 if (migrate->fence) {
773 nouveau_fence_wait(migrate->fence, true, false);
774 nouveau_fence_unref(&migrate->fence);
777 * FIXME wait for channel to be IDLE before finalizing
778 * the hmem object below (nouveau_migrate_hmem_fini()) ?
782 while (migrate->dma_nr--) {
783 dma_unmap_page(drm->dev->dev, migrate->dma[migrate->dma_nr],
784 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
789 * FIXME optimization: update GPU page table to point to newly
794 static const struct migrate_vma_ops nouveau_dmem_migrate_ops = {
795 .alloc_and_copy = nouveau_dmem_migrate_alloc_and_copy,
796 .finalize_and_map = nouveau_dmem_migrate_finalize_and_map,
800 nouveau_dmem_migrate_vma(struct nouveau_drm *drm,
801 struct vm_area_struct *vma,
805 unsigned long *src_pfns, *dst_pfns, npages;
806 struct nouveau_migrate migrate = {0};
807 unsigned long i, c, max;
810 npages = (end - start) >> PAGE_SHIFT;
811 max = min(SG_MAX_SINGLE_ALLOC, npages);
812 src_pfns = kzalloc(sizeof(long) * max, GFP_KERNEL);
813 if (src_pfns == NULL)
815 dst_pfns = kzalloc(sizeof(long) * max, GFP_KERNEL);
816 if (dst_pfns == NULL) {
823 migrate.npages = npages;
824 for (i = 0; i < npages; i += c) {
827 c = min(SG_MAX_SINGLE_ALLOC, npages);
828 next = start + (c << PAGE_SHIFT);
829 ret = migrate_vma(&nouveau_dmem_migrate_ops, vma, start,
830 next, src_pfns, dst_pfns, &migrate);
843 nouveau_dmem_page(struct nouveau_drm *drm, struct page *page)
845 if (!is_device_private_page(page))
848 if (drm->dmem->devmem != page->pgmap->data)
855 nouveau_dmem_convert_pfn(struct nouveau_drm *drm,
856 struct hmm_range *range)
858 unsigned long i, npages;
860 npages = (range->end - range->start) >> PAGE_SHIFT;
861 for (i = 0; i < npages; ++i) {
862 struct nouveau_dmem_chunk *chunk;
866 page = hmm_pfn_to_page(range, range->pfns[i]);
870 if (!(range->pfns[i] & range->flags[HMM_PFN_DEVICE_PRIVATE])) {
874 if (!nouveau_dmem_page(drm, page)) {
875 WARN(1, "Some unknown device memory !\n");
880 chunk = (void *)hmm_devmem_page_get_drvdata(page);
881 addr = page_to_pfn(page) - chunk->pfn_first;
882 addr = (addr + chunk->bo->bo.mem.start) << PAGE_SHIFT;
884 range->pfns[i] &= ((1UL << range->pfn_shift) - 1);
885 range->pfns[i] |= (addr >> PAGE_SHIFT) << range->pfn_shift;