drm/<drivers>: Use drmm_add_final_kfree
[linux-2.6-block.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>
4a457910 34#include <linux/pseudo_fs.h>
5a0e3ad6 35#include <linux/slab.h>
bee330f3 36#include <linux/srcu.h>
85e634bc 37
c76f0f7c 38#include <drm/drm_client.h>
0500c04e 39#include <drm/drm_color_mgmt.h>
85e634bc 40#include <drm/drm_drv.h>
0500c04e 41#include <drm/drm_file.h>
6f365e56 42#include <drm/drm_managed.h>
0500c04e
SR
43#include <drm/drm_mode_object.h>
44#include <drm/drm_print.h>
85e634bc 45
79190ea2 46#include "drm_crtc_internal.h"
67d0ec4e 47#include "drm_internal.h"
0500c04e 48#include "drm_legacy.h"
1da177e4 49
82d5e73f
DH
50MODULE_AUTHOR("Gareth Hughes, Leif Delgass, José Fonseca, Jon Smirl");
51MODULE_DESCRIPTION("DRM shared core routines");
1da177e4 52MODULE_LICENSE("GPL and additional rights");
1da177e4 53
0d639883 54static DEFINE_SPINLOCK(drm_minor_lock);
1b7199fe 55static struct idr drm_minors_idr;
2c14f28b 56
371c2279
AM
57/*
58 * If the drm core fails to init for whatever reason,
59 * we should prevent any drivers from registering with it.
60 * It's best to check this at drm_dev_init(), as some drivers
61 * prefer to embed struct drm_device into their own device
62 * structure and call drm_dev_init() themselves.
63 */
64static bool drm_core_init_complete = false;
65
1b7199fe 66static struct dentry *drm_debugfs_root;
5ad3d883 67
bee330f3
NT
68DEFINE_STATIC_SRCU(drm_unplug_srcu);
69
0d639883
DH
70/*
71 * DRM Minors
72 * A DRM device can provide several char-dev interfaces on the DRM-Major. Each
73 * of them is represented by a drm_minor object. Depending on the capabilities
74 * of the device-driver, different interfaces are registered.
1da177e4 75 *
0d639883
DH
76 * Minors can be accessed via dev->$minor_name. This pointer is either
77 * NULL or a valid drm_minor pointer and stays valid as long as the device is
78 * valid. This means, DRM minors have the same life-time as the underlying
79 * device. However, this doesn't mean that the minor is active. Minors are
80 * registered and unregistered dynamically according to device-state.
1da177e4 81 */
0d639883 82
05b701f6
DH
83static struct drm_minor **drm_minor_get_slot(struct drm_device *dev,
84 unsigned int type)
85{
86 switch (type) {
a3ccc461 87 case DRM_MINOR_PRIMARY:
05b701f6
DH
88 return &dev->primary;
89 case DRM_MINOR_RENDER:
90 return &dev->render;
05b701f6 91 default:
3bd07ccd 92 BUG();
05b701f6
DH
93 }
94}
95
96static int drm_minor_alloc(struct drm_device *dev, unsigned int type)
97{
98 struct drm_minor *minor;
f1b85962
DH
99 unsigned long flags;
100 int r;
05b701f6
DH
101
102 minor = kzalloc(sizeof(*minor), GFP_KERNEL);
103 if (!minor)
104 return -ENOMEM;
105
106 minor->type = type;
107 minor->dev = dev;
05b701f6 108
f1b85962
DH
109 idr_preload(GFP_KERNEL);
110 spin_lock_irqsave(&drm_minor_lock, flags);
111 r = idr_alloc(&drm_minors_idr,
112 NULL,
113 64 * type,
114 64 * (type + 1),
115 GFP_NOWAIT);
116 spin_unlock_irqrestore(&drm_minor_lock, flags);
117 idr_preload_end();
118
119 if (r < 0)
120 goto err_free;
121
122 minor->index = r;
123
e1728075
DH
124 minor->kdev = drm_sysfs_minor_alloc(minor);
125 if (IS_ERR(minor->kdev)) {
126 r = PTR_ERR(minor->kdev);
127 goto err_index;
128 }
129
05b701f6
DH
130 *drm_minor_get_slot(dev, type) = minor;
131 return 0;
f1b85962 132
e1728075
DH
133err_index:
134 spin_lock_irqsave(&drm_minor_lock, flags);
135 idr_remove(&drm_minors_idr, minor->index);
136 spin_unlock_irqrestore(&drm_minor_lock, flags);
f1b85962
DH
137err_free:
138 kfree(minor);
139 return r;
05b701f6
DH
140}
141
bd9dfa98
DH
142static void drm_minor_free(struct drm_device *dev, unsigned int type)
143{
f1b85962
DH
144 struct drm_minor **slot, *minor;
145 unsigned long flags;
bd9dfa98
DH
146
147 slot = drm_minor_get_slot(dev, type);
f1b85962
DH
148 minor = *slot;
149 if (!minor)
150 return;
151
e1728075 152 put_device(minor->kdev);
f1b85962
DH
153
154 spin_lock_irqsave(&drm_minor_lock, flags);
155 idr_remove(&drm_minors_idr, minor->index);
156 spin_unlock_irqrestore(&drm_minor_lock, flags);
157
158 kfree(minor);
159 *slot = NULL;
bd9dfa98
DH
160}
161
afcdbc86 162static int drm_minor_register(struct drm_device *dev, unsigned int type)
1da177e4 163{
f1b85962 164 struct drm_minor *minor;
0d639883 165 unsigned long flags;
1da177e4 166 int ret;
1da177e4
LT
167
168 DRM_DEBUG("\n");
169
f1b85962
DH
170 minor = *drm_minor_get_slot(dev, type);
171 if (!minor)
05b701f6
DH
172 return 0;
173
f1b85962 174 ret = drm_debugfs_init(minor, minor->index, drm_debugfs_root);
955b12de 175 if (ret) {
156f5a78 176 DRM_ERROR("DRM: Failed to initialize /sys/kernel/debug/dri.\n");
a67834f8 177 goto err_debugfs;
955b12de 178 }
2c14f28b 179
e1728075
DH
180 ret = device_add(minor->kdev);
181 if (ret)
cb6458f9 182 goto err_debugfs;
2c14f28b 183
0d639883
DH
184 /* replace NULL with @minor so lookups will succeed from now on */
185 spin_lock_irqsave(&drm_minor_lock, flags);
f1b85962 186 idr_replace(&drm_minors_idr, minor, minor->index);
0d639883 187 spin_unlock_irqrestore(&drm_minor_lock, flags);
2c14f28b 188
f1b85962 189 DRM_DEBUG("new minor registered %d\n", minor->index);
2c14f28b
DA
190 return 0;
191
cb6458f9 192err_debugfs:
f1b85962 193 drm_debugfs_cleanup(minor);
1da177e4
LT
194 return ret;
195}
b5e89ed5 196
afcdbc86 197static void drm_minor_unregister(struct drm_device *dev, unsigned int type)
f73aca50 198{
afcdbc86 199 struct drm_minor *minor;
0d639883 200 unsigned long flags;
afcdbc86
DH
201
202 minor = *drm_minor_get_slot(dev, type);
e1728075 203 if (!minor || !device_is_registered(minor->kdev))
f73aca50
DH
204 return;
205
f1b85962 206 /* replace @minor with NULL so lookups will fail from now on */
0d639883 207 spin_lock_irqsave(&drm_minor_lock, flags);
f1b85962 208 idr_replace(&drm_minors_idr, NULL, minor->index);
0d639883 209 spin_unlock_irqrestore(&drm_minor_lock, flags);
865fb47f 210
e1728075
DH
211 device_del(minor->kdev);
212 dev_set_drvdata(minor->kdev, NULL); /* safety belt */
865fb47f 213 drm_debugfs_cleanup(minor);
f73aca50
DH
214}
215
85e634bc 216/*
1616c525
DH
217 * Looks up the given minor-ID and returns the respective DRM-minor object. The
218 * refence-count of the underlying device is increased so you must release this
219 * object with drm_minor_release().
220 *
221 * As long as you hold this minor, it is guaranteed that the object and the
222 * minor->dev pointer will stay valid! However, the device may get unplugged and
223 * unregistered while you hold the minor.
1da177e4 224 */
1616c525 225struct drm_minor *drm_minor_acquire(unsigned int minor_id)
1da177e4 226{
1616c525 227 struct drm_minor *minor;
0d639883 228 unsigned long flags;
1616c525 229
0d639883 230 spin_lock_irqsave(&drm_minor_lock, flags);
1616c525 231 minor = idr_find(&drm_minors_idr, minor_id);
0d639883 232 if (minor)
9a96f550 233 drm_dev_get(minor->dev);
0d639883
DH
234 spin_unlock_irqrestore(&drm_minor_lock, flags);
235
236 if (!minor) {
237 return ERR_PTR(-ENODEV);
c07dcd61 238 } else if (drm_dev_is_unplugged(minor->dev)) {
9a96f550 239 drm_dev_put(minor->dev);
1616c525 240 return ERR_PTR(-ENODEV);
0d639883 241 }
673a394b 242
1616c525
DH
243 return minor;
244}
b5e89ed5 245
1616c525
DH
246void drm_minor_release(struct drm_minor *minor)
247{
9a96f550 248 drm_dev_put(minor->dev);
1da177e4 249}
112b715e 250
6e3f797c
DV
251/**
252 * DOC: driver instance overview
253 *
ea0dd85a 254 * A device instance for a drm driver is represented by &struct drm_device. This
33e70110 255 * is initialized with drm_dev_init(), usually from bus-specific ->probe()
6e3f797c
DV
256 * callbacks implemented by the driver. The driver then needs to initialize all
257 * the various subsystems for the drm device like memory management, vblank
258 * handling, modesetting support and intial output configuration plus obviously
33e70110
DV
259 * initialize all the corresponding hardware bits. Finally when everything is up
260 * and running and ready for userspace the device instance can be published
261 * using drm_dev_register().
6e3f797c
DV
262 *
263 * There is also deprecated support for initalizing device instances using
ef40cbf9 264 * bus-specific helpers and the &drm_driver.load callback. But due to
6e3f797c
DV
265 * backwards-compatibility needs the device instance have to be published too
266 * early, which requires unpretty global locking to make safe and is therefore
267 * only support for existing drivers not yet converted to the new scheme.
268 *
269 * When cleaning up a device instance everything needs to be done in reverse:
270 * First unpublish the device instance with drm_dev_unregister(). Then clean up
271 * any other resources allocated at device initialization and drop the driver's
9a96f550 272 * reference to &drm_device using drm_dev_put().
6e3f797c
DV
273 *
274 * Note that the lifetime rules for &drm_device instance has still a lot of
275 * historical baggage. Hence use the reference counting provided by
9a96f550 276 * drm_dev_get() and drm_dev_put() only carefully.
de99f060
NT
277 *
278 * Display driver example
279 * ~~~~~~~~~~~~~~~~~~~~~~
280 *
281 * The following example shows a typical structure of a DRM display driver.
282 * The example focus on the probe() function and the other functions that is
283 * almost always present and serves as a demonstration of devm_drm_dev_init()
284 * usage with its accompanying drm_driver->release callback.
285 *
286 * .. code-block:: c
287 *
288 * struct driver_device {
289 * struct drm_device drm;
290 * void *userspace_facing;
291 * struct clk *pclk;
292 * };
293 *
294 * static void driver_drm_release(struct drm_device *drm)
295 * {
296 * struct driver_device *priv = container_of(...);
297 *
298 * drm_mode_config_cleanup(drm);
299 * drm_dev_fini(drm);
300 * kfree(priv->userspace_facing);
301 * kfree(priv);
302 * }
303 *
304 * static struct drm_driver driver_drm_driver = {
305 * [...]
306 * .release = driver_drm_release,
307 * };
308 *
309 * static int driver_probe(struct platform_device *pdev)
310 * {
311 * struct driver_device *priv;
312 * struct drm_device *drm;
313 * int ret;
314 *
56d8d641
JN
315 * // devm_kzalloc() can't be used here because the drm_device '
316 * // lifetime can exceed the device lifetime if driver unbind
317 * // happens when userspace still has open file descriptors.
de99f060
NT
318 * priv = kzalloc(sizeof(*priv), GFP_KERNEL);
319 * if (!priv)
320 * return -ENOMEM;
321 *
322 * drm = &priv->drm;
323 *
324 * ret = devm_drm_dev_init(&pdev->dev, drm, &driver_drm_driver);
325 * if (ret) {
326 * kfree(drm);
327 * return ret;
328 * }
329 *
330 * drm_mode_config_init(drm);
331 *
332 * priv->userspace_facing = kzalloc(..., GFP_KERNEL);
333 * if (!priv->userspace_facing)
334 * return -ENOMEM;
335 *
336 * priv->pclk = devm_clk_get(dev, "PCLK");
337 * if (IS_ERR(priv->pclk))
338 * return PTR_ERR(priv->pclk);
339 *
56d8d641 340 * // Further setup, display pipeline etc
de99f060
NT
341 *
342 * platform_set_drvdata(pdev, drm);
343 *
344 * drm_mode_config_reset(drm);
345 *
346 * ret = drm_dev_register(drm);
347 * if (ret)
348 * return ret;
349 *
350 * drm_fbdev_generic_setup(drm, 32);
351 *
352 * return 0;
353 * }
354 *
56d8d641 355 * // This function is called before the devm_ resources are released
de99f060
NT
356 * static int driver_remove(struct platform_device *pdev)
357 * {
358 * struct drm_device *drm = platform_get_drvdata(pdev);
359 *
360 * drm_dev_unregister(drm);
361 * drm_atomic_helper_shutdown(drm)
362 *
363 * return 0;
364 * }
365 *
56d8d641 366 * // This function is called on kernel restart and shutdown
de99f060
NT
367 * static void driver_shutdown(struct platform_device *pdev)
368 * {
369 * drm_atomic_helper_shutdown(platform_get_drvdata(pdev));
370 * }
371 *
372 * static int __maybe_unused driver_pm_suspend(struct device *dev)
373 * {
374 * return drm_mode_config_helper_suspend(dev_get_drvdata(dev));
375 * }
376 *
377 * static int __maybe_unused driver_pm_resume(struct device *dev)
378 * {
379 * drm_mode_config_helper_resume(dev_get_drvdata(dev));
380 *
381 * return 0;
382 * }
383 *
384 * static const struct dev_pm_ops driver_pm_ops = {
385 * SET_SYSTEM_SLEEP_PM_OPS(driver_pm_suspend, driver_pm_resume)
386 * };
387 *
388 * static struct platform_driver driver_driver = {
389 * .driver = {
390 * [...]
391 * .pm = &driver_pm_ops,
392 * },
393 * .probe = driver_probe,
394 * .remove = driver_remove,
395 * .shutdown = driver_shutdown,
396 * };
397 * module_platform_driver(driver_driver);
398 *
399 * Drivers that want to support device unplugging (USB, DT overlay unload) should
400 * use drm_dev_unplug() instead of drm_dev_unregister(). The driver must protect
401 * regions that is accessing device resources to prevent use after they're
402 * released. This is done using drm_dev_enter() and drm_dev_exit(). There is one
403 * shortcoming however, drm_dev_unplug() marks the drm_device as unplugged before
404 * drm_atomic_helper_shutdown() is called. This means that if the disable code
405 * paths are protected, they will not run on regular driver module unload,
406 * possibily leaving the hardware enabled.
6e3f797c
DV
407 */
408
112b715e 409/**
c6a1af8a
TR
410 * drm_put_dev - Unregister and release a DRM device
411 * @dev: DRM device
112b715e 412 *
c6a1af8a 413 * Called at module unload time or when a PCI device is unplugged.
112b715e 414 *
c6a1af8a 415 * Cleans up all DRM device, calling drm_lastclose().
6e3f797c
DV
416 *
417 * Note: Use of this function is deprecated. It will eventually go away
9a96f550 418 * completely. Please use drm_dev_unregister() and drm_dev_put() explicitly
6e3f797c
DV
419 * instead to make sure that the device isn't userspace accessible any more
420 * while teardown is in progress, ensuring that userspace can't access an
421 * inconsistent state.
112b715e
KH
422 */
423void drm_put_dev(struct drm_device *dev)
424{
112b715e
KH
425 DRM_DEBUG("\n");
426
427 if (!dev) {
428 DRM_ERROR("cleanup called no dev\n");
429 return;
430 }
431
c3a49737 432 drm_dev_unregister(dev);
9a96f550 433 drm_dev_put(dev);
112b715e
KH
434}
435EXPORT_SYMBOL(drm_put_dev);
2c07a21d 436
bee330f3
NT
437/**
438 * drm_dev_enter - Enter device critical section
439 * @dev: DRM device
440 * @idx: Pointer to index that will be passed to the matching drm_dev_exit()
441 *
442 * This function marks and protects the beginning of a section that should not
443 * be entered after the device has been unplugged. The section end is marked
444 * with drm_dev_exit(). Calls to this function can be nested.
445 *
446 * Returns:
447 * True if it is OK to enter the section, false otherwise.
448 */
449bool drm_dev_enter(struct drm_device *dev, int *idx)
c07dcd61 450{
bee330f3
NT
451 *idx = srcu_read_lock(&drm_unplug_srcu);
452
453 if (dev->unplugged) {
454 srcu_read_unlock(&drm_unplug_srcu, *idx);
455 return false;
456 }
457
458 return true;
c07dcd61 459}
bee330f3
NT
460EXPORT_SYMBOL(drm_dev_enter);
461
462/**
463 * drm_dev_exit - Exit device critical section
464 * @idx: index returned from drm_dev_enter()
465 *
466 * This function marks the end of a section that should not be entered after
467 * the device has been unplugged.
468 */
469void drm_dev_exit(int idx)
c07dcd61 470{
bee330f3 471 srcu_read_unlock(&drm_unplug_srcu, idx);
c07dcd61 472}
bee330f3 473EXPORT_SYMBOL(drm_dev_exit);
c07dcd61
DV
474
475/**
476 * drm_dev_unplug - unplug a DRM device
477 * @dev: DRM device
478 *
479 * This unplugs a hotpluggable DRM device, which makes it inaccessible to
bee330f3
NT
480 * userspace operations. Entry-points can use drm_dev_enter() and
481 * drm_dev_exit() to protect device resources in a race free manner. This
c07dcd61
DV
482 * essentially unregisters the device like drm_dev_unregister(), but can be
483 * called while there are still open users of @dev.
484 */
485void drm_dev_unplug(struct drm_device *dev)
2c07a21d 486{
bee330f3
NT
487 /*
488 * After synchronizing any critical read section is guaranteed to see
489 * the new value of ->unplugged, and any critical section which might
490 * still have seen the old value of ->unplugged is guaranteed to have
491 * finished.
492 */
493 dev->unplugged = true;
494 synchronize_srcu(&drm_unplug_srcu);
069035c5
OA
495
496 drm_dev_unregister(dev);
2c07a21d 497}
c07dcd61 498EXPORT_SYMBOL(drm_dev_unplug);
1bb72532 499
31bbe16f
DH
500/*
501 * DRM internal mount
502 * We want to be able to allocate our own "struct address_space" to control
503 * memory-mappings in VRAM (or stolen RAM, ...). However, core MM does not allow
504 * stand-alone address_space objects, so we need an underlying inode. As there
505 * is no way to allocate an independent inode easily, we need a fake internal
506 * VFS mount-point.
507 *
508 * The drm_fs_inode_new() function allocates a new inode, drm_fs_inode_free()
509 * frees it again. You are allowed to use iget() and iput() to get references to
510 * the inode. But each drm_fs_inode_new() call must be paired with exactly one
511 * drm_fs_inode_free() call (which does not have to be the last iput()).
512 * We use drm_fs_inode_*() to manage our internal VFS mount-point and share it
513 * between multiple inode-users. You could, technically, call
514 * iget() + drm_fs_inode_free() directly after alloc and sometime later do an
515 * iput(), but this way you'd end up with a new vfsmount for each inode.
516 */
517
518static int drm_fs_cnt;
519static struct vfsmount *drm_fs_mnt;
520
4a457910 521static int drm_fs_init_fs_context(struct fs_context *fc)
31bbe16f 522{
4a457910 523 return init_pseudo(fc, 0x010203ff) ? 0 : -ENOMEM;
31bbe16f
DH
524}
525
526static struct file_system_type drm_fs_type = {
527 .name = "drm",
528 .owner = THIS_MODULE,
4a457910 529 .init_fs_context = drm_fs_init_fs_context,
31bbe16f
DH
530 .kill_sb = kill_anon_super,
531};
532
533static struct inode *drm_fs_inode_new(void)
534{
535 struct inode *inode;
536 int r;
537
538 r = simple_pin_fs(&drm_fs_type, &drm_fs_mnt, &drm_fs_cnt);
539 if (r < 0) {
540 DRM_ERROR("Cannot mount pseudo fs: %d\n", r);
541 return ERR_PTR(r);
542 }
543
544 inode = alloc_anon_inode(drm_fs_mnt->mnt_sb);
545 if (IS_ERR(inode))
546 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
547
548 return inode;
549}
550
551static void drm_fs_inode_free(struct inode *inode)
552{
553 if (inode) {
554 iput(inode);
555 simple_release_fs(&drm_fs_mnt, &drm_fs_cnt);
556 }
557}
558
86ab67df
DV
559/**
560 * DOC: component helper usage recommendations
561 *
562 * DRM drivers that drive hardware where a logical device consists of a pile of
563 * independent hardware blocks are recommended to use the :ref:`component helper
564 * library<component>`. For consistency and better options for code reuse the
565 * following guidelines apply:
566 *
567 * - The entire device initialization procedure should be run from the
568 * &component_master_ops.master_bind callback, starting with drm_dev_init(),
569 * then binding all components with component_bind_all() and finishing with
570 * drm_dev_register().
571 *
572 * - The opaque pointer passed to all components through component_bind_all()
573 * should point at &struct drm_device of the device instance, not some driver
574 * specific private structure.
575 *
576 * - The component helper fills the niche where further standardization of
577 * interfaces is not practical. When there already is, or will be, a
578 * standardized interface like &drm_bridge or &drm_panel, providing its own
579 * functions to find such components at driver load time, like
580 * drm_of_find_panel_or_bridge(), then the component helper should not be
581 * used.
582 */
583
1bb72532 584/**
b209aca3
CW
585 * drm_dev_init - Initialise new DRM device
586 * @dev: DRM device
587 * @driver: DRM driver
1bb72532
DH
588 * @parent: Parent device object
589 *
b209aca3 590 * Initialize a new DRM device. No device registration is done.
c22f0ace 591 * Call drm_dev_register() to advertice the device to user space and register it
6e3f797c
DV
592 * with other core subsystems. This should be done last in the device
593 * initialization sequence to make sure userspace can't access an inconsistent
594 * state.
1bb72532 595 *
9a96f550
AP
596 * The initial ref-count of the object is 1. Use drm_dev_get() and
597 * drm_dev_put() to take and drop further ref-counts.
099d1c29 598 *
33e70110
DV
599 * It is recommended that drivers embed &struct drm_device into their own device
600 * structure.
601 *
b209aca3 602 * Drivers that do not want to allocate their own device struct
ea0dd85a
DV
603 * embedding &struct drm_device can call drm_dev_alloc() instead. For drivers
604 * that do embed &struct drm_device it must be placed first in the overall
d82faafd
DV
605 * structure, and the overall structure must be allocated using kmalloc(): The
606 * drm core's release function unconditionally calls kfree() on the @dev pointer
f30c9257
CW
607 * when the final reference is released. To override this behaviour, and so
608 * allow embedding of the drm_device inside the driver's device struct at an
609 * arbitrary offset, you must supply a &drm_driver.release callback and control
610 * the finalization explicitly.
b209aca3 611 *
1bb72532 612 * RETURNS:
b209aca3 613 * 0 on success, or error code on failure.
1bb72532 614 */
b209aca3
CW
615int drm_dev_init(struct drm_device *dev,
616 struct drm_driver *driver,
617 struct device *parent)
1bb72532 618{
1bb72532
DH
619 int ret;
620
371c2279
AM
621 if (!drm_core_init_complete) {
622 DRM_ERROR("DRM core is not initialized\n");
623 return -ENODEV;
624 }
625
8b6fc114
AP
626 if (WARN_ON(!parent))
627 return -EINVAL;
f08877e7 628
099d1c29 629 kref_init(&dev->ref);
56be6503 630 dev->dev = get_device(parent);
1bb72532
DH
631 dev->driver = driver;
632
c6603c74
DV
633 INIT_LIST_HEAD(&dev->managed.resources);
634 spin_lock_init(&dev->managed.lock);
635
18ace11f
VS
636 /* no per-device feature limits by default */
637 dev->driver_features = ~0u;
638
8437dd73 639 drm_legacy_init_members(dev);
1bb72532 640 INIT_LIST_HEAD(&dev->filelist);
c76f0f7c
NT
641 INIT_LIST_HEAD(&dev->filelist_internal);
642 INIT_LIST_HEAD(&dev->clientlist);
1bb72532
DH
643 INIT_LIST_HEAD(&dev->vblank_event_list);
644
1bb72532
DH
645 spin_lock_init(&dev->event_lock);
646 mutex_init(&dev->struct_mutex);
1d2ac403 647 mutex_init(&dev->filelist_mutex);
c76f0f7c 648 mutex_init(&dev->clientlist_mutex);
c996fd0b 649 mutex_init(&dev->master_mutex);
1bb72532 650
6796cb16
DH
651 dev->anon_inode = drm_fs_inode_new();
652 if (IS_ERR(dev->anon_inode)) {
653 ret = PTR_ERR(dev->anon_inode);
654 DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
1bb72532 655 goto err_free;
6796cb16
DH
656 }
657
6d6dfcfb 658 if (drm_core_check_feature(dev, DRIVER_RENDER)) {
05b701f6
DH
659 ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
660 if (ret)
661 goto err_minors;
662 }
663
a3ccc461 664 ret = drm_minor_alloc(dev, DRM_MINOR_PRIMARY);
05b701f6
DH
665 if (ret)
666 goto err_minors;
667
fabb0e2a 668 ret = drm_legacy_create_map_hash(dev);
b209aca3 669 if (ret)
05b701f6 670 goto err_minors;
1bb72532 671
ba6976c1 672 drm_legacy_ctxbitmap_init(dev);
1bb72532 673
1bcecfac 674 if (drm_core_check_feature(dev, DRIVER_GEM)) {
1bb72532
DH
675 ret = drm_gem_init(dev);
676 if (ret) {
677 DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
678 goto err_ctxbitmap;
679 }
680 }
681
f08877e7 682 ret = drm_dev_set_unique(dev, dev_name(parent));
5079c464
DV
683 if (ret)
684 goto err_setunique;
e112e593 685
b209aca3 686 return 0;
1bb72532 687
e112e593
NI
688err_setunique:
689 if (drm_core_check_feature(dev, DRIVER_GEM))
690 drm_gem_destroy(dev);
1bb72532 691err_ctxbitmap:
e7b96070 692 drm_legacy_ctxbitmap_cleanup(dev);
fabb0e2a 693 drm_legacy_remove_map_hash(dev);
05b701f6 694err_minors:
a3ccc461 695 drm_minor_free(dev, DRM_MINOR_PRIMARY);
bd9dfa98 696 drm_minor_free(dev, DRM_MINOR_RENDER);
6796cb16 697 drm_fs_inode_free(dev->anon_inode);
1bb72532 698err_free:
56be6503 699 put_device(dev->dev);
c996fd0b 700 mutex_destroy(&dev->master_mutex);
c76f0f7c 701 mutex_destroy(&dev->clientlist_mutex);
f92e1ee5
JL
702 mutex_destroy(&dev->filelist_mutex);
703 mutex_destroy(&dev->struct_mutex);
8437dd73 704 drm_legacy_destroy_members(dev);
b209aca3
CW
705 return ret;
706}
707EXPORT_SYMBOL(drm_dev_init);
708
9b1f1b6b
NT
709static void devm_drm_dev_init_release(void *data)
710{
711 drm_dev_put(data);
712}
713
714/**
715 * devm_drm_dev_init - Resource managed drm_dev_init()
716 * @parent: Parent device object
717 * @dev: DRM device
718 * @driver: DRM driver
719 *
720 * Managed drm_dev_init(). The DRM device initialized with this function is
721 * automatically put on driver detach using drm_dev_put(). You must supply a
722 * &drm_driver.release callback to control the finalization explicitly.
723 *
724 * RETURNS:
725 * 0 on success, or error code on failure.
726 */
727int devm_drm_dev_init(struct device *parent,
728 struct drm_device *dev,
729 struct drm_driver *driver)
730{
731 int ret;
732
8b6fc114 733 if (WARN_ON(!driver->release))
9b1f1b6b
NT
734 return -EINVAL;
735
736 ret = drm_dev_init(dev, driver, parent);
737 if (ret)
738 return ret;
739
740 ret = devm_add_action(parent, devm_drm_dev_init_release, dev);
741 if (ret)
742 devm_drm_dev_init_release(dev);
743
744 return ret;
745}
746EXPORT_SYMBOL(devm_drm_dev_init);
747
f30c9257
CW
748/**
749 * drm_dev_fini - Finalize a dead DRM device
750 * @dev: DRM device
751 *
752 * Finalize a dead DRM device. This is the converse to drm_dev_init() and
753 * frees up all data allocated by it. All driver private data should be
754 * finalized first. Note that this function does not free the @dev, that is
755 * left to the caller.
756 *
757 * The ref-count of @dev must be zero, and drm_dev_fini() should only be called
758 * from a &drm_driver.release callback.
759 */
760void drm_dev_fini(struct drm_device *dev)
761{
762 drm_vblank_cleanup(dev);
763
764 if (drm_core_check_feature(dev, DRIVER_GEM))
765 drm_gem_destroy(dev);
766
767 drm_legacy_ctxbitmap_cleanup(dev);
fabb0e2a 768 drm_legacy_remove_map_hash(dev);
f30c9257
CW
769 drm_fs_inode_free(dev->anon_inode);
770
771 drm_minor_free(dev, DRM_MINOR_PRIMARY);
772 drm_minor_free(dev, DRM_MINOR_RENDER);
f30c9257 773
56be6503
NT
774 put_device(dev->dev);
775
f30c9257 776 mutex_destroy(&dev->master_mutex);
c76f0f7c 777 mutex_destroy(&dev->clientlist_mutex);
f30c9257
CW
778 mutex_destroy(&dev->filelist_mutex);
779 mutex_destroy(&dev->struct_mutex);
8437dd73 780 drm_legacy_destroy_members(dev);
f30c9257
CW
781 kfree(dev->unique);
782}
783EXPORT_SYMBOL(drm_dev_fini);
784
b209aca3
CW
785/**
786 * drm_dev_alloc - Allocate new DRM device
787 * @driver: DRM driver to allocate device for
788 * @parent: Parent device object
789 *
790 * Allocate and initialize a new DRM device. No device registration is done.
791 * Call drm_dev_register() to advertice the device to user space and register it
792 * with other core subsystems. This should be done last in the device
793 * initialization sequence to make sure userspace can't access an inconsistent
794 * state.
795 *
9a96f550
AP
796 * The initial ref-count of the object is 1. Use drm_dev_get() and
797 * drm_dev_put() to take and drop further ref-counts.
b209aca3
CW
798 *
799 * Note that for purely virtual devices @parent can be NULL.
800 *
ea0dd85a 801 * Drivers that wish to subclass or embed &struct drm_device into their
b209aca3
CW
802 * own struct should look at using drm_dev_init() instead.
803 *
804 * RETURNS:
0f288605 805 * Pointer to new DRM device, or ERR_PTR on failure.
b209aca3
CW
806 */
807struct drm_device *drm_dev_alloc(struct drm_driver *driver,
808 struct device *parent)
809{
810 struct drm_device *dev;
811 int ret;
812
813 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
814 if (!dev)
0f288605 815 return ERR_PTR(-ENOMEM);
b209aca3
CW
816
817 ret = drm_dev_init(dev, driver, parent);
818 if (ret) {
819 kfree(dev);
0f288605 820 return ERR_PTR(ret);
b209aca3
CW
821 }
822
6f365e56
DV
823 drmm_add_final_kfree(dev, dev);
824
b209aca3 825 return dev;
1bb72532
DH
826}
827EXPORT_SYMBOL(drm_dev_alloc);
c22f0ace 828
099d1c29 829static void drm_dev_release(struct kref *ref)
0dc8fe59 830{
099d1c29 831 struct drm_device *dev = container_of(ref, struct drm_device, ref);
8f6599da 832
c6603c74 833 if (dev->driver->release)
f30c9257 834 dev->driver->release(dev);
c6603c74 835 else
f30c9257 836 drm_dev_fini(dev);
c6603c74
DV
837
838 drm_managed_release(dev);
839
840 if (!dev->driver->release && !dev->managed.final_kfree) {
841 WARN_ON(!list_empty(&dev->managed.resources));
f30c9257 842 kfree(dev);
c6603c74
DV
843 } else if (dev->managed.final_kfree)
844 kfree(dev->managed.final_kfree);
0dc8fe59 845}
099d1c29
DH
846
847/**
9a96f550 848 * drm_dev_get - Take reference of a DRM device
099d1c29
DH
849 * @dev: device to take reference of or NULL
850 *
851 * This increases the ref-count of @dev by one. You *must* already own a
9a96f550 852 * reference when calling this. Use drm_dev_put() to drop this reference
099d1c29
DH
853 * again.
854 *
855 * This function never fails. However, this function does not provide *any*
856 * guarantee whether the device is alive or running. It only provides a
857 * reference to the object and the memory associated with it.
858 */
9a96f550 859void drm_dev_get(struct drm_device *dev)
099d1c29
DH
860{
861 if (dev)
862 kref_get(&dev->ref);
863}
9a96f550 864EXPORT_SYMBOL(drm_dev_get);
099d1c29
DH
865
866/**
9a96f550 867 * drm_dev_put - Drop reference of a DRM device
099d1c29
DH
868 * @dev: device to drop reference of or NULL
869 *
870 * This decreases the ref-count of @dev by one. The device is destroyed if the
871 * ref-count drops to zero.
872 */
9a96f550 873void drm_dev_put(struct drm_device *dev)
099d1c29
DH
874{
875 if (dev)
876 kref_put(&dev->ref, drm_dev_release);
877}
9a96f550
AP
878EXPORT_SYMBOL(drm_dev_put);
879
6449b088
DV
880static int create_compat_control_link(struct drm_device *dev)
881{
882 struct drm_minor *minor;
883 char *name;
884 int ret;
885
886 if (!drm_core_check_feature(dev, DRIVER_MODESET))
887 return 0;
888
889 minor = *drm_minor_get_slot(dev, DRM_MINOR_PRIMARY);
890 if (!minor)
891 return 0;
892
893 /*
894 * Some existing userspace out there uses the existing of the controlD*
895 * sysfs files to figure out whether it's a modeset driver. It only does
896 * readdir, hence a symlink is sufficient (and the least confusing
897 * option). Otherwise controlD* is entirely unused.
898 *
899 * Old controlD chardev have been allocated in the range
900 * 64-127.
901 */
902 name = kasprintf(GFP_KERNEL, "controlD%d", minor->index + 64);
903 if (!name)
904 return -ENOMEM;
905
906 ret = sysfs_create_link(minor->kdev->kobj.parent,
907 &minor->kdev->kobj,
908 name);
909
910 kfree(name);
911
912 return ret;
913}
914
915static void remove_compat_control_link(struct drm_device *dev)
916{
917 struct drm_minor *minor;
918 char *name;
919
920 if (!drm_core_check_feature(dev, DRIVER_MODESET))
921 return;
922
923 minor = *drm_minor_get_slot(dev, DRM_MINOR_PRIMARY);
924 if (!minor)
925 return;
926
7f6df440 927 name = kasprintf(GFP_KERNEL, "controlD%d", minor->index + 64);
6449b088
DV
928 if (!name)
929 return;
930
931 sysfs_remove_link(minor->kdev->kobj.parent, name);
932
933 kfree(name);
934}
935
c22f0ace
DH
936/**
937 * drm_dev_register - Register DRM device
938 * @dev: Device to register
c6a1af8a 939 * @flags: Flags passed to the driver's .load() function
c22f0ace
DH
940 *
941 * Register the DRM device @dev with the system, advertise device to user-space
33e70110 942 * and start normal device operation. @dev must be initialized via drm_dev_init()
e28cd4d0 943 * previously.
c22f0ace
DH
944 *
945 * Never call this twice on any device!
946 *
6e3f797c 947 * NOTE: To ensure backward compatibility with existing drivers method this
ef40cbf9
DV
948 * function calls the &drm_driver.load method after registering the device
949 * nodes, creating race conditions. Usage of the &drm_driver.load methods is
950 * therefore deprecated, drivers must perform all initialization before calling
6e3f797c
DV
951 * drm_dev_register().
952 *
c22f0ace
DH
953 * RETURNS:
954 * 0 on success, negative error code on failure.
955 */
956int drm_dev_register(struct drm_device *dev, unsigned long flags)
957{
75f6dfe3 958 struct drm_driver *driver = dev->driver;
c22f0ace
DH
959 int ret;
960
71427795
VS
961 if (!driver->load)
962 drm_mode_config_validate(dev);
963
4017ad7b
DV
964 if (drm_dev_needs_global_mutex(dev))
965 mutex_lock(&drm_global_mutex);
c22f0ace 966
afcdbc86 967 ret = drm_minor_register(dev, DRM_MINOR_RENDER);
05b701f6
DH
968 if (ret)
969 goto err_minors;
c22f0ace 970
a3ccc461 971 ret = drm_minor_register(dev, DRM_MINOR_PRIMARY);
c22f0ace 972 if (ret)
05b701f6 973 goto err_minors;
c22f0ace 974
6449b088
DV
975 ret = create_compat_control_link(dev);
976 if (ret)
977 goto err_minors;
978
e0f32f78
DV
979 dev->registered = true;
980
c22f0ace
DH
981 if (dev->driver->load) {
982 ret = dev->driver->load(dev, flags);
983 if (ret)
05b701f6 984 goto err_minors;
c22f0ace
DH
985 }
986
bee7fb15 987 if (drm_core_check_feature(dev, DRIVER_MODESET))
79190ea2 988 drm_modeset_register_all(dev);
e28cd4d0 989
c22f0ace 990 ret = 0;
75f6dfe3
GKB
991
992 DRM_INFO("Initialized %s %d.%d.%d %s for %s on minor %d\n",
993 driver->name, driver->major, driver->minor,
6098909c
CW
994 driver->patchlevel, driver->date,
995 dev->dev ? dev_name(dev->dev) : "virtual device",
75f6dfe3
GKB
996 dev->primary->index);
997
c22f0ace
DH
998 goto out_unlock;
999
05b701f6 1000err_minors:
6449b088 1001 remove_compat_control_link(dev);
a3ccc461 1002 drm_minor_unregister(dev, DRM_MINOR_PRIMARY);
afcdbc86 1003 drm_minor_unregister(dev, DRM_MINOR_RENDER);
c22f0ace 1004out_unlock:
4017ad7b
DV
1005 if (drm_dev_needs_global_mutex(dev))
1006 mutex_unlock(&drm_global_mutex);
c22f0ace
DH
1007 return ret;
1008}
1009EXPORT_SYMBOL(drm_dev_register);
c3a49737
DH
1010
1011/**
1012 * drm_dev_unregister - Unregister DRM device
1013 * @dev: Device to unregister
1014 *
1015 * Unregister the DRM device from the system. This does the reverse of
1016 * drm_dev_register() but does not deallocate the device. The caller must call
9a96f550 1017 * drm_dev_put() to drop their final reference.
6e3f797c 1018 *
c07dcd61
DV
1019 * A special form of unregistering for hotpluggable devices is drm_dev_unplug(),
1020 * which can be called while there are still open users of @dev.
1021 *
6e3f797c
DV
1022 * This should be called first in the device teardown code to make sure
1023 * userspace can't access the device instance any more.
c3a49737
DH
1024 */
1025void drm_dev_unregister(struct drm_device *dev)
1026{
2e45eeac
DV
1027 if (drm_core_check_feature(dev, DRIVER_LEGACY))
1028 drm_lastclose(dev);
c3a49737 1029
e6e7b48b
DV
1030 dev->registered = false;
1031
c76f0f7c
NT
1032 drm_client_dev_unregister(dev);
1033
bee7fb15 1034 if (drm_core_check_feature(dev, DRIVER_MODESET))
79190ea2 1035 drm_modeset_unregister_all(dev);
e28cd4d0 1036
c3a49737
DH
1037 if (dev->driver->unload)
1038 dev->driver->unload(dev);
1039
4efafebe
DV
1040 if (dev->agp)
1041 drm_pci_agp_destroy(dev);
c3a49737 1042
35a28021 1043 drm_legacy_rmmaps(dev);
c3a49737 1044
6449b088 1045 remove_compat_control_link(dev);
a3ccc461 1046 drm_minor_unregister(dev, DRM_MINOR_PRIMARY);
afcdbc86 1047 drm_minor_unregister(dev, DRM_MINOR_RENDER);
c3a49737
DH
1048}
1049EXPORT_SYMBOL(drm_dev_unregister);
ca8e2ad7 1050
a6b5fac5
GH
1051/**
1052 * drm_dev_set_unique - Set the unique name of a DRM device
1053 * @dev: device of which to set the unique name
1054 * @name: unique name
1055 *
33e70110
DV
1056 * Sets the unique name of a DRM device using the specified string. This is
1057 * already done by drm_dev_init(), drivers should only override the default
1058 * unique name for backwards compatibility reasons.
a6b5fac5
GH
1059 *
1060 * Return: 0 on success or a negative error code on failure.
1061 */
1062int drm_dev_set_unique(struct drm_device *dev, const char *name)
1063{
1064 kfree(dev->unique);
1065 dev->unique = kstrdup(name, GFP_KERNEL);
1066
1067 return dev->unique ? 0 : -ENOMEM;
1068}
1069EXPORT_SYMBOL(drm_dev_set_unique);
1070
1b7199fe
DH
1071/*
1072 * DRM Core
1073 * The DRM core module initializes all global DRM objects and makes them
1074 * available to drivers. Once setup, drivers can probe their respective
1075 * devices.
1076 * Currently, core management includes:
1077 * - The "DRM-Global" key/value database
1078 * - Global ID management for connectors
1079 * - DRM major number allocation
1080 * - DRM minor management
1081 * - DRM sysfs class
1082 * - DRM debugfs root
1083 *
1084 * Furthermore, the DRM core provides dynamic char-dev lookups. For each
1085 * interface registered on a DRM device, you can request minor numbers from DRM
1086 * core. DRM core takes care of major-number management and char-dev
1087 * registration. A stub ->open() callback forwards any open() requests to the
1088 * registered minor.
1089 */
1090
1091static int drm_stub_open(struct inode *inode, struct file *filp)
1092{
1093 const struct file_operations *new_fops;
1094 struct drm_minor *minor;
1095 int err;
1096
1097 DRM_DEBUG("\n");
1098
1b7199fe 1099 minor = drm_minor_acquire(iminor(inode));
591a2abf
DV
1100 if (IS_ERR(minor))
1101 return PTR_ERR(minor);
1b7199fe
DH
1102
1103 new_fops = fops_get(minor->dev->driver->fops);
1104 if (!new_fops) {
1105 err = -ENODEV;
591a2abf 1106 goto out;
1b7199fe
DH
1107 }
1108
1109 replace_fops(filp, new_fops);
1110 if (filp->f_op->open)
1111 err = filp->f_op->open(inode, filp);
1112 else
1113 err = 0;
1114
591a2abf 1115out:
1b7199fe 1116 drm_minor_release(minor);
591a2abf 1117
1b7199fe
DH
1118 return err;
1119}
1120
1121static const struct file_operations drm_stub_fops = {
1122 .owner = THIS_MODULE,
1123 .open = drm_stub_open,
1124 .llseek = noop_llseek,
1125};
1126
2cc107dc
DH
1127static void drm_core_exit(void)
1128{
1129 unregister_chrdev(DRM_MAJOR, "drm");
1130 debugfs_remove(drm_debugfs_root);
1131 drm_sysfs_destroy();
1132 idr_destroy(&drm_minors_idr);
1133 drm_connector_ida_destroy();
2cc107dc
DH
1134}
1135
1b7199fe
DH
1136static int __init drm_core_init(void)
1137{
2cc107dc 1138 int ret;
1b7199fe 1139
1b7199fe
DH
1140 drm_connector_ida_init();
1141 idr_init(&drm_minors_idr);
1142
fcc90213
DH
1143 ret = drm_sysfs_init();
1144 if (ret < 0) {
2cc107dc
DH
1145 DRM_ERROR("Cannot create DRM class: %d\n", ret);
1146 goto error;
1b7199fe
DH
1147 }
1148
1149 drm_debugfs_root = debugfs_create_dir("dri", NULL);
1b7199fe 1150
2cc107dc
DH
1151 ret = register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops);
1152 if (ret < 0)
1153 goto error;
1154
371c2279
AM
1155 drm_core_init_complete = true;
1156
e82dfa00 1157 DRM_DEBUG("Initialized\n");
1b7199fe 1158 return 0;
1b7199fe 1159
2cc107dc
DH
1160error:
1161 drm_core_exit();
1b7199fe
DH
1162 return ret;
1163}
1164
1b7199fe
DH
1165module_init(drm_core_init);
1166module_exit(drm_core_exit);