LCOV - code coverage report
Current view: top level - drivers/gpu/drm/tests - drm_kunit_helpers.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 22 23 95.7 %
Date: 2023-04-06 08:38:28 Functions: 4 4 100.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0
       2             : 
       3             : #include <drm/drm_drv.h>
       4             : #include <drm/drm_kunit_helpers.h>
       5             : #include <drm/drm_managed.h>
       6             : 
       7             : #include <kunit/resource.h>
       8             : 
       9             : #include <linux/device.h>
      10             : #include <linux/platform_device.h>
      11             : 
      12             : #define KUNIT_DEVICE_NAME       "drm-kunit-mock-device"
      13             : 
      14             : static const struct drm_mode_config_funcs drm_mode_config_funcs = {
      15             : };
      16             : 
      17          17 : static int fake_probe(struct platform_device *pdev)
      18             : {
      19          17 :         return 0;
      20             : }
      21             : 
      22             : static struct platform_driver fake_platform_driver = {
      23             :         .probe  = fake_probe,
      24             :         .driver = {
      25             :                 .name   = KUNIT_DEVICE_NAME,
      26             :         },
      27             : };
      28             : 
      29             : /**
      30             :  * drm_kunit_helper_alloc_device - Allocate a mock device for a KUnit test
      31             :  * @test: The test context object
      32             :  *
      33             :  * This allocates a fake struct &device to create a mock for a KUnit
      34             :  * test. The device will also be bound to a fake driver. It will thus be
      35             :  * able to leverage the usual infrastructure and most notably the
      36             :  * device-managed resources just like a "real" device.
      37             :  *
      38             :  * Callers need to make sure drm_kunit_helper_free_device() on the
      39             :  * device when done.
      40             :  *
      41             :  * Returns:
      42             :  * A pointer to the new device, or an ERR_PTR() otherwise.
      43             :  */
      44          17 : struct device *drm_kunit_helper_alloc_device(struct kunit *test)
      45             : {
      46             :         struct platform_device *pdev;
      47             :         int ret;
      48             : 
      49          17 :         ret = platform_driver_register(&fake_platform_driver);
      50          17 :         KUNIT_ASSERT_EQ(test, ret, 0);
      51             : 
      52          17 :         pdev = platform_device_alloc(KUNIT_DEVICE_NAME, PLATFORM_DEVID_NONE);
      53          34 :         KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
      54             : 
      55          17 :         ret = platform_device_add(pdev);
      56          17 :         KUNIT_ASSERT_EQ(test, ret, 0);
      57             : 
      58          17 :         return &pdev->dev;
      59             : }
      60             : EXPORT_SYMBOL_GPL(drm_kunit_helper_alloc_device);
      61             : 
      62             : /**
      63             :  * drm_kunit_helper_free_device - Frees a mock device
      64             :  * @test: The test context object
      65             :  * @dev: The device to free
      66             :  *
      67             :  * Frees a device allocated with drm_kunit_helper_alloc_device().
      68             :  */
      69          17 : void drm_kunit_helper_free_device(struct kunit *test, struct device *dev)
      70             : {
      71          17 :         struct platform_device *pdev = to_platform_device(dev);
      72             : 
      73          17 :         platform_device_unregister(pdev);
      74          17 :         platform_driver_unregister(&fake_platform_driver);
      75          17 : }
      76             : EXPORT_SYMBOL_GPL(drm_kunit_helper_free_device);
      77             : 
      78             : struct drm_device *
      79          17 : __drm_kunit_helper_alloc_drm_device_with_driver(struct kunit *test,
      80             :                                                 struct device *dev,
      81             :                                                 size_t size, size_t offset,
      82             :                                                 const struct drm_driver *driver)
      83             : {
      84             :         struct drm_device *drm;
      85             :         void *container;
      86             :         int ret;
      87             : 
      88          17 :         container = __devm_drm_dev_alloc(dev, driver, size, offset);
      89          17 :         if (IS_ERR(container))
      90             :                 return ERR_CAST(container);
      91             : 
      92          17 :         drm = container + offset;
      93          17 :         drm->mode_config.funcs = &drm_mode_config_funcs;
      94             : 
      95          17 :         ret = drmm_mode_config_init(drm);
      96          17 :         if (ret)
      97           0 :                 return ERR_PTR(ret);
      98             : 
      99             :         return drm;
     100             : }
     101             : EXPORT_SYMBOL_GPL(__drm_kunit_helper_alloc_drm_device_with_driver);
     102             : 
     103             : MODULE_AUTHOR("Maxime Ripard <maxime@cerno.tech>");
     104             : MODULE_LICENSE("GPL");

Generated by: LCOV version 1.14