xref: /linux/drivers/gpu/drm/amd/amdkfd/kfd_migrate.c (revision 0a94608f0f7de9b1135ffea3546afe68eafef57f)
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /*
3  * Copyright 2020-2021 Advanced Micro Devices, Inc.
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 shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  */
23 #include <linux/types.h>
24 #include <linux/hmm.h>
25 #include <linux/dma-direction.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/migrate.h>
28 #include "amdgpu_sync.h"
29 #include "amdgpu_object.h"
30 #include "amdgpu_vm.h"
31 #include "amdgpu_mn.h"
32 #include "amdgpu_res_cursor.h"
33 #include "kfd_priv.h"
34 #include "kfd_svm.h"
35 #include "kfd_migrate.h"
36 #include "kfd_smi_events.h"
37 
38 #ifdef dev_fmt
39 #undef dev_fmt
40 #endif
41 #define dev_fmt(fmt) "kfd_migrate: " fmt
42 
43 static uint64_t
44 svm_migrate_direct_mapping_addr(struct amdgpu_device *adev, uint64_t addr)
45 {
46 	return addr + amdgpu_ttm_domain_start(adev, TTM_PL_VRAM);
47 }
48 
49 static int
50 svm_migrate_gart_map(struct amdgpu_ring *ring, uint64_t npages,
51 		     dma_addr_t *addr, uint64_t *gart_addr, uint64_t flags)
52 {
53 	struct amdgpu_device *adev = ring->adev;
54 	struct amdgpu_job *job;
55 	unsigned int num_dw, num_bytes;
56 	struct dma_fence *fence;
57 	uint64_t src_addr, dst_addr;
58 	uint64_t pte_flags;
59 	void *cpu_addr;
60 	int r;
61 
62 	/* use gart window 0 */
63 	*gart_addr = adev->gmc.gart_start;
64 
65 	num_dw = ALIGN(adev->mman.buffer_funcs->copy_num_dw, 8);
66 	num_bytes = npages * 8;
67 
68 	r = amdgpu_job_alloc_with_ib(adev, num_dw * 4 + num_bytes,
69 				     AMDGPU_IB_POOL_DELAYED, &job);
70 	if (r)
71 		return r;
72 
73 	src_addr = num_dw * 4;
74 	src_addr += job->ibs[0].gpu_addr;
75 
76 	dst_addr = amdgpu_bo_gpu_offset(adev->gart.bo);
77 	amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr,
78 				dst_addr, num_bytes, false);
79 
80 	amdgpu_ring_pad_ib(ring, &job->ibs[0]);
81 	WARN_ON(job->ibs[0].length_dw > num_dw);
82 
83 	pte_flags = AMDGPU_PTE_VALID | AMDGPU_PTE_READABLE;
84 	pte_flags |= AMDGPU_PTE_SYSTEM | AMDGPU_PTE_SNOOPED;
85 	if (!(flags & KFD_IOCTL_SVM_FLAG_GPU_RO))
86 		pte_flags |= AMDGPU_PTE_WRITEABLE;
87 	pte_flags |= adev->gart.gart_pte_flags;
88 
89 	cpu_addr = &job->ibs[0].ptr[num_dw];
90 
91 	amdgpu_gart_map(adev, 0, npages, addr, pte_flags, cpu_addr);
92 	r = amdgpu_job_submit(job, &adev->mman.entity,
93 			      AMDGPU_FENCE_OWNER_UNDEFINED, &fence);
94 	if (r)
95 		goto error_free;
96 
97 	dma_fence_put(fence);
98 
99 	return r;
100 
101 error_free:
102 	amdgpu_job_free(job);
103 	return r;
104 }
105 
106 /**
107  * svm_migrate_copy_memory_gart - sdma copy data between ram and vram
108  *
109  * @adev: amdgpu device the sdma ring running
110  * @sys: system DMA pointer to be copied
111  * @vram: vram destination DMA pointer
112  * @npages: number of pages to copy
113  * @direction: enum MIGRATION_COPY_DIR
114  * @mfence: output, sdma fence to signal after sdma is done
115  *
116  * ram address uses GART table continuous entries mapping to ram pages,
117  * vram address uses direct mapping of vram pages, which must have npages
118  * number of continuous pages.
119  * GART update and sdma uses same buf copy function ring, sdma is splited to
120  * multiple GTT_MAX_PAGES transfer, all sdma operations are serialized, wait for
121  * the last sdma finish fence which is returned to check copy memory is done.
122  *
123  * Context: Process context, takes and releases gtt_window_lock
124  *
125  * Return:
126  * 0 - OK, otherwise error code
127  */
128 
129 static int
130 svm_migrate_copy_memory_gart(struct amdgpu_device *adev, dma_addr_t *sys,
131 			     uint64_t *vram, uint64_t npages,
132 			     enum MIGRATION_COPY_DIR direction,
133 			     struct dma_fence **mfence)
134 {
135 	const uint64_t GTT_MAX_PAGES = AMDGPU_GTT_MAX_TRANSFER_SIZE;
136 	struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
137 	uint64_t gart_s, gart_d;
138 	struct dma_fence *next;
139 	uint64_t size;
140 	int r;
141 
142 	mutex_lock(&adev->mman.gtt_window_lock);
143 
144 	while (npages) {
145 		size = min(GTT_MAX_PAGES, npages);
146 
147 		if (direction == FROM_VRAM_TO_RAM) {
148 			gart_s = svm_migrate_direct_mapping_addr(adev, *vram);
149 			r = svm_migrate_gart_map(ring, size, sys, &gart_d, 0);
150 
151 		} else if (direction == FROM_RAM_TO_VRAM) {
152 			r = svm_migrate_gart_map(ring, size, sys, &gart_s,
153 						 KFD_IOCTL_SVM_FLAG_GPU_RO);
154 			gart_d = svm_migrate_direct_mapping_addr(adev, *vram);
155 		}
156 		if (r) {
157 			dev_err(adev->dev, "fail %d create gart mapping\n", r);
158 			goto out_unlock;
159 		}
160 
161 		r = amdgpu_copy_buffer(ring, gart_s, gart_d, size * PAGE_SIZE,
162 				       NULL, &next, false, true, false);
163 		if (r) {
164 			dev_err(adev->dev, "fail %d to copy memory\n", r);
165 			goto out_unlock;
166 		}
167 
168 		dma_fence_put(*mfence);
169 		*mfence = next;
170 		npages -= size;
171 		if (npages) {
172 			sys += size;
173 			vram += size;
174 		}
175 	}
176 
177 out_unlock:
178 	mutex_unlock(&adev->mman.gtt_window_lock);
179 
180 	return r;
181 }
182 
183 /**
184  * svm_migrate_copy_done - wait for memory copy sdma is done
185  *
186  * @adev: amdgpu device the sdma memory copy is executing on
187  * @mfence: migrate fence
188  *
189  * Wait for dma fence is signaled, if the copy ssplit into multiple sdma
190  * operations, this is the last sdma operation fence.
191  *
192  * Context: called after svm_migrate_copy_memory
193  *
194  * Return:
195  * 0		- success
196  * otherwise	- error code from dma fence signal
197  */
198 static int
199 svm_migrate_copy_done(struct amdgpu_device *adev, struct dma_fence *mfence)
200 {
201 	int r = 0;
202 
203 	if (mfence) {
204 		r = dma_fence_wait(mfence, false);
205 		dma_fence_put(mfence);
206 		pr_debug("sdma copy memory fence done\n");
207 	}
208 
209 	return r;
210 }
211 
212 unsigned long
213 svm_migrate_addr_to_pfn(struct amdgpu_device *adev, unsigned long addr)
214 {
215 	return (addr + adev->kfd.dev->pgmap.range.start) >> PAGE_SHIFT;
216 }
217 
218 static void
219 svm_migrate_get_vram_page(struct svm_range *prange, unsigned long pfn)
220 {
221 	struct page *page;
222 
223 	page = pfn_to_page(pfn);
224 	svm_range_bo_ref(prange->svm_bo);
225 	page->zone_device_data = prange->svm_bo;
226 	lock_page(page);
227 }
228 
229 static void
230 svm_migrate_put_vram_page(struct amdgpu_device *adev, unsigned long addr)
231 {
232 	struct page *page;
233 
234 	page = pfn_to_page(svm_migrate_addr_to_pfn(adev, addr));
235 	unlock_page(page);
236 	put_page(page);
237 }
238 
239 static unsigned long
240 svm_migrate_addr(struct amdgpu_device *adev, struct page *page)
241 {
242 	unsigned long addr;
243 
244 	addr = page_to_pfn(page) << PAGE_SHIFT;
245 	return (addr - adev->kfd.dev->pgmap.range.start);
246 }
247 
248 static struct page *
249 svm_migrate_get_sys_page(struct vm_area_struct *vma, unsigned long addr)
250 {
251 	struct page *page;
252 
253 	page = alloc_page_vma(GFP_HIGHUSER, vma, addr);
254 	if (page)
255 		lock_page(page);
256 
257 	return page;
258 }
259 
260 static void svm_migrate_put_sys_page(unsigned long addr)
261 {
262 	struct page *page;
263 
264 	page = pfn_to_page(addr >> PAGE_SHIFT);
265 	unlock_page(page);
266 	put_page(page);
267 }
268 
269 static unsigned long svm_migrate_successful_pages(struct migrate_vma *migrate)
270 {
271 	unsigned long cpages = 0;
272 	unsigned long i;
273 
274 	for (i = 0; i < migrate->npages; i++) {
275 		if (migrate->src[i] & MIGRATE_PFN_VALID &&
276 		    migrate->src[i] & MIGRATE_PFN_MIGRATE)
277 			cpages++;
278 	}
279 	return cpages;
280 }
281 
282 static unsigned long svm_migrate_unsuccessful_pages(struct migrate_vma *migrate)
283 {
284 	unsigned long upages = 0;
285 	unsigned long i;
286 
287 	for (i = 0; i < migrate->npages; i++) {
288 		if (migrate->src[i] & MIGRATE_PFN_VALID &&
289 		    !(migrate->src[i] & MIGRATE_PFN_MIGRATE))
290 			upages++;
291 	}
292 	return upages;
293 }
294 
295 static int
296 svm_migrate_copy_to_vram(struct amdgpu_device *adev, struct svm_range *prange,
297 			 struct migrate_vma *migrate, struct dma_fence **mfence,
298 			 dma_addr_t *scratch)
299 {
300 	uint64_t npages = migrate->npages;
301 	struct device *dev = adev->dev;
302 	struct amdgpu_res_cursor cursor;
303 	dma_addr_t *src;
304 	uint64_t *dst;
305 	uint64_t i, j;
306 	int r;
307 
308 	pr_debug("svms 0x%p [0x%lx 0x%lx]\n", prange->svms, prange->start,
309 		 prange->last);
310 
311 	src = scratch;
312 	dst = (uint64_t *)(scratch + npages);
313 
314 	r = svm_range_vram_node_new(adev, prange, true);
315 	if (r) {
316 		dev_dbg(adev->dev, "fail %d to alloc vram\n", r);
317 		goto out;
318 	}
319 
320 	amdgpu_res_first(prange->ttm_res, prange->offset << PAGE_SHIFT,
321 			 npages << PAGE_SHIFT, &cursor);
322 	for (i = j = 0; i < npages; i++) {
323 		struct page *spage;
324 
325 		spage = migrate_pfn_to_page(migrate->src[i]);
326 		if (spage && !is_zone_device_page(spage)) {
327 			dst[i] = cursor.start + (j << PAGE_SHIFT);
328 			migrate->dst[i] = svm_migrate_addr_to_pfn(adev, dst[i]);
329 			svm_migrate_get_vram_page(prange, migrate->dst[i]);
330 			migrate->dst[i] = migrate_pfn(migrate->dst[i]);
331 			src[i] = dma_map_page(dev, spage, 0, PAGE_SIZE,
332 					      DMA_TO_DEVICE);
333 			r = dma_mapping_error(dev, src[i]);
334 			if (r) {
335 				dev_err(adev->dev, "%s: fail %d dma_map_page\n",
336 					__func__, r);
337 				goto out_free_vram_pages;
338 			}
339 		} else {
340 			if (j) {
341 				r = svm_migrate_copy_memory_gart(
342 						adev, src + i - j,
343 						dst + i - j, j,
344 						FROM_RAM_TO_VRAM,
345 						mfence);
346 				if (r)
347 					goto out_free_vram_pages;
348 				amdgpu_res_next(&cursor, (j + 1) << PAGE_SHIFT);
349 				j = 0;
350 			} else {
351 				amdgpu_res_next(&cursor, PAGE_SIZE);
352 			}
353 			continue;
354 		}
355 
356 		pr_debug_ratelimited("dma mapping src to 0x%llx, pfn 0x%lx\n",
357 				     src[i] >> PAGE_SHIFT, page_to_pfn(spage));
358 
359 		if (j >= (cursor.size >> PAGE_SHIFT) - 1 && i < npages - 1) {
360 			r = svm_migrate_copy_memory_gart(adev, src + i - j,
361 							 dst + i - j, j + 1,
362 							 FROM_RAM_TO_VRAM,
363 							 mfence);
364 			if (r)
365 				goto out_free_vram_pages;
366 			amdgpu_res_next(&cursor, (j + 1) * PAGE_SIZE);
367 			j = 0;
368 		} else {
369 			j++;
370 		}
371 	}
372 
373 	r = svm_migrate_copy_memory_gart(adev, src + i - j, dst + i - j, j,
374 					 FROM_RAM_TO_VRAM, mfence);
375 
376 out_free_vram_pages:
377 	if (r) {
378 		pr_debug("failed %d to copy memory to vram\n", r);
379 		while (i--) {
380 			svm_migrate_put_vram_page(adev, dst[i]);
381 			migrate->dst[i] = 0;
382 		}
383 	}
384 
385 #ifdef DEBUG_FORCE_MIXED_DOMAINS
386 	for (i = 0, j = 0; i < npages; i += 4, j++) {
387 		if (j & 1)
388 			continue;
389 		svm_migrate_put_vram_page(adev, dst[i]);
390 		migrate->dst[i] = 0;
391 		svm_migrate_put_vram_page(adev, dst[i + 1]);
392 		migrate->dst[i + 1] = 0;
393 		svm_migrate_put_vram_page(adev, dst[i + 2]);
394 		migrate->dst[i + 2] = 0;
395 		svm_migrate_put_vram_page(adev, dst[i + 3]);
396 		migrate->dst[i + 3] = 0;
397 	}
398 #endif
399 out:
400 	return r;
401 }
402 
403 static long
404 svm_migrate_vma_to_vram(struct amdgpu_device *adev, struct svm_range *prange,
405 			struct vm_area_struct *vma, uint64_t start,
406 			uint64_t end, uint32_t trigger)
407 {
408 	struct kfd_process *p = container_of(prange->svms, struct kfd_process, svms);
409 	uint64_t npages = (end - start) >> PAGE_SHIFT;
410 	struct kfd_process_device *pdd;
411 	struct dma_fence *mfence = NULL;
412 	struct migrate_vma migrate;
413 	unsigned long cpages = 0;
414 	dma_addr_t *scratch;
415 	void *buf;
416 	int r = -ENOMEM;
417 
418 	memset(&migrate, 0, sizeof(migrate));
419 	migrate.vma = vma;
420 	migrate.start = start;
421 	migrate.end = end;
422 	migrate.flags = MIGRATE_VMA_SELECT_SYSTEM;
423 	migrate.pgmap_owner = SVM_ADEV_PGMAP_OWNER(adev);
424 
425 	buf = kvcalloc(npages,
426 		       2 * sizeof(*migrate.src) + sizeof(uint64_t) + sizeof(dma_addr_t),
427 		       GFP_KERNEL);
428 	if (!buf)
429 		goto out;
430 
431 	migrate.src = buf;
432 	migrate.dst = migrate.src + npages;
433 	scratch = (dma_addr_t *)(migrate.dst + npages);
434 
435 	kfd_smi_event_migration_start(adev->kfd.dev, p->lead_thread->pid,
436 				      start >> PAGE_SHIFT, end >> PAGE_SHIFT,
437 				      0, adev->kfd.dev->id, prange->prefetch_loc,
438 				      prange->preferred_loc, trigger);
439 
440 	r = migrate_vma_setup(&migrate);
441 	if (r) {
442 		dev_err(adev->dev, "%s: vma setup fail %d range [0x%lx 0x%lx]\n",
443 			__func__, r, prange->start, prange->last);
444 		goto out_free;
445 	}
446 
447 	cpages = migrate.cpages;
448 	if (!cpages) {
449 		pr_debug("failed collect migrate sys pages [0x%lx 0x%lx]\n",
450 			 prange->start, prange->last);
451 		goto out_free;
452 	}
453 	if (cpages != npages)
454 		pr_debug("partial migration, 0x%lx/0x%llx pages migrated\n",
455 			 cpages, npages);
456 	else
457 		pr_debug("0x%lx pages migrated\n", cpages);
458 
459 	r = svm_migrate_copy_to_vram(adev, prange, &migrate, &mfence, scratch);
460 	migrate_vma_pages(&migrate);
461 
462 	pr_debug("successful/cpages/npages 0x%lx/0x%lx/0x%lx\n",
463 		svm_migrate_successful_pages(&migrate), cpages, migrate.npages);
464 
465 	svm_migrate_copy_done(adev, mfence);
466 	migrate_vma_finalize(&migrate);
467 
468 	kfd_smi_event_migration_end(adev->kfd.dev, p->lead_thread->pid,
469 				    start >> PAGE_SHIFT, end >> PAGE_SHIFT,
470 				    0, adev->kfd.dev->id, trigger);
471 
472 	svm_range_dma_unmap(adev->dev, scratch, 0, npages);
473 	svm_range_free_dma_mappings(prange);
474 
475 out_free:
476 	kvfree(buf);
477 out:
478 	if (!r && cpages) {
479 		pdd = svm_range_get_pdd_by_adev(prange, adev);
480 		if (pdd)
481 			WRITE_ONCE(pdd->page_in, pdd->page_in + cpages);
482 
483 		return cpages;
484 	}
485 	return r;
486 }
487 
488 /**
489  * svm_migrate_ram_to_vram - migrate svm range from system to device
490  * @prange: range structure
491  * @best_loc: the device to migrate to
492  * @mm: the process mm structure
493  * @trigger: reason of migration
494  *
495  * Context: Process context, caller hold mmap read lock, svms lock, prange lock
496  *
497  * Return:
498  * 0 - OK, otherwise error code
499  */
500 static int
501 svm_migrate_ram_to_vram(struct svm_range *prange, uint32_t best_loc,
502 			struct mm_struct *mm, uint32_t trigger)
503 {
504 	unsigned long addr, start, end;
505 	struct vm_area_struct *vma;
506 	struct amdgpu_device *adev;
507 	unsigned long cpages = 0;
508 	long r = 0;
509 
510 	if (prange->actual_loc == best_loc) {
511 		pr_debug("svms 0x%p [0x%lx 0x%lx] already on best_loc 0x%x\n",
512 			 prange->svms, prange->start, prange->last, best_loc);
513 		return 0;
514 	}
515 
516 	adev = svm_range_get_adev_by_id(prange, best_loc);
517 	if (!adev) {
518 		pr_debug("failed to get device by id 0x%x\n", best_loc);
519 		return -ENODEV;
520 	}
521 
522 	pr_debug("svms 0x%p [0x%lx 0x%lx] to gpu 0x%x\n", prange->svms,
523 		 prange->start, prange->last, best_loc);
524 
525 	/* FIXME: workaround for page locking bug with invalid pages */
526 	svm_range_prefault(prange, mm, SVM_ADEV_PGMAP_OWNER(adev));
527 
528 	start = prange->start << PAGE_SHIFT;
529 	end = (prange->last + 1) << PAGE_SHIFT;
530 
531 	for (addr = start; addr < end;) {
532 		unsigned long next;
533 
534 		vma = find_vma(mm, addr);
535 		if (!vma || addr < vma->vm_start)
536 			break;
537 
538 		next = min(vma->vm_end, end);
539 		r = svm_migrate_vma_to_vram(adev, prange, vma, addr, next, trigger);
540 		if (r < 0) {
541 			pr_debug("failed %ld to migrate\n", r);
542 			break;
543 		} else {
544 			cpages += r;
545 		}
546 		addr = next;
547 	}
548 
549 	if (cpages)
550 		prange->actual_loc = best_loc;
551 
552 	return r < 0 ? r : 0;
553 }
554 
555 static void svm_migrate_page_free(struct page *page)
556 {
557 	struct svm_range_bo *svm_bo = page->zone_device_data;
558 
559 	if (svm_bo) {
560 		pr_debug_ratelimited("ref: %d\n", kref_read(&svm_bo->kref));
561 		svm_range_bo_unref_async(svm_bo);
562 	}
563 }
564 
565 static int
566 svm_migrate_copy_to_ram(struct amdgpu_device *adev, struct svm_range *prange,
567 			struct migrate_vma *migrate, struct dma_fence **mfence,
568 			dma_addr_t *scratch, uint64_t npages)
569 {
570 	struct device *dev = adev->dev;
571 	uint64_t *src;
572 	dma_addr_t *dst;
573 	struct page *dpage;
574 	uint64_t i = 0, j;
575 	uint64_t addr;
576 	int r = 0;
577 
578 	pr_debug("svms 0x%p [0x%lx 0x%lx]\n", prange->svms, prange->start,
579 		 prange->last);
580 
581 	addr = prange->start << PAGE_SHIFT;
582 
583 	src = (uint64_t *)(scratch + npages);
584 	dst = scratch;
585 
586 	for (i = 0, j = 0; i < npages; i++, addr += PAGE_SIZE) {
587 		struct page *spage;
588 
589 		spage = migrate_pfn_to_page(migrate->src[i]);
590 		if (!spage || !is_zone_device_page(spage)) {
591 			pr_debug("invalid page. Could be in CPU already svms 0x%p [0x%lx 0x%lx]\n",
592 				 prange->svms, prange->start, prange->last);
593 			if (j) {
594 				r = svm_migrate_copy_memory_gart(adev, dst + i - j,
595 								 src + i - j, j,
596 								 FROM_VRAM_TO_RAM,
597 								 mfence);
598 				if (r)
599 					goto out_oom;
600 				j = 0;
601 			}
602 			continue;
603 		}
604 		src[i] = svm_migrate_addr(adev, spage);
605 		if (j > 0 && src[i] != src[i - 1] + PAGE_SIZE) {
606 			r = svm_migrate_copy_memory_gart(adev, dst + i - j,
607 							 src + i - j, j,
608 							 FROM_VRAM_TO_RAM,
609 							 mfence);
610 			if (r)
611 				goto out_oom;
612 			j = 0;
613 		}
614 
615 		dpage = svm_migrate_get_sys_page(migrate->vma, addr);
616 		if (!dpage) {
617 			pr_debug("failed get page svms 0x%p [0x%lx 0x%lx]\n",
618 				 prange->svms, prange->start, prange->last);
619 			r = -ENOMEM;
620 			goto out_oom;
621 		}
622 
623 		dst[i] = dma_map_page(dev, dpage, 0, PAGE_SIZE, DMA_FROM_DEVICE);
624 		r = dma_mapping_error(dev, dst[i]);
625 		if (r) {
626 			dev_err(adev->dev, "%s: fail %d dma_map_page\n", __func__, r);
627 			goto out_oom;
628 		}
629 
630 		pr_debug_ratelimited("dma mapping dst to 0x%llx, pfn 0x%lx\n",
631 				     dst[i] >> PAGE_SHIFT, page_to_pfn(dpage));
632 
633 		migrate->dst[i] = migrate_pfn(page_to_pfn(dpage));
634 		j++;
635 	}
636 
637 	r = svm_migrate_copy_memory_gart(adev, dst + i - j, src + i - j, j,
638 					 FROM_VRAM_TO_RAM, mfence);
639 
640 out_oom:
641 	if (r) {
642 		pr_debug("failed %d copy to ram\n", r);
643 		while (i--) {
644 			svm_migrate_put_sys_page(dst[i]);
645 			migrate->dst[i] = 0;
646 		}
647 	}
648 
649 	return r;
650 }
651 
652 /**
653  * svm_migrate_vma_to_ram - migrate range inside one vma from device to system
654  *
655  * @adev: amdgpu device to migrate from
656  * @prange: svm range structure
657  * @vma: vm_area_struct that range [start, end] belongs to
658  * @start: range start virtual address in pages
659  * @end: range end virtual address in pages
660  *
661  * Context: Process context, caller hold mmap read lock, prange->migrate_mutex
662  *
663  * Return:
664  *   0 - success with all pages migrated
665  *   negative values - indicate error
666  *   positive values - partial migration, number of pages not migrated
667  */
668 static long
669 svm_migrate_vma_to_ram(struct amdgpu_device *adev, struct svm_range *prange,
670 		       struct vm_area_struct *vma, uint64_t start, uint64_t end,
671 		       uint32_t trigger)
672 {
673 	struct kfd_process *p = container_of(prange->svms, struct kfd_process, svms);
674 	uint64_t npages = (end - start) >> PAGE_SHIFT;
675 	unsigned long upages = npages;
676 	unsigned long cpages = 0;
677 	struct kfd_process_device *pdd;
678 	struct dma_fence *mfence = NULL;
679 	struct migrate_vma migrate;
680 	dma_addr_t *scratch;
681 	void *buf;
682 	int r = -ENOMEM;
683 
684 	memset(&migrate, 0, sizeof(migrate));
685 	migrate.vma = vma;
686 	migrate.start = start;
687 	migrate.end = end;
688 	migrate.flags = MIGRATE_VMA_SELECT_DEVICE_PRIVATE;
689 	migrate.pgmap_owner = SVM_ADEV_PGMAP_OWNER(adev);
690 
691 	buf = kvcalloc(npages,
692 		       2 * sizeof(*migrate.src) + sizeof(uint64_t) + sizeof(dma_addr_t),
693 		       GFP_KERNEL);
694 
695 	if (!buf)
696 		goto out;
697 
698 	migrate.src = buf;
699 	migrate.dst = migrate.src + npages;
700 	scratch = (dma_addr_t *)(migrate.dst + npages);
701 
702 	kfd_smi_event_migration_start(adev->kfd.dev, p->lead_thread->pid,
703 				      start >> PAGE_SHIFT, end >> PAGE_SHIFT,
704 				      adev->kfd.dev->id, 0, prange->prefetch_loc,
705 				      prange->preferred_loc, trigger);
706 
707 	r = migrate_vma_setup(&migrate);
708 	if (r) {
709 		dev_err(adev->dev, "%s: vma setup fail %d range [0x%lx 0x%lx]\n",
710 			__func__, r, prange->start, prange->last);
711 		goto out_free;
712 	}
713 
714 	cpages = migrate.cpages;
715 	if (!cpages) {
716 		pr_debug("failed collect migrate device pages [0x%lx 0x%lx]\n",
717 			 prange->start, prange->last);
718 		upages = svm_migrate_unsuccessful_pages(&migrate);
719 		goto out_free;
720 	}
721 	if (cpages != npages)
722 		pr_debug("partial migration, 0x%lx/0x%llx pages migrated\n",
723 			 cpages, npages);
724 	else
725 		pr_debug("0x%lx pages migrated\n", cpages);
726 
727 	r = svm_migrate_copy_to_ram(adev, prange, &migrate, &mfence,
728 				    scratch, npages);
729 	migrate_vma_pages(&migrate);
730 
731 	upages = svm_migrate_unsuccessful_pages(&migrate);
732 	pr_debug("unsuccessful/cpages/npages 0x%lx/0x%lx/0x%lx\n",
733 		 upages, cpages, migrate.npages);
734 
735 	svm_migrate_copy_done(adev, mfence);
736 	migrate_vma_finalize(&migrate);
737 
738 	kfd_smi_event_migration_end(adev->kfd.dev, p->lead_thread->pid,
739 				    start >> PAGE_SHIFT, end >> PAGE_SHIFT,
740 				    adev->kfd.dev->id, 0, trigger);
741 
742 	svm_range_dma_unmap(adev->dev, scratch, 0, npages);
743 
744 out_free:
745 	kvfree(buf);
746 out:
747 	if (!r && cpages) {
748 		pdd = svm_range_get_pdd_by_adev(prange, adev);
749 		if (pdd)
750 			WRITE_ONCE(pdd->page_out, pdd->page_out + cpages);
751 	}
752 	return r ? r : upages;
753 }
754 
755 /**
756  * svm_migrate_vram_to_ram - migrate svm range from device to system
757  * @prange: range structure
758  * @mm: process mm, use current->mm if NULL
759  * @trigger: reason of migration
760  *
761  * Context: Process context, caller hold mmap read lock, prange->migrate_mutex
762  *
763  * Return:
764  * 0 - OK, otherwise error code
765  */
766 int svm_migrate_vram_to_ram(struct svm_range *prange, struct mm_struct *mm,
767 			    uint32_t trigger)
768 {
769 	struct amdgpu_device *adev;
770 	struct vm_area_struct *vma;
771 	unsigned long addr;
772 	unsigned long start;
773 	unsigned long end;
774 	unsigned long upages = 0;
775 	long r = 0;
776 
777 	if (!prange->actual_loc) {
778 		pr_debug("[0x%lx 0x%lx] already migrated to ram\n",
779 			 prange->start, prange->last);
780 		return 0;
781 	}
782 
783 	adev = svm_range_get_adev_by_id(prange, prange->actual_loc);
784 	if (!adev) {
785 		pr_debug("failed to get device by id 0x%x\n",
786 			 prange->actual_loc);
787 		return -ENODEV;
788 	}
789 
790 	pr_debug("svms 0x%p prange 0x%p [0x%lx 0x%lx] from gpu 0x%x to ram\n",
791 		 prange->svms, prange, prange->start, prange->last,
792 		 prange->actual_loc);
793 
794 	start = prange->start << PAGE_SHIFT;
795 	end = (prange->last + 1) << PAGE_SHIFT;
796 
797 	for (addr = start; addr < end;) {
798 		unsigned long next;
799 
800 		vma = find_vma(mm, addr);
801 		if (!vma || addr < vma->vm_start) {
802 			pr_debug("failed to find vma for prange %p\n", prange);
803 			r = -EFAULT;
804 			break;
805 		}
806 
807 		next = min(vma->vm_end, end);
808 		r = svm_migrate_vma_to_ram(adev, prange, vma, addr, next, trigger);
809 		if (r < 0) {
810 			pr_debug("failed %ld to migrate prange %p\n", r, prange);
811 			break;
812 		} else {
813 			upages += r;
814 		}
815 		addr = next;
816 	}
817 
818 	if (r >= 0 && !upages) {
819 		svm_range_vram_node_free(prange);
820 		prange->actual_loc = 0;
821 	}
822 
823 	return r < 0 ? r : 0;
824 }
825 
826 /**
827  * svm_migrate_vram_to_vram - migrate svm range from device to device
828  * @prange: range structure
829  * @best_loc: the device to migrate to
830  * @mm: process mm, use current->mm if NULL
831  * @trigger: reason of migration
832  *
833  * Context: Process context, caller hold mmap read lock, svms lock, prange lock
834  *
835  * Return:
836  * 0 - OK, otherwise error code
837  */
838 static int
839 svm_migrate_vram_to_vram(struct svm_range *prange, uint32_t best_loc,
840 			 struct mm_struct *mm, uint32_t trigger)
841 {
842 	int r, retries = 3;
843 
844 	/*
845 	 * TODO: for both devices with PCIe large bar or on same xgmi hive, skip
846 	 * system memory as migration bridge
847 	 */
848 
849 	pr_debug("from gpu 0x%x to gpu 0x%x\n", prange->actual_loc, best_loc);
850 
851 	do {
852 		r = svm_migrate_vram_to_ram(prange, mm, trigger);
853 		if (r)
854 			return r;
855 	} while (prange->actual_loc && --retries);
856 
857 	if (prange->actual_loc)
858 		return -EDEADLK;
859 
860 	return svm_migrate_ram_to_vram(prange, best_loc, mm, trigger);
861 }
862 
863 int
864 svm_migrate_to_vram(struct svm_range *prange, uint32_t best_loc,
865 		    struct mm_struct *mm, uint32_t trigger)
866 {
867 	if  (!prange->actual_loc)
868 		return svm_migrate_ram_to_vram(prange, best_loc, mm, trigger);
869 	else
870 		return svm_migrate_vram_to_vram(prange, best_loc, mm, trigger);
871 
872 }
873 
874 /**
875  * svm_migrate_to_ram - CPU page fault handler
876  * @vmf: CPU vm fault vma, address
877  *
878  * Context: vm fault handler, caller holds the mmap read lock
879  *
880  * Return:
881  * 0 - OK
882  * VM_FAULT_SIGBUS - notice application to have SIGBUS page fault
883  */
884 static vm_fault_t svm_migrate_to_ram(struct vm_fault *vmf)
885 {
886 	unsigned long addr = vmf->address;
887 	struct vm_area_struct *vma;
888 	enum svm_work_list_ops op;
889 	struct svm_range *parent;
890 	struct svm_range *prange;
891 	struct kfd_process *p;
892 	struct mm_struct *mm;
893 	int r = 0;
894 
895 	vma = vmf->vma;
896 	mm = vma->vm_mm;
897 
898 	p = kfd_lookup_process_by_mm(vma->vm_mm);
899 	if (!p) {
900 		pr_debug("failed find process at fault address 0x%lx\n", addr);
901 		return VM_FAULT_SIGBUS;
902 	}
903 	if (READ_ONCE(p->svms.faulting_task) == current) {
904 		pr_debug("skipping ram migration\n");
905 		kfd_unref_process(p);
906 		return 0;
907 	}
908 	addr >>= PAGE_SHIFT;
909 	pr_debug("CPU page fault svms 0x%p address 0x%lx\n", &p->svms, addr);
910 
911 	mutex_lock(&p->svms.lock);
912 
913 	prange = svm_range_from_addr(&p->svms, addr, &parent);
914 	if (!prange) {
915 		pr_debug("cannot find svm range at 0x%lx\n", addr);
916 		r = -EFAULT;
917 		goto out;
918 	}
919 
920 	mutex_lock(&parent->migrate_mutex);
921 	if (prange != parent)
922 		mutex_lock_nested(&prange->migrate_mutex, 1);
923 
924 	if (!prange->actual_loc)
925 		goto out_unlock_prange;
926 
927 	svm_range_lock(parent);
928 	if (prange != parent)
929 		mutex_lock_nested(&prange->lock, 1);
930 	r = svm_range_split_by_granularity(p, mm, addr, parent, prange);
931 	if (prange != parent)
932 		mutex_unlock(&prange->lock);
933 	svm_range_unlock(parent);
934 	if (r) {
935 		pr_debug("failed %d to split range by granularity\n", r);
936 		goto out_unlock_prange;
937 	}
938 
939 	r = svm_migrate_vram_to_ram(prange, mm, KFD_MIGRATE_TRIGGER_PAGEFAULT_CPU);
940 	if (r)
941 		pr_debug("failed %d migrate 0x%p [0x%lx 0x%lx] to ram\n", r,
942 			 prange, prange->start, prange->last);
943 
944 	/* xnack on, update mapping on GPUs with ACCESS_IN_PLACE */
945 	if (p->xnack_enabled && parent == prange)
946 		op = SVM_OP_UPDATE_RANGE_NOTIFIER_AND_MAP;
947 	else
948 		op = SVM_OP_UPDATE_RANGE_NOTIFIER;
949 	svm_range_add_list_work(&p->svms, parent, mm, op);
950 	schedule_deferred_list_work(&p->svms);
951 
952 out_unlock_prange:
953 	if (prange != parent)
954 		mutex_unlock(&prange->migrate_mutex);
955 	mutex_unlock(&parent->migrate_mutex);
956 out:
957 	mutex_unlock(&p->svms.lock);
958 	kfd_unref_process(p);
959 
960 	pr_debug("CPU fault svms 0x%p address 0x%lx done\n", &p->svms, addr);
961 
962 	return r ? VM_FAULT_SIGBUS : 0;
963 }
964 
965 static const struct dev_pagemap_ops svm_migrate_pgmap_ops = {
966 	.page_free		= svm_migrate_page_free,
967 	.migrate_to_ram		= svm_migrate_to_ram,
968 };
969 
970 /* Each VRAM page uses sizeof(struct page) on system memory */
971 #define SVM_HMM_PAGE_STRUCT_SIZE(size) ((size)/PAGE_SIZE * sizeof(struct page))
972 
973 int svm_migrate_init(struct amdgpu_device *adev)
974 {
975 	struct kfd_dev *kfddev = adev->kfd.dev;
976 	struct dev_pagemap *pgmap;
977 	struct resource *res;
978 	unsigned long size;
979 	void *r;
980 
981 	/* Page migration works on Vega10 or newer */
982 	if (!KFD_IS_SOC15(kfddev))
983 		return -EINVAL;
984 
985 	pgmap = &kfddev->pgmap;
986 	memset(pgmap, 0, sizeof(*pgmap));
987 
988 	/* TODO: register all vram to HMM for now.
989 	 * should remove reserved size
990 	 */
991 	size = ALIGN(adev->gmc.real_vram_size, 2ULL << 20);
992 	res = devm_request_free_mem_region(adev->dev, &iomem_resource, size);
993 	if (IS_ERR(res))
994 		return -ENOMEM;
995 
996 	pgmap->type = MEMORY_DEVICE_PRIVATE;
997 	pgmap->nr_range = 1;
998 	pgmap->range.start = res->start;
999 	pgmap->range.end = res->end;
1000 	pgmap->ops = &svm_migrate_pgmap_ops;
1001 	pgmap->owner = SVM_ADEV_PGMAP_OWNER(adev);
1002 	pgmap->flags = MIGRATE_VMA_SELECT_DEVICE_PRIVATE;
1003 
1004 	/* Device manager releases device-specific resources, memory region and
1005 	 * pgmap when driver disconnects from device.
1006 	 */
1007 	r = devm_memremap_pages(adev->dev, pgmap);
1008 	if (IS_ERR(r)) {
1009 		pr_err("failed to register HMM device memory\n");
1010 
1011 		/* Disable SVM support capability */
1012 		pgmap->type = 0;
1013 		devm_release_mem_region(adev->dev, res->start, resource_size(res));
1014 		return PTR_ERR(r);
1015 	}
1016 
1017 	pr_debug("reserve %ldMB system memory for VRAM pages struct\n",
1018 		 SVM_HMM_PAGE_STRUCT_SIZE(size) >> 20);
1019 
1020 	amdgpu_amdkfd_reserve_system_mem(SVM_HMM_PAGE_STRUCT_SIZE(size));
1021 
1022 	pr_info("HMM registered %ldMB device memory\n", size >> 20);
1023 
1024 	return 0;
1025 }
1026