]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/firmware/qcom_scm-32.c
firmware: qcom: scm: Generalize shared error map
[mirror_ubuntu-jammy-kernel.git] / drivers / firmware / qcom_scm-32.c
1 /* Copyright (c) 2010,2015, The Linux Foundation. All rights reserved.
2 * Copyright (C) 2015 Linaro Ltd.
3 *
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.
7 *
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.
12 *
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
16 * 02110-1301, USA.
17 */
18
19 #include <linux/slab.h>
20 #include <linux/io.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>
26
27 #include <asm/cacheflush.h>
28
29 #include "qcom_scm.h"
30
31 #define QCOM_SCM_FLAG_COLDBOOT_CPU0 0x00
32 #define QCOM_SCM_FLAG_COLDBOOT_CPU1 0x01
33 #define QCOM_SCM_FLAG_COLDBOOT_CPU2 0x08
34 #define QCOM_SCM_FLAG_COLDBOOT_CPU3 0x20
35
36 #define QCOM_SCM_FLAG_WARMBOOT_CPU0 0x04
37 #define QCOM_SCM_FLAG_WARMBOOT_CPU1 0x02
38 #define QCOM_SCM_FLAG_WARMBOOT_CPU2 0x10
39 #define QCOM_SCM_FLAG_WARMBOOT_CPU3 0x40
40
41 struct qcom_scm_entry {
42 int flag;
43 void *entry;
44 };
45
46 static struct qcom_scm_entry qcom_scm_wb[] = {
47 { .flag = QCOM_SCM_FLAG_WARMBOOT_CPU0 },
48 { .flag = QCOM_SCM_FLAG_WARMBOOT_CPU1 },
49 { .flag = QCOM_SCM_FLAG_WARMBOOT_CPU2 },
50 { .flag = QCOM_SCM_FLAG_WARMBOOT_CPU3 },
51 };
52
53 static DEFINE_MUTEX(qcom_scm_lock);
54
55 /**
56 * struct qcom_scm_command - one SCM command buffer
57 * @len: total available memory for command and response
58 * @buf_offset: start of command buffer
59 * @resp_hdr_offset: start of response buffer
60 * @id: command to be executed
61 * @buf: buffer returned from qcom_scm_get_command_buffer()
62 *
63 * An SCM command is laid out in memory as follows:
64 *
65 * ------------------- <--- struct qcom_scm_command
66 * | command header |
67 * ------------------- <--- qcom_scm_get_command_buffer()
68 * | command buffer |
69 * ------------------- <--- struct qcom_scm_response and
70 * | response header | qcom_scm_command_to_response()
71 * ------------------- <--- qcom_scm_get_response_buffer()
72 * | response buffer |
73 * -------------------
74 *
75 * There can be arbitrary padding between the headers and buffers so
76 * you should always use the appropriate qcom_scm_get_*_buffer() routines
77 * to access the buffers in a safe manner.
78 */
79 struct qcom_scm_command {
80 __le32 len;
81 __le32 buf_offset;
82 __le32 resp_hdr_offset;
83 __le32 id;
84 __le32 buf[0];
85 };
86
87 /**
88 * struct qcom_scm_response - one SCM response buffer
89 * @len: total available memory for response
90 * @buf_offset: start of response data relative to start of qcom_scm_response
91 * @is_complete: indicates if the command has finished processing
92 */
93 struct qcom_scm_response {
94 __le32 len;
95 __le32 buf_offset;
96 __le32 is_complete;
97 };
98
99 /**
100 * alloc_qcom_scm_command() - Allocate an SCM command
101 * @cmd_size: size of the command buffer
102 * @resp_size: size of the response buffer
103 *
104 * Allocate an SCM command, including enough room for the command
105 * and response headers as well as the command and response buffers.
106 *
107 * Returns a valid &qcom_scm_command on success or %NULL if the allocation fails.
108 */
109 static struct qcom_scm_command *alloc_qcom_scm_command(size_t cmd_size, size_t resp_size)
110 {
111 struct qcom_scm_command *cmd;
112 size_t len = sizeof(*cmd) + sizeof(struct qcom_scm_response) + cmd_size +
113 resp_size;
114 u32 offset;
115
116 cmd = kzalloc(PAGE_ALIGN(len), GFP_KERNEL);
117 if (cmd) {
118 cmd->len = cpu_to_le32(len);
119 offset = offsetof(struct qcom_scm_command, buf);
120 cmd->buf_offset = cpu_to_le32(offset);
121 cmd->resp_hdr_offset = cpu_to_le32(offset + cmd_size);
122 }
123 return cmd;
124 }
125
126 /**
127 * free_qcom_scm_command() - Free an SCM command
128 * @cmd: command to free
129 *
130 * Free an SCM command.
131 */
132 static inline void free_qcom_scm_command(struct qcom_scm_command *cmd)
133 {
134 kfree(cmd);
135 }
136
137 /**
138 * qcom_scm_command_to_response() - Get a pointer to a qcom_scm_response
139 * @cmd: command
140 *
141 * Returns a pointer to a response for a command.
142 */
143 static inline struct qcom_scm_response *qcom_scm_command_to_response(
144 const struct qcom_scm_command *cmd)
145 {
146 return (void *)cmd + le32_to_cpu(cmd->resp_hdr_offset);
147 }
148
149 /**
150 * qcom_scm_get_command_buffer() - Get a pointer to a command buffer
151 * @cmd: command
152 *
153 * Returns a pointer to the command buffer of a command.
154 */
155 static inline void *qcom_scm_get_command_buffer(const struct qcom_scm_command *cmd)
156 {
157 return (void *)cmd->buf;
158 }
159
160 /**
161 * qcom_scm_get_response_buffer() - Get a pointer to a response buffer
162 * @rsp: response
163 *
164 * Returns a pointer to a response buffer of a response.
165 */
166 static inline void *qcom_scm_get_response_buffer(const struct qcom_scm_response *rsp)
167 {
168 return (void *)rsp + le32_to_cpu(rsp->buf_offset);
169 }
170
171 static u32 smc(u32 cmd_addr)
172 {
173 int context_id;
174 register u32 r0 asm("r0") = 1;
175 register u32 r1 asm("r1") = (u32)&context_id;
176 register u32 r2 asm("r2") = cmd_addr;
177 do {
178 asm volatile(
179 __asmeq("%0", "r0")
180 __asmeq("%1", "r0")
181 __asmeq("%2", "r1")
182 __asmeq("%3", "r2")
183 #ifdef REQUIRES_SEC
184 ".arch_extension sec\n"
185 #endif
186 "smc #0 @ switch to secure world\n"
187 : "=r" (r0)
188 : "r" (r0), "r" (r1), "r" (r2)
189 : "r3");
190 } while (r0 == QCOM_SCM_INTERRUPTED);
191
192 return r0;
193 }
194
195 static int __qcom_scm_call(const struct qcom_scm_command *cmd)
196 {
197 int ret;
198 u32 cmd_addr = virt_to_phys(cmd);
199
200 /*
201 * Flush the command buffer so that the secure world sees
202 * the correct data.
203 */
204 secure_flush_area(cmd, cmd->len);
205
206 ret = smc(cmd_addr);
207 if (ret < 0)
208 ret = qcom_scm_remap_error(ret);
209
210 return ret;
211 }
212
213 static void qcom_scm_inv_range(unsigned long start, unsigned long end)
214 {
215 u32 cacheline_size, ctr;
216
217 asm volatile("mrc p15, 0, %0, c0, c0, 1" : "=r" (ctr));
218 cacheline_size = 4 << ((ctr >> 16) & 0xf);
219
220 start = round_down(start, cacheline_size);
221 end = round_up(end, cacheline_size);
222 outer_inv_range(start, end);
223 while (start < end) {
224 asm ("mcr p15, 0, %0, c7, c6, 1" : : "r" (start)
225 : "memory");
226 start += cacheline_size;
227 }
228 dsb();
229 isb();
230 }
231
232 /**
233 * qcom_scm_call() - Send an SCM command
234 * @svc_id: service identifier
235 * @cmd_id: command identifier
236 * @cmd_buf: command buffer
237 * @cmd_len: length of the command buffer
238 * @resp_buf: response buffer
239 * @resp_len: length of the response buffer
240 *
241 * Sends a command to the SCM and waits for the command to finish processing.
242 *
243 * A note on cache maintenance:
244 * Note that any buffers that are expected to be accessed by the secure world
245 * must be flushed before invoking qcom_scm_call and invalidated in the cache
246 * immediately after qcom_scm_call returns. Cache maintenance on the command
247 * and response buffers is taken care of by qcom_scm_call; however, callers are
248 * responsible for any other cached buffers passed over to the secure world.
249 */
250 static int qcom_scm_call(u32 svc_id, u32 cmd_id, const void *cmd_buf,
251 size_t cmd_len, void *resp_buf, size_t resp_len)
252 {
253 int ret;
254 struct qcom_scm_command *cmd;
255 struct qcom_scm_response *rsp;
256 unsigned long start, end;
257
258 cmd = alloc_qcom_scm_command(cmd_len, resp_len);
259 if (!cmd)
260 return -ENOMEM;
261
262 cmd->id = cpu_to_le32((svc_id << 10) | cmd_id);
263 if (cmd_buf)
264 memcpy(qcom_scm_get_command_buffer(cmd), cmd_buf, cmd_len);
265
266 mutex_lock(&qcom_scm_lock);
267 ret = __qcom_scm_call(cmd);
268 mutex_unlock(&qcom_scm_lock);
269 if (ret)
270 goto out;
271
272 rsp = qcom_scm_command_to_response(cmd);
273 start = (unsigned long)rsp;
274
275 do {
276 qcom_scm_inv_range(start, start + sizeof(*rsp));
277 } while (!rsp->is_complete);
278
279 end = (unsigned long)qcom_scm_get_response_buffer(rsp) + resp_len;
280 qcom_scm_inv_range(start, end);
281
282 if (resp_buf)
283 memcpy(resp_buf, qcom_scm_get_response_buffer(rsp), resp_len);
284 out:
285 free_qcom_scm_command(cmd);
286 return ret;
287 }
288
289 #define SCM_CLASS_REGISTER (0x2 << 8)
290 #define SCM_MASK_IRQS BIT(5)
291 #define SCM_ATOMIC(svc, cmd, n) (((((svc) << 10)|((cmd) & 0x3ff)) << 12) | \
292 SCM_CLASS_REGISTER | \
293 SCM_MASK_IRQS | \
294 (n & 0xf))
295
296 /**
297 * qcom_scm_call_atomic1() - Send an atomic SCM command with one argument
298 * @svc_id: service identifier
299 * @cmd_id: command identifier
300 * @arg1: first argument
301 *
302 * This shall only be used with commands that are guaranteed to be
303 * uninterruptable, atomic and SMP safe.
304 */
305 static s32 qcom_scm_call_atomic1(u32 svc, u32 cmd, u32 arg1)
306 {
307 int context_id;
308
309 register u32 r0 asm("r0") = SCM_ATOMIC(svc, cmd, 1);
310 register u32 r1 asm("r1") = (u32)&context_id;
311 register u32 r2 asm("r2") = arg1;
312
313 asm volatile(
314 __asmeq("%0", "r0")
315 __asmeq("%1", "r0")
316 __asmeq("%2", "r1")
317 __asmeq("%3", "r2")
318 #ifdef REQUIRES_SEC
319 ".arch_extension sec\n"
320 #endif
321 "smc #0 @ switch to secure world\n"
322 : "=r" (r0)
323 : "r" (r0), "r" (r1), "r" (r2)
324 : "r3");
325 return r0;
326 }
327
328 /**
329 * qcom_scm_call_atomic2() - Send an atomic SCM command with two arguments
330 * @svc_id: service identifier
331 * @cmd_id: command identifier
332 * @arg1: first argument
333 * @arg2: second argument
334 *
335 * This shall only be used with commands that are guaranteed to be
336 * uninterruptable, atomic and SMP safe.
337 */
338 static s32 qcom_scm_call_atomic2(u32 svc, u32 cmd, u32 arg1, u32 arg2)
339 {
340 int context_id;
341
342 register u32 r0 asm("r0") = SCM_ATOMIC(svc, cmd, 2);
343 register u32 r1 asm("r1") = (u32)&context_id;
344 register u32 r2 asm("r2") = arg1;
345 register u32 r3 asm("r3") = arg2;
346
347 asm volatile(
348 __asmeq("%0", "r0")
349 __asmeq("%1", "r0")
350 __asmeq("%2", "r1")
351 __asmeq("%3", "r2")
352 __asmeq("%4", "r3")
353 #ifdef REQUIRES_SEC
354 ".arch_extension sec\n"
355 #endif
356 "smc #0 @ switch to secure world\n"
357 : "=r" (r0)
358 : "r" (r0), "r" (r1), "r" (r2), "r" (r3)
359 );
360 return r0;
361 }
362
363 u32 qcom_scm_get_version(void)
364 {
365 int context_id;
366 static u32 version = -1;
367 register u32 r0 asm("r0");
368 register u32 r1 asm("r1");
369
370 if (version != -1)
371 return version;
372
373 mutex_lock(&qcom_scm_lock);
374
375 r0 = 0x1 << 8;
376 r1 = (u32)&context_id;
377 do {
378 asm volatile(
379 __asmeq("%0", "r0")
380 __asmeq("%1", "r1")
381 __asmeq("%2", "r0")
382 __asmeq("%3", "r1")
383 #ifdef REQUIRES_SEC
384 ".arch_extension sec\n"
385 #endif
386 "smc #0 @ switch to secure world\n"
387 : "=r" (r0), "=r" (r1)
388 : "r" (r0), "r" (r1)
389 : "r2", "r3");
390 } while (r0 == QCOM_SCM_INTERRUPTED);
391
392 version = r1;
393 mutex_unlock(&qcom_scm_lock);
394
395 return version;
396 }
397 EXPORT_SYMBOL(qcom_scm_get_version);
398
399 /**
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
403 *
404 * Set the cold boot address of the cpus. Any cpu outside the supported
405 * range would be removed from the cpu present mask.
406 */
407 int __qcom_scm_set_cold_boot_addr(void *entry, const cpumask_t *cpus)
408 {
409 int flags = 0;
410 int cpu;
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,
416 };
417
418 if (!cpus || (cpus && cpumask_empty(cpus)))
419 return -EINVAL;
420
421 for_each_cpu(cpu, cpus) {
422 if (cpu < ARRAY_SIZE(scm_cb_flags))
423 flags |= scm_cb_flags[cpu];
424 else
425 set_cpu_present(cpu, false);
426 }
427
428 return qcom_scm_call_atomic2(QCOM_SCM_SVC_BOOT, QCOM_SCM_BOOT_ADDR,
429 flags, virt_to_phys(entry));
430 }
431
432 /**
433 * qcom_scm_set_warm_boot_addr() - Set the warm boot address for cpus
434 * @entry: Entry point function for the cpus
435 * @cpus: The cpumask of cpus that will use the entry point
436 *
437 * Set the Linux entry point for the SCM to transfer control to when coming
438 * out of a power down. CPU power down may be executed on cpuidle or hotplug.
439 */
440 int __qcom_scm_set_warm_boot_addr(void *entry, const cpumask_t *cpus)
441 {
442 int ret;
443 int flags = 0;
444 int cpu;
445 struct {
446 __le32 flags;
447 __le32 addr;
448 } cmd;
449
450 /*
451 * Reassign only if we are switching from hotplug entry point
452 * to cpuidle entry point or vice versa.
453 */
454 for_each_cpu(cpu, cpus) {
455 if (entry == qcom_scm_wb[cpu].entry)
456 continue;
457 flags |= qcom_scm_wb[cpu].flag;
458 }
459
460 /* No change in entry function */
461 if (!flags)
462 return 0;
463
464 cmd.addr = cpu_to_le32(virt_to_phys(entry));
465 cmd.flags = cpu_to_le32(flags);
466 ret = qcom_scm_call(QCOM_SCM_SVC_BOOT, QCOM_SCM_BOOT_ADDR,
467 &cmd, sizeof(cmd), NULL, 0);
468 if (!ret) {
469 for_each_cpu(cpu, cpus)
470 qcom_scm_wb[cpu].entry = entry;
471 }
472
473 return ret;
474 }
475
476 /**
477 * qcom_scm_cpu_power_down() - Power down the cpu
478 * @flags - Flags to flush cache
479 *
480 * This is an end point to power down cpu. If there was a pending interrupt,
481 * the control would return from this function, otherwise, the cpu jumps to the
482 * warm boot entry point set for this cpu upon reset.
483 */
484 void __qcom_scm_cpu_power_down(u32 flags)
485 {
486 qcom_scm_call_atomic1(QCOM_SCM_SVC_BOOT, QCOM_SCM_CMD_TERMINATE_PC,
487 flags & QCOM_SCM_FLUSH_FLAG_MASK);
488 }
489
490 int __qcom_scm_is_call_available(u32 svc_id, u32 cmd_id)
491 {
492 int ret;
493 __le32 svc_cmd = cpu_to_le32((svc_id << 10) | cmd_id);
494 __le32 ret_val = 0;
495
496 ret = qcom_scm_call(QCOM_SCM_SVC_INFO, QCOM_IS_CALL_AVAIL_CMD, &svc_cmd,
497 sizeof(svc_cmd), &ret_val, sizeof(ret_val));
498 if (ret)
499 return ret;
500
501 return le32_to_cpu(ret_val);
502 }
503
504 int __qcom_scm_hdcp_req(struct qcom_scm_hdcp_req *req, u32 req_cnt, u32 *resp)
505 {
506 if (req_cnt > QCOM_SCM_HDCP_MAX_REQ_CNT)
507 return -ERANGE;
508
509 return qcom_scm_call(QCOM_SCM_SVC_HDCP, QCOM_SCM_CMD_HDCP,
510 req, req_cnt * sizeof(*req), resp, sizeof(*resp));
511 }