<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<p style="font-family:Arial;font-size:10pt;color:#0000FF;margin:5pt;" align="Left">
[AMD Official Use Only - General]<br>
</p>
<br>
<div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof ContentPasted0">
Reviewed-by: Mukul Joshi <mukul.joshi@amd.com><br>
</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Alex Deucher <alexdeucher@gmail.com><br>
<b>Sent:</b> Wednesday, May 17, 2023 12:28 PM<br>
<b>To:</b> Joshi, Mukul <Mukul.Joshi@amd.com><br>
<b>Cc:</b> Deucher, Alexander <Alexander.Deucher@amd.com>; amd-gfx@lists.freedesktop.org <amd-gfx@lists.freedesktop.org>; Chen, Guchun <Guchun.Chen@amd.com><br>
<b>Subject:</b> Re: [PATCH] drm/amdgpu/gmc9: fix 64 bit division in partition code</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.<br>
<br>
<br>
On Wed, May 17, 2023 at 12:26 PM Joshi, Mukul <Mukul.Joshi@amd.com> wrote:<br>
><br>
> [AMD Official Use Only - General]<br>
><br>
><br>
> Hi Alex,<br>
><br>
> ________________________________<br>
> From: amd-gfx <amd-gfx-bounces@lists.freedesktop.org> on behalf of Alex Deucher <alexander.deucher@amd.com><br>
> Sent: Wednesday, May 17, 2023 10:31 AM<br>
> To: amd-gfx@lists.freedesktop.org <amd-gfx@lists.freedesktop.org><br>
> Cc: Deucher, Alexander <Alexander.Deucher@amd.com>; Chen, Guchun <Guchun.Chen@amd.com><br>
> Subject: [PATCH] drm/amdgpu/gmc9: fix 64 bit division in partition code<br>
><br>
> Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.<br>
><br>
><br>
> Rework logic or use do_div() to avoid problems on 32 bit.<br>
><br>
> v2: add a missing case for XCP macro<br>
><br>
> Acked-by: Guchun Chen <guchun.chen@amd.com> (v1)<br>
> Signed-off-by: Alex Deucher <alexander.deucher@amd.com><br>
> ---<br>
> drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c | 14 ++++++++++++++<br>
> drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 9 ++++-----<br>
> drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 5 ++++-<br>
> drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c | 11 ++++++-----<br>
> 4 files changed, 28 insertions(+), 11 deletions(-)<br>
><br>
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c<br>
> index 739eb7c0d133..fed545cdd1de 100644<br>
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c<br>
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c<br>
> @@ -794,3 +794,17 @@ void amdgpu_amdkfd_unlock_kfd(struct amdgpu_device *adev)<br>
> {<br>
> kgd2kfd_unlock_kfd();<br>
> }<br>
> +<br>
> +<br>
> +u64 amdgpu_amdkfd_xcp_memory_size(struct amdgpu_device *adev, int xcp_id)<br>
> +{<br>
> + u64 tmp;<br>
> +<br>
> + if (adev->gmc.num_mem_partitions && xcp_id >= 0) {<br>
> + tmp = adev->gmc.mem_partitions[KFD_XCP_MEM_ID(adev, xcp_id)].size;<br>
> + do_div(tmp, adev->xcp_mgr->num_xcp_per_mem_partition);<br>
> + return tmp;<br>
> + } else {<br>
> + return adev->gmc.real_vram_size;<br>
> + }<br>
> +}<br>
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h<br>
> index be43d71ba7ef..94cc456761e5 100644<br>
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h<br>
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h<br>
> @@ -333,15 +333,14 @@ int amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device *adev,<br>
> void amdgpu_amdkfd_unreserve_mem_limit(struct amdgpu_device *adev,<br>
> uint64_t size, u32 alloc_flag, int8_t xcp_id);<br>
><br>
> +u64 amdgpu_amdkfd_xcp_memory_size(struct amdgpu_device *adev, int xcp_id);<br>
> +<br>
> #define KFD_XCP_MEM_ID(adev, xcp_id) \<br>
> ((adev)->xcp_mgr && (xcp_id) >= 0 ?\<br>
> (adev)->xcp_mgr->xcp[(xcp_id)].mem_id : -1)<br>
><br>
> -#define KFD_XCP_MEMORY_SIZE(adev, xcp_id)\<br>
> - ((adev)->gmc.num_mem_partitions && (xcp_id) >= 0 ?\<br>
> - (adev)->gmc.mem_partitions[KFD_XCP_MEM_ID((adev), (xcp_id))].size /\<br>
> - (adev)->xcp_mgr->num_xcp_per_mem_partition :\<br>
> - (adev)->gmc.real_vram_size)<br>
> +#define KFD_XCP_MEMORY_SIZE(adev, xcp_id) amdgpu_amdkfd_xcp_memory_size((adev), (xcp_id))<br>
> +<br>
><br>
> #if IS_ENABLED(CONFIG_HSA_AMD)<br>
> void amdgpu_amdkfd_gpuvm_init_mem_limits(void);<br>
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c<br>
> index ad664ef640ff..34724b771ace 100644<br>
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c<br>
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c<br>
> @@ -818,11 +818,14 @@ static void amdgpu_ttm_gart_bind_gfx9_mqd(struct amdgpu_device *adev,<br>
> struct amdgpu_ttm_tt *gtt = (void *)ttm;<br>
> uint64_t total_pages = ttm->num_pages;<br>
> int num_xcc = max(1U, adev->gfx.num_xcc_per_xcp);<br>
> - uint64_t page_idx, pages_per_xcc = total_pages / num_xcc;<br>
> + uint64_t page_idx, pages_per_xcc;<br>
> int i;<br>
> uint64_t ctrl_flags = (flags & ~AMDGPU_PTE_MTYPE_VG10_MASK) |<br>
> AMDGPU_PTE_MTYPE_VG10(AMDGPU_MTYPE_NC);<br>
><br>
> + pages_per_xcc = total_pages;<br>
> + do_div(pages_per_xcc, num_xcc);<br>
> +<br>
> for (i = 0, page_idx = 0; i < num_xcc; i++, page_idx += pages_per_xcc) {<br>
> /* MQD page: use default flags */<br>
> amdgpu_gart_bind(adev,<br>
> diff --git a/drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c b/drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c<br>
> index 938c8dba9057..d559e7bc0f09 100644<br>
> --- a/drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c<br>
> +++ b/drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c<br>
> @@ -1919,9 +1919,10 @@ gmc_v9_0_init_acpi_mem_ranges(struct amdgpu_device *adev,<br>
> adev->gmc.num_mem_partitions = num_ranges;<br>
><br>
> /* If there is only partition, don't use entire size */<br>
> - if (adev->gmc.num_mem_partitions == 1)<br>
> - mem_ranges[0].size =<br>
> - (mem_ranges[0].size * (mem_groups - 1) / mem_groups);<br>
> + if (adev->gmc.num_mem_partitions == 1) {<br>
> + mem_ranges[0].size = mem_ranges[0].size * (mem_groups - 1);<br>
> + do_div(mem_ranges[0].size, mem_groups);<br>
> + }<br>
> }<br>
><br>
> static void<br>
> @@ -1953,8 +1954,8 @@ gmc_v9_0_init_sw_mem_ranges(struct amdgpu_device *adev,<br>
> break;<br>
> }<br>
><br>
> - size = (adev->gmc.real_vram_size >> AMDGPU_GPU_PAGE_SHIFT) /<br>
> - adev->gmc.num_mem_partitions;<br>
> + size = adev->gmc.real_vram_size >> AMDGPU_GPU_PAGE_SHIFT;<br>
> + size /= adev->gmc.num_mem_partitions;<br>
><br>
> Did you miss using the do_div() here?<br>
<br>
It's not needed here. size is 32 bits IIRC.<br>
<br>
Alex<br>
<br>
><br>
> Thanks,<br>
> Mukul<br>
><br>
> for (i = 0; i < adev->gmc.num_mem_partitions; ++i) {<br>
> mem_ranges[i].range.fpfn = start_addr;<br>
> --<br>
> 2.40.1<br>
><br>
</div>
</span></font></div>
</div>
</body>
</html>