idle.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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_BMIPS5000:
  169. cpu_wait = r4k_wait_irqoff;
  170. break;
  171. case CPU_RM7000:
  172. cpu_wait = rm7k_wait_irqoff;
  173. break;
  174. case CPU_PROAPTIV:
  175. case CPU_P5600:
  176. /*
  177. * Incoming Fast Debug Channel (FDC) data during a wait
  178. * instruction causes the wait never to resume, even if an
  179. * interrupt is received. Avoid using wait at all if FDC data is
  180. * likely to be received.
  181. */
  182. if (IS_ENABLED(CONFIG_MIPS_EJTAG_FDC_TTY))
  183. break;
  184. /* fall through */
  185. case CPU_M14KC:
  186. case CPU_M14KEC:
  187. case CPU_24K:
  188. case CPU_34K:
  189. case CPU_1004K:
  190. case CPU_1074K:
  191. case CPU_INTERAPTIV:
  192. case CPU_M5150:
  193. case CPU_QEMU_GENERIC:
  194. cpu_wait = r4k_wait;
  195. if (read_c0_config7() & MIPS_CONF7_WII)
  196. cpu_wait = r4k_wait_irqoff;
  197. break;
  198. case CPU_74K:
  199. cpu_wait = r4k_wait;
  200. if ((c->processor_id & 0xff) >= PRID_REV_ENCODE_332(2, 1, 0))
  201. cpu_wait = r4k_wait_irqoff;
  202. break;
  203. case CPU_TX49XX:
  204. cpu_wait = r4k_wait_irqoff;
  205. break;
  206. case CPU_ALCHEMY:
  207. cpu_wait = au1k_wait;
  208. break;
  209. case CPU_20KC:
  210. /*
  211. * WAIT on Rev1.0 has E1, E2, E3 and E16.
  212. * WAIT on Rev2.0 and Rev3.0 has E16.
  213. * Rev3.1 WAIT is nop, why bother
  214. */
  215. if ((c->processor_id & 0xff) <= 0x64)
  216. break;
  217. /*
  218. * Another rev is incremeting c0_count at a reduced clock
  219. * rate while in WAIT mode. So we basically have the choice
  220. * between using the cp0 timer as clocksource or avoiding
  221. * the WAIT instruction. Until more details are known,
  222. * disable the use of WAIT for 20Kc entirely.
  223. cpu_wait = r4k_wait;
  224. */
  225. break;
  226. default:
  227. break;
  228. }
  229. }
  230. void arch_cpu_idle(void)
  231. {
  232. if (cpu_wait)
  233. cpu_wait();
  234. else
  235. local_irq_enable();
  236. }
  237. #ifdef CONFIG_CPU_IDLE
  238. int mips_cpuidle_wait_enter(struct cpuidle_device *dev,
  239. struct cpuidle_driver *drv, int index)
  240. {
  241. arch_cpu_idle();
  242. return index;
  243. }
  244. #endif