2 * Created: Sun Dec 21 13:08:50 2008 by bgamari@gmail.com
4 * Copyright 2008 Ben Gamari <bgamari@gmail.com>
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:
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
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.
26 #include <linux/debugfs.h>
27 #include <linux/seq_file.h>
28 #include <linux/slab.h>
29 #include <linux/export.h>
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>
39 #include "drm_internal.h"
40 #include "drm_crtc_internal.h"
42 #if defined(CONFIG_DEBUG_FS)
44 /***************************************************
45 * Initialization, etc.
46 **************************************************/
48 static int drm_name_info(struct seq_file
*m
, void *data
)
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
;
55 mutex_lock(&dev
->master_mutex
);
57 seq_printf(m
, "%s", dev
->driver
->name
);
59 seq_printf(m
, " dev=%s", dev_name(dev
->dev
));
60 if (master
&& master
->unique
)
61 seq_printf(m
, " master=%s", master
->unique
);
63 seq_printf(m
, " unique=%s", dev
->unique
);
65 mutex_unlock(&dev
->master_mutex
);
70 static int drm_clients_info(struct seq_file
*m
, void *data
)
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
;
78 "%20s %5s %3s master a %5s %10s\n",
85 /* dev->filelist is sorted youngest first, but we want to present
86 * oldest first (i.e. kernel, servers, clients), so walk backwardss.
88 mutex_lock(&dev
->filelist_mutex
);
89 list_for_each_entry_reverse(priv
, &dev
->filelist
, lhead
) {
90 struct task_struct
*task
;
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>",
99 drm_is_current_master(priv
) ? 'y' : 'n',
100 priv
->authenticated
? 'y' : 'n',
101 from_kuid_munged(seq_user_ns(m
), uid
),
105 mutex_unlock(&dev
->filelist_mutex
);
109 static int drm_gem_one_name_info(int id
, void *ptr
, void *data
)
111 struct drm_gem_object
*obj
= ptr
;
112 struct seq_file
*m
= data
;
114 seq_printf(m
, "%6d %8zd %7d %8d\n",
115 obj
->name
, obj
->size
,
117 kref_read(&obj
->refcount
));
121 static int drm_gem_name_info(struct seq_file
*m
, void *data
)
123 struct drm_info_node
*node
= (struct drm_info_node
*) m
->private;
124 struct drm_device
*dev
= node
->minor
->dev
;
126 seq_printf(m
, " name size handles refcount\n");
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
);
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
},
140 #define DRM_DEBUGFS_ENTRIES ARRAY_SIZE(drm_debugfs_list)
143 static int drm_debugfs_open(struct inode
*inode
, struct file
*file
)
145 struct drm_info_node
*node
= inode
->i_private
;
147 return single_open(file
, node
->info_ent
->show
, node
);
151 static const struct file_operations drm_debugfs_fops
= {
152 .owner
= THIS_MODULE
,
153 .open
= drm_debugfs_open
,
156 .release
= single_release
,
161 * drm_debugfs_create_files - Initialize a given set of debugfs files for DRM
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
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().
172 int drm_debugfs_create_files(const struct drm_info_list
*files
, int count
,
173 struct dentry
*root
, struct drm_minor
*minor
)
175 struct drm_device
*dev
= minor
->dev
;
177 struct drm_info_node
*tmp
;
180 for (i
= 0; i
< count
; i
++) {
181 u32 features
= files
[i
].driver_features
;
184 (dev
->driver
->driver_features
& features
) != features
)
187 tmp
= kmalloc(sizeof(struct drm_info_node
), GFP_KERNEL
);
192 ent
= debugfs_create_file(files
[i
].name
, S_IFREG
| S_IRUGO
,
193 root
, tmp
, &drm_debugfs_fops
);
195 DRM_ERROR("Cannot create /sys/kernel/debug/dri/%pd/%s\n",
196 root
, files
[i
].name
);
204 tmp
->info_ent
= &files
[i
];
206 mutex_lock(&minor
->debugfs_lock
);
207 list_add(&tmp
->list
, &minor
->debugfs_list
);
208 mutex_unlock(&minor
->debugfs_lock
);
213 drm_debugfs_remove_files(files
, count
, minor
);
216 EXPORT_SYMBOL(drm_debugfs_create_files
);
218 int drm_debugfs_init(struct drm_minor
*minor
, int minor_id
,
221 struct drm_device
*dev
= minor
->dev
;
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
);
234 ret
= drm_debugfs_create_files(drm_debugfs_list
, DRM_DEBUGFS_ENTRIES
,
235 minor
->debugfs_root
, minor
);
237 debugfs_remove(minor
->debugfs_root
);
238 minor
->debugfs_root
= NULL
;
239 DRM_ERROR("Failed to create core drm debugfs files\n");
243 if (drm_drv_uses_atomic_modeset(dev
)) {
244 ret
= drm_atomic_debugfs_init(minor
);
246 DRM_ERROR("Failed to create atomic debugfs files\n");
251 if (drm_core_check_feature(dev
, DRIVER_MODESET
)) {
252 ret
= drm_framebuffer_debugfs_init(minor
);
254 DRM_ERROR("Failed to create framebuffer debugfs file\n");
258 ret
= drm_client_debugfs_init(minor
);
260 DRM_ERROR("Failed to create client debugfs file\n");
265 if (dev
->driver
->debugfs_init
) {
266 ret
= dev
->driver
->debugfs_init(minor
);
268 DRM_ERROR("DRM: Driver failed to initialize "
269 "/sys/kernel/debug/dri.\n");
277 int drm_debugfs_remove_files(const struct drm_info_list
*files
, int count
,
278 struct drm_minor
*minor
)
280 struct list_head
*pos
, *q
;
281 struct drm_info_node
*tmp
;
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
);
295 mutex_unlock(&minor
->debugfs_lock
);
298 EXPORT_SYMBOL(drm_debugfs_remove_files
);
300 static void drm_debugfs_remove_all_files(struct drm_minor
*minor
)
302 struct drm_info_node
*node
, *tmp
;
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
);
310 mutex_unlock(&minor
->debugfs_lock
);
313 int drm_debugfs_cleanup(struct drm_minor
*minor
)
315 if (!minor
->debugfs_root
)
318 drm_debugfs_remove_all_files(minor
);
320 debugfs_remove_recursive(minor
->debugfs_root
);
321 minor
->debugfs_root
= NULL
;
326 static int connector_show(struct seq_file
*m
, void *data
)
328 struct drm_connector
*connector
= m
->private;
330 seq_printf(m
, "%s\n", drm_get_connector_force_name(connector
->force
));
335 static int connector_open(struct inode
*inode
, struct file
*file
)
337 struct drm_connector
*dev
= inode
->i_private
;
339 return single_open(file
, connector_show
, dev
);
342 static ssize_t
connector_write(struct file
*file
, const char __user
*ubuf
,
343 size_t len
, loff_t
*offp
)
345 struct seq_file
*m
= file
->private_data
;
346 struct drm_connector
*connector
= m
->private;
349 if (len
> sizeof(buf
) - 1)
352 if (copy_from_user(buf
, ubuf
, len
))
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
;
371 static int edid_show(struct seq_file
*m
, void *data
)
373 struct drm_connector
*connector
= m
->private;
374 struct drm_property_blob
*edid
= connector
->edid_blob_ptr
;
376 if (connector
->override_edid
&& edid
)
377 seq_write(m
, edid
->data
, edid
->length
);
382 static int edid_open(struct inode
*inode
, struct file
*file
)
384 struct drm_connector
*dev
= inode
->i_private
;
386 return single_open(file
, edid_show
, dev
);
389 static ssize_t
edid_write(struct file
*file
, const char __user
*ubuf
,
390 size_t len
, loff_t
*offp
)
392 struct seq_file
*m
= file
->private_data
;
393 struct drm_connector
*connector
= m
->private;
398 buf
= memdup_user(ubuf
, len
);
402 edid
= (struct edid
*) buf
;
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
)
411 connector
->override_edid
= false;
412 ret
= drm_connector_update_edid_property(connector
, edid
);
414 connector
->override_edid
= true;
419 return (ret
) ? ret
: len
;
422 static const struct file_operations drm_edid_fops
= {
423 .owner
= THIS_MODULE
,
427 .release
= single_release
,
432 static const struct file_operations drm_connector_fops
= {
433 .owner
= THIS_MODULE
,
434 .open
= connector_open
,
437 .release
= single_release
,
438 .write
= connector_write
441 int drm_debugfs_connector_add(struct drm_connector
*connector
)
443 struct drm_minor
*minor
= connector
->dev
->primary
;
444 struct dentry
*root
, *ent
;
446 if (!minor
->debugfs_root
)
449 root
= debugfs_create_dir(connector
->name
, minor
->debugfs_root
);
453 connector
->debugfs_entry
= root
;
456 ent
= debugfs_create_file("force", S_IRUGO
| S_IWUSR
, root
, connector
,
457 &drm_connector_fops
);
462 ent
= debugfs_create_file("edid_override", S_IRUGO
| S_IWUSR
, root
,
463 connector
, &drm_edid_fops
);
470 debugfs_remove_recursive(connector
->debugfs_entry
);
471 connector
->debugfs_entry
= NULL
;
475 void drm_debugfs_connector_remove(struct drm_connector
*connector
)
477 if (!connector
->debugfs_entry
)
480 debugfs_remove_recursive(connector
->debugfs_entry
);
482 connector
->debugfs_entry
= NULL
;
485 int drm_debugfs_crtc_add(struct drm_crtc
*crtc
)
487 struct drm_minor
*minor
= crtc
->dev
->primary
;
491 name
= kasprintf(GFP_KERNEL
, "crtc-%d", crtc
->index
);
495 root
= debugfs_create_dir(name
, minor
->debugfs_root
);
500 crtc
->debugfs_entry
= root
;
502 if (drm_debugfs_crtc_crc_add(crtc
))
508 drm_debugfs_crtc_remove(crtc
);
512 void drm_debugfs_crtc_remove(struct drm_crtc
*crtc
)
514 debugfs_remove_recursive(crtc
->debugfs_entry
);
515 crtc
->debugfs_entry
= NULL
;
518 #endif /* CONFIG_DEBUG_FS */