1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
3 *
4 * Copyright 2009-2022 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 <drm/drm_atomic.h>
29 #include <drm/drm_atomic_helper.h>
30 #include <drm/drm_fourcc.h>
31
32 #include "vmwgfx_kms.h"
33
34 #define vmw_crtc_to_ldu(x) \
35 container_of(x, struct vmw_legacy_display_unit, base.crtc)
36 #define vmw_encoder_to_ldu(x) \
37 container_of(x, struct vmw_legacy_display_unit, base.encoder)
38 #define vmw_connector_to_ldu(x) \
39 container_of(x, struct vmw_legacy_display_unit, base.connector)
40
41 struct vmw_legacy_display {
42 struct list_head active;
43
44 unsigned num_active;
45 unsigned last_num_active;
46
47 struct vmw_framebuffer *fb;
48 };
49
50 /*
51 * Display unit using the legacy register interface.
52 */
53 struct vmw_legacy_display_unit {
54 struct vmw_display_unit base;
55
56 struct list_head active;
57 };
58
vmw_ldu_destroy(struct vmw_legacy_display_unit * ldu)59 static void vmw_ldu_destroy(struct vmw_legacy_display_unit *ldu)
60 {
61 list_del_init(&ldu->active);
62 vmw_du_cleanup(&ldu->base);
63 kfree(ldu);
64 }
65
66
67 /*
68 * Legacy Display Unit CRTC functions
69 */
70
vmw_ldu_crtc_destroy(struct drm_crtc * crtc)71 static void vmw_ldu_crtc_destroy(struct drm_crtc *crtc)
72 {
73 vmw_ldu_destroy(vmw_crtc_to_ldu(crtc));
74 }
75
vmw_ldu_commit_list(struct vmw_private * dev_priv)76 static int vmw_ldu_commit_list(struct vmw_private *dev_priv)
77 {
78 struct vmw_legacy_display *lds = dev_priv->ldu_priv;
79 struct vmw_legacy_display_unit *entry;
80 struct drm_framebuffer *fb = NULL;
81 struct drm_crtc *crtc = NULL;
82 int i;
83
84 /* If there is no display topology the host just assumes
85 * that the guest will set the same layout as the host.
86 */
87 if (!(dev_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY)) {
88 int w = 0, h = 0;
89 list_for_each_entry(entry, &lds->active, active) {
90 crtc = &entry->base.crtc;
91 w = max(w, crtc->x + crtc->mode.hdisplay);
92 h = max(h, crtc->y + crtc->mode.vdisplay);
93 }
94
95 if (crtc == NULL)
96 return 0;
97 fb = crtc->primary->state->fb;
98
99 return vmw_kms_write_svga(dev_priv, w, h, fb->pitches[0],
100 fb->format->cpp[0] * 8,
101 fb->format->depth);
102 }
103
104 if (!list_empty(&lds->active)) {
105 entry = list_entry(lds->active.next, typeof(*entry), active);
106 fb = entry->base.crtc.primary->state->fb;
107
108 vmw_kms_write_svga(dev_priv, fb->width, fb->height, fb->pitches[0],
109 fb->format->cpp[0] * 8, fb->format->depth);
110 }
111
112 /* Make sure we always show something. */
113 vmw_write(dev_priv, SVGA_REG_NUM_GUEST_DISPLAYS,
114 lds->num_active ? lds->num_active : 1);
115
116 i = 0;
117 list_for_each_entry(entry, &lds->active, active) {
118 crtc = &entry->base.crtc;
119
120 vmw_write(dev_priv, SVGA_REG_DISPLAY_ID, i);
121 vmw_write(dev_priv, SVGA_REG_DISPLAY_IS_PRIMARY, !i);
122 vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_X, crtc->x);
123 vmw_write(dev_priv, SVGA_REG_DISPLAY_POSITION_Y, crtc->y);
124 vmw_write(dev_priv, SVGA_REG_DISPLAY_WIDTH, crtc->mode.hdisplay);
125 vmw_write(dev_priv, SVGA_REG_DISPLAY_HEIGHT, crtc->mode.vdisplay);
126
127 i++;
128 }
129
130 BUG_ON(i != lds->num_active);
131
132 lds->last_num_active = lds->num_active;
133
134 return 0;
135 }
136
vmw_ldu_del_active(struct vmw_private * vmw_priv,struct vmw_legacy_display_unit * ldu)137 static 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
144 /* Must init otherwise list_empty(&ldu->active) will not work. */
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
vmw_ldu_add_active(struct vmw_private * vmw_priv,struct vmw_legacy_display_unit * ldu,struct vmw_framebuffer * vfb)156 static 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
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 vmw_svga_enable(vmw_priv);
169 if (vfb->pin)
170 vfb->pin(vfb);
171 ld->fb = vfb;
172 }
173
174 if (!list_empty(&ldu->active))
175 return 0;
176
177 at = &ld->active;
178 list_for_each_entry(entry, &ld->active, active) {
179 if (entry->base.unit > ldu->base.unit)
180 break;
181
182 at = &entry->active;
183 }
184
185 list_add(&ldu->active, at);
186
187 ld->num_active++;
188
189 return 0;
190 }
191
192 /**
193 * vmw_ldu_crtc_mode_set_nofb - Enable svga
194 *
195 * @crtc: CRTC associated with the new screen
196 *
197 * For LDU, just enable the svga
198 */
vmw_ldu_crtc_mode_set_nofb(struct drm_crtc * crtc)199 static void vmw_ldu_crtc_mode_set_nofb(struct drm_crtc *crtc)
200 {
201 }
202
203 /**
204 * vmw_ldu_crtc_atomic_enable - Noop
205 *
206 * @crtc: CRTC associated with the new screen
207 * @state: Unused
208 *
209 * This is called after a mode set has been completed. Here's
210 * usually a good place to call vmw_ldu_add_active/vmw_ldu_del_active
211 * but since for LDU the display plane is closely tied to the
212 * CRTC, it makes more sense to do those at plane update time.
213 */
vmw_ldu_crtc_atomic_enable(struct drm_crtc * crtc,struct drm_atomic_state * state)214 static void vmw_ldu_crtc_atomic_enable(struct drm_crtc *crtc,
215 struct drm_atomic_state *state)
216 {
217 }
218
219 /**
220 * vmw_ldu_crtc_atomic_disable - Turns off CRTC
221 *
222 * @crtc: CRTC to be turned off
223 * @state: Unused
224 */
vmw_ldu_crtc_atomic_disable(struct drm_crtc * crtc,struct drm_atomic_state * state)225 static void vmw_ldu_crtc_atomic_disable(struct drm_crtc *crtc,
226 struct drm_atomic_state *state)
227 {
228 }
229
230 static const struct drm_crtc_funcs vmw_legacy_crtc_funcs = {
231 .gamma_set = vmw_du_crtc_gamma_set,
232 .destroy = vmw_ldu_crtc_destroy,
233 .reset = vmw_du_crtc_reset,
234 .atomic_duplicate_state = vmw_du_crtc_duplicate_state,
235 .atomic_destroy_state = vmw_du_crtc_destroy_state,
236 .set_config = drm_atomic_helper_set_config,
237 .page_flip = drm_atomic_helper_page_flip,
238 };
239
240
241 /*
242 * Legacy Display Unit encoder functions
243 */
244
vmw_ldu_encoder_destroy(struct drm_encoder * encoder)245 static void vmw_ldu_encoder_destroy(struct drm_encoder *encoder)
246 {
247 vmw_ldu_destroy(vmw_encoder_to_ldu(encoder));
248 }
249
250 static const struct drm_encoder_funcs vmw_legacy_encoder_funcs = {
251 .destroy = vmw_ldu_encoder_destroy,
252 };
253
254 /*
255 * Legacy Display Unit connector functions
256 */
257
vmw_ldu_connector_destroy(struct drm_connector * connector)258 static void vmw_ldu_connector_destroy(struct drm_connector *connector)
259 {
260 vmw_ldu_destroy(vmw_connector_to_ldu(connector));
261 }
262
263 static const struct drm_connector_funcs vmw_legacy_connector_funcs = {
264 .dpms = vmw_du_connector_dpms,
265 .detect = vmw_du_connector_detect,
266 .fill_modes = vmw_du_connector_fill_modes,
267 .destroy = vmw_ldu_connector_destroy,
268 .reset = vmw_du_connector_reset,
269 .atomic_duplicate_state = vmw_du_connector_duplicate_state,
270 .atomic_destroy_state = vmw_du_connector_destroy_state,
271 };
272
273 static const struct
274 drm_connector_helper_funcs vmw_ldu_connector_helper_funcs = {
275 };
276
277 static int vmw_kms_ldu_do_bo_dirty(struct vmw_private *dev_priv,
278 struct vmw_framebuffer *framebuffer,
279 unsigned int flags, unsigned int color,
280 struct drm_mode_rect *clips,
281 unsigned int num_clips);
282
283 /*
284 * Legacy Display Plane Functions
285 */
286
287 static void
vmw_ldu_primary_plane_atomic_update(struct drm_plane * plane,struct drm_atomic_state * state)288 vmw_ldu_primary_plane_atomic_update(struct drm_plane *plane,
289 struct drm_atomic_state *state)
290 {
291 struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
292 plane);
293 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
294 plane);
295 struct vmw_private *dev_priv;
296 struct vmw_legacy_display_unit *ldu;
297 struct vmw_framebuffer *vfb;
298 struct drm_framebuffer *fb;
299 struct drm_crtc *crtc = new_state->crtc ?: old_state->crtc;
300
301 ldu = vmw_crtc_to_ldu(crtc);
302 dev_priv = vmw_priv(plane->dev);
303 fb = new_state->fb;
304
305 vfb = (fb) ? vmw_framebuffer_to_vfb(fb) : NULL;
306
307 if (vfb)
308 vmw_ldu_add_active(dev_priv, ldu, vfb);
309 else
310 vmw_ldu_del_active(dev_priv, ldu);
311
312 vmw_ldu_commit_list(dev_priv);
313
314 if (vfb && vmw_cmd_supported(dev_priv)) {
315 struct drm_mode_rect fb_rect = {
316 .x1 = 0,
317 .y1 = 0,
318 .x2 = vfb->base.width,
319 .y2 = vfb->base.height
320 };
321 struct drm_mode_rect *damage_rects = drm_plane_get_damage_clips(new_state);
322 u32 rect_count = drm_plane_get_damage_clips_count(new_state);
323 int ret;
324
325 if (!damage_rects) {
326 damage_rects = &fb_rect;
327 rect_count = 1;
328 }
329
330 ret = vmw_kms_ldu_do_bo_dirty(dev_priv, vfb, 0, 0, damage_rects, rect_count);
331
332 drm_WARN_ONCE(plane->dev, ret,
333 "vmw_kms_ldu_do_bo_dirty failed with: ret=%d\n", ret);
334
335 vmw_cmd_flush(dev_priv, false);
336 }
337 }
338
339 static const struct drm_plane_funcs vmw_ldu_plane_funcs = {
340 .update_plane = drm_atomic_helper_update_plane,
341 .disable_plane = drm_atomic_helper_disable_plane,
342 .destroy = vmw_du_primary_plane_destroy,
343 .reset = vmw_du_plane_reset,
344 .atomic_duplicate_state = vmw_du_plane_duplicate_state,
345 .atomic_destroy_state = vmw_du_plane_destroy_state,
346 };
347
348 static const struct drm_plane_funcs vmw_ldu_cursor_funcs = {
349 .update_plane = drm_atomic_helper_update_plane,
350 .disable_plane = drm_atomic_helper_disable_plane,
351 .destroy = vmw_du_cursor_plane_destroy,
352 .reset = vmw_du_plane_reset,
353 .atomic_duplicate_state = vmw_du_plane_duplicate_state,
354 .atomic_destroy_state = vmw_du_plane_destroy_state,
355 };
356
357 /*
358 * Atomic Helpers
359 */
360 static const struct
361 drm_plane_helper_funcs vmw_ldu_cursor_plane_helper_funcs = {
362 .atomic_check = vmw_du_cursor_plane_atomic_check,
363 .atomic_update = vmw_du_cursor_plane_atomic_update,
364 .prepare_fb = vmw_du_cursor_plane_prepare_fb,
365 .cleanup_fb = vmw_du_cursor_plane_cleanup_fb,
366 };
367
368 static const struct
369 drm_plane_helper_funcs vmw_ldu_primary_plane_helper_funcs = {
370 .atomic_check = vmw_du_primary_plane_atomic_check,
371 .atomic_update = vmw_ldu_primary_plane_atomic_update,
372 };
373
374 static const struct drm_crtc_helper_funcs vmw_ldu_crtc_helper_funcs = {
375 .mode_set_nofb = vmw_ldu_crtc_mode_set_nofb,
376 .atomic_check = vmw_du_crtc_atomic_check,
377 .atomic_begin = vmw_du_crtc_atomic_begin,
378 .atomic_flush = vmw_du_crtc_atomic_flush,
379 .atomic_enable = vmw_ldu_crtc_atomic_enable,
380 .atomic_disable = vmw_ldu_crtc_atomic_disable,
381 };
382
383
vmw_ldu_init(struct vmw_private * dev_priv,unsigned unit)384 static int vmw_ldu_init(struct vmw_private *dev_priv, unsigned unit)
385 {
386 struct vmw_legacy_display_unit *ldu;
387 struct drm_device *dev = &dev_priv->drm;
388 struct drm_connector *connector;
389 struct drm_encoder *encoder;
390 struct drm_plane *primary;
391 struct vmw_cursor_plane *cursor;
392 struct drm_crtc *crtc;
393 int ret;
394
395 ldu = kzalloc(sizeof(*ldu), GFP_KERNEL);
396 if (!ldu)
397 return -ENOMEM;
398
399 ldu->base.unit = unit;
400 crtc = &ldu->base.crtc;
401 encoder = &ldu->base.encoder;
402 connector = &ldu->base.connector;
403 primary = &ldu->base.primary;
404 cursor = &ldu->base.cursor;
405
406 INIT_LIST_HEAD(&ldu->active);
407
408 ldu->base.pref_active = (unit == 0);
409 ldu->base.pref_width = dev_priv->initial_width;
410 ldu->base.pref_height = dev_priv->initial_height;
411 ldu->base.pref_mode = NULL;
412
413 /*
414 * Remove this after enabling atomic because property values can
415 * only exist in a state object
416 */
417 ldu->base.is_implicit = true;
418
419 /* Initialize primary plane */
420 ret = drm_universal_plane_init(dev, primary,
421 0, &vmw_ldu_plane_funcs,
422 vmw_primary_plane_formats,
423 ARRAY_SIZE(vmw_primary_plane_formats),
424 NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
425 if (ret) {
426 DRM_ERROR("Failed to initialize primary plane");
427 goto err_free;
428 }
429
430 drm_plane_helper_add(primary, &vmw_ldu_primary_plane_helper_funcs);
431
432 /*
433 * We're going to be using traces and software cursors
434 */
435 if (vmw_cmd_supported(dev_priv)) {
436 /* Initialize cursor plane */
437 ret = drm_universal_plane_init(dev, &cursor->base,
438 0, &vmw_ldu_cursor_funcs,
439 vmw_cursor_plane_formats,
440 ARRAY_SIZE(vmw_cursor_plane_formats),
441 NULL, DRM_PLANE_TYPE_CURSOR, NULL);
442 if (ret) {
443 DRM_ERROR("Failed to initialize cursor plane");
444 drm_plane_cleanup(&ldu->base.primary);
445 goto err_free;
446 }
447
448 drm_plane_helper_add(&cursor->base, &vmw_ldu_cursor_plane_helper_funcs);
449 }
450
451 ret = drm_connector_init(dev, connector, &vmw_legacy_connector_funcs,
452 DRM_MODE_CONNECTOR_VIRTUAL);
453 if (ret) {
454 DRM_ERROR("Failed to initialize connector\n");
455 goto err_free;
456 }
457
458 drm_connector_helper_add(connector, &vmw_ldu_connector_helper_funcs);
459 connector->status = vmw_du_connector_detect(connector, true);
460
461 ret = drm_encoder_init(dev, encoder, &vmw_legacy_encoder_funcs,
462 DRM_MODE_ENCODER_VIRTUAL, NULL);
463 if (ret) {
464 DRM_ERROR("Failed to initialize encoder\n");
465 goto err_free_connector;
466 }
467
468 (void) drm_connector_attach_encoder(connector, encoder);
469 encoder->possible_crtcs = (1 << unit);
470 encoder->possible_clones = 0;
471
472 ret = drm_connector_register(connector);
473 if (ret) {
474 DRM_ERROR("Failed to register connector\n");
475 goto err_free_encoder;
476 }
477
478 ret = drm_crtc_init_with_planes(dev, crtc, primary,
479 vmw_cmd_supported(dev_priv) ? &cursor->base : NULL,
480 &vmw_legacy_crtc_funcs, NULL);
481 if (ret) {
482 DRM_ERROR("Failed to initialize CRTC\n");
483 goto err_free_unregister;
484 }
485
486 drm_crtc_helper_add(crtc, &vmw_ldu_crtc_helper_funcs);
487
488 drm_mode_crtc_set_gamma_size(crtc, 256);
489
490 drm_object_attach_property(&connector->base,
491 dev_priv->hotplug_mode_update_property, 1);
492 drm_object_attach_property(&connector->base,
493 dev->mode_config.suggested_x_property, 0);
494 drm_object_attach_property(&connector->base,
495 dev->mode_config.suggested_y_property, 0);
496 if (dev_priv->implicit_placement_property)
497 drm_object_attach_property
498 (&connector->base,
499 dev_priv->implicit_placement_property,
500 1);
501
502 return 0;
503
504 err_free_unregister:
505 drm_connector_unregister(connector);
506 err_free_encoder:
507 drm_encoder_cleanup(encoder);
508 err_free_connector:
509 drm_connector_cleanup(connector);
510 err_free:
511 kfree(ldu);
512 return ret;
513 }
514
vmw_kms_ldu_init_display(struct vmw_private * dev_priv)515 int vmw_kms_ldu_init_display(struct vmw_private *dev_priv)
516 {
517 struct drm_device *dev = &dev_priv->drm;
518 int i, ret;
519 int num_display_units = (dev_priv->capabilities & SVGA_CAP_MULTIMON) ?
520 VMWGFX_NUM_DISPLAY_UNITS : 1;
521
522 if (unlikely(dev_priv->ldu_priv)) {
523 return -EINVAL;
524 }
525
526 dev_priv->ldu_priv = kmalloc(sizeof(*dev_priv->ldu_priv), GFP_KERNEL);
527 if (!dev_priv->ldu_priv)
528 return -ENOMEM;
529
530 INIT_LIST_HEAD(&dev_priv->ldu_priv->active);
531 dev_priv->ldu_priv->num_active = 0;
532 dev_priv->ldu_priv->last_num_active = 0;
533 dev_priv->ldu_priv->fb = NULL;
534
535 vmw_kms_create_implicit_placement_property(dev_priv);
536
537 for (i = 0; i < num_display_units; ++i) {
538 ret = vmw_ldu_init(dev_priv, i);
539 if (ret != 0)
540 goto err_free;
541 }
542
543 dev_priv->active_display_unit = vmw_du_legacy;
544
545 drm_mode_config_reset(dev);
546
547 return 0;
548
549 err_free:
550 kfree(dev_priv->ldu_priv);
551 dev_priv->ldu_priv = NULL;
552 return ret;
553 }
554
vmw_kms_ldu_close_display(struct vmw_private * dev_priv)555 int vmw_kms_ldu_close_display(struct vmw_private *dev_priv)
556 {
557 if (!dev_priv->ldu_priv)
558 return -ENOSYS;
559
560 BUG_ON(!list_empty(&dev_priv->ldu_priv->active));
561
562 kfree(dev_priv->ldu_priv);
563
564 return 0;
565 }
566
567
vmw_kms_ldu_do_bo_dirty(struct vmw_private * dev_priv,struct vmw_framebuffer * framebuffer,unsigned int flags,unsigned int color,struct drm_mode_rect * clips,unsigned int num_clips)568 static int vmw_kms_ldu_do_bo_dirty(struct vmw_private *dev_priv,
569 struct vmw_framebuffer *framebuffer,
570 unsigned int flags, unsigned int color,
571 struct drm_mode_rect *clips,
572 unsigned int num_clips)
573 {
574 size_t fifo_size;
575 int i;
576
577 struct {
578 uint32_t header;
579 SVGAFifoCmdUpdate body;
580 } *cmd;
581
582 fifo_size = sizeof(*cmd) * num_clips;
583 cmd = VMW_CMD_RESERVE(dev_priv, fifo_size);
584 if (unlikely(cmd == NULL))
585 return -ENOMEM;
586
587 memset(cmd, 0, fifo_size);
588 for (i = 0; i < num_clips; i++, clips++) {
589 cmd[i].header = SVGA_CMD_UPDATE;
590 cmd[i].body.x = clips->x1;
591 cmd[i].body.y = clips->y1;
592 cmd[i].body.width = clips->x2 - clips->x1;
593 cmd[i].body.height = clips->y2 - clips->y1;
594 }
595
596 vmw_cmd_commit(dev_priv, fifo_size);
597 return 0;
598 }
599