forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/drivers/gpu/drm/drm_drv.c
....@@ -31,38 +31,25 @@
3131 #include <linux/module.h>
3232 #include <linux/moduleparam.h>
3333 #include <linux/mount.h>
34
+#include <linux/pseudo_fs.h>
3435 #include <linux/slab.h>
3536 #include <linux/srcu.h>
3637
3738 #include <drm/drm_client.h>
39
+#include <drm/drm_color_mgmt.h>
3840 #include <drm/drm_drv.h>
39
-#include <drm/drmP.h>
41
+#include <drm/drm_file.h>
42
+#include <drm/drm_managed.h>
43
+#include <drm/drm_mode_object.h>
44
+#include <drm/drm_print.h>
4045
4146 #include "drm_crtc_internal.h"
42
-#include "drm_legacy.h"
4347 #include "drm_internal.h"
44
-#include "drm_crtc_internal.h"
45
-
46
-/*
47
- * drm_debug: Enable debug output.
48
- * Bitmask of DRM_UT_x. See include/drm/drmP.h for details.
49
- */
50
-unsigned int drm_debug = 0;
51
-EXPORT_SYMBOL(drm_debug);
48
+#include "drm_legacy.h"
5249
5350 MODULE_AUTHOR("Gareth Hughes, Leif Delgass, José Fonseca, Jon Smirl");
5451 MODULE_DESCRIPTION("DRM shared core routines");
5552 MODULE_LICENSE("GPL and additional rights");
56
-MODULE_PARM_DESC(debug, "Enable debug output, where each bit enables a debug category.\n"
57
-"\t\tBit 0 (0x01) will enable CORE messages (drm core code)\n"
58
-"\t\tBit 1 (0x02) will enable DRIVER messages (drm controller code)\n"
59
-"\t\tBit 2 (0x04) will enable KMS messages (modesetting code)\n"
60
-"\t\tBit 3 (0x08) will enable PRIME messages (prime code)\n"
61
-"\t\tBit 4 (0x10) will enable ATOMIC messages (atomic code)\n"
62
-"\t\tBit 5 (0x20) will enable VBL messages (vblank code)\n"
63
-"\t\tBit 7 (0x80) will enable LEASE messages (leasing code)\n"
64
-"\t\tBit 8 (0x100) will enable DP messages (displayport code)");
65
-module_param_named(debug, drm_debug, int, 0600);
6653
6754 static DEFINE_SPINLOCK(drm_minor_lock);
6855 static struct idr drm_minors_idr;
....@@ -106,13 +93,27 @@
10693 }
10794 }
10895
96
+static void drm_minor_alloc_release(struct drm_device *dev, void *data)
97
+{
98
+ struct drm_minor *minor = data;
99
+ unsigned long flags;
100
+
101
+ WARN_ON(dev != minor->dev);
102
+
103
+ put_device(minor->kdev);
104
+
105
+ spin_lock_irqsave(&drm_minor_lock, flags);
106
+ idr_remove(&drm_minors_idr, minor->index);
107
+ spin_unlock_irqrestore(&drm_minor_lock, flags);
108
+}
109
+
109110 static int drm_minor_alloc(struct drm_device *dev, unsigned int type)
110111 {
111112 struct drm_minor *minor;
112113 unsigned long flags;
113114 int r;
114115
115
- minor = kzalloc(sizeof(*minor), GFP_KERNEL);
116
+ minor = drmm_kzalloc(dev, sizeof(*minor), GFP_KERNEL);
116117 if (!minor)
117118 return -ENOMEM;
118119
....@@ -130,46 +131,20 @@
130131 idr_preload_end();
131132
132133 if (r < 0)
133
- goto err_free;
134
+ return r;
134135
135136 minor->index = r;
136137
138
+ r = drmm_add_action_or_reset(dev, drm_minor_alloc_release, minor);
139
+ if (r)
140
+ return r;
141
+
137142 minor->kdev = drm_sysfs_minor_alloc(minor);
138
- if (IS_ERR(minor->kdev)) {
139
- r = PTR_ERR(minor->kdev);
140
- goto err_index;
141
- }
143
+ if (IS_ERR(minor->kdev))
144
+ return PTR_ERR(minor->kdev);
142145
143146 *drm_minor_get_slot(dev, type) = minor;
144147 return 0;
145
-
146
-err_index:
147
- spin_lock_irqsave(&drm_minor_lock, flags);
148
- idr_remove(&drm_minors_idr, minor->index);
149
- spin_unlock_irqrestore(&drm_minor_lock, flags);
150
-err_free:
151
- kfree(minor);
152
- return r;
153
-}
154
-
155
-static void drm_minor_free(struct drm_device *dev, unsigned int type)
156
-{
157
- struct drm_minor **slot, *minor;
158
- unsigned long flags;
159
-
160
- slot = drm_minor_get_slot(dev, type);
161
- minor = *slot;
162
- if (!minor)
163
- return;
164
-
165
- put_device(minor->kdev);
166
-
167
- spin_lock_irqsave(&drm_minor_lock, flags);
168
- idr_remove(&drm_minors_idr, minor->index);
169
- spin_unlock_irqrestore(&drm_minor_lock, flags);
170
-
171
- kfree(minor);
172
- *slot = NULL;
173148 }
174149
175150 static int drm_minor_register(struct drm_device *dev, unsigned int type)
....@@ -261,40 +236,17 @@
261236 drm_dev_put(minor->dev);
262237 }
263238
264
-struct drm_device *drm_device_get_by_name(const char *name)
265
-{
266
- int i;
267
-
268
- for (i = 0; i < 64; i++) {
269
- struct drm_minor *minor;
270
-
271
- minor = drm_minor_acquire(i + DRM_MINOR_PRIMARY);
272
- if (IS_ERR(minor))
273
- continue;
274
- if (!minor->dev || !minor->dev->driver ||
275
- !minor->dev->driver->name)
276
- continue;
277
- if (!name)
278
- return minor->dev;
279
- if (!strcmp(name, minor->dev->driver->name))
280
- return minor->dev;
281
- }
282
-
283
- return NULL;
284
-}
285
-
286239 /**
287240 * DOC: driver instance overview
288241 *
289242 * A device instance for a drm driver is represented by &struct drm_device. This
290
- * is allocated with drm_dev_alloc(), usually from bus-specific ->probe()
291
- * callbacks implemented by the driver. The driver then needs to initialize all
292
- * the various subsystems for the drm device like memory management, vblank
293
- * handling, modesetting support and intial output configuration plus obviously
294
- * initialize all the corresponding hardware bits. An important part of this is
295
- * also calling drm_dev_set_unique() to set the userspace-visible unique name of
296
- * this device instance. Finally when everything is up and running and ready for
297
- * userspace the device instance can be published using drm_dev_register().
243
+ * is allocated and initialized with devm_drm_dev_alloc(), usually from
244
+ * bus-specific ->probe() callbacks implemented by the driver. The driver then
245
+ * needs to initialize all the various subsystems for the drm device like memory
246
+ * management, vblank handling, modesetting support and initial output
247
+ * configuration plus obviously initialize all the corresponding hardware bits.
248
+ * Finally when everything is up and running and ready for userspace the device
249
+ * instance can be published using drm_dev_register().
298250 *
299251 * There is also deprecated support for initalizing device instances using
300252 * bus-specific helpers and the &drm_driver.load callback. But due to
....@@ -307,12 +259,126 @@
307259 * any other resources allocated at device initialization and drop the driver's
308260 * reference to &drm_device using drm_dev_put().
309261 *
310
- * Note that the lifetime rules for &drm_device instance has still a lot of
311
- * historical baggage. Hence use the reference counting provided by
312
- * drm_dev_get() and drm_dev_put() only carefully.
262
+ * Note that any allocation or resource which is visible to userspace must be
263
+ * released only when the final drm_dev_put() is called, and not when the
264
+ * driver is unbound from the underlying physical struct &device. Best to use
265
+ * &drm_device managed resources with drmm_add_action(), drmm_kmalloc() and
266
+ * related functions.
313267 *
314
- * It is recommended that drivers embed &struct drm_device into their own device
315
- * structure, which is supported through drm_dev_init().
268
+ * devres managed resources like devm_kmalloc() can only be used for resources
269
+ * directly related to the underlying hardware device, and only used in code
270
+ * paths fully protected by drm_dev_enter() and drm_dev_exit().
271
+ *
272
+ * Display driver example
273
+ * ~~~~~~~~~~~~~~~~~~~~~~
274
+ *
275
+ * The following example shows a typical structure of a DRM display driver.
276
+ * The example focus on the probe() function and the other functions that is
277
+ * almost always present and serves as a demonstration of devm_drm_dev_alloc().
278
+ *
279
+ * .. code-block:: c
280
+ *
281
+ * struct driver_device {
282
+ * struct drm_device drm;
283
+ * void *userspace_facing;
284
+ * struct clk *pclk;
285
+ * };
286
+ *
287
+ * static struct drm_driver driver_drm_driver = {
288
+ * [...]
289
+ * };
290
+ *
291
+ * static int driver_probe(struct platform_device *pdev)
292
+ * {
293
+ * struct driver_device *priv;
294
+ * struct drm_device *drm;
295
+ * int ret;
296
+ *
297
+ * priv = devm_drm_dev_alloc(&pdev->dev, &driver_drm_driver,
298
+ * struct driver_device, drm);
299
+ * if (IS_ERR(priv))
300
+ * return PTR_ERR(priv);
301
+ * drm = &priv->drm;
302
+ *
303
+ * ret = drmm_mode_config_init(drm);
304
+ * if (ret)
305
+ * return ret;
306
+ *
307
+ * priv->userspace_facing = drmm_kzalloc(..., GFP_KERNEL);
308
+ * if (!priv->userspace_facing)
309
+ * return -ENOMEM;
310
+ *
311
+ * priv->pclk = devm_clk_get(dev, "PCLK");
312
+ * if (IS_ERR(priv->pclk))
313
+ * return PTR_ERR(priv->pclk);
314
+ *
315
+ * // Further setup, display pipeline etc
316
+ *
317
+ * platform_set_drvdata(pdev, drm);
318
+ *
319
+ * drm_mode_config_reset(drm);
320
+ *
321
+ * ret = drm_dev_register(drm);
322
+ * if (ret)
323
+ * return ret;
324
+ *
325
+ * drm_fbdev_generic_setup(drm, 32);
326
+ *
327
+ * return 0;
328
+ * }
329
+ *
330
+ * // This function is called before the devm_ resources are released
331
+ * static int driver_remove(struct platform_device *pdev)
332
+ * {
333
+ * struct drm_device *drm = platform_get_drvdata(pdev);
334
+ *
335
+ * drm_dev_unregister(drm);
336
+ * drm_atomic_helper_shutdown(drm)
337
+ *
338
+ * return 0;
339
+ * }
340
+ *
341
+ * // This function is called on kernel restart and shutdown
342
+ * static void driver_shutdown(struct platform_device *pdev)
343
+ * {
344
+ * drm_atomic_helper_shutdown(platform_get_drvdata(pdev));
345
+ * }
346
+ *
347
+ * static int __maybe_unused driver_pm_suspend(struct device *dev)
348
+ * {
349
+ * return drm_mode_config_helper_suspend(dev_get_drvdata(dev));
350
+ * }
351
+ *
352
+ * static int __maybe_unused driver_pm_resume(struct device *dev)
353
+ * {
354
+ * drm_mode_config_helper_resume(dev_get_drvdata(dev));
355
+ *
356
+ * return 0;
357
+ * }
358
+ *
359
+ * static const struct dev_pm_ops driver_pm_ops = {
360
+ * SET_SYSTEM_SLEEP_PM_OPS(driver_pm_suspend, driver_pm_resume)
361
+ * };
362
+ *
363
+ * static struct platform_driver driver_driver = {
364
+ * .driver = {
365
+ * [...]
366
+ * .pm = &driver_pm_ops,
367
+ * },
368
+ * .probe = driver_probe,
369
+ * .remove = driver_remove,
370
+ * .shutdown = driver_shutdown,
371
+ * };
372
+ * module_platform_driver(driver_driver);
373
+ *
374
+ * Drivers that want to support device unplugging (USB, DT overlay unload) should
375
+ * use drm_dev_unplug() instead of drm_dev_unregister(). The driver must protect
376
+ * regions that is accessing device resources to prevent use after they're
377
+ * released. This is done using drm_dev_enter() and drm_dev_exit(). There is one
378
+ * shortcoming however, drm_dev_unplug() marks the drm_device as unplugged before
379
+ * drm_atomic_helper_shutdown() is called. This means that if the disable code
380
+ * paths are protected, they will not run on regular driver module unload,
381
+ * possibily leaving the hardware enabled.
316382 */
317383
318384 /**
....@@ -403,7 +469,6 @@
403469 synchronize_srcu(&drm_unplug_srcu);
404470
405471 drm_dev_unregister(dev);
406
- drm_dev_put(dev);
407472 }
408473 EXPORT_SYMBOL(drm_dev_unplug);
409474
....@@ -428,28 +493,15 @@
428493 static int drm_fs_cnt;
429494 static struct vfsmount *drm_fs_mnt;
430495
431
-static const struct dentry_operations drm_fs_dops = {
432
- .d_dname = simple_dname,
433
-};
434
-
435
-static const struct super_operations drm_fs_sops = {
436
- .statfs = simple_statfs,
437
-};
438
-
439
-static struct dentry *drm_fs_mount(struct file_system_type *fs_type, int flags,
440
- const char *dev_name, void *data)
496
+static int drm_fs_init_fs_context(struct fs_context *fc)
441497 {
442
- return mount_pseudo(fs_type,
443
- "drm:",
444
- &drm_fs_sops,
445
- &drm_fs_dops,
446
- 0x010203ff);
498
+ return init_pseudo(fc, 0x010203ff) ? 0 : -ENOMEM;
447499 }
448500
449501 static struct file_system_type drm_fs_type = {
450502 .name = "drm",
451503 .owner = THIS_MODULE,
452
- .mount = drm_fs_mount,
504
+ .init_fs_context = drm_fs_init_fs_context,
453505 .kill_sb = kill_anon_super,
454506 };
455507
....@@ -480,39 +532,52 @@
480532 }
481533
482534 /**
483
- * drm_dev_init - Initialise new DRM device
484
- * @dev: DRM device
485
- * @driver: DRM driver
486
- * @parent: Parent device object
535
+ * DOC: component helper usage recommendations
487536 *
488
- * Initialize a new DRM device. No device registration is done.
489
- * Call drm_dev_register() to advertice the device to user space and register it
490
- * with other core subsystems. This should be done last in the device
491
- * initialization sequence to make sure userspace can't access an inconsistent
492
- * state.
537
+ * DRM drivers that drive hardware where a logical device consists of a pile of
538
+ * independent hardware blocks are recommended to use the :ref:`component helper
539
+ * library<component>`. For consistency and better options for code reuse the
540
+ * following guidelines apply:
493541 *
494
- * The initial ref-count of the object is 1. Use drm_dev_get() and
495
- * drm_dev_put() to take and drop further ref-counts.
542
+ * - The entire device initialization procedure should be run from the
543
+ * &component_master_ops.master_bind callback, starting with
544
+ * devm_drm_dev_alloc(), then binding all components with
545
+ * component_bind_all() and finishing with drm_dev_register().
496546 *
497
- * Note that for purely virtual devices @parent can be NULL.
547
+ * - The opaque pointer passed to all components through component_bind_all()
548
+ * should point at &struct drm_device of the device instance, not some driver
549
+ * specific private structure.
498550 *
499
- * Drivers that do not want to allocate their own device struct
500
- * embedding &struct drm_device can call drm_dev_alloc() instead. For drivers
501
- * that do embed &struct drm_device it must be placed first in the overall
502
- * structure, and the overall structure must be allocated using kmalloc(): The
503
- * drm core's release function unconditionally calls kfree() on the @dev pointer
504
- * when the final reference is released. To override this behaviour, and so
505
- * allow embedding of the drm_device inside the driver's device struct at an
506
- * arbitrary offset, you must supply a &drm_driver.release callback and control
507
- * the finalization explicitly.
508
- *
509
- * RETURNS:
510
- * 0 on success, or error code on failure.
551
+ * - The component helper fills the niche where further standardization of
552
+ * interfaces is not practical. When there already is, or will be, a
553
+ * standardized interface like &drm_bridge or &drm_panel, providing its own
554
+ * functions to find such components at driver load time, like
555
+ * drm_of_find_panel_or_bridge(), then the component helper should not be
556
+ * used.
511557 */
512
-int drm_dev_init(struct drm_device *dev,
513
- struct drm_driver *driver,
514
- struct device *parent)
558
+
559
+static void drm_dev_init_release(struct drm_device *dev, void *res)
515560 {
561
+ drm_legacy_ctxbitmap_cleanup(dev);
562
+ drm_legacy_remove_map_hash(dev);
563
+ drm_fs_inode_free(dev->anon_inode);
564
+
565
+ put_device(dev->dev);
566
+ /* Prevent use-after-free in drm_managed_release when debugging is
567
+ * enabled. Slightly awkward, but can't really be helped. */
568
+ dev->dev = NULL;
569
+ mutex_destroy(&dev->master_mutex);
570
+ mutex_destroy(&dev->clientlist_mutex);
571
+ mutex_destroy(&dev->filelist_mutex);
572
+ mutex_destroy(&dev->struct_mutex);
573
+ drm_legacy_destroy_members(dev);
574
+}
575
+
576
+static int drm_dev_init(struct drm_device *dev,
577
+ struct drm_driver *driver,
578
+ struct device *parent)
579
+{
580
+ struct inode *inode;
516581 int ret;
517582
518583 if (!drm_core_init_complete) {
....@@ -520,46 +585,57 @@
520585 return -ENODEV;
521586 }
522587
588
+ if (WARN_ON(!parent))
589
+ return -EINVAL;
590
+
523591 kref_init(&dev->ref);
524592 dev->dev = get_device(parent);
525593 dev->driver = driver;
526594
595
+ INIT_LIST_HEAD(&dev->managed.resources);
596
+ spin_lock_init(&dev->managed.lock);
597
+
598
+ /* no per-device feature limits by default */
599
+ dev->driver_features = ~0u;
600
+
601
+ drm_legacy_init_members(dev);
527602 INIT_LIST_HEAD(&dev->filelist);
528603 INIT_LIST_HEAD(&dev->filelist_internal);
529604 INIT_LIST_HEAD(&dev->clientlist);
530
- INIT_LIST_HEAD(&dev->ctxlist);
531
- INIT_LIST_HEAD(&dev->vmalist);
532
- INIT_LIST_HEAD(&dev->maplist);
533605 INIT_LIST_HEAD(&dev->vblank_event_list);
534606
535
- spin_lock_init(&dev->buf_lock);
536607 spin_lock_init(&dev->event_lock);
537608 mutex_init(&dev->struct_mutex);
538609 mutex_init(&dev->filelist_mutex);
539610 mutex_init(&dev->clientlist_mutex);
540
- mutex_init(&dev->ctxlist_mutex);
541611 mutex_init(&dev->master_mutex);
542612
543
- dev->anon_inode = drm_fs_inode_new();
544
- if (IS_ERR(dev->anon_inode)) {
545
- ret = PTR_ERR(dev->anon_inode);
613
+ ret = drmm_add_action_or_reset(dev, drm_dev_init_release, NULL);
614
+ if (ret)
615
+ return ret;
616
+
617
+ inode = drm_fs_inode_new();
618
+ if (IS_ERR(inode)) {
619
+ ret = PTR_ERR(inode);
546620 DRM_ERROR("Cannot allocate anonymous inode: %d\n", ret);
547
- goto err_free;
621
+ goto err;
548622 }
623
+
624
+ dev->anon_inode = inode;
549625
550626 if (drm_core_check_feature(dev, DRIVER_RENDER)) {
551627 ret = drm_minor_alloc(dev, DRM_MINOR_RENDER);
552628 if (ret)
553
- goto err_minors;
629
+ goto err;
554630 }
555631
556632 ret = drm_minor_alloc(dev, DRM_MINOR_PRIMARY);
557633 if (ret)
558
- goto err_minors;
634
+ goto err;
559635
560
- ret = drm_ht_create(&dev->map_hash, 12);
636
+ ret = drm_legacy_create_map_hash(dev);
561637 if (ret)
562
- goto err_minors;
638
+ goto err;
563639
564640 drm_legacy_ctxbitmap_init(dev);
565641
....@@ -567,94 +643,75 @@
567643 ret = drm_gem_init(dev);
568644 if (ret) {
569645 DRM_ERROR("Cannot initialize graphics execution manager (GEM)\n");
570
- goto err_ctxbitmap;
646
+ goto err;
571647 }
572648 }
573649
574
- /* Use the parent device name as DRM device unique identifier, but fall
575
- * back to the driver name for virtual devices like vgem. */
576
- ret = drm_dev_set_unique(dev, parent ? dev_name(parent) : driver->name);
650
+ ret = drm_dev_set_unique(dev, dev_name(parent));
577651 if (ret)
578
- goto err_setunique;
652
+ goto err;
579653
580654 return 0;
581655
582
-err_setunique:
583
- if (drm_core_check_feature(dev, DRIVER_GEM))
584
- drm_gem_destroy(dev);
585
-err_ctxbitmap:
586
- drm_legacy_ctxbitmap_cleanup(dev);
587
- drm_ht_remove(&dev->map_hash);
588
-err_minors:
589
- drm_minor_free(dev, DRM_MINOR_PRIMARY);
590
- drm_minor_free(dev, DRM_MINOR_RENDER);
591
- drm_fs_inode_free(dev->anon_inode);
592
-err_free:
593
- put_device(dev->dev);
594
- mutex_destroy(&dev->master_mutex);
595
- mutex_destroy(&dev->ctxlist_mutex);
596
- mutex_destroy(&dev->clientlist_mutex);
597
- mutex_destroy(&dev->filelist_mutex);
598
- mutex_destroy(&dev->struct_mutex);
656
+err:
657
+ drm_managed_release(dev);
658
+
599659 return ret;
600660 }
601
-EXPORT_SYMBOL(drm_dev_init);
602661
603
-/**
604
- * drm_dev_fini - Finalize a dead DRM device
605
- * @dev: DRM device
606
- *
607
- * Finalize a dead DRM device. This is the converse to drm_dev_init() and
608
- * frees up all data allocated by it. All driver private data should be
609
- * finalized first. Note that this function does not free the @dev, that is
610
- * left to the caller.
611
- *
612
- * The ref-count of @dev must be zero, and drm_dev_fini() should only be called
613
- * from a &drm_driver.release callback.
614
- */
615
-void drm_dev_fini(struct drm_device *dev)
662
+static void devm_drm_dev_init_release(void *data)
616663 {
617
- drm_vblank_cleanup(dev);
618
-
619
- if (drm_core_check_feature(dev, DRIVER_GEM))
620
- drm_gem_destroy(dev);
621
-
622
- drm_legacy_ctxbitmap_cleanup(dev);
623
- drm_ht_remove(&dev->map_hash);
624
- drm_fs_inode_free(dev->anon_inode);
625
-
626
- drm_minor_free(dev, DRM_MINOR_PRIMARY);
627
- drm_minor_free(dev, DRM_MINOR_RENDER);
628
-
629
- put_device(dev->dev);
630
-
631
- mutex_destroy(&dev->master_mutex);
632
- mutex_destroy(&dev->ctxlist_mutex);
633
- mutex_destroy(&dev->clientlist_mutex);
634
- mutex_destroy(&dev->filelist_mutex);
635
- mutex_destroy(&dev->struct_mutex);
636
- kfree(dev->unique);
664
+ drm_dev_put(data);
637665 }
638
-EXPORT_SYMBOL(drm_dev_fini);
666
+
667
+static int devm_drm_dev_init(struct device *parent,
668
+ struct drm_device *dev,
669
+ struct drm_driver *driver)
670
+{
671
+ int ret;
672
+
673
+ ret = drm_dev_init(dev, driver, parent);
674
+ if (ret)
675
+ return ret;
676
+
677
+ ret = devm_add_action(parent, devm_drm_dev_init_release, dev);
678
+ if (ret)
679
+ devm_drm_dev_init_release(dev);
680
+
681
+ return ret;
682
+}
683
+
684
+void *__devm_drm_dev_alloc(struct device *parent, struct drm_driver *driver,
685
+ size_t size, size_t offset)
686
+{
687
+ void *container;
688
+ struct drm_device *drm;
689
+ int ret;
690
+
691
+ container = kzalloc(size, GFP_KERNEL);
692
+ if (!container)
693
+ return ERR_PTR(-ENOMEM);
694
+
695
+ drm = container + offset;
696
+ ret = devm_drm_dev_init(parent, drm, driver);
697
+ if (ret) {
698
+ kfree(container);
699
+ return ERR_PTR(ret);
700
+ }
701
+ drmm_add_final_kfree(drm, container);
702
+
703
+ return container;
704
+}
705
+EXPORT_SYMBOL(__devm_drm_dev_alloc);
639706
640707 /**
641708 * drm_dev_alloc - Allocate new DRM device
642709 * @driver: DRM driver to allocate device for
643710 * @parent: Parent device object
644711 *
645
- * Allocate and initialize a new DRM device. No device registration is done.
646
- * Call drm_dev_register() to advertice the device to user space and register it
647
- * with other core subsystems. This should be done last in the device
648
- * initialization sequence to make sure userspace can't access an inconsistent
649
- * state.
650
- *
651
- * The initial ref-count of the object is 1. Use drm_dev_get() and
652
- * drm_dev_put() to take and drop further ref-counts.
653
- *
654
- * Note that for purely virtual devices @parent can be NULL.
655
- *
656
- * Drivers that wish to subclass or embed &struct drm_device into their
657
- * own struct should look at using drm_dev_init() instead.
712
+ * This is the deprecated version of devm_drm_dev_alloc(), which does not support
713
+ * subclassing through embedding the struct &drm_device in a driver private
714
+ * structure, and which does not support automatic cleanup through devres.
658715 *
659716 * RETURNS:
660717 * Pointer to new DRM device, or ERR_PTR on failure.
....@@ -675,6 +732,8 @@
675732 return ERR_PTR(ret);
676733 }
677734
735
+ drmm_add_final_kfree(dev, dev);
736
+
678737 return dev;
679738 }
680739 EXPORT_SYMBOL(drm_dev_alloc);
....@@ -683,12 +742,12 @@
683742 {
684743 struct drm_device *dev = container_of(ref, struct drm_device, ref);
685744
686
- if (dev->driver->release) {
745
+ if (dev->driver->release)
687746 dev->driver->release(dev);
688
- } else {
689
- drm_dev_fini(dev);
690
- kfree(dev);
691
- }
747
+
748
+ drm_managed_release(dev);
749
+
750
+ kfree(dev->managed.final_kfree);
692751 }
693752
694753 /**
....@@ -723,19 +782,6 @@
723782 kref_put(&dev->ref, drm_dev_release);
724783 }
725784 EXPORT_SYMBOL(drm_dev_put);
726
-
727
-/**
728
- * drm_dev_unref - Drop reference of a DRM device
729
- * @dev: device to drop reference of or NULL
730
- *
731
- * This is a compatibility alias for drm_dev_put() and should not be used by new
732
- * code.
733
- */
734
-void drm_dev_unref(struct drm_device *dev)
735
-{
736
- drm_dev_put(dev);
737
-}
738
-EXPORT_SYMBOL(drm_dev_unref);
739785
740786 static int create_compat_control_link(struct drm_device *dev)
741787 {
....@@ -799,7 +845,7 @@
799845 * @flags: Flags passed to the driver's .load() function
800846 *
801847 * Register the DRM device @dev with the system, advertise device to user-space
802
- * and start normal device operation. @dev must be allocated via drm_dev_alloc()
848
+ * and start normal device operation. @dev must be initialized via drm_dev_init()
803849 * previously.
804850 *
805851 * Never call this twice on any device!
....@@ -818,7 +864,13 @@
818864 struct drm_driver *driver = dev->driver;
819865 int ret;
820866
821
- mutex_lock(&drm_global_mutex);
867
+ if (!driver->load)
868
+ drm_mode_config_validate(dev);
869
+
870
+ WARN_ON(!dev->managed.final_kfree);
871
+
872
+ if (drm_dev_needs_global_mutex(dev))
873
+ mutex_lock(&drm_global_mutex);
822874
823875 ret = drm_minor_register(dev, DRM_MINOR_RENDER);
824876 if (ret)
....@@ -858,7 +910,8 @@
858910 drm_minor_unregister(dev, DRM_MINOR_PRIMARY);
859911 drm_minor_unregister(dev, DRM_MINOR_RENDER);
860912 out_unlock:
861
- mutex_unlock(&drm_global_mutex);
913
+ if (drm_dev_needs_global_mutex(dev))
914
+ mutex_unlock(&drm_global_mutex);
862915 return ret;
863916 }
864917 EXPORT_SYMBOL(drm_dev_register);
....@@ -879,8 +932,6 @@
879932 */
880933 void drm_dev_unregister(struct drm_device *dev)
881934 {
882
- struct drm_map_list *r_list, *list_temp;
883
-
884935 if (drm_core_check_feature(dev, DRIVER_LEGACY))
885936 drm_lastclose(dev);
886937
....@@ -897,8 +948,7 @@
897948 if (dev->agp)
898949 drm_pci_agp_destroy(dev);
899950
900
- list_for_each_entry_safe(r_list, list_temp, &dev->maplist, head)
901
- drm_legacy_rmmap(dev, r_list->map);
951
+ drm_legacy_rmmaps(dev);
902952
903953 remove_compat_control_link(dev);
904954 drm_minor_unregister(dev, DRM_MINOR_PRIMARY);
....@@ -911,16 +961,16 @@
911961 * @dev: device of which to set the unique name
912962 * @name: unique name
913963 *
914
- * Sets the unique name of a DRM device using the specified string. Drivers
915
- * can use this at driver probe time if the unique name of the devices they
916
- * drive is static.
964
+ * Sets the unique name of a DRM device using the specified string. This is
965
+ * already done by drm_dev_init(), drivers should only override the default
966
+ * unique name for backwards compatibility reasons.
917967 *
918968 * Return: 0 on success or a negative error code on failure.
919969 */
920970 int drm_dev_set_unique(struct drm_device *dev, const char *name)
921971 {
922
- kfree(dev->unique);
923
- dev->unique = kstrdup(name, GFP_KERNEL);
972
+ drmm_kfree(dev, dev->unique);
973
+ dev->unique = drmm_kstrdup(dev, name, GFP_KERNEL);
924974
925975 return dev->unique ? 0 : -ENOMEM;
926976 }
....@@ -954,17 +1004,14 @@
9541004
9551005 DRM_DEBUG("\n");
9561006
957
- mutex_lock(&drm_global_mutex);
9581007 minor = drm_minor_acquire(iminor(inode));
959
- if (IS_ERR(minor)) {
960
- err = PTR_ERR(minor);
961
- goto out_unlock;
962
- }
1008
+ if (IS_ERR(minor))
1009
+ return PTR_ERR(minor);
9631010
9641011 new_fops = fops_get(minor->dev->driver->fops);
9651012 if (!new_fops) {
9661013 err = -ENODEV;
967
- goto out_release;
1014
+ goto out;
9681015 }
9691016
9701017 replace_fops(filp, new_fops);
....@@ -973,10 +1020,9 @@
9731020 else
9741021 err = 0;
9751022
976
-out_release:
1023
+out:
9771024 drm_minor_release(minor);
978
-out_unlock:
979
- mutex_unlock(&drm_global_mutex);
1025
+
9801026 return err;
9811027 }
9821028
....@@ -993,14 +1039,12 @@
9931039 drm_sysfs_destroy();
9941040 idr_destroy(&drm_minors_idr);
9951041 drm_connector_ida_destroy();
996
- drm_global_release();
9971042 }
9981043
9991044 static int __init drm_core_init(void)
10001045 {
10011046 int ret;
10021047
1003
- drm_global_init();
10041048 drm_connector_ida_init();
10051049 idr_init(&drm_minors_idr);
10061050
....@@ -1011,11 +1055,6 @@
10111055 }
10121056
10131057 drm_debugfs_root = debugfs_create_dir("dri", NULL);
1014
- if (!drm_debugfs_root) {
1015
- ret = -ENOMEM;
1016
- DRM_ERROR("Cannot create debugfs-root: %d\n", ret);
1017
- goto error;
1018
- }
10191058
10201059 ret = register_chrdev(DRM_MAJOR, "drm", &drm_stub_fops);
10211060 if (ret < 0)
....@@ -1031,8 +1070,8 @@
10311070 return ret;
10321071 }
10331072
1034
-#ifdef CONFIG_ROCKCHIP_THUNDER_BOOT
1035
-subsys_initcall(drm_core_init);
1073
+#ifdef CONFIG_VIDEO_REVERSE_IMAGE
1074
+fs_initcall(drm_core_init);
10361075 #else
10371076 module_init(drm_core_init);
10381077 #endif