drm_debugfs_crc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 CRC sampling "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. * On the driver side the implementation effort is minimal, drivers only need to
  60. * implement &drm_crtc_funcs.set_crc_source. The debugfs files are automatically
  61. * set up if that vfunc is set. CRC samples need to be captured in the driver by
  62. * calling drm_crtc_add_crc_entry().
  63. */
  64. static int crc_control_show(struct seq_file *m, void *data)
  65. {
  66. struct drm_crtc *crtc = m->private;
  67. seq_printf(m, "%s\n", crtc->crc.source);
  68. return 0;
  69. }
  70. static int crc_control_open(struct inode *inode, struct file *file)
  71. {
  72. struct drm_crtc *crtc = inode->i_private;
  73. return single_open(file, crc_control_show, crtc);
  74. }
  75. static ssize_t crc_control_write(struct file *file, const char __user *ubuf,
  76. size_t len, loff_t *offp)
  77. {
  78. struct seq_file *m = file->private_data;
  79. struct drm_crtc *crtc = m->private;
  80. struct drm_crtc_crc *crc = &crtc->crc;
  81. char *source;
  82. if (len == 0)
  83. return 0;
  84. if (len > PAGE_SIZE - 1) {
  85. DRM_DEBUG_KMS("Expected < %lu bytes into crtc crc control\n",
  86. PAGE_SIZE);
  87. return -E2BIG;
  88. }
  89. source = memdup_user_nul(ubuf, len);
  90. if (IS_ERR(source))
  91. return PTR_ERR(source);
  92. if (source[len - 1] == '\n')
  93. source[len - 1] = '\0';
  94. spin_lock_irq(&crc->lock);
  95. if (crc->opened) {
  96. spin_unlock_irq(&crc->lock);
  97. kfree(source);
  98. return -EBUSY;
  99. }
  100. kfree(crc->source);
  101. crc->source = source;
  102. spin_unlock_irq(&crc->lock);
  103. *offp += len;
  104. return len;
  105. }
  106. static const struct file_operations drm_crtc_crc_control_fops = {
  107. .owner = THIS_MODULE,
  108. .open = crc_control_open,
  109. .read = seq_read,
  110. .llseek = seq_lseek,
  111. .release = single_release,
  112. .write = crc_control_write
  113. };
  114. static int crtc_crc_data_count(struct drm_crtc_crc *crc)
  115. {
  116. assert_spin_locked(&crc->lock);
  117. return CIRC_CNT(crc->head, crc->tail, DRM_CRC_ENTRIES_NR);
  118. }
  119. static void crtc_crc_cleanup(struct drm_crtc_crc *crc)
  120. {
  121. kfree(crc->entries);
  122. crc->overflow = false;
  123. crc->entries = NULL;
  124. crc->head = 0;
  125. crc->tail = 0;
  126. crc->values_cnt = 0;
  127. crc->opened = false;
  128. }
  129. static int crtc_crc_open(struct inode *inode, struct file *filep)
  130. {
  131. struct drm_crtc *crtc = inode->i_private;
  132. struct drm_crtc_crc *crc = &crtc->crc;
  133. struct drm_crtc_crc_entry *entries = NULL;
  134. size_t values_cnt;
  135. int ret = 0;
  136. if (drm_drv_uses_atomic_modeset(crtc->dev)) {
  137. ret = drm_modeset_lock_single_interruptible(&crtc->mutex);
  138. if (ret)
  139. return ret;
  140. if (!crtc->state->active)
  141. ret = -EIO;
  142. drm_modeset_unlock(&crtc->mutex);
  143. if (ret)
  144. return ret;
  145. }
  146. spin_lock_irq(&crc->lock);
  147. if (!crc->opened)
  148. crc->opened = true;
  149. else
  150. ret = -EBUSY;
  151. spin_unlock_irq(&crc->lock);
  152. if (ret)
  153. return ret;
  154. ret = crtc->funcs->set_crc_source(crtc, crc->source, &values_cnt);
  155. if (ret)
  156. goto err;
  157. if (WARN_ON(values_cnt > DRM_MAX_CRC_NR)) {
  158. ret = -EINVAL;
  159. goto err_disable;
  160. }
  161. if (WARN_ON(values_cnt == 0)) {
  162. ret = -EINVAL;
  163. goto err_disable;
  164. }
  165. entries = kcalloc(DRM_CRC_ENTRIES_NR, sizeof(*entries), GFP_KERNEL);
  166. if (!entries) {
  167. ret = -ENOMEM;
  168. goto err_disable;
  169. }
  170. spin_lock_irq(&crc->lock);
  171. crc->entries = entries;
  172. crc->values_cnt = values_cnt;
  173. /*
  174. * Only return once we got a first frame, so userspace doesn't have to
  175. * guess when this particular piece of HW will be ready to start
  176. * generating CRCs.
  177. */
  178. ret = wait_event_interruptible_lock_irq(crc->wq,
  179. crtc_crc_data_count(crc),
  180. crc->lock);
  181. spin_unlock_irq(&crc->lock);
  182. if (ret)
  183. goto err_disable;
  184. return 0;
  185. err_disable:
  186. crtc->funcs->set_crc_source(crtc, NULL, &values_cnt);
  187. err:
  188. spin_lock_irq(&crc->lock);
  189. crtc_crc_cleanup(crc);
  190. spin_unlock_irq(&crc->lock);
  191. return ret;
  192. }
  193. static int crtc_crc_release(struct inode *inode, struct file *filep)
  194. {
  195. struct drm_crtc *crtc = filep->f_inode->i_private;
  196. struct drm_crtc_crc *crc = &crtc->crc;
  197. size_t values_cnt;
  198. crtc->funcs->set_crc_source(crtc, NULL, &values_cnt);
  199. spin_lock_irq(&crc->lock);
  200. crtc_crc_cleanup(crc);
  201. spin_unlock_irq(&crc->lock);
  202. return 0;
  203. }
  204. /*
  205. * 1 frame field of 10 chars plus a number of CRC fields of 10 chars each, space
  206. * separated, with a newline at the end and null-terminated.
  207. */
  208. #define LINE_LEN(values_cnt) (10 + 11 * values_cnt + 1 + 1)
  209. #define MAX_LINE_LEN (LINE_LEN(DRM_MAX_CRC_NR))
  210. static ssize_t crtc_crc_read(struct file *filep, char __user *user_buf,
  211. size_t count, loff_t *pos)
  212. {
  213. struct drm_crtc *crtc = filep->f_inode->i_private;
  214. struct drm_crtc_crc *crc = &crtc->crc;
  215. struct drm_crtc_crc_entry *entry;
  216. char buf[MAX_LINE_LEN];
  217. int ret, i;
  218. spin_lock_irq(&crc->lock);
  219. if (!crc->source) {
  220. spin_unlock_irq(&crc->lock);
  221. return 0;
  222. }
  223. /* Nothing to read? */
  224. while (crtc_crc_data_count(crc) == 0) {
  225. if (filep->f_flags & O_NONBLOCK) {
  226. spin_unlock_irq(&crc->lock);
  227. return -EAGAIN;
  228. }
  229. ret = wait_event_interruptible_lock_irq(crc->wq,
  230. crtc_crc_data_count(crc),
  231. crc->lock);
  232. if (ret) {
  233. spin_unlock_irq(&crc->lock);
  234. return ret;
  235. }
  236. }
  237. /* We know we have an entry to be read */
  238. entry = &crc->entries[crc->tail];
  239. if (count < LINE_LEN(crc->values_cnt)) {
  240. spin_unlock_irq(&crc->lock);
  241. return -EINVAL;
  242. }
  243. BUILD_BUG_ON_NOT_POWER_OF_2(DRM_CRC_ENTRIES_NR);
  244. crc->tail = (crc->tail + 1) & (DRM_CRC_ENTRIES_NR - 1);
  245. spin_unlock_irq(&crc->lock);
  246. if (entry->has_frame_counter)
  247. sprintf(buf, "0x%08x", entry->frame);
  248. else
  249. sprintf(buf, "XXXXXXXXXX");
  250. for (i = 0; i < crc->values_cnt; i++)
  251. sprintf(buf + 10 + i * 11, " 0x%08x", entry->crcs[i]);
  252. sprintf(buf + 10 + crc->values_cnt * 11, "\n");
  253. if (copy_to_user(user_buf, buf, LINE_LEN(crc->values_cnt)))
  254. return -EFAULT;
  255. return LINE_LEN(crc->values_cnt);
  256. }
  257. static unsigned int crtc_crc_poll(struct file *file, poll_table *wait)
  258. {
  259. struct drm_crtc *crtc = file->f_inode->i_private;
  260. struct drm_crtc_crc *crc = &crtc->crc;
  261. unsigned ret;
  262. poll_wait(file, &crc->wq, wait);
  263. spin_lock_irq(&crc->lock);
  264. if (crc->source && crtc_crc_data_count(crc))
  265. ret = POLLIN | POLLRDNORM;
  266. else
  267. ret = 0;
  268. spin_unlock_irq(&crc->lock);
  269. return ret;
  270. }
  271. static const struct file_operations drm_crtc_crc_data_fops = {
  272. .owner = THIS_MODULE,
  273. .open = crtc_crc_open,
  274. .read = crtc_crc_read,
  275. .poll = crtc_crc_poll,
  276. .release = crtc_crc_release,
  277. };
  278. int drm_debugfs_crtc_crc_add(struct drm_crtc *crtc)
  279. {
  280. struct dentry *crc_ent, *ent;
  281. if (!crtc->funcs->set_crc_source)
  282. return 0;
  283. crc_ent = debugfs_create_dir("crc", crtc->debugfs_entry);
  284. if (!crc_ent)
  285. return -ENOMEM;
  286. ent = debugfs_create_file("control", S_IRUGO, crc_ent, crtc,
  287. &drm_crtc_crc_control_fops);
  288. if (!ent)
  289. goto error;
  290. ent = debugfs_create_file("data", S_IRUGO, crc_ent, crtc,
  291. &drm_crtc_crc_data_fops);
  292. if (!ent)
  293. goto error;
  294. return 0;
  295. error:
  296. debugfs_remove_recursive(crc_ent);
  297. return -ENOMEM;
  298. }
  299. /**
  300. * drm_crtc_add_crc_entry - Add entry with CRC information for a frame
  301. * @crtc: CRTC to which the frame belongs
  302. * @has_frame: whether this entry has a frame number to go with
  303. * @frame: number of the frame these CRCs are about
  304. * @crcs: array of CRC values, with length matching #drm_crtc_crc.values_cnt
  305. *
  306. * For each frame, the driver polls the source of CRCs for new data and calls
  307. * this function to add them to the buffer from where userspace reads.
  308. */
  309. int drm_crtc_add_crc_entry(struct drm_crtc *crtc, bool has_frame,
  310. uint32_t frame, uint32_t *crcs)
  311. {
  312. struct drm_crtc_crc *crc = &crtc->crc;
  313. struct drm_crtc_crc_entry *entry;
  314. int head, tail;
  315. unsigned long flags;
  316. spin_lock_irqsave(&crc->lock, flags);
  317. /* Caller may not have noticed yet that userspace has stopped reading */
  318. if (!crc->entries) {
  319. spin_unlock_irqrestore(&crc->lock, flags);
  320. return -EINVAL;
  321. }
  322. head = crc->head;
  323. tail = crc->tail;
  324. if (CIRC_SPACE(head, tail, DRM_CRC_ENTRIES_NR) < 1) {
  325. bool was_overflow = crc->overflow;
  326. crc->overflow = true;
  327. spin_unlock_irqrestore(&crc->lock, flags);
  328. if (!was_overflow)
  329. DRM_ERROR("Overflow of CRC buffer, userspace reads too slow.\n");
  330. return -ENOBUFS;
  331. }
  332. entry = &crc->entries[head];
  333. entry->frame = frame;
  334. entry->has_frame_counter = has_frame;
  335. memcpy(&entry->crcs, crcs, sizeof(*crcs) * crc->values_cnt);
  336. head = (head + 1) & (DRM_CRC_ENTRIES_NR - 1);
  337. crc->head = head;
  338. spin_unlock_irqrestore(&crc->lock, flags);
  339. wake_up_interruptible(&crc->wq);
  340. return 0;
  341. }
  342. EXPORT_SYMBOL_GPL(drm_crtc_add_crc_entry);