• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
3  *
4  * Copyright 2009-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 <drm/drm_atomic.h>
29 #include <drm/drm_atomic_helper.h>
30 #include <drm/drm_damage_helper.h>
31 #include <drm/drm_fourcc.h>
32 #include <drm/drm_plane_helper.h>
33 #include <drm/drm_rect.h>
34 #include <drm/drm_sysfs.h>
35 #include <drm/drm_vblank.h>
36 
37 #include "vmwgfx_kms.h"
38 
vmw_du_cleanup(struct vmw_display_unit * du)39 void vmw_du_cleanup(struct vmw_display_unit *du)
40 {
41 	struct vmw_private *dev_priv = vmw_priv(du->primary.dev);
42 	drm_plane_cleanup(&du->primary);
43 	if (vmw_cmd_supported(dev_priv))
44 		drm_plane_cleanup(&du->cursor);
45 
46 	drm_connector_unregister(&du->connector);
47 	drm_crtc_cleanup(&du->crtc);
48 	drm_encoder_cleanup(&du->encoder);
49 	drm_connector_cleanup(&du->connector);
50 }
51 
52 /*
53  * Display Unit Cursor functions
54  */
55 
vmw_cursor_update_image(struct vmw_private * dev_priv,u32 * image,u32 width,u32 height,u32 hotspotX,u32 hotspotY)56 static int vmw_cursor_update_image(struct vmw_private *dev_priv,
57 				   u32 *image, u32 width, u32 height,
58 				   u32 hotspotX, u32 hotspotY)
59 {
60 	struct {
61 		u32 cmd;
62 		SVGAFifoCmdDefineAlphaCursor cursor;
63 	} *cmd;
64 	u32 image_size = width * height * 4;
65 	u32 cmd_size = sizeof(*cmd) + image_size;
66 
67 	if (!image)
68 		return -EINVAL;
69 
70 	cmd = VMW_CMD_RESERVE(dev_priv, cmd_size);
71 	if (unlikely(cmd == NULL))
72 		return -ENOMEM;
73 
74 	memset(cmd, 0, sizeof(*cmd));
75 
76 	memcpy(&cmd[1], image, image_size);
77 
78 	cmd->cmd = SVGA_CMD_DEFINE_ALPHA_CURSOR;
79 	cmd->cursor.id = 0;
80 	cmd->cursor.width = width;
81 	cmd->cursor.height = height;
82 	cmd->cursor.hotspotX = hotspotX;
83 	cmd->cursor.hotspotY = hotspotY;
84 
85 	vmw_cmd_commit_flush(dev_priv, cmd_size);
86 
87 	return 0;
88 }
89 
vmw_cursor_update_bo(struct vmw_private * dev_priv,struct vmw_buffer_object * bo,u32 width,u32 height,u32 hotspotX,u32 hotspotY)90 static int vmw_cursor_update_bo(struct vmw_private *dev_priv,
91 				struct vmw_buffer_object *bo,
92 				u32 width, u32 height,
93 				u32 hotspotX, u32 hotspotY)
94 {
95 	struct ttm_bo_kmap_obj map;
96 	unsigned long kmap_offset;
97 	unsigned long kmap_num;
98 	void *virtual;
99 	bool dummy;
100 	int ret;
101 
102 	kmap_offset = 0;
103 	kmap_num = PFN_UP(width*height*4);
104 
105 	ret = ttm_bo_reserve(&bo->base, true, false, NULL);
106 	if (unlikely(ret != 0)) {
107 		DRM_ERROR("reserve failed\n");
108 		return -EINVAL;
109 	}
110 
111 	ret = ttm_bo_kmap(&bo->base, kmap_offset, kmap_num, &map);
112 	if (unlikely(ret != 0))
113 		goto err_unreserve;
114 
115 	virtual = ttm_kmap_obj_virtual(&map, &dummy);
116 	ret = vmw_cursor_update_image(dev_priv, virtual, width, height,
117 				      hotspotX, hotspotY);
118 
119 	ttm_bo_kunmap(&map);
120 err_unreserve:
121 	ttm_bo_unreserve(&bo->base);
122 
123 	return ret;
124 }
125 
126 
vmw_cursor_update_position(struct vmw_private * dev_priv,bool show,int x,int y)127 static void vmw_cursor_update_position(struct vmw_private *dev_priv,
128 				       bool show, int x, int y)
129 {
130 	uint32_t count;
131 
132 	spin_lock(&dev_priv->cursor_lock);
133 	if (vmw_is_cursor_bypass3_enabled(dev_priv)) {
134 		vmw_fifo_mem_write(dev_priv, SVGA_FIFO_CURSOR_ON, show ? 1 : 0);
135 		vmw_fifo_mem_write(dev_priv, SVGA_FIFO_CURSOR_X, x);
136 		vmw_fifo_mem_write(dev_priv, SVGA_FIFO_CURSOR_Y, y);
137 		count = vmw_fifo_mem_read(dev_priv, SVGA_FIFO_CURSOR_COUNT);
138 		vmw_fifo_mem_write(dev_priv, SVGA_FIFO_CURSOR_COUNT, ++count);
139 	} else {
140 		vmw_write(dev_priv, SVGA_REG_CURSOR_X, x);
141 		vmw_write(dev_priv, SVGA_REG_CURSOR_Y, y);
142 		vmw_write(dev_priv, SVGA_REG_CURSOR_ON, show ? 1 : 0);
143 	}
144 	spin_unlock(&dev_priv->cursor_lock);
145 }
146 
147 
vmw_kms_cursor_snoop(struct vmw_surface * srf,struct ttm_object_file * tfile,struct ttm_buffer_object * bo,SVGA3dCmdHeader * header)148 void vmw_kms_cursor_snoop(struct vmw_surface *srf,
149 			  struct ttm_object_file *tfile,
150 			  struct ttm_buffer_object *bo,
151 			  SVGA3dCmdHeader *header)
152 {
153 	struct ttm_bo_kmap_obj map;
154 	unsigned long kmap_offset;
155 	unsigned long kmap_num;
156 	SVGA3dCopyBox *box;
157 	unsigned box_count;
158 	void *virtual;
159 	bool dummy;
160 	struct vmw_dma_cmd {
161 		SVGA3dCmdHeader header;
162 		SVGA3dCmdSurfaceDMA dma;
163 	} *cmd;
164 	int i, ret;
165 
166 	cmd = container_of(header, struct vmw_dma_cmd, header);
167 
168 	/* No snooper installed */
169 	if (!srf->snooper.image)
170 		return;
171 
172 	if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
173 		DRM_ERROR("face and mipmap for cursors should never != 0\n");
174 		return;
175 	}
176 
177 	if (cmd->header.size < 64) {
178 		DRM_ERROR("at least one full copy box must be given\n");
179 		return;
180 	}
181 
182 	box = (SVGA3dCopyBox *)&cmd[1];
183 	box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
184 			sizeof(SVGA3dCopyBox);
185 
186 	if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
187 	    box->x != 0    || box->y != 0    || box->z != 0    ||
188 	    box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
189 	    box->d != 1    || box_count != 1 ||
190 	    box->w > 64 || box->h > 64) {
191 		/* TODO handle none page aligned offsets */
192 		/* TODO handle more dst & src != 0 */
193 		/* TODO handle more then one copy */
194 		DRM_ERROR("Can't snoop dma request for cursor!\n");
195 		DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
196 			  box->srcx, box->srcy, box->srcz,
197 			  box->x, box->y, box->z,
198 			  box->w, box->h, box->d, box_count,
199 			  cmd->dma.guest.ptr.offset);
200 		return;
201 	}
202 
203 	kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
204 	kmap_num = (64*64*4) >> PAGE_SHIFT;
205 
206 	ret = ttm_bo_reserve(bo, true, false, NULL);
207 	if (unlikely(ret != 0)) {
208 		DRM_ERROR("reserve failed\n");
209 		return;
210 	}
211 
212 	ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
213 	if (unlikely(ret != 0))
214 		goto err_unreserve;
215 
216 	virtual = ttm_kmap_obj_virtual(&map, &dummy);
217 
218 	if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
219 		memcpy(srf->snooper.image, virtual, 64*64*4);
220 	} else {
221 		/* Image is unsigned pointer. */
222 		for (i = 0; i < box->h; i++)
223 			memcpy(srf->snooper.image + i * 64,
224 			       virtual + i * cmd->dma.guest.pitch,
225 			       box->w * 4);
226 	}
227 
228 	srf->snooper.age++;
229 
230 	ttm_bo_kunmap(&map);
231 err_unreserve:
232 	ttm_bo_unreserve(bo);
233 }
234 
235 /**
236  * vmw_kms_legacy_hotspot_clear - Clear legacy hotspots
237  *
238  * @dev_priv: Pointer to the device private struct.
239  *
240  * Clears all legacy hotspots.
241  */
vmw_kms_legacy_hotspot_clear(struct vmw_private * dev_priv)242 void vmw_kms_legacy_hotspot_clear(struct vmw_private *dev_priv)
243 {
244 	struct drm_device *dev = &dev_priv->drm;
245 	struct vmw_display_unit *du;
246 	struct drm_crtc *crtc;
247 
248 	drm_modeset_lock_all(dev);
249 	drm_for_each_crtc(crtc, dev) {
250 		du = vmw_crtc_to_du(crtc);
251 
252 		du->hotspot_x = 0;
253 		du->hotspot_y = 0;
254 	}
255 	drm_modeset_unlock_all(dev);
256 }
257 
vmw_kms_cursor_post_execbuf(struct vmw_private * dev_priv)258 void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
259 {
260 	struct drm_device *dev = &dev_priv->drm;
261 	struct vmw_display_unit *du;
262 	struct drm_crtc *crtc;
263 
264 	mutex_lock(&dev->mode_config.mutex);
265 
266 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
267 		du = vmw_crtc_to_du(crtc);
268 		if (!du->cursor_surface ||
269 		    du->cursor_age == du->cursor_surface->snooper.age)
270 			continue;
271 
272 		du->cursor_age = du->cursor_surface->snooper.age;
273 		vmw_cursor_update_image(dev_priv,
274 					du->cursor_surface->snooper.image,
275 					64, 64,
276 					du->hotspot_x + du->core_hotspot_x,
277 					du->hotspot_y + du->core_hotspot_y);
278 	}
279 
280 	mutex_unlock(&dev->mode_config.mutex);
281 }
282 
283 
vmw_du_cursor_plane_destroy(struct drm_plane * plane)284 void vmw_du_cursor_plane_destroy(struct drm_plane *plane)
285 {
286 	vmw_cursor_update_position(plane->dev->dev_private, false, 0, 0);
287 
288 	drm_plane_cleanup(plane);
289 }
290 
291 
vmw_du_primary_plane_destroy(struct drm_plane * plane)292 void vmw_du_primary_plane_destroy(struct drm_plane *plane)
293 {
294 	drm_plane_cleanup(plane);
295 
296 	/* Planes are static in our case so we don't free it */
297 }
298 
299 
300 /**
301  * vmw_du_plane_unpin_surf - unpins resource associated with a framebuffer surface
302  *
303  * @vps: plane state associated with the display surface
304  * @unreference: true if we also want to unreference the display.
305  */
vmw_du_plane_unpin_surf(struct vmw_plane_state * vps,bool unreference)306 void vmw_du_plane_unpin_surf(struct vmw_plane_state *vps,
307 			     bool unreference)
308 {
309 	if (vps->surf) {
310 		if (vps->pinned) {
311 			vmw_resource_unpin(&vps->surf->res);
312 			vps->pinned--;
313 		}
314 
315 		if (unreference) {
316 			if (vps->pinned)
317 				DRM_ERROR("Surface still pinned\n");
318 			vmw_surface_unreference(&vps->surf);
319 		}
320 	}
321 }
322 
323 
324 /**
325  * vmw_du_plane_cleanup_fb - Unpins the cursor
326  *
327  * @plane:  display plane
328  * @old_state: Contains the FB to clean up
329  *
330  * Unpins the framebuffer surface
331  *
332  * Returns 0 on success
333  */
334 void
vmw_du_plane_cleanup_fb(struct drm_plane * plane,struct drm_plane_state * old_state)335 vmw_du_plane_cleanup_fb(struct drm_plane *plane,
336 			struct drm_plane_state *old_state)
337 {
338 	struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
339 
340 	vmw_du_plane_unpin_surf(vps, false);
341 }
342 
343 
344 /**
345  * vmw_du_cursor_plane_prepare_fb - Readies the cursor by referencing it
346  *
347  * @plane:  display plane
348  * @new_state: info on the new plane state, including the FB
349  *
350  * Returns 0 on success
351  */
352 int
vmw_du_cursor_plane_prepare_fb(struct drm_plane * plane,struct drm_plane_state * new_state)353 vmw_du_cursor_plane_prepare_fb(struct drm_plane *plane,
354 			       struct drm_plane_state *new_state)
355 {
356 	struct drm_framebuffer *fb = new_state->fb;
357 	struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
358 
359 
360 	if (vps->surf)
361 		vmw_surface_unreference(&vps->surf);
362 
363 	if (vps->bo)
364 		vmw_bo_unreference(&vps->bo);
365 
366 	if (fb) {
367 		if (vmw_framebuffer_to_vfb(fb)->bo) {
368 			vps->bo = vmw_framebuffer_to_vfbd(fb)->buffer;
369 			vmw_bo_reference(vps->bo);
370 		} else {
371 			vps->surf = vmw_framebuffer_to_vfbs(fb)->surface;
372 			vmw_surface_reference(vps->surf);
373 		}
374 	}
375 
376 	return 0;
377 }
378 
379 
380 void
vmw_du_cursor_plane_atomic_update(struct drm_plane * plane,struct drm_atomic_state * state)381 vmw_du_cursor_plane_atomic_update(struct drm_plane *plane,
382 				  struct drm_atomic_state *state)
383 {
384 	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
385 									   plane);
386 	struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
387 									   plane);
388 	struct drm_crtc *crtc = new_state->crtc ?: old_state->crtc;
389 	struct vmw_private *dev_priv = vmw_priv(crtc->dev);
390 	struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
391 	struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
392 	s32 hotspot_x, hotspot_y;
393 	int ret = 0;
394 
395 
396 	hotspot_x = du->hotspot_x;
397 	hotspot_y = du->hotspot_y;
398 
399 	if (new_state->fb) {
400 		hotspot_x += new_state->fb->hot_x;
401 		hotspot_y += new_state->fb->hot_y;
402 	}
403 
404 	du->cursor_surface = vps->surf;
405 	du->cursor_bo = vps->bo;
406 
407 	if (vps->surf) {
408 		du->cursor_age = du->cursor_surface->snooper.age;
409 
410 		ret = vmw_cursor_update_image(dev_priv,
411 					      vps->surf->snooper.image,
412 					      64, 64, hotspot_x,
413 					      hotspot_y);
414 	} else if (vps->bo) {
415 		ret = vmw_cursor_update_bo(dev_priv, vps->bo,
416 					   new_state->crtc_w,
417 					   new_state->crtc_h,
418 					   hotspot_x, hotspot_y);
419 	} else {
420 		vmw_cursor_update_position(dev_priv, false, 0, 0);
421 		return;
422 	}
423 
424 	if (!ret) {
425 		du->cursor_x = new_state->crtc_x + du->set_gui_x;
426 		du->cursor_y = new_state->crtc_y + du->set_gui_y;
427 
428 		vmw_cursor_update_position(dev_priv, true,
429 					   du->cursor_x + hotspot_x,
430 					   du->cursor_y + hotspot_y);
431 
432 		du->core_hotspot_x = hotspot_x - du->hotspot_x;
433 		du->core_hotspot_y = hotspot_y - du->hotspot_y;
434 	} else {
435 		DRM_ERROR("Failed to update cursor image\n");
436 	}
437 }
438 
439 
440 /**
441  * vmw_du_primary_plane_atomic_check - check if the new state is okay
442  *
443  * @plane: display plane
444  * @state: info on the new plane state, including the FB
445  *
446  * Check if the new state is settable given the current state.  Other
447  * than what the atomic helper checks, we care about crtc fitting
448  * the FB and maintaining one active framebuffer.
449  *
450  * Returns 0 on success
451  */
vmw_du_primary_plane_atomic_check(struct drm_plane * plane,struct drm_atomic_state * state)452 int vmw_du_primary_plane_atomic_check(struct drm_plane *plane,
453 				      struct drm_atomic_state *state)
454 {
455 	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
456 									   plane);
457 	struct drm_crtc_state *crtc_state = NULL;
458 	struct drm_framebuffer *new_fb = new_state->fb;
459 	int ret;
460 
461 	if (new_state->crtc)
462 		crtc_state = drm_atomic_get_new_crtc_state(state,
463 							   new_state->crtc);
464 
465 	ret = drm_atomic_helper_check_plane_state(new_state, crtc_state,
466 						  DRM_PLANE_HELPER_NO_SCALING,
467 						  DRM_PLANE_HELPER_NO_SCALING,
468 						  false, true);
469 
470 	if (!ret && new_fb) {
471 		struct drm_crtc *crtc = new_state->crtc;
472 		struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
473 
474 		vmw_connector_state_to_vcs(du->connector.state);
475 	}
476 
477 
478 	return ret;
479 }
480 
481 
482 /**
483  * vmw_du_cursor_plane_atomic_check - check if the new state is okay
484  *
485  * @plane: cursor plane
486  * @state: info on the new plane state
487  *
488  * This is a chance to fail if the new cursor state does not fit
489  * our requirements.
490  *
491  * Returns 0 on success
492  */
vmw_du_cursor_plane_atomic_check(struct drm_plane * plane,struct drm_atomic_state * state)493 int vmw_du_cursor_plane_atomic_check(struct drm_plane *plane,
494 				     struct drm_atomic_state *state)
495 {
496 	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
497 									   plane);
498 	int ret = 0;
499 	struct drm_crtc_state *crtc_state = NULL;
500 	struct vmw_surface *surface = NULL;
501 	struct drm_framebuffer *fb = new_state->fb;
502 
503 	if (new_state->crtc)
504 		crtc_state = drm_atomic_get_new_crtc_state(new_state->state,
505 							   new_state->crtc);
506 
507 	ret = drm_atomic_helper_check_plane_state(new_state, crtc_state,
508 						  DRM_PLANE_HELPER_NO_SCALING,
509 						  DRM_PLANE_HELPER_NO_SCALING,
510 						  true, true);
511 	if (ret)
512 		return ret;
513 
514 	/* Turning off */
515 	if (!fb)
516 		return 0;
517 
518 	/* A lot of the code assumes this */
519 	if (new_state->crtc_w != 64 || new_state->crtc_h != 64) {
520 		DRM_ERROR("Invalid cursor dimensions (%d, %d)\n",
521 			  new_state->crtc_w, new_state->crtc_h);
522 		ret = -EINVAL;
523 	}
524 
525 	if (!vmw_framebuffer_to_vfb(fb)->bo)
526 		surface = vmw_framebuffer_to_vfbs(fb)->surface;
527 
528 	if (surface && !surface->snooper.image) {
529 		DRM_ERROR("surface not suitable for cursor\n");
530 		ret = -EINVAL;
531 	}
532 
533 	return ret;
534 }
535 
536 
vmw_du_crtc_atomic_check(struct drm_crtc * crtc,struct drm_atomic_state * state)537 int vmw_du_crtc_atomic_check(struct drm_crtc *crtc,
538 			     struct drm_atomic_state *state)
539 {
540 	struct drm_crtc_state *new_state = drm_atomic_get_new_crtc_state(state,
541 									 crtc);
542 	struct vmw_display_unit *du = vmw_crtc_to_du(new_state->crtc);
543 	int connector_mask = drm_connector_mask(&du->connector);
544 	bool has_primary = new_state->plane_mask &
545 			   drm_plane_mask(crtc->primary);
546 
547 	/* We always want to have an active plane with an active CRTC */
548 	if (has_primary != new_state->enable)
549 		return -EINVAL;
550 
551 
552 	if (new_state->connector_mask != connector_mask &&
553 	    new_state->connector_mask != 0) {
554 		DRM_ERROR("Invalid connectors configuration\n");
555 		return -EINVAL;
556 	}
557 
558 	/*
559 	 * Our virtual device does not have a dot clock, so use the logical
560 	 * clock value as the dot clock.
561 	 */
562 	if (new_state->mode.crtc_clock == 0)
563 		new_state->adjusted_mode.crtc_clock = new_state->mode.clock;
564 
565 	return 0;
566 }
567 
568 
vmw_du_crtc_atomic_begin(struct drm_crtc * crtc,struct drm_atomic_state * state)569 void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc,
570 			      struct drm_atomic_state *state)
571 {
572 }
573 
574 
vmw_du_crtc_atomic_flush(struct drm_crtc * crtc,struct drm_atomic_state * state)575 void vmw_du_crtc_atomic_flush(struct drm_crtc *crtc,
576 			      struct drm_atomic_state *state)
577 {
578 	struct drm_pending_vblank_event *event = crtc->state->event;
579 
580 	if (event) {
581 		crtc->state->event = NULL;
582 
583 		spin_lock_irq(&crtc->dev->event_lock);
584 		drm_crtc_send_vblank_event(crtc, event);
585 		spin_unlock_irq(&crtc->dev->event_lock);
586 	}
587 }
588 
589 
590 /**
591  * vmw_du_crtc_duplicate_state - duplicate crtc state
592  * @crtc: DRM crtc
593  *
594  * Allocates and returns a copy of the crtc state (both common and
595  * vmw-specific) for the specified crtc.
596  *
597  * Returns: The newly allocated crtc state, or NULL on failure.
598  */
599 struct drm_crtc_state *
vmw_du_crtc_duplicate_state(struct drm_crtc * crtc)600 vmw_du_crtc_duplicate_state(struct drm_crtc *crtc)
601 {
602 	struct drm_crtc_state *state;
603 	struct vmw_crtc_state *vcs;
604 
605 	if (WARN_ON(!crtc->state))
606 		return NULL;
607 
608 	vcs = kmemdup(crtc->state, sizeof(*vcs), GFP_KERNEL);
609 
610 	if (!vcs)
611 		return NULL;
612 
613 	state = &vcs->base;
614 
615 	__drm_atomic_helper_crtc_duplicate_state(crtc, state);
616 
617 	return state;
618 }
619 
620 
621 /**
622  * vmw_du_crtc_reset - creates a blank vmw crtc state
623  * @crtc: DRM crtc
624  *
625  * Resets the atomic state for @crtc by freeing the state pointer (which
626  * might be NULL, e.g. at driver load time) and allocating a new empty state
627  * object.
628  */
vmw_du_crtc_reset(struct drm_crtc * crtc)629 void vmw_du_crtc_reset(struct drm_crtc *crtc)
630 {
631 	struct vmw_crtc_state *vcs;
632 
633 
634 	if (crtc->state) {
635 		__drm_atomic_helper_crtc_destroy_state(crtc->state);
636 
637 		kfree(vmw_crtc_state_to_vcs(crtc->state));
638 	}
639 
640 	vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
641 
642 	if (!vcs) {
643 		DRM_ERROR("Cannot allocate vmw_crtc_state\n");
644 		return;
645 	}
646 
647 	__drm_atomic_helper_crtc_reset(crtc, &vcs->base);
648 }
649 
650 
651 /**
652  * vmw_du_crtc_destroy_state - destroy crtc state
653  * @crtc: DRM crtc
654  * @state: state object to destroy
655  *
656  * Destroys the crtc state (both common and vmw-specific) for the
657  * specified plane.
658  */
659 void
vmw_du_crtc_destroy_state(struct drm_crtc * crtc,struct drm_crtc_state * state)660 vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
661 			  struct drm_crtc_state *state)
662 {
663 	drm_atomic_helper_crtc_destroy_state(crtc, state);
664 }
665 
666 
667 /**
668  * vmw_du_plane_duplicate_state - duplicate plane state
669  * @plane: drm plane
670  *
671  * Allocates and returns a copy of the plane state (both common and
672  * vmw-specific) for the specified plane.
673  *
674  * Returns: The newly allocated plane state, or NULL on failure.
675  */
676 struct drm_plane_state *
vmw_du_plane_duplicate_state(struct drm_plane * plane)677 vmw_du_plane_duplicate_state(struct drm_plane *plane)
678 {
679 	struct drm_plane_state *state;
680 	struct vmw_plane_state *vps;
681 
682 	vps = kmemdup(plane->state, sizeof(*vps), GFP_KERNEL);
683 
684 	if (!vps)
685 		return NULL;
686 
687 	vps->pinned = 0;
688 	vps->cpp = 0;
689 
690 	/* Each ref counted resource needs to be acquired again */
691 	if (vps->surf)
692 		(void) vmw_surface_reference(vps->surf);
693 
694 	if (vps->bo)
695 		(void) vmw_bo_reference(vps->bo);
696 
697 	state = &vps->base;
698 
699 	__drm_atomic_helper_plane_duplicate_state(plane, state);
700 
701 	return state;
702 }
703 
704 
705 /**
706  * vmw_du_plane_reset - creates a blank vmw plane state
707  * @plane: drm plane
708  *
709  * Resets the atomic state for @plane by freeing the state pointer (which might
710  * be NULL, e.g. at driver load time) and allocating a new empty state object.
711  */
vmw_du_plane_reset(struct drm_plane * plane)712 void vmw_du_plane_reset(struct drm_plane *plane)
713 {
714 	struct vmw_plane_state *vps;
715 
716 
717 	if (plane->state)
718 		vmw_du_plane_destroy_state(plane, plane->state);
719 
720 	vps = kzalloc(sizeof(*vps), GFP_KERNEL);
721 
722 	if (!vps) {
723 		DRM_ERROR("Cannot allocate vmw_plane_state\n");
724 		return;
725 	}
726 
727 	__drm_atomic_helper_plane_reset(plane, &vps->base);
728 }
729 
730 
731 /**
732  * vmw_du_plane_destroy_state - destroy plane state
733  * @plane: DRM plane
734  * @state: state object to destroy
735  *
736  * Destroys the plane state (both common and vmw-specific) for the
737  * specified plane.
738  */
739 void
vmw_du_plane_destroy_state(struct drm_plane * plane,struct drm_plane_state * state)740 vmw_du_plane_destroy_state(struct drm_plane *plane,
741 			   struct drm_plane_state *state)
742 {
743 	struct vmw_plane_state *vps = vmw_plane_state_to_vps(state);
744 
745 
746 	/* Should have been freed by cleanup_fb */
747 	if (vps->surf)
748 		vmw_surface_unreference(&vps->surf);
749 
750 	if (vps->bo)
751 		vmw_bo_unreference(&vps->bo);
752 
753 	drm_atomic_helper_plane_destroy_state(plane, state);
754 }
755 
756 
757 /**
758  * vmw_du_connector_duplicate_state - duplicate connector state
759  * @connector: DRM connector
760  *
761  * Allocates and returns a copy of the connector state (both common and
762  * vmw-specific) for the specified connector.
763  *
764  * Returns: The newly allocated connector state, or NULL on failure.
765  */
766 struct drm_connector_state *
vmw_du_connector_duplicate_state(struct drm_connector * connector)767 vmw_du_connector_duplicate_state(struct drm_connector *connector)
768 {
769 	struct drm_connector_state *state;
770 	struct vmw_connector_state *vcs;
771 
772 	if (WARN_ON(!connector->state))
773 		return NULL;
774 
775 	vcs = kmemdup(connector->state, sizeof(*vcs), GFP_KERNEL);
776 
777 	if (!vcs)
778 		return NULL;
779 
780 	state = &vcs->base;
781 
782 	__drm_atomic_helper_connector_duplicate_state(connector, state);
783 
784 	return state;
785 }
786 
787 
788 /**
789  * vmw_du_connector_reset - creates a blank vmw connector state
790  * @connector: DRM connector
791  *
792  * Resets the atomic state for @connector by freeing the state pointer (which
793  * might be NULL, e.g. at driver load time) and allocating a new empty state
794  * object.
795  */
vmw_du_connector_reset(struct drm_connector * connector)796 void vmw_du_connector_reset(struct drm_connector *connector)
797 {
798 	struct vmw_connector_state *vcs;
799 
800 
801 	if (connector->state) {
802 		__drm_atomic_helper_connector_destroy_state(connector->state);
803 
804 		kfree(vmw_connector_state_to_vcs(connector->state));
805 	}
806 
807 	vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
808 
809 	if (!vcs) {
810 		DRM_ERROR("Cannot allocate vmw_connector_state\n");
811 		return;
812 	}
813 
814 	__drm_atomic_helper_connector_reset(connector, &vcs->base);
815 }
816 
817 
818 /**
819  * vmw_du_connector_destroy_state - destroy connector state
820  * @connector: DRM connector
821  * @state: state object to destroy
822  *
823  * Destroys the connector state (both common and vmw-specific) for the
824  * specified plane.
825  */
826 void
vmw_du_connector_destroy_state(struct drm_connector * connector,struct drm_connector_state * state)827 vmw_du_connector_destroy_state(struct drm_connector *connector,
828 			  struct drm_connector_state *state)
829 {
830 	drm_atomic_helper_connector_destroy_state(connector, state);
831 }
832 /*
833  * Generic framebuffer code
834  */
835 
836 /*
837  * Surface framebuffer code
838  */
839 
vmw_framebuffer_surface_destroy(struct drm_framebuffer * framebuffer)840 static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
841 {
842 	struct vmw_framebuffer_surface *vfbs =
843 		vmw_framebuffer_to_vfbs(framebuffer);
844 
845 	drm_framebuffer_cleanup(framebuffer);
846 	vmw_surface_unreference(&vfbs->surface);
847 	if (vfbs->base.user_obj)
848 		ttm_base_object_unref(&vfbs->base.user_obj);
849 
850 	kfree(vfbs);
851 }
852 
853 /**
854  * vmw_kms_readback - Perform a readback from the screen system to
855  * a buffer-object backed framebuffer.
856  *
857  * @dev_priv: Pointer to the device private structure.
858  * @file_priv: Pointer to a struct drm_file identifying the caller.
859  * Must be set to NULL if @user_fence_rep is NULL.
860  * @vfb: Pointer to the buffer-object backed framebuffer.
861  * @user_fence_rep: User-space provided structure for fence information.
862  * Must be set to non-NULL if @file_priv is non-NULL.
863  * @vclips: Array of clip rects.
864  * @num_clips: Number of clip rects in @vclips.
865  *
866  * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
867  * interrupted.
868  */
vmw_kms_readback(struct vmw_private * dev_priv,struct drm_file * file_priv,struct vmw_framebuffer * vfb,struct drm_vmw_fence_rep __user * user_fence_rep,struct drm_vmw_rect * vclips,uint32_t num_clips)869 int vmw_kms_readback(struct vmw_private *dev_priv,
870 		     struct drm_file *file_priv,
871 		     struct vmw_framebuffer *vfb,
872 		     struct drm_vmw_fence_rep __user *user_fence_rep,
873 		     struct drm_vmw_rect *vclips,
874 		     uint32_t num_clips)
875 {
876 	switch (dev_priv->active_display_unit) {
877 	case vmw_du_screen_object:
878 		return vmw_kms_sou_readback(dev_priv, file_priv, vfb,
879 					    user_fence_rep, vclips, num_clips,
880 					    NULL);
881 	case vmw_du_screen_target:
882 		return vmw_kms_stdu_dma(dev_priv, file_priv, vfb,
883 					user_fence_rep, NULL, vclips, num_clips,
884 					1, false, true, NULL);
885 	default:
886 		WARN_ONCE(true,
887 			  "Readback called with invalid display system.\n");
888 }
889 
890 	return -ENOSYS;
891 }
892 
893 
894 static const struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
895 	.destroy = vmw_framebuffer_surface_destroy,
896 	.dirty = drm_atomic_helper_dirtyfb,
897 };
898 
vmw_kms_new_framebuffer_surface(struct vmw_private * dev_priv,struct vmw_surface * surface,struct vmw_framebuffer ** out,const struct drm_mode_fb_cmd2 * mode_cmd,bool is_bo_proxy)899 static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
900 					   struct vmw_surface *surface,
901 					   struct vmw_framebuffer **out,
902 					   const struct drm_mode_fb_cmd2
903 					   *mode_cmd,
904 					   bool is_bo_proxy)
905 
906 {
907 	struct drm_device *dev = &dev_priv->drm;
908 	struct vmw_framebuffer_surface *vfbs;
909 	enum SVGA3dSurfaceFormat format;
910 	int ret;
911 
912 	/* 3D is only supported on HWv8 and newer hosts */
913 	if (dev_priv->active_display_unit == vmw_du_legacy)
914 		return -ENOSYS;
915 
916 	/*
917 	 * Sanity checks.
918 	 */
919 
920 	if (!drm_any_plane_has_format(&dev_priv->drm,
921 				      mode_cmd->pixel_format,
922 				      mode_cmd->modifier[0])) {
923 		drm_dbg(&dev_priv->drm,
924 			"unsupported pixel format %p4cc / modifier 0x%llx\n",
925 			&mode_cmd->pixel_format, mode_cmd->modifier[0]);
926 		return -EINVAL;
927 	}
928 
929 	/* Surface must be marked as a scanout. */
930 	if (unlikely(!surface->metadata.scanout))
931 		return -EINVAL;
932 
933 	if (unlikely(surface->metadata.mip_levels[0] != 1 ||
934 		     surface->metadata.num_sizes != 1 ||
935 		     surface->metadata.base_size.width < mode_cmd->width ||
936 		     surface->metadata.base_size.height < mode_cmd->height ||
937 		     surface->metadata.base_size.depth != 1)) {
938 		DRM_ERROR("Incompatible surface dimensions "
939 			  "for requested mode.\n");
940 		return -EINVAL;
941 	}
942 
943 	switch (mode_cmd->pixel_format) {
944 	case DRM_FORMAT_ARGB8888:
945 		format = SVGA3D_A8R8G8B8;
946 		break;
947 	case DRM_FORMAT_XRGB8888:
948 		format = SVGA3D_X8R8G8B8;
949 		break;
950 	case DRM_FORMAT_RGB565:
951 		format = SVGA3D_R5G6B5;
952 		break;
953 	case DRM_FORMAT_XRGB1555:
954 		format = SVGA3D_A1R5G5B5;
955 		break;
956 	default:
957 		DRM_ERROR("Invalid pixel format: %p4cc\n",
958 			  &mode_cmd->pixel_format);
959 		return -EINVAL;
960 	}
961 
962 	/*
963 	 * For DX, surface format validation is done when surface->scanout
964 	 * is set.
965 	 */
966 	if (!has_sm4_context(dev_priv) && format != surface->metadata.format) {
967 		DRM_ERROR("Invalid surface format for requested mode.\n");
968 		return -EINVAL;
969 	}
970 
971 	vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
972 	if (!vfbs) {
973 		ret = -ENOMEM;
974 		goto out_err1;
975 	}
976 
977 	drm_helper_mode_fill_fb_struct(dev, &vfbs->base.base, mode_cmd);
978 	vfbs->surface = vmw_surface_reference(surface);
979 	vfbs->base.user_handle = mode_cmd->handles[0];
980 	vfbs->is_bo_proxy = is_bo_proxy;
981 
982 	*out = &vfbs->base;
983 
984 	ret = drm_framebuffer_init(dev, &vfbs->base.base,
985 				   &vmw_framebuffer_surface_funcs);
986 	if (ret)
987 		goto out_err2;
988 
989 	return 0;
990 
991 out_err2:
992 	vmw_surface_unreference(&surface);
993 	kfree(vfbs);
994 out_err1:
995 	return ret;
996 }
997 
998 /*
999  * Buffer-object framebuffer code
1000  */
1001 
vmw_framebuffer_bo_destroy(struct drm_framebuffer * framebuffer)1002 static void vmw_framebuffer_bo_destroy(struct drm_framebuffer *framebuffer)
1003 {
1004 	struct vmw_framebuffer_bo *vfbd =
1005 		vmw_framebuffer_to_vfbd(framebuffer);
1006 
1007 	drm_framebuffer_cleanup(framebuffer);
1008 	vmw_bo_unreference(&vfbd->buffer);
1009 	if (vfbd->base.user_obj)
1010 		ttm_base_object_unref(&vfbd->base.user_obj);
1011 
1012 	kfree(vfbd);
1013 }
1014 
vmw_framebuffer_bo_dirty(struct drm_framebuffer * framebuffer,struct drm_file * file_priv,unsigned int flags,unsigned int color,struct drm_clip_rect * clips,unsigned int num_clips)1015 static int vmw_framebuffer_bo_dirty(struct drm_framebuffer *framebuffer,
1016 				    struct drm_file *file_priv,
1017 				    unsigned int flags, unsigned int color,
1018 				    struct drm_clip_rect *clips,
1019 				    unsigned int num_clips)
1020 {
1021 	struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
1022 	struct vmw_framebuffer_bo *vfbd =
1023 		vmw_framebuffer_to_vfbd(framebuffer);
1024 	struct drm_clip_rect norect;
1025 	int ret, increment = 1;
1026 
1027 	drm_modeset_lock_all(&dev_priv->drm);
1028 
1029 	if (!num_clips) {
1030 		num_clips = 1;
1031 		clips = &norect;
1032 		norect.x1 = norect.y1 = 0;
1033 		norect.x2 = framebuffer->width;
1034 		norect.y2 = framebuffer->height;
1035 	} else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
1036 		num_clips /= 2;
1037 		increment = 2;
1038 	}
1039 
1040 	switch (dev_priv->active_display_unit) {
1041 	case vmw_du_legacy:
1042 		ret = vmw_kms_ldu_do_bo_dirty(dev_priv, &vfbd->base, 0, 0,
1043 					      clips, num_clips, increment);
1044 		break;
1045 	default:
1046 		ret = -EINVAL;
1047 		WARN_ONCE(true, "Dirty called with invalid display system.\n");
1048 		break;
1049 	}
1050 
1051 	vmw_cmd_flush(dev_priv, false);
1052 
1053 	drm_modeset_unlock_all(&dev_priv->drm);
1054 
1055 	return ret;
1056 }
1057 
vmw_framebuffer_bo_dirty_ext(struct drm_framebuffer * framebuffer,struct drm_file * file_priv,unsigned int flags,unsigned int color,struct drm_clip_rect * clips,unsigned int num_clips)1058 static int vmw_framebuffer_bo_dirty_ext(struct drm_framebuffer *framebuffer,
1059 					struct drm_file *file_priv,
1060 					unsigned int flags, unsigned int color,
1061 					struct drm_clip_rect *clips,
1062 					unsigned int num_clips)
1063 {
1064 	struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
1065 
1066 	if (dev_priv->active_display_unit == vmw_du_legacy &&
1067 	    vmw_cmd_supported(dev_priv))
1068 		return vmw_framebuffer_bo_dirty(framebuffer, file_priv, flags,
1069 						color, clips, num_clips);
1070 
1071 	return drm_atomic_helper_dirtyfb(framebuffer, file_priv, flags, color,
1072 					 clips, num_clips);
1073 }
1074 
1075 static const struct drm_framebuffer_funcs vmw_framebuffer_bo_funcs = {
1076 	.destroy = vmw_framebuffer_bo_destroy,
1077 	.dirty = vmw_framebuffer_bo_dirty_ext,
1078 };
1079 
1080 /*
1081  * Pin the bofer in a location suitable for access by the
1082  * display system.
1083  */
vmw_framebuffer_pin(struct vmw_framebuffer * vfb)1084 static int vmw_framebuffer_pin(struct vmw_framebuffer *vfb)
1085 {
1086 	struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
1087 	struct vmw_buffer_object *buf;
1088 	struct ttm_placement *placement;
1089 	int ret;
1090 
1091 	buf = vfb->bo ?  vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
1092 		vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
1093 
1094 	if (!buf)
1095 		return 0;
1096 
1097 	switch (dev_priv->active_display_unit) {
1098 	case vmw_du_legacy:
1099 		vmw_overlay_pause_all(dev_priv);
1100 		ret = vmw_bo_pin_in_start_of_vram(dev_priv, buf, false);
1101 		vmw_overlay_resume_all(dev_priv);
1102 		break;
1103 	case vmw_du_screen_object:
1104 	case vmw_du_screen_target:
1105 		if (vfb->bo) {
1106 			if (dev_priv->capabilities & SVGA_CAP_3D) {
1107 				/*
1108 				 * Use surface DMA to get content to
1109 				 * sreen target surface.
1110 				 */
1111 				placement = &vmw_vram_gmr_placement;
1112 			} else {
1113 				/* Use CPU blit. */
1114 				placement = &vmw_sys_placement;
1115 			}
1116 		} else {
1117 			/* Use surface / image update */
1118 			placement = &vmw_mob_placement;
1119 		}
1120 
1121 		return vmw_bo_pin_in_placement(dev_priv, buf, placement, false);
1122 	default:
1123 		return -EINVAL;
1124 	}
1125 
1126 	return ret;
1127 }
1128 
vmw_framebuffer_unpin(struct vmw_framebuffer * vfb)1129 static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb)
1130 {
1131 	struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
1132 	struct vmw_buffer_object *buf;
1133 
1134 	buf = vfb->bo ?  vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
1135 		vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
1136 
1137 	if (WARN_ON(!buf))
1138 		return 0;
1139 
1140 	return vmw_bo_unpin(dev_priv, buf, false);
1141 }
1142 
1143 /**
1144  * vmw_create_bo_proxy - create a proxy surface for the buffer object
1145  *
1146  * @dev: DRM device
1147  * @mode_cmd: parameters for the new surface
1148  * @bo_mob: MOB backing the buffer object
1149  * @srf_out: newly created surface
1150  *
1151  * When the content FB is a buffer object, we create a surface as a proxy to the
1152  * same buffer.  This way we can do a surface copy rather than a surface DMA.
1153  * This is a more efficient approach
1154  *
1155  * RETURNS:
1156  * 0 on success, error code otherwise
1157  */
vmw_create_bo_proxy(struct drm_device * dev,const struct drm_mode_fb_cmd2 * mode_cmd,struct vmw_buffer_object * bo_mob,struct vmw_surface ** srf_out)1158 static int vmw_create_bo_proxy(struct drm_device *dev,
1159 			       const struct drm_mode_fb_cmd2 *mode_cmd,
1160 			       struct vmw_buffer_object *bo_mob,
1161 			       struct vmw_surface **srf_out)
1162 {
1163 	struct vmw_surface_metadata metadata = {0};
1164 	uint32_t format;
1165 	struct vmw_resource *res;
1166 	unsigned int bytes_pp;
1167 	int ret;
1168 
1169 	switch (mode_cmd->pixel_format) {
1170 	case DRM_FORMAT_ARGB8888:
1171 	case DRM_FORMAT_XRGB8888:
1172 		format = SVGA3D_X8R8G8B8;
1173 		bytes_pp = 4;
1174 		break;
1175 
1176 	case DRM_FORMAT_RGB565:
1177 	case DRM_FORMAT_XRGB1555:
1178 		format = SVGA3D_R5G6B5;
1179 		bytes_pp = 2;
1180 		break;
1181 
1182 	case 8:
1183 		format = SVGA3D_P8;
1184 		bytes_pp = 1;
1185 		break;
1186 
1187 	default:
1188 		DRM_ERROR("Invalid framebuffer format %p4cc\n",
1189 			  &mode_cmd->pixel_format);
1190 		return -EINVAL;
1191 	}
1192 
1193 	metadata.format = format;
1194 	metadata.mip_levels[0] = 1;
1195 	metadata.num_sizes = 1;
1196 	metadata.base_size.width = mode_cmd->pitches[0] / bytes_pp;
1197 	metadata.base_size.height =  mode_cmd->height;
1198 	metadata.base_size.depth = 1;
1199 	metadata.scanout = true;
1200 
1201 	ret = vmw_gb_surface_define(vmw_priv(dev), 0, &metadata, srf_out);
1202 	if (ret) {
1203 		DRM_ERROR("Failed to allocate proxy content buffer\n");
1204 		return ret;
1205 	}
1206 
1207 	res = &(*srf_out)->res;
1208 
1209 	/* Reserve and switch the backing mob. */
1210 	mutex_lock(&res->dev_priv->cmdbuf_mutex);
1211 	(void) vmw_resource_reserve(res, false, true);
1212 	vmw_bo_unreference(&res->backup);
1213 	res->backup = vmw_bo_reference(bo_mob);
1214 	res->backup_offset = 0;
1215 	vmw_resource_unreserve(res, false, false, false, NULL, 0);
1216 	mutex_unlock(&res->dev_priv->cmdbuf_mutex);
1217 
1218 	return 0;
1219 }
1220 
1221 
1222 
vmw_kms_new_framebuffer_bo(struct vmw_private * dev_priv,struct vmw_buffer_object * bo,struct vmw_framebuffer ** out,const struct drm_mode_fb_cmd2 * mode_cmd)1223 static int vmw_kms_new_framebuffer_bo(struct vmw_private *dev_priv,
1224 				      struct vmw_buffer_object *bo,
1225 				      struct vmw_framebuffer **out,
1226 				      const struct drm_mode_fb_cmd2
1227 				      *mode_cmd)
1228 
1229 {
1230 	struct drm_device *dev = &dev_priv->drm;
1231 	struct vmw_framebuffer_bo *vfbd;
1232 	unsigned int requested_size;
1233 	int ret;
1234 
1235 	requested_size = mode_cmd->height * mode_cmd->pitches[0];
1236 	if (unlikely(requested_size > bo->base.base.size)) {
1237 		DRM_ERROR("Screen buffer object size is too small "
1238 			  "for requested mode.\n");
1239 		return -EINVAL;
1240 	}
1241 
1242 	if (!drm_any_plane_has_format(&dev_priv->drm,
1243 				      mode_cmd->pixel_format,
1244 				      mode_cmd->modifier[0])) {
1245 		drm_dbg(&dev_priv->drm,
1246 			"unsupported pixel format %p4cc / modifier 0x%llx\n",
1247 			&mode_cmd->pixel_format, mode_cmd->modifier[0]);
1248 		return -EINVAL;
1249 	}
1250 
1251 	vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
1252 	if (!vfbd) {
1253 		ret = -ENOMEM;
1254 		goto out_err1;
1255 	}
1256 
1257 	drm_helper_mode_fill_fb_struct(dev, &vfbd->base.base, mode_cmd);
1258 	vfbd->base.bo = true;
1259 	vfbd->buffer = vmw_bo_reference(bo);
1260 	vfbd->base.user_handle = mode_cmd->handles[0];
1261 	*out = &vfbd->base;
1262 
1263 	ret = drm_framebuffer_init(dev, &vfbd->base.base,
1264 				   &vmw_framebuffer_bo_funcs);
1265 	if (ret)
1266 		goto out_err2;
1267 
1268 	return 0;
1269 
1270 out_err2:
1271 	vmw_bo_unreference(&bo);
1272 	kfree(vfbd);
1273 out_err1:
1274 	return ret;
1275 }
1276 
1277 
1278 /**
1279  * vmw_kms_srf_ok - check if a surface can be created
1280  *
1281  * @dev_priv: Pointer to device private struct.
1282  * @width: requested width
1283  * @height: requested height
1284  *
1285  * Surfaces need to be less than texture size
1286  */
1287 static bool
vmw_kms_srf_ok(struct vmw_private * dev_priv,uint32_t width,uint32_t height)1288 vmw_kms_srf_ok(struct vmw_private *dev_priv, uint32_t width, uint32_t height)
1289 {
1290 	if (width  > dev_priv->texture_max_width ||
1291 	    height > dev_priv->texture_max_height)
1292 		return false;
1293 
1294 	return true;
1295 }
1296 
1297 /**
1298  * vmw_kms_new_framebuffer - Create a new framebuffer.
1299  *
1300  * @dev_priv: Pointer to device private struct.
1301  * @bo: Pointer to buffer object to wrap the kms framebuffer around.
1302  * Either @bo or @surface must be NULL.
1303  * @surface: Pointer to a surface to wrap the kms framebuffer around.
1304  * Either @bo or @surface must be NULL.
1305  * @only_2d: No presents will occur to this buffer object based framebuffer.
1306  * This helps the code to do some important optimizations.
1307  * @mode_cmd: Frame-buffer metadata.
1308  */
1309 struct vmw_framebuffer *
vmw_kms_new_framebuffer(struct vmw_private * dev_priv,struct vmw_buffer_object * bo,struct vmw_surface * surface,bool only_2d,const struct drm_mode_fb_cmd2 * mode_cmd)1310 vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
1311 			struct vmw_buffer_object *bo,
1312 			struct vmw_surface *surface,
1313 			bool only_2d,
1314 			const struct drm_mode_fb_cmd2 *mode_cmd)
1315 {
1316 	struct vmw_framebuffer *vfb = NULL;
1317 	bool is_bo_proxy = false;
1318 	int ret;
1319 
1320 	/*
1321 	 * We cannot use the SurfaceDMA command in an non-accelerated VM,
1322 	 * therefore, wrap the buffer object in a surface so we can use the
1323 	 * SurfaceCopy command.
1324 	 */
1325 	if (vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height)  &&
1326 	    bo && only_2d &&
1327 	    mode_cmd->width > 64 &&  /* Don't create a proxy for cursor */
1328 	    dev_priv->active_display_unit == vmw_du_screen_target) {
1329 		ret = vmw_create_bo_proxy(&dev_priv->drm, mode_cmd,
1330 					  bo, &surface);
1331 		if (ret)
1332 			return ERR_PTR(ret);
1333 
1334 		is_bo_proxy = true;
1335 	}
1336 
1337 	/* Create the new framebuffer depending one what we have */
1338 	if (surface) {
1339 		ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb,
1340 						      mode_cmd,
1341 						      is_bo_proxy);
1342 		/*
1343 		 * vmw_create_bo_proxy() adds a reference that is no longer
1344 		 * needed
1345 		 */
1346 		if (is_bo_proxy)
1347 			vmw_surface_unreference(&surface);
1348 	} else if (bo) {
1349 		ret = vmw_kms_new_framebuffer_bo(dev_priv, bo, &vfb,
1350 						 mode_cmd);
1351 	} else {
1352 		BUG();
1353 	}
1354 
1355 	if (ret)
1356 		return ERR_PTR(ret);
1357 
1358 	vfb->pin = vmw_framebuffer_pin;
1359 	vfb->unpin = vmw_framebuffer_unpin;
1360 
1361 	return vfb;
1362 }
1363 
1364 /*
1365  * Generic Kernel modesetting functions
1366  */
1367 
vmw_kms_fb_create(struct drm_device * dev,struct drm_file * file_priv,const struct drm_mode_fb_cmd2 * mode_cmd)1368 static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
1369 						 struct drm_file *file_priv,
1370 						 const struct drm_mode_fb_cmd2 *mode_cmd)
1371 {
1372 	struct vmw_private *dev_priv = vmw_priv(dev);
1373 	struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1374 	struct vmw_framebuffer *vfb = NULL;
1375 	struct vmw_surface *surface = NULL;
1376 	struct vmw_buffer_object *bo = NULL;
1377 	struct ttm_base_object *user_obj;
1378 	int ret;
1379 
1380 	/*
1381 	 * Take a reference on the user object of the resource
1382 	 * backing the kms fb. This ensures that user-space handle
1383 	 * lookups on that resource will always work as long as
1384 	 * it's registered with a kms framebuffer. This is important,
1385 	 * since vmw_execbuf_process identifies resources in the
1386 	 * command stream using user-space handles.
1387 	 */
1388 
1389 	user_obj = ttm_base_object_lookup(tfile, mode_cmd->handles[0]);
1390 	if (unlikely(user_obj == NULL)) {
1391 		DRM_ERROR("Could not locate requested kms frame buffer.\n");
1392 		return ERR_PTR(-ENOENT);
1393 	}
1394 
1395 	/**
1396 	 * End conditioned code.
1397 	 */
1398 
1399 	/* returns either a bo or surface */
1400 	ret = vmw_user_lookup_handle(dev_priv, tfile,
1401 				     mode_cmd->handles[0],
1402 				     &surface, &bo);
1403 	if (ret) {
1404 		DRM_ERROR("Invalid buffer object handle %u (0x%x).\n",
1405 			  mode_cmd->handles[0], mode_cmd->handles[0]);
1406 		goto err_out;
1407 	}
1408 
1409 
1410 	if (!bo &&
1411 	    !vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height)) {
1412 		DRM_ERROR("Surface size cannot exceed %dx%d\n",
1413 			dev_priv->texture_max_width,
1414 			dev_priv->texture_max_height);
1415 		goto err_out;
1416 	}
1417 
1418 
1419 	vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface,
1420 				      !(dev_priv->capabilities & SVGA_CAP_3D),
1421 				      mode_cmd);
1422 	if (IS_ERR(vfb)) {
1423 		ret = PTR_ERR(vfb);
1424 		goto err_out;
1425  	}
1426 
1427 err_out:
1428 	/* vmw_user_lookup_handle takes one ref so does new_fb */
1429 	if (bo)
1430 		vmw_bo_unreference(&bo);
1431 	if (surface)
1432 		vmw_surface_unreference(&surface);
1433 
1434 	if (ret) {
1435 		DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
1436 		ttm_base_object_unref(&user_obj);
1437 		return ERR_PTR(ret);
1438 	} else
1439 		vfb->user_obj = user_obj;
1440 
1441 	return &vfb->base;
1442 }
1443 
1444 /**
1445  * vmw_kms_check_display_memory - Validates display memory required for a
1446  * topology
1447  * @dev: DRM device
1448  * @num_rects: number of drm_rect in rects
1449  * @rects: array of drm_rect representing the topology to validate indexed by
1450  * crtc index.
1451  *
1452  * Returns:
1453  * 0 on success otherwise negative error code
1454  */
vmw_kms_check_display_memory(struct drm_device * dev,uint32_t num_rects,struct drm_rect * rects)1455 static int vmw_kms_check_display_memory(struct drm_device *dev,
1456 					uint32_t num_rects,
1457 					struct drm_rect *rects)
1458 {
1459 	struct vmw_private *dev_priv = vmw_priv(dev);
1460 	struct drm_rect bounding_box = {0};
1461 	u64 total_pixels = 0, pixel_mem, bb_mem;
1462 	int i;
1463 
1464 	for (i = 0; i < num_rects; i++) {
1465 		/*
1466 		 * For STDU only individual screen (screen target) is limited by
1467 		 * SCREENTARGET_MAX_WIDTH/HEIGHT registers.
1468 		 */
1469 		if (dev_priv->active_display_unit == vmw_du_screen_target &&
1470 		    (drm_rect_width(&rects[i]) > dev_priv->stdu_max_width ||
1471 		     drm_rect_height(&rects[i]) > dev_priv->stdu_max_height)) {
1472 			VMW_DEBUG_KMS("Screen size not supported.\n");
1473 			return -EINVAL;
1474 		}
1475 
1476 		/* Bounding box upper left is at (0,0). */
1477 		if (rects[i].x2 > bounding_box.x2)
1478 			bounding_box.x2 = rects[i].x2;
1479 
1480 		if (rects[i].y2 > bounding_box.y2)
1481 			bounding_box.y2 = rects[i].y2;
1482 
1483 		total_pixels += (u64) drm_rect_width(&rects[i]) *
1484 			(u64) drm_rect_height(&rects[i]);
1485 	}
1486 
1487 	/* Virtual svga device primary limits are always in 32-bpp. */
1488 	pixel_mem = total_pixels * 4;
1489 
1490 	/*
1491 	 * For HV10 and below prim_bb_mem is vram size. When
1492 	 * SVGA_REG_MAX_PRIMARY_BOUNDING_BOX_MEM is not present vram size is
1493 	 * limit on primary bounding box
1494 	 */
1495 	if (pixel_mem > dev_priv->max_primary_mem) {
1496 		VMW_DEBUG_KMS("Combined output size too large.\n");
1497 		return -EINVAL;
1498 	}
1499 
1500 	/* SVGA_CAP_NO_BB_RESTRICTION is available for STDU only. */
1501 	if (dev_priv->active_display_unit != vmw_du_screen_target ||
1502 	    !(dev_priv->capabilities & SVGA_CAP_NO_BB_RESTRICTION)) {
1503 		bb_mem = (u64) bounding_box.x2 * bounding_box.y2 * 4;
1504 
1505 		if (bb_mem > dev_priv->max_primary_mem) {
1506 			VMW_DEBUG_KMS("Topology is beyond supported limits.\n");
1507 			return -EINVAL;
1508 		}
1509 	}
1510 
1511 	return 0;
1512 }
1513 
1514 /**
1515  * vmw_crtc_state_and_lock - Return new or current crtc state with locked
1516  * crtc mutex
1517  * @state: The atomic state pointer containing the new atomic state
1518  * @crtc: The crtc
1519  *
1520  * This function returns the new crtc state if it's part of the state update.
1521  * Otherwise returns the current crtc state. It also makes sure that the
1522  * crtc mutex is locked.
1523  *
1524  * Returns: A valid crtc state pointer or NULL. It may also return a
1525  * pointer error, in particular -EDEADLK if locking needs to be rerun.
1526  */
1527 static struct drm_crtc_state *
vmw_crtc_state_and_lock(struct drm_atomic_state * state,struct drm_crtc * crtc)1528 vmw_crtc_state_and_lock(struct drm_atomic_state *state, struct drm_crtc *crtc)
1529 {
1530 	struct drm_crtc_state *crtc_state;
1531 
1532 	crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1533 	if (crtc_state) {
1534 		lockdep_assert_held(&crtc->mutex.mutex.base);
1535 	} else {
1536 		int ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
1537 
1538 		if (ret != 0 && ret != -EALREADY)
1539 			return ERR_PTR(ret);
1540 
1541 		crtc_state = crtc->state;
1542 	}
1543 
1544 	return crtc_state;
1545 }
1546 
1547 /**
1548  * vmw_kms_check_implicit - Verify that all implicit display units scan out
1549  * from the same fb after the new state is committed.
1550  * @dev: The drm_device.
1551  * @state: The new state to be checked.
1552  *
1553  * Returns:
1554  *   Zero on success,
1555  *   -EINVAL on invalid state,
1556  *   -EDEADLK if modeset locking needs to be rerun.
1557  */
vmw_kms_check_implicit(struct drm_device * dev,struct drm_atomic_state * state)1558 static int vmw_kms_check_implicit(struct drm_device *dev,
1559 				  struct drm_atomic_state *state)
1560 {
1561 	struct drm_framebuffer *implicit_fb = NULL;
1562 	struct drm_crtc *crtc;
1563 	struct drm_crtc_state *crtc_state;
1564 	struct drm_plane_state *plane_state;
1565 
1566 	drm_for_each_crtc(crtc, dev) {
1567 		struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
1568 
1569 		if (!du->is_implicit)
1570 			continue;
1571 
1572 		crtc_state = vmw_crtc_state_and_lock(state, crtc);
1573 		if (IS_ERR(crtc_state))
1574 			return PTR_ERR(crtc_state);
1575 
1576 		if (!crtc_state || !crtc_state->enable)
1577 			continue;
1578 
1579 		/*
1580 		 * Can't move primary planes across crtcs, so this is OK.
1581 		 * It also means we don't need to take the plane mutex.
1582 		 */
1583 		plane_state = du->primary.state;
1584 		if (plane_state->crtc != crtc)
1585 			continue;
1586 
1587 		if (!implicit_fb)
1588 			implicit_fb = plane_state->fb;
1589 		else if (implicit_fb != plane_state->fb)
1590 			return -EINVAL;
1591 	}
1592 
1593 	return 0;
1594 }
1595 
1596 /**
1597  * vmw_kms_check_topology - Validates topology in drm_atomic_state
1598  * @dev: DRM device
1599  * @state: the driver state object
1600  *
1601  * Returns:
1602  * 0 on success otherwise negative error code
1603  */
vmw_kms_check_topology(struct drm_device * dev,struct drm_atomic_state * state)1604 static int vmw_kms_check_topology(struct drm_device *dev,
1605 				  struct drm_atomic_state *state)
1606 {
1607 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1608 	struct drm_rect *rects;
1609 	struct drm_crtc *crtc;
1610 	uint32_t i;
1611 	int ret = 0;
1612 
1613 	rects = kcalloc(dev->mode_config.num_crtc, sizeof(struct drm_rect),
1614 			GFP_KERNEL);
1615 	if (!rects)
1616 		return -ENOMEM;
1617 
1618 	drm_for_each_crtc(crtc, dev) {
1619 		struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
1620 		struct drm_crtc_state *crtc_state;
1621 
1622 		i = drm_crtc_index(crtc);
1623 
1624 		crtc_state = vmw_crtc_state_and_lock(state, crtc);
1625 		if (IS_ERR(crtc_state)) {
1626 			ret = PTR_ERR(crtc_state);
1627 			goto clean;
1628 		}
1629 
1630 		if (!crtc_state)
1631 			continue;
1632 
1633 		if (crtc_state->enable) {
1634 			rects[i].x1 = du->gui_x;
1635 			rects[i].y1 = du->gui_y;
1636 			rects[i].x2 = du->gui_x + crtc_state->mode.hdisplay;
1637 			rects[i].y2 = du->gui_y + crtc_state->mode.vdisplay;
1638 		} else {
1639 			rects[i].x1 = 0;
1640 			rects[i].y1 = 0;
1641 			rects[i].x2 = 0;
1642 			rects[i].y2 = 0;
1643 		}
1644 	}
1645 
1646 	/* Determine change to topology due to new atomic state */
1647 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
1648 				      new_crtc_state, i) {
1649 		struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
1650 		struct drm_connector *connector;
1651 		struct drm_connector_state *conn_state;
1652 		struct vmw_connector_state *vmw_conn_state;
1653 
1654 		if (!du->pref_active && new_crtc_state->enable) {
1655 			VMW_DEBUG_KMS("Enabling a disabled display unit\n");
1656 			ret = -EINVAL;
1657 			goto clean;
1658 		}
1659 
1660 		/*
1661 		 * For vmwgfx each crtc has only one connector attached and it
1662 		 * is not changed so don't really need to check the
1663 		 * crtc->connector_mask and iterate over it.
1664 		 */
1665 		connector = &du->connector;
1666 		conn_state = drm_atomic_get_connector_state(state, connector);
1667 		if (IS_ERR(conn_state)) {
1668 			ret = PTR_ERR(conn_state);
1669 			goto clean;
1670 		}
1671 
1672 		vmw_conn_state = vmw_connector_state_to_vcs(conn_state);
1673 		vmw_conn_state->gui_x = du->gui_x;
1674 		vmw_conn_state->gui_y = du->gui_y;
1675 	}
1676 
1677 	ret = vmw_kms_check_display_memory(dev, dev->mode_config.num_crtc,
1678 					   rects);
1679 
1680 clean:
1681 	kfree(rects);
1682 	return ret;
1683 }
1684 
1685 /**
1686  * vmw_kms_atomic_check_modeset- validate state object for modeset changes
1687  *
1688  * @dev: DRM device
1689  * @state: the driver state object
1690  *
1691  * This is a simple wrapper around drm_atomic_helper_check_modeset() for
1692  * us to assign a value to mode->crtc_clock so that
1693  * drm_calc_timestamping_constants() won't throw an error message
1694  *
1695  * Returns:
1696  * Zero for success or -errno
1697  */
1698 static int
vmw_kms_atomic_check_modeset(struct drm_device * dev,struct drm_atomic_state * state)1699 vmw_kms_atomic_check_modeset(struct drm_device *dev,
1700 			     struct drm_atomic_state *state)
1701 {
1702 	struct drm_crtc *crtc;
1703 	struct drm_crtc_state *crtc_state;
1704 	bool need_modeset = false;
1705 	int i, ret;
1706 
1707 	ret = drm_atomic_helper_check(dev, state);
1708 	if (ret)
1709 		return ret;
1710 
1711 	ret = vmw_kms_check_implicit(dev, state);
1712 	if (ret) {
1713 		VMW_DEBUG_KMS("Invalid implicit state\n");
1714 		return ret;
1715 	}
1716 
1717 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1718 		if (drm_atomic_crtc_needs_modeset(crtc_state))
1719 			need_modeset = true;
1720 	}
1721 
1722 	if (need_modeset)
1723 		return vmw_kms_check_topology(dev, state);
1724 
1725 	return ret;
1726 }
1727 
1728 static const struct drm_mode_config_funcs vmw_kms_funcs = {
1729 	.fb_create = vmw_kms_fb_create,
1730 	.atomic_check = vmw_kms_atomic_check_modeset,
1731 	.atomic_commit = drm_atomic_helper_commit,
1732 };
1733 
vmw_kms_generic_present(struct vmw_private * dev_priv,struct drm_file * file_priv,struct vmw_framebuffer * vfb,struct vmw_surface * surface,uint32_t sid,int32_t destX,int32_t destY,struct drm_vmw_rect * clips,uint32_t num_clips)1734 static int vmw_kms_generic_present(struct vmw_private *dev_priv,
1735 				   struct drm_file *file_priv,
1736 				   struct vmw_framebuffer *vfb,
1737 				   struct vmw_surface *surface,
1738 				   uint32_t sid,
1739 				   int32_t destX, int32_t destY,
1740 				   struct drm_vmw_rect *clips,
1741 				   uint32_t num_clips)
1742 {
1743 	return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
1744 					    &surface->res, destX, destY,
1745 					    num_clips, 1, NULL, NULL);
1746 }
1747 
1748 
vmw_kms_present(struct vmw_private * dev_priv,struct drm_file * file_priv,struct vmw_framebuffer * vfb,struct vmw_surface * surface,uint32_t sid,int32_t destX,int32_t destY,struct drm_vmw_rect * clips,uint32_t num_clips)1749 int vmw_kms_present(struct vmw_private *dev_priv,
1750 		    struct drm_file *file_priv,
1751 		    struct vmw_framebuffer *vfb,
1752 		    struct vmw_surface *surface,
1753 		    uint32_t sid,
1754 		    int32_t destX, int32_t destY,
1755 		    struct drm_vmw_rect *clips,
1756 		    uint32_t num_clips)
1757 {
1758 	int ret;
1759 
1760 	switch (dev_priv->active_display_unit) {
1761 	case vmw_du_screen_target:
1762 		ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
1763 						 &surface->res, destX, destY,
1764 						 num_clips, 1, NULL, NULL);
1765 		break;
1766 	case vmw_du_screen_object:
1767 		ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
1768 					      sid, destX, destY, clips,
1769 					      num_clips);
1770 		break;
1771 	default:
1772 		WARN_ONCE(true,
1773 			  "Present called with invalid display system.\n");
1774 		ret = -ENOSYS;
1775 		break;
1776 	}
1777 	if (ret)
1778 		return ret;
1779 
1780 	vmw_cmd_flush(dev_priv, false);
1781 
1782 	return 0;
1783 }
1784 
1785 static void
vmw_kms_create_hotplug_mode_update_property(struct vmw_private * dev_priv)1786 vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
1787 {
1788 	if (dev_priv->hotplug_mode_update_property)
1789 		return;
1790 
1791 	dev_priv->hotplug_mode_update_property =
1792 		drm_property_create_range(&dev_priv->drm,
1793 					  DRM_MODE_PROP_IMMUTABLE,
1794 					  "hotplug_mode_update", 0, 1);
1795 }
1796 
vmw_kms_init(struct vmw_private * dev_priv)1797 int vmw_kms_init(struct vmw_private *dev_priv)
1798 {
1799 	struct drm_device *dev = &dev_priv->drm;
1800 	int ret;
1801 	static const char *display_unit_names[] = {
1802 		"Invalid",
1803 		"Legacy",
1804 		"Screen Object",
1805 		"Screen Target",
1806 		"Invalid (max)"
1807 	};
1808 
1809 	drm_mode_config_init(dev);
1810 	dev->mode_config.funcs = &vmw_kms_funcs;
1811 	dev->mode_config.min_width = 1;
1812 	dev->mode_config.min_height = 1;
1813 	dev->mode_config.max_width = dev_priv->texture_max_width;
1814 	dev->mode_config.max_height = dev_priv->texture_max_height;
1815 
1816 	drm_mode_create_suggested_offset_properties(dev);
1817 	vmw_kms_create_hotplug_mode_update_property(dev_priv);
1818 
1819 	ret = vmw_kms_stdu_init_display(dev_priv);
1820 	if (ret) {
1821 		ret = vmw_kms_sou_init_display(dev_priv);
1822 		if (ret) /* Fallback */
1823 			ret = vmw_kms_ldu_init_display(dev_priv);
1824 	}
1825 	BUILD_BUG_ON(ARRAY_SIZE(display_unit_names) != (vmw_du_max + 1));
1826 	drm_info(&dev_priv->drm, "%s display unit initialized\n",
1827 		 display_unit_names[dev_priv->active_display_unit]);
1828 
1829 	return ret;
1830 }
1831 
vmw_kms_close(struct vmw_private * dev_priv)1832 int vmw_kms_close(struct vmw_private *dev_priv)
1833 {
1834 	int ret = 0;
1835 
1836 	/*
1837 	 * Docs says we should take the lock before calling this function
1838 	 * but since it destroys encoders and our destructor calls
1839 	 * drm_encoder_cleanup which takes the lock we deadlock.
1840 	 */
1841 	drm_mode_config_cleanup(&dev_priv->drm);
1842 	if (dev_priv->active_display_unit == vmw_du_legacy)
1843 		ret = vmw_kms_ldu_close_display(dev_priv);
1844 
1845 	return ret;
1846 }
1847 
vmw_kms_cursor_bypass_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)1848 int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1849 				struct drm_file *file_priv)
1850 {
1851 	struct drm_vmw_cursor_bypass_arg *arg = data;
1852 	struct vmw_display_unit *du;
1853 	struct drm_crtc *crtc;
1854 	int ret = 0;
1855 
1856 
1857 	mutex_lock(&dev->mode_config.mutex);
1858 	if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1859 
1860 		list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1861 			du = vmw_crtc_to_du(crtc);
1862 			du->hotspot_x = arg->xhot;
1863 			du->hotspot_y = arg->yhot;
1864 		}
1865 
1866 		mutex_unlock(&dev->mode_config.mutex);
1867 		return 0;
1868 	}
1869 
1870 	crtc = drm_crtc_find(dev, file_priv, arg->crtc_id);
1871 	if (!crtc) {
1872 		ret = -ENOENT;
1873 		goto out;
1874 	}
1875 
1876 	du = vmw_crtc_to_du(crtc);
1877 
1878 	du->hotspot_x = arg->xhot;
1879 	du->hotspot_y = arg->yhot;
1880 
1881 out:
1882 	mutex_unlock(&dev->mode_config.mutex);
1883 
1884 	return ret;
1885 }
1886 
vmw_kms_write_svga(struct vmw_private * vmw_priv,unsigned width,unsigned height,unsigned pitch,unsigned bpp,unsigned depth)1887 int vmw_kms_write_svga(struct vmw_private *vmw_priv,
1888 			unsigned width, unsigned height, unsigned pitch,
1889 			unsigned bpp, unsigned depth)
1890 {
1891 	if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1892 		vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1893 	else if (vmw_fifo_have_pitchlock(vmw_priv))
1894 		vmw_fifo_mem_write(vmw_priv, SVGA_FIFO_PITCHLOCK, pitch);
1895 	vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1896 	vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
1897 	if ((vmw_priv->capabilities & SVGA_CAP_8BIT_EMULATION) != 0)
1898 		vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
1899 
1900 	if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1901 		DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1902 			  depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1903 		return -EINVAL;
1904 	}
1905 
1906 	return 0;
1907 }
1908 
vmw_kms_validate_mode_vram(struct vmw_private * dev_priv,uint32_t pitch,uint32_t height)1909 bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1910 				uint32_t pitch,
1911 				uint32_t height)
1912 {
1913 	return ((u64) pitch * (u64) height) < (u64)
1914 		((dev_priv->active_display_unit == vmw_du_screen_target) ?
1915 		 dev_priv->max_primary_mem : dev_priv->vram_size);
1916 }
1917 
1918 
1919 /*
1920  * Function called by DRM code called with vbl_lock held.
1921  */
vmw_get_vblank_counter(struct drm_crtc * crtc)1922 u32 vmw_get_vblank_counter(struct drm_crtc *crtc)
1923 {
1924 	return 0;
1925 }
1926 
1927 /*
1928  * Function called by DRM code called with vbl_lock held.
1929  */
vmw_enable_vblank(struct drm_crtc * crtc)1930 int vmw_enable_vblank(struct drm_crtc *crtc)
1931 {
1932 	return -EINVAL;
1933 }
1934 
1935 /*
1936  * Function called by DRM code called with vbl_lock held.
1937  */
vmw_disable_vblank(struct drm_crtc * crtc)1938 void vmw_disable_vblank(struct drm_crtc *crtc)
1939 {
1940 }
1941 
1942 /**
1943  * vmw_du_update_layout - Update the display unit with topology from resolution
1944  * plugin and generate DRM uevent
1945  * @dev_priv: device private
1946  * @num_rects: number of drm_rect in rects
1947  * @rects: toplogy to update
1948  */
vmw_du_update_layout(struct vmw_private * dev_priv,unsigned int num_rects,struct drm_rect * rects)1949 static int vmw_du_update_layout(struct vmw_private *dev_priv,
1950 				unsigned int num_rects, struct drm_rect *rects)
1951 {
1952 	struct drm_device *dev = &dev_priv->drm;
1953 	struct vmw_display_unit *du;
1954 	struct drm_connector *con;
1955 	struct drm_connector_list_iter conn_iter;
1956 	struct drm_modeset_acquire_ctx ctx;
1957 	struct drm_crtc *crtc;
1958 	int ret;
1959 
1960 	/* Currently gui_x/y is protected with the crtc mutex */
1961 	mutex_lock(&dev->mode_config.mutex);
1962 	drm_modeset_acquire_init(&ctx, 0);
1963 retry:
1964 	drm_for_each_crtc(crtc, dev) {
1965 		ret = drm_modeset_lock(&crtc->mutex, &ctx);
1966 		if (ret < 0) {
1967 			if (ret == -EDEADLK) {
1968 				drm_modeset_backoff(&ctx);
1969 				goto retry;
1970       		}
1971 			goto out_fini;
1972 		}
1973 	}
1974 
1975 	drm_connector_list_iter_begin(dev, &conn_iter);
1976 	drm_for_each_connector_iter(con, &conn_iter) {
1977 		du = vmw_connector_to_du(con);
1978 		if (num_rects > du->unit) {
1979 			du->pref_width = drm_rect_width(&rects[du->unit]);
1980 			du->pref_height = drm_rect_height(&rects[du->unit]);
1981 			du->pref_active = true;
1982 			du->gui_x = rects[du->unit].x1;
1983 			du->gui_y = rects[du->unit].y1;
1984 		} else {
1985 			du->pref_width = 800;
1986 			du->pref_height = 600;
1987 			du->pref_active = false;
1988 			du->gui_x = 0;
1989 			du->gui_y = 0;
1990 		}
1991 	}
1992 	drm_connector_list_iter_end(&conn_iter);
1993 
1994 	list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1995 		du = vmw_connector_to_du(con);
1996 		if (num_rects > du->unit) {
1997 			drm_object_property_set_value
1998 			  (&con->base, dev->mode_config.suggested_x_property,
1999 			   du->gui_x);
2000 			drm_object_property_set_value
2001 			  (&con->base, dev->mode_config.suggested_y_property,
2002 			   du->gui_y);
2003 		} else {
2004 			drm_object_property_set_value
2005 			  (&con->base, dev->mode_config.suggested_x_property,
2006 			   0);
2007 			drm_object_property_set_value
2008 			  (&con->base, dev->mode_config.suggested_y_property,
2009 			   0);
2010 		}
2011 		con->status = vmw_du_connector_detect(con, true);
2012 	}
2013 
2014 	drm_sysfs_hotplug_event(dev);
2015 out_fini:
2016 	drm_modeset_drop_locks(&ctx);
2017 	drm_modeset_acquire_fini(&ctx);
2018 	mutex_unlock(&dev->mode_config.mutex);
2019 
2020 	return 0;
2021 }
2022 
vmw_du_crtc_gamma_set(struct drm_crtc * crtc,u16 * r,u16 * g,u16 * b,uint32_t size,struct drm_modeset_acquire_ctx * ctx)2023 int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
2024 			  u16 *r, u16 *g, u16 *b,
2025 			  uint32_t size,
2026 			  struct drm_modeset_acquire_ctx *ctx)
2027 {
2028 	struct vmw_private *dev_priv = vmw_priv(crtc->dev);
2029 	int i;
2030 
2031 	for (i = 0; i < size; i++) {
2032 		DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
2033 			  r[i], g[i], b[i]);
2034 		vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
2035 		vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
2036 		vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
2037 	}
2038 
2039 	return 0;
2040 }
2041 
vmw_du_connector_dpms(struct drm_connector * connector,int mode)2042 int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
2043 {
2044 	return 0;
2045 }
2046 
2047 enum drm_connector_status
vmw_du_connector_detect(struct drm_connector * connector,bool force)2048 vmw_du_connector_detect(struct drm_connector *connector, bool force)
2049 {
2050 	uint32_t num_displays;
2051 	struct drm_device *dev = connector->dev;
2052 	struct vmw_private *dev_priv = vmw_priv(dev);
2053 	struct vmw_display_unit *du = vmw_connector_to_du(connector);
2054 
2055 	num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
2056 
2057 	return ((vmw_connector_to_du(connector)->unit < num_displays &&
2058 		 du->pref_active) ?
2059 		connector_status_connected : connector_status_disconnected);
2060 }
2061 
2062 static struct drm_display_mode vmw_kms_connector_builtin[] = {
2063 	/* 640x480@60Hz */
2064 	{ DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
2065 		   752, 800, 0, 480, 489, 492, 525, 0,
2066 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
2067 	/* 800x600@60Hz */
2068 	{ DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
2069 		   968, 1056, 0, 600, 601, 605, 628, 0,
2070 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2071 	/* 1024x768@60Hz */
2072 	{ DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
2073 		   1184, 1344, 0, 768, 771, 777, 806, 0,
2074 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
2075 	/* 1152x864@75Hz */
2076 	{ DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
2077 		   1344, 1600, 0, 864, 865, 868, 900, 0,
2078 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2079 	/* 1280x720@60Hz */
2080 	{ DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74500, 1280, 1344,
2081 		   1472, 1664, 0, 720, 723, 728, 748, 0,
2082 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2083 	/* 1280x768@60Hz */
2084 	{ DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
2085 		   1472, 1664, 0, 768, 771, 778, 798, 0,
2086 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2087 	/* 1280x800@60Hz */
2088 	{ DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
2089 		   1480, 1680, 0, 800, 803, 809, 831, 0,
2090 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
2091 	/* 1280x960@60Hz */
2092 	{ DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
2093 		   1488, 1800, 0, 960, 961, 964, 1000, 0,
2094 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2095 	/* 1280x1024@60Hz */
2096 	{ DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
2097 		   1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
2098 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2099 	/* 1360x768@60Hz */
2100 	{ DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
2101 		   1536, 1792, 0, 768, 771, 777, 795, 0,
2102 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2103 	/* 1440x1050@60Hz */
2104 	{ DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
2105 		   1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
2106 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2107 	/* 1440x900@60Hz */
2108 	{ DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
2109 		   1672, 1904, 0, 900, 903, 909, 934, 0,
2110 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2111 	/* 1600x1200@60Hz */
2112 	{ DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
2113 		   1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
2114 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2115 	/* 1680x1050@60Hz */
2116 	{ DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
2117 		   1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
2118 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2119 	/* 1792x1344@60Hz */
2120 	{ DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
2121 		   2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
2122 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2123 	/* 1853x1392@60Hz */
2124 	{ DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
2125 		   2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
2126 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2127 	/* 1920x1080@60Hz */
2128 	{ DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 173000, 1920, 2048,
2129 		   2248, 2576, 0, 1080, 1083, 1088, 1120, 0,
2130 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2131 	/* 1920x1200@60Hz */
2132 	{ DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
2133 		   2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
2134 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2135 	/* 1920x1440@60Hz */
2136 	{ DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
2137 		   2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
2138 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2139 	/* 2560x1440@60Hz */
2140 	{ DRM_MODE("2560x1440", DRM_MODE_TYPE_DRIVER, 241500, 2560, 2608,
2141 		   2640, 2720, 0, 1440, 1443, 1448, 1481, 0,
2142 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
2143 	/* 2560x1600@60Hz */
2144 	{ DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
2145 		   3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
2146 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2147 	/* 2880x1800@60Hz */
2148 	{ DRM_MODE("2880x1800", DRM_MODE_TYPE_DRIVER, 337500, 2880, 2928,
2149 		   2960, 3040, 0, 1800, 1803, 1809, 1852, 0,
2150 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
2151 	/* 3840x2160@60Hz */
2152 	{ DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 533000, 3840, 3888,
2153 		   3920, 4000, 0, 2160, 2163, 2168, 2222, 0,
2154 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
2155 	/* 3840x2400@60Hz */
2156 	{ DRM_MODE("3840x2400", DRM_MODE_TYPE_DRIVER, 592250, 3840, 3888,
2157 		   3920, 4000, 0, 2400, 2403, 2409, 2469, 0,
2158 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
2159 	/* Terminate */
2160 	{ DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
2161 };
2162 
2163 /**
2164  * vmw_guess_mode_timing - Provide fake timings for a
2165  * 60Hz vrefresh mode.
2166  *
2167  * @mode: Pointer to a struct drm_display_mode with hdisplay and vdisplay
2168  * members filled in.
2169  */
vmw_guess_mode_timing(struct drm_display_mode * mode)2170 void vmw_guess_mode_timing(struct drm_display_mode *mode)
2171 {
2172 	mode->hsync_start = mode->hdisplay + 50;
2173 	mode->hsync_end = mode->hsync_start + 50;
2174 	mode->htotal = mode->hsync_end + 50;
2175 
2176 	mode->vsync_start = mode->vdisplay + 50;
2177 	mode->vsync_end = mode->vsync_start + 50;
2178 	mode->vtotal = mode->vsync_end + 50;
2179 
2180 	mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
2181 }
2182 
2183 
vmw_du_connector_fill_modes(struct drm_connector * connector,uint32_t max_width,uint32_t max_height)2184 int vmw_du_connector_fill_modes(struct drm_connector *connector,
2185 				uint32_t max_width, uint32_t max_height)
2186 {
2187 	struct vmw_display_unit *du = vmw_connector_to_du(connector);
2188 	struct drm_device *dev = connector->dev;
2189 	struct vmw_private *dev_priv = vmw_priv(dev);
2190 	struct drm_display_mode *mode = NULL;
2191 	struct drm_display_mode *bmode;
2192 	struct drm_display_mode prefmode = { DRM_MODE("preferred",
2193 		DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
2194 		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2195 		DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
2196 	};
2197 	int i;
2198 	u32 assumed_bpp = 4;
2199 
2200 	if (dev_priv->assume_16bpp)
2201 		assumed_bpp = 2;
2202 
2203 	max_width  = min(max_width,  dev_priv->texture_max_width);
2204 	max_height = min(max_height, dev_priv->texture_max_height);
2205 
2206 	/*
2207 	 * For STDU extra limit for a mode on SVGA_REG_SCREENTARGET_MAX_WIDTH/
2208 	 * HEIGHT registers.
2209 	 */
2210 	if (dev_priv->active_display_unit == vmw_du_screen_target) {
2211 		max_width  = min(max_width,  dev_priv->stdu_max_width);
2212 		max_height = min(max_height, dev_priv->stdu_max_height);
2213 	}
2214 
2215 	/* Add preferred mode */
2216 	mode = drm_mode_duplicate(dev, &prefmode);
2217 	if (!mode)
2218 		return 0;
2219 	mode->hdisplay = du->pref_width;
2220 	mode->vdisplay = du->pref_height;
2221 	vmw_guess_mode_timing(mode);
2222 	drm_mode_set_name(mode);
2223 
2224 	if (vmw_kms_validate_mode_vram(dev_priv,
2225 					mode->hdisplay * assumed_bpp,
2226 					mode->vdisplay)) {
2227 		drm_mode_probed_add(connector, mode);
2228 	} else {
2229 		drm_mode_destroy(dev, mode);
2230 		mode = NULL;
2231 	}
2232 
2233 	if (du->pref_mode) {
2234 		list_del_init(&du->pref_mode->head);
2235 		drm_mode_destroy(dev, du->pref_mode);
2236 	}
2237 
2238 	/* mode might be null here, this is intended */
2239 	du->pref_mode = mode;
2240 
2241 	for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
2242 		bmode = &vmw_kms_connector_builtin[i];
2243 		if (bmode->hdisplay > max_width ||
2244 		    bmode->vdisplay > max_height)
2245 			continue;
2246 
2247 		if (!vmw_kms_validate_mode_vram(dev_priv,
2248 						bmode->hdisplay * assumed_bpp,
2249 						bmode->vdisplay))
2250 			continue;
2251 
2252 		mode = drm_mode_duplicate(dev, bmode);
2253 		if (!mode)
2254 			return 0;
2255 
2256 		drm_mode_probed_add(connector, mode);
2257 	}
2258 
2259 	drm_connector_list_update(connector);
2260 	/* Move the prefered mode first, help apps pick the right mode. */
2261 	drm_mode_sort(&connector->modes);
2262 
2263 	return 1;
2264 }
2265 
2266 /**
2267  * vmw_kms_update_layout_ioctl - Handler for DRM_VMW_UPDATE_LAYOUT ioctl
2268  * @dev: drm device for the ioctl
2269  * @data: data pointer for the ioctl
2270  * @file_priv: drm file for the ioctl call
2271  *
2272  * Update preferred topology of display unit as per ioctl request. The topology
2273  * is expressed as array of drm_vmw_rect.
2274  * e.g.
2275  * [0 0 640 480] [640 0 800 600] [0 480 640 480]
2276  *
2277  * NOTE:
2278  * The x and y offset (upper left) in drm_vmw_rect cannot be less than 0. Beside
2279  * device limit on topology, x + w and y + h (lower right) cannot be greater
2280  * than INT_MAX. So topology beyond these limits will return with error.
2281  *
2282  * Returns:
2283  * Zero on success, negative errno on failure.
2284  */
vmw_kms_update_layout_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)2285 int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
2286 				struct drm_file *file_priv)
2287 {
2288 	struct vmw_private *dev_priv = vmw_priv(dev);
2289 	struct drm_mode_config *mode_config = &dev->mode_config;
2290 	struct drm_vmw_update_layout_arg *arg =
2291 		(struct drm_vmw_update_layout_arg *)data;
2292 	void __user *user_rects;
2293 	struct drm_vmw_rect *rects;
2294 	struct drm_rect *drm_rects;
2295 	unsigned rects_size;
2296 	int ret, i;
2297 
2298 	if (!arg->num_outputs) {
2299 		struct drm_rect def_rect = {0, 0, 800, 600};
2300 		VMW_DEBUG_KMS("Default layout x1 = %d y1 = %d x2 = %d y2 = %d\n",
2301 			      def_rect.x1, def_rect.y1,
2302 			      def_rect.x2, def_rect.y2);
2303 		vmw_du_update_layout(dev_priv, 1, &def_rect);
2304 		return 0;
2305 	}
2306 
2307 	rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
2308 	rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
2309 			GFP_KERNEL);
2310 	if (unlikely(!rects))
2311 		return -ENOMEM;
2312 
2313 	user_rects = (void __user *)(unsigned long)arg->rects;
2314 	ret = copy_from_user(rects, user_rects, rects_size);
2315 	if (unlikely(ret != 0)) {
2316 		DRM_ERROR("Failed to get rects.\n");
2317 		ret = -EFAULT;
2318 		goto out_free;
2319 	}
2320 
2321 	drm_rects = (struct drm_rect *)rects;
2322 
2323 	VMW_DEBUG_KMS("Layout count = %u\n", arg->num_outputs);
2324 	for (i = 0; i < arg->num_outputs; i++) {
2325 		struct drm_vmw_rect curr_rect;
2326 
2327 		/* Verify user-space for overflow as kernel use drm_rect */
2328 		if ((rects[i].x + rects[i].w > INT_MAX) ||
2329 		    (rects[i].y + rects[i].h > INT_MAX)) {
2330 			ret = -ERANGE;
2331 			goto out_free;
2332 		}
2333 
2334 		curr_rect = rects[i];
2335 		drm_rects[i].x1 = curr_rect.x;
2336 		drm_rects[i].y1 = curr_rect.y;
2337 		drm_rects[i].x2 = curr_rect.x + curr_rect.w;
2338 		drm_rects[i].y2 = curr_rect.y + curr_rect.h;
2339 
2340 		VMW_DEBUG_KMS("  x1 = %d y1 = %d x2 = %d y2 = %d\n",
2341 			      drm_rects[i].x1, drm_rects[i].y1,
2342 			      drm_rects[i].x2, drm_rects[i].y2);
2343 
2344 		/*
2345 		 * Currently this check is limiting the topology within
2346 		 * mode_config->max (which actually is max texture size
2347 		 * supported by virtual device). This limit is here to address
2348 		 * window managers that create a big framebuffer for whole
2349 		 * topology.
2350 		 */
2351 		if (drm_rects[i].x1 < 0 ||  drm_rects[i].y1 < 0 ||
2352 		    drm_rects[i].x2 > mode_config->max_width ||
2353 		    drm_rects[i].y2 > mode_config->max_height) {
2354 			VMW_DEBUG_KMS("Invalid layout %d %d %d %d\n",
2355 				      drm_rects[i].x1, drm_rects[i].y1,
2356 				      drm_rects[i].x2, drm_rects[i].y2);
2357 			ret = -EINVAL;
2358 			goto out_free;
2359 		}
2360 	}
2361 
2362 	ret = vmw_kms_check_display_memory(dev, arg->num_outputs, drm_rects);
2363 
2364 	if (ret == 0)
2365 		vmw_du_update_layout(dev_priv, arg->num_outputs, drm_rects);
2366 
2367 out_free:
2368 	kfree(rects);
2369 	return ret;
2370 }
2371 
2372 /**
2373  * vmw_kms_helper_dirty - Helper to build commands and perform actions based
2374  * on a set of cliprects and a set of display units.
2375  *
2376  * @dev_priv: Pointer to a device private structure.
2377  * @framebuffer: Pointer to the framebuffer on which to perform the actions.
2378  * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
2379  * Cliprects are given in framebuffer coordinates.
2380  * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
2381  * be NULL. Cliprects are given in source coordinates.
2382  * @dest_x: X coordinate offset for the crtc / destination clip rects.
2383  * @dest_y: Y coordinate offset for the crtc / destination clip rects.
2384  * @num_clips: Number of cliprects in the @clips or @vclips array.
2385  * @increment: Integer with which to increment the clip counter when looping.
2386  * Used to skip a predetermined number of clip rects.
2387  * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
2388  */
vmw_kms_helper_dirty(struct vmw_private * dev_priv,struct vmw_framebuffer * framebuffer,const struct drm_clip_rect * clips,const struct drm_vmw_rect * vclips,s32 dest_x,s32 dest_y,int num_clips,int increment,struct vmw_kms_dirty * dirty)2389 int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
2390 			 struct vmw_framebuffer *framebuffer,
2391 			 const struct drm_clip_rect *clips,
2392 			 const struct drm_vmw_rect *vclips,
2393 			 s32 dest_x, s32 dest_y,
2394 			 int num_clips,
2395 			 int increment,
2396 			 struct vmw_kms_dirty *dirty)
2397 {
2398 	struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
2399 	struct drm_crtc *crtc;
2400 	u32 num_units = 0;
2401 	u32 i, k;
2402 
2403 	dirty->dev_priv = dev_priv;
2404 
2405 	/* If crtc is passed, no need to iterate over other display units */
2406 	if (dirty->crtc) {
2407 		units[num_units++] = vmw_crtc_to_du(dirty->crtc);
2408 	} else {
2409 		list_for_each_entry(crtc, &dev_priv->drm.mode_config.crtc_list,
2410 				    head) {
2411 			struct drm_plane *plane = crtc->primary;
2412 
2413 			if (plane->state->fb == &framebuffer->base)
2414 				units[num_units++] = vmw_crtc_to_du(crtc);
2415 		}
2416 	}
2417 
2418 	for (k = 0; k < num_units; k++) {
2419 		struct vmw_display_unit *unit = units[k];
2420 		s32 crtc_x = unit->crtc.x;
2421 		s32 crtc_y = unit->crtc.y;
2422 		s32 crtc_width = unit->crtc.mode.hdisplay;
2423 		s32 crtc_height = unit->crtc.mode.vdisplay;
2424 		const struct drm_clip_rect *clips_ptr = clips;
2425 		const struct drm_vmw_rect *vclips_ptr = vclips;
2426 
2427 		dirty->unit = unit;
2428 		if (dirty->fifo_reserve_size > 0) {
2429 			dirty->cmd = VMW_CMD_RESERVE(dev_priv,
2430 						      dirty->fifo_reserve_size);
2431 			if (!dirty->cmd)
2432 				return -ENOMEM;
2433 
2434 			memset(dirty->cmd, 0, dirty->fifo_reserve_size);
2435 		}
2436 		dirty->num_hits = 0;
2437 		for (i = 0; i < num_clips; i++, clips_ptr += increment,
2438 		       vclips_ptr += increment) {
2439 			s32 clip_left;
2440 			s32 clip_top;
2441 
2442 			/*
2443 			 * Select clip array type. Note that integer type
2444 			 * in @clips is unsigned short, whereas in @vclips
2445 			 * it's 32-bit.
2446 			 */
2447 			if (clips) {
2448 				dirty->fb_x = (s32) clips_ptr->x1;
2449 				dirty->fb_y = (s32) clips_ptr->y1;
2450 				dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
2451 					crtc_x;
2452 				dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
2453 					crtc_y;
2454 			} else {
2455 				dirty->fb_x = vclips_ptr->x;
2456 				dirty->fb_y = vclips_ptr->y;
2457 				dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
2458 					dest_x - crtc_x;
2459 				dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
2460 					dest_y - crtc_y;
2461 			}
2462 
2463 			dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
2464 			dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
2465 
2466 			/* Skip this clip if it's outside the crtc region */
2467 			if (dirty->unit_x1 >= crtc_width ||
2468 			    dirty->unit_y1 >= crtc_height ||
2469 			    dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
2470 				continue;
2471 
2472 			/* Clip right and bottom to crtc limits */
2473 			dirty->unit_x2 = min_t(s32, dirty->unit_x2,
2474 					       crtc_width);
2475 			dirty->unit_y2 = min_t(s32, dirty->unit_y2,
2476 					       crtc_height);
2477 
2478 			/* Clip left and top to crtc limits */
2479 			clip_left = min_t(s32, dirty->unit_x1, 0);
2480 			clip_top = min_t(s32, dirty->unit_y1, 0);
2481 			dirty->unit_x1 -= clip_left;
2482 			dirty->unit_y1 -= clip_top;
2483 			dirty->fb_x -= clip_left;
2484 			dirty->fb_y -= clip_top;
2485 
2486 			dirty->clip(dirty);
2487 		}
2488 
2489 		dirty->fifo_commit(dirty);
2490 	}
2491 
2492 	return 0;
2493 }
2494 
2495 /**
2496  * vmw_kms_helper_validation_finish - Helper for post KMS command submission
2497  * cleanup and fencing
2498  * @dev_priv: Pointer to the device-private struct
2499  * @file_priv: Pointer identifying the client when user-space fencing is used
2500  * @ctx: Pointer to the validation context
2501  * @out_fence: If non-NULL, returned refcounted fence-pointer
2502  * @user_fence_rep: If non-NULL, pointer to user-space address area
2503  * in which to copy user-space fence info
2504  */
vmw_kms_helper_validation_finish(struct vmw_private * dev_priv,struct drm_file * file_priv,struct vmw_validation_context * ctx,struct vmw_fence_obj ** out_fence,struct drm_vmw_fence_rep __user * user_fence_rep)2505 void vmw_kms_helper_validation_finish(struct vmw_private *dev_priv,
2506 				      struct drm_file *file_priv,
2507 				      struct vmw_validation_context *ctx,
2508 				      struct vmw_fence_obj **out_fence,
2509 				      struct drm_vmw_fence_rep __user *
2510 				      user_fence_rep)
2511 {
2512 	struct vmw_fence_obj *fence = NULL;
2513 	uint32_t handle = 0;
2514 	int ret = 0;
2515 
2516 	if (file_priv || user_fence_rep || vmw_validation_has_bos(ctx) ||
2517 	    out_fence)
2518 		ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
2519 						 file_priv ? &handle : NULL);
2520 	vmw_validation_done(ctx, fence);
2521 	if (file_priv)
2522 		vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
2523 					    ret, user_fence_rep, fence,
2524 					    handle, -1);
2525 	if (out_fence)
2526 		*out_fence = fence;
2527 	else
2528 		vmw_fence_obj_unreference(&fence);
2529 }
2530 
2531 /**
2532  * vmw_kms_update_proxy - Helper function to update a proxy surface from
2533  * its backing MOB.
2534  *
2535  * @res: Pointer to the surface resource
2536  * @clips: Clip rects in framebuffer (surface) space.
2537  * @num_clips: Number of clips in @clips.
2538  * @increment: Integer with which to increment the clip counter when looping.
2539  * Used to skip a predetermined number of clip rects.
2540  *
2541  * This function makes sure the proxy surface is updated from its backing MOB
2542  * using the region given by @clips. The surface resource @res and its backing
2543  * MOB needs to be reserved and validated on call.
2544  */
vmw_kms_update_proxy(struct vmw_resource * res,const struct drm_clip_rect * clips,unsigned num_clips,int increment)2545 int vmw_kms_update_proxy(struct vmw_resource *res,
2546 			 const struct drm_clip_rect *clips,
2547 			 unsigned num_clips,
2548 			 int increment)
2549 {
2550 	struct vmw_private *dev_priv = res->dev_priv;
2551 	struct drm_vmw_size *size = &vmw_res_to_srf(res)->metadata.base_size;
2552 	struct {
2553 		SVGA3dCmdHeader header;
2554 		SVGA3dCmdUpdateGBImage body;
2555 	} *cmd;
2556 	SVGA3dBox *box;
2557 	size_t copy_size = 0;
2558 	int i;
2559 
2560 	if (!clips)
2561 		return 0;
2562 
2563 	cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd) * num_clips);
2564 	if (!cmd)
2565 		return -ENOMEM;
2566 
2567 	for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) {
2568 		box = &cmd->body.box;
2569 
2570 		cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
2571 		cmd->header.size = sizeof(cmd->body);
2572 		cmd->body.image.sid = res->id;
2573 		cmd->body.image.face = 0;
2574 		cmd->body.image.mipmap = 0;
2575 
2576 		if (clips->x1 > size->width || clips->x2 > size->width ||
2577 		    clips->y1 > size->height || clips->y2 > size->height) {
2578 			DRM_ERROR("Invalid clips outsize of framebuffer.\n");
2579 			return -EINVAL;
2580 		}
2581 
2582 		box->x = clips->x1;
2583 		box->y = clips->y1;
2584 		box->z = 0;
2585 		box->w = clips->x2 - clips->x1;
2586 		box->h = clips->y2 - clips->y1;
2587 		box->d = 1;
2588 
2589 		copy_size += sizeof(*cmd);
2590 	}
2591 
2592 	vmw_cmd_commit(dev_priv, copy_size);
2593 
2594 	return 0;
2595 }
2596 
vmw_kms_fbdev_init_data(struct vmw_private * dev_priv,unsigned unit,u32 max_width,u32 max_height,struct drm_connector ** p_con,struct drm_crtc ** p_crtc,struct drm_display_mode ** p_mode)2597 int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
2598 			    unsigned unit,
2599 			    u32 max_width,
2600 			    u32 max_height,
2601 			    struct drm_connector **p_con,
2602 			    struct drm_crtc **p_crtc,
2603 			    struct drm_display_mode **p_mode)
2604 {
2605 	struct drm_connector *con;
2606 	struct vmw_display_unit *du;
2607 	struct drm_display_mode *mode;
2608 	int i = 0;
2609 	int ret = 0;
2610 
2611 	mutex_lock(&dev_priv->drm.mode_config.mutex);
2612 	list_for_each_entry(con, &dev_priv->drm.mode_config.connector_list,
2613 			    head) {
2614 		if (i == unit)
2615 			break;
2616 
2617 		++i;
2618 	}
2619 
2620 	if (&con->head == &dev_priv->drm.mode_config.connector_list) {
2621 		DRM_ERROR("Could not find initial display unit.\n");
2622 		ret = -EINVAL;
2623 		goto out_unlock;
2624 	}
2625 
2626 	if (list_empty(&con->modes))
2627 		(void) vmw_du_connector_fill_modes(con, max_width, max_height);
2628 
2629 	if (list_empty(&con->modes)) {
2630 		DRM_ERROR("Could not find initial display mode.\n");
2631 		ret = -EINVAL;
2632 		goto out_unlock;
2633 	}
2634 
2635 	du = vmw_connector_to_du(con);
2636 	*p_con = con;
2637 	*p_crtc = &du->crtc;
2638 
2639 	list_for_each_entry(mode, &con->modes, head) {
2640 		if (mode->type & DRM_MODE_TYPE_PREFERRED)
2641 			break;
2642 	}
2643 
2644 	if (&mode->head == &con->modes) {
2645 		WARN_ONCE(true, "Could not find initial preferred mode.\n");
2646 		*p_mode = list_first_entry(&con->modes,
2647 					   struct drm_display_mode,
2648 					   head);
2649 	} else {
2650 		*p_mode = mode;
2651 	}
2652 
2653  out_unlock:
2654 	mutex_unlock(&dev_priv->drm.mode_config.mutex);
2655 
2656 	return ret;
2657 }
2658 
2659 /**
2660  * vmw_kms_create_implicit_placement_property - Set up the implicit placement
2661  * property.
2662  *
2663  * @dev_priv: Pointer to a device private struct.
2664  *
2665  * Sets up the implicit placement property unless it's already set up.
2666  */
2667 void
vmw_kms_create_implicit_placement_property(struct vmw_private * dev_priv)2668 vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv)
2669 {
2670 	if (dev_priv->implicit_placement_property)
2671 		return;
2672 
2673 	dev_priv->implicit_placement_property =
2674 		drm_property_create_range(&dev_priv->drm,
2675 					  DRM_MODE_PROP_IMMUTABLE,
2676 					  "implicit_placement", 0, 1);
2677 }
2678 
2679 /**
2680  * vmw_kms_suspend - Save modesetting state and turn modesetting off.
2681  *
2682  * @dev: Pointer to the drm device
2683  * Return: 0 on success. Negative error code on failure.
2684  */
vmw_kms_suspend(struct drm_device * dev)2685 int vmw_kms_suspend(struct drm_device *dev)
2686 {
2687 	struct vmw_private *dev_priv = vmw_priv(dev);
2688 
2689 	dev_priv->suspend_state = drm_atomic_helper_suspend(dev);
2690 	if (IS_ERR(dev_priv->suspend_state)) {
2691 		int ret = PTR_ERR(dev_priv->suspend_state);
2692 
2693 		DRM_ERROR("Failed kms suspend: %d\n", ret);
2694 		dev_priv->suspend_state = NULL;
2695 
2696 		return ret;
2697 	}
2698 
2699 	return 0;
2700 }
2701 
2702 
2703 /**
2704  * vmw_kms_resume - Re-enable modesetting and restore state
2705  *
2706  * @dev: Pointer to the drm device
2707  * Return: 0 on success. Negative error code on failure.
2708  *
2709  * State is resumed from a previous vmw_kms_suspend(). It's illegal
2710  * to call this function without a previous vmw_kms_suspend().
2711  */
vmw_kms_resume(struct drm_device * dev)2712 int vmw_kms_resume(struct drm_device *dev)
2713 {
2714 	struct vmw_private *dev_priv = vmw_priv(dev);
2715 	int ret;
2716 
2717 	if (WARN_ON(!dev_priv->suspend_state))
2718 		return 0;
2719 
2720 	ret = drm_atomic_helper_resume(dev, dev_priv->suspend_state);
2721 	dev_priv->suspend_state = NULL;
2722 
2723 	return ret;
2724 }
2725 
2726 /**
2727  * vmw_kms_lost_device - Notify kms that modesetting capabilities will be lost
2728  *
2729  * @dev: Pointer to the drm device
2730  */
vmw_kms_lost_device(struct drm_device * dev)2731 void vmw_kms_lost_device(struct drm_device *dev)
2732 {
2733 	drm_atomic_helper_shutdown(dev);
2734 }
2735 
2736 /**
2737  * vmw_du_helper_plane_update - Helper to do plane update on a display unit.
2738  * @update: The closure structure.
2739  *
2740  * Call this helper after setting callbacks in &vmw_du_update_plane to do plane
2741  * update on display unit.
2742  *
2743  * Return: 0 on success or a negative error code on failure.
2744  */
vmw_du_helper_plane_update(struct vmw_du_update_plane * update)2745 int vmw_du_helper_plane_update(struct vmw_du_update_plane *update)
2746 {
2747 	struct drm_plane_state *state = update->plane->state;
2748 	struct drm_plane_state *old_state = update->old_state;
2749 	struct drm_atomic_helper_damage_iter iter;
2750 	struct drm_rect clip;
2751 	struct drm_rect bb;
2752 	DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
2753 	uint32_t reserved_size = 0;
2754 	uint32_t submit_size = 0;
2755 	uint32_t curr_size = 0;
2756 	uint32_t num_hits = 0;
2757 	void *cmd_start;
2758 	char *cmd_next;
2759 	int ret;
2760 
2761 	/*
2762 	 * Iterate in advance to check if really need plane update and find the
2763 	 * number of clips that actually are in plane src for fifo allocation.
2764 	 */
2765 	drm_atomic_helper_damage_iter_init(&iter, old_state, state);
2766 	drm_atomic_for_each_plane_damage(&iter, &clip)
2767 		num_hits++;
2768 
2769 	if (num_hits == 0)
2770 		return 0;
2771 
2772 	if (update->vfb->bo) {
2773 		struct vmw_framebuffer_bo *vfbbo =
2774 			container_of(update->vfb, typeof(*vfbbo), base);
2775 
2776 		ret = vmw_validation_add_bo(&val_ctx, vfbbo->buffer, false,
2777 					    update->cpu_blit);
2778 	} else {
2779 		struct vmw_framebuffer_surface *vfbs =
2780 			container_of(update->vfb, typeof(*vfbs), base);
2781 
2782 		ret = vmw_validation_add_resource(&val_ctx, &vfbs->surface->res,
2783 						  0, VMW_RES_DIRTY_NONE, NULL,
2784 						  NULL);
2785 	}
2786 
2787 	if (ret)
2788 		return ret;
2789 
2790 	ret = vmw_validation_prepare(&val_ctx, update->mutex, update->intr);
2791 	if (ret)
2792 		goto out_unref;
2793 
2794 	reserved_size = update->calc_fifo_size(update, num_hits);
2795 	cmd_start = VMW_CMD_RESERVE(update->dev_priv, reserved_size);
2796 	if (!cmd_start) {
2797 		ret = -ENOMEM;
2798 		goto out_revert;
2799 	}
2800 
2801 	cmd_next = cmd_start;
2802 
2803 	if (update->post_prepare) {
2804 		curr_size = update->post_prepare(update, cmd_next);
2805 		cmd_next += curr_size;
2806 		submit_size += curr_size;
2807 	}
2808 
2809 	if (update->pre_clip) {
2810 		curr_size = update->pre_clip(update, cmd_next, num_hits);
2811 		cmd_next += curr_size;
2812 		submit_size += curr_size;
2813 	}
2814 
2815 	bb.x1 = INT_MAX;
2816 	bb.y1 = INT_MAX;
2817 	bb.x2 = INT_MIN;
2818 	bb.y2 = INT_MIN;
2819 
2820 	drm_atomic_helper_damage_iter_init(&iter, old_state, state);
2821 	drm_atomic_for_each_plane_damage(&iter, &clip) {
2822 		uint32_t fb_x = clip.x1;
2823 		uint32_t fb_y = clip.y1;
2824 
2825 		vmw_du_translate_to_crtc(state, &clip);
2826 		if (update->clip) {
2827 			curr_size = update->clip(update, cmd_next, &clip, fb_x,
2828 						 fb_y);
2829 			cmd_next += curr_size;
2830 			submit_size += curr_size;
2831 		}
2832 		bb.x1 = min_t(int, bb.x1, clip.x1);
2833 		bb.y1 = min_t(int, bb.y1, clip.y1);
2834 		bb.x2 = max_t(int, bb.x2, clip.x2);
2835 		bb.y2 = max_t(int, bb.y2, clip.y2);
2836 	}
2837 
2838 	curr_size = update->post_clip(update, cmd_next, &bb);
2839 	submit_size += curr_size;
2840 
2841 	if (reserved_size < submit_size)
2842 		submit_size = 0;
2843 
2844 	vmw_cmd_commit(update->dev_priv, submit_size);
2845 
2846 	vmw_kms_helper_validation_finish(update->dev_priv, NULL, &val_ctx,
2847 					 update->out_fence, NULL);
2848 	return ret;
2849 
2850 out_revert:
2851 	vmw_validation_revert(&val_ctx);
2852 
2853 out_unref:
2854 	vmw_validation_unref_lists(&val_ctx);
2855 	return ret;
2856 }
2857