1dff96888SDirk Hohndel (VMware) // SPDX-License-Identifier: GPL-2.0 OR MIT 23eab3d9eSThomas Hellstrom /************************************************************************** 33eab3d9eSThomas Hellstrom * 4dff96888SDirk Hohndel (VMware) * Copyright 2015 VMware, Inc., Palo Alto, CA., USA 53eab3d9eSThomas Hellstrom * 63eab3d9eSThomas Hellstrom * Permission is hereby granted, free of charge, to any person obtaining a 73eab3d9eSThomas Hellstrom * copy of this software and associated documentation files (the 83eab3d9eSThomas Hellstrom * "Software"), to deal in the Software without restriction, including 93eab3d9eSThomas Hellstrom * without limitation the rights to use, copy, modify, merge, publish, 103eab3d9eSThomas Hellstrom * distribute, sub license, and/or sell copies of the Software, and to 113eab3d9eSThomas Hellstrom * permit persons to whom the Software is furnished to do so, subject to 123eab3d9eSThomas Hellstrom * the following conditions: 133eab3d9eSThomas Hellstrom * 143eab3d9eSThomas Hellstrom * The above copyright notice and this permission notice (including the 153eab3d9eSThomas Hellstrom * next paragraph) shall be included in all copies or substantial portions 163eab3d9eSThomas Hellstrom * of the Software. 173eab3d9eSThomas Hellstrom * 183eab3d9eSThomas Hellstrom * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 193eab3d9eSThomas Hellstrom * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 203eab3d9eSThomas Hellstrom * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 213eab3d9eSThomas Hellstrom * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 223eab3d9eSThomas Hellstrom * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 233eab3d9eSThomas Hellstrom * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 243eab3d9eSThomas Hellstrom * USE OR OTHER DEALINGS IN THE SOFTWARE. 253eab3d9eSThomas Hellstrom * 263eab3d9eSThomas Hellstrom **************************************************************************/ 273eab3d9eSThomas Hellstrom 28d5c1f011SSam Ravnborg #include <linux/dmapool.h> 29d5c1f011SSam Ravnborg #include <linux/pci.h> 30d5c1f011SSam Ravnborg 31008be682SMasahiro Yamada #include <drm/ttm/ttm_bo_api.h> 32008be682SMasahiro Yamada 333eab3d9eSThomas Hellstrom #include "vmwgfx_drv.h" 343eab3d9eSThomas Hellstrom 353eab3d9eSThomas Hellstrom /* 363eab3d9eSThomas Hellstrom * Size of inline command buffers. Try to make sure that a page size is a 373eab3d9eSThomas Hellstrom * multiple of the DMA pool allocation size. 383eab3d9eSThomas Hellstrom */ 393eab3d9eSThomas Hellstrom #define VMW_CMDBUF_INLINE_ALIGN 64 409b590783SThomas Hellstrom #define VMW_CMDBUF_INLINE_SIZE \ 419b590783SThomas Hellstrom (1024 - ALIGN(sizeof(SVGACBHeader), VMW_CMDBUF_INLINE_ALIGN)) 423eab3d9eSThomas Hellstrom 433eab3d9eSThomas Hellstrom /** 443eab3d9eSThomas Hellstrom * struct vmw_cmdbuf_context - Command buffer context queues 453eab3d9eSThomas Hellstrom * 463eab3d9eSThomas Hellstrom * @submitted: List of command buffers that have been submitted to the 473eab3d9eSThomas Hellstrom * manager but not yet submitted to hardware. 483eab3d9eSThomas Hellstrom * @hw_submitted: List of command buffers submitted to hardware. 493eab3d9eSThomas Hellstrom * @preempted: List of preempted command buffers. 503eab3d9eSThomas Hellstrom * @num_hw_submitted: Number of buffers currently being processed by hardware 513eab3d9eSThomas Hellstrom */ 523eab3d9eSThomas Hellstrom struct vmw_cmdbuf_context { 533eab3d9eSThomas Hellstrom struct list_head submitted; 543eab3d9eSThomas Hellstrom struct list_head hw_submitted; 553eab3d9eSThomas Hellstrom struct list_head preempted; 563eab3d9eSThomas Hellstrom unsigned num_hw_submitted; 5765b97a2bSThomas Hellstrom bool block_submission; 583eab3d9eSThomas Hellstrom }; 593eab3d9eSThomas Hellstrom 603eab3d9eSThomas Hellstrom /** 613eab3d9eSThomas Hellstrom * struct vmw_cmdbuf_man: - Command buffer manager 623eab3d9eSThomas Hellstrom * 633eab3d9eSThomas Hellstrom * @cur_mutex: Mutex protecting the command buffer used for incremental small 643eab3d9eSThomas Hellstrom * kernel command submissions, @cur. 653eab3d9eSThomas Hellstrom * @space_mutex: Mutex to protect against starvation when we allocate 663eab3d9eSThomas Hellstrom * main pool buffer space. 6765b97a2bSThomas Hellstrom * @error_mutex: Mutex to serialize the work queue error handling. 6865b97a2bSThomas Hellstrom * Note this is not needed if the same workqueue handler 6965b97a2bSThomas Hellstrom * can't race with itself... 703eab3d9eSThomas Hellstrom * @work: A struct work_struct implementeing command buffer error handling. 713eab3d9eSThomas Hellstrom * Immutable. 723eab3d9eSThomas Hellstrom * @dev_priv: Pointer to the device private struct. Immutable. 733eab3d9eSThomas Hellstrom * @ctx: Array of command buffer context queues. The queues and the context 743eab3d9eSThomas Hellstrom * data is protected by @lock. 753eab3d9eSThomas Hellstrom * @error: List of command buffers that have caused device errors. 763eab3d9eSThomas Hellstrom * Protected by @lock. 773eab3d9eSThomas Hellstrom * @mm: Range manager for the command buffer space. Manager allocations and 783eab3d9eSThomas Hellstrom * frees are protected by @lock. 793eab3d9eSThomas Hellstrom * @cmd_space: Buffer object for the command buffer space, unless we were 803eab3d9eSThomas Hellstrom * able to make a contigous coherent DMA memory allocation, @handle. Immutable. 813eab3d9eSThomas Hellstrom * @map_obj: Mapping state for @cmd_space. Immutable. 823eab3d9eSThomas Hellstrom * @map: Pointer to command buffer space. May be a mapped buffer object or 833eab3d9eSThomas Hellstrom * a contigous coherent DMA memory allocation. Immutable. 843eab3d9eSThomas Hellstrom * @cur: Command buffer for small kernel command submissions. Protected by 853eab3d9eSThomas Hellstrom * the @cur_mutex. 863eab3d9eSThomas Hellstrom * @cur_pos: Space already used in @cur. Protected by @cur_mutex. 873eab3d9eSThomas Hellstrom * @default_size: Default size for the @cur command buffer. Immutable. 883eab3d9eSThomas Hellstrom * @max_hw_submitted: Max number of in-flight command buffers the device can 893eab3d9eSThomas Hellstrom * handle. Immutable. 903eab3d9eSThomas Hellstrom * @lock: Spinlock protecting command submission queues. 913eab3d9eSThomas Hellstrom * @header: Pool of DMA memory for device command buffer headers. 923eab3d9eSThomas Hellstrom * Internal protection. 933eab3d9eSThomas Hellstrom * @dheaders: Pool of DMA memory for device command buffer headers with trailing 943eab3d9eSThomas Hellstrom * space for inline data. Internal protection. 953eab3d9eSThomas Hellstrom * @alloc_queue: Wait queue for processes waiting to allocate command buffer 963eab3d9eSThomas Hellstrom * space. 973eab3d9eSThomas Hellstrom * @idle_queue: Wait queue for processes waiting for command buffer idle. 983eab3d9eSThomas Hellstrom * @irq_on: Whether the process function has requested irq to be turned on. 993eab3d9eSThomas Hellstrom * Protected by @lock. 1003eab3d9eSThomas Hellstrom * @using_mob: Whether the command buffer space is a MOB or a contigous DMA 1013eab3d9eSThomas Hellstrom * allocation. Immutable. 1023eab3d9eSThomas Hellstrom * @has_pool: Has a large pool of DMA memory which allows larger allocations. 1033eab3d9eSThomas Hellstrom * Typically this is false only during bootstrap. 1043eab3d9eSThomas Hellstrom * @handle: DMA address handle for the command buffer space if @using_mob is 1053eab3d9eSThomas Hellstrom * false. Immutable. 1063eab3d9eSThomas Hellstrom * @size: The size of the command buffer space. Immutable. 107dc366364SThomas Hellstrom * @num_contexts: Number of contexts actually enabled. 1083eab3d9eSThomas Hellstrom */ 1093eab3d9eSThomas Hellstrom struct vmw_cmdbuf_man { 1103eab3d9eSThomas Hellstrom struct mutex cur_mutex; 1113eab3d9eSThomas Hellstrom struct mutex space_mutex; 11265b97a2bSThomas Hellstrom struct mutex error_mutex; 1133eab3d9eSThomas Hellstrom struct work_struct work; 1143eab3d9eSThomas Hellstrom struct vmw_private *dev_priv; 1153eab3d9eSThomas Hellstrom struct vmw_cmdbuf_context ctx[SVGA_CB_CONTEXT_MAX]; 1163eab3d9eSThomas Hellstrom struct list_head error; 1173eab3d9eSThomas Hellstrom struct drm_mm mm; 1183eab3d9eSThomas Hellstrom struct ttm_buffer_object *cmd_space; 1193eab3d9eSThomas Hellstrom struct ttm_bo_kmap_obj map_obj; 1203eab3d9eSThomas Hellstrom u8 *map; 1213eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header *cur; 1223eab3d9eSThomas Hellstrom size_t cur_pos; 1233eab3d9eSThomas Hellstrom size_t default_size; 1243eab3d9eSThomas Hellstrom unsigned max_hw_submitted; 1253eab3d9eSThomas Hellstrom spinlock_t lock; 1263eab3d9eSThomas Hellstrom struct dma_pool *headers; 1273eab3d9eSThomas Hellstrom struct dma_pool *dheaders; 1283eab3d9eSThomas Hellstrom wait_queue_head_t alloc_queue; 1293eab3d9eSThomas Hellstrom wait_queue_head_t idle_queue; 1303eab3d9eSThomas Hellstrom bool irq_on; 1313eab3d9eSThomas Hellstrom bool using_mob; 1323eab3d9eSThomas Hellstrom bool has_pool; 1333eab3d9eSThomas Hellstrom dma_addr_t handle; 1343eab3d9eSThomas Hellstrom size_t size; 135dc366364SThomas Hellstrom u32 num_contexts; 1363eab3d9eSThomas Hellstrom }; 1373eab3d9eSThomas Hellstrom 1383eab3d9eSThomas Hellstrom /** 1393eab3d9eSThomas Hellstrom * struct vmw_cmdbuf_header - Command buffer metadata 1403eab3d9eSThomas Hellstrom * 1413eab3d9eSThomas Hellstrom * @man: The command buffer manager. 1423eab3d9eSThomas Hellstrom * @cb_header: Device command buffer header, allocated from a DMA pool. 1433eab3d9eSThomas Hellstrom * @cb_context: The device command buffer context. 1443eab3d9eSThomas Hellstrom * @list: List head for attaching to the manager lists. 1453eab3d9eSThomas Hellstrom * @node: The range manager node. 1463eab3d9eSThomas Hellstrom * @handle. The DMA address of @cb_header. Handed to the device on command 1473eab3d9eSThomas Hellstrom * buffer submission. 1483eab3d9eSThomas Hellstrom * @cmd: Pointer to the command buffer space of this buffer. 1493eab3d9eSThomas Hellstrom * @size: Size of the command buffer space of this buffer. 1503eab3d9eSThomas Hellstrom * @reserved: Reserved space of this buffer. 1513eab3d9eSThomas Hellstrom * @inline_space: Whether inline command buffer space is used. 1523eab3d9eSThomas Hellstrom */ 1533eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header { 1543eab3d9eSThomas Hellstrom struct vmw_cmdbuf_man *man; 1553eab3d9eSThomas Hellstrom SVGACBHeader *cb_header; 1563eab3d9eSThomas Hellstrom SVGACBContext cb_context; 1573eab3d9eSThomas Hellstrom struct list_head list; 1589b590783SThomas Hellstrom struct drm_mm_node node; 1593eab3d9eSThomas Hellstrom dma_addr_t handle; 1603eab3d9eSThomas Hellstrom u8 *cmd; 1613eab3d9eSThomas Hellstrom size_t size; 1623eab3d9eSThomas Hellstrom size_t reserved; 1633eab3d9eSThomas Hellstrom bool inline_space; 1643eab3d9eSThomas Hellstrom }; 1653eab3d9eSThomas Hellstrom 1663eab3d9eSThomas Hellstrom /** 1673eab3d9eSThomas Hellstrom * struct vmw_cmdbuf_dheader - Device command buffer header with inline 1683eab3d9eSThomas Hellstrom * command buffer space. 1693eab3d9eSThomas Hellstrom * 1703eab3d9eSThomas Hellstrom * @cb_header: Device command buffer header. 1713eab3d9eSThomas Hellstrom * @cmd: Inline command buffer space. 1723eab3d9eSThomas Hellstrom */ 1733eab3d9eSThomas Hellstrom struct vmw_cmdbuf_dheader { 1743eab3d9eSThomas Hellstrom SVGACBHeader cb_header; 1753eab3d9eSThomas Hellstrom u8 cmd[VMW_CMDBUF_INLINE_SIZE] __aligned(VMW_CMDBUF_INLINE_ALIGN); 1763eab3d9eSThomas Hellstrom }; 1773eab3d9eSThomas Hellstrom 1783eab3d9eSThomas Hellstrom /** 1793eab3d9eSThomas Hellstrom * struct vmw_cmdbuf_alloc_info - Command buffer space allocation metadata 1803eab3d9eSThomas Hellstrom * 1813eab3d9eSThomas Hellstrom * @page_size: Size of requested command buffer space in pages. 1829b590783SThomas Hellstrom * @node: Pointer to the range manager node. 1839b590783SThomas Hellstrom * @done: True if this allocation has succeeded. 1843eab3d9eSThomas Hellstrom */ 1853eab3d9eSThomas Hellstrom struct vmw_cmdbuf_alloc_info { 1863eab3d9eSThomas Hellstrom size_t page_size; 1873eab3d9eSThomas Hellstrom struct drm_mm_node *node; 1889b590783SThomas Hellstrom bool done; 1893eab3d9eSThomas Hellstrom }; 1903eab3d9eSThomas Hellstrom 1913eab3d9eSThomas Hellstrom /* Loop over each context in the command buffer manager. */ 1923eab3d9eSThomas Hellstrom #define for_each_cmdbuf_ctx(_man, _i, _ctx) \ 193dc366364SThomas Hellstrom for (_i = 0, _ctx = &(_man)->ctx[0]; (_i) < (_man)->num_contexts; \ 1943eab3d9eSThomas Hellstrom ++(_i), ++(_ctx)) 1953eab3d9eSThomas Hellstrom 19665b97a2bSThomas Hellstrom static int vmw_cmdbuf_startstop(struct vmw_cmdbuf_man *man, u32 context, 19765b97a2bSThomas Hellstrom bool enable); 19865b97a2bSThomas Hellstrom static int vmw_cmdbuf_preempt(struct vmw_cmdbuf_man *man, u32 context); 1993eab3d9eSThomas Hellstrom 2003eab3d9eSThomas Hellstrom /** 2013eab3d9eSThomas Hellstrom * vmw_cmdbuf_cur_lock - Helper to lock the cur_mutex. 2023eab3d9eSThomas Hellstrom * 2033eab3d9eSThomas Hellstrom * @man: The range manager. 2043eab3d9eSThomas Hellstrom * @interruptible: Whether to wait interruptible when locking. 2053eab3d9eSThomas Hellstrom */ 2063eab3d9eSThomas Hellstrom static int vmw_cmdbuf_cur_lock(struct vmw_cmdbuf_man *man, bool interruptible) 2073eab3d9eSThomas Hellstrom { 2083eab3d9eSThomas Hellstrom if (interruptible) { 2093eab3d9eSThomas Hellstrom if (mutex_lock_interruptible(&man->cur_mutex)) 2103eab3d9eSThomas Hellstrom return -ERESTARTSYS; 2113eab3d9eSThomas Hellstrom } else { 2123eab3d9eSThomas Hellstrom mutex_lock(&man->cur_mutex); 2133eab3d9eSThomas Hellstrom } 2143eab3d9eSThomas Hellstrom 2153eab3d9eSThomas Hellstrom return 0; 2163eab3d9eSThomas Hellstrom } 2173eab3d9eSThomas Hellstrom 2183eab3d9eSThomas Hellstrom /** 2193eab3d9eSThomas Hellstrom * vmw_cmdbuf_cur_unlock - Helper to unlock the cur_mutex. 2203eab3d9eSThomas Hellstrom * 2213eab3d9eSThomas Hellstrom * @man: The range manager. 2223eab3d9eSThomas Hellstrom */ 2233eab3d9eSThomas Hellstrom static void vmw_cmdbuf_cur_unlock(struct vmw_cmdbuf_man *man) 2243eab3d9eSThomas Hellstrom { 2253eab3d9eSThomas Hellstrom mutex_unlock(&man->cur_mutex); 2263eab3d9eSThomas Hellstrom } 2273eab3d9eSThomas Hellstrom 2283eab3d9eSThomas Hellstrom /** 2293eab3d9eSThomas Hellstrom * vmw_cmdbuf_header_inline_free - Free a struct vmw_cmdbuf_header that has 2303eab3d9eSThomas Hellstrom * been used for the device context with inline command buffers. 2313eab3d9eSThomas Hellstrom * Need not be called locked. 2323eab3d9eSThomas Hellstrom * 2333eab3d9eSThomas Hellstrom * @header: Pointer to the header to free. 2343eab3d9eSThomas Hellstrom */ 2353eab3d9eSThomas Hellstrom static void vmw_cmdbuf_header_inline_free(struct vmw_cmdbuf_header *header) 2363eab3d9eSThomas Hellstrom { 2373eab3d9eSThomas Hellstrom struct vmw_cmdbuf_dheader *dheader; 2383eab3d9eSThomas Hellstrom 2393eab3d9eSThomas Hellstrom if (WARN_ON_ONCE(!header->inline_space)) 2403eab3d9eSThomas Hellstrom return; 2413eab3d9eSThomas Hellstrom 2423eab3d9eSThomas Hellstrom dheader = container_of(header->cb_header, struct vmw_cmdbuf_dheader, 2433eab3d9eSThomas Hellstrom cb_header); 2443eab3d9eSThomas Hellstrom dma_pool_free(header->man->dheaders, dheader, header->handle); 2453eab3d9eSThomas Hellstrom kfree(header); 2463eab3d9eSThomas Hellstrom } 2473eab3d9eSThomas Hellstrom 2483eab3d9eSThomas Hellstrom /** 2493eab3d9eSThomas Hellstrom * __vmw_cmdbuf_header_free - Free a struct vmw_cmdbuf_header and its 2503eab3d9eSThomas Hellstrom * associated structures. 2513eab3d9eSThomas Hellstrom * 2523eab3d9eSThomas Hellstrom * header: Pointer to the header to free. 2533eab3d9eSThomas Hellstrom * 2543eab3d9eSThomas Hellstrom * For internal use. Must be called with man::lock held. 2553eab3d9eSThomas Hellstrom */ 2563eab3d9eSThomas Hellstrom static void __vmw_cmdbuf_header_free(struct vmw_cmdbuf_header *header) 2573eab3d9eSThomas Hellstrom { 2583eab3d9eSThomas Hellstrom struct vmw_cmdbuf_man *man = header->man; 2593eab3d9eSThomas Hellstrom 260fb89ac51SThomas Hellstrom lockdep_assert_held_once(&man->lock); 2613eab3d9eSThomas Hellstrom 2623eab3d9eSThomas Hellstrom if (header->inline_space) { 2633eab3d9eSThomas Hellstrom vmw_cmdbuf_header_inline_free(header); 2643eab3d9eSThomas Hellstrom return; 2653eab3d9eSThomas Hellstrom } 2663eab3d9eSThomas Hellstrom 2679b590783SThomas Hellstrom drm_mm_remove_node(&header->node); 2683eab3d9eSThomas Hellstrom wake_up_all(&man->alloc_queue); 2693eab3d9eSThomas Hellstrom if (header->cb_header) 2703eab3d9eSThomas Hellstrom dma_pool_free(man->headers, header->cb_header, 2713eab3d9eSThomas Hellstrom header->handle); 2723eab3d9eSThomas Hellstrom kfree(header); 2733eab3d9eSThomas Hellstrom } 2743eab3d9eSThomas Hellstrom 2753eab3d9eSThomas Hellstrom /** 2763eab3d9eSThomas Hellstrom * vmw_cmdbuf_header_free - Free a struct vmw_cmdbuf_header and its 2773eab3d9eSThomas Hellstrom * associated structures. 2783eab3d9eSThomas Hellstrom * 2793eab3d9eSThomas Hellstrom * @header: Pointer to the header to free. 2803eab3d9eSThomas Hellstrom */ 2813eab3d9eSThomas Hellstrom void vmw_cmdbuf_header_free(struct vmw_cmdbuf_header *header) 2823eab3d9eSThomas Hellstrom { 2833eab3d9eSThomas Hellstrom struct vmw_cmdbuf_man *man = header->man; 2843eab3d9eSThomas Hellstrom 2853eab3d9eSThomas Hellstrom /* Avoid locking if inline_space */ 2863eab3d9eSThomas Hellstrom if (header->inline_space) { 2873eab3d9eSThomas Hellstrom vmw_cmdbuf_header_inline_free(header); 2883eab3d9eSThomas Hellstrom return; 2893eab3d9eSThomas Hellstrom } 290ef369904SThomas Hellstrom spin_lock(&man->lock); 2913eab3d9eSThomas Hellstrom __vmw_cmdbuf_header_free(header); 292ef369904SThomas Hellstrom spin_unlock(&man->lock); 2933eab3d9eSThomas Hellstrom } 2943eab3d9eSThomas Hellstrom 2953eab3d9eSThomas Hellstrom 2963eab3d9eSThomas Hellstrom /** 2973eab3d9eSThomas Hellstrom * vmw_cmbuf_header_submit: Submit a command buffer to hardware. 2983eab3d9eSThomas Hellstrom * 2993eab3d9eSThomas Hellstrom * @header: The header of the buffer to submit. 3003eab3d9eSThomas Hellstrom */ 3013eab3d9eSThomas Hellstrom static int vmw_cmdbuf_header_submit(struct vmw_cmdbuf_header *header) 3023eab3d9eSThomas Hellstrom { 3033eab3d9eSThomas Hellstrom struct vmw_cmdbuf_man *man = header->man; 3043eab3d9eSThomas Hellstrom u32 val; 3053eab3d9eSThomas Hellstrom 3060e7c875dSPaul Bolle val = upper_32_bits(header->handle); 3073eab3d9eSThomas Hellstrom vmw_write(man->dev_priv, SVGA_REG_COMMAND_HIGH, val); 3082e3cc8cfSThomas Hellstrom 3090e7c875dSPaul Bolle val = lower_32_bits(header->handle); 3103eab3d9eSThomas Hellstrom val |= header->cb_context & SVGA_CB_CONTEXT_MASK; 3113eab3d9eSThomas Hellstrom vmw_write(man->dev_priv, SVGA_REG_COMMAND_LOW, val); 3123eab3d9eSThomas Hellstrom 3133eab3d9eSThomas Hellstrom return header->cb_header->status; 3143eab3d9eSThomas Hellstrom } 3153eab3d9eSThomas Hellstrom 3163eab3d9eSThomas Hellstrom /** 3173eab3d9eSThomas Hellstrom * vmw_cmdbuf_ctx_init: Initialize a command buffer context. 3183eab3d9eSThomas Hellstrom * 3193eab3d9eSThomas Hellstrom * @ctx: The command buffer context to initialize 3203eab3d9eSThomas Hellstrom */ 3213eab3d9eSThomas Hellstrom static void vmw_cmdbuf_ctx_init(struct vmw_cmdbuf_context *ctx) 3223eab3d9eSThomas Hellstrom { 3233eab3d9eSThomas Hellstrom INIT_LIST_HEAD(&ctx->hw_submitted); 3243eab3d9eSThomas Hellstrom INIT_LIST_HEAD(&ctx->submitted); 3253eab3d9eSThomas Hellstrom INIT_LIST_HEAD(&ctx->preempted); 3263eab3d9eSThomas Hellstrom ctx->num_hw_submitted = 0; 3273eab3d9eSThomas Hellstrom } 3283eab3d9eSThomas Hellstrom 3293eab3d9eSThomas Hellstrom /** 3303eab3d9eSThomas Hellstrom * vmw_cmdbuf_ctx_submit: Submit command buffers from a command buffer 3313eab3d9eSThomas Hellstrom * context. 3323eab3d9eSThomas Hellstrom * 3333eab3d9eSThomas Hellstrom * @man: The command buffer manager. 3343eab3d9eSThomas Hellstrom * @ctx: The command buffer context. 3353eab3d9eSThomas Hellstrom * 3363eab3d9eSThomas Hellstrom * Submits command buffers to hardware until there are no more command 3373eab3d9eSThomas Hellstrom * buffers to submit or the hardware can't handle more command buffers. 3383eab3d9eSThomas Hellstrom */ 3393eab3d9eSThomas Hellstrom static void vmw_cmdbuf_ctx_submit(struct vmw_cmdbuf_man *man, 3403eab3d9eSThomas Hellstrom struct vmw_cmdbuf_context *ctx) 3413eab3d9eSThomas Hellstrom { 3423eab3d9eSThomas Hellstrom while (ctx->num_hw_submitted < man->max_hw_submitted && 34365b97a2bSThomas Hellstrom !list_empty(&ctx->submitted) && 34465b97a2bSThomas Hellstrom !ctx->block_submission) { 3453eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header *entry; 3463eab3d9eSThomas Hellstrom SVGACBStatus status; 3473eab3d9eSThomas Hellstrom 3483eab3d9eSThomas Hellstrom entry = list_first_entry(&ctx->submitted, 3493eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header, 3503eab3d9eSThomas Hellstrom list); 3513eab3d9eSThomas Hellstrom 3523eab3d9eSThomas Hellstrom status = vmw_cmdbuf_header_submit(entry); 3533eab3d9eSThomas Hellstrom 3543eab3d9eSThomas Hellstrom /* This should never happen */ 3553eab3d9eSThomas Hellstrom if (WARN_ON_ONCE(status == SVGA_CB_STATUS_QUEUE_FULL)) { 3563eab3d9eSThomas Hellstrom entry->cb_header->status = SVGA_CB_STATUS_NONE; 3573eab3d9eSThomas Hellstrom break; 3583eab3d9eSThomas Hellstrom } 3593eab3d9eSThomas Hellstrom 3603eab3d9eSThomas Hellstrom list_del(&entry->list); 3613eab3d9eSThomas Hellstrom list_add_tail(&entry->list, &ctx->hw_submitted); 3623eab3d9eSThomas Hellstrom ctx->num_hw_submitted++; 3633eab3d9eSThomas Hellstrom } 3643eab3d9eSThomas Hellstrom 3653eab3d9eSThomas Hellstrom } 3663eab3d9eSThomas Hellstrom 3673eab3d9eSThomas Hellstrom /** 3683eab3d9eSThomas Hellstrom * vmw_cmdbuf_ctx_submit: Process a command buffer context. 3693eab3d9eSThomas Hellstrom * 3703eab3d9eSThomas Hellstrom * @man: The command buffer manager. 3713eab3d9eSThomas Hellstrom * @ctx: The command buffer context. 3723eab3d9eSThomas Hellstrom * 3733eab3d9eSThomas Hellstrom * Submit command buffers to hardware if possible, and process finished 3743eab3d9eSThomas Hellstrom * buffers. Typically freeing them, but on preemption or error take 3753eab3d9eSThomas Hellstrom * appropriate action. Wake up waiters if appropriate. 3763eab3d9eSThomas Hellstrom */ 3773eab3d9eSThomas Hellstrom static void vmw_cmdbuf_ctx_process(struct vmw_cmdbuf_man *man, 3783eab3d9eSThomas Hellstrom struct vmw_cmdbuf_context *ctx, 3793eab3d9eSThomas Hellstrom int *notempty) 3803eab3d9eSThomas Hellstrom { 3813eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header *entry, *next; 3823eab3d9eSThomas Hellstrom 3833eab3d9eSThomas Hellstrom vmw_cmdbuf_ctx_submit(man, ctx); 3843eab3d9eSThomas Hellstrom 3853eab3d9eSThomas Hellstrom list_for_each_entry_safe(entry, next, &ctx->hw_submitted, list) { 3863eab3d9eSThomas Hellstrom SVGACBStatus status = entry->cb_header->status; 3873eab3d9eSThomas Hellstrom 3883eab3d9eSThomas Hellstrom if (status == SVGA_CB_STATUS_NONE) 3893eab3d9eSThomas Hellstrom break; 3903eab3d9eSThomas Hellstrom 3913eab3d9eSThomas Hellstrom list_del(&entry->list); 3923eab3d9eSThomas Hellstrom wake_up_all(&man->idle_queue); 3933eab3d9eSThomas Hellstrom ctx->num_hw_submitted--; 3943eab3d9eSThomas Hellstrom switch (status) { 3953eab3d9eSThomas Hellstrom case SVGA_CB_STATUS_COMPLETED: 3963eab3d9eSThomas Hellstrom __vmw_cmdbuf_header_free(entry); 3973eab3d9eSThomas Hellstrom break; 3983eab3d9eSThomas Hellstrom case SVGA_CB_STATUS_COMMAND_ERROR: 3994062dd3eSDeepak Rawat WARN_ONCE(true, "Command buffer error.\n"); 40065b97a2bSThomas Hellstrom entry->cb_header->status = SVGA_CB_STATUS_NONE; 4013eab3d9eSThomas Hellstrom list_add_tail(&entry->list, &man->error); 4023eab3d9eSThomas Hellstrom schedule_work(&man->work); 4033eab3d9eSThomas Hellstrom break; 4043eab3d9eSThomas Hellstrom case SVGA_CB_STATUS_PREEMPTED: 40565b97a2bSThomas Hellstrom entry->cb_header->status = SVGA_CB_STATUS_NONE; 40665b97a2bSThomas Hellstrom list_add_tail(&entry->list, &ctx->preempted); 40765b97a2bSThomas Hellstrom break; 40865b97a2bSThomas Hellstrom case SVGA_CB_STATUS_CB_HEADER_ERROR: 40965b97a2bSThomas Hellstrom WARN_ONCE(true, "Command buffer header error.\n"); 41065b97a2bSThomas Hellstrom __vmw_cmdbuf_header_free(entry); 4113eab3d9eSThomas Hellstrom break; 4123eab3d9eSThomas Hellstrom default: 4133eab3d9eSThomas Hellstrom WARN_ONCE(true, "Undefined command buffer status.\n"); 4143eab3d9eSThomas Hellstrom __vmw_cmdbuf_header_free(entry); 4153eab3d9eSThomas Hellstrom break; 4163eab3d9eSThomas Hellstrom } 4173eab3d9eSThomas Hellstrom } 4183eab3d9eSThomas Hellstrom 4193eab3d9eSThomas Hellstrom vmw_cmdbuf_ctx_submit(man, ctx); 4203eab3d9eSThomas Hellstrom if (!list_empty(&ctx->submitted)) 4213eab3d9eSThomas Hellstrom (*notempty)++; 4223eab3d9eSThomas Hellstrom } 4233eab3d9eSThomas Hellstrom 4243eab3d9eSThomas Hellstrom /** 4253eab3d9eSThomas Hellstrom * vmw_cmdbuf_man_process - Process all command buffer contexts and 4263eab3d9eSThomas Hellstrom * switch on and off irqs as appropriate. 4273eab3d9eSThomas Hellstrom * 4283eab3d9eSThomas Hellstrom * @man: The command buffer manager. 4293eab3d9eSThomas Hellstrom * 4303eab3d9eSThomas Hellstrom * Calls vmw_cmdbuf_ctx_process() on all contexts. If any context has 4313eab3d9eSThomas Hellstrom * command buffers left that are not submitted to hardware, Make sure 43209dc1387SThomas Hellstrom * IRQ handling is turned on. Otherwise, make sure it's turned off. 4333eab3d9eSThomas Hellstrom */ 43409dc1387SThomas Hellstrom static void vmw_cmdbuf_man_process(struct vmw_cmdbuf_man *man) 4353eab3d9eSThomas Hellstrom { 43609dc1387SThomas Hellstrom int notempty; 4373eab3d9eSThomas Hellstrom struct vmw_cmdbuf_context *ctx; 4383eab3d9eSThomas Hellstrom int i; 4393eab3d9eSThomas Hellstrom 44009dc1387SThomas Hellstrom retry: 44109dc1387SThomas Hellstrom notempty = 0; 4423eab3d9eSThomas Hellstrom for_each_cmdbuf_ctx(man, i, ctx) 4433eab3d9eSThomas Hellstrom vmw_cmdbuf_ctx_process(man, ctx, ¬empty); 4443eab3d9eSThomas Hellstrom 4453eab3d9eSThomas Hellstrom if (man->irq_on && !notempty) { 4463eab3d9eSThomas Hellstrom vmw_generic_waiter_remove(man->dev_priv, 4473eab3d9eSThomas Hellstrom SVGA_IRQFLAG_COMMAND_BUFFER, 4483eab3d9eSThomas Hellstrom &man->dev_priv->cmdbuf_waiters); 4493eab3d9eSThomas Hellstrom man->irq_on = false; 4503eab3d9eSThomas Hellstrom } else if (!man->irq_on && notempty) { 4513eab3d9eSThomas Hellstrom vmw_generic_waiter_add(man->dev_priv, 4523eab3d9eSThomas Hellstrom SVGA_IRQFLAG_COMMAND_BUFFER, 4533eab3d9eSThomas Hellstrom &man->dev_priv->cmdbuf_waiters); 4543eab3d9eSThomas Hellstrom man->irq_on = true; 4553eab3d9eSThomas Hellstrom 4563eab3d9eSThomas Hellstrom /* Rerun in case we just missed an irq. */ 45709dc1387SThomas Hellstrom goto retry; 4583eab3d9eSThomas Hellstrom } 4593eab3d9eSThomas Hellstrom } 4603eab3d9eSThomas Hellstrom 4613eab3d9eSThomas Hellstrom /** 4623eab3d9eSThomas Hellstrom * vmw_cmdbuf_ctx_add - Schedule a command buffer for submission on a 4633eab3d9eSThomas Hellstrom * command buffer context 4643eab3d9eSThomas Hellstrom * 4653eab3d9eSThomas Hellstrom * @man: The command buffer manager. 4663eab3d9eSThomas Hellstrom * @header: The header of the buffer to submit. 4673eab3d9eSThomas Hellstrom * @cb_context: The command buffer context to use. 4683eab3d9eSThomas Hellstrom * 4693eab3d9eSThomas Hellstrom * This function adds @header to the "submitted" queue of the command 4703eab3d9eSThomas Hellstrom * buffer context identified by @cb_context. It then calls the command buffer 4713eab3d9eSThomas Hellstrom * manager processing to potentially submit the buffer to hardware. 4723eab3d9eSThomas Hellstrom * @man->lock needs to be held when calling this function. 4733eab3d9eSThomas Hellstrom */ 4743eab3d9eSThomas Hellstrom static void vmw_cmdbuf_ctx_add(struct vmw_cmdbuf_man *man, 4753eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header *header, 4763eab3d9eSThomas Hellstrom SVGACBContext cb_context) 4773eab3d9eSThomas Hellstrom { 4783eab3d9eSThomas Hellstrom if (!(header->cb_header->flags & SVGA_CB_FLAG_DX_CONTEXT)) 4793eab3d9eSThomas Hellstrom header->cb_header->dxContext = 0; 4803eab3d9eSThomas Hellstrom header->cb_context = cb_context; 4813eab3d9eSThomas Hellstrom list_add_tail(&header->list, &man->ctx[cb_context].submitted); 4823eab3d9eSThomas Hellstrom 4833eab3d9eSThomas Hellstrom vmw_cmdbuf_man_process(man); 4843eab3d9eSThomas Hellstrom } 4853eab3d9eSThomas Hellstrom 4863eab3d9eSThomas Hellstrom /** 487ef369904SThomas Hellstrom * vmw_cmdbuf_irqthread - The main part of the command buffer interrupt 488ef369904SThomas Hellstrom * handler implemented as a threaded irq task. 4893eab3d9eSThomas Hellstrom * 490ef369904SThomas Hellstrom * @man: Pointer to the command buffer manager. 4913eab3d9eSThomas Hellstrom * 492ef369904SThomas Hellstrom * The bottom half of the interrupt handler simply calls into the 4933eab3d9eSThomas Hellstrom * command buffer processor to free finished buffers and submit any 4943eab3d9eSThomas Hellstrom * queued buffers to hardware. 4953eab3d9eSThomas Hellstrom */ 496ef369904SThomas Hellstrom void vmw_cmdbuf_irqthread(struct vmw_cmdbuf_man *man) 4973eab3d9eSThomas Hellstrom { 4983eab3d9eSThomas Hellstrom spin_lock(&man->lock); 49909dc1387SThomas Hellstrom vmw_cmdbuf_man_process(man); 5003eab3d9eSThomas Hellstrom spin_unlock(&man->lock); 5013eab3d9eSThomas Hellstrom } 5023eab3d9eSThomas Hellstrom 5033eab3d9eSThomas Hellstrom /** 5043eab3d9eSThomas Hellstrom * vmw_cmdbuf_work_func - The deferred work function that handles 5053eab3d9eSThomas Hellstrom * command buffer errors. 5063eab3d9eSThomas Hellstrom * 5073eab3d9eSThomas Hellstrom * @work: The work func closure argument. 5083eab3d9eSThomas Hellstrom * 5093eab3d9eSThomas Hellstrom * Restarting the command buffer context after an error requires process 5103eab3d9eSThomas Hellstrom * context, so it is deferred to this work function. 5113eab3d9eSThomas Hellstrom */ 5123eab3d9eSThomas Hellstrom static void vmw_cmdbuf_work_func(struct work_struct *work) 5133eab3d9eSThomas Hellstrom { 5143eab3d9eSThomas Hellstrom struct vmw_cmdbuf_man *man = 5153eab3d9eSThomas Hellstrom container_of(work, struct vmw_cmdbuf_man, work); 5163eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header *entry, *next; 51709dc1387SThomas Hellstrom uint32_t dummy; 51865b97a2bSThomas Hellstrom bool send_fence = false; 51965b97a2bSThomas Hellstrom struct list_head restart_head[SVGA_CB_CONTEXT_MAX]; 52065b97a2bSThomas Hellstrom int i; 52165b97a2bSThomas Hellstrom struct vmw_cmdbuf_context *ctx; 522dc366364SThomas Hellstrom bool global_block = false; 5233eab3d9eSThomas Hellstrom 524b2130ccaSYueHaibing for_each_cmdbuf_ctx(man, i, ctx) 52565b97a2bSThomas Hellstrom INIT_LIST_HEAD(&restart_head[i]); 52665b97a2bSThomas Hellstrom 52765b97a2bSThomas Hellstrom mutex_lock(&man->error_mutex); 528ef369904SThomas Hellstrom spin_lock(&man->lock); 5293eab3d9eSThomas Hellstrom list_for_each_entry_safe(entry, next, &man->error, list) { 53065b97a2bSThomas Hellstrom SVGACBHeader *cb_hdr = entry->cb_header; 53165b97a2bSThomas Hellstrom SVGA3dCmdHeader *header = (SVGA3dCmdHeader *) 53265b97a2bSThomas Hellstrom (entry->cmd + cb_hdr->errorOffset); 53365b97a2bSThomas Hellstrom u32 error_cmd_size, new_start_offset; 53465b97a2bSThomas Hellstrom const char *cmd_name; 5353eab3d9eSThomas Hellstrom 53665b97a2bSThomas Hellstrom list_del_init(&entry->list); 537dc366364SThomas Hellstrom global_block = true; 53865b97a2bSThomas Hellstrom 53965b97a2bSThomas Hellstrom if (!vmw_cmd_describe(header, &error_cmd_size, &cmd_name)) { 5404062dd3eSDeepak Rawat VMW_DEBUG_USER("Unknown command causing device error.\n"); 5414062dd3eSDeepak Rawat VMW_DEBUG_USER("Command buffer offset is %lu\n", 54265b97a2bSThomas Hellstrom (unsigned long) cb_hdr->errorOffset); 5433eab3d9eSThomas Hellstrom __vmw_cmdbuf_header_free(entry); 54465b97a2bSThomas Hellstrom send_fence = true; 54565b97a2bSThomas Hellstrom continue; 54665b97a2bSThomas Hellstrom } 54765b97a2bSThomas Hellstrom 5484062dd3eSDeepak Rawat VMW_DEBUG_USER("Command \"%s\" causing device error.\n", 5494062dd3eSDeepak Rawat cmd_name); 5504062dd3eSDeepak Rawat VMW_DEBUG_USER("Command buffer offset is %lu\n", 55165b97a2bSThomas Hellstrom (unsigned long) cb_hdr->errorOffset); 5524062dd3eSDeepak Rawat VMW_DEBUG_USER("Command size is %lu\n", 55365b97a2bSThomas Hellstrom (unsigned long) error_cmd_size); 55465b97a2bSThomas Hellstrom 55565b97a2bSThomas Hellstrom new_start_offset = cb_hdr->errorOffset + error_cmd_size; 55665b97a2bSThomas Hellstrom 55765b97a2bSThomas Hellstrom if (new_start_offset >= cb_hdr->length) { 55865b97a2bSThomas Hellstrom __vmw_cmdbuf_header_free(entry); 55965b97a2bSThomas Hellstrom send_fence = true; 56065b97a2bSThomas Hellstrom continue; 56165b97a2bSThomas Hellstrom } 56265b97a2bSThomas Hellstrom 56365b97a2bSThomas Hellstrom if (man->using_mob) 56465b97a2bSThomas Hellstrom cb_hdr->ptr.mob.mobOffset += new_start_offset; 56565b97a2bSThomas Hellstrom else 56665b97a2bSThomas Hellstrom cb_hdr->ptr.pa += (u64) new_start_offset; 56765b97a2bSThomas Hellstrom 56865b97a2bSThomas Hellstrom entry->cmd += new_start_offset; 56965b97a2bSThomas Hellstrom cb_hdr->length -= new_start_offset; 57065b97a2bSThomas Hellstrom cb_hdr->errorOffset = 0; 5711f1a36ccSThomas Hellstrom cb_hdr->offset = 0; 572dc366364SThomas Hellstrom 57365b97a2bSThomas Hellstrom list_add_tail(&entry->list, &restart_head[entry->cb_context]); 5743eab3d9eSThomas Hellstrom } 575dc366364SThomas Hellstrom 576dc366364SThomas Hellstrom for_each_cmdbuf_ctx(man, i, ctx) 577dc366364SThomas Hellstrom man->ctx[i].block_submission = true; 578dc366364SThomas Hellstrom 579ef369904SThomas Hellstrom spin_unlock(&man->lock); 5803eab3d9eSThomas Hellstrom 581dc366364SThomas Hellstrom /* Preempt all contexts */ 582dc366364SThomas Hellstrom if (global_block && vmw_cmdbuf_preempt(man, 0)) 583dc366364SThomas Hellstrom DRM_ERROR("Failed preempting command buffer contexts\n"); 58465b97a2bSThomas Hellstrom 58565b97a2bSThomas Hellstrom spin_lock(&man->lock); 58665b97a2bSThomas Hellstrom for_each_cmdbuf_ctx(man, i, ctx) { 58765b97a2bSThomas Hellstrom /* Move preempted command buffers to the preempted queue. */ 58865b97a2bSThomas Hellstrom vmw_cmdbuf_ctx_process(man, ctx, &dummy); 58965b97a2bSThomas Hellstrom 59065b97a2bSThomas Hellstrom /* 59165b97a2bSThomas Hellstrom * Add the preempted queue after the command buffer 59265b97a2bSThomas Hellstrom * that caused an error. 59365b97a2bSThomas Hellstrom */ 59465b97a2bSThomas Hellstrom list_splice_init(&ctx->preempted, restart_head[i].prev); 59565b97a2bSThomas Hellstrom 59665b97a2bSThomas Hellstrom /* 59765b97a2bSThomas Hellstrom * Finally add all command buffers first in the submitted 59865b97a2bSThomas Hellstrom * queue, to rerun them. 59965b97a2bSThomas Hellstrom */ 60065b97a2bSThomas Hellstrom 60165b97a2bSThomas Hellstrom ctx->block_submission = false; 602dc366364SThomas Hellstrom list_splice_init(&restart_head[i], &ctx->submitted); 60365b97a2bSThomas Hellstrom } 60465b97a2bSThomas Hellstrom 60565b97a2bSThomas Hellstrom vmw_cmdbuf_man_process(man); 60665b97a2bSThomas Hellstrom spin_unlock(&man->lock); 60765b97a2bSThomas Hellstrom 608dc366364SThomas Hellstrom if (global_block && vmw_cmdbuf_startstop(man, 0, true)) 609dc366364SThomas Hellstrom DRM_ERROR("Failed restarting command buffer contexts\n"); 6103eab3d9eSThomas Hellstrom 61109dc1387SThomas Hellstrom /* Send a new fence in case one was removed */ 61265b97a2bSThomas Hellstrom if (send_fence) { 61309dc1387SThomas Hellstrom vmw_fifo_send_fence(man->dev_priv, &dummy); 61465b97a2bSThomas Hellstrom wake_up_all(&man->idle_queue); 61565b97a2bSThomas Hellstrom } 61665b97a2bSThomas Hellstrom 61765b97a2bSThomas Hellstrom mutex_unlock(&man->error_mutex); 6183eab3d9eSThomas Hellstrom } 6193eab3d9eSThomas Hellstrom 6203eab3d9eSThomas Hellstrom /** 6213eab3d9eSThomas Hellstrom * vmw_cmdbuf_man idle - Check whether the command buffer manager is idle. 6223eab3d9eSThomas Hellstrom * 6233eab3d9eSThomas Hellstrom * @man: The command buffer manager. 6243eab3d9eSThomas Hellstrom * @check_preempted: Check also the preempted queue for pending command buffers. 6253eab3d9eSThomas Hellstrom * 6263eab3d9eSThomas Hellstrom */ 6273eab3d9eSThomas Hellstrom static bool vmw_cmdbuf_man_idle(struct vmw_cmdbuf_man *man, 6283eab3d9eSThomas Hellstrom bool check_preempted) 6293eab3d9eSThomas Hellstrom { 6303eab3d9eSThomas Hellstrom struct vmw_cmdbuf_context *ctx; 6313eab3d9eSThomas Hellstrom bool idle = false; 6323eab3d9eSThomas Hellstrom int i; 6333eab3d9eSThomas Hellstrom 634ef369904SThomas Hellstrom spin_lock(&man->lock); 6353eab3d9eSThomas Hellstrom vmw_cmdbuf_man_process(man); 6363eab3d9eSThomas Hellstrom for_each_cmdbuf_ctx(man, i, ctx) { 6373eab3d9eSThomas Hellstrom if (!list_empty(&ctx->submitted) || 6383eab3d9eSThomas Hellstrom !list_empty(&ctx->hw_submitted) || 6393eab3d9eSThomas Hellstrom (check_preempted && !list_empty(&ctx->preempted))) 6403eab3d9eSThomas Hellstrom goto out_unlock; 6413eab3d9eSThomas Hellstrom } 6423eab3d9eSThomas Hellstrom 6433eab3d9eSThomas Hellstrom idle = list_empty(&man->error); 6443eab3d9eSThomas Hellstrom 6453eab3d9eSThomas Hellstrom out_unlock: 646ef369904SThomas Hellstrom spin_unlock(&man->lock); 6473eab3d9eSThomas Hellstrom 6483eab3d9eSThomas Hellstrom return idle; 6493eab3d9eSThomas Hellstrom } 6503eab3d9eSThomas Hellstrom 6513eab3d9eSThomas Hellstrom /** 6523eab3d9eSThomas Hellstrom * __vmw_cmdbuf_cur_flush - Flush the current command buffer for small kernel 6533eab3d9eSThomas Hellstrom * command submissions 6543eab3d9eSThomas Hellstrom * 6553eab3d9eSThomas Hellstrom * @man: The command buffer manager. 6563eab3d9eSThomas Hellstrom * 6573eab3d9eSThomas Hellstrom * Flushes the current command buffer without allocating a new one. A new one 6583eab3d9eSThomas Hellstrom * is automatically allocated when needed. Call with @man->cur_mutex held. 6593eab3d9eSThomas Hellstrom */ 6603eab3d9eSThomas Hellstrom static void __vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man *man) 6613eab3d9eSThomas Hellstrom { 6623eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header *cur = man->cur; 6633eab3d9eSThomas Hellstrom 664d76ce03eSThomas Hellstrom lockdep_assert_held_once(&man->cur_mutex); 6653eab3d9eSThomas Hellstrom 6663eab3d9eSThomas Hellstrom if (!cur) 6673eab3d9eSThomas Hellstrom return; 6683eab3d9eSThomas Hellstrom 669ef369904SThomas Hellstrom spin_lock(&man->lock); 6703eab3d9eSThomas Hellstrom if (man->cur_pos == 0) { 6713eab3d9eSThomas Hellstrom __vmw_cmdbuf_header_free(cur); 6723eab3d9eSThomas Hellstrom goto out_unlock; 6733eab3d9eSThomas Hellstrom } 6743eab3d9eSThomas Hellstrom 6753eab3d9eSThomas Hellstrom man->cur->cb_header->length = man->cur_pos; 6763eab3d9eSThomas Hellstrom vmw_cmdbuf_ctx_add(man, man->cur, SVGA_CB_CONTEXT_0); 6773eab3d9eSThomas Hellstrom out_unlock: 678ef369904SThomas Hellstrom spin_unlock(&man->lock); 6793eab3d9eSThomas Hellstrom man->cur = NULL; 6803eab3d9eSThomas Hellstrom man->cur_pos = 0; 6813eab3d9eSThomas Hellstrom } 6823eab3d9eSThomas Hellstrom 6833eab3d9eSThomas Hellstrom /** 6843eab3d9eSThomas Hellstrom * vmw_cmdbuf_cur_flush - Flush the current command buffer for small kernel 6853eab3d9eSThomas Hellstrom * command submissions 6863eab3d9eSThomas Hellstrom * 6873eab3d9eSThomas Hellstrom * @man: The command buffer manager. 6883eab3d9eSThomas Hellstrom * @interruptible: Whether to sleep interruptible when sleeping. 6893eab3d9eSThomas Hellstrom * 6903eab3d9eSThomas Hellstrom * Flushes the current command buffer without allocating a new one. A new one 6913eab3d9eSThomas Hellstrom * is automatically allocated when needed. 6923eab3d9eSThomas Hellstrom */ 6933eab3d9eSThomas Hellstrom int vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man *man, 6943eab3d9eSThomas Hellstrom bool interruptible) 6953eab3d9eSThomas Hellstrom { 6963eab3d9eSThomas Hellstrom int ret = vmw_cmdbuf_cur_lock(man, interruptible); 6973eab3d9eSThomas Hellstrom 6983eab3d9eSThomas Hellstrom if (ret) 6993eab3d9eSThomas Hellstrom return ret; 7003eab3d9eSThomas Hellstrom 7013eab3d9eSThomas Hellstrom __vmw_cmdbuf_cur_flush(man); 7023eab3d9eSThomas Hellstrom vmw_cmdbuf_cur_unlock(man); 7033eab3d9eSThomas Hellstrom 7043eab3d9eSThomas Hellstrom return 0; 7053eab3d9eSThomas Hellstrom } 7063eab3d9eSThomas Hellstrom 7073eab3d9eSThomas Hellstrom /** 7083eab3d9eSThomas Hellstrom * vmw_cmdbuf_idle - Wait for command buffer manager idle. 7093eab3d9eSThomas Hellstrom * 7103eab3d9eSThomas Hellstrom * @man: The command buffer manager. 7113eab3d9eSThomas Hellstrom * @interruptible: Sleep interruptible while waiting. 7123eab3d9eSThomas Hellstrom * @timeout: Time out after this many ticks. 7133eab3d9eSThomas Hellstrom * 7143eab3d9eSThomas Hellstrom * Wait until the command buffer manager has processed all command buffers, 7153eab3d9eSThomas Hellstrom * or until a timeout occurs. If a timeout occurs, the function will return 7163eab3d9eSThomas Hellstrom * -EBUSY. 7173eab3d9eSThomas Hellstrom */ 7183eab3d9eSThomas Hellstrom int vmw_cmdbuf_idle(struct vmw_cmdbuf_man *man, bool interruptible, 7193eab3d9eSThomas Hellstrom unsigned long timeout) 7203eab3d9eSThomas Hellstrom { 7213eab3d9eSThomas Hellstrom int ret; 7223eab3d9eSThomas Hellstrom 7233eab3d9eSThomas Hellstrom ret = vmw_cmdbuf_cur_flush(man, interruptible); 7243eab3d9eSThomas Hellstrom vmw_generic_waiter_add(man->dev_priv, 7253eab3d9eSThomas Hellstrom SVGA_IRQFLAG_COMMAND_BUFFER, 7263eab3d9eSThomas Hellstrom &man->dev_priv->cmdbuf_waiters); 7273eab3d9eSThomas Hellstrom 7283eab3d9eSThomas Hellstrom if (interruptible) { 7293eab3d9eSThomas Hellstrom ret = wait_event_interruptible_timeout 7303eab3d9eSThomas Hellstrom (man->idle_queue, vmw_cmdbuf_man_idle(man, true), 7313eab3d9eSThomas Hellstrom timeout); 7323eab3d9eSThomas Hellstrom } else { 7333eab3d9eSThomas Hellstrom ret = wait_event_timeout 7343eab3d9eSThomas Hellstrom (man->idle_queue, vmw_cmdbuf_man_idle(man, true), 7353eab3d9eSThomas Hellstrom timeout); 7363eab3d9eSThomas Hellstrom } 7373eab3d9eSThomas Hellstrom vmw_generic_waiter_remove(man->dev_priv, 7383eab3d9eSThomas Hellstrom SVGA_IRQFLAG_COMMAND_BUFFER, 7393eab3d9eSThomas Hellstrom &man->dev_priv->cmdbuf_waiters); 7403eab3d9eSThomas Hellstrom if (ret == 0) { 7413eab3d9eSThomas Hellstrom if (!vmw_cmdbuf_man_idle(man, true)) 7423eab3d9eSThomas Hellstrom ret = -EBUSY; 7433eab3d9eSThomas Hellstrom else 7443eab3d9eSThomas Hellstrom ret = 0; 7453eab3d9eSThomas Hellstrom } 7463eab3d9eSThomas Hellstrom if (ret > 0) 7473eab3d9eSThomas Hellstrom ret = 0; 7483eab3d9eSThomas Hellstrom 7493eab3d9eSThomas Hellstrom return ret; 7503eab3d9eSThomas Hellstrom } 7513eab3d9eSThomas Hellstrom 7523eab3d9eSThomas Hellstrom /** 7533eab3d9eSThomas Hellstrom * vmw_cmdbuf_try_alloc - Try to allocate buffer space from the main pool. 7543eab3d9eSThomas Hellstrom * 7553eab3d9eSThomas Hellstrom * @man: The command buffer manager. 7563eab3d9eSThomas Hellstrom * @info: Allocation info. Will hold the size on entry and allocated mm node 7573eab3d9eSThomas Hellstrom * on successful return. 7583eab3d9eSThomas Hellstrom * 7593eab3d9eSThomas Hellstrom * Try to allocate buffer space from the main pool. Returns true if succeeded. 7603eab3d9eSThomas Hellstrom * If a fatal error was hit, the error code is returned in @info->ret. 7613eab3d9eSThomas Hellstrom */ 7623eab3d9eSThomas Hellstrom static bool vmw_cmdbuf_try_alloc(struct vmw_cmdbuf_man *man, 7633eab3d9eSThomas Hellstrom struct vmw_cmdbuf_alloc_info *info) 7643eab3d9eSThomas Hellstrom { 7653eab3d9eSThomas Hellstrom int ret; 7663eab3d9eSThomas Hellstrom 7679b590783SThomas Hellstrom if (info->done) 7683eab3d9eSThomas Hellstrom return true; 7693eab3d9eSThomas Hellstrom 7709b590783SThomas Hellstrom memset(info->node, 0, sizeof(*info->node)); 771ef369904SThomas Hellstrom spin_lock(&man->lock); 7724e64e553SChris Wilson ret = drm_mm_insert_node(&man->mm, info->node, info->page_size); 773575f9c86SThomas Hellstrom if (ret) { 77409dc1387SThomas Hellstrom vmw_cmdbuf_man_process(man); 7754e64e553SChris Wilson ret = drm_mm_insert_node(&man->mm, info->node, info->page_size); 776575f9c86SThomas Hellstrom } 777575f9c86SThomas Hellstrom 778ef369904SThomas Hellstrom spin_unlock(&man->lock); 7799b590783SThomas Hellstrom info->done = !ret; 7803eab3d9eSThomas Hellstrom 7819b590783SThomas Hellstrom return info->done; 7823eab3d9eSThomas Hellstrom } 7833eab3d9eSThomas Hellstrom 7843eab3d9eSThomas Hellstrom /** 7853eab3d9eSThomas Hellstrom * vmw_cmdbuf_alloc_space - Allocate buffer space from the main pool. 7863eab3d9eSThomas Hellstrom * 7873eab3d9eSThomas Hellstrom * @man: The command buffer manager. 7889b590783SThomas Hellstrom * @node: Pointer to pre-allocated range-manager node. 7893eab3d9eSThomas Hellstrom * @size: The size of the allocation. 7903eab3d9eSThomas Hellstrom * @interruptible: Whether to sleep interruptible while waiting for space. 7913eab3d9eSThomas Hellstrom * 7923eab3d9eSThomas Hellstrom * This function allocates buffer space from the main pool, and if there is 7933eab3d9eSThomas Hellstrom * no space available ATM, it turns on IRQ handling and sleeps waiting for it to 7943eab3d9eSThomas Hellstrom * become available. 7953eab3d9eSThomas Hellstrom */ 796b9eb1a61SThomas Hellstrom static int vmw_cmdbuf_alloc_space(struct vmw_cmdbuf_man *man, 7979b590783SThomas Hellstrom struct drm_mm_node *node, 7983eab3d9eSThomas Hellstrom size_t size, 7993eab3d9eSThomas Hellstrom bool interruptible) 8003eab3d9eSThomas Hellstrom { 8013eab3d9eSThomas Hellstrom struct vmw_cmdbuf_alloc_info info; 8023eab3d9eSThomas Hellstrom 8033eab3d9eSThomas Hellstrom info.page_size = PAGE_ALIGN(size) >> PAGE_SHIFT; 8049b590783SThomas Hellstrom info.node = node; 8059b590783SThomas Hellstrom info.done = false; 8063eab3d9eSThomas Hellstrom 8073eab3d9eSThomas Hellstrom /* 8083eab3d9eSThomas Hellstrom * To prevent starvation of large requests, only one allocating call 8093eab3d9eSThomas Hellstrom * at a time waiting for space. 8103eab3d9eSThomas Hellstrom */ 8113eab3d9eSThomas Hellstrom if (interruptible) { 8123eab3d9eSThomas Hellstrom if (mutex_lock_interruptible(&man->space_mutex)) 8139b590783SThomas Hellstrom return -ERESTARTSYS; 8143eab3d9eSThomas Hellstrom } else { 8153eab3d9eSThomas Hellstrom mutex_lock(&man->space_mutex); 8163eab3d9eSThomas Hellstrom } 8173eab3d9eSThomas Hellstrom 8183eab3d9eSThomas Hellstrom /* Try to allocate space without waiting. */ 8199b590783SThomas Hellstrom if (vmw_cmdbuf_try_alloc(man, &info)) 8209b590783SThomas Hellstrom goto out_unlock; 8213eab3d9eSThomas Hellstrom 8223eab3d9eSThomas Hellstrom vmw_generic_waiter_add(man->dev_priv, 8233eab3d9eSThomas Hellstrom SVGA_IRQFLAG_COMMAND_BUFFER, 8243eab3d9eSThomas Hellstrom &man->dev_priv->cmdbuf_waiters); 8253eab3d9eSThomas Hellstrom 8263eab3d9eSThomas Hellstrom if (interruptible) { 8273eab3d9eSThomas Hellstrom int ret; 8283eab3d9eSThomas Hellstrom 8293eab3d9eSThomas Hellstrom ret = wait_event_interruptible 8303eab3d9eSThomas Hellstrom (man->alloc_queue, vmw_cmdbuf_try_alloc(man, &info)); 8313eab3d9eSThomas Hellstrom if (ret) { 8323eab3d9eSThomas Hellstrom vmw_generic_waiter_remove 8333eab3d9eSThomas Hellstrom (man->dev_priv, SVGA_IRQFLAG_COMMAND_BUFFER, 8343eab3d9eSThomas Hellstrom &man->dev_priv->cmdbuf_waiters); 8353eab3d9eSThomas Hellstrom mutex_unlock(&man->space_mutex); 8369b590783SThomas Hellstrom return ret; 8373eab3d9eSThomas Hellstrom } 8383eab3d9eSThomas Hellstrom } else { 8393eab3d9eSThomas Hellstrom wait_event(man->alloc_queue, vmw_cmdbuf_try_alloc(man, &info)); 8403eab3d9eSThomas Hellstrom } 8413eab3d9eSThomas Hellstrom vmw_generic_waiter_remove(man->dev_priv, 8423eab3d9eSThomas Hellstrom SVGA_IRQFLAG_COMMAND_BUFFER, 8433eab3d9eSThomas Hellstrom &man->dev_priv->cmdbuf_waiters); 8443eab3d9eSThomas Hellstrom 8459b590783SThomas Hellstrom out_unlock: 8469b590783SThomas Hellstrom mutex_unlock(&man->space_mutex); 8479b590783SThomas Hellstrom 8489b590783SThomas Hellstrom return 0; 8493eab3d9eSThomas Hellstrom } 8503eab3d9eSThomas Hellstrom 8513eab3d9eSThomas Hellstrom /** 8523eab3d9eSThomas Hellstrom * vmw_cmdbuf_space_pool - Set up a command buffer header with command buffer 8533eab3d9eSThomas Hellstrom * space from the main pool. 8543eab3d9eSThomas Hellstrom * 8553eab3d9eSThomas Hellstrom * @man: The command buffer manager. 8563eab3d9eSThomas Hellstrom * @header: Pointer to the header to set up. 8573eab3d9eSThomas Hellstrom * @size: The requested size of the buffer space. 8583eab3d9eSThomas Hellstrom * @interruptible: Whether to sleep interruptible while waiting for space. 8593eab3d9eSThomas Hellstrom */ 8603eab3d9eSThomas Hellstrom static int vmw_cmdbuf_space_pool(struct vmw_cmdbuf_man *man, 8613eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header *header, 8623eab3d9eSThomas Hellstrom size_t size, 8633eab3d9eSThomas Hellstrom bool interruptible) 8643eab3d9eSThomas Hellstrom { 8653eab3d9eSThomas Hellstrom SVGACBHeader *cb_hdr; 8663eab3d9eSThomas Hellstrom size_t offset; 8673eab3d9eSThomas Hellstrom int ret; 8683eab3d9eSThomas Hellstrom 8693eab3d9eSThomas Hellstrom if (!man->has_pool) 8703eab3d9eSThomas Hellstrom return -ENOMEM; 8713eab3d9eSThomas Hellstrom 8729b590783SThomas Hellstrom ret = vmw_cmdbuf_alloc_space(man, &header->node, size, interruptible); 8733eab3d9eSThomas Hellstrom 8749b590783SThomas Hellstrom if (ret) 8759b590783SThomas Hellstrom return ret; 8763eab3d9eSThomas Hellstrom 877a02f6da6SSouptick Joarder header->cb_header = dma_pool_zalloc(man->headers, GFP_KERNEL, 8783eab3d9eSThomas Hellstrom &header->handle); 8793eab3d9eSThomas Hellstrom if (!header->cb_header) { 8803eab3d9eSThomas Hellstrom ret = -ENOMEM; 8813eab3d9eSThomas Hellstrom goto out_no_cb_header; 8823eab3d9eSThomas Hellstrom } 8833eab3d9eSThomas Hellstrom 8849b590783SThomas Hellstrom header->size = header->node.size << PAGE_SHIFT; 8853eab3d9eSThomas Hellstrom cb_hdr = header->cb_header; 8869b590783SThomas Hellstrom offset = header->node.start << PAGE_SHIFT; 8873eab3d9eSThomas Hellstrom header->cmd = man->map + offset; 8883eab3d9eSThomas Hellstrom if (man->using_mob) { 8893eab3d9eSThomas Hellstrom cb_hdr->flags = SVGA_CB_FLAG_MOB; 8903eab3d9eSThomas Hellstrom cb_hdr->ptr.mob.mobid = man->cmd_space->mem.start; 8913eab3d9eSThomas Hellstrom cb_hdr->ptr.mob.mobOffset = offset; 8923eab3d9eSThomas Hellstrom } else { 8933eab3d9eSThomas Hellstrom cb_hdr->ptr.pa = (u64)man->handle + (u64)offset; 8943eab3d9eSThomas Hellstrom } 8953eab3d9eSThomas Hellstrom 8963eab3d9eSThomas Hellstrom return 0; 8973eab3d9eSThomas Hellstrom 8983eab3d9eSThomas Hellstrom out_no_cb_header: 899ef369904SThomas Hellstrom spin_lock(&man->lock); 9009b590783SThomas Hellstrom drm_mm_remove_node(&header->node); 901ef369904SThomas Hellstrom spin_unlock(&man->lock); 9023eab3d9eSThomas Hellstrom 9033eab3d9eSThomas Hellstrom return ret; 9043eab3d9eSThomas Hellstrom } 9053eab3d9eSThomas Hellstrom 9063eab3d9eSThomas Hellstrom /** 9073eab3d9eSThomas Hellstrom * vmw_cmdbuf_space_inline - Set up a command buffer header with 9083eab3d9eSThomas Hellstrom * inline command buffer space. 9093eab3d9eSThomas Hellstrom * 9103eab3d9eSThomas Hellstrom * @man: The command buffer manager. 9113eab3d9eSThomas Hellstrom * @header: Pointer to the header to set up. 9123eab3d9eSThomas Hellstrom * @size: The requested size of the buffer space. 9133eab3d9eSThomas Hellstrom */ 9143eab3d9eSThomas Hellstrom static int vmw_cmdbuf_space_inline(struct vmw_cmdbuf_man *man, 9153eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header *header, 9163eab3d9eSThomas Hellstrom int size) 9173eab3d9eSThomas Hellstrom { 9183eab3d9eSThomas Hellstrom struct vmw_cmdbuf_dheader *dheader; 9193eab3d9eSThomas Hellstrom SVGACBHeader *cb_hdr; 9203eab3d9eSThomas Hellstrom 9213eab3d9eSThomas Hellstrom if (WARN_ON_ONCE(size > VMW_CMDBUF_INLINE_SIZE)) 9223eab3d9eSThomas Hellstrom return -ENOMEM; 9233eab3d9eSThomas Hellstrom 924a02f6da6SSouptick Joarder dheader = dma_pool_zalloc(man->dheaders, GFP_KERNEL, 9253eab3d9eSThomas Hellstrom &header->handle); 9263eab3d9eSThomas Hellstrom if (!dheader) 9273eab3d9eSThomas Hellstrom return -ENOMEM; 9283eab3d9eSThomas Hellstrom 9293eab3d9eSThomas Hellstrom header->inline_space = true; 9303eab3d9eSThomas Hellstrom header->size = VMW_CMDBUF_INLINE_SIZE; 9313eab3d9eSThomas Hellstrom cb_hdr = &dheader->cb_header; 9323eab3d9eSThomas Hellstrom header->cb_header = cb_hdr; 9333eab3d9eSThomas Hellstrom header->cmd = dheader->cmd; 9343eab3d9eSThomas Hellstrom cb_hdr->status = SVGA_CB_STATUS_NONE; 9353eab3d9eSThomas Hellstrom cb_hdr->flags = SVGA_CB_FLAG_NONE; 9363eab3d9eSThomas Hellstrom cb_hdr->ptr.pa = (u64)header->handle + 9373eab3d9eSThomas Hellstrom (u64)offsetof(struct vmw_cmdbuf_dheader, cmd); 9383eab3d9eSThomas Hellstrom 9393eab3d9eSThomas Hellstrom return 0; 9403eab3d9eSThomas Hellstrom } 9413eab3d9eSThomas Hellstrom 9423eab3d9eSThomas Hellstrom /** 9433eab3d9eSThomas Hellstrom * vmw_cmdbuf_alloc - Allocate a command buffer header complete with 9443eab3d9eSThomas Hellstrom * command buffer space. 9453eab3d9eSThomas Hellstrom * 9463eab3d9eSThomas Hellstrom * @man: The command buffer manager. 9473eab3d9eSThomas Hellstrom * @size: The requested size of the buffer space. 9483eab3d9eSThomas Hellstrom * @interruptible: Whether to sleep interruptible while waiting for space. 9493eab3d9eSThomas Hellstrom * @p_header: points to a header pointer to populate on successful return. 9503eab3d9eSThomas Hellstrom * 9513eab3d9eSThomas Hellstrom * Returns a pointer to command buffer space if successful. Otherwise 9523eab3d9eSThomas Hellstrom * returns an error pointer. The header pointer returned in @p_header should 9533eab3d9eSThomas Hellstrom * be used for upcoming calls to vmw_cmdbuf_reserve() and vmw_cmdbuf_commit(). 9543eab3d9eSThomas Hellstrom */ 9553eab3d9eSThomas Hellstrom void *vmw_cmdbuf_alloc(struct vmw_cmdbuf_man *man, 9563eab3d9eSThomas Hellstrom size_t size, bool interruptible, 9573eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header **p_header) 9583eab3d9eSThomas Hellstrom { 9593eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header *header; 9603eab3d9eSThomas Hellstrom int ret = 0; 9613eab3d9eSThomas Hellstrom 9623eab3d9eSThomas Hellstrom *p_header = NULL; 9633eab3d9eSThomas Hellstrom 9643eab3d9eSThomas Hellstrom header = kzalloc(sizeof(*header), GFP_KERNEL); 9653eab3d9eSThomas Hellstrom if (!header) 9663eab3d9eSThomas Hellstrom return ERR_PTR(-ENOMEM); 9673eab3d9eSThomas Hellstrom 9683eab3d9eSThomas Hellstrom if (size <= VMW_CMDBUF_INLINE_SIZE) 9693eab3d9eSThomas Hellstrom ret = vmw_cmdbuf_space_inline(man, header, size); 9703eab3d9eSThomas Hellstrom else 9713eab3d9eSThomas Hellstrom ret = vmw_cmdbuf_space_pool(man, header, size, interruptible); 9723eab3d9eSThomas Hellstrom 9733eab3d9eSThomas Hellstrom if (ret) { 9743eab3d9eSThomas Hellstrom kfree(header); 9753eab3d9eSThomas Hellstrom return ERR_PTR(ret); 9763eab3d9eSThomas Hellstrom } 9773eab3d9eSThomas Hellstrom 9783eab3d9eSThomas Hellstrom header->man = man; 9793eab3d9eSThomas Hellstrom INIT_LIST_HEAD(&header->list); 9803eab3d9eSThomas Hellstrom header->cb_header->status = SVGA_CB_STATUS_NONE; 9813eab3d9eSThomas Hellstrom *p_header = header; 9823eab3d9eSThomas Hellstrom 9833eab3d9eSThomas Hellstrom return header->cmd; 9843eab3d9eSThomas Hellstrom } 9853eab3d9eSThomas Hellstrom 9863eab3d9eSThomas Hellstrom /** 9873eab3d9eSThomas Hellstrom * vmw_cmdbuf_reserve_cur - Reserve space for commands in the current 9883eab3d9eSThomas Hellstrom * command buffer. 9893eab3d9eSThomas Hellstrom * 9903eab3d9eSThomas Hellstrom * @man: The command buffer manager. 9913eab3d9eSThomas Hellstrom * @size: The requested size of the commands. 9923eab3d9eSThomas Hellstrom * @ctx_id: The context id if any. Otherwise set to SVGA3D_REG_INVALID. 9933eab3d9eSThomas Hellstrom * @interruptible: Whether to sleep interruptible while waiting for space. 9943eab3d9eSThomas Hellstrom * 9953eab3d9eSThomas Hellstrom * Returns a pointer to command buffer space if successful. Otherwise 9963eab3d9eSThomas Hellstrom * returns an error pointer. 9973eab3d9eSThomas Hellstrom */ 9983eab3d9eSThomas Hellstrom static void *vmw_cmdbuf_reserve_cur(struct vmw_cmdbuf_man *man, 9993eab3d9eSThomas Hellstrom size_t size, 10003eab3d9eSThomas Hellstrom int ctx_id, 10013eab3d9eSThomas Hellstrom bool interruptible) 10023eab3d9eSThomas Hellstrom { 10033eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header *cur; 10043eab3d9eSThomas Hellstrom void *ret; 10053eab3d9eSThomas Hellstrom 10063eab3d9eSThomas Hellstrom if (vmw_cmdbuf_cur_lock(man, interruptible)) 10073eab3d9eSThomas Hellstrom return ERR_PTR(-ERESTARTSYS); 10083eab3d9eSThomas Hellstrom 10093eab3d9eSThomas Hellstrom cur = man->cur; 10103eab3d9eSThomas Hellstrom if (cur && (size + man->cur_pos > cur->size || 1011d80efd5cSThomas Hellstrom ((cur->cb_header->flags & SVGA_CB_FLAG_DX_CONTEXT) && 10123eab3d9eSThomas Hellstrom ctx_id != cur->cb_header->dxContext))) 10133eab3d9eSThomas Hellstrom __vmw_cmdbuf_cur_flush(man); 10143eab3d9eSThomas Hellstrom 10153eab3d9eSThomas Hellstrom if (!man->cur) { 10163eab3d9eSThomas Hellstrom ret = vmw_cmdbuf_alloc(man, 10173eab3d9eSThomas Hellstrom max_t(size_t, size, man->default_size), 10183eab3d9eSThomas Hellstrom interruptible, &man->cur); 10193eab3d9eSThomas Hellstrom if (IS_ERR(ret)) { 10203eab3d9eSThomas Hellstrom vmw_cmdbuf_cur_unlock(man); 10213eab3d9eSThomas Hellstrom return ret; 10223eab3d9eSThomas Hellstrom } 10233eab3d9eSThomas Hellstrom 10243eab3d9eSThomas Hellstrom cur = man->cur; 10253eab3d9eSThomas Hellstrom } 10263eab3d9eSThomas Hellstrom 10273eab3d9eSThomas Hellstrom if (ctx_id != SVGA3D_INVALID_ID) { 10283eab3d9eSThomas Hellstrom cur->cb_header->flags |= SVGA_CB_FLAG_DX_CONTEXT; 10293eab3d9eSThomas Hellstrom cur->cb_header->dxContext = ctx_id; 10303eab3d9eSThomas Hellstrom } 10313eab3d9eSThomas Hellstrom 10323eab3d9eSThomas Hellstrom cur->reserved = size; 10333eab3d9eSThomas Hellstrom 10343eab3d9eSThomas Hellstrom return (void *) (man->cur->cmd + man->cur_pos); 10353eab3d9eSThomas Hellstrom } 10363eab3d9eSThomas Hellstrom 10373eab3d9eSThomas Hellstrom /** 10383eab3d9eSThomas Hellstrom * vmw_cmdbuf_commit_cur - Commit commands in the current command buffer. 10393eab3d9eSThomas Hellstrom * 10403eab3d9eSThomas Hellstrom * @man: The command buffer manager. 10413eab3d9eSThomas Hellstrom * @size: The size of the commands actually written. 10423eab3d9eSThomas Hellstrom * @flush: Whether to flush the command buffer immediately. 10433eab3d9eSThomas Hellstrom */ 10443eab3d9eSThomas Hellstrom static void vmw_cmdbuf_commit_cur(struct vmw_cmdbuf_man *man, 10453eab3d9eSThomas Hellstrom size_t size, bool flush) 10463eab3d9eSThomas Hellstrom { 10473eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header *cur = man->cur; 10483eab3d9eSThomas Hellstrom 1049d76ce03eSThomas Hellstrom lockdep_assert_held_once(&man->cur_mutex); 10503eab3d9eSThomas Hellstrom 10513eab3d9eSThomas Hellstrom WARN_ON(size > cur->reserved); 10523eab3d9eSThomas Hellstrom man->cur_pos += size; 10533eab3d9eSThomas Hellstrom if (!size) 10543eab3d9eSThomas Hellstrom cur->cb_header->flags &= ~SVGA_CB_FLAG_DX_CONTEXT; 10553eab3d9eSThomas Hellstrom if (flush) 10563eab3d9eSThomas Hellstrom __vmw_cmdbuf_cur_flush(man); 10573eab3d9eSThomas Hellstrom vmw_cmdbuf_cur_unlock(man); 10583eab3d9eSThomas Hellstrom } 10593eab3d9eSThomas Hellstrom 10603eab3d9eSThomas Hellstrom /** 10613eab3d9eSThomas Hellstrom * vmw_cmdbuf_reserve - Reserve space for commands in a command buffer. 10623eab3d9eSThomas Hellstrom * 10633eab3d9eSThomas Hellstrom * @man: The command buffer manager. 10643eab3d9eSThomas Hellstrom * @size: The requested size of the commands. 10653eab3d9eSThomas Hellstrom * @ctx_id: The context id if any. Otherwise set to SVGA3D_REG_INVALID. 10663eab3d9eSThomas Hellstrom * @interruptible: Whether to sleep interruptible while waiting for space. 10673eab3d9eSThomas Hellstrom * @header: Header of the command buffer. NULL if the current command buffer 10683eab3d9eSThomas Hellstrom * should be used. 10693eab3d9eSThomas Hellstrom * 10703eab3d9eSThomas Hellstrom * Returns a pointer to command buffer space if successful. Otherwise 10713eab3d9eSThomas Hellstrom * returns an error pointer. 10723eab3d9eSThomas Hellstrom */ 10733eab3d9eSThomas Hellstrom void *vmw_cmdbuf_reserve(struct vmw_cmdbuf_man *man, size_t size, 10743eab3d9eSThomas Hellstrom int ctx_id, bool interruptible, 10753eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header *header) 10763eab3d9eSThomas Hellstrom { 10773eab3d9eSThomas Hellstrom if (!header) 10783eab3d9eSThomas Hellstrom return vmw_cmdbuf_reserve_cur(man, size, ctx_id, interruptible); 10793eab3d9eSThomas Hellstrom 10803eab3d9eSThomas Hellstrom if (size > header->size) 10813eab3d9eSThomas Hellstrom return ERR_PTR(-EINVAL); 10823eab3d9eSThomas Hellstrom 10833eab3d9eSThomas Hellstrom if (ctx_id != SVGA3D_INVALID_ID) { 10843eab3d9eSThomas Hellstrom header->cb_header->flags |= SVGA_CB_FLAG_DX_CONTEXT; 10853eab3d9eSThomas Hellstrom header->cb_header->dxContext = ctx_id; 10863eab3d9eSThomas Hellstrom } 10873eab3d9eSThomas Hellstrom 10883eab3d9eSThomas Hellstrom header->reserved = size; 10893eab3d9eSThomas Hellstrom return header->cmd; 10903eab3d9eSThomas Hellstrom } 10913eab3d9eSThomas Hellstrom 10923eab3d9eSThomas Hellstrom /** 10933eab3d9eSThomas Hellstrom * vmw_cmdbuf_commit - Commit commands in a command buffer. 10943eab3d9eSThomas Hellstrom * 10953eab3d9eSThomas Hellstrom * @man: The command buffer manager. 10963eab3d9eSThomas Hellstrom * @size: The size of the commands actually written. 10973eab3d9eSThomas Hellstrom * @header: Header of the command buffer. NULL if the current command buffer 10983eab3d9eSThomas Hellstrom * should be used. 10993eab3d9eSThomas Hellstrom * @flush: Whether to flush the command buffer immediately. 11003eab3d9eSThomas Hellstrom */ 11013eab3d9eSThomas Hellstrom void vmw_cmdbuf_commit(struct vmw_cmdbuf_man *man, size_t size, 11023eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header *header, bool flush) 11033eab3d9eSThomas Hellstrom { 11043eab3d9eSThomas Hellstrom if (!header) { 11053eab3d9eSThomas Hellstrom vmw_cmdbuf_commit_cur(man, size, flush); 11063eab3d9eSThomas Hellstrom return; 11073eab3d9eSThomas Hellstrom } 11083eab3d9eSThomas Hellstrom 11093eab3d9eSThomas Hellstrom (void) vmw_cmdbuf_cur_lock(man, false); 11103eab3d9eSThomas Hellstrom __vmw_cmdbuf_cur_flush(man); 11113eab3d9eSThomas Hellstrom WARN_ON(size > header->reserved); 11123eab3d9eSThomas Hellstrom man->cur = header; 11133eab3d9eSThomas Hellstrom man->cur_pos = size; 11143eab3d9eSThomas Hellstrom if (!size) 11153eab3d9eSThomas Hellstrom header->cb_header->flags &= ~SVGA_CB_FLAG_DX_CONTEXT; 11163eab3d9eSThomas Hellstrom if (flush) 11173eab3d9eSThomas Hellstrom __vmw_cmdbuf_cur_flush(man); 11183eab3d9eSThomas Hellstrom vmw_cmdbuf_cur_unlock(man); 11193eab3d9eSThomas Hellstrom } 11203eab3d9eSThomas Hellstrom 11213eab3d9eSThomas Hellstrom 11223eab3d9eSThomas Hellstrom /** 11233eab3d9eSThomas Hellstrom * vmw_cmdbuf_send_device_command - Send a command through the device context. 11243eab3d9eSThomas Hellstrom * 11253eab3d9eSThomas Hellstrom * @man: The command buffer manager. 11263eab3d9eSThomas Hellstrom * @command: Pointer to the command to send. 11273eab3d9eSThomas Hellstrom * @size: Size of the command. 11283eab3d9eSThomas Hellstrom * 11293eab3d9eSThomas Hellstrom * Synchronously sends a device context command. 11303eab3d9eSThomas Hellstrom */ 11313eab3d9eSThomas Hellstrom static int vmw_cmdbuf_send_device_command(struct vmw_cmdbuf_man *man, 11323eab3d9eSThomas Hellstrom const void *command, 11333eab3d9eSThomas Hellstrom size_t size) 11343eab3d9eSThomas Hellstrom { 11353eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header *header; 11363eab3d9eSThomas Hellstrom int status; 11373eab3d9eSThomas Hellstrom void *cmd = vmw_cmdbuf_alloc(man, size, false, &header); 11383eab3d9eSThomas Hellstrom 11393eab3d9eSThomas Hellstrom if (IS_ERR(cmd)) 11403eab3d9eSThomas Hellstrom return PTR_ERR(cmd); 11413eab3d9eSThomas Hellstrom 11423eab3d9eSThomas Hellstrom memcpy(cmd, command, size); 11433eab3d9eSThomas Hellstrom header->cb_header->length = size; 11443eab3d9eSThomas Hellstrom header->cb_context = SVGA_CB_CONTEXT_DEVICE; 1145ef369904SThomas Hellstrom spin_lock(&man->lock); 11463eab3d9eSThomas Hellstrom status = vmw_cmdbuf_header_submit(header); 1147ef369904SThomas Hellstrom spin_unlock(&man->lock); 11483eab3d9eSThomas Hellstrom vmw_cmdbuf_header_free(header); 11493eab3d9eSThomas Hellstrom 11503eab3d9eSThomas Hellstrom if (status != SVGA_CB_STATUS_COMPLETED) { 11513eab3d9eSThomas Hellstrom DRM_ERROR("Device context command failed with status %d\n", 11523eab3d9eSThomas Hellstrom status); 11533eab3d9eSThomas Hellstrom return -EINVAL; 11543eab3d9eSThomas Hellstrom } 11553eab3d9eSThomas Hellstrom 11563eab3d9eSThomas Hellstrom return 0; 11573eab3d9eSThomas Hellstrom } 11583eab3d9eSThomas Hellstrom 11593eab3d9eSThomas Hellstrom /** 116065b97a2bSThomas Hellstrom * vmw_cmdbuf_preempt - Send a preempt command through the device 116165b97a2bSThomas Hellstrom * context. 116265b97a2bSThomas Hellstrom * 116365b97a2bSThomas Hellstrom * @man: The command buffer manager. 116465b97a2bSThomas Hellstrom * 116565b97a2bSThomas Hellstrom * Synchronously sends a preempt command. 116665b97a2bSThomas Hellstrom */ 116765b97a2bSThomas Hellstrom static int vmw_cmdbuf_preempt(struct vmw_cmdbuf_man *man, u32 context) 116865b97a2bSThomas Hellstrom { 116965b97a2bSThomas Hellstrom struct { 117065b97a2bSThomas Hellstrom uint32 id; 117165b97a2bSThomas Hellstrom SVGADCCmdPreempt body; 117265b97a2bSThomas Hellstrom } __packed cmd; 117365b97a2bSThomas Hellstrom 117465b97a2bSThomas Hellstrom cmd.id = SVGA_DC_CMD_PREEMPT; 117565b97a2bSThomas Hellstrom cmd.body.context = SVGA_CB_CONTEXT_0 + context; 117665b97a2bSThomas Hellstrom cmd.body.ignoreIDZero = 0; 117765b97a2bSThomas Hellstrom 117865b97a2bSThomas Hellstrom return vmw_cmdbuf_send_device_command(man, &cmd, sizeof(cmd)); 117965b97a2bSThomas Hellstrom } 118065b97a2bSThomas Hellstrom 118165b97a2bSThomas Hellstrom 118265b97a2bSThomas Hellstrom /** 11833eab3d9eSThomas Hellstrom * vmw_cmdbuf_startstop - Send a start / stop command through the device 11843eab3d9eSThomas Hellstrom * context. 11853eab3d9eSThomas Hellstrom * 11863eab3d9eSThomas Hellstrom * @man: The command buffer manager. 11873eab3d9eSThomas Hellstrom * @enable: Whether to enable or disable the context. 11883eab3d9eSThomas Hellstrom * 11893eab3d9eSThomas Hellstrom * Synchronously sends a device start / stop context command. 11903eab3d9eSThomas Hellstrom */ 119165b97a2bSThomas Hellstrom static int vmw_cmdbuf_startstop(struct vmw_cmdbuf_man *man, u32 context, 11923eab3d9eSThomas Hellstrom bool enable) 11933eab3d9eSThomas Hellstrom { 11943eab3d9eSThomas Hellstrom struct { 11953eab3d9eSThomas Hellstrom uint32 id; 11963eab3d9eSThomas Hellstrom SVGADCCmdStartStop body; 11973eab3d9eSThomas Hellstrom } __packed cmd; 11983eab3d9eSThomas Hellstrom 11993eab3d9eSThomas Hellstrom cmd.id = SVGA_DC_CMD_START_STOP_CONTEXT; 12003eab3d9eSThomas Hellstrom cmd.body.enable = (enable) ? 1 : 0; 120165b97a2bSThomas Hellstrom cmd.body.context = SVGA_CB_CONTEXT_0 + context; 12023eab3d9eSThomas Hellstrom 12033eab3d9eSThomas Hellstrom return vmw_cmdbuf_send_device_command(man, &cmd, sizeof(cmd)); 12043eab3d9eSThomas Hellstrom } 12053eab3d9eSThomas Hellstrom 12063eab3d9eSThomas Hellstrom /** 12073eab3d9eSThomas Hellstrom * vmw_cmdbuf_set_pool_size - Set command buffer manager sizes 12083eab3d9eSThomas Hellstrom * 12093eab3d9eSThomas Hellstrom * @man: The command buffer manager. 12103eab3d9eSThomas Hellstrom * @size: The size of the main space pool. 12113eab3d9eSThomas Hellstrom * @default_size: The default size of the command buffer for small kernel 12123eab3d9eSThomas Hellstrom * submissions. 12133eab3d9eSThomas Hellstrom * 12143eab3d9eSThomas Hellstrom * Set the size and allocate the main command buffer space pool, 12153eab3d9eSThomas Hellstrom * as well as the default size of the command buffer for 12163eab3d9eSThomas Hellstrom * small kernel submissions. If successful, this enables large command 12173eab3d9eSThomas Hellstrom * submissions. Note that this function requires that rudimentary command 12183eab3d9eSThomas Hellstrom * submission is already available and that the MOB memory manager is alive. 12193eab3d9eSThomas Hellstrom * Returns 0 on success. Negative error code on failure. 12203eab3d9eSThomas Hellstrom */ 12213eab3d9eSThomas Hellstrom int vmw_cmdbuf_set_pool_size(struct vmw_cmdbuf_man *man, 12223eab3d9eSThomas Hellstrom size_t size, size_t default_size) 12233eab3d9eSThomas Hellstrom { 12243eab3d9eSThomas Hellstrom struct vmw_private *dev_priv = man->dev_priv; 12253eab3d9eSThomas Hellstrom bool dummy; 12263eab3d9eSThomas Hellstrom int ret; 12273eab3d9eSThomas Hellstrom 12283eab3d9eSThomas Hellstrom if (man->has_pool) 12293eab3d9eSThomas Hellstrom return -EINVAL; 12303eab3d9eSThomas Hellstrom 12313eab3d9eSThomas Hellstrom /* First, try to allocate a huge chunk of DMA memory */ 12323eab3d9eSThomas Hellstrom size = PAGE_ALIGN(size); 12333eab3d9eSThomas Hellstrom man->map = dma_alloc_coherent(&dev_priv->dev->pdev->dev, size, 12343eab3d9eSThomas Hellstrom &man->handle, GFP_KERNEL); 12353eab3d9eSThomas Hellstrom if (man->map) { 12363eab3d9eSThomas Hellstrom man->using_mob = false; 12373eab3d9eSThomas Hellstrom } else { 12383eab3d9eSThomas Hellstrom /* 12393eab3d9eSThomas Hellstrom * DMA memory failed. If we can have command buffers in a 12403eab3d9eSThomas Hellstrom * MOB, try to use that instead. Note that this will 12413eab3d9eSThomas Hellstrom * actually call into the already enabled manager, when 12423eab3d9eSThomas Hellstrom * binding the MOB. 12433eab3d9eSThomas Hellstrom */ 124481a00960SThomas Hellstrom if (!(dev_priv->capabilities & SVGA_CAP_DX) || 124581a00960SThomas Hellstrom !dev_priv->has_mob) 12463eab3d9eSThomas Hellstrom return -ENOMEM; 12473eab3d9eSThomas Hellstrom 1248*b254557cSChristian König ret = vmw_bo_create_kernel(dev_priv, size, 1249*b254557cSChristian König &vmw_mob_placement, 12503eab3d9eSThomas Hellstrom &man->cmd_space); 12513eab3d9eSThomas Hellstrom if (ret) 12523eab3d9eSThomas Hellstrom return ret; 12533eab3d9eSThomas Hellstrom 12543eab3d9eSThomas Hellstrom man->using_mob = true; 12553eab3d9eSThomas Hellstrom ret = ttm_bo_kmap(man->cmd_space, 0, size >> PAGE_SHIFT, 12563eab3d9eSThomas Hellstrom &man->map_obj); 12573eab3d9eSThomas Hellstrom if (ret) 12583eab3d9eSThomas Hellstrom goto out_no_map; 12593eab3d9eSThomas Hellstrom 12603eab3d9eSThomas Hellstrom man->map = ttm_kmap_obj_virtual(&man->map_obj, &dummy); 12613eab3d9eSThomas Hellstrom } 12623eab3d9eSThomas Hellstrom 12633eab3d9eSThomas Hellstrom man->size = size; 12643eab3d9eSThomas Hellstrom drm_mm_init(&man->mm, 0, size >> PAGE_SHIFT); 12653eab3d9eSThomas Hellstrom 12663eab3d9eSThomas Hellstrom man->has_pool = true; 126709dc1387SThomas Hellstrom 126809dc1387SThomas Hellstrom /* 126909dc1387SThomas Hellstrom * For now, set the default size to VMW_CMDBUF_INLINE_SIZE to 127009dc1387SThomas Hellstrom * prevent deadlocks from happening when vmw_cmdbuf_space_pool() 127109dc1387SThomas Hellstrom * needs to wait for space and we block on further command 127209dc1387SThomas Hellstrom * submissions to be able to free up space. 127309dc1387SThomas Hellstrom */ 127409dc1387SThomas Hellstrom man->default_size = VMW_CMDBUF_INLINE_SIZE; 12753eab3d9eSThomas Hellstrom DRM_INFO("Using command buffers with %s pool.\n", 12763eab3d9eSThomas Hellstrom (man->using_mob) ? "MOB" : "DMA"); 12773eab3d9eSThomas Hellstrom 12783eab3d9eSThomas Hellstrom return 0; 12793eab3d9eSThomas Hellstrom 12803eab3d9eSThomas Hellstrom out_no_map: 12816034d9d4SThomas Zimmermann if (man->using_mob) { 12826034d9d4SThomas Zimmermann ttm_bo_put(man->cmd_space); 12836034d9d4SThomas Zimmermann man->cmd_space = NULL; 12846034d9d4SThomas Zimmermann } 12853eab3d9eSThomas Hellstrom 12863eab3d9eSThomas Hellstrom return ret; 12873eab3d9eSThomas Hellstrom } 12883eab3d9eSThomas Hellstrom 12893eab3d9eSThomas Hellstrom /** 12903eab3d9eSThomas Hellstrom * vmw_cmdbuf_man_create: Create a command buffer manager and enable it for 12913eab3d9eSThomas Hellstrom * inline command buffer submissions only. 12923eab3d9eSThomas Hellstrom * 12933eab3d9eSThomas Hellstrom * @dev_priv: Pointer to device private structure. 12943eab3d9eSThomas Hellstrom * 12953eab3d9eSThomas Hellstrom * Returns a pointer to a cummand buffer manager to success or error pointer 12963eab3d9eSThomas Hellstrom * on failure. The command buffer manager will be enabled for submissions of 12973eab3d9eSThomas Hellstrom * size VMW_CMDBUF_INLINE_SIZE only. 12983eab3d9eSThomas Hellstrom */ 12993eab3d9eSThomas Hellstrom struct vmw_cmdbuf_man *vmw_cmdbuf_man_create(struct vmw_private *dev_priv) 13003eab3d9eSThomas Hellstrom { 13013eab3d9eSThomas Hellstrom struct vmw_cmdbuf_man *man; 13023eab3d9eSThomas Hellstrom struct vmw_cmdbuf_context *ctx; 130365b97a2bSThomas Hellstrom unsigned int i; 13043eab3d9eSThomas Hellstrom int ret; 13053eab3d9eSThomas Hellstrom 13063eab3d9eSThomas Hellstrom if (!(dev_priv->capabilities & SVGA_CAP_COMMAND_BUFFERS)) 13073eab3d9eSThomas Hellstrom return ERR_PTR(-ENOSYS); 13083eab3d9eSThomas Hellstrom 13093eab3d9eSThomas Hellstrom man = kzalloc(sizeof(*man), GFP_KERNEL); 13103eab3d9eSThomas Hellstrom if (!man) 13113eab3d9eSThomas Hellstrom return ERR_PTR(-ENOMEM); 13123eab3d9eSThomas Hellstrom 1313dc366364SThomas Hellstrom man->num_contexts = (dev_priv->capabilities & SVGA_CAP_HP_CMD_QUEUE) ? 1314dc366364SThomas Hellstrom 2 : 1; 13153eab3d9eSThomas Hellstrom man->headers = dma_pool_create("vmwgfx cmdbuf", 13163eab3d9eSThomas Hellstrom &dev_priv->dev->pdev->dev, 13173eab3d9eSThomas Hellstrom sizeof(SVGACBHeader), 13183eab3d9eSThomas Hellstrom 64, PAGE_SIZE); 13193eab3d9eSThomas Hellstrom if (!man->headers) { 13203eab3d9eSThomas Hellstrom ret = -ENOMEM; 13213eab3d9eSThomas Hellstrom goto out_no_pool; 13223eab3d9eSThomas Hellstrom } 13233eab3d9eSThomas Hellstrom 13243eab3d9eSThomas Hellstrom man->dheaders = dma_pool_create("vmwgfx inline cmdbuf", 13253eab3d9eSThomas Hellstrom &dev_priv->dev->pdev->dev, 13263eab3d9eSThomas Hellstrom sizeof(struct vmw_cmdbuf_dheader), 13273eab3d9eSThomas Hellstrom 64, PAGE_SIZE); 13283eab3d9eSThomas Hellstrom if (!man->dheaders) { 13293eab3d9eSThomas Hellstrom ret = -ENOMEM; 13303eab3d9eSThomas Hellstrom goto out_no_dpool; 13313eab3d9eSThomas Hellstrom } 13323eab3d9eSThomas Hellstrom 13333eab3d9eSThomas Hellstrom for_each_cmdbuf_ctx(man, i, ctx) 13343eab3d9eSThomas Hellstrom vmw_cmdbuf_ctx_init(ctx); 13353eab3d9eSThomas Hellstrom 13363eab3d9eSThomas Hellstrom INIT_LIST_HEAD(&man->error); 13373eab3d9eSThomas Hellstrom spin_lock_init(&man->lock); 13383eab3d9eSThomas Hellstrom mutex_init(&man->cur_mutex); 13393eab3d9eSThomas Hellstrom mutex_init(&man->space_mutex); 134065b97a2bSThomas Hellstrom mutex_init(&man->error_mutex); 13413eab3d9eSThomas Hellstrom man->default_size = VMW_CMDBUF_INLINE_SIZE; 13423eab3d9eSThomas Hellstrom init_waitqueue_head(&man->alloc_queue); 13433eab3d9eSThomas Hellstrom init_waitqueue_head(&man->idle_queue); 13443eab3d9eSThomas Hellstrom man->dev_priv = dev_priv; 13453eab3d9eSThomas Hellstrom man->max_hw_submitted = SVGA_CB_MAX_QUEUED_PER_CONTEXT - 1; 13463eab3d9eSThomas Hellstrom INIT_WORK(&man->work, &vmw_cmdbuf_work_func); 13473eab3d9eSThomas Hellstrom vmw_generic_waiter_add(dev_priv, SVGA_IRQFLAG_ERROR, 13483eab3d9eSThomas Hellstrom &dev_priv->error_waiters); 1349dc366364SThomas Hellstrom ret = vmw_cmdbuf_startstop(man, 0, true); 13503eab3d9eSThomas Hellstrom if (ret) { 1351dc366364SThomas Hellstrom DRM_ERROR("Failed starting command buffer contexts\n"); 13523eab3d9eSThomas Hellstrom vmw_cmdbuf_man_destroy(man); 13533eab3d9eSThomas Hellstrom return ERR_PTR(ret); 13543eab3d9eSThomas Hellstrom } 13553eab3d9eSThomas Hellstrom 13563eab3d9eSThomas Hellstrom return man; 13573eab3d9eSThomas Hellstrom 13583eab3d9eSThomas Hellstrom out_no_dpool: 13593eab3d9eSThomas Hellstrom dma_pool_destroy(man->headers); 13603eab3d9eSThomas Hellstrom out_no_pool: 13613eab3d9eSThomas Hellstrom kfree(man); 13623eab3d9eSThomas Hellstrom 13633eab3d9eSThomas Hellstrom return ERR_PTR(ret); 13643eab3d9eSThomas Hellstrom } 13653eab3d9eSThomas Hellstrom 13663eab3d9eSThomas Hellstrom /** 13673eab3d9eSThomas Hellstrom * vmw_cmdbuf_remove_pool - Take down the main buffer space pool. 13683eab3d9eSThomas Hellstrom * 13693eab3d9eSThomas Hellstrom * @man: Pointer to a command buffer manager. 13703eab3d9eSThomas Hellstrom * 13713eab3d9eSThomas Hellstrom * This function removes the main buffer space pool, and should be called 13723eab3d9eSThomas Hellstrom * before MOB memory management is removed. When this function has been called, 13733eab3d9eSThomas Hellstrom * only small command buffer submissions of size VMW_CMDBUF_INLINE_SIZE or 13743eab3d9eSThomas Hellstrom * less are allowed, and the default size of the command buffer for small kernel 13753eab3d9eSThomas Hellstrom * submissions is also set to this size. 13763eab3d9eSThomas Hellstrom */ 13773eab3d9eSThomas Hellstrom void vmw_cmdbuf_remove_pool(struct vmw_cmdbuf_man *man) 13783eab3d9eSThomas Hellstrom { 13793eab3d9eSThomas Hellstrom if (!man->has_pool) 13803eab3d9eSThomas Hellstrom return; 13813eab3d9eSThomas Hellstrom 13823eab3d9eSThomas Hellstrom man->has_pool = false; 13833eab3d9eSThomas Hellstrom man->default_size = VMW_CMDBUF_INLINE_SIZE; 13843eab3d9eSThomas Hellstrom (void) vmw_cmdbuf_idle(man, false, 10*HZ); 13853eab3d9eSThomas Hellstrom if (man->using_mob) { 13863eab3d9eSThomas Hellstrom (void) ttm_bo_kunmap(&man->map_obj); 13876034d9d4SThomas Zimmermann ttm_bo_put(man->cmd_space); 13886034d9d4SThomas Zimmermann man->cmd_space = NULL; 13893eab3d9eSThomas Hellstrom } else { 13903eab3d9eSThomas Hellstrom dma_free_coherent(&man->dev_priv->dev->pdev->dev, 13913eab3d9eSThomas Hellstrom man->size, man->map, man->handle); 13923eab3d9eSThomas Hellstrom } 13933eab3d9eSThomas Hellstrom } 13943eab3d9eSThomas Hellstrom 13953eab3d9eSThomas Hellstrom /** 13963eab3d9eSThomas Hellstrom * vmw_cmdbuf_man_destroy - Take down a command buffer manager. 13973eab3d9eSThomas Hellstrom * 13983eab3d9eSThomas Hellstrom * @man: Pointer to a command buffer manager. 13993eab3d9eSThomas Hellstrom * 14003eab3d9eSThomas Hellstrom * This function idles and then destroys a command buffer manager. 14013eab3d9eSThomas Hellstrom */ 14023eab3d9eSThomas Hellstrom void vmw_cmdbuf_man_destroy(struct vmw_cmdbuf_man *man) 14033eab3d9eSThomas Hellstrom { 14043eab3d9eSThomas Hellstrom WARN_ON_ONCE(man->has_pool); 14053eab3d9eSThomas Hellstrom (void) vmw_cmdbuf_idle(man, false, 10*HZ); 140665b97a2bSThomas Hellstrom 1407dc366364SThomas Hellstrom if (vmw_cmdbuf_startstop(man, 0, false)) 1408dc366364SThomas Hellstrom DRM_ERROR("Failed stopping command buffer contexts.\n"); 14093eab3d9eSThomas Hellstrom 14103eab3d9eSThomas Hellstrom vmw_generic_waiter_remove(man->dev_priv, SVGA_IRQFLAG_ERROR, 14113eab3d9eSThomas Hellstrom &man->dev_priv->error_waiters); 14123eab3d9eSThomas Hellstrom (void) cancel_work_sync(&man->work); 14133eab3d9eSThomas Hellstrom dma_pool_destroy(man->dheaders); 14143eab3d9eSThomas Hellstrom dma_pool_destroy(man->headers); 14153eab3d9eSThomas Hellstrom mutex_destroy(&man->cur_mutex); 14163eab3d9eSThomas Hellstrom mutex_destroy(&man->space_mutex); 141765b97a2bSThomas Hellstrom mutex_destroy(&man->error_mutex); 14183eab3d9eSThomas Hellstrom kfree(man); 14193eab3d9eSThomas Hellstrom } 1420