gruhandles.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * GRU KERNEL MCS INSTRUCTIONS
  3. *
  4. * Copyright (c) 2008 Silicon Graphics, Inc. All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/kernel.h>
  21. #include "gru.h"
  22. #include "grulib.h"
  23. #include "grutables.h"
  24. /* 10 sec */
  25. #ifdef CONFIG_IA64
  26. #include <asm/processor.h>
  27. #define GRU_OPERATION_TIMEOUT (((cycles_t) local_cpu_data->itc_freq)*10)
  28. #else
  29. #include <asm/tsc.h>
  30. #define GRU_OPERATION_TIMEOUT ((cycles_t) tsc_khz*10*1000)
  31. #endif
  32. /* Extract the status field from a kernel handle */
  33. #define GET_MSEG_HANDLE_STATUS(h) (((*(unsigned long *)(h)) >> 16) & 3)
  34. struct mcs_op_statistic mcs_op_statistics[mcsop_last];
  35. static void update_mcs_stats(enum mcs_op op, unsigned long clks)
  36. {
  37. atomic_long_inc(&mcs_op_statistics[op].count);
  38. atomic_long_add(clks, &mcs_op_statistics[op].total);
  39. if (mcs_op_statistics[op].max < clks)
  40. mcs_op_statistics[op].max = clks;
  41. }
  42. static void start_instruction(void *h)
  43. {
  44. unsigned long *w0 = h;
  45. wmb(); /* setting CMD bit must be last */
  46. *w0 = *w0 | 1;
  47. gru_flush_cache(h);
  48. }
  49. static void report_instruction_timeout(void *h)
  50. {
  51. unsigned long goff = GSEGPOFF((unsigned long)h);
  52. char *id = "???";
  53. if (TYPE_IS(CCH, goff))
  54. id = "CCH";
  55. else if (TYPE_IS(TGH, goff))
  56. id = "TGH";
  57. else if (TYPE_IS(TFH, goff))
  58. id = "TFH";
  59. panic(KERN_ALERT "GRU %p (%s) is malfunctioning\n", h, id);
  60. }
  61. static int wait_instruction_complete(void *h, enum mcs_op opc)
  62. {
  63. int status;
  64. unsigned long start_time = get_cycles();
  65. while (1) {
  66. cpu_relax();
  67. status = GET_MSEG_HANDLE_STATUS(h);
  68. if (status != CCHSTATUS_ACTIVE)
  69. break;
  70. if (GRU_OPERATION_TIMEOUT < (get_cycles() - start_time)) {
  71. report_instruction_timeout(h);
  72. start_time = get_cycles();
  73. }
  74. }
  75. if (gru_options & OPT_STATS)
  76. update_mcs_stats(opc, get_cycles() - start_time);
  77. return status;
  78. }
  79. int cch_allocate(struct gru_context_configuration_handle *cch)
  80. {
  81. int ret;
  82. cch->opc = CCHOP_ALLOCATE;
  83. start_instruction(cch);
  84. ret = wait_instruction_complete(cch, cchop_allocate);
  85. /*
  86. * Stop speculation into the GSEG being mapped by the previous ALLOCATE.
  87. * The GSEG memory does not exist until the ALLOCATE completes.
  88. */
  89. sync_core();
  90. return ret;
  91. }
  92. int cch_start(struct gru_context_configuration_handle *cch)
  93. {
  94. cch->opc = CCHOP_START;
  95. start_instruction(cch);
  96. return wait_instruction_complete(cch, cchop_start);
  97. }
  98. int cch_interrupt(struct gru_context_configuration_handle *cch)
  99. {
  100. cch->opc = CCHOP_INTERRUPT;
  101. start_instruction(cch);
  102. return wait_instruction_complete(cch, cchop_interrupt);
  103. }
  104. int cch_deallocate(struct gru_context_configuration_handle *cch)
  105. {
  106. int ret;
  107. cch->opc = CCHOP_DEALLOCATE;
  108. start_instruction(cch);
  109. ret = wait_instruction_complete(cch, cchop_deallocate);
  110. /*
  111. * Stop speculation into the GSEG being unmapped by the previous
  112. * DEALLOCATE.
  113. */
  114. sync_core();
  115. return ret;
  116. }
  117. int cch_interrupt_sync(struct gru_context_configuration_handle
  118. *cch)
  119. {
  120. cch->opc = CCHOP_INTERRUPT_SYNC;
  121. start_instruction(cch);
  122. return wait_instruction_complete(cch, cchop_interrupt_sync);
  123. }
  124. int tgh_invalidate(struct gru_tlb_global_handle *tgh,
  125. unsigned long vaddr, unsigned long vaddrmask,
  126. int asid, int pagesize, int global, int n,
  127. unsigned short ctxbitmap)
  128. {
  129. tgh->vaddr = vaddr;
  130. tgh->asid = asid;
  131. tgh->pagesize = pagesize;
  132. tgh->n = n;
  133. tgh->global = global;
  134. tgh->vaddrmask = vaddrmask;
  135. tgh->ctxbitmap = ctxbitmap;
  136. tgh->opc = TGHOP_TLBINV;
  137. start_instruction(tgh);
  138. return wait_instruction_complete(tgh, tghop_invalidate);
  139. }
  140. void tfh_write_only(struct gru_tlb_fault_handle *tfh,
  141. unsigned long pfn, unsigned long vaddr,
  142. int asid, int dirty, int pagesize)
  143. {
  144. tfh->fillasid = asid;
  145. tfh->fillvaddr = vaddr;
  146. tfh->pfn = pfn;
  147. tfh->dirty = dirty;
  148. tfh->pagesize = pagesize;
  149. tfh->opc = TFHOP_WRITE_ONLY;
  150. start_instruction(tfh);
  151. }
  152. void tfh_write_restart(struct gru_tlb_fault_handle *tfh,
  153. unsigned long paddr, int gaa,
  154. unsigned long vaddr, int asid, int dirty,
  155. int pagesize)
  156. {
  157. tfh->fillasid = asid;
  158. tfh->fillvaddr = vaddr;
  159. tfh->pfn = paddr >> GRU_PADDR_SHIFT;
  160. tfh->gaa = gaa;
  161. tfh->dirty = dirty;
  162. tfh->pagesize = pagesize;
  163. tfh->opc = TFHOP_WRITE_RESTART;
  164. start_instruction(tfh);
  165. }
  166. void tfh_restart(struct gru_tlb_fault_handle *tfh)
  167. {
  168. tfh->opc = TFHOP_RESTART;
  169. start_instruction(tfh);
  170. }
  171. void tfh_user_polling_mode(struct gru_tlb_fault_handle *tfh)
  172. {
  173. tfh->opc = TFHOP_USER_POLLING_MODE;
  174. start_instruction(tfh);
  175. }
  176. void tfh_exception(struct gru_tlb_fault_handle *tfh)
  177. {
  178. tfh->opc = TFHOP_EXCEPTION;
  179. start_instruction(tfh);
  180. }