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