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 /* QXL cmd/ring handling */ 27 28 #include "qxl_drv.h" 29 #include "qxl_object.h" 30 31 static int qxl_reap_surface_id(struct qxl_device *qdev, int max_to_reap); 32 33 struct ring { 34 struct qxl_ring_header header; 35 uint8_t elements[0]; 36 }; 37 38 struct qxl_ring { 39 struct ring *ring; 40 int element_size; 41 int n_elements; 42 int prod_notify; 43 wait_queue_head_t *push_event; 44 spinlock_t lock; 45 }; 46 47 void qxl_ring_free(struct qxl_ring *ring) 48 { 49 kfree(ring); 50 } 51 52 void qxl_ring_init_hdr(struct qxl_ring *ring) 53 { 54 ring->ring->header.notify_on_prod = ring->n_elements; 55 } 56 57 struct qxl_ring * 58 qxl_ring_create(struct qxl_ring_header *header, 59 int element_size, 60 int n_elements, 61 int prod_notify, 62 bool set_prod_notify, 63 wait_queue_head_t *push_event) 64 { 65 struct qxl_ring *ring; 66 67 ring = kmalloc(sizeof(*ring), GFP_KERNEL); 68 if (!ring) 69 return NULL; 70 71 ring->ring = (struct ring *)header; 72 ring->element_size = element_size; 73 ring->n_elements = n_elements; 74 ring->prod_notify = prod_notify; 75 ring->push_event = push_event; 76 if (set_prod_notify) 77 qxl_ring_init_hdr(ring); 78 spin_lock_init(&ring->lock); 79 return ring; 80 } 81 82 static int qxl_check_header(struct qxl_ring *ring) 83 { 84 int ret; 85 struct qxl_ring_header *header = &(ring->ring->header); 86 unsigned long flags; 87 88 spin_lock_irqsave(&ring->lock, flags); 89 ret = header->prod - header->cons < header->num_items; 90 if (ret == 0) 91 header->notify_on_cons = header->cons + 1; 92 spin_unlock_irqrestore(&ring->lock, flags); 93 return ret; 94 } 95 96 int qxl_check_idle(struct qxl_ring *ring) 97 { 98 int ret; 99 struct qxl_ring_header *header = &(ring->ring->header); 100 unsigned long flags; 101 102 spin_lock_irqsave(&ring->lock, flags); 103 ret = header->prod == header->cons; 104 spin_unlock_irqrestore(&ring->lock, flags); 105 return ret; 106 } 107 108 int qxl_ring_push(struct qxl_ring *ring, 109 const void *new_elt, bool interruptible) 110 { 111 struct qxl_ring_header *header = &(ring->ring->header); 112 uint8_t *elt; 113 int idx, ret; 114 unsigned long flags; 115 116 spin_lock_irqsave(&ring->lock, flags); 117 if (header->prod - header->cons == header->num_items) { 118 header->notify_on_cons = header->cons + 1; 119 mb(); 120 spin_unlock_irqrestore(&ring->lock, flags); 121 if (!drm_can_sleep()) { 122 while (!qxl_check_header(ring)) 123 udelay(1); 124 } else { 125 if (interruptible) { 126 ret = wait_event_interruptible(*ring->push_event, 127 qxl_check_header(ring)); 128 if (ret) 129 return ret; 130 } else { 131 wait_event(*ring->push_event, 132 qxl_check_header(ring)); 133 } 134 135 } 136 spin_lock_irqsave(&ring->lock, flags); 137 } 138 139 idx = header->prod & (ring->n_elements - 1); 140 elt = ring->ring->elements + idx * ring->element_size; 141 142 memcpy((void *)elt, new_elt, ring->element_size); 143 144 header->prod++; 145 146 mb(); 147 148 if (header->prod == header->notify_on_prod) 149 outb(0, ring->prod_notify); 150 151 spin_unlock_irqrestore(&ring->lock, flags); 152 return 0; 153 } 154 155 static bool qxl_ring_pop(struct qxl_ring *ring, 156 void *element) 157 { 158 volatile struct qxl_ring_header *header = &(ring->ring->header); 159 volatile uint8_t *ring_elt; 160 int idx; 161 unsigned long flags; 162 163 spin_lock_irqsave(&ring->lock, flags); 164 if (header->cons == header->prod) { 165 header->notify_on_prod = header->cons + 1; 166 spin_unlock_irqrestore(&ring->lock, flags); 167 return false; 168 } 169 170 idx = header->cons & (ring->n_elements - 1); 171 ring_elt = ring->ring->elements + idx * ring->element_size; 172 173 memcpy(element, (void *)ring_elt, ring->element_size); 174 175 header->cons++; 176 177 spin_unlock_irqrestore(&ring->lock, flags); 178 return true; 179 } 180 181 int 182 qxl_push_command_ring_release(struct qxl_device *qdev, struct qxl_release *release, 183 uint32_t type, bool interruptible) 184 { 185 struct qxl_command cmd; 186 187 cmd.type = type; 188 cmd.data = qxl_bo_physical_address(qdev, release->release_bo, release->release_offset); 189 190 return qxl_ring_push(qdev->command_ring, &cmd, interruptible); 191 } 192 193 int 194 qxl_push_cursor_ring_release(struct qxl_device *qdev, struct qxl_release *release, 195 uint32_t type, bool interruptible) 196 { 197 struct qxl_command cmd; 198 199 cmd.type = type; 200 cmd.data = qxl_bo_physical_address(qdev, release->release_bo, release->release_offset); 201 202 return qxl_ring_push(qdev->cursor_ring, &cmd, interruptible); 203 } 204 205 bool qxl_queue_garbage_collect(struct qxl_device *qdev, bool flush) 206 { 207 if (!qxl_check_idle(qdev->release_ring)) { 208 schedule_work(&qdev->gc_work); 209 if (flush) 210 flush_work(&qdev->gc_work); 211 return true; 212 } 213 return false; 214 } 215 216 int qxl_garbage_collect(struct qxl_device *qdev) 217 { 218 struct qxl_release *release; 219 uint64_t id, next_id; 220 int i = 0; 221 union qxl_release_info *info; 222 223 while (qxl_ring_pop(qdev->release_ring, &id)) { 224 DRM_DEBUG_DRIVER("popped %lld\n", id); 225 while (id) { 226 release = qxl_release_from_id_locked(qdev, id); 227 if (release == NULL) 228 break; 229 230 info = qxl_release_map(qdev, release); 231 next_id = info->next; 232 qxl_release_unmap(qdev, release, info); 233 234 DRM_DEBUG_DRIVER("popped %lld, next %lld\n", id, 235 next_id); 236 237 switch (release->type) { 238 case QXL_RELEASE_DRAWABLE: 239 case QXL_RELEASE_SURFACE_CMD: 240 case QXL_RELEASE_CURSOR_CMD: 241 break; 242 default: 243 DRM_ERROR("unexpected release type\n"); 244 break; 245 } 246 id = next_id; 247 248 qxl_release_free(qdev, release); 249 ++i; 250 } 251 } 252 253 DRM_DEBUG_DRIVER("%d\n", i); 254 255 return i; 256 } 257 258 int qxl_alloc_bo_reserved(struct qxl_device *qdev, 259 struct qxl_release *release, 260 unsigned long size, 261 struct qxl_bo **_bo) 262 { 263 struct qxl_bo *bo; 264 int ret; 265 266 ret = qxl_bo_create(qdev, size, false /* not kernel - device */, 267 false, QXL_GEM_DOMAIN_VRAM, NULL, &bo); 268 if (ret) { 269 DRM_ERROR("failed to allocate VRAM BO\n"); 270 return ret; 271 } 272 ret = qxl_release_list_add(release, bo); 273 if (ret) 274 goto out_unref; 275 276 *_bo = bo; 277 return 0; 278 out_unref: 279 qxl_bo_unref(&bo); 280 return ret; 281 } 282 283 static int wait_for_io_cmd_user(struct qxl_device *qdev, uint8_t val, long port, bool intr) 284 { 285 int irq_num; 286 long addr = qdev->io_base + port; 287 int ret; 288 289 mutex_lock(&qdev->async_io_mutex); 290 irq_num = atomic_read(&qdev->irq_received_io_cmd); 291 if (qdev->last_sent_io_cmd > irq_num) { 292 if (intr) 293 ret = wait_event_interruptible_timeout(qdev->io_cmd_event, 294 atomic_read(&qdev->irq_received_io_cmd) > irq_num, 5*HZ); 295 else 296 ret = wait_event_timeout(qdev->io_cmd_event, 297 atomic_read(&qdev->irq_received_io_cmd) > irq_num, 5*HZ); 298 /* 0 is timeout, just bail the "hw" has gone away */ 299 if (ret <= 0) 300 goto out; 301 irq_num = atomic_read(&qdev->irq_received_io_cmd); 302 } 303 outb(val, addr); 304 qdev->last_sent_io_cmd = irq_num + 1; 305 if (intr) 306 ret = wait_event_interruptible_timeout(qdev->io_cmd_event, 307 atomic_read(&qdev->irq_received_io_cmd) > irq_num, 5*HZ); 308 else 309 ret = wait_event_timeout(qdev->io_cmd_event, 310 atomic_read(&qdev->irq_received_io_cmd) > irq_num, 5*HZ); 311 out: 312 if (ret > 0) 313 ret = 0; 314 mutex_unlock(&qdev->async_io_mutex); 315 return ret; 316 } 317 318 static void wait_for_io_cmd(struct qxl_device *qdev, uint8_t val, long port) 319 { 320 int ret; 321 322 restart: 323 ret = wait_for_io_cmd_user(qdev, val, port, false); 324 if (ret == -ERESTARTSYS) 325 goto restart; 326 } 327 328 int qxl_io_update_area(struct qxl_device *qdev, struct qxl_bo *surf, 329 const struct qxl_rect *area) 330 { 331 int surface_id; 332 uint32_t surface_width, surface_height; 333 int ret; 334 335 if (!surf->hw_surf_alloc) 336 DRM_ERROR("got io update area with no hw surface\n"); 337 338 if (surf->is_primary) 339 surface_id = 0; 340 else 341 surface_id = surf->surface_id; 342 surface_width = surf->surf.width; 343 surface_height = surf->surf.height; 344 345 if (area->left < 0 || area->top < 0 || 346 area->right > surface_width || area->bottom > surface_height) 347 return -EINVAL; 348 349 mutex_lock(&qdev->update_area_mutex); 350 qdev->ram_header->update_area = *area; 351 qdev->ram_header->update_surface = surface_id; 352 ret = wait_for_io_cmd_user(qdev, 0, QXL_IO_UPDATE_AREA_ASYNC, true); 353 mutex_unlock(&qdev->update_area_mutex); 354 return ret; 355 } 356 357 void qxl_io_notify_oom(struct qxl_device *qdev) 358 { 359 outb(0, qdev->io_base + QXL_IO_NOTIFY_OOM); 360 } 361 362 void qxl_io_flush_release(struct qxl_device *qdev) 363 { 364 outb(0, qdev->io_base + QXL_IO_FLUSH_RELEASE); 365 } 366 367 void qxl_io_flush_surfaces(struct qxl_device *qdev) 368 { 369 wait_for_io_cmd(qdev, 0, QXL_IO_FLUSH_SURFACES_ASYNC); 370 } 371 372 void qxl_io_destroy_primary(struct qxl_device *qdev) 373 { 374 wait_for_io_cmd(qdev, 0, QXL_IO_DESTROY_PRIMARY_ASYNC); 375 qdev->primary_created = false; 376 } 377 378 void qxl_io_create_primary(struct qxl_device *qdev, 379 unsigned int offset, struct qxl_bo *bo) 380 { 381 struct qxl_surface_create *create; 382 383 DRM_DEBUG_DRIVER("qdev %p, ram_header %p\n", qdev, qdev->ram_header); 384 create = &qdev->ram_header->create_surface; 385 create->format = bo->surf.format; 386 create->width = bo->surf.width; 387 create->height = bo->surf.height; 388 create->stride = bo->surf.stride; 389 if (bo->shadow) { 390 create->mem = qxl_bo_physical_address(qdev, bo->shadow, offset); 391 } else { 392 create->mem = qxl_bo_physical_address(qdev, bo, offset); 393 } 394 395 DRM_DEBUG_DRIVER("mem = %llx, from %p\n", create->mem, bo->kptr); 396 397 create->flags = QXL_SURF_FLAG_KEEP_DATA; 398 create->type = QXL_SURF_TYPE_PRIMARY; 399 400 wait_for_io_cmd(qdev, 0, QXL_IO_CREATE_PRIMARY_ASYNC); 401 qdev->primary_created = true; 402 } 403 404 void qxl_io_memslot_add(struct qxl_device *qdev, uint8_t id) 405 { 406 DRM_DEBUG_DRIVER("qxl_memslot_add %d\n", id); 407 wait_for_io_cmd(qdev, id, QXL_IO_MEMSLOT_ADD_ASYNC); 408 } 409 410 void qxl_io_reset(struct qxl_device *qdev) 411 { 412 outb(0, qdev->io_base + QXL_IO_RESET); 413 } 414 415 void qxl_io_monitors_config(struct qxl_device *qdev) 416 { 417 wait_for_io_cmd(qdev, 0, QXL_IO_MONITORS_CONFIG_ASYNC); 418 } 419 420 int qxl_surface_id_alloc(struct qxl_device *qdev, 421 struct qxl_bo *surf) 422 { 423 uint32_t handle; 424 int idr_ret; 425 int count = 0; 426 again: 427 idr_preload(GFP_ATOMIC); 428 spin_lock(&qdev->surf_id_idr_lock); 429 idr_ret = idr_alloc(&qdev->surf_id_idr, NULL, 1, 0, GFP_NOWAIT); 430 spin_unlock(&qdev->surf_id_idr_lock); 431 idr_preload_end(); 432 if (idr_ret < 0) 433 return idr_ret; 434 handle = idr_ret; 435 436 if (handle >= qdev->rom->n_surfaces) { 437 count++; 438 spin_lock(&qdev->surf_id_idr_lock); 439 idr_remove(&qdev->surf_id_idr, handle); 440 spin_unlock(&qdev->surf_id_idr_lock); 441 qxl_reap_surface_id(qdev, 2); 442 goto again; 443 } 444 surf->surface_id = handle; 445 446 spin_lock(&qdev->surf_id_idr_lock); 447 qdev->last_alloced_surf_id = handle; 448 spin_unlock(&qdev->surf_id_idr_lock); 449 return 0; 450 } 451 452 void qxl_surface_id_dealloc(struct qxl_device *qdev, 453 uint32_t surface_id) 454 { 455 spin_lock(&qdev->surf_id_idr_lock); 456 idr_remove(&qdev->surf_id_idr, surface_id); 457 spin_unlock(&qdev->surf_id_idr_lock); 458 } 459 460 int qxl_hw_surface_alloc(struct qxl_device *qdev, 461 struct qxl_bo *surf, 462 struct ttm_mem_reg *new_mem) 463 { 464 struct qxl_surface_cmd *cmd; 465 struct qxl_release *release; 466 int ret; 467 468 if (surf->hw_surf_alloc) 469 return 0; 470 471 ret = qxl_alloc_surface_release_reserved(qdev, QXL_SURFACE_CMD_CREATE, 472 NULL, 473 &release); 474 if (ret) 475 return ret; 476 477 ret = qxl_release_reserve_list(release, true); 478 if (ret) 479 return ret; 480 481 cmd = (struct qxl_surface_cmd *)qxl_release_map(qdev, release); 482 cmd->type = QXL_SURFACE_CMD_CREATE; 483 cmd->flags = QXL_SURF_FLAG_KEEP_DATA; 484 cmd->u.surface_create.format = surf->surf.format; 485 cmd->u.surface_create.width = surf->surf.width; 486 cmd->u.surface_create.height = surf->surf.height; 487 cmd->u.surface_create.stride = surf->surf.stride; 488 if (new_mem) { 489 int slot_id = surf->type == QXL_GEM_DOMAIN_VRAM ? qdev->main_mem_slot : qdev->surfaces_mem_slot; 490 struct qxl_memslot *slot = &(qdev->mem_slots[slot_id]); 491 492 /* TODO - need to hold one of the locks to read tbo.offset */ 493 cmd->u.surface_create.data = slot->high_bits; 494 495 cmd->u.surface_create.data |= (new_mem->start << PAGE_SHIFT) + surf->tbo.bdev->man[new_mem->mem_type].gpu_offset; 496 } else 497 cmd->u.surface_create.data = qxl_bo_physical_address(qdev, surf, 0); 498 cmd->surface_id = surf->surface_id; 499 qxl_release_unmap(qdev, release, &cmd->release_info); 500 501 surf->surf_create = release; 502 503 /* no need to add a release to the fence for this surface bo, 504 since it is only released when we ask to destroy the surface 505 and it would never signal otherwise */ 506 qxl_push_command_ring_release(qdev, release, QXL_CMD_SURFACE, false); 507 qxl_release_fence_buffer_objects(release); 508 509 surf->hw_surf_alloc = true; 510 spin_lock(&qdev->surf_id_idr_lock); 511 idr_replace(&qdev->surf_id_idr, surf, surf->surface_id); 512 spin_unlock(&qdev->surf_id_idr_lock); 513 return 0; 514 } 515 516 int qxl_hw_surface_dealloc(struct qxl_device *qdev, 517 struct qxl_bo *surf) 518 { 519 struct qxl_surface_cmd *cmd; 520 struct qxl_release *release; 521 int ret; 522 int id; 523 524 if (!surf->hw_surf_alloc) 525 return 0; 526 527 ret = qxl_alloc_surface_release_reserved(qdev, QXL_SURFACE_CMD_DESTROY, 528 surf->surf_create, 529 &release); 530 if (ret) 531 return ret; 532 533 surf->surf_create = NULL; 534 /* remove the surface from the idr, but not the surface id yet */ 535 spin_lock(&qdev->surf_id_idr_lock); 536 idr_replace(&qdev->surf_id_idr, NULL, surf->surface_id); 537 spin_unlock(&qdev->surf_id_idr_lock); 538 surf->hw_surf_alloc = false; 539 540 id = surf->surface_id; 541 surf->surface_id = 0; 542 543 release->surface_release_id = id; 544 cmd = (struct qxl_surface_cmd *)qxl_release_map(qdev, release); 545 cmd->type = QXL_SURFACE_CMD_DESTROY; 546 cmd->surface_id = id; 547 qxl_release_unmap(qdev, release, &cmd->release_info); 548 549 qxl_push_command_ring_release(qdev, release, QXL_CMD_SURFACE, false); 550 551 qxl_release_fence_buffer_objects(release); 552 553 return 0; 554 } 555 556 static int qxl_update_surface(struct qxl_device *qdev, struct qxl_bo *surf) 557 { 558 struct qxl_rect rect; 559 int ret; 560 561 /* if we are evicting, we need to make sure the surface is up 562 to date */ 563 rect.left = 0; 564 rect.right = surf->surf.width; 565 rect.top = 0; 566 rect.bottom = surf->surf.height; 567 retry: 568 ret = qxl_io_update_area(qdev, surf, &rect); 569 if (ret == -ERESTARTSYS) 570 goto retry; 571 return ret; 572 } 573 574 static void qxl_surface_evict_locked(struct qxl_device *qdev, struct qxl_bo *surf, bool do_update_area) 575 { 576 /* no need to update area if we are just freeing the surface normally */ 577 if (do_update_area) 578 qxl_update_surface(qdev, surf); 579 580 /* nuke the surface id at the hw */ 581 qxl_hw_surface_dealloc(qdev, surf); 582 } 583 584 void qxl_surface_evict(struct qxl_device *qdev, struct qxl_bo *surf, bool do_update_area) 585 { 586 mutex_lock(&qdev->surf_evict_mutex); 587 qxl_surface_evict_locked(qdev, surf, do_update_area); 588 mutex_unlock(&qdev->surf_evict_mutex); 589 } 590 591 static int qxl_reap_surf(struct qxl_device *qdev, struct qxl_bo *surf, bool stall) 592 { 593 int ret; 594 595 ret = qxl_bo_reserve(surf, false); 596 if (ret) 597 return ret; 598 599 if (stall) 600 mutex_unlock(&qdev->surf_evict_mutex); 601 602 ret = ttm_bo_wait(&surf->tbo, true, !stall); 603 604 if (stall) 605 mutex_lock(&qdev->surf_evict_mutex); 606 if (ret) { 607 qxl_bo_unreserve(surf); 608 return ret; 609 } 610 611 qxl_surface_evict_locked(qdev, surf, true); 612 qxl_bo_unreserve(surf); 613 return 0; 614 } 615 616 static int qxl_reap_surface_id(struct qxl_device *qdev, int max_to_reap) 617 { 618 int num_reaped = 0; 619 int i, ret; 620 bool stall = false; 621 int start = 0; 622 623 mutex_lock(&qdev->surf_evict_mutex); 624 again: 625 626 spin_lock(&qdev->surf_id_idr_lock); 627 start = qdev->last_alloced_surf_id + 1; 628 spin_unlock(&qdev->surf_id_idr_lock); 629 630 for (i = start; i < start + qdev->rom->n_surfaces; i++) { 631 void *objptr; 632 int surfid = i % qdev->rom->n_surfaces; 633 634 /* this avoids the case where the objects is in the 635 idr but has been evicted half way - its makes 636 the idr lookup atomic with the eviction */ 637 spin_lock(&qdev->surf_id_idr_lock); 638 objptr = idr_find(&qdev->surf_id_idr, surfid); 639 spin_unlock(&qdev->surf_id_idr_lock); 640 641 if (!objptr) 642 continue; 643 644 ret = qxl_reap_surf(qdev, objptr, stall); 645 if (ret == 0) 646 num_reaped++; 647 if (num_reaped >= max_to_reap) 648 break; 649 } 650 if (num_reaped == 0 && stall == false) { 651 stall = true; 652 goto again; 653 } 654 655 mutex_unlock(&qdev->surf_evict_mutex); 656 if (num_reaped) { 657 usleep_range(500, 1000); 658 qxl_queue_garbage_collect(qdev, true); 659 } 660 661 return 0; 662 } 663