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