18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 2020 Advanced Micro Devices, Inc.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
58c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
68c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
78c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
98c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
128c2ecf20Sopenharmony_ci * all copies or substantial portions of the Software.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
158c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
168c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
178c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
188c2ecf20Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
198c2ecf20Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
208c2ecf20Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci */
248c2ecf20Sopenharmony_ci#include <linux/debugfs.h>
258c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#include "amdgpu.h"
288c2ecf20Sopenharmony_ci#include "amdgpu_rap.h"
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/**
318c2ecf20Sopenharmony_ci * DOC: AMDGPU RAP debugfs test interface
328c2ecf20Sopenharmony_ci *
338c2ecf20Sopenharmony_ci * how to use?
348c2ecf20Sopenharmony_ci * echo opcode > <debugfs_dir>/dri/xxx/rap_test
358c2ecf20Sopenharmony_ci *
368c2ecf20Sopenharmony_ci * opcode:
378c2ecf20Sopenharmony_ci * currently, only 2 is supported by Linux host driver,
388c2ecf20Sopenharmony_ci * opcode 2 stands for TA_CMD_RAP__VALIDATE_L0, used to
398c2ecf20Sopenharmony_ci * trigger L0 policy validation, you can refer more detail
408c2ecf20Sopenharmony_ci * from header file ta_rap_if.h
418c2ecf20Sopenharmony_ci *
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_cistatic ssize_t amdgpu_rap_debugfs_write(struct file *f, const char __user *buf,
448c2ecf20Sopenharmony_ci		size_t size, loff_t *pos)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
478c2ecf20Sopenharmony_ci	struct ta_rap_shared_memory *rap_shared_mem;
488c2ecf20Sopenharmony_ci	struct ta_rap_cmd_output_data *rap_cmd_output;
498c2ecf20Sopenharmony_ci	struct drm_device *dev = adev_to_drm(adev);
508c2ecf20Sopenharmony_ci	uint32_t op;
518c2ecf20Sopenharmony_ci	int ret;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	if (*pos || size != 2)
548c2ecf20Sopenharmony_ci		return -EINVAL;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	ret = kstrtouint_from_user(buf, size, *pos, &op);
578c2ecf20Sopenharmony_ci	if (ret)
588c2ecf20Sopenharmony_ci		return ret;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	ret = pm_runtime_get_sync(dev->dev);
618c2ecf20Sopenharmony_ci	if (ret < 0) {
628c2ecf20Sopenharmony_ci		pm_runtime_put_autosuspend(dev->dev);
638c2ecf20Sopenharmony_ci		return ret;
648c2ecf20Sopenharmony_ci	}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	/* make sure gfx core is on, RAP TA cann't handle
678c2ecf20Sopenharmony_ci	 * GFX OFF case currently.
688c2ecf20Sopenharmony_ci	 */
698c2ecf20Sopenharmony_ci	amdgpu_gfx_off_ctrl(adev, false);
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	switch (op) {
728c2ecf20Sopenharmony_ci	case 2:
738c2ecf20Sopenharmony_ci		ret = psp_rap_invoke(&adev->psp, op);
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci		if (ret == TA_RAP_STATUS__SUCCESS) {
768c2ecf20Sopenharmony_ci			dev_info(adev->dev, "RAP L0 validate test success.\n");
778c2ecf20Sopenharmony_ci		} else {
788c2ecf20Sopenharmony_ci			rap_shared_mem = (struct ta_rap_shared_memory *)
798c2ecf20Sopenharmony_ci					 adev->psp.rap_context.rap_shared_buf;
808c2ecf20Sopenharmony_ci			rap_cmd_output = &(rap_shared_mem->rap_out_message.output);
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci			dev_info(adev->dev, "RAP test failed, the output is:\n");
838c2ecf20Sopenharmony_ci			dev_info(adev->dev, "\tlast_subsection: 0x%08x.\n",
848c2ecf20Sopenharmony_ci				 rap_cmd_output->last_subsection);
858c2ecf20Sopenharmony_ci			dev_info(adev->dev, "\tnum_total_validate: 0x%08x.\n",
868c2ecf20Sopenharmony_ci				 rap_cmd_output->num_total_validate);
878c2ecf20Sopenharmony_ci			dev_info(adev->dev, "\tnum_valid: 0x%08x.\n",
888c2ecf20Sopenharmony_ci				 rap_cmd_output->num_valid);
898c2ecf20Sopenharmony_ci			dev_info(adev->dev, "\tlast_validate_addr: 0x%08x.\n",
908c2ecf20Sopenharmony_ci				 rap_cmd_output->last_validate_addr);
918c2ecf20Sopenharmony_ci			dev_info(adev->dev, "\tlast_validate_val: 0x%08x.\n",
928c2ecf20Sopenharmony_ci				 rap_cmd_output->last_validate_val);
938c2ecf20Sopenharmony_ci			dev_info(adev->dev, "\tlast_validate_val_exptd: 0x%08x.\n",
948c2ecf20Sopenharmony_ci				 rap_cmd_output->last_validate_val_exptd);
958c2ecf20Sopenharmony_ci		}
968c2ecf20Sopenharmony_ci		break;
978c2ecf20Sopenharmony_ci	default:
988c2ecf20Sopenharmony_ci		dev_info(adev->dev, "Unsupported op id: %d, ", op);
998c2ecf20Sopenharmony_ci		dev_info(adev->dev, "Only support op 2(L0 validate test).\n");
1008c2ecf20Sopenharmony_ci	}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	amdgpu_gfx_off_ctrl(adev, true);
1038c2ecf20Sopenharmony_ci	pm_runtime_mark_last_busy(dev->dev);
1048c2ecf20Sopenharmony_ci	pm_runtime_put_autosuspend(dev->dev);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	return size;
1078c2ecf20Sopenharmony_ci}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_cistatic const struct file_operations amdgpu_rap_debugfs_ops = {
1108c2ecf20Sopenharmony_ci	.owner = THIS_MODULE,
1118c2ecf20Sopenharmony_ci	.read = NULL,
1128c2ecf20Sopenharmony_ci	.write = amdgpu_rap_debugfs_write,
1138c2ecf20Sopenharmony_ci	.llseek = default_llseek
1148c2ecf20Sopenharmony_ci};
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_civoid amdgpu_rap_debugfs_init(struct amdgpu_device *adev)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci#if defined(CONFIG_DEBUG_FS)
1198c2ecf20Sopenharmony_ci	struct drm_minor *minor = adev_to_drm(adev)->primary;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	if (!adev->psp.rap_context.rap_initialized)
1228c2ecf20Sopenharmony_ci		return;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	debugfs_create_file("rap_test", S_IWUSR, minor->debugfs_root,
1258c2ecf20Sopenharmony_ci				adev, &amdgpu_rap_debugfs_ops);
1268c2ecf20Sopenharmony_ci#endif
1278c2ecf20Sopenharmony_ci}
128