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