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