<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<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> Deucher, Alexander <Alexander.Deucher@amd.com><br>
<b>Sent:</b> Wednesday, May 17, 2023 1:17 PM<br>
<b>To:</b> amd-gfx@lists.freedesktop.org <amd-gfx@lists.freedesktop.org><br>
<b>Cc:</b> Deucher, Alexander <Alexander.Deucher@amd.com>; Chen, Guchun <Guchun.Chen@amd.com>; Joshi, Mukul <Mukul.Joshi@amd.com><br>
<b>Subject:</b> [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">Rework logic or use do_div() to avoid problems on 32 bit.<br>
<br>
v2: add a missing case for XCP macro<br>
v3: fix out of bounds array access<br>
<br>
Acked-by: Guchun Chen <guchun.chen@amd.com> (v1)<br>
Reviewed-by: Mukul Joshi <mukul.joshi@amd.com> (v2)<br>
Signed-off-by: Alex Deucher <alexander.deucher@amd.com><br>
---<br>
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c | 15 +++++++++++++++<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, 29 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..5de92c9ab18f 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,18 @@ 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>
+       s8 mem_id = KFD_XCP_MEM_ID(adev, xcp_id);<br>
+<br>
+       if (adev->gmc.num_mem_partitions && xcp_id >= 0 && mem_id >= 0) {<br>
+               tmp = adev->gmc.mem_partitions[mem_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>
         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>