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/cpumask.h>
8 #include <linux/export.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/qcom_scm.h>
14 #include <linux/of_address.h>
15 #include <linux/of_platform.h>
16 #include <linux/clk.h>
17 #include <linux/reset-controller.h>
18 #include <linux/arm-smccc.h>
22 static bool download_mode = IS_ENABLED(CONFIG_QCOM_SCM_DOWNLOAD_MODE_DEFAULT);
23 module_param(download_mode, bool, 0);
25 #define SCM_HAS_CORE_CLK BIT(0)
26 #define SCM_HAS_IFACE_CLK BIT(1)
27 #define SCM_HAS_BUS_CLK BIT(2)
32 struct clk *iface_clk;
34 struct reset_controller_dev reset;
39 struct qcom_scm_current_perm_info {
47 struct qcom_scm_mem_map_info {
52 /* Each bit configures cold/warm boot address for one of the 4 CPUs */
53 static const u8 qcom_scm_cpu_cold_bits[QCOM_SCM_BOOT_MAX_CPUS] = {
54 0, BIT(0), BIT(3), BIT(5)
56 static const u8 qcom_scm_cpu_warm_bits[QCOM_SCM_BOOT_MAX_CPUS] = {
57 BIT(2), BIT(1), BIT(4), BIT(6)
60 static const char * const qcom_scm_convention_names[] = {
61 [SMC_CONVENTION_UNKNOWN] = "unknown",
62 [SMC_CONVENTION_ARM_32] = "smc arm 32",
63 [SMC_CONVENTION_ARM_64] = "smc arm 64",
64 [SMC_CONVENTION_LEGACY] = "smc legacy",
67 static struct qcom_scm *__scm;
69 static int qcom_scm_clk_enable(void)
73 ret = clk_prepare_enable(__scm->core_clk);
77 ret = clk_prepare_enable(__scm->iface_clk);
81 ret = clk_prepare_enable(__scm->bus_clk);
88 clk_disable_unprepare(__scm->iface_clk);
90 clk_disable_unprepare(__scm->core_clk);
95 static void qcom_scm_clk_disable(void)
97 clk_disable_unprepare(__scm->core_clk);
98 clk_disable_unprepare(__scm->iface_clk);
99 clk_disable_unprepare(__scm->bus_clk);
102 enum qcom_scm_convention qcom_scm_convention = SMC_CONVENTION_UNKNOWN;
103 static DEFINE_SPINLOCK(scm_query_lock);
105 static enum qcom_scm_convention __get_convention(void)
108 struct qcom_scm_desc desc = {
109 .svc = QCOM_SCM_SVC_INFO,
110 .cmd = QCOM_SCM_INFO_IS_CALL_AVAIL,
111 .args[0] = SCM_SMC_FNID(QCOM_SCM_SVC_INFO,
112 QCOM_SCM_INFO_IS_CALL_AVAIL) |
113 (ARM_SMCCC_OWNER_SIP << ARM_SMCCC_OWNER_SHIFT),
114 .arginfo = QCOM_SCM_ARGS(1),
115 .owner = ARM_SMCCC_OWNER_SIP,
117 struct qcom_scm_res res;
118 enum qcom_scm_convention probed_convention;
122 if (likely(qcom_scm_convention != SMC_CONVENTION_UNKNOWN))
123 return qcom_scm_convention;
126 * Device isn't required as there is only one argument - no device
127 * needed to dma_map_single to secure world
129 probed_convention = SMC_CONVENTION_ARM_64;
130 ret = __scm_smc_call(NULL, &desc, probed_convention, &res, true);
131 if (!ret && res.result[0] == 1)
135 * Some SC7180 firmwares didn't implement the
136 * QCOM_SCM_INFO_IS_CALL_AVAIL call, so we fallback to forcing ARM_64
137 * calling conventions on these firmwares. Luckily we don't make any
138 * early calls into the firmware on these SoCs so the device pointer
139 * will be valid here to check if the compatible matches.
141 if (of_device_is_compatible(__scm ? __scm->dev->of_node : NULL, "qcom,scm-sc7180")) {
146 probed_convention = SMC_CONVENTION_ARM_32;
147 ret = __scm_smc_call(NULL, &desc, probed_convention, &res, true);
148 if (!ret && res.result[0] == 1)
151 probed_convention = SMC_CONVENTION_LEGACY;
153 spin_lock_irqsave(&scm_query_lock, flags);
154 if (probed_convention != qcom_scm_convention) {
155 qcom_scm_convention = probed_convention;
156 pr_info("qcom_scm: convention: %s%s\n",
157 qcom_scm_convention_names[qcom_scm_convention],
158 forced ? " (forced)" : "");
160 spin_unlock_irqrestore(&scm_query_lock, flags);
162 return qcom_scm_convention;
166 * qcom_scm_call() - Invoke a syscall in the secure world
168 * @desc: Descriptor structure containing arguments and return values
169 * @res: Structure containing results from SMC/HVC call
171 * Sends a command to the SCM and waits for the command to finish processing.
172 * This should *only* be called in pre-emptible context.
174 static int qcom_scm_call(struct device *dev, const struct qcom_scm_desc *desc,
175 struct qcom_scm_res *res)
178 switch (__get_convention()) {
179 case SMC_CONVENTION_ARM_32:
180 case SMC_CONVENTION_ARM_64:
181 return scm_smc_call(dev, desc, res, false);
182 case SMC_CONVENTION_LEGACY:
183 return scm_legacy_call(dev, desc, res);
185 pr_err("Unknown current SCM calling convention.\n");
191 * qcom_scm_call_atomic() - atomic variation of qcom_scm_call()
193 * @desc: Descriptor structure containing arguments and return values
194 * @res: Structure containing results from SMC/HVC call
196 * Sends a command to the SCM and waits for the command to finish processing.
197 * This can be called in atomic context.
199 static int qcom_scm_call_atomic(struct device *dev,
200 const struct qcom_scm_desc *desc,
201 struct qcom_scm_res *res)
203 switch (__get_convention()) {
204 case SMC_CONVENTION_ARM_32:
205 case SMC_CONVENTION_ARM_64:
206 return scm_smc_call(dev, desc, res, true);
207 case SMC_CONVENTION_LEGACY:
208 return scm_legacy_call_atomic(dev, desc, res);
210 pr_err("Unknown current SCM calling convention.\n");
215 static bool __qcom_scm_is_call_available(struct device *dev, u32 svc_id,
219 struct qcom_scm_desc desc = {
220 .svc = QCOM_SCM_SVC_INFO,
221 .cmd = QCOM_SCM_INFO_IS_CALL_AVAIL,
222 .owner = ARM_SMCCC_OWNER_SIP,
224 struct qcom_scm_res res;
226 desc.arginfo = QCOM_SCM_ARGS(1);
227 switch (__get_convention()) {
228 case SMC_CONVENTION_ARM_32:
229 case SMC_CONVENTION_ARM_64:
230 desc.args[0] = SCM_SMC_FNID(svc_id, cmd_id) |
231 (ARM_SMCCC_OWNER_SIP << ARM_SMCCC_OWNER_SHIFT);
233 case SMC_CONVENTION_LEGACY:
234 desc.args[0] = SCM_LEGACY_FNID(svc_id, cmd_id);
237 pr_err("Unknown SMC convention being used\n");
241 ret = qcom_scm_call(dev, &desc, &res);
243 return ret ? false : !!res.result[0];
246 static int qcom_scm_set_boot_addr(void *entry, const u8 *cpu_bits)
249 unsigned int flags = 0;
250 struct qcom_scm_desc desc = {
251 .svc = QCOM_SCM_SVC_BOOT,
252 .cmd = QCOM_SCM_BOOT_SET_ADDR,
253 .arginfo = QCOM_SCM_ARGS(2),
254 .owner = ARM_SMCCC_OWNER_SIP,
257 for_each_present_cpu(cpu) {
258 if (cpu >= QCOM_SCM_BOOT_MAX_CPUS)
260 flags |= cpu_bits[cpu];
263 desc.args[0] = flags;
264 desc.args[1] = virt_to_phys(entry);
266 return qcom_scm_call_atomic(__scm ? __scm->dev : NULL, &desc, NULL);
269 static int qcom_scm_set_boot_addr_mc(void *entry, unsigned int flags)
271 struct qcom_scm_desc desc = {
272 .svc = QCOM_SCM_SVC_BOOT,
273 .cmd = QCOM_SCM_BOOT_SET_ADDR_MC,
274 .owner = ARM_SMCCC_OWNER_SIP,
275 .arginfo = QCOM_SCM_ARGS(6),
278 /* Apply to all CPUs in all affinity levels */
279 ~0ULL, ~0ULL, ~0ULL, ~0ULL,
284 /* Need a device for DMA of the additional arguments */
285 if (!__scm || __get_convention() == SMC_CONVENTION_LEGACY)
288 return qcom_scm_call(__scm->dev, &desc, NULL);
292 * qcom_scm_set_warm_boot_addr() - Set the warm boot address for all cpus
293 * @entry: Entry point function for the cpus
295 * Set the Linux entry point for the SCM to transfer control to when coming
296 * out of a power down. CPU power down may be executed on cpuidle or hotplug.
298 int qcom_scm_set_warm_boot_addr(void *entry)
300 if (qcom_scm_set_boot_addr_mc(entry, QCOM_SCM_BOOT_MC_FLAG_WARMBOOT))
301 /* Fallback to old SCM call */
302 return qcom_scm_set_boot_addr(entry, qcom_scm_cpu_warm_bits);
305 EXPORT_SYMBOL(qcom_scm_set_warm_boot_addr);
308 * qcom_scm_set_cold_boot_addr() - Set the cold boot address for all cpus
309 * @entry: Entry point function for the cpus
311 int qcom_scm_set_cold_boot_addr(void *entry)
313 if (qcom_scm_set_boot_addr_mc(entry, QCOM_SCM_BOOT_MC_FLAG_COLDBOOT))
314 /* Fallback to old SCM call */
315 return qcom_scm_set_boot_addr(entry, qcom_scm_cpu_cold_bits);
318 EXPORT_SYMBOL(qcom_scm_set_cold_boot_addr);
321 * qcom_scm_cpu_power_down() - Power down the cpu
322 * @flags: Flags to flush cache
324 * This is an end point to power down cpu. If there was a pending interrupt,
325 * the control would return from this function, otherwise, the cpu jumps to the
326 * warm boot entry point set for this cpu upon reset.
328 void qcom_scm_cpu_power_down(u32 flags)
330 struct qcom_scm_desc desc = {
331 .svc = QCOM_SCM_SVC_BOOT,
332 .cmd = QCOM_SCM_BOOT_TERMINATE_PC,
333 .args[0] = flags & QCOM_SCM_FLUSH_FLAG_MASK,
334 .arginfo = QCOM_SCM_ARGS(1),
335 .owner = ARM_SMCCC_OWNER_SIP,
338 qcom_scm_call_atomic(__scm ? __scm->dev : NULL, &desc, NULL);
340 EXPORT_SYMBOL(qcom_scm_cpu_power_down);
342 int qcom_scm_set_remote_state(u32 state, u32 id)
344 struct qcom_scm_desc desc = {
345 .svc = QCOM_SCM_SVC_BOOT,
346 .cmd = QCOM_SCM_BOOT_SET_REMOTE_STATE,
347 .arginfo = QCOM_SCM_ARGS(2),
350 .owner = ARM_SMCCC_OWNER_SIP,
352 struct qcom_scm_res res;
355 ret = qcom_scm_call(__scm->dev, &desc, &res);
357 return ret ? : res.result[0];
359 EXPORT_SYMBOL(qcom_scm_set_remote_state);
361 static int __qcom_scm_set_dload_mode(struct device *dev, bool enable)
363 struct qcom_scm_desc desc = {
364 .svc = QCOM_SCM_SVC_BOOT,
365 .cmd = QCOM_SCM_BOOT_SET_DLOAD_MODE,
366 .arginfo = QCOM_SCM_ARGS(2),
367 .args[0] = QCOM_SCM_BOOT_SET_DLOAD_MODE,
368 .owner = ARM_SMCCC_OWNER_SIP,
371 desc.args[1] = enable ? QCOM_SCM_BOOT_SET_DLOAD_MODE : 0;
373 return qcom_scm_call_atomic(__scm->dev, &desc, NULL);
376 static void qcom_scm_set_download_mode(bool enable)
381 avail = __qcom_scm_is_call_available(__scm->dev,
383 QCOM_SCM_BOOT_SET_DLOAD_MODE);
385 ret = __qcom_scm_set_dload_mode(__scm->dev, enable);
386 } else if (__scm->dload_mode_addr) {
387 ret = qcom_scm_io_writel(__scm->dload_mode_addr,
388 enable ? QCOM_SCM_BOOT_SET_DLOAD_MODE : 0);
391 "No available mechanism for setting download mode\n");
395 dev_err(__scm->dev, "failed to set download mode: %d\n", ret);
399 * qcom_scm_pas_init_image() - Initialize peripheral authentication service
400 * state machine for a given peripheral, using the
402 * @peripheral: peripheral id
403 * @metadata: pointer to memory containing ELF header, program header table
404 * and optional blob of data used for authenticating the metadata
405 * and the rest of the firmware
406 * @size: size of the metadata
407 * @ctx: optional metadata context
409 * Return: 0 on success.
411 * Upon successful return, the PAS metadata context (@ctx) will be used to
412 * track the metadata allocation, this needs to be released by invoking
413 * qcom_scm_pas_metadata_release() by the caller.
415 int qcom_scm_pas_init_image(u32 peripheral, const void *metadata, size_t size,
416 struct qcom_scm_pas_metadata *ctx)
418 dma_addr_t mdata_phys;
421 struct qcom_scm_desc desc = {
422 .svc = QCOM_SCM_SVC_PIL,
423 .cmd = QCOM_SCM_PIL_PAS_INIT_IMAGE,
424 .arginfo = QCOM_SCM_ARGS(2, QCOM_SCM_VAL, QCOM_SCM_RW),
425 .args[0] = peripheral,
426 .owner = ARM_SMCCC_OWNER_SIP,
428 struct qcom_scm_res res;
431 * During the scm call memory protection will be enabled for the meta
432 * data blob, so make sure it's physically contiguous, 4K aligned and
433 * non-cachable to avoid XPU violations.
435 mdata_buf = dma_alloc_coherent(__scm->dev, size, &mdata_phys,
438 dev_err(__scm->dev, "Allocation of metadata buffer failed.\n");
441 memcpy(mdata_buf, metadata, size);
443 ret = qcom_scm_clk_enable();
447 desc.args[1] = mdata_phys;
449 ret = qcom_scm_call(__scm->dev, &desc, &res);
451 qcom_scm_clk_disable();
454 if (ret < 0 || !ctx) {
455 dma_free_coherent(__scm->dev, size, mdata_buf, mdata_phys);
457 ctx->ptr = mdata_buf;
458 ctx->phys = mdata_phys;
462 return ret ? : res.result[0];
464 EXPORT_SYMBOL(qcom_scm_pas_init_image);
467 * qcom_scm_pas_metadata_release() - release metadata context
468 * @ctx: metadata context
470 void qcom_scm_pas_metadata_release(struct qcom_scm_pas_metadata *ctx)
475 dma_free_coherent(__scm->dev, ctx->size, ctx->ptr, ctx->phys);
481 EXPORT_SYMBOL(qcom_scm_pas_metadata_release);
484 * qcom_scm_pas_mem_setup() - Prepare the memory related to a given peripheral
485 * for firmware loading
486 * @peripheral: peripheral id
487 * @addr: start address of memory area to prepare
488 * @size: size of the memory area to prepare
490 * Returns 0 on success.
492 int qcom_scm_pas_mem_setup(u32 peripheral, phys_addr_t addr, phys_addr_t size)
495 struct qcom_scm_desc desc = {
496 .svc = QCOM_SCM_SVC_PIL,
497 .cmd = QCOM_SCM_PIL_PAS_MEM_SETUP,
498 .arginfo = QCOM_SCM_ARGS(3),
499 .args[0] = peripheral,
502 .owner = ARM_SMCCC_OWNER_SIP,
504 struct qcom_scm_res res;
506 ret = qcom_scm_clk_enable();
510 ret = qcom_scm_call(__scm->dev, &desc, &res);
511 qcom_scm_clk_disable();
513 return ret ? : res.result[0];
515 EXPORT_SYMBOL(qcom_scm_pas_mem_setup);
518 * qcom_scm_pas_auth_and_reset() - Authenticate the given peripheral firmware
519 * and reset the remote processor
520 * @peripheral: peripheral id
522 * Return 0 on success.
524 int qcom_scm_pas_auth_and_reset(u32 peripheral)
527 struct qcom_scm_desc desc = {
528 .svc = QCOM_SCM_SVC_PIL,
529 .cmd = QCOM_SCM_PIL_PAS_AUTH_AND_RESET,
530 .arginfo = QCOM_SCM_ARGS(1),
531 .args[0] = peripheral,
532 .owner = ARM_SMCCC_OWNER_SIP,
534 struct qcom_scm_res res;
536 ret = qcom_scm_clk_enable();
540 ret = qcom_scm_call(__scm->dev, &desc, &res);
541 qcom_scm_clk_disable();
543 return ret ? : res.result[0];
545 EXPORT_SYMBOL(qcom_scm_pas_auth_and_reset);
548 * qcom_scm_pas_shutdown() - Shut down the remote processor
549 * @peripheral: peripheral id
551 * Returns 0 on success.
553 int qcom_scm_pas_shutdown(u32 peripheral)
556 struct qcom_scm_desc desc = {
557 .svc = QCOM_SCM_SVC_PIL,
558 .cmd = QCOM_SCM_PIL_PAS_SHUTDOWN,
559 .arginfo = QCOM_SCM_ARGS(1),
560 .args[0] = peripheral,
561 .owner = ARM_SMCCC_OWNER_SIP,
563 struct qcom_scm_res res;
565 ret = qcom_scm_clk_enable();
569 ret = qcom_scm_call(__scm->dev, &desc, &res);
571 qcom_scm_clk_disable();
573 return ret ? : res.result[0];
575 EXPORT_SYMBOL(qcom_scm_pas_shutdown);
578 * qcom_scm_pas_supported() - Check if the peripheral authentication service is
579 * available for the given peripherial
580 * @peripheral: peripheral id
582 * Returns true if PAS is supported for this peripheral, otherwise false.
584 bool qcom_scm_pas_supported(u32 peripheral)
587 struct qcom_scm_desc desc = {
588 .svc = QCOM_SCM_SVC_PIL,
589 .cmd = QCOM_SCM_PIL_PAS_IS_SUPPORTED,
590 .arginfo = QCOM_SCM_ARGS(1),
591 .args[0] = peripheral,
592 .owner = ARM_SMCCC_OWNER_SIP,
594 struct qcom_scm_res res;
596 if (!__qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_PIL,
597 QCOM_SCM_PIL_PAS_IS_SUPPORTED))
600 ret = qcom_scm_call(__scm->dev, &desc, &res);
602 return ret ? false : !!res.result[0];
604 EXPORT_SYMBOL(qcom_scm_pas_supported);
606 static int __qcom_scm_pas_mss_reset(struct device *dev, bool reset)
608 struct qcom_scm_desc desc = {
609 .svc = QCOM_SCM_SVC_PIL,
610 .cmd = QCOM_SCM_PIL_PAS_MSS_RESET,
611 .arginfo = QCOM_SCM_ARGS(2),
614 .owner = ARM_SMCCC_OWNER_SIP,
616 struct qcom_scm_res res;
619 ret = qcom_scm_call(__scm->dev, &desc, &res);
621 return ret ? : res.result[0];
624 static int qcom_scm_pas_reset_assert(struct reset_controller_dev *rcdev,
630 return __qcom_scm_pas_mss_reset(__scm->dev, 1);
633 static int qcom_scm_pas_reset_deassert(struct reset_controller_dev *rcdev,
639 return __qcom_scm_pas_mss_reset(__scm->dev, 0);
642 static const struct reset_control_ops qcom_scm_pas_reset_ops = {
643 .assert = qcom_scm_pas_reset_assert,
644 .deassert = qcom_scm_pas_reset_deassert,
647 int qcom_scm_io_readl(phys_addr_t addr, unsigned int *val)
649 struct qcom_scm_desc desc = {
650 .svc = QCOM_SCM_SVC_IO,
651 .cmd = QCOM_SCM_IO_READ,
652 .arginfo = QCOM_SCM_ARGS(1),
654 .owner = ARM_SMCCC_OWNER_SIP,
656 struct qcom_scm_res res;
660 ret = qcom_scm_call_atomic(__scm->dev, &desc, &res);
662 *val = res.result[0];
664 return ret < 0 ? ret : 0;
666 EXPORT_SYMBOL(qcom_scm_io_readl);
668 int qcom_scm_io_writel(phys_addr_t addr, unsigned int val)
670 struct qcom_scm_desc desc = {
671 .svc = QCOM_SCM_SVC_IO,
672 .cmd = QCOM_SCM_IO_WRITE,
673 .arginfo = QCOM_SCM_ARGS(2),
676 .owner = ARM_SMCCC_OWNER_SIP,
679 return qcom_scm_call_atomic(__scm->dev, &desc, NULL);
681 EXPORT_SYMBOL(qcom_scm_io_writel);
684 * qcom_scm_restore_sec_cfg_available() - Check if secure environment
685 * supports restore security config interface.
687 * Return true if restore-cfg interface is supported, false if not.
689 bool qcom_scm_restore_sec_cfg_available(void)
691 return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_MP,
692 QCOM_SCM_MP_RESTORE_SEC_CFG);
694 EXPORT_SYMBOL(qcom_scm_restore_sec_cfg_available);
696 int qcom_scm_restore_sec_cfg(u32 device_id, u32 spare)
698 struct qcom_scm_desc desc = {
699 .svc = QCOM_SCM_SVC_MP,
700 .cmd = QCOM_SCM_MP_RESTORE_SEC_CFG,
701 .arginfo = QCOM_SCM_ARGS(2),
702 .args[0] = device_id,
704 .owner = ARM_SMCCC_OWNER_SIP,
706 struct qcom_scm_res res;
709 ret = qcom_scm_call(__scm->dev, &desc, &res);
711 return ret ? : res.result[0];
713 EXPORT_SYMBOL(qcom_scm_restore_sec_cfg);
715 int qcom_scm_iommu_secure_ptbl_size(u32 spare, size_t *size)
717 struct qcom_scm_desc desc = {
718 .svc = QCOM_SCM_SVC_MP,
719 .cmd = QCOM_SCM_MP_IOMMU_SECURE_PTBL_SIZE,
720 .arginfo = QCOM_SCM_ARGS(1),
722 .owner = ARM_SMCCC_OWNER_SIP,
724 struct qcom_scm_res res;
727 ret = qcom_scm_call(__scm->dev, &desc, &res);
730 *size = res.result[0];
732 return ret ? : res.result[1];
734 EXPORT_SYMBOL(qcom_scm_iommu_secure_ptbl_size);
736 int qcom_scm_iommu_secure_ptbl_init(u64 addr, u32 size, u32 spare)
738 struct qcom_scm_desc desc = {
739 .svc = QCOM_SCM_SVC_MP,
740 .cmd = QCOM_SCM_MP_IOMMU_SECURE_PTBL_INIT,
741 .arginfo = QCOM_SCM_ARGS(3, QCOM_SCM_RW, QCOM_SCM_VAL,
746 .owner = ARM_SMCCC_OWNER_SIP,
750 ret = qcom_scm_call(__scm->dev, &desc, NULL);
752 /* the pg table has been initialized already, ignore the error */
758 EXPORT_SYMBOL(qcom_scm_iommu_secure_ptbl_init);
760 int qcom_scm_iommu_set_cp_pool_size(u32 spare, u32 size)
762 struct qcom_scm_desc desc = {
763 .svc = QCOM_SCM_SVC_MP,
764 .cmd = QCOM_SCM_MP_IOMMU_SET_CP_POOL_SIZE,
765 .arginfo = QCOM_SCM_ARGS(2),
768 .owner = ARM_SMCCC_OWNER_SIP,
771 return qcom_scm_call(__scm->dev, &desc, NULL);
773 EXPORT_SYMBOL(qcom_scm_iommu_set_cp_pool_size);
775 int qcom_scm_mem_protect_video_var(u32 cp_start, u32 cp_size,
776 u32 cp_nonpixel_start,
777 u32 cp_nonpixel_size)
780 struct qcom_scm_desc desc = {
781 .svc = QCOM_SCM_SVC_MP,
782 .cmd = QCOM_SCM_MP_VIDEO_VAR,
783 .arginfo = QCOM_SCM_ARGS(4, QCOM_SCM_VAL, QCOM_SCM_VAL,
784 QCOM_SCM_VAL, QCOM_SCM_VAL),
787 .args[2] = cp_nonpixel_start,
788 .args[3] = cp_nonpixel_size,
789 .owner = ARM_SMCCC_OWNER_SIP,
791 struct qcom_scm_res res;
793 ret = qcom_scm_call(__scm->dev, &desc, &res);
795 return ret ? : res.result[0];
797 EXPORT_SYMBOL(qcom_scm_mem_protect_video_var);
799 static int __qcom_scm_assign_mem(struct device *dev, phys_addr_t mem_region,
800 size_t mem_sz, phys_addr_t src, size_t src_sz,
801 phys_addr_t dest, size_t dest_sz)
804 struct qcom_scm_desc desc = {
805 .svc = QCOM_SCM_SVC_MP,
806 .cmd = QCOM_SCM_MP_ASSIGN,
807 .arginfo = QCOM_SCM_ARGS(7, QCOM_SCM_RO, QCOM_SCM_VAL,
808 QCOM_SCM_RO, QCOM_SCM_VAL, QCOM_SCM_RO,
809 QCOM_SCM_VAL, QCOM_SCM_VAL),
810 .args[0] = mem_region,
817 .owner = ARM_SMCCC_OWNER_SIP,
819 struct qcom_scm_res res;
821 ret = qcom_scm_call(dev, &desc, &res);
823 return ret ? : res.result[0];
827 * qcom_scm_assign_mem() - Make a secure call to reassign memory ownership
828 * @mem_addr: mem region whose ownership need to be reassigned
829 * @mem_sz: size of the region.
830 * @srcvm: vmid for current set of owners, each set bit in
831 * flag indicate a unique owner
832 * @newvm: array having new owners and corresponding permission
834 * @dest_cnt: number of owners in next set.
836 * Return negative errno on failure or 0 on success with @srcvm updated.
838 int qcom_scm_assign_mem(phys_addr_t mem_addr, size_t mem_sz,
840 const struct qcom_scm_vmperm *newvm,
841 unsigned int dest_cnt)
843 struct qcom_scm_current_perm_info *destvm;
844 struct qcom_scm_mem_map_info *mem_to_map;
845 phys_addr_t mem_to_map_phys;
846 phys_addr_t dest_phys;
848 size_t mem_to_map_sz;
856 unsigned long srcvm_bits = *srcvm;
858 src_sz = hweight_long(srcvm_bits) * sizeof(*src);
859 mem_to_map_sz = sizeof(*mem_to_map);
860 dest_sz = dest_cnt * sizeof(*destvm);
861 ptr_sz = ALIGN(src_sz, SZ_64) + ALIGN(mem_to_map_sz, SZ_64) +
862 ALIGN(dest_sz, SZ_64);
864 ptr = dma_alloc_coherent(__scm->dev, ptr_sz, &ptr_phys, GFP_KERNEL);
868 /* Fill source vmid detail */
871 for_each_set_bit(b, &srcvm_bits, BITS_PER_LONG)
872 src[i++] = cpu_to_le32(b);
874 /* Fill details of mem buff to map */
875 mem_to_map = ptr + ALIGN(src_sz, SZ_64);
876 mem_to_map_phys = ptr_phys + ALIGN(src_sz, SZ_64);
877 mem_to_map->mem_addr = cpu_to_le64(mem_addr);
878 mem_to_map->mem_size = cpu_to_le64(mem_sz);
881 /* Fill details of next vmid detail */
882 destvm = ptr + ALIGN(mem_to_map_sz, SZ_64) + ALIGN(src_sz, SZ_64);
883 dest_phys = ptr_phys + ALIGN(mem_to_map_sz, SZ_64) + ALIGN(src_sz, SZ_64);
884 for (i = 0; i < dest_cnt; i++, destvm++, newvm++) {
885 destvm->vmid = cpu_to_le32(newvm->vmid);
886 destvm->perm = cpu_to_le32(newvm->perm);
888 destvm->ctx_size = 0;
889 next_vm |= BIT(newvm->vmid);
892 ret = __qcom_scm_assign_mem(__scm->dev, mem_to_map_phys, mem_to_map_sz,
893 ptr_phys, src_sz, dest_phys, dest_sz);
894 dma_free_coherent(__scm->dev, ptr_sz, ptr, ptr_phys);
897 "Assign memory protection call failed %d\n", ret);
904 EXPORT_SYMBOL(qcom_scm_assign_mem);
907 * qcom_scm_ocmem_lock_available() - is OCMEM lock/unlock interface available
909 bool qcom_scm_ocmem_lock_available(void)
911 return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_OCMEM,
912 QCOM_SCM_OCMEM_LOCK_CMD);
914 EXPORT_SYMBOL(qcom_scm_ocmem_lock_available);
917 * qcom_scm_ocmem_lock() - call OCMEM lock interface to assign an OCMEM
918 * region to the specified initiator
920 * @id: tz initiator id
921 * @offset: OCMEM offset
923 * @mode: access mode (WIDE/NARROW)
925 int qcom_scm_ocmem_lock(enum qcom_scm_ocmem_client id, u32 offset, u32 size,
928 struct qcom_scm_desc desc = {
929 .svc = QCOM_SCM_SVC_OCMEM,
930 .cmd = QCOM_SCM_OCMEM_LOCK_CMD,
935 .arginfo = QCOM_SCM_ARGS(4),
938 return qcom_scm_call(__scm->dev, &desc, NULL);
940 EXPORT_SYMBOL(qcom_scm_ocmem_lock);
943 * qcom_scm_ocmem_unlock() - call OCMEM unlock interface to release an OCMEM
944 * region from the specified initiator
946 * @id: tz initiator id
947 * @offset: OCMEM offset
950 int qcom_scm_ocmem_unlock(enum qcom_scm_ocmem_client id, u32 offset, u32 size)
952 struct qcom_scm_desc desc = {
953 .svc = QCOM_SCM_SVC_OCMEM,
954 .cmd = QCOM_SCM_OCMEM_UNLOCK_CMD,
958 .arginfo = QCOM_SCM_ARGS(3),
961 return qcom_scm_call(__scm->dev, &desc, NULL);
963 EXPORT_SYMBOL(qcom_scm_ocmem_unlock);
966 * qcom_scm_ice_available() - Is the ICE key programming interface available?
968 * Return: true iff the SCM calls wrapped by qcom_scm_ice_invalidate_key() and
969 * qcom_scm_ice_set_key() are available.
971 bool qcom_scm_ice_available(void)
973 return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_ES,
974 QCOM_SCM_ES_INVALIDATE_ICE_KEY) &&
975 __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_ES,
976 QCOM_SCM_ES_CONFIG_SET_ICE_KEY);
978 EXPORT_SYMBOL(qcom_scm_ice_available);
981 * qcom_scm_ice_invalidate_key() - Invalidate an inline encryption key
982 * @index: the keyslot to invalidate
984 * The UFSHCI and eMMC standards define a standard way to do this, but it
985 * doesn't work on these SoCs; only this SCM call does.
987 * It is assumed that the SoC has only one ICE instance being used, as this SCM
988 * call doesn't specify which ICE instance the keyslot belongs to.
990 * Return: 0 on success; -errno on failure.
992 int qcom_scm_ice_invalidate_key(u32 index)
994 struct qcom_scm_desc desc = {
995 .svc = QCOM_SCM_SVC_ES,
996 .cmd = QCOM_SCM_ES_INVALIDATE_ICE_KEY,
997 .arginfo = QCOM_SCM_ARGS(1),
999 .owner = ARM_SMCCC_OWNER_SIP,
1002 return qcom_scm_call(__scm->dev, &desc, NULL);
1004 EXPORT_SYMBOL(qcom_scm_ice_invalidate_key);
1007 * qcom_scm_ice_set_key() - Set an inline encryption key
1008 * @index: the keyslot into which to set the key
1009 * @key: the key to program
1010 * @key_size: the size of the key in bytes
1011 * @cipher: the encryption algorithm the key is for
1012 * @data_unit_size: the encryption data unit size, i.e. the size of each
1013 * individual plaintext and ciphertext. Given in 512-byte
1014 * units, e.g. 1 = 512 bytes, 8 = 4096 bytes, etc.
1016 * Program a key into a keyslot of Qualcomm ICE (Inline Crypto Engine), where it
1017 * can then be used to encrypt/decrypt UFS or eMMC I/O requests inline.
1019 * The UFSHCI and eMMC standards define a standard way to do this, but it
1020 * doesn't work on these SoCs; only this SCM call does.
1022 * It is assumed that the SoC has only one ICE instance being used, as this SCM
1023 * call doesn't specify which ICE instance the keyslot belongs to.
1025 * Return: 0 on success; -errno on failure.
1027 int qcom_scm_ice_set_key(u32 index, const u8 *key, u32 key_size,
1028 enum qcom_scm_ice_cipher cipher, u32 data_unit_size)
1030 struct qcom_scm_desc desc = {
1031 .svc = QCOM_SCM_SVC_ES,
1032 .cmd = QCOM_SCM_ES_CONFIG_SET_ICE_KEY,
1033 .arginfo = QCOM_SCM_ARGS(5, QCOM_SCM_VAL, QCOM_SCM_RW,
1034 QCOM_SCM_VAL, QCOM_SCM_VAL,
1037 .args[2] = key_size,
1039 .args[4] = data_unit_size,
1040 .owner = ARM_SMCCC_OWNER_SIP,
1043 dma_addr_t key_phys;
1047 * 'key' may point to vmalloc()'ed memory, but we need to pass a
1048 * physical address that's been properly flushed. The sanctioned way to
1049 * do this is by using the DMA API. But as is best practice for crypto
1050 * keys, we also must wipe the key after use. This makes kmemdup() +
1051 * dma_map_single() not clearly correct, since the DMA API can use
1052 * bounce buffers. Instead, just use dma_alloc_coherent(). Programming
1053 * keys is normally rare and thus not performance-critical.
1056 keybuf = dma_alloc_coherent(__scm->dev, key_size, &key_phys,
1060 memcpy(keybuf, key, key_size);
1061 desc.args[1] = key_phys;
1063 ret = qcom_scm_call(__scm->dev, &desc, NULL);
1065 memzero_explicit(keybuf, key_size);
1067 dma_free_coherent(__scm->dev, key_size, keybuf, key_phys);
1070 EXPORT_SYMBOL(qcom_scm_ice_set_key);
1073 * qcom_scm_hdcp_available() - Check if secure environment supports HDCP.
1075 * Return true if HDCP is supported, false if not.
1077 bool qcom_scm_hdcp_available(void)
1080 int ret = qcom_scm_clk_enable();
1085 avail = __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_HDCP,
1086 QCOM_SCM_HDCP_INVOKE);
1088 qcom_scm_clk_disable();
1092 EXPORT_SYMBOL(qcom_scm_hdcp_available);
1095 * qcom_scm_hdcp_req() - Send HDCP request.
1096 * @req: HDCP request array
1097 * @req_cnt: HDCP request array count
1098 * @resp: response buffer passed to SCM
1100 * Write HDCP register(s) through SCM.
1102 int qcom_scm_hdcp_req(struct qcom_scm_hdcp_req *req, u32 req_cnt, u32 *resp)
1105 struct qcom_scm_desc desc = {
1106 .svc = QCOM_SCM_SVC_HDCP,
1107 .cmd = QCOM_SCM_HDCP_INVOKE,
1108 .arginfo = QCOM_SCM_ARGS(10),
1121 .owner = ARM_SMCCC_OWNER_SIP,
1123 struct qcom_scm_res res;
1125 if (req_cnt > QCOM_SCM_HDCP_MAX_REQ_CNT)
1128 ret = qcom_scm_clk_enable();
1132 ret = qcom_scm_call(__scm->dev, &desc, &res);
1133 *resp = res.result[0];
1135 qcom_scm_clk_disable();
1139 EXPORT_SYMBOL(qcom_scm_hdcp_req);
1141 int qcom_scm_iommu_set_pt_format(u32 sec_id, u32 ctx_num, u32 pt_fmt)
1143 struct qcom_scm_desc desc = {
1144 .svc = QCOM_SCM_SVC_SMMU_PROGRAM,
1145 .cmd = QCOM_SCM_SMMU_PT_FORMAT,
1146 .arginfo = QCOM_SCM_ARGS(3),
1149 .args[2] = pt_fmt, /* 0: LPAE AArch32 - 1: AArch64 */
1150 .owner = ARM_SMCCC_OWNER_SIP,
1153 return qcom_scm_call(__scm->dev, &desc, NULL);
1155 EXPORT_SYMBOL(qcom_scm_iommu_set_pt_format);
1157 int qcom_scm_qsmmu500_wait_safe_toggle(bool en)
1159 struct qcom_scm_desc desc = {
1160 .svc = QCOM_SCM_SVC_SMMU_PROGRAM,
1161 .cmd = QCOM_SCM_SMMU_CONFIG_ERRATA1,
1162 .arginfo = QCOM_SCM_ARGS(2),
1163 .args[0] = QCOM_SCM_SMMU_CONFIG_ERRATA1_CLIENT_ALL,
1165 .owner = ARM_SMCCC_OWNER_SIP,
1169 return qcom_scm_call_atomic(__scm->dev, &desc, NULL);
1171 EXPORT_SYMBOL(qcom_scm_qsmmu500_wait_safe_toggle);
1173 bool qcom_scm_lmh_dcvsh_available(void)
1175 return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_LMH, QCOM_SCM_LMH_LIMIT_DCVSH);
1177 EXPORT_SYMBOL(qcom_scm_lmh_dcvsh_available);
1179 int qcom_scm_lmh_profile_change(u32 profile_id)
1181 struct qcom_scm_desc desc = {
1182 .svc = QCOM_SCM_SVC_LMH,
1183 .cmd = QCOM_SCM_LMH_LIMIT_PROFILE_CHANGE,
1184 .arginfo = QCOM_SCM_ARGS(1, QCOM_SCM_VAL),
1185 .args[0] = profile_id,
1186 .owner = ARM_SMCCC_OWNER_SIP,
1189 return qcom_scm_call(__scm->dev, &desc, NULL);
1191 EXPORT_SYMBOL(qcom_scm_lmh_profile_change);
1193 int qcom_scm_lmh_dcvsh(u32 payload_fn, u32 payload_reg, u32 payload_val,
1194 u64 limit_node, u32 node_id, u64 version)
1196 dma_addr_t payload_phys;
1198 int ret, payload_size = 5 * sizeof(u32);
1200 struct qcom_scm_desc desc = {
1201 .svc = QCOM_SCM_SVC_LMH,
1202 .cmd = QCOM_SCM_LMH_LIMIT_DCVSH,
1203 .arginfo = QCOM_SCM_ARGS(5, QCOM_SCM_RO, QCOM_SCM_VAL, QCOM_SCM_VAL,
1204 QCOM_SCM_VAL, QCOM_SCM_VAL),
1205 .args[1] = payload_size,
1206 .args[2] = limit_node,
1209 .owner = ARM_SMCCC_OWNER_SIP,
1212 payload_buf = dma_alloc_coherent(__scm->dev, payload_size, &payload_phys, GFP_KERNEL);
1216 payload_buf[0] = payload_fn;
1218 payload_buf[2] = payload_reg;
1220 payload_buf[4] = payload_val;
1222 desc.args[0] = payload_phys;
1224 ret = qcom_scm_call(__scm->dev, &desc, NULL);
1226 dma_free_coherent(__scm->dev, payload_size, payload_buf, payload_phys);
1229 EXPORT_SYMBOL(qcom_scm_lmh_dcvsh);
1231 static int qcom_scm_find_dload_address(struct device *dev, u64 *addr)
1233 struct device_node *tcsr;
1234 struct device_node *np = dev->of_node;
1235 struct resource res;
1239 tcsr = of_parse_phandle(np, "qcom,dload-mode", 0);
1243 ret = of_address_to_resource(tcsr, 0, &res);
1248 ret = of_property_read_u32_index(np, "qcom,dload-mode", 1, &offset);
1252 *addr = res.start + offset;
1258 * qcom_scm_is_available() - Checks if SCM is available
1260 bool qcom_scm_is_available(void)
1264 EXPORT_SYMBOL(qcom_scm_is_available);
1266 static int qcom_scm_probe(struct platform_device *pdev)
1268 struct qcom_scm *scm;
1272 scm = devm_kzalloc(&pdev->dev, sizeof(*scm), GFP_KERNEL);
1276 ret = qcom_scm_find_dload_address(&pdev->dev, &scm->dload_mode_addr);
1280 clks = (unsigned long)of_device_get_match_data(&pdev->dev);
1282 scm->core_clk = devm_clk_get(&pdev->dev, "core");
1283 if (IS_ERR(scm->core_clk)) {
1284 if (PTR_ERR(scm->core_clk) == -EPROBE_DEFER)
1285 return PTR_ERR(scm->core_clk);
1287 if (clks & SCM_HAS_CORE_CLK) {
1288 dev_err(&pdev->dev, "failed to acquire core clk\n");
1289 return PTR_ERR(scm->core_clk);
1292 scm->core_clk = NULL;
1295 scm->iface_clk = devm_clk_get(&pdev->dev, "iface");
1296 if (IS_ERR(scm->iface_clk)) {
1297 if (PTR_ERR(scm->iface_clk) == -EPROBE_DEFER)
1298 return PTR_ERR(scm->iface_clk);
1300 if (clks & SCM_HAS_IFACE_CLK) {
1301 dev_err(&pdev->dev, "failed to acquire iface clk\n");
1302 return PTR_ERR(scm->iface_clk);
1305 scm->iface_clk = NULL;
1308 scm->bus_clk = devm_clk_get(&pdev->dev, "bus");
1309 if (IS_ERR(scm->bus_clk)) {
1310 if (PTR_ERR(scm->bus_clk) == -EPROBE_DEFER)
1311 return PTR_ERR(scm->bus_clk);
1313 if (clks & SCM_HAS_BUS_CLK) {
1314 dev_err(&pdev->dev, "failed to acquire bus clk\n");
1315 return PTR_ERR(scm->bus_clk);
1318 scm->bus_clk = NULL;
1321 scm->reset.ops = &qcom_scm_pas_reset_ops;
1322 scm->reset.nr_resets = 1;
1323 scm->reset.of_node = pdev->dev.of_node;
1324 ret = devm_reset_controller_register(&pdev->dev, &scm->reset);
1328 /* vote for max clk rate for highest performance */
1329 ret = clk_set_rate(scm->core_clk, INT_MAX);
1334 __scm->dev = &pdev->dev;
1339 * If requested enable "download mode", from this point on warmboot
1340 * will cause the the boot stages to enter download mode, unless
1341 * disabled below by a clean shutdown/reboot.
1344 qcom_scm_set_download_mode(true);
1349 static void qcom_scm_shutdown(struct platform_device *pdev)
1351 /* Clean shutdown, disable download mode to allow normal restart */
1353 qcom_scm_set_download_mode(false);
1356 static const struct of_device_id qcom_scm_dt_match[] = {
1357 { .compatible = "qcom,scm-apq8064",
1358 /* FIXME: This should have .data = (void *) SCM_HAS_CORE_CLK */
1360 { .compatible = "qcom,scm-apq8084", .data = (void *)(SCM_HAS_CORE_CLK |
1364 { .compatible = "qcom,scm-ipq4019" },
1365 { .compatible = "qcom,scm-mdm9607", .data = (void *)(SCM_HAS_CORE_CLK |
1368 { .compatible = "qcom,scm-msm8660", .data = (void *) SCM_HAS_CORE_CLK },
1369 { .compatible = "qcom,scm-msm8960", .data = (void *) SCM_HAS_CORE_CLK },
1370 { .compatible = "qcom,scm-msm8916", .data = (void *)(SCM_HAS_CORE_CLK |
1374 { .compatible = "qcom,scm-msm8953", .data = (void *)(SCM_HAS_CORE_CLK |
1378 { .compatible = "qcom,scm-msm8974", .data = (void *)(SCM_HAS_CORE_CLK |
1382 { .compatible = "qcom,scm-msm8976", .data = (void *)(SCM_HAS_CORE_CLK |
1386 { .compatible = "qcom,scm-msm8994" },
1387 { .compatible = "qcom,scm-msm8996" },
1388 { .compatible = "qcom,scm" },
1391 MODULE_DEVICE_TABLE(of, qcom_scm_dt_match);
1393 static struct platform_driver qcom_scm_driver = {
1396 .of_match_table = qcom_scm_dt_match,
1397 .suppress_bind_attrs = true,
1399 .probe = qcom_scm_probe,
1400 .shutdown = qcom_scm_shutdown,
1403 static int __init qcom_scm_init(void)
1405 return platform_driver_register(&qcom_scm_driver);
1407 subsys_initcall(qcom_scm_init);
1409 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. SCM driver");
1410 MODULE_LICENSE("GPL v2");