blob: 3b8235c7ee42baf1fd56b8dff40f8119fad9cc45 [file] [log] [blame]
Sinclair Yeh35c05122015-06-26 01:42:06 -07001/******************************************************************************
2 *
3 * Copyright © 2014 VMware, Inc., Palo Alto, CA., USA
4 * 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"
29#include "svga3d_surfacedefs.h"
30#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
47
48
49/**
50 * struct vmw_screen_target_display_unit
51 *
52 * @base: VMW specific DU structure
53 * @display_srf: surface to be displayed. The dimension of this will always
54 * match the display mode. If the display mode matches
55 * content_vfbs dimensions, then this is a pointer into the
56 * corresponding field in content_vfbs. If not, then this
57 * is a separate buffer to which content_vfbs will blit to.
58 * @content_fb: holds the rendered content, can be a surface or DMA buffer
59 * @content_type: content_fb type
60 * @defined: true if the current display unit has been initialized
61 */
62struct vmw_screen_target_display_unit {
63 struct vmw_display_unit base;
64
65 struct vmw_surface *display_srf;
66 struct drm_framebuffer *content_fb;
67
68 enum stdu_content_type content_fb_type;
69
70 bool defined;
71};
72
73
74
75static void vmw_stdu_destroy(struct vmw_screen_target_display_unit *stdu);
76
77
78
79/******************************************************************************
80 * Screen Target Display Unit helper Functions
81 *****************************************************************************/
82
83/**
84 * vmw_stdu_pin_display - pins the resource associated with the display surface
85 *
86 * @stdu: contains the display surface
87 *
88 * Since the display surface can either be a private surface allocated by us,
89 * or it can point to the content surface, we use this function to not pin the
90 * same resource twice.
91 */
92static int vmw_stdu_pin_display(struct vmw_screen_target_display_unit *stdu)
93{
94 return vmw_resource_pin(&stdu->display_srf->res);
95}
96
97
98
99/**
100 * vmw_stdu_unpin_display - unpins the resource associated with display surface
101 *
102 * @stdu: contains the display surface
103 *
104 * If the display surface was privatedly allocated by
105 * vmw_surface_gb_priv_define() and not registered as a framebuffer, then it
106 * won't be automatically cleaned up when all the framebuffers are freed. As
107 * such, we have to explicitly call vmw_resource_unreference() to get it freed.
108 */
109static void vmw_stdu_unpin_display(struct vmw_screen_target_display_unit *stdu)
110{
111 if (stdu->display_srf) {
112 struct vmw_resource *res = &stdu->display_srf->res;
113
114 vmw_resource_unpin(res);
115
116 if (stdu->content_fb_type != SAME_AS_DISPLAY) {
117 vmw_resource_unreference(&res);
118 stdu->content_fb_type = SAME_AS_DISPLAY;
119 }
120
121 stdu->display_srf = NULL;
122 }
123}
124
125
126
127/******************************************************************************
128 * Screen Target Display Unit CRTC Functions
129 *****************************************************************************/
130
131
132/**
133 * vmw_stdu_crtc_destroy - cleans up the STDU
134 *
135 * @crtc: used to get a reference to the containing STDU
136 */
137static void vmw_stdu_crtc_destroy(struct drm_crtc *crtc)
138{
139 vmw_stdu_destroy(vmw_crtc_to_stdu(crtc));
140}
141
142
143
144/**
145 * vmw_stdu_content_copy - copies an area from the content to display surface
146 *
147 * @dev_priv: VMW DRM device
148 * @file_priv: Pointer to a drm file private structure
149 * @stdu: STDU whose display surface will be blitted to
150 * @content_x: top/left corner of the content area to blit from
151 * @content_y: top/left corner of the content area to blit from
152 * @width: width of the blit area
153 * @height: height of the blit area
154 * @display_x: top/left corner of the display area to blit to
155 * @display_y: top/left corner of the display area to blit to
156 *
157 * Copies an area from the content surface to the display surface.
158 *
159 * RETURNs:
160 * 0 on success, error code on failure
161 */
162static int vmw_stdu_content_copy(struct vmw_private *dev_priv,
163 struct drm_file *file_priv,
164 struct vmw_screen_target_display_unit *stdu,
165 uint32_t content_x, uint32_t content_y,
166 uint32_t width, uint32_t height,
167 uint32_t display_x, uint32_t display_y)
168{
169 size_t fifo_size;
170 int ret;
171 void *cmd;
172
173 struct vmw_surface_dma {
174 SVGA3dCmdHeader header;
175 SVGA3dCmdSurfaceDMA body;
176 SVGA3dCopyBox area;
177 SVGA3dCmdSurfaceDMASuffix suffix;
178 } surface_dma_cmd;
179
180 struct {
181 SVGA3dCmdHeader header;
182 SVGA3dCmdSurfaceCopy body;
183 SVGA3dCopyBox area;
184 } surface_cpy_cmd;
185
186
187 /*
188 * Can only copy if content and display surfaces exist and are not
189 * the same surface
190 */
191 if (stdu->display_srf == NULL || stdu->content_fb == NULL ||
192 stdu->content_fb_type == SAME_AS_DISPLAY) {
193 return -EINVAL;
194 }
195
196 if (stdu->content_fb_type == SEPARATE_DMA) {
197 struct vmw_framebuffer *content_vfb;
198 struct vmw_framebuffer_dmabuf *content_vfbd;
199 struct vmw_framebuffer_surface *content_vfbs;
200 struct drm_vmw_size cur_size = {0};
201 const struct svga3d_surface_desc *desc;
202 SVGA3dCmdSurfaceDMASuffix *suffix;
203 SVGAGuestPtr ptr;
204
205 content_vfb = vmw_framebuffer_to_vfb(stdu->content_fb);
206 content_vfbd = vmw_framebuffer_to_vfbd(stdu->content_fb);
207 content_vfbs = vmw_framebuffer_to_vfbs(stdu->content_fb);
208
209 cur_size.width = width;
210 cur_size.height = height;
211 cur_size.depth = 1;
212
213 desc = svga3dsurface_get_desc(content_vfbs->surface->format);
214
215
216 fifo_size = sizeof(surface_dma_cmd);
217
218 memset(&surface_dma_cmd, 0, fifo_size);
219
220 ptr.gmrId = content_vfb->user_handle;
221 ptr.offset = 0;
222
223 surface_dma_cmd.header.id = SVGA_3D_CMD_SURFACE_DMA;
224 surface_dma_cmd.header.size = sizeof(surface_dma_cmd.body) +
225 sizeof(surface_dma_cmd.area) +
226 sizeof(surface_dma_cmd.suffix);
227
228 surface_dma_cmd.body.guest.ptr = ptr;
229 surface_dma_cmd.body.guest.pitch = stdu->content_fb->pitches[0];
230 surface_dma_cmd.body.host.sid = stdu->display_srf->res.id;
231 surface_dma_cmd.body.host.face = 0;
232 surface_dma_cmd.body.host.mipmap = 0;
233 surface_dma_cmd.body.transfer = SVGA3D_WRITE_HOST_VRAM;
234
235 surface_dma_cmd.area.srcx = content_x;
236 surface_dma_cmd.area.srcy = content_y;
237 surface_dma_cmd.area.x = display_x;
238 surface_dma_cmd.area.y = display_y;
239 surface_dma_cmd.area.d = 1;
240 surface_dma_cmd.area.w = width;
241 surface_dma_cmd.area.h = height;
242
243 suffix = &surface_dma_cmd.suffix;
244
245 suffix->suffixSize = sizeof(*suffix);
246 suffix->maximumOffset = svga3dsurface_get_image_buffer_size(
247 desc,
248 &cur_size,
249 stdu->content_fb->pitches[0]);
250
251 cmd = (void *) &surface_dma_cmd;
252 } else {
253 struct vmw_framebuffer *content_vfb;
254
255 content_vfb = vmw_framebuffer_to_vfb(stdu->content_fb);
256
257 fifo_size = sizeof(surface_cpy_cmd);
258
259 memset(&surface_cpy_cmd, 0, sizeof(surface_cpy_cmd));
260
261 surface_cpy_cmd.header.id = SVGA_3D_CMD_SURFACE_COPY;
262 surface_cpy_cmd.header.size = sizeof(surface_cpy_cmd.body) +
263 sizeof(surface_cpy_cmd.area);
264
265 surface_cpy_cmd.body.src.sid = content_vfb->user_handle;
266 surface_cpy_cmd.body.dest.sid = stdu->display_srf->res.id;
267
268 surface_cpy_cmd.area.srcx = content_x;
269 surface_cpy_cmd.area.srcy = content_y;
270 surface_cpy_cmd.area.x = display_x;
271 surface_cpy_cmd.area.y = display_y;
272 surface_cpy_cmd.area.d = 1;
273 surface_cpy_cmd.area.w = width;
274 surface_cpy_cmd.area.h = height;
275
276 cmd = (void *) &surface_cpy_cmd;
277 }
278
279 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
280 fifo_size, 0, VMW_QUIRK_SCREENTARGET,
281 NULL, NULL);
282
283 return ret;
284}
285
286
287
288/**
289 * vmw_stdu_define_st - Defines a Screen Target
290 *
291 * @dev_priv: VMW DRM device
292 * @stdu: display unit to create a Screen Target for
293 *
294 * Creates a STDU that we can used later. This function is called whenever the
295 * framebuffer size changes.
296 *
297 * RETURNs:
298 * 0 on success, error code on failure
299 */
300static int vmw_stdu_define_st(struct vmw_private *dev_priv,
301 struct vmw_screen_target_display_unit *stdu)
302{
303 struct {
304 SVGA3dCmdHeader header;
305 SVGA3dCmdDefineGBScreenTarget body;
306 } *cmd;
307
308 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
309
310 if (unlikely(cmd == NULL)) {
311 DRM_ERROR("Out of FIFO space defining Screen Target\n");
312 return -ENOMEM;
313 }
314
315 cmd->header.id = SVGA_3D_CMD_DEFINE_GB_SCREENTARGET;
316 cmd->header.size = sizeof(cmd->body);
317
318 cmd->body.stid = stdu->base.unit;
319 cmd->body.width = stdu->display_srf->base_size.width;
320 cmd->body.height = stdu->display_srf->base_size.height;
321 cmd->body.flags = (0 == cmd->body.stid) ? SVGA_STFLAG_PRIMARY : 0;
322 cmd->body.dpi = 0;
323 cmd->body.xRoot = stdu->base.crtc.x;
324 cmd->body.yRoot = stdu->base.crtc.y;
325
326 if (!stdu->base.is_implicit) {
327 cmd->body.xRoot = stdu->base.gui_x;
328 cmd->body.yRoot = stdu->base.gui_y;
329 }
330
331 vmw_fifo_commit(dev_priv, sizeof(*cmd));
332
333 stdu->defined = true;
334
335 return 0;
336}
337
338
339
340/**
341 * vmw_stdu_bind_st - Binds a surface to a Screen Target
342 *
343 * @dev_priv: VMW DRM device
344 * @stdu: display unit affected
345 * @res: Buffer to bind to the screen target. Set to NULL to blank screen.
346 *
347 * Binding a surface to a Screen Target the same as flipping
348 */
349static int vmw_stdu_bind_st(struct vmw_private *dev_priv,
350 struct vmw_screen_target_display_unit *stdu,
351 struct vmw_resource *res)
352{
353 SVGA3dSurfaceImageId image;
354
355 struct {
356 SVGA3dCmdHeader header;
357 SVGA3dCmdBindGBScreenTarget body;
358 } *cmd;
359
360
361 if (!stdu->defined) {
362 DRM_ERROR("No screen target defined\n");
363 return -EINVAL;
364 }
365
366 /* Set up image using information in vfb */
367 memset(&image, 0, sizeof(image));
368 image.sid = res ? res->id : SVGA3D_INVALID_ID;
369
370 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
371
372 if (unlikely(cmd == NULL)) {
373 DRM_ERROR("Out of FIFO space binding a screen target\n");
374 return -ENOMEM;
375 }
376
377 cmd->header.id = SVGA_3D_CMD_BIND_GB_SCREENTARGET;
378 cmd->header.size = sizeof(cmd->body);
379
380 cmd->body.stid = stdu->base.unit;
381 cmd->body.image = image;
382
383 vmw_fifo_commit(dev_priv, sizeof(*cmd));
384
385 return 0;
386}
387
388
389
390/**
391 * vmw_stdu_update_st - Updates a Screen Target
392 *
393 * @dev_priv: VMW DRM device
394 * @file_priv: Pointer to a drm file private structure
395 * @stdu: display unit affected
396 * @update_area: area that needs to be updated
397 *
398 * This function needs to be called whenever the content of a screen
399 * target changes.
400 * If the display and content buffers are different, then this function does
401 * a blit first from the content buffer to the display buffer before issuing
402 * the Screen Target update command.
403 *
404 * RETURNS:
405 * 0 on success, error code on failure
406 */
407static int vmw_stdu_update_st(struct vmw_private *dev_priv,
408 struct drm_file *file_priv,
409 struct vmw_screen_target_display_unit *stdu,
410 struct drm_clip_rect *update_area)
411{
412 u32 width, height;
413 u32 display_update_x, display_update_y;
414 unsigned short display_x1, display_y1, display_x2, display_y2;
415
416 struct {
417 SVGA3dCmdHeader header;
418 SVGA3dCmdUpdateGBScreenTarget body;
419 } *cmd;
420
421
422 if (!stdu->defined) {
423 DRM_ERROR("No screen target defined");
424 return -EINVAL;
425 }
426
427 /* Display coordinates relative to its position in content surface */
428 display_x1 = stdu->base.crtc.x;
429 display_y1 = stdu->base.crtc.y;
430 display_x2 = display_x1 + stdu->display_srf->base_size.width;
431 display_y2 = display_y1 + stdu->display_srf->base_size.height;
432
433 /* Do nothing if the update area is outside of the display surface */
434 if (update_area->x2 <= display_x1 || update_area->x1 >= display_x2 ||
435 update_area->y2 <= display_y1 || update_area->y1 >= display_y2)
436 return 0;
437
438 /* The top-left hand corner of the update area in display surface */
439 display_update_x = max(update_area->x1 - display_x1, 0);
440 display_update_y = max(update_area->y1 - display_y1, 0);
441
442 width = min(update_area->x2, display_x2) -
443 max(update_area->x1, display_x1);
444 height = min(update_area->y2, display_y2) -
445 max(update_area->y1, display_y1);
446
447 if (file_priv && stdu->content_fb_type != SAME_AS_DISPLAY) {
448 int ret;
449
450 ret = vmw_stdu_content_copy(dev_priv, file_priv,
451 stdu,
452 max(update_area->x1, display_x1),
453 max(update_area->y1, display_y1),
454 width, height,
455 display_update_x, display_update_y);
456 if (unlikely(ret != 0)) {
457 DRM_ERROR("Failed to blit content\n");
458 return ret;
459 }
460 }
461
462 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
463
464 if (unlikely(cmd == NULL)) {
465 DRM_ERROR("Out of FIFO space updating a Screen Target\n");
466 return -ENOMEM;
467 }
468
469 cmd->header.id = SVGA_3D_CMD_UPDATE_GB_SCREENTARGET;
470 cmd->header.size = sizeof(cmd->body);
471
472 cmd->body.stid = stdu->base.unit;
473 cmd->body.rect.x = display_update_x;
474 cmd->body.rect.y = display_update_y;
475 cmd->body.rect.w = width;
476 cmd->body.rect.h = height;
477
478 vmw_fifo_commit(dev_priv, sizeof(*cmd));
479
480 return 0;
481}
482
483
484
485/**
486 * vmw_stdu_destroy_st - Destroy a Screen Target
487 *
488 * @dev_priv: VMW DRM device
489 * @stdu: display unit to destroy
490 */
491static int vmw_stdu_destroy_st(struct vmw_private *dev_priv,
492 struct vmw_screen_target_display_unit *stdu)
493{
494 int ret;
495
496 struct {
497 SVGA3dCmdHeader header;
498 SVGA3dCmdDestroyGBScreenTarget body;
499 } *cmd;
500
501
502 /* Nothing to do if not successfully defined */
503 if (unlikely(!stdu->defined))
504 return 0;
505
506 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
507
508 if (unlikely(cmd == NULL)) {
509 DRM_ERROR("Out of FIFO space, screen target not destroyed\n");
510 return -ENOMEM;
511 }
512
513 cmd->header.id = SVGA_3D_CMD_DESTROY_GB_SCREENTARGET;
514 cmd->header.size = sizeof(cmd->body);
515
516 cmd->body.stid = stdu->base.unit;
517
518 vmw_fifo_commit(dev_priv, sizeof(*cmd));
519
520 /* Force sync */
521 ret = vmw_fallback_wait(dev_priv, false, true, 0, false, 3*HZ);
522 if (unlikely(ret != 0))
523 DRM_ERROR("Failed to sync with HW");
524
525 stdu->defined = false;
526
527 return ret;
528}
529
530
531
532/**
533 * vmw_stdu_crtc_set_config - Sets a mode
534 *
535 * @set: mode parameters
536 *
537 * This function is the device-specific portion of the DRM CRTC mode set.
538 * For the SVGA device, we do this by defining a Screen Target, binding a
539 * GB Surface to that target, and finally update the screen target.
540 *
541 * RETURNS:
542 * 0 on success, error code otherwise
543 */
544static int vmw_stdu_crtc_set_config(struct drm_mode_set *set)
545{
546 struct vmw_private *dev_priv;
547 struct vmw_screen_target_display_unit *stdu;
548 struct vmw_framebuffer *vfb;
549 struct vmw_framebuffer_surface *new_vfbs;
550 struct drm_display_mode *mode;
551 struct drm_framebuffer *new_fb;
552 struct drm_crtc *crtc;
553 struct drm_encoder *encoder;
554 struct drm_connector *connector;
555 struct drm_clip_rect update_area = {0};
556 int ret;
557
558
559 if (!set || !set->crtc)
560 return -EINVAL;
561
562 crtc = set->crtc;
563 crtc->x = set->x;
564 crtc->y = set->y;
565 stdu = vmw_crtc_to_stdu(crtc);
566 mode = set->mode;
567 new_fb = set->fb;
568 dev_priv = vmw_priv(crtc->dev);
569
570
571 if (set->num_connectors > 1) {
572 DRM_ERROR("Too many connectors\n");
573 return -EINVAL;
574 }
575
576 if (set->num_connectors == 1 &&
577 set->connectors[0] != &stdu->base.connector) {
578 DRM_ERROR("Connectors don't match %p %p\n",
579 set->connectors[0], &stdu->base.connector);
580 return -EINVAL;
581 }
582
583
584 /* Since they always map one to one these are safe */
585 connector = &stdu->base.connector;
586 encoder = &stdu->base.encoder;
587
588
589 /*
590 * After this point the CRTC will be considered off unless a new fb
591 * is bound
592 */
593 if (stdu->defined) {
594 /* Unbind current surface by binding an invalid one */
595 ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
596 if (unlikely(ret != 0))
597 return ret;
598
599 /* Update Screen Target, display will now be blank */
600 if (crtc->primary->fb) {
601 update_area.x2 = crtc->primary->fb->width;
602 update_area.y2 = crtc->primary->fb->height;
603
604 ret = vmw_stdu_update_st(dev_priv, NULL,
605 stdu,
606 &update_area);
607 if (unlikely(ret != 0))
608 return ret;
609 }
610
611 crtc->primary->fb = NULL;
612 crtc->enabled = false;
613 encoder->crtc = NULL;
614 connector->encoder = NULL;
615
616 vmw_stdu_unpin_display(stdu);
617 stdu->content_fb = NULL;
618 stdu->content_fb_type = SAME_AS_DISPLAY;
619
620 ret = vmw_stdu_destroy_st(dev_priv, stdu);
621 /* The hardware is hung, give up */
622 if (unlikely(ret != 0))
623 return ret;
624 }
625
626
627 /* Any of these conditions means the caller wants CRTC off */
628 if (set->num_connectors == 0 || !mode || !new_fb)
629 return 0;
630
631
632 if (set->x + mode->hdisplay > new_fb->width ||
633 set->y + mode->vdisplay > new_fb->height) {
634 DRM_ERROR("Set outside of framebuffer\n");
635 return -EINVAL;
636 }
637
638 stdu->content_fb = new_fb;
639 vfb = vmw_framebuffer_to_vfb(stdu->content_fb);
640
641 if (vfb->dmabuf)
642 stdu->content_fb_type = SEPARATE_DMA;
643
644 /*
645 * If the requested mode is different than the width and height
646 * of the FB or if the content buffer is a DMA buf, then allocate
647 * a display FB that matches the dimension of the mode
648 */
649 if (mode->hdisplay != new_fb->width ||
650 mode->vdisplay != new_fb->height ||
651 stdu->content_fb_type != SAME_AS_DISPLAY) {
652 struct vmw_surface content_srf;
653 struct drm_vmw_size display_base_size = {0};
654 struct vmw_surface *display_srf;
655
656
657 display_base_size.width = mode->hdisplay;
658 display_base_size.height = mode->vdisplay;
659 display_base_size.depth = 1;
660
661 /*
662 * If content buffer is a DMA buf, then we have to construct
663 * surface info
664 */
665 if (stdu->content_fb_type == SEPARATE_DMA) {
666
667 switch (new_fb->bits_per_pixel) {
668 case 32:
669 content_srf.format = SVGA3D_X8R8G8B8;
670 break;
671
672 case 16:
673 content_srf.format = SVGA3D_R5G6B5;
674 break;
675
676 case 8:
677 content_srf.format = SVGA3D_P8;
678 break;
679
680 default:
681 DRM_ERROR("Invalid format\n");
682 ret = -EINVAL;
683 goto err_unref_content;
684 }
685
686 content_srf.flags = 0;
687 content_srf.mip_levels[0] = 1;
688 content_srf.multisample_count = 0;
689 } else {
690
691 stdu->content_fb_type = SEPARATE_SURFACE;
692
693 new_vfbs = vmw_framebuffer_to_vfbs(new_fb);
694 content_srf = *new_vfbs->surface;
695 }
696
697
698 ret = vmw_surface_gb_priv_define(crtc->dev,
699 0, /* because kernel visible only */
700 content_srf.flags,
701 content_srf.format,
702 true, /* a scanout buffer */
703 content_srf.mip_levels[0],
704 content_srf.multisample_count,
705 display_base_size,
706 &display_srf);
707 if (unlikely(ret != 0)) {
708 DRM_ERROR("Cannot allocate a display FB.\n");
709 goto err_unref_content;
710 }
711
712 stdu->display_srf = display_srf;
713 } else {
714 new_vfbs = vmw_framebuffer_to_vfbs(new_fb);
715 stdu->display_srf = new_vfbs->surface;
716 }
717
718
719 ret = vmw_stdu_pin_display(stdu);
720 if (unlikely(ret != 0)) {
721 stdu->display_srf = NULL;
722 goto err_unref_content;
723 }
724
725 vmw_fb_off(dev_priv);
726 vmw_svga_enable(dev_priv);
727
728 /*
729 * Steps to displaying a surface, assume surface is already
730 * bound:
731 * 1. define a screen target
732 * 2. bind a fb to the screen target
733 * 3. update that screen target (this is done later by
734 * vmw_kms_stdu_do_surface_dirty_or_present)
735 */
736 ret = vmw_stdu_define_st(dev_priv, stdu);
737 if (unlikely(ret != 0))
738 goto err_unpin_display_and_content;
739
740 ret = vmw_stdu_bind_st(dev_priv, stdu, &stdu->display_srf->res);
741 if (unlikely(ret != 0))
742 goto err_unpin_destroy_st;
743
744
745 connector->encoder = encoder;
746 encoder->crtc = crtc;
747
748 crtc->mode = *mode;
749 crtc->primary->fb = new_fb;
750 crtc->enabled = true;
751
752 return ret;
753
754err_unpin_destroy_st:
755 vmw_stdu_destroy_st(dev_priv, stdu);
756err_unpin_display_and_content:
757 vmw_stdu_unpin_display(stdu);
758err_unref_content:
759 stdu->content_fb = NULL;
760 return ret;
761}
762
763
764
765/**
766 * vmw_stdu_crtc_page_flip - Binds a buffer to a screen target
767 *
768 * @crtc: CRTC to attach FB to
769 * @fb: FB to attach
770 * @event: Event to be posted. This event should've been alloced
771 * using k[mz]alloc, and should've been completely initialized.
772 * @page_flip_flags: Input flags.
773 *
774 * If the STDU uses the same display and content buffers, i.e. a true flip,
775 * this function will replace the existing display buffer with the new content
776 * buffer.
777 *
778 * If the STDU uses different display and content buffers, i.e. a blit, then
779 * only the content buffer will be updated.
780 *
781 * RETURNS:
782 * 0 on success, error code on failure
783 */
784static int vmw_stdu_crtc_page_flip(struct drm_crtc *crtc,
785 struct drm_framebuffer *new_fb,
786 struct drm_pending_vblank_event *event,
787 uint32_t flags)
788
789{
790 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
791 struct vmw_screen_target_display_unit *stdu;
792 struct drm_file *file_priv;
793 struct drm_clip_rect update_area = {0};
794 int ret;
795
796 /*
797 * Temporarily don't support event == NULL. We need the
798 * @file_priv pointer!
799 */
800 if (event == NULL)
801 return -EINVAL;
802
803 if (crtc == NULL)
804 return -EINVAL;
805
806 dev_priv = vmw_priv(crtc->dev);
807 stdu = vmw_crtc_to_stdu(crtc);
808 crtc->primary->fb = new_fb;
809 stdu->content_fb = new_fb;
810
811 if (stdu->display_srf) {
812 update_area.x2 = stdu->display_srf->base_size.width;
813 update_area.y2 = stdu->display_srf->base_size.height;
814
815 /*
816 * If the display surface is the same as the content surface
817 * then remove the reference
818 */
819 if (stdu->content_fb_type == SAME_AS_DISPLAY) {
820 if (stdu->defined) {
821 /* Unbind the current surface */
822 ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
823 if (unlikely(ret != 0))
824 goto err_out;
825 }
826 vmw_stdu_unpin_display(stdu);
827 stdu->display_srf = NULL;
828 }
829 }
830
831
832 if (!new_fb) {
833 /* Blanks the display */
834 (void) vmw_stdu_update_st(dev_priv, NULL, stdu, &update_area);
835
836 return 0;
837 }
838
839
840 if (stdu->content_fb_type == SAME_AS_DISPLAY) {
841 stdu->display_srf = vmw_framebuffer_to_vfbs(new_fb)->surface;
842 ret = vmw_stdu_pin_display(stdu);
843 if (ret) {
844 stdu->display_srf = NULL;
845 goto err_out;
846 }
847
848 /* Bind display surface */
849 ret = vmw_stdu_bind_st(dev_priv, stdu, &stdu->display_srf->res);
850 if (unlikely(ret != 0))
851 goto err_unpin_display_and_content;
852 }
853
854 /* Update display surface: after this point everything is bound */
855 update_area.x2 = stdu->display_srf->base_size.width;
856 update_area.y2 = stdu->display_srf->base_size.height;
857
858 file_priv = event->base.file_priv;
859 ret = vmw_stdu_update_st(dev_priv, file_priv, stdu, &update_area);
860 if (unlikely(ret != 0))
861 return ret;
862
863 if (event) {
864 struct vmw_fence_obj *fence = NULL;
865
866 vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
867 if (!fence)
868 return -ENOMEM;
869
870 ret = vmw_event_fence_action_queue(file_priv, fence,
871 &event->base,
872 &event->event.tv_sec,
873 &event->event.tv_usec,
874 true);
875 vmw_fence_obj_unreference(&fence);
876 }
877
878 return ret;
879
880err_unpin_display_and_content:
881 vmw_stdu_unpin_display(stdu);
882err_out:
883 crtc->primary->fb = NULL;
884 stdu->content_fb = NULL;
885 return ret;
886}
887
888
889
890/*
891 * Screen Target CRTC dispatch table
892 */
893static struct drm_crtc_funcs vmw_stdu_crtc_funcs = {
894 .save = vmw_du_crtc_save,
895 .restore = vmw_du_crtc_restore,
896 .cursor_set = vmw_du_crtc_cursor_set,
897 .cursor_move = vmw_du_crtc_cursor_move,
898 .gamma_set = vmw_du_crtc_gamma_set,
899 .destroy = vmw_stdu_crtc_destroy,
900 .set_config = vmw_stdu_crtc_set_config,
901 .page_flip = vmw_stdu_crtc_page_flip,
902};
903
904
905
906/******************************************************************************
907 * Screen Target Display Unit Encoder Functions
908 *****************************************************************************/
909
910/**
911 * vmw_stdu_encoder_destroy - cleans up the STDU
912 *
913 * @encoder: used the get the containing STDU
914 *
915 * vmwgfx cleans up crtc/encoder/connector all at the same time so technically
916 * this can be a no-op. Nevertheless, it doesn't hurt of have this in case
917 * the common KMS code changes and somehow vmw_stdu_crtc_destroy() doesn't
918 * get called.
919 */
920static void vmw_stdu_encoder_destroy(struct drm_encoder *encoder)
921{
922 vmw_stdu_destroy(vmw_encoder_to_stdu(encoder));
923}
924
925static struct drm_encoder_funcs vmw_stdu_encoder_funcs = {
926 .destroy = vmw_stdu_encoder_destroy,
927};
928
929
930
931/******************************************************************************
932 * Screen Target Display Unit Connector Functions
933 *****************************************************************************/
934
935/**
936 * vmw_stdu_connector_destroy - cleans up the STDU
937 *
938 * @connector: used to get the containing STDU
939 *
940 * vmwgfx cleans up crtc/encoder/connector all at the same time so technically
941 * this can be a no-op. Nevertheless, it doesn't hurt of have this in case
942 * the common KMS code changes and somehow vmw_stdu_crtc_destroy() doesn't
943 * get called.
944 */
945static void vmw_stdu_connector_destroy(struct drm_connector *connector)
946{
947 vmw_stdu_destroy(vmw_connector_to_stdu(connector));
948}
949
950
951
952static struct drm_connector_funcs vmw_stdu_connector_funcs = {
953 .dpms = vmw_du_connector_dpms,
954 .save = vmw_du_connector_save,
955 .restore = vmw_du_connector_restore,
956 .detect = vmw_du_connector_detect,
957 .fill_modes = vmw_du_connector_fill_modes,
958 .set_property = vmw_du_connector_set_property,
959 .destroy = vmw_stdu_connector_destroy,
960};
961
962
963
964/**
965 * vmw_stdu_init - Sets up a Screen Target Display Unit
966 *
967 * @dev_priv: VMW DRM device
968 * @unit: unit number range from 0 to VMWGFX_NUM_DISPLAY_UNITS
969 *
970 * This function is called once per CRTC, and allocates one Screen Target
971 * display unit to represent that CRTC. Since the SVGA device does not separate
972 * out encoder and connector, they are represented as part of the STDU as well.
973 */
974static int vmw_stdu_init(struct vmw_private *dev_priv, unsigned unit)
975{
976 struct vmw_screen_target_display_unit *stdu;
977 struct drm_device *dev = dev_priv->dev;
978 struct drm_connector *connector;
979 struct drm_encoder *encoder;
980 struct drm_crtc *crtc;
981
982
983 stdu = kzalloc(sizeof(*stdu), GFP_KERNEL);
984 if (!stdu)
985 return -ENOMEM;
986
987 stdu->base.unit = unit;
988 crtc = &stdu->base.crtc;
989 encoder = &stdu->base.encoder;
990 connector = &stdu->base.connector;
991
992 stdu->base.pref_active = (unit == 0);
993 stdu->base.pref_width = dev_priv->initial_width;
994 stdu->base.pref_height = dev_priv->initial_height;
995 stdu->base.pref_mode = NULL;
996 stdu->base.is_implicit = true;
997
998 drm_connector_init(dev, connector, &vmw_stdu_connector_funcs,
999 DRM_MODE_CONNECTOR_VIRTUAL);
1000 connector->status = vmw_du_connector_detect(connector, false);
1001
1002 drm_encoder_init(dev, encoder, &vmw_stdu_encoder_funcs,
1003 DRM_MODE_ENCODER_VIRTUAL);
1004 drm_mode_connector_attach_encoder(connector, encoder);
1005 encoder->possible_crtcs = (1 << unit);
1006 encoder->possible_clones = 0;
1007
1008 (void) drm_connector_register(connector);
1009
1010 drm_crtc_init(dev, crtc, &vmw_stdu_crtc_funcs);
1011
1012 drm_mode_crtc_set_gamma_size(crtc, 256);
1013
1014 drm_object_attach_property(&connector->base,
1015 dev->mode_config.dirty_info_property,
1016 1);
1017
1018 return 0;
1019}
1020
1021
1022
1023/**
1024 * vmw_stdu_destroy - Cleans up a vmw_screen_target_display_unit
1025 *
1026 * @stdu: Screen Target Display Unit to be destroyed
1027 *
1028 * Clean up after vmw_stdu_init
1029 */
1030static void vmw_stdu_destroy(struct vmw_screen_target_display_unit *stdu)
1031{
1032 vmw_stdu_unpin_display(stdu);
1033
1034 vmw_du_cleanup(&stdu->base);
1035 kfree(stdu);
1036}
1037
1038
1039
1040/******************************************************************************
1041 * Screen Target Display KMS Functions
1042 *
1043 * These functions are called by the common KMS code in vmwgfx_kms.c
1044 *****************************************************************************/
1045
1046/**
1047 * vmw_kms_stdu_init_display - Initializes a Screen Target based display
1048 *
1049 * @dev_priv: VMW DRM device
1050 *
1051 * This function initialize a Screen Target based display device. It checks
1052 * the capability bits to make sure the underlying hardware can support
1053 * screen targets, and then creates the maximum number of CRTCs, a.k.a Display
1054 * Units, as supported by the display hardware.
1055 *
1056 * RETURNS:
1057 * 0 on success, error code otherwise
1058 */
1059int vmw_kms_stdu_init_display(struct vmw_private *dev_priv)
1060{
1061 struct drm_device *dev = dev_priv->dev;
1062 int i, ret;
1063
1064
1065 /* Do nothing if Screen Target support is turned off */
1066 if (!VMWGFX_ENABLE_SCREEN_TARGET_OTABLE)
1067 return -ENOSYS;
1068
1069 if (!(dev_priv->capabilities & SVGA_CAP_GBOBJECTS) ||
1070 !(dev_priv->capabilities & SVGA_CAP_3D))
1071 return -ENOSYS;
1072
1073 ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
1074 if (unlikely(ret != 0))
1075 return ret;
1076
1077 ret = drm_mode_create_dirty_info_property(dev);
1078 if (unlikely(ret != 0))
1079 goto err_vblank_cleanup;
1080
1081 for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i) {
1082 ret = vmw_stdu_init(dev_priv, i);
1083
1084 if (unlikely(ret != 0)) {
1085 DRM_ERROR("Failed to initialize STDU %d", i);
1086 goto err_vblank_cleanup;
1087 }
1088 }
1089
1090 dev_priv->active_display_unit = vmw_du_screen_target;
1091
1092 DRM_INFO("Screen Target Display device initialized\n");
1093
1094 return 0;
1095
1096err_vblank_cleanup:
1097 drm_vblank_cleanup(dev);
1098 return ret;
1099}
1100
1101
1102
1103/**
1104 * vmw_kms_stdu_close_display - Cleans up after vmw_kms_stdu_init_display
1105 *
1106 * @dev_priv: VMW DRM device
1107 *
1108 * Frees up any resources allocated by vmw_kms_stdu_init_display
1109 *
1110 * RETURNS:
1111 * 0 on success
1112 */
1113int vmw_kms_stdu_close_display(struct vmw_private *dev_priv)
1114{
1115 struct drm_device *dev = dev_priv->dev;
1116
1117 drm_vblank_cleanup(dev);
1118
1119 return 0;
1120}
1121
1122
1123
1124/**
1125 * vmw_kms_stdu_do_surface_dirty - updates a dirty rectange to SVGA device
1126 *
1127 * @dev_priv: VMW DRM device
1128 * @file_priv: Pointer to a drm file private structure
1129 * @framebuffer: FB with the new content to be copied to SVGA device
1130 * @clip_rects: array of dirty rectanges
1131 * @num_of_clip_rects: number of rectanges in @clips
1132 * @increment: increment to the next dirty rect in @clips
1133 *
1134 * This function sends an Update command to the SVGA device. This will notify
1135 * the device that a region needs to be copied to the screen. At this time
1136 * we are not coalescing clip rects into one large clip rect because the SVGA
1137 * device will do it for us.
1138 *
1139 * RETURNS:
1140 * 0 on success, error code otherwise
1141 */
1142int vmw_kms_stdu_do_surface_dirty(struct vmw_private *dev_priv,
1143 struct drm_file *file_priv,
1144 struct vmw_framebuffer *framebuffer,
1145 struct drm_clip_rect *clip_rects,
1146 unsigned num_of_clip_rects, int increment)
1147{
1148 struct vmw_screen_target_display_unit *stdu[VMWGFX_NUM_DISPLAY_UNITS];
1149 struct drm_clip_rect *cur_rect;
1150 struct drm_crtc *crtc;
1151
1152 unsigned num_of_du = 0, cur_du, count = 0;
1153 int ret = 0;
1154
1155
1156 BUG_ON(!clip_rects || !num_of_clip_rects);
1157
1158 /* Figure out all the DU affected by this surface */
1159 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list,
1160 head) {
1161 if (crtc->primary->fb != &framebuffer->base)
1162 continue;
1163
1164 stdu[num_of_du++] = vmw_crtc_to_stdu(crtc);
1165 }
1166
1167 for (cur_du = 0; cur_du < num_of_du; cur_du++)
1168 for (cur_rect = clip_rects, count = 0;
1169 count < num_of_clip_rects && ret == 0;
1170 cur_rect += increment, count++) {
1171 ret = vmw_stdu_update_st(dev_priv, file_priv,
1172 stdu[cur_du],
1173 cur_rect);
1174 }
1175
1176 return ret;
1177}
1178
1179
1180
1181/**
1182 * vmw_kms_stdu_present - present a surface to the display surface
1183 *
1184 * @dev_priv: VMW DRM device
1185 * @file_priv: Pointer to a drm file private structure
1186 * @vfb: Used to pick which STDU(s) is affected
1187 * @user_handle: user handle for the source surface
1188 * @dest_x: top/left corner of the display area to blit to
1189 * @dest_y: top/left corner of the display area to blit to
1190 * @clip_rects: array of dirty rectanges
1191 * @num_of_clip_rects: number of rectanges in @clips
1192 *
1193 * This function copies a surface onto the display surface, and
1194 * updates the screen target. Strech blit is currently not
1195 * supported.
1196 *
1197 * RETURNS:
1198 * 0 on success, error code otherwise
1199 */
1200int vmw_kms_stdu_present(struct vmw_private *dev_priv,
1201 struct drm_file *file_priv,
1202 struct vmw_framebuffer *vfb,
1203 uint32_t user_handle,
1204 int32_t dest_x, int32_t dest_y,
1205 struct drm_vmw_rect *clip_rects,
1206 uint32_t num_of_clip_rects)
1207{
1208 struct vmw_screen_target_display_unit *stdu[VMWGFX_NUM_DISPLAY_UNITS];
1209 struct drm_clip_rect *update_area;
1210 struct drm_crtc *crtc;
1211 size_t fifo_size;
1212 int num_of_du = 0, cur_du, i;
1213 int ret = 0;
1214 struct vmw_clip_rect src_bb;
1215
1216 struct {
1217 SVGA3dCmdHeader header;
1218 SVGA3dCmdSurfaceCopy body;
1219 } *cmd;
1220 SVGA3dCopyBox *blits;
1221
1222
1223 BUG_ON(!clip_rects || !num_of_clip_rects);
1224
1225 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1226 if (crtc->primary->fb != &vfb->base)
1227 continue;
1228
1229 stdu[num_of_du++] = vmw_crtc_to_stdu(crtc);
1230 }
1231
1232
1233 update_area = kcalloc(num_of_clip_rects, sizeof(*update_area),
1234 GFP_KERNEL);
1235 if (unlikely(update_area == NULL)) {
1236 DRM_ERROR("Temporary clip rect memory alloc failed.\n");
1237 return -ENOMEM;
1238 }
1239
1240
1241 fifo_size = sizeof(*cmd) + sizeof(SVGA3dCopyBox) * num_of_clip_rects;
1242
1243 cmd = kmalloc(fifo_size, GFP_KERNEL);
1244 if (unlikely(cmd == NULL)) {
1245 DRM_ERROR("Failed to allocate memory for surface copy.\n");
1246 ret = -ENOMEM;
1247 goto out_free_update_area;
1248 }
1249
1250 memset(cmd, 0, fifo_size);
1251 cmd->header.id = SVGA_3D_CMD_SURFACE_COPY;
1252
1253 blits = (SVGA3dCopyBox *)&cmd[1];
1254
1255
1256 /* Figure out the source bounding box */
1257 src_bb.x1 = clip_rects->x;
1258 src_bb.y1 = clip_rects->y;
1259 src_bb.x2 = clip_rects->x + clip_rects->w;
1260 src_bb.y2 = clip_rects->y + clip_rects->h;
1261
1262 for (i = 1; i < num_of_clip_rects; i++) {
1263 src_bb.x1 = min_t(int, src_bb.x1, clip_rects[i].x);
1264 src_bb.x2 = max_t(int, src_bb.x2,
1265 clip_rects[i].x + (int) clip_rects[i].w);
1266 src_bb.y1 = min_t(int, src_bb.y1, clip_rects[i].y);
1267 src_bb.y2 = max_t(int, src_bb.y2,
1268 clip_rects[i].y + (int) clip_rects[i].h);
1269 }
1270
1271 for (i = 0; i < num_of_clip_rects; i++) {
1272 update_area[i].x1 = clip_rects[i].x - src_bb.x1;
1273 update_area[i].x2 = update_area[i].x1 + clip_rects[i].w;
1274 update_area[i].y1 = clip_rects[i].y - src_bb.y1;
1275 update_area[i].y2 = update_area[i].y1 + clip_rects[i].h;
1276 }
1277
1278
1279 for (cur_du = 0; cur_du < num_of_du; cur_du++) {
1280 struct vmw_clip_rect dest_bb;
1281 int num_of_blits;
1282
1283 crtc = &stdu[cur_du]->base.crtc;
1284
1285 dest_bb.x1 = src_bb.x1 + dest_x - crtc->x;
1286 dest_bb.y1 = src_bb.y1 + dest_y - crtc->y;
1287 dest_bb.x2 = src_bb.x2 + dest_x - crtc->x;
1288 dest_bb.y2 = src_bb.y2 + dest_y - crtc->y;
1289
1290 /* Skip any STDU outside of the destination bounding box */
1291 if (dest_bb.x1 >= crtc->mode.hdisplay ||
1292 dest_bb.y1 >= crtc->mode.vdisplay ||
1293 dest_bb.x2 <= 0 || dest_bb.y2 <= 0)
1294 continue;
1295
1296 /* Normalize to top-left of src bounding box in dest coord */
1297 dest_bb.x2 = crtc->mode.hdisplay - dest_bb.x1;
1298 dest_bb.y2 = crtc->mode.vdisplay - dest_bb.y1;
1299 dest_bb.x1 = 0 - dest_bb.x1;
1300 dest_bb.y1 = 0 - dest_bb.y1;
1301
1302 for (i = 0, num_of_blits = 0; i < num_of_clip_rects; i++) {
1303 int x1 = max_t(int, dest_bb.x1, (int)update_area[i].x1);
1304 int y1 = max_t(int, dest_bb.y1, (int)update_area[i].y1);
1305 int x2 = min_t(int, dest_bb.x2, (int)update_area[i].x2);
1306 int y2 = min_t(int, dest_bb.y2, (int)update_area[i].y2);
1307
1308 if (x1 >= x2)
1309 continue;
1310
1311 if (y1 >= y2)
1312 continue;
1313
1314 blits[num_of_blits].srcx = src_bb.x1 + x1;
1315 blits[num_of_blits].srcy = src_bb.y1 + y1;
1316 blits[num_of_blits].x = -dest_bb.x1 + x1;
1317 blits[num_of_blits].y = -dest_bb.y1 + y1;
1318 blits[num_of_blits].d = 1;
1319 blits[num_of_blits].w = x2 - x1;
1320 blits[num_of_blits].h = y2 - y1;
1321 num_of_blits++;
1322 }
1323
1324 if (num_of_blits == 0)
1325 continue;
1326
1327 /* Calculate new command size */
1328 fifo_size = sizeof(*cmd) + sizeof(SVGA3dCopyBox) * num_of_blits;
1329
1330 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
1331
1332 cmd->body.src.sid = user_handle;
1333 cmd->body.dest.sid = stdu[cur_du]->display_srf->res.id;
1334
1335 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
1336 fifo_size, 0, VMW_QUIRK_SCREENTARGET,
1337 NULL, NULL);
1338
1339 if (unlikely(ret != 0))
1340 break;
1341
1342 for (i = 0; i < num_of_blits; i++) {
1343 struct drm_clip_rect blit_area;
1344
1345 /*
1346 * Add crtc offset because vmw_stdu_update_st expects
1347 * desktop coordinates
1348 */
1349 blit_area.x1 = blits[i].x + crtc->x;
1350 blit_area.x2 = blit_area.x1 + blits[i].w;
1351 blit_area.y1 = blits[i].y + crtc->y;
1352 blit_area.y2 = blit_area.y1 + blits[i].h;
1353 (void) vmw_stdu_update_st(dev_priv, NULL, stdu[cur_du],
1354 &blit_area);
1355 }
1356 }
1357
1358 kfree(cmd);
1359
1360out_free_update_area:
1361 kfree(update_area);
1362
1363 return ret;
1364}