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
52 struct vmw_cmdbuf_context
{
53 struct list_head submitted
;
54 struct list_head hw_submitted
;
55 struct list_head preempted
;
56 unsigned num_hw_submitted
;
57 bool block_submission
;
61 * struct vmw_cmdbuf_man: - Command buffer manager
63 * @cur_mutex: Mutex protecting the command buffer used for incremental small
64 * kernel command submissions, @cur.
65 * @space_mutex: Mutex to protect against starvation when we allocate
66 * main pool buffer space.
67 * @error_mutex: Mutex to serialize the work queue error handling.
68 * Note this is not needed if the same workqueue handler
69 * can't race with itself...
70 * @work: A struct work_struct implementeing command buffer error handling.
72 * @dev_priv: Pointer to the device private struct. Immutable.
73 * @ctx: Array of command buffer context queues. The queues and the context
74 * data is protected by @lock.
75 * @error: List of command buffers that have caused device errors.
77 * @mm: Range manager for the command buffer space. Manager allocations and
78 * frees are protected by @lock.
79 * @cmd_space: Buffer object for the command buffer space, unless we were
80 * able to make a contigous coherent DMA memory allocation, @handle. Immutable.
81 * @map_obj: Mapping state for @cmd_space. Immutable.
82 * @map: Pointer to command buffer space. May be a mapped buffer object or
83 * a contigous coherent DMA memory allocation. Immutable.
84 * @cur: Command buffer for small kernel command submissions. Protected by
86 * @cur_pos: Space already used in @cur. Protected by @cur_mutex.
87 * @default_size: Default size for the @cur command buffer. Immutable.
88 * @max_hw_submitted: Max number of in-flight command buffers the device can
90 * @lock: Spinlock protecting command submission queues.
91 * @header: Pool of DMA memory for device command buffer headers.
92 * Internal protection.
93 * @dheaders: Pool of DMA memory for device command buffer headers with trailing
94 * space for inline data. Internal protection.
95 * @alloc_queue: Wait queue for processes waiting to allocate command buffer
97 * @idle_queue: Wait queue for processes waiting for command buffer idle.
98 * @irq_on: Whether the process function has requested irq to be turned on.
100 * @using_mob: Whether the command buffer space is a MOB or a contigous DMA
101 * allocation. Immutable.
102 * @has_pool: Has a large pool of DMA memory which allows larger allocations.
103 * Typically this is false only during bootstrap.
104 * @handle: DMA address handle for the command buffer space if @using_mob is
106 * @size: The size of the command buffer space. Immutable.
107 * @num_contexts: Number of contexts actually enabled.
109 struct vmw_cmdbuf_man
{
110 struct mutex cur_mutex
;
111 struct mutex space_mutex
;
112 struct mutex error_mutex
;
113 struct work_struct work
;
114 struct vmw_private
*dev_priv
;
115 struct vmw_cmdbuf_context ctx
[SVGA_CB_CONTEXT_MAX
];
116 struct list_head error
;
118 struct ttm_buffer_object
*cmd_space
;
119 struct ttm_bo_kmap_obj map_obj
;
121 struct vmw_cmdbuf_header
*cur
;
124 unsigned max_hw_submitted
;
126 struct dma_pool
*headers
;
127 struct dma_pool
*dheaders
;
128 wait_queue_head_t alloc_queue
;
129 wait_queue_head_t idle_queue
;
139 * struct vmw_cmdbuf_header - Command buffer metadata
141 * @man: The command buffer manager.
142 * @cb_header: Device command buffer header, allocated from a DMA pool.
143 * @cb_context: The device command buffer context.
144 * @list: List head for attaching to the manager lists.
145 * @node: The range manager node.
146 * @handle. The DMA address of @cb_header. Handed to the device on command
148 * @cmd: Pointer to the command buffer space of this buffer.
149 * @size: Size of the command buffer space of this buffer.
150 * @reserved: Reserved space of this buffer.
151 * @inline_space: Whether inline command buffer space is used.
153 struct vmw_cmdbuf_header
{
154 struct vmw_cmdbuf_man
*man
;
155 SVGACBHeader
*cb_header
;
156 SVGACBContext cb_context
;
157 struct list_head list
;
158 struct drm_mm_node node
;
167 * struct vmw_cmdbuf_dheader - Device command buffer header with inline
168 * command buffer space.
170 * @cb_header: Device command buffer header.
171 * @cmd: Inline command buffer space.
173 struct vmw_cmdbuf_dheader
{
174 SVGACBHeader cb_header
;
175 u8 cmd
[VMW_CMDBUF_INLINE_SIZE
] __aligned(VMW_CMDBUF_INLINE_ALIGN
);
179 * struct vmw_cmdbuf_alloc_info - Command buffer space allocation metadata
181 * @page_size: Size of requested command buffer space in pages.
182 * @node: Pointer to the range manager node.
183 * @done: True if this allocation has succeeded.
185 struct vmw_cmdbuf_alloc_info
{
187 struct drm_mm_node
*node
;
191 /* Loop over each context in the command buffer manager. */
192 #define for_each_cmdbuf_ctx(_man, _i, _ctx) \
193 for (_i = 0, _ctx = &(_man)->ctx[0]; (_i) < (_man)->num_contexts; \
196 static int vmw_cmdbuf_startstop(struct vmw_cmdbuf_man
*man
, u32 context
,
198 static int vmw_cmdbuf_preempt(struct vmw_cmdbuf_man
*man
, u32 context
);
201 * vmw_cmdbuf_cur_lock - Helper to lock the cur_mutex.
203 * @man: The range manager.
204 * @interruptible: Whether to wait interruptible when locking.
206 static int vmw_cmdbuf_cur_lock(struct vmw_cmdbuf_man
*man
, bool interruptible
)
209 if (mutex_lock_interruptible(&man
->cur_mutex
))
212 mutex_lock(&man
->cur_mutex
);
219 * vmw_cmdbuf_cur_unlock - Helper to unlock the cur_mutex.
221 * @man: The range manager.
223 static void vmw_cmdbuf_cur_unlock(struct vmw_cmdbuf_man
*man
)
225 mutex_unlock(&man
->cur_mutex
);
229 * vmw_cmdbuf_header_inline_free - Free a struct vmw_cmdbuf_header that has
230 * been used for the device context with inline command buffers.
231 * Need not be called locked.
233 * @header: Pointer to the header to free.
235 static void vmw_cmdbuf_header_inline_free(struct vmw_cmdbuf_header
*header
)
237 struct vmw_cmdbuf_dheader
*dheader
;
239 if (WARN_ON_ONCE(!header
->inline_space
))
242 dheader
= container_of(header
->cb_header
, struct vmw_cmdbuf_dheader
,
244 dma_pool_free(header
->man
->dheaders
, dheader
, header
->handle
);
249 * __vmw_cmdbuf_header_free - Free a struct vmw_cmdbuf_header and its
250 * associated structures.
252 * header: Pointer to the header to free.
254 * For internal use. Must be called with man::lock held.
256 static void __vmw_cmdbuf_header_free(struct vmw_cmdbuf_header
*header
)
258 struct vmw_cmdbuf_man
*man
= header
->man
;
260 lockdep_assert_held_once(&man
->lock
);
262 if (header
->inline_space
) {
263 vmw_cmdbuf_header_inline_free(header
);
267 drm_mm_remove_node(&header
->node
);
268 wake_up_all(&man
->alloc_queue
);
269 if (header
->cb_header
)
270 dma_pool_free(man
->headers
, header
->cb_header
,
276 * vmw_cmdbuf_header_free - Free a struct vmw_cmdbuf_header and its
277 * associated structures.
279 * @header: Pointer to the header to free.
281 void vmw_cmdbuf_header_free(struct vmw_cmdbuf_header
*header
)
283 struct vmw_cmdbuf_man
*man
= header
->man
;
285 /* Avoid locking if inline_space */
286 if (header
->inline_space
) {
287 vmw_cmdbuf_header_inline_free(header
);
290 spin_lock(&man
->lock
);
291 __vmw_cmdbuf_header_free(header
);
292 spin_unlock(&man
->lock
);
297 * vmw_cmbuf_header_submit: Submit a command buffer to hardware.
299 * @header: The header of the buffer to submit.
301 static int vmw_cmdbuf_header_submit(struct vmw_cmdbuf_header
*header
)
303 struct vmw_cmdbuf_man
*man
= header
->man
;
306 val
= upper_32_bits(header
->handle
);
307 vmw_write(man
->dev_priv
, SVGA_REG_COMMAND_HIGH
, val
);
309 val
= lower_32_bits(header
->handle
);
310 val
|= header
->cb_context
& SVGA_CB_CONTEXT_MASK
;
311 vmw_write(man
->dev_priv
, SVGA_REG_COMMAND_LOW
, val
);
313 return header
->cb_header
->status
;
317 * vmw_cmdbuf_ctx_init: Initialize a command buffer context.
319 * @ctx: The command buffer context to initialize
321 static void vmw_cmdbuf_ctx_init(struct vmw_cmdbuf_context
*ctx
)
323 INIT_LIST_HEAD(&ctx
->hw_submitted
);
324 INIT_LIST_HEAD(&ctx
->submitted
);
325 INIT_LIST_HEAD(&ctx
->preempted
);
326 ctx
->num_hw_submitted
= 0;
330 * vmw_cmdbuf_ctx_submit: Submit command buffers from a command buffer
333 * @man: The command buffer manager.
334 * @ctx: The command buffer context.
336 * Submits command buffers to hardware until there are no more command
337 * buffers to submit or the hardware can't handle more command buffers.
339 static void vmw_cmdbuf_ctx_submit(struct vmw_cmdbuf_man
*man
,
340 struct vmw_cmdbuf_context
*ctx
)
342 while (ctx
->num_hw_submitted
< man
->max_hw_submitted
&&
343 !list_empty(&ctx
->submitted
) &&
344 !ctx
->block_submission
) {
345 struct vmw_cmdbuf_header
*entry
;
348 entry
= list_first_entry(&ctx
->submitted
,
349 struct vmw_cmdbuf_header
,
352 status
= vmw_cmdbuf_header_submit(entry
);
354 /* This should never happen */
355 if (WARN_ON_ONCE(status
== SVGA_CB_STATUS_QUEUE_FULL
)) {
356 entry
->cb_header
->status
= SVGA_CB_STATUS_NONE
;
360 list_del(&entry
->list
);
361 list_add_tail(&entry
->list
, &ctx
->hw_submitted
);
362 ctx
->num_hw_submitted
++;
368 * vmw_cmdbuf_ctx_submit: Process a command buffer context.
370 * @man: The command buffer manager.
371 * @ctx: The command buffer context.
373 * Submit command buffers to hardware if possible, and process finished
374 * buffers. Typically freeing them, but on preemption or error take
375 * appropriate action. Wake up waiters if appropriate.
377 static void vmw_cmdbuf_ctx_process(struct vmw_cmdbuf_man
*man
,
378 struct vmw_cmdbuf_context
*ctx
,
381 struct vmw_cmdbuf_header
*entry
, *next
;
383 vmw_cmdbuf_ctx_submit(man
, ctx
);
385 list_for_each_entry_safe(entry
, next
, &ctx
->hw_submitted
, list
) {
386 SVGACBStatus status
= entry
->cb_header
->status
;
388 if (status
== SVGA_CB_STATUS_NONE
)
391 list_del(&entry
->list
);
392 wake_up_all(&man
->idle_queue
);
393 ctx
->num_hw_submitted
--;
395 case SVGA_CB_STATUS_COMPLETED
:
396 __vmw_cmdbuf_header_free(entry
);
398 case SVGA_CB_STATUS_COMMAND_ERROR
:
399 WARN_ONCE(true, "Command buffer error.\n");
400 entry
->cb_header
->status
= SVGA_CB_STATUS_NONE
;
401 list_add_tail(&entry
->list
, &man
->error
);
402 schedule_work(&man
->work
);
404 case SVGA_CB_STATUS_PREEMPTED
:
405 entry
->cb_header
->status
= SVGA_CB_STATUS_NONE
;
406 list_add_tail(&entry
->list
, &ctx
->preempted
);
408 case SVGA_CB_STATUS_CB_HEADER_ERROR
:
409 WARN_ONCE(true, "Command buffer header error.\n");
410 __vmw_cmdbuf_header_free(entry
);
413 WARN_ONCE(true, "Undefined command buffer status.\n");
414 __vmw_cmdbuf_header_free(entry
);
419 vmw_cmdbuf_ctx_submit(man
, ctx
);
420 if (!list_empty(&ctx
->submitted
))
425 * vmw_cmdbuf_man_process - Process all command buffer contexts and
426 * switch on and off irqs as appropriate.
428 * @man: The command buffer manager.
430 * Calls vmw_cmdbuf_ctx_process() on all contexts. If any context has
431 * command buffers left that are not submitted to hardware, Make sure
432 * IRQ handling is turned on. Otherwise, make sure it's turned off.
434 static void vmw_cmdbuf_man_process(struct vmw_cmdbuf_man
*man
)
437 struct vmw_cmdbuf_context
*ctx
;
442 for_each_cmdbuf_ctx(man
, i
, ctx
)
443 vmw_cmdbuf_ctx_process(man
, ctx
, ¬empty
);
445 if (man
->irq_on
&& !notempty
) {
446 vmw_generic_waiter_remove(man
->dev_priv
,
447 SVGA_IRQFLAG_COMMAND_BUFFER
,
448 &man
->dev_priv
->cmdbuf_waiters
);
450 } else if (!man
->irq_on
&& notempty
) {
451 vmw_generic_waiter_add(man
->dev_priv
,
452 SVGA_IRQFLAG_COMMAND_BUFFER
,
453 &man
->dev_priv
->cmdbuf_waiters
);
456 /* Rerun in case we just missed an irq. */
462 * vmw_cmdbuf_ctx_add - Schedule a command buffer for submission on a
463 * command buffer context
465 * @man: The command buffer manager.
466 * @header: The header of the buffer to submit.
467 * @cb_context: The command buffer context to use.
469 * This function adds @header to the "submitted" queue of the command
470 * buffer context identified by @cb_context. It then calls the command buffer
471 * manager processing to potentially submit the buffer to hardware.
472 * @man->lock needs to be held when calling this function.
474 static void vmw_cmdbuf_ctx_add(struct vmw_cmdbuf_man
*man
,
475 struct vmw_cmdbuf_header
*header
,
476 SVGACBContext cb_context
)
478 if (!(header
->cb_header
->flags
& SVGA_CB_FLAG_DX_CONTEXT
))
479 header
->cb_header
->dxContext
= 0;
480 header
->cb_context
= cb_context
;
481 list_add_tail(&header
->list
, &man
->ctx
[cb_context
].submitted
);
483 vmw_cmdbuf_man_process(man
);
487 * vmw_cmdbuf_irqthread - The main part of the command buffer interrupt
488 * handler implemented as a threaded irq task.
490 * @man: Pointer to the command buffer manager.
492 * The bottom half of the interrupt handler simply calls into the
493 * command buffer processor to free finished buffers and submit any
494 * queued buffers to hardware.
496 void vmw_cmdbuf_irqthread(struct vmw_cmdbuf_man
*man
)
498 spin_lock(&man
->lock
);
499 vmw_cmdbuf_man_process(man
);
500 spin_unlock(&man
->lock
);
504 * vmw_cmdbuf_work_func - The deferred work function that handles
505 * command buffer errors.
507 * @work: The work func closure argument.
509 * Restarting the command buffer context after an error requires process
510 * context, so it is deferred to this work function.
512 static void vmw_cmdbuf_work_func(struct work_struct
*work
)
514 struct vmw_cmdbuf_man
*man
=
515 container_of(work
, struct vmw_cmdbuf_man
, work
);
516 struct vmw_cmdbuf_header
*entry
, *next
;
518 bool send_fence
= false;
519 struct list_head restart_head
[SVGA_CB_CONTEXT_MAX
];
521 struct vmw_cmdbuf_context
*ctx
;
522 bool global_block
= false;
524 for_each_cmdbuf_ctx(man
, i
, ctx
)
525 INIT_LIST_HEAD(&restart_head
[i
]);
527 mutex_lock(&man
->error_mutex
);
528 spin_lock(&man
->lock
);
529 list_for_each_entry_safe(entry
, next
, &man
->error
, list
) {
530 SVGACBHeader
*cb_hdr
= entry
->cb_header
;
531 SVGA3dCmdHeader
*header
= (SVGA3dCmdHeader
*)
532 (entry
->cmd
+ cb_hdr
->errorOffset
);
533 u32 error_cmd_size
, new_start_offset
;
534 const char *cmd_name
;
536 list_del_init(&entry
->list
);
539 if (!vmw_cmd_describe(header
, &error_cmd_size
, &cmd_name
)) {
540 VMW_DEBUG_USER("Unknown command causing device error.\n");
541 VMW_DEBUG_USER("Command buffer offset is %lu\n",
542 (unsigned long) cb_hdr
->errorOffset
);
543 __vmw_cmdbuf_header_free(entry
);
548 VMW_DEBUG_USER("Command \"%s\" causing device error.\n",
550 VMW_DEBUG_USER("Command buffer offset is %lu\n",
551 (unsigned long) cb_hdr
->errorOffset
);
552 VMW_DEBUG_USER("Command size is %lu\n",
553 (unsigned long) error_cmd_size
);
555 new_start_offset
= cb_hdr
->errorOffset
+ error_cmd_size
;
557 if (new_start_offset
>= cb_hdr
->length
) {
558 __vmw_cmdbuf_header_free(entry
);
564 cb_hdr
->ptr
.mob
.mobOffset
+= new_start_offset
;
566 cb_hdr
->ptr
.pa
+= (u64
) new_start_offset
;
568 entry
->cmd
+= new_start_offset
;
569 cb_hdr
->length
-= new_start_offset
;
570 cb_hdr
->errorOffset
= 0;
573 list_add_tail(&entry
->list
, &restart_head
[entry
->cb_context
]);
576 for_each_cmdbuf_ctx(man
, i
, ctx
)
577 man
->ctx
[i
].block_submission
= true;
579 spin_unlock(&man
->lock
);
581 /* Preempt all contexts */
582 if (global_block
&& vmw_cmdbuf_preempt(man
, 0))
583 DRM_ERROR("Failed preempting command buffer contexts\n");
585 spin_lock(&man
->lock
);
586 for_each_cmdbuf_ctx(man
, i
, ctx
) {
587 /* Move preempted command buffers to the preempted queue. */
588 vmw_cmdbuf_ctx_process(man
, ctx
, &dummy
);
591 * Add the preempted queue after the command buffer
592 * that caused an error.
594 list_splice_init(&ctx
->preempted
, restart_head
[i
].prev
);
597 * Finally add all command buffers first in the submitted
598 * queue, to rerun them.
601 ctx
->block_submission
= false;
602 list_splice_init(&restart_head
[i
], &ctx
->submitted
);
605 vmw_cmdbuf_man_process(man
);
606 spin_unlock(&man
->lock
);
608 if (global_block
&& vmw_cmdbuf_startstop(man
, 0, true))
609 DRM_ERROR("Failed restarting command buffer contexts\n");
611 /* Send a new fence in case one was removed */
613 vmw_fifo_send_fence(man
->dev_priv
, &dummy
);
614 wake_up_all(&man
->idle_queue
);
617 mutex_unlock(&man
->error_mutex
);
621 * vmw_cmdbuf_man idle - Check whether the command buffer manager is idle.
623 * @man: The command buffer manager.
624 * @check_preempted: Check also the preempted queue for pending command buffers.
627 static bool vmw_cmdbuf_man_idle(struct vmw_cmdbuf_man
*man
,
628 bool check_preempted
)
630 struct vmw_cmdbuf_context
*ctx
;
634 spin_lock(&man
->lock
);
635 vmw_cmdbuf_man_process(man
);
636 for_each_cmdbuf_ctx(man
, i
, ctx
) {
637 if (!list_empty(&ctx
->submitted
) ||
638 !list_empty(&ctx
->hw_submitted
) ||
639 (check_preempted
&& !list_empty(&ctx
->preempted
)))
643 idle
= list_empty(&man
->error
);
646 spin_unlock(&man
->lock
);
652 * __vmw_cmdbuf_cur_flush - Flush the current command buffer for small kernel
653 * command submissions
655 * @man: The command buffer manager.
657 * Flushes the current command buffer without allocating a new one. A new one
658 * is automatically allocated when needed. Call with @man->cur_mutex held.
660 static void __vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man
*man
)
662 struct vmw_cmdbuf_header
*cur
= man
->cur
;
664 lockdep_assert_held_once(&man
->cur_mutex
);
669 spin_lock(&man
->lock
);
670 if (man
->cur_pos
== 0) {
671 __vmw_cmdbuf_header_free(cur
);
675 man
->cur
->cb_header
->length
= man
->cur_pos
;
676 vmw_cmdbuf_ctx_add(man
, man
->cur
, SVGA_CB_CONTEXT_0
);
678 spin_unlock(&man
->lock
);
684 * vmw_cmdbuf_cur_flush - Flush the current command buffer for small kernel
685 * command submissions
687 * @man: The command buffer manager.
688 * @interruptible: Whether to sleep interruptible when sleeping.
690 * Flushes the current command buffer without allocating a new one. A new one
691 * is automatically allocated when needed.
693 int vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man
*man
,
696 int ret
= vmw_cmdbuf_cur_lock(man
, interruptible
);
701 __vmw_cmdbuf_cur_flush(man
);
702 vmw_cmdbuf_cur_unlock(man
);
708 * vmw_cmdbuf_idle - Wait for command buffer manager idle.
710 * @man: The command buffer manager.
711 * @interruptible: Sleep interruptible while waiting.
712 * @timeout: Time out after this many ticks.
714 * Wait until the command buffer manager has processed all command buffers,
715 * or until a timeout occurs. If a timeout occurs, the function will return
718 int vmw_cmdbuf_idle(struct vmw_cmdbuf_man
*man
, bool interruptible
,
719 unsigned long timeout
)
723 ret
= vmw_cmdbuf_cur_flush(man
, interruptible
);
724 vmw_generic_waiter_add(man
->dev_priv
,
725 SVGA_IRQFLAG_COMMAND_BUFFER
,
726 &man
->dev_priv
->cmdbuf_waiters
);
729 ret
= wait_event_interruptible_timeout
730 (man
->idle_queue
, vmw_cmdbuf_man_idle(man
, true),
733 ret
= wait_event_timeout
734 (man
->idle_queue
, vmw_cmdbuf_man_idle(man
, true),
737 vmw_generic_waiter_remove(man
->dev_priv
,
738 SVGA_IRQFLAG_COMMAND_BUFFER
,
739 &man
->dev_priv
->cmdbuf_waiters
);
741 if (!vmw_cmdbuf_man_idle(man
, true))
753 * vmw_cmdbuf_try_alloc - Try to allocate buffer space from the main pool.
755 * @man: The command buffer manager.
756 * @info: Allocation info. Will hold the size on entry and allocated mm node
757 * on successful return.
759 * Try to allocate buffer space from the main pool. Returns true if succeeded.
760 * If a fatal error was hit, the error code is returned in @info->ret.
762 static bool vmw_cmdbuf_try_alloc(struct vmw_cmdbuf_man
*man
,
763 struct vmw_cmdbuf_alloc_info
*info
)
770 memset(info
->node
, 0, sizeof(*info
->node
));
771 spin_lock(&man
->lock
);
772 ret
= drm_mm_insert_node(&man
->mm
, info
->node
, info
->page_size
);
774 vmw_cmdbuf_man_process(man
);
775 ret
= drm_mm_insert_node(&man
->mm
, info
->node
, info
->page_size
);
778 spin_unlock(&man
->lock
);
785 * vmw_cmdbuf_alloc_space - Allocate buffer space from the main pool.
787 * @man: The command buffer manager.
788 * @node: Pointer to pre-allocated range-manager node.
789 * @size: The size of the allocation.
790 * @interruptible: Whether to sleep interruptible while waiting for space.
792 * This function allocates buffer space from the main pool, and if there is
793 * no space available ATM, it turns on IRQ handling and sleeps waiting for it to
796 static int vmw_cmdbuf_alloc_space(struct vmw_cmdbuf_man
*man
,
797 struct drm_mm_node
*node
,
801 struct vmw_cmdbuf_alloc_info info
;
803 info
.page_size
= PAGE_ALIGN(size
) >> PAGE_SHIFT
;
808 * To prevent starvation of large requests, only one allocating call
809 * at a time waiting for space.
812 if (mutex_lock_interruptible(&man
->space_mutex
))
815 mutex_lock(&man
->space_mutex
);
818 /* Try to allocate space without waiting. */
819 if (vmw_cmdbuf_try_alloc(man
, &info
))
822 vmw_generic_waiter_add(man
->dev_priv
,
823 SVGA_IRQFLAG_COMMAND_BUFFER
,
824 &man
->dev_priv
->cmdbuf_waiters
);
829 ret
= wait_event_interruptible
830 (man
->alloc_queue
, vmw_cmdbuf_try_alloc(man
, &info
));
832 vmw_generic_waiter_remove
833 (man
->dev_priv
, SVGA_IRQFLAG_COMMAND_BUFFER
,
834 &man
->dev_priv
->cmdbuf_waiters
);
835 mutex_unlock(&man
->space_mutex
);
839 wait_event(man
->alloc_queue
, vmw_cmdbuf_try_alloc(man
, &info
));
841 vmw_generic_waiter_remove(man
->dev_priv
,
842 SVGA_IRQFLAG_COMMAND_BUFFER
,
843 &man
->dev_priv
->cmdbuf_waiters
);
846 mutex_unlock(&man
->space_mutex
);
852 * vmw_cmdbuf_space_pool - Set up a command buffer header with command buffer
853 * space from the main pool.
855 * @man: The command buffer manager.
856 * @header: Pointer to the header to set up.
857 * @size: The requested size of the buffer space.
858 * @interruptible: Whether to sleep interruptible while waiting for space.
860 static int vmw_cmdbuf_space_pool(struct vmw_cmdbuf_man
*man
,
861 struct vmw_cmdbuf_header
*header
,
865 SVGACBHeader
*cb_hdr
;
872 ret
= vmw_cmdbuf_alloc_space(man
, &header
->node
, size
, interruptible
);
877 header
->cb_header
= dma_pool_zalloc(man
->headers
, GFP_KERNEL
,
879 if (!header
->cb_header
) {
881 goto out_no_cb_header
;
884 header
->size
= header
->node
.size
<< PAGE_SHIFT
;
885 cb_hdr
= header
->cb_header
;
886 offset
= header
->node
.start
<< PAGE_SHIFT
;
887 header
->cmd
= man
->map
+ offset
;
888 if (man
->using_mob
) {
889 cb_hdr
->flags
= SVGA_CB_FLAG_MOB
;
890 cb_hdr
->ptr
.mob
.mobid
= man
->cmd_space
->mem
.start
;
891 cb_hdr
->ptr
.mob
.mobOffset
= offset
;
893 cb_hdr
->ptr
.pa
= (u64
)man
->handle
+ (u64
)offset
;
899 spin_lock(&man
->lock
);
900 drm_mm_remove_node(&header
->node
);
901 spin_unlock(&man
->lock
);
907 * vmw_cmdbuf_space_inline - Set up a command buffer header with
908 * inline command buffer space.
910 * @man: The command buffer manager.
911 * @header: Pointer to the header to set up.
912 * @size: The requested size of the buffer space.
914 static int vmw_cmdbuf_space_inline(struct vmw_cmdbuf_man
*man
,
915 struct vmw_cmdbuf_header
*header
,
918 struct vmw_cmdbuf_dheader
*dheader
;
919 SVGACBHeader
*cb_hdr
;
921 if (WARN_ON_ONCE(size
> VMW_CMDBUF_INLINE_SIZE
))
924 dheader
= dma_pool_zalloc(man
->dheaders
, GFP_KERNEL
,
929 header
->inline_space
= true;
930 header
->size
= VMW_CMDBUF_INLINE_SIZE
;
931 cb_hdr
= &dheader
->cb_header
;
932 header
->cb_header
= cb_hdr
;
933 header
->cmd
= dheader
->cmd
;
934 cb_hdr
->status
= SVGA_CB_STATUS_NONE
;
935 cb_hdr
->flags
= SVGA_CB_FLAG_NONE
;
936 cb_hdr
->ptr
.pa
= (u64
)header
->handle
+
937 (u64
)offsetof(struct vmw_cmdbuf_dheader
, cmd
);
943 * vmw_cmdbuf_alloc - Allocate a command buffer header complete with
944 * command buffer space.
946 * @man: The command buffer manager.
947 * @size: The requested size of the buffer space.
948 * @interruptible: Whether to sleep interruptible while waiting for space.
949 * @p_header: points to a header pointer to populate on successful return.
951 * Returns a pointer to command buffer space if successful. Otherwise
952 * returns an error pointer. The header pointer returned in @p_header should
953 * be used for upcoming calls to vmw_cmdbuf_reserve() and vmw_cmdbuf_commit().
955 void *vmw_cmdbuf_alloc(struct vmw_cmdbuf_man
*man
,
956 size_t size
, bool interruptible
,
957 struct vmw_cmdbuf_header
**p_header
)
959 struct vmw_cmdbuf_header
*header
;
964 header
= kzalloc(sizeof(*header
), GFP_KERNEL
);
966 return ERR_PTR(-ENOMEM
);
968 if (size
<= VMW_CMDBUF_INLINE_SIZE
)
969 ret
= vmw_cmdbuf_space_inline(man
, header
, size
);
971 ret
= vmw_cmdbuf_space_pool(man
, header
, size
, interruptible
);
979 INIT_LIST_HEAD(&header
->list
);
980 header
->cb_header
->status
= SVGA_CB_STATUS_NONE
;
987 * vmw_cmdbuf_reserve_cur - Reserve space for commands in the current
990 * @man: The command buffer manager.
991 * @size: The requested size of the commands.
992 * @ctx_id: The context id if any. Otherwise set to SVGA3D_REG_INVALID.
993 * @interruptible: Whether to sleep interruptible while waiting for space.
995 * Returns a pointer to command buffer space if successful. Otherwise
996 * returns an error pointer.
998 static void *vmw_cmdbuf_reserve_cur(struct vmw_cmdbuf_man
*man
,
1003 struct vmw_cmdbuf_header
*cur
;
1006 if (vmw_cmdbuf_cur_lock(man
, interruptible
))
1007 return ERR_PTR(-ERESTARTSYS
);
1010 if (cur
&& (size
+ man
->cur_pos
> cur
->size
||
1011 ((cur
->cb_header
->flags
& SVGA_CB_FLAG_DX_CONTEXT
) &&
1012 ctx_id
!= cur
->cb_header
->dxContext
)))
1013 __vmw_cmdbuf_cur_flush(man
);
1016 ret
= vmw_cmdbuf_alloc(man
,
1017 max_t(size_t, size
, man
->default_size
),
1018 interruptible
, &man
->cur
);
1020 vmw_cmdbuf_cur_unlock(man
);
1027 if (ctx_id
!= SVGA3D_INVALID_ID
) {
1028 cur
->cb_header
->flags
|= SVGA_CB_FLAG_DX_CONTEXT
;
1029 cur
->cb_header
->dxContext
= ctx_id
;
1032 cur
->reserved
= size
;
1034 return (void *) (man
->cur
->cmd
+ man
->cur_pos
);
1038 * vmw_cmdbuf_commit_cur - Commit commands in the current command buffer.
1040 * @man: The command buffer manager.
1041 * @size: The size of the commands actually written.
1042 * @flush: Whether to flush the command buffer immediately.
1044 static void vmw_cmdbuf_commit_cur(struct vmw_cmdbuf_man
*man
,
1045 size_t size
, bool flush
)
1047 struct vmw_cmdbuf_header
*cur
= man
->cur
;
1049 lockdep_assert_held_once(&man
->cur_mutex
);
1051 WARN_ON(size
> cur
->reserved
);
1052 man
->cur_pos
+= size
;
1054 cur
->cb_header
->flags
&= ~SVGA_CB_FLAG_DX_CONTEXT
;
1056 __vmw_cmdbuf_cur_flush(man
);
1057 vmw_cmdbuf_cur_unlock(man
);
1061 * vmw_cmdbuf_reserve - Reserve space for commands in a command buffer.
1063 * @man: The command buffer manager.
1064 * @size: The requested size of the commands.
1065 * @ctx_id: The context id if any. Otherwise set to SVGA3D_REG_INVALID.
1066 * @interruptible: Whether to sleep interruptible while waiting for space.
1067 * @header: Header of the command buffer. NULL if the current command buffer
1070 * Returns a pointer to command buffer space if successful. Otherwise
1071 * returns an error pointer.
1073 void *vmw_cmdbuf_reserve(struct vmw_cmdbuf_man
*man
, size_t size
,
1074 int ctx_id
, bool interruptible
,
1075 struct vmw_cmdbuf_header
*header
)
1078 return vmw_cmdbuf_reserve_cur(man
, size
, ctx_id
, interruptible
);
1080 if (size
> header
->size
)
1081 return ERR_PTR(-EINVAL
);
1083 if (ctx_id
!= SVGA3D_INVALID_ID
) {
1084 header
->cb_header
->flags
|= SVGA_CB_FLAG_DX_CONTEXT
;
1085 header
->cb_header
->dxContext
= ctx_id
;
1088 header
->reserved
= size
;
1093 * vmw_cmdbuf_commit - Commit commands in a command buffer.
1095 * @man: The command buffer manager.
1096 * @size: The size of the commands actually written.
1097 * @header: Header of the command buffer. NULL if the current command buffer
1099 * @flush: Whether to flush the command buffer immediately.
1101 void vmw_cmdbuf_commit(struct vmw_cmdbuf_man
*man
, size_t size
,
1102 struct vmw_cmdbuf_header
*header
, bool flush
)
1105 vmw_cmdbuf_commit_cur(man
, size
, flush
);
1109 (void) vmw_cmdbuf_cur_lock(man
, false);
1110 __vmw_cmdbuf_cur_flush(man
);
1111 WARN_ON(size
> header
->reserved
);
1113 man
->cur_pos
= size
;
1115 header
->cb_header
->flags
&= ~SVGA_CB_FLAG_DX_CONTEXT
;
1117 __vmw_cmdbuf_cur_flush(man
);
1118 vmw_cmdbuf_cur_unlock(man
);
1123 * vmw_cmdbuf_send_device_command - Send a command through the device context.
1125 * @man: The command buffer manager.
1126 * @command: Pointer to the command to send.
1127 * @size: Size of the command.
1129 * Synchronously sends a device context command.
1131 static int vmw_cmdbuf_send_device_command(struct vmw_cmdbuf_man
*man
,
1132 const void *command
,
1135 struct vmw_cmdbuf_header
*header
;
1137 void *cmd
= vmw_cmdbuf_alloc(man
, size
, false, &header
);
1140 return PTR_ERR(cmd
);
1142 memcpy(cmd
, command
, size
);
1143 header
->cb_header
->length
= size
;
1144 header
->cb_context
= SVGA_CB_CONTEXT_DEVICE
;
1145 spin_lock(&man
->lock
);
1146 status
= vmw_cmdbuf_header_submit(header
);
1147 spin_unlock(&man
->lock
);
1148 vmw_cmdbuf_header_free(header
);
1150 if (status
!= SVGA_CB_STATUS_COMPLETED
) {
1151 DRM_ERROR("Device context command failed with status %d\n",
1160 * vmw_cmdbuf_preempt - Send a preempt command through the device
1163 * @man: The command buffer manager.
1165 * Synchronously sends a preempt command.
1167 static int vmw_cmdbuf_preempt(struct vmw_cmdbuf_man
*man
, u32 context
)
1171 SVGADCCmdPreempt body
;
1174 cmd
.id
= SVGA_DC_CMD_PREEMPT
;
1175 cmd
.body
.context
= SVGA_CB_CONTEXT_0
+ context
;
1176 cmd
.body
.ignoreIDZero
= 0;
1178 return vmw_cmdbuf_send_device_command(man
, &cmd
, sizeof(cmd
));
1183 * vmw_cmdbuf_startstop - Send a start / stop command through the device
1186 * @man: The command buffer manager.
1187 * @enable: Whether to enable or disable the context.
1189 * Synchronously sends a device start / stop context command.
1191 static int vmw_cmdbuf_startstop(struct vmw_cmdbuf_man
*man
, u32 context
,
1196 SVGADCCmdStartStop body
;
1199 cmd
.id
= SVGA_DC_CMD_START_STOP_CONTEXT
;
1200 cmd
.body
.enable
= (enable
) ? 1 : 0;
1201 cmd
.body
.context
= SVGA_CB_CONTEXT_0
+ context
;
1203 return vmw_cmdbuf_send_device_command(man
, &cmd
, sizeof(cmd
));
1207 * vmw_cmdbuf_set_pool_size - Set command buffer manager sizes
1209 * @man: The command buffer manager.
1210 * @size: The size of the main space pool.
1211 * @default_size: The default size of the command buffer for small kernel
1214 * Set the size and allocate the main command buffer space pool,
1215 * as well as the default size of the command buffer for
1216 * small kernel submissions. If successful, this enables large command
1217 * submissions. Note that this function requires that rudimentary command
1218 * submission is already available and that the MOB memory manager is alive.
1219 * Returns 0 on success. Negative error code on failure.
1221 int vmw_cmdbuf_set_pool_size(struct vmw_cmdbuf_man
*man
,
1222 size_t size
, size_t default_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
->dev
->pdev
->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("Using command buffers with %s pool.\n",
1276 (man
->using_mob
) ? "MOB" : "DMA");
1281 if (man
->using_mob
) {
1282 ttm_bo_put(man
->cmd_space
);
1283 man
->cmd_space
= NULL
;
1290 * vmw_cmdbuf_man_create: Create a command buffer manager and enable it for
1291 * inline command buffer submissions only.
1293 * @dev_priv: Pointer to device private structure.
1295 * Returns a pointer to a cummand buffer manager to success or error pointer
1296 * on failure. The command buffer manager will be enabled for submissions of
1297 * size VMW_CMDBUF_INLINE_SIZE only.
1299 struct vmw_cmdbuf_man
*vmw_cmdbuf_man_create(struct vmw_private
*dev_priv
)
1301 struct vmw_cmdbuf_man
*man
;
1302 struct vmw_cmdbuf_context
*ctx
;
1306 if (!(dev_priv
->capabilities
& SVGA_CAP_COMMAND_BUFFERS
))
1307 return ERR_PTR(-ENOSYS
);
1309 man
= kzalloc(sizeof(*man
), GFP_KERNEL
);
1311 return ERR_PTR(-ENOMEM
);
1313 man
->num_contexts
= (dev_priv
->capabilities
& SVGA_CAP_HP_CMD_QUEUE
) ?
1315 man
->headers
= dma_pool_create("vmwgfx cmdbuf",
1316 &dev_priv
->dev
->pdev
->dev
,
1317 sizeof(SVGACBHeader
),
1319 if (!man
->headers
) {
1324 man
->dheaders
= dma_pool_create("vmwgfx inline cmdbuf",
1325 &dev_priv
->dev
->pdev
->dev
,
1326 sizeof(struct vmw_cmdbuf_dheader
),
1328 if (!man
->dheaders
) {
1333 for_each_cmdbuf_ctx(man
, i
, ctx
)
1334 vmw_cmdbuf_ctx_init(ctx
);
1336 INIT_LIST_HEAD(&man
->error
);
1337 spin_lock_init(&man
->lock
);
1338 mutex_init(&man
->cur_mutex
);
1339 mutex_init(&man
->space_mutex
);
1340 mutex_init(&man
->error_mutex
);
1341 man
->default_size
= VMW_CMDBUF_INLINE_SIZE
;
1342 init_waitqueue_head(&man
->alloc_queue
);
1343 init_waitqueue_head(&man
->idle_queue
);
1344 man
->dev_priv
= dev_priv
;
1345 man
->max_hw_submitted
= SVGA_CB_MAX_QUEUED_PER_CONTEXT
- 1;
1346 INIT_WORK(&man
->work
, &vmw_cmdbuf_work_func
);
1347 vmw_generic_waiter_add(dev_priv
, SVGA_IRQFLAG_ERROR
,
1348 &dev_priv
->error_waiters
);
1349 ret
= vmw_cmdbuf_startstop(man
, 0, true);
1351 DRM_ERROR("Failed starting command buffer contexts\n");
1352 vmw_cmdbuf_man_destroy(man
);
1353 return ERR_PTR(ret
);
1359 dma_pool_destroy(man
->headers
);
1363 return ERR_PTR(ret
);
1367 * vmw_cmdbuf_remove_pool - Take down the main buffer space pool.
1369 * @man: Pointer to a command buffer manager.
1371 * This function removes the main buffer space pool, and should be called
1372 * before MOB memory management is removed. When this function has been called,
1373 * only small command buffer submissions of size VMW_CMDBUF_INLINE_SIZE or
1374 * less are allowed, and the default size of the command buffer for small kernel
1375 * submissions is also set to this size.
1377 void vmw_cmdbuf_remove_pool(struct vmw_cmdbuf_man
*man
)
1382 man
->has_pool
= false;
1383 man
->default_size
= VMW_CMDBUF_INLINE_SIZE
;
1384 (void) vmw_cmdbuf_idle(man
, false, 10*HZ
);
1385 if (man
->using_mob
) {
1386 (void) ttm_bo_kunmap(&man
->map_obj
);
1387 ttm_bo_put(man
->cmd_space
);
1388 man
->cmd_space
= NULL
;
1390 dma_free_coherent(&man
->dev_priv
->dev
->pdev
->dev
,
1391 man
->size
, man
->map
, man
->handle
);
1396 * vmw_cmdbuf_man_destroy - Take down a command buffer manager.
1398 * @man: Pointer to a command buffer manager.
1400 * This function idles and then destroys a command buffer manager.
1402 void vmw_cmdbuf_man_destroy(struct vmw_cmdbuf_man
*man
)
1404 WARN_ON_ONCE(man
->has_pool
);
1405 (void) vmw_cmdbuf_idle(man
, false, 10*HZ
);
1407 if (vmw_cmdbuf_startstop(man
, 0, false))
1408 DRM_ERROR("Failed stopping command buffer contexts.\n");
1410 vmw_generic_waiter_remove(man
->dev_priv
, SVGA_IRQFLAG_ERROR
,
1411 &man
->dev_priv
->error_waiters
);
1412 (void) cancel_work_sync(&man
->work
);
1413 dma_pool_destroy(man
->dheaders
);
1414 dma_pool_destroy(man
->headers
);
1415 mutex_destroy(&man
->cur_mutex
);
1416 mutex_destroy(&man
->space_mutex
);
1417 mutex_destroy(&man
->error_mutex
);