xref: /openbmc/linux/drivers/gpu/drm/qxl/qxl_display.c (revision 2bd6ce84e1b4799be1f328a165d0b8a4fdfd2141)
1f64122c1SDave Airlie /*
2f64122c1SDave Airlie  * Copyright 2013 Red Hat Inc.
3f64122c1SDave Airlie  *
4f64122c1SDave Airlie  * Permission is hereby granted, free of charge, to any person obtaining a
5f64122c1SDave Airlie  * copy of this software and associated documentation files (the "Software"),
6f64122c1SDave Airlie  * to deal in the Software without restriction, including without limitation
7f64122c1SDave Airlie  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8f64122c1SDave Airlie  * and/or sell copies of the Software, and to permit persons to whom the
9f64122c1SDave Airlie  * Software is furnished to do so, subject to the following conditions:
10f64122c1SDave Airlie  *
11f64122c1SDave Airlie  * The above copyright notice and this permission notice shall be included in
12f64122c1SDave Airlie  * all copies or substantial portions of the Software.
13f64122c1SDave Airlie  *
14f64122c1SDave Airlie  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15f64122c1SDave Airlie  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16f64122c1SDave Airlie  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17f64122c1SDave Airlie  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18f64122c1SDave Airlie  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19f64122c1SDave Airlie  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20f64122c1SDave Airlie  * OTHER DEALINGS IN THE SOFTWARE.
21f64122c1SDave Airlie  *
22f64122c1SDave Airlie  * Authors: Dave Airlie
23f64122c1SDave Airlie  *          Alon Levy
24f64122c1SDave Airlie  */
25f64122c1SDave Airlie 
26f64122c1SDave Airlie 
27f64122c1SDave Airlie #include "linux/crc32.h"
28f64122c1SDave Airlie 
29f64122c1SDave Airlie #include "qxl_drv.h"
30f64122c1SDave Airlie #include "qxl_object.h"
31f64122c1SDave Airlie #include "drm_crtc_helper.h"
32f64122c1SDave Airlie 
3307f8d9bdSDave Airlie static bool qxl_head_enabled(struct qxl_head *head)
3407f8d9bdSDave Airlie {
3507f8d9bdSDave Airlie 	return head->width && head->height;
3607f8d9bdSDave Airlie }
3707f8d9bdSDave Airlie 
38f64122c1SDave Airlie void qxl_alloc_client_monitors_config(struct qxl_device *qdev, unsigned count)
39f64122c1SDave Airlie {
40f64122c1SDave Airlie 	if (qdev->client_monitors_config &&
41f64122c1SDave Airlie 	    count > qdev->client_monitors_config->count) {
42f64122c1SDave Airlie 		kfree(qdev->client_monitors_config);
4362c8ba7cSDave Airlie 		qdev->client_monitors_config = NULL;
44f64122c1SDave Airlie 	}
45f64122c1SDave Airlie 	if (!qdev->client_monitors_config) {
46f64122c1SDave Airlie 		qdev->client_monitors_config = kzalloc(
47f64122c1SDave Airlie 				sizeof(struct qxl_monitors_config) +
48f64122c1SDave Airlie 				sizeof(struct qxl_head) * count, GFP_KERNEL);
49f64122c1SDave Airlie 		if (!qdev->client_monitors_config) {
50f64122c1SDave Airlie 			qxl_io_log(qdev,
51f64122c1SDave Airlie 				   "%s: allocation failure for %u heads\n",
52f64122c1SDave Airlie 				   __func__, count);
53f64122c1SDave Airlie 			return;
54f64122c1SDave Airlie 		}
55f64122c1SDave Airlie 	}
56f64122c1SDave Airlie 	qdev->client_monitors_config->count = count;
57f64122c1SDave Airlie }
58f64122c1SDave Airlie 
59f64122c1SDave Airlie static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
60f64122c1SDave Airlie {
61f64122c1SDave Airlie 	int i;
62f64122c1SDave Airlie 	int num_monitors;
63f64122c1SDave Airlie 	uint32_t crc;
64f64122c1SDave Airlie 
65f64122c1SDave Airlie 	num_monitors = qdev->rom->client_monitors_config.count;
66f64122c1SDave Airlie 	crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
67f64122c1SDave Airlie 		  sizeof(qdev->rom->client_monitors_config));
68f64122c1SDave Airlie 	if (crc != qdev->rom->client_monitors_config_crc) {
69f64122c1SDave Airlie 		qxl_io_log(qdev, "crc mismatch: have %X (%d) != %X\n", crc,
70f64122c1SDave Airlie 			   sizeof(qdev->rom->client_monitors_config),
71f64122c1SDave Airlie 			   qdev->rom->client_monitors_config_crc);
72f64122c1SDave Airlie 		return 1;
73f64122c1SDave Airlie 	}
74f64122c1SDave Airlie 	if (num_monitors > qdev->monitors_config->max_allowed) {
755b8788c1SDave Airlie 		DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
76f64122c1SDave Airlie 			      qdev->monitors_config->max_allowed, num_monitors);
77f64122c1SDave Airlie 		num_monitors = qdev->monitors_config->max_allowed;
78f64122c1SDave Airlie 	} else {
79f64122c1SDave Airlie 		num_monitors = qdev->rom->client_monitors_config.count;
80f64122c1SDave Airlie 	}
81f64122c1SDave Airlie 	qxl_alloc_client_monitors_config(qdev, num_monitors);
82f64122c1SDave Airlie 	/* we copy max from the client but it isn't used */
83f64122c1SDave Airlie 	qdev->client_monitors_config->max_allowed =
84f64122c1SDave Airlie 				qdev->monitors_config->max_allowed;
85f64122c1SDave Airlie 	for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
86f64122c1SDave Airlie 		struct qxl_urect *c_rect =
87f64122c1SDave Airlie 			&qdev->rom->client_monitors_config.heads[i];
88f64122c1SDave Airlie 		struct qxl_head *client_head =
89f64122c1SDave Airlie 			&qdev->client_monitors_config->heads[i];
9007f8d9bdSDave Airlie 		client_head->x = c_rect->left;
9107f8d9bdSDave Airlie 		client_head->y = c_rect->top;
9207f8d9bdSDave Airlie 		client_head->width = c_rect->right - c_rect->left;
9307f8d9bdSDave Airlie 		client_head->height = c_rect->bottom - c_rect->top;
9407f8d9bdSDave Airlie 		client_head->surface_id = 0;
9507f8d9bdSDave Airlie 		client_head->id = i;
9607f8d9bdSDave Airlie 		client_head->flags = 0;
9707f8d9bdSDave Airlie 		DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
9807f8d9bdSDave Airlie 			  client_head->x, client_head->y);
99f64122c1SDave Airlie 	}
100f64122c1SDave Airlie 	return 0;
101f64122c1SDave Airlie }
102f64122c1SDave Airlie 
103f64122c1SDave Airlie void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
104f64122c1SDave Airlie {
105f64122c1SDave Airlie 
106f64122c1SDave Airlie 	while (qxl_display_copy_rom_client_monitors_config(qdev)) {
107f64122c1SDave Airlie 		qxl_io_log(qdev, "failed crc check for client_monitors_config,"
108f64122c1SDave Airlie 				 " retrying\n");
109f64122c1SDave Airlie 	}
110f64122c1SDave Airlie 	drm_sysfs_hotplug_event(qdev->ddev);
111f64122c1SDave Airlie }
112f64122c1SDave Airlie 
113f64122c1SDave Airlie static int qxl_add_monitors_config_modes(struct drm_connector *connector)
114f64122c1SDave Airlie {
115f64122c1SDave Airlie 	struct drm_device *dev = connector->dev;
116f64122c1SDave Airlie 	struct qxl_device *qdev = dev->dev_private;
117f64122c1SDave Airlie 	struct qxl_output *output = drm_connector_to_qxl_output(connector);
118f64122c1SDave Airlie 	int h = output->index;
119f64122c1SDave Airlie 	struct drm_display_mode *mode = NULL;
120f64122c1SDave Airlie 	struct qxl_head *head;
121f64122c1SDave Airlie 
12207f8d9bdSDave Airlie 	if (!qdev->client_monitors_config)
123f64122c1SDave Airlie 		return 0;
12407f8d9bdSDave Airlie 	head = &qdev->client_monitors_config->heads[h];
125f64122c1SDave Airlie 
126f64122c1SDave Airlie 	mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false,
127f64122c1SDave Airlie 			    false);
128f64122c1SDave Airlie 	mode->type |= DRM_MODE_TYPE_PREFERRED;
129f64122c1SDave Airlie 	drm_mode_probed_add(connector, mode);
130f64122c1SDave Airlie 	return 1;
131f64122c1SDave Airlie }
132f64122c1SDave Airlie 
133f64122c1SDave Airlie static int qxl_add_common_modes(struct drm_connector *connector)
134f64122c1SDave Airlie {
135f64122c1SDave Airlie 	struct drm_device *dev = connector->dev;
136f64122c1SDave Airlie 	struct drm_display_mode *mode = NULL;
137f64122c1SDave Airlie 	int i;
138f64122c1SDave Airlie 	struct mode_size {
139f64122c1SDave Airlie 		int w;
140f64122c1SDave Airlie 		int h;
141f64122c1SDave Airlie 	} common_modes[] = {
142f64122c1SDave Airlie 		{ 640,  480},
143f64122c1SDave Airlie 		{ 720,  480},
144f64122c1SDave Airlie 		{ 800,  600},
145f64122c1SDave Airlie 		{ 848,  480},
146f64122c1SDave Airlie 		{1024,  768},
147f64122c1SDave Airlie 		{1152,  768},
148f64122c1SDave Airlie 		{1280,  720},
149f64122c1SDave Airlie 		{1280,  800},
150f64122c1SDave Airlie 		{1280,  854},
151f64122c1SDave Airlie 		{1280,  960},
152f64122c1SDave Airlie 		{1280, 1024},
153f64122c1SDave Airlie 		{1440,  900},
154f64122c1SDave Airlie 		{1400, 1050},
155f64122c1SDave Airlie 		{1680, 1050},
156f64122c1SDave Airlie 		{1600, 1200},
157f64122c1SDave Airlie 		{1920, 1080},
158f64122c1SDave Airlie 		{1920, 1200}
159f64122c1SDave Airlie 	};
160f64122c1SDave Airlie 
161f64122c1SDave Airlie 	for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
162f64122c1SDave Airlie 		if (common_modes[i].w < 320 || common_modes[i].h < 200)
163f64122c1SDave Airlie 			continue;
164f64122c1SDave Airlie 
165f64122c1SDave Airlie 		mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h,
166f64122c1SDave Airlie 				    60, false, false, false);
167f64122c1SDave Airlie 		if (common_modes[i].w == 1024 && common_modes[i].h == 768)
168f64122c1SDave Airlie 			mode->type |= DRM_MODE_TYPE_PREFERRED;
169f64122c1SDave Airlie 		drm_mode_probed_add(connector, mode);
170f64122c1SDave Airlie 	}
171f64122c1SDave Airlie 	return i - 1;
172f64122c1SDave Airlie }
173f64122c1SDave Airlie 
174f64122c1SDave Airlie static void qxl_crtc_destroy(struct drm_crtc *crtc)
175f64122c1SDave Airlie {
176f64122c1SDave Airlie 	struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
177f64122c1SDave Airlie 
178f64122c1SDave Airlie 	drm_crtc_cleanup(crtc);
179f64122c1SDave Airlie 	kfree(qxl_crtc);
180f64122c1SDave Airlie }
181f64122c1SDave Airlie 
182f64122c1SDave Airlie static void
183f64122c1SDave Airlie qxl_hide_cursor(struct qxl_device *qdev)
184f64122c1SDave Airlie {
185f64122c1SDave Airlie 	struct qxl_release *release;
186f64122c1SDave Airlie 	struct qxl_cursor_cmd *cmd;
187f64122c1SDave Airlie 	int ret;
188f64122c1SDave Airlie 
189f64122c1SDave Airlie 	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
190f64122c1SDave Airlie 					 &release, NULL);
191f64122c1SDave Airlie 
192f64122c1SDave Airlie 	cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
193f64122c1SDave Airlie 	cmd->type = QXL_CURSOR_HIDE;
194f64122c1SDave Airlie 	qxl_release_unmap(qdev, release, &cmd->release_info);
195f64122c1SDave Airlie 
196f64122c1SDave Airlie 	qxl_fence_releaseable(qdev, release);
197f64122c1SDave Airlie 	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
198f64122c1SDave Airlie 	qxl_release_unreserve(qdev, release);
199f64122c1SDave Airlie }
200f64122c1SDave Airlie 
201c0a60802SDave Airlie static int qxl_crtc_cursor_set2(struct drm_crtc *crtc,
202f64122c1SDave Airlie 				struct drm_file *file_priv,
203f64122c1SDave Airlie 				uint32_t handle,
204f64122c1SDave Airlie 				uint32_t width,
205c0a60802SDave Airlie 				uint32_t height, int32_t hot_x, int32_t hot_y)
206f64122c1SDave Airlie {
207f64122c1SDave Airlie 	struct drm_device *dev = crtc->dev;
208f64122c1SDave Airlie 	struct qxl_device *qdev = dev->dev_private;
209f64122c1SDave Airlie 	struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
210f64122c1SDave Airlie 	struct drm_gem_object *obj;
211f64122c1SDave Airlie 	struct qxl_cursor *cursor;
212f64122c1SDave Airlie 	struct qxl_cursor_cmd *cmd;
213f64122c1SDave Airlie 	struct qxl_bo *cursor_bo, *user_bo;
214f64122c1SDave Airlie 	struct qxl_release *release;
215f64122c1SDave Airlie 	void *user_ptr;
216f64122c1SDave Airlie 
217f64122c1SDave Airlie 	int size = 64*64*4;
218f64122c1SDave Airlie 	int ret = 0;
219f64122c1SDave Airlie 	if (!handle) {
220f64122c1SDave Airlie 		qxl_hide_cursor(qdev);
221f64122c1SDave Airlie 		return 0;
222f64122c1SDave Airlie 	}
223f64122c1SDave Airlie 
224f64122c1SDave Airlie 	obj = drm_gem_object_lookup(crtc->dev, file_priv, handle);
225f64122c1SDave Airlie 	if (!obj) {
226f64122c1SDave Airlie 		DRM_ERROR("cannot find cursor object\n");
227f64122c1SDave Airlie 		return -ENOENT;
228f64122c1SDave Airlie 	}
229f64122c1SDave Airlie 
230f64122c1SDave Airlie 	user_bo = gem_to_qxl_bo(obj);
231f64122c1SDave Airlie 
232f64122c1SDave Airlie 	ret = qxl_bo_reserve(user_bo, false);
233f64122c1SDave Airlie 	if (ret)
234f64122c1SDave Airlie 		goto out_unref;
235f64122c1SDave Airlie 
236f64122c1SDave Airlie 	ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
237f64122c1SDave Airlie 	if (ret)
238f64122c1SDave Airlie 		goto out_unreserve;
239f64122c1SDave Airlie 
240f64122c1SDave Airlie 	ret = qxl_bo_kmap(user_bo, &user_ptr);
241f64122c1SDave Airlie 	if (ret)
242f64122c1SDave Airlie 		goto out_unpin;
243f64122c1SDave Airlie 
244f64122c1SDave Airlie 	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
245f64122c1SDave Airlie 					 QXL_RELEASE_CURSOR_CMD,
246f64122c1SDave Airlie 					 &release, NULL);
247f64122c1SDave Airlie 	if (ret)
248f64122c1SDave Airlie 		goto out_kunmap;
249f64122c1SDave Airlie 	ret = qxl_alloc_bo_reserved(qdev, sizeof(struct qxl_cursor) + size,
250f64122c1SDave Airlie 				    &cursor_bo);
251f64122c1SDave Airlie 	if (ret)
252f64122c1SDave Airlie 		goto out_free_release;
253f64122c1SDave Airlie 	ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
254f64122c1SDave Airlie 	if (ret)
255f64122c1SDave Airlie 		goto out_free_bo;
256f64122c1SDave Airlie 
257f64122c1SDave Airlie 	cursor->header.unique = 0;
258f64122c1SDave Airlie 	cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
259f64122c1SDave Airlie 	cursor->header.width = 64;
260f64122c1SDave Airlie 	cursor->header.height = 64;
261c0a60802SDave Airlie 	cursor->header.hot_spot_x = hot_x;
262c0a60802SDave Airlie 	cursor->header.hot_spot_y = hot_y;
263f64122c1SDave Airlie 	cursor->data_size = size;
264f64122c1SDave Airlie 	cursor->chunk.next_chunk = 0;
265f64122c1SDave Airlie 	cursor->chunk.prev_chunk = 0;
266f64122c1SDave Airlie 	cursor->chunk.data_size = size;
267f64122c1SDave Airlie 
268f64122c1SDave Airlie 	memcpy(cursor->chunk.data, user_ptr, size);
269f64122c1SDave Airlie 
270f64122c1SDave Airlie 	qxl_bo_kunmap(cursor_bo);
271f64122c1SDave Airlie 
272f64122c1SDave Airlie 	/* finish with the userspace bo */
273f64122c1SDave Airlie 	qxl_bo_kunmap(user_bo);
274f64122c1SDave Airlie 	qxl_bo_unpin(user_bo);
275f64122c1SDave Airlie 	qxl_bo_unreserve(user_bo);
276f64122c1SDave Airlie 	drm_gem_object_unreference_unlocked(obj);
277f64122c1SDave Airlie 
278f64122c1SDave Airlie 	cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
279f64122c1SDave Airlie 	cmd->type = QXL_CURSOR_SET;
280f64122c1SDave Airlie 	cmd->u.set.position.x = qcrtc->cur_x;
281f64122c1SDave Airlie 	cmd->u.set.position.y = qcrtc->cur_y;
282f64122c1SDave Airlie 
283f64122c1SDave Airlie 	cmd->u.set.shape = qxl_bo_physical_address(qdev, cursor_bo, 0);
284f64122c1SDave Airlie 	qxl_release_add_res(qdev, release, cursor_bo);
285f64122c1SDave Airlie 
286f64122c1SDave Airlie 	cmd->u.set.visible = 1;
287f64122c1SDave Airlie 	qxl_release_unmap(qdev, release, &cmd->release_info);
288f64122c1SDave Airlie 
289f64122c1SDave Airlie 	qxl_fence_releaseable(qdev, release);
290f64122c1SDave Airlie 	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
291f64122c1SDave Airlie 	qxl_release_unreserve(qdev, release);
292f64122c1SDave Airlie 
293f64122c1SDave Airlie 	qxl_bo_unreserve(cursor_bo);
294f64122c1SDave Airlie 	qxl_bo_unref(&cursor_bo);
295f64122c1SDave Airlie 
296f64122c1SDave Airlie 	return ret;
297f64122c1SDave Airlie out_free_bo:
298f64122c1SDave Airlie 	qxl_bo_unref(&cursor_bo);
299f64122c1SDave Airlie out_free_release:
300f64122c1SDave Airlie 	qxl_release_unreserve(qdev, release);
301f64122c1SDave Airlie 	qxl_release_free(qdev, release);
302f64122c1SDave Airlie out_kunmap:
303f64122c1SDave Airlie 	qxl_bo_kunmap(user_bo);
304f64122c1SDave Airlie out_unpin:
305f64122c1SDave Airlie 	qxl_bo_unpin(user_bo);
306f64122c1SDave Airlie out_unreserve:
307f64122c1SDave Airlie 	qxl_bo_unreserve(user_bo);
308f64122c1SDave Airlie out_unref:
309f64122c1SDave Airlie 	drm_gem_object_unreference_unlocked(obj);
310f64122c1SDave Airlie 	return ret;
311f64122c1SDave Airlie }
312f64122c1SDave Airlie 
313f64122c1SDave Airlie static int qxl_crtc_cursor_move(struct drm_crtc *crtc,
314f64122c1SDave Airlie 				int x, int y)
315f64122c1SDave Airlie {
316f64122c1SDave Airlie 	struct drm_device *dev = crtc->dev;
317f64122c1SDave Airlie 	struct qxl_device *qdev = dev->dev_private;
318f64122c1SDave Airlie 	struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
319f64122c1SDave Airlie 	struct qxl_release *release;
320f64122c1SDave Airlie 	struct qxl_cursor_cmd *cmd;
321f64122c1SDave Airlie 	int ret;
322f64122c1SDave Airlie 
323f64122c1SDave Airlie 	ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
324f64122c1SDave Airlie 				   &release, NULL);
325f64122c1SDave Airlie 
326f64122c1SDave Airlie 	qcrtc->cur_x = x;
327f64122c1SDave Airlie 	qcrtc->cur_y = y;
328f64122c1SDave Airlie 
329f64122c1SDave Airlie 	cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
330f64122c1SDave Airlie 	cmd->type = QXL_CURSOR_MOVE;
331f64122c1SDave Airlie 	cmd->u.position.x = qcrtc->cur_x;
332f64122c1SDave Airlie 	cmd->u.position.y = qcrtc->cur_y;
333f64122c1SDave Airlie 	qxl_release_unmap(qdev, release, &cmd->release_info);
334f64122c1SDave Airlie 
335f64122c1SDave Airlie 	qxl_fence_releaseable(qdev, release);
336f64122c1SDave Airlie 	qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
337f64122c1SDave Airlie 	qxl_release_unreserve(qdev, release);
338f64122c1SDave Airlie 	return 0;
339f64122c1SDave Airlie }
340f64122c1SDave Airlie 
341f64122c1SDave Airlie 
342f64122c1SDave Airlie static const struct drm_crtc_funcs qxl_crtc_funcs = {
343c0a60802SDave Airlie 	.cursor_set2 = qxl_crtc_cursor_set2,
344f64122c1SDave Airlie 	.cursor_move = qxl_crtc_cursor_move,
345f64122c1SDave Airlie 	.set_config = drm_crtc_helper_set_config,
346f64122c1SDave Airlie 	.destroy = qxl_crtc_destroy,
347f64122c1SDave Airlie };
348f64122c1SDave Airlie 
349f64122c1SDave Airlie static void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
350f64122c1SDave Airlie {
351f64122c1SDave Airlie 	struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
352f64122c1SDave Airlie 
353f64122c1SDave Airlie 	if (qxl_fb->obj)
354f64122c1SDave Airlie 		drm_gem_object_unreference_unlocked(qxl_fb->obj);
355f64122c1SDave Airlie 	drm_framebuffer_cleanup(fb);
356f64122c1SDave Airlie 	kfree(qxl_fb);
357f64122c1SDave Airlie }
358f64122c1SDave Airlie 
3596d01f1f5SDave Airlie static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
360f64122c1SDave Airlie 					 struct drm_file *file_priv,
361f64122c1SDave Airlie 					 unsigned flags, unsigned color,
362f64122c1SDave Airlie 					 struct drm_clip_rect *clips,
363f64122c1SDave Airlie 					 unsigned num_clips)
364f64122c1SDave Airlie {
365f64122c1SDave Airlie 	/* TODO: vmwgfx where this was cribbed from had locking. Why? */
366f64122c1SDave Airlie 	struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
367f64122c1SDave Airlie 	struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
368f64122c1SDave Airlie 	struct drm_clip_rect norect;
369f64122c1SDave Airlie 	struct qxl_bo *qobj;
370f64122c1SDave Airlie 	int inc = 1;
371f64122c1SDave Airlie 
372f64122c1SDave Airlie 	qobj = gem_to_qxl_bo(qxl_fb->obj);
373b2b4465dSDave Airlie 	/* if we aren't primary surface ignore this */
374b2b4465dSDave Airlie 	if (!qobj->is_primary)
375b2b4465dSDave Airlie 		return 0;
376b2b4465dSDave Airlie 
377f64122c1SDave Airlie 	if (!num_clips) {
378f64122c1SDave Airlie 		num_clips = 1;
379f64122c1SDave Airlie 		clips = &norect;
380f64122c1SDave Airlie 		norect.x1 = norect.y1 = 0;
381f64122c1SDave Airlie 		norect.x2 = fb->width;
382f64122c1SDave Airlie 		norect.y2 = fb->height;
383f64122c1SDave Airlie 	} else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
384f64122c1SDave Airlie 		num_clips /= 2;
385f64122c1SDave Airlie 		inc = 2; /* skip source rects */
386f64122c1SDave Airlie 	}
387f64122c1SDave Airlie 
388f64122c1SDave Airlie 	qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
389f64122c1SDave Airlie 			  clips, num_clips, inc);
390f64122c1SDave Airlie 	return 0;
391f64122c1SDave Airlie }
392f64122c1SDave Airlie 
393f64122c1SDave Airlie static const struct drm_framebuffer_funcs qxl_fb_funcs = {
394f64122c1SDave Airlie 	.destroy = qxl_user_framebuffer_destroy,
395f64122c1SDave Airlie 	.dirty = qxl_framebuffer_surface_dirty,
396f64122c1SDave Airlie /*	TODO?
397f64122c1SDave Airlie  *	.create_handle = qxl_user_framebuffer_create_handle, */
398f64122c1SDave Airlie };
399f64122c1SDave Airlie 
400f64122c1SDave Airlie int
401f64122c1SDave Airlie qxl_framebuffer_init(struct drm_device *dev,
402f64122c1SDave Airlie 		     struct qxl_framebuffer *qfb,
403f64122c1SDave Airlie 		     struct drm_mode_fb_cmd2 *mode_cmd,
404f64122c1SDave Airlie 		     struct drm_gem_object *obj)
405f64122c1SDave Airlie {
406f64122c1SDave Airlie 	int ret;
407f64122c1SDave Airlie 
408f64122c1SDave Airlie 	qfb->obj = obj;
409f64122c1SDave Airlie 	ret = drm_framebuffer_init(dev, &qfb->base, &qxl_fb_funcs);
410f64122c1SDave Airlie 	if (ret) {
411f64122c1SDave Airlie 		qfb->obj = NULL;
412f64122c1SDave Airlie 		return ret;
413f64122c1SDave Airlie 	}
414f64122c1SDave Airlie 	drm_helper_mode_fill_fb_struct(&qfb->base, mode_cmd);
415f64122c1SDave Airlie 	return 0;
416f64122c1SDave Airlie }
417f64122c1SDave Airlie 
418f64122c1SDave Airlie static void qxl_crtc_dpms(struct drm_crtc *crtc, int mode)
419f64122c1SDave Airlie {
420f64122c1SDave Airlie }
421f64122c1SDave Airlie 
422f64122c1SDave Airlie static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
423f64122c1SDave Airlie 				  const struct drm_display_mode *mode,
424f64122c1SDave Airlie 				  struct drm_display_mode *adjusted_mode)
425f64122c1SDave Airlie {
426f64122c1SDave Airlie 	struct drm_device *dev = crtc->dev;
427f64122c1SDave Airlie 	struct qxl_device *qdev = dev->dev_private;
428f64122c1SDave Airlie 
429f64122c1SDave Airlie 	qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n",
430f64122c1SDave Airlie 		   __func__,
431f64122c1SDave Airlie 		   mode->hdisplay, mode->vdisplay,
432f64122c1SDave Airlie 		   adjusted_mode->hdisplay,
433f64122c1SDave Airlie 		   adjusted_mode->vdisplay);
434f64122c1SDave Airlie 	return true;
435f64122c1SDave Airlie }
436f64122c1SDave Airlie 
437f64122c1SDave Airlie void
438f64122c1SDave Airlie qxl_send_monitors_config(struct qxl_device *qdev)
439f64122c1SDave Airlie {
440f64122c1SDave Airlie 	int i;
441f64122c1SDave Airlie 
442f64122c1SDave Airlie 	BUG_ON(!qdev->ram_header->monitors_config);
443f64122c1SDave Airlie 
444f64122c1SDave Airlie 	if (qdev->monitors_config->count == 0) {
445f64122c1SDave Airlie 		qxl_io_log(qdev, "%s: 0 monitors??\n", __func__);
446f64122c1SDave Airlie 		return;
447f64122c1SDave Airlie 	}
448f64122c1SDave Airlie 	for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
449f64122c1SDave Airlie 		struct qxl_head *head = &qdev->monitors_config->heads[i];
450f64122c1SDave Airlie 
45107f8d9bdSDave Airlie 		if (head->y > 8192 || head->x > 8192 ||
452f64122c1SDave Airlie 		    head->width > 8192 || head->height > 8192) {
453f64122c1SDave Airlie 			DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
454f64122c1SDave Airlie 				  i, head->width, head->height,
455f64122c1SDave Airlie 				  head->x, head->y);
456f64122c1SDave Airlie 			return;
457f64122c1SDave Airlie 		}
458f64122c1SDave Airlie 	}
459f64122c1SDave Airlie 	qxl_io_monitors_config(qdev);
460f64122c1SDave Airlie }
461f64122c1SDave Airlie 
46207f8d9bdSDave Airlie static void qxl_monitors_config_set(struct qxl_device *qdev,
46307f8d9bdSDave Airlie 				    int index,
464f64122c1SDave Airlie 				    unsigned x, unsigned y,
46507f8d9bdSDave Airlie 				    unsigned width, unsigned height,
46607f8d9bdSDave Airlie 				    unsigned surf_id)
467f64122c1SDave Airlie {
46807f8d9bdSDave Airlie 	DRM_DEBUG_KMS("%d:%dx%d+%d+%d\n", index, width, height, x, y);
46907f8d9bdSDave Airlie 	qdev->monitors_config->heads[index].x = x;
47007f8d9bdSDave Airlie 	qdev->monitors_config->heads[index].y = y;
47107f8d9bdSDave Airlie 	qdev->monitors_config->heads[index].width = width;
47207f8d9bdSDave Airlie 	qdev->monitors_config->heads[index].height = height;
47307f8d9bdSDave Airlie 	qdev->monitors_config->heads[index].surface_id = surf_id;
47407f8d9bdSDave Airlie 
475f64122c1SDave Airlie }
476f64122c1SDave Airlie 
477f64122c1SDave Airlie static int qxl_crtc_mode_set(struct drm_crtc *crtc,
478f64122c1SDave Airlie 			       struct drm_display_mode *mode,
479f64122c1SDave Airlie 			       struct drm_display_mode *adjusted_mode,
480f64122c1SDave Airlie 			       int x, int y,
481f64122c1SDave Airlie 			       struct drm_framebuffer *old_fb)
482f64122c1SDave Airlie {
483f64122c1SDave Airlie 	struct drm_device *dev = crtc->dev;
484f64122c1SDave Airlie 	struct qxl_device *qdev = dev->dev_private;
485f64122c1SDave Airlie 	struct qxl_mode *m = (void *)mode->private;
486f64122c1SDave Airlie 	struct qxl_framebuffer *qfb;
487f64122c1SDave Airlie 	struct qxl_bo *bo, *old_bo = NULL;
48807f8d9bdSDave Airlie 	struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
489f64122c1SDave Airlie 	uint32_t width, height, base_offset;
490f64122c1SDave Airlie 	bool recreate_primary = false;
491f64122c1SDave Airlie 	int ret;
49207f8d9bdSDave Airlie 	int surf_id;
493f64122c1SDave Airlie 	if (!crtc->fb) {
494f64122c1SDave Airlie 		DRM_DEBUG_KMS("No FB bound\n");
495f64122c1SDave Airlie 		return 0;
496f64122c1SDave Airlie 	}
497f64122c1SDave Airlie 
498f64122c1SDave Airlie 	if (old_fb) {
499f64122c1SDave Airlie 		qfb = to_qxl_framebuffer(old_fb);
500f64122c1SDave Airlie 		old_bo = gem_to_qxl_bo(qfb->obj);
501f64122c1SDave Airlie 	}
502f64122c1SDave Airlie 	qfb = to_qxl_framebuffer(crtc->fb);
503f64122c1SDave Airlie 	bo = gem_to_qxl_bo(qfb->obj);
504f64122c1SDave Airlie 	if (!m)
505f64122c1SDave Airlie 		/* and do we care? */
506f64122c1SDave Airlie 		DRM_DEBUG("%dx%d: not a native mode\n", x, y);
507f64122c1SDave Airlie 	else
508f64122c1SDave Airlie 		DRM_DEBUG("%dx%d: qxl id %d\n",
509f64122c1SDave Airlie 			  mode->hdisplay, mode->vdisplay, m->id);
510f64122c1SDave Airlie 	DRM_DEBUG("+%d+%d (%d,%d) => (%d,%d)\n",
511f64122c1SDave Airlie 		  x, y,
512f64122c1SDave Airlie 		  mode->hdisplay, mode->vdisplay,
513f64122c1SDave Airlie 		  adjusted_mode->hdisplay,
514f64122c1SDave Airlie 		  adjusted_mode->vdisplay);
515f64122c1SDave Airlie 
51607f8d9bdSDave Airlie 	if (qcrtc->index == 0)
517f64122c1SDave Airlie 		recreate_primary = true;
518f64122c1SDave Airlie 
519f64122c1SDave Airlie 	width = mode->hdisplay;
520f64122c1SDave Airlie 	height = mode->vdisplay;
521f64122c1SDave Airlie 	base_offset = 0;
522f64122c1SDave Airlie 
523f64122c1SDave Airlie 	ret = qxl_bo_reserve(bo, false);
524f64122c1SDave Airlie 	if (ret != 0)
525f64122c1SDave Airlie 		return ret;
526f64122c1SDave Airlie 	ret = qxl_bo_pin(bo, bo->type, NULL);
527f64122c1SDave Airlie 	if (ret != 0) {
528f64122c1SDave Airlie 		qxl_bo_unreserve(bo);
529f64122c1SDave Airlie 		return -EINVAL;
530f64122c1SDave Airlie 	}
531f64122c1SDave Airlie 	qxl_bo_unreserve(bo);
532f64122c1SDave Airlie 	if (recreate_primary) {
533f64122c1SDave Airlie 		qxl_io_destroy_primary(qdev);
534f64122c1SDave Airlie 		qxl_io_log(qdev,
535f64122c1SDave Airlie 			   "recreate primary: %dx%d (was %dx%d,%d,%d)\n",
536f64122c1SDave Airlie 			   width, height, bo->surf.width,
537f64122c1SDave Airlie 			   bo->surf.height, bo->surf.stride, bo->surf.format);
53807f8d9bdSDave Airlie 		qxl_io_create_primary(qdev, base_offset, bo);
539f64122c1SDave Airlie 		bo->is_primary = true;
54007f8d9bdSDave Airlie 		surf_id = 0;
54107f8d9bdSDave Airlie 	} else {
54207f8d9bdSDave Airlie 		surf_id = bo->surface_id;
543f64122c1SDave Airlie 	}
544f64122c1SDave Airlie 
545f64122c1SDave Airlie 	if (old_bo && old_bo != bo) {
546f64122c1SDave Airlie 		old_bo->is_primary = false;
547f64122c1SDave Airlie 		ret = qxl_bo_reserve(old_bo, false);
548f64122c1SDave Airlie 		qxl_bo_unpin(old_bo);
549f64122c1SDave Airlie 		qxl_bo_unreserve(old_bo);
550f64122c1SDave Airlie 	}
551f64122c1SDave Airlie 
55207f8d9bdSDave Airlie 	qxl_monitors_config_set(qdev, qcrtc->index, x, y,
553f64122c1SDave Airlie 				mode->hdisplay,
55407f8d9bdSDave Airlie 				mode->vdisplay, surf_id);
555f64122c1SDave Airlie 	return 0;
556f64122c1SDave Airlie }
557f64122c1SDave Airlie 
558f64122c1SDave Airlie static void qxl_crtc_prepare(struct drm_crtc *crtc)
559f64122c1SDave Airlie {
560f64122c1SDave Airlie 	DRM_DEBUG("current: %dx%d+%d+%d (%d).\n",
561f64122c1SDave Airlie 		  crtc->mode.hdisplay, crtc->mode.vdisplay,
562f64122c1SDave Airlie 		  crtc->x, crtc->y, crtc->enabled);
563f64122c1SDave Airlie }
564f64122c1SDave Airlie 
565f64122c1SDave Airlie static void qxl_crtc_commit(struct drm_crtc *crtc)
566f64122c1SDave Airlie {
567f64122c1SDave Airlie 	DRM_DEBUG("\n");
568f64122c1SDave Airlie }
569f64122c1SDave Airlie 
57007f8d9bdSDave Airlie static void qxl_crtc_disable(struct drm_crtc *crtc)
57107f8d9bdSDave Airlie {
57207f8d9bdSDave Airlie 	struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
57307f8d9bdSDave Airlie 	struct drm_device *dev = crtc->dev;
57407f8d9bdSDave Airlie 	struct qxl_device *qdev = dev->dev_private;
57507f8d9bdSDave Airlie 	if (crtc->fb) {
57607f8d9bdSDave Airlie 		struct qxl_framebuffer *qfb = to_qxl_framebuffer(crtc->fb);
57707f8d9bdSDave Airlie 		struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
57807f8d9bdSDave Airlie 		int ret;
57907f8d9bdSDave Airlie 		ret = qxl_bo_reserve(bo, false);
58007f8d9bdSDave Airlie 		qxl_bo_unpin(bo);
58107f8d9bdSDave Airlie 		qxl_bo_unreserve(bo);
58207f8d9bdSDave Airlie 		crtc->fb = NULL;
58307f8d9bdSDave Airlie 	}
58407f8d9bdSDave Airlie 
58507f8d9bdSDave Airlie 	qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, 0, 0, 0);
58607f8d9bdSDave Airlie 
58707f8d9bdSDave Airlie 	qxl_send_monitors_config(qdev);
58807f8d9bdSDave Airlie }
58907f8d9bdSDave Airlie 
590f64122c1SDave Airlie static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
591f64122c1SDave Airlie 	.dpms = qxl_crtc_dpms,
59207f8d9bdSDave Airlie 	.disable = qxl_crtc_disable,
593f64122c1SDave Airlie 	.mode_fixup = qxl_crtc_mode_fixup,
594f64122c1SDave Airlie 	.mode_set = qxl_crtc_mode_set,
595f64122c1SDave Airlie 	.prepare = qxl_crtc_prepare,
596f64122c1SDave Airlie 	.commit = qxl_crtc_commit,
597f64122c1SDave Airlie };
598f64122c1SDave Airlie 
59907f8d9bdSDave Airlie static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
600f64122c1SDave Airlie {
601f64122c1SDave Airlie 	struct qxl_crtc *qxl_crtc;
602f64122c1SDave Airlie 
603f64122c1SDave Airlie 	qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
604f64122c1SDave Airlie 	if (!qxl_crtc)
605f64122c1SDave Airlie 		return -ENOMEM;
606f64122c1SDave Airlie 
607f64122c1SDave Airlie 	drm_crtc_init(dev, &qxl_crtc->base, &qxl_crtc_funcs);
60807f8d9bdSDave Airlie 	qxl_crtc->index = crtc_id;
609f64122c1SDave Airlie 	drm_mode_crtc_set_gamma_size(&qxl_crtc->base, 256);
610f64122c1SDave Airlie 	drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
611f64122c1SDave Airlie 	return 0;
612f64122c1SDave Airlie }
613f64122c1SDave Airlie 
614f64122c1SDave Airlie static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
615f64122c1SDave Airlie {
616f64122c1SDave Airlie 	DRM_DEBUG("\n");
617f64122c1SDave Airlie }
618f64122c1SDave Airlie 
619f64122c1SDave Airlie static bool qxl_enc_mode_fixup(struct drm_encoder *encoder,
620f64122c1SDave Airlie 			       const struct drm_display_mode *mode,
621f64122c1SDave Airlie 			       struct drm_display_mode *adjusted_mode)
622f64122c1SDave Airlie {
623f64122c1SDave Airlie 	DRM_DEBUG("\n");
624f64122c1SDave Airlie 	return true;
625f64122c1SDave Airlie }
626f64122c1SDave Airlie 
627f64122c1SDave Airlie static void qxl_enc_prepare(struct drm_encoder *encoder)
628f64122c1SDave Airlie {
629f64122c1SDave Airlie 	DRM_DEBUG("\n");
630f64122c1SDave Airlie }
631f64122c1SDave Airlie 
632f64122c1SDave Airlie static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
633f64122c1SDave Airlie 		struct drm_encoder *encoder)
634f64122c1SDave Airlie {
635f64122c1SDave Airlie 	int i;
63607f8d9bdSDave Airlie 	struct qxl_output *output = drm_encoder_to_qxl_output(encoder);
637f64122c1SDave Airlie 	struct qxl_head *head;
638f64122c1SDave Airlie 	struct drm_display_mode *mode;
639f64122c1SDave Airlie 
640f64122c1SDave Airlie 	BUG_ON(!encoder);
641f64122c1SDave Airlie 	/* TODO: ugly, do better */
64207f8d9bdSDave Airlie 	i = output->index;
643f64122c1SDave Airlie 	if (!qdev->monitors_config ||
644f64122c1SDave Airlie 	    qdev->monitors_config->max_allowed <= i) {
645f64122c1SDave Airlie 		DRM_ERROR(
646f64122c1SDave Airlie 		"head number too large or missing monitors config: %p, %d",
647f64122c1SDave Airlie 		qdev->monitors_config,
648f64122c1SDave Airlie 		qdev->monitors_config ?
649f64122c1SDave Airlie 			qdev->monitors_config->max_allowed : -1);
650f64122c1SDave Airlie 		return;
651f64122c1SDave Airlie 	}
652f64122c1SDave Airlie 	if (!encoder->crtc) {
653f64122c1SDave Airlie 		DRM_ERROR("missing crtc on encoder %p\n", encoder);
654f64122c1SDave Airlie 		return;
655f64122c1SDave Airlie 	}
656f64122c1SDave Airlie 	if (i != 0)
657f64122c1SDave Airlie 		DRM_DEBUG("missing for multiple monitors: no head holes\n");
658f64122c1SDave Airlie 	head = &qdev->monitors_config->heads[i];
659f64122c1SDave Airlie 	head->id = i;
660f64122c1SDave Airlie 	if (encoder->crtc->enabled) {
661f64122c1SDave Airlie 		mode = &encoder->crtc->mode;
662f64122c1SDave Airlie 		head->width = mode->hdisplay;
663f64122c1SDave Airlie 		head->height = mode->vdisplay;
664f64122c1SDave Airlie 		head->x = encoder->crtc->x;
665f64122c1SDave Airlie 		head->y = encoder->crtc->y;
666f64122c1SDave Airlie 		if (qdev->monitors_config->count < i + 1)
667f64122c1SDave Airlie 			qdev->monitors_config->count = i + 1;
668f64122c1SDave Airlie 	} else {
669f64122c1SDave Airlie 		head->width = 0;
670f64122c1SDave Airlie 		head->height = 0;
671f64122c1SDave Airlie 		head->x = 0;
672f64122c1SDave Airlie 		head->y = 0;
673f64122c1SDave Airlie 	}
67407f8d9bdSDave Airlie 	DRM_DEBUG_KMS("setting head %d to +%d+%d %dx%d out of %d\n",
67507f8d9bdSDave Airlie 		      i, head->x, head->y, head->width, head->height, qdev->monitors_config->count);
676f64122c1SDave Airlie 	head->flags = 0;
677f64122c1SDave Airlie 	/* TODO - somewhere else to call this for multiple monitors
678f64122c1SDave Airlie 	 * (config_commit?) */
679f64122c1SDave Airlie 	qxl_send_monitors_config(qdev);
680f64122c1SDave Airlie }
681f64122c1SDave Airlie 
682f64122c1SDave Airlie static void qxl_enc_commit(struct drm_encoder *encoder)
683f64122c1SDave Airlie {
684f64122c1SDave Airlie 	struct qxl_device *qdev = encoder->dev->dev_private;
685f64122c1SDave Airlie 
686f64122c1SDave Airlie 	qxl_write_monitors_config_for_encoder(qdev, encoder);
687f64122c1SDave Airlie 	DRM_DEBUG("\n");
688f64122c1SDave Airlie }
689f64122c1SDave Airlie 
690f64122c1SDave Airlie static void qxl_enc_mode_set(struct drm_encoder *encoder,
691f64122c1SDave Airlie 				struct drm_display_mode *mode,
692f64122c1SDave Airlie 				struct drm_display_mode *adjusted_mode)
693f64122c1SDave Airlie {
694f64122c1SDave Airlie 	DRM_DEBUG("\n");
695f64122c1SDave Airlie }
696f64122c1SDave Airlie 
697f64122c1SDave Airlie static int qxl_conn_get_modes(struct drm_connector *connector)
698f64122c1SDave Airlie {
699f64122c1SDave Airlie 	int ret = 0;
700f64122c1SDave Airlie 	struct qxl_device *qdev = connector->dev->dev_private;
701f64122c1SDave Airlie 
702f64122c1SDave Airlie 	DRM_DEBUG_KMS("monitors_config=%p\n", qdev->monitors_config);
703f64122c1SDave Airlie 	/* TODO: what should we do here? only show the configured modes for the
704f64122c1SDave Airlie 	 * device, or allow the full list, or both? */
705f64122c1SDave Airlie 	if (qdev->monitors_config && qdev->monitors_config->count) {
706f64122c1SDave Airlie 		ret = qxl_add_monitors_config_modes(connector);
707f64122c1SDave Airlie 		if (ret < 0)
708f64122c1SDave Airlie 			return ret;
709f64122c1SDave Airlie 	}
710f64122c1SDave Airlie 	ret += qxl_add_common_modes(connector);
711f64122c1SDave Airlie 	return ret;
712f64122c1SDave Airlie }
713f64122c1SDave Airlie 
714f64122c1SDave Airlie static int qxl_conn_mode_valid(struct drm_connector *connector,
715f64122c1SDave Airlie 			       struct drm_display_mode *mode)
716f64122c1SDave Airlie {
717f64122c1SDave Airlie 	/* TODO: is this called for user defined modes? (xrandr --add-mode)
718f64122c1SDave Airlie 	 * TODO: check that the mode fits in the framebuffer */
719f64122c1SDave Airlie 	DRM_DEBUG("%s: %dx%d status=%d\n", mode->name, mode->hdisplay,
720f64122c1SDave Airlie 		  mode->vdisplay, mode->status);
721f64122c1SDave Airlie 	return MODE_OK;
722f64122c1SDave Airlie }
723f64122c1SDave Airlie 
7246d01f1f5SDave Airlie static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
725f64122c1SDave Airlie {
726f64122c1SDave Airlie 	struct qxl_output *qxl_output =
727f64122c1SDave Airlie 		drm_connector_to_qxl_output(connector);
728f64122c1SDave Airlie 
729f64122c1SDave Airlie 	DRM_DEBUG("\n");
730f64122c1SDave Airlie 	return &qxl_output->enc;
731f64122c1SDave Airlie }
732f64122c1SDave Airlie 
733f64122c1SDave Airlie 
734f64122c1SDave Airlie static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
735f64122c1SDave Airlie 	.dpms = qxl_enc_dpms,
736f64122c1SDave Airlie 	.mode_fixup = qxl_enc_mode_fixup,
737f64122c1SDave Airlie 	.prepare = qxl_enc_prepare,
738f64122c1SDave Airlie 	.mode_set = qxl_enc_mode_set,
739f64122c1SDave Airlie 	.commit = qxl_enc_commit,
740f64122c1SDave Airlie };
741f64122c1SDave Airlie 
742f64122c1SDave Airlie static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
743f64122c1SDave Airlie 	.get_modes = qxl_conn_get_modes,
744f64122c1SDave Airlie 	.mode_valid = qxl_conn_mode_valid,
745f64122c1SDave Airlie 	.best_encoder = qxl_best_encoder,
746f64122c1SDave Airlie };
747f64122c1SDave Airlie 
748f64122c1SDave Airlie static void qxl_conn_save(struct drm_connector *connector)
749f64122c1SDave Airlie {
750f64122c1SDave Airlie 	DRM_DEBUG("\n");
751f64122c1SDave Airlie }
752f64122c1SDave Airlie 
753f64122c1SDave Airlie static void qxl_conn_restore(struct drm_connector *connector)
754f64122c1SDave Airlie {
755f64122c1SDave Airlie 	DRM_DEBUG("\n");
756f64122c1SDave Airlie }
757f64122c1SDave Airlie 
758f64122c1SDave Airlie static enum drm_connector_status qxl_conn_detect(
759f64122c1SDave Airlie 			struct drm_connector *connector,
760f64122c1SDave Airlie 			bool force)
761f64122c1SDave Airlie {
762f64122c1SDave Airlie 	struct qxl_output *output =
763f64122c1SDave Airlie 		drm_connector_to_qxl_output(connector);
764f64122c1SDave Airlie 	struct drm_device *ddev = connector->dev;
765f64122c1SDave Airlie 	struct qxl_device *qdev = ddev->dev_private;
766f64122c1SDave Airlie 	int connected;
767f64122c1SDave Airlie 
768f64122c1SDave Airlie 	/* The first monitor is always connected */
769f64122c1SDave Airlie 	connected = (output->index == 0) ||
77007f8d9bdSDave Airlie 		    (qdev->client_monitors_config &&
77107f8d9bdSDave Airlie 		     qdev->client_monitors_config->count > output->index &&
77207f8d9bdSDave Airlie 		     qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]));
773f64122c1SDave Airlie 
774f64122c1SDave Airlie 	DRM_DEBUG("\n");
775f64122c1SDave Airlie 	return connected ? connector_status_connected
776f64122c1SDave Airlie 			 : connector_status_disconnected;
777f64122c1SDave Airlie }
778f64122c1SDave Airlie 
779f64122c1SDave Airlie static int qxl_conn_set_property(struct drm_connector *connector,
780f64122c1SDave Airlie 				   struct drm_property *property,
781f64122c1SDave Airlie 				   uint64_t value)
782f64122c1SDave Airlie {
783f64122c1SDave Airlie 	DRM_DEBUG("\n");
784f64122c1SDave Airlie 	return 0;
785f64122c1SDave Airlie }
786f64122c1SDave Airlie 
787f64122c1SDave Airlie static void qxl_conn_destroy(struct drm_connector *connector)
788f64122c1SDave Airlie {
789f64122c1SDave Airlie 	struct qxl_output *qxl_output =
790f64122c1SDave Airlie 		drm_connector_to_qxl_output(connector);
791f64122c1SDave Airlie 
792f64122c1SDave Airlie 	drm_sysfs_connector_remove(connector);
793f64122c1SDave Airlie 	drm_connector_cleanup(connector);
794f64122c1SDave Airlie 	kfree(qxl_output);
795f64122c1SDave Airlie }
796f64122c1SDave Airlie 
797f64122c1SDave Airlie static const struct drm_connector_funcs qxl_connector_funcs = {
798f64122c1SDave Airlie 	.dpms = drm_helper_connector_dpms,
799f64122c1SDave Airlie 	.save = qxl_conn_save,
800f64122c1SDave Airlie 	.restore = qxl_conn_restore,
801f64122c1SDave Airlie 	.detect = qxl_conn_detect,
802f64122c1SDave Airlie 	.fill_modes = drm_helper_probe_single_connector_modes,
803f64122c1SDave Airlie 	.set_property = qxl_conn_set_property,
804f64122c1SDave Airlie 	.destroy = qxl_conn_destroy,
805f64122c1SDave Airlie };
806f64122c1SDave Airlie 
807f64122c1SDave Airlie static void qxl_enc_destroy(struct drm_encoder *encoder)
808f64122c1SDave Airlie {
809f64122c1SDave Airlie 	drm_encoder_cleanup(encoder);
810f64122c1SDave Airlie }
811f64122c1SDave Airlie 
812f64122c1SDave Airlie static const struct drm_encoder_funcs qxl_enc_funcs = {
813f64122c1SDave Airlie 	.destroy = qxl_enc_destroy,
814f64122c1SDave Airlie };
815f64122c1SDave Airlie 
8166d01f1f5SDave Airlie static int qdev_output_init(struct drm_device *dev, int num_output)
817f64122c1SDave Airlie {
818f64122c1SDave Airlie 	struct qxl_output *qxl_output;
819f64122c1SDave Airlie 	struct drm_connector *connector;
820f64122c1SDave Airlie 	struct drm_encoder *encoder;
821f64122c1SDave Airlie 
822f64122c1SDave Airlie 	qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
823f64122c1SDave Airlie 	if (!qxl_output)
824f64122c1SDave Airlie 		return -ENOMEM;
825f64122c1SDave Airlie 
826f64122c1SDave Airlie 	qxl_output->index = num_output;
827f64122c1SDave Airlie 
828f64122c1SDave Airlie 	connector = &qxl_output->base;
829f64122c1SDave Airlie 	encoder = &qxl_output->enc;
830f64122c1SDave Airlie 	drm_connector_init(dev, &qxl_output->base,
831f64122c1SDave Airlie 			   &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
832f64122c1SDave Airlie 
833f64122c1SDave Airlie 	drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
834f64122c1SDave Airlie 			 DRM_MODE_ENCODER_VIRTUAL);
835f64122c1SDave Airlie 
836f64122c1SDave Airlie 	encoder->possible_crtcs = 1 << num_output;
837f64122c1SDave Airlie 	drm_mode_connector_attach_encoder(&qxl_output->base,
838f64122c1SDave Airlie 					  &qxl_output->enc);
839f64122c1SDave Airlie 	drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
840f64122c1SDave Airlie 	drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
841f64122c1SDave Airlie 
842f64122c1SDave Airlie 	drm_sysfs_connector_add(connector);
843f64122c1SDave Airlie 	return 0;
844f64122c1SDave Airlie }
845f64122c1SDave Airlie 
846f64122c1SDave Airlie static struct drm_framebuffer *
847f64122c1SDave Airlie qxl_user_framebuffer_create(struct drm_device *dev,
848f64122c1SDave Airlie 			    struct drm_file *file_priv,
849f64122c1SDave Airlie 			    struct drm_mode_fb_cmd2 *mode_cmd)
850f64122c1SDave Airlie {
851f64122c1SDave Airlie 	struct drm_gem_object *obj;
852f64122c1SDave Airlie 	struct qxl_framebuffer *qxl_fb;
853f64122c1SDave Airlie 	int ret;
854f64122c1SDave Airlie 
855f64122c1SDave Airlie 	obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
856f64122c1SDave Airlie 
857f64122c1SDave Airlie 	qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
858f64122c1SDave Airlie 	if (qxl_fb == NULL)
859f64122c1SDave Airlie 		return NULL;
860f64122c1SDave Airlie 
861f64122c1SDave Airlie 	ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj);
862f64122c1SDave Airlie 	if (ret) {
863f64122c1SDave Airlie 		kfree(qxl_fb);
864f64122c1SDave Airlie 		drm_gem_object_unreference_unlocked(obj);
865f64122c1SDave Airlie 		return NULL;
866f64122c1SDave Airlie 	}
867f64122c1SDave Airlie 
868f64122c1SDave Airlie 	return &qxl_fb->base;
869f64122c1SDave Airlie }
870f64122c1SDave Airlie 
871f64122c1SDave Airlie static const struct drm_mode_config_funcs qxl_mode_funcs = {
872f64122c1SDave Airlie 	.fb_create = qxl_user_framebuffer_create,
873f64122c1SDave Airlie };
874f64122c1SDave Airlie 
875*2bd6ce84SDave Airlie int qxl_create_monitors_object(struct qxl_device *qdev)
876f64122c1SDave Airlie {
877f64122c1SDave Airlie 	int ret;
878f64122c1SDave Airlie 	struct drm_gem_object *gobj;
87907f8d9bdSDave Airlie 	int max_allowed = qxl_num_crtc;
880f64122c1SDave Airlie 	int monitors_config_size = sizeof(struct qxl_monitors_config) +
881f64122c1SDave Airlie 		max_allowed * sizeof(struct qxl_head);
882f64122c1SDave Airlie 
883f64122c1SDave Airlie 	ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
884f64122c1SDave Airlie 				    QXL_GEM_DOMAIN_VRAM,
885f64122c1SDave Airlie 				    false, false, NULL, &gobj);
886f64122c1SDave Airlie 	if (ret) {
887f64122c1SDave Airlie 		DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
888f64122c1SDave Airlie 		return -ENOMEM;
889f64122c1SDave Airlie 	}
890f64122c1SDave Airlie 	qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
891*2bd6ce84SDave Airlie 
892*2bd6ce84SDave Airlie 	ret = qxl_bo_reserve(qdev->monitors_config_bo, false);
893*2bd6ce84SDave Airlie 	if (ret)
894*2bd6ce84SDave Airlie 		return ret;
895*2bd6ce84SDave Airlie 
896*2bd6ce84SDave Airlie 	ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL);
897*2bd6ce84SDave Airlie 	if (ret) {
898*2bd6ce84SDave Airlie 		qxl_bo_unreserve(qdev->monitors_config_bo);
899*2bd6ce84SDave Airlie 		return ret;
900*2bd6ce84SDave Airlie 	}
901*2bd6ce84SDave Airlie 
902*2bd6ce84SDave Airlie 	qxl_bo_unreserve(qdev->monitors_config_bo);
903*2bd6ce84SDave Airlie 
904f64122c1SDave Airlie 	qxl_bo_kmap(qdev->monitors_config_bo, NULL);
905*2bd6ce84SDave Airlie 
906f64122c1SDave Airlie 	qdev->monitors_config = qdev->monitors_config_bo->kptr;
907f64122c1SDave Airlie 	qdev->ram_header->monitors_config =
908f64122c1SDave Airlie 		qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
909f64122c1SDave Airlie 
910f64122c1SDave Airlie 	memset(qdev->monitors_config, 0, monitors_config_size);
911f64122c1SDave Airlie 	qdev->monitors_config->max_allowed = max_allowed;
912*2bd6ce84SDave Airlie 	return 0;
913*2bd6ce84SDave Airlie }
914*2bd6ce84SDave Airlie 
915*2bd6ce84SDave Airlie int qxl_destroy_monitors_object(struct qxl_device *qdev)
916*2bd6ce84SDave Airlie {
917*2bd6ce84SDave Airlie 	int ret;
918*2bd6ce84SDave Airlie 
919*2bd6ce84SDave Airlie 	qdev->monitors_config = NULL;
920*2bd6ce84SDave Airlie 	qdev->ram_header->monitors_config = 0;
921*2bd6ce84SDave Airlie 
922*2bd6ce84SDave Airlie 	qxl_bo_kunmap(qdev->monitors_config_bo);
923*2bd6ce84SDave Airlie 	ret = qxl_bo_reserve(qdev->monitors_config_bo, false);
924*2bd6ce84SDave Airlie 	if (ret)
925*2bd6ce84SDave Airlie 		return ret;
926*2bd6ce84SDave Airlie 
927*2bd6ce84SDave Airlie 	qxl_bo_unpin(qdev->monitors_config_bo);
928*2bd6ce84SDave Airlie 	qxl_bo_unreserve(qdev->monitors_config_bo);
929*2bd6ce84SDave Airlie 
930*2bd6ce84SDave Airlie 	qxl_bo_unref(&qdev->monitors_config_bo);
931*2bd6ce84SDave Airlie 	return 0;
932*2bd6ce84SDave Airlie }
933*2bd6ce84SDave Airlie 
934*2bd6ce84SDave Airlie int qxl_modeset_init(struct qxl_device *qdev)
935*2bd6ce84SDave Airlie {
936*2bd6ce84SDave Airlie 	int i;
937*2bd6ce84SDave Airlie 	int ret;
938*2bd6ce84SDave Airlie 
939*2bd6ce84SDave Airlie 	drm_mode_config_init(qdev->ddev);
940*2bd6ce84SDave Airlie 
941*2bd6ce84SDave Airlie 	ret = qxl_create_monitors_object(qdev);
942*2bd6ce84SDave Airlie 	if (ret)
943*2bd6ce84SDave Airlie 		return ret;
944f64122c1SDave Airlie 
945f64122c1SDave Airlie 	qdev->ddev->mode_config.funcs = (void *)&qxl_mode_funcs;
946f64122c1SDave Airlie 
947f64122c1SDave Airlie 	/* modes will be validated against the framebuffer size */
948f64122c1SDave Airlie 	qdev->ddev->mode_config.min_width = 320;
949f64122c1SDave Airlie 	qdev->ddev->mode_config.min_height = 200;
950f64122c1SDave Airlie 	qdev->ddev->mode_config.max_width = 8192;
951f64122c1SDave Airlie 	qdev->ddev->mode_config.max_height = 8192;
952f64122c1SDave Airlie 
953f64122c1SDave Airlie 	qdev->ddev->mode_config.fb_base = qdev->vram_base;
95407f8d9bdSDave Airlie 	for (i = 0 ; i < qxl_num_crtc; ++i) {
955f64122c1SDave Airlie 		qdev_crtc_init(qdev->ddev, i);
956f64122c1SDave Airlie 		qdev_output_init(qdev->ddev, i);
957f64122c1SDave Airlie 	}
958f64122c1SDave Airlie 
959f64122c1SDave Airlie 	qdev->mode_info.mode_config_initialized = true;
960f64122c1SDave Airlie 
961f64122c1SDave Airlie 	/* primary surface must be created by this point, to allow
962f64122c1SDave Airlie 	 * issuing command queue commands and having them read by
963f64122c1SDave Airlie 	 * spice server. */
964f64122c1SDave Airlie 	qxl_fbdev_init(qdev);
965f64122c1SDave Airlie 	return 0;
966f64122c1SDave Airlie }
967f64122c1SDave Airlie 
968f64122c1SDave Airlie void qxl_modeset_fini(struct qxl_device *qdev)
969f64122c1SDave Airlie {
970f64122c1SDave Airlie 	qxl_fbdev_fini(qdev);
971*2bd6ce84SDave Airlie 
972*2bd6ce84SDave Airlie 	qxl_destroy_monitors_object(qdev);
973f64122c1SDave Airlie 	if (qdev->mode_info.mode_config_initialized) {
974f64122c1SDave Airlie 		drm_mode_config_cleanup(qdev->ddev);
975f64122c1SDave Airlie 		qdev->mode_info.mode_config_initialized = false;
976f64122c1SDave Airlie 	}
977f64122c1SDave Airlie }
978