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