cpu-bugs64.c 7.7 KB

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