cpu-bugs64.c 7.5 KB

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