msm_rd.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. #ifdef CONFIG_DEBUG_FS
  31. #include <linux/kfifo.h>
  32. #include <linux/debugfs.h>
  33. #include <linux/circ_buf.h>
  34. #include <linux/wait.h>
  35. #include "msm_drv.h"
  36. #include "msm_gpu.h"
  37. #include "msm_gem.h"
  38. enum rd_sect_type {
  39. RD_NONE,
  40. RD_TEST, /* ascii text */
  41. RD_CMD, /* ascii text */
  42. RD_GPUADDR, /* u32 gpuaddr, u32 size */
  43. RD_CONTEXT, /* raw dump */
  44. RD_CMDSTREAM, /* raw dump */
  45. RD_CMDSTREAM_ADDR, /* gpu addr of cmdstream */
  46. RD_PARAM, /* u32 param_type, u32 param_val, u32 bitlen */
  47. RD_FLUSH, /* empty, clear previous params */
  48. RD_PROGRAM, /* shader program, raw dump */
  49. RD_VERT_SHADER,
  50. RD_FRAG_SHADER,
  51. RD_BUFFER_CONTENTS,
  52. RD_GPU_ID,
  53. };
  54. #define BUF_SZ 512 /* should be power of 2 */
  55. /* space used: */
  56. #define circ_count(circ) \
  57. (CIRC_CNT((circ)->head, (circ)->tail, BUF_SZ))
  58. #define circ_count_to_end(circ) \
  59. (CIRC_CNT_TO_END((circ)->head, (circ)->tail, BUF_SZ))
  60. /* space available: */
  61. #define circ_space(circ) \
  62. (CIRC_SPACE((circ)->head, (circ)->tail, BUF_SZ))
  63. #define circ_space_to_end(circ) \
  64. (CIRC_SPACE_TO_END((circ)->head, (circ)->tail, BUF_SZ))
  65. struct msm_rd_state {
  66. struct drm_device *dev;
  67. bool open;
  68. struct dentry *ent;
  69. struct drm_info_node *node;
  70. /* current submit to read out: */
  71. struct msm_gem_submit *submit;
  72. /* fifo access is synchronized on the producer side by
  73. * struct_mutex held by submit code (otherwise we could
  74. * end up w/ cmds logged in different order than they
  75. * were executed). And read_lock synchronizes the reads
  76. */
  77. struct mutex read_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);
  90. n = min(sz, circ_space_to_end(&rd->fifo));
  91. memcpy(fptr, ptr, n);
  92. fifo->head = (fifo->head + n) & (BUF_SZ - 1);
  93. sz -= n;
  94. ptr += n;
  95. wake_up_all(&rd->fifo_event);
  96. }
  97. }
  98. static void rd_write_section(struct msm_rd_state *rd,
  99. enum rd_sect_type type, const void *buf, int sz)
  100. {
  101. rd_write(rd, &type, 4);
  102. rd_write(rd, &sz, 4);
  103. rd_write(rd, buf, sz);
  104. }
  105. static ssize_t rd_read(struct file *file, char __user *buf,
  106. size_t sz, loff_t *ppos)
  107. {
  108. struct msm_rd_state *rd = file->private_data;
  109. struct circ_buf *fifo = &rd->fifo;
  110. const char *fptr = &fifo->buf[fifo->tail];
  111. int n = 0, ret = 0;
  112. mutex_lock(&rd->read_lock);
  113. ret = wait_event_interruptible(rd->fifo_event,
  114. circ_count(&rd->fifo) > 0);
  115. if (ret)
  116. goto out;
  117. n = min_t(int, sz, circ_count_to_end(&rd->fifo));
  118. ret = copy_to_user(buf, fptr, n);
  119. if (ret)
  120. goto out;
  121. fifo->tail = (fifo->tail + n) & (BUF_SZ - 1);
  122. *ppos += n;
  123. wake_up_all(&rd->fifo_event);
  124. out:
  125. mutex_unlock(&rd->read_lock);
  126. if (ret)
  127. return ret;
  128. return n;
  129. }
  130. static int rd_open(struct inode *inode, struct file *file)
  131. {
  132. struct msm_rd_state *rd = inode->i_private;
  133. struct drm_device *dev = rd->dev;
  134. struct msm_drm_private *priv = dev->dev_private;
  135. struct msm_gpu *gpu = priv->gpu;
  136. uint64_t val;
  137. uint32_t gpu_id;
  138. int ret = 0;
  139. mutex_lock(&dev->struct_mutex);
  140. if (rd->open || !gpu) {
  141. ret = -EBUSY;
  142. goto out;
  143. }
  144. file->private_data = rd;
  145. rd->open = true;
  146. /* the parsing tools need to know gpu-id to know which
  147. * register database to load.
  148. */
  149. gpu->funcs->get_param(gpu, MSM_PARAM_GPU_ID, &val);
  150. gpu_id = val;
  151. rd_write_section(rd, RD_GPU_ID, &gpu_id, sizeof(gpu_id));
  152. out:
  153. mutex_unlock(&dev->struct_mutex);
  154. return ret;
  155. }
  156. static int rd_release(struct inode *inode, struct file *file)
  157. {
  158. struct msm_rd_state *rd = inode->i_private;
  159. rd->open = false;
  160. return 0;
  161. }
  162. static const struct file_operations rd_debugfs_fops = {
  163. .owner = THIS_MODULE,
  164. .open = rd_open,
  165. .read = rd_read,
  166. .llseek = no_llseek,
  167. .release = rd_release,
  168. };
  169. int msm_rd_debugfs_init(struct drm_minor *minor)
  170. {
  171. struct msm_drm_private *priv = minor->dev->dev_private;
  172. struct msm_rd_state *rd;
  173. /* only create on first minor: */
  174. if (priv->rd)
  175. return 0;
  176. rd = kzalloc(sizeof(*rd), GFP_KERNEL);
  177. if (!rd)
  178. return -ENOMEM;
  179. rd->dev = minor->dev;
  180. rd->fifo.buf = rd->buf;
  181. mutex_init(&rd->read_lock);
  182. priv->rd = rd;
  183. init_waitqueue_head(&rd->fifo_event);
  184. rd->node = kzalloc(sizeof(*rd->node), GFP_KERNEL);
  185. if (!rd->node)
  186. goto fail;
  187. rd->ent = debugfs_create_file("rd", S_IFREG | S_IRUGO,
  188. minor->debugfs_root, rd, &rd_debugfs_fops);
  189. if (!rd->ent) {
  190. DRM_ERROR("Cannot create /sys/kernel/debug/dri/%s/rd\n",
  191. minor->debugfs_root->d_name.name);
  192. goto fail;
  193. }
  194. rd->node->minor = minor;
  195. rd->node->dent = rd->ent;
  196. rd->node->info_ent = NULL;
  197. mutex_lock(&minor->debugfs_lock);
  198. list_add(&rd->node->list, &minor->debugfs_list);
  199. mutex_unlock(&minor->debugfs_lock);
  200. return 0;
  201. fail:
  202. msm_rd_debugfs_cleanup(minor);
  203. return -1;
  204. }
  205. void msm_rd_debugfs_cleanup(struct drm_minor *minor)
  206. {
  207. struct msm_drm_private *priv = minor->dev->dev_private;
  208. struct msm_rd_state *rd = priv->rd;
  209. if (!rd)
  210. return;
  211. priv->rd = NULL;
  212. debugfs_remove(rd->ent);
  213. if (rd->node) {
  214. mutex_lock(&minor->debugfs_lock);
  215. list_del(&rd->node->list);
  216. mutex_unlock(&minor->debugfs_lock);
  217. kfree(rd->node);
  218. }
  219. mutex_destroy(&rd->read_lock);
  220. kfree(rd);
  221. }
  222. /* called under struct_mutex */
  223. void msm_rd_dump_submit(struct msm_gem_submit *submit)
  224. {
  225. struct drm_device *dev = submit->dev;
  226. struct msm_drm_private *priv = dev->dev_private;
  227. struct msm_rd_state *rd = priv->rd;
  228. char msg[128];
  229. int i, n;
  230. if (!rd->open)
  231. return;
  232. /* writing into fifo is serialized by caller, and
  233. * rd->read_lock is used to serialize the reads
  234. */
  235. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  236. n = snprintf(msg, sizeof(msg), "%.*s/%d: fence=%u",
  237. TASK_COMM_LEN, current->comm, task_pid_nr(current),
  238. submit->fence);
  239. rd_write_section(rd, RD_CMD, msg, ALIGN(n, 4));
  240. /* could be nice to have an option (module-param?) to snapshot
  241. * all the bo's associated with the submit. Handy to see vtx
  242. * buffers, etc. For now just the cmdstream bo's is enough.
  243. */
  244. for (i = 0; i < submit->nr_cmds; i++) {
  245. uint32_t idx = submit->cmd[i].idx;
  246. uint32_t iova = submit->cmd[i].iova;
  247. uint32_t szd = submit->cmd[i].size; /* in dwords */
  248. struct msm_gem_object *obj = submit->bos[idx].obj;
  249. const char *buf = msm_gem_vaddr_locked(&obj->base);
  250. buf += iova - submit->bos[idx].iova;
  251. rd_write_section(rd, RD_GPUADDR,
  252. (uint32_t[2]){ iova, szd * 4 }, 8);
  253. rd_write_section(rd, RD_BUFFER_CONTENTS,
  254. buf, szd * 4);
  255. switch (submit->cmd[i].type) {
  256. case MSM_SUBMIT_CMD_IB_TARGET_BUF:
  257. /* ignore IB-targets, we've logged the buffer, the
  258. * parser tool will follow the IB based on the logged
  259. * buffer/gpuaddr, so nothing more to do.
  260. */
  261. break;
  262. case MSM_SUBMIT_CMD_CTX_RESTORE_BUF:
  263. case MSM_SUBMIT_CMD_BUF:
  264. rd_write_section(rd, RD_CMDSTREAM_ADDR,
  265. (uint32_t[2]){ iova, szd }, 8);
  266. break;
  267. }
  268. }
  269. }
  270. #endif