Thomas Gleixner | 08dbd0f | 2019-05-29 07:12:41 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Elliot Berman | e0aa153 | 2020-01-07 13:04:10 -0800 | [diff] [blame] | 2 | /* Copyright (c) 2010,2015,2019 The Linux Foundation. All rights reserved. |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 3 | * Copyright (C) 2015 Linaro Ltd. |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/slab.h> |
| 7 | #include <linux/io.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/mutex.h> |
| 10 | #include <linux/errno.h> |
| 11 | #include <linux/err.h> |
| 12 | #include <linux/qcom_scm.h> |
Elliot Berman | 0224898 | 2020-01-07 13:04:18 -0800 | [diff] [blame] | 13 | #include <linux/arm-smccc.h> |
Andy Gross | 16e5946 | 2016-06-03 18:25:25 -0500 | [diff] [blame] | 14 | #include <linux/dma-mapping.h> |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 15 | |
| 16 | #include "qcom_scm.h" |
| 17 | |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 18 | static DEFINE_MUTEX(qcom_scm_lock); |
| 19 | |
Elliot Berman | efd2b15 | 2020-01-07 13:04:20 -0800 | [diff] [blame] | 20 | |
Elliot Berman | 590e928 | 2020-01-07 13:04:21 -0800 | [diff] [blame] | 21 | /** |
| 22 | * struct arm_smccc_args |
| 23 | * @args: The array of values used in registers in smc instruction |
| 24 | */ |
| 25 | struct arm_smccc_args { |
| 26 | unsigned long args[8]; |
| 27 | }; |
| 28 | |
Elliot Berman | fd62c30 | 2020-01-07 13:04:19 -0800 | [diff] [blame] | 29 | |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 30 | /** |
Elliot Berman | e0aa153 | 2020-01-07 13:04:10 -0800 | [diff] [blame] | 31 | * struct scm_legacy_command - one SCM command buffer |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 32 | * @len: total available memory for command and response |
| 33 | * @buf_offset: start of command buffer |
| 34 | * @resp_hdr_offset: start of response buffer |
| 35 | * @id: command to be executed |
Elliot Berman | e0aa153 | 2020-01-07 13:04:10 -0800 | [diff] [blame] | 36 | * @buf: buffer returned from scm_legacy_get_command_buffer() |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 37 | * |
| 38 | * An SCM command is laid out in memory as follows: |
| 39 | * |
Elliot Berman | e0aa153 | 2020-01-07 13:04:10 -0800 | [diff] [blame] | 40 | * ------------------- <--- struct scm_legacy_command |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 41 | * | command header | |
Elliot Berman | e0aa153 | 2020-01-07 13:04:10 -0800 | [diff] [blame] | 42 | * ------------------- <--- scm_legacy_get_command_buffer() |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 43 | * | command buffer | |
Elliot Berman | e0aa153 | 2020-01-07 13:04:10 -0800 | [diff] [blame] | 44 | * ------------------- <--- struct scm_legacy_response and |
| 45 | * | response header | scm_legacy_command_to_response() |
| 46 | * ------------------- <--- scm_legacy_get_response_buffer() |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 47 | * | response buffer | |
| 48 | * ------------------- |
| 49 | * |
| 50 | * There can be arbitrary padding between the headers and buffers so |
Elliot Berman | e0aa153 | 2020-01-07 13:04:10 -0800 | [diff] [blame] | 51 | * you should always use the appropriate scm_legacy_get_*_buffer() routines |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 52 | * to access the buffers in a safe manner. |
| 53 | */ |
Elliot Berman | e0aa153 | 2020-01-07 13:04:10 -0800 | [diff] [blame] | 54 | struct scm_legacy_command { |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 55 | __le32 len; |
| 56 | __le32 buf_offset; |
| 57 | __le32 resp_hdr_offset; |
| 58 | __le32 id; |
| 59 | __le32 buf[0]; |
| 60 | }; |
| 61 | |
| 62 | /** |
Elliot Berman | e0aa153 | 2020-01-07 13:04:10 -0800 | [diff] [blame] | 63 | * struct scm_legacy_response - one SCM response buffer |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 64 | * @len: total available memory for response |
Elliot Berman | e0aa153 | 2020-01-07 13:04:10 -0800 | [diff] [blame] | 65 | * @buf_offset: start of response data relative to start of scm_legacy_response |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 66 | * @is_complete: indicates if the command has finished processing |
| 67 | */ |
Elliot Berman | e0aa153 | 2020-01-07 13:04:10 -0800 | [diff] [blame] | 68 | struct scm_legacy_response { |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 69 | __le32 len; |
| 70 | __le32 buf_offset; |
| 71 | __le32 is_complete; |
| 72 | }; |
| 73 | |
| 74 | /** |
Elliot Berman | e0aa153 | 2020-01-07 13:04:10 -0800 | [diff] [blame] | 75 | * scm_legacy_command_to_response() - Get a pointer to a scm_legacy_response |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 76 | * @cmd: command |
| 77 | * |
| 78 | * Returns a pointer to a response for a command. |
| 79 | */ |
Elliot Berman | e0aa153 | 2020-01-07 13:04:10 -0800 | [diff] [blame] | 80 | static inline struct scm_legacy_response *scm_legacy_command_to_response( |
| 81 | const struct scm_legacy_command *cmd) |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 82 | { |
| 83 | return (void *)cmd + le32_to_cpu(cmd->resp_hdr_offset); |
| 84 | } |
| 85 | |
| 86 | /** |
Elliot Berman | e0aa153 | 2020-01-07 13:04:10 -0800 | [diff] [blame] | 87 | * scm_legacy_get_command_buffer() - Get a pointer to a command buffer |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 88 | * @cmd: command |
| 89 | * |
| 90 | * Returns a pointer to the command buffer of a command. |
| 91 | */ |
Elliot Berman | e0aa153 | 2020-01-07 13:04:10 -0800 | [diff] [blame] | 92 | static inline void *scm_legacy_get_command_buffer( |
| 93 | const struct scm_legacy_command *cmd) |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 94 | { |
| 95 | return (void *)cmd->buf; |
| 96 | } |
| 97 | |
| 98 | /** |
Elliot Berman | e0aa153 | 2020-01-07 13:04:10 -0800 | [diff] [blame] | 99 | * scm_legacy_get_response_buffer() - Get a pointer to a response buffer |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 100 | * @rsp: response |
| 101 | * |
| 102 | * Returns a pointer to a response buffer of a response. |
| 103 | */ |
Elliot Berman | e0aa153 | 2020-01-07 13:04:10 -0800 | [diff] [blame] | 104 | static inline void *scm_legacy_get_response_buffer( |
| 105 | const struct scm_legacy_response *rsp) |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 106 | { |
| 107 | return (void *)rsp + le32_to_cpu(rsp->buf_offset); |
| 108 | } |
| 109 | |
Elliot Berman | 590e928 | 2020-01-07 13:04:21 -0800 | [diff] [blame] | 110 | static void __scm_legacy_do(const struct arm_smccc_args *smc, |
| 111 | struct arm_smccc_res *res) |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 112 | { |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 113 | do { |
Elliot Berman | 590e928 | 2020-01-07 13:04:21 -0800 | [diff] [blame] | 114 | arm_smccc_smc(smc->args[0], smc->args[1], smc->args[2], |
| 115 | smc->args[3], smc->args[4], smc->args[5], |
| 116 | smc->args[6], smc->args[7], res); |
| 117 | } while (res->a0 == QCOM_SCM_INTERRUPTED); |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 118 | } |
| 119 | |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 120 | /** |
Elliot Berman | efd2b15 | 2020-01-07 13:04:20 -0800 | [diff] [blame] | 121 | * qcom_scm_call() - Sends a command to the SCM and waits for the command to |
| 122 | * finish processing. |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 123 | * |
| 124 | * A note on cache maintenance: |
| 125 | * Note that any buffers that are expected to be accessed by the secure world |
| 126 | * must be flushed before invoking qcom_scm_call and invalidated in the cache |
| 127 | * immediately after qcom_scm_call returns. Cache maintenance on the command |
| 128 | * and response buffers is taken care of by qcom_scm_call; however, callers are |
| 129 | * responsible for any other cached buffers passed over to the secure world. |
| 130 | */ |
Elliot Berman | 9a434cee | 2020-01-07 13:04:26 -0800 | [diff] [blame] | 131 | int scm_legacy_call(struct device *dev, const struct qcom_scm_desc *desc, |
| 132 | struct qcom_scm_res *res) |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 133 | { |
Elliot Berman | efd2b15 | 2020-01-07 13:04:20 -0800 | [diff] [blame] | 134 | u8 arglen = desc->arginfo & 0xf; |
Elliot Berman | 590e928 | 2020-01-07 13:04:21 -0800 | [diff] [blame] | 135 | int ret = 0, context_id; |
Elliot Berman | efd2b15 | 2020-01-07 13:04:20 -0800 | [diff] [blame] | 136 | unsigned int i; |
Elliot Berman | e0aa153 | 2020-01-07 13:04:10 -0800 | [diff] [blame] | 137 | struct scm_legacy_command *cmd; |
| 138 | struct scm_legacy_response *rsp; |
Elliot Berman | 590e928 | 2020-01-07 13:04:21 -0800 | [diff] [blame] | 139 | struct arm_smccc_args smc = {0}; |
| 140 | struct arm_smccc_res smc_res; |
Elliot Berman | efd2b15 | 2020-01-07 13:04:20 -0800 | [diff] [blame] | 141 | const size_t cmd_len = arglen * sizeof(__le32); |
| 142 | const size_t resp_len = MAX_QCOM_SCM_RETS * sizeof(__le32); |
Andy Gross | 16e5946 | 2016-06-03 18:25:25 -0500 | [diff] [blame] | 143 | size_t alloc_len = sizeof(*cmd) + cmd_len + sizeof(*rsp) + resp_len; |
| 144 | dma_addr_t cmd_phys; |
Elliot Berman | efd2b15 | 2020-01-07 13:04:20 -0800 | [diff] [blame] | 145 | __le32 *arg_buf; |
| 146 | const __le32 *res_buf; |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 147 | |
Andy Gross | 16e5946 | 2016-06-03 18:25:25 -0500 | [diff] [blame] | 148 | cmd = kzalloc(PAGE_ALIGN(alloc_len), GFP_KERNEL); |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 149 | if (!cmd) |
| 150 | return -ENOMEM; |
| 151 | |
Andy Gross | 16e5946 | 2016-06-03 18:25:25 -0500 | [diff] [blame] | 152 | cmd->len = cpu_to_le32(alloc_len); |
| 153 | cmd->buf_offset = cpu_to_le32(sizeof(*cmd)); |
| 154 | cmd->resp_hdr_offset = cpu_to_le32(sizeof(*cmd) + cmd_len); |
Elliot Berman | efd2b15 | 2020-01-07 13:04:20 -0800 | [diff] [blame] | 155 | cmd->id = cpu_to_le32(SCM_LEGACY_FNID(desc->svc, desc->cmd)); |
Andy Gross | 16e5946 | 2016-06-03 18:25:25 -0500 | [diff] [blame] | 156 | |
Elliot Berman | efd2b15 | 2020-01-07 13:04:20 -0800 | [diff] [blame] | 157 | arg_buf = scm_legacy_get_command_buffer(cmd); |
| 158 | for (i = 0; i < arglen; i++) |
| 159 | arg_buf[i] = cpu_to_le32(desc->args[i]); |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 160 | |
Elliot Berman | e0aa153 | 2020-01-07 13:04:10 -0800 | [diff] [blame] | 161 | rsp = scm_legacy_command_to_response(cmd); |
Andy Gross | 16e5946 | 2016-06-03 18:25:25 -0500 | [diff] [blame] | 162 | |
| 163 | cmd_phys = dma_map_single(dev, cmd, alloc_len, DMA_TO_DEVICE); |
| 164 | if (dma_mapping_error(dev, cmd_phys)) { |
| 165 | kfree(cmd); |
| 166 | return -ENOMEM; |
| 167 | } |
| 168 | |
Elliot Berman | 590e928 | 2020-01-07 13:04:21 -0800 | [diff] [blame] | 169 | smc.args[0] = 1; |
| 170 | smc.args[1] = (unsigned long)&context_id; |
| 171 | smc.args[2] = cmd_phys; |
| 172 | |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 173 | mutex_lock(&qcom_scm_lock); |
Elliot Berman | 590e928 | 2020-01-07 13:04:21 -0800 | [diff] [blame] | 174 | __scm_legacy_do(&smc, &smc_res); |
| 175 | if (smc_res.a0) |
| 176 | ret = qcom_scm_remap_error(smc_res.a0); |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 177 | mutex_unlock(&qcom_scm_lock); |
| 178 | if (ret) |
| 179 | goto out; |
| 180 | |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 181 | do { |
Andy Gross | 16e5946 | 2016-06-03 18:25:25 -0500 | [diff] [blame] | 182 | dma_sync_single_for_cpu(dev, cmd_phys + sizeof(*cmd) + cmd_len, |
| 183 | sizeof(*rsp), DMA_FROM_DEVICE); |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 184 | } while (!rsp->is_complete); |
| 185 | |
Elliot Berman | efd2b15 | 2020-01-07 13:04:20 -0800 | [diff] [blame] | 186 | dma_sync_single_for_cpu(dev, cmd_phys + sizeof(*cmd) + cmd_len + |
| 187 | le32_to_cpu(rsp->buf_offset), |
| 188 | resp_len, DMA_FROM_DEVICE); |
| 189 | |
| 190 | if (res) { |
| 191 | res_buf = scm_legacy_get_response_buffer(rsp); |
| 192 | for (i = 0; i < MAX_QCOM_SCM_RETS; i++) |
| 193 | res->result[i] = le32_to_cpu(res_buf[i]); |
Andy Gross | 16e5946 | 2016-06-03 18:25:25 -0500 | [diff] [blame] | 194 | } |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 195 | out: |
Andy Gross | 16e5946 | 2016-06-03 18:25:25 -0500 | [diff] [blame] | 196 | dma_unmap_single(dev, cmd_phys, alloc_len, DMA_TO_DEVICE); |
| 197 | kfree(cmd); |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 198 | return ret; |
| 199 | } |
| 200 | |
Elliot Berman | 8452848 | 2020-01-07 13:04:22 -0800 | [diff] [blame] | 201 | #define SCM_LEGACY_ATOMIC_N_REG_ARGS 5 |
| 202 | #define SCM_LEGACY_ATOMIC_FIRST_REG_IDX 2 |
Elliot Berman | e0aa153 | 2020-01-07 13:04:10 -0800 | [diff] [blame] | 203 | #define SCM_LEGACY_CLASS_REGISTER (0x2 << 8) |
| 204 | #define SCM_LEGACY_MASK_IRQS BIT(5) |
| 205 | #define SCM_LEGACY_ATOMIC_ID(svc, cmd, n) \ |
Elliot Berman | fd62c30 | 2020-01-07 13:04:19 -0800 | [diff] [blame] | 206 | ((SCM_LEGACY_FNID(svc, cmd) << 12) | \ |
Elliot Berman | e0aa153 | 2020-01-07 13:04:10 -0800 | [diff] [blame] | 207 | SCM_LEGACY_CLASS_REGISTER | \ |
| 208 | SCM_LEGACY_MASK_IRQS | \ |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 209 | (n & 0xf)) |
| 210 | |
| 211 | /** |
Elliot Berman | 8452848 | 2020-01-07 13:04:22 -0800 | [diff] [blame] | 212 | * qcom_scm_call_atomic() - Send an atomic SCM command with up to 5 arguments |
| 213 | * and 3 return values |
| 214 | * @desc: SCM call descriptor containing arguments |
| 215 | * @res: SCM call return values |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 216 | * |
| 217 | * This shall only be used with commands that are guaranteed to be |
| 218 | * uninterruptable, atomic and SMP safe. |
| 219 | */ |
Elliot Berman | 9a434cee | 2020-01-07 13:04:26 -0800 | [diff] [blame] | 220 | int scm_legacy_call_atomic(struct device *unused, |
| 221 | const struct qcom_scm_desc *desc, |
| 222 | struct qcom_scm_res *res) |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 223 | { |
| 224 | int context_id; |
Elliot Berman | 8452848 | 2020-01-07 13:04:22 -0800 | [diff] [blame] | 225 | struct arm_smccc_res smc_res; |
| 226 | size_t arglen = desc->arginfo & 0xf; |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 227 | |
Elliot Berman | 8452848 | 2020-01-07 13:04:22 -0800 | [diff] [blame] | 228 | BUG_ON(arglen > SCM_LEGACY_ATOMIC_N_REG_ARGS); |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 229 | |
Elliot Berman | 8452848 | 2020-01-07 13:04:22 -0800 | [diff] [blame] | 230 | arm_smccc_smc(SCM_LEGACY_ATOMIC_ID(desc->svc, desc->cmd, arglen), |
| 231 | (unsigned long)&context_id, |
| 232 | desc->args[0], desc->args[1], desc->args[2], |
| 233 | desc->args[3], desc->args[4], 0, &smc_res); |
Kumar Gala | b6a1dfb | 2015-03-11 16:28:10 -0500 | [diff] [blame] | 234 | |
Elliot Berman | 8452848 | 2020-01-07 13:04:22 -0800 | [diff] [blame] | 235 | if (res) { |
| 236 | res->result[0] = smc_res.a1; |
| 237 | res->result[1] = smc_res.a2; |
| 238 | res->result[2] = smc_res.a3; |
| 239 | } |
Andy Gross | 13e7774 | 2016-06-03 18:25:23 -0500 | [diff] [blame] | 240 | |
Elliot Berman | 8452848 | 2020-01-07 13:04:22 -0800 | [diff] [blame] | 241 | return smc_res.a0; |
Andy Gross | 13e7774 | 2016-06-03 18:25:23 -0500 | [diff] [blame] | 242 | } |