cpu-bugs64.c 7.4 KB

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