Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 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 | |
| 29 | #include <linux/circ_buf.h> |
| 30 | #include <linux/ctype.h> |
| 31 | #include <linux/debugfs.h> |
Sam Ravnborg | 0500c04 | 2019-05-26 19:35:35 +0200 | [diff] [blame] | 32 | #include <linux/poll.h> |
| 33 | #include <linux/uaccess.h> |
| 34 | |
| 35 | #include <drm/drm_crtc.h> |
| 36 | #include <drm/drm_debugfs_crc.h> |
| 37 | #include <drm/drm_drv.h> |
| 38 | #include <drm/drm_print.h> |
| 39 | |
Jani Nikula | e3f56b2 | 2016-10-18 14:28:35 +0300 | [diff] [blame] | 40 | #include "drm_internal.h" |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * DOC: CRC ABI |
| 44 | * |
| 45 | * DRM device drivers can provide to userspace CRC information of each frame as |
Daniel Vetter | 760f71e | 2017-03-22 09:36:04 +0100 | [diff] [blame] | 46 | * it reached a given hardware component (a CRC sampling "source"). |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 47 | * |
| 48 | * Userspace can control generation of CRCs in a given CRTC by writing to the |
| 49 | * file dri/0/crtc-N/crc/control in debugfs, with N being the index of the CRTC. |
| 50 | * Accepted values are source names (which are driver-specific) and the "auto" |
| 51 | * keyword, which will let the driver select a default source of frame CRCs |
| 52 | * for this CRTC. |
| 53 | * |
| 54 | * Once frame CRC generation is enabled, userspace can capture them by reading |
| 55 | * the dri/0/crtc-N/crc/data file. Each line in that file contains the frame |
| 56 | * number in the first field and then a number of unsigned integer fields |
| 57 | * containing the CRC data. Fields are separated by a single space and the number |
| 58 | * of CRC fields is source-specific. |
| 59 | * |
| 60 | * Note that though in some cases the CRC is computed in a specified way and on |
| 61 | * the frame contents as supplied by userspace (eDP 1.3), in general the CRC |
| 62 | * computation is performed in an unspecified way and on frame contents that have |
| 63 | * been already processed in also an unspecified way and thus userspace cannot |
| 64 | * rely on being able to generate matching CRC values for the frame contents that |
| 65 | * it submits. In this general case, the maximum userspace can do is to compare |
| 66 | * the reported CRCs of frames that should have the same contents. |
Daniel Vetter | 760f71e | 2017-03-22 09:36:04 +0100 | [diff] [blame] | 67 | * |
| 68 | * On the driver side the implementation effort is minimal, drivers only need to |
Liviu Dudau | 84a6810 | 2019-07-03 15:56:03 +0100 | [diff] [blame^] | 69 | * implement &drm_crtc_funcs.set_crc_source and &drm_crtc_funcs.verify_crc_source. |
| 70 | * The debugfs files are automatically set up if those vfuncs are set. CRC samples |
| 71 | * need to be captured in the driver by calling drm_crtc_add_crc_entry(). |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 72 | */ |
| 73 | |
| 74 | static int crc_control_show(struct seq_file *m, void *data) |
| 75 | { |
| 76 | struct drm_crtc *crtc = m->private; |
| 77 | |
Mahesh Kumar | 4396551 | 2018-07-13 19:29:34 +0530 | [diff] [blame] | 78 | if (crtc->funcs->get_crc_sources) { |
| 79 | size_t count; |
| 80 | const char *const *sources = crtc->funcs->get_crc_sources(crtc, |
| 81 | &count); |
| 82 | size_t values_cnt; |
| 83 | int i; |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 84 | |
Mahesh Kumar | 4396551 | 2018-07-13 19:29:34 +0530 | [diff] [blame] | 85 | if (count == 0 || !sources) |
| 86 | goto out; |
| 87 | |
| 88 | for (i = 0; i < count; i++) |
| 89 | if (!crtc->funcs->verify_crc_source(crtc, sources[i], |
| 90 | &values_cnt)) { |
| 91 | if (strcmp(sources[i], crtc->crc.source)) |
| 92 | seq_printf(m, "%s\n", sources[i]); |
| 93 | else |
| 94 | seq_printf(m, "%s*\n", sources[i]); |
| 95 | } |
| 96 | } |
| 97 | return 0; |
| 98 | |
| 99 | out: |
| 100 | seq_printf(m, "%s*\n", crtc->crc.source); |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static int crc_control_open(struct inode *inode, struct file *file) |
| 105 | { |
| 106 | struct drm_crtc *crtc = inode->i_private; |
| 107 | |
| 108 | return single_open(file, crc_control_show, crtc); |
| 109 | } |
| 110 | |
| 111 | static ssize_t crc_control_write(struct file *file, const char __user *ubuf, |
| 112 | size_t len, loff_t *offp) |
| 113 | { |
| 114 | struct seq_file *m = file->private_data; |
| 115 | struct drm_crtc *crtc = m->private; |
| 116 | struct drm_crtc_crc *crc = &crtc->crc; |
| 117 | char *source; |
Mahesh Kumar | d5cc15a | 2018-07-13 19:29:33 +0530 | [diff] [blame] | 118 | size_t values_cnt; |
| 119 | int ret; |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 120 | |
| 121 | if (len == 0) |
| 122 | return 0; |
| 123 | |
| 124 | if (len > PAGE_SIZE - 1) { |
| 125 | DRM_DEBUG_KMS("Expected < %lu bytes into crtc crc control\n", |
| 126 | PAGE_SIZE); |
| 127 | return -E2BIG; |
| 128 | } |
| 129 | |
| 130 | source = memdup_user_nul(ubuf, len); |
| 131 | if (IS_ERR(source)) |
| 132 | return PTR_ERR(source); |
| 133 | |
| 134 | if (source[len] == '\n') |
| 135 | source[len] = '\0'; |
| 136 | |
Mahesh Kumar | c0811a7 | 2018-08-21 14:08:56 +0530 | [diff] [blame] | 137 | ret = crtc->funcs->verify_crc_source(crtc, source, &values_cnt); |
| 138 | if (ret) |
| 139 | return ret; |
Mahesh Kumar | d5cc15a | 2018-07-13 19:29:33 +0530 | [diff] [blame] | 140 | |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 141 | spin_lock_irq(&crc->lock); |
| 142 | |
| 143 | if (crc->opened) { |
| 144 | spin_unlock_irq(&crc->lock); |
| 145 | kfree(source); |
| 146 | return -EBUSY; |
| 147 | } |
| 148 | |
| 149 | kfree(crc->source); |
| 150 | crc->source = source; |
| 151 | |
| 152 | spin_unlock_irq(&crc->lock); |
| 153 | |
| 154 | *offp += len; |
| 155 | return len; |
| 156 | } |
| 157 | |
Jani Nikula | e3f56b2 | 2016-10-18 14:28:35 +0300 | [diff] [blame] | 158 | static const struct file_operations drm_crtc_crc_control_fops = { |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 159 | .owner = THIS_MODULE, |
| 160 | .open = crc_control_open, |
| 161 | .read = seq_read, |
| 162 | .llseek = seq_lseek, |
| 163 | .release = single_release, |
| 164 | .write = crc_control_write |
| 165 | }; |
| 166 | |
Tomeu Vizoso | e8fa567 | 2017-01-02 13:59:10 +0100 | [diff] [blame] | 167 | static int crtc_crc_data_count(struct drm_crtc_crc *crc) |
| 168 | { |
| 169 | assert_spin_locked(&crc->lock); |
| 170 | return CIRC_CNT(crc->head, crc->tail, DRM_CRC_ENTRIES_NR); |
| 171 | } |
| 172 | |
Maarten Lankhorst | eb42ea6 | 2017-06-21 13:00:07 +0200 | [diff] [blame] | 173 | static void crtc_crc_cleanup(struct drm_crtc_crc *crc) |
| 174 | { |
| 175 | kfree(crc->entries); |
Maarten Lankhorst | a012024 | 2018-04-18 14:51:21 +0200 | [diff] [blame] | 176 | crc->overflow = false; |
Maarten Lankhorst | eb42ea6 | 2017-06-21 13:00:07 +0200 | [diff] [blame] | 177 | crc->entries = NULL; |
| 178 | crc->head = 0; |
| 179 | crc->tail = 0; |
| 180 | crc->values_cnt = 0; |
| 181 | crc->opened = false; |
| 182 | } |
| 183 | |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 184 | static int crtc_crc_open(struct inode *inode, struct file *filep) |
| 185 | { |
| 186 | struct drm_crtc *crtc = inode->i_private; |
| 187 | struct drm_crtc_crc *crc = &crtc->crc; |
| 188 | struct drm_crtc_crc_entry *entries = NULL; |
| 189 | size_t values_cnt; |
Maarten Lankhorst | eb42ea6 | 2017-06-21 13:00:07 +0200 | [diff] [blame] | 190 | int ret = 0; |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 191 | |
Maarten Lankhorst | 8038e09 | 2017-07-06 15:03:15 +0200 | [diff] [blame] | 192 | if (drm_drv_uses_atomic_modeset(crtc->dev)) { |
Maarten Lankhorst | 6f8bcc7 | 2017-09-12 15:37:44 +0200 | [diff] [blame] | 193 | ret = drm_modeset_lock_single_interruptible(&crtc->mutex); |
Maarten Lankhorst | 8038e09 | 2017-07-06 15:03:15 +0200 | [diff] [blame] | 194 | if (ret) |
| 195 | return ret; |
| 196 | |
| 197 | if (!crtc->state->active) |
| 198 | ret = -EIO; |
| 199 | drm_modeset_unlock(&crtc->mutex); |
| 200 | |
| 201 | if (ret) |
| 202 | return ret; |
| 203 | } |
| 204 | |
Mahesh Kumar | c0811a7 | 2018-08-21 14:08:56 +0530 | [diff] [blame] | 205 | ret = crtc->funcs->verify_crc_source(crtc, crc->source, &values_cnt); |
Maarten Lankhorst | eb42ea6 | 2017-06-21 13:00:07 +0200 | [diff] [blame] | 206 | if (ret) |
| 207 | return ret; |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 208 | |
Mahesh Kumar | c0811a7 | 2018-08-21 14:08:56 +0530 | [diff] [blame] | 209 | if (WARN_ON(values_cnt > DRM_MAX_CRC_NR)) |
| 210 | return -EINVAL; |
| 211 | |
| 212 | if (WARN_ON(values_cnt == 0)) |
| 213 | return -EINVAL; |
| 214 | |
| 215 | entries = kcalloc(DRM_CRC_ENTRIES_NR, sizeof(*entries), GFP_KERNEL); |
| 216 | if (!entries) |
| 217 | return -ENOMEM; |
| 218 | |
| 219 | spin_lock_irq(&crc->lock); |
| 220 | if (!crc->opened) { |
| 221 | crc->opened = true; |
| 222 | crc->entries = entries; |
| 223 | crc->values_cnt = values_cnt; |
| 224 | } else { |
| 225 | ret = -EBUSY; |
| 226 | } |
| 227 | spin_unlock_irq(&crc->lock); |
| 228 | |
| 229 | if (ret) { |
| 230 | kfree(entries); |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | ret = crtc->funcs->set_crc_source(crtc, crc->source); |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 235 | if (ret) |
Maarten Lankhorst | eb42ea6 | 2017-06-21 13:00:07 +0200 | [diff] [blame] | 236 | goto err; |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 237 | |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 238 | return 0; |
| 239 | |
Maarten Lankhorst | eb42ea6 | 2017-06-21 13:00:07 +0200 | [diff] [blame] | 240 | err: |
| 241 | spin_lock_irq(&crc->lock); |
| 242 | crtc_crc_cleanup(crc); |
| 243 | spin_unlock_irq(&crc->lock); |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | static int crtc_crc_release(struct inode *inode, struct file *filep) |
| 248 | { |
| 249 | struct drm_crtc *crtc = filep->f_inode->i_private; |
| 250 | struct drm_crtc_crc *crc = &crtc->crc; |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 251 | |
Mahesh Kumar | c0811a7 | 2018-08-21 14:08:56 +0530 | [diff] [blame] | 252 | crtc->funcs->set_crc_source(crtc, NULL); |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 253 | |
Maarten Lankhorst | eb42ea6 | 2017-06-21 13:00:07 +0200 | [diff] [blame] | 254 | spin_lock_irq(&crc->lock); |
| 255 | crtc_crc_cleanup(crc); |
| 256 | spin_unlock_irq(&crc->lock); |
| 257 | |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 258 | return 0; |
| 259 | } |
| 260 | |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 261 | /* |
| 262 | * 1 frame field of 10 chars plus a number of CRC fields of 10 chars each, space |
| 263 | * separated, with a newline at the end and null-terminated. |
| 264 | */ |
| 265 | #define LINE_LEN(values_cnt) (10 + 11 * values_cnt + 1 + 1) |
| 266 | #define MAX_LINE_LEN (LINE_LEN(DRM_MAX_CRC_NR)) |
| 267 | |
| 268 | static ssize_t crtc_crc_read(struct file *filep, char __user *user_buf, |
| 269 | size_t count, loff_t *pos) |
| 270 | { |
| 271 | struct drm_crtc *crtc = filep->f_inode->i_private; |
| 272 | struct drm_crtc_crc *crc = &crtc->crc; |
| 273 | struct drm_crtc_crc_entry *entry; |
| 274 | char buf[MAX_LINE_LEN]; |
| 275 | int ret, i; |
| 276 | |
| 277 | spin_lock_irq(&crc->lock); |
| 278 | |
| 279 | if (!crc->source) { |
| 280 | spin_unlock_irq(&crc->lock); |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | /* Nothing to read? */ |
| 285 | while (crtc_crc_data_count(crc) == 0) { |
| 286 | if (filep->f_flags & O_NONBLOCK) { |
| 287 | spin_unlock_irq(&crc->lock); |
| 288 | return -EAGAIN; |
| 289 | } |
| 290 | |
| 291 | ret = wait_event_interruptible_lock_irq(crc->wq, |
| 292 | crtc_crc_data_count(crc), |
| 293 | crc->lock); |
| 294 | if (ret) { |
| 295 | spin_unlock_irq(&crc->lock); |
| 296 | return ret; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | /* We know we have an entry to be read */ |
| 301 | entry = &crc->entries[crc->tail]; |
| 302 | |
| 303 | if (count < LINE_LEN(crc->values_cnt)) { |
| 304 | spin_unlock_irq(&crc->lock); |
| 305 | return -EINVAL; |
| 306 | } |
| 307 | |
| 308 | BUILD_BUG_ON_NOT_POWER_OF_2(DRM_CRC_ENTRIES_NR); |
| 309 | crc->tail = (crc->tail + 1) & (DRM_CRC_ENTRIES_NR - 1); |
| 310 | |
| 311 | spin_unlock_irq(&crc->lock); |
| 312 | |
| 313 | if (entry->has_frame_counter) |
| 314 | sprintf(buf, "0x%08x", entry->frame); |
| 315 | else |
| 316 | sprintf(buf, "XXXXXXXXXX"); |
| 317 | |
| 318 | for (i = 0; i < crc->values_cnt; i++) |
| 319 | sprintf(buf + 10 + i * 11, " 0x%08x", entry->crcs[i]); |
| 320 | sprintf(buf + 10 + crc->values_cnt * 11, "\n"); |
| 321 | |
| 322 | if (copy_to_user(user_buf, buf, LINE_LEN(crc->values_cnt))) |
| 323 | return -EFAULT; |
| 324 | |
| 325 | return LINE_LEN(crc->values_cnt); |
| 326 | } |
| 327 | |
Maarten Lankhorst | 4beb3b4 | 2018-02-02 15:27:43 +0100 | [diff] [blame] | 328 | static unsigned int crtc_crc_poll(struct file *file, poll_table *wait) |
| 329 | { |
| 330 | struct drm_crtc *crtc = file->f_inode->i_private; |
| 331 | struct drm_crtc_crc *crc = &crtc->crc; |
| 332 | unsigned ret; |
| 333 | |
| 334 | poll_wait(file, &crc->wq, wait); |
| 335 | |
| 336 | spin_lock_irq(&crc->lock); |
| 337 | if (crc->source && crtc_crc_data_count(crc)) |
| 338 | ret = POLLIN | POLLRDNORM; |
| 339 | else |
| 340 | ret = 0; |
| 341 | spin_unlock_irq(&crc->lock); |
| 342 | |
| 343 | return ret; |
| 344 | } |
| 345 | |
Jani Nikula | e3f56b2 | 2016-10-18 14:28:35 +0300 | [diff] [blame] | 346 | static const struct file_operations drm_crtc_crc_data_fops = { |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 347 | .owner = THIS_MODULE, |
| 348 | .open = crtc_crc_open, |
| 349 | .read = crtc_crc_read, |
Maarten Lankhorst | 4beb3b4 | 2018-02-02 15:27:43 +0100 | [diff] [blame] | 350 | .poll = crtc_crc_poll, |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 351 | .release = crtc_crc_release, |
| 352 | }; |
| 353 | |
Greg Kroah-Hartman | b792e64 | 2019-06-13 15:34:39 +0200 | [diff] [blame] | 354 | void drm_debugfs_crtc_crc_add(struct drm_crtc *crtc) |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 355 | { |
Greg Kroah-Hartman | b792e64 | 2019-06-13 15:34:39 +0200 | [diff] [blame] | 356 | struct dentry *crc_ent; |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 357 | |
Mahesh Kumar | c0811a7 | 2018-08-21 14:08:56 +0530 | [diff] [blame] | 358 | if (!crtc->funcs->set_crc_source || !crtc->funcs->verify_crc_source) |
Greg Kroah-Hartman | b792e64 | 2019-06-13 15:34:39 +0200 | [diff] [blame] | 359 | return; |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 360 | |
| 361 | crc_ent = debugfs_create_dir("crc", crtc->debugfs_entry); |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 362 | |
Greg Kroah-Hartman | b792e64 | 2019-06-13 15:34:39 +0200 | [diff] [blame] | 363 | debugfs_create_file("control", S_IRUGO, crc_ent, crtc, |
| 364 | &drm_crtc_crc_control_fops); |
| 365 | debugfs_create_file("data", S_IRUGO, crc_ent, crtc, |
| 366 | &drm_crtc_crc_data_fops); |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /** |
| 370 | * drm_crtc_add_crc_entry - Add entry with CRC information for a frame |
| 371 | * @crtc: CRTC to which the frame belongs |
| 372 | * @has_frame: whether this entry has a frame number to go with |
| 373 | * @frame: number of the frame these CRCs are about |
| 374 | * @crcs: array of CRC values, with length matching #drm_crtc_crc.values_cnt |
| 375 | * |
| 376 | * For each frame, the driver polls the source of CRCs for new data and calls |
| 377 | * this function to add them to the buffer from where userspace reads. |
| 378 | */ |
| 379 | int drm_crtc_add_crc_entry(struct drm_crtc *crtc, bool has_frame, |
| 380 | uint32_t frame, uint32_t *crcs) |
| 381 | { |
| 382 | struct drm_crtc_crc *crc = &crtc->crc; |
| 383 | struct drm_crtc_crc_entry *entry; |
| 384 | int head, tail; |
Daniel Vetter | 1882018 | 2019-06-05 21:45:56 +0200 | [diff] [blame] | 385 | unsigned long flags; |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 386 | |
Daniel Vetter | 1882018 | 2019-06-05 21:45:56 +0200 | [diff] [blame] | 387 | spin_lock_irqsave(&crc->lock, flags); |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 388 | |
| 389 | /* Caller may not have noticed yet that userspace has stopped reading */ |
Maarten Lankhorst | eb42ea6 | 2017-06-21 13:00:07 +0200 | [diff] [blame] | 390 | if (!crc->entries) { |
Daniel Vetter | d99004d | 2019-06-06 23:15:44 +0200 | [diff] [blame] | 391 | spin_unlock_irqrestore(&crc->lock, flags); |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 392 | return -EINVAL; |
Tomeu Vizoso | 1aa81be | 2017-01-02 13:59:09 +0100 | [diff] [blame] | 393 | } |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 394 | |
| 395 | head = crc->head; |
| 396 | tail = crc->tail; |
| 397 | |
| 398 | if (CIRC_SPACE(head, tail, DRM_CRC_ENTRIES_NR) < 1) { |
Maarten Lankhorst | a012024 | 2018-04-18 14:51:21 +0200 | [diff] [blame] | 399 | bool was_overflow = crc->overflow; |
| 400 | |
| 401 | crc->overflow = true; |
Daniel Vetter | d99004d | 2019-06-06 23:15:44 +0200 | [diff] [blame] | 402 | spin_unlock_irqrestore(&crc->lock, flags); |
Maarten Lankhorst | a012024 | 2018-04-18 14:51:21 +0200 | [diff] [blame] | 403 | |
| 404 | if (!was_overflow) |
| 405 | DRM_ERROR("Overflow of CRC buffer, userspace reads too slow.\n"); |
| 406 | |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 407 | return -ENOBUFS; |
| 408 | } |
| 409 | |
| 410 | entry = &crc->entries[head]; |
| 411 | entry->frame = frame; |
| 412 | entry->has_frame_counter = has_frame; |
| 413 | memcpy(&entry->crcs, crcs, sizeof(*crcs) * crc->values_cnt); |
| 414 | |
| 415 | head = (head + 1) & (DRM_CRC_ENTRIES_NR - 1); |
| 416 | crc->head = head; |
| 417 | |
Daniel Vetter | 1882018 | 2019-06-05 21:45:56 +0200 | [diff] [blame] | 418 | spin_unlock_irqrestore(&crc->lock, flags); |
Tomeu Vizoso | 1aa81be | 2017-01-02 13:59:09 +0100 | [diff] [blame] | 419 | |
Benjamin Gaignard | 9f54742 | 2017-01-06 10:15:03 +0100 | [diff] [blame] | 420 | wake_up_interruptible(&crc->wq); |
| 421 | |
Tomeu Vizoso | 9edbf1f | 2016-10-06 17:21:06 +0200 | [diff] [blame] | 422 | return 0; |
| 423 | } |
| 424 | EXPORT_SYMBOL_GPL(drm_crtc_add_crc_entry); |