summaryrefslogtreecommitdiffstats
path: root/drivers/gpu/drm/amd/amdgpu/amdgpu_psp_ta.c
blob: 0c856005df6b95642af7b7ae4bdbf6a33a86f7ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 * Copyright 2022 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#include "amdgpu.h"
#include "amdgpu_psp_ta.h"

#if defined(CONFIG_DEBUG_FS)

static ssize_t ta_if_load_debugfs_write(struct file *fp, const char *buf,
					    size_t len, loff_t *off);
static ssize_t ta_if_unload_debugfs_write(struct file *fp, const char *buf,
					    size_t len, loff_t *off);
static ssize_t ta_if_invoke_debugfs_write(struct file *fp, const char *buf,
					    size_t len, loff_t *off);

static uint32_t get_bin_version(const uint8_t *bin)
{
	const struct common_firmware_header *hdr =
			     (const struct common_firmware_header *)bin;

	return hdr->ucode_version;
}

static int prep_ta_mem_context(struct ta_mem_context *mem_context,
					     uint8_t *shared_buf,
					     uint32_t shared_buf_len)
{
	if (mem_context->shared_mem_size < shared_buf_len)
		return -EINVAL;
	memset(mem_context->shared_buf, 0, mem_context->shared_mem_size);
	memcpy((void *)mem_context->shared_buf, shared_buf, shared_buf_len);

	return 0;
}

static bool is_ta_type_valid(enum ta_type_id ta_type)
{
	switch (ta_type) {
	case TA_TYPE_RAS:
		return true;
	default:
		return false;
	}
}

static const struct ta_funcs ras_ta_funcs = {
	.fn_ta_initialize = psp_ras_initialize,
	.fn_ta_invoke    = psp_ras_invoke,
	.fn_ta_terminate = psp_ras_terminate
};

static void set_ta_context_funcs(struct psp_context *psp,
						      enum ta_type_id ta_type,
						      struct ta_context **pcontext)
{
	switch (ta_type) {
	case TA_TYPE_RAS:
		*pcontext = &psp->ras_context.context;
		psp->ta_funcs = &ras_ta_funcs;
		break;
	default:
		break;
	}
}

static const struct file_operations ta_load_debugfs_fops = {
	.write  = ta_if_load_debugfs_write,
	.llseek = default_llseek,
	.owner  = THIS_MODULE
};

static const struct file_operations ta_unload_debugfs_fops = {
	.write  = ta_if_unload_debugfs_write,
	.llseek = default_llseek,
	.owner  = THIS_MODULE
};

static const struct file_operations ta_invoke_debugfs_fops = {
	.write  = ta_if_invoke_debugfs_write,
	.llseek = default_llseek,
	.owner  = THIS_MODULE
};

/*
 * DOC: AMDGPU TA debugfs interfaces
 *
 * Three debugfs interfaces can be opened by a program to
 * load/invoke/unload TA,
 *
 * - /sys/kernel/debug/dri/<N>/ta_if/ta_load
 * - /sys/kernel/debug/dri/<N>/ta_if/ta_invoke
 * - /sys/kernel/debug/dri/<N>/ta_if/ta_unload
 *
 * How to use the interfaces in a program?
 *
 * A program needs to provide transmit buffer to the interfaces
 * and will receive buffer from the interfaces below,
 *
 * - For TA load debugfs interface:
 *   Transmit buffer:
 *    - TA type (4bytes)
 *    - TA bin length (4bytes)
 *    - TA bin
 *   Receive buffer:
 *    - TA ID (4bytes)
 *
 * - For TA invoke debugfs interface:
 *   Transmit buffer:
 *    - TA type (4bytes)
 *    - TA ID (4bytes)
 *    - TA CMD ID (4bytes)
 *    - TA shard buf length
 *      (4bytes, value not beyond TA shared memory size)
 *    - TA shared buf
 *   Receive buffer:
 *    - TA shared buf
 *
 * - For TA unload debugfs interface:
 *   Transmit buffer:
 *    - TA type (4bytes)
 *    - TA ID (4bytes)
 */

static ssize_t ta_if_load_debugfs_write(struct file *fp, const char *buf, size_t len, loff_t *off)
{
	uint32_t ta_type    = 0;
	uint32_t ta_bin_len = 0;
	uint8_t  *ta_bin    = NULL;
	uint32_t copy_pos   = 0;
	int      ret        = 0;

	struct amdgpu_device *adev    = (struct amdgpu_device *)file_inode(fp)->i_private;
	struct psp_context   *psp     = &adev->psp;
	struct ta_context    *context = NULL;

	if (!buf)
		return -EINVAL;

	ret = copy_from_user((void *)&ta_type, &buf[copy_pos], sizeof(uint32_t));
	if (ret || (!is_ta_type_valid(ta_type)))
		return -EFAULT;

	copy_pos += sizeof(uint32_t);

	ret = copy_from_user((void *)&ta_bin_len, &buf[copy_pos], sizeof(uint32_t));
	if (ret)
		return -EFAULT;

	copy_pos += sizeof(uint32_t);

	ta_bin = kzalloc(ta_bin_len, GFP_KERNEL);
	if (!ta_bin)
		return -ENOMEM;
	if (copy_from_user((void *)ta_bin, &buf[copy_pos], ta_bin_len)) {
		ret = -EFAULT;
		goto err_free_bin;
	}

	/* Set TA context and functions */
	set_ta_context_funcs(psp, ta_type, &context);

	if (!psp->ta_funcs || !psp->ta_funcs->fn_ta_terminate) {
		dev_err(adev->dev, "Unsupported function to terminate TA\n");
		ret = -EOPNOTSUPP;
		goto err_free_bin;
	}

	/*
	 * Allocate TA shared buf in case shared buf was freed
	 * due to loading TA failed before.
	 */
	if (!context->mem_context.shared_buf) {
		ret = psp_ta_init_shared_buf(psp, &context->mem_context);
		if (ret) {
			ret = -ENOMEM;
			goto err_free_bin;
		}
	}

	ret = psp_fn_ta_terminate(psp);
	if (ret || context->resp_status) {
		dev_err(adev->dev,
			"Failed to unload embedded TA (%d) and status (0x%X)\n",
			ret, context->resp_status);
		if (!ret)
			ret = -EINVAL;
		goto err_free_ta_shared_buf;
	}

	/* Prepare TA context for TA initialization */
	context->ta_type                     = ta_type;
	context->bin_desc.fw_version         = get_bin_version(ta_bin);
	context->bin_desc.size_bytes         = ta_bin_len;
	context->bin_desc.start_addr         = ta_bin;

	if (!psp->ta_funcs->fn_ta_initialize) {
		dev_err(adev->dev, "Unsupported function to initialize TA\n");
		ret = -EOPNOTSUPP;
		goto err_free_ta_shared_buf;
	}

	ret = psp_fn_ta_initialize(psp);
	if (ret || context->resp_status) {
		dev_err(adev->dev, "Failed to load TA via debugfs (%d) and status (0x%X)\n",
			ret, context->resp_status);
		if (!ret)
			ret = -EINVAL;
		goto err_free_ta_shared_buf;
	}

	if (copy_to_user((char *)buf, (void *)&context->session_id, sizeof(uint32_t)))
		ret = -EFAULT;

err_free_ta_shared_buf:
	/* Only free TA shared buf when returns error code */
	if (ret && context->mem_context.shared_buf)
		psp_ta_free_shared_buf(&context->mem_context);
err_free_bin:
	kfree(ta_bin);

	return ret;
}

static ssize_t ta_if_unload_debugfs_write(struct file *fp, const char *buf, size_t len, loff_t *off)
{
	uint32_t ta_type    = 0;
	uint32_t ta_id      = 0;
	uint32_t copy_pos   = 0;
	int      ret        = 0;

	struct amdgpu_device *adev    = (struct amdgpu_device *)file_inode(fp)->i_private;
	struct psp_context   *psp     = &adev->psp;
	struct ta_context    *context = NULL;

	if (!buf)
		return -EINVAL;

	ret = copy_from_user((void *)&ta_type, &buf[copy_pos], sizeof(uint32_t));
	if (ret || (!is_ta_type_valid(ta_type)))
		return -EFAULT;

	copy_pos += sizeof(uint32_t);

	ret = copy_from_user((void *)&ta_id, &buf[copy_pos], sizeof(uint32_t));
	if (ret)
		return -EFAULT;

	set_ta_context_funcs(psp, ta_type, &context);
	context->session_id = ta_id;

	if (!psp->ta_funcs || !psp->ta_funcs->fn_ta_terminate) {
		dev_err(adev->dev, "Unsupported function to terminate TA\n");
		return -EOPNOTSUPP;
	}

	ret = psp_fn_ta_terminate(psp);
	if (ret || context->resp_status) {
		dev_err(adev->dev, "Failed to unload TA via debugfs (%d) and status (0x%X)\n",
			ret, context->resp_status);
		if (!ret)
			ret = -EINVAL;
	}

	if (context->mem_context.shared_buf)
		psp_ta_free_shared_buf(&context->mem_context);

	return ret;
}

static ssize_t ta_if_invoke_debugfs_write(struct file *fp, const char *buf, size_t len, loff_t *off)
{
	uint32_t ta_type        = 0;
	uint32_t ta_id          = 0;
	uint32_t cmd_id         = 0;
	uint32_t shared_buf_len = 0;
	uint8_t *shared_buf     = NULL;
	uint32_t copy_pos       = 0;
	int      ret            = 0;

	struct amdgpu_device *adev    = (struct amdgpu_device *)file_inode(fp)->i_private;
	struct psp_context   *psp     = &adev->psp;
	struct ta_context    *context = NULL;

	if (!buf)
		return -EINVAL;

	ret = copy_from_user((void *)&ta_type, &buf[copy_pos], sizeof(uint32_t));
	if (ret)
		return -EFAULT;
	copy_pos += sizeof(uint32_t);

	ret = copy_from_user((void *)&ta_id, &buf[copy_pos], sizeof(uint32_t));
	if (ret)
		return -EFAULT;
	copy_pos += sizeof(uint32_t);

	ret = copy_from_user((void *)&cmd_id, &buf[copy_pos], sizeof(uint32_t));
	if (ret)
		return -EFAULT;
	copy_pos += sizeof(uint32_t);

	ret = copy_from_user((void *)&shared_buf_len, &buf[copy_pos], sizeof(uint32_t));
	if (ret)
		return -EFAULT;
	copy_pos += sizeof(uint32_t);

	shared_buf = kzalloc(shared_buf_len, GFP_KERNEL);
	if (!shared_buf)
		return -ENOMEM;
	if (copy_from_user((void *)shared_buf, &buf[copy_pos], shared_buf_len)) {
		ret = -EFAULT;
		goto err_free_shared_buf;
	}

	set_ta_context_funcs(psp, ta_type, &context);

	if (!context || !context->initialized) {
		dev_err(adev->dev, "TA is not initialized\n");
		ret = -EINVAL;
		goto err_free_shared_buf;
	}

	if (!psp->ta_funcs || !psp->ta_funcs->fn_ta_invoke) {
		dev_err(adev->dev, "Unsupported function to invoke TA\n");
		ret = -EOPNOTSUPP;
		goto err_free_shared_buf;
	}

	context->session_id = ta_id;

	mutex_lock(&psp->ras_context.mutex);
	ret = prep_ta_mem_context(&context->mem_context, shared_buf, shared_buf_len);
	if (ret)
		goto err_free_shared_buf;

	ret = psp_fn_ta_invoke(psp, cmd_id);
	if (ret || context->resp_status) {
		dev_err(adev->dev, "Failed to invoke TA via debugfs (%d) and status (0x%X)\n",
			ret, context->resp_status);
		if (!ret) {
			ret = -EINVAL;
			goto err_free_shared_buf;
		}
	}

	if (copy_to_user((char *)&buf[copy_pos], context->mem_context.shared_buf, shared_buf_len))
		ret = -EFAULT;

err_free_shared_buf:
	mutex_unlock(&psp->ras_context.mutex);
	kfree(shared_buf);

	return ret;
}

void amdgpu_ta_if_debugfs_init(struct amdgpu_device *adev)
{
	struct drm_minor *minor = adev_to_drm(adev)->primary;

	struct dentry *dir = debugfs_create_dir("ta_if", minor->debugfs_root);

	debugfs_create_file("ta_load", 0200, dir, adev,
				     &ta_load_debugfs_fops);

	debugfs_create_file("ta_unload", 0200, dir,
				     adev, &ta_unload_debugfs_fops);

	debugfs_create_file("ta_invoke", 0200, dir,
				     adev, &ta_invoke_debugfs_fops);
}

#else
void amdgpu_ta_if_debugfs_init(struct amdgpu_device *adev)
{

}
#endif