1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
3  *
4  * Copyright 2009-2015 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/drmP.h>
32 #include <drm/drm_crtc_helper.h>
33 #include <drm/drm_encoder.h>
34 #include "vmwgfx_drv.h"
35 
36 
37 
38 /**
39  * struct vmw_kms_dirty - closure structure for the vmw_kms_helper_dirty
40  * function.
41  *
42  * @fifo_commit: Callback that is called once for each display unit after
43  * all clip rects. This function must commit the fifo space reserved by the
44  * helper. Set up by the caller.
45  * @clip: Callback that is called for each cliprect on each display unit.
46  * Set up by the caller.
47  * @fifo_reserve_size: Fifo size that the helper should try to allocat for
48  * each display unit. Set up by the caller.
49  * @dev_priv: Pointer to the device private. Set up by the helper.
50  * @unit: The current display unit. Set up by the helper before a call to @clip.
51  * @cmd: The allocated fifo space. Set up by the helper before the first @clip
52  * call.
53  * @crtc: The crtc for which to build dirty commands.
54  * @num_hits: Number of clip rect commands for this display unit.
55  * Cleared by the helper before the first @clip call. Updated by the @clip
56  * callback.
57  * @fb_x: Clip rect left side in framebuffer coordinates.
58  * @fb_y: Clip rect right side in framebuffer coordinates.
59  * @unit_x1: Clip rect left side in crtc coordinates.
60  * @unit_y1: Clip rect top side in crtc coordinates.
61  * @unit_x2: Clip rect right side in crtc coordinates.
62  * @unit_y2: Clip rect bottom side in crtc coordinates.
63  *
64  * The clip rect coordinates are updated by the helper for each @clip call.
65  * Note that this may be derived from if more info needs to be passed between
66  * helper caller and helper callbacks.
67  */
68 struct vmw_kms_dirty {
69 	void (*fifo_commit)(struct vmw_kms_dirty *);
70 	void (*clip)(struct vmw_kms_dirty *);
71 	size_t fifo_reserve_size;
72 	struct vmw_private *dev_priv;
73 	struct vmw_display_unit *unit;
74 	void *cmd;
75 	struct drm_crtc *crtc;
76 	u32 num_hits;
77 	s32 fb_x;
78 	s32 fb_y;
79 	s32 unit_x1;
80 	s32 unit_y1;
81 	s32 unit_x2;
82 	s32 unit_y2;
83 };
84 
85 #define VMWGFX_NUM_DISPLAY_UNITS 8
86 
87 
88 #define vmw_framebuffer_to_vfb(x) \
89 	container_of(x, struct vmw_framebuffer, base)
90 #define vmw_framebuffer_to_vfbs(x) \
91 	container_of(x, struct vmw_framebuffer_surface, base.base)
92 #define vmw_framebuffer_to_vfbd(x) \
93 	container_of(x, struct vmw_framebuffer_bo, base.base)
94 
95 /**
96  * Base class for framebuffers
97  *
98  * @pin is called the when ever a crtc uses this framebuffer
99  * @unpin is called
100  */
101 struct vmw_framebuffer {
102 	struct drm_framebuffer base;
103 	int (*pin)(struct vmw_framebuffer *fb);
104 	int (*unpin)(struct vmw_framebuffer *fb);
105 	bool bo;
106 	struct ttm_base_object *user_obj;
107 	uint32_t user_handle;
108 };
109 
110 /*
111  * Clip rectangle
112  */
113 struct vmw_clip_rect {
114 	int x1, x2, y1, y2;
115 };
116 
117 struct vmw_framebuffer_surface {
118 	struct vmw_framebuffer base;
119 	struct vmw_surface *surface;
120 	struct vmw_buffer_object *buffer;
121 	struct list_head head;
122 	bool is_bo_proxy;  /* true if this is proxy surface for DMA buf */
123 };
124 
125 
126 struct vmw_framebuffer_bo {
127 	struct vmw_framebuffer base;
128 	struct vmw_buffer_object *buffer;
129 };
130 
131 
132 static const uint32_t vmw_primary_plane_formats[] = {
133 	DRM_FORMAT_XRGB1555,
134 	DRM_FORMAT_RGB565,
135 	DRM_FORMAT_RGB888,
136 	DRM_FORMAT_XRGB8888,
137 	DRM_FORMAT_ARGB8888,
138 };
139 
140 static const uint32_t vmw_cursor_plane_formats[] = {
141 	DRM_FORMAT_ARGB8888,
142 };
143 
144 
145 #define vmw_crtc_state_to_vcs(x) container_of(x, struct vmw_crtc_state, base)
146 #define vmw_plane_state_to_vps(x) container_of(x, struct vmw_plane_state, base)
147 #define vmw_connector_state_to_vcs(x) \
148 		container_of(x, struct vmw_connector_state, base)
149 
150 /**
151  * Derived class for crtc state object
152  *
153  * @base DRM crtc object
154  */
155 struct vmw_crtc_state {
156 	struct drm_crtc_state base;
157 };
158 
159 /**
160  * Derived class for plane state object
161  *
162  * @base DRM plane object
163  * @surf Display surface for STDU
164  * @bo display bo for SOU
165  * @content_fb_type Used by STDU.
166  * @bo_size Size of the bo, used by Screen Object Display Unit
167  * @pinned pin count for STDU display surface
168  */
169 struct vmw_plane_state {
170 	struct drm_plane_state base;
171 	struct vmw_surface *surf;
172 	struct vmw_buffer_object *bo;
173 
174 	int content_fb_type;
175 	unsigned long bo_size;
176 
177 	int pinned;
178 
179 	/* For CPU Blit */
180 	unsigned int cpp;
181 };
182 
183 
184 /**
185  * Derived class for connector state object
186  *
187  * @base DRM connector object
188  * @is_implicit connector property
189  *
190  */
191 struct vmw_connector_state {
192 	struct drm_connector_state base;
193 
194 	bool is_implicit;
195 
196 	/**
197 	 * @gui_x:
198 	 *
199 	 * vmwgfx connector property representing the x position of this display
200 	 * unit (connector is synonymous to display unit) in overall topology.
201 	 * This is what the device expect as xRoot while creating screen.
202 	 */
203 	int gui_x;
204 
205 	/**
206 	 * @gui_y:
207 	 *
208 	 * vmwgfx connector property representing the y position of this display
209 	 * unit (connector is synonymous to display unit) in overall topology.
210 	 * This is what the device expect as yRoot while creating screen.
211 	 */
212 	int gui_y;
213 };
214 
215 /**
216  * Base class display unit.
217  *
218  * Since the SVGA hw doesn't have a concept of a crtc, encoder or connector
219  * so the display unit is all of them at the same time. This is true for both
220  * legacy multimon and screen objects.
221  */
222 struct vmw_display_unit {
223 	struct drm_crtc crtc;
224 	struct drm_encoder encoder;
225 	struct drm_connector connector;
226 	struct drm_plane primary;
227 	struct drm_plane cursor;
228 
229 	struct vmw_surface *cursor_surface;
230 	struct vmw_buffer_object *cursor_bo;
231 	size_t cursor_age;
232 
233 	int cursor_x;
234 	int cursor_y;
235 
236 	int hotspot_x;
237 	int hotspot_y;
238 	s32 core_hotspot_x;
239 	s32 core_hotspot_y;
240 
241 	unsigned unit;
242 
243 	/*
244 	 * Prefered mode tracking.
245 	 */
246 	unsigned pref_width;
247 	unsigned pref_height;
248 	bool pref_active;
249 	struct drm_display_mode *pref_mode;
250 
251 	/*
252 	 * Gui positioning
253 	 */
254 	int gui_x;
255 	int gui_y;
256 	bool is_implicit;
257 	bool active_implicit;
258 	int set_gui_x;
259 	int set_gui_y;
260 };
261 
262 struct vmw_validation_ctx {
263 	struct vmw_resource *res;
264 	struct vmw_buffer_object *buf;
265 };
266 
267 #define vmw_crtc_to_du(x) \
268 	container_of(x, struct vmw_display_unit, crtc)
269 #define vmw_connector_to_du(x) \
270 	container_of(x, struct vmw_display_unit, connector)
271 
272 
273 /*
274  * Shared display unit functions - vmwgfx_kms.c
275  */
276 void vmw_du_cleanup(struct vmw_display_unit *du);
277 void vmw_du_crtc_save(struct drm_crtc *crtc);
278 void vmw_du_crtc_restore(struct drm_crtc *crtc);
279 int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
280 			   u16 *r, u16 *g, u16 *b,
281 			   uint32_t size,
282 			   struct drm_modeset_acquire_ctx *ctx);
283 int vmw_du_connector_set_property(struct drm_connector *connector,
284 				  struct drm_property *property,
285 				  uint64_t val);
286 int vmw_du_connector_atomic_set_property(struct drm_connector *connector,
287 					 struct drm_connector_state *state,
288 					 struct drm_property *property,
289 					 uint64_t val);
290 int
291 vmw_du_connector_atomic_get_property(struct drm_connector *connector,
292 				     const struct drm_connector_state *state,
293 				     struct drm_property *property,
294 				     uint64_t *val);
295 int vmw_du_connector_dpms(struct drm_connector *connector, int mode);
296 void vmw_du_connector_save(struct drm_connector *connector);
297 void vmw_du_connector_restore(struct drm_connector *connector);
298 enum drm_connector_status
299 vmw_du_connector_detect(struct drm_connector *connector, bool force);
300 int vmw_du_connector_fill_modes(struct drm_connector *connector,
301 				uint32_t max_width, uint32_t max_height);
302 int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
303 			 struct vmw_framebuffer *framebuffer,
304 			 const struct drm_clip_rect *clips,
305 			 const struct drm_vmw_rect *vclips,
306 			 s32 dest_x, s32 dest_y,
307 			 int num_clips,
308 			 int increment,
309 			 struct vmw_kms_dirty *dirty);
310 
311 void vmw_kms_helper_validation_finish(struct vmw_private *dev_priv,
312 				      struct drm_file *file_priv,
313 				      struct vmw_validation_context *ctx,
314 				      struct vmw_fence_obj **out_fence,
315 				      struct drm_vmw_fence_rep __user *
316 				      user_fence_rep);
317 int vmw_kms_readback(struct vmw_private *dev_priv,
318 		     struct drm_file *file_priv,
319 		     struct vmw_framebuffer *vfb,
320 		     struct drm_vmw_fence_rep __user *user_fence_rep,
321 		     struct drm_vmw_rect *vclips,
322 		     uint32_t num_clips);
323 struct vmw_framebuffer *
324 vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
325 			struct vmw_buffer_object *bo,
326 			struct vmw_surface *surface,
327 			bool only_2d,
328 			const struct drm_mode_fb_cmd2 *mode_cmd);
329 int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
330 			    unsigned unit,
331 			    u32 max_width,
332 			    u32 max_height,
333 			    struct drm_connector **p_con,
334 			    struct drm_crtc **p_crtc,
335 			    struct drm_display_mode **p_mode);
336 void vmw_guess_mode_timing(struct drm_display_mode *mode);
337 void vmw_kms_del_active(struct vmw_private *dev_priv,
338 			struct vmw_display_unit *du);
339 void vmw_kms_add_active(struct vmw_private *dev_priv,
340 			struct vmw_display_unit *du,
341 			struct vmw_framebuffer *vfb);
342 bool vmw_kms_crtc_flippable(struct vmw_private *dev_priv,
343 			    struct drm_crtc *crtc);
344 void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv,
345 				struct drm_crtc *crtc);
346 void vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv,
347 						bool immutable);
348 
349 /* Universal Plane Helpers */
350 void vmw_du_primary_plane_destroy(struct drm_plane *plane);
351 void vmw_du_cursor_plane_destroy(struct drm_plane *plane);
352 
353 /* Atomic Helpers */
354 int vmw_du_primary_plane_atomic_check(struct drm_plane *plane,
355 				      struct drm_plane_state *state);
356 int vmw_du_cursor_plane_atomic_check(struct drm_plane *plane,
357 				     struct drm_plane_state *state);
358 void vmw_du_cursor_plane_atomic_update(struct drm_plane *plane,
359 				       struct drm_plane_state *old_state);
360 int vmw_du_cursor_plane_prepare_fb(struct drm_plane *plane,
361 				   struct drm_plane_state *new_state);
362 void vmw_du_plane_cleanup_fb(struct drm_plane *plane,
363 			     struct drm_plane_state *old_state);
364 void vmw_du_plane_reset(struct drm_plane *plane);
365 struct drm_plane_state *vmw_du_plane_duplicate_state(struct drm_plane *plane);
366 void vmw_du_plane_destroy_state(struct drm_plane *plane,
367 				struct drm_plane_state *state);
368 void vmw_du_plane_unpin_surf(struct vmw_plane_state *vps,
369 			     bool unreference);
370 
371 int vmw_du_crtc_atomic_check(struct drm_crtc *crtc,
372 			     struct drm_crtc_state *state);
373 void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc,
374 			      struct drm_crtc_state *old_crtc_state);
375 void vmw_du_crtc_atomic_flush(struct drm_crtc *crtc,
376 			      struct drm_crtc_state *old_crtc_state);
377 void vmw_du_crtc_reset(struct drm_crtc *crtc);
378 struct drm_crtc_state *vmw_du_crtc_duplicate_state(struct drm_crtc *crtc);
379 void vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
380 				struct drm_crtc_state *state);
381 void vmw_du_connector_reset(struct drm_connector *connector);
382 struct drm_connector_state *
383 vmw_du_connector_duplicate_state(struct drm_connector *connector);
384 
385 void vmw_du_connector_destroy_state(struct drm_connector *connector,
386 				    struct drm_connector_state *state);
387 
388 /*
389  * Legacy display unit functions - vmwgfx_ldu.c
390  */
391 int vmw_kms_ldu_init_display(struct vmw_private *dev_priv);
392 int vmw_kms_ldu_close_display(struct vmw_private *dev_priv);
393 int vmw_kms_ldu_do_bo_dirty(struct vmw_private *dev_priv,
394 			    struct vmw_framebuffer *framebuffer,
395 			    unsigned int flags, unsigned int color,
396 			    struct drm_clip_rect *clips,
397 			    unsigned int num_clips, int increment);
398 int vmw_kms_update_proxy(struct vmw_resource *res,
399 			 const struct drm_clip_rect *clips,
400 			 unsigned num_clips,
401 			 int increment);
402 
403 /*
404  * Screen Objects display functions - vmwgfx_scrn.c
405  */
406 int vmw_kms_sou_init_display(struct vmw_private *dev_priv);
407 int vmw_kms_sou_do_surface_dirty(struct vmw_private *dev_priv,
408 				 struct vmw_framebuffer *framebuffer,
409 				 struct drm_clip_rect *clips,
410 				 struct drm_vmw_rect *vclips,
411 				 struct vmw_resource *srf,
412 				 s32 dest_x,
413 				 s32 dest_y,
414 				 unsigned num_clips, int inc,
415 				 struct vmw_fence_obj **out_fence,
416 				 struct drm_crtc *crtc);
417 int vmw_kms_sou_do_bo_dirty(struct vmw_private *dev_priv,
418 			    struct vmw_framebuffer *framebuffer,
419 			    struct drm_clip_rect *clips,
420 			    struct drm_vmw_rect *vclips,
421 			    unsigned int num_clips, int increment,
422 			    bool interruptible,
423 			    struct vmw_fence_obj **out_fence,
424 			    struct drm_crtc *crtc);
425 int vmw_kms_sou_readback(struct vmw_private *dev_priv,
426 			 struct drm_file *file_priv,
427 			 struct vmw_framebuffer *vfb,
428 			 struct drm_vmw_fence_rep __user *user_fence_rep,
429 			 struct drm_vmw_rect *vclips,
430 			 uint32_t num_clips,
431 			 struct drm_crtc *crtc);
432 
433 /*
434  * Screen Target Display Unit functions - vmwgfx_stdu.c
435  */
436 int vmw_kms_stdu_init_display(struct vmw_private *dev_priv);
437 int vmw_kms_stdu_surface_dirty(struct vmw_private *dev_priv,
438 			       struct vmw_framebuffer *framebuffer,
439 			       struct drm_clip_rect *clips,
440 			       struct drm_vmw_rect *vclips,
441 			       struct vmw_resource *srf,
442 			       s32 dest_x,
443 			       s32 dest_y,
444 			       unsigned num_clips, int inc,
445 			       struct vmw_fence_obj **out_fence,
446 			       struct drm_crtc *crtc);
447 int vmw_kms_stdu_dma(struct vmw_private *dev_priv,
448 		     struct drm_file *file_priv,
449 		     struct vmw_framebuffer *vfb,
450 		     struct drm_vmw_fence_rep __user *user_fence_rep,
451 		     struct drm_clip_rect *clips,
452 		     struct drm_vmw_rect *vclips,
453 		     uint32_t num_clips,
454 		     int increment,
455 		     bool to_surface,
456 		     bool interruptible,
457 		     struct drm_crtc *crtc);
458 
459 int vmw_kms_set_config(struct drm_mode_set *set,
460 		       struct drm_modeset_acquire_ctx *ctx);
461 #endif
462