xref: /openbmc/linux/drivers/gpu/drm/vkms/vkms_drv.c (revision 2c6467d2)
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  */
7 
8 /**
9  * DOC: vkms (Virtual Kernel Modesetting)
10  *
11  * vkms is a software-only model of a kms driver that is useful for testing,
12  * or for running X (or similar) on headless machines and be able to still
13  * use the GPU. vkms aims to enable a virtual display without the need for
14  * a hardware display capability.
15  */
16 
17 #include <linux/module.h>
18 #include <drm/drm_gem.h>
19 #include <drm/drm_crtc_helper.h>
20 #include <drm/drm_atomic_helper.h>
21 #include <drm/drm_gem_framebuffer_helper.h>
22 #include <drm/drm_fb_helper.h>
23 #include "vkms_drv.h"
24 
25 #define DRIVER_NAME	"vkms"
26 #define DRIVER_DESC	"Virtual Kernel Mode Setting"
27 #define DRIVER_DATE	"20180514"
28 #define DRIVER_MAJOR	1
29 #define DRIVER_MINOR	0
30 
31 static struct vkms_device *vkms_device;
32 
33 bool enable_cursor;
34 module_param_named(enable_cursor, enable_cursor, bool, 0444);
35 MODULE_PARM_DESC(enable_cursor, "Enable/Disable cursor support");
36 
37 static const struct file_operations vkms_driver_fops = {
38 	.owner		= THIS_MODULE,
39 	.open		= drm_open,
40 	.mmap		= drm_gem_mmap,
41 	.unlocked_ioctl	= drm_ioctl,
42 	.compat_ioctl	= drm_compat_ioctl,
43 	.poll		= drm_poll,
44 	.read		= drm_read,
45 	.llseek		= no_llseek,
46 	.release	= drm_release,
47 };
48 
49 static const struct vm_operations_struct vkms_gem_vm_ops = {
50 	.fault = vkms_gem_fault,
51 	.open = drm_gem_vm_open,
52 	.close = drm_gem_vm_close,
53 };
54 
55 static void vkms_release(struct drm_device *dev)
56 {
57 	struct vkms_device *vkms = container_of(dev, struct vkms_device, drm);
58 
59 	platform_device_unregister(vkms->platform);
60 	drm_atomic_helper_shutdown(&vkms->drm);
61 	drm_mode_config_cleanup(&vkms->drm);
62 	drm_dev_fini(&vkms->drm);
63 	destroy_workqueue(vkms->output.crc_workq);
64 }
65 
66 static struct drm_driver vkms_driver = {
67 	.driver_features	= DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_GEM,
68 	.release		= vkms_release,
69 	.fops			= &vkms_driver_fops,
70 	.dumb_create		= vkms_dumb_create,
71 	.gem_vm_ops		= &vkms_gem_vm_ops,
72 	.gem_free_object_unlocked = vkms_gem_free_object,
73 	.get_vblank_timestamp	= vkms_get_vblank_timestamp,
74 
75 	.name			= DRIVER_NAME,
76 	.desc			= DRIVER_DESC,
77 	.date			= DRIVER_DATE,
78 	.major			= DRIVER_MAJOR,
79 	.minor			= DRIVER_MINOR,
80 };
81 
82 static const struct drm_mode_config_funcs vkms_mode_funcs = {
83 	.fb_create = drm_gem_fb_create,
84 	.atomic_check = drm_atomic_helper_check,
85 	.atomic_commit = drm_atomic_helper_commit,
86 };
87 
88 static int vkms_modeset_init(struct vkms_device *vkmsdev)
89 {
90 	struct drm_device *dev = &vkmsdev->drm;
91 
92 	drm_mode_config_init(dev);
93 	dev->mode_config.funcs = &vkms_mode_funcs;
94 	dev->mode_config.min_width = XRES_MIN;
95 	dev->mode_config.min_height = YRES_MIN;
96 	dev->mode_config.max_width = XRES_MAX;
97 	dev->mode_config.max_height = YRES_MAX;
98 	dev->mode_config.preferred_depth = 24;
99 
100 	return vkms_output_init(vkmsdev);
101 }
102 
103 static int __init vkms_init(void)
104 {
105 	int ret;
106 
107 	vkms_device = kzalloc(sizeof(*vkms_device), GFP_KERNEL);
108 	if (!vkms_device)
109 		return -ENOMEM;
110 
111 	vkms_device->platform =
112 		platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
113 	if (IS_ERR(vkms_device->platform)) {
114 		ret = PTR_ERR(vkms_device->platform);
115 		goto out_free;
116 	}
117 
118 	ret = drm_dev_init(&vkms_device->drm, &vkms_driver,
119 			   &vkms_device->platform->dev);
120 	if (ret)
121 		goto out_unregister;
122 
123 	vkms_device->drm.irq_enabled = true;
124 
125 	ret = drm_vblank_init(&vkms_device->drm, 1);
126 	if (ret) {
127 		DRM_ERROR("Failed to vblank\n");
128 		goto out_fini;
129 	}
130 
131 	ret = vkms_modeset_init(vkms_device);
132 	if (ret)
133 		goto out_fini;
134 
135 	ret = drm_dev_register(&vkms_device->drm, 0);
136 	if (ret)
137 		goto out_fini;
138 
139 	return 0;
140 
141 out_fini:
142 	drm_dev_fini(&vkms_device->drm);
143 
144 out_unregister:
145 	platform_device_unregister(vkms_device->platform);
146 
147 out_free:
148 	kfree(vkms_device);
149 	return ret;
150 }
151 
152 static void __exit vkms_exit(void)
153 {
154 	if (!vkms_device) {
155 		DRM_INFO("vkms_device is NULL.\n");
156 		return;
157 	}
158 
159 	drm_dev_unregister(&vkms_device->drm);
160 	drm_dev_put(&vkms_device->drm);
161 
162 	kfree(vkms_device);
163 }
164 
165 module_init(vkms_init);
166 module_exit(vkms_exit);
167 
168 MODULE_AUTHOR("Haneen Mohammed <hamohammed.sa@gmail.com>");
169 MODULE_AUTHOR("Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>");
170 MODULE_DESCRIPTION(DRIVER_DESC);
171 MODULE_LICENSE("GPL");
172