msm_rd.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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);
  102. /* Note that smp_load_acquire() is not strictly required
  103. * as CIRC_SPACE_TO_END() does not access the tail more
  104. * than once.
  105. */
  106. n = min(sz, circ_space_to_end(&rd->fifo));
  107. memcpy(fptr, ptr, n);
  108. smp_store_release(&fifo->head, (fifo->head + n) & (BUF_SZ - 1));
  109. sz -= n;
  110. ptr += n;
  111. wake_up_all(&rd->fifo_event);
  112. }
  113. }
  114. static void rd_write_section(struct msm_rd_state *rd,
  115. enum rd_sect_type type, const void *buf, int sz)
  116. {
  117. rd_write(rd, &type, 4);
  118. rd_write(rd, &sz, 4);
  119. rd_write(rd, buf, sz);
  120. }
  121. static ssize_t rd_read(struct file *file, char __user *buf,
  122. size_t sz, loff_t *ppos)
  123. {
  124. struct msm_rd_state *rd = file->private_data;
  125. struct circ_buf *fifo = &rd->fifo;
  126. const char *fptr = &fifo->buf[fifo->tail];
  127. int n = 0, ret = 0;
  128. mutex_lock(&rd->read_lock);
  129. ret = wait_event_interruptible(rd->fifo_event,
  130. circ_count(&rd->fifo) > 0);
  131. if (ret)
  132. goto out;
  133. /* Note that smp_load_acquire() is not strictly required
  134. * as CIRC_CNT_TO_END() does not access the head more than
  135. * once.
  136. */
  137. n = min_t(int, sz, circ_count_to_end(&rd->fifo));
  138. if (copy_to_user(buf, fptr, n)) {
  139. ret = -EFAULT;
  140. goto out;
  141. }
  142. smp_store_release(&fifo->tail, (fifo->tail + n) & (BUF_SZ - 1));
  143. *ppos += n;
  144. wake_up_all(&rd->fifo_event);
  145. out:
  146. mutex_unlock(&rd->read_lock);
  147. if (ret)
  148. return ret;
  149. return n;
  150. }
  151. static int rd_open(struct inode *inode, struct file *file)
  152. {
  153. struct msm_rd_state *rd = inode->i_private;
  154. struct drm_device *dev = rd->dev;
  155. struct msm_drm_private *priv = dev->dev_private;
  156. struct msm_gpu *gpu = priv->gpu;
  157. uint64_t val;
  158. uint32_t gpu_id;
  159. int ret = 0;
  160. mutex_lock(&dev->struct_mutex);
  161. if (rd->open || !gpu) {
  162. ret = -EBUSY;
  163. goto out;
  164. }
  165. file->private_data = rd;
  166. rd->open = true;
  167. /* the parsing tools need to know gpu-id to know which
  168. * register database to load.
  169. */
  170. gpu->funcs->get_param(gpu, MSM_PARAM_GPU_ID, &val);
  171. gpu_id = val;
  172. rd_write_section(rd, RD_GPU_ID, &gpu_id, sizeof(gpu_id));
  173. out:
  174. mutex_unlock(&dev->struct_mutex);
  175. return ret;
  176. }
  177. static int rd_release(struct inode *inode, struct file *file)
  178. {
  179. struct msm_rd_state *rd = inode->i_private;
  180. rd->open = false;
  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. .llseek = no_llseek,
  188. .release = rd_release,
  189. };
  190. static void rd_cleanup(struct msm_rd_state *rd)
  191. {
  192. if (!rd)
  193. return;
  194. mutex_destroy(&rd->read_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. struct dentry *ent;
  201. int ret = 0;
  202. rd = kzalloc(sizeof(*rd), GFP_KERNEL);
  203. if (!rd)
  204. return ERR_PTR(-ENOMEM);
  205. rd->dev = minor->dev;
  206. rd->fifo.buf = rd->buf;
  207. mutex_init(&rd->read_lock);
  208. init_waitqueue_head(&rd->fifo_event);
  209. ent = debugfs_create_file(name, S_IFREG | S_IRUGO,
  210. minor->debugfs_root, rd, &rd_debugfs_fops);
  211. if (!ent) {
  212. DRM_ERROR("Cannot create /sys/kernel/debug/dri/%pd/%s\n",
  213. minor->debugfs_root, name);
  214. ret = -ENOMEM;
  215. goto fail;
  216. }
  217. return rd;
  218. fail:
  219. rd_cleanup(rd);
  220. return ERR_PTR(ret);
  221. }
  222. int msm_rd_debugfs_init(struct drm_minor *minor)
  223. {
  224. struct msm_drm_private *priv = minor->dev->dev_private;
  225. struct msm_rd_state *rd;
  226. int ret;
  227. /* only create on first minor: */
  228. if (priv->rd)
  229. return 0;
  230. rd = rd_init(minor, "rd");
  231. if (IS_ERR(rd)) {
  232. ret = PTR_ERR(rd);
  233. goto fail;
  234. }
  235. priv->rd = rd;
  236. rd = rd_init(minor, "hangrd");
  237. if (IS_ERR(rd)) {
  238. ret = PTR_ERR(rd);
  239. goto fail;
  240. }
  241. priv->hangrd = rd;
  242. return 0;
  243. fail:
  244. msm_rd_debugfs_cleanup(priv);
  245. return ret;
  246. }
  247. void msm_rd_debugfs_cleanup(struct msm_drm_private *priv)
  248. {
  249. rd_cleanup(priv->rd);
  250. priv->rd = NULL;
  251. rd_cleanup(priv->hangrd);
  252. priv->hangrd = NULL;
  253. }
  254. static void snapshot_buf(struct msm_rd_state *rd,
  255. struct msm_gem_submit *submit, int idx,
  256. uint64_t iova, uint32_t size)
  257. {
  258. struct msm_gem_object *obj = submit->bos[idx].obj;
  259. const char *buf;
  260. if (iova) {
  261. buf += iova - submit->bos[idx].iova;
  262. } else {
  263. iova = submit->bos[idx].iova;
  264. size = obj->base.size;
  265. }
  266. /*
  267. * Always write the GPUADDR header so can get a complete list of all the
  268. * buffers in the cmd
  269. */
  270. rd_write_section(rd, RD_GPUADDR,
  271. (uint32_t[3]){ iova, size, iova >> 32 }, 12);
  272. /* But only dump the contents of buffers marked READ */
  273. if (!(submit->bos[idx].flags & MSM_SUBMIT_BO_READ))
  274. return;
  275. buf = msm_gem_get_vaddr_active(&obj->base);
  276. if (IS_ERR(buf))
  277. return;
  278. rd_write_section(rd, RD_BUFFER_CONTENTS, buf, size);
  279. msm_gem_put_vaddr(&obj->base);
  280. }
  281. /* called under struct_mutex */
  282. void msm_rd_dump_submit(struct msm_rd_state *rd, struct msm_gem_submit *submit,
  283. const char *fmt, ...)
  284. {
  285. struct drm_device *dev = submit->dev;
  286. struct task_struct *task;
  287. char msg[256];
  288. int i, n;
  289. if (!rd->open)
  290. return;
  291. /* writing into fifo is serialized by caller, and
  292. * rd->read_lock is used to serialize the reads
  293. */
  294. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  295. if (fmt) {
  296. va_list args;
  297. va_start(args, fmt);
  298. n = vsnprintf(msg, sizeof(msg), fmt, args);
  299. va_end(args);
  300. rd_write_section(rd, RD_CMD, msg, ALIGN(n, 4));
  301. }
  302. rcu_read_lock();
  303. task = pid_task(submit->pid, PIDTYPE_PID);
  304. if (task) {
  305. n = snprintf(msg, sizeof(msg), "%.*s/%d: fence=%u",
  306. TASK_COMM_LEN, task->comm,
  307. pid_nr(submit->pid), submit->seqno);
  308. } else {
  309. n = snprintf(msg, sizeof(msg), "???/%d: fence=%u",
  310. pid_nr(submit->pid), submit->seqno);
  311. }
  312. rcu_read_unlock();
  313. rd_write_section(rd, RD_CMD, msg, ALIGN(n, 4));
  314. for (i = 0; rd_full && i < submit->nr_bos; i++)
  315. snapshot_buf(rd, submit, i, 0, 0);
  316. for (i = 0; i < submit->nr_cmds; i++) {
  317. uint64_t iova = submit->cmd[i].iova;
  318. uint32_t szd = submit->cmd[i].size; /* in dwords */
  319. /* snapshot cmdstream bo's (if we haven't already): */
  320. if (!rd_full) {
  321. snapshot_buf(rd, submit, submit->cmd[i].idx,
  322. submit->cmd[i].iova, szd * 4);
  323. }
  324. switch (submit->cmd[i].type) {
  325. case MSM_SUBMIT_CMD_IB_TARGET_BUF:
  326. /* ignore IB-targets, we've logged the buffer, the
  327. * parser tool will follow the IB based on the logged
  328. * buffer/gpuaddr, so nothing more to do.
  329. */
  330. break;
  331. case MSM_SUBMIT_CMD_CTX_RESTORE_BUF:
  332. case MSM_SUBMIT_CMD_BUF:
  333. rd_write_section(rd, RD_CMDSTREAM_ADDR,
  334. (uint32_t[3]){ iova, szd, iova >> 32 }, 12);
  335. break;
  336. }
  337. }
  338. }
  339. #endif