msm_rd.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. * This bypasses drm_debugfs_create_files() mainly because we need to use
  27. * our own fops for a bit more control. In particular, we don't want to
  28. * do anything if userspace doesn't have the debugfs file open.
  29. *
  30. * The module-param "rd_full", which defaults to false, enables snapshotting
  31. * all (non-written) buffers in the submit, rather than just cmdstream bo's.
  32. * This is useful to capture the contents of (for example) vbo's or textures,
  33. * or shader programs (if not emitted inline in cmdstream).
  34. */
  35. #ifdef CONFIG_DEBUG_FS
  36. #include <linux/kfifo.h>
  37. #include <linux/debugfs.h>
  38. #include <linux/circ_buf.h>
  39. #include <linux/wait.h>
  40. #include "msm_drv.h"
  41. #include "msm_gpu.h"
  42. #include "msm_gem.h"
  43. static bool rd_full = false;
  44. MODULE_PARM_DESC(rd_full, "If true, $debugfs/.../rd will snapshot all buffer contents");
  45. module_param_named(rd_full, rd_full, bool, 0600);
  46. enum rd_sect_type {
  47. RD_NONE,
  48. RD_TEST, /* ascii text */
  49. RD_CMD, /* ascii text */
  50. RD_GPUADDR, /* u32 gpuaddr, u32 size */
  51. RD_CONTEXT, /* raw dump */
  52. RD_CMDSTREAM, /* raw dump */
  53. RD_CMDSTREAM_ADDR, /* gpu addr of cmdstream */
  54. RD_PARAM, /* u32 param_type, u32 param_val, u32 bitlen */
  55. RD_FLUSH, /* empty, clear previous params */
  56. RD_PROGRAM, /* shader program, raw dump */
  57. RD_VERT_SHADER,
  58. RD_FRAG_SHADER,
  59. RD_BUFFER_CONTENTS,
  60. RD_GPU_ID,
  61. };
  62. #define BUF_SZ 512 /* should be power of 2 */
  63. /* space used: */
  64. #define circ_count(circ) \
  65. (CIRC_CNT((circ)->head, (circ)->tail, BUF_SZ))
  66. #define circ_count_to_end(circ) \
  67. (CIRC_CNT_TO_END((circ)->head, (circ)->tail, BUF_SZ))
  68. /* space available: */
  69. #define circ_space(circ) \
  70. (CIRC_SPACE((circ)->head, (circ)->tail, BUF_SZ))
  71. #define circ_space_to_end(circ) \
  72. (CIRC_SPACE_TO_END((circ)->head, (circ)->tail, BUF_SZ))
  73. struct msm_rd_state {
  74. struct drm_device *dev;
  75. bool open;
  76. /* current submit to read out: */
  77. struct msm_gem_submit *submit;
  78. /* fifo access is synchronized on the producer side by
  79. * struct_mutex held by submit code (otherwise we could
  80. * end up w/ cmds logged in different order than they
  81. * were executed). And read_lock synchronizes the reads
  82. */
  83. struct mutex read_lock;
  84. wait_queue_head_t fifo_event;
  85. struct circ_buf fifo;
  86. char buf[BUF_SZ];
  87. };
  88. static void rd_write(struct msm_rd_state *rd, const void *buf, int sz)
  89. {
  90. struct circ_buf *fifo = &rd->fifo;
  91. const char *ptr = buf;
  92. while (sz > 0) {
  93. char *fptr = &fifo->buf[fifo->head];
  94. int n;
  95. wait_event(rd->fifo_event, circ_space(&rd->fifo) > 0);
  96. n = min(sz, circ_space_to_end(&rd->fifo));
  97. memcpy(fptr, ptr, n);
  98. 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. n = min_t(int, sz, circ_count_to_end(&rd->fifo));
  124. if (copy_to_user(buf, fptr, n)) {
  125. ret = -EFAULT;
  126. goto out;
  127. }
  128. fifo->tail = (fifo->tail + n) & (BUF_SZ - 1);
  129. *ppos += n;
  130. wake_up_all(&rd->fifo_event);
  131. out:
  132. mutex_unlock(&rd->read_lock);
  133. if (ret)
  134. return ret;
  135. return n;
  136. }
  137. static int rd_open(struct inode *inode, struct file *file)
  138. {
  139. struct msm_rd_state *rd = inode->i_private;
  140. struct drm_device *dev = rd->dev;
  141. struct msm_drm_private *priv = dev->dev_private;
  142. struct msm_gpu *gpu = priv->gpu;
  143. uint64_t val;
  144. uint32_t gpu_id;
  145. int ret = 0;
  146. mutex_lock(&dev->struct_mutex);
  147. if (rd->open || !gpu) {
  148. ret = -EBUSY;
  149. goto out;
  150. }
  151. file->private_data = rd;
  152. rd->open = true;
  153. /* the parsing tools need to know gpu-id to know which
  154. * register database to load.
  155. */
  156. gpu->funcs->get_param(gpu, MSM_PARAM_GPU_ID, &val);
  157. gpu_id = val;
  158. rd_write_section(rd, RD_GPU_ID, &gpu_id, sizeof(gpu_id));
  159. out:
  160. mutex_unlock(&dev->struct_mutex);
  161. return ret;
  162. }
  163. static int rd_release(struct inode *inode, struct file *file)
  164. {
  165. struct msm_rd_state *rd = inode->i_private;
  166. rd->open = false;
  167. return 0;
  168. }
  169. static const struct file_operations rd_debugfs_fops = {
  170. .owner = THIS_MODULE,
  171. .open = rd_open,
  172. .read = rd_read,
  173. .llseek = no_llseek,
  174. .release = rd_release,
  175. };
  176. int msm_rd_debugfs_init(struct drm_minor *minor)
  177. {
  178. struct msm_drm_private *priv = minor->dev->dev_private;
  179. struct msm_rd_state *rd;
  180. struct dentry *ent;
  181. /* only create on first minor: */
  182. if (priv->rd)
  183. return 0;
  184. rd = kzalloc(sizeof(*rd), GFP_KERNEL);
  185. if (!rd)
  186. return -ENOMEM;
  187. rd->dev = minor->dev;
  188. rd->fifo.buf = rd->buf;
  189. mutex_init(&rd->read_lock);
  190. priv->rd = rd;
  191. init_waitqueue_head(&rd->fifo_event);
  192. ent = debugfs_create_file("rd", S_IFREG | S_IRUGO,
  193. minor->debugfs_root, rd, &rd_debugfs_fops);
  194. if (!ent) {
  195. DRM_ERROR("Cannot create /sys/kernel/debug/dri/%pd/rd\n",
  196. minor->debugfs_root);
  197. goto fail;
  198. }
  199. return 0;
  200. fail:
  201. msm_rd_debugfs_cleanup(priv);
  202. return -1;
  203. }
  204. void msm_rd_debugfs_cleanup(struct msm_drm_private *priv)
  205. {
  206. struct msm_rd_state *rd = priv->rd;
  207. if (!rd)
  208. return;
  209. priv->rd = NULL;
  210. mutex_destroy(&rd->read_lock);
  211. kfree(rd);
  212. }
  213. static void snapshot_buf(struct msm_rd_state *rd,
  214. struct msm_gem_submit *submit, int idx,
  215. uint64_t iova, uint32_t size)
  216. {
  217. struct msm_gem_object *obj = submit->bos[idx].obj;
  218. const char *buf;
  219. buf = msm_gem_get_vaddr_locked(&obj->base);
  220. if (IS_ERR(buf))
  221. return;
  222. if (iova) {
  223. buf += iova - submit->bos[idx].iova;
  224. } else {
  225. iova = submit->bos[idx].iova;
  226. size = obj->base.size;
  227. }
  228. rd_write_section(rd, RD_GPUADDR,
  229. (uint32_t[3]){ iova, size, iova >> 32 }, 12);
  230. rd_write_section(rd, RD_BUFFER_CONTENTS, buf, size);
  231. msm_gem_put_vaddr_locked(&obj->base);
  232. }
  233. /* called under struct_mutex */
  234. void msm_rd_dump_submit(struct msm_gem_submit *submit)
  235. {
  236. struct drm_device *dev = submit->dev;
  237. struct msm_drm_private *priv = dev->dev_private;
  238. struct msm_rd_state *rd = priv->rd;
  239. char msg[128];
  240. int i, n;
  241. if (!rd->open)
  242. return;
  243. /* writing into fifo is serialized by caller, and
  244. * rd->read_lock is used to serialize the reads
  245. */
  246. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  247. n = snprintf(msg, sizeof(msg), "%.*s/%d: fence=%u",
  248. TASK_COMM_LEN, current->comm, task_pid_nr(current),
  249. submit->fence->seqno);
  250. rd_write_section(rd, RD_CMD, msg, ALIGN(n, 4));
  251. if (rd_full) {
  252. for (i = 0; i < submit->nr_bos; i++) {
  253. /* buffers that are written to probably don't start out
  254. * with anything interesting:
  255. */
  256. if (submit->bos[i].flags & MSM_SUBMIT_BO_WRITE)
  257. continue;
  258. snapshot_buf(rd, submit, i, 0, 0);
  259. }
  260. }
  261. for (i = 0; i < submit->nr_cmds; i++) {
  262. uint32_t iova = submit->cmd[i].iova;
  263. uint32_t szd = submit->cmd[i].size; /* in dwords */
  264. /* snapshot cmdstream bo's (if we haven't already): */
  265. if (!rd_full) {
  266. snapshot_buf(rd, submit, submit->cmd[i].idx,
  267. submit->cmd[i].iova, szd * 4);
  268. }
  269. switch (submit->cmd[i].type) {
  270. case MSM_SUBMIT_CMD_IB_TARGET_BUF:
  271. /* ignore IB-targets, we've logged the buffer, the
  272. * parser tool will follow the IB based on the logged
  273. * buffer/gpuaddr, so nothing more to do.
  274. */
  275. break;
  276. case MSM_SUBMIT_CMD_CTX_RESTORE_BUF:
  277. case MSM_SUBMIT_CMD_BUF:
  278. rd_write_section(rd, RD_CMDSTREAM_ADDR,
  279. (uint32_t[2]){ iova, szd }, 8);
  280. break;
  281. }
  282. }
  283. }
  284. #endif