dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / gpu / drm / drm_debugfs.c
blobf8468eae05033f7c90242e2203eaacc306fe37ff
1 /*
2 * Created: Sun Dec 21 13:08:50 2008 by bgamari@gmail.com
4 * Copyright 2008 Ben Gamari <bgamari@gmail.com>
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:
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.
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.
26 #include <linux/debugfs.h>
27 #include <linux/seq_file.h>
28 #include <linux/slab.h>
29 #include <linux/export.h>
31 #include <drm/drm_client.h>
32 #include <drm/drm_debugfs.h>
33 #include <drm/drm_edid.h>
34 #include <drm/drm_atomic.h>
35 #include <drm/drm_auth.h>
36 #include <drm/drm_gem.h>
37 #include <drm/drmP.h>
39 #include "drm_internal.h"
40 #include "drm_crtc_internal.h"
42 #if defined(CONFIG_DEBUG_FS)
44 /***************************************************
45 * Initialization, etc.
46 **************************************************/
48 static int drm_name_info(struct seq_file *m, void *data)
50 struct drm_info_node *node = (struct drm_info_node *) m->private;
51 struct drm_minor *minor = node->minor;
52 struct drm_device *dev = minor->dev;
53 struct drm_master *master;
55 mutex_lock(&dev->master_mutex);
56 master = dev->master;
57 seq_printf(m, "%s", dev->driver->name);
58 if (dev->dev)
59 seq_printf(m, " dev=%s", dev_name(dev->dev));
60 if (master && master->unique)
61 seq_printf(m, " master=%s", master->unique);
62 if (dev->unique)
63 seq_printf(m, " unique=%s", dev->unique);
64 seq_printf(m, "\n");
65 mutex_unlock(&dev->master_mutex);
67 return 0;
70 static int drm_clients_info(struct seq_file *m, void *data)
72 struct drm_info_node *node = (struct drm_info_node *) m->private;
73 struct drm_device *dev = node->minor->dev;
74 struct drm_file *priv;
75 kuid_t uid;
77 seq_printf(m,
78 "%20s %5s %3s master a %5s %10s\n",
79 "command",
80 "pid",
81 "dev",
82 "uid",
83 "magic");
85 /* dev->filelist is sorted youngest first, but we want to present
86 * oldest first (i.e. kernel, servers, clients), so walk backwardss.
88 mutex_lock(&dev->filelist_mutex);
89 list_for_each_entry_reverse(priv, &dev->filelist, lhead) {
90 struct task_struct *task;
92 rcu_read_lock(); /* locks pid_task()->comm */
93 task = pid_task(priv->pid, PIDTYPE_PID);
94 uid = task ? __task_cred(task)->euid : GLOBAL_ROOT_UID;
95 seq_printf(m, "%20s %5d %3d %c %c %5d %10u\n",
96 task ? task->comm : "<unknown>",
97 pid_vnr(priv->pid),
98 priv->minor->index,
99 drm_is_current_master(priv) ? 'y' : 'n',
100 priv->authenticated ? 'y' : 'n',
101 from_kuid_munged(seq_user_ns(m), uid),
102 priv->magic);
103 rcu_read_unlock();
105 mutex_unlock(&dev->filelist_mutex);
106 return 0;
109 static int drm_gem_one_name_info(int id, void *ptr, void *data)
111 struct drm_gem_object *obj = ptr;
112 struct seq_file *m = data;
114 seq_printf(m, "%6d %8zd %7d %8d\n",
115 obj->name, obj->size,
116 obj->handle_count,
117 kref_read(&obj->refcount));
118 return 0;
121 static int drm_gem_name_info(struct seq_file *m, void *data)
123 struct drm_info_node *node = (struct drm_info_node *) m->private;
124 struct drm_device *dev = node->minor->dev;
126 seq_printf(m, " name size handles refcount\n");
128 mutex_lock(&dev->object_name_lock);
129 idr_for_each(&dev->object_name_idr, drm_gem_one_name_info, m);
130 mutex_unlock(&dev->object_name_lock);
132 return 0;
135 static const struct drm_info_list drm_debugfs_list[] = {
136 {"name", drm_name_info, 0},
137 {"clients", drm_clients_info, 0},
138 {"gem_names", drm_gem_name_info, DRIVER_GEM},
140 #define DRM_DEBUGFS_ENTRIES ARRAY_SIZE(drm_debugfs_list)
143 static int drm_debugfs_open(struct inode *inode, struct file *file)
145 struct drm_info_node *node = inode->i_private;
147 return single_open(file, node->info_ent->show, node);
151 static const struct file_operations drm_debugfs_fops = {
152 .owner = THIS_MODULE,
153 .open = drm_debugfs_open,
154 .read = seq_read,
155 .llseek = seq_lseek,
156 .release = single_release,
161 * drm_debugfs_create_files - Initialize a given set of debugfs files for DRM
162 * minor
163 * @files: The array of files to create
164 * @count: The number of files given
165 * @root: DRI debugfs dir entry.
166 * @minor: device minor number
168 * Create a given set of debugfs files represented by an array of
169 * &struct drm_info_list in the given root directory. These files will be removed
170 * automatically on drm_debugfs_cleanup().
172 int drm_debugfs_create_files(const struct drm_info_list *files, int count,
173 struct dentry *root, struct drm_minor *minor)
175 struct drm_device *dev = minor->dev;
176 struct dentry *ent;
177 struct drm_info_node *tmp;
178 int i, ret;
180 for (i = 0; i < count; i++) {
181 u32 features = files[i].driver_features;
183 if (features != 0 &&
184 (dev->driver->driver_features & features) != features)
185 continue;
187 tmp = kmalloc(sizeof(struct drm_info_node), GFP_KERNEL);
188 if (tmp == NULL) {
189 ret = -1;
190 goto fail;
192 ent = debugfs_create_file(files[i].name, S_IFREG | S_IRUGO,
193 root, tmp, &drm_debugfs_fops);
194 if (!ent) {
195 DRM_ERROR("Cannot create /sys/kernel/debug/dri/%pd/%s\n",
196 root, files[i].name);
197 kfree(tmp);
198 ret = -1;
199 goto fail;
202 tmp->minor = minor;
203 tmp->dent = ent;
204 tmp->info_ent = &files[i];
206 mutex_lock(&minor->debugfs_lock);
207 list_add(&tmp->list, &minor->debugfs_list);
208 mutex_unlock(&minor->debugfs_lock);
210 return 0;
212 fail:
213 drm_debugfs_remove_files(files, count, minor);
214 return ret;
216 EXPORT_SYMBOL(drm_debugfs_create_files);
218 int drm_debugfs_init(struct drm_minor *minor, int minor_id,
219 struct dentry *root)
221 struct drm_device *dev = minor->dev;
222 char name[64];
223 int ret;
225 INIT_LIST_HEAD(&minor->debugfs_list);
226 mutex_init(&minor->debugfs_lock);
227 sprintf(name, "%d", minor_id);
228 minor->debugfs_root = debugfs_create_dir(name, root);
229 if (!minor->debugfs_root) {
230 DRM_ERROR("Cannot create /sys/kernel/debug/dri/%s\n", name);
231 return -1;
234 ret = drm_debugfs_create_files(drm_debugfs_list, DRM_DEBUGFS_ENTRIES,
235 minor->debugfs_root, minor);
236 if (ret) {
237 debugfs_remove(minor->debugfs_root);
238 minor->debugfs_root = NULL;
239 DRM_ERROR("Failed to create core drm debugfs files\n");
240 return ret;
243 if (drm_drv_uses_atomic_modeset(dev)) {
244 ret = drm_atomic_debugfs_init(minor);
245 if (ret) {
246 DRM_ERROR("Failed to create atomic debugfs files\n");
247 return ret;
251 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
252 ret = drm_framebuffer_debugfs_init(minor);
253 if (ret) {
254 DRM_ERROR("Failed to create framebuffer debugfs file\n");
255 return ret;
258 ret = drm_client_debugfs_init(minor);
259 if (ret) {
260 DRM_ERROR("Failed to create client debugfs file\n");
261 return ret;
265 if (dev->driver->debugfs_init) {
266 ret = dev->driver->debugfs_init(minor);
267 if (ret) {
268 DRM_ERROR("DRM: Driver failed to initialize "
269 "/sys/kernel/debug/dri.\n");
270 return ret;
273 return 0;
277 int drm_debugfs_remove_files(const struct drm_info_list *files, int count,
278 struct drm_minor *minor)
280 struct list_head *pos, *q;
281 struct drm_info_node *tmp;
282 int i;
284 mutex_lock(&minor->debugfs_lock);
285 for (i = 0; i < count; i++) {
286 list_for_each_safe(pos, q, &minor->debugfs_list) {
287 tmp = list_entry(pos, struct drm_info_node, list);
288 if (tmp->info_ent == &files[i]) {
289 debugfs_remove(tmp->dent);
290 list_del(pos);
291 kfree(tmp);
295 mutex_unlock(&minor->debugfs_lock);
296 return 0;
298 EXPORT_SYMBOL(drm_debugfs_remove_files);
300 static void drm_debugfs_remove_all_files(struct drm_minor *minor)
302 struct drm_info_node *node, *tmp;
304 mutex_lock(&minor->debugfs_lock);
305 list_for_each_entry_safe(node, tmp, &minor->debugfs_list, list) {
306 debugfs_remove(node->dent);
307 list_del(&node->list);
308 kfree(node);
310 mutex_unlock(&minor->debugfs_lock);
313 int drm_debugfs_cleanup(struct drm_minor *minor)
315 if (!minor->debugfs_root)
316 return 0;
318 drm_debugfs_remove_all_files(minor);
320 debugfs_remove_recursive(minor->debugfs_root);
321 minor->debugfs_root = NULL;
323 return 0;
326 static int connector_show(struct seq_file *m, void *data)
328 struct drm_connector *connector = m->private;
330 seq_printf(m, "%s\n", drm_get_connector_force_name(connector->force));
332 return 0;
335 static int connector_open(struct inode *inode, struct file *file)
337 struct drm_connector *dev = inode->i_private;
339 return single_open(file, connector_show, dev);
342 static ssize_t connector_write(struct file *file, const char __user *ubuf,
343 size_t len, loff_t *offp)
345 struct seq_file *m = file->private_data;
346 struct drm_connector *connector = m->private;
347 char buf[12];
349 if (len > sizeof(buf) - 1)
350 return -EINVAL;
352 if (copy_from_user(buf, ubuf, len))
353 return -EFAULT;
355 buf[len] = '\0';
357 if (!strcmp(buf, "on"))
358 connector->force = DRM_FORCE_ON;
359 else if (!strcmp(buf, "digital"))
360 connector->force = DRM_FORCE_ON_DIGITAL;
361 else if (!strcmp(buf, "off"))
362 connector->force = DRM_FORCE_OFF;
363 else if (!strcmp(buf, "unspecified"))
364 connector->force = DRM_FORCE_UNSPECIFIED;
365 else
366 return -EINVAL;
368 return len;
371 static int edid_show(struct seq_file *m, void *data)
373 struct drm_connector *connector = m->private;
374 struct drm_property_blob *edid = connector->edid_blob_ptr;
376 if (connector->override_edid && edid)
377 seq_write(m, edid->data, edid->length);
379 return 0;
382 static int edid_open(struct inode *inode, struct file *file)
384 struct drm_connector *dev = inode->i_private;
386 return single_open(file, edid_show, dev);
389 static ssize_t edid_write(struct file *file, const char __user *ubuf,
390 size_t len, loff_t *offp)
392 struct seq_file *m = file->private_data;
393 struct drm_connector *connector = m->private;
394 char *buf;
395 struct edid *edid;
396 int ret;
398 buf = memdup_user(ubuf, len);
399 if (IS_ERR(buf))
400 return PTR_ERR(buf);
402 edid = (struct edid *) buf;
404 if (len == 5 && !strncmp(buf, "reset", 5)) {
405 connector->override_edid = false;
406 ret = drm_connector_update_edid_property(connector, NULL);
407 } else if (len < EDID_LENGTH ||
408 EDID_LENGTH * (1 + edid->extensions) > len)
409 ret = -EINVAL;
410 else {
411 connector->override_edid = false;
412 ret = drm_connector_update_edid_property(connector, edid);
413 if (!ret)
414 connector->override_edid = true;
417 kfree(buf);
419 return (ret) ? ret : len;
422 static const struct file_operations drm_edid_fops = {
423 .owner = THIS_MODULE,
424 .open = edid_open,
425 .read = seq_read,
426 .llseek = seq_lseek,
427 .release = single_release,
428 .write = edid_write
432 static const struct file_operations drm_connector_fops = {
433 .owner = THIS_MODULE,
434 .open = connector_open,
435 .read = seq_read,
436 .llseek = seq_lseek,
437 .release = single_release,
438 .write = connector_write
441 int drm_debugfs_connector_add(struct drm_connector *connector)
443 struct drm_minor *minor = connector->dev->primary;
444 struct dentry *root, *ent;
446 if (!minor->debugfs_root)
447 return -1;
449 root = debugfs_create_dir(connector->name, minor->debugfs_root);
450 if (!root)
451 return -ENOMEM;
453 connector->debugfs_entry = root;
455 /* force */
456 ent = debugfs_create_file("force", S_IRUGO | S_IWUSR, root, connector,
457 &drm_connector_fops);
458 if (!ent)
459 goto error;
461 /* edid */
462 ent = debugfs_create_file("edid_override", S_IRUGO | S_IWUSR, root,
463 connector, &drm_edid_fops);
464 if (!ent)
465 goto error;
467 return 0;
469 error:
470 debugfs_remove_recursive(connector->debugfs_entry);
471 connector->debugfs_entry = NULL;
472 return -ENOMEM;
475 void drm_debugfs_connector_remove(struct drm_connector *connector)
477 if (!connector->debugfs_entry)
478 return;
480 debugfs_remove_recursive(connector->debugfs_entry);
482 connector->debugfs_entry = NULL;
485 int drm_debugfs_crtc_add(struct drm_crtc *crtc)
487 struct drm_minor *minor = crtc->dev->primary;
488 struct dentry *root;
489 char *name;
491 name = kasprintf(GFP_KERNEL, "crtc-%d", crtc->index);
492 if (!name)
493 return -ENOMEM;
495 root = debugfs_create_dir(name, minor->debugfs_root);
496 kfree(name);
497 if (!root)
498 return -ENOMEM;
500 crtc->debugfs_entry = root;
502 if (drm_debugfs_crtc_crc_add(crtc))
503 goto error;
505 return 0;
507 error:
508 drm_debugfs_crtc_remove(crtc);
509 return -ENOMEM;
512 void drm_debugfs_crtc_remove(struct drm_crtc *crtc)
514 debugfs_remove_recursive(crtc->debugfs_entry);
515 crtc->debugfs_entry = NULL;
518 #endif /* CONFIG_DEBUG_FS */