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