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