Stephen Boyd | 2a1eb58 | 2010-08-27 10:01:23 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2010, Code Aurora Forum. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License |
| 13 | * along with this program; if not, write to the Free Software |
| 14 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 15 | * 02110-1301, USA. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/io.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/mutex.h> |
| 22 | #include <linux/errno.h> |
| 23 | #include <linux/err.h> |
| 24 | |
Stephen Boyd | f76c691 | 2014-08-04 18:31:43 -0700 | [diff] [blame^] | 25 | #include <asm/outercache.h> |
Stephen Boyd | 2a1eb58 | 2010-08-27 10:01:23 -0700 | [diff] [blame] | 26 | #include <asm/cacheflush.h> |
| 27 | |
| 28 | #include "scm.h" |
| 29 | |
| 30 | /* Cache line size for msm8x60 */ |
| 31 | #define CACHELINESIZE 32 |
| 32 | |
| 33 | #define SCM_ENOMEM -5 |
| 34 | #define SCM_EOPNOTSUPP -4 |
| 35 | #define SCM_EINVAL_ADDR -3 |
| 36 | #define SCM_EINVAL_ARG -2 |
| 37 | #define SCM_ERROR -1 |
| 38 | #define SCM_INTERRUPTED 1 |
| 39 | |
| 40 | static DEFINE_MUTEX(scm_lock); |
| 41 | |
| 42 | /** |
| 43 | * struct scm_command - one SCM command buffer |
| 44 | * @len: total available memory for command and response |
| 45 | * @buf_offset: start of command buffer |
| 46 | * @resp_hdr_offset: start of response buffer |
| 47 | * @id: command to be executed |
| 48 | * @buf: buffer returned from scm_get_command_buffer() |
| 49 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 50 | * An SCM command is laid out in memory as follows: |
Stephen Boyd | 2a1eb58 | 2010-08-27 10:01:23 -0700 | [diff] [blame] | 51 | * |
| 52 | * ------------------- <--- struct scm_command |
| 53 | * | command header | |
| 54 | * ------------------- <--- scm_get_command_buffer() |
| 55 | * | command buffer | |
| 56 | * ------------------- <--- struct scm_response and |
| 57 | * | response header | scm_command_to_response() |
| 58 | * ------------------- <--- scm_get_response_buffer() |
| 59 | * | response buffer | |
| 60 | * ------------------- |
| 61 | * |
| 62 | * There can be arbitrary padding between the headers and buffers so |
| 63 | * you should always use the appropriate scm_get_*_buffer() routines |
| 64 | * to access the buffers in a safe manner. |
| 65 | */ |
| 66 | struct scm_command { |
| 67 | u32 len; |
| 68 | u32 buf_offset; |
| 69 | u32 resp_hdr_offset; |
| 70 | u32 id; |
| 71 | u32 buf[0]; |
| 72 | }; |
| 73 | |
| 74 | /** |
| 75 | * struct scm_response - one SCM response buffer |
| 76 | * @len: total available memory for response |
| 77 | * @buf_offset: start of response data relative to start of scm_response |
| 78 | * @is_complete: indicates if the command has finished processing |
| 79 | */ |
| 80 | struct scm_response { |
| 81 | u32 len; |
| 82 | u32 buf_offset; |
| 83 | u32 is_complete; |
| 84 | }; |
| 85 | |
| 86 | /** |
| 87 | * alloc_scm_command() - Allocate an SCM command |
| 88 | * @cmd_size: size of the command buffer |
| 89 | * @resp_size: size of the response buffer |
| 90 | * |
| 91 | * Allocate an SCM command, including enough room for the command |
| 92 | * and response headers as well as the command and response buffers. |
| 93 | * |
| 94 | * Returns a valid &scm_command on success or %NULL if the allocation fails. |
| 95 | */ |
| 96 | static struct scm_command *alloc_scm_command(size_t cmd_size, size_t resp_size) |
| 97 | { |
| 98 | struct scm_command *cmd; |
| 99 | size_t len = sizeof(*cmd) + sizeof(struct scm_response) + cmd_size + |
| 100 | resp_size; |
| 101 | |
| 102 | cmd = kzalloc(PAGE_ALIGN(len), GFP_KERNEL); |
| 103 | if (cmd) { |
| 104 | cmd->len = len; |
| 105 | cmd->buf_offset = offsetof(struct scm_command, buf); |
| 106 | cmd->resp_hdr_offset = cmd->buf_offset + cmd_size; |
| 107 | } |
| 108 | return cmd; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * free_scm_command() - Free an SCM command |
| 113 | * @cmd: command to free |
| 114 | * |
| 115 | * Free an SCM command. |
| 116 | */ |
| 117 | static inline void free_scm_command(struct scm_command *cmd) |
| 118 | { |
| 119 | kfree(cmd); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * scm_command_to_response() - Get a pointer to a scm_response |
| 124 | * @cmd: command |
| 125 | * |
| 126 | * Returns a pointer to a response for a command. |
| 127 | */ |
| 128 | static inline struct scm_response *scm_command_to_response( |
| 129 | const struct scm_command *cmd) |
| 130 | { |
| 131 | return (void *)cmd + cmd->resp_hdr_offset; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * scm_get_command_buffer() - Get a pointer to a command buffer |
| 136 | * @cmd: command |
| 137 | * |
| 138 | * Returns a pointer to the command buffer of a command. |
| 139 | */ |
| 140 | static inline void *scm_get_command_buffer(const struct scm_command *cmd) |
| 141 | { |
| 142 | return (void *)cmd->buf; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * scm_get_response_buffer() - Get a pointer to a response buffer |
| 147 | * @rsp: response |
| 148 | * |
| 149 | * Returns a pointer to a response buffer of a response. |
| 150 | */ |
| 151 | static inline void *scm_get_response_buffer(const struct scm_response *rsp) |
| 152 | { |
| 153 | return (void *)rsp + rsp->buf_offset; |
| 154 | } |
| 155 | |
| 156 | static int scm_remap_error(int err) |
| 157 | { |
| 158 | switch (err) { |
| 159 | case SCM_ERROR: |
| 160 | return -EIO; |
| 161 | case SCM_EINVAL_ADDR: |
| 162 | case SCM_EINVAL_ARG: |
| 163 | return -EINVAL; |
| 164 | case SCM_EOPNOTSUPP: |
| 165 | return -EOPNOTSUPP; |
| 166 | case SCM_ENOMEM: |
| 167 | return -ENOMEM; |
| 168 | } |
| 169 | return -EINVAL; |
| 170 | } |
| 171 | |
| 172 | static u32 smc(u32 cmd_addr) |
| 173 | { |
| 174 | int context_id; |
| 175 | register u32 r0 asm("r0") = 1; |
| 176 | register u32 r1 asm("r1") = (u32)&context_id; |
| 177 | register u32 r2 asm("r2") = cmd_addr; |
Stephen Boyd | 8e76a80 | 2011-02-24 10:44:44 -0800 | [diff] [blame] | 178 | do { |
| 179 | asm volatile( |
| 180 | __asmeq("%0", "r0") |
| 181 | __asmeq("%1", "r0") |
| 182 | __asmeq("%2", "r1") |
| 183 | __asmeq("%3", "r2") |
Marc Zyngier | eca55f4 | 2011-11-08 13:07:36 +0000 | [diff] [blame] | 184 | #ifdef REQUIRES_SEC |
| 185 | ".arch_extension sec\n" |
| 186 | #endif |
Stephen Boyd | 8e76a80 | 2011-02-24 10:44:44 -0800 | [diff] [blame] | 187 | "smc #0 @ switch to secure world\n" |
| 188 | : "=r" (r0) |
| 189 | : "r" (r0), "r" (r1), "r" (r2) |
| 190 | : "r3"); |
| 191 | } while (r0 == SCM_INTERRUPTED); |
| 192 | |
Stephen Boyd | 2a1eb58 | 2010-08-27 10:01:23 -0700 | [diff] [blame] | 193 | return r0; |
| 194 | } |
| 195 | |
| 196 | static int __scm_call(const struct scm_command *cmd) |
| 197 | { |
| 198 | int ret; |
| 199 | u32 cmd_addr = virt_to_phys(cmd); |
| 200 | |
| 201 | /* |
| 202 | * Flush the entire cache here so callers don't have to remember |
| 203 | * to flush the cache when passing physical addresses to the secure |
| 204 | * side in the buffer. |
| 205 | */ |
| 206 | flush_cache_all(); |
Stephen Boyd | f76c691 | 2014-08-04 18:31:43 -0700 | [diff] [blame^] | 207 | outer_flush_all(); |
Stephen Boyd | 8e76a80 | 2011-02-24 10:44:44 -0800 | [diff] [blame] | 208 | ret = smc(cmd_addr); |
| 209 | if (ret < 0) |
| 210 | ret = scm_remap_error(ret); |
Stephen Boyd | 2a1eb58 | 2010-08-27 10:01:23 -0700 | [diff] [blame] | 211 | |
| 212 | return ret; |
| 213 | } |
| 214 | |
Stephen Boyd | f76c691 | 2014-08-04 18:31:43 -0700 | [diff] [blame^] | 215 | static void scm_inv_range(unsigned long start, unsigned long end) |
| 216 | { |
| 217 | start = round_down(start, CACHELINESIZE); |
| 218 | end = round_up(end, CACHELINESIZE); |
| 219 | outer_inv_range(start, end); |
| 220 | while (start < end) { |
| 221 | asm ("mcr p15, 0, %0, c7, c6, 1" : : "r" (start) |
| 222 | : "memory"); |
| 223 | start += CACHELINESIZE; |
| 224 | } |
| 225 | dsb(); |
| 226 | isb(); |
| 227 | } |
| 228 | |
Stephen Boyd | 2a1eb58 | 2010-08-27 10:01:23 -0700 | [diff] [blame] | 229 | /** |
| 230 | * scm_call() - Send an SCM command |
| 231 | * @svc_id: service identifier |
| 232 | * @cmd_id: command identifier |
| 233 | * @cmd_buf: command buffer |
| 234 | * @cmd_len: length of the command buffer |
| 235 | * @resp_buf: response buffer |
| 236 | * @resp_len: length of the response buffer |
| 237 | * |
| 238 | * Sends a command to the SCM and waits for the command to finish processing. |
| 239 | */ |
| 240 | int scm_call(u32 svc_id, u32 cmd_id, const void *cmd_buf, size_t cmd_len, |
| 241 | void *resp_buf, size_t resp_len) |
| 242 | { |
| 243 | int ret; |
| 244 | struct scm_command *cmd; |
| 245 | struct scm_response *rsp; |
Stephen Boyd | f76c691 | 2014-08-04 18:31:43 -0700 | [diff] [blame^] | 246 | unsigned long start, end; |
Stephen Boyd | 2a1eb58 | 2010-08-27 10:01:23 -0700 | [diff] [blame] | 247 | |
| 248 | cmd = alloc_scm_command(cmd_len, resp_len); |
| 249 | if (!cmd) |
| 250 | return -ENOMEM; |
| 251 | |
| 252 | cmd->id = (svc_id << 10) | cmd_id; |
| 253 | if (cmd_buf) |
| 254 | memcpy(scm_get_command_buffer(cmd), cmd_buf, cmd_len); |
| 255 | |
| 256 | mutex_lock(&scm_lock); |
| 257 | ret = __scm_call(cmd); |
| 258 | mutex_unlock(&scm_lock); |
| 259 | if (ret) |
| 260 | goto out; |
| 261 | |
| 262 | rsp = scm_command_to_response(cmd); |
Stephen Boyd | f76c691 | 2014-08-04 18:31:43 -0700 | [diff] [blame^] | 263 | start = (unsigned long)rsp; |
| 264 | |
Stephen Boyd | 2a1eb58 | 2010-08-27 10:01:23 -0700 | [diff] [blame] | 265 | do { |
Stephen Boyd | f76c691 | 2014-08-04 18:31:43 -0700 | [diff] [blame^] | 266 | scm_inv_range(start, start + sizeof(*rsp)); |
Stephen Boyd | 2a1eb58 | 2010-08-27 10:01:23 -0700 | [diff] [blame] | 267 | } while (!rsp->is_complete); |
| 268 | |
Stephen Boyd | f76c691 | 2014-08-04 18:31:43 -0700 | [diff] [blame^] | 269 | end = (unsigned long)scm_get_response_buffer(rsp) + resp_len; |
| 270 | scm_inv_range(start, end); |
| 271 | |
Stephen Boyd | 2a1eb58 | 2010-08-27 10:01:23 -0700 | [diff] [blame] | 272 | if (resp_buf) |
| 273 | memcpy(resp_buf, scm_get_response_buffer(rsp), resp_len); |
| 274 | out: |
| 275 | free_scm_command(cmd); |
| 276 | return ret; |
| 277 | } |
| 278 | EXPORT_SYMBOL(scm_call); |
| 279 | |
| 280 | u32 scm_get_version(void) |
| 281 | { |
| 282 | int context_id; |
| 283 | static u32 version = -1; |
Stephen Boyd | 98d4ded | 2011-02-24 10:44:43 -0800 | [diff] [blame] | 284 | register u32 r0 asm("r0"); |
| 285 | register u32 r1 asm("r1"); |
Stephen Boyd | 2a1eb58 | 2010-08-27 10:01:23 -0700 | [diff] [blame] | 286 | |
| 287 | if (version != -1) |
| 288 | return version; |
| 289 | |
| 290 | mutex_lock(&scm_lock); |
Stephen Boyd | 98d4ded | 2011-02-24 10:44:43 -0800 | [diff] [blame] | 291 | |
| 292 | r0 = 0x1 << 8; |
| 293 | r1 = (u32)&context_id; |
Stephen Boyd | 8e76a80 | 2011-02-24 10:44:44 -0800 | [diff] [blame] | 294 | do { |
| 295 | asm volatile( |
| 296 | __asmeq("%0", "r0") |
| 297 | __asmeq("%1", "r1") |
| 298 | __asmeq("%2", "r0") |
| 299 | __asmeq("%3", "r1") |
Stephen Boyd | 26e87b1 | 2012-04-30 19:17:20 -0700 | [diff] [blame] | 300 | #ifdef REQUIRES_SEC |
| 301 | ".arch_extension sec\n" |
| 302 | #endif |
Stephen Boyd | 8e76a80 | 2011-02-24 10:44:44 -0800 | [diff] [blame] | 303 | "smc #0 @ switch to secure world\n" |
| 304 | : "=r" (r0), "=r" (r1) |
| 305 | : "r" (r0), "r" (r1) |
| 306 | : "r2", "r3"); |
| 307 | } while (r0 == SCM_INTERRUPTED); |
| 308 | |
Stephen Boyd | 2a1eb58 | 2010-08-27 10:01:23 -0700 | [diff] [blame] | 309 | version = r1; |
| 310 | mutex_unlock(&scm_lock); |
| 311 | |
| 312 | return version; |
| 313 | } |
| 314 | EXPORT_SYMBOL(scm_get_version); |