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