drm_debugfs_crc.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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_data_count(struct drm_crtc_crc *crc)
  110. {
  111. assert_spin_locked(&crc->lock);
  112. return CIRC_CNT(crc->head, crc->tail, DRM_CRC_ENTRIES_NR);
  113. }
  114. static int crtc_crc_open(struct inode *inode, struct file *filep)
  115. {
  116. struct drm_crtc *crtc = inode->i_private;
  117. struct drm_crtc_crc *crc = &crtc->crc;
  118. struct drm_crtc_crc_entry *entries = NULL;
  119. size_t values_cnt;
  120. int ret;
  121. if (crc->opened)
  122. return -EBUSY;
  123. ret = crtc->funcs->set_crc_source(crtc, crc->source, &values_cnt);
  124. if (ret)
  125. return ret;
  126. if (WARN_ON(values_cnt > DRM_MAX_CRC_NR)) {
  127. ret = -EINVAL;
  128. goto err_disable;
  129. }
  130. if (WARN_ON(values_cnt == 0)) {
  131. ret = -EINVAL;
  132. goto err_disable;
  133. }
  134. entries = kcalloc(DRM_CRC_ENTRIES_NR, sizeof(*entries), GFP_KERNEL);
  135. if (!entries) {
  136. ret = -ENOMEM;
  137. goto err_disable;
  138. }
  139. spin_lock_irq(&crc->lock);
  140. crc->entries = entries;
  141. crc->values_cnt = values_cnt;
  142. crc->opened = true;
  143. /*
  144. * Only return once we got a first frame, so userspace doesn't have to
  145. * guess when this particular piece of HW will be ready to start
  146. * generating CRCs.
  147. */
  148. ret = wait_event_interruptible_lock_irq(crc->wq,
  149. crtc_crc_data_count(crc),
  150. crc->lock);
  151. spin_unlock_irq(&crc->lock);
  152. WARN_ON(ret);
  153. return 0;
  154. err_disable:
  155. crtc->funcs->set_crc_source(crtc, NULL, &values_cnt);
  156. return ret;
  157. }
  158. static int crtc_crc_release(struct inode *inode, struct file *filep)
  159. {
  160. struct drm_crtc *crtc = filep->f_inode->i_private;
  161. struct drm_crtc_crc *crc = &crtc->crc;
  162. size_t values_cnt;
  163. spin_lock_irq(&crc->lock);
  164. kfree(crc->entries);
  165. crc->entries = NULL;
  166. crc->head = 0;
  167. crc->tail = 0;
  168. crc->values_cnt = 0;
  169. crc->opened = false;
  170. spin_unlock_irq(&crc->lock);
  171. crtc->funcs->set_crc_source(crtc, NULL, &values_cnt);
  172. return 0;
  173. }
  174. /*
  175. * 1 frame field of 10 chars plus a number of CRC fields of 10 chars each, space
  176. * separated, with a newline at the end and null-terminated.
  177. */
  178. #define LINE_LEN(values_cnt) (10 + 11 * values_cnt + 1 + 1)
  179. #define MAX_LINE_LEN (LINE_LEN(DRM_MAX_CRC_NR))
  180. static ssize_t crtc_crc_read(struct file *filep, char __user *user_buf,
  181. size_t count, loff_t *pos)
  182. {
  183. struct drm_crtc *crtc = filep->f_inode->i_private;
  184. struct drm_crtc_crc *crc = &crtc->crc;
  185. struct drm_crtc_crc_entry *entry;
  186. char buf[MAX_LINE_LEN];
  187. int ret, i;
  188. spin_lock_irq(&crc->lock);
  189. if (!crc->source) {
  190. spin_unlock_irq(&crc->lock);
  191. return 0;
  192. }
  193. /* Nothing to read? */
  194. while (crtc_crc_data_count(crc) == 0) {
  195. if (filep->f_flags & O_NONBLOCK) {
  196. spin_unlock_irq(&crc->lock);
  197. return -EAGAIN;
  198. }
  199. ret = wait_event_interruptible_lock_irq(crc->wq,
  200. crtc_crc_data_count(crc),
  201. crc->lock);
  202. if (ret) {
  203. spin_unlock_irq(&crc->lock);
  204. return ret;
  205. }
  206. }
  207. /* We know we have an entry to be read */
  208. entry = &crc->entries[crc->tail];
  209. if (count < LINE_LEN(crc->values_cnt)) {
  210. spin_unlock_irq(&crc->lock);
  211. return -EINVAL;
  212. }
  213. BUILD_BUG_ON_NOT_POWER_OF_2(DRM_CRC_ENTRIES_NR);
  214. crc->tail = (crc->tail + 1) & (DRM_CRC_ENTRIES_NR - 1);
  215. spin_unlock_irq(&crc->lock);
  216. if (entry->has_frame_counter)
  217. sprintf(buf, "0x%08x", entry->frame);
  218. else
  219. sprintf(buf, "XXXXXXXXXX");
  220. for (i = 0; i < crc->values_cnt; i++)
  221. sprintf(buf + 10 + i * 11, " 0x%08x", entry->crcs[i]);
  222. sprintf(buf + 10 + crc->values_cnt * 11, "\n");
  223. if (copy_to_user(user_buf, buf, LINE_LEN(crc->values_cnt)))
  224. return -EFAULT;
  225. return LINE_LEN(crc->values_cnt);
  226. }
  227. static const struct file_operations drm_crtc_crc_data_fops = {
  228. .owner = THIS_MODULE,
  229. .open = crtc_crc_open,
  230. .read = crtc_crc_read,
  231. .release = crtc_crc_release,
  232. };
  233. /**
  234. * drm_debugfs_crtc_crc_add - Add files to debugfs for capture of frame CRCs
  235. * @crtc: CRTC to whom the frames will belong
  236. *
  237. * Adds files to debugfs directory that allows userspace to control the
  238. * generation of frame CRCs and to read them.
  239. *
  240. * Returns:
  241. * Zero on success, error code on failure.
  242. */
  243. int drm_debugfs_crtc_crc_add(struct drm_crtc *crtc)
  244. {
  245. struct dentry *crc_ent, *ent;
  246. if (!crtc->funcs->set_crc_source)
  247. return 0;
  248. crc_ent = debugfs_create_dir("crc", crtc->debugfs_entry);
  249. if (!crc_ent)
  250. return -ENOMEM;
  251. ent = debugfs_create_file("control", S_IRUGO, crc_ent, crtc,
  252. &drm_crtc_crc_control_fops);
  253. if (!ent)
  254. goto error;
  255. ent = debugfs_create_file("data", S_IRUGO, crc_ent, crtc,
  256. &drm_crtc_crc_data_fops);
  257. if (!ent)
  258. goto error;
  259. return 0;
  260. error:
  261. debugfs_remove_recursive(crc_ent);
  262. return -ENOMEM;
  263. }
  264. /**
  265. * drm_crtc_add_crc_entry - Add entry with CRC information for a frame
  266. * @crtc: CRTC to which the frame belongs
  267. * @has_frame: whether this entry has a frame number to go with
  268. * @frame: number of the frame these CRCs are about
  269. * @crcs: array of CRC values, with length matching #drm_crtc_crc.values_cnt
  270. *
  271. * For each frame, the driver polls the source of CRCs for new data and calls
  272. * this function to add them to the buffer from where userspace reads.
  273. */
  274. int drm_crtc_add_crc_entry(struct drm_crtc *crtc, bool has_frame,
  275. uint32_t frame, uint32_t *crcs)
  276. {
  277. struct drm_crtc_crc *crc = &crtc->crc;
  278. struct drm_crtc_crc_entry *entry;
  279. int head, tail;
  280. spin_lock(&crc->lock);
  281. /* Caller may not have noticed yet that userspace has stopped reading */
  282. if (!crc->opened) {
  283. spin_unlock(&crc->lock);
  284. return -EINVAL;
  285. }
  286. head = crc->head;
  287. tail = crc->tail;
  288. if (CIRC_SPACE(head, tail, DRM_CRC_ENTRIES_NR) < 1) {
  289. spin_unlock(&crc->lock);
  290. DRM_ERROR("Overflow of CRC buffer, userspace reads too slow.\n");
  291. return -ENOBUFS;
  292. }
  293. entry = &crc->entries[head];
  294. entry->frame = frame;
  295. entry->has_frame_counter = has_frame;
  296. memcpy(&entry->crcs, crcs, sizeof(*crcs) * crc->values_cnt);
  297. head = (head + 1) & (DRM_CRC_ENTRIES_NR - 1);
  298. crc->head = head;
  299. spin_unlock(&crc->lock);
  300. wake_up_interruptible(&crc->wq);
  301. return 0;
  302. }
  303. EXPORT_SYMBOL_GPL(drm_crtc_add_crc_entry);