On 11/11/26 20:23, Zhigang Luo wrote:
> gmc_v12_1_get_vm_pde() converts a VRAM MC address to a physical
> address and skips that conversion when AMDGPU_PTE_SYSTEM is already
> set. Set the bit after the translation so the address stays physical.
> 
> PDB0 is filled through amdgpu_gmc_get_vm_pde() so those entries take
> the same path. The VRAM entries are PDE_PTEs and already hold a
> physical address, so the translation is skipped. The GART PDE is
> given the GART BO's MC address and translated once.
> 
> gmc_v12_1_get_vm_pte() sets the bit only for VRAM. SVM VRAM ranges
> call it with a NULL buffer object and leave the bit clear. NPA
> mappings are left unchanged.
> 
> Signed-off-by: Zhigang Luo <[email protected]>
> ---
>  drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c | 22 ++++++++++++++++------
>  drivers/gpu/drm/amd/amdgpu/gmc_v12_1.c  | 12 +++++++++++-
>  2 files changed, 27 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c 
> b/drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c
> index 659e89e5e78f..04cf02dd22ac 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c
> @@ -1272,7 +1272,7 @@ void amdgpu_gmc_init_pdb0(struct amdgpu_device *adev)
>       u64 vram_size = adev->gmc.xgmi.node_segment_size * 
> adev->gmc.xgmi.num_physical_nodes;
>       u64 pde0_page_size = (1ULL<<adev->gmc.vmid0_page_table_block_size)<<21;
>       u64 vram_addr, vram_end;
> -     u64 gart_ptb_gpu_pa = amdgpu_gmc_vram_pa(adev, adev->gart.bo);
> +     u64 pde_addr, pde_flags;
>       int idx;
>  
>       if (!drm_dev_enter(adev_to_drm(adev), &idx))
> @@ -1290,10 +1290,16 @@ void amdgpu_gmc_init_pdb0(struct amdgpu_device *adev)
>       vram_end = vram_addr + vram_size;
>  
>       /* The first n PDE0 entries are used as PTE,
> -      * pointing to vram
> +      * pointing to vram. PDE_PTE already holds a physical address, so
> +      * get_vm_pde() skips translation and sets S for SPM.
>        */
> -     for (i = 0; vram_addr < vram_end; i++, vram_addr += pde0_page_size)
> -             amdgpu_gmc_set_pte_pde(adev, adev->gmc.ptr_pdb0, i, vram_addr, 
> flags);
> +     for (i = 0; vram_addr < vram_end; i++, vram_addr += pde0_page_size) {
> +             pde_addr = vram_addr;
> +             pde_flags = flags;
> +             amdgpu_gmc_get_vm_pde(adev, AMDGPU_VM_PDB0, &pde_addr, 
> &pde_flags);
> +             amdgpu_gmc_set_pte_pde(adev, adev->gmc.ptr_pdb0, i, pde_addr,
> +                                    pde_flags);
> +     }

That is still a little bit hacky but certainly better than before.

>  
>       /* The n+1'th PDE0 entry points to a huge
>        * PTB who has more than 512 entries each
> @@ -1301,8 +1307,12 @@ void amdgpu_gmc_init_pdb0(struct amdgpu_device *adev)
>        */
>       flags = AMDGPU_PTE_VALID;
>       flags |= AMDGPU_PTE_SNOOPED | AMDGPU_PDE_BFS_FLAG(adev, 0);
> -     /* Requires gart_ptb_gpu_pa to be 4K aligned */
> -     amdgpu_gmc_set_pte_pde(adev, adev->gmc.ptr_pdb0, i, gart_ptb_gpu_pa, 
> flags);
> +     /* MC address. get_vm_pde() translates it once and sets S for SPM.
> +      * Requires the GART PT to be 4K aligned.
> +      */
> +     pde_addr = amdgpu_bo_gpu_offset(adev->gart.bo);
> +     amdgpu_gmc_get_vm_pde(adev, AMDGPU_VM_PDB0, &pde_addr, &flags);
> +     amdgpu_gmc_set_pte_pde(adev, adev->gmc.ptr_pdb0, i, pde_addr, flags);

Please move those two changes into a separate patch.

>       drm_dev_exit(idx);
>  }
>  
> diff --git a/drivers/gpu/drm/amd/amdgpu/gmc_v12_1.c 
> b/drivers/gpu/drm/amd/amdgpu/gmc_v12_1.c
> index 76391fca6c62..0fbf4525947a 100644
> --- a/drivers/gpu/drm/amd/amdgpu/gmc_v12_1.c
> +++ b/drivers/gpu/drm/amd/amdgpu/gmc_v12_1.c
> @@ -586,6 +586,12 @@ static void gmc_v12_1_get_vm_pde(struct amdgpu_device 
> *adev, int level,
>               *addr = adev->vm_manager.vram_base_offset + *addr -
>                       adev->gmc.vram_start;
>  
> +     /* SPM VRAM is reached at a system physical address. Set S after the
> +      * translation above so that translation is not skipped.
> +      */
> +     if (amdgpu_virt_vram_is_spm(adev))
> +             *flags |= AMDGPU_PTE_SYSTEM;
> +
>       if (*addr & ~adev->gmc.pte_addr_mask) {
>               dev_err(adev->dev, "Invalid PDE address: %llx\n", *addr);
>               *addr = 0;
> @@ -756,8 +762,12 @@ static void gmc_v12_1_get_vm_pte(struct amdgpu_device 
> *adev,
>               break;
>       }
>  
> +     mem = bo ? bo->tbo.resource : NULL;
> +     if (amdgpu_virt_vram_is_spm(adev) &&
> +         (!mem || mem->mem_type == TTM_PL_VRAM))
> +             *flags |= AMDGPU_PTE_SYSTEM;
> +
>       if (bo) {
> -             mem = bo->tbo.resource;

Move the check here, this way you don't need to check if there is a BO any more 
and already have mem.

Apart from those minor comments that looks good to me now.

Regards,
Christian.

>               if (mem && mem->mem_type == AMDGPU_PL_NPA) {
>                       dev_dbg(adev->dev,
>                               "Setting PTE for NPA BO, mem->type: %d, 
> mem->start: %lx, mem->size: %u, cur_flags: %llx\n",

Reply via email to