ARM64: DTS: Add rk3399-firefly uart4 device, node as /dev/ttyS1
[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 /**
421  * DOC: driver instance overview
422  *
423  * A device instance for a drm driver is represented by struct &drm_device. This
424  * is allocated with drm_dev_alloc(), usually from bus-specific ->probe()
425  * callbacks implemented by the driver. The driver then needs to initialize all
426  * the various subsystems for the drm device like memory management, vblank
427  * handling, modesetting support and intial output configuration plus obviously
428  * initialize all the corresponding hardware bits. An important part of this is
429  * also calling drm_dev_set_unique() to set the userspace-visible unique name of
430  * this device instance. Finally when everything is up and running and ready for
431  * userspace the device instance can be published using drm_dev_register().
432  *
433  * There is also deprecated support for initalizing device instances using
434  * bus-specific helpers and the ->load() callback. But due to
435  * backwards-compatibility needs the device instance have to be published too
436  * early, which requires unpretty global locking to make safe and is therefore
437  * only support for existing drivers not yet converted to the new scheme.
438  *
439  * When cleaning up a device instance everything needs to be done in reverse:
440  * First unpublish the device instance with drm_dev_unregister(). Then clean up
441  * any other resources allocated at device initialization and drop the driver's
442  * reference to &drm_device using drm_dev_unref().
443  *
444  * Note that the lifetime rules for &drm_device instance has still a lot of
445  * historical baggage. Hence use the reference counting provided by
446  * drm_dev_ref() and drm_dev_unref() only carefully.
447  *
448  * Also note that embedding of &drm_device is currently not (yet) supported (but
449  * it would be easy to add). Drivers can store driver-private data in the
450  * dev_priv field of &drm_device.
451  */
452
453 /**
454  * drm_put_dev - Unregister and release a DRM device
455  * @dev: DRM device
456  *
457  * Called at module unload time or when a PCI device is unplugged.
458  *
459  * Cleans up all DRM device, calling drm_lastclose().
460  *
461  * Note: Use of this function is deprecated. It will eventually go away
462  * completely.  Please use drm_dev_unregister() and drm_dev_unref() explicitly
463  * instead to make sure that the device isn't userspace accessible any more
464  * while teardown is in progress, ensuring that userspace can't access an
465  * inconsistent state.
466  */
467 void drm_put_dev(struct drm_device *dev)
468 {
469         DRM_DEBUG("\n");
470
471         if (!dev) {
472                 DRM_ERROR("cleanup called no dev\n");
473                 return;
474         }
475
476         drm_dev_unregister(dev);
477         drm_dev_unref(dev);
478 }
479 EXPORT_SYMBOL(drm_put_dev);
480
481 void drm_unplug_dev(struct drm_device *dev)
482 {
483         /* for a USB device */
484         drm_minor_unregister(dev, DRM_MINOR_LEGACY);
485         drm_minor_unregister(dev, DRM_MINOR_RENDER);
486         drm_minor_unregister(dev, DRM_MINOR_CONTROL);
487
488         mutex_lock(&drm_global_mutex);
489
490         drm_device_set_unplugged(dev);
491
492         if (dev->open_count == 0) {
493                 drm_put_dev(dev);
494         }
495         mutex_unlock(&drm_global_mutex);
496 }
497 EXPORT_SYMBOL(drm_unplug_dev);
498
499 /*
500  * DRM internal mount
501  * We want to be able to allocate our own "struct address_space" to control
502  * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
503  * stand-alone address_space objects, so we need an underlying inode. As there
504  * is no way to allocate an independent inode easily, we need a fake internal
505  * VFS mount-point.
506  *
507  * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
508  * frees it again. You are allowed to use iget() and iput() to get references to
509  * the inode. But each drm_fs_inode_new() call must be paired with exactly one
510  * drm_fs_inode_free() call (which does not have to be the last iput()).
511  * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
512  * between multiple inode-users. You could, technically, call
513  * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
514  * iput(), but this way you'd end up with a new vfsmount for each inode.
515  */
516
517 static int drm_fs_cnt;
518 static struct vfsmount *drm_fs_mnt;
519
520 static const struct dentry_operations drm_fs_dops = {
521         .d_dname        = simple_dname,
522 };
523
524 static const struct super_operations drm_fs_sops = {
525         .statfs         = simple_statfs,
526 };
527
528 static struct dentry *drm_fs_mount(struct file_system_type *fs_type, int flags,
529                                    const char *dev_name, void *data)
530 {
531         return mount_pseudo(fs_type,
532                             "drm:",
533                             &drm_fs_sops,
534                             &drm_fs_dops,
535                             0x010203ff);
536 }
537
538 static struct file_system_type drm_fs_type = {
539         .name           = "drm",
540         .owner          = THIS_MODULE,
541         .mount          = drm_fs_mount,
542         .kill_sb        = kill_anon_super,
543 };
544
545 static struct inode *drm_fs_inode_new(void)
546 {
547         struct inode *inode;
548         int r;
549
550         r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt);
551         if (r < 0) {
552                 DRM_ERROR("Cannot mount pseudo fs: %d\n", r);
553                 return ERR_PTR(r);
554         }
555
556         inode = alloc_anon_inode(drm_fs_mnt->mnt_sb);
557         if (IS_ERR(inode))
558                 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
559
560         return inode;
561 }
562
563 static void drm_fs_inode_free(struct inode *inode)
564 {
565         if (inode) {
566                 iput(inode);
567                 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
568         }
569 }
570
571 /**
572  * drm_dev_alloc - Allocate new DRM device
573  * @driver: DRM driver to allocate device for
574  * @parent: Parent device object
575  *
576  * Allocate and initialize a new DRM device. No device registration is done.
577  * Call drm_dev_register() to advertice the device to user space and register it
578  * with other core subsystems. This should be done last in the device
579  * initialization sequence to make sure userspace can't access an inconsistent
580  * state.
581  *
582  * The initial ref-count of the object is 1. Use drm_dev_ref() and
583  * drm_dev_unref() to take and drop further ref-counts.
584  *
585  * Note that for purely virtual devices @parent can be NULL.
586  *
587  * RETURNS:
588  * Pointer to new DRM device, or NULL if out of memory.
589  */
590 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
591                                  struct device *parent)
592 {
593         struct drm_device *dev;
594         int ret;
595
596         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
597         if (!dev)
598                 return NULL;
599
600         kref_init(&dev->ref);
601         dev->dev = parent;
602         dev->driver = driver;
603
604         INIT_LIST_HEAD(&dev->filelist);
605         INIT_LIST_HEAD(&dev->ctxlist);
606         INIT_LIST_HEAD(&dev->vmalist);
607         INIT_LIST_HEAD(&dev->maplist);
608         INIT_LIST_HEAD(&dev->vblank_event_list);
609
610         spin_lock_init(&dev->buf_lock);
611         spin_lock_init(&dev->event_lock);
612         mutex_init(&dev->struct_mutex);
613         mutex_init(&dev->ctxlist_mutex);
614         mutex_init(&dev->master_mutex);
615
616         dev->anon_inode = drm_fs_inode_new();
617         if (IS_ERR(dev->anon_inode)) {
618                 ret = PTR_ERR(dev->anon_inode);
619                 DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
620                 goto err_free;
621         }
622
623         if (drm_core_check_feature(dev, DRIVER_MODESET)) {
624                 ret = drm_minor_alloc(dev, DRM_MINOR_CONTROL);
625                 if (ret)
626                         goto err_minors;
627
628                 WARN_ON(driver->suspend || driver->resume);
629         }
630
631         if (drm_core_check_feature(dev, DRIVER_RENDER)) {
632                 ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
633                 if (ret)
634                         goto err_minors;
635         }
636
637         ret = drm_minor_alloc(dev, DRM_MINOR_LEGACY);
638         if (ret)
639                 goto err_minors;
640
641         if (drm_ht_create(&dev->map_hash, 12))
642                 goto err_minors;
643
644         drm_legacy_ctxbitmap_init(dev);
645
646         if (drm_core_check_feature(dev, DRIVER_GEM)) {
647                 ret = drm_gem_init(dev);
648                 if (ret) {
649                         DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
650                         goto err_ctxbitmap;
651                 }
652         }
653
654         return dev;
655
656 err_ctxbitmap:
657         drm_legacy_ctxbitmap_cleanup(dev);
658         drm_ht_remove(&dev->map_hash);
659 err_minors:
660         drm_minor_free(dev, DRM_MINOR_LEGACY);
661         drm_minor_free(dev, DRM_MINOR_RENDER);
662         drm_minor_free(dev, DRM_MINOR_CONTROL);
663         drm_fs_inode_free(dev->anon_inode);
664 err_free:
665         mutex_destroy(&dev->master_mutex);
666         kfree(dev);
667         return NULL;
668 }
669 EXPORT_SYMBOL(drm_dev_alloc);
670
671 static void drm_dev_release(struct kref *ref)
672 {
673         struct drm_device *dev = container_of(ref, struct drm_device, ref);
674
675         if (drm_core_check_feature(dev, DRIVER_GEM))
676                 drm_gem_destroy(dev);
677
678         drm_legacy_ctxbitmap_cleanup(dev);
679         drm_ht_remove(&dev->map_hash);
680         drm_fs_inode_free(dev->anon_inode);
681
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
686         mutex_destroy(&dev->master_mutex);
687         kfree(dev->unique);
688         kfree(dev);
689 }
690
691 /**
692  * drm_dev_ref - Take reference of a DRM device
693  * @dev: device to take reference of or NULL
694  *
695  * This increases the ref-count of @dev by one. You *must* already own a
696  * reference when calling this. Use drm_dev_unref() to drop this reference
697  * again.
698  *
699  * This function never fails. However, this function does not provide *any*
700  * guarantee whether the device is alive or running. It only provides a
701  * reference to the object and the memory associated with it.
702  */
703 void drm_dev_ref(struct drm_device *dev)
704 {
705         if (dev)
706                 kref_get(&dev->ref);
707 }
708 EXPORT_SYMBOL(drm_dev_ref);
709
710 /**
711  * drm_dev_unref - Drop reference of a DRM device
712  * @dev: device to drop reference of or NULL
713  *
714  * This decreases the ref-count of @dev by one. The device is destroyed if the
715  * ref-count drops to zero.
716  */
717 void drm_dev_unref(struct drm_device *dev)
718 {
719         if (dev)
720                 kref_put(&dev->ref, drm_dev_release);
721 }
722 EXPORT_SYMBOL(drm_dev_unref);
723
724 /**
725  * drm_dev_register - Register DRM device
726  * @dev: Device to register
727  * @flags: Flags passed to the driver's .load() function
728  *
729  * Register the DRM device @dev with the system, advertise device to user-space
730  * and start normal device operation. @dev must be allocated via drm_dev_alloc()
731  * previously.
732  *
733  * Never call this twice on any device!
734  *
735  * NOTE: To ensure backward compatibility with existing drivers method this
736  * function calls the ->load() method after registering the device nodes,
737  * creating race conditions. Usage of the ->load() methods is therefore
738  * deprecated, drivers must perform all initialization before calling
739  * drm_dev_register().
740  *
741  * RETURNS:
742  * 0 on success, negative error code on failure.
743  */
744 int drm_dev_register(struct drm_device *dev, unsigned long flags)
745 {
746         int ret;
747
748         mutex_lock(&drm_global_mutex);
749
750         ret = drm_minor_register(dev, DRM_MINOR_CONTROL);
751         if (ret)
752                 goto err_minors;
753
754         ret = drm_minor_register(dev, DRM_MINOR_RENDER);
755         if (ret)
756                 goto err_minors;
757
758         ret = drm_minor_register(dev, DRM_MINOR_LEGACY);
759         if (ret)
760                 goto err_minors;
761
762         if (dev->driver->load) {
763                 ret = dev->driver->load(dev, flags);
764                 if (ret)
765                         goto err_minors;
766         }
767
768         if (drm_core_check_feature(dev, DRIVER_MODESET))
769                 drm_modeset_register_all(dev);
770
771         ret = 0;
772         goto out_unlock;
773
774 err_minors:
775         drm_minor_unregister(dev, DRM_MINOR_LEGACY);
776         drm_minor_unregister(dev, DRM_MINOR_RENDER);
777         drm_minor_unregister(dev, DRM_MINOR_CONTROL);
778 out_unlock:
779         mutex_unlock(&drm_global_mutex);
780         return ret;
781 }
782 EXPORT_SYMBOL(drm_dev_register);
783
784 /**
785  * drm_dev_unregister - Unregister DRM device
786  * @dev: Device to unregister
787  *
788  * Unregister the DRM device from the system. This does the reverse of
789  * drm_dev_register() but does not deallocate the device. The caller must call
790  * drm_dev_unref() to drop their final reference.
791  *
792  * This should be called first in the device teardown code to make sure
793  * userspace can't access the device instance any more.
794  */
795 void drm_dev_unregister(struct drm_device *dev)
796 {
797         struct drm_map_list *r_list, *list_temp;
798
799         drm_lastclose(dev);
800
801         if (drm_core_check_feature(dev, DRIVER_MODESET))
802                 drm_modeset_unregister_all(dev);
803
804         if (dev->driver->unload)
805                 dev->driver->unload(dev);
806
807         if (dev->agp)
808                 drm_pci_agp_destroy(dev);
809
810         drm_vblank_cleanup(dev);
811
812         list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
813                 drm_legacy_rmmap(dev, r_list->map);
814
815         drm_minor_unregister(dev, DRM_MINOR_LEGACY);
816         drm_minor_unregister(dev, DRM_MINOR_RENDER);
817         drm_minor_unregister(dev, DRM_MINOR_CONTROL);
818 }
819 EXPORT_SYMBOL(drm_dev_unregister);
820
821 /**
822  * drm_dev_set_unique - Set the unique name of a DRM device
823  * @dev: device of which to set the unique name
824  * @fmt: format string for unique name
825  *
826  * Sets the unique name of a DRM device using the specified format string and
827  * a variable list of arguments. Drivers can use this at driver probe time if
828  * the unique name of the devices they drive is static.
829  *
830  * Return: 0 on success or a negative error code on failure.
831  */
832 int drm_dev_set_unique(struct drm_device *dev, const char *fmt, ...)
833 {
834         va_list ap;
835
836         kfree(dev->unique);
837
838         va_start(ap, fmt);
839         dev->unique = kvasprintf(GFP_KERNEL, fmt, ap);
840         va_end(ap);
841
842         return dev->unique ? 0 : -ENOMEM;
843 }
844 EXPORT_SYMBOL(drm_dev_set_unique);
845
846 /*
847  * DRM Core
848  * The DRM core module initializes all global DRM objects and makes them
849  * available to drivers. Once setup, drivers can probe their respective
850  * devices.
851  * Currently, core management includes:
852  *  - The "DRM-Global" key/value database
853  *  - Global ID management for connectors
854  *  - DRM major number allocation
855  *  - DRM minor management
856  *  - DRM sysfs class
857  *  - DRM debugfs root
858  *
859  * Furthermore, the DRM core provides dynamic char-dev lookups. For each
860  * interface registered on a DRM device, you can request minor numbers from DRM
861  * core. DRM core takes care of major-number management and char-dev
862  * registration. A stub ->open() callback forwards any open() requests to the
863  * registered minor.
864  */
865
866 static int drm_stub_open(struct inode *inode, struct file *filp)
867 {
868         const struct file_operations *new_fops;
869         struct drm_minor *minor;
870         int err;
871
872         DRM_DEBUG("\n");
873
874         mutex_lock(&drm_global_mutex);
875         minor = drm_minor_acquire(iminor(inode));
876         if (IS_ERR(minor)) {
877                 err = PTR_ERR(minor);
878                 goto out_unlock;
879         }
880
881         new_fops = fops_get(minor->dev->driver->fops);
882         if (!new_fops) {
883                 err = -ENODEV;
884                 goto out_release;
885         }
886
887         replace_fops(filp, new_fops);
888         if (filp->f_op->open)
889                 err = filp->f_op->open(inode, filp);
890         else
891                 err = 0;
892
893 out_release:
894         drm_minor_release(minor);
895 out_unlock:
896         mutex_unlock(&drm_global_mutex);
897         return err;
898 }
899
900 static const struct file_operations drm_stub_fops = {
901         .owner = THIS_MODULE,
902         .open = drm_stub_open,
903         .llseek = noop_llseek,
904 };
905
906 static int __init drm_core_init(void)
907 {
908         int ret = -ENOMEM;
909
910         drm_global_init();
911         drm_connector_ida_init();
912         idr_init(&drm_minors_idr);
913
914         if (register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops))
915                 goto err_p1;
916
917         ret = drm_sysfs_init();
918         if (ret < 0) {
919                 printk(KERN_ERR "DRM: Error creating drm class.\n");
920                 goto err_p2;
921         }
922
923         drm_debugfs_root = debugfs_create_dir("dri", NULL);
924         if (!drm_debugfs_root) {
925                 DRM_ERROR("Cannot create /sys/kernel/debug/dri\n");
926                 ret = -1;
927                 goto err_p3;
928         }
929
930         DRM_INFO("Initialized %s %d.%d.%d %s\n",
931                  CORE_NAME, CORE_MAJOR, CORE_MINOR, CORE_PATCHLEVEL, CORE_DATE);
932         return 0;
933 err_p3:
934         drm_sysfs_destroy();
935 err_p2:
936         unregister_chrdev(DRM_MAJOR, "drm");
937
938         idr_destroy(&drm_minors_idr);
939 err_p1:
940         return ret;
941 }
942
943 static void __exit drm_core_exit(void)
944 {
945         debugfs_remove(drm_debugfs_root);
946         drm_sysfs_destroy();
947
948         unregister_chrdev(DRM_MAJOR, "drm");
949
950         drm_connector_ida_destroy();
951         idr_destroy(&drm_minors_idr);
952 }
953
954 module_init(drm_core_init);
955 module_exit(drm_core_exit);