| 1 | /* |
| 2 | * Copyright 2021 Advanced Micro Devices, Inc. |
| 3 | * |
| 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: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 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. |
| 21 | * |
| 22 | * |
| 23 | */ |
| 24 | #include <linux/debugfs.h> |
| 25 | #include <linux/pm_runtime.h> |
| 26 | |
| 27 | #include "amdgpu.h" |
| 28 | #include "amdgpu_securedisplay.h" |
| 29 | |
| 30 | /** |
| 31 | * DOC: AMDGPU SECUREDISPLAY debugfs test interface |
| 32 | * |
| 33 | * how to use? |
| 34 | * echo opcode <value> > <debugfs_dir>/dri/xxx/securedisplay_test |
| 35 | * eg. echo 1 > <debugfs_dir>/dri/xxx/securedisplay_test |
| 36 | * eg. echo 2 phy_id > <debugfs_dir>/dri/xxx/securedisplay_test |
| 37 | * |
| 38 | * opcode: |
| 39 | * 1:Query whether TA is responding used only for validation pupose |
| 40 | * 2: Send region of Interest and CRC value to I2C. (uint32)phy_id is |
| 41 | * send to determine which DIO scratch register should be used to get |
| 42 | * ROI and receive i2c_buf as the output. |
| 43 | * |
| 44 | * You can refer more detail from header file ta_securedisplay_if.h |
| 45 | * |
| 46 | */ |
| 47 | |
| 48 | void psp_securedisplay_parse_resp_status(struct psp_context *psp, |
| 49 | enum ta_securedisplay_status status) |
| 50 | { |
| 51 | switch (status) { |
| 52 | case TA_SECUREDISPLAY_STATUS__SUCCESS: |
| 53 | break; |
| 54 | case TA_SECUREDISPLAY_STATUS__GENERIC_FAILURE: |
| 55 | dev_err(psp->adev->dev, "Secure display: Generic Failure." ); |
| 56 | break; |
| 57 | case TA_SECUREDISPLAY_STATUS__INVALID_PARAMETER: |
| 58 | dev_err(psp->adev->dev, "Secure display: Invalid Parameter." ); |
| 59 | break; |
| 60 | case TA_SECUREDISPLAY_STATUS__NULL_POINTER: |
| 61 | dev_err(psp->adev->dev, "Secure display: Null Pointer." ); |
| 62 | break; |
| 63 | case TA_SECUREDISPLAY_STATUS__I2C_WRITE_ERROR: |
| 64 | dev_err(psp->adev->dev, "Secure display: Failed to write to I2C." ); |
| 65 | break; |
| 66 | case TA_SECUREDISPLAY_STATUS__READ_DIO_SCRATCH_ERROR: |
| 67 | dev_err(psp->adev->dev, "Secure display: Failed to Read DIO Scratch Register." ); |
| 68 | break; |
| 69 | case TA_SECUREDISPLAY_STATUS__READ_CRC_ERROR: |
| 70 | dev_err(psp->adev->dev, "Secure display: Failed to Read CRC" ); |
| 71 | break; |
| 72 | case TA_SECUREDISPLAY_STATUS__I2C_INIT_ERROR: |
| 73 | dev_err(psp->adev->dev, "Secure display: Failed to initialize I2C." ); |
| 74 | break; |
| 75 | default: |
| 76 | dev_err(psp->adev->dev, "Secure display: Failed to parse status: %d\n" , status); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void psp_prep_securedisplay_cmd_buf(struct psp_context *psp, struct ta_securedisplay_cmd **cmd, |
| 81 | enum ta_securedisplay_command command_id) |
| 82 | { |
| 83 | *cmd = (struct ta_securedisplay_cmd *)psp->securedisplay_context.context.mem_context.shared_buf; |
| 84 | memset(*cmd, 0, sizeof(struct ta_securedisplay_cmd)); |
| 85 | (*cmd)->status = TA_SECUREDISPLAY_STATUS__GENERIC_FAILURE; |
| 86 | (*cmd)->cmd_id = command_id; |
| 87 | } |
| 88 | |
| 89 | #if defined(CONFIG_DEBUG_FS) |
| 90 | |
| 91 | static ssize_t amdgpu_securedisplay_debugfs_write(struct file *f, const char __user *buf, |
| 92 | size_t size, loff_t *pos) |
| 93 | { |
| 94 | struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private; |
| 95 | struct psp_context *psp = &adev->psp; |
| 96 | struct ta_securedisplay_cmd *securedisplay_cmd; |
| 97 | struct drm_device *dev = adev_to_drm(adev); |
| 98 | uint32_t phy_id; |
| 99 | uint32_t op; |
| 100 | char str[64]; |
| 101 | int ret; |
| 102 | |
| 103 | if (*pos || size > sizeof(str) - 1) |
| 104 | return -EINVAL; |
| 105 | |
| 106 | memset(str, 0, sizeof(str)); |
| 107 | ret = copy_from_user(to: str, from: buf, n: size); |
| 108 | if (ret) |
| 109 | return -EFAULT; |
| 110 | |
| 111 | ret = pm_runtime_get_sync(dev: dev->dev); |
| 112 | if (ret < 0) { |
| 113 | pm_runtime_put_autosuspend(dev: dev->dev); |
| 114 | return ret; |
| 115 | } |
| 116 | |
| 117 | if (size < 3) |
| 118 | sscanf(str, "%u " , &op); |
| 119 | else |
| 120 | sscanf(str, "%u %u" , &op, &phy_id); |
| 121 | |
| 122 | switch (op) { |
| 123 | case 1: |
| 124 | mutex_lock(&psp->securedisplay_context.mutex); |
| 125 | psp_prep_securedisplay_cmd_buf(psp, cmd: &securedisplay_cmd, |
| 126 | command_id: TA_SECUREDISPLAY_COMMAND__QUERY_TA); |
| 127 | ret = psp_securedisplay_invoke(psp, ta_cmd_id: TA_SECUREDISPLAY_COMMAND__QUERY_TA); |
| 128 | if (!ret) { |
| 129 | if (securedisplay_cmd->status == TA_SECUREDISPLAY_STATUS__SUCCESS) |
| 130 | dev_info(adev->dev, "SECUREDISPLAY: query securedisplay TA ret is 0x%X\n" , |
| 131 | securedisplay_cmd->securedisplay_out_message.query_ta.query_cmd_ret); |
| 132 | else |
| 133 | psp_securedisplay_parse_resp_status(psp, status: securedisplay_cmd->status); |
| 134 | } |
| 135 | mutex_unlock(lock: &psp->securedisplay_context.mutex); |
| 136 | break; |
| 137 | case 2: |
| 138 | if (size < 3 || phy_id >= TA_SECUREDISPLAY_MAX_PHY) { |
| 139 | dev_err(adev->dev, "Invalid input: %s\n" , str); |
| 140 | return -EINVAL; |
| 141 | } |
| 142 | mutex_lock(&psp->securedisplay_context.mutex); |
| 143 | psp_prep_securedisplay_cmd_buf(psp, cmd: &securedisplay_cmd, |
| 144 | command_id: TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC); |
| 145 | securedisplay_cmd->securedisplay_in_message.send_roi_crc.phy_id = phy_id; |
| 146 | ret = psp_securedisplay_invoke(psp, ta_cmd_id: TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC); |
| 147 | if (!ret) { |
| 148 | if (securedisplay_cmd->status == TA_SECUREDISPLAY_STATUS__SUCCESS) { |
| 149 | dev_info(adev->dev, "SECUREDISPLAY: I2C buffer out put is: %*ph\n" , |
| 150 | TA_SECUREDISPLAY_I2C_BUFFER_SIZE, |
| 151 | securedisplay_cmd->securedisplay_out_message.send_roi_crc.i2c_buf); |
| 152 | } else { |
| 153 | psp_securedisplay_parse_resp_status(psp, status: securedisplay_cmd->status); |
| 154 | } |
| 155 | } |
| 156 | mutex_unlock(lock: &psp->securedisplay_context.mutex); |
| 157 | break; |
| 158 | default: |
| 159 | dev_err(adev->dev, "Invalid input: %s\n" , str); |
| 160 | } |
| 161 | |
| 162 | pm_runtime_put_autosuspend(dev: dev->dev); |
| 163 | |
| 164 | return size; |
| 165 | } |
| 166 | |
| 167 | static const struct file_operations amdgpu_securedisplay_debugfs_ops = { |
| 168 | .owner = THIS_MODULE, |
| 169 | .read = NULL, |
| 170 | .write = amdgpu_securedisplay_debugfs_write, |
| 171 | .llseek = default_llseek |
| 172 | }; |
| 173 | |
| 174 | #endif |
| 175 | |
| 176 | void amdgpu_securedisplay_debugfs_init(struct amdgpu_device *adev) |
| 177 | { |
| 178 | #if defined(CONFIG_DEBUG_FS) |
| 179 | |
| 180 | if (!adev->psp.securedisplay_context.context.initialized) |
| 181 | return; |
| 182 | |
| 183 | debugfs_create_file("securedisplay_test" , S_IWUSR, adev_to_drm(adev)->primary->debugfs_root, |
| 184 | adev, &amdgpu_securedisplay_debugfs_ops); |
| 185 | #endif |
| 186 | } |
| 187 | |