<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:#008000;margin:15pt;" align="Left">
[Public]<br>
</p>
<br>
<div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
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.</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Alex</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<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> amd-gfx <amd-gfx-bounces@lists.freedesktop.org> on behalf of Daniel Phillips <daniel.phillips@amd.com><br>
<b>Sent:</b> Monday, January 10, 2022 3:54 PM<br>
<b>To:</b> amd-gfx@lists.freedesktop.org <amd-gfx@lists.freedesktop.org>; dri-devel@lists.freedesktop.org <dri-devel@lists.freedesktop.org><br>
<b>Cc:</b> Phillips, Daniel <Daniel.Phillips@amd.com><br>
<b>Subject:</b> [PATCH 1/1] Add available memory ioctl for libhsakmt</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">From: Daniel Phillips <dphillip@amd.com><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);<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<br>
<br>
</div>
</span></font></div>
</div>
</body>
</html>