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 34 #include "virtgpu_drv.h" 35 36 #define XRES_MIN 32 37 #define YRES_MIN 32 38 39 #define XRES_DEF 1024 40 #define YRES_DEF 768 41 42 #define XRES_MAX 8192 43 #define YRES_MAX 8192 44 45 #define drm_connector_to_virtio_gpu_output(x) \ 46 container_of(x, struct virtio_gpu_output, conn) 47 48 static const struct drm_crtc_funcs virtio_gpu_crtc_funcs = { 49 .set_config = drm_atomic_helper_set_config, 50 .destroy = drm_crtc_cleanup, 51 52 .page_flip = drm_atomic_helper_page_flip, 53 .reset = drm_atomic_helper_crtc_reset, 54 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, 55 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, 56 }; 57 58 static const struct drm_framebuffer_funcs virtio_gpu_fb_funcs = { 59 .create_handle = drm_gem_fb_create_handle, 60 .destroy = drm_gem_fb_destroy, 61 .dirty = drm_atomic_helper_dirtyfb, 62 }; 63 64 static int 65 virtio_gpu_framebuffer_init(struct drm_device *dev, 66 struct virtio_gpu_framebuffer *vgfb, 67 const struct drm_mode_fb_cmd2 *mode_cmd, 68 struct drm_gem_object *obj) 69 { 70 int ret; 71 72 vgfb->base.obj[0] = obj; 73 74 drm_helper_mode_fill_fb_struct(dev, &vgfb->base, mode_cmd); 75 76 ret = drm_framebuffer_init(dev, &vgfb->base, &virtio_gpu_fb_funcs); 77 if (ret) { 78 vgfb->base.obj[0] = NULL; 79 return ret; 80 } 81 return 0; 82 } 83 84 static void virtio_gpu_crtc_mode_set_nofb(struct drm_crtc *crtc) 85 { 86 struct drm_device *dev = crtc->dev; 87 struct virtio_gpu_device *vgdev = dev->dev_private; 88 struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc); 89 90 virtio_gpu_cmd_set_scanout(vgdev, output->index, 0, 91 crtc->mode.hdisplay, 92 crtc->mode.vdisplay, 0, 0); 93 virtio_gpu_notify(vgdev); 94 } 95 96 static void virtio_gpu_crtc_atomic_enable(struct drm_crtc *crtc, 97 struct drm_crtc_state *old_state) 98 { 99 struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc); 100 101 output->enabled = true; 102 } 103 104 static void virtio_gpu_crtc_atomic_disable(struct drm_crtc *crtc, 105 struct drm_crtc_state *old_state) 106 { 107 struct drm_device *dev = crtc->dev; 108 struct virtio_gpu_device *vgdev = dev->dev_private; 109 struct virtio_gpu_output *output = drm_crtc_to_virtio_gpu_output(crtc); 110 111 virtio_gpu_cmd_set_scanout(vgdev, output->index, 0, 0, 0, 0, 0); 112 virtio_gpu_notify(vgdev); 113 output->enabled = false; 114 } 115 116 static int virtio_gpu_crtc_atomic_check(struct drm_crtc *crtc, 117 struct drm_crtc_state *state) 118 { 119 return 0; 120 } 121 122 static void virtio_gpu_crtc_atomic_flush(struct drm_crtc *crtc, 123 struct drm_crtc_state *old_state) 124 { 125 } 126 127 static const struct drm_crtc_helper_funcs virtio_gpu_crtc_helper_funcs = { 128 .mode_set_nofb = virtio_gpu_crtc_mode_set_nofb, 129 .atomic_check = virtio_gpu_crtc_atomic_check, 130 .atomic_flush = virtio_gpu_crtc_atomic_flush, 131 .atomic_enable = virtio_gpu_crtc_atomic_enable, 132 .atomic_disable = virtio_gpu_crtc_atomic_disable, 133 }; 134 135 static void virtio_gpu_enc_mode_set(struct drm_encoder *encoder, 136 struct drm_display_mode *mode, 137 struct drm_display_mode *adjusted_mode) 138 { 139 } 140 141 static void virtio_gpu_enc_enable(struct drm_encoder *encoder) 142 { 143 } 144 145 static void virtio_gpu_enc_disable(struct drm_encoder *encoder) 146 { 147 } 148 149 static int virtio_gpu_conn_get_modes(struct drm_connector *connector) 150 { 151 struct virtio_gpu_output *output = 152 drm_connector_to_virtio_gpu_output(connector); 153 struct drm_display_mode *mode = NULL; 154 int count, width, height; 155 156 if (output->edid) { 157 count = drm_add_edid_modes(connector, output->edid); 158 if (count) 159 return count; 160 } 161 162 width = le32_to_cpu(output->info.r.width); 163 height = le32_to_cpu(output->info.r.height); 164 count = drm_add_modes_noedid(connector, XRES_MAX, YRES_MAX); 165 166 if (width == 0 || height == 0) { 167 width = XRES_DEF; 168 height = YRES_DEF; 169 drm_set_preferred_mode(connector, XRES_DEF, YRES_DEF); 170 } else { 171 DRM_DEBUG("add mode: %dx%d\n", width, height); 172 mode = drm_cvt_mode(connector->dev, width, height, 60, 173 false, false, false); 174 mode->type |= DRM_MODE_TYPE_PREFERRED; 175 drm_mode_probed_add(connector, mode); 176 count++; 177 } 178 179 return count; 180 } 181 182 static enum drm_mode_status virtio_gpu_conn_mode_valid(struct drm_connector *connector, 183 struct drm_display_mode *mode) 184 { 185 struct virtio_gpu_output *output = 186 drm_connector_to_virtio_gpu_output(connector); 187 int width, height; 188 189 width = le32_to_cpu(output->info.r.width); 190 height = le32_to_cpu(output->info.r.height); 191 192 if (!(mode->type & DRM_MODE_TYPE_PREFERRED)) 193 return MODE_OK; 194 if (mode->hdisplay == XRES_DEF && mode->vdisplay == YRES_DEF) 195 return MODE_OK; 196 if (mode->hdisplay <= width && mode->hdisplay >= width - 16 && 197 mode->vdisplay <= height && mode->vdisplay >= height - 16) 198 return MODE_OK; 199 200 DRM_DEBUG("del mode: %dx%d\n", mode->hdisplay, mode->vdisplay); 201 return MODE_BAD; 202 } 203 204 static const struct drm_encoder_helper_funcs virtio_gpu_enc_helper_funcs = { 205 .mode_set = virtio_gpu_enc_mode_set, 206 .enable = virtio_gpu_enc_enable, 207 .disable = virtio_gpu_enc_disable, 208 }; 209 210 static const struct drm_connector_helper_funcs virtio_gpu_conn_helper_funcs = { 211 .get_modes = virtio_gpu_conn_get_modes, 212 .mode_valid = virtio_gpu_conn_mode_valid, 213 }; 214 215 static enum drm_connector_status virtio_gpu_conn_detect( 216 struct drm_connector *connector, 217 bool force) 218 { 219 struct virtio_gpu_output *output = 220 drm_connector_to_virtio_gpu_output(connector); 221 222 if (output->info.enabled) 223 return connector_status_connected; 224 else 225 return connector_status_disconnected; 226 } 227 228 static void virtio_gpu_conn_destroy(struct drm_connector *connector) 229 { 230 drm_connector_unregister(connector); 231 drm_connector_cleanup(connector); 232 } 233 234 static const struct drm_connector_funcs virtio_gpu_connector_funcs = { 235 .detect = virtio_gpu_conn_detect, 236 .fill_modes = drm_helper_probe_single_connector_modes, 237 .destroy = virtio_gpu_conn_destroy, 238 .reset = drm_atomic_helper_connector_reset, 239 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 240 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 241 }; 242 243 static const struct drm_encoder_funcs virtio_gpu_enc_funcs = { 244 .destroy = drm_encoder_cleanup, 245 }; 246 247 static int vgdev_output_init(struct virtio_gpu_device *vgdev, int index) 248 { 249 struct drm_device *dev = vgdev->ddev; 250 struct virtio_gpu_output *output = vgdev->outputs + index; 251 struct drm_connector *connector = &output->conn; 252 struct drm_encoder *encoder = &output->enc; 253 struct drm_crtc *crtc = &output->crtc; 254 struct drm_plane *primary, *cursor; 255 256 output->index = index; 257 if (index == 0) { 258 output->info.enabled = cpu_to_le32(true); 259 output->info.r.width = cpu_to_le32(XRES_DEF); 260 output->info.r.height = cpu_to_le32(YRES_DEF); 261 } 262 263 primary = virtio_gpu_plane_init(vgdev, DRM_PLANE_TYPE_PRIMARY, index); 264 if (IS_ERR(primary)) 265 return PTR_ERR(primary); 266 cursor = virtio_gpu_plane_init(vgdev, DRM_PLANE_TYPE_CURSOR, index); 267 if (IS_ERR(cursor)) 268 return PTR_ERR(cursor); 269 drm_crtc_init_with_planes(dev, crtc, primary, cursor, 270 &virtio_gpu_crtc_funcs, NULL); 271 drm_crtc_helper_add(crtc, &virtio_gpu_crtc_helper_funcs); 272 273 drm_connector_init(dev, connector, &virtio_gpu_connector_funcs, 274 DRM_MODE_CONNECTOR_VIRTUAL); 275 drm_connector_helper_add(connector, &virtio_gpu_conn_helper_funcs); 276 if (vgdev->has_edid) 277 drm_connector_attach_edid_property(connector); 278 279 drm_encoder_init(dev, encoder, &virtio_gpu_enc_funcs, 280 DRM_MODE_ENCODER_VIRTUAL, NULL); 281 drm_encoder_helper_add(encoder, &virtio_gpu_enc_helper_funcs); 282 encoder->possible_crtcs = 1 << index; 283 284 drm_connector_attach_encoder(connector, encoder); 285 drm_connector_register(connector); 286 return 0; 287 } 288 289 static struct drm_framebuffer * 290 virtio_gpu_user_framebuffer_create(struct drm_device *dev, 291 struct drm_file *file_priv, 292 const struct drm_mode_fb_cmd2 *mode_cmd) 293 { 294 struct drm_gem_object *obj = NULL; 295 struct virtio_gpu_framebuffer *virtio_gpu_fb; 296 int ret; 297 298 if (mode_cmd->pixel_format != DRM_FORMAT_HOST_XRGB8888 && 299 mode_cmd->pixel_format != DRM_FORMAT_HOST_ARGB8888) 300 return ERR_PTR(-ENOENT); 301 302 /* lookup object associated with res handle */ 303 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]); 304 if (!obj) 305 return ERR_PTR(-EINVAL); 306 307 virtio_gpu_fb = kzalloc(sizeof(*virtio_gpu_fb), GFP_KERNEL); 308 if (virtio_gpu_fb == NULL) 309 return ERR_PTR(-ENOMEM); 310 311 ret = virtio_gpu_framebuffer_init(dev, virtio_gpu_fb, mode_cmd, obj); 312 if (ret) { 313 kfree(virtio_gpu_fb); 314 drm_gem_object_put_unlocked(obj); 315 return NULL; 316 } 317 318 return &virtio_gpu_fb->base; 319 } 320 321 static void vgdev_atomic_commit_tail(struct drm_atomic_state *state) 322 { 323 struct drm_device *dev = state->dev; 324 325 drm_atomic_helper_commit_modeset_disables(dev, state); 326 drm_atomic_helper_commit_modeset_enables(dev, state); 327 drm_atomic_helper_commit_planes(dev, state, 0); 328 329 drm_atomic_helper_fake_vblank(state); 330 drm_atomic_helper_commit_hw_done(state); 331 332 drm_atomic_helper_wait_for_vblanks(dev, state); 333 drm_atomic_helper_cleanup_planes(dev, state); 334 } 335 336 static const struct drm_mode_config_helper_funcs virtio_mode_config_helpers = { 337 .atomic_commit_tail = vgdev_atomic_commit_tail, 338 }; 339 340 static const struct drm_mode_config_funcs virtio_gpu_mode_funcs = { 341 .fb_create = virtio_gpu_user_framebuffer_create, 342 .atomic_check = drm_atomic_helper_check, 343 .atomic_commit = drm_atomic_helper_commit, 344 }; 345 346 void virtio_gpu_modeset_init(struct virtio_gpu_device *vgdev) 347 { 348 int i; 349 350 drm_mode_config_init(vgdev->ddev); 351 vgdev->ddev->mode_config.quirk_addfb_prefer_host_byte_order = true; 352 vgdev->ddev->mode_config.funcs = &virtio_gpu_mode_funcs; 353 vgdev->ddev->mode_config.helper_private = &virtio_mode_config_helpers; 354 355 /* modes will be validated against the framebuffer size */ 356 vgdev->ddev->mode_config.min_width = XRES_MIN; 357 vgdev->ddev->mode_config.min_height = YRES_MIN; 358 vgdev->ddev->mode_config.max_width = XRES_MAX; 359 vgdev->ddev->mode_config.max_height = YRES_MAX; 360 361 for (i = 0 ; i < vgdev->num_scanouts; ++i) 362 vgdev_output_init(vgdev, i); 363 364 drm_mode_config_reset(vgdev->ddev); 365 } 366 367 void virtio_gpu_modeset_fini(struct virtio_gpu_device *vgdev) 368 { 369 int i; 370 371 for (i = 0 ; i < vgdev->num_scanouts; ++i) 372 kfree(vgdev->outputs[i].edid); 373 drm_mode_config_cleanup(vgdev->ddev); 374 } 375