Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0+
  2
  3/**
  4 * DOC: vkms (Virtual Kernel Modesetting)
  5 *
  6 * VKMS is a software-only model of a KMS driver that is useful for testing
  7 * and for running X (or similar) on headless machines. VKMS aims to enable
  8 * a virtual display with no need of a hardware display capability, releasing
  9 * the GPU in DRM API tests.
 10 */
 11
 12#include <linux/module.h>
 13#include <linux/platform_device.h>
 14#include <linux/dma-mapping.h>
 15
 16#include <drm/drm_gem.h>
 17#include <drm/drm_atomic.h>
 18#include <drm/drm_atomic_helper.h>
 19#include <drm/drm_client_setup.h>
 20#include <drm/drm_drv.h>
 21#include <drm/drm_fbdev_shmem.h>
 22#include <drm/drm_file.h>
 23#include <drm/drm_gem_framebuffer_helper.h>
 24#include <drm/drm_ioctl.h>
 25#include <drm/drm_managed.h>
 26#include <drm/drm_probe_helper.h>
 27#include <drm/drm_gem_shmem_helper.h>
 28#include <drm/drm_vblank.h>
 29
 30#include "vkms_drv.h"
 31
 32#include <drm/drm_print.h>
 33#include <drm/drm_debugfs.h>
 34
 35#define DRIVER_NAME	"vkms"
 36#define DRIVER_DESC	"Virtual Kernel Mode Setting"
 37#define DRIVER_DATE	"20180514"
 38#define DRIVER_MAJOR	1
 39#define DRIVER_MINOR	0
 40
 41static struct vkms_config *default_config;
 42
 43static bool enable_cursor = true;
 44module_param_named(enable_cursor, enable_cursor, bool, 0444);
 45MODULE_PARM_DESC(enable_cursor, "Enable/Disable cursor support");
 46
 47static bool enable_writeback = true;
 48module_param_named(enable_writeback, enable_writeback, bool, 0444);
 49MODULE_PARM_DESC(enable_writeback, "Enable/Disable writeback connector support");
 50
 51static bool enable_overlay;
 52module_param_named(enable_overlay, enable_overlay, bool, 0444);
 53MODULE_PARM_DESC(enable_overlay, "Enable/Disable overlay support");
 54
 55DEFINE_DRM_GEM_FOPS(vkms_driver_fops);
 56
 57static void vkms_release(struct drm_device *dev)
 58{
 59	struct vkms_device *vkms = drm_device_to_vkms_device(dev);
 60
 61	if (vkms->output.composer_workq)
 62		destroy_workqueue(vkms->output.composer_workq);
 63}
 64
 65static void vkms_atomic_commit_tail(struct drm_atomic_state *old_state)
 66{
 67	struct drm_device *dev = old_state->dev;
 68	struct drm_crtc *crtc;
 69	struct drm_crtc_state *old_crtc_state;
 70	int i;
 71
 72	drm_atomic_helper_commit_modeset_disables(dev, old_state);
 73
 74	drm_atomic_helper_commit_planes(dev, old_state, 0);
 75
 76	drm_atomic_helper_commit_modeset_enables(dev, old_state);
 77
 78	drm_atomic_helper_fake_vblank(old_state);
 79
 80	drm_atomic_helper_commit_hw_done(old_state);
 81
 82	drm_atomic_helper_wait_for_flip_done(dev, old_state);
 83
 84	for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
 85		struct vkms_crtc_state *vkms_state =
 86			to_vkms_crtc_state(old_crtc_state);
 87
 88		flush_work(&vkms_state->composer_work);
 89	}
 90
 91	drm_atomic_helper_cleanup_planes(dev, old_state);
 92}
 93
 94static int vkms_config_show(struct seq_file *m, void *data)
 95{
 96	struct drm_debugfs_entry *entry = m->private;
 97	struct drm_device *dev = entry->dev;
 98	struct vkms_device *vkmsdev = drm_device_to_vkms_device(dev);
 99
100	seq_printf(m, "writeback=%d\n", vkmsdev->config->writeback);
101	seq_printf(m, "cursor=%d\n", vkmsdev->config->cursor);
102	seq_printf(m, "overlay=%d\n", vkmsdev->config->overlay);
103
104	return 0;
105}
106
107static const struct drm_debugfs_info vkms_config_debugfs_list[] = {
108	{ "vkms_config", vkms_config_show, 0 },
109};
110
111static const struct drm_driver vkms_driver = {
112	.driver_features	= DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_GEM,
113	.release		= vkms_release,
114	.fops			= &vkms_driver_fops,
115	DRM_GEM_SHMEM_DRIVER_OPS,
116	DRM_FBDEV_SHMEM_DRIVER_OPS,
117
118	.name			= DRIVER_NAME,
119	.desc			= DRIVER_DESC,
120	.date			= DRIVER_DATE,
121	.major			= DRIVER_MAJOR,
122	.minor			= DRIVER_MINOR,
123};
124
125static int vkms_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
126{
127	struct drm_crtc *crtc;
128	struct drm_crtc_state *new_crtc_state;
129	int i;
130
131	for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
132		if (!new_crtc_state->gamma_lut || !new_crtc_state->color_mgmt_changed)
133			continue;
134
135		if (new_crtc_state->gamma_lut->length / sizeof(struct drm_color_lut *)
136		    > VKMS_LUT_SIZE)
137			return -EINVAL;
138	}
139
140	return drm_atomic_helper_check(dev, state);
141}
142
143static const struct drm_mode_config_funcs vkms_mode_funcs = {
144	.fb_create = drm_gem_fb_create,
145	.atomic_check = vkms_atomic_check,
146	.atomic_commit = drm_atomic_helper_commit,
147};
148
149static const struct drm_mode_config_helper_funcs vkms_mode_config_helpers = {
150	.atomic_commit_tail = vkms_atomic_commit_tail,
151};
152
153static int vkms_modeset_init(struct vkms_device *vkmsdev)
154{
155	struct drm_device *dev = &vkmsdev->drm;
156	int ret;
157
158	ret = drmm_mode_config_init(dev);
159	if (ret)
160		return ret;
161
 
162	dev->mode_config.funcs = &vkms_mode_funcs;
163	dev->mode_config.min_width = XRES_MIN;
164	dev->mode_config.min_height = YRES_MIN;
165	dev->mode_config.max_width = XRES_MAX;
166	dev->mode_config.max_height = YRES_MAX;
167	dev->mode_config.cursor_width = 512;
168	dev->mode_config.cursor_height = 512;
169	/*
170	 * FIXME: There's a confusion between bpp and depth between this and
171	 * fbdev helpers. We have to go with 0, meaning "pick the default",
172	 * which is XRGB8888 in all cases.
173	 */
174	dev->mode_config.preferred_depth = 0;
175	dev->mode_config.helper_private = &vkms_mode_config_helpers;
176
177	return vkms_output_init(vkmsdev, 0);
178}
179
180static int vkms_create(struct vkms_config *config)
181{
182	int ret;
183	struct platform_device *pdev;
184	struct vkms_device *vkms_device;
185
186	pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
187	if (IS_ERR(pdev))
188		return PTR_ERR(pdev);
189
190	if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
191		ret = -ENOMEM;
192		goto out_unregister;
193	}
194
195	vkms_device = devm_drm_dev_alloc(&pdev->dev, &vkms_driver,
196					 struct vkms_device, drm);
197	if (IS_ERR(vkms_device)) {
198		ret = PTR_ERR(vkms_device);
199		goto out_devres;
200	}
201	vkms_device->platform = pdev;
202	vkms_device->config = config;
203	config->dev = vkms_device;
204
205	ret = dma_coerce_mask_and_coherent(vkms_device->drm.dev,
206					   DMA_BIT_MASK(64));
207
208	if (ret) {
209		DRM_ERROR("Could not initialize DMA support\n");
210		goto out_devres;
211	}
212
 
 
213	ret = drm_vblank_init(&vkms_device->drm, 1);
214	if (ret) {
215		DRM_ERROR("Failed to vblank\n");
216		goto out_devres;
217	}
218
219	ret = vkms_modeset_init(vkms_device);
220	if (ret)
221		goto out_devres;
222
223	drm_debugfs_add_files(&vkms_device->drm, vkms_config_debugfs_list,
224			      ARRAY_SIZE(vkms_config_debugfs_list));
225
226	ret = drm_dev_register(&vkms_device->drm, 0);
227	if (ret)
228		goto out_devres;
229
230	drm_client_setup(&vkms_device->drm, NULL);
231
232	return 0;
233
234out_devres:
235	devres_release_group(&pdev->dev, NULL);
236out_unregister:
237	platform_device_unregister(pdev);
238	return ret;
239}
240
241static int __init vkms_init(void)
242{
243	int ret;
244	struct vkms_config *config;
245
246	config = kmalloc(sizeof(*config), GFP_KERNEL);
247	if (!config)
248		return -ENOMEM;
249
250	default_config = config;
251
252	config->cursor = enable_cursor;
253	config->writeback = enable_writeback;
254	config->overlay = enable_overlay;
255
256	ret = vkms_create(config);
257	if (ret)
258		kfree(config);
259
260	return ret;
261}
262
263static void vkms_destroy(struct vkms_config *config)
264{
265	struct platform_device *pdev;
266
267	if (!config->dev) {
268		DRM_INFO("vkms_device is NULL.\n");
269		return;
270	}
271
272	pdev = config->dev->platform;
273
274	drm_dev_unregister(&config->dev->drm);
275	drm_atomic_helper_shutdown(&config->dev->drm);
276	devres_release_group(&pdev->dev, NULL);
277	platform_device_unregister(pdev);
278
279	config->dev = NULL;
280}
281
282static void __exit vkms_exit(void)
283{
284	if (default_config->dev)
285		vkms_destroy(default_config);
286
287	kfree(default_config);
288}
289
290module_init(vkms_init);
291module_exit(vkms_exit);
292
293MODULE_AUTHOR("Haneen Mohammed <hamohammed.sa@gmail.com>");
294MODULE_AUTHOR("Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>");
295MODULE_DESCRIPTION(DRIVER_DESC);
296MODULE_LICENSE("GPL");
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0+
  2
  3/**
  4 * DOC: vkms (Virtual Kernel Modesetting)
  5 *
  6 * VKMS is a software-only model of a KMS driver that is useful for testing
  7 * and for running X (or similar) on headless machines. VKMS aims to enable
  8 * a virtual display with no need of a hardware display capability, releasing
  9 * the GPU in DRM API tests.
 10 */
 11
 12#include <linux/module.h>
 13#include <linux/platform_device.h>
 14#include <linux/dma-mapping.h>
 15
 16#include <drm/drm_gem.h>
 17#include <drm/drm_atomic.h>
 18#include <drm/drm_atomic_helper.h>
 
 19#include <drm/drm_drv.h>
 20#include <drm/drm_fb_helper.h>
 21#include <drm/drm_file.h>
 22#include <drm/drm_gem_framebuffer_helper.h>
 23#include <drm/drm_ioctl.h>
 24#include <drm/drm_managed.h>
 25#include <drm/drm_probe_helper.h>
 26#include <drm/drm_gem_shmem_helper.h>
 27#include <drm/drm_vblank.h>
 28
 29#include "vkms_drv.h"
 30
 
 
 
 31#define DRIVER_NAME	"vkms"
 32#define DRIVER_DESC	"Virtual Kernel Mode Setting"
 33#define DRIVER_DATE	"20180514"
 34#define DRIVER_MAJOR	1
 35#define DRIVER_MINOR	0
 36
 37static struct vkms_config *default_config;
 38
 39static bool enable_cursor = true;
 40module_param_named(enable_cursor, enable_cursor, bool, 0444);
 41MODULE_PARM_DESC(enable_cursor, "Enable/Disable cursor support");
 42
 43static bool enable_writeback = true;
 44module_param_named(enable_writeback, enable_writeback, bool, 0444);
 45MODULE_PARM_DESC(enable_writeback, "Enable/Disable writeback connector support");
 46
 47static bool enable_overlay;
 48module_param_named(enable_overlay, enable_overlay, bool, 0444);
 49MODULE_PARM_DESC(enable_overlay, "Enable/Disable overlay support");
 50
 51DEFINE_DRM_GEM_FOPS(vkms_driver_fops);
 52
 53static void vkms_release(struct drm_device *dev)
 54{
 55	struct vkms_device *vkms = container_of(dev, struct vkms_device, drm);
 56
 57	destroy_workqueue(vkms->output.composer_workq);
 
 58}
 59
 60static void vkms_atomic_commit_tail(struct drm_atomic_state *old_state)
 61{
 62	struct drm_device *dev = old_state->dev;
 63	struct drm_crtc *crtc;
 64	struct drm_crtc_state *old_crtc_state;
 65	int i;
 66
 67	drm_atomic_helper_commit_modeset_disables(dev, old_state);
 68
 69	drm_atomic_helper_commit_planes(dev, old_state, 0);
 70
 71	drm_atomic_helper_commit_modeset_enables(dev, old_state);
 72
 73	drm_atomic_helper_fake_vblank(old_state);
 74
 75	drm_atomic_helper_commit_hw_done(old_state);
 76
 77	drm_atomic_helper_wait_for_flip_done(dev, old_state);
 78
 79	for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
 80		struct vkms_crtc_state *vkms_state =
 81			to_vkms_crtc_state(old_crtc_state);
 82
 83		flush_work(&vkms_state->composer_work);
 84	}
 85
 86	drm_atomic_helper_cleanup_planes(dev, old_state);
 87}
 88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 89static const struct drm_driver vkms_driver = {
 90	.driver_features	= DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_GEM,
 91	.release		= vkms_release,
 92	.fops			= &vkms_driver_fops,
 93	DRM_GEM_SHMEM_DRIVER_OPS,
 
 94
 95	.name			= DRIVER_NAME,
 96	.desc			= DRIVER_DESC,
 97	.date			= DRIVER_DATE,
 98	.major			= DRIVER_MAJOR,
 99	.minor			= DRIVER_MINOR,
100};
101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102static const struct drm_mode_config_funcs vkms_mode_funcs = {
103	.fb_create = drm_gem_fb_create,
104	.atomic_check = drm_atomic_helper_check,
105	.atomic_commit = drm_atomic_helper_commit,
106};
107
108static const struct drm_mode_config_helper_funcs vkms_mode_config_helpers = {
109	.atomic_commit_tail = vkms_atomic_commit_tail,
110};
111
112static int vkms_modeset_init(struct vkms_device *vkmsdev)
113{
114	struct drm_device *dev = &vkmsdev->drm;
 
 
 
 
 
115
116	drm_mode_config_init(dev);
117	dev->mode_config.funcs = &vkms_mode_funcs;
118	dev->mode_config.min_width = XRES_MIN;
119	dev->mode_config.min_height = YRES_MIN;
120	dev->mode_config.max_width = XRES_MAX;
121	dev->mode_config.max_height = YRES_MAX;
122	dev->mode_config.cursor_width = 512;
123	dev->mode_config.cursor_height = 512;
124	/* FIXME: There's a confusion between bpp and depth between this and
 
125	 * fbdev helpers. We have to go with 0, meaning "pick the default",
126	 * which ix XRGB8888 in all cases. */
 
127	dev->mode_config.preferred_depth = 0;
128	dev->mode_config.helper_private = &vkms_mode_config_helpers;
129
130	return vkms_output_init(vkmsdev, 0);
131}
132
133static int vkms_create(struct vkms_config *config)
134{
135	int ret;
136	struct platform_device *pdev;
137	struct vkms_device *vkms_device;
138
139	pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
140	if (IS_ERR(pdev))
141		return PTR_ERR(pdev);
142
143	if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) {
144		ret = -ENOMEM;
145		goto out_unregister;
146	}
147
148	vkms_device = devm_drm_dev_alloc(&pdev->dev, &vkms_driver,
149					 struct vkms_device, drm);
150	if (IS_ERR(vkms_device)) {
151		ret = PTR_ERR(vkms_device);
152		goto out_devres;
153	}
154	vkms_device->platform = pdev;
155	vkms_device->config = config;
156	config->dev = vkms_device;
157
158	ret = dma_coerce_mask_and_coherent(vkms_device->drm.dev,
159					   DMA_BIT_MASK(64));
160
161	if (ret) {
162		DRM_ERROR("Could not initialize DMA support\n");
163		goto out_devres;
164	}
165
166	vkms_device->drm.irq_enabled = true;
167
168	ret = drm_vblank_init(&vkms_device->drm, 1);
169	if (ret) {
170		DRM_ERROR("Failed to vblank\n");
171		goto out_devres;
172	}
173
174	ret = vkms_modeset_init(vkms_device);
175	if (ret)
176		goto out_devres;
177
 
 
 
178	ret = drm_dev_register(&vkms_device->drm, 0);
179	if (ret)
180		goto out_devres;
181
182	drm_fbdev_generic_setup(&vkms_device->drm, 0);
183
184	return 0;
185
186out_devres:
187	devres_release_group(&pdev->dev, NULL);
188out_unregister:
189	platform_device_unregister(pdev);
190	return ret;
191}
192
193static int __init vkms_init(void)
194{
 
195	struct vkms_config *config;
196
197	config = kmalloc(sizeof(*config), GFP_KERNEL);
198	if (!config)
199		return -ENOMEM;
200
201	default_config = config;
202
203	config->cursor = enable_cursor;
204	config->writeback = enable_writeback;
205	config->overlay = enable_overlay;
206
207	return vkms_create(config);
 
 
 
 
208}
209
210static void vkms_destroy(struct vkms_config *config)
211{
212	struct platform_device *pdev;
213
214	if (!config->dev) {
215		DRM_INFO("vkms_device is NULL.\n");
216		return;
217	}
218
219	pdev = config->dev->platform;
220
221	drm_dev_unregister(&config->dev->drm);
222	drm_atomic_helper_shutdown(&config->dev->drm);
223	devres_release_group(&pdev->dev, NULL);
224	platform_device_unregister(pdev);
225
226	config->dev = NULL;
227}
228
229static void __exit vkms_exit(void)
230{
231	if (default_config->dev)
232		vkms_destroy(default_config);
233
234	kfree(default_config);
235}
236
237module_init(vkms_init);
238module_exit(vkms_exit);
239
240MODULE_AUTHOR("Haneen Mohammed <hamohammed.sa@gmail.com>");
241MODULE_AUTHOR("Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>");
242MODULE_DESCRIPTION(DRIVER_DESC);
243MODULE_LICENSE("GPL");