]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/vmwgfx/vmwgfx_ldu.c
drm/vmwgfx: CRTC atomic state
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / vmwgfx / vmwgfx_ldu.c
CommitLineData
fb1d9738
JB
1/**************************************************************************
2 *
54fbde8a 3 * Copyright © 2009-2015 VMware, Inc., Palo Alto, CA., USA
fb1d9738
JB
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"
3cb9ae4f 29#include <drm/drm_plane_helper.h>
fb1d9738 30
7a1c2f6c 31
fb1d9738
JB
32#define vmw_crtc_to_ldu(x) \
33 container_of(x, struct vmw_legacy_display_unit, base.crtc)
34#define vmw_encoder_to_ldu(x) \
35 container_of(x, struct vmw_legacy_display_unit, base.encoder)
36#define vmw_connector_to_ldu(x) \
37 container_of(x, struct vmw_legacy_display_unit, base.connector)
38
39struct vmw_legacy_display {
40 struct list_head active;
41
42 unsigned num_active;
d7e1958d 43 unsigned last_num_active;
fb1d9738
JB
44
45 struct vmw_framebuffer *fb;
46};
47
48/**
49 * Display unit using the legacy register interface.
50 */
51struct vmw_legacy_display_unit {
52 struct vmw_display_unit base;
53
54 struct list_head active;
fb1d9738
JB
55};
56
57static void vmw_ldu_destroy(struct vmw_legacy_display_unit *ldu)
58{
59 list_del_init(&ldu->active);
c8261a96 60 vmw_du_cleanup(&ldu->base);
fb1d9738
JB
61 kfree(ldu);
62}
63
64
65/*
66 * Legacy Display Unit CRTC functions
67 */
68
fb1d9738
JB
69static void vmw_ldu_crtc_destroy(struct drm_crtc *crtc)
70{
71 vmw_ldu_destroy(vmw_crtc_to_ldu(crtc));
72}
73
74static int vmw_ldu_commit_list(struct vmw_private *dev_priv)
75{
76 struct vmw_legacy_display *lds = dev_priv->ldu_priv;
77 struct vmw_legacy_display_unit *entry;
d7e1958d
JB
78 struct drm_framebuffer *fb = NULL;
79 struct drm_crtc *crtc = NULL;
36cc79bc 80 int i = 0;
fb1d9738 81
d7e1958d
JB
82 /* If there is no display topology the host just assumes
83 * that the guest will set the same layout as the host.
84 */
85 if (!(dev_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY)) {
86 int w = 0, h = 0;
87 list_for_each_entry(entry, &lds->active, active) {
88 crtc = &entry->base.crtc;
89 w = max(w, crtc->x + crtc->mode.hdisplay);
90 h = max(h, crtc->y + crtc->mode.vdisplay);
91 i++;
92 }
93
94 if (crtc == NULL)
95 return 0;
f4510a27 96 fb = entry->base.crtc.primary->fb;
d7e1958d 97
01f2c773 98 return vmw_kms_write_svga(dev_priv, w, h, fb->pitches[0],
272725c7 99 fb->format->cpp[0] * 8,
b00c600e 100 fb->format->depth);
d7e1958d
JB
101 }
102
d7e1958d
JB
103 if (!list_empty(&lds->active)) {
104 entry = list_entry(lds->active.next, typeof(*entry), active);
f4510a27 105 fb = entry->base.crtc.primary->fb;
d7e1958d 106
01f2c773 107 vmw_kms_write_svga(dev_priv, fb->width, fb->height, fb->pitches[0],
272725c7 108 fb->format->cpp[0] * 8, fb->format->depth);
d7e1958d
JB
109 }
110
259600d5
JB
111 /* Make sure we always show something. */
112 vmw_write(dev_priv, SVGA_REG_NUM_GUEST_DISPLAYS,
113 lds->num_active ? lds->num_active : 1);
114
fb1d9738
JB
115 i = 0;
116 list_for_each_entry(entry, &lds->active, active) {
117 crtc = &entry->base.crtc;
118
119 vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, i);
120 vmw_write(dev_priv, SVGA_REG_DISPLAY_IS_PRIMARY, !i);
121 vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_X, crtc->x);
122 vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_Y, crtc->y);
123 vmw_write(dev_priv, SVGA_REG_DISPLAY_WIDTH, crtc->mode.hdisplay);
124 vmw_write(dev_priv, SVGA_REG_DISPLAY_HEIGHT, crtc->mode.vdisplay);
125 vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
126
127 i++;
128 }
129
d7e1958d
JB
130 BUG_ON(i != lds->num_active);
131
132 lds->last_num_active = lds->num_active;
133
fb1d9738
JB
134 return 0;
135}
136
137static int vmw_ldu_del_active(struct vmw_private *vmw_priv,
138 struct vmw_legacy_display_unit *ldu)
139{
140 struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
141 if (list_empty(&ldu->active))
142 return 0;
143
6a591a96 144 /* Must init otherwise list_empty(&ldu->active) will not work. */
fb1d9738
JB
145 list_del_init(&ldu->active);
146 if (--(ld->num_active) == 0) {
147 BUG_ON(!ld->fb);
148 if (ld->fb->unpin)
149 ld->fb->unpin(ld->fb);
150 ld->fb = NULL;
151 }
152
153 return 0;
154}
155
156static int vmw_ldu_add_active(struct vmw_private *vmw_priv,
157 struct vmw_legacy_display_unit *ldu,
158 struct vmw_framebuffer *vfb)
159{
160 struct vmw_legacy_display *ld = vmw_priv->ldu_priv;
161 struct vmw_legacy_display_unit *entry;
162 struct list_head *at;
163
04e9e94d
JB
164 BUG_ON(!ld->num_active && ld->fb);
165 if (vfb != ld->fb) {
166 if (ld->fb && ld->fb->unpin)
167 ld->fb->unpin(ld->fb);
168 if (vfb->pin)
169 vfb->pin(vfb);
170 ld->fb = vfb;
171 }
172
fb1d9738
JB
173 if (!list_empty(&ldu->active))
174 return 0;
175
176 at = &ld->active;
177 list_for_each_entry(entry, &ld->active, active) {
bbfad336 178 if (entry->base.unit > ldu->base.unit)
fb1d9738
JB
179 break;
180
181 at = &entry->active;
182 }
183
184 list_add(&ldu->active, at);
04e9e94d
JB
185
186 ld->num_active++;
fb1d9738
JB
187
188 return 0;
189}
190
191static int vmw_ldu_crtc_set_config(struct drm_mode_set *set)
192{
193 struct vmw_private *dev_priv;
194 struct vmw_legacy_display_unit *ldu;
195 struct drm_connector *connector;
196 struct drm_display_mode *mode;
197 struct drm_encoder *encoder;
198 struct vmw_framebuffer *vfb;
199 struct drm_framebuffer *fb;
200 struct drm_crtc *crtc;
201
202 if (!set)
203 return -EINVAL;
204
205 if (!set->crtc)
206 return -EINVAL;
207
208 /* get the ldu */
209 crtc = set->crtc;
210 ldu = vmw_crtc_to_ldu(crtc);
211 vfb = set->fb ? vmw_framebuffer_to_vfb(set->fb) : NULL;
212 dev_priv = vmw_priv(crtc->dev);
213
214 if (set->num_connectors > 1) {
215 DRM_ERROR("to many connectors\n");
216 return -EINVAL;
217 }
218
219 if (set->num_connectors == 1 &&
220 set->connectors[0] != &ldu->base.connector) {
221 DRM_ERROR("connector doesn't match %p %p\n",
222 set->connectors[0], &ldu->base.connector);
223 return -EINVAL;
224 }
225
226 /* ldu only supports one fb active at the time */
227 if (dev_priv->ldu_priv->fb && vfb &&
6a591a96
JB
228 !(dev_priv->ldu_priv->num_active == 1 &&
229 !list_empty(&ldu->active)) &&
fb1d9738
JB
230 dev_priv->ldu_priv->fb != vfb) {
231 DRM_ERROR("Multiple framebuffers not supported\n");
232 return -EINVAL;
233 }
234
235 /* since they always map one to one these are safe */
236 connector = &ldu->base.connector;
237 encoder = &ldu->base.encoder;
238
239 /* should we turn the crtc off? */
240 if (set->num_connectors == 0 || !set->mode || !set->fb) {
241
242 connector->encoder = NULL;
243 encoder->crtc = NULL;
f4510a27 244 crtc->primary->fb = NULL;
c6c1f325 245 crtc->enabled = false;
fb1d9738
JB
246
247 vmw_ldu_del_active(dev_priv, ldu);
248
0bef23f9 249 return vmw_ldu_commit_list(dev_priv);
fb1d9738
JB
250 }
251
252
253 /* we now know we want to set a mode */
254 mode = set->mode;
255 fb = set->fb;
256
257 if (set->x + mode->hdisplay > fb->width ||
258 set->y + mode->vdisplay > fb->height) {
259 DRM_ERROR("set outside of framebuffer\n");
260 return -EINVAL;
261 }
262
153b3d5b 263 vmw_svga_enable(dev_priv);
fb1d9738 264
f4510a27 265 crtc->primary->fb = fb;
fb1d9738
JB
266 encoder->crtc = crtc;
267 connector->encoder = encoder;
268 crtc->x = set->x;
269 crtc->y = set->y;
270 crtc->mode = *mode;
c6c1f325 271 crtc->enabled = true;
6dd687b4
TH
272 ldu->base.set_gui_x = set->x;
273 ldu->base.set_gui_y = set->y;
fb1d9738
JB
274
275 vmw_ldu_add_active(dev_priv, ldu, vfb);
276
0bef23f9 277 return vmw_ldu_commit_list(dev_priv);
fb1d9738
JB
278}
279
d7955fcf 280static const struct drm_crtc_funcs vmw_legacy_crtc_funcs = {
626ab771 281 .gamma_set = vmw_du_crtc_gamma_set,
fb1d9738 282 .destroy = vmw_ldu_crtc_destroy,
9c2542a4
SY
283 .reset = vmw_du_crtc_reset,
284 .atomic_duplicate_state = vmw_du_crtc_duplicate_state,
285 .atomic_destroy_state = vmw_du_crtc_destroy_state,
fb1d9738
JB
286 .set_config = vmw_ldu_crtc_set_config,
287};
288
626ab771 289
fb1d9738
JB
290/*
291 * Legacy Display Unit encoder functions
292 */
293
294static void vmw_ldu_encoder_destroy(struct drm_encoder *encoder)
295{
296 vmw_ldu_destroy(vmw_encoder_to_ldu(encoder));
297}
298
d7955fcf 299static const struct drm_encoder_funcs vmw_legacy_encoder_funcs = {
fb1d9738
JB
300 .destroy = vmw_ldu_encoder_destroy,
301};
302
303/*
304 * Legacy Display Unit connector functions
305 */
306
fb1d9738
JB
307static void vmw_ldu_connector_destroy(struct drm_connector *connector)
308{
309 vmw_ldu_destroy(vmw_connector_to_ldu(connector));
310}
311
d7955fcf 312static const struct drm_connector_funcs vmw_legacy_connector_funcs = {
626ab771 313 .dpms = vmw_du_connector_dpms,
626ab771
JB
314 .detect = vmw_du_connector_detect,
315 .fill_modes = vmw_du_connector_fill_modes,
316 .set_property = vmw_du_connector_set_property,
fb1d9738
JB
317 .destroy = vmw_ldu_connector_destroy,
318};
319
36cc79bc
SY
320/*
321 * Legacy Display Plane Functions
322 */
323
324static const struct drm_plane_funcs vmw_ldu_plane_funcs = {
325 .update_plane = drm_primary_helper_update,
326 .disable_plane = drm_primary_helper_disable,
327 .destroy = vmw_du_primary_plane_destroy,
328};
329
330static const struct drm_plane_funcs vmw_ldu_cursor_funcs = {
331 .update_plane = vmw_du_cursor_plane_update,
332 .disable_plane = vmw_du_cursor_plane_disable,
333 .destroy = vmw_du_cursor_plane_destroy,
334};
335
336
fb1d9738
JB
337static int vmw_ldu_init(struct vmw_private *dev_priv, unsigned unit)
338{
339 struct vmw_legacy_display_unit *ldu;
340 struct drm_device *dev = dev_priv->dev;
341 struct drm_connector *connector;
342 struct drm_encoder *encoder;
343 struct drm_crtc *crtc;
36cc79bc 344 int ret;
fb1d9738
JB
345
346 ldu = kzalloc(sizeof(*ldu), GFP_KERNEL);
347 if (!ldu)
348 return -ENOMEM;
349
bbfad336 350 ldu->base.unit = unit;
fb1d9738
JB
351 crtc = &ldu->base.crtc;
352 encoder = &ldu->base.encoder;
353 connector = &ldu->base.connector;
354
1ae1ddd5
JB
355 INIT_LIST_HEAD(&ldu->active);
356
626ab771 357 ldu->base.pref_active = (unit == 0);
eb4f923b
JB
358 ldu->base.pref_width = dev_priv->initial_width;
359 ldu->base.pref_height = dev_priv->initial_height;
626ab771 360 ldu->base.pref_mode = NULL;
9c2542a4
SY
361
362 /*
363 * Remove this after enabling atomic because property values can
364 * only exist in a state object
365 */
6987427a 366 ldu->base.is_implicit = true;
d8bd19d2 367
36cc79bc
SY
368 /* Initialize primary plane */
369 ret = drm_universal_plane_init(dev, &ldu->base.primary,
370 0, &vmw_ldu_plane_funcs,
371 vmw_primary_plane_formats,
372 ARRAY_SIZE(vmw_primary_plane_formats),
373 DRM_PLANE_TYPE_PRIMARY, NULL);
374 if (ret) {
375 DRM_ERROR("Failed to initialize primary plane");
376 goto err_free;
377 }
378
379 /* Initialize cursor plane */
380 ret = drm_universal_plane_init(dev, &ldu->base.cursor,
381 0, &vmw_ldu_cursor_funcs,
382 vmw_cursor_plane_formats,
383 ARRAY_SIZE(vmw_cursor_plane_formats),
384 DRM_PLANE_TYPE_CURSOR, NULL);
385 if (ret) {
386 DRM_ERROR("Failed to initialize cursor plane");
387 drm_plane_cleanup(&ldu->base.primary);
388 goto err_free;
389 }
390
391 ret = drm_connector_init(dev, connector, &vmw_legacy_connector_funcs,
392 DRM_MODE_CONNECTOR_VIRTUAL);
393 if (ret) {
394 DRM_ERROR("Failed to initialize connector\n");
395 goto err_free;
396 }
626ab771 397 connector->status = vmw_du_connector_detect(connector, true);
fb1d9738 398
36cc79bc
SY
399 ret = drm_encoder_init(dev, encoder, &vmw_legacy_encoder_funcs,
400 DRM_MODE_ENCODER_VIRTUAL, NULL);
401 if (ret) {
402 DRM_ERROR("Failed to initialize encoder\n");
403 goto err_free_connector;
404 }
405
406 (void) drm_mode_connector_attach_encoder(connector, encoder);
fb1d9738
JB
407 encoder->possible_crtcs = (1 << unit);
408 encoder->possible_clones = 0;
409
36cc79bc
SY
410 ret = drm_connector_register(connector);
411 if (ret) {
412 DRM_ERROR("Failed to register connector\n");
413 goto err_free_encoder;
414 }
6a0a7a9e 415
9c2542a4
SY
416 /* FIXME: Turn on after plane/connector states are implemented. */
417 /* vmw_du_crtc_reset(crtc); */
36cc79bc
SY
418 ret = drm_crtc_init_with_planes(dev, crtc, &ldu->base.primary,
419 &ldu->base.cursor,
420 &vmw_legacy_crtc_funcs, NULL);
421 if (ret) {
422 DRM_ERROR("Failed to initialize CRTC\n");
423 goto err_free_unregister;
424 }
fb1d9738 425
f01b7ba0
MD
426 drm_mode_crtc_set_gamma_size(crtc, 256);
427
578e609a
TH
428 drm_object_attach_property(&connector->base,
429 dev_priv->hotplug_mode_update_property, 1);
430 drm_object_attach_property(&connector->base,
431 dev->mode_config.suggested_x_property, 0);
432 drm_object_attach_property(&connector->base,
433 dev->mode_config.suggested_y_property, 0);
76404ac0
TH
434 if (dev_priv->implicit_placement_property)
435 drm_object_attach_property
436 (&connector->base,
437 dev_priv->implicit_placement_property,
438 1);
fb1d9738
JB
439
440 return 0;
36cc79bc
SY
441
442err_free_unregister:
443 drm_connector_unregister(connector);
444err_free_encoder:
445 drm_encoder_cleanup(encoder);
446err_free_connector:
447 drm_connector_cleanup(connector);
448err_free:
449 kfree(ldu);
450 return ret;
fb1d9738
JB
451}
452
c8261a96 453int vmw_kms_ldu_init_display(struct vmw_private *dev_priv)
fb1d9738 454{
7a1c2f6c 455 struct drm_device *dev = dev_priv->dev;
74b5ea30 456 int i, ret;
7a1c2f6c 457
fb1d9738
JB
458 if (dev_priv->ldu_priv) {
459 DRM_INFO("ldu system already on\n");
460 return -EINVAL;
461 }
462
85b54e0c 463 dev_priv->ldu_priv = kmalloc(sizeof(*dev_priv->ldu_priv), GFP_KERNEL);
fb1d9738
JB
464 if (!dev_priv->ldu_priv)
465 return -ENOMEM;
466
467 INIT_LIST_HEAD(&dev_priv->ldu_priv->active);
468 dev_priv->ldu_priv->num_active = 0;
d7e1958d 469 dev_priv->ldu_priv->last_num_active = 0;
fb1d9738
JB
470 dev_priv->ldu_priv->fb = NULL;
471
60a16a30
JB
472 /* for old hardware without multimon only enable one display */
473 if (dev_priv->capabilities & SVGA_CAP_MULTIMON)
474 ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
475 else
476 ret = drm_vblank_init(dev, 1);
477 if (ret != 0)
478 goto err_free;
fb1d9738 479
76404ac0
TH
480 vmw_kms_create_implicit_placement_property(dev_priv, true);
481
60a16a30 482 if (dev_priv->capabilities & SVGA_CAP_MULTIMON)
56d1c78d 483 for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
7a1c2f6c 484 vmw_ldu_init(dev_priv, i);
60a16a30 485 else
7a1c2f6c 486 vmw_ldu_init(dev_priv, 0);
fb1d9738 487
c8261a96
SY
488 dev_priv->active_display_unit = vmw_du_legacy;
489
490 DRM_INFO("Legacy Display Unit initialized\n");
491
60a16a30
JB
492 return 0;
493
60a16a30
JB
494err_free:
495 kfree(dev_priv->ldu_priv);
496 dev_priv->ldu_priv = NULL;
7a1c2f6c 497 return ret;
fb1d9738
JB
498}
499
c8261a96 500int vmw_kms_ldu_close_display(struct vmw_private *dev_priv)
fb1d9738 501{
7a1c2f6c
TH
502 struct drm_device *dev = dev_priv->dev;
503
fb1d9738
JB
504 if (!dev_priv->ldu_priv)
505 return -ENOSYS;
506
60a16a30
JB
507 drm_vblank_cleanup(dev);
508
fb1d9738
JB
509 BUG_ON(!list_empty(&dev_priv->ldu_priv->active));
510
511 kfree(dev_priv->ldu_priv);
512
513 return 0;
514}
c8261a96
SY
515
516
517int vmw_kms_ldu_do_dmabuf_dirty(struct vmw_private *dev_priv,
518 struct vmw_framebuffer *framebuffer,
519 unsigned flags, unsigned color,
520 struct drm_clip_rect *clips,
521 unsigned num_clips, int increment)
522{
523 size_t fifo_size;
524 int i;
525
526 struct {
527 uint32_t header;
528 SVGAFifoCmdUpdate body;
529 } *cmd;
530
531 fifo_size = sizeof(*cmd) * num_clips;
532 cmd = vmw_fifo_reserve(dev_priv, fifo_size);
533 if (unlikely(cmd == NULL)) {
534 DRM_ERROR("Fifo reserve failed.\n");
535 return -ENOMEM;
536 }
537
538 memset(cmd, 0, fifo_size);
539 for (i = 0; i < num_clips; i++, clips += increment) {
b9eb1a61
TH
540 cmd[i].header = SVGA_CMD_UPDATE;
541 cmd[i].body.x = clips->x1;
542 cmd[i].body.y = clips->y1;
543 cmd[i].body.width = clips->x2 - clips->x1;
544 cmd[i].body.height = clips->y2 - clips->y1;
c8261a96
SY
545 }
546
547 vmw_fifo_commit(dev_priv, fifo_size);
548 return 0;
549}