s32c1i_selftest.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. unsigned long exccause)
  37. {
  38. if (regs->pc == rcw_probe_pc) { /* exception on s32c1i ? */
  39. regs->pc += 3; /* skip the s32c1i instruction */
  40. rcw_exc = exccause;
  41. } else {
  42. do_unhandled(regs, exccause);
  43. }
  44. }
  45. /* Simple test of S32C1I (soc bringup assist) */
  46. static int __init check_s32c1i(void)
  47. {
  48. int n, cause1, cause2;
  49. void *handbus, *handdata, *handaddr; /* temporarily saved handlers */
  50. rcw_probe_pc = 0;
  51. handbus = trap_set_handler(EXCCAUSE_LOAD_STORE_ERROR,
  52. do_probed_exception);
  53. handdata = trap_set_handler(EXCCAUSE_LOAD_STORE_DATA_ERROR,
  54. do_probed_exception);
  55. handaddr = trap_set_handler(EXCCAUSE_LOAD_STORE_ADDR_ERROR,
  56. do_probed_exception);
  57. /* First try an S32C1I that does not store: */
  58. rcw_exc = 0;
  59. rcw_word = 1;
  60. n = probed_compare_swap(&rcw_word, 0, 2);
  61. cause1 = rcw_exc;
  62. /* took exception? */
  63. if (cause1 != 0) {
  64. /* unclean exception? */
  65. if (n != 2 || rcw_word != 1)
  66. panic("S32C1I exception error");
  67. } else if (rcw_word != 1 || n != 1) {
  68. panic("S32C1I compare error");
  69. }
  70. /* Then an S32C1I that stores: */
  71. rcw_exc = 0;
  72. rcw_word = 0x1234567;
  73. n = probed_compare_swap(&rcw_word, 0x1234567, 0xabcde);
  74. cause2 = rcw_exc;
  75. if (cause2 != 0) {
  76. /* unclean exception? */
  77. if (n != 0xabcde || rcw_word != 0x1234567)
  78. panic("S32C1I exception error (b)");
  79. } else if (rcw_word != 0xabcde || n != 0x1234567) {
  80. panic("S32C1I store error");
  81. }
  82. /* Verify consistency of exceptions: */
  83. if (cause1 || cause2) {
  84. pr_warn("S32C1I took exception %d, %d\n", cause1, cause2);
  85. /* If emulation of S32C1I upon bus error gets implemented,
  86. * we can get rid of this panic for single core (not SMP)
  87. */
  88. panic("S32C1I exceptions not currently supported");
  89. }
  90. if (cause1 != cause2)
  91. panic("inconsistent S32C1I exceptions");
  92. trap_set_handler(EXCCAUSE_LOAD_STORE_ERROR, handbus);
  93. trap_set_handler(EXCCAUSE_LOAD_STORE_DATA_ERROR, handdata);
  94. trap_set_handler(EXCCAUSE_LOAD_STORE_ADDR_ERROR, handaddr);
  95. return 0;
  96. }
  97. #else /* XCHAL_HAVE_S32C1I */
  98. /* This condition should not occur with a commercially deployed processor.
  99. * Display reminder for early engr test or demo chips / FPGA bitstreams
  100. */
  101. static int __init check_s32c1i(void)
  102. {
  103. pr_warn("Processor configuration lacks atomic compare-and-swap support!\n");
  104. return 0;
  105. }
  106. #endif /* XCHAL_HAVE_S32C1I */
  107. early_initcall(check_s32c1i);