linux/drivers/gpu/drm/vkms/vkms_writeback.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2
   3#include <linux/dma-buf-map.h>
   4
   5#include <drm/drm_atomic.h>
   6#include <drm/drm_fourcc.h>
   7#include <drm/drm_writeback.h>
   8#include <drm/drm_probe_helper.h>
   9#include <drm/drm_atomic_helper.h>
  10#include <drm/drm_gem_framebuffer_helper.h>
  11#include <drm/drm_gem_shmem_helper.h>
  12
  13#include "vkms_drv.h"
  14
  15static const u32 vkms_wb_formats[] = {
  16        DRM_FORMAT_XRGB8888,
  17};
  18
  19static const struct drm_connector_funcs vkms_wb_connector_funcs = {
  20        .fill_modes = drm_helper_probe_single_connector_modes,
  21        .destroy = drm_connector_cleanup,
  22        .reset = drm_atomic_helper_connector_reset,
  23        .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  24        .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  25};
  26
  27static int vkms_wb_encoder_atomic_check(struct drm_encoder *encoder,
  28                                        struct drm_crtc_state *crtc_state,
  29                                        struct drm_connector_state *conn_state)
  30{
  31        struct drm_framebuffer *fb;
  32        const struct drm_display_mode *mode = &crtc_state->mode;
  33
  34        if (!conn_state->writeback_job || !conn_state->writeback_job->fb)
  35                return 0;
  36
  37        fb = conn_state->writeback_job->fb;
  38        if (fb->width != mode->hdisplay || fb->height != mode->vdisplay) {
  39                DRM_DEBUG_KMS("Invalid framebuffer size %ux%u\n",
  40                              fb->width, fb->height);
  41                return -EINVAL;
  42        }
  43
  44        if (fb->format->format != vkms_wb_formats[0]) {
  45                DRM_DEBUG_KMS("Invalid pixel format %p4cc\n",
  46                              &fb->format->format);
  47                return -EINVAL;
  48        }
  49
  50        return 0;
  51}
  52
  53static const struct drm_encoder_helper_funcs vkms_wb_encoder_helper_funcs = {
  54        .atomic_check = vkms_wb_encoder_atomic_check,
  55};
  56
  57static int vkms_wb_connector_get_modes(struct drm_connector *connector)
  58{
  59        struct drm_device *dev = connector->dev;
  60
  61        return drm_add_modes_noedid(connector, dev->mode_config.max_width,
  62                                    dev->mode_config.max_height);
  63}
  64
  65static int vkms_wb_prepare_job(struct drm_writeback_connector *wb_connector,
  66                               struct drm_writeback_job *job)
  67{
  68        struct drm_gem_object *gem_obj;
  69        struct dma_buf_map map;
  70        int ret;
  71
  72        if (!job->fb)
  73                return 0;
  74
  75        gem_obj = drm_gem_fb_get_obj(job->fb, 0);
  76        ret = drm_gem_shmem_vmap(gem_obj, &map);
  77        if (ret) {
  78                DRM_ERROR("vmap failed: %d\n", ret);
  79                return ret;
  80        }
  81
  82        job->priv = map.vaddr;
  83
  84        return 0;
  85}
  86
  87static void vkms_wb_cleanup_job(struct drm_writeback_connector *connector,
  88                                struct drm_writeback_job *job)
  89{
  90        struct drm_gem_object *gem_obj;
  91        struct vkms_device *vkmsdev;
  92        struct dma_buf_map map;
  93
  94        if (!job->fb)
  95                return;
  96
  97        gem_obj = drm_gem_fb_get_obj(job->fb, 0);
  98        dma_buf_map_set_vaddr(&map, job->priv);
  99        drm_gem_shmem_vunmap(gem_obj, &map);
 100
 101        vkmsdev = drm_device_to_vkms_device(gem_obj->dev);
 102        vkms_set_composer(&vkmsdev->output, false);
 103}
 104
 105static void vkms_wb_atomic_commit(struct drm_connector *conn,
 106                                  struct drm_atomic_state *state)
 107{
 108        struct drm_connector_state *connector_state = drm_atomic_get_new_connector_state(state,
 109                                                                                         conn);
 110        struct vkms_device *vkmsdev = drm_device_to_vkms_device(conn->dev);
 111        struct vkms_output *output = &vkmsdev->output;
 112        struct drm_writeback_connector *wb_conn = &output->wb_connector;
 113        struct drm_connector_state *conn_state = wb_conn->base.state;
 114        struct vkms_crtc_state *crtc_state = output->composer_state;
 115
 116        if (!conn_state)
 117                return;
 118
 119        vkms_set_composer(&vkmsdev->output, true);
 120
 121        spin_lock_irq(&output->composer_lock);
 122        crtc_state->active_writeback = conn_state->writeback_job->priv;
 123        crtc_state->wb_pending = true;
 124        spin_unlock_irq(&output->composer_lock);
 125        drm_writeback_queue_job(wb_conn, connector_state);
 126}
 127
 128static const struct drm_connector_helper_funcs vkms_wb_conn_helper_funcs = {
 129        .get_modes = vkms_wb_connector_get_modes,
 130        .prepare_writeback_job = vkms_wb_prepare_job,
 131        .cleanup_writeback_job = vkms_wb_cleanup_job,
 132        .atomic_commit = vkms_wb_atomic_commit,
 133};
 134
 135int vkms_enable_writeback_connector(struct vkms_device *vkmsdev)
 136{
 137        struct drm_writeback_connector *wb = &vkmsdev->output.wb_connector;
 138
 139        vkmsdev->output.wb_connector.encoder.possible_crtcs = 1;
 140        drm_connector_helper_add(&wb->base, &vkms_wb_conn_helper_funcs);
 141
 142        return drm_writeback_connector_init(&vkmsdev->drm, wb,
 143                                            &vkms_wb_connector_funcs,
 144                                            &vkms_wb_encoder_helper_funcs,
 145                                            vkms_wb_formats,
 146                                            ARRAY_SIZE(vkms_wb_formats));
 147}
 148