drm: Protect the master management with a drm_device::master_mutex v3
[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/module.h>
35 #include <linux/moduleparam.h>
36 #include <linux/slab.h>
37 #include <drm/drmP.h>
38 #include <drm/drm_core.h>
39
40 unsigned int drm_debug = 0;     /* 1 to enable debug output */
41 EXPORT_SYMBOL(drm_debug);
42
43 unsigned int drm_rnodes = 0;    /* 1 to enable experimental render nodes API */
44 EXPORT_SYMBOL(drm_rnodes);
45
46 unsigned int drm_vblank_offdelay = 5000;    /* Default to 5000 msecs. */
47 EXPORT_SYMBOL(drm_vblank_offdelay);
48
49 unsigned int drm_timestamp_precision = 20;  /* Default to 20 usecs. */
50 EXPORT_SYMBOL(drm_timestamp_precision);
51
52 /*
53  * Default to use monotonic timestamps for wait-for-vblank and page-flip
54  * complete events.
55  */
56 unsigned int drm_timestamp_monotonic = 1;
57
58 MODULE_AUTHOR(CORE_AUTHOR);
59 MODULE_DESCRIPTION(CORE_DESC);
60 MODULE_LICENSE("GPL and additional rights");
61 MODULE_PARM_DESC(debug, "Enable debug output");
62 MODULE_PARM_DESC(rnodes, "Enable experimental render nodes API");
63 MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs]");
64 MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]");
65 MODULE_PARM_DESC(timestamp_monotonic, "Use monotonic timestamps");
66
67 module_param_named(debug, drm_debug, int, 0600);
68 module_param_named(rnodes, drm_rnodes, int, 0600);
69 module_param_named(vblankoffdelay, drm_vblank_offdelay, int, 0600);
70 module_param_named(timestamp_precision_usec, drm_timestamp_precision, int, 0600);
71 module_param_named(timestamp_monotonic, drm_timestamp_monotonic, int, 0600);
72
73 struct idr drm_minors_idr;
74
75 struct class *drm_class;
76 struct dentry *drm_debugfs_root;
77
78 int drm_err(const char *func, const char *format, ...)
79 {
80         struct va_format vaf;
81         va_list args;
82         int r;
83
84         va_start(args, format);
85
86         vaf.fmt = format;
87         vaf.va = &args;
88
89         r = printk(KERN_ERR "[" DRM_NAME ":%s] *ERROR* %pV", func, &vaf);
90
91         va_end(args);
92
93         return r;
94 }
95 EXPORT_SYMBOL(drm_err);
96
97 void drm_ut_debug_printk(unsigned int request_level,
98                          const char *prefix,
99                          const char *function_name,
100                          const char *format, ...)
101 {
102         struct va_format vaf;
103         va_list args;
104
105         if (drm_debug & request_level) {
106                 va_start(args, format);
107                 vaf.fmt = format;
108                 vaf.va = &args;
109
110                 if (function_name)
111                         printk(KERN_DEBUG "[%s:%s], %pV", prefix,
112                                function_name, &vaf);
113                 else
114                         printk(KERN_DEBUG "%pV", &vaf);
115                 va_end(args);
116         }
117 }
118 EXPORT_SYMBOL(drm_ut_debug_printk);
119
120 static int drm_minor_get_id(struct drm_device *dev, int type)
121 {
122         int ret;
123         int base = 0, limit = 63;
124
125         if (type == DRM_MINOR_CONTROL) {
126                 base += 64;
127                 limit = base + 63;
128         } else if (type == DRM_MINOR_RENDER) {
129                 base += 128;
130                 limit = base + 63;
131         }
132
133         mutex_lock(&dev->struct_mutex);
134         ret = idr_alloc(&drm_minors_idr, NULL, base, limit, GFP_KERNEL);
135         mutex_unlock(&dev->struct_mutex);
136
137         return ret == -ENOSPC ? -EINVAL : ret;
138 }
139
140 struct drm_master *drm_master_create(struct drm_minor *minor)
141 {
142         struct drm_master *master;
143
144         master = kzalloc(sizeof(*master), GFP_KERNEL);
145         if (!master)
146                 return NULL;
147
148         kref_init(&master->refcount);
149         spin_lock_init(&master->lock.spinlock);
150         init_waitqueue_head(&master->lock.lock_queue);
151         drm_ht_create(&master->magiclist, DRM_MAGIC_HASH_ORDER);
152         INIT_LIST_HEAD(&master->magicfree);
153         master->minor = minor;
154
155         return master;
156 }
157
158 struct drm_master *drm_master_get(struct drm_master *master)
159 {
160         kref_get(&master->refcount);
161         return master;
162 }
163 EXPORT_SYMBOL(drm_master_get);
164
165 static void drm_master_destroy(struct kref *kref)
166 {
167         struct drm_master *master = container_of(kref, struct drm_master, refcount);
168         struct drm_magic_entry *pt, *next;
169         struct drm_device *dev = master->minor->dev;
170         struct drm_map_list *r_list, *list_temp;
171
172         mutex_lock(&dev->struct_mutex);
173         if (dev->driver->master_destroy)
174                 dev->driver->master_destroy(dev, master);
175
176         list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
177                 if (r_list->master == master) {
178                         drm_rmmap_locked(dev, r_list->map);
179                         r_list = NULL;
180                 }
181         }
182
183         if (master->unique) {
184                 kfree(master->unique);
185                 master->unique = NULL;
186                 master->unique_len = 0;
187         }
188
189         kfree(dev->devname);
190         dev->devname = NULL;
191
192         list_for_each_entry_safe(pt, next, &master->magicfree, head) {
193                 list_del(&pt->head);
194                 drm_ht_remove_item(&master->magiclist, &pt->hash_item);
195                 kfree(pt);
196         }
197
198         drm_ht_remove(&master->magiclist);
199
200         mutex_unlock(&dev->struct_mutex);
201         kfree(master);
202 }
203
204 void drm_master_put(struct drm_master **master)
205 {
206         kref_put(&(*master)->refcount, drm_master_destroy);
207         *master = NULL;
208 }
209 EXPORT_SYMBOL(drm_master_put);
210
211 int drm_setmaster_ioctl(struct drm_device *dev, void *data,
212                         struct drm_file *file_priv)
213 {
214         int ret = 0;
215
216         mutex_lock(&dev->master_mutex);
217         if (file_priv->is_master)
218                 goto out_unlock;
219
220         if (file_priv->minor->master) {
221                 ret = -EINVAL;
222                 goto out_unlock;
223         }
224
225         if (!file_priv->master) {
226                 ret = -EINVAL;
227                 goto out_unlock;
228         }
229
230         file_priv->minor->master = drm_master_get(file_priv->master);
231         file_priv->is_master = 1;
232         if (dev->driver->master_set) {
233                 ret = dev->driver->master_set(dev, file_priv, false);
234                 if (unlikely(ret != 0)) {
235                         file_priv->is_master = 0;
236                         drm_master_put(&file_priv->minor->master);
237                 }
238         }
239
240 out_unlock:
241         mutex_unlock(&dev->master_mutex);
242         return ret;
243 }
244
245 int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
246                          struct drm_file *file_priv)
247 {
248         int ret = -EINVAL;
249
250         mutex_lock(&dev->master_mutex);
251         if (!file_priv->is_master)
252                 goto out_unlock;
253
254         if (!file_priv->minor->master)
255                 goto out_unlock;
256
257         ret = 0;
258         if (dev->driver->master_drop)
259                 dev->driver->master_drop(dev, file_priv, false);
260         drm_master_put(&file_priv->minor->master);
261         file_priv->is_master = 0;
262
263 out_unlock:
264         mutex_unlock(&dev->master_mutex);
265         return ret;
266 }
267
268 static struct drm_minor **drm_minor_get_slot(struct drm_device *dev,
269                                              unsigned int type)
270 {
271         switch (type) {
272         case DRM_MINOR_LEGACY:
273                 return &dev->primary;
274         case DRM_MINOR_RENDER:
275                 return &dev->render;
276         case DRM_MINOR_CONTROL:
277                 return &dev->control;
278         default:
279                 return NULL;
280         }
281 }
282
283 static int drm_minor_alloc(struct drm_device *dev, unsigned int type)
284 {
285         struct drm_minor *minor;
286
287         minor = kzalloc(sizeof(*minor), GFP_KERNEL);
288         if (!minor)
289                 return -ENOMEM;
290
291         minor->type = type;
292         minor->dev = dev;
293
294         *drm_minor_get_slot(dev, type) = minor;
295         return 0;
296 }
297
298 /**
299  * drm_get_minor - Register DRM minor
300  * @dev: DRM device
301  * @type: Type of minor
302  *
303  * Register minor of given type.
304  * Caller must hold the global DRM mutex.
305  *
306  * RETURNS:
307  * 0 on success, negative error code on failure.
308  */
309 static int drm_get_minor(struct drm_device *dev, unsigned int type)
310 {
311         struct drm_minor *new_minor;
312         int ret;
313         int minor_id;
314
315         DRM_DEBUG("\n");
316
317         new_minor = *drm_minor_get_slot(dev, type);
318         if (!new_minor)
319                 return 0;
320
321         minor_id = drm_minor_get_id(dev, type);
322         if (minor_id < 0)
323                 return minor_id;
324
325         new_minor->device = MKDEV(DRM_MAJOR, minor_id);
326         new_minor->index = minor_id;
327
328         idr_replace(&drm_minors_idr, new_minor, minor_id);
329
330 #if defined(CONFIG_DEBUG_FS)
331         ret = drm_debugfs_init(new_minor, minor_id, drm_debugfs_root);
332         if (ret) {
333                 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
334                 goto err_mem;
335         }
336 #endif
337
338         ret = drm_sysfs_device_add(new_minor);
339         if (ret) {
340                 printk(KERN_ERR
341                        "DRM: Error sysfs_device_add.\n");
342                 goto err_debugfs;
343         }
344
345         DRM_DEBUG("new minor assigned %d\n", minor_id);
346         return 0;
347
348
349 err_debugfs:
350 #if defined(CONFIG_DEBUG_FS)
351         drm_debugfs_cleanup(new_minor);
352 err_mem:
353 #endif
354         idr_remove(&drm_minors_idr, minor_id);
355         return ret;
356 }
357
358 /**
359  * drm_unplug_minor - Unplug DRM minor
360  * @minor: Minor to unplug
361  *
362  * Unplugs the given DRM minor but keeps the object. So after this returns,
363  * minor->dev is still valid so existing open-files can still access it to get
364  * device information from their drm_file ojects.
365  * If the minor is already unplugged or if @minor is NULL, nothing is done.
366  * The global DRM mutex must be held by the caller.
367  */
368 static void drm_unplug_minor(struct drm_minor *minor)
369 {
370         if (!minor || !minor->kdev)
371                 return;
372
373 #if defined(CONFIG_DEBUG_FS)
374         drm_debugfs_cleanup(minor);
375 #endif
376
377         drm_sysfs_device_remove(minor);
378         idr_remove(&drm_minors_idr, minor->index);
379 }
380
381 /**
382  * drm_minor_acquire - Acquire a DRM minor
383  * @minor_id: Minor ID of the DRM-minor
384  *
385  * Looks up the given minor-ID and returns the respective DRM-minor object. The
386  * refence-count of the underlying device is increased so you must release this
387  * object with drm_minor_release().
388  *
389  * As long as you hold this minor, it is guaranteed that the object and the
390  * minor->dev pointer will stay valid! However, the device may get unplugged and
391  * unregistered while you hold the minor.
392  *
393  * Returns:
394  * Pointer to minor-object with increased device-refcount, or PTR_ERR on
395  * failure.
396  */
397 struct drm_minor *drm_minor_acquire(unsigned int minor_id)
398 {
399         struct drm_minor *minor;
400
401         minor = idr_find(&drm_minors_idr, minor_id);
402         if (!minor)
403                 return ERR_PTR(-ENODEV);
404
405         drm_dev_ref(minor->dev);
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  * drm_put_minor - Destroy DRM minor
422  * @minor: Minor to destroy
423  *
424  * This calls drm_unplug_minor() on the given minor and then frees it. Nothing
425  * is done if @minor is NULL. It is fine to call this on already unplugged
426  * minors.
427  * The global DRM mutex must be held by the caller.
428  */
429 static void drm_put_minor(struct drm_minor *minor)
430 {
431         if (!minor)
432                 return;
433
434         DRM_DEBUG("release secondary minor %d\n", minor->index);
435
436         drm_unplug_minor(minor);
437         kfree(minor);
438 }
439
440 /**
441  * Called via drm_exit() at module unload time or when pci device is
442  * unplugged.
443  *
444  * Cleans up all DRM device, calling drm_lastclose().
445  *
446  */
447 void drm_put_dev(struct drm_device *dev)
448 {
449         DRM_DEBUG("\n");
450
451         if (!dev) {
452                 DRM_ERROR("cleanup called no dev\n");
453                 return;
454         }
455
456         drm_dev_unregister(dev);
457         drm_dev_free(dev);
458 }
459 EXPORT_SYMBOL(drm_put_dev);
460
461 void drm_unplug_dev(struct drm_device *dev)
462 {
463         /* for a USB device */
464         if (drm_core_check_feature(dev, DRIVER_MODESET))
465                 drm_unplug_minor(dev->control);
466         if (dev->render)
467                 drm_unplug_minor(dev->render);
468         drm_unplug_minor(dev->primary);
469
470         mutex_lock(&drm_global_mutex);
471
472         drm_device_set_unplugged(dev);
473
474         if (dev->open_count == 0) {
475                 drm_put_dev(dev);
476         }
477         mutex_unlock(&drm_global_mutex);
478 }
479 EXPORT_SYMBOL(drm_unplug_dev);
480
481 /**
482  * drm_dev_alloc - Allocate new drm device
483  * @driver: DRM driver to allocate device for
484  * @parent: Parent device object
485  *
486  * Allocate and initialize a new DRM device. No device registration is done.
487  * Call drm_dev_register() to advertice the device to user space and register it
488  * with other core subsystems.
489  *
490  * RETURNS:
491  * Pointer to new DRM device, or NULL if out of memory.
492  */
493 struct drm_device *drm_dev_alloc(struct drm_driver *driver,
494                                  struct device *parent)
495 {
496         struct drm_device *dev;
497         int ret;
498
499         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
500         if (!dev)
501                 return NULL;
502
503         dev->dev = parent;
504         dev->driver = driver;
505
506         INIT_LIST_HEAD(&dev->filelist);
507         INIT_LIST_HEAD(&dev->ctxlist);
508         INIT_LIST_HEAD(&dev->vmalist);
509         INIT_LIST_HEAD(&dev->maplist);
510         INIT_LIST_HEAD(&dev->vblank_event_list);
511
512         spin_lock_init(&dev->count_lock);
513         spin_lock_init(&dev->event_lock);
514         mutex_init(&dev->struct_mutex);
515         mutex_init(&dev->ctxlist_mutex);
516         mutex_init(&dev->master_mutex);
517
518         if (drm_core_check_feature(dev, DRIVER_MODESET)) {
519                 ret = drm_minor_alloc(dev, DRM_MINOR_CONTROL);
520                 if (ret)
521                         goto err_minors;
522         }
523
524         if (drm_core_check_feature(dev, DRIVER_RENDER) && drm_rnodes) {
525                 ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
526                 if (ret)
527                         goto err_minors;
528         }
529         ret = drm_minor_alloc(dev, DRM_MINOR_LEGACY);
530         if (ret)
531                 goto err_minors;
532
533         dev->anon_inode = drm_fs_inode_new();
534         if (IS_ERR(dev->anon_inode)) {
535                 ret = PTR_ERR(dev->anon_inode);
536                 DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
537                 goto err_minors;
538         }
539
540         if (drm_ht_create(&dev->map_hash, 12))
541                 goto err_inode;
542
543         ret = drm_ctxbitmap_init(dev);
544         if (ret) {
545                 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
546                 goto err_ht;
547         }
548
549         if (driver->driver_features & DRIVER_GEM) {
550                 ret = drm_gem_init(dev);
551                 if (ret) {
552                         DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
553                         goto err_ctxbitmap;
554                 }
555         }
556
557         return dev;
558
559 err_ctxbitmap:
560         drm_ctxbitmap_cleanup(dev);
561 err_ht:
562         drm_ht_remove(&dev->map_hash);
563 err_inode:
564         drm_fs_inode_free(dev->anon_inode);
565 err_minors:
566         drm_put_minor(dev->control);
567         drm_put_minor(dev->render);
568         drm_put_minor(dev->primary);
569         mutex_destroy(&dev->master_mutex);
570         kfree(dev);
571         return NULL;
572 }
573 EXPORT_SYMBOL(drm_dev_alloc);
574
575 /**
576  * drm_dev_free - Free DRM device
577  * @dev: DRM device to free
578  *
579  * Free a DRM device that has previously been allocated via drm_dev_alloc().
580  * You must not use kfree() instead or you will leak memory.
581  *
582  * This must not be called once the device got registered. Use drm_put_dev()
583  * instead, which then calls drm_dev_free().
584  */
585 void drm_dev_free(struct drm_device *dev)
586 {
587         drm_put_minor(dev->control);
588         drm_put_minor(dev->render);
589         drm_put_minor(dev->primary);
590
591         if (dev->driver->driver_features & DRIVER_GEM)
592                 drm_gem_destroy(dev);
593
594         drm_ctxbitmap_cleanup(dev);
595         drm_ht_remove(&dev->map_hash);
596         drm_fs_inode_free(dev->anon_inode);
597
598         kfree(dev->devname);
599
600         mutex_destroy(&dev->master_mutex);
601         kfree(dev);
602 }
603 EXPORT_SYMBOL(drm_dev_free);
604
605 /**
606  * drm_dev_register - Register DRM device
607  * @dev: Device to register
608  *
609  * Register the DRM device @dev with the system, advertise device to user-space
610  * and start normal device operation. @dev must be allocated via drm_dev_alloc()
611  * previously.
612  *
613  * Never call this twice on any device!
614  *
615  * RETURNS:
616  * 0 on success, negative error code on failure.
617  */
618 int drm_dev_register(struct drm_device *dev, unsigned long flags)
619 {
620         int ret;
621
622         mutex_lock(&drm_global_mutex);
623
624         ret = drm_get_minor(dev, DRM_MINOR_CONTROL);
625         if (ret)
626                 goto err_minors;
627
628         ret = drm_get_minor(dev, DRM_MINOR_RENDER);
629         if (ret)
630                 goto err_minors;
631
632         ret = drm_get_minor(dev, DRM_MINOR_LEGACY);
633         if (ret)
634                 goto err_minors;
635
636         if (dev->driver->load) {
637                 ret = dev->driver->load(dev, flags);
638                 if (ret)
639                         goto err_minors;
640         }
641
642         /* setup grouping for legacy outputs */
643         if (drm_core_check_feature(dev, DRIVER_MODESET)) {
644                 ret = drm_mode_group_init_legacy_group(dev,
645                                 &dev->primary->mode_group);
646                 if (ret)
647                         goto err_unload;
648         }
649
650         ret = 0;
651         goto out_unlock;
652
653 err_unload:
654         if (dev->driver->unload)
655                 dev->driver->unload(dev);
656 err_minors:
657         drm_unplug_minor(dev->control);
658         drm_unplug_minor(dev->render);
659         drm_unplug_minor(dev->primary);
660 out_unlock:
661         mutex_unlock(&drm_global_mutex);
662         return ret;
663 }
664 EXPORT_SYMBOL(drm_dev_register);
665
666 /**
667  * drm_dev_unregister - Unregister DRM device
668  * @dev: Device to unregister
669  *
670  * Unregister the DRM device from the system. This does the reverse of
671  * drm_dev_register() but does not deallocate the device. The caller must call
672  * drm_dev_free() to free all resources.
673  */
674 void drm_dev_unregister(struct drm_device *dev)
675 {
676         struct drm_map_list *r_list, *list_temp;
677
678         drm_lastclose(dev);
679
680         if (dev->driver->unload)
681                 dev->driver->unload(dev);
682
683         if (dev->agp)
684                 drm_pci_agp_destroy(dev);
685
686         drm_vblank_cleanup(dev);
687
688         list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
689                 drm_rmmap(dev, r_list->map);
690
691         drm_unplug_minor(dev->control);
692         drm_unplug_minor(dev->render);
693         drm_unplug_minor(dev->primary);
694 }
695 EXPORT_SYMBOL(drm_dev_unregister);