1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
4 * Copyright 2015 VMware, Inc., Palo Alto, CA., USA
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include <linux/dmapool.h>
29 #include <linux/pci.h>
31 #include <drm/ttm/ttm_bo_api.h>
33 #include "vmwgfx_drv.h"
36 * Size of inline command buffers. Try to make sure that a page size is a
37 * multiple of the DMA pool allocation size.
39 #define VMW_CMDBUF_INLINE_ALIGN 64
40 #define VMW_CMDBUF_INLINE_SIZE \
41 (1024 - ALIGN(sizeof(SVGACBHeader), VMW_CMDBUF_INLINE_ALIGN))
44 * struct vmw_cmdbuf_context - Command buffer context queues
46 * @submitted: List of command buffers that have been submitted to the
47 * manager but not yet submitted to hardware.
48 * @hw_submitted: List of command buffers submitted to hardware.
49 * @preempted: List of preempted command buffers.
50 * @num_hw_submitted: Number of buffers currently being processed by hardware
51 * @block_submission: Identifies a block command submission.
53 struct vmw_cmdbuf_context {
54 struct list_head submitted;
55 struct list_head hw_submitted;
56 struct list_head preempted;
57 unsigned num_hw_submitted;
58 bool block_submission;
62 * struct vmw_cmdbuf_man - Command buffer manager
64 * @cur_mutex: Mutex protecting the command buffer used for incremental small
65 * kernel command submissions, @cur.
66 * @space_mutex: Mutex to protect against starvation when we allocate
67 * main pool buffer space.
68 * @error_mutex: Mutex to serialize the work queue error handling.
69 * Note this is not needed if the same workqueue handler
70 * can't race with itself...
71 * @work: A struct work_struct implementeing command buffer error handling.
73 * @dev_priv: Pointer to the device private struct. Immutable.
74 * @ctx: Array of command buffer context queues. The queues and the context
75 * data is protected by @lock.
76 * @error: List of command buffers that have caused device errors.
78 * @mm: Range manager for the command buffer space. Manager allocations and
79 * frees are protected by @lock.
80 * @cmd_space: Buffer object for the command buffer space, unless we were
81 * able to make a contigous coherent DMA memory allocation, @handle. Immutable.
82 * @map_obj: Mapping state for @cmd_space. 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
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
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
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
107 * @size: The size of the command buffer space. Immutable.
108 * @num_contexts: Number of contexts actually enabled.
110 struct vmw_cmdbuf_man {
111 struct mutex cur_mutex;
112 struct mutex space_mutex;
113 struct mutex error_mutex;
114 struct work_struct work;
115 struct vmw_private *dev_priv;
116 struct vmw_cmdbuf_context ctx[SVGA_CB_CONTEXT_MAX];
117 struct list_head error;
119 struct ttm_buffer_object *cmd_space;
120 struct ttm_bo_kmap_obj map_obj;
122 struct vmw_cmdbuf_header *cur;
125 unsigned max_hw_submitted;
127 struct dma_pool *headers;
128 struct dma_pool *dheaders;
129 wait_queue_head_t alloc_queue;
130 wait_queue_head_t idle_queue;
140 * struct vmw_cmdbuf_header - Command buffer metadata
142 * @man: The command buffer manager.
143 * @cb_header: Device command buffer header, allocated from a DMA pool.
144 * @cb_context: The device command buffer context.
145 * @list: List head for attaching to the manager lists.
146 * @node: The range manager node.
147 * @handle: The DMA address of @cb_header. Handed to the device on command
149 * @cmd: Pointer to the command buffer space of this buffer.
150 * @size: Size of the command buffer space of this buffer.
151 * @reserved: Reserved space of this buffer.
152 * @inline_space: Whether inline command buffer space is used.
154 struct vmw_cmdbuf_header {
155 struct vmw_cmdbuf_man *man;
156 SVGACBHeader *cb_header;
157 SVGACBContext cb_context;
158 struct list_head list;
159 struct drm_mm_node node;
168 * struct vmw_cmdbuf_dheader - Device command buffer header with inline
169 * command buffer space.
171 * @cb_header: Device command buffer header.
172 * @cmd: Inline command buffer space.
174 struct vmw_cmdbuf_dheader {
175 SVGACBHeader cb_header;
176 u8 cmd[VMW_CMDBUF_INLINE_SIZE] __aligned(VMW_CMDBUF_INLINE_ALIGN);
180 * struct vmw_cmdbuf_alloc_info - Command buffer space allocation metadata
182 * @page_size: Size of requested command buffer space in pages.
183 * @node: Pointer to the range manager node.
184 * @done: True if this allocation has succeeded.
186 struct vmw_cmdbuf_alloc_info {
188 struct drm_mm_node *node;
192 /* Loop over each context in the command buffer manager. */
193 #define for_each_cmdbuf_ctx(_man, _i, _ctx) \
194 for (_i = 0, _ctx = &(_man)->ctx[0]; (_i) < (_man)->num_contexts; \
197 static int vmw_cmdbuf_startstop(struct vmw_cmdbuf_man *man, u32 context,
199 static int vmw_cmdbuf_preempt(struct vmw_cmdbuf_man *man, u32 context);
202 * vmw_cmdbuf_cur_lock - Helper to lock the cur_mutex.
204 * @man: The range manager.
205 * @interruptible: Whether to wait interruptible when locking.
207 static int vmw_cmdbuf_cur_lock(struct vmw_cmdbuf_man *man, bool interruptible)
210 if (mutex_lock_interruptible(&man->cur_mutex))
213 mutex_lock(&man->cur_mutex);
220 * vmw_cmdbuf_cur_unlock - Helper to unlock the cur_mutex.
222 * @man: The range manager.
224 static void vmw_cmdbuf_cur_unlock(struct vmw_cmdbuf_man *man)
226 mutex_unlock(&man->cur_mutex);
230 * vmw_cmdbuf_header_inline_free - Free a struct vmw_cmdbuf_header that has
231 * been used for the device context with inline command buffers.
232 * Need not be called locked.
234 * @header: Pointer to the header to free.
236 static void vmw_cmdbuf_header_inline_free(struct vmw_cmdbuf_header *header)
238 struct vmw_cmdbuf_dheader *dheader;
240 if (WARN_ON_ONCE(!header->inline_space))
243 dheader = container_of(header->cb_header, struct vmw_cmdbuf_dheader,
245 dma_pool_free(header->man->dheaders, dheader, header->handle);
250 * __vmw_cmdbuf_header_free - Free a struct vmw_cmdbuf_header and its
251 * associated structures.
253 * @header: Pointer to the header to free.
255 * For internal use. Must be called with man::lock held.
257 static void __vmw_cmdbuf_header_free(struct vmw_cmdbuf_header *header)
259 struct vmw_cmdbuf_man *man = header->man;
261 lockdep_assert_held_once(&man->lock);
263 if (header->inline_space) {
264 vmw_cmdbuf_header_inline_free(header);
268 drm_mm_remove_node(&header->node);
269 wake_up_all(&man->alloc_queue);
270 if (header->cb_header)
271 dma_pool_free(man->headers, header->cb_header,
277 * vmw_cmdbuf_header_free - Free a struct vmw_cmdbuf_header and its
278 * associated structures.
280 * @header: Pointer to the header to free.
282 void vmw_cmdbuf_header_free(struct vmw_cmdbuf_header *header)
284 struct vmw_cmdbuf_man *man = header->man;
286 /* Avoid locking if inline_space */
287 if (header->inline_space) {
288 vmw_cmdbuf_header_inline_free(header);
291 spin_lock(&man->lock);
292 __vmw_cmdbuf_header_free(header);
293 spin_unlock(&man->lock);
298 * vmw_cmdbuf_header_submit: Submit a command buffer to hardware.
300 * @header: The header of the buffer to submit.
302 static int vmw_cmdbuf_header_submit(struct vmw_cmdbuf_header *header)
304 struct vmw_cmdbuf_man *man = header->man;
307 val = upper_32_bits(header->handle);
308 vmw_write(man->dev_priv, SVGA_REG_COMMAND_HIGH, val);
310 val = lower_32_bits(header->handle);
311 val |= header->cb_context & SVGA_CB_CONTEXT_MASK;
312 vmw_write(man->dev_priv, SVGA_REG_COMMAND_LOW, val);
314 return header->cb_header->status;
318 * vmw_cmdbuf_ctx_init: Initialize a command buffer context.
320 * @ctx: The command buffer context to initialize
322 static void vmw_cmdbuf_ctx_init(struct vmw_cmdbuf_context *ctx)
324 INIT_LIST_HEAD(&ctx->hw_submitted);
325 INIT_LIST_HEAD(&ctx->submitted);
326 INIT_LIST_HEAD(&ctx->preempted);
327 ctx->num_hw_submitted = 0;
331 * vmw_cmdbuf_ctx_submit: Submit command buffers from a command buffer
334 * @man: The command buffer manager.
335 * @ctx: The command buffer context.
337 * Submits command buffers to hardware until there are no more command
338 * buffers to submit or the hardware can't handle more command buffers.
340 static void vmw_cmdbuf_ctx_submit(struct vmw_cmdbuf_man *man,
341 struct vmw_cmdbuf_context *ctx)
343 while (ctx->num_hw_submitted < man->max_hw_submitted &&
344 !list_empty(&ctx->submitted) &&
345 !ctx->block_submission) {
346 struct vmw_cmdbuf_header *entry;
349 entry = list_first_entry(&ctx->submitted,
350 struct vmw_cmdbuf_header,
353 status = vmw_cmdbuf_header_submit(entry);
355 /* This should never happen */
356 if (WARN_ON_ONCE(status == SVGA_CB_STATUS_QUEUE_FULL)) {
357 entry->cb_header->status = SVGA_CB_STATUS_NONE;
361 list_del(&entry->list);
362 list_add_tail(&entry->list, &ctx->hw_submitted);
363 ctx->num_hw_submitted++;
369 * vmw_cmdbuf_ctx_process - Process a command buffer context.
371 * @man: The command buffer manager.
372 * @ctx: The command buffer context.
373 * @notempty: Pass back count of non-empty command submitted lists.
375 * Submit command buffers to hardware if possible, and process finished
376 * buffers. Typically freeing them, but on preemption or error take
377 * appropriate action. Wake up waiters if appropriate.
379 static void vmw_cmdbuf_ctx_process(struct vmw_cmdbuf_man *man,
380 struct vmw_cmdbuf_context *ctx,
383 struct vmw_cmdbuf_header *entry, *next;
385 vmw_cmdbuf_ctx_submit(man, ctx);
387 list_for_each_entry_safe(entry, next, &ctx->hw_submitted, list) {
388 SVGACBStatus status = entry->cb_header->status;
390 if (status == SVGA_CB_STATUS_NONE)
393 list_del(&entry->list);
394 wake_up_all(&man->idle_queue);
395 ctx->num_hw_submitted--;
397 case SVGA_CB_STATUS_COMPLETED:
398 __vmw_cmdbuf_header_free(entry);
400 case SVGA_CB_STATUS_COMMAND_ERROR:
401 WARN_ONCE(true, "Command buffer error.\n");
402 entry->cb_header->status = SVGA_CB_STATUS_NONE;
403 list_add_tail(&entry->list, &man->error);
404 schedule_work(&man->work);
406 case SVGA_CB_STATUS_PREEMPTED:
407 entry->cb_header->status = SVGA_CB_STATUS_NONE;
408 list_add_tail(&entry->list, &ctx->preempted);
410 case SVGA_CB_STATUS_CB_HEADER_ERROR:
411 WARN_ONCE(true, "Command buffer header error.\n");
412 __vmw_cmdbuf_header_free(entry);
415 WARN_ONCE(true, "Undefined command buffer status.\n");
416 __vmw_cmdbuf_header_free(entry);
421 vmw_cmdbuf_ctx_submit(man, ctx);
422 if (!list_empty(&ctx->submitted))
427 * vmw_cmdbuf_man_process - Process all command buffer contexts and
428 * switch on and off irqs as appropriate.
430 * @man: The command buffer manager.
432 * Calls vmw_cmdbuf_ctx_process() on all contexts. If any context has
433 * command buffers left that are not submitted to hardware, Make sure
434 * IRQ handling is turned on. Otherwise, make sure it's turned off.
436 static void vmw_cmdbuf_man_process(struct vmw_cmdbuf_man *man)
439 struct vmw_cmdbuf_context *ctx;
444 for_each_cmdbuf_ctx(man, i, ctx)
445 vmw_cmdbuf_ctx_process(man, ctx, ¬empty);
447 if (man->irq_on && !notempty) {
448 vmw_generic_waiter_remove(man->dev_priv,
449 SVGA_IRQFLAG_COMMAND_BUFFER,
450 &man->dev_priv->cmdbuf_waiters);
452 } else if (!man->irq_on && notempty) {
453 vmw_generic_waiter_add(man->dev_priv,
454 SVGA_IRQFLAG_COMMAND_BUFFER,
455 &man->dev_priv->cmdbuf_waiters);
458 /* Rerun in case we just missed an irq. */
464 * vmw_cmdbuf_ctx_add - Schedule a command buffer for submission on a
465 * command buffer context
467 * @man: The command buffer manager.
468 * @header: The header of the buffer to submit.
469 * @cb_context: The command buffer context to use.
471 * This function adds @header to the "submitted" queue of the command
472 * buffer context identified by @cb_context. It then calls the command buffer
473 * manager processing to potentially submit the buffer to hardware.
474 * @man->lock needs to be held when calling this function.
476 static void vmw_cmdbuf_ctx_add(struct vmw_cmdbuf_man *man,
477 struct vmw_cmdbuf_header *header,
478 SVGACBContext cb_context)
480 if (!(header->cb_header->flags & SVGA_CB_FLAG_DX_CONTEXT))
481 header->cb_header->dxContext = 0;
482 header->cb_context = cb_context;
483 list_add_tail(&header->list, &man->ctx[cb_context].submitted);
485 vmw_cmdbuf_man_process(man);
489 * vmw_cmdbuf_irqthread - The main part of the command buffer interrupt
490 * handler implemented as a threaded irq task.
492 * @man: Pointer to the command buffer manager.
494 * The bottom half of the interrupt handler simply calls into the
495 * command buffer processor to free finished buffers and submit any
496 * queued buffers to hardware.
498 void vmw_cmdbuf_irqthread(struct vmw_cmdbuf_man *man)
500 spin_lock(&man->lock);
501 vmw_cmdbuf_man_process(man);
502 spin_unlock(&man->lock);
506 * vmw_cmdbuf_work_func - The deferred work function that handles
507 * command buffer errors.
509 * @work: The work func closure argument.
511 * Restarting the command buffer context after an error requires process
512 * context, so it is deferred to this work function.
514 static void vmw_cmdbuf_work_func(struct work_struct *work)
516 struct vmw_cmdbuf_man *man =
517 container_of(work, struct vmw_cmdbuf_man, work);
518 struct vmw_cmdbuf_header *entry, *next;
520 bool send_fence = false;
521 struct list_head restart_head[SVGA_CB_CONTEXT_MAX];
523 struct vmw_cmdbuf_context *ctx;
524 bool global_block = false;
526 for_each_cmdbuf_ctx(man, i, ctx)
527 INIT_LIST_HEAD(&restart_head[i]);
529 mutex_lock(&man->error_mutex);
530 spin_lock(&man->lock);
531 list_for_each_entry_safe(entry, next, &man->error, list) {
532 SVGACBHeader *cb_hdr = entry->cb_header;
533 SVGA3dCmdHeader *header = (SVGA3dCmdHeader *)
534 (entry->cmd + cb_hdr->errorOffset);
535 u32 error_cmd_size, new_start_offset;
536 const char *cmd_name;
538 list_del_init(&entry->list);
541 if (!vmw_cmd_describe(header, &error_cmd_size, &cmd_name)) {
542 VMW_DEBUG_USER("Unknown command causing device error.\n");
543 VMW_DEBUG_USER("Command buffer offset is %lu\n",
544 (unsigned long) cb_hdr->errorOffset);
545 __vmw_cmdbuf_header_free(entry);
550 VMW_DEBUG_USER("Command \"%s\" causing device error.\n",
552 VMW_DEBUG_USER("Command buffer offset is %lu\n",
553 (unsigned long) cb_hdr->errorOffset);
554 VMW_DEBUG_USER("Command size is %lu\n",
555 (unsigned long) error_cmd_size);
557 new_start_offset = cb_hdr->errorOffset + error_cmd_size;
559 if (new_start_offset >= cb_hdr->length) {
560 __vmw_cmdbuf_header_free(entry);
566 cb_hdr->ptr.mob.mobOffset += new_start_offset;
568 cb_hdr->ptr.pa += (u64) new_start_offset;
570 entry->cmd += new_start_offset;
571 cb_hdr->length -= new_start_offset;
572 cb_hdr->errorOffset = 0;
575 list_add_tail(&entry->list, &restart_head[entry->cb_context]);
578 for_each_cmdbuf_ctx(man, i, ctx)
579 man->ctx[i].block_submission = true;
581 spin_unlock(&man->lock);
583 /* Preempt all contexts */
584 if (global_block && vmw_cmdbuf_preempt(man, 0))
585 DRM_ERROR("Failed preempting command buffer contexts\n");
587 spin_lock(&man->lock);
588 for_each_cmdbuf_ctx(man, i, ctx) {
589 /* Move preempted command buffers to the preempted queue. */
590 vmw_cmdbuf_ctx_process(man, ctx, &dummy);
593 * Add the preempted queue after the command buffer
594 * that caused an error.
596 list_splice_init(&ctx->preempted, restart_head[i].prev);
599 * Finally add all command buffers first in the submitted
600 * queue, to rerun them.
603 ctx->block_submission = false;
604 list_splice_init(&restart_head[i], &ctx->submitted);
607 vmw_cmdbuf_man_process(man);
608 spin_unlock(&man->lock);
610 if (global_block && vmw_cmdbuf_startstop(man, 0, true))
611 DRM_ERROR("Failed restarting command buffer contexts\n");
613 /* Send a new fence in case one was removed */
615 vmw_cmd_send_fence(man->dev_priv, &dummy);
616 wake_up_all(&man->idle_queue);
619 mutex_unlock(&man->error_mutex);
623 * vmw_cmdbuf_man_idle - Check whether the command buffer manager is idle.
625 * @man: The command buffer manager.
626 * @check_preempted: Check also the preempted queue for pending command buffers.
629 static bool vmw_cmdbuf_man_idle(struct vmw_cmdbuf_man *man,
630 bool check_preempted)
632 struct vmw_cmdbuf_context *ctx;
636 spin_lock(&man->lock);
637 vmw_cmdbuf_man_process(man);
638 for_each_cmdbuf_ctx(man, i, ctx) {
639 if (!list_empty(&ctx->submitted) ||
640 !list_empty(&ctx->hw_submitted) ||
641 (check_preempted && !list_empty(&ctx->preempted)))
645 idle = list_empty(&man->error);
648 spin_unlock(&man->lock);
654 * __vmw_cmdbuf_cur_flush - Flush the current command buffer for small kernel
655 * command submissions
657 * @man: The command buffer manager.
659 * Flushes the current command buffer without allocating a new one. A new one
660 * is automatically allocated when needed. Call with @man->cur_mutex held.
662 static void __vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man *man)
664 struct vmw_cmdbuf_header *cur = man->cur;
666 lockdep_assert_held_once(&man->cur_mutex);
671 spin_lock(&man->lock);
672 if (man->cur_pos == 0) {
673 __vmw_cmdbuf_header_free(cur);
677 man->cur->cb_header->length = man->cur_pos;
678 vmw_cmdbuf_ctx_add(man, man->cur, SVGA_CB_CONTEXT_0);
680 spin_unlock(&man->lock);
686 * vmw_cmdbuf_cur_flush - Flush the current command buffer for small kernel
687 * command submissions
689 * @man: The command buffer manager.
690 * @interruptible: Whether to sleep interruptible when sleeping.
692 * Flushes the current command buffer without allocating a new one. A new one
693 * is automatically allocated when needed.
695 int vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man *man,
698 int ret = vmw_cmdbuf_cur_lock(man, interruptible);
703 __vmw_cmdbuf_cur_flush(man);
704 vmw_cmdbuf_cur_unlock(man);
710 * vmw_cmdbuf_idle - Wait for command buffer manager idle.
712 * @man: The command buffer manager.
713 * @interruptible: Sleep interruptible while waiting.
714 * @timeout: Time out after this many ticks.
716 * Wait until the command buffer manager has processed all command buffers,
717 * or until a timeout occurs. If a timeout occurs, the function will return
720 int vmw_cmdbuf_idle(struct vmw_cmdbuf_man *man, bool interruptible,
721 unsigned long timeout)
725 ret = vmw_cmdbuf_cur_flush(man, interruptible);
726 vmw_generic_waiter_add(man->dev_priv,
727 SVGA_IRQFLAG_COMMAND_BUFFER,
728 &man->dev_priv->cmdbuf_waiters);
731 ret = wait_event_interruptible_timeout
732 (man->idle_queue, vmw_cmdbuf_man_idle(man, true),
735 ret = wait_event_timeout
736 (man->idle_queue, vmw_cmdbuf_man_idle(man, true),
739 vmw_generic_waiter_remove(man->dev_priv,
740 SVGA_IRQFLAG_COMMAND_BUFFER,
741 &man->dev_priv->cmdbuf_waiters);
743 if (!vmw_cmdbuf_man_idle(man, true))
755 * vmw_cmdbuf_try_alloc - Try to allocate buffer space from the main pool.
757 * @man: The command buffer manager.
758 * @info: Allocation info. Will hold the size on entry and allocated mm node
759 * on successful return.
761 * Try to allocate buffer space from the main pool. Returns true if succeeded.
762 * If a fatal error was hit, the error code is returned in @info->ret.
764 static bool vmw_cmdbuf_try_alloc(struct vmw_cmdbuf_man *man,
765 struct vmw_cmdbuf_alloc_info *info)
772 memset(info->node, 0, sizeof(*info->node));
773 spin_lock(&man->lock);
774 ret = drm_mm_insert_node(&man->mm, info->node, info->page_size);
776 vmw_cmdbuf_man_process(man);
777 ret = drm_mm_insert_node(&man->mm, info->node, info->page_size);
780 spin_unlock(&man->lock);
787 * vmw_cmdbuf_alloc_space - Allocate buffer space from the main pool.
789 * @man: The command buffer manager.
790 * @node: Pointer to pre-allocated range-manager node.
791 * @size: The size of the allocation.
792 * @interruptible: Whether to sleep interruptible while waiting for space.
794 * This function allocates buffer space from the main pool, and if there is
795 * no space available ATM, it turns on IRQ handling and sleeps waiting for it to
798 static int vmw_cmdbuf_alloc_space(struct vmw_cmdbuf_man *man,
799 struct drm_mm_node *node,
803 struct vmw_cmdbuf_alloc_info info;
805 info.page_size = PAGE_ALIGN(size) >> PAGE_SHIFT;
810 * To prevent starvation of large requests, only one allocating call
811 * at a time waiting for space.
814 if (mutex_lock_interruptible(&man->space_mutex))
817 mutex_lock(&man->space_mutex);
820 /* Try to allocate space without waiting. */
821 if (vmw_cmdbuf_try_alloc(man, &info))
824 vmw_generic_waiter_add(man->dev_priv,
825 SVGA_IRQFLAG_COMMAND_BUFFER,
826 &man->dev_priv->cmdbuf_waiters);
831 ret = wait_event_interruptible
832 (man->alloc_queue, vmw_cmdbuf_try_alloc(man, &info));
834 vmw_generic_waiter_remove
835 (man->dev_priv, SVGA_IRQFLAG_COMMAND_BUFFER,
836 &man->dev_priv->cmdbuf_waiters);
837 mutex_unlock(&man->space_mutex);
841 wait_event(man->alloc_queue, vmw_cmdbuf_try_alloc(man, &info));
843 vmw_generic_waiter_remove(man->dev_priv,
844 SVGA_IRQFLAG_COMMAND_BUFFER,
845 &man->dev_priv->cmdbuf_waiters);
848 mutex_unlock(&man->space_mutex);
854 * vmw_cmdbuf_space_pool - Set up a command buffer header with command buffer
855 * space from the main pool.
857 * @man: The command buffer manager.
858 * @header: Pointer to the header to set up.
859 * @size: The requested size of the buffer space.
860 * @interruptible: Whether to sleep interruptible while waiting for space.
862 static int vmw_cmdbuf_space_pool(struct vmw_cmdbuf_man *man,
863 struct vmw_cmdbuf_header *header,
867 SVGACBHeader *cb_hdr;
874 ret = vmw_cmdbuf_alloc_space(man, &header->node, size, interruptible);
879 header->cb_header = dma_pool_zalloc(man->headers, GFP_KERNEL,
881 if (!header->cb_header) {
883 goto out_no_cb_header;
886 header->size = header->node.size << PAGE_SHIFT;
887 cb_hdr = header->cb_header;
888 offset = header->node.start << PAGE_SHIFT;
889 header->cmd = man->map + offset;
890 if (man->using_mob) {
891 cb_hdr->flags = SVGA_CB_FLAG_MOB;
892 cb_hdr->ptr.mob.mobid = man->cmd_space->resource->start;
893 cb_hdr->ptr.mob.mobOffset = offset;
895 cb_hdr->ptr.pa = (u64)man->handle + (u64)offset;
901 spin_lock(&man->lock);
902 drm_mm_remove_node(&header->node);
903 spin_unlock(&man->lock);
909 * vmw_cmdbuf_space_inline - Set up a command buffer header with
910 * inline command buffer space.
912 * @man: The command buffer manager.
913 * @header: Pointer to the header to set up.
914 * @size: The requested size of the buffer space.
916 static int vmw_cmdbuf_space_inline(struct vmw_cmdbuf_man *man,
917 struct vmw_cmdbuf_header *header,
920 struct vmw_cmdbuf_dheader *dheader;
921 SVGACBHeader *cb_hdr;
923 if (WARN_ON_ONCE(size > VMW_CMDBUF_INLINE_SIZE))
926 dheader = dma_pool_zalloc(man->dheaders, GFP_KERNEL,
931 header->inline_space = true;
932 header->size = VMW_CMDBUF_INLINE_SIZE;
933 cb_hdr = &dheader->cb_header;
934 header->cb_header = cb_hdr;
935 header->cmd = dheader->cmd;
936 cb_hdr->status = SVGA_CB_STATUS_NONE;
937 cb_hdr->flags = SVGA_CB_FLAG_NONE;
938 cb_hdr->ptr.pa = (u64)header->handle +
939 (u64)offsetof(struct vmw_cmdbuf_dheader, cmd);
945 * vmw_cmdbuf_alloc - Allocate a command buffer header complete with
946 * command buffer space.
948 * @man: The command buffer manager.
949 * @size: The requested size of the buffer space.
950 * @interruptible: Whether to sleep interruptible while waiting for space.
951 * @p_header: points to a header pointer to populate on successful return.
953 * Returns a pointer to command buffer space if successful. Otherwise
954 * returns an error pointer. The header pointer returned in @p_header should
955 * be used for upcoming calls to vmw_cmdbuf_reserve() and vmw_cmdbuf_commit().
957 void *vmw_cmdbuf_alloc(struct vmw_cmdbuf_man *man,
958 size_t size, bool interruptible,
959 struct vmw_cmdbuf_header **p_header)
961 struct vmw_cmdbuf_header *header;
966 header = kzalloc(sizeof(*header), GFP_KERNEL);
968 return ERR_PTR(-ENOMEM);
970 if (size <= VMW_CMDBUF_INLINE_SIZE)
971 ret = vmw_cmdbuf_space_inline(man, header, size);
973 ret = vmw_cmdbuf_space_pool(man, header, size, interruptible);
981 INIT_LIST_HEAD(&header->list);
982 header->cb_header->status = SVGA_CB_STATUS_NONE;
989 * vmw_cmdbuf_reserve_cur - Reserve space for commands in the current
992 * @man: The command buffer manager.
993 * @size: The requested size of the commands.
994 * @ctx_id: The context id if any. Otherwise set to SVGA3D_REG_INVALID.
995 * @interruptible: Whether to sleep interruptible while waiting for space.
997 * Returns a pointer to command buffer space if successful. Otherwise
998 * returns an error pointer.
1000 static void *vmw_cmdbuf_reserve_cur(struct vmw_cmdbuf_man *man,
1005 struct vmw_cmdbuf_header *cur;
1008 if (vmw_cmdbuf_cur_lock(man, interruptible))
1009 return ERR_PTR(-ERESTARTSYS);
1012 if (cur && (size + man->cur_pos > cur->size ||
1013 ((cur->cb_header->flags & SVGA_CB_FLAG_DX_CONTEXT) &&
1014 ctx_id != cur->cb_header->dxContext)))
1015 __vmw_cmdbuf_cur_flush(man);
1018 ret = vmw_cmdbuf_alloc(man,
1019 max_t(size_t, size, man->default_size),
1020 interruptible, &man->cur);
1022 vmw_cmdbuf_cur_unlock(man);
1029 if (ctx_id != SVGA3D_INVALID_ID) {
1030 cur->cb_header->flags |= SVGA_CB_FLAG_DX_CONTEXT;
1031 cur->cb_header->dxContext = ctx_id;
1034 cur->reserved = size;
1036 return (void *) (man->cur->cmd + man->cur_pos);
1040 * vmw_cmdbuf_commit_cur - Commit commands in the current command buffer.
1042 * @man: The command buffer manager.
1043 * @size: The size of the commands actually written.
1044 * @flush: Whether to flush the command buffer immediately.
1046 static void vmw_cmdbuf_commit_cur(struct vmw_cmdbuf_man *man,
1047 size_t size, bool flush)
1049 struct vmw_cmdbuf_header *cur = man->cur;
1051 lockdep_assert_held_once(&man->cur_mutex);
1053 WARN_ON(size > cur->reserved);
1054 man->cur_pos += size;
1056 cur->cb_header->flags &= ~SVGA_CB_FLAG_DX_CONTEXT;
1058 __vmw_cmdbuf_cur_flush(man);
1059 vmw_cmdbuf_cur_unlock(man);
1063 * vmw_cmdbuf_reserve - Reserve space for commands in a command buffer.
1065 * @man: The command buffer manager.
1066 * @size: The requested size of the commands.
1067 * @ctx_id: The context id if any. Otherwise set to SVGA3D_REG_INVALID.
1068 * @interruptible: Whether to sleep interruptible while waiting for space.
1069 * @header: Header of the command buffer. NULL if the current command buffer
1072 * Returns a pointer to command buffer space if successful. Otherwise
1073 * returns an error pointer.
1075 void *vmw_cmdbuf_reserve(struct vmw_cmdbuf_man *man, size_t size,
1076 int ctx_id, bool interruptible,
1077 struct vmw_cmdbuf_header *header)
1080 return vmw_cmdbuf_reserve_cur(man, size, ctx_id, interruptible);
1082 if (size > header->size)
1083 return ERR_PTR(-EINVAL);
1085 if (ctx_id != SVGA3D_INVALID_ID) {
1086 header->cb_header->flags |= SVGA_CB_FLAG_DX_CONTEXT;
1087 header->cb_header->dxContext = ctx_id;
1090 header->reserved = size;
1095 * vmw_cmdbuf_commit - Commit commands in a command buffer.
1097 * @man: The command buffer manager.
1098 * @size: The size of the commands actually written.
1099 * @header: Header of the command buffer. NULL if the current command buffer
1101 * @flush: Whether to flush the command buffer immediately.
1103 void vmw_cmdbuf_commit(struct vmw_cmdbuf_man *man, size_t size,
1104 struct vmw_cmdbuf_header *header, bool flush)
1107 vmw_cmdbuf_commit_cur(man, size, flush);
1111 (void) vmw_cmdbuf_cur_lock(man, false);
1112 __vmw_cmdbuf_cur_flush(man);
1113 WARN_ON(size > header->reserved);
1115 man->cur_pos = size;
1117 header->cb_header->flags &= ~SVGA_CB_FLAG_DX_CONTEXT;
1119 __vmw_cmdbuf_cur_flush(man);
1120 vmw_cmdbuf_cur_unlock(man);
1125 * vmw_cmdbuf_send_device_command - Send a command through the device context.
1127 * @man: The command buffer manager.
1128 * @command: Pointer to the command to send.
1129 * @size: Size of the command.
1131 * Synchronously sends a device context command.
1133 static int vmw_cmdbuf_send_device_command(struct vmw_cmdbuf_man *man,
1134 const void *command,
1137 struct vmw_cmdbuf_header *header;
1139 void *cmd = vmw_cmdbuf_alloc(man, size, false, &header);
1142 return PTR_ERR(cmd);
1144 memcpy(cmd, command, size);
1145 header->cb_header->length = size;
1146 header->cb_context = SVGA_CB_CONTEXT_DEVICE;
1147 spin_lock(&man->lock);
1148 status = vmw_cmdbuf_header_submit(header);
1149 spin_unlock(&man->lock);
1150 vmw_cmdbuf_header_free(header);
1152 if (status != SVGA_CB_STATUS_COMPLETED) {
1153 DRM_ERROR("Device context command failed with status %d\n",
1162 * vmw_cmdbuf_preempt - Send a preempt command through the device
1165 * @man: The command buffer manager.
1166 * @context: Device context to pass command through.
1168 * Synchronously sends a preempt command.
1170 static int vmw_cmdbuf_preempt(struct vmw_cmdbuf_man *man, u32 context)
1174 SVGADCCmdPreempt body;
1177 cmd.id = SVGA_DC_CMD_PREEMPT;
1178 cmd.body.context = SVGA_CB_CONTEXT_0 + context;
1179 cmd.body.ignoreIDZero = 0;
1181 return vmw_cmdbuf_send_device_command(man, &cmd, sizeof(cmd));
1186 * vmw_cmdbuf_startstop - Send a start / stop command through the device
1189 * @man: The command buffer manager.
1190 * @context: Device context to start/stop.
1191 * @enable: Whether to enable or disable the context.
1193 * Synchronously sends a device start / stop context command.
1195 static int vmw_cmdbuf_startstop(struct vmw_cmdbuf_man *man, u32 context,
1200 SVGADCCmdStartStop body;
1203 cmd.id = SVGA_DC_CMD_START_STOP_CONTEXT;
1204 cmd.body.enable = (enable) ? 1 : 0;
1205 cmd.body.context = SVGA_CB_CONTEXT_0 + context;
1207 return vmw_cmdbuf_send_device_command(man, &cmd, sizeof(cmd));
1211 * vmw_cmdbuf_set_pool_size - Set command buffer manager sizes
1213 * @man: The command buffer manager.
1214 * @size: The size of the main space pool.
1216 * Set the size and allocate the main command buffer space pool.
1217 * If successful, this enables large command submissions.
1218 * Note that this function requires that rudimentary command
1219 * submission is already available and that the MOB memory manager is alive.
1220 * Returns 0 on success. Negative error code on failure.
1222 int vmw_cmdbuf_set_pool_size(struct vmw_cmdbuf_man *man, size_t size)
1224 struct vmw_private *dev_priv = man->dev_priv;
1231 /* First, try to allocate a huge chunk of DMA memory */
1232 size = PAGE_ALIGN(size);
1233 man->map = dma_alloc_coherent(dev_priv->drm.dev, size,
1234 &man->handle, GFP_KERNEL);
1236 man->using_mob = false;
1239 * DMA memory failed. If we can have command buffers in a
1240 * MOB, try to use that instead. Note that this will
1241 * actually call into the already enabled manager, when
1244 if (!(dev_priv->capabilities & SVGA_CAP_DX) ||
1248 ret = vmw_bo_create_kernel(dev_priv, size,
1254 man->using_mob = true;
1255 ret = ttm_bo_kmap(man->cmd_space, 0, size >> PAGE_SHIFT,
1260 man->map = ttm_kmap_obj_virtual(&man->map_obj, &dummy);
1264 drm_mm_init(&man->mm, 0, size >> PAGE_SHIFT);
1266 man->has_pool = true;
1269 * For now, set the default size to VMW_CMDBUF_INLINE_SIZE to
1270 * prevent deadlocks from happening when vmw_cmdbuf_space_pool()
1271 * needs to wait for space and we block on further command
1272 * submissions to be able to free up space.
1274 man->default_size = VMW_CMDBUF_INLINE_SIZE;
1275 drm_info(&dev_priv->drm,
1276 "Using command buffers with %s pool.\n",
1277 (man->using_mob) ? "MOB" : "DMA");
1282 if (man->using_mob) {
1283 ttm_bo_put(man->cmd_space);
1284 man->cmd_space = NULL;
1291 * vmw_cmdbuf_man_create: Create a command buffer manager and enable it for
1292 * inline command buffer submissions only.
1294 * @dev_priv: Pointer to device private structure.
1296 * Returns a pointer to a cummand buffer manager to success or error pointer
1297 * on failure. The command buffer manager will be enabled for submissions of
1298 * size VMW_CMDBUF_INLINE_SIZE only.
1300 struct vmw_cmdbuf_man *vmw_cmdbuf_man_create(struct vmw_private *dev_priv)
1302 struct vmw_cmdbuf_man *man;
1303 struct vmw_cmdbuf_context *ctx;
1307 if (!(dev_priv->capabilities & SVGA_CAP_COMMAND_BUFFERS))
1308 return ERR_PTR(-ENOSYS);
1310 man = kzalloc(sizeof(*man), GFP_KERNEL);
1312 return ERR_PTR(-ENOMEM);
1314 man->num_contexts = (dev_priv->capabilities & SVGA_CAP_HP_CMD_QUEUE) ?
1316 man->headers = dma_pool_create("vmwgfx cmdbuf",
1318 sizeof(SVGACBHeader),
1320 if (!man->headers) {
1325 man->dheaders = dma_pool_create("vmwgfx inline cmdbuf",
1327 sizeof(struct vmw_cmdbuf_dheader),
1329 if (!man->dheaders) {
1334 for_each_cmdbuf_ctx(man, i, ctx)
1335 vmw_cmdbuf_ctx_init(ctx);
1337 INIT_LIST_HEAD(&man->error);
1338 spin_lock_init(&man->lock);
1339 mutex_init(&man->cur_mutex);
1340 mutex_init(&man->space_mutex);
1341 mutex_init(&man->error_mutex);
1342 man->default_size = VMW_CMDBUF_INLINE_SIZE;
1343 init_waitqueue_head(&man->alloc_queue);
1344 init_waitqueue_head(&man->idle_queue);
1345 man->dev_priv = dev_priv;
1346 man->max_hw_submitted = SVGA_CB_MAX_QUEUED_PER_CONTEXT - 1;
1347 INIT_WORK(&man->work, &vmw_cmdbuf_work_func);
1348 vmw_generic_waiter_add(dev_priv, SVGA_IRQFLAG_ERROR,
1349 &dev_priv->error_waiters);
1350 ret = vmw_cmdbuf_startstop(man, 0, true);
1352 DRM_ERROR("Failed starting command buffer contexts\n");
1353 vmw_cmdbuf_man_destroy(man);
1354 return ERR_PTR(ret);
1360 dma_pool_destroy(man->headers);
1364 return ERR_PTR(ret);
1368 * vmw_cmdbuf_remove_pool - Take down the main buffer space pool.
1370 * @man: Pointer to a command buffer manager.
1372 * This function removes the main buffer space pool, and should be called
1373 * before MOB memory management is removed. When this function has been called,
1374 * only small command buffer submissions of size VMW_CMDBUF_INLINE_SIZE or
1375 * less are allowed, and the default size of the command buffer for small kernel
1376 * submissions is also set to this size.
1378 void vmw_cmdbuf_remove_pool(struct vmw_cmdbuf_man *man)
1383 man->has_pool = false;
1384 man->default_size = VMW_CMDBUF_INLINE_SIZE;
1385 (void) vmw_cmdbuf_idle(man, false, 10*HZ);
1386 if (man->using_mob) {
1387 (void) ttm_bo_kunmap(&man->map_obj);
1388 ttm_bo_put(man->cmd_space);
1389 man->cmd_space = NULL;
1391 dma_free_coherent(man->dev_priv->drm.dev,
1392 man->size, man->map, man->handle);
1397 * vmw_cmdbuf_man_destroy - Take down a command buffer manager.
1399 * @man: Pointer to a command buffer manager.
1401 * This function idles and then destroys a command buffer manager.
1403 void vmw_cmdbuf_man_destroy(struct vmw_cmdbuf_man *man)
1405 WARN_ON_ONCE(man->has_pool);
1406 (void) vmw_cmdbuf_idle(man, false, 10*HZ);
1408 if (vmw_cmdbuf_startstop(man, 0, false))
1409 DRM_ERROR("Failed stopping command buffer contexts.\n");
1411 vmw_generic_waiter_remove(man->dev_priv, SVGA_IRQFLAG_ERROR,
1412 &man->dev_priv->error_waiters);
1413 (void) cancel_work_sync(&man->work);
1414 dma_pool_destroy(man->dheaders);
1415 dma_pool_destroy(man->headers);
1416 mutex_destroy(&man->cur_mutex);
1417 mutex_destroy(&man->space_mutex);
1418 mutex_destroy(&man->error_mutex);