drm: add pseudo filesystem for shared inodes
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / gpu / drm / drm_stub.c
1 /**
2  * \file drm_stub.h
3  * Stub support
4  *
5  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6  */
7
8 /*
9  * Created: Fri Jan 19 10:48:35 2001 by faith@acm.org
10  *
11  * Copyright 2001 VA Linux Systems, Inc., Sunnyvale, California.
12  * All Rights Reserved.
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a
15  * copy of this software and associated documentation files (the "Software"),
16  * to deal in the Software without restriction, including without limitation
17  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18  * and/or sell copies of the Software, and to permit persons to whom the
19  * Software is furnished to do so, subject to the following conditions:
20  *
21  * The above copyright notice and this permission notice (including the next
22  * paragraph) shall be included in all copies or substantial portions of the
23  * Software.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
28  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31  * DEALINGS IN THE SOFTWARE.
32  */
33
34 #include <linux/fs.h>
35 #include <linux/module.h>
36 #include <linux/moduleparam.h>
37 #include <linux/mount.h>
38 #include <linux/slab.h>
39 #include <drm/drmP.h>
40 #include <drm/drm_core.h>
41
42 unsigned int drm_debug = 0;     /* 1 to enable debug output */
43 EXPORT_SYMBOL(drm_debug);
44
45 unsigned int drm_rnodes = 0;    /* 1 to enable experimental render nodes API */
46 EXPORT_SYMBOL(drm_rnodes);
47
48 unsigned int drm_vblank_offdelay = 5000;    /* Default to 5000 msecs. */
49 EXPORT_SYMBOL(drm_vblank_offdelay);
50
51 unsigned int drm_timestamp_precision = 20;  /* Default to 20 usecs. */
52 EXPORT_SYMBOL(drm_timestamp_precision);
53
54 /*
55  * Default to use monotonic timestamps for wait-for-vblank and page-flip
56  * complete events.
57  */
58 unsigned int drm_timestamp_monotonic = 1;
59
60 MODULE_AUTHOR(CORE_AUTHOR);
61 MODULE_DESCRIPTION(CORE_DESC);
62 MODULE_LICENSE("GPL and additional rights");
63 MODULE_PARM_DESC(debug, "Enable debug output");
64 MODULE_PARM_DESC(rnodes, "Enable experimental render nodes API");
65 MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs]");
66 MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]");
67 MODULE_PARM_DESC(timestamp_monotonic, "Use monotonic timestamps");
68
69 module_param_named(debug, drm_debug, int, 0600);
70 module_param_named(rnodes, drm_rnodes, int, 0600);
71 module_param_named(vblankoffdelay, drm_vblank_offdelay, int, 0600);
72 module_param_named(timestamp_precision_usec, drm_timestamp_precision, int, 0600);
73 module_param_named(timestamp_monotonic, drm_timestamp_monotonic, int, 0600);
74
75 struct idr drm_minors_idr;
76
77 struct class *drm_class;
78 struct dentry *drm_debugfs_root;
79
80 int drm_err(const char *func, const char *format, ...)
81 {
82         struct va_format vaf;
83         va_list args;
84         int r;
85
86         va_start(args, format);
87
88         vaf.fmt = format;
89         vaf.va = &args;
90
91         r = printk(KERN_ERR "[" DRM_NAME ":%s] *ERROR* %pV", func, &vaf);
92
93         va_end(args);
94
95         return r;
96 }
97 EXPORT_SYMBOL(drm_err);
98
99 void drm_ut_debug_printk(unsigned int request_level,
100                          const char *prefix,
101                          const char *function_name,
102                          const char *format, ...)
103 {
104         struct va_format vaf;
105         va_list args;
106
107         if (drm_debug & request_level) {
108                 va_start(args, format);
109                 vaf.fmt = format;
110                 vaf.va = &args;
111
112                 if (function_name)
113                         printk(KERN_DEBUG "[%s:%s], %pV", prefix,
114                                function_name, &vaf);
115                 else
116                         printk(KERN_DEBUG "%pV", &vaf);
117                 va_end(args);
118         }
119 }
120 EXPORT_SYMBOL(drm_ut_debug_printk);
121
122 static int drm_minor_get_id(struct drm_device *dev, int type)
123 {
124         int ret;
125         int base = 0, limit = 63;
126
127         if (type == DRM_MINOR_CONTROL) {
128                 base += 64;
129                 limit = base + 63;
130         } else if (type == DRM_MINOR_RENDER) {
131                 base += 128;
132                 limit = base + 63;
133         }
134
135         mutex_lock(&dev->struct_mutex);
136         ret = idr_alloc(&drm_minors_idr, NULL, base, limit, GFP_KERNEL);
137         mutex_unlock(&dev->struct_mutex);
138
139         return ret == -ENOSPC ? -EINVAL : ret;
140 }
141
142 struct drm_master *drm_master_create(struct drm_minor *minor)
143 {
144         struct drm_master *master;
145
146         master = kzalloc(sizeof(*master), GFP_KERNEL);
147         if (!master)
148                 return NULL;
149
150         kref_init(&master->refcount);
151         spin_lock_init(&master->lock.spinlock);
152         init_waitqueue_head(&master->lock.lock_queue);
153         drm_ht_create(&master->magiclist, DRM_MAGIC_HASH_ORDER);
154         INIT_LIST_HEAD(&master->magicfree);
155         master->minor = minor;
156
157         return master;
158 }
159
160 struct drm_master *drm_master_get(struct drm_master *master)
161 {
162         kref_get(&master->refcount);
163         return master;
164 }
165 EXPORT_SYMBOL(drm_master_get);
166
167 static void drm_master_destroy(struct kref *kref)
168 {
169         struct drm_master *master = container_of(kref, struct drm_master, refcount);
170         struct drm_magic_entry *pt, *next;
171         struct drm_device *dev = master->minor->dev;
172         struct drm_map_list *r_list, *list_temp;
173
174         mutex_lock(&dev->struct_mutex);
175         if (dev->driver->master_destroy)
176                 dev->driver->master_destroy(dev, master);
177
178         list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
179                 if (r_list->master == master) {
180                         drm_rmmap_locked(dev, r_list->map);
181                         r_list = NULL;
182                 }
183         }
184
185         if (master->unique) {
186                 kfree(master->unique);
187                 master->unique = NULL;
188                 master->unique_len = 0;
189         }
190
191         list_for_each_entry_safe(pt, next, &master->magicfree, head) {
192                 list_del(&pt->head);
193                 drm_ht_remove_item(&master->magiclist, &pt->hash_item);
194                 kfree(pt);
195         }
196
197         drm_ht_remove(&master->magiclist);
198
199         mutex_unlock(&dev->struct_mutex);
200         kfree(master);
201 }
202
203 void drm_master_put(struct drm_master **master)
204 {
205         kref_put(&(*master)->refcount, drm_master_destroy);
206         *master = NULL;
207 }
208 EXPORT_SYMBOL(drm_master_put);
209
210 int drm_setmaster_ioctl(struct drm_device *dev, void *data,
211                         struct drm_file *file_priv)
212 {
213         int ret = 0;
214
215         mutex_lock(&dev->master_mutex);
216         if (file_priv->is_master)
217                 goto out_unlock;
218
219         if (file_priv->minor->master) {
220                 ret = -EINVAL;
221                 goto out_unlock;
222         }
223
224         if (!file_priv->master) {
225                 ret = -EINVAL;
226                 goto out_unlock;
227         }
228
229         file_priv->minor->master = drm_master_get(file_priv->master);
230         file_priv->is_master = 1;
231         if (dev->driver->master_set) {
232                 ret = dev->driver->master_set(dev, file_priv, false);
233                 if (unlikely(ret != 0)) {
234                         file_priv->is_master = 0;
235                         drm_master_put(&file_priv->minor->master);
236                 }
237         }
238
239 out_unlock:
240         mutex_unlock(&dev->master_mutex);
241         return ret;
242 }
243
244 int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
245                          struct drm_file *file_priv)
246 {
247         int ret = -EINVAL;
248
249         mutex_lock(&dev->master_mutex);
250         if (!file_priv->is_master)
251                 goto out_unlock;
252
253         if (!file_priv->minor->master)
254                 goto out_unlock;
255
256         ret = 0;
257         if (dev->driver->master_drop)
258                 dev->driver->master_drop(dev, file_priv, false);
259         drm_master_put(&file_priv->minor->master);
260         file_priv->is_master = 0;
261
262 out_unlock:
263         mutex_unlock(&dev->master_mutex);
264         return ret;
265 }
266
267 static struct drm_minor **drm_minor_get_slot(struct drm_device *dev,
268                                              unsigned int type)
269 {
270         switch (type) {
271         case DRM_MINOR_LEGACY:
272                 return &dev->primary;
273         case DRM_MINOR_RENDER:
274                 return &dev->render;
275         case DRM_MINOR_CONTROL:
276                 return &dev->control;
277         default:
278                 return NULL;
279         }
280 }
281
282 static int drm_minor_alloc(struct drm_device *dev, unsigned int type)
283 {
284         struct drm_minor *minor;
285
286         minor = kzalloc(sizeof(*minor), GFP_KERNEL);
287         if (!minor)
288                 return -ENOMEM;
289
290         minor->type = type;
291         minor->dev = dev;
292
293         *drm_minor_get_slot(dev, type) = minor;
294         return 0;
295 }
296
297 /**
298  * drm_get_minor - Register DRM minor
299  * @dev: DRM device
300  * @type: Type of minor
301  *
302  * Register minor of given type.
303  * Caller must hold the global DRM mutex.
304  *
305  * RETURNS:
306  * 0 on success, negative error code on failure.
307  */
308 static int drm_get_minor(struct drm_device *dev, unsigned int type)
309 {
310         struct drm_minor *new_minor;
311         int ret;
312         int minor_id;
313
314         DRM_DEBUG("\n");
315
316         new_minor = *drm_minor_get_slot(dev, type);
317         if (!new_minor)
318                 return 0;
319
320         minor_id = drm_minor_get_id(dev, type);
321         if (minor_id < 0)
322                 return minor_id;
323
324         new_minor->device = MKDEV(DRM_MAJOR, minor_id);
325         new_minor->index = minor_id;
326
327         idr_replace(&drm_minors_idr, new_minor, minor_id);
328
329 #if defined(CONFIG_DEBUG_FS)
330         ret = drm_debugfs_init(new_minor, minor_id, drm_debugfs_root);
331         if (ret) {
332                 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
333                 goto err_mem;
334         }
335 #endif
336
337         ret = drm_sysfs_device_add(new_minor);
338         if (ret) {
339                 printk(KERN_ERR
340                        "DRM: Error sysfs_device_add.\n");
341                 goto err_debugfs;
342         }
343
344         DRM_DEBUG("new minor assigned %d\n", minor_id);
345         return 0;
346
347
348 err_debugfs:
349 #if defined(CONFIG_DEBUG_FS)
350         drm_debugfs_cleanup(new_minor);
351 err_mem:
352 #endif
353         idr_remove(&drm_minors_idr, minor_id);
354         return ret;
355 }
356
357 /**
358  * drm_unplug_minor - Unplug DRM minor
359  * @minor: Minor to unplug
360  *
361  * Unplugs the given DRM minor but keeps the object. So after this returns,
362  * minor->dev is still valid so existing open-files can still access it to get
363  * device information from their drm_file ojects.
364  * If the minor is already unplugged or if @minor is NULL, nothing is done.
365  * The global DRM mutex must be held by the caller.
366  */
367 static void drm_unplug_minor(struct drm_minor *minor)
368 {
369         if (!minor || !minor->kdev)
370                 return;
371
372 #if defined(CONFIG_DEBUG_FS)
373         drm_debugfs_cleanup(minor);
374 #endif
375
376         drm_sysfs_device_remove(minor);
377         idr_remove(&drm_minors_idr, minor->index);
378 }
379
380 /**
381  * drm_minor_acquire - Acquire a DRM minor
382  * @minor_id: Minor ID of the DRM-minor
383  *
384  * Looks up the given minor-ID and returns the respective DRM-minor object. The
385  * refence-count of the underlying device is increased so you must release this
386  * object with drm_minor_release().
387  *
388  * As long as you hold this minor, it is guaranteed that the object and the
389  * minor->dev pointer will stay valid! However, the device may get unplugged and
390  * unregistered while you hold the minor.
391  *
392  * Returns:
393  * Pointer to minor-object with increased device-refcount, or PTR_ERR on
394  * failure.
395  */
396 struct drm_minor *drm_minor_acquire(unsigned int minor_id)
397 {
398         struct drm_minor *minor;
399
400         minor = idr_find(&drm_minors_idr, minor_id);
401         if (!minor)
402                 return ERR_PTR(-ENODEV);
403
404         drm_dev_ref(minor->dev);
405         return minor;
406 }
407
408 /**
409  * drm_minor_release - Release DRM minor
410  * @minor: Pointer to DRM minor object
411  *
412  * Release a minor that was previously acquired via drm_minor_acquire().
413  */
414 void drm_minor_release(struct drm_minor *minor)
415 {
416         drm_dev_unref(minor->dev);
417 }
418
419 /**
420  * drm_put_minor - Destroy DRM minor
421  * @minor: Minor to destroy
422  *
423  * This calls drm_unplug_minor() on the given minor and then frees it. Nothing
424  * is done if @minor is NULL. It is fine to call this on already unplugged
425  * minors.
426  * The global DRM mutex must be held by the caller.
427  */
428 static void drm_put_minor(struct drm_minor *minor)
429 {
430         if (!minor)
431                 return;
432
433         DRM_DEBUG("release secondary minor %d\n", minor->index);
434
435         drm_unplug_minor(minor);
436         kfree(minor);
437 }
438
439 /**
440  * Called via drm_exit() at module unload time or when pci device is
441  * unplugged.
442  *
443  * Cleans up all DRM device, calling drm_lastclose().
444  *
445  */
446 void drm_put_dev(struct drm_device *dev)
447 {
448         DRM_DEBUG("\n");
449
450         if (!dev) {
451                 DRM_ERROR("cleanup called no dev\n");
452                 return;
453         }
454
455         drm_dev_unregister(dev);
456         drm_dev_unref(dev);
457 }
458 EXPORT_SYMBOL(drm_put_dev);
459
460 void drm_unplug_dev(struct drm_device *dev)
461 {
462         /* for a USB device */
463         if (drm_core_check_feature(dev, DRIVER_MODESET))
464                 drm_unplug_minor(dev->control);
465         if (dev->render)
466                 drm_unplug_minor(dev->render);
467         drm_unplug_minor(dev->primary);
468
469         mutex_lock(&drm_global_mutex);
470
471         drm_device_set_unplugged(dev);
472
473         if (dev->open_count == 0) {
474                 drm_put_dev(dev);
475         }
476         mutex_unlock(&drm_global_mutex);
477 }
478 EXPORT_SYMBOL(drm_unplug_dev);
479
480 /*
481  * DRM internal mount
482  * We want to be able to allocate our own "struct address_space" to control
483  * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
484  * stand-alone address_space objects, so we need an underlying inode. As there
485  * is no way to allocate an independent inode easily, we need a fake internal
486  * VFS mount-point.
487  *
488  * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
489  * frees it again. You are allowed to use iget() and iput() to get references to
490  * the inode. But each drm_fs_inode_new() call must be paired with exactly one
491  * drm_fs_inode_free() call (which does not have to be the last iput()).
492  * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
493  * between multiple inode-users. You could, technically, call
494  * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
495  * iput(), but this way you'd end up with a new vfsmount for each inode.
496  */
497
498 static int drm_fs_cnt;
499 static struct vfsmount *drm_fs_mnt;
500
501 static const struct dentry_operations drm_fs_dops = {
502         .d_dname        = simple_dname,
503 };
504
505 static const struct super_operations drm_fs_sops = {
506         .statfs         = simple_statfs,
507 };
508
509 static struct dentry *drm_fs_mount(struct file_system_type *fs_type, int flags,
510                                    const char *dev_name, void *data)
511 {
512         return mount_pseudo(fs_type,
513                             "drm:",
514                             &drm_fs_sops,
515                             &drm_fs_dops,
516                             0x010203ff);
517 }
518
519 static struct file_system_type drm_fs_type = {
520         .name           = "drm",
521         .owner          = THIS_MODULE,
522         .mount          = drm_fs_mount,
523         .kill_sb        = kill_anon_super,
524 };
525
526 static struct inode *drm_fs_inode_new(void)
527 {
528         struct inode *inode;
529         int r;
530
531         r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt);
532         if (r < 0) {
533                 DRM_ERROR("Cannot mount pseudo fs: %d\n", r);
534                 return ERR_PTR(r);
535         }
536
537         inode = alloc_anon_inode(drm_fs_mnt->mnt_sb);
538         if (IS_ERR(inode))
539                 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
540
541         return inode;
542 }
543
544 static void drm_fs_inode_free(struct inode *inode)
545 {
546         if (inode) {
547                 iput(inode);
548                 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
549         }
550 }
551
552 /**
553  * drm_dev_alloc - Allocate new drm device
554  * @driver: DRM driver to allocate device for
555  * @parent: Parent device object
556  *
557  * Allocate and initialize a new DRM device. No device registration is done.
558  * Call drm_dev_register() to advertice the device to user space and register it
559  * with other core subsystems.
560  *
561  * The initial ref-count of the object is 1. Use drm_dev_ref() and
562  * drm_dev_unref() to take and drop further ref-counts.
563  *
564  * RETURNS:
565  * Pointer to new DRM device, or NULL if out of memory.
566  */
567 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
568                                  struct device *parent)
569 {
570         struct drm_device *dev;
571         int ret;
572
573         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
574         if (!dev)
575                 return NULL;
576
577         kref_init(&dev->ref);
578         dev->dev = parent;
579         dev->driver = driver;
580
581         INIT_LIST_HEAD(&dev->filelist);
582         INIT_LIST_HEAD(&dev->ctxlist);
583         INIT_LIST_HEAD(&dev->vmalist);
584         INIT_LIST_HEAD(&dev->maplist);
585         INIT_LIST_HEAD(&dev->vblank_event_list);
586
587         spin_lock_init(&dev->count_lock);
588         spin_lock_init(&dev->event_lock);
589         mutex_init(&dev->struct_mutex);
590         mutex_init(&dev->ctxlist_mutex);
591         mutex_init(&dev->master_mutex);
592
593         if (drm_core_check_feature(dev, DRIVER_MODESET)) {
594                 ret = drm_minor_alloc(dev, DRM_MINOR_CONTROL);
595                 if (ret)
596                         goto err_minors;
597         }
598
599         if (drm_core_check_feature(dev, DRIVER_RENDER) && drm_rnodes) {
600                 ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
601                 if (ret)
602                         goto err_minors;
603         }
604         ret = drm_minor_alloc(dev, DRM_MINOR_LEGACY);
605         if (ret)
606                 goto err_minors;
607
608         dev->anon_inode = drm_fs_inode_new();
609         if (IS_ERR(dev->anon_inode)) {
610                 ret = PTR_ERR(dev->anon_inode);
611                 DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
612                 goto err_minors;
613         }
614
615         if (drm_ht_create(&dev->map_hash, 12))
616                 goto err_inode;
617
618         ret = drm_ctxbitmap_init(dev);
619         if (ret) {
620                 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
621                 goto err_ht;
622         }
623
624         if (driver->driver_features & DRIVER_GEM) {
625                 ret = drm_gem_init(dev);
626                 if (ret) {
627                         DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
628                         goto err_ctxbitmap;
629                 }
630         }
631
632         return dev;
633
634 err_ctxbitmap:
635         drm_ctxbitmap_cleanup(dev);
636 err_ht:
637         drm_ht_remove(&dev->map_hash);
638 err_inode:
639         drm_fs_inode_free(dev->anon_inode);
640 err_minors:
641         drm_put_minor(dev->control);
642         drm_put_minor(dev->render);
643         drm_put_minor(dev->primary);
644         mutex_destroy(&dev->master_mutex);
645         kfree(dev);
646         return NULL;
647 }
648 EXPORT_SYMBOL(drm_dev_alloc);
649
650 static void drm_dev_release(struct kref *ref)
651 {
652         struct drm_device *dev = container_of(ref, struct drm_device, ref);
653
654         drm_put_minor(dev->control);
655         drm_put_minor(dev->render);
656         drm_put_minor(dev->primary);
657
658         if (dev->driver->driver_features & DRIVER_GEM)
659                 drm_gem_destroy(dev);
660
661         drm_ctxbitmap_cleanup(dev);
662         drm_ht_remove(&dev->map_hash);
663         drm_fs_inode_free(dev->anon_inode);
664
665         mutex_destroy(&dev->master_mutex);
666         kfree(dev->unique);
667         kfree(dev);
668 }
669
670 /**
671  * drm_dev_ref - Take reference of a DRM device
672  * @dev: device to take reference of or NULL
673  *
674  * This increases the ref-count of @dev by one. You *must* already own a
675  * reference when calling this. Use drm_dev_unref() to drop this reference
676  * again.
677  *
678  * This function never fails. However, this function does not provide *any*
679  * guarantee whether the device is alive or running. It only provides a
680  * reference to the object and the memory associated with it.
681  */
682 void drm_dev_ref(struct drm_device *dev)
683 {
684         if (dev)
685                 kref_get(&dev->ref);
686 }
687 EXPORT_SYMBOL(drm_dev_ref);
688
689 /**
690  * drm_dev_unref - Drop reference of a DRM device
691  * @dev: device to drop reference of or NULL
692  *
693  * This decreases the ref-count of @dev by one. The device is destroyed if the
694  * ref-count drops to zero.
695  */
696 void drm_dev_unref(struct drm_device *dev)
697 {
698         if (dev)
699                 kref_put(&dev->ref, drm_dev_release);
700 }
701 EXPORT_SYMBOL(drm_dev_unref);
702
703 /**
704  * drm_dev_register - Register DRM device
705  * @dev: Device to register
706  *
707  * Register the DRM device @dev with the system, advertise device to user-space
708  * and start normal device operation. @dev must be allocated via drm_dev_alloc()
709  * previously.
710  *
711  * Never call this twice on any device!
712  *
713  * RETURNS:
714  * 0 on success, negative error code on failure.
715  */
716 int drm_dev_register(struct drm_device *dev, unsigned long flags)
717 {
718         int ret;
719
720         mutex_lock(&drm_global_mutex);
721
722         ret = drm_get_minor(dev, DRM_MINOR_CONTROL);
723         if (ret)
724                 goto err_minors;
725
726         ret = drm_get_minor(dev, DRM_MINOR_RENDER);
727         if (ret)
728                 goto err_minors;
729
730         ret = drm_get_minor(dev, DRM_MINOR_LEGACY);
731         if (ret)
732                 goto err_minors;
733
734         if (dev->driver->load) {
735                 ret = dev->driver->load(dev, flags);
736                 if (ret)
737                         goto err_minors;
738         }
739
740         /* setup grouping for legacy outputs */
741         if (drm_core_check_feature(dev, DRIVER_MODESET)) {
742                 ret = drm_mode_group_init_legacy_group(dev,
743                                 &dev->primary->mode_group);
744                 if (ret)
745                         goto err_unload;
746         }
747
748         ret = 0;
749         goto out_unlock;
750
751 err_unload:
752         if (dev->driver->unload)
753                 dev->driver->unload(dev);
754 err_minors:
755         drm_unplug_minor(dev->control);
756         drm_unplug_minor(dev->render);
757         drm_unplug_minor(dev->primary);
758 out_unlock:
759         mutex_unlock(&drm_global_mutex);
760         return ret;
761 }
762 EXPORT_SYMBOL(drm_dev_register);
763
764 /**
765  * drm_dev_unregister - Unregister DRM device
766  * @dev: Device to unregister
767  *
768  * Unregister the DRM device from the system. This does the reverse of
769  * drm_dev_register() but does not deallocate the device. The caller must call
770  * drm_dev_unref() to drop their final reference.
771  */
772 void drm_dev_unregister(struct drm_device *dev)
773 {
774         struct drm_map_list *r_list, *list_temp;
775
776         drm_lastclose(dev);
777
778         if (dev->driver->unload)
779                 dev->driver->unload(dev);
780
781         if (dev->agp)
782                 drm_pci_agp_destroy(dev);
783
784         drm_vblank_cleanup(dev);
785
786         list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
787                 drm_rmmap(dev, r_list->map);
788
789         drm_unplug_minor(dev->control);
790         drm_unplug_minor(dev->render);
791         drm_unplug_minor(dev->primary);
792 }
793 EXPORT_SYMBOL(drm_dev_unregister);
794
795 /**
796  * drm_dev_set_unique - Set the unique name of a DRM device
797  * @dev: device of which to set the unique name
798  * @fmt: format string for unique name
799  *
800  * Sets the unique name of a DRM device using the specified format string and
801  * a variable list of arguments. Drivers can use this at driver probe time if
802  * the unique name of the devices they drive is static.
803  *
804  * Return: 0 on success or a negative error code on failure.
805  */
806 int drm_dev_set_unique(struct drm_device *dev, const char *fmt, ...)
807 {
808         va_list ap;
809
810         kfree(dev->unique);
811
812         va_start(ap, fmt);
813         dev->unique = kvasprintf(GFP_KERNEL, fmt, ap);
814         va_end(ap);
815
816         return dev->unique ? 0 : -ENOMEM;
817 }
818 EXPORT_SYMBOL(drm_dev_set_unique);