1 /* 2 * Copyright (C) 2015 Red Hat, Inc. 3 * All Rights Reserved. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining 6 * a copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sublicense, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the 14 * next paragraph) shall be included in all copies or substantial 15 * portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE 21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 */ 25 26 #include <drm/drm_atomic_helper.h> 27 #include <drm/drm_blend.h> 28 #include <drm/drm_damage_helper.h> 29 #include <drm/drm_fourcc.h> 30 #include <drm/drm_gem_atomic_helper.h> 31 #include <linux/virtio_dma_buf.h> 32 #include <drm/drm_managed.h> 33 #include <drm/drm_panic.h> 34 #include <drm/drm_panic_helper.h> 35 #include <drm/drm_print.h> 36 37 #include "virtgpu_drv.h" 38 39 static const uint32_t virtio_gpu_formats[] = { 40 DRM_FORMAT_HOST_XRGB8888, 41 }; 42 43 static const uint32_t virtio_gpu_cursor_formats[] = { 44 DRM_FORMAT_HOST_ARGB8888, 45 }; 46 47 uint32_t virtio_gpu_translate_format(uint32_t drm_fourcc) 48 { 49 uint32_t format; 50 51 switch (drm_fourcc) { 52 case DRM_FORMAT_XRGB8888: 53 format = VIRTIO_GPU_FORMAT_B8G8R8X8_UNORM; 54 break; 55 case DRM_FORMAT_ARGB8888: 56 format = VIRTIO_GPU_FORMAT_B8G8R8A8_UNORM; 57 break; 58 case DRM_FORMAT_BGRX8888: 59 format = VIRTIO_GPU_FORMAT_X8R8G8B8_UNORM; 60 break; 61 case DRM_FORMAT_BGRA8888: 62 format = VIRTIO_GPU_FORMAT_A8R8G8B8_UNORM; 63 break; 64 default: 65 /* 66 * This should not happen, we handle everything listed 67 * in virtio_gpu_formats[]. 68 */ 69 format = 0; 70 break; 71 } 72 WARN_ON(format == 0); 73 return format; 74 } 75 76 static struct 77 drm_plane_state *virtio_gpu_plane_duplicate_state(struct drm_plane *plane) 78 { 79 struct virtio_gpu_plane_state *new; 80 81 if (WARN_ON(!plane->state)) 82 return NULL; 83 84 new = kzalloc_obj(*new); 85 if (!new) 86 return NULL; 87 88 __drm_atomic_helper_plane_duplicate_state(plane, &new->base); 89 90 return &new->base; 91 } 92 93 static const struct drm_plane_funcs virtio_gpu_plane_funcs = { 94 .update_plane = drm_atomic_helper_update_plane, 95 .disable_plane = drm_atomic_helper_disable_plane, 96 .atomic_create_state = drm_atomic_helper_plane_create_state, 97 .atomic_duplicate_state = virtio_gpu_plane_duplicate_state, 98 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state, 99 DRM_PANIC_PLANE_FUNCS, 100 }; 101 102 static int virtio_gpu_plane_atomic_check(struct drm_plane *plane, 103 struct drm_atomic_commit *state) 104 { 105 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, 106 plane); 107 struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, 108 plane); 109 bool is_cursor = plane->type == DRM_PLANE_TYPE_CURSOR; 110 struct drm_crtc_state *crtc_state; 111 int ret; 112 113 if (!new_plane_state->fb || WARN_ON(!new_plane_state->crtc)) 114 return 0; 115 116 /* 117 * Ignore damage clips if the framebuffer attached to the plane's state 118 * has changed since the last plane update (page-flip). In this case, a 119 * full plane update should happen because uploads are done per-buffer. 120 */ 121 if (old_plane_state->fb != new_plane_state->fb) 122 new_plane_state->ignore_damage_clips = true; 123 124 crtc_state = drm_atomic_get_crtc_state(state, 125 new_plane_state->crtc); 126 if (IS_ERR(crtc_state)) 127 return PTR_ERR(crtc_state); 128 129 ret = drm_atomic_helper_check_plane_state(new_plane_state, crtc_state, 130 DRM_PLANE_NO_SCALING, 131 DRM_PLANE_NO_SCALING, 132 is_cursor, true); 133 return ret; 134 } 135 136 /* For drm panic */ 137 static int virtio_gpu_panic_update_dumb_bo(struct virtio_gpu_device *vgdev, 138 struct drm_plane_state *state, 139 struct drm_rect *rect) 140 { 141 struct virtio_gpu_object *bo = 142 gem_to_virtio_gpu_obj(state->fb->obj[0]); 143 struct virtio_gpu_object_array *objs; 144 uint32_t w = rect->x2 - rect->x1; 145 uint32_t h = rect->y2 - rect->y1; 146 uint32_t x = rect->x1; 147 uint32_t y = rect->y1; 148 uint32_t off = x * state->fb->format->cpp[0] + 149 y * state->fb->pitches[0]; 150 151 objs = virtio_gpu_panic_array_alloc(); 152 if (!objs) 153 return -ENOMEM; 154 virtio_gpu_array_add_obj(objs, &bo->base.base); 155 156 return virtio_gpu_panic_cmd_transfer_to_host_2d(vgdev, off, w, h, x, y, 157 objs); 158 } 159 160 static void virtio_gpu_update_dumb_bo(struct virtio_gpu_device *vgdev, 161 struct drm_plane_state *state, 162 struct drm_rect *rect) 163 { 164 struct virtio_gpu_object *bo = 165 gem_to_virtio_gpu_obj(state->fb->obj[0]); 166 struct virtio_gpu_object_array *objs; 167 uint32_t w = rect->x2 - rect->x1; 168 uint32_t h = rect->y2 - rect->y1; 169 uint32_t x = rect->x1; 170 uint32_t y = rect->y1; 171 uint32_t off = x * state->fb->format->cpp[0] + 172 y * state->fb->pitches[0]; 173 174 objs = virtio_gpu_array_alloc(1); 175 if (!objs) 176 return; 177 virtio_gpu_array_add_obj(objs, &bo->base.base); 178 179 virtio_gpu_cmd_transfer_to_host_2d(vgdev, off, w, h, x, y, 180 objs, NULL); 181 } 182 183 /* For drm_panic */ 184 static void virtio_gpu_panic_resource_flush(struct drm_plane *plane, 185 uint32_t x, uint32_t y, 186 uint32_t width, uint32_t height) 187 { 188 struct drm_device *dev = plane->dev; 189 struct virtio_gpu_device *vgdev = dev->dev_private; 190 struct virtio_gpu_framebuffer *vgfb; 191 struct virtio_gpu_object *bo; 192 193 vgfb = to_virtio_gpu_framebuffer(plane->state->fb); 194 bo = gem_to_virtio_gpu_obj(vgfb->base.obj[0]); 195 196 virtio_gpu_panic_cmd_resource_flush(vgdev, bo->hw_res_handle, x, y, 197 width, height); 198 virtio_gpu_panic_notify(vgdev); 199 } 200 201 static void virtio_gpu_resource_flush(struct drm_plane *plane, 202 uint32_t x, uint32_t y, 203 uint32_t width, uint32_t height) 204 { 205 struct drm_device *dev = plane->dev; 206 struct virtio_gpu_device *vgdev = dev->dev_private; 207 struct virtio_gpu_framebuffer *vgfb; 208 struct virtio_gpu_plane_state *vgplane_st; 209 struct virtio_gpu_object *bo; 210 211 vgfb = to_virtio_gpu_framebuffer(plane->state->fb); 212 vgplane_st = to_virtio_gpu_plane_state(plane->state); 213 bo = gem_to_virtio_gpu_obj(vgfb->base.obj[0]); 214 if (vgplane_st->fence) { 215 struct virtio_gpu_object_array *objs; 216 217 objs = virtio_gpu_array_alloc(1); 218 if (!objs) 219 return; 220 virtio_gpu_array_add_obj(objs, vgfb->base.obj[0]); 221 if (virtio_gpu_lock_one_resv_uninterruptible(objs)) { 222 virtio_gpu_array_put_free(objs); 223 return; 224 } 225 virtio_gpu_cmd_resource_flush(vgdev, bo->hw_res_handle, x, y, 226 width, height, objs, 227 vgplane_st->fence); 228 virtio_gpu_notify(vgdev); 229 dma_fence_wait_timeout(&vgplane_st->fence->f, true, 230 msecs_to_jiffies(50)); 231 } else { 232 virtio_gpu_cmd_resource_flush(vgdev, bo->hw_res_handle, x, y, 233 width, height, NULL, NULL); 234 virtio_gpu_notify(vgdev); 235 } 236 } 237 238 static void virtio_gpu_primary_plane_update(struct drm_plane *plane, 239 struct drm_atomic_commit *state) 240 { 241 struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state, 242 plane); 243 struct drm_device *dev = plane->dev; 244 struct virtio_gpu_device *vgdev = dev->dev_private; 245 struct virtio_gpu_output *output = NULL; 246 struct virtio_gpu_object *bo; 247 struct drm_rect rect; 248 249 if (plane->state->crtc) 250 output = drm_crtc_to_virtio_gpu_output(plane->state->crtc); 251 if (old_state->crtc) 252 output = drm_crtc_to_virtio_gpu_output(old_state->crtc); 253 if (WARN_ON(!output)) 254 return; 255 256 if (!plane->state->fb || !output->crtc.state->active) { 257 DRM_DEBUG("nofb\n"); 258 virtio_gpu_cmd_set_scanout(vgdev, output->index, 0, 259 plane->state->src_w >> 16, 260 plane->state->src_h >> 16, 261 0, 0); 262 virtio_gpu_notify(vgdev); 263 return; 264 } 265 266 if (!drm_atomic_helper_damage_merged(old_state, plane->state, &rect)) 267 return; 268 269 bo = gem_to_virtio_gpu_obj(plane->state->fb->obj[0]); 270 if (bo->dumb) 271 virtio_gpu_update_dumb_bo(vgdev, plane->state, &rect); 272 273 if (plane->state->fb != old_state->fb || 274 plane->state->src_w != old_state->src_w || 275 plane->state->src_h != old_state->src_h || 276 plane->state->src_x != old_state->src_x || 277 plane->state->src_y != old_state->src_y || 278 output->needs_modeset) { 279 output->needs_modeset = false; 280 DRM_DEBUG("handle 0x%x, crtc %dx%d+%d+%d, src %dx%d+%d+%d\n", 281 bo->hw_res_handle, 282 plane->state->crtc_w, plane->state->crtc_h, 283 plane->state->crtc_x, plane->state->crtc_y, 284 plane->state->src_w >> 16, 285 plane->state->src_h >> 16, 286 plane->state->src_x >> 16, 287 plane->state->src_y >> 16); 288 289 if (bo->host3d_blob || bo->guest_blob) { 290 virtio_gpu_cmd_set_scanout_blob 291 (vgdev, output->index, bo, 292 plane->state->fb, 293 plane->state->src_w >> 16, 294 plane->state->src_h >> 16, 295 plane->state->src_x >> 16, 296 plane->state->src_y >> 16); 297 } else { 298 virtio_gpu_cmd_set_scanout(vgdev, output->index, 299 bo->hw_res_handle, 300 plane->state->src_w >> 16, 301 plane->state->src_h >> 16, 302 plane->state->src_x >> 16, 303 plane->state->src_y >> 16); 304 } 305 } 306 307 virtio_gpu_resource_flush(plane, 308 rect.x1, 309 rect.y1, 310 rect.x2 - rect.x1, 311 rect.y2 - rect.y1); 312 } 313 314 static int virtio_gpu_prepare_imported_obj(struct drm_plane *plane, 315 struct drm_plane_state *new_state, 316 struct drm_gem_object *obj) 317 { 318 struct virtio_gpu_device *vgdev = plane->dev->dev_private; 319 struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(obj); 320 struct dma_buf_attachment *attach = obj->import_attach; 321 struct dma_resv *resv = attach->dmabuf->resv; 322 struct virtio_gpu_mem_entry *ents = NULL; 323 unsigned int nents; 324 int ret; 325 326 dma_resv_lock(resv, NULL); 327 328 ret = dma_buf_pin(attach); 329 if (ret) { 330 dma_resv_unlock(resv); 331 return ret; 332 } 333 334 if (!bo->sgt) { 335 ret = virtgpu_dma_buf_import_sgt(&ents, &nents, 336 bo, attach); 337 if (ret) 338 goto err; 339 340 virtio_gpu_object_attach(vgdev, bo, ents, nents); 341 } 342 343 dma_resv_unlock(resv); 344 return 0; 345 346 err: 347 dma_buf_unpin(attach); 348 dma_resv_unlock(resv); 349 return ret; 350 } 351 352 static int virtio_gpu_plane_prepare_fb(struct drm_plane *plane, 353 struct drm_plane_state *new_state) 354 { 355 struct drm_device *dev = plane->dev; 356 struct virtio_gpu_device *vgdev = dev->dev_private; 357 struct virtio_gpu_framebuffer *vgfb; 358 struct virtio_gpu_plane_state *vgplane_st; 359 struct virtio_gpu_object *bo; 360 struct drm_gem_object *obj; 361 int ret; 362 363 if (!new_state->fb) 364 return 0; 365 366 vgfb = to_virtio_gpu_framebuffer(new_state->fb); 367 vgplane_st = to_virtio_gpu_plane_state(new_state); 368 bo = gem_to_virtio_gpu_obj(vgfb->base.obj[0]); 369 370 drm_gem_plane_helper_prepare_fb(plane, new_state); 371 372 if (!bo || (plane->type == DRM_PLANE_TYPE_PRIMARY && !bo->guest_blob)) 373 return 0; 374 375 obj = new_state->fb->obj[0]; 376 if (bo->dumb || drm_gem_is_imported(obj)) { 377 vgplane_st->fence = virtio_gpu_fence_alloc(vgdev, 378 vgdev->fence_drv.context, 379 0); 380 if (!vgplane_st->fence) 381 return -ENOMEM; 382 } 383 384 if (drm_gem_is_imported(obj)) { 385 ret = virtio_gpu_prepare_imported_obj(plane, new_state, obj); 386 if (ret) 387 goto err_fence; 388 } 389 390 return 0; 391 392 err_fence: 393 if (vgplane_st->fence) { 394 dma_fence_put(&vgplane_st->fence->f); 395 vgplane_st->fence = NULL; 396 } 397 398 return ret; 399 } 400 401 static void virtio_gpu_cleanup_imported_obj(struct drm_gem_object *obj) 402 { 403 struct dma_buf_attachment *attach = obj->import_attach; 404 struct dma_resv *resv = attach->dmabuf->resv; 405 406 dma_resv_lock(resv, NULL); 407 dma_buf_unpin(attach); 408 dma_resv_unlock(resv); 409 } 410 411 static void virtio_gpu_plane_cleanup_fb(struct drm_plane *plane, 412 struct drm_plane_state *state) 413 { 414 struct virtio_gpu_plane_state *vgplane_st; 415 struct drm_gem_object *obj; 416 417 if (!state->fb) 418 return; 419 420 vgplane_st = to_virtio_gpu_plane_state(state); 421 if (vgplane_st->fence) { 422 dma_fence_put(&vgplane_st->fence->f); 423 vgplane_st->fence = NULL; 424 } 425 426 obj = state->fb->obj[0]; 427 if (drm_gem_is_imported(obj)) 428 virtio_gpu_cleanup_imported_obj(obj); 429 } 430 431 static void virtio_gpu_cursor_plane_update(struct drm_plane *plane, 432 struct drm_atomic_commit *state) 433 { 434 struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state, 435 plane); 436 struct drm_device *dev = plane->dev; 437 struct virtio_gpu_device *vgdev = dev->dev_private; 438 struct virtio_gpu_output *output = NULL; 439 struct virtio_gpu_framebuffer *vgfb; 440 struct virtio_gpu_plane_state *vgplane_st; 441 struct virtio_gpu_object *bo = NULL; 442 uint32_t handle; 443 444 if (plane->state->crtc) 445 output = drm_crtc_to_virtio_gpu_output(plane->state->crtc); 446 if (old_state->crtc) 447 output = drm_crtc_to_virtio_gpu_output(old_state->crtc); 448 if (WARN_ON(!output)) 449 return; 450 451 if (plane->state->fb) { 452 vgfb = to_virtio_gpu_framebuffer(plane->state->fb); 453 vgplane_st = to_virtio_gpu_plane_state(plane->state); 454 bo = gem_to_virtio_gpu_obj(vgfb->base.obj[0]); 455 handle = bo->hw_res_handle; 456 } else { 457 handle = 0; 458 } 459 460 if (bo && bo->dumb && (plane->state->fb != old_state->fb)) { 461 /* new cursor -- update & wait */ 462 struct virtio_gpu_object_array *objs; 463 464 objs = virtio_gpu_array_alloc(1); 465 if (!objs) 466 return; 467 virtio_gpu_array_add_obj(objs, vgfb->base.obj[0]); 468 if (virtio_gpu_lock_one_resv_uninterruptible(objs)) { 469 virtio_gpu_array_put_free(objs); 470 return; 471 } 472 virtio_gpu_cmd_transfer_to_host_2d 473 (vgdev, 0, 474 plane->state->crtc_w, 475 plane->state->crtc_h, 476 0, 0, objs, vgplane_st->fence); 477 virtio_gpu_notify(vgdev); 478 dma_fence_wait(&vgplane_st->fence->f, true); 479 } 480 481 if (plane->state->fb != old_state->fb) { 482 DRM_DEBUG("update, handle %d, pos +%d+%d, hot %d,%d\n", handle, 483 plane->state->crtc_x, 484 plane->state->crtc_y, 485 plane->state->hotspot_x, 486 plane->state->hotspot_y); 487 output->cursor.hdr.type = 488 cpu_to_le32(VIRTIO_GPU_CMD_UPDATE_CURSOR); 489 output->cursor.resource_id = cpu_to_le32(handle); 490 if (plane->state->fb) { 491 output->cursor.hot_x = 492 cpu_to_le32(plane->state->hotspot_x); 493 output->cursor.hot_y = 494 cpu_to_le32(plane->state->hotspot_y); 495 } else { 496 output->cursor.hot_x = cpu_to_le32(0); 497 output->cursor.hot_y = cpu_to_le32(0); 498 } 499 } else { 500 DRM_DEBUG("move +%d+%d\n", 501 plane->state->crtc_x, 502 plane->state->crtc_y); 503 output->cursor.hdr.type = 504 cpu_to_le32(VIRTIO_GPU_CMD_MOVE_CURSOR); 505 } 506 output->cursor.pos.x = cpu_to_le32(plane->state->crtc_x); 507 output->cursor.pos.y = cpu_to_le32(plane->state->crtc_y); 508 virtio_gpu_cursor_ping(vgdev, output); 509 } 510 511 static int virtio_drm_get_scanout_buffer(struct drm_plane *plane, 512 struct drm_scanout_buffer *sb) 513 { 514 struct virtio_gpu_object *bo; 515 516 if (!plane->state || !plane->state->fb || !plane->state->visible) 517 return -ENODEV; 518 519 bo = gem_to_virtio_gpu_obj(plane->state->fb->obj[0]); 520 521 if (virtio_gpu_is_vram(bo) || drm_gem_is_imported(&bo->base.base)) 522 return -ENODEV; 523 524 if (bo->base.vaddr) { 525 iosys_map_set_vaddr(&sb->map[0], bo->base.vaddr); 526 } else { 527 struct drm_gem_shmem_object *shmem = &bo->base; 528 529 if (!shmem->pages) 530 return -ENODEV; 531 /* map scanout buffer later */ 532 sb->pages = shmem->pages; 533 } 534 535 sb->format = plane->state->fb->format; 536 sb->height = plane->state->fb->height; 537 sb->width = plane->state->fb->width; 538 sb->pitch[0] = plane->state->fb->pitches[0]; 539 return 0; 540 } 541 542 static void virtio_panic_flush(struct drm_plane *plane) 543 { 544 struct virtio_gpu_object *bo; 545 struct drm_device *dev = plane->dev; 546 struct virtio_gpu_device *vgdev = dev->dev_private; 547 struct drm_rect rect; 548 549 rect.x1 = 0; 550 rect.y1 = 0; 551 rect.x2 = plane->state->fb->width; 552 rect.y2 = plane->state->fb->height; 553 554 bo = gem_to_virtio_gpu_obj(plane->state->fb->obj[0]); 555 556 if (bo->dumb) { 557 if (virtio_gpu_panic_update_dumb_bo(vgdev, plane->state, 558 &rect)) 559 return; 560 } 561 562 virtio_gpu_panic_resource_flush(plane, 563 plane->state->src_x >> 16, 564 plane->state->src_y >> 16, 565 plane->state->src_w >> 16, 566 plane->state->src_h >> 16); 567 } 568 569 static const struct drm_plane_helper_funcs virtio_gpu_primary_helper_funcs = { 570 .prepare_fb = virtio_gpu_plane_prepare_fb, 571 .cleanup_fb = virtio_gpu_plane_cleanup_fb, 572 .atomic_check = virtio_gpu_plane_atomic_check, 573 .atomic_update = virtio_gpu_primary_plane_update, 574 .get_scanout_buffer = virtio_drm_get_scanout_buffer, 575 .panic_flush = virtio_panic_flush, 576 }; 577 578 static const struct drm_plane_helper_funcs virtio_gpu_cursor_helper_funcs = { 579 .prepare_fb = virtio_gpu_plane_prepare_fb, 580 .cleanup_fb = virtio_gpu_plane_cleanup_fb, 581 .atomic_check = virtio_gpu_plane_atomic_check, 582 .atomic_update = virtio_gpu_cursor_plane_update, 583 }; 584 585 struct drm_plane *virtio_gpu_plane_init(struct virtio_gpu_device *vgdev, 586 enum drm_plane_type type, 587 int index) 588 { 589 struct drm_device *dev = vgdev->ddev; 590 const struct drm_plane_helper_funcs *funcs; 591 struct drm_plane *plane; 592 const uint32_t *formats; 593 int nformats; 594 595 if (type == DRM_PLANE_TYPE_CURSOR) { 596 formats = virtio_gpu_cursor_formats; 597 nformats = ARRAY_SIZE(virtio_gpu_cursor_formats); 598 funcs = &virtio_gpu_cursor_helper_funcs; 599 } else { 600 formats = virtio_gpu_formats; 601 nformats = ARRAY_SIZE(virtio_gpu_formats); 602 funcs = &virtio_gpu_primary_helper_funcs; 603 } 604 605 plane = drmm_universal_plane_alloc(dev, struct drm_plane, dev, 606 1 << index, &virtio_gpu_plane_funcs, 607 formats, nformats, NULL, type, NULL); 608 if (IS_ERR(plane)) 609 return plane; 610 611 drm_plane_helper_add(plane, funcs); 612 613 if (type == DRM_PLANE_TYPE_PRIMARY) 614 drm_plane_enable_fb_damage_clips(plane); 615 else if (type == DRM_PLANE_TYPE_CURSOR) 616 drm_plane_create_blend_mode_property(plane, 617 BIT(DRM_MODE_BLEND_PREMULTI)); 618 619 return plane; 620 } 621