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