Debian Codegraph @

drivers/firmware/qcom/qcom_scm.c

raw
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2010,2015,2019 The Linux Foundation. All rights reserved.
3 * Copyright (C) 2015 Linaro Ltd.
4 */
5
6 #include <linux/arm-smccc.h>
7 #include <linux/bitfield.h>
8 #include <linux/bits.h>
9 #include <linux/cleanup.h>
10 #include <linux/clk.h>
11 #include <linux/completion.h>
12 #include <linux/cpumask.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/err.h>
15 #include <linux/export.h>
16 #include <linux/firmware/qcom/qcom_scm.h>
17 #include <linux/firmware/qcom/qcom_tzmem.h>
18 #include <linux/init.h>
19 #include <linux/interconnect.h>
20 #include <linux/interrupt.h>
21 #include <linux/kstrtox.h>
22 #include <linux/module.h>
23 #include <linux/of.h>
24 #include <linux/of_address.h>
25 #include <linux/of_irq.h>
26 #include <linux/of_platform.h>
27 #include <linux/of_reserved_mem.h>
28 #include <linux/platform_device.h>
29 #include <linux/reset-controller.h>
30 #include <linux/remoteproc.h>
31 #include <linux/sizes.h>
32 #include <linux/types.h>
33
34 #include <dt-bindings/interrupt-controller/arm-gic.h>
35
36 #include "qcom_scm.h"
37 #include "qcom_tzmem.h"
38
39 static u32 download_mode;
40
41 #define GIC_SPI_BASE 32
42 #define GIC_MAX_SPI 1019 // SPIs in GICv3 spec range from 32..1019
43 #define GIC_ESPI_BASE 4096
44 #define GIC_MAX_ESPI 5119 // ESPIs in GICv3 spec range from 4096..5119
45
46 struct qcom_scm {
47 struct device *dev;
48 struct clk *core_clk;
49 struct clk *iface_clk;
50 struct clk *bus_clk;
51 struct icc_path *path;
52 struct completion *waitq_comps;
53 struct reset_controller_dev reset;
54
55 /* control access to the interconnect path */
56 struct mutex scm_bw_lock;
57 int scm_vote_count;
58
59 u64 dload_mode_addr;
60
61 struct qcom_tzmem_pool *mempool;
62 unsigned int wq_cnt;
63 };
64
65 struct qcom_scm_current_perm_info {
66 __le32 vmid;
67 __le32 perm;
68 __le64 ctx;
69 __le32 ctx_size;
70 __le32 unused;
71 };
72
73 struct qcom_scm_mem_map_info {
74 __le64 mem_addr;
75 __le64 mem_size;
76 };
77
78 /**
79 * struct qcom_scm_qseecom_resp - QSEECOM SCM call response.
80 * @result: Result or status of the SCM call. See &enum qcom_scm_qseecom_result.
81 * @resp_type: Type of the response. See &enum qcom_scm_qseecom_resp_type.
82 * @data: Response data. The type of this data is given in @resp_type.
83 */
84 struct qcom_scm_qseecom_resp {
85 u64 result;
86 u64 resp_type;
87 u64 data;
88 };
89
90 enum qcom_scm_qseecom_result {
91 QSEECOM_RESULT_SUCCESS = 0,
92 QSEECOM_RESULT_INCOMPLETE = 1,
93 QSEECOM_RESULT_BLOCKED_ON_LISTENER = 2,
94 QSEECOM_RESULT_FAILURE = 0xFFFFFFFF,
95 };
96
97 enum qcom_scm_qseecom_resp_type {
98 QSEECOM_SCM_RES_APP_ID = 0xEE01,
99 QSEECOM_SCM_RES_QSEOS_LISTENER_ID = 0xEE02,
100 };
101
102 enum qcom_scm_qseecom_tz_owner {
103 QSEECOM_TZ_OWNER_SIP = 2,
104 QSEECOM_TZ_OWNER_TZ_APPS = 48,
105 QSEECOM_TZ_OWNER_QSEE_OS = 50
106 };
107
108 enum qcom_scm_qseecom_tz_svc {
109 QSEECOM_TZ_SVC_APP_ID_PLACEHOLDER = 0,
110 QSEECOM_TZ_SVC_APP_MGR = 1,
111 QSEECOM_TZ_SVC_INFO = 6,
112 };
113
114 enum qcom_scm_qseecom_tz_cmd_app {
115 QSEECOM_TZ_CMD_APP_SEND = 1,
116 QSEECOM_TZ_CMD_APP_LOOKUP = 3,
117 };
118
119 enum qcom_scm_qseecom_tz_cmd_info {
120 QSEECOM_TZ_CMD_INFO_VERSION = 3,
121 };
122
123 #define RSCTABLE_BUFFER_NOT_SUFFICIENT 20
124
125 #define QSEECOM_MAX_APP_NAME_SIZE 64
126 #define SHMBRIDGE_RESULT_NOTSUPP 4
127
128 /* Each bit configures cold/warm boot address for one of the 4 CPUs */
129 static const u8 qcom_scm_cpu_cold_bits[QCOM_SCM_BOOT_MAX_CPUS] = {
130 0, BIT(0), BIT(3), BIT(5)
131 };
132 static const u8 qcom_scm_cpu_warm_bits[QCOM_SCM_BOOT_MAX_CPUS] = {
133 BIT(2), BIT(1), BIT(4), BIT(6)
134 };
135
136 #define QCOM_SMC_WAITQ_FLAG_WAKE_ONE BIT(0)
137
138 #define QCOM_DLOAD_MASK GENMASK(5, 4)
139 #define QCOM_DLOAD_NODUMP 0
140 #define QCOM_DLOAD_FULLDUMP 1
141 #define QCOM_DLOAD_MINIDUMP 2
142 #define QCOM_DLOAD_BOTHDUMP 3
143
144 #define QCOM_SCM_DEFAULT_WAITQ_COUNT 1
145
146 static const char * const qcom_scm_convention_names[] = {
147 [SMC_CONVENTION_UNKNOWN] = "unknown",
148 [SMC_CONVENTION_ARM_32] = "smc arm 32",
149 [SMC_CONVENTION_ARM_64] = "smc arm 64",
150 [SMC_CONVENTION_LEGACY] = "smc legacy",
151 };
152
153 static const char * const download_mode_name[] = {
154 [QCOM_DLOAD_NODUMP] = "off",
155 [QCOM_DLOAD_FULLDUMP] = "full",
156 [QCOM_DLOAD_MINIDUMP] = "mini",
157 [QCOM_DLOAD_BOTHDUMP] = "full,mini",
158 };
159
160 static struct qcom_scm *__scm;
161
162 static int qcom_scm_clk_enable(void)
163 {
164 int ret;
165
166 ret = clk_prepare_enable(__scm->core_clk);
167 if (ret)
168 goto bail;
169
170 ret = clk_prepare_enable(__scm->iface_clk);
171 if (ret)
172 goto disable_core;
173
174 ret = clk_prepare_enable(__scm->bus_clk);
175 if (ret)
176 goto disable_iface;
177
178 return 0;
179
180 disable_iface:
181 clk_disable_unprepare(__scm->iface_clk);
182 disable_core:
183 clk_disable_unprepare(__scm->core_clk);
184 bail:
185 return ret;
186 }
187
188 static void qcom_scm_clk_disable(void)
189 {
190 clk_disable_unprepare(__scm->core_clk);
191 clk_disable_unprepare(__scm->iface_clk);
192 clk_disable_unprepare(__scm->bus_clk);
193 }
194
195 static int qcom_scm_bw_enable(void)
196 {
197 int ret = 0;
198
199 if (!__scm->path)
200 return 0;
201
202 mutex_lock(&__scm->scm_bw_lock);
203 if (!__scm->scm_vote_count) {
204 ret = icc_set_bw(__scm->path, 0, UINT_MAX);
205 if (ret < 0) {
206 dev_err(__scm->dev, "failed to set bandwidth request\n");
207 goto err_bw;
208 }
209 }
210 __scm->scm_vote_count++;
211 err_bw:
212 mutex_unlock(&__scm->scm_bw_lock);
213
214 return ret;
215 }
216
217 static void qcom_scm_bw_disable(void)
218 {
219 if (!__scm->path)
220 return;
221
222 mutex_lock(&__scm->scm_bw_lock);
223 if (__scm->scm_vote_count-- == 1)
224 icc_set_bw(__scm->path, 0, 0);
225 mutex_unlock(&__scm->scm_bw_lock);
226 }
227
228 enum qcom_scm_convention qcom_scm_convention = SMC_CONVENTION_UNKNOWN;
229 static DEFINE_SPINLOCK(scm_query_lock);
230
231 struct qcom_tzmem_pool *qcom_scm_get_tzmem_pool(void)
232 {
233 if (!qcom_scm_is_available())
234 return NULL;
235
236 return __scm->mempool;
237 }
238
239 static enum qcom_scm_convention __get_convention(void)
240 {
241 unsigned long flags;
242 struct qcom_scm_desc desc = {
243 .svc = QCOM_SCM_SVC_INFO,
244 .cmd = QCOM_SCM_INFO_IS_CALL_AVAIL,
245 .args[0] = SCM_SMC_FNID(QCOM_SCM_SVC_INFO,
246 QCOM_SCM_INFO_IS_CALL_AVAIL) |
247 (ARM_SMCCC_OWNER_SIP << ARM_SMCCC_OWNER_SHIFT),
248 .arginfo = QCOM_SCM_ARGS(1),
249 .owner = ARM_SMCCC_OWNER_SIP,
250 };
251 struct qcom_scm_res res;
252 enum qcom_scm_convention probed_convention;
253 int ret;
254 bool forced = false;
255
256 if (likely(qcom_scm_convention != SMC_CONVENTION_UNKNOWN))
257 return qcom_scm_convention;
258
259 /*
260 * Per the "SMC calling convention specification", the 64-bit calling
261 * convention can only be used when the client is 64-bit, otherwise
262 * system will encounter the undefined behaviour.
263 */
264 #if IS_ENABLED(CONFIG_ARM64)
265 /*
266 * Device isn't required as there is only one argument - no device
267 * needed to dma_map_single to secure world
268 */
269 probed_convention = SMC_CONVENTION_ARM_64;
270 ret = __scm_smc_call(NULL, &desc, probed_convention, &res, true);
271 if (!ret && res.result[0] == 1)
272 goto found;
273
274 /*
275 * Some SC7180 firmwares didn't implement the
276 * QCOM_SCM_INFO_IS_CALL_AVAIL call, so we fallback to forcing ARM_64
277 * calling conventions on these firmwares. Luckily we don't make any
278 * early calls into the firmware on these SoCs so the device pointer
279 * will be valid here to check if the compatible matches.
280 */
281 if (of_device_is_compatible(__scm ? __scm->dev->of_node : NULL, "qcom,scm-sc7180")) {
282 forced = true;
283 goto found;
284 }
285 #endif
286
287 probed_convention = SMC_CONVENTION_ARM_32;
288 ret = __scm_smc_call(NULL, &desc, probed_convention, &res, true);
289 if (!ret && res.result[0] == 1)
290 goto found;
291
292 probed_convention = SMC_CONVENTION_LEGACY;
293 found:
294 spin_lock_irqsave(&scm_query_lock, flags);
295 if (probed_convention != qcom_scm_convention) {
296 qcom_scm_convention = probed_convention;
297 pr_info("qcom_scm: convention: %s%s\n",
298 qcom_scm_convention_names[qcom_scm_convention],
299 forced ? " (forced)" : "");
300 }
301 spin_unlock_irqrestore(&scm_query_lock, flags);
302
303 return qcom_scm_convention;
304 }
305
306 /**
307 * qcom_scm_call() - Invoke a syscall in the secure world
308 * @dev: device
309 * @desc: Descriptor structure containing arguments and return values
310 * @res: Structure containing results from SMC/HVC call
311 *
312 * Sends a command to the SCM and waits for the command to finish processing.
313 * This should *only* be called in pre-emptible context.
314 */
315 static int qcom_scm_call(struct device *dev, const struct qcom_scm_desc *desc,
316 struct qcom_scm_res *res)
317 {
318 might_sleep();
319 switch (__get_convention()) {
320 case SMC_CONVENTION_ARM_32:
321 case SMC_CONVENTION_ARM_64:
322 return scm_smc_call(dev, desc, res, false);
323 case SMC_CONVENTION_LEGACY:
324 return scm_legacy_call(dev, desc, res);
325 default:
326 pr_err("Unknown current SCM calling convention.\n");
327 return -EINVAL;
328 }
329 }
330
331 /**
332 * qcom_scm_call_atomic() - atomic variation of qcom_scm_call()
333 * @dev: device
334 * @desc: Descriptor structure containing arguments and return values
335 * @res: Structure containing results from SMC/HVC call
336 *
337 * Sends a command to the SCM and waits for the command to finish processing.
338 * This can be called in atomic context.
339 */
340 static int qcom_scm_call_atomic(struct device *dev,
341 const struct qcom_scm_desc *desc,
342 struct qcom_scm_res *res)
343 {
344 switch (__get_convention()) {
345 case SMC_CONVENTION_ARM_32:
346 case SMC_CONVENTION_ARM_64:
347 return scm_smc_call(dev, desc, res, true);
348 case SMC_CONVENTION_LEGACY:
349 return scm_legacy_call_atomic(dev, desc, res);
350 default:
351 pr_err("Unknown current SCM calling convention.\n");
352 return -EINVAL;
353 }
354 }
355
356 static bool __qcom_scm_is_call_available(struct device *dev, u32 svc_id,
357 u32 cmd_id)
358 {
359 int ret;
360 struct qcom_scm_desc desc = {
361 .svc = QCOM_SCM_SVC_INFO,
362 .cmd = QCOM_SCM_INFO_IS_CALL_AVAIL,
363 .owner = ARM_SMCCC_OWNER_SIP,
364 };
365 struct qcom_scm_res res;
366
367 desc.arginfo = QCOM_SCM_ARGS(1);
368 switch (__get_convention()) {
369 case SMC_CONVENTION_ARM_32:
370 case SMC_CONVENTION_ARM_64:
371 desc.args[0] = SCM_SMC_FNID(svc_id, cmd_id) |
372 (ARM_SMCCC_OWNER_SIP << ARM_SMCCC_OWNER_SHIFT);
373 break;
374 case SMC_CONVENTION_LEGACY:
375 desc.args[0] = SCM_LEGACY_FNID(svc_id, cmd_id);
376 break;
377 default:
378 pr_err("Unknown SMC convention being used\n");
379 return false;
380 }
381
382 ret = qcom_scm_call(dev, &desc, &res);
383
384 return ret ? false : !!res.result[0];
385 }
386
387 static int qcom_scm_set_boot_addr(void *entry, const u8 *cpu_bits)
388 {
389 int cpu;
390 unsigned int flags = 0;
391 struct qcom_scm_desc desc = {
392 .svc = QCOM_SCM_SVC_BOOT,
393 .cmd = QCOM_SCM_BOOT_SET_ADDR,
394 .arginfo = QCOM_SCM_ARGS(2),
395 .owner = ARM_SMCCC_OWNER_SIP,
396 };
397
398 for_each_present_cpu(cpu) {
399 if (cpu >= QCOM_SCM_BOOT_MAX_CPUS)
400 return -EINVAL;
401 flags |= cpu_bits[cpu];
402 }
403
404 desc.args[0] = flags;
405 desc.args[1] = virt_to_phys(entry);
406
407 return qcom_scm_call_atomic(__scm ? __scm->dev : NULL, &desc, NULL);
408 }
409
410 static int qcom_scm_set_boot_addr_mc(void *entry, unsigned int flags)
411 {
412 struct qcom_scm_desc desc = {
413 .svc = QCOM_SCM_SVC_BOOT,
414 .cmd = QCOM_SCM_BOOT_SET_ADDR_MC,
415 .owner = ARM_SMCCC_OWNER_SIP,
416 .arginfo = QCOM_SCM_ARGS(6),
417 .args = {
418 virt_to_phys(entry),
419 /* Apply to all CPUs in all affinity levels */
420 ~0ULL, ~0ULL, ~0ULL, ~0ULL,
421 flags,
422 },
423 };
424
425 /* Need a device for DMA of the additional arguments */
426 if (!__scm || __get_convention() == SMC_CONVENTION_LEGACY)
427 return -EOPNOTSUPP;
428
429 return qcom_scm_call(__scm->dev, &desc, NULL);
430 }
431
432 /**
433 * qcom_scm_set_warm_boot_addr() - Set the warm boot address for all cpus
434 * @entry: Entry point function for the cpus
435 *
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.
438 */
439 int qcom_scm_set_warm_boot_addr(void *entry)
440 {
441 if (qcom_scm_set_boot_addr_mc(entry, QCOM_SCM_BOOT_MC_FLAG_WARMBOOT))
442 /* Fallback to old SCM call */
443 return qcom_scm_set_boot_addr(entry, qcom_scm_cpu_warm_bits);
444 return 0;
445 }
446 EXPORT_SYMBOL_GPL(qcom_scm_set_warm_boot_addr);
447
448 /**
449 * qcom_scm_set_cold_boot_addr() - Set the cold boot address for all cpus
450 * @entry: Entry point function for the cpus
451 */
452 int qcom_scm_set_cold_boot_addr(void *entry)
453 {
454 if (qcom_scm_set_boot_addr_mc(entry, QCOM_SCM_BOOT_MC_FLAG_COLDBOOT))
455 /* Fallback to old SCM call */
456 return qcom_scm_set_boot_addr(entry, qcom_scm_cpu_cold_bits);
457 return 0;
458 }
459 EXPORT_SYMBOL_GPL(qcom_scm_set_cold_boot_addr);
460
461 /**
462 * qcom_scm_cpu_power_down() - Power down the cpu
463 * @flags: Flags to flush cache
464 *
465 * This is an end point to power down cpu. If there was a pending interrupt,
466 * the control would return from this function, otherwise, the cpu jumps to the
467 * warm boot entry point set for this cpu upon reset.
468 */
469 void qcom_scm_cpu_power_down(u32 flags)
470 {
471 struct qcom_scm_desc desc = {
472 .svc = QCOM_SCM_SVC_BOOT,
473 .cmd = QCOM_SCM_BOOT_TERMINATE_PC,
474 .args[0] = flags & QCOM_SCM_FLUSH_FLAG_MASK,
475 .arginfo = QCOM_SCM_ARGS(1),
476 .owner = ARM_SMCCC_OWNER_SIP,
477 };
478
479 qcom_scm_call_atomic(__scm ? __scm->dev : NULL, &desc, NULL);
480 }
481 EXPORT_SYMBOL_GPL(qcom_scm_cpu_power_down);
482
483 int qcom_scm_set_remote_state(u32 state, u32 id)
484 {
485 struct qcom_scm_desc desc = {
486 .svc = QCOM_SCM_SVC_BOOT,
487 .cmd = QCOM_SCM_BOOT_SET_REMOTE_STATE,
488 .arginfo = QCOM_SCM_ARGS(2),
489 .args[0] = state,
490 .args[1] = id,
491 .owner = ARM_SMCCC_OWNER_SIP,
492 };
493 struct qcom_scm_res res;
494 int ret;
495
496 ret = qcom_scm_call(__scm->dev, &desc, &res);
497
498 return ret ? : res.result[0];
499 }
500 EXPORT_SYMBOL_GPL(qcom_scm_set_remote_state);
501
502 static int qcom_scm_disable_sdi(void)
503 {
504 int ret;
505 struct qcom_scm_desc desc = {
506 .svc = QCOM_SCM_SVC_BOOT,
507 .cmd = QCOM_SCM_BOOT_SDI_CONFIG,
508 .args[0] = 1, /* Disable watchdog debug */
509 .args[1] = 0, /* Disable SDI */
510 .arginfo = QCOM_SCM_ARGS(2),
511 .owner = ARM_SMCCC_OWNER_SIP,
512 };
513 struct qcom_scm_res res;
514
515 ret = qcom_scm_clk_enable();
516 if (ret)
517 return ret;
518 ret = qcom_scm_call(__scm->dev, &desc, &res);
519
520 qcom_scm_clk_disable();
521
522 return ret ? : res.result[0];
523 }
524
525 static int __qcom_scm_set_dload_mode(struct device *dev, bool enable)
526 {
527 struct qcom_scm_desc desc = {
528 .svc = QCOM_SCM_SVC_BOOT,
529 .cmd = QCOM_SCM_BOOT_SET_DLOAD_MODE,
530 .arginfo = QCOM_SCM_ARGS(2),
531 .args[0] = QCOM_SCM_BOOT_SET_DLOAD_MODE,
532 .owner = ARM_SMCCC_OWNER_SIP,
533 };
534
535 desc.args[1] = enable ? QCOM_SCM_BOOT_SET_DLOAD_MODE : 0;
536
537 return qcom_scm_call_atomic(__scm->dev, &desc, NULL);
538 }
539
540 static int qcom_scm_io_rmw(phys_addr_t addr, unsigned int mask, unsigned int val)
541 {
542 unsigned int old;
543 unsigned int new;
544 int ret;
545
546 ret = qcom_scm_io_readl(addr, &old);
547 if (ret)
548 return ret;
549
550 new = (old & ~mask) | (val & mask);
551
552 return qcom_scm_io_writel(addr, new);
553 }
554
555 static void qcom_scm_set_download_mode(u32 dload_mode)
556 {
557 int ret = 0;
558
559 if (__scm->dload_mode_addr) {
560 ret = qcom_scm_io_rmw(__scm->dload_mode_addr, QCOM_DLOAD_MASK,
561 FIELD_PREP(QCOM_DLOAD_MASK, dload_mode));
562 } else if (__qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_BOOT,
563 QCOM_SCM_BOOT_SET_DLOAD_MODE)) {
564 ret = __qcom_scm_set_dload_mode(__scm->dev, !!dload_mode);
565 } else if (dload_mode) {
566 dev_err(__scm->dev,
567 "No available mechanism for setting download mode\n");
568 }
569
570 if (ret)
571 dev_err(__scm->dev, "failed to set download mode: %d\n", ret);
572 }
573
574 /**
575 * devm_qcom_scm_pas_context_alloc() - Allocate peripheral authentication service
576 * context for a given peripheral
577 *
578 * PAS context is device-resource managed, so the caller does not need
579 * to worry about freeing the context memory.
580 *
581 * @dev: PAS firmware device
582 * @pas_id: peripheral authentication service id
583 * @mem_phys: Subsystem reserve memory start address
584 * @mem_size: Subsystem reserve memory size
585 *
586 * Returns: The new PAS context, or ERR_PTR() on failure.
587 */
588 struct qcom_scm_pas_context *devm_qcom_scm_pas_context_alloc(struct device *dev,
589 u32 pas_id,
590 phys_addr_t mem_phys,
591 size_t mem_size)
592 {
593 struct qcom_scm_pas_context *ctx;
594
595 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
596 if (!ctx)
597 return ERR_PTR(-ENOMEM);
598
599 ctx->dev = dev;
600 ctx->pas_id = pas_id;
601 ctx->mem_phys = mem_phys;
602 ctx->mem_size = mem_size;
603
604 return ctx;
605 }
606 EXPORT_SYMBOL_GPL(devm_qcom_scm_pas_context_alloc);
607
608 static int __qcom_scm_pas_init_image(u32 pas_id, dma_addr_t mdata_phys,
609 struct qcom_scm_res *res)
610 {
611 struct qcom_scm_desc desc = {
612 .svc = QCOM_SCM_SVC_PIL,
613 .cmd = QCOM_SCM_PIL_PAS_INIT_IMAGE,
614 .arginfo = QCOM_SCM_ARGS(2, QCOM_SCM_VAL, QCOM_SCM_RW),
615 .args[0] = pas_id,
616 .owner = ARM_SMCCC_OWNER_SIP,
617 };
618 int ret;
619
620 ret = qcom_scm_clk_enable();
621 if (ret)
622 return ret;
623
624 ret = qcom_scm_bw_enable();
625 if (ret)
626 goto disable_clk;
627
628 desc.args[1] = mdata_phys;
629
630 ret = qcom_scm_call(__scm->dev, &desc, res);
631 qcom_scm_bw_disable();
632
633 disable_clk:
634 qcom_scm_clk_disable();
635
636 return ret;
637 }
638
639 static int qcom_scm_pas_prep_and_init_image(struct qcom_scm_pas_context *ctx,
640 const void *metadata, size_t size)
641 {
642 struct qcom_scm_res res;
643 phys_addr_t mdata_phys;
644 void *mdata_buf;
645 int ret;
646
647 mdata_buf = qcom_tzmem_alloc(__scm->mempool, size, GFP_KERNEL);
648 if (!mdata_buf)
649 return -ENOMEM;
650
651 memcpy(mdata_buf, metadata, size);
652 mdata_phys = qcom_tzmem_to_phys(mdata_buf);
653
654 ret = __qcom_scm_pas_init_image(ctx->pas_id, mdata_phys, &res);
655 if (ret < 0)
656 qcom_tzmem_free(mdata_buf);
657 else
658 ctx->ptr = mdata_buf;
659
660 return ret ? : res.result[0];
661 }
662
663 /**
664 * qcom_scm_pas_init_image() - Initialize peripheral authentication service
665 * state machine for a given peripheral, using the
666 * metadata
667 * @pas_id: peripheral authentication service id
668 * @metadata: pointer to memory containing ELF header, program header table
669 * and optional blob of data used for authenticating the metadata
670 * and the rest of the firmware
671 * @size: size of the metadata
672 * @ctx: optional pas context
673 *
674 * Return: 0 on success.
675 *
676 * Upon successful return, the PAS metadata context (@ctx) will be used to
677 * track the metadata allocation, this needs to be released by invoking
678 * qcom_scm_pas_metadata_release() by the caller.
679 */
680 int qcom_scm_pas_init_image(u32 pas_id, const void *metadata, size_t size,
681 struct qcom_scm_pas_context *ctx)
682 {
683 struct qcom_scm_res res;
684 dma_addr_t mdata_phys;
685 void *mdata_buf;
686 int ret;
687
688 if (ctx && ctx->use_tzmem)
689 return qcom_scm_pas_prep_and_init_image(ctx, metadata, size);
690
691 /*
692 * During the scm call memory protection will be enabled for the meta
693 * data blob, so make sure it's physically contiguous, 4K aligned and
694 * non-cachable to avoid XPU violations.
695 *
696 * For PIL calls the hypervisor creates SHM Bridges for the blob
697 * buffers on behalf of Linux so we must not do it ourselves hence
698 * not using the TZMem allocator here.
699 *
700 * If we pass a buffer that is already part of an SHM Bridge to this
701 * call, it will fail.
702 */
703 mdata_buf = dma_alloc_coherent(__scm->dev, size, &mdata_phys,
704 GFP_KERNEL);
705 if (!mdata_buf)
706 return -ENOMEM;
707
708 memcpy(mdata_buf, metadata, size);
709
710 ret = __qcom_scm_pas_init_image(pas_id, mdata_phys, &res);
711 if (ret < 0 || !ctx) {
712 dma_free_coherent(__scm->dev, size, mdata_buf, mdata_phys);
713 } else if (ctx) {
714 ctx->ptr = mdata_buf;
715 ctx->phys = mdata_phys;
716 ctx->size = size;
717 }
718
719 return ret ? : res.result[0];
720 }
721 EXPORT_SYMBOL_GPL(qcom_scm_pas_init_image);
722
723 /**
724 * qcom_scm_pas_metadata_release() - release metadata context
725 * @ctx: pas context
726 */
727 void qcom_scm_pas_metadata_release(struct qcom_scm_pas_context *ctx)
728 {
729 if (!ctx->ptr)
730 return;
731
732 if (ctx->use_tzmem)
733 qcom_tzmem_free(ctx->ptr);
734 else
735 dma_free_coherent(__scm->dev, ctx->size, ctx->ptr, ctx->phys);
736
737 ctx->ptr = NULL;
738 }
739 EXPORT_SYMBOL_GPL(qcom_scm_pas_metadata_release);
740
741 /**
742 * qcom_scm_pas_mem_setup() - Prepare the memory related to a given peripheral
743 * for firmware loading
744 * @pas_id: peripheral authentication service id
745 * @addr: start address of memory area to prepare
746 * @size: size of the memory area to prepare
747 *
748 * Returns 0 on success.
749 */
750 int qcom_scm_pas_mem_setup(u32 pas_id, phys_addr_t addr, phys_addr_t size)
751 {
752 int ret;
753 struct qcom_scm_desc desc = {
754 .svc = QCOM_SCM_SVC_PIL,
755 .cmd = QCOM_SCM_PIL_PAS_MEM_SETUP,
756 .arginfo = QCOM_SCM_ARGS(3),
757 .args[0] = pas_id,
758 .args[1] = addr,
759 .args[2] = size,
760 .owner = ARM_SMCCC_OWNER_SIP,
761 };
762 struct qcom_scm_res res;
763
764 ret = qcom_scm_clk_enable();
765 if (ret)
766 return ret;
767
768 ret = qcom_scm_bw_enable();
769 if (ret)
770 goto disable_clk;
771
772 ret = qcom_scm_call(__scm->dev, &desc, &res);
773 qcom_scm_bw_disable();
774
775 disable_clk:
776 qcom_scm_clk_disable();
777
778 return ret ? : res.result[0];
779 }
780 EXPORT_SYMBOL_GPL(qcom_scm_pas_mem_setup);
781
782 static void *__qcom_scm_pas_get_rsc_table(u32 pas_id, void *input_rt_tzm,
783 size_t input_rt_size,
784 size_t *output_rt_size)
785 {
786 struct qcom_scm_desc desc = {
787 .svc = QCOM_SCM_SVC_PIL,
788 .cmd = QCOM_SCM_PIL_PAS_GET_RSCTABLE,
789 .arginfo = QCOM_SCM_ARGS(5, QCOM_SCM_VAL, QCOM_SCM_RO, QCOM_SCM_VAL,
790 QCOM_SCM_RW, QCOM_SCM_VAL),
791 .args[0] = pas_id,
792 .owner = ARM_SMCCC_OWNER_SIP,
793 };
794 struct qcom_scm_res res;
795 void *output_rt_tzm;
796 int ret;
797
798 output_rt_tzm = qcom_tzmem_alloc(__scm->mempool, *output_rt_size, GFP_KERNEL);
799 if (!output_rt_tzm)
800 return ERR_PTR(-ENOMEM);
801
802 desc.args[1] = qcom_tzmem_to_phys(input_rt_tzm);
803 desc.args[2] = input_rt_size;
804 desc.args[3] = qcom_tzmem_to_phys(output_rt_tzm);
805 desc.args[4] = *output_rt_size;
806
807 /*
808 * Whether SMC fail or pass, res.result[2] will hold actual resource table
809 * size.
810 *
811 * If passed 'output_rt_size' buffer size is not sufficient to hold the
812 * resource table TrustZone sends, response code in res.result[1] as
813 * RSCTABLE_BUFFER_NOT_SUFFICIENT so that caller can retry this SMC call
814 * with output_rt_tzm buffer with res.result[2] size however, It should not
815 * be of unresonable size.
816 */
817 ret = qcom_scm_call(__scm->dev, &desc, &res);
818 if (!ret && res.result[2] > SZ_1G) {
819 ret = -E2BIG;
820 goto free_output_rt;
821 }
822
823 *output_rt_size = res.result[2];
824 if (ret && res.result[1] == RSCTABLE_BUFFER_NOT_SUFFICIENT)
825 ret = -EOVERFLOW;
826
827 free_output_rt:
828 if (ret)
829 qcom_tzmem_free(output_rt_tzm);
830
831 return ret ? ERR_PTR(ret) : output_rt_tzm;
832 }
833
834 /**
835 * qcom_scm_pas_get_rsc_table() - Retrieve the resource table in passed output buffer
836 * for a given peripheral.
837 *
838 * Qualcomm remote processor may rely on both static and dynamic resources for
839 * its functionality. Static resources typically refer to memory-mapped addresses
840 * required by the subsystem and are often embedded within the firmware binary
841 * and dynamic resources, such as shared memory in DDR etc., are determined at
842 * runtime during the boot process.
843 *
844 * On Qualcomm Technologies devices, it's possible that static resources are not
845 * embedded in the firmware binary and instead are provided by TrustZone However,
846 * dynamic resources are always expected to come from TrustZone. This indicates
847 * that for Qualcomm devices, all resources (static and dynamic) will be provided
848 * by TrustZone via the SMC call.
849 *
850 * If the remote processor firmware binary does contain static resources, they
851 * should be passed in input_rt. These will be forwarded to TrustZone for
852 * authentication. TrustZone will then append the dynamic resources and return
853 * the complete resource table in output_rt_tzm.
854 *
855 * If the remote processor firmware binary does not include a resource table,
856 * the caller of this function should set input_rt as NULL and input_rt_size
857 * as zero respectively.
858 *
859 * More about documentation on resource table data structures can be found in
860 * include/linux/remoteproc.h
861 *
862 * @ctx: PAS context
863 * @pas_id: peripheral authentication service id
864 * @input_rt: resource table buffer which is present in firmware binary
865 * @input_rt_size: size of the resource table present in firmware binary
866 * @output_rt_size: TrustZone expects caller should pass worst case size for
867 * the output_rt_tzm.
868 *
869 * Return:
870 * On success, returns a pointer to the allocated buffer containing the final
871 * resource table and output_rt_size will have actual resource table size from
872 * TrustZone. The caller is responsible for freeing the buffer. On failure,
873 * returns ERR_PTR(-errno).
874 */
875 struct resource_table *qcom_scm_pas_get_rsc_table(struct qcom_scm_pas_context *ctx,
876 void *input_rt,
877 size_t input_rt_size,
878 size_t *output_rt_size)
879 {
880 struct resource_table empty_rsc = {};
881 size_t size = SZ_16K;
882 void *output_rt_tzm;
883 void *input_rt_tzm;
884 void *tbl_ptr;
885 int ret;
886
887 ret = qcom_scm_clk_enable();
888 if (ret)
889 return ERR_PTR(ret);
890
891 ret = qcom_scm_bw_enable();
892 if (ret)
893 goto disable_clk;
894
895 /*
896 * TrustZone can not accept buffer as NULL value as argument hence,
897 * we need to pass a input buffer indicating that subsystem firmware
898 * does not have resource table by filling resource table structure.
899 */
900 if (!input_rt) {
901 input_rt = &empty_rsc;
902 input_rt_size = sizeof(empty_rsc);
903 }
904
905 input_rt_tzm = qcom_tzmem_alloc(__scm->mempool, input_rt_size, GFP_KERNEL);
906 if (!input_rt_tzm) {
907 ret = -ENOMEM;
908 goto disable_scm_bw;
909 }
910
911 memcpy(input_rt_tzm, input_rt, input_rt_size);
912
913 output_rt_tzm = __qcom_scm_pas_get_rsc_table(ctx->pas_id, input_rt_tzm,
914 input_rt_size, &size);
915 if (PTR_ERR(output_rt_tzm) == -EOVERFLOW)
916 /* Try again with the size requested by the TZ */
917 output_rt_tzm = __qcom_scm_pas_get_rsc_table(ctx->pas_id,
918 input_rt_tzm,
919 input_rt_size,
920 &size);
921 if (IS_ERR(output_rt_tzm)) {
922 ret = PTR_ERR(output_rt_tzm);
923 goto free_input_rt;
924 }
925
926 tbl_ptr = kmemdup(output_rt_tzm, size, GFP_KERNEL);
927 if (!tbl_ptr) {
928 qcom_tzmem_free(output_rt_tzm);
929 ret = -ENOMEM;
930 goto free_input_rt;
931 }
932
933 *output_rt_size = size;
934 qcom_tzmem_free(output_rt_tzm);
935
936 free_input_rt:
937 qcom_tzmem_free(input_rt_tzm);
938
939 disable_scm_bw:
940 qcom_scm_bw_disable();
941
942 disable_clk:
943 qcom_scm_clk_disable();
944
945 return ret ? ERR_PTR(ret) : tbl_ptr;
946 }
947 EXPORT_SYMBOL_GPL(qcom_scm_pas_get_rsc_table);
948
949 /**
950 * qcom_scm_pas_auth_and_reset() - Authenticate the given peripheral firmware
951 * and reset the remote processor
952 * @pas_id: peripheral authentication service id
953 *
954 * Return 0 on success.
955 */
956 int qcom_scm_pas_auth_and_reset(u32 pas_id)
957 {
958 int ret;
959 struct qcom_scm_desc desc = {
960 .svc = QCOM_SCM_SVC_PIL,
961 .cmd = QCOM_SCM_PIL_PAS_AUTH_AND_RESET,
962 .arginfo = QCOM_SCM_ARGS(1),
963 .args[0] = pas_id,
964 .owner = ARM_SMCCC_OWNER_SIP,
965 };
966 struct qcom_scm_res res;
967
968 ret = qcom_scm_clk_enable();
969 if (ret)
970 return ret;
971
972 ret = qcom_scm_bw_enable();
973 if (ret)
974 goto disable_clk;
975
976 ret = qcom_scm_call(__scm->dev, &desc, &res);
977 qcom_scm_bw_disable();
978
979 disable_clk:
980 qcom_scm_clk_disable();
981
982 return ret ? : res.result[0];
983 }
984 EXPORT_SYMBOL_GPL(qcom_scm_pas_auth_and_reset);
985
986 /**
987 * qcom_scm_pas_prepare_and_auth_reset() - Prepare, authenticate, and reset the
988 * remote processor
989 *
990 * @ctx: Context saved during call to qcom_scm_pas_context_init()
991 *
992 * This function performs the necessary steps to prepare a PAS subsystem,
993 * authenticate it using the provided metadata, and initiate a reset sequence.
994 *
995 * It should be used when Linux is in control setting up the IOMMU hardware
996 * for remote subsystem during secure firmware loading processes. The preparation
997 * step sets up a shmbridge over the firmware memory before TrustZone accesses the
998 * firmware memory region for authentication. The authentication step verifies
999 * the integrity and authenticity of the firmware or configuration using secure
1000 * metadata. Finally, the reset step ensures the subsystem starts in a clean and
1001 * sane state.
1002 *
1003 * Return: 0 on success, negative errno on failure.
1004 */
1005 int qcom_scm_pas_prepare_and_auth_reset(struct qcom_scm_pas_context *ctx)
1006 {
1007 u64 handle;
1008 int ret;
1009
1010 /*
1011 * When Linux running @ EL1, Gunyah hypervisor running @ EL2 traps the
1012 * auth_and_reset call and create an shmbridge on the remote subsystem
1013 * memory region and then invokes a call to TrustZone to authenticate.
1014 */
1015 if (!ctx->use_tzmem)
1016 return qcom_scm_pas_auth_and_reset(ctx->pas_id);
1017
1018 /*
1019 * When Linux runs @ EL2 Linux must create the shmbridge itself and then
1020 * subsequently call TrustZone for authenticate and reset.
1021 */
1022 ret = qcom_tzmem_shm_bridge_create(ctx->mem_phys, ctx->mem_size, &handle);
1023 if (ret)
1024 return ret;
1025
1026 ret = qcom_scm_pas_auth_and_reset(ctx->pas_id);
1027 qcom_tzmem_shm_bridge_delete(handle);
1028
1029 return ret;
1030 }
1031 EXPORT_SYMBOL_GPL(qcom_scm_pas_prepare_and_auth_reset);
1032
1033 /**
1034 * qcom_scm_pas_shutdown() - Shut down the remote processor
1035 * @pas_id: peripheral authentication service id
1036 *
1037 * Returns 0 on success.
1038 */
1039 int qcom_scm_pas_shutdown(u32 pas_id)
1040 {
1041 int ret;
1042 struct qcom_scm_desc desc = {
1043 .svc = QCOM_SCM_SVC_PIL,
1044 .cmd = QCOM_SCM_PIL_PAS_SHUTDOWN,
1045 .arginfo = QCOM_SCM_ARGS(1),
1046 .args[0] = pas_id,
1047 .owner = ARM_SMCCC_OWNER_SIP,
1048 };
1049 struct qcom_scm_res res;
1050
1051 ret = qcom_scm_clk_enable();
1052 if (ret)
1053 return ret;
1054
1055 ret = qcom_scm_bw_enable();
1056 if (ret)
1057 goto disable_clk;
1058
1059 ret = qcom_scm_call(__scm->dev, &desc, &res);
1060 qcom_scm_bw_disable();
1061
1062 disable_clk:
1063 qcom_scm_clk_disable();
1064
1065 return ret ? : res.result[0];
1066 }
1067 EXPORT_SYMBOL_GPL(qcom_scm_pas_shutdown);
1068
1069 /**
1070 * qcom_scm_pas_supported() - Check if the peripheral authentication service is
1071 * available for the given peripherial
1072 * @pas_id: peripheral authentication service id
1073 *
1074 * Returns true if PAS is supported for this peripheral, otherwise false.
1075 */
1076 bool qcom_scm_pas_supported(u32 pas_id)
1077 {
1078 int ret;
1079 struct qcom_scm_desc desc = {
1080 .svc = QCOM_SCM_SVC_PIL,
1081 .cmd = QCOM_SCM_PIL_PAS_IS_SUPPORTED,
1082 .arginfo = QCOM_SCM_ARGS(1),
1083 .args[0] = pas_id,
1084 .owner = ARM_SMCCC_OWNER_SIP,
1085 };
1086 struct qcom_scm_res res;
1087
1088 if (!__qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_PIL,
1089 QCOM_SCM_PIL_PAS_IS_SUPPORTED))
1090 return false;
1091
1092 ret = qcom_scm_call(__scm->dev, &desc, &res);
1093
1094 return ret ? false : !!res.result[0];
1095 }
1096 EXPORT_SYMBOL_GPL(qcom_scm_pas_supported);
1097
1098 static int __qcom_scm_pas_mss_reset(struct device *dev, bool reset)
1099 {
1100 struct qcom_scm_desc desc = {
1101 .svc = QCOM_SCM_SVC_PIL,
1102 .cmd = QCOM_SCM_PIL_PAS_MSS_RESET,
1103 .arginfo = QCOM_SCM_ARGS(2),
1104 .args[0] = reset,
1105 .args[1] = 0,
1106 .owner = ARM_SMCCC_OWNER_SIP,
1107 };
1108 struct qcom_scm_res res;
1109 int ret;
1110
1111 ret = qcom_scm_call(__scm->dev, &desc, &res);
1112
1113 return ret ? : res.result[0];
1114 }
1115
1116 static int qcom_scm_pas_reset_assert(struct reset_controller_dev *rcdev,
1117 unsigned long idx)
1118 {
1119 if (idx != 0)
1120 return -EINVAL;
1121
1122 return __qcom_scm_pas_mss_reset(__scm->dev, 1);
1123 }
1124
1125 static int qcom_scm_pas_reset_deassert(struct reset_controller_dev *rcdev,
1126 unsigned long idx)
1127 {
1128 if (idx != 0)
1129 return -EINVAL;
1130
1131 return __qcom_scm_pas_mss_reset(__scm->dev, 0);
1132 }
1133
1134 static const struct reset_control_ops qcom_scm_pas_reset_ops = {
1135 .assert = qcom_scm_pas_reset_assert,
1136 .deassert = qcom_scm_pas_reset_deassert,
1137 };
1138
1139 int qcom_scm_io_readl(phys_addr_t addr, unsigned int *val)
1140 {
1141 struct qcom_scm_desc desc = {
1142 .svc = QCOM_SCM_SVC_IO,
1143 .cmd = QCOM_SCM_IO_READ,
1144 .arginfo = QCOM_SCM_ARGS(1),
1145 .args[0] = addr,
1146 .owner = ARM_SMCCC_OWNER_SIP,
1147 };
1148 struct qcom_scm_res res;
1149 int ret;
1150
1151
1152 ret = qcom_scm_call_atomic(__scm->dev, &desc, &res);
1153 if (ret >= 0)
1154 *val = res.result[0];
1155
1156 return ret < 0 ? ret : 0;
1157 }
1158 EXPORT_SYMBOL_GPL(qcom_scm_io_readl);
1159
1160 int qcom_scm_io_writel(phys_addr_t addr, unsigned int val)
1161 {
1162 struct qcom_scm_desc desc = {
1163 .svc = QCOM_SCM_SVC_IO,
1164 .cmd = QCOM_SCM_IO_WRITE,
1165 .arginfo = QCOM_SCM_ARGS(2),
1166 .args[0] = addr,
1167 .args[1] = val,
1168 .owner = ARM_SMCCC_OWNER_SIP,
1169 };
1170
1171 return qcom_scm_call_atomic(__scm->dev, &desc, NULL);
1172 }
1173 EXPORT_SYMBOL_GPL(qcom_scm_io_writel);
1174
1175 /**
1176 * qcom_scm_restore_sec_cfg_available() - Check if secure environment
1177 * supports restore security config interface.
1178 *
1179 * Return true if restore-cfg interface is supported, false if not.
1180 */
1181 bool qcom_scm_restore_sec_cfg_available(void)
1182 {
1183 return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_MP,
1184 QCOM_SCM_MP_RESTORE_SEC_CFG);
1185 }
1186 EXPORT_SYMBOL_GPL(qcom_scm_restore_sec_cfg_available);
1187
1188 int qcom_scm_restore_sec_cfg(u32 device_id, u32 spare)
1189 {
1190 struct qcom_scm_desc desc = {
1191 .svc = QCOM_SCM_SVC_MP,
1192 .cmd = QCOM_SCM_MP_RESTORE_SEC_CFG,
1193 .arginfo = QCOM_SCM_ARGS(2),
1194 .args[0] = device_id,
1195 .args[1] = spare,
1196 .owner = ARM_SMCCC_OWNER_SIP,
1197 };
1198 struct qcom_scm_res res;
1199 int ret;
1200
1201 ret = qcom_scm_call(__scm->dev, &desc, &res);
1202
1203 return ret ? : res.result[0];
1204 }
1205 EXPORT_SYMBOL_GPL(qcom_scm_restore_sec_cfg);
1206
1207 #define QCOM_SCM_CP_APERTURE_CONTEXT_MASK GENMASK(7, 0)
1208
1209 bool qcom_scm_set_gpu_smmu_aperture_is_available(void)
1210 {
1211 return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_MP,
1212 QCOM_SCM_MP_CP_SMMU_APERTURE_ID);
1213 }
1214 EXPORT_SYMBOL_GPL(qcom_scm_set_gpu_smmu_aperture_is_available);
1215
1216 int qcom_scm_set_gpu_smmu_aperture(unsigned int context_bank)
1217 {
1218 struct qcom_scm_desc desc = {
1219 .svc = QCOM_SCM_SVC_MP,
1220 .cmd = QCOM_SCM_MP_CP_SMMU_APERTURE_ID,
1221 .arginfo = QCOM_SCM_ARGS(4),
1222 .args[0] = 0xffff0000 | FIELD_PREP(QCOM_SCM_CP_APERTURE_CONTEXT_MASK, context_bank),
1223 .args[1] = 0xffffffff,
1224 .args[2] = 0xffffffff,
1225 .args[3] = 0xffffffff,
1226 .owner = ARM_SMCCC_OWNER_SIP
1227 };
1228
1229 return qcom_scm_call(__scm->dev, &desc, NULL);
1230 }
1231 EXPORT_SYMBOL_GPL(qcom_scm_set_gpu_smmu_aperture);
1232
1233 int qcom_scm_iommu_secure_ptbl_size(u32 spare, size_t *size)
1234 {
1235 struct qcom_scm_desc desc = {
1236 .svc = QCOM_SCM_SVC_MP,
1237 .cmd = QCOM_SCM_MP_IOMMU_SECURE_PTBL_SIZE,
1238 .arginfo = QCOM_SCM_ARGS(1),
1239 .args[0] = spare,
1240 .owner = ARM_SMCCC_OWNER_SIP,
1241 };
1242 struct qcom_scm_res res;
1243 int ret;
1244
1245 ret = qcom_scm_call(__scm->dev, &desc, &res);
1246
1247 if (size)
1248 *size = res.result[0];
1249
1250 return ret ? : res.result[1];
1251 }
1252 EXPORT_SYMBOL_GPL(qcom_scm_iommu_secure_ptbl_size);
1253
1254 int qcom_scm_iommu_secure_ptbl_init(u64 addr, u32 size, u32 spare)
1255 {
1256 struct qcom_scm_desc desc = {
1257 .svc = QCOM_SCM_SVC_MP,
1258 .cmd = QCOM_SCM_MP_IOMMU_SECURE_PTBL_INIT,
1259 .arginfo = QCOM_SCM_ARGS(3, QCOM_SCM_RW, QCOM_SCM_VAL,
1260 QCOM_SCM_VAL),
1261 .args[0] = addr,
1262 .args[1] = size,
1263 .args[2] = spare,
1264 .owner = ARM_SMCCC_OWNER_SIP,
1265 };
1266 int ret;
1267
1268 ret = qcom_scm_call(__scm->dev, &desc, NULL);
1269
1270 /* the pg table has been initialized already, ignore the error */
1271 if (ret == -EPERM)
1272 ret = 0;
1273
1274 return ret;
1275 }
1276 EXPORT_SYMBOL_GPL(qcom_scm_iommu_secure_ptbl_init);
1277
1278 int qcom_scm_iommu_set_cp_pool_size(u32 spare, u32 size)
1279 {
1280 struct qcom_scm_desc desc = {
1281 .svc = QCOM_SCM_SVC_MP,
1282 .cmd = QCOM_SCM_MP_IOMMU_SET_CP_POOL_SIZE,
1283 .arginfo = QCOM_SCM_ARGS(2),
1284 .args[0] = size,
1285 .args[1] = spare,
1286 .owner = ARM_SMCCC_OWNER_SIP,
1287 };
1288
1289 return qcom_scm_call(__scm->dev, &desc, NULL);
1290 }
1291 EXPORT_SYMBOL_GPL(qcom_scm_iommu_set_cp_pool_size);
1292
1293 int qcom_scm_mem_protect_video_var(u32 cp_start, u32 cp_size,
1294 u32 cp_nonpixel_start,
1295 u32 cp_nonpixel_size)
1296 {
1297 int ret;
1298 struct qcom_scm_desc desc = {
1299 .svc = QCOM_SCM_SVC_MP,
1300 .cmd = QCOM_SCM_MP_VIDEO_VAR,
1301 .arginfo = QCOM_SCM_ARGS(4, QCOM_SCM_VAL, QCOM_SCM_VAL,
1302 QCOM_SCM_VAL, QCOM_SCM_VAL),
1303 .args[0] = cp_start,
1304 .args[1] = cp_size,
1305 .args[2] = cp_nonpixel_start,
1306 .args[3] = cp_nonpixel_size,
1307 .owner = ARM_SMCCC_OWNER_SIP,
1308 };
1309 struct qcom_scm_res res;
1310
1311 ret = qcom_scm_call(__scm->dev, &desc, &res);
1312
1313 return ret ? : res.result[0];
1314 }
1315 EXPORT_SYMBOL_GPL(qcom_scm_mem_protect_video_var);
1316
1317 static int __qcom_scm_assign_mem(struct device *dev, phys_addr_t mem_region,
1318 size_t mem_sz, phys_addr_t src, size_t src_sz,
1319 phys_addr_t dest, size_t dest_sz)
1320 {
1321 int ret;
1322 struct qcom_scm_desc desc = {
1323 .svc = QCOM_SCM_SVC_MP,
1324 .cmd = QCOM_SCM_MP_ASSIGN,
1325 .arginfo = QCOM_SCM_ARGS(7, QCOM_SCM_RO, QCOM_SCM_VAL,
1326 QCOM_SCM_RO, QCOM_SCM_VAL, QCOM_SCM_RO,
1327 QCOM_SCM_VAL, QCOM_SCM_VAL),
1328 .args[0] = mem_region,
1329 .args[1] = mem_sz,
1330 .args[2] = src,
1331 .args[3] = src_sz,
1332 .args[4] = dest,
1333 .args[5] = dest_sz,
1334 .args[6] = 0,
1335 .owner = ARM_SMCCC_OWNER_SIP,
1336 };
1337 struct qcom_scm_res res;
1338
1339 ret = qcom_scm_call(dev, &desc, &res);
1340
1341 return ret ? : res.result[0];
1342 }
1343
1344 /**
1345 * qcom_scm_assign_mem() - Make a secure call to reassign memory ownership
1346 * @mem_addr: mem region whose ownership need to be reassigned
1347 * @mem_sz: size of the region.
1348 * @srcvm: vmid for current set of owners, each set bit in
1349 * flag indicate a unique owner
1350 * @newvm: array having new owners and corresponding permission
1351 * flags
1352 * @dest_cnt: number of owners in next set.
1353 *
1354 * Return negative errno on failure or 0 on success with @srcvm updated.
1355 */
1356 int qcom_scm_assign_mem(phys_addr_t mem_addr, size_t mem_sz,
1357 u64 *srcvm,
1358 const struct qcom_scm_vmperm *newvm,
1359 unsigned int dest_cnt)
1360 {
1361 struct qcom_scm_current_perm_info *destvm;
1362 struct qcom_scm_mem_map_info *mem_to_map;
1363 phys_addr_t mem_to_map_phys;
1364 phys_addr_t dest_phys;
1365 phys_addr_t ptr_phys;
1366 size_t mem_to_map_sz;
1367 size_t dest_sz;
1368 size_t src_sz;
1369 size_t ptr_sz;
1370 int next_vm;
1371 __le32 *src;
1372 int ret, i, b;
1373 u64 srcvm_bits = *srcvm;
1374
1375 src_sz = hweight64(srcvm_bits) * sizeof(*src);
1376 mem_to_map_sz = sizeof(*mem_to_map);
1377 dest_sz = dest_cnt * sizeof(*destvm);
1378 ptr_sz = ALIGN(src_sz, SZ_64) + ALIGN(mem_to_map_sz, SZ_64) +
1379 ALIGN(dest_sz, SZ_64);
1380
1381 void *ptr __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool,
1382 ptr_sz, GFP_KERNEL);
1383 if (!ptr)
1384 return -ENOMEM;
1385
1386 ptr_phys = qcom_tzmem_to_phys(ptr);
1387
1388 /* Fill source vmid detail */
1389 src = ptr;
1390 i = 0;
1391 for (b = 0; b < BITS_PER_TYPE(u64); b++) {
1392 if (srcvm_bits & BIT(b))
1393 src[i++] = cpu_to_le32(b);
1394 }
1395
1396 /* Fill details of mem buff to map */
1397 mem_to_map = ptr + ALIGN(src_sz, SZ_64);
1398 mem_to_map_phys = ptr_phys + ALIGN(src_sz, SZ_64);
1399 mem_to_map->mem_addr = cpu_to_le64(mem_addr);
1400 mem_to_map->mem_size = cpu_to_le64(mem_sz);
1401
1402 next_vm = 0;
1403 /* Fill details of next vmid detail */
1404 destvm = ptr + ALIGN(mem_to_map_sz, SZ_64) + ALIGN(src_sz, SZ_64);
1405 dest_phys = ptr_phys + ALIGN(mem_to_map_sz, SZ_64) + ALIGN(src_sz, SZ_64);
1406 for (i = 0; i < dest_cnt; i++, destvm++, newvm++) {
1407 destvm->vmid = cpu_to_le32(newvm->vmid);
1408 destvm->perm = cpu_to_le32(newvm->perm);
1409 destvm->ctx = 0;
1410 destvm->ctx_size = 0;
1411 next_vm |= BIT(newvm->vmid);
1412 }
1413
1414 ret = __qcom_scm_assign_mem(__scm->dev, mem_to_map_phys, mem_to_map_sz,
1415 ptr_phys, src_sz, dest_phys, dest_sz);
1416 if (ret) {
1417 dev_err(__scm->dev,
1418 "Assign memory protection call failed %d\n", ret);
1419 return ret;
1420 }
1421
1422 *srcvm = next_vm;
1423 return 0;
1424 }
1425 EXPORT_SYMBOL_GPL(qcom_scm_assign_mem);
1426
1427 /**
1428 * qcom_scm_ocmem_lock_available() - is OCMEM lock/unlock interface available
1429 */
1430 bool qcom_scm_ocmem_lock_available(void)
1431 {
1432 return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_OCMEM,
1433 QCOM_SCM_OCMEM_LOCK_CMD);
1434 }
1435 EXPORT_SYMBOL_GPL(qcom_scm_ocmem_lock_available);
1436
1437 /**
1438 * qcom_scm_ocmem_lock() - call OCMEM lock interface to assign an OCMEM
1439 * region to the specified initiator
1440 *
1441 * @id: tz initiator id
1442 * @offset: OCMEM offset
1443 * @size: OCMEM size
1444 * @mode: access mode (WIDE/NARROW)
1445 */
1446 int qcom_scm_ocmem_lock(enum qcom_scm_ocmem_client id, u32 offset, u32 size,
1447 u32 mode)
1448 {
1449 struct qcom_scm_desc desc = {
1450 .svc = QCOM_SCM_SVC_OCMEM,
1451 .cmd = QCOM_SCM_OCMEM_LOCK_CMD,
1452 .args[0] = id,
1453 .args[1] = offset,
1454 .args[2] = size,
1455 .args[3] = mode,
1456 .arginfo = QCOM_SCM_ARGS(4),
1457 };
1458
1459 return qcom_scm_call(__scm->dev, &desc, NULL);
1460 }
1461 EXPORT_SYMBOL_GPL(qcom_scm_ocmem_lock);
1462
1463 /**
1464 * qcom_scm_ocmem_unlock() - call OCMEM unlock interface to release an OCMEM
1465 * region from the specified initiator
1466 *
1467 * @id: tz initiator id
1468 * @offset: OCMEM offset
1469 * @size: OCMEM size
1470 */
1471 int qcom_scm_ocmem_unlock(enum qcom_scm_ocmem_client id, u32 offset, u32 size)
1472 {
1473 struct qcom_scm_desc desc = {
1474 .svc = QCOM_SCM_SVC_OCMEM,
1475 .cmd = QCOM_SCM_OCMEM_UNLOCK_CMD,
1476 .args[0] = id,
1477 .args[1] = offset,
1478 .args[2] = size,
1479 .arginfo = QCOM_SCM_ARGS(3),
1480 };
1481
1482 return qcom_scm_call(__scm->dev, &desc, NULL);
1483 }
1484 EXPORT_SYMBOL_GPL(qcom_scm_ocmem_unlock);
1485
1486 /**
1487 * qcom_scm_ice_available() - Is the ICE key programming interface available?
1488 *
1489 * Return: true iff the SCM calls wrapped by qcom_scm_ice_invalidate_key() and
1490 * qcom_scm_ice_set_key() are available.
1491 */
1492 bool qcom_scm_ice_available(void)
1493 {
1494 return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_ES,
1495 QCOM_SCM_ES_INVALIDATE_ICE_KEY) &&
1496 __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_ES,
1497 QCOM_SCM_ES_CONFIG_SET_ICE_KEY);
1498 }
1499 EXPORT_SYMBOL_GPL(qcom_scm_ice_available);
1500
1501 /**
1502 * qcom_scm_ice_invalidate_key() - Invalidate an inline encryption key
1503 * @index: the keyslot to invalidate
1504 *
1505 * The UFSHCI and eMMC standards define a standard way to do this, but it
1506 * doesn't work on these SoCs; only this SCM call does.
1507 *
1508 * It is assumed that the SoC has only one ICE instance being used, as this SCM
1509 * call doesn't specify which ICE instance the keyslot belongs to.
1510 *
1511 * Return: 0 on success; -errno on failure.
1512 */
1513 int qcom_scm_ice_invalidate_key(u32 index)
1514 {
1515 struct qcom_scm_desc desc = {
1516 .svc = QCOM_SCM_SVC_ES,
1517 .cmd = QCOM_SCM_ES_INVALIDATE_ICE_KEY,
1518 .arginfo = QCOM_SCM_ARGS(1),
1519 .args[0] = index,
1520 .owner = ARM_SMCCC_OWNER_SIP,
1521 };
1522
1523 return qcom_scm_call(__scm->dev, &desc, NULL);
1524 }
1525 EXPORT_SYMBOL_GPL(qcom_scm_ice_invalidate_key);
1526
1527 /**
1528 * qcom_scm_ice_set_key() - Set an inline encryption key
1529 * @index: the keyslot into which to set the key
1530 * @key: the key to program
1531 * @key_size: the size of the key in bytes
1532 * @cipher: the encryption algorithm the key is for
1533 * @data_unit_size: the encryption data unit size, i.e. the size of each
1534 * individual plaintext and ciphertext. Given in 512-byte
1535 * units, e.g. 1 = 512 bytes, 8 = 4096 bytes, etc.
1536 *
1537 * Program a key into a keyslot of Qualcomm ICE (Inline Crypto Engine), where it
1538 * can then be used to encrypt/decrypt UFS or eMMC I/O requests inline.
1539 *
1540 * The UFSHCI and eMMC standards define a standard way to do this, but it
1541 * doesn't work on these SoCs; only this SCM call does.
1542 *
1543 * It is assumed that the SoC has only one ICE instance being used, as this SCM
1544 * call doesn't specify which ICE instance the keyslot belongs to.
1545 *
1546 * Return: 0 on success; -errno on failure.
1547 */
1548 int qcom_scm_ice_set_key(u32 index, const u8 *key, u32 key_size,
1549 enum qcom_scm_ice_cipher cipher, u32 data_unit_size)
1550 {
1551 struct qcom_scm_desc desc = {
1552 .svc = QCOM_SCM_SVC_ES,
1553 .cmd = QCOM_SCM_ES_CONFIG_SET_ICE_KEY,
1554 .arginfo = QCOM_SCM_ARGS(5, QCOM_SCM_VAL, QCOM_SCM_RW,
1555 QCOM_SCM_VAL, QCOM_SCM_VAL,
1556 QCOM_SCM_VAL),
1557 .args[0] = index,
1558 .args[2] = key_size,
1559 .args[3] = cipher,
1560 .args[4] = data_unit_size,
1561 .owner = ARM_SMCCC_OWNER_SIP,
1562 };
1563
1564 int ret;
1565
1566 void *keybuf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool,
1567 key_size,
1568 GFP_KERNEL);
1569 if (!keybuf)
1570 return -ENOMEM;
1571 memcpy(keybuf, key, key_size);
1572 desc.args[1] = qcom_tzmem_to_phys(keybuf);
1573
1574 ret = qcom_scm_call(__scm->dev, &desc, NULL);
1575
1576 memzero_explicit(keybuf, key_size);
1577
1578 return ret;
1579 }
1580 EXPORT_SYMBOL_GPL(qcom_scm_ice_set_key);
1581
1582 bool qcom_scm_has_wrapped_key_support(void)
1583 {
1584 return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_ES,
1585 QCOM_SCM_ES_DERIVE_SW_SECRET) &&
1586 __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_ES,
1587 QCOM_SCM_ES_GENERATE_ICE_KEY) &&
1588 __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_ES,
1589 QCOM_SCM_ES_PREPARE_ICE_KEY) &&
1590 __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_ES,
1591 QCOM_SCM_ES_IMPORT_ICE_KEY);
1592 }
1593 EXPORT_SYMBOL_GPL(qcom_scm_has_wrapped_key_support);
1594
1595 /**
1596 * qcom_scm_derive_sw_secret() - Derive software secret from wrapped key
1597 * @eph_key: an ephemerally-wrapped key
1598 * @eph_key_size: size of @eph_key in bytes
1599 * @sw_secret: output buffer for the software secret
1600 * @sw_secret_size: size of the software secret to derive in bytes
1601 *
1602 * Derive a software secret from an ephemerally-wrapped key for software crypto
1603 * operations. This is done by calling into the secure execution environment,
1604 * which then calls into the hardware to unwrap and derive the secret.
1605 *
1606 * For more information on sw_secret, see the "Hardware-wrapped keys" section of
1607 * Documentation/block/inline-encryption.rst.
1608 *
1609 * Return: 0 on success; -errno on failure.
1610 */
1611 int qcom_scm_derive_sw_secret(const u8 *eph_key, size_t eph_key_size,
1612 u8 *sw_secret, size_t sw_secret_size)
1613 {
1614 struct qcom_scm_desc desc = {
1615 .svc = QCOM_SCM_SVC_ES,
1616 .cmd = QCOM_SCM_ES_DERIVE_SW_SECRET,
1617 .arginfo = QCOM_SCM_ARGS(4, QCOM_SCM_RW, QCOM_SCM_VAL,
1618 QCOM_SCM_RW, QCOM_SCM_VAL),
1619 .owner = ARM_SMCCC_OWNER_SIP,
1620 };
1621 int ret;
1622
1623 void *eph_key_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool,
1624 eph_key_size,
1625 GFP_KERNEL);
1626 if (!eph_key_buf)
1627 return -ENOMEM;
1628
1629 void *sw_secret_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool,
1630 sw_secret_size,
1631 GFP_KERNEL);
1632 if (!sw_secret_buf)
1633 return -ENOMEM;
1634
1635 memcpy(eph_key_buf, eph_key, eph_key_size);
1636 desc.args[0] = qcom_tzmem_to_phys(eph_key_buf);
1637 desc.args[1] = eph_key_size;
1638 desc.args[2] = qcom_tzmem_to_phys(sw_secret_buf);
1639 desc.args[3] = sw_secret_size;
1640
1641 ret = qcom_scm_call(__scm->dev, &desc, NULL);
1642 if (!ret)
1643 memcpy(sw_secret, sw_secret_buf, sw_secret_size);
1644
1645 memzero_explicit(eph_key_buf, eph_key_size);
1646 memzero_explicit(sw_secret_buf, sw_secret_size);
1647 return ret;
1648 }
1649 EXPORT_SYMBOL_GPL(qcom_scm_derive_sw_secret);
1650
1651 /**
1652 * qcom_scm_generate_ice_key() - Generate a wrapped key for storage encryption
1653 * @lt_key: output buffer for the long-term wrapped key
1654 * @lt_key_size: size of @lt_key in bytes. Must be the exact wrapped key size
1655 * used by the SoC.
1656 *
1657 * Generate a key using the built-in HW module in the SoC. The resulting key is
1658 * returned wrapped with the platform-specific Key Encryption Key.
1659 *
1660 * Return: 0 on success; -errno on failure.
1661 */
1662 int qcom_scm_generate_ice_key(u8 *lt_key, size_t lt_key_size)
1663 {
1664 struct qcom_scm_desc desc = {
1665 .svc = QCOM_SCM_SVC_ES,
1666 .cmd = QCOM_SCM_ES_GENERATE_ICE_KEY,
1667 .arginfo = QCOM_SCM_ARGS(2, QCOM_SCM_RW, QCOM_SCM_VAL),
1668 .owner = ARM_SMCCC_OWNER_SIP,
1669 };
1670 int ret;
1671
1672 void *lt_key_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool,
1673 lt_key_size,
1674 GFP_KERNEL);
1675 if (!lt_key_buf)
1676 return -ENOMEM;
1677
1678 desc.args[0] = qcom_tzmem_to_phys(lt_key_buf);
1679 desc.args[1] = lt_key_size;
1680
1681 ret = qcom_scm_call(__scm->dev, &desc, NULL);
1682 if (!ret)
1683 memcpy(lt_key, lt_key_buf, lt_key_size);
1684
1685 memzero_explicit(lt_key_buf, lt_key_size);
1686 return ret;
1687 }
1688 EXPORT_SYMBOL_GPL(qcom_scm_generate_ice_key);
1689
1690 /**
1691 * qcom_scm_prepare_ice_key() - Re-wrap a key with the per-boot ephemeral key
1692 * @lt_key: a long-term wrapped key
1693 * @lt_key_size: size of @lt_key in bytes
1694 * @eph_key: output buffer for the ephemerally-wrapped key
1695 * @eph_key_size: size of @eph_key in bytes. Must be the exact wrapped key size
1696 * used by the SoC.
1697 *
1698 * Given a long-term wrapped key, re-wrap it with the per-boot ephemeral key for
1699 * added protection. The resulting key will only be valid for the current boot.
1700 *
1701 * Return: 0 on success; -errno on failure.
1702 */
1703 int qcom_scm_prepare_ice_key(const u8 *lt_key, size_t lt_key_size,
1704 u8 *eph_key, size_t eph_key_size)
1705 {
1706 struct qcom_scm_desc desc = {
1707 .svc = QCOM_SCM_SVC_ES,
1708 .cmd = QCOM_SCM_ES_PREPARE_ICE_KEY,
1709 .arginfo = QCOM_SCM_ARGS(4, QCOM_SCM_RO, QCOM_SCM_VAL,
1710 QCOM_SCM_RW, QCOM_SCM_VAL),
1711 .owner = ARM_SMCCC_OWNER_SIP,
1712 };
1713 int ret;
1714
1715 void *lt_key_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool,
1716 lt_key_size,
1717 GFP_KERNEL);
1718 if (!lt_key_buf)
1719 return -ENOMEM;
1720
1721 void *eph_key_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool,
1722 eph_key_size,
1723 GFP_KERNEL);
1724 if (!eph_key_buf)
1725 return -ENOMEM;
1726
1727 memcpy(lt_key_buf, lt_key, lt_key_size);
1728 desc.args[0] = qcom_tzmem_to_phys(lt_key_buf);
1729 desc.args[1] = lt_key_size;
1730 desc.args[2] = qcom_tzmem_to_phys(eph_key_buf);
1731 desc.args[3] = eph_key_size;
1732
1733 ret = qcom_scm_call(__scm->dev, &desc, NULL);
1734 if (!ret)
1735 memcpy(eph_key, eph_key_buf, eph_key_size);
1736
1737 memzero_explicit(lt_key_buf, lt_key_size);
1738 memzero_explicit(eph_key_buf, eph_key_size);
1739 return ret;
1740 }
1741 EXPORT_SYMBOL_GPL(qcom_scm_prepare_ice_key);
1742
1743 /**
1744 * qcom_scm_import_ice_key() - Import key for storage encryption
1745 * @raw_key: the raw key to import
1746 * @raw_key_size: size of @raw_key in bytes
1747 * @lt_key: output buffer for the long-term wrapped key
1748 * @lt_key_size: size of @lt_key in bytes. Must be the exact wrapped key size
1749 * used by the SoC.
1750 *
1751 * Import a raw key and return a long-term wrapped key. Uses the SoC's HWKM to
1752 * wrap the raw key using the platform-specific Key Encryption Key.
1753 *
1754 * Return: 0 on success; -errno on failure.
1755 */
1756 int qcom_scm_import_ice_key(const u8 *raw_key, size_t raw_key_size,
1757 u8 *lt_key, size_t lt_key_size)
1758 {
1759 struct qcom_scm_desc desc = {
1760 .svc = QCOM_SCM_SVC_ES,
1761 .cmd = QCOM_SCM_ES_IMPORT_ICE_KEY,
1762 .arginfo = QCOM_SCM_ARGS(4, QCOM_SCM_RO, QCOM_SCM_VAL,
1763 QCOM_SCM_RW, QCOM_SCM_VAL),
1764 .owner = ARM_SMCCC_OWNER_SIP,
1765 };
1766 int ret;
1767
1768 void *raw_key_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool,
1769 raw_key_size,
1770 GFP_KERNEL);
1771 if (!raw_key_buf)
1772 return -ENOMEM;
1773
1774 void *lt_key_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool,
1775 lt_key_size,
1776 GFP_KERNEL);
1777 if (!lt_key_buf)
1778 return -ENOMEM;
1779
1780 memcpy(raw_key_buf, raw_key, raw_key_size);
1781 desc.args[0] = qcom_tzmem_to_phys(raw_key_buf);
1782 desc.args[1] = raw_key_size;
1783 desc.args[2] = qcom_tzmem_to_phys(lt_key_buf);
1784 desc.args[3] = lt_key_size;
1785
1786 ret = qcom_scm_call(__scm->dev, &desc, NULL);
1787 if (!ret)
1788 memcpy(lt_key, lt_key_buf, lt_key_size);
1789
1790 memzero_explicit(raw_key_buf, raw_key_size);
1791 memzero_explicit(lt_key_buf, lt_key_size);
1792 return ret;
1793 }
1794 EXPORT_SYMBOL_GPL(qcom_scm_import_ice_key);
1795
1796 /**
1797 * qcom_scm_hdcp_available() - Check if secure environment supports HDCP.
1798 *
1799 * Return true if HDCP is supported, false if not.
1800 */
1801 bool qcom_scm_hdcp_available(void)
1802 {
1803 bool avail;
1804 int ret = qcom_scm_clk_enable();
1805
1806 if (ret)
1807 return ret;
1808
1809 avail = __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_HDCP,
1810 QCOM_SCM_HDCP_INVOKE);
1811
1812 qcom_scm_clk_disable();
1813
1814 return avail;
1815 }
1816 EXPORT_SYMBOL_GPL(qcom_scm_hdcp_available);
1817
1818 /**
1819 * qcom_scm_hdcp_req() - Send HDCP request.
1820 * @req: HDCP request array
1821 * @req_cnt: HDCP request array count
1822 * @resp: response buffer passed to SCM
1823 *
1824 * Write HDCP register(s) through SCM.
1825 */
1826 int qcom_scm_hdcp_req(struct qcom_scm_hdcp_req *req, u32 req_cnt, u32 *resp)
1827 {
1828 int ret;
1829 struct qcom_scm_desc desc = {
1830 .svc = QCOM_SCM_SVC_HDCP,
1831 .cmd = QCOM_SCM_HDCP_INVOKE,
1832 .arginfo = QCOM_SCM_ARGS(10),
1833 .args = {
1834 req[0].addr,
1835 req[0].val,
1836 req[1].addr,
1837 req[1].val,
1838 req[2].addr,
1839 req[2].val,
1840 req[3].addr,
1841 req[3].val,
1842 req[4].addr,
1843 req[4].val
1844 },
1845 .owner = ARM_SMCCC_OWNER_SIP,
1846 };
1847 struct qcom_scm_res res;
1848
1849 if (req_cnt > QCOM_SCM_HDCP_MAX_REQ_CNT)
1850 return -ERANGE;
1851
1852 ret = qcom_scm_clk_enable();
1853 if (ret)
1854 return ret;
1855
1856 ret = qcom_scm_call(__scm->dev, &desc, &res);
1857 *resp = res.result[0];
1858
1859 qcom_scm_clk_disable();
1860
1861 return ret;
1862 }
1863 EXPORT_SYMBOL_GPL(qcom_scm_hdcp_req);
1864
1865 int qcom_scm_iommu_set_pt_format(u32 sec_id, u32 ctx_num, u32 pt_fmt)
1866 {
1867 struct qcom_scm_desc desc = {
1868 .svc = QCOM_SCM_SVC_SMMU_PROGRAM,
1869 .cmd = QCOM_SCM_SMMU_PT_FORMAT,
1870 .arginfo = QCOM_SCM_ARGS(3),
1871 .args[0] = sec_id,
1872 .args[1] = ctx_num,
1873 .args[2] = pt_fmt, /* 0: LPAE AArch32 - 1: AArch64 */
1874 .owner = ARM_SMCCC_OWNER_SIP,
1875 };
1876
1877 return qcom_scm_call(__scm->dev, &desc, NULL);
1878 }
1879 EXPORT_SYMBOL_GPL(qcom_scm_iommu_set_pt_format);
1880
1881 int qcom_scm_qsmmu500_wait_safe_toggle(bool en)
1882 {
1883 struct qcom_scm_desc desc = {
1884 .svc = QCOM_SCM_SVC_SMMU_PROGRAM,
1885 .cmd = QCOM_SCM_SMMU_CONFIG_ERRATA1,
1886 .arginfo = QCOM_SCM_ARGS(2),
1887 .args[0] = QCOM_SCM_SMMU_CONFIG_ERRATA1_CLIENT_ALL,
1888 .args[1] = en,
1889 .owner = ARM_SMCCC_OWNER_SIP,
1890 };
1891
1892
1893 return qcom_scm_call_atomic(__scm->dev, &desc, NULL);
1894 }
1895 EXPORT_SYMBOL_GPL(qcom_scm_qsmmu500_wait_safe_toggle);
1896
1897 bool qcom_scm_lmh_dcvsh_available(void)
1898 {
1899 return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_LMH, QCOM_SCM_LMH_LIMIT_DCVSH);
1900 }
1901 EXPORT_SYMBOL_GPL(qcom_scm_lmh_dcvsh_available);
1902
1903 /*
1904 * This is only supposed to be called once by the TZMem module. It takes the
1905 * SCM struct device as argument and uses it to pass the call as at the time
1906 * the SHM Bridge is enabled, the SCM is not yet fully set up and doesn't
1907 * accept global user calls. Don't try to use the __scm pointer here.
1908 */
1909 int qcom_scm_shm_bridge_enable(struct device *scm_dev)
1910 {
1911 int ret;
1912
1913 struct qcom_scm_desc desc = {
1914 .svc = QCOM_SCM_SVC_MP,
1915 .cmd = QCOM_SCM_MP_SHM_BRIDGE_ENABLE,
1916 .owner = ARM_SMCCC_OWNER_SIP
1917 };
1918
1919 struct qcom_scm_res res;
1920
1921 if (!__qcom_scm_is_call_available(scm_dev, QCOM_SCM_SVC_MP,
1922 QCOM_SCM_MP_SHM_BRIDGE_ENABLE))
1923 return -EOPNOTSUPP;
1924
1925 ret = qcom_scm_call(scm_dev, &desc, &res);
1926
1927 if (ret)
1928 return ret;
1929
1930 if (res.result[0] == SHMBRIDGE_RESULT_NOTSUPP)
1931 return -EOPNOTSUPP;
1932
1933 return res.result[0];
1934 }
1935 EXPORT_SYMBOL_GPL(qcom_scm_shm_bridge_enable);
1936
1937 int qcom_scm_shm_bridge_create(u64 pfn_and_ns_perm_flags,
1938 u64 ipfn_and_s_perm_flags, u64 size_and_flags,
1939 u64 ns_vmids, u64 *handle)
1940 {
1941 struct qcom_scm_desc desc = {
1942 .svc = QCOM_SCM_SVC_MP,
1943 .cmd = QCOM_SCM_MP_SHM_BRIDGE_CREATE,
1944 .owner = ARM_SMCCC_OWNER_SIP,
1945 .args[0] = pfn_and_ns_perm_flags,
1946 .args[1] = ipfn_and_s_perm_flags,
1947 .args[2] = size_and_flags,
1948 .args[3] = ns_vmids,
1949 .arginfo = QCOM_SCM_ARGS(4, QCOM_SCM_VAL, QCOM_SCM_VAL,
1950 QCOM_SCM_VAL, QCOM_SCM_VAL),
1951 };
1952
1953 struct qcom_scm_res res;
1954 int ret;
1955
1956 ret = qcom_scm_call(__scm->dev, &desc, &res);
1957
1958 if (handle && !ret)
1959 *handle = res.result[1];
1960
1961 return ret ?: res.result[0];
1962 }
1963 EXPORT_SYMBOL_GPL(qcom_scm_shm_bridge_create);
1964
1965 int qcom_scm_shm_bridge_delete(u64 handle)
1966 {
1967 struct qcom_scm_desc desc = {
1968 .svc = QCOM_SCM_SVC_MP,
1969 .cmd = QCOM_SCM_MP_SHM_BRIDGE_DELETE,
1970 .owner = ARM_SMCCC_OWNER_SIP,
1971 .args[0] = handle,
1972 .arginfo = QCOM_SCM_ARGS(1, QCOM_SCM_VAL),
1973 };
1974
1975 return qcom_scm_call(__scm->dev, &desc, NULL);
1976 }
1977 EXPORT_SYMBOL_GPL(qcom_scm_shm_bridge_delete);
1978
1979 int qcom_scm_lmh_profile_change(u32 profile_id)
1980 {
1981 struct qcom_scm_desc desc = {
1982 .svc = QCOM_SCM_SVC_LMH,
1983 .cmd = QCOM_SCM_LMH_LIMIT_PROFILE_CHANGE,
1984 .arginfo = QCOM_SCM_ARGS(1, QCOM_SCM_VAL),
1985 .args[0] = profile_id,
1986 .owner = ARM_SMCCC_OWNER_SIP,
1987 };
1988
1989 return qcom_scm_call(__scm->dev, &desc, NULL);
1990 }
1991 EXPORT_SYMBOL_GPL(qcom_scm_lmh_profile_change);
1992
1993 int qcom_scm_lmh_dcvsh(u32 payload_fn, u32 payload_reg, u32 payload_val,
1994 u64 limit_node, u32 node_id, u64 version)
1995 {
1996 int ret, payload_size = 5 * sizeof(u32);
1997
1998 struct qcom_scm_desc desc = {
1999 .svc = QCOM_SCM_SVC_LMH,
2000 .cmd = QCOM_SCM_LMH_LIMIT_DCVSH,
2001 .arginfo = QCOM_SCM_ARGS(5, QCOM_SCM_RO, QCOM_SCM_VAL, QCOM_SCM_VAL,
2002 QCOM_SCM_VAL, QCOM_SCM_VAL),
2003 .args[1] = payload_size,
2004 .args[2] = limit_node,
2005 .args[3] = node_id,
2006 .args[4] = version,
2007 .owner = ARM_SMCCC_OWNER_SIP,
2008 };
2009
2010 u32 *payload_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool,
2011 payload_size,
2012 GFP_KERNEL);
2013 if (!payload_buf)
2014 return -ENOMEM;
2015
2016 payload_buf[0] = payload_fn;
2017 payload_buf[1] = 0;
2018 payload_buf[2] = payload_reg;
2019 payload_buf[3] = 1;
2020 payload_buf[4] = payload_val;
2021
2022 desc.args[0] = qcom_tzmem_to_phys(payload_buf);
2023
2024 ret = qcom_scm_call(__scm->dev, &desc, NULL);
2025
2026 return ret;
2027 }
2028 EXPORT_SYMBOL_GPL(qcom_scm_lmh_dcvsh);
2029
2030 int qcom_scm_gpu_init_regs(u32 gpu_req)
2031 {
2032 struct qcom_scm_desc desc = {
2033 .svc = QCOM_SCM_SVC_GPU,
2034 .cmd = QCOM_SCM_SVC_GPU_INIT_REGS,
2035 .arginfo = QCOM_SCM_ARGS(1),
2036 .args[0] = gpu_req,
2037 .owner = ARM_SMCCC_OWNER_SIP,
2038 };
2039
2040 return qcom_scm_call(__scm->dev, &desc, NULL);
2041 }
2042 EXPORT_SYMBOL_GPL(qcom_scm_gpu_init_regs);
2043
2044 static int qcom_scm_find_dload_address(struct device *dev, u64 *addr)
2045 {
2046 struct device_node *tcsr;
2047 struct device_node *np = dev->of_node;
2048 struct resource res;
2049 u32 offset;
2050 int ret;
2051
2052 tcsr = of_parse_phandle(np, "qcom,dload-mode", 0);
2053 if (!tcsr)
2054 return 0;
2055
2056 ret = of_address_to_resource(tcsr, 0, &res);
2057 of_node_put(tcsr);
2058 if (ret)
2059 return ret;
2060
2061 ret = of_property_read_u32_index(np, "qcom,dload-mode", 1, &offset);
2062 if (ret < 0)
2063 return ret;
2064
2065 *addr = res.start + offset;
2066
2067 return 0;
2068 }
2069
2070 #ifdef CONFIG_QCOM_QSEECOM
2071
2072 /* Lock for QSEECOM SCM call executions */
2073 static DEFINE_MUTEX(qcom_scm_qseecom_call_lock);
2074
2075 static int __qcom_scm_qseecom_call(const struct qcom_scm_desc *desc,
2076 struct qcom_scm_qseecom_resp *res)
2077 {
2078 struct qcom_scm_res scm_res = {};
2079 int status;
2080
2081 /*
2082 * QSEECOM SCM calls should not be executed concurrently. Therefore, we
2083 * require the respective call lock to be held.
2084 */
2085 lockdep_assert_held(&qcom_scm_qseecom_call_lock);
2086
2087 status = qcom_scm_call(__scm->dev, desc, &scm_res);
2088
2089 res->result = scm_res.result[0];
2090 res->resp_type = scm_res.result[1];
2091 res->data = scm_res.result[2];
2092
2093 if (status)
2094 return status;
2095
2096 return 0;
2097 }
2098
2099 /**
2100 * qcom_scm_qseecom_call() - Perform a QSEECOM SCM call.
2101 * @desc: SCM call descriptor.
2102 * @res: SCM call response (output).
2103 *
2104 * Performs the QSEECOM SCM call described by @desc, returning the response in
2105 * @rsp.
2106 *
2107 * Return: Zero on success, nonzero on failure.
2108 */
2109 static int qcom_scm_qseecom_call(const struct qcom_scm_desc *desc,
2110 struct qcom_scm_qseecom_resp *res)
2111 {
2112 int status;
2113
2114 /*
2115 * Note: Multiple QSEECOM SCM calls should not be executed same time,
2116 * so lock things here. This needs to be extended to callback/listener
2117 * handling when support for that is implemented.
2118 */
2119
2120 mutex_lock(&qcom_scm_qseecom_call_lock);
2121 status = __qcom_scm_qseecom_call(desc, res);
2122 mutex_unlock(&qcom_scm_qseecom_call_lock);
2123
2124 dev_dbg(__scm->dev, "%s: owner=%x, svc=%x, cmd=%x, result=%lld, type=%llx, data=%llx\n",
2125 __func__, desc->owner, desc->svc, desc->cmd, res->result,
2126 res->resp_type, res->data);
2127
2128 if (status) {
2129 dev_err(__scm->dev, "qseecom: scm call failed with error %d\n", status);
2130 return status;
2131 }
2132
2133 /*
2134 * TODO: Handle incomplete and blocked calls:
2135 *
2136 * Incomplete and blocked calls are not supported yet. Some devices
2137 * and/or commands require those, some don't. Let's warn about them
2138 * prominently in case someone attempts to try these commands with a
2139 * device/command combination that isn't supported yet.
2140 */
2141 WARN_ON(res->result == QSEECOM_RESULT_INCOMPLETE);
2142 WARN_ON(res->result == QSEECOM_RESULT_BLOCKED_ON_LISTENER);
2143
2144 return 0;
2145 }
2146
2147 /**
2148 * qcom_scm_qseecom_get_version() - Query the QSEECOM version.
2149 * @version: Pointer where the QSEECOM version will be stored.
2150 *
2151 * Performs the QSEECOM SCM querying the QSEECOM version currently running in
2152 * the TrustZone.
2153 *
2154 * Return: Zero on success, nonzero on failure.
2155 */
2156 static int qcom_scm_qseecom_get_version(u32 *version)
2157 {
2158 struct qcom_scm_desc desc = {};
2159 struct qcom_scm_qseecom_resp res = {};
2160 u32 feature = 10;
2161 int ret;
2162
2163 desc.owner = QSEECOM_TZ_OWNER_SIP;
2164 desc.svc = QSEECOM_TZ_SVC_INFO;
2165 desc.cmd = QSEECOM_TZ_CMD_INFO_VERSION;
2166 desc.arginfo = QCOM_SCM_ARGS(1, QCOM_SCM_VAL);
2167 desc.args[0] = feature;
2168
2169 ret = qcom_scm_qseecom_call(&desc, &res);
2170 if (ret)
2171 return ret;
2172
2173 *version = res.result;
2174 return 0;
2175 }
2176
2177 /**
2178 * qcom_scm_qseecom_app_get_id() - Query the app ID for a given QSEE app name.
2179 * @app_name: The name of the app.
2180 * @app_id: The returned app ID.
2181 *
2182 * Query and return the application ID of the SEE app identified by the given
2183 * name. This returned ID is the unique identifier of the app required for
2184 * subsequent communication.
2185 *
2186 * Return: Zero on success, nonzero on failure, -ENOENT if the app has not been
2187 * loaded or could not be found.
2188 */
2189 int qcom_scm_qseecom_app_get_id(const char *app_name, u32 *app_id)
2190 {
2191 unsigned long name_buf_size = QSEECOM_MAX_APP_NAME_SIZE;
2192 unsigned long app_name_len = strlen(app_name);
2193 struct qcom_scm_desc desc = {};
2194 struct qcom_scm_qseecom_resp res = {};
2195 int status;
2196
2197 if (app_name_len >= name_buf_size)
2198 return -EINVAL;
2199
2200 char *name_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool,
2201 name_buf_size,
2202 GFP_KERNEL);
2203 if (!name_buf)
2204 return -ENOMEM;
2205
2206 memcpy(name_buf, app_name, app_name_len);
2207
2208 desc.owner = QSEECOM_TZ_OWNER_QSEE_OS;
2209 desc.svc = QSEECOM_TZ_SVC_APP_MGR;
2210 desc.cmd = QSEECOM_TZ_CMD_APP_LOOKUP;
2211 desc.arginfo = QCOM_SCM_ARGS(2, QCOM_SCM_RW, QCOM_SCM_VAL);
2212 desc.args[0] = qcom_tzmem_to_phys(name_buf);
2213 desc.args[1] = app_name_len;
2214
2215 status = qcom_scm_qseecom_call(&desc, &res);
2216
2217 if (status)
2218 return status;
2219
2220 if (res.result == QSEECOM_RESULT_FAILURE)
2221 return -ENOENT;
2222
2223 if (res.result != QSEECOM_RESULT_SUCCESS)
2224 return -EINVAL;
2225
2226 if (res.resp_type != QSEECOM_SCM_RES_APP_ID)
2227 return -EINVAL;
2228
2229 *app_id = res.data;
2230 return 0;
2231 }
2232 EXPORT_SYMBOL_GPL(qcom_scm_qseecom_app_get_id);
2233
2234 /**
2235 * qcom_scm_qseecom_app_send() - Send to and receive data from a given QSEE app.
2236 * @app_id: The ID of the target app.
2237 * @req: Request buffer sent to the app (must be TZ memory)
2238 * @req_size: Size of the request buffer.
2239 * @rsp: Response buffer, written to by the app (must be TZ memory)
2240 * @rsp_size: Size of the response buffer.
2241 *
2242 * Sends a request to the QSEE app associated with the given ID and read back
2243 * its response. The caller must provide two DMA memory regions, one for the
2244 * request and one for the response, and fill out the @req region with the
2245 * respective (app-specific) request data. The QSEE app reads this and returns
2246 * its response in the @rsp region.
2247 *
2248 * Return: Zero on success, nonzero on failure.
2249 */
2250 int qcom_scm_qseecom_app_send(u32 app_id, void *req, size_t req_size,
2251 void *rsp, size_t rsp_size)
2252 {
2253 struct qcom_scm_qseecom_resp res = {};
2254 struct qcom_scm_desc desc = {};
2255 phys_addr_t req_phys;
2256 phys_addr_t rsp_phys;
2257 int status;
2258
2259 req_phys = qcom_tzmem_to_phys(req);
2260 rsp_phys = qcom_tzmem_to_phys(rsp);
2261
2262 desc.owner = QSEECOM_TZ_OWNER_TZ_APPS;
2263 desc.svc = QSEECOM_TZ_SVC_APP_ID_PLACEHOLDER;
2264 desc.cmd = QSEECOM_TZ_CMD_APP_SEND;
2265 desc.arginfo = QCOM_SCM_ARGS(5, QCOM_SCM_VAL,
2266 QCOM_SCM_RW, QCOM_SCM_VAL,
2267 QCOM_SCM_RW, QCOM_SCM_VAL);
2268 desc.args[0] = app_id;
2269 desc.args[1] = req_phys;
2270 desc.args[2] = req_size;
2271 desc.args[3] = rsp_phys;
2272 desc.args[4] = rsp_size;
2273
2274 status = qcom_scm_qseecom_call(&desc, &res);
2275
2276 if (status)
2277 return status;
2278
2279 if (res.result != QSEECOM_RESULT_SUCCESS)
2280 return -EIO;
2281
2282 return 0;
2283 }
2284 EXPORT_SYMBOL_GPL(qcom_scm_qseecom_app_send);
2285
2286 /*
2287 * We do not yet support re-entrant calls via the qseecom interface. To prevent
2288 + any potential issues with this, only allow validated machines for now.
2289 */
2290 static const struct of_device_id qcom_scm_qseecom_allowlist[] __maybe_unused = {
2291 { .compatible = "asus,vivobook-s15" },
2292 { .compatible = "asus,zenbook-a14-ux3407qa" },
2293 { .compatible = "asus,zenbook-a14-ux3407ra" },
2294 { .compatible = "dell,inspiron-14-plus-7441" },
2295 { .compatible = "dell,latitude-7455" },
2296 { .compatible = "dell,xps13-9345" },
2297 { .compatible = "hp,elitebook-ultra-g1q" },
2298 { .compatible = "hp,omnibook-x14" },
2299 { .compatible = "huawei,gaokun3" },
2300 { .compatible = "lenovo,flex-5g" },
2301 { .compatible = "lenovo,thinkbook-16" },
2302 { .compatible = "lenovo,thinkpad-t14s" },
2303 { .compatible = "lenovo,thinkpad-x13s", },
2304 { .compatible = "lenovo,yoga-slim7x" },
2305 { .compatible = "medion,sprchrgd14s1" },
2306 { .compatible = "microsoft,arcata", },
2307 { .compatible = "microsoft,blackrock" },
2308 { .compatible = "microsoft,denali", },
2309 { .compatible = "microsoft,romulus13", },
2310 { .compatible = "microsoft,romulus15", },
2311 { .compatible = "qcom,hamoa-iot-evk" },
2312 { .compatible = "qcom,sc8180x-primus" },
2313 { .compatible = "qcom,x1e001de-devkit" },
2314 { .compatible = "qcom,x1e80100-crd" },
2315 { .compatible = "qcom,x1e80100-qcp" },
2316 { .compatible = "qcom,x1p42100-crd" },
2317 { }
2318 };
2319
2320 static void qcom_scm_qseecom_free(void *data)
2321 {
2322 struct platform_device *qseecom_dev = data;
2323
2324 platform_device_del(qseecom_dev);
2325 platform_device_put(qseecom_dev);
2326 }
2327
2328 static int qcom_scm_qseecom_init(struct qcom_scm *scm)
2329 {
2330 struct platform_device *qseecom_dev;
2331 u32 version;
2332 int ret;
2333
2334 /*
2335 * Note: We do two steps of validation here: First, we try to query the
2336 * QSEECOM version as a check to see if the interface exists on this
2337 * device. Second, we check against known good devices due to current
2338 * driver limitations (see comment in qcom_scm_qseecom_allowlist).
2339 *
2340 * Note that we deliberately do the machine check after the version
2341 * check so that we can log potentially supported devices. This should
2342 * be safe as downstream sources indicate that the version query is
2343 * neither blocking nor reentrant.
2344 */
2345 ret = qcom_scm_qseecom_get_version(&version);
2346 if (ret)
2347 return 0;
2348
2349 dev_info(scm->dev, "qseecom: found qseecom with version 0x%x\n", version);
2350
2351 if (!of_machine_device_match(qcom_scm_qseecom_allowlist)) {
2352 dev_info(scm->dev, "qseecom: untested machine, skipping\n");
2353 return 0;
2354 }
2355
2356 /*
2357 * Set up QSEECOM interface device. All application clients will be
2358 * set up and managed by the corresponding driver for it.
2359 */
2360 qseecom_dev = platform_device_alloc("qcom_qseecom", -1);
2361 if (!qseecom_dev)
2362 return -ENOMEM;
2363
2364 qseecom_dev->dev.parent = scm->dev;
2365
2366 ret = platform_device_add(qseecom_dev);
2367 if (ret) {
2368 platform_device_put(qseecom_dev);
2369 return ret;
2370 }
2371
2372 return devm_add_action_or_reset(scm->dev, qcom_scm_qseecom_free, qseecom_dev);
2373 }
2374
2375 #else /* CONFIG_QCOM_QSEECOM */
2376
2377 static int qcom_scm_qseecom_init(struct qcom_scm *scm)
2378 {
2379 return 0;
2380 }
2381
2382 #endif /* CONFIG_QCOM_QSEECOM */
2383
2384 /**
2385 * qcom_scm_qtee_invoke_smc() - Invoke a QTEE object.
2386 * @inbuf: start address of memory area used for inbound buffer.
2387 * @inbuf_size: size of the memory area used for inbound buffer.
2388 * @outbuf: start address of memory area used for outbound buffer.
2389 * @outbuf_size: size of the memory area used for outbound buffer.
2390 * @result: result of QTEE object invocation.
2391 * @response_type: response type returned by QTEE.
2392 *
2393 * @response_type determines how the contents of @inbuf and @outbuf
2394 * should be processed.
2395 *
2396 * Return: On success, return 0 or <0 on failure.
2397 */
2398 int qcom_scm_qtee_invoke_smc(phys_addr_t inbuf, size_t inbuf_size,
2399 phys_addr_t outbuf, size_t outbuf_size,
2400 u64 *result, u64 *response_type)
2401 {
2402 struct qcom_scm_desc desc = {
2403 .svc = QCOM_SCM_SVC_SMCINVOKE,
2404 .cmd = QCOM_SCM_SMCINVOKE_INVOKE,
2405 .owner = ARM_SMCCC_OWNER_TRUSTED_OS,
2406 .args[0] = inbuf,
2407 .args[1] = inbuf_size,
2408 .args[2] = outbuf,
2409 .args[3] = outbuf_size,
2410 .arginfo = QCOM_SCM_ARGS(4, QCOM_SCM_RW, QCOM_SCM_VAL,
2411 QCOM_SCM_RW, QCOM_SCM_VAL),
2412 };
2413 struct qcom_scm_res res;
2414 int ret;
2415
2416 ret = qcom_scm_call(__scm->dev, &desc, &res);
2417 if (ret)
2418 return ret;
2419
2420 if (response_type)
2421 *response_type = res.result[0];
2422
2423 if (result)
2424 *result = res.result[1];
2425
2426 return 0;
2427 }
2428 EXPORT_SYMBOL(qcom_scm_qtee_invoke_smc);
2429
2430 /**
2431 * qcom_scm_qtee_callback_response() - Submit response for callback request.
2432 * @buf: start address of memory area used for outbound buffer.
2433 * @buf_size: size of the memory area used for outbound buffer.
2434 * @result: Result of QTEE object invocation.
2435 * @response_type: Response type returned by QTEE.
2436 *
2437 * @response_type determines how the contents of @buf should be processed.
2438 *
2439 * Return: On success, return 0 or <0 on failure.
2440 */
2441 int qcom_scm_qtee_callback_response(phys_addr_t buf, size_t buf_size,
2442 u64 *result, u64 *response_type)
2443 {
2444 struct qcom_scm_desc desc = {
2445 .svc = QCOM_SCM_SVC_SMCINVOKE,
2446 .cmd = QCOM_SCM_SMCINVOKE_CB_RSP,
2447 .owner = ARM_SMCCC_OWNER_TRUSTED_OS,
2448 .args[0] = buf,
2449 .args[1] = buf_size,
2450 .arginfo = QCOM_SCM_ARGS(2, QCOM_SCM_RW, QCOM_SCM_VAL),
2451 };
2452 struct qcom_scm_res res;
2453 int ret;
2454
2455 ret = qcom_scm_call(__scm->dev, &desc, &res);
2456 if (ret)
2457 return ret;
2458
2459 if (response_type)
2460 *response_type = res.result[0];
2461
2462 if (result)
2463 *result = res.result[1];
2464
2465 return 0;
2466 }
2467 EXPORT_SYMBOL(qcom_scm_qtee_callback_response);
2468
2469 static void qcom_scm_qtee_free(void *data)
2470 {
2471 struct platform_device *qtee_dev = data;
2472
2473 platform_device_unregister(qtee_dev);
2474 }
2475
2476 static void qcom_scm_qtee_init(struct qcom_scm *scm)
2477 {
2478 struct platform_device *qtee_dev;
2479 u64 result, response_type;
2480 int ret;
2481
2482 /*
2483 * Probe for smcinvoke support. This will fail due to invalid buffers,
2484 * but first, it checks whether the call is supported in QTEE syscall
2485 * handler. If it is not supported, -EIO is returned.
2486 */
2487 ret = qcom_scm_qtee_invoke_smc(0, 0, 0, 0, &result, &response_type);
2488 if (ret == -EIO)
2489 return;
2490
2491 /* Setup QTEE interface device. */
2492 qtee_dev = platform_device_register_data(scm->dev, "qcomtee",
2493 PLATFORM_DEVID_NONE, NULL, 0);
2494 if (IS_ERR(qtee_dev))
2495 return;
2496
2497 devm_add_action_or_reset(scm->dev, qcom_scm_qtee_free, qtee_dev);
2498 }
2499
2500 /**
2501 * qcom_scm_is_available() - Checks if SCM is available
2502 */
2503 bool qcom_scm_is_available(void)
2504 {
2505 /* Paired with smp_store_release() in qcom_scm_probe */
2506 return !!smp_load_acquire(&__scm);
2507 }
2508 EXPORT_SYMBOL_GPL(qcom_scm_is_available);
2509
2510 static int qcom_scm_fill_irq_fwspec_params(struct irq_fwspec *fwspec, u32 hwirq)
2511 {
2512 if (hwirq >= GIC_SPI_BASE && hwirq <= GIC_MAX_SPI) {
2513 fwspec->param[0] = GIC_SPI;
2514 fwspec->param[1] = hwirq - GIC_SPI_BASE;
2515 } else if (hwirq >= GIC_ESPI_BASE && hwirq <= GIC_MAX_ESPI) {
2516 fwspec->param[0] = GIC_ESPI;
2517 fwspec->param[1] = hwirq - GIC_ESPI_BASE;
2518 } else {
2519 WARN(1, "Unexpected hwirq: %d\n", hwirq);
2520 return -ENXIO;
2521 }
2522
2523 fwspec->param[2] = IRQ_TYPE_EDGE_RISING;
2524 fwspec->param_count = 3;
2525
2526 return 0;
2527 }
2528
2529 static int qcom_scm_query_waitq_count(struct qcom_scm *scm)
2530 {
2531 struct qcom_scm_desc desc = {
2532 .svc = QCOM_SCM_SVC_WAITQ,
2533 .cmd = QCOM_SCM_WAITQ_GET_INFO,
2534 .owner = ARM_SMCCC_OWNER_SIP
2535 };
2536 struct qcom_scm_res res;
2537 int ret;
2538
2539 ret = qcom_scm_call_atomic(scm->dev, &desc, &res);
2540 if (ret)
2541 return ret;
2542
2543 return res.result[0] & GENMASK(7, 0);
2544 }
2545
2546 static int qcom_scm_get_waitq_irq(struct qcom_scm *scm)
2547 {
2548 struct qcom_scm_desc desc = {
2549 .svc = QCOM_SCM_SVC_WAITQ,
2550 .cmd = QCOM_SCM_WAITQ_GET_INFO,
2551 .owner = ARM_SMCCC_OWNER_SIP
2552 };
2553 struct device_node *parent_irq_node;
2554 struct irq_fwspec fwspec;
2555 struct qcom_scm_res res;
2556 u32 hwirq;
2557 int ret;
2558
2559 ret = qcom_scm_call_atomic(scm->dev, &desc, &res);
2560 if (ret)
2561 return ret;
2562
2563 hwirq = res.result[1] & GENMASK(15, 0);
2564 ret = qcom_scm_fill_irq_fwspec_params(&fwspec, hwirq);
2565 if (ret)
2566 return ret;
2567
2568 parent_irq_node = of_irq_find_parent(scm->dev->of_node);
2569 if (!parent_irq_node)
2570 return -ENODEV;
2571
2572 fwspec.fwnode = of_fwnode_handle(parent_irq_node);
2573
2574 return irq_create_fwspec_mapping(&fwspec);
2575 }
2576
2577 static struct completion *qcom_scm_get_completion(u32 wq_ctx)
2578 {
2579 struct completion *wq;
2580
2581 if (WARN_ON_ONCE(wq_ctx >= __scm->wq_cnt))
2582 return ERR_PTR(-EINVAL);
2583
2584 wq = &__scm->waitq_comps[wq_ctx];
2585
2586 return wq;
2587 }
2588
2589 int qcom_scm_wait_for_wq_completion(u32 wq_ctx)
2590 {
2591 struct completion *wq;
2592
2593 wq = qcom_scm_get_completion(wq_ctx);
2594 if (IS_ERR(wq))
2595 return PTR_ERR(wq);
2596
2597 wait_for_completion_state(wq, TASK_IDLE);
2598
2599 return 0;
2600 }
2601
2602 static int qcom_scm_waitq_wakeup(unsigned int wq_ctx)
2603 {
2604 struct completion *wq;
2605
2606 wq = qcom_scm_get_completion(wq_ctx);
2607 if (IS_ERR(wq))
2608 return PTR_ERR(wq);
2609
2610 complete(wq);
2611
2612 return 0;
2613 }
2614
2615 static irqreturn_t qcom_scm_irq_handler(int irq, void *data)
2616 {
2617 int ret;
2618 struct qcom_scm *scm = data;
2619 u32 wq_ctx, flags, more_pending = 0;
2620
2621 do {
2622 ret = scm_get_wq_ctx(&wq_ctx, &flags, &more_pending);
2623 if (ret) {
2624 dev_err(scm->dev, "GET_WQ_CTX SMC call failed: %d\n", ret);
2625 goto out;
2626 }
2627
2628 if (flags != QCOM_SMC_WAITQ_FLAG_WAKE_ONE) {
2629 dev_err(scm->dev, "Invalid flags received for wq_ctx: %u\n", flags);
2630 goto out;
2631 }
2632
2633 ret = qcom_scm_waitq_wakeup(wq_ctx);
2634 if (ret)
2635 goto out;
2636 } while (more_pending);
2637
2638 out:
2639 return IRQ_HANDLED;
2640 }
2641
2642 static int get_download_mode(char *buffer, const struct kernel_param *kp)
2643 {
2644 if (download_mode >= ARRAY_SIZE(download_mode_name))
2645 return sysfs_emit(buffer, "unknown mode\n");
2646
2647 return sysfs_emit(buffer, "%s\n", download_mode_name[download_mode]);
2648 }
2649
2650 static int set_download_mode(const char *val, const struct kernel_param *kp)
2651 {
2652 bool tmp;
2653 int ret;
2654
2655 ret = sysfs_match_string(download_mode_name, val);
2656 if (ret < 0) {
2657 ret = kstrtobool(val, &tmp);
2658 if (ret < 0) {
2659 pr_err("qcom_scm: err: %d\n", ret);
2660 return ret;
2661 }
2662
2663 ret = tmp ? 1 : 0;
2664 }
2665
2666 download_mode = ret;
2667 if (__scm)
2668 qcom_scm_set_download_mode(download_mode);
2669
2670 return 0;
2671 }
2672
2673 static const struct kernel_param_ops download_mode_param_ops = {
2674 .get = get_download_mode,
2675 .set = set_download_mode,
2676 };
2677
2678 module_param_cb(download_mode, &download_mode_param_ops, NULL, 0644);
2679 MODULE_PARM_DESC(download_mode, "download mode: off/0/N for no dump mode, full/on/1/Y for full dump mode, mini for minidump mode and full,mini for both full and minidump mode together are acceptable values");
2680
2681 static int qcom_scm_probe(struct platform_device *pdev)
2682 {
2683 struct qcom_tzmem_pool_config pool_config;
2684 struct qcom_scm *scm;
2685 int irq, ret;
2686 int i;
2687
2688 scm = devm_kzalloc(&pdev->dev, sizeof(*scm), GFP_KERNEL);
2689 if (!scm)
2690 return -ENOMEM;
2691
2692 scm->dev = &pdev->dev;
2693 ret = qcom_scm_find_dload_address(&pdev->dev, &scm->dload_mode_addr);
2694 if (ret < 0)
2695 return ret;
2696
2697 mutex_init(&scm->scm_bw_lock);
2698
2699 scm->path = devm_of_icc_get(&pdev->dev, NULL);
2700 if (IS_ERR(scm->path))
2701 return dev_err_probe(&pdev->dev, PTR_ERR(scm->path),
2702 "failed to acquire interconnect path\n");
2703
2704 scm->core_clk = devm_clk_get_optional(&pdev->dev, "core");
2705 if (IS_ERR(scm->core_clk))
2706 return PTR_ERR(scm->core_clk);
2707
2708 scm->iface_clk = devm_clk_get_optional(&pdev->dev, "iface");
2709 if (IS_ERR(scm->iface_clk))
2710 return PTR_ERR(scm->iface_clk);
2711
2712 scm->bus_clk = devm_clk_get_optional(&pdev->dev, "bus");
2713 if (IS_ERR(scm->bus_clk))
2714 return PTR_ERR(scm->bus_clk);
2715
2716 scm->reset.ops = &qcom_scm_pas_reset_ops;
2717 scm->reset.nr_resets = 1;
2718 scm->reset.of_node = pdev->dev.of_node;
2719 ret = devm_reset_controller_register(&pdev->dev, &scm->reset);
2720 if (ret)
2721 return ret;
2722
2723 /* vote for max clk rate for highest performance */
2724 ret = clk_set_rate(scm->core_clk, INT_MAX);
2725 if (ret)
2726 return ret;
2727
2728 ret = of_reserved_mem_device_init(scm->dev);
2729 if (ret && ret != -ENODEV)
2730 return dev_err_probe(scm->dev, ret,
2731 "Failed to setup the reserved memory region for TZ mem\n");
2732
2733 ret = qcom_tzmem_enable(scm->dev);
2734 if (ret)
2735 return dev_err_probe(scm->dev, ret,
2736 "Failed to enable the TrustZone memory allocator\n");
2737
2738 memset(&pool_config, 0, sizeof(pool_config));
2739 pool_config.initial_size = 0;
2740 pool_config.policy = QCOM_TZMEM_POLICY_ON_DEMAND;
2741 pool_config.max_size = SZ_256K;
2742
2743 scm->mempool = devm_qcom_tzmem_pool_new(scm->dev, &pool_config);
2744 if (IS_ERR(scm->mempool))
2745 return dev_err_probe(scm->dev, PTR_ERR(scm->mempool),
2746 "Failed to create the SCM memory pool\n");
2747
2748 ret = qcom_scm_query_waitq_count(scm);
2749 scm->wq_cnt = ret < 0 ? QCOM_SCM_DEFAULT_WAITQ_COUNT : ret;
2750 scm->waitq_comps = devm_kcalloc(&pdev->dev, scm->wq_cnt, sizeof(*scm->waitq_comps),
2751 GFP_KERNEL);
2752 if (!scm->waitq_comps)
2753 return -ENOMEM;
2754
2755 for (i = 0; i < scm->wq_cnt; i++)
2756 init_completion(&scm->waitq_comps[i]);
2757
2758 irq = qcom_scm_get_waitq_irq(scm);
2759 if (irq < 0)
2760 irq = platform_get_irq_optional(pdev, 0);
2761
2762 if (irq < 0) {
2763 if (irq != -ENXIO)
2764 return irq;
2765 } else {
2766 ret = devm_request_threaded_irq(scm->dev, irq, NULL, qcom_scm_irq_handler,
2767 IRQF_ONESHOT, "qcom-scm", scm);
2768 if (ret < 0)
2769 return dev_err_probe(scm->dev, ret,
2770 "Failed to request qcom-scm irq\n");
2771 }
2772
2773 /*
2774 * Paired with smp_load_acquire() in qcom_scm_is_available().
2775 *
2776 * This marks the SCM API as ready to accept user calls and can only
2777 * be called after the TrustZone memory pool is initialized and the
2778 * waitqueue interrupt requested.
2779 */
2780 smp_store_release(&__scm, scm);
2781
2782 __get_convention();
2783
2784 /*
2785 * If "download mode" is requested, from this point on warmboot
2786 * will cause the boot stages to enter download mode, unless
2787 * disabled below by a clean shutdown/reboot.
2788 */
2789 qcom_scm_set_download_mode(download_mode);
2790
2791 /*
2792 * Disable SDI if indicated by DT that it is enabled by default.
2793 */
2794 if (of_property_read_bool(pdev->dev.of_node, "qcom,sdi-enabled") || !download_mode)
2795 qcom_scm_disable_sdi();
2796
2797 /*
2798 * Initialize the QSEECOM interface.
2799 *
2800 * Note: QSEECOM is fairly self-contained and this only adds the
2801 * interface device (the driver of which does most of the heavy
2802 * lifting). So any errors returned here should be either -ENOMEM or
2803 * -EINVAL (with the latter only in case there's a bug in our code).
2804 * This means that there is no need to bring down the whole SCM driver.
2805 * Just log the error instead and let SCM live.
2806 */
2807 ret = qcom_scm_qseecom_init(scm);
2808 WARN(ret < 0, "failed to initialize qseecom: %d\n", ret);
2809
2810 /* Initialize the QTEE object interface. */
2811 qcom_scm_qtee_init(scm);
2812
2813 return 0;
2814 }
2815
2816 static void qcom_scm_shutdown(struct platform_device *pdev)
2817 {
2818 /* Clean shutdown, disable download mode to allow normal restart */
2819 qcom_scm_set_download_mode(QCOM_DLOAD_NODUMP);
2820 }
2821
2822 static const struct of_device_id qcom_scm_dt_match[] = {
2823 { .compatible = "qcom,scm" },
2824
2825 /* Legacy entries kept for backwards compatibility */
2826 { .compatible = "qcom,scm-apq8064" },
2827 { .compatible = "qcom,scm-apq8084" },
2828 { .compatible = "qcom,scm-ipq4019" },
2829 { .compatible = "qcom,scm-msm8953" },
2830 { .compatible = "qcom,scm-msm8974" },
2831 { .compatible = "qcom,scm-msm8996" },
2832 {}
2833 };
2834 MODULE_DEVICE_TABLE(of, qcom_scm_dt_match);
2835
2836 static struct platform_driver qcom_scm_driver = {
2837 .driver = {
2838 .name = "qcom_scm",
2839 .of_match_table = qcom_scm_dt_match,
2840 .suppress_bind_attrs = true,
2841 },
2842 .probe = qcom_scm_probe,
2843 .shutdown = qcom_scm_shutdown,
2844 };
2845
2846 static int __init qcom_scm_init(void)
2847 {
2848 return platform_driver_register(&qcom_scm_driver);
2849 }
2850 subsys_initcall(qcom_scm_init);
2851
2852 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. SCM driver");
2853 MODULE_LICENSE("GPL v2");