xref: /linux/drivers/gpu/drm/tests/drm_kunit_helpers.c (revision 7482c19173b7eb044d476b3444d7ee55bc669d03)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <drm/drm_drv.h>
4 #include <drm/drm_managed.h>
5 
6 #include <kunit/resource.h>
7 
8 #include <linux/device.h>
9 
10 #include "drm_kunit_helpers.h"
11 
12 struct kunit_dev {
13 	struct drm_device base;
14 };
15 
16 static const struct drm_mode_config_funcs drm_mode_config_funcs = {
17 };
18 
19 static int dev_init(struct kunit_resource *res, void *ptr)
20 {
21 	char *name = ptr;
22 	struct device *dev;
23 
24 	dev = root_device_register(name);
25 	if (IS_ERR(dev))
26 		return PTR_ERR(dev);
27 
28 	res->data = dev;
29 	return 0;
30 }
31 
32 static void dev_free(struct kunit_resource *res)
33 {
34 	struct device *dev = res->data;
35 
36 	root_device_unregister(dev);
37 }
38 
39 struct drm_device *drm_kunit_device_init(struct kunit *test, u32 features, char *name)
40 {
41 	struct kunit_dev *kdev;
42 	struct drm_device *drm;
43 	struct drm_driver *driver;
44 	struct device *dev;
45 	int ret;
46 
47 	dev = kunit_alloc_resource(test, dev_init, dev_free, GFP_KERNEL, name);
48 	if (!dev)
49 		return ERR_PTR(-ENOMEM);
50 
51 	driver = kunit_kzalloc(test, sizeof(*driver), GFP_KERNEL);
52 	if (!driver)
53 		return ERR_PTR(-ENOMEM);
54 
55 	driver->driver_features = features;
56 	kdev = devm_drm_dev_alloc(dev, driver, struct kunit_dev, base);
57 	if (IS_ERR(kdev))
58 		return ERR_CAST(kdev);
59 
60 	drm = &kdev->base;
61 	drm->mode_config.funcs = &drm_mode_config_funcs;
62 
63 	ret = drmm_mode_config_init(drm);
64 	if (ret)
65 		return ERR_PTR(ret);
66 
67 	return drm;
68 }
69 
70 MODULE_AUTHOR("Maxime Ripard <maxime@cerno.tech>");
71 MODULE_LICENSE("GPL");
72