Add amdgpu_svm_range_migrate.c/.h implementing the per-range migration helpers for SVM VRAM migration via drm_pagemap:
- Implement range_needs_migrate_to_vram() checking migrate_devmem capability and current backing location. - Add amdgpu_svm_range_migrate_to_vram() wrapping drm_pagemap_populate_mm() for RAM-to-VRAM migration. - Add amdgpu_pagemap_capable() to check device memory support. Signed-off-by: Junhua Shen <[email protected]> --- drivers/gpu/drm/amd/amdgpu/Makefile | 6 +- .../drm/amd/amdgpu/amdgpu_svm_range_migrate.c | 115 ++++++++++++++++++ .../drm/amd/amdgpu/amdgpu_svm_range_migrate.h | 35 ++++++ 3 files changed, 153 insertions(+), 3 deletions(-) create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_svm_range_migrate.c create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_svm_range_migrate.h diff --git a/drivers/gpu/drm/amd/amdgpu/Makefile b/drivers/gpu/drm/amd/amdgpu/Makefile index 08207cad310d..d644079f3e55 100644 --- a/drivers/gpu/drm/amd/amdgpu/Makefile +++ b/drivers/gpu/drm/amd/amdgpu/Makefile @@ -328,14 +328,14 @@ amdgpu-$(CONFIG_HMM_MIRROR) += amdgpu_hmm.o # svm support amdgpu-$(CONFIG_DRM_AMDGPU_SVM) += amdgpu_svm.o amdgpu_svm_attr.o \ - amdgpu_svm_fault.o amdgpu_svm_range.o amdgpu_userptr.o amdgpu_migrate.o + amdgpu_svm_fault.o amdgpu_svm_range.o amdgpu_svm_range_migrate.o amdgpu_userptr.o amdgpu_migrate.o .PHONY: clean-svm clean-svm: rm -f $(obj)/amdgpu_svm.o $(obj)/amdgpu_svm_attr.o $(obj)/amdgpu_svm_fault.o $(obj)/amdgpu_svm_range.o \ - $(obj)/amdgpu_userptr.o $(obj)/amdgpu_migrate.o \ + $(obj)/amdgpu_svm_range_migrate.o $(obj)/amdgpu_userptr.o $(obj)/amdgpu_migrate.o \ $(obj)/.amdgpu_svm.o.cmd $(obj)/.amdgpu_svm_attr.o.cmd $(obj)/.amdgpu_svm_fault.o.cmd $(obj)/.amdgpu_svm_range.o.cmd \ - $(obj)/.amdgpu_userptr.o.cmd $(obj)/.amdgpu_migrate.o.cmd + $(obj)/.amdgpu_svm_range_migrate.o.cmd $(obj)/.amdgpu_userptr.o.cmd $(obj)/.amdgpu_migrate.o.cmd include $(FULL_AMD_PATH)/pm/Makefile diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_svm_range_migrate.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_svm_range_migrate.c new file mode 100644 index 000000000000..e51657c2a65c --- /dev/null +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_svm_range_migrate.c @@ -0,0 +1,115 @@ +// SPDX-License-Identifier: GPL-2.0 OR MIT +/* + * Copyright 2026 Advanced Micro Devices, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + */ + +#include "amdgpu.h" +#include "amdgpu_svm.h" +#include "amdgpu_svm_range.h" +#include "amdgpu_migrate.h" +#include "amdgpu_svm_range_migrate.h" + +static bool +range_in_vram(struct drm_gpusvm_range *range) +{ + struct drm_gpusvm_pages_flags flags = { + /* Pairs with WRITE_ONCE in drm_gpusvm_get_pages() */ + .__flags = READ_ONCE(range->pages.flags.__flags), + }; + + return flags.has_devmem_pages; +} + +static bool +range_needs_migrate_to_vram(struct drm_gpusvm_range *range) +{ + if (!range->pages.flags.migrate_devmem) + return false; + if (range_in_vram(range)) + return false; + + return true; +} + +static struct drm_pagemap * +amdgpu_svm_get_dpagemap(struct amdgpu_svm *svm) +{ + struct amdgpu_pagemap *apagemap = svm->adev->apagemap; + + if (!apagemap || !apagemap->initialized) + return NULL; + + return &apagemap->dpagemap; +} + +bool +amdgpu_pagemap_capable(struct amdgpu_svm *svm) +{ + if (svm->adev->gmc.is_app_apu) + return false; + + if (!amdgpu_svm_get_dpagemap(svm)) + return false; + + return true; +} + +/** + * amdgpu_svm_range_migrate_to_vram - Migrate range pages to VRAM + * @svm: Pointer to the AMDGPU SVM structure + * @range: The GPU SVM range to migrate + * + * Wraps drm_pagemap_populate_mm() to migrate system memory pages + * backing the given range into device VRAM. Skips migration if the + * range doesn't support devmem or is already in VRAM. + * + * Return: 0 on success or no-op, negative error code on failure + */ +int +amdgpu_svm_range_migrate_to_vram(struct amdgpu_svm *svm, + struct drm_gpusvm_range *range) +{ + struct drm_pagemap *dpagemap; + unsigned long start, end; + int ret; + + if (!range_needs_migrate_to_vram(range)) + return 0; + + dpagemap = amdgpu_svm_get_dpagemap(svm); + if (!dpagemap) + return -ENODEV; + + start = drm_gpusvm_range_start(range); + end = drm_gpusvm_range_end(range); + + ret = drm_pagemap_populate_mm(dpagemap, start, end, + svm->gpusvm.mm, 0); + + if (ret) { + AMDGPU_SVM_TRACE("migrate_to_vram failed: ret=%d [0x%lx-0x%lx]\n", + ret, start, end); + return ret; + } + + return 0; +} diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_svm_range_migrate.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_svm_range_migrate.h new file mode 100644 index 000000000000..6e4b6e45512e --- /dev/null +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_svm_range_migrate.h @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0 OR MIT */ +/* + * Copyright 2026 Advanced Micro Devices, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + */ + +#ifndef __AMDGPU_SVM_RANGE_MIGRATE_H__ +#define __AMDGPU_SVM_RANGE_MIGRATE_H__ + +struct amdgpu_svm; +struct drm_gpusvm_range; + +bool amdgpu_pagemap_capable(struct amdgpu_svm *svm); +int amdgpu_svm_range_migrate_to_vram(struct amdgpu_svm *svm, + struct drm_gpusvm_range *range); + +#endif /* __AMDGPU_SVM_RANGE_MIGRATE_H__ */ -- 2.34.1
