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