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 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 /**
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_buffer_object *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 	/* CursorMob flipping index; -1 if cursor mobs not used */
299 	unsigned int cursor_mob_idx;
300 	/* Currently-active CursorMob */
301 	struct ttm_buffer_object *cm_bo;
302 	/* CursorMob kmap_obj; expected valid at cursor_plane_atomic_update
303 	   IFF currently-active CursorMob above is valid */
304 	struct ttm_bo_kmap_obj cm_map;
305 };
306 
307 
308 /**
309  * Derived class for connector state object
310  *
311  * @base DRM connector object
312  * @is_implicit connector property
313  *
314  */
315 struct vmw_connector_state {
316 	struct drm_connector_state base;
317 
318 	/**
319 	 * @gui_x:
320 	 *
321 	 * vmwgfx connector property representing the x position of this display
322 	 * unit (connector is synonymous to display unit) in overall topology.
323 	 * This is what the device expect as xRoot while creating screen.
324 	 */
325 	int gui_x;
326 
327 	/**
328 	 * @gui_y:
329 	 *
330 	 * vmwgfx connector property representing the y position of this display
331 	 * unit (connector is synonymous to display unit) in overall topology.
332 	 * This is what the device expect as yRoot while creating screen.
333 	 */
334 	int gui_y;
335 };
336 
337 /**
338  * Derived class for cursor plane object
339  *
340  * @base DRM plane object
341  * @cursor_mob array of two MOBs for CursorMob flipping
342  */
343 struct vmw_cursor_plane {
344 	struct drm_plane base;
345 	struct ttm_buffer_object *cursor_mob[2];
346 };
347 
348 /**
349  * Base class display unit.
350  *
351  * Since the SVGA hw doesn't have a concept of a crtc, encoder or connector
352  * so the display unit is all of them at the same time. This is true for both
353  * legacy multimon and screen objects.
354  */
355 struct vmw_display_unit {
356 	struct drm_crtc crtc;
357 	struct drm_encoder encoder;
358 	struct drm_connector connector;
359 	struct drm_plane primary;
360 	struct vmw_cursor_plane cursor;
361 
362 	struct vmw_surface *cursor_surface;
363 	struct vmw_buffer_object *cursor_bo;
364 	size_t cursor_age;
365 
366 	int cursor_x;
367 	int cursor_y;
368 
369 	int hotspot_x;
370 	int hotspot_y;
371 	s32 core_hotspot_x;
372 	s32 core_hotspot_y;
373 
374 	unsigned unit;
375 
376 	/*
377 	 * Prefered mode tracking.
378 	 */
379 	unsigned pref_width;
380 	unsigned pref_height;
381 	bool pref_active;
382 	struct drm_display_mode *pref_mode;
383 
384 	/*
385 	 * Gui positioning
386 	 */
387 	int gui_x;
388 	int gui_y;
389 	bool is_implicit;
390 	int set_gui_x;
391 	int set_gui_y;
392 };
393 
394 struct vmw_validation_ctx {
395 	struct vmw_resource *res;
396 	struct vmw_buffer_object *buf;
397 };
398 
399 #define vmw_crtc_to_du(x) \
400 	container_of(x, struct vmw_display_unit, crtc)
401 #define vmw_connector_to_du(x) \
402 	container_of(x, struct vmw_display_unit, connector)
403 
404 
405 /*
406  * Shared display unit functions - vmwgfx_kms.c
407  */
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_du_connector_fill_modes(struct drm_connector *connector,
433 				uint32_t max_width, uint32_t max_height);
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 
443 void vmw_kms_helper_validation_finish(struct vmw_private *dev_priv,
444 				      struct drm_file *file_priv,
445 				      struct vmw_validation_context *ctx,
446 				      struct vmw_fence_obj **out_fence,
447 				      struct drm_vmw_fence_rep __user *
448 				      user_fence_rep);
449 int vmw_kms_readback(struct vmw_private *dev_priv,
450 		     struct drm_file *file_priv,
451 		     struct vmw_framebuffer *vfb,
452 		     struct drm_vmw_fence_rep __user *user_fence_rep,
453 		     struct drm_vmw_rect *vclips,
454 		     uint32_t num_clips);
455 struct vmw_framebuffer *
456 vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
457 			struct vmw_buffer_object *bo,
458 			struct vmw_surface *surface,
459 			bool only_2d,
460 			const struct drm_mode_fb_cmd2 *mode_cmd);
461 int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
462 			    unsigned unit,
463 			    u32 max_width,
464 			    u32 max_height,
465 			    struct drm_connector **p_con,
466 			    struct drm_crtc **p_crtc,
467 			    struct drm_display_mode **p_mode);
468 void vmw_guess_mode_timing(struct drm_display_mode *mode);
469 void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv);
470 void vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv);
471 
472 /* Universal Plane Helpers */
473 void vmw_du_primary_plane_destroy(struct drm_plane *plane);
474 void vmw_du_cursor_plane_destroy(struct drm_plane *plane);
475 int vmw_du_create_cursor_mob_array(struct vmw_cursor_plane *vcp);
476 void vmw_du_destroy_cursor_mob_array(struct vmw_cursor_plane *vcp);
477 
478 /* Atomic Helpers */
479 int vmw_du_primary_plane_atomic_check(struct drm_plane *plane,
480 				      struct drm_atomic_state *state);
481 int vmw_du_cursor_plane_atomic_check(struct drm_plane *plane,
482 				     struct drm_atomic_state *state);
483 void vmw_du_cursor_plane_atomic_update(struct drm_plane *plane,
484 				       struct drm_atomic_state *state);
485 int vmw_du_cursor_plane_prepare_fb(struct drm_plane *plane,
486 				   struct drm_plane_state *new_state);
487 void vmw_du_cursor_plane_cleanup_fb(struct drm_plane *plane,
488 			     struct drm_plane_state *old_state);
489 void vmw_du_plane_cleanup_fb(struct drm_plane *plane,
490 			     struct drm_plane_state *old_state);
491 void vmw_du_plane_reset(struct drm_plane *plane);
492 struct drm_plane_state *vmw_du_plane_duplicate_state(struct drm_plane *plane);
493 void vmw_du_plane_destroy_state(struct drm_plane *plane,
494 				struct drm_plane_state *state);
495 void vmw_du_plane_unpin_surf(struct vmw_plane_state *vps,
496 			     bool unreference);
497 
498 int vmw_du_crtc_atomic_check(struct drm_crtc *crtc,
499 			     struct drm_atomic_state *state);
500 void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc,
501 			      struct drm_atomic_state *state);
502 void vmw_du_crtc_atomic_flush(struct drm_crtc *crtc,
503 			      struct drm_atomic_state *state);
504 void vmw_du_crtc_reset(struct drm_crtc *crtc);
505 struct drm_crtc_state *vmw_du_crtc_duplicate_state(struct drm_crtc *crtc);
506 void vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
507 				struct drm_crtc_state *state);
508 void vmw_du_connector_reset(struct drm_connector *connector);
509 struct drm_connector_state *
510 vmw_du_connector_duplicate_state(struct drm_connector *connector);
511 
512 void vmw_du_connector_destroy_state(struct drm_connector *connector,
513 				    struct drm_connector_state *state);
514 
515 /*
516  * Legacy display unit functions - vmwgfx_ldu.c
517  */
518 int vmw_kms_ldu_init_display(struct vmw_private *dev_priv);
519 int vmw_kms_ldu_close_display(struct vmw_private *dev_priv);
520 int vmw_kms_ldu_do_bo_dirty(struct vmw_private *dev_priv,
521 			    struct vmw_framebuffer *framebuffer,
522 			    unsigned int flags, unsigned int color,
523 			    struct drm_clip_rect *clips,
524 			    unsigned int num_clips, int increment);
525 int vmw_kms_update_proxy(struct vmw_resource *res,
526 			 const struct drm_clip_rect *clips,
527 			 unsigned num_clips,
528 			 int increment);
529 
530 /*
531  * Screen Objects display functions - vmwgfx_scrn.c
532  */
533 int vmw_kms_sou_init_display(struct vmw_private *dev_priv);
534 int vmw_kms_sou_do_surface_dirty(struct vmw_private *dev_priv,
535 				 struct vmw_framebuffer *framebuffer,
536 				 struct drm_clip_rect *clips,
537 				 struct drm_vmw_rect *vclips,
538 				 struct vmw_resource *srf,
539 				 s32 dest_x,
540 				 s32 dest_y,
541 				 unsigned num_clips, int inc,
542 				 struct vmw_fence_obj **out_fence,
543 				 struct drm_crtc *crtc);
544 int vmw_kms_sou_do_bo_dirty(struct vmw_private *dev_priv,
545 			    struct vmw_framebuffer *framebuffer,
546 			    struct drm_clip_rect *clips,
547 			    struct drm_vmw_rect *vclips,
548 			    unsigned int num_clips, int increment,
549 			    bool interruptible,
550 			    struct vmw_fence_obj **out_fence,
551 			    struct drm_crtc *crtc);
552 int vmw_kms_sou_readback(struct vmw_private *dev_priv,
553 			 struct drm_file *file_priv,
554 			 struct vmw_framebuffer *vfb,
555 			 struct drm_vmw_fence_rep __user *user_fence_rep,
556 			 struct drm_vmw_rect *vclips,
557 			 uint32_t num_clips,
558 			 struct drm_crtc *crtc);
559 
560 /*
561  * Screen Target Display Unit functions - vmwgfx_stdu.c
562  */
563 int vmw_kms_stdu_init_display(struct vmw_private *dev_priv);
564 int vmw_kms_stdu_surface_dirty(struct vmw_private *dev_priv,
565 			       struct vmw_framebuffer *framebuffer,
566 			       struct drm_clip_rect *clips,
567 			       struct drm_vmw_rect *vclips,
568 			       struct vmw_resource *srf,
569 			       s32 dest_x,
570 			       s32 dest_y,
571 			       unsigned num_clips, int inc,
572 			       struct vmw_fence_obj **out_fence,
573 			       struct drm_crtc *crtc);
574 int vmw_kms_stdu_dma(struct vmw_private *dev_priv,
575 		     struct drm_file *file_priv,
576 		     struct vmw_framebuffer *vfb,
577 		     struct drm_vmw_fence_rep __user *user_fence_rep,
578 		     struct drm_clip_rect *clips,
579 		     struct drm_vmw_rect *vclips,
580 		     uint32_t num_clips,
581 		     int increment,
582 		     bool to_surface,
583 		     bool interruptible,
584 		     struct drm_crtc *crtc);
585 
586 int vmw_du_helper_plane_update(struct vmw_du_update_plane *update);
587 
588 /**
589  * vmw_du_translate_to_crtc - Translate a rect from framebuffer to crtc
590  * @state: Plane state.
591  * @r: Rectangle to translate.
592  */
593 static inline void vmw_du_translate_to_crtc(struct drm_plane_state *state,
594 					    struct drm_rect *r)
595 {
596 	int translate_crtc_x = -((state->src_x >> 16) - state->crtc_x);
597 	int translate_crtc_y = -((state->src_y >> 16) - state->crtc_y);
598 
599 	drm_rect_translate(r, translate_crtc_x, translate_crtc_y);
600 }
601 
602 #endif
603