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
11 #include <drm/drm_debugfs.h>
12 #include <drm/drm_file.h>
13
14 #include "msm_drv.h"
15 #include "msm_gpu.h"
16 #include "msm_kms.h"
17 #include "msm_debugfs.h"
18
19 struct msm_gpu_show_priv {
20 struct msm_gpu_state *state;
21 struct drm_device *dev;
22 };
23
msm_gpu_show(struct seq_file * m,void * arg)24 static int msm_gpu_show(struct seq_file *m, void *arg)
25 {
26 struct drm_printer p = drm_seq_file_printer(m);
27 struct msm_gpu_show_priv *show_priv = m->private;
28 struct msm_drm_private *priv = show_priv->dev->dev_private;
29 struct msm_gpu *gpu = priv->gpu;
30 int ret;
31
32 ret = mutex_lock_interruptible(&gpu->lock);
33 if (ret)
34 return ret;
35
36 drm_printf(&p, "%s Status:\n", gpu->name);
37 gpu->funcs->show(gpu, show_priv->state, &p);
38
39 mutex_unlock(&gpu->lock);
40
41 return 0;
42 }
43
msm_gpu_release(struct inode * inode,struct file * file)44 static int msm_gpu_release(struct inode *inode, struct file *file)
45 {
46 struct seq_file *m = file->private_data;
47 struct msm_gpu_show_priv *show_priv = m->private;
48 struct msm_drm_private *priv = show_priv->dev->dev_private;
49 struct msm_gpu *gpu = priv->gpu;
50
51 mutex_lock(&gpu->lock);
52 gpu->funcs->gpu_state_put(show_priv->state);
53 mutex_unlock(&gpu->lock);
54
55 kfree(show_priv);
56
57 return single_release(inode, file);
58 }
59
msm_gpu_open(struct inode * inode,struct file * file)60 static int msm_gpu_open(struct inode *inode, struct file *file)
61 {
62 struct drm_device *dev = inode->i_private;
63 struct msm_drm_private *priv = dev->dev_private;
64 struct msm_gpu *gpu = priv->gpu;
65 struct msm_gpu_show_priv *show_priv;
66 int ret;
67
68 if (!gpu || !gpu->funcs->gpu_state_get)
69 return -ENODEV;
70
71 show_priv = kmalloc(sizeof(*show_priv), GFP_KERNEL);
72 if (!show_priv)
73 return -ENOMEM;
74
75 ret = mutex_lock_interruptible(&gpu->lock);
76 if (ret)
77 goto free_priv;
78
79 pm_runtime_get_sync(&gpu->pdev->dev);
80 msm_gpu_hw_init(gpu);
81 show_priv->state = gpu->funcs->gpu_state_get(gpu);
82 pm_runtime_put_sync(&gpu->pdev->dev);
83
84 mutex_unlock(&gpu->lock);
85
86 if (IS_ERR(show_priv->state)) {
87 ret = PTR_ERR(show_priv->state);
88 goto free_priv;
89 }
90
91 show_priv->dev = dev;
92
93 ret = single_open(file, msm_gpu_show, show_priv);
94 if (ret)
95 goto free_priv;
96
97 return 0;
98
99 free_priv:
100 kfree(show_priv);
101 return ret;
102 }
103
104 static const struct file_operations msm_gpu_fops = {
105 .owner = THIS_MODULE,
106 .open = msm_gpu_open,
107 .read = seq_read,
108 .llseek = seq_lseek,
109 .release = msm_gpu_release,
110 };
111
112 static unsigned long last_shrink_freed;
113
114 static int
shrink_get(void * data,u64 * val)115 shrink_get(void *data, u64 *val)
116 {
117 *val = last_shrink_freed;
118
119 return 0;
120 }
121
122 static int
shrink_set(void * data,u64 val)123 shrink_set(void *data, u64 val)
124 {
125 struct drm_device *dev = data;
126
127 last_shrink_freed = msm_gem_shrinker_shrink(dev, val);
128
129 return 0;
130 }
131
132 DEFINE_SIMPLE_ATTRIBUTE(shrink_fops,
133 shrink_get, shrink_set,
134 "0x%08llx\n");
135
136
msm_gem_show(struct drm_device * dev,struct seq_file * m)137 static int msm_gem_show(struct drm_device *dev, struct seq_file *m)
138 {
139 struct msm_drm_private *priv = dev->dev_private;
140 int ret;
141
142 ret = mutex_lock_interruptible(&priv->obj_lock);
143 if (ret)
144 return ret;
145
146 msm_gem_describe_objects(&priv->objects, m);
147
148 mutex_unlock(&priv->obj_lock);
149
150 return 0;
151 }
152
msm_mm_show(struct drm_device * dev,struct seq_file * m)153 static int msm_mm_show(struct drm_device *dev, struct seq_file *m)
154 {
155 struct drm_printer p = drm_seq_file_printer(m);
156
157 drm_mm_print(&dev->vma_offset_manager->vm_addr_space_mm, &p);
158
159 return 0;
160 }
161
msm_fb_show(struct drm_device * dev,struct seq_file * m)162 static int msm_fb_show(struct drm_device *dev, struct seq_file *m)
163 {
164 struct msm_drm_private *priv = dev->dev_private;
165 struct drm_framebuffer *fb, *fbdev_fb = NULL;
166
167 if (priv->fbdev) {
168 seq_printf(m, "fbcon ");
169 fbdev_fb = priv->fbdev->fb;
170 msm_framebuffer_describe(fbdev_fb, m);
171 }
172
173 mutex_lock(&dev->mode_config.fb_lock);
174 list_for_each_entry(fb, &dev->mode_config.fb_list, head) {
175 if (fb == fbdev_fb)
176 continue;
177
178 seq_printf(m, "user ");
179 msm_framebuffer_describe(fb, m);
180 }
181 mutex_unlock(&dev->mode_config.fb_lock);
182
183 return 0;
184 }
185
show_locked(struct seq_file * m,void * arg)186 static int show_locked(struct seq_file *m, void *arg)
187 {
188 struct drm_info_node *node = (struct drm_info_node *) m->private;
189 struct drm_device *dev = node->minor->dev;
190 int (*show)(struct drm_device *dev, struct seq_file *m) =
191 node->info_ent->data;
192 int ret;
193
194 ret = mutex_lock_interruptible(&dev->struct_mutex);
195 if (ret)
196 return ret;
197
198 ret = show(dev, m);
199
200 mutex_unlock(&dev->struct_mutex);
201
202 return ret;
203 }
204
205 static struct drm_info_list msm_debugfs_list[] = {
206 {"gem", show_locked, 0, msm_gem_show},
207 { "mm", show_locked, 0, msm_mm_show },
208 { "fb", show_locked, 0, msm_fb_show },
209 };
210
late_init_minor(struct drm_minor * minor)211 static int late_init_minor(struct drm_minor *minor)
212 {
213 int ret;
214
215 if (!minor)
216 return 0;
217
218 ret = msm_rd_debugfs_init(minor);
219 if (ret) {
220 DRM_DEV_ERROR(minor->dev->dev, "could not install rd debugfs\n");
221 return ret;
222 }
223
224 ret = msm_perf_debugfs_init(minor);
225 if (ret) {
226 DRM_DEV_ERROR(minor->dev->dev, "could not install perf debugfs\n");
227 return ret;
228 }
229
230 return 0;
231 }
232
msm_debugfs_late_init(struct drm_device * dev)233 int msm_debugfs_late_init(struct drm_device *dev)
234 {
235 int ret;
236 ret = late_init_minor(dev->primary);
237 if (ret)
238 return ret;
239 ret = late_init_minor(dev->render);
240 return ret;
241 }
242
msm_debugfs_init(struct drm_minor * minor)243 void msm_debugfs_init(struct drm_minor *minor)
244 {
245 struct drm_device *dev = minor->dev;
246 struct msm_drm_private *priv = dev->dev_private;
247
248 drm_debugfs_create_files(msm_debugfs_list,
249 ARRAY_SIZE(msm_debugfs_list),
250 minor->debugfs_root, minor);
251
252 debugfs_create_file("gpu", S_IRUSR, minor->debugfs_root,
253 dev, &msm_gpu_fops);
254
255 debugfs_create_u32("hangcheck_period_ms", 0600, minor->debugfs_root,
256 &priv->hangcheck_period);
257
258 debugfs_create_file("shrink", S_IRWXU, minor->debugfs_root,
259 dev, &shrink_fops);
260
261 if (priv->kms && priv->kms->funcs->debugfs_init)
262 priv->kms->funcs->debugfs_init(priv->kms, minor);
263 }
264 #endif
265
266