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 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_update_proxy(struct vmw_resource *res,
521 const struct drm_clip_rect *clips,
522 unsigned num_clips,
523 int increment);
524
525 /*
526 * Screen Objects display functions - vmwgfx_scrn.c
527 */
528 int vmw_kms_sou_init_display(struct vmw_private *dev_priv);
529 int vmw_kms_sou_do_surface_dirty(struct vmw_private *dev_priv,
530 struct vmw_framebuffer *framebuffer,
531 struct drm_clip_rect *clips,
532 struct drm_vmw_rect *vclips,
533 struct vmw_resource *srf,
534 s32 dest_x,
535 s32 dest_y,
536 unsigned num_clips, int inc,
537 struct vmw_fence_obj **out_fence,
538 struct drm_crtc *crtc);
539 int vmw_kms_sou_do_bo_dirty(struct vmw_private *dev_priv,
540 struct vmw_framebuffer *framebuffer,
541 struct drm_clip_rect *clips,
542 struct drm_vmw_rect *vclips,
543 unsigned int num_clips, int increment,
544 bool interruptible,
545 struct vmw_fence_obj **out_fence,
546 struct drm_crtc *crtc);
547 int vmw_kms_sou_readback(struct vmw_private *dev_priv,
548 struct drm_file *file_priv,
549 struct vmw_framebuffer *vfb,
550 struct drm_vmw_fence_rep __user *user_fence_rep,
551 struct drm_vmw_rect *vclips,
552 uint32_t num_clips,
553 struct drm_crtc *crtc);
554
555 /*
556 * Screen Target Display Unit functions - vmwgfx_stdu.c
557 */
558 int vmw_kms_stdu_init_display(struct vmw_private *dev_priv);
559 int vmw_kms_stdu_surface_dirty(struct vmw_private *dev_priv,
560 struct vmw_framebuffer *framebuffer,
561 struct drm_clip_rect *clips,
562 struct drm_vmw_rect *vclips,
563 struct vmw_resource *srf,
564 s32 dest_x,
565 s32 dest_y,
566 unsigned num_clips, int inc,
567 struct vmw_fence_obj **out_fence,
568 struct drm_crtc *crtc);
569 int vmw_kms_stdu_dma(struct vmw_private *dev_priv,
570 struct drm_file *file_priv,
571 struct vmw_framebuffer *vfb,
572 struct drm_vmw_fence_rep __user *user_fence_rep,
573 struct drm_clip_rect *clips,
574 struct drm_vmw_rect *vclips,
575 uint32_t num_clips,
576 int increment,
577 bool to_surface,
578 bool interruptible,
579 struct drm_crtc *crtc);
580
581 int vmw_du_helper_plane_update(struct vmw_du_update_plane *update);
582
583 /**
584 * vmw_du_translate_to_crtc - Translate a rect from framebuffer to crtc
585 * @state: Plane state.
586 * @r: Rectangle to translate.
587 */
vmw_du_translate_to_crtc(struct drm_plane_state * state,struct drm_rect * r)588 static inline void vmw_du_translate_to_crtc(struct drm_plane_state *state,
589 struct drm_rect *r)
590 {
591 int translate_crtc_x = -((state->src_x >> 16) - state->crtc_x);
592 int translate_crtc_y = -((state->src_y >> 16) - state->crtc_y);
593
594 drm_rect_translate(r, translate_crtc_x, translate_crtc_y);
595 }
596
597 #endif
598