treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / firmware / qcom_scm-32.c
blob48e2ef794ea3cb8a5b2cc6b44cfcdb23f0ab1dd8
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2010,2015, The Linux Foundation. All rights reserved.
3 * Copyright (C) 2015 Linaro Ltd.
4 */
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>
13 #include <linux/dma-mapping.h>
15 #include "qcom_scm.h"
17 #define QCOM_SCM_FLAG_COLDBOOT_CPU0 0x00
18 #define QCOM_SCM_FLAG_COLDBOOT_CPU1 0x01
19 #define QCOM_SCM_FLAG_COLDBOOT_CPU2 0x08
20 #define QCOM_SCM_FLAG_COLDBOOT_CPU3 0x20
22 #define QCOM_SCM_FLAG_WARMBOOT_CPU0 0x04
23 #define QCOM_SCM_FLAG_WARMBOOT_CPU1 0x02
24 #define QCOM_SCM_FLAG_WARMBOOT_CPU2 0x10
25 #define QCOM_SCM_FLAG_WARMBOOT_CPU3 0x40
27 struct qcom_scm_entry {
28 int flag;
29 void *entry;
32 static struct qcom_scm_entry qcom_scm_wb[] = {
33 { .flag = QCOM_SCM_FLAG_WARMBOOT_CPU0 },
34 { .flag = QCOM_SCM_FLAG_WARMBOOT_CPU1 },
35 { .flag = QCOM_SCM_FLAG_WARMBOOT_CPU2 },
36 { .flag = QCOM_SCM_FLAG_WARMBOOT_CPU3 },
39 static DEFINE_MUTEX(qcom_scm_lock);
41 /**
42 * struct qcom_scm_command - one SCM command buffer
43 * @len: total available memory for command and response
44 * @buf_offset: start of command buffer
45 * @resp_hdr_offset: start of response buffer
46 * @id: command to be executed
47 * @buf: buffer returned from qcom_scm_get_command_buffer()
49 * An SCM command is laid out in memory as follows:
51 * ------------------- <--- struct qcom_scm_command
52 * | command header |
53 * ------------------- <--- qcom_scm_get_command_buffer()
54 * | command buffer |
55 * ------------------- <--- struct qcom_scm_response and
56 * | response header | qcom_scm_command_to_response()
57 * ------------------- <--- qcom_scm_get_response_buffer()
58 * | response buffer |
59 * -------------------
61 * There can be arbitrary padding between the headers and buffers so
62 * you should always use the appropriate qcom_scm_get_*_buffer() routines
63 * to access the buffers in a safe manner.
65 struct qcom_scm_command {
66 __le32 len;
67 __le32 buf_offset;
68 __le32 resp_hdr_offset;
69 __le32 id;
70 __le32 buf[0];
73 /**
74 * struct qcom_scm_response - one SCM response buffer
75 * @len: total available memory for response
76 * @buf_offset: start of response data relative to start of qcom_scm_response
77 * @is_complete: indicates if the command has finished processing
79 struct qcom_scm_response {
80 __le32 len;
81 __le32 buf_offset;
82 __le32 is_complete;
85 /**
86 * qcom_scm_command_to_response() - Get a pointer to a qcom_scm_response
87 * @cmd: command
89 * Returns a pointer to a response for a command.
91 static inline struct qcom_scm_response *qcom_scm_command_to_response(
92 const struct qcom_scm_command *cmd)
94 return (void *)cmd + le32_to_cpu(cmd->resp_hdr_offset);
97 /**
98 * qcom_scm_get_command_buffer() - Get a pointer to a command buffer
99 * @cmd: command
101 * Returns a pointer to the command buffer of a command.
103 static inline void *qcom_scm_get_command_buffer(const struct qcom_scm_command *cmd)
105 return (void *)cmd->buf;
109 * qcom_scm_get_response_buffer() - Get a pointer to a response buffer
110 * @rsp: response
112 * Returns a pointer to a response buffer of a response.
114 static inline void *qcom_scm_get_response_buffer(const struct qcom_scm_response *rsp)
116 return (void *)rsp + le32_to_cpu(rsp->buf_offset);
119 static u32 smc(u32 cmd_addr)
121 int context_id;
122 register u32 r0 asm("r0") = 1;
123 register u32 r1 asm("r1") = (u32)&context_id;
124 register u32 r2 asm("r2") = cmd_addr;
125 do {
126 asm volatile(
127 __asmeq("%0", "r0")
128 __asmeq("%1", "r0")
129 __asmeq("%2", "r1")
130 __asmeq("%3", "r2")
131 #ifdef REQUIRES_SEC
132 ".arch_extension sec\n"
133 #endif
134 "smc #0 @ switch to secure world\n"
135 : "=r" (r0)
136 : "r" (r0), "r" (r1), "r" (r2)
137 : "r3", "r12");
138 } while (r0 == QCOM_SCM_INTERRUPTED);
140 return r0;
144 * qcom_scm_call() - Send an SCM command
145 * @dev: struct device
146 * @svc_id: service identifier
147 * @cmd_id: command identifier
148 * @cmd_buf: command buffer
149 * @cmd_len: length of the command buffer
150 * @resp_buf: response buffer
151 * @resp_len: length of the response buffer
153 * Sends a command to the SCM and waits for the command to finish processing.
155 * A note on cache maintenance:
156 * Note that any buffers that are expected to be accessed by the secure world
157 * must be flushed before invoking qcom_scm_call and invalidated in the cache
158 * immediately after qcom_scm_call returns. Cache maintenance on the command
159 * and response buffers is taken care of by qcom_scm_call; however, callers are
160 * responsible for any other cached buffers passed over to the secure world.
162 static int qcom_scm_call(struct device *dev, u32 svc_id, u32 cmd_id,
163 const void *cmd_buf, size_t cmd_len, void *resp_buf,
164 size_t resp_len)
166 int ret;
167 struct qcom_scm_command *cmd;
168 struct qcom_scm_response *rsp;
169 size_t alloc_len = sizeof(*cmd) + cmd_len + sizeof(*rsp) + resp_len;
170 dma_addr_t cmd_phys;
172 cmd = kzalloc(PAGE_ALIGN(alloc_len), GFP_KERNEL);
173 if (!cmd)
174 return -ENOMEM;
176 cmd->len = cpu_to_le32(alloc_len);
177 cmd->buf_offset = cpu_to_le32(sizeof(*cmd));
178 cmd->resp_hdr_offset = cpu_to_le32(sizeof(*cmd) + cmd_len);
180 cmd->id = cpu_to_le32((svc_id << 10) | cmd_id);
181 if (cmd_buf)
182 memcpy(qcom_scm_get_command_buffer(cmd), cmd_buf, cmd_len);
184 rsp = qcom_scm_command_to_response(cmd);
186 cmd_phys = dma_map_single(dev, cmd, alloc_len, DMA_TO_DEVICE);
187 if (dma_mapping_error(dev, cmd_phys)) {
188 kfree(cmd);
189 return -ENOMEM;
192 mutex_lock(&qcom_scm_lock);
193 ret = smc(cmd_phys);
194 if (ret < 0)
195 ret = qcom_scm_remap_error(ret);
196 mutex_unlock(&qcom_scm_lock);
197 if (ret)
198 goto out;
200 do {
201 dma_sync_single_for_cpu(dev, cmd_phys + sizeof(*cmd) + cmd_len,
202 sizeof(*rsp), DMA_FROM_DEVICE);
203 } while (!rsp->is_complete);
205 if (resp_buf) {
206 dma_sync_single_for_cpu(dev, cmd_phys + sizeof(*cmd) + cmd_len +
207 le32_to_cpu(rsp->buf_offset),
208 resp_len, DMA_FROM_DEVICE);
209 memcpy(resp_buf, qcom_scm_get_response_buffer(rsp),
210 resp_len);
212 out:
213 dma_unmap_single(dev, cmd_phys, alloc_len, DMA_TO_DEVICE);
214 kfree(cmd);
215 return ret;
218 #define SCM_CLASS_REGISTER (0x2 << 8)
219 #define SCM_MASK_IRQS BIT(5)
220 #define SCM_ATOMIC(svc, cmd, n) (((((svc) << 10)|((cmd) & 0x3ff)) << 12) | \
221 SCM_CLASS_REGISTER | \
222 SCM_MASK_IRQS | \
223 (n & 0xf))
226 * qcom_scm_call_atomic1() - Send an atomic SCM command with one argument
227 * @svc_id: service identifier
228 * @cmd_id: command identifier
229 * @arg1: first argument
231 * This shall only be used with commands that are guaranteed to be
232 * uninterruptable, atomic and SMP safe.
234 static s32 qcom_scm_call_atomic1(u32 svc, u32 cmd, u32 arg1)
236 int context_id;
238 register u32 r0 asm("r0") = SCM_ATOMIC(svc, cmd, 1);
239 register u32 r1 asm("r1") = (u32)&context_id;
240 register u32 r2 asm("r2") = arg1;
242 asm volatile(
243 __asmeq("%0", "r0")
244 __asmeq("%1", "r0")
245 __asmeq("%2", "r1")
246 __asmeq("%3", "r2")
247 #ifdef REQUIRES_SEC
248 ".arch_extension sec\n"
249 #endif
250 "smc #0 @ switch to secure world\n"
251 : "=r" (r0)
252 : "r" (r0), "r" (r1), "r" (r2)
253 : "r3", "r12");
254 return r0;
258 * qcom_scm_call_atomic2() - Send an atomic SCM command with two arguments
259 * @svc_id: service identifier
260 * @cmd_id: command identifier
261 * @arg1: first argument
262 * @arg2: second argument
264 * This shall only be used with commands that are guaranteed to be
265 * uninterruptable, atomic and SMP safe.
267 static s32 qcom_scm_call_atomic2(u32 svc, u32 cmd, u32 arg1, u32 arg2)
269 int context_id;
271 register u32 r0 asm("r0") = SCM_ATOMIC(svc, cmd, 2);
272 register u32 r1 asm("r1") = (u32)&context_id;
273 register u32 r2 asm("r2") = arg1;
274 register u32 r3 asm("r3") = arg2;
276 asm volatile(
277 __asmeq("%0", "r0")
278 __asmeq("%1", "r0")
279 __asmeq("%2", "r1")
280 __asmeq("%3", "r2")
281 __asmeq("%4", "r3")
282 #ifdef REQUIRES_SEC
283 ".arch_extension sec\n"
284 #endif
285 "smc #0 @ switch to secure world\n"
286 : "=r" (r0)
287 : "r" (r0), "r" (r1), "r" (r2), "r" (r3)
288 : "r12");
289 return r0;
292 u32 qcom_scm_get_version(void)
294 int context_id;
295 static u32 version = -1;
296 register u32 r0 asm("r0");
297 register u32 r1 asm("r1");
299 if (version != -1)
300 return version;
302 mutex_lock(&qcom_scm_lock);
304 r0 = 0x1 << 8;
305 r1 = (u32)&context_id;
306 do {
307 asm volatile(
308 __asmeq("%0", "r0")
309 __asmeq("%1", "r1")
310 __asmeq("%2", "r0")
311 __asmeq("%3", "r1")
312 #ifdef REQUIRES_SEC
313 ".arch_extension sec\n"
314 #endif
315 "smc #0 @ switch to secure world\n"
316 : "=r" (r0), "=r" (r1)
317 : "r" (r0), "r" (r1)
318 : "r2", "r3", "r12");
319 } while (r0 == QCOM_SCM_INTERRUPTED);
321 version = r1;
322 mutex_unlock(&qcom_scm_lock);
324 return version;
326 EXPORT_SYMBOL(qcom_scm_get_version);
329 * qcom_scm_set_cold_boot_addr() - Set the cold boot address for cpus
330 * @entry: Entry point function for the cpus
331 * @cpus: The cpumask of cpus that will use the entry point
333 * Set the cold boot address of the cpus. Any cpu outside the supported
334 * range would be removed from the cpu present mask.
336 int __qcom_scm_set_cold_boot_addr(void *entry, const cpumask_t *cpus)
338 int flags = 0;
339 int cpu;
340 int scm_cb_flags[] = {
341 QCOM_SCM_FLAG_COLDBOOT_CPU0,
342 QCOM_SCM_FLAG_COLDBOOT_CPU1,
343 QCOM_SCM_FLAG_COLDBOOT_CPU2,
344 QCOM_SCM_FLAG_COLDBOOT_CPU3,
347 if (!cpus || (cpus && cpumask_empty(cpus)))
348 return -EINVAL;
350 for_each_cpu(cpu, cpus) {
351 if (cpu < ARRAY_SIZE(scm_cb_flags))
352 flags |= scm_cb_flags[cpu];
353 else
354 set_cpu_present(cpu, false);
357 return qcom_scm_call_atomic2(QCOM_SCM_SVC_BOOT, QCOM_SCM_BOOT_ADDR,
358 flags, virt_to_phys(entry));
362 * qcom_scm_set_warm_boot_addr() - Set the warm boot address for cpus
363 * @entry: Entry point function for the cpus
364 * @cpus: The cpumask of cpus that will use the entry point
366 * Set the Linux entry point for the SCM to transfer control to when coming
367 * out of a power down. CPU power down may be executed on cpuidle or hotplug.
369 int __qcom_scm_set_warm_boot_addr(struct device *dev, void *entry,
370 const cpumask_t *cpus)
372 int ret;
373 int flags = 0;
374 int cpu;
375 struct {
376 __le32 flags;
377 __le32 addr;
378 } cmd;
381 * Reassign only if we are switching from hotplug entry point
382 * to cpuidle entry point or vice versa.
384 for_each_cpu(cpu, cpus) {
385 if (entry == qcom_scm_wb[cpu].entry)
386 continue;
387 flags |= qcom_scm_wb[cpu].flag;
390 /* No change in entry function */
391 if (!flags)
392 return 0;
394 cmd.addr = cpu_to_le32(virt_to_phys(entry));
395 cmd.flags = cpu_to_le32(flags);
396 ret = qcom_scm_call(dev, QCOM_SCM_SVC_BOOT, QCOM_SCM_BOOT_ADDR,
397 &cmd, sizeof(cmd), NULL, 0);
398 if (!ret) {
399 for_each_cpu(cpu, cpus)
400 qcom_scm_wb[cpu].entry = entry;
403 return ret;
407 * qcom_scm_cpu_power_down() - Power down the cpu
408 * @flags - Flags to flush cache
410 * This is an end point to power down cpu. If there was a pending interrupt,
411 * the control would return from this function, otherwise, the cpu jumps to the
412 * warm boot entry point set for this cpu upon reset.
414 void __qcom_scm_cpu_power_down(u32 flags)
416 qcom_scm_call_atomic1(QCOM_SCM_SVC_BOOT, QCOM_SCM_CMD_TERMINATE_PC,
417 flags & QCOM_SCM_FLUSH_FLAG_MASK);
420 int __qcom_scm_is_call_available(struct device *dev, u32 svc_id, u32 cmd_id)
422 int ret;
423 __le32 svc_cmd = cpu_to_le32((svc_id << 10) | cmd_id);
424 __le32 ret_val = 0;
426 ret = qcom_scm_call(dev, QCOM_SCM_SVC_INFO, QCOM_IS_CALL_AVAIL_CMD,
427 &svc_cmd, sizeof(svc_cmd), &ret_val,
428 sizeof(ret_val));
429 if (ret)
430 return ret;
432 return le32_to_cpu(ret_val);
435 int __qcom_scm_hdcp_req(struct device *dev, struct qcom_scm_hdcp_req *req,
436 u32 req_cnt, u32 *resp)
438 if (req_cnt > QCOM_SCM_HDCP_MAX_REQ_CNT)
439 return -ERANGE;
441 return qcom_scm_call(dev, QCOM_SCM_SVC_HDCP, QCOM_SCM_CMD_HDCP,
442 req, req_cnt * sizeof(*req), resp, sizeof(*resp));
445 int __qcom_scm_ocmem_lock(struct device *dev, u32 id, u32 offset, u32 size,
446 u32 mode)
448 struct ocmem_tz_lock {
449 __le32 id;
450 __le32 offset;
451 __le32 size;
452 __le32 mode;
453 } request;
455 request.id = cpu_to_le32(id);
456 request.offset = cpu_to_le32(offset);
457 request.size = cpu_to_le32(size);
458 request.mode = cpu_to_le32(mode);
460 return qcom_scm_call(dev, QCOM_SCM_OCMEM_SVC, QCOM_SCM_OCMEM_LOCK_CMD,
461 &request, sizeof(request), NULL, 0);
464 int __qcom_scm_ocmem_unlock(struct device *dev, u32 id, u32 offset, u32 size)
466 struct ocmem_tz_unlock {
467 __le32 id;
468 __le32 offset;
469 __le32 size;
470 } request;
472 request.id = cpu_to_le32(id);
473 request.offset = cpu_to_le32(offset);
474 request.size = cpu_to_le32(size);
476 return qcom_scm_call(dev, QCOM_SCM_OCMEM_SVC, QCOM_SCM_OCMEM_UNLOCK_CMD,
477 &request, sizeof(request), NULL, 0);
480 void __qcom_scm_init(void)
484 bool __qcom_scm_pas_supported(struct device *dev, u32 peripheral)
486 __le32 out;
487 __le32 in;
488 int ret;
490 in = cpu_to_le32(peripheral);
491 ret = qcom_scm_call(dev, QCOM_SCM_SVC_PIL,
492 QCOM_SCM_PAS_IS_SUPPORTED_CMD,
493 &in, sizeof(in),
494 &out, sizeof(out));
496 return ret ? false : !!out;
499 int __qcom_scm_pas_init_image(struct device *dev, u32 peripheral,
500 dma_addr_t metadata_phys)
502 __le32 scm_ret;
503 int ret;
504 struct {
505 __le32 proc;
506 __le32 image_addr;
507 } request;
509 request.proc = cpu_to_le32(peripheral);
510 request.image_addr = cpu_to_le32(metadata_phys);
512 ret = qcom_scm_call(dev, QCOM_SCM_SVC_PIL,
513 QCOM_SCM_PAS_INIT_IMAGE_CMD,
514 &request, sizeof(request),
515 &scm_ret, sizeof(scm_ret));
517 return ret ? : le32_to_cpu(scm_ret);
520 int __qcom_scm_pas_mem_setup(struct device *dev, u32 peripheral,
521 phys_addr_t addr, phys_addr_t size)
523 __le32 scm_ret;
524 int ret;
525 struct {
526 __le32 proc;
527 __le32 addr;
528 __le32 len;
529 } request;
531 request.proc = cpu_to_le32(peripheral);
532 request.addr = cpu_to_le32(addr);
533 request.len = cpu_to_le32(size);
535 ret = qcom_scm_call(dev, QCOM_SCM_SVC_PIL,
536 QCOM_SCM_PAS_MEM_SETUP_CMD,
537 &request, sizeof(request),
538 &scm_ret, sizeof(scm_ret));
540 return ret ? : le32_to_cpu(scm_ret);
543 int __qcom_scm_pas_auth_and_reset(struct device *dev, u32 peripheral)
545 __le32 out;
546 __le32 in;
547 int ret;
549 in = cpu_to_le32(peripheral);
550 ret = qcom_scm_call(dev, QCOM_SCM_SVC_PIL,
551 QCOM_SCM_PAS_AUTH_AND_RESET_CMD,
552 &in, sizeof(in),
553 &out, sizeof(out));
555 return ret ? : le32_to_cpu(out);
558 int __qcom_scm_pas_shutdown(struct device *dev, u32 peripheral)
560 __le32 out;
561 __le32 in;
562 int ret;
564 in = cpu_to_le32(peripheral);
565 ret = qcom_scm_call(dev, QCOM_SCM_SVC_PIL,
566 QCOM_SCM_PAS_SHUTDOWN_CMD,
567 &in, sizeof(in),
568 &out, sizeof(out));
570 return ret ? : le32_to_cpu(out);
573 int __qcom_scm_pas_mss_reset(struct device *dev, bool reset)
575 __le32 out;
576 __le32 in = cpu_to_le32(reset);
577 int ret;
579 ret = qcom_scm_call(dev, QCOM_SCM_SVC_PIL, QCOM_SCM_PAS_MSS_RESET,
580 &in, sizeof(in),
581 &out, sizeof(out));
583 return ret ? : le32_to_cpu(out);
586 int __qcom_scm_set_dload_mode(struct device *dev, bool enable)
588 return qcom_scm_call_atomic2(QCOM_SCM_SVC_BOOT, QCOM_SCM_SET_DLOAD_MODE,
589 enable ? QCOM_SCM_SET_DLOAD_MODE : 0, 0);
592 int __qcom_scm_set_remote_state(struct device *dev, u32 state, u32 id)
594 struct {
595 __le32 state;
596 __le32 id;
597 } req;
598 __le32 scm_ret = 0;
599 int ret;
601 req.state = cpu_to_le32(state);
602 req.id = cpu_to_le32(id);
604 ret = qcom_scm_call(dev, QCOM_SCM_SVC_BOOT, QCOM_SCM_SET_REMOTE_STATE,
605 &req, sizeof(req), &scm_ret, sizeof(scm_ret));
607 return ret ? : le32_to_cpu(scm_ret);
610 int __qcom_scm_assign_mem(struct device *dev, phys_addr_t mem_region,
611 size_t mem_sz, phys_addr_t src, size_t src_sz,
612 phys_addr_t dest, size_t dest_sz)
614 return -ENODEV;
617 int __qcom_scm_restore_sec_cfg(struct device *dev, u32 device_id,
618 u32 spare)
620 struct msm_scm_sec_cfg {
621 __le32 id;
622 __le32 ctx_bank_num;
623 } cfg;
624 int ret, scm_ret = 0;
626 cfg.id = cpu_to_le32(device_id);
627 cfg.ctx_bank_num = cpu_to_le32(spare);
629 ret = qcom_scm_call(dev, QCOM_SCM_SVC_MP, QCOM_SCM_RESTORE_SEC_CFG,
630 &cfg, sizeof(cfg), &scm_ret, sizeof(scm_ret));
632 if (ret || scm_ret)
633 return ret ? ret : -EINVAL;
635 return 0;
638 int __qcom_scm_iommu_secure_ptbl_size(struct device *dev, u32 spare,
639 size_t *size)
641 return -ENODEV;
644 int __qcom_scm_iommu_secure_ptbl_init(struct device *dev, u64 addr, u32 size,
645 u32 spare)
647 return -ENODEV;
650 int __qcom_scm_io_readl(struct device *dev, phys_addr_t addr,
651 unsigned int *val)
653 int ret;
655 ret = qcom_scm_call_atomic1(QCOM_SCM_SVC_IO, QCOM_SCM_IO_READ, addr);
656 if (ret >= 0)
657 *val = ret;
659 return ret < 0 ? ret : 0;
662 int __qcom_scm_io_writel(struct device *dev, phys_addr_t addr, unsigned int val)
664 return qcom_scm_call_atomic2(QCOM_SCM_SVC_IO, QCOM_SCM_IO_WRITE,
665 addr, val);
668 int __qcom_scm_qsmmu500_wait_safe_toggle(struct device *dev, bool enable)
670 return -ENODEV;