drm/amdgpu: SVM VRAM migration via drm_pagemap
Subsystems (2)
v3 cover drm/amdgpu: SVM VRAM migration via drm_pagemap
This series adds VRAM migration support to the amdgpu SVM (Shared Virtual Memory) subsystem, built on top of the drm_pagemap infrastructure [1].
It enables transparent page migration between system RAM and device VRAM using SDMA, driven by userspace SVM attribute hints (prefetch location, access attributes) through the existing AMDGPU SVM ioctl interface.
Limitations:
- Single GPU only; multi-GPU migration is not addressed
- No XNACK-on GPU fault-driven migration (XNACK-off ioctl path only)
- No VRAM-to-VRAM (peer GPU) migration
- No eviction fence / VRAM overcommit handling yet
Design highlights:
- ZONE_DEVICE pages managed via devm_memremap_pages / drm_pagemap
- SDMA-based migration with proper DMA fence synchronization
- Migration decision layer that evaluates SVM attributes to determine when and where to migrate
- Zero modifications to the KFD subsystem
Patch breakdown:
1. Core VRAM migration infrastructure (ZONE_DEVICE, drm_pagemap_ops)
2. SDMA migration callbacks (copy_to_ram / copy_to_dev)
3. Migration decision layer (policy evaluation)
4. SVM attribute extensions (prefetch force-trigger)
5. Integration into SVM range map path + ZONE_DEVICE registration
Built on top of the drm_pagemap SVM series [1].
Changes since v2:
- Moved amdgpu_pagemap entirely to amdgpu side, eliminating all KFD modifications
- Split commits for better reviewability: separated infrastructure from SDMA callbacks, decision layer from integration
- Merged ZONE_DEVICE registration hook into the integration patch
Changes since v1:
- Dropped the eviction fence patch per Christian König's review (violates dma_fence contract)
[1] https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
Junhua Shen (5):
drm/amdgpu: add VRAM migration infrastructure for drm_pagemap drm/amdgpu: implement drm_pagemap SDMA migration callbacks drm/amdgpu: introduce SVM range migration decision layer drm/amdgpu: add SVM attr prefetch/force-trigger functionality drm/amdgpu: integrate VRAM migration into SVM range map path
drivers/gpu/drm/amd/amdgpu/Makefile | 6 +- drivers/gpu/drm/amd/amdgpu/amdgpu.h | 8 + drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 4 + drivers/gpu/drm/amd/amdgpu/amdgpu_migrate.c | 789 ++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_migrate.h | 98 +++ drivers/gpu/drm/amd/amdgpu/amdgpu_reset.c | 4 + drivers/gpu/drm/amd/amdgpu/amdgpu_svm.c | 4 +- drivers/gpu/drm/amd/amdgpu/amdgpu_svm_attr.c | 34 +- drivers/gpu/drm/amd/amdgpu/amdgpu_svm_range.c | 136 +-- drivers/gpu/drm/amd/amdgpu/amdgpu_svm_range.h | 5 +- .../drm/amd/amdgpu/amdgpu_svm_range_migrate.c | 140 ++++ .../drm/amd/amdgpu/amdgpu_svm_range_migrate.h | 60 ++ 12 files changed, 1212 insertions(+), 76 deletions(-) create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_migrate.c create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_migrate.h 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
Signature · 1 line
2.34.1
PATCH v3 1/5 drm/amdgpu: add VRAM migration infrastructure for drm_pagemap
Add the drm_pagemap-based VRAM migration infrastructure:
- Define struct amdgpu_pagemap wrapping dev_pagemap + drm_pagemap
- Define AMDGPU_SVM_PGMAP_OWNER() and AMDGPU_INTERCONNECT_VRAM macros
- Implement amdgpu_svm_migration_init() to register ZONE_DEVICE via devm_memremap_pages() and initialize the drm_pagemap
- Add amdgpu_pagemap pointer (apagemap) to struct amdgpu_device
--- drivers/gpu/drm/amd/amdgpu/Makefile | 6 +- drivers/gpu/drm/amd/amdgpu/amdgpu.h | 8 + drivers/gpu/drm/amd/amdgpu/amdgpu_migrate.c | 179 ++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_migrate.h | 98 +++++++++++ 4 files changed, 289 insertions(+), 2 deletions(-) create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_migrate.c create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_migrate.h
drivers/gpu/drm/amd/amdgpu/Makefile +4 -2
@@ -323,12 +323,14 @@ amdgpu-$(CONFIG_HMM_MIRROR) += amdgpu_hmm.o # svm support amdgpu-$(CONFIG_DRM_AMDGPU_SVM) += amdgpu_svm.o amdgpu_svm_attr.o \ - amdgpu_svm_range.o + amdgpu_svm_range.o amdgpu_migrate.o .PHONY: clean-svm clean-svm: rm -f $(obj)/amdgpu_svm.o $(obj)/amdgpu_svm_attr.o $(obj)/amdgpu_svm_range.o \ - $(obj)/.amdgpu_svm.o.cmd $(obj)/.amdgpu_svm_attr.o.cmd $(obj)/.amdgpu_svm_range.o.cmd + $(obj)/amdgpu_migrate.o \ + $(obj)/.amdgpu_svm.o.cmd $(obj)/.amdgpu_svm_attr.o.cmd $(obj)/.amdgpu_svm_range.o.cmd \ + $(obj)/.amdgpu_migrate.o.cmd include $(FULL_AMD_PATH)/pm/Makefile
drivers/gpu/drm/amd/amdgpu/amdgpu.h +8 -0
@@ -325,6 +325,7 @@ struct amdgpu_fpriv;struct amdgpu_bo_va_mapping ;struct kfd_vm_fault_info ;struct amdgpu_hive_info ; +struct amdgpu_pagemap ;struct amdgpu_reset_context ;struct amdgpu_reset_control ;struct amdgpu_coredump_info ;
@@ -1200,6 +1201,13 @@ struct amdgpu_device {struct amdgpu_uma_carveout_info uma_info; +#if IS_ENABLED (CONFIG_DRM_AMDGPU_SVM ) +/* SVM VRAM migration via drm_pagemap (drm_gpusvm path). +* Allocated in amdgpu_svm_migration_init(), NULL if SVM disabled. +*/ +struct amdgpu_pagemap *apagemap; +#endif +/* KFD * Must be last --ends in a flexible-array member. */
drivers/gpu/drm/amd/amdgpu/amdgpu_migrate.c new file +179 -0
179 lines not on this page · raw message
drivers/gpu/drm/amd/amdgpu/amdgpu_migrate.h new file +98 -0
98 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v3 2/5 drm/amdgpu: implement drm_pagemap SDMA migration callbacks
In reply to Junhua ShenImplement the drm_pagemap_devmem_ops and drm_pagemap_ops callbacks that the DRM GPUSVM migration framework requires:
drm_pagemap_ops (top-level entry points):
- device_map: convert ZONE_DEVICE page to GPU PTE address
- populate_mm: allocate VRAM BO and trigger migration
drm_pagemap_devmem_ops (per-BO migration mechanics):
- populate_devmem_pfn: walk BO buddy blocks to build PFN array
- copy_to_devmem: SDMA copy system RAM -> VRAM via GART window - copy_to_ram: SDMA copy VRAM -> system RAM via GART window - devmem_release: free BO when all pages migrate back
--- drivers/gpu/drm/amd/amdgpu/amdgpu_migrate.c | 616 +++++++++++++++++++- 1 file changed, 613 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_migrate.c +613 -3
@@ -64,12 +64,20 @@#include <linux/memremap.h> #include <linux/migrate.h> -#include "amdgpu_amdkfd.h" #include "amdgpu_migrate.h" #include "amdgpu.h" +#include "amdgpu_ttm.h" +#include "amdgpu_res_cursor.h" + +#define AMDGPU_MIGRATE_TRACE (fmt, ...) \ + pr_debug("%s: " fmt, __func__, ##__VA_ARGS__) + +/* SDMA copy direction */ +#define FROM_RAM_TO_VRAM 0 +#define FROM_VRAM_TO_RAM 1static inline struct amdgpu_pagemap * -dpagemap_to_apagemap (struct drm_pagemap *dpagemap) +to_amdgpu_pagemap (struct drm_pagemap *dpagemap) { return container_of(dpagemap, struct amdgpu_pagemap, dpagemap); }
@@ -94,8 +102,610 @@ amdgpu_svm_page_to_apagemap(struct page *page) return container_of(pgmap, struct amdgpu_pagemap, pgmap); } +/* drm_pagemap_devmem_ops — per-BO migration mechanics */ + +/** +* struct amdgpu_svm_bo - Wrapper linking drm_pagemap_devmem to amdgpu_bo +* +* @devmem: drm_pagemap device memory allocation (passed to framework) +* @bo: The backing VRAM amdgpu_bo +* +* It is allocated per-migration in populate_mm() and freed by +* devmem_release() when all device-private pages have migrated +* back to system memory. +* +* Lifecycle is managed by the drm_pagemap framework's internal zdd refcount: +* - zdd->devmem_allocation points to &svm_bo->devmem +* - When zdd refcount drops to zero, framework calls devmem_release() +* - devmem_release() frees both the BO reference and the svm_bo itself +*/ +struct amdgpu_svm_bo { +struct amdgpu_bo *bo; +struct drm_pagemap_devmem devmem; +}; + +static inline struct amdgpu_svm_bo * +to_amdgpu_svm_bo (struct drm_pagemap_devmem *devmem_allocation) +{ + return container_of(devmem_allocation, struct amdgpu_svm_bo, devmem); +} + +/** +* amdgpu_svm_devmem_release - Release BO when all device pages migrate back +* +* Called by the drm_pagemap framework (via drm_pagemap_zdd_destroy) when the +* last device-private page backed by this allocation has been migrated back +* to system memory (or the owning process exits). +* +* Frees both the amdgpu_bo reference and the wrapper amdgpu_svm_bo itself. +*/ +static void +amdgpu_svm_devmem_release (struct drm_pagemap_devmem *devmem_allocation) +{ +struct amdgpu_svm_bo *svm_bo =to_amdgpu_svm_bo (devmem_allocation); + +AMDGPU_MIGRATE_TRACE ("Release svm_bo=%px bo=%px\n" , svm_bo, svm_bo->bo); +amdgpu_bo_unref (&svm_bo->bo); +kfree (svm_bo); +} + +/** +* amdgpu_svm_populate_devmem_pfn - Convert BO VRAM allocation to PFN array +* @devmem_allocation: The devmem allocation in the amdgpu_svm_bo wrapper +* @npages: Number of PFN entries to fill +* @pfn: Output PFN array +* +* Iterates over the BO's TTM vram_mgr buddy blocks and converts each +* block's VRAM offset to ZONE_DEVICE PFNs: +* +* PFN = PHYS_PFN(block_offset + apagemap.hpa_base) + page_index +* +* This is called by drm_pagemap_migrate_to_devmem() to build the +* destination PFN array for migrate_vma_pages(). +* +* Return: 0 on success +*/ +static int +amdgpu_svm_populate_devmem_pfn (struct drm_pagemap_devmem *devmem_allocation, +unsigned long npages,unsigned long *pfn) +{ +struct amdgpu_pagemap *svm_dm =to_amdgpu_pagemap (devmem_allocation->dpagemap); +struct amdgpu_svm_bo *svm_bo =to_amdgpu_svm_bo (devmem_allocation); +struct amdgpu_bo *bo = svm_bo->bo; +struct amdgpu_res_cursor cursor; +unsigned long i =0 ; +int ret; + + ret =amdgpu_bo_reserve (bo, false); +if (ret) +return ret; + +amdgpu_res_first (bo->tbo.resource,0 , npages <<PAGE_SHIFT , &cursor); +while (cursor.remaining && i < npages) { +u64 pfn_base =PHYS_PFN (cursor.start + svm_dm->hpa_base); +u64 pages = cursor.size >>PAGE_SHIFT ; +unsigned long j; + +for (j =0 ; j < pages && i < npages; j++, i++) + pfn[i] = pfn_base + j; + +amdgpu_res_next (&cursor, cursor.size); + } + +amdgpu_bo_unreserve (bo); + +AMDGPU_MIGRATE_TRACE ("populate_devmem_pfn: npages=%lu first_pfn=0x%lx\n" , + npages, npages >0 ? pfn[0 ] :0 ); + +return 0 ; +} + +/* SDMA copy helpers — GART window based data transfer */ + +/** +* amdgpu_svm_direct_mapping_addr - Convert VRAM offset to MC address +* @adev: AMDGPU device +* @vram_offset: Byte offset within VRAM +* +* Return: MC address suitable for SDMA src/dst +*/ +static u64 +amdgpu_svm_direct_mapping_addr (struct amdgpu_device *adev,u64 vram_offset) +{ +return vram_offset +amdgpu_ttm_domain_start (adev,TTM_PL_VRAM ); +} + +/** +* amdgpu_svm_gart_map - Map system DMA addresses into GART window +* @ring: SDMA ring for the GART update job +* @npages: Number of pages to map +* @addr: Array of system memory DMA addresses +* @gart_addr: Output — GART base address to use in SDMA copy +* @flags: PTE flags (e.g. writeable for RAM-to-VRAM src) +* +* Builds GART PTEs pointing at the given DMA addresses, submits an +* SDMA job to update the GART entries, and returns the GART address +* that can be used as src or dst in a subsequent amdgpu_copy_buffer(). +* +* Uses GART window 0, protected by gtt_window_lock. +* +* Return: 0 on success, negative error code on failure +*/ +static int +amdgpu_svm_gart_map (struct amdgpu_ring *ring, +struct amdgpu_ttm_buffer_entity *entity, +u64 npages, +dma_addr_t *addr,u64 *gart_addr,u64 flags) +{ +struct amdgpu_device *adev = ring->adev; +struct amdgpu_job *job; +unsigned int num_dw, num_bytes; +struct dma_fence *fence; +u64 src_addr, dst_addr; +u64 pte_flags; +void *cpu_addr; +int r; + +/* Use entity's GART window 0 */ + *gart_addr =amdgpu_compute_gart_address (&adev->gmc, entity,0 ); + + num_dw =ALIGN (adev->mman.buffer_funcs->copy_num_dw,8 ); + num_bytes = npages *8 *AMDGPU_GPU_PAGES_IN_CPU_PAGE ; + + r =amdgpu_job_alloc_with_ib (adev, &entity->base, +AMDGPU_FENCE_OWNER_UNDEFINED , + num_dw *4 + num_bytes, +AMDGPU_IB_POOL_DELAYED , + &job, +AMDGPU_KERNEL_JOB_ID_KFD_GART_MAP ); +if (r) +return r; + + src_addr = num_dw *4 ; + src_addr += job->ibs[0 ].gpu_addr; + + dst_addr =amdgpu_bo_gpu_offset (adev->gart.bo); + dst_addr += (entity->gart_window_offs[0 ] >>AMDGPU_GPU_PAGE_SHIFT ) *8 ; +amdgpu_emit_copy_buffer (adev, &job->ibs[0 ], src_addr, + dst_addr, num_bytes,0 ); + +amdgpu_ring_pad_ib (ring, &job->ibs[0 ]); +WARN_ON (job->ibs[0 ].length_dw > num_dw); + + pte_flags =AMDGPU_PTE_VALID |AMDGPU_PTE_READABLE ; + pte_flags |=AMDGPU_PTE_SYSTEM |AMDGPU_PTE_SNOOPED ; +if (flags &AMDGPU_PTE_WRITEABLE ) + pte_flags |=AMDGPU_PTE_WRITEABLE ; + pte_flags |= adev->gart.gart_pte_flags; + + cpu_addr = &job->ibs[0 ].ptr[num_dw]; + +amdgpu_gart_map (adev,0 , npages, addr, pte_flags, cpu_addr); + fence =amdgpu_job_submit (job); +dma_fence_put (fence); + +return 0 ; +} + +/** +* amdgpu_svm_copy_memory_gart - SDMA copy between system RAM and VRAM +* @adev: AMDGPU device +* @sys: Array of DMA addresses for system memory pages +* @vram: Array of VRAM byte offsets (relative to start of VRAM) +* @npages: Number of pages to copy +* @direction: FROM_RAM_TO_VRAM or FROM_VRAM_TO_RAM +* @mfence: In/out — carries the last SDMA fence for serialization +* +* Maps system memory pages into the GART window and uses SDMA to copy +* data to/from VRAM. Handles splitting into AMDGPU_GTT_MAX_TRANSFER_SIZE +* chunks. Acquires entity->lock internally to protect the GART window, +* matching the KFD svm_migrate_copy_memory_gart() pattern. +* +* Return: 0 on success, negative error code on failure +*/ +static int +amdgpu_svm_copy_memory_gart (struct amdgpu_device *adev,dma_addr_t *sys, +u64 *vram,u64 npages,int direction, +struct dma_fence **mfence) +{ +const u64 max_pages =AMDGPU_GTT_MAX_TRANSFER_SIZE ; +struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring; +struct amdgpu_ttm_buffer_entity *entity = &adev->mman.move_entity; +u64 gart_s, gart_d; +struct dma_fence *next; +u64 size; +int r; + +mutex_lock (&entity->lock); + +while (npages) { + size =min (max_pages, npages); + +if (direction ==FROM_VRAM_TO_RAM ) { + gart_s =amdgpu_svm_direct_mapping_addr (adev, *vram); + r =amdgpu_svm_gart_map (ring, entity, size, sys, + &gart_d,AMDGPU_PTE_WRITEABLE ); + }else { + r =amdgpu_svm_gart_map (ring, entity, size, sys, + &gart_s,0 ); + gart_d =amdgpu_svm_direct_mapping_addr (adev, *vram); + } +if (r) { +dev_err (adev->dev,"failed %d to map GART for SDMA\n" , r); + goto out_unlock; + } + +AMDGPU_MIGRATE_TRACE ("SDMA_COPY: %s npages=%llu vram_off=0x%llx\n" , + direction ==FROM_RAM_TO_VRAM ?"RAM->VRAM" :"VRAM->RAM" , + size, (u64)*vram); + + r =amdgpu_copy_buffer (adev, entity, gart_s, gart_d, + size *PAGE_SIZE , +NULL , &next, true,0 ); +if (r) { +dev_err (adev->dev,"failed %d to copy buffer\n" , r); + goto out_unlock; + } + +dma_fence_put (*mfence); + *mfence = next; + npages -= size; +if (npages) { + sys += size; + vram += size; + } + } + +out_unlock: +mutex_unlock (&entity->lock); + +return r; +} + +/** +* amdgpu_svm_copy_to_devmem - SDMA copy system memory -> VRAM +* @pages: Array of destination ZONE_DEVICE pages (VRAM-backed) +* @pagemap_addr: Array of source DMA addresses (system memory, already mapped) +* @npages: Number of pages to copy +* +* Builds parallel sys[] and vram[] arrays from the framework-provided +* pagemap_addr and device pages, then submits batched SDMA copies via +* the GART window. +* +* Return: 0 on success, negative error code on failure +*/ +static int +amdgpu_svm_copy_to_devmem (struct page **pages, +struct drm_pagemap_addr *pagemap_addr, +unsigned long npages, +struct dma_fence *pre_migrate_fence) +{ +struct amdgpu_device *adev; +struct amdgpu_pagemap *svm_dm; +struct dma_fence *mfence =NULL ; +dma_addr_t *sys; +u64 *vram; +unsigned long i, j; +int ret =0 ; + +if (!npages) +return 0 ; + +/* +* Find the first non-NULL page to derive the device. +* The pages array may contain NULL entries for positions where +* no valid device page exists. +*/ +for (i =0 ; i < npages; i++) { +if (pages[i]) +break ; + } +if (i == npages) +return 0 ; + + svm_dm =amdgpu_svm_page_to_apagemap (pages[i]); + adev = svm_dm->adev; + + sys =kvcalloc (npages,sizeof (*sys),GFP_KERNEL ); + vram =kvcalloc (npages,sizeof (*vram),GFP_KERNEL ); +if (!sys || !vram) { + ret = -ENOMEM ; + goto out_free; + } + +for (i =0 , j =0 ; i < npages; i++) { +if (!pagemap_addr[i].addr) + goto flush; + + sys[j] = pagemap_addr[i].addr; + vram[j] = ((u64 )page_to_pfn (pages[i]) <<PAGE_SHIFT ) - + svm_dm->hpa_base; + +/* Check if next vram page is contiguous with current */ +if (j >0 && vram[j] != vram[j -1 ] +PAGE_SIZE ) + goto flush; + + j++; +continue ; +flush: +if (j) { + ret =amdgpu_svm_copy_memory_gart (adev, sys, vram, j, +FROM_RAM_TO_VRAM , + &mfence); +if (ret) + goto out_fence; + j =0 ; + } +/* Re-process current page if it was valid but broke contiguity */ +if (pagemap_addr[i].addr) { + sys[0 ] = pagemap_addr[i].addr; + vram[0 ] = ((u64 )page_to_pfn (pages[i]) <<PAGE_SHIFT ) - + svm_dm->hpa_base; + j =1 ; + } + } + +/* Flush remaining batch */ +if (j) + ret =amdgpu_svm_copy_memory_gart (adev, sys, vram, j, +FROM_RAM_TO_VRAM , &mfence); + +out_fence: +if (mfence) { +dma_fence_wait (mfence, false); +dma_fence_put (mfence); + } + +AMDGPU_MIGRATE_TRACE ("copy_to_devmem done: npages=%ld ret=%d\n" , + npages, ret); + +out_free: +kvfree (vram); +kvfree (sys); +return ret; +} + +/** +* amdgpu_svm_copy_to_ram - SDMA copy VRAM -> system memory +* @pages: Array of source ZONE_DEVICE pages (VRAM-backed) +* @pagemap_addr: Array of destination DMA addresses (system memory, already mapped) +* @npages: Number of pages to copy +* +* Mirror of copy_to_devmem with src/dst swapped. +* +* Return: 0 on success, negative error code on failure +*/ +static int +amdgpu_svm_copy_to_ram (struct page **pages, +struct drm_pagemap_addr *pagemap_addr, +unsigned long npages, +struct dma_fence *pre_migrate_fence) +{ +struct amdgpu_device *adev; +struct amdgpu_pagemap *svm_dm; +struct dma_fence *mfence =NULL ; +dma_addr_t *sys; +u64 *vram; +unsigned long i, j; +int ret =0 ; + +if (!npages) +return 0 ; + +for (i =0 ; i < npages; i++) { +if (pages[i]) +break ; + } +if (i == npages) +return 0 ; + + svm_dm =amdgpu_svm_page_to_apagemap (pages[i]); + adev = svm_dm->adev; + + sys =kvcalloc (npages,sizeof (*sys),GFP_KERNEL ); + vram =kvcalloc (npages,sizeof (*vram),GFP_KERNEL ); +if (!sys || !vram) { + ret = -ENOMEM ; + goto out_free; + } + +for (i =0 , j =0 ; i < npages; i++) { +if (!pagemap_addr[i].addr || !pages[i]) + goto flush; + + vram[j] = ((u64 )page_to_pfn (pages[i]) <<PAGE_SHIFT ) - + svm_dm->hpa_base; + sys[j] = pagemap_addr[i].addr; + +/* Check if next vram page is contiguous with current */ +if (j >0 && vram[j] != vram[j -1 ] +PAGE_SIZE ) + goto flush; + + j++; +continue ; +flush: +if (j) { + ret =amdgpu_svm_copy_memory_gart (adev, sys, vram, j, +FROM_VRAM_TO_RAM , + &mfence); +if (ret) + goto out_fence; + j =0 ; + } +/* Re-process current page if it was valid but broke contiguity */ +if (pagemap_addr[i].addr && pages[i]) { + vram[0 ] = ((u64 )page_to_pfn (pages[i]) <<PAGE_SHIFT ) - + svm_dm->hpa_base; + sys[0 ] = pagemap_addr[i].addr; + j =1 ; + } + } + +/* Flush remaining batch */ +if (j) + ret =amdgpu_svm_copy_memory_gart (adev, sys, vram, j, +FROM_VRAM_TO_RAM , &mfence); + +out_fence: +if (mfence) { +dma_fence_wait (mfence, false); +dma_fence_put (mfence); + } + +AMDGPU_MIGRATE_TRACE ("copy_to_ram done: npages=%ld ret=%d\n" , npages, ret); + +out_free: +kvfree (vram); +kvfree (sys); +return ret; +} + +static const struct drm_pagemap_devmem_ops amdgpu_pagemap_ops = { + .devmem_release = amdgpu_svm_devmem_release, + .populate_devmem_pfn = amdgpu_svm_populate_devmem_pfn, + .copy_to_devmem = amdgpu_svm_copy_to_devmem, + .copy_to_ram = amdgpu_svm_copy_to_ram, +}; + +/* drm_pagemap_ops — top-level migration entry points */ + +/** +* amdgpu_svm_device_map - Convert ZONE_DEVICE page to GPU PTE address +* @dpagemap: The drm_pagemap for this device +* @dev: Requesting device (for P2P check) +* @page: ZONE_DEVICE page backed by VRAM +* @order: Page order (0 = 4K, 9 = 2M, etc.) +* @dir: DMA direction (unused for local VRAM) +* +* Address conversion chain: +* page -> PFN -> HPA -> VRAM offset -> PTE address +* +* HPA = page_to_pfn(page) << PAGE_SHIFT +* VRAM offset = HPA - apagemap.hpa_base +* PTE address = VRAM offset + adev->vm_manager.vram_base_offset +* +* Return: drm_pagemap_addr with PTE address and AMDGPU_INTERCONNECT_VRAM protocol +*/ +static struct drm_pagemap_addr +amdgpu_svm_device_map (struct drm_pagemap *dpagemap, +struct device *dev, +struct page *page, +unsigned int order, +enum dma_data_direction dir) +{ +struct amdgpu_pagemap *svm_dm =to_amdgpu_pagemap (dpagemap); +struct amdgpu_device *adev =dpagemap_to_adev (dpagemap); +dma_addr_t addr; + +if (dpagemap->drm->dev == dev) { +/* Same device: return VRAM PTE address */ +u64 hpa = (u64 )page_to_pfn (page) <<PAGE_SHIFT ; +u64 vram_offset = hpa - svm_dm->hpa_base; + + addr = vram_offset + adev->vm_manager.vram_base_offset; + }else { +/* Cross-device P2P: not yet supported */ + addr =DMA_MAPPING_ERROR ; + } + +return drm_pagemap_addr_encode (addr, +AMDGPU_INTERCONNECT_VRAM , order, dir); +} + +/** +* amdgpu_svm_bo_alloc - Allocate an amdgpu_svm_bo wrapper with VRAM backing +* @adev: AMDGPU device +* @dpagemap: The drm_pagemap for this device +* @mm: mm_struct of the owning process +* @size: Allocation size in bytes +* +* Return: Pointer to allocated amdgpu_svm_bo on success, ERR_PTR on failure +*/ +static struct amdgpu_svm_bo * +amdgpu_svm_bo_alloc (struct amdgpu_device *adev, +struct drm_pagemap *dpagemap, +struct mm_struct *mm,unsigned long size) +{ +struct amdgpu_svm_bo *svm_bo; +struct amdgpu_bo_param bp = {}; +struct amdgpu_bo *bo; +int ret; + + svm_bo =kzalloc (sizeof (*svm_bo),GFP_KERNEL ); +if (!svm_bo) +return ERR_PTR (-ENOMEM ); + + bp.size = size; + bp.bo_ptr_size =sizeof (struct amdgpu_bo ); + bp.domain =AMDGPU_GEM_DOMAIN_VRAM ; + bp.type = ttm_bo_type_device; + bp.flags =AMDGPU_GEM_CREATE_NO_CPU_ACCESS | +AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS | +AMDGPU_GEM_CREATE_VRAM_CLEARED ; + + ret =amdgpu_bo_create (adev, &bp, &bo); +if (ret) { +AMDGPU_MIGRATE_TRACE ("Failed to create SVM BO\n" ); +kfree (svm_bo); +return ERR_PTR (ret); + } + +amdgpu_bo_unreserve (bo); + svm_bo->bo = bo; + +drm_pagemap_devmem_init (&svm_bo->devmem, + adev->dev, mm, + &amdgpu_pagemap_ops, + dpagemap, size,NULL ); + +return svm_bo; +} + +/** +* amdgpu_svm_populate_mm - Allocate VRAM BO and migrate pages +* @dpagemap: The drm_pagemap for this device +* @start: Start virtual address of the range to migrate +* @end: End virtual address (exclusive) +* @mm: mm_struct of the owning process +* @timeslice_ms: Maximum time to spend migrating (for fairness) +* +* Core migration entry point called by drm_pagemap_populate_mm(). +* Allocates an amdgpu_svm_bo via amdgpu_svm_bo_alloc(), then calls +* drm_pagemap_migrate_to_devmem() to execute the actual migration. +* +* Return: 0 on success, negative error code on failure +*/ +static int +amdgpu_svm_populate_mm (struct drm_pagemap *dpagemap, +unsigned long start,unsigned long end, +struct mm_struct *mm, +unsigned long timeslice_ms) +{ +struct amdgpu_device *adev =dpagemap_to_adev (dpagemap); +struct drm_pagemap_migrate_details mdetails = { + .timeslice_ms = timeslice_ms, + }; +struct amdgpu_svm_bo *svm_bo; +int ret; + + svm_bo =amdgpu_svm_bo_alloc (adev, dpagemap, mm, end - start); +if (IS_ERR (svm_bo)) +return PTR_ERR (svm_bo); + +AMDGPU_MIGRATE_TRACE ("populate_mm: [0x%lx-0x%lx] size=%lu\n" , + start, end, end - start); + + ret =drm_pagemap_migrate_to_devmem (&svm_bo->devmem, + mm, start, end, + &mdetails); + +return ret; +} -const struct drm_pagemap_ops amdgpu_svm_drm_pagemap_ops = { }; +const struct drm_pagemap_ops amdgpu_svm_drm_pagemap_ops = { + .device_map = amdgpu_svm_device_map, + .populate_mm = amdgpu_svm_populate_mm, +};/** * amdgpu_svm_migration_init - Register ZONE_DEVICE and initialize drm_pagemap
Signature · 1 line
2.34.1
Re: [PATCH v3 2/5] drm/amdgpu: implement drm_pagemap SDMA migration callbacks
Quoted 118 lines · Junhua Shen
Implement the
drm_pagemap_devmem_opsanddrm_pagemap_opscallbacks that the DRM GPUSVM migration framework requires:
drm_pagemap_ops(top-level entry points):
-device_map: convertZONE_DEVICEpage to GPU PTE address
-populate_mm: allocate VRAM BO and trigger migration
drm_pagemap_devmem_ops(per-BO migration mechanics):
-populate_devmem_pfn: walk BO buddy blocks to build PFN array- copy_to_devmem: SDMA copy system RAM -> VRAM via GART window - copy_to_ram: SDMA copy VRAM -> system RAM via GART window - devmem_release: free BO when all pages migrate backSigned-off-by Junhua Shen <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_migrate.c | 616 +++++++++++++++++++- 1 file changed, 613 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_migrate.c +79
-2@@ -64,12 +64,20 @@#include <linux/memremap.h> #include <linux/migrate.h> -#include "amdgpu_amdkfd.h" #include "amdgpu_migrate.h" #include "amdgpu.h" +#include "amdgpu_ttm.h" +#include "amdgpu_res_cursor.h" + +#define AMDGPU_MIGRATE_TRACE (fmt, ...) \ + pr_debug("%s: " fmt, __func__, ##__VA_ARGS__) + +/* SDMA copy direction */ +#define FROM_RAM_TO_VRAM 0 +#define FROM_VRAM_TO_RAM 1static inline struct amdgpu_pagemap * -dpagemap_to_apagemap (struct drm_pagemap *dpagemap) +to_amdgpu_pagemap (struct drm_pagemap *dpagemap) { return container_of(dpagemap, struct amdgpu_pagemap, dpagemap); }@@ -94,8 +102,610 @@ amdgpu_svm_page_to_apagemap(struct page *page) return container_of(pgmap, struct amdgpu_pagemap, pgmap); } +/* drm_pagemap_devmem_ops — per-BO migration mechanics */ + +/** +* struct amdgpu_svm_bo - Wrapper linking drm_pagemap_devmem to amdgpu_bo +* +* @devmem: drm_pagemap device memory allocation (passed to framework) +* @bo: The backing VRAM amdgpu_bo +* +* It is allocated per-migration in populate_mm() and freed by +* devmem_release() when all device-private pages have migrated +* back to system memory. +* +* Lifecycle is managed by the drm_pagemap framework's internal zdd refcount: +* - zdd->devmem_allocation points to &svm_bo->devmem +* - When zdd refcount drops to zero, framework calls devmem_release() +* - devmem_release() frees both the BO reference and the svm_bo itself +*/ +struct amdgpu_svm_bo { +struct amdgpu_bo *bo; +struct drm_pagemap_devmem devmem; +}; + +static inline struct amdgpu_svm_bo * +to_amdgpu_svm_bo (struct drm_pagemap_devmem *devmem_allocation) +{ + return container_of(devmem_allocation, struct amdgpu_svm_bo, devmem); +} + +/** +* amdgpu_svm_devmem_release - Release BO when all device pages migrate back +* +* Called by the drm_pagemap framework (via drm_pagemap_zdd_destroy) when the +* last device-private page backed by this allocation has been migrated back +* to system memory (or the owning process exits). +* +* Frees both the amdgpu_bo reference and the wrapper amdgpu_svm_bo itself. +*/ +static void +amdgpu_svm_devmem_release (struct drm_pagemap_devmem *devmem_allocation) +{ +struct amdgpu_svm_bo *svm_bo =to_amdgpu_svm_bo (devmem_allocation); + +AMDGPU_MIGRATE_TRACE ("Release svm_bo=%px bo=%px\n" , svm_bo, svm_bo->bo); +amdgpu_bo_unref (&svm_bo->bo); +kfree (svm_bo); +} + +/** +* amdgpu_svm_populate_devmem_pfn - Convert BO VRAM allocation to PFN array +* @devmem_allocation: The devmem allocation in the amdgpu_svm_bo wrapper +* @npages: Number of PFN entries to fill +* @pfn: Output PFN array +* +* Iterates over the BO's TTM vram_mgr buddy blocks and converts each +* block's VRAM offset to ZONE_DEVICE PFNs: +* +* PFN = PHYS_PFN(block_offset + apagemap.hpa_base) + page_index +* +* This is called by drm_pagemap_migrate_to_devmem() to build the +* destination PFN array for migrate_vma_pages(). +* +* Return: 0 on success +*/ +static int +amdgpu_svm_populate_devmem_pfn (struct drm_pagemap_devmem *devmem_allocation, +unsigned long npages,unsigned long *pfn) +{ +struct amdgpu_pagemap *svm_dm =to_amdgpu_pagemap (devmem_allocation->dpagemap); +struct amdgpu_svm_bo *svm_bo =to_amdgpu_svm_bo (devmem_allocation);
drivers/gpu/drm/amd/amdgpu/amdgpu_migrate.c amdgpu_svm_page_to_apagemap(struct page *page) · line 183
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_migrate.c +10
-0+struct amdgpu_bo *bo = svm_bo->bo; +struct amdgpu_res_cursor cursor; +unsigned long i =0 ; +int ret; + + ret =amdgpu_bo_reserve (bo, false); +if (ret) +return ret; + +amdgpu_res_first (bo->tbo.resource,0 , npages <<PAGE_SHIFT , &cursor);
How do you ensure that the BO is valid and actually in VRAM at the time? And how do you ensure that it stays there as long as the zone_device pages are in use? As far as I can tell, the BO is not pinned and there is no fence that prevents it from being evicted to GTT by TTM without warning.
Regards,
Felix
Quoted 529 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_migrate.c +524
-1+while (cursor.remaining && i < npages) { +u64 pfn_base =PHYS_PFN (cursor.start + svm_dm->hpa_base); +u64 pages = cursor.size >>PAGE_SHIFT ; +unsigned long j; + +for (j =0 ; j < pages && i < npages; j++, i++) + pfn[i] = pfn_base + j; + +amdgpu_res_next (&cursor, cursor.size); + } + +amdgpu_bo_unreserve (bo); + +AMDGPU_MIGRATE_TRACE ("populate_devmem_pfn: npages=%lu first_pfn=0x%lx\n" , + npages, npages >0 ? pfn[0 ] :0 ); + +return 0 ; +} + +/* SDMA copy helpers — GART window based data transfer */ + +/** +* amdgpu_svm_direct_mapping_addr - Convert VRAM offset to MC address +* @adev: AMDGPU device +* @vram_offset: Byte offset within VRAM +* +* Return: MC address suitable for SDMA src/dst +*/ +static u64 +amdgpu_svm_direct_mapping_addr (struct amdgpu_device *adev,u64 vram_offset) +{ +return vram_offset +amdgpu_ttm_domain_start (adev,TTM_PL_VRAM ); +} + +/** +* amdgpu_svm_gart_map - Map system DMA addresses into GART window +* @ring: SDMA ring for the GART update job +* @npages: Number of pages to map +* @addr: Array of system memory DMA addresses +* @gart_addr: Output — GART base address to use in SDMA copy +* @flags: PTE flags (e.g. writeable for RAM-to-VRAM src) +* +* Builds GART PTEs pointing at the given DMA addresses, submits an +* SDMA job to update the GART entries, and returns the GART address +* that can be used as src or dst in a subsequent amdgpu_copy_buffer(). +* +* Uses GART window 0, protected by gtt_window_lock. +* +* Return: 0 on success, negative error code on failure +*/ +static int +amdgpu_svm_gart_map (struct amdgpu_ring *ring, +struct amdgpu_ttm_buffer_entity *entity, +u64 npages, +dma_addr_t *addr,u64 *gart_addr,u64 flags) +{ +struct amdgpu_device *adev = ring->adev; +struct amdgpu_job *job; +unsigned int num_dw, num_bytes; +struct dma_fence *fence; +u64 src_addr, dst_addr; +u64 pte_flags; +void *cpu_addr; +int r; + +/* Use entity's GART window 0 */ + *gart_addr =amdgpu_compute_gart_address (&adev->gmc, entity,0 ); + + num_dw =ALIGN (adev->mman.buffer_funcs->copy_num_dw,8 ); + num_bytes = npages *8 *AMDGPU_GPU_PAGES_IN_CPU_PAGE ; + + r =amdgpu_job_alloc_with_ib (adev, &entity->base, +AMDGPU_FENCE_OWNER_UNDEFINED , + num_dw *4 + num_bytes, +AMDGPU_IB_POOL_DELAYED , + &job, +AMDGPU_KERNEL_JOB_ID_KFD_GART_MAP ); +if (r) +return r; + + src_addr = num_dw *4 ; + src_addr += job->ibs[0 ].gpu_addr; + + dst_addr =amdgpu_bo_gpu_offset (adev->gart.bo); + dst_addr += (entity->gart_window_offs[0 ] >>AMDGPU_GPU_PAGE_SHIFT ) *8 ; +amdgpu_emit_copy_buffer (adev, &job->ibs[0 ], src_addr, + dst_addr, num_bytes,0 ); + +amdgpu_ring_pad_ib (ring, &job->ibs[0 ]); +WARN_ON (job->ibs[0 ].length_dw > num_dw); + + pte_flags =AMDGPU_PTE_VALID |AMDGPU_PTE_READABLE ; + pte_flags |=AMDGPU_PTE_SYSTEM |AMDGPU_PTE_SNOOPED ; +if (flags &AMDGPU_PTE_WRITEABLE ) + pte_flags |=AMDGPU_PTE_WRITEABLE ; + pte_flags |= adev->gart.gart_pte_flags; + + cpu_addr = &job->ibs[0 ].ptr[num_dw]; + +amdgpu_gart_map (adev,0 , npages, addr, pte_flags, cpu_addr); + fence =amdgpu_job_submit (job); +dma_fence_put (fence); + +return 0 ; +} + +/** +* amdgpu_svm_copy_memory_gart - SDMA copy between system RAM and VRAM +* @adev: AMDGPU device +* @sys: Array of DMA addresses for system memory pages +* @vram: Array of VRAM byte offsets (relative to start of VRAM) +* @npages: Number of pages to copy +* @direction: FROM_RAM_TO_VRAM or FROM_VRAM_TO_RAM +* @mfence: In/out — carries the last SDMA fence for serialization +* +* Maps system memory pages into the GART window and uses SDMA to copy +* data to/from VRAM. Handles splitting into AMDGPU_GTT_MAX_TRANSFER_SIZE +* chunks. Acquires entity->lock internally to protect the GART window, +* matching the KFD svm_migrate_copy_memory_gart() pattern. +* +* Return: 0 on success, negative error code on failure +*/ +static int +amdgpu_svm_copy_memory_gart (struct amdgpu_device *adev,dma_addr_t *sys, +u64 *vram,u64 npages,int direction, +struct dma_fence **mfence) +{ +const u64 max_pages =AMDGPU_GTT_MAX_TRANSFER_SIZE ; +struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring; +struct amdgpu_ttm_buffer_entity *entity = &adev->mman.move_entity; +u64 gart_s, gart_d; +struct dma_fence *next; +u64 size; +int r; + +mutex_lock (&entity->lock); + +while (npages) { + size =min (max_pages, npages); + +if (direction ==FROM_VRAM_TO_RAM ) { + gart_s =amdgpu_svm_direct_mapping_addr (adev, *vram); + r =amdgpu_svm_gart_map (ring, entity, size, sys, + &gart_d,AMDGPU_PTE_WRITEABLE ); + }else { + r =amdgpu_svm_gart_map (ring, entity, size, sys, + &gart_s,0 ); + gart_d =amdgpu_svm_direct_mapping_addr (adev, *vram); + } +if (r) { +dev_err (adev->dev,"failed %d to map GART for SDMA\n" , r); + goto out_unlock; + } + +AMDGPU_MIGRATE_TRACE ("SDMA_COPY: %s npages=%llu vram_off=0x%llx\n" , + direction ==FROM_RAM_TO_VRAM ?"RAM->VRAM" :"VRAM->RAM" , + size, (u64)*vram); + + r =amdgpu_copy_buffer (adev, entity, gart_s, gart_d, + size *PAGE_SIZE , +NULL , &next, true,0 ); +if (r) { +dev_err (adev->dev,"failed %d to copy buffer\n" , r); + goto out_unlock; + } + +dma_fence_put (*mfence); + *mfence = next; + npages -= size; +if (npages) { + sys += size; + vram += size; + } + } + +out_unlock: +mutex_unlock (&entity->lock); + +return r; +} + +/** +* amdgpu_svm_copy_to_devmem - SDMA copy system memory -> VRAM +* @pages: Array of destination ZONE_DEVICE pages (VRAM-backed) +* @pagemap_addr: Array of source DMA addresses (system memory, already mapped) +* @npages: Number of pages to copy +* +* Builds parallel sys[] and vram[] arrays from the framework-provided +* pagemap_addr and device pages, then submits batched SDMA copies via +* the GART window. +* +* Return: 0 on success, negative error code on failure +*/ +static int +amdgpu_svm_copy_to_devmem (struct page **pages, +struct drm_pagemap_addr *pagemap_addr, +unsigned long npages, +struct dma_fence *pre_migrate_fence) +{ +struct amdgpu_device *adev; +struct amdgpu_pagemap *svm_dm; +struct dma_fence *mfence =NULL ; +dma_addr_t *sys; +u64 *vram; +unsigned long i, j; +int ret =0 ; + +if (!npages) +return 0 ; + +/* +* Find the first non-NULL page to derive the device. +* The pages array may contain NULL entries for positions where +* no valid device page exists. +*/ +for (i =0 ; i < npages; i++) { +if (pages[i]) +break ; + } +if (i == npages) +return 0 ; + + svm_dm =amdgpu_svm_page_to_apagemap (pages[i]); + adev = svm_dm->adev; + + sys =kvcalloc (npages,sizeof (*sys),GFP_KERNEL ); + vram =kvcalloc (npages,sizeof (*vram),GFP_KERNEL ); +if (!sys || !vram) { + ret = -ENOMEM ; + goto out_free; + } + +for (i =0 , j =0 ; i < npages; i++) { +if (!pagemap_addr[i].addr) + goto flush; + + sys[j] = pagemap_addr[i].addr; + vram[j] = ((u64 )page_to_pfn (pages[i]) <<PAGE_SHIFT ) - + svm_dm->hpa_base; + +/* Check if next vram page is contiguous with current */ +if (j >0 && vram[j] != vram[j -1 ] +PAGE_SIZE ) + goto flush; + + j++; +continue ; +flush: +if (j) { + ret =amdgpu_svm_copy_memory_gart (adev, sys, vram, j, +FROM_RAM_TO_VRAM , + &mfence); +if (ret) + goto out_fence; + j =0 ; + } +/* Re-process current page if it was valid but broke contiguity */ +if (pagemap_addr[i].addr) { + sys[0 ] = pagemap_addr[i].addr; + vram[0 ] = ((u64 )page_to_pfn (pages[i]) <<PAGE_SHIFT ) - + svm_dm->hpa_base; + j =1 ; + } + } + +/* Flush remaining batch */ +if (j) + ret =amdgpu_svm_copy_memory_gart (adev, sys, vram, j, +FROM_RAM_TO_VRAM , &mfence); + +out_fence: +if (mfence) { +dma_fence_wait (mfence, false); +dma_fence_put (mfence); + } + +AMDGPU_MIGRATE_TRACE ("copy_to_devmem done: npages=%ld ret=%d\n" , + npages, ret); + +out_free: +kvfree (vram); +kvfree (sys); +return ret; +} + +/** +* amdgpu_svm_copy_to_ram - SDMA copy VRAM -> system memory +* @pages: Array of source ZONE_DEVICE pages (VRAM-backed) +* @pagemap_addr: Array of destination DMA addresses (system memory, already mapped) +* @npages: Number of pages to copy +* +* Mirror of copy_to_devmem with src/dst swapped. +* +* Return: 0 on success, negative error code on failure +*/ +static int +amdgpu_svm_copy_to_ram (struct page **pages, +struct drm_pagemap_addr *pagemap_addr, +unsigned long npages, +struct dma_fence *pre_migrate_fence) +{ +struct amdgpu_device *adev; +struct amdgpu_pagemap *svm_dm; +struct dma_fence *mfence =NULL ; +dma_addr_t *sys; +u64 *vram; +unsigned long i, j; +int ret =0 ; + +if (!npages) +return 0 ; + +for (i =0 ; i < npages; i++) { +if (pages[i]) +break ; + } +if (i == npages) +return 0 ; + + svm_dm =amdgpu_svm_page_to_apagemap (pages[i]); + adev = svm_dm->adev; + + sys =kvcalloc (npages,sizeof (*sys),GFP_KERNEL ); + vram =kvcalloc (npages,sizeof (*vram),GFP_KERNEL ); +if (!sys || !vram) { + ret = -ENOMEM ; + goto out_free; + } + +for (i =0 , j =0 ; i < npages; i++) { +if (!pagemap_addr[i].addr || !pages[i]) + goto flush; + + vram[j] = ((u64 )page_to_pfn (pages[i]) <<PAGE_SHIFT ) - + svm_dm->hpa_base; + sys[j] = pagemap_addr[i].addr; + +/* Check if next vram page is contiguous with current */ +if (j >0 && vram[j] != vram[j -1 ] +PAGE_SIZE ) + goto flush; + + j++; +continue ; +flush: +if (j) { + ret =amdgpu_svm_copy_memory_gart (adev, sys, vram, j, +FROM_VRAM_TO_RAM , + &mfence); +if (ret) + goto out_fence; + j =0 ; + } +/* Re-process current page if it was valid but broke contiguity */ +if (pagemap_addr[i].addr && pages[i]) { + vram[0 ] = ((u64 )page_to_pfn (pages[i]) <<PAGE_SHIFT ) - + svm_dm->hpa_base; + sys[0 ] = pagemap_addr[i].addr; + j =1 ; + } + } + +/* Flush remaining batch */ +if (j) + ret =amdgpu_svm_copy_memory_gart (adev, sys, vram, j, +FROM_VRAM_TO_RAM , &mfence); + +out_fence: +if (mfence) { +dma_fence_wait (mfence, false); +dma_fence_put (mfence); + } + +AMDGPU_MIGRATE_TRACE ("copy_to_ram done: npages=%ld ret=%d\n" , npages, ret); + +out_free: +kvfree (vram); +kvfree (sys); +return ret; +} + +static const struct drm_pagemap_devmem_ops amdgpu_pagemap_ops = { + .devmem_release = amdgpu_svm_devmem_release, + .populate_devmem_pfn = amdgpu_svm_populate_devmem_pfn, + .copy_to_devmem = amdgpu_svm_copy_to_devmem, + .copy_to_ram = amdgpu_svm_copy_to_ram, +}; + +/* drm_pagemap_ops — top-level migration entry points */ + +/** +* amdgpu_svm_device_map - Convert ZONE_DEVICE page to GPU PTE address +* @dpagemap: The drm_pagemap for this device +* @dev: Requesting device (for P2P check) +* @page: ZONE_DEVICE page backed by VRAM +* @order: Page order (0 = 4K, 9 = 2M, etc.) +* @dir: DMA direction (unused for local VRAM) +* +* Address conversion chain: +* page -> PFN -> HPA -> VRAM offset -> PTE address +* +* HPA = page_to_pfn(page) << PAGE_SHIFT +* VRAM offset = HPA - apagemap.hpa_base +* PTE address = VRAM offset + adev->vm_manager.vram_base_offset +* +* Return: drm_pagemap_addr with PTE address and AMDGPU_INTERCONNECT_VRAM protocol +*/ +static struct drm_pagemap_addr +amdgpu_svm_device_map (struct drm_pagemap *dpagemap, +struct device *dev, +struct page *page, +unsigned int order, +enum dma_data_direction dir) +{ +struct amdgpu_pagemap *svm_dm =to_amdgpu_pagemap (dpagemap); +struct amdgpu_device *adev =dpagemap_to_adev (dpagemap); +dma_addr_t addr; + +if (dpagemap->drm->dev == dev) { +/* Same device: return VRAM PTE address */ +u64 hpa = (u64 )page_to_pfn (page) <<PAGE_SHIFT ; +u64 vram_offset = hpa - svm_dm->hpa_base; + + addr = vram_offset + adev->vm_manager.vram_base_offset; + }else { +/* Cross-device P2P: not yet supported */ + addr =DMA_MAPPING_ERROR ; + } + +return drm_pagemap_addr_encode (addr, +AMDGPU_INTERCONNECT_VRAM , order, dir); +} + +/** +* amdgpu_svm_bo_alloc - Allocate an amdgpu_svm_bo wrapper with VRAM backing +* @adev: AMDGPU device +* @dpagemap: The drm_pagemap for this device +* @mm: mm_struct of the owning process +* @size: Allocation size in bytes +* +* Return: Pointer to allocated amdgpu_svm_bo on success, ERR_PTR on failure +*/ +static struct amdgpu_svm_bo * +amdgpu_svm_bo_alloc (struct amdgpu_device *adev, +struct drm_pagemap *dpagemap, +struct mm_struct *mm,unsigned long size) +{ +struct amdgpu_svm_bo *svm_bo; +struct amdgpu_bo_param bp = {}; +struct amdgpu_bo *bo; +int ret; + + svm_bo =kzalloc (sizeof (*svm_bo),GFP_KERNEL ); +if (!svm_bo) +return ERR_PTR (-ENOMEM ); + + bp.size = size; + bp.bo_ptr_size =sizeof (struct amdgpu_bo ); + bp.domain =AMDGPU_GEM_DOMAIN_VRAM ; + bp.type = ttm_bo_type_device; + bp.flags =AMDGPU_GEM_CREATE_NO_CPU_ACCESS | +AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS | +AMDGPU_GEM_CREATE_VRAM_CLEARED ; + + ret =amdgpu_bo_create (adev, &bp, &bo); +if (ret) { +AMDGPU_MIGRATE_TRACE ("Failed to create SVM BO\n" ); +kfree (svm_bo); +return ERR_PTR (ret); + } + +amdgpu_bo_unreserve (bo); + svm_bo->bo = bo; + +drm_pagemap_devmem_init (&svm_bo->devmem, + adev->dev, mm, + &amdgpu_pagemap_ops, + dpagemap, size,NULL ); + +return svm_bo; +} + +/** +* amdgpu_svm_populate_mm - Allocate VRAM BO and migrate pages +* @dpagemap: The drm_pagemap for this device +* @start: Start virtual address of the range to migrate +* @end: End virtual address (exclusive) +* @mm: mm_struct of the owning process +* @timeslice_ms: Maximum time to spend migrating (for fairness) +* +* Core migration entry point called by drm_pagemap_populate_mm(). +* Allocates an amdgpu_svm_bo via amdgpu_svm_bo_alloc(), then calls +* drm_pagemap_migrate_to_devmem() to execute the actual migration. +* +* Return: 0 on success, negative error code on failure +*/ +static int +amdgpu_svm_populate_mm (struct drm_pagemap *dpagemap, +unsigned long start,unsigned long end, +struct mm_struct *mm, +unsigned long timeslice_ms) +{ +struct amdgpu_device *adev =dpagemap_to_adev (dpagemap); +struct drm_pagemap_migrate_details mdetails = { + .timeslice_ms = timeslice_ms, + }; +struct amdgpu_svm_bo *svm_bo; +int ret; + + svm_bo =amdgpu_svm_bo_alloc (adev, dpagemap, mm, end - start); +if (IS_ERR (svm_bo)) +return PTR_ERR (svm_bo); + +AMDGPU_MIGRATE_TRACE ("populate_mm: [0x%lx-0x%lx] size=%lu\n" , + start, end, end - start); + + ret =drm_pagemap_migrate_to_devmem (&svm_bo->devmem, + mm, start, end, + &mdetails); + +return ret; +} -const struct drm_pagemap_ops amdgpu_svm_drm_pagemap_ops = { }; +const struct drm_pagemap_ops amdgpu_svm_drm_pagemap_ops = { + .device_map = amdgpu_svm_device_map, + .populate_mm = amdgpu_svm_populate_mm, +};/** * amdgpu_svm_migration_init - Register ZONE_DEVICE and initialize drm_pagemap
Re: [PATCH v3 2/5] drm/amdgpu: implement drm_pagemap SDMA migration callbacks
Quoted 133 lines · Felix Kuehling
Quoted 115 lines · Junhua Shen
Implement the
drm_pagemap_devmem_opsanddrm_pagemap_opscallbacks that the DRM GPUSVM migration framework requires:
drm_pagemap_ops(top-level entry points):
-device_map: convertZONE_DEVICEpage to GPU PTE address
-populate_mm: allocate VRAM BO and trigger migration
drm_pagemap_devmem_ops(per-BO migration mechanics):
-populate_devmem_pfn: walk BO buddy blocks to build PFN array- copy_to_devmem: SDMA copy system RAM -> VRAM via GART window - copy_to_ram: SDMA copy VRAM -> system RAM via GART window - devmem_release: free BO when all pages migrate backSigned-off-by Junhua Shen <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_migrate.c | 616 +++++++++++++++++++- 1 file changed, 613 insertions(+), 3 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_migrate.c +79
-2@@ -64,12 +64,20 @@#include <linux/memremap.h> #include <linux/migrate.h> -#include "amdgpu_amdkfd.h" #include "amdgpu_migrate.h" #include "amdgpu.h" +#include "amdgpu_ttm.h" +#include "amdgpu_res_cursor.h" + +#define AMDGPU_MIGRATE_TRACE (fmt, ...) \ + pr_debug("%s: " fmt, __func__, ##__VA_ARGS__) + +/* SDMA copy direction */ +#define FROM_RAM_TO_VRAM 0 +#define FROM_VRAM_TO_RAM 1static inline struct amdgpu_pagemap * -dpagemap_to_apagemap (struct drm_pagemap *dpagemap) +to_amdgpu_pagemap (struct drm_pagemap *dpagemap) { return container_of(dpagemap, struct amdgpu_pagemap, dpagemap); }@@ -94,8 +102,610 @@ amdgpu_svm_page_to_apagemap(struct page *page) return container_of(pgmap, struct amdgpu_pagemap, pgmap); } +/* drm_pagemap_devmem_ops — per-BO migration mechanics */ + +/** +* struct amdgpu_svm_bo - Wrapper linking drm_pagemap_devmem to amdgpu_bo +* +* @devmem: drm_pagemap device memory allocation (passed to framework) +* @bo: The backing VRAM amdgpu_bo +* +* It is allocated per-migration in populate_mm() and freed by +* devmem_release() when all device-private pages have migrated +* back to system memory. +* +* Lifecycle is managed by the drm_pagemap framework's internal zdd refcount: +* - zdd->devmem_allocation points to &svm_bo->devmem +* - When zdd refcount drops to zero, framework calls devmem_release() +* - devmem_release() frees both the BO reference and the svm_bo itself +*/ +struct amdgpu_svm_bo { +struct amdgpu_bo *bo; +struct drm_pagemap_devmem devmem; +}; + +static inline struct amdgpu_svm_bo * +to_amdgpu_svm_bo (struct drm_pagemap_devmem *devmem_allocation) +{ + return container_of(devmem_allocation, struct amdgpu_svm_bo, devmem); +} + +/** +* amdgpu_svm_devmem_release - Release BO when all device pages migrate back +* +* Called by the drm_pagemap framework (via drm_pagemap_zdd_destroy) when the +* last device-private page backed by this allocation has been migrated back +* to system memory (or the owning process exits). +* +* Frees both the amdgpu_bo reference and the wrapper amdgpu_svm_bo itself. +*/ +static void +amdgpu_svm_devmem_release (struct drm_pagemap_devmem *devmem_allocation) +{ +struct amdgpu_svm_bo *svm_bo =to_amdgpu_svm_bo (devmem_allocation); + +AMDGPU_MIGRATE_TRACE ("Release svm_bo=%px bo=%px\n" , svm_bo, svm_bo->bo); +amdgpu_bo_unref (&svm_bo->bo); +kfree (svm_bo); +} + +/** +* amdgpu_svm_populate_devmem_pfn - Convert BO VRAM allocation to PFN array +* @devmem_allocation: The devmem allocation in the amdgpu_svm_bo wrapper +* @npages: Number of PFN entries to fill +* @pfn: Output PFN array +* +* Iterates over the BO's TTM vram_mgr buddy blocks and converts each +* block's VRAM offset to ZONE_DEVICE PFNs: +* +* PFN = PHYS_PFN(block_offset + apagemap.hpa_base) + page_index +* +* This is called by drm_pagemap_migrate_to_devmem() to build the +* destination PFN array for migrate_vma_pages(). +* +* Return: 0 on success +*/ +static int +amdgpu_svm_populate_devmem_pfn (struct drm_pagemap_devmem *devmem_allocation, +unsigned long npages,unsigned long *pfn) +{ +struct amdgpu_pagemap *svm_dm =to_amdgpu_pagemap (devmem_allocation->dpagemap); +struct amdgpu_svm_bo *svm_bo =to_amdgpu_svm_bo (devmem_allocation);drivers/gpu/drm/amd/amdgpu/amdgpu_migrate.c amdgpu_svm_page_to_apagemap(struct page *page) · line 182
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_migrate.c +10
-0+struct amdgpu_bo *bo = svm_bo->bo; +struct amdgpu_res_cursor cursor; +unsigned long i =0 ; +int ret; + + ret =amdgpu_bo_reserve (bo, false); +if (ret) +return ret; + +amdgpu_res_first (bo->tbo.resource,0 , npages <<PAGE_SHIFT , &cursor);How do you ensure that the BO is valid and actually in VRAM at the time? And how do you ensure that it stays there as long as the
zone_devicepages are in use? As far as I can tell, the BO is not pinned and there is no fence that prevents it from being evicted to GTT by TTM without warning.
Quoted 3 lines
Regards,
Felix
Hi Felix,
Thanks for the review.
You're right — the BO is not pinned and the current version lacks protection against TTM eviction, so there is no guarantee that the BO stays in VRAM while zone_device pages are in use.
The v1 series had an eviction fence patch to address this, but the implementation had issues, so it was dropped in v2.
I'm working on a proper eviction support implementation that will migrate zone_device pages back to system RAM before TTM reclaims the VRAM backing. This will be added in a follow-up series.
Regards,
Junhua
Quoted 527 lines
Quoted 527 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_migrate.c +524
-1+while (cursor.remaining && i < npages) { +u64 pfn_base =PHYS_PFN (cursor.start + svm_dm->hpa_base); +u64 pages = cursor.size >>PAGE_SHIFT ; +unsigned long j; + +for (j =0 ; j < pages && i < npages; j++, i++) + pfn[i] = pfn_base + j; + +amdgpu_res_next (&cursor, cursor.size); + } + +amdgpu_bo_unreserve (bo); + +AMDGPU_MIGRATE_TRACE ("populate_devmem_pfn: npages=%lu first_pfn=0x%lx\n" , + npages, npages >0 ? pfn[0 ] :0 ); + +return 0 ; +} + +/* SDMA copy helpers — GART window based data transfer */ + +/** +* amdgpu_svm_direct_mapping_addr - Convert VRAM offset to MC address +* @adev: AMDGPU device +* @vram_offset: Byte offset within VRAM +* +* Return: MC address suitable for SDMA src/dst +*/ +static u64 +amdgpu_svm_direct_mapping_addr (struct amdgpu_device *adev,u64 vram_offset) +{ +return vram_offset +amdgpu_ttm_domain_start (adev,TTM_PL_VRAM ); +} + +/** +* amdgpu_svm_gart_map - Map system DMA addresses into GART window +* @ring: SDMA ring for the GART update job +* @npages: Number of pages to map +* @addr: Array of system memory DMA addresses +* @gart_addr: Output — GART base address to use in SDMA copy +* @flags: PTE flags (e.g. writeable for RAM-to-VRAM src) +* +* Builds GART PTEs pointing at the given DMA addresses, submits an +* SDMA job to update the GART entries, and returns the GART address +* that can be used as src or dst in a subsequent amdgpu_copy_buffer(). +* +* Uses GART window 0, protected by gtt_window_lock. +* +* Return: 0 on success, negative error code on failure +*/ +static int +amdgpu_svm_gart_map (struct amdgpu_ring *ring, +struct amdgpu_ttm_buffer_entity *entity, +u64 npages, +dma_addr_t *addr,u64 *gart_addr,u64 flags) +{ +struct amdgpu_device *adev = ring->adev; +struct amdgpu_job *job; +unsigned int num_dw, num_bytes; +struct dma_fence *fence; +u64 src_addr, dst_addr; +u64 pte_flags; +void *cpu_addr; +int r; + +/* Use entity's GART window 0 */ + *gart_addr =amdgpu_compute_gart_address (&adev->gmc, entity,0 ); + + num_dw =ALIGN (adev->mman.buffer_funcs->copy_num_dw,8 ); + num_bytes = npages *8 *AMDGPU_GPU_PAGES_IN_CPU_PAGE ; + + r =amdgpu_job_alloc_with_ib (adev, &entity->base, +AMDGPU_FENCE_OWNER_UNDEFINED , + num_dw *4 + num_bytes, +AMDGPU_IB_POOL_DELAYED , + &job, +AMDGPU_KERNEL_JOB_ID_KFD_GART_MAP ); +if (r) +return r; + + src_addr = num_dw *4 ; + src_addr += job->ibs[0 ].gpu_addr; + + dst_addr =amdgpu_bo_gpu_offset (adev->gart.bo); + dst_addr += (entity->gart_window_offs[0 ] >>AMDGPU_GPU_PAGE_SHIFT ) *8 ; +amdgpu_emit_copy_buffer (adev, &job->ibs[0 ], src_addr, + dst_addr, num_bytes,0 ); + +amdgpu_ring_pad_ib (ring, &job->ibs[0 ]); +WARN_ON (job->ibs[0 ].length_dw > num_dw); + + pte_flags =AMDGPU_PTE_VALID |AMDGPU_PTE_READABLE ; + pte_flags |=AMDGPU_PTE_SYSTEM |AMDGPU_PTE_SNOOPED ; +if (flags &AMDGPU_PTE_WRITEABLE ) + pte_flags |=AMDGPU_PTE_WRITEABLE ; + pte_flags |= adev->gart.gart_pte_flags; + + cpu_addr = &job->ibs[0 ].ptr[num_dw]; + +amdgpu_gart_map (adev,0 , npages, addr, pte_flags, cpu_addr); + fence =amdgpu_job_submit (job); +dma_fence_put (fence); + +return 0 ; +} + +/** +* amdgpu_svm_copy_memory_gart - SDMA copy between system RAM and VRAM +* @adev: AMDGPU device +* @sys: Array of DMA addresses for system memory pages +* @vram: Array of VRAM byte offsets (relative to start of VRAM) +* @npages: Number of pages to copy +* @direction: FROM_RAM_TO_VRAM or FROM_VRAM_TO_RAM +* @mfence: In/out — carries the last SDMA fence for serialization +* +* Maps system memory pages into the GART window and uses SDMA to copy +* data to/from VRAM. Handles splitting into AMDGPU_GTT_MAX_TRANSFER_SIZE +* chunks. Acquires entity->lock internally to protect the GART window, +* matching the KFD svm_migrate_copy_memory_gart() pattern. +* +* Return: 0 on success, negative error code on failure +*/ +static int +amdgpu_svm_copy_memory_gart (struct amdgpu_device *adev,dma_addr_t *sys, +u64 *vram,u64 npages,int direction, +struct dma_fence **mfence) +{ +const u64 max_pages =AMDGPU_GTT_MAX_TRANSFER_SIZE ; +struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring; +struct amdgpu_ttm_buffer_entity *entity = &adev->mman.move_entity; +u64 gart_s, gart_d; +struct dma_fence *next; +u64 size; +int r; + +mutex_lock (&entity->lock); + +while (npages) { + size =min (max_pages, npages); + +if (direction ==FROM_VRAM_TO_RAM ) { + gart_s =amdgpu_svm_direct_mapping_addr (adev, *vram); + r =amdgpu_svm_gart_map (ring, entity, size, sys, + &gart_d,AMDGPU_PTE_WRITEABLE ); + }else { + r =amdgpu_svm_gart_map (ring, entity, size, sys, + &gart_s,0 ); + gart_d =amdgpu_svm_direct_mapping_addr (adev, *vram); + } +if (r) { +dev_err (adev->dev,"failed %d to map GART for SDMA\n" , r); + goto out_unlock; + } + +AMDGPU_MIGRATE_TRACE ("SDMA_COPY: %s npages=%llu vram_off=0x%llx\n" , + direction ==FROM_RAM_TO_VRAM ?"RAM->VRAM" :"VRAM->RAM" , + size, (u64)*vram); + + r =amdgpu_copy_buffer (adev, entity, gart_s, gart_d, + size *PAGE_SIZE , +NULL , &next, true,0 ); +if (r) { +dev_err (adev->dev,"failed %d to copy buffer\n" , r); + goto out_unlock; + } + +dma_fence_put (*mfence); + *mfence = next; + npages -= size; +if (npages) { + sys += size; + vram += size; + } + } + +out_unlock: +mutex_unlock (&entity->lock); + +return r; +} + +/** +* amdgpu_svm_copy_to_devmem - SDMA copy system memory -> VRAM +* @pages: Array of destination ZONE_DEVICE pages (VRAM-backed) +* @pagemap_addr: Array of source DMA addresses (system memory, already mapped) +* @npages: Number of pages to copy +* +* Builds parallel sys[] and vram[] arrays from the framework-provided +* pagemap_addr and device pages, then submits batched SDMA copies via +* the GART window. +* +* Return: 0 on success, negative error code on failure +*/ +static int +amdgpu_svm_copy_to_devmem (struct page **pages, +struct drm_pagemap_addr *pagemap_addr, +unsigned long npages, +struct dma_fence *pre_migrate_fence) +{ +struct amdgpu_device *adev; +struct amdgpu_pagemap *svm_dm; +struct dma_fence *mfence =NULL ; +dma_addr_t *sys; +u64 *vram; +unsigned long i, j; +int ret =0 ; + +if (!npages) +return 0 ; + +/* +* Find the first non-NULL page to derive the device. +* The pages array may contain NULL entries for positions where +* no valid device page exists. +*/ +for (i =0 ; i < npages; i++) { +if (pages[i]) +break ; + } +if (i == npages) +return 0 ; + + svm_dm =amdgpu_svm_page_to_apagemap (pages[i]); + adev = svm_dm->adev; + + sys =kvcalloc (npages,sizeof (*sys),GFP_KERNEL ); + vram =kvcalloc (npages,sizeof (*vram),GFP_KERNEL ); +if (!sys || !vram) { + ret = -ENOMEM ; + goto out_free; + } + +for (i =0 , j =0 ; i < npages; i++) { +if (!pagemap_addr[i].addr) + goto flush; + + sys[j] = pagemap_addr[i].addr; + vram[j] = ((u64 )page_to_pfn (pages[i]) <<PAGE_SHIFT ) - + svm_dm->hpa_base; + +/* Check if next vram page is contiguous with current */ +if (j >0 && vram[j] != vram[j -1 ] +PAGE_SIZE ) + goto flush; + + j++; +continue ; +flush: +if (j) { + ret =amdgpu_svm_copy_memory_gart (adev, sys, vram, j, +FROM_RAM_TO_VRAM , + &mfence); +if (ret) + goto out_fence; + j =0 ; + } +/* Re-process current page if it was valid but broke contiguity */ +if (pagemap_addr[i].addr) { + sys[0 ] = pagemap_addr[i].addr; + vram[0 ] = ((u64 )page_to_pfn (pages[i]) <<PAGE_SHIFT ) - + svm_dm->hpa_base; + j =1 ; + } + } + +/* Flush remaining batch */ +if (j) + ret =amdgpu_svm_copy_memory_gart (adev, sys, vram, j, +FROM_RAM_TO_VRAM , &mfence); + +out_fence: +if (mfence) { +dma_fence_wait (mfence, false); +dma_fence_put (mfence); + } + +AMDGPU_MIGRATE_TRACE ("copy_to_devmem done: npages=%ld ret=%d\n" , + npages, ret); + +out_free: +kvfree (vram); +kvfree (sys); +return ret; +} + +/** +* amdgpu_svm_copy_to_ram - SDMA copy VRAM -> system memory +* @pages: Array of source ZONE_DEVICE pages (VRAM-backed) +* @pagemap_addr: Array of destination DMA addresses (system memory, already mapped) +* @npages: Number of pages to copy +* +* Mirror of copy_to_devmem with src/dst swapped. +* +* Return: 0 on success, negative error code on failure +*/ +static int +amdgpu_svm_copy_to_ram (struct page **pages, +struct drm_pagemap_addr *pagemap_addr, +unsigned long npages, +struct dma_fence *pre_migrate_fence) +{ +struct amdgpu_device *adev; +struct amdgpu_pagemap *svm_dm; +struct dma_fence *mfence =NULL ; +dma_addr_t *sys; +u64 *vram; +unsigned long i, j; +int ret =0 ; + +if (!npages) +return 0 ; + +for (i =0 ; i < npages; i++) { +if (pages[i]) +break ; + } +if (i == npages) +return 0 ; + + svm_dm =amdgpu_svm_page_to_apagemap (pages[i]); + adev = svm_dm->adev; + + sys =kvcalloc (npages,sizeof (*sys),GFP_KERNEL ); + vram =kvcalloc (npages,sizeof (*vram),GFP_KERNEL ); +if (!sys || !vram) { + ret = -ENOMEM ; + goto out_free; + } + +for (i =0 , j =0 ; i < npages; i++) { +if (!pagemap_addr[i].addr || !pages[i]) + goto flush; + + vram[j] = ((u64 )page_to_pfn (pages[i]) <<PAGE_SHIFT ) - + svm_dm->hpa_base; + sys[j] = pagemap_addr[i].addr; + +/* Check if next vram page is contiguous with current */ +if (j >0 && vram[j] != vram[j -1 ] +PAGE_SIZE ) + goto flush; + + j++; +continue ; +flush: +if (j) { + ret =amdgpu_svm_copy_memory_gart (adev, sys, vram, j, +FROM_VRAM_TO_RAM , + &mfence); +if (ret) + goto out_fence; + j =0 ; + } +/* Re-process current page if it was valid but broke contiguity */ +if (pagemap_addr[i].addr && pages[i]) { + vram[0 ] = ((u64 )page_to_pfn (pages[i]) <<PAGE_SHIFT ) - + svm_dm->hpa_base; + sys[0 ] = pagemap_addr[i].addr; + j =1 ; + } + } + +/* Flush remaining batch */ +if (j) + ret =amdgpu_svm_copy_memory_gart (adev, sys, vram, j, +FROM_VRAM_TO_RAM , &mfence); + +out_fence: +if (mfence) { +dma_fence_wait (mfence, false); +dma_fence_put (mfence); + } + +AMDGPU_MIGRATE_TRACE ("copy_to_ram done: npages=%ld ret=%d\n" , npages, ret); + +out_free: +kvfree (vram); +kvfree (sys); +return ret; +} + +static const struct drm_pagemap_devmem_ops amdgpu_pagemap_ops = { + .devmem_release = amdgpu_svm_devmem_release, + .populate_devmem_pfn = amdgpu_svm_populate_devmem_pfn, + .copy_to_devmem = amdgpu_svm_copy_to_devmem, + .copy_to_ram = amdgpu_svm_copy_to_ram, +}; + +/* drm_pagemap_ops — top-level migration entry points */ + +/** +* amdgpu_svm_device_map - Convert ZONE_DEVICE page to GPU PTE address +* @dpagemap: The drm_pagemap for this device +* @dev: Requesting device (for P2P check) +* @page: ZONE_DEVICE page backed by VRAM +* @order: Page order (0 = 4K, 9 = 2M, etc.) +* @dir: DMA direction (unused for local VRAM) +* +* Address conversion chain: +* page -> PFN -> HPA -> VRAM offset -> PTE address +* +* HPA = page_to_pfn(page) << PAGE_SHIFT +* VRAM offset = HPA - apagemap.hpa_base +* PTE address = VRAM offset + adev->vm_manager.vram_base_offset +* +* Return: drm_pagemap_addr with PTE address and AMDGPU_INTERCONNECT_VRAM protocol +*/ +static struct drm_pagemap_addr +amdgpu_svm_device_map (struct drm_pagemap *dpagemap, +struct device *dev, +struct page *page, +unsigned int order, +enum dma_data_direction dir) +{ +struct amdgpu_pagemap *svm_dm =to_amdgpu_pagemap (dpagemap); +struct amdgpu_device *adev =dpagemap_to_adev (dpagemap); +dma_addr_t addr; + +if (dpagemap->drm->dev == dev) { +/* Same device: return VRAM PTE address */ +u64 hpa = (u64 )page_to_pfn (page) <<PAGE_SHIFT ; +u64 vram_offset = hpa - svm_dm->hpa_base; + + addr = vram_offset + adev->vm_manager.vram_base_offset; + }else { +/* Cross-device P2P: not yet supported */ + addr =DMA_MAPPING_ERROR ; + } + +return drm_pagemap_addr_encode (addr, +AMDGPU_INTERCONNECT_VRAM , order, dir); +} + +/** +* amdgpu_svm_bo_alloc - Allocate an amdgpu_svm_bo wrapper with VRAM backing +* @adev: AMDGPU device +* @dpagemap: The drm_pagemap for this device +* @mm: mm_struct of the owning process +* @size: Allocation size in bytes +* +* Return: Pointer to allocated amdgpu_svm_bo on success, ERR_PTR on failure +*/ +static struct amdgpu_svm_bo * +amdgpu_svm_bo_alloc (struct amdgpu_device *adev, +struct drm_pagemap *dpagemap, +struct mm_struct *mm,unsigned long size) +{ +struct amdgpu_svm_bo *svm_bo; +struct amdgpu_bo_param bp = {}; +struct amdgpu_bo *bo; +int ret; + + svm_bo =kzalloc (sizeof (*svm_bo),GFP_KERNEL ); +if (!svm_bo) +return ERR_PTR (-ENOMEM ); + + bp.size = size; + bp.bo_ptr_size =sizeof (struct amdgpu_bo ); + bp.domain =AMDGPU_GEM_DOMAIN_VRAM ; + bp.type = ttm_bo_type_device; + bp.flags =AMDGPU_GEM_CREATE_NO_CPU_ACCESS | +AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS | +AMDGPU_GEM_CREATE_VRAM_CLEARED ; + + ret =amdgpu_bo_create (adev, &bp, &bo); +if (ret) { +AMDGPU_MIGRATE_TRACE ("Failed to create SVM BO\n" ); +kfree (svm_bo); +return ERR_PTR (ret); + } + +amdgpu_bo_unreserve (bo); + svm_bo->bo = bo; + +drm_pagemap_devmem_init (&svm_bo->devmem, + adev->dev, mm, + &amdgpu_pagemap_ops, + dpagemap, size,NULL ); + +return svm_bo; +} + +/** +* amdgpu_svm_populate_mm - Allocate VRAM BO and migrate pages +* @dpagemap: The drm_pagemap for this device +* @start: Start virtual address of the range to migrate +* @end: End virtual address (exclusive) +* @mm: mm_struct of the owning process +* @timeslice_ms: Maximum time to spend migrating (for fairness) +* +* Core migration entry point called by drm_pagemap_populate_mm(). +* Allocates an amdgpu_svm_bo via amdgpu_svm_bo_alloc(), then calls +* drm_pagemap_migrate_to_devmem() to execute the actual migration. +* +* Return: 0 on success, negative error code on failure +*/ +static int +amdgpu_svm_populate_mm (struct drm_pagemap *dpagemap, +unsigned long start,unsigned long end, +struct mm_struct *mm, +unsigned long timeslice_ms) +{ +struct amdgpu_device *adev =dpagemap_to_adev (dpagemap); +struct drm_pagemap_migrate_details mdetails = { + .timeslice_ms = timeslice_ms, + }; +struct amdgpu_svm_bo *svm_bo; +int ret; + + svm_bo =amdgpu_svm_bo_alloc (adev, dpagemap, mm, end - start); +if (IS_ERR (svm_bo)) +return PTR_ERR (svm_bo); + +AMDGPU_MIGRATE_TRACE ("populate_mm: [0x%lx-0x%lx] size=%lu\n" , + start, end, end - start); + + ret =drm_pagemap_migrate_to_devmem (&svm_bo->devmem, + mm, start, end, + &mdetails); + +return ret; +} -const struct drm_pagemap_ops amdgpu_svm_drm_pagemap_ops = { }; +const struct drm_pagemap_ops amdgpu_svm_drm_pagemap_ops = { + .device_map = amdgpu_svm_device_map, + .populate_mm = amdgpu_svm_populate_mm, +};/** * amdgpu_svm_migration_init - Register ZONE_DEVICE and initialize drm_pagemap
PATCH v3 3/5 drm/amdgpu: introduce SVM range migration decision layer
In reply to Junhua ShenAdd the migration decision infrastructure for drm_pagemap-based SVM VRAM migration:
- Define enum amdgpu_svm_migrate_mode (NONE, TO_VRAM, TO_SYSMEM, PREFERRED) to carry migration intent
- Implement amdgpu_svm_range_migrate_range() which handles both
directions: TO_VRAM via drm_pagemap_populate_mm(), TO_SYSMEM via drm_gpusvm_range_evict()
--- drivers/gpu/drm/amd/amdgpu/Makefile | 6 +- .../drm/amd/amdgpu/amdgpu_svm_range_migrate.c | 140 ++++++++++++++++++ .../drm/amd/amdgpu/amdgpu_svm_range_migrate.h | 60 ++++++++ 3 files changed, 203 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
drivers/gpu/drm/amd/amdgpu/Makefile +3 -3
@@ -323,14 +323,14 @@ amdgpu-$(CONFIG_HMM_MIRROR) += amdgpu_hmm.o # svm support amdgpu-$(CONFIG_DRM_AMDGPU_SVM) += amdgpu_svm.o amdgpu_svm_attr.o \ - amdgpu_svm_range.o amdgpu_migrate.o + amdgpu_svm_range.o amdgpu_svm_range_migrate.o amdgpu_migrate.o .PHONY: clean-svm clean-svm: rm -f $(obj)/amdgpu_svm.o $(obj)/amdgpu_svm_attr.o $(obj)/amdgpu_svm_range.o \ - $(obj)/amdgpu_migrate.o \ + $(obj)/amdgpu_svm_range_migrate.o $(obj)/amdgpu_migrate.o \ $(obj)/.amdgpu_svm.o.cmd $(obj)/.amdgpu_svm_attr.o.cmd $(obj)/.amdgpu_svm_range.o.cmd \ - $(obj)/.amdgpu_migrate.o.cmd + $(obj)/.amdgpu_svm_range_migrate.o.cmd $(obj)/.amdgpu_migrate.o.cmd include $(FULL_AMD_PATH)/pm/Makefile
drivers/gpu/drm/amd/amdgpu/amdgpu_svm_range_migrate.c new file +140 -0
140 lines not on this page · raw message
drivers/gpu/drm/amd/amdgpu/amdgpu_svm_range_migrate.h new file +60 -0
60 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v3 4/5 drm/amdgpu: add SVM attr prefetch/force-trigger functionality
In reply to Junhua ShenAdd attr_has_prefetch_loc() helper to detect prefetch-loc attributes and use it to force-trigger migration even when the stored value has not changed (prefetch is a one-shot command).
Refine attr_change_ctx_trigger() to only fire LOCATION_CHANGE on prefetch_loc changes, not preferred_loc changes.
--- drivers/gpu/drm/amd/amdgpu/amdgpu_svm_attr.c | 34 +++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_svm_attr.c +26 -8
@@ -159,8 +159,7 @@ attr_change_ctx_trigger(const struct amdgpu_svm_attrs *prev_attrs, trigger |=AMDGPU_SVM_ATTR_TRIGGER_PTE_FLAG_CHANGE ;if (changed_flags &AMDGPU_SVM_MAPPING_FLAG_MASK ) trigger |=AMDGPU_SVM_ATTR_TRIGGER_MAPPING_FLAG_CHANGE ; -if (prev_attrs->preferred_loc != new_attrs->preferred_loc || - prev_attrs->prefetch_loc != new_attrs->prefetch_loc) +if (prev_attrs->prefetch_loc != new_attrs->prefetch_loc) trigger |=AMDGPU_SVM_ATTR_TRIGGER_LOCATION_CHANGE ;if (prev_attrs->granularity != new_attrs->granularity) trigger |=AMDGPU_SVM_ATTR_TRIGGER_GRANULARITY_CHANGE ;
@@ -187,9 +186,22 @@ static bool attr_has_access(uint32_t nattr,return false; } +static bool attr_has_prefetch_loc (uint32_t nattr, +const struct drm_amdgpu_svm_attribute *attrs) +{ +uint32_t i; + +for (i =0 ; i < nattr; i++) { +if (attrs[i].type ==AMDGPU_SVM_ATTR_PREFETCH_LOC ) +return true; + } + +return false; +} +static struct amdgpu_svm_attr_range *attr_alloc_range (unsigned long start, -unsigned long last, +unsigned long last,const struct amdgpu_svm_attrs *attrs) {struct amdgpu_svm_attr_range *range;
@@ -477,23 +489,29 @@ amdgpu_svm_attr_set_existing(struct amdgpu_svm_attr_tree *attr_tree,struct amdgpu_svm_attrs old_attrs;struct amdgpu_svm_attrs new_attrs;uint32_t trigger; -bool force_trigger; +uint32_t force_trigger =0 ;lockdep_assert_held (&attr_tree->lock); old_attrs = range->attrs; -/* The attr layer doesn't store the gpu mapped state, and for align with KFD, -* need force trigger range layer to check if gpu mapped. +/* +* Force the range layer to act even when the attr value is +* unchanged: ACCESS needs re-check on xnack-off because the +* attr layer has no gpu_mapped state; PREFETCH_LOC is one-shot +* so repeated prefetch must always trigger migration. */ - force_trigger = !attr_tree->svm->xnack_enabled &&attr_has_access (nattr, attrs); +if (!attr_tree->svm->xnack_enabled &&attr_has_access (nattr, attrs)) + force_trigger |=AMDGPU_SVM_ATTR_TRIGGER_ACCESS_CHANGE ; +if (attr_has_prefetch_loc (nattr, attrs)) + force_trigger |=AMDGPU_SVM_ATTR_TRIGGER_LOCATION_CHANGE ;if (attr_same_attrs (range, nattr, attrs)) {if (!force_trigger)return 0 ;amdgpu_svm_attr_change_ctx_set (change, start, last, -AMDGPU_SVM_ATTR_TRIGGER_ACCESS_CHANGE , + force_trigger, &old_attrs, &old_attrs);return 0 ; }
Signature · 1 line
2.34.1
PATCH v3 5/5 drm/amdgpu: integrate VRAM migration into SVM range map path
In reply to Junhua Shenhook up ZONE_DEVICE registration in device init and reset.
Wire the migration decision layer into the SVM range map call chain:
- Add migrate_mode parameter to amdgpu_svm_range_map_attr_ranges() and propagate it through map_interval -> per-range map loop
- Call amdgpu_svm_range_migrate_range() on each drm_gpusvm_range before GPU mapping to perform VRAM migration or eviction
- Pass appropriate migrate_mode from SVM attr prefetch triggers and the restore worker
--- drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 4 + drivers/gpu/drm/amd/amdgpu/amdgpu_reset.c | 4 + drivers/gpu/drm/amd/amdgpu/amdgpu_svm.c | 4 +- drivers/gpu/drm/amd/amdgpu/amdgpu_svm_range.c | 136 +++++++++--------- drivers/gpu/drm/amd/amdgpu/amdgpu_svm_range.h | 5 +- 5 files changed, 87 insertions(+), 66 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +4 -0
@@ -78,6 +78,7 @@#include "amdgpu_reset.h" #include "amdgpu_virt.h" #include "amdgpu_dev_coredump.h" +#include "amdgpu_migrate.h" #include <linux/suspend.h> #include <drm/task_barrier.h>
@@ -4076,6 +4077,9 @@ int amdgpu_device_init(struct amdgpu_device *adev,/* Don't init kfd if whole hive need to be reset during init */ if (adev->init_lvl->level !=AMDGPU_INIT_LEVEL_MINIMAL_XGMI ) { +#if IS_ENABLED (CONFIG_DRM_AMDGPU_SVM ) +amdgpu_svm_migration_init (adev); +#endif kgd2kfd_init_zone_device (adev);kfd_update_svm_support_properties (adev); }
drivers/gpu/drm/amd/amdgpu/amdgpu_reset.c +4 -0
@@ -25,6 +25,7 @@#include "aldebaran.h" #include "sienna_cichlid.h" #include "smu_v13_0_10.h" +#include "amdgpu_migrate.h" static int amdgpu_reset_xgmi_reset_on_init_suspend (struct amdgpu_device *adev) {
@@ -87,6 +88,9 @@ static int amdgpu_reset_xgmi_reset_on_init_restore_hwctxt(return r;list_for_each_entry (tmp_adev, reset_device_list, reset_list) {if (!tmp_adev->kfd.init_complete) { +#if IS_ENABLED (CONFIG_DRM_AMDGPU_SVM ) +amdgpu_svm_migration_init (tmp_adev); +#endif kgd2kfd_init_zone_device (tmp_adev);amdgpu_amdkfd_device_init (tmp_adev);amdgpu_amdkfd_drm_client_create (tmp_adev);
drivers/gpu/drm/amd/amdgpu/amdgpu_svm.c +3 -1
@@ -32,6 +32,7 @@#include "amdgpu_svm.h" #include "amdgpu_svm_attr.h" #include "amdgpu_svm_range.h" +#include "amdgpu_svm_range_migrate.h" #include "amdgpu_vm.h" #if IS_ENABLED (CONFIG_DRM_AMDGPU_SVM )
@@ -341,7 +342,8 @@ int amdgpu_svm_handle_fault(struct amdgpu_device *adev, uint32_t pasid,AMDGPU_SVM_TRACE ("handle_fault: map_attr page=0x%lx\n" , fault_page);down_write (&svm->svm_lock); - ret =amdgpu_svm_range_map_attr_ranges (svm, fault_page, fault_page); + ret =amdgpu_svm_range_map_attr_ranges (svm, fault_page, fault_page, +AMDGPU_SVM_MIGRATE_PREFERRED );up_write (&svm->svm_lock);if (ret)
drivers/gpu/drm/amd/amdgpu/amdgpu_svm_range.c +72 -64
276 lines not on this page · raw message
drivers/gpu/drm/amd/amdgpu/amdgpu_svm_range.h +4 -1
@@ -30,6 +30,8 @@#include <linux/list.h> #include <linux/types.h> +#include "amdgpu_svm_range_migrate.h" +struct amdgpu_svm ;struct amdgpu_svm_attrs ;struct drm_gpusvm_notifier ;
@@ -62,7 +64,8 @@ void amdgpu_svm_range_flush(struct amdgpu_svm *svm);void amdgpu_svm_range_sync_work (struct amdgpu_svm *svm);int amdgpu_svm_range_map_attr_ranges (struct amdgpu_svm *svm,unsigned long start_page, -unsigned long last_page); +unsigned long last_page, +enum amdgpu_svm_migrate_mode migrate_mode); int amdgpu_svm_range_apply_attr_change( struct amdgpu_svm *svm, unsigned long start, unsigned long last, uint32_t trigger, const struct amdgpu_svm_attrs *prev_attrs,
Signature · 1 line
2.34.1