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