1f64122c1SDave Airlie /* 2f64122c1SDave Airlie * Copyright 2013 Red Hat Inc. 3f64122c1SDave Airlie * 4f64122c1SDave Airlie * Permission is hereby granted, free of charge, to any person obtaining a 5f64122c1SDave Airlie * copy of this software and associated documentation files (the "Software"), 6f64122c1SDave Airlie * to deal in the Software without restriction, including without limitation 7f64122c1SDave Airlie * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8f64122c1SDave Airlie * and/or sell copies of the Software, and to permit persons to whom the 9f64122c1SDave Airlie * Software is furnished to do so, subject to the following conditions: 10f64122c1SDave Airlie * 11f64122c1SDave Airlie * The above copyright notice and this permission notice shall be included in 12f64122c1SDave Airlie * all copies or substantial portions of the Software. 13f64122c1SDave Airlie * 14f64122c1SDave Airlie * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15f64122c1SDave Airlie * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16f64122c1SDave Airlie * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17f64122c1SDave Airlie * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18f64122c1SDave Airlie * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19f64122c1SDave Airlie * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20f64122c1SDave Airlie * OTHER DEALINGS IN THE SOFTWARE. 21f64122c1SDave Airlie * 22f64122c1SDave Airlie * Authors: Dave Airlie 23f64122c1SDave Airlie * Alon Levy 24f64122c1SDave Airlie */ 25f64122c1SDave Airlie 26f64122c1SDave Airlie 27c5416d66SRandy Dunlap #include <linux/crc32.h> 28f64122c1SDave Airlie 29f64122c1SDave Airlie #include "qxl_drv.h" 30f64122c1SDave Airlie #include "qxl_object.h" 31f64122c1SDave Airlie #include "drm_crtc_helper.h" 323cb9ae4fSDaniel Vetter #include <drm/drm_plane_helper.h> 33f64122c1SDave Airlie 3407f8d9bdSDave Airlie static bool qxl_head_enabled(struct qxl_head *head) 3507f8d9bdSDave Airlie { 3607f8d9bdSDave Airlie return head->width && head->height; 3707f8d9bdSDave Airlie } 3807f8d9bdSDave Airlie 39f64122c1SDave Airlie void qxl_alloc_client_monitors_config(struct qxl_device *qdev, unsigned count) 40f64122c1SDave Airlie { 41f64122c1SDave Airlie if (qdev->client_monitors_config && 42f64122c1SDave Airlie count > qdev->client_monitors_config->count) { 43f64122c1SDave Airlie kfree(qdev->client_monitors_config); 4462c8ba7cSDave Airlie qdev->client_monitors_config = NULL; 45f64122c1SDave Airlie } 46f64122c1SDave Airlie if (!qdev->client_monitors_config) { 47f64122c1SDave Airlie qdev->client_monitors_config = kzalloc( 48f64122c1SDave Airlie sizeof(struct qxl_monitors_config) + 49f64122c1SDave Airlie sizeof(struct qxl_head) * count, GFP_KERNEL); 50f64122c1SDave Airlie if (!qdev->client_monitors_config) { 51f64122c1SDave Airlie qxl_io_log(qdev, 52f64122c1SDave Airlie "%s: allocation failure for %u heads\n", 53f64122c1SDave Airlie __func__, count); 54f64122c1SDave Airlie return; 55f64122c1SDave Airlie } 56f64122c1SDave Airlie } 57f64122c1SDave Airlie qdev->client_monitors_config->count = count; 58f64122c1SDave Airlie } 59f64122c1SDave Airlie 60f64122c1SDave Airlie static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev) 61f64122c1SDave Airlie { 62f64122c1SDave Airlie int i; 63f64122c1SDave Airlie int num_monitors; 64f64122c1SDave Airlie uint32_t crc; 65f64122c1SDave Airlie 66f64122c1SDave Airlie num_monitors = qdev->rom->client_monitors_config.count; 67f64122c1SDave Airlie crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config, 68f64122c1SDave Airlie sizeof(qdev->rom->client_monitors_config)); 69f64122c1SDave Airlie if (crc != qdev->rom->client_monitors_config_crc) { 7072ec5650SFrediano Ziglio qxl_io_log(qdev, "crc mismatch: have %X (%zd) != %X\n", crc, 71f64122c1SDave Airlie sizeof(qdev->rom->client_monitors_config), 72f64122c1SDave Airlie qdev->rom->client_monitors_config_crc); 73f64122c1SDave Airlie return 1; 74f64122c1SDave Airlie } 75f64122c1SDave Airlie if (num_monitors > qdev->monitors_config->max_allowed) { 765b8788c1SDave Airlie DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n", 77f64122c1SDave Airlie qdev->monitors_config->max_allowed, num_monitors); 78f64122c1SDave Airlie num_monitors = qdev->monitors_config->max_allowed; 79f64122c1SDave Airlie } else { 80f64122c1SDave Airlie num_monitors = qdev->rom->client_monitors_config.count; 81f64122c1SDave Airlie } 82f64122c1SDave Airlie qxl_alloc_client_monitors_config(qdev, num_monitors); 83f64122c1SDave Airlie /* we copy max from the client but it isn't used */ 84f64122c1SDave Airlie qdev->client_monitors_config->max_allowed = 85f64122c1SDave Airlie qdev->monitors_config->max_allowed; 86f64122c1SDave Airlie for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) { 87f64122c1SDave Airlie struct qxl_urect *c_rect = 88f64122c1SDave Airlie &qdev->rom->client_monitors_config.heads[i]; 89f64122c1SDave Airlie struct qxl_head *client_head = 90f64122c1SDave Airlie &qdev->client_monitors_config->heads[i]; 9107f8d9bdSDave Airlie client_head->x = c_rect->left; 9207f8d9bdSDave Airlie client_head->y = c_rect->top; 9307f8d9bdSDave Airlie client_head->width = c_rect->right - c_rect->left; 9407f8d9bdSDave Airlie client_head->height = c_rect->bottom - c_rect->top; 9507f8d9bdSDave Airlie client_head->surface_id = 0; 9607f8d9bdSDave Airlie client_head->id = i; 9707f8d9bdSDave Airlie client_head->flags = 0; 9807f8d9bdSDave Airlie DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height, 9907f8d9bdSDave Airlie client_head->x, client_head->y); 100f64122c1SDave Airlie } 101f64122c1SDave Airlie return 0; 102f64122c1SDave Airlie } 103f64122c1SDave Airlie 1047dea0941SDave Airlie static void qxl_update_offset_props(struct qxl_device *qdev) 1057dea0941SDave Airlie { 1067dea0941SDave Airlie struct drm_device *dev = qdev->ddev; 1077dea0941SDave Airlie struct drm_connector *connector; 1087dea0941SDave Airlie struct qxl_output *output; 1097dea0941SDave Airlie struct qxl_head *head; 1107dea0941SDave Airlie 1117dea0941SDave Airlie list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 1127dea0941SDave Airlie output = drm_connector_to_qxl_output(connector); 1137dea0941SDave Airlie 1147dea0941SDave Airlie head = &qdev->client_monitors_config->heads[output->index]; 1157dea0941SDave Airlie 1167dea0941SDave Airlie drm_object_property_set_value(&connector->base, 1177dea0941SDave Airlie dev->mode_config.suggested_x_property, head->x); 1187dea0941SDave Airlie drm_object_property_set_value(&connector->base, 1197dea0941SDave Airlie dev->mode_config.suggested_y_property, head->y); 1207dea0941SDave Airlie } 1217dea0941SDave Airlie } 1227dea0941SDave Airlie 123f64122c1SDave Airlie void qxl_display_read_client_monitors_config(struct qxl_device *qdev) 124f64122c1SDave Airlie { 125f64122c1SDave Airlie 1267dea0941SDave Airlie struct drm_device *dev = qdev->ddev; 127f64122c1SDave Airlie while (qxl_display_copy_rom_client_monitors_config(qdev)) { 128f64122c1SDave Airlie qxl_io_log(qdev, "failed crc check for client_monitors_config," 129f64122c1SDave Airlie " retrying\n"); 130f64122c1SDave Airlie } 1314fdb0869SMarc-André Lureau 1327dea0941SDave Airlie drm_modeset_lock_all(dev); 1337dea0941SDave Airlie qxl_update_offset_props(qdev); 1347dea0941SDave Airlie drm_modeset_unlock_all(dev); 1354fdb0869SMarc-André Lureau if (!drm_helper_hpd_irq_event(qdev->ddev)) { 1364fdb0869SMarc-André Lureau /* notify that the monitor configuration changed, to 1374fdb0869SMarc-André Lureau adjust at the arbitrary resolution */ 1384fdb0869SMarc-André Lureau drm_kms_helper_hotplug_event(qdev->ddev); 1394fdb0869SMarc-André Lureau } 140f64122c1SDave Airlie } 141f64122c1SDave Airlie 142b0807423SMarc-André Lureau static int qxl_add_monitors_config_modes(struct drm_connector *connector, 143b0807423SMarc-André Lureau unsigned *pwidth, 144b0807423SMarc-André Lureau unsigned *pheight) 145f64122c1SDave Airlie { 146f64122c1SDave Airlie struct drm_device *dev = connector->dev; 147f64122c1SDave Airlie struct qxl_device *qdev = dev->dev_private; 148f64122c1SDave Airlie struct qxl_output *output = drm_connector_to_qxl_output(connector); 149f64122c1SDave Airlie int h = output->index; 150f64122c1SDave Airlie struct drm_display_mode *mode = NULL; 151f64122c1SDave Airlie struct qxl_head *head; 152f64122c1SDave Airlie 15307f8d9bdSDave Airlie if (!qdev->client_monitors_config) 154f64122c1SDave Airlie return 0; 15507f8d9bdSDave Airlie head = &qdev->client_monitors_config->heads[h]; 156f64122c1SDave Airlie 157f64122c1SDave Airlie mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false, 158f64122c1SDave Airlie false); 159f64122c1SDave Airlie mode->type |= DRM_MODE_TYPE_PREFERRED; 160b0807423SMarc-André Lureau *pwidth = head->width; 161b0807423SMarc-André Lureau *pheight = head->height; 162f64122c1SDave Airlie drm_mode_probed_add(connector, mode); 163bd3e1c7cSJonathon Jongsma /* remember the last custom size for mode validation */ 164bd3e1c7cSJonathon Jongsma qdev->monitors_config_width = mode->hdisplay; 165bd3e1c7cSJonathon Jongsma qdev->monitors_config_height = mode->vdisplay; 166f64122c1SDave Airlie return 1; 167f64122c1SDave Airlie } 168f64122c1SDave Airlie 169bd3e1c7cSJonathon Jongsma static struct mode_size { 170f64122c1SDave Airlie int w; 171f64122c1SDave Airlie int h; 172f64122c1SDave Airlie } common_modes[] = { 173f64122c1SDave Airlie { 640, 480}, 174f64122c1SDave Airlie { 720, 480}, 175f64122c1SDave Airlie { 800, 600}, 176f64122c1SDave Airlie { 848, 480}, 177f64122c1SDave Airlie {1024, 768}, 178f64122c1SDave Airlie {1152, 768}, 179f64122c1SDave Airlie {1280, 720}, 180f64122c1SDave Airlie {1280, 800}, 181f64122c1SDave Airlie {1280, 854}, 182f64122c1SDave Airlie {1280, 960}, 183f64122c1SDave Airlie {1280, 1024}, 184f64122c1SDave Airlie {1440, 900}, 185f64122c1SDave Airlie {1400, 1050}, 186f64122c1SDave Airlie {1680, 1050}, 187f64122c1SDave Airlie {1600, 1200}, 188f64122c1SDave Airlie {1920, 1080}, 189f64122c1SDave Airlie {1920, 1200} 190f64122c1SDave Airlie }; 191f64122c1SDave Airlie 192bd3e1c7cSJonathon Jongsma static int qxl_add_common_modes(struct drm_connector *connector, 193bd3e1c7cSJonathon Jongsma unsigned pwidth, 194bd3e1c7cSJonathon Jongsma unsigned pheight) 195bd3e1c7cSJonathon Jongsma { 196bd3e1c7cSJonathon Jongsma struct drm_device *dev = connector->dev; 197bd3e1c7cSJonathon Jongsma struct drm_display_mode *mode = NULL; 198bd3e1c7cSJonathon Jongsma int i; 199f64122c1SDave Airlie for (i = 0; i < ARRAY_SIZE(common_modes); i++) { 200f64122c1SDave Airlie mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h, 201f64122c1SDave Airlie 60, false, false, false); 202b0807423SMarc-André Lureau if (common_modes[i].w == pwidth && common_modes[i].h == pheight) 203f64122c1SDave Airlie mode->type |= DRM_MODE_TYPE_PREFERRED; 204f64122c1SDave Airlie drm_mode_probed_add(connector, mode); 205f64122c1SDave Airlie } 206f64122c1SDave Airlie return i - 1; 207f64122c1SDave Airlie } 208f64122c1SDave Airlie 209f64122c1SDave Airlie static void qxl_crtc_destroy(struct drm_crtc *crtc) 210f64122c1SDave Airlie { 211f64122c1SDave Airlie struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc); 212f64122c1SDave Airlie 213f64122c1SDave Airlie drm_crtc_cleanup(crtc); 214f64122c1SDave Airlie kfree(qxl_crtc); 215f64122c1SDave Airlie } 216f64122c1SDave Airlie 217058e9f5cSAndreas Pokorny static int qxl_crtc_page_flip(struct drm_crtc *crtc, 218058e9f5cSAndreas Pokorny struct drm_framebuffer *fb, 219058e9f5cSAndreas Pokorny struct drm_pending_vblank_event *event, 220058e9f5cSAndreas Pokorny uint32_t page_flip_flags) 221058e9f5cSAndreas Pokorny { 222058e9f5cSAndreas Pokorny struct drm_device *dev = crtc->dev; 223058e9f5cSAndreas Pokorny struct qxl_device *qdev = dev->dev_private; 224058e9f5cSAndreas Pokorny struct qxl_crtc *qcrtc = to_qxl_crtc(crtc); 225058e9f5cSAndreas Pokorny struct qxl_framebuffer *qfb_src = to_qxl_framebuffer(fb); 226058e9f5cSAndreas Pokorny struct qxl_framebuffer *qfb_old = to_qxl_framebuffer(crtc->primary->fb); 227058e9f5cSAndreas Pokorny struct qxl_bo *bo_old = gem_to_qxl_bo(qfb_old->obj); 228058e9f5cSAndreas Pokorny struct qxl_bo *bo = gem_to_qxl_bo(qfb_src->obj); 229058e9f5cSAndreas Pokorny unsigned long flags; 230058e9f5cSAndreas Pokorny struct drm_clip_rect norect = { 231058e9f5cSAndreas Pokorny .x1 = 0, 232058e9f5cSAndreas Pokorny .y1 = 0, 233058e9f5cSAndreas Pokorny .x2 = fb->width, 234058e9f5cSAndreas Pokorny .y2 = fb->height 235058e9f5cSAndreas Pokorny }; 236058e9f5cSAndreas Pokorny int inc = 1; 237058e9f5cSAndreas Pokorny int one_clip_rect = 1; 238058e9f5cSAndreas Pokorny int ret = 0; 239058e9f5cSAndreas Pokorny 240058e9f5cSAndreas Pokorny crtc->primary->fb = fb; 241058e9f5cSAndreas Pokorny bo_old->is_primary = false; 242058e9f5cSAndreas Pokorny bo->is_primary = true; 243058e9f5cSAndreas Pokorny 244058e9f5cSAndreas Pokorny ret = qxl_bo_reserve(bo, false); 245058e9f5cSAndreas Pokorny if (ret) 246058e9f5cSAndreas Pokorny return ret; 2477eb9974fSFrediano Ziglio ret = qxl_bo_pin(bo, bo->type, NULL); 2487eb9974fSFrediano Ziglio qxl_bo_unreserve(bo); 2497eb9974fSFrediano Ziglio if (ret) 2507eb9974fSFrediano Ziglio return ret; 251058e9f5cSAndreas Pokorny 252058e9f5cSAndreas Pokorny qxl_draw_dirty_fb(qdev, qfb_src, bo, 0, 0, 253058e9f5cSAndreas Pokorny &norect, one_clip_rect, inc); 254058e9f5cSAndreas Pokorny 255058e9f5cSAndreas Pokorny drm_vblank_get(dev, qcrtc->index); 256058e9f5cSAndreas Pokorny 257058e9f5cSAndreas Pokorny if (event) { 258058e9f5cSAndreas Pokorny spin_lock_irqsave(&dev->event_lock, flags); 259058e9f5cSAndreas Pokorny drm_send_vblank_event(dev, qcrtc->index, event); 260058e9f5cSAndreas Pokorny spin_unlock_irqrestore(&dev->event_lock, flags); 261058e9f5cSAndreas Pokorny } 262058e9f5cSAndreas Pokorny drm_vblank_put(dev, qcrtc->index); 263058e9f5cSAndreas Pokorny 2647eb9974fSFrediano Ziglio ret = qxl_bo_reserve(bo, false); 2657eb9974fSFrediano Ziglio if (!ret) { 2667eb9974fSFrediano Ziglio qxl_bo_unpin(bo); 267058e9f5cSAndreas Pokorny qxl_bo_unreserve(bo); 2687eb9974fSFrediano Ziglio } 269058e9f5cSAndreas Pokorny 270058e9f5cSAndreas Pokorny return 0; 271058e9f5cSAndreas Pokorny } 272058e9f5cSAndreas Pokorny 2738002db63SDave Airlie static int 274f64122c1SDave Airlie qxl_hide_cursor(struct qxl_device *qdev) 275f64122c1SDave Airlie { 276f64122c1SDave Airlie struct qxl_release *release; 277f64122c1SDave Airlie struct qxl_cursor_cmd *cmd; 278f64122c1SDave Airlie int ret; 279f64122c1SDave Airlie 280f64122c1SDave Airlie ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD, 281f64122c1SDave Airlie &release, NULL); 2828002db63SDave Airlie if (ret) 2838002db63SDave Airlie return ret; 2848002db63SDave Airlie 2858002db63SDave Airlie ret = qxl_release_reserve_list(release, true); 2868002db63SDave Airlie if (ret) { 2878002db63SDave Airlie qxl_release_free(qdev, release); 2888002db63SDave Airlie return ret; 2898002db63SDave Airlie } 290f64122c1SDave Airlie 291f64122c1SDave Airlie cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release); 292f64122c1SDave Airlie cmd->type = QXL_CURSOR_HIDE; 293f64122c1SDave Airlie qxl_release_unmap(qdev, release, &cmd->release_info); 294f64122c1SDave Airlie 295f64122c1SDave Airlie qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false); 2968002db63SDave Airlie qxl_release_fence_buffer_objects(release); 2978002db63SDave Airlie return 0; 298f64122c1SDave Airlie } 299f64122c1SDave Airlie 300c0a60802SDave Airlie static int qxl_crtc_cursor_set2(struct drm_crtc *crtc, 301f64122c1SDave Airlie struct drm_file *file_priv, 302f64122c1SDave Airlie uint32_t handle, 303f64122c1SDave Airlie uint32_t width, 304c0a60802SDave Airlie uint32_t height, int32_t hot_x, int32_t hot_y) 305f64122c1SDave Airlie { 306f64122c1SDave Airlie struct drm_device *dev = crtc->dev; 307f64122c1SDave Airlie struct qxl_device *qdev = dev->dev_private; 308f64122c1SDave Airlie struct qxl_crtc *qcrtc = to_qxl_crtc(crtc); 309f64122c1SDave Airlie struct drm_gem_object *obj; 310f64122c1SDave Airlie struct qxl_cursor *cursor; 311f64122c1SDave Airlie struct qxl_cursor_cmd *cmd; 312f64122c1SDave Airlie struct qxl_bo *cursor_bo, *user_bo; 313f64122c1SDave Airlie struct qxl_release *release; 314f64122c1SDave Airlie void *user_ptr; 315f64122c1SDave Airlie 316f64122c1SDave Airlie int size = 64*64*4; 317f64122c1SDave Airlie int ret = 0; 3188002db63SDave Airlie if (!handle) 3198002db63SDave Airlie return qxl_hide_cursor(qdev); 320f64122c1SDave Airlie 321f64122c1SDave Airlie obj = drm_gem_object_lookup(crtc->dev, file_priv, handle); 322f64122c1SDave Airlie if (!obj) { 323f64122c1SDave Airlie DRM_ERROR("cannot find cursor object\n"); 324f64122c1SDave Airlie return -ENOENT; 325f64122c1SDave Airlie } 326f64122c1SDave Airlie 327f64122c1SDave Airlie user_bo = gem_to_qxl_bo(obj); 328f64122c1SDave Airlie 329f64122c1SDave Airlie ret = qxl_bo_reserve(user_bo, false); 330f64122c1SDave Airlie if (ret) 331f64122c1SDave Airlie goto out_unref; 332f64122c1SDave Airlie 333f64122c1SDave Airlie ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL); 3348002db63SDave Airlie qxl_bo_unreserve(user_bo); 335f64122c1SDave Airlie if (ret) 3368002db63SDave Airlie goto out_unref; 337f64122c1SDave Airlie 338f64122c1SDave Airlie ret = qxl_bo_kmap(user_bo, &user_ptr); 339f64122c1SDave Airlie if (ret) 340f64122c1SDave Airlie goto out_unpin; 341f64122c1SDave Airlie 342f64122c1SDave Airlie ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), 343f64122c1SDave Airlie QXL_RELEASE_CURSOR_CMD, 344f64122c1SDave Airlie &release, NULL); 345f64122c1SDave Airlie if (ret) 346f64122c1SDave Airlie goto out_kunmap; 3478002db63SDave Airlie 3488002db63SDave Airlie ret = qxl_alloc_bo_reserved(qdev, release, sizeof(struct qxl_cursor) + size, 349f64122c1SDave Airlie &cursor_bo); 350f64122c1SDave Airlie if (ret) 351f64122c1SDave Airlie goto out_free_release; 3528002db63SDave Airlie 3538002db63SDave Airlie ret = qxl_release_reserve_list(release, false); 354f64122c1SDave Airlie if (ret) 355f64122c1SDave Airlie goto out_free_bo; 356f64122c1SDave Airlie 3578002db63SDave Airlie ret = qxl_bo_kmap(cursor_bo, (void **)&cursor); 3588002db63SDave Airlie if (ret) 3598002db63SDave Airlie goto out_backoff; 3608002db63SDave Airlie 361f64122c1SDave Airlie cursor->header.unique = 0; 362f64122c1SDave Airlie cursor->header.type = SPICE_CURSOR_TYPE_ALPHA; 363f64122c1SDave Airlie cursor->header.width = 64; 364f64122c1SDave Airlie cursor->header.height = 64; 365c0a60802SDave Airlie cursor->header.hot_spot_x = hot_x; 366c0a60802SDave Airlie cursor->header.hot_spot_y = hot_y; 367f64122c1SDave Airlie cursor->data_size = size; 368f64122c1SDave Airlie cursor->chunk.next_chunk = 0; 369f64122c1SDave Airlie cursor->chunk.prev_chunk = 0; 370f64122c1SDave Airlie cursor->chunk.data_size = size; 371f64122c1SDave Airlie 372f64122c1SDave Airlie memcpy(cursor->chunk.data, user_ptr, size); 373f64122c1SDave Airlie 374f64122c1SDave Airlie qxl_bo_kunmap(cursor_bo); 375f64122c1SDave Airlie 376f64122c1SDave Airlie qxl_bo_kunmap(user_bo); 377f64122c1SDave Airlie 378f64122c1SDave Airlie cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release); 379f64122c1SDave Airlie cmd->type = QXL_CURSOR_SET; 380f64122c1SDave Airlie cmd->u.set.position.x = qcrtc->cur_x; 381f64122c1SDave Airlie cmd->u.set.position.y = qcrtc->cur_y; 382f64122c1SDave Airlie 383f64122c1SDave Airlie cmd->u.set.shape = qxl_bo_physical_address(qdev, cursor_bo, 0); 384f64122c1SDave Airlie 385f64122c1SDave Airlie cmd->u.set.visible = 1; 386f64122c1SDave Airlie qxl_release_unmap(qdev, release, &cmd->release_info); 387f64122c1SDave Airlie 388f64122c1SDave Airlie qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false); 3898002db63SDave Airlie qxl_release_fence_buffer_objects(release); 390f64122c1SDave Airlie 3918002db63SDave Airlie /* finish with the userspace bo */ 3928002db63SDave Airlie ret = qxl_bo_reserve(user_bo, false); 3938002db63SDave Airlie if (!ret) { 3948002db63SDave Airlie qxl_bo_unpin(user_bo); 3958002db63SDave Airlie qxl_bo_unreserve(user_bo); 3968002db63SDave Airlie } 3978002db63SDave Airlie drm_gem_object_unreference_unlocked(obj); 3988002db63SDave Airlie 399f64122c1SDave Airlie qxl_bo_unref(&cursor_bo); 400f64122c1SDave Airlie 401f64122c1SDave Airlie return ret; 4028002db63SDave Airlie 4038002db63SDave Airlie out_backoff: 4048002db63SDave Airlie qxl_release_backoff_reserve_list(release); 405f64122c1SDave Airlie out_free_bo: 406f64122c1SDave Airlie qxl_bo_unref(&cursor_bo); 407f64122c1SDave Airlie out_free_release: 408f64122c1SDave Airlie qxl_release_free(qdev, release); 409f64122c1SDave Airlie out_kunmap: 410f64122c1SDave Airlie qxl_bo_kunmap(user_bo); 411f64122c1SDave Airlie out_unpin: 412f64122c1SDave Airlie qxl_bo_unpin(user_bo); 413f64122c1SDave Airlie out_unref: 414f64122c1SDave Airlie drm_gem_object_unreference_unlocked(obj); 415f64122c1SDave Airlie return ret; 416f64122c1SDave Airlie } 417f64122c1SDave Airlie 418f64122c1SDave Airlie static int qxl_crtc_cursor_move(struct drm_crtc *crtc, 419f64122c1SDave Airlie int x, int y) 420f64122c1SDave Airlie { 421f64122c1SDave Airlie struct drm_device *dev = crtc->dev; 422f64122c1SDave Airlie struct qxl_device *qdev = dev->dev_private; 423f64122c1SDave Airlie struct qxl_crtc *qcrtc = to_qxl_crtc(crtc); 424f64122c1SDave Airlie struct qxl_release *release; 425f64122c1SDave Airlie struct qxl_cursor_cmd *cmd; 426f64122c1SDave Airlie int ret; 427f64122c1SDave Airlie 428f64122c1SDave Airlie ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD, 429f64122c1SDave Airlie &release, NULL); 4308002db63SDave Airlie if (ret) 4318002db63SDave Airlie return ret; 4328002db63SDave Airlie 4338002db63SDave Airlie ret = qxl_release_reserve_list(release, true); 4348002db63SDave Airlie if (ret) { 4358002db63SDave Airlie qxl_release_free(qdev, release); 4368002db63SDave Airlie return ret; 4378002db63SDave Airlie } 438f64122c1SDave Airlie 439f64122c1SDave Airlie qcrtc->cur_x = x; 440f64122c1SDave Airlie qcrtc->cur_y = y; 441f64122c1SDave Airlie 442f64122c1SDave Airlie cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release); 443f64122c1SDave Airlie cmd->type = QXL_CURSOR_MOVE; 444f64122c1SDave Airlie cmd->u.position.x = qcrtc->cur_x; 445f64122c1SDave Airlie cmd->u.position.y = qcrtc->cur_y; 446f64122c1SDave Airlie qxl_release_unmap(qdev, release, &cmd->release_info); 447f64122c1SDave Airlie 448f64122c1SDave Airlie qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false); 4498002db63SDave Airlie qxl_release_fence_buffer_objects(release); 4508002db63SDave Airlie 451f64122c1SDave Airlie return 0; 452f64122c1SDave Airlie } 453f64122c1SDave Airlie 454f64122c1SDave Airlie 455f64122c1SDave Airlie static const struct drm_crtc_funcs qxl_crtc_funcs = { 456c0a60802SDave Airlie .cursor_set2 = qxl_crtc_cursor_set2, 457f64122c1SDave Airlie .cursor_move = qxl_crtc_cursor_move, 458f64122c1SDave Airlie .set_config = drm_crtc_helper_set_config, 459f64122c1SDave Airlie .destroy = qxl_crtc_destroy, 460058e9f5cSAndreas Pokorny .page_flip = qxl_crtc_page_flip, 461f64122c1SDave Airlie }; 462f64122c1SDave Airlie 463f64122c1SDave Airlie static void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb) 464f64122c1SDave Airlie { 465f64122c1SDave Airlie struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb); 466f64122c1SDave Airlie 467f64122c1SDave Airlie if (qxl_fb->obj) 468f64122c1SDave Airlie drm_gem_object_unreference_unlocked(qxl_fb->obj); 469f64122c1SDave Airlie drm_framebuffer_cleanup(fb); 470f64122c1SDave Airlie kfree(qxl_fb); 471f64122c1SDave Airlie } 472f64122c1SDave Airlie 4736d01f1f5SDave Airlie static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb, 474f64122c1SDave Airlie struct drm_file *file_priv, 475f64122c1SDave Airlie unsigned flags, unsigned color, 476f64122c1SDave Airlie struct drm_clip_rect *clips, 477f64122c1SDave Airlie unsigned num_clips) 478f64122c1SDave Airlie { 479f64122c1SDave Airlie /* TODO: vmwgfx where this was cribbed from had locking. Why? */ 480f64122c1SDave Airlie struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb); 481f64122c1SDave Airlie struct qxl_device *qdev = qxl_fb->base.dev->dev_private; 482f64122c1SDave Airlie struct drm_clip_rect norect; 483f64122c1SDave Airlie struct qxl_bo *qobj; 484f64122c1SDave Airlie int inc = 1; 485f64122c1SDave Airlie 48673e9efd4SVille Syrjälä drm_modeset_lock_all(fb->dev); 48773e9efd4SVille Syrjälä 488f64122c1SDave Airlie qobj = gem_to_qxl_bo(qxl_fb->obj); 489b2b4465dSDave Airlie /* if we aren't primary surface ignore this */ 49073e9efd4SVille Syrjälä if (!qobj->is_primary) { 49173e9efd4SVille Syrjälä drm_modeset_unlock_all(fb->dev); 492b2b4465dSDave Airlie return 0; 49373e9efd4SVille Syrjälä } 494b2b4465dSDave Airlie 495f64122c1SDave Airlie if (!num_clips) { 496f64122c1SDave Airlie num_clips = 1; 497f64122c1SDave Airlie clips = &norect; 498f64122c1SDave Airlie norect.x1 = norect.y1 = 0; 499f64122c1SDave Airlie norect.x2 = fb->width; 500f64122c1SDave Airlie norect.y2 = fb->height; 501f64122c1SDave Airlie } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) { 502f64122c1SDave Airlie num_clips /= 2; 503f64122c1SDave Airlie inc = 2; /* skip source rects */ 504f64122c1SDave Airlie } 505f64122c1SDave Airlie 506f64122c1SDave Airlie qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color, 507f64122c1SDave Airlie clips, num_clips, inc); 50873e9efd4SVille Syrjälä 50973e9efd4SVille Syrjälä drm_modeset_unlock_all(fb->dev); 51073e9efd4SVille Syrjälä 511f64122c1SDave Airlie return 0; 512f64122c1SDave Airlie } 513f64122c1SDave Airlie 514f64122c1SDave Airlie static const struct drm_framebuffer_funcs qxl_fb_funcs = { 515f64122c1SDave Airlie .destroy = qxl_user_framebuffer_destroy, 516f64122c1SDave Airlie .dirty = qxl_framebuffer_surface_dirty, 517f64122c1SDave Airlie /* TODO? 518f64122c1SDave Airlie * .create_handle = qxl_user_framebuffer_create_handle, */ 519f64122c1SDave Airlie }; 520f64122c1SDave Airlie 521f64122c1SDave Airlie int 522f64122c1SDave Airlie qxl_framebuffer_init(struct drm_device *dev, 523f64122c1SDave Airlie struct qxl_framebuffer *qfb, 5241eb83451SVille Syrjälä const struct drm_mode_fb_cmd2 *mode_cmd, 525f64122c1SDave Airlie struct drm_gem_object *obj) 526f64122c1SDave Airlie { 527f64122c1SDave Airlie int ret; 528f64122c1SDave Airlie 529f64122c1SDave Airlie qfb->obj = obj; 530f64122c1SDave Airlie ret = drm_framebuffer_init(dev, &qfb->base, &qxl_fb_funcs); 531f64122c1SDave Airlie if (ret) { 532f64122c1SDave Airlie qfb->obj = NULL; 533f64122c1SDave Airlie return ret; 534f64122c1SDave Airlie } 535f64122c1SDave Airlie drm_helper_mode_fill_fb_struct(&qfb->base, mode_cmd); 536f64122c1SDave Airlie return 0; 537f64122c1SDave Airlie } 538f64122c1SDave Airlie 539f64122c1SDave Airlie static void qxl_crtc_dpms(struct drm_crtc *crtc, int mode) 540f64122c1SDave Airlie { 541f64122c1SDave Airlie } 542f64122c1SDave Airlie 543f64122c1SDave Airlie static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc, 544f64122c1SDave Airlie const struct drm_display_mode *mode, 545f64122c1SDave Airlie struct drm_display_mode *adjusted_mode) 546f64122c1SDave Airlie { 547f64122c1SDave Airlie struct drm_device *dev = crtc->dev; 548f64122c1SDave Airlie struct qxl_device *qdev = dev->dev_private; 549f64122c1SDave Airlie 550f64122c1SDave Airlie qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n", 551f64122c1SDave Airlie __func__, 552f64122c1SDave Airlie mode->hdisplay, mode->vdisplay, 553f64122c1SDave Airlie adjusted_mode->hdisplay, 554f64122c1SDave Airlie adjusted_mode->vdisplay); 555f64122c1SDave Airlie return true; 556f64122c1SDave Airlie } 557f64122c1SDave Airlie 558f64122c1SDave Airlie void 559f64122c1SDave Airlie qxl_send_monitors_config(struct qxl_device *qdev) 560f64122c1SDave Airlie { 561f64122c1SDave Airlie int i; 562f64122c1SDave Airlie 563f64122c1SDave Airlie BUG_ON(!qdev->ram_header->monitors_config); 564f64122c1SDave Airlie 565f64122c1SDave Airlie if (qdev->monitors_config->count == 0) { 566f64122c1SDave Airlie qxl_io_log(qdev, "%s: 0 monitors??\n", __func__); 567f64122c1SDave Airlie return; 568f64122c1SDave Airlie } 569f64122c1SDave Airlie for (i = 0 ; i < qdev->monitors_config->count ; ++i) { 570f64122c1SDave Airlie struct qxl_head *head = &qdev->monitors_config->heads[i]; 571f64122c1SDave Airlie 57207f8d9bdSDave Airlie if (head->y > 8192 || head->x > 8192 || 573f64122c1SDave Airlie head->width > 8192 || head->height > 8192) { 574f64122c1SDave Airlie DRM_ERROR("head %d wrong: %dx%d+%d+%d\n", 575f64122c1SDave Airlie i, head->width, head->height, 576f64122c1SDave Airlie head->x, head->y); 577f64122c1SDave Airlie return; 578f64122c1SDave Airlie } 579f64122c1SDave Airlie } 580f64122c1SDave Airlie qxl_io_monitors_config(qdev); 581f64122c1SDave Airlie } 582f64122c1SDave Airlie 58307f8d9bdSDave Airlie static void qxl_monitors_config_set(struct qxl_device *qdev, 58407f8d9bdSDave Airlie int index, 585f64122c1SDave Airlie unsigned x, unsigned y, 58607f8d9bdSDave Airlie unsigned width, unsigned height, 58707f8d9bdSDave Airlie unsigned surf_id) 588f64122c1SDave Airlie { 58907f8d9bdSDave Airlie DRM_DEBUG_KMS("%d:%dx%d+%d+%d\n", index, width, height, x, y); 59007f8d9bdSDave Airlie qdev->monitors_config->heads[index].x = x; 59107f8d9bdSDave Airlie qdev->monitors_config->heads[index].y = y; 59207f8d9bdSDave Airlie qdev->monitors_config->heads[index].width = width; 59307f8d9bdSDave Airlie qdev->monitors_config->heads[index].height = height; 59407f8d9bdSDave Airlie qdev->monitors_config->heads[index].surface_id = surf_id; 59507f8d9bdSDave Airlie 596f64122c1SDave Airlie } 597f64122c1SDave Airlie 598f64122c1SDave Airlie static int qxl_crtc_mode_set(struct drm_crtc *crtc, 599f64122c1SDave Airlie struct drm_display_mode *mode, 600f64122c1SDave Airlie struct drm_display_mode *adjusted_mode, 601f64122c1SDave Airlie int x, int y, 602f64122c1SDave Airlie struct drm_framebuffer *old_fb) 603f64122c1SDave Airlie { 604f64122c1SDave Airlie struct drm_device *dev = crtc->dev; 605f64122c1SDave Airlie struct qxl_device *qdev = dev->dev_private; 606f64122c1SDave Airlie struct qxl_framebuffer *qfb; 607f64122c1SDave Airlie struct qxl_bo *bo, *old_bo = NULL; 60807f8d9bdSDave Airlie struct qxl_crtc *qcrtc = to_qxl_crtc(crtc); 609f64122c1SDave Airlie bool recreate_primary = false; 610f64122c1SDave Airlie int ret; 61107f8d9bdSDave Airlie int surf_id; 612f4510a27SMatt Roper if (!crtc->primary->fb) { 613f64122c1SDave Airlie DRM_DEBUG_KMS("No FB bound\n"); 614f64122c1SDave Airlie return 0; 615f64122c1SDave Airlie } 616f64122c1SDave Airlie 617f64122c1SDave Airlie if (old_fb) { 618f64122c1SDave Airlie qfb = to_qxl_framebuffer(old_fb); 619f64122c1SDave Airlie old_bo = gem_to_qxl_bo(qfb->obj); 620f64122c1SDave Airlie } 621f4510a27SMatt Roper qfb = to_qxl_framebuffer(crtc->primary->fb); 622f64122c1SDave Airlie bo = gem_to_qxl_bo(qfb->obj); 623f64122c1SDave Airlie DRM_DEBUG("+%d+%d (%d,%d) => (%d,%d)\n", 624f64122c1SDave Airlie x, y, 625f64122c1SDave Airlie mode->hdisplay, mode->vdisplay, 626f64122c1SDave Airlie adjusted_mode->hdisplay, 627f64122c1SDave Airlie adjusted_mode->vdisplay); 628f64122c1SDave Airlie 6298d0d9401SFabiano Fidêncio if (bo->is_primary == false) 630f64122c1SDave Airlie recreate_primary = true; 631f64122c1SDave Airlie 632c572aaf4SMarc-André Lureau if (bo->surf.stride * bo->surf.height > qdev->vram_size) { 633c572aaf4SMarc-André Lureau DRM_ERROR("Mode doesn't fit in vram size (vgamem)"); 634c572aaf4SMarc-André Lureau return -EINVAL; 635c572aaf4SMarc-André Lureau } 636f64122c1SDave Airlie 637f64122c1SDave Airlie ret = qxl_bo_reserve(bo, false); 638f64122c1SDave Airlie if (ret != 0) 639f64122c1SDave Airlie return ret; 640f64122c1SDave Airlie ret = qxl_bo_pin(bo, bo->type, NULL); 641f64122c1SDave Airlie if (ret != 0) { 642f64122c1SDave Airlie qxl_bo_unreserve(bo); 643f64122c1SDave Airlie return -EINVAL; 644f64122c1SDave Airlie } 645f64122c1SDave Airlie qxl_bo_unreserve(bo); 646f64122c1SDave Airlie if (recreate_primary) { 647f64122c1SDave Airlie qxl_io_destroy_primary(qdev); 648f64122c1SDave Airlie qxl_io_log(qdev, 649c572aaf4SMarc-André Lureau "recreate primary: %dx%d,%d,%d\n", 650c572aaf4SMarc-André Lureau bo->surf.width, bo->surf.height, 651c572aaf4SMarc-André Lureau bo->surf.stride, bo->surf.format); 652c572aaf4SMarc-André Lureau qxl_io_create_primary(qdev, 0, bo); 653f64122c1SDave Airlie bo->is_primary = true; 65452571ad5SDavid Mansfield } 65552571ad5SDavid Mansfield 65652571ad5SDavid Mansfield if (bo->is_primary) { 65752571ad5SDavid Mansfield DRM_DEBUG_KMS("setting surface_id to 0 for primary surface %d on crtc %d\n", bo->surface_id, qcrtc->index); 65807f8d9bdSDave Airlie surf_id = 0; 65907f8d9bdSDave Airlie } else { 66007f8d9bdSDave Airlie surf_id = bo->surface_id; 661f64122c1SDave Airlie } 662f64122c1SDave Airlie 663f64122c1SDave Airlie if (old_bo && old_bo != bo) { 664f64122c1SDave Airlie old_bo->is_primary = false; 665f64122c1SDave Airlie ret = qxl_bo_reserve(old_bo, false); 666f64122c1SDave Airlie qxl_bo_unpin(old_bo); 667f64122c1SDave Airlie qxl_bo_unreserve(old_bo); 668f64122c1SDave Airlie } 669f64122c1SDave Airlie 67007f8d9bdSDave Airlie qxl_monitors_config_set(qdev, qcrtc->index, x, y, 671f64122c1SDave Airlie mode->hdisplay, 67207f8d9bdSDave Airlie mode->vdisplay, surf_id); 673f64122c1SDave Airlie return 0; 674f64122c1SDave Airlie } 675f64122c1SDave Airlie 676f64122c1SDave Airlie static void qxl_crtc_prepare(struct drm_crtc *crtc) 677f64122c1SDave Airlie { 678f64122c1SDave Airlie DRM_DEBUG("current: %dx%d+%d+%d (%d).\n", 679f64122c1SDave Airlie crtc->mode.hdisplay, crtc->mode.vdisplay, 680f64122c1SDave Airlie crtc->x, crtc->y, crtc->enabled); 681f64122c1SDave Airlie } 682f64122c1SDave Airlie 683f64122c1SDave Airlie static void qxl_crtc_commit(struct drm_crtc *crtc) 684f64122c1SDave Airlie { 685f64122c1SDave Airlie DRM_DEBUG("\n"); 686f64122c1SDave Airlie } 687f64122c1SDave Airlie 68807f8d9bdSDave Airlie static void qxl_crtc_disable(struct drm_crtc *crtc) 68907f8d9bdSDave Airlie { 69007f8d9bdSDave Airlie struct qxl_crtc *qcrtc = to_qxl_crtc(crtc); 69107f8d9bdSDave Airlie struct drm_device *dev = crtc->dev; 69207f8d9bdSDave Airlie struct qxl_device *qdev = dev->dev_private; 693f4510a27SMatt Roper if (crtc->primary->fb) { 694f4510a27SMatt Roper struct qxl_framebuffer *qfb = to_qxl_framebuffer(crtc->primary->fb); 69507f8d9bdSDave Airlie struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj); 69607f8d9bdSDave Airlie int ret; 69707f8d9bdSDave Airlie ret = qxl_bo_reserve(bo, false); 69807f8d9bdSDave Airlie qxl_bo_unpin(bo); 69907f8d9bdSDave Airlie qxl_bo_unreserve(bo); 700f4510a27SMatt Roper crtc->primary->fb = NULL; 70107f8d9bdSDave Airlie } 70207f8d9bdSDave Airlie 70307f8d9bdSDave Airlie qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, 0, 0, 0); 70407f8d9bdSDave Airlie 70507f8d9bdSDave Airlie qxl_send_monitors_config(qdev); 70607f8d9bdSDave Airlie } 70707f8d9bdSDave Airlie 708f64122c1SDave Airlie static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = { 709f64122c1SDave Airlie .dpms = qxl_crtc_dpms, 71007f8d9bdSDave Airlie .disable = qxl_crtc_disable, 711f64122c1SDave Airlie .mode_fixup = qxl_crtc_mode_fixup, 712f64122c1SDave Airlie .mode_set = qxl_crtc_mode_set, 713f64122c1SDave Airlie .prepare = qxl_crtc_prepare, 714f64122c1SDave Airlie .commit = qxl_crtc_commit, 715f64122c1SDave Airlie }; 716f64122c1SDave Airlie 71707f8d9bdSDave Airlie static int qdev_crtc_init(struct drm_device *dev, int crtc_id) 718f64122c1SDave Airlie { 719f64122c1SDave Airlie struct qxl_crtc *qxl_crtc; 720f64122c1SDave Airlie 721f64122c1SDave Airlie qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL); 722f64122c1SDave Airlie if (!qxl_crtc) 723f64122c1SDave Airlie return -ENOMEM; 724f64122c1SDave Airlie 725f64122c1SDave Airlie drm_crtc_init(dev, &qxl_crtc->base, &qxl_crtc_funcs); 72607f8d9bdSDave Airlie qxl_crtc->index = crtc_id; 727f64122c1SDave Airlie drm_mode_crtc_set_gamma_size(&qxl_crtc->base, 256); 728f64122c1SDave Airlie drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs); 729f64122c1SDave Airlie return 0; 730f64122c1SDave Airlie } 731f64122c1SDave Airlie 732f64122c1SDave Airlie static void qxl_enc_dpms(struct drm_encoder *encoder, int mode) 733f64122c1SDave Airlie { 734f64122c1SDave Airlie DRM_DEBUG("\n"); 735f64122c1SDave Airlie } 736f64122c1SDave Airlie 737f64122c1SDave Airlie static bool qxl_enc_mode_fixup(struct drm_encoder *encoder, 738f64122c1SDave Airlie const struct drm_display_mode *mode, 739f64122c1SDave Airlie struct drm_display_mode *adjusted_mode) 740f64122c1SDave Airlie { 741f64122c1SDave Airlie DRM_DEBUG("\n"); 742f64122c1SDave Airlie return true; 743f64122c1SDave Airlie } 744f64122c1SDave Airlie 745f64122c1SDave Airlie static void qxl_enc_prepare(struct drm_encoder *encoder) 746f64122c1SDave Airlie { 747f64122c1SDave Airlie DRM_DEBUG("\n"); 748f64122c1SDave Airlie } 749f64122c1SDave Airlie 750f64122c1SDave Airlie static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev, 751f64122c1SDave Airlie struct drm_encoder *encoder) 752f64122c1SDave Airlie { 753f64122c1SDave Airlie int i; 75407f8d9bdSDave Airlie struct qxl_output *output = drm_encoder_to_qxl_output(encoder); 755f64122c1SDave Airlie struct qxl_head *head; 756f64122c1SDave Airlie struct drm_display_mode *mode; 757f64122c1SDave Airlie 758f64122c1SDave Airlie BUG_ON(!encoder); 759f64122c1SDave Airlie /* TODO: ugly, do better */ 76007f8d9bdSDave Airlie i = output->index; 761f64122c1SDave Airlie if (!qdev->monitors_config || 762f64122c1SDave Airlie qdev->monitors_config->max_allowed <= i) { 763f64122c1SDave Airlie DRM_ERROR( 764f64122c1SDave Airlie "head number too large or missing monitors config: %p, %d", 765f64122c1SDave Airlie qdev->monitors_config, 766f64122c1SDave Airlie qdev->monitors_config ? 767f64122c1SDave Airlie qdev->monitors_config->max_allowed : -1); 768f64122c1SDave Airlie return; 769f64122c1SDave Airlie } 770f64122c1SDave Airlie if (!encoder->crtc) { 771f64122c1SDave Airlie DRM_ERROR("missing crtc on encoder %p\n", encoder); 772f64122c1SDave Airlie return; 773f64122c1SDave Airlie } 774f64122c1SDave Airlie if (i != 0) 775f64122c1SDave Airlie DRM_DEBUG("missing for multiple monitors: no head holes\n"); 776f64122c1SDave Airlie head = &qdev->monitors_config->heads[i]; 777f64122c1SDave Airlie head->id = i; 778f64122c1SDave Airlie if (encoder->crtc->enabled) { 779f64122c1SDave Airlie mode = &encoder->crtc->mode; 780f64122c1SDave Airlie head->width = mode->hdisplay; 781f64122c1SDave Airlie head->height = mode->vdisplay; 782f64122c1SDave Airlie head->x = encoder->crtc->x; 783f64122c1SDave Airlie head->y = encoder->crtc->y; 784f64122c1SDave Airlie if (qdev->monitors_config->count < i + 1) 785f64122c1SDave Airlie qdev->monitors_config->count = i + 1; 786f64122c1SDave Airlie } else { 787f64122c1SDave Airlie head->width = 0; 788f64122c1SDave Airlie head->height = 0; 789f64122c1SDave Airlie head->x = 0; 790f64122c1SDave Airlie head->y = 0; 791f64122c1SDave Airlie } 79207f8d9bdSDave Airlie DRM_DEBUG_KMS("setting head %d to +%d+%d %dx%d out of %d\n", 79307f8d9bdSDave Airlie i, head->x, head->y, head->width, head->height, qdev->monitors_config->count); 794f64122c1SDave Airlie head->flags = 0; 795f64122c1SDave Airlie /* TODO - somewhere else to call this for multiple monitors 796f64122c1SDave Airlie * (config_commit?) */ 797f64122c1SDave Airlie qxl_send_monitors_config(qdev); 798f64122c1SDave Airlie } 799f64122c1SDave Airlie 800f64122c1SDave Airlie static void qxl_enc_commit(struct drm_encoder *encoder) 801f64122c1SDave Airlie { 802f64122c1SDave Airlie struct qxl_device *qdev = encoder->dev->dev_private; 803f64122c1SDave Airlie 804f64122c1SDave Airlie qxl_write_monitors_config_for_encoder(qdev, encoder); 805f64122c1SDave Airlie DRM_DEBUG("\n"); 806f64122c1SDave Airlie } 807f64122c1SDave Airlie 808f64122c1SDave Airlie static void qxl_enc_mode_set(struct drm_encoder *encoder, 809f64122c1SDave Airlie struct drm_display_mode *mode, 810f64122c1SDave Airlie struct drm_display_mode *adjusted_mode) 811f64122c1SDave Airlie { 812f64122c1SDave Airlie DRM_DEBUG("\n"); 813f64122c1SDave Airlie } 814f64122c1SDave Airlie 815f64122c1SDave Airlie static int qxl_conn_get_modes(struct drm_connector *connector) 816f64122c1SDave Airlie { 817f64122c1SDave Airlie int ret = 0; 818f64122c1SDave Airlie struct qxl_device *qdev = connector->dev->dev_private; 819b0807423SMarc-André Lureau unsigned pwidth = 1024; 820b0807423SMarc-André Lureau unsigned pheight = 768; 821f64122c1SDave Airlie 822f64122c1SDave Airlie DRM_DEBUG_KMS("monitors_config=%p\n", qdev->monitors_config); 823f64122c1SDave Airlie /* TODO: what should we do here? only show the configured modes for the 824f64122c1SDave Airlie * device, or allow the full list, or both? */ 825f64122c1SDave Airlie if (qdev->monitors_config && qdev->monitors_config->count) { 826b0807423SMarc-André Lureau ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight); 827f64122c1SDave Airlie if (ret < 0) 828f64122c1SDave Airlie return ret; 829f64122c1SDave Airlie } 830b0807423SMarc-André Lureau ret += qxl_add_common_modes(connector, pwidth, pheight); 831f64122c1SDave Airlie return ret; 832f64122c1SDave Airlie } 833f64122c1SDave Airlie 834f64122c1SDave Airlie static int qxl_conn_mode_valid(struct drm_connector *connector, 835f64122c1SDave Airlie struct drm_display_mode *mode) 836f64122c1SDave Airlie { 837bd3e1c7cSJonathon Jongsma struct drm_device *ddev = connector->dev; 838bd3e1c7cSJonathon Jongsma struct qxl_device *qdev = ddev->dev_private; 839bd3e1c7cSJonathon Jongsma int i; 840bd3e1c7cSJonathon Jongsma 841f64122c1SDave Airlie /* TODO: is this called for user defined modes? (xrandr --add-mode) 842f64122c1SDave Airlie * TODO: check that the mode fits in the framebuffer */ 843bd3e1c7cSJonathon Jongsma 844bd3e1c7cSJonathon Jongsma if(qdev->monitors_config_width == mode->hdisplay && 845bd3e1c7cSJonathon Jongsma qdev->monitors_config_height == mode->vdisplay) 846f64122c1SDave Airlie return MODE_OK; 847bd3e1c7cSJonathon Jongsma 848bd3e1c7cSJonathon Jongsma for (i = 0; i < ARRAY_SIZE(common_modes); i++) { 849bd3e1c7cSJonathon Jongsma if (common_modes[i].w == mode->hdisplay && common_modes[i].h == mode->vdisplay) 850bd3e1c7cSJonathon Jongsma return MODE_OK; 851bd3e1c7cSJonathon Jongsma } 852bd3e1c7cSJonathon Jongsma return MODE_BAD; 853f64122c1SDave Airlie } 854f64122c1SDave Airlie 8556d01f1f5SDave Airlie static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector) 856f64122c1SDave Airlie { 857f64122c1SDave Airlie struct qxl_output *qxl_output = 858f64122c1SDave Airlie drm_connector_to_qxl_output(connector); 859f64122c1SDave Airlie 860f64122c1SDave Airlie DRM_DEBUG("\n"); 861f64122c1SDave Airlie return &qxl_output->enc; 862f64122c1SDave Airlie } 863f64122c1SDave Airlie 864f64122c1SDave Airlie 865f64122c1SDave Airlie static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = { 866f64122c1SDave Airlie .dpms = qxl_enc_dpms, 867f64122c1SDave Airlie .mode_fixup = qxl_enc_mode_fixup, 868f64122c1SDave Airlie .prepare = qxl_enc_prepare, 869f64122c1SDave Airlie .mode_set = qxl_enc_mode_set, 870f64122c1SDave Airlie .commit = qxl_enc_commit, 871f64122c1SDave Airlie }; 872f64122c1SDave Airlie 873f64122c1SDave Airlie static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = { 874f64122c1SDave Airlie .get_modes = qxl_conn_get_modes, 875f64122c1SDave Airlie .mode_valid = qxl_conn_mode_valid, 876f64122c1SDave Airlie .best_encoder = qxl_best_encoder, 877f64122c1SDave Airlie }; 878f64122c1SDave Airlie 879f64122c1SDave Airlie static enum drm_connector_status qxl_conn_detect( 880f64122c1SDave Airlie struct drm_connector *connector, 881f64122c1SDave Airlie bool force) 882f64122c1SDave Airlie { 883f64122c1SDave Airlie struct qxl_output *output = 884f64122c1SDave Airlie drm_connector_to_qxl_output(connector); 885f64122c1SDave Airlie struct drm_device *ddev = connector->dev; 886f64122c1SDave Airlie struct qxl_device *qdev = ddev->dev_private; 88769e5d3f8SDave Airlie bool connected = false; 888f64122c1SDave Airlie 889f64122c1SDave Airlie /* The first monitor is always connected */ 89069e5d3f8SDave Airlie if (!qdev->client_monitors_config) { 89169e5d3f8SDave Airlie if (output->index == 0) 89269e5d3f8SDave Airlie connected = true; 89369e5d3f8SDave Airlie } else 89469e5d3f8SDave Airlie connected = qdev->client_monitors_config->count > output->index && 89569e5d3f8SDave Airlie qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]); 896f64122c1SDave Airlie 8975cab51cbSMarc-André Lureau DRM_DEBUG("#%d connected: %d\n", output->index, connected); 8985cab51cbSMarc-André Lureau if (!connected) 8995cab51cbSMarc-André Lureau qxl_monitors_config_set(qdev, output->index, 0, 0, 0, 0, 0); 9005cab51cbSMarc-André Lureau 901f64122c1SDave Airlie return connected ? connector_status_connected 902f64122c1SDave Airlie : connector_status_disconnected; 903f64122c1SDave Airlie } 904f64122c1SDave Airlie 905f64122c1SDave Airlie static int qxl_conn_set_property(struct drm_connector *connector, 906f64122c1SDave Airlie struct drm_property *property, 907f64122c1SDave Airlie uint64_t value) 908f64122c1SDave Airlie { 909f64122c1SDave Airlie DRM_DEBUG("\n"); 910f64122c1SDave Airlie return 0; 911f64122c1SDave Airlie } 912f64122c1SDave Airlie 913f64122c1SDave Airlie static void qxl_conn_destroy(struct drm_connector *connector) 914f64122c1SDave Airlie { 915f64122c1SDave Airlie struct qxl_output *qxl_output = 916f64122c1SDave Airlie drm_connector_to_qxl_output(connector); 917f64122c1SDave Airlie 91834ea3d38SThomas Wood drm_connector_unregister(connector); 919f64122c1SDave Airlie drm_connector_cleanup(connector); 920f64122c1SDave Airlie kfree(qxl_output); 921f64122c1SDave Airlie } 922f64122c1SDave Airlie 923f64122c1SDave Airlie static const struct drm_connector_funcs qxl_connector_funcs = { 924f64122c1SDave Airlie .dpms = drm_helper_connector_dpms, 925f64122c1SDave Airlie .detect = qxl_conn_detect, 926b87577b7SDave Airlie .fill_modes = drm_helper_probe_single_connector_modes_nomerge, 927f64122c1SDave Airlie .set_property = qxl_conn_set_property, 928f64122c1SDave Airlie .destroy = qxl_conn_destroy, 929f64122c1SDave Airlie }; 930f64122c1SDave Airlie 931f64122c1SDave Airlie static void qxl_enc_destroy(struct drm_encoder *encoder) 932f64122c1SDave Airlie { 933f64122c1SDave Airlie drm_encoder_cleanup(encoder); 934f64122c1SDave Airlie } 935f64122c1SDave Airlie 936f64122c1SDave Airlie static const struct drm_encoder_funcs qxl_enc_funcs = { 937f64122c1SDave Airlie .destroy = qxl_enc_destroy, 938f64122c1SDave Airlie }; 939f64122c1SDave Airlie 9404695b039SDave Airlie static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev) 9414695b039SDave Airlie { 9424695b039SDave Airlie if (qdev->hotplug_mode_update_property) 9434695b039SDave Airlie return 0; 9444695b039SDave Airlie 9454695b039SDave Airlie qdev->hotplug_mode_update_property = 9464695b039SDave Airlie drm_property_create_range(qdev->ddev, DRM_MODE_PROP_IMMUTABLE, 9474695b039SDave Airlie "hotplug_mode_update", 0, 1); 9484695b039SDave Airlie 9494695b039SDave Airlie return 0; 9504695b039SDave Airlie } 9514695b039SDave Airlie 9526d01f1f5SDave Airlie static int qdev_output_init(struct drm_device *dev, int num_output) 953f64122c1SDave Airlie { 9544695b039SDave Airlie struct qxl_device *qdev = dev->dev_private; 955f64122c1SDave Airlie struct qxl_output *qxl_output; 956f64122c1SDave Airlie struct drm_connector *connector; 957f64122c1SDave Airlie struct drm_encoder *encoder; 958f64122c1SDave Airlie 959f64122c1SDave Airlie qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL); 960f64122c1SDave Airlie if (!qxl_output) 961f64122c1SDave Airlie return -ENOMEM; 962f64122c1SDave Airlie 963f64122c1SDave Airlie qxl_output->index = num_output; 964f64122c1SDave Airlie 965f64122c1SDave Airlie connector = &qxl_output->base; 966f64122c1SDave Airlie encoder = &qxl_output->enc; 967f64122c1SDave Airlie drm_connector_init(dev, &qxl_output->base, 968f64122c1SDave Airlie &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL); 969f64122c1SDave Airlie 970f64122c1SDave Airlie drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs, 971*13a3d91fSVille Syrjälä DRM_MODE_ENCODER_VIRTUAL, NULL); 972f64122c1SDave Airlie 9735ff91e44SDave Airlie /* we get HPD via client monitors config */ 9745ff91e44SDave Airlie connector->polled = DRM_CONNECTOR_POLL_HPD; 975f64122c1SDave Airlie encoder->possible_crtcs = 1 << num_output; 976f64122c1SDave Airlie drm_mode_connector_attach_encoder(&qxl_output->base, 977f64122c1SDave Airlie &qxl_output->enc); 978f64122c1SDave Airlie drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs); 979f64122c1SDave Airlie drm_connector_helper_add(connector, &qxl_connector_helper_funcs); 980f64122c1SDave Airlie 9814695b039SDave Airlie drm_object_attach_property(&connector->base, 9824695b039SDave Airlie qdev->hotplug_mode_update_property, 0); 9837dea0941SDave Airlie drm_object_attach_property(&connector->base, 9847dea0941SDave Airlie dev->mode_config.suggested_x_property, 0); 9857dea0941SDave Airlie drm_object_attach_property(&connector->base, 9867dea0941SDave Airlie dev->mode_config.suggested_y_property, 0); 98734ea3d38SThomas Wood drm_connector_register(connector); 988f64122c1SDave Airlie return 0; 989f64122c1SDave Airlie } 990f64122c1SDave Airlie 991f64122c1SDave Airlie static struct drm_framebuffer * 992f64122c1SDave Airlie qxl_user_framebuffer_create(struct drm_device *dev, 993f64122c1SDave Airlie struct drm_file *file_priv, 9941eb83451SVille Syrjälä const struct drm_mode_fb_cmd2 *mode_cmd) 995f64122c1SDave Airlie { 996f64122c1SDave Airlie struct drm_gem_object *obj; 997f64122c1SDave Airlie struct qxl_framebuffer *qxl_fb; 998f64122c1SDave Airlie int ret; 999f64122c1SDave Airlie 1000f64122c1SDave Airlie obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]); 1001f64122c1SDave Airlie 1002f64122c1SDave Airlie qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL); 1003f64122c1SDave Airlie if (qxl_fb == NULL) 1004f64122c1SDave Airlie return NULL; 1005f64122c1SDave Airlie 1006f64122c1SDave Airlie ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj); 1007f64122c1SDave Airlie if (ret) { 1008f64122c1SDave Airlie kfree(qxl_fb); 1009f64122c1SDave Airlie drm_gem_object_unreference_unlocked(obj); 1010f64122c1SDave Airlie return NULL; 1011f64122c1SDave Airlie } 1012f64122c1SDave Airlie 1013f64122c1SDave Airlie return &qxl_fb->base; 1014f64122c1SDave Airlie } 1015f64122c1SDave Airlie 1016f64122c1SDave Airlie static const struct drm_mode_config_funcs qxl_mode_funcs = { 1017f64122c1SDave Airlie .fb_create = qxl_user_framebuffer_create, 1018f64122c1SDave Airlie }; 1019f64122c1SDave Airlie 10202bd6ce84SDave Airlie int qxl_create_monitors_object(struct qxl_device *qdev) 1021f64122c1SDave Airlie { 1022f64122c1SDave Airlie int ret; 1023f64122c1SDave Airlie struct drm_gem_object *gobj; 102407f8d9bdSDave Airlie int max_allowed = qxl_num_crtc; 1025f64122c1SDave Airlie int monitors_config_size = sizeof(struct qxl_monitors_config) + 1026f64122c1SDave Airlie max_allowed * sizeof(struct qxl_head); 1027f64122c1SDave Airlie 1028f64122c1SDave Airlie ret = qxl_gem_object_create(qdev, monitors_config_size, 0, 1029f64122c1SDave Airlie QXL_GEM_DOMAIN_VRAM, 1030f64122c1SDave Airlie false, false, NULL, &gobj); 1031f64122c1SDave Airlie if (ret) { 1032f64122c1SDave Airlie DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret); 1033f64122c1SDave Airlie return -ENOMEM; 1034f64122c1SDave Airlie } 1035f64122c1SDave Airlie qdev->monitors_config_bo = gem_to_qxl_bo(gobj); 10362bd6ce84SDave Airlie 10372bd6ce84SDave Airlie ret = qxl_bo_reserve(qdev->monitors_config_bo, false); 10382bd6ce84SDave Airlie if (ret) 10392bd6ce84SDave Airlie return ret; 10402bd6ce84SDave Airlie 10412bd6ce84SDave Airlie ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL); 10422bd6ce84SDave Airlie if (ret) { 10432bd6ce84SDave Airlie qxl_bo_unreserve(qdev->monitors_config_bo); 10442bd6ce84SDave Airlie return ret; 10452bd6ce84SDave Airlie } 10462bd6ce84SDave Airlie 10472bd6ce84SDave Airlie qxl_bo_unreserve(qdev->monitors_config_bo); 10482bd6ce84SDave Airlie 1049f64122c1SDave Airlie qxl_bo_kmap(qdev->monitors_config_bo, NULL); 10502bd6ce84SDave Airlie 1051f64122c1SDave Airlie qdev->monitors_config = qdev->monitors_config_bo->kptr; 1052f64122c1SDave Airlie qdev->ram_header->monitors_config = 1053f64122c1SDave Airlie qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0); 1054f64122c1SDave Airlie 1055f64122c1SDave Airlie memset(qdev->monitors_config, 0, monitors_config_size); 1056f64122c1SDave Airlie qdev->monitors_config->max_allowed = max_allowed; 10572bd6ce84SDave Airlie return 0; 10582bd6ce84SDave Airlie } 10592bd6ce84SDave Airlie 10602bd6ce84SDave Airlie int qxl_destroy_monitors_object(struct qxl_device *qdev) 10612bd6ce84SDave Airlie { 10622bd6ce84SDave Airlie int ret; 10632bd6ce84SDave Airlie 10642bd6ce84SDave Airlie qdev->monitors_config = NULL; 10652bd6ce84SDave Airlie qdev->ram_header->monitors_config = 0; 10662bd6ce84SDave Airlie 10672bd6ce84SDave Airlie qxl_bo_kunmap(qdev->monitors_config_bo); 10682bd6ce84SDave Airlie ret = qxl_bo_reserve(qdev->monitors_config_bo, false); 10692bd6ce84SDave Airlie if (ret) 10702bd6ce84SDave Airlie return ret; 10712bd6ce84SDave Airlie 10722bd6ce84SDave Airlie qxl_bo_unpin(qdev->monitors_config_bo); 10732bd6ce84SDave Airlie qxl_bo_unreserve(qdev->monitors_config_bo); 10742bd6ce84SDave Airlie 10752bd6ce84SDave Airlie qxl_bo_unref(&qdev->monitors_config_bo); 10762bd6ce84SDave Airlie return 0; 10772bd6ce84SDave Airlie } 10782bd6ce84SDave Airlie 10792bd6ce84SDave Airlie int qxl_modeset_init(struct qxl_device *qdev) 10802bd6ce84SDave Airlie { 10812bd6ce84SDave Airlie int i; 10822bd6ce84SDave Airlie int ret; 10832bd6ce84SDave Airlie 10842bd6ce84SDave Airlie drm_mode_config_init(qdev->ddev); 10852bd6ce84SDave Airlie 10862bd6ce84SDave Airlie ret = qxl_create_monitors_object(qdev); 10872bd6ce84SDave Airlie if (ret) 10882bd6ce84SDave Airlie return ret; 1089f64122c1SDave Airlie 1090f64122c1SDave Airlie qdev->ddev->mode_config.funcs = (void *)&qxl_mode_funcs; 1091f64122c1SDave Airlie 1092f64122c1SDave Airlie /* modes will be validated against the framebuffer size */ 1093f64122c1SDave Airlie qdev->ddev->mode_config.min_width = 320; 1094f64122c1SDave Airlie qdev->ddev->mode_config.min_height = 200; 1095f64122c1SDave Airlie qdev->ddev->mode_config.max_width = 8192; 1096f64122c1SDave Airlie qdev->ddev->mode_config.max_height = 8192; 1097f64122c1SDave Airlie 1098f64122c1SDave Airlie qdev->ddev->mode_config.fb_base = qdev->vram_base; 10994695b039SDave Airlie 11007dea0941SDave Airlie drm_mode_create_suggested_offset_properties(qdev->ddev); 11014695b039SDave Airlie qxl_mode_create_hotplug_mode_update_property(qdev); 11024695b039SDave Airlie 110307f8d9bdSDave Airlie for (i = 0 ; i < qxl_num_crtc; ++i) { 1104f64122c1SDave Airlie qdev_crtc_init(qdev->ddev, i); 1105f64122c1SDave Airlie qdev_output_init(qdev->ddev, i); 1106f64122c1SDave Airlie } 1107f64122c1SDave Airlie 1108f64122c1SDave Airlie qdev->mode_info.mode_config_initialized = true; 1109f64122c1SDave Airlie 1110f64122c1SDave Airlie /* primary surface must be created by this point, to allow 1111f64122c1SDave Airlie * issuing command queue commands and having them read by 1112f64122c1SDave Airlie * spice server. */ 1113f64122c1SDave Airlie qxl_fbdev_init(qdev); 1114f64122c1SDave Airlie return 0; 1115f64122c1SDave Airlie } 1116f64122c1SDave Airlie 1117f64122c1SDave Airlie void qxl_modeset_fini(struct qxl_device *qdev) 1118f64122c1SDave Airlie { 1119f64122c1SDave Airlie qxl_fbdev_fini(qdev); 11202bd6ce84SDave Airlie 11212bd6ce84SDave Airlie qxl_destroy_monitors_object(qdev); 1122f64122c1SDave Airlie if (qdev->mode_info.mode_config_initialized) { 1123f64122c1SDave Airlie drm_mode_config_cleanup(qdev->ddev); 1124f64122c1SDave Airlie qdev->mode_info.mode_config_initialized = false; 1125f64122c1SDave Airlie } 1126f64122c1SDave Airlie } 1127