msm_rd.c 9.9 KB

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