drm_debugfs_crc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 <linux/export.h>
  32. #include <linux/poll.h>
  33. #include <linux/uaccess.h>
  34. #include <drm/drm_crtc.h>
  35. #include <drm/drm_debugfs_crc.h>
  36. #include <drm/drm_drv.h>
  37. #include <drm/drm_print.h>
  38. #include "drm_internal.h"
  39. /**
  40. * DOC: CRC ABI
  41. *
  42. * DRM device drivers can provide to userspace CRC information of each frame as
  43. * it reached a given hardware component (a CRC sampling "source").
  44. *
  45. * Userspace can control generation of CRCs in a given CRTC by writing to the
  46. * file dri/0/crtc-N/crc/control in debugfs, with N being the :ref:`index of
  47. * the CRTC<crtc_index>`. Accepted values are source names (which are
  48. * driver-specific) and the "auto" keyword, which will let the driver select a
  49. * default source of frame CRCs for this CRTC.
  50. *
  51. * Once frame CRC generation is enabled, userspace can capture them by reading
  52. * the dri/0/crtc-N/crc/data file. Each line in that file contains the frame
  53. * number in the first field and then a number of unsigned integer fields
  54. * containing the CRC data. Fields are separated by a single space and the number
  55. * of CRC fields is source-specific.
  56. *
  57. * Note that though in some cases the CRC is computed in a specified way and on
  58. * the frame contents as supplied by userspace (eDP 1.3), in general the CRC
  59. * computation is performed in an unspecified way and on frame contents that have
  60. * been already processed in also an unspecified way and thus userspace cannot
  61. * rely on being able to generate matching CRC values for the frame contents that
  62. * it submits. In this general case, the maximum userspace can do is to compare
  63. * the reported CRCs of frames that should have the same contents.
  64. *
  65. * On the driver side the implementation effort is minimal, drivers only need to
  66. * implement &drm_crtc_funcs.set_crc_source and &drm_crtc_funcs.verify_crc_source.
  67. * The debugfs files are automatically set up if those vfuncs are set. CRC samples
  68. * need to be captured in the driver by calling drm_crtc_add_crc_entry().
  69. * Depending on the driver and HW requirements, &drm_crtc_funcs.set_crc_source
  70. * may result in a commit (even a full modeset).
  71. *
  72. * CRC results must be reliable across non-full-modeset atomic commits, so if a
  73. * commit via DRM_IOCTL_MODE_ATOMIC would disable or otherwise interfere with
  74. * CRC generation, then the driver must mark that commit as a full modeset
  75. * (drm_atomic_crtc_needs_modeset() should return true). As a result, to ensure
  76. * consistent results, generic userspace must re-setup CRC generation after a
  77. * legacy SETCRTC or an atomic commit with DRM_MODE_ATOMIC_ALLOW_MODESET.
  78. */
  79. static int crc_control_show(struct seq_file *m, void *data)
  80. {
  81. struct drm_crtc *crtc = m->private;
  82. if (crtc->funcs->get_crc_sources) {
  83. size_t count;
  84. const char *const *sources = crtc->funcs->get_crc_sources(crtc,
  85. &count);
  86. size_t values_cnt;
  87. int i;
  88. if (count == 0 || !sources)
  89. goto out;
  90. for (i = 0; i < count; i++)
  91. if (!crtc->funcs->verify_crc_source(crtc, sources[i],
  92. &values_cnt)) {
  93. if (strcmp(sources[i], crtc->crc.source))
  94. seq_printf(m, "%s\n", sources[i]);
  95. else
  96. seq_printf(m, "%s*\n", sources[i]);
  97. }
  98. }
  99. return 0;
  100. out:
  101. seq_printf(m, "%s*\n", crtc->crc.source);
  102. return 0;
  103. }
  104. static int crc_control_open(struct inode *inode, struct file *file)
  105. {
  106. struct drm_crtc *crtc = inode->i_private;
  107. return single_open(file, crc_control_show, crtc);
  108. }
  109. static ssize_t crc_control_write(struct file *file, const char __user *ubuf,
  110. size_t len, loff_t *offp)
  111. {
  112. struct seq_file *m = file->private_data;
  113. struct drm_crtc *crtc = m->private;
  114. struct drm_crtc_crc *crc = &crtc->crc;
  115. char *source;
  116. size_t values_cnt;
  117. int ret;
  118. if (len == 0)
  119. return 0;
  120. if (len > PAGE_SIZE - 1) {
  121. DRM_DEBUG_KMS("Expected < %lu bytes into crtc crc control\n",
  122. PAGE_SIZE);
  123. return -E2BIG;
  124. }
  125. source = memdup_user_nul(ubuf, len);
  126. if (IS_ERR(source))
  127. return PTR_ERR(source);
  128. if (source[len - 1] == '\n')
  129. source[len - 1] = '\0';
  130. ret = crtc->funcs->verify_crc_source(crtc, source, &values_cnt);
  131. if (ret) {
  132. kfree(source);
  133. return ret;
  134. }
  135. spin_lock_irq(&crc->lock);
  136. if (crc->opened) {
  137. spin_unlock_irq(&crc->lock);
  138. kfree(source);
  139. return -EBUSY;
  140. }
  141. kfree(crc->source);
  142. crc->source = source;
  143. spin_unlock_irq(&crc->lock);
  144. *offp += len;
  145. return len;
  146. }
  147. static const struct file_operations drm_crtc_crc_control_fops = {
  148. .owner = THIS_MODULE,
  149. .open = crc_control_open,
  150. .read = seq_read,
  151. .llseek = seq_lseek,
  152. .release = single_release,
  153. .write = crc_control_write
  154. };
  155. static int crtc_crc_data_count(struct drm_crtc_crc *crc)
  156. {
  157. assert_spin_locked(&crc->lock);
  158. return CIRC_CNT(crc->head, crc->tail, DRM_CRC_ENTRIES_NR);
  159. }
  160. static void crtc_crc_cleanup(struct drm_crtc_crc *crc)
  161. {
  162. kfree(crc->entries);
  163. crc->overflow = false;
  164. crc->entries = NULL;
  165. crc->head = 0;
  166. crc->tail = 0;
  167. crc->values_cnt = 0;
  168. crc->opened = false;
  169. }
  170. static int crtc_crc_open(struct inode *inode, struct file *filep)
  171. {
  172. struct drm_crtc *crtc = inode->i_private;
  173. struct drm_crtc_crc *crc = &crtc->crc;
  174. struct drm_crtc_crc_entry *entries = NULL;
  175. size_t values_cnt;
  176. int ret = 0;
  177. if (drm_drv_uses_atomic_modeset(crtc->dev)) {
  178. ret = drm_modeset_lock_single_interruptible(&crtc->mutex);
  179. if (ret)
  180. return ret;
  181. if (!crtc->state->active)
  182. ret = -EIO;
  183. drm_modeset_unlock(&crtc->mutex);
  184. if (ret)
  185. return ret;
  186. }
  187. ret = crtc->funcs->verify_crc_source(crtc, crc->source, &values_cnt);
  188. if (ret)
  189. return ret;
  190. if (WARN_ON(values_cnt > DRM_MAX_CRC_NR))
  191. return -EINVAL;
  192. if (WARN_ON(values_cnt == 0))
  193. return -EINVAL;
  194. entries = kzalloc_objs(*entries, DRM_CRC_ENTRIES_NR);
  195. if (!entries)
  196. return -ENOMEM;
  197. spin_lock_irq(&crc->lock);
  198. if (!crc->opened) {
  199. crc->opened = true;
  200. crc->entries = entries;
  201. crc->values_cnt = values_cnt;
  202. } else {
  203. ret = -EBUSY;
  204. }
  205. spin_unlock_irq(&crc->lock);
  206. if (ret) {
  207. kfree(entries);
  208. return ret;
  209. }
  210. ret = crtc->funcs->set_crc_source(crtc, crc->source);
  211. if (ret)
  212. goto err;
  213. return 0;
  214. err:
  215. spin_lock_irq(&crc->lock);
  216. crtc_crc_cleanup(crc);
  217. spin_unlock_irq(&crc->lock);
  218. return ret;
  219. }
  220. static int crtc_crc_release(struct inode *inode, struct file *filep)
  221. {
  222. struct drm_crtc *crtc = filep->f_inode->i_private;
  223. struct drm_crtc_crc *crc = &crtc->crc;
  224. /* terminate the infinite while loop if 'drm_dp_aux_crc_work' running */
  225. spin_lock_irq(&crc->lock);
  226. crc->opened = false;
  227. spin_unlock_irq(&crc->lock);
  228. crtc->funcs->set_crc_source(crtc, NULL);
  229. spin_lock_irq(&crc->lock);
  230. crtc_crc_cleanup(crc);
  231. spin_unlock_irq(&crc->lock);
  232. return 0;
  233. }
  234. /*
  235. * 1 frame field of 10 chars plus a number of CRC fields of 10 chars each, space
  236. * separated, with a newline at the end and null-terminated.
  237. */
  238. #define LINE_LEN(values_cnt) (10 + 11 * values_cnt + 1 + 1)
  239. #define MAX_LINE_LEN (LINE_LEN(DRM_MAX_CRC_NR))
  240. static ssize_t crtc_crc_read(struct file *filep, char __user *user_buf,
  241. size_t count, loff_t *pos)
  242. {
  243. struct drm_crtc *crtc = filep->f_inode->i_private;
  244. struct drm_crtc_crc *crc = &crtc->crc;
  245. struct drm_crtc_crc_entry *entry;
  246. char buf[MAX_LINE_LEN];
  247. int ret, i;
  248. spin_lock_irq(&crc->lock);
  249. if (!crc->source) {
  250. spin_unlock_irq(&crc->lock);
  251. return 0;
  252. }
  253. /* Nothing to read? */
  254. while (crtc_crc_data_count(crc) == 0) {
  255. if (filep->f_flags & O_NONBLOCK) {
  256. spin_unlock_irq(&crc->lock);
  257. return -EAGAIN;
  258. }
  259. ret = wait_event_interruptible_lock_irq(crc->wq,
  260. crtc_crc_data_count(crc),
  261. crc->lock);
  262. if (ret) {
  263. spin_unlock_irq(&crc->lock);
  264. return ret;
  265. }
  266. }
  267. /* We know we have an entry to be read */
  268. entry = &crc->entries[crc->tail];
  269. if (count < LINE_LEN(crc->values_cnt)) {
  270. spin_unlock_irq(&crc->lock);
  271. return -EINVAL;
  272. }
  273. BUILD_BUG_ON_NOT_POWER_OF_2(DRM_CRC_ENTRIES_NR);
  274. crc->tail = (crc->tail + 1) & (DRM_CRC_ENTRIES_NR - 1);
  275. spin_unlock_irq(&crc->lock);
  276. if (entry->has_frame_counter)
  277. sprintf(buf, "0x%08x", entry->frame);
  278. else
  279. sprintf(buf, "XXXXXXXXXX");
  280. for (i = 0; i < crc->values_cnt; i++)
  281. sprintf(buf + 10 + i * 11, " 0x%08x", entry->crcs[i]);
  282. sprintf(buf + 10 + crc->values_cnt * 11, "\n");
  283. if (copy_to_user(user_buf, buf, LINE_LEN(crc->values_cnt)))
  284. return -EFAULT;
  285. return LINE_LEN(crc->values_cnt);
  286. }
  287. static __poll_t crtc_crc_poll(struct file *file, poll_table *wait)
  288. {
  289. struct drm_crtc *crtc = file->f_inode->i_private;
  290. struct drm_crtc_crc *crc = &crtc->crc;
  291. __poll_t ret = 0;
  292. poll_wait(file, &crc->wq, wait);
  293. spin_lock_irq(&crc->lock);
  294. if (crc->source && crtc_crc_data_count(crc))
  295. ret |= EPOLLIN | EPOLLRDNORM;
  296. spin_unlock_irq(&crc->lock);
  297. return ret;
  298. }
  299. static const struct file_operations drm_crtc_crc_data_fops = {
  300. .owner = THIS_MODULE,
  301. .open = crtc_crc_open,
  302. .read = crtc_crc_read,
  303. .poll = crtc_crc_poll,
  304. .release = crtc_crc_release,
  305. };
  306. void drm_debugfs_crtc_crc_add(struct drm_crtc *crtc)
  307. {
  308. struct dentry *crc_ent;
  309. if (!crtc->funcs->set_crc_source || !crtc->funcs->verify_crc_source)
  310. return;
  311. crc_ent = debugfs_create_dir("crc", crtc->debugfs_entry);
  312. debugfs_create_file("control", S_IRUGO | S_IWUSR, crc_ent, crtc,
  313. &drm_crtc_crc_control_fops);
  314. debugfs_create_file("data", S_IRUGO, crc_ent, crtc,
  315. &drm_crtc_crc_data_fops);
  316. }
  317. /**
  318. * drm_crtc_add_crc_entry - Add entry with CRC information for a frame
  319. * @crtc: CRTC to which the frame belongs
  320. * @has_frame: whether this entry has a frame number to go with
  321. * @frame: number of the frame these CRCs are about
  322. * @crcs: array of CRC values, with length matching #drm_crtc_crc.values_cnt
  323. *
  324. * For each frame, the driver polls the source of CRCs for new data and calls
  325. * this function to add them to the buffer from where userspace reads.
  326. */
  327. int drm_crtc_add_crc_entry(struct drm_crtc *crtc, bool has_frame,
  328. uint32_t frame, uint32_t *crcs)
  329. {
  330. struct drm_crtc_crc *crc = &crtc->crc;
  331. struct drm_crtc_crc_entry *entry;
  332. int head, tail;
  333. unsigned long flags;
  334. spin_lock_irqsave(&crc->lock, flags);
  335. /* Caller may not have noticed yet that userspace has stopped reading */
  336. if (!crc->entries) {
  337. spin_unlock_irqrestore(&crc->lock, flags);
  338. return -EINVAL;
  339. }
  340. head = crc->head;
  341. tail = crc->tail;
  342. if (CIRC_SPACE(head, tail, DRM_CRC_ENTRIES_NR) < 1) {
  343. bool was_overflow = crc->overflow;
  344. crc->overflow = true;
  345. spin_unlock_irqrestore(&crc->lock, flags);
  346. if (!was_overflow)
  347. DRM_ERROR("Overflow of CRC buffer, userspace reads too slow.\n");
  348. return -ENOBUFS;
  349. }
  350. entry = &crc->entries[head];
  351. entry->frame = frame;
  352. entry->has_frame_counter = has_frame;
  353. memcpy(&entry->crcs, crcs, sizeof(*crcs) * crc->values_cnt);
  354. head = (head + 1) & (DRM_CRC_ENTRIES_NR - 1);
  355. crc->head = head;
  356. spin_unlock_irqrestore(&crc->lock, flags);
  357. wake_up_interruptible(&crc->wq);
  358. return 0;
  359. }
  360. EXPORT_SYMBOL_GPL(drm_crtc_add_crc_entry);