blob: dae8723d5a95ee55b5c7e94448f01ab73caac5fb [file] [log] [blame]
Siddhartha Agrawaleb094c52013-01-28 12:11:43 -08001/* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
Deepa Dinamani904f8f82012-12-05 16:35:01 -08002 *
Shashank Mittal162244e2011-08-08 19:01:25 -07003 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
Deepa Dinamani904f8f82012-12-05 16:35:01 -08006 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer in the documentation and/or other materials provided
11 * with the distribution.
12 * * Neither the name of The Linux Foundation nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
Shashank Mittal162244e2011-08-08 19:01:25 -070015 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <stdlib.h>
30#include <string.h>
31#include <err.h>
Neeti Desai127b9e02012-03-20 16:11:23 -070032#include <arch/ops.h>
Shashank Mittal162244e2011-08-08 19:01:25 -070033#include "scm.h"
34
35#pragma GCC optimize ("O0")
36
37/* From Linux Kernel asm/system.h */
38#define __asmeq(x, y) ".ifnc " x "," y " ; .err ; .endif\n\t"
39
40#ifndef offsetof
41# define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
42#endif
43
44/**
45 * alloc_scm_command() - Allocate an SCM command
46 * @cmd_size: size of the command buffer
47 * @resp_size: size of the response buffer
48 *
49 * Allocate an SCM command, including enough room for the command
50 * and response headers as well as the command and response buffers.
51 *
52 * Returns a valid &scm_command on success or %NULL if the allocation fails.
53 */
54static struct scm_command *alloc_scm_command(size_t cmd_size, size_t resp_size)
55{
56 struct scm_command *cmd;
57 size_t len = sizeof(*cmd) + sizeof(struct scm_response) + cmd_size +
Ajay Dudanib01e5062011-12-03 23:23:42 -080058 resp_size;
Shashank Mittal162244e2011-08-08 19:01:25 -070059
Deepa Dinamani904f8f82012-12-05 16:35:01 -080060 cmd = memalign(CACHE_LINE, ROUNDUP(len, CACHE_LINE));
Ajay Dudanib01e5062011-12-03 23:23:42 -080061 if (cmd) {
Shashank Mittal162244e2011-08-08 19:01:25 -070062 cmd->len = len;
63 cmd->buf_offset = offsetof(struct scm_command, buf);
64 cmd->resp_hdr_offset = cmd->buf_offset + cmd_size;
65 }
66 return cmd;
67}
68
69/**
70 * free_scm_command() - Free an SCM command
71 * @cmd: command to free
72 *
73 * Free an SCM command.
74 */
75static inline void free_scm_command(struct scm_command *cmd)
76{
77 free(cmd);
78}
79
80/**
81 * scm_command_to_response() - Get a pointer to a scm_response
82 * @cmd: command
83 *
84 * Returns a pointer to a response for a command.
85 */
Ajay Dudanib01e5062011-12-03 23:23:42 -080086static inline struct scm_response *scm_command_to_response(const struct
87 scm_command *cmd)
Shashank Mittal162244e2011-08-08 19:01:25 -070088{
89 return (void *)cmd + cmd->resp_hdr_offset;
90}
91
92/**
93 * scm_get_command_buffer() - Get a pointer to a command buffer
94 * @cmd: command
95 *
96 * Returns a pointer to the command buffer of a command.
97 */
98static inline void *scm_get_command_buffer(const struct scm_command *cmd)
99{
100 return (void *)cmd->buf;
101}
102
103/**
104 * scm_get_response_buffer() - Get a pointer to a response buffer
105 * @rsp: response
106 *
107 * Returns a pointer to a response buffer of a response.
108 */
109static inline void *scm_get_response_buffer(const struct scm_response *rsp)
110{
111 return (void *)rsp + rsp->buf_offset;
112}
113
114static uint32_t smc(uint32_t cmd_addr)
115{
116 uint32_t context_id;
117 register uint32_t r0 __asm__("r0") = 1;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800118 register uint32_t r1 __asm__("r1") = (uint32_t) & context_id;
Shashank Mittal162244e2011-08-08 19:01:25 -0700119 register uint32_t r2 __asm__("r2") = cmd_addr;
Ajay Dudanib01e5062011-12-03 23:23:42 -0800120 __asm__("1:smc #0 @ switch to secure world\n" "cmp r0, #1 \n" "beq 1b \n": "=r"(r0): "r"(r0), "r"(r1), "r"(r2):"r3", "cc");
Shashank Mittal162244e2011-08-08 19:01:25 -0700121 return r0;
122}
123
124/**
125 * scm_call() - Send an SCM command
126 * @svc_id: service identifier
127 * @cmd_id: command identifier
128 * @cmd_buf: command buffer
129 * @cmd_len: length of the command buffer
130 * @resp_buf: response buffer
131 * @resp_len: length of the response buffer
132 *
133 * Sends a command to the SCM and waits for the command to finish processing.
134 */
Ajay Dudanib01e5062011-12-03 23:23:42 -0800135int
136scm_call(uint32_t svc_id, uint32_t cmd_id, const void *cmd_buf,
137 size_t cmd_len, void *resp_buf, size_t resp_len)
Shashank Mittal162244e2011-08-08 19:01:25 -0700138{
139 int ret;
140 struct scm_command *cmd;
141 struct scm_response *rsp;
Neeti Desai127b9e02012-03-20 16:11:23 -0700142 uint8_t *resp_ptr;
Shashank Mittal162244e2011-08-08 19:01:25 -0700143
144 cmd = alloc_scm_command(cmd_len, resp_len);
145 if (!cmd)
146 return ERR_NO_MEMORY;
147
148 cmd->id = (svc_id << 10) | cmd_id;
149 if (cmd_buf)
150 memcpy(scm_get_command_buffer(cmd), cmd_buf, cmd_len);
151
Neeti Desai127b9e02012-03-20 16:11:23 -0700152 /* Flush command to main memory for TZ */
153 arch_clean_invalidate_cache_range((addr_t) cmd, cmd->len);
154
Ajay Dudanib01e5062011-12-03 23:23:42 -0800155 ret = smc((uint32_t) cmd);
Neeti Desai127b9e02012-03-20 16:11:23 -0700156
Shashank Mittal162244e2011-08-08 19:01:25 -0700157 if (ret)
158 goto out;
159
Ajay Dudanib01e5062011-12-03 23:23:42 -0800160 if (resp_len) {
Shashank Mittal162244e2011-08-08 19:01:25 -0700161 rsp = scm_command_to_response(cmd);
162
Neeti Desai127b9e02012-03-20 16:11:23 -0700163 do
164 {
165 /* Need to invalidate before each check since TZ will update
166 * the response complete flag in main memory.
167 */
168 arch_clean_invalidate_cache_range((addr_t) rsp, sizeof(*rsp));
169 } while (!rsp->is_complete);
170
171
172 resp_ptr = scm_get_response_buffer(rsp);
173
174 /* Invalidate any cached response data */
175 arch_clean_invalidate_cache_range((addr_t) resp_ptr, resp_len);
Shashank Mittal162244e2011-08-08 19:01:25 -0700176
177 if (resp_buf)
Neeti Desai127b9e02012-03-20 16:11:23 -0700178 memcpy(resp_buf, resp_ptr, resp_len);
Shashank Mittal162244e2011-08-08 19:01:25 -0700179 }
Ajay Dudanib01e5062011-12-03 23:23:42 -0800180 out:
Shashank Mittal162244e2011-08-08 19:01:25 -0700181 free_scm_command(cmd);
182 return ret;
183}
184
Siddhartha Agrawaleb094c52013-01-28 12:11:43 -0800185int restore_secure_cfg(uint32_t id)
186{
187 int ret, scm_ret = 0;
188 tz_secure_cfg secure_cfg;
189
190 secure_cfg.id = 1;
191 secure_cfg.spare = 0;
192
sundarajan srinivasanc2dee742013-02-21 11:31:36 -0800193 ret = scm_call(SVC_MEMORY_PROTECTION, IOMMU_SECURE_CFG, &secure_cfg, sizeof(secure_cfg),
Siddhartha Agrawaleb094c52013-01-28 12:11:43 -0800194 &scm_ret, sizeof(scm_ret));
195
196 if (ret || scm_ret) {
197 dprintf(CRITICAL, "Secure Config failed\n");
198 ret = 1;
199 } else
200 ret = 0;
201
202 return ret;
203
204}
205
Neeti Desai127b9e02012-03-20 16:11:23 -0700206/* SCM Encrypt Command */
207int encrypt_scm(uint32_t ** img_ptr, uint32_t * img_len_ptr)
Shashank Mittal162244e2011-08-08 19:01:25 -0700208{
Neeti Desai127b9e02012-03-20 16:11:23 -0700209 int ret;
210 img_req cmd;
Shashank Mittal162244e2011-08-08 19:01:25 -0700211
Neeti Desai127b9e02012-03-20 16:11:23 -0700212 cmd.img_ptr = (uint32*) img_ptr;
213 cmd.img_len_ptr = img_len_ptr;
Shashank Mittal162244e2011-08-08 19:01:25 -0700214
Neeti Desai127b9e02012-03-20 16:11:23 -0700215 /* Image data is operated upon by TZ, which accesses only the main memory.
216 * It must be flushed/invalidated before and after TZ call.
217 */
218 arch_clean_invalidate_cache_range((addr_t) *img_ptr, *img_len_ptr);
Shashank Mittal162244e2011-08-08 19:01:25 -0700219
Neeti Desai127b9e02012-03-20 16:11:23 -0700220 ret = scm_call(SCM_SVC_SSD, SSD_ENCRYPT_ID, &cmd, sizeof(cmd), NULL, 0);
Shashank Mittal162244e2011-08-08 19:01:25 -0700221
Neeti Desai127b9e02012-03-20 16:11:23 -0700222 /* Values at img_ptr and img_len_ptr are updated by TZ. Must be invalidated
223 * before we use them.
Amol Jadi55e58da2011-11-17 14:03:34 -0800224 */
225 arch_clean_invalidate_cache_range((addr_t) img_ptr, sizeof(img_ptr));
Neeti Desai127b9e02012-03-20 16:11:23 -0700226 arch_clean_invalidate_cache_range((addr_t) img_len_ptr, sizeof(img_len_ptr));
Amol Jadi55e58da2011-11-17 14:03:34 -0800227
Neeti Desai127b9e02012-03-20 16:11:23 -0700228 /* Invalidate the updated image data */
229 arch_clean_invalidate_cache_range((addr_t) *img_ptr, *img_len_ptr);
Amol Jadi55e58da2011-11-17 14:03:34 -0800230
Shashank Mittal162244e2011-08-08 19:01:25 -0700231 return ret;
232}
233
Neeti Desai127b9e02012-03-20 16:11:23 -0700234/* SCM Decrypt Command */
235int decrypt_scm(uint32_t ** img_ptr, uint32_t * img_len_ptr)
236{
237 int ret;
238 img_req cmd;
239
240 cmd.img_ptr = (uint32*) img_ptr;
241 cmd.img_len_ptr = img_len_ptr;
242
243 /* Image data is operated upon by TZ, which accesses only the main memory.
244 * It must be flushed/invalidated before and after TZ call.
245 */
246 arch_clean_invalidate_cache_range((addr_t) *img_ptr, *img_len_ptr);
247
248 ret = scm_call(SCM_SVC_SSD, SSD_DECRYPT_ID, &cmd, sizeof(cmd), NULL, 0);
249
250 /* Values at img_ptr and img_len_ptr are updated by TZ. Must be invalidated
251 * before we use them.
252 */
253 arch_clean_invalidate_cache_range((addr_t) img_ptr, sizeof(img_ptr));
254 arch_clean_invalidate_cache_range((addr_t) img_len_ptr, sizeof(img_len_ptr));
255
256 /* Invalidate the updated image data */
257 arch_clean_invalidate_cache_range((addr_t) *img_ptr, *img_len_ptr);
258
259 return ret;
260}
261
262
Shashank Mittal162244e2011-08-08 19:01:25 -0700263void set_tamper_fuse_cmd()
264{
265 uint32_t svc_id;
266 uint32_t cmd_id;
267 void *cmd_buf;
268 size_t cmd_len;
269 void *resp_buf = NULL;
270 size_t resp_len = 0;
271
272 uint32_t fuse_id = HLOS_IMG_TAMPER_FUSE;
273 cmd_buf = (void *)&fuse_id;
274 cmd_len = sizeof(fuse_id);
275
Ajay Dudanib01e5062011-12-03 23:23:42 -0800276 /*no response */
Shashank Mittal162244e2011-08-08 19:01:25 -0700277 resp_buf = NULL;
278 resp_len = 0;
279
280 svc_id = SCM_SVC_FUSE;
281 cmd_id = SCM_BLOW_SW_FUSE_ID;
282
283 scm_call(svc_id, cmd_id, cmd_buf, cmd_len, resp_buf, resp_len);
284 return;
285}
286
287uint8_t get_tamper_fuse_cmd()
288{
289 uint32_t svc_id;
290 uint32_t cmd_id;
291 void *cmd_buf;
292 size_t cmd_len;
293 size_t resp_len = 0;
294 uint8_t resp_buf;
295
296 uint32_t fuse_id = HLOS_IMG_TAMPER_FUSE;
297 cmd_buf = (void *)&fuse_id;
298 cmd_len = sizeof(fuse_id);
299
Ajay Dudanib01e5062011-12-03 23:23:42 -0800300 /*response */
Shashank Mittal162244e2011-08-08 19:01:25 -0700301 resp_len = sizeof(resp_buf);
302
303 svc_id = SCM_SVC_FUSE;
304 cmd_id = SCM_IS_SW_FUSE_BLOWN_ID;
305
306 scm_call(svc_id, cmd_id, cmd_buf, cmd_len, &resp_buf, resp_len);
307 return resp_buf;
308}
Deepa Dinamani193874e2012-02-07 14:00:04 -0800309
310/*
311 * Switches the CE1 channel between ADM and register usage.
312 * channel : AP_CE_REGISTER_USE, CE1 uses register interface
313 * : AP_CE_ADM_USE, CE1 uses ADM interface
314 */
315uint8_t switch_ce_chn_cmd(enum ap_ce_channel_type channel)
316{
317 uint32_t svc_id;
318 uint32_t cmd_id;
319 void *cmd_buf;
320 size_t cmd_len;
321 size_t resp_len = 0;
322 uint8_t resp_buf;
323
324 struct {
325 uint32_t resource;
326 uint32_t chn_id;
327 }__PACKED switch_ce_chn_buf;
328
329 switch_ce_chn_buf.resource = TZ_RESOURCE_CE_AP;
330 switch_ce_chn_buf.chn_id = channel;
331 cmd_buf = (void *)&switch_ce_chn_buf;
332 cmd_len = sizeof(switch_ce_chn_buf);
333
334 /*response */
335 resp_len = sizeof(resp_buf);
336
337 svc_id = SCM_SVC_CE_CHN_SWITCH_ID;
338 cmd_id = SCM_CE_CHN_SWITCH_ID;
339
340 scm_call(svc_id, cmd_id, cmd_buf, cmd_len, &resp_buf, resp_len);
341 return resp_buf;
342}
343