]> Git Repo - linux.git/blob - drivers/gpu/drm/vmwgfx/vmwgfx_stdu.c
Merge tag 'drm-misc-next-2018-11-21' of git://anongit.freedesktop.org/drm/drm-misc...
[linux.git] / drivers / gpu / drm / vmwgfx / vmwgfx_stdu.c
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /******************************************************************************
3  *
4  * COPYRIGHT (C) 2014-2015 VMware, Inc., Palo Alto, CA., USA
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 "device_include/svga3d_surfacedefs.h"
30 #include <drm/drm_plane_helper.h>
31 #include <drm/drm_atomic.h>
32 #include <drm/drm_atomic_helper.h>
33
34
35 #define vmw_crtc_to_stdu(x) \
36         container_of(x, struct vmw_screen_target_display_unit, base.crtc)
37 #define vmw_encoder_to_stdu(x) \
38         container_of(x, struct vmw_screen_target_display_unit, base.encoder)
39 #define vmw_connector_to_stdu(x) \
40         container_of(x, struct vmw_screen_target_display_unit, base.connector)
41
42
43
44 enum stdu_content_type {
45         SAME_AS_DISPLAY = 0,
46         SEPARATE_SURFACE,
47         SEPARATE_BO
48 };
49
50 /**
51  * struct vmw_stdu_dirty - closure structure for the update functions
52  *
53  * @base: The base type we derive from. Used by vmw_kms_helper_dirty().
54  * @transfer: Transfer direction for DMA command.
55  * @left: Left side of bounding box.
56  * @right: Right side of bounding box.
57  * @top: Top side of bounding box.
58  * @bottom: Bottom side of bounding box.
59  * @fb_left: Left side of the framebuffer/content bounding box
60  * @fb_top: Top of the framebuffer/content bounding box
61  * @buf: buffer object when DMA-ing between buffer and screen targets.
62  * @sid: Surface ID when copying between surface and screen targets.
63  */
64 struct vmw_stdu_dirty {
65         struct vmw_kms_dirty base;
66         SVGA3dTransferType  transfer;
67         s32 left, right, top, bottom;
68         s32 fb_left, fb_top;
69         u32 pitch;
70         union {
71                 struct vmw_buffer_object *buf;
72                 u32 sid;
73         };
74 };
75
76 /*
77  * SVGA commands that are used by this code. Please see the device headers
78  * for explanation.
79  */
80 struct vmw_stdu_update {
81         SVGA3dCmdHeader header;
82         SVGA3dCmdUpdateGBScreenTarget body;
83 };
84
85 struct vmw_stdu_dma {
86         SVGA3dCmdHeader     header;
87         SVGA3dCmdSurfaceDMA body;
88 };
89
90 struct vmw_stdu_surface_copy {
91         SVGA3dCmdHeader      header;
92         SVGA3dCmdSurfaceCopy body;
93 };
94
95
96 /**
97  * struct vmw_screen_target_display_unit
98  *
99  * @base: VMW specific DU structure
100  * @display_srf: surface to be displayed.  The dimension of this will always
101  *               match the display mode.  If the display mode matches
102  *               content_vfbs dimensions, then this is a pointer into the
103  *               corresponding field in content_vfbs.  If not, then this
104  *               is a separate buffer to which content_vfbs will blit to.
105  * @content_type:  content_fb type
106  * @defined:  true if the current display unit has been initialized
107  */
108 struct vmw_screen_target_display_unit {
109         struct vmw_display_unit base;
110         const struct vmw_surface *display_srf;
111         enum stdu_content_type content_fb_type;
112         s32 display_width, display_height;
113
114         bool defined;
115
116         /* For CPU Blit */
117         unsigned int cpp;
118 };
119
120
121
122 static void vmw_stdu_destroy(struct vmw_screen_target_display_unit *stdu);
123
124
125
126 /******************************************************************************
127  * Screen Target Display Unit CRTC Functions
128  *****************************************************************************/
129
130
131 /**
132  * vmw_stdu_crtc_destroy - cleans up the STDU
133  *
134  * @crtc: used to get a reference to the containing STDU
135  */
136 static void vmw_stdu_crtc_destroy(struct drm_crtc *crtc)
137 {
138         vmw_stdu_destroy(vmw_crtc_to_stdu(crtc));
139 }
140
141 /**
142  * vmw_stdu_define_st - Defines a Screen Target
143  *
144  * @dev_priv:  VMW DRM device
145  * @stdu: display unit to create a Screen Target for
146  * @mode: The mode to set.
147  * @crtc_x: X coordinate of screen target relative to framebuffer origin.
148  * @crtc_y: Y coordinate of screen target relative to framebuffer origin.
149  *
150  * Creates a STDU that we can used later.  This function is called whenever the
151  * framebuffer size changes.
152  *
153  * RETURNs:
154  * 0 on success, error code on failure
155  */
156 static int vmw_stdu_define_st(struct vmw_private *dev_priv,
157                               struct vmw_screen_target_display_unit *stdu,
158                               struct drm_display_mode *mode,
159                               int crtc_x, int crtc_y)
160 {
161         struct {
162                 SVGA3dCmdHeader header;
163                 SVGA3dCmdDefineGBScreenTarget body;
164         } *cmd;
165
166         cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
167
168         if (unlikely(cmd == NULL)) {
169                 DRM_ERROR("Out of FIFO space defining Screen Target\n");
170                 return -ENOMEM;
171         }
172
173         cmd->header.id   = SVGA_3D_CMD_DEFINE_GB_SCREENTARGET;
174         cmd->header.size = sizeof(cmd->body);
175
176         cmd->body.stid   = stdu->base.unit;
177         cmd->body.width  = mode->hdisplay;
178         cmd->body.height = mode->vdisplay;
179         cmd->body.flags  = (0 == cmd->body.stid) ? SVGA_STFLAG_PRIMARY : 0;
180         cmd->body.dpi    = 0;
181         cmd->body.xRoot  = crtc_x;
182         cmd->body.yRoot  = crtc_y;
183
184         stdu->base.set_gui_x = cmd->body.xRoot;
185         stdu->base.set_gui_y = cmd->body.yRoot;
186
187         vmw_fifo_commit(dev_priv, sizeof(*cmd));
188
189         stdu->defined = true;
190         stdu->display_width  = mode->hdisplay;
191         stdu->display_height = mode->vdisplay;
192
193         return 0;
194 }
195
196
197
198 /**
199  * vmw_stdu_bind_st - Binds a surface to a Screen Target
200  *
201  * @dev_priv: VMW DRM device
202  * @stdu: display unit affected
203  * @res: Buffer to bind to the screen target.  Set to NULL to blank screen.
204  *
205  * Binding a surface to a Screen Target the same as flipping
206  */
207 static int vmw_stdu_bind_st(struct vmw_private *dev_priv,
208                             struct vmw_screen_target_display_unit *stdu,
209                             const struct vmw_resource *res)
210 {
211         SVGA3dSurfaceImageId image;
212
213         struct {
214                 SVGA3dCmdHeader header;
215                 SVGA3dCmdBindGBScreenTarget body;
216         } *cmd;
217
218
219         if (!stdu->defined) {
220                 DRM_ERROR("No screen target defined\n");
221                 return -EINVAL;
222         }
223
224         /* Set up image using information in vfb */
225         memset(&image, 0, sizeof(image));
226         image.sid = res ? res->id : SVGA3D_INVALID_ID;
227
228         cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
229
230         if (unlikely(cmd == NULL)) {
231                 DRM_ERROR("Out of FIFO space binding a screen target\n");
232                 return -ENOMEM;
233         }
234
235         cmd->header.id   = SVGA_3D_CMD_BIND_GB_SCREENTARGET;
236         cmd->header.size = sizeof(cmd->body);
237
238         cmd->body.stid   = stdu->base.unit;
239         cmd->body.image  = image;
240
241         vmw_fifo_commit(dev_priv, sizeof(*cmd));
242
243         return 0;
244 }
245
246 /**
247  * vmw_stdu_populate_update - populate an UPDATE_GB_SCREENTARGET command with a
248  * bounding box.
249  *
250  * @cmd: Pointer to command stream.
251  * @unit: Screen target unit.
252  * @left: Left side of bounding box.
253  * @right: Right side of bounding box.
254  * @top: Top side of bounding box.
255  * @bottom: Bottom side of bounding box.
256  */
257 static void vmw_stdu_populate_update(void *cmd, int unit,
258                                      s32 left, s32 right, s32 top, s32 bottom)
259 {
260         struct vmw_stdu_update *update = cmd;
261
262         update->header.id   = SVGA_3D_CMD_UPDATE_GB_SCREENTARGET;
263         update->header.size = sizeof(update->body);
264
265         update->body.stid   = unit;
266         update->body.rect.x = left;
267         update->body.rect.y = top;
268         update->body.rect.w = right - left;
269         update->body.rect.h = bottom - top;
270 }
271
272 /**
273  * vmw_stdu_update_st - Full update of a Screen Target
274  *
275  * @dev_priv: VMW DRM device
276  * @stdu: display unit affected
277  *
278  * This function needs to be called whenever the content of a screen
279  * target has changed completely. Typically as a result of a backing
280  * surface change.
281  *
282  * RETURNS:
283  * 0 on success, error code on failure
284  */
285 static int vmw_stdu_update_st(struct vmw_private *dev_priv,
286                               struct vmw_screen_target_display_unit *stdu)
287 {
288         struct vmw_stdu_update *cmd;
289
290         if (!stdu->defined) {
291                 DRM_ERROR("No screen target defined");
292                 return -EINVAL;
293         }
294
295         cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
296
297         if (unlikely(cmd == NULL)) {
298                 DRM_ERROR("Out of FIFO space updating a Screen Target\n");
299                 return -ENOMEM;
300         }
301
302         vmw_stdu_populate_update(cmd, stdu->base.unit,
303                                  0, stdu->display_width,
304                                  0, stdu->display_height);
305
306         vmw_fifo_commit(dev_priv, sizeof(*cmd));
307
308         return 0;
309 }
310
311
312
313 /**
314  * vmw_stdu_destroy_st - Destroy a Screen Target
315  *
316  * @dev_priv:  VMW DRM device
317  * @stdu: display unit to destroy
318  */
319 static int vmw_stdu_destroy_st(struct vmw_private *dev_priv,
320                                struct vmw_screen_target_display_unit *stdu)
321 {
322         int    ret;
323
324         struct {
325                 SVGA3dCmdHeader header;
326                 SVGA3dCmdDestroyGBScreenTarget body;
327         } *cmd;
328
329
330         /* Nothing to do if not successfully defined */
331         if (unlikely(!stdu->defined))
332                 return 0;
333
334         cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
335
336         if (unlikely(cmd == NULL)) {
337                 DRM_ERROR("Out of FIFO space, screen target not destroyed\n");
338                 return -ENOMEM;
339         }
340
341         cmd->header.id   = SVGA_3D_CMD_DESTROY_GB_SCREENTARGET;
342         cmd->header.size = sizeof(cmd->body);
343
344         cmd->body.stid   = stdu->base.unit;
345
346         vmw_fifo_commit(dev_priv, sizeof(*cmd));
347
348         /* Force sync */
349         ret = vmw_fallback_wait(dev_priv, false, true, 0, false, 3*HZ);
350         if (unlikely(ret != 0))
351                 DRM_ERROR("Failed to sync with HW");
352
353         stdu->defined = false;
354         stdu->display_width  = 0;
355         stdu->display_height = 0;
356
357         return ret;
358 }
359
360
361 /**
362  * vmw_stdu_crtc_mode_set_nofb - Updates screen target size
363  *
364  * @crtc: CRTC associated with the screen target
365  *
366  * This function defines/destroys a screen target
367  *
368  */
369 static void vmw_stdu_crtc_mode_set_nofb(struct drm_crtc *crtc)
370 {
371         struct vmw_private *dev_priv;
372         struct vmw_screen_target_display_unit *stdu;
373         struct drm_connector_state *conn_state;
374         struct vmw_connector_state *vmw_conn_state;
375         int x, y, ret;
376
377         stdu = vmw_crtc_to_stdu(crtc);
378         dev_priv = vmw_priv(crtc->dev);
379         conn_state = stdu->base.connector.state;
380         vmw_conn_state = vmw_connector_state_to_vcs(conn_state);
381
382         if (stdu->defined) {
383                 ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
384                 if (ret)
385                         DRM_ERROR("Failed to blank CRTC\n");
386
387                 (void) vmw_stdu_update_st(dev_priv, stdu);
388
389                 ret = vmw_stdu_destroy_st(dev_priv, stdu);
390                 if (ret)
391                         DRM_ERROR("Failed to destroy Screen Target\n");
392
393                 stdu->content_fb_type = SAME_AS_DISPLAY;
394         }
395
396         if (!crtc->state->enable)
397                 return;
398
399         if (stdu->base.is_implicit) {
400                 x = crtc->x;
401                 y = crtc->y;
402         } else {
403                 x = vmw_conn_state->gui_x;
404                 y = vmw_conn_state->gui_y;
405         }
406
407         vmw_svga_enable(dev_priv);
408         ret = vmw_stdu_define_st(dev_priv, stdu, &crtc->mode, x, y);
409
410         if (ret)
411                 DRM_ERROR("Failed to define Screen Target of size %dx%d\n",
412                           crtc->x, crtc->y);
413 }
414
415
416 static void vmw_stdu_crtc_helper_prepare(struct drm_crtc *crtc)
417 {
418 }
419
420
421 static void vmw_stdu_crtc_atomic_enable(struct drm_crtc *crtc,
422                                         struct drm_crtc_state *old_state)
423 {
424         struct drm_plane_state *plane_state = crtc->primary->state;
425         struct vmw_private *dev_priv;
426         struct vmw_screen_target_display_unit *stdu;
427         struct vmw_framebuffer *vfb;
428         struct drm_framebuffer *fb;
429
430
431         stdu     = vmw_crtc_to_stdu(crtc);
432         dev_priv = vmw_priv(crtc->dev);
433         fb       = plane_state->fb;
434
435         vfb = (fb) ? vmw_framebuffer_to_vfb(fb) : NULL;
436
437         if (vfb)
438                 vmw_kms_add_active(dev_priv, &stdu->base, vfb);
439         else
440                 vmw_kms_del_active(dev_priv, &stdu->base);
441 }
442
443 static void vmw_stdu_crtc_atomic_disable(struct drm_crtc *crtc,
444                                          struct drm_crtc_state *old_state)
445 {
446         struct vmw_private *dev_priv;
447         struct vmw_screen_target_display_unit *stdu;
448         int ret;
449
450
451         if (!crtc) {
452                 DRM_ERROR("CRTC is NULL\n");
453                 return;
454         }
455
456         stdu     = vmw_crtc_to_stdu(crtc);
457         dev_priv = vmw_priv(crtc->dev);
458
459         if (stdu->defined) {
460                 ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
461                 if (ret)
462                         DRM_ERROR("Failed to blank CRTC\n");
463
464                 (void) vmw_stdu_update_st(dev_priv, stdu);
465
466                 ret = vmw_stdu_destroy_st(dev_priv, stdu);
467                 if (ret)
468                         DRM_ERROR("Failed to destroy Screen Target\n");
469
470                 stdu->content_fb_type = SAME_AS_DISPLAY;
471         }
472 }
473
474 /**
475  * vmw_stdu_crtc_page_flip - Binds a buffer to a screen target
476  *
477  * @crtc: CRTC to attach FB to
478  * @fb: FB to attach
479  * @event: Event to be posted. This event should've been alloced
480  *         using k[mz]alloc, and should've been completely initialized.
481  * @page_flip_flags: Input flags.
482  *
483  * If the STDU uses the same display and content buffers, i.e. a true flip,
484  * this function will replace the existing display buffer with the new content
485  * buffer.
486  *
487  * If the STDU uses different display and content buffers, i.e. a blit, then
488  * only the content buffer will be updated.
489  *
490  * RETURNS:
491  * 0 on success, error code on failure
492  */
493 static int vmw_stdu_crtc_page_flip(struct drm_crtc *crtc,
494                                    struct drm_framebuffer *new_fb,
495                                    struct drm_pending_vblank_event *event,
496                                    uint32_t flags,
497                                    struct drm_modeset_acquire_ctx *ctx)
498
499 {
500         struct vmw_private *dev_priv = vmw_priv(crtc->dev);
501         struct vmw_screen_target_display_unit *stdu = vmw_crtc_to_stdu(crtc);
502         int ret;
503
504         if (!stdu->defined || !vmw_kms_crtc_flippable(dev_priv, crtc))
505                 return -EINVAL;
506
507         ret = drm_atomic_helper_page_flip(crtc, new_fb, event, flags, ctx);
508         if (ret) {
509                 DRM_ERROR("Page flip error %d.\n", ret);
510                 return ret;
511         }
512
513         return 0;
514 }
515
516
517 /**
518  * vmw_stdu_bo_clip - Callback to encode a suface DMA command cliprect
519  *
520  * @dirty: The closure structure.
521  *
522  * Encodes a surface DMA command cliprect and updates the bounding box
523  * for the DMA.
524  */
525 static void vmw_stdu_bo_clip(struct vmw_kms_dirty *dirty)
526 {
527         struct vmw_stdu_dirty *ddirty =
528                 container_of(dirty, struct vmw_stdu_dirty, base);
529         struct vmw_stdu_dma *cmd = dirty->cmd;
530         struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
531
532         blit += dirty->num_hits;
533         blit->srcx = dirty->fb_x;
534         blit->srcy = dirty->fb_y;
535         blit->x = dirty->unit_x1;
536         blit->y = dirty->unit_y1;
537         blit->d = 1;
538         blit->w = dirty->unit_x2 - dirty->unit_x1;
539         blit->h = dirty->unit_y2 - dirty->unit_y1;
540         dirty->num_hits++;
541
542         if (ddirty->transfer != SVGA3D_WRITE_HOST_VRAM)
543                 return;
544
545         /* Destination bounding box */
546         ddirty->left = min_t(s32, ddirty->left, dirty->unit_x1);
547         ddirty->top = min_t(s32, ddirty->top, dirty->unit_y1);
548         ddirty->right = max_t(s32, ddirty->right, dirty->unit_x2);
549         ddirty->bottom = max_t(s32, ddirty->bottom, dirty->unit_y2);
550 }
551
552 /**
553  * vmw_stdu_bo_fifo_commit - Callback to fill in and submit a DMA command.
554  *
555  * @dirty: The closure structure.
556  *
557  * Fills in the missing fields in a DMA command, and optionally encodes
558  * a screen target update command, depending on transfer direction.
559  */
560 static void vmw_stdu_bo_fifo_commit(struct vmw_kms_dirty *dirty)
561 {
562         struct vmw_stdu_dirty *ddirty =
563                 container_of(dirty, struct vmw_stdu_dirty, base);
564         struct vmw_screen_target_display_unit *stdu =
565                 container_of(dirty->unit, typeof(*stdu), base);
566         struct vmw_stdu_dma *cmd = dirty->cmd;
567         struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
568         SVGA3dCmdSurfaceDMASuffix *suffix =
569                 (SVGA3dCmdSurfaceDMASuffix *) &blit[dirty->num_hits];
570         size_t blit_size = sizeof(*blit) * dirty->num_hits + sizeof(*suffix);
571
572         if (!dirty->num_hits) {
573                 vmw_fifo_commit(dirty->dev_priv, 0);
574                 return;
575         }
576
577         cmd->header.id = SVGA_3D_CMD_SURFACE_DMA;
578         cmd->header.size = sizeof(cmd->body) + blit_size;
579         vmw_bo_get_guest_ptr(&ddirty->buf->base, &cmd->body.guest.ptr);
580         cmd->body.guest.pitch = ddirty->pitch;
581         cmd->body.host.sid = stdu->display_srf->res.id;
582         cmd->body.host.face = 0;
583         cmd->body.host.mipmap = 0;
584         cmd->body.transfer = ddirty->transfer;
585         suffix->suffixSize = sizeof(*suffix);
586         suffix->maximumOffset = ddirty->buf->base.num_pages * PAGE_SIZE;
587
588         if (ddirty->transfer == SVGA3D_WRITE_HOST_VRAM) {
589                 blit_size += sizeof(struct vmw_stdu_update);
590
591                 vmw_stdu_populate_update(&suffix[1], stdu->base.unit,
592                                          ddirty->left, ddirty->right,
593                                          ddirty->top, ddirty->bottom);
594         }
595
596         vmw_fifo_commit(dirty->dev_priv, sizeof(*cmd) + blit_size);
597
598         ddirty->left = ddirty->top = S32_MAX;
599         ddirty->right = ddirty->bottom = S32_MIN;
600 }
601
602
603 /**
604  * vmw_stdu_bo_cpu_clip - Callback to encode a CPU blit
605  *
606  * @dirty: The closure structure.
607  *
608  * This function calculates the bounding box for all the incoming clips.
609  */
610 static void vmw_stdu_bo_cpu_clip(struct vmw_kms_dirty *dirty)
611 {
612         struct vmw_stdu_dirty *ddirty =
613                 container_of(dirty, struct vmw_stdu_dirty, base);
614
615         dirty->num_hits = 1;
616
617         /* Calculate destination bounding box */
618         ddirty->left = min_t(s32, ddirty->left, dirty->unit_x1);
619         ddirty->top = min_t(s32, ddirty->top, dirty->unit_y1);
620         ddirty->right = max_t(s32, ddirty->right, dirty->unit_x2);
621         ddirty->bottom = max_t(s32, ddirty->bottom, dirty->unit_y2);
622
623         /*
624          * Calculate content bounding box.  We only need the top-left
625          * coordinate because width and height will be the same as the
626          * destination bounding box above
627          */
628         ddirty->fb_left = min_t(s32, ddirty->fb_left, dirty->fb_x);
629         ddirty->fb_top  = min_t(s32, ddirty->fb_top, dirty->fb_y);
630 }
631
632
633 /**
634  * vmw_stdu_bo_cpu_commit - Callback to do a CPU blit from buffer object
635  *
636  * @dirty: The closure structure.
637  *
638  * For the special case when we cannot create a proxy surface in a
639  * 2D VM, we have to do a CPU blit ourselves.
640  */
641 static void vmw_stdu_bo_cpu_commit(struct vmw_kms_dirty *dirty)
642 {
643         struct vmw_stdu_dirty *ddirty =
644                 container_of(dirty, struct vmw_stdu_dirty, base);
645         struct vmw_screen_target_display_unit *stdu =
646                 container_of(dirty->unit, typeof(*stdu), base);
647         s32 width, height;
648         s32 src_pitch, dst_pitch;
649         struct ttm_buffer_object *src_bo, *dst_bo;
650         u32 src_offset, dst_offset;
651         struct vmw_diff_cpy diff = VMW_CPU_BLIT_DIFF_INITIALIZER(stdu->cpp);
652
653         if (!dirty->num_hits)
654                 return;
655
656         width = ddirty->right - ddirty->left;
657         height = ddirty->bottom - ddirty->top;
658
659         if (width == 0 || height == 0)
660                 return;
661
662         /* Assume we are blitting from Guest (bo) to Host (display_srf) */
663         dst_pitch = stdu->display_srf->base_size.width * stdu->cpp;
664         dst_bo = &stdu->display_srf->res.backup->base;
665         dst_offset = ddirty->top * dst_pitch + ddirty->left * stdu->cpp;
666
667         src_pitch = ddirty->pitch;
668         src_bo = &ddirty->buf->base;
669         src_offset = ddirty->fb_top * src_pitch + ddirty->fb_left * stdu->cpp;
670
671         /* Swap src and dst if the assumption was wrong. */
672         if (ddirty->transfer != SVGA3D_WRITE_HOST_VRAM) {
673                 swap(dst_pitch, src_pitch);
674                 swap(dst_bo, src_bo);
675                 swap(src_offset, dst_offset);
676         }
677
678         (void) vmw_bo_cpu_blit(dst_bo, dst_offset, dst_pitch,
679                                src_bo, src_offset, src_pitch,
680                                width * stdu->cpp, height, &diff);
681
682         if (ddirty->transfer == SVGA3D_WRITE_HOST_VRAM &&
683             drm_rect_visible(&diff.rect)) {
684                 struct vmw_private *dev_priv;
685                 struct vmw_stdu_update *cmd;
686                 struct drm_clip_rect region;
687                 int ret;
688
689                 /* We are updating the actual surface, not a proxy */
690                 region.x1 = diff.rect.x1;
691                 region.x2 = diff.rect.x2;
692                 region.y1 = diff.rect.y1;
693                 region.y2 = diff.rect.y2;
694                 ret = vmw_kms_update_proxy(
695                         (struct vmw_resource *) &stdu->display_srf->res,
696                         (const struct drm_clip_rect *) &region, 1, 1);
697                 if (ret)
698                         goto out_cleanup;
699
700
701                 dev_priv = vmw_priv(stdu->base.crtc.dev);
702                 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
703
704                 if (!cmd) {
705                         DRM_ERROR("Cannot reserve FIFO space to update STDU");
706                         goto out_cleanup;
707                 }
708
709                 vmw_stdu_populate_update(cmd, stdu->base.unit,
710                                          region.x1, region.x2,
711                                          region.y1, region.y2);
712
713                 vmw_fifo_commit(dev_priv, sizeof(*cmd));
714         }
715
716 out_cleanup:
717         ddirty->left = ddirty->top = ddirty->fb_left = ddirty->fb_top = S32_MAX;
718         ddirty->right = ddirty->bottom = S32_MIN;
719 }
720
721 /**
722  * vmw_kms_stdu_dma - Perform a DMA transfer between a buffer-object backed
723  * framebuffer and the screen target system.
724  *
725  * @dev_priv: Pointer to the device private structure.
726  * @file_priv: Pointer to a struct drm-file identifying the caller. May be
727  * set to NULL, but then @user_fence_rep must also be set to NULL.
728  * @vfb: Pointer to the buffer-object backed framebuffer.
729  * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
730  * @vclips: Alternate array of clip rects. Either @clips or @vclips must
731  * be NULL.
732  * @num_clips: Number of clip rects in @clips or @vclips.
733  * @increment: Increment to use when looping over @clips or @vclips.
734  * @to_surface: Whether to DMA to the screen target system as opposed to
735  * from the screen target system.
736  * @interruptible: Whether to perform waits interruptible if possible.
737  * @crtc: If crtc is passed, perform stdu dma on that crtc only.
738  *
739  * If DMA-ing till the screen target system, the function will also notify
740  * the screen target system that a bounding box of the cliprects has been
741  * updated.
742  * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
743  * interrupted.
744  */
745 int vmw_kms_stdu_dma(struct vmw_private *dev_priv,
746                      struct drm_file *file_priv,
747                      struct vmw_framebuffer *vfb,
748                      struct drm_vmw_fence_rep __user *user_fence_rep,
749                      struct drm_clip_rect *clips,
750                      struct drm_vmw_rect *vclips,
751                      uint32_t num_clips,
752                      int increment,
753                      bool to_surface,
754                      bool interruptible,
755                      struct drm_crtc *crtc)
756 {
757         struct vmw_buffer_object *buf =
758                 container_of(vfb, struct vmw_framebuffer_bo, base)->buffer;
759         struct vmw_stdu_dirty ddirty;
760         int ret;
761         bool cpu_blit = !(dev_priv->capabilities & SVGA_CAP_3D);
762         DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
763
764         /*
765          * VMs without 3D support don't have the surface DMA command and
766          * we'll be using a CPU blit, and the framebuffer should be moved out
767          * of VRAM.
768          */
769         ret = vmw_validation_add_bo(&val_ctx, buf, false, cpu_blit);
770         if (ret)
771                 return ret;
772
773         ret = vmw_validation_prepare(&val_ctx, NULL, interruptible);
774         if (ret)
775                 goto out_unref;
776
777         ddirty.transfer = (to_surface) ? SVGA3D_WRITE_HOST_VRAM :
778                 SVGA3D_READ_HOST_VRAM;
779         ddirty.left = ddirty.top = S32_MAX;
780         ddirty.right = ddirty.bottom = S32_MIN;
781         ddirty.fb_left = ddirty.fb_top = S32_MAX;
782         ddirty.pitch = vfb->base.pitches[0];
783         ddirty.buf = buf;
784         ddirty.base.fifo_commit = vmw_stdu_bo_fifo_commit;
785         ddirty.base.clip = vmw_stdu_bo_clip;
786         ddirty.base.fifo_reserve_size = sizeof(struct vmw_stdu_dma) +
787                 num_clips * sizeof(SVGA3dCopyBox) +
788                 sizeof(SVGA3dCmdSurfaceDMASuffix);
789         if (to_surface)
790                 ddirty.base.fifo_reserve_size += sizeof(struct vmw_stdu_update);
791
792
793         if (cpu_blit) {
794                 ddirty.base.fifo_commit = vmw_stdu_bo_cpu_commit;
795                 ddirty.base.clip = vmw_stdu_bo_cpu_clip;
796                 ddirty.base.fifo_reserve_size = 0;
797         }
798
799         ddirty.base.crtc = crtc;
800
801         ret = vmw_kms_helper_dirty(dev_priv, vfb, clips, vclips,
802                                    0, 0, num_clips, increment, &ddirty.base);
803
804         vmw_kms_helper_validation_finish(dev_priv, file_priv, &val_ctx, NULL,
805                                          user_fence_rep);
806         return ret;
807
808 out_unref:
809         vmw_validation_unref_lists(&val_ctx);
810         return ret;
811 }
812
813 /**
814  * vmw_stdu_surface_clip - Callback to encode a surface copy command cliprect
815  *
816  * @dirty: The closure structure.
817  *
818  * Encodes a surface copy command cliprect and updates the bounding box
819  * for the copy.
820  */
821 static void vmw_kms_stdu_surface_clip(struct vmw_kms_dirty *dirty)
822 {
823         struct vmw_stdu_dirty *sdirty =
824                 container_of(dirty, struct vmw_stdu_dirty, base);
825         struct vmw_stdu_surface_copy *cmd = dirty->cmd;
826         struct vmw_screen_target_display_unit *stdu =
827                 container_of(dirty->unit, typeof(*stdu), base);
828
829         if (sdirty->sid != stdu->display_srf->res.id) {
830                 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
831
832                 blit += dirty->num_hits;
833                 blit->srcx = dirty->fb_x;
834                 blit->srcy = dirty->fb_y;
835                 blit->x = dirty->unit_x1;
836                 blit->y = dirty->unit_y1;
837                 blit->d = 1;
838                 blit->w = dirty->unit_x2 - dirty->unit_x1;
839                 blit->h = dirty->unit_y2 - dirty->unit_y1;
840         }
841
842         dirty->num_hits++;
843
844         /* Destination bounding box */
845         sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1);
846         sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1);
847         sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2);
848         sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2);
849 }
850
851 /**
852  * vmw_stdu_surface_fifo_commit - Callback to fill in and submit a surface
853  * copy command.
854  *
855  * @dirty: The closure structure.
856  *
857  * Fills in the missing fields in a surface copy command, and encodes a screen
858  * target update command.
859  */
860 static void vmw_kms_stdu_surface_fifo_commit(struct vmw_kms_dirty *dirty)
861 {
862         struct vmw_stdu_dirty *sdirty =
863                 container_of(dirty, struct vmw_stdu_dirty, base);
864         struct vmw_screen_target_display_unit *stdu =
865                 container_of(dirty->unit, typeof(*stdu), base);
866         struct vmw_stdu_surface_copy *cmd = dirty->cmd;
867         struct vmw_stdu_update *update;
868         size_t blit_size = sizeof(SVGA3dCopyBox) * dirty->num_hits;
869         size_t commit_size;
870
871         if (!dirty->num_hits) {
872                 vmw_fifo_commit(dirty->dev_priv, 0);
873                 return;
874         }
875
876         if (sdirty->sid != stdu->display_srf->res.id) {
877                 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
878
879                 cmd->header.id = SVGA_3D_CMD_SURFACE_COPY;
880                 cmd->header.size = sizeof(cmd->body) + blit_size;
881                 cmd->body.src.sid = sdirty->sid;
882                 cmd->body.dest.sid = stdu->display_srf->res.id;
883                 update = (struct vmw_stdu_update *) &blit[dirty->num_hits];
884                 commit_size = sizeof(*cmd) + blit_size + sizeof(*update);
885         } else {
886                 update = dirty->cmd;
887                 commit_size = sizeof(*update);
888         }
889
890         vmw_stdu_populate_update(update, stdu->base.unit, sdirty->left,
891                                  sdirty->right, sdirty->top, sdirty->bottom);
892
893         vmw_fifo_commit(dirty->dev_priv, commit_size);
894
895         sdirty->left = sdirty->top = S32_MAX;
896         sdirty->right = sdirty->bottom = S32_MIN;
897 }
898
899 /**
900  * vmw_kms_stdu_surface_dirty - Dirty part of a surface backed framebuffer
901  *
902  * @dev_priv: Pointer to the device private structure.
903  * @framebuffer: Pointer to the surface-buffer backed framebuffer.
904  * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
905  * @vclips: Alternate array of clip rects. Either @clips or @vclips must
906  * be NULL.
907  * @srf: Pointer to surface to blit from. If NULL, the surface attached
908  * to @framebuffer will be used.
909  * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
910  * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
911  * @num_clips: Number of clip rects in @clips.
912  * @inc: Increment to use when looping over @clips.
913  * @out_fence: If non-NULL, will return a ref-counted pointer to a
914  * struct vmw_fence_obj. The returned fence pointer may be NULL in which
915  * case the device has already synchronized.
916  * @crtc: If crtc is passed, perform surface dirty on that crtc only.
917  *
918  * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
919  * interrupted.
920  */
921 int vmw_kms_stdu_surface_dirty(struct vmw_private *dev_priv,
922                                struct vmw_framebuffer *framebuffer,
923                                struct drm_clip_rect *clips,
924                                struct drm_vmw_rect *vclips,
925                                struct vmw_resource *srf,
926                                s32 dest_x,
927                                s32 dest_y,
928                                unsigned num_clips, int inc,
929                                struct vmw_fence_obj **out_fence,
930                                struct drm_crtc *crtc)
931 {
932         struct vmw_framebuffer_surface *vfbs =
933                 container_of(framebuffer, typeof(*vfbs), base);
934         struct vmw_stdu_dirty sdirty;
935         DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
936         int ret;
937
938         if (!srf)
939                 srf = &vfbs->surface->res;
940
941         ret = vmw_validation_add_resource(&val_ctx, srf, 0, NULL, NULL);
942         if (ret)
943                 return ret;
944
945         ret = vmw_validation_prepare(&val_ctx, &dev_priv->cmdbuf_mutex, true);
946         if (ret)
947                 goto out_unref;
948
949         if (vfbs->is_bo_proxy) {
950                 ret = vmw_kms_update_proxy(srf, clips, num_clips, inc);
951                 if (ret)
952                         goto out_finish;
953         }
954
955         sdirty.base.fifo_commit = vmw_kms_stdu_surface_fifo_commit;
956         sdirty.base.clip = vmw_kms_stdu_surface_clip;
957         sdirty.base.fifo_reserve_size = sizeof(struct vmw_stdu_surface_copy) +
958                 sizeof(SVGA3dCopyBox) * num_clips +
959                 sizeof(struct vmw_stdu_update);
960         sdirty.base.crtc = crtc;
961         sdirty.sid = srf->id;
962         sdirty.left = sdirty.top = S32_MAX;
963         sdirty.right = sdirty.bottom = S32_MIN;
964
965         ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
966                                    dest_x, dest_y, num_clips, inc,
967                                    &sdirty.base);
968 out_finish:
969         vmw_kms_helper_validation_finish(dev_priv, NULL, &val_ctx, out_fence,
970                                          NULL);
971
972         return ret;
973
974 out_unref:
975         vmw_validation_unref_lists(&val_ctx);
976         return ret;
977 }
978
979
980 /*
981  *  Screen Target CRTC dispatch table
982  */
983 static const struct drm_crtc_funcs vmw_stdu_crtc_funcs = {
984         .gamma_set = vmw_du_crtc_gamma_set,
985         .destroy = vmw_stdu_crtc_destroy,
986         .reset = vmw_du_crtc_reset,
987         .atomic_duplicate_state = vmw_du_crtc_duplicate_state,
988         .atomic_destroy_state = vmw_du_crtc_destroy_state,
989         .set_config = vmw_kms_set_config,
990         .page_flip = vmw_stdu_crtc_page_flip,
991 };
992
993
994
995 /******************************************************************************
996  * Screen Target Display Unit Encoder Functions
997  *****************************************************************************/
998
999 /**
1000  * vmw_stdu_encoder_destroy - cleans up the STDU
1001  *
1002  * @encoder: used the get the containing STDU
1003  *
1004  * vmwgfx cleans up crtc/encoder/connector all at the same time so technically
1005  * this can be a no-op.  Nevertheless, it doesn't hurt of have this in case
1006  * the common KMS code changes and somehow vmw_stdu_crtc_destroy() doesn't
1007  * get called.
1008  */
1009 static void vmw_stdu_encoder_destroy(struct drm_encoder *encoder)
1010 {
1011         vmw_stdu_destroy(vmw_encoder_to_stdu(encoder));
1012 }
1013
1014 static const struct drm_encoder_funcs vmw_stdu_encoder_funcs = {
1015         .destroy = vmw_stdu_encoder_destroy,
1016 };
1017
1018
1019
1020 /******************************************************************************
1021  * Screen Target Display Unit Connector Functions
1022  *****************************************************************************/
1023
1024 /**
1025  * vmw_stdu_connector_destroy - cleans up the STDU
1026  *
1027  * @connector: used to get the containing STDU
1028  *
1029  * vmwgfx cleans up crtc/encoder/connector all at the same time so technically
1030  * this can be a no-op.  Nevertheless, it doesn't hurt of have this in case
1031  * the common KMS code changes and somehow vmw_stdu_crtc_destroy() doesn't
1032  * get called.
1033  */
1034 static void vmw_stdu_connector_destroy(struct drm_connector *connector)
1035 {
1036         vmw_stdu_destroy(vmw_connector_to_stdu(connector));
1037 }
1038
1039
1040
1041 static const struct drm_connector_funcs vmw_stdu_connector_funcs = {
1042         .dpms = vmw_du_connector_dpms,
1043         .detect = vmw_du_connector_detect,
1044         .fill_modes = vmw_du_connector_fill_modes,
1045         .set_property = vmw_du_connector_set_property,
1046         .destroy = vmw_stdu_connector_destroy,
1047         .reset = vmw_du_connector_reset,
1048         .atomic_duplicate_state = vmw_du_connector_duplicate_state,
1049         .atomic_destroy_state = vmw_du_connector_destroy_state,
1050         .atomic_set_property = vmw_du_connector_atomic_set_property,
1051         .atomic_get_property = vmw_du_connector_atomic_get_property,
1052 };
1053
1054
1055 static const struct
1056 drm_connector_helper_funcs vmw_stdu_connector_helper_funcs = {
1057 };
1058
1059
1060
1061 /******************************************************************************
1062  * Screen Target Display Plane Functions
1063  *****************************************************************************/
1064
1065
1066
1067 /**
1068  * vmw_stdu_primary_plane_cleanup_fb - Unpins the display surface
1069  *
1070  * @plane:  display plane
1071  * @old_state: Contains the FB to clean up
1072  *
1073  * Unpins the display surface
1074  *
1075  * Returns 0 on success
1076  */
1077 static void
1078 vmw_stdu_primary_plane_cleanup_fb(struct drm_plane *plane,
1079                                   struct drm_plane_state *old_state)
1080 {
1081         struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
1082
1083         if (vps->surf)
1084                 WARN_ON(!vps->pinned);
1085
1086         vmw_du_plane_cleanup_fb(plane, old_state);
1087
1088         vps->content_fb_type = SAME_AS_DISPLAY;
1089         vps->cpp = 0;
1090 }
1091
1092
1093
1094 /**
1095  * vmw_stdu_primary_plane_prepare_fb - Readies the display surface
1096  *
1097  * @plane:  display plane
1098  * @new_state: info on the new plane state, including the FB
1099  *
1100  * This function allocates a new display surface if the content is
1101  * backed by a buffer object.  The display surface is pinned here, and it'll
1102  * be unpinned in .cleanup_fb()
1103  *
1104  * Returns 0 on success
1105  */
1106 static int
1107 vmw_stdu_primary_plane_prepare_fb(struct drm_plane *plane,
1108                                   struct drm_plane_state *new_state)
1109 {
1110         struct vmw_private *dev_priv = vmw_priv(plane->dev);
1111         struct drm_framebuffer *new_fb = new_state->fb;
1112         struct vmw_framebuffer *vfb;
1113         struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
1114         enum stdu_content_type new_content_type;
1115         struct vmw_framebuffer_surface *new_vfbs;
1116         struct drm_crtc *crtc = new_state->crtc;
1117         uint32_t hdisplay = new_state->crtc_w, vdisplay = new_state->crtc_h;
1118         int ret;
1119
1120         /* No FB to prepare */
1121         if (!new_fb) {
1122                 if (vps->surf) {
1123                         WARN_ON(vps->pinned != 0);
1124                         vmw_surface_unreference(&vps->surf);
1125                 }
1126
1127                 return 0;
1128         }
1129
1130         vfb = vmw_framebuffer_to_vfb(new_fb);
1131         new_vfbs = (vfb->bo) ? NULL : vmw_framebuffer_to_vfbs(new_fb);
1132
1133         if (new_vfbs && new_vfbs->surface->base_size.width == hdisplay &&
1134             new_vfbs->surface->base_size.height == vdisplay)
1135                 new_content_type = SAME_AS_DISPLAY;
1136         else if (vfb->bo)
1137                 new_content_type = SEPARATE_BO;
1138         else
1139                 new_content_type = SEPARATE_SURFACE;
1140
1141         if (new_content_type != SAME_AS_DISPLAY) {
1142                 struct vmw_surface content_srf;
1143                 struct drm_vmw_size display_base_size = {0};
1144
1145                 display_base_size.width  = hdisplay;
1146                 display_base_size.height = vdisplay;
1147                 display_base_size.depth  = 1;
1148
1149                 /*
1150                  * If content buffer is a buffer object, then we have to
1151                  * construct surface info
1152                  */
1153                 if (new_content_type == SEPARATE_BO) {
1154
1155                         switch (new_fb->format->cpp[0]*8) {
1156                         case 32:
1157                                 content_srf.format = SVGA3D_X8R8G8B8;
1158                                 break;
1159
1160                         case 16:
1161                                 content_srf.format = SVGA3D_R5G6B5;
1162                                 break;
1163
1164                         case 8:
1165                                 content_srf.format = SVGA3D_P8;
1166                                 break;
1167
1168                         default:
1169                                 DRM_ERROR("Invalid format\n");
1170                                 return -EINVAL;
1171                         }
1172
1173                         content_srf.flags             = 0;
1174                         content_srf.mip_levels[0]     = 1;
1175                         content_srf.multisample_count = 0;
1176                         content_srf.multisample_pattern =
1177                                 SVGA3D_MS_PATTERN_NONE;
1178                         content_srf.quality_level = SVGA3D_MS_QUALITY_NONE;
1179                 } else {
1180                         content_srf = *new_vfbs->surface;
1181                 }
1182
1183                 if (vps->surf) {
1184                         struct drm_vmw_size cur_base_size = vps->surf->base_size;
1185
1186                         if (cur_base_size.width != display_base_size.width ||
1187                             cur_base_size.height != display_base_size.height ||
1188                             vps->surf->format != content_srf.format) {
1189                                 WARN_ON(vps->pinned != 0);
1190                                 vmw_surface_unreference(&vps->surf);
1191                         }
1192
1193                 }
1194
1195                 if (!vps->surf) {
1196                         ret = vmw_surface_gb_priv_define
1197                                 (crtc->dev,
1198                                  /* Kernel visible only */
1199                                  0,
1200                                  content_srf.flags,
1201                                  content_srf.format,
1202                                  true,  /* a scanout buffer */
1203                                  content_srf.mip_levels[0],
1204                                  content_srf.multisample_count,
1205                                  0,
1206                                  display_base_size,
1207                                  content_srf.multisample_pattern,
1208                                  content_srf.quality_level,
1209                                  &vps->surf);
1210                         if (ret != 0) {
1211                                 DRM_ERROR("Couldn't allocate STDU surface.\n");
1212                                 return ret;
1213                         }
1214                 }
1215         } else {
1216                 /*
1217                  * prepare_fb and clean_fb should only take care of pinning
1218                  * and unpinning.  References are tracked by state objects.
1219                  * The only time we add a reference in prepare_fb is if the
1220                  * state object doesn't have a reference to begin with
1221                  */
1222                 if (vps->surf) {
1223                         WARN_ON(vps->pinned != 0);
1224                         vmw_surface_unreference(&vps->surf);
1225                 }
1226
1227                 vps->surf = vmw_surface_reference(new_vfbs->surface);
1228         }
1229
1230         if (vps->surf) {
1231
1232                 /* Pin new surface before flipping */
1233                 ret = vmw_resource_pin(&vps->surf->res, false);
1234                 if (ret)
1235                         goto out_srf_unref;
1236
1237                 vps->pinned++;
1238         }
1239
1240         vps->content_fb_type = new_content_type;
1241
1242         /*
1243          * This should only happen if the buffer object is too large to create a
1244          * proxy surface for.
1245          * If we are a 2D VM with a buffer object then we have to use CPU blit
1246          * so cache these mappings
1247          */
1248         if (vps->content_fb_type == SEPARATE_BO &&
1249             !(dev_priv->capabilities & SVGA_CAP_3D))
1250                 vps->cpp = new_fb->pitches[0] / new_fb->width;
1251
1252         return 0;
1253
1254 out_srf_unref:
1255         vmw_surface_unreference(&vps->surf);
1256         return ret;
1257 }
1258
1259
1260
1261 /**
1262  * vmw_stdu_primary_plane_atomic_update - formally switches STDU to new plane
1263  *
1264  * @plane: display plane
1265  * @old_state: Only used to get crtc info
1266  *
1267  * Formally update stdu->display_srf to the new plane, and bind the new
1268  * plane STDU.  This function is called during the commit phase when
1269  * all the preparation have been done and all the configurations have
1270  * been checked.
1271  */
1272 static void
1273 vmw_stdu_primary_plane_atomic_update(struct drm_plane *plane,
1274                                      struct drm_plane_state *old_state)
1275 {
1276         struct vmw_plane_state *vps = vmw_plane_state_to_vps(plane->state);
1277         struct drm_crtc *crtc = plane->state->crtc;
1278         struct vmw_screen_target_display_unit *stdu;
1279         struct drm_pending_vblank_event *event;
1280         struct vmw_private *dev_priv;
1281         int ret;
1282
1283         /*
1284          * We cannot really fail this function, so if we do, then output an
1285          * error and maintain consistent atomic state.
1286          */
1287         if (crtc && plane->state->fb) {
1288                 struct vmw_framebuffer *vfb =
1289                         vmw_framebuffer_to_vfb(plane->state->fb);
1290                 struct drm_vmw_rect vclips;
1291                 stdu = vmw_crtc_to_stdu(crtc);
1292                 dev_priv = vmw_priv(crtc->dev);
1293
1294                 stdu->display_srf = vps->surf;
1295                 stdu->content_fb_type = vps->content_fb_type;
1296                 stdu->cpp = vps->cpp;
1297
1298                 vclips.x = crtc->x;
1299                 vclips.y = crtc->y;
1300                 vclips.w = crtc->mode.hdisplay;
1301                 vclips.h = crtc->mode.vdisplay;
1302
1303                 ret = vmw_stdu_bind_st(dev_priv, stdu, &stdu->display_srf->res);
1304                 if (ret)
1305                         DRM_ERROR("Failed to bind surface to STDU.\n");
1306
1307                 if (vfb->bo)
1308                         ret = vmw_kms_stdu_dma(dev_priv, NULL, vfb, NULL, NULL,
1309                                                &vclips, 1, 1, true, false,
1310                                                crtc);
1311                 else
1312                         ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL,
1313                                                          &vclips, NULL, 0, 0,
1314                                                          1, 1, NULL, crtc);
1315                 if (ret)
1316                         DRM_ERROR("Failed to update STDU.\n");
1317         } else {
1318                 crtc = old_state->crtc;
1319                 stdu = vmw_crtc_to_stdu(crtc);
1320                 dev_priv = vmw_priv(crtc->dev);
1321
1322                 /*
1323                  * When disabling a plane, CRTC and FB should always be NULL
1324                  * together, otherwise it's an error.
1325                  * Here primary plane is being disable so blank the screen
1326                  * target display unit, if not already done.
1327                  */
1328                 if (!stdu->defined)
1329                         return;
1330
1331                 ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
1332                 if (ret)
1333                         DRM_ERROR("Failed to blank STDU\n");
1334
1335                 ret = vmw_stdu_update_st(dev_priv, stdu);
1336                 if (ret)
1337                         DRM_ERROR("Failed to update STDU.\n");
1338
1339                 return;
1340         }
1341
1342         event = crtc->state->event;
1343         /*
1344          * In case of failure and other cases, vblank event will be sent in
1345          * vmw_du_crtc_atomic_flush.
1346          */
1347         if (event && (ret == 0)) {
1348                 struct vmw_fence_obj *fence = NULL;
1349                 struct drm_file *file_priv = event->base.file_priv;
1350
1351                 vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
1352
1353                 /*
1354                  * If fence is NULL, then already sync.
1355                  */
1356                 if (fence) {
1357                         ret = vmw_event_fence_action_queue(
1358                                 file_priv, fence, &event->base,
1359                                 &event->event.vbl.tv_sec,
1360                                 &event->event.vbl.tv_usec,
1361                                 true);
1362                         if (ret)
1363                                 DRM_ERROR("Failed to queue event on fence.\n");
1364                         else
1365                                 crtc->state->event = NULL;
1366
1367                         vmw_fence_obj_unreference(&fence);
1368                 }
1369         } else {
1370                 (void) vmw_fifo_flush(dev_priv, false);
1371         }
1372 }
1373
1374
1375 static const struct drm_plane_funcs vmw_stdu_plane_funcs = {
1376         .update_plane = drm_atomic_helper_update_plane,
1377         .disable_plane = drm_atomic_helper_disable_plane,
1378         .destroy = vmw_du_primary_plane_destroy,
1379         .reset = vmw_du_plane_reset,
1380         .atomic_duplicate_state = vmw_du_plane_duplicate_state,
1381         .atomic_destroy_state = vmw_du_plane_destroy_state,
1382 };
1383
1384 static const struct drm_plane_funcs vmw_stdu_cursor_funcs = {
1385         .update_plane = drm_atomic_helper_update_plane,
1386         .disable_plane = drm_atomic_helper_disable_plane,
1387         .destroy = vmw_du_cursor_plane_destroy,
1388         .reset = vmw_du_plane_reset,
1389         .atomic_duplicate_state = vmw_du_plane_duplicate_state,
1390         .atomic_destroy_state = vmw_du_plane_destroy_state,
1391 };
1392
1393
1394 /*
1395  * Atomic Helpers
1396  */
1397 static const struct
1398 drm_plane_helper_funcs vmw_stdu_cursor_plane_helper_funcs = {
1399         .atomic_check = vmw_du_cursor_plane_atomic_check,
1400         .atomic_update = vmw_du_cursor_plane_atomic_update,
1401         .prepare_fb = vmw_du_cursor_plane_prepare_fb,
1402         .cleanup_fb = vmw_du_plane_cleanup_fb,
1403 };
1404
1405 static const struct
1406 drm_plane_helper_funcs vmw_stdu_primary_plane_helper_funcs = {
1407         .atomic_check = vmw_du_primary_plane_atomic_check,
1408         .atomic_update = vmw_stdu_primary_plane_atomic_update,
1409         .prepare_fb = vmw_stdu_primary_plane_prepare_fb,
1410         .cleanup_fb = vmw_stdu_primary_plane_cleanup_fb,
1411 };
1412
1413 static const struct drm_crtc_helper_funcs vmw_stdu_crtc_helper_funcs = {
1414         .prepare = vmw_stdu_crtc_helper_prepare,
1415         .mode_set_nofb = vmw_stdu_crtc_mode_set_nofb,
1416         .atomic_check = vmw_du_crtc_atomic_check,
1417         .atomic_begin = vmw_du_crtc_atomic_begin,
1418         .atomic_flush = vmw_du_crtc_atomic_flush,
1419         .atomic_enable = vmw_stdu_crtc_atomic_enable,
1420         .atomic_disable = vmw_stdu_crtc_atomic_disable,
1421 };
1422
1423
1424 /**
1425  * vmw_stdu_init - Sets up a Screen Target Display Unit
1426  *
1427  * @dev_priv: VMW DRM device
1428  * @unit: unit number range from 0 to VMWGFX_NUM_DISPLAY_UNITS
1429  *
1430  * This function is called once per CRTC, and allocates one Screen Target
1431  * display unit to represent that CRTC.  Since the SVGA device does not separate
1432  * out encoder and connector, they are represented as part of the STDU as well.
1433  */
1434 static int vmw_stdu_init(struct vmw_private *dev_priv, unsigned unit)
1435 {
1436         struct vmw_screen_target_display_unit *stdu;
1437         struct drm_device *dev = dev_priv->dev;
1438         struct drm_connector *connector;
1439         struct drm_encoder *encoder;
1440         struct drm_plane *primary, *cursor;
1441         struct drm_crtc *crtc;
1442         int    ret;
1443
1444
1445         stdu = kzalloc(sizeof(*stdu), GFP_KERNEL);
1446         if (!stdu)
1447                 return -ENOMEM;
1448
1449         stdu->base.unit = unit;
1450         crtc = &stdu->base.crtc;
1451         encoder = &stdu->base.encoder;
1452         connector = &stdu->base.connector;
1453         primary = &stdu->base.primary;
1454         cursor = &stdu->base.cursor;
1455
1456         stdu->base.pref_active = (unit == 0);
1457         stdu->base.pref_width  = dev_priv->initial_width;
1458         stdu->base.pref_height = dev_priv->initial_height;
1459
1460         /*
1461          * Remove this after enabling atomic because property values can
1462          * only exist in a state object
1463          */
1464         stdu->base.is_implicit = false;
1465
1466         /* Initialize primary plane */
1467         vmw_du_plane_reset(primary);
1468
1469         ret = drm_universal_plane_init(dev, primary,
1470                                        0, &vmw_stdu_plane_funcs,
1471                                        vmw_primary_plane_formats,
1472                                        ARRAY_SIZE(vmw_primary_plane_formats),
1473                                        NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
1474         if (ret) {
1475                 DRM_ERROR("Failed to initialize primary plane");
1476                 goto err_free;
1477         }
1478
1479         drm_plane_helper_add(primary, &vmw_stdu_primary_plane_helper_funcs);
1480
1481         /* Initialize cursor plane */
1482         vmw_du_plane_reset(cursor);
1483
1484         ret = drm_universal_plane_init(dev, cursor,
1485                         0, &vmw_stdu_cursor_funcs,
1486                         vmw_cursor_plane_formats,
1487                         ARRAY_SIZE(vmw_cursor_plane_formats),
1488                         NULL, DRM_PLANE_TYPE_CURSOR, NULL);
1489         if (ret) {
1490                 DRM_ERROR("Failed to initialize cursor plane");
1491                 drm_plane_cleanup(&stdu->base.primary);
1492                 goto err_free;
1493         }
1494
1495         drm_plane_helper_add(cursor, &vmw_stdu_cursor_plane_helper_funcs);
1496
1497         vmw_du_connector_reset(connector);
1498
1499         ret = drm_connector_init(dev, connector, &vmw_stdu_connector_funcs,
1500                                  DRM_MODE_CONNECTOR_VIRTUAL);
1501         if (ret) {
1502                 DRM_ERROR("Failed to initialize connector\n");
1503                 goto err_free;
1504         }
1505
1506         drm_connector_helper_add(connector, &vmw_stdu_connector_helper_funcs);
1507         connector->status = vmw_du_connector_detect(connector, false);
1508         vmw_connector_state_to_vcs(connector->state)->is_implicit = false;
1509
1510         ret = drm_encoder_init(dev, encoder, &vmw_stdu_encoder_funcs,
1511                                DRM_MODE_ENCODER_VIRTUAL, NULL);
1512         if (ret) {
1513                 DRM_ERROR("Failed to initialize encoder\n");
1514                 goto err_free_connector;
1515         }
1516
1517         (void) drm_connector_attach_encoder(connector, encoder);
1518         encoder->possible_crtcs = (1 << unit);
1519         encoder->possible_clones = 0;
1520
1521         ret = drm_connector_register(connector);
1522         if (ret) {
1523                 DRM_ERROR("Failed to register connector\n");
1524                 goto err_free_encoder;
1525         }
1526
1527         vmw_du_crtc_reset(crtc);
1528         ret = drm_crtc_init_with_planes(dev, crtc, &stdu->base.primary,
1529                                         &stdu->base.cursor,
1530                                         &vmw_stdu_crtc_funcs, NULL);
1531         if (ret) {
1532                 DRM_ERROR("Failed to initialize CRTC\n");
1533                 goto err_free_unregister;
1534         }
1535
1536         drm_crtc_helper_add(crtc, &vmw_stdu_crtc_helper_funcs);
1537
1538         drm_mode_crtc_set_gamma_size(crtc, 256);
1539
1540         drm_object_attach_property(&connector->base,
1541                                    dev_priv->hotplug_mode_update_property, 1);
1542         drm_object_attach_property(&connector->base,
1543                                    dev->mode_config.suggested_x_property, 0);
1544         drm_object_attach_property(&connector->base,
1545                                    dev->mode_config.suggested_y_property, 0);
1546         if (dev_priv->implicit_placement_property)
1547                 drm_object_attach_property
1548                         (&connector->base,
1549                          dev_priv->implicit_placement_property,
1550                          stdu->base.is_implicit);
1551         return 0;
1552
1553 err_free_unregister:
1554         drm_connector_unregister(connector);
1555 err_free_encoder:
1556         drm_encoder_cleanup(encoder);
1557 err_free_connector:
1558         drm_connector_cleanup(connector);
1559 err_free:
1560         kfree(stdu);
1561         return ret;
1562 }
1563
1564
1565
1566 /**
1567  *  vmw_stdu_destroy - Cleans up a vmw_screen_target_display_unit
1568  *
1569  *  @stdu:  Screen Target Display Unit to be destroyed
1570  *
1571  *  Clean up after vmw_stdu_init
1572  */
1573 static void vmw_stdu_destroy(struct vmw_screen_target_display_unit *stdu)
1574 {
1575         vmw_du_cleanup(&stdu->base);
1576         kfree(stdu);
1577 }
1578
1579
1580
1581 /******************************************************************************
1582  * Screen Target Display KMS Functions
1583  *
1584  * These functions are called by the common KMS code in vmwgfx_kms.c
1585  *****************************************************************************/
1586
1587 /**
1588  * vmw_kms_stdu_init_display - Initializes a Screen Target based display
1589  *
1590  * @dev_priv: VMW DRM device
1591  *
1592  * This function initialize a Screen Target based display device.  It checks
1593  * the capability bits to make sure the underlying hardware can support
1594  * screen targets, and then creates the maximum number of CRTCs, a.k.a Display
1595  * Units, as supported by the display hardware.
1596  *
1597  * RETURNS:
1598  * 0 on success, error code otherwise
1599  */
1600 int vmw_kms_stdu_init_display(struct vmw_private *dev_priv)
1601 {
1602         struct drm_device *dev = dev_priv->dev;
1603         int i, ret;
1604
1605
1606         /* Do nothing if Screen Target support is turned off */
1607         if (!VMWGFX_ENABLE_SCREEN_TARGET_OTABLE)
1608                 return -ENOSYS;
1609
1610         if (!(dev_priv->capabilities & SVGA_CAP_GBOBJECTS))
1611                 return -ENOSYS;
1612
1613         ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
1614         if (unlikely(ret != 0))
1615                 return ret;
1616
1617         dev_priv->active_display_unit = vmw_du_screen_target;
1618
1619         vmw_kms_create_implicit_placement_property(dev_priv, false);
1620
1621         for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i) {
1622                 ret = vmw_stdu_init(dev_priv, i);
1623
1624                 if (unlikely(ret != 0)) {
1625                         DRM_ERROR("Failed to initialize STDU %d", i);
1626                         return ret;
1627                 }
1628         }
1629
1630         DRM_INFO("Screen Target Display device initialized\n");
1631
1632         return 0;
1633 }
This page took 0.133141 seconds and 4 git commands to generate.