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 vmw_framebuffer_to_vfb(x) \
202 	container_of(x, struct vmw_framebuffer, base)
203 #define vmw_framebuffer_to_vfbs(x) \
204 	container_of(x, struct vmw_framebuffer_surface, base.base)
205 #define vmw_framebuffer_to_vfbd(x) \
206 	container_of(x, struct vmw_framebuffer_bo, base.base)
207 
208 /**
209  * Base class for framebuffers
210  *
211  * @pin is called the when ever a crtc uses this framebuffer
212  * @unpin is called
213  */
214 struct vmw_framebuffer {
215 	struct drm_framebuffer base;
216 	bool bo;
217 	uint32_t user_handle;
218 };
219 
220 /*
221  * Clip rectangle
222  */
223 struct vmw_clip_rect {
224 	int x1, x2, y1, y2;
225 };
226 
227 struct vmw_framebuffer_surface {
228 	struct vmw_framebuffer base;
229 	struct vmw_surface *surface;
230 	struct vmw_bo *buffer;
231 	struct list_head head;
232 	bool is_bo_proxy;  /* true if this is proxy surface for DMA buf */
233 };
234 
235 
236 struct vmw_framebuffer_bo {
237 	struct vmw_framebuffer base;
238 	struct vmw_bo *buffer;
239 };
240 
241 
242 static const uint32_t __maybe_unused vmw_primary_plane_formats[] = {
243 	DRM_FORMAT_XRGB8888,
244 	DRM_FORMAT_ARGB8888,
245 	DRM_FORMAT_RGB565,
246 	DRM_FORMAT_XRGB1555,
247 };
248 
249 static const uint32_t __maybe_unused vmw_cursor_plane_formats[] = {
250 	DRM_FORMAT_ARGB8888,
251 };
252 
253 
254 #define vmw_crtc_state_to_vcs(x) container_of(x, struct vmw_crtc_state, base)
255 #define vmw_plane_state_to_vps(x) container_of(x, struct vmw_plane_state, base)
256 #define vmw_connector_state_to_vcs(x) \
257 		container_of(x, struct vmw_connector_state, base)
258 #define vmw_plane_to_vcp(x) container_of(x, struct vmw_cursor_plane, base)
259 
260 /**
261  * Derived class for crtc state object
262  *
263  * @base DRM crtc object
264  */
265 struct vmw_crtc_state {
266 	struct drm_crtc_state base;
267 };
268 
269 struct vmw_cursor_plane_state {
270 	struct vmw_bo *bo;
271 	s32 hotspot_x;
272 	s32 hotspot_y;
273 };
274 
275 /**
276  * Derived class for plane state object
277  *
278  * @base DRM plane object
279  * @surf Display surface for STDU
280  * @bo display bo for SOU
281  * @content_fb_type Used by STDU.
282  * @bo_size Size of the bo, used by Screen Object Display Unit
283  * @pinned pin count for STDU display surface
284  */
285 struct vmw_plane_state {
286 	struct drm_plane_state base;
287 	struct vmw_surface *surf;
288 	struct vmw_bo *bo;
289 
290 	int content_fb_type;
291 	unsigned long bo_size;
292 
293 	int pinned;
294 
295 	/* For CPU Blit */
296 	unsigned int cpp;
297 
298 	bool surf_mapped;
299 	struct vmw_cursor_plane_state cursor;
300 };
301 
302 
303 /**
304  * Derived class for connector state object
305  *
306  * @base DRM connector object
307  * @is_implicit connector property
308  *
309  */
310 struct vmw_connector_state {
311 	struct drm_connector_state base;
312 
313 	/**
314 	 * @gui_x:
315 	 *
316 	 * vmwgfx connector property representing the x position of this display
317 	 * unit (connector is synonymous to display unit) in overall topology.
318 	 * This is what the device expect as xRoot while creating screen.
319 	 */
320 	int gui_x;
321 
322 	/**
323 	 * @gui_y:
324 	 *
325 	 * vmwgfx connector property representing the y position of this display
326 	 * unit (connector is synonymous to display unit) in overall topology.
327 	 * This is what the device expect as yRoot while creating screen.
328 	 */
329 	int gui_y;
330 };
331 
332 /**
333  * Derived class for cursor plane object
334  *
335  * @base DRM plane object
336  * @cursor.cursor_mobs Cursor mobs available for re-use
337  */
338 struct vmw_cursor_plane {
339 	struct drm_plane base;
340 
341 	struct vmw_bo *cursor_mobs[3];
342 };
343 
344 /**
345  * Base class display unit.
346  *
347  * Since the SVGA hw doesn't have a concept of a crtc, encoder or connector
348  * so the display unit is all of them at the same time. This is true for both
349  * legacy multimon and screen objects.
350  */
351 struct vmw_display_unit {
352 	struct drm_crtc crtc;
353 	struct drm_encoder encoder;
354 	struct drm_connector connector;
355 	struct drm_plane primary;
356 	struct vmw_cursor_plane cursor;
357 
358 	struct vmw_surface *cursor_surface;
359 	struct vmw_bo *cursor_bo;
360 	size_t cursor_age;
361 
362 	int cursor_x;
363 	int cursor_y;
364 
365 	int hotspot_x;
366 	int hotspot_y;
367 	s32 core_hotspot_x;
368 	s32 core_hotspot_y;
369 
370 	unsigned unit;
371 
372 	/*
373 	 * Prefered mode tracking.
374 	 */
375 	unsigned pref_width;
376 	unsigned pref_height;
377 	bool pref_active;
378 
379 	/*
380 	 * Gui positioning
381 	 */
382 	int gui_x;
383 	int gui_y;
384 	bool is_implicit;
385 	int set_gui_x;
386 	int set_gui_y;
387 };
388 
389 struct vmw_validation_ctx {
390 	struct vmw_resource *res;
391 	struct vmw_bo *buf;
392 };
393 
394 #define vmw_crtc_to_du(x) \
395 	container_of(x, struct vmw_display_unit, crtc)
396 #define vmw_connector_to_du(x) \
397 	container_of(x, struct vmw_display_unit, connector)
398 
399 
400 /*
401  * Shared display unit functions - vmwgfx_kms.c
402  */
403 void vmw_du_cleanup(struct vmw_display_unit *du);
404 void vmw_du_crtc_save(struct drm_crtc *crtc);
405 void vmw_du_crtc_restore(struct drm_crtc *crtc);
406 int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
407 			   u16 *r, u16 *g, u16 *b,
408 			   uint32_t size,
409 			   struct drm_modeset_acquire_ctx *ctx);
410 int vmw_du_connector_set_property(struct drm_connector *connector,
411 				  struct drm_property *property,
412 				  uint64_t val);
413 int vmw_du_connector_atomic_set_property(struct drm_connector *connector,
414 					 struct drm_connector_state *state,
415 					 struct drm_property *property,
416 					 uint64_t val);
417 int
418 vmw_du_connector_atomic_get_property(struct drm_connector *connector,
419 				     const struct drm_connector_state *state,
420 				     struct drm_property *property,
421 				     uint64_t *val);
422 int vmw_du_connector_dpms(struct drm_connector *connector, int mode);
423 void vmw_du_connector_save(struct drm_connector *connector);
424 void vmw_du_connector_restore(struct drm_connector *connector);
425 enum drm_connector_status
426 vmw_du_connector_detect(struct drm_connector *connector, bool force);
427 int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
428 			 struct vmw_framebuffer *framebuffer,
429 			 const struct drm_clip_rect *clips,
430 			 const struct drm_vmw_rect *vclips,
431 			 s32 dest_x, s32 dest_y,
432 			 int num_clips,
433 			 int increment,
434 			 struct vmw_kms_dirty *dirty);
435 enum drm_mode_status vmw_connector_mode_valid(struct drm_connector *connector,
436 					      struct drm_display_mode *mode);
437 int vmw_connector_get_modes(struct drm_connector *connector);
438 
439 void vmw_kms_helper_validation_finish(struct vmw_private *dev_priv,
440 				      struct drm_file *file_priv,
441 				      struct vmw_validation_context *ctx,
442 				      struct vmw_fence_obj **out_fence,
443 				      struct drm_vmw_fence_rep __user *
444 				      user_fence_rep);
445 int vmw_kms_readback(struct vmw_private *dev_priv,
446 		     struct drm_file *file_priv,
447 		     struct vmw_framebuffer *vfb,
448 		     struct drm_vmw_fence_rep __user *user_fence_rep,
449 		     struct drm_vmw_rect *vclips,
450 		     uint32_t num_clips);
451 struct vmw_framebuffer *
452 vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
453 			struct vmw_bo *bo,
454 			struct vmw_surface *surface,
455 			bool only_2d,
456 			const struct drm_mode_fb_cmd2 *mode_cmd);
457 void vmw_guess_mode_timing(struct drm_display_mode *mode);
458 void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv);
459 void vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv);
460 
461 /* Universal Plane Helpers */
462 void vmw_du_primary_plane_destroy(struct drm_plane *plane);
463 void vmw_du_cursor_plane_destroy(struct drm_plane *plane);
464 
465 /* Atomic Helpers */
466 int vmw_du_primary_plane_atomic_check(struct drm_plane *plane,
467 				      struct drm_atomic_state *state);
468 int vmw_du_cursor_plane_atomic_check(struct drm_plane *plane,
469 				     struct drm_atomic_state *state);
470 void vmw_du_cursor_plane_atomic_update(struct drm_plane *plane,
471 				       struct drm_atomic_state *state);
472 int vmw_du_cursor_plane_prepare_fb(struct drm_plane *plane,
473 				   struct drm_plane_state *new_state);
474 void vmw_du_cursor_plane_cleanup_fb(struct drm_plane *plane,
475 			     struct drm_plane_state *old_state);
476 void vmw_du_plane_cleanup_fb(struct drm_plane *plane,
477 			     struct drm_plane_state *old_state);
478 void vmw_du_plane_reset(struct drm_plane *plane);
479 struct drm_plane_state *vmw_du_plane_duplicate_state(struct drm_plane *plane);
480 void vmw_du_plane_destroy_state(struct drm_plane *plane,
481 				struct drm_plane_state *state);
482 void vmw_du_plane_unpin_surf(struct vmw_plane_state *vps,
483 			     bool unreference);
484 
485 int vmw_du_crtc_atomic_check(struct drm_crtc *crtc,
486 			     struct drm_atomic_state *state);
487 void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc,
488 			      struct drm_atomic_state *state);
489 void vmw_du_crtc_atomic_flush(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