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
26c5416d66SRandy Dunlap #include <linux/crc32.h>
27c0f4b75cSSam Ravnborg #include <linux/delay.h>
287938f421SLucas De Marchi #include <linux/iosys-map.h>
29c0f4b75cSSam Ravnborg
30ce5c207cSDave Airlie #include <drm/drm_drv.h>
3110a0bd89SGabriel Krisman Bertazi #include <drm/drm_atomic.h>
32fcd70cd3SDaniel Vetter #include <drm/drm_atomic_helper.h>
33255490f9SVille Syrjälä #include <drm/drm_edid.h>
34720cf96dSVille Syrjälä #include <drm/drm_framebuffer.h>
35bf8744e4SPeter Wu #include <drm/drm_gem_framebuffer_helper.h>
36fcd70cd3SDaniel Vetter #include <drm/drm_plane_helper.h>
37fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h>
386f2bb119SThomas Zimmermann #include <drm/drm_simple_kms_helper.h>
396071c4c2SChristian König #include <drm/drm_gem_atomic_helper.h>
40f64122c1SDave Airlie
41edaf492cSMasahiro Yamada #include "qxl_drv.h"
42edaf492cSMasahiro Yamada #include "qxl_object.h"
43edaf492cSMasahiro Yamada
qxl_head_enabled(struct qxl_head * head)4407f8d9bdSDave Airlie static bool qxl_head_enabled(struct qxl_head *head)
4507f8d9bdSDave Airlie {
4607f8d9bdSDave Airlie return head->width && head->height;
4707f8d9bdSDave Airlie }
4807f8d9bdSDave Airlie
qxl_alloc_client_monitors_config(struct qxl_device * qdev,unsigned int count)4966e0c8a5SAnton Vasilyev static int qxl_alloc_client_monitors_config(struct qxl_device *qdev,
5066e0c8a5SAnton Vasilyev unsigned int count)
51f64122c1SDave Airlie {
52f64122c1SDave Airlie if (qdev->client_monitors_config &&
53f64122c1SDave Airlie count > qdev->client_monitors_config->count) {
54f64122c1SDave Airlie kfree(qdev->client_monitors_config);
5562c8ba7cSDave Airlie qdev->client_monitors_config = NULL;
56f64122c1SDave Airlie }
57f64122c1SDave Airlie if (!qdev->client_monitors_config) {
58f64122c1SDave Airlie qdev->client_monitors_config = kzalloc(
59d4b9dd50SGustavo A. R. Silva struct_size(qdev->client_monitors_config,
60d4b9dd50SGustavo A. R. Silva heads, count), GFP_KERNEL);
61735581a0SGerd Hoffmann if (!qdev->client_monitors_config)
6266e0c8a5SAnton Vasilyev return -ENOMEM;
63f64122c1SDave Airlie }
64f64122c1SDave Airlie qdev->client_monitors_config->count = count;
6566e0c8a5SAnton Vasilyev return 0;
66f64122c1SDave Airlie }
67f64122c1SDave Airlie
689e3b3178SChristophe Fergeau enum {
699e3b3178SChristophe Fergeau MONITORS_CONFIG_MODIFIED,
709e3b3178SChristophe Fergeau MONITORS_CONFIG_UNCHANGED,
719e3b3178SChristophe Fergeau MONITORS_CONFIG_BAD_CRC,
7266e0c8a5SAnton Vasilyev MONITORS_CONFIG_ERROR,
739e3b3178SChristophe Fergeau };
749e3b3178SChristophe Fergeau
qxl_display_copy_rom_client_monitors_config(struct qxl_device * qdev)75f64122c1SDave Airlie static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
76f64122c1SDave Airlie {
77f64122c1SDave Airlie int i;
78f64122c1SDave Airlie int num_monitors;
79f64122c1SDave Airlie uint32_t crc;
809e3b3178SChristophe Fergeau int status = MONITORS_CONFIG_UNCHANGED;
81f64122c1SDave Airlie
82f64122c1SDave Airlie num_monitors = qdev->rom->client_monitors_config.count;
83f64122c1SDave Airlie crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
84f64122c1SDave Airlie sizeof(qdev->rom->client_monitors_config));
85735581a0SGerd Hoffmann if (crc != qdev->rom->client_monitors_config_crc)
869e3b3178SChristophe Fergeau return MONITORS_CONFIG_BAD_CRC;
87c50fad8fSGerd Hoffmann if (!num_monitors) {
88c50fad8fSGerd Hoffmann DRM_DEBUG_KMS("no client monitors configured\n");
89c50fad8fSGerd Hoffmann return status;
90c50fad8fSGerd Hoffmann }
9121c76bd1SGerd Hoffmann if (num_monitors > qxl_num_crtc) {
925b8788c1SDave Airlie DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
9321c76bd1SGerd Hoffmann qxl_num_crtc, num_monitors);
9421c76bd1SGerd Hoffmann num_monitors = qxl_num_crtc;
95f64122c1SDave Airlie } else {
96f64122c1SDave Airlie num_monitors = qdev->rom->client_monitors_config.count;
97f64122c1SDave Airlie }
989e3b3178SChristophe Fergeau if (qdev->client_monitors_config
999e3b3178SChristophe Fergeau && (num_monitors != qdev->client_monitors_config->count)) {
1009e3b3178SChristophe Fergeau status = MONITORS_CONFIG_MODIFIED;
1019e3b3178SChristophe Fergeau }
10266e0c8a5SAnton Vasilyev if (qxl_alloc_client_monitors_config(qdev, num_monitors)) {
10366e0c8a5SAnton Vasilyev status = MONITORS_CONFIG_ERROR;
10466e0c8a5SAnton Vasilyev return status;
10566e0c8a5SAnton Vasilyev }
106f64122c1SDave Airlie /* we copy max from the client but it isn't used */
10721c76bd1SGerd Hoffmann qdev->client_monitors_config->max_allowed = qxl_num_crtc;
108f64122c1SDave Airlie for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
109f64122c1SDave Airlie struct qxl_urect *c_rect =
110f64122c1SDave Airlie &qdev->rom->client_monitors_config.heads[i];
111f64122c1SDave Airlie struct qxl_head *client_head =
112f64122c1SDave Airlie &qdev->client_monitors_config->heads[i];
1139e3b3178SChristophe Fergeau if (client_head->x != c_rect->left) {
11407f8d9bdSDave Airlie client_head->x = c_rect->left;
1159e3b3178SChristophe Fergeau status = MONITORS_CONFIG_MODIFIED;
1169e3b3178SChristophe Fergeau }
1179e3b3178SChristophe Fergeau if (client_head->y != c_rect->top) {
11807f8d9bdSDave Airlie client_head->y = c_rect->top;
1199e3b3178SChristophe Fergeau status = MONITORS_CONFIG_MODIFIED;
1209e3b3178SChristophe Fergeau }
1219e3b3178SChristophe Fergeau if (client_head->width != c_rect->right - c_rect->left) {
12207f8d9bdSDave Airlie client_head->width = c_rect->right - c_rect->left;
1239e3b3178SChristophe Fergeau status = MONITORS_CONFIG_MODIFIED;
1249e3b3178SChristophe Fergeau }
1259e3b3178SChristophe Fergeau if (client_head->height != c_rect->bottom - c_rect->top) {
12607f8d9bdSDave Airlie client_head->height = c_rect->bottom - c_rect->top;
1279e3b3178SChristophe Fergeau status = MONITORS_CONFIG_MODIFIED;
1289e3b3178SChristophe Fergeau }
1299e3b3178SChristophe Fergeau if (client_head->surface_id != 0) {
13007f8d9bdSDave Airlie client_head->surface_id = 0;
1319e3b3178SChristophe Fergeau status = MONITORS_CONFIG_MODIFIED;
1329e3b3178SChristophe Fergeau }
1339e3b3178SChristophe Fergeau if (client_head->id != i) {
13407f8d9bdSDave Airlie client_head->id = i;
1359e3b3178SChristophe Fergeau status = MONITORS_CONFIG_MODIFIED;
1369e3b3178SChristophe Fergeau }
1379e3b3178SChristophe Fergeau if (client_head->flags != 0) {
13807f8d9bdSDave Airlie client_head->flags = 0;
1399e3b3178SChristophe Fergeau status = MONITORS_CONFIG_MODIFIED;
1409e3b3178SChristophe Fergeau }
14107f8d9bdSDave Airlie DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
14207f8d9bdSDave Airlie client_head->x, client_head->y);
143f64122c1SDave Airlie }
1449e3b3178SChristophe Fergeau
1459e3b3178SChristophe Fergeau return status;
146f64122c1SDave Airlie }
147f64122c1SDave Airlie
qxl_update_offset_props(struct qxl_device * qdev)1487dea0941SDave Airlie static void qxl_update_offset_props(struct qxl_device *qdev)
1497dea0941SDave Airlie {
150cbdded7fSGabriel Krisman Bertazi struct drm_device *dev = &qdev->ddev;
1517dea0941SDave Airlie struct drm_connector *connector;
1527dea0941SDave Airlie struct qxl_output *output;
1537dea0941SDave Airlie struct qxl_head *head;
1547dea0941SDave Airlie
1557dea0941SDave Airlie list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1567dea0941SDave Airlie output = drm_connector_to_qxl_output(connector);
1577dea0941SDave Airlie
1587dea0941SDave Airlie head = &qdev->client_monitors_config->heads[output->index];
1597dea0941SDave Airlie
1607dea0941SDave Airlie drm_object_property_set_value(&connector->base,
1617dea0941SDave Airlie dev->mode_config.suggested_x_property, head->x);
1627dea0941SDave Airlie drm_object_property_set_value(&connector->base,
1637dea0941SDave Airlie dev->mode_config.suggested_y_property, head->y);
1647dea0941SDave Airlie }
1657dea0941SDave Airlie }
1667dea0941SDave Airlie
qxl_display_read_client_monitors_config(struct qxl_device * qdev)167f64122c1SDave Airlie void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
168f64122c1SDave Airlie {
169cbdded7fSGabriel Krisman Bertazi struct drm_device *dev = &qdev->ddev;
170bbaac135SSidong Yang struct drm_modeset_acquire_ctx ctx;
171bbaac135SSidong Yang int status, retries, ret;
1729e3b3178SChristophe Fergeau
1739062155dSGerd Hoffmann for (retries = 0; retries < 10; retries++) {
1749e3b3178SChristophe Fergeau status = qxl_display_copy_rom_client_monitors_config(qdev);
1759062155dSGerd Hoffmann if (status != MONITORS_CONFIG_BAD_CRC)
1769062155dSGerd Hoffmann break;
1779062155dSGerd Hoffmann udelay(5);
1789062155dSGerd Hoffmann }
17966e0c8a5SAnton Vasilyev if (status == MONITORS_CONFIG_ERROR) {
18066e0c8a5SAnton Vasilyev DRM_DEBUG_KMS("ignoring client monitors config: error");
18166e0c8a5SAnton Vasilyev return;
18266e0c8a5SAnton Vasilyev }
1839062155dSGerd Hoffmann if (status == MONITORS_CONFIG_BAD_CRC) {
1849062155dSGerd Hoffmann DRM_DEBUG_KMS("ignoring client monitors config: bad crc");
1859062155dSGerd Hoffmann return;
1869e3b3178SChristophe Fergeau }
1879e3b3178SChristophe Fergeau if (status == MONITORS_CONFIG_UNCHANGED) {
1889062155dSGerd Hoffmann DRM_DEBUG_KMS("ignoring client monitors config: unchanged");
1899e3b3178SChristophe Fergeau return;
190f64122c1SDave Airlie }
1914fdb0869SMarc-André Lureau
192bbaac135SSidong Yang DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE, ret);
1937dea0941SDave Airlie qxl_update_offset_props(qdev);
194ce5c207cSDave Airlie DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
195cbdded7fSGabriel Krisman Bertazi if (!drm_helper_hpd_irq_event(dev)) {
1964fdb0869SMarc-André Lureau /* notify that the monitor configuration changed, to
1974fdb0869SMarc-André Lureau adjust at the arbitrary resolution */
198cbdded7fSGabriel Krisman Bertazi drm_kms_helper_hotplug_event(dev);
1994fdb0869SMarc-André Lureau }
200f64122c1SDave Airlie }
201f64122c1SDave Airlie
qxl_check_mode(struct qxl_device * qdev,unsigned int width,unsigned int height)202feba24deSGerd Hoffmann static int qxl_check_mode(struct qxl_device *qdev,
203feba24deSGerd Hoffmann unsigned int width,
204feba24deSGerd Hoffmann unsigned int height)
205feba24deSGerd Hoffmann {
206feba24deSGerd Hoffmann unsigned int stride;
207feba24deSGerd Hoffmann unsigned int size;
208feba24deSGerd Hoffmann
209feba24deSGerd Hoffmann if (check_mul_overflow(width, 4u, &stride))
210feba24deSGerd Hoffmann return -EINVAL;
211feba24deSGerd Hoffmann if (check_mul_overflow(stride, height, &size))
212feba24deSGerd Hoffmann return -EINVAL;
213feba24deSGerd Hoffmann if (size > qdev->vram_size)
214feba24deSGerd Hoffmann return -ENOMEM;
215feba24deSGerd Hoffmann return 0;
216feba24deSGerd Hoffmann }
217feba24deSGerd Hoffmann
qxl_check_framebuffer(struct qxl_device * qdev,struct qxl_bo * bo)218feba24deSGerd Hoffmann static int qxl_check_framebuffer(struct qxl_device *qdev,
219feba24deSGerd Hoffmann struct qxl_bo *bo)
220feba24deSGerd Hoffmann {
221feba24deSGerd Hoffmann return qxl_check_mode(qdev, bo->surf.width, bo->surf.height);
222feba24deSGerd Hoffmann }
223feba24deSGerd Hoffmann
qxl_add_mode(struct drm_connector * connector,unsigned int width,unsigned int height,bool preferred)2241b043677SGerd Hoffmann static int qxl_add_mode(struct drm_connector *connector,
2251b043677SGerd Hoffmann unsigned int width,
2261b043677SGerd Hoffmann unsigned int height,
2271b043677SGerd Hoffmann bool preferred)
2281b043677SGerd Hoffmann {
2291b043677SGerd Hoffmann struct drm_device *dev = connector->dev;
230e304f8a0SDaniel Vetter struct qxl_device *qdev = to_qxl(dev);
2311b043677SGerd Hoffmann struct drm_display_mode *mode = NULL;
2321b043677SGerd Hoffmann int rc;
2331b043677SGerd Hoffmann
2341b043677SGerd Hoffmann rc = qxl_check_mode(qdev, width, height);
2351b043677SGerd Hoffmann if (rc != 0)
2361b043677SGerd Hoffmann return 0;
2371b043677SGerd Hoffmann
2381b043677SGerd Hoffmann mode = drm_cvt_mode(dev, width, height, 60, false, false, false);
239*4b1f303bSChen Ni if (!mode)
240*4b1f303bSChen Ni return 0;
241*4b1f303bSChen Ni
2421b043677SGerd Hoffmann if (preferred)
2431b043677SGerd Hoffmann mode->type |= DRM_MODE_TYPE_PREFERRED;
2441b043677SGerd Hoffmann mode->hdisplay = width;
2451b043677SGerd Hoffmann mode->vdisplay = height;
2461b043677SGerd Hoffmann drm_mode_set_name(mode);
2471b043677SGerd Hoffmann drm_mode_probed_add(connector, mode);
2481b043677SGerd Hoffmann return 1;
2491b043677SGerd Hoffmann }
2501b043677SGerd Hoffmann
qxl_add_monitors_config_modes(struct drm_connector * connector)2511b043677SGerd Hoffmann static int qxl_add_monitors_config_modes(struct drm_connector *connector)
252f64122c1SDave Airlie {
253f64122c1SDave Airlie struct drm_device *dev = connector->dev;
254e304f8a0SDaniel Vetter struct qxl_device *qdev = to_qxl(dev);
255f64122c1SDave Airlie struct qxl_output *output = drm_connector_to_qxl_output(connector);
256f64122c1SDave Airlie int h = output->index;
257f64122c1SDave Airlie struct qxl_head *head;
258f64122c1SDave Airlie
2592d856f94SGerd Hoffmann if (!qdev->monitors_config)
2602d856f94SGerd Hoffmann return 0;
26121c76bd1SGerd Hoffmann if (h >= qxl_num_crtc)
2622d856f94SGerd Hoffmann return 0;
26307f8d9bdSDave Airlie if (!qdev->client_monitors_config)
264f64122c1SDave Airlie return 0;
2652d856f94SGerd Hoffmann if (h >= qdev->client_monitors_config->count)
2662d856f94SGerd Hoffmann return 0;
2672d856f94SGerd Hoffmann
26807f8d9bdSDave Airlie head = &qdev->client_monitors_config->heads[h];
2692d856f94SGerd Hoffmann DRM_DEBUG_KMS("head %d is %dx%d\n", h, head->width, head->height);
270f64122c1SDave Airlie
2711b043677SGerd Hoffmann return qxl_add_mode(connector, head->width, head->height, true);
272f64122c1SDave Airlie }
273f64122c1SDave Airlie
274bd3e1c7cSJonathon Jongsma static struct mode_size {
275f64122c1SDave Airlie int w;
276f64122c1SDave Airlie int h;
277b5f030b7SGerd Hoffmann } extra_modes[] = {
278f64122c1SDave Airlie { 720, 480},
279f64122c1SDave Airlie {1152, 768},
280f64122c1SDave Airlie {1280, 854},
281f64122c1SDave Airlie };
282f64122c1SDave Airlie
qxl_add_extra_modes(struct drm_connector * connector)283b5f030b7SGerd Hoffmann static int qxl_add_extra_modes(struct drm_connector *connector)
284bd3e1c7cSJonathon Jongsma {
2851b043677SGerd Hoffmann int i, ret = 0;
286408799ebSShayenne da Luz Moura
287b5f030b7SGerd Hoffmann for (i = 0; i < ARRAY_SIZE(extra_modes); i++)
2881b043677SGerd Hoffmann ret += qxl_add_mode(connector,
289b5f030b7SGerd Hoffmann extra_modes[i].w,
290b5f030b7SGerd Hoffmann extra_modes[i].h,
2911b043677SGerd Hoffmann false);
2921b043677SGerd Hoffmann return ret;
293f64122c1SDave Airlie }
294f64122c1SDave Airlie
qxl_send_monitors_config(struct qxl_device * qdev)295998010bfSGerd Hoffmann static void qxl_send_monitors_config(struct qxl_device *qdev)
296998010bfSGerd Hoffmann {
297998010bfSGerd Hoffmann int i;
298998010bfSGerd Hoffmann
299998010bfSGerd Hoffmann BUG_ON(!qdev->ram_header->monitors_config);
300998010bfSGerd Hoffmann
301998010bfSGerd Hoffmann if (qdev->monitors_config->count == 0)
302998010bfSGerd Hoffmann return;
303998010bfSGerd Hoffmann
304998010bfSGerd Hoffmann for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
305998010bfSGerd Hoffmann struct qxl_head *head = &qdev->monitors_config->heads[i];
306998010bfSGerd Hoffmann
307998010bfSGerd Hoffmann if (head->y > 8192 || head->x > 8192 ||
308998010bfSGerd Hoffmann head->width > 8192 || head->height > 8192) {
309998010bfSGerd Hoffmann DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
310998010bfSGerd Hoffmann i, head->width, head->height,
311998010bfSGerd Hoffmann head->x, head->y);
312998010bfSGerd Hoffmann return;
313998010bfSGerd Hoffmann }
314998010bfSGerd Hoffmann }
315998010bfSGerd Hoffmann qxl_io_monitors_config(qdev);
316998010bfSGerd Hoffmann }
317998010bfSGerd Hoffmann
qxl_crtc_update_monitors_config(struct drm_crtc * crtc,const char * reason)318a6d3c4d7SGerd Hoffmann static void qxl_crtc_update_monitors_config(struct drm_crtc *crtc,
319a6d3c4d7SGerd Hoffmann const char *reason)
320a6d3c4d7SGerd Hoffmann {
321a6d3c4d7SGerd Hoffmann struct drm_device *dev = crtc->dev;
322e304f8a0SDaniel Vetter struct qxl_device *qdev = to_qxl(dev);
323a6d3c4d7SGerd Hoffmann struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
324a6d3c4d7SGerd Hoffmann struct qxl_head head;
325a6d3c4d7SGerd Hoffmann int oldcount, i = qcrtc->index;
326a6d3c4d7SGerd Hoffmann
32716620544SGerd Hoffmann if (!qdev->primary_bo) {
328a6d3c4d7SGerd Hoffmann DRM_DEBUG_KMS("no primary surface, skip (%s)\n", reason);
329a6d3c4d7SGerd Hoffmann return;
330a6d3c4d7SGerd Hoffmann }
331a6d3c4d7SGerd Hoffmann
33221c76bd1SGerd Hoffmann if (!qdev->monitors_config || qxl_num_crtc <= i)
333a6d3c4d7SGerd Hoffmann return;
334a6d3c4d7SGerd Hoffmann
335a6d3c4d7SGerd Hoffmann head.id = i;
336a6d3c4d7SGerd Hoffmann head.flags = 0;
337738acd49SColin Ian King head.surface_id = 0;
338a6d3c4d7SGerd Hoffmann oldcount = qdev->monitors_config->count;
339a6d3c4d7SGerd Hoffmann if (crtc->state->active) {
340a6d3c4d7SGerd Hoffmann struct drm_display_mode *mode = &crtc->mode;
341408799ebSShayenne da Luz Moura
342a6d3c4d7SGerd Hoffmann head.width = mode->hdisplay;
343a6d3c4d7SGerd Hoffmann head.height = mode->vdisplay;
344a6d3c4d7SGerd Hoffmann head.x = crtc->x;
345a6d3c4d7SGerd Hoffmann head.y = crtc->y;
346a6d3c4d7SGerd Hoffmann if (qdev->monitors_config->count < i + 1)
347a6d3c4d7SGerd Hoffmann qdev->monitors_config->count = i + 1;
34890adda2cSGerd Hoffmann if (qdev->primary_bo == qdev->dumb_shadow_bo)
34990adda2cSGerd Hoffmann head.x += qdev->dumb_heads[i].x;
350a6d3c4d7SGerd Hoffmann } else if (i > 0) {
351a6d3c4d7SGerd Hoffmann head.width = 0;
352a6d3c4d7SGerd Hoffmann head.height = 0;
353a6d3c4d7SGerd Hoffmann head.x = 0;
354a6d3c4d7SGerd Hoffmann head.y = 0;
355a6d3c4d7SGerd Hoffmann if (qdev->monitors_config->count == i + 1)
356a6d3c4d7SGerd Hoffmann qdev->monitors_config->count = i;
357a6d3c4d7SGerd Hoffmann } else {
358a6d3c4d7SGerd Hoffmann DRM_DEBUG_KMS("inactive head 0, skip (%s)\n", reason);
359a6d3c4d7SGerd Hoffmann return;
360a6d3c4d7SGerd Hoffmann }
361a6d3c4d7SGerd Hoffmann
362a6d3c4d7SGerd Hoffmann if (head.width == qdev->monitors_config->heads[i].width &&
363a6d3c4d7SGerd Hoffmann head.height == qdev->monitors_config->heads[i].height &&
364a6d3c4d7SGerd Hoffmann head.x == qdev->monitors_config->heads[i].x &&
365a6d3c4d7SGerd Hoffmann head.y == qdev->monitors_config->heads[i].y &&
366a6d3c4d7SGerd Hoffmann oldcount == qdev->monitors_config->count)
367a6d3c4d7SGerd Hoffmann return;
368a6d3c4d7SGerd Hoffmann
369a6d3c4d7SGerd Hoffmann DRM_DEBUG_KMS("head %d, %dx%d, at +%d+%d, %s (%s)\n",
370a6d3c4d7SGerd Hoffmann i, head.width, head.height, head.x, head.y,
371a6d3c4d7SGerd Hoffmann crtc->state->active ? "on" : "off", reason);
372a6d3c4d7SGerd Hoffmann if (oldcount != qdev->monitors_config->count)
373a6d3c4d7SGerd Hoffmann DRM_DEBUG_KMS("active heads %d -> %d (%d total)\n",
374a6d3c4d7SGerd Hoffmann oldcount, qdev->monitors_config->count,
37521c76bd1SGerd Hoffmann qxl_num_crtc);
376a6d3c4d7SGerd Hoffmann
377a6d3c4d7SGerd Hoffmann qdev->monitors_config->heads[i] = head;
37821c76bd1SGerd Hoffmann qdev->monitors_config->max_allowed = qxl_num_crtc;
379a6d3c4d7SGerd Hoffmann qxl_send_monitors_config(qdev);
380a6d3c4d7SGerd Hoffmann }
381a6d3c4d7SGerd Hoffmann
qxl_crtc_atomic_flush(struct drm_crtc * crtc,struct drm_atomic_state * state)382c2ff6632SGabriel Krisman Bertazi static void qxl_crtc_atomic_flush(struct drm_crtc *crtc,
383f6ebe9f9SMaxime Ripard struct drm_atomic_state *state)
384c2ff6632SGabriel Krisman Bertazi {
385a6d3c4d7SGerd Hoffmann qxl_crtc_update_monitors_config(crtc, "flush");
386c2ff6632SGabriel Krisman Bertazi }
387c2ff6632SGabriel Krisman Bertazi
qxl_crtc_destroy(struct drm_crtc * crtc)388f64122c1SDave Airlie static void qxl_crtc_destroy(struct drm_crtc *crtc)
389f64122c1SDave Airlie {
390f64122c1SDave Airlie struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
391f64122c1SDave Airlie
3929428088cSRay Strode qxl_bo_unref(&qxl_crtc->cursor_bo);
393f64122c1SDave Airlie drm_crtc_cleanup(crtc);
394f64122c1SDave Airlie kfree(qxl_crtc);
395f64122c1SDave Airlie }
396f64122c1SDave Airlie
397f64122c1SDave Airlie static const struct drm_crtc_funcs qxl_crtc_funcs = {
398bc8a00d9SGabriel Krisman Bertazi .set_config = drm_atomic_helper_set_config,
399f64122c1SDave Airlie .destroy = qxl_crtc_destroy,
4009973c879SGabriel Krisman Bertazi .page_flip = drm_atomic_helper_page_flip,
4019ade8b98SGabriel Krisman Bertazi .reset = drm_atomic_helper_crtc_reset,
4029ade8b98SGabriel Krisman Bertazi .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
4039ade8b98SGabriel Krisman Bertazi .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
404f64122c1SDave Airlie };
405f64122c1SDave Airlie
qxl_framebuffer_surface_dirty(struct drm_framebuffer * fb,struct drm_file * file_priv,unsigned int flags,unsigned int color,struct drm_clip_rect * clips,unsigned int num_clips)4066d01f1f5SDave Airlie static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
407f64122c1SDave Airlie struct drm_file *file_priv,
4081b000494SShayenne da Luz Moura unsigned int flags, unsigned int color,
409f64122c1SDave Airlie struct drm_clip_rect *clips,
4101b000494SShayenne da Luz Moura unsigned int num_clips)
411f64122c1SDave Airlie {
412f64122c1SDave Airlie /* TODO: vmwgfx where this was cribbed from had locking. Why? */
413e304f8a0SDaniel Vetter struct qxl_device *qdev = to_qxl(fb->dev);
414f64122c1SDave Airlie struct drm_clip_rect norect;
415f64122c1SDave Airlie struct qxl_bo *qobj;
416bbaac135SSidong Yang struct drm_modeset_acquire_ctx ctx;
4174979904cSGerd Hoffmann bool is_primary;
418bbaac135SSidong Yang int inc = 1, ret;
419f64122c1SDave Airlie
420bbaac135SSidong Yang DRM_MODESET_LOCK_ALL_BEGIN(fb->dev, ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE, ret);
42173e9efd4SVille Syrjälä
422bf8744e4SPeter Wu qobj = gem_to_qxl_bo(fb->obj[0]);
423b2b4465dSDave Airlie /* if we aren't primary surface ignore this */
4244979904cSGerd Hoffmann is_primary = qobj->shadow ? qobj->shadow->is_primary : qobj->is_primary;
425bbaac135SSidong Yang if (!is_primary)
426bbaac135SSidong Yang goto out_lock_end;
427b2b4465dSDave Airlie
428f64122c1SDave Airlie if (!num_clips) {
429f64122c1SDave Airlie num_clips = 1;
430f64122c1SDave Airlie clips = &norect;
431f64122c1SDave Airlie norect.x1 = norect.y1 = 0;
432f64122c1SDave Airlie norect.x2 = fb->width;
433f64122c1SDave Airlie norect.y2 = fb->height;
434f64122c1SDave Airlie } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
435f64122c1SDave Airlie num_clips /= 2;
436f64122c1SDave Airlie inc = 2; /* skip source rects */
437f64122c1SDave Airlie }
438f64122c1SDave Airlie
439bf8744e4SPeter Wu qxl_draw_dirty_fb(qdev, fb, qobj, flags, color,
44090adda2cSGerd Hoffmann clips, num_clips, inc, 0);
44173e9efd4SVille Syrjälä
442bbaac135SSidong Yang out_lock_end:
443ce5c207cSDave Airlie DRM_MODESET_LOCK_ALL_END(fb->dev, ctx, ret);
44473e9efd4SVille Syrjälä
445f64122c1SDave Airlie return 0;
446f64122c1SDave Airlie }
447f64122c1SDave Airlie
448f64122c1SDave Airlie static const struct drm_framebuffer_funcs qxl_fb_funcs = {
449bf8744e4SPeter Wu .destroy = drm_gem_fb_destroy,
450f64122c1SDave Airlie .dirty = qxl_framebuffer_surface_dirty,
451bf8744e4SPeter Wu .create_handle = drm_gem_fb_create_handle,
452f64122c1SDave Airlie };
453f64122c1SDave Airlie
qxl_crtc_atomic_enable(struct drm_crtc * crtc,struct drm_atomic_state * state)4540b20a0f8SLaurent Pinchart static void qxl_crtc_atomic_enable(struct drm_crtc *crtc,
455351f950dSMaxime Ripard struct drm_atomic_state *state)
456f64122c1SDave Airlie {
457a6d3c4d7SGerd Hoffmann qxl_crtc_update_monitors_config(crtc, "enable");
458f64122c1SDave Airlie }
459f64122c1SDave Airlie
qxl_crtc_atomic_disable(struct drm_crtc * crtc,struct drm_atomic_state * state)46064581714SLaurent Pinchart static void qxl_crtc_atomic_disable(struct drm_crtc *crtc,
461351f950dSMaxime Ripard struct drm_atomic_state *state)
46207f8d9bdSDave Airlie {
463a6d3c4d7SGerd Hoffmann qxl_crtc_update_monitors_config(crtc, "disable");
46407f8d9bdSDave Airlie }
46507f8d9bdSDave Airlie
466f64122c1SDave Airlie static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
467c2ff6632SGabriel Krisman Bertazi .atomic_flush = qxl_crtc_atomic_flush,
4680b20a0f8SLaurent Pinchart .atomic_enable = qxl_crtc_atomic_enable,
46964581714SLaurent Pinchart .atomic_disable = qxl_crtc_atomic_disable,
470f64122c1SDave Airlie };
471f64122c1SDave Airlie
qxl_primary_atomic_check(struct drm_plane * plane,struct drm_atomic_state * state)47245dfe577SGerd Hoffmann static int qxl_primary_atomic_check(struct drm_plane *plane,
4737c11b99aSMaxime Ripard struct drm_atomic_state *state)
474c2ff6632SGabriel Krisman Bertazi {
4757c11b99aSMaxime Ripard struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
4767c11b99aSMaxime Ripard plane);
477e304f8a0SDaniel Vetter struct qxl_device *qdev = to_qxl(plane->dev);
478c2ff6632SGabriel Krisman Bertazi struct qxl_bo *bo;
479c2ff6632SGabriel Krisman Bertazi
480ba5c1649SMaxime Ripard if (!new_plane_state->crtc || !new_plane_state->fb)
481c2ff6632SGabriel Krisman Bertazi return 0;
482c2ff6632SGabriel Krisman Bertazi
483ba5c1649SMaxime Ripard bo = gem_to_qxl_bo(new_plane_state->fb->obj[0]);
484c2ff6632SGabriel Krisman Bertazi
485feba24deSGerd Hoffmann return qxl_check_framebuffer(qdev, bo);
486c2ff6632SGabriel Krisman Bertazi }
487c2ff6632SGabriel Krisman Bertazi
qxl_primary_apply_cursor(struct qxl_device * qdev,struct drm_plane_state * plane_state)488b4b27f08SGerd Hoffmann static int qxl_primary_apply_cursor(struct qxl_device *qdev,
489b4b27f08SGerd Hoffmann struct drm_plane_state *plane_state)
4909428088cSRay Strode {
491b4b27f08SGerd Hoffmann struct drm_framebuffer *fb = plane_state->fb;
492b4b27f08SGerd Hoffmann struct qxl_crtc *qcrtc = to_qxl_crtc(plane_state->crtc);
4939428088cSRay Strode struct qxl_cursor_cmd *cmd;
4949428088cSRay Strode struct qxl_release *release;
4959428088cSRay Strode int ret = 0;
4969428088cSRay Strode
4979428088cSRay Strode if (!qcrtc->cursor_bo)
4989428088cSRay Strode return 0;
4999428088cSRay Strode
5009428088cSRay Strode ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
5019428088cSRay Strode QXL_RELEASE_CURSOR_CMD,
5029428088cSRay Strode &release, NULL);
5039428088cSRay Strode if (ret)
5049428088cSRay Strode return ret;
5059428088cSRay Strode
5069428088cSRay Strode ret = qxl_release_list_add(release, qcrtc->cursor_bo);
5079428088cSRay Strode if (ret)
5089428088cSRay Strode goto out_free_release;
5099428088cSRay Strode
5109428088cSRay Strode ret = qxl_release_reserve_list(release, false);
5119428088cSRay Strode if (ret)
5129428088cSRay Strode goto out_free_release;
5139428088cSRay Strode
5149428088cSRay Strode cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
5159428088cSRay Strode cmd->type = QXL_CURSOR_SET;
516b4b27f08SGerd Hoffmann cmd->u.set.position.x = plane_state->crtc_x + fb->hot_x;
517b4b27f08SGerd Hoffmann cmd->u.set.position.y = plane_state->crtc_y + fb->hot_y;
5189428088cSRay Strode
5199428088cSRay Strode cmd->u.set.shape = qxl_bo_physical_address(qdev, qcrtc->cursor_bo, 0);
5209428088cSRay Strode
5219428088cSRay Strode cmd->u.set.visible = 1;
5229428088cSRay Strode qxl_release_unmap(qdev, release, &cmd->release_info);
5239428088cSRay Strode
5249428088cSRay Strode qxl_release_fence_buffer_objects(release);
525933db733SVasily Averin qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
5269428088cSRay Strode
5279428088cSRay Strode return ret;
5289428088cSRay Strode
5299428088cSRay Strode out_free_release:
5309428088cSRay Strode qxl_release_free(qdev, release);
5319428088cSRay Strode return ret;
5329428088cSRay Strode }
5339428088cSRay Strode
qxl_primary_move_cursor(struct qxl_device * qdev,struct drm_plane_state * plane_state)534b4b27f08SGerd Hoffmann static int qxl_primary_move_cursor(struct qxl_device *qdev,
535b4b27f08SGerd Hoffmann struct drm_plane_state *plane_state)
536c2ff6632SGabriel Krisman Bertazi {
537b4b27f08SGerd Hoffmann struct drm_framebuffer *fb = plane_state->fb;
538b4b27f08SGerd Hoffmann struct qxl_crtc *qcrtc = to_qxl_crtc(plane_state->crtc);
539b4b27f08SGerd Hoffmann struct qxl_cursor_cmd *cmd;
540b4b27f08SGerd Hoffmann struct qxl_release *release;
541b4b27f08SGerd Hoffmann int ret = 0;
542b4b27f08SGerd Hoffmann
543b4b27f08SGerd Hoffmann if (!qcrtc->cursor_bo)
544b4b27f08SGerd Hoffmann return 0;
545b4b27f08SGerd Hoffmann
546b4b27f08SGerd Hoffmann ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
547b4b27f08SGerd Hoffmann QXL_RELEASE_CURSOR_CMD,
548b4b27f08SGerd Hoffmann &release, NULL);
549b4b27f08SGerd Hoffmann if (ret)
550b4b27f08SGerd Hoffmann return ret;
551b4b27f08SGerd Hoffmann
552b4b27f08SGerd Hoffmann ret = qxl_release_reserve_list(release, true);
553b4b27f08SGerd Hoffmann if (ret) {
554b4b27f08SGerd Hoffmann qxl_release_free(qdev, release);
555b4b27f08SGerd Hoffmann return ret;
556b4b27f08SGerd Hoffmann }
557b4b27f08SGerd Hoffmann
558b4b27f08SGerd Hoffmann cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
559b4b27f08SGerd Hoffmann cmd->type = QXL_CURSOR_MOVE;
560b4b27f08SGerd Hoffmann cmd->u.position.x = plane_state->crtc_x + fb->hot_x;
561b4b27f08SGerd Hoffmann cmd->u.position.y = plane_state->crtc_y + fb->hot_y;
562b4b27f08SGerd Hoffmann qxl_release_unmap(qdev, release, &cmd->release_info);
563b4b27f08SGerd Hoffmann
564b4b27f08SGerd Hoffmann qxl_release_fence_buffer_objects(release);
565b4b27f08SGerd Hoffmann qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
566b4b27f08SGerd Hoffmann return ret;
567b4b27f08SGerd Hoffmann }
568b4b27f08SGerd Hoffmann
qxl_create_cursor(struct qxl_device * qdev,struct qxl_bo * user_bo,int hot_x,int hot_y)569b4b27f08SGerd Hoffmann static struct qxl_bo *qxl_create_cursor(struct qxl_device *qdev,
570b4b27f08SGerd Hoffmann struct qxl_bo *user_bo,
571b4b27f08SGerd Hoffmann int hot_x, int hot_y)
572b4b27f08SGerd Hoffmann {
573b4b27f08SGerd Hoffmann static const u32 size = 64 * 64 * 4;
574b4b27f08SGerd Hoffmann struct qxl_bo *cursor_bo;
5757938f421SLucas De Marchi struct iosys_map cursor_map;
5767938f421SLucas De Marchi struct iosys_map user_map;
577b4b27f08SGerd Hoffmann struct qxl_cursor cursor;
578b4b27f08SGerd Hoffmann int ret;
579b4b27f08SGerd Hoffmann
580b4b27f08SGerd Hoffmann if (!user_bo)
581b4b27f08SGerd Hoffmann return NULL;
582b4b27f08SGerd Hoffmann
583b4b27f08SGerd Hoffmann ret = qxl_bo_create(qdev, sizeof(struct qxl_cursor) + size,
584b4b27f08SGerd Hoffmann false, true, QXL_GEM_DOMAIN_VRAM, 1,
585b4b27f08SGerd Hoffmann NULL, &cursor_bo);
586b4b27f08SGerd Hoffmann if (ret)
587b4b27f08SGerd Hoffmann goto err;
588b4b27f08SGerd Hoffmann
589b4b27f08SGerd Hoffmann ret = qxl_bo_vmap(cursor_bo, &cursor_map);
590b4b27f08SGerd Hoffmann if (ret)
591b4b27f08SGerd Hoffmann goto err_unref;
592b4b27f08SGerd Hoffmann
593b4b27f08SGerd Hoffmann ret = qxl_bo_vmap(user_bo, &user_map);
594b4b27f08SGerd Hoffmann if (ret)
595b4b27f08SGerd Hoffmann goto err_unmap;
596b4b27f08SGerd Hoffmann
597b4b27f08SGerd Hoffmann cursor.header.unique = 0;
598b4b27f08SGerd Hoffmann cursor.header.type = SPICE_CURSOR_TYPE_ALPHA;
599b4b27f08SGerd Hoffmann cursor.header.width = 64;
600b4b27f08SGerd Hoffmann cursor.header.height = 64;
601b4b27f08SGerd Hoffmann cursor.header.hot_spot_x = hot_x;
602b4b27f08SGerd Hoffmann cursor.header.hot_spot_y = hot_y;
603b4b27f08SGerd Hoffmann cursor.data_size = size;
604b4b27f08SGerd Hoffmann cursor.chunk.next_chunk = 0;
605b4b27f08SGerd Hoffmann cursor.chunk.prev_chunk = 0;
606b4b27f08SGerd Hoffmann cursor.chunk.data_size = size;
607b4b27f08SGerd Hoffmann if (cursor_map.is_iomem) {
608b4b27f08SGerd Hoffmann memcpy_toio(cursor_map.vaddr_iomem,
609b4b27f08SGerd Hoffmann &cursor, sizeof(cursor));
610b4b27f08SGerd Hoffmann memcpy_toio(cursor_map.vaddr_iomem + sizeof(cursor),
611b4b27f08SGerd Hoffmann user_map.vaddr, size);
612b4b27f08SGerd Hoffmann } else {
613b4b27f08SGerd Hoffmann memcpy(cursor_map.vaddr,
614b4b27f08SGerd Hoffmann &cursor, sizeof(cursor));
615b4b27f08SGerd Hoffmann memcpy(cursor_map.vaddr + sizeof(cursor),
616b4b27f08SGerd Hoffmann user_map.vaddr, size);
617b4b27f08SGerd Hoffmann }
618b4b27f08SGerd Hoffmann
619b4b27f08SGerd Hoffmann qxl_bo_vunmap(user_bo);
620b4b27f08SGerd Hoffmann qxl_bo_vunmap(cursor_bo);
621b4b27f08SGerd Hoffmann return cursor_bo;
622b4b27f08SGerd Hoffmann
623b4b27f08SGerd Hoffmann err_unmap:
624b4b27f08SGerd Hoffmann qxl_bo_vunmap(cursor_bo);
625b4b27f08SGerd Hoffmann err_unref:
626b4b27f08SGerd Hoffmann qxl_bo_unpin(cursor_bo);
627b4b27f08SGerd Hoffmann qxl_bo_unref(&cursor_bo);
628b4b27f08SGerd Hoffmann err:
629b4b27f08SGerd Hoffmann return NULL;
630b4b27f08SGerd Hoffmann }
631b4b27f08SGerd Hoffmann
qxl_free_cursor(struct qxl_bo * cursor_bo)632b4b27f08SGerd Hoffmann static void qxl_free_cursor(struct qxl_bo *cursor_bo)
633b4b27f08SGerd Hoffmann {
634b4b27f08SGerd Hoffmann if (!cursor_bo)
635b4b27f08SGerd Hoffmann return;
636b4b27f08SGerd Hoffmann
637b4b27f08SGerd Hoffmann qxl_bo_unpin(cursor_bo);
638b4b27f08SGerd Hoffmann qxl_bo_unref(&cursor_bo);
639b4b27f08SGerd Hoffmann }
640b4b27f08SGerd Hoffmann
qxl_primary_atomic_update(struct drm_plane * plane,struct drm_atomic_state * state)641c2ff6632SGabriel Krisman Bertazi static void qxl_primary_atomic_update(struct drm_plane *plane,
642977697e2SMaxime Ripard struct drm_atomic_state *state)
643c2ff6632SGabriel Krisman Bertazi {
64437418bf1SMaxime Ripard struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
64537418bf1SMaxime Ripard plane);
646e304f8a0SDaniel Vetter struct qxl_device *qdev = to_qxl(plane->dev);
647e05162c0SMaxime Ripard struct qxl_bo *bo = gem_to_qxl_bo(new_state->fb->obj[0]);
648fb8cd60cSYueHaibing struct qxl_bo *primary;
649c2ff6632SGabriel Krisman Bertazi struct drm_clip_rect norect = {
650c2ff6632SGabriel Krisman Bertazi .x1 = 0,
651c2ff6632SGabriel Krisman Bertazi .y1 = 0,
652e05162c0SMaxime Ripard .x2 = new_state->fb->width,
653e05162c0SMaxime Ripard .y2 = new_state->fb->height
654c2ff6632SGabriel Krisman Bertazi };
65590adda2cSGerd Hoffmann uint32_t dumb_shadow_offset = 0;
656c2ff6632SGabriel Krisman Bertazi
6574979904cSGerd Hoffmann primary = bo->shadow ? bo->shadow : bo;
658b0e07da3SGerd Hoffmann
6594979904cSGerd Hoffmann if (!primary->is_primary) {
6604979904cSGerd Hoffmann if (qdev->primary_bo)
661b0e07da3SGerd Hoffmann qxl_io_destroy_primary(qdev);
6624979904cSGerd Hoffmann qxl_io_create_primary(qdev, primary);
663b4b27f08SGerd Hoffmann qxl_primary_apply_cursor(qdev, plane->state);
6641f85535cSGerd Hoffmann }
66562676d10SGerd Hoffmann
66690adda2cSGerd Hoffmann if (bo->is_dumb)
66790adda2cSGerd Hoffmann dumb_shadow_offset =
668e05162c0SMaxime Ripard qdev->dumb_heads[new_state->crtc->index].x;
66990adda2cSGerd Hoffmann
670e05162c0SMaxime Ripard qxl_draw_dirty_fb(qdev, new_state->fb, bo, 0, 0, &norect, 1, 1,
67190adda2cSGerd Hoffmann dumb_shadow_offset);
672c2ff6632SGabriel Krisman Bertazi }
673c2ff6632SGabriel Krisman Bertazi
qxl_primary_atomic_disable(struct drm_plane * plane,struct drm_atomic_state * state)674c2ff6632SGabriel Krisman Bertazi static void qxl_primary_atomic_disable(struct drm_plane *plane,
675977697e2SMaxime Ripard struct drm_atomic_state *state)
676c2ff6632SGabriel Krisman Bertazi {
677977697e2SMaxime Ripard struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
678977697e2SMaxime Ripard plane);
679e304f8a0SDaniel Vetter struct qxl_device *qdev = to_qxl(plane->dev);
680c2ff6632SGabriel Krisman Bertazi
681b0e07da3SGerd Hoffmann if (old_state->fb) {
682bf8744e4SPeter Wu struct qxl_bo *bo = gem_to_qxl_bo(old_state->fb->obj[0]);
683c2ff6632SGabriel Krisman Bertazi
684a7709b9bSGerd Hoffmann if (bo->shadow)
685a7709b9bSGerd Hoffmann bo = bo->shadow;
6861d878df0SGerd Hoffmann if (bo->is_primary)
687c2ff6632SGabriel Krisman Bertazi qxl_io_destroy_primary(qdev);
688c2ff6632SGabriel Krisman Bertazi }
689b0e07da3SGerd Hoffmann }
690c2ff6632SGabriel Krisman Bertazi
qxl_cursor_atomic_update(struct drm_plane * plane,struct drm_atomic_state * state)6911277eed5SGabriel Krisman Bertazi static void qxl_cursor_atomic_update(struct drm_plane *plane,
692977697e2SMaxime Ripard struct drm_atomic_state *state)
6931277eed5SGabriel Krisman Bertazi {
694977697e2SMaxime Ripard struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
695977697e2SMaxime Ripard plane);
69637418bf1SMaxime Ripard struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
69737418bf1SMaxime Ripard plane);
698b4b27f08SGerd Hoffmann struct qxl_device *qdev = to_qxl(plane->dev);
699e05162c0SMaxime Ripard struct drm_framebuffer *fb = new_state->fb;
7001277eed5SGabriel Krisman Bertazi
7011277eed5SGabriel Krisman Bertazi if (fb != old_state->fb) {
702e05162c0SMaxime Ripard qxl_primary_apply_cursor(qdev, new_state);
7031277eed5SGabriel Krisman Bertazi } else {
704e05162c0SMaxime Ripard qxl_primary_move_cursor(qdev, new_state);
7051277eed5SGabriel Krisman Bertazi }
7061277eed5SGabriel Krisman Bertazi }
7071277eed5SGabriel Krisman Bertazi
qxl_cursor_atomic_disable(struct drm_plane * plane,struct drm_atomic_state * state)70845dfe577SGerd Hoffmann static void qxl_cursor_atomic_disable(struct drm_plane *plane,
709977697e2SMaxime Ripard struct drm_atomic_state *state)
7101277eed5SGabriel Krisman Bertazi {
711977697e2SMaxime Ripard struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
712977697e2SMaxime Ripard plane);
713e304f8a0SDaniel Vetter struct qxl_device *qdev = to_qxl(plane->dev);
714b4b27f08SGerd Hoffmann struct qxl_crtc *qcrtc;
7151277eed5SGabriel Krisman Bertazi struct qxl_release *release;
7161277eed5SGabriel Krisman Bertazi struct qxl_cursor_cmd *cmd;
7171277eed5SGabriel Krisman Bertazi int ret;
7181277eed5SGabriel Krisman Bertazi
7191277eed5SGabriel Krisman Bertazi ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
7201277eed5SGabriel Krisman Bertazi QXL_RELEASE_CURSOR_CMD,
7211277eed5SGabriel Krisman Bertazi &release, NULL);
7221277eed5SGabriel Krisman Bertazi if (ret)
7231277eed5SGabriel Krisman Bertazi return;
7241277eed5SGabriel Krisman Bertazi
7251277eed5SGabriel Krisman Bertazi ret = qxl_release_reserve_list(release, true);
7261277eed5SGabriel Krisman Bertazi if (ret) {
7271277eed5SGabriel Krisman Bertazi qxl_release_free(qdev, release);
7281277eed5SGabriel Krisman Bertazi return;
7291277eed5SGabriel Krisman Bertazi }
7301277eed5SGabriel Krisman Bertazi
7311277eed5SGabriel Krisman Bertazi cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
7321277eed5SGabriel Krisman Bertazi cmd->type = QXL_CURSOR_HIDE;
7331277eed5SGabriel Krisman Bertazi qxl_release_unmap(qdev, release, &cmd->release_info);
7341277eed5SGabriel Krisman Bertazi
7351277eed5SGabriel Krisman Bertazi qxl_release_fence_buffer_objects(release);
736933db733SVasily Averin qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
737b4b27f08SGerd Hoffmann
738b4b27f08SGerd Hoffmann qcrtc = to_qxl_crtc(old_state->crtc);
739b4b27f08SGerd Hoffmann qxl_free_cursor(qcrtc->cursor_bo);
740b4b27f08SGerd Hoffmann qcrtc->cursor_bo = NULL;
7411277eed5SGabriel Krisman Bertazi }
7421277eed5SGabriel Krisman Bertazi
qxl_update_dumb_head(struct qxl_device * qdev,int index,struct qxl_bo * bo)74390adda2cSGerd Hoffmann static void qxl_update_dumb_head(struct qxl_device *qdev,
74490adda2cSGerd Hoffmann int index, struct qxl_bo *bo)
74590adda2cSGerd Hoffmann {
74690adda2cSGerd Hoffmann uint32_t width, height;
74790adda2cSGerd Hoffmann
74890adda2cSGerd Hoffmann if (index >= qdev->monitors_config->max_allowed)
74990adda2cSGerd Hoffmann return;
75090adda2cSGerd Hoffmann
75190adda2cSGerd Hoffmann if (bo && bo->is_dumb) {
75290adda2cSGerd Hoffmann width = bo->surf.width;
75390adda2cSGerd Hoffmann height = bo->surf.height;
75490adda2cSGerd Hoffmann } else {
75590adda2cSGerd Hoffmann width = 0;
75690adda2cSGerd Hoffmann height = 0;
75790adda2cSGerd Hoffmann }
75890adda2cSGerd Hoffmann
75990adda2cSGerd Hoffmann if (qdev->dumb_heads[index].width == width &&
76090adda2cSGerd Hoffmann qdev->dumb_heads[index].height == height)
76190adda2cSGerd Hoffmann return;
76290adda2cSGerd Hoffmann
76390adda2cSGerd Hoffmann DRM_DEBUG("#%d: %dx%d -> %dx%d\n", index,
76490adda2cSGerd Hoffmann qdev->dumb_heads[index].width,
76590adda2cSGerd Hoffmann qdev->dumb_heads[index].height,
76690adda2cSGerd Hoffmann width, height);
76790adda2cSGerd Hoffmann qdev->dumb_heads[index].width = width;
76890adda2cSGerd Hoffmann qdev->dumb_heads[index].height = height;
76990adda2cSGerd Hoffmann }
77090adda2cSGerd Hoffmann
qxl_calc_dumb_shadow(struct qxl_device * qdev,struct qxl_surface * surf)77190adda2cSGerd Hoffmann static void qxl_calc_dumb_shadow(struct qxl_device *qdev,
77290adda2cSGerd Hoffmann struct qxl_surface *surf)
77390adda2cSGerd Hoffmann {
77490adda2cSGerd Hoffmann struct qxl_head *head;
77590adda2cSGerd Hoffmann int i;
77690adda2cSGerd Hoffmann
77790adda2cSGerd Hoffmann memset(surf, 0, sizeof(*surf));
77890adda2cSGerd Hoffmann for (i = 0; i < qdev->monitors_config->max_allowed; i++) {
77990adda2cSGerd Hoffmann head = qdev->dumb_heads + i;
78090adda2cSGerd Hoffmann head->x = surf->width;
78190adda2cSGerd Hoffmann surf->width += head->width;
78290adda2cSGerd Hoffmann if (surf->height < head->height)
78390adda2cSGerd Hoffmann surf->height = head->height;
78490adda2cSGerd Hoffmann }
78590adda2cSGerd Hoffmann if (surf->width < 64)
78690adda2cSGerd Hoffmann surf->width = 64;
78790adda2cSGerd Hoffmann if (surf->height < 64)
78890adda2cSGerd Hoffmann surf->height = 64;
78990adda2cSGerd Hoffmann surf->format = SPICE_SURFACE_FMT_32_xRGB;
79090adda2cSGerd Hoffmann surf->stride = surf->width * 4;
79190adda2cSGerd Hoffmann
79290adda2cSGerd Hoffmann if (!qdev->dumb_shadow_bo ||
79390adda2cSGerd Hoffmann qdev->dumb_shadow_bo->surf.width != surf->width ||
79490adda2cSGerd Hoffmann qdev->dumb_shadow_bo->surf.height != surf->height)
79590adda2cSGerd Hoffmann DRM_DEBUG("%dx%d\n", surf->width, surf->height);
79690adda2cSGerd Hoffmann }
79790adda2cSGerd Hoffmann
qxl_prepare_shadow(struct qxl_device * qdev,struct qxl_bo * user_bo,int crtc_index)798ab422b01SGerd Hoffmann static void qxl_prepare_shadow(struct qxl_device *qdev, struct qxl_bo *user_bo,
799ab422b01SGerd Hoffmann int crtc_index)
8001277eed5SGabriel Krisman Bertazi {
80190adda2cSGerd Hoffmann struct qxl_surface surf;
8021277eed5SGabriel Krisman Bertazi
803ab422b01SGerd Hoffmann qxl_update_dumb_head(qdev, crtc_index,
80490adda2cSGerd Hoffmann user_bo);
80590adda2cSGerd Hoffmann qxl_calc_dumb_shadow(qdev, &surf);
80690adda2cSGerd Hoffmann if (!qdev->dumb_shadow_bo ||
80790adda2cSGerd Hoffmann qdev->dumb_shadow_bo->surf.width != surf.width ||
80890adda2cSGerd Hoffmann qdev->dumb_shadow_bo->surf.height != surf.height) {
80990adda2cSGerd Hoffmann if (qdev->dumb_shadow_bo) {
810af4c1a86SGerd Hoffmann qxl_bo_unpin(qdev->dumb_shadow_bo);
8119c86fb18SEmil Velikov drm_gem_object_put
812e0828d54SGerd Hoffmann (&qdev->dumb_shadow_bo->tbo.base);
81390adda2cSGerd Hoffmann qdev->dumb_shadow_bo = NULL;
81462676d10SGerd Hoffmann }
81590adda2cSGerd Hoffmann qxl_bo_create(qdev, surf.height * surf.stride,
8164fff19aeSGerd Hoffmann true, true, QXL_GEM_DOMAIN_SURFACE, 0,
8174fff19aeSGerd Hoffmann &surf, &qdev->dumb_shadow_bo);
81890adda2cSGerd Hoffmann }
81990adda2cSGerd Hoffmann if (user_bo->shadow != qdev->dumb_shadow_bo) {
82090adda2cSGerd Hoffmann if (user_bo->shadow) {
82176aefa4dSGerd Hoffmann qxl_bo_unpin(user_bo->shadow);
8229c86fb18SEmil Velikov drm_gem_object_put
823e0828d54SGerd Hoffmann (&user_bo->shadow->tbo.base);
82490adda2cSGerd Hoffmann user_bo->shadow = NULL;
82590adda2cSGerd Hoffmann }
826e0828d54SGerd Hoffmann drm_gem_object_get(&qdev->dumb_shadow_bo->tbo.base);
82790adda2cSGerd Hoffmann user_bo->shadow = qdev->dumb_shadow_bo;
82876aefa4dSGerd Hoffmann qxl_bo_pin(user_bo->shadow);
82962676d10SGerd Hoffmann }
83062676d10SGerd Hoffmann }
83162676d10SGerd Hoffmann
qxl_plane_prepare_fb(struct drm_plane * plane,struct drm_plane_state * new_state)832ab422b01SGerd Hoffmann static int qxl_plane_prepare_fb(struct drm_plane *plane,
833ab422b01SGerd Hoffmann struct drm_plane_state *new_state)
834ab422b01SGerd Hoffmann {
835ab422b01SGerd Hoffmann struct qxl_device *qdev = to_qxl(plane->dev);
836ab422b01SGerd Hoffmann struct drm_gem_object *obj;
837ab422b01SGerd Hoffmann struct qxl_bo *user_bo;
8386071c4c2SChristian König int ret;
839ab422b01SGerd Hoffmann
840ab422b01SGerd Hoffmann if (!new_state->fb)
841ab422b01SGerd Hoffmann return 0;
842ab422b01SGerd Hoffmann
843ab422b01SGerd Hoffmann obj = new_state->fb->obj[0];
844ab422b01SGerd Hoffmann user_bo = gem_to_qxl_bo(obj);
845ab422b01SGerd Hoffmann
846ab422b01SGerd Hoffmann if (plane->type == DRM_PLANE_TYPE_PRIMARY &&
847ab422b01SGerd Hoffmann user_bo->is_dumb) {
848ab422b01SGerd Hoffmann qxl_prepare_shadow(qdev, user_bo, new_state->crtc->index);
849ab422b01SGerd Hoffmann }
850ab422b01SGerd Hoffmann
851b4b27f08SGerd Hoffmann if (plane->type == DRM_PLANE_TYPE_CURSOR &&
852b4b27f08SGerd Hoffmann plane->state->fb != new_state->fb) {
853b4b27f08SGerd Hoffmann struct qxl_crtc *qcrtc = to_qxl_crtc(new_state->crtc);
854b4b27f08SGerd Hoffmann struct qxl_bo *old_cursor_bo = qcrtc->cursor_bo;
855b4b27f08SGerd Hoffmann
856b4b27f08SGerd Hoffmann qcrtc->cursor_bo = qxl_create_cursor(qdev, user_bo,
857b4b27f08SGerd Hoffmann new_state->fb->hot_x,
858b4b27f08SGerd Hoffmann new_state->fb->hot_y);
859b4b27f08SGerd Hoffmann qxl_free_cursor(old_cursor_bo);
860b4b27f08SGerd Hoffmann }
861b4b27f08SGerd Hoffmann
8626071c4c2SChristian König ret = qxl_bo_pin(user_bo);
8636071c4c2SChristian König if (ret)
8646071c4c2SChristian König return ret;
8656071c4c2SChristian König
8666071c4c2SChristian König return drm_gem_plane_helper_prepare_fb(plane, new_state);
8671277eed5SGabriel Krisman Bertazi }
8681277eed5SGabriel Krisman Bertazi
qxl_plane_cleanup_fb(struct drm_plane * plane,struct drm_plane_state * old_state)8691277eed5SGabriel Krisman Bertazi static void qxl_plane_cleanup_fb(struct drm_plane *plane,
8701277eed5SGabriel Krisman Bertazi struct drm_plane_state *old_state)
8711277eed5SGabriel Krisman Bertazi {
8721277eed5SGabriel Krisman Bertazi struct drm_gem_object *obj;
8731277eed5SGabriel Krisman Bertazi struct qxl_bo *user_bo;
8741277eed5SGabriel Krisman Bertazi
8755f3d862aSGerd Hoffmann if (!old_state->fb) {
8765f3d862aSGerd Hoffmann /*
8775f3d862aSGerd Hoffmann * we never executed prepare_fb, so there's nothing to
8781277eed5SGabriel Krisman Bertazi * unpin.
8791277eed5SGabriel Krisman Bertazi */
8801277eed5SGabriel Krisman Bertazi return;
8811277eed5SGabriel Krisman Bertazi }
8821277eed5SGabriel Krisman Bertazi
883bf8744e4SPeter Wu obj = old_state->fb->obj[0];
8841277eed5SGabriel Krisman Bertazi user_bo = gem_to_qxl_bo(obj);
8851277eed5SGabriel Krisman Bertazi qxl_bo_unpin(user_bo);
88662676d10SGerd Hoffmann
88790adda2cSGerd Hoffmann if (old_state->fb != plane->state->fb && user_bo->shadow) {
88876aefa4dSGerd Hoffmann qxl_bo_unpin(user_bo->shadow);
8899c86fb18SEmil Velikov drm_gem_object_put(&user_bo->shadow->tbo.base);
89062676d10SGerd Hoffmann user_bo->shadow = NULL;
89162676d10SGerd Hoffmann }
8921277eed5SGabriel Krisman Bertazi }
8931277eed5SGabriel Krisman Bertazi
8941277eed5SGabriel Krisman Bertazi static const uint32_t qxl_cursor_plane_formats[] = {
8951277eed5SGabriel Krisman Bertazi DRM_FORMAT_ARGB8888,
8961277eed5SGabriel Krisman Bertazi };
8971277eed5SGabriel Krisman Bertazi
8981277eed5SGabriel Krisman Bertazi static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = {
8991277eed5SGabriel Krisman Bertazi .atomic_update = qxl_cursor_atomic_update,
9001277eed5SGabriel Krisman Bertazi .atomic_disable = qxl_cursor_atomic_disable,
9011277eed5SGabriel Krisman Bertazi .prepare_fb = qxl_plane_prepare_fb,
9021277eed5SGabriel Krisman Bertazi .cleanup_fb = qxl_plane_cleanup_fb,
9031277eed5SGabriel Krisman Bertazi };
9041277eed5SGabriel Krisman Bertazi
9051277eed5SGabriel Krisman Bertazi static const struct drm_plane_funcs qxl_cursor_plane_funcs = {
906472e6d46SGabriel Krisman Bertazi .update_plane = drm_atomic_helper_update_plane,
907472e6d46SGabriel Krisman Bertazi .disable_plane = drm_atomic_helper_disable_plane,
90830c63715SThomas Zimmermann .destroy = drm_plane_helper_destroy,
9099ade8b98SGabriel Krisman Bertazi .reset = drm_atomic_helper_plane_reset,
9109ade8b98SGabriel Krisman Bertazi .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
9119ade8b98SGabriel Krisman Bertazi .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
9121277eed5SGabriel Krisman Bertazi };
9131277eed5SGabriel Krisman Bertazi
914d3e7e42dSGabriel Krisman Bertazi static const uint32_t qxl_primary_plane_formats[] = {
915d3e7e42dSGabriel Krisman Bertazi DRM_FORMAT_XRGB8888,
916d3e7e42dSGabriel Krisman Bertazi DRM_FORMAT_ARGB8888,
917d3e7e42dSGabriel Krisman Bertazi };
918d3e7e42dSGabriel Krisman Bertazi
919c2ff6632SGabriel Krisman Bertazi static const struct drm_plane_helper_funcs primary_helper_funcs = {
920c2ff6632SGabriel Krisman Bertazi .atomic_check = qxl_primary_atomic_check,
921c2ff6632SGabriel Krisman Bertazi .atomic_update = qxl_primary_atomic_update,
922c2ff6632SGabriel Krisman Bertazi .atomic_disable = qxl_primary_atomic_disable,
923c2ff6632SGabriel Krisman Bertazi .prepare_fb = qxl_plane_prepare_fb,
924c2ff6632SGabriel Krisman Bertazi .cleanup_fb = qxl_plane_cleanup_fb,
925c2ff6632SGabriel Krisman Bertazi };
926c2ff6632SGabriel Krisman Bertazi
927d3e7e42dSGabriel Krisman Bertazi static const struct drm_plane_funcs qxl_primary_plane_funcs = {
928472e6d46SGabriel Krisman Bertazi .update_plane = drm_atomic_helper_update_plane,
929472e6d46SGabriel Krisman Bertazi .disable_plane = drm_atomic_helper_disable_plane,
93030c63715SThomas Zimmermann .destroy = drm_plane_helper_destroy,
9319ade8b98SGabriel Krisman Bertazi .reset = drm_atomic_helper_plane_reset,
9329ade8b98SGabriel Krisman Bertazi .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
9339ade8b98SGabriel Krisman Bertazi .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
934d3e7e42dSGabriel Krisman Bertazi };
935d3e7e42dSGabriel Krisman Bertazi
qxl_create_plane(struct qxl_device * qdev,unsigned int possible_crtcs,enum drm_plane_type type)936d3e7e42dSGabriel Krisman Bertazi static struct drm_plane *qxl_create_plane(struct qxl_device *qdev,
937d3e7e42dSGabriel Krisman Bertazi unsigned int possible_crtcs,
938d3e7e42dSGabriel Krisman Bertazi enum drm_plane_type type)
939d3e7e42dSGabriel Krisman Bertazi {
940d3e7e42dSGabriel Krisman Bertazi const struct drm_plane_helper_funcs *helper_funcs = NULL;
941d3e7e42dSGabriel Krisman Bertazi struct drm_plane *plane;
942d3e7e42dSGabriel Krisman Bertazi const struct drm_plane_funcs *funcs;
943d3e7e42dSGabriel Krisman Bertazi const uint32_t *formats;
944d3e7e42dSGabriel Krisman Bertazi int num_formats;
945d3e7e42dSGabriel Krisman Bertazi int err;
946d3e7e42dSGabriel Krisman Bertazi
947d3e7e42dSGabriel Krisman Bertazi if (type == DRM_PLANE_TYPE_PRIMARY) {
948d3e7e42dSGabriel Krisman Bertazi funcs = &qxl_primary_plane_funcs;
949d3e7e42dSGabriel Krisman Bertazi formats = qxl_primary_plane_formats;
950d3e7e42dSGabriel Krisman Bertazi num_formats = ARRAY_SIZE(qxl_primary_plane_formats);
951c2ff6632SGabriel Krisman Bertazi helper_funcs = &primary_helper_funcs;
9521277eed5SGabriel Krisman Bertazi } else if (type == DRM_PLANE_TYPE_CURSOR) {
9531277eed5SGabriel Krisman Bertazi funcs = &qxl_cursor_plane_funcs;
9541277eed5SGabriel Krisman Bertazi formats = qxl_cursor_plane_formats;
9551277eed5SGabriel Krisman Bertazi helper_funcs = &qxl_cursor_helper_funcs;
9561277eed5SGabriel Krisman Bertazi num_formats = ARRAY_SIZE(qxl_cursor_plane_formats);
957d3e7e42dSGabriel Krisman Bertazi } else {
958d3e7e42dSGabriel Krisman Bertazi return ERR_PTR(-EINVAL);
959d3e7e42dSGabriel Krisman Bertazi }
960d3e7e42dSGabriel Krisman Bertazi
961d3e7e42dSGabriel Krisman Bertazi plane = kzalloc(sizeof(*plane), GFP_KERNEL);
962d3e7e42dSGabriel Krisman Bertazi if (!plane)
963d3e7e42dSGabriel Krisman Bertazi return ERR_PTR(-ENOMEM);
964d3e7e42dSGabriel Krisman Bertazi
965d3e7e42dSGabriel Krisman Bertazi err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs,
966d3e7e42dSGabriel Krisman Bertazi funcs, formats, num_formats,
967e6fc3b68SBen Widawsky NULL, type, NULL);
968d3e7e42dSGabriel Krisman Bertazi if (err)
969d3e7e42dSGabriel Krisman Bertazi goto free_plane;
970d3e7e42dSGabriel Krisman Bertazi
971d3e7e42dSGabriel Krisman Bertazi drm_plane_helper_add(plane, helper_funcs);
972d3e7e42dSGabriel Krisman Bertazi
973d3e7e42dSGabriel Krisman Bertazi return plane;
974d3e7e42dSGabriel Krisman Bertazi
975d3e7e42dSGabriel Krisman Bertazi free_plane:
976d3e7e42dSGabriel Krisman Bertazi kfree(plane);
977d3e7e42dSGabriel Krisman Bertazi return ERR_PTR(-EINVAL);
978d3e7e42dSGabriel Krisman Bertazi }
979d3e7e42dSGabriel Krisman Bertazi
qdev_crtc_init(struct drm_device * dev,int crtc_id)98007f8d9bdSDave Airlie static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
981f64122c1SDave Airlie {
982f64122c1SDave Airlie struct qxl_crtc *qxl_crtc;
9831277eed5SGabriel Krisman Bertazi struct drm_plane *primary, *cursor;
984e304f8a0SDaniel Vetter struct qxl_device *qdev = to_qxl(dev);
985d3e7e42dSGabriel Krisman Bertazi int r;
986f64122c1SDave Airlie
987f64122c1SDave Airlie qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
988f64122c1SDave Airlie if (!qxl_crtc)
989f64122c1SDave Airlie return -ENOMEM;
990f64122c1SDave Airlie
991d3e7e42dSGabriel Krisman Bertazi primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY);
992d3e7e42dSGabriel Krisman Bertazi if (IS_ERR(primary)) {
993d3e7e42dSGabriel Krisman Bertazi r = -ENOMEM;
994d3e7e42dSGabriel Krisman Bertazi goto free_mem;
995d3e7e42dSGabriel Krisman Bertazi }
996d3e7e42dSGabriel Krisman Bertazi
9971277eed5SGabriel Krisman Bertazi cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR);
9981277eed5SGabriel Krisman Bertazi if (IS_ERR(cursor)) {
9991277eed5SGabriel Krisman Bertazi r = -ENOMEM;
10001277eed5SGabriel Krisman Bertazi goto clean_primary;
10011277eed5SGabriel Krisman Bertazi }
10021277eed5SGabriel Krisman Bertazi
10031277eed5SGabriel Krisman Bertazi r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor,
1004d3e7e42dSGabriel Krisman Bertazi &qxl_crtc_funcs, NULL);
1005d3e7e42dSGabriel Krisman Bertazi if (r)
10061277eed5SGabriel Krisman Bertazi goto clean_cursor;
1007d3e7e42dSGabriel Krisman Bertazi
100807f8d9bdSDave Airlie qxl_crtc->index = crtc_id;
1009f64122c1SDave Airlie drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
1010f64122c1SDave Airlie return 0;
1011d3e7e42dSGabriel Krisman Bertazi
10121277eed5SGabriel Krisman Bertazi clean_cursor:
10131277eed5SGabriel Krisman Bertazi drm_plane_cleanup(cursor);
10141277eed5SGabriel Krisman Bertazi kfree(cursor);
1015d3e7e42dSGabriel Krisman Bertazi clean_primary:
1016d3e7e42dSGabriel Krisman Bertazi drm_plane_cleanup(primary);
1017d3e7e42dSGabriel Krisman Bertazi kfree(primary);
1018d3e7e42dSGabriel Krisman Bertazi free_mem:
1019d3e7e42dSGabriel Krisman Bertazi kfree(qxl_crtc);
1020d3e7e42dSGabriel Krisman Bertazi return r;
1021f64122c1SDave Airlie }
1022f64122c1SDave Airlie
qxl_conn_get_modes(struct drm_connector * connector)1023f64122c1SDave Airlie static int qxl_conn_get_modes(struct drm_connector *connector)
1024f64122c1SDave Airlie {
10251b043677SGerd Hoffmann struct drm_device *dev = connector->dev;
1026e304f8a0SDaniel Vetter struct qxl_device *qdev = to_qxl(dev);
10271b043677SGerd Hoffmann struct qxl_output *output = drm_connector_to_qxl_output(connector);
10281b000494SShayenne da Luz Moura unsigned int pwidth = 1024;
10291b000494SShayenne da Luz Moura unsigned int pheight = 768;
10302d856f94SGerd Hoffmann int ret = 0;
1031f64122c1SDave Airlie
10321b043677SGerd Hoffmann if (qdev->client_monitors_config) {
10331b043677SGerd Hoffmann struct qxl_head *head;
10341b043677SGerd Hoffmann head = &qdev->client_monitors_config->heads[output->index];
10351b043677SGerd Hoffmann if (head->width)
10361b043677SGerd Hoffmann pwidth = head->width;
10371b043677SGerd Hoffmann if (head->height)
10381b043677SGerd Hoffmann pheight = head->height;
10391b043677SGerd Hoffmann }
10401b043677SGerd Hoffmann
1041b5f030b7SGerd Hoffmann ret += drm_add_modes_noedid(connector, 8192, 8192);
1042b5f030b7SGerd Hoffmann ret += qxl_add_extra_modes(connector);
10431b043677SGerd Hoffmann ret += qxl_add_monitors_config_modes(connector);
10441b043677SGerd Hoffmann drm_set_preferred_mode(connector, pwidth, pheight);
1045f64122c1SDave Airlie return ret;
1046f64122c1SDave Airlie }
1047f64122c1SDave Airlie
qxl_conn_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode)1048e0d92e16SLuc Van Oostenryck static enum drm_mode_status qxl_conn_mode_valid(struct drm_connector *connector,
1049f64122c1SDave Airlie struct drm_display_mode *mode)
1050f64122c1SDave Airlie {
1051bd3e1c7cSJonathon Jongsma struct drm_device *ddev = connector->dev;
1052e304f8a0SDaniel Vetter struct qxl_device *qdev = to_qxl(ddev);
1053bd3e1c7cSJonathon Jongsma
1054feba24deSGerd Hoffmann if (qxl_check_mode(qdev, mode->hdisplay, mode->vdisplay) != 0)
1055bd3e1c7cSJonathon Jongsma return MODE_BAD;
1056feba24deSGerd Hoffmann
1057feba24deSGerd Hoffmann return MODE_OK;
1058f64122c1SDave Airlie }
1059f64122c1SDave Airlie
qxl_best_encoder(struct drm_connector * connector)10606d01f1f5SDave Airlie static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
1061f64122c1SDave Airlie {
1062f64122c1SDave Airlie struct qxl_output *qxl_output =
1063f64122c1SDave Airlie drm_connector_to_qxl_output(connector);
1064f64122c1SDave Airlie
1065f64122c1SDave Airlie DRM_DEBUG("\n");
1066f64122c1SDave Airlie return &qxl_output->enc;
1067f64122c1SDave Airlie }
1068f64122c1SDave Airlie
1069f64122c1SDave Airlie static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
1070f64122c1SDave Airlie .get_modes = qxl_conn_get_modes,
1071f64122c1SDave Airlie .mode_valid = qxl_conn_mode_valid,
1072f64122c1SDave Airlie .best_encoder = qxl_best_encoder,
1073f64122c1SDave Airlie };
1074f64122c1SDave Airlie
qxl_conn_detect(struct drm_connector * connector,bool force)1075f64122c1SDave Airlie static enum drm_connector_status qxl_conn_detect(
1076f64122c1SDave Airlie struct drm_connector *connector,
1077f64122c1SDave Airlie bool force)
1078f64122c1SDave Airlie {
1079f64122c1SDave Airlie struct qxl_output *output =
1080f64122c1SDave Airlie drm_connector_to_qxl_output(connector);
1081f64122c1SDave Airlie struct drm_device *ddev = connector->dev;
1082e304f8a0SDaniel Vetter struct qxl_device *qdev = to_qxl(ddev);
108369e5d3f8SDave Airlie bool connected = false;
1084f64122c1SDave Airlie
1085f64122c1SDave Airlie /* The first monitor is always connected */
108669e5d3f8SDave Airlie if (!qdev->client_monitors_config) {
108769e5d3f8SDave Airlie if (output->index == 0)
108869e5d3f8SDave Airlie connected = true;
108969e5d3f8SDave Airlie } else
109069e5d3f8SDave Airlie connected = qdev->client_monitors_config->count > output->index &&
109169e5d3f8SDave Airlie qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
1092f64122c1SDave Airlie
10935cab51cbSMarc-André Lureau DRM_DEBUG("#%d connected: %d\n", output->index, connected);
10945cab51cbSMarc-André Lureau
1095f64122c1SDave Airlie return connected ? connector_status_connected
1096f64122c1SDave Airlie : connector_status_disconnected;
1097f64122c1SDave Airlie }
1098f64122c1SDave Airlie
qxl_conn_destroy(struct drm_connector * connector)1099f64122c1SDave Airlie static void qxl_conn_destroy(struct drm_connector *connector)
1100f64122c1SDave Airlie {
1101f64122c1SDave Airlie struct qxl_output *qxl_output =
1102f64122c1SDave Airlie drm_connector_to_qxl_output(connector);
1103f64122c1SDave Airlie
110434ea3d38SThomas Wood drm_connector_unregister(connector);
1105f64122c1SDave Airlie drm_connector_cleanup(connector);
1106f64122c1SDave Airlie kfree(qxl_output);
1107f64122c1SDave Airlie }
1108f64122c1SDave Airlie
1109f64122c1SDave Airlie static const struct drm_connector_funcs qxl_connector_funcs = {
1110f64122c1SDave Airlie .detect = qxl_conn_detect,
11116af3e656SVille Syrjälä .fill_modes = drm_helper_probe_single_connector_modes,
1112f64122c1SDave Airlie .destroy = qxl_conn_destroy,
11139ade8b98SGabriel Krisman Bertazi .reset = drm_atomic_helper_connector_reset,
11149ade8b98SGabriel Krisman Bertazi .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
11159ade8b98SGabriel Krisman Bertazi .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1116f64122c1SDave Airlie };
1117f64122c1SDave Airlie
qxl_mode_create_hotplug_mode_update_property(struct qxl_device * qdev)11184695b039SDave Airlie static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
11194695b039SDave Airlie {
11204695b039SDave Airlie if (qdev->hotplug_mode_update_property)
11214695b039SDave Airlie return 0;
11224695b039SDave Airlie
11234695b039SDave Airlie qdev->hotplug_mode_update_property =
1124cbdded7fSGabriel Krisman Bertazi drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
11254695b039SDave Airlie "hotplug_mode_update", 0, 1);
11264695b039SDave Airlie
11274695b039SDave Airlie return 0;
11284695b039SDave Airlie }
11294695b039SDave Airlie
qdev_output_init(struct drm_device * dev,int num_output)11306d01f1f5SDave Airlie static int qdev_output_init(struct drm_device *dev, int num_output)
1131f64122c1SDave Airlie {
1132e304f8a0SDaniel Vetter struct qxl_device *qdev = to_qxl(dev);
1133f64122c1SDave Airlie struct qxl_output *qxl_output;
1134f64122c1SDave Airlie struct drm_connector *connector;
1135f64122c1SDave Airlie struct drm_encoder *encoder;
11366f2bb119SThomas Zimmermann int ret;
1137f64122c1SDave Airlie
1138f64122c1SDave Airlie qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
1139f64122c1SDave Airlie if (!qxl_output)
1140f64122c1SDave Airlie return -ENOMEM;
1141f64122c1SDave Airlie
1142f64122c1SDave Airlie qxl_output->index = num_output;
1143f64122c1SDave Airlie
1144f64122c1SDave Airlie connector = &qxl_output->base;
1145f64122c1SDave Airlie encoder = &qxl_output->enc;
1146f64122c1SDave Airlie drm_connector_init(dev, &qxl_output->base,
1147f64122c1SDave Airlie &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
1148f64122c1SDave Airlie
11496f2bb119SThomas Zimmermann ret = drm_simple_encoder_init(dev, &qxl_output->enc,
11506f2bb119SThomas Zimmermann DRM_MODE_ENCODER_VIRTUAL);
11516f2bb119SThomas Zimmermann if (ret) {
11526f2bb119SThomas Zimmermann drm_err(dev, "drm_simple_encoder_init() failed, error %d\n",
11536f2bb119SThomas Zimmermann ret);
11546f2bb119SThomas Zimmermann goto err_drm_connector_cleanup;
11556f2bb119SThomas Zimmermann }
1156f64122c1SDave Airlie
11575ff91e44SDave Airlie /* we get HPD via client monitors config */
11585ff91e44SDave Airlie connector->polled = DRM_CONNECTOR_POLL_HPD;
1159f64122c1SDave Airlie encoder->possible_crtcs = 1 << num_output;
1160cde4c44dSDaniel Vetter drm_connector_attach_encoder(&qxl_output->base,
1161f64122c1SDave Airlie &qxl_output->enc);
1162f64122c1SDave Airlie drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
1163f64122c1SDave Airlie
11644695b039SDave Airlie drm_object_attach_property(&connector->base,
11654695b039SDave Airlie qdev->hotplug_mode_update_property, 0);
11667dea0941SDave Airlie drm_object_attach_property(&connector->base,
11677dea0941SDave Airlie dev->mode_config.suggested_x_property, 0);
11687dea0941SDave Airlie drm_object_attach_property(&connector->base,
11697dea0941SDave Airlie dev->mode_config.suggested_y_property, 0);
1170f64122c1SDave Airlie return 0;
11716f2bb119SThomas Zimmermann
11726f2bb119SThomas Zimmermann err_drm_connector_cleanup:
11736f2bb119SThomas Zimmermann drm_connector_cleanup(&qxl_output->base);
11746f2bb119SThomas Zimmermann kfree(qxl_output);
11756f2bb119SThomas Zimmermann return ret;
1176f64122c1SDave Airlie }
1177f64122c1SDave Airlie
1178f64122c1SDave Airlie static struct drm_framebuffer *
qxl_user_framebuffer_create(struct drm_device * dev,struct drm_file * file_priv,const struct drm_mode_fb_cmd2 * mode_cmd)1179f64122c1SDave Airlie qxl_user_framebuffer_create(struct drm_device *dev,
1180f64122c1SDave Airlie struct drm_file *file_priv,
11811eb83451SVille Syrjälä const struct drm_mode_fb_cmd2 *mode_cmd)
1182f64122c1SDave Airlie {
1183bf8744e4SPeter Wu return drm_gem_fb_create_with_funcs(dev, file_priv, mode_cmd,
1184bf8744e4SPeter Wu &qxl_fb_funcs);
1185f64122c1SDave Airlie }
1186f64122c1SDave Airlie
1187f64122c1SDave Airlie static const struct drm_mode_config_funcs qxl_mode_funcs = {
1188f64122c1SDave Airlie .fb_create = qxl_user_framebuffer_create,
1189472e6d46SGabriel Krisman Bertazi .atomic_check = drm_atomic_helper_check,
1190472e6d46SGabriel Krisman Bertazi .atomic_commit = drm_atomic_helper_commit,
1191f64122c1SDave Airlie };
1192f64122c1SDave Airlie
qxl_create_monitors_object(struct qxl_device * qdev)11932bd6ce84SDave Airlie int qxl_create_monitors_object(struct qxl_device *qdev)
1194f64122c1SDave Airlie {
1195f64122c1SDave Airlie int ret;
1196f64122c1SDave Airlie struct drm_gem_object *gobj;
11977938f421SLucas De Marchi struct iosys_map map;
1198f64122c1SDave Airlie int monitors_config_size = sizeof(struct qxl_monitors_config) +
119921c76bd1SGerd Hoffmann qxl_num_crtc * sizeof(struct qxl_head);
1200f64122c1SDave Airlie
1201f64122c1SDave Airlie ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1202f64122c1SDave Airlie QXL_GEM_DOMAIN_VRAM,
1203f64122c1SDave Airlie false, false, NULL, &gobj);
1204f64122c1SDave Airlie if (ret) {
1205f64122c1SDave Airlie DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1206f64122c1SDave Airlie return -ENOMEM;
1207f64122c1SDave Airlie }
1208f64122c1SDave Airlie qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
12092bd6ce84SDave Airlie
1210cb981987SGerd Hoffmann ret = qxl_bo_vmap(qdev->monitors_config_bo, &map);
12112bd6ce84SDave Airlie if (ret)
12122bd6ce84SDave Airlie return ret;
12132bd6ce84SDave Airlie
1214f64122c1SDave Airlie qdev->monitors_config = qdev->monitors_config_bo->kptr;
1215f64122c1SDave Airlie qdev->ram_header->monitors_config =
1216f64122c1SDave Airlie qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1217f64122c1SDave Airlie
1218f64122c1SDave Airlie memset(qdev->monitors_config, 0, monitors_config_size);
121921c76bd1SGerd Hoffmann qdev->dumb_heads = kcalloc(qxl_num_crtc, sizeof(qdev->dumb_heads[0]),
122021c76bd1SGerd Hoffmann GFP_KERNEL);
122190adda2cSGerd Hoffmann if (!qdev->dumb_heads) {
122290adda2cSGerd Hoffmann qxl_destroy_monitors_object(qdev);
122390adda2cSGerd Hoffmann return -ENOMEM;
122490adda2cSGerd Hoffmann }
12252bd6ce84SDave Airlie return 0;
12262bd6ce84SDave Airlie }
12272bd6ce84SDave Airlie
qxl_destroy_monitors_object(struct qxl_device * qdev)12282bd6ce84SDave Airlie int qxl_destroy_monitors_object(struct qxl_device *qdev)
12292bd6ce84SDave Airlie {
12302bd6ce84SDave Airlie int ret;
12312bd6ce84SDave Airlie
123226fe1f4cSGerd Hoffmann if (!qdev->monitors_config_bo)
123326fe1f4cSGerd Hoffmann return 0;
123426fe1f4cSGerd Hoffmann
123501834d42SZongmin Zhou kfree(qdev->dumb_heads);
123601834d42SZongmin Zhou qdev->dumb_heads = NULL;
123701834d42SZongmin Zhou
12382bd6ce84SDave Airlie qdev->monitors_config = NULL;
12392bd6ce84SDave Airlie qdev->ram_header->monitors_config = 0;
12402bd6ce84SDave Airlie
1241cb981987SGerd Hoffmann ret = qxl_bo_vunmap(qdev->monitors_config_bo);
12422bd6ce84SDave Airlie if (ret)
12432bd6ce84SDave Airlie return ret;
12442bd6ce84SDave Airlie
12452bd6ce84SDave Airlie qxl_bo_unref(&qdev->monitors_config_bo);
12462bd6ce84SDave Airlie return 0;
12472bd6ce84SDave Airlie }
12482bd6ce84SDave Airlie
qxl_modeset_init(struct qxl_device * qdev)12492bd6ce84SDave Airlie int qxl_modeset_init(struct qxl_device *qdev)
12502bd6ce84SDave Airlie {
12512bd6ce84SDave Airlie int i;
12522bd6ce84SDave Airlie int ret;
12532bd6ce84SDave Airlie
1254f8752f14SGerd Hoffmann ret = drmm_mode_config_init(&qdev->ddev);
1255f8752f14SGerd Hoffmann if (ret)
1256f8752f14SGerd Hoffmann return ret;
12572bd6ce84SDave Airlie
12582bd6ce84SDave Airlie ret = qxl_create_monitors_object(qdev);
12592bd6ce84SDave Airlie if (ret)
12602bd6ce84SDave Airlie return ret;
1261f64122c1SDave Airlie
1262cbdded7fSGabriel Krisman Bertazi qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs;
1263f64122c1SDave Airlie
1264f64122c1SDave Airlie /* modes will be validated against the framebuffer size */
12651277eed5SGabriel Krisman Bertazi qdev->ddev.mode_config.min_width = 0;
12661277eed5SGabriel Krisman Bertazi qdev->ddev.mode_config.min_height = 0;
1267cbdded7fSGabriel Krisman Bertazi qdev->ddev.mode_config.max_width = 8192;
1268cbdded7fSGabriel Krisman Bertazi qdev->ddev.mode_config.max_height = 8192;
1269f64122c1SDave Airlie
1270cbdded7fSGabriel Krisman Bertazi drm_mode_create_suggested_offset_properties(&qdev->ddev);
12714695b039SDave Airlie qxl_mode_create_hotplug_mode_update_property(qdev);
12724695b039SDave Airlie
127307f8d9bdSDave Airlie for (i = 0 ; i < qxl_num_crtc; ++i) {
1274cbdded7fSGabriel Krisman Bertazi qdev_crtc_init(&qdev->ddev, i);
1275cbdded7fSGabriel Krisman Bertazi qdev_output_init(&qdev->ddev, i);
1276f64122c1SDave Airlie }
1277f64122c1SDave Airlie
1278c50fad8fSGerd Hoffmann qxl_display_read_client_monitors_config(qdev);
1279f64122c1SDave Airlie
12809ade8b98SGabriel Krisman Bertazi drm_mode_config_reset(&qdev->ddev);
1281f64122c1SDave Airlie return 0;
1282f64122c1SDave Airlie }
1283f64122c1SDave Airlie
qxl_modeset_fini(struct qxl_device * qdev)1284f64122c1SDave Airlie void qxl_modeset_fini(struct qxl_device *qdev)
1285f64122c1SDave Airlie {
12864ca77c51SGerd Hoffmann if (qdev->dumb_shadow_bo) {
128776aefa4dSGerd Hoffmann qxl_bo_unpin(qdev->dumb_shadow_bo);
12884ca77c51SGerd Hoffmann drm_gem_object_put(&qdev->dumb_shadow_bo->tbo.base);
12894ca77c51SGerd Hoffmann qdev->dumb_shadow_bo = NULL;
12904ca77c51SGerd Hoffmann }
12912bd6ce84SDave Airlie qxl_destroy_monitors_object(qdev);
1292f64122c1SDave Airlie }
1293