drm_debugfs_crc.c 10 KB

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