blob: b1fae4975247e2e29bcb929f4880898d2efbae39 [file] [log] [blame]
Sinclair Yeh35c05122015-06-26 01:42:06 -07001/******************************************************************************
2 *
Sinclair Yeh54fbde82015-07-29 12:38:02 -07003 * COPYRIGHT © 2014-2015 VMware, Inc., Palo Alto, CA., USA
Sinclair Yeh35c05122015-06-26 01:42:06 -07004 * All Rights Reserved.
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#include "vmwgfx_kms.h"
Sinclair Yeh8ce75f82015-07-08 21:20:39 -070029#include "device_include/svga3d_surfacedefs.h"
Sinclair Yeh35c05122015-06-26 01:42:06 -070030#include <drm/drm_plane_helper.h>
Sinclair Yehd7721ca2017-03-23 11:48:44 -070031#include <drm/drm_atomic.h>
32#include <drm/drm_atomic_helper.h>
33
Sinclair Yeh35c05122015-06-26 01:42:06 -070034
35#define vmw_crtc_to_stdu(x) \
36 container_of(x, struct vmw_screen_target_display_unit, base.crtc)
37#define vmw_encoder_to_stdu(x) \
38 container_of(x, struct vmw_screen_target_display_unit, base.encoder)
39#define vmw_connector_to_stdu(x) \
40 container_of(x, struct vmw_screen_target_display_unit, base.connector)
41
42
43
44enum stdu_content_type {
45 SAME_AS_DISPLAY = 0,
46 SEPARATE_SURFACE,
47 SEPARATE_DMA
48};
49
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -070050/**
51 * struct vmw_stdu_dirty - closure structure for the update functions
52 *
53 * @base: The base type we derive from. Used by vmw_kms_helper_dirty().
54 * @transfer: Transfer direction for DMA command.
55 * @left: Left side of bounding box.
56 * @right: Right side of bounding box.
57 * @top: Top side of bounding box.
58 * @bottom: Bottom side of bounding box.
59 * @buf: DMA buffer when DMA-ing between buffer and screen targets.
60 * @sid: Surface ID when copying between surface and screen targets.
61 */
62struct vmw_stdu_dirty {
63 struct vmw_kms_dirty base;
64 SVGA3dTransferType transfer;
65 s32 left, right, top, bottom;
66 u32 pitch;
67 union {
68 struct vmw_dma_buffer *buf;
69 u32 sid;
70 };
71};
72
73/*
74 * SVGA commands that are used by this code. Please see the device headers
75 * for explanation.
76 */
77struct vmw_stdu_update {
78 SVGA3dCmdHeader header;
79 SVGA3dCmdUpdateGBScreenTarget body;
80};
81
82struct vmw_stdu_dma {
83 SVGA3dCmdHeader header;
84 SVGA3dCmdSurfaceDMA body;
85};
86
87struct vmw_stdu_surface_copy {
88 SVGA3dCmdHeader header;
89 SVGA3dCmdSurfaceCopy body;
90};
Sinclair Yeh35c05122015-06-26 01:42:06 -070091
92
93/**
94 * struct vmw_screen_target_display_unit
95 *
96 * @base: VMW specific DU structure
97 * @display_srf: surface to be displayed. The dimension of this will always
98 * match the display mode. If the display mode matches
99 * content_vfbs dimensions, then this is a pointer into the
100 * corresponding field in content_vfbs. If not, then this
101 * is a separate buffer to which content_vfbs will blit to.
Sinclair Yeh35c05122015-06-26 01:42:06 -0700102 * @content_type: content_fb type
103 * @defined: true if the current display unit has been initialized
104 */
105struct vmw_screen_target_display_unit {
106 struct vmw_display_unit base;
107
108 struct vmw_surface *display_srf;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700109 enum stdu_content_type content_fb_type;
110
111 bool defined;
112};
113
114
115
116static void vmw_stdu_destroy(struct vmw_screen_target_display_unit *stdu);
117
118
119
120/******************************************************************************
121 * Screen Target Display Unit helper Functions
122 *****************************************************************************/
123
124/**
Sinclair Yeh35c05122015-06-26 01:42:06 -0700125 * vmw_stdu_unpin_display - unpins the resource associated with display surface
126 *
127 * @stdu: contains the display surface
128 *
129 * If the display surface was privatedly allocated by
130 * vmw_surface_gb_priv_define() and not registered as a framebuffer, then it
131 * won't be automatically cleaned up when all the framebuffers are freed. As
132 * such, we have to explicitly call vmw_resource_unreference() to get it freed.
133 */
134static void vmw_stdu_unpin_display(struct vmw_screen_target_display_unit *stdu)
135{
136 if (stdu->display_srf) {
137 struct vmw_resource *res = &stdu->display_srf->res;
138
139 vmw_resource_unpin(res);
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100140 vmw_surface_unreference(&stdu->display_srf);
Sinclair Yeh35c05122015-06-26 01:42:06 -0700141 }
142}
143
144
145
146/******************************************************************************
147 * Screen Target Display Unit CRTC Functions
148 *****************************************************************************/
149
150
151/**
152 * vmw_stdu_crtc_destroy - cleans up the STDU
153 *
154 * @crtc: used to get a reference to the containing STDU
155 */
156static void vmw_stdu_crtc_destroy(struct drm_crtc *crtc)
157{
158 vmw_stdu_destroy(vmw_crtc_to_stdu(crtc));
159}
160
Sinclair Yeh35c05122015-06-26 01:42:06 -0700161/**
162 * vmw_stdu_define_st - Defines a Screen Target
163 *
164 * @dev_priv: VMW DRM device
165 * @stdu: display unit to create a Screen Target for
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100166 * @mode: The mode to set.
167 * @crtc_x: X coordinate of screen target relative to framebuffer origin.
168 * @crtc_y: Y coordinate of screen target relative to framebuffer origin.
Sinclair Yeh35c05122015-06-26 01:42:06 -0700169 *
170 * Creates a STDU that we can used later. This function is called whenever the
171 * framebuffer size changes.
172 *
173 * RETURNs:
174 * 0 on success, error code on failure
175 */
176static int vmw_stdu_define_st(struct vmw_private *dev_priv,
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100177 struct vmw_screen_target_display_unit *stdu,
178 struct drm_display_mode *mode,
179 int crtc_x, int crtc_y)
Sinclair Yeh35c05122015-06-26 01:42:06 -0700180{
181 struct {
182 SVGA3dCmdHeader header;
183 SVGA3dCmdDefineGBScreenTarget body;
184 } *cmd;
185
186 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
187
188 if (unlikely(cmd == NULL)) {
189 DRM_ERROR("Out of FIFO space defining Screen Target\n");
190 return -ENOMEM;
191 }
192
193 cmd->header.id = SVGA_3D_CMD_DEFINE_GB_SCREENTARGET;
194 cmd->header.size = sizeof(cmd->body);
195
196 cmd->body.stid = stdu->base.unit;
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100197 cmd->body.width = mode->hdisplay;
198 cmd->body.height = mode->vdisplay;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700199 cmd->body.flags = (0 == cmd->body.stid) ? SVGA_STFLAG_PRIMARY : 0;
200 cmd->body.dpi = 0;
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100201 if (stdu->base.is_implicit) {
202 cmd->body.xRoot = crtc_x;
203 cmd->body.yRoot = crtc_y;
204 } else {
Sinclair Yeh35c05122015-06-26 01:42:06 -0700205 cmd->body.xRoot = stdu->base.gui_x;
206 cmd->body.yRoot = stdu->base.gui_y;
207 }
Thomas Hellstrom6dd687b2016-02-12 09:57:15 +0100208 stdu->base.set_gui_x = cmd->body.xRoot;
209 stdu->base.set_gui_y = cmd->body.yRoot;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700210
211 vmw_fifo_commit(dev_priv, sizeof(*cmd));
212
213 stdu->defined = true;
214
215 return 0;
216}
217
218
219
220/**
221 * vmw_stdu_bind_st - Binds a surface to a Screen Target
222 *
223 * @dev_priv: VMW DRM device
224 * @stdu: display unit affected
225 * @res: Buffer to bind to the screen target. Set to NULL to blank screen.
226 *
227 * Binding a surface to a Screen Target the same as flipping
228 */
229static int vmw_stdu_bind_st(struct vmw_private *dev_priv,
230 struct vmw_screen_target_display_unit *stdu,
231 struct vmw_resource *res)
232{
233 SVGA3dSurfaceImageId image;
234
235 struct {
236 SVGA3dCmdHeader header;
237 SVGA3dCmdBindGBScreenTarget body;
238 } *cmd;
239
240
241 if (!stdu->defined) {
242 DRM_ERROR("No screen target defined\n");
243 return -EINVAL;
244 }
245
246 /* Set up image using information in vfb */
247 memset(&image, 0, sizeof(image));
248 image.sid = res ? res->id : SVGA3D_INVALID_ID;
249
250 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
251
252 if (unlikely(cmd == NULL)) {
253 DRM_ERROR("Out of FIFO space binding a screen target\n");
254 return -ENOMEM;
255 }
256
257 cmd->header.id = SVGA_3D_CMD_BIND_GB_SCREENTARGET;
258 cmd->header.size = sizeof(cmd->body);
259
260 cmd->body.stid = stdu->base.unit;
261 cmd->body.image = image;
262
263 vmw_fifo_commit(dev_priv, sizeof(*cmd));
264
265 return 0;
266}
267
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700268/**
269 * vmw_stdu_populate_update - populate an UPDATE_GB_SCREENTARGET command with a
270 * bounding box.
271 *
272 * @cmd: Pointer to command stream.
273 * @unit: Screen target unit.
274 * @left: Left side of bounding box.
275 * @right: Right side of bounding box.
276 * @top: Top side of bounding box.
277 * @bottom: Bottom side of bounding box.
278 */
279static void vmw_stdu_populate_update(void *cmd, int unit,
280 s32 left, s32 right, s32 top, s32 bottom)
281{
282 struct vmw_stdu_update *update = cmd;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700283
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700284 update->header.id = SVGA_3D_CMD_UPDATE_GB_SCREENTARGET;
285 update->header.size = sizeof(update->body);
286
287 update->body.stid = unit;
288 update->body.rect.x = left;
289 update->body.rect.y = top;
290 update->body.rect.w = right - left;
291 update->body.rect.h = bottom - top;
292}
Sinclair Yeh35c05122015-06-26 01:42:06 -0700293
294/**
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700295 * vmw_stdu_update_st - Full update of a Screen Target
Sinclair Yeh35c05122015-06-26 01:42:06 -0700296 *
297 * @dev_priv: VMW DRM device
Sinclair Yeh35c05122015-06-26 01:42:06 -0700298 * @stdu: display unit affected
Sinclair Yeh35c05122015-06-26 01:42:06 -0700299 *
300 * This function needs to be called whenever the content of a screen
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700301 * target has changed completely. Typically as a result of a backing
302 * surface change.
Sinclair Yeh35c05122015-06-26 01:42:06 -0700303 *
304 * RETURNS:
305 * 0 on success, error code on failure
306 */
307static int vmw_stdu_update_st(struct vmw_private *dev_priv,
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700308 struct vmw_screen_target_display_unit *stdu)
Sinclair Yeh35c05122015-06-26 01:42:06 -0700309{
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700310 struct vmw_stdu_update *cmd;
311 struct drm_crtc *crtc = &stdu->base.crtc;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700312
313 if (!stdu->defined) {
314 DRM_ERROR("No screen target defined");
315 return -EINVAL;
316 }
317
Sinclair Yeh35c05122015-06-26 01:42:06 -0700318 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
319
320 if (unlikely(cmd == NULL)) {
321 DRM_ERROR("Out of FIFO space updating a Screen Target\n");
322 return -ENOMEM;
323 }
324
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700325 vmw_stdu_populate_update(cmd, stdu->base.unit, 0, crtc->mode.hdisplay,
326 0, crtc->mode.vdisplay);
Sinclair Yeh35c05122015-06-26 01:42:06 -0700327
328 vmw_fifo_commit(dev_priv, sizeof(*cmd));
329
330 return 0;
331}
332
333
334
335/**
336 * vmw_stdu_destroy_st - Destroy a Screen Target
337 *
338 * @dev_priv: VMW DRM device
339 * @stdu: display unit to destroy
340 */
341static int vmw_stdu_destroy_st(struct vmw_private *dev_priv,
342 struct vmw_screen_target_display_unit *stdu)
343{
344 int ret;
345
346 struct {
347 SVGA3dCmdHeader header;
348 SVGA3dCmdDestroyGBScreenTarget body;
349 } *cmd;
350
351
352 /* Nothing to do if not successfully defined */
353 if (unlikely(!stdu->defined))
354 return 0;
355
356 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
357
358 if (unlikely(cmd == NULL)) {
359 DRM_ERROR("Out of FIFO space, screen target not destroyed\n");
360 return -ENOMEM;
361 }
362
363 cmd->header.id = SVGA_3D_CMD_DESTROY_GB_SCREENTARGET;
364 cmd->header.size = sizeof(cmd->body);
365
366 cmd->body.stid = stdu->base.unit;
367
368 vmw_fifo_commit(dev_priv, sizeof(*cmd));
369
370 /* Force sync */
371 ret = vmw_fallback_wait(dev_priv, false, true, 0, false, 3*HZ);
372 if (unlikely(ret != 0))
373 DRM_ERROR("Failed to sync with HW");
374
375 stdu->defined = false;
376
377 return ret;
378}
379
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100380/**
381 * vmw_stdu_bind_fb - Bind an fb to a defined screen target
382 *
383 * @dev_priv: Pointer to a device private struct.
384 * @crtc: The crtc holding the screen target.
385 * @mode: The mode currently used by the screen target. Must be non-NULL.
386 * @new_fb: The new framebuffer to bind. Must be non-NULL.
387 *
388 * RETURNS:
389 * 0 on success, error code on failure.
390 */
391static int vmw_stdu_bind_fb(struct vmw_private *dev_priv,
392 struct drm_crtc *crtc,
393 struct drm_display_mode *mode,
394 struct drm_framebuffer *new_fb)
395{
396 struct vmw_screen_target_display_unit *stdu = vmw_crtc_to_stdu(crtc);
397 struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(new_fb);
398 struct vmw_surface *new_display_srf = NULL;
399 enum stdu_content_type new_content_type;
400 struct vmw_framebuffer_surface *new_vfbs;
401 int ret;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700402
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100403 WARN_ON_ONCE(!stdu->defined);
404
Thomas Hellstrombeca4cf2016-06-29 13:37:35 -0700405 new_vfbs = (vfb->dmabuf) ? NULL : vmw_framebuffer_to_vfbs(new_fb);
406
407 if (new_vfbs && new_vfbs->surface->base_size.width == mode->hdisplay &&
408 new_vfbs->surface->base_size.height == mode->vdisplay)
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100409 new_content_type = SAME_AS_DISPLAY;
410 else if (vfb->dmabuf)
411 new_content_type = SEPARATE_DMA;
412 else
413 new_content_type = SEPARATE_SURFACE;
414
415 if (new_content_type != SAME_AS_DISPLAY &&
416 !stdu->display_srf) {
417 struct vmw_surface content_srf;
418 struct drm_vmw_size display_base_size = {0};
419
420 display_base_size.width = mode->hdisplay;
421 display_base_size.height = mode->vdisplay;
422 display_base_size.depth = 1;
423
424 /*
425 * If content buffer is a DMA buf, then we have to construct
426 * surface info
427 */
428 if (new_content_type == SEPARATE_DMA) {
429
Ville Syrjälä272725c2016-12-14 23:32:20 +0200430 switch (new_fb->format->cpp[0] * 8) {
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100431 case 32:
432 content_srf.format = SVGA3D_X8R8G8B8;
433 break;
434
435 case 16:
436 content_srf.format = SVGA3D_R5G6B5;
437 break;
438
439 case 8:
440 content_srf.format = SVGA3D_P8;
441 break;
442
443 default:
444 DRM_ERROR("Invalid format\n");
445 return -EINVAL;
446 }
447
448 content_srf.flags = 0;
449 content_srf.mip_levels[0] = 1;
450 content_srf.multisample_count = 0;
451 } else {
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100452 content_srf = *new_vfbs->surface;
453 }
454
455
456 ret = vmw_surface_gb_priv_define(crtc->dev,
457 0, /* because kernel visible only */
458 content_srf.flags,
459 content_srf.format,
460 true, /* a scanout buffer */
461 content_srf.mip_levels[0],
462 content_srf.multisample_count,
463 0,
464 display_base_size,
465 &new_display_srf);
466 if (unlikely(ret != 0)) {
467 DRM_ERROR("Could not allocate screen target surface.\n");
468 return ret;
469 }
470 } else if (new_content_type == SAME_AS_DISPLAY) {
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100471 new_display_srf = vmw_surface_reference(new_vfbs->surface);
472 }
473
474 if (new_display_srf) {
475 /* Pin new surface before flipping */
476 ret = vmw_resource_pin(&new_display_srf->res, false);
477 if (ret)
478 goto out_srf_unref;
479
480 ret = vmw_stdu_bind_st(dev_priv, stdu, &new_display_srf->res);
481 if (ret)
482 goto out_srf_unpin;
483
484 /* Unpin and unreference old surface */
485 vmw_stdu_unpin_display(stdu);
486
487 /* Transfer the reference */
488 stdu->display_srf = new_display_srf;
489 new_display_srf = NULL;
490 }
491
492 crtc->primary->fb = new_fb;
493 stdu->content_fb_type = new_content_type;
494 return 0;
495
496out_srf_unpin:
497 vmw_resource_unpin(&new_display_srf->res);
498out_srf_unref:
499 vmw_surface_unreference(&new_display_srf);
500 return ret;
501}
Sinclair Yeh35c05122015-06-26 01:42:06 -0700502
503/**
504 * vmw_stdu_crtc_set_config - Sets a mode
505 *
506 * @set: mode parameters
507 *
508 * This function is the device-specific portion of the DRM CRTC mode set.
509 * For the SVGA device, we do this by defining a Screen Target, binding a
510 * GB Surface to that target, and finally update the screen target.
511 *
512 * RETURNS:
513 * 0 on success, error code otherwise
514 */
515static int vmw_stdu_crtc_set_config(struct drm_mode_set *set)
516{
517 struct vmw_private *dev_priv;
Thomas Hellstrom4d492a02016-02-12 09:01:29 +0100518 struct vmw_framebuffer *vfb;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700519 struct vmw_screen_target_display_unit *stdu;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700520 struct drm_display_mode *mode;
521 struct drm_framebuffer *new_fb;
522 struct drm_crtc *crtc;
523 struct drm_encoder *encoder;
524 struct drm_connector *connector;
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100525 bool turning_off;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700526 int ret;
527
528
529 if (!set || !set->crtc)
530 return -EINVAL;
531
532 crtc = set->crtc;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700533 stdu = vmw_crtc_to_stdu(crtc);
534 mode = set->mode;
535 new_fb = set->fb;
536 dev_priv = vmw_priv(crtc->dev);
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100537 turning_off = set->num_connectors == 0 || !mode || !new_fb;
Thomas Hellstrom4d492a02016-02-12 09:01:29 +0100538 vfb = (new_fb) ? vmw_framebuffer_to_vfb(new_fb) : NULL;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700539
540 if (set->num_connectors > 1) {
541 DRM_ERROR("Too many connectors\n");
542 return -EINVAL;
543 }
544
545 if (set->num_connectors == 1 &&
546 set->connectors[0] != &stdu->base.connector) {
547 DRM_ERROR("Connectors don't match %p %p\n",
548 set->connectors[0], &stdu->base.connector);
549 return -EINVAL;
550 }
551
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100552 if (!turning_off && (set->x + mode->hdisplay > new_fb->width ||
553 set->y + mode->vdisplay > new_fb->height)) {
554 DRM_ERROR("Set outside of framebuffer\n");
555 return -EINVAL;
556 }
Sinclair Yeh35c05122015-06-26 01:42:06 -0700557
Thomas Hellstrom4d492a02016-02-12 09:01:29 +0100558 /* Only one active implicit frame-buffer at a time. */
Thomas Hellstrom93cd1682016-05-03 11:24:35 +0200559 mutex_lock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom4d492a02016-02-12 09:01:29 +0100560 if (!turning_off && stdu->base.is_implicit && dev_priv->implicit_fb &&
561 !(dev_priv->num_implicit == 1 && stdu->base.active_implicit)
562 && dev_priv->implicit_fb != vfb) {
Thomas Hellstrom93cd1682016-05-03 11:24:35 +0200563 mutex_unlock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom4d492a02016-02-12 09:01:29 +0100564 DRM_ERROR("Multiple implicit framebuffers not supported.\n");
565 return -EINVAL;
566 }
Thomas Hellstrom93cd1682016-05-03 11:24:35 +0200567 mutex_unlock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom4d492a02016-02-12 09:01:29 +0100568
Sinclair Yeh35c05122015-06-26 01:42:06 -0700569 /* Since they always map one to one these are safe */
570 connector = &stdu->base.connector;
571 encoder = &stdu->base.encoder;
572
Sinclair Yeh35c05122015-06-26 01:42:06 -0700573 if (stdu->defined) {
Sinclair Yeh35c05122015-06-26 01:42:06 -0700574 ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100575 if (ret)
Sinclair Yeh35c05122015-06-26 01:42:06 -0700576 return ret;
577
Sinclair Yeh35c05122015-06-26 01:42:06 -0700578 vmw_stdu_unpin_display(stdu);
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100579 (void) vmw_stdu_update_st(dev_priv, stdu);
Thomas Hellstrom4d492a02016-02-12 09:01:29 +0100580 vmw_kms_del_active(dev_priv, &stdu->base);
Sinclair Yeh35c05122015-06-26 01:42:06 -0700581
582 ret = vmw_stdu_destroy_st(dev_priv, stdu);
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100583 if (ret)
Sinclair Yeh35c05122015-06-26 01:42:06 -0700584 return ret;
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100585
586 crtc->primary->fb = NULL;
587 crtc->enabled = false;
588 encoder->crtc = NULL;
589 connector->encoder = NULL;
590 stdu->content_fb_type = SAME_AS_DISPLAY;
591 crtc->x = set->x;
592 crtc->y = set->y;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700593 }
594
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100595 if (turning_off)
Sinclair Yeh35c05122015-06-26 01:42:06 -0700596 return 0;
597
Sinclair Yeh35c05122015-06-26 01:42:06 -0700598 /*
599 * Steps to displaying a surface, assume surface is already
600 * bound:
601 * 1. define a screen target
602 * 2. bind a fb to the screen target
603 * 3. update that screen target (this is done later by
604 * vmw_kms_stdu_do_surface_dirty_or_present)
605 */
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100606 /*
607 * Note on error handling: We can't really restore the crtc to
608 * it's original state on error, but we at least update the
609 * current state to what's submitted to hardware to enable
610 * future recovery.
611 */
612 vmw_svga_enable(dev_priv);
613 ret = vmw_stdu_define_st(dev_priv, stdu, mode, set->x, set->y);
614 if (ret)
615 return ret;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700616
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100617 crtc->x = set->x;
618 crtc->y = set->y;
619 crtc->mode = *mode;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700620
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100621 ret = vmw_stdu_bind_fb(dev_priv, crtc, mode, new_fb);
622 if (ret)
623 return ret;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700624
Thomas Hellstrom4d492a02016-02-12 09:01:29 +0100625 vmw_kms_add_active(dev_priv, &stdu->base, vfb);
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100626 crtc->enabled = true;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700627 connector->encoder = encoder;
628 encoder->crtc = crtc;
629
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100630 return 0;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700631}
632
Sinclair Yeh35c05122015-06-26 01:42:06 -0700633/**
634 * vmw_stdu_crtc_page_flip - Binds a buffer to a screen target
635 *
636 * @crtc: CRTC to attach FB to
637 * @fb: FB to attach
638 * @event: Event to be posted. This event should've been alloced
639 * using k[mz]alloc, and should've been completely initialized.
640 * @page_flip_flags: Input flags.
641 *
642 * If the STDU uses the same display and content buffers, i.e. a true flip,
643 * this function will replace the existing display buffer with the new content
644 * buffer.
645 *
646 * If the STDU uses different display and content buffers, i.e. a blit, then
647 * only the content buffer will be updated.
648 *
649 * RETURNS:
650 * 0 on success, error code on failure
651 */
652static int vmw_stdu_crtc_page_flip(struct drm_crtc *crtc,
653 struct drm_framebuffer *new_fb,
654 struct drm_pending_vblank_event *event,
655 uint32_t flags)
656
657{
658 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
659 struct vmw_screen_target_display_unit *stdu;
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100660 struct drm_vmw_rect vclips;
661 struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(new_fb);
Sinclair Yeh35c05122015-06-26 01:42:06 -0700662 int ret;
663
Sinclair Yeh35c05122015-06-26 01:42:06 -0700664 dev_priv = vmw_priv(crtc->dev);
665 stdu = vmw_crtc_to_stdu(crtc);
Sinclair Yeh35c05122015-06-26 01:42:06 -0700666
Thomas Hellstrom4d492a02016-02-12 09:01:29 +0100667 if (!stdu->defined || !vmw_kms_crtc_flippable(dev_priv, crtc))
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100668 return -EINVAL;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700669
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100670 ret = vmw_stdu_bind_fb(dev_priv, crtc, &crtc->mode, new_fb);
671 if (ret)
672 return ret;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700673
Thomas Hellstrom4d492a02016-02-12 09:01:29 +0100674 if (stdu->base.is_implicit)
675 vmw_kms_update_implicit_fb(dev_priv, crtc);
676
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100677 vclips.x = crtc->x;
678 vclips.y = crtc->y;
679 vclips.w = crtc->mode.hdisplay;
680 vclips.h = crtc->mode.vdisplay;
681 if (vfb->dmabuf)
682 ret = vmw_kms_stdu_dma(dev_priv, NULL, vfb, NULL, NULL, &vclips,
683 1, 1, true, false);
684 else
685 ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, &vclips,
686 NULL, 0, 0, 1, 1, NULL);
687 if (ret)
Sinclair Yeh35c05122015-06-26 01:42:06 -0700688 return ret;
689
690 if (event) {
691 struct vmw_fence_obj *fence = NULL;
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700692 struct drm_file *file_priv = event->base.file_priv;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700693
694 vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
695 if (!fence)
696 return -ENOMEM;
697
698 ret = vmw_event_fence_action_queue(file_priv, fence,
699 &event->base,
700 &event->event.tv_sec,
701 &event->event.tv_usec,
702 true);
703 vmw_fence_obj_unreference(&fence);
Thomas Hellstrom4e0858a2015-11-05 02:18:55 -0800704 } else {
705 vmw_fifo_flush(dev_priv, false);
Sinclair Yeh35c05122015-06-26 01:42:06 -0700706 }
707
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100708 return 0;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700709}
710
711
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700712/**
713 * vmw_stdu_dmabuf_clip - Callback to encode a suface DMA command cliprect
714 *
715 * @dirty: The closure structure.
716 *
717 * Encodes a surface DMA command cliprect and updates the bounding box
718 * for the DMA.
719 */
720static void vmw_stdu_dmabuf_clip(struct vmw_kms_dirty *dirty)
721{
722 struct vmw_stdu_dirty *ddirty =
723 container_of(dirty, struct vmw_stdu_dirty, base);
724 struct vmw_stdu_dma *cmd = dirty->cmd;
725 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
726
727 blit += dirty->num_hits;
728 blit->srcx = dirty->fb_x;
729 blit->srcy = dirty->fb_y;
730 blit->x = dirty->unit_x1;
731 blit->y = dirty->unit_y1;
732 blit->d = 1;
733 blit->w = dirty->unit_x2 - dirty->unit_x1;
734 blit->h = dirty->unit_y2 - dirty->unit_y1;
735 dirty->num_hits++;
736
737 if (ddirty->transfer != SVGA3D_WRITE_HOST_VRAM)
738 return;
739
740 /* Destination bounding box */
741 ddirty->left = min_t(s32, ddirty->left, dirty->unit_x1);
742 ddirty->top = min_t(s32, ddirty->top, dirty->unit_y1);
743 ddirty->right = max_t(s32, ddirty->right, dirty->unit_x2);
744 ddirty->bottom = max_t(s32, ddirty->bottom, dirty->unit_y2);
745}
746
747/**
748 * vmw_stdu_dmabuf_fifo_commit - Callback to fill in and submit a DMA command.
749 *
750 * @dirty: The closure structure.
751 *
752 * Fills in the missing fields in a DMA command, and optionally encodes
753 * a screen target update command, depending on transfer direction.
754 */
755static void vmw_stdu_dmabuf_fifo_commit(struct vmw_kms_dirty *dirty)
756{
757 struct vmw_stdu_dirty *ddirty =
758 container_of(dirty, struct vmw_stdu_dirty, base);
759 struct vmw_screen_target_display_unit *stdu =
760 container_of(dirty->unit, typeof(*stdu), base);
761 struct vmw_stdu_dma *cmd = dirty->cmd;
762 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
763 SVGA3dCmdSurfaceDMASuffix *suffix =
764 (SVGA3dCmdSurfaceDMASuffix *) &blit[dirty->num_hits];
765 size_t blit_size = sizeof(*blit) * dirty->num_hits + sizeof(*suffix);
766
767 if (!dirty->num_hits) {
768 vmw_fifo_commit(dirty->dev_priv, 0);
769 return;
770 }
771
772 cmd->header.id = SVGA_3D_CMD_SURFACE_DMA;
773 cmd->header.size = sizeof(cmd->body) + blit_size;
774 vmw_bo_get_guest_ptr(&ddirty->buf->base, &cmd->body.guest.ptr);
775 cmd->body.guest.pitch = ddirty->pitch;
776 cmd->body.host.sid = stdu->display_srf->res.id;
777 cmd->body.host.face = 0;
778 cmd->body.host.mipmap = 0;
779 cmd->body.transfer = ddirty->transfer;
780 suffix->suffixSize = sizeof(*suffix);
781 suffix->maximumOffset = ddirty->buf->base.num_pages * PAGE_SIZE;
782
783 if (ddirty->transfer == SVGA3D_WRITE_HOST_VRAM) {
784 blit_size += sizeof(struct vmw_stdu_update);
785
786 vmw_stdu_populate_update(&suffix[1], stdu->base.unit,
787 ddirty->left, ddirty->right,
788 ddirty->top, ddirty->bottom);
789 }
790
791 vmw_fifo_commit(dirty->dev_priv, sizeof(*cmd) + blit_size);
792
793 ddirty->left = ddirty->top = S32_MAX;
794 ddirty->right = ddirty->bottom = S32_MIN;
795}
796
797/**
798 * vmw_kms_stdu_dma - Perform a DMA transfer between a dma-buffer backed
799 * framebuffer and the screen target system.
800 *
801 * @dev_priv: Pointer to the device private structure.
802 * @file_priv: Pointer to a struct drm-file identifying the caller. May be
803 * set to NULL, but then @user_fence_rep must also be set to NULL.
804 * @vfb: Pointer to the dma-buffer backed framebuffer.
805 * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
806 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
807 * be NULL.
808 * @num_clips: Number of clip rects in @clips or @vclips.
809 * @increment: Increment to use when looping over @clips or @vclips.
810 * @to_surface: Whether to DMA to the screen target system as opposed to
811 * from the screen target system.
812 * @interruptible: Whether to perform waits interruptible if possible.
813 *
814 * If DMA-ing till the screen target system, the function will also notify
815 * the screen target system that a bounding box of the cliprects has been
816 * updated.
817 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
818 * interrupted.
819 */
820int vmw_kms_stdu_dma(struct vmw_private *dev_priv,
821 struct drm_file *file_priv,
822 struct vmw_framebuffer *vfb,
823 struct drm_vmw_fence_rep __user *user_fence_rep,
824 struct drm_clip_rect *clips,
825 struct drm_vmw_rect *vclips,
826 uint32_t num_clips,
827 int increment,
828 bool to_surface,
829 bool interruptible)
830{
831 struct vmw_dma_buffer *buf =
832 container_of(vfb, struct vmw_framebuffer_dmabuf, base)->buffer;
833 struct vmw_stdu_dirty ddirty;
834 int ret;
835
836 ret = vmw_kms_helper_buffer_prepare(dev_priv, buf, interruptible,
837 false);
838 if (ret)
839 return ret;
840
841 ddirty.transfer = (to_surface) ? SVGA3D_WRITE_HOST_VRAM :
842 SVGA3D_READ_HOST_VRAM;
843 ddirty.left = ddirty.top = S32_MAX;
844 ddirty.right = ddirty.bottom = S32_MIN;
845 ddirty.pitch = vfb->base.pitches[0];
846 ddirty.buf = buf;
847 ddirty.base.fifo_commit = vmw_stdu_dmabuf_fifo_commit;
848 ddirty.base.clip = vmw_stdu_dmabuf_clip;
849 ddirty.base.fifo_reserve_size = sizeof(struct vmw_stdu_dma) +
850 num_clips * sizeof(SVGA3dCopyBox) +
851 sizeof(SVGA3dCmdSurfaceDMASuffix);
852 if (to_surface)
853 ddirty.base.fifo_reserve_size += sizeof(struct vmw_stdu_update);
854
855 ret = vmw_kms_helper_dirty(dev_priv, vfb, clips, vclips,
856 0, 0, num_clips, increment, &ddirty.base);
857 vmw_kms_helper_buffer_finish(dev_priv, file_priv, buf, NULL,
858 user_fence_rep);
859
860 return ret;
861}
862
863/**
864 * vmw_stdu_surface_clip - Callback to encode a surface copy command cliprect
865 *
866 * @dirty: The closure structure.
867 *
868 * Encodes a surface copy command cliprect and updates the bounding box
869 * for the copy.
870 */
871static void vmw_kms_stdu_surface_clip(struct vmw_kms_dirty *dirty)
872{
873 struct vmw_stdu_dirty *sdirty =
874 container_of(dirty, struct vmw_stdu_dirty, base);
875 struct vmw_stdu_surface_copy *cmd = dirty->cmd;
876 struct vmw_screen_target_display_unit *stdu =
877 container_of(dirty->unit, typeof(*stdu), base);
878
879 if (sdirty->sid != stdu->display_srf->res.id) {
880 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
881
882 blit += dirty->num_hits;
883 blit->srcx = dirty->fb_x;
884 blit->srcy = dirty->fb_y;
885 blit->x = dirty->unit_x1;
886 blit->y = dirty->unit_y1;
887 blit->d = 1;
888 blit->w = dirty->unit_x2 - dirty->unit_x1;
889 blit->h = dirty->unit_y2 - dirty->unit_y1;
890 }
891
892 dirty->num_hits++;
893
894 /* Destination bounding box */
895 sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1);
896 sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1);
897 sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2);
898 sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2);
899}
900
901/**
902 * vmw_stdu_surface_fifo_commit - Callback to fill in and submit a surface
903 * copy command.
904 *
905 * @dirty: The closure structure.
906 *
907 * Fills in the missing fields in a surface copy command, and encodes a screen
908 * target update command.
909 */
910static void vmw_kms_stdu_surface_fifo_commit(struct vmw_kms_dirty *dirty)
911{
912 struct vmw_stdu_dirty *sdirty =
913 container_of(dirty, struct vmw_stdu_dirty, base);
914 struct vmw_screen_target_display_unit *stdu =
915 container_of(dirty->unit, typeof(*stdu), base);
916 struct vmw_stdu_surface_copy *cmd = dirty->cmd;
917 struct vmw_stdu_update *update;
918 size_t blit_size = sizeof(SVGA3dCopyBox) * dirty->num_hits;
919 size_t commit_size;
920
921 if (!dirty->num_hits) {
922 vmw_fifo_commit(dirty->dev_priv, 0);
923 return;
924 }
925
926 if (sdirty->sid != stdu->display_srf->res.id) {
927 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
928
929 cmd->header.id = SVGA_3D_CMD_SURFACE_COPY;
930 cmd->header.size = sizeof(cmd->body) + blit_size;
931 cmd->body.src.sid = sdirty->sid;
932 cmd->body.dest.sid = stdu->display_srf->res.id;
933 update = (struct vmw_stdu_update *) &blit[dirty->num_hits];
934 commit_size = sizeof(*cmd) + blit_size + sizeof(*update);
935 } else {
936 update = dirty->cmd;
937 commit_size = sizeof(*update);
938 }
939
940 vmw_stdu_populate_update(update, stdu->base.unit, sdirty->left,
941 sdirty->right, sdirty->top, sdirty->bottom);
942
943 vmw_fifo_commit(dirty->dev_priv, commit_size);
944
945 sdirty->left = sdirty->top = S32_MAX;
946 sdirty->right = sdirty->bottom = S32_MIN;
947}
948
949/**
950 * vmw_kms_stdu_surface_dirty - Dirty part of a surface backed framebuffer
951 *
952 * @dev_priv: Pointer to the device private structure.
953 * @framebuffer: Pointer to the surface-buffer backed framebuffer.
954 * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
955 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
956 * be NULL.
957 * @srf: Pointer to surface to blit from. If NULL, the surface attached
958 * to @framebuffer will be used.
959 * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
960 * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
961 * @num_clips: Number of clip rects in @clips.
962 * @inc: Increment to use when looping over @clips.
963 * @out_fence: If non-NULL, will return a ref-counted pointer to a
964 * struct vmw_fence_obj. The returned fence pointer may be NULL in which
965 * case the device has already synchronized.
966 *
967 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
968 * interrupted.
969 */
970int vmw_kms_stdu_surface_dirty(struct vmw_private *dev_priv,
971 struct vmw_framebuffer *framebuffer,
972 struct drm_clip_rect *clips,
973 struct drm_vmw_rect *vclips,
974 struct vmw_resource *srf,
975 s32 dest_x,
976 s32 dest_y,
977 unsigned num_clips, int inc,
978 struct vmw_fence_obj **out_fence)
979{
980 struct vmw_framebuffer_surface *vfbs =
981 container_of(framebuffer, typeof(*vfbs), base);
982 struct vmw_stdu_dirty sdirty;
983 int ret;
984
985 if (!srf)
986 srf = &vfbs->surface->res;
987
988 ret = vmw_kms_helper_resource_prepare(srf, true);
989 if (ret)
990 return ret;
991
992 if (vfbs->is_dmabuf_proxy) {
993 ret = vmw_kms_update_proxy(srf, clips, num_clips, inc);
994 if (ret)
995 goto out_finish;
996 }
997
998 sdirty.base.fifo_commit = vmw_kms_stdu_surface_fifo_commit;
999 sdirty.base.clip = vmw_kms_stdu_surface_clip;
1000 sdirty.base.fifo_reserve_size = sizeof(struct vmw_stdu_surface_copy) +
1001 sizeof(SVGA3dCopyBox) * num_clips +
1002 sizeof(struct vmw_stdu_update);
1003 sdirty.sid = srf->id;
1004 sdirty.left = sdirty.top = S32_MAX;
1005 sdirty.right = sdirty.bottom = S32_MIN;
1006
1007 ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
1008 dest_x, dest_y, num_clips, inc,
1009 &sdirty.base);
1010out_finish:
1011 vmw_kms_helper_resource_finish(srf, out_fence);
1012
1013 return ret;
1014}
1015
Sinclair Yeh35c05122015-06-26 01:42:06 -07001016
1017/*
1018 * Screen Target CRTC dispatch table
1019 */
Ville Syrjäläd7955fc2015-12-15 12:21:15 +01001020static const struct drm_crtc_funcs vmw_stdu_crtc_funcs = {
Sinclair Yeh35c05122015-06-26 01:42:06 -07001021 .gamma_set = vmw_du_crtc_gamma_set,
1022 .destroy = vmw_stdu_crtc_destroy,
Sinclair Yeh9c2542a2017-03-23 11:33:39 -07001023 .reset = vmw_du_crtc_reset,
1024 .atomic_duplicate_state = vmw_du_crtc_duplicate_state,
1025 .atomic_destroy_state = vmw_du_crtc_destroy_state,
Sinclair Yeh35c05122015-06-26 01:42:06 -07001026 .set_config = vmw_stdu_crtc_set_config,
1027 .page_flip = vmw_stdu_crtc_page_flip,
1028};
1029
1030
1031
1032/******************************************************************************
1033 * Screen Target Display Unit Encoder Functions
1034 *****************************************************************************/
1035
1036/**
1037 * vmw_stdu_encoder_destroy - cleans up the STDU
1038 *
1039 * @encoder: used the get the containing STDU
1040 *
1041 * vmwgfx cleans up crtc/encoder/connector all at the same time so technically
1042 * this can be a no-op. Nevertheless, it doesn't hurt of have this in case
1043 * the common KMS code changes and somehow vmw_stdu_crtc_destroy() doesn't
1044 * get called.
1045 */
1046static void vmw_stdu_encoder_destroy(struct drm_encoder *encoder)
1047{
1048 vmw_stdu_destroy(vmw_encoder_to_stdu(encoder));
1049}
1050
Ville Syrjäläd7955fc2015-12-15 12:21:15 +01001051static const struct drm_encoder_funcs vmw_stdu_encoder_funcs = {
Sinclair Yeh35c05122015-06-26 01:42:06 -07001052 .destroy = vmw_stdu_encoder_destroy,
1053};
1054
1055
1056
1057/******************************************************************************
1058 * Screen Target Display Unit Connector Functions
1059 *****************************************************************************/
1060
1061/**
1062 * vmw_stdu_connector_destroy - cleans up the STDU
1063 *
1064 * @connector: used to get the containing STDU
1065 *
1066 * vmwgfx cleans up crtc/encoder/connector all at the same time so technically
1067 * this can be a no-op. Nevertheless, it doesn't hurt of have this in case
1068 * the common KMS code changes and somehow vmw_stdu_crtc_destroy() doesn't
1069 * get called.
1070 */
1071static void vmw_stdu_connector_destroy(struct drm_connector *connector)
1072{
1073 vmw_stdu_destroy(vmw_connector_to_stdu(connector));
1074}
1075
1076
1077
Ville Syrjäläd7955fc2015-12-15 12:21:15 +01001078static const struct drm_connector_funcs vmw_stdu_connector_funcs = {
Sinclair Yeh35c05122015-06-26 01:42:06 -07001079 .dpms = vmw_du_connector_dpms,
Sinclair Yeh35c05122015-06-26 01:42:06 -07001080 .detect = vmw_du_connector_detect,
1081 .fill_modes = vmw_du_connector_fill_modes,
1082 .set_property = vmw_du_connector_set_property,
1083 .destroy = vmw_stdu_connector_destroy,
Sinclair Yehd7721ca2017-03-23 11:48:44 -07001084 .reset = vmw_du_connector_reset,
1085 .atomic_duplicate_state = vmw_du_connector_duplicate_state,
1086 .atomic_destroy_state = vmw_du_connector_destroy_state,
1087 .atomic_set_property = vmw_du_connector_atomic_set_property,
1088 .atomic_get_property = vmw_du_connector_atomic_get_property,
Sinclair Yeh35c05122015-06-26 01:42:06 -07001089};
1090
1091
1092
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001093/******************************************************************************
1094 * Screen Target Display Plane Functions
1095 *****************************************************************************/
1096
1097static const struct drm_plane_funcs vmw_stdu_plane_funcs = {
1098 .update_plane = drm_primary_helper_update,
1099 .disable_plane = drm_primary_helper_disable,
1100 .destroy = vmw_du_primary_plane_destroy,
Sinclair Yehcc5ec452017-03-23 11:36:05 -07001101 .reset = vmw_du_plane_reset,
1102 .atomic_duplicate_state = vmw_du_plane_duplicate_state,
1103 .atomic_destroy_state = vmw_du_plane_destroy_state,
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001104};
1105
1106static const struct drm_plane_funcs vmw_stdu_cursor_funcs = {
1107 .update_plane = vmw_du_cursor_plane_update,
1108 .disable_plane = vmw_du_cursor_plane_disable,
1109 .destroy = vmw_du_cursor_plane_destroy,
Sinclair Yehcc5ec452017-03-23 11:36:05 -07001110 .reset = vmw_du_plane_reset,
1111 .atomic_duplicate_state = vmw_du_plane_duplicate_state,
1112 .atomic_destroy_state = vmw_du_plane_destroy_state,
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001113};
1114
1115
Sinclair Yeh35c05122015-06-26 01:42:06 -07001116/**
1117 * vmw_stdu_init - Sets up a Screen Target Display Unit
1118 *
1119 * @dev_priv: VMW DRM device
1120 * @unit: unit number range from 0 to VMWGFX_NUM_DISPLAY_UNITS
1121 *
1122 * This function is called once per CRTC, and allocates one Screen Target
1123 * display unit to represent that CRTC. Since the SVGA device does not separate
1124 * out encoder and connector, they are represented as part of the STDU as well.
1125 */
1126static int vmw_stdu_init(struct vmw_private *dev_priv, unsigned unit)
1127{
1128 struct vmw_screen_target_display_unit *stdu;
1129 struct drm_device *dev = dev_priv->dev;
1130 struct drm_connector *connector;
1131 struct drm_encoder *encoder;
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001132 struct drm_plane *primary, *cursor;
Sinclair Yeh35c05122015-06-26 01:42:06 -07001133 struct drm_crtc *crtc;
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001134 int ret;
Sinclair Yeh35c05122015-06-26 01:42:06 -07001135
1136
1137 stdu = kzalloc(sizeof(*stdu), GFP_KERNEL);
1138 if (!stdu)
1139 return -ENOMEM;
1140
1141 stdu->base.unit = unit;
1142 crtc = &stdu->base.crtc;
1143 encoder = &stdu->base.encoder;
1144 connector = &stdu->base.connector;
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001145 primary = &stdu->base.primary;
1146 cursor = &stdu->base.cursor;
Sinclair Yeh35c05122015-06-26 01:42:06 -07001147
1148 stdu->base.pref_active = (unit == 0);
1149 stdu->base.pref_width = dev_priv->initial_width;
1150 stdu->base.pref_height = dev_priv->initial_height;
Sinclair Yeh9c2542a2017-03-23 11:33:39 -07001151
1152 /*
1153 * Remove this after enabling atomic because property values can
1154 * only exist in a state object
1155 */
Thomas Hellstrom2e69b252016-02-12 09:59:50 +01001156 stdu->base.is_implicit = false;
Sinclair Yeh35c05122015-06-26 01:42:06 -07001157
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001158 /* Initialize primary plane */
Sinclair Yehcc5ec452017-03-23 11:36:05 -07001159 vmw_du_plane_reset(primary);
1160
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001161 ret = drm_universal_plane_init(dev, primary,
1162 0, &vmw_stdu_plane_funcs,
1163 vmw_primary_plane_formats,
1164 ARRAY_SIZE(vmw_primary_plane_formats),
1165 DRM_PLANE_TYPE_PRIMARY, NULL);
1166 if (ret) {
1167 DRM_ERROR("Failed to initialize primary plane");
1168 goto err_free;
1169 }
1170
1171 /* Initialize cursor plane */
Sinclair Yehcc5ec452017-03-23 11:36:05 -07001172 vmw_du_plane_reset(cursor);
1173
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001174 ret = drm_universal_plane_init(dev, cursor,
1175 0, &vmw_stdu_cursor_funcs,
1176 vmw_cursor_plane_formats,
1177 ARRAY_SIZE(vmw_cursor_plane_formats),
1178 DRM_PLANE_TYPE_CURSOR, NULL);
1179 if (ret) {
1180 DRM_ERROR("Failed to initialize cursor plane");
1181 drm_plane_cleanup(&stdu->base.primary);
1182 goto err_free;
1183 }
1184
Sinclair Yehd7721ca2017-03-23 11:48:44 -07001185 vmw_du_connector_reset(connector);
1186
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001187 ret = drm_connector_init(dev, connector, &vmw_stdu_connector_funcs,
1188 DRM_MODE_CONNECTOR_VIRTUAL);
1189 if (ret) {
1190 DRM_ERROR("Failed to initialize connector\n");
1191 goto err_free;
1192 }
Sinclair Yeh35c05122015-06-26 01:42:06 -07001193 connector->status = vmw_du_connector_detect(connector, false);
Sinclair Yehd7721ca2017-03-23 11:48:44 -07001194 vmw_connector_state_to_vcs(connector->state)->is_implicit = false;
Sinclair Yeh35c05122015-06-26 01:42:06 -07001195
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001196 ret = drm_encoder_init(dev, encoder, &vmw_stdu_encoder_funcs,
1197 DRM_MODE_ENCODER_VIRTUAL, NULL);
1198 if (ret) {
1199 DRM_ERROR("Failed to initialize encoder\n");
1200 goto err_free_connector;
1201 }
1202
1203 (void) drm_mode_connector_attach_encoder(connector, encoder);
Sinclair Yeh35c05122015-06-26 01:42:06 -07001204 encoder->possible_crtcs = (1 << unit);
1205 encoder->possible_clones = 0;
1206
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001207 ret = drm_connector_register(connector);
1208 if (ret) {
1209 DRM_ERROR("Failed to register connector\n");
1210 goto err_free_encoder;
1211 }
Sinclair Yeh35c05122015-06-26 01:42:06 -07001212
Sinclair Yehd7721ca2017-03-23 11:48:44 -07001213 vmw_du_crtc_reset(crtc);
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001214 ret = drm_crtc_init_with_planes(dev, crtc, &stdu->base.primary,
1215 &stdu->base.cursor,
1216 &vmw_stdu_crtc_funcs, NULL);
1217 if (ret) {
1218 DRM_ERROR("Failed to initialize CRTC\n");
1219 goto err_free_unregister;
1220 }
Sinclair Yeh35c05122015-06-26 01:42:06 -07001221
1222 drm_mode_crtc_set_gamma_size(crtc, 256);
1223
1224 drm_object_attach_property(&connector->base,
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001225 dev_priv->hotplug_mode_update_property, 1);
1226 drm_object_attach_property(&connector->base,
1227 dev->mode_config.suggested_x_property, 0);
1228 drm_object_attach_property(&connector->base,
1229 dev->mode_config.suggested_y_property, 0);
Thomas Hellstrom76404ac2016-02-12 09:55:45 +01001230 if (dev_priv->implicit_placement_property)
1231 drm_object_attach_property
1232 (&connector->base,
1233 dev_priv->implicit_placement_property,
1234 stdu->base.is_implicit);
Sinclair Yeh35c05122015-06-26 01:42:06 -07001235 return 0;
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001236
1237err_free_unregister:
1238 drm_connector_unregister(connector);
1239err_free_encoder:
1240 drm_encoder_cleanup(encoder);
1241err_free_connector:
1242 drm_connector_cleanup(connector);
1243err_free:
1244 kfree(stdu);
1245 return ret;
Sinclair Yeh35c05122015-06-26 01:42:06 -07001246}
1247
1248
1249
1250/**
1251 * vmw_stdu_destroy - Cleans up a vmw_screen_target_display_unit
1252 *
1253 * @stdu: Screen Target Display Unit to be destroyed
1254 *
1255 * Clean up after vmw_stdu_init
1256 */
1257static void vmw_stdu_destroy(struct vmw_screen_target_display_unit *stdu)
1258{
1259 vmw_stdu_unpin_display(stdu);
1260
1261 vmw_du_cleanup(&stdu->base);
1262 kfree(stdu);
1263}
1264
1265
1266
1267/******************************************************************************
1268 * Screen Target Display KMS Functions
1269 *
1270 * These functions are called by the common KMS code in vmwgfx_kms.c
1271 *****************************************************************************/
1272
1273/**
1274 * vmw_kms_stdu_init_display - Initializes a Screen Target based display
1275 *
1276 * @dev_priv: VMW DRM device
1277 *
1278 * This function initialize a Screen Target based display device. It checks
1279 * the capability bits to make sure the underlying hardware can support
1280 * screen targets, and then creates the maximum number of CRTCs, a.k.a Display
1281 * Units, as supported by the display hardware.
1282 *
1283 * RETURNS:
1284 * 0 on success, error code otherwise
1285 */
1286int vmw_kms_stdu_init_display(struct vmw_private *dev_priv)
1287{
1288 struct drm_device *dev = dev_priv->dev;
1289 int i, ret;
1290
1291
1292 /* Do nothing if Screen Target support is turned off */
1293 if (!VMWGFX_ENABLE_SCREEN_TARGET_OTABLE)
1294 return -ENOSYS;
1295
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001296 if (!(dev_priv->capabilities & SVGA_CAP_GBOBJECTS))
Sinclair Yeh35c05122015-06-26 01:42:06 -07001297 return -ENOSYS;
1298
1299 ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
1300 if (unlikely(ret != 0))
1301 return ret;
1302
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001303 dev_priv->active_display_unit = vmw_du_screen_target;
1304
Thomas Hellstrom76404ac2016-02-12 09:55:45 +01001305 vmw_kms_create_implicit_placement_property(dev_priv, false);
1306
Sinclair Yeh35c05122015-06-26 01:42:06 -07001307 for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i) {
1308 ret = vmw_stdu_init(dev_priv, i);
1309
1310 if (unlikely(ret != 0)) {
1311 DRM_ERROR("Failed to initialize STDU %d", i);
1312 goto err_vblank_cleanup;
1313 }
1314 }
1315
Sinclair Yeh35c05122015-06-26 01:42:06 -07001316 DRM_INFO("Screen Target Display device initialized\n");
1317
1318 return 0;
1319
1320err_vblank_cleanup:
1321 drm_vblank_cleanup(dev);
1322 return ret;
1323}
1324
1325
1326
1327/**
1328 * vmw_kms_stdu_close_display - Cleans up after vmw_kms_stdu_init_display
1329 *
1330 * @dev_priv: VMW DRM device
1331 *
1332 * Frees up any resources allocated by vmw_kms_stdu_init_display
1333 *
1334 * RETURNS:
1335 * 0 on success
1336 */
1337int vmw_kms_stdu_close_display(struct vmw_private *dev_priv)
1338{
1339 struct drm_device *dev = dev_priv->dev;
1340
1341 drm_vblank_cleanup(dev);
1342
1343 return 0;
1344}