13eab3d9eSThomas Hellstrom /************************************************************************** 23eab3d9eSThomas Hellstrom * 33eab3d9eSThomas Hellstrom * Copyright © 2015 VMware, Inc., Palo Alto, CA., USA 43eab3d9eSThomas Hellstrom * All Rights Reserved. 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 28*008be682SMasahiro Yamada #include <drm/ttm/ttm_bo_api.h> 29*008be682SMasahiro Yamada 303eab3d9eSThomas Hellstrom #include "vmwgfx_drv.h" 313eab3d9eSThomas Hellstrom 323eab3d9eSThomas Hellstrom /* 333eab3d9eSThomas Hellstrom * Size of inline command buffers. Try to make sure that a page size is a 343eab3d9eSThomas Hellstrom * multiple of the DMA pool allocation size. 353eab3d9eSThomas Hellstrom */ 363eab3d9eSThomas Hellstrom #define VMW_CMDBUF_INLINE_ALIGN 64 379b590783SThomas Hellstrom #define VMW_CMDBUF_INLINE_SIZE \ 389b590783SThomas Hellstrom (1024 - ALIGN(sizeof(SVGACBHeader), VMW_CMDBUF_INLINE_ALIGN)) 393eab3d9eSThomas Hellstrom 403eab3d9eSThomas Hellstrom /** 413eab3d9eSThomas Hellstrom * struct vmw_cmdbuf_context - Command buffer context queues 423eab3d9eSThomas Hellstrom * 433eab3d9eSThomas Hellstrom * @submitted: List of command buffers that have been submitted to the 443eab3d9eSThomas Hellstrom * manager but not yet submitted to hardware. 453eab3d9eSThomas Hellstrom * @hw_submitted: List of command buffers submitted to hardware. 463eab3d9eSThomas Hellstrom * @preempted: List of preempted command buffers. 473eab3d9eSThomas Hellstrom * @num_hw_submitted: Number of buffers currently being processed by hardware 483eab3d9eSThomas Hellstrom */ 493eab3d9eSThomas Hellstrom struct vmw_cmdbuf_context { 503eab3d9eSThomas Hellstrom struct list_head submitted; 513eab3d9eSThomas Hellstrom struct list_head hw_submitted; 523eab3d9eSThomas Hellstrom struct list_head preempted; 533eab3d9eSThomas Hellstrom unsigned num_hw_submitted; 543eab3d9eSThomas Hellstrom }; 553eab3d9eSThomas Hellstrom 563eab3d9eSThomas Hellstrom /** 573eab3d9eSThomas Hellstrom * struct vmw_cmdbuf_man: - Command buffer manager 583eab3d9eSThomas Hellstrom * 593eab3d9eSThomas Hellstrom * @cur_mutex: Mutex protecting the command buffer used for incremental small 603eab3d9eSThomas Hellstrom * kernel command submissions, @cur. 613eab3d9eSThomas Hellstrom * @space_mutex: Mutex to protect against starvation when we allocate 623eab3d9eSThomas Hellstrom * main pool buffer space. 633eab3d9eSThomas Hellstrom * @work: A struct work_struct implementeing command buffer error handling. 643eab3d9eSThomas Hellstrom * Immutable. 653eab3d9eSThomas Hellstrom * @dev_priv: Pointer to the device private struct. Immutable. 663eab3d9eSThomas Hellstrom * @ctx: Array of command buffer context queues. The queues and the context 673eab3d9eSThomas Hellstrom * data is protected by @lock. 683eab3d9eSThomas Hellstrom * @error: List of command buffers that have caused device errors. 693eab3d9eSThomas Hellstrom * Protected by @lock. 703eab3d9eSThomas Hellstrom * @mm: Range manager for the command buffer space. Manager allocations and 713eab3d9eSThomas Hellstrom * frees are protected by @lock. 723eab3d9eSThomas Hellstrom * @cmd_space: Buffer object for the command buffer space, unless we were 733eab3d9eSThomas Hellstrom * able to make a contigous coherent DMA memory allocation, @handle. Immutable. 743eab3d9eSThomas Hellstrom * @map_obj: Mapping state for @cmd_space. Immutable. 753eab3d9eSThomas Hellstrom * @map: Pointer to command buffer space. May be a mapped buffer object or 763eab3d9eSThomas Hellstrom * a contigous coherent DMA memory allocation. Immutable. 773eab3d9eSThomas Hellstrom * @cur: Command buffer for small kernel command submissions. Protected by 783eab3d9eSThomas Hellstrom * the @cur_mutex. 793eab3d9eSThomas Hellstrom * @cur_pos: Space already used in @cur. Protected by @cur_mutex. 803eab3d9eSThomas Hellstrom * @default_size: Default size for the @cur command buffer. Immutable. 813eab3d9eSThomas Hellstrom * @max_hw_submitted: Max number of in-flight command buffers the device can 823eab3d9eSThomas Hellstrom * handle. Immutable. 833eab3d9eSThomas Hellstrom * @lock: Spinlock protecting command submission queues. 843eab3d9eSThomas Hellstrom * @header: Pool of DMA memory for device command buffer headers. 853eab3d9eSThomas Hellstrom * Internal protection. 863eab3d9eSThomas Hellstrom * @dheaders: Pool of DMA memory for device command buffer headers with trailing 873eab3d9eSThomas Hellstrom * space for inline data. Internal protection. 883eab3d9eSThomas Hellstrom * @tasklet: Tasklet struct for irq processing. Immutable. 893eab3d9eSThomas Hellstrom * @alloc_queue: Wait queue for processes waiting to allocate command buffer 903eab3d9eSThomas Hellstrom * space. 913eab3d9eSThomas Hellstrom * @idle_queue: Wait queue for processes waiting for command buffer idle. 923eab3d9eSThomas Hellstrom * @irq_on: Whether the process function has requested irq to be turned on. 933eab3d9eSThomas Hellstrom * Protected by @lock. 943eab3d9eSThomas Hellstrom * @using_mob: Whether the command buffer space is a MOB or a contigous DMA 953eab3d9eSThomas Hellstrom * allocation. Immutable. 963eab3d9eSThomas Hellstrom * @has_pool: Has a large pool of DMA memory which allows larger allocations. 973eab3d9eSThomas Hellstrom * Typically this is false only during bootstrap. 983eab3d9eSThomas Hellstrom * @handle: DMA address handle for the command buffer space if @using_mob is 993eab3d9eSThomas Hellstrom * false. Immutable. 1003eab3d9eSThomas Hellstrom * @size: The size of the command buffer space. Immutable. 1013eab3d9eSThomas Hellstrom */ 1023eab3d9eSThomas Hellstrom struct vmw_cmdbuf_man { 1033eab3d9eSThomas Hellstrom struct mutex cur_mutex; 1043eab3d9eSThomas Hellstrom struct mutex space_mutex; 1053eab3d9eSThomas Hellstrom struct work_struct work; 1063eab3d9eSThomas Hellstrom struct vmw_private *dev_priv; 1073eab3d9eSThomas Hellstrom struct vmw_cmdbuf_context ctx[SVGA_CB_CONTEXT_MAX]; 1083eab3d9eSThomas Hellstrom struct list_head error; 1093eab3d9eSThomas Hellstrom struct drm_mm mm; 1103eab3d9eSThomas Hellstrom struct ttm_buffer_object *cmd_space; 1113eab3d9eSThomas Hellstrom struct ttm_bo_kmap_obj map_obj; 1123eab3d9eSThomas Hellstrom u8 *map; 1133eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header *cur; 1143eab3d9eSThomas Hellstrom size_t cur_pos; 1153eab3d9eSThomas Hellstrom size_t default_size; 1163eab3d9eSThomas Hellstrom unsigned max_hw_submitted; 1173eab3d9eSThomas Hellstrom spinlock_t lock; 1183eab3d9eSThomas Hellstrom struct dma_pool *headers; 1193eab3d9eSThomas Hellstrom struct dma_pool *dheaders; 1203eab3d9eSThomas Hellstrom struct tasklet_struct tasklet; 1213eab3d9eSThomas Hellstrom wait_queue_head_t alloc_queue; 1223eab3d9eSThomas Hellstrom wait_queue_head_t idle_queue; 1233eab3d9eSThomas Hellstrom bool irq_on; 1243eab3d9eSThomas Hellstrom bool using_mob; 1253eab3d9eSThomas Hellstrom bool has_pool; 1263eab3d9eSThomas Hellstrom dma_addr_t handle; 1273eab3d9eSThomas Hellstrom size_t size; 1283eab3d9eSThomas Hellstrom }; 1293eab3d9eSThomas Hellstrom 1303eab3d9eSThomas Hellstrom /** 1313eab3d9eSThomas Hellstrom * struct vmw_cmdbuf_header - Command buffer metadata 1323eab3d9eSThomas Hellstrom * 1333eab3d9eSThomas Hellstrom * @man: The command buffer manager. 1343eab3d9eSThomas Hellstrom * @cb_header: Device command buffer header, allocated from a DMA pool. 1353eab3d9eSThomas Hellstrom * @cb_context: The device command buffer context. 1363eab3d9eSThomas Hellstrom * @list: List head for attaching to the manager lists. 1373eab3d9eSThomas Hellstrom * @node: The range manager node. 1383eab3d9eSThomas Hellstrom * @handle. The DMA address of @cb_header. Handed to the device on command 1393eab3d9eSThomas Hellstrom * buffer submission. 1403eab3d9eSThomas Hellstrom * @cmd: Pointer to the command buffer space of this buffer. 1413eab3d9eSThomas Hellstrom * @size: Size of the command buffer space of this buffer. 1423eab3d9eSThomas Hellstrom * @reserved: Reserved space of this buffer. 1433eab3d9eSThomas Hellstrom * @inline_space: Whether inline command buffer space is used. 1443eab3d9eSThomas Hellstrom */ 1453eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header { 1463eab3d9eSThomas Hellstrom struct vmw_cmdbuf_man *man; 1473eab3d9eSThomas Hellstrom SVGACBHeader *cb_header; 1483eab3d9eSThomas Hellstrom SVGACBContext cb_context; 1493eab3d9eSThomas Hellstrom struct list_head list; 1509b590783SThomas Hellstrom struct drm_mm_node node; 1513eab3d9eSThomas Hellstrom dma_addr_t handle; 1523eab3d9eSThomas Hellstrom u8 *cmd; 1533eab3d9eSThomas Hellstrom size_t size; 1543eab3d9eSThomas Hellstrom size_t reserved; 1553eab3d9eSThomas Hellstrom bool inline_space; 1563eab3d9eSThomas Hellstrom }; 1573eab3d9eSThomas Hellstrom 1583eab3d9eSThomas Hellstrom /** 1593eab3d9eSThomas Hellstrom * struct vmw_cmdbuf_dheader - Device command buffer header with inline 1603eab3d9eSThomas Hellstrom * command buffer space. 1613eab3d9eSThomas Hellstrom * 1623eab3d9eSThomas Hellstrom * @cb_header: Device command buffer header. 1633eab3d9eSThomas Hellstrom * @cmd: Inline command buffer space. 1643eab3d9eSThomas Hellstrom */ 1653eab3d9eSThomas Hellstrom struct vmw_cmdbuf_dheader { 1663eab3d9eSThomas Hellstrom SVGACBHeader cb_header; 1673eab3d9eSThomas Hellstrom u8 cmd[VMW_CMDBUF_INLINE_SIZE] __aligned(VMW_CMDBUF_INLINE_ALIGN); 1683eab3d9eSThomas Hellstrom }; 1693eab3d9eSThomas Hellstrom 1703eab3d9eSThomas Hellstrom /** 1713eab3d9eSThomas Hellstrom * struct vmw_cmdbuf_alloc_info - Command buffer space allocation metadata 1723eab3d9eSThomas Hellstrom * 1733eab3d9eSThomas Hellstrom * @page_size: Size of requested command buffer space in pages. 1749b590783SThomas Hellstrom * @node: Pointer to the range manager node. 1759b590783SThomas Hellstrom * @done: True if this allocation has succeeded. 1763eab3d9eSThomas Hellstrom */ 1773eab3d9eSThomas Hellstrom struct vmw_cmdbuf_alloc_info { 1783eab3d9eSThomas Hellstrom size_t page_size; 1793eab3d9eSThomas Hellstrom struct drm_mm_node *node; 1809b590783SThomas Hellstrom bool done; 1813eab3d9eSThomas Hellstrom }; 1823eab3d9eSThomas Hellstrom 1833eab3d9eSThomas Hellstrom /* Loop over each context in the command buffer manager. */ 1843eab3d9eSThomas Hellstrom #define for_each_cmdbuf_ctx(_man, _i, _ctx) \ 1853eab3d9eSThomas Hellstrom for (_i = 0, _ctx = &(_man)->ctx[0]; (_i) < SVGA_CB_CONTEXT_MAX; \ 1863eab3d9eSThomas Hellstrom ++(_i), ++(_ctx)) 1873eab3d9eSThomas Hellstrom 1883eab3d9eSThomas Hellstrom static int vmw_cmdbuf_startstop(struct vmw_cmdbuf_man *man, bool enable); 1893eab3d9eSThomas Hellstrom 1903eab3d9eSThomas Hellstrom 1913eab3d9eSThomas Hellstrom /** 1923eab3d9eSThomas Hellstrom * vmw_cmdbuf_cur_lock - Helper to lock the cur_mutex. 1933eab3d9eSThomas Hellstrom * 1943eab3d9eSThomas Hellstrom * @man: The range manager. 1953eab3d9eSThomas Hellstrom * @interruptible: Whether to wait interruptible when locking. 1963eab3d9eSThomas Hellstrom */ 1973eab3d9eSThomas Hellstrom static int vmw_cmdbuf_cur_lock(struct vmw_cmdbuf_man *man, bool interruptible) 1983eab3d9eSThomas Hellstrom { 1993eab3d9eSThomas Hellstrom if (interruptible) { 2003eab3d9eSThomas Hellstrom if (mutex_lock_interruptible(&man->cur_mutex)) 2013eab3d9eSThomas Hellstrom return -ERESTARTSYS; 2023eab3d9eSThomas Hellstrom } else { 2033eab3d9eSThomas Hellstrom mutex_lock(&man->cur_mutex); 2043eab3d9eSThomas Hellstrom } 2053eab3d9eSThomas Hellstrom 2063eab3d9eSThomas Hellstrom return 0; 2073eab3d9eSThomas Hellstrom } 2083eab3d9eSThomas Hellstrom 2093eab3d9eSThomas Hellstrom /** 2103eab3d9eSThomas Hellstrom * vmw_cmdbuf_cur_unlock - Helper to unlock the cur_mutex. 2113eab3d9eSThomas Hellstrom * 2123eab3d9eSThomas Hellstrom * @man: The range manager. 2133eab3d9eSThomas Hellstrom */ 2143eab3d9eSThomas Hellstrom static void vmw_cmdbuf_cur_unlock(struct vmw_cmdbuf_man *man) 2153eab3d9eSThomas Hellstrom { 2163eab3d9eSThomas Hellstrom mutex_unlock(&man->cur_mutex); 2173eab3d9eSThomas Hellstrom } 2183eab3d9eSThomas Hellstrom 2193eab3d9eSThomas Hellstrom /** 2203eab3d9eSThomas Hellstrom * vmw_cmdbuf_header_inline_free - Free a struct vmw_cmdbuf_header that has 2213eab3d9eSThomas Hellstrom * been used for the device context with inline command buffers. 2223eab3d9eSThomas Hellstrom * Need not be called locked. 2233eab3d9eSThomas Hellstrom * 2243eab3d9eSThomas Hellstrom * @header: Pointer to the header to free. 2253eab3d9eSThomas Hellstrom */ 2263eab3d9eSThomas Hellstrom static void vmw_cmdbuf_header_inline_free(struct vmw_cmdbuf_header *header) 2273eab3d9eSThomas Hellstrom { 2283eab3d9eSThomas Hellstrom struct vmw_cmdbuf_dheader *dheader; 2293eab3d9eSThomas Hellstrom 2303eab3d9eSThomas Hellstrom if (WARN_ON_ONCE(!header->inline_space)) 2313eab3d9eSThomas Hellstrom return; 2323eab3d9eSThomas Hellstrom 2333eab3d9eSThomas Hellstrom dheader = container_of(header->cb_header, struct vmw_cmdbuf_dheader, 2343eab3d9eSThomas Hellstrom cb_header); 2353eab3d9eSThomas Hellstrom dma_pool_free(header->man->dheaders, dheader, header->handle); 2363eab3d9eSThomas Hellstrom kfree(header); 2373eab3d9eSThomas Hellstrom } 2383eab3d9eSThomas Hellstrom 2393eab3d9eSThomas Hellstrom /** 2403eab3d9eSThomas Hellstrom * __vmw_cmdbuf_header_free - Free a struct vmw_cmdbuf_header and its 2413eab3d9eSThomas Hellstrom * associated structures. 2423eab3d9eSThomas Hellstrom * 2433eab3d9eSThomas Hellstrom * header: Pointer to the header to free. 2443eab3d9eSThomas Hellstrom * 2453eab3d9eSThomas Hellstrom * For internal use. Must be called with man::lock held. 2463eab3d9eSThomas Hellstrom */ 2473eab3d9eSThomas Hellstrom static void __vmw_cmdbuf_header_free(struct vmw_cmdbuf_header *header) 2483eab3d9eSThomas Hellstrom { 2493eab3d9eSThomas Hellstrom struct vmw_cmdbuf_man *man = header->man; 2503eab3d9eSThomas Hellstrom 251fb89ac51SThomas Hellstrom lockdep_assert_held_once(&man->lock); 2523eab3d9eSThomas Hellstrom 2533eab3d9eSThomas Hellstrom if (header->inline_space) { 2543eab3d9eSThomas Hellstrom vmw_cmdbuf_header_inline_free(header); 2553eab3d9eSThomas Hellstrom return; 2563eab3d9eSThomas Hellstrom } 2573eab3d9eSThomas Hellstrom 2589b590783SThomas Hellstrom drm_mm_remove_node(&header->node); 2593eab3d9eSThomas Hellstrom wake_up_all(&man->alloc_queue); 2603eab3d9eSThomas Hellstrom if (header->cb_header) 2613eab3d9eSThomas Hellstrom dma_pool_free(man->headers, header->cb_header, 2623eab3d9eSThomas Hellstrom header->handle); 2633eab3d9eSThomas Hellstrom kfree(header); 2643eab3d9eSThomas Hellstrom } 2653eab3d9eSThomas Hellstrom 2663eab3d9eSThomas Hellstrom /** 2673eab3d9eSThomas Hellstrom * vmw_cmdbuf_header_free - Free a struct vmw_cmdbuf_header and its 2683eab3d9eSThomas Hellstrom * associated structures. 2693eab3d9eSThomas Hellstrom * 2703eab3d9eSThomas Hellstrom * @header: Pointer to the header to free. 2713eab3d9eSThomas Hellstrom */ 2723eab3d9eSThomas Hellstrom void vmw_cmdbuf_header_free(struct vmw_cmdbuf_header *header) 2733eab3d9eSThomas Hellstrom { 2743eab3d9eSThomas Hellstrom struct vmw_cmdbuf_man *man = header->man; 2753eab3d9eSThomas Hellstrom 2763eab3d9eSThomas Hellstrom /* Avoid locking if inline_space */ 2773eab3d9eSThomas Hellstrom if (header->inline_space) { 2783eab3d9eSThomas Hellstrom vmw_cmdbuf_header_inline_free(header); 2793eab3d9eSThomas Hellstrom return; 2803eab3d9eSThomas Hellstrom } 2813eab3d9eSThomas Hellstrom spin_lock_bh(&man->lock); 2823eab3d9eSThomas Hellstrom __vmw_cmdbuf_header_free(header); 2833eab3d9eSThomas Hellstrom spin_unlock_bh(&man->lock); 2843eab3d9eSThomas Hellstrom } 2853eab3d9eSThomas Hellstrom 2863eab3d9eSThomas Hellstrom 2873eab3d9eSThomas Hellstrom /** 2883eab3d9eSThomas Hellstrom * vmw_cmbuf_header_submit: Submit a command buffer to hardware. 2893eab3d9eSThomas Hellstrom * 2903eab3d9eSThomas Hellstrom * @header: The header of the buffer to submit. 2913eab3d9eSThomas Hellstrom */ 2923eab3d9eSThomas Hellstrom static int vmw_cmdbuf_header_submit(struct vmw_cmdbuf_header *header) 2933eab3d9eSThomas Hellstrom { 2943eab3d9eSThomas Hellstrom struct vmw_cmdbuf_man *man = header->man; 2953eab3d9eSThomas Hellstrom u32 val; 2963eab3d9eSThomas Hellstrom 2970e7c875dSPaul Bolle val = upper_32_bits(header->handle); 2983eab3d9eSThomas Hellstrom vmw_write(man->dev_priv, SVGA_REG_COMMAND_HIGH, val); 2992e3cc8cfSThomas Hellstrom 3000e7c875dSPaul Bolle val = lower_32_bits(header->handle); 3013eab3d9eSThomas Hellstrom val |= header->cb_context & SVGA_CB_CONTEXT_MASK; 3023eab3d9eSThomas Hellstrom vmw_write(man->dev_priv, SVGA_REG_COMMAND_LOW, val); 3033eab3d9eSThomas Hellstrom 3043eab3d9eSThomas Hellstrom return header->cb_header->status; 3053eab3d9eSThomas Hellstrom } 3063eab3d9eSThomas Hellstrom 3073eab3d9eSThomas Hellstrom /** 3083eab3d9eSThomas Hellstrom * vmw_cmdbuf_ctx_init: Initialize a command buffer context. 3093eab3d9eSThomas Hellstrom * 3103eab3d9eSThomas Hellstrom * @ctx: The command buffer context to initialize 3113eab3d9eSThomas Hellstrom */ 3123eab3d9eSThomas Hellstrom static void vmw_cmdbuf_ctx_init(struct vmw_cmdbuf_context *ctx) 3133eab3d9eSThomas Hellstrom { 3143eab3d9eSThomas Hellstrom INIT_LIST_HEAD(&ctx->hw_submitted); 3153eab3d9eSThomas Hellstrom INIT_LIST_HEAD(&ctx->submitted); 3163eab3d9eSThomas Hellstrom INIT_LIST_HEAD(&ctx->preempted); 3173eab3d9eSThomas Hellstrom ctx->num_hw_submitted = 0; 3183eab3d9eSThomas Hellstrom } 3193eab3d9eSThomas Hellstrom 3203eab3d9eSThomas Hellstrom /** 3213eab3d9eSThomas Hellstrom * vmw_cmdbuf_ctx_submit: Submit command buffers from a command buffer 3223eab3d9eSThomas Hellstrom * context. 3233eab3d9eSThomas Hellstrom * 3243eab3d9eSThomas Hellstrom * @man: The command buffer manager. 3253eab3d9eSThomas Hellstrom * @ctx: The command buffer context. 3263eab3d9eSThomas Hellstrom * 3273eab3d9eSThomas Hellstrom * Submits command buffers to hardware until there are no more command 3283eab3d9eSThomas Hellstrom * buffers to submit or the hardware can't handle more command buffers. 3293eab3d9eSThomas Hellstrom */ 3303eab3d9eSThomas Hellstrom static void vmw_cmdbuf_ctx_submit(struct vmw_cmdbuf_man *man, 3313eab3d9eSThomas Hellstrom struct vmw_cmdbuf_context *ctx) 3323eab3d9eSThomas Hellstrom { 3333eab3d9eSThomas Hellstrom while (ctx->num_hw_submitted < man->max_hw_submitted && 3343eab3d9eSThomas Hellstrom !list_empty(&ctx->submitted)) { 3353eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header *entry; 3363eab3d9eSThomas Hellstrom SVGACBStatus status; 3373eab3d9eSThomas Hellstrom 3383eab3d9eSThomas Hellstrom entry = list_first_entry(&ctx->submitted, 3393eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header, 3403eab3d9eSThomas Hellstrom list); 3413eab3d9eSThomas Hellstrom 3423eab3d9eSThomas Hellstrom status = vmw_cmdbuf_header_submit(entry); 3433eab3d9eSThomas Hellstrom 3443eab3d9eSThomas Hellstrom /* This should never happen */ 3453eab3d9eSThomas Hellstrom if (WARN_ON_ONCE(status == SVGA_CB_STATUS_QUEUE_FULL)) { 3463eab3d9eSThomas Hellstrom entry->cb_header->status = SVGA_CB_STATUS_NONE; 3473eab3d9eSThomas Hellstrom break; 3483eab3d9eSThomas Hellstrom } 3493eab3d9eSThomas Hellstrom 3503eab3d9eSThomas Hellstrom list_del(&entry->list); 3513eab3d9eSThomas Hellstrom list_add_tail(&entry->list, &ctx->hw_submitted); 3523eab3d9eSThomas Hellstrom ctx->num_hw_submitted++; 3533eab3d9eSThomas Hellstrom } 3543eab3d9eSThomas Hellstrom 3553eab3d9eSThomas Hellstrom } 3563eab3d9eSThomas Hellstrom 3573eab3d9eSThomas Hellstrom /** 3583eab3d9eSThomas Hellstrom * vmw_cmdbuf_ctx_submit: Process a command buffer context. 3593eab3d9eSThomas Hellstrom * 3603eab3d9eSThomas Hellstrom * @man: The command buffer manager. 3613eab3d9eSThomas Hellstrom * @ctx: The command buffer context. 3623eab3d9eSThomas Hellstrom * 3633eab3d9eSThomas Hellstrom * Submit command buffers to hardware if possible, and process finished 3643eab3d9eSThomas Hellstrom * buffers. Typically freeing them, but on preemption or error take 3653eab3d9eSThomas Hellstrom * appropriate action. Wake up waiters if appropriate. 3663eab3d9eSThomas Hellstrom */ 3673eab3d9eSThomas Hellstrom static void vmw_cmdbuf_ctx_process(struct vmw_cmdbuf_man *man, 3683eab3d9eSThomas Hellstrom struct vmw_cmdbuf_context *ctx, 3693eab3d9eSThomas Hellstrom int *notempty) 3703eab3d9eSThomas Hellstrom { 3713eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header *entry, *next; 3723eab3d9eSThomas Hellstrom 3733eab3d9eSThomas Hellstrom vmw_cmdbuf_ctx_submit(man, ctx); 3743eab3d9eSThomas Hellstrom 3753eab3d9eSThomas Hellstrom list_for_each_entry_safe(entry, next, &ctx->hw_submitted, list) { 3763eab3d9eSThomas Hellstrom SVGACBStatus status = entry->cb_header->status; 3773eab3d9eSThomas Hellstrom 3783eab3d9eSThomas Hellstrom if (status == SVGA_CB_STATUS_NONE) 3793eab3d9eSThomas Hellstrom break; 3803eab3d9eSThomas Hellstrom 3813eab3d9eSThomas Hellstrom list_del(&entry->list); 3823eab3d9eSThomas Hellstrom wake_up_all(&man->idle_queue); 3833eab3d9eSThomas Hellstrom ctx->num_hw_submitted--; 3843eab3d9eSThomas Hellstrom switch (status) { 3853eab3d9eSThomas Hellstrom case SVGA_CB_STATUS_COMPLETED: 3863eab3d9eSThomas Hellstrom __vmw_cmdbuf_header_free(entry); 3873eab3d9eSThomas Hellstrom break; 3883eab3d9eSThomas Hellstrom case SVGA_CB_STATUS_COMMAND_ERROR: 3893eab3d9eSThomas Hellstrom case SVGA_CB_STATUS_CB_HEADER_ERROR: 3903eab3d9eSThomas Hellstrom list_add_tail(&entry->list, &man->error); 3913eab3d9eSThomas Hellstrom schedule_work(&man->work); 3923eab3d9eSThomas Hellstrom break; 3933eab3d9eSThomas Hellstrom case SVGA_CB_STATUS_PREEMPTED: 3943eab3d9eSThomas Hellstrom list_add(&entry->list, &ctx->preempted); 3953eab3d9eSThomas Hellstrom break; 3963eab3d9eSThomas Hellstrom default: 3973eab3d9eSThomas Hellstrom WARN_ONCE(true, "Undefined command buffer status.\n"); 3983eab3d9eSThomas Hellstrom __vmw_cmdbuf_header_free(entry); 3993eab3d9eSThomas Hellstrom break; 4003eab3d9eSThomas Hellstrom } 4013eab3d9eSThomas Hellstrom } 4023eab3d9eSThomas Hellstrom 4033eab3d9eSThomas Hellstrom vmw_cmdbuf_ctx_submit(man, ctx); 4043eab3d9eSThomas Hellstrom if (!list_empty(&ctx->submitted)) 4053eab3d9eSThomas Hellstrom (*notempty)++; 4063eab3d9eSThomas Hellstrom } 4073eab3d9eSThomas Hellstrom 4083eab3d9eSThomas Hellstrom /** 4093eab3d9eSThomas Hellstrom * vmw_cmdbuf_man_process - Process all command buffer contexts and 4103eab3d9eSThomas Hellstrom * switch on and off irqs as appropriate. 4113eab3d9eSThomas Hellstrom * 4123eab3d9eSThomas Hellstrom * @man: The command buffer manager. 4133eab3d9eSThomas Hellstrom * 4143eab3d9eSThomas Hellstrom * Calls vmw_cmdbuf_ctx_process() on all contexts. If any context has 4153eab3d9eSThomas Hellstrom * command buffers left that are not submitted to hardware, Make sure 41609dc1387SThomas Hellstrom * IRQ handling is turned on. Otherwise, make sure it's turned off. 4173eab3d9eSThomas Hellstrom */ 41809dc1387SThomas Hellstrom static void vmw_cmdbuf_man_process(struct vmw_cmdbuf_man *man) 4193eab3d9eSThomas Hellstrom { 42009dc1387SThomas Hellstrom int notempty; 4213eab3d9eSThomas Hellstrom struct vmw_cmdbuf_context *ctx; 4223eab3d9eSThomas Hellstrom int i; 4233eab3d9eSThomas Hellstrom 42409dc1387SThomas Hellstrom retry: 42509dc1387SThomas Hellstrom notempty = 0; 4263eab3d9eSThomas Hellstrom for_each_cmdbuf_ctx(man, i, ctx) 4273eab3d9eSThomas Hellstrom vmw_cmdbuf_ctx_process(man, ctx, ¬empty); 4283eab3d9eSThomas Hellstrom 4293eab3d9eSThomas Hellstrom if (man->irq_on && !notempty) { 4303eab3d9eSThomas Hellstrom vmw_generic_waiter_remove(man->dev_priv, 4313eab3d9eSThomas Hellstrom SVGA_IRQFLAG_COMMAND_BUFFER, 4323eab3d9eSThomas Hellstrom &man->dev_priv->cmdbuf_waiters); 4333eab3d9eSThomas Hellstrom man->irq_on = false; 4343eab3d9eSThomas Hellstrom } else if (!man->irq_on && notempty) { 4353eab3d9eSThomas Hellstrom vmw_generic_waiter_add(man->dev_priv, 4363eab3d9eSThomas Hellstrom SVGA_IRQFLAG_COMMAND_BUFFER, 4373eab3d9eSThomas Hellstrom &man->dev_priv->cmdbuf_waiters); 4383eab3d9eSThomas Hellstrom man->irq_on = true; 4393eab3d9eSThomas Hellstrom 4403eab3d9eSThomas Hellstrom /* Rerun in case we just missed an irq. */ 44109dc1387SThomas Hellstrom goto retry; 4423eab3d9eSThomas Hellstrom } 4433eab3d9eSThomas Hellstrom } 4443eab3d9eSThomas Hellstrom 4453eab3d9eSThomas Hellstrom /** 4463eab3d9eSThomas Hellstrom * vmw_cmdbuf_ctx_add - Schedule a command buffer for submission on a 4473eab3d9eSThomas Hellstrom * command buffer context 4483eab3d9eSThomas Hellstrom * 4493eab3d9eSThomas Hellstrom * @man: The command buffer manager. 4503eab3d9eSThomas Hellstrom * @header: The header of the buffer to submit. 4513eab3d9eSThomas Hellstrom * @cb_context: The command buffer context to use. 4523eab3d9eSThomas Hellstrom * 4533eab3d9eSThomas Hellstrom * This function adds @header to the "submitted" queue of the command 4543eab3d9eSThomas Hellstrom * buffer context identified by @cb_context. It then calls the command buffer 4553eab3d9eSThomas Hellstrom * manager processing to potentially submit the buffer to hardware. 4563eab3d9eSThomas Hellstrom * @man->lock needs to be held when calling this function. 4573eab3d9eSThomas Hellstrom */ 4583eab3d9eSThomas Hellstrom static void vmw_cmdbuf_ctx_add(struct vmw_cmdbuf_man *man, 4593eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header *header, 4603eab3d9eSThomas Hellstrom SVGACBContext cb_context) 4613eab3d9eSThomas Hellstrom { 4623eab3d9eSThomas Hellstrom if (!(header->cb_header->flags & SVGA_CB_FLAG_DX_CONTEXT)) 4633eab3d9eSThomas Hellstrom header->cb_header->dxContext = 0; 4643eab3d9eSThomas Hellstrom header->cb_context = cb_context; 4653eab3d9eSThomas Hellstrom list_add_tail(&header->list, &man->ctx[cb_context].submitted); 4663eab3d9eSThomas Hellstrom 4673eab3d9eSThomas Hellstrom vmw_cmdbuf_man_process(man); 4683eab3d9eSThomas Hellstrom } 4693eab3d9eSThomas Hellstrom 4703eab3d9eSThomas Hellstrom /** 4713eab3d9eSThomas Hellstrom * vmw_cmdbuf_man_tasklet - The main part of the command buffer interrupt 4723eab3d9eSThomas Hellstrom * handler implemented as a tasklet. 4733eab3d9eSThomas Hellstrom * 4743eab3d9eSThomas Hellstrom * @data: Tasklet closure. A pointer to the command buffer manager cast to 4753eab3d9eSThomas Hellstrom * an unsigned long. 4763eab3d9eSThomas Hellstrom * 4773eab3d9eSThomas Hellstrom * The bottom half (tasklet) of the interrupt handler simply calls into the 4783eab3d9eSThomas Hellstrom * command buffer processor to free finished buffers and submit any 4793eab3d9eSThomas Hellstrom * queued buffers to hardware. 4803eab3d9eSThomas Hellstrom */ 4813eab3d9eSThomas Hellstrom static void vmw_cmdbuf_man_tasklet(unsigned long data) 4823eab3d9eSThomas Hellstrom { 4833eab3d9eSThomas Hellstrom struct vmw_cmdbuf_man *man = (struct vmw_cmdbuf_man *) data; 4843eab3d9eSThomas Hellstrom 4853eab3d9eSThomas Hellstrom spin_lock(&man->lock); 48609dc1387SThomas Hellstrom vmw_cmdbuf_man_process(man); 4873eab3d9eSThomas Hellstrom spin_unlock(&man->lock); 4883eab3d9eSThomas Hellstrom } 4893eab3d9eSThomas Hellstrom 4903eab3d9eSThomas Hellstrom /** 4913eab3d9eSThomas Hellstrom * vmw_cmdbuf_work_func - The deferred work function that handles 4923eab3d9eSThomas Hellstrom * command buffer errors. 4933eab3d9eSThomas Hellstrom * 4943eab3d9eSThomas Hellstrom * @work: The work func closure argument. 4953eab3d9eSThomas Hellstrom * 4963eab3d9eSThomas Hellstrom * Restarting the command buffer context after an error requires process 4973eab3d9eSThomas Hellstrom * context, so it is deferred to this work function. 4983eab3d9eSThomas Hellstrom */ 4993eab3d9eSThomas Hellstrom static void vmw_cmdbuf_work_func(struct work_struct *work) 5003eab3d9eSThomas Hellstrom { 5013eab3d9eSThomas Hellstrom struct vmw_cmdbuf_man *man = 5023eab3d9eSThomas Hellstrom container_of(work, struct vmw_cmdbuf_man, work); 5033eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header *entry, *next; 50409dc1387SThomas Hellstrom uint32_t dummy; 5056a5278eeSThomas Hellstrom bool restart = false; 5063eab3d9eSThomas Hellstrom 5073eab3d9eSThomas Hellstrom spin_lock_bh(&man->lock); 5083eab3d9eSThomas Hellstrom list_for_each_entry_safe(entry, next, &man->error, list) { 5093eab3d9eSThomas Hellstrom restart = true; 5103eab3d9eSThomas Hellstrom DRM_ERROR("Command buffer error.\n"); 5113eab3d9eSThomas Hellstrom 5123eab3d9eSThomas Hellstrom list_del(&entry->list); 5133eab3d9eSThomas Hellstrom __vmw_cmdbuf_header_free(entry); 5143eab3d9eSThomas Hellstrom wake_up_all(&man->idle_queue); 5153eab3d9eSThomas Hellstrom } 5163eab3d9eSThomas Hellstrom spin_unlock_bh(&man->lock); 5173eab3d9eSThomas Hellstrom 5183eab3d9eSThomas Hellstrom if (restart && vmw_cmdbuf_startstop(man, true)) 5193eab3d9eSThomas Hellstrom DRM_ERROR("Failed restarting command buffer context 0.\n"); 5203eab3d9eSThomas Hellstrom 52109dc1387SThomas Hellstrom /* Send a new fence in case one was removed */ 52209dc1387SThomas Hellstrom vmw_fifo_send_fence(man->dev_priv, &dummy); 5233eab3d9eSThomas Hellstrom } 5243eab3d9eSThomas Hellstrom 5253eab3d9eSThomas Hellstrom /** 5263eab3d9eSThomas Hellstrom * vmw_cmdbuf_man idle - Check whether the command buffer manager is idle. 5273eab3d9eSThomas Hellstrom * 5283eab3d9eSThomas Hellstrom * @man: The command buffer manager. 5293eab3d9eSThomas Hellstrom * @check_preempted: Check also the preempted queue for pending command buffers. 5303eab3d9eSThomas Hellstrom * 5313eab3d9eSThomas Hellstrom */ 5323eab3d9eSThomas Hellstrom static bool vmw_cmdbuf_man_idle(struct vmw_cmdbuf_man *man, 5333eab3d9eSThomas Hellstrom bool check_preempted) 5343eab3d9eSThomas Hellstrom { 5353eab3d9eSThomas Hellstrom struct vmw_cmdbuf_context *ctx; 5363eab3d9eSThomas Hellstrom bool idle = false; 5373eab3d9eSThomas Hellstrom int i; 5383eab3d9eSThomas Hellstrom 5393eab3d9eSThomas Hellstrom spin_lock_bh(&man->lock); 5403eab3d9eSThomas Hellstrom vmw_cmdbuf_man_process(man); 5413eab3d9eSThomas Hellstrom for_each_cmdbuf_ctx(man, i, ctx) { 5423eab3d9eSThomas Hellstrom if (!list_empty(&ctx->submitted) || 5433eab3d9eSThomas Hellstrom !list_empty(&ctx->hw_submitted) || 5443eab3d9eSThomas Hellstrom (check_preempted && !list_empty(&ctx->preempted))) 5453eab3d9eSThomas Hellstrom goto out_unlock; 5463eab3d9eSThomas Hellstrom } 5473eab3d9eSThomas Hellstrom 5483eab3d9eSThomas Hellstrom idle = list_empty(&man->error); 5493eab3d9eSThomas Hellstrom 5503eab3d9eSThomas Hellstrom out_unlock: 5513eab3d9eSThomas Hellstrom spin_unlock_bh(&man->lock); 5523eab3d9eSThomas Hellstrom 5533eab3d9eSThomas Hellstrom return idle; 5543eab3d9eSThomas Hellstrom } 5553eab3d9eSThomas Hellstrom 5563eab3d9eSThomas Hellstrom /** 5573eab3d9eSThomas Hellstrom * __vmw_cmdbuf_cur_flush - Flush the current command buffer for small kernel 5583eab3d9eSThomas Hellstrom * command submissions 5593eab3d9eSThomas Hellstrom * 5603eab3d9eSThomas Hellstrom * @man: The command buffer manager. 5613eab3d9eSThomas Hellstrom * 5623eab3d9eSThomas Hellstrom * Flushes the current command buffer without allocating a new one. A new one 5633eab3d9eSThomas Hellstrom * is automatically allocated when needed. Call with @man->cur_mutex held. 5643eab3d9eSThomas Hellstrom */ 5653eab3d9eSThomas Hellstrom static void __vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man *man) 5663eab3d9eSThomas Hellstrom { 5673eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header *cur = man->cur; 5683eab3d9eSThomas Hellstrom 5693eab3d9eSThomas Hellstrom WARN_ON(!mutex_is_locked(&man->cur_mutex)); 5703eab3d9eSThomas Hellstrom 5713eab3d9eSThomas Hellstrom if (!cur) 5723eab3d9eSThomas Hellstrom return; 5733eab3d9eSThomas Hellstrom 5743eab3d9eSThomas Hellstrom spin_lock_bh(&man->lock); 5753eab3d9eSThomas Hellstrom if (man->cur_pos == 0) { 5763eab3d9eSThomas Hellstrom __vmw_cmdbuf_header_free(cur); 5773eab3d9eSThomas Hellstrom goto out_unlock; 5783eab3d9eSThomas Hellstrom } 5793eab3d9eSThomas Hellstrom 5803eab3d9eSThomas Hellstrom man->cur->cb_header->length = man->cur_pos; 5813eab3d9eSThomas Hellstrom vmw_cmdbuf_ctx_add(man, man->cur, SVGA_CB_CONTEXT_0); 5823eab3d9eSThomas Hellstrom out_unlock: 5833eab3d9eSThomas Hellstrom spin_unlock_bh(&man->lock); 5843eab3d9eSThomas Hellstrom man->cur = NULL; 5853eab3d9eSThomas Hellstrom man->cur_pos = 0; 5863eab3d9eSThomas Hellstrom } 5873eab3d9eSThomas Hellstrom 5883eab3d9eSThomas Hellstrom /** 5893eab3d9eSThomas Hellstrom * vmw_cmdbuf_cur_flush - Flush the current command buffer for small kernel 5903eab3d9eSThomas Hellstrom * command submissions 5913eab3d9eSThomas Hellstrom * 5923eab3d9eSThomas Hellstrom * @man: The command buffer manager. 5933eab3d9eSThomas Hellstrom * @interruptible: Whether to sleep interruptible when sleeping. 5943eab3d9eSThomas Hellstrom * 5953eab3d9eSThomas Hellstrom * Flushes the current command buffer without allocating a new one. A new one 5963eab3d9eSThomas Hellstrom * is automatically allocated when needed. 5973eab3d9eSThomas Hellstrom */ 5983eab3d9eSThomas Hellstrom int vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man *man, 5993eab3d9eSThomas Hellstrom bool interruptible) 6003eab3d9eSThomas Hellstrom { 6013eab3d9eSThomas Hellstrom int ret = vmw_cmdbuf_cur_lock(man, interruptible); 6023eab3d9eSThomas Hellstrom 6033eab3d9eSThomas Hellstrom if (ret) 6043eab3d9eSThomas Hellstrom return ret; 6053eab3d9eSThomas Hellstrom 6063eab3d9eSThomas Hellstrom __vmw_cmdbuf_cur_flush(man); 6073eab3d9eSThomas Hellstrom vmw_cmdbuf_cur_unlock(man); 6083eab3d9eSThomas Hellstrom 6093eab3d9eSThomas Hellstrom return 0; 6103eab3d9eSThomas Hellstrom } 6113eab3d9eSThomas Hellstrom 6123eab3d9eSThomas Hellstrom /** 6133eab3d9eSThomas Hellstrom * vmw_cmdbuf_idle - Wait for command buffer manager idle. 6143eab3d9eSThomas Hellstrom * 6153eab3d9eSThomas Hellstrom * @man: The command buffer manager. 6163eab3d9eSThomas Hellstrom * @interruptible: Sleep interruptible while waiting. 6173eab3d9eSThomas Hellstrom * @timeout: Time out after this many ticks. 6183eab3d9eSThomas Hellstrom * 6193eab3d9eSThomas Hellstrom * Wait until the command buffer manager has processed all command buffers, 6203eab3d9eSThomas Hellstrom * or until a timeout occurs. If a timeout occurs, the function will return 6213eab3d9eSThomas Hellstrom * -EBUSY. 6223eab3d9eSThomas Hellstrom */ 6233eab3d9eSThomas Hellstrom int vmw_cmdbuf_idle(struct vmw_cmdbuf_man *man, bool interruptible, 6243eab3d9eSThomas Hellstrom unsigned long timeout) 6253eab3d9eSThomas Hellstrom { 6263eab3d9eSThomas Hellstrom int ret; 6273eab3d9eSThomas Hellstrom 6283eab3d9eSThomas Hellstrom ret = vmw_cmdbuf_cur_flush(man, interruptible); 6293eab3d9eSThomas Hellstrom vmw_generic_waiter_add(man->dev_priv, 6303eab3d9eSThomas Hellstrom SVGA_IRQFLAG_COMMAND_BUFFER, 6313eab3d9eSThomas Hellstrom &man->dev_priv->cmdbuf_waiters); 6323eab3d9eSThomas Hellstrom 6333eab3d9eSThomas Hellstrom if (interruptible) { 6343eab3d9eSThomas Hellstrom ret = wait_event_interruptible_timeout 6353eab3d9eSThomas Hellstrom (man->idle_queue, vmw_cmdbuf_man_idle(man, true), 6363eab3d9eSThomas Hellstrom timeout); 6373eab3d9eSThomas Hellstrom } else { 6383eab3d9eSThomas Hellstrom ret = wait_event_timeout 6393eab3d9eSThomas Hellstrom (man->idle_queue, vmw_cmdbuf_man_idle(man, true), 6403eab3d9eSThomas Hellstrom timeout); 6413eab3d9eSThomas Hellstrom } 6423eab3d9eSThomas Hellstrom vmw_generic_waiter_remove(man->dev_priv, 6433eab3d9eSThomas Hellstrom SVGA_IRQFLAG_COMMAND_BUFFER, 6443eab3d9eSThomas Hellstrom &man->dev_priv->cmdbuf_waiters); 6453eab3d9eSThomas Hellstrom if (ret == 0) { 6463eab3d9eSThomas Hellstrom if (!vmw_cmdbuf_man_idle(man, true)) 6473eab3d9eSThomas Hellstrom ret = -EBUSY; 6483eab3d9eSThomas Hellstrom else 6493eab3d9eSThomas Hellstrom ret = 0; 6503eab3d9eSThomas Hellstrom } 6513eab3d9eSThomas Hellstrom if (ret > 0) 6523eab3d9eSThomas Hellstrom ret = 0; 6533eab3d9eSThomas Hellstrom 6543eab3d9eSThomas Hellstrom return ret; 6553eab3d9eSThomas Hellstrom } 6563eab3d9eSThomas Hellstrom 6573eab3d9eSThomas Hellstrom /** 6583eab3d9eSThomas Hellstrom * vmw_cmdbuf_try_alloc - Try to allocate buffer space from the main pool. 6593eab3d9eSThomas Hellstrom * 6603eab3d9eSThomas Hellstrom * @man: The command buffer manager. 6613eab3d9eSThomas Hellstrom * @info: Allocation info. Will hold the size on entry and allocated mm node 6623eab3d9eSThomas Hellstrom * on successful return. 6633eab3d9eSThomas Hellstrom * 6643eab3d9eSThomas Hellstrom * Try to allocate buffer space from the main pool. Returns true if succeeded. 6653eab3d9eSThomas Hellstrom * If a fatal error was hit, the error code is returned in @info->ret. 6663eab3d9eSThomas Hellstrom */ 6673eab3d9eSThomas Hellstrom static bool vmw_cmdbuf_try_alloc(struct vmw_cmdbuf_man *man, 6683eab3d9eSThomas Hellstrom struct vmw_cmdbuf_alloc_info *info) 6693eab3d9eSThomas Hellstrom { 6703eab3d9eSThomas Hellstrom int ret; 6713eab3d9eSThomas Hellstrom 6729b590783SThomas Hellstrom if (info->done) 6733eab3d9eSThomas Hellstrom return true; 6743eab3d9eSThomas Hellstrom 6759b590783SThomas Hellstrom memset(info->node, 0, sizeof(*info->node)); 6763eab3d9eSThomas Hellstrom spin_lock_bh(&man->lock); 6774e64e553SChris Wilson ret = drm_mm_insert_node(&man->mm, info->node, info->page_size); 678575f9c86SThomas Hellstrom if (ret) { 67909dc1387SThomas Hellstrom vmw_cmdbuf_man_process(man); 6804e64e553SChris Wilson ret = drm_mm_insert_node(&man->mm, info->node, info->page_size); 681575f9c86SThomas Hellstrom } 682575f9c86SThomas Hellstrom 6833eab3d9eSThomas Hellstrom spin_unlock_bh(&man->lock); 6849b590783SThomas Hellstrom info->done = !ret; 6853eab3d9eSThomas Hellstrom 6869b590783SThomas Hellstrom return info->done; 6873eab3d9eSThomas Hellstrom } 6883eab3d9eSThomas Hellstrom 6893eab3d9eSThomas Hellstrom /** 6903eab3d9eSThomas Hellstrom * vmw_cmdbuf_alloc_space - Allocate buffer space from the main pool. 6913eab3d9eSThomas Hellstrom * 6923eab3d9eSThomas Hellstrom * @man: The command buffer manager. 6939b590783SThomas Hellstrom * @node: Pointer to pre-allocated range-manager node. 6943eab3d9eSThomas Hellstrom * @size: The size of the allocation. 6953eab3d9eSThomas Hellstrom * @interruptible: Whether to sleep interruptible while waiting for space. 6963eab3d9eSThomas Hellstrom * 6973eab3d9eSThomas Hellstrom * This function allocates buffer space from the main pool, and if there is 6983eab3d9eSThomas Hellstrom * no space available ATM, it turns on IRQ handling and sleeps waiting for it to 6993eab3d9eSThomas Hellstrom * become available. 7003eab3d9eSThomas Hellstrom */ 701b9eb1a61SThomas Hellstrom static int vmw_cmdbuf_alloc_space(struct vmw_cmdbuf_man *man, 7029b590783SThomas Hellstrom struct drm_mm_node *node, 7033eab3d9eSThomas Hellstrom size_t size, 7043eab3d9eSThomas Hellstrom bool interruptible) 7053eab3d9eSThomas Hellstrom { 7063eab3d9eSThomas Hellstrom struct vmw_cmdbuf_alloc_info info; 7073eab3d9eSThomas Hellstrom 7083eab3d9eSThomas Hellstrom info.page_size = PAGE_ALIGN(size) >> PAGE_SHIFT; 7099b590783SThomas Hellstrom info.node = node; 7109b590783SThomas Hellstrom info.done = false; 7113eab3d9eSThomas Hellstrom 7123eab3d9eSThomas Hellstrom /* 7133eab3d9eSThomas Hellstrom * To prevent starvation of large requests, only one allocating call 7143eab3d9eSThomas Hellstrom * at a time waiting for space. 7153eab3d9eSThomas Hellstrom */ 7163eab3d9eSThomas Hellstrom if (interruptible) { 7173eab3d9eSThomas Hellstrom if (mutex_lock_interruptible(&man->space_mutex)) 7189b590783SThomas Hellstrom return -ERESTARTSYS; 7193eab3d9eSThomas Hellstrom } else { 7203eab3d9eSThomas Hellstrom mutex_lock(&man->space_mutex); 7213eab3d9eSThomas Hellstrom } 7223eab3d9eSThomas Hellstrom 7233eab3d9eSThomas Hellstrom /* Try to allocate space without waiting. */ 7249b590783SThomas Hellstrom if (vmw_cmdbuf_try_alloc(man, &info)) 7259b590783SThomas Hellstrom goto out_unlock; 7263eab3d9eSThomas Hellstrom 7273eab3d9eSThomas Hellstrom vmw_generic_waiter_add(man->dev_priv, 7283eab3d9eSThomas Hellstrom SVGA_IRQFLAG_COMMAND_BUFFER, 7293eab3d9eSThomas Hellstrom &man->dev_priv->cmdbuf_waiters); 7303eab3d9eSThomas Hellstrom 7313eab3d9eSThomas Hellstrom if (interruptible) { 7323eab3d9eSThomas Hellstrom int ret; 7333eab3d9eSThomas Hellstrom 7343eab3d9eSThomas Hellstrom ret = wait_event_interruptible 7353eab3d9eSThomas Hellstrom (man->alloc_queue, vmw_cmdbuf_try_alloc(man, &info)); 7363eab3d9eSThomas Hellstrom if (ret) { 7373eab3d9eSThomas Hellstrom vmw_generic_waiter_remove 7383eab3d9eSThomas Hellstrom (man->dev_priv, SVGA_IRQFLAG_COMMAND_BUFFER, 7393eab3d9eSThomas Hellstrom &man->dev_priv->cmdbuf_waiters); 7403eab3d9eSThomas Hellstrom mutex_unlock(&man->space_mutex); 7419b590783SThomas Hellstrom return ret; 7423eab3d9eSThomas Hellstrom } 7433eab3d9eSThomas Hellstrom } else { 7443eab3d9eSThomas Hellstrom wait_event(man->alloc_queue, vmw_cmdbuf_try_alloc(man, &info)); 7453eab3d9eSThomas Hellstrom } 7463eab3d9eSThomas Hellstrom vmw_generic_waiter_remove(man->dev_priv, 7473eab3d9eSThomas Hellstrom SVGA_IRQFLAG_COMMAND_BUFFER, 7483eab3d9eSThomas Hellstrom &man->dev_priv->cmdbuf_waiters); 7493eab3d9eSThomas Hellstrom 7509b590783SThomas Hellstrom out_unlock: 7519b590783SThomas Hellstrom mutex_unlock(&man->space_mutex); 7529b590783SThomas Hellstrom 7539b590783SThomas Hellstrom return 0; 7543eab3d9eSThomas Hellstrom } 7553eab3d9eSThomas Hellstrom 7563eab3d9eSThomas Hellstrom /** 7573eab3d9eSThomas Hellstrom * vmw_cmdbuf_space_pool - Set up a command buffer header with command buffer 7583eab3d9eSThomas Hellstrom * space from the main pool. 7593eab3d9eSThomas Hellstrom * 7603eab3d9eSThomas Hellstrom * @man: The command buffer manager. 7613eab3d9eSThomas Hellstrom * @header: Pointer to the header to set up. 7623eab3d9eSThomas Hellstrom * @size: The requested size of the buffer space. 7633eab3d9eSThomas Hellstrom * @interruptible: Whether to sleep interruptible while waiting for space. 7643eab3d9eSThomas Hellstrom */ 7653eab3d9eSThomas Hellstrom static int vmw_cmdbuf_space_pool(struct vmw_cmdbuf_man *man, 7663eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header *header, 7673eab3d9eSThomas Hellstrom size_t size, 7683eab3d9eSThomas Hellstrom bool interruptible) 7693eab3d9eSThomas Hellstrom { 7703eab3d9eSThomas Hellstrom SVGACBHeader *cb_hdr; 7713eab3d9eSThomas Hellstrom size_t offset; 7723eab3d9eSThomas Hellstrom int ret; 7733eab3d9eSThomas Hellstrom 7743eab3d9eSThomas Hellstrom if (!man->has_pool) 7753eab3d9eSThomas Hellstrom return -ENOMEM; 7763eab3d9eSThomas Hellstrom 7779b590783SThomas Hellstrom ret = vmw_cmdbuf_alloc_space(man, &header->node, size, interruptible); 7783eab3d9eSThomas Hellstrom 7799b590783SThomas Hellstrom if (ret) 7809b590783SThomas Hellstrom return ret; 7813eab3d9eSThomas Hellstrom 7823eab3d9eSThomas Hellstrom header->cb_header = dma_pool_alloc(man->headers, GFP_KERNEL, 7833eab3d9eSThomas Hellstrom &header->handle); 7843eab3d9eSThomas Hellstrom if (!header->cb_header) { 7853eab3d9eSThomas Hellstrom ret = -ENOMEM; 7863eab3d9eSThomas Hellstrom goto out_no_cb_header; 7873eab3d9eSThomas Hellstrom } 7883eab3d9eSThomas Hellstrom 7899b590783SThomas Hellstrom header->size = header->node.size << PAGE_SHIFT; 7903eab3d9eSThomas Hellstrom cb_hdr = header->cb_header; 7919b590783SThomas Hellstrom offset = header->node.start << PAGE_SHIFT; 7923eab3d9eSThomas Hellstrom header->cmd = man->map + offset; 7933eab3d9eSThomas Hellstrom memset(cb_hdr, 0, sizeof(*cb_hdr)); 7943eab3d9eSThomas Hellstrom if (man->using_mob) { 7953eab3d9eSThomas Hellstrom cb_hdr->flags = SVGA_CB_FLAG_MOB; 7963eab3d9eSThomas Hellstrom cb_hdr->ptr.mob.mobid = man->cmd_space->mem.start; 7973eab3d9eSThomas Hellstrom cb_hdr->ptr.mob.mobOffset = offset; 7983eab3d9eSThomas Hellstrom } else { 7993eab3d9eSThomas Hellstrom cb_hdr->ptr.pa = (u64)man->handle + (u64)offset; 8003eab3d9eSThomas Hellstrom } 8013eab3d9eSThomas Hellstrom 8023eab3d9eSThomas Hellstrom return 0; 8033eab3d9eSThomas Hellstrom 8043eab3d9eSThomas Hellstrom out_no_cb_header: 8053eab3d9eSThomas Hellstrom spin_lock_bh(&man->lock); 8069b590783SThomas Hellstrom drm_mm_remove_node(&header->node); 8073eab3d9eSThomas Hellstrom spin_unlock_bh(&man->lock); 8083eab3d9eSThomas Hellstrom 8093eab3d9eSThomas Hellstrom return ret; 8103eab3d9eSThomas Hellstrom } 8113eab3d9eSThomas Hellstrom 8123eab3d9eSThomas Hellstrom /** 8133eab3d9eSThomas Hellstrom * vmw_cmdbuf_space_inline - Set up a command buffer header with 8143eab3d9eSThomas Hellstrom * inline command buffer space. 8153eab3d9eSThomas Hellstrom * 8163eab3d9eSThomas Hellstrom * @man: The command buffer manager. 8173eab3d9eSThomas Hellstrom * @header: Pointer to the header to set up. 8183eab3d9eSThomas Hellstrom * @size: The requested size of the buffer space. 8193eab3d9eSThomas Hellstrom */ 8203eab3d9eSThomas Hellstrom static int vmw_cmdbuf_space_inline(struct vmw_cmdbuf_man *man, 8213eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header *header, 8223eab3d9eSThomas Hellstrom int size) 8233eab3d9eSThomas Hellstrom { 8243eab3d9eSThomas Hellstrom struct vmw_cmdbuf_dheader *dheader; 8253eab3d9eSThomas Hellstrom SVGACBHeader *cb_hdr; 8263eab3d9eSThomas Hellstrom 8273eab3d9eSThomas Hellstrom if (WARN_ON_ONCE(size > VMW_CMDBUF_INLINE_SIZE)) 8283eab3d9eSThomas Hellstrom return -ENOMEM; 8293eab3d9eSThomas Hellstrom 8303eab3d9eSThomas Hellstrom dheader = dma_pool_alloc(man->dheaders, GFP_KERNEL, 8313eab3d9eSThomas Hellstrom &header->handle); 8323eab3d9eSThomas Hellstrom if (!dheader) 8333eab3d9eSThomas Hellstrom return -ENOMEM; 8343eab3d9eSThomas Hellstrom 8353eab3d9eSThomas Hellstrom header->inline_space = true; 8363eab3d9eSThomas Hellstrom header->size = VMW_CMDBUF_INLINE_SIZE; 8373eab3d9eSThomas Hellstrom cb_hdr = &dheader->cb_header; 8383eab3d9eSThomas Hellstrom header->cb_header = cb_hdr; 8393eab3d9eSThomas Hellstrom header->cmd = dheader->cmd; 8403eab3d9eSThomas Hellstrom memset(dheader, 0, sizeof(*dheader)); 8413eab3d9eSThomas Hellstrom cb_hdr->status = SVGA_CB_STATUS_NONE; 8423eab3d9eSThomas Hellstrom cb_hdr->flags = SVGA_CB_FLAG_NONE; 8433eab3d9eSThomas Hellstrom cb_hdr->ptr.pa = (u64)header->handle + 8443eab3d9eSThomas Hellstrom (u64)offsetof(struct vmw_cmdbuf_dheader, cmd); 8453eab3d9eSThomas Hellstrom 8463eab3d9eSThomas Hellstrom return 0; 8473eab3d9eSThomas Hellstrom } 8483eab3d9eSThomas Hellstrom 8493eab3d9eSThomas Hellstrom /** 8503eab3d9eSThomas Hellstrom * vmw_cmdbuf_alloc - Allocate a command buffer header complete with 8513eab3d9eSThomas Hellstrom * command buffer space. 8523eab3d9eSThomas Hellstrom * 8533eab3d9eSThomas Hellstrom * @man: The command buffer manager. 8543eab3d9eSThomas Hellstrom * @size: The requested size of the buffer space. 8553eab3d9eSThomas Hellstrom * @interruptible: Whether to sleep interruptible while waiting for space. 8563eab3d9eSThomas Hellstrom * @p_header: points to a header pointer to populate on successful return. 8573eab3d9eSThomas Hellstrom * 8583eab3d9eSThomas Hellstrom * Returns a pointer to command buffer space if successful. Otherwise 8593eab3d9eSThomas Hellstrom * returns an error pointer. The header pointer returned in @p_header should 8603eab3d9eSThomas Hellstrom * be used for upcoming calls to vmw_cmdbuf_reserve() and vmw_cmdbuf_commit(). 8613eab3d9eSThomas Hellstrom */ 8623eab3d9eSThomas Hellstrom void *vmw_cmdbuf_alloc(struct vmw_cmdbuf_man *man, 8633eab3d9eSThomas Hellstrom size_t size, bool interruptible, 8643eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header **p_header) 8653eab3d9eSThomas Hellstrom { 8663eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header *header; 8673eab3d9eSThomas Hellstrom int ret = 0; 8683eab3d9eSThomas Hellstrom 8693eab3d9eSThomas Hellstrom *p_header = NULL; 8703eab3d9eSThomas Hellstrom 8713eab3d9eSThomas Hellstrom header = kzalloc(sizeof(*header), GFP_KERNEL); 8723eab3d9eSThomas Hellstrom if (!header) 8733eab3d9eSThomas Hellstrom return ERR_PTR(-ENOMEM); 8743eab3d9eSThomas Hellstrom 8753eab3d9eSThomas Hellstrom if (size <= VMW_CMDBUF_INLINE_SIZE) 8763eab3d9eSThomas Hellstrom ret = vmw_cmdbuf_space_inline(man, header, size); 8773eab3d9eSThomas Hellstrom else 8783eab3d9eSThomas Hellstrom ret = vmw_cmdbuf_space_pool(man, header, size, interruptible); 8793eab3d9eSThomas Hellstrom 8803eab3d9eSThomas Hellstrom if (ret) { 8813eab3d9eSThomas Hellstrom kfree(header); 8823eab3d9eSThomas Hellstrom return ERR_PTR(ret); 8833eab3d9eSThomas Hellstrom } 8843eab3d9eSThomas Hellstrom 8853eab3d9eSThomas Hellstrom header->man = man; 8863eab3d9eSThomas Hellstrom INIT_LIST_HEAD(&header->list); 8873eab3d9eSThomas Hellstrom header->cb_header->status = SVGA_CB_STATUS_NONE; 8883eab3d9eSThomas Hellstrom *p_header = header; 8893eab3d9eSThomas Hellstrom 8903eab3d9eSThomas Hellstrom return header->cmd; 8913eab3d9eSThomas Hellstrom } 8923eab3d9eSThomas Hellstrom 8933eab3d9eSThomas Hellstrom /** 8943eab3d9eSThomas Hellstrom * vmw_cmdbuf_reserve_cur - Reserve space for commands in the current 8953eab3d9eSThomas Hellstrom * command buffer. 8963eab3d9eSThomas Hellstrom * 8973eab3d9eSThomas Hellstrom * @man: The command buffer manager. 8983eab3d9eSThomas Hellstrom * @size: The requested size of the commands. 8993eab3d9eSThomas Hellstrom * @ctx_id: The context id if any. Otherwise set to SVGA3D_REG_INVALID. 9003eab3d9eSThomas Hellstrom * @interruptible: Whether to sleep interruptible while waiting for space. 9013eab3d9eSThomas Hellstrom * 9023eab3d9eSThomas Hellstrom * Returns a pointer to command buffer space if successful. Otherwise 9033eab3d9eSThomas Hellstrom * returns an error pointer. 9043eab3d9eSThomas Hellstrom */ 9053eab3d9eSThomas Hellstrom static void *vmw_cmdbuf_reserve_cur(struct vmw_cmdbuf_man *man, 9063eab3d9eSThomas Hellstrom size_t size, 9073eab3d9eSThomas Hellstrom int ctx_id, 9083eab3d9eSThomas Hellstrom bool interruptible) 9093eab3d9eSThomas Hellstrom { 9103eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header *cur; 9113eab3d9eSThomas Hellstrom void *ret; 9123eab3d9eSThomas Hellstrom 9133eab3d9eSThomas Hellstrom if (vmw_cmdbuf_cur_lock(man, interruptible)) 9143eab3d9eSThomas Hellstrom return ERR_PTR(-ERESTARTSYS); 9153eab3d9eSThomas Hellstrom 9163eab3d9eSThomas Hellstrom cur = man->cur; 9173eab3d9eSThomas Hellstrom if (cur && (size + man->cur_pos > cur->size || 918d80efd5cSThomas Hellstrom ((cur->cb_header->flags & SVGA_CB_FLAG_DX_CONTEXT) && 9193eab3d9eSThomas Hellstrom ctx_id != cur->cb_header->dxContext))) 9203eab3d9eSThomas Hellstrom __vmw_cmdbuf_cur_flush(man); 9213eab3d9eSThomas Hellstrom 9223eab3d9eSThomas Hellstrom if (!man->cur) { 9233eab3d9eSThomas Hellstrom ret = vmw_cmdbuf_alloc(man, 9243eab3d9eSThomas Hellstrom max_t(size_t, size, man->default_size), 9253eab3d9eSThomas Hellstrom interruptible, &man->cur); 9263eab3d9eSThomas Hellstrom if (IS_ERR(ret)) { 9273eab3d9eSThomas Hellstrom vmw_cmdbuf_cur_unlock(man); 9283eab3d9eSThomas Hellstrom return ret; 9293eab3d9eSThomas Hellstrom } 9303eab3d9eSThomas Hellstrom 9313eab3d9eSThomas Hellstrom cur = man->cur; 9323eab3d9eSThomas Hellstrom } 9333eab3d9eSThomas Hellstrom 9343eab3d9eSThomas Hellstrom if (ctx_id != SVGA3D_INVALID_ID) { 9353eab3d9eSThomas Hellstrom cur->cb_header->flags |= SVGA_CB_FLAG_DX_CONTEXT; 9363eab3d9eSThomas Hellstrom cur->cb_header->dxContext = ctx_id; 9373eab3d9eSThomas Hellstrom } 9383eab3d9eSThomas Hellstrom 9393eab3d9eSThomas Hellstrom cur->reserved = size; 9403eab3d9eSThomas Hellstrom 9413eab3d9eSThomas Hellstrom return (void *) (man->cur->cmd + man->cur_pos); 9423eab3d9eSThomas Hellstrom } 9433eab3d9eSThomas Hellstrom 9443eab3d9eSThomas Hellstrom /** 9453eab3d9eSThomas Hellstrom * vmw_cmdbuf_commit_cur - Commit commands in the current command buffer. 9463eab3d9eSThomas Hellstrom * 9473eab3d9eSThomas Hellstrom * @man: The command buffer manager. 9483eab3d9eSThomas Hellstrom * @size: The size of the commands actually written. 9493eab3d9eSThomas Hellstrom * @flush: Whether to flush the command buffer immediately. 9503eab3d9eSThomas Hellstrom */ 9513eab3d9eSThomas Hellstrom static void vmw_cmdbuf_commit_cur(struct vmw_cmdbuf_man *man, 9523eab3d9eSThomas Hellstrom size_t size, bool flush) 9533eab3d9eSThomas Hellstrom { 9543eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header *cur = man->cur; 9553eab3d9eSThomas Hellstrom 9563eab3d9eSThomas Hellstrom WARN_ON(!mutex_is_locked(&man->cur_mutex)); 9573eab3d9eSThomas Hellstrom 9583eab3d9eSThomas Hellstrom WARN_ON(size > cur->reserved); 9593eab3d9eSThomas Hellstrom man->cur_pos += size; 9603eab3d9eSThomas Hellstrom if (!size) 9613eab3d9eSThomas Hellstrom cur->cb_header->flags &= ~SVGA_CB_FLAG_DX_CONTEXT; 9623eab3d9eSThomas Hellstrom if (flush) 9633eab3d9eSThomas Hellstrom __vmw_cmdbuf_cur_flush(man); 9643eab3d9eSThomas Hellstrom vmw_cmdbuf_cur_unlock(man); 9653eab3d9eSThomas Hellstrom } 9663eab3d9eSThomas Hellstrom 9673eab3d9eSThomas Hellstrom /** 9683eab3d9eSThomas Hellstrom * vmw_cmdbuf_reserve - Reserve space for commands in a command buffer. 9693eab3d9eSThomas Hellstrom * 9703eab3d9eSThomas Hellstrom * @man: The command buffer manager. 9713eab3d9eSThomas Hellstrom * @size: The requested size of the commands. 9723eab3d9eSThomas Hellstrom * @ctx_id: The context id if any. Otherwise set to SVGA3D_REG_INVALID. 9733eab3d9eSThomas Hellstrom * @interruptible: Whether to sleep interruptible while waiting for space. 9743eab3d9eSThomas Hellstrom * @header: Header of the command buffer. NULL if the current command buffer 9753eab3d9eSThomas Hellstrom * should be used. 9763eab3d9eSThomas Hellstrom * 9773eab3d9eSThomas Hellstrom * Returns a pointer to command buffer space if successful. Otherwise 9783eab3d9eSThomas Hellstrom * returns an error pointer. 9793eab3d9eSThomas Hellstrom */ 9803eab3d9eSThomas Hellstrom void *vmw_cmdbuf_reserve(struct vmw_cmdbuf_man *man, size_t size, 9813eab3d9eSThomas Hellstrom int ctx_id, bool interruptible, 9823eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header *header) 9833eab3d9eSThomas Hellstrom { 9843eab3d9eSThomas Hellstrom if (!header) 9853eab3d9eSThomas Hellstrom return vmw_cmdbuf_reserve_cur(man, size, ctx_id, interruptible); 9863eab3d9eSThomas Hellstrom 9873eab3d9eSThomas Hellstrom if (size > header->size) 9883eab3d9eSThomas Hellstrom return ERR_PTR(-EINVAL); 9893eab3d9eSThomas Hellstrom 9903eab3d9eSThomas Hellstrom if (ctx_id != SVGA3D_INVALID_ID) { 9913eab3d9eSThomas Hellstrom header->cb_header->flags |= SVGA_CB_FLAG_DX_CONTEXT; 9923eab3d9eSThomas Hellstrom header->cb_header->dxContext = ctx_id; 9933eab3d9eSThomas Hellstrom } 9943eab3d9eSThomas Hellstrom 9953eab3d9eSThomas Hellstrom header->reserved = size; 9963eab3d9eSThomas Hellstrom return header->cmd; 9973eab3d9eSThomas Hellstrom } 9983eab3d9eSThomas Hellstrom 9993eab3d9eSThomas Hellstrom /** 10003eab3d9eSThomas Hellstrom * vmw_cmdbuf_commit - Commit commands in a command buffer. 10013eab3d9eSThomas Hellstrom * 10023eab3d9eSThomas Hellstrom * @man: The command buffer manager. 10033eab3d9eSThomas Hellstrom * @size: The size of the commands actually written. 10043eab3d9eSThomas Hellstrom * @header: Header of the command buffer. NULL if the current command buffer 10053eab3d9eSThomas Hellstrom * should be used. 10063eab3d9eSThomas Hellstrom * @flush: Whether to flush the command buffer immediately. 10073eab3d9eSThomas Hellstrom */ 10083eab3d9eSThomas Hellstrom void vmw_cmdbuf_commit(struct vmw_cmdbuf_man *man, size_t size, 10093eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header *header, bool flush) 10103eab3d9eSThomas Hellstrom { 10113eab3d9eSThomas Hellstrom if (!header) { 10123eab3d9eSThomas Hellstrom vmw_cmdbuf_commit_cur(man, size, flush); 10133eab3d9eSThomas Hellstrom return; 10143eab3d9eSThomas Hellstrom } 10153eab3d9eSThomas Hellstrom 10163eab3d9eSThomas Hellstrom (void) vmw_cmdbuf_cur_lock(man, false); 10173eab3d9eSThomas Hellstrom __vmw_cmdbuf_cur_flush(man); 10183eab3d9eSThomas Hellstrom WARN_ON(size > header->reserved); 10193eab3d9eSThomas Hellstrom man->cur = header; 10203eab3d9eSThomas Hellstrom man->cur_pos = size; 10213eab3d9eSThomas Hellstrom if (!size) 10223eab3d9eSThomas Hellstrom header->cb_header->flags &= ~SVGA_CB_FLAG_DX_CONTEXT; 10233eab3d9eSThomas Hellstrom if (flush) 10243eab3d9eSThomas Hellstrom __vmw_cmdbuf_cur_flush(man); 10253eab3d9eSThomas Hellstrom vmw_cmdbuf_cur_unlock(man); 10263eab3d9eSThomas Hellstrom } 10273eab3d9eSThomas Hellstrom 10283eab3d9eSThomas Hellstrom /** 10293eab3d9eSThomas Hellstrom * vmw_cmdbuf_tasklet_schedule - Schedule the interrupt handler bottom half. 10303eab3d9eSThomas Hellstrom * 10313eab3d9eSThomas Hellstrom * @man: The command buffer manager. 10323eab3d9eSThomas Hellstrom */ 10333eab3d9eSThomas Hellstrom void vmw_cmdbuf_tasklet_schedule(struct vmw_cmdbuf_man *man) 10343eab3d9eSThomas Hellstrom { 10353eab3d9eSThomas Hellstrom if (!man) 10363eab3d9eSThomas Hellstrom return; 10373eab3d9eSThomas Hellstrom 10383eab3d9eSThomas Hellstrom tasklet_schedule(&man->tasklet); 10393eab3d9eSThomas Hellstrom } 10403eab3d9eSThomas Hellstrom 10413eab3d9eSThomas Hellstrom /** 10423eab3d9eSThomas Hellstrom * vmw_cmdbuf_send_device_command - Send a command through the device context. 10433eab3d9eSThomas Hellstrom * 10443eab3d9eSThomas Hellstrom * @man: The command buffer manager. 10453eab3d9eSThomas Hellstrom * @command: Pointer to the command to send. 10463eab3d9eSThomas Hellstrom * @size: Size of the command. 10473eab3d9eSThomas Hellstrom * 10483eab3d9eSThomas Hellstrom * Synchronously sends a device context command. 10493eab3d9eSThomas Hellstrom */ 10503eab3d9eSThomas Hellstrom static int vmw_cmdbuf_send_device_command(struct vmw_cmdbuf_man *man, 10513eab3d9eSThomas Hellstrom const void *command, 10523eab3d9eSThomas Hellstrom size_t size) 10533eab3d9eSThomas Hellstrom { 10543eab3d9eSThomas Hellstrom struct vmw_cmdbuf_header *header; 10553eab3d9eSThomas Hellstrom int status; 10563eab3d9eSThomas Hellstrom void *cmd = vmw_cmdbuf_alloc(man, size, false, &header); 10573eab3d9eSThomas Hellstrom 10583eab3d9eSThomas Hellstrom if (IS_ERR(cmd)) 10593eab3d9eSThomas Hellstrom return PTR_ERR(cmd); 10603eab3d9eSThomas Hellstrom 10613eab3d9eSThomas Hellstrom memcpy(cmd, command, size); 10623eab3d9eSThomas Hellstrom header->cb_header->length = size; 10633eab3d9eSThomas Hellstrom header->cb_context = SVGA_CB_CONTEXT_DEVICE; 10643eab3d9eSThomas Hellstrom spin_lock_bh(&man->lock); 10653eab3d9eSThomas Hellstrom status = vmw_cmdbuf_header_submit(header); 10663eab3d9eSThomas Hellstrom spin_unlock_bh(&man->lock); 10673eab3d9eSThomas Hellstrom vmw_cmdbuf_header_free(header); 10683eab3d9eSThomas Hellstrom 10693eab3d9eSThomas Hellstrom if (status != SVGA_CB_STATUS_COMPLETED) { 10703eab3d9eSThomas Hellstrom DRM_ERROR("Device context command failed with status %d\n", 10713eab3d9eSThomas Hellstrom status); 10723eab3d9eSThomas Hellstrom return -EINVAL; 10733eab3d9eSThomas Hellstrom } 10743eab3d9eSThomas Hellstrom 10753eab3d9eSThomas Hellstrom return 0; 10763eab3d9eSThomas Hellstrom } 10773eab3d9eSThomas Hellstrom 10783eab3d9eSThomas Hellstrom /** 10793eab3d9eSThomas Hellstrom * vmw_cmdbuf_startstop - Send a start / stop command through the device 10803eab3d9eSThomas Hellstrom * context. 10813eab3d9eSThomas Hellstrom * 10823eab3d9eSThomas Hellstrom * @man: The command buffer manager. 10833eab3d9eSThomas Hellstrom * @enable: Whether to enable or disable the context. 10843eab3d9eSThomas Hellstrom * 10853eab3d9eSThomas Hellstrom * Synchronously sends a device start / stop context command. 10863eab3d9eSThomas Hellstrom */ 10873eab3d9eSThomas Hellstrom static int vmw_cmdbuf_startstop(struct vmw_cmdbuf_man *man, 10883eab3d9eSThomas Hellstrom bool enable) 10893eab3d9eSThomas Hellstrom { 10903eab3d9eSThomas Hellstrom struct { 10913eab3d9eSThomas Hellstrom uint32 id; 10923eab3d9eSThomas Hellstrom SVGADCCmdStartStop body; 10933eab3d9eSThomas Hellstrom } __packed cmd; 10943eab3d9eSThomas Hellstrom 10953eab3d9eSThomas Hellstrom cmd.id = SVGA_DC_CMD_START_STOP_CONTEXT; 10963eab3d9eSThomas Hellstrom cmd.body.enable = (enable) ? 1 : 0; 10973eab3d9eSThomas Hellstrom cmd.body.context = SVGA_CB_CONTEXT_0; 10983eab3d9eSThomas Hellstrom 10993eab3d9eSThomas Hellstrom return vmw_cmdbuf_send_device_command(man, &cmd, sizeof(cmd)); 11003eab3d9eSThomas Hellstrom } 11013eab3d9eSThomas Hellstrom 11023eab3d9eSThomas Hellstrom /** 11033eab3d9eSThomas Hellstrom * vmw_cmdbuf_set_pool_size - Set command buffer manager sizes 11043eab3d9eSThomas Hellstrom * 11053eab3d9eSThomas Hellstrom * @man: The command buffer manager. 11063eab3d9eSThomas Hellstrom * @size: The size of the main space pool. 11073eab3d9eSThomas Hellstrom * @default_size: The default size of the command buffer for small kernel 11083eab3d9eSThomas Hellstrom * submissions. 11093eab3d9eSThomas Hellstrom * 11103eab3d9eSThomas Hellstrom * Set the size and allocate the main command buffer space pool, 11113eab3d9eSThomas Hellstrom * as well as the default size of the command buffer for 11123eab3d9eSThomas Hellstrom * small kernel submissions. If successful, this enables large command 11133eab3d9eSThomas Hellstrom * submissions. Note that this function requires that rudimentary command 11143eab3d9eSThomas Hellstrom * submission is already available and that the MOB memory manager is alive. 11153eab3d9eSThomas Hellstrom * Returns 0 on success. Negative error code on failure. 11163eab3d9eSThomas Hellstrom */ 11173eab3d9eSThomas Hellstrom int vmw_cmdbuf_set_pool_size(struct vmw_cmdbuf_man *man, 11183eab3d9eSThomas Hellstrom size_t size, size_t default_size) 11193eab3d9eSThomas Hellstrom { 11203eab3d9eSThomas Hellstrom struct vmw_private *dev_priv = man->dev_priv; 11213eab3d9eSThomas Hellstrom bool dummy; 11223eab3d9eSThomas Hellstrom int ret; 11233eab3d9eSThomas Hellstrom 11243eab3d9eSThomas Hellstrom if (man->has_pool) 11253eab3d9eSThomas Hellstrom return -EINVAL; 11263eab3d9eSThomas Hellstrom 11273eab3d9eSThomas Hellstrom /* First, try to allocate a huge chunk of DMA memory */ 11283eab3d9eSThomas Hellstrom size = PAGE_ALIGN(size); 11293eab3d9eSThomas Hellstrom man->map = dma_alloc_coherent(&dev_priv->dev->pdev->dev, size, 11303eab3d9eSThomas Hellstrom &man->handle, GFP_KERNEL); 11313eab3d9eSThomas Hellstrom if (man->map) { 11323eab3d9eSThomas Hellstrom man->using_mob = false; 11333eab3d9eSThomas Hellstrom } else { 11343eab3d9eSThomas Hellstrom /* 11353eab3d9eSThomas Hellstrom * DMA memory failed. If we can have command buffers in a 11363eab3d9eSThomas Hellstrom * MOB, try to use that instead. Note that this will 11373eab3d9eSThomas Hellstrom * actually call into the already enabled manager, when 11383eab3d9eSThomas Hellstrom * binding the MOB. 11393eab3d9eSThomas Hellstrom */ 11408ce75f8aSSinclair Yeh if (!(dev_priv->capabilities & SVGA_CAP_DX)) 11413eab3d9eSThomas Hellstrom return -ENOMEM; 11423eab3d9eSThomas Hellstrom 11433eab3d9eSThomas Hellstrom ret = ttm_bo_create(&dev_priv->bdev, size, ttm_bo_type_device, 11443eab3d9eSThomas Hellstrom &vmw_mob_ne_placement, 0, false, NULL, 11453eab3d9eSThomas Hellstrom &man->cmd_space); 11463eab3d9eSThomas Hellstrom if (ret) 11473eab3d9eSThomas Hellstrom return ret; 11483eab3d9eSThomas Hellstrom 11493eab3d9eSThomas Hellstrom man->using_mob = true; 11503eab3d9eSThomas Hellstrom ret = ttm_bo_kmap(man->cmd_space, 0, size >> PAGE_SHIFT, 11513eab3d9eSThomas Hellstrom &man->map_obj); 11523eab3d9eSThomas Hellstrom if (ret) 11533eab3d9eSThomas Hellstrom goto out_no_map; 11543eab3d9eSThomas Hellstrom 11553eab3d9eSThomas Hellstrom man->map = ttm_kmap_obj_virtual(&man->map_obj, &dummy); 11563eab3d9eSThomas Hellstrom } 11573eab3d9eSThomas Hellstrom 11583eab3d9eSThomas Hellstrom man->size = size; 11593eab3d9eSThomas Hellstrom drm_mm_init(&man->mm, 0, size >> PAGE_SHIFT); 11603eab3d9eSThomas Hellstrom 11613eab3d9eSThomas Hellstrom man->has_pool = true; 116209dc1387SThomas Hellstrom 116309dc1387SThomas Hellstrom /* 116409dc1387SThomas Hellstrom * For now, set the default size to VMW_CMDBUF_INLINE_SIZE to 116509dc1387SThomas Hellstrom * prevent deadlocks from happening when vmw_cmdbuf_space_pool() 116609dc1387SThomas Hellstrom * needs to wait for space and we block on further command 116709dc1387SThomas Hellstrom * submissions to be able to free up space. 116809dc1387SThomas Hellstrom */ 116909dc1387SThomas Hellstrom man->default_size = VMW_CMDBUF_INLINE_SIZE; 11703eab3d9eSThomas Hellstrom DRM_INFO("Using command buffers with %s pool.\n", 11713eab3d9eSThomas Hellstrom (man->using_mob) ? "MOB" : "DMA"); 11723eab3d9eSThomas Hellstrom 11733eab3d9eSThomas Hellstrom return 0; 11743eab3d9eSThomas Hellstrom 11753eab3d9eSThomas Hellstrom out_no_map: 11763eab3d9eSThomas Hellstrom if (man->using_mob) 11773eab3d9eSThomas Hellstrom ttm_bo_unref(&man->cmd_space); 11783eab3d9eSThomas Hellstrom 11793eab3d9eSThomas Hellstrom return ret; 11803eab3d9eSThomas Hellstrom } 11813eab3d9eSThomas Hellstrom 11823eab3d9eSThomas Hellstrom /** 11833eab3d9eSThomas Hellstrom * vmw_cmdbuf_man_create: Create a command buffer manager and enable it for 11843eab3d9eSThomas Hellstrom * inline command buffer submissions only. 11853eab3d9eSThomas Hellstrom * 11863eab3d9eSThomas Hellstrom * @dev_priv: Pointer to device private structure. 11873eab3d9eSThomas Hellstrom * 11883eab3d9eSThomas Hellstrom * Returns a pointer to a cummand buffer manager to success or error pointer 11893eab3d9eSThomas Hellstrom * on failure. The command buffer manager will be enabled for submissions of 11903eab3d9eSThomas Hellstrom * size VMW_CMDBUF_INLINE_SIZE only. 11913eab3d9eSThomas Hellstrom */ 11923eab3d9eSThomas Hellstrom struct vmw_cmdbuf_man *vmw_cmdbuf_man_create(struct vmw_private *dev_priv) 11933eab3d9eSThomas Hellstrom { 11943eab3d9eSThomas Hellstrom struct vmw_cmdbuf_man *man; 11953eab3d9eSThomas Hellstrom struct vmw_cmdbuf_context *ctx; 11963eab3d9eSThomas Hellstrom int i; 11973eab3d9eSThomas Hellstrom int ret; 11983eab3d9eSThomas Hellstrom 11993eab3d9eSThomas Hellstrom if (!(dev_priv->capabilities & SVGA_CAP_COMMAND_BUFFERS)) 12003eab3d9eSThomas Hellstrom return ERR_PTR(-ENOSYS); 12013eab3d9eSThomas Hellstrom 12023eab3d9eSThomas Hellstrom man = kzalloc(sizeof(*man), GFP_KERNEL); 12033eab3d9eSThomas Hellstrom if (!man) 12043eab3d9eSThomas Hellstrom return ERR_PTR(-ENOMEM); 12053eab3d9eSThomas Hellstrom 12063eab3d9eSThomas Hellstrom man->headers = dma_pool_create("vmwgfx cmdbuf", 12073eab3d9eSThomas Hellstrom &dev_priv->dev->pdev->dev, 12083eab3d9eSThomas Hellstrom sizeof(SVGACBHeader), 12093eab3d9eSThomas Hellstrom 64, PAGE_SIZE); 12103eab3d9eSThomas Hellstrom if (!man->headers) { 12113eab3d9eSThomas Hellstrom ret = -ENOMEM; 12123eab3d9eSThomas Hellstrom goto out_no_pool; 12133eab3d9eSThomas Hellstrom } 12143eab3d9eSThomas Hellstrom 12153eab3d9eSThomas Hellstrom man->dheaders = dma_pool_create("vmwgfx inline cmdbuf", 12163eab3d9eSThomas Hellstrom &dev_priv->dev->pdev->dev, 12173eab3d9eSThomas Hellstrom sizeof(struct vmw_cmdbuf_dheader), 12183eab3d9eSThomas Hellstrom 64, PAGE_SIZE); 12193eab3d9eSThomas Hellstrom if (!man->dheaders) { 12203eab3d9eSThomas Hellstrom ret = -ENOMEM; 12213eab3d9eSThomas Hellstrom goto out_no_dpool; 12223eab3d9eSThomas Hellstrom } 12233eab3d9eSThomas Hellstrom 12243eab3d9eSThomas Hellstrom for_each_cmdbuf_ctx(man, i, ctx) 12253eab3d9eSThomas Hellstrom vmw_cmdbuf_ctx_init(ctx); 12263eab3d9eSThomas Hellstrom 12273eab3d9eSThomas Hellstrom INIT_LIST_HEAD(&man->error); 12283eab3d9eSThomas Hellstrom spin_lock_init(&man->lock); 12293eab3d9eSThomas Hellstrom mutex_init(&man->cur_mutex); 12303eab3d9eSThomas Hellstrom mutex_init(&man->space_mutex); 12313eab3d9eSThomas Hellstrom tasklet_init(&man->tasklet, vmw_cmdbuf_man_tasklet, 12323eab3d9eSThomas Hellstrom (unsigned long) man); 12333eab3d9eSThomas Hellstrom man->default_size = VMW_CMDBUF_INLINE_SIZE; 12343eab3d9eSThomas Hellstrom init_waitqueue_head(&man->alloc_queue); 12353eab3d9eSThomas Hellstrom init_waitqueue_head(&man->idle_queue); 12363eab3d9eSThomas Hellstrom man->dev_priv = dev_priv; 12373eab3d9eSThomas Hellstrom man->max_hw_submitted = SVGA_CB_MAX_QUEUED_PER_CONTEXT - 1; 12383eab3d9eSThomas Hellstrom INIT_WORK(&man->work, &vmw_cmdbuf_work_func); 12393eab3d9eSThomas Hellstrom vmw_generic_waiter_add(dev_priv, SVGA_IRQFLAG_ERROR, 12403eab3d9eSThomas Hellstrom &dev_priv->error_waiters); 12413eab3d9eSThomas Hellstrom ret = vmw_cmdbuf_startstop(man, true); 12423eab3d9eSThomas Hellstrom if (ret) { 12433eab3d9eSThomas Hellstrom DRM_ERROR("Failed starting command buffer context 0.\n"); 12443eab3d9eSThomas Hellstrom vmw_cmdbuf_man_destroy(man); 12453eab3d9eSThomas Hellstrom return ERR_PTR(ret); 12463eab3d9eSThomas Hellstrom } 12473eab3d9eSThomas Hellstrom 12483eab3d9eSThomas Hellstrom return man; 12493eab3d9eSThomas Hellstrom 12503eab3d9eSThomas Hellstrom out_no_dpool: 12513eab3d9eSThomas Hellstrom dma_pool_destroy(man->headers); 12523eab3d9eSThomas Hellstrom out_no_pool: 12533eab3d9eSThomas Hellstrom kfree(man); 12543eab3d9eSThomas Hellstrom 12553eab3d9eSThomas Hellstrom return ERR_PTR(ret); 12563eab3d9eSThomas Hellstrom } 12573eab3d9eSThomas Hellstrom 12583eab3d9eSThomas Hellstrom /** 12593eab3d9eSThomas Hellstrom * vmw_cmdbuf_remove_pool - Take down the main buffer space pool. 12603eab3d9eSThomas Hellstrom * 12613eab3d9eSThomas Hellstrom * @man: Pointer to a command buffer manager. 12623eab3d9eSThomas Hellstrom * 12633eab3d9eSThomas Hellstrom * This function removes the main buffer space pool, and should be called 12643eab3d9eSThomas Hellstrom * before MOB memory management is removed. When this function has been called, 12653eab3d9eSThomas Hellstrom * only small command buffer submissions of size VMW_CMDBUF_INLINE_SIZE or 12663eab3d9eSThomas Hellstrom * less are allowed, and the default size of the command buffer for small kernel 12673eab3d9eSThomas Hellstrom * submissions is also set to this size. 12683eab3d9eSThomas Hellstrom */ 12693eab3d9eSThomas Hellstrom void vmw_cmdbuf_remove_pool(struct vmw_cmdbuf_man *man) 12703eab3d9eSThomas Hellstrom { 12713eab3d9eSThomas Hellstrom if (!man->has_pool) 12723eab3d9eSThomas Hellstrom return; 12733eab3d9eSThomas Hellstrom 12743eab3d9eSThomas Hellstrom man->has_pool = false; 12753eab3d9eSThomas Hellstrom man->default_size = VMW_CMDBUF_INLINE_SIZE; 12763eab3d9eSThomas Hellstrom (void) vmw_cmdbuf_idle(man, false, 10*HZ); 12773eab3d9eSThomas Hellstrom if (man->using_mob) { 12783eab3d9eSThomas Hellstrom (void) ttm_bo_kunmap(&man->map_obj); 12793eab3d9eSThomas Hellstrom ttm_bo_unref(&man->cmd_space); 12803eab3d9eSThomas Hellstrom } else { 12813eab3d9eSThomas Hellstrom dma_free_coherent(&man->dev_priv->dev->pdev->dev, 12823eab3d9eSThomas Hellstrom man->size, man->map, man->handle); 12833eab3d9eSThomas Hellstrom } 12843eab3d9eSThomas Hellstrom } 12853eab3d9eSThomas Hellstrom 12863eab3d9eSThomas Hellstrom /** 12873eab3d9eSThomas Hellstrom * vmw_cmdbuf_man_destroy - Take down a command buffer manager. 12883eab3d9eSThomas Hellstrom * 12893eab3d9eSThomas Hellstrom * @man: Pointer to a command buffer manager. 12903eab3d9eSThomas Hellstrom * 12913eab3d9eSThomas Hellstrom * This function idles and then destroys a command buffer manager. 12923eab3d9eSThomas Hellstrom */ 12933eab3d9eSThomas Hellstrom void vmw_cmdbuf_man_destroy(struct vmw_cmdbuf_man *man) 12943eab3d9eSThomas Hellstrom { 12953eab3d9eSThomas Hellstrom WARN_ON_ONCE(man->has_pool); 12963eab3d9eSThomas Hellstrom (void) vmw_cmdbuf_idle(man, false, 10*HZ); 12973eab3d9eSThomas Hellstrom if (vmw_cmdbuf_startstop(man, false)) 12983eab3d9eSThomas Hellstrom DRM_ERROR("Failed stopping command buffer context 0.\n"); 12993eab3d9eSThomas Hellstrom 13003eab3d9eSThomas Hellstrom vmw_generic_waiter_remove(man->dev_priv, SVGA_IRQFLAG_ERROR, 13013eab3d9eSThomas Hellstrom &man->dev_priv->error_waiters); 13023eab3d9eSThomas Hellstrom tasklet_kill(&man->tasklet); 13033eab3d9eSThomas Hellstrom (void) cancel_work_sync(&man->work); 13043eab3d9eSThomas Hellstrom dma_pool_destroy(man->dheaders); 13053eab3d9eSThomas Hellstrom dma_pool_destroy(man->headers); 13063eab3d9eSThomas Hellstrom mutex_destroy(&man->cur_mutex); 13073eab3d9eSThomas Hellstrom mutex_destroy(&man->space_mutex); 13083eab3d9eSThomas Hellstrom kfree(man); 13093eab3d9eSThomas Hellstrom } 1310