gruhandles.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #define CLKS2NSEC(c) ((c) *1000000000 / local_cpu_data->itc_freq)
  29. #else
  30. #include <asm/tsc.h>
  31. #define GRU_OPERATION_TIMEOUT ((cycles_t) tsc_khz*10*1000)
  32. #define CLKS2NSEC(c) ((c) * 1000000 / tsc_khz)
  33. #endif
  34. /* Extract the status field from a kernel handle */
  35. #define GET_MSEG_HANDLE_STATUS(h) (((*(unsigned long *)(h)) >> 16) & 3)
  36. struct mcs_op_statistic mcs_op_statistics[mcsop_last];
  37. static void update_mcs_stats(enum mcs_op op, unsigned long clks)
  38. {
  39. unsigned long nsec;
  40. nsec = CLKS2NSEC(clks);
  41. atomic_long_inc(&mcs_op_statistics[op].count);
  42. atomic_long_add(nsec, &mcs_op_statistics[op].total);
  43. if (mcs_op_statistics[op].max < nsec)
  44. mcs_op_statistics[op].max = nsec;
  45. }
  46. static void start_instruction(void *h)
  47. {
  48. unsigned long *w0 = h;
  49. wmb(); /* setting CMD/STATUS bits must be last */
  50. *w0 = *w0 | 0x20001;
  51. gru_flush_cache(h);
  52. }
  53. static void report_instruction_timeout(void *h)
  54. {
  55. unsigned long goff = GSEGPOFF((unsigned long)h);
  56. char *id = "???";
  57. if (TYPE_IS(CCH, goff))
  58. id = "CCH";
  59. else if (TYPE_IS(TGH, goff))
  60. id = "TGH";
  61. else if (TYPE_IS(TFH, goff))
  62. id = "TFH";
  63. panic(KERN_ALERT "GRU %p (%s) is malfunctioning\n", h, id);
  64. }
  65. static int wait_instruction_complete(void *h, enum mcs_op opc)
  66. {
  67. int status;
  68. unsigned long start_time = get_cycles();
  69. while (1) {
  70. cpu_relax();
  71. status = GET_MSEG_HANDLE_STATUS(h);
  72. if (status != CCHSTATUS_ACTIVE)
  73. break;
  74. if (GRU_OPERATION_TIMEOUT < (get_cycles() - start_time)) {
  75. report_instruction_timeout(h);
  76. start_time = get_cycles();
  77. }
  78. }
  79. if (gru_options & OPT_STATS)
  80. update_mcs_stats(opc, get_cycles() - start_time);
  81. return status;
  82. }
  83. int cch_allocate(struct gru_context_configuration_handle *cch)
  84. {
  85. int ret;
  86. cch->opc = CCHOP_ALLOCATE;
  87. start_instruction(cch);
  88. ret = wait_instruction_complete(cch, cchop_allocate);
  89. /*
  90. * Stop speculation into the GSEG being mapped by the previous ALLOCATE.
  91. * The GSEG memory does not exist until the ALLOCATE completes.
  92. */
  93. sync_core();
  94. return ret;
  95. }
  96. int cch_start(struct gru_context_configuration_handle *cch)
  97. {
  98. cch->opc = CCHOP_START;
  99. start_instruction(cch);
  100. return wait_instruction_complete(cch, cchop_start);
  101. }
  102. int cch_interrupt(struct gru_context_configuration_handle *cch)
  103. {
  104. cch->opc = CCHOP_INTERRUPT;
  105. start_instruction(cch);
  106. return wait_instruction_complete(cch, cchop_interrupt);
  107. }
  108. int cch_deallocate(struct gru_context_configuration_handle *cch)
  109. {
  110. int ret;
  111. cch->opc = CCHOP_DEALLOCATE;
  112. start_instruction(cch);
  113. ret = wait_instruction_complete(cch, cchop_deallocate);
  114. /*
  115. * Stop speculation into the GSEG being unmapped by the previous
  116. * DEALLOCATE.
  117. */
  118. sync_core();
  119. return ret;
  120. }
  121. int cch_interrupt_sync(struct gru_context_configuration_handle
  122. *cch)
  123. {
  124. cch->opc = CCHOP_INTERRUPT_SYNC;
  125. start_instruction(cch);
  126. return wait_instruction_complete(cch, cchop_interrupt_sync);
  127. }
  128. int tgh_invalidate(struct gru_tlb_global_handle *tgh,
  129. unsigned long vaddr, unsigned long vaddrmask,
  130. int asid, int pagesize, int global, int n,
  131. unsigned short ctxbitmap)
  132. {
  133. tgh->vaddr = vaddr;
  134. tgh->asid = asid;
  135. tgh->pagesize = pagesize;
  136. tgh->n = n;
  137. tgh->global = global;
  138. tgh->vaddrmask = vaddrmask;
  139. tgh->ctxbitmap = ctxbitmap;
  140. tgh->opc = TGHOP_TLBINV;
  141. start_instruction(tgh);
  142. return wait_instruction_complete(tgh, tghop_invalidate);
  143. }
  144. int tfh_write_only(struct gru_tlb_fault_handle *tfh,
  145. unsigned long paddr, int gaa,
  146. unsigned long vaddr, int asid, int dirty,
  147. int pagesize)
  148. {
  149. tfh->fillasid = asid;
  150. tfh->fillvaddr = vaddr;
  151. tfh->pfn = paddr >> GRU_PADDR_SHIFT;
  152. tfh->gaa = gaa;
  153. tfh->dirty = dirty;
  154. tfh->pagesize = pagesize;
  155. tfh->opc = TFHOP_WRITE_ONLY;
  156. start_instruction(tfh);
  157. return wait_instruction_complete(tfh, tfhop_write_only);
  158. }
  159. void tfh_write_restart(struct gru_tlb_fault_handle *tfh,
  160. unsigned long paddr, int gaa,
  161. unsigned long vaddr, int asid, int dirty,
  162. int pagesize)
  163. {
  164. tfh->fillasid = asid;
  165. tfh->fillvaddr = vaddr;
  166. tfh->pfn = paddr >> GRU_PADDR_SHIFT;
  167. tfh->gaa = gaa;
  168. tfh->dirty = dirty;
  169. tfh->pagesize = pagesize;
  170. tfh->opc = TFHOP_WRITE_RESTART;
  171. start_instruction(tfh);
  172. }
  173. void tfh_restart(struct gru_tlb_fault_handle *tfh)
  174. {
  175. tfh->opc = TFHOP_RESTART;
  176. start_instruction(tfh);
  177. }
  178. void tfh_user_polling_mode(struct gru_tlb_fault_handle *tfh)
  179. {
  180. tfh->opc = TFHOP_USER_POLLING_MODE;
  181. start_instruction(tfh);
  182. }
  183. void tfh_exception(struct gru_tlb_fault_handle *tfh)
  184. {
  185. tfh->opc = TFHOP_EXCEPTION;
  186. start_instruction(tfh);
  187. }