idle.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. }
  35. void __cpuidle r4k_wait(void)
  36. {
  37. raw_local_irq_enable();
  38. __r4k_wait();
  39. raw_local_irq_disable();
  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. }
  57. /*
  58. * The RM7000 variant has to handle erratum 38. The workaround is to not
  59. * have any pending stores when the WAIT instruction is executed.
  60. */
  61. static void __cpuidle rm7k_wait_irqoff(void)
  62. {
  63. if (!need_resched())
  64. __asm__(
  65. " .set push \n"
  66. " .set arch=r4000 \n"
  67. " .set noat \n"
  68. " mfc0 $1, $12 \n"
  69. " sync \n"
  70. " mtc0 $1, $12 # stalls until W stage \n"
  71. " wait \n"
  72. " mtc0 $1, $12 # stalls until W stage \n"
  73. " .set pop \n");
  74. }
  75. /*
  76. * Au1 'wait' is only useful when the 32kHz counter is used as timer,
  77. * since coreclock (and the cp0 counter) stops upon executing it. Only an
  78. * interrupt can wake it, so they must be enabled before entering idle modes.
  79. */
  80. static void __cpuidle au1k_wait(void)
  81. {
  82. unsigned long c0status = read_c0_status() | 1; /* irqs on */
  83. __asm__(
  84. " .set push \n"
  85. " .set arch=r4000 \n"
  86. " cache 0x14, 0(%0) \n"
  87. " cache 0x14, 32(%0) \n"
  88. " sync \n"
  89. " mtc0 %1, $12 \n" /* wr c0status */
  90. " wait \n"
  91. " nop \n"
  92. " nop \n"
  93. " nop \n"
  94. " nop \n"
  95. " .set pop \n"
  96. : : "r" (au1k_wait), "r" (c0status));
  97. raw_local_irq_disable();
  98. }
  99. static int __initdata nowait;
  100. static int __init wait_disable(char *s)
  101. {
  102. nowait = 1;
  103. return 1;
  104. }
  105. __setup("nowait", wait_disable);
  106. void __init check_wait(void)
  107. {
  108. struct cpuinfo_mips *c = &current_cpu_data;
  109. if (nowait) {
  110. printk("Wait instruction disabled.\n");
  111. return;
  112. }
  113. /*
  114. * MIPSr6 specifies that masked interrupts should unblock an executing
  115. * wait instruction, and thus that it is safe for us to use
  116. * r4k_wait_irqoff. Yippee!
  117. */
  118. if (cpu_has_mips_r6) {
  119. cpu_wait = r4k_wait_irqoff;
  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_R4200:
  128. /* case CPU_R4300: */
  129. case CPU_R4600:
  130. case CPU_R4640:
  131. case CPU_R4650:
  132. case CPU_R4700:
  133. case CPU_R5000:
  134. case CPU_R5500:
  135. case CPU_NEVADA:
  136. case CPU_4KC:
  137. case CPU_4KEC:
  138. case CPU_4KSC:
  139. case CPU_5KC:
  140. case CPU_5KE:
  141. case CPU_25KF:
  142. case CPU_PR4450:
  143. case CPU_BMIPS3300:
  144. case CPU_BMIPS4350:
  145. case CPU_BMIPS4380:
  146. case CPU_CAVIUM_OCTEON:
  147. case CPU_CAVIUM_OCTEON_PLUS:
  148. case CPU_CAVIUM_OCTEON2:
  149. case CPU_CAVIUM_OCTEON3:
  150. case CPU_XBURST:
  151. case CPU_LOONGSON32:
  152. cpu_wait = r4k_wait;
  153. break;
  154. case CPU_LOONGSON64:
  155. if ((c->processor_id & (PRID_IMP_MASK | PRID_REV_MASK)) >=
  156. (PRID_IMP_LOONGSON_64C | PRID_REV_LOONGSON3A_R2_0) ||
  157. (c->processor_id & PRID_IMP_MASK) == PRID_IMP_LOONGSON_64R)
  158. cpu_wait = r4k_wait;
  159. break;
  160. case CPU_BMIPS5000:
  161. cpu_wait = r4k_wait_irqoff;
  162. break;
  163. case CPU_RM7000:
  164. cpu_wait = rm7k_wait_irqoff;
  165. break;
  166. case CPU_PROAPTIV:
  167. case CPU_P5600:
  168. /*
  169. * Incoming Fast Debug Channel (FDC) data during a wait
  170. * instruction causes the wait never to resume, even if an
  171. * interrupt is received. Avoid using wait at all if FDC data is
  172. * likely to be received.
  173. */
  174. if (IS_ENABLED(CONFIG_MIPS_EJTAG_FDC_TTY))
  175. break;
  176. fallthrough;
  177. case CPU_M14KC:
  178. case CPU_M14KEC:
  179. case CPU_24K:
  180. case CPU_34K:
  181. case CPU_1004K:
  182. case CPU_1074K:
  183. case CPU_INTERAPTIV:
  184. case CPU_M5150:
  185. case CPU_QEMU_GENERIC:
  186. cpu_wait = r4k_wait;
  187. if (read_c0_config7() & MIPS_CONF7_WII)
  188. cpu_wait = r4k_wait_irqoff;
  189. break;
  190. case CPU_74K:
  191. cpu_wait = r4k_wait;
  192. if ((c->processor_id & 0xff) >= PRID_REV_ENCODE_332(2, 1, 0))
  193. cpu_wait = r4k_wait_irqoff;
  194. break;
  195. case CPU_TX49XX:
  196. cpu_wait = r4k_wait_irqoff;
  197. break;
  198. case CPU_ALCHEMY:
  199. cpu_wait = au1k_wait;
  200. break;
  201. case CPU_20KC:
  202. /*
  203. * WAIT on Rev1.0 has E1, E2, E3 and E16.
  204. * WAIT on Rev2.0 and Rev3.0 has E16.
  205. * Rev3.1 WAIT is nop, why bother
  206. */
  207. if ((c->processor_id & 0xff) <= 0x64)
  208. break;
  209. /*
  210. * Another rev is incrementing c0_count at a reduced clock
  211. * rate while in WAIT mode. So we basically have the choice
  212. * between using the cp0 timer as clocksource or avoiding
  213. * the WAIT instruction. Until more details are known,
  214. * disable the use of WAIT for 20Kc entirely.
  215. cpu_wait = r4k_wait;
  216. */
  217. break;
  218. default:
  219. break;
  220. }
  221. }
  222. __cpuidle void arch_cpu_idle(void)
  223. {
  224. if (cpu_wait)
  225. cpu_wait();
  226. }
  227. #ifdef CONFIG_CPU_IDLE
  228. __cpuidle int mips_cpuidle_wait_enter(struct cpuidle_device *dev,
  229. struct cpuidle_driver *drv, int index)
  230. {
  231. arch_cpu_idle();
  232. return index;
  233. }
  234. #endif