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/export.h>
28 #include <linux/seq_file.h>
29 #include <linux/slab.h>
30 #include <linux/uaccess.h>
31 
32 #include <drm/drm_atomic.h>
33 #include <drm/drm_auth.h>
34 #include <drm/drm_client.h>
35 #include <drm/drm_debugfs.h>
36 #include <drm/drm_device.h>
37 #include <drm/drm_drv.h>
38 #include <drm/drm_edid.h>
39 #include <drm/drm_file.h>
40 #include <drm/drm_gem.h>
41 
42 #include "drm_crtc_internal.h"
43 #include "drm_internal.h"
44 
45 #if defined(CONFIG_DEBUG_FS)
46 
47 /***************************************************
48  * Initialization, etc.
49  **************************************************/
50 
drm_name_info(struct seq_file *m, void *data)51 static int drm_name_info(struct seq_file *m, void *data)
52 {
53     struct drm_info_node *node = (struct drm_info_node *)m->private;
54     struct drm_minor *minor = node->minor;
55     struct drm_device *dev = minor->dev;
56     struct drm_master *master;
57 
58     mutex_lock(&dev->master_mutex);
59     master = dev->master;
60     seq_printf(m, "%s", dev->driver->name);
61     if (dev->dev) {
62         seq_printf(m, " dev=%s", dev_name(dev->dev));
63     }
64     if (master && master->unique) {
65         seq_printf(m, " master=%s", master->unique);
66     }
67     if (dev->unique) {
68         seq_printf(m, " unique=%s", dev->unique);
69     }
70     seq_printf(m, "\n");
71     mutex_unlock(&dev->master_mutex);
72 
73     return 0;
74 }
75 
drm_clients_info(struct seq_file *m, void *data)76 static int drm_clients_info(struct seq_file *m, void *data)
77 {
78     struct drm_info_node *node = (struct drm_info_node *)m->private;
79     struct drm_device *dev = node->minor->dev;
80     struct drm_file *priv;
81     kuid_t uid;
82 
83     seq_printf(m, "%20s %5s %3s master a %5s %10s\n", "command", "pid", "dev", "uid", "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     {
91         struct task_struct *task;
92 
93         rcu_read_lock(); /* locks pid_task()->comm */
94         task = pid_task(priv->pid, PIDTYPE_PID);
95         uid = task ? __task_cred(task)->euid : GLOBAL_ROOT_UID;
96         seq_printf(m, "%20s %5d %3d   %c    %c %5d %10u\n", task ? task->comm : "<unknown>", pid_vnr(priv->pid),
97                    priv->minor->index, drm_is_current_master(priv) ? 'y' : 'n', priv->authenticated ? 'y' : 'n',
98                    from_kuid_munged(seq_user_ns(m), uid), priv->magic);
99         rcu_read_unlock();
100     }
101     mutex_unlock(&dev->filelist_mutex);
102     return 0;
103 }
104 
drm_gem_one_name_info(int id, void *ptr, void *data)105 static int drm_gem_one_name_info(int id, void *ptr, void *data)
106 {
107     struct drm_gem_object *obj = ptr;
108     struct seq_file *m = data;
109 
110     seq_printf(m, "%6d %8zd %7d %8d\n", obj->name, obj->size, obj->handle_count, kref_read(&obj->refcount));
111     return 0;
112 }
113 
drm_gem_name_info(struct seq_file *m, void *data)114 static int drm_gem_name_info(struct seq_file *m, void *data)
115 {
116     struct drm_info_node *node = (struct drm_info_node *)m->private;
117     struct drm_device *dev = node->minor->dev;
118 
119     seq_printf(m, "  name     size handles refcount\n");
120 
121     mutex_lock(&dev->object_name_lock);
122     idr_for_each(&dev->object_name_idr, drm_gem_one_name_info, m);
123     mutex_unlock(&dev->object_name_lock);
124 
125     return 0;
126 }
127 
128 static const struct drm_info_list drm_debugfs_list[] = {
129     {"name", drm_name_info, 0},
130     {"clients", drm_clients_info, 0},
131     {"gem_names", drm_gem_name_info, DRIVER_GEM},
132 };
133 #define DRM_DEBUGFS_ENTRIES ARRAY_SIZE(drm_debugfs_list)
134 
drm_debugfs_open(struct inode *inode, struct file *file)135 static int drm_debugfs_open(struct inode *inode, struct file *file)
136 {
137     struct drm_info_node *node = inode->i_private;
138 
139     return single_open(file, node->info_ent->show, node);
140 }
141 
142 static const struct file_operations drm_debugfs_fops = {
143     .owner = THIS_MODULE,
144     .open = drm_debugfs_open,
145     .read = seq_read,
146     .llseek = seq_lseek,
147     .release = single_release,
148 };
149 
150 /**
151  * drm_debugfs_create_files - Initialize a given set of debugfs files for DRM
152  *             minor
153  * @files: The array of files to create
154  * @count: The number of files given
155  * @root: DRI debugfs dir entry.
156  * @minor: device minor number
157  *
158  * Create a given set of debugfs files represented by an array of
159  * &struct drm_info_list in the given root directory. These files will be removed
160  * automatically on drm_debugfs_cleanup().
161  */
drm_debugfs_create_files(const struct drm_info_list *files, int count, struct dentry *root, struct drm_minor *minor)162 void drm_debugfs_create_files(const struct drm_info_list *files, int count, struct dentry *root,
163                               struct drm_minor *minor)
164 {
165     struct drm_device *dev = minor->dev;
166     struct drm_info_node *tmp;
167     int i;
168 
169     for (i = 0; i < count; i++) {
170         u32 features = files[i].driver_features;
171 
172         if (features && !drm_core_check_all_features(dev, features)) {
173             continue;
174         }
175 
176         tmp = kmalloc(sizeof(struct drm_info_node), GFP_KERNEL);
177         if (tmp == NULL) {
178             continue;
179         }
180 
181         tmp->minor = minor;
182         tmp->dent = debugfs_create_file(files[i].name, S_IFREG | S_IRUGO, root, tmp, &drm_debugfs_fops);
183         tmp->info_ent = &files[i];
184 
185         mutex_lock(&minor->debugfs_lock);
186         list_add(&tmp->list, &minor->debugfs_list);
187         mutex_unlock(&minor->debugfs_lock);
188     }
189 }
190 EXPORT_SYMBOL(drm_debugfs_create_files);
191 
drm_debugfs_init(struct drm_minor *minor, int minor_id, struct dentry *root)192 int drm_debugfs_init(struct drm_minor *minor, int minor_id, struct dentry *root)
193 {
194     struct drm_device *dev = minor->dev;
195     char name[64];
196     int ret = 0;
197 
198     INIT_LIST_HEAD(&minor->debugfs_list);
199     mutex_init(&minor->debugfs_lock);
200     ret = sprintf(name, "%d", minor_id);
201     minor->debugfs_root = debugfs_create_dir(name, root);
202 
203     drm_debugfs_create_files(drm_debugfs_list, DRM_DEBUGFS_ENTRIES, minor->debugfs_root, minor);
204 
205     if (drm_drv_uses_atomic_modeset(dev)) {
206         drm_atomic_debugfs_init(minor);
207     }
208 
209     if (drm_core_check_feature(dev, DRIVER_MODESET)) {
210         drm_framebuffer_debugfs_init(minor);
211 
212         drm_client_debugfs_init(minor);
213     }
214 
215     if (dev->driver->debugfs_init) {
216         dev->driver->debugfs_init(minor);
217     }
218 
219     return 0;
220 }
221 
drm_debugfs_remove_files(const struct drm_info_list *files, int count, struct drm_minor *minor)222 int drm_debugfs_remove_files(const struct drm_info_list *files, int count, struct drm_minor *minor)
223 {
224     struct list_head *pos, *q;
225     struct drm_info_node *tmp;
226     int i;
227 
228     mutex_lock(&minor->debugfs_lock);
229     for (i = 0; i < count; i++) {
230         list_for_each_safe(pos, q, &minor->debugfs_list)
231         {
232             tmp = list_entry(pos, struct drm_info_node, list);
233             if (tmp->info_ent == &files[i]) {
234                 debugfs_remove(tmp->dent);
235                 list_del(pos);
236                 kfree(tmp);
237             }
238         }
239     }
240     mutex_unlock(&minor->debugfs_lock);
241     return 0;
242 }
243 EXPORT_SYMBOL(drm_debugfs_remove_files);
244 
drm_debugfs_remove_all_files(struct drm_minor *minor)245 static void drm_debugfs_remove_all_files(struct drm_minor *minor)
246 {
247     struct drm_info_node *node, *tmp;
248 
249     mutex_lock(&minor->debugfs_lock);
250     list_for_each_entry_safe(node, tmp, &minor->debugfs_list, list)
251     {
252         debugfs_remove(node->dent);
253         list_del(&node->list);
254         kfree(node);
255     }
256     mutex_unlock(&minor->debugfs_lock);
257 }
258 
drm_debugfs_cleanup(struct drm_minor *minor)259 void drm_debugfs_cleanup(struct drm_minor *minor)
260 {
261     if (!minor->debugfs_root) {
262         return;
263     }
264 
265     drm_debugfs_remove_all_files(minor);
266 
267     debugfs_remove_recursive(minor->debugfs_root);
268     minor->debugfs_root = NULL;
269 }
270 
connector_show(struct seq_file *m, void *data)271 static int connector_show(struct seq_file *m, void *data)
272 {
273     struct drm_connector *connector = m->private;
274 
275     seq_printf(m, "%s\n", drm_get_connector_force_name(connector->force));
276 
277     return 0;
278 }
279 
connector_open(struct inode *inode, struct file *file)280 static int connector_open(struct inode *inode, struct file *file)
281 {
282     struct drm_connector *dev = inode->i_private;
283 
284     return single_open(file, connector_show, dev);
285 }
286 
connector_write(struct file *file, const char __user *ubuf, size_t len, loff_t *offp)287 static ssize_t connector_write(struct file *file, const char __user *ubuf, size_t len, loff_t *offp)
288 {
289     struct seq_file *m = file->private_data;
290     struct drm_connector *connector = m->private;
291     char buf[12];
292 
293     if (len > sizeof(buf) - 1) {
294         return -EINVAL;
295     }
296 
297     if (copy_from_user(buf, ubuf, len)) {
298         return -EFAULT;
299     }
300 
301     buf[len] = '\0';
302 
303     if (sysfs_streq(buf, "on")) {
304         connector->force = DRM_FORCE_ON;
305     } else if (sysfs_streq(buf, "digital")) {
306         connector->force = DRM_FORCE_ON_DIGITAL;
307     } else if (sysfs_streq(buf, "off")) {
308         connector->force = DRM_FORCE_OFF;
309     } else if (sysfs_streq(buf, "unspecified")) {
310         connector->force = DRM_FORCE_UNSPECIFIED;
311     } else {
312         return -EINVAL;
313     }
314 
315     return len;
316 }
317 
edid_show(struct seq_file *m, void *data)318 static int edid_show(struct seq_file *m, void *data)
319 {
320     struct drm_connector *connector = m->private;
321     struct drm_property_blob *edid = connector->edid_blob_ptr;
322 
323     if (connector->override_edid && edid) {
324         seq_write(m, edid->data, edid->length);
325     }
326 
327     return 0;
328 }
329 
edid_open(struct inode *inode, struct file *file)330 static int edid_open(struct inode *inode, struct file *file)
331 {
332     struct drm_connector *dev = inode->i_private;
333 
334     return single_open(file, edid_show, dev);
335 }
336 
edid_write(struct file *file, const char __user *ubuf, size_t len, loff_t *offp)337 static ssize_t edid_write(struct file *file, const char __user *ubuf, size_t len, loff_t *offp)
338 {
339     struct seq_file *m = file->private_data;
340     struct drm_connector *connector = m->private;
341     char *buf;
342     struct edid *edid;
343     int ret;
344 
345     buf = memdup_user(ubuf, len);
346     if (IS_ERR(buf)) {
347         return PTR_ERR(buf);
348     }
349 
350     edid = (struct edid *)buf;
351 
352     if (len == 5 && !strncmp(buf, "reset", 5)) {
353         connector->override_edid = false;
354         ret = drm_connector_update_edid_property(connector, NULL);
355     } else if (len < EDID_LENGTH || EDID_LENGTH * (1 + edid->extensions) > len) {
356         ret = -EINVAL;
357     } else {
358         connector->override_edid = false;
359         ret = drm_connector_update_edid_property(connector, edid);
360         if (!ret) {
361             connector->override_edid = true;
362         }
363     }
364 
365     kfree(buf);
366 
367     return (ret) ? ret : len;
368 }
369 
370 /*
371  * Returns the min and max vrr vfreq through the connector's debugfs file.
372  * Example usage: cat /sys/kernel/debug/dri/0/DP-1/vrr_range
373  */
vrr_range_show(struct seq_file *m, void *data)374 static int vrr_range_show(struct seq_file *m, void *data)
375 {
376     struct drm_connector *connector = m->private;
377 
378     if (connector->status != connector_status_connected) {
379         return -ENODEV;
380     }
381 
382     seq_printf(m, "Min: %u\n", (u8)connector->display_info.monitor_range.min_vfreq);
383     seq_printf(m, "Max: %u\n", (u8)connector->display_info.monitor_range.max_vfreq);
384 
385     return 0;
386 }
387 DEFINE_SHOW_ATTRIBUTE(vrr_range);
388 
389 static const struct file_operations drm_edid_fops = {.owner = THIS_MODULE,
390                                                      .open = edid_open,
391                                                      .read = seq_read,
392                                                      .llseek = seq_lseek,
393                                                      .release = single_release,
394                                                      .write = edid_write};
395 
396 static const struct file_operations drm_connector_fops = {.owner = THIS_MODULE,
397                                                           .open = connector_open,
398                                                           .read = seq_read,
399                                                           .llseek = seq_lseek,
400                                                           .release = single_release,
401                                                           .write = connector_write};
402 
drm_debugfs_connector_add(struct drm_connector *connector)403 void drm_debugfs_connector_add(struct drm_connector *connector)
404 {
405     struct drm_minor *minor = connector->dev->primary;
406     struct dentry *root;
407 
408     if (!minor->debugfs_root) {
409         return;
410     }
411 
412     root = debugfs_create_dir(connector->name, minor->debugfs_root);
413     connector->debugfs_entry = root;
414 
415     /* force */
416     debugfs_create_file("force", S_IRUGO | S_IWUSR, root, connector, &drm_connector_fops);
417 
418     /* edid */
419     debugfs_create_file("edid_override", S_IRUGO | S_IWUSR, root, connector, &drm_edid_fops);
420 
421     /* vrr range */
422     debugfs_create_file("vrr_range", S_IRUGO, root, connector, &vrr_range_fops);
423 }
424 
drm_debugfs_connector_remove(struct drm_connector *connector)425 void drm_debugfs_connector_remove(struct drm_connector *connector)
426 {
427     if (!connector->debugfs_entry) {
428         return;
429     }
430 
431     debugfs_remove_recursive(connector->debugfs_entry);
432 
433     connector->debugfs_entry = NULL;
434 }
435 
drm_debugfs_crtc_add(struct drm_crtc *crtc)436 void drm_debugfs_crtc_add(struct drm_crtc *crtc)
437 {
438     struct drm_minor *minor = crtc->dev->primary;
439     struct dentry *root;
440     char *name;
441 
442     name = kasprintf(GFP_KERNEL, "crtc-%d", crtc->index);
443     if (!name) {
444         return;
445     }
446 
447     root = debugfs_create_dir(name, minor->debugfs_root);
448     kfree(name);
449 
450     crtc->debugfs_entry = root;
451 
452     drm_debugfs_crtc_crc_add(crtc);
453 }
454 
drm_debugfs_crtc_remove(struct drm_crtc *crtc)455 void drm_debugfs_crtc_remove(struct drm_crtc *crtc)
456 {
457     debugfs_remove_recursive(crtc->debugfs_entry);
458     crtc->debugfs_entry = NULL;
459 }
460 
461 #endif /* CONFIG_DEBUG_FS */
462