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