kpacket_gen: use constants for cmdlength
[cor.git] / drivers / firmware / qcom_scm-64.c
blob3c5850350974dc99fe85d3a36583604349c5c099
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2015, The Linux Foundation. All rights reserved.
3 */
5 #include <linux/io.h>
6 #include <linux/errno.h>
7 #include <linux/delay.h>
8 #include <linux/mutex.h>
9 #include <linux/slab.h>
10 #include <linux/types.h>
11 #include <linux/qcom_scm.h>
12 #include <linux/arm-smccc.h>
13 #include <linux/dma-mapping.h>
15 #include "qcom_scm.h"
17 #define QCOM_SCM_FNID(s, c) ((((s) & 0xFF) << 8) | ((c) & 0xFF))
19 #define MAX_QCOM_SCM_ARGS 10
20 #define MAX_QCOM_SCM_RETS 3
22 enum qcom_scm_arg_types {
23 QCOM_SCM_VAL,
24 QCOM_SCM_RO,
25 QCOM_SCM_RW,
26 QCOM_SCM_BUFVAL,
29 #define QCOM_SCM_ARGS_IMPL(num, a, b, c, d, e, f, g, h, i, j, ...) (\
30 (((a) & 0x3) << 4) | \
31 (((b) & 0x3) << 6) | \
32 (((c) & 0x3) << 8) | \
33 (((d) & 0x3) << 10) | \
34 (((e) & 0x3) << 12) | \
35 (((f) & 0x3) << 14) | \
36 (((g) & 0x3) << 16) | \
37 (((h) & 0x3) << 18) | \
38 (((i) & 0x3) << 20) | \
39 (((j) & 0x3) << 22) | \
40 ((num) & 0xf))
42 #define QCOM_SCM_ARGS(...) QCOM_SCM_ARGS_IMPL(__VA_ARGS__, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
44 /**
45 * struct qcom_scm_desc
46 * @arginfo: Metadata describing the arguments in args[]
47 * @args: The array of arguments for the secure syscall
48 * @res: The values returned by the secure syscall
50 struct qcom_scm_desc {
51 u32 arginfo;
52 u64 args[MAX_QCOM_SCM_ARGS];
55 static u64 qcom_smccc_convention = -1;
56 static DEFINE_MUTEX(qcom_scm_lock);
58 #define QCOM_SCM_EBUSY_WAIT_MS 30
59 #define QCOM_SCM_EBUSY_MAX_RETRY 20
61 #define N_EXT_QCOM_SCM_ARGS 7
62 #define FIRST_EXT_ARG_IDX 3
63 #define N_REGISTER_ARGS (MAX_QCOM_SCM_ARGS - N_EXT_QCOM_SCM_ARGS + 1)
65 static void __qcom_scm_call_do(const struct qcom_scm_desc *desc,
66 struct arm_smccc_res *res, u32 fn_id,
67 u64 x5, u32 type)
69 u64 cmd;
70 struct arm_smccc_quirk quirk = { .id = ARM_SMCCC_QUIRK_QCOM_A6 };
72 cmd = ARM_SMCCC_CALL_VAL(type, qcom_smccc_convention,
73 ARM_SMCCC_OWNER_SIP, fn_id);
75 quirk.state.a6 = 0;
77 do {
78 arm_smccc_smc_quirk(cmd, desc->arginfo, desc->args[0],
79 desc->args[1], desc->args[2], x5,
80 quirk.state.a6, 0, res, &quirk);
82 if (res->a0 == QCOM_SCM_INTERRUPTED)
83 cmd = res->a0;
85 } while (res->a0 == QCOM_SCM_INTERRUPTED);
88 static void qcom_scm_call_do(const struct qcom_scm_desc *desc,
89 struct arm_smccc_res *res, u32 fn_id,
90 u64 x5, bool atomic)
92 int retry_count = 0;
94 if (atomic) {
95 __qcom_scm_call_do(desc, res, fn_id, x5, ARM_SMCCC_FAST_CALL);
96 return;
99 do {
100 mutex_lock(&qcom_scm_lock);
102 __qcom_scm_call_do(desc, res, fn_id, x5,
103 ARM_SMCCC_STD_CALL);
105 mutex_unlock(&qcom_scm_lock);
107 if (res->a0 == QCOM_SCM_V2_EBUSY) {
108 if (retry_count++ > QCOM_SCM_EBUSY_MAX_RETRY)
109 break;
110 msleep(QCOM_SCM_EBUSY_WAIT_MS);
112 } while (res->a0 == QCOM_SCM_V2_EBUSY);
115 static int ___qcom_scm_call(struct device *dev, u32 svc_id, u32 cmd_id,
116 const struct qcom_scm_desc *desc,
117 struct arm_smccc_res *res, bool atomic)
119 int arglen = desc->arginfo & 0xf;
120 int i;
121 u32 fn_id = QCOM_SCM_FNID(svc_id, cmd_id);
122 u64 x5 = desc->args[FIRST_EXT_ARG_IDX];
123 dma_addr_t args_phys = 0;
124 void *args_virt = NULL;
125 size_t alloc_len;
126 gfp_t flag = atomic ? GFP_ATOMIC : GFP_KERNEL;
128 if (unlikely(arglen > N_REGISTER_ARGS)) {
129 alloc_len = N_EXT_QCOM_SCM_ARGS * sizeof(u64);
130 args_virt = kzalloc(PAGE_ALIGN(alloc_len), flag);
132 if (!args_virt)
133 return -ENOMEM;
135 if (qcom_smccc_convention == ARM_SMCCC_SMC_32) {
136 __le32 *args = args_virt;
138 for (i = 0; i < N_EXT_QCOM_SCM_ARGS; i++)
139 args[i] = cpu_to_le32(desc->args[i +
140 FIRST_EXT_ARG_IDX]);
141 } else {
142 __le64 *args = args_virt;
144 for (i = 0; i < N_EXT_QCOM_SCM_ARGS; i++)
145 args[i] = cpu_to_le64(desc->args[i +
146 FIRST_EXT_ARG_IDX]);
149 args_phys = dma_map_single(dev, args_virt, alloc_len,
150 DMA_TO_DEVICE);
152 if (dma_mapping_error(dev, args_phys)) {
153 kfree(args_virt);
154 return -ENOMEM;
157 x5 = args_phys;
160 qcom_scm_call_do(desc, res, fn_id, x5, atomic);
162 if (args_virt) {
163 dma_unmap_single(dev, args_phys, alloc_len, DMA_TO_DEVICE);
164 kfree(args_virt);
167 if ((long)res->a0 < 0)
168 return qcom_scm_remap_error(res->a0);
170 return 0;
174 * qcom_scm_call() - Invoke a syscall in the secure world
175 * @dev: device
176 * @svc_id: service identifier
177 * @cmd_id: command identifier
178 * @desc: Descriptor structure containing arguments and return values
180 * Sends a command to the SCM and waits for the command to finish processing.
181 * This should *only* be called in pre-emptible context.
183 static int qcom_scm_call(struct device *dev, u32 svc_id, u32 cmd_id,
184 const struct qcom_scm_desc *desc,
185 struct arm_smccc_res *res)
187 might_sleep();
188 return ___qcom_scm_call(dev, svc_id, cmd_id, desc, res, false);
192 * qcom_scm_call_atomic() - atomic variation of qcom_scm_call()
193 * @dev: device
194 * @svc_id: service identifier
195 * @cmd_id: command identifier
196 * @desc: Descriptor structure containing arguments and return values
197 * @res: Structure containing results from SMC/HVC call
199 * Sends a command to the SCM and waits for the command to finish processing.
200 * This can be called in atomic context.
202 static int qcom_scm_call_atomic(struct device *dev, u32 svc_id, u32 cmd_id,
203 const struct qcom_scm_desc *desc,
204 struct arm_smccc_res *res)
206 return ___qcom_scm_call(dev, svc_id, cmd_id, desc, res, true);
210 * qcom_scm_set_cold_boot_addr() - Set the cold boot address for cpus
211 * @entry: Entry point function for the cpus
212 * @cpus: The cpumask of cpus that will use the entry point
214 * Set the cold boot address of the cpus. Any cpu outside the supported
215 * range would be removed from the cpu present mask.
217 int __qcom_scm_set_cold_boot_addr(void *entry, const cpumask_t *cpus)
219 return -ENOTSUPP;
223 * qcom_scm_set_warm_boot_addr() - Set the warm boot address for cpus
224 * @dev: Device pointer
225 * @entry: Entry point function for the cpus
226 * @cpus: The cpumask of cpus that will use the entry point
228 * Set the Linux entry point for the SCM to transfer control to when coming
229 * out of a power down. CPU power down may be executed on cpuidle or hotplug.
231 int __qcom_scm_set_warm_boot_addr(struct device *dev, void *entry,
232 const cpumask_t *cpus)
234 return -ENOTSUPP;
238 * qcom_scm_cpu_power_down() - Power down the cpu
239 * @flags - Flags to flush cache
241 * This is an end point to power down cpu. If there was a pending interrupt,
242 * the control would return from this function, otherwise, the cpu jumps to the
243 * warm boot entry point set for this cpu upon reset.
245 void __qcom_scm_cpu_power_down(u32 flags)
249 int __qcom_scm_is_call_available(struct device *dev, u32 svc_id, u32 cmd_id)
251 int ret;
252 struct qcom_scm_desc desc = {0};
253 struct arm_smccc_res res;
255 desc.arginfo = QCOM_SCM_ARGS(1);
256 desc.args[0] = QCOM_SCM_FNID(svc_id, cmd_id) |
257 (ARM_SMCCC_OWNER_SIP << ARM_SMCCC_OWNER_SHIFT);
259 ret = qcom_scm_call(dev, QCOM_SCM_SVC_INFO, QCOM_IS_CALL_AVAIL_CMD,
260 &desc, &res);
262 return ret ? : res.a1;
265 int __qcom_scm_hdcp_req(struct device *dev, struct qcom_scm_hdcp_req *req,
266 u32 req_cnt, u32 *resp)
268 int ret;
269 struct qcom_scm_desc desc = {0};
270 struct arm_smccc_res res;
272 if (req_cnt > QCOM_SCM_HDCP_MAX_REQ_CNT)
273 return -ERANGE;
275 desc.args[0] = req[0].addr;
276 desc.args[1] = req[0].val;
277 desc.args[2] = req[1].addr;
278 desc.args[3] = req[1].val;
279 desc.args[4] = req[2].addr;
280 desc.args[5] = req[2].val;
281 desc.args[6] = req[3].addr;
282 desc.args[7] = req[3].val;
283 desc.args[8] = req[4].addr;
284 desc.args[9] = req[4].val;
285 desc.arginfo = QCOM_SCM_ARGS(10);
287 ret = qcom_scm_call(dev, QCOM_SCM_SVC_HDCP, QCOM_SCM_CMD_HDCP, &desc,
288 &res);
289 *resp = res.a1;
291 return ret;
294 int __qcom_scm_ocmem_lock(struct device *dev, uint32_t id, uint32_t offset,
295 uint32_t size, uint32_t mode)
297 return -ENOTSUPP;
300 int __qcom_scm_ocmem_unlock(struct device *dev, uint32_t id, uint32_t offset,
301 uint32_t size)
303 return -ENOTSUPP;
306 void __qcom_scm_init(void)
308 u64 cmd;
309 struct arm_smccc_res res;
310 u32 function = QCOM_SCM_FNID(QCOM_SCM_SVC_INFO, QCOM_IS_CALL_AVAIL_CMD);
312 /* First try a SMC64 call */
313 cmd = ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_64,
314 ARM_SMCCC_OWNER_SIP, function);
316 arm_smccc_smc(cmd, QCOM_SCM_ARGS(1), cmd & (~BIT(ARM_SMCCC_TYPE_SHIFT)),
317 0, 0, 0, 0, 0, &res);
319 if (!res.a0 && res.a1)
320 qcom_smccc_convention = ARM_SMCCC_SMC_64;
321 else
322 qcom_smccc_convention = ARM_SMCCC_SMC_32;
325 bool __qcom_scm_pas_supported(struct device *dev, u32 peripheral)
327 int ret;
328 struct qcom_scm_desc desc = {0};
329 struct arm_smccc_res res;
331 desc.args[0] = peripheral;
332 desc.arginfo = QCOM_SCM_ARGS(1);
334 ret = qcom_scm_call(dev, QCOM_SCM_SVC_PIL,
335 QCOM_SCM_PAS_IS_SUPPORTED_CMD,
336 &desc, &res);
338 return ret ? false : !!res.a1;
341 int __qcom_scm_pas_init_image(struct device *dev, u32 peripheral,
342 dma_addr_t metadata_phys)
344 int ret;
345 struct qcom_scm_desc desc = {0};
346 struct arm_smccc_res res;
348 desc.args[0] = peripheral;
349 desc.args[1] = metadata_phys;
350 desc.arginfo = QCOM_SCM_ARGS(2, QCOM_SCM_VAL, QCOM_SCM_RW);
352 ret = qcom_scm_call(dev, QCOM_SCM_SVC_PIL, QCOM_SCM_PAS_INIT_IMAGE_CMD,
353 &desc, &res);
355 return ret ? : res.a1;
358 int __qcom_scm_pas_mem_setup(struct device *dev, u32 peripheral,
359 phys_addr_t addr, phys_addr_t size)
361 int ret;
362 struct qcom_scm_desc desc = {0};
363 struct arm_smccc_res res;
365 desc.args[0] = peripheral;
366 desc.args[1] = addr;
367 desc.args[2] = size;
368 desc.arginfo = QCOM_SCM_ARGS(3);
370 ret = qcom_scm_call(dev, QCOM_SCM_SVC_PIL, QCOM_SCM_PAS_MEM_SETUP_CMD,
371 &desc, &res);
373 return ret ? : res.a1;
376 int __qcom_scm_pas_auth_and_reset(struct device *dev, u32 peripheral)
378 int ret;
379 struct qcom_scm_desc desc = {0};
380 struct arm_smccc_res res;
382 desc.args[0] = peripheral;
383 desc.arginfo = QCOM_SCM_ARGS(1);
385 ret = qcom_scm_call(dev, QCOM_SCM_SVC_PIL,
386 QCOM_SCM_PAS_AUTH_AND_RESET_CMD,
387 &desc, &res);
389 return ret ? : res.a1;
392 int __qcom_scm_pas_shutdown(struct device *dev, u32 peripheral)
394 int ret;
395 struct qcom_scm_desc desc = {0};
396 struct arm_smccc_res res;
398 desc.args[0] = peripheral;
399 desc.arginfo = QCOM_SCM_ARGS(1);
401 ret = qcom_scm_call(dev, QCOM_SCM_SVC_PIL, QCOM_SCM_PAS_SHUTDOWN_CMD,
402 &desc, &res);
404 return ret ? : res.a1;
407 int __qcom_scm_pas_mss_reset(struct device *dev, bool reset)
409 struct qcom_scm_desc desc = {0};
410 struct arm_smccc_res res;
411 int ret;
413 desc.args[0] = reset;
414 desc.args[1] = 0;
415 desc.arginfo = QCOM_SCM_ARGS(2);
417 ret = qcom_scm_call(dev, QCOM_SCM_SVC_PIL, QCOM_SCM_PAS_MSS_RESET, &desc,
418 &res);
420 return ret ? : res.a1;
423 int __qcom_scm_set_remote_state(struct device *dev, u32 state, u32 id)
425 struct qcom_scm_desc desc = {0};
426 struct arm_smccc_res res;
427 int ret;
429 desc.args[0] = state;
430 desc.args[1] = id;
431 desc.arginfo = QCOM_SCM_ARGS(2);
433 ret = qcom_scm_call(dev, QCOM_SCM_SVC_BOOT, QCOM_SCM_SET_REMOTE_STATE,
434 &desc, &res);
436 return ret ? : res.a1;
439 int __qcom_scm_assign_mem(struct device *dev, phys_addr_t mem_region,
440 size_t mem_sz, phys_addr_t src, size_t src_sz,
441 phys_addr_t dest, size_t dest_sz)
443 int ret;
444 struct qcom_scm_desc desc = {0};
445 struct arm_smccc_res res;
447 desc.args[0] = mem_region;
448 desc.args[1] = mem_sz;
449 desc.args[2] = src;
450 desc.args[3] = src_sz;
451 desc.args[4] = dest;
452 desc.args[5] = dest_sz;
453 desc.args[6] = 0;
455 desc.arginfo = QCOM_SCM_ARGS(7, QCOM_SCM_RO, QCOM_SCM_VAL,
456 QCOM_SCM_RO, QCOM_SCM_VAL, QCOM_SCM_RO,
457 QCOM_SCM_VAL, QCOM_SCM_VAL);
459 ret = qcom_scm_call(dev, QCOM_SCM_SVC_MP,
460 QCOM_MEM_PROT_ASSIGN_ID,
461 &desc, &res);
463 return ret ? : res.a1;
466 int __qcom_scm_restore_sec_cfg(struct device *dev, u32 device_id, u32 spare)
468 struct qcom_scm_desc desc = {0};
469 struct arm_smccc_res res;
470 int ret;
472 desc.args[0] = device_id;
473 desc.args[1] = spare;
474 desc.arginfo = QCOM_SCM_ARGS(2);
476 ret = qcom_scm_call(dev, QCOM_SCM_SVC_MP, QCOM_SCM_RESTORE_SEC_CFG,
477 &desc, &res);
479 return ret ? : res.a1;
482 int __qcom_scm_iommu_secure_ptbl_size(struct device *dev, u32 spare,
483 size_t *size)
485 struct qcom_scm_desc desc = {0};
486 struct arm_smccc_res res;
487 int ret;
489 desc.args[0] = spare;
490 desc.arginfo = QCOM_SCM_ARGS(1);
492 ret = qcom_scm_call(dev, QCOM_SCM_SVC_MP,
493 QCOM_SCM_IOMMU_SECURE_PTBL_SIZE, &desc, &res);
495 if (size)
496 *size = res.a1;
498 return ret ? : res.a2;
501 int __qcom_scm_iommu_secure_ptbl_init(struct device *dev, u64 addr, u32 size,
502 u32 spare)
504 struct qcom_scm_desc desc = {0};
505 struct arm_smccc_res res;
506 int ret;
508 desc.args[0] = addr;
509 desc.args[1] = size;
510 desc.args[2] = spare;
511 desc.arginfo = QCOM_SCM_ARGS(3, QCOM_SCM_RW, QCOM_SCM_VAL,
512 QCOM_SCM_VAL);
514 ret = qcom_scm_call(dev, QCOM_SCM_SVC_MP,
515 QCOM_SCM_IOMMU_SECURE_PTBL_INIT, &desc, &res);
517 /* the pg table has been initialized already, ignore the error */
518 if (ret == -EPERM)
519 ret = 0;
521 return ret;
524 int __qcom_scm_set_dload_mode(struct device *dev, bool enable)
526 struct qcom_scm_desc desc = {0};
527 struct arm_smccc_res res;
529 desc.args[0] = QCOM_SCM_SET_DLOAD_MODE;
530 desc.args[1] = enable ? QCOM_SCM_SET_DLOAD_MODE : 0;
531 desc.arginfo = QCOM_SCM_ARGS(2);
533 return qcom_scm_call(dev, QCOM_SCM_SVC_BOOT, QCOM_SCM_SET_DLOAD_MODE,
534 &desc, &res);
537 int __qcom_scm_io_readl(struct device *dev, phys_addr_t addr,
538 unsigned int *val)
540 struct qcom_scm_desc desc = {0};
541 struct arm_smccc_res res;
542 int ret;
544 desc.args[0] = addr;
545 desc.arginfo = QCOM_SCM_ARGS(1);
547 ret = qcom_scm_call(dev, QCOM_SCM_SVC_IO, QCOM_SCM_IO_READ,
548 &desc, &res);
549 if (ret >= 0)
550 *val = res.a1;
552 return ret < 0 ? ret : 0;
555 int __qcom_scm_io_writel(struct device *dev, phys_addr_t addr, unsigned int val)
557 struct qcom_scm_desc desc = {0};
558 struct arm_smccc_res res;
560 desc.args[0] = addr;
561 desc.args[1] = val;
562 desc.arginfo = QCOM_SCM_ARGS(2);
564 return qcom_scm_call(dev, QCOM_SCM_SVC_IO, QCOM_SCM_IO_WRITE,
565 &desc, &res);
568 int __qcom_scm_qsmmu500_wait_safe_toggle(struct device *dev, bool en)
570 struct qcom_scm_desc desc = {0};
571 struct arm_smccc_res res;
573 desc.args[0] = QCOM_SCM_CONFIG_ERRATA1_CLIENT_ALL;
574 desc.args[1] = en;
575 desc.arginfo = QCOM_SCM_ARGS(2);
577 return qcom_scm_call_atomic(dev, QCOM_SCM_SVC_SMMU_PROGRAM,
578 QCOM_SCM_CONFIG_ERRATA1, &desc, &res);