b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [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 | #include <linux/crc32.h> |
| 27 | #include <linux/delay.h> |
| 28 | |
| 29 | #include <drm/drm_atomic.h> |
| 30 | #include <drm/drm_atomic_helper.h> |
| 31 | #include <drm/drm_gem_framebuffer_helper.h> |
| 32 | #include <drm/drm_plane_helper.h> |
| 33 | #include <drm/drm_probe_helper.h> |
| 34 | #include <drm/drm_vblank.h> |
| 35 | |
| 36 | #include "qxl_drv.h" |
| 37 | #include "qxl_object.h" |
| 38 | |
| 39 | static bool qxl_head_enabled(struct qxl_head *head) |
| 40 | { |
| 41 | return head->width && head->height; |
| 42 | } |
| 43 | |
| 44 | static int qxl_alloc_client_monitors_config(struct qxl_device *qdev, |
| 45 | unsigned int count) |
| 46 | { |
| 47 | if (qdev->client_monitors_config && |
| 48 | count > qdev->client_monitors_config->count) { |
| 49 | kfree(qdev->client_monitors_config); |
| 50 | qdev->client_monitors_config = NULL; |
| 51 | } |
| 52 | if (!qdev->client_monitors_config) { |
| 53 | qdev->client_monitors_config = kzalloc( |
| 54 | struct_size(qdev->client_monitors_config, |
| 55 | heads, count), GFP_KERNEL); |
| 56 | if (!qdev->client_monitors_config) |
| 57 | return -ENOMEM; |
| 58 | } |
| 59 | qdev->client_monitors_config->count = count; |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | enum { |
| 64 | MONITORS_CONFIG_MODIFIED, |
| 65 | MONITORS_CONFIG_UNCHANGED, |
| 66 | MONITORS_CONFIG_BAD_CRC, |
| 67 | MONITORS_CONFIG_ERROR, |
| 68 | }; |
| 69 | |
| 70 | static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev) |
| 71 | { |
| 72 | int i; |
| 73 | int num_monitors; |
| 74 | uint32_t crc; |
| 75 | int status = MONITORS_CONFIG_UNCHANGED; |
| 76 | |
| 77 | num_monitors = qdev->rom->client_monitors_config.count; |
| 78 | crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config, |
| 79 | sizeof(qdev->rom->client_monitors_config)); |
| 80 | if (crc != qdev->rom->client_monitors_config_crc) |
| 81 | return MONITORS_CONFIG_BAD_CRC; |
| 82 | if (!num_monitors) { |
| 83 | DRM_DEBUG_KMS("no client monitors configured\n"); |
| 84 | return status; |
| 85 | } |
| 86 | if (num_monitors > qxl_num_crtc) { |
| 87 | DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n", |
| 88 | qxl_num_crtc, num_monitors); |
| 89 | num_monitors = qxl_num_crtc; |
| 90 | } else { |
| 91 | num_monitors = qdev->rom->client_monitors_config.count; |
| 92 | } |
| 93 | if (qdev->client_monitors_config |
| 94 | && (num_monitors != qdev->client_monitors_config->count)) { |
| 95 | status = MONITORS_CONFIG_MODIFIED; |
| 96 | } |
| 97 | if (qxl_alloc_client_monitors_config(qdev, num_monitors)) { |
| 98 | status = MONITORS_CONFIG_ERROR; |
| 99 | return status; |
| 100 | } |
| 101 | /* we copy max from the client but it isn't used */ |
| 102 | qdev->client_monitors_config->max_allowed = qxl_num_crtc; |
| 103 | for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) { |
| 104 | struct qxl_urect *c_rect = |
| 105 | &qdev->rom->client_monitors_config.heads[i]; |
| 106 | struct qxl_head *client_head = |
| 107 | &qdev->client_monitors_config->heads[i]; |
| 108 | if (client_head->x != c_rect->left) { |
| 109 | client_head->x = c_rect->left; |
| 110 | status = MONITORS_CONFIG_MODIFIED; |
| 111 | } |
| 112 | if (client_head->y != c_rect->top) { |
| 113 | client_head->y = c_rect->top; |
| 114 | status = MONITORS_CONFIG_MODIFIED; |
| 115 | } |
| 116 | if (client_head->width != c_rect->right - c_rect->left) { |
| 117 | client_head->width = c_rect->right - c_rect->left; |
| 118 | status = MONITORS_CONFIG_MODIFIED; |
| 119 | } |
| 120 | if (client_head->height != c_rect->bottom - c_rect->top) { |
| 121 | client_head->height = c_rect->bottom - c_rect->top; |
| 122 | status = MONITORS_CONFIG_MODIFIED; |
| 123 | } |
| 124 | if (client_head->surface_id != 0) { |
| 125 | client_head->surface_id = 0; |
| 126 | status = MONITORS_CONFIG_MODIFIED; |
| 127 | } |
| 128 | if (client_head->id != i) { |
| 129 | client_head->id = i; |
| 130 | status = MONITORS_CONFIG_MODIFIED; |
| 131 | } |
| 132 | if (client_head->flags != 0) { |
| 133 | client_head->flags = 0; |
| 134 | status = MONITORS_CONFIG_MODIFIED; |
| 135 | } |
| 136 | DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height, |
| 137 | client_head->x, client_head->y); |
| 138 | } |
| 139 | |
| 140 | return status; |
| 141 | } |
| 142 | |
| 143 | static void qxl_update_offset_props(struct qxl_device *qdev) |
| 144 | { |
| 145 | struct drm_device *dev = &qdev->ddev; |
| 146 | struct drm_connector *connector; |
| 147 | struct qxl_output *output; |
| 148 | struct qxl_head *head; |
| 149 | |
| 150 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { |
| 151 | output = drm_connector_to_qxl_output(connector); |
| 152 | |
| 153 | head = &qdev->client_monitors_config->heads[output->index]; |
| 154 | |
| 155 | drm_object_property_set_value(&connector->base, |
| 156 | dev->mode_config.suggested_x_property, head->x); |
| 157 | drm_object_property_set_value(&connector->base, |
| 158 | dev->mode_config.suggested_y_property, head->y); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | void qxl_display_read_client_monitors_config(struct qxl_device *qdev) |
| 163 | { |
| 164 | struct drm_device *dev = &qdev->ddev; |
| 165 | int status, retries; |
| 166 | |
| 167 | for (retries = 0; retries < 10; retries++) { |
| 168 | status = qxl_display_copy_rom_client_monitors_config(qdev); |
| 169 | if (status != MONITORS_CONFIG_BAD_CRC) |
| 170 | break; |
| 171 | udelay(5); |
| 172 | } |
| 173 | if (status == MONITORS_CONFIG_ERROR) { |
| 174 | DRM_DEBUG_KMS("ignoring client monitors config: error"); |
| 175 | return; |
| 176 | } |
| 177 | if (status == MONITORS_CONFIG_BAD_CRC) { |
| 178 | DRM_DEBUG_KMS("ignoring client monitors config: bad crc"); |
| 179 | return; |
| 180 | } |
| 181 | if (status == MONITORS_CONFIG_UNCHANGED) { |
| 182 | DRM_DEBUG_KMS("ignoring client monitors config: unchanged"); |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | drm_modeset_lock_all(dev); |
| 187 | qxl_update_offset_props(qdev); |
| 188 | drm_modeset_unlock_all(dev); |
| 189 | if (!drm_helper_hpd_irq_event(dev)) { |
| 190 | /* notify that the monitor configuration changed, to |
| 191 | adjust at the arbitrary resolution */ |
| 192 | drm_kms_helper_hotplug_event(dev); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | static int qxl_check_mode(struct qxl_device *qdev, |
| 197 | unsigned int width, |
| 198 | unsigned int height) |
| 199 | { |
| 200 | unsigned int stride; |
| 201 | unsigned int size; |
| 202 | |
| 203 | if (check_mul_overflow(width, 4u, &stride)) |
| 204 | return -EINVAL; |
| 205 | if (check_mul_overflow(stride, height, &size)) |
| 206 | return -EINVAL; |
| 207 | if (size > qdev->vram_size) |
| 208 | return -ENOMEM; |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static int qxl_check_framebuffer(struct qxl_device *qdev, |
| 213 | struct qxl_bo *bo) |
| 214 | { |
| 215 | return qxl_check_mode(qdev, bo->surf.width, bo->surf.height); |
| 216 | } |
| 217 | |
| 218 | static int qxl_add_mode(struct drm_connector *connector, |
| 219 | unsigned int width, |
| 220 | unsigned int height, |
| 221 | bool preferred) |
| 222 | { |
| 223 | struct drm_device *dev = connector->dev; |
| 224 | struct qxl_device *qdev = dev->dev_private; |
| 225 | struct drm_display_mode *mode = NULL; |
| 226 | int rc; |
| 227 | |
| 228 | rc = qxl_check_mode(qdev, width, height); |
| 229 | if (rc != 0) |
| 230 | return 0; |
| 231 | |
| 232 | mode = drm_cvt_mode(dev, width, height, 60, false, false, false); |
| 233 | if (!mode) |
| 234 | return 0; |
| 235 | |
| 236 | if (preferred) |
| 237 | mode->type |= DRM_MODE_TYPE_PREFERRED; |
| 238 | mode->hdisplay = width; |
| 239 | mode->vdisplay = height; |
| 240 | drm_mode_set_name(mode); |
| 241 | drm_mode_probed_add(connector, mode); |
| 242 | return 1; |
| 243 | } |
| 244 | |
| 245 | static int qxl_add_monitors_config_modes(struct drm_connector *connector) |
| 246 | { |
| 247 | struct drm_device *dev = connector->dev; |
| 248 | struct qxl_device *qdev = dev->dev_private; |
| 249 | struct qxl_output *output = drm_connector_to_qxl_output(connector); |
| 250 | int h = output->index; |
| 251 | struct qxl_head *head; |
| 252 | |
| 253 | if (!qdev->monitors_config) |
| 254 | return 0; |
| 255 | if (h >= qxl_num_crtc) |
| 256 | return 0; |
| 257 | if (!qdev->client_monitors_config) |
| 258 | return 0; |
| 259 | if (h >= qdev->client_monitors_config->count) |
| 260 | return 0; |
| 261 | |
| 262 | head = &qdev->client_monitors_config->heads[h]; |
| 263 | DRM_DEBUG_KMS("head %d is %dx%d\n", h, head->width, head->height); |
| 264 | |
| 265 | return qxl_add_mode(connector, head->width, head->height, true); |
| 266 | } |
| 267 | |
| 268 | static struct mode_size { |
| 269 | int w; |
| 270 | int h; |
| 271 | } extra_modes[] = { |
| 272 | { 720, 480}, |
| 273 | {1152, 768}, |
| 274 | {1280, 854}, |
| 275 | }; |
| 276 | |
| 277 | static int qxl_add_extra_modes(struct drm_connector *connector) |
| 278 | { |
| 279 | int i, ret = 0; |
| 280 | |
| 281 | for (i = 0; i < ARRAY_SIZE(extra_modes); i++) |
| 282 | ret += qxl_add_mode(connector, |
| 283 | extra_modes[i].w, |
| 284 | extra_modes[i].h, |
| 285 | false); |
| 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | static void qxl_send_monitors_config(struct qxl_device *qdev) |
| 290 | { |
| 291 | int i; |
| 292 | |
| 293 | BUG_ON(!qdev->ram_header->monitors_config); |
| 294 | |
| 295 | if (qdev->monitors_config->count == 0) |
| 296 | return; |
| 297 | |
| 298 | for (i = 0 ; i < qdev->monitors_config->count ; ++i) { |
| 299 | struct qxl_head *head = &qdev->monitors_config->heads[i]; |
| 300 | |
| 301 | if (head->y > 8192 || head->x > 8192 || |
| 302 | head->width > 8192 || head->height > 8192) { |
| 303 | DRM_ERROR("head %d wrong: %dx%d+%d+%d\n", |
| 304 | i, head->width, head->height, |
| 305 | head->x, head->y); |
| 306 | return; |
| 307 | } |
| 308 | } |
| 309 | qxl_io_monitors_config(qdev); |
| 310 | } |
| 311 | |
| 312 | static void qxl_crtc_update_monitors_config(struct drm_crtc *crtc, |
| 313 | const char *reason) |
| 314 | { |
| 315 | struct drm_device *dev = crtc->dev; |
| 316 | struct qxl_device *qdev = dev->dev_private; |
| 317 | struct qxl_crtc *qcrtc = to_qxl_crtc(crtc); |
| 318 | struct qxl_head head; |
| 319 | int oldcount, i = qcrtc->index; |
| 320 | |
| 321 | if (!qdev->primary_bo) { |
| 322 | DRM_DEBUG_KMS("no primary surface, skip (%s)\n", reason); |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | if (!qdev->monitors_config || qxl_num_crtc <= i) |
| 327 | return; |
| 328 | |
| 329 | head.id = i; |
| 330 | head.flags = 0; |
| 331 | head.surface_id = 0; |
| 332 | oldcount = qdev->monitors_config->count; |
| 333 | if (crtc->state->active) { |
| 334 | struct drm_display_mode *mode = &crtc->mode; |
| 335 | |
| 336 | head.width = mode->hdisplay; |
| 337 | head.height = mode->vdisplay; |
| 338 | head.x = crtc->x; |
| 339 | head.y = crtc->y; |
| 340 | if (qdev->monitors_config->count < i + 1) |
| 341 | qdev->monitors_config->count = i + 1; |
| 342 | if (qdev->primary_bo == qdev->dumb_shadow_bo) |
| 343 | head.x += qdev->dumb_heads[i].x; |
| 344 | } else if (i > 0) { |
| 345 | head.width = 0; |
| 346 | head.height = 0; |
| 347 | head.x = 0; |
| 348 | head.y = 0; |
| 349 | if (qdev->monitors_config->count == i + 1) |
| 350 | qdev->monitors_config->count = i; |
| 351 | } else { |
| 352 | DRM_DEBUG_KMS("inactive head 0, skip (%s)\n", reason); |
| 353 | return; |
| 354 | } |
| 355 | |
| 356 | if (head.width == qdev->monitors_config->heads[i].width && |
| 357 | head.height == qdev->monitors_config->heads[i].height && |
| 358 | head.x == qdev->monitors_config->heads[i].x && |
| 359 | head.y == qdev->monitors_config->heads[i].y && |
| 360 | oldcount == qdev->monitors_config->count) |
| 361 | return; |
| 362 | |
| 363 | DRM_DEBUG_KMS("head %d, %dx%d, at +%d+%d, %s (%s)\n", |
| 364 | i, head.width, head.height, head.x, head.y, |
| 365 | crtc->state->active ? "on" : "off", reason); |
| 366 | if (oldcount != qdev->monitors_config->count) |
| 367 | DRM_DEBUG_KMS("active heads %d -> %d (%d total)\n", |
| 368 | oldcount, qdev->monitors_config->count, |
| 369 | qxl_num_crtc); |
| 370 | |
| 371 | qdev->monitors_config->heads[i] = head; |
| 372 | qdev->monitors_config->max_allowed = qxl_num_crtc; |
| 373 | qxl_send_monitors_config(qdev); |
| 374 | } |
| 375 | |
| 376 | static void qxl_crtc_atomic_flush(struct drm_crtc *crtc, |
| 377 | struct drm_crtc_state *old_crtc_state) |
| 378 | { |
| 379 | struct drm_device *dev = crtc->dev; |
| 380 | struct drm_pending_vblank_event *event; |
| 381 | unsigned long flags; |
| 382 | |
| 383 | if (crtc->state && crtc->state->event) { |
| 384 | event = crtc->state->event; |
| 385 | crtc->state->event = NULL; |
| 386 | |
| 387 | spin_lock_irqsave(&dev->event_lock, flags); |
| 388 | drm_crtc_send_vblank_event(crtc, event); |
| 389 | spin_unlock_irqrestore(&dev->event_lock, flags); |
| 390 | } |
| 391 | |
| 392 | qxl_crtc_update_monitors_config(crtc, "flush"); |
| 393 | } |
| 394 | |
| 395 | static void qxl_crtc_destroy(struct drm_crtc *crtc) |
| 396 | { |
| 397 | struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc); |
| 398 | |
| 399 | qxl_bo_unref(&qxl_crtc->cursor_bo); |
| 400 | drm_crtc_cleanup(crtc); |
| 401 | kfree(qxl_crtc); |
| 402 | } |
| 403 | |
| 404 | static const struct drm_crtc_funcs qxl_crtc_funcs = { |
| 405 | .set_config = drm_atomic_helper_set_config, |
| 406 | .destroy = qxl_crtc_destroy, |
| 407 | .page_flip = drm_atomic_helper_page_flip, |
| 408 | .reset = drm_atomic_helper_crtc_reset, |
| 409 | .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, |
| 410 | .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, |
| 411 | }; |
| 412 | |
| 413 | static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb, |
| 414 | struct drm_file *file_priv, |
| 415 | unsigned int flags, unsigned int color, |
| 416 | struct drm_clip_rect *clips, |
| 417 | unsigned int num_clips) |
| 418 | { |
| 419 | /* TODO: vmwgfx where this was cribbed from had locking. Why? */ |
| 420 | struct qxl_device *qdev = fb->dev->dev_private; |
| 421 | struct drm_clip_rect norect; |
| 422 | struct qxl_bo *qobj; |
| 423 | bool is_primary; |
| 424 | int inc = 1; |
| 425 | |
| 426 | drm_modeset_lock_all(fb->dev); |
| 427 | |
| 428 | qobj = gem_to_qxl_bo(fb->obj[0]); |
| 429 | /* if we aren't primary surface ignore this */ |
| 430 | is_primary = qobj->shadow ? qobj->shadow->is_primary : qobj->is_primary; |
| 431 | if (!is_primary) { |
| 432 | drm_modeset_unlock_all(fb->dev); |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | if (!num_clips) { |
| 437 | num_clips = 1; |
| 438 | clips = &norect; |
| 439 | norect.x1 = norect.y1 = 0; |
| 440 | norect.x2 = fb->width; |
| 441 | norect.y2 = fb->height; |
| 442 | } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) { |
| 443 | num_clips /= 2; |
| 444 | inc = 2; /* skip source rects */ |
| 445 | } |
| 446 | |
| 447 | qxl_draw_dirty_fb(qdev, fb, qobj, flags, color, |
| 448 | clips, num_clips, inc, 0); |
| 449 | |
| 450 | drm_modeset_unlock_all(fb->dev); |
| 451 | |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | static const struct drm_framebuffer_funcs qxl_fb_funcs = { |
| 456 | .destroy = drm_gem_fb_destroy, |
| 457 | .dirty = qxl_framebuffer_surface_dirty, |
| 458 | .create_handle = drm_gem_fb_create_handle, |
| 459 | }; |
| 460 | |
| 461 | static void qxl_crtc_atomic_enable(struct drm_crtc *crtc, |
| 462 | struct drm_crtc_state *old_state) |
| 463 | { |
| 464 | qxl_crtc_update_monitors_config(crtc, "enable"); |
| 465 | } |
| 466 | |
| 467 | static void qxl_crtc_atomic_disable(struct drm_crtc *crtc, |
| 468 | struct drm_crtc_state *old_state) |
| 469 | { |
| 470 | qxl_crtc_update_monitors_config(crtc, "disable"); |
| 471 | } |
| 472 | |
| 473 | static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = { |
| 474 | .atomic_flush = qxl_crtc_atomic_flush, |
| 475 | .atomic_enable = qxl_crtc_atomic_enable, |
| 476 | .atomic_disable = qxl_crtc_atomic_disable, |
| 477 | }; |
| 478 | |
| 479 | static int qxl_primary_atomic_check(struct drm_plane *plane, |
| 480 | struct drm_plane_state *state) |
| 481 | { |
| 482 | struct qxl_device *qdev = plane->dev->dev_private; |
| 483 | struct qxl_bo *bo; |
| 484 | |
| 485 | if (!state->crtc || !state->fb) |
| 486 | return 0; |
| 487 | |
| 488 | bo = gem_to_qxl_bo(state->fb->obj[0]); |
| 489 | |
| 490 | return qxl_check_framebuffer(qdev, bo); |
| 491 | } |
| 492 | |
| 493 | static int qxl_primary_apply_cursor(struct drm_plane *plane) |
| 494 | { |
| 495 | struct drm_device *dev = plane->dev; |
| 496 | struct qxl_device *qdev = dev->dev_private; |
| 497 | struct drm_framebuffer *fb = plane->state->fb; |
| 498 | struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc); |
| 499 | struct qxl_cursor_cmd *cmd; |
| 500 | struct qxl_release *release; |
| 501 | int ret = 0; |
| 502 | |
| 503 | if (!qcrtc->cursor_bo) |
| 504 | return 0; |
| 505 | |
| 506 | ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), |
| 507 | QXL_RELEASE_CURSOR_CMD, |
| 508 | &release, NULL); |
| 509 | if (ret) |
| 510 | return ret; |
| 511 | |
| 512 | ret = qxl_release_list_add(release, qcrtc->cursor_bo); |
| 513 | if (ret) |
| 514 | goto out_free_release; |
| 515 | |
| 516 | ret = qxl_release_reserve_list(release, false); |
| 517 | if (ret) |
| 518 | goto out_free_release; |
| 519 | |
| 520 | cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release); |
| 521 | cmd->type = QXL_CURSOR_SET; |
| 522 | cmd->u.set.position.x = plane->state->crtc_x + fb->hot_x; |
| 523 | cmd->u.set.position.y = plane->state->crtc_y + fb->hot_y; |
| 524 | |
| 525 | cmd->u.set.shape = qxl_bo_physical_address(qdev, qcrtc->cursor_bo, 0); |
| 526 | |
| 527 | cmd->u.set.visible = 1; |
| 528 | qxl_release_unmap(qdev, release, &cmd->release_info); |
| 529 | |
| 530 | qxl_release_fence_buffer_objects(release); |
| 531 | qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false); |
| 532 | |
| 533 | return ret; |
| 534 | |
| 535 | out_free_release: |
| 536 | qxl_release_free(qdev, release); |
| 537 | return ret; |
| 538 | } |
| 539 | |
| 540 | static void qxl_primary_atomic_update(struct drm_plane *plane, |
| 541 | struct drm_plane_state *old_state) |
| 542 | { |
| 543 | struct qxl_device *qdev = plane->dev->dev_private; |
| 544 | struct qxl_bo *bo = gem_to_qxl_bo(plane->state->fb->obj[0]); |
| 545 | struct qxl_bo *primary; |
| 546 | struct drm_clip_rect norect = { |
| 547 | .x1 = 0, |
| 548 | .y1 = 0, |
| 549 | .x2 = plane->state->fb->width, |
| 550 | .y2 = plane->state->fb->height |
| 551 | }; |
| 552 | uint32_t dumb_shadow_offset = 0; |
| 553 | |
| 554 | primary = bo->shadow ? bo->shadow : bo; |
| 555 | |
| 556 | if (!primary->is_primary) { |
| 557 | if (qdev->primary_bo) |
| 558 | qxl_io_destroy_primary(qdev); |
| 559 | qxl_io_create_primary(qdev, primary); |
| 560 | qxl_primary_apply_cursor(plane); |
| 561 | } |
| 562 | |
| 563 | if (bo->is_dumb) |
| 564 | dumb_shadow_offset = |
| 565 | qdev->dumb_heads[plane->state->crtc->index].x; |
| 566 | |
| 567 | qxl_draw_dirty_fb(qdev, plane->state->fb, bo, 0, 0, &norect, 1, 1, |
| 568 | dumb_shadow_offset); |
| 569 | } |
| 570 | |
| 571 | static void qxl_primary_atomic_disable(struct drm_plane *plane, |
| 572 | struct drm_plane_state *old_state) |
| 573 | { |
| 574 | struct qxl_device *qdev = plane->dev->dev_private; |
| 575 | |
| 576 | if (old_state->fb) { |
| 577 | struct qxl_bo *bo = gem_to_qxl_bo(old_state->fb->obj[0]); |
| 578 | |
| 579 | if (bo->is_primary) { |
| 580 | qxl_io_destroy_primary(qdev); |
| 581 | bo->is_primary = false; |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | static void qxl_cursor_atomic_update(struct drm_plane *plane, |
| 587 | struct drm_plane_state *old_state) |
| 588 | { |
| 589 | struct drm_device *dev = plane->dev; |
| 590 | struct qxl_device *qdev = dev->dev_private; |
| 591 | struct drm_framebuffer *fb = plane->state->fb; |
| 592 | struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc); |
| 593 | struct qxl_release *release; |
| 594 | struct qxl_cursor_cmd *cmd; |
| 595 | struct qxl_cursor *cursor; |
| 596 | struct drm_gem_object *obj; |
| 597 | struct qxl_bo *cursor_bo = NULL, *user_bo = NULL, *old_cursor_bo = NULL; |
| 598 | int ret; |
| 599 | void *user_ptr; |
| 600 | int size = 64*64*4; |
| 601 | |
| 602 | ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), |
| 603 | QXL_RELEASE_CURSOR_CMD, |
| 604 | &release, NULL); |
| 605 | if (ret) |
| 606 | return; |
| 607 | |
| 608 | if (fb != old_state->fb) { |
| 609 | obj = fb->obj[0]; |
| 610 | user_bo = gem_to_qxl_bo(obj); |
| 611 | |
| 612 | /* pinning is done in the prepare/cleanup framevbuffer */ |
| 613 | ret = qxl_bo_kmap(user_bo, &user_ptr); |
| 614 | if (ret) |
| 615 | goto out_free_release; |
| 616 | |
| 617 | ret = qxl_alloc_bo_reserved(qdev, release, |
| 618 | sizeof(struct qxl_cursor) + size, |
| 619 | &cursor_bo); |
| 620 | if (ret) |
| 621 | goto out_kunmap; |
| 622 | |
| 623 | ret = qxl_bo_pin(cursor_bo); |
| 624 | if (ret) |
| 625 | goto out_free_bo; |
| 626 | |
| 627 | ret = qxl_release_reserve_list(release, true); |
| 628 | if (ret) |
| 629 | goto out_unpin; |
| 630 | |
| 631 | ret = qxl_bo_kmap(cursor_bo, (void **)&cursor); |
| 632 | if (ret) |
| 633 | goto out_backoff; |
| 634 | |
| 635 | cursor->header.unique = 0; |
| 636 | cursor->header.type = SPICE_CURSOR_TYPE_ALPHA; |
| 637 | cursor->header.width = 64; |
| 638 | cursor->header.height = 64; |
| 639 | cursor->header.hot_spot_x = fb->hot_x; |
| 640 | cursor->header.hot_spot_y = fb->hot_y; |
| 641 | cursor->data_size = size; |
| 642 | cursor->chunk.next_chunk = 0; |
| 643 | cursor->chunk.prev_chunk = 0; |
| 644 | cursor->chunk.data_size = size; |
| 645 | memcpy(cursor->chunk.data, user_ptr, size); |
| 646 | qxl_bo_kunmap(cursor_bo); |
| 647 | qxl_bo_kunmap(user_bo); |
| 648 | |
| 649 | cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release); |
| 650 | cmd->u.set.visible = 1; |
| 651 | cmd->u.set.shape = qxl_bo_physical_address(qdev, |
| 652 | cursor_bo, 0); |
| 653 | cmd->type = QXL_CURSOR_SET; |
| 654 | |
| 655 | old_cursor_bo = qcrtc->cursor_bo; |
| 656 | qcrtc->cursor_bo = cursor_bo; |
| 657 | cursor_bo = NULL; |
| 658 | } else { |
| 659 | |
| 660 | ret = qxl_release_reserve_list(release, true); |
| 661 | if (ret) |
| 662 | goto out_free_release; |
| 663 | |
| 664 | cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release); |
| 665 | cmd->type = QXL_CURSOR_MOVE; |
| 666 | } |
| 667 | |
| 668 | cmd->u.position.x = plane->state->crtc_x + fb->hot_x; |
| 669 | cmd->u.position.y = plane->state->crtc_y + fb->hot_y; |
| 670 | |
| 671 | qxl_release_unmap(qdev, release, &cmd->release_info); |
| 672 | qxl_release_fence_buffer_objects(release); |
| 673 | qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false); |
| 674 | |
| 675 | if (old_cursor_bo != NULL) |
| 676 | qxl_bo_unpin(old_cursor_bo); |
| 677 | qxl_bo_unref(&old_cursor_bo); |
| 678 | qxl_bo_unref(&cursor_bo); |
| 679 | |
| 680 | return; |
| 681 | |
| 682 | out_backoff: |
| 683 | qxl_release_backoff_reserve_list(release); |
| 684 | out_unpin: |
| 685 | qxl_bo_unpin(cursor_bo); |
| 686 | out_free_bo: |
| 687 | qxl_bo_unref(&cursor_bo); |
| 688 | out_kunmap: |
| 689 | qxl_bo_kunmap(user_bo); |
| 690 | out_free_release: |
| 691 | qxl_release_free(qdev, release); |
| 692 | return; |
| 693 | |
| 694 | } |
| 695 | |
| 696 | static void qxl_cursor_atomic_disable(struct drm_plane *plane, |
| 697 | struct drm_plane_state *old_state) |
| 698 | { |
| 699 | struct qxl_device *qdev = plane->dev->dev_private; |
| 700 | struct qxl_release *release; |
| 701 | struct qxl_cursor_cmd *cmd; |
| 702 | int ret; |
| 703 | |
| 704 | ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), |
| 705 | QXL_RELEASE_CURSOR_CMD, |
| 706 | &release, NULL); |
| 707 | if (ret) |
| 708 | return; |
| 709 | |
| 710 | ret = qxl_release_reserve_list(release, true); |
| 711 | if (ret) { |
| 712 | qxl_release_free(qdev, release); |
| 713 | return; |
| 714 | } |
| 715 | |
| 716 | cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release); |
| 717 | cmd->type = QXL_CURSOR_HIDE; |
| 718 | qxl_release_unmap(qdev, release, &cmd->release_info); |
| 719 | |
| 720 | qxl_release_fence_buffer_objects(release); |
| 721 | qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false); |
| 722 | } |
| 723 | |
| 724 | static void qxl_update_dumb_head(struct qxl_device *qdev, |
| 725 | int index, struct qxl_bo *bo) |
| 726 | { |
| 727 | uint32_t width, height; |
| 728 | |
| 729 | if (index >= qdev->monitors_config->max_allowed) |
| 730 | return; |
| 731 | |
| 732 | if (bo && bo->is_dumb) { |
| 733 | width = bo->surf.width; |
| 734 | height = bo->surf.height; |
| 735 | } else { |
| 736 | width = 0; |
| 737 | height = 0; |
| 738 | } |
| 739 | |
| 740 | if (qdev->dumb_heads[index].width == width && |
| 741 | qdev->dumb_heads[index].height == height) |
| 742 | return; |
| 743 | |
| 744 | DRM_DEBUG("#%d: %dx%d -> %dx%d\n", index, |
| 745 | qdev->dumb_heads[index].width, |
| 746 | qdev->dumb_heads[index].height, |
| 747 | width, height); |
| 748 | qdev->dumb_heads[index].width = width; |
| 749 | qdev->dumb_heads[index].height = height; |
| 750 | } |
| 751 | |
| 752 | static void qxl_calc_dumb_shadow(struct qxl_device *qdev, |
| 753 | struct qxl_surface *surf) |
| 754 | { |
| 755 | struct qxl_head *head; |
| 756 | int i; |
| 757 | |
| 758 | memset(surf, 0, sizeof(*surf)); |
| 759 | for (i = 0; i < qdev->monitors_config->max_allowed; i++) { |
| 760 | head = qdev->dumb_heads + i; |
| 761 | head->x = surf->width; |
| 762 | surf->width += head->width; |
| 763 | if (surf->height < head->height) |
| 764 | surf->height = head->height; |
| 765 | } |
| 766 | if (surf->width < 64) |
| 767 | surf->width = 64; |
| 768 | if (surf->height < 64) |
| 769 | surf->height = 64; |
| 770 | surf->format = SPICE_SURFACE_FMT_32_xRGB; |
| 771 | surf->stride = surf->width * 4; |
| 772 | |
| 773 | if (!qdev->dumb_shadow_bo || |
| 774 | qdev->dumb_shadow_bo->surf.width != surf->width || |
| 775 | qdev->dumb_shadow_bo->surf.height != surf->height) |
| 776 | DRM_DEBUG("%dx%d\n", surf->width, surf->height); |
| 777 | } |
| 778 | |
| 779 | static int qxl_plane_prepare_fb(struct drm_plane *plane, |
| 780 | struct drm_plane_state *new_state) |
| 781 | { |
| 782 | struct qxl_device *qdev = plane->dev->dev_private; |
| 783 | struct drm_gem_object *obj; |
| 784 | struct qxl_bo *user_bo; |
| 785 | struct qxl_surface surf; |
| 786 | int ret; |
| 787 | |
| 788 | if (!new_state->fb) |
| 789 | return 0; |
| 790 | |
| 791 | obj = new_state->fb->obj[0]; |
| 792 | user_bo = gem_to_qxl_bo(obj); |
| 793 | |
| 794 | if (plane->type == DRM_PLANE_TYPE_PRIMARY && |
| 795 | user_bo->is_dumb) { |
| 796 | qxl_update_dumb_head(qdev, new_state->crtc->index, |
| 797 | user_bo); |
| 798 | qxl_calc_dumb_shadow(qdev, &surf); |
| 799 | if (!qdev->dumb_shadow_bo || |
| 800 | qdev->dumb_shadow_bo->surf.width != surf.width || |
| 801 | qdev->dumb_shadow_bo->surf.height != surf.height) { |
| 802 | if (qdev->dumb_shadow_bo) { |
| 803 | drm_gem_object_put_unlocked |
| 804 | (&qdev->dumb_shadow_bo->tbo.base); |
| 805 | qdev->dumb_shadow_bo = NULL; |
| 806 | } |
| 807 | qxl_bo_create(qdev, surf.height * surf.stride, |
| 808 | true, true, QXL_GEM_DOMAIN_SURFACE, &surf, |
| 809 | &qdev->dumb_shadow_bo); |
| 810 | } |
| 811 | if (user_bo->shadow != qdev->dumb_shadow_bo) { |
| 812 | if (user_bo->shadow) { |
| 813 | drm_gem_object_put_unlocked |
| 814 | (&user_bo->shadow->tbo.base); |
| 815 | user_bo->shadow = NULL; |
| 816 | } |
| 817 | drm_gem_object_get(&qdev->dumb_shadow_bo->tbo.base); |
| 818 | user_bo->shadow = qdev->dumb_shadow_bo; |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | ret = qxl_bo_pin(user_bo); |
| 823 | if (ret) |
| 824 | return ret; |
| 825 | |
| 826 | return 0; |
| 827 | } |
| 828 | |
| 829 | static void qxl_plane_cleanup_fb(struct drm_plane *plane, |
| 830 | struct drm_plane_state *old_state) |
| 831 | { |
| 832 | struct drm_gem_object *obj; |
| 833 | struct qxl_bo *user_bo; |
| 834 | |
| 835 | if (!old_state->fb) { |
| 836 | /* |
| 837 | * we never executed prepare_fb, so there's nothing to |
| 838 | * unpin. |
| 839 | */ |
| 840 | return; |
| 841 | } |
| 842 | |
| 843 | obj = old_state->fb->obj[0]; |
| 844 | user_bo = gem_to_qxl_bo(obj); |
| 845 | qxl_bo_unpin(user_bo); |
| 846 | |
| 847 | if (old_state->fb != plane->state->fb && user_bo->shadow) { |
| 848 | drm_gem_object_put_unlocked(&user_bo->shadow->tbo.base); |
| 849 | user_bo->shadow = NULL; |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | static const uint32_t qxl_cursor_plane_formats[] = { |
| 854 | DRM_FORMAT_ARGB8888, |
| 855 | }; |
| 856 | |
| 857 | static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = { |
| 858 | .atomic_update = qxl_cursor_atomic_update, |
| 859 | .atomic_disable = qxl_cursor_atomic_disable, |
| 860 | .prepare_fb = qxl_plane_prepare_fb, |
| 861 | .cleanup_fb = qxl_plane_cleanup_fb, |
| 862 | }; |
| 863 | |
| 864 | static const struct drm_plane_funcs qxl_cursor_plane_funcs = { |
| 865 | .update_plane = drm_atomic_helper_update_plane, |
| 866 | .disable_plane = drm_atomic_helper_disable_plane, |
| 867 | .destroy = drm_primary_helper_destroy, |
| 868 | .reset = drm_atomic_helper_plane_reset, |
| 869 | .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state, |
| 870 | .atomic_destroy_state = drm_atomic_helper_plane_destroy_state, |
| 871 | }; |
| 872 | |
| 873 | static const uint32_t qxl_primary_plane_formats[] = { |
| 874 | DRM_FORMAT_XRGB8888, |
| 875 | DRM_FORMAT_ARGB8888, |
| 876 | }; |
| 877 | |
| 878 | static const struct drm_plane_helper_funcs primary_helper_funcs = { |
| 879 | .atomic_check = qxl_primary_atomic_check, |
| 880 | .atomic_update = qxl_primary_atomic_update, |
| 881 | .atomic_disable = qxl_primary_atomic_disable, |
| 882 | .prepare_fb = qxl_plane_prepare_fb, |
| 883 | .cleanup_fb = qxl_plane_cleanup_fb, |
| 884 | }; |
| 885 | |
| 886 | static const struct drm_plane_funcs qxl_primary_plane_funcs = { |
| 887 | .update_plane = drm_atomic_helper_update_plane, |
| 888 | .disable_plane = drm_atomic_helper_disable_plane, |
| 889 | .destroy = drm_primary_helper_destroy, |
| 890 | .reset = drm_atomic_helper_plane_reset, |
| 891 | .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state, |
| 892 | .atomic_destroy_state = drm_atomic_helper_plane_destroy_state, |
| 893 | }; |
| 894 | |
| 895 | static struct drm_plane *qxl_create_plane(struct qxl_device *qdev, |
| 896 | unsigned int possible_crtcs, |
| 897 | enum drm_plane_type type) |
| 898 | { |
| 899 | const struct drm_plane_helper_funcs *helper_funcs = NULL; |
| 900 | struct drm_plane *plane; |
| 901 | const struct drm_plane_funcs *funcs; |
| 902 | const uint32_t *formats; |
| 903 | int num_formats; |
| 904 | int err; |
| 905 | |
| 906 | if (type == DRM_PLANE_TYPE_PRIMARY) { |
| 907 | funcs = &qxl_primary_plane_funcs; |
| 908 | formats = qxl_primary_plane_formats; |
| 909 | num_formats = ARRAY_SIZE(qxl_primary_plane_formats); |
| 910 | helper_funcs = &primary_helper_funcs; |
| 911 | } else if (type == DRM_PLANE_TYPE_CURSOR) { |
| 912 | funcs = &qxl_cursor_plane_funcs; |
| 913 | formats = qxl_cursor_plane_formats; |
| 914 | helper_funcs = &qxl_cursor_helper_funcs; |
| 915 | num_formats = ARRAY_SIZE(qxl_cursor_plane_formats); |
| 916 | } else { |
| 917 | return ERR_PTR(-EINVAL); |
| 918 | } |
| 919 | |
| 920 | plane = kzalloc(sizeof(*plane), GFP_KERNEL); |
| 921 | if (!plane) |
| 922 | return ERR_PTR(-ENOMEM); |
| 923 | |
| 924 | err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs, |
| 925 | funcs, formats, num_formats, |
| 926 | NULL, type, NULL); |
| 927 | if (err) |
| 928 | goto free_plane; |
| 929 | |
| 930 | drm_plane_helper_add(plane, helper_funcs); |
| 931 | |
| 932 | return plane; |
| 933 | |
| 934 | free_plane: |
| 935 | kfree(plane); |
| 936 | return ERR_PTR(-EINVAL); |
| 937 | } |
| 938 | |
| 939 | static int qdev_crtc_init(struct drm_device *dev, int crtc_id) |
| 940 | { |
| 941 | struct qxl_crtc *qxl_crtc; |
| 942 | struct drm_plane *primary, *cursor; |
| 943 | struct qxl_device *qdev = dev->dev_private; |
| 944 | int r; |
| 945 | |
| 946 | qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL); |
| 947 | if (!qxl_crtc) |
| 948 | return -ENOMEM; |
| 949 | |
| 950 | primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY); |
| 951 | if (IS_ERR(primary)) { |
| 952 | r = -ENOMEM; |
| 953 | goto free_mem; |
| 954 | } |
| 955 | |
| 956 | cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR); |
| 957 | if (IS_ERR(cursor)) { |
| 958 | r = -ENOMEM; |
| 959 | goto clean_primary; |
| 960 | } |
| 961 | |
| 962 | r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor, |
| 963 | &qxl_crtc_funcs, NULL); |
| 964 | if (r) |
| 965 | goto clean_cursor; |
| 966 | |
| 967 | qxl_crtc->index = crtc_id; |
| 968 | drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs); |
| 969 | return 0; |
| 970 | |
| 971 | clean_cursor: |
| 972 | drm_plane_cleanup(cursor); |
| 973 | kfree(cursor); |
| 974 | clean_primary: |
| 975 | drm_plane_cleanup(primary); |
| 976 | kfree(primary); |
| 977 | free_mem: |
| 978 | kfree(qxl_crtc); |
| 979 | return r; |
| 980 | } |
| 981 | |
| 982 | static int qxl_conn_get_modes(struct drm_connector *connector) |
| 983 | { |
| 984 | struct drm_device *dev = connector->dev; |
| 985 | struct qxl_device *qdev = dev->dev_private; |
| 986 | struct qxl_output *output = drm_connector_to_qxl_output(connector); |
| 987 | unsigned int pwidth = 1024; |
| 988 | unsigned int pheight = 768; |
| 989 | int ret = 0; |
| 990 | |
| 991 | if (qdev->client_monitors_config) { |
| 992 | struct qxl_head *head; |
| 993 | head = &qdev->client_monitors_config->heads[output->index]; |
| 994 | if (head->width) |
| 995 | pwidth = head->width; |
| 996 | if (head->height) |
| 997 | pheight = head->height; |
| 998 | } |
| 999 | |
| 1000 | ret += drm_add_modes_noedid(connector, 8192, 8192); |
| 1001 | ret += qxl_add_extra_modes(connector); |
| 1002 | ret += qxl_add_monitors_config_modes(connector); |
| 1003 | drm_set_preferred_mode(connector, pwidth, pheight); |
| 1004 | return ret; |
| 1005 | } |
| 1006 | |
| 1007 | static enum drm_mode_status qxl_conn_mode_valid(struct drm_connector *connector, |
| 1008 | struct drm_display_mode *mode) |
| 1009 | { |
| 1010 | struct drm_device *ddev = connector->dev; |
| 1011 | struct qxl_device *qdev = ddev->dev_private; |
| 1012 | |
| 1013 | if (qxl_check_mode(qdev, mode->hdisplay, mode->vdisplay) != 0) |
| 1014 | return MODE_BAD; |
| 1015 | |
| 1016 | return MODE_OK; |
| 1017 | } |
| 1018 | |
| 1019 | static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector) |
| 1020 | { |
| 1021 | struct qxl_output *qxl_output = |
| 1022 | drm_connector_to_qxl_output(connector); |
| 1023 | |
| 1024 | DRM_DEBUG("\n"); |
| 1025 | return &qxl_output->enc; |
| 1026 | } |
| 1027 | |
| 1028 | static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = { |
| 1029 | }; |
| 1030 | |
| 1031 | static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = { |
| 1032 | .get_modes = qxl_conn_get_modes, |
| 1033 | .mode_valid = qxl_conn_mode_valid, |
| 1034 | .best_encoder = qxl_best_encoder, |
| 1035 | }; |
| 1036 | |
| 1037 | static enum drm_connector_status qxl_conn_detect( |
| 1038 | struct drm_connector *connector, |
| 1039 | bool force) |
| 1040 | { |
| 1041 | struct qxl_output *output = |
| 1042 | drm_connector_to_qxl_output(connector); |
| 1043 | struct drm_device *ddev = connector->dev; |
| 1044 | struct qxl_device *qdev = ddev->dev_private; |
| 1045 | bool connected = false; |
| 1046 | |
| 1047 | /* The first monitor is always connected */ |
| 1048 | if (!qdev->client_monitors_config) { |
| 1049 | if (output->index == 0) |
| 1050 | connected = true; |
| 1051 | } else |
| 1052 | connected = qdev->client_monitors_config->count > output->index && |
| 1053 | qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]); |
| 1054 | |
| 1055 | DRM_DEBUG("#%d connected: %d\n", output->index, connected); |
| 1056 | |
| 1057 | return connected ? connector_status_connected |
| 1058 | : connector_status_disconnected; |
| 1059 | } |
| 1060 | |
| 1061 | static void qxl_conn_destroy(struct drm_connector *connector) |
| 1062 | { |
| 1063 | struct qxl_output *qxl_output = |
| 1064 | drm_connector_to_qxl_output(connector); |
| 1065 | |
| 1066 | drm_connector_unregister(connector); |
| 1067 | drm_connector_cleanup(connector); |
| 1068 | kfree(qxl_output); |
| 1069 | } |
| 1070 | |
| 1071 | static const struct drm_connector_funcs qxl_connector_funcs = { |
| 1072 | .detect = qxl_conn_detect, |
| 1073 | .fill_modes = drm_helper_probe_single_connector_modes, |
| 1074 | .destroy = qxl_conn_destroy, |
| 1075 | .reset = drm_atomic_helper_connector_reset, |
| 1076 | .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, |
| 1077 | .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, |
| 1078 | }; |
| 1079 | |
| 1080 | static void qxl_enc_destroy(struct drm_encoder *encoder) |
| 1081 | { |
| 1082 | drm_encoder_cleanup(encoder); |
| 1083 | } |
| 1084 | |
| 1085 | static const struct drm_encoder_funcs qxl_enc_funcs = { |
| 1086 | .destroy = qxl_enc_destroy, |
| 1087 | }; |
| 1088 | |
| 1089 | static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev) |
| 1090 | { |
| 1091 | if (qdev->hotplug_mode_update_property) |
| 1092 | return 0; |
| 1093 | |
| 1094 | qdev->hotplug_mode_update_property = |
| 1095 | drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE, |
| 1096 | "hotplug_mode_update", 0, 1); |
| 1097 | |
| 1098 | return 0; |
| 1099 | } |
| 1100 | |
| 1101 | static int qdev_output_init(struct drm_device *dev, int num_output) |
| 1102 | { |
| 1103 | struct qxl_device *qdev = dev->dev_private; |
| 1104 | struct qxl_output *qxl_output; |
| 1105 | struct drm_connector *connector; |
| 1106 | struct drm_encoder *encoder; |
| 1107 | |
| 1108 | qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL); |
| 1109 | if (!qxl_output) |
| 1110 | return -ENOMEM; |
| 1111 | |
| 1112 | qxl_output->index = num_output; |
| 1113 | |
| 1114 | connector = &qxl_output->base; |
| 1115 | encoder = &qxl_output->enc; |
| 1116 | drm_connector_init(dev, &qxl_output->base, |
| 1117 | &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL); |
| 1118 | |
| 1119 | drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs, |
| 1120 | DRM_MODE_ENCODER_VIRTUAL, NULL); |
| 1121 | |
| 1122 | /* we get HPD via client monitors config */ |
| 1123 | connector->polled = DRM_CONNECTOR_POLL_HPD; |
| 1124 | encoder->possible_crtcs = 1 << num_output; |
| 1125 | drm_connector_attach_encoder(&qxl_output->base, |
| 1126 | &qxl_output->enc); |
| 1127 | drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs); |
| 1128 | drm_connector_helper_add(connector, &qxl_connector_helper_funcs); |
| 1129 | |
| 1130 | drm_object_attach_property(&connector->base, |
| 1131 | qdev->hotplug_mode_update_property, 0); |
| 1132 | drm_object_attach_property(&connector->base, |
| 1133 | dev->mode_config.suggested_x_property, 0); |
| 1134 | drm_object_attach_property(&connector->base, |
| 1135 | dev->mode_config.suggested_y_property, 0); |
| 1136 | return 0; |
| 1137 | } |
| 1138 | |
| 1139 | static struct drm_framebuffer * |
| 1140 | qxl_user_framebuffer_create(struct drm_device *dev, |
| 1141 | struct drm_file *file_priv, |
| 1142 | const struct drm_mode_fb_cmd2 *mode_cmd) |
| 1143 | { |
| 1144 | return drm_gem_fb_create_with_funcs(dev, file_priv, mode_cmd, |
| 1145 | &qxl_fb_funcs); |
| 1146 | } |
| 1147 | |
| 1148 | static const struct drm_mode_config_funcs qxl_mode_funcs = { |
| 1149 | .fb_create = qxl_user_framebuffer_create, |
| 1150 | .atomic_check = drm_atomic_helper_check, |
| 1151 | .atomic_commit = drm_atomic_helper_commit, |
| 1152 | }; |
| 1153 | |
| 1154 | int qxl_create_monitors_object(struct qxl_device *qdev) |
| 1155 | { |
| 1156 | int ret; |
| 1157 | struct drm_gem_object *gobj; |
| 1158 | int monitors_config_size = sizeof(struct qxl_monitors_config) + |
| 1159 | qxl_num_crtc * sizeof(struct qxl_head); |
| 1160 | |
| 1161 | ret = qxl_gem_object_create(qdev, monitors_config_size, 0, |
| 1162 | QXL_GEM_DOMAIN_VRAM, |
| 1163 | false, false, NULL, &gobj); |
| 1164 | if (ret) { |
| 1165 | DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret); |
| 1166 | return -ENOMEM; |
| 1167 | } |
| 1168 | qdev->monitors_config_bo = gem_to_qxl_bo(gobj); |
| 1169 | |
| 1170 | ret = qxl_bo_pin(qdev->monitors_config_bo); |
| 1171 | if (ret) |
| 1172 | return ret; |
| 1173 | |
| 1174 | qxl_bo_kmap(qdev->monitors_config_bo, NULL); |
| 1175 | |
| 1176 | qdev->monitors_config = qdev->monitors_config_bo->kptr; |
| 1177 | qdev->ram_header->monitors_config = |
| 1178 | qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0); |
| 1179 | |
| 1180 | memset(qdev->monitors_config, 0, monitors_config_size); |
| 1181 | qdev->dumb_heads = kcalloc(qxl_num_crtc, sizeof(qdev->dumb_heads[0]), |
| 1182 | GFP_KERNEL); |
| 1183 | if (!qdev->dumb_heads) { |
| 1184 | qxl_destroy_monitors_object(qdev); |
| 1185 | return -ENOMEM; |
| 1186 | } |
| 1187 | return 0; |
| 1188 | } |
| 1189 | |
| 1190 | int qxl_destroy_monitors_object(struct qxl_device *qdev) |
| 1191 | { |
| 1192 | int ret; |
| 1193 | |
| 1194 | qdev->monitors_config = NULL; |
| 1195 | qdev->ram_header->monitors_config = 0; |
| 1196 | |
| 1197 | qxl_bo_kunmap(qdev->monitors_config_bo); |
| 1198 | ret = qxl_bo_unpin(qdev->monitors_config_bo); |
| 1199 | if (ret) |
| 1200 | return ret; |
| 1201 | |
| 1202 | qxl_bo_unref(&qdev->monitors_config_bo); |
| 1203 | return 0; |
| 1204 | } |
| 1205 | |
| 1206 | int qxl_modeset_init(struct qxl_device *qdev) |
| 1207 | { |
| 1208 | int i; |
| 1209 | int ret; |
| 1210 | |
| 1211 | drm_mode_config_init(&qdev->ddev); |
| 1212 | |
| 1213 | ret = qxl_create_monitors_object(qdev); |
| 1214 | if (ret) |
| 1215 | return ret; |
| 1216 | |
| 1217 | qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs; |
| 1218 | |
| 1219 | /* modes will be validated against the framebuffer size */ |
| 1220 | qdev->ddev.mode_config.min_width = 0; |
| 1221 | qdev->ddev.mode_config.min_height = 0; |
| 1222 | qdev->ddev.mode_config.max_width = 8192; |
| 1223 | qdev->ddev.mode_config.max_height = 8192; |
| 1224 | |
| 1225 | qdev->ddev.mode_config.fb_base = qdev->vram_base; |
| 1226 | |
| 1227 | drm_mode_create_suggested_offset_properties(&qdev->ddev); |
| 1228 | qxl_mode_create_hotplug_mode_update_property(qdev); |
| 1229 | |
| 1230 | for (i = 0 ; i < qxl_num_crtc; ++i) { |
| 1231 | qdev_crtc_init(&qdev->ddev, i); |
| 1232 | qdev_output_init(&qdev->ddev, i); |
| 1233 | } |
| 1234 | |
| 1235 | qxl_display_read_client_monitors_config(qdev); |
| 1236 | |
| 1237 | drm_mode_config_reset(&qdev->ddev); |
| 1238 | return 0; |
| 1239 | } |
| 1240 | |
| 1241 | void qxl_modeset_fini(struct qxl_device *qdev) |
| 1242 | { |
| 1243 | if (qdev->dumb_shadow_bo) { |
| 1244 | drm_gem_object_put(&qdev->dumb_shadow_bo->tbo.base); |
| 1245 | qdev->dumb_shadow_bo = NULL; |
| 1246 | } |
| 1247 | qxl_destroy_monitors_object(qdev); |
| 1248 | drm_mode_config_cleanup(&qdev->ddev); |
| 1249 | } |