162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright 2021 Advanced Micro Devices, Inc.
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
562306a36Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
662306a36Sopenharmony_ci * to deal in the Software without restriction, including without limitation
762306a36Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
862306a36Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
962306a36Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
1262306a36Sopenharmony_ci * all copies or substantial portions of the Software.
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1562306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1662306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1762306a36Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
1862306a36Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1962306a36Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2062306a36Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
2162306a36Sopenharmony_ci *
2262306a36Sopenharmony_ci *
2362306a36Sopenharmony_ci */
2462306a36Sopenharmony_ci#include <linux/debugfs.h>
2562306a36Sopenharmony_ci#include <linux/pm_runtime.h>
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci#include "amdgpu.h"
2862306a36Sopenharmony_ci#include "amdgpu_securedisplay.h"
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci/**
3162306a36Sopenharmony_ci * DOC: AMDGPU SECUREDISPLAY debugfs test interface
3262306a36Sopenharmony_ci *
3362306a36Sopenharmony_ci * how to use?
3462306a36Sopenharmony_ci * echo opcode <value> > <debugfs_dir>/dri/xxx/securedisplay_test
3562306a36Sopenharmony_ci * eg. echo 1 > <debugfs_dir>/dri/xxx/securedisplay_test
3662306a36Sopenharmony_ci * eg. echo 2 phy_id > <debugfs_dir>/dri/xxx/securedisplay_test
3762306a36Sopenharmony_ci *
3862306a36Sopenharmony_ci * opcode:
3962306a36Sopenharmony_ci * 1:Query whether TA is responding used only for validation pupose
4062306a36Sopenharmony_ci * 2: Send region of Interest and CRC value to I2C. (uint32)phy_id is
4162306a36Sopenharmony_ci * send to determine which DIO scratch register should be used to get
4262306a36Sopenharmony_ci * ROI and receive i2c_buf as the output.
4362306a36Sopenharmony_ci *
4462306a36Sopenharmony_ci * You can refer more detail from header file ta_securedisplay_if.h
4562306a36Sopenharmony_ci *
4662306a36Sopenharmony_ci */
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_civoid psp_securedisplay_parse_resp_status(struct psp_context *psp,
4962306a36Sopenharmony_ci	enum ta_securedisplay_status status)
5062306a36Sopenharmony_ci{
5162306a36Sopenharmony_ci	switch (status) {
5262306a36Sopenharmony_ci	case TA_SECUREDISPLAY_STATUS__SUCCESS:
5362306a36Sopenharmony_ci		break;
5462306a36Sopenharmony_ci	case TA_SECUREDISPLAY_STATUS__GENERIC_FAILURE:
5562306a36Sopenharmony_ci		dev_err(psp->adev->dev, "Secure display: Generic Failure.");
5662306a36Sopenharmony_ci		break;
5762306a36Sopenharmony_ci	case TA_SECUREDISPLAY_STATUS__INVALID_PARAMETER:
5862306a36Sopenharmony_ci		dev_err(psp->adev->dev, "Secure display: Invalid Parameter.");
5962306a36Sopenharmony_ci		break;
6062306a36Sopenharmony_ci	case TA_SECUREDISPLAY_STATUS__NULL_POINTER:
6162306a36Sopenharmony_ci		dev_err(psp->adev->dev, "Secure display: Null Pointer.");
6262306a36Sopenharmony_ci		break;
6362306a36Sopenharmony_ci	case TA_SECUREDISPLAY_STATUS__I2C_WRITE_ERROR:
6462306a36Sopenharmony_ci		dev_err(psp->adev->dev, "Secure display: Failed to write to I2C.");
6562306a36Sopenharmony_ci		break;
6662306a36Sopenharmony_ci	case TA_SECUREDISPLAY_STATUS__READ_DIO_SCRATCH_ERROR:
6762306a36Sopenharmony_ci		dev_err(psp->adev->dev, "Secure display: Failed to Read DIO Scratch Register.");
6862306a36Sopenharmony_ci		break;
6962306a36Sopenharmony_ci	case TA_SECUREDISPLAY_STATUS__READ_CRC_ERROR:
7062306a36Sopenharmony_ci		dev_err(psp->adev->dev, "Secure display: Failed to Read CRC");
7162306a36Sopenharmony_ci		break;
7262306a36Sopenharmony_ci	case TA_SECUREDISPLAY_STATUS__I2C_INIT_ERROR:
7362306a36Sopenharmony_ci		dev_err(psp->adev->dev, "Secure display: Failed to initialize I2C.");
7462306a36Sopenharmony_ci		break;
7562306a36Sopenharmony_ci	default:
7662306a36Sopenharmony_ci		dev_err(psp->adev->dev, "Secure display: Failed to parse status: %d\n", status);
7762306a36Sopenharmony_ci	}
7862306a36Sopenharmony_ci}
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_civoid psp_prep_securedisplay_cmd_buf(struct psp_context *psp, struct ta_securedisplay_cmd **cmd,
8162306a36Sopenharmony_ci	enum ta_securedisplay_command command_id)
8262306a36Sopenharmony_ci{
8362306a36Sopenharmony_ci	*cmd = (struct ta_securedisplay_cmd *)psp->securedisplay_context.context.mem_context.shared_buf;
8462306a36Sopenharmony_ci	memset(*cmd, 0, sizeof(struct ta_securedisplay_cmd));
8562306a36Sopenharmony_ci	(*cmd)->status = TA_SECUREDISPLAY_STATUS__GENERIC_FAILURE;
8662306a36Sopenharmony_ci	(*cmd)->cmd_id = command_id;
8762306a36Sopenharmony_ci}
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci#if defined(CONFIG_DEBUG_FS)
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_cistatic ssize_t amdgpu_securedisplay_debugfs_write(struct file *f, const char __user *buf,
9262306a36Sopenharmony_ci		size_t size, loff_t *pos)
9362306a36Sopenharmony_ci{
9462306a36Sopenharmony_ci	struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
9562306a36Sopenharmony_ci	struct psp_context *psp = &adev->psp;
9662306a36Sopenharmony_ci	struct ta_securedisplay_cmd *securedisplay_cmd;
9762306a36Sopenharmony_ci	struct drm_device *dev = adev_to_drm(adev);
9862306a36Sopenharmony_ci	uint32_t phy_id;
9962306a36Sopenharmony_ci	uint32_t op;
10062306a36Sopenharmony_ci	char str[64];
10162306a36Sopenharmony_ci	int ret;
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	if (*pos || size > sizeof(str) - 1)
10462306a36Sopenharmony_ci		return -EINVAL;
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci	memset(str,  0, sizeof(str));
10762306a36Sopenharmony_ci	ret = copy_from_user(str, buf, size);
10862306a36Sopenharmony_ci	if (ret)
10962306a36Sopenharmony_ci		return -EFAULT;
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci	ret = pm_runtime_get_sync(dev->dev);
11262306a36Sopenharmony_ci	if (ret < 0) {
11362306a36Sopenharmony_ci		pm_runtime_put_autosuspend(dev->dev);
11462306a36Sopenharmony_ci		return ret;
11562306a36Sopenharmony_ci	}
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci	if (size < 3)
11862306a36Sopenharmony_ci		sscanf(str, "%u ", &op);
11962306a36Sopenharmony_ci	else
12062306a36Sopenharmony_ci		sscanf(str, "%u %u", &op, &phy_id);
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci	switch (op) {
12362306a36Sopenharmony_ci	case 1:
12462306a36Sopenharmony_ci		mutex_lock(&psp->securedisplay_context.mutex);
12562306a36Sopenharmony_ci		psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
12662306a36Sopenharmony_ci			TA_SECUREDISPLAY_COMMAND__QUERY_TA);
12762306a36Sopenharmony_ci		ret = psp_securedisplay_invoke(psp, TA_SECUREDISPLAY_COMMAND__QUERY_TA);
12862306a36Sopenharmony_ci		if (!ret) {
12962306a36Sopenharmony_ci			if (securedisplay_cmd->status == TA_SECUREDISPLAY_STATUS__SUCCESS)
13062306a36Sopenharmony_ci				dev_info(adev->dev, "SECUREDISPLAY: query securedisplay TA ret is 0x%X\n",
13162306a36Sopenharmony_ci					securedisplay_cmd->securedisplay_out_message.query_ta.query_cmd_ret);
13262306a36Sopenharmony_ci			else
13362306a36Sopenharmony_ci				psp_securedisplay_parse_resp_status(psp, securedisplay_cmd->status);
13462306a36Sopenharmony_ci		}
13562306a36Sopenharmony_ci		mutex_unlock(&psp->securedisplay_context.mutex);
13662306a36Sopenharmony_ci		break;
13762306a36Sopenharmony_ci	case 2:
13862306a36Sopenharmony_ci		mutex_lock(&psp->securedisplay_context.mutex);
13962306a36Sopenharmony_ci		psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
14062306a36Sopenharmony_ci			TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC);
14162306a36Sopenharmony_ci		securedisplay_cmd->securedisplay_in_message.send_roi_crc.phy_id = phy_id;
14262306a36Sopenharmony_ci		ret = psp_securedisplay_invoke(psp, TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC);
14362306a36Sopenharmony_ci		if (!ret) {
14462306a36Sopenharmony_ci			if (securedisplay_cmd->status == TA_SECUREDISPLAY_STATUS__SUCCESS) {
14562306a36Sopenharmony_ci				dev_info(adev->dev, "SECUREDISPLAY: I2C buffer out put is: %*ph\n",
14662306a36Sopenharmony_ci					 TA_SECUREDISPLAY_I2C_BUFFER_SIZE,
14762306a36Sopenharmony_ci					 securedisplay_cmd->securedisplay_out_message.send_roi_crc.i2c_buf);
14862306a36Sopenharmony_ci			} else {
14962306a36Sopenharmony_ci				psp_securedisplay_parse_resp_status(psp, securedisplay_cmd->status);
15062306a36Sopenharmony_ci			}
15162306a36Sopenharmony_ci		}
15262306a36Sopenharmony_ci		mutex_unlock(&psp->securedisplay_context.mutex);
15362306a36Sopenharmony_ci		break;
15462306a36Sopenharmony_ci	default:
15562306a36Sopenharmony_ci		dev_err(adev->dev, "Invalid input: %s\n", str);
15662306a36Sopenharmony_ci	}
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci	pm_runtime_mark_last_busy(dev->dev);
15962306a36Sopenharmony_ci	pm_runtime_put_autosuspend(dev->dev);
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci	return size;
16262306a36Sopenharmony_ci}
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_cistatic const struct file_operations amdgpu_securedisplay_debugfs_ops = {
16562306a36Sopenharmony_ci	.owner = THIS_MODULE,
16662306a36Sopenharmony_ci	.read = NULL,
16762306a36Sopenharmony_ci	.write = amdgpu_securedisplay_debugfs_write,
16862306a36Sopenharmony_ci	.llseek = default_llseek
16962306a36Sopenharmony_ci};
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci#endif
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_civoid amdgpu_securedisplay_debugfs_init(struct amdgpu_device *adev)
17462306a36Sopenharmony_ci{
17562306a36Sopenharmony_ci#if defined(CONFIG_DEBUG_FS)
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci	if (!adev->psp.securedisplay_context.context.initialized)
17862306a36Sopenharmony_ci		return;
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci	debugfs_create_file("securedisplay_test", S_IWUSR, adev_to_drm(adev)->primary->debugfs_root,
18162306a36Sopenharmony_ci				adev, &amdgpu_securedisplay_debugfs_ops);
18262306a36Sopenharmony_ci#endif
18362306a36Sopenharmony_ci}
184