Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright 2013 Red Hat Inc.
0003  *
0004  * Permission is hereby granted, free of charge, to any person obtaining a
0005  * copy of this software and associated documentation files (the "Software"),
0006  * to deal in the Software without restriction, including without limitation
0007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0008  * and/or sell copies of the Software, and to permit persons to whom the
0009  * Software is furnished to do so, subject to the following conditions:
0010  *
0011  * The above copyright notice and this permission notice shall be included in
0012  * all copies or substantial portions of the Software.
0013  *
0014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0017  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
0018  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0020  * OTHER DEALINGS IN THE SOFTWARE.
0021  *
0022  * Authors: Dave Airlie
0023  *          Alon Levy
0024  */
0025 
0026 #include <linux/crc32.h>
0027 #include <linux/delay.h>
0028 #include <linux/iosys-map.h>
0029 
0030 #include <drm/drm_drv.h>
0031 #include <drm/drm_atomic.h>
0032 #include <drm/drm_atomic_helper.h>
0033 #include <drm/drm_edid.h>
0034 #include <drm/drm_framebuffer.h>
0035 #include <drm/drm_gem_framebuffer_helper.h>
0036 #include <drm/drm_plane_helper.h>
0037 #include <drm/drm_probe_helper.h>
0038 #include <drm/drm_simple_kms_helper.h>
0039 #include <drm/drm_gem_atomic_helper.h>
0040 
0041 #include "qxl_drv.h"
0042 #include "qxl_object.h"
0043 
0044 static bool qxl_head_enabled(struct qxl_head *head)
0045 {
0046     return head->width && head->height;
0047 }
0048 
0049 static int qxl_alloc_client_monitors_config(struct qxl_device *qdev,
0050         unsigned int count)
0051 {
0052     if (qdev->client_monitors_config &&
0053         count > qdev->client_monitors_config->count) {
0054         kfree(qdev->client_monitors_config);
0055         qdev->client_monitors_config = NULL;
0056     }
0057     if (!qdev->client_monitors_config) {
0058         qdev->client_monitors_config = kzalloc(
0059                 struct_size(qdev->client_monitors_config,
0060                 heads, count), GFP_KERNEL);
0061         if (!qdev->client_monitors_config)
0062             return -ENOMEM;
0063     }
0064     qdev->client_monitors_config->count = count;
0065     return 0;
0066 }
0067 
0068 enum {
0069     MONITORS_CONFIG_MODIFIED,
0070     MONITORS_CONFIG_UNCHANGED,
0071     MONITORS_CONFIG_BAD_CRC,
0072     MONITORS_CONFIG_ERROR,
0073 };
0074 
0075 static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
0076 {
0077     int i;
0078     int num_monitors;
0079     uint32_t crc;
0080     int status = MONITORS_CONFIG_UNCHANGED;
0081 
0082     num_monitors = qdev->rom->client_monitors_config.count;
0083     crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
0084           sizeof(qdev->rom->client_monitors_config));
0085     if (crc != qdev->rom->client_monitors_config_crc)
0086         return MONITORS_CONFIG_BAD_CRC;
0087     if (!num_monitors) {
0088         DRM_DEBUG_KMS("no client monitors configured\n");
0089         return status;
0090     }
0091     if (num_monitors > qxl_num_crtc) {
0092         DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
0093                   qxl_num_crtc, num_monitors);
0094         num_monitors = qxl_num_crtc;
0095     } else {
0096         num_monitors = qdev->rom->client_monitors_config.count;
0097     }
0098     if (qdev->client_monitors_config
0099           && (num_monitors != qdev->client_monitors_config->count)) {
0100         status = MONITORS_CONFIG_MODIFIED;
0101     }
0102     if (qxl_alloc_client_monitors_config(qdev, num_monitors)) {
0103         status = MONITORS_CONFIG_ERROR;
0104         return status;
0105     }
0106     /* we copy max from the client but it isn't used */
0107     qdev->client_monitors_config->max_allowed = qxl_num_crtc;
0108     for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
0109         struct qxl_urect *c_rect =
0110             &qdev->rom->client_monitors_config.heads[i];
0111         struct qxl_head *client_head =
0112             &qdev->client_monitors_config->heads[i];
0113         if (client_head->x != c_rect->left) {
0114             client_head->x = c_rect->left;
0115             status = MONITORS_CONFIG_MODIFIED;
0116         }
0117         if (client_head->y != c_rect->top) {
0118             client_head->y = c_rect->top;
0119             status = MONITORS_CONFIG_MODIFIED;
0120         }
0121         if (client_head->width != c_rect->right - c_rect->left) {
0122             client_head->width = c_rect->right - c_rect->left;
0123             status = MONITORS_CONFIG_MODIFIED;
0124         }
0125         if (client_head->height != c_rect->bottom - c_rect->top) {
0126             client_head->height = c_rect->bottom - c_rect->top;
0127             status = MONITORS_CONFIG_MODIFIED;
0128         }
0129         if (client_head->surface_id != 0) {
0130             client_head->surface_id = 0;
0131             status = MONITORS_CONFIG_MODIFIED;
0132         }
0133         if (client_head->id != i) {
0134             client_head->id = i;
0135             status = MONITORS_CONFIG_MODIFIED;
0136         }
0137         if (client_head->flags != 0) {
0138             client_head->flags = 0;
0139             status = MONITORS_CONFIG_MODIFIED;
0140         }
0141         DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
0142               client_head->x, client_head->y);
0143     }
0144 
0145     return status;
0146 }
0147 
0148 static void qxl_update_offset_props(struct qxl_device *qdev)
0149 {
0150     struct drm_device *dev = &qdev->ddev;
0151     struct drm_connector *connector;
0152     struct qxl_output *output;
0153     struct qxl_head *head;
0154 
0155     list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
0156         output = drm_connector_to_qxl_output(connector);
0157 
0158         head = &qdev->client_monitors_config->heads[output->index];
0159 
0160         drm_object_property_set_value(&connector->base,
0161             dev->mode_config.suggested_x_property, head->x);
0162         drm_object_property_set_value(&connector->base,
0163             dev->mode_config.suggested_y_property, head->y);
0164     }
0165 }
0166 
0167 void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
0168 {
0169     struct drm_device *dev = &qdev->ddev;
0170     struct drm_modeset_acquire_ctx ctx;
0171     int status, retries, ret;
0172 
0173     for (retries = 0; retries < 10; retries++) {
0174         status = qxl_display_copy_rom_client_monitors_config(qdev);
0175         if (status != MONITORS_CONFIG_BAD_CRC)
0176             break;
0177         udelay(5);
0178     }
0179     if (status == MONITORS_CONFIG_ERROR) {
0180         DRM_DEBUG_KMS("ignoring client monitors config: error");
0181         return;
0182     }
0183     if (status == MONITORS_CONFIG_BAD_CRC) {
0184         DRM_DEBUG_KMS("ignoring client monitors config: bad crc");
0185         return;
0186     }
0187     if (status == MONITORS_CONFIG_UNCHANGED) {
0188         DRM_DEBUG_KMS("ignoring client monitors config: unchanged");
0189         return;
0190     }
0191 
0192     DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE, ret);
0193     qxl_update_offset_props(qdev);
0194     DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
0195     if (!drm_helper_hpd_irq_event(dev)) {
0196         /* notify that the monitor configuration changed, to
0197            adjust at the arbitrary resolution */
0198         drm_kms_helper_hotplug_event(dev);
0199     }
0200 }
0201 
0202 static int qxl_check_mode(struct qxl_device *qdev,
0203               unsigned int width,
0204               unsigned int height)
0205 {
0206     unsigned int stride;
0207     unsigned int size;
0208 
0209     if (check_mul_overflow(width, 4u, &stride))
0210         return -EINVAL;
0211     if (check_mul_overflow(stride, height, &size))
0212         return -EINVAL;
0213     if (size > qdev->vram_size)
0214         return -ENOMEM;
0215     return 0;
0216 }
0217 
0218 static int qxl_check_framebuffer(struct qxl_device *qdev,
0219                  struct qxl_bo *bo)
0220 {
0221     return qxl_check_mode(qdev, bo->surf.width, bo->surf.height);
0222 }
0223 
0224 static int qxl_add_mode(struct drm_connector *connector,
0225             unsigned int width,
0226             unsigned int height,
0227             bool preferred)
0228 {
0229     struct drm_device *dev = connector->dev;
0230     struct qxl_device *qdev = to_qxl(dev);
0231     struct drm_display_mode *mode = NULL;
0232     int rc;
0233 
0234     rc = qxl_check_mode(qdev, width, height);
0235     if (rc != 0)
0236         return 0;
0237 
0238     mode = drm_cvt_mode(dev, width, height, 60, false, false, false);
0239     if (preferred)
0240         mode->type |= DRM_MODE_TYPE_PREFERRED;
0241     mode->hdisplay = width;
0242     mode->vdisplay = height;
0243     drm_mode_set_name(mode);
0244     drm_mode_probed_add(connector, mode);
0245     return 1;
0246 }
0247 
0248 static int qxl_add_monitors_config_modes(struct drm_connector *connector)
0249 {
0250     struct drm_device *dev = connector->dev;
0251     struct qxl_device *qdev = to_qxl(dev);
0252     struct qxl_output *output = drm_connector_to_qxl_output(connector);
0253     int h = output->index;
0254     struct qxl_head *head;
0255 
0256     if (!qdev->monitors_config)
0257         return 0;
0258     if (h >= qxl_num_crtc)
0259         return 0;
0260     if (!qdev->client_monitors_config)
0261         return 0;
0262     if (h >= qdev->client_monitors_config->count)
0263         return 0;
0264 
0265     head = &qdev->client_monitors_config->heads[h];
0266     DRM_DEBUG_KMS("head %d is %dx%d\n", h, head->width, head->height);
0267 
0268     return qxl_add_mode(connector, head->width, head->height, true);
0269 }
0270 
0271 static struct mode_size {
0272     int w;
0273     int h;
0274 } extra_modes[] = {
0275     { 720,  480},
0276     {1152,  768},
0277     {1280,  854},
0278 };
0279 
0280 static int qxl_add_extra_modes(struct drm_connector *connector)
0281 {
0282     int i, ret = 0;
0283 
0284     for (i = 0; i < ARRAY_SIZE(extra_modes); i++)
0285         ret += qxl_add_mode(connector,
0286                     extra_modes[i].w,
0287                     extra_modes[i].h,
0288                     false);
0289     return ret;
0290 }
0291 
0292 static void qxl_send_monitors_config(struct qxl_device *qdev)
0293 {
0294     int i;
0295 
0296     BUG_ON(!qdev->ram_header->monitors_config);
0297 
0298     if (qdev->monitors_config->count == 0)
0299         return;
0300 
0301     for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
0302         struct qxl_head *head = &qdev->monitors_config->heads[i];
0303 
0304         if (head->y > 8192 || head->x > 8192 ||
0305             head->width > 8192 || head->height > 8192) {
0306             DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
0307                   i, head->width, head->height,
0308                   head->x, head->y);
0309             return;
0310         }
0311     }
0312     qxl_io_monitors_config(qdev);
0313 }
0314 
0315 static void qxl_crtc_update_monitors_config(struct drm_crtc *crtc,
0316                         const char *reason)
0317 {
0318     struct drm_device *dev = crtc->dev;
0319     struct qxl_device *qdev = to_qxl(dev);
0320     struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
0321     struct qxl_head head;
0322     int oldcount, i = qcrtc->index;
0323 
0324     if (!qdev->primary_bo) {
0325         DRM_DEBUG_KMS("no primary surface, skip (%s)\n", reason);
0326         return;
0327     }
0328 
0329     if (!qdev->monitors_config || qxl_num_crtc <= i)
0330         return;
0331 
0332     head.id = i;
0333     head.flags = 0;
0334     head.surface_id = 0;
0335     oldcount = qdev->monitors_config->count;
0336     if (crtc->state->active) {
0337         struct drm_display_mode *mode = &crtc->mode;
0338 
0339         head.width = mode->hdisplay;
0340         head.height = mode->vdisplay;
0341         head.x = crtc->x;
0342         head.y = crtc->y;
0343         if (qdev->monitors_config->count < i + 1)
0344             qdev->monitors_config->count = i + 1;
0345         if (qdev->primary_bo == qdev->dumb_shadow_bo)
0346             head.x += qdev->dumb_heads[i].x;
0347     } else if (i > 0) {
0348         head.width = 0;
0349         head.height = 0;
0350         head.x = 0;
0351         head.y = 0;
0352         if (qdev->monitors_config->count == i + 1)
0353             qdev->monitors_config->count = i;
0354     } else {
0355         DRM_DEBUG_KMS("inactive head 0, skip (%s)\n", reason);
0356         return;
0357     }
0358 
0359     if (head.width  == qdev->monitors_config->heads[i].width  &&
0360         head.height == qdev->monitors_config->heads[i].height &&
0361         head.x      == qdev->monitors_config->heads[i].x      &&
0362         head.y      == qdev->monitors_config->heads[i].y      &&
0363         oldcount    == qdev->monitors_config->count)
0364         return;
0365 
0366     DRM_DEBUG_KMS("head %d, %dx%d, at +%d+%d, %s (%s)\n",
0367               i, head.width, head.height, head.x, head.y,
0368               crtc->state->active ? "on" : "off", reason);
0369     if (oldcount != qdev->monitors_config->count)
0370         DRM_DEBUG_KMS("active heads %d -> %d (%d total)\n",
0371                   oldcount, qdev->monitors_config->count,
0372                   qxl_num_crtc);
0373 
0374     qdev->monitors_config->heads[i] = head;
0375     qdev->monitors_config->max_allowed = qxl_num_crtc;
0376     qxl_send_monitors_config(qdev);
0377 }
0378 
0379 static void qxl_crtc_atomic_flush(struct drm_crtc *crtc,
0380                   struct drm_atomic_state *state)
0381 {
0382     qxl_crtc_update_monitors_config(crtc, "flush");
0383 }
0384 
0385 static void qxl_crtc_destroy(struct drm_crtc *crtc)
0386 {
0387     struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
0388 
0389     qxl_bo_unref(&qxl_crtc->cursor_bo);
0390     drm_crtc_cleanup(crtc);
0391     kfree(qxl_crtc);
0392 }
0393 
0394 static const struct drm_crtc_funcs qxl_crtc_funcs = {
0395     .set_config = drm_atomic_helper_set_config,
0396     .destroy = qxl_crtc_destroy,
0397     .page_flip = drm_atomic_helper_page_flip,
0398     .reset = drm_atomic_helper_crtc_reset,
0399     .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
0400     .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
0401 };
0402 
0403 static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
0404                      struct drm_file *file_priv,
0405                      unsigned int flags, unsigned int color,
0406                      struct drm_clip_rect *clips,
0407                      unsigned int num_clips)
0408 {
0409     /* TODO: vmwgfx where this was cribbed from had locking. Why? */
0410     struct qxl_device *qdev = to_qxl(fb->dev);
0411     struct drm_clip_rect norect;
0412     struct qxl_bo *qobj;
0413     struct drm_modeset_acquire_ctx ctx;
0414     bool is_primary;
0415     int inc = 1, ret;
0416 
0417     DRM_MODESET_LOCK_ALL_BEGIN(fb->dev, ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE, ret);
0418 
0419     qobj = gem_to_qxl_bo(fb->obj[0]);
0420     /* if we aren't primary surface ignore this */
0421     is_primary = qobj->shadow ? qobj->shadow->is_primary : qobj->is_primary;
0422     if (!is_primary)
0423         goto out_lock_end;
0424 
0425     if (!num_clips) {
0426         num_clips = 1;
0427         clips = &norect;
0428         norect.x1 = norect.y1 = 0;
0429         norect.x2 = fb->width;
0430         norect.y2 = fb->height;
0431     } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
0432         num_clips /= 2;
0433         inc = 2; /* skip source rects */
0434     }
0435 
0436     qxl_draw_dirty_fb(qdev, fb, qobj, flags, color,
0437               clips, num_clips, inc, 0);
0438 
0439 out_lock_end:
0440     DRM_MODESET_LOCK_ALL_END(fb->dev, ctx, ret);
0441 
0442     return 0;
0443 }
0444 
0445 static const struct drm_framebuffer_funcs qxl_fb_funcs = {
0446     .destroy = drm_gem_fb_destroy,
0447     .dirty = qxl_framebuffer_surface_dirty,
0448     .create_handle = drm_gem_fb_create_handle,
0449 };
0450 
0451 static void qxl_crtc_atomic_enable(struct drm_crtc *crtc,
0452                    struct drm_atomic_state *state)
0453 {
0454     qxl_crtc_update_monitors_config(crtc, "enable");
0455 }
0456 
0457 static void qxl_crtc_atomic_disable(struct drm_crtc *crtc,
0458                     struct drm_atomic_state *state)
0459 {
0460     qxl_crtc_update_monitors_config(crtc, "disable");
0461 }
0462 
0463 static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
0464     .atomic_flush = qxl_crtc_atomic_flush,
0465     .atomic_enable = qxl_crtc_atomic_enable,
0466     .atomic_disable = qxl_crtc_atomic_disable,
0467 };
0468 
0469 static int qxl_primary_atomic_check(struct drm_plane *plane,
0470                     struct drm_atomic_state *state)
0471 {
0472     struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
0473                                          plane);
0474     struct qxl_device *qdev = to_qxl(plane->dev);
0475     struct qxl_bo *bo;
0476 
0477     if (!new_plane_state->crtc || !new_plane_state->fb)
0478         return 0;
0479 
0480     bo = gem_to_qxl_bo(new_plane_state->fb->obj[0]);
0481 
0482     return qxl_check_framebuffer(qdev, bo);
0483 }
0484 
0485 static int qxl_primary_apply_cursor(struct qxl_device *qdev,
0486                     struct drm_plane_state *plane_state)
0487 {
0488     struct drm_framebuffer *fb = plane_state->fb;
0489     struct qxl_crtc *qcrtc = to_qxl_crtc(plane_state->crtc);
0490     struct qxl_cursor_cmd *cmd;
0491     struct qxl_release *release;
0492     int ret = 0;
0493 
0494     if (!qcrtc->cursor_bo)
0495         return 0;
0496 
0497     ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
0498                      QXL_RELEASE_CURSOR_CMD,
0499                      &release, NULL);
0500     if (ret)
0501         return ret;
0502 
0503     ret = qxl_release_list_add(release, qcrtc->cursor_bo);
0504     if (ret)
0505         goto out_free_release;
0506 
0507     ret = qxl_release_reserve_list(release, false);
0508     if (ret)
0509         goto out_free_release;
0510 
0511     cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
0512     cmd->type = QXL_CURSOR_SET;
0513     cmd->u.set.position.x = plane_state->crtc_x + fb->hot_x;
0514     cmd->u.set.position.y = plane_state->crtc_y + fb->hot_y;
0515 
0516     cmd->u.set.shape = qxl_bo_physical_address(qdev, qcrtc->cursor_bo, 0);
0517 
0518     cmd->u.set.visible = 1;
0519     qxl_release_unmap(qdev, release, &cmd->release_info);
0520 
0521     qxl_release_fence_buffer_objects(release);
0522     qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
0523 
0524     return ret;
0525 
0526 out_free_release:
0527     qxl_release_free(qdev, release);
0528     return ret;
0529 }
0530 
0531 static int qxl_primary_move_cursor(struct qxl_device *qdev,
0532                    struct drm_plane_state *plane_state)
0533 {
0534     struct drm_framebuffer *fb = plane_state->fb;
0535     struct qxl_crtc *qcrtc = to_qxl_crtc(plane_state->crtc);
0536     struct qxl_cursor_cmd *cmd;
0537     struct qxl_release *release;
0538     int ret = 0;
0539 
0540     if (!qcrtc->cursor_bo)
0541         return 0;
0542 
0543     ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
0544                      QXL_RELEASE_CURSOR_CMD,
0545                      &release, NULL);
0546     if (ret)
0547         return ret;
0548 
0549     ret = qxl_release_reserve_list(release, true);
0550     if (ret) {
0551         qxl_release_free(qdev, release);
0552         return ret;
0553     }
0554 
0555     cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
0556     cmd->type = QXL_CURSOR_MOVE;
0557     cmd->u.position.x = plane_state->crtc_x + fb->hot_x;
0558     cmd->u.position.y = plane_state->crtc_y + fb->hot_y;
0559     qxl_release_unmap(qdev, release, &cmd->release_info);
0560 
0561     qxl_release_fence_buffer_objects(release);
0562     qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
0563     return ret;
0564 }
0565 
0566 static struct qxl_bo *qxl_create_cursor(struct qxl_device *qdev,
0567                     struct qxl_bo *user_bo,
0568                     int hot_x, int hot_y)
0569 {
0570     static const u32 size = 64 * 64 * 4;
0571     struct qxl_bo *cursor_bo;
0572     struct iosys_map cursor_map;
0573     struct iosys_map user_map;
0574     struct qxl_cursor cursor;
0575     int ret;
0576 
0577     if (!user_bo)
0578         return NULL;
0579 
0580     ret = qxl_bo_create(qdev, sizeof(struct qxl_cursor) + size,
0581                 false, true, QXL_GEM_DOMAIN_VRAM, 1,
0582                 NULL, &cursor_bo);
0583     if (ret)
0584         goto err;
0585 
0586     ret = qxl_bo_vmap(cursor_bo, &cursor_map);
0587     if (ret)
0588         goto err_unref;
0589 
0590     ret = qxl_bo_vmap(user_bo, &user_map);
0591     if (ret)
0592         goto err_unmap;
0593 
0594     cursor.header.unique = 0;
0595     cursor.header.type = SPICE_CURSOR_TYPE_ALPHA;
0596     cursor.header.width = 64;
0597     cursor.header.height = 64;
0598     cursor.header.hot_spot_x = hot_x;
0599     cursor.header.hot_spot_y = hot_y;
0600     cursor.data_size = size;
0601     cursor.chunk.next_chunk = 0;
0602     cursor.chunk.prev_chunk = 0;
0603     cursor.chunk.data_size = size;
0604     if (cursor_map.is_iomem) {
0605         memcpy_toio(cursor_map.vaddr_iomem,
0606                 &cursor, sizeof(cursor));
0607         memcpy_toio(cursor_map.vaddr_iomem + sizeof(cursor),
0608                 user_map.vaddr, size);
0609     } else {
0610         memcpy(cursor_map.vaddr,
0611                &cursor, sizeof(cursor));
0612         memcpy(cursor_map.vaddr + sizeof(cursor),
0613                user_map.vaddr, size);
0614     }
0615 
0616     qxl_bo_vunmap(user_bo);
0617     qxl_bo_vunmap(cursor_bo);
0618     return cursor_bo;
0619 
0620 err_unmap:
0621     qxl_bo_vunmap(cursor_bo);
0622 err_unref:
0623     qxl_bo_unpin(cursor_bo);
0624     qxl_bo_unref(&cursor_bo);
0625 err:
0626     return NULL;
0627 }
0628 
0629 static void qxl_free_cursor(struct qxl_bo *cursor_bo)
0630 {
0631     if (!cursor_bo)
0632         return;
0633 
0634     qxl_bo_unpin(cursor_bo);
0635     qxl_bo_unref(&cursor_bo);
0636 }
0637 
0638 static void qxl_primary_atomic_update(struct drm_plane *plane,
0639                       struct drm_atomic_state *state)
0640 {
0641     struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
0642                                        plane);
0643     struct qxl_device *qdev = to_qxl(plane->dev);
0644     struct qxl_bo *bo = gem_to_qxl_bo(new_state->fb->obj[0]);
0645     struct qxl_bo *primary;
0646     struct drm_clip_rect norect = {
0647         .x1 = 0,
0648         .y1 = 0,
0649         .x2 = new_state->fb->width,
0650         .y2 = new_state->fb->height
0651     };
0652     uint32_t dumb_shadow_offset = 0;
0653 
0654     primary = bo->shadow ? bo->shadow : bo;
0655 
0656     if (!primary->is_primary) {
0657         if (qdev->primary_bo)
0658             qxl_io_destroy_primary(qdev);
0659         qxl_io_create_primary(qdev, primary);
0660         qxl_primary_apply_cursor(qdev, plane->state);
0661     }
0662 
0663     if (bo->is_dumb)
0664         dumb_shadow_offset =
0665             qdev->dumb_heads[new_state->crtc->index].x;
0666 
0667     qxl_draw_dirty_fb(qdev, new_state->fb, bo, 0, 0, &norect, 1, 1,
0668               dumb_shadow_offset);
0669 }
0670 
0671 static void qxl_primary_atomic_disable(struct drm_plane *plane,
0672                        struct drm_atomic_state *state)
0673 {
0674     struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
0675                                        plane);
0676     struct qxl_device *qdev = to_qxl(plane->dev);
0677 
0678     if (old_state->fb) {
0679         struct qxl_bo *bo = gem_to_qxl_bo(old_state->fb->obj[0]);
0680 
0681         if (bo->shadow)
0682             bo = bo->shadow;
0683         if (bo->is_primary)
0684             qxl_io_destroy_primary(qdev);
0685     }
0686 }
0687 
0688 static void qxl_cursor_atomic_update(struct drm_plane *plane,
0689                      struct drm_atomic_state *state)
0690 {
0691     struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
0692                                        plane);
0693     struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
0694                                        plane);
0695     struct qxl_device *qdev = to_qxl(plane->dev);
0696     struct drm_framebuffer *fb = new_state->fb;
0697 
0698     if (fb != old_state->fb) {
0699         qxl_primary_apply_cursor(qdev, new_state);
0700     } else {
0701         qxl_primary_move_cursor(qdev, new_state);
0702     }
0703 }
0704 
0705 static void qxl_cursor_atomic_disable(struct drm_plane *plane,
0706                       struct drm_atomic_state *state)
0707 {
0708     struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
0709                                        plane);
0710     struct qxl_device *qdev = to_qxl(plane->dev);
0711     struct qxl_crtc *qcrtc;
0712     struct qxl_release *release;
0713     struct qxl_cursor_cmd *cmd;
0714     int ret;
0715 
0716     ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
0717                      QXL_RELEASE_CURSOR_CMD,
0718                      &release, NULL);
0719     if (ret)
0720         return;
0721 
0722     ret = qxl_release_reserve_list(release, true);
0723     if (ret) {
0724         qxl_release_free(qdev, release);
0725         return;
0726     }
0727 
0728     cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
0729     cmd->type = QXL_CURSOR_HIDE;
0730     qxl_release_unmap(qdev, release, &cmd->release_info);
0731 
0732     qxl_release_fence_buffer_objects(release);
0733     qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
0734 
0735     qcrtc = to_qxl_crtc(old_state->crtc);
0736     qxl_free_cursor(qcrtc->cursor_bo);
0737     qcrtc->cursor_bo = NULL;
0738 }
0739 
0740 static void qxl_update_dumb_head(struct qxl_device *qdev,
0741                  int index, struct qxl_bo *bo)
0742 {
0743     uint32_t width, height;
0744 
0745     if (index >= qdev->monitors_config->max_allowed)
0746         return;
0747 
0748     if (bo && bo->is_dumb) {
0749         width = bo->surf.width;
0750         height = bo->surf.height;
0751     } else {
0752         width = 0;
0753         height = 0;
0754     }
0755 
0756     if (qdev->dumb_heads[index].width == width &&
0757         qdev->dumb_heads[index].height == height)
0758         return;
0759 
0760     DRM_DEBUG("#%d: %dx%d -> %dx%d\n", index,
0761           qdev->dumb_heads[index].width,
0762           qdev->dumb_heads[index].height,
0763           width, height);
0764     qdev->dumb_heads[index].width = width;
0765     qdev->dumb_heads[index].height = height;
0766 }
0767 
0768 static void qxl_calc_dumb_shadow(struct qxl_device *qdev,
0769                  struct qxl_surface *surf)
0770 {
0771     struct qxl_head *head;
0772     int i;
0773 
0774     memset(surf, 0, sizeof(*surf));
0775     for (i = 0; i < qdev->monitors_config->max_allowed; i++) {
0776         head = qdev->dumb_heads + i;
0777         head->x = surf->width;
0778         surf->width += head->width;
0779         if (surf->height < head->height)
0780             surf->height = head->height;
0781     }
0782     if (surf->width < 64)
0783         surf->width = 64;
0784     if (surf->height < 64)
0785         surf->height = 64;
0786     surf->format = SPICE_SURFACE_FMT_32_xRGB;
0787     surf->stride = surf->width * 4;
0788 
0789     if (!qdev->dumb_shadow_bo ||
0790         qdev->dumb_shadow_bo->surf.width != surf->width ||
0791         qdev->dumb_shadow_bo->surf.height != surf->height)
0792         DRM_DEBUG("%dx%d\n", surf->width, surf->height);
0793 }
0794 
0795 static void qxl_prepare_shadow(struct qxl_device *qdev, struct qxl_bo *user_bo,
0796                    int crtc_index)
0797 {
0798     struct qxl_surface surf;
0799 
0800     qxl_update_dumb_head(qdev, crtc_index,
0801                  user_bo);
0802     qxl_calc_dumb_shadow(qdev, &surf);
0803     if (!qdev->dumb_shadow_bo ||
0804         qdev->dumb_shadow_bo->surf.width  != surf.width ||
0805         qdev->dumb_shadow_bo->surf.height != surf.height) {
0806         if (qdev->dumb_shadow_bo) {
0807             qxl_bo_unpin(qdev->dumb_shadow_bo);
0808             drm_gem_object_put
0809                 (&qdev->dumb_shadow_bo->tbo.base);
0810             qdev->dumb_shadow_bo = NULL;
0811         }
0812         qxl_bo_create(qdev, surf.height * surf.stride,
0813                   true, true, QXL_GEM_DOMAIN_SURFACE, 0,
0814                   &surf, &qdev->dumb_shadow_bo);
0815     }
0816     if (user_bo->shadow != qdev->dumb_shadow_bo) {
0817         if (user_bo->shadow) {
0818             qxl_bo_unpin(user_bo->shadow);
0819             drm_gem_object_put
0820                 (&user_bo->shadow->tbo.base);
0821             user_bo->shadow = NULL;
0822         }
0823         drm_gem_object_get(&qdev->dumb_shadow_bo->tbo.base);
0824         user_bo->shadow = qdev->dumb_shadow_bo;
0825         qxl_bo_pin(user_bo->shadow);
0826     }
0827 }
0828 
0829 static int qxl_plane_prepare_fb(struct drm_plane *plane,
0830                 struct drm_plane_state *new_state)
0831 {
0832     struct qxl_device *qdev = to_qxl(plane->dev);
0833     struct drm_gem_object *obj;
0834     struct qxl_bo *user_bo;
0835     int ret;
0836 
0837     if (!new_state->fb)
0838         return 0;
0839 
0840     obj = new_state->fb->obj[0];
0841     user_bo = gem_to_qxl_bo(obj);
0842 
0843     if (plane->type == DRM_PLANE_TYPE_PRIMARY &&
0844         user_bo->is_dumb) {
0845         qxl_prepare_shadow(qdev, user_bo, new_state->crtc->index);
0846     }
0847 
0848     if (plane->type == DRM_PLANE_TYPE_CURSOR &&
0849         plane->state->fb != new_state->fb) {
0850         struct qxl_crtc *qcrtc = to_qxl_crtc(new_state->crtc);
0851         struct qxl_bo *old_cursor_bo = qcrtc->cursor_bo;
0852 
0853         qcrtc->cursor_bo = qxl_create_cursor(qdev, user_bo,
0854                              new_state->fb->hot_x,
0855                              new_state->fb->hot_y);
0856         qxl_free_cursor(old_cursor_bo);
0857     }
0858 
0859     ret = qxl_bo_pin(user_bo);
0860     if (ret)
0861         return ret;
0862 
0863     return drm_gem_plane_helper_prepare_fb(plane, new_state);
0864 }
0865 
0866 static void qxl_plane_cleanup_fb(struct drm_plane *plane,
0867                  struct drm_plane_state *old_state)
0868 {
0869     struct drm_gem_object *obj;
0870     struct qxl_bo *user_bo;
0871 
0872     if (!old_state->fb) {
0873         /*
0874          * we never executed prepare_fb, so there's nothing to
0875          * unpin.
0876          */
0877         return;
0878     }
0879 
0880     obj = old_state->fb->obj[0];
0881     user_bo = gem_to_qxl_bo(obj);
0882     qxl_bo_unpin(user_bo);
0883 
0884     if (old_state->fb != plane->state->fb && user_bo->shadow) {
0885         qxl_bo_unpin(user_bo->shadow);
0886         drm_gem_object_put(&user_bo->shadow->tbo.base);
0887         user_bo->shadow = NULL;
0888     }
0889 }
0890 
0891 static const uint32_t qxl_cursor_plane_formats[] = {
0892     DRM_FORMAT_ARGB8888,
0893 };
0894 
0895 static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = {
0896     .atomic_update = qxl_cursor_atomic_update,
0897     .atomic_disable = qxl_cursor_atomic_disable,
0898     .prepare_fb = qxl_plane_prepare_fb,
0899     .cleanup_fb = qxl_plane_cleanup_fb,
0900 };
0901 
0902 static const struct drm_plane_funcs qxl_cursor_plane_funcs = {
0903     .update_plane   = drm_atomic_helper_update_plane,
0904     .disable_plane  = drm_atomic_helper_disable_plane,
0905     .destroy    = drm_primary_helper_destroy,
0906     .reset      = drm_atomic_helper_plane_reset,
0907     .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
0908     .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
0909 };
0910 
0911 static const uint32_t qxl_primary_plane_formats[] = {
0912     DRM_FORMAT_XRGB8888,
0913     DRM_FORMAT_ARGB8888,
0914 };
0915 
0916 static const struct drm_plane_helper_funcs primary_helper_funcs = {
0917     .atomic_check = qxl_primary_atomic_check,
0918     .atomic_update = qxl_primary_atomic_update,
0919     .atomic_disable = qxl_primary_atomic_disable,
0920     .prepare_fb = qxl_plane_prepare_fb,
0921     .cleanup_fb = qxl_plane_cleanup_fb,
0922 };
0923 
0924 static const struct drm_plane_funcs qxl_primary_plane_funcs = {
0925     .update_plane   = drm_atomic_helper_update_plane,
0926     .disable_plane  = drm_atomic_helper_disable_plane,
0927     .destroy    = drm_primary_helper_destroy,
0928     .reset      = drm_atomic_helper_plane_reset,
0929     .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
0930     .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
0931 };
0932 
0933 static struct drm_plane *qxl_create_plane(struct qxl_device *qdev,
0934                       unsigned int possible_crtcs,
0935                       enum drm_plane_type type)
0936 {
0937     const struct drm_plane_helper_funcs *helper_funcs = NULL;
0938     struct drm_plane *plane;
0939     const struct drm_plane_funcs *funcs;
0940     const uint32_t *formats;
0941     int num_formats;
0942     int err;
0943 
0944     if (type == DRM_PLANE_TYPE_PRIMARY) {
0945         funcs = &qxl_primary_plane_funcs;
0946         formats = qxl_primary_plane_formats;
0947         num_formats = ARRAY_SIZE(qxl_primary_plane_formats);
0948         helper_funcs = &primary_helper_funcs;
0949     } else if (type == DRM_PLANE_TYPE_CURSOR) {
0950         funcs = &qxl_cursor_plane_funcs;
0951         formats = qxl_cursor_plane_formats;
0952         helper_funcs = &qxl_cursor_helper_funcs;
0953         num_formats = ARRAY_SIZE(qxl_cursor_plane_formats);
0954     } else {
0955         return ERR_PTR(-EINVAL);
0956     }
0957 
0958     plane = kzalloc(sizeof(*plane), GFP_KERNEL);
0959     if (!plane)
0960         return ERR_PTR(-ENOMEM);
0961 
0962     err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs,
0963                        funcs, formats, num_formats,
0964                        NULL, type, NULL);
0965     if (err)
0966         goto free_plane;
0967 
0968     drm_plane_helper_add(plane, helper_funcs);
0969 
0970     return plane;
0971 
0972 free_plane:
0973     kfree(plane);
0974     return ERR_PTR(-EINVAL);
0975 }
0976 
0977 static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
0978 {
0979     struct qxl_crtc *qxl_crtc;
0980     struct drm_plane *primary, *cursor;
0981     struct qxl_device *qdev = to_qxl(dev);
0982     int r;
0983 
0984     qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
0985     if (!qxl_crtc)
0986         return -ENOMEM;
0987 
0988     primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY);
0989     if (IS_ERR(primary)) {
0990         r = -ENOMEM;
0991         goto free_mem;
0992     }
0993 
0994     cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR);
0995     if (IS_ERR(cursor)) {
0996         r = -ENOMEM;
0997         goto clean_primary;
0998     }
0999 
1000     r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor,
1001                       &qxl_crtc_funcs, NULL);
1002     if (r)
1003         goto clean_cursor;
1004 
1005     qxl_crtc->index = crtc_id;
1006     drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
1007     return 0;
1008 
1009 clean_cursor:
1010     drm_plane_cleanup(cursor);
1011     kfree(cursor);
1012 clean_primary:
1013     drm_plane_cleanup(primary);
1014     kfree(primary);
1015 free_mem:
1016     kfree(qxl_crtc);
1017     return r;
1018 }
1019 
1020 static int qxl_conn_get_modes(struct drm_connector *connector)
1021 {
1022     struct drm_device *dev = connector->dev;
1023     struct qxl_device *qdev = to_qxl(dev);
1024     struct qxl_output *output = drm_connector_to_qxl_output(connector);
1025     unsigned int pwidth = 1024;
1026     unsigned int pheight = 768;
1027     int ret = 0;
1028 
1029     if (qdev->client_monitors_config) {
1030         struct qxl_head *head;
1031         head = &qdev->client_monitors_config->heads[output->index];
1032         if (head->width)
1033             pwidth = head->width;
1034         if (head->height)
1035             pheight = head->height;
1036     }
1037 
1038     ret += drm_add_modes_noedid(connector, 8192, 8192);
1039     ret += qxl_add_extra_modes(connector);
1040     ret += qxl_add_monitors_config_modes(connector);
1041     drm_set_preferred_mode(connector, pwidth, pheight);
1042     return ret;
1043 }
1044 
1045 static enum drm_mode_status qxl_conn_mode_valid(struct drm_connector *connector,
1046                    struct drm_display_mode *mode)
1047 {
1048     struct drm_device *ddev = connector->dev;
1049     struct qxl_device *qdev = to_qxl(ddev);
1050 
1051     if (qxl_check_mode(qdev, mode->hdisplay, mode->vdisplay) != 0)
1052         return MODE_BAD;
1053 
1054     return MODE_OK;
1055 }
1056 
1057 static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
1058 {
1059     struct qxl_output *qxl_output =
1060         drm_connector_to_qxl_output(connector);
1061 
1062     DRM_DEBUG("\n");
1063     return &qxl_output->enc;
1064 }
1065 
1066 static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
1067     .get_modes = qxl_conn_get_modes,
1068     .mode_valid = qxl_conn_mode_valid,
1069     .best_encoder = qxl_best_encoder,
1070 };
1071 
1072 static enum drm_connector_status qxl_conn_detect(
1073             struct drm_connector *connector,
1074             bool force)
1075 {
1076     struct qxl_output *output =
1077         drm_connector_to_qxl_output(connector);
1078     struct drm_device *ddev = connector->dev;
1079     struct qxl_device *qdev = to_qxl(ddev);
1080     bool connected = false;
1081 
1082     /* The first monitor is always connected */
1083     if (!qdev->client_monitors_config) {
1084         if (output->index == 0)
1085             connected = true;
1086     } else
1087         connected = qdev->client_monitors_config->count > output->index &&
1088              qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
1089 
1090     DRM_DEBUG("#%d connected: %d\n", output->index, connected);
1091 
1092     return connected ? connector_status_connected
1093              : connector_status_disconnected;
1094 }
1095 
1096 static void qxl_conn_destroy(struct drm_connector *connector)
1097 {
1098     struct qxl_output *qxl_output =
1099         drm_connector_to_qxl_output(connector);
1100 
1101     drm_connector_unregister(connector);
1102     drm_connector_cleanup(connector);
1103     kfree(qxl_output);
1104 }
1105 
1106 static const struct drm_connector_funcs qxl_connector_funcs = {
1107     .detect = qxl_conn_detect,
1108     .fill_modes = drm_helper_probe_single_connector_modes,
1109     .destroy = qxl_conn_destroy,
1110     .reset = drm_atomic_helper_connector_reset,
1111     .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1112     .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1113 };
1114 
1115 static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
1116 {
1117     if (qdev->hotplug_mode_update_property)
1118         return 0;
1119 
1120     qdev->hotplug_mode_update_property =
1121         drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
1122                       "hotplug_mode_update", 0, 1);
1123 
1124     return 0;
1125 }
1126 
1127 static int qdev_output_init(struct drm_device *dev, int num_output)
1128 {
1129     struct qxl_device *qdev = to_qxl(dev);
1130     struct qxl_output *qxl_output;
1131     struct drm_connector *connector;
1132     struct drm_encoder *encoder;
1133     int ret;
1134 
1135     qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
1136     if (!qxl_output)
1137         return -ENOMEM;
1138 
1139     qxl_output->index = num_output;
1140 
1141     connector = &qxl_output->base;
1142     encoder = &qxl_output->enc;
1143     drm_connector_init(dev, &qxl_output->base,
1144                &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
1145 
1146     ret = drm_simple_encoder_init(dev, &qxl_output->enc,
1147                       DRM_MODE_ENCODER_VIRTUAL);
1148     if (ret) {
1149         drm_err(dev, "drm_simple_encoder_init() failed, error %d\n",
1150             ret);
1151         goto err_drm_connector_cleanup;
1152     }
1153 
1154     /* we get HPD via client monitors config */
1155     connector->polled = DRM_CONNECTOR_POLL_HPD;
1156     encoder->possible_crtcs = 1 << num_output;
1157     drm_connector_attach_encoder(&qxl_output->base,
1158                       &qxl_output->enc);
1159     drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
1160 
1161     drm_object_attach_property(&connector->base,
1162                    qdev->hotplug_mode_update_property, 0);
1163     drm_object_attach_property(&connector->base,
1164                    dev->mode_config.suggested_x_property, 0);
1165     drm_object_attach_property(&connector->base,
1166                    dev->mode_config.suggested_y_property, 0);
1167     return 0;
1168 
1169 err_drm_connector_cleanup:
1170     drm_connector_cleanup(&qxl_output->base);
1171     kfree(qxl_output);
1172     return ret;
1173 }
1174 
1175 static struct drm_framebuffer *
1176 qxl_user_framebuffer_create(struct drm_device *dev,
1177                 struct drm_file *file_priv,
1178                 const struct drm_mode_fb_cmd2 *mode_cmd)
1179 {
1180     return drm_gem_fb_create_with_funcs(dev, file_priv, mode_cmd,
1181                         &qxl_fb_funcs);
1182 }
1183 
1184 static const struct drm_mode_config_funcs qxl_mode_funcs = {
1185     .fb_create = qxl_user_framebuffer_create,
1186     .atomic_check = drm_atomic_helper_check,
1187     .atomic_commit = drm_atomic_helper_commit,
1188 };
1189 
1190 int qxl_create_monitors_object(struct qxl_device *qdev)
1191 {
1192     int ret;
1193     struct drm_gem_object *gobj;
1194     struct iosys_map map;
1195     int monitors_config_size = sizeof(struct qxl_monitors_config) +
1196         qxl_num_crtc * sizeof(struct qxl_head);
1197 
1198     ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1199                     QXL_GEM_DOMAIN_VRAM,
1200                     false, false, NULL, &gobj);
1201     if (ret) {
1202         DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1203         return -ENOMEM;
1204     }
1205     qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
1206 
1207     ret = qxl_bo_vmap(qdev->monitors_config_bo, &map);
1208     if (ret)
1209         return ret;
1210 
1211     qdev->monitors_config = qdev->monitors_config_bo->kptr;
1212     qdev->ram_header->monitors_config =
1213         qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1214 
1215     memset(qdev->monitors_config, 0, monitors_config_size);
1216     qdev->dumb_heads = kcalloc(qxl_num_crtc, sizeof(qdev->dumb_heads[0]),
1217                    GFP_KERNEL);
1218     if (!qdev->dumb_heads) {
1219         qxl_destroy_monitors_object(qdev);
1220         return -ENOMEM;
1221     }
1222     return 0;
1223 }
1224 
1225 int qxl_destroy_monitors_object(struct qxl_device *qdev)
1226 {
1227     int ret;
1228 
1229     if (!qdev->monitors_config_bo)
1230         return 0;
1231 
1232     qdev->monitors_config = NULL;
1233     qdev->ram_header->monitors_config = 0;
1234 
1235     ret = qxl_bo_vunmap(qdev->monitors_config_bo);
1236     if (ret)
1237         return ret;
1238 
1239     qxl_bo_unref(&qdev->monitors_config_bo);
1240     return 0;
1241 }
1242 
1243 int qxl_modeset_init(struct qxl_device *qdev)
1244 {
1245     int i;
1246     int ret;
1247 
1248     ret = drmm_mode_config_init(&qdev->ddev);
1249     if (ret)
1250         return ret;
1251 
1252     ret = qxl_create_monitors_object(qdev);
1253     if (ret)
1254         return ret;
1255 
1256     qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs;
1257 
1258     /* modes will be validated against the framebuffer size */
1259     qdev->ddev.mode_config.min_width = 0;
1260     qdev->ddev.mode_config.min_height = 0;
1261     qdev->ddev.mode_config.max_width = 8192;
1262     qdev->ddev.mode_config.max_height = 8192;
1263 
1264     qdev->ddev.mode_config.fb_base = qdev->vram_base;
1265 
1266     drm_mode_create_suggested_offset_properties(&qdev->ddev);
1267     qxl_mode_create_hotplug_mode_update_property(qdev);
1268 
1269     for (i = 0 ; i < qxl_num_crtc; ++i) {
1270         qdev_crtc_init(&qdev->ddev, i);
1271         qdev_output_init(&qdev->ddev, i);
1272     }
1273 
1274     qxl_display_read_client_monitors_config(qdev);
1275 
1276     drm_mode_config_reset(&qdev->ddev);
1277     return 0;
1278 }
1279 
1280 void qxl_modeset_fini(struct qxl_device *qdev)
1281 {
1282     if (qdev->dumb_shadow_bo) {
1283         qxl_bo_unpin(qdev->dumb_shadow_bo);
1284         drm_gem_object_put(&qdev->dumb_shadow_bo->tbo.base);
1285         qdev->dumb_shadow_bo = NULL;
1286     }
1287     qxl_destroy_monitors_object(qdev);
1288 }