blob: 85e12309cb71b17be1527eef7ca1effb3c6b9020 [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>
31
32#define vmw_crtc_to_stdu(x) \
33 container_of(x, struct vmw_screen_target_display_unit, base.crtc)
34#define vmw_encoder_to_stdu(x) \
35 container_of(x, struct vmw_screen_target_display_unit, base.encoder)
36#define vmw_connector_to_stdu(x) \
37 container_of(x, struct vmw_screen_target_display_unit, base.connector)
38
39
40
41enum stdu_content_type {
42 SAME_AS_DISPLAY = 0,
43 SEPARATE_SURFACE,
44 SEPARATE_DMA
45};
46
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -070047/**
48 * struct vmw_stdu_dirty - closure structure for the update functions
49 *
50 * @base: The base type we derive from. Used by vmw_kms_helper_dirty().
51 * @transfer: Transfer direction for DMA command.
52 * @left: Left side of bounding box.
53 * @right: Right side of bounding box.
54 * @top: Top side of bounding box.
55 * @bottom: Bottom side of bounding box.
56 * @buf: DMA buffer when DMA-ing between buffer and screen targets.
57 * @sid: Surface ID when copying between surface and screen targets.
58 */
59struct vmw_stdu_dirty {
60 struct vmw_kms_dirty base;
61 SVGA3dTransferType transfer;
62 s32 left, right, top, bottom;
63 u32 pitch;
64 union {
65 struct vmw_dma_buffer *buf;
66 u32 sid;
67 };
68};
69
70/*
71 * SVGA commands that are used by this code. Please see the device headers
72 * for explanation.
73 */
74struct vmw_stdu_update {
75 SVGA3dCmdHeader header;
76 SVGA3dCmdUpdateGBScreenTarget body;
77};
78
79struct vmw_stdu_dma {
80 SVGA3dCmdHeader header;
81 SVGA3dCmdSurfaceDMA body;
82};
83
84struct vmw_stdu_surface_copy {
85 SVGA3dCmdHeader header;
86 SVGA3dCmdSurfaceCopy body;
87};
Sinclair Yeh35c05122015-06-26 01:42:06 -070088
89
90/**
91 * struct vmw_screen_target_display_unit
92 *
93 * @base: VMW specific DU structure
94 * @display_srf: surface to be displayed. The dimension of this will always
95 * match the display mode. If the display mode matches
96 * content_vfbs dimensions, then this is a pointer into the
97 * corresponding field in content_vfbs. If not, then this
98 * is a separate buffer to which content_vfbs will blit to.
Sinclair Yeh35c05122015-06-26 01:42:06 -070099 * @content_type: content_fb type
100 * @defined: true if the current display unit has been initialized
101 */
102struct vmw_screen_target_display_unit {
103 struct vmw_display_unit base;
104
105 struct vmw_surface *display_srf;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700106 enum stdu_content_type content_fb_type;
107
108 bool defined;
109};
110
111
112
113static void vmw_stdu_destroy(struct vmw_screen_target_display_unit *stdu);
114
115
116
117/******************************************************************************
118 * Screen Target Display Unit helper Functions
119 *****************************************************************************/
120
121/**
Sinclair Yeh35c05122015-06-26 01:42:06 -0700122 * vmw_stdu_unpin_display - unpins the resource associated with display surface
123 *
124 * @stdu: contains the display surface
125 *
126 * If the display surface was privatedly allocated by
127 * vmw_surface_gb_priv_define() and not registered as a framebuffer, then it
128 * won't be automatically cleaned up when all the framebuffers are freed. As
129 * such, we have to explicitly call vmw_resource_unreference() to get it freed.
130 */
131static void vmw_stdu_unpin_display(struct vmw_screen_target_display_unit *stdu)
132{
133 if (stdu->display_srf) {
134 struct vmw_resource *res = &stdu->display_srf->res;
135
136 vmw_resource_unpin(res);
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100137 vmw_surface_unreference(&stdu->display_srf);
Sinclair Yeh35c05122015-06-26 01:42:06 -0700138 }
139}
140
141
142
143/******************************************************************************
144 * Screen Target Display Unit CRTC Functions
145 *****************************************************************************/
146
147
148/**
149 * vmw_stdu_crtc_destroy - cleans up the STDU
150 *
151 * @crtc: used to get a reference to the containing STDU
152 */
153static void vmw_stdu_crtc_destroy(struct drm_crtc *crtc)
154{
155 vmw_stdu_destroy(vmw_crtc_to_stdu(crtc));
156}
157
Sinclair Yeh35c05122015-06-26 01:42:06 -0700158/**
159 * vmw_stdu_define_st - Defines a Screen Target
160 *
161 * @dev_priv: VMW DRM device
162 * @stdu: display unit to create a Screen Target for
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100163 * @mode: The mode to set.
164 * @crtc_x: X coordinate of screen target relative to framebuffer origin.
165 * @crtc_y: Y coordinate of screen target relative to framebuffer origin.
Sinclair Yeh35c05122015-06-26 01:42:06 -0700166 *
167 * Creates a STDU that we can used later. This function is called whenever the
168 * framebuffer size changes.
169 *
170 * RETURNs:
171 * 0 on success, error code on failure
172 */
173static int vmw_stdu_define_st(struct vmw_private *dev_priv,
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100174 struct vmw_screen_target_display_unit *stdu,
175 struct drm_display_mode *mode,
176 int crtc_x, int crtc_y)
Sinclair Yeh35c05122015-06-26 01:42:06 -0700177{
178 struct {
179 SVGA3dCmdHeader header;
180 SVGA3dCmdDefineGBScreenTarget body;
181 } *cmd;
182
183 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
184
185 if (unlikely(cmd == NULL)) {
186 DRM_ERROR("Out of FIFO space defining Screen Target\n");
187 return -ENOMEM;
188 }
189
190 cmd->header.id = SVGA_3D_CMD_DEFINE_GB_SCREENTARGET;
191 cmd->header.size = sizeof(cmd->body);
192
193 cmd->body.stid = stdu->base.unit;
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100194 cmd->body.width = mode->hdisplay;
195 cmd->body.height = mode->vdisplay;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700196 cmd->body.flags = (0 == cmd->body.stid) ? SVGA_STFLAG_PRIMARY : 0;
197 cmd->body.dpi = 0;
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100198 if (stdu->base.is_implicit) {
199 cmd->body.xRoot = crtc_x;
200 cmd->body.yRoot = crtc_y;
201 } else {
Sinclair Yeh35c05122015-06-26 01:42:06 -0700202 cmd->body.xRoot = stdu->base.gui_x;
203 cmd->body.yRoot = stdu->base.gui_y;
204 }
Thomas Hellstrom6dd687b2016-02-12 09:57:15 +0100205 stdu->base.set_gui_x = cmd->body.xRoot;
206 stdu->base.set_gui_y = cmd->body.yRoot;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700207
208 vmw_fifo_commit(dev_priv, sizeof(*cmd));
209
210 stdu->defined = true;
211
212 return 0;
213}
214
215
216
217/**
218 * vmw_stdu_bind_st - Binds a surface to a Screen Target
219 *
220 * @dev_priv: VMW DRM device
221 * @stdu: display unit affected
222 * @res: Buffer to bind to the screen target. Set to NULL to blank screen.
223 *
224 * Binding a surface to a Screen Target the same as flipping
225 */
226static int vmw_stdu_bind_st(struct vmw_private *dev_priv,
227 struct vmw_screen_target_display_unit *stdu,
228 struct vmw_resource *res)
229{
230 SVGA3dSurfaceImageId image;
231
232 struct {
233 SVGA3dCmdHeader header;
234 SVGA3dCmdBindGBScreenTarget body;
235 } *cmd;
236
237
238 if (!stdu->defined) {
239 DRM_ERROR("No screen target defined\n");
240 return -EINVAL;
241 }
242
243 /* Set up image using information in vfb */
244 memset(&image, 0, sizeof(image));
245 image.sid = res ? res->id : SVGA3D_INVALID_ID;
246
247 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
248
249 if (unlikely(cmd == NULL)) {
250 DRM_ERROR("Out of FIFO space binding a screen target\n");
251 return -ENOMEM;
252 }
253
254 cmd->header.id = SVGA_3D_CMD_BIND_GB_SCREENTARGET;
255 cmd->header.size = sizeof(cmd->body);
256
257 cmd->body.stid = stdu->base.unit;
258 cmd->body.image = image;
259
260 vmw_fifo_commit(dev_priv, sizeof(*cmd));
261
262 return 0;
263}
264
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700265/**
266 * vmw_stdu_populate_update - populate an UPDATE_GB_SCREENTARGET command with a
267 * bounding box.
268 *
269 * @cmd: Pointer to command stream.
270 * @unit: Screen target unit.
271 * @left: Left side of bounding box.
272 * @right: Right side of bounding box.
273 * @top: Top side of bounding box.
274 * @bottom: Bottom side of bounding box.
275 */
276static void vmw_stdu_populate_update(void *cmd, int unit,
277 s32 left, s32 right, s32 top, s32 bottom)
278{
279 struct vmw_stdu_update *update = cmd;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700280
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700281 update->header.id = SVGA_3D_CMD_UPDATE_GB_SCREENTARGET;
282 update->header.size = sizeof(update->body);
283
284 update->body.stid = unit;
285 update->body.rect.x = left;
286 update->body.rect.y = top;
287 update->body.rect.w = right - left;
288 update->body.rect.h = bottom - top;
289}
Sinclair Yeh35c05122015-06-26 01:42:06 -0700290
291/**
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700292 * vmw_stdu_update_st - Full update of a Screen Target
Sinclair Yeh35c05122015-06-26 01:42:06 -0700293 *
294 * @dev_priv: VMW DRM device
Sinclair Yeh35c05122015-06-26 01:42:06 -0700295 * @stdu: display unit affected
Sinclair Yeh35c05122015-06-26 01:42:06 -0700296 *
297 * This function needs to be called whenever the content of a screen
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700298 * target has changed completely. Typically as a result of a backing
299 * surface change.
Sinclair Yeh35c05122015-06-26 01:42:06 -0700300 *
301 * RETURNS:
302 * 0 on success, error code on failure
303 */
304static int vmw_stdu_update_st(struct vmw_private *dev_priv,
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700305 struct vmw_screen_target_display_unit *stdu)
Sinclair Yeh35c05122015-06-26 01:42:06 -0700306{
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700307 struct vmw_stdu_update *cmd;
308 struct drm_crtc *crtc = &stdu->base.crtc;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700309
310 if (!stdu->defined) {
311 DRM_ERROR("No screen target defined");
312 return -EINVAL;
313 }
314
Sinclair Yeh35c05122015-06-26 01:42:06 -0700315 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
316
317 if (unlikely(cmd == NULL)) {
318 DRM_ERROR("Out of FIFO space updating a Screen Target\n");
319 return -ENOMEM;
320 }
321
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700322 vmw_stdu_populate_update(cmd, stdu->base.unit, 0, crtc->mode.hdisplay,
323 0, crtc->mode.vdisplay);
Sinclair Yeh35c05122015-06-26 01:42:06 -0700324
325 vmw_fifo_commit(dev_priv, sizeof(*cmd));
326
327 return 0;
328}
329
330
331
332/**
333 * vmw_stdu_destroy_st - Destroy a Screen Target
334 *
335 * @dev_priv: VMW DRM device
336 * @stdu: display unit to destroy
337 */
338static int vmw_stdu_destroy_st(struct vmw_private *dev_priv,
339 struct vmw_screen_target_display_unit *stdu)
340{
341 int ret;
342
343 struct {
344 SVGA3dCmdHeader header;
345 SVGA3dCmdDestroyGBScreenTarget body;
346 } *cmd;
347
348
349 /* Nothing to do if not successfully defined */
350 if (unlikely(!stdu->defined))
351 return 0;
352
353 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
354
355 if (unlikely(cmd == NULL)) {
356 DRM_ERROR("Out of FIFO space, screen target not destroyed\n");
357 return -ENOMEM;
358 }
359
360 cmd->header.id = SVGA_3D_CMD_DESTROY_GB_SCREENTARGET;
361 cmd->header.size = sizeof(cmd->body);
362
363 cmd->body.stid = stdu->base.unit;
364
365 vmw_fifo_commit(dev_priv, sizeof(*cmd));
366
367 /* Force sync */
368 ret = vmw_fallback_wait(dev_priv, false, true, 0, false, 3*HZ);
369 if (unlikely(ret != 0))
370 DRM_ERROR("Failed to sync with HW");
371
372 stdu->defined = false;
373
374 return ret;
375}
376
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100377/**
378 * vmw_stdu_bind_fb - Bind an fb to a defined screen target
379 *
380 * @dev_priv: Pointer to a device private struct.
381 * @crtc: The crtc holding the screen target.
382 * @mode: The mode currently used by the screen target. Must be non-NULL.
383 * @new_fb: The new framebuffer to bind. Must be non-NULL.
384 *
385 * RETURNS:
386 * 0 on success, error code on failure.
387 */
388static int vmw_stdu_bind_fb(struct vmw_private *dev_priv,
389 struct drm_crtc *crtc,
390 struct drm_display_mode *mode,
391 struct drm_framebuffer *new_fb)
392{
393 struct vmw_screen_target_display_unit *stdu = vmw_crtc_to_stdu(crtc);
394 struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(new_fb);
395 struct vmw_surface *new_display_srf = NULL;
396 enum stdu_content_type new_content_type;
397 struct vmw_framebuffer_surface *new_vfbs;
398 int ret;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700399
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100400 WARN_ON_ONCE(!stdu->defined);
401
Thomas Hellstrombeca4cf2016-06-29 13:37:35 -0700402 new_vfbs = (vfb->dmabuf) ? NULL : vmw_framebuffer_to_vfbs(new_fb);
403
404 if (new_vfbs && new_vfbs->surface->base_size.width == mode->hdisplay &&
405 new_vfbs->surface->base_size.height == mode->vdisplay)
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100406 new_content_type = SAME_AS_DISPLAY;
407 else if (vfb->dmabuf)
408 new_content_type = SEPARATE_DMA;
409 else
410 new_content_type = SEPARATE_SURFACE;
411
412 if (new_content_type != SAME_AS_DISPLAY &&
413 !stdu->display_srf) {
414 struct vmw_surface content_srf;
415 struct drm_vmw_size display_base_size = {0};
416
417 display_base_size.width = mode->hdisplay;
418 display_base_size.height = mode->vdisplay;
419 display_base_size.depth = 1;
420
421 /*
422 * If content buffer is a DMA buf, then we have to construct
423 * surface info
424 */
425 if (new_content_type == SEPARATE_DMA) {
426
Ville Syrjälä272725c2016-12-14 23:32:20 +0200427 switch (new_fb->format->cpp[0] * 8) {
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100428 case 32:
429 content_srf.format = SVGA3D_X8R8G8B8;
430 break;
431
432 case 16:
433 content_srf.format = SVGA3D_R5G6B5;
434 break;
435
436 case 8:
437 content_srf.format = SVGA3D_P8;
438 break;
439
440 default:
441 DRM_ERROR("Invalid format\n");
442 return -EINVAL;
443 }
444
445 content_srf.flags = 0;
446 content_srf.mip_levels[0] = 1;
447 content_srf.multisample_count = 0;
448 } else {
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100449 content_srf = *new_vfbs->surface;
450 }
451
452
453 ret = vmw_surface_gb_priv_define(crtc->dev,
454 0, /* because kernel visible only */
455 content_srf.flags,
456 content_srf.format,
457 true, /* a scanout buffer */
458 content_srf.mip_levels[0],
459 content_srf.multisample_count,
460 0,
461 display_base_size,
462 &new_display_srf);
463 if (unlikely(ret != 0)) {
464 DRM_ERROR("Could not allocate screen target surface.\n");
465 return ret;
466 }
467 } else if (new_content_type == SAME_AS_DISPLAY) {
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100468 new_display_srf = vmw_surface_reference(new_vfbs->surface);
469 }
470
471 if (new_display_srf) {
472 /* Pin new surface before flipping */
473 ret = vmw_resource_pin(&new_display_srf->res, false);
474 if (ret)
475 goto out_srf_unref;
476
477 ret = vmw_stdu_bind_st(dev_priv, stdu, &new_display_srf->res);
478 if (ret)
479 goto out_srf_unpin;
480
481 /* Unpin and unreference old surface */
482 vmw_stdu_unpin_display(stdu);
483
484 /* Transfer the reference */
485 stdu->display_srf = new_display_srf;
486 new_display_srf = NULL;
487 }
488
489 crtc->primary->fb = new_fb;
490 stdu->content_fb_type = new_content_type;
491 return 0;
492
493out_srf_unpin:
494 vmw_resource_unpin(&new_display_srf->res);
495out_srf_unref:
496 vmw_surface_unreference(&new_display_srf);
497 return ret;
498}
Sinclair Yeh35c05122015-06-26 01:42:06 -0700499
500/**
501 * vmw_stdu_crtc_set_config - Sets a mode
502 *
503 * @set: mode parameters
504 *
505 * This function is the device-specific portion of the DRM CRTC mode set.
506 * For the SVGA device, we do this by defining a Screen Target, binding a
507 * GB Surface to that target, and finally update the screen target.
508 *
509 * RETURNS:
510 * 0 on success, error code otherwise
511 */
512static int vmw_stdu_crtc_set_config(struct drm_mode_set *set)
513{
514 struct vmw_private *dev_priv;
Thomas Hellstrom4d492a02016-02-12 09:01:29 +0100515 struct vmw_framebuffer *vfb;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700516 struct vmw_screen_target_display_unit *stdu;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700517 struct drm_display_mode *mode;
518 struct drm_framebuffer *new_fb;
519 struct drm_crtc *crtc;
520 struct drm_encoder *encoder;
521 struct drm_connector *connector;
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100522 bool turning_off;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700523 int ret;
524
525
526 if (!set || !set->crtc)
527 return -EINVAL;
528
529 crtc = set->crtc;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700530 stdu = vmw_crtc_to_stdu(crtc);
531 mode = set->mode;
532 new_fb = set->fb;
533 dev_priv = vmw_priv(crtc->dev);
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100534 turning_off = set->num_connectors == 0 || !mode || !new_fb;
Thomas Hellstrom4d492a02016-02-12 09:01:29 +0100535 vfb = (new_fb) ? vmw_framebuffer_to_vfb(new_fb) : NULL;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700536
537 if (set->num_connectors > 1) {
538 DRM_ERROR("Too many connectors\n");
539 return -EINVAL;
540 }
541
542 if (set->num_connectors == 1 &&
543 set->connectors[0] != &stdu->base.connector) {
544 DRM_ERROR("Connectors don't match %p %p\n",
545 set->connectors[0], &stdu->base.connector);
546 return -EINVAL;
547 }
548
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100549 if (!turning_off && (set->x + mode->hdisplay > new_fb->width ||
550 set->y + mode->vdisplay > new_fb->height)) {
551 DRM_ERROR("Set outside of framebuffer\n");
552 return -EINVAL;
553 }
Sinclair Yeh35c05122015-06-26 01:42:06 -0700554
Thomas Hellstrom4d492a02016-02-12 09:01:29 +0100555 /* Only one active implicit frame-buffer at a time. */
Thomas Hellstrom93cd1682016-05-03 11:24:35 +0200556 mutex_lock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom4d492a02016-02-12 09:01:29 +0100557 if (!turning_off && stdu->base.is_implicit && dev_priv->implicit_fb &&
558 !(dev_priv->num_implicit == 1 && stdu->base.active_implicit)
559 && dev_priv->implicit_fb != vfb) {
Thomas Hellstrom93cd1682016-05-03 11:24:35 +0200560 mutex_unlock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom4d492a02016-02-12 09:01:29 +0100561 DRM_ERROR("Multiple implicit framebuffers not supported.\n");
562 return -EINVAL;
563 }
Thomas Hellstrom93cd1682016-05-03 11:24:35 +0200564 mutex_unlock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom4d492a02016-02-12 09:01:29 +0100565
Sinclair Yeh35c05122015-06-26 01:42:06 -0700566 /* Since they always map one to one these are safe */
567 connector = &stdu->base.connector;
568 encoder = &stdu->base.encoder;
569
Sinclair Yeh35c05122015-06-26 01:42:06 -0700570 if (stdu->defined) {
Sinclair Yeh35c05122015-06-26 01:42:06 -0700571 ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100572 if (ret)
Sinclair Yeh35c05122015-06-26 01:42:06 -0700573 return ret;
574
Sinclair Yeh35c05122015-06-26 01:42:06 -0700575 vmw_stdu_unpin_display(stdu);
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100576 (void) vmw_stdu_update_st(dev_priv, stdu);
Thomas Hellstrom4d492a02016-02-12 09:01:29 +0100577 vmw_kms_del_active(dev_priv, &stdu->base);
Sinclair Yeh35c05122015-06-26 01:42:06 -0700578
579 ret = vmw_stdu_destroy_st(dev_priv, stdu);
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100580 if (ret)
Sinclair Yeh35c05122015-06-26 01:42:06 -0700581 return ret;
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100582
583 crtc->primary->fb = NULL;
584 crtc->enabled = false;
585 encoder->crtc = NULL;
586 connector->encoder = NULL;
587 stdu->content_fb_type = SAME_AS_DISPLAY;
588 crtc->x = set->x;
589 crtc->y = set->y;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700590 }
591
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100592 if (turning_off)
Sinclair Yeh35c05122015-06-26 01:42:06 -0700593 return 0;
594
Sinclair Yeh35c05122015-06-26 01:42:06 -0700595 /*
596 * Steps to displaying a surface, assume surface is already
597 * bound:
598 * 1. define a screen target
599 * 2. bind a fb to the screen target
600 * 3. update that screen target (this is done later by
601 * vmw_kms_stdu_do_surface_dirty_or_present)
602 */
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100603 /*
604 * Note on error handling: We can't really restore the crtc to
605 * it's original state on error, but we at least update the
606 * current state to what's submitted to hardware to enable
607 * future recovery.
608 */
609 vmw_svga_enable(dev_priv);
610 ret = vmw_stdu_define_st(dev_priv, stdu, mode, set->x, set->y);
611 if (ret)
612 return ret;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700613
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100614 crtc->x = set->x;
615 crtc->y = set->y;
616 crtc->mode = *mode;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700617
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100618 ret = vmw_stdu_bind_fb(dev_priv, crtc, mode, new_fb);
619 if (ret)
620 return ret;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700621
Thomas Hellstrom4d492a02016-02-12 09:01:29 +0100622 vmw_kms_add_active(dev_priv, &stdu->base, vfb);
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100623 crtc->enabled = true;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700624 connector->encoder = encoder;
625 encoder->crtc = crtc;
626
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100627 return 0;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700628}
629
Sinclair Yeh35c05122015-06-26 01:42:06 -0700630/**
631 * vmw_stdu_crtc_page_flip - Binds a buffer to a screen target
632 *
633 * @crtc: CRTC to attach FB to
634 * @fb: FB to attach
635 * @event: Event to be posted. This event should've been alloced
636 * using k[mz]alloc, and should've been completely initialized.
637 * @page_flip_flags: Input flags.
638 *
639 * If the STDU uses the same display and content buffers, i.e. a true flip,
640 * this function will replace the existing display buffer with the new content
641 * buffer.
642 *
643 * If the STDU uses different display and content buffers, i.e. a blit, then
644 * only the content buffer will be updated.
645 *
646 * RETURNS:
647 * 0 on success, error code on failure
648 */
649static int vmw_stdu_crtc_page_flip(struct drm_crtc *crtc,
650 struct drm_framebuffer *new_fb,
651 struct drm_pending_vblank_event *event,
Daniel Vetter41292b1f2017-03-22 22:50:50 +0100652 uint32_t flags,
653 struct drm_modeset_acquire_ctx *ctx)
Sinclair Yeh35c05122015-06-26 01:42:06 -0700654
655{
656 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
657 struct vmw_screen_target_display_unit *stdu;
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100658 struct drm_vmw_rect vclips;
659 struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(new_fb);
Sinclair Yeh35c05122015-06-26 01:42:06 -0700660 int ret;
661
Sinclair Yeh35c05122015-06-26 01:42:06 -0700662 dev_priv = vmw_priv(crtc->dev);
663 stdu = vmw_crtc_to_stdu(crtc);
Sinclair Yeh35c05122015-06-26 01:42:06 -0700664
Thomas Hellstrom4d492a02016-02-12 09:01:29 +0100665 if (!stdu->defined || !vmw_kms_crtc_flippable(dev_priv, crtc))
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100666 return -EINVAL;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700667
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100668 ret = vmw_stdu_bind_fb(dev_priv, crtc, &crtc->mode, new_fb);
669 if (ret)
670 return ret;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700671
Thomas Hellstrom4d492a02016-02-12 09:01:29 +0100672 if (stdu->base.is_implicit)
673 vmw_kms_update_implicit_fb(dev_priv, crtc);
674
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100675 vclips.x = crtc->x;
676 vclips.y = crtc->y;
677 vclips.w = crtc->mode.hdisplay;
678 vclips.h = crtc->mode.vdisplay;
679 if (vfb->dmabuf)
680 ret = vmw_kms_stdu_dma(dev_priv, NULL, vfb, NULL, NULL, &vclips,
681 1, 1, true, false);
682 else
683 ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, &vclips,
684 NULL, 0, 0, 1, 1, NULL);
685 if (ret)
Sinclair Yeh35c05122015-06-26 01:42:06 -0700686 return ret;
687
688 if (event) {
689 struct vmw_fence_obj *fence = NULL;
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700690 struct drm_file *file_priv = event->base.file_priv;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700691
692 vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
693 if (!fence)
694 return -ENOMEM;
695
696 ret = vmw_event_fence_action_queue(file_priv, fence,
697 &event->base,
698 &event->event.tv_sec,
699 &event->event.tv_usec,
700 true);
701 vmw_fence_obj_unreference(&fence);
Thomas Hellstrom4e0858a2015-11-05 02:18:55 -0800702 } else {
703 vmw_fifo_flush(dev_priv, false);
Sinclair Yeh35c05122015-06-26 01:42:06 -0700704 }
705
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100706 return 0;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700707}
708
709
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700710/**
711 * vmw_stdu_dmabuf_clip - Callback to encode a suface DMA command cliprect
712 *
713 * @dirty: The closure structure.
714 *
715 * Encodes a surface DMA command cliprect and updates the bounding box
716 * for the DMA.
717 */
718static void vmw_stdu_dmabuf_clip(struct vmw_kms_dirty *dirty)
719{
720 struct vmw_stdu_dirty *ddirty =
721 container_of(dirty, struct vmw_stdu_dirty, base);
722 struct vmw_stdu_dma *cmd = dirty->cmd;
723 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
724
725 blit += dirty->num_hits;
726 blit->srcx = dirty->fb_x;
727 blit->srcy = dirty->fb_y;
728 blit->x = dirty->unit_x1;
729 blit->y = dirty->unit_y1;
730 blit->d = 1;
731 blit->w = dirty->unit_x2 - dirty->unit_x1;
732 blit->h = dirty->unit_y2 - dirty->unit_y1;
733 dirty->num_hits++;
734
735 if (ddirty->transfer != SVGA3D_WRITE_HOST_VRAM)
736 return;
737
738 /* Destination bounding box */
739 ddirty->left = min_t(s32, ddirty->left, dirty->unit_x1);
740 ddirty->top = min_t(s32, ddirty->top, dirty->unit_y1);
741 ddirty->right = max_t(s32, ddirty->right, dirty->unit_x2);
742 ddirty->bottom = max_t(s32, ddirty->bottom, dirty->unit_y2);
743}
744
745/**
746 * vmw_stdu_dmabuf_fifo_commit - Callback to fill in and submit a DMA command.
747 *
748 * @dirty: The closure structure.
749 *
750 * Fills in the missing fields in a DMA command, and optionally encodes
751 * a screen target update command, depending on transfer direction.
752 */
753static void vmw_stdu_dmabuf_fifo_commit(struct vmw_kms_dirty *dirty)
754{
755 struct vmw_stdu_dirty *ddirty =
756 container_of(dirty, struct vmw_stdu_dirty, base);
757 struct vmw_screen_target_display_unit *stdu =
758 container_of(dirty->unit, typeof(*stdu), base);
759 struct vmw_stdu_dma *cmd = dirty->cmd;
760 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
761 SVGA3dCmdSurfaceDMASuffix *suffix =
762 (SVGA3dCmdSurfaceDMASuffix *) &blit[dirty->num_hits];
763 size_t blit_size = sizeof(*blit) * dirty->num_hits + sizeof(*suffix);
764
765 if (!dirty->num_hits) {
766 vmw_fifo_commit(dirty->dev_priv, 0);
767 return;
768 }
769
770 cmd->header.id = SVGA_3D_CMD_SURFACE_DMA;
771 cmd->header.size = sizeof(cmd->body) + blit_size;
772 vmw_bo_get_guest_ptr(&ddirty->buf->base, &cmd->body.guest.ptr);
773 cmd->body.guest.pitch = ddirty->pitch;
774 cmd->body.host.sid = stdu->display_srf->res.id;
775 cmd->body.host.face = 0;
776 cmd->body.host.mipmap = 0;
777 cmd->body.transfer = ddirty->transfer;
778 suffix->suffixSize = sizeof(*suffix);
779 suffix->maximumOffset = ddirty->buf->base.num_pages * PAGE_SIZE;
780
781 if (ddirty->transfer == SVGA3D_WRITE_HOST_VRAM) {
782 blit_size += sizeof(struct vmw_stdu_update);
783
784 vmw_stdu_populate_update(&suffix[1], stdu->base.unit,
785 ddirty->left, ddirty->right,
786 ddirty->top, ddirty->bottom);
787 }
788
789 vmw_fifo_commit(dirty->dev_priv, sizeof(*cmd) + blit_size);
790
791 ddirty->left = ddirty->top = S32_MAX;
792 ddirty->right = ddirty->bottom = S32_MIN;
793}
794
795/**
796 * vmw_kms_stdu_dma - Perform a DMA transfer between a dma-buffer backed
797 * framebuffer and the screen target system.
798 *
799 * @dev_priv: Pointer to the device private structure.
800 * @file_priv: Pointer to a struct drm-file identifying the caller. May be
801 * set to NULL, but then @user_fence_rep must also be set to NULL.
802 * @vfb: Pointer to the dma-buffer backed framebuffer.
803 * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
804 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
805 * be NULL.
806 * @num_clips: Number of clip rects in @clips or @vclips.
807 * @increment: Increment to use when looping over @clips or @vclips.
808 * @to_surface: Whether to DMA to the screen target system as opposed to
809 * from the screen target system.
810 * @interruptible: Whether to perform waits interruptible if possible.
811 *
812 * If DMA-ing till the screen target system, the function will also notify
813 * the screen target system that a bounding box of the cliprects has been
814 * updated.
815 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
816 * interrupted.
817 */
818int vmw_kms_stdu_dma(struct vmw_private *dev_priv,
819 struct drm_file *file_priv,
820 struct vmw_framebuffer *vfb,
821 struct drm_vmw_fence_rep __user *user_fence_rep,
822 struct drm_clip_rect *clips,
823 struct drm_vmw_rect *vclips,
824 uint32_t num_clips,
825 int increment,
826 bool to_surface,
827 bool interruptible)
828{
829 struct vmw_dma_buffer *buf =
830 container_of(vfb, struct vmw_framebuffer_dmabuf, base)->buffer;
831 struct vmw_stdu_dirty ddirty;
832 int ret;
833
834 ret = vmw_kms_helper_buffer_prepare(dev_priv, buf, interruptible,
835 false);
836 if (ret)
837 return ret;
838
839 ddirty.transfer = (to_surface) ? SVGA3D_WRITE_HOST_VRAM :
840 SVGA3D_READ_HOST_VRAM;
841 ddirty.left = ddirty.top = S32_MAX;
842 ddirty.right = ddirty.bottom = S32_MIN;
843 ddirty.pitch = vfb->base.pitches[0];
844 ddirty.buf = buf;
845 ddirty.base.fifo_commit = vmw_stdu_dmabuf_fifo_commit;
846 ddirty.base.clip = vmw_stdu_dmabuf_clip;
847 ddirty.base.fifo_reserve_size = sizeof(struct vmw_stdu_dma) +
848 num_clips * sizeof(SVGA3dCopyBox) +
849 sizeof(SVGA3dCmdSurfaceDMASuffix);
850 if (to_surface)
851 ddirty.base.fifo_reserve_size += sizeof(struct vmw_stdu_update);
852
853 ret = vmw_kms_helper_dirty(dev_priv, vfb, clips, vclips,
854 0, 0, num_clips, increment, &ddirty.base);
855 vmw_kms_helper_buffer_finish(dev_priv, file_priv, buf, NULL,
856 user_fence_rep);
857
858 return ret;
859}
860
861/**
862 * vmw_stdu_surface_clip - Callback to encode a surface copy command cliprect
863 *
864 * @dirty: The closure structure.
865 *
866 * Encodes a surface copy command cliprect and updates the bounding box
867 * for the copy.
868 */
869static void vmw_kms_stdu_surface_clip(struct vmw_kms_dirty *dirty)
870{
871 struct vmw_stdu_dirty *sdirty =
872 container_of(dirty, struct vmw_stdu_dirty, base);
873 struct vmw_stdu_surface_copy *cmd = dirty->cmd;
874 struct vmw_screen_target_display_unit *stdu =
875 container_of(dirty->unit, typeof(*stdu), base);
876
877 if (sdirty->sid != stdu->display_srf->res.id) {
878 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
879
880 blit += dirty->num_hits;
881 blit->srcx = dirty->fb_x;
882 blit->srcy = dirty->fb_y;
883 blit->x = dirty->unit_x1;
884 blit->y = dirty->unit_y1;
885 blit->d = 1;
886 blit->w = dirty->unit_x2 - dirty->unit_x1;
887 blit->h = dirty->unit_y2 - dirty->unit_y1;
888 }
889
890 dirty->num_hits++;
891
892 /* Destination bounding box */
893 sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1);
894 sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1);
895 sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2);
896 sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2);
897}
898
899/**
900 * vmw_stdu_surface_fifo_commit - Callback to fill in and submit a surface
901 * copy command.
902 *
903 * @dirty: The closure structure.
904 *
905 * Fills in the missing fields in a surface copy command, and encodes a screen
906 * target update command.
907 */
908static void vmw_kms_stdu_surface_fifo_commit(struct vmw_kms_dirty *dirty)
909{
910 struct vmw_stdu_dirty *sdirty =
911 container_of(dirty, struct vmw_stdu_dirty, base);
912 struct vmw_screen_target_display_unit *stdu =
913 container_of(dirty->unit, typeof(*stdu), base);
914 struct vmw_stdu_surface_copy *cmd = dirty->cmd;
915 struct vmw_stdu_update *update;
916 size_t blit_size = sizeof(SVGA3dCopyBox) * dirty->num_hits;
917 size_t commit_size;
918
919 if (!dirty->num_hits) {
920 vmw_fifo_commit(dirty->dev_priv, 0);
921 return;
922 }
923
924 if (sdirty->sid != stdu->display_srf->res.id) {
925 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
926
927 cmd->header.id = SVGA_3D_CMD_SURFACE_COPY;
928 cmd->header.size = sizeof(cmd->body) + blit_size;
929 cmd->body.src.sid = sdirty->sid;
930 cmd->body.dest.sid = stdu->display_srf->res.id;
931 update = (struct vmw_stdu_update *) &blit[dirty->num_hits];
932 commit_size = sizeof(*cmd) + blit_size + sizeof(*update);
933 } else {
934 update = dirty->cmd;
935 commit_size = sizeof(*update);
936 }
937
938 vmw_stdu_populate_update(update, stdu->base.unit, sdirty->left,
939 sdirty->right, sdirty->top, sdirty->bottom);
940
941 vmw_fifo_commit(dirty->dev_priv, commit_size);
942
943 sdirty->left = sdirty->top = S32_MAX;
944 sdirty->right = sdirty->bottom = S32_MIN;
945}
946
947/**
948 * vmw_kms_stdu_surface_dirty - Dirty part of a surface backed framebuffer
949 *
950 * @dev_priv: Pointer to the device private structure.
951 * @framebuffer: Pointer to the surface-buffer backed framebuffer.
952 * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
953 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
954 * be NULL.
955 * @srf: Pointer to surface to blit from. If NULL, the surface attached
956 * to @framebuffer will be used.
957 * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
958 * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
959 * @num_clips: Number of clip rects in @clips.
960 * @inc: Increment to use when looping over @clips.
961 * @out_fence: If non-NULL, will return a ref-counted pointer to a
962 * struct vmw_fence_obj. The returned fence pointer may be NULL in which
963 * case the device has already synchronized.
964 *
965 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
966 * interrupted.
967 */
968int vmw_kms_stdu_surface_dirty(struct vmw_private *dev_priv,
969 struct vmw_framebuffer *framebuffer,
970 struct drm_clip_rect *clips,
971 struct drm_vmw_rect *vclips,
972 struct vmw_resource *srf,
973 s32 dest_x,
974 s32 dest_y,
975 unsigned num_clips, int inc,
976 struct vmw_fence_obj **out_fence)
977{
978 struct vmw_framebuffer_surface *vfbs =
979 container_of(framebuffer, typeof(*vfbs), base);
980 struct vmw_stdu_dirty sdirty;
981 int ret;
982
983 if (!srf)
984 srf = &vfbs->surface->res;
985
986 ret = vmw_kms_helper_resource_prepare(srf, true);
987 if (ret)
988 return ret;
989
990 if (vfbs->is_dmabuf_proxy) {
991 ret = vmw_kms_update_proxy(srf, clips, num_clips, inc);
992 if (ret)
993 goto out_finish;
994 }
995
996 sdirty.base.fifo_commit = vmw_kms_stdu_surface_fifo_commit;
997 sdirty.base.clip = vmw_kms_stdu_surface_clip;
998 sdirty.base.fifo_reserve_size = sizeof(struct vmw_stdu_surface_copy) +
999 sizeof(SVGA3dCopyBox) * num_clips +
1000 sizeof(struct vmw_stdu_update);
1001 sdirty.sid = srf->id;
1002 sdirty.left = sdirty.top = S32_MAX;
1003 sdirty.right = sdirty.bottom = S32_MIN;
1004
1005 ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
1006 dest_x, dest_y, num_clips, inc,
1007 &sdirty.base);
1008out_finish:
1009 vmw_kms_helper_resource_finish(srf, out_fence);
1010
1011 return ret;
1012}
1013
Sinclair Yeh35c05122015-06-26 01:42:06 -07001014
1015/*
1016 * Screen Target CRTC dispatch table
1017 */
Ville Syrjäläd7955fc2015-12-15 12:21:15 +01001018static const struct drm_crtc_funcs vmw_stdu_crtc_funcs = {
Thomas Hellstrom8fbf9d92015-11-26 19:45:16 +01001019 .cursor_set2 = vmw_du_crtc_cursor_set2,
Sinclair Yeh35c05122015-06-26 01:42:06 -07001020 .cursor_move = vmw_du_crtc_cursor_move,
1021 .gamma_set = vmw_du_crtc_gamma_set,
1022 .destroy = vmw_stdu_crtc_destroy,
1023 .set_config = vmw_stdu_crtc_set_config,
1024 .page_flip = vmw_stdu_crtc_page_flip,
1025};
1026
1027
1028
1029/******************************************************************************
1030 * Screen Target Display Unit Encoder Functions
1031 *****************************************************************************/
1032
1033/**
1034 * vmw_stdu_encoder_destroy - cleans up the STDU
1035 *
1036 * @encoder: used the get the containing STDU
1037 *
1038 * vmwgfx cleans up crtc/encoder/connector all at the same time so technically
1039 * this can be a no-op. Nevertheless, it doesn't hurt of have this in case
1040 * the common KMS code changes and somehow vmw_stdu_crtc_destroy() doesn't
1041 * get called.
1042 */
1043static void vmw_stdu_encoder_destroy(struct drm_encoder *encoder)
1044{
1045 vmw_stdu_destroy(vmw_encoder_to_stdu(encoder));
1046}
1047
Ville Syrjäläd7955fc2015-12-15 12:21:15 +01001048static const struct drm_encoder_funcs vmw_stdu_encoder_funcs = {
Sinclair Yeh35c05122015-06-26 01:42:06 -07001049 .destroy = vmw_stdu_encoder_destroy,
1050};
1051
1052
1053
1054/******************************************************************************
1055 * Screen Target Display Unit Connector Functions
1056 *****************************************************************************/
1057
1058/**
1059 * vmw_stdu_connector_destroy - cleans up the STDU
1060 *
1061 * @connector: used to get the containing STDU
1062 *
1063 * vmwgfx cleans up crtc/encoder/connector all at the same time so technically
1064 * this can be a no-op. Nevertheless, it doesn't hurt of have this in case
1065 * the common KMS code changes and somehow vmw_stdu_crtc_destroy() doesn't
1066 * get called.
1067 */
1068static void vmw_stdu_connector_destroy(struct drm_connector *connector)
1069{
1070 vmw_stdu_destroy(vmw_connector_to_stdu(connector));
1071}
1072
1073
1074
Ville Syrjäläd7955fc2015-12-15 12:21:15 +01001075static const struct drm_connector_funcs vmw_stdu_connector_funcs = {
Sinclair Yeh35c05122015-06-26 01:42:06 -07001076 .dpms = vmw_du_connector_dpms,
Sinclair Yeh35c05122015-06-26 01:42:06 -07001077 .detect = vmw_du_connector_detect,
1078 .fill_modes = vmw_du_connector_fill_modes,
1079 .set_property = vmw_du_connector_set_property,
1080 .destroy = vmw_stdu_connector_destroy,
1081};
1082
1083
1084
1085/**
1086 * vmw_stdu_init - Sets up a Screen Target Display Unit
1087 *
1088 * @dev_priv: VMW DRM device
1089 * @unit: unit number range from 0 to VMWGFX_NUM_DISPLAY_UNITS
1090 *
1091 * This function is called once per CRTC, and allocates one Screen Target
1092 * display unit to represent that CRTC. Since the SVGA device does not separate
1093 * out encoder and connector, they are represented as part of the STDU as well.
1094 */
1095static int vmw_stdu_init(struct vmw_private *dev_priv, unsigned unit)
1096{
1097 struct vmw_screen_target_display_unit *stdu;
1098 struct drm_device *dev = dev_priv->dev;
1099 struct drm_connector *connector;
1100 struct drm_encoder *encoder;
1101 struct drm_crtc *crtc;
1102
1103
1104 stdu = kzalloc(sizeof(*stdu), GFP_KERNEL);
1105 if (!stdu)
1106 return -ENOMEM;
1107
1108 stdu->base.unit = unit;
1109 crtc = &stdu->base.crtc;
1110 encoder = &stdu->base.encoder;
1111 connector = &stdu->base.connector;
1112
1113 stdu->base.pref_active = (unit == 0);
1114 stdu->base.pref_width = dev_priv->initial_width;
1115 stdu->base.pref_height = dev_priv->initial_height;
Thomas Hellstrom2e69b252016-02-12 09:59:50 +01001116 stdu->base.is_implicit = false;
Sinclair Yeh35c05122015-06-26 01:42:06 -07001117
1118 drm_connector_init(dev, connector, &vmw_stdu_connector_funcs,
1119 DRM_MODE_CONNECTOR_VIRTUAL);
1120 connector->status = vmw_du_connector_detect(connector, false);
1121
1122 drm_encoder_init(dev, encoder, &vmw_stdu_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001123 DRM_MODE_ENCODER_VIRTUAL, NULL);
Sinclair Yeh35c05122015-06-26 01:42:06 -07001124 drm_mode_connector_attach_encoder(connector, encoder);
1125 encoder->possible_crtcs = (1 << unit);
1126 encoder->possible_clones = 0;
1127
1128 (void) drm_connector_register(connector);
1129
1130 drm_crtc_init(dev, crtc, &vmw_stdu_crtc_funcs);
1131
1132 drm_mode_crtc_set_gamma_size(crtc, 256);
1133
1134 drm_object_attach_property(&connector->base,
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001135 dev_priv->hotplug_mode_update_property, 1);
1136 drm_object_attach_property(&connector->base,
1137 dev->mode_config.suggested_x_property, 0);
1138 drm_object_attach_property(&connector->base,
1139 dev->mode_config.suggested_y_property, 0);
Thomas Hellstrom76404ac2016-02-12 09:55:45 +01001140 if (dev_priv->implicit_placement_property)
1141 drm_object_attach_property
1142 (&connector->base,
1143 dev_priv->implicit_placement_property,
1144 stdu->base.is_implicit);
Sinclair Yeh35c05122015-06-26 01:42:06 -07001145 return 0;
1146}
1147
1148
1149
1150/**
1151 * vmw_stdu_destroy - Cleans up a vmw_screen_target_display_unit
1152 *
1153 * @stdu: Screen Target Display Unit to be destroyed
1154 *
1155 * Clean up after vmw_stdu_init
1156 */
1157static void vmw_stdu_destroy(struct vmw_screen_target_display_unit *stdu)
1158{
1159 vmw_stdu_unpin_display(stdu);
1160
1161 vmw_du_cleanup(&stdu->base);
1162 kfree(stdu);
1163}
1164
1165
1166
1167/******************************************************************************
1168 * Screen Target Display KMS Functions
1169 *
1170 * These functions are called by the common KMS code in vmwgfx_kms.c
1171 *****************************************************************************/
1172
1173/**
1174 * vmw_kms_stdu_init_display - Initializes a Screen Target based display
1175 *
1176 * @dev_priv: VMW DRM device
1177 *
1178 * This function initialize a Screen Target based display device. It checks
1179 * the capability bits to make sure the underlying hardware can support
1180 * screen targets, and then creates the maximum number of CRTCs, a.k.a Display
1181 * Units, as supported by the display hardware.
1182 *
1183 * RETURNS:
1184 * 0 on success, error code otherwise
1185 */
1186int vmw_kms_stdu_init_display(struct vmw_private *dev_priv)
1187{
1188 struct drm_device *dev = dev_priv->dev;
1189 int i, ret;
1190
1191
1192 /* Do nothing if Screen Target support is turned off */
1193 if (!VMWGFX_ENABLE_SCREEN_TARGET_OTABLE)
1194 return -ENOSYS;
1195
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001196 if (!(dev_priv->capabilities & SVGA_CAP_GBOBJECTS))
Sinclair Yeh35c05122015-06-26 01:42:06 -07001197 return -ENOSYS;
1198
1199 ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
1200 if (unlikely(ret != 0))
1201 return ret;
1202
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001203 dev_priv->active_display_unit = vmw_du_screen_target;
1204
Thomas Hellstrom76404ac2016-02-12 09:55:45 +01001205 vmw_kms_create_implicit_placement_property(dev_priv, false);
1206
Sinclair Yeh35c05122015-06-26 01:42:06 -07001207 for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i) {
1208 ret = vmw_stdu_init(dev_priv, i);
1209
1210 if (unlikely(ret != 0)) {
1211 DRM_ERROR("Failed to initialize STDU %d", i);
1212 goto err_vblank_cleanup;
1213 }
1214 }
1215
Sinclair Yeh35c05122015-06-26 01:42:06 -07001216 DRM_INFO("Screen Target Display device initialized\n");
1217
1218 return 0;
1219
1220err_vblank_cleanup:
1221 drm_vblank_cleanup(dev);
1222 return ret;
1223}
1224
1225
1226
1227/**
1228 * vmw_kms_stdu_close_display - Cleans up after vmw_kms_stdu_init_display
1229 *
1230 * @dev_priv: VMW DRM device
1231 *
1232 * Frees up any resources allocated by vmw_kms_stdu_init_display
1233 *
1234 * RETURNS:
1235 * 0 on success
1236 */
1237int vmw_kms_stdu_close_display(struct vmw_private *dev_priv)
1238{
1239 struct drm_device *dev = dev_priv->dev;
1240
1241 drm_vblank_cleanup(dev);
1242
1243 return 0;
1244}