1/*
2 * Copyright 2013 Red Hat Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: Dave Airlie
23 *          Alon Levy
24 */
25
26#include <linux/crc32.h>
27#include <linux/delay.h>
28
29#include <drm/drm_drv.h>
30#include <drm/drm_atomic.h>
31#include <drm/drm_atomic_helper.h>
32#include <drm/drm_gem_framebuffer_helper.h>
33#include <drm/drm_plane_helper.h>
34#include <drm/drm_probe_helper.h>
35#include <drm/drm_simple_kms_helper.h>
36
37#include "qxl_drv.h"
38#include "qxl_object.h"
39
40static bool qxl_head_enabled(struct qxl_head *head)
41{
42	return head->width && head->height;
43}
44
45static int qxl_alloc_client_monitors_config(struct qxl_device *qdev,
46		unsigned int count)
47{
48	if (qdev->client_monitors_config &&
49	    count > qdev->client_monitors_config->count) {
50		kfree(qdev->client_monitors_config);
51		qdev->client_monitors_config = NULL;
52	}
53	if (!qdev->client_monitors_config) {
54		qdev->client_monitors_config = kzalloc(
55				struct_size(qdev->client_monitors_config,
56				heads, count), GFP_KERNEL);
57		if (!qdev->client_monitors_config)
58			return -ENOMEM;
59	}
60	qdev->client_monitors_config->count = count;
61	return 0;
62}
63
64enum {
65	MONITORS_CONFIG_MODIFIED,
66	MONITORS_CONFIG_UNCHANGED,
67	MONITORS_CONFIG_BAD_CRC,
68	MONITORS_CONFIG_ERROR,
69};
70
71static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
72{
73	int i;
74	int num_monitors;
75	uint32_t crc;
76	int status = MONITORS_CONFIG_UNCHANGED;
77
78	num_monitors = qdev->rom->client_monitors_config.count;
79	crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
80		  sizeof(qdev->rom->client_monitors_config));
81	if (crc != qdev->rom->client_monitors_config_crc)
82		return MONITORS_CONFIG_BAD_CRC;
83	if (!num_monitors) {
84		DRM_DEBUG_KMS("no client monitors configured\n");
85		return status;
86	}
87	if (num_monitors > qxl_num_crtc) {
88		DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
89			      qxl_num_crtc, num_monitors);
90		num_monitors = qxl_num_crtc;
91	} else {
92		num_monitors = qdev->rom->client_monitors_config.count;
93	}
94	if (qdev->client_monitors_config
95	      && (num_monitors != qdev->client_monitors_config->count)) {
96		status = MONITORS_CONFIG_MODIFIED;
97	}
98	if (qxl_alloc_client_monitors_config(qdev, num_monitors)) {
99		status = MONITORS_CONFIG_ERROR;
100		return status;
101	}
102	/* we copy max from the client but it isn't used */
103	qdev->client_monitors_config->max_allowed = qxl_num_crtc;
104	for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
105		struct qxl_urect *c_rect =
106			&qdev->rom->client_monitors_config.heads[i];
107		struct qxl_head *client_head =
108			&qdev->client_monitors_config->heads[i];
109		if (client_head->x != c_rect->left) {
110			client_head->x = c_rect->left;
111			status = MONITORS_CONFIG_MODIFIED;
112		}
113		if (client_head->y != c_rect->top) {
114			client_head->y = c_rect->top;
115			status = MONITORS_CONFIG_MODIFIED;
116		}
117		if (client_head->width != c_rect->right - c_rect->left) {
118			client_head->width = c_rect->right - c_rect->left;
119			status = MONITORS_CONFIG_MODIFIED;
120		}
121		if (client_head->height != c_rect->bottom - c_rect->top) {
122			client_head->height = c_rect->bottom - c_rect->top;
123			status = MONITORS_CONFIG_MODIFIED;
124		}
125		if (client_head->surface_id != 0) {
126			client_head->surface_id = 0;
127			status = MONITORS_CONFIG_MODIFIED;
128		}
129		if (client_head->id != i) {
130			client_head->id = i;
131			status = MONITORS_CONFIG_MODIFIED;
132		}
133		if (client_head->flags != 0) {
134			client_head->flags = 0;
135			status = MONITORS_CONFIG_MODIFIED;
136		}
137		DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
138			  client_head->x, client_head->y);
139	}
140
141	return status;
142}
143
144static void qxl_update_offset_props(struct qxl_device *qdev)
145{
146	struct drm_device *dev = &qdev->ddev;
147	struct drm_connector *connector;
148	struct qxl_output *output;
149	struct qxl_head *head;
150
151	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
152		output = drm_connector_to_qxl_output(connector);
153
154		head = &qdev->client_monitors_config->heads[output->index];
155
156		drm_object_property_set_value(&connector->base,
157			dev->mode_config.suggested_x_property, head->x);
158		drm_object_property_set_value(&connector->base,
159			dev->mode_config.suggested_y_property, head->y);
160	}
161}
162
163void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
164{
165	struct drm_device *dev = &qdev->ddev;
166	struct drm_modeset_acquire_ctx ctx;
167	int status, retries, ret;
168
169	for (retries = 0; retries < 10; retries++) {
170		status = qxl_display_copy_rom_client_monitors_config(qdev);
171		if (status != MONITORS_CONFIG_BAD_CRC)
172			break;
173		udelay(5);
174	}
175	if (status == MONITORS_CONFIG_ERROR) {
176		DRM_DEBUG_KMS("ignoring client monitors config: error");
177		return;
178	}
179	if (status == MONITORS_CONFIG_BAD_CRC) {
180		DRM_DEBUG_KMS("ignoring client monitors config: bad crc");
181		return;
182	}
183	if (status == MONITORS_CONFIG_UNCHANGED) {
184		DRM_DEBUG_KMS("ignoring client monitors config: unchanged");
185		return;
186	}
187
188	DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE, ret);
189	qxl_update_offset_props(qdev);
190	DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
191	if (!drm_helper_hpd_irq_event(dev)) {
192		/* notify that the monitor configuration changed, to
193		   adjust at the arbitrary resolution */
194		drm_kms_helper_hotplug_event(dev);
195	}
196}
197
198static int qxl_check_mode(struct qxl_device *qdev,
199			  unsigned int width,
200			  unsigned int height)
201{
202	unsigned int stride;
203	unsigned int size;
204
205	if (check_mul_overflow(width, 4u, &stride))
206		return -EINVAL;
207	if (check_mul_overflow(stride, height, &size))
208		return -EINVAL;
209	if (size > qdev->vram_size)
210		return -ENOMEM;
211	return 0;
212}
213
214static int qxl_check_framebuffer(struct qxl_device *qdev,
215				 struct qxl_bo *bo)
216{
217	return qxl_check_mode(qdev, bo->surf.width, bo->surf.height);
218}
219
220static int qxl_add_mode(struct drm_connector *connector,
221			unsigned int width,
222			unsigned int height,
223			bool preferred)
224{
225	struct drm_device *dev = connector->dev;
226	struct qxl_device *qdev = to_qxl(dev);
227	struct drm_display_mode *mode = NULL;
228	int rc;
229
230	rc = qxl_check_mode(qdev, width, height);
231	if (rc != 0)
232		return 0;
233
234	mode = drm_cvt_mode(dev, width, height, 60, false, false, false);
235	if (preferred)
236		mode->type |= DRM_MODE_TYPE_PREFERRED;
237	mode->hdisplay = width;
238	mode->vdisplay = height;
239	drm_mode_set_name(mode);
240	drm_mode_probed_add(connector, mode);
241	return 1;
242}
243
244static int qxl_add_monitors_config_modes(struct drm_connector *connector)
245{
246	struct drm_device *dev = connector->dev;
247	struct qxl_device *qdev = to_qxl(dev);
248	struct qxl_output *output = drm_connector_to_qxl_output(connector);
249	int h = output->index;
250	struct qxl_head *head;
251
252	if (!qdev->monitors_config)
253		return 0;
254	if (h >= qxl_num_crtc)
255		return 0;
256	if (!qdev->client_monitors_config)
257		return 0;
258	if (h >= qdev->client_monitors_config->count)
259		return 0;
260
261	head = &qdev->client_monitors_config->heads[h];
262	DRM_DEBUG_KMS("head %d is %dx%d\n", h, head->width, head->height);
263
264	return qxl_add_mode(connector, head->width, head->height, true);
265}
266
267static struct mode_size {
268	int w;
269	int h;
270} extra_modes[] = {
271	{ 720,  480},
272	{1152,  768},
273	{1280,  854},
274};
275
276static int qxl_add_extra_modes(struct drm_connector *connector)
277{
278	int i, ret = 0;
279
280	for (i = 0; i < ARRAY_SIZE(extra_modes); i++)
281		ret += qxl_add_mode(connector,
282				    extra_modes[i].w,
283				    extra_modes[i].h,
284				    false);
285	return ret;
286}
287
288static void qxl_send_monitors_config(struct qxl_device *qdev)
289{
290	int i;
291
292	BUG_ON(!qdev->ram_header->monitors_config);
293
294	if (qdev->monitors_config->count == 0)
295		return;
296
297	for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
298		struct qxl_head *head = &qdev->monitors_config->heads[i];
299
300		if (head->y > 8192 || head->x > 8192 ||
301		    head->width > 8192 || head->height > 8192) {
302			DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
303				  i, head->width, head->height,
304				  head->x, head->y);
305			return;
306		}
307	}
308	qxl_io_monitors_config(qdev);
309}
310
311static void qxl_crtc_update_monitors_config(struct drm_crtc *crtc,
312					    const char *reason)
313{
314	struct drm_device *dev = crtc->dev;
315	struct qxl_device *qdev = to_qxl(dev);
316	struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
317	struct qxl_head head;
318	int oldcount, i = qcrtc->index;
319
320	if (!qdev->primary_bo) {
321		DRM_DEBUG_KMS("no primary surface, skip (%s)\n", reason);
322		return;
323	}
324
325	if (!qdev->monitors_config || qxl_num_crtc <= i)
326		return;
327
328	head.id = i;
329	head.flags = 0;
330	head.surface_id = 0;
331	oldcount = qdev->monitors_config->count;
332	if (crtc->state->active) {
333		struct drm_display_mode *mode = &crtc->mode;
334
335		head.width = mode->hdisplay;
336		head.height = mode->vdisplay;
337		head.x = crtc->x;
338		head.y = crtc->y;
339		if (qdev->monitors_config->count < i + 1)
340			qdev->monitors_config->count = i + 1;
341		if (qdev->primary_bo == qdev->dumb_shadow_bo)
342			head.x += qdev->dumb_heads[i].x;
343	} else if (i > 0) {
344		head.width = 0;
345		head.height = 0;
346		head.x = 0;
347		head.y = 0;
348		if (qdev->monitors_config->count == i + 1)
349			qdev->monitors_config->count = i;
350	} else {
351		DRM_DEBUG_KMS("inactive head 0, skip (%s)\n", reason);
352		return;
353	}
354
355	if (head.width  == qdev->monitors_config->heads[i].width  &&
356	    head.height == qdev->monitors_config->heads[i].height &&
357	    head.x      == qdev->monitors_config->heads[i].x      &&
358	    head.y      == qdev->monitors_config->heads[i].y      &&
359	    oldcount    == qdev->monitors_config->count)
360		return;
361
362	DRM_DEBUG_KMS("head %d, %dx%d, at +%d+%d, %s (%s)\n",
363		      i, head.width, head.height, head.x, head.y,
364		      crtc->state->active ? "on" : "off", reason);
365	if (oldcount != qdev->monitors_config->count)
366		DRM_DEBUG_KMS("active heads %d -> %d (%d total)\n",
367			      oldcount, qdev->monitors_config->count,
368			      qxl_num_crtc);
369
370	qdev->monitors_config->heads[i] = head;
371	qdev->monitors_config->max_allowed = qxl_num_crtc;
372	qxl_send_monitors_config(qdev);
373}
374
375static void qxl_crtc_atomic_flush(struct drm_crtc *crtc,
376				  struct drm_crtc_state *old_crtc_state)
377{
378	qxl_crtc_update_monitors_config(crtc, "flush");
379}
380
381static void qxl_crtc_destroy(struct drm_crtc *crtc)
382{
383	struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
384
385	qxl_bo_unref(&qxl_crtc->cursor_bo);
386	drm_crtc_cleanup(crtc);
387	kfree(qxl_crtc);
388}
389
390static const struct drm_crtc_funcs qxl_crtc_funcs = {
391	.set_config = drm_atomic_helper_set_config,
392	.destroy = qxl_crtc_destroy,
393	.page_flip = drm_atomic_helper_page_flip,
394	.reset = drm_atomic_helper_crtc_reset,
395	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
396	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
397};
398
399static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
400					 struct drm_file *file_priv,
401					 unsigned int flags, unsigned int color,
402					 struct drm_clip_rect *clips,
403					 unsigned int num_clips)
404{
405	/* TODO: vmwgfx where this was cribbed from had locking. Why? */
406	struct qxl_device *qdev = to_qxl(fb->dev);
407	struct drm_clip_rect norect;
408	struct qxl_bo *qobj;
409	struct drm_modeset_acquire_ctx ctx;
410	bool is_primary;
411	int inc = 1, ret;
412
413	DRM_MODESET_LOCK_ALL_BEGIN(fb->dev, ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE, ret);
414
415	qobj = gem_to_qxl_bo(fb->obj[0]);
416	/* if we aren't primary surface ignore this */
417	is_primary = qobj->shadow ? qobj->shadow->is_primary : qobj->is_primary;
418	if (!is_primary)
419		goto out_lock_end;
420
421	if (!num_clips) {
422		num_clips = 1;
423		clips = &norect;
424		norect.x1 = norect.y1 = 0;
425		norect.x2 = fb->width;
426		norect.y2 = fb->height;
427	} else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
428		num_clips /= 2;
429		inc = 2; /* skip source rects */
430	}
431
432	qxl_draw_dirty_fb(qdev, fb, qobj, flags, color,
433			  clips, num_clips, inc, 0);
434
435out_lock_end:
436	DRM_MODESET_LOCK_ALL_END(fb->dev, ctx, ret);
437
438	return 0;
439}
440
441static const struct drm_framebuffer_funcs qxl_fb_funcs = {
442	.destroy = drm_gem_fb_destroy,
443	.dirty = qxl_framebuffer_surface_dirty,
444	.create_handle = drm_gem_fb_create_handle,
445};
446
447static void qxl_crtc_atomic_enable(struct drm_crtc *crtc,
448				   struct drm_crtc_state *old_state)
449{
450	qxl_crtc_update_monitors_config(crtc, "enable");
451}
452
453static void qxl_crtc_atomic_disable(struct drm_crtc *crtc,
454				    struct drm_crtc_state *old_state)
455{
456	qxl_crtc_update_monitors_config(crtc, "disable");
457}
458
459static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
460	.atomic_flush = qxl_crtc_atomic_flush,
461	.atomic_enable = qxl_crtc_atomic_enable,
462	.atomic_disable = qxl_crtc_atomic_disable,
463};
464
465static int qxl_primary_atomic_check(struct drm_plane *plane,
466				    struct drm_plane_state *state)
467{
468	struct qxl_device *qdev = to_qxl(plane->dev);
469	struct qxl_bo *bo;
470
471	if (!state->crtc || !state->fb)
472		return 0;
473
474	bo = gem_to_qxl_bo(state->fb->obj[0]);
475
476	return qxl_check_framebuffer(qdev, bo);
477}
478
479static int qxl_primary_apply_cursor(struct drm_plane *plane)
480{
481	struct drm_device *dev = plane->dev;
482	struct qxl_device *qdev = to_qxl(dev);
483	struct drm_framebuffer *fb = plane->state->fb;
484	struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc);
485	struct qxl_cursor_cmd *cmd;
486	struct qxl_release *release;
487	int ret = 0;
488
489	if (!qcrtc->cursor_bo)
490		return 0;
491
492	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
493					 QXL_RELEASE_CURSOR_CMD,
494					 &release, NULL);
495	if (ret)
496		return ret;
497
498	ret = qxl_release_list_add(release, qcrtc->cursor_bo);
499	if (ret)
500		goto out_free_release;
501
502	ret = qxl_release_reserve_list(release, false);
503	if (ret)
504		goto out_free_release;
505
506	cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
507	cmd->type = QXL_CURSOR_SET;
508	cmd->u.set.position.x = plane->state->crtc_x + fb->hot_x;
509	cmd->u.set.position.y = plane->state->crtc_y + fb->hot_y;
510
511	cmd->u.set.shape = qxl_bo_physical_address(qdev, qcrtc->cursor_bo, 0);
512
513	cmd->u.set.visible = 1;
514	qxl_release_unmap(qdev, release, &cmd->release_info);
515
516	qxl_release_fence_buffer_objects(release);
517	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
518
519	return ret;
520
521out_free_release:
522	qxl_release_free(qdev, release);
523	return ret;
524}
525
526static void qxl_primary_atomic_update(struct drm_plane *plane,
527				      struct drm_plane_state *old_state)
528{
529	struct qxl_device *qdev = to_qxl(plane->dev);
530	struct qxl_bo *bo = gem_to_qxl_bo(plane->state->fb->obj[0]);
531	struct qxl_bo *primary;
532	struct drm_clip_rect norect = {
533	    .x1 = 0,
534	    .y1 = 0,
535	    .x2 = plane->state->fb->width,
536	    .y2 = plane->state->fb->height
537	};
538	uint32_t dumb_shadow_offset = 0;
539
540	primary = bo->shadow ? bo->shadow : bo;
541
542	if (!primary->is_primary) {
543		if (qdev->primary_bo)
544			qxl_io_destroy_primary(qdev);
545		qxl_io_create_primary(qdev, primary);
546		qxl_primary_apply_cursor(plane);
547	}
548
549	if (bo->is_dumb)
550		dumb_shadow_offset =
551			qdev->dumb_heads[plane->state->crtc->index].x;
552
553	qxl_draw_dirty_fb(qdev, plane->state->fb, bo, 0, 0, &norect, 1, 1,
554			  dumb_shadow_offset);
555}
556
557static void qxl_primary_atomic_disable(struct drm_plane *plane,
558				       struct drm_plane_state *old_state)
559{
560	struct qxl_device *qdev = to_qxl(plane->dev);
561
562	if (old_state->fb) {
563		struct qxl_bo *bo = gem_to_qxl_bo(old_state->fb->obj[0]);
564
565		if (bo->is_primary) {
566			qxl_io_destroy_primary(qdev);
567			bo->is_primary = false;
568		}
569	}
570}
571
572static void qxl_cursor_atomic_update(struct drm_plane *plane,
573				     struct drm_plane_state *old_state)
574{
575	struct drm_device *dev = plane->dev;
576	struct qxl_device *qdev = to_qxl(dev);
577	struct drm_framebuffer *fb = plane->state->fb;
578	struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc);
579	struct qxl_release *release;
580	struct qxl_cursor_cmd *cmd;
581	struct qxl_cursor *cursor;
582	struct drm_gem_object *obj;
583	struct qxl_bo *cursor_bo = NULL, *user_bo = NULL, *old_cursor_bo = NULL;
584	int ret;
585	void *user_ptr;
586	int size = 64*64*4;
587
588	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
589					 QXL_RELEASE_CURSOR_CMD,
590					 &release, NULL);
591	if (ret)
592		return;
593
594	if (fb != old_state->fb) {
595		obj = fb->obj[0];
596		user_bo = gem_to_qxl_bo(obj);
597
598		/* pinning is done in the prepare/cleanup framevbuffer */
599		ret = qxl_bo_kmap(user_bo, &user_ptr);
600		if (ret)
601			goto out_free_release;
602
603		ret = qxl_alloc_bo_reserved(qdev, release,
604					    sizeof(struct qxl_cursor) + size,
605					    &cursor_bo);
606		if (ret)
607			goto out_kunmap;
608
609		ret = qxl_bo_pin(cursor_bo);
610		if (ret)
611			goto out_free_bo;
612
613		ret = qxl_release_reserve_list(release, true);
614		if (ret)
615			goto out_unpin;
616
617		ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
618		if (ret)
619			goto out_backoff;
620
621		cursor->header.unique = 0;
622		cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
623		cursor->header.width = 64;
624		cursor->header.height = 64;
625		cursor->header.hot_spot_x = fb->hot_x;
626		cursor->header.hot_spot_y = fb->hot_y;
627		cursor->data_size = size;
628		cursor->chunk.next_chunk = 0;
629		cursor->chunk.prev_chunk = 0;
630		cursor->chunk.data_size = size;
631		memcpy(cursor->chunk.data, user_ptr, size);
632		qxl_bo_kunmap(cursor_bo);
633		qxl_bo_kunmap(user_bo);
634
635		cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
636		cmd->u.set.visible = 1;
637		cmd->u.set.shape = qxl_bo_physical_address(qdev,
638							   cursor_bo, 0);
639		cmd->type = QXL_CURSOR_SET;
640
641		old_cursor_bo = qcrtc->cursor_bo;
642		qcrtc->cursor_bo = cursor_bo;
643		cursor_bo = NULL;
644	} else {
645
646		ret = qxl_release_reserve_list(release, true);
647		if (ret)
648			goto out_free_release;
649
650		cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
651		cmd->type = QXL_CURSOR_MOVE;
652	}
653
654	cmd->u.position.x = plane->state->crtc_x + fb->hot_x;
655	cmd->u.position.y = plane->state->crtc_y + fb->hot_y;
656
657	qxl_release_unmap(qdev, release, &cmd->release_info);
658	qxl_release_fence_buffer_objects(release);
659	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
660
661	if (old_cursor_bo != NULL)
662		qxl_bo_unpin(old_cursor_bo);
663	qxl_bo_unref(&old_cursor_bo);
664	qxl_bo_unref(&cursor_bo);
665
666	return;
667
668out_backoff:
669	qxl_release_backoff_reserve_list(release);
670out_unpin:
671	qxl_bo_unpin(cursor_bo);
672out_free_bo:
673	qxl_bo_unref(&cursor_bo);
674out_kunmap:
675	qxl_bo_kunmap(user_bo);
676out_free_release:
677	qxl_release_free(qdev, release);
678	return;
679
680}
681
682static void qxl_cursor_atomic_disable(struct drm_plane *plane,
683				      struct drm_plane_state *old_state)
684{
685	struct qxl_device *qdev = to_qxl(plane->dev);
686	struct qxl_release *release;
687	struct qxl_cursor_cmd *cmd;
688	int ret;
689
690	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
691					 QXL_RELEASE_CURSOR_CMD,
692					 &release, NULL);
693	if (ret)
694		return;
695
696	ret = qxl_release_reserve_list(release, true);
697	if (ret) {
698		qxl_release_free(qdev, release);
699		return;
700	}
701
702	cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
703	cmd->type = QXL_CURSOR_HIDE;
704	qxl_release_unmap(qdev, release, &cmd->release_info);
705
706	qxl_release_fence_buffer_objects(release);
707	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
708}
709
710static void qxl_update_dumb_head(struct qxl_device *qdev,
711				 int index, struct qxl_bo *bo)
712{
713	uint32_t width, height;
714
715	if (index >= qdev->monitors_config->max_allowed)
716		return;
717
718	if (bo && bo->is_dumb) {
719		width = bo->surf.width;
720		height = bo->surf.height;
721	} else {
722		width = 0;
723		height = 0;
724	}
725
726	if (qdev->dumb_heads[index].width == width &&
727	    qdev->dumb_heads[index].height == height)
728		return;
729
730	DRM_DEBUG("#%d: %dx%d -> %dx%d\n", index,
731		  qdev->dumb_heads[index].width,
732		  qdev->dumb_heads[index].height,
733		  width, height);
734	qdev->dumb_heads[index].width = width;
735	qdev->dumb_heads[index].height = height;
736}
737
738static void qxl_calc_dumb_shadow(struct qxl_device *qdev,
739				 struct qxl_surface *surf)
740{
741	struct qxl_head *head;
742	int i;
743
744	memset(surf, 0, sizeof(*surf));
745	for (i = 0; i < qdev->monitors_config->max_allowed; i++) {
746		head = qdev->dumb_heads + i;
747		head->x = surf->width;
748		surf->width += head->width;
749		if (surf->height < head->height)
750			surf->height = head->height;
751	}
752	if (surf->width < 64)
753		surf->width = 64;
754	if (surf->height < 64)
755		surf->height = 64;
756	surf->format = SPICE_SURFACE_FMT_32_xRGB;
757	surf->stride = surf->width * 4;
758
759	if (!qdev->dumb_shadow_bo ||
760	    qdev->dumb_shadow_bo->surf.width != surf->width ||
761	    qdev->dumb_shadow_bo->surf.height != surf->height)
762		DRM_DEBUG("%dx%d\n", surf->width, surf->height);
763}
764
765static int qxl_plane_prepare_fb(struct drm_plane *plane,
766				struct drm_plane_state *new_state)
767{
768	struct qxl_device *qdev = to_qxl(plane->dev);
769	struct drm_gem_object *obj;
770	struct qxl_bo *user_bo;
771	struct qxl_surface surf;
772	int ret;
773
774	if (!new_state->fb)
775		return 0;
776
777	obj = new_state->fb->obj[0];
778	user_bo = gem_to_qxl_bo(obj);
779
780	if (plane->type == DRM_PLANE_TYPE_PRIMARY &&
781	    user_bo->is_dumb) {
782		qxl_update_dumb_head(qdev, new_state->crtc->index,
783				     user_bo);
784		qxl_calc_dumb_shadow(qdev, &surf);
785		if (!qdev->dumb_shadow_bo ||
786		    qdev->dumb_shadow_bo->surf.width  != surf.width ||
787		    qdev->dumb_shadow_bo->surf.height != surf.height) {
788			if (qdev->dumb_shadow_bo) {
789				drm_gem_object_put
790					(&qdev->dumb_shadow_bo->tbo.base);
791				qdev->dumb_shadow_bo = NULL;
792			}
793			qxl_bo_create(qdev, surf.height * surf.stride,
794				      true, true, QXL_GEM_DOMAIN_SURFACE, 0,
795				      &surf, &qdev->dumb_shadow_bo);
796		}
797		if (user_bo->shadow != qdev->dumb_shadow_bo) {
798			if (user_bo->shadow) {
799				drm_gem_object_put
800					(&user_bo->shadow->tbo.base);
801				user_bo->shadow = NULL;
802			}
803			drm_gem_object_get(&qdev->dumb_shadow_bo->tbo.base);
804			user_bo->shadow = qdev->dumb_shadow_bo;
805		}
806	}
807
808	ret = qxl_bo_pin(user_bo);
809	if (ret)
810		return ret;
811
812	return 0;
813}
814
815static void qxl_plane_cleanup_fb(struct drm_plane *plane,
816				 struct drm_plane_state *old_state)
817{
818	struct drm_gem_object *obj;
819	struct qxl_bo *user_bo;
820
821	if (!old_state->fb) {
822		/*
823		 * we never executed prepare_fb, so there's nothing to
824		 * unpin.
825		 */
826		return;
827	}
828
829	obj = old_state->fb->obj[0];
830	user_bo = gem_to_qxl_bo(obj);
831	qxl_bo_unpin(user_bo);
832
833	if (old_state->fb != plane->state->fb && user_bo->shadow) {
834		drm_gem_object_put(&user_bo->shadow->tbo.base);
835		user_bo->shadow = NULL;
836	}
837}
838
839static const uint32_t qxl_cursor_plane_formats[] = {
840	DRM_FORMAT_ARGB8888,
841};
842
843static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = {
844	.atomic_update = qxl_cursor_atomic_update,
845	.atomic_disable = qxl_cursor_atomic_disable,
846	.prepare_fb = qxl_plane_prepare_fb,
847	.cleanup_fb = qxl_plane_cleanup_fb,
848};
849
850static const struct drm_plane_funcs qxl_cursor_plane_funcs = {
851	.update_plane	= drm_atomic_helper_update_plane,
852	.disable_plane	= drm_atomic_helper_disable_plane,
853	.destroy	= drm_primary_helper_destroy,
854	.reset		= drm_atomic_helper_plane_reset,
855	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
856	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
857};
858
859static const uint32_t qxl_primary_plane_formats[] = {
860	DRM_FORMAT_XRGB8888,
861	DRM_FORMAT_ARGB8888,
862};
863
864static const struct drm_plane_helper_funcs primary_helper_funcs = {
865	.atomic_check = qxl_primary_atomic_check,
866	.atomic_update = qxl_primary_atomic_update,
867	.atomic_disable = qxl_primary_atomic_disable,
868	.prepare_fb = qxl_plane_prepare_fb,
869	.cleanup_fb = qxl_plane_cleanup_fb,
870};
871
872static const struct drm_plane_funcs qxl_primary_plane_funcs = {
873	.update_plane	= drm_atomic_helper_update_plane,
874	.disable_plane	= drm_atomic_helper_disable_plane,
875	.destroy	= drm_primary_helper_destroy,
876	.reset		= drm_atomic_helper_plane_reset,
877	.atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
878	.atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
879};
880
881static struct drm_plane *qxl_create_plane(struct qxl_device *qdev,
882					  unsigned int possible_crtcs,
883					  enum drm_plane_type type)
884{
885	const struct drm_plane_helper_funcs *helper_funcs = NULL;
886	struct drm_plane *plane;
887	const struct drm_plane_funcs *funcs;
888	const uint32_t *formats;
889	int num_formats;
890	int err;
891
892	if (type == DRM_PLANE_TYPE_PRIMARY) {
893		funcs = &qxl_primary_plane_funcs;
894		formats = qxl_primary_plane_formats;
895		num_formats = ARRAY_SIZE(qxl_primary_plane_formats);
896		helper_funcs = &primary_helper_funcs;
897	} else if (type == DRM_PLANE_TYPE_CURSOR) {
898		funcs = &qxl_cursor_plane_funcs;
899		formats = qxl_cursor_plane_formats;
900		helper_funcs = &qxl_cursor_helper_funcs;
901		num_formats = ARRAY_SIZE(qxl_cursor_plane_formats);
902	} else {
903		return ERR_PTR(-EINVAL);
904	}
905
906	plane = kzalloc(sizeof(*plane), GFP_KERNEL);
907	if (!plane)
908		return ERR_PTR(-ENOMEM);
909
910	err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs,
911				       funcs, formats, num_formats,
912				       NULL, type, NULL);
913	if (err)
914		goto free_plane;
915
916	drm_plane_helper_add(plane, helper_funcs);
917
918	return plane;
919
920free_plane:
921	kfree(plane);
922	return ERR_PTR(-EINVAL);
923}
924
925static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
926{
927	struct qxl_crtc *qxl_crtc;
928	struct drm_plane *primary, *cursor;
929	struct qxl_device *qdev = to_qxl(dev);
930	int r;
931
932	qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
933	if (!qxl_crtc)
934		return -ENOMEM;
935
936	primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY);
937	if (IS_ERR(primary)) {
938		r = -ENOMEM;
939		goto free_mem;
940	}
941
942	cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR);
943	if (IS_ERR(cursor)) {
944		r = -ENOMEM;
945		goto clean_primary;
946	}
947
948	r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor,
949				      &qxl_crtc_funcs, NULL);
950	if (r)
951		goto clean_cursor;
952
953	qxl_crtc->index = crtc_id;
954	drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
955	return 0;
956
957clean_cursor:
958	drm_plane_cleanup(cursor);
959	kfree(cursor);
960clean_primary:
961	drm_plane_cleanup(primary);
962	kfree(primary);
963free_mem:
964	kfree(qxl_crtc);
965	return r;
966}
967
968static int qxl_conn_get_modes(struct drm_connector *connector)
969{
970	struct drm_device *dev = connector->dev;
971	struct qxl_device *qdev = to_qxl(dev);
972	struct qxl_output *output = drm_connector_to_qxl_output(connector);
973	unsigned int pwidth = 1024;
974	unsigned int pheight = 768;
975	int ret = 0;
976
977	if (qdev->client_monitors_config) {
978		struct qxl_head *head;
979		head = &qdev->client_monitors_config->heads[output->index];
980		if (head->width)
981			pwidth = head->width;
982		if (head->height)
983			pheight = head->height;
984	}
985
986	ret += drm_add_modes_noedid(connector, 8192, 8192);
987	ret += qxl_add_extra_modes(connector);
988	ret += qxl_add_monitors_config_modes(connector);
989	drm_set_preferred_mode(connector, pwidth, pheight);
990	return ret;
991}
992
993static enum drm_mode_status qxl_conn_mode_valid(struct drm_connector *connector,
994			       struct drm_display_mode *mode)
995{
996	struct drm_device *ddev = connector->dev;
997	struct qxl_device *qdev = to_qxl(ddev);
998
999	if (qxl_check_mode(qdev, mode->hdisplay, mode->vdisplay) != 0)
1000		return MODE_BAD;
1001
1002	return MODE_OK;
1003}
1004
1005static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
1006{
1007	struct qxl_output *qxl_output =
1008		drm_connector_to_qxl_output(connector);
1009
1010	DRM_DEBUG("\n");
1011	return &qxl_output->enc;
1012}
1013
1014static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
1015	.get_modes = qxl_conn_get_modes,
1016	.mode_valid = qxl_conn_mode_valid,
1017	.best_encoder = qxl_best_encoder,
1018};
1019
1020static enum drm_connector_status qxl_conn_detect(
1021			struct drm_connector *connector,
1022			bool force)
1023{
1024	struct qxl_output *output =
1025		drm_connector_to_qxl_output(connector);
1026	struct drm_device *ddev = connector->dev;
1027	struct qxl_device *qdev = to_qxl(ddev);
1028	bool connected = false;
1029
1030	/* The first monitor is always connected */
1031	if (!qdev->client_monitors_config) {
1032		if (output->index == 0)
1033			connected = true;
1034	} else
1035		connected = qdev->client_monitors_config->count > output->index &&
1036		     qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
1037
1038	DRM_DEBUG("#%d connected: %d\n", output->index, connected);
1039
1040	return connected ? connector_status_connected
1041			 : connector_status_disconnected;
1042}
1043
1044static void qxl_conn_destroy(struct drm_connector *connector)
1045{
1046	struct qxl_output *qxl_output =
1047		drm_connector_to_qxl_output(connector);
1048
1049	drm_connector_unregister(connector);
1050	drm_connector_cleanup(connector);
1051	kfree(qxl_output);
1052}
1053
1054static const struct drm_connector_funcs qxl_connector_funcs = {
1055	.detect = qxl_conn_detect,
1056	.fill_modes = drm_helper_probe_single_connector_modes,
1057	.destroy = qxl_conn_destroy,
1058	.reset = drm_atomic_helper_connector_reset,
1059	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1060	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1061};
1062
1063static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
1064{
1065	if (qdev->hotplug_mode_update_property)
1066		return 0;
1067
1068	qdev->hotplug_mode_update_property =
1069		drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
1070					  "hotplug_mode_update", 0, 1);
1071
1072	return 0;
1073}
1074
1075static int qdev_output_init(struct drm_device *dev, int num_output)
1076{
1077	struct qxl_device *qdev = to_qxl(dev);
1078	struct qxl_output *qxl_output;
1079	struct drm_connector *connector;
1080	struct drm_encoder *encoder;
1081	int ret;
1082
1083	qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
1084	if (!qxl_output)
1085		return -ENOMEM;
1086
1087	qxl_output->index = num_output;
1088
1089	connector = &qxl_output->base;
1090	encoder = &qxl_output->enc;
1091	drm_connector_init(dev, &qxl_output->base,
1092			   &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
1093
1094	ret = drm_simple_encoder_init(dev, &qxl_output->enc,
1095				      DRM_MODE_ENCODER_VIRTUAL);
1096	if (ret) {
1097		drm_err(dev, "drm_simple_encoder_init() failed, error %d\n",
1098			ret);
1099		goto err_drm_connector_cleanup;
1100	}
1101
1102	/* we get HPD via client monitors config */
1103	connector->polled = DRM_CONNECTOR_POLL_HPD;
1104	encoder->possible_crtcs = 1 << num_output;
1105	drm_connector_attach_encoder(&qxl_output->base,
1106					  &qxl_output->enc);
1107	drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
1108
1109	drm_object_attach_property(&connector->base,
1110				   qdev->hotplug_mode_update_property, 0);
1111	drm_object_attach_property(&connector->base,
1112				   dev->mode_config.suggested_x_property, 0);
1113	drm_object_attach_property(&connector->base,
1114				   dev->mode_config.suggested_y_property, 0);
1115	return 0;
1116
1117err_drm_connector_cleanup:
1118	drm_connector_cleanup(&qxl_output->base);
1119	kfree(qxl_output);
1120	return ret;
1121}
1122
1123static struct drm_framebuffer *
1124qxl_user_framebuffer_create(struct drm_device *dev,
1125			    struct drm_file *file_priv,
1126			    const struct drm_mode_fb_cmd2 *mode_cmd)
1127{
1128	return drm_gem_fb_create_with_funcs(dev, file_priv, mode_cmd,
1129					    &qxl_fb_funcs);
1130}
1131
1132static const struct drm_mode_config_funcs qxl_mode_funcs = {
1133	.fb_create = qxl_user_framebuffer_create,
1134	.atomic_check = drm_atomic_helper_check,
1135	.atomic_commit = drm_atomic_helper_commit,
1136};
1137
1138int qxl_create_monitors_object(struct qxl_device *qdev)
1139{
1140	int ret;
1141	struct drm_gem_object *gobj;
1142	int monitors_config_size = sizeof(struct qxl_monitors_config) +
1143		qxl_num_crtc * sizeof(struct qxl_head);
1144
1145	ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1146				    QXL_GEM_DOMAIN_VRAM,
1147				    false, false, NULL, &gobj);
1148	if (ret) {
1149		DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1150		return -ENOMEM;
1151	}
1152	qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
1153
1154	ret = qxl_bo_pin(qdev->monitors_config_bo);
1155	if (ret)
1156		return ret;
1157
1158	qxl_bo_kmap(qdev->monitors_config_bo, NULL);
1159
1160	qdev->monitors_config = qdev->monitors_config_bo->kptr;
1161	qdev->ram_header->monitors_config =
1162		qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1163
1164	memset(qdev->monitors_config, 0, monitors_config_size);
1165	qdev->dumb_heads = kcalloc(qxl_num_crtc, sizeof(qdev->dumb_heads[0]),
1166				   GFP_KERNEL);
1167	if (!qdev->dumb_heads) {
1168		qxl_destroy_monitors_object(qdev);
1169		return -ENOMEM;
1170	}
1171	return 0;
1172}
1173
1174int qxl_destroy_monitors_object(struct qxl_device *qdev)
1175{
1176	int ret;
1177
1178	qdev->monitors_config = NULL;
1179	qdev->ram_header->monitors_config = 0;
1180
1181	qxl_bo_kunmap(qdev->monitors_config_bo);
1182	ret = qxl_bo_unpin(qdev->monitors_config_bo);
1183	if (ret)
1184		return ret;
1185
1186	qxl_bo_unref(&qdev->monitors_config_bo);
1187	return 0;
1188}
1189
1190int qxl_modeset_init(struct qxl_device *qdev)
1191{
1192	int i;
1193	int ret;
1194
1195	drm_mode_config_init(&qdev->ddev);
1196
1197	ret = qxl_create_monitors_object(qdev);
1198	if (ret)
1199		return ret;
1200
1201	qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs;
1202
1203	/* modes will be validated against the framebuffer size */
1204	qdev->ddev.mode_config.min_width = 0;
1205	qdev->ddev.mode_config.min_height = 0;
1206	qdev->ddev.mode_config.max_width = 8192;
1207	qdev->ddev.mode_config.max_height = 8192;
1208
1209	qdev->ddev.mode_config.fb_base = qdev->vram_base;
1210
1211	drm_mode_create_suggested_offset_properties(&qdev->ddev);
1212	qxl_mode_create_hotplug_mode_update_property(qdev);
1213
1214	for (i = 0 ; i < qxl_num_crtc; ++i) {
1215		qdev_crtc_init(&qdev->ddev, i);
1216		qdev_output_init(&qdev->ddev, i);
1217	}
1218
1219	qxl_display_read_client_monitors_config(qdev);
1220
1221	drm_mode_config_reset(&qdev->ddev);
1222	return 0;
1223}
1224
1225void qxl_modeset_fini(struct qxl_device *qdev)
1226{
1227	if (qdev->dumb_shadow_bo) {
1228		drm_gem_object_put(&qdev->dumb_shadow_bo->tbo.base);
1229		qdev->dumb_shadow_bo = NULL;
1230	}
1231	qxl_destroy_monitors_object(qdev);
1232	drm_mode_config_cleanup(&qdev->ddev);
1233}
1234