msm_rd.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2013 Red Hat
  4. * Author: Rob Clark <robdclark@gmail.com>
  5. */
  6. /* For debugging crashes, userspace can:
  7. *
  8. * tail -f /sys/kernel/debug/dri/<minor>/rd > logfile.rd
  9. *
  10. * to log the cmdstream in a format that is understood by freedreno/cffdump
  11. * utility. By comparing the last successfully completed fence #, to the
  12. * cmdstream for the next fence, you can narrow down which process and submit
  13. * caused the gpu crash/lockup.
  14. *
  15. * Additionally:
  16. *
  17. * tail -f /sys/kernel/debug/dri/<minor>/hangrd > logfile.rd
  18. *
  19. * will capture just the cmdstream from submits which triggered a GPU hang.
  20. *
  21. * This bypasses drm_debugfs_create_files() mainly because we need to use
  22. * our own fops for a bit more control. In particular, we don't want to
  23. * do anything if userspace doesn't have the debugfs file open.
  24. *
  25. * The module-param "rd_full", which defaults to false, enables snapshotting
  26. * all (non-written) buffers in the submit, rather than just cmdstream bo's.
  27. * This is useful to capture the contents of (for example) vbo's or textures,
  28. * or shader programs (if not emitted inline in cmdstream).
  29. */
  30. #include <linux/circ_buf.h>
  31. #include <linux/debugfs.h>
  32. #include <linux/kfifo.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/wait.h>
  35. #include <drm/drm_file.h>
  36. #include "msm_drv.h"
  37. #include "msm_gpu.h"
  38. #include "msm_gem.h"
  39. bool rd_full = false;
  40. MODULE_PARM_DESC(rd_full, "If true, $debugfs/.../rd will snapshot all buffer contents");
  41. module_param_named(rd_full, rd_full, bool, 0600);
  42. #ifdef CONFIG_DEBUG_FS
  43. enum rd_sect_type {
  44. RD_NONE,
  45. RD_TEST, /* ascii text */
  46. RD_CMD, /* ascii text */
  47. RD_GPUADDR, /* u32 gpuaddr, u32 size */
  48. RD_CONTEXT, /* raw dump */
  49. RD_CMDSTREAM, /* raw dump */
  50. RD_CMDSTREAM_ADDR, /* gpu addr of cmdstream */
  51. RD_PARAM, /* u32 param_type, u32 param_val, u32 bitlen */
  52. RD_FLUSH, /* empty, clear previous params */
  53. RD_PROGRAM, /* shader program, raw dump */
  54. RD_VERT_SHADER,
  55. RD_FRAG_SHADER,
  56. RD_BUFFER_CONTENTS,
  57. RD_GPU_ID,
  58. RD_CHIP_ID,
  59. };
  60. #define BUF_SZ 512 /* should be power of 2 */
  61. /* space used: */
  62. #define circ_count(circ) \
  63. (CIRC_CNT((circ)->head, (circ)->tail, BUF_SZ))
  64. #define circ_count_to_end(circ) \
  65. (CIRC_CNT_TO_END((circ)->head, (circ)->tail, BUF_SZ))
  66. /* space available: */
  67. #define circ_space(circ) \
  68. (CIRC_SPACE((circ)->head, (circ)->tail, BUF_SZ))
  69. #define circ_space_to_end(circ) \
  70. (CIRC_SPACE_TO_END((circ)->head, (circ)->tail, BUF_SZ))
  71. struct msm_rd_state {
  72. struct drm_device *dev;
  73. bool open;
  74. /* fifo access is synchronized on the producer side by
  75. * write_lock. And read_lock synchronizes the reads
  76. */
  77. struct mutex read_lock, write_lock;
  78. wait_queue_head_t fifo_event;
  79. struct circ_buf fifo;
  80. char buf[BUF_SZ];
  81. };
  82. static void rd_write(struct msm_rd_state *rd, const void *buf, int sz)
  83. {
  84. struct circ_buf *fifo = &rd->fifo;
  85. const char *ptr = buf;
  86. while (sz > 0) {
  87. char *fptr = &fifo->buf[fifo->head];
  88. int n;
  89. wait_event(rd->fifo_event, circ_space(&rd->fifo) > 0 || !rd->open);
  90. if (!rd->open)
  91. return;
  92. /* Note that smp_load_acquire() is not strictly required
  93. * as CIRC_SPACE_TO_END() does not access the tail more
  94. * than once.
  95. */
  96. n = min(sz, circ_space_to_end(&rd->fifo));
  97. memcpy(fptr, ptr, n);
  98. smp_store_release(&fifo->head, (fifo->head + n) & (BUF_SZ - 1));
  99. sz -= n;
  100. ptr += n;
  101. wake_up_all(&rd->fifo_event);
  102. }
  103. }
  104. static void rd_write_section(struct msm_rd_state *rd,
  105. enum rd_sect_type type, const void *buf, int sz)
  106. {
  107. rd_write(rd, &type, 4);
  108. rd_write(rd, &sz, 4);
  109. rd_write(rd, buf, sz);
  110. }
  111. static ssize_t rd_read(struct file *file, char __user *buf,
  112. size_t sz, loff_t *ppos)
  113. {
  114. struct msm_rd_state *rd = file->private_data;
  115. struct circ_buf *fifo = &rd->fifo;
  116. const char *fptr = &fifo->buf[fifo->tail];
  117. int n = 0, ret = 0;
  118. mutex_lock(&rd->read_lock);
  119. ret = wait_event_interruptible(rd->fifo_event,
  120. circ_count(&rd->fifo) > 0);
  121. if (ret)
  122. goto out;
  123. /* Note that smp_load_acquire() is not strictly required
  124. * as CIRC_CNT_TO_END() does not access the head more than
  125. * once.
  126. */
  127. n = min_t(int, sz, circ_count_to_end(&rd->fifo));
  128. if (copy_to_user(buf, fptr, n)) {
  129. ret = -EFAULT;
  130. goto out;
  131. }
  132. smp_store_release(&fifo->tail, (fifo->tail + n) & (BUF_SZ - 1));
  133. *ppos += n;
  134. wake_up_all(&rd->fifo_event);
  135. out:
  136. mutex_unlock(&rd->read_lock);
  137. if (ret)
  138. return ret;
  139. return n;
  140. }
  141. static int rd_open(struct inode *inode, struct file *file)
  142. {
  143. struct msm_rd_state *rd = inode->i_private;
  144. struct drm_device *dev = rd->dev;
  145. struct msm_drm_private *priv = dev->dev_private;
  146. struct msm_gpu *gpu = priv->gpu;
  147. uint64_t val;
  148. uint32_t gpu_id;
  149. uint32_t zero = 0;
  150. int ret = 0;
  151. if (!gpu)
  152. return -ENODEV;
  153. mutex_lock(&gpu->lock);
  154. if (rd->open) {
  155. ret = -EBUSY;
  156. goto out;
  157. }
  158. file->private_data = rd;
  159. rd->open = true;
  160. /* Reset fifo to clear any previously unread data: */
  161. rd->fifo.head = rd->fifo.tail = 0;
  162. /* the parsing tools need to know gpu-id to know which
  163. * register database to load.
  164. *
  165. * Note: These particular params do not require a context
  166. */
  167. gpu->funcs->get_param(gpu, NULL, MSM_PARAM_GPU_ID, &val, &zero);
  168. gpu_id = val;
  169. rd_write_section(rd, RD_GPU_ID, &gpu_id, sizeof(gpu_id));
  170. gpu->funcs->get_param(gpu, NULL, MSM_PARAM_CHIP_ID, &val, &zero);
  171. rd_write_section(rd, RD_CHIP_ID, &val, sizeof(val));
  172. out:
  173. mutex_unlock(&gpu->lock);
  174. return ret;
  175. }
  176. static int rd_release(struct inode *inode, struct file *file)
  177. {
  178. struct msm_rd_state *rd = inode->i_private;
  179. rd->open = false;
  180. wake_up_all(&rd->fifo_event);
  181. return 0;
  182. }
  183. static const struct file_operations rd_debugfs_fops = {
  184. .owner = THIS_MODULE,
  185. .open = rd_open,
  186. .read = rd_read,
  187. .release = rd_release,
  188. };
  189. static void rd_cleanup(struct msm_rd_state *rd)
  190. {
  191. if (!rd)
  192. return;
  193. mutex_destroy(&rd->read_lock);
  194. mutex_destroy(&rd->write_lock);
  195. kfree(rd);
  196. }
  197. static struct msm_rd_state *rd_init(struct drm_minor *minor, const char *name)
  198. {
  199. struct msm_rd_state *rd;
  200. rd = kzalloc_obj(*rd);
  201. if (!rd)
  202. return ERR_PTR(-ENOMEM);
  203. rd->dev = minor->dev;
  204. rd->fifo.buf = rd->buf;
  205. mutex_init(&rd->read_lock);
  206. mutex_init(&rd->write_lock);
  207. init_waitqueue_head(&rd->fifo_event);
  208. debugfs_create_file(name, S_IFREG | S_IRUGO, minor->debugfs_root, rd,
  209. &rd_debugfs_fops);
  210. return rd;
  211. }
  212. int msm_rd_debugfs_init(struct drm_minor *minor)
  213. {
  214. struct msm_drm_private *priv = minor->dev->dev_private;
  215. struct msm_rd_state *rd;
  216. int ret;
  217. if (!priv->gpu_pdev)
  218. return 0;
  219. /* only create on first minor: */
  220. if (priv->rd)
  221. return 0;
  222. rd = rd_init(minor, "rd");
  223. if (IS_ERR(rd)) {
  224. ret = PTR_ERR(rd);
  225. goto fail;
  226. }
  227. priv->rd = rd;
  228. rd = rd_init(minor, "hangrd");
  229. if (IS_ERR(rd)) {
  230. ret = PTR_ERR(rd);
  231. goto fail;
  232. }
  233. priv->hangrd = rd;
  234. return 0;
  235. fail:
  236. msm_rd_debugfs_cleanup(priv);
  237. return ret;
  238. }
  239. void msm_rd_debugfs_cleanup(struct msm_drm_private *priv)
  240. {
  241. rd_cleanup(priv->rd);
  242. priv->rd = NULL;
  243. rd_cleanup(priv->hangrd);
  244. priv->hangrd = NULL;
  245. }
  246. static void snapshot_buf(struct msm_rd_state *rd, struct drm_gem_object *obj,
  247. uint64_t iova, bool full, size_t offset, size_t size)
  248. {
  249. const char *buf;
  250. /*
  251. * Always write the GPUADDR header so can get a complete list of all the
  252. * buffers in the cmd
  253. */
  254. rd_write_section(rd, RD_GPUADDR,
  255. (uint32_t[3]){ iova, size, iova >> 32 }, 12);
  256. if (!full)
  257. return;
  258. buf = msm_gem_get_vaddr_active(obj);
  259. if (IS_ERR(buf))
  260. return;
  261. buf += offset;
  262. rd_write_section(rd, RD_BUFFER_CONTENTS, buf, size);
  263. msm_gem_put_vaddr_locked(obj);
  264. }
  265. /* called under gpu->lock */
  266. void msm_rd_dump_submit(struct msm_rd_state *rd, struct msm_gem_submit *submit,
  267. const char *fmt, ...)
  268. {
  269. extern bool rd_full;
  270. struct task_struct *task;
  271. char msg[256];
  272. int i, n;
  273. if (!rd->open)
  274. return;
  275. mutex_lock(&rd->write_lock);
  276. if (fmt) {
  277. va_list args;
  278. va_start(args, fmt);
  279. n = vscnprintf(msg, sizeof(msg), fmt, args);
  280. va_end(args);
  281. rd_write_section(rd, RD_CMD, msg, ALIGN(n, 4));
  282. }
  283. rcu_read_lock();
  284. task = pid_task(submit->pid, PIDTYPE_PID);
  285. if (task) {
  286. n = scnprintf(msg, sizeof(msg), "%.*s/%d: fence=%u",
  287. TASK_COMM_LEN, task->comm,
  288. pid_nr(submit->pid), submit->seqno);
  289. } else {
  290. n = scnprintf(msg, sizeof(msg), "???/%d: fence=%u",
  291. pid_nr(submit->pid), submit->seqno);
  292. }
  293. rcu_read_unlock();
  294. rd_write_section(rd, RD_CMD, msg, ALIGN(n, 4));
  295. if (msm_context_is_vmbind(submit->queue->ctx)) {
  296. struct drm_gpuva *vma;
  297. drm_gpuvm_resv_assert_held(submit->vm);
  298. drm_gpuvm_for_each_va (vma, submit->vm) {
  299. bool dump = rd_full || (vma->flags & MSM_VMA_DUMP);
  300. /* Skip MAP_NULL/PRR VMAs: */
  301. if (!vma->gem.obj)
  302. continue;
  303. snapshot_buf(rd, vma->gem.obj, vma->va.addr, dump,
  304. vma->gem.offset, vma->va.range);
  305. }
  306. } else {
  307. for (i = 0; i < submit->nr_bos; i++) {
  308. struct drm_gem_object *obj = submit->bos[i].obj;
  309. bool dump = rd_full || (submit->bos[i].flags & MSM_SUBMIT_BO_DUMP);
  310. snapshot_buf(rd, obj, submit->bos[i].iova, dump, 0, obj->size);
  311. }
  312. for (i = 0; i < submit->nr_cmds; i++) {
  313. uint32_t szd = submit->cmd[i].size; /* in dwords */
  314. int idx = submit->cmd[i].idx;
  315. bool dump = rd_full || (submit->bos[idx].flags & MSM_SUBMIT_BO_DUMP);
  316. /* snapshot cmdstream bo's (if we haven't already): */
  317. if (!dump) {
  318. struct drm_gem_object *obj = submit->bos[idx].obj;
  319. size_t offset = submit->cmd[i].iova - submit->bos[idx].iova;
  320. snapshot_buf(rd, obj, submit->cmd[i].iova, true,
  321. offset, szd * 4);
  322. }
  323. }
  324. }
  325. for (i = 0; i < submit->nr_cmds; i++) {
  326. uint64_t iova = submit->cmd[i].iova;
  327. uint32_t szd = submit->cmd[i].size; /* in dwords */
  328. switch (submit->cmd[i].type) {
  329. case MSM_SUBMIT_CMD_IB_TARGET_BUF:
  330. /* ignore IB-targets, we've logged the buffer, the
  331. * parser tool will follow the IB based on the logged
  332. * buffer/gpuaddr, so nothing more to do.
  333. */
  334. break;
  335. case MSM_SUBMIT_CMD_CTX_RESTORE_BUF:
  336. case MSM_SUBMIT_CMD_BUF:
  337. rd_write_section(rd, RD_CMDSTREAM_ADDR,
  338. (uint32_t[3]){ iova, szd, iova >> 32 }, 12);
  339. break;
  340. }
  341. }
  342. mutex_unlock(&rd->write_lock);
  343. }
  344. #endif