grukdump.c 5.6 KB

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