ARM: tegra: Support L2 cache maintenance done via firmware
[linux] / arch / arm / mach-tegra / pm.c
1 /*
2  * CPU complex suspend & resume functions for Tegra SoCs
3  *
4  * Copyright (c) 2009-2012, NVIDIA Corporation. All rights reserved.
5  *
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.
9  *
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
13  * more details.
14  *
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/>.
17  */
18
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>
24 #include <linux/io.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/spinlock.h>
28 #include <linux/suspend.h>
29
30 #include <soc/tegra/flowctrl.h>
31 #include <soc/tegra/fuse.h>
32 #include <soc/tegra/pm.h>
33 #include <soc/tegra/pmc.h>
34
35 #include <asm/cacheflush.h>
36 #include <asm/idmap.h>
37 #include <asm/proc-fns.h>
38 #include <asm/smp_plat.h>
39 #include <asm/suspend.h>
40 #include <asm/tlbflush.h>
41 #include <asm/trusted_foundations.h>
42
43 #include "iomap.h"
44 #include "pm.h"
45 #include "reset.h"
46 #include "sleep.h"
47
48 #ifdef CONFIG_PM_SLEEP
49 static DEFINE_SPINLOCK(tegra_lp2_lock);
50 static u32 iram_save_size;
51 static void *iram_save_addr;
52 struct tegra_lp1_iram tegra_lp1_iram;
53 void (*tegra_tear_down_cpu)(void);
54 void (*tegra_sleep_core_finish)(unsigned long v2p);
55 static int (*tegra_sleep_func)(unsigned long v2p);
56
57 static void tegra_tear_down_cpu_init(void)
58 {
59         switch (tegra_get_chip_id()) {
60         case TEGRA20:
61                 if (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC))
62                         tegra_tear_down_cpu = tegra20_tear_down_cpu;
63                 break;
64         case TEGRA30:
65         case TEGRA114:
66         case TEGRA124:
67                 if (IS_ENABLED(CONFIG_ARCH_TEGRA_3x_SOC) ||
68                     IS_ENABLED(CONFIG_ARCH_TEGRA_114_SOC) ||
69                     IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC))
70                         tegra_tear_down_cpu = tegra30_tear_down_cpu;
71                 break;
72         }
73 }
74
75 /*
76  * restore_cpu_complex
77  *
78  * restores cpu clock setting, clears flow controller
79  *
80  * Always called on CPU 0.
81  */
82 static void restore_cpu_complex(void)
83 {
84         int cpu = smp_processor_id();
85
86         BUG_ON(cpu != 0);
87
88 #ifdef CONFIG_SMP
89         cpu = cpu_logical_map(cpu);
90 #endif
91
92         /* Restore the CPU clock settings */
93         tegra_cpu_clock_resume();
94
95         flowctrl_cpu_suspend_exit(cpu);
96 }
97
98 /*
99  * suspend_cpu_complex
100  *
101  * saves pll state for use by restart_plls, prepares flow controller for
102  * transition to suspend state
103  *
104  * Must always be called on cpu 0.
105  */
106 static void suspend_cpu_complex(void)
107 {
108         int cpu = smp_processor_id();
109
110         BUG_ON(cpu != 0);
111
112 #ifdef CONFIG_SMP
113         cpu = cpu_logical_map(cpu);
114 #endif
115
116         /* Save the CPU clock settings */
117         tegra_cpu_clock_suspend();
118
119         flowctrl_cpu_suspend_enter(cpu);
120 }
121
122 void tegra_clear_cpu_in_lp2(void)
123 {
124         int phy_cpu_id = cpu_logical_map(smp_processor_id());
125         u32 *cpu_in_lp2 = tegra_cpu_lp2_mask;
126
127         spin_lock(&tegra_lp2_lock);
128
129         BUG_ON(!(*cpu_in_lp2 & BIT(phy_cpu_id)));
130         *cpu_in_lp2 &= ~BIT(phy_cpu_id);
131
132         spin_unlock(&tegra_lp2_lock);
133 }
134
135 bool tegra_set_cpu_in_lp2(void)
136 {
137         int phy_cpu_id = cpu_logical_map(smp_processor_id());
138         bool last_cpu = false;
139         cpumask_t *cpu_lp2_mask = tegra_cpu_lp2_mask;
140         u32 *cpu_in_lp2 = tegra_cpu_lp2_mask;
141
142         spin_lock(&tegra_lp2_lock);
143
144         BUG_ON((*cpu_in_lp2 & BIT(phy_cpu_id)));
145         *cpu_in_lp2 |= BIT(phy_cpu_id);
146
147         if ((phy_cpu_id == 0) && cpumask_equal(cpu_lp2_mask, cpu_online_mask))
148                 last_cpu = true;
149         else if (tegra_get_chip_id() == TEGRA20 && phy_cpu_id == 1)
150                 tegra20_cpu_set_resettable_soon();
151
152         spin_unlock(&tegra_lp2_lock);
153         return last_cpu;
154 }
155
156 int tegra_cpu_do_idle(void)
157 {
158         return cpu_do_idle();
159 }
160
161 static int tegra_sleep_cpu(unsigned long v2p)
162 {
163         setup_mm_for_reboot();
164         tegra_sleep_cpu_finish(v2p);
165
166         /* should never here */
167         BUG();
168
169         return 0;
170 }
171
172 static void tegra_pm_set(enum tegra_suspend_mode mode)
173 {
174         u32 value;
175
176         switch (tegra_get_chip_id()) {
177         case TEGRA20:
178         case TEGRA30:
179                 break;
180         default:
181                 /* Turn off CRAIL */
182                 value = flowctrl_read_cpu_csr(0);
183                 value &= ~FLOW_CTRL_CSR_ENABLE_EXT_MASK;
184                 value |= FLOW_CTRL_CSR_ENABLE_EXT_CRAIL;
185                 flowctrl_write_cpu_csr(0, value);
186                 break;
187         }
188
189         tegra_pmc_enter_suspend_mode(mode);
190 }
191
192 void tegra_idle_lp2_last(void)
193 {
194         tegra_pm_set(TEGRA_SUSPEND_LP2);
195
196         cpu_cluster_pm_enter();
197         suspend_cpu_complex();
198
199         /*
200          * L2 cache disabling using kernel API only allowed when all
201          * secondary CPU's are offline. Cache have to be disabled early
202          * if cache maintenance is done via Trusted Foundations firmware.
203          * Note that CPUIDLE won't ever enter powergate on Tegra30 if any
204          * of secondary CPU's is online and this is the LP2 codepath only
205          * for Tegra20/30.
206          */
207         if (trusted_foundations_registered())
208                 outer_disable();
209
210         cpu_suspend(PHYS_OFFSET - PAGE_OFFSET, &tegra_sleep_cpu);
211
212         /*
213          * Resume L2 cache if it wasn't re-enabled early during resume,
214          * which is the case for Tegra30 that has to re-enable the cache
215          * via firmware call. In other cases cache is already enabled and
216          * hence re-enabling is a no-op.
217          */
218         outer_resume();
219
220         restore_cpu_complex();
221         cpu_cluster_pm_exit();
222 }
223
224 enum tegra_suspend_mode tegra_pm_validate_suspend_mode(
225                                 enum tegra_suspend_mode mode)
226 {
227         /*
228          * The Tegra devices support suspending to LP1 or lower currently.
229          */
230         if (mode > TEGRA_SUSPEND_LP1)
231                 return TEGRA_SUSPEND_LP1;
232
233         return mode;
234 }
235
236 static int tegra_sleep_core(unsigned long v2p)
237 {
238         setup_mm_for_reboot();
239         tegra_sleep_core_finish(v2p);
240
241         /* should never here */
242         BUG();
243
244         return 0;
245 }
246
247 /*
248  * tegra_lp1_iram_hook
249  *
250  * Hooking the address of LP1 reset vector and SDRAM self-refresh code in
251  * SDRAM. These codes not be copied to IRAM in this fuction. We need to
252  * copy these code to IRAM before LP0/LP1 suspend and restore the content
253  * of IRAM after resume.
254  */
255 static bool tegra_lp1_iram_hook(void)
256 {
257         switch (tegra_get_chip_id()) {
258         case TEGRA20:
259                 if (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC))
260                         tegra20_lp1_iram_hook();
261                 break;
262         case TEGRA30:
263         case TEGRA114:
264         case TEGRA124:
265                 if (IS_ENABLED(CONFIG_ARCH_TEGRA_3x_SOC) ||
266                     IS_ENABLED(CONFIG_ARCH_TEGRA_114_SOC) ||
267                     IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC))
268                         tegra30_lp1_iram_hook();
269                 break;
270         default:
271                 break;
272         }
273
274         if (!tegra_lp1_iram.start_addr || !tegra_lp1_iram.end_addr)
275                 return false;
276
277         iram_save_size = tegra_lp1_iram.end_addr - tegra_lp1_iram.start_addr;
278         iram_save_addr = kmalloc(iram_save_size, GFP_KERNEL);
279         if (!iram_save_addr)
280                 return false;
281
282         return true;
283 }
284
285 static bool tegra_sleep_core_init(void)
286 {
287         switch (tegra_get_chip_id()) {
288         case TEGRA20:
289                 if (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC))
290                         tegra20_sleep_core_init();
291                 break;
292         case TEGRA30:
293         case TEGRA114:
294         case TEGRA124:
295                 if (IS_ENABLED(CONFIG_ARCH_TEGRA_3x_SOC) ||
296                     IS_ENABLED(CONFIG_ARCH_TEGRA_114_SOC) ||
297                     IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC))
298                         tegra30_sleep_core_init();
299                 break;
300         default:
301                 break;
302         }
303
304         if (!tegra_sleep_core_finish)
305                 return false;
306
307         return true;
308 }
309
310 static void tegra_suspend_enter_lp1(void)
311 {
312         /* copy the reset vector & SDRAM shutdown code into IRAM */
313         memcpy(iram_save_addr, IO_ADDRESS(TEGRA_IRAM_LPx_RESUME_AREA),
314                 iram_save_size);
315         memcpy(IO_ADDRESS(TEGRA_IRAM_LPx_RESUME_AREA),
316                 tegra_lp1_iram.start_addr, iram_save_size);
317
318         *((u32 *)tegra_cpu_lp1_mask) = 1;
319 }
320
321 static void tegra_suspend_exit_lp1(void)
322 {
323         /* restore IRAM */
324         memcpy(IO_ADDRESS(TEGRA_IRAM_LPx_RESUME_AREA), iram_save_addr,
325                 iram_save_size);
326
327         *(u32 *)tegra_cpu_lp1_mask = 0;
328 }
329
330 static const char *lp_state[TEGRA_MAX_SUSPEND_MODE] = {
331         [TEGRA_SUSPEND_NONE] = "none",
332         [TEGRA_SUSPEND_LP2] = "LP2",
333         [TEGRA_SUSPEND_LP1] = "LP1",
334         [TEGRA_SUSPEND_LP0] = "LP0",
335 };
336
337 static int tegra_suspend_enter(suspend_state_t state)
338 {
339         enum tegra_suspend_mode mode = tegra_pmc_get_suspend_mode();
340
341         if (WARN_ON(mode < TEGRA_SUSPEND_NONE ||
342                     mode >= TEGRA_MAX_SUSPEND_MODE))
343                 return -EINVAL;
344
345         pr_info("Entering suspend state %s\n", lp_state[mode]);
346
347         tegra_pm_set(mode);
348
349         local_fiq_disable();
350
351         suspend_cpu_complex();
352         switch (mode) {
353         case TEGRA_SUSPEND_LP1:
354                 tegra_suspend_enter_lp1();
355                 break;
356         case TEGRA_SUSPEND_LP2:
357                 tegra_set_cpu_in_lp2();
358                 break;
359         default:
360                 break;
361         }
362
363         /*
364          * Cache have to be disabled early if cache maintenance is done
365          * via Trusted Foundations firmware. Otherwise this is a no-op,
366          * like on Tegra114+.
367          */
368         if (trusted_foundations_registered())
369                 outer_disable();
370
371         cpu_suspend(PHYS_OFFSET - PAGE_OFFSET, tegra_sleep_func);
372
373         /*
374          * Resume L2 cache if it wasn't re-enabled early during resume,
375          * which is the case for Tegra30 that has to re-enable the cache
376          * via firmware call. In other cases cache is already enabled and
377          * hence re-enabling is a no-op.
378          */
379         outer_resume();
380
381         switch (mode) {
382         case TEGRA_SUSPEND_LP1:
383                 tegra_suspend_exit_lp1();
384                 break;
385         case TEGRA_SUSPEND_LP2:
386                 tegra_clear_cpu_in_lp2();
387                 break;
388         default:
389                 break;
390         }
391         restore_cpu_complex();
392
393         local_fiq_enable();
394
395         return 0;
396 }
397
398 static const struct platform_suspend_ops tegra_suspend_ops = {
399         .valid          = suspend_valid_only_mem,
400         .enter          = tegra_suspend_enter,
401 };
402
403 void __init tegra_init_suspend(void)
404 {
405         enum tegra_suspend_mode mode = tegra_pmc_get_suspend_mode();
406
407         if (mode == TEGRA_SUSPEND_NONE)
408                 return;
409
410         tegra_tear_down_cpu_init();
411
412         if (mode >= TEGRA_SUSPEND_LP1) {
413                 if (!tegra_lp1_iram_hook() || !tegra_sleep_core_init()) {
414                         pr_err("%s: unable to allocate memory for SDRAM"
415                                "self-refresh -- LP0/LP1 unavailable\n",
416                                __func__);
417                         tegra_pmc_set_suspend_mode(TEGRA_SUSPEND_LP2);
418                         mode = TEGRA_SUSPEND_LP2;
419                 }
420         }
421
422         /* set up sleep function for cpu_suspend */
423         switch (mode) {
424         case TEGRA_SUSPEND_LP1:
425                 tegra_sleep_func = tegra_sleep_core;
426                 break;
427         case TEGRA_SUSPEND_LP2:
428                 tegra_sleep_func = tegra_sleep_cpu;
429                 break;
430         default:
431                 break;
432         }
433
434         suspend_set_ops(&tegra_suspend_ops);
435 }
436 #endif