blob: 985cc02786e3d7ab3d49a4305502487fe0d46ca4 [file] [log] [blame]
Ralf Baechle49f2ec92013-05-21 10:53:37 +02001/*
2 * MIPS idle loop and WAIT instruction support.
3 *
4 * Copyright (C) xxxx the Anonymous
5 * Copyright (C) 1994 - 2006 Ralf Baechle
6 * Copyright (C) 2003, 2004 Maciej W. Rozycki
7 * Copyright (C) 2001, 2004, 2011, 2012 MIPS Technologies, Inc.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 */
14#include <linux/export.h>
15#include <linux/init.h>
16#include <linux/irqflags.h>
17#include <linux/printk.h>
18#include <linux/sched.h>
19#include <asm/cpu.h>
20#include <asm/cpu-info.h>
Ralf Baechlebdc92d742013-05-21 16:59:19 +020021#include <asm/idle.h>
Ralf Baechle49f2ec92013-05-21 10:53:37 +020022#include <asm/mipsregs.h>
23
24/*
25 * Not all of the MIPS CPUs have the "wait" instruction available. Moreover,
26 * the implementation of the "wait" feature differs between CPU families. This
27 * points to the function that implements CPU specific wait.
28 * The wait instruction stops the pipeline and reduces the power consumption of
29 * the CPU very much.
30 */
31void (*cpu_wait)(void);
32EXPORT_SYMBOL(cpu_wait);
33
34static void r3081_wait(void)
35{
36 unsigned long cfg = read_c0_conf();
37 write_c0_conf(cfg | R30XX_CONF_HALT);
Ralf Baechlefb40bc32013-05-21 14:05:27 +020038 local_irq_enable();
Ralf Baechle49f2ec92013-05-21 10:53:37 +020039}
40
41static void r39xx_wait(void)
42{
Ralf Baechle49f2ec92013-05-21 10:53:37 +020043 if (!need_resched())
44 write_c0_conf(read_c0_conf() | TX39_CONF_HALT);
45 local_irq_enable();
46}
47
Ralf Baechle49f2ec92013-05-21 10:53:37 +020048/*
49 * This variant is preferable as it allows testing need_resched and going to
50 * sleep depending on the outcome atomically. Unfortunately the "It is
51 * implementation-dependent whether the pipeline restarts when a non-enabled
52 * interrupt is requested" restriction in the MIPS32/MIPS64 architecture makes
53 * using this version a gamble.
54 */
55void r4k_wait_irqoff(void)
56{
Ralf Baechle49f2ec92013-05-21 10:53:37 +020057 if (!need_resched())
Ralf Baechlef91a1482013-05-21 12:58:08 +020058 __asm__(
59 " .set push \n"
60 " .set mips3 \n"
61 " wait \n"
62 " .set pop \n");
Ralf Baechle49f2ec92013-05-21 10:53:37 +020063 local_irq_enable();
Ralf Baechlef91a1482013-05-21 12:58:08 +020064 __asm__(
65 " .globl __pastwait \n"
66 "__pastwait: \n");
Ralf Baechle49f2ec92013-05-21 10:53:37 +020067}
68
69/*
70 * The RM7000 variant has to handle erratum 38. The workaround is to not
71 * have any pending stores when the WAIT instruction is executed.
72 */
73static void rm7k_wait_irqoff(void)
74{
Ralf Baechle49f2ec92013-05-21 10:53:37 +020075 if (!need_resched())
76 __asm__(
77 " .set push \n"
78 " .set mips3 \n"
79 " .set noat \n"
80 " mfc0 $1, $12 \n"
81 " sync \n"
82 " mtc0 $1, $12 # stalls until W stage \n"
83 " wait \n"
84 " mtc0 $1, $12 # stalls until W stage \n"
85 " .set pop \n");
86 local_irq_enable();
87}
88
89/*
90 * The Au1xxx wait is available only if using 32khz counter or
91 * external timer source, but specifically not CP0 Counter.
92 * alchemy/common/time.c may override cpu_wait!
93 */
94static void au1k_wait(void)
95{
Ralf Baechlef91a1482013-05-21 12:58:08 +020096 __asm__(
97 " .set mips3 \n"
98 " cache 0x14, 0(%0) \n"
99 " cache 0x14, 32(%0) \n"
100 " sync \n"
101 " nop \n"
102 " wait \n"
103 " nop \n"
104 " nop \n"
105 " nop \n"
106 " nop \n"
107 " .set mips0 \n"
108 : : "r" (au1k_wait));
Ralf Baechlefb40bc32013-05-21 14:05:27 +0200109 local_irq_enable();
Ralf Baechle49f2ec92013-05-21 10:53:37 +0200110}
111
112static int __initdata nowait;
113
114static int __init wait_disable(char *s)
115{
116 nowait = 1;
117
118 return 1;
119}
120
121__setup("nowait", wait_disable);
122
123void __init check_wait(void)
124{
125 struct cpuinfo_mips *c = &current_cpu_data;
126
127 if (nowait) {
128 printk("Wait instruction disabled.\n");
129 return;
130 }
131
132 switch (c->cputype) {
133 case CPU_R3081:
134 case CPU_R3081E:
135 cpu_wait = r3081_wait;
136 break;
137 case CPU_TX3927:
138 cpu_wait = r39xx_wait;
139 break;
140 case CPU_R4200:
141/* case CPU_R4300: */
142 case CPU_R4600:
143 case CPU_R4640:
144 case CPU_R4650:
145 case CPU_R4700:
146 case CPU_R5000:
147 case CPU_R5500:
148 case CPU_NEVADA:
149 case CPU_4KC:
150 case CPU_4KEC:
151 case CPU_4KSC:
152 case CPU_5KC:
153 case CPU_25KF:
154 case CPU_PR4450:
155 case CPU_BMIPS3300:
156 case CPU_BMIPS4350:
157 case CPU_BMIPS4380:
158 case CPU_BMIPS5000:
159 case CPU_CAVIUM_OCTEON:
160 case CPU_CAVIUM_OCTEON_PLUS:
161 case CPU_CAVIUM_OCTEON2:
162 case CPU_JZRISC:
163 case CPU_LOONGSON1:
164 case CPU_XLR:
165 case CPU_XLP:
166 cpu_wait = r4k_wait;
167 break;
168
169 case CPU_RM7000:
170 cpu_wait = rm7k_wait_irqoff;
171 break;
172
173 case CPU_M14KC:
174 case CPU_M14KEC:
175 case CPU_24K:
176 case CPU_34K:
177 case CPU_1004K:
178 cpu_wait = r4k_wait;
179 if (read_c0_config7() & MIPS_CONF7_WII)
180 cpu_wait = r4k_wait_irqoff;
181 break;
182
183 case CPU_74K:
184 cpu_wait = r4k_wait;
185 if ((c->processor_id & 0xff) >= PRID_REV_ENCODE_332(2, 1, 0))
186 cpu_wait = r4k_wait_irqoff;
187 break;
188
189 case CPU_TX49XX:
190 cpu_wait = r4k_wait_irqoff;
191 break;
192 case CPU_ALCHEMY:
193 cpu_wait = au1k_wait;
194 break;
195 case CPU_20KC:
196 /*
197 * WAIT on Rev1.0 has E1, E2, E3 and E16.
198 * WAIT on Rev2.0 and Rev3.0 has E16.
199 * Rev3.1 WAIT is nop, why bother
200 */
201 if ((c->processor_id & 0xff) <= 0x64)
202 break;
203
204 /*
205 * Another rev is incremeting c0_count at a reduced clock
206 * rate while in WAIT mode. So we basically have the choice
207 * between using the cp0 timer as clocksource or avoiding
208 * the WAIT instruction. Until more details are known,
209 * disable the use of WAIT for 20Kc entirely.
210 cpu_wait = r4k_wait;
211 */
212 break;
213 case CPU_RM9000:
214 if ((c->processor_id & 0x00ff) >= 0x40)
215 cpu_wait = r4k_wait;
216 break;
217 default:
218 break;
219 }
220}
221
Ralf Baechle00baf852013-05-21 12:47:26 +0200222static void smtc_idle_hook(void)
Ralf Baechle49f2ec92013-05-21 10:53:37 +0200223{
224#ifdef CONFIG_MIPS_MT_SMTC
Ralf Baechle00baf852013-05-21 12:47:26 +0200225 void smtc_idle_loop_hook(void);
Ralf Baechle49f2ec92013-05-21 10:53:37 +0200226
227 smtc_idle_loop_hook();
228#endif
Ralf Baechle00baf852013-05-21 12:47:26 +0200229}
230
231void arch_cpu_idle(void)
232{
233 smtc_idle_hook();
Ralf Baechle49f2ec92013-05-21 10:53:37 +0200234 if (cpu_wait)
Ralf Baechlec9b68692013-05-21 13:02:12 +0200235 cpu_wait();
Ralf Baechle49f2ec92013-05-21 10:53:37 +0200236 else
237 local_irq_enable();
238}