Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright 2021 Advanced Micro Devices, Inc.
0003  *
0004  * Permission is hereby granted, free of charge, to any person obtaining a
0005  * copy of this software and associated documentation files (the "Software"),
0006  * to deal in the Software without restriction, including without limitation
0007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0008  * and/or sell copies of the Software, and to permit persons to whom the
0009  * Software is furnished to do so, subject to the following conditions:
0010  *
0011  * The above copyright notice and this permission notice shall be included in
0012  * all copies or substantial portions of the Software.
0013  *
0014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0017  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
0018  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0020  * OTHER DEALINGS IN THE SOFTWARE.
0021  *
0022  *
0023  */
0024 #include <linux/debugfs.h>
0025 #include <linux/pm_runtime.h>
0026 
0027 #include "amdgpu.h"
0028 #include "amdgpu_securedisplay.h"
0029 
0030 /**
0031  * DOC: AMDGPU SECUREDISPLAY debugfs test interface
0032  *
0033  * how to use?
0034  * echo opcode <value> > <debugfs_dir>/dri/xxx/securedisplay_test
0035  * eg. echo 1 > <debugfs_dir>/dri/xxx/securedisplay_test
0036  * eg. echo 2 phy_id > <debugfs_dir>/dri/xxx/securedisplay_test
0037  *
0038  * opcode:
0039  * 1:Query whether TA is responding used only for validation pupose
0040  * 2: Send region of Interest and CRC value to I2C. (uint32)phy_id is
0041  * send to determine which DIO scratch register should be used to get
0042  * ROI and receive i2c_buf as the output.
0043  *
0044  * You can refer more detail from header file ta_securedisplay_if.h
0045  *
0046  */
0047 
0048 void psp_securedisplay_parse_resp_status(struct psp_context *psp,
0049     enum ta_securedisplay_status status)
0050 {
0051     switch (status) {
0052     case TA_SECUREDISPLAY_STATUS__SUCCESS:
0053         break;
0054     case TA_SECUREDISPLAY_STATUS__GENERIC_FAILURE:
0055         dev_err(psp->adev->dev, "Secure display: Generic Failure.");
0056         break;
0057     case TA_SECUREDISPLAY_STATUS__INVALID_PARAMETER:
0058         dev_err(psp->adev->dev, "Secure display: Invalid Parameter.");
0059         break;
0060     case TA_SECUREDISPLAY_STATUS__NULL_POINTER:
0061         dev_err(psp->adev->dev, "Secure display: Null Pointer.");
0062         break;
0063     case TA_SECUREDISPLAY_STATUS__I2C_WRITE_ERROR:
0064         dev_err(psp->adev->dev, "Secure display: Failed to write to I2C.");
0065         break;
0066     case TA_SECUREDISPLAY_STATUS__READ_DIO_SCRATCH_ERROR:
0067         dev_err(psp->adev->dev, "Secure display: Failed to Read DIO Scratch Register.");
0068         break;
0069     case TA_SECUREDISPLAY_STATUS__READ_CRC_ERROR:
0070         dev_err(psp->adev->dev, "Secure display: Failed to Read CRC");
0071         break;
0072     case TA_SECUREDISPLAY_STATUS__I2C_INIT_ERROR:
0073         dev_err(psp->adev->dev, "Secure display: Failed to initialize I2C.");
0074         break;
0075     default:
0076         dev_err(psp->adev->dev, "Secure display: Failed to parse status: %d\n", status);
0077     }
0078 }
0079 
0080 void psp_prep_securedisplay_cmd_buf(struct psp_context *psp, struct securedisplay_cmd **cmd,
0081     enum ta_securedisplay_command command_id)
0082 {
0083     *cmd = (struct securedisplay_cmd *)psp->securedisplay_context.context.mem_context.shared_buf;
0084     memset(*cmd, 0, sizeof(struct securedisplay_cmd));
0085     (*cmd)->status = TA_SECUREDISPLAY_STATUS__GENERIC_FAILURE;
0086     (*cmd)->cmd_id = command_id;
0087 }
0088 
0089 #if defined(CONFIG_DEBUG_FS)
0090 
0091 static ssize_t amdgpu_securedisplay_debugfs_write(struct file *f, const char __user *buf,
0092         size_t size, loff_t *pos)
0093 {
0094     struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
0095     struct psp_context *psp = &adev->psp;
0096     struct securedisplay_cmd *securedisplay_cmd;
0097     struct drm_device *dev = adev_to_drm(adev);
0098     uint32_t phy_id;
0099     uint32_t op;
0100     char str[64];
0101     int ret;
0102 
0103     if (*pos || size > sizeof(str) - 1)
0104         return -EINVAL;
0105 
0106     memset(str,  0, sizeof(str));
0107     ret = copy_from_user(str, buf, size);
0108     if (ret)
0109         return -EFAULT;
0110 
0111     ret = pm_runtime_get_sync(dev->dev);
0112     if (ret < 0) {
0113         pm_runtime_put_autosuspend(dev->dev);
0114         return ret;
0115     }
0116 
0117     if (size < 3)
0118         sscanf(str, "%u ", &op);
0119     else
0120         sscanf(str, "%u %u", &op, &phy_id);
0121 
0122     switch (op) {
0123     case 1:
0124         psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
0125             TA_SECUREDISPLAY_COMMAND__QUERY_TA);
0126         ret = psp_securedisplay_invoke(psp, TA_SECUREDISPLAY_COMMAND__QUERY_TA);
0127         if (!ret) {
0128             if (securedisplay_cmd->status == TA_SECUREDISPLAY_STATUS__SUCCESS)
0129                 dev_info(adev->dev, "SECUREDISPLAY: query securedisplay TA ret is 0x%X\n",
0130                     securedisplay_cmd->securedisplay_out_message.query_ta.query_cmd_ret);
0131             else
0132                 psp_securedisplay_parse_resp_status(psp, securedisplay_cmd->status);
0133         }
0134         break;
0135     case 2:
0136         psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
0137             TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC);
0138         securedisplay_cmd->securedisplay_in_message.send_roi_crc.phy_id = phy_id;
0139         ret = psp_securedisplay_invoke(psp, TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC);
0140         if (!ret) {
0141             if (securedisplay_cmd->status == TA_SECUREDISPLAY_STATUS__SUCCESS) {
0142                 dev_info(adev->dev, "SECUREDISPLAY: I2C buffer out put is: %*ph\n",
0143                      TA_SECUREDISPLAY_I2C_BUFFER_SIZE,
0144                      securedisplay_cmd->securedisplay_out_message.send_roi_crc.i2c_buf);
0145             } else {
0146                 psp_securedisplay_parse_resp_status(psp, securedisplay_cmd->status);
0147             }
0148         }
0149         break;
0150     default:
0151         dev_err(adev->dev, "Invalid input: %s\n", str);
0152     }
0153 
0154     pm_runtime_mark_last_busy(dev->dev);
0155     pm_runtime_put_autosuspend(dev->dev);
0156 
0157     return size;
0158 }
0159 
0160 static const struct file_operations amdgpu_securedisplay_debugfs_ops = {
0161     .owner = THIS_MODULE,
0162     .read = NULL,
0163     .write = amdgpu_securedisplay_debugfs_write,
0164     .llseek = default_llseek
0165 };
0166 
0167 #endif
0168 
0169 void amdgpu_securedisplay_debugfs_init(struct amdgpu_device *adev)
0170 {
0171 #if defined(CONFIG_DEBUG_FS)
0172 
0173     if (!adev->psp.securedisplay_context.context.initialized)
0174         return;
0175 
0176     debugfs_create_file("securedisplay_test", S_IWUSR, adev_to_drm(adev)->primary->debugfs_root,
0177                 adev, &amdgpu_securedisplay_debugfs_ops);
0178 #endif
0179 }