r4k-bugs64.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2003, 2004, 2007 Maciej W. Rozycki
  4. */
  5. #include <linux/context_tracking.h>
  6. #include <linux/init.h>
  7. #include <linux/kernel.h>
  8. #include <linux/ptrace.h>
  9. #include <linux/stddef.h>
  10. #include <asm/bugs.h>
  11. #include <asm/compiler.h>
  12. #include <asm/cpu.h>
  13. #include <asm/fpu.h>
  14. #include <asm/mipsregs.h>
  15. #include <asm/setup.h>
  16. static char bug64hit[] __initdata =
  17. "reliable operation impossible!\n%s";
  18. static char nowar[] __initdata =
  19. "Please report to <[email protected]>.";
  20. static char r4kwar[] __initdata =
  21. "Enable CPU_R4000_WORKAROUNDS to rectify.";
  22. static char daddiwar[] __initdata =
  23. "Enable CPU_DADDI_WORKAROUNDS to rectify.";
  24. static __always_inline __init
  25. void align_mod(const int align, const int mod)
  26. {
  27. asm volatile(
  28. ".set push\n\t"
  29. ".set noreorder\n\t"
  30. ".balign %0\n\t"
  31. ".rept %1\n\t"
  32. "nop\n\t"
  33. ".endr\n\t"
  34. ".set pop"
  35. :
  36. : "n"(align), "n"(mod));
  37. }
  38. static __always_inline __init
  39. void mult_sh_align_mod(long *v1, long *v2, long *w,
  40. const int align, const int mod)
  41. {
  42. unsigned long flags;
  43. int m1, m2;
  44. long p, s, lv1, lv2, lw;
  45. /*
  46. * We want the multiply and the shift to be isolated from the
  47. * rest of the code to disable gcc optimizations. Hence the
  48. * asm statements that execute nothing, but make gcc not know
  49. * what the values of m1, m2 and s are and what lv2 and p are
  50. * used for.
  51. */
  52. local_irq_save(flags);
  53. /*
  54. * The following code leads to a wrong result of the first
  55. * dsll32 when executed on R4000 rev. 2.2 or 3.0 (PRId
  56. * 00000422 or 00000430, respectively).
  57. *
  58. * See "MIPS R4000PC/SC Errata, Processor Revision 2.2 and
  59. * 3.0" by MIPS Technologies, Inc., errata #16 and #28 for
  60. * details. I got no permission to duplicate them here,
  61. * sigh... --macro
  62. */
  63. asm volatile(
  64. ""
  65. : "=r" (m1), "=r" (m2), "=r" (s)
  66. : "0" (5), "1" (8), "2" (5));
  67. align_mod(align, mod);
  68. /*
  69. * The trailing nop is needed to fulfill the two-instruction
  70. * requirement between reading hi/lo and staring a mult/div.
  71. * Leaving it out may cause gas insert a nop itself breaking
  72. * the desired alignment of the next chunk.
  73. */
  74. asm volatile(
  75. ".set push\n\t"
  76. ".set noat\n\t"
  77. ".set noreorder\n\t"
  78. ".set nomacro\n\t"
  79. "mult %2, %3\n\t"
  80. "dsll32 %0, %4, %5\n\t"
  81. "mflo $0\n\t"
  82. "dsll32 %1, %4, %5\n\t"
  83. "nop\n\t"
  84. ".set pop"
  85. : "=&r" (lv1), "=r" (lw)
  86. : "r" (m1), "r" (m2), "r" (s), "I" (0)
  87. : "hi", "lo", "$0");
  88. /* We have to use single integers for m1 and m2 and a double
  89. * one for p to be sure the mulsidi3 gcc's RTL multiplication
  90. * instruction has the workaround applied. Older versions of
  91. * gcc have correct umulsi3 and mulsi3, but other
  92. * multiplication variants lack the workaround.
  93. */
  94. asm volatile(
  95. ""
  96. : "=r" (m1), "=r" (m2), "=r" (s)
  97. : "0" (m1), "1" (m2), "2" (s));
  98. align_mod(align, mod);
  99. p = m1 * m2;
  100. lv2 = s << 32;
  101. asm volatile(
  102. ""
  103. : "=r" (lv2)
  104. : "0" (lv2), "r" (p));
  105. local_irq_restore(flags);
  106. *v1 = lv1;
  107. *v2 = lv2;
  108. *w = lw;
  109. }
  110. static __always_inline __init void check_mult_sh(void)
  111. {
  112. long v1[8], v2[8], w[8];
  113. int bug, fix, i;
  114. printk("Checking for the multiply/shift bug... ");
  115. /*
  116. * Testing discovered false negatives for certain code offsets
  117. * into cache lines. Hence we test all possible offsets for
  118. * the worst assumption of an R4000 I-cache line width of 32
  119. * bytes.
  120. *
  121. * We can't use a loop as alignment directives need to be
  122. * immediates.
  123. */
  124. mult_sh_align_mod(&v1[0], &v2[0], &w[0], 32, 0);
  125. mult_sh_align_mod(&v1[1], &v2[1], &w[1], 32, 1);
  126. mult_sh_align_mod(&v1[2], &v2[2], &w[2], 32, 2);
  127. mult_sh_align_mod(&v1[3], &v2[3], &w[3], 32, 3);
  128. mult_sh_align_mod(&v1[4], &v2[4], &w[4], 32, 4);
  129. mult_sh_align_mod(&v1[5], &v2[5], &w[5], 32, 5);
  130. mult_sh_align_mod(&v1[6], &v2[6], &w[6], 32, 6);
  131. mult_sh_align_mod(&v1[7], &v2[7], &w[7], 32, 7);
  132. bug = 0;
  133. for (i = 0; i < 8; i++)
  134. if (v1[i] != w[i])
  135. bug = 1;
  136. if (bug == 0) {
  137. pr_cont("no.\n");
  138. return;
  139. }
  140. pr_cont("yes, workaround... ");
  141. fix = 1;
  142. for (i = 0; i < 8; i++)
  143. if (v2[i] != w[i])
  144. fix = 0;
  145. if (fix == 1) {
  146. pr_cont("yes.\n");
  147. return;
  148. }
  149. pr_cont("no.\n");
  150. panic(bug64hit,
  151. IS_ENABLED(CONFIG_CPU_R4000_WORKAROUNDS) ? nowar : r4kwar);
  152. }
  153. static volatile int daddi_ov;
  154. asmlinkage void __init do_daddi_ov(struct pt_regs *regs)
  155. {
  156. enum ctx_state prev_state;
  157. prev_state = exception_enter();
  158. daddi_ov = 1;
  159. regs->cp0_epc += 4;
  160. exception_exit(prev_state);
  161. }
  162. static __init void check_daddi(void)
  163. {
  164. extern asmlinkage void handle_daddi_ov(void);
  165. unsigned long flags;
  166. void *handler;
  167. long v, tmp;
  168. printk("Checking for the daddi bug... ");
  169. local_irq_save(flags);
  170. handler = set_except_vector(EXCCODE_OV, handle_daddi_ov);
  171. /*
  172. * The following code fails to trigger an overflow exception
  173. * when executed on R4000 rev. 2.2 or 3.0 (PRId 00000422 or
  174. * 00000430, respectively).
  175. *
  176. * See "MIPS R4000PC/SC Errata, Processor Revision 2.2 and
  177. * 3.0" by MIPS Technologies, Inc., erratum #23 for details.
  178. * I got no permission to duplicate it here, sigh... --macro
  179. */
  180. asm volatile(
  181. ".set push\n\t"
  182. ".set noat\n\t"
  183. ".set noreorder\n\t"
  184. ".set nomacro\n\t"
  185. "addiu %1, $0, %2\n\t"
  186. "dsrl %1, %1, 1\n\t"
  187. #ifdef HAVE_AS_SET_DADDI
  188. ".set daddi\n\t"
  189. #endif
  190. "daddi %0, %1, %3\n\t"
  191. ".set pop"
  192. : "=r" (v), "=&r" (tmp)
  193. : "I" (0xffffffffffffdb9aUL), "I" (0x1234));
  194. set_except_vector(EXCCODE_OV, handler);
  195. local_irq_restore(flags);
  196. if (daddi_ov) {
  197. pr_cont("no.\n");
  198. return;
  199. }
  200. pr_cont("yes, workaround... ");
  201. local_irq_save(flags);
  202. handler = set_except_vector(EXCCODE_OV, handle_daddi_ov);
  203. asm volatile(
  204. "addiu %1, $0, %2\n\t"
  205. "dsrl %1, %1, 1\n\t"
  206. "daddi %0, %1, %3"
  207. : "=r" (v), "=&r" (tmp)
  208. : "I" (0xffffffffffffdb9aUL), "I" (0x1234));
  209. set_except_vector(EXCCODE_OV, handler);
  210. local_irq_restore(flags);
  211. if (daddi_ov) {
  212. pr_cont("yes.\n");
  213. return;
  214. }
  215. pr_cont("no.\n");
  216. panic(bug64hit,
  217. IS_ENABLED(CONFIG_CPU_DADDI_WORKAROUNDS) ? nowar : daddiwar);
  218. }
  219. int daddiu_bug = -1;
  220. static __init void check_daddiu(void)
  221. {
  222. long v, w, tmp;
  223. printk("Checking for the daddiu bug... ");
  224. /*
  225. * The following code leads to a wrong result of daddiu when
  226. * executed on R4400 rev. 1.0 (PRId 00000440).
  227. *
  228. * See "MIPS R4400PC/SC Errata, Processor Revision 1.0" by
  229. * MIPS Technologies, Inc., erratum #7 for details.
  230. *
  231. * According to "MIPS R4000PC/SC Errata, Processor Revision
  232. * 2.2 and 3.0" by MIPS Technologies, Inc., erratum #41 this
  233. * problem affects R4000 rev. 2.2 and 3.0 (PRId 00000422 and
  234. * 00000430, respectively), too. Testing failed to trigger it
  235. * so far.
  236. *
  237. * I got no permission to duplicate the errata here, sigh...
  238. * --macro
  239. */
  240. asm volatile(
  241. ".set push\n\t"
  242. ".set noat\n\t"
  243. ".set noreorder\n\t"
  244. ".set nomacro\n\t"
  245. "addiu %2, $0, %3\n\t"
  246. "dsrl %2, %2, 1\n\t"
  247. #ifdef HAVE_AS_SET_DADDI
  248. ".set daddi\n\t"
  249. #endif
  250. "daddiu %0, %2, %4\n\t"
  251. "addiu %1, $0, %4\n\t"
  252. "daddu %1, %2\n\t"
  253. ".set pop"
  254. : "=&r" (v), "=&r" (w), "=&r" (tmp)
  255. : "I" (0xffffffffffffdb9aUL), "I" (0x1234));
  256. daddiu_bug = v != w;
  257. if (!daddiu_bug) {
  258. pr_cont("no.\n");
  259. return;
  260. }
  261. pr_cont("yes, workaround... ");
  262. asm volatile(
  263. "addiu %2, $0, %3\n\t"
  264. "dsrl %2, %2, 1\n\t"
  265. "daddiu %0, %2, %4\n\t"
  266. "addiu %1, $0, %4\n\t"
  267. "daddu %1, %2"
  268. : "=&r" (v), "=&r" (w), "=&r" (tmp)
  269. : "I" (0xffffffffffffdb9aUL), "I" (0x1234));
  270. if (v == w) {
  271. pr_cont("yes.\n");
  272. return;
  273. }
  274. pr_cont("no.\n");
  275. panic(bug64hit,
  276. IS_ENABLED(CONFIG_CPU_DADDI_WORKAROUNDS) ? nowar : daddiwar);
  277. }
  278. void __init check_bugs64_early(void)
  279. {
  280. check_mult_sh();
  281. check_daddiu();
  282. }
  283. void __init check_bugs64(void)
  284. {
  285. check_daddi();
  286. }