1 // SPDX-License-Identifier: GPL-2.0-only
3 * CPU complex suspend & resume functions for Tegra SoCs
5 * Copyright (c) 2009-2012, NVIDIA Corporation. All rights reserved.
8 #include <linux/clk/tegra.h>
9 #include <linux/cpumask.h>
10 #include <linux/cpu_pm.h>
11 #include <linux/delay.h>
12 #include <linux/err.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/spinlock.h>
17 #include <linux/suspend.h>
19 #include <linux/firmware/trusted_foundations.h>
21 #include <soc/tegra/flowctrl.h>
22 #include <soc/tegra/fuse.h>
23 #include <soc/tegra/pm.h>
24 #include <soc/tegra/pmc.h>
26 #include <asm/cacheflush.h>
27 #include <asm/firmware.h>
28 #include <asm/idmap.h>
29 #include <asm/proc-fns.h>
30 #include <asm/smp_plat.h>
31 #include <asm/suspend.h>
32 #include <asm/tlbflush.h>
39 #ifdef CONFIG_PM_SLEEP
40 static DEFINE_SPINLOCK(tegra_lp2_lock
);
41 static u32 iram_save_size
;
42 static void *iram_save_addr
;
43 struct tegra_lp1_iram tegra_lp1_iram
;
44 void (*tegra_tear_down_cpu
)(void);
45 void (*tegra_sleep_core_finish
)(unsigned long v2p
);
46 static int (*tegra_sleep_func
)(unsigned long v2p
);
48 static void tegra_tear_down_cpu_init(void)
50 switch (tegra_get_chip_id()) {
52 if (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC
))
53 tegra_tear_down_cpu
= tegra20_tear_down_cpu
;
58 if (IS_ENABLED(CONFIG_ARCH_TEGRA_3x_SOC
) ||
59 IS_ENABLED(CONFIG_ARCH_TEGRA_114_SOC
) ||
60 IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC
))
61 tegra_tear_down_cpu
= tegra30_tear_down_cpu
;
69 * restores cpu clock setting, clears flow controller
71 * Always called on CPU 0.
73 static void restore_cpu_complex(void)
75 int cpu
= smp_processor_id();
80 cpu
= cpu_logical_map(cpu
);
83 /* Restore the CPU clock settings */
84 tegra_cpu_clock_resume();
86 flowctrl_cpu_suspend_exit(cpu
);
92 * saves pll state for use by restart_plls, prepares flow controller for
93 * transition to suspend state
95 * Must always be called on cpu 0.
97 static void suspend_cpu_complex(void)
99 int cpu
= smp_processor_id();
104 cpu
= cpu_logical_map(cpu
);
107 /* Save the CPU clock settings */
108 tegra_cpu_clock_suspend();
110 flowctrl_cpu_suspend_enter(cpu
);
113 void tegra_pm_clear_cpu_in_lp2(void)
115 int phy_cpu_id
= cpu_logical_map(smp_processor_id());
116 u32
*cpu_in_lp2
= tegra_cpu_lp2_mask
;
118 spin_lock(&tegra_lp2_lock
);
120 BUG_ON(!(*cpu_in_lp2
& BIT(phy_cpu_id
)));
121 *cpu_in_lp2
&= ~BIT(phy_cpu_id
);
123 spin_unlock(&tegra_lp2_lock
);
126 void tegra_pm_set_cpu_in_lp2(void)
128 int phy_cpu_id
= cpu_logical_map(smp_processor_id());
129 u32
*cpu_in_lp2
= tegra_cpu_lp2_mask
;
131 spin_lock(&tegra_lp2_lock
);
133 BUG_ON((*cpu_in_lp2
& BIT(phy_cpu_id
)));
134 *cpu_in_lp2
|= BIT(phy_cpu_id
);
136 spin_unlock(&tegra_lp2_lock
);
139 static int tegra_sleep_cpu(unsigned long v2p
)
141 if (tegra_cpu_car_ops
->rail_off_ready
&&
142 WARN_ON(!tegra_cpu_rail_off_ready()))
146 * L2 cache disabling using kernel API only allowed when all
147 * secondary CPU's are offline. Cache have to be disabled with
148 * MMU-on if cache maintenance is done via Trusted Foundations
149 * firmware. Note that CPUIDLE won't ever enter powergate on Tegra30
150 * if any of secondary CPU's is online and this is the LP2-idle
151 * code-path only for Tegra20/30.
153 #ifdef CONFIG_OUTER_CACHE
154 if (trusted_foundations_registered() && outer_cache
.disable
)
155 outer_cache
.disable();
158 * Note that besides of setting up CPU reset vector this firmware
159 * call may also do the following, depending on the FW version:
160 * 1) Disable L2. But this doesn't matter since we already
162 * 2) Disable D-cache. This need to be taken into account in
163 * particular by the tegra_disable_clean_inv_dcache() which
164 * shall avoid the re-disable.
166 call_firmware_op(prepare_idle
, TF_PM_MODE_LP2
);
168 setup_mm_for_reboot();
169 tegra_sleep_cpu_finish(v2p
);
171 /* should never here */
177 static void tegra_pm_set(enum tegra_suspend_mode mode
)
181 switch (tegra_get_chip_id()) {
187 value
= flowctrl_read_cpu_csr(0);
188 value
&= ~FLOW_CTRL_CSR_ENABLE_EXT_MASK
;
189 value
|= FLOW_CTRL_CSR_ENABLE_EXT_CRAIL
;
190 flowctrl_write_cpu_csr(0, value
);
194 tegra_pmc_enter_suspend_mode(mode
);
197 int tegra_pm_enter_lp2(void)
201 tegra_pm_set(TEGRA_SUSPEND_LP2
);
203 cpu_cluster_pm_enter();
204 suspend_cpu_complex();
206 err
= cpu_suspend(PHYS_OFFSET
- PAGE_OFFSET
, &tegra_sleep_cpu
);
209 * Resume L2 cache if it wasn't re-enabled early during resume,
210 * which is the case for Tegra30 that has to re-enable the cache
211 * via firmware call. In other cases cache is already enabled and
212 * hence re-enabling is a no-op. This is always a no-op on Tegra114+.
216 restore_cpu_complex();
217 cpu_cluster_pm_exit();
219 call_firmware_op(prepare_idle
, TF_PM_MODE_NONE
);
224 enum tegra_suspend_mode
tegra_pm_validate_suspend_mode(
225 enum tegra_suspend_mode mode
)
228 * The Tegra devices support suspending to LP1 or lower currently.
230 if (mode
> TEGRA_SUSPEND_LP1
)
231 return TEGRA_SUSPEND_LP1
;
236 static int tegra_sleep_core(unsigned long v2p
)
239 * Cache have to be disabled with MMU-on if cache maintenance is done
240 * via Trusted Foundations firmware. This is a no-op on Tegra114+.
242 if (trusted_foundations_registered())
245 call_firmware_op(prepare_idle
, TF_PM_MODE_LP1
);
247 setup_mm_for_reboot();
248 tegra_sleep_core_finish(v2p
);
250 /* should never here */
257 * tegra_lp1_iram_hook
259 * Hooking the address of LP1 reset vector and SDRAM self-refresh code in
260 * SDRAM. These codes not be copied to IRAM in this fuction. We need to
261 * copy these code to IRAM before LP0/LP1 suspend and restore the content
262 * of IRAM after resume.
264 static bool tegra_lp1_iram_hook(void)
266 switch (tegra_get_chip_id()) {
268 if (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC
))
269 tegra20_lp1_iram_hook();
274 if (IS_ENABLED(CONFIG_ARCH_TEGRA_3x_SOC
) ||
275 IS_ENABLED(CONFIG_ARCH_TEGRA_114_SOC
) ||
276 IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC
))
277 tegra30_lp1_iram_hook();
283 if (!tegra_lp1_iram
.start_addr
|| !tegra_lp1_iram
.end_addr
)
286 iram_save_size
= tegra_lp1_iram
.end_addr
- tegra_lp1_iram
.start_addr
;
287 iram_save_addr
= kmalloc(iram_save_size
, GFP_KERNEL
);
294 static bool tegra_sleep_core_init(void)
296 switch (tegra_get_chip_id()) {
298 if (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC
))
299 tegra20_sleep_core_init();
304 if (IS_ENABLED(CONFIG_ARCH_TEGRA_3x_SOC
) ||
305 IS_ENABLED(CONFIG_ARCH_TEGRA_114_SOC
) ||
306 IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC
))
307 tegra30_sleep_core_init();
313 if (!tegra_sleep_core_finish
)
319 static void tegra_suspend_enter_lp1(void)
321 /* copy the reset vector & SDRAM shutdown code into IRAM */
322 memcpy(iram_save_addr
, IO_ADDRESS(TEGRA_IRAM_LPx_RESUME_AREA
),
324 memcpy(IO_ADDRESS(TEGRA_IRAM_LPx_RESUME_AREA
),
325 tegra_lp1_iram
.start_addr
, iram_save_size
);
327 *((u32
*)tegra_cpu_lp1_mask
) = 1;
330 static void tegra_suspend_exit_lp1(void)
333 memcpy(IO_ADDRESS(TEGRA_IRAM_LPx_RESUME_AREA
), iram_save_addr
,
336 *(u32
*)tegra_cpu_lp1_mask
= 0;
339 static const char *lp_state
[TEGRA_MAX_SUSPEND_MODE
] = {
340 [TEGRA_SUSPEND_NONE
] = "none",
341 [TEGRA_SUSPEND_LP2
] = "LP2",
342 [TEGRA_SUSPEND_LP1
] = "LP1",
343 [TEGRA_SUSPEND_LP0
] = "LP0",
346 static int tegra_suspend_enter(suspend_state_t state
)
348 enum tegra_suspend_mode mode
= tegra_pmc_get_suspend_mode();
350 if (WARN_ON(mode
< TEGRA_SUSPEND_NONE
||
351 mode
>= TEGRA_MAX_SUSPEND_MODE
))
354 pr_info("Entering suspend state %s\n", lp_state
[mode
]);
360 suspend_cpu_complex();
362 case TEGRA_SUSPEND_LP1
:
363 tegra_suspend_enter_lp1();
365 case TEGRA_SUSPEND_LP2
:
366 tegra_pm_set_cpu_in_lp2();
372 cpu_suspend(PHYS_OFFSET
- PAGE_OFFSET
, tegra_sleep_func
);
375 * Resume L2 cache if it wasn't re-enabled early during resume,
376 * which is the case for Tegra30 that has to re-enable the cache
377 * via firmware call. In other cases cache is already enabled and
378 * hence re-enabling is a no-op.
383 case TEGRA_SUSPEND_LP1
:
384 tegra_suspend_exit_lp1();
386 case TEGRA_SUSPEND_LP2
:
387 tegra_pm_clear_cpu_in_lp2();
392 restore_cpu_complex();
396 call_firmware_op(prepare_idle
, TF_PM_MODE_NONE
);
401 static const struct platform_suspend_ops tegra_suspend_ops
= {
402 .valid
= suspend_valid_only_mem
,
403 .enter
= tegra_suspend_enter
,
406 void __init
tegra_init_suspend(void)
408 enum tegra_suspend_mode mode
= tegra_pmc_get_suspend_mode();
410 if (mode
== TEGRA_SUSPEND_NONE
)
413 tegra_tear_down_cpu_init();
415 if (mode
>= TEGRA_SUSPEND_LP1
) {
416 if (!tegra_lp1_iram_hook() || !tegra_sleep_core_init()) {
417 pr_err("%s: unable to allocate memory for SDRAM"
418 "self-refresh -- LP0/LP1 unavailable\n",
420 tegra_pmc_set_suspend_mode(TEGRA_SUSPEND_LP2
);
421 mode
= TEGRA_SUSPEND_LP2
;
425 /* set up sleep function for cpu_suspend */
427 case TEGRA_SUSPEND_LP1
:
428 tegra_sleep_func
= tegra_sleep_core
;
430 case TEGRA_SUSPEND_LP2
:
431 tegra_sleep_func
= tegra_sleep_cpu
;
437 suspend_set_ops(&tegra_suspend_ops
);
440 int tegra_pm_park_secondary_cpu(unsigned long cpu
)
443 tegra_disable_clean_inv_dcache(TEGRA_FLUSH_CACHE_LOUIS
);
445 if (tegra_get_chip_id() == TEGRA20
)
446 tegra20_hotplug_shutdown();
448 tegra30_hotplug_shutdown();