Home | History | Annotate | Line # | Download | only in virtio
      1 /*	$NetBSD: virtgpu_display.c,v 1.3 2021/12/18 23:45:45 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 2015 Red Hat, Inc.
      5  * All Rights Reserved.
      6  *
      7  * Authors:
      8  *    Dave Airlie
      9  *    Alon Levy
     10  *
     11  * Permission is hereby granted, free of charge, to any person obtaining a
     12  * copy of this software and associated documentation files (the "Software"),
     13  * to deal in the Software without restriction, including without limitation
     14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     15  * and/or sell copies of the Software, and to permit persons to whom the
     16  * Software is furnished to do so, subject to the following conditions:
     17  *
     18  * The above copyright notice and this permission notice shall be included in
     19  * all copies or substantial portions of the Software.
     20  *
     21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     24  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     25  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     27  * OTHER DEALINGS IN THE SOFTWARE.
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: virtgpu_display.c,v 1.3 2021/12/18 23:45:45 riastradh Exp $");
     32 
     33 #include <drm/drm_atomic_helper.h>
     34 #include <drm/drm_damage_helper.h>
     35 #include <drm/drm_fourcc.h>
     36 #include <drm/drm_gem_framebuffer_helper.h>
     37 #include <drm/drm_probe_helper.h>
     38 #include <drm/drm_vblank.h>
     39 
     40 #include "virtgpu_drv.h"
     41 
     42 #define XRES_MIN    32
     43 #define YRES_MIN    32
     44 
     45 #define XRES_DEF  1024
     46 #define YRES_DEF   768
     47 
     48 #define XRES_MAX  8192
     49 #define YRES_MAX  8192
     50 
     51 #define drm_connector_to_virtio_gpu_output(x) \
     52 	container_of(x, struct virtio_gpu_output, conn)
     53 
     54 static const struct drm_crtc_funcs virtio_gpu_crtc_funcs = {
     55 	.set_config             = drm_atomic_helper_set_config,
     56 	.destroy                = drm_crtc_cleanup,
     57 
     58 	.page_flip              = drm_atomic_helper_page_flip,
     59 	.reset                  = drm_atomic_helper_crtc_reset,
     60 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
     61 	.atomic_destroy_state   = drm_atomic_helper_crtc_destroy_state,
     62 };
     63 
     64 static const struct drm_framebuffer_funcs virtio_gpu_fb_funcs = {
     65 	.create_handle = drm_gem_fb_create_handle,
     66 	.destroy = drm_gem_fb_destroy,
     67 	.dirty = drm_atomic_helper_dirtyfb,
     68 };
     69 
     70 static int
     71 virtio_gpu_framebuffer_init(struct drm_device *dev,
     72 			    struct virtio_gpu_framebuffer *vgfb,
     73 			    const struct drm_mode_fb_cmd2 *mode_cmd,
     74 			    struct drm_gem_object *obj)
     75 {
     76 	int ret;
     77 
     78 	vgfb->base.obj[0] = obj;
     79 
     80 	drm_helper_mode_fill_fb_struct(dev, &vgfb->base, mode_cmd);
     81 
     82 	ret = drm_framebuffer_init(dev, &vgfb->base, &virtio_gpu_fb_funcs);
     83 	if (ret) {
     84 		vgfb->base.obj[0] = NULL;
     85 		return ret;
     86 	}
     87 	return 0;
     88 }
     89 
     90 static void virtio_gpu_crtc_mode_set_nofb(struct drm_crtc *crtc)
     91 {
     92 	struct drm_device *dev = crtc->dev;
     93 	struct virtio_gpu_device *vgdev = dev->dev_private;
     94 	struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
     95 
     96 	virtio_gpu_cmd_set_scanout(vgdev, output->index, 0,
     97 				   crtc->mode.hdisplay,
     98 				   crtc->mode.vdisplay, 0, 0);
     99 }
    100 
    101 static void virtio_gpu_crtc_atomic_enable(struct drm_crtc *crtc,
    102 					  struct drm_crtc_state *old_state)
    103 {
    104 	struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
    105 
    106 	output->enabled = true;
    107 }
    108 
    109 static void virtio_gpu_crtc_atomic_disable(struct drm_crtc *crtc,
    110 					   struct drm_crtc_state *old_state)
    111 {
    112 	struct drm_device *dev = crtc->dev;
    113 	struct virtio_gpu_device *vgdev = dev->dev_private;
    114 	struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc);
    115 
    116 	virtio_gpu_cmd_set_scanout(vgdev, output->index, 0, 0, 0, 0, 0);
    117 	output->enabled = false;
    118 }
    119 
    120 static int virtio_gpu_crtc_atomic_check(struct drm_crtc *crtc,
    121 					struct drm_crtc_state *state)
    122 {
    123 	return 0;
    124 }
    125 
    126 static void virtio_gpu_crtc_atomic_flush(struct drm_crtc *crtc,
    127 					 struct drm_crtc_state *old_state)
    128 {
    129 	unsigned long flags;
    130 
    131 	spin_lock_irqsave(&crtc->dev->event_lock, flags);
    132 	if (crtc->state->event)
    133 		drm_crtc_send_vblank_event(crtc, crtc->state->event);
    134 	crtc->state->event = NULL;
    135 	spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
    136 }
    137 
    138 static const struct drm_crtc_helper_funcs virtio_gpu_crtc_helper_funcs = {
    139 	.mode_set_nofb = virtio_gpu_crtc_mode_set_nofb,
    140 	.atomic_check  = virtio_gpu_crtc_atomic_check,
    141 	.atomic_flush  = virtio_gpu_crtc_atomic_flush,
    142 	.atomic_enable = virtio_gpu_crtc_atomic_enable,
    143 	.atomic_disable = virtio_gpu_crtc_atomic_disable,
    144 };
    145 
    146 static void virtio_gpu_enc_mode_set(struct drm_encoder *encoder,
    147 				    struct drm_display_mode *mode,
    148 				    struct drm_display_mode *adjusted_mode)
    149 {
    150 }
    151 
    152 static void virtio_gpu_enc_enable(struct drm_encoder *encoder)
    153 {
    154 }
    155 
    156 static void virtio_gpu_enc_disable(struct drm_encoder *encoder)
    157 {
    158 }
    159 
    160 static int virtio_gpu_conn_get_modes(struct drm_connector *connector)
    161 {
    162 	struct virtio_gpu_output *output =
    163 		drm_connector_to_virtio_gpu_output(connector);
    164 	struct drm_display_mode *mode = NULL;
    165 	int count, width, height;
    166 
    167 	if (output->edid) {
    168 		count = drm_add_edid_modes(connector, output->edid);
    169 		if (count)
    170 			return count;
    171 	}
    172 
    173 	width  = le32_to_cpu(output->info.r.width);
    174 	height = le32_to_cpu(output->info.r.height);
    175 	count = drm_add_modes_noedid(connector, XRES_MAX, YRES_MAX);
    176 
    177 	if (width == 0 || height == 0) {
    178 		width = XRES_DEF;
    179 		height = YRES_DEF;
    180 		drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF);
    181 	} else {
    182 		DRM_DEBUG("add mode: %dx%d\n", width, height);
    183 		mode = drm_cvt_mode(connector->dev, width, height, 60,
    184 				    false, false, false);
    185 		mode->type |= DRM_MODE_TYPE_PREFERRED;
    186 		drm_mode_probed_add(connector, mode);
    187 		count++;
    188 	}
    189 
    190 	return count;
    191 }
    192 
    193 static enum drm_mode_status virtio_gpu_conn_mode_valid(struct drm_connector *connector,
    194 				      struct drm_display_mode *mode)
    195 {
    196 	struct virtio_gpu_output *output =
    197 		drm_connector_to_virtio_gpu_output(connector);
    198 	int width, height;
    199 
    200 	width  = le32_to_cpu(output->info.r.width);
    201 	height = le32_to_cpu(output->info.r.height);
    202 
    203 	if (!(mode->type & DRM_MODE_TYPE_PREFERRED))
    204 		return MODE_OK;
    205 	if (mode->hdisplay == XRES_DEF && mode->vdisplay == YRES_DEF)
    206 		return MODE_OK;
    207 	if (mode->hdisplay <= width  && mode->hdisplay >= width - 16 &&
    208 	    mode->vdisplay <= height && mode->vdisplay >= height - 16)
    209 		return MODE_OK;
    210 
    211 	DRM_DEBUG("del mode: %dx%d\n", mode->hdisplay, mode->vdisplay);
    212 	return MODE_BAD;
    213 }
    214 
    215 static const struct drm_encoder_helper_funcs virtio_gpu_enc_helper_funcs = {
    216 	.mode_set   = virtio_gpu_enc_mode_set,
    217 	.enable     = virtio_gpu_enc_enable,
    218 	.disable    = virtio_gpu_enc_disable,
    219 };
    220 
    221 static const struct drm_connector_helper_funcs virtio_gpu_conn_helper_funcs = {
    222 	.get_modes    = virtio_gpu_conn_get_modes,
    223 	.mode_valid   = virtio_gpu_conn_mode_valid,
    224 };
    225 
    226 static enum drm_connector_status virtio_gpu_conn_detect(
    227 			struct drm_connector *connector,
    228 			bool force)
    229 {
    230 	struct virtio_gpu_output *output =
    231 		drm_connector_to_virtio_gpu_output(connector);
    232 
    233 	if (output->info.enabled)
    234 		return connector_status_connected;
    235 	else
    236 		return connector_status_disconnected;
    237 }
    238 
    239 static void virtio_gpu_conn_destroy(struct drm_connector *connector)
    240 {
    241 	drm_connector_unregister(connector);
    242 	drm_connector_cleanup(connector);
    243 }
    244 
    245 static const struct drm_connector_funcs virtio_gpu_connector_funcs = {
    246 	.detect = virtio_gpu_conn_detect,
    247 	.fill_modes = drm_helper_probe_single_connector_modes,
    248 	.destroy = virtio_gpu_conn_destroy,
    249 	.reset = drm_atomic_helper_connector_reset,
    250 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
    251 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
    252 };
    253 
    254 static const struct drm_encoder_funcs virtio_gpu_enc_funcs = {
    255 	.destroy = drm_encoder_cleanup,
    256 };
    257 
    258 static int vgdev_output_init(struct virtio_gpu_device *vgdev, int index)
    259 {
    260 	struct drm_device *dev = vgdev->ddev;
    261 	struct virtio_gpu_output *output = vgdev->outputs + index;
    262 	struct drm_connector *connector = &output->conn;
    263 	struct drm_encoder *encoder = &output->enc;
    264 	struct drm_crtc *crtc = &output->crtc;
    265 	struct drm_plane *primary, *cursor;
    266 
    267 	output->index = index;
    268 	if (index == 0) {
    269 		output->info.enabled = cpu_to_le32(true);
    270 		output->info.r.width = cpu_to_le32(XRES_DEF);
    271 		output->info.r.height = cpu_to_le32(YRES_DEF);
    272 	}
    273 
    274 	primary = virtio_gpu_plane_init(vgdev, DRM_PLANE_TYPE_PRIMARY, index);
    275 	if (IS_ERR(primary))
    276 		return PTR_ERR(primary);
    277 	cursor = virtio_gpu_plane_init(vgdev, DRM_PLANE_TYPE_CURSOR, index);
    278 	if (IS_ERR(cursor))
    279 		return PTR_ERR(cursor);
    280 	drm_crtc_init_with_planes(dev, crtc, primary, cursor,
    281 				  &virtio_gpu_crtc_funcs, NULL);
    282 	drm_crtc_helper_add(crtc, &virtio_gpu_crtc_helper_funcs);
    283 
    284 	drm_connector_init(dev, connector, &virtio_gpu_connector_funcs,
    285 			   DRM_MODE_CONNECTOR_VIRTUAL);
    286 	drm_connector_helper_add(connector, &virtio_gpu_conn_helper_funcs);
    287 	if (vgdev->has_edid)
    288 		drm_connector_attach_edid_property(connector);
    289 
    290 	drm_encoder_init(dev, encoder, &virtio_gpu_enc_funcs,
    291 			 DRM_MODE_ENCODER_VIRTUAL, NULL);
    292 	drm_encoder_helper_add(encoder, &virtio_gpu_enc_helper_funcs);
    293 	encoder->possible_crtcs = 1 << index;
    294 
    295 	drm_connector_attach_encoder(connector, encoder);
    296 	drm_connector_register(connector);
    297 	return 0;
    298 }
    299 
    300 static struct drm_framebuffer *
    301 virtio_gpu_user_framebuffer_create(struct drm_device *dev,
    302 				   struct drm_file *file_priv,
    303 				   const struct drm_mode_fb_cmd2 *mode_cmd)
    304 {
    305 	struct drm_gem_object *obj = NULL;
    306 	struct virtio_gpu_framebuffer *virtio_gpu_fb;
    307 	int ret;
    308 
    309 	if (mode_cmd->pixel_format != DRM_FORMAT_HOST_XRGB8888 &&
    310 	    mode_cmd->pixel_format != DRM_FORMAT_HOST_ARGB8888)
    311 		return ERR_PTR(-ENOENT);
    312 
    313 	/* lookup object associated with res handle */
    314 	obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
    315 	if (!obj)
    316 		return ERR_PTR(-EINVAL);
    317 
    318 	virtio_gpu_fb = kzalloc(sizeof(*virtio_gpu_fb), GFP_KERNEL);
    319 	if (virtio_gpu_fb == NULL)
    320 		return ERR_PTR(-ENOMEM);
    321 
    322 	ret = virtio_gpu_framebuffer_init(dev, virtio_gpu_fb, mode_cmd, obj);
    323 	if (ret) {
    324 		kfree(virtio_gpu_fb);
    325 		drm_gem_object_put_unlocked(obj);
    326 		return NULL;
    327 	}
    328 
    329 	return &virtio_gpu_fb->base;
    330 }
    331 
    332 static void vgdev_atomic_commit_tail(struct drm_atomic_state *state)
    333 {
    334 	struct drm_device *dev = state->dev;
    335 
    336 	drm_atomic_helper_commit_modeset_disables(dev, state);
    337 	drm_atomic_helper_commit_modeset_enables(dev, state);
    338 	drm_atomic_helper_commit_planes(dev, state, 0);
    339 
    340 	drm_atomic_helper_commit_hw_done(state);
    341 
    342 	drm_atomic_helper_wait_for_vblanks(dev, state);
    343 	drm_atomic_helper_cleanup_planes(dev, state);
    344 }
    345 
    346 static const struct drm_mode_config_helper_funcs virtio_mode_config_helpers = {
    347 	.atomic_commit_tail = vgdev_atomic_commit_tail,
    348 };
    349 
    350 static const struct drm_mode_config_funcs virtio_gpu_mode_funcs = {
    351 	.fb_create = virtio_gpu_user_framebuffer_create,
    352 	.atomic_check = drm_atomic_helper_check,
    353 	.atomic_commit = drm_atomic_helper_commit,
    354 };
    355 
    356 void virtio_gpu_modeset_init(struct virtio_gpu_device *vgdev)
    357 {
    358 	int i;
    359 
    360 	drm_mode_config_init(vgdev->ddev);
    361 	vgdev->ddev->mode_config.quirk_addfb_prefer_host_byte_order = true;
    362 	vgdev->ddev->mode_config.funcs = &virtio_gpu_mode_funcs;
    363 	vgdev->ddev->mode_config.helper_private = &virtio_mode_config_helpers;
    364 
    365 	/* modes will be validated against the framebuffer size */
    366 	vgdev->ddev->mode_config.min_width = XRES_MIN;
    367 	vgdev->ddev->mode_config.min_height = YRES_MIN;
    368 	vgdev->ddev->mode_config.max_width = XRES_MAX;
    369 	vgdev->ddev->mode_config.max_height = YRES_MAX;
    370 
    371 	for (i = 0 ; i < vgdev->num_scanouts; ++i)
    372 		vgdev_output_init(vgdev, i);
    373 
    374 	drm_mode_config_reset(vgdev->ddev);
    375 }
    376 
    377 void virtio_gpu_modeset_fini(struct virtio_gpu_device *vgdev)
    378 {
    379 	int i;
    380 
    381 	for (i = 0 ; i < vgdev->num_scanouts; ++i)
    382 		kfree(vgdev->outputs[i].edid);
    383 	drm_atomic_helper_shutdown(vgdev->ddev);
    384 	drm_mode_config_cleanup(vgdev->ddev);
    385 }
    386