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 #ifndef VMWGFX_KMS_H_ 10 #define VMWGFX_KMS_H_ 11 12 #include "vmwgfx_cursor_plane.h" 13 #include "vmwgfx_drv.h" 14 15 #include <drm/drm_encoder.h> 16 #include <drm/drm_framebuffer.h> 17 #include <drm/drm_probe_helper.h> 18 19 /** 20 * struct vmw_du_update_plane - Closure structure for vmw_du_helper_plane_update 21 * @plane: Plane which is being updated. 22 * @old_state: Old state of plane. 23 * @dev_priv: Device private. 24 * @du: Display unit on which to update the plane. 25 * @vfb: Framebuffer which is blitted to display unit. 26 * @out_fence: Out fence for resource finish. 27 * @mutex: The mutex used to protect resource reservation. 28 * @cpu_blit: True if need cpu blit. 29 * @intr: Whether to perform waits interruptible if possible. 30 * 31 * This structure loosely represent the set of operations needed to perform a 32 * plane update on a display unit. Implementer will define that functionality 33 * according to the function callbacks for this structure. In brief it involves 34 * surface/buffer object validation, populate FIFO commands and command 35 * submission to the device. 36 */ 37 struct vmw_du_update_plane { 38 /** 39 * @calc_fifo_size: Calculate fifo size. 40 * 41 * Determine fifo size for the commands needed for update. The number of 42 * damage clips on display unit @num_hits will be passed to allocate 43 * sufficient fifo space. 44 * 45 * Return: Fifo size needed 46 */ 47 uint32_t (*calc_fifo_size)(struct vmw_du_update_plane *update, 48 uint32_t num_hits); 49 50 /** 51 * @post_prepare: Populate fifo for resource preparation. 52 * 53 * Some surface resource or buffer object need some extra cmd submission 54 * like update GB image for proxy surface and define a GMRFB for screen 55 * object. That should be done here as this callback will be 56 * called after FIFO allocation with the address of command buufer. 57 * 58 * This callback is optional. 59 * 60 * Return: Size of commands populated to command buffer. 61 */ 62 uint32_t (*post_prepare)(struct vmw_du_update_plane *update, void *cmd); 63 64 /** 65 * @pre_clip: Populate fifo before clip. 66 * 67 * This is where pre clip related command should be populated like 68 * surface copy/DMA, etc. 69 * 70 * This callback is optional. 71 * 72 * Return: Size of commands populated to command buffer. 73 */ 74 uint32_t (*pre_clip)(struct vmw_du_update_plane *update, void *cmd, 75 uint32_t num_hits); 76 77 /** 78 * @clip: Populate fifo for clip. 79 * 80 * This is where to populate clips for surface copy/dma or blit commands 81 * if needed. This will be called times have damage in display unit, 82 * which is one if doing full update. @clip is the damage in destination 83 * coordinates which is crtc/DU and @src_x, @src_y is damage clip src in 84 * framebuffer coordinate. 85 * 86 * This callback is optional. 87 * 88 * Return: Size of commands populated to command buffer. 89 */ 90 uint32_t (*clip)(struct vmw_du_update_plane *update, void *cmd, 91 struct drm_rect *clip, uint32_t src_x, uint32_t src_y); 92 93 /** 94 * @post_clip: Populate fifo after clip. 95 * 96 * This is where to populate display unit update commands or blit 97 * commands. 98 * 99 * Return: Size of commands populated to command buffer. 100 */ 101 uint32_t (*post_clip)(struct vmw_du_update_plane *update, void *cmd, 102 struct drm_rect *bb); 103 104 struct drm_plane *plane; 105 struct drm_plane_state *old_state; 106 struct vmw_private *dev_priv; 107 struct vmw_display_unit *du; 108 struct vmw_framebuffer *vfb; 109 struct vmw_fence_obj **out_fence; 110 struct mutex *mutex; 111 bool intr; 112 }; 113 114 /** 115 * struct vmw_du_update_plane_surface - closure structure for surface 116 * @base: base closure structure. 117 * @cmd_start: FIFO command start address (used by SOU only). 118 */ 119 struct vmw_du_update_plane_surface { 120 struct vmw_du_update_plane base; 121 /* This member is to handle special case SOU surface update */ 122 void *cmd_start; 123 }; 124 125 /** 126 * struct vmw_du_update_plane_buffer - Closure structure for buffer object 127 * @base: Base closure structure. 128 * @fb_left: x1 for fb damage bounding box. 129 * @fb_top: y1 for fb damage bounding box. 130 */ 131 struct vmw_du_update_plane_buffer { 132 struct vmw_du_update_plane base; 133 int fb_left, fb_top; 134 }; 135 136 /** 137 * struct vmw_kms_dirty - closure structure for the vmw_kms_helper_dirty 138 * function. 139 * 140 * @fifo_commit: Callback that is called once for each display unit after 141 * all clip rects. This function must commit the fifo space reserved by the 142 * helper. Set up by the caller. 143 * @clip: Callback that is called for each cliprect on each display unit. 144 * Set up by the caller. 145 * @fifo_reserve_size: Fifo size that the helper should try to allocat for 146 * each display unit. Set up by the caller. 147 * @dev_priv: Pointer to the device private. Set up by the helper. 148 * @unit: The current display unit. Set up by the helper before a call to @clip. 149 * @cmd: The allocated fifo space. Set up by the helper before the first @clip 150 * call. 151 * @crtc: The crtc for which to build dirty commands. 152 * @num_hits: Number of clip rect commands for this display unit. 153 * Cleared by the helper before the first @clip call. Updated by the @clip 154 * callback. 155 * @fb_x: Clip rect left side in framebuffer coordinates. 156 * @fb_y: Clip rect right side in framebuffer coordinates. 157 * @unit_x1: Clip rect left side in crtc coordinates. 158 * @unit_y1: Clip rect top side in crtc coordinates. 159 * @unit_x2: Clip rect right side in crtc coordinates. 160 * @unit_y2: Clip rect bottom side in crtc coordinates. 161 * 162 * The clip rect coordinates are updated by the helper for each @clip call. 163 * Note that this may be derived from if more info needs to be passed between 164 * helper caller and helper callbacks. 165 */ 166 struct vmw_kms_dirty { 167 void (*fifo_commit)(struct vmw_kms_dirty *); 168 void (*clip)(struct vmw_kms_dirty *); 169 size_t fifo_reserve_size; 170 struct vmw_private *dev_priv; 171 struct vmw_display_unit *unit; 172 void *cmd; 173 struct drm_crtc *crtc; 174 u32 num_hits; 175 s32 fb_x; 176 s32 fb_y; 177 s32 unit_x1; 178 s32 unit_y1; 179 s32 unit_x2; 180 s32 unit_y2; 181 }; 182 183 #define vmw_framebuffer_to_vfb(x) \ 184 container_of(x, struct vmw_framebuffer, base) 185 #define vmw_framebuffer_to_vfbs(x) \ 186 container_of(x, struct vmw_framebuffer_surface, base.base) 187 #define vmw_framebuffer_to_vfbd(x) \ 188 container_of(x, struct vmw_framebuffer_bo, base.base) 189 190 /** 191 * Base class for framebuffers 192 * 193 * @pin is called the when ever a crtc uses this framebuffer 194 * @unpin is called 195 */ 196 struct vmw_framebuffer { 197 struct drm_framebuffer base; 198 bool bo; 199 }; 200 201 struct vmw_framebuffer_surface { 202 struct vmw_framebuffer base; 203 struct vmw_user_object uo; 204 }; 205 206 struct vmw_framebuffer_bo { 207 struct vmw_framebuffer base; 208 struct vmw_bo *buffer; 209 }; 210 211 212 static const uint32_t __maybe_unused vmw_primary_plane_formats[] = { 213 DRM_FORMAT_XRGB8888, 214 DRM_FORMAT_ARGB8888, 215 DRM_FORMAT_RGB565, 216 DRM_FORMAT_XRGB1555, 217 }; 218 219 220 #define vmw_crtc_state_to_vcs(x) container_of(x, struct vmw_crtc_state, base) 221 #define vmw_plane_state_to_vps(x) container_of(x, struct vmw_plane_state, base) 222 #define vmw_connector_state_to_vcs(x) \ 223 container_of(x, struct vmw_connector_state, base) 224 225 /** 226 * Derived class for crtc state object 227 * 228 * @base DRM crtc object 229 */ 230 struct vmw_crtc_state { 231 struct drm_crtc_state base; 232 }; 233 234 235 /** 236 * Derived class for plane state object 237 * 238 * @base DRM plane object 239 * @surf Display surface for STDU 240 * @bo display bo for SOU 241 * @content_fb_type Used by STDU. 242 * @bo_size Size of the bo, used by Screen Object Display Unit 243 * @pinned pin count for STDU display surface 244 */ 245 struct vmw_plane_state { 246 struct drm_plane_state base; 247 struct vmw_user_object uo; 248 249 int content_fb_type; 250 unsigned long bo_size; 251 252 int pinned; 253 254 /* For CPU Blit */ 255 unsigned int cpp; 256 257 struct vmw_cursor_plane_state cursor; 258 }; 259 260 261 /** 262 * Derived class for connector state object 263 * 264 * @base DRM connector object 265 * @is_implicit connector property 266 * 267 */ 268 struct vmw_connector_state { 269 struct drm_connector_state base; 270 271 /** 272 * @gui_x: 273 * 274 * vmwgfx connector property representing the x position of this display 275 * unit (connector is synonymous to display unit) in overall topology. 276 * This is what the device expect as xRoot while creating screen. 277 */ 278 int gui_x; 279 280 /** 281 * @gui_y: 282 * 283 * vmwgfx connector property representing the y position of this display 284 * unit (connector is synonymous to display unit) in overall topology. 285 * This is what the device expect as yRoot while creating screen. 286 */ 287 int gui_y; 288 }; 289 290 291 /** 292 * Base class display unit. 293 * 294 * Since the SVGA hw doesn't have a concept of a crtc, encoder or connector 295 * so the display unit is all of them at the same time. This is true for both 296 * legacy multimon and screen objects. 297 */ 298 struct vmw_display_unit { 299 struct drm_crtc crtc; 300 struct drm_encoder encoder; 301 struct drm_connector connector; 302 struct drm_plane primary; 303 struct vmw_cursor_plane cursor; 304 305 unsigned unit; 306 307 /* 308 * Prefered mode tracking. 309 */ 310 unsigned pref_width; 311 unsigned pref_height; 312 bool pref_active; 313 314 /* 315 * Gui positioning 316 */ 317 int gui_x; 318 int gui_y; 319 bool is_implicit; 320 int set_gui_x; 321 int set_gui_y; 322 323 struct { 324 struct work_struct crc_generator_work; 325 326 /* protects concurrent access to the vblank handler */ 327 atomic_t atomic_lock; 328 /* protected by @atomic_lock */ 329 bool crc_enabled; 330 struct vmw_surface *surface; 331 332 /* protects concurrent access to the crc worker */ 333 spinlock_t crc_state_lock; 334 /* protected by @crc_state_lock */ 335 bool crc_pending; 336 u64 frame_start; 337 u64 frame_end; 338 } vkms; 339 }; 340 341 #define vmw_crtc_to_du(x) \ 342 container_of(x, struct vmw_display_unit, crtc) 343 #define vmw_connector_to_du(x) \ 344 container_of(x, struct vmw_display_unit, connector) 345 346 347 /* 348 * Shared display unit functions - vmwgfx_kms.c 349 */ 350 void vmw_du_init(struct vmw_display_unit *du); 351 void vmw_du_cleanup(struct vmw_display_unit *du); 352 int vmw_du_crtc_gamma_set(struct drm_crtc *crtc, 353 u16 *r, u16 *g, u16 *b, 354 uint32_t size, 355 struct drm_modeset_acquire_ctx *ctx); 356 int vmw_du_connector_set_property(struct drm_connector *connector, 357 struct drm_property *property, 358 uint64_t val); 359 int vmw_du_connector_atomic_set_property(struct drm_connector *connector, 360 struct drm_connector_state *state, 361 struct drm_property *property, 362 uint64_t val); 363 int 364 vmw_du_connector_atomic_get_property(struct drm_connector *connector, 365 const struct drm_connector_state *state, 366 struct drm_property *property, 367 uint64_t *val); 368 int vmw_du_connector_dpms(struct drm_connector *connector, int mode); 369 void vmw_du_connector_save(struct drm_connector *connector); 370 void vmw_du_connector_restore(struct drm_connector *connector); 371 enum drm_connector_status 372 vmw_du_connector_detect(struct drm_connector *connector, bool force); 373 int vmw_kms_helper_dirty(struct vmw_private *dev_priv, 374 struct vmw_framebuffer *framebuffer, 375 const struct drm_clip_rect *clips, 376 const struct drm_vmw_rect *vclips, 377 s32 dest_x, s32 dest_y, 378 int num_clips, 379 int increment, 380 struct vmw_kms_dirty *dirty); 381 enum drm_mode_status vmw_connector_mode_valid(struct drm_connector *connector, 382 const struct drm_display_mode *mode); 383 int vmw_connector_get_modes(struct drm_connector *connector); 384 385 void vmw_kms_helper_validation_finish(struct vmw_private *dev_priv, 386 struct drm_file *file_priv, 387 struct vmw_validation_context *ctx, 388 struct vmw_fence_obj **out_fence, 389 struct drm_vmw_fence_rep __user * 390 user_fence_rep); 391 int vmw_kms_readback(struct vmw_private *dev_priv, 392 struct drm_file *file_priv, 393 struct vmw_framebuffer *vfb, 394 struct drm_vmw_fence_rep __user *user_fence_rep, 395 struct drm_vmw_rect *vclips, 396 uint32_t num_clips); 397 struct vmw_framebuffer * 398 vmw_kms_new_framebuffer(struct vmw_private *dev_priv, 399 struct vmw_user_object *uo, 400 const struct drm_format_info *info, 401 const struct drm_mode_fb_cmd2 *mode_cmd); 402 void vmw_guess_mode_timing(struct drm_display_mode *mode); 403 void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv); 404 void vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv); 405 406 /* Universal Plane Helpers */ 407 void vmw_du_primary_plane_destroy(struct drm_plane *plane); 408 409 /* Atomic Helpers */ 410 int vmw_du_primary_plane_atomic_check(struct drm_plane *plane, 411 struct drm_atomic_commit *state); 412 void vmw_du_plane_cleanup_fb(struct drm_plane *plane, 413 struct drm_plane_state *old_state); 414 void vmw_du_plane_reset(struct drm_plane *plane); 415 struct drm_plane_state *vmw_du_plane_duplicate_state(struct drm_plane *plane); 416 void vmw_du_plane_destroy_state(struct drm_plane *plane, 417 struct drm_plane_state *state); 418 void vmw_du_plane_unpin_surf(struct vmw_plane_state *vps); 419 420 int vmw_du_crtc_atomic_check(struct drm_crtc *crtc, 421 struct drm_atomic_commit *state); 422 void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc, 423 struct drm_atomic_commit *state); 424 void vmw_du_crtc_reset(struct drm_crtc *crtc); 425 struct drm_crtc_state *vmw_du_crtc_duplicate_state(struct drm_crtc *crtc); 426 void vmw_du_crtc_destroy_state(struct drm_crtc *crtc, 427 struct drm_crtc_state *state); 428 void vmw_du_connector_reset(struct drm_connector *connector); 429 struct drm_connector_state * 430 vmw_du_connector_duplicate_state(struct drm_connector *connector); 431 432 void vmw_du_connector_destroy_state(struct drm_connector *connector, 433 struct drm_connector_state *state); 434 435 /* 436 * Legacy display unit functions - vmwgfx_ldu.c 437 */ 438 int vmw_kms_ldu_init_display(struct vmw_private *dev_priv); 439 int vmw_kms_ldu_close_display(struct vmw_private *dev_priv); 440 int vmw_kms_update_proxy(struct vmw_resource *res, 441 const struct drm_clip_rect *clips, 442 unsigned num_clips, 443 int increment); 444 445 /* 446 * Screen Objects display functions - vmwgfx_scrn.c 447 */ 448 int vmw_kms_sou_init_display(struct vmw_private *dev_priv); 449 int vmw_kms_sou_do_surface_dirty(struct vmw_private *dev_priv, 450 struct vmw_framebuffer *framebuffer, 451 struct drm_clip_rect *clips, 452 struct drm_vmw_rect *vclips, 453 struct vmw_resource *srf, 454 s32 dest_x, 455 s32 dest_y, 456 unsigned num_clips, int inc, 457 struct vmw_fence_obj **out_fence, 458 struct drm_crtc *crtc); 459 int vmw_kms_sou_do_bo_dirty(struct vmw_private *dev_priv, 460 struct vmw_framebuffer *framebuffer, 461 struct drm_clip_rect *clips, 462 struct drm_vmw_rect *vclips, 463 unsigned int num_clips, int increment, 464 bool interruptible, 465 struct vmw_fence_obj **out_fence, 466 struct drm_crtc *crtc); 467 int vmw_kms_sou_readback(struct vmw_private *dev_priv, 468 struct drm_file *file_priv, 469 struct vmw_framebuffer *vfb, 470 struct drm_vmw_fence_rep __user *user_fence_rep, 471 struct drm_vmw_rect *vclips, 472 uint32_t num_clips, 473 struct drm_crtc *crtc); 474 475 /* 476 * Screen Target Display Unit functions - vmwgfx_stdu.c 477 */ 478 int vmw_kms_stdu_init_display(struct vmw_private *dev_priv); 479 int vmw_kms_stdu_surface_dirty(struct vmw_private *dev_priv, 480 struct vmw_framebuffer *framebuffer, 481 struct drm_clip_rect *clips, 482 struct drm_vmw_rect *vclips, 483 struct vmw_resource *srf, 484 s32 dest_x, 485 s32 dest_y, 486 unsigned num_clips, int inc, 487 struct vmw_fence_obj **out_fence, 488 struct drm_crtc *crtc); 489 int vmw_kms_stdu_readback(struct vmw_private *dev_priv, 490 struct drm_file *file_priv, 491 struct vmw_framebuffer *vfb, 492 struct drm_vmw_fence_rep __user *user_fence_rep, 493 struct drm_clip_rect *clips, 494 struct drm_vmw_rect *vclips, 495 uint32_t num_clips, 496 int increment, 497 struct drm_crtc *crtc); 498 499 int vmw_du_helper_plane_update(struct vmw_du_update_plane *update); 500 501 /** 502 * vmw_du_translate_to_crtc - Translate a rect from framebuffer to crtc 503 * @state: Plane state. 504 * @r: Rectangle to translate. 505 */ 506 static inline void vmw_du_translate_to_crtc(struct drm_plane_state *state, 507 struct drm_rect *r) 508 { 509 int translate_crtc_x = -((state->src_x >> 16) - state->crtc_x); 510 int translate_crtc_y = -((state->src_y >> 16) - state->crtc_y); 511 512 drm_rect_translate(r, translate_crtc_x, translate_crtc_y); 513 } 514 515 #endif 516