1 /**************************************************************************
3 * 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 "vmwgfx_drv.h"
29 #include "ttm/ttm_bo_api.h"
32 * Size of inline command buffers. Try to make sure that a page size is a
33 * multiple of the DMA pool allocation size.
35 #define VMW_CMDBUF_INLINE_ALIGN 64
36 #define VMW_CMDBUF_INLINE_SIZE \
37 (1024 - ALIGN(sizeof(SVGACBHeader), VMW_CMDBUF_INLINE_ALIGN))
40 * struct vmw_cmdbuf_context - Command buffer context queues
42 * @submitted: List of command buffers that have been submitted to the
43 * manager but not yet submitted to hardware.
44 * @hw_submitted: List of command buffers submitted to hardware.
45 * @preempted: List of preempted command buffers.
46 * @num_hw_submitted: Number of buffers currently being processed by hardware
48 struct vmw_cmdbuf_context
{
49 struct list_head submitted
;
50 struct list_head hw_submitted
;
51 struct list_head preempted
;
52 unsigned num_hw_submitted
;
56 * struct vmw_cmdbuf_man: - Command buffer manager
58 * @cur_mutex: Mutex protecting the command buffer used for incremental small
59 * kernel command submissions, @cur.
60 * @space_mutex: Mutex to protect against starvation when we allocate
61 * main pool buffer space.
62 * @work: A struct work_struct implementeing command buffer error handling.
64 * @dev_priv: Pointer to the device private struct. Immutable.
65 * @ctx: Array of command buffer context queues. The queues and the context
66 * data is protected by @lock.
67 * @error: List of command buffers that have caused device errors.
69 * @mm: Range manager for the command buffer space. Manager allocations and
70 * frees are protected by @lock.
71 * @cmd_space: Buffer object for the command buffer space, unless we were
72 * able to make a contigous coherent DMA memory allocation, @handle. Immutable.
73 * @map_obj: Mapping state for @cmd_space. Immutable.
74 * @map: Pointer to command buffer space. May be a mapped buffer object or
75 * a contigous coherent DMA memory allocation. Immutable.
76 * @cur: Command buffer for small kernel command submissions. Protected by
78 * @cur_pos: Space already used in @cur. Protected by @cur_mutex.
79 * @default_size: Default size for the @cur command buffer. Immutable.
80 * @max_hw_submitted: Max number of in-flight command buffers the device can
82 * @lock: Spinlock protecting command submission queues.
83 * @header: Pool of DMA memory for device command buffer headers.
84 * Internal protection.
85 * @dheaders: Pool of DMA memory for device command buffer headers with trailing
86 * space for inline data. Internal protection.
87 * @tasklet: Tasklet struct for irq processing. Immutable.
88 * @alloc_queue: Wait queue for processes waiting to allocate command buffer
90 * @idle_queue: Wait queue for processes waiting for command buffer idle.
91 * @irq_on: Whether the process function has requested irq to be turned on.
93 * @using_mob: Whether the command buffer space is a MOB or a contigous DMA
94 * allocation. Immutable.
95 * @has_pool: Has a large pool of DMA memory which allows larger allocations.
96 * Typically this is false only during bootstrap.
97 * @handle: DMA address handle for the command buffer space if @using_mob is
99 * @size: The size of the command buffer space. Immutable.
101 struct vmw_cmdbuf_man
{
102 struct mutex cur_mutex
;
103 struct mutex space_mutex
;
104 struct work_struct work
;
105 struct vmw_private
*dev_priv
;
106 struct vmw_cmdbuf_context ctx
[SVGA_CB_CONTEXT_MAX
];
107 struct list_head error
;
109 struct ttm_buffer_object
*cmd_space
;
110 struct ttm_bo_kmap_obj map_obj
;
112 struct vmw_cmdbuf_header
*cur
;
115 unsigned max_hw_submitted
;
117 struct dma_pool
*headers
;
118 struct dma_pool
*dheaders
;
119 struct tasklet_struct tasklet
;
120 wait_queue_head_t alloc_queue
;
121 wait_queue_head_t idle_queue
;
130 * struct vmw_cmdbuf_header - Command buffer metadata
132 * @man: The command buffer manager.
133 * @cb_header: Device command buffer header, allocated from a DMA pool.
134 * @cb_context: The device command buffer context.
135 * @list: List head for attaching to the manager lists.
136 * @node: The range manager node.
137 * @handle. The DMA address of @cb_header. Handed to the device on command
139 * @cmd: Pointer to the command buffer space of this buffer.
140 * @size: Size of the command buffer space of this buffer.
141 * @reserved: Reserved space of this buffer.
142 * @inline_space: Whether inline command buffer space is used.
144 struct vmw_cmdbuf_header
{
145 struct vmw_cmdbuf_man
*man
;
146 SVGACBHeader
*cb_header
;
147 SVGACBContext cb_context
;
148 struct list_head list
;
149 struct drm_mm_node node
;
158 * struct vmw_cmdbuf_dheader - Device command buffer header with inline
159 * command buffer space.
161 * @cb_header: Device command buffer header.
162 * @cmd: Inline command buffer space.
164 struct vmw_cmdbuf_dheader
{
165 SVGACBHeader cb_header
;
166 u8 cmd
[VMW_CMDBUF_INLINE_SIZE
] __aligned(VMW_CMDBUF_INLINE_ALIGN
);
170 * struct vmw_cmdbuf_alloc_info - Command buffer space allocation metadata
172 * @page_size: Size of requested command buffer space in pages.
173 * @node: Pointer to the range manager node.
174 * @done: True if this allocation has succeeded.
176 struct vmw_cmdbuf_alloc_info
{
178 struct drm_mm_node
*node
;
182 /* Loop over each context in the command buffer manager. */
183 #define for_each_cmdbuf_ctx(_man, _i, _ctx) \
184 for (_i = 0, _ctx = &(_man)->ctx[0]; (_i) < SVGA_CB_CONTEXT_MAX; \
187 static int vmw_cmdbuf_startstop(struct vmw_cmdbuf_man
*man
, bool enable
);
191 * vmw_cmdbuf_cur_lock - Helper to lock the cur_mutex.
193 * @man: The range manager.
194 * @interruptible: Whether to wait interruptible when locking.
196 static int vmw_cmdbuf_cur_lock(struct vmw_cmdbuf_man
*man
, bool interruptible
)
199 if (mutex_lock_interruptible(&man
->cur_mutex
))
202 mutex_lock(&man
->cur_mutex
);
209 * vmw_cmdbuf_cur_unlock - Helper to unlock the cur_mutex.
211 * @man: The range manager.
213 static void vmw_cmdbuf_cur_unlock(struct vmw_cmdbuf_man
*man
)
215 mutex_unlock(&man
->cur_mutex
);
219 * vmw_cmdbuf_header_inline_free - Free a struct vmw_cmdbuf_header that has
220 * been used for the device context with inline command buffers.
221 * Need not be called locked.
223 * @header: Pointer to the header to free.
225 static void vmw_cmdbuf_header_inline_free(struct vmw_cmdbuf_header
*header
)
227 struct vmw_cmdbuf_dheader
*dheader
;
229 if (WARN_ON_ONCE(!header
->inline_space
))
232 dheader
= container_of(header
->cb_header
, struct vmw_cmdbuf_dheader
,
234 dma_pool_free(header
->man
->dheaders
, dheader
, header
->handle
);
239 * __vmw_cmdbuf_header_free - Free a struct vmw_cmdbuf_header and its
240 * associated structures.
242 * header: Pointer to the header to free.
244 * For internal use. Must be called with man::lock held.
246 static void __vmw_cmdbuf_header_free(struct vmw_cmdbuf_header
*header
)
248 struct vmw_cmdbuf_man
*man
= header
->man
;
250 BUG_ON(!spin_is_locked(&man
->lock
));
252 if (header
->inline_space
) {
253 vmw_cmdbuf_header_inline_free(header
);
257 drm_mm_remove_node(&header
->node
);
258 wake_up_all(&man
->alloc_queue
);
259 if (header
->cb_header
)
260 dma_pool_free(man
->headers
, header
->cb_header
,
266 * vmw_cmdbuf_header_free - Free a struct vmw_cmdbuf_header and its
267 * associated structures.
269 * @header: Pointer to the header to free.
271 void vmw_cmdbuf_header_free(struct vmw_cmdbuf_header
*header
)
273 struct vmw_cmdbuf_man
*man
= header
->man
;
275 /* Avoid locking if inline_space */
276 if (header
->inline_space
) {
277 vmw_cmdbuf_header_inline_free(header
);
280 spin_lock_bh(&man
->lock
);
281 __vmw_cmdbuf_header_free(header
);
282 spin_unlock_bh(&man
->lock
);
287 * vmw_cmbuf_header_submit: Submit a command buffer to hardware.
289 * @header: The header of the buffer to submit.
291 static int vmw_cmdbuf_header_submit(struct vmw_cmdbuf_header
*header
)
293 struct vmw_cmdbuf_man
*man
= header
->man
;
296 if (sizeof(header
->handle
) > 4)
297 val
= (header
->handle
>> 32);
300 vmw_write(man
->dev_priv
, SVGA_REG_COMMAND_HIGH
, val
);
302 val
= (header
->handle
& 0xFFFFFFFFULL
);
303 val
|= header
->cb_context
& SVGA_CB_CONTEXT_MASK
;
304 vmw_write(man
->dev_priv
, SVGA_REG_COMMAND_LOW
, val
);
306 return header
->cb_header
->status
;
310 * vmw_cmdbuf_ctx_init: Initialize a command buffer context.
312 * @ctx: The command buffer context to initialize
314 static void vmw_cmdbuf_ctx_init(struct vmw_cmdbuf_context
*ctx
)
316 INIT_LIST_HEAD(&ctx
->hw_submitted
);
317 INIT_LIST_HEAD(&ctx
->submitted
);
318 INIT_LIST_HEAD(&ctx
->preempted
);
319 ctx
->num_hw_submitted
= 0;
323 * vmw_cmdbuf_ctx_submit: Submit command buffers from a command buffer
326 * @man: The command buffer manager.
327 * @ctx: The command buffer context.
329 * Submits command buffers to hardware until there are no more command
330 * buffers to submit or the hardware can't handle more command buffers.
332 static void vmw_cmdbuf_ctx_submit(struct vmw_cmdbuf_man
*man
,
333 struct vmw_cmdbuf_context
*ctx
)
335 while (ctx
->num_hw_submitted
< man
->max_hw_submitted
&&
336 !list_empty(&ctx
->submitted
)) {
337 struct vmw_cmdbuf_header
*entry
;
340 entry
= list_first_entry(&ctx
->submitted
,
341 struct vmw_cmdbuf_header
,
344 status
= vmw_cmdbuf_header_submit(entry
);
346 /* This should never happen */
347 if (WARN_ON_ONCE(status
== SVGA_CB_STATUS_QUEUE_FULL
)) {
348 entry
->cb_header
->status
= SVGA_CB_STATUS_NONE
;
352 list_del(&entry
->list
);
353 list_add_tail(&entry
->list
, &ctx
->hw_submitted
);
354 ctx
->num_hw_submitted
++;
360 * vmw_cmdbuf_ctx_submit: Process a command buffer context.
362 * @man: The command buffer manager.
363 * @ctx: The command buffer context.
365 * Submit command buffers to hardware if possible, and process finished
366 * buffers. Typically freeing them, but on preemption or error take
367 * appropriate action. Wake up waiters if appropriate.
369 static void vmw_cmdbuf_ctx_process(struct vmw_cmdbuf_man
*man
,
370 struct vmw_cmdbuf_context
*ctx
,
373 struct vmw_cmdbuf_header
*entry
, *next
;
375 vmw_cmdbuf_ctx_submit(man
, ctx
);
377 list_for_each_entry_safe(entry
, next
, &ctx
->hw_submitted
, list
) {
378 SVGACBStatus status
= entry
->cb_header
->status
;
380 if (status
== SVGA_CB_STATUS_NONE
)
383 list_del(&entry
->list
);
384 wake_up_all(&man
->idle_queue
);
385 ctx
->num_hw_submitted
--;
387 case SVGA_CB_STATUS_COMPLETED
:
388 __vmw_cmdbuf_header_free(entry
);
390 case SVGA_CB_STATUS_COMMAND_ERROR
:
391 case SVGA_CB_STATUS_CB_HEADER_ERROR
:
392 list_add_tail(&entry
->list
, &man
->error
);
393 schedule_work(&man
->work
);
395 case SVGA_CB_STATUS_PREEMPTED
:
396 list_add(&entry
->list
, &ctx
->preempted
);
399 WARN_ONCE(true, "Undefined command buffer status.\n");
400 __vmw_cmdbuf_header_free(entry
);
405 vmw_cmdbuf_ctx_submit(man
, ctx
);
406 if (!list_empty(&ctx
->submitted
))
411 * vmw_cmdbuf_man_process - Process all command buffer contexts and
412 * switch on and off irqs as appropriate.
414 * @man: The command buffer manager.
416 * Calls vmw_cmdbuf_ctx_process() on all contexts. If any context has
417 * command buffers left that are not submitted to hardware, Make sure
418 * IRQ handling is turned on. Otherwise, make sure it's turned off.
420 static void vmw_cmdbuf_man_process(struct vmw_cmdbuf_man
*man
)
423 struct vmw_cmdbuf_context
*ctx
;
428 for_each_cmdbuf_ctx(man
, i
, ctx
)
429 vmw_cmdbuf_ctx_process(man
, ctx
, ¬empty
);
431 if (man
->irq_on
&& !notempty
) {
432 vmw_generic_waiter_remove(man
->dev_priv
,
433 SVGA_IRQFLAG_COMMAND_BUFFER
,
434 &man
->dev_priv
->cmdbuf_waiters
);
436 } else if (!man
->irq_on
&& notempty
) {
437 vmw_generic_waiter_add(man
->dev_priv
,
438 SVGA_IRQFLAG_COMMAND_BUFFER
,
439 &man
->dev_priv
->cmdbuf_waiters
);
442 /* Rerun in case we just missed an irq. */
448 * vmw_cmdbuf_ctx_add - Schedule a command buffer for submission on a
449 * command buffer context
451 * @man: The command buffer manager.
452 * @header: The header of the buffer to submit.
453 * @cb_context: The command buffer context to use.
455 * This function adds @header to the "submitted" queue of the command
456 * buffer context identified by @cb_context. It then calls the command buffer
457 * manager processing to potentially submit the buffer to hardware.
458 * @man->lock needs to be held when calling this function.
460 static void vmw_cmdbuf_ctx_add(struct vmw_cmdbuf_man
*man
,
461 struct vmw_cmdbuf_header
*header
,
462 SVGACBContext cb_context
)
464 if (!(header
->cb_header
->flags
& SVGA_CB_FLAG_DX_CONTEXT
))
465 header
->cb_header
->dxContext
= 0;
466 header
->cb_context
= cb_context
;
467 list_add_tail(&header
->list
, &man
->ctx
[cb_context
].submitted
);
469 vmw_cmdbuf_man_process(man
);
473 * vmw_cmdbuf_man_tasklet - The main part of the command buffer interrupt
474 * handler implemented as a tasklet.
476 * @data: Tasklet closure. A pointer to the command buffer manager cast to
479 * The bottom half (tasklet) of the interrupt handler simply calls into the
480 * command buffer processor to free finished buffers and submit any
481 * queued buffers to hardware.
483 static void vmw_cmdbuf_man_tasklet(unsigned long data
)
485 struct vmw_cmdbuf_man
*man
= (struct vmw_cmdbuf_man
*) data
;
487 spin_lock(&man
->lock
);
488 vmw_cmdbuf_man_process(man
);
489 spin_unlock(&man
->lock
);
493 * vmw_cmdbuf_work_func - The deferred work function that handles
494 * command buffer errors.
496 * @work: The work func closure argument.
498 * Restarting the command buffer context after an error requires process
499 * context, so it is deferred to this work function.
501 static void vmw_cmdbuf_work_func(struct work_struct
*work
)
503 struct vmw_cmdbuf_man
*man
=
504 container_of(work
, struct vmw_cmdbuf_man
, work
);
505 struct vmw_cmdbuf_header
*entry
, *next
;
507 bool restart
= false;
509 spin_lock_bh(&man
->lock
);
510 list_for_each_entry_safe(entry
, next
, &man
->error
, list
) {
512 DRM_ERROR("Command buffer error.\n");
514 list_del(&entry
->list
);
515 __vmw_cmdbuf_header_free(entry
);
516 wake_up_all(&man
->idle_queue
);
518 spin_unlock_bh(&man
->lock
);
520 if (restart
&& vmw_cmdbuf_startstop(man
, true))
521 DRM_ERROR("Failed restarting command buffer context 0.\n");
523 /* Send a new fence in case one was removed */
524 vmw_fifo_send_fence(man
->dev_priv
, &dummy
);
528 * vmw_cmdbuf_man idle - Check whether the command buffer manager is idle.
530 * @man: The command buffer manager.
531 * @check_preempted: Check also the preempted queue for pending command buffers.
534 static bool vmw_cmdbuf_man_idle(struct vmw_cmdbuf_man
*man
,
535 bool check_preempted
)
537 struct vmw_cmdbuf_context
*ctx
;
541 spin_lock_bh(&man
->lock
);
542 vmw_cmdbuf_man_process(man
);
543 for_each_cmdbuf_ctx(man
, i
, ctx
) {
544 if (!list_empty(&ctx
->submitted
) ||
545 !list_empty(&ctx
->hw_submitted
) ||
546 (check_preempted
&& !list_empty(&ctx
->preempted
)))
550 idle
= list_empty(&man
->error
);
553 spin_unlock_bh(&man
->lock
);
559 * __vmw_cmdbuf_cur_flush - Flush the current command buffer for small kernel
560 * command submissions
562 * @man: The command buffer manager.
564 * Flushes the current command buffer without allocating a new one. A new one
565 * is automatically allocated when needed. Call with @man->cur_mutex held.
567 static void __vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man
*man
)
569 struct vmw_cmdbuf_header
*cur
= man
->cur
;
571 WARN_ON(!mutex_is_locked(&man
->cur_mutex
));
576 spin_lock_bh(&man
->lock
);
577 if (man
->cur_pos
== 0) {
578 __vmw_cmdbuf_header_free(cur
);
582 man
->cur
->cb_header
->length
= man
->cur_pos
;
583 vmw_cmdbuf_ctx_add(man
, man
->cur
, SVGA_CB_CONTEXT_0
);
585 spin_unlock_bh(&man
->lock
);
591 * vmw_cmdbuf_cur_flush - Flush the current command buffer for small kernel
592 * command submissions
594 * @man: The command buffer manager.
595 * @interruptible: Whether to sleep interruptible when sleeping.
597 * Flushes the current command buffer without allocating a new one. A new one
598 * is automatically allocated when needed.
600 int vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man
*man
,
603 int ret
= vmw_cmdbuf_cur_lock(man
, interruptible
);
608 __vmw_cmdbuf_cur_flush(man
);
609 vmw_cmdbuf_cur_unlock(man
);
615 * vmw_cmdbuf_idle - Wait for command buffer manager idle.
617 * @man: The command buffer manager.
618 * @interruptible: Sleep interruptible while waiting.
619 * @timeout: Time out after this many ticks.
621 * Wait until the command buffer manager has processed all command buffers,
622 * or until a timeout occurs. If a timeout occurs, the function will return
625 int vmw_cmdbuf_idle(struct vmw_cmdbuf_man
*man
, bool interruptible
,
626 unsigned long timeout
)
630 ret
= vmw_cmdbuf_cur_flush(man
, interruptible
);
631 vmw_generic_waiter_add(man
->dev_priv
,
632 SVGA_IRQFLAG_COMMAND_BUFFER
,
633 &man
->dev_priv
->cmdbuf_waiters
);
636 ret
= wait_event_interruptible_timeout
637 (man
->idle_queue
, vmw_cmdbuf_man_idle(man
, true),
640 ret
= wait_event_timeout
641 (man
->idle_queue
, vmw_cmdbuf_man_idle(man
, true),
644 vmw_generic_waiter_remove(man
->dev_priv
,
645 SVGA_IRQFLAG_COMMAND_BUFFER
,
646 &man
->dev_priv
->cmdbuf_waiters
);
648 if (!vmw_cmdbuf_man_idle(man
, true))
660 * vmw_cmdbuf_try_alloc - Try to allocate buffer space from the main pool.
662 * @man: The command buffer manager.
663 * @info: Allocation info. Will hold the size on entry and allocated mm node
664 * on successful return.
666 * Try to allocate buffer space from the main pool. Returns true if succeeded.
667 * If a fatal error was hit, the error code is returned in @info->ret.
669 static bool vmw_cmdbuf_try_alloc(struct vmw_cmdbuf_man
*man
,
670 struct vmw_cmdbuf_alloc_info
*info
)
677 memset(info
->node
, 0, sizeof(*info
->node
));
678 spin_lock_bh(&man
->lock
);
679 ret
= drm_mm_insert_node_generic(&man
->mm
, info
->node
, info
->page_size
,
681 DRM_MM_SEARCH_DEFAULT
,
682 DRM_MM_CREATE_DEFAULT
);
684 vmw_cmdbuf_man_process(man
);
685 ret
= drm_mm_insert_node_generic(&man
->mm
, info
->node
,
686 info
->page_size
, 0, 0,
687 DRM_MM_SEARCH_DEFAULT
,
688 DRM_MM_CREATE_DEFAULT
);
691 spin_unlock_bh(&man
->lock
);
698 * vmw_cmdbuf_alloc_space - Allocate buffer space from the main pool.
700 * @man: The command buffer manager.
701 * @node: Pointer to pre-allocated range-manager node.
702 * @size: The size of the allocation.
703 * @interruptible: Whether to sleep interruptible while waiting for space.
705 * This function allocates buffer space from the main pool, and if there is
706 * no space available ATM, it turns on IRQ handling and sleeps waiting for it to
709 static int vmw_cmdbuf_alloc_space(struct vmw_cmdbuf_man
*man
,
710 struct drm_mm_node
*node
,
714 struct vmw_cmdbuf_alloc_info info
;
716 info
.page_size
= PAGE_ALIGN(size
) >> PAGE_SHIFT
;
721 * To prevent starvation of large requests, only one allocating call
722 * at a time waiting for space.
725 if (mutex_lock_interruptible(&man
->space_mutex
))
728 mutex_lock(&man
->space_mutex
);
731 /* Try to allocate space without waiting. */
732 if (vmw_cmdbuf_try_alloc(man
, &info
))
735 vmw_generic_waiter_add(man
->dev_priv
,
736 SVGA_IRQFLAG_COMMAND_BUFFER
,
737 &man
->dev_priv
->cmdbuf_waiters
);
742 ret
= wait_event_interruptible
743 (man
->alloc_queue
, vmw_cmdbuf_try_alloc(man
, &info
));
745 vmw_generic_waiter_remove
746 (man
->dev_priv
, SVGA_IRQFLAG_COMMAND_BUFFER
,
747 &man
->dev_priv
->cmdbuf_waiters
);
748 mutex_unlock(&man
->space_mutex
);
752 wait_event(man
->alloc_queue
, vmw_cmdbuf_try_alloc(man
, &info
));
754 vmw_generic_waiter_remove(man
->dev_priv
,
755 SVGA_IRQFLAG_COMMAND_BUFFER
,
756 &man
->dev_priv
->cmdbuf_waiters
);
759 mutex_unlock(&man
->space_mutex
);
765 * vmw_cmdbuf_space_pool - Set up a command buffer header with command buffer
766 * space from the main pool.
768 * @man: The command buffer manager.
769 * @header: Pointer to the header to set up.
770 * @size: The requested size of the buffer space.
771 * @interruptible: Whether to sleep interruptible while waiting for space.
773 static int vmw_cmdbuf_space_pool(struct vmw_cmdbuf_man
*man
,
774 struct vmw_cmdbuf_header
*header
,
778 SVGACBHeader
*cb_hdr
;
785 ret
= vmw_cmdbuf_alloc_space(man
, &header
->node
, size
, interruptible
);
790 header
->cb_header
= dma_pool_alloc(man
->headers
, GFP_KERNEL
,
792 if (!header
->cb_header
) {
794 goto out_no_cb_header
;
797 header
->size
= header
->node
.size
<< PAGE_SHIFT
;
798 cb_hdr
= header
->cb_header
;
799 offset
= header
->node
.start
<< PAGE_SHIFT
;
800 header
->cmd
= man
->map
+ offset
;
801 memset(cb_hdr
, 0, sizeof(*cb_hdr
));
802 if (man
->using_mob
) {
803 cb_hdr
->flags
= SVGA_CB_FLAG_MOB
;
804 cb_hdr
->ptr
.mob
.mobid
= man
->cmd_space
->mem
.start
;
805 cb_hdr
->ptr
.mob
.mobOffset
= offset
;
807 cb_hdr
->ptr
.pa
= (u64
)man
->handle
+ (u64
)offset
;
813 spin_lock_bh(&man
->lock
);
814 drm_mm_remove_node(&header
->node
);
815 spin_unlock_bh(&man
->lock
);
821 * vmw_cmdbuf_space_inline - Set up a command buffer header with
822 * inline command buffer space.
824 * @man: The command buffer manager.
825 * @header: Pointer to the header to set up.
826 * @size: The requested size of the buffer space.
828 static int vmw_cmdbuf_space_inline(struct vmw_cmdbuf_man
*man
,
829 struct vmw_cmdbuf_header
*header
,
832 struct vmw_cmdbuf_dheader
*dheader
;
833 SVGACBHeader
*cb_hdr
;
835 if (WARN_ON_ONCE(size
> VMW_CMDBUF_INLINE_SIZE
))
838 dheader
= dma_pool_alloc(man
->dheaders
, GFP_KERNEL
,
843 header
->inline_space
= true;
844 header
->size
= VMW_CMDBUF_INLINE_SIZE
;
845 cb_hdr
= &dheader
->cb_header
;
846 header
->cb_header
= cb_hdr
;
847 header
->cmd
= dheader
->cmd
;
848 memset(dheader
, 0, sizeof(*dheader
));
849 cb_hdr
->status
= SVGA_CB_STATUS_NONE
;
850 cb_hdr
->flags
= SVGA_CB_FLAG_NONE
;
851 cb_hdr
->ptr
.pa
= (u64
)header
->handle
+
852 (u64
)offsetof(struct vmw_cmdbuf_dheader
, cmd
);
858 * vmw_cmdbuf_alloc - Allocate a command buffer header complete with
859 * command buffer space.
861 * @man: The command buffer manager.
862 * @size: The requested size of the buffer space.
863 * @interruptible: Whether to sleep interruptible while waiting for space.
864 * @p_header: points to a header pointer to populate on successful return.
866 * Returns a pointer to command buffer space if successful. Otherwise
867 * returns an error pointer. The header pointer returned in @p_header should
868 * be used for upcoming calls to vmw_cmdbuf_reserve() and vmw_cmdbuf_commit().
870 void *vmw_cmdbuf_alloc(struct vmw_cmdbuf_man
*man
,
871 size_t size
, bool interruptible
,
872 struct vmw_cmdbuf_header
**p_header
)
874 struct vmw_cmdbuf_header
*header
;
879 header
= kzalloc(sizeof(*header
), GFP_KERNEL
);
881 return ERR_PTR(-ENOMEM
);
883 if (size
<= VMW_CMDBUF_INLINE_SIZE
)
884 ret
= vmw_cmdbuf_space_inline(man
, header
, size
);
886 ret
= vmw_cmdbuf_space_pool(man
, header
, size
, interruptible
);
894 INIT_LIST_HEAD(&header
->list
);
895 header
->cb_header
->status
= SVGA_CB_STATUS_NONE
;
902 * vmw_cmdbuf_reserve_cur - Reserve space for commands in the current
905 * @man: The command buffer manager.
906 * @size: The requested size of the commands.
907 * @ctx_id: The context id if any. Otherwise set to SVGA3D_REG_INVALID.
908 * @interruptible: Whether to sleep interruptible while waiting for space.
910 * Returns a pointer to command buffer space if successful. Otherwise
911 * returns an error pointer.
913 static void *vmw_cmdbuf_reserve_cur(struct vmw_cmdbuf_man
*man
,
918 struct vmw_cmdbuf_header
*cur
;
921 if (vmw_cmdbuf_cur_lock(man
, interruptible
))
922 return ERR_PTR(-ERESTARTSYS
);
925 if (cur
&& (size
+ man
->cur_pos
> cur
->size
||
926 ((cur
->cb_header
->flags
& SVGA_CB_FLAG_DX_CONTEXT
) &&
927 ctx_id
!= cur
->cb_header
->dxContext
)))
928 __vmw_cmdbuf_cur_flush(man
);
931 ret
= vmw_cmdbuf_alloc(man
,
932 max_t(size_t, size
, man
->default_size
),
933 interruptible
, &man
->cur
);
935 vmw_cmdbuf_cur_unlock(man
);
942 if (ctx_id
!= SVGA3D_INVALID_ID
) {
943 cur
->cb_header
->flags
|= SVGA_CB_FLAG_DX_CONTEXT
;
944 cur
->cb_header
->dxContext
= ctx_id
;
947 cur
->reserved
= size
;
949 return (void *) (man
->cur
->cmd
+ man
->cur_pos
);
953 * vmw_cmdbuf_commit_cur - Commit commands in the current command buffer.
955 * @man: The command buffer manager.
956 * @size: The size of the commands actually written.
957 * @flush: Whether to flush the command buffer immediately.
959 static void vmw_cmdbuf_commit_cur(struct vmw_cmdbuf_man
*man
,
960 size_t size
, bool flush
)
962 struct vmw_cmdbuf_header
*cur
= man
->cur
;
964 WARN_ON(!mutex_is_locked(&man
->cur_mutex
));
966 WARN_ON(size
> cur
->reserved
);
967 man
->cur_pos
+= size
;
969 cur
->cb_header
->flags
&= ~SVGA_CB_FLAG_DX_CONTEXT
;
971 __vmw_cmdbuf_cur_flush(man
);
972 vmw_cmdbuf_cur_unlock(man
);
976 * vmw_cmdbuf_reserve - Reserve space for commands in a command buffer.
978 * @man: The command buffer manager.
979 * @size: The requested size of the commands.
980 * @ctx_id: The context id if any. Otherwise set to SVGA3D_REG_INVALID.
981 * @interruptible: Whether to sleep interruptible while waiting for space.
982 * @header: Header of the command buffer. NULL if the current command buffer
985 * Returns a pointer to command buffer space if successful. Otherwise
986 * returns an error pointer.
988 void *vmw_cmdbuf_reserve(struct vmw_cmdbuf_man
*man
, size_t size
,
989 int ctx_id
, bool interruptible
,
990 struct vmw_cmdbuf_header
*header
)
993 return vmw_cmdbuf_reserve_cur(man
, size
, ctx_id
, interruptible
);
995 if (size
> header
->size
)
996 return ERR_PTR(-EINVAL
);
998 if (ctx_id
!= SVGA3D_INVALID_ID
) {
999 header
->cb_header
->flags
|= SVGA_CB_FLAG_DX_CONTEXT
;
1000 header
->cb_header
->dxContext
= ctx_id
;
1003 header
->reserved
= size
;
1008 * vmw_cmdbuf_commit - Commit commands in a command buffer.
1010 * @man: The command buffer manager.
1011 * @size: The size of the commands actually written.
1012 * @header: Header of the command buffer. NULL if the current command buffer
1014 * @flush: Whether to flush the command buffer immediately.
1016 void vmw_cmdbuf_commit(struct vmw_cmdbuf_man
*man
, size_t size
,
1017 struct vmw_cmdbuf_header
*header
, bool flush
)
1020 vmw_cmdbuf_commit_cur(man
, size
, flush
);
1024 (void) vmw_cmdbuf_cur_lock(man
, false);
1025 __vmw_cmdbuf_cur_flush(man
);
1026 WARN_ON(size
> header
->reserved
);
1028 man
->cur_pos
= size
;
1030 header
->cb_header
->flags
&= ~SVGA_CB_FLAG_DX_CONTEXT
;
1032 __vmw_cmdbuf_cur_flush(man
);
1033 vmw_cmdbuf_cur_unlock(man
);
1037 * vmw_cmdbuf_tasklet_schedule - Schedule the interrupt handler bottom half.
1039 * @man: The command buffer manager.
1041 void vmw_cmdbuf_tasklet_schedule(struct vmw_cmdbuf_man
*man
)
1046 tasklet_schedule(&man
->tasklet
);
1050 * vmw_cmdbuf_send_device_command - Send a command through the device context.
1052 * @man: The command buffer manager.
1053 * @command: Pointer to the command to send.
1054 * @size: Size of the command.
1056 * Synchronously sends a device context command.
1058 static int vmw_cmdbuf_send_device_command(struct vmw_cmdbuf_man
*man
,
1059 const void *command
,
1062 struct vmw_cmdbuf_header
*header
;
1064 void *cmd
= vmw_cmdbuf_alloc(man
, size
, false, &header
);
1067 return PTR_ERR(cmd
);
1069 memcpy(cmd
, command
, size
);
1070 header
->cb_header
->length
= size
;
1071 header
->cb_context
= SVGA_CB_CONTEXT_DEVICE
;
1072 spin_lock_bh(&man
->lock
);
1073 status
= vmw_cmdbuf_header_submit(header
);
1074 spin_unlock_bh(&man
->lock
);
1075 vmw_cmdbuf_header_free(header
);
1077 if (status
!= SVGA_CB_STATUS_COMPLETED
) {
1078 DRM_ERROR("Device context command failed with status %d\n",
1087 * vmw_cmdbuf_startstop - Send a start / stop command through the device
1090 * @man: The command buffer manager.
1091 * @enable: Whether to enable or disable the context.
1093 * Synchronously sends a device start / stop context command.
1095 static int vmw_cmdbuf_startstop(struct vmw_cmdbuf_man
*man
,
1100 SVGADCCmdStartStop body
;
1103 cmd
.id
= SVGA_DC_CMD_START_STOP_CONTEXT
;
1104 cmd
.body
.enable
= (enable
) ? 1 : 0;
1105 cmd
.body
.context
= SVGA_CB_CONTEXT_0
;
1107 return vmw_cmdbuf_send_device_command(man
, &cmd
, sizeof(cmd
));
1111 * vmw_cmdbuf_set_pool_size - Set command buffer manager sizes
1113 * @man: The command buffer manager.
1114 * @size: The size of the main space pool.
1115 * @default_size: The default size of the command buffer for small kernel
1118 * Set the size and allocate the main command buffer space pool,
1119 * as well as the default size of the command buffer for
1120 * small kernel submissions. If successful, this enables large command
1121 * submissions. Note that this function requires that rudimentary command
1122 * submission is already available and that the MOB memory manager is alive.
1123 * Returns 0 on success. Negative error code on failure.
1125 int vmw_cmdbuf_set_pool_size(struct vmw_cmdbuf_man
*man
,
1126 size_t size
, size_t default_size
)
1128 struct vmw_private
*dev_priv
= man
->dev_priv
;
1135 /* First, try to allocate a huge chunk of DMA memory */
1136 size
= PAGE_ALIGN(size
);
1137 man
->map
= dma_alloc_coherent(&dev_priv
->dev
->pdev
->dev
, size
,
1138 &man
->handle
, GFP_KERNEL
);
1140 man
->using_mob
= false;
1143 * DMA memory failed. If we can have command buffers in a
1144 * MOB, try to use that instead. Note that this will
1145 * actually call into the already enabled manager, when
1148 if (!(dev_priv
->capabilities
& SVGA_CAP_DX
))
1151 ret
= ttm_bo_create(&dev_priv
->bdev
, size
, ttm_bo_type_device
,
1152 &vmw_mob_ne_placement
, 0, false, NULL
,
1157 man
->using_mob
= true;
1158 ret
= ttm_bo_kmap(man
->cmd_space
, 0, size
>> PAGE_SHIFT
,
1163 man
->map
= ttm_kmap_obj_virtual(&man
->map_obj
, &dummy
);
1167 drm_mm_init(&man
->mm
, 0, size
>> PAGE_SHIFT
);
1169 man
->has_pool
= true;
1172 * For now, set the default size to VMW_CMDBUF_INLINE_SIZE to
1173 * prevent deadlocks from happening when vmw_cmdbuf_space_pool()
1174 * needs to wait for space and we block on further command
1175 * submissions to be able to free up space.
1177 man
->default_size
= VMW_CMDBUF_INLINE_SIZE
;
1178 DRM_INFO("Using command buffers with %s pool.\n",
1179 (man
->using_mob
) ? "MOB" : "DMA");
1185 ttm_bo_unref(&man
->cmd_space
);
1191 * vmw_cmdbuf_man_create: Create a command buffer manager and enable it for
1192 * inline command buffer submissions only.
1194 * @dev_priv: Pointer to device private structure.
1196 * Returns a pointer to a cummand buffer manager to success or error pointer
1197 * on failure. The command buffer manager will be enabled for submissions of
1198 * size VMW_CMDBUF_INLINE_SIZE only.
1200 struct vmw_cmdbuf_man
*vmw_cmdbuf_man_create(struct vmw_private
*dev_priv
)
1202 struct vmw_cmdbuf_man
*man
;
1203 struct vmw_cmdbuf_context
*ctx
;
1207 if (!(dev_priv
->capabilities
& SVGA_CAP_COMMAND_BUFFERS
))
1208 return ERR_PTR(-ENOSYS
);
1210 man
= kzalloc(sizeof(*man
), GFP_KERNEL
);
1212 return ERR_PTR(-ENOMEM
);
1214 man
->headers
= dma_pool_create("vmwgfx cmdbuf",
1215 &dev_priv
->dev
->pdev
->dev
,
1216 sizeof(SVGACBHeader
),
1218 if (!man
->headers
) {
1223 man
->dheaders
= dma_pool_create("vmwgfx inline cmdbuf",
1224 &dev_priv
->dev
->pdev
->dev
,
1225 sizeof(struct vmw_cmdbuf_dheader
),
1227 if (!man
->dheaders
) {
1232 for_each_cmdbuf_ctx(man
, i
, ctx
)
1233 vmw_cmdbuf_ctx_init(ctx
);
1235 INIT_LIST_HEAD(&man
->error
);
1236 spin_lock_init(&man
->lock
);
1237 mutex_init(&man
->cur_mutex
);
1238 mutex_init(&man
->space_mutex
);
1239 tasklet_init(&man
->tasklet
, vmw_cmdbuf_man_tasklet
,
1240 (unsigned long) man
);
1241 man
->default_size
= VMW_CMDBUF_INLINE_SIZE
;
1242 init_waitqueue_head(&man
->alloc_queue
);
1243 init_waitqueue_head(&man
->idle_queue
);
1244 man
->dev_priv
= dev_priv
;
1245 man
->max_hw_submitted
= SVGA_CB_MAX_QUEUED_PER_CONTEXT
- 1;
1246 INIT_WORK(&man
->work
, &vmw_cmdbuf_work_func
);
1247 vmw_generic_waiter_add(dev_priv
, SVGA_IRQFLAG_ERROR
,
1248 &dev_priv
->error_waiters
);
1249 ret
= vmw_cmdbuf_startstop(man
, true);
1251 DRM_ERROR("Failed starting command buffer context 0.\n");
1252 vmw_cmdbuf_man_destroy(man
);
1253 return ERR_PTR(ret
);
1259 dma_pool_destroy(man
->headers
);
1263 return ERR_PTR(ret
);
1267 * vmw_cmdbuf_remove_pool - Take down the main buffer space pool.
1269 * @man: Pointer to a command buffer manager.
1271 * This function removes the main buffer space pool, and should be called
1272 * before MOB memory management is removed. When this function has been called,
1273 * only small command buffer submissions of size VMW_CMDBUF_INLINE_SIZE or
1274 * less are allowed, and the default size of the command buffer for small kernel
1275 * submissions is also set to this size.
1277 void vmw_cmdbuf_remove_pool(struct vmw_cmdbuf_man
*man
)
1282 man
->has_pool
= false;
1283 man
->default_size
= VMW_CMDBUF_INLINE_SIZE
;
1284 (void) vmw_cmdbuf_idle(man
, false, 10*HZ
);
1285 if (man
->using_mob
) {
1286 (void) ttm_bo_kunmap(&man
->map_obj
);
1287 ttm_bo_unref(&man
->cmd_space
);
1289 dma_free_coherent(&man
->dev_priv
->dev
->pdev
->dev
,
1290 man
->size
, man
->map
, man
->handle
);
1295 * vmw_cmdbuf_man_destroy - Take down a command buffer manager.
1297 * @man: Pointer to a command buffer manager.
1299 * This function idles and then destroys a command buffer manager.
1301 void vmw_cmdbuf_man_destroy(struct vmw_cmdbuf_man
*man
)
1303 WARN_ON_ONCE(man
->has_pool
);
1304 (void) vmw_cmdbuf_idle(man
, false, 10*HZ
);
1305 if (vmw_cmdbuf_startstop(man
, false))
1306 DRM_ERROR("Failed stopping command buffer context 0.\n");
1308 vmw_generic_waiter_remove(man
->dev_priv
, SVGA_IRQFLAG_ERROR
,
1309 &man
->dev_priv
->error_waiters
);
1310 tasklet_kill(&man
->tasklet
);
1311 (void) cancel_work_sync(&man
->work
);
1312 dma_pool_destroy(man
->dheaders
);
1313 dma_pool_destroy(man
->headers
);
1314 mutex_destroy(&man
->cur_mutex
);
1315 mutex_destroy(&man
->space_mutex
);