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_bridge.h>
35 #include <drm/drm_client.h>
36 #include <drm/drm_debugfs.h>
37 #include <drm/drm_device.h>
38 #include <drm/drm_drv.h>
39 #include <drm/drm_edid.h>
40 #include <drm/drm_file.h>
41 #include <drm/drm_gem.h>
42 #include <drm/drm_managed.h>
43 #include <drm/drm_gpuvm.h>
44 
45 #include "drm_crtc_internal.h"
46 #include "drm_internal.h"
47 
48 #include <linux/android_kabi.h>
49 ANDROID_KABI_DECLONLY(dma_buf);
50 ANDROID_KABI_DECLONLY(dma_buf_attachment);
51 
52 /***************************************************
53  * Initialization, etc.
54  **************************************************/
55 
drm_name_info(struct seq_file * m,void * data)56 static int drm_name_info(struct seq_file *m, void *data)
57 {
58 	struct drm_debugfs_entry *entry = m->private;
59 	struct drm_device *dev = entry->dev;
60 	struct drm_master *master;
61 
62 	mutex_lock(&dev->master_mutex);
63 	master = dev->master;
64 	seq_printf(m, "%s", dev->driver->name);
65 	if (dev->dev)
66 		seq_printf(m, " dev=%s", dev_name(dev->dev));
67 	if (master && master->unique)
68 		seq_printf(m, " master=%s", master->unique);
69 	if (dev->unique)
70 		seq_printf(m, " unique=%s", dev->unique);
71 	seq_printf(m, "\n");
72 	mutex_unlock(&dev->master_mutex);
73 
74 	return 0;
75 }
76 
drm_clients_info(struct seq_file * m,void * data)77 static int drm_clients_info(struct seq_file *m, void *data)
78 {
79 	struct drm_debugfs_entry *entry = m->private;
80 	struct drm_device *dev = entry->dev;
81 	struct drm_file *priv;
82 	kuid_t uid;
83 
84 	seq_printf(m,
85 		   "%20s %5s %3s master a %5s %10s\n",
86 		   "command",
87 		   "tgid",
88 		   "dev",
89 		   "uid",
90 		   "magic");
91 
92 	/* dev->filelist is sorted youngest first, but we want to present
93 	 * oldest first (i.e. kernel, servers, clients), so walk backwardss.
94 	 */
95 	mutex_lock(&dev->filelist_mutex);
96 	list_for_each_entry_reverse(priv, &dev->filelist, lhead) {
97 		bool is_current_master = drm_is_current_master(priv);
98 		struct task_struct *task;
99 		struct pid *pid;
100 
101 		rcu_read_lock(); /* Locks priv->pid and pid_task()->comm! */
102 		pid = rcu_dereference(priv->pid);
103 		task = pid_task(pid, PIDTYPE_TGID);
104 		uid = task ? __task_cred(task)->euid : GLOBAL_ROOT_UID;
105 		seq_printf(m, "%20s %5d %3d   %c    %c %5d %10u\n",
106 			   task ? task->comm : "<unknown>",
107 			   pid_vnr(pid),
108 			   priv->minor->index,
109 			   is_current_master ? 'y' : 'n',
110 			   priv->authenticated ? 'y' : 'n',
111 			   from_kuid_munged(seq_user_ns(m), uid),
112 			   priv->magic);
113 		rcu_read_unlock();
114 	}
115 	mutex_unlock(&dev->filelist_mutex);
116 	return 0;
117 }
118 
drm_gem_one_name_info(int id,void * ptr,void * data)119 static int drm_gem_one_name_info(int id, void *ptr, void *data)
120 {
121 	struct drm_gem_object *obj = ptr;
122 	struct seq_file *m = data;
123 
124 	seq_printf(m, "%6d %8zd %7d %8d\n",
125 		   obj->name, obj->size,
126 		   obj->handle_count,
127 		   kref_read(&obj->refcount));
128 	return 0;
129 }
130 
drm_gem_name_info(struct seq_file * m,void * data)131 static int drm_gem_name_info(struct seq_file *m, void *data)
132 {
133 	struct drm_debugfs_entry *entry = m->private;
134 	struct drm_device *dev = entry->dev;
135 
136 	seq_printf(m, "  name     size handles refcount\n");
137 
138 	mutex_lock(&dev->object_name_lock);
139 	idr_for_each(&dev->object_name_idr, drm_gem_one_name_info, m);
140 	mutex_unlock(&dev->object_name_lock);
141 
142 	return 0;
143 }
144 
145 static const struct drm_debugfs_info drm_debugfs_list[] = {
146 	{"name", drm_name_info, 0},
147 	{"clients", drm_clients_info, 0},
148 	{"gem_names", drm_gem_name_info, DRIVER_GEM},
149 };
150 #define DRM_DEBUGFS_ENTRIES ARRAY_SIZE(drm_debugfs_list)
151 
152 
drm_debugfs_open(struct inode * inode,struct file * file)153 static int drm_debugfs_open(struct inode *inode, struct file *file)
154 {
155 	struct drm_info_node *node = inode->i_private;
156 
157 	if (!device_is_registered(node->minor->kdev))
158 		return -ENODEV;
159 
160 	return single_open(file, node->info_ent->show, node);
161 }
162 
drm_debugfs_entry_open(struct inode * inode,struct file * file)163 static int drm_debugfs_entry_open(struct inode *inode, struct file *file)
164 {
165 	struct drm_debugfs_entry *entry = inode->i_private;
166 	struct drm_debugfs_info *node = &entry->file;
167 	struct drm_minor *minor = entry->dev->primary ?: entry->dev->accel;
168 
169 	if (!device_is_registered(minor->kdev))
170 		return -ENODEV;
171 
172 	return single_open(file, node->show, entry);
173 }
174 
175 static const struct file_operations drm_debugfs_entry_fops = {
176 	.owner = THIS_MODULE,
177 	.open = drm_debugfs_entry_open,
178 	.read = seq_read,
179 	.llseek = seq_lseek,
180 	.release = single_release,
181 };
182 
183 static const struct file_operations drm_debugfs_fops = {
184 	.owner = THIS_MODULE,
185 	.open = drm_debugfs_open,
186 	.read = seq_read,
187 	.llseek = seq_lseek,
188 	.release = single_release,
189 };
190 
191 /**
192  * drm_debugfs_gpuva_info - dump the given DRM GPU VA space
193  * @m: pointer to the &seq_file to write
194  * @gpuvm: the &drm_gpuvm representing the GPU VA space
195  *
196  * Dumps the GPU VA mappings of a given DRM GPU VA manager.
197  *
198  * For each DRM GPU VA space drivers should call this function from their
199  * &drm_info_list's show callback.
200  *
201  * Returns: 0 on success, -ENODEV if the &gpuvm is not initialized
202  */
drm_debugfs_gpuva_info(struct seq_file * m,struct drm_gpuvm * gpuvm)203 int drm_debugfs_gpuva_info(struct seq_file *m,
204 			   struct drm_gpuvm *gpuvm)
205 {
206 	struct drm_gpuva *va, *kva = &gpuvm->kernel_alloc_node;
207 
208 	if (!gpuvm->name)
209 		return -ENODEV;
210 
211 	seq_printf(m, "DRM GPU VA space (%s) [0x%016llx;0x%016llx]\n",
212 		   gpuvm->name, gpuvm->mm_start, gpuvm->mm_start + gpuvm->mm_range);
213 	seq_printf(m, "Kernel reserved node [0x%016llx;0x%016llx]\n",
214 		   kva->va.addr, kva->va.addr + kva->va.range);
215 	seq_puts(m, "\n");
216 	seq_puts(m, " VAs | start              | range              | end                | object             | object offset\n");
217 	seq_puts(m, "-------------------------------------------------------------------------------------------------------------\n");
218 	drm_gpuvm_for_each_va(va, gpuvm) {
219 		if (unlikely(va == kva))
220 			continue;
221 
222 		seq_printf(m, "     | 0x%016llx | 0x%016llx | 0x%016llx | 0x%016llx | 0x%016llx\n",
223 			   va->va.addr, va->va.range, va->va.addr + va->va.range,
224 			   (u64)(uintptr_t)va->gem.obj, va->gem.offset);
225 	}
226 
227 	return 0;
228 }
229 EXPORT_SYMBOL(drm_debugfs_gpuva_info);
230 
231 /**
232  * drm_debugfs_create_files - Initialize a given set of debugfs files for DRM
233  * 			minor
234  * @files: The array of files to create
235  * @count: The number of files given
236  * @root: DRI debugfs dir entry.
237  * @minor: device minor number
238  *
239  * Create a given set of debugfs files represented by an array of
240  * &struct drm_info_list in the given root directory. These files will be removed
241  * automatically on drm_debugfs_dev_fini().
242  */
drm_debugfs_create_files(const struct drm_info_list * files,int count,struct dentry * root,struct drm_minor * minor)243 void drm_debugfs_create_files(const struct drm_info_list *files, int count,
244 			      struct dentry *root, struct drm_minor *minor)
245 {
246 	struct drm_device *dev = minor->dev;
247 	struct drm_info_node *tmp;
248 	int i;
249 
250 	for (i = 0; i < count; i++) {
251 		u32 features = files[i].driver_features;
252 
253 		if (features && !drm_core_check_all_features(dev, features))
254 			continue;
255 
256 		tmp = drmm_kzalloc(dev, sizeof(*tmp), GFP_KERNEL);
257 		if (tmp == NULL)
258 			continue;
259 
260 		tmp->minor = minor;
261 		tmp->dent = debugfs_create_file(files[i].name,
262 						0444, root, tmp,
263 						&drm_debugfs_fops);
264 		tmp->info_ent = &files[i];
265 	}
266 }
267 EXPORT_SYMBOL(drm_debugfs_create_files);
268 
drm_debugfs_remove_files(const struct drm_info_list * files,int count,struct dentry * root,struct drm_minor * minor)269 int drm_debugfs_remove_files(const struct drm_info_list *files, int count,
270 			     struct dentry *root, struct drm_minor *minor)
271 {
272 	int i;
273 
274 	for (i = 0; i < count; i++) {
275 		struct dentry *dent = debugfs_lookup(files[i].name, root);
276 
277 		if (!dent)
278 			continue;
279 
280 		drmm_kfree(minor->dev, d_inode(dent)->i_private);
281 		debugfs_remove(dent);
282 	}
283 	return 0;
284 }
285 EXPORT_SYMBOL(drm_debugfs_remove_files);
286 
287 /**
288  * drm_debugfs_dev_init - create debugfs directory for the device
289  * @dev: the device which we want to create the directory for
290  * @root: the parent directory depending on the device type
291  *
292  * Creates the debugfs directory for the device under the given root directory.
293  */
drm_debugfs_dev_init(struct drm_device * dev,struct dentry * root)294 void drm_debugfs_dev_init(struct drm_device *dev, struct dentry *root)
295 {
296 	dev->debugfs_root = debugfs_create_dir(dev->unique, root);
297 }
298 
299 /**
300  * drm_debugfs_dev_fini - cleanup debugfs directory
301  * @dev: the device to cleanup the debugfs stuff
302  *
303  * Remove the debugfs directory, might be called multiple times.
304  */
drm_debugfs_dev_fini(struct drm_device * dev)305 void drm_debugfs_dev_fini(struct drm_device *dev)
306 {
307 	debugfs_remove_recursive(dev->debugfs_root);
308 	dev->debugfs_root = NULL;
309 }
310 
drm_debugfs_dev_register(struct drm_device * dev)311 void drm_debugfs_dev_register(struct drm_device *dev)
312 {
313 	drm_debugfs_add_files(dev, drm_debugfs_list, DRM_DEBUGFS_ENTRIES);
314 
315 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
316 		drm_framebuffer_debugfs_init(dev);
317 		drm_client_debugfs_init(dev);
318 	}
319 	if (drm_drv_uses_atomic_modeset(dev))
320 		drm_atomic_debugfs_init(dev);
321 }
322 
drm_debugfs_register(struct drm_minor * minor,int minor_id,struct dentry * root)323 int drm_debugfs_register(struct drm_minor *minor, int minor_id,
324 			 struct dentry *root)
325 {
326 	struct drm_device *dev = minor->dev;
327 	char name[64];
328 
329 	sprintf(name, "%d", minor_id);
330 	minor->debugfs_symlink = debugfs_create_symlink(name, root,
331 							dev->unique);
332 
333 	/* TODO: Only for compatibility with drivers */
334 	minor->debugfs_root = dev->debugfs_root;
335 
336 	if (dev->driver->debugfs_init && dev->render != minor)
337 		dev->driver->debugfs_init(minor);
338 
339 	return 0;
340 }
341 
drm_debugfs_unregister(struct drm_minor * minor)342 void drm_debugfs_unregister(struct drm_minor *minor)
343 {
344 	debugfs_remove(minor->debugfs_symlink);
345 	minor->debugfs_symlink = NULL;
346 }
347 
348 /**
349  * drm_debugfs_add_file - Add a given file to the DRM device debugfs file list
350  * @dev: drm device for the ioctl
351  * @name: debugfs file name
352  * @show: show callback
353  * @data: driver-private data, should not be device-specific
354  *
355  * Add a given file entry to the DRM device debugfs file list to be created on
356  * drm_debugfs_init.
357  */
drm_debugfs_add_file(struct drm_device * dev,const char * name,int (* show)(struct seq_file *,void *),void * data)358 void drm_debugfs_add_file(struct drm_device *dev, const char *name,
359 			  int (*show)(struct seq_file*, void*), void *data)
360 {
361 	struct drm_debugfs_entry *entry = drmm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
362 
363 	if (!entry)
364 		return;
365 
366 	entry->file.name = name;
367 	entry->file.show = show;
368 	entry->file.data = data;
369 	entry->dev = dev;
370 
371 	debugfs_create_file(name, 0444, dev->debugfs_root, entry,
372 			    &drm_debugfs_entry_fops);
373 }
374 EXPORT_SYMBOL(drm_debugfs_add_file);
375 
376 /**
377  * drm_debugfs_add_files - Add an array of files to the DRM device debugfs file list
378  * @dev: drm device for the ioctl
379  * @files: The array of files to create
380  * @count: The number of files given
381  *
382  * Add a given set of debugfs files represented by an array of
383  * &struct drm_debugfs_info in the DRM device debugfs file list.
384  */
drm_debugfs_add_files(struct drm_device * dev,const struct drm_debugfs_info * files,int count)385 void drm_debugfs_add_files(struct drm_device *dev, const struct drm_debugfs_info *files, int count)
386 {
387 	int i;
388 
389 	for (i = 0; i < count; i++)
390 		drm_debugfs_add_file(dev, files[i].name, files[i].show, files[i].data);
391 }
392 EXPORT_SYMBOL(drm_debugfs_add_files);
393 
connector_show(struct seq_file * m,void * data)394 static int connector_show(struct seq_file *m, void *data)
395 {
396 	struct drm_connector *connector = m->private;
397 
398 	seq_printf(m, "%s\n", drm_get_connector_force_name(connector->force));
399 
400 	return 0;
401 }
402 
connector_open(struct inode * inode,struct file * file)403 static int connector_open(struct inode *inode, struct file *file)
404 {
405 	struct drm_connector *dev = inode->i_private;
406 
407 	return single_open(file, connector_show, dev);
408 }
409 
connector_write(struct file * file,const char __user * ubuf,size_t len,loff_t * offp)410 static ssize_t connector_write(struct file *file, const char __user *ubuf,
411 			       size_t len, loff_t *offp)
412 {
413 	struct seq_file *m = file->private_data;
414 	struct drm_connector *connector = m->private;
415 	char buf[12];
416 
417 	if (len > sizeof(buf) - 1)
418 		return -EINVAL;
419 
420 	if (copy_from_user(buf, ubuf, len))
421 		return -EFAULT;
422 
423 	buf[len] = '\0';
424 
425 	if (sysfs_streq(buf, "on"))
426 		connector->force = DRM_FORCE_ON;
427 	else if (sysfs_streq(buf, "digital"))
428 		connector->force = DRM_FORCE_ON_DIGITAL;
429 	else if (sysfs_streq(buf, "off"))
430 		connector->force = DRM_FORCE_OFF;
431 	else if (sysfs_streq(buf, "unspecified"))
432 		connector->force = DRM_FORCE_UNSPECIFIED;
433 	else
434 		return -EINVAL;
435 
436 	return len;
437 }
438 
edid_show(struct seq_file * m,void * data)439 static int edid_show(struct seq_file *m, void *data)
440 {
441 	return drm_edid_override_show(m->private, m);
442 }
443 
edid_open(struct inode * inode,struct file * file)444 static int edid_open(struct inode *inode, struct file *file)
445 {
446 	struct drm_connector *dev = inode->i_private;
447 
448 	return single_open(file, edid_show, dev);
449 }
450 
edid_write(struct file * file,const char __user * ubuf,size_t len,loff_t * offp)451 static ssize_t edid_write(struct file *file, const char __user *ubuf,
452 			  size_t len, loff_t *offp)
453 {
454 	struct seq_file *m = file->private_data;
455 	struct drm_connector *connector = m->private;
456 	char *buf;
457 	int ret;
458 
459 	buf = memdup_user(ubuf, len);
460 	if (IS_ERR(buf))
461 		return PTR_ERR(buf);
462 
463 	if (len == 5 && !strncmp(buf, "reset", 5))
464 		ret = drm_edid_override_reset(connector);
465 	else
466 		ret = drm_edid_override_set(connector, buf, len);
467 
468 	kfree(buf);
469 
470 	return ret ? ret : len;
471 }
472 
473 /*
474  * Returns the min and max vrr vfreq through the connector's debugfs file.
475  * Example usage: cat /sys/kernel/debug/dri/0/DP-1/vrr_range
476  */
vrr_range_show(struct seq_file * m,void * data)477 static int vrr_range_show(struct seq_file *m, void *data)
478 {
479 	struct drm_connector *connector = m->private;
480 
481 	if (connector->status != connector_status_connected)
482 		return -ENODEV;
483 
484 	seq_printf(m, "Min: %u\n", connector->display_info.monitor_range.min_vfreq);
485 	seq_printf(m, "Max: %u\n", connector->display_info.monitor_range.max_vfreq);
486 
487 	return 0;
488 }
489 DEFINE_SHOW_ATTRIBUTE(vrr_range);
490 
491 /*
492  * Returns Connector's max supported bpc through debugfs file.
493  * Example usage: cat /sys/kernel/debug/dri/0/DP-1/output_bpc
494  */
output_bpc_show(struct seq_file * m,void * data)495 static int output_bpc_show(struct seq_file *m, void *data)
496 {
497 	struct drm_connector *connector = m->private;
498 
499 	if (connector->status != connector_status_connected)
500 		return -ENODEV;
501 
502 	seq_printf(m, "Maximum: %u\n", connector->display_info.bpc);
503 
504 	return 0;
505 }
506 DEFINE_SHOW_ATTRIBUTE(output_bpc);
507 
508 static const struct file_operations drm_edid_fops = {
509 	.owner = THIS_MODULE,
510 	.open = edid_open,
511 	.read = seq_read,
512 	.llseek = seq_lseek,
513 	.release = single_release,
514 	.write = edid_write
515 };
516 
517 
518 static const struct file_operations drm_connector_fops = {
519 	.owner = THIS_MODULE,
520 	.open = connector_open,
521 	.read = seq_read,
522 	.llseek = seq_lseek,
523 	.release = single_release,
524 	.write = connector_write
525 };
526 
527 static ssize_t
audio_infoframe_read(struct file * filp,char __user * ubuf,size_t count,loff_t * ppos)528 audio_infoframe_read(struct file *filp, char __user *ubuf, size_t count, loff_t *ppos)
529 {
530 	struct drm_connector_hdmi_infoframe *infoframe;
531 	struct drm_connector *connector;
532 	union hdmi_infoframe *frame;
533 	u8 buf[HDMI_INFOFRAME_SIZE(AUDIO)];
534 	ssize_t len = 0;
535 
536 	connector = filp->private_data;
537 	mutex_lock(&connector->hdmi.infoframes.lock);
538 
539 	infoframe = &connector->hdmi.infoframes.audio;
540 	if (!infoframe->set)
541 		goto out;
542 
543 	frame = &infoframe->data;
544 	len = hdmi_infoframe_pack(frame, buf, sizeof(buf));
545 	if (len < 0)
546 		goto out;
547 
548 	len = simple_read_from_buffer(ubuf, count, ppos, buf, len);
549 
550 out:
551 	mutex_unlock(&connector->hdmi.infoframes.lock);
552 	return len;
553 }
554 
555 static const struct file_operations audio_infoframe_fops = {
556 	.owner   = THIS_MODULE,
557 	.open    = simple_open,
558 	.read    = audio_infoframe_read,
559 };
560 
create_hdmi_audio_infoframe_file(struct drm_connector * connector,struct dentry * parent)561 static int create_hdmi_audio_infoframe_file(struct drm_connector *connector,
562 					    struct dentry *parent)
563 {
564 	struct dentry *file;
565 
566 	file = debugfs_create_file("audio", 0400, parent, connector, &audio_infoframe_fops);
567 	if (IS_ERR(file))
568 		return PTR_ERR(file);
569 
570 	return 0;
571 }
572 
573 #define DEFINE_INFOFRAME_FILE(_f) \
574 static ssize_t _f##_read_infoframe(struct file *filp, \
575 				   char __user *ubuf, \
576 				   size_t count,      \
577 				   loff_t *ppos)      \
578 { \
579 	struct drm_connector_hdmi_infoframe *infoframe; \
580 	struct drm_connector_state *conn_state; \
581 	struct drm_connector *connector; \
582 	union hdmi_infoframe *frame; \
583 	struct drm_device *dev; \
584 	u8 buf[HDMI_INFOFRAME_SIZE(MAX)]; \
585 	ssize_t len = 0; \
586 	\
587 	connector = filp->private_data; \
588 	dev = connector->dev; \
589 	\
590 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL); \
591 	\
592 	conn_state = connector->state; \
593 	infoframe = &conn_state->hdmi.infoframes._f; \
594 	if (!infoframe->set) \
595 		goto out; \
596 	\
597 	frame = &infoframe->data; \
598 	len = hdmi_infoframe_pack(frame, buf, sizeof(buf)); \
599 	if (len < 0) \
600 		goto out; \
601 	\
602 	len = simple_read_from_buffer(ubuf, count, ppos, buf, len); \
603 	\
604 out: \
605 	drm_modeset_unlock(&dev->mode_config.connection_mutex); \
606 	return len; \
607 } \
608 \
609 static const struct file_operations _f##_infoframe_fops = { \
610 	.owner = THIS_MODULE, \
611 	.open = simple_open, \
612 	.read = _f##_read_infoframe, \
613 }; \
614 \
615 static int create_hdmi_## _f ## _infoframe_file(struct drm_connector *connector, \
616 						struct dentry *parent) \
617 { \
618 	struct dentry *file; \
619 	\
620 	file = debugfs_create_file(#_f, 0400, parent, connector, &_f ## _infoframe_fops); \
621 	if (IS_ERR(file)) \
622 		return PTR_ERR(file); \
623 	\
624 	return 0; \
625 }
626 
627 DEFINE_INFOFRAME_FILE(avi);
628 DEFINE_INFOFRAME_FILE(hdmi);
629 DEFINE_INFOFRAME_FILE(hdr_drm);
630 DEFINE_INFOFRAME_FILE(spd);
631 
create_hdmi_infoframe_files(struct drm_connector * connector,struct dentry * parent)632 static int create_hdmi_infoframe_files(struct drm_connector *connector,
633 				       struct dentry *parent)
634 {
635 	int ret;
636 
637 	ret = create_hdmi_audio_infoframe_file(connector, parent);
638 	if (ret)
639 		return ret;
640 
641 	ret = create_hdmi_avi_infoframe_file(connector, parent);
642 	if (ret)
643 		return ret;
644 
645 	ret = create_hdmi_hdmi_infoframe_file(connector, parent);
646 	if (ret)
647 		return ret;
648 
649 	ret = create_hdmi_hdr_drm_infoframe_file(connector, parent);
650 	if (ret)
651 		return ret;
652 
653 	ret = create_hdmi_spd_infoframe_file(connector, parent);
654 	if (ret)
655 		return ret;
656 
657 	return 0;
658 }
659 
hdmi_debugfs_add(struct drm_connector * connector)660 static void hdmi_debugfs_add(struct drm_connector *connector)
661 {
662 	struct dentry *dir;
663 
664 	if (!(connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
665 	      connector->connector_type == DRM_MODE_CONNECTOR_HDMIB))
666 		return;
667 
668 	dir = debugfs_create_dir("infoframes", connector->debugfs_entry);
669 	if (IS_ERR(dir))
670 		return;
671 
672 	create_hdmi_infoframe_files(connector, dir);
673 }
674 
drm_debugfs_connector_add(struct drm_connector * connector)675 void drm_debugfs_connector_add(struct drm_connector *connector)
676 {
677 	struct drm_device *dev = connector->dev;
678 	struct dentry *root;
679 
680 	if (!dev->debugfs_root)
681 		return;
682 
683 	root = debugfs_create_dir(connector->name, dev->debugfs_root);
684 	connector->debugfs_entry = root;
685 
686 	/* force */
687 	debugfs_create_file("force", 0644, root, connector,
688 			    &drm_connector_fops);
689 
690 	/* edid */
691 	debugfs_create_file("edid_override", 0644, root, connector,
692 			    &drm_edid_fops);
693 
694 	/* vrr range */
695 	debugfs_create_file("vrr_range", 0444, root, connector,
696 			    &vrr_range_fops);
697 
698 	/* max bpc */
699 	debugfs_create_file("output_bpc", 0444, root, connector,
700 			    &output_bpc_fops);
701 
702 	hdmi_debugfs_add(connector);
703 
704 	if (connector->funcs->debugfs_init)
705 		connector->funcs->debugfs_init(connector, root);
706 }
707 
drm_debugfs_connector_remove(struct drm_connector * connector)708 void drm_debugfs_connector_remove(struct drm_connector *connector)
709 {
710 	if (!connector->debugfs_entry)
711 		return;
712 
713 	debugfs_remove_recursive(connector->debugfs_entry);
714 
715 	connector->debugfs_entry = NULL;
716 }
717 
drm_debugfs_crtc_add(struct drm_crtc * crtc)718 void drm_debugfs_crtc_add(struct drm_crtc *crtc)
719 {
720 	struct drm_device *dev = crtc->dev;
721 	struct dentry *root;
722 	char *name;
723 
724 	name = kasprintf(GFP_KERNEL, "crtc-%d", crtc->index);
725 	if (!name)
726 		return;
727 
728 	root = debugfs_create_dir(name, dev->debugfs_root);
729 	kfree(name);
730 
731 	crtc->debugfs_entry = root;
732 
733 	drm_debugfs_crtc_crc_add(crtc);
734 }
735 
drm_debugfs_crtc_remove(struct drm_crtc * crtc)736 void drm_debugfs_crtc_remove(struct drm_crtc *crtc)
737 {
738 	debugfs_remove_recursive(crtc->debugfs_entry);
739 	crtc->debugfs_entry = NULL;
740 }
741 
bridges_show(struct seq_file * m,void * data)742 static int bridges_show(struct seq_file *m, void *data)
743 {
744 	struct drm_encoder *encoder = m->private;
745 	struct drm_printer p = drm_seq_file_printer(m);
746 	struct drm_bridge *bridge;
747 	unsigned int idx = 0;
748 
749 	drm_for_each_bridge_in_chain(encoder, bridge) {
750 		drm_printf(&p, "bridge[%u]: %ps\n", idx++, bridge->funcs);
751 		drm_printf(&p, "\ttype: [%d] %s\n",
752 			   bridge->type,
753 			   drm_get_connector_type_name(bridge->type));
754 
755 		if (bridge->of_node)
756 			drm_printf(&p, "\tOF: %pOFfc\n", bridge->of_node);
757 
758 		drm_printf(&p, "\tops: [0x%x]", bridge->ops);
759 		if (bridge->ops & DRM_BRIDGE_OP_DETECT)
760 			drm_puts(&p, " detect");
761 		if (bridge->ops & DRM_BRIDGE_OP_EDID)
762 			drm_puts(&p, " edid");
763 		if (bridge->ops & DRM_BRIDGE_OP_HPD)
764 			drm_puts(&p, " hpd");
765 		if (bridge->ops & DRM_BRIDGE_OP_MODES)
766 			drm_puts(&p, " modes");
767 		if (bridge->ops & DRM_BRIDGE_OP_HDMI)
768 			drm_puts(&p, " hdmi");
769 		drm_puts(&p, "\n");
770 	}
771 
772 	return 0;
773 }
774 DEFINE_SHOW_ATTRIBUTE(bridges);
775 
drm_debugfs_encoder_add(struct drm_encoder * encoder)776 void drm_debugfs_encoder_add(struct drm_encoder *encoder)
777 {
778 	struct drm_minor *minor = encoder->dev->primary;
779 	struct dentry *root;
780 	char *name;
781 
782 	name = kasprintf(GFP_KERNEL, "encoder-%d", encoder->index);
783 	if (!name)
784 		return;
785 
786 	root = debugfs_create_dir(name, minor->debugfs_root);
787 	kfree(name);
788 
789 	encoder->debugfs_entry = root;
790 
791 	/* bridges list */
792 	debugfs_create_file("bridges", 0444, root, encoder,
793 			    &bridges_fops);
794 
795 	if (encoder->funcs && encoder->funcs->debugfs_init)
796 		encoder->funcs->debugfs_init(encoder, root);
797 }
798 
drm_debugfs_encoder_remove(struct drm_encoder * encoder)799 void drm_debugfs_encoder_remove(struct drm_encoder *encoder)
800 {
801 	debugfs_remove_recursive(encoder->debugfs_entry);
802 	encoder->debugfs_entry = NULL;
803 }
804