1 /*	$NetBSD: qxl_debugfs.c,v 1.3 2021/12/18 23:45:42 riastradh Exp $	*/
2 
3 /*
4  * Copyright (C) 2009 Red Hat <bskeggs@redhat.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial
16  * portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  */
27 
28 /*
29  * Authors:
30  *  Alon Levy <alevy@redhat.com>
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: qxl_debugfs.c,v 1.3 2021/12/18 23:45:42 riastradh Exp $");
35 
36 #include <drm/drm_debugfs.h>
37 #include <drm/drm_file.h>
38 
39 #include "qxl_drv.h"
40 #include "qxl_object.h"
41 
42 #if defined(CONFIG_DEBUG_FS)
43 static int
qxl_debugfs_irq_received(struct seq_file * m,void * data)44 qxl_debugfs_irq_received(struct seq_file *m, void *data)
45 {
46 	struct drm_info_node *node = (struct drm_info_node *) m->private;
47 	struct qxl_device *qdev = node->minor->dev->dev_private;
48 
49 	seq_printf(m, "%d\n", atomic_read(&qdev->irq_received));
50 	seq_printf(m, "%d\n", atomic_read(&qdev->irq_received_display));
51 	seq_printf(m, "%d\n", atomic_read(&qdev->irq_received_cursor));
52 	seq_printf(m, "%d\n", atomic_read(&qdev->irq_received_io_cmd));
53 	seq_printf(m, "%d\n", qdev->irq_received_error);
54 	return 0;
55 }
56 
57 static int
qxl_debugfs_buffers_info(struct seq_file * m,void * data)58 qxl_debugfs_buffers_info(struct seq_file *m, void *data)
59 {
60 	struct drm_info_node *node = (struct drm_info_node *) m->private;
61 	struct qxl_device *qdev = node->minor->dev->dev_private;
62 	struct qxl_bo *bo;
63 
64 	list_for_each_entry(bo, &qdev->gem.objects, list) {
65 		struct dma_resv_list *fobj;
66 		int rel;
67 
68 		rcu_read_lock();
69 		fobj = rcu_dereference(bo->tbo.base.resv->fence);
70 		rel = fobj ? fobj->shared_count : 0;
71 		rcu_read_unlock();
72 
73 		seq_printf(m, "size %ld, pc %d, num releases %d\n",
74 			   (unsigned long)bo->tbo.base.size,
75 			   bo->pin_count, rel);
76 	}
77 	return 0;
78 }
79 
80 static struct drm_info_list qxl_debugfs_list[] = {
81 	{ "irq_received", qxl_debugfs_irq_received, 0, NULL },
82 	{ "qxl_buffers", qxl_debugfs_buffers_info, 0, NULL },
83 };
84 #define QXL_DEBUGFS_ENTRIES ARRAY_SIZE(qxl_debugfs_list)
85 #endif
86 
87 int
qxl_debugfs_init(struct drm_minor * minor)88 qxl_debugfs_init(struct drm_minor *minor)
89 {
90 #if defined(CONFIG_DEBUG_FS)
91 	int r;
92 	struct qxl_device *dev =
93 		(struct qxl_device *) minor->dev->dev_private;
94 
95 	drm_debugfs_create_files(qxl_debugfs_list, QXL_DEBUGFS_ENTRIES,
96 				 minor->debugfs_root, minor);
97 
98 	r = qxl_ttm_debugfs_init(dev);
99 	if (r) {
100 		DRM_ERROR("Failed to init TTM debugfs\n");
101 		return r;
102 	}
103 #endif
104 	return 0;
105 }
106 
qxl_debugfs_add_files(struct qxl_device * qdev,struct drm_info_list * files,unsigned int nfiles)107 int qxl_debugfs_add_files(struct qxl_device *qdev,
108 			  struct drm_info_list *files,
109 			  unsigned int nfiles)
110 {
111 	unsigned int i;
112 
113 	for (i = 0; i < qdev->debugfs_count; i++) {
114 		if (qdev->debugfs[i].files == files) {
115 			/* Already registered */
116 			return 0;
117 		}
118 	}
119 
120 	i = qdev->debugfs_count + 1;
121 	if (i > QXL_DEBUGFS_MAX_COMPONENTS) {
122 		DRM_ERROR("Reached maximum number of debugfs components.\n");
123 		DRM_ERROR("Report so we increase QXL_DEBUGFS_MAX_COMPONENTS.\n");
124 		return -EINVAL;
125 	}
126 	qdev->debugfs[qdev->debugfs_count].files = files;
127 	qdev->debugfs[qdev->debugfs_count].num_files = nfiles;
128 	qdev->debugfs_count = i;
129 #if defined(CONFIG_DEBUG_FS)
130 	drm_debugfs_create_files(files, nfiles,
131 				 qdev->ddev.primary->debugfs_root,
132 				 qdev->ddev.primary);
133 #endif
134 	return 0;
135 }
136