2 * CPU complex suspend & resume functions for Tegra SoCs
4 * Copyright (c) 2009-2012, NVIDIA Corporation. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <linux/clk/tegra.h>
20 #include <linux/cpumask.h>
21 #include <linux/cpu_pm.h>
22 #include <linux/delay.h>
23 #include <linux/err.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/spinlock.h>
28 #include <linux/suspend.h>
30 #include <linux/firmware/trusted_foundations.h>
32 #include <soc/tegra/flowctrl.h>
33 #include <soc/tegra/fuse.h>
34 #include <soc/tegra/pm.h>
35 #include <soc/tegra/pmc.h>
37 #include <asm/cacheflush.h>
38 #include <asm/firmware.h>
39 #include <asm/idmap.h>
40 #include <asm/proc-fns.h>
41 #include <asm/smp_plat.h>
42 #include <asm/suspend.h>
43 #include <asm/tlbflush.h>
50 #ifdef CONFIG_PM_SLEEP
51 static DEFINE_SPINLOCK(tegra_lp2_lock);
52 static u32 iram_save_size;
53 static void *iram_save_addr;
54 struct tegra_lp1_iram tegra_lp1_iram;
55 void (*tegra_tear_down_cpu)(void);
56 void (*tegra_sleep_core_finish)(unsigned long v2p);
57 static int (*tegra_sleep_func)(unsigned long v2p);
59 static void tegra_tear_down_cpu_init(void)
61 switch (tegra_get_chip_id()) {
63 if (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC))
64 tegra_tear_down_cpu = tegra20_tear_down_cpu;
69 if (IS_ENABLED(CONFIG_ARCH_TEGRA_3x_SOC) ||
70 IS_ENABLED(CONFIG_ARCH_TEGRA_114_SOC) ||
71 IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC))
72 tegra_tear_down_cpu = tegra30_tear_down_cpu;
80 * restores cpu clock setting, clears flow controller
82 * Always called on CPU 0.
84 static void restore_cpu_complex(void)
86 int cpu = smp_processor_id();
91 cpu = cpu_logical_map(cpu);
94 /* Restore the CPU clock settings */
95 tegra_cpu_clock_resume();
97 flowctrl_cpu_suspend_exit(cpu);
101 * suspend_cpu_complex
103 * saves pll state for use by restart_plls, prepares flow controller for
104 * transition to suspend state
106 * Must always be called on cpu 0.
108 static void suspend_cpu_complex(void)
110 int cpu = smp_processor_id();
115 cpu = cpu_logical_map(cpu);
118 /* Save the CPU clock settings */
119 tegra_cpu_clock_suspend();
121 flowctrl_cpu_suspend_enter(cpu);
124 void tegra_clear_cpu_in_lp2(void)
126 int phy_cpu_id = cpu_logical_map(smp_processor_id());
127 u32 *cpu_in_lp2 = tegra_cpu_lp2_mask;
129 spin_lock(&tegra_lp2_lock);
131 BUG_ON(!(*cpu_in_lp2 & BIT(phy_cpu_id)));
132 *cpu_in_lp2 &= ~BIT(phy_cpu_id);
134 spin_unlock(&tegra_lp2_lock);
137 bool tegra_set_cpu_in_lp2(void)
139 int phy_cpu_id = cpu_logical_map(smp_processor_id());
140 bool last_cpu = false;
141 cpumask_t *cpu_lp2_mask = tegra_cpu_lp2_mask;
142 u32 *cpu_in_lp2 = tegra_cpu_lp2_mask;
144 spin_lock(&tegra_lp2_lock);
146 BUG_ON((*cpu_in_lp2 & BIT(phy_cpu_id)));
147 *cpu_in_lp2 |= BIT(phy_cpu_id);
149 if ((phy_cpu_id == 0) && cpumask_equal(cpu_lp2_mask, cpu_online_mask))
151 else if (tegra_get_chip_id() == TEGRA20 && phy_cpu_id == 1)
152 tegra20_cpu_set_resettable_soon();
154 spin_unlock(&tegra_lp2_lock);
158 int tegra_cpu_do_idle(void)
160 return cpu_do_idle();
163 static int tegra_sleep_cpu(unsigned long v2p)
166 * L2 cache disabling using kernel API only allowed when all
167 * secondary CPU's are offline. Cache have to be disabled with
168 * MMU-on if cache maintenance is done via Trusted Foundations
169 * firmware. Note that CPUIDLE won't ever enter powergate on Tegra30
170 * if any of secondary CPU's is online and this is the LP2-idle
171 * code-path only for Tegra20/30.
173 if (trusted_foundations_registered())
177 * Note that besides of setting up CPU reset vector this firmware
178 * call may also do the following, depending on the FW version:
179 * 1) Disable L2. But this doesn't matter since we already
181 * 2) Disable D-cache. This need to be taken into account in
182 * particular by the tegra_disable_clean_inv_dcache() which
183 * shall avoid the re-disable.
185 call_firmware_op(prepare_idle, TF_PM_MODE_LP2);
187 setup_mm_for_reboot();
188 tegra_sleep_cpu_finish(v2p);
190 /* should never here */
196 static void tegra_pm_set(enum tegra_suspend_mode mode)
200 switch (tegra_get_chip_id()) {
206 value = flowctrl_read_cpu_csr(0);
207 value &= ~FLOW_CTRL_CSR_ENABLE_EXT_MASK;
208 value |= FLOW_CTRL_CSR_ENABLE_EXT_CRAIL;
209 flowctrl_write_cpu_csr(0, value);
213 tegra_pmc_enter_suspend_mode(mode);
216 void tegra_idle_lp2_last(void)
218 tegra_pm_set(TEGRA_SUSPEND_LP2);
220 cpu_cluster_pm_enter();
221 suspend_cpu_complex();
223 cpu_suspend(PHYS_OFFSET - PAGE_OFFSET, &tegra_sleep_cpu);
226 * Resume L2 cache if it wasn't re-enabled early during resume,
227 * which is the case for Tegra30 that has to re-enable the cache
228 * via firmware call. In other cases cache is already enabled and
229 * hence re-enabling is a no-op. This is always a no-op on Tegra114+.
233 restore_cpu_complex();
234 cpu_cluster_pm_exit();
237 enum tegra_suspend_mode tegra_pm_validate_suspend_mode(
238 enum tegra_suspend_mode mode)
241 * The Tegra devices support suspending to LP1 or lower currently.
243 if (mode > TEGRA_SUSPEND_LP1)
244 return TEGRA_SUSPEND_LP1;
249 static int tegra_sleep_core(unsigned long v2p)
252 * Cache have to be disabled with MMU-on if cache maintenance is done
253 * via Trusted Foundations firmware. This is a no-op on Tegra114+.
255 if (trusted_foundations_registered())
258 call_firmware_op(prepare_idle, TF_PM_MODE_LP1);
260 setup_mm_for_reboot();
261 tegra_sleep_core_finish(v2p);
263 /* should never here */
270 * tegra_lp1_iram_hook
272 * Hooking the address of LP1 reset vector and SDRAM self-refresh code in
273 * SDRAM. These codes not be copied to IRAM in this fuction. We need to
274 * copy these code to IRAM before LP0/LP1 suspend and restore the content
275 * of IRAM after resume.
277 static bool tegra_lp1_iram_hook(void)
279 switch (tegra_get_chip_id()) {
281 if (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC))
282 tegra20_lp1_iram_hook();
287 if (IS_ENABLED(CONFIG_ARCH_TEGRA_3x_SOC) ||
288 IS_ENABLED(CONFIG_ARCH_TEGRA_114_SOC) ||
289 IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC))
290 tegra30_lp1_iram_hook();
296 if (!tegra_lp1_iram.start_addr || !tegra_lp1_iram.end_addr)
299 iram_save_size = tegra_lp1_iram.end_addr - tegra_lp1_iram.start_addr;
300 iram_save_addr = kmalloc(iram_save_size, GFP_KERNEL);
307 static bool tegra_sleep_core_init(void)
309 switch (tegra_get_chip_id()) {
311 if (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC))
312 tegra20_sleep_core_init();
317 if (IS_ENABLED(CONFIG_ARCH_TEGRA_3x_SOC) ||
318 IS_ENABLED(CONFIG_ARCH_TEGRA_114_SOC) ||
319 IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC))
320 tegra30_sleep_core_init();
326 if (!tegra_sleep_core_finish)
332 static void tegra_suspend_enter_lp1(void)
334 /* copy the reset vector & SDRAM shutdown code into IRAM */
335 memcpy(iram_save_addr, IO_ADDRESS(TEGRA_IRAM_LPx_RESUME_AREA),
337 memcpy(IO_ADDRESS(TEGRA_IRAM_LPx_RESUME_AREA),
338 tegra_lp1_iram.start_addr, iram_save_size);
340 *((u32 *)tegra_cpu_lp1_mask) = 1;
343 static void tegra_suspend_exit_lp1(void)
346 memcpy(IO_ADDRESS(TEGRA_IRAM_LPx_RESUME_AREA), iram_save_addr,
349 *(u32 *)tegra_cpu_lp1_mask = 0;
352 static const char *lp_state[TEGRA_MAX_SUSPEND_MODE] = {
353 [TEGRA_SUSPEND_NONE] = "none",
354 [TEGRA_SUSPEND_LP2] = "LP2",
355 [TEGRA_SUSPEND_LP1] = "LP1",
356 [TEGRA_SUSPEND_LP0] = "LP0",
359 static int tegra_suspend_enter(suspend_state_t state)
361 enum tegra_suspend_mode mode = tegra_pmc_get_suspend_mode();
363 if (WARN_ON(mode < TEGRA_SUSPEND_NONE ||
364 mode >= TEGRA_MAX_SUSPEND_MODE))
367 pr_info("Entering suspend state %s\n", lp_state[mode]);
373 suspend_cpu_complex();
375 case TEGRA_SUSPEND_LP1:
376 tegra_suspend_enter_lp1();
378 case TEGRA_SUSPEND_LP2:
379 tegra_set_cpu_in_lp2();
385 cpu_suspend(PHYS_OFFSET - PAGE_OFFSET, tegra_sleep_func);
388 * Resume L2 cache if it wasn't re-enabled early during resume,
389 * which is the case for Tegra30 that has to re-enable the cache
390 * via firmware call. In other cases cache is already enabled and
391 * hence re-enabling is a no-op.
396 case TEGRA_SUSPEND_LP1:
397 tegra_suspend_exit_lp1();
399 case TEGRA_SUSPEND_LP2:
400 tegra_clear_cpu_in_lp2();
405 restore_cpu_complex();
412 static const struct platform_suspend_ops tegra_suspend_ops = {
413 .valid = suspend_valid_only_mem,
414 .enter = tegra_suspend_enter,
417 void __init tegra_init_suspend(void)
419 enum tegra_suspend_mode mode = tegra_pmc_get_suspend_mode();
421 if (mode == TEGRA_SUSPEND_NONE)
424 tegra_tear_down_cpu_init();
426 if (mode >= TEGRA_SUSPEND_LP1) {
427 if (!tegra_lp1_iram_hook() || !tegra_sleep_core_init()) {
428 pr_err("%s: unable to allocate memory for SDRAM"
429 "self-refresh -- LP0/LP1 unavailable\n",
431 tegra_pmc_set_suspend_mode(TEGRA_SUSPEND_LP2);
432 mode = TEGRA_SUSPEND_LP2;
436 /* set up sleep function for cpu_suspend */
438 case TEGRA_SUSPEND_LP1:
439 tegra_sleep_func = tegra_sleep_core;
441 case TEGRA_SUSPEND_LP2:
442 tegra_sleep_func = tegra_sleep_cpu;
448 suspend_set_ops(&tegra_suspend_ops);