grukdump.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * SN Platform GRU Driver
  3. *
  4. * Dump GRU State
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. *
  10. * Copyright (c) 2008 Silicon Graphics, Inc. All Rights Reserved.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/mm.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/delay.h>
  17. #include <linux/bitops.h>
  18. #include <asm/uv/uv_hub.h>
  19. #include "gru.h"
  20. #include "grutables.h"
  21. #include "gruhandles.h"
  22. #include "grulib.h"
  23. #define CCH_LOCK_ATTEMPTS 10
  24. static int gru_user_copy_handle(void __user **dp, void *s)
  25. {
  26. if (copy_to_user(dp, s, GRU_HANDLE_BYTES))
  27. return -1;
  28. *dp += GRU_HANDLE_BYTES;
  29. return 0;
  30. }
  31. static int gru_dump_context_data(void *grubase,
  32. struct gru_context_configuration_handle *cch,
  33. void __user *ubuf, int ctxnum, int dsrcnt)
  34. {
  35. void *cb, *cbe, *tfh, *gseg;
  36. int i, scr;
  37. gseg = grubase + ctxnum * GRU_GSEG_STRIDE;
  38. cb = gseg + GRU_CB_BASE;
  39. cbe = grubase + GRU_CBE_BASE;
  40. tfh = grubase + GRU_TFH_BASE;
  41. for_each_cbr_in_allocation_map(i, &cch->cbr_allocation_map, scr) {
  42. if (gru_user_copy_handle(&ubuf, cb))
  43. goto fail;
  44. if (gru_user_copy_handle(&ubuf, tfh + i * GRU_HANDLE_STRIDE))
  45. goto fail;
  46. if (gru_user_copy_handle(&ubuf, cbe + i * GRU_HANDLE_STRIDE))
  47. goto fail;
  48. cb += GRU_HANDLE_STRIDE;
  49. }
  50. if (dsrcnt)
  51. memcpy(ubuf, gseg + GRU_DS_BASE, dsrcnt * GRU_HANDLE_STRIDE);
  52. return 0;
  53. fail:
  54. return -EFAULT;
  55. }
  56. static int gru_dump_tfm(struct gru_state *gru,
  57. void __user *ubuf, void __user *ubufend)
  58. {
  59. struct gru_tlb_fault_map *tfm;
  60. int i, ret, bytes;
  61. bytes = GRU_NUM_TFM * GRU_CACHE_LINE_BYTES;
  62. if (bytes > ubufend - ubuf)
  63. ret = -EFBIG;
  64. for (i = 0; i < GRU_NUM_TFM; i++) {
  65. tfm = get_tfm(gru->gs_gru_base_vaddr, i);
  66. if (gru_user_copy_handle(&ubuf, tfm))
  67. goto fail;
  68. }
  69. return GRU_NUM_TFM * GRU_CACHE_LINE_BYTES;
  70. fail:
  71. return -EFAULT;
  72. }
  73. static int gru_dump_tgh(struct gru_state *gru,
  74. void __user *ubuf, void __user *ubufend)
  75. {
  76. struct gru_tlb_global_handle *tgh;
  77. int i, ret, bytes;
  78. bytes = GRU_NUM_TGH * GRU_CACHE_LINE_BYTES;
  79. if (bytes > ubufend - ubuf)
  80. ret = -EFBIG;
  81. for (i = 0; i < GRU_NUM_TGH; i++) {
  82. tgh = get_tgh(gru->gs_gru_base_vaddr, i);
  83. if (gru_user_copy_handle(&ubuf, tgh))
  84. goto fail;
  85. }
  86. return GRU_NUM_TGH * GRU_CACHE_LINE_BYTES;
  87. fail:
  88. return -EFAULT;
  89. }
  90. static int gru_dump_context(struct gru_state *gru, int ctxnum,
  91. void __user *ubuf, void __user *ubufend, char data_opt,
  92. char lock_cch)
  93. {
  94. struct gru_dump_context_header hdr;
  95. struct gru_dump_context_header __user *uhdr = ubuf;
  96. struct gru_context_configuration_handle *cch;
  97. struct gru_thread_state *gts;
  98. int try, cch_locked, cbrcnt = 0, dsrcnt = 0, bytes = 0, ret = 0;
  99. void *grubase;
  100. memset(&hdr, 0, sizeof(hdr));
  101. grubase = gru->gs_gru_base_vaddr;
  102. cch = get_cch(grubase, ctxnum);
  103. for (try = 0; try < CCH_LOCK_ATTEMPTS; try++) {
  104. cch_locked = trylock_cch_handle(cch);
  105. if (cch_locked)
  106. break;
  107. msleep(1);
  108. }
  109. ubuf += sizeof(hdr);
  110. if (gru_user_copy_handle(&ubuf, cch))
  111. goto fail;
  112. bytes = sizeof(hdr) + GRU_CACHE_LINE_BYTES;
  113. if (cch_locked || !lock_cch) {
  114. gts = gru->gs_gts[ctxnum];
  115. if (gts && gts->ts_vma) {
  116. hdr.pid = gts->ts_tgid_owner;
  117. hdr.vaddr = gts->ts_vma->vm_start;
  118. }
  119. if (cch->state != CCHSTATE_INACTIVE) {
  120. cbrcnt = hweight64(cch->cbr_allocation_map) *
  121. GRU_CBR_AU_SIZE;
  122. dsrcnt = data_opt ? hweight32(cch->dsr_allocation_map) *
  123. GRU_DSR_AU_CL : 0;
  124. }
  125. bytes += (3 * cbrcnt + dsrcnt) * GRU_CACHE_LINE_BYTES;
  126. if (bytes > ubufend - ubuf)
  127. ret = -EFBIG;
  128. else
  129. ret = gru_dump_context_data(grubase, cch, ubuf, ctxnum,
  130. dsrcnt);
  131. }
  132. if (cch_locked)
  133. unlock_cch_handle(cch);
  134. if (ret)
  135. return ret;
  136. hdr.magic = GRU_DUMP_MAGIC;
  137. hdr.ctxnum = ctxnum;
  138. hdr.cbrcnt = cbrcnt;
  139. hdr.dsrcnt = dsrcnt;
  140. hdr.cch_locked = cch_locked;
  141. if (!ret && copy_to_user((void __user *)uhdr, &hdr, sizeof(hdr)))
  142. ret = -EFAULT;
  143. return ret ? ret : bytes;
  144. fail:
  145. unlock_cch_handle(cch);
  146. return -EFAULT;
  147. }
  148. int gru_dump_chiplet_request(unsigned long arg)
  149. {
  150. struct gru_state *gru;
  151. struct gru_dump_chiplet_state_req req;
  152. void __user *ubuf;
  153. void __user *ubufend;
  154. int ctxnum, ret, cnt = 0;
  155. if (copy_from_user(&req, (void __user *)arg, sizeof(req)))
  156. return -EFAULT;
  157. /* Currently, only dump by gid is implemented */
  158. if (req.gid >= gru_max_gids || req.gid < 0)
  159. return -EINVAL;
  160. gru = GID_TO_GRU(req.gid);
  161. ubuf = req.buf;
  162. ubufend = req.buf + req.buflen;
  163. ret = gru_dump_tfm(gru, ubuf, ubufend);
  164. if (ret < 0)
  165. goto fail;
  166. ubuf += ret;
  167. ret = gru_dump_tgh(gru, ubuf, ubufend);
  168. if (ret < 0)
  169. goto fail;
  170. ubuf += ret;
  171. for (ctxnum = 0; ctxnum < GRU_NUM_CCH; ctxnum++) {
  172. if (req.ctxnum == ctxnum || req.ctxnum < 0) {
  173. ret = gru_dump_context(gru, ctxnum, ubuf, ubufend,
  174. req.data_opt, req.lock_cch);
  175. if (ret < 0)
  176. goto fail;
  177. ubuf += ret;
  178. cnt++;
  179. }
  180. }
  181. if (copy_to_user((void __user *)arg, &req, sizeof(req)))
  182. return -EFAULT;
  183. return cnt;
  184. fail:
  185. return ret;
  186. }