<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:11pt;color:#0078D7;margin:5pt;" align="Left">
[AMD Official Use Only - Internal Distribution Only]<br>
</p>
<br>
<div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
I think we should probably add the pci domain to the bdf to match the format in the kernel</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
<span style="color: rgb(0, 0, 0); font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, system-ui, Roboto, "Helvetica Neue", sans-serif; font-size: 14.6667px; background-color: rgb(255, 255, 255); display: inline !important;">+      
 seq_printf(m, "pdev:\t%02x:%02x.%d\npasid:\t%u\n", bus, dev, fn,</span><br>
<span style="color: rgb(0, 0, 0); font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, system-ui, Roboto, "Helvetica Neue", sans-serif; font-size: 14.6667px; background-color: rgb(255, 255, 255); display: inline !important;">+                      
 fpriv->vm.pasid);</span><br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
<span style="color: rgb(0, 0, 0); font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, system-ui, Roboto, "Helvetica Neue", sans-serif; font-size: 14.6667px; background-color: rgb(255, 255, 255); display: inline !important;"><br>
</span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);">
<span style="color: rgb(0, 0, 0); font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, system-ui, Roboto, "Helvetica Neue", sans-serif; font-size: 14.6667px; background-color: rgb(255, 255, 255); display: inline !important;">you
 can get it with 
<pre style="font-family: "Ubuntu Mono", monospace; font-size: 0.9em; line-height: 1.2; padding: 1em 0px 4em 1em; color: rgb(0, 0, 0); margin: 0px; min-height: 100%; tab-size: 8; hyphens: none;"><span class="n"><a href="https://elixir.bootlin.com/linux/latest/C/ident/pci_domain_nr" style="background: linear-gradient(rgba(0, 0, 0, 0) 10%, rgb(244, 246, 255) 10%, rgb(244, 246, 255) 90%, rgba(0, 0, 0, 0) 90%); font-weight: 700; border-radius: 0.2em; border-bottom: 1px dotted rgb(0, 0, 0);">pci_domain_nr</a></span><span class="p" style="color: rgb(102, 102, 102);">(</span><span class="n">pdev</span><span class="o" style="color: rgb(102, 102, 102);">-></span><span class="n"><a href="https://elixir.bootlin.com/linux/latest/C/ident/bus" style="background: linear-gradient(rgba(0, 0, 0, 0) 10%, rgb(244, 246, 255) 10%, rgb(244, 246, 255) 90%, rgba(0, 0, 0, 0) 90%); font-weight: 700; border-radius: 0.2em;">bus</a></span><span class="p" style="color: rgb(102, 102, 102);">)</span></pre>
<pre style="font-family: "Ubuntu Mono", monospace; font-size: 0.9em; line-height: 1.2; padding: 1em 0px 4em 1em; color: rgb(0, 0, 0); margin: 0px; min-height: 100%; tab-size: 8; hyphens: none;"><span class="p" style="color: rgb(102, 102, 102);">David</span></pre>
</span></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> Roy Sun <Roy.Sun@amd.com><br>
<b>Sent:</b> Tuesday, April 20, 2021 8:46 PM<br>
<b>To:</b> amd-gfx@lists.freedesktop.org <amd-gfx@lists.freedesktop.org><br>
<b>Cc:</b> Sun, Roy <Roy.Sun@amd.com>; Nieto, David M <David.Nieto@amd.com><br>
<b>Subject:</b> [PATCH 2/2] drm/amdgpu: Add show_fdinfo() interface</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">Tracking devices, process info and fence info using<br>
/proc/pid/fdinfo<br>
<br>
Signed-off-by: David M Nieto <David.Nieto@amd.com><br>
Signed-off-by: Roy Sun <Roy.Sun@amd.com><br>
---<br>
 drivers/gpu/drm/amd/amdgpu/Makefile        |  2 +<br>
 drivers/gpu/drm/amd/amdgpu/amdgpu.h        |  1 +<br>
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c    | 61 ++++++++++++++<br>
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h    |  5 +-<br>
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c    |  5 +-<br>
 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c | 92 ++++++++++++++++++++++<br>
 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.h | 43 ++++++++++<br>
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c     |  1 +<br>
 8 files changed, 208 insertions(+), 2 deletions(-)<br>
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c<br>
 create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.h<br>
<br>
diff --git a/drivers/gpu/drm/amd/amdgpu/Makefile b/drivers/gpu/drm/amd/amdgpu/Makefile<br>
index ee85e8aba636..d216b7ecb5d1 100644<br>
--- a/drivers/gpu/drm/amd/amdgpu/Makefile<br>
+++ b/drivers/gpu/drm/amd/amdgpu/Makefile<br>
@@ -58,6 +58,8 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \<br>
         amdgpu_umc.o smu_v11_0_i2c.o amdgpu_fru_eeprom.o amdgpu_rap.o \<br>
         amdgpu_fw_attestation.o amdgpu_securedisplay.o<br>
 <br>
+amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o<br>
+<br>
 amdgpu-$(CONFIG_PERF_EVENTS) += amdgpu_pmu.o<br>
 <br>
 # add asic specific block<br>
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu.h b/drivers/gpu/drm/amd/amdgpu/amdgpu.h<br>
index 125b25a5ce5b..3365feae15e1 100644<br>
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu.h<br>
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu.h<br>
@@ -107,6 +107,7 @@<br>
 #include "amdgpu_gfxhub.h"<br>
 #include "amdgpu_df.h"<br>
 #include "amdgpu_smuio.h"<br>
+#include "amdgpu_fdinfo.h"<br>
 <br>
 #define MAX_GPU_INSTANCE                16<br>
 <br>
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c<br>
index 0350205c4897..01fe60fedcbe 100644<br>
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c<br>
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c<br>
@@ -651,3 +651,64 @@ void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)<br>
         idr_destroy(&mgr->ctx_handles);<br>
         mutex_destroy(&mgr->lock);<br>
 }<br>
+<br>
+void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity *centity,<br>
+               ktime_t *total, ktime_t *max)<br>
+{<br>
+       ktime_t now, t1;<br>
+       uint32_t i;<br>
+<br>
+       now = ktime_get();<br>
+       for (i = 0; i < amdgpu_sched_jobs; i++) {<br>
+               struct dma_fence *fence;<br>
+               struct drm_sched_fence *s_fence;<br>
+<br>
+               spin_lock(&ctx->ring_lock);<br>
+               fence = dma_fence_get(centity->fences[i]);<br>
+               spin_unlock(&ctx->ring_lock);<br>
+               if (!fence)<br>
+                       continue;<br>
+               s_fence = to_drm_sched_fence(fence);<br>
+               if (!dma_fence_is_signaled(&s_fence->scheduled))<br>
+                       continue;<br>
+               t1 = s_fence->scheduled.timestamp;<br>
+               if (t1 >= now)<br>
+                       continue;<br>
+               if (dma_fence_is_signaled(&s_fence->finished) &&<br>
+                       s_fence->finished.timestamp < now)<br>
+                       *total += ktime_sub(s_fence->finished.timestamp, t1);<br>
+               else<br>
+                       *total += ktime_sub(now, t1);<br>
+               t1 = ktime_sub(now, t1);<br>
+               dma_fence_put(fence);<br>
+               *max = max(t1, *max);<br>
+       }<br>
+}<br>
+<br>
+ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip,<br>
+               uint32_t idx, uint64_t *elapsed)<br>
+{<br>
+       struct idr *idp;<br>
+       struct amdgpu_ctx *ctx;<br>
+       uint32_t id;<br>
+       struct amdgpu_ctx_entity *centity;<br>
+       ktime_t total = 0, max = 0;<br>
+<br>
+       if (idx >= AMDGPU_MAX_ENTITY_NUM)<br>
+               return 0;<br>
+       idp = &mgr->ctx_handles;<br>
+       mutex_lock(&mgr->lock);<br>
+       idr_for_each_entry(idp, ctx, id) {<br>
+               if (!ctx->entities[hwip][idx])<br>
+                       continue;<br>
+<br>
+               centity = ctx->entities[hwip][idx];<br>
+               amdgpu_ctx_fence_time(ctx, centity, &total, &max);<br>
+       }<br>
+<br>
+       mutex_unlock(&mgr->lock);<br>
+       if (elapsed)<br>
+               *elapsed = max;<br>
+<br>
+       return total;<br>
+}<br>
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h<br>
index f54e10314661..10dcf59a5c6b 100644<br>
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h<br>
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.h<br>
@@ -87,5 +87,8 @@ void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr);<br>
 void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr);<br>
 long amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr, long timeout);<br>
 void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr);<br>
-<br>
+ktime_t amdgpu_ctx_mgr_fence_usage(struct amdgpu_ctx_mgr *mgr, uint32_t hwip,<br>
+               uint32_t idx, uint64_t *elapsed);<br>
+void amdgpu_ctx_fence_time(struct amdgpu_ctx *ctx, struct amdgpu_ctx_entity *centity,<br>
+               ktime_t *total, ktime_t *max);<br>
 #endif<br>
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c<br>
index 0369d3532bf0..01603378dbc9 100644<br>
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c<br>
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c<br>
@@ -42,7 +42,7 @@<br>
 #include "amdgpu_irq.h"<br>
 #include "amdgpu_dma_buf.h"<br>
 #include "amdgpu_sched.h"<br>
-<br>
+#include "amdgpu_fdinfo.h"<br>
 #include "amdgpu_amdkfd.h"<br>
 <br>
 #include "amdgpu_ras.h"<br>
@@ -1692,6 +1692,9 @@ static const struct file_operations amdgpu_driver_kms_fops = {<br>
 #ifdef CONFIG_COMPAT<br>
         .compat_ioctl = amdgpu_kms_compat_ioctl,<br>
 #endif<br>
+#ifdef CONFIG_PROC_FS<br>
+       .show_fdinfo = amdgpu_show_fdinfo<br>
+#endif<br>
 };<br>
 <br>
 int amdgpu_file_to_fpriv(struct file *filp, struct amdgpu_fpriv **fpriv)<br>
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c<br>
new file mode 100644<br>
index 000000000000..56ad1e819822<br>
--- /dev/null<br>
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.c<br>
@@ -0,0 +1,92 @@<br>
+// SPDX-License-Identifier: MIT<br>
+/* Copyright 2021 Advanced Micro Devices, Inc.<br>
+ *<br>
+ * Permission is hereby granted, free of charge, to any person obtaining a<br>
+ * copy of this software and associated documentation files (the "Software"),<br>
+ * to deal in the Software without restriction, including without limitation<br>
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
+ * and/or sell copies of the Software, and to permit persons to whom the<br>
+ * Software is furnished to do so, subject to the following conditions:<br>
+ *<br>
+ * The above copyright notice and this permission notice shall be included in<br>
+ * all copies or substantial portions of the Software.<br>
+ *<br>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL<br>
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR<br>
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,<br>
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR<br>
+ * OTHER DEALINGS IN THE SOFTWARE.<br>
+ *<br>
+ * Authors: David Nieto<br>
+ *          Roy Sun<br>
+ */<br>
+<br>
+#include <linux/debugfs.h><br>
+#include <linux/list.h><br>
+#include <linux/module.h><br>
+#include <linux/uaccess.h><br>
+#include <linux/reboot.h><br>
+#include <linux/syscalls.h><br>
+<br>
+#include <drm/amdgpu_drm.h><br>
+#include <drm/drm_debugfs.h><br>
+<br>
+#include "amdgpu.h"<br>
+#include "amdgpu_vm.h"<br>
+#include "amdgpu_gem.h"<br>
+#include "amdgpu_ctx.h"<br>
+#include "amdgpu_fdinfo.h"<br>
+<br>
+<br>
+static const char *amdgpu_ip_name[AMDGPU_HW_IP_NUM] = {<br>
+       [AMDGPU_HW_IP_GFX]      =       "gfx",<br>
+       [AMDGPU_HW_IP_COMPUTE]  =       "compute",<br>
+       [AMDGPU_HW_IP_DMA]      =       "dma",<br>
+       [AMDGPU_HW_IP_UVD]      =       "dec",<br>
+       [AMDGPU_HW_IP_VCE]      =       "enc",<br>
+       [AMDGPU_HW_IP_UVD_ENC]  =       "enc_1",<br>
+       [AMDGPU_HW_IP_VCN_DEC]  =       "dec",<br>
+       [AMDGPU_HW_IP_VCN_ENC]  =       "enc",<br>
+       [AMDGPU_HW_IP_VCN_JPEG] =       "jpeg",<br>
+};<br>
+<br>
+void amdgpu_show_fdinfo(struct seq_file *m, struct file *f)<br>
+{<br>
+       struct amdgpu_fpriv *fpriv;<br>
+       uint32_t bus, dev, fn, i;<br>
+       uint64_t vram_mem = 0, gtt_mem = 0;<br>
+       struct drm_file *file = f->private_data;<br>
+       struct amdgpu_device *adev = drm_to_adev(file->minor->dev);<br>
+<br>
+       if (amdgpu_file_to_fpriv(f, &fpriv))<br>
+               return;<br>
+       bus = adev->pdev->bus->number;<br>
+       dev = PCI_SLOT(adev->pdev->devfn);<br>
+       fn = PCI_FUNC(adev->pdev->devfn);<br>
+<br>
+       amdgpu_debugfs_vm_bo_info(&fpriv->vm, m);<br>
+       seq_printf(m, "pdev:\t%02x:%02x.%d\npasid:\t%u\n", bus, dev, fn,<br>
+                       fpriv->vm.pasid);<br>
+       for (i = 0; i < AMDGPU_HW_IP_NUM; i++) {<br>
+               uint32_t count = amdgpu_ctx_num_entities[i];<br>
+               int idx = 0;<br>
+               uint64_t total = 0, min = 0;<br>
+               uint32_t perc, frac;<br>
+<br>
+               for (idx = 0; idx < count; idx++) {<br>
+                       total = amdgpu_ctx_mgr_fence_usage(&fpriv->ctx_mgr,<br>
+                               i, idx, &min);<br>
+                       if ((total == 0) || (min == 0))<br>
+                               continue;<br>
+<br>
+                       perc = div64_u64(10000 * total, min);<br>
+                       frac = perc % 100;<br>
+<br>
+                       seq_printf(m, "%s%d:\t%d.%d%%\n",<br>
+                                       amdgpu_ip_name[i],<br>
+                                       idx, perc/100, frac);<br>
+               }<br>
+       }<br>
+}<br>
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.h<br>
new file mode 100644<br>
index 000000000000..41a4c7056729<br>
--- /dev/null<br>
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_fdinfo.h<br>
@@ -0,0 +1,43 @@<br>
+/* SPDX-License-Identifier: MIT<br>
+ * Copyright 2021 Advanced Micro Devices, Inc.<br>
+ *<br>
+ * Permission is hereby granted, free of charge, to any person obtaining a<br>
+ * copy of this software and associated documentation files (the "Software"),<br>
+ * to deal in the Software without restriction, including without limitation<br>
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
+ * and/or sell copies of the Software, and to permit persons to whom the<br>
+ * Software is furnished to do so, subject to the following conditions:<br>
+ *<br>
+ * The above copyright notice and this permission notice shall be included in<br>
+ * all copies or substantial portions of the Software.<br>
+ *<br>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL<br>
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR<br>
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,<br>
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR<br>
+ * OTHER DEALINGS IN THE SOFTWARE.<br>
+ *<br>
+ * Authors: David Nieto<br>
+ *          Roy Sun<br>
+ */<br>
+#ifndef __AMDGPU_SMI_H__<br>
+#define __AMDGPU_SMI_H__<br>
+<br>
+#include <linux/idr.h><br>
+#include <linux/kfifo.h><br>
+#include <linux/rbtree.h><br>
+#include <drm/gpu_scheduler.h><br>
+#include <drm/drm_file.h><br>
+#include <drm/ttm/ttm_bo_driver.h><br>
+#include <linux/sched/mm.h><br>
+<br>
+#include "amdgpu_sync.h"<br>
+#include "amdgpu_ring.h"<br>
+#include "amdgpu_ids.h"<br>
+<br>
+uint32_t amdgpu_get_ip_count(struct amdgpu_device *adev, int id);<br>
+void amdgpu_show_fdinfo(struct seq_file *m, struct file *f);<br>
+<br>
+#endif<br>
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c<br>
index f95bcda8463f..3c7390727edf 100644<br>
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c<br>
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c<br>
@@ -25,6 +25,7 @@<br>
  *          Alex Deucher<br>
  *          Jerome Glisse<br>
  */<br>
+<br>
 #include <linux/dma-fence-array.h><br>
 #include <linux/interval_tree_generic.h><br>
 #include <linux/idr.h><br>
-- <br>
2.31.1<br>
<br>
</div>
</span></font></div>
</div>
</body>
</html>