| 1 | // SPDX-License-Identifier: GPL-2.0 OR MIT |
| 2 | /************************************************************************** |
| 3 | * |
| 4 | * Copyright (c) 2009-2025 Broadcom. All Rights Reserved. The term |
| 5 | * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. |
| 6 | * |
| 7 | **************************************************************************/ |
| 8 | |
| 9 | #include "vmwgfx_kms.h" |
| 10 | |
| 11 | #include "vmwgfx_bo.h" |
| 12 | #include "vmwgfx_resource_priv.h" |
| 13 | #include "vmwgfx_vkms.h" |
| 14 | #include "vmw_surface_cache.h" |
| 15 | |
| 16 | #include <drm/drm_atomic.h> |
| 17 | #include <drm/drm_atomic_helper.h> |
| 18 | #include <drm/drm_damage_helper.h> |
| 19 | #include <drm/drm_fourcc.h> |
| 20 | #include <drm/drm_rect.h> |
| 21 | #include <drm/drm_sysfs.h> |
| 22 | #include <drm/drm_edid.h> |
| 23 | |
| 24 | void vmw_du_init(struct vmw_display_unit *du) |
| 25 | { |
| 26 | vmw_vkms_crtc_init(crtc: &du->crtc); |
| 27 | } |
| 28 | |
| 29 | void vmw_du_cleanup(struct vmw_display_unit *du) |
| 30 | { |
| 31 | struct vmw_private *dev_priv = vmw_priv(dev: du->primary.dev); |
| 32 | |
| 33 | vmw_vkms_crtc_cleanup(crtc: &du->crtc); |
| 34 | drm_plane_cleanup(plane: &du->primary); |
| 35 | if (vmw_cmd_supported(vmw: dev_priv)) |
| 36 | drm_plane_cleanup(plane: &du->cursor.base); |
| 37 | |
| 38 | drm_connector_unregister(connector: &du->connector); |
| 39 | drm_crtc_cleanup(crtc: &du->crtc); |
| 40 | drm_encoder_cleanup(encoder: &du->encoder); |
| 41 | drm_connector_cleanup(connector: &du->connector); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | void vmw_du_primary_plane_destroy(struct drm_plane *plane) |
| 46 | { |
| 47 | drm_plane_cleanup(plane); |
| 48 | |
| 49 | /* Planes are static in our case so we don't free it */ |
| 50 | } |
| 51 | |
| 52 | |
| 53 | /** |
| 54 | * vmw_du_plane_unpin_surf - unpins resource associated with a framebuffer surface |
| 55 | * |
| 56 | * @vps: plane state associated with the display surface |
| 57 | */ |
| 58 | void vmw_du_plane_unpin_surf(struct vmw_plane_state *vps) |
| 59 | { |
| 60 | struct vmw_surface *surf = vmw_user_object_surface(uo: &vps->uo); |
| 61 | |
| 62 | if (surf) { |
| 63 | if (vps->pinned) { |
| 64 | vmw_resource_unpin(res: &surf->res); |
| 65 | vps->pinned--; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | |
| 71 | /** |
| 72 | * vmw_du_plane_cleanup_fb - Unpins the plane surface |
| 73 | * |
| 74 | * @plane: display plane |
| 75 | * @old_state: Contains the FB to clean up |
| 76 | * |
| 77 | * Unpins the framebuffer surface |
| 78 | * |
| 79 | * Returns 0 on success |
| 80 | */ |
| 81 | void |
| 82 | vmw_du_plane_cleanup_fb(struct drm_plane *plane, |
| 83 | struct drm_plane_state *old_state) |
| 84 | { |
| 85 | struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state); |
| 86 | |
| 87 | vmw_du_plane_unpin_surf(vps); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | /** |
| 92 | * vmw_du_primary_plane_atomic_check - check if the new state is okay |
| 93 | * |
| 94 | * @plane: display plane |
| 95 | * @state: info on the new plane state, including the FB |
| 96 | * |
| 97 | * Check if the new state is settable given the current state. Other |
| 98 | * than what the atomic helper checks, we care about crtc fitting |
| 99 | * the FB and maintaining one active framebuffer. |
| 100 | * |
| 101 | * Returns 0 on success |
| 102 | */ |
| 103 | int vmw_du_primary_plane_atomic_check(struct drm_plane *plane, |
| 104 | struct drm_atomic_state *state) |
| 105 | { |
| 106 | struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, |
| 107 | plane); |
| 108 | struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state, |
| 109 | plane); |
| 110 | struct drm_crtc_state *crtc_state = NULL; |
| 111 | struct drm_framebuffer *new_fb = new_state->fb; |
| 112 | struct drm_framebuffer *old_fb = old_state->fb; |
| 113 | int ret; |
| 114 | |
| 115 | /* |
| 116 | * Ignore damage clips if the framebuffer attached to the plane's state |
| 117 | * has changed since the last plane update (page-flip). In this case, a |
| 118 | * full plane update should happen because uploads are done per-buffer. |
| 119 | */ |
| 120 | if (old_fb != new_fb) |
| 121 | new_state->ignore_damage_clips = true; |
| 122 | |
| 123 | if (new_state->crtc) |
| 124 | crtc_state = drm_atomic_get_new_crtc_state(state, |
| 125 | crtc: new_state->crtc); |
| 126 | |
| 127 | ret = drm_atomic_helper_check_plane_state(plane_state: new_state, crtc_state, |
| 128 | DRM_PLANE_NO_SCALING, |
| 129 | DRM_PLANE_NO_SCALING, |
| 130 | can_position: false, can_update_disabled: true); |
| 131 | return ret; |
| 132 | } |
| 133 | |
| 134 | int vmw_du_crtc_atomic_check(struct drm_crtc *crtc, |
| 135 | struct drm_atomic_state *state) |
| 136 | { |
| 137 | struct vmw_private *vmw = vmw_priv(dev: crtc->dev); |
| 138 | struct drm_crtc_state *new_state = drm_atomic_get_new_crtc_state(state, |
| 139 | crtc); |
| 140 | struct vmw_display_unit *du = vmw_crtc_to_du(new_state->crtc); |
| 141 | int connector_mask = drm_connector_mask(connector: &du->connector); |
| 142 | bool has_primary = new_state->plane_mask & |
| 143 | drm_plane_mask(plane: crtc->primary); |
| 144 | |
| 145 | /* |
| 146 | * This is fine in general, but broken userspace might expect |
| 147 | * some actual rendering so give a clue as why it's blank. |
| 148 | */ |
| 149 | if (new_state->enable && !has_primary) |
| 150 | drm_dbg_driver(&vmw->drm, |
| 151 | "CRTC without a primary plane will be blank.\n" ); |
| 152 | |
| 153 | |
| 154 | if (new_state->connector_mask != connector_mask && |
| 155 | new_state->connector_mask != 0) { |
| 156 | DRM_ERROR("Invalid connectors configuration\n" ); |
| 157 | return -EINVAL; |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Our virtual device does not have a dot clock, so use the logical |
| 162 | * clock value as the dot clock. |
| 163 | */ |
| 164 | if (new_state->mode.crtc_clock == 0) |
| 165 | new_state->adjusted_mode.crtc_clock = new_state->mode.clock; |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc, |
| 172 | struct drm_atomic_state *state) |
| 173 | { |
| 174 | vmw_vkms_crtc_atomic_begin(crtc, state); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * vmw_du_crtc_duplicate_state - duplicate crtc state |
| 179 | * @crtc: DRM crtc |
| 180 | * |
| 181 | * Allocates and returns a copy of the crtc state (both common and |
| 182 | * vmw-specific) for the specified crtc. |
| 183 | * |
| 184 | * Returns: The newly allocated crtc state, or NULL on failure. |
| 185 | */ |
| 186 | struct drm_crtc_state * |
| 187 | vmw_du_crtc_duplicate_state(struct drm_crtc *crtc) |
| 188 | { |
| 189 | struct drm_crtc_state *state; |
| 190 | struct vmw_crtc_state *vcs; |
| 191 | |
| 192 | if (WARN_ON(!crtc->state)) |
| 193 | return NULL; |
| 194 | |
| 195 | vcs = kmemdup(crtc->state, sizeof(*vcs), GFP_KERNEL); |
| 196 | |
| 197 | if (!vcs) |
| 198 | return NULL; |
| 199 | |
| 200 | state = &vcs->base; |
| 201 | |
| 202 | __drm_atomic_helper_crtc_duplicate_state(crtc, state); |
| 203 | |
| 204 | return state; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | /** |
| 209 | * vmw_du_crtc_reset - creates a blank vmw crtc state |
| 210 | * @crtc: DRM crtc |
| 211 | * |
| 212 | * Resets the atomic state for @crtc by freeing the state pointer (which |
| 213 | * might be NULL, e.g. at driver load time) and allocating a new empty state |
| 214 | * object. |
| 215 | */ |
| 216 | void vmw_du_crtc_reset(struct drm_crtc *crtc) |
| 217 | { |
| 218 | struct vmw_crtc_state *vcs; |
| 219 | |
| 220 | |
| 221 | if (crtc->state) { |
| 222 | __drm_atomic_helper_crtc_destroy_state(state: crtc->state); |
| 223 | |
| 224 | kfree(vmw_crtc_state_to_vcs(crtc->state)); |
| 225 | } |
| 226 | |
| 227 | vcs = kzalloc(sizeof(*vcs), GFP_KERNEL); |
| 228 | |
| 229 | if (!vcs) { |
| 230 | DRM_ERROR("Cannot allocate vmw_crtc_state\n" ); |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | __drm_atomic_helper_crtc_reset(crtc, state: &vcs->base); |
| 235 | } |
| 236 | |
| 237 | |
| 238 | /** |
| 239 | * vmw_du_crtc_destroy_state - destroy crtc state |
| 240 | * @crtc: DRM crtc |
| 241 | * @state: state object to destroy |
| 242 | * |
| 243 | * Destroys the crtc state (both common and vmw-specific) for the |
| 244 | * specified plane. |
| 245 | */ |
| 246 | void |
| 247 | vmw_du_crtc_destroy_state(struct drm_crtc *crtc, |
| 248 | struct drm_crtc_state *state) |
| 249 | { |
| 250 | drm_atomic_helper_crtc_destroy_state(crtc, state); |
| 251 | } |
| 252 | |
| 253 | |
| 254 | /** |
| 255 | * vmw_du_plane_duplicate_state - duplicate plane state |
| 256 | * @plane: drm plane |
| 257 | * |
| 258 | * Allocates and returns a copy of the plane state (both common and |
| 259 | * vmw-specific) for the specified plane. |
| 260 | * |
| 261 | * Returns: The newly allocated plane state, or NULL on failure. |
| 262 | */ |
| 263 | struct drm_plane_state * |
| 264 | vmw_du_plane_duplicate_state(struct drm_plane *plane) |
| 265 | { |
| 266 | struct drm_plane_state *state; |
| 267 | struct vmw_plane_state *vps; |
| 268 | |
| 269 | vps = kmemdup(plane->state, sizeof(*vps), GFP_KERNEL); |
| 270 | |
| 271 | if (!vps) |
| 272 | return NULL; |
| 273 | |
| 274 | vps->pinned = 0; |
| 275 | vps->cpp = 0; |
| 276 | |
| 277 | vps->cursor.mob = NULL; |
| 278 | |
| 279 | /* Each ref counted resource needs to be acquired again */ |
| 280 | vmw_user_object_ref(uo: &vps->uo); |
| 281 | state = &vps->base; |
| 282 | |
| 283 | __drm_atomic_helper_plane_duplicate_state(plane, state); |
| 284 | |
| 285 | return state; |
| 286 | } |
| 287 | |
| 288 | |
| 289 | /** |
| 290 | * vmw_du_plane_reset - creates a blank vmw plane state |
| 291 | * @plane: drm plane |
| 292 | * |
| 293 | * Resets the atomic state for @plane by freeing the state pointer (which might |
| 294 | * be NULL, e.g. at driver load time) and allocating a new empty state object. |
| 295 | */ |
| 296 | void vmw_du_plane_reset(struct drm_plane *plane) |
| 297 | { |
| 298 | struct vmw_plane_state *vps; |
| 299 | |
| 300 | if (plane->state) |
| 301 | vmw_du_plane_destroy_state(plane, state: plane->state); |
| 302 | |
| 303 | vps = kzalloc(sizeof(*vps), GFP_KERNEL); |
| 304 | |
| 305 | if (!vps) { |
| 306 | DRM_ERROR("Cannot allocate vmw_plane_state\n" ); |
| 307 | return; |
| 308 | } |
| 309 | |
| 310 | __drm_atomic_helper_plane_reset(plane, state: &vps->base); |
| 311 | } |
| 312 | |
| 313 | |
| 314 | /** |
| 315 | * vmw_du_plane_destroy_state - destroy plane state |
| 316 | * @plane: DRM plane |
| 317 | * @state: state object to destroy |
| 318 | * |
| 319 | * Destroys the plane state (both common and vmw-specific) for the |
| 320 | * specified plane. |
| 321 | */ |
| 322 | void |
| 323 | vmw_du_plane_destroy_state(struct drm_plane *plane, |
| 324 | struct drm_plane_state *state) |
| 325 | { |
| 326 | struct vmw_plane_state *vps = vmw_plane_state_to_vps(state); |
| 327 | |
| 328 | /* Should have been freed by cleanup_fb */ |
| 329 | vmw_user_object_unref(uo: &vps->uo); |
| 330 | |
| 331 | drm_atomic_helper_plane_destroy_state(plane, state); |
| 332 | } |
| 333 | |
| 334 | |
| 335 | /** |
| 336 | * vmw_du_connector_duplicate_state - duplicate connector state |
| 337 | * @connector: DRM connector |
| 338 | * |
| 339 | * Allocates and returns a copy of the connector state (both common and |
| 340 | * vmw-specific) for the specified connector. |
| 341 | * |
| 342 | * Returns: The newly allocated connector state, or NULL on failure. |
| 343 | */ |
| 344 | struct drm_connector_state * |
| 345 | vmw_du_connector_duplicate_state(struct drm_connector *connector) |
| 346 | { |
| 347 | struct drm_connector_state *state; |
| 348 | struct vmw_connector_state *vcs; |
| 349 | |
| 350 | if (WARN_ON(!connector->state)) |
| 351 | return NULL; |
| 352 | |
| 353 | vcs = kmemdup(connector->state, sizeof(*vcs), GFP_KERNEL); |
| 354 | |
| 355 | if (!vcs) |
| 356 | return NULL; |
| 357 | |
| 358 | state = &vcs->base; |
| 359 | |
| 360 | __drm_atomic_helper_connector_duplicate_state(connector, state); |
| 361 | |
| 362 | return state; |
| 363 | } |
| 364 | |
| 365 | |
| 366 | /** |
| 367 | * vmw_du_connector_reset - creates a blank vmw connector state |
| 368 | * @connector: DRM connector |
| 369 | * |
| 370 | * Resets the atomic state for @connector by freeing the state pointer (which |
| 371 | * might be NULL, e.g. at driver load time) and allocating a new empty state |
| 372 | * object. |
| 373 | */ |
| 374 | void vmw_du_connector_reset(struct drm_connector *connector) |
| 375 | { |
| 376 | struct vmw_connector_state *vcs; |
| 377 | |
| 378 | |
| 379 | if (connector->state) { |
| 380 | __drm_atomic_helper_connector_destroy_state(state: connector->state); |
| 381 | |
| 382 | kfree(vmw_connector_state_to_vcs(connector->state)); |
| 383 | } |
| 384 | |
| 385 | vcs = kzalloc(sizeof(*vcs), GFP_KERNEL); |
| 386 | |
| 387 | if (!vcs) { |
| 388 | DRM_ERROR("Cannot allocate vmw_connector_state\n" ); |
| 389 | return; |
| 390 | } |
| 391 | |
| 392 | __drm_atomic_helper_connector_reset(connector, conn_state: &vcs->base); |
| 393 | } |
| 394 | |
| 395 | |
| 396 | /** |
| 397 | * vmw_du_connector_destroy_state - destroy connector state |
| 398 | * @connector: DRM connector |
| 399 | * @state: state object to destroy |
| 400 | * |
| 401 | * Destroys the connector state (both common and vmw-specific) for the |
| 402 | * specified plane. |
| 403 | */ |
| 404 | void |
| 405 | vmw_du_connector_destroy_state(struct drm_connector *connector, |
| 406 | struct drm_connector_state *state) |
| 407 | { |
| 408 | drm_atomic_helper_connector_destroy_state(connector, state); |
| 409 | } |
| 410 | /* |
| 411 | * Generic framebuffer code |
| 412 | */ |
| 413 | |
| 414 | /* |
| 415 | * Surface framebuffer code |
| 416 | */ |
| 417 | |
| 418 | static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer) |
| 419 | { |
| 420 | struct vmw_framebuffer_surface *vfbs = |
| 421 | vmw_framebuffer_to_vfbs(framebuffer); |
| 422 | struct vmw_bo *bo = vmw_user_object_buffer(uo: &vfbs->uo); |
| 423 | struct vmw_surface *surf = vmw_user_object_surface(uo: &vfbs->uo); |
| 424 | |
| 425 | if (bo) { |
| 426 | vmw_bo_dirty_release(vbo: bo); |
| 427 | /* |
| 428 | * bo->dirty is reference counted so it being NULL |
| 429 | * means that the surface wasn't coherent to begin |
| 430 | * with and so we have to free the dirty tracker |
| 431 | * in the vmw_resource |
| 432 | */ |
| 433 | if (!bo->dirty && surf && surf->res.dirty) |
| 434 | surf->res.func->dirty_free(&surf->res); |
| 435 | } |
| 436 | drm_framebuffer_cleanup(fb: framebuffer); |
| 437 | vmw_user_object_unref(uo: &vfbs->uo); |
| 438 | |
| 439 | kfree(objp: vfbs); |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * vmw_kms_readback - Perform a readback from the screen system to |
| 444 | * a buffer-object backed framebuffer. |
| 445 | * |
| 446 | * @dev_priv: Pointer to the device private structure. |
| 447 | * @file_priv: Pointer to a struct drm_file identifying the caller. |
| 448 | * Must be set to NULL if @user_fence_rep is NULL. |
| 449 | * @vfb: Pointer to the buffer-object backed framebuffer. |
| 450 | * @user_fence_rep: User-space provided structure for fence information. |
| 451 | * Must be set to non-NULL if @file_priv is non-NULL. |
| 452 | * @vclips: Array of clip rects. |
| 453 | * @num_clips: Number of clip rects in @vclips. |
| 454 | * |
| 455 | * Returns 0 on success, negative error code on failure. -ERESTARTSYS if |
| 456 | * interrupted. |
| 457 | */ |
| 458 | int vmw_kms_readback(struct vmw_private *dev_priv, |
| 459 | struct drm_file *file_priv, |
| 460 | struct vmw_framebuffer *vfb, |
| 461 | struct drm_vmw_fence_rep __user *user_fence_rep, |
| 462 | struct drm_vmw_rect *vclips, |
| 463 | uint32_t num_clips) |
| 464 | { |
| 465 | switch (dev_priv->active_display_unit) { |
| 466 | case vmw_du_screen_object: |
| 467 | return vmw_kms_sou_readback(dev_priv, file_priv, vfb, |
| 468 | user_fence_rep, vclips, num_clips, |
| 469 | NULL); |
| 470 | case vmw_du_screen_target: |
| 471 | return vmw_kms_stdu_readback(dev_priv, file_priv, vfb, |
| 472 | user_fence_rep, NULL, vclips, num_clips, |
| 473 | increment: 1, NULL); |
| 474 | default: |
| 475 | WARN_ONCE(true, |
| 476 | "Readback called with invalid display system.\n" ); |
| 477 | } |
| 478 | |
| 479 | return -ENOSYS; |
| 480 | } |
| 481 | |
| 482 | static int vmw_framebuffer_surface_create_handle(struct drm_framebuffer *fb, |
| 483 | struct drm_file *file_priv, |
| 484 | unsigned int *handle) |
| 485 | { |
| 486 | struct vmw_framebuffer_surface *vfbs = vmw_framebuffer_to_vfbs(fb); |
| 487 | struct vmw_bo *bo = vmw_user_object_buffer(uo: &vfbs->uo); |
| 488 | |
| 489 | if (WARN_ON(!bo)) |
| 490 | return -EINVAL; |
| 491 | return drm_gem_handle_create(file_priv, obj: &bo->tbo.base, handlep: handle); |
| 492 | } |
| 493 | |
| 494 | static const struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = { |
| 495 | .create_handle = vmw_framebuffer_surface_create_handle, |
| 496 | .destroy = vmw_framebuffer_surface_destroy, |
| 497 | .dirty = drm_atomic_helper_dirtyfb, |
| 498 | }; |
| 499 | |
| 500 | static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv, |
| 501 | struct vmw_user_object *uo, |
| 502 | struct vmw_framebuffer **out, |
| 503 | const struct drm_format_info *info, |
| 504 | const struct drm_mode_fb_cmd2 |
| 505 | *mode_cmd) |
| 506 | |
| 507 | { |
| 508 | struct drm_device *dev = &dev_priv->drm; |
| 509 | struct vmw_framebuffer_surface *vfbs; |
| 510 | struct vmw_surface *surface; |
| 511 | int ret; |
| 512 | |
| 513 | /* 3D is only supported on HWv8 and newer hosts */ |
| 514 | if (dev_priv->active_display_unit == vmw_du_legacy) |
| 515 | return -ENOSYS; |
| 516 | |
| 517 | surface = vmw_user_object_surface(uo); |
| 518 | |
| 519 | /* |
| 520 | * Sanity checks. |
| 521 | */ |
| 522 | |
| 523 | if (!drm_any_plane_has_format(dev: &dev_priv->drm, |
| 524 | format: mode_cmd->pixel_format, |
| 525 | modifier: mode_cmd->modifier[0])) { |
| 526 | drm_dbg(&dev_priv->drm, |
| 527 | "unsupported pixel format %p4cc / modifier 0x%llx\n" , |
| 528 | &mode_cmd->pixel_format, mode_cmd->modifier[0]); |
| 529 | return -EINVAL; |
| 530 | } |
| 531 | |
| 532 | /* Surface must be marked as a scanout. */ |
| 533 | if (unlikely(!surface->metadata.scanout)) |
| 534 | return -EINVAL; |
| 535 | |
| 536 | if (unlikely(surface->metadata.mip_levels[0] != 1 || |
| 537 | surface->metadata.num_sizes != 1 || |
| 538 | surface->metadata.base_size.width < mode_cmd->width || |
| 539 | surface->metadata.base_size.height < mode_cmd->height || |
| 540 | surface->metadata.base_size.depth != 1)) { |
| 541 | DRM_ERROR("Incompatible surface dimensions " |
| 542 | "for requested mode.\n" ); |
| 543 | return -EINVAL; |
| 544 | } |
| 545 | |
| 546 | vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL); |
| 547 | if (!vfbs) { |
| 548 | ret = -ENOMEM; |
| 549 | goto out_err1; |
| 550 | } |
| 551 | |
| 552 | drm_helper_mode_fill_fb_struct(dev, fb: &vfbs->base.base, info, mode_cmd); |
| 553 | memcpy(&vfbs->uo, uo, sizeof(vfbs->uo)); |
| 554 | vmw_user_object_ref(uo: &vfbs->uo); |
| 555 | |
| 556 | if (vfbs->uo.buffer) |
| 557 | vfbs->base.base.obj[0] = &vfbs->uo.buffer->tbo.base; |
| 558 | |
| 559 | *out = &vfbs->base; |
| 560 | |
| 561 | ret = drm_framebuffer_init(dev, fb: &vfbs->base.base, |
| 562 | funcs: &vmw_framebuffer_surface_funcs); |
| 563 | if (ret) |
| 564 | goto out_err2; |
| 565 | |
| 566 | return 0; |
| 567 | |
| 568 | out_err2: |
| 569 | vmw_user_object_unref(uo: &vfbs->uo); |
| 570 | kfree(objp: vfbs); |
| 571 | out_err1: |
| 572 | return ret; |
| 573 | } |
| 574 | |
| 575 | /* |
| 576 | * Buffer-object framebuffer code |
| 577 | */ |
| 578 | |
| 579 | static int vmw_framebuffer_bo_create_handle(struct drm_framebuffer *fb, |
| 580 | struct drm_file *file_priv, |
| 581 | unsigned int *handle) |
| 582 | { |
| 583 | struct vmw_framebuffer_bo *vfbd = |
| 584 | vmw_framebuffer_to_vfbd(fb); |
| 585 | return drm_gem_handle_create(file_priv, obj: &vfbd->buffer->tbo.base, handlep: handle); |
| 586 | } |
| 587 | |
| 588 | static void vmw_framebuffer_bo_destroy(struct drm_framebuffer *framebuffer) |
| 589 | { |
| 590 | struct vmw_framebuffer_bo *vfbd = |
| 591 | vmw_framebuffer_to_vfbd(framebuffer); |
| 592 | |
| 593 | vmw_bo_dirty_release(vbo: vfbd->buffer); |
| 594 | drm_framebuffer_cleanup(fb: framebuffer); |
| 595 | vmw_bo_unreference(buf: &vfbd->buffer); |
| 596 | |
| 597 | kfree(objp: vfbd); |
| 598 | } |
| 599 | |
| 600 | static const struct drm_framebuffer_funcs vmw_framebuffer_bo_funcs = { |
| 601 | .create_handle = vmw_framebuffer_bo_create_handle, |
| 602 | .destroy = vmw_framebuffer_bo_destroy, |
| 603 | .dirty = drm_atomic_helper_dirtyfb, |
| 604 | }; |
| 605 | |
| 606 | static int vmw_kms_new_framebuffer_bo(struct vmw_private *dev_priv, |
| 607 | struct vmw_bo *bo, |
| 608 | struct vmw_framebuffer **out, |
| 609 | const struct drm_format_info *info, |
| 610 | const struct drm_mode_fb_cmd2 |
| 611 | *mode_cmd) |
| 612 | |
| 613 | { |
| 614 | struct drm_device *dev = &dev_priv->drm; |
| 615 | struct vmw_framebuffer_bo *vfbd; |
| 616 | unsigned int requested_size; |
| 617 | int ret; |
| 618 | |
| 619 | requested_size = mode_cmd->height * mode_cmd->pitches[0]; |
| 620 | if (unlikely(requested_size > bo->tbo.base.size)) { |
| 621 | DRM_ERROR("Screen buffer object size is too small " |
| 622 | "for requested mode.\n" ); |
| 623 | return -EINVAL; |
| 624 | } |
| 625 | |
| 626 | if (!drm_any_plane_has_format(dev: &dev_priv->drm, |
| 627 | format: mode_cmd->pixel_format, |
| 628 | modifier: mode_cmd->modifier[0])) { |
| 629 | drm_dbg(&dev_priv->drm, |
| 630 | "unsupported pixel format %p4cc / modifier 0x%llx\n" , |
| 631 | &mode_cmd->pixel_format, mode_cmd->modifier[0]); |
| 632 | return -EINVAL; |
| 633 | } |
| 634 | |
| 635 | vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL); |
| 636 | if (!vfbd) { |
| 637 | ret = -ENOMEM; |
| 638 | goto out_err1; |
| 639 | } |
| 640 | |
| 641 | vfbd->base.base.obj[0] = &bo->tbo.base; |
| 642 | drm_helper_mode_fill_fb_struct(dev, fb: &vfbd->base.base, info, mode_cmd); |
| 643 | vfbd->base.bo = true; |
| 644 | vfbd->buffer = vmw_bo_reference(buf: bo); |
| 645 | *out = &vfbd->base; |
| 646 | |
| 647 | ret = drm_framebuffer_init(dev, fb: &vfbd->base.base, |
| 648 | funcs: &vmw_framebuffer_bo_funcs); |
| 649 | if (ret) |
| 650 | goto out_err2; |
| 651 | |
| 652 | return 0; |
| 653 | |
| 654 | out_err2: |
| 655 | vmw_bo_unreference(buf: &bo); |
| 656 | kfree(objp: vfbd); |
| 657 | out_err1: |
| 658 | return ret; |
| 659 | } |
| 660 | |
| 661 | |
| 662 | /** |
| 663 | * vmw_kms_srf_ok - check if a surface can be created |
| 664 | * |
| 665 | * @dev_priv: Pointer to device private struct. |
| 666 | * @width: requested width |
| 667 | * @height: requested height |
| 668 | * |
| 669 | * Surfaces need to be less than texture size |
| 670 | */ |
| 671 | static bool |
| 672 | vmw_kms_srf_ok(struct vmw_private *dev_priv, uint32_t width, uint32_t height) |
| 673 | { |
| 674 | if (width > dev_priv->texture_max_width || |
| 675 | height > dev_priv->texture_max_height) |
| 676 | return false; |
| 677 | |
| 678 | return true; |
| 679 | } |
| 680 | |
| 681 | /** |
| 682 | * vmw_kms_new_framebuffer - Create a new framebuffer. |
| 683 | * |
| 684 | * @dev_priv: Pointer to device private struct. |
| 685 | * @uo: Pointer to user object to wrap the kms framebuffer around. |
| 686 | * Either the buffer or surface inside the user object must be NULL. |
| 687 | * @info: pixel format information. |
| 688 | * @mode_cmd: Frame-buffer metadata. |
| 689 | */ |
| 690 | struct vmw_framebuffer * |
| 691 | vmw_kms_new_framebuffer(struct vmw_private *dev_priv, |
| 692 | struct vmw_user_object *uo, |
| 693 | const struct drm_format_info *info, |
| 694 | const struct drm_mode_fb_cmd2 *mode_cmd) |
| 695 | { |
| 696 | struct vmw_framebuffer *vfb = NULL; |
| 697 | int ret; |
| 698 | |
| 699 | /* Create the new framebuffer depending one what we have */ |
| 700 | if (vmw_user_object_surface(uo)) { |
| 701 | ret = vmw_kms_new_framebuffer_surface(dev_priv, uo, out: &vfb, |
| 702 | info, mode_cmd); |
| 703 | } else if (uo->buffer) { |
| 704 | ret = vmw_kms_new_framebuffer_bo(dev_priv, bo: uo->buffer, out: &vfb, |
| 705 | info, mode_cmd); |
| 706 | } else { |
| 707 | BUG(); |
| 708 | } |
| 709 | |
| 710 | if (ret) |
| 711 | return ERR_PTR(error: ret); |
| 712 | |
| 713 | return vfb; |
| 714 | } |
| 715 | |
| 716 | /* |
| 717 | * Generic Kernel modesetting functions |
| 718 | */ |
| 719 | |
| 720 | static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev, |
| 721 | struct drm_file *file_priv, |
| 722 | const struct drm_format_info *info, |
| 723 | const struct drm_mode_fb_cmd2 *mode_cmd) |
| 724 | { |
| 725 | struct vmw_private *dev_priv = vmw_priv(dev); |
| 726 | struct vmw_framebuffer *vfb = NULL; |
| 727 | struct vmw_user_object uo = {0}; |
| 728 | struct vmw_bo *bo; |
| 729 | struct vmw_surface *surface; |
| 730 | int ret; |
| 731 | |
| 732 | /* returns either a bo or surface */ |
| 733 | ret = vmw_user_object_lookup(dev_priv, filp: file_priv, handle: mode_cmd->handles[0], |
| 734 | uo: &uo); |
| 735 | if (ret) { |
| 736 | DRM_ERROR("Invalid buffer object handle %u (0x%x).\n" , |
| 737 | mode_cmd->handles[0], mode_cmd->handles[0]); |
| 738 | goto err_out; |
| 739 | } |
| 740 | |
| 741 | |
| 742 | if (vmw_user_object_surface(uo: &uo) && |
| 743 | !vmw_kms_srf_ok(dev_priv, width: mode_cmd->width, height: mode_cmd->height)) { |
| 744 | DRM_ERROR("Surface size cannot exceed %dx%d\n" , |
| 745 | dev_priv->texture_max_width, |
| 746 | dev_priv->texture_max_height); |
| 747 | ret = -EINVAL; |
| 748 | goto err_out; |
| 749 | } |
| 750 | |
| 751 | |
| 752 | vfb = vmw_kms_new_framebuffer(dev_priv, uo: &uo, info, mode_cmd); |
| 753 | if (IS_ERR(ptr: vfb)) { |
| 754 | ret = PTR_ERR(ptr: vfb); |
| 755 | goto err_out; |
| 756 | } |
| 757 | |
| 758 | err_out: |
| 759 | bo = vmw_user_object_buffer(uo: &uo); |
| 760 | surface = vmw_user_object_surface(uo: &uo); |
| 761 | /* vmw_user_object_lookup takes one ref so does new_fb */ |
| 762 | vmw_user_object_unref(uo: &uo); |
| 763 | |
| 764 | if (ret) { |
| 765 | DRM_ERROR("failed to create vmw_framebuffer: %i\n" , ret); |
| 766 | return ERR_PTR(error: ret); |
| 767 | } |
| 768 | |
| 769 | if (bo) { |
| 770 | ttm_bo_reserve(bo: &bo->tbo, interruptible: false, no_wait: false, NULL); |
| 771 | ret = vmw_bo_dirty_add(vbo: bo); |
| 772 | if (!ret && surface && surface->res.func->dirty_alloc) { |
| 773 | surface->res.coherent = true; |
| 774 | ret = surface->res.func->dirty_alloc(&surface->res); |
| 775 | } |
| 776 | ttm_bo_unreserve(bo: &bo->tbo); |
| 777 | } |
| 778 | |
| 779 | return &vfb->base; |
| 780 | } |
| 781 | |
| 782 | /** |
| 783 | * vmw_kms_check_display_memory - Validates display memory required for a |
| 784 | * topology |
| 785 | * @dev: DRM device |
| 786 | * @num_rects: number of drm_rect in rects |
| 787 | * @rects: array of drm_rect representing the topology to validate indexed by |
| 788 | * crtc index. |
| 789 | * |
| 790 | * Returns: |
| 791 | * 0 on success otherwise negative error code |
| 792 | */ |
| 793 | static int vmw_kms_check_display_memory(struct drm_device *dev, |
| 794 | uint32_t num_rects, |
| 795 | struct drm_rect *rects) |
| 796 | { |
| 797 | struct vmw_private *dev_priv = vmw_priv(dev); |
| 798 | struct drm_rect bounding_box = {0}; |
| 799 | u64 total_pixels = 0, pixel_mem, bb_mem; |
| 800 | int i; |
| 801 | |
| 802 | for (i = 0; i < num_rects; i++) { |
| 803 | /* |
| 804 | * For STDU only individual screen (screen target) is limited by |
| 805 | * SCREENTARGET_MAX_WIDTH/HEIGHT registers. |
| 806 | */ |
| 807 | if (dev_priv->active_display_unit == vmw_du_screen_target && |
| 808 | (drm_rect_width(r: &rects[i]) > dev_priv->stdu_max_width || |
| 809 | drm_rect_height(r: &rects[i]) > dev_priv->stdu_max_height)) { |
| 810 | VMW_DEBUG_KMS("Screen size not supported.\n" ); |
| 811 | return -EINVAL; |
| 812 | } |
| 813 | |
| 814 | /* Bounding box upper left is at (0,0). */ |
| 815 | if (rects[i].x2 > bounding_box.x2) |
| 816 | bounding_box.x2 = rects[i].x2; |
| 817 | |
| 818 | if (rects[i].y2 > bounding_box.y2) |
| 819 | bounding_box.y2 = rects[i].y2; |
| 820 | |
| 821 | total_pixels += (u64) drm_rect_width(r: &rects[i]) * |
| 822 | (u64) drm_rect_height(r: &rects[i]); |
| 823 | } |
| 824 | |
| 825 | /* Virtual svga device primary limits are always in 32-bpp. */ |
| 826 | pixel_mem = total_pixels * 4; |
| 827 | |
| 828 | /* |
| 829 | * For HV10 and below prim_bb_mem is vram size. When |
| 830 | * SVGA_REG_MAX_PRIMARY_BOUNDING_BOX_MEM is not present vram size is |
| 831 | * limit on primary bounding box |
| 832 | */ |
| 833 | if (pixel_mem > dev_priv->max_primary_mem) { |
| 834 | VMW_DEBUG_KMS("Combined output size too large.\n" ); |
| 835 | return -EINVAL; |
| 836 | } |
| 837 | |
| 838 | /* SVGA_CAP_NO_BB_RESTRICTION is available for STDU only. */ |
| 839 | if (dev_priv->active_display_unit != vmw_du_screen_target || |
| 840 | !(dev_priv->capabilities & SVGA_CAP_NO_BB_RESTRICTION)) { |
| 841 | bb_mem = (u64) bounding_box.x2 * bounding_box.y2 * 4; |
| 842 | |
| 843 | if (bb_mem > dev_priv->max_primary_mem) { |
| 844 | VMW_DEBUG_KMS("Topology is beyond supported limits.\n" ); |
| 845 | return -EINVAL; |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | return 0; |
| 850 | } |
| 851 | |
| 852 | /** |
| 853 | * vmw_crtc_state_and_lock - Return new or current crtc state with locked |
| 854 | * crtc mutex |
| 855 | * @state: The atomic state pointer containing the new atomic state |
| 856 | * @crtc: The crtc |
| 857 | * |
| 858 | * This function returns the new crtc state if it's part of the state update. |
| 859 | * Otherwise returns the current crtc state. It also makes sure that the |
| 860 | * crtc mutex is locked. |
| 861 | * |
| 862 | * Returns: A valid crtc state pointer or NULL. It may also return a |
| 863 | * pointer error, in particular -EDEADLK if locking needs to be rerun. |
| 864 | */ |
| 865 | static struct drm_crtc_state * |
| 866 | vmw_crtc_state_and_lock(struct drm_atomic_state *state, struct drm_crtc *crtc) |
| 867 | { |
| 868 | struct drm_crtc_state *crtc_state; |
| 869 | |
| 870 | crtc_state = drm_atomic_get_new_crtc_state(state, crtc); |
| 871 | if (crtc_state) { |
| 872 | lockdep_assert_held(&crtc->mutex.mutex.base); |
| 873 | } else { |
| 874 | int ret = drm_modeset_lock(lock: &crtc->mutex, ctx: state->acquire_ctx); |
| 875 | |
| 876 | if (ret != 0 && ret != -EALREADY) |
| 877 | return ERR_PTR(error: ret); |
| 878 | |
| 879 | crtc_state = crtc->state; |
| 880 | } |
| 881 | |
| 882 | return crtc_state; |
| 883 | } |
| 884 | |
| 885 | /** |
| 886 | * vmw_kms_check_implicit - Verify that all implicit display units scan out |
| 887 | * from the same fb after the new state is committed. |
| 888 | * @dev: The drm_device. |
| 889 | * @state: The new state to be checked. |
| 890 | * |
| 891 | * Returns: |
| 892 | * Zero on success, |
| 893 | * -EINVAL on invalid state, |
| 894 | * -EDEADLK if modeset locking needs to be rerun. |
| 895 | */ |
| 896 | static int vmw_kms_check_implicit(struct drm_device *dev, |
| 897 | struct drm_atomic_state *state) |
| 898 | { |
| 899 | struct drm_framebuffer *implicit_fb = NULL; |
| 900 | struct drm_crtc *crtc; |
| 901 | struct drm_crtc_state *crtc_state; |
| 902 | struct drm_plane_state *plane_state; |
| 903 | |
| 904 | drm_for_each_crtc(crtc, dev) { |
| 905 | struct vmw_display_unit *du = vmw_crtc_to_du(crtc); |
| 906 | |
| 907 | if (!du->is_implicit) |
| 908 | continue; |
| 909 | |
| 910 | crtc_state = vmw_crtc_state_and_lock(state, crtc); |
| 911 | if (IS_ERR(ptr: crtc_state)) |
| 912 | return PTR_ERR(ptr: crtc_state); |
| 913 | |
| 914 | if (!crtc_state || !crtc_state->enable) |
| 915 | continue; |
| 916 | |
| 917 | /* |
| 918 | * Can't move primary planes across crtcs, so this is OK. |
| 919 | * It also means we don't need to take the plane mutex. |
| 920 | */ |
| 921 | plane_state = du->primary.state; |
| 922 | if (plane_state->crtc != crtc) |
| 923 | continue; |
| 924 | |
| 925 | if (!implicit_fb) |
| 926 | implicit_fb = plane_state->fb; |
| 927 | else if (implicit_fb != plane_state->fb) |
| 928 | return -EINVAL; |
| 929 | } |
| 930 | |
| 931 | return 0; |
| 932 | } |
| 933 | |
| 934 | /** |
| 935 | * vmw_kms_check_topology - Validates topology in drm_atomic_state |
| 936 | * @dev: DRM device |
| 937 | * @state: the driver state object |
| 938 | * |
| 939 | * Returns: |
| 940 | * 0 on success otherwise negative error code |
| 941 | */ |
| 942 | static int vmw_kms_check_topology(struct drm_device *dev, |
| 943 | struct drm_atomic_state *state) |
| 944 | { |
| 945 | struct drm_crtc_state *old_crtc_state, *new_crtc_state; |
| 946 | struct drm_rect *rects; |
| 947 | struct drm_crtc *crtc; |
| 948 | uint32_t i; |
| 949 | int ret = 0; |
| 950 | |
| 951 | rects = kcalloc(dev->mode_config.num_crtc, sizeof(struct drm_rect), |
| 952 | GFP_KERNEL); |
| 953 | if (!rects) |
| 954 | return -ENOMEM; |
| 955 | |
| 956 | drm_for_each_crtc(crtc, dev) { |
| 957 | struct vmw_display_unit *du = vmw_crtc_to_du(crtc); |
| 958 | struct drm_crtc_state *crtc_state; |
| 959 | |
| 960 | i = drm_crtc_index(crtc); |
| 961 | |
| 962 | crtc_state = vmw_crtc_state_and_lock(state, crtc); |
| 963 | if (IS_ERR(ptr: crtc_state)) { |
| 964 | ret = PTR_ERR(ptr: crtc_state); |
| 965 | goto clean; |
| 966 | } |
| 967 | |
| 968 | if (!crtc_state) |
| 969 | continue; |
| 970 | |
| 971 | if (crtc_state->enable) { |
| 972 | rects[i].x1 = du->gui_x; |
| 973 | rects[i].y1 = du->gui_y; |
| 974 | rects[i].x2 = du->gui_x + crtc_state->mode.hdisplay; |
| 975 | rects[i].y2 = du->gui_y + crtc_state->mode.vdisplay; |
| 976 | } else { |
| 977 | rects[i].x1 = 0; |
| 978 | rects[i].y1 = 0; |
| 979 | rects[i].x2 = 0; |
| 980 | rects[i].y2 = 0; |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | /* Determine change to topology due to new atomic state */ |
| 985 | for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, |
| 986 | new_crtc_state, i) { |
| 987 | struct vmw_display_unit *du = vmw_crtc_to_du(crtc); |
| 988 | struct drm_connector *connector; |
| 989 | struct drm_connector_state *conn_state; |
| 990 | struct vmw_connector_state *vmw_conn_state; |
| 991 | |
| 992 | if (!du->pref_active && new_crtc_state->enable) { |
| 993 | VMW_DEBUG_KMS("Enabling a disabled display unit\n" ); |
| 994 | ret = -EINVAL; |
| 995 | goto clean; |
| 996 | } |
| 997 | |
| 998 | /* |
| 999 | * For vmwgfx each crtc has only one connector attached and it |
| 1000 | * is not changed so don't really need to check the |
| 1001 | * crtc->connector_mask and iterate over it. |
| 1002 | */ |
| 1003 | connector = &du->connector; |
| 1004 | conn_state = drm_atomic_get_connector_state(state, connector); |
| 1005 | if (IS_ERR(ptr: conn_state)) { |
| 1006 | ret = PTR_ERR(ptr: conn_state); |
| 1007 | goto clean; |
| 1008 | } |
| 1009 | |
| 1010 | vmw_conn_state = vmw_connector_state_to_vcs(conn_state); |
| 1011 | vmw_conn_state->gui_x = du->gui_x; |
| 1012 | vmw_conn_state->gui_y = du->gui_y; |
| 1013 | } |
| 1014 | |
| 1015 | ret = vmw_kms_check_display_memory(dev, num_rects: dev->mode_config.num_crtc, |
| 1016 | rects); |
| 1017 | |
| 1018 | clean: |
| 1019 | kfree(objp: rects); |
| 1020 | return ret; |
| 1021 | } |
| 1022 | |
| 1023 | /** |
| 1024 | * vmw_kms_atomic_check_modeset- validate state object for modeset changes |
| 1025 | * |
| 1026 | * @dev: DRM device |
| 1027 | * @state: the driver state object |
| 1028 | * |
| 1029 | * This is a simple wrapper around drm_atomic_helper_check_modeset() for |
| 1030 | * us to assign a value to mode->crtc_clock so that |
| 1031 | * drm_calc_timestamping_constants() won't throw an error message |
| 1032 | * |
| 1033 | * Returns: |
| 1034 | * Zero for success or -errno |
| 1035 | */ |
| 1036 | static int |
| 1037 | vmw_kms_atomic_check_modeset(struct drm_device *dev, |
| 1038 | struct drm_atomic_state *state) |
| 1039 | { |
| 1040 | struct drm_crtc *crtc; |
| 1041 | struct drm_crtc_state *crtc_state; |
| 1042 | bool need_modeset = false; |
| 1043 | int i, ret; |
| 1044 | |
| 1045 | ret = drm_atomic_helper_check(dev, state); |
| 1046 | if (ret) |
| 1047 | return ret; |
| 1048 | |
| 1049 | ret = vmw_kms_check_implicit(dev, state); |
| 1050 | if (ret) { |
| 1051 | VMW_DEBUG_KMS("Invalid implicit state\n" ); |
| 1052 | return ret; |
| 1053 | } |
| 1054 | |
| 1055 | for_each_new_crtc_in_state(state, crtc, crtc_state, i) { |
| 1056 | if (drm_atomic_crtc_needs_modeset(state: crtc_state)) |
| 1057 | need_modeset = true; |
| 1058 | } |
| 1059 | |
| 1060 | if (need_modeset) |
| 1061 | return vmw_kms_check_topology(dev, state); |
| 1062 | |
| 1063 | return ret; |
| 1064 | } |
| 1065 | |
| 1066 | static const struct drm_mode_config_funcs vmw_kms_funcs = { |
| 1067 | .fb_create = vmw_kms_fb_create, |
| 1068 | .atomic_check = vmw_kms_atomic_check_modeset, |
| 1069 | .atomic_commit = drm_atomic_helper_commit, |
| 1070 | }; |
| 1071 | |
| 1072 | static int vmw_kms_generic_present(struct vmw_private *dev_priv, |
| 1073 | struct drm_file *file_priv, |
| 1074 | struct vmw_framebuffer *vfb, |
| 1075 | struct vmw_surface *surface, |
| 1076 | uint32_t sid, |
| 1077 | int32_t destX, int32_t destY, |
| 1078 | struct drm_vmw_rect *clips, |
| 1079 | uint32_t num_clips) |
| 1080 | { |
| 1081 | return vmw_kms_sou_do_surface_dirty(dev_priv, framebuffer: vfb, NULL, vclips: clips, |
| 1082 | srf: &surface->res, dest_x: destX, dest_y: destY, |
| 1083 | num_clips, inc: 1, NULL, NULL); |
| 1084 | } |
| 1085 | |
| 1086 | |
| 1087 | int vmw_kms_present(struct vmw_private *dev_priv, |
| 1088 | struct drm_file *file_priv, |
| 1089 | struct vmw_framebuffer *vfb, |
| 1090 | struct vmw_surface *surface, |
| 1091 | uint32_t sid, |
| 1092 | int32_t destX, int32_t destY, |
| 1093 | struct drm_vmw_rect *clips, |
| 1094 | uint32_t num_clips) |
| 1095 | { |
| 1096 | int ret; |
| 1097 | |
| 1098 | switch (dev_priv->active_display_unit) { |
| 1099 | case vmw_du_screen_target: |
| 1100 | ret = vmw_kms_stdu_surface_dirty(dev_priv, framebuffer: vfb, NULL, vclips: clips, |
| 1101 | srf: &surface->res, dest_x: destX, dest_y: destY, |
| 1102 | num_clips, inc: 1, NULL, NULL); |
| 1103 | break; |
| 1104 | case vmw_du_screen_object: |
| 1105 | ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface, |
| 1106 | sid, destX, destY, clips, |
| 1107 | num_clips); |
| 1108 | break; |
| 1109 | default: |
| 1110 | WARN_ONCE(true, |
| 1111 | "Present called with invalid display system.\n" ); |
| 1112 | ret = -ENOSYS; |
| 1113 | break; |
| 1114 | } |
| 1115 | if (ret) |
| 1116 | return ret; |
| 1117 | |
| 1118 | vmw_cmd_flush(dev_priv, interruptible: false); |
| 1119 | |
| 1120 | return 0; |
| 1121 | } |
| 1122 | |
| 1123 | static void |
| 1124 | vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv) |
| 1125 | { |
| 1126 | if (dev_priv->hotplug_mode_update_property) |
| 1127 | return; |
| 1128 | |
| 1129 | dev_priv->hotplug_mode_update_property = |
| 1130 | drm_property_create_range(dev: &dev_priv->drm, |
| 1131 | DRM_MODE_PROP_IMMUTABLE, |
| 1132 | name: "hotplug_mode_update" , min: 0, max: 1); |
| 1133 | } |
| 1134 | |
| 1135 | static void |
| 1136 | vmw_atomic_commit_tail(struct drm_atomic_state *old_state) |
| 1137 | { |
| 1138 | struct vmw_private *vmw = vmw_priv(dev: old_state->dev); |
| 1139 | struct drm_crtc *crtc; |
| 1140 | struct drm_crtc_state *old_crtc_state; |
| 1141 | int i; |
| 1142 | |
| 1143 | drm_atomic_helper_commit_tail(state: old_state); |
| 1144 | |
| 1145 | if (vmw->vkms_enabled) { |
| 1146 | for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) { |
| 1147 | struct vmw_display_unit *du = vmw_crtc_to_du(crtc); |
| 1148 | (void)old_crtc_state; |
| 1149 | flush_work(work: &du->vkms.crc_generator_work); |
| 1150 | } |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | static const struct drm_mode_config_helper_funcs vmw_mode_config_helpers = { |
| 1155 | .atomic_commit_tail = vmw_atomic_commit_tail, |
| 1156 | }; |
| 1157 | |
| 1158 | int vmw_kms_init(struct vmw_private *dev_priv) |
| 1159 | { |
| 1160 | struct drm_device *dev = &dev_priv->drm; |
| 1161 | int ret; |
| 1162 | static const char *display_unit_names[] = { |
| 1163 | "Invalid" , |
| 1164 | "Legacy" , |
| 1165 | "Screen Object" , |
| 1166 | "Screen Target" , |
| 1167 | "Invalid (max)" |
| 1168 | }; |
| 1169 | |
| 1170 | drm_mode_config_init(dev); |
| 1171 | dev->mode_config.funcs = &vmw_kms_funcs; |
| 1172 | dev->mode_config.min_width = 1; |
| 1173 | dev->mode_config.min_height = 1; |
| 1174 | dev->mode_config.max_width = dev_priv->texture_max_width; |
| 1175 | dev->mode_config.max_height = dev_priv->texture_max_height; |
| 1176 | dev->mode_config.preferred_depth = dev_priv->assume_16bpp ? 16 : 32; |
| 1177 | dev->mode_config.helper_private = &vmw_mode_config_helpers; |
| 1178 | |
| 1179 | drm_mode_create_suggested_offset_properties(dev); |
| 1180 | vmw_kms_create_hotplug_mode_update_property(dev_priv); |
| 1181 | |
| 1182 | ret = vmw_kms_stdu_init_display(dev_priv); |
| 1183 | if (ret) { |
| 1184 | ret = vmw_kms_sou_init_display(dev_priv); |
| 1185 | if (ret) /* Fallback */ |
| 1186 | ret = vmw_kms_ldu_init_display(dev_priv); |
| 1187 | } |
| 1188 | BUILD_BUG_ON(ARRAY_SIZE(display_unit_names) != (vmw_du_max + 1)); |
| 1189 | drm_info(&dev_priv->drm, "%s display unit initialized\n" , |
| 1190 | display_unit_names[dev_priv->active_display_unit]); |
| 1191 | |
| 1192 | return ret; |
| 1193 | } |
| 1194 | |
| 1195 | int vmw_kms_close(struct vmw_private *dev_priv) |
| 1196 | { |
| 1197 | int ret = 0; |
| 1198 | |
| 1199 | /* |
| 1200 | * Docs says we should take the lock before calling this function |
| 1201 | * but since it destroys encoders and our destructor calls |
| 1202 | * drm_encoder_cleanup which takes the lock we deadlock. |
| 1203 | */ |
| 1204 | drm_mode_config_cleanup(dev: &dev_priv->drm); |
| 1205 | if (dev_priv->active_display_unit == vmw_du_legacy) |
| 1206 | ret = vmw_kms_ldu_close_display(dev_priv); |
| 1207 | |
| 1208 | return ret; |
| 1209 | } |
| 1210 | |
| 1211 | int vmw_kms_write_svga(struct vmw_private *vmw_priv, |
| 1212 | unsigned width, unsigned height, unsigned pitch, |
| 1213 | unsigned bpp, unsigned depth) |
| 1214 | { |
| 1215 | if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK) |
| 1216 | vmw_write(dev_priv: vmw_priv, offset: SVGA_REG_PITCHLOCK, value: pitch); |
| 1217 | else if (vmw_fifo_have_pitchlock(dev_priv: vmw_priv)) |
| 1218 | vmw_fifo_mem_write(vmw: vmw_priv, fifo_reg: SVGA_FIFO_PITCHLOCK, value: pitch); |
| 1219 | vmw_write(dev_priv: vmw_priv, offset: SVGA_REG_WIDTH, value: width); |
| 1220 | vmw_write(dev_priv: vmw_priv, offset: SVGA_REG_HEIGHT, value: height); |
| 1221 | if ((vmw_priv->capabilities & SVGA_CAP_8BIT_EMULATION) != 0) |
| 1222 | vmw_write(dev_priv: vmw_priv, offset: SVGA_REG_BITS_PER_PIXEL, value: bpp); |
| 1223 | |
| 1224 | if (vmw_read(dev_priv: vmw_priv, offset: SVGA_REG_DEPTH) != depth) { |
| 1225 | DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n" , |
| 1226 | depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH)); |
| 1227 | return -EINVAL; |
| 1228 | } |
| 1229 | |
| 1230 | return 0; |
| 1231 | } |
| 1232 | |
| 1233 | static |
| 1234 | bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv, |
| 1235 | u64 pitch, |
| 1236 | u64 height) |
| 1237 | { |
| 1238 | return (pitch * height) < (u64)dev_priv->vram_size; |
| 1239 | } |
| 1240 | |
| 1241 | /** |
| 1242 | * vmw_du_update_layout - Update the display unit with topology from resolution |
| 1243 | * plugin and generate DRM uevent |
| 1244 | * @dev_priv: device private |
| 1245 | * @num_rects: number of drm_rect in rects |
| 1246 | * @rects: toplogy to update |
| 1247 | */ |
| 1248 | static int vmw_du_update_layout(struct vmw_private *dev_priv, |
| 1249 | unsigned int num_rects, struct drm_rect *rects) |
| 1250 | { |
| 1251 | struct drm_device *dev = &dev_priv->drm; |
| 1252 | struct vmw_display_unit *du; |
| 1253 | struct drm_connector *con; |
| 1254 | struct drm_connector_list_iter conn_iter; |
| 1255 | struct drm_modeset_acquire_ctx ctx; |
| 1256 | struct drm_crtc *crtc; |
| 1257 | int ret; |
| 1258 | |
| 1259 | /* Currently gui_x/y is protected with the crtc mutex */ |
| 1260 | mutex_lock(&dev->mode_config.mutex); |
| 1261 | drm_modeset_acquire_init(ctx: &ctx, flags: 0); |
| 1262 | retry: |
| 1263 | drm_for_each_crtc(crtc, dev) { |
| 1264 | ret = drm_modeset_lock(lock: &crtc->mutex, ctx: &ctx); |
| 1265 | if (ret < 0) { |
| 1266 | if (ret == -EDEADLK) { |
| 1267 | drm_modeset_backoff(ctx: &ctx); |
| 1268 | goto retry; |
| 1269 | } |
| 1270 | goto out_fini; |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | drm_connector_list_iter_begin(dev, iter: &conn_iter); |
| 1275 | drm_for_each_connector_iter(con, &conn_iter) { |
| 1276 | du = vmw_connector_to_du(con); |
| 1277 | if (num_rects > du->unit) { |
| 1278 | du->pref_width = drm_rect_width(r: &rects[du->unit]); |
| 1279 | du->pref_height = drm_rect_height(r: &rects[du->unit]); |
| 1280 | du->pref_active = true; |
| 1281 | du->gui_x = rects[du->unit].x1; |
| 1282 | du->gui_y = rects[du->unit].y1; |
| 1283 | } else { |
| 1284 | du->pref_width = VMWGFX_MIN_INITIAL_WIDTH; |
| 1285 | du->pref_height = VMWGFX_MIN_INITIAL_HEIGHT; |
| 1286 | du->pref_active = false; |
| 1287 | du->gui_x = 0; |
| 1288 | du->gui_y = 0; |
| 1289 | } |
| 1290 | } |
| 1291 | drm_connector_list_iter_end(iter: &conn_iter); |
| 1292 | |
| 1293 | list_for_each_entry(con, &dev->mode_config.connector_list, head) { |
| 1294 | du = vmw_connector_to_du(con); |
| 1295 | if (num_rects > du->unit) { |
| 1296 | drm_object_property_set_value |
| 1297 | (obj: &con->base, property: dev->mode_config.suggested_x_property, |
| 1298 | val: du->gui_x); |
| 1299 | drm_object_property_set_value |
| 1300 | (obj: &con->base, property: dev->mode_config.suggested_y_property, |
| 1301 | val: du->gui_y); |
| 1302 | } else { |
| 1303 | drm_object_property_set_value |
| 1304 | (obj: &con->base, property: dev->mode_config.suggested_x_property, |
| 1305 | val: 0); |
| 1306 | drm_object_property_set_value |
| 1307 | (obj: &con->base, property: dev->mode_config.suggested_y_property, |
| 1308 | val: 0); |
| 1309 | } |
| 1310 | con->status = vmw_du_connector_detect(connector: con, force: true); |
| 1311 | } |
| 1312 | out_fini: |
| 1313 | drm_modeset_drop_locks(ctx: &ctx); |
| 1314 | drm_modeset_acquire_fini(ctx: &ctx); |
| 1315 | mutex_unlock(lock: &dev->mode_config.mutex); |
| 1316 | |
| 1317 | drm_sysfs_hotplug_event(dev); |
| 1318 | |
| 1319 | return 0; |
| 1320 | } |
| 1321 | |
| 1322 | int vmw_du_crtc_gamma_set(struct drm_crtc *crtc, |
| 1323 | u16 *r, u16 *g, u16 *b, |
| 1324 | uint32_t size, |
| 1325 | struct drm_modeset_acquire_ctx *ctx) |
| 1326 | { |
| 1327 | struct vmw_private *dev_priv = vmw_priv(dev: crtc->dev); |
| 1328 | int i; |
| 1329 | |
| 1330 | for (i = 0; i < size; i++) { |
| 1331 | DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n" , i, |
| 1332 | r[i], g[i], b[i]); |
| 1333 | vmw_write(dev_priv, offset: SVGA_PALETTE_BASE + i * 3 + 0, value: r[i] >> 8); |
| 1334 | vmw_write(dev_priv, offset: SVGA_PALETTE_BASE + i * 3 + 1, value: g[i] >> 8); |
| 1335 | vmw_write(dev_priv, offset: SVGA_PALETTE_BASE + i * 3 + 2, value: b[i] >> 8); |
| 1336 | } |
| 1337 | |
| 1338 | return 0; |
| 1339 | } |
| 1340 | |
| 1341 | int vmw_du_connector_dpms(struct drm_connector *connector, int mode) |
| 1342 | { |
| 1343 | return 0; |
| 1344 | } |
| 1345 | |
| 1346 | enum drm_connector_status |
| 1347 | vmw_du_connector_detect(struct drm_connector *connector, bool force) |
| 1348 | { |
| 1349 | uint32_t num_displays; |
| 1350 | struct drm_device *dev = connector->dev; |
| 1351 | struct vmw_private *dev_priv = vmw_priv(dev); |
| 1352 | struct vmw_display_unit *du = vmw_connector_to_du(connector); |
| 1353 | |
| 1354 | num_displays = vmw_read(dev_priv, offset: SVGA_REG_NUM_DISPLAYS); |
| 1355 | |
| 1356 | return ((vmw_connector_to_du(connector)->unit < num_displays && |
| 1357 | du->pref_active) ? |
| 1358 | connector_status_connected : connector_status_disconnected); |
| 1359 | } |
| 1360 | |
| 1361 | /** |
| 1362 | * vmw_guess_mode_timing - Provide fake timings for a |
| 1363 | * 60Hz vrefresh mode. |
| 1364 | * |
| 1365 | * @mode: Pointer to a struct drm_display_mode with hdisplay and vdisplay |
| 1366 | * members filled in. |
| 1367 | */ |
| 1368 | void vmw_guess_mode_timing(struct drm_display_mode *mode) |
| 1369 | { |
| 1370 | mode->hsync_start = mode->hdisplay + 50; |
| 1371 | mode->hsync_end = mode->hsync_start + 50; |
| 1372 | mode->htotal = mode->hsync_end + 50; |
| 1373 | |
| 1374 | mode->vsync_start = mode->vdisplay + 50; |
| 1375 | mode->vsync_end = mode->vsync_start + 50; |
| 1376 | mode->vtotal = mode->vsync_end + 50; |
| 1377 | |
| 1378 | mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6; |
| 1379 | } |
| 1380 | |
| 1381 | |
| 1382 | /** |
| 1383 | * vmw_kms_update_layout_ioctl - Handler for DRM_VMW_UPDATE_LAYOUT ioctl |
| 1384 | * @dev: drm device for the ioctl |
| 1385 | * @data: data pointer for the ioctl |
| 1386 | * @file_priv: drm file for the ioctl call |
| 1387 | * |
| 1388 | * Update preferred topology of display unit as per ioctl request. The topology |
| 1389 | * is expressed as array of drm_vmw_rect. |
| 1390 | * e.g. |
| 1391 | * [0 0 640 480] [640 0 800 600] [0 480 640 480] |
| 1392 | * |
| 1393 | * NOTE: |
| 1394 | * The x and y offset (upper left) in drm_vmw_rect cannot be less than 0. Beside |
| 1395 | * device limit on topology, x + w and y + h (lower right) cannot be greater |
| 1396 | * than INT_MAX. So topology beyond these limits will return with error. |
| 1397 | * |
| 1398 | * Returns: |
| 1399 | * Zero on success, negative errno on failure. |
| 1400 | */ |
| 1401 | int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data, |
| 1402 | struct drm_file *file_priv) |
| 1403 | { |
| 1404 | struct vmw_private *dev_priv = vmw_priv(dev); |
| 1405 | struct drm_mode_config *mode_config = &dev->mode_config; |
| 1406 | struct drm_vmw_update_layout_arg *arg = |
| 1407 | (struct drm_vmw_update_layout_arg *)data; |
| 1408 | const void __user *user_rects; |
| 1409 | struct drm_vmw_rect *rects; |
| 1410 | struct drm_rect *drm_rects; |
| 1411 | unsigned rects_size; |
| 1412 | int ret, i; |
| 1413 | |
| 1414 | if (!arg->num_outputs) { |
| 1415 | struct drm_rect def_rect = {0, 0, |
| 1416 | VMWGFX_MIN_INITIAL_WIDTH, |
| 1417 | VMWGFX_MIN_INITIAL_HEIGHT}; |
| 1418 | vmw_du_update_layout(dev_priv, num_rects: 1, rects: &def_rect); |
| 1419 | return 0; |
| 1420 | } else if (arg->num_outputs > VMWGFX_NUM_DISPLAY_UNITS) { |
| 1421 | return -E2BIG; |
| 1422 | } |
| 1423 | |
| 1424 | rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect); |
| 1425 | rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect), |
| 1426 | GFP_KERNEL); |
| 1427 | if (unlikely(!rects)) |
| 1428 | return -ENOMEM; |
| 1429 | |
| 1430 | user_rects = (void __user *)(unsigned long)arg->rects; |
| 1431 | ret = copy_from_user(to: rects, from: user_rects, n: rects_size); |
| 1432 | if (unlikely(ret != 0)) { |
| 1433 | DRM_ERROR("Failed to get rects.\n" ); |
| 1434 | ret = -EFAULT; |
| 1435 | goto out_free; |
| 1436 | } |
| 1437 | |
| 1438 | drm_rects = (struct drm_rect *)rects; |
| 1439 | |
| 1440 | VMW_DEBUG_KMS("Layout count = %u\n" , arg->num_outputs); |
| 1441 | for (i = 0; i < arg->num_outputs; i++) { |
| 1442 | struct drm_vmw_rect curr_rect; |
| 1443 | |
| 1444 | /* Verify user-space for overflow as kernel use drm_rect */ |
| 1445 | if ((rects[i].x + rects[i].w > INT_MAX) || |
| 1446 | (rects[i].y + rects[i].h > INT_MAX)) { |
| 1447 | ret = -ERANGE; |
| 1448 | goto out_free; |
| 1449 | } |
| 1450 | |
| 1451 | curr_rect = rects[i]; |
| 1452 | drm_rects[i].x1 = curr_rect.x; |
| 1453 | drm_rects[i].y1 = curr_rect.y; |
| 1454 | drm_rects[i].x2 = curr_rect.x + curr_rect.w; |
| 1455 | drm_rects[i].y2 = curr_rect.y + curr_rect.h; |
| 1456 | |
| 1457 | VMW_DEBUG_KMS(" x1 = %d y1 = %d x2 = %d y2 = %d\n" , |
| 1458 | drm_rects[i].x1, drm_rects[i].y1, |
| 1459 | drm_rects[i].x2, drm_rects[i].y2); |
| 1460 | |
| 1461 | /* |
| 1462 | * Currently this check is limiting the topology within |
| 1463 | * mode_config->max (which actually is max texture size |
| 1464 | * supported by virtual device). This limit is here to address |
| 1465 | * window managers that create a big framebuffer for whole |
| 1466 | * topology. |
| 1467 | */ |
| 1468 | if (drm_rects[i].x1 < 0 || drm_rects[i].y1 < 0 || |
| 1469 | drm_rects[i].x2 > mode_config->max_width || |
| 1470 | drm_rects[i].y2 > mode_config->max_height) { |
| 1471 | VMW_DEBUG_KMS("Invalid layout %d %d %d %d\n" , |
| 1472 | drm_rects[i].x1, drm_rects[i].y1, |
| 1473 | drm_rects[i].x2, drm_rects[i].y2); |
| 1474 | ret = -EINVAL; |
| 1475 | goto out_free; |
| 1476 | } |
| 1477 | } |
| 1478 | |
| 1479 | ret = vmw_kms_check_display_memory(dev, num_rects: arg->num_outputs, rects: drm_rects); |
| 1480 | |
| 1481 | if (ret == 0) |
| 1482 | vmw_du_update_layout(dev_priv, num_rects: arg->num_outputs, rects: drm_rects); |
| 1483 | |
| 1484 | out_free: |
| 1485 | kfree(objp: rects); |
| 1486 | return ret; |
| 1487 | } |
| 1488 | |
| 1489 | /** |
| 1490 | * vmw_kms_helper_dirty - Helper to build commands and perform actions based |
| 1491 | * on a set of cliprects and a set of display units. |
| 1492 | * |
| 1493 | * @dev_priv: Pointer to a device private structure. |
| 1494 | * @framebuffer: Pointer to the framebuffer on which to perform the actions. |
| 1495 | * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL. |
| 1496 | * Cliprects are given in framebuffer coordinates. |
| 1497 | * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must |
| 1498 | * be NULL. Cliprects are given in source coordinates. |
| 1499 | * @dest_x: X coordinate offset for the crtc / destination clip rects. |
| 1500 | * @dest_y: Y coordinate offset for the crtc / destination clip rects. |
| 1501 | * @num_clips: Number of cliprects in the @clips or @vclips array. |
| 1502 | * @increment: Integer with which to increment the clip counter when looping. |
| 1503 | * Used to skip a predetermined number of clip rects. |
| 1504 | * @dirty: Closure structure. See the description of struct vmw_kms_dirty. |
| 1505 | */ |
| 1506 | int vmw_kms_helper_dirty(struct vmw_private *dev_priv, |
| 1507 | struct vmw_framebuffer *framebuffer, |
| 1508 | const struct drm_clip_rect *clips, |
| 1509 | const struct drm_vmw_rect *vclips, |
| 1510 | s32 dest_x, s32 dest_y, |
| 1511 | int num_clips, |
| 1512 | int increment, |
| 1513 | struct vmw_kms_dirty *dirty) |
| 1514 | { |
| 1515 | struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS]; |
| 1516 | struct drm_crtc *crtc; |
| 1517 | u32 num_units = 0; |
| 1518 | u32 i, k; |
| 1519 | |
| 1520 | dirty->dev_priv = dev_priv; |
| 1521 | |
| 1522 | /* If crtc is passed, no need to iterate over other display units */ |
| 1523 | if (dirty->crtc) { |
| 1524 | units[num_units++] = vmw_crtc_to_du(dirty->crtc); |
| 1525 | } else { |
| 1526 | list_for_each_entry(crtc, &dev_priv->drm.mode_config.crtc_list, |
| 1527 | head) { |
| 1528 | struct drm_plane *plane = crtc->primary; |
| 1529 | |
| 1530 | if (plane->state->fb == &framebuffer->base) |
| 1531 | units[num_units++] = vmw_crtc_to_du(crtc); |
| 1532 | } |
| 1533 | } |
| 1534 | |
| 1535 | for (k = 0; k < num_units; k++) { |
| 1536 | struct vmw_display_unit *unit = units[k]; |
| 1537 | s32 crtc_x = unit->crtc.x; |
| 1538 | s32 crtc_y = unit->crtc.y; |
| 1539 | s32 crtc_width = unit->crtc.mode.hdisplay; |
| 1540 | s32 crtc_height = unit->crtc.mode.vdisplay; |
| 1541 | const struct drm_clip_rect *clips_ptr = clips; |
| 1542 | const struct drm_vmw_rect *vclips_ptr = vclips; |
| 1543 | |
| 1544 | dirty->unit = unit; |
| 1545 | if (dirty->fifo_reserve_size > 0) { |
| 1546 | dirty->cmd = VMW_CMD_RESERVE(dev_priv, |
| 1547 | dirty->fifo_reserve_size); |
| 1548 | if (!dirty->cmd) |
| 1549 | return -ENOMEM; |
| 1550 | |
| 1551 | memset(dirty->cmd, 0, dirty->fifo_reserve_size); |
| 1552 | } |
| 1553 | dirty->num_hits = 0; |
| 1554 | for (i = 0; i < num_clips; i++, clips_ptr += increment, |
| 1555 | vclips_ptr += increment) { |
| 1556 | s32 clip_left; |
| 1557 | s32 clip_top; |
| 1558 | |
| 1559 | /* |
| 1560 | * Select clip array type. Note that integer type |
| 1561 | * in @clips is unsigned short, whereas in @vclips |
| 1562 | * it's 32-bit. |
| 1563 | */ |
| 1564 | if (clips) { |
| 1565 | dirty->fb_x = (s32) clips_ptr->x1; |
| 1566 | dirty->fb_y = (s32) clips_ptr->y1; |
| 1567 | dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x - |
| 1568 | crtc_x; |
| 1569 | dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y - |
| 1570 | crtc_y; |
| 1571 | } else { |
| 1572 | dirty->fb_x = vclips_ptr->x; |
| 1573 | dirty->fb_y = vclips_ptr->y; |
| 1574 | dirty->unit_x2 = dirty->fb_x + vclips_ptr->w + |
| 1575 | dest_x - crtc_x; |
| 1576 | dirty->unit_y2 = dirty->fb_y + vclips_ptr->h + |
| 1577 | dest_y - crtc_y; |
| 1578 | } |
| 1579 | |
| 1580 | dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x; |
| 1581 | dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y; |
| 1582 | |
| 1583 | /* Skip this clip if it's outside the crtc region */ |
| 1584 | if (dirty->unit_x1 >= crtc_width || |
| 1585 | dirty->unit_y1 >= crtc_height || |
| 1586 | dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0) |
| 1587 | continue; |
| 1588 | |
| 1589 | /* Clip right and bottom to crtc limits */ |
| 1590 | dirty->unit_x2 = min_t(s32, dirty->unit_x2, |
| 1591 | crtc_width); |
| 1592 | dirty->unit_y2 = min_t(s32, dirty->unit_y2, |
| 1593 | crtc_height); |
| 1594 | |
| 1595 | /* Clip left and top to crtc limits */ |
| 1596 | clip_left = min_t(s32, dirty->unit_x1, 0); |
| 1597 | clip_top = min_t(s32, dirty->unit_y1, 0); |
| 1598 | dirty->unit_x1 -= clip_left; |
| 1599 | dirty->unit_y1 -= clip_top; |
| 1600 | dirty->fb_x -= clip_left; |
| 1601 | dirty->fb_y -= clip_top; |
| 1602 | |
| 1603 | dirty->clip(dirty); |
| 1604 | } |
| 1605 | |
| 1606 | dirty->fifo_commit(dirty); |
| 1607 | } |
| 1608 | |
| 1609 | return 0; |
| 1610 | } |
| 1611 | |
| 1612 | /** |
| 1613 | * vmw_kms_helper_validation_finish - Helper for post KMS command submission |
| 1614 | * cleanup and fencing |
| 1615 | * @dev_priv: Pointer to the device-private struct |
| 1616 | * @file_priv: Pointer identifying the client when user-space fencing is used |
| 1617 | * @ctx: Pointer to the validation context |
| 1618 | * @out_fence: If non-NULL, returned refcounted fence-pointer |
| 1619 | * @user_fence_rep: If non-NULL, pointer to user-space address area |
| 1620 | * in which to copy user-space fence info |
| 1621 | */ |
| 1622 | void vmw_kms_helper_validation_finish(struct vmw_private *dev_priv, |
| 1623 | struct drm_file *file_priv, |
| 1624 | struct vmw_validation_context *ctx, |
| 1625 | struct vmw_fence_obj **out_fence, |
| 1626 | struct drm_vmw_fence_rep __user * |
| 1627 | user_fence_rep) |
| 1628 | { |
| 1629 | struct vmw_fence_obj *fence = NULL; |
| 1630 | uint32_t handle = 0; |
| 1631 | int ret = 0; |
| 1632 | |
| 1633 | if (file_priv || user_fence_rep || vmw_validation_has_bos(ctx) || |
| 1634 | out_fence) |
| 1635 | ret = vmw_execbuf_fence_commands(file_priv, dev_priv, p_fence: &fence, |
| 1636 | p_handle: file_priv ? &handle : NULL); |
| 1637 | vmw_validation_done(ctx, fence); |
| 1638 | if (file_priv) |
| 1639 | vmw_execbuf_copy_fence_user(dev_priv, vmw_fp: vmw_fpriv(file_priv), |
| 1640 | ret, user_fence_rep, fence, |
| 1641 | fence_handle: handle, out_fence_fd: -1); |
| 1642 | if (out_fence) |
| 1643 | *out_fence = fence; |
| 1644 | else |
| 1645 | vmw_fence_obj_unreference(fence_p: &fence); |
| 1646 | } |
| 1647 | |
| 1648 | /** |
| 1649 | * vmw_kms_create_implicit_placement_property - Set up the implicit placement |
| 1650 | * property. |
| 1651 | * |
| 1652 | * @dev_priv: Pointer to a device private struct. |
| 1653 | * |
| 1654 | * Sets up the implicit placement property unless it's already set up. |
| 1655 | */ |
| 1656 | void |
| 1657 | vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv) |
| 1658 | { |
| 1659 | if (dev_priv->implicit_placement_property) |
| 1660 | return; |
| 1661 | |
| 1662 | dev_priv->implicit_placement_property = |
| 1663 | drm_property_create_range(dev: &dev_priv->drm, |
| 1664 | DRM_MODE_PROP_IMMUTABLE, |
| 1665 | name: "implicit_placement" , min: 0, max: 1); |
| 1666 | } |
| 1667 | |
| 1668 | /** |
| 1669 | * vmw_kms_suspend - Save modesetting state and turn modesetting off. |
| 1670 | * |
| 1671 | * @dev: Pointer to the drm device |
| 1672 | * Return: 0 on success. Negative error code on failure. |
| 1673 | */ |
| 1674 | int vmw_kms_suspend(struct drm_device *dev) |
| 1675 | { |
| 1676 | struct vmw_private *dev_priv = vmw_priv(dev); |
| 1677 | |
| 1678 | dev_priv->suspend_state = drm_atomic_helper_suspend(dev); |
| 1679 | if (IS_ERR(ptr: dev_priv->suspend_state)) { |
| 1680 | int ret = PTR_ERR(ptr: dev_priv->suspend_state); |
| 1681 | |
| 1682 | DRM_ERROR("Failed kms suspend: %d\n" , ret); |
| 1683 | dev_priv->suspend_state = NULL; |
| 1684 | |
| 1685 | return ret; |
| 1686 | } |
| 1687 | |
| 1688 | return 0; |
| 1689 | } |
| 1690 | |
| 1691 | |
| 1692 | /** |
| 1693 | * vmw_kms_resume - Re-enable modesetting and restore state |
| 1694 | * |
| 1695 | * @dev: Pointer to the drm device |
| 1696 | * Return: 0 on success. Negative error code on failure. |
| 1697 | * |
| 1698 | * State is resumed from a previous vmw_kms_suspend(). It's illegal |
| 1699 | * to call this function without a previous vmw_kms_suspend(). |
| 1700 | */ |
| 1701 | int vmw_kms_resume(struct drm_device *dev) |
| 1702 | { |
| 1703 | struct vmw_private *dev_priv = vmw_priv(dev); |
| 1704 | int ret; |
| 1705 | |
| 1706 | if (WARN_ON(!dev_priv->suspend_state)) |
| 1707 | return 0; |
| 1708 | |
| 1709 | ret = drm_atomic_helper_resume(dev, state: dev_priv->suspend_state); |
| 1710 | dev_priv->suspend_state = NULL; |
| 1711 | |
| 1712 | return ret; |
| 1713 | } |
| 1714 | |
| 1715 | /** |
| 1716 | * vmw_kms_lost_device - Notify kms that modesetting capabilities will be lost |
| 1717 | * |
| 1718 | * @dev: Pointer to the drm device |
| 1719 | */ |
| 1720 | void vmw_kms_lost_device(struct drm_device *dev) |
| 1721 | { |
| 1722 | drm_atomic_helper_shutdown(dev); |
| 1723 | } |
| 1724 | |
| 1725 | /** |
| 1726 | * vmw_du_helper_plane_update - Helper to do plane update on a display unit. |
| 1727 | * @update: The closure structure. |
| 1728 | * |
| 1729 | * Call this helper after setting callbacks in &vmw_du_update_plane to do plane |
| 1730 | * update on display unit. |
| 1731 | * |
| 1732 | * Return: 0 on success or a negative error code on failure. |
| 1733 | */ |
| 1734 | int vmw_du_helper_plane_update(struct vmw_du_update_plane *update) |
| 1735 | { |
| 1736 | struct drm_plane_state *state = update->plane->state; |
| 1737 | struct drm_plane_state *old_state = update->old_state; |
| 1738 | struct drm_atomic_helper_damage_iter iter; |
| 1739 | struct drm_rect clip; |
| 1740 | struct drm_rect bb; |
| 1741 | DECLARE_VAL_CONTEXT(val_ctx, NULL, 0); |
| 1742 | uint32_t reserved_size = 0; |
| 1743 | uint32_t submit_size = 0; |
| 1744 | uint32_t curr_size = 0; |
| 1745 | uint32_t num_hits = 0; |
| 1746 | void *cmd_start; |
| 1747 | char *cmd_next; |
| 1748 | int ret; |
| 1749 | |
| 1750 | /* |
| 1751 | * Iterate in advance to check if really need plane update and find the |
| 1752 | * number of clips that actually are in plane src for fifo allocation. |
| 1753 | */ |
| 1754 | drm_atomic_helper_damage_iter_init(iter: &iter, old_state, new_state: state); |
| 1755 | drm_atomic_for_each_plane_damage(&iter, &clip) |
| 1756 | num_hits++; |
| 1757 | |
| 1758 | if (num_hits == 0) |
| 1759 | return 0; |
| 1760 | |
| 1761 | if (update->vfb->bo) { |
| 1762 | struct vmw_framebuffer_bo *vfbbo = |
| 1763 | container_of(update->vfb, typeof(*vfbbo), base); |
| 1764 | |
| 1765 | /* |
| 1766 | * For screen targets we want a mappable bo, for everything else we want |
| 1767 | * accelerated i.e. host backed (vram or gmr) bo. If the display unit |
| 1768 | * is not screen target then mob's shouldn't be available. |
| 1769 | */ |
| 1770 | if (update->dev_priv->active_display_unit == vmw_du_screen_target) { |
| 1771 | vmw_bo_placement_set(bo: vfbbo->buffer, |
| 1772 | domain: VMW_BO_DOMAIN_SYS | VMW_BO_DOMAIN_MOB | VMW_BO_DOMAIN_GMR, |
| 1773 | busy_domain: VMW_BO_DOMAIN_SYS | VMW_BO_DOMAIN_MOB | VMW_BO_DOMAIN_GMR); |
| 1774 | } else { |
| 1775 | WARN_ON(update->dev_priv->has_mob); |
| 1776 | vmw_bo_placement_set_default_accelerated(bo: vfbbo->buffer); |
| 1777 | } |
| 1778 | ret = vmw_validation_add_bo(ctx: &val_ctx, vbo: vfbbo->buffer); |
| 1779 | } else { |
| 1780 | struct vmw_framebuffer_surface *vfbs = |
| 1781 | container_of(update->vfb, typeof(*vfbs), base); |
| 1782 | struct vmw_surface *surf = vmw_user_object_surface(uo: &vfbs->uo); |
| 1783 | |
| 1784 | ret = vmw_validation_add_resource(ctx: &val_ctx, res: &surf->res, |
| 1785 | priv_size: 0, VMW_RES_DIRTY_NONE, NULL, |
| 1786 | NULL); |
| 1787 | } |
| 1788 | |
| 1789 | if (ret) |
| 1790 | return ret; |
| 1791 | |
| 1792 | ret = vmw_validation_prepare(ctx: &val_ctx, mutex: update->mutex, intr: update->intr); |
| 1793 | if (ret) |
| 1794 | goto out_unref; |
| 1795 | |
| 1796 | reserved_size = update->calc_fifo_size(update, num_hits); |
| 1797 | cmd_start = VMW_CMD_RESERVE(update->dev_priv, reserved_size); |
| 1798 | if (!cmd_start) { |
| 1799 | ret = -ENOMEM; |
| 1800 | goto out_revert; |
| 1801 | } |
| 1802 | |
| 1803 | cmd_next = cmd_start; |
| 1804 | |
| 1805 | if (update->post_prepare) { |
| 1806 | curr_size = update->post_prepare(update, cmd_next); |
| 1807 | cmd_next += curr_size; |
| 1808 | submit_size += curr_size; |
| 1809 | } |
| 1810 | |
| 1811 | if (update->pre_clip) { |
| 1812 | curr_size = update->pre_clip(update, cmd_next, num_hits); |
| 1813 | cmd_next += curr_size; |
| 1814 | submit_size += curr_size; |
| 1815 | } |
| 1816 | |
| 1817 | bb.x1 = INT_MAX; |
| 1818 | bb.y1 = INT_MAX; |
| 1819 | bb.x2 = INT_MIN; |
| 1820 | bb.y2 = INT_MIN; |
| 1821 | |
| 1822 | drm_atomic_helper_damage_iter_init(iter: &iter, old_state, new_state: state); |
| 1823 | drm_atomic_for_each_plane_damage(&iter, &clip) { |
| 1824 | uint32_t fb_x = clip.x1; |
| 1825 | uint32_t fb_y = clip.y1; |
| 1826 | |
| 1827 | vmw_du_translate_to_crtc(state, r: &clip); |
| 1828 | if (update->clip) { |
| 1829 | curr_size = update->clip(update, cmd_next, &clip, fb_x, |
| 1830 | fb_y); |
| 1831 | cmd_next += curr_size; |
| 1832 | submit_size += curr_size; |
| 1833 | } |
| 1834 | bb.x1 = min_t(int, bb.x1, clip.x1); |
| 1835 | bb.y1 = min_t(int, bb.y1, clip.y1); |
| 1836 | bb.x2 = max_t(int, bb.x2, clip.x2); |
| 1837 | bb.y2 = max_t(int, bb.y2, clip.y2); |
| 1838 | } |
| 1839 | |
| 1840 | curr_size = update->post_clip(update, cmd_next, &bb); |
| 1841 | submit_size += curr_size; |
| 1842 | |
| 1843 | if (reserved_size < submit_size) |
| 1844 | submit_size = 0; |
| 1845 | |
| 1846 | vmw_cmd_commit(dev_priv: update->dev_priv, bytes: submit_size); |
| 1847 | |
| 1848 | vmw_kms_helper_validation_finish(dev_priv: update->dev_priv, NULL, ctx: &val_ctx, |
| 1849 | out_fence: update->out_fence, NULL); |
| 1850 | return ret; |
| 1851 | |
| 1852 | out_revert: |
| 1853 | vmw_validation_revert(ctx: &val_ctx); |
| 1854 | |
| 1855 | out_unref: |
| 1856 | vmw_validation_unref_lists(ctx: &val_ctx); |
| 1857 | return ret; |
| 1858 | } |
| 1859 | |
| 1860 | /** |
| 1861 | * vmw_connector_mode_valid - implements drm_connector_helper_funcs.mode_valid callback |
| 1862 | * |
| 1863 | * @connector: the drm connector, part of a DU container |
| 1864 | * @mode: drm mode to check |
| 1865 | * |
| 1866 | * Returns MODE_OK on success, or a drm_mode_status error code. |
| 1867 | */ |
| 1868 | enum drm_mode_status vmw_connector_mode_valid(struct drm_connector *connector, |
| 1869 | const struct drm_display_mode *mode) |
| 1870 | { |
| 1871 | enum drm_mode_status ret; |
| 1872 | struct drm_device *dev = connector->dev; |
| 1873 | struct vmw_private *dev_priv = vmw_priv(dev); |
| 1874 | u32 assumed_cpp = 4; |
| 1875 | |
| 1876 | if (dev_priv->assume_16bpp) |
| 1877 | assumed_cpp = 2; |
| 1878 | |
| 1879 | ret = drm_mode_validate_size(mode, maxX: dev_priv->texture_max_width, |
| 1880 | maxY: dev_priv->texture_max_height); |
| 1881 | if (ret != MODE_OK) |
| 1882 | return ret; |
| 1883 | |
| 1884 | if (!vmw_kms_validate_mode_vram(dev_priv, |
| 1885 | pitch: mode->hdisplay * assumed_cpp, |
| 1886 | height: mode->vdisplay)) |
| 1887 | return MODE_MEM; |
| 1888 | |
| 1889 | return MODE_OK; |
| 1890 | } |
| 1891 | |
| 1892 | /** |
| 1893 | * vmw_connector_get_modes - implements drm_connector_helper_funcs.get_modes callback |
| 1894 | * |
| 1895 | * @connector: the drm connector, part of a DU container |
| 1896 | * |
| 1897 | * Returns the number of added modes. |
| 1898 | */ |
| 1899 | int vmw_connector_get_modes(struct drm_connector *connector) |
| 1900 | { |
| 1901 | struct vmw_display_unit *du = vmw_connector_to_du(connector); |
| 1902 | struct drm_device *dev = connector->dev; |
| 1903 | struct vmw_private *dev_priv = vmw_priv(dev); |
| 1904 | struct drm_display_mode *mode = NULL; |
| 1905 | struct drm_display_mode prefmode = { DRM_MODE("preferred" , |
| 1906 | DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED, |
| 1907 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 1908 | DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) |
| 1909 | }; |
| 1910 | u32 max_width; |
| 1911 | u32 max_height; |
| 1912 | u32 num_modes; |
| 1913 | |
| 1914 | /* Add preferred mode */ |
| 1915 | mode = drm_mode_duplicate(dev, mode: &prefmode); |
| 1916 | if (!mode) |
| 1917 | return 0; |
| 1918 | |
| 1919 | mode->hdisplay = du->pref_width; |
| 1920 | mode->vdisplay = du->pref_height; |
| 1921 | vmw_guess_mode_timing(mode); |
| 1922 | drm_mode_set_name(mode); |
| 1923 | |
| 1924 | drm_mode_probed_add(connector, mode); |
| 1925 | drm_dbg_kms(dev, "preferred mode " DRM_MODE_FMT "\n" , DRM_MODE_ARG(mode)); |
| 1926 | |
| 1927 | /* Probe connector for all modes not exceeding our geom limits */ |
| 1928 | max_width = dev_priv->texture_max_width; |
| 1929 | max_height = dev_priv->texture_max_height; |
| 1930 | |
| 1931 | if (dev_priv->active_display_unit == vmw_du_screen_target) { |
| 1932 | max_width = min(dev_priv->stdu_max_width, max_width); |
| 1933 | max_height = min(dev_priv->stdu_max_height, max_height); |
| 1934 | } |
| 1935 | |
| 1936 | num_modes = 1 + drm_add_modes_noedid(connector, hdisplay: max_width, vdisplay: max_height); |
| 1937 | |
| 1938 | return num_modes; |
| 1939 | } |
| 1940 | |
| 1941 | struct vmw_user_object *vmw_user_object_ref(struct vmw_user_object *uo) |
| 1942 | { |
| 1943 | if (uo->buffer) |
| 1944 | vmw_user_bo_ref(vbo: uo->buffer); |
| 1945 | else if (uo->surface) |
| 1946 | vmw_surface_reference(srf: uo->surface); |
| 1947 | return uo; |
| 1948 | } |
| 1949 | |
| 1950 | void vmw_user_object_unref(struct vmw_user_object *uo) |
| 1951 | { |
| 1952 | if (uo->buffer) |
| 1953 | vmw_user_bo_unref(buf: &uo->buffer); |
| 1954 | else if (uo->surface) |
| 1955 | vmw_surface_unreference(srf: &uo->surface); |
| 1956 | } |
| 1957 | |
| 1958 | struct vmw_bo * |
| 1959 | vmw_user_object_buffer(struct vmw_user_object *uo) |
| 1960 | { |
| 1961 | if (uo->buffer) |
| 1962 | return uo->buffer; |
| 1963 | else if (uo->surface) |
| 1964 | return uo->surface->res.guest_memory_bo; |
| 1965 | return NULL; |
| 1966 | } |
| 1967 | |
| 1968 | struct vmw_surface * |
| 1969 | vmw_user_object_surface(struct vmw_user_object *uo) |
| 1970 | { |
| 1971 | if (uo->buffer) |
| 1972 | return uo->buffer->dumb_surface; |
| 1973 | return uo->surface; |
| 1974 | } |
| 1975 | |
| 1976 | void *vmw_user_object_map(struct vmw_user_object *uo) |
| 1977 | { |
| 1978 | struct vmw_bo *bo = vmw_user_object_buffer(uo); |
| 1979 | |
| 1980 | WARN_ON(!bo); |
| 1981 | return vmw_bo_map_and_cache(vbo: bo); |
| 1982 | } |
| 1983 | |
| 1984 | void *vmw_user_object_map_size(struct vmw_user_object *uo, size_t size) |
| 1985 | { |
| 1986 | struct vmw_bo *bo = vmw_user_object_buffer(uo); |
| 1987 | |
| 1988 | WARN_ON(!bo); |
| 1989 | return vmw_bo_map_and_cache_size(vbo: bo, size); |
| 1990 | } |
| 1991 | |
| 1992 | void vmw_user_object_unmap(struct vmw_user_object *uo) |
| 1993 | { |
| 1994 | struct vmw_bo *bo = vmw_user_object_buffer(uo); |
| 1995 | int ret; |
| 1996 | |
| 1997 | WARN_ON(!bo); |
| 1998 | |
| 1999 | /* Fence the mob creation so we are guarateed to have the mob */ |
| 2000 | ret = ttm_bo_reserve(bo: &bo->tbo, interruptible: false, no_wait: false, NULL); |
| 2001 | if (ret != 0) |
| 2002 | return; |
| 2003 | |
| 2004 | vmw_bo_unmap(vbo: bo); |
| 2005 | vmw_bo_pin_reserved(bo, pin: false); |
| 2006 | |
| 2007 | ttm_bo_unreserve(bo: &bo->tbo); |
| 2008 | } |
| 2009 | |
| 2010 | bool vmw_user_object_is_mapped(struct vmw_user_object *uo) |
| 2011 | { |
| 2012 | struct vmw_bo *bo; |
| 2013 | |
| 2014 | if (!uo || vmw_user_object_is_null(uo)) |
| 2015 | return false; |
| 2016 | |
| 2017 | bo = vmw_user_object_buffer(uo); |
| 2018 | |
| 2019 | if (WARN_ON(!bo)) |
| 2020 | return false; |
| 2021 | |
| 2022 | WARN_ON(bo->map.bo && !bo->map.virtual); |
| 2023 | return bo->map.virtual; |
| 2024 | } |
| 2025 | |
| 2026 | bool vmw_user_object_is_null(struct vmw_user_object *uo) |
| 2027 | { |
| 2028 | return !uo->buffer && !uo->surface; |
| 2029 | } |
| 2030 | |