blob: 2e5718509c2308addc27614e9d9fa283b9bdaeb0 [file] [log] [blame]
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07001/**************************************************************************
2 *
3 * Copyright © 2015 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
Masahiro Yamada008be682017-04-24 13:50:35 +090028#include <drm/ttm/ttm_bo_api.h>
29
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -070030#include "vmwgfx_drv.h"
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -070031
32/*
33 * Size of inline command buffers. Try to make sure that a page size is a
34 * multiple of the DMA pool allocation size.
35 */
36#define VMW_CMDBUF_INLINE_ALIGN 64
Thomas Hellstrom9b590782015-06-26 04:46:52 -070037#define VMW_CMDBUF_INLINE_SIZE \
38 (1024 - ALIGN(sizeof(SVGACBHeader), VMW_CMDBUF_INLINE_ALIGN))
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -070039
40/**
41 * struct vmw_cmdbuf_context - Command buffer context queues
42 *
43 * @submitted: List of command buffers that have been submitted to the
44 * manager but not yet submitted to hardware.
45 * @hw_submitted: List of command buffers submitted to hardware.
46 * @preempted: List of preempted command buffers.
47 * @num_hw_submitted: Number of buffers currently being processed by hardware
48 */
49struct vmw_cmdbuf_context {
50 struct list_head submitted;
51 struct list_head hw_submitted;
52 struct list_head preempted;
53 unsigned num_hw_submitted;
54};
55
56/**
57 * struct vmw_cmdbuf_man: - Command buffer manager
58 *
59 * @cur_mutex: Mutex protecting the command buffer used for incremental small
60 * kernel command submissions, @cur.
61 * @space_mutex: Mutex to protect against starvation when we allocate
62 * main pool buffer space.
63 * @work: A struct work_struct implementeing command buffer error handling.
64 * Immutable.
65 * @dev_priv: Pointer to the device private struct. Immutable.
66 * @ctx: Array of command buffer context queues. The queues and the context
67 * data is protected by @lock.
68 * @error: List of command buffers that have caused device errors.
69 * Protected by @lock.
70 * @mm: Range manager for the command buffer space. Manager allocations and
71 * frees are protected by @lock.
72 * @cmd_space: Buffer object for the command buffer space, unless we were
73 * able to make a contigous coherent DMA memory allocation, @handle. Immutable.
74 * @map_obj: Mapping state for @cmd_space. Immutable.
75 * @map: Pointer to command buffer space. May be a mapped buffer object or
76 * a contigous coherent DMA memory allocation. Immutable.
77 * @cur: Command buffer for small kernel command submissions. Protected by
78 * the @cur_mutex.
79 * @cur_pos: Space already used in @cur. Protected by @cur_mutex.
80 * @default_size: Default size for the @cur command buffer. Immutable.
81 * @max_hw_submitted: Max number of in-flight command buffers the device can
82 * handle. Immutable.
83 * @lock: Spinlock protecting command submission queues.
84 * @header: Pool of DMA memory for device command buffer headers.
85 * Internal protection.
86 * @dheaders: Pool of DMA memory for device command buffer headers with trailing
87 * space for inline data. Internal protection.
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -070088 * @alloc_queue: Wait queue for processes waiting to allocate command buffer
89 * space.
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.
92 * Protected by @lock.
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
98 * false. Immutable.
99 * @size: The size of the command buffer space. Immutable.
100 */
101struct 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;
108 struct drm_mm mm;
109 struct ttm_buffer_object *cmd_space;
110 struct ttm_bo_kmap_obj map_obj;
111 u8 *map;
112 struct vmw_cmdbuf_header *cur;
113 size_t cur_pos;
114 size_t default_size;
115 unsigned max_hw_submitted;
116 spinlock_t lock;
117 struct dma_pool *headers;
118 struct dma_pool *dheaders;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700119 wait_queue_head_t alloc_queue;
120 wait_queue_head_t idle_queue;
121 bool irq_on;
122 bool using_mob;
123 bool has_pool;
124 dma_addr_t handle;
125 size_t size;
126};
127
128/**
129 * struct vmw_cmdbuf_header - Command buffer metadata
130 *
131 * @man: The command buffer manager.
132 * @cb_header: Device command buffer header, allocated from a DMA pool.
133 * @cb_context: The device command buffer context.
134 * @list: List head for attaching to the manager lists.
135 * @node: The range manager node.
136 * @handle. The DMA address of @cb_header. Handed to the device on command
137 * buffer submission.
138 * @cmd: Pointer to the command buffer space of this buffer.
139 * @size: Size of the command buffer space of this buffer.
140 * @reserved: Reserved space of this buffer.
141 * @inline_space: Whether inline command buffer space is used.
142 */
143struct vmw_cmdbuf_header {
144 struct vmw_cmdbuf_man *man;
145 SVGACBHeader *cb_header;
146 SVGACBContext cb_context;
147 struct list_head list;
Thomas Hellstrom9b590782015-06-26 04:46:52 -0700148 struct drm_mm_node node;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700149 dma_addr_t handle;
150 u8 *cmd;
151 size_t size;
152 size_t reserved;
153 bool inline_space;
154};
155
156/**
157 * struct vmw_cmdbuf_dheader - Device command buffer header with inline
158 * command buffer space.
159 *
160 * @cb_header: Device command buffer header.
161 * @cmd: Inline command buffer space.
162 */
163struct vmw_cmdbuf_dheader {
164 SVGACBHeader cb_header;
165 u8 cmd[VMW_CMDBUF_INLINE_SIZE] __aligned(VMW_CMDBUF_INLINE_ALIGN);
166};
167
168/**
169 * struct vmw_cmdbuf_alloc_info - Command buffer space allocation metadata
170 *
171 * @page_size: Size of requested command buffer space in pages.
Thomas Hellstrom9b590782015-06-26 04:46:52 -0700172 * @node: Pointer to the range manager node.
173 * @done: True if this allocation has succeeded.
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700174 */
175struct vmw_cmdbuf_alloc_info {
176 size_t page_size;
177 struct drm_mm_node *node;
Thomas Hellstrom9b590782015-06-26 04:46:52 -0700178 bool done;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700179};
180
181/* Loop over each context in the command buffer manager. */
182#define for_each_cmdbuf_ctx(_man, _i, _ctx) \
183 for (_i = 0, _ctx = &(_man)->ctx[0]; (_i) < SVGA_CB_CONTEXT_MAX; \
184 ++(_i), ++(_ctx))
185
186static int vmw_cmdbuf_startstop(struct vmw_cmdbuf_man *man, bool enable);
187
188
189/**
190 * vmw_cmdbuf_cur_lock - Helper to lock the cur_mutex.
191 *
192 * @man: The range manager.
193 * @interruptible: Whether to wait interruptible when locking.
194 */
195static int vmw_cmdbuf_cur_lock(struct vmw_cmdbuf_man *man, bool interruptible)
196{
197 if (interruptible) {
198 if (mutex_lock_interruptible(&man->cur_mutex))
199 return -ERESTARTSYS;
200 } else {
201 mutex_lock(&man->cur_mutex);
202 }
203
204 return 0;
205}
206
207/**
208 * vmw_cmdbuf_cur_unlock - Helper to unlock the cur_mutex.
209 *
210 * @man: The range manager.
211 */
212static void vmw_cmdbuf_cur_unlock(struct vmw_cmdbuf_man *man)
213{
214 mutex_unlock(&man->cur_mutex);
215}
216
217/**
218 * vmw_cmdbuf_header_inline_free - Free a struct vmw_cmdbuf_header that has
219 * been used for the device context with inline command buffers.
220 * Need not be called locked.
221 *
222 * @header: Pointer to the header to free.
223 */
224static void vmw_cmdbuf_header_inline_free(struct vmw_cmdbuf_header *header)
225{
226 struct vmw_cmdbuf_dheader *dheader;
227
228 if (WARN_ON_ONCE(!header->inline_space))
229 return;
230
231 dheader = container_of(header->cb_header, struct vmw_cmdbuf_dheader,
232 cb_header);
233 dma_pool_free(header->man->dheaders, dheader, header->handle);
234 kfree(header);
235}
236
237/**
238 * __vmw_cmdbuf_header_free - Free a struct vmw_cmdbuf_header and its
239 * associated structures.
240 *
241 * header: Pointer to the header to free.
242 *
243 * For internal use. Must be called with man::lock held.
244 */
245static void __vmw_cmdbuf_header_free(struct vmw_cmdbuf_header *header)
246{
247 struct vmw_cmdbuf_man *man = header->man;
248
Thomas Hellstromfb89ac52016-01-08 20:29:39 +0100249 lockdep_assert_held_once(&man->lock);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700250
251 if (header->inline_space) {
252 vmw_cmdbuf_header_inline_free(header);
253 return;
254 }
255
Thomas Hellstrom9b590782015-06-26 04:46:52 -0700256 drm_mm_remove_node(&header->node);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700257 wake_up_all(&man->alloc_queue);
258 if (header->cb_header)
259 dma_pool_free(man->headers, header->cb_header,
260 header->handle);
261 kfree(header);
262}
263
264/**
265 * vmw_cmdbuf_header_free - Free a struct vmw_cmdbuf_header and its
266 * associated structures.
267 *
268 * @header: Pointer to the header to free.
269 */
270void vmw_cmdbuf_header_free(struct vmw_cmdbuf_header *header)
271{
272 struct vmw_cmdbuf_man *man = header->man;
273
274 /* Avoid locking if inline_space */
275 if (header->inline_space) {
276 vmw_cmdbuf_header_inline_free(header);
277 return;
278 }
Thomas Hellstromef369902017-08-24 08:06:28 +0200279 spin_lock(&man->lock);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700280 __vmw_cmdbuf_header_free(header);
Thomas Hellstromef369902017-08-24 08:06:28 +0200281 spin_unlock(&man->lock);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700282}
283
284
285/**
286 * vmw_cmbuf_header_submit: Submit a command buffer to hardware.
287 *
288 * @header: The header of the buffer to submit.
289 */
290static int vmw_cmdbuf_header_submit(struct vmw_cmdbuf_header *header)
291{
292 struct vmw_cmdbuf_man *man = header->man;
293 u32 val;
294
Paul Bolle0e7c8752016-03-03 11:26:10 +0100295 val = upper_32_bits(header->handle);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700296 vmw_write(man->dev_priv, SVGA_REG_COMMAND_HIGH, val);
Thomas Hellstrom2e3cc8c2015-08-04 15:34:14 +0200297
Paul Bolle0e7c8752016-03-03 11:26:10 +0100298 val = lower_32_bits(header->handle);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700299 val |= header->cb_context & SVGA_CB_CONTEXT_MASK;
300 vmw_write(man->dev_priv, SVGA_REG_COMMAND_LOW, val);
301
302 return header->cb_header->status;
303}
304
305/**
306 * vmw_cmdbuf_ctx_init: Initialize a command buffer context.
307 *
308 * @ctx: The command buffer context to initialize
309 */
310static void vmw_cmdbuf_ctx_init(struct vmw_cmdbuf_context *ctx)
311{
312 INIT_LIST_HEAD(&ctx->hw_submitted);
313 INIT_LIST_HEAD(&ctx->submitted);
314 INIT_LIST_HEAD(&ctx->preempted);
315 ctx->num_hw_submitted = 0;
316}
317
318/**
319 * vmw_cmdbuf_ctx_submit: Submit command buffers from a command buffer
320 * context.
321 *
322 * @man: The command buffer manager.
323 * @ctx: The command buffer context.
324 *
325 * Submits command buffers to hardware until there are no more command
326 * buffers to submit or the hardware can't handle more command buffers.
327 */
328static void vmw_cmdbuf_ctx_submit(struct vmw_cmdbuf_man *man,
329 struct vmw_cmdbuf_context *ctx)
330{
331 while (ctx->num_hw_submitted < man->max_hw_submitted &&
332 !list_empty(&ctx->submitted)) {
333 struct vmw_cmdbuf_header *entry;
334 SVGACBStatus status;
335
336 entry = list_first_entry(&ctx->submitted,
337 struct vmw_cmdbuf_header,
338 list);
339
340 status = vmw_cmdbuf_header_submit(entry);
341
342 /* This should never happen */
343 if (WARN_ON_ONCE(status == SVGA_CB_STATUS_QUEUE_FULL)) {
344 entry->cb_header->status = SVGA_CB_STATUS_NONE;
345 break;
346 }
347
348 list_del(&entry->list);
349 list_add_tail(&entry->list, &ctx->hw_submitted);
350 ctx->num_hw_submitted++;
351 }
352
353}
354
355/**
356 * vmw_cmdbuf_ctx_submit: Process a command buffer context.
357 *
358 * @man: The command buffer manager.
359 * @ctx: The command buffer context.
360 *
361 * Submit command buffers to hardware if possible, and process finished
362 * buffers. Typically freeing them, but on preemption or error take
363 * appropriate action. Wake up waiters if appropriate.
364 */
365static void vmw_cmdbuf_ctx_process(struct vmw_cmdbuf_man *man,
366 struct vmw_cmdbuf_context *ctx,
367 int *notempty)
368{
369 struct vmw_cmdbuf_header *entry, *next;
370
371 vmw_cmdbuf_ctx_submit(man, ctx);
372
373 list_for_each_entry_safe(entry, next, &ctx->hw_submitted, list) {
374 SVGACBStatus status = entry->cb_header->status;
375
376 if (status == SVGA_CB_STATUS_NONE)
377 break;
378
379 list_del(&entry->list);
380 wake_up_all(&man->idle_queue);
381 ctx->num_hw_submitted--;
382 switch (status) {
383 case SVGA_CB_STATUS_COMPLETED:
384 __vmw_cmdbuf_header_free(entry);
385 break;
386 case SVGA_CB_STATUS_COMMAND_ERROR:
387 case SVGA_CB_STATUS_CB_HEADER_ERROR:
388 list_add_tail(&entry->list, &man->error);
389 schedule_work(&man->work);
390 break;
391 case SVGA_CB_STATUS_PREEMPTED:
392 list_add(&entry->list, &ctx->preempted);
393 break;
394 default:
395 WARN_ONCE(true, "Undefined command buffer status.\n");
396 __vmw_cmdbuf_header_free(entry);
397 break;
398 }
399 }
400
401 vmw_cmdbuf_ctx_submit(man, ctx);
402 if (!list_empty(&ctx->submitted))
403 (*notempty)++;
404}
405
406/**
407 * vmw_cmdbuf_man_process - Process all command buffer contexts and
408 * switch on and off irqs as appropriate.
409 *
410 * @man: The command buffer manager.
411 *
412 * Calls vmw_cmdbuf_ctx_process() on all contexts. If any context has
413 * command buffers left that are not submitted to hardware, Make sure
Thomas Hellstrom09dc1382015-10-21 21:31:49 +0200414 * IRQ handling is turned on. Otherwise, make sure it's turned off.
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700415 */
Thomas Hellstrom09dc1382015-10-21 21:31:49 +0200416static void vmw_cmdbuf_man_process(struct vmw_cmdbuf_man *man)
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700417{
Thomas Hellstrom09dc1382015-10-21 21:31:49 +0200418 int notempty;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700419 struct vmw_cmdbuf_context *ctx;
420 int i;
421
Thomas Hellstrom09dc1382015-10-21 21:31:49 +0200422retry:
423 notempty = 0;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700424 for_each_cmdbuf_ctx(man, i, ctx)
425 vmw_cmdbuf_ctx_process(man, ctx, &notempty);
426
427 if (man->irq_on && !notempty) {
428 vmw_generic_waiter_remove(man->dev_priv,
429 SVGA_IRQFLAG_COMMAND_BUFFER,
430 &man->dev_priv->cmdbuf_waiters);
431 man->irq_on = false;
432 } else if (!man->irq_on && notempty) {
433 vmw_generic_waiter_add(man->dev_priv,
434 SVGA_IRQFLAG_COMMAND_BUFFER,
435 &man->dev_priv->cmdbuf_waiters);
436 man->irq_on = true;
437
438 /* Rerun in case we just missed an irq. */
Thomas Hellstrom09dc1382015-10-21 21:31:49 +0200439 goto retry;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700440 }
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700441}
442
443/**
444 * vmw_cmdbuf_ctx_add - Schedule a command buffer for submission on a
445 * command buffer context
446 *
447 * @man: The command buffer manager.
448 * @header: The header of the buffer to submit.
449 * @cb_context: The command buffer context to use.
450 *
451 * This function adds @header to the "submitted" queue of the command
452 * buffer context identified by @cb_context. It then calls the command buffer
453 * manager processing to potentially submit the buffer to hardware.
454 * @man->lock needs to be held when calling this function.
455 */
456static void vmw_cmdbuf_ctx_add(struct vmw_cmdbuf_man *man,
457 struct vmw_cmdbuf_header *header,
458 SVGACBContext cb_context)
459{
460 if (!(header->cb_header->flags & SVGA_CB_FLAG_DX_CONTEXT))
461 header->cb_header->dxContext = 0;
462 header->cb_context = cb_context;
463 list_add_tail(&header->list, &man->ctx[cb_context].submitted);
464
Thomas Hellstrom09dc1382015-10-21 21:31:49 +0200465 vmw_cmdbuf_man_process(man);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700466}
467
468/**
Thomas Hellstromef369902017-08-24 08:06:28 +0200469 * vmw_cmdbuf_irqthread - The main part of the command buffer interrupt
470 * handler implemented as a threaded irq task.
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700471 *
Thomas Hellstromef369902017-08-24 08:06:28 +0200472 * @man: Pointer to the command buffer manager.
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700473 *
Thomas Hellstromef369902017-08-24 08:06:28 +0200474 * The bottom half of the interrupt handler simply calls into the
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700475 * command buffer processor to free finished buffers and submit any
476 * queued buffers to hardware.
477 */
Thomas Hellstromef369902017-08-24 08:06:28 +0200478void vmw_cmdbuf_irqthread(struct vmw_cmdbuf_man *man)
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700479{
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700480 spin_lock(&man->lock);
Thomas Hellstrom09dc1382015-10-21 21:31:49 +0200481 vmw_cmdbuf_man_process(man);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700482 spin_unlock(&man->lock);
483}
484
485/**
486 * vmw_cmdbuf_work_func - The deferred work function that handles
487 * command buffer errors.
488 *
489 * @work: The work func closure argument.
490 *
491 * Restarting the command buffer context after an error requires process
492 * context, so it is deferred to this work function.
493 */
494static void vmw_cmdbuf_work_func(struct work_struct *work)
495{
496 struct vmw_cmdbuf_man *man =
497 container_of(work, struct vmw_cmdbuf_man, work);
498 struct vmw_cmdbuf_header *entry, *next;
Thomas Hellstrom09dc1382015-10-21 21:31:49 +0200499 uint32_t dummy;
Thomas Hellstrom6a5278e2015-08-04 15:37:16 +0200500 bool restart = false;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700501
Thomas Hellstromef369902017-08-24 08:06:28 +0200502 spin_lock(&man->lock);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700503 list_for_each_entry_safe(entry, next, &man->error, list) {
504 restart = true;
505 DRM_ERROR("Command buffer error.\n");
506
507 list_del(&entry->list);
508 __vmw_cmdbuf_header_free(entry);
509 wake_up_all(&man->idle_queue);
510 }
Thomas Hellstromef369902017-08-24 08:06:28 +0200511 spin_unlock(&man->lock);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700512
513 if (restart && vmw_cmdbuf_startstop(man, true))
514 DRM_ERROR("Failed restarting command buffer context 0.\n");
515
Thomas Hellstrom09dc1382015-10-21 21:31:49 +0200516 /* Send a new fence in case one was removed */
517 vmw_fifo_send_fence(man->dev_priv, &dummy);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700518}
519
520/**
521 * vmw_cmdbuf_man idle - Check whether the command buffer manager is idle.
522 *
523 * @man: The command buffer manager.
524 * @check_preempted: Check also the preempted queue for pending command buffers.
525 *
526 */
527static bool vmw_cmdbuf_man_idle(struct vmw_cmdbuf_man *man,
528 bool check_preempted)
529{
530 struct vmw_cmdbuf_context *ctx;
531 bool idle = false;
532 int i;
533
Thomas Hellstromef369902017-08-24 08:06:28 +0200534 spin_lock(&man->lock);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700535 vmw_cmdbuf_man_process(man);
536 for_each_cmdbuf_ctx(man, i, ctx) {
537 if (!list_empty(&ctx->submitted) ||
538 !list_empty(&ctx->hw_submitted) ||
539 (check_preempted && !list_empty(&ctx->preempted)))
540 goto out_unlock;
541 }
542
543 idle = list_empty(&man->error);
544
545out_unlock:
Thomas Hellstromef369902017-08-24 08:06:28 +0200546 spin_unlock(&man->lock);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700547
548 return idle;
549}
550
551/**
552 * __vmw_cmdbuf_cur_flush - Flush the current command buffer for small kernel
553 * command submissions
554 *
555 * @man: The command buffer manager.
556 *
557 * Flushes the current command buffer without allocating a new one. A new one
558 * is automatically allocated when needed. Call with @man->cur_mutex held.
559 */
560static void __vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man *man)
561{
562 struct vmw_cmdbuf_header *cur = man->cur;
563
564 WARN_ON(!mutex_is_locked(&man->cur_mutex));
565
566 if (!cur)
567 return;
568
Thomas Hellstromef369902017-08-24 08:06:28 +0200569 spin_lock(&man->lock);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700570 if (man->cur_pos == 0) {
571 __vmw_cmdbuf_header_free(cur);
572 goto out_unlock;
573 }
574
575 man->cur->cb_header->length = man->cur_pos;
576 vmw_cmdbuf_ctx_add(man, man->cur, SVGA_CB_CONTEXT_0);
577out_unlock:
Thomas Hellstromef369902017-08-24 08:06:28 +0200578 spin_unlock(&man->lock);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700579 man->cur = NULL;
580 man->cur_pos = 0;
581}
582
583/**
584 * vmw_cmdbuf_cur_flush - Flush the current command buffer for small kernel
585 * command submissions
586 *
587 * @man: The command buffer manager.
588 * @interruptible: Whether to sleep interruptible when sleeping.
589 *
590 * Flushes the current command buffer without allocating a new one. A new one
591 * is automatically allocated when needed.
592 */
593int vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man *man,
594 bool interruptible)
595{
596 int ret = vmw_cmdbuf_cur_lock(man, interruptible);
597
598 if (ret)
599 return ret;
600
601 __vmw_cmdbuf_cur_flush(man);
602 vmw_cmdbuf_cur_unlock(man);
603
604 return 0;
605}
606
607/**
608 * vmw_cmdbuf_idle - Wait for command buffer manager idle.
609 *
610 * @man: The command buffer manager.
611 * @interruptible: Sleep interruptible while waiting.
612 * @timeout: Time out after this many ticks.
613 *
614 * Wait until the command buffer manager has processed all command buffers,
615 * or until a timeout occurs. If a timeout occurs, the function will return
616 * -EBUSY.
617 */
618int vmw_cmdbuf_idle(struct vmw_cmdbuf_man *man, bool interruptible,
619 unsigned long timeout)
620{
621 int ret;
622
623 ret = vmw_cmdbuf_cur_flush(man, interruptible);
624 vmw_generic_waiter_add(man->dev_priv,
625 SVGA_IRQFLAG_COMMAND_BUFFER,
626 &man->dev_priv->cmdbuf_waiters);
627
628 if (interruptible) {
629 ret = wait_event_interruptible_timeout
630 (man->idle_queue, vmw_cmdbuf_man_idle(man, true),
631 timeout);
632 } else {
633 ret = wait_event_timeout
634 (man->idle_queue, vmw_cmdbuf_man_idle(man, true),
635 timeout);
636 }
637 vmw_generic_waiter_remove(man->dev_priv,
638 SVGA_IRQFLAG_COMMAND_BUFFER,
639 &man->dev_priv->cmdbuf_waiters);
640 if (ret == 0) {
641 if (!vmw_cmdbuf_man_idle(man, true))
642 ret = -EBUSY;
643 else
644 ret = 0;
645 }
646 if (ret > 0)
647 ret = 0;
648
649 return ret;
650}
651
652/**
653 * vmw_cmdbuf_try_alloc - Try to allocate buffer space from the main pool.
654 *
655 * @man: The command buffer manager.
656 * @info: Allocation info. Will hold the size on entry and allocated mm node
657 * on successful return.
658 *
659 * Try to allocate buffer space from the main pool. Returns true if succeeded.
660 * If a fatal error was hit, the error code is returned in @info->ret.
661 */
662static bool vmw_cmdbuf_try_alloc(struct vmw_cmdbuf_man *man,
663 struct vmw_cmdbuf_alloc_info *info)
664{
665 int ret;
666
Thomas Hellstrom9b590782015-06-26 04:46:52 -0700667 if (info->done)
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700668 return true;
Thomas Hellstrom9b590782015-06-26 04:46:52 -0700669
670 memset(info->node, 0, sizeof(*info->node));
Thomas Hellstromef369902017-08-24 08:06:28 +0200671 spin_lock(&man->lock);
Chris Wilson4e64e552017-02-02 21:04:38 +0000672 ret = drm_mm_insert_node(&man->mm, info->node, info->page_size);
Thomas Hellstrom575f9c82015-09-29 07:49:56 -0700673 if (ret) {
Thomas Hellstrom09dc1382015-10-21 21:31:49 +0200674 vmw_cmdbuf_man_process(man);
Chris Wilson4e64e552017-02-02 21:04:38 +0000675 ret = drm_mm_insert_node(&man->mm, info->node, info->page_size);
Thomas Hellstrom575f9c82015-09-29 07:49:56 -0700676 }
677
Thomas Hellstromef369902017-08-24 08:06:28 +0200678 spin_unlock(&man->lock);
Thomas Hellstrom9b590782015-06-26 04:46:52 -0700679 info->done = !ret;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700680
Thomas Hellstrom9b590782015-06-26 04:46:52 -0700681 return info->done;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700682}
683
684/**
685 * vmw_cmdbuf_alloc_space - Allocate buffer space from the main pool.
686 *
687 * @man: The command buffer manager.
Thomas Hellstrom9b590782015-06-26 04:46:52 -0700688 * @node: Pointer to pre-allocated range-manager node.
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700689 * @size: The size of the allocation.
690 * @interruptible: Whether to sleep interruptible while waiting for space.
691 *
692 * This function allocates buffer space from the main pool, and if there is
693 * no space available ATM, it turns on IRQ handling and sleeps waiting for it to
694 * become available.
695 */
Thomas Hellstromb9eb1a62015-04-02 02:39:45 -0700696static int vmw_cmdbuf_alloc_space(struct vmw_cmdbuf_man *man,
697 struct drm_mm_node *node,
698 size_t size,
699 bool interruptible)
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700700{
701 struct vmw_cmdbuf_alloc_info info;
702
703 info.page_size = PAGE_ALIGN(size) >> PAGE_SHIFT;
Thomas Hellstrom9b590782015-06-26 04:46:52 -0700704 info.node = node;
705 info.done = false;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700706
707 /*
708 * To prevent starvation of large requests, only one allocating call
709 * at a time waiting for space.
710 */
711 if (interruptible) {
712 if (mutex_lock_interruptible(&man->space_mutex))
Thomas Hellstrom9b590782015-06-26 04:46:52 -0700713 return -ERESTARTSYS;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700714 } else {
715 mutex_lock(&man->space_mutex);
716 }
717
718 /* Try to allocate space without waiting. */
Thomas Hellstrom9b590782015-06-26 04:46:52 -0700719 if (vmw_cmdbuf_try_alloc(man, &info))
720 goto out_unlock;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700721
722 vmw_generic_waiter_add(man->dev_priv,
723 SVGA_IRQFLAG_COMMAND_BUFFER,
724 &man->dev_priv->cmdbuf_waiters);
725
726 if (interruptible) {
727 int ret;
728
729 ret = wait_event_interruptible
730 (man->alloc_queue, vmw_cmdbuf_try_alloc(man, &info));
731 if (ret) {
732 vmw_generic_waiter_remove
733 (man->dev_priv, SVGA_IRQFLAG_COMMAND_BUFFER,
734 &man->dev_priv->cmdbuf_waiters);
735 mutex_unlock(&man->space_mutex);
Thomas Hellstrom9b590782015-06-26 04:46:52 -0700736 return ret;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700737 }
738 } else {
739 wait_event(man->alloc_queue, vmw_cmdbuf_try_alloc(man, &info));
740 }
741 vmw_generic_waiter_remove(man->dev_priv,
742 SVGA_IRQFLAG_COMMAND_BUFFER,
743 &man->dev_priv->cmdbuf_waiters);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700744
Thomas Hellstrom9b590782015-06-26 04:46:52 -0700745out_unlock:
746 mutex_unlock(&man->space_mutex);
747
748 return 0;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700749}
750
751/**
752 * vmw_cmdbuf_space_pool - Set up a command buffer header with command buffer
753 * space from the main pool.
754 *
755 * @man: The command buffer manager.
756 * @header: Pointer to the header to set up.
757 * @size: The requested size of the buffer space.
758 * @interruptible: Whether to sleep interruptible while waiting for space.
759 */
760static int vmw_cmdbuf_space_pool(struct vmw_cmdbuf_man *man,
761 struct vmw_cmdbuf_header *header,
762 size_t size,
763 bool interruptible)
764{
765 SVGACBHeader *cb_hdr;
766 size_t offset;
767 int ret;
768
769 if (!man->has_pool)
770 return -ENOMEM;
771
Thomas Hellstrom9b590782015-06-26 04:46:52 -0700772 ret = vmw_cmdbuf_alloc_space(man, &header->node, size, interruptible);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700773
Thomas Hellstrom9b590782015-06-26 04:46:52 -0700774 if (ret)
775 return ret;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700776
Souptick Joardera02f6da2016-12-02 01:25:45 +0530777 header->cb_header = dma_pool_zalloc(man->headers, GFP_KERNEL,
778 &header->handle);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700779 if (!header->cb_header) {
780 ret = -ENOMEM;
781 goto out_no_cb_header;
782 }
783
Thomas Hellstrom9b590782015-06-26 04:46:52 -0700784 header->size = header->node.size << PAGE_SHIFT;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700785 cb_hdr = header->cb_header;
Thomas Hellstrom9b590782015-06-26 04:46:52 -0700786 offset = header->node.start << PAGE_SHIFT;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700787 header->cmd = man->map + offset;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700788 if (man->using_mob) {
789 cb_hdr->flags = SVGA_CB_FLAG_MOB;
790 cb_hdr->ptr.mob.mobid = man->cmd_space->mem.start;
791 cb_hdr->ptr.mob.mobOffset = offset;
792 } else {
793 cb_hdr->ptr.pa = (u64)man->handle + (u64)offset;
794 }
795
796 return 0;
797
798out_no_cb_header:
Thomas Hellstromef369902017-08-24 08:06:28 +0200799 spin_lock(&man->lock);
Thomas Hellstrom9b590782015-06-26 04:46:52 -0700800 drm_mm_remove_node(&header->node);
Thomas Hellstromef369902017-08-24 08:06:28 +0200801 spin_unlock(&man->lock);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700802
803 return ret;
804}
805
806/**
807 * vmw_cmdbuf_space_inline - Set up a command buffer header with
808 * inline command buffer space.
809 *
810 * @man: The command buffer manager.
811 * @header: Pointer to the header to set up.
812 * @size: The requested size of the buffer space.
813 */
814static int vmw_cmdbuf_space_inline(struct vmw_cmdbuf_man *man,
815 struct vmw_cmdbuf_header *header,
816 int size)
817{
818 struct vmw_cmdbuf_dheader *dheader;
819 SVGACBHeader *cb_hdr;
820
821 if (WARN_ON_ONCE(size > VMW_CMDBUF_INLINE_SIZE))
822 return -ENOMEM;
823
Souptick Joardera02f6da2016-12-02 01:25:45 +0530824 dheader = dma_pool_zalloc(man->dheaders, GFP_KERNEL,
825 &header->handle);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700826 if (!dheader)
827 return -ENOMEM;
828
829 header->inline_space = true;
830 header->size = VMW_CMDBUF_INLINE_SIZE;
831 cb_hdr = &dheader->cb_header;
832 header->cb_header = cb_hdr;
833 header->cmd = dheader->cmd;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700834 cb_hdr->status = SVGA_CB_STATUS_NONE;
835 cb_hdr->flags = SVGA_CB_FLAG_NONE;
836 cb_hdr->ptr.pa = (u64)header->handle +
837 (u64)offsetof(struct vmw_cmdbuf_dheader, cmd);
838
839 return 0;
840}
841
842/**
843 * vmw_cmdbuf_alloc - Allocate a command buffer header complete with
844 * command buffer space.
845 *
846 * @man: The command buffer manager.
847 * @size: The requested size of the buffer space.
848 * @interruptible: Whether to sleep interruptible while waiting for space.
849 * @p_header: points to a header pointer to populate on successful return.
850 *
851 * Returns a pointer to command buffer space if successful. Otherwise
852 * returns an error pointer. The header pointer returned in @p_header should
853 * be used for upcoming calls to vmw_cmdbuf_reserve() and vmw_cmdbuf_commit().
854 */
855void *vmw_cmdbuf_alloc(struct vmw_cmdbuf_man *man,
856 size_t size, bool interruptible,
857 struct vmw_cmdbuf_header **p_header)
858{
859 struct vmw_cmdbuf_header *header;
860 int ret = 0;
861
862 *p_header = NULL;
863
864 header = kzalloc(sizeof(*header), GFP_KERNEL);
865 if (!header)
866 return ERR_PTR(-ENOMEM);
867
868 if (size <= VMW_CMDBUF_INLINE_SIZE)
869 ret = vmw_cmdbuf_space_inline(man, header, size);
870 else
871 ret = vmw_cmdbuf_space_pool(man, header, size, interruptible);
872
873 if (ret) {
874 kfree(header);
875 return ERR_PTR(ret);
876 }
877
878 header->man = man;
879 INIT_LIST_HEAD(&header->list);
880 header->cb_header->status = SVGA_CB_STATUS_NONE;
881 *p_header = header;
882
883 return header->cmd;
884}
885
886/**
887 * vmw_cmdbuf_reserve_cur - Reserve space for commands in the current
888 * command buffer.
889 *
890 * @man: The command buffer manager.
891 * @size: The requested size of the commands.
892 * @ctx_id: The context id if any. Otherwise set to SVGA3D_REG_INVALID.
893 * @interruptible: Whether to sleep interruptible while waiting for space.
894 *
895 * Returns a pointer to command buffer space if successful. Otherwise
896 * returns an error pointer.
897 */
898static void *vmw_cmdbuf_reserve_cur(struct vmw_cmdbuf_man *man,
899 size_t size,
900 int ctx_id,
901 bool interruptible)
902{
903 struct vmw_cmdbuf_header *cur;
904 void *ret;
905
906 if (vmw_cmdbuf_cur_lock(man, interruptible))
907 return ERR_PTR(-ERESTARTSYS);
908
909 cur = man->cur;
910 if (cur && (size + man->cur_pos > cur->size ||
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700911 ((cur->cb_header->flags & SVGA_CB_FLAG_DX_CONTEXT) &&
912 ctx_id != cur->cb_header->dxContext)))
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700913 __vmw_cmdbuf_cur_flush(man);
914
915 if (!man->cur) {
916 ret = vmw_cmdbuf_alloc(man,
917 max_t(size_t, size, man->default_size),
918 interruptible, &man->cur);
919 if (IS_ERR(ret)) {
920 vmw_cmdbuf_cur_unlock(man);
921 return ret;
922 }
923
924 cur = man->cur;
925 }
926
927 if (ctx_id != SVGA3D_INVALID_ID) {
928 cur->cb_header->flags |= SVGA_CB_FLAG_DX_CONTEXT;
929 cur->cb_header->dxContext = ctx_id;
930 }
931
932 cur->reserved = size;
933
934 return (void *) (man->cur->cmd + man->cur_pos);
935}
936
937/**
938 * vmw_cmdbuf_commit_cur - Commit commands in the current command buffer.
939 *
940 * @man: The command buffer manager.
941 * @size: The size of the commands actually written.
942 * @flush: Whether to flush the command buffer immediately.
943 */
944static void vmw_cmdbuf_commit_cur(struct vmw_cmdbuf_man *man,
945 size_t size, bool flush)
946{
947 struct vmw_cmdbuf_header *cur = man->cur;
948
949 WARN_ON(!mutex_is_locked(&man->cur_mutex));
950
951 WARN_ON(size > cur->reserved);
952 man->cur_pos += size;
953 if (!size)
954 cur->cb_header->flags &= ~SVGA_CB_FLAG_DX_CONTEXT;
955 if (flush)
956 __vmw_cmdbuf_cur_flush(man);
957 vmw_cmdbuf_cur_unlock(man);
958}
959
960/**
961 * vmw_cmdbuf_reserve - Reserve space for commands in a command buffer.
962 *
963 * @man: The command buffer manager.
964 * @size: The requested size of the commands.
965 * @ctx_id: The context id if any. Otherwise set to SVGA3D_REG_INVALID.
966 * @interruptible: Whether to sleep interruptible while waiting for space.
967 * @header: Header of the command buffer. NULL if the current command buffer
968 * should be used.
969 *
970 * Returns a pointer to command buffer space if successful. Otherwise
971 * returns an error pointer.
972 */
973void *vmw_cmdbuf_reserve(struct vmw_cmdbuf_man *man, size_t size,
974 int ctx_id, bool interruptible,
975 struct vmw_cmdbuf_header *header)
976{
977 if (!header)
978 return vmw_cmdbuf_reserve_cur(man, size, ctx_id, interruptible);
979
980 if (size > header->size)
981 return ERR_PTR(-EINVAL);
982
983 if (ctx_id != SVGA3D_INVALID_ID) {
984 header->cb_header->flags |= SVGA_CB_FLAG_DX_CONTEXT;
985 header->cb_header->dxContext = ctx_id;
986 }
987
988 header->reserved = size;
989 return header->cmd;
990}
991
992/**
993 * vmw_cmdbuf_commit - Commit commands in a command buffer.
994 *
995 * @man: The command buffer manager.
996 * @size: The size of the commands actually written.
997 * @header: Header of the command buffer. NULL if the current command buffer
998 * should be used.
999 * @flush: Whether to flush the command buffer immediately.
1000 */
1001void vmw_cmdbuf_commit(struct vmw_cmdbuf_man *man, size_t size,
1002 struct vmw_cmdbuf_header *header, bool flush)
1003{
1004 if (!header) {
1005 vmw_cmdbuf_commit_cur(man, size, flush);
1006 return;
1007 }
1008
1009 (void) vmw_cmdbuf_cur_lock(man, false);
1010 __vmw_cmdbuf_cur_flush(man);
1011 WARN_ON(size > header->reserved);
1012 man->cur = header;
1013 man->cur_pos = size;
1014 if (!size)
1015 header->cb_header->flags &= ~SVGA_CB_FLAG_DX_CONTEXT;
1016 if (flush)
1017 __vmw_cmdbuf_cur_flush(man);
1018 vmw_cmdbuf_cur_unlock(man);
1019}
1020
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07001021
1022/**
1023 * vmw_cmdbuf_send_device_command - Send a command through the device context.
1024 *
1025 * @man: The command buffer manager.
1026 * @command: Pointer to the command to send.
1027 * @size: Size of the command.
1028 *
1029 * Synchronously sends a device context command.
1030 */
1031static int vmw_cmdbuf_send_device_command(struct vmw_cmdbuf_man *man,
1032 const void *command,
1033 size_t size)
1034{
1035 struct vmw_cmdbuf_header *header;
1036 int status;
1037 void *cmd = vmw_cmdbuf_alloc(man, size, false, &header);
1038
1039 if (IS_ERR(cmd))
1040 return PTR_ERR(cmd);
1041
1042 memcpy(cmd, command, size);
1043 header->cb_header->length = size;
1044 header->cb_context = SVGA_CB_CONTEXT_DEVICE;
Thomas Hellstromef369902017-08-24 08:06:28 +02001045 spin_lock(&man->lock);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07001046 status = vmw_cmdbuf_header_submit(header);
Thomas Hellstromef369902017-08-24 08:06:28 +02001047 spin_unlock(&man->lock);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07001048 vmw_cmdbuf_header_free(header);
1049
1050 if (status != SVGA_CB_STATUS_COMPLETED) {
1051 DRM_ERROR("Device context command failed with status %d\n",
1052 status);
1053 return -EINVAL;
1054 }
1055
1056 return 0;
1057}
1058
1059/**
1060 * vmw_cmdbuf_startstop - Send a start / stop command through the device
1061 * context.
1062 *
1063 * @man: The command buffer manager.
1064 * @enable: Whether to enable or disable the context.
1065 *
1066 * Synchronously sends a device start / stop context command.
1067 */
1068static int vmw_cmdbuf_startstop(struct vmw_cmdbuf_man *man,
1069 bool enable)
1070{
1071 struct {
1072 uint32 id;
1073 SVGADCCmdStartStop body;
1074 } __packed cmd;
1075
1076 cmd.id = SVGA_DC_CMD_START_STOP_CONTEXT;
1077 cmd.body.enable = (enable) ? 1 : 0;
1078 cmd.body.context = SVGA_CB_CONTEXT_0;
1079
1080 return vmw_cmdbuf_send_device_command(man, &cmd, sizeof(cmd));
1081}
1082
1083/**
1084 * vmw_cmdbuf_set_pool_size - Set command buffer manager sizes
1085 *
1086 * @man: The command buffer manager.
1087 * @size: The size of the main space pool.
1088 * @default_size: The default size of the command buffer for small kernel
1089 * submissions.
1090 *
1091 * Set the size and allocate the main command buffer space pool,
1092 * as well as the default size of the command buffer for
1093 * small kernel submissions. If successful, this enables large command
1094 * submissions. Note that this function requires that rudimentary command
1095 * submission is already available and that the MOB memory manager is alive.
1096 * Returns 0 on success. Negative error code on failure.
1097 */
1098int vmw_cmdbuf_set_pool_size(struct vmw_cmdbuf_man *man,
1099 size_t size, size_t default_size)
1100{
1101 struct vmw_private *dev_priv = man->dev_priv;
1102 bool dummy;
1103 int ret;
1104
1105 if (man->has_pool)
1106 return -EINVAL;
1107
1108 /* First, try to allocate a huge chunk of DMA memory */
1109 size = PAGE_ALIGN(size);
1110 man->map = dma_alloc_coherent(&dev_priv->dev->pdev->dev, size,
1111 &man->handle, GFP_KERNEL);
1112 if (man->map) {
1113 man->using_mob = false;
1114 } else {
1115 /*
1116 * DMA memory failed. If we can have command buffers in a
1117 * MOB, try to use that instead. Note that this will
1118 * actually call into the already enabled manager, when
1119 * binding the MOB.
1120 */
Sinclair Yeh8ce75f82015-07-08 21:20:39 -07001121 if (!(dev_priv->capabilities & SVGA_CAP_DX))
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07001122 return -ENOMEM;
1123
1124 ret = ttm_bo_create(&dev_priv->bdev, size, ttm_bo_type_device,
1125 &vmw_mob_ne_placement, 0, false, NULL,
1126 &man->cmd_space);
1127 if (ret)
1128 return ret;
1129
1130 man->using_mob = true;
1131 ret = ttm_bo_kmap(man->cmd_space, 0, size >> PAGE_SHIFT,
1132 &man->map_obj);
1133 if (ret)
1134 goto out_no_map;
1135
1136 man->map = ttm_kmap_obj_virtual(&man->map_obj, &dummy);
1137 }
1138
1139 man->size = size;
1140 drm_mm_init(&man->mm, 0, size >> PAGE_SHIFT);
1141
1142 man->has_pool = true;
Thomas Hellstrom09dc1382015-10-21 21:31:49 +02001143
1144 /*
1145 * For now, set the default size to VMW_CMDBUF_INLINE_SIZE to
1146 * prevent deadlocks from happening when vmw_cmdbuf_space_pool()
1147 * needs to wait for space and we block on further command
1148 * submissions to be able to free up space.
1149 */
1150 man->default_size = VMW_CMDBUF_INLINE_SIZE;
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07001151 DRM_INFO("Using command buffers with %s pool.\n",
1152 (man->using_mob) ? "MOB" : "DMA");
1153
1154 return 0;
1155
1156out_no_map:
1157 if (man->using_mob)
1158 ttm_bo_unref(&man->cmd_space);
1159
1160 return ret;
1161}
1162
1163/**
1164 * vmw_cmdbuf_man_create: Create a command buffer manager and enable it for
1165 * inline command buffer submissions only.
1166 *
1167 * @dev_priv: Pointer to device private structure.
1168 *
1169 * Returns a pointer to a cummand buffer manager to success or error pointer
1170 * on failure. The command buffer manager will be enabled for submissions of
1171 * size VMW_CMDBUF_INLINE_SIZE only.
1172 */
1173struct vmw_cmdbuf_man *vmw_cmdbuf_man_create(struct vmw_private *dev_priv)
1174{
1175 struct vmw_cmdbuf_man *man;
1176 struct vmw_cmdbuf_context *ctx;
1177 int i;
1178 int ret;
1179
1180 if (!(dev_priv->capabilities & SVGA_CAP_COMMAND_BUFFERS))
1181 return ERR_PTR(-ENOSYS);
1182
1183 man = kzalloc(sizeof(*man), GFP_KERNEL);
1184 if (!man)
1185 return ERR_PTR(-ENOMEM);
1186
1187 man->headers = dma_pool_create("vmwgfx cmdbuf",
1188 &dev_priv->dev->pdev->dev,
1189 sizeof(SVGACBHeader),
1190 64, PAGE_SIZE);
1191 if (!man->headers) {
1192 ret = -ENOMEM;
1193 goto out_no_pool;
1194 }
1195
1196 man->dheaders = dma_pool_create("vmwgfx inline cmdbuf",
1197 &dev_priv->dev->pdev->dev,
1198 sizeof(struct vmw_cmdbuf_dheader),
1199 64, PAGE_SIZE);
1200 if (!man->dheaders) {
1201 ret = -ENOMEM;
1202 goto out_no_dpool;
1203 }
1204
1205 for_each_cmdbuf_ctx(man, i, ctx)
1206 vmw_cmdbuf_ctx_init(ctx);
1207
1208 INIT_LIST_HEAD(&man->error);
1209 spin_lock_init(&man->lock);
1210 mutex_init(&man->cur_mutex);
1211 mutex_init(&man->space_mutex);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07001212 man->default_size = VMW_CMDBUF_INLINE_SIZE;
1213 init_waitqueue_head(&man->alloc_queue);
1214 init_waitqueue_head(&man->idle_queue);
1215 man->dev_priv = dev_priv;
1216 man->max_hw_submitted = SVGA_CB_MAX_QUEUED_PER_CONTEXT - 1;
1217 INIT_WORK(&man->work, &vmw_cmdbuf_work_func);
1218 vmw_generic_waiter_add(dev_priv, SVGA_IRQFLAG_ERROR,
1219 &dev_priv->error_waiters);
1220 ret = vmw_cmdbuf_startstop(man, true);
1221 if (ret) {
1222 DRM_ERROR("Failed starting command buffer context 0.\n");
1223 vmw_cmdbuf_man_destroy(man);
1224 return ERR_PTR(ret);
1225 }
1226
1227 return man;
1228
1229out_no_dpool:
1230 dma_pool_destroy(man->headers);
1231out_no_pool:
1232 kfree(man);
1233
1234 return ERR_PTR(ret);
1235}
1236
1237/**
1238 * vmw_cmdbuf_remove_pool - Take down the main buffer space pool.
1239 *
1240 * @man: Pointer to a command buffer manager.
1241 *
1242 * This function removes the main buffer space pool, and should be called
1243 * before MOB memory management is removed. When this function has been called,
1244 * only small command buffer submissions of size VMW_CMDBUF_INLINE_SIZE or
1245 * less are allowed, and the default size of the command buffer for small kernel
1246 * submissions is also set to this size.
1247 */
1248void vmw_cmdbuf_remove_pool(struct vmw_cmdbuf_man *man)
1249{
1250 if (!man->has_pool)
1251 return;
1252
1253 man->has_pool = false;
1254 man->default_size = VMW_CMDBUF_INLINE_SIZE;
1255 (void) vmw_cmdbuf_idle(man, false, 10*HZ);
1256 if (man->using_mob) {
1257 (void) ttm_bo_kunmap(&man->map_obj);
1258 ttm_bo_unref(&man->cmd_space);
1259 } else {
1260 dma_free_coherent(&man->dev_priv->dev->pdev->dev,
1261 man->size, man->map, man->handle);
1262 }
1263}
1264
1265/**
1266 * vmw_cmdbuf_man_destroy - Take down a command buffer manager.
1267 *
1268 * @man: Pointer to a command buffer manager.
1269 *
1270 * This function idles and then destroys a command buffer manager.
1271 */
1272void vmw_cmdbuf_man_destroy(struct vmw_cmdbuf_man *man)
1273{
1274 WARN_ON_ONCE(man->has_pool);
1275 (void) vmw_cmdbuf_idle(man, false, 10*HZ);
1276 if (vmw_cmdbuf_startstop(man, false))
1277 DRM_ERROR("Failed stopping command buffer context 0.\n");
1278
1279 vmw_generic_waiter_remove(man->dev_priv, SVGA_IRQFLAG_ERROR,
1280 &man->dev_priv->error_waiters);
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07001281 (void) cancel_work_sync(&man->work);
1282 dma_pool_destroy(man->dheaders);
1283 dma_pool_destroy(man->headers);
1284 mutex_destroy(&man->cur_mutex);
1285 mutex_destroy(&man->space_mutex);
1286 kfree(man);
1287}