1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013-2016 Red Hat
4  * Author: Rob Clark <robdclark@gmail.com>
5  */
6 
7 #ifdef CONFIG_DEBUG_FS
8 
9 #include <linux/debugfs.h>
10 #include <linux/fault-inject.h>
11 
12 #include <drm/drm_debugfs.h>
13 #include <drm/drm_file.h>
14 #include <drm/drm_framebuffer.h>
15 
16 #include "msm_drv.h"
17 #include "msm_gpu.h"
18 #include "msm_kms.h"
19 #include "msm_debugfs.h"
20 #include "disp/msm_disp_snapshot.h"
21 
22 /*
23  * GPU Snapshot:
24  */
25 
26 struct msm_gpu_show_priv {
27 	struct msm_gpu_state *state;
28 	struct drm_device *dev;
29 };
30 
msm_gpu_show(struct seq_file * m,void * arg)31 static int msm_gpu_show(struct seq_file *m, void *arg)
32 {
33 	struct drm_printer p = drm_seq_file_printer(m);
34 	struct msm_gpu_show_priv *show_priv = m->private;
35 	struct msm_drm_private *priv = show_priv->dev->dev_private;
36 	struct msm_gpu *gpu = priv->gpu;
37 	int ret;
38 
39 	ret = mutex_lock_interruptible(&gpu->lock);
40 	if (ret)
41 		return ret;
42 
43 	drm_printf(&p, "%s Status:\n", gpu->name);
44 	gpu->funcs->show(gpu, show_priv->state, &p);
45 
46 	mutex_unlock(&gpu->lock);
47 
48 	return 0;
49 }
50 
msm_gpu_release(struct inode * inode,struct file * file)51 static int msm_gpu_release(struct inode *inode, struct file *file)
52 {
53 	struct seq_file *m = file->private_data;
54 	struct msm_gpu_show_priv *show_priv = m->private;
55 	struct msm_drm_private *priv = show_priv->dev->dev_private;
56 	struct msm_gpu *gpu = priv->gpu;
57 
58 	mutex_lock(&gpu->lock);
59 	gpu->funcs->gpu_state_put(show_priv->state);
60 	mutex_unlock(&gpu->lock);
61 
62 	kfree(show_priv);
63 
64 	return single_release(inode, file);
65 }
66 
msm_gpu_open(struct inode * inode,struct file * file)67 static int msm_gpu_open(struct inode *inode, struct file *file)
68 {
69 	struct drm_device *dev = inode->i_private;
70 	struct msm_drm_private *priv = dev->dev_private;
71 	struct msm_gpu *gpu = priv->gpu;
72 	struct msm_gpu_show_priv *show_priv;
73 	int ret;
74 
75 	if (!gpu || !gpu->funcs->gpu_state_get)
76 		return -ENODEV;
77 
78 	show_priv = kmalloc(sizeof(*show_priv), GFP_KERNEL);
79 	if (!show_priv)
80 		return -ENOMEM;
81 
82 	ret = mutex_lock_interruptible(&gpu->lock);
83 	if (ret)
84 		goto free_priv;
85 
86 	pm_runtime_get_sync(&gpu->pdev->dev);
87 	msm_gpu_hw_init(gpu);
88 	show_priv->state = gpu->funcs->gpu_state_get(gpu);
89 	pm_runtime_put_sync(&gpu->pdev->dev);
90 
91 	mutex_unlock(&gpu->lock);
92 
93 	if (IS_ERR(show_priv->state)) {
94 		ret = PTR_ERR(show_priv->state);
95 		goto free_priv;
96 	}
97 
98 	show_priv->dev = dev;
99 
100 	ret = single_open(file, msm_gpu_show, show_priv);
101 	if (ret)
102 		goto free_priv;
103 
104 	return 0;
105 
106 free_priv:
107 	kfree(show_priv);
108 	return ret;
109 }
110 
111 static const struct file_operations msm_gpu_fops = {
112 	.owner = THIS_MODULE,
113 	.open = msm_gpu_open,
114 	.read = seq_read,
115 	.llseek = seq_lseek,
116 	.release = msm_gpu_release,
117 };
118 
119 /*
120  * Display Snapshot:
121  */
122 
msm_kms_show(struct seq_file * m,void * arg)123 static int msm_kms_show(struct seq_file *m, void *arg)
124 {
125 	struct drm_printer p = drm_seq_file_printer(m);
126 	struct msm_disp_state *state = m->private;
127 
128 	msm_disp_state_print(state, &p);
129 
130 	return 0;
131 }
132 
msm_kms_release(struct inode * inode,struct file * file)133 static int msm_kms_release(struct inode *inode, struct file *file)
134 {
135 	struct seq_file *m = file->private_data;
136 	struct msm_disp_state *state = m->private;
137 
138 	msm_disp_state_free(state);
139 
140 	return single_release(inode, file);
141 }
142 
msm_kms_open(struct inode * inode,struct file * file)143 static int msm_kms_open(struct inode *inode, struct file *file)
144 {
145 	struct drm_device *dev = inode->i_private;
146 	struct msm_drm_private *priv = dev->dev_private;
147 	struct msm_disp_state *state;
148 	int ret;
149 
150 	if (!priv->kms)
151 		return -ENODEV;
152 
153 	ret = mutex_lock_interruptible(&priv->kms->dump_mutex);
154 	if (ret)
155 		return ret;
156 
157 	state = msm_disp_snapshot_state_sync(priv->kms);
158 
159 	mutex_unlock(&priv->kms->dump_mutex);
160 
161 	if (IS_ERR(state)) {
162 		return PTR_ERR(state);
163 	}
164 
165 	ret = single_open(file, msm_kms_show, state);
166 	if (ret) {
167 		msm_disp_state_free(state);
168 		return ret;
169 	}
170 
171 	return 0;
172 }
173 
174 static const struct file_operations msm_kms_fops = {
175 	.owner = THIS_MODULE,
176 	.open = msm_kms_open,
177 	.read = seq_read,
178 	.llseek = seq_lseek,
179 	.release = msm_kms_release,
180 };
181 
182 /*
183  * Other debugfs:
184  */
185 
186 static unsigned long last_shrink_freed;
187 
188 static int
shrink_get(void * data,u64 * val)189 shrink_get(void *data, u64 *val)
190 {
191 	*val = last_shrink_freed;
192 
193 	return 0;
194 }
195 
196 static int
shrink_set(void * data,u64 val)197 shrink_set(void *data, u64 val)
198 {
199 	struct drm_device *dev = data;
200 
201 	last_shrink_freed = msm_gem_shrinker_shrink(dev, val);
202 
203 	return 0;
204 }
205 
206 DEFINE_DEBUGFS_ATTRIBUTE(shrink_fops,
207 			 shrink_get, shrink_set,
208 			 "0x%08llx\n");
209 
210 
msm_gem_show(struct seq_file * m,void * arg)211 static int msm_gem_show(struct seq_file *m, void *arg)
212 {
213 	struct drm_info_node *node = (struct drm_info_node *) m->private;
214 	struct drm_device *dev = node->minor->dev;
215 	struct msm_drm_private *priv = dev->dev_private;
216 	int ret;
217 
218 	ret = mutex_lock_interruptible(&priv->obj_lock);
219 	if (ret)
220 		return ret;
221 
222 	msm_gem_describe_objects(&priv->objects, m);
223 
224 	mutex_unlock(&priv->obj_lock);
225 
226 	return 0;
227 }
228 
msm_mm_show(struct seq_file * m,void * arg)229 static int msm_mm_show(struct seq_file *m, void *arg)
230 {
231 	struct drm_info_node *node = (struct drm_info_node *) m->private;
232 	struct drm_device *dev = node->minor->dev;
233 	struct drm_printer p = drm_seq_file_printer(m);
234 
235 	drm_mm_print(&dev->vma_offset_manager->vm_addr_space_mm, &p);
236 
237 	return 0;
238 }
239 
msm_fb_show(struct seq_file * m,void * arg)240 static int msm_fb_show(struct seq_file *m, void *arg)
241 {
242 	struct drm_info_node *node = (struct drm_info_node *) m->private;
243 	struct drm_device *dev = node->minor->dev;
244 	struct msm_drm_private *priv = dev->dev_private;
245 	struct drm_framebuffer *fb, *fbdev_fb = NULL;
246 
247 	if (priv->fbdev) {
248 		seq_printf(m, "fbcon ");
249 		fbdev_fb = priv->fbdev->fb;
250 		msm_framebuffer_describe(fbdev_fb, m);
251 	}
252 
253 	mutex_lock(&dev->mode_config.fb_lock);
254 	list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
255 		if (fb == fbdev_fb)
256 			continue;
257 
258 		seq_printf(m, "user ");
259 		msm_framebuffer_describe(fb, m);
260 	}
261 	mutex_unlock(&dev->mode_config.fb_lock);
262 
263 	return 0;
264 }
265 
266 static struct drm_info_list msm_debugfs_list[] = {
267 		{"gem", msm_gem_show},
268 		{ "mm", msm_mm_show },
269 		{ "fb", msm_fb_show },
270 };
271 
late_init_minor(struct drm_minor * minor)272 static int late_init_minor(struct drm_minor *minor)
273 {
274 	int ret;
275 
276 	if (!minor)
277 		return 0;
278 
279 	ret = msm_rd_debugfs_init(minor);
280 	if (ret) {
281 		DRM_DEV_ERROR(minor->dev->dev, "could not install rd debugfs\n");
282 		return ret;
283 	}
284 
285 	ret = msm_perf_debugfs_init(minor);
286 	if (ret) {
287 		DRM_DEV_ERROR(minor->dev->dev, "could not install perf debugfs\n");
288 		return ret;
289 	}
290 
291 	return 0;
292 }
293 
msm_debugfs_late_init(struct drm_device * dev)294 int msm_debugfs_late_init(struct drm_device *dev)
295 {
296 	int ret;
297 	ret = late_init_minor(dev->primary);
298 	if (ret)
299 		return ret;
300 	ret = late_init_minor(dev->render);
301 	return ret;
302 }
303 
msm_debugfs_init(struct drm_minor * minor)304 void msm_debugfs_init(struct drm_minor *minor)
305 {
306 	struct drm_device *dev = minor->dev;
307 	struct msm_drm_private *priv = dev->dev_private;
308 
309 	drm_debugfs_create_files(msm_debugfs_list,
310 				 ARRAY_SIZE(msm_debugfs_list),
311 				 minor->debugfs_root, minor);
312 
313 	debugfs_create_file("gpu", S_IRUSR, minor->debugfs_root,
314 		dev, &msm_gpu_fops);
315 
316 	debugfs_create_file("kms", S_IRUSR, minor->debugfs_root,
317 		dev, &msm_kms_fops);
318 
319 	debugfs_create_u32("hangcheck_period_ms", 0600, minor->debugfs_root,
320 		&priv->hangcheck_period);
321 
322 	debugfs_create_bool("disable_err_irq", 0600, minor->debugfs_root,
323 		&priv->disable_err_irq);
324 
325 	debugfs_create_file("shrink", S_IRWXU, minor->debugfs_root,
326 		dev, &shrink_fops);
327 
328 	if (priv->kms && priv->kms->funcs->debugfs_init)
329 		priv->kms->funcs->debugfs_init(priv->kms, minor);
330 
331 #ifdef CONFIG_FAULT_INJECTION
332 	fault_create_debugfs_attr("fail_gem_alloc", minor->debugfs_root,
333 				  &fail_gem_alloc);
334 	fault_create_debugfs_attr("fail_gem_iova", minor->debugfs_root,
335 				  &fail_gem_iova);
336 #endif
337 }
338 #endif
339 
340