drm_debugfs.c revision 1.4 1 1.4 riastrad /* $NetBSD: drm_debugfs.c,v 1.4 2018/08/27 04:58:19 riastradh Exp $ */
2 1.4 riastrad
3 1.1 riastrad /**
4 1.1 riastrad * \file drm_debugfs.c
5 1.1 riastrad * debugfs support for DRM
6 1.1 riastrad *
7 1.1 riastrad * \author Ben Gamari <bgamari (at) gmail.com>
8 1.1 riastrad */
9 1.1 riastrad
10 1.1 riastrad /*
11 1.1 riastrad * Created: Sun Dec 21 13:08:50 2008 by bgamari (at) gmail.com
12 1.1 riastrad *
13 1.1 riastrad * Copyright 2008 Ben Gamari <bgamari (at) gmail.com>
14 1.1 riastrad *
15 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a
16 1.1 riastrad * copy of this software and associated documentation files (the "Software"),
17 1.1 riastrad * to deal in the Software without restriction, including without limitation
18 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense,
19 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the
20 1.1 riastrad * Software is furnished to do so, subject to the following conditions:
21 1.1 riastrad *
22 1.1 riastrad * The above copyright notice and this permission notice (including the next
23 1.1 riastrad * paragraph) shall be included in all copies or substantial portions of the
24 1.1 riastrad * Software.
25 1.1 riastrad *
26 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
29 1.1 riastrad * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
30 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
31 1.1 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
32 1.1 riastrad * OTHER DEALINGS IN THE SOFTWARE.
33 1.1 riastrad */
34 1.1 riastrad
35 1.4 riastrad #include <sys/cdefs.h>
36 1.4 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_debugfs.c,v 1.4 2018/08/27 04:58:19 riastradh Exp $");
37 1.4 riastrad
38 1.1 riastrad #include <linux/debugfs.h>
39 1.1 riastrad #include <linux/seq_file.h>
40 1.1 riastrad #include <linux/slab.h>
41 1.1 riastrad #include <linux/export.h>
42 1.1 riastrad #include <drm/drmP.h>
43 1.4 riastrad #include <drm/drm_edid.h>
44 1.4 riastrad #include "drm_internal.h"
45 1.1 riastrad
46 1.1 riastrad #if defined(CONFIG_DEBUG_FS)
47 1.1 riastrad
48 1.1 riastrad /***************************************************
49 1.1 riastrad * Initialization, etc.
50 1.1 riastrad **************************************************/
51 1.1 riastrad
52 1.3 riastrad static const struct drm_info_list drm_debugfs_list[] = {
53 1.1 riastrad {"name", drm_name_info, 0},
54 1.1 riastrad {"vm", drm_vm_info, 0},
55 1.1 riastrad {"clients", drm_clients_info, 0},
56 1.1 riastrad {"bufs", drm_bufs_info, 0},
57 1.1 riastrad {"gem_names", drm_gem_name_info, DRIVER_GEM},
58 1.1 riastrad {"vma", drm_vma_info, 0},
59 1.1 riastrad };
60 1.1 riastrad #define DRM_DEBUGFS_ENTRIES ARRAY_SIZE(drm_debugfs_list)
61 1.1 riastrad
62 1.1 riastrad
63 1.1 riastrad static int drm_debugfs_open(struct inode *inode, struct file *file)
64 1.1 riastrad {
65 1.1 riastrad struct drm_info_node *node = inode->i_private;
66 1.1 riastrad
67 1.1 riastrad return single_open(file, node->info_ent->show, node);
68 1.1 riastrad }
69 1.1 riastrad
70 1.1 riastrad
71 1.1 riastrad static const struct file_operations drm_debugfs_fops = {
72 1.1 riastrad .owner = THIS_MODULE,
73 1.1 riastrad .open = drm_debugfs_open,
74 1.1 riastrad .read = seq_read,
75 1.1 riastrad .llseek = seq_lseek,
76 1.1 riastrad .release = single_release,
77 1.1 riastrad };
78 1.1 riastrad
79 1.1 riastrad
80 1.1 riastrad /**
81 1.1 riastrad * Initialize a given set of debugfs files for a device
82 1.1 riastrad *
83 1.1 riastrad * \param files The array of files to create
84 1.1 riastrad * \param count The number of files given
85 1.1 riastrad * \param root DRI debugfs dir entry.
86 1.1 riastrad * \param minor device minor number
87 1.1 riastrad * \return Zero on success, non-zero on failure
88 1.1 riastrad *
89 1.1 riastrad * Create a given set of debugfs files represented by an array of
90 1.1 riastrad * gdm_debugfs_lists in the given root directory.
91 1.1 riastrad */
92 1.3 riastrad int drm_debugfs_create_files(const struct drm_info_list *files, int count,
93 1.1 riastrad struct dentry *root, struct drm_minor *minor)
94 1.1 riastrad {
95 1.1 riastrad struct drm_device *dev = minor->dev;
96 1.1 riastrad struct dentry *ent;
97 1.1 riastrad struct drm_info_node *tmp;
98 1.1 riastrad int i, ret;
99 1.1 riastrad
100 1.1 riastrad for (i = 0; i < count; i++) {
101 1.1 riastrad u32 features = files[i].driver_features;
102 1.1 riastrad
103 1.1 riastrad if (features != 0 &&
104 1.1 riastrad (dev->driver->driver_features & features) != features)
105 1.1 riastrad continue;
106 1.1 riastrad
107 1.1 riastrad tmp = kmalloc(sizeof(struct drm_info_node), GFP_KERNEL);
108 1.1 riastrad if (tmp == NULL) {
109 1.1 riastrad ret = -1;
110 1.1 riastrad goto fail;
111 1.1 riastrad }
112 1.1 riastrad ent = debugfs_create_file(files[i].name, S_IFREG | S_IRUGO,
113 1.1 riastrad root, tmp, &drm_debugfs_fops);
114 1.1 riastrad if (!ent) {
115 1.1 riastrad DRM_ERROR("Cannot create /sys/kernel/debug/dri/%s/%s\n",
116 1.1 riastrad root->d_name.name, files[i].name);
117 1.1 riastrad kfree(tmp);
118 1.1 riastrad ret = -1;
119 1.1 riastrad goto fail;
120 1.1 riastrad }
121 1.1 riastrad
122 1.1 riastrad tmp->minor = minor;
123 1.1 riastrad tmp->dent = ent;
124 1.1 riastrad tmp->info_ent = &files[i];
125 1.1 riastrad
126 1.1 riastrad mutex_lock(&minor->debugfs_lock);
127 1.1 riastrad list_add(&tmp->list, &minor->debugfs_list);
128 1.1 riastrad mutex_unlock(&minor->debugfs_lock);
129 1.1 riastrad }
130 1.1 riastrad return 0;
131 1.1 riastrad
132 1.1 riastrad fail:
133 1.1 riastrad drm_debugfs_remove_files(files, count, minor);
134 1.1 riastrad return ret;
135 1.1 riastrad }
136 1.1 riastrad EXPORT_SYMBOL(drm_debugfs_create_files);
137 1.1 riastrad
138 1.1 riastrad /**
139 1.1 riastrad * Initialize the DRI debugfs filesystem for a device
140 1.1 riastrad *
141 1.1 riastrad * \param dev DRM device
142 1.1 riastrad * \param minor device minor number
143 1.1 riastrad * \param root DRI debugfs dir entry.
144 1.1 riastrad *
145 1.1 riastrad * Create the DRI debugfs root entry "/sys/kernel/debug/dri", the device debugfs root entry
146 1.1 riastrad * "/sys/kernel/debug/dri/%minor%/", and each entry in debugfs_list as
147 1.1 riastrad * "/sys/kernel/debug/dri/%minor%/%name%".
148 1.1 riastrad */
149 1.1 riastrad int drm_debugfs_init(struct drm_minor *minor, int minor_id,
150 1.1 riastrad struct dentry *root)
151 1.1 riastrad {
152 1.1 riastrad struct drm_device *dev = minor->dev;
153 1.1 riastrad char name[64];
154 1.1 riastrad int ret;
155 1.1 riastrad
156 1.1 riastrad INIT_LIST_HEAD(&minor->debugfs_list);
157 1.1 riastrad mutex_init(&minor->debugfs_lock);
158 1.2 christos snprintf(name, sizeof(name), "%d", minor_id);
159 1.1 riastrad minor->debugfs_root = debugfs_create_dir(name, root);
160 1.1 riastrad if (!minor->debugfs_root) {
161 1.1 riastrad DRM_ERROR("Cannot create /sys/kernel/debug/dri/%s\n", name);
162 1.1 riastrad return -1;
163 1.1 riastrad }
164 1.1 riastrad
165 1.1 riastrad ret = drm_debugfs_create_files(drm_debugfs_list, DRM_DEBUGFS_ENTRIES,
166 1.1 riastrad minor->debugfs_root, minor);
167 1.1 riastrad if (ret) {
168 1.1 riastrad debugfs_remove(minor->debugfs_root);
169 1.1 riastrad minor->debugfs_root = NULL;
170 1.1 riastrad DRM_ERROR("Failed to create core drm debugfs files\n");
171 1.1 riastrad return ret;
172 1.1 riastrad }
173 1.1 riastrad
174 1.1 riastrad if (dev->driver->debugfs_init) {
175 1.1 riastrad ret = dev->driver->debugfs_init(minor);
176 1.1 riastrad if (ret) {
177 1.1 riastrad DRM_ERROR("DRM: Driver failed to initialize "
178 1.1 riastrad "/sys/kernel/debug/dri.\n");
179 1.1 riastrad return ret;
180 1.1 riastrad }
181 1.1 riastrad }
182 1.1 riastrad return 0;
183 1.1 riastrad }
184 1.1 riastrad
185 1.1 riastrad
186 1.1 riastrad /**
187 1.1 riastrad * Remove a list of debugfs files
188 1.1 riastrad *
189 1.1 riastrad * \param files The list of files
190 1.1 riastrad * \param count The number of files
191 1.1 riastrad * \param minor The minor of which we should remove the files
192 1.1 riastrad * \return always zero.
193 1.1 riastrad *
194 1.1 riastrad * Remove all debugfs entries created by debugfs_init().
195 1.1 riastrad */
196 1.3 riastrad int drm_debugfs_remove_files(const struct drm_info_list *files, int count,
197 1.1 riastrad struct drm_minor *minor)
198 1.1 riastrad {
199 1.1 riastrad struct list_head *pos, *q;
200 1.1 riastrad struct drm_info_node *tmp;
201 1.1 riastrad int i;
202 1.1 riastrad
203 1.1 riastrad mutex_lock(&minor->debugfs_lock);
204 1.1 riastrad for (i = 0; i < count; i++) {
205 1.1 riastrad list_for_each_safe(pos, q, &minor->debugfs_list) {
206 1.1 riastrad tmp = list_entry(pos, struct drm_info_node, list);
207 1.1 riastrad if (tmp->info_ent == &files[i]) {
208 1.1 riastrad debugfs_remove(tmp->dent);
209 1.1 riastrad list_del(pos);
210 1.1 riastrad kfree(tmp);
211 1.1 riastrad }
212 1.1 riastrad }
213 1.1 riastrad }
214 1.1 riastrad mutex_unlock(&minor->debugfs_lock);
215 1.1 riastrad return 0;
216 1.1 riastrad }
217 1.1 riastrad EXPORT_SYMBOL(drm_debugfs_remove_files);
218 1.1 riastrad
219 1.1 riastrad /**
220 1.1 riastrad * Cleanup the debugfs filesystem resources.
221 1.1 riastrad *
222 1.1 riastrad * \param minor device minor number.
223 1.1 riastrad * \return always zero.
224 1.1 riastrad *
225 1.1 riastrad * Remove all debugfs entries created by debugfs_init().
226 1.1 riastrad */
227 1.1 riastrad int drm_debugfs_cleanup(struct drm_minor *minor)
228 1.1 riastrad {
229 1.1 riastrad struct drm_device *dev = minor->dev;
230 1.1 riastrad
231 1.1 riastrad if (!minor->debugfs_root)
232 1.1 riastrad return 0;
233 1.1 riastrad
234 1.1 riastrad if (dev->driver->debugfs_cleanup)
235 1.1 riastrad dev->driver->debugfs_cleanup(minor);
236 1.1 riastrad
237 1.1 riastrad drm_debugfs_remove_files(drm_debugfs_list, DRM_DEBUGFS_ENTRIES, minor);
238 1.1 riastrad
239 1.1 riastrad debugfs_remove(minor->debugfs_root);
240 1.1 riastrad minor->debugfs_root = NULL;
241 1.1 riastrad
242 1.1 riastrad return 0;
243 1.1 riastrad }
244 1.1 riastrad
245 1.4 riastrad static int connector_show(struct seq_file *m, void *data)
246 1.4 riastrad {
247 1.4 riastrad struct drm_connector *connector = m->private;
248 1.4 riastrad const char *status;
249 1.4 riastrad
250 1.4 riastrad switch (connector->force) {
251 1.4 riastrad case DRM_FORCE_ON:
252 1.4 riastrad status = "on\n";
253 1.4 riastrad break;
254 1.4 riastrad
255 1.4 riastrad case DRM_FORCE_ON_DIGITAL:
256 1.4 riastrad status = "digital\n";
257 1.4 riastrad break;
258 1.4 riastrad
259 1.4 riastrad case DRM_FORCE_OFF:
260 1.4 riastrad status = "off\n";
261 1.4 riastrad break;
262 1.4 riastrad
263 1.4 riastrad case DRM_FORCE_UNSPECIFIED:
264 1.4 riastrad status = "unspecified\n";
265 1.4 riastrad break;
266 1.4 riastrad
267 1.4 riastrad default:
268 1.4 riastrad return 0;
269 1.4 riastrad }
270 1.4 riastrad
271 1.4 riastrad seq_puts(m, status);
272 1.4 riastrad
273 1.4 riastrad return 0;
274 1.4 riastrad }
275 1.4 riastrad
276 1.4 riastrad static int connector_open(struct inode *inode, struct file *file)
277 1.4 riastrad {
278 1.4 riastrad struct drm_connector *dev = inode->i_private;
279 1.4 riastrad
280 1.4 riastrad return single_open(file, connector_show, dev);
281 1.4 riastrad }
282 1.4 riastrad
283 1.4 riastrad static ssize_t connector_write(struct file *file, const char __user *ubuf,
284 1.4 riastrad size_t len, loff_t *offp)
285 1.4 riastrad {
286 1.4 riastrad struct seq_file *m = file->private_data;
287 1.4 riastrad struct drm_connector *connector = m->private;
288 1.4 riastrad char buf[12];
289 1.4 riastrad
290 1.4 riastrad if (len > sizeof(buf) - 1)
291 1.4 riastrad return -EINVAL;
292 1.4 riastrad
293 1.4 riastrad if (copy_from_user(buf, ubuf, len))
294 1.4 riastrad return -EFAULT;
295 1.4 riastrad
296 1.4 riastrad buf[len] = '\0';
297 1.4 riastrad
298 1.4 riastrad if (!strcmp(buf, "on"))
299 1.4 riastrad connector->force = DRM_FORCE_ON;
300 1.4 riastrad else if (!strcmp(buf, "digital"))
301 1.4 riastrad connector->force = DRM_FORCE_ON_DIGITAL;
302 1.4 riastrad else if (!strcmp(buf, "off"))
303 1.4 riastrad connector->force = DRM_FORCE_OFF;
304 1.4 riastrad else if (!strcmp(buf, "unspecified"))
305 1.4 riastrad connector->force = DRM_FORCE_UNSPECIFIED;
306 1.4 riastrad else
307 1.4 riastrad return -EINVAL;
308 1.4 riastrad
309 1.4 riastrad return len;
310 1.4 riastrad }
311 1.4 riastrad
312 1.4 riastrad static int edid_show(struct seq_file *m, void *data)
313 1.4 riastrad {
314 1.4 riastrad struct drm_connector *connector = m->private;
315 1.4 riastrad struct drm_property_blob *edid = connector->edid_blob_ptr;
316 1.4 riastrad
317 1.4 riastrad if (connector->override_edid && edid)
318 1.4 riastrad seq_write(m, edid->data, edid->length);
319 1.4 riastrad
320 1.4 riastrad return 0;
321 1.4 riastrad }
322 1.4 riastrad
323 1.4 riastrad static int edid_open(struct inode *inode, struct file *file)
324 1.4 riastrad {
325 1.4 riastrad struct drm_connector *dev = inode->i_private;
326 1.4 riastrad
327 1.4 riastrad return single_open(file, edid_show, dev);
328 1.4 riastrad }
329 1.4 riastrad
330 1.4 riastrad static ssize_t edid_write(struct file *file, const char __user *ubuf,
331 1.4 riastrad size_t len, loff_t *offp)
332 1.4 riastrad {
333 1.4 riastrad struct seq_file *m = file->private_data;
334 1.4 riastrad struct drm_connector *connector = m->private;
335 1.4 riastrad char *buf;
336 1.4 riastrad struct edid *edid;
337 1.4 riastrad int ret;
338 1.4 riastrad
339 1.4 riastrad buf = memdup_user(ubuf, len);
340 1.4 riastrad if (IS_ERR(buf))
341 1.4 riastrad return PTR_ERR(buf);
342 1.4 riastrad
343 1.4 riastrad edid = (struct edid *) buf;
344 1.4 riastrad
345 1.4 riastrad if (len == 5 && !strncmp(buf, "reset", 5)) {
346 1.4 riastrad connector->override_edid = false;
347 1.4 riastrad ret = drm_mode_connector_update_edid_property(connector, NULL);
348 1.4 riastrad } else if (len < EDID_LENGTH ||
349 1.4 riastrad EDID_LENGTH * (1 + edid->extensions) > len)
350 1.4 riastrad ret = -EINVAL;
351 1.4 riastrad else {
352 1.4 riastrad connector->override_edid = false;
353 1.4 riastrad ret = drm_mode_connector_update_edid_property(connector, edid);
354 1.4 riastrad if (!ret)
355 1.4 riastrad connector->override_edid = true;
356 1.4 riastrad }
357 1.4 riastrad
358 1.4 riastrad kfree(buf);
359 1.4 riastrad
360 1.4 riastrad return (ret) ? ret : len;
361 1.4 riastrad }
362 1.4 riastrad
363 1.4 riastrad static const struct file_operations drm_edid_fops = {
364 1.4 riastrad .owner = THIS_MODULE,
365 1.4 riastrad .open = edid_open,
366 1.4 riastrad .read = seq_read,
367 1.4 riastrad .llseek = seq_lseek,
368 1.4 riastrad .release = single_release,
369 1.4 riastrad .write = edid_write
370 1.4 riastrad };
371 1.4 riastrad
372 1.4 riastrad
373 1.4 riastrad static const struct file_operations drm_connector_fops = {
374 1.4 riastrad .owner = THIS_MODULE,
375 1.4 riastrad .open = connector_open,
376 1.4 riastrad .read = seq_read,
377 1.4 riastrad .llseek = seq_lseek,
378 1.4 riastrad .release = single_release,
379 1.4 riastrad .write = connector_write
380 1.4 riastrad };
381 1.4 riastrad
382 1.4 riastrad int drm_debugfs_connector_add(struct drm_connector *connector)
383 1.4 riastrad {
384 1.4 riastrad struct drm_minor *minor = connector->dev->primary;
385 1.4 riastrad struct dentry *root, *ent;
386 1.4 riastrad
387 1.4 riastrad if (!minor->debugfs_root)
388 1.4 riastrad return -1;
389 1.4 riastrad
390 1.4 riastrad root = debugfs_create_dir(connector->name, minor->debugfs_root);
391 1.4 riastrad if (!root)
392 1.4 riastrad return -ENOMEM;
393 1.4 riastrad
394 1.4 riastrad connector->debugfs_entry = root;
395 1.4 riastrad
396 1.4 riastrad /* force */
397 1.4 riastrad ent = debugfs_create_file("force", S_IRUGO | S_IWUSR, root, connector,
398 1.4 riastrad &drm_connector_fops);
399 1.4 riastrad if (!ent)
400 1.4 riastrad goto error;
401 1.4 riastrad
402 1.4 riastrad /* edid */
403 1.4 riastrad ent = debugfs_create_file("edid_override", S_IRUGO | S_IWUSR, root,
404 1.4 riastrad connector, &drm_edid_fops);
405 1.4 riastrad if (!ent)
406 1.4 riastrad goto error;
407 1.4 riastrad
408 1.4 riastrad return 0;
409 1.4 riastrad
410 1.4 riastrad error:
411 1.4 riastrad debugfs_remove_recursive(connector->debugfs_entry);
412 1.4 riastrad connector->debugfs_entry = NULL;
413 1.4 riastrad return -ENOMEM;
414 1.4 riastrad }
415 1.4 riastrad
416 1.4 riastrad void drm_debugfs_connector_remove(struct drm_connector *connector)
417 1.4 riastrad {
418 1.4 riastrad if (!connector->debugfs_entry)
419 1.4 riastrad return;
420 1.4 riastrad
421 1.4 riastrad debugfs_remove_recursive(connector->debugfs_entry);
422 1.4 riastrad
423 1.4 riastrad connector->debugfs_entry = NULL;
424 1.4 riastrad }
425 1.4 riastrad
426 1.1 riastrad #endif /* CONFIG_DEBUG_FS */
427 1.1 riastrad
428