gruhandles.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * GRU KERNEL MCS INSTRUCTIONS
  4. *
  5. * Copyright (c) 2008 Silicon Graphics, Inc. All Rights Reserved.
  6. */
  7. #include <linux/kernel.h>
  8. #include "gru.h"
  9. #include "grulib.h"
  10. #include "grutables.h"
  11. /* 10 sec */
  12. #include <linux/sync_core.h>
  13. #include <asm/tsc.h>
  14. #define GRU_OPERATION_TIMEOUT ((cycles_t) tsc_khz*10*1000)
  15. #define CLKS2NSEC(c) ((c) * 1000000 / tsc_khz)
  16. /* Extract the status field from a kernel handle */
  17. #define GET_MSEG_HANDLE_STATUS(h) (((*(unsigned long *)(h)) >> 16) & 3)
  18. struct mcs_op_statistic mcs_op_statistics[mcsop_last];
  19. static void update_mcs_stats(enum mcs_op op, unsigned long clks)
  20. {
  21. unsigned long nsec;
  22. nsec = CLKS2NSEC(clks);
  23. atomic_long_inc(&mcs_op_statistics[op].count);
  24. atomic_long_add(nsec, &mcs_op_statistics[op].total);
  25. if (mcs_op_statistics[op].max < nsec)
  26. mcs_op_statistics[op].max = nsec;
  27. }
  28. static void start_instruction(void *h)
  29. {
  30. unsigned long *w0 = h;
  31. wmb(); /* setting CMD/STATUS bits must be last */
  32. *w0 = *w0 | 0x20001;
  33. gru_flush_cache(h);
  34. }
  35. static void report_instruction_timeout(void *h)
  36. {
  37. unsigned long goff = GSEGPOFF((unsigned long)h);
  38. char *id = "???";
  39. if (TYPE_IS(CCH, goff))
  40. id = "CCH";
  41. else if (TYPE_IS(TGH, goff))
  42. id = "TGH";
  43. else if (TYPE_IS(TFH, goff))
  44. id = "TFH";
  45. panic(KERN_ALERT "GRU %p (%s) is malfunctioning\n", h, id);
  46. }
  47. static int wait_instruction_complete(void *h, enum mcs_op opc)
  48. {
  49. int status;
  50. unsigned long start_time = get_cycles();
  51. while (1) {
  52. cpu_relax();
  53. status = GET_MSEG_HANDLE_STATUS(h);
  54. if (status != CCHSTATUS_ACTIVE)
  55. break;
  56. if (GRU_OPERATION_TIMEOUT < (get_cycles() - start_time)) {
  57. report_instruction_timeout(h);
  58. start_time = get_cycles();
  59. }
  60. }
  61. if (gru_options & OPT_STATS)
  62. update_mcs_stats(opc, get_cycles() - start_time);
  63. return status;
  64. }
  65. int cch_allocate(struct gru_context_configuration_handle *cch)
  66. {
  67. int ret;
  68. cch->opc = CCHOP_ALLOCATE;
  69. start_instruction(cch);
  70. ret = wait_instruction_complete(cch, cchop_allocate);
  71. /*
  72. * Stop speculation into the GSEG being mapped by the previous ALLOCATE.
  73. * The GSEG memory does not exist until the ALLOCATE completes.
  74. */
  75. sync_core();
  76. return ret;
  77. }
  78. int cch_start(struct gru_context_configuration_handle *cch)
  79. {
  80. cch->opc = CCHOP_START;
  81. start_instruction(cch);
  82. return wait_instruction_complete(cch, cchop_start);
  83. }
  84. int cch_interrupt(struct gru_context_configuration_handle *cch)
  85. {
  86. cch->opc = CCHOP_INTERRUPT;
  87. start_instruction(cch);
  88. return wait_instruction_complete(cch, cchop_interrupt);
  89. }
  90. int cch_deallocate(struct gru_context_configuration_handle *cch)
  91. {
  92. int ret;
  93. cch->opc = CCHOP_DEALLOCATE;
  94. start_instruction(cch);
  95. ret = wait_instruction_complete(cch, cchop_deallocate);
  96. /*
  97. * Stop speculation into the GSEG being unmapped by the previous
  98. * DEALLOCATE.
  99. */
  100. sync_core();
  101. return ret;
  102. }
  103. int cch_interrupt_sync(struct gru_context_configuration_handle
  104. *cch)
  105. {
  106. cch->opc = CCHOP_INTERRUPT_SYNC;
  107. start_instruction(cch);
  108. return wait_instruction_complete(cch, cchop_interrupt_sync);
  109. }
  110. int tgh_invalidate(struct gru_tlb_global_handle *tgh,
  111. unsigned long vaddr, unsigned long vaddrmask,
  112. int asid, int pagesize, int global, int n,
  113. unsigned short ctxbitmap)
  114. {
  115. tgh->vaddr = vaddr;
  116. tgh->asid = asid;
  117. tgh->pagesize = pagesize;
  118. tgh->n = n;
  119. tgh->global = global;
  120. tgh->vaddrmask = vaddrmask;
  121. tgh->ctxbitmap = ctxbitmap;
  122. tgh->opc = TGHOP_TLBINV;
  123. start_instruction(tgh);
  124. return wait_instruction_complete(tgh, tghop_invalidate);
  125. }
  126. int tfh_write_only(struct gru_tlb_fault_handle *tfh,
  127. unsigned long paddr, int gaa,
  128. unsigned long vaddr, int asid, int dirty,
  129. int pagesize)
  130. {
  131. tfh->fillasid = asid;
  132. tfh->fillvaddr = vaddr;
  133. tfh->pfn = paddr >> GRU_PADDR_SHIFT;
  134. tfh->gaa = gaa;
  135. tfh->dirty = dirty;
  136. tfh->pagesize = pagesize;
  137. tfh->opc = TFHOP_WRITE_ONLY;
  138. start_instruction(tfh);
  139. return wait_instruction_complete(tfh, tfhop_write_only);
  140. }
  141. void tfh_write_restart(struct gru_tlb_fault_handle *tfh,
  142. unsigned long paddr, int gaa,
  143. unsigned long vaddr, int asid, int dirty,
  144. int pagesize)
  145. {
  146. tfh->fillasid = asid;
  147. tfh->fillvaddr = vaddr;
  148. tfh->pfn = paddr >> GRU_PADDR_SHIFT;
  149. tfh->gaa = gaa;
  150. tfh->dirty = dirty;
  151. tfh->pagesize = pagesize;
  152. tfh->opc = TFHOP_WRITE_RESTART;
  153. start_instruction(tfh);
  154. }
  155. void tfh_user_polling_mode(struct gru_tlb_fault_handle *tfh)
  156. {
  157. tfh->opc = TFHOP_USER_POLLING_MODE;
  158. start_instruction(tfh);
  159. }
  160. void tfh_exception(struct gru_tlb_fault_handle *tfh)
  161. {
  162. tfh->opc = TFHOP_EXCEPTION;
  163. start_instruction(tfh);
  164. }