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