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