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_probe_helper.h>
33 
34 #include "vmwgfx_drv.h"
35 
36 /**
37  * struct vmw_du_update_plane - Closure structure for vmw_du_helper_plane_update
38  * @plane: Plane which is being updated.
39  * @old_state: Old state of plane.
40  * @dev_priv: Device private.
41  * @du: Display unit on which to update the plane.
42  * @vfb: Framebuffer which is blitted to display unit.
43  * @out_fence: Out fence for resource finish.
44  * @mutex: The mutex used to protect resource reservation.
45  * @cpu_blit: True if need cpu blit.
46  * @intr: Whether to perform waits interruptible if possible.
47  *
48  * This structure loosely represent the set of operations needed to perform a
49  * plane update on a display unit. Implementer will define that functionality
50  * according to the function callbacks for this structure. In brief it involves
51  * surface/buffer object validation, populate FIFO commands and command
52  * submission to the device.
53  */
54 struct vmw_du_update_plane {
55 	/**
56 	 * @calc_fifo_size: Calculate fifo size.
57 	 *
58 	 * Determine fifo size for the commands needed for update. The number of
59 	 * damage clips on display unit @num_hits will be passed to allocate
60 	 * sufficient fifo space.
61 	 *
62 	 * Return: Fifo size needed
63 	 */
64 	uint32_t (*calc_fifo_size)(struct vmw_du_update_plane *update,
65 				   uint32_t num_hits);
66 
67 	/**
68 	 * @post_prepare: Populate fifo for resource preparation.
69 	 *
70 	 * Some surface resource or buffer object need some extra cmd submission
71 	 * like update GB image for proxy surface and define a GMRFB for screen
72 	 * object. That should should be done here as this callback will be
73 	 * called after FIFO allocation with the address of command buufer.
74 	 *
75 	 * This callback is optional.
76 	 *
77 	 * Return: Size of commands populated to command buffer.
78 	 */
79 	uint32_t (*post_prepare)(struct vmw_du_update_plane *update, void *cmd);
80 
81 	/**
82 	 * @pre_clip: Populate fifo before clip.
83 	 *
84 	 * This is where pre clip related command should be populated like
85 	 * surface copy/DMA, etc.
86 	 *
87 	 * This callback is optional.
88 	 *
89 	 * Return: Size of commands populated to command buffer.
90 	 */
91 	uint32_t (*pre_clip)(struct vmw_du_update_plane *update, void *cmd,
92 			     uint32_t num_hits);
93 
94 	/**
95 	 * @clip: Populate fifo for clip.
96 	 *
97 	 * This is where to populate clips for surface copy/dma or blit commands
98 	 * if needed. This will be called times have damage in display unit,
99 	 * which is one if doing full update. @clip is the damage in destination
100 	 * coordinates which is crtc/DU and @src_x, @src_y is damage clip src in
101 	 * framebuffer coordinate.
102 	 *
103 	 * This callback is optional.
104 	 *
105 	 * Return: Size of commands populated to command buffer.
106 	 */
107 	uint32_t (*clip)(struct vmw_du_update_plane *update, void *cmd,
108 			 struct drm_rect *clip, uint32_t src_x, uint32_t src_y);
109 
110 	/**
111 	 * @post_clip: Populate fifo after clip.
112 	 *
113 	 * This is where to populate display unit update commands or blit
114 	 * commands.
115 	 *
116 	 * Return: Size of commands populated to command buffer.
117 	 */
118 	uint32_t (*post_clip)(struct vmw_du_update_plane *update, void *cmd,
119 				    struct drm_rect *bb);
120 
121 	struct drm_plane *plane;
122 	struct drm_plane_state *old_state;
123 	struct vmw_private *dev_priv;
124 	struct vmw_display_unit *du;
125 	struct vmw_framebuffer *vfb;
126 	struct vmw_fence_obj **out_fence;
127 	struct mutex *mutex;
128 	bool cpu_blit;
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 	int (*pin)(struct vmw_framebuffer *fb);
220 	int (*unpin)(struct vmw_framebuffer *fb);
221 	bool bo;
222 	uint32_t user_handle;
223 };
224 
225 /*
226  * Clip rectangle
227  */
228 struct vmw_clip_rect {
229 	int x1, x2, y1, y2;
230 };
231 
232 struct vmw_framebuffer_surface {
233 	struct vmw_framebuffer base;
234 	struct vmw_surface *surface;
235 	struct vmw_buffer_object *buffer;
236 	struct list_head head;
237 	bool is_bo_proxy;  /* true if this is proxy surface for DMA buf */
238 };
239 
240 
241 struct vmw_framebuffer_bo {
242 	struct vmw_framebuffer base;
243 	struct vmw_buffer_object *buffer;
244 };
245 
246 
247 static const uint32_t __maybe_unused vmw_primary_plane_formats[] = {
248 	DRM_FORMAT_XRGB1555,
249 	DRM_FORMAT_RGB565,
250 	DRM_FORMAT_XRGB8888,
251 	DRM_FORMAT_ARGB8888,
252 };
253 
254 static const uint32_t __maybe_unused vmw_cursor_plane_formats[] = {
255 	DRM_FORMAT_ARGB8888,
256 };
257 
258 
259 #define vmw_crtc_state_to_vcs(x) container_of(x, struct vmw_crtc_state, base)
260 #define vmw_plane_state_to_vps(x) container_of(x, struct vmw_plane_state, base)
261 #define vmw_connector_state_to_vcs(x) \
262 		container_of(x, struct vmw_connector_state, base)
263 #define vmw_plane_to_vcp(x) container_of(x, struct vmw_cursor_plane, base)
264 
265 /**
266  * Derived class for crtc state object
267  *
268  * @base DRM crtc object
269  */
270 struct vmw_crtc_state {
271 	struct drm_crtc_state base;
272 };
273 
274 /**
275  * Derived class for plane state object
276  *
277  * @base DRM plane object
278  * @surf Display surface for STDU
279  * @bo display bo for SOU
280  * @content_fb_type Used by STDU.
281  * @bo_size Size of the bo, used by Screen Object Display Unit
282  * @pinned pin count for STDU display surface
283  */
284 struct vmw_plane_state {
285 	struct drm_plane_state base;
286 	struct vmw_surface *surf;
287 	struct vmw_buffer_object *bo;
288 
289 	int content_fb_type;
290 	unsigned long bo_size;
291 
292 	int pinned;
293 
294 	/* For CPU Blit */
295 	unsigned int cpp;
296 
297 	/* CursorMob flipping index; -1 if cursor mobs not used */
298 	unsigned int cursor_mob_idx;
299 	/* Currently-active CursorMob */
300 	struct ttm_buffer_object *cm_bo;
301 	/* CursorMob kmap_obj; expected valid at cursor_plane_atomic_update
302 	   IFF currently-active CursorMob above is valid */
303 	struct ttm_bo_kmap_obj cm_map;
304 };
305 
306 
307 /**
308  * Derived class for connector state object
309  *
310  * @base DRM connector object
311  * @is_implicit connector property
312  *
313  */
314 struct vmw_connector_state {
315 	struct drm_connector_state base;
316 
317 	/**
318 	 * @gui_x:
319 	 *
320 	 * vmwgfx connector property representing the x position of this display
321 	 * unit (connector is synonymous to display unit) in overall topology.
322 	 * This is what the device expect as xRoot while creating screen.
323 	 */
324 	int gui_x;
325 
326 	/**
327 	 * @gui_y:
328 	 *
329 	 * vmwgfx connector property representing the y position of this display
330 	 * unit (connector is synonymous to display unit) in overall topology.
331 	 * This is what the device expect as yRoot while creating screen.
332 	 */
333 	int gui_y;
334 };
335 
336 /**
337  * Derived class for cursor plane object
338  *
339  * @base DRM plane object
340  * @cursor_mob array of two MOBs for CursorMob flipping
341  */
342 struct vmw_cursor_plane {
343 	struct drm_plane base;
344 	struct ttm_buffer_object *cursor_mob[2];
345 };
346 
347 /**
348  * Base class display unit.
349  *
350  * Since the SVGA hw doesn't have a concept of a crtc, encoder or connector
351  * so the display unit is all of them at the same time. This is true for both
352  * legacy multimon and screen objects.
353  */
354 struct vmw_display_unit {
355 	struct drm_crtc crtc;
356 	struct drm_encoder encoder;
357 	struct drm_connector connector;
358 	struct drm_plane primary;
359 	struct vmw_cursor_plane cursor;
360 
361 	struct vmw_surface *cursor_surface;
362 	struct vmw_buffer_object *cursor_bo;
363 	size_t cursor_age;
364 
365 	int cursor_x;
366 	int cursor_y;
367 
368 	int hotspot_x;
369 	int hotspot_y;
370 	s32 core_hotspot_x;
371 	s32 core_hotspot_y;
372 
373 	unsigned unit;
374 
375 	/*
376 	 * Prefered mode tracking.
377 	 */
378 	unsigned pref_width;
379 	unsigned pref_height;
380 	bool pref_active;
381 	struct drm_display_mode *pref_mode;
382 
383 	/*
384 	 * Gui positioning
385 	 */
386 	int gui_x;
387 	int gui_y;
388 	bool is_implicit;
389 	int set_gui_x;
390 	int set_gui_y;
391 };
392 
393 struct vmw_validation_ctx {
394 	struct vmw_resource *res;
395 	struct vmw_buffer_object *buf;
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_cleanup(struct vmw_display_unit *du);
408 void vmw_du_crtc_save(struct drm_crtc *crtc);
409 void vmw_du_crtc_restore(struct drm_crtc *crtc);
410 int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
411 			   u16 *r, u16 *g, u16 *b,
412 			   uint32_t size,
413 			   struct drm_modeset_acquire_ctx *ctx);
414 int vmw_du_connector_set_property(struct drm_connector *connector,
415 				  struct drm_property *property,
416 				  uint64_t val);
417 int vmw_du_connector_atomic_set_property(struct drm_connector *connector,
418 					 struct drm_connector_state *state,
419 					 struct drm_property *property,
420 					 uint64_t val);
421 int
422 vmw_du_connector_atomic_get_property(struct drm_connector *connector,
423 				     const struct drm_connector_state *state,
424 				     struct drm_property *property,
425 				     uint64_t *val);
426 int vmw_du_connector_dpms(struct drm_connector *connector, int mode);
427 void vmw_du_connector_save(struct drm_connector *connector);
428 void vmw_du_connector_restore(struct drm_connector *connector);
429 enum drm_connector_status
430 vmw_du_connector_detect(struct drm_connector *connector, bool force);
431 int vmw_du_connector_fill_modes(struct drm_connector *connector,
432 				uint32_t max_width, uint32_t max_height);
433 int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
434 			 struct vmw_framebuffer *framebuffer,
435 			 const struct drm_clip_rect *clips,
436 			 const struct drm_vmw_rect *vclips,
437 			 s32 dest_x, s32 dest_y,
438 			 int num_clips,
439 			 int increment,
440 			 struct vmw_kms_dirty *dirty);
441 
442 void vmw_kms_helper_validation_finish(struct vmw_private *dev_priv,
443 				      struct drm_file *file_priv,
444 				      struct vmw_validation_context *ctx,
445 				      struct vmw_fence_obj **out_fence,
446 				      struct drm_vmw_fence_rep __user *
447 				      user_fence_rep);
448 int vmw_kms_readback(struct vmw_private *dev_priv,
449 		     struct drm_file *file_priv,
450 		     struct vmw_framebuffer *vfb,
451 		     struct drm_vmw_fence_rep __user *user_fence_rep,
452 		     struct drm_vmw_rect *vclips,
453 		     uint32_t num_clips);
454 struct vmw_framebuffer *
455 vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
456 			struct vmw_buffer_object *bo,
457 			struct vmw_surface *surface,
458 			bool only_2d,
459 			const struct drm_mode_fb_cmd2 *mode_cmd);
460 int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
461 			    unsigned unit,
462 			    u32 max_width,
463 			    u32 max_height,
464 			    struct drm_connector **p_con,
465 			    struct drm_crtc **p_crtc,
466 			    struct drm_display_mode **p_mode);
467 void vmw_guess_mode_timing(struct drm_display_mode *mode);
468 void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv);
469 void vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv);
470 
471 /* Universal Plane Helpers */
472 void vmw_du_primary_plane_destroy(struct drm_plane *plane);
473 void vmw_du_cursor_plane_destroy(struct drm_plane *plane);
474 int vmw_du_create_cursor_mob_array(struct vmw_cursor_plane *vcp);
475 void vmw_du_destroy_cursor_mob_array(struct vmw_cursor_plane *vcp);
476 
477 /* Atomic Helpers */
478 int vmw_du_primary_plane_atomic_check(struct drm_plane *plane,
479 				      struct drm_atomic_state *state);
480 int vmw_du_cursor_plane_atomic_check(struct drm_plane *plane,
481 				     struct drm_atomic_state *state);
482 void vmw_du_cursor_plane_atomic_update(struct drm_plane *plane,
483 				       struct drm_atomic_state *state);
484 int vmw_du_cursor_plane_prepare_fb(struct drm_plane *plane,
485 				   struct drm_plane_state *new_state);
486 void vmw_du_cursor_plane_cleanup_fb(struct drm_plane *plane,
487 			     struct drm_plane_state *old_state);
488 void vmw_du_plane_cleanup_fb(struct drm_plane *plane,
489 			     struct drm_plane_state *old_state);
490 void vmw_du_plane_reset(struct drm_plane *plane);
491 struct drm_plane_state *vmw_du_plane_duplicate_state(struct drm_plane *plane);
492 void vmw_du_plane_destroy_state(struct drm_plane *plane,
493 				struct drm_plane_state *state);
494 void vmw_du_plane_unpin_surf(struct vmw_plane_state *vps,
495 			     bool unreference);
496 
497 int vmw_du_crtc_atomic_check(struct drm_crtc *crtc,
498 			     struct drm_atomic_state *state);
499 void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc,
500 			      struct drm_atomic_state *state);
501 void vmw_du_crtc_atomic_flush(struct drm_crtc *crtc,
502 			      struct drm_atomic_state *state);
503 void vmw_du_crtc_reset(struct drm_crtc *crtc);
504 struct drm_crtc_state *vmw_du_crtc_duplicate_state(struct drm_crtc *crtc);
505 void vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
506 				struct drm_crtc_state *state);
507 void vmw_du_connector_reset(struct drm_connector *connector);
508 struct drm_connector_state *
509 vmw_du_connector_duplicate_state(struct drm_connector *connector);
510 
511 void vmw_du_connector_destroy_state(struct drm_connector *connector,
512 				    struct drm_connector_state *state);
513 
514 /*
515  * Legacy display unit functions - vmwgfx_ldu.c
516  */
517 int vmw_kms_ldu_init_display(struct vmw_private *dev_priv);
518 int vmw_kms_ldu_close_display(struct vmw_private *dev_priv);
519 int vmw_kms_ldu_do_bo_dirty(struct vmw_private *dev_priv,
520 			    struct vmw_framebuffer *framebuffer,
521 			    unsigned int flags, unsigned int color,
522 			    struct drm_clip_rect *clips,
523 			    unsigned int num_clips, int increment);
524 int vmw_kms_update_proxy(struct vmw_resource *res,
525 			 const struct drm_clip_rect *clips,
526 			 unsigned num_clips,
527 			 int increment);
528 
529 /*
530  * Screen Objects display functions - vmwgfx_scrn.c
531  */
532 int vmw_kms_sou_init_display(struct vmw_private *dev_priv);
533 int vmw_kms_sou_do_surface_dirty(struct vmw_private *dev_priv,
534 				 struct vmw_framebuffer *framebuffer,
535 				 struct drm_clip_rect *clips,
536 				 struct drm_vmw_rect *vclips,
537 				 struct vmw_resource *srf,
538 				 s32 dest_x,
539 				 s32 dest_y,
540 				 unsigned num_clips, int inc,
541 				 struct vmw_fence_obj **out_fence,
542 				 struct drm_crtc *crtc);
543 int vmw_kms_sou_do_bo_dirty(struct vmw_private *dev_priv,
544 			    struct vmw_framebuffer *framebuffer,
545 			    struct drm_clip_rect *clips,
546 			    struct drm_vmw_rect *vclips,
547 			    unsigned int num_clips, int increment,
548 			    bool interruptible,
549 			    struct vmw_fence_obj **out_fence,
550 			    struct drm_crtc *crtc);
551 int vmw_kms_sou_readback(struct vmw_private *dev_priv,
552 			 struct drm_file *file_priv,
553 			 struct vmw_framebuffer *vfb,
554 			 struct drm_vmw_fence_rep __user *user_fence_rep,
555 			 struct drm_vmw_rect *vclips,
556 			 uint32_t num_clips,
557 			 struct drm_crtc *crtc);
558 
559 /*
560  * Screen Target Display Unit functions - vmwgfx_stdu.c
561  */
562 int vmw_kms_stdu_init_display(struct vmw_private *dev_priv);
563 int vmw_kms_stdu_surface_dirty(struct vmw_private *dev_priv,
564 			       struct vmw_framebuffer *framebuffer,
565 			       struct drm_clip_rect *clips,
566 			       struct drm_vmw_rect *vclips,
567 			       struct vmw_resource *srf,
568 			       s32 dest_x,
569 			       s32 dest_y,
570 			       unsigned num_clips, int inc,
571 			       struct vmw_fence_obj **out_fence,
572 			       struct drm_crtc *crtc);
573 int vmw_kms_stdu_dma(struct vmw_private *dev_priv,
574 		     struct drm_file *file_priv,
575 		     struct vmw_framebuffer *vfb,
576 		     struct drm_vmw_fence_rep __user *user_fence_rep,
577 		     struct drm_clip_rect *clips,
578 		     struct drm_vmw_rect *vclips,
579 		     uint32_t num_clips,
580 		     int increment,
581 		     bool to_surface,
582 		     bool interruptible,
583 		     struct drm_crtc *crtc);
584 
585 int vmw_du_helper_plane_update(struct vmw_du_update_plane *update);
586 
587 /**
588  * vmw_du_translate_to_crtc - Translate a rect from framebuffer to crtc
589  * @state: Plane state.
590  * @r: Rectangle to translate.
591  */
vmw_du_translate_to_crtc(struct drm_plane_state * state,struct drm_rect * r)592 static inline void vmw_du_translate_to_crtc(struct drm_plane_state *state,
593 					    struct drm_rect *r)
594 {
595 	int translate_crtc_x = -((state->src_x >> 16) - state->crtc_x);
596 	int translate_crtc_y = -((state->src_y >> 16) - state->crtc_y);
597 
598 	drm_rect_translate(r, translate_crtc_x, translate_crtc_y);
599 }
600 
601 #endif
602