idle.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. switch (current_cpu_type()) {
  123. case CPU_R3081:
  124. case CPU_R3081E:
  125. cpu_wait = r3081_wait;
  126. break;
  127. case CPU_TX3927:
  128. cpu_wait = r39xx_wait;
  129. break;
  130. case CPU_R4200:
  131. /* case CPU_R4300: */
  132. case CPU_R4600:
  133. case CPU_R4640:
  134. case CPU_R4650:
  135. case CPU_R4700:
  136. case CPU_R5000:
  137. case CPU_R5500:
  138. case CPU_NEVADA:
  139. case CPU_4KC:
  140. case CPU_4KEC:
  141. case CPU_4KSC:
  142. case CPU_5KC:
  143. case CPU_25KF:
  144. case CPU_PR4450:
  145. case CPU_BMIPS3300:
  146. case CPU_BMIPS4350:
  147. case CPU_BMIPS4380:
  148. case CPU_BMIPS5000:
  149. case CPU_CAVIUM_OCTEON:
  150. case CPU_CAVIUM_OCTEON_PLUS:
  151. case CPU_CAVIUM_OCTEON2:
  152. case CPU_CAVIUM_OCTEON3:
  153. case CPU_JZRISC:
  154. case CPU_LOONGSON1:
  155. case CPU_XLR:
  156. case CPU_XLP:
  157. cpu_wait = r4k_wait;
  158. break;
  159. case CPU_RM7000:
  160. cpu_wait = rm7k_wait_irqoff;
  161. break;
  162. case CPU_PROAPTIV:
  163. case CPU_P5600:
  164. /*
  165. * Incoming Fast Debug Channel (FDC) data during a wait
  166. * instruction causes the wait never to resume, even if an
  167. * interrupt is received. Avoid using wait at all if FDC data is
  168. * likely to be received.
  169. */
  170. if (IS_ENABLED(CONFIG_MIPS_EJTAG_FDC_TTY))
  171. break;
  172. /* fall through */
  173. case CPU_M14KC:
  174. case CPU_M14KEC:
  175. case CPU_24K:
  176. case CPU_34K:
  177. case CPU_1004K:
  178. case CPU_1074K:
  179. case CPU_INTERAPTIV:
  180. case CPU_M5150:
  181. case CPU_QEMU_GENERIC:
  182. cpu_wait = r4k_wait;
  183. if (read_c0_config7() & MIPS_CONF7_WII)
  184. cpu_wait = r4k_wait_irqoff;
  185. break;
  186. case CPU_74K:
  187. cpu_wait = r4k_wait;
  188. if ((c->processor_id & 0xff) >= PRID_REV_ENCODE_332(2, 1, 0))
  189. cpu_wait = r4k_wait_irqoff;
  190. break;
  191. case CPU_TX49XX:
  192. cpu_wait = r4k_wait_irqoff;
  193. break;
  194. case CPU_ALCHEMY:
  195. cpu_wait = au1k_wait;
  196. break;
  197. case CPU_20KC:
  198. /*
  199. * WAIT on Rev1.0 has E1, E2, E3 and E16.
  200. * WAIT on Rev2.0 and Rev3.0 has E16.
  201. * Rev3.1 WAIT is nop, why bother
  202. */
  203. if ((c->processor_id & 0xff) <= 0x64)
  204. break;
  205. /*
  206. * Another rev is incremeting c0_count at a reduced clock
  207. * rate while in WAIT mode. So we basically have the choice
  208. * between using the cp0 timer as clocksource or avoiding
  209. * the WAIT instruction. Until more details are known,
  210. * disable the use of WAIT for 20Kc entirely.
  211. cpu_wait = r4k_wait;
  212. */
  213. break;
  214. default:
  215. break;
  216. }
  217. }
  218. void arch_cpu_idle(void)
  219. {
  220. if (cpu_wait)
  221. cpu_wait();
  222. else
  223. local_irq_enable();
  224. }
  225. #ifdef CONFIG_CPU_IDLE
  226. int mips_cpuidle_wait_enter(struct cpuidle_device *dev,
  227. struct cpuidle_driver *drv, int index)
  228. {
  229. arch_cpu_idle();
  230. return index;
  231. }
  232. #endif