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