xref: /openbmc/linux/drivers/gpu/drm/vkms/vkms_drv.c (revision 0c874100)
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 	.dumb_map_offset	= vkms_dumb_map,
72 	.gem_vm_ops		= &vkms_gem_vm_ops,
73 	.gem_free_object_unlocked = vkms_gem_free_object,
74 	.get_vblank_timestamp	= vkms_get_vblank_timestamp,
75 
76 	.name			= DRIVER_NAME,
77 	.desc			= DRIVER_DESC,
78 	.date			= DRIVER_DATE,
79 	.major			= DRIVER_MAJOR,
80 	.minor			= DRIVER_MINOR,
81 };
82 
83 static const struct drm_mode_config_funcs vkms_mode_funcs = {
84 	.fb_create = drm_gem_fb_create,
85 	.atomic_check = drm_atomic_helper_check,
86 	.atomic_commit = drm_atomic_helper_commit,
87 };
88 
89 static int vkms_modeset_init(struct vkms_device *vkmsdev)
90 {
91 	struct drm_device *dev = &vkmsdev->drm;
92 
93 	drm_mode_config_init(dev);
94 	dev->mode_config.funcs = &vkms_mode_funcs;
95 	dev->mode_config.min_width = XRES_MIN;
96 	dev->mode_config.min_height = YRES_MIN;
97 	dev->mode_config.max_width = XRES_MAX;
98 	dev->mode_config.max_height = YRES_MAX;
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 	ret = drm_dev_init(&vkms_device->drm, &vkms_driver, NULL);
112 	if (ret)
113 		goto out_free;
114 
115 	vkms_device->platform =
116 		platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
117 	if (IS_ERR(vkms_device->platform)) {
118 		ret = PTR_ERR(vkms_device->platform);
119 		goto out_fini;
120 	}
121 
122 	vkms_device->drm.irq_enabled = true;
123 
124 	ret = drm_vblank_init(&vkms_device->drm, 1);
125 	if (ret) {
126 		DRM_ERROR("Failed to vblank\n");
127 		goto out_fini;
128 	}
129 
130 	ret = vkms_modeset_init(vkms_device);
131 	if (ret)
132 		goto out_unregister;
133 
134 	ret = drm_dev_register(&vkms_device->drm, 0);
135 	if (ret)
136 		goto out_unregister;
137 
138 	return 0;
139 
140 out_unregister:
141 	platform_device_unregister(vkms_device->platform);
142 
143 out_fini:
144 	drm_dev_fini(&vkms_device->drm);
145 
146 out_free:
147 	kfree(vkms_device);
148 	return ret;
149 }
150 
151 static void __exit vkms_exit(void)
152 {
153 	if (!vkms_device) {
154 		DRM_INFO("vkms_device is NULL.\n");
155 		return;
156 	}
157 
158 	drm_dev_unregister(&vkms_device->drm);
159 	drm_dev_put(&vkms_device->drm);
160 
161 	kfree(vkms_device);
162 }
163 
164 module_init(vkms_init);
165 module_exit(vkms_exit);
166 
167 MODULE_AUTHOR("Haneen Mohammed <hamohammed.sa@gmail.com>");
168 MODULE_AUTHOR("Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>");
169 MODULE_DESCRIPTION(DRIVER_DESC);
170 MODULE_LICENSE("GPL");
171