idle.c 5.8 KB

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