1 // SPDX-License-Identifier: GPL-2.0 OR MIT 2 /************************************************************************** 3 * 4 * Copyright 2015-2023 VMware, Inc., Palo Alto, CA., USA 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 24 * USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28 #include "vmwgfx_bo.h" 29 #include "vmwgfx_drv.h" 30 31 #include <drm/ttm/ttm_bo.h> 32 33 #include <linux/dmapool.h> 34 #include <linux/pci.h> 35 36 /* 37 * Size of inline command buffers. Try to make sure that a page size is a 38 * multiple of the DMA pool allocation size. 39 */ 40 #define VMW_CMDBUF_INLINE_ALIGN 64 41 #define VMW_CMDBUF_INLINE_SIZE \ 42 (1024 - ALIGN(sizeof(SVGACBHeader), VMW_CMDBUF_INLINE_ALIGN)) 43 44 /** 45 * struct vmw_cmdbuf_context - Command buffer context queues 46 * 47 * @submitted: List of command buffers that have been submitted to the 48 * manager but not yet submitted to hardware. 49 * @hw_submitted: List of command buffers submitted to hardware. 50 * @preempted: List of preempted command buffers. 51 * @num_hw_submitted: Number of buffers currently being processed by hardware 52 * @block_submission: Identifies a block command submission. 53 */ 54 struct vmw_cmdbuf_context { 55 struct list_head submitted; 56 struct list_head hw_submitted; 57 struct list_head preempted; 58 unsigned num_hw_submitted; 59 bool block_submission; 60 }; 61 62 /** 63 * struct vmw_cmdbuf_man - Command buffer manager 64 * 65 * @cur_mutex: Mutex protecting the command buffer used for incremental small 66 * kernel command submissions, @cur. 67 * @space_mutex: Mutex to protect against starvation when we allocate 68 * main pool buffer space. 69 * @error_mutex: Mutex to serialize the work queue error handling. 70 * Note this is not needed if the same workqueue handler 71 * can't race with itself... 72 * @work: A struct work_struct implementeing command buffer error handling. 73 * Immutable. 74 * @dev_priv: Pointer to the device private struct. Immutable. 75 * @ctx: Array of command buffer context queues. The queues and the context 76 * data is protected by @lock. 77 * @error: List of command buffers that have caused device errors. 78 * Protected by @lock. 79 * @mm: Range manager for the command buffer space. Manager allocations and 80 * frees are protected by @lock. 81 * @cmd_space: Buffer object for the command buffer space, unless we were 82 * able to make a contigous coherent DMA memory allocation, @handle. Immutable. 83 * @map: Pointer to command buffer space. May be a mapped buffer object or 84 * a contigous coherent DMA memory allocation. Immutable. 85 * @cur: Command buffer for small kernel command submissions. Protected by 86 * the @cur_mutex. 87 * @cur_pos: Space already used in @cur. Protected by @cur_mutex. 88 * @default_size: Default size for the @cur command buffer. Immutable. 89 * @max_hw_submitted: Max number of in-flight command buffers the device can 90 * handle. Immutable. 91 * @lock: Spinlock protecting command submission queues. 92 * @headers: Pool of DMA memory for device command buffer headers. 93 * Internal protection. 94 * @dheaders: Pool of DMA memory for device command buffer headers with trailing 95 * space for inline data. Internal protection. 96 * @alloc_queue: Wait queue for processes waiting to allocate command buffer 97 * space. 98 * @idle_queue: Wait queue for processes waiting for command buffer idle. 99 * @irq_on: Whether the process function has requested irq to be turned on. 100 * Protected by @lock. 101 * @using_mob: Whether the command buffer space is a MOB or a contigous DMA 102 * allocation. Immutable. 103 * @has_pool: Has a large pool of DMA memory which allows larger allocations. 104 * Typically this is false only during bootstrap. 105 * @handle: DMA address handle for the command buffer space if @using_mob is 106 * false. Immutable. 107 * @size: The size of the command buffer space. Immutable. 108 * @id: Monotonically increasing ID of the last cmdbuf submitted. 109 * @num_contexts: Number of contexts actually enabled. 110 */ 111 struct vmw_cmdbuf_man { 112 struct mutex cur_mutex; 113 struct mutex space_mutex; 114 struct mutex error_mutex; 115 struct work_struct work; 116 struct vmw_private *dev_priv; 117 struct vmw_cmdbuf_context ctx[SVGA_CB_CONTEXT_MAX]; 118 struct list_head error; 119 struct drm_mm mm; 120 struct vmw_bo *cmd_space; 121 u8 *map; 122 struct vmw_cmdbuf_header *cur; 123 size_t cur_pos; 124 size_t default_size; 125 unsigned max_hw_submitted; 126 spinlock_t lock; 127 struct dma_pool *headers; 128 struct dma_pool *dheaders; 129 wait_queue_head_t alloc_queue; 130 wait_queue_head_t idle_queue; 131 bool irq_on; 132 bool using_mob; 133 bool has_pool; 134 dma_addr_t handle; 135 size_t size; 136 u64 id; 137 u32 num_contexts; 138 }; 139 140 /** 141 * struct vmw_cmdbuf_header - Command buffer metadata 142 * 143 * @man: The command buffer manager. 144 * @cb_header: Device command buffer header, allocated from a DMA pool. 145 * @cb_context: The device command buffer context. 146 * @list: List head for attaching to the manager lists. 147 * @node: The range manager node. 148 * @handle: The DMA address of @cb_header. Handed to the device on command 149 * buffer submission. 150 * @cmd: Pointer to the command buffer space of this buffer. 151 * @size: Size of the command buffer space of this buffer. 152 * @reserved: Reserved space of this buffer. 153 * @inline_space: Whether inline command buffer space is used. 154 */ 155 struct vmw_cmdbuf_header { 156 struct vmw_cmdbuf_man *man; 157 SVGACBHeader *cb_header; 158 SVGACBContext cb_context; 159 struct list_head list; 160 struct drm_mm_node node; 161 dma_addr_t handle; 162 u8 *cmd; 163 size_t size; 164 size_t reserved; 165 bool inline_space; 166 }; 167 168 /** 169 * struct vmw_cmdbuf_dheader - Device command buffer header with inline 170 * command buffer space. 171 * 172 * @cb_header: Device command buffer header. 173 * @cmd: Inline command buffer space. 174 */ 175 struct vmw_cmdbuf_dheader { 176 SVGACBHeader cb_header; 177 u8 cmd[VMW_CMDBUF_INLINE_SIZE] __aligned(VMW_CMDBUF_INLINE_ALIGN); 178 }; 179 180 /** 181 * struct vmw_cmdbuf_alloc_info - Command buffer space allocation metadata 182 * 183 * @page_size: Size of requested command buffer space in pages. 184 * @node: Pointer to the range manager node. 185 * @done: True if this allocation has succeeded. 186 */ 187 struct vmw_cmdbuf_alloc_info { 188 size_t page_size; 189 struct drm_mm_node *node; 190 bool done; 191 }; 192 193 /* Loop over each context in the command buffer manager. */ 194 #define for_each_cmdbuf_ctx(_man, _i, _ctx) \ 195 for (_i = 0, _ctx = &(_man)->ctx[0]; (_i) < (_man)->num_contexts; \ 196 ++(_i), ++(_ctx)) 197 198 static int vmw_cmdbuf_startstop(struct vmw_cmdbuf_man *man, u32 context, 199 bool enable); 200 static int vmw_cmdbuf_preempt(struct vmw_cmdbuf_man *man, u32 context); 201 202 /** 203 * vmw_cmdbuf_cur_lock - Helper to lock the cur_mutex. 204 * 205 * @man: The range manager. 206 * @interruptible: Whether to wait interruptible when locking. 207 */ 208 static int vmw_cmdbuf_cur_lock(struct vmw_cmdbuf_man *man, bool interruptible) 209 { 210 if (interruptible) { 211 if (mutex_lock_interruptible(&man->cur_mutex)) 212 return -ERESTARTSYS; 213 } else { 214 mutex_lock(&man->cur_mutex); 215 } 216 217 return 0; 218 } 219 220 /** 221 * vmw_cmdbuf_cur_unlock - Helper to unlock the cur_mutex. 222 * 223 * @man: The range manager. 224 */ 225 static void vmw_cmdbuf_cur_unlock(struct vmw_cmdbuf_man *man) 226 { 227 mutex_unlock(&man->cur_mutex); 228 } 229 230 /** 231 * vmw_cmdbuf_header_inline_free - Free a struct vmw_cmdbuf_header that has 232 * been used for the device context with inline command buffers. 233 * Need not be called locked. 234 * 235 * @header: Pointer to the header to free. 236 */ 237 static void vmw_cmdbuf_header_inline_free(struct vmw_cmdbuf_header *header) 238 { 239 struct vmw_cmdbuf_dheader *dheader; 240 241 if (WARN_ON_ONCE(!header->inline_space)) 242 return; 243 244 dheader = container_of(header->cb_header, struct vmw_cmdbuf_dheader, 245 cb_header); 246 dma_pool_free(header->man->dheaders, dheader, header->handle); 247 kfree(header); 248 } 249 250 /** 251 * __vmw_cmdbuf_header_free - Free a struct vmw_cmdbuf_header and its 252 * associated structures. 253 * 254 * @header: Pointer to the header to free. 255 * 256 * For internal use. Must be called with man::lock held. 257 */ 258 static void __vmw_cmdbuf_header_free(struct vmw_cmdbuf_header *header) 259 { 260 struct vmw_cmdbuf_man *man = header->man; 261 262 lockdep_assert_held_once(&man->lock); 263 264 if (header->inline_space) { 265 vmw_cmdbuf_header_inline_free(header); 266 return; 267 } 268 269 drm_mm_remove_node(&header->node); 270 wake_up_all(&man->alloc_queue); 271 if (header->cb_header) 272 dma_pool_free(man->headers, header->cb_header, 273 header->handle); 274 kfree(header); 275 } 276 277 /** 278 * vmw_cmdbuf_header_free - Free a struct vmw_cmdbuf_header and its 279 * associated structures. 280 * 281 * @header: Pointer to the header to free. 282 */ 283 void vmw_cmdbuf_header_free(struct vmw_cmdbuf_header *header) 284 { 285 struct vmw_cmdbuf_man *man = header->man; 286 287 /* Avoid locking if inline_space */ 288 if (header->inline_space) { 289 vmw_cmdbuf_header_inline_free(header); 290 return; 291 } 292 spin_lock(&man->lock); 293 __vmw_cmdbuf_header_free(header); 294 spin_unlock(&man->lock); 295 } 296 297 298 /** 299 * vmw_cmdbuf_header_submit: Submit a command buffer to hardware. 300 * 301 * @header: The header of the buffer to submit. 302 */ 303 static int vmw_cmdbuf_header_submit(struct vmw_cmdbuf_header *header) 304 { 305 struct vmw_cmdbuf_man *man = header->man; 306 u32 val; 307 308 header->cb_header->id = man->id++; 309 310 val = upper_32_bits(header->handle); 311 vmw_write(man->dev_priv, SVGA_REG_COMMAND_HIGH, val); 312 313 val = lower_32_bits(header->handle); 314 val |= header->cb_context & SVGA_CB_CONTEXT_MASK; 315 vmw_write(man->dev_priv, SVGA_REG_COMMAND_LOW, val); 316 317 return header->cb_header->status; 318 } 319 320 /** 321 * vmw_cmdbuf_ctx_init: Initialize a command buffer context. 322 * 323 * @ctx: The command buffer context to initialize 324 */ 325 static void vmw_cmdbuf_ctx_init(struct vmw_cmdbuf_context *ctx) 326 { 327 INIT_LIST_HEAD(&ctx->hw_submitted); 328 INIT_LIST_HEAD(&ctx->submitted); 329 INIT_LIST_HEAD(&ctx->preempted); 330 ctx->num_hw_submitted = 0; 331 } 332 333 /** 334 * vmw_cmdbuf_ctx_submit: Submit command buffers from a command buffer 335 * context. 336 * 337 * @man: The command buffer manager. 338 * @ctx: The command buffer context. 339 * 340 * Submits command buffers to hardware until there are no more command 341 * buffers to submit or the hardware can't handle more command buffers. 342 */ 343 static void vmw_cmdbuf_ctx_submit(struct vmw_cmdbuf_man *man, 344 struct vmw_cmdbuf_context *ctx) 345 { 346 while (ctx->num_hw_submitted < man->max_hw_submitted && 347 !list_empty(&ctx->submitted) && 348 !ctx->block_submission) { 349 struct vmw_cmdbuf_header *entry; 350 SVGACBStatus status; 351 352 entry = list_first_entry(&ctx->submitted, 353 struct vmw_cmdbuf_header, 354 list); 355 356 status = vmw_cmdbuf_header_submit(entry); 357 358 /* This should never happen */ 359 if (WARN_ON_ONCE(status == SVGA_CB_STATUS_QUEUE_FULL)) { 360 entry->cb_header->status = SVGA_CB_STATUS_NONE; 361 break; 362 } 363 364 list_move_tail(&entry->list, &ctx->hw_submitted); 365 ctx->num_hw_submitted++; 366 } 367 368 } 369 370 /** 371 * vmw_cmdbuf_ctx_process - Process a command buffer context. 372 * 373 * @man: The command buffer manager. 374 * @ctx: The command buffer context. 375 * @notempty: Pass back count of non-empty command submitted lists. 376 * 377 * Submit command buffers to hardware if possible, and process finished 378 * buffers. Typically freeing them, but on preemption or error take 379 * appropriate action. Wake up waiters if appropriate. 380 */ 381 static void vmw_cmdbuf_ctx_process(struct vmw_cmdbuf_man *man, 382 struct vmw_cmdbuf_context *ctx, 383 int *notempty) 384 { 385 struct vmw_cmdbuf_header *entry, *next; 386 387 vmw_cmdbuf_ctx_submit(man, ctx); 388 389 list_for_each_entry_safe(entry, next, &ctx->hw_submitted, list) { 390 SVGACBStatus status = entry->cb_header->status; 391 392 if (status == SVGA_CB_STATUS_NONE) 393 break; 394 395 list_del(&entry->list); 396 wake_up_all(&man->idle_queue); 397 ctx->num_hw_submitted--; 398 switch (status) { 399 case SVGA_CB_STATUS_COMPLETED: 400 __vmw_cmdbuf_header_free(entry); 401 break; 402 case SVGA_CB_STATUS_COMMAND_ERROR: 403 WARN_ONCE(true, "Command buffer error.\n"); 404 entry->cb_header->status = SVGA_CB_STATUS_NONE; 405 list_add_tail(&entry->list, &man->error); 406 schedule_work(&man->work); 407 break; 408 case SVGA_CB_STATUS_PREEMPTED: 409 entry->cb_header->status = SVGA_CB_STATUS_NONE; 410 list_add_tail(&entry->list, &ctx->preempted); 411 break; 412 case SVGA_CB_STATUS_CB_HEADER_ERROR: 413 WARN_ONCE(true, "Command buffer header error.\n"); 414 __vmw_cmdbuf_header_free(entry); 415 break; 416 default: 417 WARN_ONCE(true, "Undefined command buffer status.\n"); 418 __vmw_cmdbuf_header_free(entry); 419 break; 420 } 421 } 422 423 vmw_cmdbuf_ctx_submit(man, ctx); 424 if (!list_empty(&ctx->submitted)) 425 (*notempty)++; 426 } 427 428 /** 429 * vmw_cmdbuf_man_process - Process all command buffer contexts and 430 * switch on and off irqs as appropriate. 431 * 432 * @man: The command buffer manager. 433 * 434 * Calls vmw_cmdbuf_ctx_process() on all contexts. If any context has 435 * command buffers left that are not submitted to hardware, Make sure 436 * IRQ handling is turned on. Otherwise, make sure it's turned off. 437 */ 438 static void vmw_cmdbuf_man_process(struct vmw_cmdbuf_man *man) 439 { 440 int notempty; 441 struct vmw_cmdbuf_context *ctx; 442 int i; 443 444 retry: 445 notempty = 0; 446 for_each_cmdbuf_ctx(man, i, ctx) 447 vmw_cmdbuf_ctx_process(man, ctx, ¬empty); 448 449 if (man->irq_on && !notempty) { 450 vmw_generic_waiter_remove(man->dev_priv, 451 SVGA_IRQFLAG_COMMAND_BUFFER, 452 &man->dev_priv->cmdbuf_waiters); 453 man->irq_on = false; 454 } else if (!man->irq_on && notempty) { 455 vmw_generic_waiter_add(man->dev_priv, 456 SVGA_IRQFLAG_COMMAND_BUFFER, 457 &man->dev_priv->cmdbuf_waiters); 458 man->irq_on = true; 459 460 /* Rerun in case we just missed an irq. */ 461 goto retry; 462 } 463 } 464 465 /** 466 * vmw_cmdbuf_ctx_add - Schedule a command buffer for submission on a 467 * command buffer context 468 * 469 * @man: The command buffer manager. 470 * @header: The header of the buffer to submit. 471 * @cb_context: The command buffer context to use. 472 * 473 * This function adds @header to the "submitted" queue of the command 474 * buffer context identified by @cb_context. It then calls the command buffer 475 * manager processing to potentially submit the buffer to hardware. 476 * @man->lock needs to be held when calling this function. 477 */ 478 static void vmw_cmdbuf_ctx_add(struct vmw_cmdbuf_man *man, 479 struct vmw_cmdbuf_header *header, 480 SVGACBContext cb_context) 481 { 482 if (!(header->cb_header->flags & SVGA_CB_FLAG_DX_CONTEXT)) 483 header->cb_header->dxContext = 0; 484 header->cb_context = cb_context; 485 list_add_tail(&header->list, &man->ctx[cb_context].submitted); 486 487 vmw_cmdbuf_man_process(man); 488 } 489 490 /** 491 * vmw_cmdbuf_irqthread - The main part of the command buffer interrupt 492 * handler implemented as a threaded irq task. 493 * 494 * @man: Pointer to the command buffer manager. 495 * 496 * The bottom half of the interrupt handler simply calls into the 497 * command buffer processor to free finished buffers and submit any 498 * queued buffers to hardware. 499 */ 500 void vmw_cmdbuf_irqthread(struct vmw_cmdbuf_man *man) 501 { 502 spin_lock(&man->lock); 503 vmw_cmdbuf_man_process(man); 504 spin_unlock(&man->lock); 505 } 506 507 /** 508 * vmw_cmdbuf_work_func - The deferred work function that handles 509 * command buffer errors. 510 * 511 * @work: The work func closure argument. 512 * 513 * Restarting the command buffer context after an error requires process 514 * context, so it is deferred to this work function. 515 */ 516 static void vmw_cmdbuf_work_func(struct work_struct *work) 517 { 518 struct vmw_cmdbuf_man *man = 519 container_of(work, struct vmw_cmdbuf_man, work); 520 struct vmw_cmdbuf_header *entry, *next; 521 uint32_t dummy = 0; 522 bool send_fence = false; 523 struct list_head restart_head[SVGA_CB_CONTEXT_MAX]; 524 int i; 525 struct vmw_cmdbuf_context *ctx; 526 bool global_block = false; 527 528 for_each_cmdbuf_ctx(man, i, ctx) 529 INIT_LIST_HEAD(&restart_head[i]); 530 531 mutex_lock(&man->error_mutex); 532 spin_lock(&man->lock); 533 list_for_each_entry_safe(entry, next, &man->error, list) { 534 SVGACBHeader *cb_hdr = entry->cb_header; 535 SVGA3dCmdHeader *header = (SVGA3dCmdHeader *) 536 (entry->cmd + cb_hdr->errorOffset); 537 u32 error_cmd_size, new_start_offset; 538 const char *cmd_name; 539 540 list_del_init(&entry->list); 541 global_block = true; 542 543 if (!vmw_cmd_describe(header, &error_cmd_size, &cmd_name)) { 544 VMW_DEBUG_USER("Unknown command causing device error.\n"); 545 VMW_DEBUG_USER("Command buffer offset is %lu\n", 546 (unsigned long) cb_hdr->errorOffset); 547 __vmw_cmdbuf_header_free(entry); 548 send_fence = true; 549 continue; 550 } 551 552 VMW_DEBUG_USER("Command \"%s\" causing device error.\n", 553 cmd_name); 554 VMW_DEBUG_USER("Command buffer offset is %lu\n", 555 (unsigned long) cb_hdr->errorOffset); 556 VMW_DEBUG_USER("Command size is %lu\n", 557 (unsigned long) error_cmd_size); 558 559 new_start_offset = cb_hdr->errorOffset + error_cmd_size; 560 561 if (new_start_offset >= cb_hdr->length) { 562 __vmw_cmdbuf_header_free(entry); 563 send_fence = true; 564 continue; 565 } 566 567 if (man->using_mob) 568 cb_hdr->ptr.mob.mobOffset += new_start_offset; 569 else 570 cb_hdr->ptr.pa += (u64) new_start_offset; 571 572 entry->cmd += new_start_offset; 573 cb_hdr->length -= new_start_offset; 574 cb_hdr->errorOffset = 0; 575 cb_hdr->offset = 0; 576 577 list_add_tail(&entry->list, &restart_head[entry->cb_context]); 578 } 579 580 for_each_cmdbuf_ctx(man, i, ctx) 581 man->ctx[i].block_submission = true; 582 583 spin_unlock(&man->lock); 584 585 /* Preempt all contexts */ 586 if (global_block && vmw_cmdbuf_preempt(man, 0)) 587 DRM_ERROR("Failed preempting command buffer contexts\n"); 588 589 spin_lock(&man->lock); 590 for_each_cmdbuf_ctx(man, i, ctx) { 591 /* Move preempted command buffers to the preempted queue. */ 592 vmw_cmdbuf_ctx_process(man, ctx, &dummy); 593 594 /* 595 * Add the preempted queue after the command buffer 596 * that caused an error. 597 */ 598 list_splice_init(&ctx->preempted, restart_head[i].prev); 599 600 /* 601 * Finally add all command buffers first in the submitted 602 * queue, to rerun them. 603 */ 604 605 ctx->block_submission = false; 606 list_splice_init(&restart_head[i], &ctx->submitted); 607 } 608 609 vmw_cmdbuf_man_process(man); 610 spin_unlock(&man->lock); 611 612 if (global_block && vmw_cmdbuf_startstop(man, 0, true)) 613 DRM_ERROR("Failed restarting command buffer contexts\n"); 614 615 /* Send a new fence in case one was removed */ 616 if (send_fence) { 617 vmw_cmd_send_fence(man->dev_priv, &dummy); 618 wake_up_all(&man->idle_queue); 619 } 620 621 mutex_unlock(&man->error_mutex); 622 } 623 624 /** 625 * vmw_cmdbuf_man_idle - Check whether the command buffer manager is idle. 626 * 627 * @man: The command buffer manager. 628 * @check_preempted: Check also the preempted queue for pending command buffers. 629 * 630 */ 631 static bool vmw_cmdbuf_man_idle(struct vmw_cmdbuf_man *man, 632 bool check_preempted) 633 { 634 struct vmw_cmdbuf_context *ctx; 635 bool idle = false; 636 int i; 637 638 spin_lock(&man->lock); 639 vmw_cmdbuf_man_process(man); 640 for_each_cmdbuf_ctx(man, i, ctx) { 641 if (!list_empty(&ctx->submitted) || 642 !list_empty(&ctx->hw_submitted) || 643 (check_preempted && !list_empty(&ctx->preempted))) 644 goto out_unlock; 645 } 646 647 idle = list_empty(&man->error); 648 649 out_unlock: 650 spin_unlock(&man->lock); 651 652 return idle; 653 } 654 655 /** 656 * __vmw_cmdbuf_cur_flush - Flush the current command buffer for small kernel 657 * command submissions 658 * 659 * @man: The command buffer manager. 660 * 661 * Flushes the current command buffer without allocating a new one. A new one 662 * is automatically allocated when needed. Call with @man->cur_mutex held. 663 */ 664 static void __vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man *man) 665 { 666 struct vmw_cmdbuf_header *cur = man->cur; 667 668 lockdep_assert_held_once(&man->cur_mutex); 669 670 if (!cur) 671 return; 672 673 spin_lock(&man->lock); 674 if (man->cur_pos == 0) { 675 __vmw_cmdbuf_header_free(cur); 676 goto out_unlock; 677 } 678 679 man->cur->cb_header->length = man->cur_pos; 680 vmw_cmdbuf_ctx_add(man, man->cur, SVGA_CB_CONTEXT_0); 681 out_unlock: 682 spin_unlock(&man->lock); 683 man->cur = NULL; 684 man->cur_pos = 0; 685 } 686 687 /** 688 * vmw_cmdbuf_cur_flush - Flush the current command buffer for small kernel 689 * command submissions 690 * 691 * @man: The command buffer manager. 692 * @interruptible: Whether to sleep interruptible when sleeping. 693 * 694 * Flushes the current command buffer without allocating a new one. A new one 695 * is automatically allocated when needed. 696 */ 697 int vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man *man, 698 bool interruptible) 699 { 700 int ret = vmw_cmdbuf_cur_lock(man, interruptible); 701 702 if (ret) 703 return ret; 704 705 __vmw_cmdbuf_cur_flush(man); 706 vmw_cmdbuf_cur_unlock(man); 707 708 return 0; 709 } 710 711 /** 712 * vmw_cmdbuf_idle - Wait for command buffer manager idle. 713 * 714 * @man: The command buffer manager. 715 * @interruptible: Sleep interruptible while waiting. 716 * @timeout: Time out after this many ticks. 717 * 718 * Wait until the command buffer manager has processed all command buffers, 719 * or until a timeout occurs. If a timeout occurs, the function will return 720 * -EBUSY. 721 */ 722 int vmw_cmdbuf_idle(struct vmw_cmdbuf_man *man, bool interruptible, 723 unsigned long timeout) 724 { 725 int ret; 726 727 ret = vmw_cmdbuf_cur_flush(man, interruptible); 728 vmw_generic_waiter_add(man->dev_priv, 729 SVGA_IRQFLAG_COMMAND_BUFFER, 730 &man->dev_priv->cmdbuf_waiters); 731 732 if (interruptible) { 733 ret = wait_event_interruptible_timeout 734 (man->idle_queue, vmw_cmdbuf_man_idle(man, true), 735 timeout); 736 } else { 737 ret = wait_event_timeout 738 (man->idle_queue, vmw_cmdbuf_man_idle(man, true), 739 timeout); 740 } 741 vmw_generic_waiter_remove(man->dev_priv, 742 SVGA_IRQFLAG_COMMAND_BUFFER, 743 &man->dev_priv->cmdbuf_waiters); 744 if (ret == 0) { 745 if (!vmw_cmdbuf_man_idle(man, true)) 746 ret = -EBUSY; 747 else 748 ret = 0; 749 } 750 if (ret > 0) 751 ret = 0; 752 753 return ret; 754 } 755 756 /** 757 * vmw_cmdbuf_try_alloc - Try to allocate buffer space from the main pool. 758 * 759 * @man: The command buffer manager. 760 * @info: Allocation info. Will hold the size on entry and allocated mm node 761 * on successful return. 762 * 763 * Try to allocate buffer space from the main pool. Returns true if succeeded. 764 * If a fatal error was hit, the error code is returned in @info->ret. 765 */ 766 static bool vmw_cmdbuf_try_alloc(struct vmw_cmdbuf_man *man, 767 struct vmw_cmdbuf_alloc_info *info) 768 { 769 int ret; 770 771 if (info->done) 772 return true; 773 774 memset(info->node, 0, sizeof(*info->node)); 775 spin_lock(&man->lock); 776 ret = drm_mm_insert_node(&man->mm, info->node, info->page_size); 777 if (ret) { 778 vmw_cmdbuf_man_process(man); 779 ret = drm_mm_insert_node(&man->mm, info->node, info->page_size); 780 } 781 782 spin_unlock(&man->lock); 783 info->done = !ret; 784 785 return info->done; 786 } 787 788 /** 789 * vmw_cmdbuf_alloc_space - Allocate buffer space from the main pool. 790 * 791 * @man: The command buffer manager. 792 * @node: Pointer to pre-allocated range-manager node. 793 * @size: The size of the allocation. 794 * @interruptible: Whether to sleep interruptible while waiting for space. 795 * 796 * This function allocates buffer space from the main pool, and if there is 797 * no space available ATM, it turns on IRQ handling and sleeps waiting for it to 798 * become available. 799 */ 800 static int vmw_cmdbuf_alloc_space(struct vmw_cmdbuf_man *man, 801 struct drm_mm_node *node, 802 size_t size, 803 bool interruptible) 804 { 805 struct vmw_cmdbuf_alloc_info info; 806 807 info.page_size = PFN_UP(size); 808 info.node = node; 809 info.done = false; 810 811 /* 812 * To prevent starvation of large requests, only one allocating call 813 * at a time waiting for space. 814 */ 815 if (interruptible) { 816 if (mutex_lock_interruptible(&man->space_mutex)) 817 return -ERESTARTSYS; 818 } else { 819 mutex_lock(&man->space_mutex); 820 } 821 822 /* Try to allocate space without waiting. */ 823 if (vmw_cmdbuf_try_alloc(man, &info)) 824 goto out_unlock; 825 826 vmw_generic_waiter_add(man->dev_priv, 827 SVGA_IRQFLAG_COMMAND_BUFFER, 828 &man->dev_priv->cmdbuf_waiters); 829 830 if (interruptible) { 831 int ret; 832 833 ret = wait_event_interruptible 834 (man->alloc_queue, vmw_cmdbuf_try_alloc(man, &info)); 835 if (ret) { 836 vmw_generic_waiter_remove 837 (man->dev_priv, SVGA_IRQFLAG_COMMAND_BUFFER, 838 &man->dev_priv->cmdbuf_waiters); 839 mutex_unlock(&man->space_mutex); 840 return ret; 841 } 842 } else { 843 wait_event(man->alloc_queue, vmw_cmdbuf_try_alloc(man, &info)); 844 } 845 vmw_generic_waiter_remove(man->dev_priv, 846 SVGA_IRQFLAG_COMMAND_BUFFER, 847 &man->dev_priv->cmdbuf_waiters); 848 849 out_unlock: 850 mutex_unlock(&man->space_mutex); 851 852 return 0; 853 } 854 855 /** 856 * vmw_cmdbuf_space_pool - Set up a command buffer header with command buffer 857 * space from the main pool. 858 * 859 * @man: The command buffer manager. 860 * @header: Pointer to the header to set up. 861 * @size: The requested size of the buffer space. 862 * @interruptible: Whether to sleep interruptible while waiting for space. 863 */ 864 static int vmw_cmdbuf_space_pool(struct vmw_cmdbuf_man *man, 865 struct vmw_cmdbuf_header *header, 866 size_t size, 867 bool interruptible) 868 { 869 SVGACBHeader *cb_hdr; 870 size_t offset; 871 int ret; 872 873 if (!man->has_pool) 874 return -ENOMEM; 875 876 ret = vmw_cmdbuf_alloc_space(man, &header->node, size, interruptible); 877 878 if (ret) 879 return ret; 880 881 header->cb_header = dma_pool_zalloc(man->headers, GFP_KERNEL, 882 &header->handle); 883 if (!header->cb_header) { 884 ret = -ENOMEM; 885 goto out_no_cb_header; 886 } 887 888 header->size = header->node.size << PAGE_SHIFT; 889 cb_hdr = header->cb_header; 890 offset = header->node.start << PAGE_SHIFT; 891 header->cmd = man->map + offset; 892 if (man->using_mob) { 893 cb_hdr->flags = SVGA_CB_FLAG_MOB; 894 cb_hdr->ptr.mob.mobid = man->cmd_space->tbo.resource->start; 895 cb_hdr->ptr.mob.mobOffset = offset; 896 } else { 897 cb_hdr->ptr.pa = (u64)man->handle + (u64)offset; 898 } 899 900 return 0; 901 902 out_no_cb_header: 903 spin_lock(&man->lock); 904 drm_mm_remove_node(&header->node); 905 spin_unlock(&man->lock); 906 907 return ret; 908 } 909 910 /** 911 * vmw_cmdbuf_space_inline - Set up a command buffer header with 912 * inline command buffer space. 913 * 914 * @man: The command buffer manager. 915 * @header: Pointer to the header to set up. 916 * @size: The requested size of the buffer space. 917 */ 918 static int vmw_cmdbuf_space_inline(struct vmw_cmdbuf_man *man, 919 struct vmw_cmdbuf_header *header, 920 int size) 921 { 922 struct vmw_cmdbuf_dheader *dheader; 923 SVGACBHeader *cb_hdr; 924 925 if (WARN_ON_ONCE(size > VMW_CMDBUF_INLINE_SIZE)) 926 return -ENOMEM; 927 928 dheader = dma_pool_zalloc(man->dheaders, GFP_KERNEL, 929 &header->handle); 930 if (!dheader) 931 return -ENOMEM; 932 933 header->inline_space = true; 934 header->size = VMW_CMDBUF_INLINE_SIZE; 935 cb_hdr = &dheader->cb_header; 936 header->cb_header = cb_hdr; 937 header->cmd = dheader->cmd; 938 cb_hdr->status = SVGA_CB_STATUS_NONE; 939 cb_hdr->flags = SVGA_CB_FLAG_NONE; 940 cb_hdr->ptr.pa = (u64)header->handle + 941 (u64)offsetof(struct vmw_cmdbuf_dheader, cmd); 942 943 return 0; 944 } 945 946 /** 947 * vmw_cmdbuf_alloc - Allocate a command buffer header complete with 948 * command buffer space. 949 * 950 * @man: The command buffer manager. 951 * @size: The requested size of the buffer space. 952 * @interruptible: Whether to sleep interruptible while waiting for space. 953 * @p_header: points to a header pointer to populate on successful return. 954 * 955 * Returns a pointer to command buffer space if successful. Otherwise 956 * returns an error pointer. The header pointer returned in @p_header should 957 * be used for upcoming calls to vmw_cmdbuf_reserve() and vmw_cmdbuf_commit(). 958 */ 959 void *vmw_cmdbuf_alloc(struct vmw_cmdbuf_man *man, 960 size_t size, bool interruptible, 961 struct vmw_cmdbuf_header **p_header) 962 { 963 struct vmw_cmdbuf_header *header; 964 int ret = 0; 965 966 *p_header = NULL; 967 968 header = kzalloc_obj(*header); 969 if (!header) 970 return ERR_PTR(-ENOMEM); 971 972 if (size <= VMW_CMDBUF_INLINE_SIZE) 973 ret = vmw_cmdbuf_space_inline(man, header, size); 974 else 975 ret = vmw_cmdbuf_space_pool(man, header, size, interruptible); 976 977 if (ret) { 978 kfree(header); 979 return ERR_PTR(ret); 980 } 981 982 header->man = man; 983 INIT_LIST_HEAD(&header->list); 984 header->cb_header->status = SVGA_CB_STATUS_NONE; 985 *p_header = header; 986 987 return header->cmd; 988 } 989 990 /** 991 * vmw_cmdbuf_reserve_cur - Reserve space for commands in the current 992 * command buffer. 993 * 994 * @man: The command buffer manager. 995 * @size: The requested size of the commands. 996 * @ctx_id: The context id if any. Otherwise set to SVGA3D_REG_INVALID. 997 * @interruptible: Whether to sleep interruptible while waiting for space. 998 * 999 * Returns a pointer to command buffer space if successful. Otherwise 1000 * returns an error pointer. 1001 */ 1002 static void *vmw_cmdbuf_reserve_cur(struct vmw_cmdbuf_man *man, 1003 size_t size, 1004 int ctx_id, 1005 bool interruptible) 1006 { 1007 struct vmw_cmdbuf_header *cur; 1008 void *ret; 1009 1010 if (vmw_cmdbuf_cur_lock(man, interruptible)) 1011 return ERR_PTR(-ERESTARTSYS); 1012 1013 cur = man->cur; 1014 if (cur && (size + man->cur_pos > cur->size || 1015 ((cur->cb_header->flags & SVGA_CB_FLAG_DX_CONTEXT) && 1016 ctx_id != cur->cb_header->dxContext))) 1017 __vmw_cmdbuf_cur_flush(man); 1018 1019 if (!man->cur) { 1020 ret = vmw_cmdbuf_alloc(man, 1021 max_t(size_t, size, man->default_size), 1022 interruptible, &man->cur); 1023 if (IS_ERR(ret)) { 1024 vmw_cmdbuf_cur_unlock(man); 1025 return ret; 1026 } 1027 1028 cur = man->cur; 1029 } 1030 1031 if (ctx_id != SVGA3D_INVALID_ID) { 1032 cur->cb_header->flags |= SVGA_CB_FLAG_DX_CONTEXT; 1033 cur->cb_header->dxContext = ctx_id; 1034 } 1035 1036 cur->reserved = size; 1037 1038 return (void *) (man->cur->cmd + man->cur_pos); 1039 } 1040 1041 /** 1042 * vmw_cmdbuf_commit_cur - Commit commands in the current command buffer. 1043 * 1044 * @man: The command buffer manager. 1045 * @size: The size of the commands actually written. 1046 * @flush: Whether to flush the command buffer immediately. 1047 */ 1048 static void vmw_cmdbuf_commit_cur(struct vmw_cmdbuf_man *man, 1049 size_t size, bool flush) 1050 { 1051 struct vmw_cmdbuf_header *cur = man->cur; 1052 1053 lockdep_assert_held_once(&man->cur_mutex); 1054 1055 WARN_ON(size > cur->reserved); 1056 man->cur_pos += size; 1057 if (!size) 1058 cur->cb_header->flags &= ~SVGA_CB_FLAG_DX_CONTEXT; 1059 if (flush) 1060 __vmw_cmdbuf_cur_flush(man); 1061 vmw_cmdbuf_cur_unlock(man); 1062 } 1063 1064 /** 1065 * vmw_cmdbuf_reserve - Reserve space for commands in a command buffer. 1066 * 1067 * @man: The command buffer manager. 1068 * @size: The requested size of the commands. 1069 * @ctx_id: The context id if any. Otherwise set to SVGA3D_REG_INVALID. 1070 * @interruptible: Whether to sleep interruptible while waiting for space. 1071 * @header: Header of the command buffer. NULL if the current command buffer 1072 * should be used. 1073 * 1074 * Returns a pointer to command buffer space if successful. Otherwise 1075 * returns an error pointer. 1076 */ 1077 void *vmw_cmdbuf_reserve(struct vmw_cmdbuf_man *man, size_t size, 1078 int ctx_id, bool interruptible, 1079 struct vmw_cmdbuf_header *header) 1080 { 1081 if (!header) 1082 return vmw_cmdbuf_reserve_cur(man, size, ctx_id, interruptible); 1083 1084 if (size > header->size) 1085 return ERR_PTR(-EINVAL); 1086 1087 if (ctx_id != SVGA3D_INVALID_ID) { 1088 header->cb_header->flags |= SVGA_CB_FLAG_DX_CONTEXT; 1089 header->cb_header->dxContext = ctx_id; 1090 } 1091 1092 header->reserved = size; 1093 return header->cmd; 1094 } 1095 1096 /** 1097 * vmw_cmdbuf_commit - Commit commands in a command buffer. 1098 * 1099 * @man: The command buffer manager. 1100 * @size: The size of the commands actually written. 1101 * @header: Header of the command buffer. NULL if the current command buffer 1102 * should be used. 1103 * @flush: Whether to flush the command buffer immediately. 1104 */ 1105 void vmw_cmdbuf_commit(struct vmw_cmdbuf_man *man, size_t size, 1106 struct vmw_cmdbuf_header *header, bool flush) 1107 { 1108 if (!header) { 1109 vmw_cmdbuf_commit_cur(man, size, flush); 1110 return; 1111 } 1112 1113 (void) vmw_cmdbuf_cur_lock(man, false); 1114 __vmw_cmdbuf_cur_flush(man); 1115 WARN_ON(size > header->reserved); 1116 man->cur = header; 1117 man->cur_pos = size; 1118 if (!size) 1119 header->cb_header->flags &= ~SVGA_CB_FLAG_DX_CONTEXT; 1120 if (flush) 1121 __vmw_cmdbuf_cur_flush(man); 1122 vmw_cmdbuf_cur_unlock(man); 1123 } 1124 1125 1126 /** 1127 * vmw_cmdbuf_send_device_command - Send a command through the device context. 1128 * 1129 * @man: The command buffer manager. 1130 * @command: Pointer to the command to send. 1131 * @size: Size of the command. 1132 * 1133 * Synchronously sends a device context command. 1134 */ 1135 static int vmw_cmdbuf_send_device_command(struct vmw_cmdbuf_man *man, 1136 const void *command, 1137 size_t size) 1138 { 1139 struct vmw_cmdbuf_header *header; 1140 int status; 1141 void *cmd = vmw_cmdbuf_alloc(man, size, false, &header); 1142 1143 if (IS_ERR(cmd)) 1144 return PTR_ERR(cmd); 1145 1146 memcpy(cmd, command, size); 1147 header->cb_header->length = size; 1148 header->cb_context = SVGA_CB_CONTEXT_DEVICE; 1149 spin_lock(&man->lock); 1150 status = vmw_cmdbuf_header_submit(header); 1151 spin_unlock(&man->lock); 1152 vmw_cmdbuf_header_free(header); 1153 1154 if (status != SVGA_CB_STATUS_COMPLETED) { 1155 DRM_ERROR("Device context command failed with status %d\n", 1156 status); 1157 return -EINVAL; 1158 } 1159 1160 return 0; 1161 } 1162 1163 /** 1164 * vmw_cmdbuf_preempt - Send a preempt command through the device 1165 * context. 1166 * 1167 * @man: The command buffer manager. 1168 * @context: Device context to pass command through. 1169 * 1170 * Synchronously sends a preempt command. 1171 */ 1172 static int vmw_cmdbuf_preempt(struct vmw_cmdbuf_man *man, u32 context) 1173 { 1174 struct { 1175 uint32 id; 1176 SVGADCCmdPreempt body; 1177 } __packed cmd; 1178 1179 cmd.id = SVGA_DC_CMD_PREEMPT; 1180 cmd.body.context = SVGA_CB_CONTEXT_0 + context; 1181 cmd.body.ignoreIDZero = 0; 1182 1183 return vmw_cmdbuf_send_device_command(man, &cmd, sizeof(cmd)); 1184 } 1185 1186 1187 /** 1188 * vmw_cmdbuf_startstop - Send a start / stop command through the device 1189 * context. 1190 * 1191 * @man: The command buffer manager. 1192 * @context: Device context to start/stop. 1193 * @enable: Whether to enable or disable the context. 1194 * 1195 * Synchronously sends a device start / stop context command. 1196 */ 1197 static int vmw_cmdbuf_startstop(struct vmw_cmdbuf_man *man, u32 context, 1198 bool enable) 1199 { 1200 struct { 1201 uint32 id; 1202 SVGADCCmdStartStop body; 1203 } __packed cmd; 1204 1205 cmd.id = SVGA_DC_CMD_START_STOP_CONTEXT; 1206 cmd.body.enable = (enable) ? 1 : 0; 1207 cmd.body.context = SVGA_CB_CONTEXT_0 + context; 1208 1209 return vmw_cmdbuf_send_device_command(man, &cmd, sizeof(cmd)); 1210 } 1211 1212 /** 1213 * vmw_cmdbuf_set_pool_size - Set command buffer manager sizes 1214 * 1215 * @man: The command buffer manager. 1216 * @size: The size of the main space pool. 1217 * 1218 * Set the size and allocate the main command buffer space pool. 1219 * If successful, this enables large command submissions. 1220 * Note that this function requires that rudimentary command 1221 * submission is already available and that the MOB memory manager is alive. 1222 * Returns 0 on success. Negative error code on failure. 1223 */ 1224 int vmw_cmdbuf_set_pool_size(struct vmw_cmdbuf_man *man, size_t size) 1225 { 1226 struct vmw_private *dev_priv = man->dev_priv; 1227 int ret; 1228 1229 if (man->has_pool) 1230 return -EINVAL; 1231 1232 /* First, try to allocate a huge chunk of DMA memory */ 1233 size = PAGE_ALIGN(size); 1234 man->map = dma_alloc_coherent(dev_priv->drm.dev, size, 1235 &man->handle, GFP_KERNEL); 1236 if (man->map) { 1237 man->using_mob = false; 1238 } else { 1239 struct vmw_bo_params bo_params = { 1240 .domain = VMW_BO_DOMAIN_MOB, 1241 .busy_domain = VMW_BO_DOMAIN_MOB, 1242 .bo_type = ttm_bo_type_kernel, 1243 .size = size, 1244 .pin = true 1245 }; 1246 /* 1247 * DMA memory failed. If we can have command buffers in a 1248 * MOB, try to use that instead. Note that this will 1249 * actually call into the already enabled manager, when 1250 * binding the MOB. 1251 */ 1252 if (!(dev_priv->capabilities & SVGA_CAP_DX) || 1253 !dev_priv->has_mob) 1254 return -ENOMEM; 1255 1256 ret = vmw_bo_create(dev_priv, &bo_params, &man->cmd_space); 1257 if (ret) 1258 return ret; 1259 1260 man->map = vmw_bo_map_and_cache(man->cmd_space); 1261 man->using_mob = man->map; 1262 } 1263 1264 man->size = size; 1265 drm_mm_init(&man->mm, 0, size >> PAGE_SHIFT); 1266 1267 man->has_pool = true; 1268 1269 /* 1270 * For now, set the default size to VMW_CMDBUF_INLINE_SIZE to 1271 * prevent deadlocks from happening when vmw_cmdbuf_space_pool() 1272 * needs to wait for space and we block on further command 1273 * submissions to be able to free up space. 1274 */ 1275 man->default_size = VMW_CMDBUF_INLINE_SIZE; 1276 drm_info(&dev_priv->drm, 1277 "Using command buffers with %s pool.\n", 1278 (man->using_mob) ? "MOB" : "DMA"); 1279 1280 return 0; 1281 } 1282 1283 /** 1284 * vmw_cmdbuf_man_create: Create a command buffer manager and enable it for 1285 * inline command buffer submissions only. 1286 * 1287 * @dev_priv: Pointer to device private structure. 1288 * 1289 * Returns a pointer to a cummand buffer manager to success or error pointer 1290 * on failure. The command buffer manager will be enabled for submissions of 1291 * size VMW_CMDBUF_INLINE_SIZE only. 1292 */ 1293 struct vmw_cmdbuf_man *vmw_cmdbuf_man_create(struct vmw_private *dev_priv) 1294 { 1295 struct vmw_cmdbuf_man *man; 1296 struct vmw_cmdbuf_context *ctx; 1297 unsigned int i; 1298 int ret; 1299 1300 if (!(dev_priv->capabilities & SVGA_CAP_COMMAND_BUFFERS)) 1301 return ERR_PTR(-ENOSYS); 1302 1303 man = kzalloc_obj(*man); 1304 if (!man) 1305 return ERR_PTR(-ENOMEM); 1306 1307 man->num_contexts = (dev_priv->capabilities & SVGA_CAP_HP_CMD_QUEUE) ? 1308 2 : 1; 1309 man->headers = dma_pool_create("vmwgfx cmdbuf", 1310 dev_priv->drm.dev, 1311 sizeof(SVGACBHeader), 1312 64, PAGE_SIZE); 1313 if (!man->headers) { 1314 ret = -ENOMEM; 1315 goto out_no_pool; 1316 } 1317 1318 man->dheaders = dma_pool_create("vmwgfx inline cmdbuf", 1319 dev_priv->drm.dev, 1320 sizeof(struct vmw_cmdbuf_dheader), 1321 64, PAGE_SIZE); 1322 if (!man->dheaders) { 1323 ret = -ENOMEM; 1324 goto out_no_dpool; 1325 } 1326 1327 for_each_cmdbuf_ctx(man, i, ctx) 1328 vmw_cmdbuf_ctx_init(ctx); 1329 1330 INIT_LIST_HEAD(&man->error); 1331 spin_lock_init(&man->lock); 1332 mutex_init(&man->cur_mutex); 1333 mutex_init(&man->space_mutex); 1334 mutex_init(&man->error_mutex); 1335 man->default_size = VMW_CMDBUF_INLINE_SIZE; 1336 init_waitqueue_head(&man->alloc_queue); 1337 init_waitqueue_head(&man->idle_queue); 1338 man->dev_priv = dev_priv; 1339 man->max_hw_submitted = SVGA_CB_MAX_QUEUED_PER_CONTEXT - 1; 1340 INIT_WORK(&man->work, &vmw_cmdbuf_work_func); 1341 vmw_generic_waiter_add(dev_priv, SVGA_IRQFLAG_ERROR, 1342 &dev_priv->error_waiters); 1343 ret = vmw_cmdbuf_startstop(man, 0, true); 1344 if (ret) { 1345 DRM_ERROR("Failed starting command buffer contexts\n"); 1346 vmw_cmdbuf_man_destroy(man); 1347 return ERR_PTR(ret); 1348 } 1349 1350 return man; 1351 1352 out_no_dpool: 1353 dma_pool_destroy(man->headers); 1354 out_no_pool: 1355 kfree(man); 1356 1357 return ERR_PTR(ret); 1358 } 1359 1360 /** 1361 * vmw_cmdbuf_remove_pool - Take down the main buffer space pool. 1362 * 1363 * @man: Pointer to a command buffer manager. 1364 * 1365 * This function removes the main buffer space pool, and should be called 1366 * before MOB memory management is removed. When this function has been called, 1367 * only small command buffer submissions of size VMW_CMDBUF_INLINE_SIZE or 1368 * less are allowed, and the default size of the command buffer for small kernel 1369 * submissions is also set to this size. 1370 */ 1371 void vmw_cmdbuf_remove_pool(struct vmw_cmdbuf_man *man) 1372 { 1373 if (!man->has_pool) 1374 return; 1375 1376 man->has_pool = false; 1377 man->default_size = VMW_CMDBUF_INLINE_SIZE; 1378 (void) vmw_cmdbuf_idle(man, false, 10*HZ); 1379 if (man->using_mob) 1380 vmw_bo_unreference(&man->cmd_space); 1381 else 1382 dma_free_coherent(man->dev_priv->drm.dev, 1383 man->size, man->map, man->handle); 1384 } 1385 1386 /** 1387 * vmw_cmdbuf_man_destroy - Take down a command buffer manager. 1388 * 1389 * @man: Pointer to a command buffer manager. 1390 * 1391 * This function idles and then destroys a command buffer manager. 1392 */ 1393 void vmw_cmdbuf_man_destroy(struct vmw_cmdbuf_man *man) 1394 { 1395 WARN_ON_ONCE(man->has_pool); 1396 (void) vmw_cmdbuf_idle(man, false, 10*HZ); 1397 1398 if (vmw_cmdbuf_startstop(man, 0, false)) 1399 DRM_ERROR("Failed stopping command buffer contexts.\n"); 1400 1401 vmw_generic_waiter_remove(man->dev_priv, SVGA_IRQFLAG_ERROR, 1402 &man->dev_priv->error_waiters); 1403 (void) cancel_work_sync(&man->work); 1404 dma_pool_destroy(man->dheaders); 1405 dma_pool_destroy(man->headers); 1406 mutex_destroy(&man->cur_mutex); 1407 mutex_destroy(&man->space_mutex); 1408 mutex_destroy(&man->error_mutex); 1409 kfree(man); 1410 } 1411