xref: /rk3399_ARM-atf/plat/nvidia/tegra/soc/t210/plat_psci_handlers.c (revision da0f47437506b437a7729c22f95b9e351401d4af)
1 /*
2  * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <cortex_a57.h>
9 #include <arch_helpers.h>
10 #include <common/debug.h>
11 #include <drivers/delay_timer.h>
12 #include <lib/mmio.h>
13 #include <lib/psci/psci.h>
14 #include <plat/common/platform.h>
15 
16 #include <bpmp.h>
17 #include <flowctrl.h>
18 #include <memctrl.h>
19 #include <pmc.h>
20 #include <platform_def.h>
21 #include <security_engine.h>
22 #include <tegra_def.h>
23 #include <tegra_private.h>
24 #include <tegra_platform.h>
25 #include <utils.h>
26 
27 /*
28  * Register used to clear CPU reset signals. Each CPU has two reset
29  * signals: CPU reset (3:0) and Core reset (19:16).
30  */
31 #define CPU_CMPLX_RESET_CLR		0x454
32 #define CPU_CORE_RESET_MASK		0x10001
33 
34 /* Clock and Reset controller registers for system clock's settings */
35 #define SCLK_RATE			0x30
36 #define SCLK_BURST_POLICY		0x28
37 #define SCLK_BURST_POLICY_DEFAULT	0x10000000
38 
39 static int cpu_powergate_mask[PLATFORM_MAX_CPUS_PER_CLUSTER];
40 static bool tegra_bpmp_available = true;
41 
42 int32_t tegra_soc_validate_power_state(unsigned int power_state,
43 					psci_power_state_t *req_state)
44 {
45 	int state_id = psci_get_pstate_id(power_state);
46 	const plat_params_from_bl2_t *plat_params = bl31_get_plat_params();
47 
48 	/* Sanity check the requested state id */
49 	switch (state_id) {
50 	case PSTATE_ID_CORE_POWERDN:
51 		/*
52 		 * Core powerdown request only for afflvl 0
53 		 */
54 		req_state->pwr_domain_state[MPIDR_AFFLVL0] = state_id & 0xff;
55 
56 		break;
57 
58 	case PSTATE_ID_CLUSTER_IDLE:
59 
60 		/*
61 		 * Cluster idle request for afflvl 0
62 		 */
63 		req_state->pwr_domain_state[MPIDR_AFFLVL0] = PSTATE_ID_CORE_POWERDN;
64 		req_state->pwr_domain_state[MPIDR_AFFLVL1] = state_id;
65 		break;
66 
67 	case PSTATE_ID_SOC_POWERDN:
68 
69 		/*
70 		 * sc7entry-fw must be present in the system when the bpmp
71 		 * firmware is not present, for a successful System Suspend
72 		 * entry.
73 		 */
74 		if (!tegra_bpmp_init() && !plat_params->sc7entry_fw_base)
75 			return PSCI_E_NOT_SUPPORTED;
76 
77 		/*
78 		 * System powerdown request only for afflvl 2
79 		 */
80 		for (uint32_t i = MPIDR_AFFLVL0; i < PLAT_MAX_PWR_LVL; i++)
81 			req_state->pwr_domain_state[i] = PLAT_MAX_OFF_STATE;
82 
83 		req_state->pwr_domain_state[PLAT_MAX_PWR_LVL] =
84 			PLAT_SYS_SUSPEND_STATE_ID;
85 
86 		break;
87 
88 	default:
89 		ERROR("%s: unsupported state id (%d)\n", __func__, state_id);
90 		return PSCI_E_INVALID_PARAMS;
91 	}
92 
93 	return PSCI_E_SUCCESS;
94 }
95 
96 /*******************************************************************************
97  * Platform handler to calculate the proper target power level at the
98  * specified affinity level.
99  ******************************************************************************/
100 plat_local_state_t tegra_soc_get_target_pwr_state(unsigned int lvl,
101 					     const plat_local_state_t *states,
102 					     unsigned int ncpu)
103 {
104 	plat_local_state_t target = PSCI_LOCAL_STATE_RUN;
105 	int cpu = plat_my_core_pos();
106 	int core_pos = read_mpidr() & MPIDR_CPU_MASK;
107 	uint32_t bpmp_reply, data[3], val;
108 	int ret;
109 
110 	/* get the power state at this level */
111 	if (lvl == MPIDR_AFFLVL1)
112 		target = *(states + core_pos);
113 	if (lvl == MPIDR_AFFLVL2)
114 		target = *(states + cpu);
115 
116 	if ((lvl == MPIDR_AFFLVL1) && (target == PSTATE_ID_CLUSTER_IDLE)) {
117 
118 		/* initialize the bpmp interface */
119 		ret = tegra_bpmp_init();
120 		if (ret != 0U) {
121 
122 			/* Cluster idle not allowed */
123 			target = PSCI_LOCAL_STATE_RUN;
124 
125 			/*******************************************
126 			 * BPMP is not present, so handle CC6 entry
127 			 * from the CPU
128 			 ******************************************/
129 
130 			/* check if cluster idle state has been enabled */
131 			val = mmio_read_32(TEGRA_CL_DVFS_BASE + DVFS_DFLL_CTRL);
132 			if (val == ENABLE_CLOSED_LOOP) {
133 				/*
134 				 * flag to indicate that BPMP firmware is not
135 				 * available and the CPU has to handle entry/exit
136 				 * for all power states
137 				 */
138 				tegra_bpmp_available = false;
139 
140 				/*
141 				 * Acquire the cluster idle lock to stop
142 				 * other CPUs from powering up.
143 				 */
144 				tegra_fc_ccplex_pgexit_lock();
145 
146 				/* Cluster idle only from the last standing CPU */
147 				if (tegra_pmc_is_last_on_cpu() && tegra_fc_is_ccx_allowed()) {
148 					/* Cluster idle allowed */
149 					target = PSTATE_ID_CLUSTER_IDLE;
150 				} else {
151 					/* release cluster idle lock */
152 					tegra_fc_ccplex_pgexit_unlock();
153 				}
154 			}
155 		} else {
156 
157 			/* Cluster power-down */
158 			data[0] = (uint32_t)cpu;
159 			data[1] = TEGRA_PM_CC6;
160 			data[2] = TEGRA_PM_SC1;
161 			ret = tegra_bpmp_send_receive_atomic(MRQ_DO_IDLE,
162 					(void *)&data, (int)sizeof(data),
163 					(void *)&bpmp_reply,
164 					(int)sizeof(bpmp_reply));
165 
166 			/* check if cluster power down is allowed */
167 			if ((ret != 0L) || (bpmp_reply != BPMP_CCx_ALLOWED)) {
168 
169 				/* Cluster power down not allowed */
170 				target = PSCI_LOCAL_STATE_RUN;
171 			}
172 		}
173 
174 	} else if (((lvl == MPIDR_AFFLVL2) || (lvl == MPIDR_AFFLVL1)) &&
175 	    (target == PSTATE_ID_SOC_POWERDN)) {
176 
177 		/* System Suspend */
178 		target = PSTATE_ID_SOC_POWERDN;
179 
180 	} else {
181 		; /* do nothing */
182 	}
183 
184 	return target;
185 }
186 
187 int tegra_soc_pwr_domain_suspend(const psci_power_state_t *target_state)
188 {
189 	u_register_t mpidr = read_mpidr();
190 	const plat_local_state_t *pwr_domain_state =
191 		target_state->pwr_domain_state;
192 	unsigned int stateid_afflvl2 = pwr_domain_state[MPIDR_AFFLVL2];
193 	unsigned int stateid_afflvl1 = pwr_domain_state[MPIDR_AFFLVL1];
194 	unsigned int stateid_afflvl0 = pwr_domain_state[MPIDR_AFFLVL0];
195 	uint32_t cfg;
196 	int ret = PSCI_E_SUCCESS;
197 	uint32_t val;
198 
199 	if (stateid_afflvl2 == PSTATE_ID_SOC_POWERDN) {
200 
201 		assert((stateid_afflvl0 == PLAT_MAX_OFF_STATE) ||
202 			(stateid_afflvl0 == PSTATE_ID_SOC_POWERDN));
203 		assert((stateid_afflvl1 == PLAT_MAX_OFF_STATE) ||
204 			(stateid_afflvl1 == PSTATE_ID_SOC_POWERDN));
205 
206 		if (tegra_chipid_is_t210_b01()) {
207 
208 			/* Suspend se/se2 and pka1 */
209 			if (tegra_se_suspend() != 0) {
210 				ret = PSCI_E_INTERN_FAIL;
211 			}
212 		}
213 
214 	} else if (stateid_afflvl1 == PSTATE_ID_CLUSTER_IDLE) {
215 
216 		assert(stateid_afflvl0 == PSTATE_ID_CORE_POWERDN);
217 
218 		if (!tegra_bpmp_available) {
219 
220 			/* Find if the platform uses OVR2/MAX77621 PMIC */
221 			cfg = mmio_read_32(TEGRA_CL_DVFS_BASE + DVFS_DFLL_OUTPUT_CFG);
222 			if (cfg & DFLL_OUTPUT_CFG_CLK_EN_BIT) {
223 				/* OVR2 */
224 
225 				/* PWM tristate */
226 				val = mmio_read_32(TEGRA_MISC_BASE + PINMUX_AUX_DVFS_PWM);
227 				val |= PINMUX_PWM_TRISTATE;
228 				mmio_write_32(TEGRA_MISC_BASE + PINMUX_AUX_DVFS_PWM, val);
229 
230 				/*
231 				 * SCRATCH201[1] is being used to identify CPU
232 				 * PMIC in warmboot code.
233 				 * 0 : OVR2
234 				 * 1 : MAX77621
235 				 */
236 				tegra_pmc_write_32(PMC_SCRATCH201, 0x0);
237 			} else {
238 				/* MAX77621 */
239 				tegra_pmc_write_32(PMC_SCRATCH201, 0x2);
240 			}
241 		}
242 
243 		/* Prepare for cluster idle */
244 		tegra_fc_cluster_idle(mpidr);
245 
246 	} else if (stateid_afflvl0 == PSTATE_ID_CORE_POWERDN) {
247 
248 		/* Prepare for cpu powerdn */
249 		tegra_fc_cpu_powerdn(mpidr);
250 
251 	} else {
252 		ERROR("%s: Unknown state id (%d, %d, %d)\n", __func__,
253 			stateid_afflvl2, stateid_afflvl1, stateid_afflvl0);
254 		ret = PSCI_E_NOT_SUPPORTED;
255 	}
256 
257 	return ret;
258 }
259 
260 static void tegra_reset_all_dma_masters(void)
261 {
262 	uint32_t val, mask;
263 
264 	/*
265 	 * Reset all possible DMA masters in the system.
266 	 */
267 	val = GPU_RESET_BIT;
268 	mmio_write_32(TEGRA_CAR_RESET_BASE + TEGRA_GPU_RESET_REG_OFFSET, val);
269 
270 	val = NVENC_RESET_BIT | TSECB_RESET_BIT | APE_RESET_BIT |
271 	      NVJPG_RESET_BIT | NVDEC_RESET_BIT;
272 	mmio_write_32(TEGRA_CAR_RESET_BASE + TEGRA_RST_DEV_SET_Y, val);
273 
274 	val = HOST1X_RESET_BIT | ISP_RESET_BIT | USBD_RESET_BIT |
275 	      VI_RESET_BIT | SDMMC4_RESET_BIT | SDMMC1_RESET_BIT |
276 	      SDMMC2_RESET_BIT;
277 	mmio_write_32(TEGRA_CAR_RESET_BASE + TEGRA_RST_DEV_SET_L, val);
278 
279 	val = USB2_RESET_BIT | APBDMA_RESET_BIT | AHBDMA_RESET_BIT;
280 	mmio_write_32(TEGRA_CAR_RESET_BASE + TEGRA_RST_DEV_SET_H, val);
281 
282 	val = XUSB_DEV_RESET_BIT | XUSB_HOST_RESET_BIT | TSEC_RESET_BIT |
283 	      PCIE_RESET_BIT | SDMMC3_RESET_BIT;
284 	mmio_write_32(TEGRA_CAR_RESET_BASE + TEGRA_RST_DEV_SET_U, val);
285 
286 	val = SE_RESET_BIT | HDA_RESET_BIT | SATA_RESET_BIT;
287 	mmio_write_32(TEGRA_CAR_RESET_BASE + TEGRA_RST_DEV_SET_V, val);
288 
289 	/*
290 	 * If any of the DMA masters are still alive, assume
291 	 * that the system has been compromised and reboot.
292 	 */
293 	val = mmio_read_32(TEGRA_CAR_RESET_BASE + TEGRA_GPU_RESET_REG_OFFSET);
294 	mask = GPU_RESET_BIT;
295 	if ((val & mask) != mask)
296 		tegra_pmc_system_reset();
297 
298 	mask = NVENC_RESET_BIT | TSECB_RESET_BIT | APE_RESET_BIT |
299 	      NVJPG_RESET_BIT | NVDEC_RESET_BIT;
300 	val = mmio_read_32(TEGRA_CAR_RESET_BASE + TEGRA_RST_DEV_SET_Y);
301 	if ((val & mask) != mask)
302 		tegra_pmc_system_reset();
303 
304 	mask = HOST1X_RESET_BIT | ISP_RESET_BIT | USBD_RESET_BIT |
305 	       VI_RESET_BIT | SDMMC4_RESET_BIT | SDMMC1_RESET_BIT |
306 	       SDMMC2_RESET_BIT;
307 	val = mmio_read_32(TEGRA_CAR_RESET_BASE + TEGRA_RST_DEV_SET_L);
308 	if ((val & mask) != mask)
309 		tegra_pmc_system_reset();
310 
311 	mask = USB2_RESET_BIT | APBDMA_RESET_BIT | AHBDMA_RESET_BIT;
312 	val = mmio_read_32(TEGRA_CAR_RESET_BASE + TEGRA_RST_DEV_SET_H);
313 	if ((val & mask) != mask)
314 		tegra_pmc_system_reset();
315 
316 	mask = XUSB_DEV_RESET_BIT | XUSB_HOST_RESET_BIT | TSEC_RESET_BIT |
317 	       PCIE_RESET_BIT | SDMMC3_RESET_BIT;
318 	val = mmio_read_32(TEGRA_CAR_RESET_BASE + TEGRA_RST_DEV_SET_U);
319 	if ((val & mask) != mask)
320 		tegra_pmc_system_reset();
321 
322 	val = mmio_read_32(TEGRA_CAR_RESET_BASE + TEGRA_RST_DEV_SET_V);
323 	mask = SE_RESET_BIT | HDA_RESET_BIT | SATA_RESET_BIT;
324 	if ((val & mask) != mask)
325 		tegra_pmc_system_reset();
326 }
327 
328 int tegra_soc_pwr_domain_power_down_wfi(const psci_power_state_t *target_state)
329 {
330 	u_register_t mpidr = read_mpidr();
331 	const plat_local_state_t *pwr_domain_state =
332 		target_state->pwr_domain_state;
333 	unsigned int stateid_afflvl2 = pwr_domain_state[PLAT_MAX_PWR_LVL];
334 	const plat_params_from_bl2_t *plat_params = bl31_get_plat_params();
335 	uint32_t val;
336 
337 	if (stateid_afflvl2 == PSTATE_ID_SOC_POWERDN) {
338 
339 		if (tegra_chipid_is_t210_b01()) {
340 			/* Save tzram contents */
341 			tegra_se_save_tzram();
342 		}
343 
344 		/* de-init the interface */
345 		tegra_bpmp_suspend();
346 
347 		/*
348 		 * The CPU needs to load the System suspend entry firmware
349 		 * if nothing is running on the BPMP.
350 		 */
351 		if (!tegra_bpmp_available) {
352 
353 			/*
354 			 * BPMP firmware is not running on the co-processor, so
355 			 * we need to explicitly load the firmware to enable
356 			 * entry/exit to/from System Suspend and set the BPMP
357 			 * on its way.
358 			 */
359 
360 			/* Power off BPMP before we proceed */
361 			tegra_fc_bpmp_off();
362 
363 			/* bond out IRAM banks B, C and D */
364 			mmio_write_32(TEGRA_CAR_RESET_BASE + TEGRA_BOND_OUT_U,
365 				IRAM_B_LOCK_BIT | IRAM_C_LOCK_BIT |
366 				IRAM_D_LOCK_BIT);
367 
368 			/* bond out APB/AHB DMAs */
369 			mmio_write_32(TEGRA_CAR_RESET_BASE + TEGRA_BOND_OUT_H,
370 				APB_DMA_LOCK_BIT | AHB_DMA_LOCK_BIT);
371 
372 			/* Power off BPMP before we proceed */
373 			tegra_fc_bpmp_off();
374 
375 			/*
376 			 * Reset all the hardware blocks that can act as DMA
377 			 * masters on the bus.
378 			 */
379 			tegra_reset_all_dma_masters();
380 
381 			/* clean up IRAM of any cruft */
382 			zeromem((void *)(uintptr_t)TEGRA_IRAM_BASE,
383 					TEGRA_IRAM_A_SIZE);
384 
385 			/* Copy the firmware to BPMP's internal RAM */
386 			(void)memcpy((void *)(uintptr_t)TEGRA_IRAM_BASE,
387 				(const void *)(plat_params->sc7entry_fw_base + SC7ENTRY_FW_HEADER_SIZE_BYTES),
388 				plat_params->sc7entry_fw_size - SC7ENTRY_FW_HEADER_SIZE_BYTES);
389 
390 			/* Power on the BPMP and execute from IRAM base */
391 			tegra_fc_bpmp_on(TEGRA_IRAM_BASE);
392 
393 			/* Wait until BPMP powers up */
394 			do {
395 				val = mmio_read_32(TEGRA_RES_SEMA_BASE + STA_OFFSET);
396 			} while (val != SIGN_OF_LIFE);
397 		}
398 
399 		/* enter system suspend */
400 		tegra_fc_soc_powerdn(mpidr);
401 	}
402 
403 	return PSCI_E_SUCCESS;
404 }
405 
406 int tegra_soc_pwr_domain_on_finish(const psci_power_state_t *target_state)
407 {
408 	const plat_params_from_bl2_t *plat_params = bl31_get_plat_params();
409 	uint32_t cfg;
410 	uint32_t val, entrypoint = 0;
411 	uint64_t offset;
412 
413 	/* platform parameter passed by the previous bootloader */
414 	if (plat_params->l2_ecc_parity_prot_dis != 1) {
415 		/* Enable ECC Parity Protection for Cortex-A57 CPUs */
416 		val = read_l2ctlr_el1();
417 		val |= (uint64_t)CORTEX_A57_L2_ECC_PARITY_PROTECTION_BIT;
418 		write_l2ctlr_el1(val);
419 	}
420 
421 	/*
422 	 * Check if we are exiting from SOC_POWERDN.
423 	 */
424 	if (target_state->pwr_domain_state[PLAT_MAX_PWR_LVL] ==
425 			PLAT_SYS_SUSPEND_STATE_ID) {
426 
427 		/*
428 		 * Security engine resume
429 		 */
430 		if (tegra_chipid_is_t210_b01()) {
431 			tegra_se_resume();
432 		}
433 
434 		/*
435 		 * Lock scratch registers which hold the CPU vectors
436 		 */
437 		tegra_pmc_lock_cpu_vectors();
438 
439 		/*
440 		 * Enable WRAP to INCR burst type conversions for
441 		 * incoming requests on the AXI slave ports.
442 		 */
443 		val = mmio_read_32(TEGRA_MSELECT_BASE + MSELECT_CONFIG);
444 		val &= ~ENABLE_UNSUP_TX_ERRORS;
445 		val |= ENABLE_WRAP_TO_INCR_BURSTS;
446 		mmio_write_32(TEGRA_MSELECT_BASE + MSELECT_CONFIG, val);
447 
448 		/*
449 		 * Restore Boot and Power Management Processor (BPMP) reset
450 		 * address and reset it, if it is supported by the platform.
451 		 */
452 		if (!tegra_bpmp_available) {
453 			tegra_fc_bpmp_off();
454 		} else {
455 			entrypoint = tegra_pmc_read_32(PMC_SCRATCH39);
456 			tegra_fc_bpmp_on(entrypoint);
457 
458 			/* initialise the interface */
459 			tegra_bpmp_resume();
460 		}
461 
462 		/* sc7entry-fw is part of TZDRAM area */
463 		if (plat_params->sc7entry_fw_base != 0U) {
464 			offset = plat_params->tzdram_base - plat_params->sc7entry_fw_base;
465 			tegra_memctrl_tzdram_setup(plat_params->sc7entry_fw_base,
466 				plat_params->tzdram_size + offset);
467 		}
468 	}
469 
470 	/*
471 	 * Check if we are exiting cluster idle state
472 	 */
473 	if (target_state->pwr_domain_state[MPIDR_AFFLVL1] ==
474 			PSTATE_ID_CLUSTER_IDLE) {
475 
476 		if (!tegra_bpmp_available) {
477 
478 			/* PWM un-tristate */
479 			cfg = mmio_read_32(TEGRA_CL_DVFS_BASE + DVFS_DFLL_OUTPUT_CFG);
480 			if (cfg & DFLL_OUTPUT_CFG_CLK_EN_BIT) {
481 				val = mmio_read_32(TEGRA_MISC_BASE + PINMUX_AUX_DVFS_PWM);
482 				val &= ~PINMUX_PWM_TRISTATE;
483 				mmio_write_32(TEGRA_MISC_BASE + PINMUX_AUX_DVFS_PWM, val);
484 			}
485 
486 			/* release cluster idle lock */
487 			tegra_fc_ccplex_pgexit_unlock();
488 		}
489 	}
490 
491 	/*
492 	 * T210 has a dedicated ARMv7 boot and power mgmt processor, BPMP. It's
493 	 * used for power management and boot purposes. Inform the BPMP that
494 	 * we have completed the cluster power up.
495 	 */
496 	tegra_fc_lock_active_cluster();
497 
498 	/*
499          * Resume PMC hardware block for Tegra210 platforms supporting sc7entry-fw
500          */
501 	if (!tegra_chipid_is_t210_b01() && (plat_params->sc7entry_fw_base != 0U))
502 		tegra_pmc_resume();
503 
504 	return PSCI_E_SUCCESS;
505 }
506 
507 int tegra_soc_pwr_domain_on(u_register_t mpidr)
508 {
509 	int cpu = mpidr & MPIDR_CPU_MASK;
510 	uint32_t mask = CPU_CORE_RESET_MASK << cpu;
511 
512 	/* Deassert CPU reset signals */
513 	mmio_write_32(TEGRA_CAR_RESET_BASE + CPU_CMPLX_RESET_CLR, mask);
514 
515 	/* Turn on CPU using flow controller or PMC */
516 	if (cpu_powergate_mask[cpu] == 0) {
517 		tegra_pmc_cpu_on(cpu);
518 		cpu_powergate_mask[cpu] = 1;
519 	} else {
520 		tegra_fc_cpu_on(cpu);
521 	}
522 
523 	return PSCI_E_SUCCESS;
524 }
525 
526 int tegra_soc_pwr_domain_off(const psci_power_state_t *target_state)
527 {
528 	tegra_fc_cpu_off(read_mpidr() & MPIDR_CPU_MASK);
529 	return PSCI_E_SUCCESS;
530 }
531 
532 int tegra_soc_prepare_system_reset(void)
533 {
534 	/*
535 	 * Set System Clock (SCLK) to POR default so that the clock source
536 	 * for the PMC APB clock would not be changed due to system reset.
537 	 */
538 	mmio_write_32((uintptr_t)TEGRA_CAR_RESET_BASE + SCLK_BURST_POLICY,
539 		SCLK_BURST_POLICY_DEFAULT);
540 	mmio_write_32((uintptr_t)TEGRA_CAR_RESET_BASE + SCLK_RATE, 0);
541 
542 	/* Wait 1 ms to make sure clock source/device logic is stabilized. */
543 	mdelay(1);
544 
545 	return PSCI_E_SUCCESS;
546 }
547