1 // SPDX-License-Identifier: GPL-2.0-only
3 * Qualcomm self-authenticating modem subsystem remoteproc driver
5 * Copyright (C) 2016 Linaro Ltd.
6 * Copyright (C) 2014 Sony Mobile Communications AB
7 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/devcoredump.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/module.h>
18 #include <linux/of_address.h>
19 #include <linux/of_device.h>
20 #include <linux/of_reserved_mem.h>
21 #include <linux/platform_device.h>
22 #include <linux/pm_domain.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/regmap.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/remoteproc.h>
27 #include <linux/reset.h>
28 #include <linux/soc/qcom/mdt_loader.h>
29 #include <linux/iopoll.h>
30 #include <linux/slab.h>
32 #include "remoteproc_internal.h"
33 #include "qcom_common.h"
34 #include "qcom_pil_info.h"
35 #include "qcom_q6v5.h"
37 #include <linux/firmware/qcom/qcom_scm.h>
39 #define MPSS_CRASH_REASON_SMEM 421
41 #define MBA_LOG_SIZE SZ_4K
45 /* RMB Status Register Values */
46 #define RMB_PBL_SUCCESS 0x1
48 #define RMB_MBA_XPU_UNLOCKED 0x1
49 #define RMB_MBA_XPU_UNLOCKED_SCRIBBLED 0x2
50 #define RMB_MBA_META_DATA_AUTH_SUCCESS 0x3
51 #define RMB_MBA_AUTH_COMPLETE 0x4
53 /* PBL/MBA interface registers */
54 #define RMB_MBA_IMAGE_REG 0x00
55 #define RMB_PBL_STATUS_REG 0x04
56 #define RMB_MBA_COMMAND_REG 0x08
57 #define RMB_MBA_STATUS_REG 0x0C
58 #define RMB_PMI_META_DATA_REG 0x10
59 #define RMB_PMI_CODE_START_REG 0x14
60 #define RMB_PMI_CODE_LENGTH_REG 0x18
61 #define RMB_MBA_MSS_STATUS 0x40
62 #define RMB_MBA_ALT_RESET 0x44
64 #define RMB_CMD_META_DATA_READY 0x1
65 #define RMB_CMD_LOAD_READY 0x2
67 /* QDSP6SS Register Offsets */
68 #define QDSP6SS_RESET_REG 0x014
69 #define QDSP6SS_GFMUX_CTL_REG 0x020
70 #define QDSP6SS_PWR_CTL_REG 0x030
71 #define QDSP6SS_MEM_PWR_CTL 0x0B0
72 #define QDSP6V6SS_MEM_PWR_CTL 0x034
73 #define QDSP6SS_STRAP_ACC 0x110
75 /* AXI Halt Register Offsets */
76 #define AXI_HALTREQ_REG 0x0
77 #define AXI_HALTACK_REG 0x4
78 #define AXI_IDLE_REG 0x8
79 #define AXI_GATING_VALID_OVERRIDE BIT(0)
81 #define HALT_ACK_TIMEOUT_US 100000
83 /* QACCEPT Register Offsets */
84 #define QACCEPT_ACCEPT_REG 0x0
85 #define QACCEPT_ACTIVE_REG 0x4
86 #define QACCEPT_DENY_REG 0x8
87 #define QACCEPT_REQ_REG 0xC
89 #define QACCEPT_TIMEOUT_US 50
92 #define Q6SS_STOP_CORE BIT(0)
93 #define Q6SS_CORE_ARES BIT(1)
94 #define Q6SS_BUS_ARES_ENABLE BIT(2)
97 #define Q6SS_CBCR_CLKEN BIT(0)
98 #define Q6SS_CBCR_CLKOFF BIT(31)
99 #define Q6SS_CBCR_TIMEOUT_US 200
101 /* QDSP6SS_GFMUX_CTL */
102 #define Q6SS_CLK_ENABLE BIT(1)
104 /* QDSP6SS_PWR_CTL */
105 #define Q6SS_L2DATA_SLP_NRET_N_0 BIT(0)
106 #define Q6SS_L2DATA_SLP_NRET_N_1 BIT(1)
107 #define Q6SS_L2DATA_SLP_NRET_N_2 BIT(2)
108 #define Q6SS_L2TAG_SLP_NRET_N BIT(16)
109 #define Q6SS_ETB_SLP_NRET_N BIT(17)
110 #define Q6SS_L2DATA_STBY_N BIT(18)
111 #define Q6SS_SLP_RET_N BIT(19)
112 #define Q6SS_CLAMP_IO BIT(20)
113 #define QDSS_BHS_ON BIT(21)
114 #define QDSS_LDO_BYP BIT(22)
116 /* QDSP6v55 parameters */
117 #define QDSP6V55_MEM_BITS GENMASK(16, 8)
119 /* QDSP6v56 parameters */
120 #define QDSP6v56_LDO_BYP BIT(25)
121 #define QDSP6v56_BHS_ON BIT(24)
122 #define QDSP6v56_CLAMP_WL BIT(21)
123 #define QDSP6v56_CLAMP_QMC_MEM BIT(22)
124 #define QDSP6SS_XO_CBCR 0x0038
125 #define QDSP6SS_ACC_OVERRIDE_VAL 0x20
127 /* QDSP6v65 parameters */
128 #define QDSP6SS_CORE_CBCR 0x20
129 #define QDSP6SS_SLEEP 0x3C
130 #define QDSP6SS_BOOT_CORE_START 0x400
131 #define QDSP6SS_BOOT_CMD 0x404
132 #define BOOT_FSM_TIMEOUT 10000
135 struct regulator *reg;
140 struct qcom_mss_reg_res {
146 struct rproc_hexagon_res {
147 const char *hexagon_mba_image;
148 struct qcom_mss_reg_res *proxy_supply;
149 struct qcom_mss_reg_res *fallback_proxy_supply;
150 struct qcom_mss_reg_res *active_supply;
151 char **proxy_clk_names;
152 char **reset_clk_names;
153 char **active_clk_names;
154 char **proxy_pd_names;
156 bool need_mem_protection;
160 bool has_qaccept_regs;
161 bool has_ext_cntl_regs;
169 void __iomem *reg_base;
170 void __iomem *rmb_base;
172 struct regmap *halt_map;
173 struct regmap *conn_map;
190 struct reset_control *mss_restart;
191 struct reset_control *pdc_reset;
193 struct qcom_q6v5 q6v5;
195 struct clk *active_clks[8];
196 struct clk *reset_clks[4];
197 struct clk *proxy_clks[4];
198 struct device *proxy_pds[3];
199 int active_clk_count;
204 struct reg_info active_regs[1];
205 struct reg_info proxy_regs[1];
206 struct reg_info fallback_proxy_regs[2];
207 int active_reg_count;
209 int fallback_proxy_reg_count;
211 bool dump_mba_loaded;
212 size_t current_dump_size;
213 size_t total_dump_size;
215 phys_addr_t mba_phys;
219 phys_addr_t mdata_phys;
222 phys_addr_t mpss_phys;
223 phys_addr_t mpss_reloc;
226 struct qcom_rproc_glink glink_subdev;
227 struct qcom_rproc_subdev smd_subdev;
228 struct qcom_rproc_ssr ssr_subdev;
229 struct qcom_sysmon *sysmon;
230 struct platform_device *bam_dmux;
231 bool need_mem_protection;
235 bool has_qaccept_regs;
236 bool has_ext_cntl_regs;
240 const char *hexagon_mdt_image;
256 static int q6v5_regulator_init(struct device *dev, struct reg_info *regs,
257 const struct qcom_mss_reg_res *reg_res)
265 for (i = 0; reg_res[i].supply; i++) {
266 regs[i].reg = devm_regulator_get(dev, reg_res[i].supply);
267 if (IS_ERR(regs[i].reg)) {
268 rc = PTR_ERR(regs[i].reg);
269 if (rc != -EPROBE_DEFER)
270 dev_err(dev, "Failed to get %s\n regulator",
275 regs[i].uV = reg_res[i].uV;
276 regs[i].uA = reg_res[i].uA;
282 static int q6v5_regulator_enable(struct q6v5 *qproc,
283 struct reg_info *regs, int count)
288 for (i = 0; i < count; i++) {
289 if (regs[i].uV > 0) {
290 ret = regulator_set_voltage(regs[i].reg,
291 regs[i].uV, INT_MAX);
294 "Failed to request voltage for %d.\n",
300 if (regs[i].uA > 0) {
301 ret = regulator_set_load(regs[i].reg,
305 "Failed to set regulator mode\n");
310 ret = regulator_enable(regs[i].reg);
312 dev_err(qproc->dev, "Regulator enable failed\n");
319 for (; i >= 0; i--) {
321 regulator_set_voltage(regs[i].reg, 0, INT_MAX);
324 regulator_set_load(regs[i].reg, 0);
326 regulator_disable(regs[i].reg);
332 static void q6v5_regulator_disable(struct q6v5 *qproc,
333 struct reg_info *regs, int count)
337 for (i = 0; i < count; i++) {
339 regulator_set_voltage(regs[i].reg, 0, INT_MAX);
342 regulator_set_load(regs[i].reg, 0);
344 regulator_disable(regs[i].reg);
348 static int q6v5_clk_enable(struct device *dev,
349 struct clk **clks, int count)
354 for (i = 0; i < count; i++) {
355 rc = clk_prepare_enable(clks[i]);
357 dev_err(dev, "Clock enable failed\n");
364 for (i--; i >= 0; i--)
365 clk_disable_unprepare(clks[i]);
370 static void q6v5_clk_disable(struct device *dev,
371 struct clk **clks, int count)
375 for (i = 0; i < count; i++)
376 clk_disable_unprepare(clks[i]);
379 static int q6v5_pds_enable(struct q6v5 *qproc, struct device **pds,
385 for (i = 0; i < pd_count; i++) {
386 dev_pm_genpd_set_performance_state(pds[i], INT_MAX);
387 ret = pm_runtime_get_sync(pds[i]);
389 pm_runtime_put_noidle(pds[i]);
390 dev_pm_genpd_set_performance_state(pds[i], 0);
391 goto unroll_pd_votes;
398 for (i--; i >= 0; i--) {
399 dev_pm_genpd_set_performance_state(pds[i], 0);
400 pm_runtime_put(pds[i]);
406 static void q6v5_pds_disable(struct q6v5 *qproc, struct device **pds,
411 for (i = 0; i < pd_count; i++) {
412 dev_pm_genpd_set_performance_state(pds[i], 0);
413 pm_runtime_put(pds[i]);
417 static int q6v5_xfer_mem_ownership(struct q6v5 *qproc, u64 *current_perm,
418 bool local, bool remote, phys_addr_t addr,
421 struct qcom_scm_vmperm next[2];
424 if (!qproc->need_mem_protection)
427 if (local == !!(*current_perm & BIT(QCOM_SCM_VMID_HLOS)) &&
428 remote == !!(*current_perm & BIT(QCOM_SCM_VMID_MSS_MSA)))
432 next[perms].vmid = QCOM_SCM_VMID_HLOS;
433 next[perms].perm = QCOM_SCM_PERM_RWX;
438 next[perms].vmid = QCOM_SCM_VMID_MSS_MSA;
439 next[perms].perm = QCOM_SCM_PERM_RW;
443 return qcom_scm_assign_mem(addr, ALIGN(size, SZ_4K),
444 current_perm, next, perms);
447 static void q6v5_debug_policy_load(struct q6v5 *qproc, void *mba_region)
449 const struct firmware *dp_fw;
451 if (request_firmware_direct(&dp_fw, "msadp", qproc->dev))
454 if (SZ_1M + dp_fw->size <= qproc->mba_size) {
455 memcpy(mba_region + SZ_1M, dp_fw->data, dp_fw->size);
456 qproc->dp_size = dp_fw->size;
459 release_firmware(dp_fw);
462 static int q6v5_load(struct rproc *rproc, const struct firmware *fw)
464 struct q6v5 *qproc = rproc->priv;
467 /* MBA is restricted to a maximum size of 1M */
468 if (fw->size > qproc->mba_size || fw->size > SZ_1M) {
469 dev_err(qproc->dev, "MBA firmware load failed\n");
473 mba_region = memremap(qproc->mba_phys, qproc->mba_size, MEMREMAP_WC);
475 dev_err(qproc->dev, "unable to map memory region: %pa+%zx\n",
476 &qproc->mba_phys, qproc->mba_size);
480 memcpy(mba_region, fw->data, fw->size);
481 q6v5_debug_policy_load(qproc, mba_region);
482 memunmap(mba_region);
487 static int q6v5_reset_assert(struct q6v5 *qproc)
491 if (qproc->has_alt_reset) {
492 reset_control_assert(qproc->pdc_reset);
493 ret = reset_control_reset(qproc->mss_restart);
494 reset_control_deassert(qproc->pdc_reset);
495 } else if (qproc->has_spare_reg) {
497 * When the AXI pipeline is being reset with the Q6 modem partly
498 * operational there is possibility of AXI valid signal to
499 * glitch, leading to spurious transactions and Q6 hangs. A work
500 * around is employed by asserting the AXI_GATING_VALID_OVERRIDE
501 * BIT before triggering Q6 MSS reset. AXI_GATING_VALID_OVERRIDE
502 * is withdrawn post MSS assert followed by a MSS deassert,
503 * while holding the PDC reset.
505 reset_control_assert(qproc->pdc_reset);
506 regmap_update_bits(qproc->conn_map, qproc->conn_box,
507 AXI_GATING_VALID_OVERRIDE, 1);
508 reset_control_assert(qproc->mss_restart);
509 reset_control_deassert(qproc->pdc_reset);
510 regmap_update_bits(qproc->conn_map, qproc->conn_box,
511 AXI_GATING_VALID_OVERRIDE, 0);
512 ret = reset_control_deassert(qproc->mss_restart);
513 } else if (qproc->has_ext_cntl_regs) {
514 regmap_write(qproc->conn_map, qproc->rscc_disable, 0);
515 reset_control_assert(qproc->pdc_reset);
516 reset_control_assert(qproc->mss_restart);
517 reset_control_deassert(qproc->pdc_reset);
518 ret = reset_control_deassert(qproc->mss_restart);
520 ret = reset_control_assert(qproc->mss_restart);
526 static int q6v5_reset_deassert(struct q6v5 *qproc)
530 if (qproc->has_alt_reset) {
531 reset_control_assert(qproc->pdc_reset);
532 writel(1, qproc->rmb_base + RMB_MBA_ALT_RESET);
533 ret = reset_control_reset(qproc->mss_restart);
534 writel(0, qproc->rmb_base + RMB_MBA_ALT_RESET);
535 reset_control_deassert(qproc->pdc_reset);
536 } else if (qproc->has_spare_reg || qproc->has_ext_cntl_regs) {
537 ret = reset_control_reset(qproc->mss_restart);
539 ret = reset_control_deassert(qproc->mss_restart);
545 static int q6v5_rmb_pbl_wait(struct q6v5 *qproc, int ms)
547 unsigned long timeout;
550 timeout = jiffies + msecs_to_jiffies(ms);
552 val = readl(qproc->rmb_base + RMB_PBL_STATUS_REG);
556 if (time_after(jiffies, timeout))
565 static int q6v5_rmb_mba_wait(struct q6v5 *qproc, u32 status, int ms)
568 unsigned long timeout;
571 timeout = jiffies + msecs_to_jiffies(ms);
573 val = readl(qproc->rmb_base + RMB_MBA_STATUS_REG);
579 else if (status && val == status)
582 if (time_after(jiffies, timeout))
591 static void q6v5_dump_mba_logs(struct q6v5 *qproc)
593 struct rproc *rproc = qproc->rproc;
597 if (!qproc->has_mba_logs)
600 if (q6v5_xfer_mem_ownership(qproc, &qproc->mba_perm, true, false, qproc->mba_phys,
604 mba_region = memremap(qproc->mba_phys, qproc->mba_size, MEMREMAP_WC);
608 data = vmalloc(MBA_LOG_SIZE);
610 memcpy(data, mba_region, MBA_LOG_SIZE);
611 dev_coredumpv(&rproc->dev, data, MBA_LOG_SIZE, GFP_KERNEL);
613 memunmap(mba_region);
616 static int q6v5proc_reset(struct q6v5 *qproc)
622 if (qproc->version == MSS_SDM845) {
623 val = readl(qproc->reg_base + QDSP6SS_SLEEP);
624 val |= Q6SS_CBCR_CLKEN;
625 writel(val, qproc->reg_base + QDSP6SS_SLEEP);
627 ret = readl_poll_timeout(qproc->reg_base + QDSP6SS_SLEEP,
628 val, !(val & Q6SS_CBCR_CLKOFF), 1,
629 Q6SS_CBCR_TIMEOUT_US);
631 dev_err(qproc->dev, "QDSP6SS Sleep clock timed out\n");
635 /* De-assert QDSP6 stop core */
636 writel(1, qproc->reg_base + QDSP6SS_BOOT_CORE_START);
637 /* Trigger boot FSM */
638 writel(1, qproc->reg_base + QDSP6SS_BOOT_CMD);
640 ret = readl_poll_timeout(qproc->rmb_base + RMB_MBA_MSS_STATUS,
641 val, (val & BIT(0)) != 0, 10, BOOT_FSM_TIMEOUT);
643 dev_err(qproc->dev, "Boot FSM failed to complete.\n");
644 /* Reset the modem so that boot FSM is in reset state */
645 q6v5_reset_deassert(qproc);
650 } else if (qproc->version == MSS_SC7180 || qproc->version == MSS_SC7280) {
651 val = readl(qproc->reg_base + QDSP6SS_SLEEP);
652 val |= Q6SS_CBCR_CLKEN;
653 writel(val, qproc->reg_base + QDSP6SS_SLEEP);
655 ret = readl_poll_timeout(qproc->reg_base + QDSP6SS_SLEEP,
656 val, !(val & Q6SS_CBCR_CLKOFF), 1,
657 Q6SS_CBCR_TIMEOUT_US);
659 dev_err(qproc->dev, "QDSP6SS Sleep clock timed out\n");
663 /* Turn on the XO clock needed for PLL setup */
664 val = readl(qproc->reg_base + QDSP6SS_XO_CBCR);
665 val |= Q6SS_CBCR_CLKEN;
666 writel(val, qproc->reg_base + QDSP6SS_XO_CBCR);
668 ret = readl_poll_timeout(qproc->reg_base + QDSP6SS_XO_CBCR,
669 val, !(val & Q6SS_CBCR_CLKOFF), 1,
670 Q6SS_CBCR_TIMEOUT_US);
672 dev_err(qproc->dev, "QDSP6SS XO clock timed out\n");
676 /* Configure Q6 core CBCR to auto-enable after reset sequence */
677 val = readl(qproc->reg_base + QDSP6SS_CORE_CBCR);
678 val |= Q6SS_CBCR_CLKEN;
679 writel(val, qproc->reg_base + QDSP6SS_CORE_CBCR);
681 /* De-assert the Q6 stop core signal */
682 writel(1, qproc->reg_base + QDSP6SS_BOOT_CORE_START);
684 /* Wait for 10 us for any staggering logic to settle */
685 usleep_range(10, 20);
687 /* Trigger the boot FSM to start the Q6 out-of-reset sequence */
688 writel(1, qproc->reg_base + QDSP6SS_BOOT_CMD);
690 /* Poll the MSS_STATUS for FSM completion */
691 ret = readl_poll_timeout(qproc->rmb_base + RMB_MBA_MSS_STATUS,
692 val, (val & BIT(0)) != 0, 10, BOOT_FSM_TIMEOUT);
694 dev_err(qproc->dev, "Boot FSM failed to complete.\n");
695 /* Reset the modem so that boot FSM is in reset state */
696 q6v5_reset_deassert(qproc);
700 } else if (qproc->version == MSS_MSM8909 ||
701 qproc->version == MSS_MSM8953 ||
702 qproc->version == MSS_MSM8996 ||
703 qproc->version == MSS_MSM8998) {
705 if (qproc->version != MSS_MSM8909 &&
706 qproc->version != MSS_MSM8953)
707 /* Override the ACC value if required */
708 writel(QDSP6SS_ACC_OVERRIDE_VAL,
709 qproc->reg_base + QDSP6SS_STRAP_ACC);
711 /* Assert resets, stop core */
712 val = readl(qproc->reg_base + QDSP6SS_RESET_REG);
713 val |= Q6SS_CORE_ARES | Q6SS_BUS_ARES_ENABLE | Q6SS_STOP_CORE;
714 writel(val, qproc->reg_base + QDSP6SS_RESET_REG);
716 /* BHS require xo cbcr to be enabled */
717 val = readl(qproc->reg_base + QDSP6SS_XO_CBCR);
718 val |= Q6SS_CBCR_CLKEN;
719 writel(val, qproc->reg_base + QDSP6SS_XO_CBCR);
721 /* Read CLKOFF bit to go low indicating CLK is enabled */
722 ret = readl_poll_timeout(qproc->reg_base + QDSP6SS_XO_CBCR,
723 val, !(val & Q6SS_CBCR_CLKOFF), 1,
724 Q6SS_CBCR_TIMEOUT_US);
727 "xo cbcr enabling timed out (rc:%d)\n", ret);
730 /* Enable power block headswitch and wait for it to stabilize */
731 val = readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
732 val |= QDSP6v56_BHS_ON;
733 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
734 val |= readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
737 /* Put LDO in bypass mode */
738 val |= QDSP6v56_LDO_BYP;
739 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
741 if (qproc->version != MSS_MSM8909) {
744 /* Deassert QDSP6 compiler memory clamp */
745 val = readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
746 val &= ~QDSP6v56_CLAMP_QMC_MEM;
747 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
749 /* Deassert memory peripheral sleep and L2 memory standby */
750 val |= Q6SS_L2DATA_STBY_N | Q6SS_SLP_RET_N;
751 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
753 /* Turn on L1, L2, ETB and JU memories 1 at a time */
754 if (qproc->version == MSS_MSM8953 ||
755 qproc->version == MSS_MSM8996) {
756 mem_pwr_ctl = QDSP6SS_MEM_PWR_CTL;
760 mem_pwr_ctl = QDSP6V6SS_MEM_PWR_CTL;
763 val = readl(qproc->reg_base + mem_pwr_ctl);
764 for (; i >= 0; i--) {
766 writel(val, qproc->reg_base + mem_pwr_ctl);
768 * Read back value to ensure the write is done then
769 * wait for 1us for both memory peripheral and data
772 val |= readl(qproc->reg_base + mem_pwr_ctl);
776 /* Turn on memories */
777 val = readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
778 val |= Q6SS_SLP_RET_N | Q6SS_L2DATA_STBY_N |
779 Q6SS_ETB_SLP_NRET_N | QDSP6V55_MEM_BITS;
780 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
782 /* Turn on L2 banks 1 at a time */
783 for (i = 0; i <= 7; i++) {
785 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
789 /* Remove word line clamp */
790 val = readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
791 val &= ~QDSP6v56_CLAMP_WL;
792 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
794 /* Assert resets, stop core */
795 val = readl(qproc->reg_base + QDSP6SS_RESET_REG);
796 val |= Q6SS_CORE_ARES | Q6SS_BUS_ARES_ENABLE | Q6SS_STOP_CORE;
797 writel(val, qproc->reg_base + QDSP6SS_RESET_REG);
799 /* Enable power block headswitch and wait for it to stabilize */
800 val = readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
801 val |= QDSS_BHS_ON | QDSS_LDO_BYP;
802 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
803 val |= readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
806 * Turn on memories. L2 banks should be done individually
807 * to minimize inrush current.
809 val = readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
810 val |= Q6SS_SLP_RET_N | Q6SS_L2TAG_SLP_NRET_N |
811 Q6SS_ETB_SLP_NRET_N | Q6SS_L2DATA_STBY_N;
812 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
813 val |= Q6SS_L2DATA_SLP_NRET_N_2;
814 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
815 val |= Q6SS_L2DATA_SLP_NRET_N_1;
816 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
817 val |= Q6SS_L2DATA_SLP_NRET_N_0;
818 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
820 /* Remove IO clamp */
821 val &= ~Q6SS_CLAMP_IO;
822 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
824 /* Bring core out of reset */
825 val = readl(qproc->reg_base + QDSP6SS_RESET_REG);
826 val &= ~Q6SS_CORE_ARES;
827 writel(val, qproc->reg_base + QDSP6SS_RESET_REG);
829 /* Turn on core clock */
830 val = readl(qproc->reg_base + QDSP6SS_GFMUX_CTL_REG);
831 val |= Q6SS_CLK_ENABLE;
832 writel(val, qproc->reg_base + QDSP6SS_GFMUX_CTL_REG);
834 /* Start core execution */
835 val = readl(qproc->reg_base + QDSP6SS_RESET_REG);
836 val &= ~Q6SS_STOP_CORE;
837 writel(val, qproc->reg_base + QDSP6SS_RESET_REG);
840 /* Wait for PBL status */
841 ret = q6v5_rmb_pbl_wait(qproc, 1000);
842 if (ret == -ETIMEDOUT) {
843 dev_err(qproc->dev, "PBL boot timed out\n");
844 } else if (ret != RMB_PBL_SUCCESS) {
845 dev_err(qproc->dev, "PBL returned unexpected status %d\n", ret);
854 static int q6v5proc_enable_qchannel(struct q6v5 *qproc, struct regmap *map, u32 offset)
859 if (!qproc->has_qaccept_regs)
862 if (qproc->has_ext_cntl_regs) {
863 regmap_write(qproc->conn_map, qproc->rscc_disable, 0);
864 regmap_write(qproc->conn_map, qproc->force_clk_on, 1);
866 ret = regmap_read_poll_timeout(qproc->halt_map, qproc->axim1_clk_off, val,
867 !val, 1, Q6SS_CBCR_TIMEOUT_US);
869 dev_err(qproc->dev, "failed to enable axim1 clock\n");
874 regmap_write(map, offset + QACCEPT_REQ_REG, 1);
876 /* Wait for accept */
877 ret = regmap_read_poll_timeout(map, offset + QACCEPT_ACCEPT_REG, val, val, 5,
880 dev_err(qproc->dev, "qchannel enable failed\n");
887 static void q6v5proc_disable_qchannel(struct q6v5 *qproc, struct regmap *map, u32 offset)
890 unsigned int val, retry;
891 unsigned int nretry = 10;
892 bool takedown_complete = false;
894 if (!qproc->has_qaccept_regs)
897 while (!takedown_complete && nretry) {
900 /* Wait for active transactions to complete */
901 regmap_read_poll_timeout(map, offset + QACCEPT_ACTIVE_REG, val, !val, 5,
904 /* Request Q-channel transaction takedown */
905 regmap_write(map, offset + QACCEPT_REQ_REG, 0);
908 * If the request is denied, reset the Q-channel takedown request,
909 * wait for active transactions to complete and retry takedown.
915 ret = regmap_read(map, offset + QACCEPT_DENY_REG, &val);
917 regmap_write(map, offset + QACCEPT_REQ_REG, 1);
921 ret = regmap_read(map, offset + QACCEPT_ACCEPT_REG, &val);
923 takedown_complete = true;
932 /* Rely on mss_restart to clear out pending transactions on takedown failure */
933 if (!takedown_complete)
934 dev_err(qproc->dev, "qchannel takedown failed\n");
937 static void q6v5proc_halt_axi_port(struct q6v5 *qproc,
938 struct regmap *halt_map,
944 /* Check if we're already idle */
945 ret = regmap_read(halt_map, offset + AXI_IDLE_REG, &val);
949 /* Assert halt request */
950 regmap_write(halt_map, offset + AXI_HALTREQ_REG, 1);
953 regmap_read_poll_timeout(halt_map, offset + AXI_HALTACK_REG, val,
954 val, 1000, HALT_ACK_TIMEOUT_US);
956 ret = regmap_read(halt_map, offset + AXI_IDLE_REG, &val);
958 dev_err(qproc->dev, "port failed halt\n");
960 /* Clear halt request (port will remain halted until reset) */
961 regmap_write(halt_map, offset + AXI_HALTREQ_REG, 0);
964 static int q6v5_mpss_init_image(struct q6v5 *qproc, const struct firmware *fw,
967 unsigned long dma_attrs = DMA_ATTR_FORCE_CONTIGUOUS;
976 metadata = qcom_mdt_read_metadata(fw, &size, fw_name, qproc->dev);
977 if (IS_ERR(metadata))
978 return PTR_ERR(metadata);
980 if (qproc->mdata_phys) {
981 if (size > qproc->mdata_size) {
983 dev_err(qproc->dev, "metadata size outside memory range\n");
987 phys = qproc->mdata_phys;
988 ptr = memremap(qproc->mdata_phys, size, MEMREMAP_WC);
991 dev_err(qproc->dev, "unable to map memory region: %pa+%zx\n",
992 &qproc->mdata_phys, size);
996 ptr = dma_alloc_attrs(qproc->dev, size, &phys, GFP_KERNEL, dma_attrs);
999 dev_err(qproc->dev, "failed to allocate mdt buffer\n");
1004 memcpy(ptr, metadata, size);
1006 if (qproc->mdata_phys)
1009 /* Hypervisor mapping to access metadata by modem */
1010 mdata_perm = BIT(QCOM_SCM_VMID_HLOS);
1011 ret = q6v5_xfer_mem_ownership(qproc, &mdata_perm, false, true,
1015 "assigning Q6 access to metadata failed: %d\n", ret);
1017 goto free_dma_attrs;
1020 writel(phys, qproc->rmb_base + RMB_PMI_META_DATA_REG);
1021 writel(RMB_CMD_META_DATA_READY, qproc->rmb_base + RMB_MBA_COMMAND_REG);
1023 ret = q6v5_rmb_mba_wait(qproc, RMB_MBA_META_DATA_AUTH_SUCCESS, 1000);
1024 if (ret == -ETIMEDOUT)
1025 dev_err(qproc->dev, "MPSS header authentication timed out\n");
1027 dev_err(qproc->dev, "MPSS header authentication failed: %d\n", ret);
1029 /* Metadata authentication done, remove modem access */
1030 xferop_ret = q6v5_xfer_mem_ownership(qproc, &mdata_perm, true, false,
1033 dev_warn(qproc->dev,
1034 "mdt buffer not reclaimed system may become unstable\n");
1037 if (!qproc->mdata_phys)
1038 dma_free_attrs(qproc->dev, size, ptr, phys, dma_attrs);
1042 return ret < 0 ? ret : 0;
1045 static bool q6v5_phdr_valid(const struct elf32_phdr *phdr)
1047 if (phdr->p_type != PT_LOAD)
1050 if ((phdr->p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH)
1059 static int q6v5_mba_load(struct q6v5 *qproc)
1063 bool mba_load_err = false;
1065 ret = qcom_q6v5_prepare(&qproc->q6v5);
1069 ret = q6v5_pds_enable(qproc, qproc->proxy_pds, qproc->proxy_pd_count);
1071 dev_err(qproc->dev, "failed to enable proxy power domains\n");
1075 ret = q6v5_regulator_enable(qproc, qproc->fallback_proxy_regs,
1076 qproc->fallback_proxy_reg_count);
1078 dev_err(qproc->dev, "failed to enable fallback proxy supplies\n");
1079 goto disable_proxy_pds;
1082 ret = q6v5_regulator_enable(qproc, qproc->proxy_regs,
1083 qproc->proxy_reg_count);
1085 dev_err(qproc->dev, "failed to enable proxy supplies\n");
1086 goto disable_fallback_proxy_reg;
1089 ret = q6v5_clk_enable(qproc->dev, qproc->proxy_clks,
1090 qproc->proxy_clk_count);
1092 dev_err(qproc->dev, "failed to enable proxy clocks\n");
1093 goto disable_proxy_reg;
1096 ret = q6v5_regulator_enable(qproc, qproc->active_regs,
1097 qproc->active_reg_count);
1099 dev_err(qproc->dev, "failed to enable supplies\n");
1100 goto disable_proxy_clk;
1103 ret = q6v5_clk_enable(qproc->dev, qproc->reset_clks,
1104 qproc->reset_clk_count);
1106 dev_err(qproc->dev, "failed to enable reset clocks\n");
1110 ret = q6v5_reset_deassert(qproc);
1112 dev_err(qproc->dev, "failed to deassert mss restart\n");
1113 goto disable_reset_clks;
1116 ret = q6v5_clk_enable(qproc->dev, qproc->active_clks,
1117 qproc->active_clk_count);
1119 dev_err(qproc->dev, "failed to enable clocks\n");
1123 ret = q6v5proc_enable_qchannel(qproc, qproc->halt_map, qproc->qaccept_axi);
1125 dev_err(qproc->dev, "failed to enable axi bridge\n");
1126 goto disable_active_clks;
1130 * Some versions of the MBA firmware will upon boot wipe the MPSS region as well, so provide
1131 * the Q6 access to this region.
1133 ret = q6v5_xfer_mem_ownership(qproc, &qproc->mpss_perm, false, true,
1134 qproc->mpss_phys, qproc->mpss_size);
1136 dev_err(qproc->dev, "assigning Q6 access to mpss memory failed: %d\n", ret);
1137 goto disable_active_clks;
1140 /* Assign MBA image access in DDR to q6 */
1141 ret = q6v5_xfer_mem_ownership(qproc, &qproc->mba_perm, false, true,
1142 qproc->mba_phys, qproc->mba_size);
1145 "assigning Q6 access to mba memory failed: %d\n", ret);
1146 goto disable_active_clks;
1149 writel(qproc->mba_phys, qproc->rmb_base + RMB_MBA_IMAGE_REG);
1150 if (qproc->dp_size) {
1151 writel(qproc->mba_phys + SZ_1M, qproc->rmb_base + RMB_PMI_CODE_START_REG);
1152 writel(qproc->dp_size, qproc->rmb_base + RMB_PMI_CODE_LENGTH_REG);
1155 ret = q6v5proc_reset(qproc);
1159 if (qproc->has_mba_logs)
1160 qcom_pil_info_store("mba", qproc->mba_phys, MBA_LOG_SIZE);
1162 ret = q6v5_rmb_mba_wait(qproc, 0, 5000);
1163 if (ret == -ETIMEDOUT) {
1164 dev_err(qproc->dev, "MBA boot timed out\n");
1165 goto halt_axi_ports;
1166 } else if (ret != RMB_MBA_XPU_UNLOCKED &&
1167 ret != RMB_MBA_XPU_UNLOCKED_SCRIBBLED) {
1168 dev_err(qproc->dev, "MBA returned unexpected status %d\n", ret);
1170 goto halt_axi_ports;
1173 qproc->dump_mba_loaded = true;
1177 q6v5proc_halt_axi_port(qproc, qproc->halt_map, qproc->halt_q6);
1179 q6v5proc_halt_axi_port(qproc, qproc->halt_map, qproc->halt_vq6);
1180 q6v5proc_halt_axi_port(qproc, qproc->halt_map, qproc->halt_modem);
1181 q6v5proc_halt_axi_port(qproc, qproc->halt_map, qproc->halt_nc);
1182 q6v5proc_disable_qchannel(qproc, qproc->halt_map, qproc->qaccept_mdm);
1183 q6v5proc_disable_qchannel(qproc, qproc->halt_map, qproc->qaccept_cx);
1184 q6v5proc_disable_qchannel(qproc, qproc->halt_map, qproc->qaccept_axi);
1185 mba_load_err = true;
1187 xfermemop_ret = q6v5_xfer_mem_ownership(qproc, &qproc->mba_perm, true,
1188 false, qproc->mba_phys,
1190 if (xfermemop_ret) {
1192 "Failed to reclaim mba buffer, system may become unstable\n");
1193 } else if (mba_load_err) {
1194 q6v5_dump_mba_logs(qproc);
1197 disable_active_clks:
1198 q6v5_clk_disable(qproc->dev, qproc->active_clks,
1199 qproc->active_clk_count);
1201 q6v5_reset_assert(qproc);
1203 q6v5_clk_disable(qproc->dev, qproc->reset_clks,
1204 qproc->reset_clk_count);
1206 q6v5_regulator_disable(qproc, qproc->active_regs,
1207 qproc->active_reg_count);
1209 q6v5_clk_disable(qproc->dev, qproc->proxy_clks,
1210 qproc->proxy_clk_count);
1212 q6v5_regulator_disable(qproc, qproc->proxy_regs,
1213 qproc->proxy_reg_count);
1214 disable_fallback_proxy_reg:
1215 q6v5_regulator_disable(qproc, qproc->fallback_proxy_regs,
1216 qproc->fallback_proxy_reg_count);
1218 q6v5_pds_disable(qproc, qproc->proxy_pds, qproc->proxy_pd_count);
1220 qcom_q6v5_unprepare(&qproc->q6v5);
1225 static void q6v5_mba_reclaim(struct q6v5 *qproc)
1230 qproc->dump_mba_loaded = false;
1233 q6v5proc_halt_axi_port(qproc, qproc->halt_map, qproc->halt_q6);
1235 q6v5proc_halt_axi_port(qproc, qproc->halt_map, qproc->halt_vq6);
1236 q6v5proc_halt_axi_port(qproc, qproc->halt_map, qproc->halt_modem);
1237 q6v5proc_halt_axi_port(qproc, qproc->halt_map, qproc->halt_nc);
1238 if (qproc->version == MSS_MSM8996) {
1240 * To avoid high MX current during LPASS/MSS restart.
1242 val = readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
1243 val |= Q6SS_CLAMP_IO | QDSP6v56_CLAMP_WL |
1244 QDSP6v56_CLAMP_QMC_MEM;
1245 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
1248 if (qproc->has_ext_cntl_regs) {
1249 regmap_write(qproc->conn_map, qproc->rscc_disable, 1);
1251 ret = regmap_read_poll_timeout(qproc->halt_map, qproc->axim1_clk_off, val,
1252 !val, 1, Q6SS_CBCR_TIMEOUT_US);
1254 dev_err(qproc->dev, "failed to enable axim1 clock\n");
1256 ret = regmap_read_poll_timeout(qproc->halt_map, qproc->crypto_clk_off, val,
1257 !val, 1, Q6SS_CBCR_TIMEOUT_US);
1259 dev_err(qproc->dev, "failed to enable crypto clock\n");
1262 q6v5proc_disable_qchannel(qproc, qproc->halt_map, qproc->qaccept_mdm);
1263 q6v5proc_disable_qchannel(qproc, qproc->halt_map, qproc->qaccept_cx);
1264 q6v5proc_disable_qchannel(qproc, qproc->halt_map, qproc->qaccept_axi);
1266 q6v5_reset_assert(qproc);
1268 q6v5_clk_disable(qproc->dev, qproc->reset_clks,
1269 qproc->reset_clk_count);
1270 q6v5_clk_disable(qproc->dev, qproc->active_clks,
1271 qproc->active_clk_count);
1272 q6v5_regulator_disable(qproc, qproc->active_regs,
1273 qproc->active_reg_count);
1275 /* In case of failure or coredump scenario where reclaiming MBA memory
1276 * could not happen reclaim it here.
1278 ret = q6v5_xfer_mem_ownership(qproc, &qproc->mba_perm, true, false,
1283 ret = qcom_q6v5_unprepare(&qproc->q6v5);
1285 q6v5_pds_disable(qproc, qproc->proxy_pds,
1286 qproc->proxy_pd_count);
1287 q6v5_clk_disable(qproc->dev, qproc->proxy_clks,
1288 qproc->proxy_clk_count);
1289 q6v5_regulator_disable(qproc, qproc->fallback_proxy_regs,
1290 qproc->fallback_proxy_reg_count);
1291 q6v5_regulator_disable(qproc, qproc->proxy_regs,
1292 qproc->proxy_reg_count);
1296 static int q6v5_reload_mba(struct rproc *rproc)
1298 struct q6v5 *qproc = rproc->priv;
1299 const struct firmware *fw;
1302 ret = request_firmware(&fw, rproc->firmware, qproc->dev);
1306 q6v5_load(rproc, fw);
1307 ret = q6v5_mba_load(qproc);
1308 release_firmware(fw);
1313 static int q6v5_mpss_load(struct q6v5 *qproc)
1315 const struct elf32_phdr *phdrs;
1316 const struct elf32_phdr *phdr;
1317 const struct firmware *seg_fw;
1318 const struct firmware *fw;
1319 struct elf32_hdr *ehdr;
1320 phys_addr_t mpss_reloc;
1321 phys_addr_t boot_addr;
1322 phys_addr_t min_addr = PHYS_ADDR_MAX;
1323 phys_addr_t max_addr = 0;
1325 bool relocate = false;
1334 fw_name_len = strlen(qproc->hexagon_mdt_image);
1335 if (fw_name_len <= 4)
1338 fw_name = kstrdup(qproc->hexagon_mdt_image, GFP_KERNEL);
1342 ret = request_firmware(&fw, fw_name, qproc->dev);
1344 dev_err(qproc->dev, "unable to load %s\n", fw_name);
1348 /* Initialize the RMB validator */
1349 writel(0, qproc->rmb_base + RMB_PMI_CODE_LENGTH_REG);
1351 ret = q6v5_mpss_init_image(qproc, fw, qproc->hexagon_mdt_image);
1353 goto release_firmware;
1355 ehdr = (struct elf32_hdr *)fw->data;
1356 phdrs = (struct elf32_phdr *)(ehdr + 1);
1358 for (i = 0; i < ehdr->e_phnum; i++) {
1361 if (!q6v5_phdr_valid(phdr))
1364 if (phdr->p_flags & QCOM_MDT_RELOCATABLE)
1367 if (phdr->p_paddr < min_addr)
1368 min_addr = phdr->p_paddr;
1370 if (phdr->p_paddr + phdr->p_memsz > max_addr)
1371 max_addr = ALIGN(phdr->p_paddr + phdr->p_memsz, SZ_4K);
1374 if (qproc->version == MSS_MSM8953) {
1375 ret = qcom_scm_pas_mem_setup(MPSS_PAS_ID, qproc->mpss_phys, qproc->mpss_size);
1378 "setting up mpss memory failed: %d\n", ret);
1379 goto release_firmware;
1384 * In case of a modem subsystem restart on secure devices, the modem
1385 * memory can be reclaimed only after MBA is loaded.
1387 q6v5_xfer_mem_ownership(qproc, &qproc->mpss_perm, true, false,
1388 qproc->mpss_phys, qproc->mpss_size);
1390 /* Share ownership between Linux and MSS, during segment loading */
1391 ret = q6v5_xfer_mem_ownership(qproc, &qproc->mpss_perm, true, true,
1392 qproc->mpss_phys, qproc->mpss_size);
1395 "assigning Q6 access to mpss memory failed: %d\n", ret);
1397 goto release_firmware;
1400 mpss_reloc = relocate ? min_addr : qproc->mpss_phys;
1401 qproc->mpss_reloc = mpss_reloc;
1402 /* Load firmware segments */
1403 for (i = 0; i < ehdr->e_phnum; i++) {
1406 if (!q6v5_phdr_valid(phdr))
1409 offset = phdr->p_paddr - mpss_reloc;
1410 if (offset < 0 || offset + phdr->p_memsz > qproc->mpss_size) {
1411 dev_err(qproc->dev, "segment outside memory range\n");
1413 goto release_firmware;
1416 if (phdr->p_filesz > phdr->p_memsz) {
1418 "refusing to load segment %d with p_filesz > p_memsz\n",
1421 goto release_firmware;
1424 ptr = memremap(qproc->mpss_phys + offset, phdr->p_memsz, MEMREMAP_WC);
1427 "unable to map memory region: %pa+%zx-%x\n",
1428 &qproc->mpss_phys, offset, phdr->p_memsz);
1429 goto release_firmware;
1432 if (phdr->p_filesz && phdr->p_offset < fw->size) {
1433 /* Firmware is large enough to be non-split */
1434 if (phdr->p_offset + phdr->p_filesz > fw->size) {
1436 "failed to load segment %d from truncated file %s\n",
1440 goto release_firmware;
1443 memcpy(ptr, fw->data + phdr->p_offset, phdr->p_filesz);
1444 } else if (phdr->p_filesz) {
1445 /* Replace "xxx.xxx" with "xxx.bxx" */
1446 sprintf(fw_name + fw_name_len - 3, "b%02d", i);
1447 ret = request_firmware_into_buf(&seg_fw, fw_name, qproc->dev,
1448 ptr, phdr->p_filesz);
1450 dev_err(qproc->dev, "failed to load %s\n", fw_name);
1452 goto release_firmware;
1455 if (seg_fw->size != phdr->p_filesz) {
1457 "failed to load segment %d from truncated file %s\n",
1460 release_firmware(seg_fw);
1462 goto release_firmware;
1465 release_firmware(seg_fw);
1468 if (phdr->p_memsz > phdr->p_filesz) {
1469 memset(ptr + phdr->p_filesz, 0,
1470 phdr->p_memsz - phdr->p_filesz);
1473 size += phdr->p_memsz;
1475 code_length = readl(qproc->rmb_base + RMB_PMI_CODE_LENGTH_REG);
1477 boot_addr = relocate ? qproc->mpss_phys : min_addr;
1478 writel(boot_addr, qproc->rmb_base + RMB_PMI_CODE_START_REG);
1479 writel(RMB_CMD_LOAD_READY, qproc->rmb_base + RMB_MBA_COMMAND_REG);
1481 writel(size, qproc->rmb_base + RMB_PMI_CODE_LENGTH_REG);
1483 ret = readl(qproc->rmb_base + RMB_MBA_STATUS_REG);
1485 dev_err(qproc->dev, "MPSS authentication failed: %d\n",
1487 goto release_firmware;
1491 /* Transfer ownership of modem ddr region to q6 */
1492 ret = q6v5_xfer_mem_ownership(qproc, &qproc->mpss_perm, false, true,
1493 qproc->mpss_phys, qproc->mpss_size);
1496 "assigning Q6 access to mpss memory failed: %d\n", ret);
1498 goto release_firmware;
1501 ret = q6v5_rmb_mba_wait(qproc, RMB_MBA_AUTH_COMPLETE, 10000);
1502 if (ret == -ETIMEDOUT)
1503 dev_err(qproc->dev, "MPSS authentication timed out\n");
1505 dev_err(qproc->dev, "MPSS authentication failed: %d\n", ret);
1507 qcom_pil_info_store("modem", qproc->mpss_phys, qproc->mpss_size);
1510 release_firmware(fw);
1514 return ret < 0 ? ret : 0;
1517 static void qcom_q6v5_dump_segment(struct rproc *rproc,
1518 struct rproc_dump_segment *segment,
1519 void *dest, size_t cp_offset, size_t size)
1522 struct q6v5 *qproc = rproc->priv;
1523 int offset = segment->da - qproc->mpss_reloc;
1526 /* Unlock mba before copying segments */
1527 if (!qproc->dump_mba_loaded) {
1528 ret = q6v5_reload_mba(rproc);
1530 /* Reset ownership back to Linux to copy segments */
1531 ret = q6v5_xfer_mem_ownership(qproc, &qproc->mpss_perm,
1539 ptr = memremap(qproc->mpss_phys + offset + cp_offset, size, MEMREMAP_WC);
1542 memcpy(dest, ptr, size);
1545 memset(dest, 0xff, size);
1548 qproc->current_dump_size += size;
1550 /* Reclaim mba after copying segments */
1551 if (qproc->current_dump_size == qproc->total_dump_size) {
1552 if (qproc->dump_mba_loaded) {
1553 /* Try to reset ownership back to Q6 */
1554 q6v5_xfer_mem_ownership(qproc, &qproc->mpss_perm,
1558 q6v5_mba_reclaim(qproc);
1563 static int q6v5_start(struct rproc *rproc)
1565 struct q6v5 *qproc = rproc->priv;
1569 ret = q6v5_mba_load(qproc);
1573 dev_info(qproc->dev, "MBA booted with%s debug policy, loading mpss\n",
1574 qproc->dp_size ? "" : "out");
1576 ret = q6v5_mpss_load(qproc);
1580 ret = qcom_q6v5_wait_for_start(&qproc->q6v5, msecs_to_jiffies(5000));
1581 if (ret == -ETIMEDOUT) {
1582 dev_err(qproc->dev, "start timed out\n");
1586 xfermemop_ret = q6v5_xfer_mem_ownership(qproc, &qproc->mba_perm, true,
1587 false, qproc->mba_phys,
1591 "Failed to reclaim mba buffer system may become unstable\n");
1593 /* Reset Dump Segment Mask */
1594 qproc->current_dump_size = 0;
1599 q6v5_mba_reclaim(qproc);
1600 q6v5_dump_mba_logs(qproc);
1605 static int q6v5_stop(struct rproc *rproc)
1607 struct q6v5 *qproc = rproc->priv;
1610 ret = qcom_q6v5_request_stop(&qproc->q6v5, qproc->sysmon);
1611 if (ret == -ETIMEDOUT)
1612 dev_err(qproc->dev, "timed out on wait\n");
1614 q6v5_mba_reclaim(qproc);
1619 static int qcom_q6v5_register_dump_segments(struct rproc *rproc,
1620 const struct firmware *mba_fw)
1622 const struct firmware *fw;
1623 const struct elf32_phdr *phdrs;
1624 const struct elf32_phdr *phdr;
1625 const struct elf32_hdr *ehdr;
1626 struct q6v5 *qproc = rproc->priv;
1630 ret = request_firmware(&fw, qproc->hexagon_mdt_image, qproc->dev);
1632 dev_err(qproc->dev, "unable to load %s\n",
1633 qproc->hexagon_mdt_image);
1637 rproc_coredump_set_elf_info(rproc, ELFCLASS32, EM_NONE);
1639 ehdr = (struct elf32_hdr *)fw->data;
1640 phdrs = (struct elf32_phdr *)(ehdr + 1);
1641 qproc->total_dump_size = 0;
1643 for (i = 0; i < ehdr->e_phnum; i++) {
1646 if (!q6v5_phdr_valid(phdr))
1649 ret = rproc_coredump_add_custom_segment(rproc, phdr->p_paddr,
1651 qcom_q6v5_dump_segment,
1656 qproc->total_dump_size += phdr->p_memsz;
1659 release_firmware(fw);
1663 static unsigned long q6v5_panic(struct rproc *rproc)
1665 struct q6v5 *qproc = rproc->priv;
1667 return qcom_q6v5_panic(&qproc->q6v5);
1670 static const struct rproc_ops q6v5_ops = {
1671 .start = q6v5_start,
1673 .parse_fw = qcom_q6v5_register_dump_segments,
1675 .panic = q6v5_panic,
1678 static void qcom_msa_handover(struct qcom_q6v5 *q6v5)
1680 struct q6v5 *qproc = container_of(q6v5, struct q6v5, q6v5);
1682 q6v5_clk_disable(qproc->dev, qproc->proxy_clks,
1683 qproc->proxy_clk_count);
1684 q6v5_regulator_disable(qproc, qproc->proxy_regs,
1685 qproc->proxy_reg_count);
1686 q6v5_regulator_disable(qproc, qproc->fallback_proxy_regs,
1687 qproc->fallback_proxy_reg_count);
1688 q6v5_pds_disable(qproc, qproc->proxy_pds, qproc->proxy_pd_count);
1691 static int q6v5_init_mem(struct q6v5 *qproc, struct platform_device *pdev)
1693 struct of_phandle_args args;
1694 int halt_cell_cnt = 3;
1697 qproc->reg_base = devm_platform_ioremap_resource_byname(pdev, "qdsp6");
1698 if (IS_ERR(qproc->reg_base))
1699 return PTR_ERR(qproc->reg_base);
1701 qproc->rmb_base = devm_platform_ioremap_resource_byname(pdev, "rmb");
1702 if (IS_ERR(qproc->rmb_base))
1703 return PTR_ERR(qproc->rmb_base);
1708 ret = of_parse_phandle_with_fixed_args(pdev->dev.of_node,
1709 "qcom,halt-regs", halt_cell_cnt, 0, &args);
1711 dev_err(&pdev->dev, "failed to parse qcom,halt-regs\n");
1715 qproc->halt_map = syscon_node_to_regmap(args.np);
1716 of_node_put(args.np);
1717 if (IS_ERR(qproc->halt_map))
1718 return PTR_ERR(qproc->halt_map);
1720 qproc->halt_q6 = args.args[0];
1721 qproc->halt_modem = args.args[1];
1722 qproc->halt_nc = args.args[2];
1725 qproc->halt_vq6 = args.args[3];
1727 if (qproc->has_qaccept_regs) {
1728 ret = of_parse_phandle_with_fixed_args(pdev->dev.of_node,
1729 "qcom,qaccept-regs",
1732 dev_err(&pdev->dev, "failed to parse qaccept-regs\n");
1736 qproc->qaccept_mdm = args.args[0];
1737 qproc->qaccept_cx = args.args[1];
1738 qproc->qaccept_axi = args.args[2];
1741 if (qproc->has_ext_cntl_regs) {
1742 ret = of_parse_phandle_with_fixed_args(pdev->dev.of_node,
1746 dev_err(&pdev->dev, "failed to parse ext-regs index 0\n");
1750 qproc->conn_map = syscon_node_to_regmap(args.np);
1751 of_node_put(args.np);
1752 if (IS_ERR(qproc->conn_map))
1753 return PTR_ERR(qproc->conn_map);
1755 qproc->force_clk_on = args.args[0];
1756 qproc->rscc_disable = args.args[1];
1758 ret = of_parse_phandle_with_fixed_args(pdev->dev.of_node,
1762 dev_err(&pdev->dev, "failed to parse ext-regs index 1\n");
1766 qproc->axim1_clk_off = args.args[0];
1767 qproc->crypto_clk_off = args.args[1];
1770 if (qproc->has_spare_reg) {
1771 ret = of_parse_phandle_with_fixed_args(pdev->dev.of_node,
1775 dev_err(&pdev->dev, "failed to parse spare-regs\n");
1779 qproc->conn_map = syscon_node_to_regmap(args.np);
1780 of_node_put(args.np);
1781 if (IS_ERR(qproc->conn_map))
1782 return PTR_ERR(qproc->conn_map);
1784 qproc->conn_box = args.args[0];
1790 static int q6v5_init_clocks(struct device *dev, struct clk **clks,
1798 for (i = 0; clk_names[i]; i++) {
1799 clks[i] = devm_clk_get(dev, clk_names[i]);
1800 if (IS_ERR(clks[i])) {
1801 int rc = PTR_ERR(clks[i]);
1803 if (rc != -EPROBE_DEFER)
1804 dev_err(dev, "Failed to get %s clock\n",
1813 static int q6v5_pds_attach(struct device *dev, struct device **devs,
1823 while (pd_names[num_pds])
1826 for (i = 0; i < num_pds; i++) {
1827 devs[i] = dev_pm_domain_attach_by_name(dev, pd_names[i]);
1828 if (IS_ERR_OR_NULL(devs[i])) {
1829 ret = PTR_ERR(devs[i]) ? : -ENODATA;
1837 for (i--; i >= 0; i--)
1838 dev_pm_domain_detach(devs[i], false);
1843 static void q6v5_pds_detach(struct q6v5 *qproc, struct device **pds,
1848 for (i = 0; i < pd_count; i++)
1849 dev_pm_domain_detach(pds[i], false);
1852 static int q6v5_init_reset(struct q6v5 *qproc)
1854 qproc->mss_restart = devm_reset_control_get_exclusive(qproc->dev,
1856 if (IS_ERR(qproc->mss_restart)) {
1857 dev_err(qproc->dev, "failed to acquire mss restart\n");
1858 return PTR_ERR(qproc->mss_restart);
1861 if (qproc->has_alt_reset || qproc->has_spare_reg || qproc->has_ext_cntl_regs) {
1862 qproc->pdc_reset = devm_reset_control_get_exclusive(qproc->dev,
1864 if (IS_ERR(qproc->pdc_reset)) {
1865 dev_err(qproc->dev, "failed to acquire pdc reset\n");
1866 return PTR_ERR(qproc->pdc_reset);
1873 static int q6v5_alloc_memory_region(struct q6v5 *qproc)
1875 struct device_node *child;
1876 struct reserved_mem *rmem;
1877 struct device_node *node;
1882 * In the absence of mba/mpss sub-child, extract the mba and mpss
1883 * reserved memory regions from device's memory-region property.
1885 child = of_get_child_by_name(qproc->dev->of_node, "mba");
1887 node = of_parse_phandle(qproc->dev->of_node,
1888 "memory-region", 0);
1890 node = of_parse_phandle(child, "memory-region", 0);
1894 ret = of_address_to_resource(node, 0, &r);
1897 dev_err(qproc->dev, "unable to resolve mba region\n");
1901 qproc->mba_phys = r.start;
1902 qproc->mba_size = resource_size(&r);
1905 node = of_parse_phandle(qproc->dev->of_node,
1906 "memory-region", 1);
1908 child = of_get_child_by_name(qproc->dev->of_node, "mpss");
1909 node = of_parse_phandle(child, "memory-region", 0);
1913 ret = of_address_to_resource(node, 0, &r);
1916 dev_err(qproc->dev, "unable to resolve mpss region\n");
1920 qproc->mpss_phys = qproc->mpss_reloc = r.start;
1921 qproc->mpss_size = resource_size(&r);
1924 node = of_parse_phandle(qproc->dev->of_node, "memory-region", 2);
1926 child = of_get_child_by_name(qproc->dev->of_node, "metadata");
1927 node = of_parse_phandle(child, "memory-region", 0);
1934 rmem = of_reserved_mem_lookup(node);
1936 dev_err(qproc->dev, "unable to resolve metadata region\n");
1940 qproc->mdata_phys = rmem->base;
1941 qproc->mdata_size = rmem->size;
1946 static int q6v5_probe(struct platform_device *pdev)
1948 const struct rproc_hexagon_res *desc;
1949 struct device_node *node;
1951 struct rproc *rproc;
1952 const char *mba_image;
1955 desc = of_device_get_match_data(&pdev->dev);
1959 if (desc->need_mem_protection && !qcom_scm_is_available())
1960 return -EPROBE_DEFER;
1962 mba_image = desc->hexagon_mba_image;
1963 ret = of_property_read_string_index(pdev->dev.of_node, "firmware-name",
1965 if (ret < 0 && ret != -EINVAL) {
1966 dev_err(&pdev->dev, "unable to read mba firmware-name\n");
1970 rproc = rproc_alloc(&pdev->dev, pdev->name, &q6v5_ops,
1971 mba_image, sizeof(*qproc));
1973 dev_err(&pdev->dev, "failed to allocate rproc\n");
1977 rproc->auto_boot = false;
1978 rproc_coredump_set_elf_info(rproc, ELFCLASS32, EM_NONE);
1980 qproc = rproc->priv;
1981 qproc->dev = &pdev->dev;
1982 qproc->rproc = rproc;
1983 qproc->hexagon_mdt_image = "modem.mdt";
1984 ret = of_property_read_string_index(pdev->dev.of_node, "firmware-name",
1985 1, &qproc->hexagon_mdt_image);
1986 if (ret < 0 && ret != -EINVAL) {
1987 dev_err(&pdev->dev, "unable to read mpss firmware-name\n");
1991 platform_set_drvdata(pdev, qproc);
1993 qproc->has_qaccept_regs = desc->has_qaccept_regs;
1994 qproc->has_ext_cntl_regs = desc->has_ext_cntl_regs;
1995 qproc->has_vq6 = desc->has_vq6;
1996 qproc->has_spare_reg = desc->has_spare_reg;
1997 ret = q6v5_init_mem(qproc, pdev);
2001 ret = q6v5_alloc_memory_region(qproc);
2005 ret = q6v5_init_clocks(&pdev->dev, qproc->proxy_clks,
2006 desc->proxy_clk_names);
2008 dev_err(&pdev->dev, "Failed to get proxy clocks.\n");
2011 qproc->proxy_clk_count = ret;
2013 ret = q6v5_init_clocks(&pdev->dev, qproc->reset_clks,
2014 desc->reset_clk_names);
2016 dev_err(&pdev->dev, "Failed to get reset clocks.\n");
2019 qproc->reset_clk_count = ret;
2021 ret = q6v5_init_clocks(&pdev->dev, qproc->active_clks,
2022 desc->active_clk_names);
2024 dev_err(&pdev->dev, "Failed to get active clocks.\n");
2027 qproc->active_clk_count = ret;
2029 ret = q6v5_regulator_init(&pdev->dev, qproc->proxy_regs,
2030 desc->proxy_supply);
2032 dev_err(&pdev->dev, "Failed to get proxy regulators.\n");
2035 qproc->proxy_reg_count = ret;
2037 ret = q6v5_regulator_init(&pdev->dev, qproc->active_regs,
2038 desc->active_supply);
2040 dev_err(&pdev->dev, "Failed to get active regulators.\n");
2043 qproc->active_reg_count = ret;
2045 ret = q6v5_pds_attach(&pdev->dev, qproc->proxy_pds,
2046 desc->proxy_pd_names);
2047 /* Fallback to regulators for old device trees */
2048 if (ret == -ENODATA && desc->fallback_proxy_supply) {
2049 ret = q6v5_regulator_init(&pdev->dev,
2050 qproc->fallback_proxy_regs,
2051 desc->fallback_proxy_supply);
2053 dev_err(&pdev->dev, "Failed to get fallback proxy regulators.\n");
2056 qproc->fallback_proxy_reg_count = ret;
2057 } else if (ret < 0) {
2058 dev_err(&pdev->dev, "Failed to init power domains\n");
2061 qproc->proxy_pd_count = ret;
2064 qproc->has_alt_reset = desc->has_alt_reset;
2065 ret = q6v5_init_reset(qproc);
2067 goto detach_proxy_pds;
2069 qproc->version = desc->version;
2070 qproc->need_mem_protection = desc->need_mem_protection;
2071 qproc->has_mba_logs = desc->has_mba_logs;
2073 ret = qcom_q6v5_init(&qproc->q6v5, pdev, rproc, MPSS_CRASH_REASON_SMEM, "modem",
2076 goto detach_proxy_pds;
2078 qproc->mpss_perm = BIT(QCOM_SCM_VMID_HLOS);
2079 qproc->mba_perm = BIT(QCOM_SCM_VMID_HLOS);
2080 qcom_add_glink_subdev(rproc, &qproc->glink_subdev, "mpss");
2081 qcom_add_smd_subdev(rproc, &qproc->smd_subdev);
2082 qcom_add_ssr_subdev(rproc, &qproc->ssr_subdev, "mpss");
2083 qproc->sysmon = qcom_add_sysmon_subdev(rproc, "modem", 0x12);
2084 if (IS_ERR(qproc->sysmon)) {
2085 ret = PTR_ERR(qproc->sysmon);
2086 goto remove_subdevs;
2089 ret = rproc_add(rproc);
2091 goto remove_sysmon_subdev;
2093 node = of_get_compatible_child(pdev->dev.of_node, "qcom,bam-dmux");
2094 qproc->bam_dmux = of_platform_device_create(node, NULL, &pdev->dev);
2099 remove_sysmon_subdev:
2100 qcom_remove_sysmon_subdev(qproc->sysmon);
2102 qcom_remove_ssr_subdev(rproc, &qproc->ssr_subdev);
2103 qcom_remove_smd_subdev(rproc, &qproc->smd_subdev);
2104 qcom_remove_glink_subdev(rproc, &qproc->glink_subdev);
2106 q6v5_pds_detach(qproc, qproc->proxy_pds, qproc->proxy_pd_count);
2113 static void q6v5_remove(struct platform_device *pdev)
2115 struct q6v5 *qproc = platform_get_drvdata(pdev);
2116 struct rproc *rproc = qproc->rproc;
2118 if (qproc->bam_dmux)
2119 of_platform_device_destroy(&qproc->bam_dmux->dev, NULL);
2122 qcom_q6v5_deinit(&qproc->q6v5);
2123 qcom_remove_sysmon_subdev(qproc->sysmon);
2124 qcom_remove_ssr_subdev(rproc, &qproc->ssr_subdev);
2125 qcom_remove_smd_subdev(rproc, &qproc->smd_subdev);
2126 qcom_remove_glink_subdev(rproc, &qproc->glink_subdev);
2128 q6v5_pds_detach(qproc, qproc->proxy_pds, qproc->proxy_pd_count);
2133 static const struct rproc_hexagon_res sc7180_mss = {
2134 .hexagon_mba_image = "mba.mbn",
2135 .proxy_clk_names = (char*[]){
2139 .reset_clk_names = (char*[]){
2145 .active_clk_names = (char*[]){
2150 .proxy_pd_names = (char*[]){
2156 .need_mem_protection = true,
2157 .has_alt_reset = false,
2158 .has_mba_logs = true,
2159 .has_spare_reg = true,
2160 .has_qaccept_regs = false,
2161 .has_ext_cntl_regs = false,
2163 .version = MSS_SC7180,
2166 static const struct rproc_hexagon_res sc7280_mss = {
2167 .hexagon_mba_image = "mba.mbn",
2168 .proxy_clk_names = (char*[]){
2173 .active_clk_names = (char*[]){
2179 .proxy_pd_names = (char*[]){
2184 .need_mem_protection = true,
2185 .has_alt_reset = false,
2186 .has_mba_logs = true,
2187 .has_spare_reg = false,
2188 .has_qaccept_regs = true,
2189 .has_ext_cntl_regs = true,
2191 .version = MSS_SC7280,
2194 static const struct rproc_hexagon_res sdm845_mss = {
2195 .hexagon_mba_image = "mba.mbn",
2196 .proxy_clk_names = (char*[]){
2201 .reset_clk_names = (char*[]){
2206 .active_clk_names = (char*[]){
2213 .proxy_pd_names = (char*[]){
2219 .need_mem_protection = true,
2220 .has_alt_reset = true,
2221 .has_mba_logs = false,
2222 .has_spare_reg = false,
2223 .has_qaccept_regs = false,
2224 .has_ext_cntl_regs = false,
2226 .version = MSS_SDM845,
2229 static const struct rproc_hexagon_res msm8998_mss = {
2230 .hexagon_mba_image = "mba.mbn",
2231 .proxy_clk_names = (char*[]){
2237 .active_clk_names = (char*[]){
2245 .proxy_pd_names = (char*[]){
2250 .need_mem_protection = true,
2251 .has_alt_reset = false,
2252 .has_mba_logs = false,
2253 .has_spare_reg = false,
2254 .has_qaccept_regs = false,
2255 .has_ext_cntl_regs = false,
2257 .version = MSS_MSM8998,
2260 static const struct rproc_hexagon_res msm8996_mss = {
2261 .hexagon_mba_image = "mba.mbn",
2262 .proxy_supply = (struct qcom_mss_reg_res[]) {
2269 .proxy_clk_names = (char*[]){
2275 .active_clk_names = (char*[]){
2284 .proxy_pd_names = (char*[]){
2289 .need_mem_protection = true,
2290 .has_alt_reset = false,
2291 .has_mba_logs = false,
2292 .has_spare_reg = false,
2293 .has_qaccept_regs = false,
2294 .has_ext_cntl_regs = false,
2296 .version = MSS_MSM8996,
2299 static const struct rproc_hexagon_res msm8909_mss = {
2300 .hexagon_mba_image = "mba.mbn",
2301 .proxy_supply = (struct qcom_mss_reg_res[]) {
2308 .proxy_clk_names = (char*[]){
2312 .active_clk_names = (char*[]){
2318 .proxy_pd_names = (char*[]){
2323 .need_mem_protection = false,
2324 .has_alt_reset = false,
2325 .has_mba_logs = false,
2326 .has_spare_reg = false,
2327 .has_qaccept_regs = false,
2328 .has_ext_cntl_regs = false,
2330 .version = MSS_MSM8909,
2333 static const struct rproc_hexagon_res msm8916_mss = {
2334 .hexagon_mba_image = "mba.mbn",
2335 .proxy_supply = (struct qcom_mss_reg_res[]) {
2342 .fallback_proxy_supply = (struct qcom_mss_reg_res[]) {
2353 .proxy_clk_names = (char*[]){
2357 .active_clk_names = (char*[]){
2363 .proxy_pd_names = (char*[]){
2368 .need_mem_protection = false,
2369 .has_alt_reset = false,
2370 .has_mba_logs = false,
2371 .has_spare_reg = false,
2372 .has_qaccept_regs = false,
2373 .has_ext_cntl_regs = false,
2375 .version = MSS_MSM8916,
2378 static const struct rproc_hexagon_res msm8953_mss = {
2379 .hexagon_mba_image = "mba.mbn",
2380 .proxy_supply = (struct qcom_mss_reg_res[]) {
2387 .proxy_clk_names = (char*[]){
2391 .active_clk_names = (char*[]){
2397 .proxy_pd_names = (char*[]) {
2403 .need_mem_protection = false,
2404 .has_alt_reset = false,
2405 .has_mba_logs = false,
2406 .has_spare_reg = false,
2407 .has_qaccept_regs = false,
2408 .has_ext_cntl_regs = false,
2410 .version = MSS_MSM8953,
2413 static const struct rproc_hexagon_res msm8974_mss = {
2414 .hexagon_mba_image = "mba.b00",
2415 .proxy_supply = (struct qcom_mss_reg_res[]) {
2422 .fallback_proxy_supply = (struct qcom_mss_reg_res[]) {
2433 .active_supply = (struct qcom_mss_reg_res[]) {
2441 .proxy_clk_names = (char*[]){
2445 .active_clk_names = (char*[]){
2451 .proxy_pd_names = (char*[]){
2456 .need_mem_protection = false,
2457 .has_alt_reset = false,
2458 .has_mba_logs = false,
2459 .has_spare_reg = false,
2460 .has_qaccept_regs = false,
2461 .has_ext_cntl_regs = false,
2463 .version = MSS_MSM8974,
2466 static const struct of_device_id q6v5_of_match[] = {
2467 { .compatible = "qcom,q6v5-pil", .data = &msm8916_mss},
2468 { .compatible = "qcom,msm8909-mss-pil", .data = &msm8909_mss},
2469 { .compatible = "qcom,msm8916-mss-pil", .data = &msm8916_mss},
2470 { .compatible = "qcom,msm8953-mss-pil", .data = &msm8953_mss},
2471 { .compatible = "qcom,msm8974-mss-pil", .data = &msm8974_mss},
2472 { .compatible = "qcom,msm8996-mss-pil", .data = &msm8996_mss},
2473 { .compatible = "qcom,msm8998-mss-pil", .data = &msm8998_mss},
2474 { .compatible = "qcom,sc7180-mss-pil", .data = &sc7180_mss},
2475 { .compatible = "qcom,sc7280-mss-pil", .data = &sc7280_mss},
2476 { .compatible = "qcom,sdm845-mss-pil", .data = &sdm845_mss},
2479 MODULE_DEVICE_TABLE(of, q6v5_of_match);
2481 static struct platform_driver q6v5_driver = {
2482 .probe = q6v5_probe,
2483 .remove_new = q6v5_remove,
2485 .name = "qcom-q6v5-mss",
2486 .of_match_table = q6v5_of_match,
2489 module_platform_driver(q6v5_driver);
2491 MODULE_DESCRIPTION("Qualcomm Self-authenticating modem remoteproc driver");
2492 MODULE_LICENSE("GPL v2");