idle.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  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>
  21. #include <asm/cpu-type.h>
  22. #include <asm/idle.h>
  23. #include <asm/mipsregs.h>
  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. */
  31. void (*cpu_wait)(void);
  32. EXPORT_SYMBOL(cpu_wait);
  33. static void r3081_wait(void)
  34. {
  35. unsigned long cfg = read_c0_conf();
  36. write_c0_conf(cfg | R30XX_CONF_HALT);
  37. local_irq_enable();
  38. }
  39. static void r39xx_wait(void)
  40. {
  41. if (!need_resched())
  42. write_c0_conf(read_c0_conf() | TX39_CONF_HALT);
  43. local_irq_enable();
  44. }
  45. void r4k_wait(void)
  46. {
  47. local_irq_enable();
  48. __r4k_wait();
  49. }
  50. /*
  51. * This variant is preferable as it allows testing need_resched and going to
  52. * sleep depending on the outcome atomically. Unfortunately the "It is
  53. * implementation-dependent whether the pipeline restarts when a non-enabled
  54. * interrupt is requested" restriction in the MIPS32/MIPS64 architecture makes
  55. * using this version a gamble.
  56. */
  57. void r4k_wait_irqoff(void)
  58. {
  59. if (!need_resched())
  60. __asm__(
  61. " .set push \n"
  62. " .set arch=r4000 \n"
  63. " wait \n"
  64. " .set pop \n");
  65. local_irq_enable();
  66. }
  67. /*
  68. * The RM7000 variant has to handle erratum 38. The workaround is to not
  69. * have any pending stores when the WAIT instruction is executed.
  70. */
  71. static void rm7k_wait_irqoff(void)
  72. {
  73. if (!need_resched())
  74. __asm__(
  75. " .set push \n"
  76. " .set arch=r4000 \n"
  77. " .set noat \n"
  78. " mfc0 $1, $12 \n"
  79. " sync \n"
  80. " mtc0 $1, $12 # stalls until W stage \n"
  81. " wait \n"
  82. " mtc0 $1, $12 # stalls until W stage \n"
  83. " .set pop \n");
  84. local_irq_enable();
  85. }
  86. /*
  87. * Au1 'wait' is only useful when the 32kHz counter is used as timer,
  88. * since coreclock (and the cp0 counter) stops upon executing it. Only an
  89. * interrupt can wake it, so they must be enabled before entering idle modes.
  90. */
  91. static void au1k_wait(void)
  92. {
  93. unsigned long c0status = read_c0_status() | 1; /* irqs on */
  94. __asm__(
  95. " .set arch=r4000 \n"
  96. " cache 0x14, 0(%0) \n"
  97. " cache 0x14, 32(%0) \n"
  98. " sync \n"
  99. " mtc0 %1, $12 \n" /* wr c0status */
  100. " wait \n"
  101. " nop \n"
  102. " nop \n"
  103. " nop \n"
  104. " nop \n"
  105. " .set mips0 \n"
  106. : : "r" (au1k_wait), "r" (c0status));
  107. }
  108. static int __initdata nowait;
  109. static int __init wait_disable(char *s)
  110. {
  111. nowait = 1;
  112. return 1;
  113. }
  114. __setup("nowait", wait_disable);
  115. void __init check_wait(void)
  116. {
  117. struct cpuinfo_mips *c = &current_cpu_data;
  118. if (nowait) {
  119. printk("Wait instruction disabled.\n");
  120. return;
  121. }
  122. /*
  123. * MIPSr6 specifies that masked interrupts should unblock an executing
  124. * wait instruction, and thus that it is safe for us to use
  125. * r4k_wait_irqoff. Yippee!
  126. */
  127. if (cpu_has_mips_r6) {
  128. cpu_wait = r4k_wait_irqoff;
  129. return;
  130. }
  131. switch (current_cpu_type()) {
  132. case CPU_R3081:
  133. case CPU_R3081E:
  134. cpu_wait = r3081_wait;
  135. break;
  136. case CPU_TX3927:
  137. cpu_wait = r39xx_wait;
  138. break;
  139. case CPU_R4200:
  140. /* case CPU_R4300: */
  141. case CPU_R4600:
  142. case CPU_R4640:
  143. case CPU_R4650:
  144. case CPU_R4700:
  145. case CPU_R5000:
  146. case CPU_R5500:
  147. case CPU_NEVADA:
  148. case CPU_4KC:
  149. case CPU_4KEC:
  150. case CPU_4KSC:
  151. case CPU_5KC:
  152. case CPU_5KE:
  153. case CPU_25KF:
  154. case CPU_PR4450:
  155. case CPU_BMIPS3300:
  156. case CPU_BMIPS4350:
  157. case CPU_BMIPS4380:
  158. case CPU_CAVIUM_OCTEON:
  159. case CPU_CAVIUM_OCTEON_PLUS:
  160. case CPU_CAVIUM_OCTEON2:
  161. case CPU_CAVIUM_OCTEON3:
  162. case CPU_JZRISC:
  163. case CPU_LOONGSON1:
  164. case CPU_XLR:
  165. case CPU_XLP:
  166. cpu_wait = r4k_wait;
  167. break;
  168. case CPU_LOONGSON3:
  169. if ((c->processor_id & PRID_REV_MASK) >= PRID_REV_LOONGSON3A_R2)
  170. cpu_wait = r4k_wait;
  171. break;
  172. case CPU_BMIPS5000:
  173. cpu_wait = r4k_wait_irqoff;
  174. break;
  175. case CPU_RM7000:
  176. cpu_wait = rm7k_wait_irqoff;
  177. break;
  178. case CPU_PROAPTIV:
  179. case CPU_P5600:
  180. /*
  181. * Incoming Fast Debug Channel (FDC) data during a wait
  182. * instruction causes the wait never to resume, even if an
  183. * interrupt is received. Avoid using wait at all if FDC data is
  184. * likely to be received.
  185. */
  186. if (IS_ENABLED(CONFIG_MIPS_EJTAG_FDC_TTY))
  187. break;
  188. /* fall through */
  189. case CPU_M14KC:
  190. case CPU_M14KEC:
  191. case CPU_24K:
  192. case CPU_34K:
  193. case CPU_1004K:
  194. case CPU_1074K:
  195. case CPU_INTERAPTIV:
  196. case CPU_M5150:
  197. case CPU_QEMU_GENERIC:
  198. cpu_wait = r4k_wait;
  199. if (read_c0_config7() & MIPS_CONF7_WII)
  200. cpu_wait = r4k_wait_irqoff;
  201. break;
  202. case CPU_74K:
  203. cpu_wait = r4k_wait;
  204. if ((c->processor_id & 0xff) >= PRID_REV_ENCODE_332(2, 1, 0))
  205. cpu_wait = r4k_wait_irqoff;
  206. break;
  207. case CPU_TX49XX:
  208. cpu_wait = r4k_wait_irqoff;
  209. break;
  210. case CPU_ALCHEMY:
  211. cpu_wait = au1k_wait;
  212. break;
  213. case CPU_20KC:
  214. /*
  215. * WAIT on Rev1.0 has E1, E2, E3 and E16.
  216. * WAIT on Rev2.0 and Rev3.0 has E16.
  217. * Rev3.1 WAIT is nop, why bother
  218. */
  219. if ((c->processor_id & 0xff) <= 0x64)
  220. break;
  221. /*
  222. * Another rev is incremeting c0_count at a reduced clock
  223. * rate while in WAIT mode. So we basically have the choice
  224. * between using the cp0 timer as clocksource or avoiding
  225. * the WAIT instruction. Until more details are known,
  226. * disable the use of WAIT for 20Kc entirely.
  227. cpu_wait = r4k_wait;
  228. */
  229. break;
  230. default:
  231. break;
  232. }
  233. }
  234. void arch_cpu_idle(void)
  235. {
  236. if (cpu_wait)
  237. cpu_wait();
  238. else
  239. local_irq_enable();
  240. }
  241. #ifdef CONFIG_CPU_IDLE
  242. int mips_cpuidle_wait_enter(struct cpuidle_device *dev,
  243. struct cpuidle_driver *drv, int index)
  244. {
  245. arch_cpu_idle();
  246. return index;
  247. }
  248. #endif