drm: add drm_device_get_by_name support
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / drm_drv.c
1 /*
2  * Created: Fri Jan 19 10:48:35 2001 by faith@acm.org
3  *
4  * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
5  * All Rights Reserved.
6  *
7  * Author Rickard E. (Rik) Faith <faith@valinux.com>
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the next
17  * paragraph) shall be included in all copies or substantial portions of the
18  * Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  * DEALINGS IN THE SOFTWARE.
27  */
28
29 #include <linux/debugfs.h>
30 #include <linux/fs.h>
31 #include <linux/module.h>
32 #include <linux/moduleparam.h>
33 #include <linux/mount.h>
34 #include <linux/slab.h>
35 #include <drm/drmP.h>
36 #include <drm/drm_core.h>
37 #include "drm_crtc_internal.h"
38 #include "drm_legacy.h"
39 #include "drm_internal.h"
40
41 /*
42  * drm_debug: Enable debug output.
43  * Bitmask of DRM_UT_x. See include/drm/drmP.h for details.
44  */
45 unsigned int drm_debug = 0;
46 EXPORT_SYMBOL(drm_debug);
47
48 MODULE_AUTHOR(CORE_AUTHOR);
49 MODULE_DESCRIPTION(CORE_DESC);
50 MODULE_LICENSE("GPL and additional rights");
51 MODULE_PARM_DESC(debug, "Enable debug output, where each bit enables a debug category.\n"
52 "\t\tBit 0 (0x01) will enable CORE messages (drm core code)\n"
53 "\t\tBit 1 (0x02) will enable DRIVER messages (drm controller code)\n"
54 "\t\tBit 2 (0x04) will enable KMS messages (modesetting code)\n"
55 "\t\tBit 3 (0x08) will enable PRIME messages (prime code)\n"
56 "\t\tBit 4 (0x10) will enable ATOMIC messages (atomic code)\n"
57 "\t\tBit 5 (0x20) will enable VBL messages (vblank code)");
58 module_param_named(debug, drm_debug, int, 0600);
59
60 static DEFINE_SPINLOCK(drm_minor_lock);
61 static struct idr drm_minors_idr;
62
63 static struct dentry *drm_debugfs_root;
64
65 #define DRM_PRINTK_FMT "[" DRM_NAME ":%s]%s %pV"
66
67 void drm_dev_printk(const struct device *dev, const char *level,
68                     unsigned int category, const char *function_name,
69                     const char *prefix, const char *format, ...)
70 {
71         struct va_format vaf;
72         va_list args;
73
74         if (category != DRM_UT_NONE && !(drm_debug & category))
75                 return;
76
77         va_start(args, format);
78         vaf.fmt = format;
79         vaf.va = &args;
80
81         dev_printk(level, dev, DRM_PRINTK_FMT, function_name, prefix,
82                    &vaf);
83
84         va_end(args);
85 }
86 EXPORT_SYMBOL(drm_dev_printk);
87
88 void drm_printk(const char *level, unsigned int category,
89                 const char *function_name, const char *prefix,
90                 const char *format, ...)
91 {
92         struct va_format vaf;
93         va_list args;
94
95         if (category != DRM_UT_NONE && !(drm_debug & category))
96                 return;
97
98         va_start(args, format);
99         vaf.fmt = format;
100         vaf.va = &args;
101
102         printk("%s" DRM_PRINTK_FMT, level, function_name, prefix, &vaf);
103
104         va_end(args);
105 }
106 EXPORT_SYMBOL(drm_printk);
107
108 struct drm_master *drm_master_create(struct drm_minor *minor)
109 {
110         struct drm_master *master;
111
112         master = kzalloc(sizeof(*master), GFP_KERNEL);
113         if (!master)
114                 return NULL;
115
116         kref_init(&master->refcount);
117         spin_lock_init(&master->lock.spinlock);
118         init_waitqueue_head(&master->lock.lock_queue);
119         idr_init(&master->magic_map);
120         master->minor = minor;
121
122         return master;
123 }
124
125 struct drm_master *drm_master_get(struct drm_master *master)
126 {
127         kref_get(&master->refcount);
128         return master;
129 }
130 EXPORT_SYMBOL(drm_master_get);
131
132 static void drm_master_destroy(struct kref *kref)
133 {
134         struct drm_master *master = container_of(kref, struct drm_master, refcount);
135         struct drm_device *dev = master->minor->dev;
136         struct drm_map_list *r_list, *list_temp;
137
138         mutex_lock(&dev->struct_mutex);
139         if (dev->driver->master_destroy)
140                 dev->driver->master_destroy(dev, master);
141
142         list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
143                 if (r_list->master == master) {
144                         drm_legacy_rmmap_locked(dev, r_list->map);
145                         r_list = NULL;
146                 }
147         }
148         mutex_unlock(&dev->struct_mutex);
149
150         idr_destroy(&master->magic_map);
151         kfree(master->unique);
152         kfree(master);
153 }
154
155 void drm_master_put(struct drm_master **master)
156 {
157         kref_put(&(*master)->refcount, drm_master_destroy);
158         *master = NULL;
159 }
160 EXPORT_SYMBOL(drm_master_put);
161
162 int drm_setmaster_ioctl(struct drm_device *dev, void *data,
163                         struct drm_file *file_priv)
164 {
165         int ret = 0;
166
167         mutex_lock(&dev->master_mutex);
168         if (file_priv->is_master)
169                 goto out_unlock;
170
171         if (file_priv->minor->master) {
172                 ret = -EINVAL;
173                 goto out_unlock;
174         }
175
176         if (!file_priv->master) {
177                 ret = -EINVAL;
178                 goto out_unlock;
179         }
180
181         if (!file_priv->allowed_master) {
182                 ret = drm_new_set_master(dev, file_priv);
183                 goto out_unlock;
184         }
185
186         file_priv->minor->master = drm_master_get(file_priv->master);
187         file_priv->is_master = 1;
188         if (dev->driver->master_set) {
189                 ret = dev->driver->master_set(dev, file_priv, false);
190                 if (unlikely(ret != 0)) {
191                         file_priv->is_master = 0;
192                         drm_master_put(&file_priv->minor->master);
193                 }
194         }
195
196 out_unlock:
197         mutex_unlock(&dev->master_mutex);
198         return ret;
199 }
200
201 int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
202                          struct drm_file *file_priv)
203 {
204         int ret = -EINVAL;
205
206         mutex_lock(&dev->master_mutex);
207         if (!file_priv->is_master)
208                 goto out_unlock;
209
210         if (!file_priv->minor->master)
211                 goto out_unlock;
212
213         ret = 0;
214         if (dev->driver->master_drop)
215                 dev->driver->master_drop(dev, file_priv, false);
216         drm_master_put(&file_priv->minor->master);
217         file_priv->is_master = 0;
218
219 out_unlock:
220         mutex_unlock(&dev->master_mutex);
221         return ret;
222 }
223
224 /*
225  * DRM Minors
226  * A DRM device can provide several char-dev interfaces on the DRM-Major. Each
227  * of them is represented by a drm_minor object. Depending on the capabilities
228  * of the device-driver, different interfaces are registered.
229  *
230  * Minors can be accessed via dev->$minor_name. This pointer is either
231  * NULL or a valid drm_minor pointer and stays valid as long as the device is
232  * valid. This means, DRM minors have the same life-time as the underlying
233  * device. However, this doesn't mean that the minor is active. Minors are
234  * registered and unregistered dynamically according to device-state.
235  */
236
237 static struct drm_minor **drm_minor_get_slot(struct drm_device *dev,
238                                              unsigned int type)
239 {
240         switch (type) {
241         case DRM_MINOR_LEGACY:
242                 return &dev->primary;
243         case DRM_MINOR_RENDER:
244                 return &dev->render;
245         case DRM_MINOR_CONTROL:
246                 return &dev->control;
247         default:
248                 return NULL;
249         }
250 }
251
252 static int drm_minor_alloc(struct drm_device *dev, unsigned int type)
253 {
254         struct drm_minor *minor;
255         unsigned long flags;
256         int r;
257
258         minor = kzalloc(sizeof(*minor), GFP_KERNEL);
259         if (!minor)
260                 return -ENOMEM;
261
262         minor->type = type;
263         minor->dev = dev;
264
265         idr_preload(GFP_KERNEL);
266         spin_lock_irqsave(&drm_minor_lock, flags);
267         r = idr_alloc(&drm_minors_idr,
268                       NULL,
269                       64 * type,
270                       64 * (type + 1),
271                       GFP_NOWAIT);
272         spin_unlock_irqrestore(&drm_minor_lock, flags);
273         idr_preload_end();
274
275         if (r < 0)
276                 goto err_free;
277
278         minor->index = r;
279
280         minor->kdev = drm_sysfs_minor_alloc(minor);
281         if (IS_ERR(minor->kdev)) {
282                 r = PTR_ERR(minor->kdev);
283                 goto err_index;
284         }
285
286         *drm_minor_get_slot(dev, type) = minor;
287         return 0;
288
289 err_index:
290         spin_lock_irqsave(&drm_minor_lock, flags);
291         idr_remove(&drm_minors_idr, minor->index);
292         spin_unlock_irqrestore(&drm_minor_lock, flags);
293 err_free:
294         kfree(minor);
295         return r;
296 }
297
298 static void drm_minor_free(struct drm_device *dev, unsigned int type)
299 {
300         struct drm_minor **slot, *minor;
301         unsigned long flags;
302
303         slot = drm_minor_get_slot(dev, type);
304         minor = *slot;
305         if (!minor)
306                 return;
307
308         put_device(minor->kdev);
309
310         spin_lock_irqsave(&drm_minor_lock, flags);
311         idr_remove(&drm_minors_idr, minor->index);
312         spin_unlock_irqrestore(&drm_minor_lock, flags);
313
314         kfree(minor);
315         *slot = NULL;
316 }
317
318 static int drm_minor_register(struct drm_device *dev, unsigned int type)
319 {
320         struct drm_minor *minor;
321         unsigned long flags;
322         int ret;
323
324         DRM_DEBUG("\n");
325
326         minor = *drm_minor_get_slot(dev, type);
327         if (!minor)
328                 return 0;
329
330         ret = drm_debugfs_init(minor, minor->index, drm_debugfs_root);
331         if (ret) {
332                 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
333                 return ret;
334         }
335
336         ret = device_add(minor->kdev);
337         if (ret)
338                 goto err_debugfs;
339
340         /* replace NULL with @minor so lookups will succeed from now on */
341         spin_lock_irqsave(&drm_minor_lock, flags);
342         idr_replace(&drm_minors_idr, minor, minor->index);
343         spin_unlock_irqrestore(&drm_minor_lock, flags);
344
345         DRM_DEBUG("new minor registered %d\n", minor->index);
346         return 0;
347
348 err_debugfs:
349         drm_debugfs_cleanup(minor);
350         return ret;
351 }
352
353 static void drm_minor_unregister(struct drm_device *dev, unsigned int type)
354 {
355         struct drm_minor *minor;
356         unsigned long flags;
357
358         minor = *drm_minor_get_slot(dev, type);
359         if (!minor || !device_is_registered(minor->kdev))
360                 return;
361
362         /* replace @minor with NULL so lookups will fail from now on */
363         spin_lock_irqsave(&drm_minor_lock, flags);
364         idr_replace(&drm_minors_idr, NULL, minor->index);
365         spin_unlock_irqrestore(&drm_minor_lock, flags);
366
367         device_del(minor->kdev);
368         dev_set_drvdata(minor->kdev, NULL); /* safety belt */
369         drm_debugfs_cleanup(minor);
370 }
371
372 /**
373  * drm_minor_acquire - Acquire a DRM minor
374  * @minor_id: Minor ID of the DRM-minor
375  *
376  * Looks up the given minor-ID and returns the respective DRM-minor object. The
377  * refence-count of the underlying device is increased so you must release this
378  * object with drm_minor_release().
379  *
380  * As long as you hold this minor, it is guaranteed that the object and the
381  * minor->dev pointer will stay valid! However, the device may get unplugged and
382  * unregistered while you hold the minor.
383  *
384  * Returns:
385  * Pointer to minor-object with increased device-refcount, or PTR_ERR on
386  * failure.
387  */
388 struct drm_minor *drm_minor_acquire(unsigned int minor_id)
389 {
390         struct drm_minor *minor;
391         unsigned long flags;
392
393         spin_lock_irqsave(&drm_minor_lock, flags);
394         minor = idr_find(&drm_minors_idr, minor_id);
395         if (minor)
396                 drm_dev_ref(minor->dev);
397         spin_unlock_irqrestore(&drm_minor_lock, flags);
398
399         if (!minor) {
400                 return ERR_PTR(-ENODEV);
401         } else if (drm_device_is_unplugged(minor->dev)) {
402                 drm_dev_unref(minor->dev);
403                 return ERR_PTR(-ENODEV);
404         }
405
406         return minor;
407 }
408
409 /**
410  * drm_minor_release - Release DRM minor
411  * @minor: Pointer to DRM minor object
412  *
413  * Release a minor that was previously acquired via drm_minor_acquire().
414  */
415 void drm_minor_release(struct drm_minor *minor)
416 {
417         drm_dev_unref(minor->dev);
418 }
419
420 struct drm_device *drm_device_get_by_name(const char *name)
421 {
422         int i;
423
424         for (i = 0; i < 64; i++) {
425                 struct drm_minor *minor;
426
427                 minor = drm_minor_acquire(i + DRM_MINOR_CONTROL);
428                 if (IS_ERR(minor))
429                         continue;
430                 if (!minor->dev || !minor->dev->driver ||
431                     !minor->dev->driver->name)
432                         continue;
433                 if (!name)
434                         return minor->dev;
435                 if (!strcmp(name, minor->dev->driver->name))
436                         return minor->dev;
437         }
438
439         return NULL;
440 }
441
442 /**
443  * DOC: driver instance overview
444  *
445  * A device instance for a drm driver is represented by struct &drm_device. This
446  * is allocated with drm_dev_alloc(), usually from bus-specific ->probe()
447  * callbacks implemented by the driver. The driver then needs to initialize all
448  * the various subsystems for the drm device like memory management, vblank
449  * handling, modesetting support and intial output configuration plus obviously
450  * initialize all the corresponding hardware bits. An important part of this is
451  * also calling drm_dev_set_unique() to set the userspace-visible unique name of
452  * this device instance. Finally when everything is up and running and ready for
453  * userspace the device instance can be published using drm_dev_register().
454  *
455  * There is also deprecated support for initalizing device instances using
456  * bus-specific helpers and the ->load() callback. But due to
457  * backwards-compatibility needs the device instance have to be published too
458  * early, which requires unpretty global locking to make safe and is therefore
459  * only support for existing drivers not yet converted to the new scheme.
460  *
461  * When cleaning up a device instance everything needs to be done in reverse:
462  * First unpublish the device instance with drm_dev_unregister(). Then clean up
463  * any other resources allocated at device initialization and drop the driver's
464  * reference to &drm_device using drm_dev_unref().
465  *
466  * Note that the lifetime rules for &drm_device instance has still a lot of
467  * historical baggage. Hence use the reference counting provided by
468  * drm_dev_ref() and drm_dev_unref() only carefully.
469  *
470  * Also note that embedding of &drm_device is currently not (yet) supported (but
471  * it would be easy to add). Drivers can store driver-private data in the
472  * dev_priv field of &drm_device.
473  */
474
475 /**
476  * drm_put_dev - Unregister and release a DRM device
477  * @dev: DRM device
478  *
479  * Called at module unload time or when a PCI device is unplugged.
480  *
481  * Cleans up all DRM device, calling drm_lastclose().
482  *
483  * Note: Use of this function is deprecated. It will eventually go away
484  * completely.  Please use drm_dev_unregister() and drm_dev_unref() explicitly
485  * instead to make sure that the device isn't userspace accessible any more
486  * while teardown is in progress, ensuring that userspace can't access an
487  * inconsistent state.
488  */
489 void drm_put_dev(struct drm_device *dev)
490 {
491         DRM_DEBUG("\n");
492
493         if (!dev) {
494                 DRM_ERROR("cleanup called no dev\n");
495                 return;
496         }
497
498         drm_dev_unregister(dev);
499         drm_dev_unref(dev);
500 }
501 EXPORT_SYMBOL(drm_put_dev);
502
503 void drm_unplug_dev(struct drm_device *dev)
504 {
505         /* for a USB device */
506         drm_minor_unregister(dev, DRM_MINOR_LEGACY);
507         drm_minor_unregister(dev, DRM_MINOR_RENDER);
508         drm_minor_unregister(dev, DRM_MINOR_CONTROL);
509
510         mutex_lock(&drm_global_mutex);
511
512         drm_device_set_unplugged(dev);
513
514         if (dev->open_count == 0) {
515                 drm_put_dev(dev);
516         }
517         mutex_unlock(&drm_global_mutex);
518 }
519 EXPORT_SYMBOL(drm_unplug_dev);
520
521 /*
522  * DRM internal mount
523  * We want to be able to allocate our own "struct address_space" to control
524  * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
525  * stand-alone address_space objects, so we need an underlying inode. As there
526  * is no way to allocate an independent inode easily, we need a fake internal
527  * VFS mount-point.
528  *
529  * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
530  * frees it again. You are allowed to use iget() and iput() to get references to
531  * the inode. But each drm_fs_inode_new() call must be paired with exactly one
532  * drm_fs_inode_free() call (which does not have to be the last iput()).
533  * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
534  * between multiple inode-users. You could, technically, call
535  * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
536  * iput(), but this way you'd end up with a new vfsmount for each inode.
537  */
538
539 static int drm_fs_cnt;
540 static struct vfsmount *drm_fs_mnt;
541
542 static const struct dentry_operations drm_fs_dops = {
543         .d_dname        = simple_dname,
544 };
545
546 static const struct super_operations drm_fs_sops = {
547         .statfs         = simple_statfs,
548 };
549
550 static struct dentry *drm_fs_mount(struct file_system_type *fs_type, int flags,
551                                    const char *dev_name, void *data)
552 {
553         return mount_pseudo(fs_type,
554                             "drm:",
555                             &drm_fs_sops,
556                             &drm_fs_dops,
557                             0x010203ff);
558 }
559
560 static struct file_system_type drm_fs_type = {
561         .name           = "drm",
562         .owner          = THIS_MODULE,
563         .mount          = drm_fs_mount,
564         .kill_sb        = kill_anon_super,
565 };
566
567 static struct inode *drm_fs_inode_new(void)
568 {
569         struct inode *inode;
570         int r;
571
572         r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt);
573         if (r < 0) {
574                 DRM_ERROR("Cannot mount pseudo fs: %d\n", r);
575                 return ERR_PTR(r);
576         }
577
578         inode = alloc_anon_inode(drm_fs_mnt->mnt_sb);
579         if (IS_ERR(inode))
580                 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
581
582         return inode;
583 }
584
585 static void drm_fs_inode_free(struct inode *inode)
586 {
587         if (inode) {
588                 iput(inode);
589                 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
590         }
591 }
592
593 /**
594  * drm_dev_alloc - Allocate new DRM device
595  * @driver: DRM driver to allocate device for
596  * @parent: Parent device object
597  *
598  * Allocate and initialize a new DRM device. No device registration is done.
599  * Call drm_dev_register() to advertice the device to user space and register it
600  * with other core subsystems. This should be done last in the device
601  * initialization sequence to make sure userspace can't access an inconsistent
602  * state.
603  *
604  * The initial ref-count of the object is 1. Use drm_dev_ref() and
605  * drm_dev_unref() to take and drop further ref-counts.
606  *
607  * Note that for purely virtual devices @parent can be NULL.
608  *
609  * RETURNS:
610  * Pointer to new DRM device, or NULL if out of memory.
611  */
612 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
613                                  struct device *parent)
614 {
615         struct drm_device *dev;
616         int ret;
617
618         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
619         if (!dev)
620                 return NULL;
621
622         kref_init(&dev->ref);
623         dev->dev = parent;
624         dev->driver = driver;
625
626         INIT_LIST_HEAD(&dev->filelist);
627         INIT_LIST_HEAD(&dev->ctxlist);
628         INIT_LIST_HEAD(&dev->vmalist);
629         INIT_LIST_HEAD(&dev->maplist);
630         INIT_LIST_HEAD(&dev->vblank_event_list);
631
632         spin_lock_init(&dev->buf_lock);
633         spin_lock_init(&dev->event_lock);
634         mutex_init(&dev->struct_mutex);
635         mutex_init(&dev->ctxlist_mutex);
636         mutex_init(&dev->master_mutex);
637
638         dev->anon_inode = drm_fs_inode_new();
639         if (IS_ERR(dev->anon_inode)) {
640                 ret = PTR_ERR(dev->anon_inode);
641                 DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
642                 goto err_free;
643         }
644
645         if (drm_core_check_feature(dev, DRIVER_MODESET)) {
646                 ret = drm_minor_alloc(dev, DRM_MINOR_CONTROL);
647                 if (ret)
648                         goto err_minors;
649
650                 WARN_ON(driver->suspend || driver->resume);
651         }
652
653         if (drm_core_check_feature(dev, DRIVER_RENDER)) {
654                 ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
655                 if (ret)
656                         goto err_minors;
657         }
658
659         ret = drm_minor_alloc(dev, DRM_MINOR_LEGACY);
660         if (ret)
661                 goto err_minors;
662
663         if (drm_ht_create(&dev->map_hash, 12))
664                 goto err_minors;
665
666         drm_legacy_ctxbitmap_init(dev);
667
668         if (drm_core_check_feature(dev, DRIVER_GEM)) {
669                 ret = drm_gem_init(dev);
670                 if (ret) {
671                         DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
672                         goto err_ctxbitmap;
673                 }
674         }
675
676         return dev;
677
678 err_ctxbitmap:
679         drm_legacy_ctxbitmap_cleanup(dev);
680         drm_ht_remove(&dev->map_hash);
681 err_minors:
682         drm_minor_free(dev, DRM_MINOR_LEGACY);
683         drm_minor_free(dev, DRM_MINOR_RENDER);
684         drm_minor_free(dev, DRM_MINOR_CONTROL);
685         drm_fs_inode_free(dev->anon_inode);
686 err_free:
687         mutex_destroy(&dev->master_mutex);
688         kfree(dev);
689         return NULL;
690 }
691 EXPORT_SYMBOL(drm_dev_alloc);
692
693 static void drm_dev_release(struct kref *ref)
694 {
695         struct drm_device *dev = container_of(ref, struct drm_device, ref);
696
697         if (drm_core_check_feature(dev, DRIVER_GEM))
698                 drm_gem_destroy(dev);
699
700         drm_legacy_ctxbitmap_cleanup(dev);
701         drm_ht_remove(&dev->map_hash);
702         drm_fs_inode_free(dev->anon_inode);
703
704         drm_minor_free(dev, DRM_MINOR_LEGACY);
705         drm_minor_free(dev, DRM_MINOR_RENDER);
706         drm_minor_free(dev, DRM_MINOR_CONTROL);
707
708         mutex_destroy(&dev->master_mutex);
709         kfree(dev->unique);
710         kfree(dev);
711 }
712
713 /**
714  * drm_dev_ref - Take reference of a DRM device
715  * @dev: device to take reference of or NULL
716  *
717  * This increases the ref-count of @dev by one. You *must* already own a
718  * reference when calling this. Use drm_dev_unref() to drop this reference
719  * again.
720  *
721  * This function never fails. However, this function does not provide *any*
722  * guarantee whether the device is alive or running. It only provides a
723  * reference to the object and the memory associated with it.
724  */
725 void drm_dev_ref(struct drm_device *dev)
726 {
727         if (dev)
728                 kref_get(&dev->ref);
729 }
730 EXPORT_SYMBOL(drm_dev_ref);
731
732 /**
733  * drm_dev_unref - Drop reference of a DRM device
734  * @dev: device to drop reference of or NULL
735  *
736  * This decreases the ref-count of @dev by one. The device is destroyed if the
737  * ref-count drops to zero.
738  */
739 void drm_dev_unref(struct drm_device *dev)
740 {
741         if (dev)
742                 kref_put(&dev->ref, drm_dev_release);
743 }
744 EXPORT_SYMBOL(drm_dev_unref);
745
746 /**
747  * drm_dev_register - Register DRM device
748  * @dev: Device to register
749  * @flags: Flags passed to the driver's .load() function
750  *
751  * Register the DRM device @dev with the system, advertise device to user-space
752  * and start normal device operation. @dev must be allocated via drm_dev_alloc()
753  * previously.
754  *
755  * Never call this twice on any device!
756  *
757  * NOTE: To ensure backward compatibility with existing drivers method this
758  * function calls the ->load() method after registering the device nodes,
759  * creating race conditions. Usage of the ->load() methods is therefore
760  * deprecated, drivers must perform all initialization before calling
761  * drm_dev_register().
762  *
763  * RETURNS:
764  * 0 on success, negative error code on failure.
765  */
766 int drm_dev_register(struct drm_device *dev, unsigned long flags)
767 {
768         int ret;
769
770         mutex_lock(&drm_global_mutex);
771
772         ret = drm_minor_register(dev, DRM_MINOR_CONTROL);
773         if (ret)
774                 goto err_minors;
775
776         ret = drm_minor_register(dev, DRM_MINOR_RENDER);
777         if (ret)
778                 goto err_minors;
779
780         ret = drm_minor_register(dev, DRM_MINOR_LEGACY);
781         if (ret)
782                 goto err_minors;
783
784         if (dev->driver->load) {
785                 ret = dev->driver->load(dev, flags);
786                 if (ret)
787                         goto err_minors;
788         }
789
790         if (drm_core_check_feature(dev, DRIVER_MODESET))
791                 drm_modeset_register_all(dev);
792
793         ret = 0;
794         goto out_unlock;
795
796 err_minors:
797         drm_minor_unregister(dev, DRM_MINOR_LEGACY);
798         drm_minor_unregister(dev, DRM_MINOR_RENDER);
799         drm_minor_unregister(dev, DRM_MINOR_CONTROL);
800 out_unlock:
801         mutex_unlock(&drm_global_mutex);
802         return ret;
803 }
804 EXPORT_SYMBOL(drm_dev_register);
805
806 /**
807  * drm_dev_unregister - Unregister DRM device
808  * @dev: Device to unregister
809  *
810  * Unregister the DRM device from the system. This does the reverse of
811  * drm_dev_register() but does not deallocate the device. The caller must call
812  * drm_dev_unref() to drop their final reference.
813  *
814  * This should be called first in the device teardown code to make sure
815  * userspace can't access the device instance any more.
816  */
817 void drm_dev_unregister(struct drm_device *dev)
818 {
819         struct drm_map_list *r_list, *list_temp;
820
821         drm_lastclose(dev);
822
823         if (drm_core_check_feature(dev, DRIVER_MODESET))
824                 drm_modeset_unregister_all(dev);
825
826         if (dev->driver->unload)
827                 dev->driver->unload(dev);
828
829         if (dev->agp)
830                 drm_pci_agp_destroy(dev);
831
832         drm_vblank_cleanup(dev);
833
834         list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
835                 drm_legacy_rmmap(dev, r_list->map);
836
837         drm_minor_unregister(dev, DRM_MINOR_LEGACY);
838         drm_minor_unregister(dev, DRM_MINOR_RENDER);
839         drm_minor_unregister(dev, DRM_MINOR_CONTROL);
840 }
841 EXPORT_SYMBOL(drm_dev_unregister);
842
843 /**
844  * drm_dev_set_unique - Set the unique name of a DRM device
845  * @dev: device of which to set the unique name
846  * @fmt: format string for unique name
847  *
848  * Sets the unique name of a DRM device using the specified format string and
849  * a variable list of arguments. Drivers can use this at driver probe time if
850  * the unique name of the devices they drive is static.
851  *
852  * Return: 0 on success or a negative error code on failure.
853  */
854 int drm_dev_set_unique(struct drm_device *dev, const char *fmt, ...)
855 {
856         va_list ap;
857
858         kfree(dev->unique);
859
860         va_start(ap, fmt);
861         dev->unique = kvasprintf(GFP_KERNEL, fmt, ap);
862         va_end(ap);
863
864         return dev->unique ? 0 : -ENOMEM;
865 }
866 EXPORT_SYMBOL(drm_dev_set_unique);
867
868 /*
869  * DRM Core
870  * The DRM core module initializes all global DRM objects and makes them
871  * available to drivers. Once setup, drivers can probe their respective
872  * devices.
873  * Currently, core management includes:
874  *  - The "DRM-Global" key/value database
875  *  - Global ID management for connectors
876  *  - DRM major number allocation
877  *  - DRM minor management
878  *  - DRM sysfs class
879  *  - DRM debugfs root
880  *
881  * Furthermore, the DRM core provides dynamic char-dev lookups. For each
882  * interface registered on a DRM device, you can request minor numbers from DRM
883  * core. DRM core takes care of major-number management and char-dev
884  * registration. A stub ->open() callback forwards any open() requests to the
885  * registered minor.
886  */
887
888 static int drm_stub_open(struct inode *inode, struct file *filp)
889 {
890         const struct file_operations *new_fops;
891         struct drm_minor *minor;
892         int err;
893
894         DRM_DEBUG("\n");
895
896         mutex_lock(&drm_global_mutex);
897         minor = drm_minor_acquire(iminor(inode));
898         if (IS_ERR(minor)) {
899                 err = PTR_ERR(minor);
900                 goto out_unlock;
901         }
902
903         new_fops = fops_get(minor->dev->driver->fops);
904         if (!new_fops) {
905                 err = -ENODEV;
906                 goto out_release;
907         }
908
909         replace_fops(filp, new_fops);
910         if (filp->f_op->open)
911                 err = filp->f_op->open(inode, filp);
912         else
913                 err = 0;
914
915 out_release:
916         drm_minor_release(minor);
917 out_unlock:
918         mutex_unlock(&drm_global_mutex);
919         return err;
920 }
921
922 static const struct file_operations drm_stub_fops = {
923         .owner = THIS_MODULE,
924         .open = drm_stub_open,
925         .llseek = noop_llseek,
926 };
927
928 static int __init drm_core_init(void)
929 {
930         int ret = -ENOMEM;
931
932         drm_global_init();
933         drm_connector_ida_init();
934         idr_init(&drm_minors_idr);
935
936         if (register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops))
937                 goto err_p1;
938
939         ret = drm_sysfs_init();
940         if (ret < 0) {
941                 printk(KERN_ERR "DRM: Error creating drm class.\n");
942                 goto err_p2;
943         }
944
945         drm_debugfs_root = debugfs_create_dir("dri", NULL);
946         if (!drm_debugfs_root) {
947                 DRM_ERROR("Cannot create /sys/kernel/debug/dri\n");
948                 ret = -1;
949                 goto err_p3;
950         }
951
952         DRM_INFO("Initialized %s %d.%d.%d %s\n",
953                  CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
954         return 0;
955 err_p3:
956         drm_sysfs_destroy();
957 err_p2:
958         unregister_chrdev(DRM_MAJOR, "drm");
959
960         idr_destroy(&drm_minors_idr);
961 err_p1:
962         return ret;
963 }
964
965 static void __exit drm_core_exit(void)
966 {
967         debugfs_remove(drm_debugfs_root);
968         drm_sysfs_destroy();
969
970         unregister_chrdev(DRM_MAJOR, "drm");
971
972         drm_connector_ida_destroy();
973         idr_destroy(&drm_minors_idr);
974 }
975
976 module_init(drm_core_init);
977 module_exit(drm_core_exit);