grukdump.c 5.0 KB

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