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.
5 #include <linux/platform_device.h>
6 #include <linux/init.h>
7 #include <linux/interrupt.h>
8 #include <linux/completion.h>
9 #include <linux/cpumask.h>
10 #include <linux/export.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/interconnect.h>
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/firmware/qcom/qcom_scm.h>
17 #include <linux/of_address.h>
18 #include <linux/of_irq.h>
19 #include <linux/of_platform.h>
20 #include <linux/clk.h>
21 #include <linux/reset-controller.h>
22 #include <linux/arm-smccc.h>
26 static bool download_mode = IS_ENABLED(CONFIG_QCOM_SCM_DOWNLOAD_MODE_DEFAULT);
27 module_param(download_mode, bool, 0);
32 struct clk *iface_clk;
34 struct icc_path *path;
35 struct completion waitq_comp;
36 struct reset_controller_dev reset;
38 /* control access to the interconnect path */
39 struct mutex scm_bw_lock;
45 struct qcom_scm_current_perm_info {
53 struct qcom_scm_mem_map_info {
58 /* Each bit configures cold/warm boot address for one of the 4 CPUs */
59 static const u8 qcom_scm_cpu_cold_bits[QCOM_SCM_BOOT_MAX_CPUS] = {
60 0, BIT(0), BIT(3), BIT(5)
62 static const u8 qcom_scm_cpu_warm_bits[QCOM_SCM_BOOT_MAX_CPUS] = {
63 BIT(2), BIT(1), BIT(4), BIT(6)
66 #define QCOM_SMC_WAITQ_FLAG_WAKE_ONE BIT(0)
67 #define QCOM_SMC_WAITQ_FLAG_WAKE_ALL BIT(1)
69 static const char * const qcom_scm_convention_names[] = {
70 [SMC_CONVENTION_UNKNOWN] = "unknown",
71 [SMC_CONVENTION_ARM_32] = "smc arm 32",
72 [SMC_CONVENTION_ARM_64] = "smc arm 64",
73 [SMC_CONVENTION_LEGACY] = "smc legacy",
76 static struct qcom_scm *__scm;
78 static int qcom_scm_clk_enable(void)
82 ret = clk_prepare_enable(__scm->core_clk);
86 ret = clk_prepare_enable(__scm->iface_clk);
90 ret = clk_prepare_enable(__scm->bus_clk);
97 clk_disable_unprepare(__scm->iface_clk);
99 clk_disable_unprepare(__scm->core_clk);
104 static void qcom_scm_clk_disable(void)
106 clk_disable_unprepare(__scm->core_clk);
107 clk_disable_unprepare(__scm->iface_clk);
108 clk_disable_unprepare(__scm->bus_clk);
111 static int qcom_scm_bw_enable(void)
118 if (IS_ERR(__scm->path))
121 mutex_lock(&__scm->scm_bw_lock);
122 if (!__scm->scm_vote_count) {
123 ret = icc_set_bw(__scm->path, 0, UINT_MAX);
125 dev_err(__scm->dev, "failed to set bandwidth request\n");
129 __scm->scm_vote_count++;
131 mutex_unlock(&__scm->scm_bw_lock);
136 static void qcom_scm_bw_disable(void)
138 if (IS_ERR_OR_NULL(__scm->path))
141 mutex_lock(&__scm->scm_bw_lock);
142 if (__scm->scm_vote_count-- == 1)
143 icc_set_bw(__scm->path, 0, 0);
144 mutex_unlock(&__scm->scm_bw_lock);
147 enum qcom_scm_convention qcom_scm_convention = SMC_CONVENTION_UNKNOWN;
148 static DEFINE_SPINLOCK(scm_query_lock);
150 static enum qcom_scm_convention __get_convention(void)
153 struct qcom_scm_desc desc = {
154 .svc = QCOM_SCM_SVC_INFO,
155 .cmd = QCOM_SCM_INFO_IS_CALL_AVAIL,
156 .args[0] = SCM_SMC_FNID(QCOM_SCM_SVC_INFO,
157 QCOM_SCM_INFO_IS_CALL_AVAIL) |
158 (ARM_SMCCC_OWNER_SIP << ARM_SMCCC_OWNER_SHIFT),
159 .arginfo = QCOM_SCM_ARGS(1),
160 .owner = ARM_SMCCC_OWNER_SIP,
162 struct qcom_scm_res res;
163 enum qcom_scm_convention probed_convention;
167 if (likely(qcom_scm_convention != SMC_CONVENTION_UNKNOWN))
168 return qcom_scm_convention;
171 * Device isn't required as there is only one argument - no device
172 * needed to dma_map_single to secure world
174 probed_convention = SMC_CONVENTION_ARM_64;
175 ret = __scm_smc_call(NULL, &desc, probed_convention, &res, true);
176 if (!ret && res.result[0] == 1)
180 * Some SC7180 firmwares didn't implement the
181 * QCOM_SCM_INFO_IS_CALL_AVAIL call, so we fallback to forcing ARM_64
182 * calling conventions on these firmwares. Luckily we don't make any
183 * early calls into the firmware on these SoCs so the device pointer
184 * will be valid here to check if the compatible matches.
186 if (of_device_is_compatible(__scm ? __scm->dev->of_node : NULL, "qcom,scm-sc7180")) {
191 probed_convention = SMC_CONVENTION_ARM_32;
192 ret = __scm_smc_call(NULL, &desc, probed_convention, &res, true);
193 if (!ret && res.result[0] == 1)
196 probed_convention = SMC_CONVENTION_LEGACY;
198 spin_lock_irqsave(&scm_query_lock, flags);
199 if (probed_convention != qcom_scm_convention) {
200 qcom_scm_convention = probed_convention;
201 pr_info("qcom_scm: convention: %s%s\n",
202 qcom_scm_convention_names[qcom_scm_convention],
203 forced ? " (forced)" : "");
205 spin_unlock_irqrestore(&scm_query_lock, flags);
207 return qcom_scm_convention;
211 * qcom_scm_call() - Invoke a syscall in the secure world
213 * @desc: Descriptor structure containing arguments and return values
214 * @res: Structure containing results from SMC/HVC call
216 * Sends a command to the SCM and waits for the command to finish processing.
217 * This should *only* be called in pre-emptible context.
219 static int qcom_scm_call(struct device *dev, const struct qcom_scm_desc *desc,
220 struct qcom_scm_res *res)
223 switch (__get_convention()) {
224 case SMC_CONVENTION_ARM_32:
225 case SMC_CONVENTION_ARM_64:
226 return scm_smc_call(dev, desc, res, false);
227 case SMC_CONVENTION_LEGACY:
228 return scm_legacy_call(dev, desc, res);
230 pr_err("Unknown current SCM calling convention.\n");
236 * qcom_scm_call_atomic() - atomic variation of qcom_scm_call()
238 * @desc: Descriptor structure containing arguments and return values
239 * @res: Structure containing results from SMC/HVC call
241 * Sends a command to the SCM and waits for the command to finish processing.
242 * This can be called in atomic context.
244 static int qcom_scm_call_atomic(struct device *dev,
245 const struct qcom_scm_desc *desc,
246 struct qcom_scm_res *res)
248 switch (__get_convention()) {
249 case SMC_CONVENTION_ARM_32:
250 case SMC_CONVENTION_ARM_64:
251 return scm_smc_call(dev, desc, res, true);
252 case SMC_CONVENTION_LEGACY:
253 return scm_legacy_call_atomic(dev, desc, res);
255 pr_err("Unknown current SCM calling convention.\n");
260 static bool __qcom_scm_is_call_available(struct device *dev, u32 svc_id,
264 struct qcom_scm_desc desc = {
265 .svc = QCOM_SCM_SVC_INFO,
266 .cmd = QCOM_SCM_INFO_IS_CALL_AVAIL,
267 .owner = ARM_SMCCC_OWNER_SIP,
269 struct qcom_scm_res res;
271 desc.arginfo = QCOM_SCM_ARGS(1);
272 switch (__get_convention()) {
273 case SMC_CONVENTION_ARM_32:
274 case SMC_CONVENTION_ARM_64:
275 desc.args[0] = SCM_SMC_FNID(svc_id, cmd_id) |
276 (ARM_SMCCC_OWNER_SIP << ARM_SMCCC_OWNER_SHIFT);
278 case SMC_CONVENTION_LEGACY:
279 desc.args[0] = SCM_LEGACY_FNID(svc_id, cmd_id);
282 pr_err("Unknown SMC convention being used\n");
286 ret = qcom_scm_call(dev, &desc, &res);
288 return ret ? false : !!res.result[0];
291 static int qcom_scm_set_boot_addr(void *entry, const u8 *cpu_bits)
294 unsigned int flags = 0;
295 struct qcom_scm_desc desc = {
296 .svc = QCOM_SCM_SVC_BOOT,
297 .cmd = QCOM_SCM_BOOT_SET_ADDR,
298 .arginfo = QCOM_SCM_ARGS(2),
299 .owner = ARM_SMCCC_OWNER_SIP,
302 for_each_present_cpu(cpu) {
303 if (cpu >= QCOM_SCM_BOOT_MAX_CPUS)
305 flags |= cpu_bits[cpu];
308 desc.args[0] = flags;
309 desc.args[1] = virt_to_phys(entry);
311 return qcom_scm_call_atomic(__scm ? __scm->dev : NULL, &desc, NULL);
314 static int qcom_scm_set_boot_addr_mc(void *entry, unsigned int flags)
316 struct qcom_scm_desc desc = {
317 .svc = QCOM_SCM_SVC_BOOT,
318 .cmd = QCOM_SCM_BOOT_SET_ADDR_MC,
319 .owner = ARM_SMCCC_OWNER_SIP,
320 .arginfo = QCOM_SCM_ARGS(6),
323 /* Apply to all CPUs in all affinity levels */
324 ~0ULL, ~0ULL, ~0ULL, ~0ULL,
329 /* Need a device for DMA of the additional arguments */
330 if (!__scm || __get_convention() == SMC_CONVENTION_LEGACY)
333 return qcom_scm_call(__scm->dev, &desc, NULL);
337 * qcom_scm_set_warm_boot_addr() - Set the warm boot address for all cpus
338 * @entry: Entry point function for the cpus
340 * Set the Linux entry point for the SCM to transfer control to when coming
341 * out of a power down. CPU power down may be executed on cpuidle or hotplug.
343 int qcom_scm_set_warm_boot_addr(void *entry)
345 if (qcom_scm_set_boot_addr_mc(entry, QCOM_SCM_BOOT_MC_FLAG_WARMBOOT))
346 /* Fallback to old SCM call */
347 return qcom_scm_set_boot_addr(entry, qcom_scm_cpu_warm_bits);
350 EXPORT_SYMBOL_GPL(qcom_scm_set_warm_boot_addr);
353 * qcom_scm_set_cold_boot_addr() - Set the cold boot address for all cpus
354 * @entry: Entry point function for the cpus
356 int qcom_scm_set_cold_boot_addr(void *entry)
358 if (qcom_scm_set_boot_addr_mc(entry, QCOM_SCM_BOOT_MC_FLAG_COLDBOOT))
359 /* Fallback to old SCM call */
360 return qcom_scm_set_boot_addr(entry, qcom_scm_cpu_cold_bits);
363 EXPORT_SYMBOL_GPL(qcom_scm_set_cold_boot_addr);
366 * qcom_scm_cpu_power_down() - Power down the cpu
367 * @flags: Flags to flush cache
369 * This is an end point to power down cpu. If there was a pending interrupt,
370 * the control would return from this function, otherwise, the cpu jumps to the
371 * warm boot entry point set for this cpu upon reset.
373 void qcom_scm_cpu_power_down(u32 flags)
375 struct qcom_scm_desc desc = {
376 .svc = QCOM_SCM_SVC_BOOT,
377 .cmd = QCOM_SCM_BOOT_TERMINATE_PC,
378 .args[0] = flags & QCOM_SCM_FLUSH_FLAG_MASK,
379 .arginfo = QCOM_SCM_ARGS(1),
380 .owner = ARM_SMCCC_OWNER_SIP,
383 qcom_scm_call_atomic(__scm ? __scm->dev : NULL, &desc, NULL);
385 EXPORT_SYMBOL_GPL(qcom_scm_cpu_power_down);
387 int qcom_scm_set_remote_state(u32 state, u32 id)
389 struct qcom_scm_desc desc = {
390 .svc = QCOM_SCM_SVC_BOOT,
391 .cmd = QCOM_SCM_BOOT_SET_REMOTE_STATE,
392 .arginfo = QCOM_SCM_ARGS(2),
395 .owner = ARM_SMCCC_OWNER_SIP,
397 struct qcom_scm_res res;
400 ret = qcom_scm_call(__scm->dev, &desc, &res);
402 return ret ? : res.result[0];
404 EXPORT_SYMBOL_GPL(qcom_scm_set_remote_state);
406 static int __qcom_scm_set_dload_mode(struct device *dev, bool enable)
408 struct qcom_scm_desc desc = {
409 .svc = QCOM_SCM_SVC_BOOT,
410 .cmd = QCOM_SCM_BOOT_SET_DLOAD_MODE,
411 .arginfo = QCOM_SCM_ARGS(2),
412 .args[0] = QCOM_SCM_BOOT_SET_DLOAD_MODE,
413 .owner = ARM_SMCCC_OWNER_SIP,
416 desc.args[1] = enable ? QCOM_SCM_BOOT_SET_DLOAD_MODE : 0;
418 return qcom_scm_call_atomic(__scm->dev, &desc, NULL);
421 static void qcom_scm_set_download_mode(bool enable)
426 avail = __qcom_scm_is_call_available(__scm->dev,
428 QCOM_SCM_BOOT_SET_DLOAD_MODE);
430 ret = __qcom_scm_set_dload_mode(__scm->dev, enable);
431 } else if (__scm->dload_mode_addr) {
432 ret = qcom_scm_io_writel(__scm->dload_mode_addr,
433 enable ? QCOM_SCM_BOOT_SET_DLOAD_MODE : 0);
436 "No available mechanism for setting download mode\n");
440 dev_err(__scm->dev, "failed to set download mode: %d\n", ret);
444 * qcom_scm_pas_init_image() - Initialize peripheral authentication service
445 * state machine for a given peripheral, using the
447 * @peripheral: peripheral id
448 * @metadata: pointer to memory containing ELF header, program header table
449 * and optional blob of data used for authenticating the metadata
450 * and the rest of the firmware
451 * @size: size of the metadata
452 * @ctx: optional metadata context
454 * Return: 0 on success.
456 * Upon successful return, the PAS metadata context (@ctx) will be used to
457 * track the metadata allocation, this needs to be released by invoking
458 * qcom_scm_pas_metadata_release() by the caller.
460 int qcom_scm_pas_init_image(u32 peripheral, const void *metadata, size_t size,
461 struct qcom_scm_pas_metadata *ctx)
463 dma_addr_t mdata_phys;
466 struct qcom_scm_desc desc = {
467 .svc = QCOM_SCM_SVC_PIL,
468 .cmd = QCOM_SCM_PIL_PAS_INIT_IMAGE,
469 .arginfo = QCOM_SCM_ARGS(2, QCOM_SCM_VAL, QCOM_SCM_RW),
470 .args[0] = peripheral,
471 .owner = ARM_SMCCC_OWNER_SIP,
473 struct qcom_scm_res res;
476 * During the scm call memory protection will be enabled for the meta
477 * data blob, so make sure it's physically contiguous, 4K aligned and
478 * non-cachable to avoid XPU violations.
480 mdata_buf = dma_alloc_coherent(__scm->dev, size, &mdata_phys,
483 dev_err(__scm->dev, "Allocation of metadata buffer failed.\n");
486 memcpy(mdata_buf, metadata, size);
488 ret = qcom_scm_clk_enable();
492 ret = qcom_scm_bw_enable();
496 desc.args[1] = mdata_phys;
498 ret = qcom_scm_call(__scm->dev, &desc, &res);
500 qcom_scm_bw_disable();
501 qcom_scm_clk_disable();
504 if (ret < 0 || !ctx) {
505 dma_free_coherent(__scm->dev, size, mdata_buf, mdata_phys);
507 ctx->ptr = mdata_buf;
508 ctx->phys = mdata_phys;
512 return ret ? : res.result[0];
514 EXPORT_SYMBOL_GPL(qcom_scm_pas_init_image);
517 * qcom_scm_pas_metadata_release() - release metadata context
518 * @ctx: metadata context
520 void qcom_scm_pas_metadata_release(struct qcom_scm_pas_metadata *ctx)
525 dma_free_coherent(__scm->dev, ctx->size, ctx->ptr, ctx->phys);
531 EXPORT_SYMBOL_GPL(qcom_scm_pas_metadata_release);
534 * qcom_scm_pas_mem_setup() - Prepare the memory related to a given peripheral
535 * for firmware loading
536 * @peripheral: peripheral id
537 * @addr: start address of memory area to prepare
538 * @size: size of the memory area to prepare
540 * Returns 0 on success.
542 int qcom_scm_pas_mem_setup(u32 peripheral, phys_addr_t addr, phys_addr_t size)
545 struct qcom_scm_desc desc = {
546 .svc = QCOM_SCM_SVC_PIL,
547 .cmd = QCOM_SCM_PIL_PAS_MEM_SETUP,
548 .arginfo = QCOM_SCM_ARGS(3),
549 .args[0] = peripheral,
552 .owner = ARM_SMCCC_OWNER_SIP,
554 struct qcom_scm_res res;
556 ret = qcom_scm_clk_enable();
560 ret = qcom_scm_bw_enable();
564 ret = qcom_scm_call(__scm->dev, &desc, &res);
565 qcom_scm_bw_disable();
566 qcom_scm_clk_disable();
568 return ret ? : res.result[0];
570 EXPORT_SYMBOL_GPL(qcom_scm_pas_mem_setup);
573 * qcom_scm_pas_auth_and_reset() - Authenticate the given peripheral firmware
574 * and reset the remote processor
575 * @peripheral: peripheral id
577 * Return 0 on success.
579 int qcom_scm_pas_auth_and_reset(u32 peripheral)
582 struct qcom_scm_desc desc = {
583 .svc = QCOM_SCM_SVC_PIL,
584 .cmd = QCOM_SCM_PIL_PAS_AUTH_AND_RESET,
585 .arginfo = QCOM_SCM_ARGS(1),
586 .args[0] = peripheral,
587 .owner = ARM_SMCCC_OWNER_SIP,
589 struct qcom_scm_res res;
591 ret = qcom_scm_clk_enable();
595 ret = qcom_scm_bw_enable();
599 ret = qcom_scm_call(__scm->dev, &desc, &res);
600 qcom_scm_bw_disable();
601 qcom_scm_clk_disable();
603 return ret ? : res.result[0];
605 EXPORT_SYMBOL_GPL(qcom_scm_pas_auth_and_reset);
608 * qcom_scm_pas_shutdown() - Shut down the remote processor
609 * @peripheral: peripheral id
611 * Returns 0 on success.
613 int qcom_scm_pas_shutdown(u32 peripheral)
616 struct qcom_scm_desc desc = {
617 .svc = QCOM_SCM_SVC_PIL,
618 .cmd = QCOM_SCM_PIL_PAS_SHUTDOWN,
619 .arginfo = QCOM_SCM_ARGS(1),
620 .args[0] = peripheral,
621 .owner = ARM_SMCCC_OWNER_SIP,
623 struct qcom_scm_res res;
625 ret = qcom_scm_clk_enable();
629 ret = qcom_scm_bw_enable();
633 ret = qcom_scm_call(__scm->dev, &desc, &res);
635 qcom_scm_bw_disable();
636 qcom_scm_clk_disable();
638 return ret ? : res.result[0];
640 EXPORT_SYMBOL_GPL(qcom_scm_pas_shutdown);
643 * qcom_scm_pas_supported() - Check if the peripheral authentication service is
644 * available for the given peripherial
645 * @peripheral: peripheral id
647 * Returns true if PAS is supported for this peripheral, otherwise false.
649 bool qcom_scm_pas_supported(u32 peripheral)
652 struct qcom_scm_desc desc = {
653 .svc = QCOM_SCM_SVC_PIL,
654 .cmd = QCOM_SCM_PIL_PAS_IS_SUPPORTED,
655 .arginfo = QCOM_SCM_ARGS(1),
656 .args[0] = peripheral,
657 .owner = ARM_SMCCC_OWNER_SIP,
659 struct qcom_scm_res res;
661 if (!__qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_PIL,
662 QCOM_SCM_PIL_PAS_IS_SUPPORTED))
665 ret = qcom_scm_call(__scm->dev, &desc, &res);
667 return ret ? false : !!res.result[0];
669 EXPORT_SYMBOL_GPL(qcom_scm_pas_supported);
671 static int __qcom_scm_pas_mss_reset(struct device *dev, bool reset)
673 struct qcom_scm_desc desc = {
674 .svc = QCOM_SCM_SVC_PIL,
675 .cmd = QCOM_SCM_PIL_PAS_MSS_RESET,
676 .arginfo = QCOM_SCM_ARGS(2),
679 .owner = ARM_SMCCC_OWNER_SIP,
681 struct qcom_scm_res res;
684 ret = qcom_scm_call(__scm->dev, &desc, &res);
686 return ret ? : res.result[0];
689 static int qcom_scm_pas_reset_assert(struct reset_controller_dev *rcdev,
695 return __qcom_scm_pas_mss_reset(__scm->dev, 1);
698 static int qcom_scm_pas_reset_deassert(struct reset_controller_dev *rcdev,
704 return __qcom_scm_pas_mss_reset(__scm->dev, 0);
707 static const struct reset_control_ops qcom_scm_pas_reset_ops = {
708 .assert = qcom_scm_pas_reset_assert,
709 .deassert = qcom_scm_pas_reset_deassert,
712 int qcom_scm_io_readl(phys_addr_t addr, unsigned int *val)
714 struct qcom_scm_desc desc = {
715 .svc = QCOM_SCM_SVC_IO,
716 .cmd = QCOM_SCM_IO_READ,
717 .arginfo = QCOM_SCM_ARGS(1),
719 .owner = ARM_SMCCC_OWNER_SIP,
721 struct qcom_scm_res res;
725 ret = qcom_scm_call_atomic(__scm->dev, &desc, &res);
727 *val = res.result[0];
729 return ret < 0 ? ret : 0;
731 EXPORT_SYMBOL_GPL(qcom_scm_io_readl);
733 int qcom_scm_io_writel(phys_addr_t addr, unsigned int val)
735 struct qcom_scm_desc desc = {
736 .svc = QCOM_SCM_SVC_IO,
737 .cmd = QCOM_SCM_IO_WRITE,
738 .arginfo = QCOM_SCM_ARGS(2),
741 .owner = ARM_SMCCC_OWNER_SIP,
744 return qcom_scm_call_atomic(__scm->dev, &desc, NULL);
746 EXPORT_SYMBOL_GPL(qcom_scm_io_writel);
749 * qcom_scm_restore_sec_cfg_available() - Check if secure environment
750 * supports restore security config interface.
752 * Return true if restore-cfg interface is supported, false if not.
754 bool qcom_scm_restore_sec_cfg_available(void)
756 return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_MP,
757 QCOM_SCM_MP_RESTORE_SEC_CFG);
759 EXPORT_SYMBOL_GPL(qcom_scm_restore_sec_cfg_available);
761 int qcom_scm_restore_sec_cfg(u32 device_id, u32 spare)
763 struct qcom_scm_desc desc = {
764 .svc = QCOM_SCM_SVC_MP,
765 .cmd = QCOM_SCM_MP_RESTORE_SEC_CFG,
766 .arginfo = QCOM_SCM_ARGS(2),
767 .args[0] = device_id,
769 .owner = ARM_SMCCC_OWNER_SIP,
771 struct qcom_scm_res res;
774 ret = qcom_scm_call(__scm->dev, &desc, &res);
776 return ret ? : res.result[0];
778 EXPORT_SYMBOL_GPL(qcom_scm_restore_sec_cfg);
780 int qcom_scm_iommu_secure_ptbl_size(u32 spare, size_t *size)
782 struct qcom_scm_desc desc = {
783 .svc = QCOM_SCM_SVC_MP,
784 .cmd = QCOM_SCM_MP_IOMMU_SECURE_PTBL_SIZE,
785 .arginfo = QCOM_SCM_ARGS(1),
787 .owner = ARM_SMCCC_OWNER_SIP,
789 struct qcom_scm_res res;
792 ret = qcom_scm_call(__scm->dev, &desc, &res);
795 *size = res.result[0];
797 return ret ? : res.result[1];
799 EXPORT_SYMBOL_GPL(qcom_scm_iommu_secure_ptbl_size);
801 int qcom_scm_iommu_secure_ptbl_init(u64 addr, u32 size, u32 spare)
803 struct qcom_scm_desc desc = {
804 .svc = QCOM_SCM_SVC_MP,
805 .cmd = QCOM_SCM_MP_IOMMU_SECURE_PTBL_INIT,
806 .arginfo = QCOM_SCM_ARGS(3, QCOM_SCM_RW, QCOM_SCM_VAL,
811 .owner = ARM_SMCCC_OWNER_SIP,
815 ret = qcom_scm_call(__scm->dev, &desc, NULL);
817 /* the pg table has been initialized already, ignore the error */
823 EXPORT_SYMBOL_GPL(qcom_scm_iommu_secure_ptbl_init);
825 int qcom_scm_iommu_set_cp_pool_size(u32 spare, u32 size)
827 struct qcom_scm_desc desc = {
828 .svc = QCOM_SCM_SVC_MP,
829 .cmd = QCOM_SCM_MP_IOMMU_SET_CP_POOL_SIZE,
830 .arginfo = QCOM_SCM_ARGS(2),
833 .owner = ARM_SMCCC_OWNER_SIP,
836 return qcom_scm_call(__scm->dev, &desc, NULL);
838 EXPORT_SYMBOL_GPL(qcom_scm_iommu_set_cp_pool_size);
840 int qcom_scm_mem_protect_video_var(u32 cp_start, u32 cp_size,
841 u32 cp_nonpixel_start,
842 u32 cp_nonpixel_size)
845 struct qcom_scm_desc desc = {
846 .svc = QCOM_SCM_SVC_MP,
847 .cmd = QCOM_SCM_MP_VIDEO_VAR,
848 .arginfo = QCOM_SCM_ARGS(4, QCOM_SCM_VAL, QCOM_SCM_VAL,
849 QCOM_SCM_VAL, QCOM_SCM_VAL),
852 .args[2] = cp_nonpixel_start,
853 .args[3] = cp_nonpixel_size,
854 .owner = ARM_SMCCC_OWNER_SIP,
856 struct qcom_scm_res res;
858 ret = qcom_scm_call(__scm->dev, &desc, &res);
860 return ret ? : res.result[0];
862 EXPORT_SYMBOL_GPL(qcom_scm_mem_protect_video_var);
864 static int __qcom_scm_assign_mem(struct device *dev, phys_addr_t mem_region,
865 size_t mem_sz, phys_addr_t src, size_t src_sz,
866 phys_addr_t dest, size_t dest_sz)
869 struct qcom_scm_desc desc = {
870 .svc = QCOM_SCM_SVC_MP,
871 .cmd = QCOM_SCM_MP_ASSIGN,
872 .arginfo = QCOM_SCM_ARGS(7, QCOM_SCM_RO, QCOM_SCM_VAL,
873 QCOM_SCM_RO, QCOM_SCM_VAL, QCOM_SCM_RO,
874 QCOM_SCM_VAL, QCOM_SCM_VAL),
875 .args[0] = mem_region,
882 .owner = ARM_SMCCC_OWNER_SIP,
884 struct qcom_scm_res res;
886 ret = qcom_scm_call(dev, &desc, &res);
888 return ret ? : res.result[0];
892 * qcom_scm_assign_mem() - Make a secure call to reassign memory ownership
893 * @mem_addr: mem region whose ownership need to be reassigned
894 * @mem_sz: size of the region.
895 * @srcvm: vmid for current set of owners, each set bit in
896 * flag indicate a unique owner
897 * @newvm: array having new owners and corresponding permission
899 * @dest_cnt: number of owners in next set.
901 * Return negative errno on failure or 0 on success with @srcvm updated.
903 int qcom_scm_assign_mem(phys_addr_t mem_addr, size_t mem_sz,
905 const struct qcom_scm_vmperm *newvm,
906 unsigned int dest_cnt)
908 struct qcom_scm_current_perm_info *destvm;
909 struct qcom_scm_mem_map_info *mem_to_map;
910 phys_addr_t mem_to_map_phys;
911 phys_addr_t dest_phys;
913 size_t mem_to_map_sz;
921 u64 srcvm_bits = *srcvm;
923 src_sz = hweight64(srcvm_bits) * sizeof(*src);
924 mem_to_map_sz = sizeof(*mem_to_map);
925 dest_sz = dest_cnt * sizeof(*destvm);
926 ptr_sz = ALIGN(src_sz, SZ_64) + ALIGN(mem_to_map_sz, SZ_64) +
927 ALIGN(dest_sz, SZ_64);
929 ptr = dma_alloc_coherent(__scm->dev, ptr_sz, &ptr_phys, GFP_KERNEL);
933 /* Fill source vmid detail */
936 for (b = 0; b < BITS_PER_TYPE(u64); b++) {
937 if (srcvm_bits & BIT(b))
938 src[i++] = cpu_to_le32(b);
941 /* Fill details of mem buff to map */
942 mem_to_map = ptr + ALIGN(src_sz, SZ_64);
943 mem_to_map_phys = ptr_phys + ALIGN(src_sz, SZ_64);
944 mem_to_map->mem_addr = cpu_to_le64(mem_addr);
945 mem_to_map->mem_size = cpu_to_le64(mem_sz);
948 /* Fill details of next vmid detail */
949 destvm = ptr + ALIGN(mem_to_map_sz, SZ_64) + ALIGN(src_sz, SZ_64);
950 dest_phys = ptr_phys + ALIGN(mem_to_map_sz, SZ_64) + ALIGN(src_sz, SZ_64);
951 for (i = 0; i < dest_cnt; i++, destvm++, newvm++) {
952 destvm->vmid = cpu_to_le32(newvm->vmid);
953 destvm->perm = cpu_to_le32(newvm->perm);
955 destvm->ctx_size = 0;
956 next_vm |= BIT(newvm->vmid);
959 ret = __qcom_scm_assign_mem(__scm->dev, mem_to_map_phys, mem_to_map_sz,
960 ptr_phys, src_sz, dest_phys, dest_sz);
961 dma_free_coherent(__scm->dev, ptr_sz, ptr, ptr_phys);
964 "Assign memory protection call failed %d\n", ret);
971 EXPORT_SYMBOL_GPL(qcom_scm_assign_mem);
974 * qcom_scm_ocmem_lock_available() - is OCMEM lock/unlock interface available
976 bool qcom_scm_ocmem_lock_available(void)
978 return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_OCMEM,
979 QCOM_SCM_OCMEM_LOCK_CMD);
981 EXPORT_SYMBOL_GPL(qcom_scm_ocmem_lock_available);
984 * qcom_scm_ocmem_lock() - call OCMEM lock interface to assign an OCMEM
985 * region to the specified initiator
987 * @id: tz initiator id
988 * @offset: OCMEM offset
990 * @mode: access mode (WIDE/NARROW)
992 int qcom_scm_ocmem_lock(enum qcom_scm_ocmem_client id, u32 offset, u32 size,
995 struct qcom_scm_desc desc = {
996 .svc = QCOM_SCM_SVC_OCMEM,
997 .cmd = QCOM_SCM_OCMEM_LOCK_CMD,
1002 .arginfo = QCOM_SCM_ARGS(4),
1005 return qcom_scm_call(__scm->dev, &desc, NULL);
1007 EXPORT_SYMBOL_GPL(qcom_scm_ocmem_lock);
1010 * qcom_scm_ocmem_unlock() - call OCMEM unlock interface to release an OCMEM
1011 * region from the specified initiator
1013 * @id: tz initiator id
1014 * @offset: OCMEM offset
1017 int qcom_scm_ocmem_unlock(enum qcom_scm_ocmem_client id, u32 offset, u32 size)
1019 struct qcom_scm_desc desc = {
1020 .svc = QCOM_SCM_SVC_OCMEM,
1021 .cmd = QCOM_SCM_OCMEM_UNLOCK_CMD,
1025 .arginfo = QCOM_SCM_ARGS(3),
1028 return qcom_scm_call(__scm->dev, &desc, NULL);
1030 EXPORT_SYMBOL_GPL(qcom_scm_ocmem_unlock);
1033 * qcom_scm_ice_available() - Is the ICE key programming interface available?
1035 * Return: true iff the SCM calls wrapped by qcom_scm_ice_invalidate_key() and
1036 * qcom_scm_ice_set_key() are available.
1038 bool qcom_scm_ice_available(void)
1040 return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_ES,
1041 QCOM_SCM_ES_INVALIDATE_ICE_KEY) &&
1042 __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_ES,
1043 QCOM_SCM_ES_CONFIG_SET_ICE_KEY);
1045 EXPORT_SYMBOL_GPL(qcom_scm_ice_available);
1048 * qcom_scm_ice_invalidate_key() - Invalidate an inline encryption key
1049 * @index: the keyslot to invalidate
1051 * The UFSHCI and eMMC standards define a standard way to do this, but it
1052 * doesn't work on these SoCs; only this SCM call does.
1054 * It is assumed that the SoC has only one ICE instance being used, as this SCM
1055 * call doesn't specify which ICE instance the keyslot belongs to.
1057 * Return: 0 on success; -errno on failure.
1059 int qcom_scm_ice_invalidate_key(u32 index)
1061 struct qcom_scm_desc desc = {
1062 .svc = QCOM_SCM_SVC_ES,
1063 .cmd = QCOM_SCM_ES_INVALIDATE_ICE_KEY,
1064 .arginfo = QCOM_SCM_ARGS(1),
1066 .owner = ARM_SMCCC_OWNER_SIP,
1069 return qcom_scm_call(__scm->dev, &desc, NULL);
1071 EXPORT_SYMBOL_GPL(qcom_scm_ice_invalidate_key);
1074 * qcom_scm_ice_set_key() - Set an inline encryption key
1075 * @index: the keyslot into which to set the key
1076 * @key: the key to program
1077 * @key_size: the size of the key in bytes
1078 * @cipher: the encryption algorithm the key is for
1079 * @data_unit_size: the encryption data unit size, i.e. the size of each
1080 * individual plaintext and ciphertext. Given in 512-byte
1081 * units, e.g. 1 = 512 bytes, 8 = 4096 bytes, etc.
1083 * Program a key into a keyslot of Qualcomm ICE (Inline Crypto Engine), where it
1084 * can then be used to encrypt/decrypt UFS or eMMC I/O requests inline.
1086 * The UFSHCI and eMMC standards define a standard way to do this, but it
1087 * doesn't work on these SoCs; only this SCM call does.
1089 * It is assumed that the SoC has only one ICE instance being used, as this SCM
1090 * call doesn't specify which ICE instance the keyslot belongs to.
1092 * Return: 0 on success; -errno on failure.
1094 int qcom_scm_ice_set_key(u32 index, const u8 *key, u32 key_size,
1095 enum qcom_scm_ice_cipher cipher, u32 data_unit_size)
1097 struct qcom_scm_desc desc = {
1098 .svc = QCOM_SCM_SVC_ES,
1099 .cmd = QCOM_SCM_ES_CONFIG_SET_ICE_KEY,
1100 .arginfo = QCOM_SCM_ARGS(5, QCOM_SCM_VAL, QCOM_SCM_RW,
1101 QCOM_SCM_VAL, QCOM_SCM_VAL,
1104 .args[2] = key_size,
1106 .args[4] = data_unit_size,
1107 .owner = ARM_SMCCC_OWNER_SIP,
1110 dma_addr_t key_phys;
1114 * 'key' may point to vmalloc()'ed memory, but we need to pass a
1115 * physical address that's been properly flushed. The sanctioned way to
1116 * do this is by using the DMA API. But as is best practice for crypto
1117 * keys, we also must wipe the key after use. This makes kmemdup() +
1118 * dma_map_single() not clearly correct, since the DMA API can use
1119 * bounce buffers. Instead, just use dma_alloc_coherent(). Programming
1120 * keys is normally rare and thus not performance-critical.
1123 keybuf = dma_alloc_coherent(__scm->dev, key_size, &key_phys,
1127 memcpy(keybuf, key, key_size);
1128 desc.args[1] = key_phys;
1130 ret = qcom_scm_call(__scm->dev, &desc, NULL);
1132 memzero_explicit(keybuf, key_size);
1134 dma_free_coherent(__scm->dev, key_size, keybuf, key_phys);
1137 EXPORT_SYMBOL_GPL(qcom_scm_ice_set_key);
1140 * qcom_scm_hdcp_available() - Check if secure environment supports HDCP.
1142 * Return true if HDCP is supported, false if not.
1144 bool qcom_scm_hdcp_available(void)
1147 int ret = qcom_scm_clk_enable();
1152 avail = __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_HDCP,
1153 QCOM_SCM_HDCP_INVOKE);
1155 qcom_scm_clk_disable();
1159 EXPORT_SYMBOL_GPL(qcom_scm_hdcp_available);
1162 * qcom_scm_hdcp_req() - Send HDCP request.
1163 * @req: HDCP request array
1164 * @req_cnt: HDCP request array count
1165 * @resp: response buffer passed to SCM
1167 * Write HDCP register(s) through SCM.
1169 int qcom_scm_hdcp_req(struct qcom_scm_hdcp_req *req, u32 req_cnt, u32 *resp)
1172 struct qcom_scm_desc desc = {
1173 .svc = QCOM_SCM_SVC_HDCP,
1174 .cmd = QCOM_SCM_HDCP_INVOKE,
1175 .arginfo = QCOM_SCM_ARGS(10),
1188 .owner = ARM_SMCCC_OWNER_SIP,
1190 struct qcom_scm_res res;
1192 if (req_cnt > QCOM_SCM_HDCP_MAX_REQ_CNT)
1195 ret = qcom_scm_clk_enable();
1199 ret = qcom_scm_call(__scm->dev, &desc, &res);
1200 *resp = res.result[0];
1202 qcom_scm_clk_disable();
1206 EXPORT_SYMBOL_GPL(qcom_scm_hdcp_req);
1208 int qcom_scm_iommu_set_pt_format(u32 sec_id, u32 ctx_num, u32 pt_fmt)
1210 struct qcom_scm_desc desc = {
1211 .svc = QCOM_SCM_SVC_SMMU_PROGRAM,
1212 .cmd = QCOM_SCM_SMMU_PT_FORMAT,
1213 .arginfo = QCOM_SCM_ARGS(3),
1216 .args[2] = pt_fmt, /* 0: LPAE AArch32 - 1: AArch64 */
1217 .owner = ARM_SMCCC_OWNER_SIP,
1220 return qcom_scm_call(__scm->dev, &desc, NULL);
1222 EXPORT_SYMBOL_GPL(qcom_scm_iommu_set_pt_format);
1224 int qcom_scm_qsmmu500_wait_safe_toggle(bool en)
1226 struct qcom_scm_desc desc = {
1227 .svc = QCOM_SCM_SVC_SMMU_PROGRAM,
1228 .cmd = QCOM_SCM_SMMU_CONFIG_ERRATA1,
1229 .arginfo = QCOM_SCM_ARGS(2),
1230 .args[0] = QCOM_SCM_SMMU_CONFIG_ERRATA1_CLIENT_ALL,
1232 .owner = ARM_SMCCC_OWNER_SIP,
1236 return qcom_scm_call_atomic(__scm->dev, &desc, NULL);
1238 EXPORT_SYMBOL_GPL(qcom_scm_qsmmu500_wait_safe_toggle);
1240 bool qcom_scm_lmh_dcvsh_available(void)
1242 return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_LMH, QCOM_SCM_LMH_LIMIT_DCVSH);
1244 EXPORT_SYMBOL_GPL(qcom_scm_lmh_dcvsh_available);
1246 int qcom_scm_lmh_profile_change(u32 profile_id)
1248 struct qcom_scm_desc desc = {
1249 .svc = QCOM_SCM_SVC_LMH,
1250 .cmd = QCOM_SCM_LMH_LIMIT_PROFILE_CHANGE,
1251 .arginfo = QCOM_SCM_ARGS(1, QCOM_SCM_VAL),
1252 .args[0] = profile_id,
1253 .owner = ARM_SMCCC_OWNER_SIP,
1256 return qcom_scm_call(__scm->dev, &desc, NULL);
1258 EXPORT_SYMBOL_GPL(qcom_scm_lmh_profile_change);
1260 int qcom_scm_lmh_dcvsh(u32 payload_fn, u32 payload_reg, u32 payload_val,
1261 u64 limit_node, u32 node_id, u64 version)
1263 dma_addr_t payload_phys;
1265 int ret, payload_size = 5 * sizeof(u32);
1267 struct qcom_scm_desc desc = {
1268 .svc = QCOM_SCM_SVC_LMH,
1269 .cmd = QCOM_SCM_LMH_LIMIT_DCVSH,
1270 .arginfo = QCOM_SCM_ARGS(5, QCOM_SCM_RO, QCOM_SCM_VAL, QCOM_SCM_VAL,
1271 QCOM_SCM_VAL, QCOM_SCM_VAL),
1272 .args[1] = payload_size,
1273 .args[2] = limit_node,
1276 .owner = ARM_SMCCC_OWNER_SIP,
1279 payload_buf = dma_alloc_coherent(__scm->dev, payload_size, &payload_phys, GFP_KERNEL);
1283 payload_buf[0] = payload_fn;
1285 payload_buf[2] = payload_reg;
1287 payload_buf[4] = payload_val;
1289 desc.args[0] = payload_phys;
1291 ret = qcom_scm_call(__scm->dev, &desc, NULL);
1293 dma_free_coherent(__scm->dev, payload_size, payload_buf, payload_phys);
1296 EXPORT_SYMBOL_GPL(qcom_scm_lmh_dcvsh);
1298 static int qcom_scm_find_dload_address(struct device *dev, u64 *addr)
1300 struct device_node *tcsr;
1301 struct device_node *np = dev->of_node;
1302 struct resource res;
1306 tcsr = of_parse_phandle(np, "qcom,dload-mode", 0);
1310 ret = of_address_to_resource(tcsr, 0, &res);
1315 ret = of_property_read_u32_index(np, "qcom,dload-mode", 1, &offset);
1319 *addr = res.start + offset;
1325 * qcom_scm_is_available() - Checks if SCM is available
1327 bool qcom_scm_is_available(void)
1331 EXPORT_SYMBOL_GPL(qcom_scm_is_available);
1333 static int qcom_scm_assert_valid_wq_ctx(u32 wq_ctx)
1335 /* FW currently only supports a single wq_ctx (zero).
1336 * TODO: Update this logic to include dynamic allocation and lookup of
1337 * completion structs when FW supports more wq_ctx values.
1340 dev_err(__scm->dev, "Firmware unexpectedly passed non-zero wq_ctx\n");
1347 int qcom_scm_wait_for_wq_completion(u32 wq_ctx)
1351 ret = qcom_scm_assert_valid_wq_ctx(wq_ctx);
1355 wait_for_completion(&__scm->waitq_comp);
1360 static int qcom_scm_waitq_wakeup(struct qcom_scm *scm, unsigned int wq_ctx)
1364 ret = qcom_scm_assert_valid_wq_ctx(wq_ctx);
1368 complete(&__scm->waitq_comp);
1373 static irqreturn_t qcom_scm_irq_handler(int irq, void *data)
1376 struct qcom_scm *scm = data;
1377 u32 wq_ctx, flags, more_pending = 0;
1380 ret = scm_get_wq_ctx(&wq_ctx, &flags, &more_pending);
1382 dev_err(scm->dev, "GET_WQ_CTX SMC call failed: %d\n", ret);
1386 if (flags != QCOM_SMC_WAITQ_FLAG_WAKE_ONE &&
1387 flags != QCOM_SMC_WAITQ_FLAG_WAKE_ALL) {
1388 dev_err(scm->dev, "Invalid flags found for wq_ctx: %u\n", flags);
1392 ret = qcom_scm_waitq_wakeup(scm, wq_ctx);
1395 } while (more_pending);
1401 static int qcom_scm_probe(struct platform_device *pdev)
1403 struct qcom_scm *scm;
1406 scm = devm_kzalloc(&pdev->dev, sizeof(*scm), GFP_KERNEL);
1410 ret = qcom_scm_find_dload_address(&pdev->dev, &scm->dload_mode_addr);
1414 mutex_init(&scm->scm_bw_lock);
1416 scm->path = devm_of_icc_get(&pdev->dev, NULL);
1417 if (IS_ERR(scm->path))
1418 return dev_err_probe(&pdev->dev, PTR_ERR(scm->path),
1419 "failed to acquire interconnect path\n");
1421 scm->core_clk = devm_clk_get_optional(&pdev->dev, "core");
1422 if (IS_ERR(scm->core_clk))
1423 return PTR_ERR(scm->core_clk);
1425 scm->iface_clk = devm_clk_get_optional(&pdev->dev, "iface");
1426 if (IS_ERR(scm->iface_clk))
1427 return PTR_ERR(scm->iface_clk);
1429 scm->bus_clk = devm_clk_get_optional(&pdev->dev, "bus");
1430 if (IS_ERR(scm->bus_clk))
1431 return PTR_ERR(scm->bus_clk);
1433 scm->reset.ops = &qcom_scm_pas_reset_ops;
1434 scm->reset.nr_resets = 1;
1435 scm->reset.of_node = pdev->dev.of_node;
1436 ret = devm_reset_controller_register(&pdev->dev, &scm->reset);
1440 /* vote for max clk rate for highest performance */
1441 ret = clk_set_rate(scm->core_clk, INT_MAX);
1446 __scm->dev = &pdev->dev;
1448 init_completion(&__scm->waitq_comp);
1450 irq = platform_get_irq_optional(pdev, 0);
1455 ret = devm_request_threaded_irq(__scm->dev, irq, NULL, qcom_scm_irq_handler,
1456 IRQF_ONESHOT, "qcom-scm", __scm);
1458 return dev_err_probe(scm->dev, ret, "Failed to request qcom-scm irq\n");
1464 * If requested enable "download mode", from this point on warmboot
1465 * will cause the boot stages to enter download mode, unless
1466 * disabled below by a clean shutdown/reboot.
1469 qcom_scm_set_download_mode(true);
1474 static void qcom_scm_shutdown(struct platform_device *pdev)
1476 /* Clean shutdown, disable download mode to allow normal restart */
1477 qcom_scm_set_download_mode(false);
1480 static const struct of_device_id qcom_scm_dt_match[] = {
1481 { .compatible = "qcom,scm" },
1483 /* Legacy entries kept for backwards compatibility */
1484 { .compatible = "qcom,scm-apq8064" },
1485 { .compatible = "qcom,scm-apq8084" },
1486 { .compatible = "qcom,scm-ipq4019" },
1487 { .compatible = "qcom,scm-msm8953" },
1488 { .compatible = "qcom,scm-msm8974" },
1489 { .compatible = "qcom,scm-msm8996" },
1492 MODULE_DEVICE_TABLE(of, qcom_scm_dt_match);
1494 static struct platform_driver qcom_scm_driver = {
1497 .of_match_table = qcom_scm_dt_match,
1498 .suppress_bind_attrs = true,
1500 .probe = qcom_scm_probe,
1501 .shutdown = qcom_scm_shutdown,
1504 static int __init qcom_scm_init(void)
1506 return platform_driver_register(&qcom_scm_driver);
1508 subsys_initcall(qcom_scm_init);
1510 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. SCM driver");
1511 MODULE_LICENSE("GPL v2");