drm_debugfs_crc.c 12 KB

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