2 * Copyright 2022 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
25 #include "amdgpu_psp_ta.h"
27 #if defined(CONFIG_DEBUG_FS)
29 static ssize_t ta_if_load_debugfs_write(struct file *fp, const char *buf,
30 size_t len, loff_t *off);
31 static ssize_t ta_if_unload_debugfs_write(struct file *fp, const char *buf,
32 size_t len, loff_t *off);
33 static ssize_t ta_if_invoke_debugfs_write(struct file *fp, const char *buf,
34 size_t len, loff_t *off);
36 static uint32_t get_bin_version(const uint8_t *bin)
38 const struct common_firmware_header *hdr =
39 (const struct common_firmware_header *)bin;
41 return hdr->ucode_version;
44 static int prep_ta_mem_context(struct ta_mem_context *mem_context,
46 uint32_t shared_buf_len)
48 if (mem_context->shared_mem_size < shared_buf_len)
50 memset(mem_context->shared_buf, 0, mem_context->shared_mem_size);
51 memcpy((void *)mem_context->shared_buf, shared_buf, shared_buf_len);
56 static bool is_ta_type_valid(enum ta_type_id ta_type)
66 static const struct ta_funcs ras_ta_funcs = {
67 .fn_ta_initialize = psp_ras_initialize,
68 .fn_ta_invoke = psp_ras_invoke,
69 .fn_ta_terminate = psp_ras_terminate
72 static void set_ta_context_funcs(struct psp_context *psp,
73 enum ta_type_id ta_type,
74 struct ta_context **pcontext)
78 *pcontext = &psp->ras_context.context;
79 psp->ta_funcs = &ras_ta_funcs;
86 static const struct file_operations ta_load_debugfs_fops = {
87 .write = ta_if_load_debugfs_write,
88 .llseek = default_llseek,
92 static const struct file_operations ta_unload_debugfs_fops = {
93 .write = ta_if_unload_debugfs_write,
94 .llseek = default_llseek,
98 static const struct file_operations ta_invoke_debugfs_fops = {
99 .write = ta_if_invoke_debugfs_write,
100 .llseek = default_llseek,
105 * DOC: AMDGPU TA debugfs interfaces
107 * Three debugfs interfaces can be opened by a program to
108 * load/invoke/unload TA,
110 * - /sys/kernel/debug/dri/<N>/ta_if/ta_load
111 * - /sys/kernel/debug/dri/<N>/ta_if/ta_invoke
112 * - /sys/kernel/debug/dri/<N>/ta_if/ta_unload
114 * How to use the interfaces in a program?
116 * A program needs to provide transmit buffer to the interfaces
117 * and will receive buffer from the interfaces below,
119 * - For TA load debugfs interface:
122 * - TA bin length (4bytes)
127 * - For TA invoke debugfs interface:
131 * - TA CMD ID (4bytes)
132 * - TA shard buf length
133 * (4bytes, value not beyond TA shared memory size)
138 * - For TA unload debugfs interface:
144 static ssize_t ta_if_load_debugfs_write(struct file *fp, const char *buf, size_t len, loff_t *off)
146 uint32_t ta_type = 0;
147 uint32_t ta_bin_len = 0;
148 uint8_t *ta_bin = NULL;
149 uint32_t copy_pos = 0;
152 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(fp)->i_private;
153 struct psp_context *psp = &adev->psp;
154 struct ta_context *context = NULL;
159 ret = copy_from_user((void *)&ta_type, &buf[copy_pos], sizeof(uint32_t));
160 if (ret || (!is_ta_type_valid(ta_type)))
163 copy_pos += sizeof(uint32_t);
165 ret = copy_from_user((void *)&ta_bin_len, &buf[copy_pos], sizeof(uint32_t));
169 if (ta_bin_len > PSP_1_MEG)
172 copy_pos += sizeof(uint32_t);
174 ta_bin = kzalloc(ta_bin_len, GFP_KERNEL);
177 if (copy_from_user((void *)ta_bin, &buf[copy_pos], ta_bin_len)) {
182 /* Set TA context and functions */
183 set_ta_context_funcs(psp, ta_type, &context);
185 if (!psp->ta_funcs || !psp->ta_funcs->fn_ta_terminate) {
186 dev_err(adev->dev, "Unsupported function to terminate TA\n");
192 * Allocate TA shared buf in case shared buf was freed
193 * due to loading TA failed before.
195 if (!context->mem_context.shared_buf) {
196 ret = psp_ta_init_shared_buf(psp, &context->mem_context);
203 ret = psp_fn_ta_terminate(psp);
204 if (ret || context->resp_status) {
206 "Failed to unload embedded TA (%d) and status (0x%X)\n",
207 ret, context->resp_status);
210 goto err_free_ta_shared_buf;
213 /* Prepare TA context for TA initialization */
214 context->ta_type = ta_type;
215 context->bin_desc.fw_version = get_bin_version(ta_bin);
216 context->bin_desc.size_bytes = ta_bin_len;
217 context->bin_desc.start_addr = ta_bin;
219 if (!psp->ta_funcs->fn_ta_initialize) {
220 dev_err(adev->dev, "Unsupported function to initialize TA\n");
222 goto err_free_ta_shared_buf;
225 ret = psp_fn_ta_initialize(psp);
226 if (ret || context->resp_status) {
227 dev_err(adev->dev, "Failed to load TA via debugfs (%d) and status (0x%X)\n",
228 ret, context->resp_status);
231 goto err_free_ta_shared_buf;
234 if (copy_to_user((char *)buf, (void *)&context->session_id, sizeof(uint32_t)))
237 err_free_ta_shared_buf:
238 /* Only free TA shared buf when returns error code */
239 if (ret && context->mem_context.shared_buf)
240 psp_ta_free_shared_buf(&context->mem_context);
247 static ssize_t ta_if_unload_debugfs_write(struct file *fp, const char *buf, size_t len, loff_t *off)
249 uint32_t ta_type = 0;
251 uint32_t copy_pos = 0;
254 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(fp)->i_private;
255 struct psp_context *psp = &adev->psp;
256 struct ta_context *context = NULL;
261 ret = copy_from_user((void *)&ta_type, &buf[copy_pos], sizeof(uint32_t));
262 if (ret || (!is_ta_type_valid(ta_type)))
265 copy_pos += sizeof(uint32_t);
267 ret = copy_from_user((void *)&ta_id, &buf[copy_pos], sizeof(uint32_t));
271 set_ta_context_funcs(psp, ta_type, &context);
272 context->session_id = ta_id;
274 if (!psp->ta_funcs || !psp->ta_funcs->fn_ta_terminate) {
275 dev_err(adev->dev, "Unsupported function to terminate TA\n");
279 ret = psp_fn_ta_terminate(psp);
280 if (ret || context->resp_status) {
281 dev_err(adev->dev, "Failed to unload TA via debugfs (%d) and status (0x%X)\n",
282 ret, context->resp_status);
287 if (context->mem_context.shared_buf)
288 psp_ta_free_shared_buf(&context->mem_context);
293 static ssize_t ta_if_invoke_debugfs_write(struct file *fp, const char *buf, size_t len, loff_t *off)
295 uint32_t ta_type = 0;
298 uint32_t shared_buf_len = 0;
299 uint8_t *shared_buf = NULL;
300 uint32_t copy_pos = 0;
303 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(fp)->i_private;
304 struct psp_context *psp = &adev->psp;
305 struct ta_context *context = NULL;
310 ret = copy_from_user((void *)&ta_type, &buf[copy_pos], sizeof(uint32_t));
313 copy_pos += sizeof(uint32_t);
315 ret = copy_from_user((void *)&ta_id, &buf[copy_pos], sizeof(uint32_t));
318 copy_pos += sizeof(uint32_t);
320 ret = copy_from_user((void *)&cmd_id, &buf[copy_pos], sizeof(uint32_t));
323 copy_pos += sizeof(uint32_t);
325 ret = copy_from_user((void *)&shared_buf_len, &buf[copy_pos], sizeof(uint32_t));
328 copy_pos += sizeof(uint32_t);
330 shared_buf = kzalloc(shared_buf_len, GFP_KERNEL);
333 if (copy_from_user((void *)shared_buf, &buf[copy_pos], shared_buf_len)) {
335 goto err_free_shared_buf;
338 set_ta_context_funcs(psp, ta_type, &context);
340 if (!context || !context->initialized) {
341 dev_err(adev->dev, "TA is not initialized\n");
343 goto err_free_shared_buf;
346 if (!psp->ta_funcs || !psp->ta_funcs->fn_ta_invoke) {
347 dev_err(adev->dev, "Unsupported function to invoke TA\n");
349 goto err_free_shared_buf;
352 context->session_id = ta_id;
354 mutex_lock(&psp->ras_context.mutex);
355 ret = prep_ta_mem_context(&context->mem_context, shared_buf, shared_buf_len);
357 goto err_free_shared_buf;
359 ret = psp_fn_ta_invoke(psp, cmd_id);
360 if (ret || context->resp_status) {
361 dev_err(adev->dev, "Failed to invoke TA via debugfs (%d) and status (0x%X)\n",
362 ret, context->resp_status);
365 goto err_free_shared_buf;
369 if (copy_to_user((char *)&buf[copy_pos], context->mem_context.shared_buf, shared_buf_len))
373 mutex_unlock(&psp->ras_context.mutex);
379 void amdgpu_ta_if_debugfs_init(struct amdgpu_device *adev)
381 struct drm_minor *minor = adev_to_drm(adev)->primary;
383 struct dentry *dir = debugfs_create_dir("ta_if", minor->debugfs_root);
385 debugfs_create_file("ta_load", 0200, dir, adev,
386 &ta_load_debugfs_fops);
388 debugfs_create_file("ta_unload", 0200, dir,
389 adev, &ta_unload_debugfs_fops);
391 debugfs_create_file("ta_invoke", 0200, dir,
392 adev, &ta_invoke_debugfs_fops);
396 void amdgpu_ta_if_debugfs_init(struct amdgpu_device *adev)