Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /**************************************************************************
  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.  
  28. #include "vmwgfx_drv.h"
  29. #include "ttm/ttm_bo_api.h"
  30.  
  31. /*
  32.  * Size of inline command buffers. Try to make sure that a page size is a
  33.  * multiple of the DMA pool allocation size.
  34.  */
  35. #define VMW_CMDBUF_INLINE_ALIGN 64
  36. #define VMW_CMDBUF_INLINE_SIZE \
  37.         (1024 - ALIGN(sizeof(SVGACBHeader), VMW_CMDBUF_INLINE_ALIGN))
  38.  
  39. /**
  40.  * struct vmw_cmdbuf_context - Command buffer context queues
  41.  *
  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
  47.  */
  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;
  53. };
  54.  
  55. /**
  56.  * struct vmw_cmdbuf_man: - Command buffer manager
  57.  *
  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.
  63.  * Immutable.
  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.
  68.  * Protected by @lock.
  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
  77.  * the @cur_mutex.
  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
  81.  * handle. Immutable.
  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
  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.  */
  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;
  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;
  119. //   struct tasklet_struct tasklet;
  120.         wait_queue_head_t alloc_queue;
  121.         wait_queue_head_t idle_queue;
  122.         bool irq_on;
  123.         bool using_mob;
  124.         bool has_pool;
  125.         dma_addr_t handle;
  126.         size_t size;
  127. };
  128.  
  129. /**
  130.  * struct vmw_cmdbuf_header - Command buffer metadata
  131.  *
  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
  138.  * buffer submission.
  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.
  143.  */
  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;
  150.         dma_addr_t handle;
  151.         u8 *cmd;
  152.         size_t size;
  153.         size_t reserved;
  154.         bool inline_space;
  155. };
  156.  
  157. /**
  158.  * struct vmw_cmdbuf_dheader - Device command buffer header with inline
  159.  * command buffer space.
  160.  *
  161.  * @cb_header: Device command buffer header.
  162.  * @cmd: Inline command buffer space.
  163.  */
  164. struct vmw_cmdbuf_dheader {
  165.         SVGACBHeader cb_header;
  166.         u8 cmd[VMW_CMDBUF_INLINE_SIZE] __aligned(VMW_CMDBUF_INLINE_ALIGN);
  167. };
  168.  
  169. /**
  170.  * struct vmw_cmdbuf_alloc_info - Command buffer space allocation metadata
  171.  *
  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.
  175.  */
  176. struct vmw_cmdbuf_alloc_info {
  177.         size_t page_size;
  178.         struct drm_mm_node *node;
  179.         bool done;
  180. };
  181.  
  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; \
  185.              ++(_i), ++(_ctx))
  186.  
  187. static int vmw_cmdbuf_startstop(struct vmw_cmdbuf_man *man, bool enable);
  188.  
  189.  
  190. /**
  191.  * vmw_cmdbuf_cur_lock - Helper to lock the cur_mutex.
  192.  *
  193.  * @man: The range manager.
  194.  * @interruptible: Whether to wait interruptible when locking.
  195.  */
  196. static int vmw_cmdbuf_cur_lock(struct vmw_cmdbuf_man *man, bool interruptible)
  197. {
  198.         if (interruptible) {
  199.                 if (mutex_lock_interruptible(&man->cur_mutex))
  200.                         return -ERESTARTSYS;
  201.         } else {
  202.                 mutex_lock(&man->cur_mutex);
  203.         }
  204.  
  205.         return 0;
  206. }
  207.  
  208. /**
  209.  * vmw_cmdbuf_cur_unlock - Helper to unlock the cur_mutex.
  210.  *
  211.  * @man: The range manager.
  212.  */
  213. static void vmw_cmdbuf_cur_unlock(struct vmw_cmdbuf_man *man)
  214. {
  215.         mutex_unlock(&man->cur_mutex);
  216. }
  217.  
  218. /**
  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.
  222.  *
  223.  * @header: Pointer to the header to free.
  224.  */
  225. static void vmw_cmdbuf_header_inline_free(struct vmw_cmdbuf_header *header)
  226. {
  227.         struct vmw_cmdbuf_dheader *dheader;
  228.  
  229.         if (WARN_ON_ONCE(!header->inline_space))
  230.                 return;
  231.  
  232.         dheader = container_of(header->cb_header, struct vmw_cmdbuf_dheader,
  233.                                cb_header);
  234.         dma_pool_free(header->man->dheaders, dheader, header->handle);
  235.         kfree(header);
  236. }
  237.  
  238. /**
  239.  * __vmw_cmdbuf_header_free - Free a struct vmw_cmdbuf_header  and its
  240.  * associated structures.
  241.  *
  242.  * header: Pointer to the header to free.
  243.  *
  244.  * For internal use. Must be called with man::lock held.
  245.  */
  246. static void __vmw_cmdbuf_header_free(struct vmw_cmdbuf_header *header)
  247. {
  248.         struct vmw_cmdbuf_man *man = header->man;
  249.  
  250.         if (header->inline_space) {
  251.                 vmw_cmdbuf_header_inline_free(header);
  252.                 return;
  253.         }
  254.  
  255.         drm_mm_remove_node(&header->node);
  256.         wake_up_all(&man->alloc_queue);
  257.         if (header->cb_header)
  258.                 dma_pool_free(man->headers, header->cb_header,
  259.                               header->handle);
  260.         kfree(header);
  261. }
  262.  
  263. /**
  264.  * vmw_cmdbuf_header_free - Free a struct vmw_cmdbuf_header  and its
  265.  * associated structures.
  266.  *
  267.  * @header: Pointer to the header to free.
  268.  */
  269. void vmw_cmdbuf_header_free(struct vmw_cmdbuf_header *header)
  270. {
  271.         struct vmw_cmdbuf_man *man = header->man;
  272.  
  273.         /* Avoid locking if inline_space */
  274.         if (header->inline_space) {
  275.                 vmw_cmdbuf_header_inline_free(header);
  276.                 return;
  277.         }
  278.     spin_lock(&man->lock);
  279.         __vmw_cmdbuf_header_free(header);
  280.     spin_unlock(&man->lock);
  281. }
  282.  
  283.  
  284. /**
  285.  * vmw_cmbuf_header_submit: Submit a command buffer to hardware.
  286.  *
  287.  * @header: The header of the buffer to submit.
  288.  */
  289. static int vmw_cmdbuf_header_submit(struct vmw_cmdbuf_header *header)
  290. {
  291.         struct vmw_cmdbuf_man *man = header->man;
  292.         u32 val;
  293.  
  294.         if (sizeof(header->handle) > 4)
  295.                 val = (header->handle >> 32);
  296.         else
  297.                 val = 0;
  298.         vmw_write(man->dev_priv, SVGA_REG_COMMAND_HIGH, val);
  299.  
  300.         val = (header->handle & 0xFFFFFFFFULL);
  301.         val |= header->cb_context & SVGA_CB_CONTEXT_MASK;
  302.         vmw_write(man->dev_priv, SVGA_REG_COMMAND_LOW, val);
  303.  
  304.         return header->cb_header->status;
  305. }
  306.  
  307. /**
  308.  * vmw_cmdbuf_ctx_init: Initialize a command buffer context.
  309.  *
  310.  * @ctx: The command buffer context to initialize
  311.  */
  312. static void vmw_cmdbuf_ctx_init(struct vmw_cmdbuf_context *ctx)
  313. {
  314.         INIT_LIST_HEAD(&ctx->hw_submitted);
  315.         INIT_LIST_HEAD(&ctx->submitted);
  316.         INIT_LIST_HEAD(&ctx->preempted);
  317.         ctx->num_hw_submitted = 0;
  318. }
  319.  
  320. /**
  321.  * vmw_cmdbuf_ctx_submit: Submit command buffers from a command buffer
  322.  * context.
  323.  *
  324.  * @man: The command buffer manager.
  325.  * @ctx: The command buffer context.
  326.  *
  327.  * Submits command buffers to hardware until there are no more command
  328.  * buffers to submit or the hardware can't handle more command buffers.
  329.  */
  330. static void vmw_cmdbuf_ctx_submit(struct vmw_cmdbuf_man *man,
  331.                                   struct vmw_cmdbuf_context *ctx)
  332. {
  333.         while (ctx->num_hw_submitted < man->max_hw_submitted &&
  334.               !list_empty(&ctx->submitted)) {
  335.                 struct vmw_cmdbuf_header *entry;
  336.                 SVGACBStatus status;
  337.  
  338.                 entry = list_first_entry(&ctx->submitted,
  339.                                          struct vmw_cmdbuf_header,
  340.                                          list);
  341.  
  342.                 status = vmw_cmdbuf_header_submit(entry);
  343.  
  344.                 /* This should never happen */
  345.                 if (WARN_ON_ONCE(status == SVGA_CB_STATUS_QUEUE_FULL)) {
  346.                         entry->cb_header->status = SVGA_CB_STATUS_NONE;
  347.                         break;
  348.                 }
  349.  
  350.                 list_del(&entry->list);
  351.                 list_add_tail(&entry->list, &ctx->hw_submitted);
  352.                 ctx->num_hw_submitted++;
  353.         }
  354.  
  355. }
  356.  
  357. /**
  358.  * vmw_cmdbuf_ctx_submit: Process a command buffer context.
  359.  *
  360.  * @man: The command buffer manager.
  361.  * @ctx: The command buffer context.
  362.  *
  363.  * Submit command buffers to hardware if possible, and process finished
  364.  * buffers. Typically freeing them, but on preemption or error take
  365.  * appropriate action. Wake up waiters if appropriate.
  366.  */
  367. static void vmw_cmdbuf_ctx_process(struct vmw_cmdbuf_man *man,
  368.                                    struct vmw_cmdbuf_context *ctx,
  369.                                    int *notempty)
  370. {
  371.         struct vmw_cmdbuf_header *entry, *next;
  372.  
  373.         vmw_cmdbuf_ctx_submit(man, ctx);
  374.  
  375.         list_for_each_entry_safe(entry, next, &ctx->hw_submitted, list) {
  376.                 SVGACBStatus status = entry->cb_header->status;
  377.  
  378.                 if (status == SVGA_CB_STATUS_NONE)
  379.                         break;
  380.  
  381.                 list_del(&entry->list);
  382.                 wake_up_all(&man->idle_queue);
  383.                 ctx->num_hw_submitted--;
  384.                 switch (status) {
  385.                 case SVGA_CB_STATUS_COMPLETED:
  386.                         __vmw_cmdbuf_header_free(entry);
  387.                         break;
  388.                 case SVGA_CB_STATUS_COMMAND_ERROR:
  389.                 case SVGA_CB_STATUS_CB_HEADER_ERROR:
  390.                         list_add_tail(&entry->list, &man->error);
  391.                         schedule_work(&man->work);
  392.                         break;
  393.                 case SVGA_CB_STATUS_PREEMPTED:
  394.                         list_add(&entry->list, &ctx->preempted);
  395.                         break;
  396.                 default:
  397.                         WARN_ONCE(true, "Undefined command buffer status.\n");
  398.                         __vmw_cmdbuf_header_free(entry);
  399.                         break;
  400.                 }
  401.         }
  402.  
  403.         vmw_cmdbuf_ctx_submit(man, ctx);
  404.         if (!list_empty(&ctx->submitted))
  405.                 (*notempty)++;
  406. }
  407.  
  408. /**
  409.  * vmw_cmdbuf_man_process - Process all command buffer contexts and
  410.  * switch on and off irqs as appropriate.
  411.  *
  412.  * @man: The command buffer manager.
  413.  *
  414.  * Calls vmw_cmdbuf_ctx_process() on all contexts. If any context has
  415.  * command buffers left that are not submitted to hardware, Make sure
  416.  * IRQ handling is turned on. Otherwise, make sure it's turned off.
  417.  */
  418. static void vmw_cmdbuf_man_process(struct vmw_cmdbuf_man *man)
  419. {
  420.         int notempty;
  421.         struct vmw_cmdbuf_context *ctx;
  422.         int i;
  423.  
  424. retry:
  425.         notempty = 0;
  426.         for_each_cmdbuf_ctx(man, i, ctx)
  427.                 vmw_cmdbuf_ctx_process(man, ctx, &notempty);
  428.  
  429.         if (man->irq_on && !notempty) {
  430.                 vmw_generic_waiter_remove(man->dev_priv,
  431.                                           SVGA_IRQFLAG_COMMAND_BUFFER,
  432.                                           &man->dev_priv->cmdbuf_waiters);
  433.                 man->irq_on = false;
  434.         } else if (!man->irq_on && notempty) {
  435.                 vmw_generic_waiter_add(man->dev_priv,
  436.                                        SVGA_IRQFLAG_COMMAND_BUFFER,
  437.                                        &man->dev_priv->cmdbuf_waiters);
  438.                 man->irq_on = true;
  439.  
  440.                 /* Rerun in case we just missed an irq. */
  441.                 goto retry;
  442.         }
  443. }
  444.  
  445. /**
  446.  * vmw_cmdbuf_ctx_add - Schedule a command buffer for submission on a
  447.  * command buffer context
  448.  *
  449.  * @man: The command buffer manager.
  450.  * @header: The header of the buffer to submit.
  451.  * @cb_context: The command buffer context to use.
  452.  *
  453.  * This function adds @header to the "submitted" queue of the command
  454.  * buffer context identified by @cb_context. It then calls the command buffer
  455.  * manager processing to potentially submit the buffer to hardware.
  456.  * @man->lock needs to be held when calling this function.
  457.  */
  458. static void vmw_cmdbuf_ctx_add(struct vmw_cmdbuf_man *man,
  459.                                struct vmw_cmdbuf_header *header,
  460.                                SVGACBContext cb_context)
  461. {
  462.         if (!(header->cb_header->flags & SVGA_CB_FLAG_DX_CONTEXT))
  463.                 header->cb_header->dxContext = 0;
  464.         header->cb_context = cb_context;
  465.         list_add_tail(&header->list, &man->ctx[cb_context].submitted);
  466.  
  467.         vmw_cmdbuf_man_process(man);
  468. }
  469.  
  470. /**
  471.  * vmw_cmdbuf_man_tasklet - The main part of the command buffer interrupt
  472.  * handler implemented as a tasklet.
  473.  *
  474.  * @data: Tasklet closure. A pointer to the command buffer manager cast to
  475.  * an unsigned long.
  476.  *
  477.  * The bottom half (tasklet) of the interrupt handler simply calls into the
  478.  * command buffer processor to free finished buffers and submit any
  479.  * queued buffers to hardware.
  480.  */
  481. static void vmw_cmdbuf_man_tasklet(unsigned long data)
  482. {
  483.         struct vmw_cmdbuf_man *man = (struct vmw_cmdbuf_man *) data;
  484.  
  485.         spin_lock(&man->lock);
  486.         vmw_cmdbuf_man_process(man);
  487.         spin_unlock(&man->lock);
  488. }
  489.  
  490. /**
  491.  * vmw_cmdbuf_work_func - The deferred work function that handles
  492.  * command buffer errors.
  493.  *
  494.  * @work: The work func closure argument.
  495.  *
  496.  * Restarting the command buffer context after an error requires process
  497.  * context, so it is deferred to this work function.
  498.  */
  499. static void vmw_cmdbuf_work_func(struct work_struct *work)
  500. {
  501.         struct vmw_cmdbuf_man *man =
  502.                 container_of(work, struct vmw_cmdbuf_man, work);
  503.         struct vmw_cmdbuf_header *entry, *next;
  504.         uint32_t dummy;
  505.         bool restart = false;
  506.  
  507.         spin_lock(&man->lock);
  508.         list_for_each_entry_safe(entry, next, &man->error, list) {
  509.                 restart = true;
  510.                 DRM_ERROR("Command buffer error.\n");
  511.  
  512.                 list_del(&entry->list);
  513.                 __vmw_cmdbuf_header_free(entry);
  514.                 wake_up_all(&man->idle_queue);
  515.         }
  516.         spin_unlock(&man->lock);
  517.  
  518.         if (restart && vmw_cmdbuf_startstop(man, true))
  519.                 DRM_ERROR("Failed restarting command buffer context 0.\n");
  520.  
  521.         /* Send a new fence in case one was removed */
  522.         vmw_fifo_send_fence(man->dev_priv, &dummy);
  523. }
  524.  
  525. /**
  526.  * vmw_cmdbuf_man idle - Check whether the command buffer manager is idle.
  527.  *
  528.  * @man: The command buffer manager.
  529.  * @check_preempted: Check also the preempted queue for pending command buffers.
  530.  *
  531.  */
  532. static bool vmw_cmdbuf_man_idle(struct vmw_cmdbuf_man *man,
  533.                                 bool check_preempted)
  534. {
  535.         struct vmw_cmdbuf_context *ctx;
  536.         bool idle = false;
  537.         int i;
  538.  
  539.     spin_lock(&man->lock);
  540.         vmw_cmdbuf_man_process(man);
  541.         for_each_cmdbuf_ctx(man, i, ctx) {
  542.                 if (!list_empty(&ctx->submitted) ||
  543.                     !list_empty(&ctx->hw_submitted) ||
  544.                     (check_preempted && !list_empty(&ctx->preempted)))
  545.                         goto out_unlock;
  546.         }
  547.  
  548.         idle = list_empty(&man->error);
  549.  
  550. out_unlock:
  551.     spin_unlock(&man->lock);
  552.  
  553.         return idle;
  554. }
  555.  
  556. /**
  557.  * __vmw_cmdbuf_cur_flush - Flush the current command buffer for small kernel
  558.  * command submissions
  559.  *
  560.  * @man: The command buffer manager.
  561.  *
  562.  * Flushes the current command buffer without allocating a new one. A new one
  563.  * is automatically allocated when needed. Call with @man->cur_mutex held.
  564.  */
  565. static void __vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man *man)
  566. {
  567.         struct vmw_cmdbuf_header *cur = man->cur;
  568.  
  569.         WARN_ON(!mutex_is_locked(&man->cur_mutex));
  570.  
  571.         if (!cur)
  572.                 return;
  573.  
  574.     spin_lock(&man->lock);
  575.         if (man->cur_pos == 0) {
  576.                 __vmw_cmdbuf_header_free(cur);
  577.                 goto out_unlock;
  578.         }
  579.  
  580.         man->cur->cb_header->length = man->cur_pos;
  581.         vmw_cmdbuf_ctx_add(man, man->cur, SVGA_CB_CONTEXT_0);
  582. out_unlock:
  583.     spin_unlock(&man->lock);
  584.         man->cur = NULL;
  585.         man->cur_pos = 0;
  586. }
  587.  
  588. /**
  589.  * vmw_cmdbuf_cur_flush - Flush the current command buffer for small kernel
  590.  * command submissions
  591.  *
  592.  * @man: The command buffer manager.
  593.  * @interruptible: Whether to sleep interruptible when sleeping.
  594.  *
  595.  * Flushes the current command buffer without allocating a new one. A new one
  596.  * is automatically allocated when needed.
  597.  */
  598. int vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man *man,
  599.                          bool interruptible)
  600. {
  601.         int ret = vmw_cmdbuf_cur_lock(man, interruptible);
  602.  
  603.         if (ret)
  604.                 return ret;
  605.  
  606.         __vmw_cmdbuf_cur_flush(man);
  607.         vmw_cmdbuf_cur_unlock(man);
  608.  
  609.         return 0;
  610. }
  611.  
  612. /**
  613.  * vmw_cmdbuf_idle - Wait for command buffer manager idle.
  614.  *
  615.  * @man: The command buffer manager.
  616.  * @interruptible: Sleep interruptible while waiting.
  617.  * @timeout: Time out after this many ticks.
  618.  *
  619.  * Wait until the command buffer manager has processed all command buffers,
  620.  * or until a timeout occurs. If a timeout occurs, the function will return
  621.  * -EBUSY.
  622.  */
  623. int vmw_cmdbuf_idle(struct vmw_cmdbuf_man *man, bool interruptible,
  624.                     unsigned long timeout)
  625. {
  626.         int ret;
  627.  
  628.         ret = vmw_cmdbuf_cur_flush(man, interruptible);
  629.         vmw_generic_waiter_add(man->dev_priv,
  630.                                SVGA_IRQFLAG_COMMAND_BUFFER,
  631.                                &man->dev_priv->cmdbuf_waiters);
  632.  
  633.         if (interruptible) {
  634.                 ret = wait_event_interruptible_timeout
  635.                         (man->idle_queue, vmw_cmdbuf_man_idle(man, true),
  636.                          timeout);
  637.         } else {
  638.                 ret = wait_event_timeout
  639.                         (man->idle_queue, vmw_cmdbuf_man_idle(man, true),
  640.                          timeout);
  641.         }
  642.         vmw_generic_waiter_remove(man->dev_priv,
  643.                                   SVGA_IRQFLAG_COMMAND_BUFFER,
  644.                                   &man->dev_priv->cmdbuf_waiters);
  645.         if (ret == 0) {
  646.                 if (!vmw_cmdbuf_man_idle(man, true))
  647.                         ret = -EBUSY;
  648.                 else
  649.                         ret = 0;
  650.         }
  651.         if (ret > 0)
  652.                 ret = 0;
  653.  
  654.         return ret;
  655. }
  656.  
  657. /**
  658.  * vmw_cmdbuf_try_alloc - Try to allocate buffer space from the main pool.
  659.  *
  660.  * @man: The command buffer manager.
  661.  * @info: Allocation info. Will hold the size on entry and allocated mm node
  662.  * on successful return.
  663.  *
  664.  * Try to allocate buffer space from the main pool. Returns true if succeeded.
  665.  * If a fatal error was hit, the error code is returned in @info->ret.
  666.  */
  667. static bool vmw_cmdbuf_try_alloc(struct vmw_cmdbuf_man *man,
  668.                                  struct vmw_cmdbuf_alloc_info *info)
  669. {
  670.         int ret;
  671.  
  672.         if (info->done)
  673.                 return true;
  674.  
  675.         memset(info->node, 0, sizeof(*info->node));
  676.     spin_lock(&man->lock);
  677.         ret = drm_mm_insert_node_generic(&man->mm, info->node, info->page_size,
  678.                                          0, 0,
  679.                                          DRM_MM_SEARCH_DEFAULT,
  680.                                          DRM_MM_CREATE_DEFAULT);
  681.         if (ret) {
  682.                 vmw_cmdbuf_man_process(man);
  683.                 ret = drm_mm_insert_node_generic(&man->mm, info->node,
  684.                                                  info->page_size, 0, 0,
  685.                                                  DRM_MM_SEARCH_DEFAULT,
  686.                                                  DRM_MM_CREATE_DEFAULT);
  687.         }
  688.  
  689.     spin_unlock(&man->lock);
  690.         info->done = !ret;
  691.  
  692.         return info->done;
  693. }
  694.  
  695. /**
  696.  * vmw_cmdbuf_alloc_space - Allocate buffer space from the main pool.
  697.  *
  698.  * @man: The command buffer manager.
  699.  * @node: Pointer to pre-allocated range-manager node.
  700.  * @size: The size of the allocation.
  701.  * @interruptible: Whether to sleep interruptible while waiting for space.
  702.  *
  703.  * This function allocates buffer space from the main pool, and if there is
  704.  * no space available ATM, it turns on IRQ handling and sleeps waiting for it to
  705.  * become available.
  706.  */
  707. static int vmw_cmdbuf_alloc_space(struct vmw_cmdbuf_man *man,
  708.                                   struct drm_mm_node *node,
  709.                                   size_t size,
  710.                                   bool interruptible)
  711. {
  712.         struct vmw_cmdbuf_alloc_info info;
  713.  
  714.         info.page_size = PAGE_ALIGN(size) >> PAGE_SHIFT;
  715.         info.node = node;
  716.         info.done = false;
  717.  
  718.         /*
  719.          * To prevent starvation of large requests, only one allocating call
  720.          * at a time waiting for space.
  721.          */
  722.         if (interruptible) {
  723.                 if (mutex_lock_interruptible(&man->space_mutex))
  724.                         return -ERESTARTSYS;
  725.         } else {
  726.                 mutex_lock(&man->space_mutex);
  727.         }
  728.  
  729.         /* Try to allocate space without waiting. */
  730.         if (vmw_cmdbuf_try_alloc(man, &info))
  731.                 goto out_unlock;
  732.  
  733.         vmw_generic_waiter_add(man->dev_priv,
  734.                                SVGA_IRQFLAG_COMMAND_BUFFER,
  735.                                &man->dev_priv->cmdbuf_waiters);
  736.  
  737.         if (interruptible) {
  738.                 int ret;
  739.  
  740.                 ret = wait_event_interruptible
  741.                         (man->alloc_queue, vmw_cmdbuf_try_alloc(man, &info));
  742.                 if (ret) {
  743.                         vmw_generic_waiter_remove
  744.                                 (man->dev_priv, SVGA_IRQFLAG_COMMAND_BUFFER,
  745.                                  &man->dev_priv->cmdbuf_waiters);
  746.                         mutex_unlock(&man->space_mutex);
  747.                         return ret;
  748.                 }
  749.         } else {
  750.                 wait_event(man->alloc_queue, vmw_cmdbuf_try_alloc(man, &info));
  751.         }
  752.         vmw_generic_waiter_remove(man->dev_priv,
  753.                                   SVGA_IRQFLAG_COMMAND_BUFFER,
  754.                                   &man->dev_priv->cmdbuf_waiters);
  755.  
  756. out_unlock:
  757.         mutex_unlock(&man->space_mutex);
  758.  
  759.         return 0;
  760. }
  761.  
  762. /**
  763.  * vmw_cmdbuf_space_pool - Set up a command buffer header with command buffer
  764.  * space from the main pool.
  765.  *
  766.  * @man: The command buffer manager.
  767.  * @header: Pointer to the header to set up.
  768.  * @size: The requested size of the buffer space.
  769.  * @interruptible: Whether to sleep interruptible while waiting for space.
  770.  */
  771. static int vmw_cmdbuf_space_pool(struct vmw_cmdbuf_man *man,
  772.                                  struct vmw_cmdbuf_header *header,
  773.                                  size_t size,
  774.                                  bool interruptible)
  775. {
  776.         SVGACBHeader *cb_hdr;
  777.         size_t offset;
  778.         int ret;
  779.  
  780.         if (!man->has_pool)
  781.                 return -ENOMEM;
  782.  
  783.         ret = vmw_cmdbuf_alloc_space(man, &header->node,  size, interruptible);
  784.  
  785.         if (ret)
  786.                 return ret;
  787.  
  788.         header->cb_header = dma_pool_alloc(man->headers, GFP_KERNEL,
  789.                                            &header->handle);
  790.         if (!header->cb_header) {
  791.                 ret = -ENOMEM;
  792.                 goto out_no_cb_header;
  793.         }
  794.  
  795.         header->size = header->node.size << PAGE_SHIFT;
  796.         cb_hdr = header->cb_header;
  797.         offset = header->node.start << PAGE_SHIFT;
  798.         header->cmd = man->map + offset;
  799.         memset(cb_hdr, 0, sizeof(*cb_hdr));
  800.         if (man->using_mob) {
  801.                 cb_hdr->flags = SVGA_CB_FLAG_MOB;
  802.                 cb_hdr->ptr.mob.mobid = man->cmd_space->mem.start;
  803.                 cb_hdr->ptr.mob.mobOffset = offset;
  804.         } else {
  805.                 cb_hdr->ptr.pa = (u64)man->handle + (u64)offset;
  806.         }
  807.  
  808.         return 0;
  809.  
  810. out_no_cb_header:
  811.     spin_lock(&man->lock);
  812.         drm_mm_remove_node(&header->node);
  813.     spin_unlock(&man->lock);
  814.  
  815.         return ret;
  816. }
  817.  
  818. /**
  819.  * vmw_cmdbuf_space_inline - Set up a command buffer header with
  820.  * inline command buffer space.
  821.  *
  822.  * @man: The command buffer manager.
  823.  * @header: Pointer to the header to set up.
  824.  * @size: The requested size of the buffer space.
  825.  */
  826. static int vmw_cmdbuf_space_inline(struct vmw_cmdbuf_man *man,
  827.                                    struct vmw_cmdbuf_header *header,
  828.                                    int size)
  829. {
  830.         struct vmw_cmdbuf_dheader *dheader;
  831.         SVGACBHeader *cb_hdr;
  832.  
  833.         if (WARN_ON_ONCE(size > VMW_CMDBUF_INLINE_SIZE))
  834.                 return -ENOMEM;
  835.  
  836.         dheader = dma_pool_alloc(man->dheaders, GFP_KERNEL,
  837.                                  &header->handle);
  838.         if (!dheader)
  839.                 return -ENOMEM;
  840.  
  841.         header->inline_space = true;
  842.         header->size = VMW_CMDBUF_INLINE_SIZE;
  843.         cb_hdr = &dheader->cb_header;
  844.         header->cb_header = cb_hdr;
  845.         header->cmd = dheader->cmd;
  846.         memset(dheader, 0, sizeof(*dheader));
  847.         cb_hdr->status = SVGA_CB_STATUS_NONE;
  848.         cb_hdr->flags = SVGA_CB_FLAG_NONE;
  849.         cb_hdr->ptr.pa = (u64)header->handle +
  850.                 (u64)offsetof(struct vmw_cmdbuf_dheader, cmd);
  851.  
  852.         return 0;
  853. }
  854.  
  855. /**
  856.  * vmw_cmdbuf_alloc - Allocate a command buffer header complete with
  857.  * command buffer space.
  858.  *
  859.  * @man: The command buffer manager.
  860.  * @size: The requested size of the buffer space.
  861.  * @interruptible: Whether to sleep interruptible while waiting for space.
  862.  * @p_header: points to a header pointer to populate on successful return.
  863.  *
  864.  * Returns a pointer to command buffer space if successful. Otherwise
  865.  * returns an error pointer. The header pointer returned in @p_header should
  866.  * be used for upcoming calls to vmw_cmdbuf_reserve() and vmw_cmdbuf_commit().
  867.  */
  868. void *vmw_cmdbuf_alloc(struct vmw_cmdbuf_man *man,
  869.                        size_t size, bool interruptible,
  870.                        struct vmw_cmdbuf_header **p_header)
  871. {
  872.         struct vmw_cmdbuf_header *header;
  873.         int ret = 0;
  874.  
  875.         *p_header = NULL;
  876.  
  877.         header = kzalloc(sizeof(*header), GFP_KERNEL);
  878.         if (!header)
  879.                 return ERR_PTR(-ENOMEM);
  880.  
  881.         if (size <= VMW_CMDBUF_INLINE_SIZE)
  882.                 ret = vmw_cmdbuf_space_inline(man, header, size);
  883.         else
  884.                 ret = vmw_cmdbuf_space_pool(man, header, size, interruptible);
  885.  
  886.         if (ret) {
  887.                 kfree(header);
  888.                 return ERR_PTR(ret);
  889.         }
  890.  
  891.         header->man = man;
  892.         INIT_LIST_HEAD(&header->list);
  893.         header->cb_header->status = SVGA_CB_STATUS_NONE;
  894.         *p_header = header;
  895.  
  896.         return header->cmd;
  897. }
  898.  
  899. /**
  900.  * vmw_cmdbuf_reserve_cur - Reserve space for commands in the current
  901.  * command buffer.
  902.  *
  903.  * @man: The command buffer manager.
  904.  * @size: The requested size of the commands.
  905.  * @ctx_id: The context id if any. Otherwise set to SVGA3D_REG_INVALID.
  906.  * @interruptible: Whether to sleep interruptible while waiting for space.
  907.  *
  908.  * Returns a pointer to command buffer space if successful. Otherwise
  909.  * returns an error pointer.
  910.  */
  911. static void *vmw_cmdbuf_reserve_cur(struct vmw_cmdbuf_man *man,
  912.                                     size_t size,
  913.                                     int ctx_id,
  914.                                     bool interruptible)
  915. {
  916.         struct vmw_cmdbuf_header *cur;
  917.         void *ret;
  918.  
  919.         if (vmw_cmdbuf_cur_lock(man, interruptible))
  920.                 return ERR_PTR(-ERESTARTSYS);
  921.  
  922.         cur = man->cur;
  923.         if (cur && (size + man->cur_pos > cur->size ||
  924.                     ((cur->cb_header->flags & SVGA_CB_FLAG_DX_CONTEXT) &&
  925.                      ctx_id != cur->cb_header->dxContext)))
  926.                 __vmw_cmdbuf_cur_flush(man);
  927.  
  928.         if (!man->cur) {
  929.                 ret = vmw_cmdbuf_alloc(man,
  930.                                        max_t(size_t, size, man->default_size),
  931.                                        interruptible, &man->cur);
  932.                 if (IS_ERR(ret)) {
  933.                         vmw_cmdbuf_cur_unlock(man);
  934.                         return ret;
  935.                 }
  936.  
  937.                 cur = man->cur;
  938.         }
  939.  
  940.         if (ctx_id != SVGA3D_INVALID_ID) {
  941.                 cur->cb_header->flags |= SVGA_CB_FLAG_DX_CONTEXT;
  942.                 cur->cb_header->dxContext = ctx_id;
  943.         }
  944.  
  945.         cur->reserved = size;
  946.  
  947.         return (void *) (man->cur->cmd + man->cur_pos);
  948. }
  949.  
  950. /**
  951.  * vmw_cmdbuf_commit_cur - Commit commands in the current command buffer.
  952.  *
  953.  * @man: The command buffer manager.
  954.  * @size: The size of the commands actually written.
  955.  * @flush: Whether to flush the command buffer immediately.
  956.  */
  957. static void vmw_cmdbuf_commit_cur(struct vmw_cmdbuf_man *man,
  958.                                   size_t size, bool flush)
  959. {
  960.         struct vmw_cmdbuf_header *cur = man->cur;
  961.  
  962.         WARN_ON(!mutex_is_locked(&man->cur_mutex));
  963.  
  964.         WARN_ON(size > cur->reserved);
  965.         man->cur_pos += size;
  966.         if (!size)
  967.                 cur->cb_header->flags &= ~SVGA_CB_FLAG_DX_CONTEXT;
  968.         if (flush)
  969.                 __vmw_cmdbuf_cur_flush(man);
  970.         vmw_cmdbuf_cur_unlock(man);
  971. }
  972.  
  973. /**
  974.  * vmw_cmdbuf_reserve - Reserve space for commands in a command buffer.
  975.  *
  976.  * @man: The command buffer manager.
  977.  * @size: The requested size of the commands.
  978.  * @ctx_id: The context id if any. Otherwise set to SVGA3D_REG_INVALID.
  979.  * @interruptible: Whether to sleep interruptible while waiting for space.
  980.  * @header: Header of the command buffer. NULL if the current command buffer
  981.  * should be used.
  982.  *
  983.  * Returns a pointer to command buffer space if successful. Otherwise
  984.  * returns an error pointer.
  985.  */
  986. void *vmw_cmdbuf_reserve(struct vmw_cmdbuf_man *man, size_t size,
  987.                          int ctx_id, bool interruptible,
  988.                          struct vmw_cmdbuf_header *header)
  989. {
  990.         if (!header)
  991.                 return vmw_cmdbuf_reserve_cur(man, size, ctx_id, interruptible);
  992.  
  993.         if (size > header->size)
  994.                 return ERR_PTR(-EINVAL);
  995.  
  996.         if (ctx_id != SVGA3D_INVALID_ID) {
  997.                 header->cb_header->flags |= SVGA_CB_FLAG_DX_CONTEXT;
  998.                 header->cb_header->dxContext = ctx_id;
  999.         }
  1000.  
  1001.         header->reserved = size;
  1002.         return header->cmd;
  1003. }
  1004.  
  1005. /**
  1006.  * vmw_cmdbuf_commit - Commit commands in a command buffer.
  1007.  *
  1008.  * @man: The command buffer manager.
  1009.  * @size: The size of the commands actually written.
  1010.  * @header: Header of the command buffer. NULL if the current command buffer
  1011.  * should be used.
  1012.  * @flush: Whether to flush the command buffer immediately.
  1013.  */
  1014. void vmw_cmdbuf_commit(struct vmw_cmdbuf_man *man, size_t size,
  1015.                        struct vmw_cmdbuf_header *header, bool flush)
  1016. {
  1017.         if (!header) {
  1018.                 vmw_cmdbuf_commit_cur(man, size, flush);
  1019.                 return;
  1020.         }
  1021.  
  1022.         (void) vmw_cmdbuf_cur_lock(man, false);
  1023.         __vmw_cmdbuf_cur_flush(man);
  1024.         WARN_ON(size > header->reserved);
  1025.         man->cur = header;
  1026.         man->cur_pos = size;
  1027.         if (!size)
  1028.                 header->cb_header->flags &= ~SVGA_CB_FLAG_DX_CONTEXT;
  1029.         if (flush)
  1030.                 __vmw_cmdbuf_cur_flush(man);
  1031.         vmw_cmdbuf_cur_unlock(man);
  1032. }
  1033.  
  1034. /**
  1035.  * vmw_cmdbuf_tasklet_schedule - Schedule the interrupt handler bottom half.
  1036.  *
  1037.  * @man: The command buffer manager.
  1038.  */
  1039. void vmw_cmdbuf_tasklet_schedule(struct vmw_cmdbuf_man *man)
  1040. {
  1041.         if (!man)
  1042.                 return;
  1043.  
  1044. //   tasklet_schedule(&man->tasklet);
  1045. }
  1046.  
  1047. /**
  1048.  * vmw_cmdbuf_send_device_command - Send a command through the device context.
  1049.  *
  1050.  * @man: The command buffer manager.
  1051.  * @command: Pointer to the command to send.
  1052.  * @size: Size of the command.
  1053.  *
  1054.  * Synchronously sends a device context command.
  1055.  */
  1056. static int vmw_cmdbuf_send_device_command(struct vmw_cmdbuf_man *man,
  1057.                                           const void *command,
  1058.                                           size_t size)
  1059. {
  1060.         struct vmw_cmdbuf_header *header;
  1061.         int status;
  1062.         void *cmd = vmw_cmdbuf_alloc(man, size, false, &header);
  1063.  
  1064.         if (IS_ERR(cmd))
  1065.                 return PTR_ERR(cmd);
  1066.  
  1067.         memcpy(cmd, command, size);
  1068.         header->cb_header->length = size;
  1069.         header->cb_context = SVGA_CB_CONTEXT_DEVICE;
  1070.     spin_lock(&man->lock);
  1071.         status = vmw_cmdbuf_header_submit(header);
  1072.     spin_unlock(&man->lock);
  1073.         vmw_cmdbuf_header_free(header);
  1074.  
  1075.         if (status != SVGA_CB_STATUS_COMPLETED) {
  1076.                 DRM_ERROR("Device context command failed with status %d\n",
  1077.                           status);
  1078.                 return -EINVAL;
  1079.         }
  1080.  
  1081.         return 0;
  1082. }
  1083.  
  1084. /**
  1085.  * vmw_cmdbuf_startstop - Send a start / stop command through the device
  1086.  * context.
  1087.  *
  1088.  * @man: The command buffer manager.
  1089.  * @enable: Whether to enable or disable the context.
  1090.  *
  1091.  * Synchronously sends a device start / stop context command.
  1092.  */
  1093. static int vmw_cmdbuf_startstop(struct vmw_cmdbuf_man *man,
  1094.                                 bool enable)
  1095. {
  1096.         struct {
  1097.                 uint32 id;
  1098.                 SVGADCCmdStartStop body;
  1099.         } __packed cmd;
  1100.  
  1101.         cmd.id = SVGA_DC_CMD_START_STOP_CONTEXT;
  1102.         cmd.body.enable = (enable) ? 1 : 0;
  1103.         cmd.body.context = SVGA_CB_CONTEXT_0;
  1104.  
  1105.         return vmw_cmdbuf_send_device_command(man, &cmd, sizeof(cmd));
  1106. }
  1107.  
  1108. /**
  1109.  * vmw_cmdbuf_set_pool_size - Set command buffer manager sizes
  1110.  *
  1111.  * @man: The command buffer manager.
  1112.  * @size: The size of the main space pool.
  1113.  * @default_size: The default size of the command buffer for small kernel
  1114.  * submissions.
  1115.  *
  1116.  * Set the size and allocate the main command buffer space pool,
  1117.  * as well as the default size of the command buffer for
  1118.  * small kernel submissions. If successful, this enables large command
  1119.  * submissions. Note that this function requires that rudimentary command
  1120.  * submission is already available and that the MOB memory manager is alive.
  1121.  * Returns 0 on success. Negative error code on failure.
  1122.  */
  1123. int vmw_cmdbuf_set_pool_size(struct vmw_cmdbuf_man *man,
  1124.                              size_t size, size_t default_size)
  1125. {
  1126.         struct vmw_private *dev_priv = man->dev_priv;
  1127.         bool dummy;
  1128.         int ret;
  1129.  
  1130.         if (man->has_pool)
  1131.                 return -EINVAL;
  1132.  
  1133.         /* First, try to allocate a huge chunk of DMA memory */
  1134.         size = PAGE_ALIGN(size);
  1135.         man->map = dma_alloc_coherent(&dev_priv->dev->pdev->dev, size,
  1136.                                       &man->handle, GFP_KERNEL);
  1137.         if (man->map) {
  1138.                 man->using_mob = false;
  1139.         } else {
  1140.                 /*
  1141.                  * DMA memory failed. If we can have command buffers in a
  1142.                  * MOB, try to use that instead. Note that this will
  1143.                  * actually call into the already enabled manager, when
  1144.                  * binding the MOB.
  1145.                  */
  1146.                 if (!(dev_priv->capabilities & SVGA_CAP_DX))
  1147.                         return -ENOMEM;
  1148.  
  1149.                 ret = ttm_bo_create(&dev_priv->bdev, size, ttm_bo_type_device,
  1150.                                     &vmw_mob_ne_placement, 0, false, NULL,
  1151.                                     &man->cmd_space);
  1152.                 if (ret)
  1153.                         return ret;
  1154.  
  1155.                 man->using_mob = true;
  1156.                 ret = ttm_bo_kmap(man->cmd_space, 0, size >> PAGE_SHIFT,
  1157.                                   &man->map_obj);
  1158.                 if (ret)
  1159.                         goto out_no_map;
  1160.  
  1161.                 man->map = ttm_kmap_obj_virtual(&man->map_obj, &dummy);
  1162.         }
  1163.  
  1164.         man->size = size;
  1165.         drm_mm_init(&man->mm, 0, size >> PAGE_SHIFT);
  1166.  
  1167.         man->has_pool = true;
  1168.  
  1169.         /*
  1170.          * For now, set the default size to VMW_CMDBUF_INLINE_SIZE to
  1171.          * prevent deadlocks from happening when vmw_cmdbuf_space_pool()
  1172.          * needs to wait for space and we block on further command
  1173.          * submissions to be able to free up space.
  1174.          */
  1175.         man->default_size = VMW_CMDBUF_INLINE_SIZE;
  1176.         DRM_INFO("Using command buffers with %s pool.\n",
  1177.                  (man->using_mob) ? "MOB" : "DMA");
  1178.  
  1179.         return 0;
  1180.  
  1181. out_no_map:
  1182.         if (man->using_mob)
  1183.                 ttm_bo_unref(&man->cmd_space);
  1184.  
  1185.         return ret;
  1186. }
  1187.  
  1188. /**
  1189.  * vmw_cmdbuf_man_create: Create a command buffer manager and enable it for
  1190.  * inline command buffer submissions only.
  1191.  *
  1192.  * @dev_priv: Pointer to device private structure.
  1193.  *
  1194.  * Returns a pointer to a cummand buffer manager to success or error pointer
  1195.  * on failure. The command buffer manager will be enabled for submissions of
  1196.  * size VMW_CMDBUF_INLINE_SIZE only.
  1197.  */
  1198. struct vmw_cmdbuf_man *vmw_cmdbuf_man_create(struct vmw_private *dev_priv)
  1199. {
  1200.         struct vmw_cmdbuf_man *man;
  1201.         struct vmw_cmdbuf_context *ctx;
  1202.         int i;
  1203.         int ret;
  1204.  
  1205.         if (!(dev_priv->capabilities & SVGA_CAP_COMMAND_BUFFERS))
  1206.                 return ERR_PTR(-ENOSYS);
  1207.  
  1208.         man = kzalloc(sizeof(*man), GFP_KERNEL);
  1209.         if (!man)
  1210.                 return ERR_PTR(-ENOMEM);
  1211.  
  1212.         man->headers = dma_pool_create("vmwgfx cmdbuf",
  1213.                                        &dev_priv->dev->pdev->dev,
  1214.                                        sizeof(SVGACBHeader),
  1215.                                        64, PAGE_SIZE);
  1216.         if (!man->headers) {
  1217.                 ret = -ENOMEM;
  1218.                 goto out_no_pool;
  1219.         }
  1220.  
  1221.         man->dheaders = dma_pool_create("vmwgfx inline cmdbuf",
  1222.                                         &dev_priv->dev->pdev->dev,
  1223.                                         sizeof(struct vmw_cmdbuf_dheader),
  1224.                                         64, PAGE_SIZE);
  1225.         if (!man->dheaders) {
  1226.                 ret = -ENOMEM;
  1227.                 goto out_no_dpool;
  1228.         }
  1229.  
  1230.         for_each_cmdbuf_ctx(man, i, ctx)
  1231.                 vmw_cmdbuf_ctx_init(ctx);
  1232.  
  1233.         INIT_LIST_HEAD(&man->error);
  1234.         spin_lock_init(&man->lock);
  1235.         mutex_init(&man->cur_mutex);
  1236.         mutex_init(&man->space_mutex);
  1237. //   tasklet_init(&man->tasklet, vmw_cmdbuf_man_tasklet,
  1238. //            (unsigned long) man);
  1239.         man->default_size = VMW_CMDBUF_INLINE_SIZE;
  1240.         init_waitqueue_head(&man->alloc_queue);
  1241.         init_waitqueue_head(&man->idle_queue);
  1242.         man->dev_priv = dev_priv;
  1243.         man->max_hw_submitted = SVGA_CB_MAX_QUEUED_PER_CONTEXT - 1;
  1244.         INIT_WORK(&man->work, &vmw_cmdbuf_work_func);
  1245.         vmw_generic_waiter_add(dev_priv, SVGA_IRQFLAG_ERROR,
  1246.                                &dev_priv->error_waiters);
  1247.         ret = vmw_cmdbuf_startstop(man, true);
  1248.         if (ret) {
  1249.                 DRM_ERROR("Failed starting command buffer context 0.\n");
  1250.                 vmw_cmdbuf_man_destroy(man);
  1251.                 return ERR_PTR(ret);
  1252.         }
  1253.  
  1254.         return man;
  1255.  
  1256. out_no_dpool:
  1257.         dma_pool_destroy(man->headers);
  1258. out_no_pool:
  1259.         kfree(man);
  1260.  
  1261.         return ERR_PTR(ret);
  1262. }
  1263.  
  1264. /**
  1265.  * vmw_cmdbuf_remove_pool - Take down the main buffer space pool.
  1266.  *
  1267.  * @man: Pointer to a command buffer manager.
  1268.  *
  1269.  * This function removes the main buffer space pool, and should be called
  1270.  * before MOB memory management is removed. When this function has been called,
  1271.  * only small command buffer submissions of size VMW_CMDBUF_INLINE_SIZE or
  1272.  * less are allowed, and the default size of the command buffer for small kernel
  1273.  * submissions is also set to this size.
  1274.  */
  1275. void vmw_cmdbuf_remove_pool(struct vmw_cmdbuf_man *man)
  1276. {
  1277.         if (!man->has_pool)
  1278.                 return;
  1279.  
  1280.         man->has_pool = false;
  1281.         man->default_size = VMW_CMDBUF_INLINE_SIZE;
  1282.         (void) vmw_cmdbuf_idle(man, false, 10*HZ);
  1283.         if (man->using_mob) {
  1284.                 (void) ttm_bo_kunmap(&man->map_obj);
  1285.                 ttm_bo_unref(&man->cmd_space);
  1286.         } else {
  1287. //       dma_free_coherent(&man->dev_priv->dev->pdev->dev,
  1288. //                 man->size, man->map, man->handle);
  1289.         }
  1290. }
  1291.  
  1292. /**
  1293.  * vmw_cmdbuf_man_destroy - Take down a command buffer manager.
  1294.  *
  1295.  * @man: Pointer to a command buffer manager.
  1296.  *
  1297.  * This function idles and then destroys a command buffer manager.
  1298.  */
  1299. void vmw_cmdbuf_man_destroy(struct vmw_cmdbuf_man *man)
  1300. {
  1301.         WARN_ON_ONCE(man->has_pool);
  1302.         (void) vmw_cmdbuf_idle(man, false, 10*HZ);
  1303.         if (vmw_cmdbuf_startstop(man, false))
  1304.                 DRM_ERROR("Failed stopping command buffer context 0.\n");
  1305.  
  1306.         vmw_generic_waiter_remove(man->dev_priv, SVGA_IRQFLAG_ERROR,
  1307.                                   &man->dev_priv->error_waiters);
  1308. //   tasklet_kill(&man->tasklet);
  1309. //   (void) cancel_work_sync(&man->work);
  1310.         dma_pool_destroy(man->dheaders);
  1311.         dma_pool_destroy(man->headers);
  1312.         mutex_destroy(&man->cur_mutex);
  1313.         mutex_destroy(&man->space_mutex);
  1314.         kfree(man);
  1315. }
  1316.