2019-02-06 21:01:16 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2018-05-14 21:33:46 +07:00
|
|
|
|
2018-09-08 00:41:36 +07:00
|
|
|
/**
|
|
|
|
* DOC: vkms (Virtual Kernel Modesetting)
|
|
|
|
*
|
|
|
|
* vkms is a software-only model of a kms driver that is useful for testing,
|
|
|
|
* or for running X (or similar) on headless machines and be able to still
|
|
|
|
* use the GPU. vkms aims to enable a virtual display without the need for
|
|
|
|
* a hardware display capability.
|
|
|
|
*/
|
|
|
|
|
2018-05-14 21:33:46 +07:00
|
|
|
#include <linux/module.h>
|
2019-06-30 13:19:01 +07:00
|
|
|
#include <linux/platform_device.h>
|
2019-09-30 22:59:24 +07:00
|
|
|
#include <linux/dma-mapping.h>
|
2019-06-30 13:19:01 +07:00
|
|
|
|
2019-09-30 22:59:24 +07:00
|
|
|
#include <drm/drm_gem.h>
|
2019-06-07 05:27:46 +07:00
|
|
|
#include <drm/drm_atomic.h>
|
2018-05-17 06:55:36 +07:00
|
|
|
#include <drm/drm_atomic_helper.h>
|
2019-06-30 13:19:01 +07:00
|
|
|
#include <drm/drm_drv.h>
|
2018-07-12 20:41:02 +07:00
|
|
|
#include <drm/drm_fb_helper.h>
|
2019-06-30 13:19:01 +07:00
|
|
|
#include <drm/drm_file.h>
|
|
|
|
#include <drm/drm_gem_framebuffer_helper.h>
|
|
|
|
#include <drm/drm_ioctl.h>
|
2019-01-18 04:03:34 +07:00
|
|
|
#include <drm/drm_probe_helper.h>
|
2019-06-30 13:19:01 +07:00
|
|
|
#include <drm/drm_vblank.h>
|
|
|
|
|
2018-05-14 21:33:46 +07:00
|
|
|
#include "vkms_drv.h"
|
|
|
|
|
|
|
|
#define DRIVER_NAME "vkms"
|
|
|
|
#define DRIVER_DESC "Virtual Kernel Mode Setting"
|
|
|
|
#define DRIVER_DATE "20180514"
|
|
|
|
#define DRIVER_MAJOR 1
|
|
|
|
#define DRIVER_MINOR 0
|
|
|
|
|
|
|
|
static struct vkms_device *vkms_device;
|
|
|
|
|
2018-09-06 12:19:11 +07:00
|
|
|
bool enable_cursor;
|
|
|
|
module_param_named(enable_cursor, enable_cursor, bool, 0444);
|
|
|
|
MODULE_PARM_DESC(enable_cursor, "Enable/Disable cursor support");
|
|
|
|
|
2018-05-14 21:33:46 +07:00
|
|
|
static const struct file_operations vkms_driver_fops = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.open = drm_open,
|
|
|
|
.mmap = drm_gem_mmap,
|
|
|
|
.unlocked_ioctl = drm_ioctl,
|
|
|
|
.compat_ioctl = drm_compat_ioctl,
|
|
|
|
.poll = drm_poll,
|
|
|
|
.read = drm_read,
|
|
|
|
.llseek = no_llseek,
|
|
|
|
.release = drm_release,
|
|
|
|
};
|
|
|
|
|
2018-07-12 09:01:47 +07:00
|
|
|
static const struct vm_operations_struct vkms_gem_vm_ops = {
|
|
|
|
.fault = vkms_gem_fault,
|
|
|
|
.open = drm_gem_vm_open,
|
|
|
|
.close = drm_gem_vm_close,
|
|
|
|
};
|
|
|
|
|
2018-05-14 21:33:46 +07:00
|
|
|
static void vkms_release(struct drm_device *dev)
|
|
|
|
{
|
|
|
|
struct vkms_device *vkms = container_of(dev, struct vkms_device, drm);
|
|
|
|
|
|
|
|
platform_device_unregister(vkms->platform);
|
2018-07-19 07:40:45 +07:00
|
|
|
drm_atomic_helper_shutdown(&vkms->drm);
|
2018-05-14 21:33:46 +07:00
|
|
|
drm_mode_config_cleanup(&vkms->drm);
|
|
|
|
drm_dev_fini(&vkms->drm);
|
2019-06-26 08:37:05 +07:00
|
|
|
destroy_workqueue(vkms->output.composer_workq);
|
2018-05-14 21:33:46 +07:00
|
|
|
}
|
|
|
|
|
2019-06-07 05:27:46 +07:00
|
|
|
static void vkms_atomic_commit_tail(struct drm_atomic_state *old_state)
|
|
|
|
{
|
|
|
|
struct drm_device *dev = old_state->dev;
|
drm/vkms: flush crc workers earlier in commit flow
Currently, we flush pending CRC workers very late in the commit flow,
when we destroy all the old crtc states. Unfortunately, at that point,
the framebuffers are already unpinned (and our vaddr possible gone), so
this isn't good. Also, the plane_states we need might also already be
cleaned up, since cleanup order of state structures isn't well defined.
Fix this by waiting for all CRC workers of the old state to complete
before we start any of the cleanup work. For correct ordering and
avoiding races, we can only flush_work after
drm_atomic_helper_wait_for_vblanks() since we know that all subsequent
queue_work will be for the new state. Only once that's done is
flush_work() useful, before that we might flush the work, and then right
after the hrtimer that simulates vblank queues it again. Every time you
have a flush_work before cleaning up the work structure, the following
sequence must be obeyed, or it can go wrong:
1. Make sure no one else can re-queue the work anymore (in our case
that's done by a combination of first updating output->crc_state and
then waiting for the vblank to pass to make sure the hrtimer has noticed
that change).
2. flush_work()
3. Actually clean up stuff (which isn't done here).
Doing the flush_work before we even completed the output->state update,
much less waited for the vblank to make sure that's happened, missed the
point.
Note that this is not yet race-free because of the hrtimer and crc
worker look at the wrong state pointers, but that will be fixed in
subsequent patches.
Cc: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Cc: Haneen Mohammed <hamohammed.sa@gmail.com>
Cc: Daniel Vetter <daniel@ffwll.ch>
Signed-off-by: Daniel Vetter <daniel.vetter@intel.com>
Reviewed-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Tested-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Signed-off-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20190606222751.32567-7-daniel.vetter@ffwll.ch
2019-06-07 05:27:47 +07:00
|
|
|
struct drm_crtc *crtc;
|
|
|
|
struct drm_crtc_state *old_crtc_state;
|
|
|
|
int i;
|
2019-06-07 05:27:46 +07:00
|
|
|
|
|
|
|
drm_atomic_helper_commit_modeset_disables(dev, old_state);
|
|
|
|
|
|
|
|
drm_atomic_helper_commit_planes(dev, old_state, 0);
|
|
|
|
|
|
|
|
drm_atomic_helper_commit_modeset_enables(dev, old_state);
|
|
|
|
|
|
|
|
drm_atomic_helper_fake_vblank(old_state);
|
|
|
|
|
|
|
|
drm_atomic_helper_commit_hw_done(old_state);
|
|
|
|
|
2019-07-19 22:23:13 +07:00
|
|
|
drm_atomic_helper_wait_for_flip_done(dev, old_state);
|
2019-06-07 05:27:46 +07:00
|
|
|
|
drm/vkms: flush crc workers earlier in commit flow
Currently, we flush pending CRC workers very late in the commit flow,
when we destroy all the old crtc states. Unfortunately, at that point,
the framebuffers are already unpinned (and our vaddr possible gone), so
this isn't good. Also, the plane_states we need might also already be
cleaned up, since cleanup order of state structures isn't well defined.
Fix this by waiting for all CRC workers of the old state to complete
before we start any of the cleanup work. For correct ordering and
avoiding races, we can only flush_work after
drm_atomic_helper_wait_for_vblanks() since we know that all subsequent
queue_work will be for the new state. Only once that's done is
flush_work() useful, before that we might flush the work, and then right
after the hrtimer that simulates vblank queues it again. Every time you
have a flush_work before cleaning up the work structure, the following
sequence must be obeyed, or it can go wrong:
1. Make sure no one else can re-queue the work anymore (in our case
that's done by a combination of first updating output->crc_state and
then waiting for the vblank to pass to make sure the hrtimer has noticed
that change).
2. flush_work()
3. Actually clean up stuff (which isn't done here).
Doing the flush_work before we even completed the output->state update,
much less waited for the vblank to make sure that's happened, missed the
point.
Note that this is not yet race-free because of the hrtimer and crc
worker look at the wrong state pointers, but that will be fixed in
subsequent patches.
Cc: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Cc: Haneen Mohammed <hamohammed.sa@gmail.com>
Cc: Daniel Vetter <daniel@ffwll.ch>
Signed-off-by: Daniel Vetter <daniel.vetter@intel.com>
Reviewed-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Tested-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Signed-off-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20190606222751.32567-7-daniel.vetter@ffwll.ch
2019-06-07 05:27:47 +07:00
|
|
|
for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
|
|
|
|
struct vkms_crtc_state *vkms_state =
|
|
|
|
to_vkms_crtc_state(old_crtc_state);
|
|
|
|
|
2019-06-26 08:37:05 +07:00
|
|
|
flush_work(&vkms_state->composer_work);
|
drm/vkms: flush crc workers earlier in commit flow
Currently, we flush pending CRC workers very late in the commit flow,
when we destroy all the old crtc states. Unfortunately, at that point,
the framebuffers are already unpinned (and our vaddr possible gone), so
this isn't good. Also, the plane_states we need might also already be
cleaned up, since cleanup order of state structures isn't well defined.
Fix this by waiting for all CRC workers of the old state to complete
before we start any of the cleanup work. For correct ordering and
avoiding races, we can only flush_work after
drm_atomic_helper_wait_for_vblanks() since we know that all subsequent
queue_work will be for the new state. Only once that's done is
flush_work() useful, before that we might flush the work, and then right
after the hrtimer that simulates vblank queues it again. Every time you
have a flush_work before cleaning up the work structure, the following
sequence must be obeyed, or it can go wrong:
1. Make sure no one else can re-queue the work anymore (in our case
that's done by a combination of first updating output->crc_state and
then waiting for the vblank to pass to make sure the hrtimer has noticed
that change).
2. flush_work()
3. Actually clean up stuff (which isn't done here).
Doing the flush_work before we even completed the output->state update,
much less waited for the vblank to make sure that's happened, missed the
point.
Note that this is not yet race-free because of the hrtimer and crc
worker look at the wrong state pointers, but that will be fixed in
subsequent patches.
Cc: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Cc: Haneen Mohammed <hamohammed.sa@gmail.com>
Cc: Daniel Vetter <daniel@ffwll.ch>
Signed-off-by: Daniel Vetter <daniel.vetter@intel.com>
Reviewed-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Tested-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Signed-off-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20190606222751.32567-7-daniel.vetter@ffwll.ch
2019-06-07 05:27:47 +07:00
|
|
|
}
|
|
|
|
|
2019-06-07 05:27:46 +07:00
|
|
|
drm_atomic_helper_cleanup_planes(dev, old_state);
|
|
|
|
}
|
|
|
|
|
2018-05-15 18:30:52 +07:00
|
|
|
static struct drm_driver vkms_driver = {
|
2018-05-14 21:33:46 +07:00
|
|
|
.driver_features = DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_GEM,
|
|
|
|
.release = vkms_release,
|
|
|
|
.fops = &vkms_driver_fops,
|
2018-07-12 09:01:47 +07:00
|
|
|
.dumb_create = vkms_dumb_create,
|
|
|
|
.gem_vm_ops = &vkms_gem_vm_ops,
|
|
|
|
.gem_free_object_unlocked = vkms_gem_free_object,
|
2018-07-12 09:02:26 +07:00
|
|
|
.get_vblank_timestamp = vkms_get_vblank_timestamp,
|
2019-09-30 22:59:24 +07:00
|
|
|
.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
|
|
|
|
.gem_prime_import_sg_table = vkms_prime_import_sg_table,
|
2018-05-14 21:33:46 +07:00
|
|
|
|
|
|
|
.name = DRIVER_NAME,
|
|
|
|
.desc = DRIVER_DESC,
|
|
|
|
.date = DRIVER_DATE,
|
|
|
|
.major = DRIVER_MAJOR,
|
|
|
|
.minor = DRIVER_MINOR,
|
|
|
|
};
|
|
|
|
|
2018-05-17 06:56:21 +07:00
|
|
|
static const struct drm_mode_config_funcs vkms_mode_funcs = {
|
2018-07-12 20:41:02 +07:00
|
|
|
.fb_create = drm_gem_fb_create,
|
2018-05-17 06:56:21 +07:00
|
|
|
.atomic_check = drm_atomic_helper_check,
|
|
|
|
.atomic_commit = drm_atomic_helper_commit,
|
2018-05-14 21:33:46 +07:00
|
|
|
};
|
|
|
|
|
2019-06-07 05:27:46 +07:00
|
|
|
static const struct drm_mode_config_helper_funcs vkms_mode_config_helpers = {
|
|
|
|
.atomic_commit_tail = vkms_atomic_commit_tail,
|
|
|
|
};
|
|
|
|
|
2018-05-17 06:56:21 +07:00
|
|
|
static int vkms_modeset_init(struct vkms_device *vkmsdev)
|
2018-05-14 21:33:46 +07:00
|
|
|
{
|
2018-05-17 06:56:21 +07:00
|
|
|
struct drm_device *dev = &vkmsdev->drm;
|
2018-05-14 21:33:46 +07:00
|
|
|
|
2018-05-17 06:56:21 +07:00
|
|
|
drm_mode_config_init(dev);
|
|
|
|
dev->mode_config.funcs = &vkms_mode_funcs;
|
|
|
|
dev->mode_config.min_width = XRES_MIN;
|
|
|
|
dev->mode_config.min_height = YRES_MIN;
|
|
|
|
dev->mode_config.max_width = XRES_MAX;
|
|
|
|
dev->mode_config.max_height = YRES_MAX;
|
2018-12-17 01:49:08 +07:00
|
|
|
dev->mode_config.preferred_depth = 24;
|
2019-06-07 05:27:46 +07:00
|
|
|
dev->mode_config.helper_private = &vkms_mode_config_helpers;
|
2018-05-14 21:33:46 +07:00
|
|
|
|
2019-06-26 08:36:18 +07:00
|
|
|
return vkms_output_init(vkmsdev, 0);
|
2018-05-17 06:56:21 +07:00
|
|
|
}
|
2018-05-17 06:55:36 +07:00
|
|
|
|
2018-05-14 21:33:46 +07:00
|
|
|
static int __init vkms_init(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
vkms_device = kzalloc(sizeof(*vkms_device), GFP_KERNEL);
|
|
|
|
if (!vkms_device)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
vkms_device->platform =
|
|
|
|
platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
|
|
|
|
if (IS_ERR(vkms_device->platform)) {
|
|
|
|
ret = PTR_ERR(vkms_device->platform);
|
2018-10-26 17:05:50 +07:00
|
|
|
goto out_free;
|
2018-05-14 21:33:46 +07:00
|
|
|
}
|
|
|
|
|
2018-10-26 17:05:50 +07:00
|
|
|
ret = drm_dev_init(&vkms_device->drm, &vkms_driver,
|
|
|
|
&vkms_device->platform->dev);
|
|
|
|
if (ret)
|
|
|
|
goto out_unregister;
|
|
|
|
|
2019-09-30 22:59:24 +07:00
|
|
|
ret = dma_coerce_mask_and_coherent(vkms_device->drm.dev,
|
|
|
|
DMA_BIT_MASK(64));
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
DRM_ERROR("Could not initialize DMA support\n");
|
|
|
|
goto out_fini;
|
|
|
|
}
|
|
|
|
|
2018-07-12 09:02:26 +07:00
|
|
|
vkms_device->drm.irq_enabled = true;
|
|
|
|
|
|
|
|
ret = drm_vblank_init(&vkms_device->drm, 1);
|
|
|
|
if (ret) {
|
|
|
|
DRM_ERROR("Failed to vblank\n");
|
|
|
|
goto out_fini;
|
|
|
|
}
|
|
|
|
|
2018-05-17 06:56:21 +07:00
|
|
|
ret = vkms_modeset_init(vkms_device);
|
|
|
|
if (ret)
|
2018-10-26 17:05:50 +07:00
|
|
|
goto out_fini;
|
2018-05-14 21:33:46 +07:00
|
|
|
|
|
|
|
ret = drm_dev_register(&vkms_device->drm, 0);
|
|
|
|
if (ret)
|
2018-10-26 17:05:50 +07:00
|
|
|
goto out_fini;
|
2018-05-14 21:33:46 +07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
out_fini:
|
|
|
|
drm_dev_fini(&vkms_device->drm);
|
2018-05-17 06:56:21 +07:00
|
|
|
|
2018-10-26 17:05:50 +07:00
|
|
|
out_unregister:
|
|
|
|
platform_device_unregister(vkms_device->platform);
|
|
|
|
|
2018-05-14 21:33:46 +07:00
|
|
|
out_free:
|
|
|
|
kfree(vkms_device);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit vkms_exit(void)
|
|
|
|
{
|
|
|
|
if (!vkms_device) {
|
|
|
|
DRM_INFO("vkms_device is NULL.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
drm_dev_unregister(&vkms_device->drm);
|
|
|
|
drm_dev_put(&vkms_device->drm);
|
|
|
|
|
|
|
|
kfree(vkms_device);
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(vkms_init);
|
|
|
|
module_exit(vkms_exit);
|
|
|
|
|
2018-05-17 06:56:40 +07:00
|
|
|
MODULE_AUTHOR("Haneen Mohammed <hamohammed.sa@gmail.com>");
|
|
|
|
MODULE_AUTHOR("Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>");
|
2018-05-14 21:33:46 +07:00
|
|
|
MODULE_DESCRIPTION(DRIVER_DESC);
|
|
|
|
MODULE_LICENSE("GPL");
|