1 /* 2 * Created: Sun Dec 21 13:08:50 2008 by bgamari@gmail.com 3 * 4 * Copyright 2008 Ben Gamari <bgamari@gmail.com> 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the next 14 * paragraph) shall be included in all copies or substantial portions of the 15 * Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23 * OTHER DEALINGS IN THE SOFTWARE. 24 */ 25 26 #include <linux/debugfs.h> 27 #include <linux/seq_file.h> 28 #include <linux/slab.h> 29 #include <linux/export.h> 30 31 #include <drm/drm_client.h> 32 #include <drm/drm_debugfs.h> 33 #include <drm/drm_edid.h> 34 #include <drm/drm_atomic.h> 35 #include <drm/drm_auth.h> 36 #include <drm/drm_gem.h> 37 #include <drm/drmP.h> 38 39 #include "drm_internal.h" 40 #include "drm_crtc_internal.h" 41 42 #if defined(CONFIG_DEBUG_FS) 43 44 /*************************************************** 45 * Initialization, etc. 46 **************************************************/ 47 48 static int drm_name_info(struct seq_file *m, void *data) 49 { 50 struct drm_info_node *node = (struct drm_info_node *) m->private; 51 struct drm_minor *minor = node->minor; 52 struct drm_device *dev = minor->dev; 53 struct drm_master *master; 54 55 mutex_lock(&dev->master_mutex); 56 master = dev->master; 57 seq_printf(m, "%s", dev->driver->name); 58 if (dev->dev) 59 seq_printf(m, " dev=%s", dev_name(dev->dev)); 60 if (master && master->unique) 61 seq_printf(m, " master=%s", master->unique); 62 if (dev->unique) 63 seq_printf(m, " unique=%s", dev->unique); 64 seq_printf(m, "\n"); 65 mutex_unlock(&dev->master_mutex); 66 67 return 0; 68 } 69 70 static int drm_clients_info(struct seq_file *m, void *data) 71 { 72 struct drm_info_node *node = (struct drm_info_node *) m->private; 73 struct drm_device *dev = node->minor->dev; 74 struct drm_file *priv; 75 kuid_t uid; 76 77 seq_printf(m, 78 "%20s %5s %3s master a %5s %10s\n", 79 "command", 80 "pid", 81 "dev", 82 "uid", 83 "magic"); 84 85 /* dev->filelist is sorted youngest first, but we want to present 86 * oldest first (i.e. kernel, servers, clients), so walk backwardss. 87 */ 88 mutex_lock(&dev->filelist_mutex); 89 list_for_each_entry_reverse(priv, &dev->filelist, lhead) { 90 struct task_struct *task; 91 92 rcu_read_lock(); /* locks pid_task()->comm */ 93 task = pid_task(priv->pid, PIDTYPE_PID); 94 uid = task ? __task_cred(task)->euid : GLOBAL_ROOT_UID; 95 seq_printf(m, "%20s %5d %3d %c %c %5d %10u\n", 96 task ? task->comm : "<unknown>", 97 pid_vnr(priv->pid), 98 priv->minor->index, 99 drm_is_current_master(priv) ? 'y' : 'n', 100 priv->authenticated ? 'y' : 'n', 101 from_kuid_munged(seq_user_ns(m), uid), 102 priv->magic); 103 rcu_read_unlock(); 104 } 105 mutex_unlock(&dev->filelist_mutex); 106 return 0; 107 } 108 109 static int drm_gem_one_name_info(int id, void *ptr, void *data) 110 { 111 struct drm_gem_object *obj = ptr; 112 struct seq_file *m = data; 113 114 seq_printf(m, "%6d %8zd %7d %8d\n", 115 obj->name, obj->size, 116 obj->handle_count, 117 kref_read(&obj->refcount)); 118 return 0; 119 } 120 121 static int drm_gem_name_info(struct seq_file *m, void *data) 122 { 123 struct drm_info_node *node = (struct drm_info_node *) m->private; 124 struct drm_device *dev = node->minor->dev; 125 126 seq_printf(m, " name size handles refcount\n"); 127 128 mutex_lock(&dev->object_name_lock); 129 idr_for_each(&dev->object_name_idr, drm_gem_one_name_info, m); 130 mutex_unlock(&dev->object_name_lock); 131 132 return 0; 133 } 134 135 static const struct drm_info_list drm_debugfs_list[] = { 136 {"name", drm_name_info, 0}, 137 {"clients", drm_clients_info, 0}, 138 {"gem_names", drm_gem_name_info, DRIVER_GEM}, 139 }; 140 #define DRM_DEBUGFS_ENTRIES ARRAY_SIZE(drm_debugfs_list) 141 142 143 static int drm_debugfs_open(struct inode *inode, struct file *file) 144 { 145 struct drm_info_node *node = inode->i_private; 146 147 return single_open(file, node->info_ent->show, node); 148 } 149 150 151 static const struct file_operations drm_debugfs_fops = { 152 .owner = THIS_MODULE, 153 .open = drm_debugfs_open, 154 .read = seq_read, 155 .llseek = seq_lseek, 156 .release = single_release, 157 }; 158 159 160 /** 161 * drm_debugfs_create_files - Initialize a given set of debugfs files for DRM 162 * minor 163 * @files: The array of files to create 164 * @count: The number of files given 165 * @root: DRI debugfs dir entry. 166 * @minor: device minor number 167 * 168 * Create a given set of debugfs files represented by an array of 169 * &struct drm_info_list in the given root directory. These files will be removed 170 * automatically on drm_debugfs_cleanup(). 171 */ 172 int drm_debugfs_create_files(const struct drm_info_list *files, int count, 173 struct dentry *root, struct drm_minor *minor) 174 { 175 struct drm_device *dev = minor->dev; 176 struct dentry *ent; 177 struct drm_info_node *tmp; 178 int i, ret; 179 180 for (i = 0; i < count; i++) { 181 u32 features = files[i].driver_features; 182 183 if (features != 0 && 184 (dev->driver->driver_features & features) != features) 185 continue; 186 187 tmp = kmalloc(sizeof(struct drm_info_node), GFP_KERNEL); 188 if (tmp == NULL) { 189 ret = -1; 190 goto fail; 191 } 192 ent = debugfs_create_file(files[i].name, S_IFREG | S_IRUGO, 193 root, tmp, &drm_debugfs_fops); 194 if (!ent) { 195 DRM_ERROR("Cannot create /sys/kernel/debug/dri/%pd/%s\n", 196 root, files[i].name); 197 kfree(tmp); 198 ret = -1; 199 goto fail; 200 } 201 202 tmp->minor = minor; 203 tmp->dent = ent; 204 tmp->info_ent = &files[i]; 205 206 mutex_lock(&minor->debugfs_lock); 207 list_add(&tmp->list, &minor->debugfs_list); 208 mutex_unlock(&minor->debugfs_lock); 209 } 210 return 0; 211 212 fail: 213 drm_debugfs_remove_files(files, count, minor); 214 return ret; 215 } 216 EXPORT_SYMBOL(drm_debugfs_create_files); 217 218 int drm_debugfs_init(struct drm_minor *minor, int minor_id, 219 struct dentry *root) 220 { 221 struct drm_device *dev = minor->dev; 222 char name[64]; 223 int ret; 224 225 INIT_LIST_HEAD(&minor->debugfs_list); 226 mutex_init(&minor->debugfs_lock); 227 sprintf(name, "%d", minor_id); 228 minor->debugfs_root = debugfs_create_dir(name, root); 229 if (!minor->debugfs_root) { 230 DRM_ERROR("Cannot create /sys/kernel/debug/dri/%s\n", name); 231 return -1; 232 } 233 234 ret = drm_debugfs_create_files(drm_debugfs_list, DRM_DEBUGFS_ENTRIES, 235 minor->debugfs_root, minor); 236 if (ret) { 237 debugfs_remove(minor->debugfs_root); 238 minor->debugfs_root = NULL; 239 DRM_ERROR("Failed to create core drm debugfs files\n"); 240 return ret; 241 } 242 243 if (drm_drv_uses_atomic_modeset(dev)) { 244 ret = drm_atomic_debugfs_init(minor); 245 if (ret) { 246 DRM_ERROR("Failed to create atomic debugfs files\n"); 247 return ret; 248 } 249 } 250 251 if (drm_core_check_feature(dev, DRIVER_MODESET)) { 252 ret = drm_framebuffer_debugfs_init(minor); 253 if (ret) { 254 DRM_ERROR("Failed to create framebuffer debugfs file\n"); 255 return ret; 256 } 257 258 ret = drm_client_debugfs_init(minor); 259 if (ret) { 260 DRM_ERROR("Failed to create client debugfs file\n"); 261 return ret; 262 } 263 } 264 265 if (dev->driver->debugfs_init) { 266 ret = dev->driver->debugfs_init(minor); 267 if (ret) { 268 DRM_ERROR("DRM: Driver failed to initialize " 269 "/sys/kernel/debug/dri.\n"); 270 return ret; 271 } 272 } 273 return 0; 274 } 275 276 277 int drm_debugfs_remove_files(const struct drm_info_list *files, int count, 278 struct drm_minor *minor) 279 { 280 struct list_head *pos, *q; 281 struct drm_info_node *tmp; 282 int i; 283 284 mutex_lock(&minor->debugfs_lock); 285 for (i = 0; i < count; i++) { 286 list_for_each_safe(pos, q, &minor->debugfs_list) { 287 tmp = list_entry(pos, struct drm_info_node, list); 288 if (tmp->info_ent == &files[i]) { 289 debugfs_remove(tmp->dent); 290 list_del(pos); 291 kfree(tmp); 292 } 293 } 294 } 295 mutex_unlock(&minor->debugfs_lock); 296 return 0; 297 } 298 EXPORT_SYMBOL(drm_debugfs_remove_files); 299 300 static void drm_debugfs_remove_all_files(struct drm_minor *minor) 301 { 302 struct drm_info_node *node, *tmp; 303 304 mutex_lock(&minor->debugfs_lock); 305 list_for_each_entry_safe(node, tmp, &minor->debugfs_list, list) { 306 debugfs_remove(node->dent); 307 list_del(&node->list); 308 kfree(node); 309 } 310 mutex_unlock(&minor->debugfs_lock); 311 } 312 313 int drm_debugfs_cleanup(struct drm_minor *minor) 314 { 315 if (!minor->debugfs_root) 316 return 0; 317 318 drm_debugfs_remove_all_files(minor); 319 320 debugfs_remove_recursive(minor->debugfs_root); 321 minor->debugfs_root = NULL; 322 323 return 0; 324 } 325 326 static int connector_show(struct seq_file *m, void *data) 327 { 328 struct drm_connector *connector = m->private; 329 330 seq_printf(m, "%s\n", drm_get_connector_force_name(connector->force)); 331 332 return 0; 333 } 334 335 static int connector_open(struct inode *inode, struct file *file) 336 { 337 struct drm_connector *dev = inode->i_private; 338 339 return single_open(file, connector_show, dev); 340 } 341 342 static ssize_t connector_write(struct file *file, const char __user *ubuf, 343 size_t len, loff_t *offp) 344 { 345 struct seq_file *m = file->private_data; 346 struct drm_connector *connector = m->private; 347 char buf[12]; 348 349 if (len > sizeof(buf) - 1) 350 return -EINVAL; 351 352 if (copy_from_user(buf, ubuf, len)) 353 return -EFAULT; 354 355 buf[len] = '\0'; 356 357 if (!strcmp(buf, "on")) 358 connector->force = DRM_FORCE_ON; 359 else if (!strcmp(buf, "digital")) 360 connector->force = DRM_FORCE_ON_DIGITAL; 361 else if (!strcmp(buf, "off")) 362 connector->force = DRM_FORCE_OFF; 363 else if (!strcmp(buf, "unspecified")) 364 connector->force = DRM_FORCE_UNSPECIFIED; 365 else 366 return -EINVAL; 367 368 return len; 369 } 370 371 static int edid_show(struct seq_file *m, void *data) 372 { 373 struct drm_connector *connector = m->private; 374 struct drm_property_blob *edid = connector->edid_blob_ptr; 375 376 if (connector->override_edid && edid) 377 seq_write(m, edid->data, edid->length); 378 379 return 0; 380 } 381 382 static int edid_open(struct inode *inode, struct file *file) 383 { 384 struct drm_connector *dev = inode->i_private; 385 386 return single_open(file, edid_show, dev); 387 } 388 389 static ssize_t edid_write(struct file *file, const char __user *ubuf, 390 size_t len, loff_t *offp) 391 { 392 struct seq_file *m = file->private_data; 393 struct drm_connector *connector = m->private; 394 char *buf; 395 struct edid *edid; 396 int ret; 397 398 buf = memdup_user(ubuf, len); 399 if (IS_ERR(buf)) 400 return PTR_ERR(buf); 401 402 edid = (struct edid *) buf; 403 404 if (len == 5 && !strncmp(buf, "reset", 5)) { 405 connector->override_edid = false; 406 ret = drm_connector_update_edid_property(connector, NULL); 407 } else if (len < EDID_LENGTH || 408 EDID_LENGTH * (1 + edid->extensions) > len) 409 ret = -EINVAL; 410 else { 411 connector->override_edid = false; 412 ret = drm_connector_update_edid_property(connector, edid); 413 if (!ret) 414 connector->override_edid = true; 415 } 416 417 kfree(buf); 418 419 return (ret) ? ret : len; 420 } 421 422 static const struct file_operations drm_edid_fops = { 423 .owner = THIS_MODULE, 424 .open = edid_open, 425 .read = seq_read, 426 .llseek = seq_lseek, 427 .release = single_release, 428 .write = edid_write 429 }; 430 431 432 static const struct file_operations drm_connector_fops = { 433 .owner = THIS_MODULE, 434 .open = connector_open, 435 .read = seq_read, 436 .llseek = seq_lseek, 437 .release = single_release, 438 .write = connector_write 439 }; 440 441 int drm_debugfs_connector_add(struct drm_connector *connector) 442 { 443 struct drm_minor *minor = connector->dev->primary; 444 struct dentry *root, *ent; 445 446 if (!minor->debugfs_root) 447 return -1; 448 449 root = debugfs_create_dir(connector->name, minor->debugfs_root); 450 if (!root) 451 return -ENOMEM; 452 453 connector->debugfs_entry = root; 454 455 /* force */ 456 ent = debugfs_create_file("force", S_IRUGO | S_IWUSR, root, connector, 457 &drm_connector_fops); 458 if (!ent) 459 goto error; 460 461 /* edid */ 462 ent = debugfs_create_file("edid_override", S_IRUGO | S_IWUSR, root, 463 connector, &drm_edid_fops); 464 if (!ent) 465 goto error; 466 467 return 0; 468 469 error: 470 debugfs_remove_recursive(connector->debugfs_entry); 471 connector->debugfs_entry = NULL; 472 return -ENOMEM; 473 } 474 475 void drm_debugfs_connector_remove(struct drm_connector *connector) 476 { 477 if (!connector->debugfs_entry) 478 return; 479 480 debugfs_remove_recursive(connector->debugfs_entry); 481 482 connector->debugfs_entry = NULL; 483 } 484 485 int drm_debugfs_crtc_add(struct drm_crtc *crtc) 486 { 487 struct drm_minor *minor = crtc->dev->primary; 488 struct dentry *root; 489 char *name; 490 491 name = kasprintf(GFP_KERNEL, "crtc-%d", crtc->index); 492 if (!name) 493 return -ENOMEM; 494 495 root = debugfs_create_dir(name, minor->debugfs_root); 496 kfree(name); 497 if (!root) 498 return -ENOMEM; 499 500 crtc->debugfs_entry = root; 501 502 if (drm_debugfs_crtc_crc_add(crtc)) 503 goto error; 504 505 return 0; 506 507 error: 508 drm_debugfs_crtc_remove(crtc); 509 return -ENOMEM; 510 } 511 512 void drm_debugfs_crtc_remove(struct drm_crtc *crtc) 513 { 514 debugfs_remove_recursive(crtc->debugfs_entry); 515 crtc->debugfs_entry = NULL; 516 } 517 518 #endif /* CONFIG_DEBUG_FS */ 519