1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
3  *
4  * Copyright 2009-2022 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 cpu_blit;
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 	int (*pin)(struct vmw_framebuffer *fb);
221 	int (*unpin)(struct vmw_framebuffer *fb);
222 	bool bo;
223 	uint32_t user_handle;
224 };
225 
226 /*
227  * Clip rectangle
228  */
229 struct vmw_clip_rect {
230 	int x1, x2, y1, y2;
231 };
232 
233 struct vmw_framebuffer_surface {
234 	struct vmw_framebuffer base;
235 	struct vmw_surface *surface;
236 	struct vmw_buffer_object *buffer;
237 	struct list_head head;
238 	bool is_bo_proxy;  /* true if this is proxy surface for DMA buf */
239 };
240 
241 
242 struct vmw_framebuffer_bo {
243 	struct vmw_framebuffer base;
244 	struct vmw_buffer_object *buffer;
245 };
246 
247 
248 static const uint32_t __maybe_unused vmw_primary_plane_formats[] = {
249 	DRM_FORMAT_XRGB1555,
250 	DRM_FORMAT_RGB565,
251 	DRM_FORMAT_XRGB8888,
252 	DRM_FORMAT_ARGB8888,
253 };
254 
255 static const uint32_t __maybe_unused vmw_cursor_plane_formats[] = {
256 	DRM_FORMAT_ARGB8888,
257 };
258 
259 
260 #define vmw_crtc_state_to_vcs(x) container_of(x, struct vmw_crtc_state, base)
261 #define vmw_plane_state_to_vps(x) container_of(x, struct vmw_plane_state, base)
262 #define vmw_connector_state_to_vcs(x) \
263 		container_of(x, struct vmw_connector_state, base)
264 #define vmw_plane_to_vcp(x) container_of(x, struct vmw_cursor_plane, base)
265 
266 /**
267  * Derived class for crtc state object
268  *
269  * @base DRM crtc object
270  */
271 struct vmw_crtc_state {
272 	struct drm_crtc_state base;
273 };
274 
275 struct vmw_cursor_plane_state {
276 	struct ttm_buffer_object *bo;
277 	struct ttm_bo_kmap_obj map;
278 	bool mapped;
279 	s32 hotspot_x;
280 	s32 hotspot_y;
281 };
282 
283 /**
284  * Derived class for plane state object
285  *
286  * @base DRM plane object
287  * @surf Display surface for STDU
288  * @bo display bo for SOU
289  * @content_fb_type Used by STDU.
290  * @bo_size Size of the bo, used by Screen Object Display Unit
291  * @pinned pin count for STDU display surface
292  */
293 struct vmw_plane_state {
294 	struct drm_plane_state base;
295 	struct vmw_surface *surf;
296 	struct vmw_buffer_object *bo;
297 
298 	int content_fb_type;
299 	unsigned long bo_size;
300 
301 	int pinned;
302 
303 	/* For CPU Blit */
304 	unsigned int cpp;
305 
306 	bool surf_mapped;
307 	struct vmw_cursor_plane_state cursor;
308 };
309 
310 
311 /**
312  * Derived class for connector state object
313  *
314  * @base DRM connector object
315  * @is_implicit connector property
316  *
317  */
318 struct vmw_connector_state {
319 	struct drm_connector_state base;
320 
321 	/**
322 	 * @gui_x:
323 	 *
324 	 * vmwgfx connector property representing the x position of this display
325 	 * unit (connector is synonymous to display unit) in overall topology.
326 	 * This is what the device expect as xRoot while creating screen.
327 	 */
328 	int gui_x;
329 
330 	/**
331 	 * @gui_y:
332 	 *
333 	 * vmwgfx connector property representing the y position of this display
334 	 * unit (connector is synonymous to display unit) in overall topology.
335 	 * This is what the device expect as yRoot while creating screen.
336 	 */
337 	int gui_y;
338 };
339 
340 /**
341  * Derived class for cursor plane object
342  *
343  * @base DRM plane object
344  * @cursor.cursor_mobs Cursor mobs available for re-use
345  */
346 struct vmw_cursor_plane {
347 	struct drm_plane base;
348 
349 	struct ttm_buffer_object *cursor_mobs[3];
350 };
351 
352 /**
353  * Base class display unit.
354  *
355  * Since the SVGA hw doesn't have a concept of a crtc, encoder or connector
356  * so the display unit is all of them at the same time. This is true for both
357  * legacy multimon and screen objects.
358  */
359 struct vmw_display_unit {
360 	struct drm_crtc crtc;
361 	struct drm_encoder encoder;
362 	struct drm_connector connector;
363 	struct drm_plane primary;
364 	struct vmw_cursor_plane cursor;
365 
366 	struct vmw_surface *cursor_surface;
367 	struct vmw_buffer_object *cursor_bo;
368 	size_t cursor_age;
369 
370 	int cursor_x;
371 	int cursor_y;
372 
373 	int hotspot_x;
374 	int hotspot_y;
375 	s32 core_hotspot_x;
376 	s32 core_hotspot_y;
377 
378 	unsigned unit;
379 
380 	/*
381 	 * Prefered mode tracking.
382 	 */
383 	unsigned pref_width;
384 	unsigned pref_height;
385 	bool pref_active;
386 	struct drm_display_mode *pref_mode;
387 
388 	/*
389 	 * Gui positioning
390 	 */
391 	int gui_x;
392 	int gui_y;
393 	bool is_implicit;
394 	int set_gui_x;
395 	int set_gui_y;
396 };
397 
398 struct vmw_validation_ctx {
399 	struct vmw_resource *res;
400 	struct vmw_buffer_object *buf;
401 };
402 
403 #define vmw_crtc_to_du(x) \
404 	container_of(x, struct vmw_display_unit, crtc)
405 #define vmw_connector_to_du(x) \
406 	container_of(x, struct vmw_display_unit, connector)
407 
408 
409 /*
410  * Shared display unit functions - vmwgfx_kms.c
411  */
412 void vmw_du_cleanup(struct vmw_display_unit *du);
413 void vmw_du_crtc_save(struct drm_crtc *crtc);
414 void vmw_du_crtc_restore(struct drm_crtc *crtc);
415 int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
416 			   u16 *r, u16 *g, u16 *b,
417 			   uint32_t size,
418 			   struct drm_modeset_acquire_ctx *ctx);
419 int vmw_du_connector_set_property(struct drm_connector *connector,
420 				  struct drm_property *property,
421 				  uint64_t val);
422 int vmw_du_connector_atomic_set_property(struct drm_connector *connector,
423 					 struct drm_connector_state *state,
424 					 struct drm_property *property,
425 					 uint64_t val);
426 int
427 vmw_du_connector_atomic_get_property(struct drm_connector *connector,
428 				     const struct drm_connector_state *state,
429 				     struct drm_property *property,
430 				     uint64_t *val);
431 int vmw_du_connector_dpms(struct drm_connector *connector, int mode);
432 void vmw_du_connector_save(struct drm_connector *connector);
433 void vmw_du_connector_restore(struct drm_connector *connector);
434 enum drm_connector_status
435 vmw_du_connector_detect(struct drm_connector *connector, bool force);
436 int vmw_du_connector_fill_modes(struct drm_connector *connector,
437 				uint32_t max_width, uint32_t max_height);
438 int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
439 			 struct vmw_framebuffer *framebuffer,
440 			 const struct drm_clip_rect *clips,
441 			 const struct drm_vmw_rect *vclips,
442 			 s32 dest_x, s32 dest_y,
443 			 int num_clips,
444 			 int increment,
445 			 struct vmw_kms_dirty *dirty);
446 
447 void vmw_kms_helper_validation_finish(struct vmw_private *dev_priv,
448 				      struct drm_file *file_priv,
449 				      struct vmw_validation_context *ctx,
450 				      struct vmw_fence_obj **out_fence,
451 				      struct drm_vmw_fence_rep __user *
452 				      user_fence_rep);
453 int vmw_kms_readback(struct vmw_private *dev_priv,
454 		     struct drm_file *file_priv,
455 		     struct vmw_framebuffer *vfb,
456 		     struct drm_vmw_fence_rep __user *user_fence_rep,
457 		     struct drm_vmw_rect *vclips,
458 		     uint32_t num_clips);
459 struct vmw_framebuffer *
460 vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
461 			struct vmw_buffer_object *bo,
462 			struct vmw_surface *surface,
463 			bool only_2d,
464 			const struct drm_mode_fb_cmd2 *mode_cmd);
465 void vmw_guess_mode_timing(struct drm_display_mode *mode);
466 void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv);
467 void vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv);
468 
469 /* Universal Plane Helpers */
470 void vmw_du_primary_plane_destroy(struct drm_plane *plane);
471 void vmw_du_cursor_plane_destroy(struct drm_plane *plane);
472 
473 /* Atomic Helpers */
474 int vmw_du_primary_plane_atomic_check(struct drm_plane *plane,
475 				      struct drm_atomic_state *state);
476 int vmw_du_cursor_plane_atomic_check(struct drm_plane *plane,
477 				     struct drm_atomic_state *state);
478 void vmw_du_cursor_plane_atomic_update(struct drm_plane *plane,
479 				       struct drm_atomic_state *state);
480 int vmw_du_cursor_plane_prepare_fb(struct drm_plane *plane,
481 				   struct drm_plane_state *new_state);
482 void vmw_du_cursor_plane_cleanup_fb(struct drm_plane *plane,
483 			     struct drm_plane_state *old_state);
484 void vmw_du_plane_cleanup_fb(struct drm_plane *plane,
485 			     struct drm_plane_state *old_state);
486 void vmw_du_plane_reset(struct drm_plane *plane);
487 struct drm_plane_state *vmw_du_plane_duplicate_state(struct drm_plane *plane);
488 void vmw_du_plane_destroy_state(struct drm_plane *plane,
489 				struct drm_plane_state *state);
490 void vmw_du_plane_unpin_surf(struct vmw_plane_state *vps,
491 			     bool unreference);
492 
493 int vmw_du_crtc_atomic_check(struct drm_crtc *crtc,
494 			     struct drm_atomic_state *state);
495 void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc,
496 			      struct drm_atomic_state *state);
497 void vmw_du_crtc_atomic_flush(struct drm_crtc *crtc,
498 			      struct drm_atomic_state *state);
499 void vmw_du_crtc_reset(struct drm_crtc *crtc);
500 struct drm_crtc_state *vmw_du_crtc_duplicate_state(struct drm_crtc *crtc);
501 void vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
502 				struct drm_crtc_state *state);
503 void vmw_du_connector_reset(struct drm_connector *connector);
504 struct drm_connector_state *
505 vmw_du_connector_duplicate_state(struct drm_connector *connector);
506 
507 void vmw_du_connector_destroy_state(struct drm_connector *connector,
508 				    struct drm_connector_state *state);
509 
510 /*
511  * Legacy display unit functions - vmwgfx_ldu.c
512  */
513 int vmw_kms_ldu_init_display(struct vmw_private *dev_priv);
514 int vmw_kms_ldu_close_display(struct vmw_private *dev_priv);
515 int vmw_kms_ldu_do_bo_dirty(struct vmw_private *dev_priv,
516 			    struct vmw_framebuffer *framebuffer,
517 			    unsigned int flags, unsigned int color,
518 			    struct drm_clip_rect *clips,
519 			    unsigned int num_clips, int increment);
520 int vmw_kms_update_proxy(struct vmw_resource *res,
521 			 const struct drm_clip_rect *clips,
522 			 unsigned num_clips,
523 			 int increment);
524 
525 /*
526  * Screen Objects display functions - vmwgfx_scrn.c
527  */
528 int vmw_kms_sou_init_display(struct vmw_private *dev_priv);
529 int vmw_kms_sou_do_surface_dirty(struct vmw_private *dev_priv,
530 				 struct vmw_framebuffer *framebuffer,
531 				 struct drm_clip_rect *clips,
532 				 struct drm_vmw_rect *vclips,
533 				 struct vmw_resource *srf,
534 				 s32 dest_x,
535 				 s32 dest_y,
536 				 unsigned num_clips, int inc,
537 				 struct vmw_fence_obj **out_fence,
538 				 struct drm_crtc *crtc);
539 int vmw_kms_sou_do_bo_dirty(struct vmw_private *dev_priv,
540 			    struct vmw_framebuffer *framebuffer,
541 			    struct drm_clip_rect *clips,
542 			    struct drm_vmw_rect *vclips,
543 			    unsigned int num_clips, int increment,
544 			    bool interruptible,
545 			    struct vmw_fence_obj **out_fence,
546 			    struct drm_crtc *crtc);
547 int vmw_kms_sou_readback(struct vmw_private *dev_priv,
548 			 struct drm_file *file_priv,
549 			 struct vmw_framebuffer *vfb,
550 			 struct drm_vmw_fence_rep __user *user_fence_rep,
551 			 struct drm_vmw_rect *vclips,
552 			 uint32_t num_clips,
553 			 struct drm_crtc *crtc);
554 
555 /*
556  * Screen Target Display Unit functions - vmwgfx_stdu.c
557  */
558 int vmw_kms_stdu_init_display(struct vmw_private *dev_priv);
559 int vmw_kms_stdu_surface_dirty(struct vmw_private *dev_priv,
560 			       struct vmw_framebuffer *framebuffer,
561 			       struct drm_clip_rect *clips,
562 			       struct drm_vmw_rect *vclips,
563 			       struct vmw_resource *srf,
564 			       s32 dest_x,
565 			       s32 dest_y,
566 			       unsigned num_clips, int inc,
567 			       struct vmw_fence_obj **out_fence,
568 			       struct drm_crtc *crtc);
569 int vmw_kms_stdu_dma(struct vmw_private *dev_priv,
570 		     struct drm_file *file_priv,
571 		     struct vmw_framebuffer *vfb,
572 		     struct drm_vmw_fence_rep __user *user_fence_rep,
573 		     struct drm_clip_rect *clips,
574 		     struct drm_vmw_rect *vclips,
575 		     uint32_t num_clips,
576 		     int increment,
577 		     bool to_surface,
578 		     bool interruptible,
579 		     struct drm_crtc *crtc);
580 
581 int vmw_du_helper_plane_update(struct vmw_du_update_plane *update);
582 
583 /**
584  * vmw_du_translate_to_crtc - Translate a rect from framebuffer to crtc
585  * @state: Plane state.
586  * @r: Rectangle to translate.
587  */
588 static inline void vmw_du_translate_to_crtc(struct drm_plane_state *state,
589 					    struct drm_rect *r)
590 {
591 	int translate_crtc_x = -((state->src_x >> 16) - state->crtc_x);
592 	int translate_crtc_y = -((state->src_y >> 16) - state->crtc_y);
593 
594 	drm_rect_translate(r, translate_crtc_x, translate_crtc_y);
595 }
596 
597 #endif
598