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