msm_rd.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 || !rd->open);
  90. if (!rd->open)
  91. return;
  92. n = min(sz, circ_space_to_end(&rd->fifo));
  93. memcpy(fptr, ptr, n);
  94. fifo->head = (fifo->head + n) & (BUF_SZ - 1);
  95. sz -= n;
  96. ptr += n;
  97. wake_up_all(&rd->fifo_event);
  98. }
  99. }
  100. static void rd_write_section(struct msm_rd_state *rd,
  101. enum rd_sect_type type, const void *buf, int sz)
  102. {
  103. rd_write(rd, &type, 4);
  104. rd_write(rd, &sz, 4);
  105. rd_write(rd, buf, sz);
  106. }
  107. static ssize_t rd_read(struct file *file, char __user *buf,
  108. size_t sz, loff_t *ppos)
  109. {
  110. struct msm_rd_state *rd = file->private_data;
  111. struct circ_buf *fifo = &rd->fifo;
  112. const char *fptr = &fifo->buf[fifo->tail];
  113. int n = 0, ret = 0;
  114. mutex_lock(&rd->read_lock);
  115. ret = wait_event_interruptible(rd->fifo_event,
  116. circ_count(&rd->fifo) > 0);
  117. if (ret)
  118. goto out;
  119. n = min_t(int, sz, circ_count_to_end(&rd->fifo));
  120. ret = copy_to_user(buf, fptr, n);
  121. if (ret)
  122. goto out;
  123. fifo->tail = (fifo->tail + n) & (BUF_SZ - 1);
  124. *ppos += n;
  125. wake_up_all(&rd->fifo_event);
  126. out:
  127. mutex_unlock(&rd->read_lock);
  128. if (ret)
  129. return ret;
  130. return n;
  131. }
  132. static int rd_open(struct inode *inode, struct file *file)
  133. {
  134. struct msm_rd_state *rd = inode->i_private;
  135. struct drm_device *dev = rd->dev;
  136. struct msm_drm_private *priv = dev->dev_private;
  137. struct msm_gpu *gpu = priv->gpu;
  138. uint64_t val;
  139. uint32_t gpu_id;
  140. int ret = 0;
  141. mutex_lock(&dev->struct_mutex);
  142. if (rd->open || !gpu) {
  143. ret = -EBUSY;
  144. goto out;
  145. }
  146. file->private_data = rd;
  147. rd->open = true;
  148. /* the parsing tools need to know gpu-id to know which
  149. * register database to load.
  150. */
  151. gpu->funcs->get_param(gpu, MSM_PARAM_GPU_ID, &val);
  152. gpu_id = val;
  153. rd_write_section(rd, RD_GPU_ID, &gpu_id, sizeof(gpu_id));
  154. out:
  155. mutex_unlock(&dev->struct_mutex);
  156. return ret;
  157. }
  158. static int rd_release(struct inode *inode, struct file *file)
  159. {
  160. struct msm_rd_state *rd = inode->i_private;
  161. rd->open = false;
  162. wake_up_all(&rd->fifo_event);
  163. return 0;
  164. }
  165. static const struct file_operations rd_debugfs_fops = {
  166. .owner = THIS_MODULE,
  167. .open = rd_open,
  168. .read = rd_read,
  169. .llseek = no_llseek,
  170. .release = rd_release,
  171. };
  172. int msm_rd_debugfs_init(struct drm_minor *minor)
  173. {
  174. struct msm_drm_private *priv = minor->dev->dev_private;
  175. struct msm_rd_state *rd;
  176. /* only create on first minor: */
  177. if (priv->rd)
  178. return 0;
  179. rd = kzalloc(sizeof(*rd), GFP_KERNEL);
  180. if (!rd)
  181. return -ENOMEM;
  182. rd->dev = minor->dev;
  183. rd->fifo.buf = rd->buf;
  184. mutex_init(&rd->read_lock);
  185. priv->rd = rd;
  186. init_waitqueue_head(&rd->fifo_event);
  187. rd->node = kzalloc(sizeof(*rd->node), GFP_KERNEL);
  188. if (!rd->node)
  189. goto fail;
  190. rd->ent = debugfs_create_file("rd", S_IFREG | S_IRUGO,
  191. minor->debugfs_root, rd, &rd_debugfs_fops);
  192. if (!rd->ent) {
  193. DRM_ERROR("Cannot create /sys/kernel/debug/dri/%s/rd\n",
  194. minor->debugfs_root->d_name.name);
  195. goto fail;
  196. }
  197. rd->node->minor = minor;
  198. rd->node->dent = rd->ent;
  199. rd->node->info_ent = NULL;
  200. mutex_lock(&minor->debugfs_lock);
  201. list_add(&rd->node->list, &minor->debugfs_list);
  202. mutex_unlock(&minor->debugfs_lock);
  203. return 0;
  204. fail:
  205. msm_rd_debugfs_cleanup(minor);
  206. return -1;
  207. }
  208. void msm_rd_debugfs_cleanup(struct drm_minor *minor)
  209. {
  210. struct msm_drm_private *priv = minor->dev->dev_private;
  211. struct msm_rd_state *rd = priv->rd;
  212. if (!rd)
  213. return;
  214. priv->rd = NULL;
  215. debugfs_remove(rd->ent);
  216. if (rd->node) {
  217. mutex_lock(&minor->debugfs_lock);
  218. list_del(&rd->node->list);
  219. mutex_unlock(&minor->debugfs_lock);
  220. kfree(rd->node);
  221. }
  222. mutex_destroy(&rd->read_lock);
  223. kfree(rd);
  224. }
  225. /* called under struct_mutex */
  226. void msm_rd_dump_submit(struct msm_gem_submit *submit)
  227. {
  228. struct drm_device *dev = submit->dev;
  229. struct msm_drm_private *priv = dev->dev_private;
  230. struct msm_rd_state *rd = priv->rd;
  231. char msg[128];
  232. int i, n;
  233. if (!rd->open)
  234. return;
  235. /* writing into fifo is serialized by caller, and
  236. * rd->read_lock is used to serialize the reads
  237. */
  238. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  239. n = snprintf(msg, sizeof(msg), "%.*s/%d: fence=%u",
  240. TASK_COMM_LEN, current->comm, task_pid_nr(current),
  241. submit->fence);
  242. rd_write_section(rd, RD_CMD, msg, ALIGN(n, 4));
  243. /* could be nice to have an option (module-param?) to snapshot
  244. * all the bo's associated with the submit. Handy to see vtx
  245. * buffers, etc. For now just the cmdstream bo's is enough.
  246. */
  247. for (i = 0; i < submit->nr_cmds; i++) {
  248. uint32_t idx = submit->cmd[i].idx;
  249. uint32_t iova = submit->cmd[i].iova;
  250. uint32_t szd = submit->cmd[i].size; /* in dwords */
  251. struct msm_gem_object *obj = submit->bos[idx].obj;
  252. const char *buf = msm_gem_vaddr_locked(&obj->base);
  253. buf += iova - submit->bos[idx].iova;
  254. rd_write_section(rd, RD_GPUADDR,
  255. (uint32_t[2]){ iova, szd * 4 }, 8);
  256. rd_write_section(rd, RD_BUFFER_CONTENTS,
  257. buf, szd * 4);
  258. switch (submit->cmd[i].type) {
  259. case MSM_SUBMIT_CMD_IB_TARGET_BUF:
  260. /* ignore IB-targets, we've logged the buffer, the
  261. * parser tool will follow the IB based on the logged
  262. * buffer/gpuaddr, so nothing more to do.
  263. */
  264. break;
  265. case MSM_SUBMIT_CMD_CTX_RESTORE_BUF:
  266. case MSM_SUBMIT_CMD_BUF:
  267. rd_write_section(rd, RD_CMDSTREAM_ADDR,
  268. (uint32_t[2]){ iova, szd }, 8);
  269. break;
  270. }
  271. }
  272. }
  273. #endif