<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:DengXian;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:"\@DengXian";
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:"Segoe UI";
panose-1:2 11 5 2 4 2 4 2 2 3;}
/* 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.elementtoproof
{mso-style-name:elementtoproof;}
span.EmailStyle19
{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;}
/* List Definitions */
@list l0
{mso-list-id:635139098;
mso-list-template-ids:763805384;}
@list l0:level1
{mso-level-tab-stop:.5in;
mso-level-number-position:left;
text-indent:-.25in;}
@list l0:level2
{mso-level-tab-stop:1.0in;
mso-level-number-position:left;
text-indent:-.25in;}
@list l0:level3
{mso-level-tab-stop:1.5in;
mso-level-number-position:left;
text-indent:-.25in;}
@list l0:level4
{mso-level-tab-stop:2.0in;
mso-level-number-position:left;
text-indent:-.25in;}
@list l0:level5
{mso-level-tab-stop:2.5in;
mso-level-number-position:left;
text-indent:-.25in;}
@list l0:level6
{mso-level-tab-stop:3.0in;
mso-level-number-position:left;
text-indent:-.25in;}
@list l0:level7
{mso-level-tab-stop:3.5in;
mso-level-number-position:left;
text-indent:-.25in;}
@list l0:level8
{mso-level-tab-stop:4.0in;
mso-level-number-position:left;
text-indent:-.25in;}
@list l0:level9
{mso-level-tab-stop:4.5in;
mso-level-number-position:left;
text-indent:-.25in;}
@list l1
{mso-list-id:776681118;
mso-list-template-ids:763805384;}
@list l2
{mso-list-id:1150439283;
mso-list-template-ids:763805384;}
@list l2:level1
{mso-level-start-at:2;
mso-level-tab-stop:.5in;
mso-level-number-position:left;
text-indent:-.25in;}
@list l3
{mso-list-id:1299803927;
mso-list-template-ids:763805384;}
@list l3:level1
{mso-level-start-at:2;
mso-level-tab-stop:.5in;
mso-level-number-position:left;
text-indent:-.25in;}
@list l3:level2
{mso-level-tab-stop:1.0in;
mso-level-number-position:left;
text-indent:-.25in;}
@list l3:level3
{mso-level-tab-stop:1.5in;
mso-level-number-position:left;
text-indent:-.25in;}
@list l3:level4
{mso-level-tab-stop:2.0in;
mso-level-number-position:left;
text-indent:-.25in;}
@list l3:level5
{mso-level-tab-stop:2.5in;
mso-level-number-position:left;
text-indent:-.25in;}
@list l3:level6
{mso-level-tab-stop:3.0in;
mso-level-number-position:left;
text-indent:-.25in;}
@list l3:level7
{mso-level-tab-stop:3.5in;
mso-level-number-position:left;
text-indent:-.25in;}
@list l3:level8
{mso-level-tab-stop:4.0in;
mso-level-number-position:left;
text-indent:-.25in;}
@list l3:level9
{mso-level-tab-stop:4.5in;
mso-level-number-position:left;
text-indent:-.25in;}
ol
{margin-bottom:0in;}
ul
{margin-bottom:0in;}
--></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">Thanks for the review, comments in line.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><span style="color:#44546A">Thanks,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="color:#44546A">Candice<o:p></o:p></span></p>
</div>
<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> Wang, Yang(Kevin) <KevinYang.Wang@amd.com> <br>
<b>Sent:</b> Monday, April 18, 2022 11:50 AM<br>
<b>To:</b> Li, Candice <Candice.Li@amd.com>; amd-gfx@lists.freedesktop.org<br>
<b>Cc:</b> Clements, John <John.Clements@amd.com><br>
<b>Subject:</b> Re: [PATCH 2/2] drm/amdgpu: Add debugfs TA load/unload/invoke support<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></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="2" 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 Candice Li <<a href="mailto:candice.li@amd.com">candice.li@amd.com</a>><br>
<b>Sent:</b> Monday, April 18, 2022 11:09 AM<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>><br>
<b>Cc:</b> Li, Candice <<a href="mailto:Candice.Li@amd.com">Candice.Li@amd.com</a>>; Clements, John <<a href="mailto:John.Clements@amd.com">John.Clements@amd.com</a>><br>
<b>Subject:</b> [PATCH 2/2] drm/amdgpu: Add debugfs TA load/unload/invoke support</span>
<o:p></o:p></p>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal">Add debugfs support to load/unload/invoke TA in runtime.<br>
<br>
Signed-off-by: John Clements <<a href="mailto:john.clements@amd.com">john.clements@amd.com</a>><br>
Signed-off-by: Candice Li <<a href="mailto:candice.li@amd.com">candice.li@amd.com</a>><br>
---<br>
drivers/gpu/drm/amd/amdgpu/Makefile | 2 +-<br>
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c | 2 +<br>
drivers/gpu/drm/amd/amdgpu/amdgpu_psp_ta.c | 312 ++++++++++++++++++++<br>
drivers/gpu/drm/amd/amdgpu/amdgpu_psp_ta.h | 30 ++<br>
4 files changed, 345 insertions(+), 1 deletion(-)<br>
create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_psp_ta.c<br>
create mode 100644 drivers/gpu/drm/amd/amdgpu/amdgpu_psp_ta.h<br>
<br>
diff --git a/drivers/gpu/drm/amd/amdgpu/Makefile b/drivers/gpu/drm/amd/amdgpu/Makefile<br>
index 7d7af43a258f83..b525f9be9326f4 100644<br>
--- a/drivers/gpu/drm/amd/amdgpu/Makefile<br>
+++ b/drivers/gpu/drm/amd/amdgpu/Makefile<br>
@@ -58,7 +58,7 @@ amdgpu-y += amdgpu_device.o amdgpu_kms.o \<br>
amdgpu_vm_sdma.o amdgpu_discovery.o amdgpu_ras_eeprom.o amdgpu_nbio.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>
- amdgpu_eeprom.o amdgpu_mca.o<br>
+ amdgpu_eeprom.o amdgpu_mca.o amdgpu_psp_ta.o<br>
<br>
amdgpu-$(CONFIG_PROC_FS) += amdgpu_fdinfo.o<br>
<br>
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c<br>
index 13e4d8f9b87449..eedb12f6b8a32d 100644<br>
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c<br>
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c<br>
@@ -38,6 +38,7 @@<br>
#include "amdgpu_umr.h"<br>
<br>
#include "amdgpu_reset.h"<br>
+#include "amdgpu_psp_ta.h"<br>
<br>
#if defined(CONFIG_DEBUG_FS)<br>
<br>
@@ -1767,6 +1768,7 @@ int amdgpu_debugfs_init(struct amdgpu_device *adev)<br>
DRM_ERROR("registering register debugfs failed (%d).\n", r);<br>
<br>
amdgpu_debugfs_firmware_init(adev);<br>
+ amdgpu_ta_if_debugfs_init(adev);<br>
<br>
#if defined(CONFIG_DRM_AMD_DC)<br>
if (amdgpu_device_has_dc_support(adev))<br>
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_psp_ta.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_psp_ta.c<br>
new file mode 100644<br>
index 00000000000000..916bf3f6fce0d4<br>
--- /dev/null<br>
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_psp_ta.c<br>
@@ -0,0 +1,312 @@<br>
+/*<br>
+ * Copyright 2022 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>
+ */<br>
+<br>
+#include "amdgpu.h"<br>
+#include "amdgpu_psp_ta.h"<br>
+<br>
+static const char *TA_IF_FS_NAME = "ta_if";<br>
+<br>
+struct dentry *dir;<br>
+struct dentry *ta_load_debugfs_dentry;<br>
+struct dentry *ta_unload_debugfs_dentry;<br>
+struct dentry *ta_invoke_debugfs_dentry;<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Segoe UI",sans-serif;color:black">[kevin]:</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Segoe UI",sans-serif;color:black">make above variable as static is better for this case, but it seems it is not used in this patch? </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Segoe UI",sans-serif;color:black">if so, you will get a "defined but not used" error, when turn on build option " -Werror=unused-variable".</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<p class="MsoNormal"><span class="elementtoproof"><span style="font-family:"Segoe UI",sans-serif;color:black">Please don't mind this prompt.</span>
<o:p></o:p></span></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Segoe UI",sans-serif;color:black">Best Regards,</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Segoe UI",sans-serif;color:black">Kevin</span><span style="font-family:"Segoe UI",sans-serif"><o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><span class="elementtoproof"><span style="color:#0070C0">[Candice] Thanks for the review. These variables are used in my patch.
<o:p></o:p></span></span></p>
<p class="MsoNormal"><span style="color:#0070C0">Will update ta_load/unload/invoke_debugfs_dentry to static in v2, but dentry dir will be used by amdgpu_ta_if_debugfs_remove, so it should be not.<span class="elementtoproof"><o:p></o:p></span></span></p>
<p class="MsoNormal"><br>
+<br>
+static ssize_t ta_if_load_debugfs_read(struct file *fp, char *buf, size_t len, loff_t *off);<br>
+static ssize_t ta_if_unload_debugfs_read(struct file *fp, char *buf, size_t len, loff_t *off);<br>
+static ssize_t ta_if_invoke_debugfs_read(struct file *fp, char *buf, size_t len, loff_t *off);<br>
+<br>
+<br>
+static uint32_t get_bin_version(const uint8_t *bin)<br>
+{<br>
+ const struct common_firmware_header *hdr =<br>
+ (const struct common_firmware_header *)bin;<br>
+<br>
+ return hdr->ucode_version;<br>
+}<br>
+<br>
+static uint32_t get_shared_buf_size(uint32_t shared_buf_len)<br>
+{<br>
+ return (shared_buf_len % PAGE_SIZE) ?<br>
+ (shared_buf_len/PAGE_SIZE + 1) * PAGE_SIZE :<br>
+ shared_buf_len;<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">[kevin]:<o:p></o:p></p>
</div>
<div>
<ol start="1" type="1">
<li class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;mso-list:l0 level1 lfo3">
the above code can be replaced with "ALIGN" macro..<o:p></o:p></li><li class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;mso-list:l0 level1 lfo3">
<o:p> </o:p></li><li class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;mso-list:l0 level1 lfo3">
#define ALIGN(x, a)>---->-------__ALIGN_KERNEL((x), (a)<o:p></o:p></li></ol>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="color:#0070C0">[Candice] Thanks for the suggestion, I will replace it with PAGE_ALIGN macro directly in v2.<o:p></o:p></span></p>
<ol start="2" type="1">
<li class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;mso-list:l3 level1 lfo6">
do you forget to add some lock resource to protect/cover multi process case?<o:p></o:p></li><li class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;mso-list:l3 level1 lfo6">
e.g: process#1 exec load_ta() and process#2 exec unload_ta()<o:p></o:p></li></ol>
<div>
<p class="MsoNormal"><span style="color:#0070C0">[Candice] We already have psp->mutex there for psp cmd submission, do we still need another one?<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
<div>
<p class="MsoNormal">and why not use write operation instead of read (load/unload/invoke)
<span style="color:black;background:white">? </span>this seems more reasonable.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><span style="color:#0070C0">[Candice] The debugfs write doesn't allow the kernel to copy data back to the user, so with debugfs read, we can copy data from the user, then copy data back to the user.</span><span style="color:#548235"><o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
<div>
<p class="MsoNormal">Best Regards,<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">Kevin<o:p></o:p></p>
</div>
<div>
<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>
<p class="MsoNormal">+}<br>
+<br>
+static void prep_ta_mem_context(struct psp_context *psp,<br>
+ struct ta_context *context,<br>
+ uint8_t *shared_buf,<br>
+ uint32_t shared_buf_len)<br>
+{<br>
+ context->mem_context.shared_mem_size = get_shared_buf_size(shared_buf_len);<br>
+ psp_ta_init_shared_buf(psp, &context->mem_context);<br>
+<br>
+ memcpy((void *)context->mem_context.shared_buf, shared_buf, shared_buf_len);<br>
+}<br>
+<br>
+static bool is_ta_type_valid(enum ta_type_id ta_type)<br>
+{<br>
+ bool ret = false;<br>
+<br>
+ switch (ta_type) {<br>
+ case TA_TYPE_RAS:<br>
+ ret = true;<br>
+ break;<br>
+ default:<br>
+ break;<br>
+ }<br>
+<br>
+ return ret;<br>
+}<br>
+<br>
+static const struct file_operations ta_load_debugfs_fops = {<br>
+ .read = ta_if_load_debugfs_read,<br>
+ .llseek = default_llseek,<br>
+ .owner = THIS_MODULE<br>
+};<br>
+<br>
+static const struct file_operations ta_unload_debugfs_fops = {<br>
+ .read = ta_if_unload_debugfs_read,<br>
+ .llseek = default_llseek,<br>
+ .owner = THIS_MODULE<br>
+};<br>
+<br>
+static const struct file_operations ta_invoke_debugfs_fops = {<br>
+ .read = ta_if_invoke_debugfs_read,<br>
+ .llseek = default_llseek,<br>
+ .owner = THIS_MODULE<br>
+};<br>
+<br>
+<br>
+/**<br>
+ * DOC: AMDGPU TA debugfs interfaces<br>
+ *<br>
+ * Three debugfs interfaces can be opened by a program to<br>
+ * load/invoke/unload TA,<br>
+ *<br>
+ * - /sys/kernel/debug/dri/<N>/ta_if/ta_load<br>
+ * - /sys/kernel/debug/dri/<N>/ta_if/ta_invoke<br>
+ * - /sys/kernel/debug/dri/<N>/ta_if/ta_unload<br>
+ *<br>
+ * How to use the interfaces in a program?<br>
+ *<br>
+ * A program needs to provide transmit buffer to the interfaces<br>
+ * and will receive buffer from the interfaces below,<br>
+ *<br>
+ * - For TA load debugfs interface:<br>
+ * Transmit buffer:<br>
+ * - TA type (4bytes)<br>
+ * - TA bin length (4bytes)<br>
+ * - TA bin<br>
+ * Receive buffer:<br>
+ * - TA ID (4bytes)<br>
+ *<br>
+ * - For TA invoke debugfs interface:<br>
+ * Transmit buffer:<br>
+ * - TA ID (4bytes)<br>
+ * - TA CMD ID (4bytes)<br>
+ * - TA shard buf length (4bytes)<br>
+ * - TA shared buf<br>
+ * Receive buffer:<br>
+ * - TA shared buf<br>
+ *<br>
+ * - For TA unload debugfs interface:<br>
+ * Transmit buffer:<br>
+ * - TA ID (4bytes)<br>
+ */<br>
+<br>
+static ssize_t ta_if_load_debugfs_read(struct file *fp, char *buf, size_t len, loff_t *off)<br>
+{<br>
+ uint32_t ta_type = 0;<br>
+ uint32_t ta_bin_len = 0;<br>
+ uint8_t *ta_bin = NULL;<br>
+ uint32_t copy_pos = 0;<br>
+ int ret = 0;<br>
+<br>
+ struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(fp)->i_private;<br>
+ struct psp_context *psp = &adev->psp;<br>
+ struct ta_context context = {0};<br>
+<br>
+ if ((fp == NULL) || (buf == NULL))<br>
+ return -EINVAL;<br>
+<br>
+ ret = copy_from_user((void *)&ta_type, &buf[copy_pos], sizeof(uint32_t));<br>
+ if (ret || (!is_ta_type_valid(ta_type)))<br>
+ return -EINVAL;<br>
+<br>
+ copy_pos += sizeof(uint32_t);<br>
+<br>
+ ret = copy_from_user((void *)&ta_bin_len, &buf[copy_pos], sizeof(uint32_t));<br>
+ if (ret)<br>
+ return -EINVAL;<br>
+<br>
+ copy_pos += sizeof(uint32_t);<br>
+<br>
+ ta_bin = kzalloc(ta_bin_len, GFP_KERNEL);<br>
+ if (!ta_bin)<br>
+ ret = -ENOMEM;<br>
+ ret = copy_from_user((void *)ta_bin, &buf[copy_pos], ta_bin_len);<br>
+ if (ret)<br>
+ goto err_free_bin;<br>
+<br>
+ ret = psp_ras_terminate(psp);<br>
+ if (ret) {<br>
+ dev_err(adev->dev, "Failed to unload embedded RAS TA\n");<br>
+ goto err_free_bin;<br>
+ }<br>
+<br>
+ context.ta_type = ta_type;<br>
+ context.ta_load_type = GFX_CMD_ID_LOAD_TA;<br>
+ context.bin_desc.fw_version = get_bin_version(ta_bin);<br>
+ context.bin_desc.size_bytes = ta_bin_len;<br>
+ context.bin_desc.start_addr = ta_bin;<br>
+<br>
+ ret = psp_ta_load(psp, &context);<br>
+<br>
+ if (ret || context.resp_status) {<br>
+ dev_err(adev->dev, "TA load via debugfs failed (%d) status %d\n",<br>
+ ret, context.resp_status);<br>
+ goto err_free_bin;<br>
+ }<br>
+<br>
+ context.initialized = true;<br>
+ ret = copy_to_user((char *)buf, (void *)&context.session_id, sizeof(uint32_t));<br>
+<br>
+err_free_bin:<br>
+ kfree(ta_bin);<br>
+<br>
+ return ret;<br>
+}<br>
+<br>
+static ssize_t ta_if_unload_debugfs_read(struct file *fp, char *buf, size_t len, loff_t *off)<br>
+{<br>
+ uint32_t ta_id = 0;<br>
+ int ret = 0;<br>
+<br>
+ struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(fp)->i_private;<br>
+ struct psp_context *psp = &adev->psp;<br>
+ struct ta_context context = {0};<br>
+<br>
+ if ((fp == NULL) || (buf == NULL))<br>
+ return -1;<br>
+<br>
+ ret = copy_from_user((void *)&ta_id, buf, sizeof(uint32_t));<br>
+ if (ret)<br>
+ return -EINVAL;<br>
+<br>
+ context.session_id = ta_id;<br>
+<br>
+ ret = psp_ta_unload(psp, &context);<br>
+ if (!ret)<br>
+ context.initialized = false;<br>
+<br>
+ return ret;<br>
+}<br>
+<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">+static ssize_t ta_if_invoke_debugfs_read(struct file *fp, char *buf, size_t len, loff_t *<span style="color:black;background:white">off</span>)<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">+{<br>
+ uint32_t ta_id = 0;<br>
+ uint32_t cmd_id = 0;<br>
+ uint32_t shared_buf_len = 0;<br>
+ uint8_t *shared_buf = NULL;<br>
+ uint32_t copy_pos = 0;<br>
+ int ret = 0;<br>
+<br>
+ struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(fp)->i_private;<br>
+ struct psp_context *psp = &adev->psp;<br>
+ struct ta_context context = {0};<br>
+<br>
+ if ((fp == NULL) || (buf == NULL))<br>
+ return -EINVAL;<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Segoe UI",sans-serif;color:black">[kevin]:</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt"><span style="font-family:"Segoe UI",sans-serif;color:black;background:white"> “if ((fp == NULL) ..."</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Segoe UI",sans-serif;color:black"> The linux kernel will automatically create struct file instance when you open the file, even if you do not provide a callback function (in fops)</span><o:p></o:p></p>
</div>
<div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
<div>
<p class="MsoNormal">[kevin]:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"> you should add some check here to avoid that the user buf is overflow.<o:p></o:p></p>
</div>
<p class="MsoNormal"><span class="elementtoproof"> e.g: If the length of the user buf is less than that required, an error should be returned directly
<o:p></o:p></span></p>
<p class="MsoNormal"><span class="elementtoproof"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="color:#0070C0">[Candice] Will remove the fp check in v2. Current check should be enough, copy_from_user will not access the buf if there is no more length.<o:p></o:p></span></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">+<br>
+ ret = copy_from_user((void *)&ta_id, &buf[copy_pos], sizeof(uint32_t));<br>
+ if (ret)<br>
+ return -EINVAL;<br>
+ copy_pos += sizeof(uint32_t);<br>
+<br>
+ ret = copy_from_user((void *)&cmd_id, &buf[copy_pos], sizeof(uint32_t));<br>
+ if (ret)<br>
+ return -EINVAL;<br>
+ copy_pos += sizeof(uint32_t);<br>
+<br>
+ ret = copy_from_user((void *)&shared_buf_len, &buf[copy_pos], sizeof(uint32_t));<br>
+ if (ret)<br>
+ return -EINVAL;<br>
+ copy_pos += sizeof(uint32_t);<br>
+<br>
+ shared_buf = kzalloc(shared_buf_len, GFP_KERNEL);<br>
+ if (!shared_buf)<br>
+ ret = -ENOMEM;<br>
+ ret = copy_from_user((void *)shared_buf, &buf[copy_pos], shared_buf_len);<br>
+ if (ret)<br>
+ goto err_free_shared_buf;<br>
+<br>
+ context.session_id = ta_id;<br>
+<br>
+ prep_ta_mem_context(psp, &context, shared_buf, shared_buf_len);<br>
+<br>
+ ret = psp_ta_invoke_indirect(psp, cmd_id, &context);<br>
+<br>
+ if (ret || context.resp_status) {<br>
+ dev_err(adev->dev, "TA invoke via debugfs failed (%d) status %d\n",<br>
+ ret, context.resp_status);<br>
+ goto err_free_ta_shared_buf;<br>
+ }<br>
+<br>
+ ret = copy_to_user((char *)buf, context.mem_context.shared_buf, shared_buf_len);<br>
+<br>
+err_free_ta_shared_buf:<br>
+ psp_ta_free_shared_buf(&context.mem_context);<br>
+<br>
+err_free_shared_buf:<br>
+ kfree(shared_buf);<br>
+<br>
+ return ret;<br>
+}<br>
+<br>
+static struct dentry *amdgpu_ta_if_debugfs_create(struct amdgpu_device *adev)<br>
+{<br>
+ struct drm_minor *minor = adev_to_drm(adev)->primary;<br>
+<br>
+ dir = debugfs_create_dir(TA_IF_FS_NAME, minor->debugfs_root);<br>
+<br>
+ ta_load_debugfs_dentry = debugfs_create_file("ta_load", 0400, dir, adev,<br>
+ &ta_load_debugfs_fops);<br>
+<br>
+ ta_unload_debugfs_dentry = debugfs_create_file("ta_unload", 0400, dir,<br>
+ adev, &ta_unload_debugfs_fops);<br>
+<br>
+ ta_invoke_debugfs_dentry = debugfs_create_file("ta_invoke", 0400, dir,<br>
+ adev, &ta_invoke_debugfs_fops);<br>
+ return dir;<br>
+}<br>
+<br>
+void amdgpu_ta_if_debugfs_init(struct amdgpu_device *adev)<br>
+{<br>
+#if defined(CONFIG_DEBUG_FS)<br>
+ dir = amdgpu_ta_if_debugfs_create(adev);<br>
+#endif<br>
+}<br>
+<br>
+void amdgpu_ta_if_debugfs_remove(void)<br>
+{<br>
+ debugfs_remove_recursive(dir);<br>
+}<br>
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_psp_ta.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_psp_ta.h<br>
new file mode 100644<br>
index 00000000000000..883f89d57616d0<br>
--- /dev/null<br>
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_psp_ta.h<br>
@@ -0,0 +1,30 @@<br>
+/*<br>
+ * Copyright 2022 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>
+ */<br>
+<br>
+#ifndef __AMDGPU_PSP_TA_H__<br>
+#define __AMDGPU_PSP_TA_H__<br>
+<br>
+void amdgpu_ta_if_debugfs_init(struct amdgpu_device *adev);<br>
+void amdgpu_ta_if_debugfs_remove(void);<br>
+<br>
+#endif<br>
-- <br>
2.17.1<o:p></o:p></p>
</div>
</div>
</div>
</div>
</body>
</html>