drm_debugfs_crc.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright © 2008 Intel Corporation
  3. * Copyright © 2016 Collabora Ltd
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. *
  24. * Based on code from the i915 driver.
  25. * Original author: Damien Lespiau <damien.lespiau@intel.com>
  26. *
  27. */
  28. #include <linux/circ_buf.h>
  29. #include <linux/ctype.h>
  30. #include <linux/debugfs.h>
  31. #include <drm/drmP.h>
  32. #include "drm_internal.h"
  33. /**
  34. * DOC: CRC ABI
  35. *
  36. * DRM device drivers can provide to userspace CRC information of each frame as
  37. * it reached a given hardware component (a "source").
  38. *
  39. * Userspace can control generation of CRCs in a given CRTC by writing to the
  40. * file dri/0/crtc-N/crc/control in debugfs, with N being the index of the CRTC.
  41. * Accepted values are source names (which are driver-specific) and the "auto"
  42. * keyword, which will let the driver select a default source of frame CRCs
  43. * for this CRTC.
  44. *
  45. * Once frame CRC generation is enabled, userspace can capture them by reading
  46. * the dri/0/crtc-N/crc/data file. Each line in that file contains the frame
  47. * number in the first field and then a number of unsigned integer fields
  48. * containing the CRC data. Fields are separated by a single space and the number
  49. * of CRC fields is source-specific.
  50. *
  51. * Note that though in some cases the CRC is computed in a specified way and on
  52. * the frame contents as supplied by userspace (eDP 1.3), in general the CRC
  53. * computation is performed in an unspecified way and on frame contents that have
  54. * been already processed in also an unspecified way and thus userspace cannot
  55. * rely on being able to generate matching CRC values for the frame contents that
  56. * it submits. In this general case, the maximum userspace can do is to compare
  57. * the reported CRCs of frames that should have the same contents.
  58. */
  59. static int crc_control_show(struct seq_file *m, void *data)
  60. {
  61. struct drm_crtc *crtc = m->private;
  62. seq_printf(m, "%s\n", crtc->crc.source);
  63. return 0;
  64. }
  65. static int crc_control_open(struct inode *inode, struct file *file)
  66. {
  67. struct drm_crtc *crtc = inode->i_private;
  68. return single_open(file, crc_control_show, crtc);
  69. }
  70. static ssize_t crc_control_write(struct file *file, const char __user *ubuf,
  71. size_t len, loff_t *offp)
  72. {
  73. struct seq_file *m = file->private_data;
  74. struct drm_crtc *crtc = m->private;
  75. struct drm_crtc_crc *crc = &crtc->crc;
  76. char *source;
  77. if (len == 0)
  78. return 0;
  79. if (len > PAGE_SIZE - 1) {
  80. DRM_DEBUG_KMS("Expected < %lu bytes into crtc crc control\n",
  81. PAGE_SIZE);
  82. return -E2BIG;
  83. }
  84. source = memdup_user_nul(ubuf, len);
  85. if (IS_ERR(source))
  86. return PTR_ERR(source);
  87. if (source[len] == '\n')
  88. source[len] = '\0';
  89. spin_lock_irq(&crc->lock);
  90. if (crc->opened) {
  91. spin_unlock_irq(&crc->lock);
  92. kfree(source);
  93. return -EBUSY;
  94. }
  95. kfree(crc->source);
  96. crc->source = source;
  97. spin_unlock_irq(&crc->lock);
  98. *offp += len;
  99. return len;
  100. }
  101. static const struct file_operations drm_crtc_crc_control_fops = {
  102. .owner = THIS_MODULE,
  103. .open = crc_control_open,
  104. .read = seq_read,
  105. .llseek = seq_lseek,
  106. .release = single_release,
  107. .write = crc_control_write
  108. };
  109. static int crtc_crc_open(struct inode *inode, struct file *filep)
  110. {
  111. struct drm_crtc *crtc = inode->i_private;
  112. struct drm_crtc_crc *crc = &crtc->crc;
  113. struct drm_crtc_crc_entry *entries = NULL;
  114. size_t values_cnt;
  115. int ret;
  116. if (crc->opened)
  117. return -EBUSY;
  118. ret = crtc->funcs->set_crc_source(crtc, crc->source, &values_cnt);
  119. if (ret)
  120. return ret;
  121. if (WARN_ON(values_cnt > DRM_MAX_CRC_NR)) {
  122. ret = -EINVAL;
  123. goto err_disable;
  124. }
  125. if (WARN_ON(values_cnt == 0)) {
  126. ret = -EINVAL;
  127. goto err_disable;
  128. }
  129. entries = kcalloc(DRM_CRC_ENTRIES_NR, sizeof(*entries), GFP_KERNEL);
  130. if (!entries) {
  131. ret = -ENOMEM;
  132. goto err_disable;
  133. }
  134. spin_lock_irq(&crc->lock);
  135. crc->entries = entries;
  136. crc->values_cnt = values_cnt;
  137. crc->opened = true;
  138. spin_unlock_irq(&crc->lock);
  139. return 0;
  140. err_disable:
  141. crtc->funcs->set_crc_source(crtc, NULL, &values_cnt);
  142. return ret;
  143. }
  144. static int crtc_crc_release(struct inode *inode, struct file *filep)
  145. {
  146. struct drm_crtc *crtc = filep->f_inode->i_private;
  147. struct drm_crtc_crc *crc = &crtc->crc;
  148. size_t values_cnt;
  149. spin_lock_irq(&crc->lock);
  150. kfree(crc->entries);
  151. crc->entries = NULL;
  152. crc->head = 0;
  153. crc->tail = 0;
  154. crc->values_cnt = 0;
  155. crc->opened = false;
  156. spin_unlock_irq(&crc->lock);
  157. crtc->funcs->set_crc_source(crtc, NULL, &values_cnt);
  158. return 0;
  159. }
  160. static int crtc_crc_data_count(struct drm_crtc_crc *crc)
  161. {
  162. assert_spin_locked(&crc->lock);
  163. return CIRC_CNT(crc->head, crc->tail, DRM_CRC_ENTRIES_NR);
  164. }
  165. /*
  166. * 1 frame field of 10 chars plus a number of CRC fields of 10 chars each, space
  167. * separated, with a newline at the end and null-terminated.
  168. */
  169. #define LINE_LEN(values_cnt) (10 + 11 * values_cnt + 1 + 1)
  170. #define MAX_LINE_LEN (LINE_LEN(DRM_MAX_CRC_NR))
  171. static ssize_t crtc_crc_read(struct file *filep, char __user *user_buf,
  172. size_t count, loff_t *pos)
  173. {
  174. struct drm_crtc *crtc = filep->f_inode->i_private;
  175. struct drm_crtc_crc *crc = &crtc->crc;
  176. struct drm_crtc_crc_entry *entry;
  177. char buf[MAX_LINE_LEN];
  178. int ret, i;
  179. spin_lock_irq(&crc->lock);
  180. if (!crc->source) {
  181. spin_unlock_irq(&crc->lock);
  182. return 0;
  183. }
  184. /* Nothing to read? */
  185. while (crtc_crc_data_count(crc) == 0) {
  186. if (filep->f_flags & O_NONBLOCK) {
  187. spin_unlock_irq(&crc->lock);
  188. return -EAGAIN;
  189. }
  190. ret = wait_event_interruptible_lock_irq(crc->wq,
  191. crtc_crc_data_count(crc),
  192. crc->lock);
  193. if (ret) {
  194. spin_unlock_irq(&crc->lock);
  195. return ret;
  196. }
  197. }
  198. /* We know we have an entry to be read */
  199. entry = &crc->entries[crc->tail];
  200. if (count < LINE_LEN(crc->values_cnt)) {
  201. spin_unlock_irq(&crc->lock);
  202. return -EINVAL;
  203. }
  204. BUILD_BUG_ON_NOT_POWER_OF_2(DRM_CRC_ENTRIES_NR);
  205. crc->tail = (crc->tail + 1) & (DRM_CRC_ENTRIES_NR - 1);
  206. spin_unlock_irq(&crc->lock);
  207. if (entry->has_frame_counter)
  208. sprintf(buf, "0x%08x", entry->frame);
  209. else
  210. sprintf(buf, "XXXXXXXXXX");
  211. for (i = 0; i < crc->values_cnt; i++)
  212. sprintf(buf + 10 + i * 11, " 0x%08x", entry->crcs[i]);
  213. sprintf(buf + 10 + crc->values_cnt * 11, "\n");
  214. if (copy_to_user(user_buf, buf, LINE_LEN(crc->values_cnt)))
  215. return -EFAULT;
  216. return LINE_LEN(crc->values_cnt);
  217. }
  218. static const struct file_operations drm_crtc_crc_data_fops = {
  219. .owner = THIS_MODULE,
  220. .open = crtc_crc_open,
  221. .read = crtc_crc_read,
  222. .release = crtc_crc_release,
  223. };
  224. /**
  225. * drm_debugfs_crtc_crc_add - Add files to debugfs for capture of frame CRCs
  226. * @crtc: CRTC to whom the frames will belong
  227. *
  228. * Adds files to debugfs directory that allows userspace to control the
  229. * generation of frame CRCs and to read them.
  230. *
  231. * Returns:
  232. * Zero on success, error code on failure.
  233. */
  234. int drm_debugfs_crtc_crc_add(struct drm_crtc *crtc)
  235. {
  236. struct dentry *crc_ent, *ent;
  237. if (!crtc->funcs->set_crc_source)
  238. return 0;
  239. crc_ent = debugfs_create_dir("crc", crtc->debugfs_entry);
  240. if (!crc_ent)
  241. return -ENOMEM;
  242. ent = debugfs_create_file("control", S_IRUGO, crc_ent, crtc,
  243. &drm_crtc_crc_control_fops);
  244. if (!ent)
  245. goto error;
  246. ent = debugfs_create_file("data", S_IRUGO, crc_ent, crtc,
  247. &drm_crtc_crc_data_fops);
  248. if (!ent)
  249. goto error;
  250. return 0;
  251. error:
  252. debugfs_remove_recursive(crc_ent);
  253. return -ENOMEM;
  254. }
  255. /**
  256. * drm_crtc_add_crc_entry - Add entry with CRC information for a frame
  257. * @crtc: CRTC to which the frame belongs
  258. * @has_frame: whether this entry has a frame number to go with
  259. * @frame: number of the frame these CRCs are about
  260. * @crcs: array of CRC values, with length matching #drm_crtc_crc.values_cnt
  261. *
  262. * For each frame, the driver polls the source of CRCs for new data and calls
  263. * this function to add them to the buffer from where userspace reads.
  264. */
  265. int drm_crtc_add_crc_entry(struct drm_crtc *crtc, bool has_frame,
  266. uint32_t frame, uint32_t *crcs)
  267. {
  268. struct drm_crtc_crc *crc = &crtc->crc;
  269. struct drm_crtc_crc_entry *entry;
  270. int head, tail;
  271. assert_spin_locked(&crc->lock);
  272. /* Caller may not have noticed yet that userspace has stopped reading */
  273. if (!crc->opened)
  274. return -EINVAL;
  275. head = crc->head;
  276. tail = crc->tail;
  277. if (CIRC_SPACE(head, tail, DRM_CRC_ENTRIES_NR) < 1) {
  278. DRM_ERROR("Overflow of CRC buffer, userspace reads too slow.\n");
  279. return -ENOBUFS;
  280. }
  281. entry = &crc->entries[head];
  282. entry->frame = frame;
  283. entry->has_frame_counter = has_frame;
  284. memcpy(&entry->crcs, crcs, sizeof(*crcs) * crc->values_cnt);
  285. head = (head + 1) & (DRM_CRC_ENTRIES_NR - 1);
  286. crc->head = head;
  287. return 0;
  288. }
  289. EXPORT_SYMBOL_GPL(drm_crtc_add_crc_entry);