1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright (C) 2013-2017 Oracle Corporation 4 * This file is based on ast_mode.c 5 * Copyright 2012 Red Hat Inc. 6 * Parts based on xf86-video-ast 7 * Copyright (c) 2005 ASPEED Technology Inc. 8 * Authors: Dave Airlie <airlied@redhat.com> 9 * Michael Thayer <michael.thayer@oracle.com, 10 * Hans de Goede <hdegoede@redhat.com> 11 */ 12 #include <linux/export.h> 13 14 #include <drm/drm_atomic.h> 15 #include <drm/drm_atomic_helper.h> 16 #include <drm/drm_fourcc.h> 17 #include <drm/drm_plane_helper.h> 18 #include <drm/drm_probe_helper.h> 19 #include <drm/drm_vblank.h> 20 21 #include "hgsmi_channels.h" 22 #include "vbox_drv.h" 23 #include "vboxvideo.h" 24 25 /* 26 * Set a graphics mode. Poke any required values into registers, do an HGSMI 27 * mode set and tell the host we support advanced graphics functions. 28 */ 29 static void vbox_do_modeset(struct drm_crtc *crtc) 30 { 31 struct drm_framebuffer *fb = crtc->primary->state->fb; 32 struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc); 33 struct vbox_private *vbox; 34 int width, height, bpp, pitch; 35 u16 flags; 36 s32 x_offset, y_offset; 37 38 vbox = crtc->dev->dev_private; 39 width = vbox_crtc->width ? vbox_crtc->width : 640; 40 height = vbox_crtc->height ? vbox_crtc->height : 480; 41 bpp = fb ? fb->format->cpp[0] * 8 : 32; 42 pitch = fb ? fb->pitches[0] : width * bpp / 8; 43 x_offset = vbox->single_framebuffer ? vbox_crtc->x : vbox_crtc->x_hint; 44 y_offset = vbox->single_framebuffer ? vbox_crtc->y : vbox_crtc->y_hint; 45 46 /* 47 * This is the old way of setting graphics modes. It assumed one screen 48 * and a frame-buffer at the start of video RAM. On older versions of 49 * VirtualBox, certain parts of the code still assume that the first 50 * screen is programmed this way, so try to fake it. 51 */ 52 if (vbox_crtc->crtc_id == 0 && fb && 53 vbox_crtc->fb_offset / pitch < 0xffff - crtc->y && 54 vbox_crtc->fb_offset % (bpp / 8) == 0) { 55 vbox_write_ioport(VBE_DISPI_INDEX_XRES, width); 56 vbox_write_ioport(VBE_DISPI_INDEX_YRES, height); 57 vbox_write_ioport(VBE_DISPI_INDEX_VIRT_WIDTH, pitch * 8 / bpp); 58 vbox_write_ioport(VBE_DISPI_INDEX_BPP, bpp); 59 vbox_write_ioport(VBE_DISPI_INDEX_ENABLE, VBE_DISPI_ENABLED); 60 vbox_write_ioport(VBE_DISPI_INDEX_X_OFFSET, 61 vbox_crtc->fb_offset % pitch / bpp * 8 + vbox_crtc->x); 62 vbox_write_ioport(VBE_DISPI_INDEX_Y_OFFSET, 63 vbox_crtc->fb_offset / pitch + vbox_crtc->y); 64 } 65 66 flags = VBVA_SCREEN_F_ACTIVE; 67 flags |= (fb && crtc->state->enable) ? 0 : VBVA_SCREEN_F_BLANK; 68 flags |= vbox_crtc->disconnected ? VBVA_SCREEN_F_DISABLED : 0; 69 hgsmi_process_display_info(vbox->guest_pool, vbox_crtc->crtc_id, 70 x_offset, y_offset, 71 vbox_crtc->x * bpp / 8 + 72 vbox_crtc->y * pitch, 73 pitch, width, height, bpp, flags); 74 } 75 76 static int vbox_set_view(struct drm_crtc *crtc) 77 { 78 struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc); 79 struct vbox_private *vbox = crtc->dev->dev_private; 80 struct vbva_infoview *p; 81 82 /* 83 * Tell the host about the view. This design originally targeted the 84 * Windows XP driver architecture and assumed that each screen would 85 * have a dedicated frame buffer with the command buffer following it, 86 * the whole being a "view". The host works out which screen a command 87 * buffer belongs to by checking whether it is in the first view, then 88 * whether it is in the second and so on. The first match wins. We 89 * cheat around this by making the first view be the managed memory 90 * plus the first command buffer, the second the same plus the second 91 * buffer and so on. 92 */ 93 p = hgsmi_buffer_alloc(vbox->guest_pool, sizeof(*p), 94 HGSMI_CH_VBVA, VBVA_INFO_VIEW); 95 if (!p) 96 return -ENOMEM; 97 98 p->view_index = vbox_crtc->crtc_id; 99 p->view_offset = vbox_crtc->fb_offset; 100 p->view_size = vbox->available_vram_size - vbox_crtc->fb_offset + 101 vbox_crtc->crtc_id * VBVA_MIN_BUFFER_SIZE; 102 p->max_screen_size = vbox->available_vram_size - vbox_crtc->fb_offset; 103 104 hgsmi_buffer_submit(vbox->guest_pool, p); 105 hgsmi_buffer_free(vbox->guest_pool, p); 106 107 return 0; 108 } 109 110 /* 111 * Try to map the layout of virtual screens to the range of the input device. 112 * Return true if we need to re-set the crtc modes due to screen offset 113 * changes. 114 */ 115 static bool vbox_set_up_input_mapping(struct vbox_private *vbox) 116 { 117 struct drm_crtc *crtci; 118 struct drm_connector *connectori; 119 struct drm_framebuffer *fb, *fb1 = NULL; 120 bool single_framebuffer = true; 121 bool old_single_framebuffer = vbox->single_framebuffer; 122 u16 width = 0, height = 0; 123 124 /* 125 * Are we using an X.Org-style single large frame-buffer for all crtcs? 126 * If so then screen layout can be deduced from the crtc offsets. 127 * Same fall-back if this is the fbdev frame-buffer. 128 */ 129 list_for_each_entry(crtci, &vbox->ddev.mode_config.crtc_list, head) { 130 fb = crtci->primary->state->fb; 131 if (!fb) 132 continue; 133 134 if (!fb1) { 135 fb1 = fb; 136 if (to_vbox_framebuffer(fb1) == &vbox->afb) 137 break; 138 } else if (fb != fb1) { 139 single_framebuffer = false; 140 } 141 } 142 if (!fb1) 143 return false; 144 145 if (single_framebuffer) { 146 vbox->single_framebuffer = true; 147 vbox->input_mapping_width = fb1->width; 148 vbox->input_mapping_height = fb1->height; 149 return old_single_framebuffer != vbox->single_framebuffer; 150 } 151 /* Otherwise calculate the total span of all screens. */ 152 list_for_each_entry(connectori, &vbox->ddev.mode_config.connector_list, 153 head) { 154 struct vbox_connector *vbox_connector = 155 to_vbox_connector(connectori); 156 struct vbox_crtc *vbox_crtc = vbox_connector->vbox_crtc; 157 158 width = max_t(u16, width, vbox_crtc->x_hint + 159 vbox_connector->mode_hint.width); 160 height = max_t(u16, height, vbox_crtc->y_hint + 161 vbox_connector->mode_hint.height); 162 } 163 164 vbox->single_framebuffer = false; 165 vbox->input_mapping_width = width; 166 vbox->input_mapping_height = height; 167 168 return old_single_framebuffer != vbox->single_framebuffer; 169 } 170 171 static void vbox_crtc_set_base_and_mode(struct drm_crtc *crtc, 172 struct drm_framebuffer *fb, 173 int x, int y) 174 { 175 struct vbox_bo *bo = gem_to_vbox_bo(to_vbox_framebuffer(fb)->obj); 176 struct vbox_private *vbox = crtc->dev->dev_private; 177 struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc); 178 bool needs_modeset = drm_atomic_crtc_needs_modeset(crtc->state); 179 180 mutex_lock(&vbox->hw_mutex); 181 182 if (crtc->state->enable) { 183 vbox_crtc->width = crtc->state->mode.hdisplay; 184 vbox_crtc->height = crtc->state->mode.vdisplay; 185 } 186 187 vbox_crtc->x = x; 188 vbox_crtc->y = y; 189 vbox_crtc->fb_offset = vbox_bo_gpu_offset(bo); 190 191 /* vbox_do_modeset() checks vbox->single_framebuffer so update it now */ 192 if (needs_modeset && vbox_set_up_input_mapping(vbox)) { 193 struct drm_crtc *crtci; 194 195 list_for_each_entry(crtci, &vbox->ddev.mode_config.crtc_list, 196 head) { 197 if (crtci == crtc) 198 continue; 199 vbox_do_modeset(crtci); 200 } 201 } 202 203 vbox_set_view(crtc); 204 vbox_do_modeset(crtc); 205 206 if (needs_modeset) 207 hgsmi_update_input_mapping(vbox->guest_pool, 0, 0, 208 vbox->input_mapping_width, 209 vbox->input_mapping_height); 210 211 mutex_unlock(&vbox->hw_mutex); 212 } 213 214 static void vbox_crtc_atomic_enable(struct drm_crtc *crtc, 215 struct drm_crtc_state *old_crtc_state) 216 { 217 } 218 219 static void vbox_crtc_atomic_disable(struct drm_crtc *crtc, 220 struct drm_crtc_state *old_crtc_state) 221 { 222 } 223 224 static void vbox_crtc_atomic_flush(struct drm_crtc *crtc, 225 struct drm_crtc_state *old_crtc_state) 226 { 227 struct drm_pending_vblank_event *event; 228 unsigned long flags; 229 230 if (crtc->state && crtc->state->event) { 231 event = crtc->state->event; 232 crtc->state->event = NULL; 233 234 spin_lock_irqsave(&crtc->dev->event_lock, flags); 235 drm_crtc_send_vblank_event(crtc, event); 236 spin_unlock_irqrestore(&crtc->dev->event_lock, flags); 237 } 238 } 239 240 static const struct drm_crtc_helper_funcs vbox_crtc_helper_funcs = { 241 .atomic_enable = vbox_crtc_atomic_enable, 242 .atomic_disable = vbox_crtc_atomic_disable, 243 .atomic_flush = vbox_crtc_atomic_flush, 244 }; 245 246 static void vbox_crtc_destroy(struct drm_crtc *crtc) 247 { 248 drm_crtc_cleanup(crtc); 249 kfree(crtc); 250 } 251 252 static const struct drm_crtc_funcs vbox_crtc_funcs = { 253 .set_config = drm_atomic_helper_set_config, 254 .page_flip = drm_atomic_helper_page_flip, 255 /* .gamma_set = vbox_crtc_gamma_set, */ 256 .destroy = vbox_crtc_destroy, 257 .reset = drm_atomic_helper_crtc_reset, 258 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, 259 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, 260 }; 261 262 static int vbox_primary_atomic_check(struct drm_plane *plane, 263 struct drm_plane_state *new_state) 264 { 265 struct drm_crtc_state *crtc_state = NULL; 266 267 if (new_state->crtc) { 268 crtc_state = drm_atomic_get_existing_crtc_state( 269 new_state->state, new_state->crtc); 270 if (WARN_ON(!crtc_state)) 271 return -EINVAL; 272 } 273 274 return drm_atomic_helper_check_plane_state(new_state, crtc_state, 275 DRM_PLANE_HELPER_NO_SCALING, 276 DRM_PLANE_HELPER_NO_SCALING, 277 false, true); 278 } 279 280 static void vbox_primary_atomic_update(struct drm_plane *plane, 281 struct drm_plane_state *old_state) 282 { 283 struct drm_crtc *crtc = plane->state->crtc; 284 struct drm_framebuffer *fb = plane->state->fb; 285 286 vbox_crtc_set_base_and_mode(crtc, fb, 287 plane->state->src_x >> 16, 288 plane->state->src_y >> 16); 289 } 290 291 static void vbox_primary_atomic_disable(struct drm_plane *plane, 292 struct drm_plane_state *old_state) 293 { 294 struct drm_crtc *crtc = old_state->crtc; 295 296 /* vbox_do_modeset checks plane->state->fb and will disable if NULL */ 297 vbox_crtc_set_base_and_mode(crtc, old_state->fb, 298 old_state->src_x >> 16, 299 old_state->src_y >> 16); 300 } 301 302 static int vbox_primary_prepare_fb(struct drm_plane *plane, 303 struct drm_plane_state *new_state) 304 { 305 struct vbox_bo *bo; 306 int ret; 307 308 if (!new_state->fb) 309 return 0; 310 311 bo = gem_to_vbox_bo(to_vbox_framebuffer(new_state->fb)->obj); 312 ret = vbox_bo_pin(bo, TTM_PL_FLAG_VRAM); 313 if (ret) 314 DRM_WARN("Error %d pinning new fb, out of video mem?\n", ret); 315 316 return ret; 317 } 318 319 static void vbox_primary_cleanup_fb(struct drm_plane *plane, 320 struct drm_plane_state *old_state) 321 { 322 struct vbox_bo *bo; 323 324 if (!old_state->fb) 325 return; 326 327 bo = gem_to_vbox_bo(to_vbox_framebuffer(old_state->fb)->obj); 328 vbox_bo_unpin(bo); 329 } 330 331 static int vbox_cursor_atomic_check(struct drm_plane *plane, 332 struct drm_plane_state *new_state) 333 { 334 struct drm_crtc_state *crtc_state = NULL; 335 u32 width = new_state->crtc_w; 336 u32 height = new_state->crtc_h; 337 int ret; 338 339 if (new_state->crtc) { 340 crtc_state = drm_atomic_get_existing_crtc_state( 341 new_state->state, new_state->crtc); 342 if (WARN_ON(!crtc_state)) 343 return -EINVAL; 344 } 345 346 ret = drm_atomic_helper_check_plane_state(new_state, crtc_state, 347 DRM_PLANE_HELPER_NO_SCALING, 348 DRM_PLANE_HELPER_NO_SCALING, 349 true, true); 350 if (ret) 351 return ret; 352 353 if (!new_state->fb) 354 return 0; 355 356 if (width > VBOX_MAX_CURSOR_WIDTH || height > VBOX_MAX_CURSOR_HEIGHT || 357 width == 0 || height == 0) 358 return -EINVAL; 359 360 return 0; 361 } 362 363 /* 364 * Copy the ARGB image and generate the mask, which is needed in case the host 365 * does not support ARGB cursors. The mask is a 1BPP bitmap with the bit set 366 * if the corresponding alpha value in the ARGB image is greater than 0xF0. 367 */ 368 static void copy_cursor_image(u8 *src, u8 *dst, u32 width, u32 height, 369 size_t mask_size) 370 { 371 size_t line_size = (width + 7) / 8; 372 u32 i, j; 373 374 memcpy(dst + mask_size, src, width * height * 4); 375 for (i = 0; i < height; ++i) 376 for (j = 0; j < width; ++j) 377 if (((u32 *)src)[i * width + j] > 0xf0000000) 378 dst[i * line_size + j / 8] |= (0x80 >> (j % 8)); 379 } 380 381 static void vbox_cursor_atomic_update(struct drm_plane *plane, 382 struct drm_plane_state *old_state) 383 { 384 struct vbox_private *vbox = 385 container_of(plane->dev, struct vbox_private, ddev); 386 struct vbox_crtc *vbox_crtc = to_vbox_crtc(plane->state->crtc); 387 struct drm_framebuffer *fb = plane->state->fb; 388 struct vbox_bo *bo = gem_to_vbox_bo(to_vbox_framebuffer(fb)->obj); 389 u32 width = plane->state->crtc_w; 390 u32 height = plane->state->crtc_h; 391 size_t data_size, mask_size; 392 u32 flags; 393 u8 *src; 394 395 /* 396 * VirtualBox uses the host windowing system to draw the cursor so 397 * moves are a no-op, we only need to upload new cursor sprites. 398 */ 399 if (fb == old_state->fb) 400 return; 401 402 mutex_lock(&vbox->hw_mutex); 403 404 vbox_crtc->cursor_enabled = true; 405 406 /* pinning is done in prepare/cleanup framebuffer */ 407 src = vbox_bo_kmap(bo); 408 if (IS_ERR(src)) { 409 mutex_unlock(&vbox->hw_mutex); 410 DRM_WARN("Could not kmap cursor bo, skipping update\n"); 411 return; 412 } 413 414 /* 415 * The mask must be calculated based on the alpha 416 * channel, one bit per ARGB word, and must be 32-bit 417 * padded. 418 */ 419 mask_size = ((width + 7) / 8 * height + 3) & ~3; 420 data_size = width * height * 4 + mask_size; 421 422 copy_cursor_image(src, vbox->cursor_data, width, height, mask_size); 423 vbox_bo_kunmap(bo); 424 425 flags = VBOX_MOUSE_POINTER_VISIBLE | VBOX_MOUSE_POINTER_SHAPE | 426 VBOX_MOUSE_POINTER_ALPHA; 427 hgsmi_update_pointer_shape(vbox->guest_pool, flags, 428 min_t(u32, max(fb->hot_x, 0), width), 429 min_t(u32, max(fb->hot_y, 0), height), 430 width, height, vbox->cursor_data, data_size); 431 432 mutex_unlock(&vbox->hw_mutex); 433 } 434 435 static void vbox_cursor_atomic_disable(struct drm_plane *plane, 436 struct drm_plane_state *old_state) 437 { 438 struct vbox_private *vbox = 439 container_of(plane->dev, struct vbox_private, ddev); 440 struct vbox_crtc *vbox_crtc = to_vbox_crtc(old_state->crtc); 441 bool cursor_enabled = false; 442 struct drm_crtc *crtci; 443 444 mutex_lock(&vbox->hw_mutex); 445 446 vbox_crtc->cursor_enabled = false; 447 448 list_for_each_entry(crtci, &vbox->ddev.mode_config.crtc_list, head) { 449 if (to_vbox_crtc(crtci)->cursor_enabled) 450 cursor_enabled = true; 451 } 452 453 if (!cursor_enabled) 454 hgsmi_update_pointer_shape(vbox->guest_pool, 0, 0, 0, 455 0, 0, NULL, 0); 456 457 mutex_unlock(&vbox->hw_mutex); 458 } 459 460 static int vbox_cursor_prepare_fb(struct drm_plane *plane, 461 struct drm_plane_state *new_state) 462 { 463 struct vbox_bo *bo; 464 465 if (!new_state->fb) 466 return 0; 467 468 bo = gem_to_vbox_bo(to_vbox_framebuffer(new_state->fb)->obj); 469 return vbox_bo_pin(bo, TTM_PL_FLAG_SYSTEM); 470 } 471 472 static void vbox_cursor_cleanup_fb(struct drm_plane *plane, 473 struct drm_plane_state *old_state) 474 { 475 struct vbox_bo *bo; 476 477 if (!plane->state->fb) 478 return; 479 480 bo = gem_to_vbox_bo(to_vbox_framebuffer(plane->state->fb)->obj); 481 vbox_bo_unpin(bo); 482 } 483 484 static const u32 vbox_cursor_plane_formats[] = { 485 DRM_FORMAT_ARGB8888, 486 }; 487 488 static const struct drm_plane_helper_funcs vbox_cursor_helper_funcs = { 489 .atomic_check = vbox_cursor_atomic_check, 490 .atomic_update = vbox_cursor_atomic_update, 491 .atomic_disable = vbox_cursor_atomic_disable, 492 .prepare_fb = vbox_cursor_prepare_fb, 493 .cleanup_fb = vbox_cursor_cleanup_fb, 494 }; 495 496 static const struct drm_plane_funcs vbox_cursor_plane_funcs = { 497 .update_plane = drm_atomic_helper_update_plane, 498 .disable_plane = drm_atomic_helper_disable_plane, 499 .destroy = drm_primary_helper_destroy, 500 .reset = drm_atomic_helper_plane_reset, 501 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state, 502 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state, 503 }; 504 505 static const u32 vbox_primary_plane_formats[] = { 506 DRM_FORMAT_XRGB8888, 507 DRM_FORMAT_ARGB8888, 508 }; 509 510 static const struct drm_plane_helper_funcs vbox_primary_helper_funcs = { 511 .atomic_check = vbox_primary_atomic_check, 512 .atomic_update = vbox_primary_atomic_update, 513 .atomic_disable = vbox_primary_atomic_disable, 514 .prepare_fb = vbox_primary_prepare_fb, 515 .cleanup_fb = vbox_primary_cleanup_fb, 516 }; 517 518 static const struct drm_plane_funcs vbox_primary_plane_funcs = { 519 .update_plane = drm_atomic_helper_update_plane, 520 .disable_plane = drm_atomic_helper_disable_plane, 521 .destroy = drm_primary_helper_destroy, 522 .reset = drm_atomic_helper_plane_reset, 523 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state, 524 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state, 525 }; 526 527 static struct drm_plane *vbox_create_plane(struct vbox_private *vbox, 528 unsigned int possible_crtcs, 529 enum drm_plane_type type) 530 { 531 const struct drm_plane_helper_funcs *helper_funcs = NULL; 532 const struct drm_plane_funcs *funcs; 533 struct drm_plane *plane; 534 const u32 *formats; 535 int num_formats; 536 int err; 537 538 if (type == DRM_PLANE_TYPE_PRIMARY) { 539 funcs = &vbox_primary_plane_funcs; 540 formats = vbox_primary_plane_formats; 541 helper_funcs = &vbox_primary_helper_funcs; 542 num_formats = ARRAY_SIZE(vbox_primary_plane_formats); 543 } else if (type == DRM_PLANE_TYPE_CURSOR) { 544 funcs = &vbox_cursor_plane_funcs; 545 formats = vbox_cursor_plane_formats; 546 helper_funcs = &vbox_cursor_helper_funcs; 547 num_formats = ARRAY_SIZE(vbox_cursor_plane_formats); 548 } else { 549 return ERR_PTR(-EINVAL); 550 } 551 552 plane = kzalloc(sizeof(*plane), GFP_KERNEL); 553 if (!plane) 554 return ERR_PTR(-ENOMEM); 555 556 err = drm_universal_plane_init(&vbox->ddev, plane, possible_crtcs, 557 funcs, formats, num_formats, 558 NULL, type, NULL); 559 if (err) 560 goto free_plane; 561 562 drm_plane_helper_add(plane, helper_funcs); 563 564 return plane; 565 566 free_plane: 567 kfree(plane); 568 return ERR_PTR(-EINVAL); 569 } 570 571 static struct vbox_crtc *vbox_crtc_init(struct drm_device *dev, unsigned int i) 572 { 573 struct vbox_private *vbox = 574 container_of(dev, struct vbox_private, ddev); 575 struct drm_plane *cursor = NULL; 576 struct vbox_crtc *vbox_crtc; 577 struct drm_plane *primary; 578 u32 caps = 0; 579 int ret; 580 581 ret = hgsmi_query_conf(vbox->guest_pool, 582 VBOX_VBVA_CONF32_CURSOR_CAPABILITIES, &caps); 583 if (ret) 584 return ERR_PTR(ret); 585 586 vbox_crtc = kzalloc(sizeof(*vbox_crtc), GFP_KERNEL); 587 if (!vbox_crtc) 588 return ERR_PTR(-ENOMEM); 589 590 primary = vbox_create_plane(vbox, 1 << i, DRM_PLANE_TYPE_PRIMARY); 591 if (IS_ERR(primary)) { 592 ret = PTR_ERR(primary); 593 goto free_mem; 594 } 595 596 if ((caps & VBOX_VBVA_CURSOR_CAPABILITY_HARDWARE)) { 597 cursor = vbox_create_plane(vbox, 1 << i, DRM_PLANE_TYPE_CURSOR); 598 if (IS_ERR(cursor)) { 599 ret = PTR_ERR(cursor); 600 goto clean_primary; 601 } 602 } else { 603 DRM_WARN("VirtualBox host is too old, no cursor support\n"); 604 } 605 606 vbox_crtc->crtc_id = i; 607 608 ret = drm_crtc_init_with_planes(dev, &vbox_crtc->base, primary, cursor, 609 &vbox_crtc_funcs, NULL); 610 if (ret) 611 goto clean_cursor; 612 613 drm_mode_crtc_set_gamma_size(&vbox_crtc->base, 256); 614 drm_crtc_helper_add(&vbox_crtc->base, &vbox_crtc_helper_funcs); 615 616 return vbox_crtc; 617 618 clean_cursor: 619 if (cursor) { 620 drm_plane_cleanup(cursor); 621 kfree(cursor); 622 } 623 clean_primary: 624 drm_plane_cleanup(primary); 625 kfree(primary); 626 free_mem: 627 kfree(vbox_crtc); 628 return ERR_PTR(ret); 629 } 630 631 static void vbox_encoder_destroy(struct drm_encoder *encoder) 632 { 633 drm_encoder_cleanup(encoder); 634 kfree(encoder); 635 } 636 637 static const struct drm_encoder_funcs vbox_enc_funcs = { 638 .destroy = vbox_encoder_destroy, 639 }; 640 641 static struct drm_encoder *vbox_encoder_init(struct drm_device *dev, 642 unsigned int i) 643 { 644 struct vbox_encoder *vbox_encoder; 645 646 vbox_encoder = kzalloc(sizeof(*vbox_encoder), GFP_KERNEL); 647 if (!vbox_encoder) 648 return NULL; 649 650 drm_encoder_init(dev, &vbox_encoder->base, &vbox_enc_funcs, 651 DRM_MODE_ENCODER_DAC, NULL); 652 653 vbox_encoder->base.possible_crtcs = 1 << i; 654 return &vbox_encoder->base; 655 } 656 657 /* 658 * Generate EDID data with a mode-unique serial number for the virtual 659 * monitor to try to persuade Unity that different modes correspond to 660 * different monitors and it should not try to force the same resolution on 661 * them. 662 */ 663 static void vbox_set_edid(struct drm_connector *connector, int width, 664 int height) 665 { 666 enum { EDID_SIZE = 128 }; 667 unsigned char edid[EDID_SIZE] = { 668 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, /* header */ 669 0x58, 0x58, /* manufacturer (VBX) */ 670 0x00, 0x00, /* product code */ 671 0x00, 0x00, 0x00, 0x00, /* serial number goes here */ 672 0x01, /* week of manufacture */ 673 0x00, /* year of manufacture */ 674 0x01, 0x03, /* EDID version */ 675 0x80, /* capabilities - digital */ 676 0x00, /* horiz. res in cm, zero for projectors */ 677 0x00, /* vert. res in cm */ 678 0x78, /* display gamma (120 == 2.2). */ 679 0xEE, /* features (standby, suspend, off, RGB, std */ 680 /* colour space, preferred timing mode) */ 681 0xEE, 0x91, 0xA3, 0x54, 0x4C, 0x99, 0x26, 0x0F, 0x50, 0x54, 682 /* chromaticity for standard colour space. */ 683 0x00, 0x00, 0x00, /* no default timings */ 684 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 685 0x01, 0x01, 686 0x01, 0x01, 0x01, 0x01, /* no standard timings */ 687 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x00, 0x02, 0x02, 688 0x02, 0x02, 689 /* descriptor block 1 goes below */ 690 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 691 /* descriptor block 2, monitor ranges */ 692 0x00, 0x00, 0x00, 0xFD, 0x00, 693 0x00, 0xC8, 0x00, 0xC8, 0x64, 0x00, 0x0A, 0x20, 0x20, 0x20, 694 0x20, 0x20, 695 /* 0-200Hz vertical, 0-200KHz horizontal, 1000MHz pixel clock */ 696 0x20, 697 /* descriptor block 3, monitor name */ 698 0x00, 0x00, 0x00, 0xFC, 0x00, 699 'V', 'B', 'O', 'X', ' ', 'm', 'o', 'n', 'i', 't', 'o', 'r', 700 '\n', 701 /* descriptor block 4: dummy data */ 702 0x00, 0x00, 0x00, 0x10, 0x00, 703 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20, 704 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 705 0x20, 706 0x00, /* number of extensions */ 707 0x00 /* checksum goes here */ 708 }; 709 int clock = (width + 6) * (height + 6) * 60 / 10000; 710 unsigned int i, sum = 0; 711 712 edid[12] = width & 0xff; 713 edid[13] = width >> 8; 714 edid[14] = height & 0xff; 715 edid[15] = height >> 8; 716 edid[54] = clock & 0xff; 717 edid[55] = clock >> 8; 718 edid[56] = width & 0xff; 719 edid[58] = (width >> 4) & 0xf0; 720 edid[59] = height & 0xff; 721 edid[61] = (height >> 4) & 0xf0; 722 for (i = 0; i < EDID_SIZE - 1; ++i) 723 sum += edid[i]; 724 edid[EDID_SIZE - 1] = (0x100 - (sum & 0xFF)) & 0xFF; 725 drm_connector_update_edid_property(connector, (struct edid *)edid); 726 } 727 728 static int vbox_get_modes(struct drm_connector *connector) 729 { 730 struct vbox_connector *vbox_connector = NULL; 731 struct drm_display_mode *mode = NULL; 732 struct vbox_private *vbox = NULL; 733 unsigned int num_modes = 0; 734 int preferred_width, preferred_height; 735 736 vbox_connector = to_vbox_connector(connector); 737 vbox = connector->dev->dev_private; 738 739 hgsmi_report_flags_location(vbox->guest_pool, GUEST_HEAP_OFFSET(vbox) + 740 HOST_FLAGS_OFFSET); 741 if (vbox_connector->vbox_crtc->crtc_id == 0) 742 vbox_report_caps(vbox); 743 744 num_modes = drm_add_modes_noedid(connector, 2560, 1600); 745 preferred_width = vbox_connector->mode_hint.width ? 746 vbox_connector->mode_hint.width : 1024; 747 preferred_height = vbox_connector->mode_hint.height ? 748 vbox_connector->mode_hint.height : 768; 749 mode = drm_cvt_mode(connector->dev, preferred_width, preferred_height, 750 60, false, false, false); 751 if (mode) { 752 mode->type |= DRM_MODE_TYPE_PREFERRED; 753 drm_mode_probed_add(connector, mode); 754 ++num_modes; 755 } 756 vbox_set_edid(connector, preferred_width, preferred_height); 757 758 if (vbox_connector->vbox_crtc->x_hint != -1) 759 drm_object_property_set_value(&connector->base, 760 vbox->ddev.mode_config.suggested_x_property, 761 vbox_connector->vbox_crtc->x_hint); 762 else 763 drm_object_property_set_value(&connector->base, 764 vbox->ddev.mode_config.suggested_x_property, 0); 765 766 if (vbox_connector->vbox_crtc->y_hint != -1) 767 drm_object_property_set_value(&connector->base, 768 vbox->ddev.mode_config.suggested_y_property, 769 vbox_connector->vbox_crtc->y_hint); 770 else 771 drm_object_property_set_value(&connector->base, 772 vbox->ddev.mode_config.suggested_y_property, 0); 773 774 return num_modes; 775 } 776 777 static void vbox_connector_destroy(struct drm_connector *connector) 778 { 779 drm_connector_unregister(connector); 780 drm_connector_cleanup(connector); 781 kfree(connector); 782 } 783 784 static enum drm_connector_status 785 vbox_connector_detect(struct drm_connector *connector, bool force) 786 { 787 struct vbox_connector *vbox_connector; 788 789 vbox_connector = to_vbox_connector(connector); 790 791 return vbox_connector->mode_hint.disconnected ? 792 connector_status_disconnected : connector_status_connected; 793 } 794 795 static int vbox_fill_modes(struct drm_connector *connector, u32 max_x, 796 u32 max_y) 797 { 798 struct vbox_connector *vbox_connector; 799 struct drm_device *dev; 800 struct drm_display_mode *mode, *iterator; 801 802 vbox_connector = to_vbox_connector(connector); 803 dev = vbox_connector->base.dev; 804 list_for_each_entry_safe(mode, iterator, &connector->modes, head) { 805 list_del(&mode->head); 806 drm_mode_destroy(dev, mode); 807 } 808 809 return drm_helper_probe_single_connector_modes(connector, max_x, max_y); 810 } 811 812 static const struct drm_connector_helper_funcs vbox_connector_helper_funcs = { 813 .get_modes = vbox_get_modes, 814 }; 815 816 static const struct drm_connector_funcs vbox_connector_funcs = { 817 .detect = vbox_connector_detect, 818 .fill_modes = vbox_fill_modes, 819 .destroy = vbox_connector_destroy, 820 .reset = drm_atomic_helper_connector_reset, 821 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 822 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 823 }; 824 825 static int vbox_connector_init(struct drm_device *dev, 826 struct vbox_crtc *vbox_crtc, 827 struct drm_encoder *encoder) 828 { 829 struct vbox_connector *vbox_connector; 830 struct drm_connector *connector; 831 832 vbox_connector = kzalloc(sizeof(*vbox_connector), GFP_KERNEL); 833 if (!vbox_connector) 834 return -ENOMEM; 835 836 connector = &vbox_connector->base; 837 vbox_connector->vbox_crtc = vbox_crtc; 838 839 drm_connector_init(dev, connector, &vbox_connector_funcs, 840 DRM_MODE_CONNECTOR_VGA); 841 drm_connector_helper_add(connector, &vbox_connector_helper_funcs); 842 843 connector->interlace_allowed = 0; 844 connector->doublescan_allowed = 0; 845 846 drm_mode_create_suggested_offset_properties(dev); 847 drm_object_attach_property(&connector->base, 848 dev->mode_config.suggested_x_property, 0); 849 drm_object_attach_property(&connector->base, 850 dev->mode_config.suggested_y_property, 0); 851 852 drm_connector_attach_encoder(connector, encoder); 853 854 return 0; 855 } 856 857 static struct drm_framebuffer *vbox_user_framebuffer_create( 858 struct drm_device *dev, 859 struct drm_file *filp, 860 const struct drm_mode_fb_cmd2 *mode_cmd) 861 { 862 struct vbox_private *vbox = 863 container_of(dev, struct vbox_private, ddev); 864 struct drm_gem_object *obj; 865 struct vbox_framebuffer *vbox_fb; 866 int ret = -ENOMEM; 867 868 obj = drm_gem_object_lookup(filp, mode_cmd->handles[0]); 869 if (!obj) 870 return ERR_PTR(-ENOENT); 871 872 vbox_fb = kzalloc(sizeof(*vbox_fb), GFP_KERNEL); 873 if (!vbox_fb) 874 goto err_unref_obj; 875 876 ret = vbox_framebuffer_init(vbox, vbox_fb, mode_cmd, obj); 877 if (ret) 878 goto err_free_vbox_fb; 879 880 return &vbox_fb->base; 881 882 err_free_vbox_fb: 883 kfree(vbox_fb); 884 err_unref_obj: 885 drm_gem_object_put_unlocked(obj); 886 return ERR_PTR(ret); 887 } 888 889 static const struct drm_mode_config_funcs vbox_mode_funcs = { 890 .fb_create = vbox_user_framebuffer_create, 891 .atomic_check = drm_atomic_helper_check, 892 .atomic_commit = drm_atomic_helper_commit, 893 }; 894 895 int vbox_mode_init(struct vbox_private *vbox) 896 { 897 struct drm_device *dev = &vbox->ddev; 898 struct drm_encoder *encoder; 899 struct vbox_crtc *vbox_crtc; 900 unsigned int i; 901 int ret; 902 903 drm_mode_config_init(dev); 904 905 dev->mode_config.funcs = (void *)&vbox_mode_funcs; 906 dev->mode_config.min_width = 0; 907 dev->mode_config.min_height = 0; 908 dev->mode_config.preferred_depth = 24; 909 dev->mode_config.max_width = VBE_DISPI_MAX_XRES; 910 dev->mode_config.max_height = VBE_DISPI_MAX_YRES; 911 912 for (i = 0; i < vbox->num_crtcs; ++i) { 913 vbox_crtc = vbox_crtc_init(dev, i); 914 if (IS_ERR(vbox_crtc)) { 915 ret = PTR_ERR(vbox_crtc); 916 goto err_drm_mode_cleanup; 917 } 918 encoder = vbox_encoder_init(dev, i); 919 if (!encoder) { 920 ret = -ENOMEM; 921 goto err_drm_mode_cleanup; 922 } 923 ret = vbox_connector_init(dev, vbox_crtc, encoder); 924 if (ret) 925 goto err_drm_mode_cleanup; 926 } 927 928 drm_mode_config_reset(dev); 929 return 0; 930 931 err_drm_mode_cleanup: 932 drm_mode_config_cleanup(dev); 933 return ret; 934 } 935 936 void vbox_mode_fini(struct vbox_private *vbox) 937 { 938 drm_mode_config_cleanup(&vbox->ddev); 939 } 940