blob: ebc3e755bcbcd488229024a2614e6dbfaf6af675 [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
Bjorn Andersson051fb702016-06-20 14:28:41 -07002/*
Bjorn Anderssonef73c222018-09-24 16:45:26 -07003 * Qualcomm self-authenticating modem subsystem remoteproc driver
Bjorn Andersson051fb702016-06-20 14:28:41 -07004 *
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.
Bjorn Andersson051fb702016-06-20 14:28:41 -07008 */
9
10#include <linux/clk.h>
11#include <linux/delay.h>
Sibi Sankar318130c2020-07-21 16:59:35 +053012#include <linux/devcoredump.h>
Bjorn Andersson051fb702016-06-20 14:28:41 -070013#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>
Avaneesh Kumar Dwivedi7a8ffe12016-12-30 19:24:00 +053019#include <linux/of_device.h>
Bjorn Andersson051fb702016-06-20 14:28:41 -070020#include <linux/platform_device.h>
Rajendra Nayak4760a892019-01-30 16:39:30 -080021#include <linux/pm_domain.h>
22#include <linux/pm_runtime.h>
Bjorn Andersson051fb702016-06-20 14:28:41 -070023#include <linux/regmap.h>
24#include <linux/regulator/consumer.h>
25#include <linux/remoteproc.h>
26#include <linux/reset.h>
Bjorn Andersson2aad40d2017-01-27 03:12:57 -080027#include <linux/soc/qcom/mdt_loader.h>
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +053028#include <linux/iopoll.h>
Herbert Xu79990962020-06-12 16:57:37 +100029#include <linux/slab.h>
Bjorn Andersson051fb702016-06-20 14:28:41 -070030
31#include "remoteproc_internal.h"
Bjorn Anderssonbde440e2017-01-27 02:28:32 -080032#include "qcom_common.h"
Bjorn Anderssond4c78d22020-06-22 12:19:40 -070033#include "qcom_pil_info.h"
Bjorn Andersson7d674732018-06-04 13:30:38 -070034#include "qcom_q6v5.h"
Bjorn Andersson051fb702016-06-20 14:28:41 -070035
36#include <linux/qcom_scm.h>
37
Bjorn Andersson051fb702016-06-20 14:28:41 -070038#define MPSS_CRASH_REASON_SMEM 421
39
Sibi Sankar318130c2020-07-21 16:59:35 +053040#define MBA_LOG_SIZE SZ_4K
41
Bjorn Andersson051fb702016-06-20 14:28:41 -070042/* RMB Status Register Values */
43#define RMB_PBL_SUCCESS 0x1
44
45#define RMB_MBA_XPU_UNLOCKED 0x1
46#define RMB_MBA_XPU_UNLOCKED_SCRIBBLED 0x2
47#define RMB_MBA_META_DATA_AUTH_SUCCESS 0x3
48#define RMB_MBA_AUTH_COMPLETE 0x4
49
50/* PBL/MBA interface registers */
51#define RMB_MBA_IMAGE_REG 0x00
52#define RMB_PBL_STATUS_REG 0x04
53#define RMB_MBA_COMMAND_REG 0x08
54#define RMB_MBA_STATUS_REG 0x0C
55#define RMB_PMI_META_DATA_REG 0x10
56#define RMB_PMI_CODE_START_REG 0x14
57#define RMB_PMI_CODE_LENGTH_REG 0x18
Sibi Sankar231f67d2018-05-21 22:57:13 +053058#define RMB_MBA_MSS_STATUS 0x40
59#define RMB_MBA_ALT_RESET 0x44
Bjorn Andersson051fb702016-06-20 14:28:41 -070060
61#define RMB_CMD_META_DATA_READY 0x1
62#define RMB_CMD_LOAD_READY 0x2
63
64/* QDSP6SS Register Offsets */
65#define QDSP6SS_RESET_REG 0x014
66#define QDSP6SS_GFMUX_CTL_REG 0x020
67#define QDSP6SS_PWR_CTL_REG 0x030
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +053068#define QDSP6SS_MEM_PWR_CTL 0x0B0
Jeffrey Hugo1665cbd2019-10-31 19:45:01 -070069#define QDSP6V6SS_MEM_PWR_CTL 0x034
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +053070#define QDSP6SS_STRAP_ACC 0x110
Bjorn Andersson051fb702016-06-20 14:28:41 -070071
72/* AXI Halt Register Offsets */
73#define AXI_HALTREQ_REG 0x0
74#define AXI_HALTACK_REG 0x4
75#define AXI_IDLE_REG 0x8
Sibi Sankar600c39b2020-01-23 18:42:36 +053076#define AXI_GATING_VALID_OVERRIDE BIT(0)
Bjorn Andersson051fb702016-06-20 14:28:41 -070077
Sibi Sankar01bf3fe2020-01-23 18:42:35 +053078#define HALT_ACK_TIMEOUT_US 100000
Bjorn Andersson051fb702016-06-20 14:28:41 -070079
80/* QDSP6SS_RESET */
81#define Q6SS_STOP_CORE BIT(0)
82#define Q6SS_CORE_ARES BIT(1)
83#define Q6SS_BUS_ARES_ENABLE BIT(2)
84
Sibi Sankar7e0f8682020-01-17 19:21:28 +053085/* QDSP6SS CBCR */
86#define Q6SS_CBCR_CLKEN BIT(0)
87#define Q6SS_CBCR_CLKOFF BIT(31)
88#define Q6SS_CBCR_TIMEOUT_US 200
89
Bjorn Andersson051fb702016-06-20 14:28:41 -070090/* QDSP6SS_GFMUX_CTL */
91#define Q6SS_CLK_ENABLE BIT(1)
92
93/* QDSP6SS_PWR_CTL */
94#define Q6SS_L2DATA_SLP_NRET_N_0 BIT(0)
95#define Q6SS_L2DATA_SLP_NRET_N_1 BIT(1)
96#define Q6SS_L2DATA_SLP_NRET_N_2 BIT(2)
97#define Q6SS_L2TAG_SLP_NRET_N BIT(16)
98#define Q6SS_ETB_SLP_NRET_N BIT(17)
99#define Q6SS_L2DATA_STBY_N BIT(18)
100#define Q6SS_SLP_RET_N BIT(19)
101#define Q6SS_CLAMP_IO BIT(20)
102#define QDSS_BHS_ON BIT(21)
103#define QDSS_LDO_BYP BIT(22)
104
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530105/* QDSP6v56 parameters */
106#define QDSP6v56_LDO_BYP BIT(25)
107#define QDSP6v56_BHS_ON BIT(24)
108#define QDSP6v56_CLAMP_WL BIT(21)
109#define QDSP6v56_CLAMP_QMC_MEM BIT(22)
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530110#define QDSP6SS_XO_CBCR 0x0038
111#define QDSP6SS_ACC_OVERRIDE_VAL 0x20
112
Sibi Sankar231f67d2018-05-21 22:57:13 +0530113/* QDSP6v65 parameters */
Sibi Sankar6439b522019-12-19 11:15:06 +0530114#define QDSP6SS_CORE_CBCR 0x20
Sibi Sankar231f67d2018-05-21 22:57:13 +0530115#define QDSP6SS_SLEEP 0x3C
116#define QDSP6SS_BOOT_CORE_START 0x400
117#define QDSP6SS_BOOT_CMD 0x404
Sibi Sankar231f67d2018-05-21 22:57:13 +0530118#define BOOT_FSM_TIMEOUT 10000
119
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530120struct reg_info {
121 struct regulator *reg;
122 int uV;
123 int uA;
124};
125
126struct qcom_mss_reg_res {
127 const char *supply;
128 int uV;
129 int uA;
130};
131
Avaneesh Kumar Dwivedi7a8ffe12016-12-30 19:24:00 +0530132struct rproc_hexagon_res {
133 const char *hexagon_mba_image;
Arnd Bergmannec671b52017-02-01 17:56:28 +0100134 struct qcom_mss_reg_res *proxy_supply;
135 struct qcom_mss_reg_res *active_supply;
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +0530136 char **proxy_clk_names;
Sibi Sankar231f67d2018-05-21 22:57:13 +0530137 char **reset_clk_names;
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +0530138 char **active_clk_names;
Bjorn Anderssondeb9bb82019-01-30 16:39:31 -0800139 char **active_pd_names;
Rajendra Nayak4760a892019-01-30 16:39:30 -0800140 char **proxy_pd_names;
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530141 int version;
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530142 bool need_mem_protection;
Sibi Sankar231f67d2018-05-21 22:57:13 +0530143 bool has_alt_reset;
Sibi Sankar318130c2020-07-21 16:59:35 +0530144 bool has_mba_logs;
Sibi Sankara9fdc792020-04-15 20:21:10 +0530145 bool has_spare_reg;
Avaneesh Kumar Dwivedi7a8ffe12016-12-30 19:24:00 +0530146};
147
Bjorn Andersson051fb702016-06-20 14:28:41 -0700148struct q6v5 {
149 struct device *dev;
150 struct rproc *rproc;
151
152 void __iomem *reg_base;
153 void __iomem *rmb_base;
154
155 struct regmap *halt_map;
Sibi Sankar6439b522019-12-19 11:15:06 +0530156 struct regmap *conn_map;
157
Bjorn Andersson051fb702016-06-20 14:28:41 -0700158 u32 halt_q6;
159 u32 halt_modem;
160 u32 halt_nc;
Sibi Sankar6439b522019-12-19 11:15:06 +0530161 u32 conn_box;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700162
163 struct reset_control *mss_restart;
Sibi Sankar29a5f9a2018-08-30 00:42:15 +0530164 struct reset_control *pdc_reset;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700165
Bjorn Andersson7d674732018-06-04 13:30:38 -0700166 struct qcom_q6v5 q6v5;
Sibi Sankar663e9842018-05-21 22:57:09 +0530167
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +0530168 struct clk *active_clks[8];
Sibi Sankar231f67d2018-05-21 22:57:13 +0530169 struct clk *reset_clks[4];
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +0530170 struct clk *proxy_clks[4];
Bjorn Anderssondeb9bb82019-01-30 16:39:31 -0800171 struct device *active_pds[1];
Rajendra Nayak4760a892019-01-30 16:39:30 -0800172 struct device *proxy_pds[3];
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +0530173 int active_clk_count;
Sibi Sankar231f67d2018-05-21 22:57:13 +0530174 int reset_clk_count;
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +0530175 int proxy_clk_count;
Bjorn Anderssondeb9bb82019-01-30 16:39:31 -0800176 int active_pd_count;
Rajendra Nayak4760a892019-01-30 16:39:30 -0800177 int proxy_pd_count;
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +0530178
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530179 struct reg_info active_regs[1];
180 struct reg_info proxy_regs[3];
181 int active_reg_count;
182 int proxy_reg_count;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700183
Sibi Sankar03045302018-10-17 19:25:25 +0530184 bool dump_mba_loaded;
Sibi Sankar7ac516d2020-07-16 15:20:32 -0700185 size_t current_dump_size;
186 size_t total_dump_size;
Sibi Sankar7dd8ade22018-10-17 19:25:26 +0530187
Bjorn Andersson051fb702016-06-20 14:28:41 -0700188 phys_addr_t mba_phys;
189 void *mba_region;
190 size_t mba_size;
Sibi Sankarfe6a5dc2020-07-23 01:40:47 +0530191 size_t dp_size;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700192
193 phys_addr_t mpss_phys;
194 phys_addr_t mpss_reloc;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700195 size_t mpss_size;
Bjorn Andersson4b489212017-01-29 14:05:50 -0800196
Sibi Sankar47254962018-05-21 22:57:14 +0530197 struct qcom_rproc_glink glink_subdev;
Bjorn Andersson4b489212017-01-29 14:05:50 -0800198 struct qcom_rproc_subdev smd_subdev;
Bjorn Andersson1e140df2017-07-24 22:56:43 -0700199 struct qcom_rproc_ssr ssr_subdev;
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -0700200 struct qcom_sysmon *sysmon;
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530201 bool need_mem_protection;
Sibi Sankar231f67d2018-05-21 22:57:13 +0530202 bool has_alt_reset;
Sibi Sankar318130c2020-07-21 16:59:35 +0530203 bool has_mba_logs;
Sibi Sankara9fdc792020-04-15 20:21:10 +0530204 bool has_spare_reg;
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530205 int mpss_perm;
206 int mba_perm;
Sibi Sankara5a4e022019-01-15 01:20:01 +0530207 const char *hexagon_mdt_image;
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530208 int version;
209};
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530210
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530211enum {
212 MSS_MSM8916,
213 MSS_MSM8974,
214 MSS_MSM8996,
Jeffrey Hugo1665cbd2019-10-31 19:45:01 -0700215 MSS_MSM8998,
Sibi Sankar6439b522019-12-19 11:15:06 +0530216 MSS_SC7180,
Sibi Sankar231f67d2018-05-21 22:57:13 +0530217 MSS_SDM845,
Bjorn Andersson051fb702016-06-20 14:28:41 -0700218};
219
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530220static int q6v5_regulator_init(struct device *dev, struct reg_info *regs,
221 const struct qcom_mss_reg_res *reg_res)
Bjorn Andersson051fb702016-06-20 14:28:41 -0700222{
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530223 int rc;
224 int i;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700225
Bjorn Andersson2bb5d902017-01-30 03:20:27 -0800226 if (!reg_res)
227 return 0;
228
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530229 for (i = 0; reg_res[i].supply; i++) {
230 regs[i].reg = devm_regulator_get(dev, reg_res[i].supply);
231 if (IS_ERR(regs[i].reg)) {
232 rc = PTR_ERR(regs[i].reg);
233 if (rc != -EPROBE_DEFER)
234 dev_err(dev, "Failed to get %s\n regulator",
235 reg_res[i].supply);
236 return rc;
237 }
Bjorn Andersson051fb702016-06-20 14:28:41 -0700238
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530239 regs[i].uV = reg_res[i].uV;
240 regs[i].uA = reg_res[i].uA;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700241 }
242
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530243 return i;
244}
245
246static int q6v5_regulator_enable(struct q6v5 *qproc,
247 struct reg_info *regs, int count)
248{
249 int ret;
250 int i;
251
252 for (i = 0; i < count; i++) {
253 if (regs[i].uV > 0) {
254 ret = regulator_set_voltage(regs[i].reg,
255 regs[i].uV, INT_MAX);
256 if (ret) {
257 dev_err(qproc->dev,
258 "Failed to request voltage for %d.\n",
259 i);
260 goto err;
261 }
262 }
263
264 if (regs[i].uA > 0) {
265 ret = regulator_set_load(regs[i].reg,
266 regs[i].uA);
267 if (ret < 0) {
268 dev_err(qproc->dev,
269 "Failed to set regulator mode\n");
270 goto err;
271 }
272 }
273
274 ret = regulator_enable(regs[i].reg);
275 if (ret) {
276 dev_err(qproc->dev, "Regulator enable failed\n");
277 goto err;
278 }
279 }
Bjorn Andersson051fb702016-06-20 14:28:41 -0700280
281 return 0;
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530282err:
283 for (; i >= 0; i--) {
284 if (regs[i].uV > 0)
285 regulator_set_voltage(regs[i].reg, 0, INT_MAX);
286
287 if (regs[i].uA > 0)
288 regulator_set_load(regs[i].reg, 0);
289
290 regulator_disable(regs[i].reg);
291 }
292
293 return ret;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700294}
295
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530296static void q6v5_regulator_disable(struct q6v5 *qproc,
297 struct reg_info *regs, int count)
Bjorn Andersson051fb702016-06-20 14:28:41 -0700298{
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530299 int i;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700300
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530301 for (i = 0; i < count; i++) {
302 if (regs[i].uV > 0)
303 regulator_set_voltage(regs[i].reg, 0, INT_MAX);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700304
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530305 if (regs[i].uA > 0)
306 regulator_set_load(regs[i].reg, 0);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700307
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +0530308 regulator_disable(regs[i].reg);
309 }
Bjorn Andersson051fb702016-06-20 14:28:41 -0700310}
311
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +0530312static int q6v5_clk_enable(struct device *dev,
313 struct clk **clks, int count)
314{
315 int rc;
316 int i;
317
318 for (i = 0; i < count; i++) {
319 rc = clk_prepare_enable(clks[i]);
320 if (rc) {
321 dev_err(dev, "Clock enable failed\n");
322 goto err;
323 }
324 }
325
326 return 0;
327err:
328 for (i--; i >= 0; i--)
329 clk_disable_unprepare(clks[i]);
330
331 return rc;
332}
333
334static void q6v5_clk_disable(struct device *dev,
335 struct clk **clks, int count)
336{
337 int i;
338
339 for (i = 0; i < count; i++)
340 clk_disable_unprepare(clks[i]);
341}
342
Rajendra Nayak4760a892019-01-30 16:39:30 -0800343static int q6v5_pds_enable(struct q6v5 *qproc, struct device **pds,
344 size_t pd_count)
345{
346 int ret;
347 int i;
348
349 for (i = 0; i < pd_count; i++) {
350 dev_pm_genpd_set_performance_state(pds[i], INT_MAX);
351 ret = pm_runtime_get_sync(pds[i]);
Zhang Qilong70ea4c72020-11-02 22:34:33 +0800352 if (ret < 0) {
353 pm_runtime_put_noidle(pds[i]);
354 dev_pm_genpd_set_performance_state(pds[i], 0);
Rajendra Nayak4760a892019-01-30 16:39:30 -0800355 goto unroll_pd_votes;
Zhang Qilong70ea4c72020-11-02 22:34:33 +0800356 }
Rajendra Nayak4760a892019-01-30 16:39:30 -0800357 }
358
359 return 0;
360
361unroll_pd_votes:
362 for (i--; i >= 0; i--) {
363 dev_pm_genpd_set_performance_state(pds[i], 0);
364 pm_runtime_put(pds[i]);
365 }
366
367 return ret;
Alex Elder58396812020-04-03 12:50:05 -0500368}
Rajendra Nayak4760a892019-01-30 16:39:30 -0800369
370static void q6v5_pds_disable(struct q6v5 *qproc, struct device **pds,
371 size_t pd_count)
372{
373 int i;
374
375 for (i = 0; i < pd_count; i++) {
376 dev_pm_genpd_set_performance_state(pds[i], 0);
377 pm_runtime_put(pds[i]);
378 }
379}
380
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530381static int q6v5_xfer_mem_ownership(struct q6v5 *qproc, int *current_perm,
Bjorn Andersson715d8522020-03-05 01:17:28 +0530382 bool local, bool remote, phys_addr_t addr,
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530383 size_t size)
384{
Bjorn Andersson715d8522020-03-05 01:17:28 +0530385 struct qcom_scm_vmperm next[2];
386 int perms = 0;
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530387
388 if (!qproc->need_mem_protection)
389 return 0;
Bjorn Andersson715d8522020-03-05 01:17:28 +0530390
391 if (local == !!(*current_perm & BIT(QCOM_SCM_VMID_HLOS)) &&
392 remote == !!(*current_perm & BIT(QCOM_SCM_VMID_MSS_MSA)))
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530393 return 0;
394
Bjorn Andersson715d8522020-03-05 01:17:28 +0530395 if (local) {
396 next[perms].vmid = QCOM_SCM_VMID_HLOS;
397 next[perms].perm = QCOM_SCM_PERM_RWX;
398 perms++;
399 }
400
401 if (remote) {
402 next[perms].vmid = QCOM_SCM_VMID_MSS_MSA;
403 next[perms].perm = QCOM_SCM_PERM_RW;
404 perms++;
405 }
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530406
Bjorn Andersson9f2a4342017-11-06 22:26:41 -0800407 return qcom_scm_assign_mem(addr, ALIGN(size, SZ_4K),
Bjorn Andersson715d8522020-03-05 01:17:28 +0530408 current_perm, next, perms);
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530409}
410
Sibi Sankarfe6a5dc2020-07-23 01:40:47 +0530411static void q6v5_debug_policy_load(struct q6v5 *qproc)
412{
413 const struct firmware *dp_fw;
414
415 if (request_firmware_direct(&dp_fw, "msadp", qproc->dev))
416 return;
417
418 if (SZ_1M + dp_fw->size <= qproc->mba_size) {
419 memcpy(qproc->mba_region + SZ_1M, dp_fw->data, dp_fw->size);
420 qproc->dp_size = dp_fw->size;
421 }
422
423 release_firmware(dp_fw);
424}
425
Bjorn Andersson051fb702016-06-20 14:28:41 -0700426static int q6v5_load(struct rproc *rproc, const struct firmware *fw)
427{
428 struct q6v5 *qproc = rproc->priv;
429
Sibi Sankare013f455d2020-07-23 01:40:45 +0530430 /* MBA is restricted to a maximum size of 1M */
431 if (fw->size > qproc->mba_size || fw->size > SZ_1M) {
432 dev_err(qproc->dev, "MBA firmware load failed\n");
433 return -EINVAL;
434 }
435
Bjorn Andersson051fb702016-06-20 14:28:41 -0700436 memcpy(qproc->mba_region, fw->data, fw->size);
Sibi Sankarfe6a5dc2020-07-23 01:40:47 +0530437 q6v5_debug_policy_load(qproc);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700438
439 return 0;
440}
441
Sibi Sankar9f135fa2018-05-21 22:57:12 +0530442static int q6v5_reset_assert(struct q6v5 *qproc)
443{
Sibi Sankar29a5f9a2018-08-30 00:42:15 +0530444 int ret;
445
446 if (qproc->has_alt_reset) {
447 reset_control_assert(qproc->pdc_reset);
448 ret = reset_control_reset(qproc->mss_restart);
449 reset_control_deassert(qproc->pdc_reset);
Sibi Sankara9fdc792020-04-15 20:21:10 +0530450 } else if (qproc->has_spare_reg) {
Sibi Sankar600c39b2020-01-23 18:42:36 +0530451 /*
452 * When the AXI pipeline is being reset with the Q6 modem partly
453 * operational there is possibility of AXI valid signal to
454 * glitch, leading to spurious transactions and Q6 hangs. A work
455 * around is employed by asserting the AXI_GATING_VALID_OVERRIDE
Sibi Sankara9fdc792020-04-15 20:21:10 +0530456 * BIT before triggering Q6 MSS reset. AXI_GATING_VALID_OVERRIDE
457 * is withdrawn post MSS assert followed by a MSS deassert,
458 * while holding the PDC reset.
Sibi Sankar600c39b2020-01-23 18:42:36 +0530459 */
Sibi Sankar6439b522019-12-19 11:15:06 +0530460 reset_control_assert(qproc->pdc_reset);
461 regmap_update_bits(qproc->conn_map, qproc->conn_box,
Sibi Sankar600c39b2020-01-23 18:42:36 +0530462 AXI_GATING_VALID_OVERRIDE, 1);
Sibi Sankar6439b522019-12-19 11:15:06 +0530463 reset_control_assert(qproc->mss_restart);
464 reset_control_deassert(qproc->pdc_reset);
465 regmap_update_bits(qproc->conn_map, qproc->conn_box,
Sibi Sankar600c39b2020-01-23 18:42:36 +0530466 AXI_GATING_VALID_OVERRIDE, 0);
Sibi Sankar6439b522019-12-19 11:15:06 +0530467 ret = reset_control_deassert(qproc->mss_restart);
Sibi Sankar29a5f9a2018-08-30 00:42:15 +0530468 } else {
469 ret = reset_control_assert(qproc->mss_restart);
470 }
471
472 return ret;
Sibi Sankar9f135fa2018-05-21 22:57:12 +0530473}
474
475static int q6v5_reset_deassert(struct q6v5 *qproc)
476{
Sibi Sankar231f67d2018-05-21 22:57:13 +0530477 int ret;
478
479 if (qproc->has_alt_reset) {
Sibi Sankar29a5f9a2018-08-30 00:42:15 +0530480 reset_control_assert(qproc->pdc_reset);
Sibi Sankar231f67d2018-05-21 22:57:13 +0530481 writel(1, qproc->rmb_base + RMB_MBA_ALT_RESET);
482 ret = reset_control_reset(qproc->mss_restart);
483 writel(0, qproc->rmb_base + RMB_MBA_ALT_RESET);
Sibi Sankar29a5f9a2018-08-30 00:42:15 +0530484 reset_control_deassert(qproc->pdc_reset);
Sibi Sankara9fdc792020-04-15 20:21:10 +0530485 } else if (qproc->has_spare_reg) {
Sibi Sankar6439b522019-12-19 11:15:06 +0530486 ret = reset_control_reset(qproc->mss_restart);
Sibi Sankar231f67d2018-05-21 22:57:13 +0530487 } else {
488 ret = reset_control_deassert(qproc->mss_restart);
489 }
490
491 return ret;
Sibi Sankar9f135fa2018-05-21 22:57:12 +0530492}
493
Bjorn Andersson051fb702016-06-20 14:28:41 -0700494static int q6v5_rmb_pbl_wait(struct q6v5 *qproc, int ms)
495{
496 unsigned long timeout;
497 s32 val;
498
499 timeout = jiffies + msecs_to_jiffies(ms);
500 for (;;) {
501 val = readl(qproc->rmb_base + RMB_PBL_STATUS_REG);
502 if (val)
503 break;
504
505 if (time_after(jiffies, timeout))
506 return -ETIMEDOUT;
507
508 msleep(1);
509 }
510
511 return val;
512}
513
514static int q6v5_rmb_mba_wait(struct q6v5 *qproc, u32 status, int ms)
515{
516
517 unsigned long timeout;
518 s32 val;
519
520 timeout = jiffies + msecs_to_jiffies(ms);
521 for (;;) {
522 val = readl(qproc->rmb_base + RMB_MBA_STATUS_REG);
523 if (val < 0)
524 break;
525
526 if (!status && val)
527 break;
528 else if (status && val == status)
529 break;
530
531 if (time_after(jiffies, timeout))
532 return -ETIMEDOUT;
533
534 msleep(1);
535 }
536
537 return val;
538}
539
Sibi Sankar318130c2020-07-21 16:59:35 +0530540static void q6v5_dump_mba_logs(struct q6v5 *qproc)
541{
542 struct rproc *rproc = qproc->rproc;
543 void *data;
544
545 if (!qproc->has_mba_logs)
546 return;
547
548 if (q6v5_xfer_mem_ownership(qproc, &qproc->mba_perm, true, false, qproc->mba_phys,
549 qproc->mba_size))
550 return;
551
552 data = vmalloc(MBA_LOG_SIZE);
553 if (!data)
554 return;
555
556 memcpy(data, qproc->mba_region, MBA_LOG_SIZE);
557 dev_coredumpv(&rproc->dev, data, MBA_LOG_SIZE, GFP_KERNEL);
558}
559
Bjorn Andersson051fb702016-06-20 14:28:41 -0700560static int q6v5proc_reset(struct q6v5 *qproc)
561{
562 u32 val;
563 int ret;
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530564 int i;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700565
Sibi Sankar231f67d2018-05-21 22:57:13 +0530566 if (qproc->version == MSS_SDM845) {
567 val = readl(qproc->reg_base + QDSP6SS_SLEEP);
Sibi Sankar7e0f8682020-01-17 19:21:28 +0530568 val |= Q6SS_CBCR_CLKEN;
Sibi Sankar231f67d2018-05-21 22:57:13 +0530569 writel(val, qproc->reg_base + QDSP6SS_SLEEP);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700570
Sibi Sankar231f67d2018-05-21 22:57:13 +0530571 ret = readl_poll_timeout(qproc->reg_base + QDSP6SS_SLEEP,
Sibi Sankar7e0f8682020-01-17 19:21:28 +0530572 val, !(val & Q6SS_CBCR_CLKOFF), 1,
573 Q6SS_CBCR_TIMEOUT_US);
Sibi Sankar231f67d2018-05-21 22:57:13 +0530574 if (ret) {
575 dev_err(qproc->dev, "QDSP6SS Sleep clock timed out\n");
576 return -ETIMEDOUT;
577 }
578
579 /* De-assert QDSP6 stop core */
580 writel(1, qproc->reg_base + QDSP6SS_BOOT_CORE_START);
581 /* Trigger boot FSM */
582 writel(1, qproc->reg_base + QDSP6SS_BOOT_CMD);
583
584 ret = readl_poll_timeout(qproc->rmb_base + RMB_MBA_MSS_STATUS,
585 val, (val & BIT(0)) != 0, 10, BOOT_FSM_TIMEOUT);
586 if (ret) {
587 dev_err(qproc->dev, "Boot FSM failed to complete.\n");
588 /* Reset the modem so that boot FSM is in reset state */
589 q6v5_reset_deassert(qproc);
590 return ret;
591 }
592
593 goto pbl_wait;
Sibi Sankar6439b522019-12-19 11:15:06 +0530594 } else if (qproc->version == MSS_SC7180) {
595 val = readl(qproc->reg_base + QDSP6SS_SLEEP);
Sibi Sankar7e0f8682020-01-17 19:21:28 +0530596 val |= Q6SS_CBCR_CLKEN;
Sibi Sankar6439b522019-12-19 11:15:06 +0530597 writel(val, qproc->reg_base + QDSP6SS_SLEEP);
598
599 ret = readl_poll_timeout(qproc->reg_base + QDSP6SS_SLEEP,
Sibi Sankar7e0f8682020-01-17 19:21:28 +0530600 val, !(val & Q6SS_CBCR_CLKOFF), 1,
601 Q6SS_CBCR_TIMEOUT_US);
Sibi Sankar6439b522019-12-19 11:15:06 +0530602 if (ret) {
603 dev_err(qproc->dev, "QDSP6SS Sleep clock timed out\n");
604 return -ETIMEDOUT;
605 }
606
607 /* Turn on the XO clock needed for PLL setup */
608 val = readl(qproc->reg_base + QDSP6SS_XO_CBCR);
Sibi Sankar7e0f8682020-01-17 19:21:28 +0530609 val |= Q6SS_CBCR_CLKEN;
Sibi Sankar6439b522019-12-19 11:15:06 +0530610 writel(val, qproc->reg_base + QDSP6SS_XO_CBCR);
611
612 ret = readl_poll_timeout(qproc->reg_base + QDSP6SS_XO_CBCR,
Sibi Sankar7e0f8682020-01-17 19:21:28 +0530613 val, !(val & Q6SS_CBCR_CLKOFF), 1,
614 Q6SS_CBCR_TIMEOUT_US);
Sibi Sankar6439b522019-12-19 11:15:06 +0530615 if (ret) {
616 dev_err(qproc->dev, "QDSP6SS XO clock timed out\n");
617 return -ETIMEDOUT;
618 }
619
620 /* Configure Q6 core CBCR to auto-enable after reset sequence */
621 val = readl(qproc->reg_base + QDSP6SS_CORE_CBCR);
Sibi Sankar7e0f8682020-01-17 19:21:28 +0530622 val |= Q6SS_CBCR_CLKEN;
Sibi Sankar6439b522019-12-19 11:15:06 +0530623 writel(val, qproc->reg_base + QDSP6SS_CORE_CBCR);
624
625 /* De-assert the Q6 stop core signal */
626 writel(1, qproc->reg_base + QDSP6SS_BOOT_CORE_START);
627
Sibi Sankar4e6751a2020-07-16 17:35:14 +0530628 /* Wait for 10 us for any staggering logic to settle */
629 usleep_range(10, 20);
630
Sibi Sankar6439b522019-12-19 11:15:06 +0530631 /* Trigger the boot FSM to start the Q6 out-of-reset sequence */
632 writel(1, qproc->reg_base + QDSP6SS_BOOT_CMD);
633
Sibi Sankar4e6751a2020-07-16 17:35:14 +0530634 /* Poll the MSS_STATUS for FSM completion */
635 ret = readl_poll_timeout(qproc->rmb_base + RMB_MBA_MSS_STATUS,
636 val, (val & BIT(0)) != 0, 10, BOOT_FSM_TIMEOUT);
Sibi Sankar6439b522019-12-19 11:15:06 +0530637 if (ret) {
638 dev_err(qproc->dev, "Boot FSM failed to complete.\n");
639 /* Reset the modem so that boot FSM is in reset state */
640 q6v5_reset_deassert(qproc);
641 return ret;
642 }
643 goto pbl_wait;
Jeffrey Hugo1665cbd2019-10-31 19:45:01 -0700644 } else if (qproc->version == MSS_MSM8996 ||
645 qproc->version == MSS_MSM8998) {
646 int mem_pwr_ctl;
647
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530648 /* Override the ACC value if required */
649 writel(QDSP6SS_ACC_OVERRIDE_VAL,
650 qproc->reg_base + QDSP6SS_STRAP_ACC);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700651
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530652 /* Assert resets, stop core */
653 val = readl(qproc->reg_base + QDSP6SS_RESET_REG);
654 val |= Q6SS_CORE_ARES | Q6SS_BUS_ARES_ENABLE | Q6SS_STOP_CORE;
655 writel(val, qproc->reg_base + QDSP6SS_RESET_REG);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700656
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530657 /* BHS require xo cbcr to be enabled */
658 val = readl(qproc->reg_base + QDSP6SS_XO_CBCR);
Sibi Sankar7e0f8682020-01-17 19:21:28 +0530659 val |= Q6SS_CBCR_CLKEN;
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530660 writel(val, qproc->reg_base + QDSP6SS_XO_CBCR);
661
662 /* Read CLKOFF bit to go low indicating CLK is enabled */
663 ret = readl_poll_timeout(qproc->reg_base + QDSP6SS_XO_CBCR,
Sibi Sankar7e0f8682020-01-17 19:21:28 +0530664 val, !(val & Q6SS_CBCR_CLKOFF), 1,
665 Q6SS_CBCR_TIMEOUT_US);
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530666 if (ret) {
667 dev_err(qproc->dev,
668 "xo cbcr enabling timed out (rc:%d)\n", ret);
669 return ret;
670 }
671 /* Enable power block headswitch and wait for it to stabilize */
672 val = readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
673 val |= QDSP6v56_BHS_ON;
674 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
675 val |= readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
676 udelay(1);
677
678 /* Put LDO in bypass mode */
679 val |= QDSP6v56_LDO_BYP;
680 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
681
682 /* Deassert QDSP6 compiler memory clamp */
683 val = readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
684 val &= ~QDSP6v56_CLAMP_QMC_MEM;
685 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
686
687 /* Deassert memory peripheral sleep and L2 memory standby */
688 val |= Q6SS_L2DATA_STBY_N | Q6SS_SLP_RET_N;
689 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
690
691 /* Turn on L1, L2, ETB and JU memories 1 at a time */
Jeffrey Hugo1665cbd2019-10-31 19:45:01 -0700692 if (qproc->version == MSS_MSM8996) {
693 mem_pwr_ctl = QDSP6SS_MEM_PWR_CTL;
694 i = 19;
695 } else {
696 /* MSS_MSM8998 */
697 mem_pwr_ctl = QDSP6V6SS_MEM_PWR_CTL;
698 i = 28;
699 }
700 val = readl(qproc->reg_base + mem_pwr_ctl);
701 for (; i >= 0; i--) {
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530702 val |= BIT(i);
Jeffrey Hugo1665cbd2019-10-31 19:45:01 -0700703 writel(val, qproc->reg_base + mem_pwr_ctl);
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530704 /*
705 * Read back value to ensure the write is done then
706 * wait for 1us for both memory peripheral and data
707 * array to turn on.
708 */
Jeffrey Hugo1665cbd2019-10-31 19:45:01 -0700709 val |= readl(qproc->reg_base + mem_pwr_ctl);
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +0530710 udelay(1);
711 }
712 /* Remove word line clamp */
713 val = readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
714 val &= ~QDSP6v56_CLAMP_WL;
715 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
716 } else {
717 /* Assert resets, stop core */
718 val = readl(qproc->reg_base + QDSP6SS_RESET_REG);
719 val |= Q6SS_CORE_ARES | Q6SS_BUS_ARES_ENABLE | Q6SS_STOP_CORE;
720 writel(val, qproc->reg_base + QDSP6SS_RESET_REG);
721
722 /* Enable power block headswitch and wait for it to stabilize */
723 val = readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
724 val |= QDSS_BHS_ON | QDSS_LDO_BYP;
725 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
726 val |= readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
727 udelay(1);
728 /*
729 * Turn on memories. L2 banks should be done individually
730 * to minimize inrush current.
731 */
732 val = readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
733 val |= Q6SS_SLP_RET_N | Q6SS_L2TAG_SLP_NRET_N |
734 Q6SS_ETB_SLP_NRET_N | Q6SS_L2DATA_STBY_N;
735 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
736 val |= Q6SS_L2DATA_SLP_NRET_N_2;
737 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
738 val |= Q6SS_L2DATA_SLP_NRET_N_1;
739 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
740 val |= Q6SS_L2DATA_SLP_NRET_N_0;
741 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
742 }
Bjorn Andersson051fb702016-06-20 14:28:41 -0700743 /* Remove IO clamp */
744 val &= ~Q6SS_CLAMP_IO;
745 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
746
747 /* Bring core out of reset */
748 val = readl(qproc->reg_base + QDSP6SS_RESET_REG);
749 val &= ~Q6SS_CORE_ARES;
750 writel(val, qproc->reg_base + QDSP6SS_RESET_REG);
751
752 /* Turn on core clock */
753 val = readl(qproc->reg_base + QDSP6SS_GFMUX_CTL_REG);
754 val |= Q6SS_CLK_ENABLE;
755 writel(val, qproc->reg_base + QDSP6SS_GFMUX_CTL_REG);
756
757 /* Start core execution */
758 val = readl(qproc->reg_base + QDSP6SS_RESET_REG);
759 val &= ~Q6SS_STOP_CORE;
760 writel(val, qproc->reg_base + QDSP6SS_RESET_REG);
761
Sibi Sankar231f67d2018-05-21 22:57:13 +0530762pbl_wait:
Bjorn Andersson051fb702016-06-20 14:28:41 -0700763 /* Wait for PBL status */
764 ret = q6v5_rmb_pbl_wait(qproc, 1000);
765 if (ret == -ETIMEDOUT) {
766 dev_err(qproc->dev, "PBL boot timed out\n");
767 } else if (ret != RMB_PBL_SUCCESS) {
768 dev_err(qproc->dev, "PBL returned unexpected status %d\n", ret);
769 ret = -EINVAL;
770 } else {
771 ret = 0;
772 }
773
774 return ret;
775}
776
777static void q6v5proc_halt_axi_port(struct q6v5 *qproc,
778 struct regmap *halt_map,
779 u32 offset)
780{
Bjorn Andersson051fb702016-06-20 14:28:41 -0700781 unsigned int val;
782 int ret;
783
784 /* Check if we're already idle */
785 ret = regmap_read(halt_map, offset + AXI_IDLE_REG, &val);
786 if (!ret && val)
787 return;
788
789 /* Assert halt request */
790 regmap_write(halt_map, offset + AXI_HALTREQ_REG, 1);
791
792 /* Wait for halt */
Sibi Sankar01bf3fe2020-01-23 18:42:35 +0530793 regmap_read_poll_timeout(halt_map, offset + AXI_HALTACK_REG, val,
794 val, 1000, HALT_ACK_TIMEOUT_US);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700795
796 ret = regmap_read(halt_map, offset + AXI_IDLE_REG, &val);
797 if (ret || !val)
798 dev_err(qproc->dev, "port failed halt\n");
799
800 /* Clear halt request (port will remain halted until reset) */
801 regmap_write(halt_map, offset + AXI_HALTREQ_REG, 0);
802}
803
804static int q6v5_mpss_init_image(struct q6v5 *qproc, const struct firmware *fw)
805{
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700806 unsigned long dma_attrs = DMA_ATTR_FORCE_CONTIGUOUS;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700807 dma_addr_t phys;
Bjorn Anderssonf04b9132019-06-21 18:21:46 -0700808 void *metadata;
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530809 int mdata_perm;
810 int xferop_ret;
Bjorn Anderssonf04b9132019-06-21 18:21:46 -0700811 size_t size;
Bjorn Andersson051fb702016-06-20 14:28:41 -0700812 void *ptr;
813 int ret;
814
Bjorn Anderssonf04b9132019-06-21 18:21:46 -0700815 metadata = qcom_mdt_read_metadata(fw, &size);
816 if (IS_ERR(metadata))
817 return PTR_ERR(metadata);
818
819 ptr = dma_alloc_attrs(qproc->dev, size, &phys, GFP_KERNEL, dma_attrs);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700820 if (!ptr) {
Bjorn Anderssonf04b9132019-06-21 18:21:46 -0700821 kfree(metadata);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700822 dev_err(qproc->dev, "failed to allocate mdt buffer\n");
823 return -ENOMEM;
824 }
825
Bjorn Anderssonf04b9132019-06-21 18:21:46 -0700826 memcpy(ptr, metadata, size);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700827
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530828 /* Hypervisor mapping to access metadata by modem */
829 mdata_perm = BIT(QCOM_SCM_VMID_HLOS);
Bjorn Andersson715d8522020-03-05 01:17:28 +0530830 ret = q6v5_xfer_mem_ownership(qproc, &mdata_perm, false, true,
831 phys, size);
Bjorn Andersson9f2a4342017-11-06 22:26:41 -0800832 if (ret) {
833 dev_err(qproc->dev,
834 "assigning Q6 access to metadata failed: %d\n", ret);
Christophe JAILLET1a5d5c52017-11-15 07:58:35 +0100835 ret = -EAGAIN;
836 goto free_dma_attrs;
Bjorn Andersson9f2a4342017-11-06 22:26:41 -0800837 }
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530838
Bjorn Andersson051fb702016-06-20 14:28:41 -0700839 writel(phys, qproc->rmb_base + RMB_PMI_META_DATA_REG);
840 writel(RMB_CMD_META_DATA_READY, qproc->rmb_base + RMB_MBA_COMMAND_REG);
841
842 ret = q6v5_rmb_mba_wait(qproc, RMB_MBA_META_DATA_AUTH_SUCCESS, 1000);
843 if (ret == -ETIMEDOUT)
844 dev_err(qproc->dev, "MPSS header authentication timed out\n");
845 else if (ret < 0)
846 dev_err(qproc->dev, "MPSS header authentication failed: %d\n", ret);
847
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530848 /* Metadata authentication done, remove modem access */
Bjorn Andersson715d8522020-03-05 01:17:28 +0530849 xferop_ret = q6v5_xfer_mem_ownership(qproc, &mdata_perm, true, false,
850 phys, size);
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +0530851 if (xferop_ret)
852 dev_warn(qproc->dev,
853 "mdt buffer not reclaimed system may become unstable\n");
854
Christophe JAILLET1a5d5c52017-11-15 07:58:35 +0100855free_dma_attrs:
Bjorn Anderssonf04b9132019-06-21 18:21:46 -0700856 dma_free_attrs(qproc->dev, size, ptr, phys, dma_attrs);
857 kfree(metadata);
Bjorn Andersson051fb702016-06-20 14:28:41 -0700858
859 return ret < 0 ? ret : 0;
860}
861
Bjorn Anderssone7fd2522017-01-26 13:58:35 -0800862static bool q6v5_phdr_valid(const struct elf32_phdr *phdr)
863{
864 if (phdr->p_type != PT_LOAD)
865 return false;
866
867 if ((phdr->p_flags & QCOM_MDT_TYPE_MASK) == QCOM_MDT_TYPE_HASH)
868 return false;
869
870 if (!phdr->p_memsz)
871 return false;
872
873 return true;
874}
875
Sibi Sankar03045302018-10-17 19:25:25 +0530876static int q6v5_mba_load(struct q6v5 *qproc)
877{
878 int ret;
879 int xfermemop_ret;
Sibi Sankar318130c2020-07-21 16:59:35 +0530880 bool mba_load_err = false;
Sibi Sankar03045302018-10-17 19:25:25 +0530881
882 qcom_q6v5_prepare(&qproc->q6v5);
883
Bjorn Anderssondeb9bb82019-01-30 16:39:31 -0800884 ret = q6v5_pds_enable(qproc, qproc->active_pds, qproc->active_pd_count);
885 if (ret < 0) {
886 dev_err(qproc->dev, "failed to enable active power domains\n");
887 goto disable_irqs;
888 }
889
Rajendra Nayak4760a892019-01-30 16:39:30 -0800890 ret = q6v5_pds_enable(qproc, qproc->proxy_pds, qproc->proxy_pd_count);
891 if (ret < 0) {
892 dev_err(qproc->dev, "failed to enable proxy power domains\n");
Bjorn Anderssondeb9bb82019-01-30 16:39:31 -0800893 goto disable_active_pds;
Rajendra Nayak4760a892019-01-30 16:39:30 -0800894 }
895
Sibi Sankar03045302018-10-17 19:25:25 +0530896 ret = q6v5_regulator_enable(qproc, qproc->proxy_regs,
897 qproc->proxy_reg_count);
898 if (ret) {
899 dev_err(qproc->dev, "failed to enable proxy supplies\n");
Rajendra Nayak4760a892019-01-30 16:39:30 -0800900 goto disable_proxy_pds;
Sibi Sankar03045302018-10-17 19:25:25 +0530901 }
902
903 ret = q6v5_clk_enable(qproc->dev, qproc->proxy_clks,
904 qproc->proxy_clk_count);
905 if (ret) {
906 dev_err(qproc->dev, "failed to enable proxy clocks\n");
907 goto disable_proxy_reg;
908 }
909
910 ret = q6v5_regulator_enable(qproc, qproc->active_regs,
911 qproc->active_reg_count);
912 if (ret) {
913 dev_err(qproc->dev, "failed to enable supplies\n");
914 goto disable_proxy_clk;
915 }
916
917 ret = q6v5_clk_enable(qproc->dev, qproc->reset_clks,
918 qproc->reset_clk_count);
919 if (ret) {
920 dev_err(qproc->dev, "failed to enable reset clocks\n");
921 goto disable_vdd;
922 }
923
924 ret = q6v5_reset_deassert(qproc);
925 if (ret) {
926 dev_err(qproc->dev, "failed to deassert mss restart\n");
927 goto disable_reset_clks;
928 }
929
930 ret = q6v5_clk_enable(qproc->dev, qproc->active_clks,
931 qproc->active_clk_count);
932 if (ret) {
933 dev_err(qproc->dev, "failed to enable clocks\n");
934 goto assert_reset;
935 }
936
Sibi Sankar4360f932020-09-17 23:28:40 +0530937 /*
938 * Some versions of the MBA firmware will upon boot wipe the MPSS region as well, so provide
939 * the Q6 access to this region.
940 */
941 ret = q6v5_xfer_mem_ownership(qproc, &qproc->mpss_perm, false, true,
942 qproc->mpss_phys, qproc->mpss_size);
943 if (ret) {
944 dev_err(qproc->dev, "assigning Q6 access to mpss memory failed: %d\n", ret);
945 goto disable_active_clks;
946 }
947
Sibi Sankar03045302018-10-17 19:25:25 +0530948 /* Assign MBA image access in DDR to q6 */
Bjorn Andersson715d8522020-03-05 01:17:28 +0530949 ret = q6v5_xfer_mem_ownership(qproc, &qproc->mba_perm, false, true,
Sibi Sankar03045302018-10-17 19:25:25 +0530950 qproc->mba_phys, qproc->mba_size);
951 if (ret) {
952 dev_err(qproc->dev,
953 "assigning Q6 access to mba memory failed: %d\n", ret);
954 goto disable_active_clks;
955 }
956
957 writel(qproc->mba_phys, qproc->rmb_base + RMB_MBA_IMAGE_REG);
Sibi Sankarfe6a5dc2020-07-23 01:40:47 +0530958 if (qproc->dp_size) {
959 writel(qproc->mba_phys + SZ_1M, qproc->rmb_base + RMB_PMI_CODE_START_REG);
960 writel(qproc->dp_size, qproc->rmb_base + RMB_PMI_CODE_LENGTH_REG);
961 }
Sibi Sankar03045302018-10-17 19:25:25 +0530962
963 ret = q6v5proc_reset(qproc);
964 if (ret)
965 goto reclaim_mba;
966
967 ret = q6v5_rmb_mba_wait(qproc, 0, 5000);
968 if (ret == -ETIMEDOUT) {
969 dev_err(qproc->dev, "MBA boot timed out\n");
970 goto halt_axi_ports;
971 } else if (ret != RMB_MBA_XPU_UNLOCKED &&
972 ret != RMB_MBA_XPU_UNLOCKED_SCRIBBLED) {
973 dev_err(qproc->dev, "MBA returned unexpected status %d\n", ret);
974 ret = -EINVAL;
975 goto halt_axi_ports;
976 }
977
978 qproc->dump_mba_loaded = true;
979 return 0;
980
981halt_axi_ports:
982 q6v5proc_halt_axi_port(qproc, qproc->halt_map, qproc->halt_q6);
983 q6v5proc_halt_axi_port(qproc, qproc->halt_map, qproc->halt_modem);
984 q6v5proc_halt_axi_port(qproc, qproc->halt_map, qproc->halt_nc);
Sibi Sankar318130c2020-07-21 16:59:35 +0530985 mba_load_err = true;
Sibi Sankar03045302018-10-17 19:25:25 +0530986reclaim_mba:
Bjorn Andersson715d8522020-03-05 01:17:28 +0530987 xfermemop_ret = q6v5_xfer_mem_ownership(qproc, &qproc->mba_perm, true,
988 false, qproc->mba_phys,
Sibi Sankar03045302018-10-17 19:25:25 +0530989 qproc->mba_size);
990 if (xfermemop_ret) {
991 dev_err(qproc->dev,
992 "Failed to reclaim mba buffer, system may become unstable\n");
Sibi Sankar318130c2020-07-21 16:59:35 +0530993 } else if (mba_load_err) {
994 q6v5_dump_mba_logs(qproc);
Sibi Sankar03045302018-10-17 19:25:25 +0530995 }
996
997disable_active_clks:
998 q6v5_clk_disable(qproc->dev, qproc->active_clks,
999 qproc->active_clk_count);
1000assert_reset:
1001 q6v5_reset_assert(qproc);
1002disable_reset_clks:
1003 q6v5_clk_disable(qproc->dev, qproc->reset_clks,
1004 qproc->reset_clk_count);
1005disable_vdd:
1006 q6v5_regulator_disable(qproc, qproc->active_regs,
1007 qproc->active_reg_count);
1008disable_proxy_clk:
1009 q6v5_clk_disable(qproc->dev, qproc->proxy_clks,
1010 qproc->proxy_clk_count);
1011disable_proxy_reg:
1012 q6v5_regulator_disable(qproc, qproc->proxy_regs,
1013 qproc->proxy_reg_count);
Rajendra Nayak4760a892019-01-30 16:39:30 -08001014disable_proxy_pds:
1015 q6v5_pds_disable(qproc, qproc->proxy_pds, qproc->proxy_pd_count);
Bjorn Anderssondeb9bb82019-01-30 16:39:31 -08001016disable_active_pds:
1017 q6v5_pds_disable(qproc, qproc->active_pds, qproc->active_pd_count);
Sibi Sankar03045302018-10-17 19:25:25 +05301018disable_irqs:
1019 qcom_q6v5_unprepare(&qproc->q6v5);
1020
1021 return ret;
1022}
1023
1024static void q6v5_mba_reclaim(struct q6v5 *qproc)
1025{
1026 int ret;
1027 u32 val;
1028
1029 qproc->dump_mba_loaded = false;
Sibi Sankarfe6a5dc2020-07-23 01:40:47 +05301030 qproc->dp_size = 0;
Sibi Sankar03045302018-10-17 19:25:25 +05301031
1032 q6v5proc_halt_axi_port(qproc, qproc->halt_map, qproc->halt_q6);
1033 q6v5proc_halt_axi_port(qproc, qproc->halt_map, qproc->halt_modem);
1034 q6v5proc_halt_axi_port(qproc, qproc->halt_map, qproc->halt_nc);
1035 if (qproc->version == MSS_MSM8996) {
1036 /*
1037 * To avoid high MX current during LPASS/MSS restart.
1038 */
1039 val = readl(qproc->reg_base + QDSP6SS_PWR_CTL_REG);
1040 val |= Q6SS_CLAMP_IO | QDSP6v56_CLAMP_WL |
1041 QDSP6v56_CLAMP_QMC_MEM;
1042 writel(val, qproc->reg_base + QDSP6SS_PWR_CTL_REG);
1043 }
1044
Sibi Sankar03045302018-10-17 19:25:25 +05301045 q6v5_reset_assert(qproc);
1046
1047 q6v5_clk_disable(qproc->dev, qproc->reset_clks,
1048 qproc->reset_clk_count);
1049 q6v5_clk_disable(qproc->dev, qproc->active_clks,
1050 qproc->active_clk_count);
1051 q6v5_regulator_disable(qproc, qproc->active_regs,
1052 qproc->active_reg_count);
Bjorn Anderssondeb9bb82019-01-30 16:39:31 -08001053 q6v5_pds_disable(qproc, qproc->active_pds, qproc->active_pd_count);
Sibi Sankar03045302018-10-17 19:25:25 +05301054
1055 /* In case of failure or coredump scenario where reclaiming MBA memory
1056 * could not happen reclaim it here.
1057 */
Bjorn Andersson715d8522020-03-05 01:17:28 +05301058 ret = q6v5_xfer_mem_ownership(qproc, &qproc->mba_perm, true, false,
Sibi Sankar03045302018-10-17 19:25:25 +05301059 qproc->mba_phys,
1060 qproc->mba_size);
1061 WARN_ON(ret);
1062
1063 ret = qcom_q6v5_unprepare(&qproc->q6v5);
1064 if (ret) {
Rajendra Nayak4760a892019-01-30 16:39:30 -08001065 q6v5_pds_disable(qproc, qproc->proxy_pds,
1066 qproc->proxy_pd_count);
Sibi Sankar03045302018-10-17 19:25:25 +05301067 q6v5_clk_disable(qproc->dev, qproc->proxy_clks,
1068 qproc->proxy_clk_count);
1069 q6v5_regulator_disable(qproc, qproc->proxy_regs,
1070 qproc->proxy_reg_count);
1071 }
1072}
1073
Sibi Sankard96f2572020-03-05 01:17:29 +05301074static int q6v5_reload_mba(struct rproc *rproc)
1075{
1076 struct q6v5 *qproc = rproc->priv;
1077 const struct firmware *fw;
1078 int ret;
1079
1080 ret = request_firmware(&fw, rproc->firmware, qproc->dev);
1081 if (ret < 0)
1082 return ret;
1083
1084 q6v5_load(rproc, fw);
1085 ret = q6v5_mba_load(qproc);
1086 release_firmware(fw);
1087
1088 return ret;
1089}
1090
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001091static int q6v5_mpss_load(struct q6v5 *qproc)
Bjorn Andersson051fb702016-06-20 14:28:41 -07001092{
1093 const struct elf32_phdr *phdrs;
1094 const struct elf32_phdr *phdr;
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001095 const struct firmware *seg_fw;
1096 const struct firmware *fw;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001097 struct elf32_hdr *ehdr;
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001098 phys_addr_t mpss_reloc;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001099 phys_addr_t boot_addr;
Stefan Agnerd7dc8992018-06-14 15:28:02 -07001100 phys_addr_t min_addr = PHYS_ADDR_MAX;
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001101 phys_addr_t max_addr = 0;
Bjorn Andersson715d8522020-03-05 01:17:28 +05301102 u32 code_length;
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001103 bool relocate = false;
Sibi Sankara5a4e022019-01-15 01:20:01 +05301104 char *fw_name;
1105 size_t fw_name_len;
Bjorn Andersson01625cc52017-02-15 14:00:41 -08001106 ssize_t offset;
Avaneesh Kumar Dwivedi94c90782017-10-24 21:22:25 +05301107 size_t size = 0;
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001108 void *ptr;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001109 int ret;
1110 int i;
1111
Sibi Sankara5a4e022019-01-15 01:20:01 +05301112 fw_name_len = strlen(qproc->hexagon_mdt_image);
1113 if (fw_name_len <= 4)
1114 return -EINVAL;
1115
1116 fw_name = kstrdup(qproc->hexagon_mdt_image, GFP_KERNEL);
1117 if (!fw_name)
1118 return -ENOMEM;
1119
1120 ret = request_firmware(&fw, fw_name, qproc->dev);
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001121 if (ret < 0) {
Sibi Sankara5a4e022019-01-15 01:20:01 +05301122 dev_err(qproc->dev, "unable to load %s\n", fw_name);
1123 goto out;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001124 }
1125
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001126 /* Initialize the RMB validator */
1127 writel(0, qproc->rmb_base + RMB_PMI_CODE_LENGTH_REG);
1128
1129 ret = q6v5_mpss_init_image(qproc, fw);
1130 if (ret)
1131 goto release_firmware;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001132
1133 ehdr = (struct elf32_hdr *)fw->data;
1134 phdrs = (struct elf32_phdr *)(ehdr + 1);
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001135
1136 for (i = 0; i < ehdr->e_phnum; i++) {
Bjorn Andersson051fb702016-06-20 14:28:41 -07001137 phdr = &phdrs[i];
1138
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001139 if (!q6v5_phdr_valid(phdr))
Bjorn Andersson051fb702016-06-20 14:28:41 -07001140 continue;
1141
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001142 if (phdr->p_flags & QCOM_MDT_RELOCATABLE)
1143 relocate = true;
1144
1145 if (phdr->p_paddr < min_addr)
1146 min_addr = phdr->p_paddr;
1147
1148 if (phdr->p_paddr + phdr->p_memsz > max_addr)
1149 max_addr = ALIGN(phdr->p_paddr + phdr->p_memsz, SZ_4K);
1150 }
1151
Sibi Sankar4360f932020-09-17 23:28:40 +05301152 /*
Bjorn Andersson900fc602020-03-05 01:17:27 +05301153 * In case of a modem subsystem restart on secure devices, the modem
Sibi Sankar4360f932020-09-17 23:28:40 +05301154 * memory can be reclaimed only after MBA is loaded.
Bjorn Andersson900fc602020-03-05 01:17:27 +05301155 */
Bjorn Andersson715d8522020-03-05 01:17:28 +05301156 q6v5_xfer_mem_ownership(qproc, &qproc->mpss_perm, true, false,
Bjorn Andersson900fc602020-03-05 01:17:27 +05301157 qproc->mpss_phys, qproc->mpss_size);
1158
Bjorn Andersson715d8522020-03-05 01:17:28 +05301159 /* Share ownership between Linux and MSS, during segment loading */
1160 ret = q6v5_xfer_mem_ownership(qproc, &qproc->mpss_perm, true, true,
1161 qproc->mpss_phys, qproc->mpss_size);
1162 if (ret) {
1163 dev_err(qproc->dev,
1164 "assigning Q6 access to mpss memory failed: %d\n", ret);
1165 ret = -EAGAIN;
1166 goto release_firmware;
1167 }
1168
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001169 mpss_reloc = relocate ? min_addr : qproc->mpss_phys;
Sibi Sankar3bf62eb2018-07-27 20:50:03 +05301170 qproc->mpss_reloc = mpss_reloc;
Avaneesh Kumar Dwivedi94c90782017-10-24 21:22:25 +05301171 /* Load firmware segments */
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001172 for (i = 0; i < ehdr->e_phnum; i++) {
1173 phdr = &phdrs[i];
1174
1175 if (!q6v5_phdr_valid(phdr))
Bjorn Andersson051fb702016-06-20 14:28:41 -07001176 continue;
1177
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001178 offset = phdr->p_paddr - mpss_reloc;
1179 if (offset < 0 || offset + phdr->p_memsz > qproc->mpss_size) {
1180 dev_err(qproc->dev, "segment outside memory range\n");
1181 ret = -EINVAL;
1182 goto release_firmware;
1183 }
1184
Bjorn Andersson1ce0d1d2021-03-12 15:20:02 -08001185 if (phdr->p_filesz > phdr->p_memsz) {
1186 dev_err(qproc->dev,
1187 "refusing to load segment %d with p_filesz > p_memsz\n",
1188 i);
1189 ret = -EINVAL;
1190 goto release_firmware;
1191 }
1192
Sibi Sankar2ec65062020-11-04 12:33:41 +05301193 ptr = memremap(qproc->mpss_phys + offset, phdr->p_memsz, MEMREMAP_WC);
Sibi Sankarbe050a32020-04-15 12:46:18 +05301194 if (!ptr) {
1195 dev_err(qproc->dev,
1196 "unable to map memory region: %pa+%zx-%x\n",
1197 &qproc->mpss_phys, offset, phdr->p_memsz);
1198 goto release_firmware;
1199 }
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001200
Bjorn Anderssonf04b9132019-06-21 18:21:46 -07001201 if (phdr->p_filesz && phdr->p_offset < fw->size) {
1202 /* Firmware is large enough to be non-split */
1203 if (phdr->p_offset + phdr->p_filesz > fw->size) {
1204 dev_err(qproc->dev,
1205 "failed to load segment %d from truncated file %s\n",
1206 i, fw_name);
1207 ret = -EINVAL;
Sibi Sankar2ec65062020-11-04 12:33:41 +05301208 memunmap(ptr);
Bjorn Anderssonf04b9132019-06-21 18:21:46 -07001209 goto release_firmware;
1210 }
1211
1212 memcpy(ptr, fw->data + phdr->p_offset, phdr->p_filesz);
1213 } else if (phdr->p_filesz) {
Sibi Sankara5a4e022019-01-15 01:20:01 +05301214 /* Replace "xxx.xxx" with "xxx.bxx" */
1215 sprintf(fw_name + fw_name_len - 3, "b%02d", i);
Sibi Sankar135b9e82020-07-23 01:40:46 +05301216 ret = request_firmware_into_buf(&seg_fw, fw_name, qproc->dev,
1217 ptr, phdr->p_filesz);
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001218 if (ret) {
Sibi Sankara5a4e022019-01-15 01:20:01 +05301219 dev_err(qproc->dev, "failed to load %s\n", fw_name);
Sibi Sankar2ec65062020-11-04 12:33:41 +05301220 memunmap(ptr);
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001221 goto release_firmware;
1222 }
1223
Bjorn Andersson1ce0d1d2021-03-12 15:20:02 -08001224 if (seg_fw->size != phdr->p_filesz) {
1225 dev_err(qproc->dev,
1226 "failed to load segment %d from truncated file %s\n",
1227 i, fw_name);
1228 ret = -EINVAL;
1229 release_firmware(seg_fw);
1230 memunmap(ptr);
1231 goto release_firmware;
1232 }
1233
Bjorn Anderssone7fd2522017-01-26 13:58:35 -08001234 release_firmware(seg_fw);
1235 }
1236
1237 if (phdr->p_memsz > phdr->p_filesz) {
1238 memset(ptr + phdr->p_filesz, 0,
1239 phdr->p_memsz - phdr->p_filesz);
1240 }
Sibi Sankar2ec65062020-11-04 12:33:41 +05301241 memunmap(ptr);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001242 size += phdr->p_memsz;
Bjorn Andersson715d8522020-03-05 01:17:28 +05301243
1244 code_length = readl(qproc->rmb_base + RMB_PMI_CODE_LENGTH_REG);
1245 if (!code_length) {
1246 boot_addr = relocate ? qproc->mpss_phys : min_addr;
1247 writel(boot_addr, qproc->rmb_base + RMB_PMI_CODE_START_REG);
1248 writel(RMB_CMD_LOAD_READY, qproc->rmb_base + RMB_MBA_COMMAND_REG);
1249 }
1250 writel(size, qproc->rmb_base + RMB_PMI_CODE_LENGTH_REG);
1251
1252 ret = readl(qproc->rmb_base + RMB_MBA_STATUS_REG);
1253 if (ret < 0) {
1254 dev_err(qproc->dev, "MPSS authentication failed: %d\n",
1255 ret);
1256 goto release_firmware;
1257 }
Bjorn Andersson051fb702016-06-20 14:28:41 -07001258 }
1259
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +05301260 /* Transfer ownership of modem ddr region to q6 */
Bjorn Andersson715d8522020-03-05 01:17:28 +05301261 ret = q6v5_xfer_mem_ownership(qproc, &qproc->mpss_perm, false, true,
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +05301262 qproc->mpss_phys, qproc->mpss_size);
Bjorn Andersson9f2a4342017-11-06 22:26:41 -08001263 if (ret) {
1264 dev_err(qproc->dev,
1265 "assigning Q6 access to mpss memory failed: %d\n", ret);
Christophe JAILLET1a5d5c52017-11-15 07:58:35 +01001266 ret = -EAGAIN;
1267 goto release_firmware;
Bjorn Andersson9f2a4342017-11-06 22:26:41 -08001268 }
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +05301269
Bjorn Andersson72beb492016-07-12 17:15:45 -07001270 ret = q6v5_rmb_mba_wait(qproc, RMB_MBA_AUTH_COMPLETE, 10000);
1271 if (ret == -ETIMEDOUT)
1272 dev_err(qproc->dev, "MPSS authentication timed out\n");
1273 else if (ret < 0)
1274 dev_err(qproc->dev, "MPSS authentication failed: %d\n", ret);
1275
Bjorn Anderssond4c78d22020-06-22 12:19:40 -07001276 qcom_pil_info_store("modem", qproc->mpss_phys, qproc->mpss_size);
1277
Bjorn Andersson051fb702016-06-20 14:28:41 -07001278release_firmware:
1279 release_firmware(fw);
Sibi Sankara5a4e022019-01-15 01:20:01 +05301280out:
1281 kfree(fw_name);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001282
1283 return ret < 0 ? ret : 0;
1284}
1285
Sibi Sankar7dd8ade22018-10-17 19:25:26 +05301286static void qcom_q6v5_dump_segment(struct rproc *rproc,
1287 struct rproc_dump_segment *segment,
Rishabh Bhatnagar76abf9c2020-07-16 15:20:33 -07001288 void *dest, size_t cp_offset, size_t size)
Sibi Sankar7dd8ade22018-10-17 19:25:26 +05301289{
1290 int ret = 0;
1291 struct q6v5 *qproc = rproc->priv;
Sibi Sankarbe050a32020-04-15 12:46:18 +05301292 int offset = segment->da - qproc->mpss_reloc;
1293 void *ptr = NULL;
Sibi Sankar7dd8ade22018-10-17 19:25:26 +05301294
1295 /* Unlock mba before copying segments */
Bjorn Andersson900fc602020-03-05 01:17:27 +05301296 if (!qproc->dump_mba_loaded) {
Sibi Sankard96f2572020-03-05 01:17:29 +05301297 ret = q6v5_reload_mba(rproc);
Bjorn Andersson900fc602020-03-05 01:17:27 +05301298 if (!ret) {
1299 /* Reset ownership back to Linux to copy segments */
1300 ret = q6v5_xfer_mem_ownership(qproc, &qproc->mpss_perm,
Bjorn Andersson715d8522020-03-05 01:17:28 +05301301 true, false,
Bjorn Andersson900fc602020-03-05 01:17:27 +05301302 qproc->mpss_phys,
1303 qproc->mpss_size);
1304 }
1305 }
Sibi Sankar7dd8ade22018-10-17 19:25:26 +05301306
Sibi Sankarbe050a32020-04-15 12:46:18 +05301307 if (!ret)
Sibi Sankar2ec65062020-11-04 12:33:41 +05301308 ptr = memremap(qproc->mpss_phys + offset + cp_offset, size, MEMREMAP_WC);
Sibi Sankarbe050a32020-04-15 12:46:18 +05301309
1310 if (ptr) {
Rishabh Bhatnagar76abf9c2020-07-16 15:20:33 -07001311 memcpy(dest, ptr, size);
Sibi Sankar2ec65062020-11-04 12:33:41 +05301312 memunmap(ptr);
Sibi Sankarbe050a32020-04-15 12:46:18 +05301313 } else {
Rishabh Bhatnagar76abf9c2020-07-16 15:20:33 -07001314 memset(dest, 0xff, size);
Sibi Sankarbe050a32020-04-15 12:46:18 +05301315 }
Sibi Sankar7dd8ade22018-10-17 19:25:26 +05301316
Rishabh Bhatnagar76abf9c2020-07-16 15:20:33 -07001317 qproc->current_dump_size += size;
Sibi Sankar7dd8ade22018-10-17 19:25:26 +05301318
1319 /* Reclaim mba after copying segments */
Sibi Sankar7ac516d2020-07-16 15:20:32 -07001320 if (qproc->current_dump_size == qproc->total_dump_size) {
Bjorn Andersson900fc602020-03-05 01:17:27 +05301321 if (qproc->dump_mba_loaded) {
1322 /* Try to reset ownership back to Q6 */
1323 q6v5_xfer_mem_ownership(qproc, &qproc->mpss_perm,
Bjorn Andersson715d8522020-03-05 01:17:28 +05301324 false, true,
Bjorn Andersson900fc602020-03-05 01:17:27 +05301325 qproc->mpss_phys,
1326 qproc->mpss_size);
Sibi Sankar7dd8ade22018-10-17 19:25:26 +05301327 q6v5_mba_reclaim(qproc);
Bjorn Andersson900fc602020-03-05 01:17:27 +05301328 }
Sibi Sankar7dd8ade22018-10-17 19:25:26 +05301329 }
1330}
1331
Bjorn Andersson051fb702016-06-20 14:28:41 -07001332static int q6v5_start(struct rproc *rproc)
1333{
1334 struct q6v5 *qproc = (struct q6v5 *)rproc->priv;
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +05301335 int xfermemop_ret;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001336 int ret;
1337
Sibi Sankar03045302018-10-17 19:25:25 +05301338 ret = q6v5_mba_load(qproc);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001339 if (ret)
Sibi Sankar03045302018-10-17 19:25:25 +05301340 return ret;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001341
Sibi Sankarfe6a5dc2020-07-23 01:40:47 +05301342 dev_info(qproc->dev, "MBA booted with%s debug policy, loading mpss\n",
1343 qproc->dp_size ? "" : "out");
Bjorn Andersson051fb702016-06-20 14:28:41 -07001344
1345 ret = q6v5_mpss_load(qproc);
1346 if (ret)
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +05301347 goto reclaim_mpss;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001348
Bjorn Andersson7d674732018-06-04 13:30:38 -07001349 ret = qcom_q6v5_wait_for_start(&qproc->q6v5, msecs_to_jiffies(5000));
1350 if (ret == -ETIMEDOUT) {
Bjorn Andersson051fb702016-06-20 14:28:41 -07001351 dev_err(qproc->dev, "start timed out\n");
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +05301352 goto reclaim_mpss;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001353 }
1354
Bjorn Andersson715d8522020-03-05 01:17:28 +05301355 xfermemop_ret = q6v5_xfer_mem_ownership(qproc, &qproc->mba_perm, true,
1356 false, qproc->mba_phys,
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +05301357 qproc->mba_size);
1358 if (xfermemop_ret)
1359 dev_err(qproc->dev,
1360 "Failed to reclaim mba buffer system may become unstable\n");
Sibi Sankar7dd8ade22018-10-17 19:25:26 +05301361
1362 /* Reset Dump Segment Mask */
Sibi Sankar7ac516d2020-07-16 15:20:32 -07001363 qproc->current_dump_size = 0;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001364
Bjorn Andersson051fb702016-06-20 14:28:41 -07001365 return 0;
1366
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +05301367reclaim_mpss:
Sibi Sankar03045302018-10-17 19:25:25 +05301368 q6v5_mba_reclaim(qproc);
Sibi Sankar318130c2020-07-21 16:59:35 +05301369 q6v5_dump_mba_logs(qproc);
Sibi Sankar663e9842018-05-21 22:57:09 +05301370
Bjorn Andersson051fb702016-06-20 14:28:41 -07001371 return ret;
1372}
1373
1374static int q6v5_stop(struct rproc *rproc)
1375{
1376 struct q6v5 *qproc = (struct q6v5 *)rproc->priv;
1377 int ret;
1378
Bjorn Andersson7d674732018-06-04 13:30:38 -07001379 ret = qcom_q6v5_request_stop(&qproc->q6v5);
1380 if (ret == -ETIMEDOUT)
Bjorn Andersson051fb702016-06-20 14:28:41 -07001381 dev_err(qproc->dev, "timed out on wait\n");
1382
Sibi Sankar03045302018-10-17 19:25:25 +05301383 q6v5_mba_reclaim(qproc);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001384
1385 return 0;
1386}
1387
Sibi Sankarf18b7e92018-10-17 19:25:27 +05301388static int qcom_q6v5_register_dump_segments(struct rproc *rproc,
1389 const struct firmware *mba_fw)
1390{
1391 const struct firmware *fw;
1392 const struct elf32_phdr *phdrs;
1393 const struct elf32_phdr *phdr;
1394 const struct elf32_hdr *ehdr;
1395 struct q6v5 *qproc = rproc->priv;
1396 unsigned long i;
1397 int ret;
1398
Sibi Sankara5a4e022019-01-15 01:20:01 +05301399 ret = request_firmware(&fw, qproc->hexagon_mdt_image, qproc->dev);
Sibi Sankarf18b7e92018-10-17 19:25:27 +05301400 if (ret < 0) {
Sibi Sankara5a4e022019-01-15 01:20:01 +05301401 dev_err(qproc->dev, "unable to load %s\n",
1402 qproc->hexagon_mdt_image);
Sibi Sankarf18b7e92018-10-17 19:25:27 +05301403 return ret;
1404 }
1405
Clement Leger3898fc92020-04-10 12:24:33 +02001406 rproc_coredump_set_elf_info(rproc, ELFCLASS32, EM_NONE);
1407
Sibi Sankarf18b7e92018-10-17 19:25:27 +05301408 ehdr = (struct elf32_hdr *)fw->data;
1409 phdrs = (struct elf32_phdr *)(ehdr + 1);
Sibi Sankar7ac516d2020-07-16 15:20:32 -07001410 qproc->total_dump_size = 0;
Sibi Sankarf18b7e92018-10-17 19:25:27 +05301411
1412 for (i = 0; i < ehdr->e_phnum; i++) {
1413 phdr = &phdrs[i];
1414
1415 if (!q6v5_phdr_valid(phdr))
1416 continue;
1417
1418 ret = rproc_coredump_add_custom_segment(rproc, phdr->p_paddr,
1419 phdr->p_memsz,
1420 qcom_q6v5_dump_segment,
Sibi Sankar7ac516d2020-07-16 15:20:32 -07001421 NULL);
Sibi Sankarf18b7e92018-10-17 19:25:27 +05301422 if (ret)
1423 break;
1424
Sibi Sankar7ac516d2020-07-16 15:20:32 -07001425 qproc->total_dump_size += phdr->p_memsz;
Sibi Sankarf18b7e92018-10-17 19:25:27 +05301426 }
1427
1428 release_firmware(fw);
1429 return ret;
1430}
1431
Bjorn Andersson051fb702016-06-20 14:28:41 -07001432static const struct rproc_ops q6v5_ops = {
1433 .start = q6v5_start,
1434 .stop = q6v5_stop,
Sibi Sankarf18b7e92018-10-17 19:25:27 +05301435 .parse_fw = qcom_q6v5_register_dump_segments,
Bjorn Andersson0f21f9c2018-01-05 15:58:01 -08001436 .load = q6v5_load,
Bjorn Andersson051fb702016-06-20 14:28:41 -07001437};
1438
Bjorn Andersson7d674732018-06-04 13:30:38 -07001439static void qcom_msa_handover(struct qcom_q6v5 *q6v5)
Bjorn Andersson051fb702016-06-20 14:28:41 -07001440{
Bjorn Andersson7d674732018-06-04 13:30:38 -07001441 struct q6v5 *qproc = container_of(q6v5, struct q6v5, q6v5);
Sibi Sankar663e9842018-05-21 22:57:09 +05301442
1443 q6v5_clk_disable(qproc->dev, qproc->proxy_clks,
1444 qproc->proxy_clk_count);
1445 q6v5_regulator_disable(qproc, qproc->proxy_regs,
1446 qproc->proxy_reg_count);
Rajendra Nayak4760a892019-01-30 16:39:30 -08001447 q6v5_pds_disable(qproc, qproc->proxy_pds, qproc->proxy_pd_count);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001448}
1449
1450static int q6v5_init_mem(struct q6v5 *qproc, struct platform_device *pdev)
1451{
1452 struct of_phandle_args args;
1453 struct resource *res;
1454 int ret;
1455
1456 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qdsp6");
1457 qproc->reg_base = devm_ioremap_resource(&pdev->dev, res);
Wei Yongjunb1653f22016-07-14 12:57:44 +00001458 if (IS_ERR(qproc->reg_base))
Bjorn Andersson051fb702016-06-20 14:28:41 -07001459 return PTR_ERR(qproc->reg_base);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001460
1461 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rmb");
1462 qproc->rmb_base = devm_ioremap_resource(&pdev->dev, res);
Wei Yongjunb1653f22016-07-14 12:57:44 +00001463 if (IS_ERR(qproc->rmb_base))
Bjorn Andersson051fb702016-06-20 14:28:41 -07001464 return PTR_ERR(qproc->rmb_base);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001465
1466 ret = of_parse_phandle_with_fixed_args(pdev->dev.of_node,
1467 "qcom,halt-regs", 3, 0, &args);
1468 if (ret < 0) {
1469 dev_err(&pdev->dev, "failed to parse qcom,halt-regs\n");
1470 return -EINVAL;
1471 }
1472
1473 qproc->halt_map = syscon_node_to_regmap(args.np);
1474 of_node_put(args.np);
1475 if (IS_ERR(qproc->halt_map))
1476 return PTR_ERR(qproc->halt_map);
1477
1478 qproc->halt_q6 = args.args[0];
1479 qproc->halt_modem = args.args[1];
1480 qproc->halt_nc = args.args[2];
1481
Sibi Sankara9fdc792020-04-15 20:21:10 +05301482 if (qproc->has_spare_reg) {
Sibi Sankar6439b522019-12-19 11:15:06 +05301483 ret = of_parse_phandle_with_fixed_args(pdev->dev.of_node,
Sibi Sankara9fdc792020-04-15 20:21:10 +05301484 "qcom,spare-regs",
Sibi Sankar6439b522019-12-19 11:15:06 +05301485 1, 0, &args);
1486 if (ret < 0) {
Sibi Sankara9fdc792020-04-15 20:21:10 +05301487 dev_err(&pdev->dev, "failed to parse spare-regs\n");
Sibi Sankar6439b522019-12-19 11:15:06 +05301488 return -EINVAL;
1489 }
1490
1491 qproc->conn_map = syscon_node_to_regmap(args.np);
1492 of_node_put(args.np);
1493 if (IS_ERR(qproc->conn_map))
1494 return PTR_ERR(qproc->conn_map);
1495
1496 qproc->conn_box = args.args[0];
1497 }
1498
Bjorn Andersson051fb702016-06-20 14:28:41 -07001499 return 0;
1500}
1501
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +05301502static int q6v5_init_clocks(struct device *dev, struct clk **clks,
1503 char **clk_names)
Bjorn Andersson051fb702016-06-20 14:28:41 -07001504{
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +05301505 int i;
1506
1507 if (!clk_names)
1508 return 0;
1509
1510 for (i = 0; clk_names[i]; i++) {
1511 clks[i] = devm_clk_get(dev, clk_names[i]);
1512 if (IS_ERR(clks[i])) {
1513 int rc = PTR_ERR(clks[i]);
1514
1515 if (rc != -EPROBE_DEFER)
1516 dev_err(dev, "Failed to get %s clock\n",
1517 clk_names[i]);
1518 return rc;
1519 }
Bjorn Andersson051fb702016-06-20 14:28:41 -07001520 }
1521
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +05301522 return i;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001523}
1524
Rajendra Nayak4760a892019-01-30 16:39:30 -08001525static int q6v5_pds_attach(struct device *dev, struct device **devs,
1526 char **pd_names)
1527{
1528 size_t num_pds = 0;
1529 int ret;
1530 int i;
1531
1532 if (!pd_names)
1533 return 0;
1534
1535 while (pd_names[num_pds])
1536 num_pds++;
1537
1538 for (i = 0; i < num_pds; i++) {
1539 devs[i] = dev_pm_domain_attach_by_name(dev, pd_names[i]);
Sibi Sankarf2583fd2019-08-21 23:35:48 +05301540 if (IS_ERR_OR_NULL(devs[i])) {
1541 ret = PTR_ERR(devs[i]) ? : -ENODATA;
Rajendra Nayak4760a892019-01-30 16:39:30 -08001542 goto unroll_attach;
1543 }
1544 }
1545
1546 return num_pds;
1547
1548unroll_attach:
1549 for (i--; i >= 0; i--)
1550 dev_pm_domain_detach(devs[i], false);
1551
1552 return ret;
Alex Elder58396812020-04-03 12:50:05 -05001553}
Rajendra Nayak4760a892019-01-30 16:39:30 -08001554
1555static void q6v5_pds_detach(struct q6v5 *qproc, struct device **pds,
1556 size_t pd_count)
1557{
1558 int i;
1559
1560 for (i = 0; i < pd_count; i++)
1561 dev_pm_domain_detach(pds[i], false);
1562}
1563
Bjorn Andersson051fb702016-06-20 14:28:41 -07001564static int q6v5_init_reset(struct q6v5 *qproc)
1565{
Philipp Zabel5acbf7e2017-07-19 17:26:16 +02001566 qproc->mss_restart = devm_reset_control_get_exclusive(qproc->dev,
Sibi Sankar9e483ef2018-08-30 00:42:14 +05301567 "mss_restart");
Bjorn Andersson051fb702016-06-20 14:28:41 -07001568 if (IS_ERR(qproc->mss_restart)) {
1569 dev_err(qproc->dev, "failed to acquire mss restart\n");
1570 return PTR_ERR(qproc->mss_restart);
1571 }
1572
Sibi Sankara9fdc792020-04-15 20:21:10 +05301573 if (qproc->has_alt_reset || qproc->has_spare_reg) {
Sibi Sankar29a5f9a2018-08-30 00:42:15 +05301574 qproc->pdc_reset = devm_reset_control_get_exclusive(qproc->dev,
1575 "pdc_reset");
1576 if (IS_ERR(qproc->pdc_reset)) {
1577 dev_err(qproc->dev, "failed to acquire pdc reset\n");
1578 return PTR_ERR(qproc->pdc_reset);
1579 }
1580 }
1581
Bjorn Andersson051fb702016-06-20 14:28:41 -07001582 return 0;
1583}
1584
Bjorn Andersson051fb702016-06-20 14:28:41 -07001585static int q6v5_alloc_memory_region(struct q6v5 *qproc)
1586{
1587 struct device_node *child;
1588 struct device_node *node;
1589 struct resource r;
1590 int ret;
1591
Sibi Sankar6663ce62020-04-21 20:02:25 +05301592 /*
1593 * In the absence of mba/mpss sub-child, extract the mba and mpss
1594 * reserved memory regions from device's memory-region property.
1595 */
Bjorn Andersson051fb702016-06-20 14:28:41 -07001596 child = of_get_child_by_name(qproc->dev->of_node, "mba");
Sibi Sankar6663ce62020-04-21 20:02:25 +05301597 if (!child)
1598 node = of_parse_phandle(qproc->dev->of_node,
1599 "memory-region", 0);
1600 else
1601 node = of_parse_phandle(child, "memory-region", 0);
1602
Bjorn Andersson051fb702016-06-20 14:28:41 -07001603 ret = of_address_to_resource(node, 0, &r);
1604 if (ret) {
1605 dev_err(qproc->dev, "unable to resolve mba region\n");
1606 return ret;
1607 }
Tobias Jordan278d7442018-02-15 16:12:55 +01001608 of_node_put(node);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001609
1610 qproc->mba_phys = r.start;
1611 qproc->mba_size = resource_size(&r);
1612 qproc->mba_region = devm_ioremap_wc(qproc->dev, qproc->mba_phys, qproc->mba_size);
1613 if (!qproc->mba_region) {
1614 dev_err(qproc->dev, "unable to map memory region: %pa+%zx\n",
1615 &r.start, qproc->mba_size);
1616 return -EBUSY;
1617 }
1618
Sibi Sankar6663ce62020-04-21 20:02:25 +05301619 if (!child) {
1620 node = of_parse_phandle(qproc->dev->of_node,
1621 "memory-region", 1);
1622 } else {
1623 child = of_get_child_by_name(qproc->dev->of_node, "mpss");
1624 node = of_parse_phandle(child, "memory-region", 0);
1625 }
1626
Bjorn Andersson051fb702016-06-20 14:28:41 -07001627 ret = of_address_to_resource(node, 0, &r);
1628 if (ret) {
1629 dev_err(qproc->dev, "unable to resolve mpss region\n");
1630 return ret;
1631 }
Tobias Jordan278d7442018-02-15 16:12:55 +01001632 of_node_put(node);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001633
1634 qproc->mpss_phys = qproc->mpss_reloc = r.start;
1635 qproc->mpss_size = resource_size(&r);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001636
1637 return 0;
1638}
1639
1640static int q6v5_probe(struct platform_device *pdev)
1641{
Avaneesh Kumar Dwivedi7a8ffe12016-12-30 19:24:00 +05301642 const struct rproc_hexagon_res *desc;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001643 struct q6v5 *qproc;
1644 struct rproc *rproc;
Sibi Sankara5a4e022019-01-15 01:20:01 +05301645 const char *mba_image;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001646 int ret;
1647
Avaneesh Kumar Dwivedi7a8ffe12016-12-30 19:24:00 +05301648 desc = of_device_get_match_data(&pdev->dev);
1649 if (!desc)
1650 return -EINVAL;
1651
Brian Norrisbbcda302018-10-08 19:08:05 -07001652 if (desc->need_mem_protection && !qcom_scm_is_available())
1653 return -EPROBE_DEFER;
1654
Sibi Sankara5a4e022019-01-15 01:20:01 +05301655 mba_image = desc->hexagon_mba_image;
1656 ret = of_property_read_string_index(pdev->dev.of_node, "firmware-name",
1657 0, &mba_image);
1658 if (ret < 0 && ret != -EINVAL)
1659 return ret;
1660
Bjorn Andersson051fb702016-06-20 14:28:41 -07001661 rproc = rproc_alloc(&pdev->dev, pdev->name, &q6v5_ops,
Sibi Sankara5a4e022019-01-15 01:20:01 +05301662 mba_image, sizeof(*qproc));
Bjorn Andersson051fb702016-06-20 14:28:41 -07001663 if (!rproc) {
1664 dev_err(&pdev->dev, "failed to allocate rproc\n");
1665 return -ENOMEM;
1666 }
1667
Ramon Fried41071022018-05-24 22:21:41 +03001668 rproc->auto_boot = false;
Clement Leger3898fc92020-04-10 12:24:33 +02001669 rproc_coredump_set_elf_info(rproc, ELFCLASS32, EM_NONE);
Ramon Fried41071022018-05-24 22:21:41 +03001670
Bjorn Andersson051fb702016-06-20 14:28:41 -07001671 qproc = (struct q6v5 *)rproc->priv;
1672 qproc->dev = &pdev->dev;
1673 qproc->rproc = rproc;
Sibi Sankara5a4e022019-01-15 01:20:01 +05301674 qproc->hexagon_mdt_image = "modem.mdt";
1675 ret = of_property_read_string_index(pdev->dev.of_node, "firmware-name",
1676 1, &qproc->hexagon_mdt_image);
1677 if (ret < 0 && ret != -EINVAL)
Alex Elder13c060b2020-04-03 12:50:04 -05001678 goto free_rproc;
Sibi Sankara5a4e022019-01-15 01:20:01 +05301679
Bjorn Andersson051fb702016-06-20 14:28:41 -07001680 platform_set_drvdata(pdev, qproc);
1681
Sibi Sankara9fdc792020-04-15 20:21:10 +05301682 qproc->has_spare_reg = desc->has_spare_reg;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001683 ret = q6v5_init_mem(qproc, pdev);
1684 if (ret)
1685 goto free_rproc;
1686
1687 ret = q6v5_alloc_memory_region(qproc);
1688 if (ret)
1689 goto free_rproc;
1690
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +05301691 ret = q6v5_init_clocks(&pdev->dev, qproc->proxy_clks,
1692 desc->proxy_clk_names);
1693 if (ret < 0) {
1694 dev_err(&pdev->dev, "Failed to get proxy clocks.\n");
Bjorn Andersson051fb702016-06-20 14:28:41 -07001695 goto free_rproc;
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +05301696 }
1697 qproc->proxy_clk_count = ret;
1698
Sibi Sankar231f67d2018-05-21 22:57:13 +05301699 ret = q6v5_init_clocks(&pdev->dev, qproc->reset_clks,
1700 desc->reset_clk_names);
1701 if (ret < 0) {
1702 dev_err(&pdev->dev, "Failed to get reset clocks.\n");
1703 goto free_rproc;
1704 }
1705 qproc->reset_clk_count = ret;
1706
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +05301707 ret = q6v5_init_clocks(&pdev->dev, qproc->active_clks,
1708 desc->active_clk_names);
1709 if (ret < 0) {
1710 dev_err(&pdev->dev, "Failed to get active clocks.\n");
1711 goto free_rproc;
1712 }
1713 qproc->active_clk_count = ret;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001714
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +05301715 ret = q6v5_regulator_init(&pdev->dev, qproc->proxy_regs,
1716 desc->proxy_supply);
1717 if (ret < 0) {
1718 dev_err(&pdev->dev, "Failed to get proxy regulators.\n");
Bjorn Andersson051fb702016-06-20 14:28:41 -07001719 goto free_rproc;
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +05301720 }
1721 qproc->proxy_reg_count = ret;
1722
1723 ret = q6v5_regulator_init(&pdev->dev, qproc->active_regs,
1724 desc->active_supply);
1725 if (ret < 0) {
1726 dev_err(&pdev->dev, "Failed to get active regulators.\n");
1727 goto free_rproc;
1728 }
1729 qproc->active_reg_count = ret;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001730
Bjorn Anderssondeb9bb82019-01-30 16:39:31 -08001731 ret = q6v5_pds_attach(&pdev->dev, qproc->active_pds,
1732 desc->active_pd_names);
1733 if (ret < 0) {
1734 dev_err(&pdev->dev, "Failed to attach active power domains\n");
1735 goto free_rproc;
1736 }
1737 qproc->active_pd_count = ret;
1738
Rajendra Nayak4760a892019-01-30 16:39:30 -08001739 ret = q6v5_pds_attach(&pdev->dev, qproc->proxy_pds,
1740 desc->proxy_pd_names);
1741 if (ret < 0) {
1742 dev_err(&pdev->dev, "Failed to init power domains\n");
Bjorn Anderssondeb9bb82019-01-30 16:39:31 -08001743 goto detach_active_pds;
Rajendra Nayak4760a892019-01-30 16:39:30 -08001744 }
1745 qproc->proxy_pd_count = ret;
1746
Sibi Sankar29a5f9a2018-08-30 00:42:15 +05301747 qproc->has_alt_reset = desc->has_alt_reset;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001748 ret = q6v5_init_reset(qproc);
1749 if (ret)
Rajendra Nayak4760a892019-01-30 16:39:30 -08001750 goto detach_proxy_pds;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001751
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +05301752 qproc->version = desc->version;
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +05301753 qproc->need_mem_protection = desc->need_mem_protection;
Sibi Sankar318130c2020-07-21 16:59:35 +05301754 qproc->has_mba_logs = desc->has_mba_logs;
Bjorn Andersson7d674732018-06-04 13:30:38 -07001755
1756 ret = qcom_q6v5_init(&qproc->q6v5, pdev, rproc, MPSS_CRASH_REASON_SMEM,
1757 qcom_msa_handover);
1758 if (ret)
Rajendra Nayak4760a892019-01-30 16:39:30 -08001759 goto detach_proxy_pds;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001760
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +05301761 qproc->mpss_perm = BIT(QCOM_SCM_VMID_HLOS);
1762 qproc->mba_perm = BIT(QCOM_SCM_VMID_HLOS);
Bjorn Anderssoncd9fc8f2020-04-22 17:37:33 -07001763 qcom_add_glink_subdev(rproc, &qproc->glink_subdev, "mpss");
Bjorn Andersson4b489212017-01-29 14:05:50 -08001764 qcom_add_smd_subdev(rproc, &qproc->smd_subdev);
Bjorn Andersson1e140df2017-07-24 22:56:43 -07001765 qcom_add_ssr_subdev(rproc, &qproc->ssr_subdev, "mpss");
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -07001766 qproc->sysmon = qcom_add_sysmon_subdev(rproc, "modem", 0x12);
Sibi Sankar027045a2019-01-08 15:53:43 +05301767 if (IS_ERR(qproc->sysmon)) {
1768 ret = PTR_ERR(qproc->sysmon);
Alex Elder58396812020-04-03 12:50:05 -05001769 goto remove_subdevs;
Sibi Sankar027045a2019-01-08 15:53:43 +05301770 }
Bjorn Andersson4b489212017-01-29 14:05:50 -08001771
Bjorn Andersson051fb702016-06-20 14:28:41 -07001772 ret = rproc_add(rproc);
1773 if (ret)
Alex Elder58396812020-04-03 12:50:05 -05001774 goto remove_sysmon_subdev;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001775
1776 return 0;
1777
Alex Elder58396812020-04-03 12:50:05 -05001778remove_sysmon_subdev:
1779 qcom_remove_sysmon_subdev(qproc->sysmon);
1780remove_subdevs:
Alex Elder58396812020-04-03 12:50:05 -05001781 qcom_remove_ssr_subdev(rproc, &qproc->ssr_subdev);
1782 qcom_remove_smd_subdev(rproc, &qproc->smd_subdev);
1783 qcom_remove_glink_subdev(rproc, &qproc->glink_subdev);
1784detach_proxy_pds:
Rajendra Nayak4760a892019-01-30 16:39:30 -08001785 q6v5_pds_detach(qproc, qproc->proxy_pds, qproc->proxy_pd_count);
Bjorn Anderssondeb9bb82019-01-30 16:39:31 -08001786detach_active_pds:
1787 q6v5_pds_detach(qproc, qproc->active_pds, qproc->active_pd_count);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001788free_rproc:
Bjorn Andersson433c0e02016-10-02 17:46:38 -07001789 rproc_free(rproc);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001790
1791 return ret;
1792}
1793
1794static int q6v5_remove(struct platform_device *pdev)
1795{
1796 struct q6v5 *qproc = platform_get_drvdata(pdev);
Alex Elder58396812020-04-03 12:50:05 -05001797 struct rproc *rproc = qproc->rproc;
Bjorn Andersson051fb702016-06-20 14:28:41 -07001798
Alex Elder58396812020-04-03 12:50:05 -05001799 rproc_del(rproc);
Bjorn Andersson4b489212017-01-29 14:05:50 -08001800
Bjorn Andersson1fb82ee2017-08-27 21:51:38 -07001801 qcom_remove_sysmon_subdev(qproc->sysmon);
Alex Elder58396812020-04-03 12:50:05 -05001802 qcom_remove_ssr_subdev(rproc, &qproc->ssr_subdev);
1803 qcom_remove_smd_subdev(rproc, &qproc->smd_subdev);
1804 qcom_remove_glink_subdev(rproc, &qproc->glink_subdev);
Rajendra Nayak4760a892019-01-30 16:39:30 -08001805
1806 q6v5_pds_detach(qproc, qproc->proxy_pds, qproc->proxy_pd_count);
Alex Elder58396812020-04-03 12:50:05 -05001807 q6v5_pds_detach(qproc, qproc->active_pds, qproc->active_pd_count);
Rajendra Nayak4760a892019-01-30 16:39:30 -08001808
Alex Elder58396812020-04-03 12:50:05 -05001809 rproc_free(rproc);
Bjorn Andersson051fb702016-06-20 14:28:41 -07001810
1811 return 0;
1812}
1813
Sibi Sankar6439b522019-12-19 11:15:06 +05301814static const struct rproc_hexagon_res sc7180_mss = {
1815 .hexagon_mba_image = "mba.mbn",
1816 .proxy_clk_names = (char*[]){
1817 "xo",
1818 NULL
1819 },
1820 .reset_clk_names = (char*[]){
1821 "iface",
1822 "bus",
1823 "snoc_axi",
1824 NULL
1825 },
1826 .active_clk_names = (char*[]){
1827 "mnoc_axi",
1828 "nav",
Sibi Sankar6439b522019-12-19 11:15:06 +05301829 NULL
1830 },
1831 .active_pd_names = (char*[]){
1832 "load_state",
1833 NULL
1834 },
1835 .proxy_pd_names = (char*[]){
1836 "cx",
1837 "mx",
1838 "mss",
1839 NULL
1840 },
1841 .need_mem_protection = true,
1842 .has_alt_reset = false,
Sibi Sankar318130c2020-07-21 16:59:35 +05301843 .has_mba_logs = true,
Sibi Sankara9fdc792020-04-15 20:21:10 +05301844 .has_spare_reg = true,
Sibi Sankar6439b522019-12-19 11:15:06 +05301845 .version = MSS_SC7180,
1846};
1847
Sibi Sankar231f67d2018-05-21 22:57:13 +05301848static const struct rproc_hexagon_res sdm845_mss = {
1849 .hexagon_mba_image = "mba.mbn",
1850 .proxy_clk_names = (char*[]){
1851 "xo",
Sibi Sankar231f67d2018-05-21 22:57:13 +05301852 "prng",
1853 NULL
1854 },
1855 .reset_clk_names = (char*[]){
1856 "iface",
1857 "snoc_axi",
1858 NULL
1859 },
1860 .active_clk_names = (char*[]){
1861 "bus",
1862 "mem",
1863 "gpll0_mss",
1864 "mnoc_axi",
1865 NULL
1866 },
Bjorn Anderssondeb9bb82019-01-30 16:39:31 -08001867 .active_pd_names = (char*[]){
1868 "load_state",
1869 NULL
1870 },
Rajendra Nayak4760a892019-01-30 16:39:30 -08001871 .proxy_pd_names = (char*[]){
1872 "cx",
1873 "mx",
1874 "mss",
1875 NULL
1876 },
Sibi Sankar231f67d2018-05-21 22:57:13 +05301877 .need_mem_protection = true,
1878 .has_alt_reset = true,
Sibi Sankar318130c2020-07-21 16:59:35 +05301879 .has_mba_logs = false,
Sibi Sankara9fdc792020-04-15 20:21:10 +05301880 .has_spare_reg = false,
Sibi Sankar231f67d2018-05-21 22:57:13 +05301881 .version = MSS_SDM845,
1882};
1883
Jeffrey Hugo1665cbd2019-10-31 19:45:01 -07001884static const struct rproc_hexagon_res msm8998_mss = {
1885 .hexagon_mba_image = "mba.mbn",
1886 .proxy_clk_names = (char*[]){
1887 "xo",
1888 "qdss",
1889 "mem",
1890 NULL
1891 },
1892 .active_clk_names = (char*[]){
1893 "iface",
1894 "bus",
Jeffrey Hugo1665cbd2019-10-31 19:45:01 -07001895 "gpll0_mss",
1896 "mnoc_axi",
1897 "snoc_axi",
1898 NULL
1899 },
1900 .proxy_pd_names = (char*[]){
1901 "cx",
1902 "mx",
1903 NULL
1904 },
1905 .need_mem_protection = true,
1906 .has_alt_reset = false,
Sibi Sankar318130c2020-07-21 16:59:35 +05301907 .has_mba_logs = false,
Sibi Sankara9fdc792020-04-15 20:21:10 +05301908 .has_spare_reg = false,
Jeffrey Hugo1665cbd2019-10-31 19:45:01 -07001909 .version = MSS_MSM8998,
1910};
1911
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +05301912static const struct rproc_hexagon_res msm8996_mss = {
1913 .hexagon_mba_image = "mba.mbn",
Sibi Sankar47b87472018-12-29 00:23:05 +05301914 .proxy_supply = (struct qcom_mss_reg_res[]) {
1915 {
1916 .supply = "pll",
1917 .uA = 100000,
1918 },
1919 {}
1920 },
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +05301921 .proxy_clk_names = (char*[]){
1922 "xo",
1923 "pnoc",
Sibi Sankar80ec4192018-12-29 00:23:03 +05301924 "qdss",
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +05301925 NULL
1926 },
1927 .active_clk_names = (char*[]){
1928 "iface",
1929 "bus",
1930 "mem",
Sibi Sankar80ec4192018-12-29 00:23:03 +05301931 "gpll0_mss",
1932 "snoc_axi",
1933 "mnoc_axi",
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +05301934 NULL
1935 },
1936 .need_mem_protection = true,
Sibi Sankar231f67d2018-05-21 22:57:13 +05301937 .has_alt_reset = false,
Sibi Sankar318130c2020-07-21 16:59:35 +05301938 .has_mba_logs = false,
Sibi Sankara9fdc792020-04-15 20:21:10 +05301939 .has_spare_reg = false,
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +05301940 .version = MSS_MSM8996,
1941};
1942
Avaneesh Kumar Dwivedi7a8ffe12016-12-30 19:24:00 +05301943static const struct rproc_hexagon_res msm8916_mss = {
1944 .hexagon_mba_image = "mba.mbn",
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +05301945 .proxy_supply = (struct qcom_mss_reg_res[]) {
1946 {
1947 .supply = "mx",
1948 .uV = 1050000,
1949 },
1950 {
1951 .supply = "cx",
1952 .uA = 100000,
1953 },
1954 {
1955 .supply = "pll",
1956 .uA = 100000,
1957 },
1958 {}
1959 },
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +05301960 .proxy_clk_names = (char*[]){
1961 "xo",
1962 NULL
1963 },
1964 .active_clk_names = (char*[]){
1965 "iface",
1966 "bus",
1967 "mem",
1968 NULL
1969 },
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +05301970 .need_mem_protection = false,
Sibi Sankar231f67d2018-05-21 22:57:13 +05301971 .has_alt_reset = false,
Sibi Sankar318130c2020-07-21 16:59:35 +05301972 .has_mba_logs = false,
Sibi Sankara9fdc792020-04-15 20:21:10 +05301973 .has_spare_reg = false,
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +05301974 .version = MSS_MSM8916,
Avaneesh Kumar Dwivedi7a8ffe12016-12-30 19:24:00 +05301975};
1976
1977static const struct rproc_hexagon_res msm8974_mss = {
1978 .hexagon_mba_image = "mba.b00",
Avaneesh Kumar Dwivedi19f902b2016-12-30 19:24:02 +05301979 .proxy_supply = (struct qcom_mss_reg_res[]) {
1980 {
1981 .supply = "mx",
1982 .uV = 1050000,
1983 },
1984 {
1985 .supply = "cx",
1986 .uA = 100000,
1987 },
1988 {
1989 .supply = "pll",
1990 .uA = 100000,
1991 },
1992 {}
1993 },
1994 .active_supply = (struct qcom_mss_reg_res[]) {
1995 {
1996 .supply = "mss",
1997 .uV = 1050000,
1998 .uA = 100000,
1999 },
2000 {}
2001 },
Avaneesh Kumar Dwivedi39b24102016-12-30 19:24:01 +05302002 .proxy_clk_names = (char*[]){
2003 "xo",
2004 NULL
2005 },
2006 .active_clk_names = (char*[]){
2007 "iface",
2008 "bus",
2009 "mem",
2010 NULL
2011 },
Avaneesh Kumar Dwivedi6c5a9dc2017-10-24 21:22:26 +05302012 .need_mem_protection = false,
Sibi Sankar231f67d2018-05-21 22:57:13 +05302013 .has_alt_reset = false,
Sibi Sankar318130c2020-07-21 16:59:35 +05302014 .has_mba_logs = false,
Sibi Sankara9fdc792020-04-15 20:21:10 +05302015 .has_spare_reg = false,
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +05302016 .version = MSS_MSM8974,
Avaneesh Kumar Dwivedi7a8ffe12016-12-30 19:24:00 +05302017};
2018
Bjorn Andersson051fb702016-06-20 14:28:41 -07002019static const struct of_device_id q6v5_of_match[] = {
Avaneesh Kumar Dwivedi7a8ffe12016-12-30 19:24:00 +05302020 { .compatible = "qcom,q6v5-pil", .data = &msm8916_mss},
2021 { .compatible = "qcom,msm8916-mss-pil", .data = &msm8916_mss},
2022 { .compatible = "qcom,msm8974-mss-pil", .data = &msm8974_mss},
Avaneesh Kumar Dwivedi9f058fa2017-10-24 21:22:27 +05302023 { .compatible = "qcom,msm8996-mss-pil", .data = &msm8996_mss},
Jeffrey Hugo1665cbd2019-10-31 19:45:01 -07002024 { .compatible = "qcom,msm8998-mss-pil", .data = &msm8998_mss},
Sibi Sankar6439b522019-12-19 11:15:06 +05302025 { .compatible = "qcom,sc7180-mss-pil", .data = &sc7180_mss},
Sibi Sankar231f67d2018-05-21 22:57:13 +05302026 { .compatible = "qcom,sdm845-mss-pil", .data = &sdm845_mss},
Bjorn Andersson051fb702016-06-20 14:28:41 -07002027 { },
2028};
Javier Martinez Canillas3227c872016-10-18 18:24:19 -03002029MODULE_DEVICE_TABLE(of, q6v5_of_match);
Bjorn Andersson051fb702016-06-20 14:28:41 -07002030
2031static struct platform_driver q6v5_driver = {
2032 .probe = q6v5_probe,
2033 .remove = q6v5_remove,
2034 .driver = {
Bjorn Anderssonef73c222018-09-24 16:45:26 -07002035 .name = "qcom-q6v5-mss",
Bjorn Andersson051fb702016-06-20 14:28:41 -07002036 .of_match_table = q6v5_of_match,
2037 },
2038};
2039module_platform_driver(q6v5_driver);
2040
Bjorn Anderssonef73c222018-09-24 16:45:26 -07002041MODULE_DESCRIPTION("Qualcomm Self-authenticating modem remoteproc driver");
Bjorn Andersson051fb702016-06-20 14:28:41 -07002042MODULE_LICENSE("GPL v2");