drm: drm_ut_debug_printk() isn't called with NULL anywmore
[deliverable/linux.git] / drivers / gpu / drm / drm_stub.c
CommitLineData
1da177e4
LT
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
31bbe16f 34#include <linux/fs.h>
1da177e4
LT
35#include <linux/module.h>
36#include <linux/moduleparam.h>
31bbe16f 37#include <linux/mount.h>
5a0e3ad6 38#include <linux/slab.h>
760285e7
DH
39#include <drm/drmP.h>
40#include <drm/drm_core.h>
1da177e4 41
b5e89ed5 42unsigned int drm_debug = 0; /* 1 to enable debug output */
1da177e4
LT
43EXPORT_SYMBOL(drm_debug);
44
1793126f
DH
45unsigned int drm_rnodes = 0; /* 1 to enable experimental render nodes API */
46EXPORT_SYMBOL(drm_rnodes);
47
27641c3f
MK
48unsigned int drm_vblank_offdelay = 5000; /* Default to 5000 msecs. */
49EXPORT_SYMBOL(drm_vblank_offdelay);
50
51unsigned int drm_timestamp_precision = 20; /* Default to 20 usecs. */
52EXPORT_SYMBOL(drm_timestamp_precision);
53
c61eef72
ID
54/*
55 * Default to use monotonic timestamps for wait-for-vblank and page-flip
56 * complete events.
57 */
58unsigned int drm_timestamp_monotonic = 1;
59
b5e89ed5
DA
60MODULE_AUTHOR(CORE_AUTHOR);
61MODULE_DESCRIPTION(CORE_DESC);
1da177e4 62MODULE_LICENSE("GPL and additional rights");
1da177e4 63MODULE_PARM_DESC(debug, "Enable debug output");
1793126f 64MODULE_PARM_DESC(rnodes, "Enable experimental render nodes API");
27641c3f
MK
65MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs]");
66MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]");
c61eef72 67MODULE_PARM_DESC(timestamp_monotonic, "Use monotonic timestamps");
1da177e4 68
c0758146 69module_param_named(debug, drm_debug, int, 0600);
1793126f 70module_param_named(rnodes, drm_rnodes, int, 0600);
27641c3f
MK
71module_param_named(vblankoffdelay, drm_vblank_offdelay, int, 0600);
72module_param_named(timestamp_precision_usec, drm_timestamp_precision, int, 0600);
c61eef72 73module_param_named(timestamp_monotonic, drm_timestamp_monotonic, int, 0600);
1da177e4 74
0d639883 75static DEFINE_SPINLOCK(drm_minor_lock);
2c14f28b
DA
76struct idr drm_minors_idr;
77
0650fd58 78struct class *drm_class;
955b12de 79struct dentry *drm_debugfs_root;
5ad3d883
JP
80
81int drm_err(const char *func, const char *format, ...)
82{
83 struct va_format vaf;
84 va_list args;
85 int r;
86
87 va_start(args, format);
88
89 vaf.fmt = format;
90 vaf.va = &args;
91
92 r = printk(KERN_ERR "[" DRM_NAME ":%s] *ERROR* %pV", func, &vaf);
93
94 va_end(args);
95
96 return r;
97}
98EXPORT_SYMBOL(drm_err);
99
a73d4e91 100void drm_ut_debug_printk(const char *prefix,
4fefcb27 101 const char *function_name,
102 const char *format, ...)
103{
fffb9065 104 struct va_format vaf;
4fefcb27 105 va_list args;
1da177e4 106
a73d4e91
LD
107 va_start(args, format);
108 vaf.fmt = format;
109 vaf.va = &args;
110
1288c19f 111 printk(KERN_DEBUG "[%s:%s], %pV", prefix, function_name, &vaf);
a73d4e91
LD
112
113 va_end(args);
4fefcb27 114}
115EXPORT_SYMBOL(drm_ut_debug_printk);
5ad3d883 116
7c1c2871
DA
117struct drm_master *drm_master_create(struct drm_minor *minor)
118{
119 struct drm_master *master;
120
9a298b2a 121 master = kzalloc(sizeof(*master), GFP_KERNEL);
7c1c2871
DA
122 if (!master)
123 return NULL;
124
125 kref_init(&master->refcount);
126 spin_lock_init(&master->lock.spinlock);
127 init_waitqueue_head(&master->lock.lock_queue);
128 drm_ht_create(&master->magiclist, DRM_MAGIC_HASH_ORDER);
129 INIT_LIST_HEAD(&master->magicfree);
130 master->minor = minor;
131
132 list_add_tail(&master->head, &minor->master_list);
133
134 return master;
135}
136
137struct drm_master *drm_master_get(struct drm_master *master)
138{
139 kref_get(&master->refcount);
140 return master;
141}
85bb0c37 142EXPORT_SYMBOL(drm_master_get);
7c1c2871
DA
143
144static void drm_master_destroy(struct kref *kref)
145{
146 struct drm_master *master = container_of(kref, struct drm_master, refcount);
147 struct drm_magic_entry *pt, *next;
148 struct drm_device *dev = master->minor->dev;
c1ff85d9 149 struct drm_map_list *r_list, *list_temp;
7c1c2871
DA
150
151 list_del(&master->head);
152
153 if (dev->driver->master_destroy)
154 dev->driver->master_destroy(dev, master);
155
c1ff85d9
DA
156 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head) {
157 if (r_list->master == master) {
158 drm_rmmap_locked(dev, r_list->map);
159 r_list = NULL;
160 }
161 }
162
7c1c2871 163 if (master->unique) {
9a298b2a 164 kfree(master->unique);
7c1c2871
DA
165 master->unique = NULL;
166 master->unique_len = 0;
167 }
168
6e35023f
CW
169 kfree(dev->devname);
170 dev->devname = NULL;
171
7c1c2871
DA
172 list_for_each_entry_safe(pt, next, &master->magicfree, head) {
173 list_del(&pt->head);
174 drm_ht_remove_item(&master->magiclist, &pt->hash_item);
9a298b2a 175 kfree(pt);
7c1c2871
DA
176 }
177
178 drm_ht_remove(&master->magiclist);
179
9a298b2a 180 kfree(master);
7c1c2871
DA
181}
182
183void drm_master_put(struct drm_master **master)
184{
185 kref_put(&(*master)->refcount, drm_master_destroy);
186 *master = NULL;
187}
85bb0c37 188EXPORT_SYMBOL(drm_master_put);
7c1c2871
DA
189
190int drm_setmaster_ioctl(struct drm_device *dev, void *data,
191 struct drm_file *file_priv)
192{
53ef1600 193 int ret = 0;
862302ff 194
6b008426
JB
195 if (file_priv->is_master)
196 return 0;
197
7c1c2871
DA
198 if (file_priv->minor->master && file_priv->minor->master != file_priv->master)
199 return -EINVAL;
200
201 if (!file_priv->master)
202 return -EINVAL;
203
08bec5b4
DH
204 if (file_priv->minor->master)
205 return -EINVAL;
206
207 mutex_lock(&dev->struct_mutex);
208 file_priv->minor->master = drm_master_get(file_priv->master);
209 file_priv->is_master = 1;
210 if (dev->driver->master_set) {
211 ret = dev->driver->master_set(dev, file_priv, false);
212 if (unlikely(ret != 0)) {
213 file_priv->is_master = 0;
214 drm_master_put(&file_priv->minor->master);
862302ff 215 }
7c1c2871 216 }
08bec5b4 217 mutex_unlock(&dev->struct_mutex);
7c1c2871 218
53ef1600 219 return ret;
7c1c2871
DA
220}
221
222int drm_dropmaster_ioctl(struct drm_device *dev, void *data,
223 struct drm_file *file_priv)
224{
6b008426 225 if (!file_priv->is_master)
7c1c2871 226 return -EINVAL;
6b008426 227
07f1c7a7
DA
228 if (!file_priv->minor->master)
229 return -EINVAL;
230
7c1c2871 231 mutex_lock(&dev->struct_mutex);
862302ff
TH
232 if (dev->driver->master_drop)
233 dev->driver->master_drop(dev, file_priv, false);
7c1c2871 234 drm_master_put(&file_priv->minor->master);
6b008426 235 file_priv->is_master = 0;
7c1c2871
DA
236 mutex_unlock(&dev->struct_mutex);
237 return 0;
238}
239
0d639883
DH
240/*
241 * DRM Minors
242 * A DRM device can provide several char-dev interfaces on the DRM-Major. Each
243 * of them is represented by a drm_minor object. Depending on the capabilities
244 * of the device-driver, different interfaces are registered.
1da177e4 245 *
0d639883
DH
246 * Minors can be accessed via dev->$minor_name. This pointer is either
247 * NULL or a valid drm_minor pointer and stays valid as long as the device is
248 * valid. This means, DRM minors have the same life-time as the underlying
249 * device. However, this doesn't mean that the minor is active. Minors are
250 * registered and unregistered dynamically according to device-state.
1da177e4 251 */
0d639883 252
05b701f6
DH
253static struct drm_minor **drm_minor_get_slot(struct drm_device *dev,
254 unsigned int type)
255{
256 switch (type) {
257 case DRM_MINOR_LEGACY:
258 return &dev->primary;
259 case DRM_MINOR_RENDER:
260 return &dev->render;
261 case DRM_MINOR_CONTROL:
262 return &dev->control;
263 default:
264 return NULL;
265 }
266}
267
268static int drm_minor_alloc(struct drm_device *dev, unsigned int type)
269{
270 struct drm_minor *minor;
271
272 minor = kzalloc(sizeof(*minor), GFP_KERNEL);
273 if (!minor)
274 return -ENOMEM;
275
276 minor->type = type;
277 minor->dev = dev;
278 INIT_LIST_HEAD(&minor->master_list);
279
280 *drm_minor_get_slot(dev, type) = minor;
281 return 0;
282}
283
bd9dfa98
DH
284static void drm_minor_free(struct drm_device *dev, unsigned int type)
285{
286 struct drm_minor **slot;
287
288 slot = drm_minor_get_slot(dev, type);
289 if (*slot) {
290 kfree(*slot);
291 *slot = NULL;
292 }
293}
294
afcdbc86 295static int drm_minor_register(struct drm_device *dev, unsigned int type)
1da177e4 296{
2c14f28b 297 struct drm_minor *new_minor;
0d639883 298 unsigned long flags;
1da177e4 299 int ret;
2c14f28b 300 int minor_id;
1da177e4
LT
301
302 DRM_DEBUG("\n");
303
05b701f6
DH
304 new_minor = *drm_minor_get_slot(dev, type);
305 if (!new_minor)
306 return 0;
307
0d639883
DH
308 idr_preload(GFP_KERNEL);
309 spin_lock_irqsave(&drm_minor_lock, flags);
7d86cf1a
DH
310 minor_id = idr_alloc(&drm_minors_idr,
311 NULL,
312 64 * type,
313 64 * (type + 1),
0d639883
DH
314 GFP_NOWAIT);
315 spin_unlock_irqrestore(&drm_minor_lock, flags);
316 idr_preload_end();
7d86cf1a 317
2c14f28b
DA
318 if (minor_id < 0)
319 return minor_id;
320
2c14f28b 321 new_minor->index = minor_id;
2c14f28b 322
955b12de
BG
323 ret = drm_debugfs_init(new_minor, minor_id, drm_debugfs_root);
324 if (ret) {
156f5a78 325 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
1abbc437 326 goto err_id;
955b12de 327 }
2c14f28b
DA
328
329 ret = drm_sysfs_device_add(new_minor);
330 if (ret) {
1abbc437 331 DRM_ERROR("DRM: Error sysfs_device_add.\n");
cb6458f9 332 goto err_debugfs;
1da177e4 333 }
2c14f28b 334
0d639883
DH
335 /* replace NULL with @minor so lookups will succeed from now on */
336 spin_lock_irqsave(&drm_minor_lock, flags);
337 idr_replace(&drm_minors_idr, new_minor, new_minor->index);
338 spin_unlock_irqrestore(&drm_minor_lock, flags);
2c14f28b
DA
339
340 DRM_DEBUG("new minor assigned %d\n", minor_id);
341 return 0;
342
cb6458f9 343err_debugfs:
cb6458f9 344 drm_debugfs_cleanup(new_minor);
1abbc437 345err_id:
0d639883 346 spin_lock_irqsave(&drm_minor_lock, flags);
2c14f28b 347 idr_remove(&drm_minors_idr, minor_id);
0d639883
DH
348 spin_unlock_irqrestore(&drm_minor_lock, flags);
349 new_minor->index = 0;
1da177e4
LT
350 return ret;
351}
b5e89ed5 352
afcdbc86 353static void drm_minor_unregister(struct drm_device *dev, unsigned int type)
f73aca50 354{
afcdbc86 355 struct drm_minor *minor;
0d639883 356 unsigned long flags;
afcdbc86
DH
357
358 minor = *drm_minor_get_slot(dev, type);
a3483353 359 if (!minor || !minor->kdev)
f73aca50
DH
360 return;
361
0d639883
DH
362 spin_lock_irqsave(&drm_minor_lock, flags);
363 idr_remove(&drm_minors_idr, minor->index);
364 spin_unlock_irqrestore(&drm_minor_lock, flags);
365 minor->index = 0;
865fb47f 366
865fb47f 367 drm_debugfs_cleanup(minor);
f73aca50
DH
368 drm_sysfs_device_remove(minor);
369}
370
371/**
1616c525
DH
372 * drm_minor_acquire - Acquire a DRM minor
373 * @minor_id: Minor ID of the DRM-minor
374 *
375 * Looks up the given minor-ID and returns the respective DRM-minor object. The
376 * refence-count of the underlying device is increased so you must release this
377 * object with drm_minor_release().
378 *
379 * As long as you hold this minor, it is guaranteed that the object and the
380 * minor->dev pointer will stay valid! However, the device may get unplugged and
381 * unregistered while you hold the minor.
f73aca50 382 *
1616c525
DH
383 * Returns:
384 * Pointer to minor-object with increased device-refcount, or PTR_ERR on
385 * failure.
1da177e4 386 */
1616c525 387struct drm_minor *drm_minor_acquire(unsigned int minor_id)
1da177e4 388{
1616c525 389 struct drm_minor *minor;
0d639883 390 unsigned long flags;
1616c525 391
0d639883 392 spin_lock_irqsave(&drm_minor_lock, flags);
1616c525 393 minor = idr_find(&drm_minors_idr, minor_id);
0d639883
DH
394 if (minor)
395 drm_dev_ref(minor->dev);
396 spin_unlock_irqrestore(&drm_minor_lock, flags);
397
398 if (!minor) {
399 return ERR_PTR(-ENODEV);
400 } else if (drm_device_is_unplugged(minor->dev)) {
401 drm_dev_unref(minor->dev);
1616c525 402 return ERR_PTR(-ENODEV);
0d639883 403 }
673a394b 404
1616c525
DH
405 return minor;
406}
b5e89ed5 407
1616c525
DH
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 */
414void drm_minor_release(struct drm_minor *minor)
415{
416 drm_dev_unref(minor->dev);
1da177e4 417}
112b715e
KH
418
419/**
420 * Called via drm_exit() at module unload time or when pci device is
421 * unplugged.
422 *
423 * Cleans up all DRM device, calling drm_lastclose().
424 *
112b715e
KH
425 */
426void drm_put_dev(struct drm_device *dev)
427{
112b715e
KH
428 DRM_DEBUG("\n");
429
430 if (!dev) {
431 DRM_ERROR("cleanup called no dev\n");
432 return;
433 }
434
c3a49737 435 drm_dev_unregister(dev);
099d1c29 436 drm_dev_unref(dev);
112b715e
KH
437}
438EXPORT_SYMBOL(drm_put_dev);
2c07a21d
DA
439
440void drm_unplug_dev(struct drm_device *dev)
441{
442 /* for a USB device */
afcdbc86
DH
443 drm_minor_unregister(dev, DRM_MINOR_LEGACY);
444 drm_minor_unregister(dev, DRM_MINOR_RENDER);
445 drm_minor_unregister(dev, DRM_MINOR_CONTROL);
2c07a21d
DA
446
447 mutex_lock(&drm_global_mutex);
448
449 drm_device_set_unplugged(dev);
450
451 if (dev->open_count == 0) {
452 drm_put_dev(dev);
453 }
454 mutex_unlock(&drm_global_mutex);
455}
456EXPORT_SYMBOL(drm_unplug_dev);
1bb72532 457
31bbe16f
DH
458/*
459 * DRM internal mount
460 * We want to be able to allocate our own "struct address_space" to control
461 * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
462 * stand-alone address_space objects, so we need an underlying inode. As there
463 * is no way to allocate an independent inode easily, we need a fake internal
464 * VFS mount-point.
465 *
466 * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
467 * frees it again. You are allowed to use iget() and iput() to get references to
468 * the inode. But each drm_fs_inode_new() call must be paired with exactly one
469 * drm_fs_inode_free() call (which does not have to be the last iput()).
470 * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
471 * between multiple inode-users. You could, technically, call
472 * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
473 * iput(), but this way you'd end up with a new vfsmount for each inode.
474 */
475
476static int drm_fs_cnt;
477static struct vfsmount *drm_fs_mnt;
478
479static const struct dentry_operations drm_fs_dops = {
480 .d_dname = simple_dname,
481};
482
483static const struct super_operations drm_fs_sops = {
484 .statfs = simple_statfs,
485};
486
487static struct dentry *drm_fs_mount(struct file_system_type *fs_type, int flags,
488 const char *dev_name, void *data)
489{
490 return mount_pseudo(fs_type,
491 "drm:",
492 &drm_fs_sops,
493 &drm_fs_dops,
494 0x010203ff);
495}
496
497static struct file_system_type drm_fs_type = {
498 .name = "drm",
499 .owner = THIS_MODULE,
500 .mount = drm_fs_mount,
501 .kill_sb = kill_anon_super,
502};
503
504static struct inode *drm_fs_inode_new(void)
505{
506 struct inode *inode;
507 int r;
508
509 r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt);
510 if (r < 0) {
511 DRM_ERROR("Cannot mount pseudo fs: %d\n", r);
512 return ERR_PTR(r);
513 }
514
515 inode = alloc_anon_inode(drm_fs_mnt->mnt_sb);
516 if (IS_ERR(inode))
517 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
518
519 return inode;
520}
521
522static void drm_fs_inode_free(struct inode *inode)
523{
524 if (inode) {
525 iput(inode);
526 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
527 }
528}
529
1bb72532
DH
530/**
531 * drm_dev_alloc - Allocate new drm device
532 * @driver: DRM driver to allocate device for
533 * @parent: Parent device object
534 *
535 * Allocate and initialize a new DRM device. No device registration is done.
c22f0ace
DH
536 * Call drm_dev_register() to advertice the device to user space and register it
537 * with other core subsystems.
1bb72532 538 *
099d1c29
DH
539 * The initial ref-count of the object is 1. Use drm_dev_ref() and
540 * drm_dev_unref() to take and drop further ref-counts.
541 *
1bb72532
DH
542 * RETURNS:
543 * Pointer to new DRM device, or NULL if out of memory.
544 */
545struct drm_device *drm_dev_alloc(struct drm_driver *driver,
546 struct device *parent)
547{
548 struct drm_device *dev;
549 int ret;
550
551 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
552 if (!dev)
553 return NULL;
554
099d1c29 555 kref_init(&dev->ref);
1bb72532
DH
556 dev->dev = parent;
557 dev->driver = driver;
558
559 INIT_LIST_HEAD(&dev->filelist);
560 INIT_LIST_HEAD(&dev->ctxlist);
561 INIT_LIST_HEAD(&dev->vmalist);
562 INIT_LIST_HEAD(&dev->maplist);
563 INIT_LIST_HEAD(&dev->vblank_event_list);
564
565 spin_lock_init(&dev->count_lock);
566 spin_lock_init(&dev->event_lock);
567 mutex_init(&dev->struct_mutex);
568 mutex_init(&dev->ctxlist_mutex);
569
6796cb16
DH
570 dev->anon_inode = drm_fs_inode_new();
571 if (IS_ERR(dev->anon_inode)) {
572 ret = PTR_ERR(dev->anon_inode);
573 DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
1bb72532 574 goto err_free;
6796cb16
DH
575 }
576
05b701f6
DH
577 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
578 ret = drm_minor_alloc(dev, DRM_MINOR_CONTROL);
579 if (ret)
580 goto err_minors;
581 }
582
583 if (drm_core_check_feature(dev, DRIVER_RENDER) && drm_rnodes) {
584 ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
585 if (ret)
586 goto err_minors;
587 }
588
589 ret = drm_minor_alloc(dev, DRM_MINOR_LEGACY);
590 if (ret)
591 goto err_minors;
592
6796cb16 593 if (drm_ht_create(&dev->map_hash, 12))
05b701f6 594 goto err_minors;
1bb72532
DH
595
596 ret = drm_ctxbitmap_init(dev);
597 if (ret) {
598 DRM_ERROR("Cannot allocate memory for context bitmap.\n");
599 goto err_ht;
600 }
601
602 if (driver->driver_features & DRIVER_GEM) {
603 ret = drm_gem_init(dev);
604 if (ret) {
605 DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
606 goto err_ctxbitmap;
607 }
608 }
609
610 return dev;
611
612err_ctxbitmap:
613 drm_ctxbitmap_cleanup(dev);
614err_ht:
615 drm_ht_remove(&dev->map_hash);
05b701f6 616err_minors:
bd9dfa98
DH
617 drm_minor_free(dev, DRM_MINOR_LEGACY);
618 drm_minor_free(dev, DRM_MINOR_RENDER);
619 drm_minor_free(dev, DRM_MINOR_CONTROL);
6796cb16 620 drm_fs_inode_free(dev->anon_inode);
1bb72532
DH
621err_free:
622 kfree(dev);
623 return NULL;
624}
625EXPORT_SYMBOL(drm_dev_alloc);
c22f0ace 626
099d1c29 627static void drm_dev_release(struct kref *ref)
0dc8fe59 628{
099d1c29 629 struct drm_device *dev = container_of(ref, struct drm_device, ref);
8f6599da 630
0dc8fe59
DH
631 if (dev->driver->driver_features & DRIVER_GEM)
632 drm_gem_destroy(dev);
633
634 drm_ctxbitmap_cleanup(dev);
635 drm_ht_remove(&dev->map_hash);
6796cb16 636 drm_fs_inode_free(dev->anon_inode);
0dc8fe59 637
bd9dfa98
DH
638 drm_minor_free(dev, DRM_MINOR_LEGACY);
639 drm_minor_free(dev, DRM_MINOR_RENDER);
640 drm_minor_free(dev, DRM_MINOR_CONTROL);
641
0dc8fe59
DH
642 kfree(dev->devname);
643 kfree(dev);
644}
099d1c29
DH
645
646/**
647 * drm_dev_ref - Take reference of a DRM device
648 * @dev: device to take reference of or NULL
649 *
650 * This increases the ref-count of @dev by one. You *must* already own a
651 * reference when calling this. Use drm_dev_unref() to drop this reference
652 * again.
653 *
654 * This function never fails. However, this function does not provide *any*
655 * guarantee whether the device is alive or running. It only provides a
656 * reference to the object and the memory associated with it.
657 */
658void drm_dev_ref(struct drm_device *dev)
659{
660 if (dev)
661 kref_get(&dev->ref);
662}
663EXPORT_SYMBOL(drm_dev_ref);
664
665/**
666 * drm_dev_unref - Drop reference of a DRM device
667 * @dev: device to drop reference of or NULL
668 *
669 * This decreases the ref-count of @dev by one. The device is destroyed if the
670 * ref-count drops to zero.
671 */
672void drm_dev_unref(struct drm_device *dev)
673{
674 if (dev)
675 kref_put(&dev->ref, drm_dev_release);
676}
677EXPORT_SYMBOL(drm_dev_unref);
0dc8fe59 678
c22f0ace
DH
679/**
680 * drm_dev_register - Register DRM device
681 * @dev: Device to register
682 *
683 * Register the DRM device @dev with the system, advertise device to user-space
684 * and start normal device operation. @dev must be allocated via drm_dev_alloc()
685 * previously.
686 *
687 * Never call this twice on any device!
688 *
689 * RETURNS:
690 * 0 on success, negative error code on failure.
691 */
692int drm_dev_register(struct drm_device *dev, unsigned long flags)
693{
694 int ret;
695
696 mutex_lock(&drm_global_mutex);
697
afcdbc86 698 ret = drm_minor_register(dev, DRM_MINOR_CONTROL);
05b701f6
DH
699 if (ret)
700 goto err_minors;
c22f0ace 701
afcdbc86 702 ret = drm_minor_register(dev, DRM_MINOR_RENDER);
05b701f6
DH
703 if (ret)
704 goto err_minors;
c22f0ace 705
afcdbc86 706 ret = drm_minor_register(dev, DRM_MINOR_LEGACY);
c22f0ace 707 if (ret)
05b701f6 708 goto err_minors;
c22f0ace
DH
709
710 if (dev->driver->load) {
711 ret = dev->driver->load(dev, flags);
712 if (ret)
05b701f6 713 goto err_minors;
c22f0ace
DH
714 }
715
716 /* setup grouping for legacy outputs */
717 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
718 ret = drm_mode_group_init_legacy_group(dev,
719 &dev->primary->mode_group);
720 if (ret)
721 goto err_unload;
722 }
723
c22f0ace
DH
724 ret = 0;
725 goto out_unlock;
726
727err_unload:
728 if (dev->driver->unload)
729 dev->driver->unload(dev);
05b701f6 730err_minors:
afcdbc86
DH
731 drm_minor_unregister(dev, DRM_MINOR_LEGACY);
732 drm_minor_unregister(dev, DRM_MINOR_RENDER);
733 drm_minor_unregister(dev, DRM_MINOR_CONTROL);
c22f0ace
DH
734out_unlock:
735 mutex_unlock(&drm_global_mutex);
736 return ret;
737}
738EXPORT_SYMBOL(drm_dev_register);
c3a49737
DH
739
740/**
741 * drm_dev_unregister - Unregister DRM device
742 * @dev: Device to unregister
743 *
744 * Unregister the DRM device from the system. This does the reverse of
745 * drm_dev_register() but does not deallocate the device. The caller must call
099d1c29 746 * drm_dev_unref() to drop their final reference.
c3a49737
DH
747 */
748void drm_dev_unregister(struct drm_device *dev)
749{
750 struct drm_map_list *r_list, *list_temp;
751
752 drm_lastclose(dev);
753
754 if (dev->driver->unload)
755 dev->driver->unload(dev);
756
4efafebe
DV
757 if (dev->agp)
758 drm_pci_agp_destroy(dev);
c3a49737
DH
759
760 drm_vblank_cleanup(dev);
761
762 list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
763 drm_rmmap(dev, r_list->map);
764
afcdbc86
DH
765 drm_minor_unregister(dev, DRM_MINOR_LEGACY);
766 drm_minor_unregister(dev, DRM_MINOR_RENDER);
767 drm_minor_unregister(dev, DRM_MINOR_CONTROL);
c3a49737
DH
768}
769EXPORT_SYMBOL(drm_dev_unregister);
This page took 2.736778 seconds and 5 git commands to generate.