1 /*	$NetBSD: vmwgfx_kms.h,v 1.3 2021/12/18 23:45:45 riastradh Exp $	*/
2 
3 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
4 /**************************************************************************
5  *
6  * Copyright 2009-2015 VMware, Inc., Palo Alto, CA., USA
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sub license, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the
17  * next paragraph) shall be included in all copies or substantial portions
18  * of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
23  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
24  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
26  * USE OR OTHER DEALINGS IN THE SOFTWARE.
27  *
28  **************************************************************************/
29 
30 #ifndef VMWGFX_KMS_H_
31 #define VMWGFX_KMS_H_
32 
33 #include <drm/drm_encoder.h>
34 #include <drm/drm_probe_helper.h>
35 
36 #include "vmwgfx_drv.h"
37 
38 /**
39  * struct vmw_du_update_plane - Closure structure for vmw_du_helper_plane_update
40  * @plane: Plane which is being updated.
41  * @old_state: Old state of plane.
42  * @dev_priv: Device private.
43  * @du: Display unit on which to update the plane.
44  * @vfb: Framebuffer which is blitted to display unit.
45  * @out_fence: Out fence for resource finish.
46  * @mutex: The mutex used to protect resource reservation.
47  * @cpu_blit: True if need cpu blit.
48  * @intr: Whether to perform waits interruptible if possible.
49  *
50  * This structure loosely represent the set of operations needed to perform a
51  * plane update on a display unit. Implementer will define that functionality
52  * according to the function callbacks for this structure. In brief it involves
53  * surface/buffer object validation, populate FIFO commands and command
54  * submission to the device.
55  */
56 struct vmw_du_update_plane {
57 	/**
58 	 * @calc_fifo_size: Calculate fifo size.
59 	 *
60 	 * Determine fifo size for the commands needed for update. The number of
61 	 * damage clips on display unit @num_hits will be passed to allocate
62 	 * sufficient fifo space.
63 	 *
64 	 * Return: Fifo size needed
65 	 */
66 	uint32_t (*calc_fifo_size)(struct vmw_du_update_plane *update,
67 				   uint32_t num_hits);
68 
69 	/**
70 	 * @post_prepare: Populate fifo for resource preparation.
71 	 *
72 	 * Some surface resource or buffer object need some extra cmd submission
73 	 * like update GB image for proxy surface and define a GMRFB for screen
74 	 * object. That should should be done here as this callback will be
75 	 * called after FIFO allocation with the address of command buufer.
76 	 *
77 	 * This callback is optional.
78 	 *
79 	 * Return: Size of commands populated to command buffer.
80 	 */
81 	uint32_t (*post_prepare)(struct vmw_du_update_plane *update, void *cmd);
82 
83 	/**
84 	 * @pre_clip: Populate fifo before clip.
85 	 *
86 	 * This is where pre clip related command should be populated like
87 	 * surface copy/DMA, etc.
88 	 *
89 	 * This callback is optional.
90 	 *
91 	 * Return: Size of commands populated to command buffer.
92 	 */
93 	uint32_t (*pre_clip)(struct vmw_du_update_plane *update, void *cmd,
94 			     uint32_t num_hits);
95 
96 	/**
97 	 * @clip: Populate fifo for clip.
98 	 *
99 	 * This is where to populate clips for surface copy/dma or blit commands
100 	 * if needed. This will be called times have damage in display unit,
101 	 * which is one if doing full update. @clip is the damage in destination
102 	 * coordinates which is crtc/DU and @src_x, @src_y is damage clip src in
103 	 * framebuffer coordinate.
104 	 *
105 	 * This callback is optional.
106 	 *
107 	 * Return: Size of commands populated to command buffer.
108 	 */
109 	uint32_t (*clip)(struct vmw_du_update_plane *update, void *cmd,
110 			 struct drm_rect *clip, uint32_t src_x, uint32_t src_y);
111 
112 	/**
113 	 * @post_clip: Populate fifo after clip.
114 	 *
115 	 * This is where to populate display unit update commands or blit
116 	 * commands.
117 	 *
118 	 * Return: Size of commands populated to command buffer.
119 	 */
120 	uint32_t (*post_clip)(struct vmw_du_update_plane *update, void *cmd,
121 				    struct drm_rect *bb);
122 
123 	struct drm_plane *plane;
124 	struct drm_plane_state *old_state;
125 	struct vmw_private *dev_priv;
126 	struct vmw_display_unit *du;
127 	struct vmw_framebuffer *vfb;
128 	struct vmw_fence_obj **out_fence;
129 	struct mutex *mutex;
130 	bool cpu_blit;
131 	bool intr;
132 };
133 
134 /**
135  * struct vmw_du_update_plane_surface - closure structure for surface
136  * @base: base closure structure.
137  * @cmd_start: FIFO command start address (used by SOU only).
138  */
139 struct vmw_du_update_plane_surface {
140 	struct vmw_du_update_plane base;
141 	/* This member is to handle special case SOU surface update */
142 	void *cmd_start;
143 };
144 
145 /**
146  * struct vmw_du_update_plane_buffer - Closure structure for buffer object
147  * @base: Base closure structure.
148  * @fb_left: x1 for fb damage bounding box.
149  * @fb_top: y1 for fb damage bounding box.
150  */
151 struct vmw_du_update_plane_buffer {
152 	struct vmw_du_update_plane base;
153 	int fb_left, fb_top;
154 };
155 
156 /**
157  * struct vmw_kms_dirty - closure structure for the vmw_kms_helper_dirty
158  * function.
159  *
160  * @fifo_commit: Callback that is called once for each display unit after
161  * all clip rects. This function must commit the fifo space reserved by the
162  * helper. Set up by the caller.
163  * @clip: Callback that is called for each cliprect on each display unit.
164  * Set up by the caller.
165  * @fifo_reserve_size: Fifo size that the helper should try to allocat for
166  * each display unit. Set up by the caller.
167  * @dev_priv: Pointer to the device private. Set up by the helper.
168  * @unit: The current display unit. Set up by the helper before a call to @clip.
169  * @cmd: The allocated fifo space. Set up by the helper before the first @clip
170  * call.
171  * @crtc: The crtc for which to build dirty commands.
172  * @num_hits: Number of clip rect commands for this display unit.
173  * Cleared by the helper before the first @clip call. Updated by the @clip
174  * callback.
175  * @fb_x: Clip rect left side in framebuffer coordinates.
176  * @fb_y: Clip rect right side in framebuffer coordinates.
177  * @unit_x1: Clip rect left side in crtc coordinates.
178  * @unit_y1: Clip rect top side in crtc coordinates.
179  * @unit_x2: Clip rect right side in crtc coordinates.
180  * @unit_y2: Clip rect bottom side in crtc coordinates.
181  *
182  * The clip rect coordinates are updated by the helper for each @clip call.
183  * Note that this may be derived from if more info needs to be passed between
184  * helper caller and helper callbacks.
185  */
186 struct vmw_kms_dirty {
187 	void (*fifo_commit)(struct vmw_kms_dirty *);
188 	void (*clip)(struct vmw_kms_dirty *);
189 	size_t fifo_reserve_size;
190 	struct vmw_private *dev_priv;
191 	struct vmw_display_unit *unit;
192 	void *cmd;
193 	struct drm_crtc *crtc;
194 	u32 num_hits;
195 	s32 fb_x;
196 	s32 fb_y;
197 	s32 unit_x1;
198 	s32 unit_y1;
199 	s32 unit_x2;
200 	s32 unit_y2;
201 };
202 
203 #define VMWGFX_NUM_DISPLAY_UNITS 8
204 
205 
206 #define vmw_framebuffer_to_vfb(x) \
207 	container_of(x, struct vmw_framebuffer, base)
208 #define vmw_framebuffer_to_vfbs(x) \
209 	container_of(x, struct vmw_framebuffer_surface, base.base)
210 #define vmw_framebuffer_to_vfbd(x) \
211 	container_of(x, struct vmw_framebuffer_bo, base.base)
212 
213 /**
214  * Base class for framebuffers
215  *
216  * @pin is called the when ever a crtc uses this framebuffer
217  * @unpin is called
218  */
219 struct vmw_framebuffer {
220 	struct drm_framebuffer base;
221 	int (*pin)(struct vmw_framebuffer *fb);
222 	int (*unpin)(struct vmw_framebuffer *fb);
223 	bool bo;
224 	struct ttm_base_object *user_obj;
225 	uint32_t user_handle;
226 };
227 
228 /*
229  * Clip rectangle
230  */
231 struct vmw_clip_rect {
232 	int x1, x2, y1, y2;
233 };
234 
235 struct vmw_framebuffer_surface {
236 	struct vmw_framebuffer base;
237 	struct vmw_surface *surface;
238 	struct vmw_buffer_object *buffer;
239 	struct list_head head;
240 	bool is_bo_proxy;  /* true if this is proxy surface for DMA buf */
241 };
242 
243 
244 struct vmw_framebuffer_bo {
245 	struct vmw_framebuffer base;
246 	struct vmw_buffer_object *buffer;
247 };
248 
249 
250 static const uint32_t vmw_primary_plane_formats[] = {
251 	DRM_FORMAT_XRGB1555,
252 	DRM_FORMAT_RGB565,
253 	DRM_FORMAT_RGB888,
254 	DRM_FORMAT_XRGB8888,
255 	DRM_FORMAT_ARGB8888,
256 };
257 
258 static const uint32_t vmw_cursor_plane_formats[] = {
259 	DRM_FORMAT_ARGB8888,
260 };
261 
262 
263 #define vmw_crtc_state_to_vcs(x) container_of(x, struct vmw_crtc_state, base)
264 #define vmw_plane_state_to_vps(x) container_of(x, struct vmw_plane_state, base)
265 #define vmw_connector_state_to_vcs(x) \
266 		container_of(x, struct vmw_connector_state, base)
267 
268 /**
269  * Derived class for crtc state object
270  *
271  * @base DRM crtc object
272  */
273 struct vmw_crtc_state {
274 	struct drm_crtc_state base;
275 };
276 
277 /**
278  * Derived class for plane state object
279  *
280  * @base DRM plane object
281  * @surf Display surface for STDU
282  * @bo display bo for SOU
283  * @content_fb_type Used by STDU.
284  * @bo_size Size of the bo, used by Screen Object Display Unit
285  * @pinned pin count for STDU display surface
286  */
287 struct vmw_plane_state {
288 	struct drm_plane_state base;
289 	struct vmw_surface *surf;
290 	struct vmw_buffer_object *bo;
291 
292 	int content_fb_type;
293 	unsigned long bo_size;
294 
295 	int pinned;
296 
297 	/* For CPU Blit */
298 	unsigned int cpp;
299 };
300 
301 
302 /**
303  * Derived class for connector state object
304  *
305  * @base DRM connector object
306  * @is_implicit connector property
307  *
308  */
309 struct vmw_connector_state {
310 	struct drm_connector_state base;
311 
312 	/**
313 	 * @gui_x:
314 	 *
315 	 * vmwgfx connector property representing the x position of this display
316 	 * unit (connector is synonymous to display unit) in overall topology.
317 	 * This is what the device expect as xRoot while creating screen.
318 	 */
319 	int gui_x;
320 
321 	/**
322 	 * @gui_y:
323 	 *
324 	 * vmwgfx connector property representing the y position of this display
325 	 * unit (connector is synonymous to display unit) in overall topology.
326 	 * This is what the device expect as yRoot while creating screen.
327 	 */
328 	int gui_y;
329 };
330 
331 /**
332  * Base class display unit.
333  *
334  * Since the SVGA hw doesn't have a concept of a crtc, encoder or connector
335  * so the display unit is all of them at the same time. This is true for both
336  * legacy multimon and screen objects.
337  */
338 struct vmw_display_unit {
339 	struct drm_crtc crtc;
340 	struct drm_encoder encoder;
341 	struct drm_connector connector;
342 	struct drm_plane primary;
343 	struct drm_plane cursor;
344 
345 	struct vmw_surface *cursor_surface;
346 	struct vmw_buffer_object *cursor_bo;
347 	size_t cursor_age;
348 
349 	int cursor_x;
350 	int cursor_y;
351 
352 	int hotspot_x;
353 	int hotspot_y;
354 	s32 core_hotspot_x;
355 	s32 core_hotspot_y;
356 
357 	unsigned unit;
358 
359 	/*
360 	 * Prefered mode tracking.
361 	 */
362 	unsigned pref_width;
363 	unsigned pref_height;
364 	bool pref_active;
365 	struct drm_display_mode *pref_mode;
366 
367 	/*
368 	 * Gui positioning
369 	 */
370 	int gui_x;
371 	int gui_y;
372 	bool is_implicit;
373 	int set_gui_x;
374 	int set_gui_y;
375 };
376 
377 struct vmw_validation_ctx {
378 	struct vmw_resource *res;
379 	struct vmw_buffer_object *buf;
380 };
381 
382 #define vmw_crtc_to_du(x) \
383 	container_of(x, struct vmw_display_unit, crtc)
384 #define vmw_connector_to_du(x) \
385 	container_of(x, struct vmw_display_unit, connector)
386 
387 
388 /*
389  * Shared display unit functions - vmwgfx_kms.c
390  */
391 void vmw_du_cleanup(struct vmw_display_unit *du);
392 void vmw_du_crtc_save(struct drm_crtc *crtc);
393 void vmw_du_crtc_restore(struct drm_crtc *crtc);
394 int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
395 			   u16 *r, u16 *g, u16 *b,
396 			   uint32_t size,
397 			   struct drm_modeset_acquire_ctx *ctx);
398 int vmw_du_connector_set_property(struct drm_connector *connector,
399 				  struct drm_property *property,
400 				  uint64_t val);
401 int vmw_du_connector_atomic_set_property(struct drm_connector *connector,
402 					 struct drm_connector_state *state,
403 					 struct drm_property *property,
404 					 uint64_t val);
405 int
406 vmw_du_connector_atomic_get_property(struct drm_connector *connector,
407 				     const struct drm_connector_state *state,
408 				     struct drm_property *property,
409 				     uint64_t *val);
410 int vmw_du_connector_dpms(struct drm_connector *connector, int mode);
411 void vmw_du_connector_save(struct drm_connector *connector);
412 void vmw_du_connector_restore(struct drm_connector *connector);
413 enum drm_connector_status
414 vmw_du_connector_detect(struct drm_connector *connector, bool force);
415 int vmw_du_connector_fill_modes(struct drm_connector *connector,
416 				uint32_t max_width, uint32_t max_height);
417 int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
418 			 struct vmw_framebuffer *framebuffer,
419 			 const struct drm_clip_rect *clips,
420 			 const struct drm_vmw_rect *vclips,
421 			 s32 dest_x, s32 dest_y,
422 			 int num_clips,
423 			 int increment,
424 			 struct vmw_kms_dirty *dirty);
425 
426 void vmw_kms_helper_validation_finish(struct vmw_private *dev_priv,
427 				      struct drm_file *file_priv,
428 				      struct vmw_validation_context *ctx,
429 				      struct vmw_fence_obj **out_fence,
430 				      struct drm_vmw_fence_rep __user *
431 				      user_fence_rep);
432 int vmw_kms_readback(struct vmw_private *dev_priv,
433 		     struct drm_file *file_priv,
434 		     struct vmw_framebuffer *vfb,
435 		     struct drm_vmw_fence_rep __user *user_fence_rep,
436 		     struct drm_vmw_rect *vclips,
437 		     uint32_t num_clips);
438 struct vmw_framebuffer *
439 vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
440 			struct vmw_buffer_object *bo,
441 			struct vmw_surface *surface,
442 			bool only_2d,
443 			const struct drm_mode_fb_cmd2 *mode_cmd);
444 int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
445 			    unsigned unit,
446 			    u32 max_width,
447 			    u32 max_height,
448 			    struct drm_connector **p_con,
449 			    struct drm_crtc **p_crtc,
450 			    struct drm_display_mode **p_mode);
451 void vmw_guess_mode_timing(struct drm_display_mode *mode);
452 void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv);
453 void vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv);
454 
455 /* Universal Plane Helpers */
456 void vmw_du_primary_plane_destroy(struct drm_plane *plane);
457 void vmw_du_cursor_plane_destroy(struct drm_plane *plane);
458 
459 /* Atomic Helpers */
460 int vmw_du_primary_plane_atomic_check(struct drm_plane *plane,
461 				      struct drm_plane_state *state);
462 int vmw_du_cursor_plane_atomic_check(struct drm_plane *plane,
463 				     struct drm_plane_state *state);
464 void vmw_du_cursor_plane_atomic_update(struct drm_plane *plane,
465 				       struct drm_plane_state *old_state);
466 int vmw_du_cursor_plane_prepare_fb(struct drm_plane *plane,
467 				   struct drm_plane_state *new_state);
468 void vmw_du_plane_cleanup_fb(struct drm_plane *plane,
469 			     struct drm_plane_state *old_state);
470 void vmw_du_plane_reset(struct drm_plane *plane);
471 struct drm_plane_state *vmw_du_plane_duplicate_state(struct drm_plane *plane);
472 void vmw_du_plane_destroy_state(struct drm_plane *plane,
473 				struct drm_plane_state *state);
474 void vmw_du_plane_unpin_surf(struct vmw_plane_state *vps,
475 			     bool unreference);
476 
477 int vmw_du_crtc_atomic_check(struct drm_crtc *crtc,
478 			     struct drm_crtc_state *state);
479 void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc,
480 			      struct drm_crtc_state *old_crtc_state);
481 void vmw_du_crtc_atomic_flush(struct drm_crtc *crtc,
482 			      struct drm_crtc_state *old_crtc_state);
483 void vmw_du_crtc_reset(struct drm_crtc *crtc);
484 struct drm_crtc_state *vmw_du_crtc_duplicate_state(struct drm_crtc *crtc);
485 void vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
486 				struct drm_crtc_state *state);
487 void vmw_du_connector_reset(struct drm_connector *connector);
488 struct drm_connector_state *
489 vmw_du_connector_duplicate_state(struct drm_connector *connector);
490 
491 void vmw_du_connector_destroy_state(struct drm_connector *connector,
492 				    struct drm_connector_state *state);
493 
494 /*
495  * Legacy display unit functions - vmwgfx_ldu.c
496  */
497 int vmw_kms_ldu_init_display(struct vmw_private *dev_priv);
498 int vmw_kms_ldu_close_display(struct vmw_private *dev_priv);
499 int vmw_kms_ldu_do_bo_dirty(struct vmw_private *dev_priv,
500 			    struct vmw_framebuffer *framebuffer,
501 			    unsigned int flags, unsigned int color,
502 			    struct drm_clip_rect *clips,
503 			    unsigned int num_clips, int increment);
504 int vmw_kms_update_proxy(struct vmw_resource *res,
505 			 const struct drm_clip_rect *clips,
506 			 unsigned num_clips,
507 			 int increment);
508 
509 /*
510  * Screen Objects display functions - vmwgfx_scrn.c
511  */
512 int vmw_kms_sou_init_display(struct vmw_private *dev_priv);
513 int vmw_kms_sou_do_surface_dirty(struct vmw_private *dev_priv,
514 				 struct vmw_framebuffer *framebuffer,
515 				 struct drm_clip_rect *clips,
516 				 struct drm_vmw_rect *vclips,
517 				 struct vmw_resource *srf,
518 				 s32 dest_x,
519 				 s32 dest_y,
520 				 unsigned num_clips, int inc,
521 				 struct vmw_fence_obj **out_fence,
522 				 struct drm_crtc *crtc);
523 int vmw_kms_sou_do_bo_dirty(struct vmw_private *dev_priv,
524 			    struct vmw_framebuffer *framebuffer,
525 			    struct drm_clip_rect *clips,
526 			    struct drm_vmw_rect *vclips,
527 			    unsigned int num_clips, int increment,
528 			    bool interruptible,
529 			    struct vmw_fence_obj **out_fence,
530 			    struct drm_crtc *crtc);
531 int vmw_kms_sou_readback(struct vmw_private *dev_priv,
532 			 struct drm_file *file_priv,
533 			 struct vmw_framebuffer *vfb,
534 			 struct drm_vmw_fence_rep __user *user_fence_rep,
535 			 struct drm_vmw_rect *vclips,
536 			 uint32_t num_clips,
537 			 struct drm_crtc *crtc);
538 
539 /*
540  * Screen Target Display Unit functions - vmwgfx_stdu.c
541  */
542 int vmw_kms_stdu_init_display(struct vmw_private *dev_priv);
543 int vmw_kms_stdu_surface_dirty(struct vmw_private *dev_priv,
544 			       struct vmw_framebuffer *framebuffer,
545 			       struct drm_clip_rect *clips,
546 			       struct drm_vmw_rect *vclips,
547 			       struct vmw_resource *srf,
548 			       s32 dest_x,
549 			       s32 dest_y,
550 			       unsigned num_clips, int inc,
551 			       struct vmw_fence_obj **out_fence,
552 			       struct drm_crtc *crtc);
553 int vmw_kms_stdu_dma(struct vmw_private *dev_priv,
554 		     struct drm_file *file_priv,
555 		     struct vmw_framebuffer *vfb,
556 		     struct drm_vmw_fence_rep __user *user_fence_rep,
557 		     struct drm_clip_rect *clips,
558 		     struct drm_vmw_rect *vclips,
559 		     uint32_t num_clips,
560 		     int increment,
561 		     bool to_surface,
562 		     bool interruptible,
563 		     struct drm_crtc *crtc);
564 
565 int vmw_du_helper_plane_update(struct vmw_du_update_plane *update);
566 
567 /**
568  * vmw_du_translate_to_crtc - Translate a rect from framebuffer to crtc
569  * @state: Plane state.
570  * @r: Rectangle to translate.
571  */
vmw_du_translate_to_crtc(struct drm_plane_state * state,struct drm_rect * r)572 static inline void vmw_du_translate_to_crtc(struct drm_plane_state *state,
573 					    struct drm_rect *r)
574 {
575 	int translate_crtc_x = -((state->src_x >> 16) - state->crtc_x);
576 	int translate_crtc_y = -((state->src_y >> 16) - state->crtc_y);
577 
578 	drm_rect_translate(r, translate_crtc_x, translate_crtc_y);
579 }
580 
581 #endif
582