drm/vmwgfx: Update device includes for DX device functionality
[deliverable/linux.git] / drivers / gpu / drm / vmwgfx / vmwgfx_stdu.c
CommitLineData
35c05125
SY
1/******************************************************************************
2 *
6bf6bf03 3 * COPYRIGHT © 2014 VMware, Inc., Palo Alto, CA., USA
35c05125
SY
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"
8ce75f8a 29#include "device_include/svga3d_surfacedefs.h"
35c05125
SY
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
6bf6bf03
TH
47/**
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};
35c05125
SY
88
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.
99 * @content_fb: holds the rendered content, can be a surface or DMA buffer
100 * @content_type: content_fb type
101 * @defined: true if the current display unit has been initialized
102 */
103struct vmw_screen_target_display_unit {
104 struct vmw_display_unit base;
105
106 struct vmw_surface *display_srf;
107 struct drm_framebuffer *content_fb;
108
109 enum stdu_content_type content_fb_type;
110
111 bool defined;
112};
113
114
115
116static void vmw_stdu_destroy(struct vmw_screen_target_display_unit *stdu);
117
118
119
120/******************************************************************************
121 * Screen Target Display Unit helper Functions
122 *****************************************************************************/
123
124/**
125 * vmw_stdu_pin_display - pins the resource associated with the display surface
126 *
127 * @stdu: contains the display surface
128 *
129 * Since the display surface can either be a private surface allocated by us,
130 * or it can point to the content surface, we use this function to not pin the
131 * same resource twice.
132 */
133static int vmw_stdu_pin_display(struct vmw_screen_target_display_unit *stdu)
134{
1a4b172a 135 return vmw_resource_pin(&stdu->display_srf->res, false);
35c05125
SY
136}
137
138
139
140/**
141 * vmw_stdu_unpin_display - unpins the resource associated with display surface
142 *
143 * @stdu: contains the display surface
144 *
145 * If the display surface was privatedly allocated by
146 * vmw_surface_gb_priv_define() and not registered as a framebuffer, then it
147 * won't be automatically cleaned up when all the framebuffers are freed. As
148 * such, we have to explicitly call vmw_resource_unreference() to get it freed.
149 */
150static void vmw_stdu_unpin_display(struct vmw_screen_target_display_unit *stdu)
151{
152 if (stdu->display_srf) {
153 struct vmw_resource *res = &stdu->display_srf->res;
154
155 vmw_resource_unpin(res);
156
157 if (stdu->content_fb_type != SAME_AS_DISPLAY) {
158 vmw_resource_unreference(&res);
159 stdu->content_fb_type = SAME_AS_DISPLAY;
160 }
161
162 stdu->display_srf = NULL;
163 }
164}
165
166
167
168/******************************************************************************
169 * Screen Target Display Unit CRTC Functions
170 *****************************************************************************/
171
172
173/**
174 * vmw_stdu_crtc_destroy - cleans up the STDU
175 *
176 * @crtc: used to get a reference to the containing STDU
177 */
178static void vmw_stdu_crtc_destroy(struct drm_crtc *crtc)
179{
180 vmw_stdu_destroy(vmw_crtc_to_stdu(crtc));
181}
182
35c05125
SY
183/**
184 * vmw_stdu_define_st - Defines a Screen Target
185 *
186 * @dev_priv: VMW DRM device
187 * @stdu: display unit to create a Screen Target for
188 *
189 * Creates a STDU that we can used later. This function is called whenever the
190 * framebuffer size changes.
191 *
192 * RETURNs:
193 * 0 on success, error code on failure
194 */
195static int vmw_stdu_define_st(struct vmw_private *dev_priv,
196 struct vmw_screen_target_display_unit *stdu)
197{
198 struct {
199 SVGA3dCmdHeader header;
200 SVGA3dCmdDefineGBScreenTarget body;
201 } *cmd;
202
203 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
204
205 if (unlikely(cmd == NULL)) {
206 DRM_ERROR("Out of FIFO space defining Screen Target\n");
207 return -ENOMEM;
208 }
209
210 cmd->header.id = SVGA_3D_CMD_DEFINE_GB_SCREENTARGET;
211 cmd->header.size = sizeof(cmd->body);
212
213 cmd->body.stid = stdu->base.unit;
214 cmd->body.width = stdu->display_srf->base_size.width;
215 cmd->body.height = stdu->display_srf->base_size.height;
216 cmd->body.flags = (0 == cmd->body.stid) ? SVGA_STFLAG_PRIMARY : 0;
217 cmd->body.dpi = 0;
218 cmd->body.xRoot = stdu->base.crtc.x;
219 cmd->body.yRoot = stdu->base.crtc.y;
220
221 if (!stdu->base.is_implicit) {
222 cmd->body.xRoot = stdu->base.gui_x;
223 cmd->body.yRoot = stdu->base.gui_y;
224 }
225
226 vmw_fifo_commit(dev_priv, sizeof(*cmd));
227
228 stdu->defined = true;
229
230 return 0;
231}
232
233
234
235/**
236 * vmw_stdu_bind_st - Binds a surface to a Screen Target
237 *
238 * @dev_priv: VMW DRM device
239 * @stdu: display unit affected
240 * @res: Buffer to bind to the screen target. Set to NULL to blank screen.
241 *
242 * Binding a surface to a Screen Target the same as flipping
243 */
244static int vmw_stdu_bind_st(struct vmw_private *dev_priv,
245 struct vmw_screen_target_display_unit *stdu,
246 struct vmw_resource *res)
247{
248 SVGA3dSurfaceImageId image;
249
250 struct {
251 SVGA3dCmdHeader header;
252 SVGA3dCmdBindGBScreenTarget body;
253 } *cmd;
254
255
256 if (!stdu->defined) {
257 DRM_ERROR("No screen target defined\n");
258 return -EINVAL;
259 }
260
261 /* Set up image using information in vfb */
262 memset(&image, 0, sizeof(image));
263 image.sid = res ? res->id : SVGA3D_INVALID_ID;
264
265 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
266
267 if (unlikely(cmd == NULL)) {
268 DRM_ERROR("Out of FIFO space binding a screen target\n");
269 return -ENOMEM;
270 }
271
272 cmd->header.id = SVGA_3D_CMD_BIND_GB_SCREENTARGET;
273 cmd->header.size = sizeof(cmd->body);
274
275 cmd->body.stid = stdu->base.unit;
276 cmd->body.image = image;
277
278 vmw_fifo_commit(dev_priv, sizeof(*cmd));
279
280 return 0;
281}
282
6bf6bf03
TH
283/**
284 * vmw_stdu_populate_update - populate an UPDATE_GB_SCREENTARGET command with a
285 * bounding box.
286 *
287 * @cmd: Pointer to command stream.
288 * @unit: Screen target unit.
289 * @left: Left side of bounding box.
290 * @right: Right side of bounding box.
291 * @top: Top side of bounding box.
292 * @bottom: Bottom side of bounding box.
293 */
294static void vmw_stdu_populate_update(void *cmd, int unit,
295 s32 left, s32 right, s32 top, s32 bottom)
296{
297 struct vmw_stdu_update *update = cmd;
298
299 update->header.id = SVGA_3D_CMD_UPDATE_GB_SCREENTARGET;
300 update->header.size = sizeof(update->body);
35c05125 301
6bf6bf03
TH
302 update->body.stid = unit;
303 update->body.rect.x = left;
304 update->body.rect.y = top;
305 update->body.rect.w = right - left;
306 update->body.rect.h = bottom - top;
307}
35c05125
SY
308
309/**
6bf6bf03 310 * vmw_stdu_update_st - Full update of a Screen Target
35c05125
SY
311 *
312 * @dev_priv: VMW DRM device
35c05125 313 * @stdu: display unit affected
35c05125
SY
314 *
315 * This function needs to be called whenever the content of a screen
6bf6bf03
TH
316 * target has changed completely. Typically as a result of a backing
317 * surface change.
35c05125
SY
318 *
319 * RETURNS:
320 * 0 on success, error code on failure
321 */
322static int vmw_stdu_update_st(struct vmw_private *dev_priv,
6bf6bf03 323 struct vmw_screen_target_display_unit *stdu)
35c05125 324{
6bf6bf03
TH
325 struct vmw_stdu_update *cmd;
326 struct drm_crtc *crtc = &stdu->base.crtc;
35c05125
SY
327
328 if (!stdu->defined) {
329 DRM_ERROR("No screen target defined");
330 return -EINVAL;
331 }
332
35c05125
SY
333 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
334
335 if (unlikely(cmd == NULL)) {
336 DRM_ERROR("Out of FIFO space updating a Screen Target\n");
337 return -ENOMEM;
338 }
339
6bf6bf03
TH
340 vmw_stdu_populate_update(cmd, stdu->base.unit, 0, crtc->mode.hdisplay,
341 0, crtc->mode.vdisplay);
35c05125
SY
342
343 vmw_fifo_commit(dev_priv, sizeof(*cmd));
344
345 return 0;
346}
347
348
349
350/**
351 * vmw_stdu_destroy_st - Destroy a Screen Target
352 *
353 * @dev_priv: VMW DRM device
354 * @stdu: display unit to destroy
355 */
356static int vmw_stdu_destroy_st(struct vmw_private *dev_priv,
357 struct vmw_screen_target_display_unit *stdu)
358{
359 int ret;
360
361 struct {
362 SVGA3dCmdHeader header;
363 SVGA3dCmdDestroyGBScreenTarget body;
364 } *cmd;
365
366
367 /* Nothing to do if not successfully defined */
368 if (unlikely(!stdu->defined))
369 return 0;
370
371 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
372
373 if (unlikely(cmd == NULL)) {
374 DRM_ERROR("Out of FIFO space, screen target not destroyed\n");
375 return -ENOMEM;
376 }
377
378 cmd->header.id = SVGA_3D_CMD_DESTROY_GB_SCREENTARGET;
379 cmd->header.size = sizeof(cmd->body);
380
381 cmd->body.stid = stdu->base.unit;
382
383 vmw_fifo_commit(dev_priv, sizeof(*cmd));
384
385 /* Force sync */
386 ret = vmw_fallback_wait(dev_priv, false, true, 0, false, 3*HZ);
387 if (unlikely(ret != 0))
388 DRM_ERROR("Failed to sync with HW");
389
390 stdu->defined = false;
391
392 return ret;
393}
394
395
396
397/**
398 * vmw_stdu_crtc_set_config - Sets a mode
399 *
400 * @set: mode parameters
401 *
402 * This function is the device-specific portion of the DRM CRTC mode set.
403 * For the SVGA device, we do this by defining a Screen Target, binding a
404 * GB Surface to that target, and finally update the screen target.
405 *
406 * RETURNS:
407 * 0 on success, error code otherwise
408 */
409static int vmw_stdu_crtc_set_config(struct drm_mode_set *set)
410{
411 struct vmw_private *dev_priv;
412 struct vmw_screen_target_display_unit *stdu;
413 struct vmw_framebuffer *vfb;
414 struct vmw_framebuffer_surface *new_vfbs;
415 struct drm_display_mode *mode;
416 struct drm_framebuffer *new_fb;
417 struct drm_crtc *crtc;
418 struct drm_encoder *encoder;
419 struct drm_connector *connector;
35c05125
SY
420 int ret;
421
422
423 if (!set || !set->crtc)
424 return -EINVAL;
425
426 crtc = set->crtc;
427 crtc->x = set->x;
428 crtc->y = set->y;
429 stdu = vmw_crtc_to_stdu(crtc);
430 mode = set->mode;
431 new_fb = set->fb;
432 dev_priv = vmw_priv(crtc->dev);
433
434
435 if (set->num_connectors > 1) {
436 DRM_ERROR("Too many connectors\n");
437 return -EINVAL;
438 }
439
440 if (set->num_connectors == 1 &&
441 set->connectors[0] != &stdu->base.connector) {
442 DRM_ERROR("Connectors don't match %p %p\n",
443 set->connectors[0], &stdu->base.connector);
444 return -EINVAL;
445 }
446
447
448 /* Since they always map one to one these are safe */
449 connector = &stdu->base.connector;
450 encoder = &stdu->base.encoder;
451
452
453 /*
454 * After this point the CRTC will be considered off unless a new fb
455 * is bound
456 */
457 if (stdu->defined) {
458 /* Unbind current surface by binding an invalid one */
459 ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
460 if (unlikely(ret != 0))
461 return ret;
462
463 /* Update Screen Target, display will now be blank */
464 if (crtc->primary->fb) {
6bf6bf03 465 vmw_stdu_update_st(dev_priv, stdu);
35c05125
SY
466 if (unlikely(ret != 0))
467 return ret;
468 }
469
470 crtc->primary->fb = NULL;
471 crtc->enabled = false;
472 encoder->crtc = NULL;
473 connector->encoder = NULL;
474
475 vmw_stdu_unpin_display(stdu);
476 stdu->content_fb = NULL;
477 stdu->content_fb_type = SAME_AS_DISPLAY;
478
479 ret = vmw_stdu_destroy_st(dev_priv, stdu);
480 /* The hardware is hung, give up */
481 if (unlikely(ret != 0))
482 return ret;
483 }
484
485
486 /* Any of these conditions means the caller wants CRTC off */
487 if (set->num_connectors == 0 || !mode || !new_fb)
488 return 0;
489
490
491 if (set->x + mode->hdisplay > new_fb->width ||
492 set->y + mode->vdisplay > new_fb->height) {
493 DRM_ERROR("Set outside of framebuffer\n");
494 return -EINVAL;
495 }
496
497 stdu->content_fb = new_fb;
498 vfb = vmw_framebuffer_to_vfb(stdu->content_fb);
499
500 if (vfb->dmabuf)
501 stdu->content_fb_type = SEPARATE_DMA;
502
503 /*
504 * If the requested mode is different than the width and height
505 * of the FB or if the content buffer is a DMA buf, then allocate
506 * a display FB that matches the dimension of the mode
507 */
508 if (mode->hdisplay != new_fb->width ||
509 mode->vdisplay != new_fb->height ||
510 stdu->content_fb_type != SAME_AS_DISPLAY) {
511 struct vmw_surface content_srf;
512 struct drm_vmw_size display_base_size = {0};
513 struct vmw_surface *display_srf;
514
515
516 display_base_size.width = mode->hdisplay;
517 display_base_size.height = mode->vdisplay;
518 display_base_size.depth = 1;
519
520 /*
521 * If content buffer is a DMA buf, then we have to construct
522 * surface info
523 */
524 if (stdu->content_fb_type == SEPARATE_DMA) {
525
526 switch (new_fb->bits_per_pixel) {
527 case 32:
528 content_srf.format = SVGA3D_X8R8G8B8;
529 break;
530
531 case 16:
532 content_srf.format = SVGA3D_R5G6B5;
533 break;
534
535 case 8:
536 content_srf.format = SVGA3D_P8;
537 break;
538
539 default:
540 DRM_ERROR("Invalid format\n");
541 ret = -EINVAL;
542 goto err_unref_content;
543 }
544
545 content_srf.flags = 0;
546 content_srf.mip_levels[0] = 1;
547 content_srf.multisample_count = 0;
548 } else {
549
550 stdu->content_fb_type = SEPARATE_SURFACE;
551
552 new_vfbs = vmw_framebuffer_to_vfbs(new_fb);
553 content_srf = *new_vfbs->surface;
554 }
555
556
557 ret = vmw_surface_gb_priv_define(crtc->dev,
558 0, /* because kernel visible only */
559 content_srf.flags,
560 content_srf.format,
561 true, /* a scanout buffer */
562 content_srf.mip_levels[0],
563 content_srf.multisample_count,
564 display_base_size,
565 &display_srf);
566 if (unlikely(ret != 0)) {
567 DRM_ERROR("Cannot allocate a display FB.\n");
568 goto err_unref_content;
569 }
570
571 stdu->display_srf = display_srf;
572 } else {
573 new_vfbs = vmw_framebuffer_to_vfbs(new_fb);
574 stdu->display_srf = new_vfbs->surface;
575 }
576
577
578 ret = vmw_stdu_pin_display(stdu);
579 if (unlikely(ret != 0)) {
580 stdu->display_srf = NULL;
581 goto err_unref_content;
582 }
583
35c05125
SY
584 vmw_svga_enable(dev_priv);
585
586 /*
587 * Steps to displaying a surface, assume surface is already
588 * bound:
589 * 1. define a screen target
590 * 2. bind a fb to the screen target
591 * 3. update that screen target (this is done later by
592 * vmw_kms_stdu_do_surface_dirty_or_present)
593 */
594 ret = vmw_stdu_define_st(dev_priv, stdu);
595 if (unlikely(ret != 0))
596 goto err_unpin_display_and_content;
597
598 ret = vmw_stdu_bind_st(dev_priv, stdu, &stdu->display_srf->res);
599 if (unlikely(ret != 0))
600 goto err_unpin_destroy_st;
601
602
603 connector->encoder = encoder;
604 encoder->crtc = crtc;
605
606 crtc->mode = *mode;
607 crtc->primary->fb = new_fb;
608 crtc->enabled = true;
609
610 return ret;
611
612err_unpin_destroy_st:
613 vmw_stdu_destroy_st(dev_priv, stdu);
614err_unpin_display_and_content:
615 vmw_stdu_unpin_display(stdu);
616err_unref_content:
617 stdu->content_fb = NULL;
618 return ret;
619}
620
621
622
623/**
624 * vmw_stdu_crtc_page_flip - Binds a buffer to a screen target
625 *
626 * @crtc: CRTC to attach FB to
627 * @fb: FB to attach
628 * @event: Event to be posted. This event should've been alloced
629 * using k[mz]alloc, and should've been completely initialized.
630 * @page_flip_flags: Input flags.
631 *
632 * If the STDU uses the same display and content buffers, i.e. a true flip,
633 * this function will replace the existing display buffer with the new content
634 * buffer.
635 *
636 * If the STDU uses different display and content buffers, i.e. a blit, then
637 * only the content buffer will be updated.
638 *
639 * RETURNS:
640 * 0 on success, error code on failure
641 */
642static int vmw_stdu_crtc_page_flip(struct drm_crtc *crtc,
643 struct drm_framebuffer *new_fb,
644 struct drm_pending_vblank_event *event,
645 uint32_t flags)
646
647{
648 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
649 struct vmw_screen_target_display_unit *stdu;
35c05125
SY
650 int ret;
651
35c05125
SY
652 if (crtc == NULL)
653 return -EINVAL;
654
655 dev_priv = vmw_priv(crtc->dev);
656 stdu = vmw_crtc_to_stdu(crtc);
657 crtc->primary->fb = new_fb;
658 stdu->content_fb = new_fb;
659
660 if (stdu->display_srf) {
35c05125
SY
661 /*
662 * If the display surface is the same as the content surface
663 * then remove the reference
664 */
665 if (stdu->content_fb_type == SAME_AS_DISPLAY) {
666 if (stdu->defined) {
667 /* Unbind the current surface */
668 ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
669 if (unlikely(ret != 0))
670 goto err_out;
671 }
672 vmw_stdu_unpin_display(stdu);
673 stdu->display_srf = NULL;
674 }
675 }
676
677
678 if (!new_fb) {
679 /* Blanks the display */
6bf6bf03 680 (void) vmw_stdu_update_st(dev_priv, stdu);
35c05125
SY
681
682 return 0;
683 }
684
685
686 if (stdu->content_fb_type == SAME_AS_DISPLAY) {
687 stdu->display_srf = vmw_framebuffer_to_vfbs(new_fb)->surface;
688 ret = vmw_stdu_pin_display(stdu);
689 if (ret) {
690 stdu->display_srf = NULL;
691 goto err_out;
692 }
693
694 /* Bind display surface */
695 ret = vmw_stdu_bind_st(dev_priv, stdu, &stdu->display_srf->res);
696 if (unlikely(ret != 0))
697 goto err_unpin_display_and_content;
698 }
699
700 /* Update display surface: after this point everything is bound */
6bf6bf03 701 ret = vmw_stdu_update_st(dev_priv, stdu);
35c05125
SY
702 if (unlikely(ret != 0))
703 return ret;
704
705 if (event) {
706 struct vmw_fence_obj *fence = NULL;
6bf6bf03 707 struct drm_file *file_priv = event->base.file_priv;
35c05125
SY
708
709 vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
710 if (!fence)
711 return -ENOMEM;
712
713 ret = vmw_event_fence_action_queue(file_priv, fence,
714 &event->base,
715 &event->event.tv_sec,
716 &event->event.tv_usec,
717 true);
718 vmw_fence_obj_unreference(&fence);
719 }
720
721 return ret;
722
723err_unpin_display_and_content:
724 vmw_stdu_unpin_display(stdu);
725err_out:
726 crtc->primary->fb = NULL;
727 stdu->content_fb = NULL;
728 return ret;
729}
730
731
6bf6bf03
TH
732/**
733 * vmw_stdu_dmabuf_clip - Callback to encode a suface DMA command cliprect
734 *
735 * @dirty: The closure structure.
736 *
737 * Encodes a surface DMA command cliprect and updates the bounding box
738 * for the DMA.
739 */
740static void vmw_stdu_dmabuf_clip(struct vmw_kms_dirty *dirty)
741{
742 struct vmw_stdu_dirty *ddirty =
743 container_of(dirty, struct vmw_stdu_dirty, base);
744 struct vmw_stdu_dma *cmd = dirty->cmd;
745 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
746
747 blit += dirty->num_hits;
748 blit->srcx = dirty->fb_x;
749 blit->srcy = dirty->fb_y;
750 blit->x = dirty->unit_x1;
751 blit->y = dirty->unit_y1;
752 blit->d = 1;
753 blit->w = dirty->unit_x2 - dirty->unit_x1;
754 blit->h = dirty->unit_y2 - dirty->unit_y1;
755 dirty->num_hits++;
756
757 if (ddirty->transfer != SVGA3D_WRITE_HOST_VRAM)
758 return;
759
760 /* Destination bounding box */
761 ddirty->left = min_t(s32, ddirty->left, dirty->unit_x1);
762 ddirty->top = min_t(s32, ddirty->top, dirty->unit_y1);
763 ddirty->right = max_t(s32, ddirty->right, dirty->unit_x2);
764 ddirty->bottom = max_t(s32, ddirty->bottom, dirty->unit_y2);
765}
766
767/**
768 * vmw_stdu_dmabuf_fifo_commit - Callback to fill in and submit a DMA command.
769 *
770 * @dirty: The closure structure.
771 *
772 * Fills in the missing fields in a DMA command, and optionally encodes
773 * a screen target update command, depending on transfer direction.
774 */
775static void vmw_stdu_dmabuf_fifo_commit(struct vmw_kms_dirty *dirty)
776{
777 struct vmw_stdu_dirty *ddirty =
778 container_of(dirty, struct vmw_stdu_dirty, base);
779 struct vmw_screen_target_display_unit *stdu =
780 container_of(dirty->unit, typeof(*stdu), base);
781 struct vmw_stdu_dma *cmd = dirty->cmd;
782 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
783 SVGA3dCmdSurfaceDMASuffix *suffix =
784 (SVGA3dCmdSurfaceDMASuffix *) &blit[dirty->num_hits];
785 size_t blit_size = sizeof(*blit) * dirty->num_hits + sizeof(*suffix);
786
787 if (!dirty->num_hits) {
788 vmw_fifo_commit(dirty->dev_priv, 0);
789 return;
790 }
791
792 cmd->header.id = SVGA_3D_CMD_SURFACE_DMA;
793 cmd->header.size = sizeof(cmd->body) + blit_size;
794 vmw_bo_get_guest_ptr(&ddirty->buf->base, &cmd->body.guest.ptr);
795 cmd->body.guest.pitch = ddirty->pitch;
796 cmd->body.host.sid = stdu->display_srf->res.id;
797 cmd->body.host.face = 0;
798 cmd->body.host.mipmap = 0;
799 cmd->body.transfer = ddirty->transfer;
800 suffix->suffixSize = sizeof(*suffix);
801 suffix->maximumOffset = ddirty->buf->base.num_pages * PAGE_SIZE;
802
803 if (ddirty->transfer == SVGA3D_WRITE_HOST_VRAM) {
804 blit_size += sizeof(struct vmw_stdu_update);
805
806 vmw_stdu_populate_update(&suffix[1], stdu->base.unit,
807 ddirty->left, ddirty->right,
808 ddirty->top, ddirty->bottom);
809 }
810
811 vmw_fifo_commit(dirty->dev_priv, sizeof(*cmd) + blit_size);
812
813 ddirty->left = ddirty->top = S32_MAX;
814 ddirty->right = ddirty->bottom = S32_MIN;
815}
816
817/**
818 * vmw_kms_stdu_dma - Perform a DMA transfer between a dma-buffer backed
819 * framebuffer and the screen target system.
820 *
821 * @dev_priv: Pointer to the device private structure.
822 * @file_priv: Pointer to a struct drm-file identifying the caller. May be
823 * set to NULL, but then @user_fence_rep must also be set to NULL.
824 * @vfb: Pointer to the dma-buffer backed framebuffer.
825 * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
826 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
827 * be NULL.
828 * @num_clips: Number of clip rects in @clips or @vclips.
829 * @increment: Increment to use when looping over @clips or @vclips.
830 * @to_surface: Whether to DMA to the screen target system as opposed to
831 * from the screen target system.
832 * @interruptible: Whether to perform waits interruptible if possible.
833 *
834 * If DMA-ing till the screen target system, the function will also notify
835 * the screen target system that a bounding box of the cliprects has been
836 * updated.
837 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
838 * interrupted.
839 */
840int vmw_kms_stdu_dma(struct vmw_private *dev_priv,
841 struct drm_file *file_priv,
842 struct vmw_framebuffer *vfb,
843 struct drm_vmw_fence_rep __user *user_fence_rep,
844 struct drm_clip_rect *clips,
845 struct drm_vmw_rect *vclips,
846 uint32_t num_clips,
847 int increment,
848 bool to_surface,
849 bool interruptible)
850{
851 struct vmw_dma_buffer *buf =
852 container_of(vfb, struct vmw_framebuffer_dmabuf, base)->buffer;
853 struct vmw_stdu_dirty ddirty;
854 int ret;
855
856 ret = vmw_kms_helper_buffer_prepare(dev_priv, buf, interruptible,
857 false);
858 if (ret)
859 return ret;
860
861 ddirty.transfer = (to_surface) ? SVGA3D_WRITE_HOST_VRAM :
862 SVGA3D_READ_HOST_VRAM;
863 ddirty.left = ddirty.top = S32_MAX;
864 ddirty.right = ddirty.bottom = S32_MIN;
865 ddirty.pitch = vfb->base.pitches[0];
866 ddirty.buf = buf;
867 ddirty.base.fifo_commit = vmw_stdu_dmabuf_fifo_commit;
868 ddirty.base.clip = vmw_stdu_dmabuf_clip;
869 ddirty.base.fifo_reserve_size = sizeof(struct vmw_stdu_dma) +
870 num_clips * sizeof(SVGA3dCopyBox) +
871 sizeof(SVGA3dCmdSurfaceDMASuffix);
872 if (to_surface)
873 ddirty.base.fifo_reserve_size += sizeof(struct vmw_stdu_update);
874
875 ret = vmw_kms_helper_dirty(dev_priv, vfb, clips, vclips,
876 0, 0, num_clips, increment, &ddirty.base);
877 vmw_kms_helper_buffer_finish(dev_priv, file_priv, buf, NULL,
878 user_fence_rep);
879
880 return ret;
881}
882
883/**
884 * vmw_stdu_surface_clip - Callback to encode a surface copy command cliprect
885 *
886 * @dirty: The closure structure.
887 *
888 * Encodes a surface copy command cliprect and updates the bounding box
889 * for the copy.
890 */
891static void vmw_kms_stdu_surface_clip(struct vmw_kms_dirty *dirty)
892{
893 struct vmw_stdu_dirty *sdirty =
894 container_of(dirty, struct vmw_stdu_dirty, base);
895 struct vmw_stdu_surface_copy *cmd = dirty->cmd;
896 struct vmw_screen_target_display_unit *stdu =
897 container_of(dirty->unit, typeof(*stdu), base);
898
899 if (sdirty->sid != stdu->display_srf->res.id) {
900 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
901
902 blit += dirty->num_hits;
903 blit->srcx = dirty->fb_x;
904 blit->srcy = dirty->fb_y;
905 blit->x = dirty->unit_x1;
906 blit->y = dirty->unit_y1;
907 blit->d = 1;
908 blit->w = dirty->unit_x2 - dirty->unit_x1;
909 blit->h = dirty->unit_y2 - dirty->unit_y1;
910 }
911
912 dirty->num_hits++;
913
914 /* Destination bounding box */
915 sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1);
916 sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1);
917 sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2);
918 sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2);
919}
920
921/**
922 * vmw_stdu_surface_fifo_commit - Callback to fill in and submit a surface
923 * copy command.
924 *
925 * @dirty: The closure structure.
926 *
927 * Fills in the missing fields in a surface copy command, and encodes a screen
928 * target update command.
929 */
930static void vmw_kms_stdu_surface_fifo_commit(struct vmw_kms_dirty *dirty)
931{
932 struct vmw_stdu_dirty *sdirty =
933 container_of(dirty, struct vmw_stdu_dirty, base);
934 struct vmw_screen_target_display_unit *stdu =
935 container_of(dirty->unit, typeof(*stdu), base);
936 struct vmw_stdu_surface_copy *cmd = dirty->cmd;
937 struct vmw_stdu_update *update;
938 size_t blit_size = sizeof(SVGA3dCopyBox) * dirty->num_hits;
939 size_t commit_size;
940
941 if (!dirty->num_hits) {
942 vmw_fifo_commit(dirty->dev_priv, 0);
943 return;
944 }
945
946 if (sdirty->sid != stdu->display_srf->res.id) {
947 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
948
949 cmd->header.id = SVGA_3D_CMD_SURFACE_COPY;
950 cmd->header.size = sizeof(cmd->body) + blit_size;
951 cmd->body.src.sid = sdirty->sid;
952 cmd->body.dest.sid = stdu->display_srf->res.id;
953 update = (struct vmw_stdu_update *) &blit[dirty->num_hits];
954 commit_size = sizeof(*cmd) + blit_size + sizeof(*update);
955 } else {
956 update = dirty->cmd;
957 commit_size = sizeof(*update);
958 }
959
960 vmw_stdu_populate_update(update, stdu->base.unit, sdirty->left,
961 sdirty->right, sdirty->top, sdirty->bottom);
962
963 vmw_fifo_commit(dirty->dev_priv, commit_size);
964
965 sdirty->left = sdirty->top = S32_MAX;
966 sdirty->right = sdirty->bottom = S32_MIN;
967}
968
969/**
970 * vmw_kms_stdu_surface_dirty - Dirty part of a surface backed framebuffer
971 *
972 * @dev_priv: Pointer to the device private structure.
973 * @framebuffer: Pointer to the surface-buffer backed framebuffer.
974 * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
975 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
976 * be NULL.
977 * @srf: Pointer to surface to blit from. If NULL, the surface attached
978 * to @framebuffer will be used.
979 * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
980 * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
981 * @num_clips: Number of clip rects in @clips.
982 * @inc: Increment to use when looping over @clips.
983 * @out_fence: If non-NULL, will return a ref-counted pointer to a
984 * struct vmw_fence_obj. The returned fence pointer may be NULL in which
985 * case the device has already synchronized.
986 *
987 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
988 * interrupted.
989 */
990int vmw_kms_stdu_surface_dirty(struct vmw_private *dev_priv,
991 struct vmw_framebuffer *framebuffer,
992 struct drm_clip_rect *clips,
993 struct drm_vmw_rect *vclips,
994 struct vmw_resource *srf,
995 s32 dest_x,
996 s32 dest_y,
997 unsigned num_clips, int inc,
998 struct vmw_fence_obj **out_fence)
999{
1000 struct vmw_framebuffer_surface *vfbs =
1001 container_of(framebuffer, typeof(*vfbs), base);
1002 struct vmw_stdu_dirty sdirty;
1003 int ret;
1004
1005 if (!srf)
1006 srf = &vfbs->surface->res;
1007
1008 ret = vmw_kms_helper_resource_prepare(srf, true);
1009 if (ret)
1010 return ret;
1011
1012 if (vfbs->is_dmabuf_proxy) {
1013 ret = vmw_kms_update_proxy(srf, clips, num_clips, inc);
1014 if (ret)
1015 goto out_finish;
1016 }
1017
1018 sdirty.base.fifo_commit = vmw_kms_stdu_surface_fifo_commit;
1019 sdirty.base.clip = vmw_kms_stdu_surface_clip;
1020 sdirty.base.fifo_reserve_size = sizeof(struct vmw_stdu_surface_copy) +
1021 sizeof(SVGA3dCopyBox) * num_clips +
1022 sizeof(struct vmw_stdu_update);
1023 sdirty.sid = srf->id;
1024 sdirty.left = sdirty.top = S32_MAX;
1025 sdirty.right = sdirty.bottom = S32_MIN;
1026
1027 ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
1028 dest_x, dest_y, num_clips, inc,
1029 &sdirty.base);
1030out_finish:
1031 vmw_kms_helper_resource_finish(srf, out_fence);
1032
1033 return ret;
1034}
1035
35c05125
SY
1036
1037/*
1038 * Screen Target CRTC dispatch table
1039 */
1040static struct drm_crtc_funcs vmw_stdu_crtc_funcs = {
1041 .save = vmw_du_crtc_save,
1042 .restore = vmw_du_crtc_restore,
1043 .cursor_set = vmw_du_crtc_cursor_set,
1044 .cursor_move = vmw_du_crtc_cursor_move,
1045 .gamma_set = vmw_du_crtc_gamma_set,
1046 .destroy = vmw_stdu_crtc_destroy,
1047 .set_config = vmw_stdu_crtc_set_config,
1048 .page_flip = vmw_stdu_crtc_page_flip,
1049};
1050
1051
1052
1053/******************************************************************************
1054 * Screen Target Display Unit Encoder Functions
1055 *****************************************************************************/
1056
1057/**
1058 * vmw_stdu_encoder_destroy - cleans up the STDU
1059 *
1060 * @encoder: used the get the containing STDU
1061 *
1062 * vmwgfx cleans up crtc/encoder/connector all at the same time so technically
1063 * this can be a no-op. Nevertheless, it doesn't hurt of have this in case
1064 * the common KMS code changes and somehow vmw_stdu_crtc_destroy() doesn't
1065 * get called.
1066 */
1067static void vmw_stdu_encoder_destroy(struct drm_encoder *encoder)
1068{
1069 vmw_stdu_destroy(vmw_encoder_to_stdu(encoder));
1070}
1071
1072static struct drm_encoder_funcs vmw_stdu_encoder_funcs = {
1073 .destroy = vmw_stdu_encoder_destroy,
1074};
1075
1076
1077
1078/******************************************************************************
1079 * Screen Target Display Unit Connector Functions
1080 *****************************************************************************/
1081
1082/**
1083 * vmw_stdu_connector_destroy - cleans up the STDU
1084 *
1085 * @connector: used to get the containing STDU
1086 *
1087 * vmwgfx cleans up crtc/encoder/connector all at the same time so technically
1088 * this can be a no-op. Nevertheless, it doesn't hurt of have this in case
1089 * the common KMS code changes and somehow vmw_stdu_crtc_destroy() doesn't
1090 * get called.
1091 */
1092static void vmw_stdu_connector_destroy(struct drm_connector *connector)
1093{
1094 vmw_stdu_destroy(vmw_connector_to_stdu(connector));
1095}
1096
1097
1098
1099static struct drm_connector_funcs vmw_stdu_connector_funcs = {
1100 .dpms = vmw_du_connector_dpms,
1101 .save = vmw_du_connector_save,
1102 .restore = vmw_du_connector_restore,
1103 .detect = vmw_du_connector_detect,
1104 .fill_modes = vmw_du_connector_fill_modes,
1105 .set_property = vmw_du_connector_set_property,
1106 .destroy = vmw_stdu_connector_destroy,
1107};
1108
1109
1110
1111/**
1112 * vmw_stdu_init - Sets up a Screen Target Display Unit
1113 *
1114 * @dev_priv: VMW DRM device
1115 * @unit: unit number range from 0 to VMWGFX_NUM_DISPLAY_UNITS
1116 *
1117 * This function is called once per CRTC, and allocates one Screen Target
1118 * display unit to represent that CRTC. Since the SVGA device does not separate
1119 * out encoder and connector, they are represented as part of the STDU as well.
1120 */
1121static int vmw_stdu_init(struct vmw_private *dev_priv, unsigned unit)
1122{
1123 struct vmw_screen_target_display_unit *stdu;
1124 struct drm_device *dev = dev_priv->dev;
1125 struct drm_connector *connector;
1126 struct drm_encoder *encoder;
1127 struct drm_crtc *crtc;
1128
1129
1130 stdu = kzalloc(sizeof(*stdu), GFP_KERNEL);
1131 if (!stdu)
1132 return -ENOMEM;
1133
1134 stdu->base.unit = unit;
1135 crtc = &stdu->base.crtc;
1136 encoder = &stdu->base.encoder;
1137 connector = &stdu->base.connector;
1138
1139 stdu->base.pref_active = (unit == 0);
1140 stdu->base.pref_width = dev_priv->initial_width;
1141 stdu->base.pref_height = dev_priv->initial_height;
35c05125
SY
1142 stdu->base.is_implicit = true;
1143
1144 drm_connector_init(dev, connector, &vmw_stdu_connector_funcs,
1145 DRM_MODE_CONNECTOR_VIRTUAL);
1146 connector->status = vmw_du_connector_detect(connector, false);
1147
1148 drm_encoder_init(dev, encoder, &vmw_stdu_encoder_funcs,
1149 DRM_MODE_ENCODER_VIRTUAL);
1150 drm_mode_connector_attach_encoder(connector, encoder);
1151 encoder->possible_crtcs = (1 << unit);
1152 encoder->possible_clones = 0;
1153
1154 (void) drm_connector_register(connector);
1155
1156 drm_crtc_init(dev, crtc, &vmw_stdu_crtc_funcs);
1157
1158 drm_mode_crtc_set_gamma_size(crtc, 256);
1159
1160 drm_object_attach_property(&connector->base,
1161 dev->mode_config.dirty_info_property,
1162 1);
1163
1164 return 0;
1165}
1166
1167
1168
1169/**
1170 * vmw_stdu_destroy - Cleans up a vmw_screen_target_display_unit
1171 *
1172 * @stdu: Screen Target Display Unit to be destroyed
1173 *
1174 * Clean up after vmw_stdu_init
1175 */
1176static void vmw_stdu_destroy(struct vmw_screen_target_display_unit *stdu)
1177{
1178 vmw_stdu_unpin_display(stdu);
1179
1180 vmw_du_cleanup(&stdu->base);
1181 kfree(stdu);
1182}
1183
1184
1185
1186/******************************************************************************
1187 * Screen Target Display KMS Functions
1188 *
1189 * These functions are called by the common KMS code in vmwgfx_kms.c
1190 *****************************************************************************/
1191
1192/**
1193 * vmw_kms_stdu_init_display - Initializes a Screen Target based display
1194 *
1195 * @dev_priv: VMW DRM device
1196 *
1197 * This function initialize a Screen Target based display device. It checks
1198 * the capability bits to make sure the underlying hardware can support
1199 * screen targets, and then creates the maximum number of CRTCs, a.k.a Display
1200 * Units, as supported by the display hardware.
1201 *
1202 * RETURNS:
1203 * 0 on success, error code otherwise
1204 */
1205int vmw_kms_stdu_init_display(struct vmw_private *dev_priv)
1206{
1207 struct drm_device *dev = dev_priv->dev;
1208 int i, ret;
1209
1210
1211 /* Do nothing if Screen Target support is turned off */
1212 if (!VMWGFX_ENABLE_SCREEN_TARGET_OTABLE)
1213 return -ENOSYS;
1214
f89c6c32 1215 if (!(dev_priv->capabilities & SVGA_CAP_GBOBJECTS))
35c05125
SY
1216 return -ENOSYS;
1217
1218 ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
1219 if (unlikely(ret != 0))
1220 return ret;
1221
1222 ret = drm_mode_create_dirty_info_property(dev);
1223 if (unlikely(ret != 0))
1224 goto err_vblank_cleanup;
1225
6bf6bf03
TH
1226 dev_priv->active_display_unit = vmw_du_screen_target;
1227
35c05125
SY
1228 for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i) {
1229 ret = vmw_stdu_init(dev_priv, i);
1230
1231 if (unlikely(ret != 0)) {
1232 DRM_ERROR("Failed to initialize STDU %d", i);
1233 goto err_vblank_cleanup;
1234 }
1235 }
1236
35c05125
SY
1237 DRM_INFO("Screen Target Display device initialized\n");
1238
1239 return 0;
1240
1241err_vblank_cleanup:
1242 drm_vblank_cleanup(dev);
1243 return ret;
1244}
1245
1246
1247
1248/**
1249 * vmw_kms_stdu_close_display - Cleans up after vmw_kms_stdu_init_display
1250 *
1251 * @dev_priv: VMW DRM device
1252 *
1253 * Frees up any resources allocated by vmw_kms_stdu_init_display
1254 *
1255 * RETURNS:
1256 * 0 on success
1257 */
1258int vmw_kms_stdu_close_display(struct vmw_private *dev_priv)
1259{
1260 struct drm_device *dev = dev_priv->dev;
1261
1262 drm_vblank_cleanup(dev);
1263
1264 return 0;
1265}
This page took 0.072617 seconds and 5 git commands to generate.