<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]--><style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
span.EmailStyle18
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:windowtext;}
p.msipheaderc10f11a2, li.msipheaderc10f11a2, div.msipheaderc10f11a2
{mso-style-name:msipheaderc10f11a2;
mso-margin-top-alt:auto;
margin-right:0in;
mso-margin-bottom-alt:auto;
margin-left:0in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="#0563C1" vlink="#954F72" style="word-wrap:break-word">
<div class="WordSection1">
<p class="msipheaderc10f11a2" style="margin:0in"><span style="font-size:10.0pt;font-family:"Arial",sans-serif;color:green">[Public]</span><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b>From:</b> amd-gfx <amd-gfx-bounces@lists.freedesktop.org>
<b>On Behalf Of </b>Deucher, Alexander<br>
<b>Sent:</b> Monday, January 10, 2022 4:11 PM<br>
<b>To:</b> Phillips, Daniel <Daniel.Phillips@amd.com>; amd-gfx@lists.freedesktop.org; dri-devel@lists.freedesktop.org<br>
<b>Subject:</b> Re: [PATCH 1/1] Add available memory ioctl for libhsakmt<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p style="margin:15.0pt"><span style="font-size:10.0pt;font-family:"Arial",sans-serif;color:green">[Public]<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p style="margin:15.0pt"><span style="font-size:10.0pt;font-family:"Arial",sans-serif;color:green">[Public]<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt;color:black">This is missing your signed-off-by. Additionally, for UAPI changes, we need a link the patches for the userspace component that will make use of it.<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt;color:black"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt;color:black">Alex<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt;color:black"><o:p> </o:p></span></p>
</div>
<div class="MsoNormal" align="center" style="text-align:center">
<hr size="1" width="98%" align="center">
</div>
<div id="divRplyFwdMsg">
<p class="MsoNormal"><b><span style="color:black">From:</span></b><span style="color:black"> amd-gfx <<a href="mailto:amd-gfx-bounces@lists.freedesktop.org">amd-gfx-bounces@lists.freedesktop.org</a>> on behalf of Daniel Phillips <<a href="mailto:daniel.phillips@amd.com">daniel.phillips@amd.com</a>><br>
<b>Sent:</b> Monday, January 10, 2022 3:54 PM<br>
<b>To:</b> <a href="mailto:amd-gfx@lists.freedesktop.org">amd-gfx@lists.freedesktop.org</a> <<a href="mailto:amd-gfx@lists.freedesktop.org">amd-gfx@lists.freedesktop.org</a>>;
<a href="mailto:dri-devel@lists.freedesktop.org">dri-devel@lists.freedesktop.org</a> <<a href="mailto:dri-devel@lists.freedesktop.org">dri-devel@lists.freedesktop.org</a>><br>
<b>Cc:</b> Phillips, Daniel <<a href="mailto:Daniel.Phillips@amd.com">Daniel.Phillips@amd.com</a>><br>
<b>Subject:</b> [PATCH 1/1] Add available memory ioctl for libhsakmt</span> <o:p>
</o:p></p>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">From: Daniel Phillips <<a href="mailto:dphillip@amd.com">dphillip@amd.com</a>><br>
<br>
Add an ioctl to inquire memory available for allocation by libhsakmt<br>
per node, allowing for space consumed by page translation tables.<br>
---<br>
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h | 1 +<br>
.../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 14 ++++++++++++++<br>
drivers/gpu/drm/amd/amdkfd/kfd_chardev.c | 17 +++++++++++++++++<br>
include/uapi/linux/kfd_ioctl.h | 14 ++++++++++++--<br>
4 files changed, 44 insertions(+), 2 deletions(-)<br>
<br>
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h<br>
index fcbc8a9c9e06..64c6c36685d3 100644<br>
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h<br>
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h<br>
@@ -266,6 +266,7 @@ int amdgpu_amdkfd_gpuvm_acquire_process_vm(struct amdgpu_device *adev,<br>
void amdgpu_amdkfd_gpuvm_release_process_vm(struct amdgpu_device *adev,<br>
void *drm_priv);<br>
uint64_t amdgpu_amdkfd_gpuvm_get_process_page_dir(void *drm_priv);<br>
+size_t amdgpu_amdkfd_get_available_memory(struct amdgpu_device *adev);<br>
int amdgpu_amdkfd_gpuvm_alloc_memory_of_gpu(<br>
struct amdgpu_device *adev, uint64_t va, uint64_t size,<br>
void *drm_priv, struct kgd_mem **mem,<br>
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c<br>
index 86a1a6c109d9..b7490a659173 100644<br>
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c<br>
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c<br>
@@ -190,6 +190,20 @@ static int amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device *adev,<br>
return ret;<br>
}<br>
<br>
+size_t amdgpu_amdkfd_get_available_memory(struct amdgpu_device *adev)<br>
+{<br>
+ uint64_t reserved_for_pt =<br>
+ ESTIMATE_PT_SIZE(amdgpu_amdkfd_total_mem_size);<br>
+ size_t available_memory;<br>
+<br>
+ spin_lock(&kfd_mem_limit.mem_limit_lock);<br>
+ available_memory =<br>
+ adev->gmc.real_vram_size -<br>
+ adev->kfd.vram_used - reserved_for_pt;<br>
+ spin_unlock(&kfd_mem_limit.mem_limit_lock);<br>
+ return available_memory;<br>
+}<br>
+<br>
static void unreserve_mem_limit(struct amdgpu_device *adev,<br>
uint64_t size, u32 alloc_flag)<br>
{<br>
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c<br>
index 4bfc0c8ab764..5c2f6d97ff1c 100644<br>
--- a/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c<br>
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_chardev.c<br>
@@ -486,6 +486,20 @@ static int kfd_ioctl_get_queue_wave_state(struct file *filep,<br>
return r;<br>
}<br>
<br>
+static int kfd_ioctl_get_available_memory(struct file *filep,<br>
+ struct kfd_process *p, void *data)<br>
+{<br>
+ struct kfd_ioctl_get_available_memory_args *args = data;<br>
+ struct kfd_dev *dev;<br>
+<br>
+ dev = kfd_device_by_id(args->gpu_id);<o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom:12.0pt">Once CRIU changes land, this need to change to kfd_process_device_data_by_id() and then use pdd->dev<o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom:12.0pt"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-bottom:12.0pt"><br>
+ if (!dev)<br>
+ return -EINVAL;<br>
+<br>
+ args->available = amdgpu_amdkfd_get_available_memory(dev->adev);<br>
+ return 0;<br>
+}<br>
+<br>
static int kfd_ioctl_set_memory_policy(struct file *filep,<br>
struct kfd_process *p, void *data)<br>
{<br>
@@ -1959,6 +1973,9 @@ static const struct amdkfd_ioctl_desc amdkfd_ioctls[] = {<br>
<br>
AMDKFD_IOCTL_DEF(AMDKFD_IOC_SET_XNACK_MODE,<br>
kfd_ioctl_set_xnack_mode, 0),<br>
+<br>
+ AMDKFD_IOCTL_DEF(AMDKFD_IOC_AVAILABLE_MEMORY,<br>
+ kfd_ioctl_get_available_memory, 0),<br>
};<br>
<br>
#define AMDKFD_CORE_IOCTL_COUNT ARRAY_SIZE(amdkfd_ioctls)<br>
diff --git a/include/uapi/linux/kfd_ioctl.h b/include/uapi/linux/kfd_ioctl.h<br>
index af96af174dc4..94a99add2432 100644<br>
--- a/include/uapi/linux/kfd_ioctl.h<br>
+++ b/include/uapi/linux/kfd_ioctl.h<br>
@@ -32,9 +32,10 @@<br>
* - 1.4 - Indicate new SRAM EDC bit in device properties<br>
* - 1.5 - Add SVM API<br>
* - 1.6 - Query clear flags in SVM get_attr API<br>
+ * - 1.7 - Add available_memory ioctl<br>
*/<br>
#define KFD_IOCTL_MAJOR_VERSION 1<br>
-#define KFD_IOCTL_MINOR_VERSION 6<br>
+#define KFD_IOCTL_MINOR_VERSION 7<br>
<br>
struct kfd_ioctl_get_version_args {<br>
__u32 major_version; /* from KFD */<br>
@@ -98,6 +99,12 @@ struct kfd_ioctl_get_queue_wave_state_args {<br>
__u32 pad;<br>
};<br>
<br>
+struct kfd_ioctl_get_available_memory_args {<br>
+ __u64 available; /* from KFD */<br>
+ __u32 gpu_id; /* to KFD */<br>
+ __u32 pad;<br>
+};<br>
+<br>
/* For kfd_ioctl_set_memory_policy_args.default_policy and alternate_policy */<br>
#define KFD_IOC_CACHE_POLICY_COHERENT 0<br>
#define KFD_IOC_CACHE_POLICY_NONCOHERENT 1<br>
@@ -742,7 +749,10 @@ struct kfd_ioctl_set_xnack_mode_args {<br>
#define AMDKFD_IOC_SET_XNACK_MODE \<br>
AMDKFD_IOWR(0x21, struct kfd_ioctl_set_xnack_mode_args)<br>
<br>
+#define AMDKFD_IOC_AVAILABLE_MEMORY \<br>
+ AMDKFD_IOR(0x22, struct kfd_ioctl_get_available_memory_args)<br>
+<br>
#define AMDKFD_COMMAND_START 0x01<br>
-#define AMDKFD_COMMAND_END 0x22<br>
+#define AMDKFD_COMMAND_END 0x23<br>
<br>
#endif<br>
-- <br>
2.34.1<o:p></o:p></p>
</div>
</div>
</div>
</div>
</div>
</body>
</html>