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 
30 
31 /* Might need a hrtimer here? */
32 #define VMWGFX_PRESENT_RATE ((HZ / 60 > 0) ? HZ / 60 : 1)
33 
34 void vmw_du_cleanup(struct vmw_display_unit *du)
35 {
36 	if (du->cursor_surface)
37 		vmw_surface_unreference(&du->cursor_surface);
38 	if (du->cursor_dmabuf)
39 		vmw_dmabuf_unreference(&du->cursor_dmabuf);
40 	drm_connector_unregister(&du->connector);
41 	drm_crtc_cleanup(&du->crtc);
42 	drm_encoder_cleanup(&du->encoder);
43 	drm_connector_cleanup(&du->connector);
44 }
45 
46 /*
47  * Display Unit Cursor functions
48  */
49 
50 int vmw_cursor_update_image(struct vmw_private *dev_priv,
51 			    u32 *image, u32 width, u32 height,
52 			    u32 hotspotX, u32 hotspotY)
53 {
54 	struct {
55 		u32 cmd;
56 		SVGAFifoCmdDefineAlphaCursor cursor;
57 	} *cmd;
58 	u32 image_size = width * height * 4;
59 	u32 cmd_size = sizeof(*cmd) + image_size;
60 
61 	if (!image)
62 		return -EINVAL;
63 
64 	cmd = vmw_fifo_reserve(dev_priv, cmd_size);
65 	if (unlikely(cmd == NULL)) {
66 		DRM_ERROR("Fifo reserve failed.\n");
67 		return -ENOMEM;
68 	}
69 
70 	memset(cmd, 0, sizeof(*cmd));
71 
72 	memcpy(&cmd[1], image, image_size);
73 
74 	cmd->cmd = SVGA_CMD_DEFINE_ALPHA_CURSOR;
75 	cmd->cursor.id = 0;
76 	cmd->cursor.width = width;
77 	cmd->cursor.height = height;
78 	cmd->cursor.hotspotX = hotspotX;
79 	cmd->cursor.hotspotY = hotspotY;
80 
81 	vmw_fifo_commit_flush(dev_priv, cmd_size);
82 
83 	return 0;
84 }
85 
86 int vmw_cursor_update_dmabuf(struct vmw_private *dev_priv,
87 			     struct vmw_dma_buffer *dmabuf,
88 			     u32 width, u32 height,
89 			     u32 hotspotX, u32 hotspotY)
90 {
91 	struct ttm_bo_kmap_obj map;
92 	unsigned long kmap_offset;
93 	unsigned long kmap_num;
94 	void *virtual;
95 	bool dummy;
96 	int ret;
97 
98 	kmap_offset = 0;
99 	kmap_num = (width*height*4 + PAGE_SIZE - 1) >> PAGE_SHIFT;
100 
101 	ret = ttm_bo_reserve(&dmabuf->base, true, false, NULL);
102 	if (unlikely(ret != 0)) {
103 		DRM_ERROR("reserve failed\n");
104 		return -EINVAL;
105 	}
106 
107 	ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
108 	if (unlikely(ret != 0))
109 		goto err_unreserve;
110 
111 	virtual = ttm_kmap_obj_virtual(&map, &dummy);
112 	ret = vmw_cursor_update_image(dev_priv, virtual, width, height,
113 				      hotspotX, hotspotY);
114 
115 	ttm_bo_kunmap(&map);
116 err_unreserve:
117 	ttm_bo_unreserve(&dmabuf->base);
118 
119 	return ret;
120 }
121 
122 
123 void vmw_cursor_update_position(struct vmw_private *dev_priv,
124 				bool show, int x, int y)
125 {
126 	u32 *fifo_mem = dev_priv->mmio_virt;
127 	uint32_t count;
128 
129 	vmw_mmio_write(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
130 	vmw_mmio_write(x, fifo_mem + SVGA_FIFO_CURSOR_X);
131 	vmw_mmio_write(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
132 	count = vmw_mmio_read(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
133 	vmw_mmio_write(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
134 }
135 
136 
137 /*
138  * vmw_du_crtc_cursor_set2 - Driver cursor_set2 callback.
139  */
140 int vmw_du_crtc_cursor_set2(struct drm_crtc *crtc, struct drm_file *file_priv,
141 			    uint32_t handle, uint32_t width, uint32_t height,
142 			    int32_t hot_x, int32_t hot_y)
143 {
144 	struct vmw_private *dev_priv = vmw_priv(crtc->dev);
145 	struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
146 	struct vmw_surface *surface = NULL;
147 	struct vmw_dma_buffer *dmabuf = NULL;
148 	s32 hotspot_x, hotspot_y;
149 	int ret;
150 
151 	/*
152 	 * FIXME: Unclear whether there's any global state touched by the
153 	 * cursor_set function, especially vmw_cursor_update_position looks
154 	 * suspicious. For now take the easy route and reacquire all locks. We
155 	 * can do this since the caller in the drm core doesn't check anything
156 	 * which is protected by any looks.
157 	 */
158 	drm_modeset_unlock_crtc(crtc);
159 	drm_modeset_lock_all(dev_priv->dev);
160 	hotspot_x = hot_x + du->hotspot_x;
161 	hotspot_y = hot_y + du->hotspot_y;
162 
163 	/* A lot of the code assumes this */
164 	if (handle && (width != 64 || height != 64)) {
165 		ret = -EINVAL;
166 		goto out;
167 	}
168 
169 	if (handle) {
170 		struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
171 
172 		ret = vmw_user_lookup_handle(dev_priv, tfile,
173 					     handle, &surface, &dmabuf);
174 		if (ret) {
175 			DRM_ERROR("failed to find surface or dmabuf: %i\n", ret);
176 			ret = -EINVAL;
177 			goto out;
178 		}
179 	}
180 
181 	/* need to do this before taking down old image */
182 	if (surface && !surface->snooper.image) {
183 		DRM_ERROR("surface not suitable for cursor\n");
184 		vmw_surface_unreference(&surface);
185 		ret = -EINVAL;
186 		goto out;
187 	}
188 
189 	/* takedown old cursor */
190 	if (du->cursor_surface) {
191 		du->cursor_surface->snooper.crtc = NULL;
192 		vmw_surface_unreference(&du->cursor_surface);
193 	}
194 	if (du->cursor_dmabuf)
195 		vmw_dmabuf_unreference(&du->cursor_dmabuf);
196 
197 	/* setup new image */
198 	ret = 0;
199 	if (surface) {
200 		/* vmw_user_surface_lookup takes one reference */
201 		du->cursor_surface = surface;
202 
203 		du->cursor_surface->snooper.crtc = crtc;
204 		du->cursor_age = du->cursor_surface->snooper.age;
205 		ret = vmw_cursor_update_image(dev_priv, surface->snooper.image,
206 					      64, 64, hotspot_x, hotspot_y);
207 	} else if (dmabuf) {
208 		/* vmw_user_surface_lookup takes one reference */
209 		du->cursor_dmabuf = dmabuf;
210 
211 		ret = vmw_cursor_update_dmabuf(dev_priv, dmabuf, width, height,
212 					       hotspot_x, hotspot_y);
213 	} else {
214 		vmw_cursor_update_position(dev_priv, false, 0, 0);
215 		goto out;
216 	}
217 
218 	if (!ret) {
219 		vmw_cursor_update_position(dev_priv, true,
220 					   du->cursor_x + hotspot_x,
221 					   du->cursor_y + hotspot_y);
222 		du->core_hotspot_x = hot_x;
223 		du->core_hotspot_y = hot_y;
224 	}
225 
226 out:
227 	drm_modeset_unlock_all(dev_priv->dev);
228 	drm_modeset_lock_crtc(crtc, crtc->cursor);
229 
230 	return ret;
231 }
232 
233 int vmw_du_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
234 {
235 	struct vmw_private *dev_priv = vmw_priv(crtc->dev);
236 	struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
237 	bool shown = du->cursor_surface || du->cursor_dmabuf ? true : false;
238 
239 	du->cursor_x = x + du->set_gui_x;
240 	du->cursor_y = y + du->set_gui_y;
241 
242 	/*
243 	 * FIXME: Unclear whether there's any global state touched by the
244 	 * cursor_set function, especially vmw_cursor_update_position looks
245 	 * suspicious. For now take the easy route and reacquire all locks. We
246 	 * can do this since the caller in the drm core doesn't check anything
247 	 * which is protected by any looks.
248 	 */
249 	drm_modeset_unlock_crtc(crtc);
250 	drm_modeset_lock_all(dev_priv->dev);
251 
252 	vmw_cursor_update_position(dev_priv, shown,
253 				   du->cursor_x + du->hotspot_x +
254 				   du->core_hotspot_x,
255 				   du->cursor_y + du->hotspot_y +
256 				   du->core_hotspot_y);
257 
258 	drm_modeset_unlock_all(dev_priv->dev);
259 	drm_modeset_lock_crtc(crtc, crtc->cursor);
260 
261 	return 0;
262 }
263 
264 void vmw_kms_cursor_snoop(struct vmw_surface *srf,
265 			  struct ttm_object_file *tfile,
266 			  struct ttm_buffer_object *bo,
267 			  SVGA3dCmdHeader *header)
268 {
269 	struct ttm_bo_kmap_obj map;
270 	unsigned long kmap_offset;
271 	unsigned long kmap_num;
272 	SVGA3dCopyBox *box;
273 	unsigned box_count;
274 	void *virtual;
275 	bool dummy;
276 	struct vmw_dma_cmd {
277 		SVGA3dCmdHeader header;
278 		SVGA3dCmdSurfaceDMA dma;
279 	} *cmd;
280 	int i, ret;
281 
282 	cmd = container_of(header, struct vmw_dma_cmd, header);
283 
284 	/* No snooper installed */
285 	if (!srf->snooper.image)
286 		return;
287 
288 	if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
289 		DRM_ERROR("face and mipmap for cursors should never != 0\n");
290 		return;
291 	}
292 
293 	if (cmd->header.size < 64) {
294 		DRM_ERROR("at least one full copy box must be given\n");
295 		return;
296 	}
297 
298 	box = (SVGA3dCopyBox *)&cmd[1];
299 	box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
300 			sizeof(SVGA3dCopyBox);
301 
302 	if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
303 	    box->x != 0    || box->y != 0    || box->z != 0    ||
304 	    box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
305 	    box->d != 1    || box_count != 1) {
306 		/* TODO handle none page aligned offsets */
307 		/* TODO handle more dst & src != 0 */
308 		/* TODO handle more then one copy */
309 		DRM_ERROR("Cant snoop dma request for cursor!\n");
310 		DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
311 			  box->srcx, box->srcy, box->srcz,
312 			  box->x, box->y, box->z,
313 			  box->w, box->h, box->d, box_count,
314 			  cmd->dma.guest.ptr.offset);
315 		return;
316 	}
317 
318 	kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
319 	kmap_num = (64*64*4) >> PAGE_SHIFT;
320 
321 	ret = ttm_bo_reserve(bo, true, false, NULL);
322 	if (unlikely(ret != 0)) {
323 		DRM_ERROR("reserve failed\n");
324 		return;
325 	}
326 
327 	ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
328 	if (unlikely(ret != 0))
329 		goto err_unreserve;
330 
331 	virtual = ttm_kmap_obj_virtual(&map, &dummy);
332 
333 	if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
334 		memcpy(srf->snooper.image, virtual, 64*64*4);
335 	} else {
336 		/* Image is unsigned pointer. */
337 		for (i = 0; i < box->h; i++)
338 			memcpy(srf->snooper.image + i * 64,
339 			       virtual + i * cmd->dma.guest.pitch,
340 			       box->w * 4);
341 	}
342 
343 	srf->snooper.age++;
344 
345 	ttm_bo_kunmap(&map);
346 err_unreserve:
347 	ttm_bo_unreserve(bo);
348 }
349 
350 /**
351  * vmw_kms_legacy_hotspot_clear - Clear legacy hotspots
352  *
353  * @dev_priv: Pointer to the device private struct.
354  *
355  * Clears all legacy hotspots.
356  */
357 void vmw_kms_legacy_hotspot_clear(struct vmw_private *dev_priv)
358 {
359 	struct drm_device *dev = dev_priv->dev;
360 	struct vmw_display_unit *du;
361 	struct drm_crtc *crtc;
362 
363 	drm_modeset_lock_all(dev);
364 	drm_for_each_crtc(crtc, dev) {
365 		du = vmw_crtc_to_du(crtc);
366 
367 		du->hotspot_x = 0;
368 		du->hotspot_y = 0;
369 	}
370 	drm_modeset_unlock_all(dev);
371 }
372 
373 void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
374 {
375 	struct drm_device *dev = dev_priv->dev;
376 	struct vmw_display_unit *du;
377 	struct drm_crtc *crtc;
378 
379 	mutex_lock(&dev->mode_config.mutex);
380 
381 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
382 		du = vmw_crtc_to_du(crtc);
383 		if (!du->cursor_surface ||
384 		    du->cursor_age == du->cursor_surface->snooper.age)
385 			continue;
386 
387 		du->cursor_age = du->cursor_surface->snooper.age;
388 		vmw_cursor_update_image(dev_priv,
389 					du->cursor_surface->snooper.image,
390 					64, 64,
391 					du->hotspot_x + du->core_hotspot_x,
392 					du->hotspot_y + du->core_hotspot_y);
393 	}
394 
395 	mutex_unlock(&dev->mode_config.mutex);
396 }
397 
398 /*
399  * Generic framebuffer code
400  */
401 
402 /*
403  * Surface framebuffer code
404  */
405 
406 static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
407 {
408 	struct vmw_framebuffer_surface *vfbs =
409 		vmw_framebuffer_to_vfbs(framebuffer);
410 
411 	drm_framebuffer_cleanup(framebuffer);
412 	vmw_surface_unreference(&vfbs->surface);
413 	if (vfbs->base.user_obj)
414 		ttm_base_object_unref(&vfbs->base.user_obj);
415 
416 	kfree(vfbs);
417 }
418 
419 static int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
420 				  struct drm_file *file_priv,
421 				  unsigned flags, unsigned color,
422 				  struct drm_clip_rect *clips,
423 				  unsigned num_clips)
424 {
425 	struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
426 	struct vmw_framebuffer_surface *vfbs =
427 		vmw_framebuffer_to_vfbs(framebuffer);
428 	struct drm_clip_rect norect;
429 	int ret, inc = 1;
430 
431 	/* Legacy Display Unit does not support 3D */
432 	if (dev_priv->active_display_unit == vmw_du_legacy)
433 		return -EINVAL;
434 
435 	drm_modeset_lock_all(dev_priv->dev);
436 
437 	ret = ttm_read_lock(&dev_priv->reservation_sem, true);
438 	if (unlikely(ret != 0)) {
439 		drm_modeset_unlock_all(dev_priv->dev);
440 		return ret;
441 	}
442 
443 	if (!num_clips) {
444 		num_clips = 1;
445 		clips = &norect;
446 		norect.x1 = norect.y1 = 0;
447 		norect.x2 = framebuffer->width;
448 		norect.y2 = framebuffer->height;
449 	} else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
450 		num_clips /= 2;
451 		inc = 2; /* skip source rects */
452 	}
453 
454 	if (dev_priv->active_display_unit == vmw_du_screen_object)
455 		ret = vmw_kms_sou_do_surface_dirty(dev_priv, &vfbs->base,
456 						   clips, NULL, NULL, 0, 0,
457 						   num_clips, inc, NULL);
458 	else
459 		ret = vmw_kms_stdu_surface_dirty(dev_priv, &vfbs->base,
460 						 clips, NULL, NULL, 0, 0,
461 						 num_clips, inc, NULL);
462 
463 	vmw_fifo_flush(dev_priv, false);
464 	ttm_read_unlock(&dev_priv->reservation_sem);
465 
466 	drm_modeset_unlock_all(dev_priv->dev);
467 
468 	return 0;
469 }
470 
471 /**
472  * vmw_kms_readback - Perform a readback from the screen system to
473  * a dma-buffer backed framebuffer.
474  *
475  * @dev_priv: Pointer to the device private structure.
476  * @file_priv: Pointer to a struct drm_file identifying the caller.
477  * Must be set to NULL if @user_fence_rep is NULL.
478  * @vfb: Pointer to the dma-buffer backed framebuffer.
479  * @user_fence_rep: User-space provided structure for fence information.
480  * Must be set to non-NULL if @file_priv is non-NULL.
481  * @vclips: Array of clip rects.
482  * @num_clips: Number of clip rects in @vclips.
483  *
484  * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
485  * interrupted.
486  */
487 int vmw_kms_readback(struct vmw_private *dev_priv,
488 		     struct drm_file *file_priv,
489 		     struct vmw_framebuffer *vfb,
490 		     struct drm_vmw_fence_rep __user *user_fence_rep,
491 		     struct drm_vmw_rect *vclips,
492 		     uint32_t num_clips)
493 {
494 	switch (dev_priv->active_display_unit) {
495 	case vmw_du_screen_object:
496 		return vmw_kms_sou_readback(dev_priv, file_priv, vfb,
497 					    user_fence_rep, vclips, num_clips);
498 	case vmw_du_screen_target:
499 		return vmw_kms_stdu_dma(dev_priv, file_priv, vfb,
500 					user_fence_rep, NULL, vclips, num_clips,
501 					1, false, true);
502 	default:
503 		WARN_ONCE(true,
504 			  "Readback called with invalid display system.\n");
505 }
506 
507 	return -ENOSYS;
508 }
509 
510 
511 static const struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
512 	.destroy = vmw_framebuffer_surface_destroy,
513 	.dirty = vmw_framebuffer_surface_dirty,
514 };
515 
516 static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
517 					   struct vmw_surface *surface,
518 					   struct vmw_framebuffer **out,
519 					   const struct drm_mode_fb_cmd2
520 					   *mode_cmd,
521 					   bool is_dmabuf_proxy)
522 
523 {
524 	struct drm_device *dev = dev_priv->dev;
525 	struct vmw_framebuffer_surface *vfbs;
526 	enum SVGA3dSurfaceFormat format;
527 	int ret;
528 	struct drm_format_name_buf format_name;
529 
530 	/* 3D is only supported on HWv8 and newer hosts */
531 	if (dev_priv->active_display_unit == vmw_du_legacy)
532 		return -ENOSYS;
533 
534 	/*
535 	 * Sanity checks.
536 	 */
537 
538 	/* Surface must be marked as a scanout. */
539 	if (unlikely(!surface->scanout))
540 		return -EINVAL;
541 
542 	if (unlikely(surface->mip_levels[0] != 1 ||
543 		     surface->num_sizes != 1 ||
544 		     surface->base_size.width < mode_cmd->width ||
545 		     surface->base_size.height < mode_cmd->height ||
546 		     surface->base_size.depth != 1)) {
547 		DRM_ERROR("Incompatible surface dimensions "
548 			  "for requested mode.\n");
549 		return -EINVAL;
550 	}
551 
552 	switch (mode_cmd->pixel_format) {
553 	case DRM_FORMAT_ARGB8888:
554 		format = SVGA3D_A8R8G8B8;
555 		break;
556 	case DRM_FORMAT_XRGB8888:
557 		format = SVGA3D_X8R8G8B8;
558 		break;
559 	case DRM_FORMAT_RGB565:
560 		format = SVGA3D_R5G6B5;
561 		break;
562 	case DRM_FORMAT_XRGB1555:
563 		format = SVGA3D_A1R5G5B5;
564 		break;
565 	default:
566 		DRM_ERROR("Invalid pixel format: %s\n",
567 			  drm_get_format_name(mode_cmd->pixel_format, &format_name));
568 		return -EINVAL;
569 	}
570 
571 	/*
572 	 * For DX, surface format validation is done when surface->scanout
573 	 * is set.
574 	 */
575 	if (!dev_priv->has_dx && format != surface->format) {
576 		DRM_ERROR("Invalid surface format for requested mode.\n");
577 		return -EINVAL;
578 	}
579 
580 	vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
581 	if (!vfbs) {
582 		ret = -ENOMEM;
583 		goto out_err1;
584 	}
585 
586 	drm_helper_mode_fill_fb_struct(dev, &vfbs->base.base, mode_cmd);
587 	vfbs->surface = vmw_surface_reference(surface);
588 	vfbs->base.user_handle = mode_cmd->handles[0];
589 	vfbs->is_dmabuf_proxy = is_dmabuf_proxy;
590 
591 	*out = &vfbs->base;
592 
593 	ret = drm_framebuffer_init(dev, &vfbs->base.base,
594 				   &vmw_framebuffer_surface_funcs);
595 	if (ret)
596 		goto out_err2;
597 
598 	return 0;
599 
600 out_err2:
601 	vmw_surface_unreference(&surface);
602 	kfree(vfbs);
603 out_err1:
604 	return ret;
605 }
606 
607 /*
608  * Dmabuf framebuffer code
609  */
610 
611 static void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
612 {
613 	struct vmw_framebuffer_dmabuf *vfbd =
614 		vmw_framebuffer_to_vfbd(framebuffer);
615 
616 	drm_framebuffer_cleanup(framebuffer);
617 	vmw_dmabuf_unreference(&vfbd->buffer);
618 	if (vfbd->base.user_obj)
619 		ttm_base_object_unref(&vfbd->base.user_obj);
620 
621 	kfree(vfbd);
622 }
623 
624 static int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
625 				 struct drm_file *file_priv,
626 				 unsigned flags, unsigned color,
627 				 struct drm_clip_rect *clips,
628 				 unsigned num_clips)
629 {
630 	struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
631 	struct vmw_framebuffer_dmabuf *vfbd =
632 		vmw_framebuffer_to_vfbd(framebuffer);
633 	struct drm_clip_rect norect;
634 	int ret, increment = 1;
635 
636 	drm_modeset_lock_all(dev_priv->dev);
637 
638 	ret = ttm_read_lock(&dev_priv->reservation_sem, true);
639 	if (unlikely(ret != 0)) {
640 		drm_modeset_unlock_all(dev_priv->dev);
641 		return ret;
642 	}
643 
644 	if (!num_clips) {
645 		num_clips = 1;
646 		clips = &norect;
647 		norect.x1 = norect.y1 = 0;
648 		norect.x2 = framebuffer->width;
649 		norect.y2 = framebuffer->height;
650 	} else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
651 		num_clips /= 2;
652 		increment = 2;
653 	}
654 
655 	switch (dev_priv->active_display_unit) {
656 	case vmw_du_screen_target:
657 		ret = vmw_kms_stdu_dma(dev_priv, NULL, &vfbd->base, NULL,
658 				       clips, NULL, num_clips, increment,
659 				       true, true);
660 		break;
661 	case vmw_du_screen_object:
662 		ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, &vfbd->base,
663 						  clips, NULL, num_clips,
664 						  increment, true, NULL);
665 		break;
666 	case vmw_du_legacy:
667 		ret = vmw_kms_ldu_do_dmabuf_dirty(dev_priv, &vfbd->base, 0, 0,
668 						  clips, num_clips, increment);
669 		break;
670 	default:
671 		ret = -EINVAL;
672 		WARN_ONCE(true, "Dirty called with invalid display system.\n");
673 		break;
674 	}
675 
676 	vmw_fifo_flush(dev_priv, false);
677 	ttm_read_unlock(&dev_priv->reservation_sem);
678 
679 	drm_modeset_unlock_all(dev_priv->dev);
680 
681 	return ret;
682 }
683 
684 static const struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
685 	.destroy = vmw_framebuffer_dmabuf_destroy,
686 	.dirty = vmw_framebuffer_dmabuf_dirty,
687 };
688 
689 /**
690  * Pin the dmabuffer to the start of vram.
691  */
692 static int vmw_framebuffer_pin(struct vmw_framebuffer *vfb)
693 {
694 	struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
695 	struct vmw_dma_buffer *buf;
696 	int ret;
697 
698 	buf = vfb->dmabuf ?  vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
699 		vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
700 
701 	if (!buf)
702 		return 0;
703 
704 	switch (dev_priv->active_display_unit) {
705 	case vmw_du_legacy:
706 		vmw_overlay_pause_all(dev_priv);
707 		ret = vmw_dmabuf_pin_in_start_of_vram(dev_priv, buf, false);
708 		vmw_overlay_resume_all(dev_priv);
709 		break;
710 	case vmw_du_screen_object:
711 	case vmw_du_screen_target:
712 		if (vfb->dmabuf)
713 			return vmw_dmabuf_pin_in_vram_or_gmr(dev_priv, buf,
714 							     false);
715 
716 		return vmw_dmabuf_pin_in_placement(dev_priv, buf,
717 						   &vmw_mob_placement, false);
718 	default:
719 		return -EINVAL;
720 	}
721 
722 	return ret;
723 }
724 
725 static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb)
726 {
727 	struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
728 	struct vmw_dma_buffer *buf;
729 
730 	buf = vfb->dmabuf ?  vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
731 		vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
732 
733 	if (WARN_ON(!buf))
734 		return 0;
735 
736 	return vmw_dmabuf_unpin(dev_priv, buf, false);
737 }
738 
739 /**
740  * vmw_create_dmabuf_proxy - create a proxy surface for the DMA buf
741  *
742  * @dev: DRM device
743  * @mode_cmd: parameters for the new surface
744  * @dmabuf_mob: MOB backing the DMA buf
745  * @srf_out: newly created surface
746  *
747  * When the content FB is a DMA buf, we create a surface as a proxy to the
748  * same buffer.  This way we can do a surface copy rather than a surface DMA.
749  * This is a more efficient approach
750  *
751  * RETURNS:
752  * 0 on success, error code otherwise
753  */
754 static int vmw_create_dmabuf_proxy(struct drm_device *dev,
755 				   const struct drm_mode_fb_cmd2 *mode_cmd,
756 				   struct vmw_dma_buffer *dmabuf_mob,
757 				   struct vmw_surface **srf_out)
758 {
759 	uint32_t format;
760 	struct drm_vmw_size content_base_size = {0};
761 	struct vmw_resource *res;
762 	unsigned int bytes_pp;
763 	struct drm_format_name_buf format_name;
764 	int ret;
765 
766 	switch (mode_cmd->pixel_format) {
767 	case DRM_FORMAT_ARGB8888:
768 	case DRM_FORMAT_XRGB8888:
769 		format = SVGA3D_X8R8G8B8;
770 		bytes_pp = 4;
771 		break;
772 
773 	case DRM_FORMAT_RGB565:
774 	case DRM_FORMAT_XRGB1555:
775 		format = SVGA3D_R5G6B5;
776 		bytes_pp = 2;
777 		break;
778 
779 	case 8:
780 		format = SVGA3D_P8;
781 		bytes_pp = 1;
782 		break;
783 
784 	default:
785 		DRM_ERROR("Invalid framebuffer format %s\n",
786 			  drm_get_format_name(mode_cmd->pixel_format, &format_name));
787 		return -EINVAL;
788 	}
789 
790 	content_base_size.width  = mode_cmd->pitches[0] / bytes_pp;
791 	content_base_size.height = mode_cmd->height;
792 	content_base_size.depth  = 1;
793 
794 	ret = vmw_surface_gb_priv_define(dev,
795 			0, /* kernel visible only */
796 			0, /* flags */
797 			format,
798 			true, /* can be a scanout buffer */
799 			1, /* num of mip levels */
800 			0,
801 			0,
802 			content_base_size,
803 			srf_out);
804 	if (ret) {
805 		DRM_ERROR("Failed to allocate proxy content buffer\n");
806 		return ret;
807 	}
808 
809 	res = &(*srf_out)->res;
810 
811 	/* Reserve and switch the backing mob. */
812 	mutex_lock(&res->dev_priv->cmdbuf_mutex);
813 	(void) vmw_resource_reserve(res, false, true);
814 	vmw_dmabuf_unreference(&res->backup);
815 	res->backup = vmw_dmabuf_reference(dmabuf_mob);
816 	res->backup_offset = 0;
817 	vmw_resource_unreserve(res, false, NULL, 0);
818 	mutex_unlock(&res->dev_priv->cmdbuf_mutex);
819 
820 	return 0;
821 }
822 
823 
824 
825 static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
826 					  struct vmw_dma_buffer *dmabuf,
827 					  struct vmw_framebuffer **out,
828 					  const struct drm_mode_fb_cmd2
829 					  *mode_cmd)
830 
831 {
832 	struct drm_device *dev = dev_priv->dev;
833 	struct vmw_framebuffer_dmabuf *vfbd;
834 	unsigned int requested_size;
835 	struct drm_format_name_buf format_name;
836 	int ret;
837 
838 	requested_size = mode_cmd->height * mode_cmd->pitches[0];
839 	if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
840 		DRM_ERROR("Screen buffer object size is too small "
841 			  "for requested mode.\n");
842 		return -EINVAL;
843 	}
844 
845 	/* Limited framebuffer color depth support for screen objects */
846 	if (dev_priv->active_display_unit == vmw_du_screen_object) {
847 		switch (mode_cmd->pixel_format) {
848 		case DRM_FORMAT_XRGB8888:
849 		case DRM_FORMAT_ARGB8888:
850 			break;
851 		case DRM_FORMAT_XRGB1555:
852 		case DRM_FORMAT_RGB565:
853 			break;
854 		default:
855 			DRM_ERROR("Invalid pixel format: %s\n",
856 				  drm_get_format_name(mode_cmd->pixel_format, &format_name));
857 			return -EINVAL;
858 		}
859 	}
860 
861 	vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
862 	if (!vfbd) {
863 		ret = -ENOMEM;
864 		goto out_err1;
865 	}
866 
867 	drm_helper_mode_fill_fb_struct(dev, &vfbd->base.base, mode_cmd);
868 	vfbd->base.dmabuf = true;
869 	vfbd->buffer = vmw_dmabuf_reference(dmabuf);
870 	vfbd->base.user_handle = mode_cmd->handles[0];
871 	*out = &vfbd->base;
872 
873 	ret = drm_framebuffer_init(dev, &vfbd->base.base,
874 				   &vmw_framebuffer_dmabuf_funcs);
875 	if (ret)
876 		goto out_err2;
877 
878 	return 0;
879 
880 out_err2:
881 	vmw_dmabuf_unreference(&dmabuf);
882 	kfree(vfbd);
883 out_err1:
884 	return ret;
885 }
886 
887 /**
888  * vmw_kms_new_framebuffer - Create a new framebuffer.
889  *
890  * @dev_priv: Pointer to device private struct.
891  * @dmabuf: Pointer to dma buffer to wrap the kms framebuffer around.
892  * Either @dmabuf or @surface must be NULL.
893  * @surface: Pointer to a surface to wrap the kms framebuffer around.
894  * Either @dmabuf or @surface must be NULL.
895  * @only_2d: No presents will occur to this dma buffer based framebuffer. This
896  * Helps the code to do some important optimizations.
897  * @mode_cmd: Frame-buffer metadata.
898  */
899 struct vmw_framebuffer *
900 vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
901 			struct vmw_dma_buffer *dmabuf,
902 			struct vmw_surface *surface,
903 			bool only_2d,
904 			const struct drm_mode_fb_cmd2 *mode_cmd)
905 {
906 	struct vmw_framebuffer *vfb = NULL;
907 	bool is_dmabuf_proxy = false;
908 	int ret;
909 
910 	/*
911 	 * We cannot use the SurfaceDMA command in an non-accelerated VM,
912 	 * therefore, wrap the DMA buf in a surface so we can use the
913 	 * SurfaceCopy command.
914 	 */
915 	if (dmabuf && only_2d &&
916 	    dev_priv->active_display_unit == vmw_du_screen_target) {
917 		ret = vmw_create_dmabuf_proxy(dev_priv->dev, mode_cmd,
918 					      dmabuf, &surface);
919 		if (ret)
920 			return ERR_PTR(ret);
921 
922 		is_dmabuf_proxy = true;
923 	}
924 
925 	/* Create the new framebuffer depending one what we have */
926 	if (surface) {
927 		ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb,
928 						      mode_cmd,
929 						      is_dmabuf_proxy);
930 
931 		/*
932 		 * vmw_create_dmabuf_proxy() adds a reference that is no longer
933 		 * needed
934 		 */
935 		if (is_dmabuf_proxy)
936 			vmw_surface_unreference(&surface);
937 	} else if (dmabuf) {
938 		ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, dmabuf, &vfb,
939 						     mode_cmd);
940 	} else {
941 		BUG();
942 	}
943 
944 	if (ret)
945 		return ERR_PTR(ret);
946 
947 	vfb->pin = vmw_framebuffer_pin;
948 	vfb->unpin = vmw_framebuffer_unpin;
949 
950 	return vfb;
951 }
952 
953 /*
954  * Generic Kernel modesetting functions
955  */
956 
957 static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
958 						 struct drm_file *file_priv,
959 						 const struct drm_mode_fb_cmd2 *mode_cmd)
960 {
961 	struct vmw_private *dev_priv = vmw_priv(dev);
962 	struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
963 	struct vmw_framebuffer *vfb = NULL;
964 	struct vmw_surface *surface = NULL;
965 	struct vmw_dma_buffer *bo = NULL;
966 	struct ttm_base_object *user_obj;
967 	int ret;
968 
969 	/**
970 	 * This code should be conditioned on Screen Objects not being used.
971 	 * If screen objects are used, we can allocate a GMR to hold the
972 	 * requested framebuffer.
973 	 */
974 
975 	if (!vmw_kms_validate_mode_vram(dev_priv,
976 					mode_cmd->pitches[0],
977 					mode_cmd->height)) {
978 		DRM_ERROR("Requested mode exceed bounding box limit.\n");
979 		return ERR_PTR(-ENOMEM);
980 	}
981 
982 	/*
983 	 * Take a reference on the user object of the resource
984 	 * backing the kms fb. This ensures that user-space handle
985 	 * lookups on that resource will always work as long as
986 	 * it's registered with a kms framebuffer. This is important,
987 	 * since vmw_execbuf_process identifies resources in the
988 	 * command stream using user-space handles.
989 	 */
990 
991 	user_obj = ttm_base_object_lookup(tfile, mode_cmd->handles[0]);
992 	if (unlikely(user_obj == NULL)) {
993 		DRM_ERROR("Could not locate requested kms frame buffer.\n");
994 		return ERR_PTR(-ENOENT);
995 	}
996 
997 	/**
998 	 * End conditioned code.
999 	 */
1000 
1001 	/* returns either a dmabuf or surface */
1002 	ret = vmw_user_lookup_handle(dev_priv, tfile,
1003 				     mode_cmd->handles[0],
1004 				     &surface, &bo);
1005 	if (ret)
1006 		goto err_out;
1007 
1008 	vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface,
1009 				      !(dev_priv->capabilities & SVGA_CAP_3D),
1010 				      mode_cmd);
1011 	if (IS_ERR(vfb)) {
1012 		ret = PTR_ERR(vfb);
1013 		goto err_out;
1014  	}
1015 
1016 err_out:
1017 	/* vmw_user_lookup_handle takes one ref so does new_fb */
1018 	if (bo)
1019 		vmw_dmabuf_unreference(&bo);
1020 	if (surface)
1021 		vmw_surface_unreference(&surface);
1022 
1023 	if (ret) {
1024 		DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
1025 		ttm_base_object_unref(&user_obj);
1026 		return ERR_PTR(ret);
1027 	} else
1028 		vfb->user_obj = user_obj;
1029 
1030 	return &vfb->base;
1031 }
1032 
1033 static const struct drm_mode_config_funcs vmw_kms_funcs = {
1034 	.fb_create = vmw_kms_fb_create,
1035 };
1036 
1037 static int vmw_kms_generic_present(struct vmw_private *dev_priv,
1038 				   struct drm_file *file_priv,
1039 				   struct vmw_framebuffer *vfb,
1040 				   struct vmw_surface *surface,
1041 				   uint32_t sid,
1042 				   int32_t destX, int32_t destY,
1043 				   struct drm_vmw_rect *clips,
1044 				   uint32_t num_clips)
1045 {
1046 	return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
1047 					    &surface->res, destX, destY,
1048 					    num_clips, 1, NULL);
1049 }
1050 
1051 
1052 int vmw_kms_present(struct vmw_private *dev_priv,
1053 		    struct drm_file *file_priv,
1054 		    struct vmw_framebuffer *vfb,
1055 		    struct vmw_surface *surface,
1056 		    uint32_t sid,
1057 		    int32_t destX, int32_t destY,
1058 		    struct drm_vmw_rect *clips,
1059 		    uint32_t num_clips)
1060 {
1061 	int ret;
1062 
1063 	switch (dev_priv->active_display_unit) {
1064 	case vmw_du_screen_target:
1065 		ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
1066 						 &surface->res, destX, destY,
1067 						 num_clips, 1, NULL);
1068 		break;
1069 	case vmw_du_screen_object:
1070 		ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
1071 					      sid, destX, destY, clips,
1072 					      num_clips);
1073 		break;
1074 	default:
1075 		WARN_ONCE(true,
1076 			  "Present called with invalid display system.\n");
1077 		ret = -ENOSYS;
1078 		break;
1079 	}
1080 	if (ret)
1081 		return ret;
1082 
1083 	vmw_fifo_flush(dev_priv, false);
1084 
1085 	return 0;
1086 }
1087 
1088 static void
1089 vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
1090 {
1091 	if (dev_priv->hotplug_mode_update_property)
1092 		return;
1093 
1094 	dev_priv->hotplug_mode_update_property =
1095 		drm_property_create_range(dev_priv->dev,
1096 					  DRM_MODE_PROP_IMMUTABLE,
1097 					  "hotplug_mode_update", 0, 1);
1098 
1099 	if (!dev_priv->hotplug_mode_update_property)
1100 		return;
1101 
1102 }
1103 
1104 int vmw_kms_init(struct vmw_private *dev_priv)
1105 {
1106 	struct drm_device *dev = dev_priv->dev;
1107 	int ret;
1108 
1109 	drm_mode_config_init(dev);
1110 	dev->mode_config.funcs = &vmw_kms_funcs;
1111 	dev->mode_config.min_width = 1;
1112 	dev->mode_config.min_height = 1;
1113 	dev->mode_config.max_width = dev_priv->texture_max_width;
1114 	dev->mode_config.max_height = dev_priv->texture_max_height;
1115 
1116 	drm_mode_create_suggested_offset_properties(dev);
1117 	vmw_kms_create_hotplug_mode_update_property(dev_priv);
1118 
1119 	ret = vmw_kms_stdu_init_display(dev_priv);
1120 	if (ret) {
1121 		ret = vmw_kms_sou_init_display(dev_priv);
1122 		if (ret) /* Fallback */
1123 			ret = vmw_kms_ldu_init_display(dev_priv);
1124 	}
1125 
1126 	return ret;
1127 }
1128 
1129 int vmw_kms_close(struct vmw_private *dev_priv)
1130 {
1131 	int ret;
1132 
1133 	/*
1134 	 * Docs says we should take the lock before calling this function
1135 	 * but since it destroys encoders and our destructor calls
1136 	 * drm_encoder_cleanup which takes the lock we deadlock.
1137 	 */
1138 	drm_mode_config_cleanup(dev_priv->dev);
1139 	if (dev_priv->active_display_unit == vmw_du_screen_object)
1140 		ret = vmw_kms_sou_close_display(dev_priv);
1141 	else if (dev_priv->active_display_unit == vmw_du_screen_target)
1142 		ret = vmw_kms_stdu_close_display(dev_priv);
1143 	else
1144 		ret = vmw_kms_ldu_close_display(dev_priv);
1145 
1146 	return ret;
1147 }
1148 
1149 int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1150 				struct drm_file *file_priv)
1151 {
1152 	struct drm_vmw_cursor_bypass_arg *arg = data;
1153 	struct vmw_display_unit *du;
1154 	struct drm_crtc *crtc;
1155 	int ret = 0;
1156 
1157 
1158 	mutex_lock(&dev->mode_config.mutex);
1159 	if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1160 
1161 		list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1162 			du = vmw_crtc_to_du(crtc);
1163 			du->hotspot_x = arg->xhot;
1164 			du->hotspot_y = arg->yhot;
1165 		}
1166 
1167 		mutex_unlock(&dev->mode_config.mutex);
1168 		return 0;
1169 	}
1170 
1171 	crtc = drm_crtc_find(dev, arg->crtc_id);
1172 	if (!crtc) {
1173 		ret = -ENOENT;
1174 		goto out;
1175 	}
1176 
1177 	du = vmw_crtc_to_du(crtc);
1178 
1179 	du->hotspot_x = arg->xhot;
1180 	du->hotspot_y = arg->yhot;
1181 
1182 out:
1183 	mutex_unlock(&dev->mode_config.mutex);
1184 
1185 	return ret;
1186 }
1187 
1188 int vmw_kms_write_svga(struct vmw_private *vmw_priv,
1189 			unsigned width, unsigned height, unsigned pitch,
1190 			unsigned bpp, unsigned depth)
1191 {
1192 	if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1193 		vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1194 	else if (vmw_fifo_have_pitchlock(vmw_priv))
1195 		vmw_mmio_write(pitch, vmw_priv->mmio_virt +
1196 			       SVGA_FIFO_PITCHLOCK);
1197 	vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1198 	vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
1199 	vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
1200 
1201 	if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1202 		DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1203 			  depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1204 		return -EINVAL;
1205 	}
1206 
1207 	return 0;
1208 }
1209 
1210 int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1211 {
1212 	struct vmw_vga_topology_state *save;
1213 	uint32_t i;
1214 
1215 	vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1216 	vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
1217 	vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
1218 	if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1219 		vmw_priv->vga_pitchlock =
1220 		  vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
1221 	else if (vmw_fifo_have_pitchlock(vmw_priv))
1222 		vmw_priv->vga_pitchlock = vmw_mmio_read(vmw_priv->mmio_virt +
1223 							SVGA_FIFO_PITCHLOCK);
1224 
1225 	if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1226 		return 0;
1227 
1228 	vmw_priv->num_displays = vmw_read(vmw_priv,
1229 					  SVGA_REG_NUM_GUEST_DISPLAYS);
1230 
1231 	if (vmw_priv->num_displays == 0)
1232 		vmw_priv->num_displays = 1;
1233 
1234 	for (i = 0; i < vmw_priv->num_displays; ++i) {
1235 		save = &vmw_priv->vga_save[i];
1236 		vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1237 		save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1238 		save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1239 		save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1240 		save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1241 		save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1242 		vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1243 		if (i == 0 && vmw_priv->num_displays == 1 &&
1244 		    save->width == 0 && save->height == 0) {
1245 
1246 			/*
1247 			 * It should be fairly safe to assume that these
1248 			 * values are uninitialized.
1249 			 */
1250 
1251 			save->width = vmw_priv->vga_width - save->pos_x;
1252 			save->height = vmw_priv->vga_height - save->pos_y;
1253 		}
1254 	}
1255 
1256 	return 0;
1257 }
1258 
1259 int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1260 {
1261 	struct vmw_vga_topology_state *save;
1262 	uint32_t i;
1263 
1264 	vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1265 	vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
1266 	vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
1267 	if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1268 		vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1269 			  vmw_priv->vga_pitchlock);
1270 	else if (vmw_fifo_have_pitchlock(vmw_priv))
1271 		vmw_mmio_write(vmw_priv->vga_pitchlock,
1272 			       vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
1273 
1274 	if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1275 		return 0;
1276 
1277 	for (i = 0; i < vmw_priv->num_displays; ++i) {
1278 		save = &vmw_priv->vga_save[i];
1279 		vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1280 		vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1281 		vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1282 		vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1283 		vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1284 		vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1285 		vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1286 	}
1287 
1288 	return 0;
1289 }
1290 
1291 bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1292 				uint32_t pitch,
1293 				uint32_t height)
1294 {
1295 	return ((u64) pitch * (u64) height) < (u64)
1296 		((dev_priv->active_display_unit == vmw_du_screen_target) ?
1297 		 dev_priv->prim_bb_mem : dev_priv->vram_size);
1298 }
1299 
1300 
1301 /**
1302  * Function called by DRM code called with vbl_lock held.
1303  */
1304 u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
1305 {
1306 	return 0;
1307 }
1308 
1309 /**
1310  * Function called by DRM code called with vbl_lock held.
1311  */
1312 int vmw_enable_vblank(struct drm_device *dev, unsigned int pipe)
1313 {
1314 	return -ENOSYS;
1315 }
1316 
1317 /**
1318  * Function called by DRM code called with vbl_lock held.
1319  */
1320 void vmw_disable_vblank(struct drm_device *dev, unsigned int pipe)
1321 {
1322 }
1323 
1324 
1325 /*
1326  * Small shared kms functions.
1327  */
1328 
1329 static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
1330 			 struct drm_vmw_rect *rects)
1331 {
1332 	struct drm_device *dev = dev_priv->dev;
1333 	struct vmw_display_unit *du;
1334 	struct drm_connector *con;
1335 
1336 	mutex_lock(&dev->mode_config.mutex);
1337 
1338 #if 0
1339 	{
1340 		unsigned int i;
1341 
1342 		DRM_INFO("%s: new layout ", __func__);
1343 		for (i = 0; i < num; i++)
1344 			DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1345 				 rects[i].w, rects[i].h);
1346 		DRM_INFO("\n");
1347 	}
1348 #endif
1349 
1350 	list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1351 		du = vmw_connector_to_du(con);
1352 		if (num > du->unit) {
1353 			du->pref_width = rects[du->unit].w;
1354 			du->pref_height = rects[du->unit].h;
1355 			du->pref_active = true;
1356 			du->gui_x = rects[du->unit].x;
1357 			du->gui_y = rects[du->unit].y;
1358 			drm_object_property_set_value
1359 			  (&con->base, dev->mode_config.suggested_x_property,
1360 			   du->gui_x);
1361 			drm_object_property_set_value
1362 			  (&con->base, dev->mode_config.suggested_y_property,
1363 			   du->gui_y);
1364 		} else {
1365 			du->pref_width = 800;
1366 			du->pref_height = 600;
1367 			du->pref_active = false;
1368 			drm_object_property_set_value
1369 			  (&con->base, dev->mode_config.suggested_x_property,
1370 			   0);
1371 			drm_object_property_set_value
1372 			  (&con->base, dev->mode_config.suggested_y_property,
1373 			   0);
1374 		}
1375 		con->status = vmw_du_connector_detect(con, true);
1376 	}
1377 
1378 	mutex_unlock(&dev->mode_config.mutex);
1379 	drm_sysfs_hotplug_event(dev);
1380 
1381 	return 0;
1382 }
1383 
1384 int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1385 			  u16 *r, u16 *g, u16 *b,
1386 			  uint32_t size)
1387 {
1388 	struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1389 	int i;
1390 
1391 	for (i = 0; i < size; i++) {
1392 		DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1393 			  r[i], g[i], b[i]);
1394 		vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1395 		vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1396 		vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1397 	}
1398 
1399 	return 0;
1400 }
1401 
1402 int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
1403 {
1404 	return 0;
1405 }
1406 
1407 enum drm_connector_status
1408 vmw_du_connector_detect(struct drm_connector *connector, bool force)
1409 {
1410 	uint32_t num_displays;
1411 	struct drm_device *dev = connector->dev;
1412 	struct vmw_private *dev_priv = vmw_priv(dev);
1413 	struct vmw_display_unit *du = vmw_connector_to_du(connector);
1414 
1415 	num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
1416 
1417 	return ((vmw_connector_to_du(connector)->unit < num_displays &&
1418 		 du->pref_active) ?
1419 		connector_status_connected : connector_status_disconnected);
1420 }
1421 
1422 static struct drm_display_mode vmw_kms_connector_builtin[] = {
1423 	/* 640x480@60Hz */
1424 	{ DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1425 		   752, 800, 0, 480, 489, 492, 525, 0,
1426 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1427 	/* 800x600@60Hz */
1428 	{ DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1429 		   968, 1056, 0, 600, 601, 605, 628, 0,
1430 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1431 	/* 1024x768@60Hz */
1432 	{ DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1433 		   1184, 1344, 0, 768, 771, 777, 806, 0,
1434 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1435 	/* 1152x864@75Hz */
1436 	{ DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1437 		   1344, 1600, 0, 864, 865, 868, 900, 0,
1438 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1439 	/* 1280x768@60Hz */
1440 	{ DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1441 		   1472, 1664, 0, 768, 771, 778, 798, 0,
1442 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1443 	/* 1280x800@60Hz */
1444 	{ DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1445 		   1480, 1680, 0, 800, 803, 809, 831, 0,
1446 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
1447 	/* 1280x960@60Hz */
1448 	{ DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
1449 		   1488, 1800, 0, 960, 961, 964, 1000, 0,
1450 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1451 	/* 1280x1024@60Hz */
1452 	{ DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
1453 		   1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1454 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1455 	/* 1360x768@60Hz */
1456 	{ DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
1457 		   1536, 1792, 0, 768, 771, 777, 795, 0,
1458 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1459 	/* 1440x1050@60Hz */
1460 	{ DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
1461 		   1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
1462 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1463 	/* 1440x900@60Hz */
1464 	{ DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
1465 		   1672, 1904, 0, 900, 903, 909, 934, 0,
1466 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1467 	/* 1600x1200@60Hz */
1468 	{ DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
1469 		   1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
1470 		   DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1471 	/* 1680x1050@60Hz */
1472 	{ DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
1473 		   1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
1474 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1475 	/* 1792x1344@60Hz */
1476 	{ DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
1477 		   2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
1478 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1479 	/* 1853x1392@60Hz */
1480 	{ DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
1481 		   2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
1482 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1483 	/* 1920x1200@60Hz */
1484 	{ DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
1485 		   2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
1486 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1487 	/* 1920x1440@60Hz */
1488 	{ DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
1489 		   2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
1490 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1491 	/* 2560x1600@60Hz */
1492 	{ DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
1493 		   3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
1494 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1495 	/* Terminate */
1496 	{ DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
1497 };
1498 
1499 /**
1500  * vmw_guess_mode_timing - Provide fake timings for a
1501  * 60Hz vrefresh mode.
1502  *
1503  * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
1504  * members filled in.
1505  */
1506 void vmw_guess_mode_timing(struct drm_display_mode *mode)
1507 {
1508 	mode->hsync_start = mode->hdisplay + 50;
1509 	mode->hsync_end = mode->hsync_start + 50;
1510 	mode->htotal = mode->hsync_end + 50;
1511 
1512 	mode->vsync_start = mode->vdisplay + 50;
1513 	mode->vsync_end = mode->vsync_start + 50;
1514 	mode->vtotal = mode->vsync_end + 50;
1515 
1516 	mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
1517 	mode->vrefresh = drm_mode_vrefresh(mode);
1518 }
1519 
1520 
1521 int vmw_du_connector_fill_modes(struct drm_connector *connector,
1522 				uint32_t max_width, uint32_t max_height)
1523 {
1524 	struct vmw_display_unit *du = vmw_connector_to_du(connector);
1525 	struct drm_device *dev = connector->dev;
1526 	struct vmw_private *dev_priv = vmw_priv(dev);
1527 	struct drm_display_mode *mode = NULL;
1528 	struct drm_display_mode *bmode;
1529 	struct drm_display_mode prefmode = { DRM_MODE("preferred",
1530 		DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
1531 		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1532 		DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
1533 	};
1534 	int i;
1535 	u32 assumed_bpp = 4;
1536 
1537 	if (dev_priv->assume_16bpp)
1538 		assumed_bpp = 2;
1539 
1540 	if (dev_priv->active_display_unit == vmw_du_screen_target) {
1541 		max_width  = min(max_width,  dev_priv->stdu_max_width);
1542 		max_height = min(max_height, dev_priv->stdu_max_height);
1543 	}
1544 
1545 	/* Add preferred mode */
1546 	mode = drm_mode_duplicate(dev, &prefmode);
1547 	if (!mode)
1548 		return 0;
1549 	mode->hdisplay = du->pref_width;
1550 	mode->vdisplay = du->pref_height;
1551 	vmw_guess_mode_timing(mode);
1552 
1553 	if (vmw_kms_validate_mode_vram(dev_priv,
1554 					mode->hdisplay * assumed_bpp,
1555 					mode->vdisplay)) {
1556 		drm_mode_probed_add(connector, mode);
1557 	} else {
1558 		drm_mode_destroy(dev, mode);
1559 		mode = NULL;
1560 	}
1561 
1562 	if (du->pref_mode) {
1563 		list_del_init(&du->pref_mode->head);
1564 		drm_mode_destroy(dev, du->pref_mode);
1565 	}
1566 
1567 	/* mode might be null here, this is intended */
1568 	du->pref_mode = mode;
1569 
1570 	for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
1571 		bmode = &vmw_kms_connector_builtin[i];
1572 		if (bmode->hdisplay > max_width ||
1573 		    bmode->vdisplay > max_height)
1574 			continue;
1575 
1576 		if (!vmw_kms_validate_mode_vram(dev_priv,
1577 						bmode->hdisplay * assumed_bpp,
1578 						bmode->vdisplay))
1579 			continue;
1580 
1581 		mode = drm_mode_duplicate(dev, bmode);
1582 		if (!mode)
1583 			return 0;
1584 		mode->vrefresh = drm_mode_vrefresh(mode);
1585 
1586 		drm_mode_probed_add(connector, mode);
1587 	}
1588 
1589 	drm_mode_connector_list_update(connector);
1590 	/* Move the prefered mode first, help apps pick the right mode. */
1591 	drm_mode_sort(&connector->modes);
1592 
1593 	return 1;
1594 }
1595 
1596 int vmw_du_connector_set_property(struct drm_connector *connector,
1597 				  struct drm_property *property,
1598 				  uint64_t val)
1599 {
1600 	struct vmw_display_unit *du = vmw_connector_to_du(connector);
1601 	struct vmw_private *dev_priv = vmw_priv(connector->dev);
1602 
1603 	if (property == dev_priv->implicit_placement_property)
1604 		du->is_implicit = val;
1605 
1606 	return 0;
1607 }
1608 
1609 
1610 int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
1611 				struct drm_file *file_priv)
1612 {
1613 	struct vmw_private *dev_priv = vmw_priv(dev);
1614 	struct drm_vmw_update_layout_arg *arg =
1615 		(struct drm_vmw_update_layout_arg *)data;
1616 	void __user *user_rects;
1617 	struct drm_vmw_rect *rects;
1618 	unsigned rects_size;
1619 	int ret;
1620 	int i;
1621 	u64 total_pixels = 0;
1622 	struct drm_mode_config *mode_config = &dev->mode_config;
1623 	struct drm_vmw_rect bounding_box = {0};
1624 
1625 	if (!arg->num_outputs) {
1626 		struct drm_vmw_rect def_rect = {0, 0, 800, 600};
1627 		vmw_du_update_layout(dev_priv, 1, &def_rect);
1628 		return 0;
1629 	}
1630 
1631 	rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
1632 	rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
1633 			GFP_KERNEL);
1634 	if (unlikely(!rects))
1635 		return -ENOMEM;
1636 
1637 	user_rects = (void __user *)(unsigned long)arg->rects;
1638 	ret = copy_from_user(rects, user_rects, rects_size);
1639 	if (unlikely(ret != 0)) {
1640 		DRM_ERROR("Failed to get rects.\n");
1641 		ret = -EFAULT;
1642 		goto out_free;
1643 	}
1644 
1645 	for (i = 0; i < arg->num_outputs; ++i) {
1646 		if (rects[i].x < 0 ||
1647 		    rects[i].y < 0 ||
1648 		    rects[i].x + rects[i].w > mode_config->max_width ||
1649 		    rects[i].y + rects[i].h > mode_config->max_height) {
1650 			DRM_ERROR("Invalid GUI layout.\n");
1651 			ret = -EINVAL;
1652 			goto out_free;
1653 		}
1654 
1655 		/*
1656 		 * bounding_box.w and bunding_box.h are used as
1657 		 * lower-right coordinates
1658 		 */
1659 		if (rects[i].x + rects[i].w > bounding_box.w)
1660 			bounding_box.w = rects[i].x + rects[i].w;
1661 
1662 		if (rects[i].y + rects[i].h > bounding_box.h)
1663 			bounding_box.h = rects[i].y + rects[i].h;
1664 
1665 		total_pixels += (u64) rects[i].w * (u64) rects[i].h;
1666 	}
1667 
1668 	if (dev_priv->active_display_unit == vmw_du_screen_target) {
1669 		/*
1670 		 * For Screen Targets, the limits for a toplogy are:
1671 		 *	1. Bounding box (assuming 32bpp) must be < prim_bb_mem
1672 		 *      2. Total pixels (assuming 32bpp) must be < prim_bb_mem
1673 		 */
1674 		u64 bb_mem    = (u64) bounding_box.w * bounding_box.h * 4;
1675 		u64 pixel_mem = total_pixels * 4;
1676 
1677 		if (bb_mem > dev_priv->prim_bb_mem) {
1678 			DRM_ERROR("Topology is beyond supported limits.\n");
1679 			ret = -EINVAL;
1680 			goto out_free;
1681 		}
1682 
1683 		if (pixel_mem > dev_priv->prim_bb_mem) {
1684 			DRM_ERROR("Combined output size too large\n");
1685 			ret = -EINVAL;
1686 			goto out_free;
1687 		}
1688 	}
1689 
1690 	vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
1691 
1692 out_free:
1693 	kfree(rects);
1694 	return ret;
1695 }
1696 
1697 /**
1698  * vmw_kms_helper_dirty - Helper to build commands and perform actions based
1699  * on a set of cliprects and a set of display units.
1700  *
1701  * @dev_priv: Pointer to a device private structure.
1702  * @framebuffer: Pointer to the framebuffer on which to perform the actions.
1703  * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
1704  * Cliprects are given in framebuffer coordinates.
1705  * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
1706  * be NULL. Cliprects are given in source coordinates.
1707  * @dest_x: X coordinate offset for the crtc / destination clip rects.
1708  * @dest_y: Y coordinate offset for the crtc / destination clip rects.
1709  * @num_clips: Number of cliprects in the @clips or @vclips array.
1710  * @increment: Integer with which to increment the clip counter when looping.
1711  * Used to skip a predetermined number of clip rects.
1712  * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
1713  */
1714 int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
1715 			 struct vmw_framebuffer *framebuffer,
1716 			 const struct drm_clip_rect *clips,
1717 			 const struct drm_vmw_rect *vclips,
1718 			 s32 dest_x, s32 dest_y,
1719 			 int num_clips,
1720 			 int increment,
1721 			 struct vmw_kms_dirty *dirty)
1722 {
1723 	struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1724 	struct drm_crtc *crtc;
1725 	u32 num_units = 0;
1726 	u32 i, k;
1727 
1728 	dirty->dev_priv = dev_priv;
1729 
1730 	list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1731 		if (crtc->primary->fb != &framebuffer->base)
1732 			continue;
1733 		units[num_units++] = vmw_crtc_to_du(crtc);
1734 	}
1735 
1736 	for (k = 0; k < num_units; k++) {
1737 		struct vmw_display_unit *unit = units[k];
1738 		s32 crtc_x = unit->crtc.x;
1739 		s32 crtc_y = unit->crtc.y;
1740 		s32 crtc_width = unit->crtc.mode.hdisplay;
1741 		s32 crtc_height = unit->crtc.mode.vdisplay;
1742 		const struct drm_clip_rect *clips_ptr = clips;
1743 		const struct drm_vmw_rect *vclips_ptr = vclips;
1744 
1745 		dirty->unit = unit;
1746 		if (dirty->fifo_reserve_size > 0) {
1747 			dirty->cmd = vmw_fifo_reserve(dev_priv,
1748 						      dirty->fifo_reserve_size);
1749 			if (!dirty->cmd) {
1750 				DRM_ERROR("Couldn't reserve fifo space "
1751 					  "for dirty blits.\n");
1752 				return -ENOMEM;
1753 			}
1754 			memset(dirty->cmd, 0, dirty->fifo_reserve_size);
1755 		}
1756 		dirty->num_hits = 0;
1757 		for (i = 0; i < num_clips; i++, clips_ptr += increment,
1758 		       vclips_ptr += increment) {
1759 			s32 clip_left;
1760 			s32 clip_top;
1761 
1762 			/*
1763 			 * Select clip array type. Note that integer type
1764 			 * in @clips is unsigned short, whereas in @vclips
1765 			 * it's 32-bit.
1766 			 */
1767 			if (clips) {
1768 				dirty->fb_x = (s32) clips_ptr->x1;
1769 				dirty->fb_y = (s32) clips_ptr->y1;
1770 				dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
1771 					crtc_x;
1772 				dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
1773 					crtc_y;
1774 			} else {
1775 				dirty->fb_x = vclips_ptr->x;
1776 				dirty->fb_y = vclips_ptr->y;
1777 				dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
1778 					dest_x - crtc_x;
1779 				dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
1780 					dest_y - crtc_y;
1781 			}
1782 
1783 			dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
1784 			dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
1785 
1786 			/* Skip this clip if it's outside the crtc region */
1787 			if (dirty->unit_x1 >= crtc_width ||
1788 			    dirty->unit_y1 >= crtc_height ||
1789 			    dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
1790 				continue;
1791 
1792 			/* Clip right and bottom to crtc limits */
1793 			dirty->unit_x2 = min_t(s32, dirty->unit_x2,
1794 					       crtc_width);
1795 			dirty->unit_y2 = min_t(s32, dirty->unit_y2,
1796 					       crtc_height);
1797 
1798 			/* Clip left and top to crtc limits */
1799 			clip_left = min_t(s32, dirty->unit_x1, 0);
1800 			clip_top = min_t(s32, dirty->unit_y1, 0);
1801 			dirty->unit_x1 -= clip_left;
1802 			dirty->unit_y1 -= clip_top;
1803 			dirty->fb_x -= clip_left;
1804 			dirty->fb_y -= clip_top;
1805 
1806 			dirty->clip(dirty);
1807 		}
1808 
1809 		dirty->fifo_commit(dirty);
1810 	}
1811 
1812 	return 0;
1813 }
1814 
1815 /**
1816  * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before
1817  * command submission.
1818  *
1819  * @dev_priv. Pointer to a device private structure.
1820  * @buf: The buffer object
1821  * @interruptible: Whether to perform waits as interruptible.
1822  * @validate_as_mob: Whether the buffer should be validated as a MOB. If false,
1823  * The buffer will be validated as a GMR. Already pinned buffers will not be
1824  * validated.
1825  *
1826  * Returns 0 on success, negative error code on failure, -ERESTARTSYS if
1827  * interrupted by a signal.
1828  */
1829 int vmw_kms_helper_buffer_prepare(struct vmw_private *dev_priv,
1830 				  struct vmw_dma_buffer *buf,
1831 				  bool interruptible,
1832 				  bool validate_as_mob)
1833 {
1834 	struct ttm_buffer_object *bo = &buf->base;
1835 	int ret;
1836 
1837 	ttm_bo_reserve(bo, false, false, NULL);
1838 	ret = vmw_validate_single_buffer(dev_priv, bo, interruptible,
1839 					 validate_as_mob);
1840 	if (ret)
1841 		ttm_bo_unreserve(bo);
1842 
1843 	return ret;
1844 }
1845 
1846 /**
1847  * vmw_kms_helper_buffer_revert - Undo the actions of
1848  * vmw_kms_helper_buffer_prepare.
1849  *
1850  * @res: Pointer to the buffer object.
1851  *
1852  * Helper to be used if an error forces the caller to undo the actions of
1853  * vmw_kms_helper_buffer_prepare.
1854  */
1855 void vmw_kms_helper_buffer_revert(struct vmw_dma_buffer *buf)
1856 {
1857 	if (buf)
1858 		ttm_bo_unreserve(&buf->base);
1859 }
1860 
1861 /**
1862  * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after
1863  * kms command submission.
1864  *
1865  * @dev_priv: Pointer to a device private structure.
1866  * @file_priv: Pointer to a struct drm_file representing the caller's
1867  * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely
1868  * if non-NULL, @user_fence_rep must be non-NULL.
1869  * @buf: The buffer object.
1870  * @out_fence:  Optional pointer to a fence pointer. If non-NULL, a
1871  * ref-counted fence pointer is returned here.
1872  * @user_fence_rep: Optional pointer to a user-space provided struct
1873  * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the
1874  * function copies fence data to user-space in a fail-safe manner.
1875  */
1876 void vmw_kms_helper_buffer_finish(struct vmw_private *dev_priv,
1877 				  struct drm_file *file_priv,
1878 				  struct vmw_dma_buffer *buf,
1879 				  struct vmw_fence_obj **out_fence,
1880 				  struct drm_vmw_fence_rep __user *
1881 				  user_fence_rep)
1882 {
1883 	struct vmw_fence_obj *fence;
1884 	uint32_t handle;
1885 	int ret;
1886 
1887 	ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
1888 					 file_priv ? &handle : NULL);
1889 	if (buf)
1890 		vmw_fence_single_bo(&buf->base, fence);
1891 	if (file_priv)
1892 		vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
1893 					    ret, user_fence_rep, fence,
1894 					    handle);
1895 	if (out_fence)
1896 		*out_fence = fence;
1897 	else
1898 		vmw_fence_obj_unreference(&fence);
1899 
1900 	vmw_kms_helper_buffer_revert(buf);
1901 }
1902 
1903 
1904 /**
1905  * vmw_kms_helper_resource_revert - Undo the actions of
1906  * vmw_kms_helper_resource_prepare.
1907  *
1908  * @res: Pointer to the resource. Typically a surface.
1909  *
1910  * Helper to be used if an error forces the caller to undo the actions of
1911  * vmw_kms_helper_resource_prepare.
1912  */
1913 void vmw_kms_helper_resource_revert(struct vmw_resource *res)
1914 {
1915 	vmw_kms_helper_buffer_revert(res->backup);
1916 	vmw_resource_unreserve(res, false, NULL, 0);
1917 	mutex_unlock(&res->dev_priv->cmdbuf_mutex);
1918 }
1919 
1920 /**
1921  * vmw_kms_helper_resource_prepare - Reserve and validate a resource before
1922  * command submission.
1923  *
1924  * @res: Pointer to the resource. Typically a surface.
1925  * @interruptible: Whether to perform waits as interruptible.
1926  *
1927  * Reserves and validates also the backup buffer if a guest-backed resource.
1928  * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1929  * interrupted by a signal.
1930  */
1931 int vmw_kms_helper_resource_prepare(struct vmw_resource *res,
1932 				    bool interruptible)
1933 {
1934 	int ret = 0;
1935 
1936 	if (interruptible)
1937 		ret = mutex_lock_interruptible(&res->dev_priv->cmdbuf_mutex);
1938 	else
1939 		mutex_lock(&res->dev_priv->cmdbuf_mutex);
1940 
1941 	if (unlikely(ret != 0))
1942 		return -ERESTARTSYS;
1943 
1944 	ret = vmw_resource_reserve(res, interruptible, false);
1945 	if (ret)
1946 		goto out_unlock;
1947 
1948 	if (res->backup) {
1949 		ret = vmw_kms_helper_buffer_prepare(res->dev_priv, res->backup,
1950 						    interruptible,
1951 						    res->dev_priv->has_mob);
1952 		if (ret)
1953 			goto out_unreserve;
1954 	}
1955 	ret = vmw_resource_validate(res);
1956 	if (ret)
1957 		goto out_revert;
1958 	return 0;
1959 
1960 out_revert:
1961 	vmw_kms_helper_buffer_revert(res->backup);
1962 out_unreserve:
1963 	vmw_resource_unreserve(res, false, NULL, 0);
1964 out_unlock:
1965 	mutex_unlock(&res->dev_priv->cmdbuf_mutex);
1966 	return ret;
1967 }
1968 
1969 /**
1970  * vmw_kms_helper_resource_finish - Unreserve and fence a resource after
1971  * kms command submission.
1972  *
1973  * @res: Pointer to the resource. Typically a surface.
1974  * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
1975  * ref-counted fence pointer is returned here.
1976  */
1977 void vmw_kms_helper_resource_finish(struct vmw_resource *res,
1978 			     struct vmw_fence_obj **out_fence)
1979 {
1980 	if (res->backup || out_fence)
1981 		vmw_kms_helper_buffer_finish(res->dev_priv, NULL, res->backup,
1982 					     out_fence, NULL);
1983 
1984 	vmw_resource_unreserve(res, false, NULL, 0);
1985 	mutex_unlock(&res->dev_priv->cmdbuf_mutex);
1986 }
1987 
1988 /**
1989  * vmw_kms_update_proxy - Helper function to update a proxy surface from
1990  * its backing MOB.
1991  *
1992  * @res: Pointer to the surface resource
1993  * @clips: Clip rects in framebuffer (surface) space.
1994  * @num_clips: Number of clips in @clips.
1995  * @increment: Integer with which to increment the clip counter when looping.
1996  * Used to skip a predetermined number of clip rects.
1997  *
1998  * This function makes sure the proxy surface is updated from its backing MOB
1999  * using the region given by @clips. The surface resource @res and its backing
2000  * MOB needs to be reserved and validated on call.
2001  */
2002 int vmw_kms_update_proxy(struct vmw_resource *res,
2003 			 const struct drm_clip_rect *clips,
2004 			 unsigned num_clips,
2005 			 int increment)
2006 {
2007 	struct vmw_private *dev_priv = res->dev_priv;
2008 	struct drm_vmw_size *size = &vmw_res_to_srf(res)->base_size;
2009 	struct {
2010 		SVGA3dCmdHeader header;
2011 		SVGA3dCmdUpdateGBImage body;
2012 	} *cmd;
2013 	SVGA3dBox *box;
2014 	size_t copy_size = 0;
2015 	int i;
2016 
2017 	if (!clips)
2018 		return 0;
2019 
2020 	cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips);
2021 	if (!cmd) {
2022 		DRM_ERROR("Couldn't reserve fifo space for proxy surface "
2023 			  "update.\n");
2024 		return -ENOMEM;
2025 	}
2026 
2027 	for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) {
2028 		box = &cmd->body.box;
2029 
2030 		cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
2031 		cmd->header.size = sizeof(cmd->body);
2032 		cmd->body.image.sid = res->id;
2033 		cmd->body.image.face = 0;
2034 		cmd->body.image.mipmap = 0;
2035 
2036 		if (clips->x1 > size->width || clips->x2 > size->width ||
2037 		    clips->y1 > size->height || clips->y2 > size->height) {
2038 			DRM_ERROR("Invalid clips outsize of framebuffer.\n");
2039 			return -EINVAL;
2040 		}
2041 
2042 		box->x = clips->x1;
2043 		box->y = clips->y1;
2044 		box->z = 0;
2045 		box->w = clips->x2 - clips->x1;
2046 		box->h = clips->y2 - clips->y1;
2047 		box->d = 1;
2048 
2049 		copy_size += sizeof(*cmd);
2050 	}
2051 
2052 	vmw_fifo_commit(dev_priv, copy_size);
2053 
2054 	return 0;
2055 }
2056 
2057 int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
2058 			    unsigned unit,
2059 			    u32 max_width,
2060 			    u32 max_height,
2061 			    struct drm_connector **p_con,
2062 			    struct drm_crtc **p_crtc,
2063 			    struct drm_display_mode **p_mode)
2064 {
2065 	struct drm_connector *con;
2066 	struct vmw_display_unit *du;
2067 	struct drm_display_mode *mode;
2068 	int i = 0;
2069 
2070 	list_for_each_entry(con, &dev_priv->dev->mode_config.connector_list,
2071 			    head) {
2072 		if (i == unit)
2073 			break;
2074 
2075 		++i;
2076 	}
2077 
2078 	if (i != unit) {
2079 		DRM_ERROR("Could not find initial display unit.\n");
2080 		return -EINVAL;
2081 	}
2082 
2083 	if (list_empty(&con->modes))
2084 		(void) vmw_du_connector_fill_modes(con, max_width, max_height);
2085 
2086 	if (list_empty(&con->modes)) {
2087 		DRM_ERROR("Could not find initial display mode.\n");
2088 		return -EINVAL;
2089 	}
2090 
2091 	du = vmw_connector_to_du(con);
2092 	*p_con = con;
2093 	*p_crtc = &du->crtc;
2094 
2095 	list_for_each_entry(mode, &con->modes, head) {
2096 		if (mode->type & DRM_MODE_TYPE_PREFERRED)
2097 			break;
2098 	}
2099 
2100 	if (mode->type & DRM_MODE_TYPE_PREFERRED)
2101 		*p_mode = mode;
2102 	else {
2103 		WARN_ONCE(true, "Could not find initial preferred mode.\n");
2104 		*p_mode = list_first_entry(&con->modes,
2105 					   struct drm_display_mode,
2106 					   head);
2107 	}
2108 
2109 	return 0;
2110 }
2111 
2112 /**
2113  * vmw_kms_del_active - unregister a crtc binding to the implicit framebuffer
2114  *
2115  * @dev_priv: Pointer to a device private struct.
2116  * @du: The display unit of the crtc.
2117  */
2118 void vmw_kms_del_active(struct vmw_private *dev_priv,
2119 			struct vmw_display_unit *du)
2120 {
2121 	mutex_lock(&dev_priv->global_kms_state_mutex);
2122 	if (du->active_implicit) {
2123 		if (--(dev_priv->num_implicit) == 0)
2124 			dev_priv->implicit_fb = NULL;
2125 		du->active_implicit = false;
2126 	}
2127 	mutex_unlock(&dev_priv->global_kms_state_mutex);
2128 }
2129 
2130 /**
2131  * vmw_kms_add_active - register a crtc binding to an implicit framebuffer
2132  *
2133  * @vmw_priv: Pointer to a device private struct.
2134  * @du: The display unit of the crtc.
2135  * @vfb: The implicit framebuffer
2136  *
2137  * Registers a binding to an implicit framebuffer.
2138  */
2139 void vmw_kms_add_active(struct vmw_private *dev_priv,
2140 			struct vmw_display_unit *du,
2141 			struct vmw_framebuffer *vfb)
2142 {
2143 	mutex_lock(&dev_priv->global_kms_state_mutex);
2144 	WARN_ON_ONCE(!dev_priv->num_implicit && dev_priv->implicit_fb);
2145 
2146 	if (!du->active_implicit && du->is_implicit) {
2147 		dev_priv->implicit_fb = vfb;
2148 		du->active_implicit = true;
2149 		dev_priv->num_implicit++;
2150 	}
2151 	mutex_unlock(&dev_priv->global_kms_state_mutex);
2152 }
2153 
2154 /**
2155  * vmw_kms_screen_object_flippable - Check whether we can page-flip a crtc.
2156  *
2157  * @dev_priv: Pointer to device-private struct.
2158  * @crtc: The crtc we want to flip.
2159  *
2160  * Returns true or false depending whether it's OK to flip this crtc
2161  * based on the criterion that we must not have more than one implicit
2162  * frame-buffer at any one time.
2163  */
2164 bool vmw_kms_crtc_flippable(struct vmw_private *dev_priv,
2165 			    struct drm_crtc *crtc)
2166 {
2167 	struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
2168 	bool ret;
2169 
2170 	mutex_lock(&dev_priv->global_kms_state_mutex);
2171 	ret = !du->is_implicit || dev_priv->num_implicit == 1;
2172 	mutex_unlock(&dev_priv->global_kms_state_mutex);
2173 
2174 	return ret;
2175 }
2176 
2177 /**
2178  * vmw_kms_update_implicit_fb - Update the implicit fb.
2179  *
2180  * @dev_priv: Pointer to device-private struct.
2181  * @crtc: The crtc the new implicit frame-buffer is bound to.
2182  */
2183 void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv,
2184 				struct drm_crtc *crtc)
2185 {
2186 	struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
2187 	struct vmw_framebuffer *vfb;
2188 
2189 	mutex_lock(&dev_priv->global_kms_state_mutex);
2190 
2191 	if (!du->is_implicit)
2192 		goto out_unlock;
2193 
2194 	vfb = vmw_framebuffer_to_vfb(crtc->primary->fb);
2195 	WARN_ON_ONCE(dev_priv->num_implicit != 1 &&
2196 		     dev_priv->implicit_fb != vfb);
2197 
2198 	dev_priv->implicit_fb = vfb;
2199 out_unlock:
2200 	mutex_unlock(&dev_priv->global_kms_state_mutex);
2201 }
2202 
2203 /**
2204  * vmw_kms_create_implicit_placement_proparty - Set up the implicit placement
2205  * property.
2206  *
2207  * @dev_priv: Pointer to a device private struct.
2208  * @immutable: Whether the property is immutable.
2209  *
2210  * Sets up the implicit placement property unless it's already set up.
2211  */
2212 void
2213 vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv,
2214 					   bool immutable)
2215 {
2216 	if (dev_priv->implicit_placement_property)
2217 		return;
2218 
2219 	dev_priv->implicit_placement_property =
2220 		drm_property_create_range(dev_priv->dev,
2221 					  immutable ?
2222 					  DRM_MODE_PROP_IMMUTABLE : 0,
2223 					  "implicit_placement", 0, 1);
2224 
2225 }
2226