1 /* Copyright (c) 2010,2015, The Linux Foundation. All rights reserved.
2 * Copyright (C) 2015 Linaro Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #include <linux/slab.h>
21 #include <linux/module.h>
22 #include <linux/mutex.h>
23 #include <linux/errno.h>
24 #include <linux/err.h>
25 #include <linux/qcom_scm.h>
27 #include <asm/outercache.h>
28 #include <asm/cacheflush.h>
32 #define QCOM_SCM_FLAG_COLDBOOT_CPU0 0x00
33 #define QCOM_SCM_FLAG_COLDBOOT_CPU1 0x01
34 #define QCOM_SCM_FLAG_COLDBOOT_CPU2 0x08
35 #define QCOM_SCM_FLAG_COLDBOOT_CPU3 0x20
37 #define QCOM_SCM_FLAG_WARMBOOT_CPU0 0x04
38 #define QCOM_SCM_FLAG_WARMBOOT_CPU1 0x02
39 #define QCOM_SCM_FLAG_WARMBOOT_CPU2 0x10
40 #define QCOM_SCM_FLAG_WARMBOOT_CPU3 0x40
42 struct qcom_scm_entry
{
47 static struct qcom_scm_entry qcom_scm_wb
[] = {
48 { .flag
= QCOM_SCM_FLAG_WARMBOOT_CPU0
},
49 { .flag
= QCOM_SCM_FLAG_WARMBOOT_CPU1
},
50 { .flag
= QCOM_SCM_FLAG_WARMBOOT_CPU2
},
51 { .flag
= QCOM_SCM_FLAG_WARMBOOT_CPU3
},
54 static DEFINE_MUTEX(qcom_scm_lock
);
57 * struct qcom_scm_command - one SCM command buffer
58 * @len: total available memory for command and response
59 * @buf_offset: start of command buffer
60 * @resp_hdr_offset: start of response buffer
61 * @id: command to be executed
62 * @buf: buffer returned from qcom_scm_get_command_buffer()
64 * An SCM command is laid out in memory as follows:
66 * ------------------- <--- struct qcom_scm_command
68 * ------------------- <--- qcom_scm_get_command_buffer()
70 * ------------------- <--- struct qcom_scm_response and
71 * | response header | qcom_scm_command_to_response()
72 * ------------------- <--- qcom_scm_get_response_buffer()
76 * There can be arbitrary padding between the headers and buffers so
77 * you should always use the appropriate qcom_scm_get_*_buffer() routines
78 * to access the buffers in a safe manner.
80 struct qcom_scm_command
{
83 __le32 resp_hdr_offset
;
89 * struct qcom_scm_response - one SCM response buffer
90 * @len: total available memory for response
91 * @buf_offset: start of response data relative to start of qcom_scm_response
92 * @is_complete: indicates if the command has finished processing
94 struct qcom_scm_response
{
101 * alloc_qcom_scm_command() - Allocate an SCM command
102 * @cmd_size: size of the command buffer
103 * @resp_size: size of the response buffer
105 * Allocate an SCM command, including enough room for the command
106 * and response headers as well as the command and response buffers.
108 * Returns a valid &qcom_scm_command on success or %NULL if the allocation fails.
110 static struct qcom_scm_command
*alloc_qcom_scm_command(size_t cmd_size
, size_t resp_size
)
112 struct qcom_scm_command
*cmd
;
113 size_t len
= sizeof(*cmd
) + sizeof(struct qcom_scm_response
) + cmd_size
+
117 cmd
= kzalloc(PAGE_ALIGN(len
), GFP_KERNEL
);
119 cmd
->len
= cpu_to_le32(len
);
120 offset
= offsetof(struct qcom_scm_command
, buf
);
121 cmd
->buf_offset
= cpu_to_le32(offset
);
122 cmd
->resp_hdr_offset
= cpu_to_le32(offset
+ cmd_size
);
128 * free_qcom_scm_command() - Free an SCM command
129 * @cmd: command to free
131 * Free an SCM command.
133 static inline void free_qcom_scm_command(struct qcom_scm_command
*cmd
)
139 * qcom_scm_command_to_response() - Get a pointer to a qcom_scm_response
142 * Returns a pointer to a response for a command.
144 static inline struct qcom_scm_response
*qcom_scm_command_to_response(
145 const struct qcom_scm_command
*cmd
)
147 return (void *)cmd
+ le32_to_cpu(cmd
->resp_hdr_offset
);
151 * qcom_scm_get_command_buffer() - Get a pointer to a command buffer
154 * Returns a pointer to the command buffer of a command.
156 static inline void *qcom_scm_get_command_buffer(const struct qcom_scm_command
*cmd
)
158 return (void *)cmd
->buf
;
162 * qcom_scm_get_response_buffer() - Get a pointer to a response buffer
165 * Returns a pointer to a response buffer of a response.
167 static inline void *qcom_scm_get_response_buffer(const struct qcom_scm_response
*rsp
)
169 return (void *)rsp
+ le32_to_cpu(rsp
->buf_offset
);
172 static int qcom_scm_remap_error(int err
)
174 pr_err("qcom_scm_call failed with error code %d\n", err
);
178 case QCOM_SCM_EINVAL_ADDR
:
179 case QCOM_SCM_EINVAL_ARG
:
181 case QCOM_SCM_EOPNOTSUPP
:
183 case QCOM_SCM_ENOMEM
:
189 static u32
smc(u32 cmd_addr
)
192 register u32 r0
asm("r0") = 1;
193 register u32 r1
asm("r1") = (u32
)&context_id
;
194 register u32 r2
asm("r2") = cmd_addr
;
202 ".arch_extension sec\n"
204 "smc #0 @ switch to secure world\n"
206 : "r" (r0
), "r" (r1
), "r" (r2
)
208 } while (r0
== QCOM_SCM_INTERRUPTED
);
213 static int __qcom_scm_call(const struct qcom_scm_command
*cmd
)
216 u32 cmd_addr
= virt_to_phys(cmd
);
219 * Flush the command buffer so that the secure world sees
222 __cpuc_flush_dcache_area((void *)cmd
, cmd
->len
);
223 outer_flush_range(cmd_addr
, cmd_addr
+ cmd
->len
);
227 ret
= qcom_scm_remap_error(ret
);
232 static void qcom_scm_inv_range(unsigned long start
, unsigned long end
)
234 u32 cacheline_size
, ctr
;
236 asm volatile("mrc p15, 0, %0, c0, c0, 1" : "=r" (ctr
));
237 cacheline_size
= 4 << ((ctr
>> 16) & 0xf);
239 start
= round_down(start
, cacheline_size
);
240 end
= round_up(end
, cacheline_size
);
241 outer_inv_range(start
, end
);
242 while (start
< end
) {
243 asm ("mcr p15, 0, %0, c7, c6, 1" : : "r" (start
)
245 start
+= cacheline_size
;
252 * qcom_scm_call() - Send an SCM command
253 * @svc_id: service identifier
254 * @cmd_id: command identifier
255 * @cmd_buf: command buffer
256 * @cmd_len: length of the command buffer
257 * @resp_buf: response buffer
258 * @resp_len: length of the response buffer
260 * Sends a command to the SCM and waits for the command to finish processing.
262 * A note on cache maintenance:
263 * Note that any buffers that are expected to be accessed by the secure world
264 * must be flushed before invoking qcom_scm_call and invalidated in the cache
265 * immediately after qcom_scm_call returns. Cache maintenance on the command
266 * and response buffers is taken care of by qcom_scm_call; however, callers are
267 * responsible for any other cached buffers passed over to the secure world.
269 static int qcom_scm_call(u32 svc_id
, u32 cmd_id
, const void *cmd_buf
,
270 size_t cmd_len
, void *resp_buf
, size_t resp_len
)
273 struct qcom_scm_command
*cmd
;
274 struct qcom_scm_response
*rsp
;
275 unsigned long start
, end
;
277 cmd
= alloc_qcom_scm_command(cmd_len
, resp_len
);
281 cmd
->id
= cpu_to_le32((svc_id
<< 10) | cmd_id
);
283 memcpy(qcom_scm_get_command_buffer(cmd
), cmd_buf
, cmd_len
);
285 mutex_lock(&qcom_scm_lock
);
286 ret
= __qcom_scm_call(cmd
);
287 mutex_unlock(&qcom_scm_lock
);
291 rsp
= qcom_scm_command_to_response(cmd
);
292 start
= (unsigned long)rsp
;
295 qcom_scm_inv_range(start
, start
+ sizeof(*rsp
));
296 } while (!rsp
->is_complete
);
298 end
= (unsigned long)qcom_scm_get_response_buffer(rsp
) + resp_len
;
299 qcom_scm_inv_range(start
, end
);
302 memcpy(resp_buf
, qcom_scm_get_response_buffer(rsp
), resp_len
);
304 free_qcom_scm_command(cmd
);
308 #define SCM_CLASS_REGISTER (0x2 << 8)
309 #define SCM_MASK_IRQS BIT(5)
310 #define SCM_ATOMIC(svc, cmd, n) (((((svc) << 10)|((cmd) & 0x3ff)) << 12) | \
311 SCM_CLASS_REGISTER | \
316 * qcom_scm_call_atomic1() - Send an atomic SCM command with one argument
317 * @svc_id: service identifier
318 * @cmd_id: command identifier
319 * @arg1: first argument
321 * This shall only be used with commands that are guaranteed to be
322 * uninterruptable, atomic and SMP safe.
324 static s32
qcom_scm_call_atomic1(u32 svc
, u32 cmd
, u32 arg1
)
328 register u32 r0
asm("r0") = SCM_ATOMIC(svc
, cmd
, 1);
329 register u32 r1
asm("r1") = (u32
)&context_id
;
330 register u32 r2
asm("r2") = arg1
;
338 ".arch_extension sec\n"
340 "smc #0 @ switch to secure world\n"
342 : "r" (r0
), "r" (r1
), "r" (r2
)
347 u32
qcom_scm_get_version(void)
350 static u32 version
= -1;
351 register u32 r0
asm("r0");
352 register u32 r1
asm("r1");
357 mutex_lock(&qcom_scm_lock
);
360 r1
= (u32
)&context_id
;
368 ".arch_extension sec\n"
370 "smc #0 @ switch to secure world\n"
371 : "=r" (r0
), "=r" (r1
)
374 } while (r0
== QCOM_SCM_INTERRUPTED
);
377 mutex_unlock(&qcom_scm_lock
);
381 EXPORT_SYMBOL(qcom_scm_get_version
);
384 * Set the cold/warm boot address for one of the CPU cores.
386 static int qcom_scm_set_boot_addr(u32 addr
, int flags
)
393 cmd
.addr
= cpu_to_le32(addr
);
394 cmd
.flags
= cpu_to_le32(flags
);
395 return qcom_scm_call(QCOM_SCM_SVC_BOOT
, QCOM_SCM_BOOT_ADDR
,
396 &cmd
, sizeof(cmd
), NULL
, 0);
400 * qcom_scm_set_cold_boot_addr() - Set the cold boot address for cpus
401 * @entry: Entry point function for the cpus
402 * @cpus: The cpumask of cpus that will use the entry point
404 * Set the cold boot address of the cpus. Any cpu outside the supported
405 * range would be removed from the cpu present mask.
407 int __qcom_scm_set_cold_boot_addr(void *entry
, const cpumask_t
*cpus
)
411 int scm_cb_flags
[] = {
412 QCOM_SCM_FLAG_COLDBOOT_CPU0
,
413 QCOM_SCM_FLAG_COLDBOOT_CPU1
,
414 QCOM_SCM_FLAG_COLDBOOT_CPU2
,
415 QCOM_SCM_FLAG_COLDBOOT_CPU3
,
418 if (!cpus
|| (cpus
&& cpumask_empty(cpus
)))
421 for_each_cpu(cpu
, cpus
) {
422 if (cpu
< ARRAY_SIZE(scm_cb_flags
))
423 flags
|= scm_cb_flags
[cpu
];
425 set_cpu_present(cpu
, false);
428 return qcom_scm_set_boot_addr(virt_to_phys(entry
), flags
);
432 * qcom_scm_set_warm_boot_addr() - Set the warm boot address for cpus
433 * @entry: Entry point function for the cpus
434 * @cpus: The cpumask of cpus that will use the entry point
436 * Set the Linux entry point for the SCM to transfer control to when coming
437 * out of a power down. CPU power down may be executed on cpuidle or hotplug.
439 int __qcom_scm_set_warm_boot_addr(void *entry
, const cpumask_t
*cpus
)
446 * Reassign only if we are switching from hotplug entry point
447 * to cpuidle entry point or vice versa.
449 for_each_cpu(cpu
, cpus
) {
450 if (entry
== qcom_scm_wb
[cpu
].entry
)
452 flags
|= qcom_scm_wb
[cpu
].flag
;
455 /* No change in entry function */
459 ret
= qcom_scm_set_boot_addr(virt_to_phys(entry
), flags
);
461 for_each_cpu(cpu
, cpus
)
462 qcom_scm_wb
[cpu
].entry
= entry
;
469 * qcom_scm_cpu_power_down() - Power down the cpu
470 * @flags - Flags to flush cache
472 * This is an end point to power down cpu. If there was a pending interrupt,
473 * the control would return from this function, otherwise, the cpu jumps to the
474 * warm boot entry point set for this cpu upon reset.
476 void __qcom_scm_cpu_power_down(u32 flags
)
478 qcom_scm_call_atomic1(QCOM_SCM_SVC_BOOT
, QCOM_SCM_CMD_TERMINATE_PC
,
479 flags
& QCOM_SCM_FLUSH_FLAG_MASK
);
482 int __qcom_scm_is_call_available(u32 svc_id
, u32 cmd_id
)
485 u32 svc_cmd
= (svc_id
<< 10) | cmd_id
;
488 ret
= qcom_scm_call(QCOM_SCM_SVC_INFO
, QCOM_IS_CALL_AVAIL_CMD
, &svc_cmd
,
489 sizeof(svc_cmd
), &ret_val
, sizeof(ret_val
));
496 int __qcom_scm_hdcp_req(struct qcom_scm_hdcp_req
*req
, u32 req_cnt
, u32
*resp
)
498 if (req_cnt
> QCOM_SCM_HDCP_MAX_REQ_CNT
)
501 return qcom_scm_call(QCOM_SCM_SVC_HDCP
, QCOM_SCM_CMD_HDCP
,
502 req
, req_cnt
* sizeof(*req
), resp
, sizeof(*resp
));