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