s32c1i_selftest.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * S32C1I selftest.
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (C) 2016 Cadence Design Systems Inc.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <asm/traps.h>
  13. #if XCHAL_HAVE_S32C1I
  14. static int __initdata rcw_word, rcw_probe_pc, rcw_exc;
  15. /*
  16. * Basic atomic compare-and-swap, that records PC of S32C1I for probing.
  17. *
  18. * If *v == cmp, set *v = set. Return previous *v.
  19. */
  20. static inline int probed_compare_swap(int *v, int cmp, int set)
  21. {
  22. int tmp;
  23. __asm__ __volatile__(
  24. " movi %1, 1f\n"
  25. " s32i %1, %4, 0\n"
  26. " wsr %2, scompare1\n"
  27. "1: s32c1i %0, %3, 0\n"
  28. : "=a" (set), "=&a" (tmp)
  29. : "a" (cmp), "a" (v), "a" (&rcw_probe_pc), "0" (set)
  30. : "memory"
  31. );
  32. return set;
  33. }
  34. /* Handle probed exception */
  35. static void __init do_probed_exception(struct pt_regs *regs)
  36. {
  37. if (regs->pc == rcw_probe_pc) { /* exception on s32c1i ? */
  38. regs->pc += 3; /* skip the s32c1i instruction */
  39. rcw_exc = regs->exccause;
  40. } else {
  41. do_unhandled(regs);
  42. }
  43. }
  44. /* Simple test of S32C1I (soc bringup assist) */
  45. static int __init check_s32c1i(void)
  46. {
  47. int n, cause1, cause2;
  48. void *handbus, *handdata, *handaddr; /* temporarily saved handlers */
  49. rcw_probe_pc = 0;
  50. handbus = trap_set_handler(EXCCAUSE_LOAD_STORE_ERROR,
  51. do_probed_exception);
  52. handdata = trap_set_handler(EXCCAUSE_LOAD_STORE_DATA_ERROR,
  53. do_probed_exception);
  54. handaddr = trap_set_handler(EXCCAUSE_LOAD_STORE_ADDR_ERROR,
  55. do_probed_exception);
  56. /* First try an S32C1I that does not store: */
  57. rcw_exc = 0;
  58. rcw_word = 1;
  59. n = probed_compare_swap(&rcw_word, 0, 2);
  60. cause1 = rcw_exc;
  61. /* took exception? */
  62. if (cause1 != 0) {
  63. /* unclean exception? */
  64. if (n != 2 || rcw_word != 1)
  65. panic("S32C1I exception error");
  66. } else if (rcw_word != 1 || n != 1) {
  67. panic("S32C1I compare error");
  68. }
  69. /* Then an S32C1I that stores: */
  70. rcw_exc = 0;
  71. rcw_word = 0x1234567;
  72. n = probed_compare_swap(&rcw_word, 0x1234567, 0xabcde);
  73. cause2 = rcw_exc;
  74. if (cause2 != 0) {
  75. /* unclean exception? */
  76. if (n != 0xabcde || rcw_word != 0x1234567)
  77. panic("S32C1I exception error (b)");
  78. } else if (rcw_word != 0xabcde || n != 0x1234567) {
  79. panic("S32C1I store error");
  80. }
  81. /* Verify consistency of exceptions: */
  82. if (cause1 || cause2) {
  83. pr_warn("S32C1I took exception %d, %d\n", cause1, cause2);
  84. /* If emulation of S32C1I upon bus error gets implemented,
  85. * we can get rid of this panic for single core (not SMP)
  86. */
  87. panic("S32C1I exceptions not currently supported");
  88. }
  89. if (cause1 != cause2)
  90. panic("inconsistent S32C1I exceptions");
  91. trap_set_handler(EXCCAUSE_LOAD_STORE_ERROR, handbus);
  92. trap_set_handler(EXCCAUSE_LOAD_STORE_DATA_ERROR, handdata);
  93. trap_set_handler(EXCCAUSE_LOAD_STORE_ADDR_ERROR, handaddr);
  94. return 0;
  95. }
  96. #else /* XCHAL_HAVE_S32C1I */
  97. /* This condition should not occur with a commercially deployed processor.
  98. * Display reminder for early engr test or demo chips / FPGA bitstreams
  99. */
  100. static int __init check_s32c1i(void)
  101. {
  102. pr_warn("Processor configuration lacks atomic compare-and-swap support!\n");
  103. return 0;
  104. }
  105. #endif /* XCHAL_HAVE_S32C1I */
  106. early_initcall(check_s32c1i);