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 void prep_ta_mem_context(struct psp_context *psp,
45 struct ta_context *context,
47 uint32_t shared_buf_len)
49 context->mem_context.shared_mem_size = PAGE_ALIGN(shared_buf_len);
50 psp_ta_init_shared_buf(psp, &context->mem_context);
52 memcpy((void *)context->mem_context.shared_buf, shared_buf, shared_buf_len);
55 static bool is_ta_type_valid(enum ta_type_id ta_type)
70 static const struct file_operations ta_load_debugfs_fops = {
71 .write = ta_if_load_debugfs_write,
72 .llseek = default_llseek,
76 static const struct file_operations ta_unload_debugfs_fops = {
77 .write = ta_if_unload_debugfs_write,
78 .llseek = default_llseek,
82 static const struct file_operations ta_invoke_debugfs_fops = {
83 .write = ta_if_invoke_debugfs_write,
84 .llseek = default_llseek,
90 * DOC: AMDGPU TA debugfs interfaces
92 * Three debugfs interfaces can be opened by a program to
93 * load/invoke/unload TA,
95 * - /sys/kernel/debug/dri/<N>/ta_if/ta_load
96 * - /sys/kernel/debug/dri/<N>/ta_if/ta_invoke
97 * - /sys/kernel/debug/dri/<N>/ta_if/ta_unload
99 * How to use the interfaces in a program?
101 * A program needs to provide transmit buffer to the interfaces
102 * and will receive buffer from the interfaces below,
104 * - For TA load debugfs interface:
107 * - TA bin length (4bytes)
112 * - For TA invoke debugfs interface:
115 * - TA CMD ID (4bytes)
116 * - TA shard buf length (4bytes)
121 * - For TA unload debugfs interface:
126 static ssize_t ta_if_load_debugfs_write(struct file *fp, const char *buf, size_t len, loff_t *off)
128 uint32_t ta_type = 0;
129 uint32_t ta_bin_len = 0;
130 uint8_t *ta_bin = NULL;
131 uint32_t copy_pos = 0;
134 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(fp)->i_private;
135 struct psp_context *psp = &adev->psp;
136 struct ta_context context = {0};
141 ret = copy_from_user((void *)&ta_type, &buf[copy_pos], sizeof(uint32_t));
142 if (ret || (!is_ta_type_valid(ta_type)))
145 copy_pos += sizeof(uint32_t);
147 ret = copy_from_user((void *)&ta_bin_len, &buf[copy_pos], sizeof(uint32_t));
151 copy_pos += sizeof(uint32_t);
153 ta_bin = kzalloc(ta_bin_len, GFP_KERNEL);
156 if (copy_from_user((void *)ta_bin, &buf[copy_pos], ta_bin_len)) {
161 ret = psp_ras_terminate(psp);
163 dev_err(adev->dev, "Failed to unload embedded RAS TA\n");
167 context.ta_type = ta_type;
168 context.ta_load_type = GFX_CMD_ID_LOAD_TA;
169 context.bin_desc.fw_version = get_bin_version(ta_bin);
170 context.bin_desc.size_bytes = ta_bin_len;
171 context.bin_desc.start_addr = ta_bin;
173 ret = psp_ta_load(psp, &context);
175 if (ret || context.resp_status) {
176 dev_err(adev->dev, "TA load via debugfs failed (%d) status %d\n",
177 ret, context.resp_status);
183 context.initialized = true;
184 if (copy_to_user((char *)buf, (void *)&context.session_id, sizeof(uint32_t)))
193 static ssize_t ta_if_unload_debugfs_write(struct file *fp, const char *buf, size_t len, loff_t *off)
198 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(fp)->i_private;
199 struct psp_context *psp = &adev->psp;
200 struct ta_context context = {0};
205 ret = copy_from_user((void *)&ta_id, buf, sizeof(uint32_t));
209 context.session_id = ta_id;
211 ret = psp_ta_unload(psp, &context);
213 context.initialized = false;
218 static ssize_t ta_if_invoke_debugfs_write(struct file *fp, const char *buf, size_t len, loff_t *off)
222 uint32_t shared_buf_len = 0;
223 uint8_t *shared_buf = NULL;
224 uint32_t copy_pos = 0;
227 struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(fp)->i_private;
228 struct psp_context *psp = &adev->psp;
229 struct ta_context context = {0};
234 ret = copy_from_user((void *)&ta_id, &buf[copy_pos], sizeof(uint32_t));
237 copy_pos += sizeof(uint32_t);
239 ret = copy_from_user((void *)&cmd_id, &buf[copy_pos], sizeof(uint32_t));
242 copy_pos += sizeof(uint32_t);
244 ret = copy_from_user((void *)&shared_buf_len, &buf[copy_pos], sizeof(uint32_t));
247 copy_pos += sizeof(uint32_t);
249 shared_buf = kzalloc(shared_buf_len, GFP_KERNEL);
252 if (copy_from_user((void *)shared_buf, &buf[copy_pos], shared_buf_len)) {
254 goto err_free_shared_buf;
257 context.session_id = ta_id;
259 prep_ta_mem_context(psp, &context, shared_buf, shared_buf_len);
261 ret = psp_ta_invoke_indirect(psp, cmd_id, &context);
263 if (ret || context.resp_status) {
264 dev_err(adev->dev, "TA invoke via debugfs failed (%d) status %d\n",
265 ret, context.resp_status);
268 goto err_free_ta_shared_buf;
271 if (copy_to_user((char *)buf, context.mem_context.shared_buf, shared_buf_len))
274 err_free_ta_shared_buf:
275 psp_ta_free_shared_buf(&context.mem_context);
283 void amdgpu_ta_if_debugfs_init(struct amdgpu_device *adev)
285 struct drm_minor *minor = adev_to_drm(adev)->primary;
287 struct dentry *dir = debugfs_create_dir("ta_if", minor->debugfs_root);
289 debugfs_create_file("ta_load", 0200, dir, adev,
290 &ta_load_debugfs_fops);
292 debugfs_create_file("ta_unload", 0200, dir,
293 adev, &ta_unload_debugfs_fops);
295 debugfs_create_file("ta_invoke", 0200, dir,
296 adev, &ta_invoke_debugfs_fops);
300 void amdgpu_ta_if_debugfs_init(struct amdgpu_device *adev)