cpu-bugs64.c 7.6 KB

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