18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 OR MIT
28c2ecf20Sopenharmony_ci/**************************************************************************
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright 2015 VMware, Inc., Palo Alto, CA., USA
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
78c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the
88c2ecf20Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
98c2ecf20Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
108c2ecf20Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
118c2ecf20Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
128c2ecf20Sopenharmony_ci * the following conditions:
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice (including the
158c2ecf20Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
168c2ecf20Sopenharmony_ci * of the Software.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
198c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
208c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
218c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
228c2ecf20Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
238c2ecf20Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
248c2ecf20Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
258c2ecf20Sopenharmony_ci *
268c2ecf20Sopenharmony_ci **************************************************************************/
278c2ecf20Sopenharmony_ci/*
288c2ecf20Sopenharmony_ci * This file implements the vmwgfx context binding manager,
298c2ecf20Sopenharmony_ci * The sole reason for having to use this code is that vmware guest
308c2ecf20Sopenharmony_ci * backed contexts can be swapped out to their backing mobs by the device
318c2ecf20Sopenharmony_ci * at any time, also swapped in at any time. At swapin time, the device
328c2ecf20Sopenharmony_ci * validates the context bindings to make sure they point to valid resources.
338c2ecf20Sopenharmony_ci * It's this outside-of-drawcall validation (that can happen at any time),
348c2ecf20Sopenharmony_ci * that makes this code necessary.
358c2ecf20Sopenharmony_ci *
368c2ecf20Sopenharmony_ci * We therefore need to kill any context bindings pointing to a resource
378c2ecf20Sopenharmony_ci * when the resource is swapped out. Furthermore, if the vmwgfx driver has
388c2ecf20Sopenharmony_ci * swapped out the context we can't swap it in again to kill bindings because
398c2ecf20Sopenharmony_ci * of backing mob reservation lockdep violations, so as part of
408c2ecf20Sopenharmony_ci * context swapout, also kill all bindings of a context, so that they are
418c2ecf20Sopenharmony_ci * already killed if a resource to which a binding points
428c2ecf20Sopenharmony_ci * needs to be swapped out.
438c2ecf20Sopenharmony_ci *
448c2ecf20Sopenharmony_ci * Note that a resource can be pointed to by bindings from multiple contexts,
458c2ecf20Sopenharmony_ci * Therefore we can't easily protect this data by a per context mutex
468c2ecf20Sopenharmony_ci * (unless we use deadlock-safe WW mutexes). So we use a global binding_mutex
478c2ecf20Sopenharmony_ci * to protect all binding manager data.
488c2ecf20Sopenharmony_ci *
498c2ecf20Sopenharmony_ci * Finally, any association between a context and a global resource
508c2ecf20Sopenharmony_ci * (surface, shader or even DX query) is conceptually a context binding that
518c2ecf20Sopenharmony_ci * needs to be tracked by this code.
528c2ecf20Sopenharmony_ci */
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#include "vmwgfx_drv.h"
558c2ecf20Sopenharmony_ci#include "vmwgfx_binding.h"
568c2ecf20Sopenharmony_ci#include "device_include/svga3d_reg.h"
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci#define VMW_BINDING_RT_BIT     0
598c2ecf20Sopenharmony_ci#define VMW_BINDING_PS_BIT     1
608c2ecf20Sopenharmony_ci#define VMW_BINDING_SO_T_BIT   2
618c2ecf20Sopenharmony_ci#define VMW_BINDING_VB_BIT     3
628c2ecf20Sopenharmony_ci#define VMW_BINDING_UAV_BIT    4
638c2ecf20Sopenharmony_ci#define VMW_BINDING_CS_UAV_BIT 5
648c2ecf20Sopenharmony_ci#define VMW_BINDING_NUM_BITS   6
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci#define VMW_BINDING_PS_SR_BIT  0
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/**
698c2ecf20Sopenharmony_ci * struct vmw_ctx_binding_state - per context binding state
708c2ecf20Sopenharmony_ci *
718c2ecf20Sopenharmony_ci * @dev_priv: Pointer to device private structure.
728c2ecf20Sopenharmony_ci * @list: linked list of individual active bindings.
738c2ecf20Sopenharmony_ci * @render_targets: Render target bindings.
748c2ecf20Sopenharmony_ci * @texture_units: Texture units bindings.
758c2ecf20Sopenharmony_ci * @ds_view: Depth-stencil view binding.
768c2ecf20Sopenharmony_ci * @so_targets: StreamOutput target bindings.
778c2ecf20Sopenharmony_ci * @vertex_buffers: Vertex buffer bindings.
788c2ecf20Sopenharmony_ci * @index_buffer: Index buffer binding.
798c2ecf20Sopenharmony_ci * @per_shader: Per shader-type bindings.
808c2ecf20Sopenharmony_ci * @ua_views: UAV bindings.
818c2ecf20Sopenharmony_ci * @so_state: StreamOutput bindings.
828c2ecf20Sopenharmony_ci * @dirty: Bitmap tracking per binding-type changes that have not yet
838c2ecf20Sopenharmony_ci * been emitted to the device.
848c2ecf20Sopenharmony_ci * @dirty_vb: Bitmap tracking individual vertex buffer binding changes that
858c2ecf20Sopenharmony_ci * have not yet been emitted to the device.
868c2ecf20Sopenharmony_ci * @bind_cmd_buffer: Scratch space used to construct binding commands.
878c2ecf20Sopenharmony_ci * @bind_cmd_count: Number of binding command data entries in @bind_cmd_buffer
888c2ecf20Sopenharmony_ci * @bind_first_slot: Used together with @bind_cmd_buffer to indicate the
898c2ecf20Sopenharmony_ci * device binding slot of the first command data entry in @bind_cmd_buffer.
908c2ecf20Sopenharmony_ci *
918c2ecf20Sopenharmony_ci * Note that this structure also provides storage space for the individual
928c2ecf20Sopenharmony_ci * struct vmw_ctx_binding objects, so that no dynamic allocation is needed
938c2ecf20Sopenharmony_ci * for individual bindings.
948c2ecf20Sopenharmony_ci *
958c2ecf20Sopenharmony_ci */
968c2ecf20Sopenharmony_cistruct vmw_ctx_binding_state {
978c2ecf20Sopenharmony_ci	struct vmw_private *dev_priv;
988c2ecf20Sopenharmony_ci	struct list_head list;
998c2ecf20Sopenharmony_ci	struct vmw_ctx_bindinfo_view render_targets[SVGA3D_RT_MAX];
1008c2ecf20Sopenharmony_ci	struct vmw_ctx_bindinfo_tex texture_units[SVGA3D_NUM_TEXTURE_UNITS];
1018c2ecf20Sopenharmony_ci	struct vmw_ctx_bindinfo_view ds_view;
1028c2ecf20Sopenharmony_ci	struct vmw_ctx_bindinfo_so_target so_targets[SVGA3D_DX_MAX_SOTARGETS];
1038c2ecf20Sopenharmony_ci	struct vmw_ctx_bindinfo_vb vertex_buffers[SVGA3D_DX_MAX_VERTEXBUFFERS];
1048c2ecf20Sopenharmony_ci	struct vmw_ctx_bindinfo_ib index_buffer;
1058c2ecf20Sopenharmony_ci	struct vmw_dx_shader_bindings per_shader[SVGA3D_NUM_SHADERTYPE];
1068c2ecf20Sopenharmony_ci	struct vmw_ctx_bindinfo_uav ua_views[VMW_MAX_UAV_BIND_TYPE];
1078c2ecf20Sopenharmony_ci	struct vmw_ctx_bindinfo_so so_state;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	unsigned long dirty;
1108c2ecf20Sopenharmony_ci	DECLARE_BITMAP(dirty_vb, SVGA3D_DX_MAX_VERTEXBUFFERS);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	u32 bind_cmd_buffer[VMW_MAX_VIEW_BINDINGS];
1138c2ecf20Sopenharmony_ci	u32 bind_cmd_count;
1148c2ecf20Sopenharmony_ci	u32 bind_first_slot;
1158c2ecf20Sopenharmony_ci};
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic int vmw_binding_scrub_shader(struct vmw_ctx_bindinfo *bi, bool rebind);
1188c2ecf20Sopenharmony_cistatic int vmw_binding_scrub_render_target(struct vmw_ctx_bindinfo *bi,
1198c2ecf20Sopenharmony_ci					   bool rebind);
1208c2ecf20Sopenharmony_cistatic int vmw_binding_scrub_texture(struct vmw_ctx_bindinfo *bi, bool rebind);
1218c2ecf20Sopenharmony_cistatic int vmw_binding_scrub_cb(struct vmw_ctx_bindinfo *bi, bool rebind);
1228c2ecf20Sopenharmony_cistatic int vmw_binding_scrub_dx_rt(struct vmw_ctx_bindinfo *bi, bool rebind);
1238c2ecf20Sopenharmony_cistatic int vmw_binding_scrub_sr(struct vmw_ctx_bindinfo *bi, bool rebind);
1248c2ecf20Sopenharmony_cistatic int vmw_binding_scrub_so_target(struct vmw_ctx_bindinfo *bi, bool rebind);
1258c2ecf20Sopenharmony_cistatic int vmw_binding_emit_dirty(struct vmw_ctx_binding_state *cbs);
1268c2ecf20Sopenharmony_cistatic int vmw_binding_scrub_dx_shader(struct vmw_ctx_bindinfo *bi,
1278c2ecf20Sopenharmony_ci				       bool rebind);
1288c2ecf20Sopenharmony_cistatic int vmw_binding_scrub_ib(struct vmw_ctx_bindinfo *bi, bool rebind);
1298c2ecf20Sopenharmony_cistatic int vmw_binding_scrub_vb(struct vmw_ctx_bindinfo *bi, bool rebind);
1308c2ecf20Sopenharmony_cistatic int vmw_binding_scrub_uav(struct vmw_ctx_bindinfo *bi, bool rebind);
1318c2ecf20Sopenharmony_cistatic int vmw_binding_scrub_cs_uav(struct vmw_ctx_bindinfo *bi, bool rebind);
1328c2ecf20Sopenharmony_cistatic int vmw_binding_scrub_so(struct vmw_ctx_bindinfo *bi, bool rebind);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic void vmw_binding_build_asserts(void) __attribute__ ((unused));
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_citypedef int (*vmw_scrub_func)(struct vmw_ctx_bindinfo *, bool);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci/**
1398c2ecf20Sopenharmony_ci * struct vmw_binding_info - Per binding type information for the binding
1408c2ecf20Sopenharmony_ci * manager
1418c2ecf20Sopenharmony_ci *
1428c2ecf20Sopenharmony_ci * @size: The size of the struct binding derived from a struct vmw_ctx_bindinfo.
1438c2ecf20Sopenharmony_ci * @offsets: array[shader_slot] of offsets to the array[slot]
1448c2ecf20Sopenharmony_ci * of struct bindings for the binding type.
1458c2ecf20Sopenharmony_ci * @scrub_func: Pointer to the scrub function for this binding type.
1468c2ecf20Sopenharmony_ci *
1478c2ecf20Sopenharmony_ci * Holds static information to help optimize the binding manager and avoid
1488c2ecf20Sopenharmony_ci * an excessive amount of switch statements.
1498c2ecf20Sopenharmony_ci */
1508c2ecf20Sopenharmony_cistruct vmw_binding_info {
1518c2ecf20Sopenharmony_ci	size_t size;
1528c2ecf20Sopenharmony_ci	const size_t *offsets;
1538c2ecf20Sopenharmony_ci	vmw_scrub_func scrub_func;
1548c2ecf20Sopenharmony_ci};
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci/*
1578c2ecf20Sopenharmony_ci * A number of static variables that help determine the scrub func and the
1588c2ecf20Sopenharmony_ci * location of the struct vmw_ctx_bindinfo slots for each binding type.
1598c2ecf20Sopenharmony_ci */
1608c2ecf20Sopenharmony_cistatic const size_t vmw_binding_shader_offsets[] = {
1618c2ecf20Sopenharmony_ci	offsetof(struct vmw_ctx_binding_state, per_shader[0].shader),
1628c2ecf20Sopenharmony_ci	offsetof(struct vmw_ctx_binding_state, per_shader[1].shader),
1638c2ecf20Sopenharmony_ci	offsetof(struct vmw_ctx_binding_state, per_shader[2].shader),
1648c2ecf20Sopenharmony_ci	offsetof(struct vmw_ctx_binding_state, per_shader[3].shader),
1658c2ecf20Sopenharmony_ci	offsetof(struct vmw_ctx_binding_state, per_shader[4].shader),
1668c2ecf20Sopenharmony_ci	offsetof(struct vmw_ctx_binding_state, per_shader[5].shader),
1678c2ecf20Sopenharmony_ci};
1688c2ecf20Sopenharmony_cistatic const size_t vmw_binding_rt_offsets[] = {
1698c2ecf20Sopenharmony_ci	offsetof(struct vmw_ctx_binding_state, render_targets),
1708c2ecf20Sopenharmony_ci};
1718c2ecf20Sopenharmony_cistatic const size_t vmw_binding_tex_offsets[] = {
1728c2ecf20Sopenharmony_ci	offsetof(struct vmw_ctx_binding_state, texture_units),
1738c2ecf20Sopenharmony_ci};
1748c2ecf20Sopenharmony_cistatic const size_t vmw_binding_cb_offsets[] = {
1758c2ecf20Sopenharmony_ci	offsetof(struct vmw_ctx_binding_state, per_shader[0].const_buffers),
1768c2ecf20Sopenharmony_ci	offsetof(struct vmw_ctx_binding_state, per_shader[1].const_buffers),
1778c2ecf20Sopenharmony_ci	offsetof(struct vmw_ctx_binding_state, per_shader[2].const_buffers),
1788c2ecf20Sopenharmony_ci	offsetof(struct vmw_ctx_binding_state, per_shader[3].const_buffers),
1798c2ecf20Sopenharmony_ci	offsetof(struct vmw_ctx_binding_state, per_shader[4].const_buffers),
1808c2ecf20Sopenharmony_ci	offsetof(struct vmw_ctx_binding_state, per_shader[5].const_buffers),
1818c2ecf20Sopenharmony_ci};
1828c2ecf20Sopenharmony_cistatic const size_t vmw_binding_dx_ds_offsets[] = {
1838c2ecf20Sopenharmony_ci	offsetof(struct vmw_ctx_binding_state, ds_view),
1848c2ecf20Sopenharmony_ci};
1858c2ecf20Sopenharmony_cistatic const size_t vmw_binding_sr_offsets[] = {
1868c2ecf20Sopenharmony_ci	offsetof(struct vmw_ctx_binding_state, per_shader[0].shader_res),
1878c2ecf20Sopenharmony_ci	offsetof(struct vmw_ctx_binding_state, per_shader[1].shader_res),
1888c2ecf20Sopenharmony_ci	offsetof(struct vmw_ctx_binding_state, per_shader[2].shader_res),
1898c2ecf20Sopenharmony_ci	offsetof(struct vmw_ctx_binding_state, per_shader[3].shader_res),
1908c2ecf20Sopenharmony_ci	offsetof(struct vmw_ctx_binding_state, per_shader[4].shader_res),
1918c2ecf20Sopenharmony_ci	offsetof(struct vmw_ctx_binding_state, per_shader[5].shader_res),
1928c2ecf20Sopenharmony_ci};
1938c2ecf20Sopenharmony_cistatic const size_t vmw_binding_so_target_offsets[] = {
1948c2ecf20Sopenharmony_ci	offsetof(struct vmw_ctx_binding_state, so_targets),
1958c2ecf20Sopenharmony_ci};
1968c2ecf20Sopenharmony_cistatic const size_t vmw_binding_vb_offsets[] = {
1978c2ecf20Sopenharmony_ci	offsetof(struct vmw_ctx_binding_state, vertex_buffers),
1988c2ecf20Sopenharmony_ci};
1998c2ecf20Sopenharmony_cistatic const size_t vmw_binding_ib_offsets[] = {
2008c2ecf20Sopenharmony_ci	offsetof(struct vmw_ctx_binding_state, index_buffer),
2018c2ecf20Sopenharmony_ci};
2028c2ecf20Sopenharmony_cistatic const size_t vmw_binding_uav_offsets[] = {
2038c2ecf20Sopenharmony_ci	offsetof(struct vmw_ctx_binding_state, ua_views[0].views),
2048c2ecf20Sopenharmony_ci};
2058c2ecf20Sopenharmony_cistatic const size_t vmw_binding_cs_uav_offsets[] = {
2068c2ecf20Sopenharmony_ci	offsetof(struct vmw_ctx_binding_state, ua_views[1].views),
2078c2ecf20Sopenharmony_ci};
2088c2ecf20Sopenharmony_cistatic const size_t vmw_binding_so_offsets[] = {
2098c2ecf20Sopenharmony_ci	offsetof(struct vmw_ctx_binding_state, so_state),
2108c2ecf20Sopenharmony_ci};
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_cistatic const struct vmw_binding_info vmw_binding_infos[] = {
2138c2ecf20Sopenharmony_ci	[vmw_ctx_binding_shader] = {
2148c2ecf20Sopenharmony_ci		.size = sizeof(struct vmw_ctx_bindinfo_shader),
2158c2ecf20Sopenharmony_ci		.offsets = vmw_binding_shader_offsets,
2168c2ecf20Sopenharmony_ci		.scrub_func = vmw_binding_scrub_shader},
2178c2ecf20Sopenharmony_ci	[vmw_ctx_binding_rt] = {
2188c2ecf20Sopenharmony_ci		.size = sizeof(struct vmw_ctx_bindinfo_view),
2198c2ecf20Sopenharmony_ci		.offsets = vmw_binding_rt_offsets,
2208c2ecf20Sopenharmony_ci		.scrub_func = vmw_binding_scrub_render_target},
2218c2ecf20Sopenharmony_ci	[vmw_ctx_binding_tex] = {
2228c2ecf20Sopenharmony_ci		.size = sizeof(struct vmw_ctx_bindinfo_tex),
2238c2ecf20Sopenharmony_ci		.offsets = vmw_binding_tex_offsets,
2248c2ecf20Sopenharmony_ci		.scrub_func = vmw_binding_scrub_texture},
2258c2ecf20Sopenharmony_ci	[vmw_ctx_binding_cb] = {
2268c2ecf20Sopenharmony_ci		.size = sizeof(struct vmw_ctx_bindinfo_cb),
2278c2ecf20Sopenharmony_ci		.offsets = vmw_binding_cb_offsets,
2288c2ecf20Sopenharmony_ci		.scrub_func = vmw_binding_scrub_cb},
2298c2ecf20Sopenharmony_ci	[vmw_ctx_binding_dx_shader] = {
2308c2ecf20Sopenharmony_ci		.size = sizeof(struct vmw_ctx_bindinfo_shader),
2318c2ecf20Sopenharmony_ci		.offsets = vmw_binding_shader_offsets,
2328c2ecf20Sopenharmony_ci		.scrub_func = vmw_binding_scrub_dx_shader},
2338c2ecf20Sopenharmony_ci	[vmw_ctx_binding_dx_rt] = {
2348c2ecf20Sopenharmony_ci		.size = sizeof(struct vmw_ctx_bindinfo_view),
2358c2ecf20Sopenharmony_ci		.offsets = vmw_binding_rt_offsets,
2368c2ecf20Sopenharmony_ci		.scrub_func = vmw_binding_scrub_dx_rt},
2378c2ecf20Sopenharmony_ci	[vmw_ctx_binding_sr] = {
2388c2ecf20Sopenharmony_ci		.size = sizeof(struct vmw_ctx_bindinfo_view),
2398c2ecf20Sopenharmony_ci		.offsets = vmw_binding_sr_offsets,
2408c2ecf20Sopenharmony_ci		.scrub_func = vmw_binding_scrub_sr},
2418c2ecf20Sopenharmony_ci	[vmw_ctx_binding_ds] = {
2428c2ecf20Sopenharmony_ci		.size = sizeof(struct vmw_ctx_bindinfo_view),
2438c2ecf20Sopenharmony_ci		.offsets = vmw_binding_dx_ds_offsets,
2448c2ecf20Sopenharmony_ci		.scrub_func = vmw_binding_scrub_dx_rt},
2458c2ecf20Sopenharmony_ci	[vmw_ctx_binding_so_target] = {
2468c2ecf20Sopenharmony_ci		.size = sizeof(struct vmw_ctx_bindinfo_so_target),
2478c2ecf20Sopenharmony_ci		.offsets = vmw_binding_so_target_offsets,
2488c2ecf20Sopenharmony_ci		.scrub_func = vmw_binding_scrub_so_target},
2498c2ecf20Sopenharmony_ci	[vmw_ctx_binding_vb] = {
2508c2ecf20Sopenharmony_ci		.size = sizeof(struct vmw_ctx_bindinfo_vb),
2518c2ecf20Sopenharmony_ci		.offsets = vmw_binding_vb_offsets,
2528c2ecf20Sopenharmony_ci		.scrub_func = vmw_binding_scrub_vb},
2538c2ecf20Sopenharmony_ci	[vmw_ctx_binding_ib] = {
2548c2ecf20Sopenharmony_ci		.size = sizeof(struct vmw_ctx_bindinfo_ib),
2558c2ecf20Sopenharmony_ci		.offsets = vmw_binding_ib_offsets,
2568c2ecf20Sopenharmony_ci		.scrub_func = vmw_binding_scrub_ib},
2578c2ecf20Sopenharmony_ci	[vmw_ctx_binding_uav] = {
2588c2ecf20Sopenharmony_ci		.size = sizeof(struct vmw_ctx_bindinfo_view),
2598c2ecf20Sopenharmony_ci		.offsets = vmw_binding_uav_offsets,
2608c2ecf20Sopenharmony_ci		.scrub_func = vmw_binding_scrub_uav},
2618c2ecf20Sopenharmony_ci	[vmw_ctx_binding_cs_uav] = {
2628c2ecf20Sopenharmony_ci		.size = sizeof(struct vmw_ctx_bindinfo_view),
2638c2ecf20Sopenharmony_ci		.offsets = vmw_binding_cs_uav_offsets,
2648c2ecf20Sopenharmony_ci		.scrub_func = vmw_binding_scrub_cs_uav},
2658c2ecf20Sopenharmony_ci	[vmw_ctx_binding_so] = {
2668c2ecf20Sopenharmony_ci		.size = sizeof(struct vmw_ctx_bindinfo_so),
2678c2ecf20Sopenharmony_ci		.offsets = vmw_binding_so_offsets,
2688c2ecf20Sopenharmony_ci		.scrub_func = vmw_binding_scrub_so},
2698c2ecf20Sopenharmony_ci};
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci/**
2728c2ecf20Sopenharmony_ci * vmw_cbs_context - Return a pointer to the context resource of a
2738c2ecf20Sopenharmony_ci * context binding state tracker.
2748c2ecf20Sopenharmony_ci *
2758c2ecf20Sopenharmony_ci * @cbs: The context binding state tracker.
2768c2ecf20Sopenharmony_ci *
2778c2ecf20Sopenharmony_ci * Provided there are any active bindings, this function will return an
2788c2ecf20Sopenharmony_ci * unreferenced pointer to the context resource that owns the context
2798c2ecf20Sopenharmony_ci * binding state tracker. If there are no active bindings, this function
2808c2ecf20Sopenharmony_ci * will return NULL. Note that the caller must somehow ensure that a reference
2818c2ecf20Sopenharmony_ci * is held on the context resource prior to calling this function.
2828c2ecf20Sopenharmony_ci */
2838c2ecf20Sopenharmony_cistatic const struct vmw_resource *
2848c2ecf20Sopenharmony_civmw_cbs_context(const struct vmw_ctx_binding_state *cbs)
2858c2ecf20Sopenharmony_ci{
2868c2ecf20Sopenharmony_ci	if (list_empty(&cbs->list))
2878c2ecf20Sopenharmony_ci		return NULL;
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	return list_first_entry(&cbs->list, struct vmw_ctx_bindinfo,
2908c2ecf20Sopenharmony_ci				ctx_list)->ctx;
2918c2ecf20Sopenharmony_ci}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci/**
2948c2ecf20Sopenharmony_ci * vmw_binding_loc - determine the struct vmw_ctx_bindinfo slot location.
2958c2ecf20Sopenharmony_ci *
2968c2ecf20Sopenharmony_ci * @cbs: Pointer to a struct vmw_ctx_binding state which holds the slot.
2978c2ecf20Sopenharmony_ci * @bt: The binding type.
2988c2ecf20Sopenharmony_ci * @shader_slot: The shader slot of the binding. If none, then set to 0.
2998c2ecf20Sopenharmony_ci * @slot: The slot of the binding.
3008c2ecf20Sopenharmony_ci */
3018c2ecf20Sopenharmony_cistatic struct vmw_ctx_bindinfo *
3028c2ecf20Sopenharmony_civmw_binding_loc(struct vmw_ctx_binding_state *cbs,
3038c2ecf20Sopenharmony_ci		enum vmw_ctx_binding_type bt, u32 shader_slot, u32 slot)
3048c2ecf20Sopenharmony_ci{
3058c2ecf20Sopenharmony_ci	const struct vmw_binding_info *b = &vmw_binding_infos[bt];
3068c2ecf20Sopenharmony_ci	size_t offset = b->offsets[shader_slot] + b->size*slot;
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	return (struct vmw_ctx_bindinfo *)((u8 *) cbs + offset);
3098c2ecf20Sopenharmony_ci}
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci/**
3128c2ecf20Sopenharmony_ci * vmw_binding_drop: Stop tracking a context binding
3138c2ecf20Sopenharmony_ci *
3148c2ecf20Sopenharmony_ci * @bi: Pointer to binding tracker storage.
3158c2ecf20Sopenharmony_ci *
3168c2ecf20Sopenharmony_ci * Stops tracking a context binding, and re-initializes its storage.
3178c2ecf20Sopenharmony_ci * Typically used when the context binding is replaced with a binding to
3188c2ecf20Sopenharmony_ci * another (or the same, for that matter) resource.
3198c2ecf20Sopenharmony_ci */
3208c2ecf20Sopenharmony_cistatic void vmw_binding_drop(struct vmw_ctx_bindinfo *bi)
3218c2ecf20Sopenharmony_ci{
3228c2ecf20Sopenharmony_ci	list_del(&bi->ctx_list);
3238c2ecf20Sopenharmony_ci	if (!list_empty(&bi->res_list))
3248c2ecf20Sopenharmony_ci		list_del(&bi->res_list);
3258c2ecf20Sopenharmony_ci	bi->ctx = NULL;
3268c2ecf20Sopenharmony_ci}
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci/**
3298c2ecf20Sopenharmony_ci * vmw_binding_add: Start tracking a context binding
3308c2ecf20Sopenharmony_ci *
3318c2ecf20Sopenharmony_ci * @cbs: Pointer to the context binding state tracker.
3328c2ecf20Sopenharmony_ci * @bi: Information about the binding to track.
3338c2ecf20Sopenharmony_ci *
3348c2ecf20Sopenharmony_ci * Starts tracking the binding in the context binding
3358c2ecf20Sopenharmony_ci * state structure @cbs.
3368c2ecf20Sopenharmony_ci */
3378c2ecf20Sopenharmony_civoid vmw_binding_add(struct vmw_ctx_binding_state *cbs,
3388c2ecf20Sopenharmony_ci		    const struct vmw_ctx_bindinfo *bi,
3398c2ecf20Sopenharmony_ci		    u32 shader_slot, u32 slot)
3408c2ecf20Sopenharmony_ci{
3418c2ecf20Sopenharmony_ci	struct vmw_ctx_bindinfo *loc =
3428c2ecf20Sopenharmony_ci		vmw_binding_loc(cbs, bi->bt, shader_slot, slot);
3438c2ecf20Sopenharmony_ci	const struct vmw_binding_info *b = &vmw_binding_infos[bi->bt];
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	if (loc->ctx != NULL)
3468c2ecf20Sopenharmony_ci		vmw_binding_drop(loc);
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	memcpy(loc, bi, b->size);
3498c2ecf20Sopenharmony_ci	loc->scrubbed = false;
3508c2ecf20Sopenharmony_ci	list_add(&loc->ctx_list, &cbs->list);
3518c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&loc->res_list);
3528c2ecf20Sopenharmony_ci}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci/**
3558c2ecf20Sopenharmony_ci * vmw_binding_add_uav_index - Add UAV index for tracking.
3568c2ecf20Sopenharmony_ci * @cbs: Pointer to the context binding state tracker.
3578c2ecf20Sopenharmony_ci * @slot: UAV type to which bind this index.
3588c2ecf20Sopenharmony_ci * @index: The splice index to track.
3598c2ecf20Sopenharmony_ci */
3608c2ecf20Sopenharmony_civoid vmw_binding_add_uav_index(struct vmw_ctx_binding_state *cbs, uint32 slot,
3618c2ecf20Sopenharmony_ci			       uint32 index)
3628c2ecf20Sopenharmony_ci{
3638c2ecf20Sopenharmony_ci	cbs->ua_views[slot].index = index;
3648c2ecf20Sopenharmony_ci}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci/**
3678c2ecf20Sopenharmony_ci * vmw_binding_transfer: Transfer a context binding tracking entry.
3688c2ecf20Sopenharmony_ci *
3698c2ecf20Sopenharmony_ci * @cbs: Pointer to the persistent context binding state tracker.
3708c2ecf20Sopenharmony_ci * @bi: Information about the binding to track.
3718c2ecf20Sopenharmony_ci *
3728c2ecf20Sopenharmony_ci */
3738c2ecf20Sopenharmony_cistatic void vmw_binding_transfer(struct vmw_ctx_binding_state *cbs,
3748c2ecf20Sopenharmony_ci				 const struct vmw_ctx_binding_state *from,
3758c2ecf20Sopenharmony_ci				 const struct vmw_ctx_bindinfo *bi)
3768c2ecf20Sopenharmony_ci{
3778c2ecf20Sopenharmony_ci	size_t offset = (unsigned long)bi - (unsigned long)from;
3788c2ecf20Sopenharmony_ci	struct vmw_ctx_bindinfo *loc = (struct vmw_ctx_bindinfo *)
3798c2ecf20Sopenharmony_ci		((unsigned long) cbs + offset);
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	if (loc->ctx != NULL) {
3828c2ecf20Sopenharmony_ci		WARN_ON(bi->scrubbed);
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci		vmw_binding_drop(loc);
3858c2ecf20Sopenharmony_ci	}
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	if (bi->res != NULL) {
3888c2ecf20Sopenharmony_ci		memcpy(loc, bi, vmw_binding_infos[bi->bt].size);
3898c2ecf20Sopenharmony_ci		list_add_tail(&loc->ctx_list, &cbs->list);
3908c2ecf20Sopenharmony_ci		list_add_tail(&loc->res_list, &loc->res->binding_head);
3918c2ecf20Sopenharmony_ci	}
3928c2ecf20Sopenharmony_ci}
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci/**
3958c2ecf20Sopenharmony_ci * vmw_binding_state_kill - Kill all bindings associated with a
3968c2ecf20Sopenharmony_ci * struct vmw_ctx_binding state structure, and re-initialize the structure.
3978c2ecf20Sopenharmony_ci *
3988c2ecf20Sopenharmony_ci * @cbs: Pointer to the context binding state tracker.
3998c2ecf20Sopenharmony_ci *
4008c2ecf20Sopenharmony_ci * Emits commands to scrub all bindings associated with the
4018c2ecf20Sopenharmony_ci * context binding state tracker. Then re-initializes the whole structure.
4028c2ecf20Sopenharmony_ci */
4038c2ecf20Sopenharmony_civoid vmw_binding_state_kill(struct vmw_ctx_binding_state *cbs)
4048c2ecf20Sopenharmony_ci{
4058c2ecf20Sopenharmony_ci	struct vmw_ctx_bindinfo *entry, *next;
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	vmw_binding_state_scrub(cbs);
4088c2ecf20Sopenharmony_ci	list_for_each_entry_safe(entry, next, &cbs->list, ctx_list)
4098c2ecf20Sopenharmony_ci		vmw_binding_drop(entry);
4108c2ecf20Sopenharmony_ci}
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci/**
4138c2ecf20Sopenharmony_ci * vmw_binding_state_scrub - Scrub all bindings associated with a
4148c2ecf20Sopenharmony_ci * struct vmw_ctx_binding state structure.
4158c2ecf20Sopenharmony_ci *
4168c2ecf20Sopenharmony_ci * @cbs: Pointer to the context binding state tracker.
4178c2ecf20Sopenharmony_ci *
4188c2ecf20Sopenharmony_ci * Emits commands to scrub all bindings associated with the
4198c2ecf20Sopenharmony_ci * context binding state tracker.
4208c2ecf20Sopenharmony_ci */
4218c2ecf20Sopenharmony_civoid vmw_binding_state_scrub(struct vmw_ctx_binding_state *cbs)
4228c2ecf20Sopenharmony_ci{
4238c2ecf20Sopenharmony_ci	struct vmw_ctx_bindinfo *entry;
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	list_for_each_entry(entry, &cbs->list, ctx_list) {
4268c2ecf20Sopenharmony_ci		if (!entry->scrubbed) {
4278c2ecf20Sopenharmony_ci			(void) vmw_binding_infos[entry->bt].scrub_func
4288c2ecf20Sopenharmony_ci				(entry, false);
4298c2ecf20Sopenharmony_ci			entry->scrubbed = true;
4308c2ecf20Sopenharmony_ci		}
4318c2ecf20Sopenharmony_ci	}
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	(void) vmw_binding_emit_dirty(cbs);
4348c2ecf20Sopenharmony_ci}
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci/**
4378c2ecf20Sopenharmony_ci * vmw_binding_res_list_kill - Kill all bindings on a
4388c2ecf20Sopenharmony_ci * resource binding list
4398c2ecf20Sopenharmony_ci *
4408c2ecf20Sopenharmony_ci * @head: list head of resource binding list
4418c2ecf20Sopenharmony_ci *
4428c2ecf20Sopenharmony_ci * Kills all bindings associated with a specific resource. Typically
4438c2ecf20Sopenharmony_ci * called before the resource is destroyed.
4448c2ecf20Sopenharmony_ci */
4458c2ecf20Sopenharmony_civoid vmw_binding_res_list_kill(struct list_head *head)
4468c2ecf20Sopenharmony_ci{
4478c2ecf20Sopenharmony_ci	struct vmw_ctx_bindinfo *entry, *next;
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	vmw_binding_res_list_scrub(head);
4508c2ecf20Sopenharmony_ci	list_for_each_entry_safe(entry, next, head, res_list)
4518c2ecf20Sopenharmony_ci		vmw_binding_drop(entry);
4528c2ecf20Sopenharmony_ci}
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci/**
4558c2ecf20Sopenharmony_ci * vmw_binding_res_list_scrub - Scrub all bindings on a
4568c2ecf20Sopenharmony_ci * resource binding list
4578c2ecf20Sopenharmony_ci *
4588c2ecf20Sopenharmony_ci * @head: list head of resource binding list
4598c2ecf20Sopenharmony_ci *
4608c2ecf20Sopenharmony_ci * Scrub all bindings associated with a specific resource. Typically
4618c2ecf20Sopenharmony_ci * called before the resource is evicted.
4628c2ecf20Sopenharmony_ci */
4638c2ecf20Sopenharmony_civoid vmw_binding_res_list_scrub(struct list_head *head)
4648c2ecf20Sopenharmony_ci{
4658c2ecf20Sopenharmony_ci	struct vmw_ctx_bindinfo *entry;
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	list_for_each_entry(entry, head, res_list) {
4688c2ecf20Sopenharmony_ci		if (!entry->scrubbed) {
4698c2ecf20Sopenharmony_ci			(void) vmw_binding_infos[entry->bt].scrub_func
4708c2ecf20Sopenharmony_ci				(entry, false);
4718c2ecf20Sopenharmony_ci			entry->scrubbed = true;
4728c2ecf20Sopenharmony_ci		}
4738c2ecf20Sopenharmony_ci	}
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	list_for_each_entry(entry, head, res_list) {
4768c2ecf20Sopenharmony_ci		struct vmw_ctx_binding_state *cbs =
4778c2ecf20Sopenharmony_ci			vmw_context_binding_state(entry->ctx);
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci		(void) vmw_binding_emit_dirty(cbs);
4808c2ecf20Sopenharmony_ci	}
4818c2ecf20Sopenharmony_ci}
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci/**
4858c2ecf20Sopenharmony_ci * vmw_binding_state_commit - Commit staged binding info
4868c2ecf20Sopenharmony_ci *
4878c2ecf20Sopenharmony_ci * @ctx: Pointer to context to commit the staged binding info to.
4888c2ecf20Sopenharmony_ci * @from: Staged binding info built during execbuf.
4898c2ecf20Sopenharmony_ci * @scrubbed: Transfer only scrubbed bindings.
4908c2ecf20Sopenharmony_ci *
4918c2ecf20Sopenharmony_ci * Transfers binding info from a temporary structure
4928c2ecf20Sopenharmony_ci * (typically used by execbuf) to the persistent
4938c2ecf20Sopenharmony_ci * structure in the context. This can be done once commands have been
4948c2ecf20Sopenharmony_ci * submitted to hardware
4958c2ecf20Sopenharmony_ci */
4968c2ecf20Sopenharmony_civoid vmw_binding_state_commit(struct vmw_ctx_binding_state *to,
4978c2ecf20Sopenharmony_ci			      struct vmw_ctx_binding_state *from)
4988c2ecf20Sopenharmony_ci{
4998c2ecf20Sopenharmony_ci	struct vmw_ctx_bindinfo *entry, *next;
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	list_for_each_entry_safe(entry, next, &from->list, ctx_list) {
5028c2ecf20Sopenharmony_ci		vmw_binding_transfer(to, from, entry);
5038c2ecf20Sopenharmony_ci		vmw_binding_drop(entry);
5048c2ecf20Sopenharmony_ci	}
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	/* Also transfer uav splice indices */
5078c2ecf20Sopenharmony_ci	to->ua_views[0].index = from->ua_views[0].index;
5088c2ecf20Sopenharmony_ci	to->ua_views[1].index = from->ua_views[1].index;
5098c2ecf20Sopenharmony_ci}
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci/**
5128c2ecf20Sopenharmony_ci * vmw_binding_rebind_all - Rebind all scrubbed bindings of a context
5138c2ecf20Sopenharmony_ci *
5148c2ecf20Sopenharmony_ci * @ctx: The context resource
5158c2ecf20Sopenharmony_ci *
5168c2ecf20Sopenharmony_ci * Walks through the context binding list and rebinds all scrubbed
5178c2ecf20Sopenharmony_ci * resources.
5188c2ecf20Sopenharmony_ci */
5198c2ecf20Sopenharmony_ciint vmw_binding_rebind_all(struct vmw_ctx_binding_state *cbs)
5208c2ecf20Sopenharmony_ci{
5218c2ecf20Sopenharmony_ci	struct vmw_ctx_bindinfo *entry;
5228c2ecf20Sopenharmony_ci	int ret;
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	list_for_each_entry(entry, &cbs->list, ctx_list) {
5258c2ecf20Sopenharmony_ci		if (likely(!entry->scrubbed))
5268c2ecf20Sopenharmony_ci			continue;
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci		if ((entry->res == NULL || entry->res->id ==
5298c2ecf20Sopenharmony_ci			    SVGA3D_INVALID_ID))
5308c2ecf20Sopenharmony_ci			continue;
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci		ret = vmw_binding_infos[entry->bt].scrub_func(entry, true);
5338c2ecf20Sopenharmony_ci		if (unlikely(ret != 0))
5348c2ecf20Sopenharmony_ci			return ret;
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci		entry->scrubbed = false;
5378c2ecf20Sopenharmony_ci	}
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	return vmw_binding_emit_dirty(cbs);
5408c2ecf20Sopenharmony_ci}
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci/**
5438c2ecf20Sopenharmony_ci * vmw_binding_scrub_shader - scrub a shader binding from a context.
5448c2ecf20Sopenharmony_ci *
5458c2ecf20Sopenharmony_ci * @bi: single binding information.
5468c2ecf20Sopenharmony_ci * @rebind: Whether to issue a bind instead of scrub command.
5478c2ecf20Sopenharmony_ci */
5488c2ecf20Sopenharmony_cistatic int vmw_binding_scrub_shader(struct vmw_ctx_bindinfo *bi, bool rebind)
5498c2ecf20Sopenharmony_ci{
5508c2ecf20Sopenharmony_ci	struct vmw_ctx_bindinfo_shader *binding =
5518c2ecf20Sopenharmony_ci		container_of(bi, typeof(*binding), bi);
5528c2ecf20Sopenharmony_ci	struct vmw_private *dev_priv = bi->ctx->dev_priv;
5538c2ecf20Sopenharmony_ci	struct {
5548c2ecf20Sopenharmony_ci		SVGA3dCmdHeader header;
5558c2ecf20Sopenharmony_ci		SVGA3dCmdSetShader body;
5568c2ecf20Sopenharmony_ci	} *cmd;
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	cmd = VMW_FIFO_RESERVE(dev_priv, sizeof(*cmd));
5598c2ecf20Sopenharmony_ci	if (unlikely(cmd == NULL))
5608c2ecf20Sopenharmony_ci		return -ENOMEM;
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	cmd->header.id = SVGA_3D_CMD_SET_SHADER;
5638c2ecf20Sopenharmony_ci	cmd->header.size = sizeof(cmd->body);
5648c2ecf20Sopenharmony_ci	cmd->body.cid = bi->ctx->id;
5658c2ecf20Sopenharmony_ci	cmd->body.type = binding->shader_slot + SVGA3D_SHADERTYPE_MIN;
5668c2ecf20Sopenharmony_ci	cmd->body.shid = ((rebind) ? bi->res->id : SVGA3D_INVALID_ID);
5678c2ecf20Sopenharmony_ci	vmw_fifo_commit(dev_priv, sizeof(*cmd));
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci	return 0;
5708c2ecf20Sopenharmony_ci}
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci/**
5738c2ecf20Sopenharmony_ci * vmw_binding_scrub_render_target - scrub a render target binding
5748c2ecf20Sopenharmony_ci * from a context.
5758c2ecf20Sopenharmony_ci *
5768c2ecf20Sopenharmony_ci * @bi: single binding information.
5778c2ecf20Sopenharmony_ci * @rebind: Whether to issue a bind instead of scrub command.
5788c2ecf20Sopenharmony_ci */
5798c2ecf20Sopenharmony_cistatic int vmw_binding_scrub_render_target(struct vmw_ctx_bindinfo *bi,
5808c2ecf20Sopenharmony_ci					   bool rebind)
5818c2ecf20Sopenharmony_ci{
5828c2ecf20Sopenharmony_ci	struct vmw_ctx_bindinfo_view *binding =
5838c2ecf20Sopenharmony_ci		container_of(bi, typeof(*binding), bi);
5848c2ecf20Sopenharmony_ci	struct vmw_private *dev_priv = bi->ctx->dev_priv;
5858c2ecf20Sopenharmony_ci	struct {
5868c2ecf20Sopenharmony_ci		SVGA3dCmdHeader header;
5878c2ecf20Sopenharmony_ci		SVGA3dCmdSetRenderTarget body;
5888c2ecf20Sopenharmony_ci	} *cmd;
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	cmd = VMW_FIFO_RESERVE(dev_priv, sizeof(*cmd));
5918c2ecf20Sopenharmony_ci	if (unlikely(cmd == NULL))
5928c2ecf20Sopenharmony_ci		return -ENOMEM;
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci	cmd->header.id = SVGA_3D_CMD_SETRENDERTARGET;
5958c2ecf20Sopenharmony_ci	cmd->header.size = sizeof(cmd->body);
5968c2ecf20Sopenharmony_ci	cmd->body.cid = bi->ctx->id;
5978c2ecf20Sopenharmony_ci	cmd->body.type = binding->slot;
5988c2ecf20Sopenharmony_ci	cmd->body.target.sid = ((rebind) ? bi->res->id : SVGA3D_INVALID_ID);
5998c2ecf20Sopenharmony_ci	cmd->body.target.face = 0;
6008c2ecf20Sopenharmony_ci	cmd->body.target.mipmap = 0;
6018c2ecf20Sopenharmony_ci	vmw_fifo_commit(dev_priv, sizeof(*cmd));
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci	return 0;
6048c2ecf20Sopenharmony_ci}
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci/**
6078c2ecf20Sopenharmony_ci * vmw_binding_scrub_texture - scrub a texture binding from a context.
6088c2ecf20Sopenharmony_ci *
6098c2ecf20Sopenharmony_ci * @bi: single binding information.
6108c2ecf20Sopenharmony_ci * @rebind: Whether to issue a bind instead of scrub command.
6118c2ecf20Sopenharmony_ci *
6128c2ecf20Sopenharmony_ci * TODO: Possibly complement this function with a function that takes
6138c2ecf20Sopenharmony_ci * a list of texture bindings and combines them to a single command.
6148c2ecf20Sopenharmony_ci */
6158c2ecf20Sopenharmony_cistatic int vmw_binding_scrub_texture(struct vmw_ctx_bindinfo *bi,
6168c2ecf20Sopenharmony_ci				     bool rebind)
6178c2ecf20Sopenharmony_ci{
6188c2ecf20Sopenharmony_ci	struct vmw_ctx_bindinfo_tex *binding =
6198c2ecf20Sopenharmony_ci		container_of(bi, typeof(*binding), bi);
6208c2ecf20Sopenharmony_ci	struct vmw_private *dev_priv = bi->ctx->dev_priv;
6218c2ecf20Sopenharmony_ci	struct {
6228c2ecf20Sopenharmony_ci		SVGA3dCmdHeader header;
6238c2ecf20Sopenharmony_ci		struct {
6248c2ecf20Sopenharmony_ci			SVGA3dCmdSetTextureState c;
6258c2ecf20Sopenharmony_ci			SVGA3dTextureState s1;
6268c2ecf20Sopenharmony_ci		} body;
6278c2ecf20Sopenharmony_ci	} *cmd;
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci	cmd = VMW_FIFO_RESERVE(dev_priv, sizeof(*cmd));
6308c2ecf20Sopenharmony_ci	if (unlikely(cmd == NULL))
6318c2ecf20Sopenharmony_ci		return -ENOMEM;
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci	cmd->header.id = SVGA_3D_CMD_SETTEXTURESTATE;
6348c2ecf20Sopenharmony_ci	cmd->header.size = sizeof(cmd->body);
6358c2ecf20Sopenharmony_ci	cmd->body.c.cid = bi->ctx->id;
6368c2ecf20Sopenharmony_ci	cmd->body.s1.stage = binding->texture_stage;
6378c2ecf20Sopenharmony_ci	cmd->body.s1.name = SVGA3D_TS_BIND_TEXTURE;
6388c2ecf20Sopenharmony_ci	cmd->body.s1.value = ((rebind) ? bi->res->id : SVGA3D_INVALID_ID);
6398c2ecf20Sopenharmony_ci	vmw_fifo_commit(dev_priv, sizeof(*cmd));
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	return 0;
6428c2ecf20Sopenharmony_ci}
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci/**
6458c2ecf20Sopenharmony_ci * vmw_binding_scrub_dx_shader - scrub a dx shader binding from a context.
6468c2ecf20Sopenharmony_ci *
6478c2ecf20Sopenharmony_ci * @bi: single binding information.
6488c2ecf20Sopenharmony_ci * @rebind: Whether to issue a bind instead of scrub command.
6498c2ecf20Sopenharmony_ci */
6508c2ecf20Sopenharmony_cistatic int vmw_binding_scrub_dx_shader(struct vmw_ctx_bindinfo *bi, bool rebind)
6518c2ecf20Sopenharmony_ci{
6528c2ecf20Sopenharmony_ci	struct vmw_ctx_bindinfo_shader *binding =
6538c2ecf20Sopenharmony_ci		container_of(bi, typeof(*binding), bi);
6548c2ecf20Sopenharmony_ci	struct vmw_private *dev_priv = bi->ctx->dev_priv;
6558c2ecf20Sopenharmony_ci	struct {
6568c2ecf20Sopenharmony_ci		SVGA3dCmdHeader header;
6578c2ecf20Sopenharmony_ci		SVGA3dCmdDXSetShader body;
6588c2ecf20Sopenharmony_ci	} *cmd;
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci	cmd = VMW_FIFO_RESERVE_DX(dev_priv, sizeof(*cmd), bi->ctx->id);
6618c2ecf20Sopenharmony_ci	if (unlikely(cmd == NULL))
6628c2ecf20Sopenharmony_ci		return -ENOMEM;
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci	cmd->header.id = SVGA_3D_CMD_DX_SET_SHADER;
6658c2ecf20Sopenharmony_ci	cmd->header.size = sizeof(cmd->body);
6668c2ecf20Sopenharmony_ci	cmd->body.type = binding->shader_slot + SVGA3D_SHADERTYPE_MIN;
6678c2ecf20Sopenharmony_ci	cmd->body.shaderId = ((rebind) ? bi->res->id : SVGA3D_INVALID_ID);
6688c2ecf20Sopenharmony_ci	vmw_fifo_commit(dev_priv, sizeof(*cmd));
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	return 0;
6718c2ecf20Sopenharmony_ci}
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci/**
6748c2ecf20Sopenharmony_ci * vmw_binding_scrub_cb - scrub a constant buffer binding from a context.
6758c2ecf20Sopenharmony_ci *
6768c2ecf20Sopenharmony_ci * @bi: single binding information.
6778c2ecf20Sopenharmony_ci * @rebind: Whether to issue a bind instead of scrub command.
6788c2ecf20Sopenharmony_ci */
6798c2ecf20Sopenharmony_cistatic int vmw_binding_scrub_cb(struct vmw_ctx_bindinfo *bi, bool rebind)
6808c2ecf20Sopenharmony_ci{
6818c2ecf20Sopenharmony_ci	struct vmw_ctx_bindinfo_cb *binding =
6828c2ecf20Sopenharmony_ci		container_of(bi, typeof(*binding), bi);
6838c2ecf20Sopenharmony_ci	struct vmw_private *dev_priv = bi->ctx->dev_priv;
6848c2ecf20Sopenharmony_ci	struct {
6858c2ecf20Sopenharmony_ci		SVGA3dCmdHeader header;
6868c2ecf20Sopenharmony_ci		SVGA3dCmdDXSetSingleConstantBuffer body;
6878c2ecf20Sopenharmony_ci	} *cmd;
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci	cmd = VMW_FIFO_RESERVE_DX(dev_priv, sizeof(*cmd), bi->ctx->id);
6908c2ecf20Sopenharmony_ci	if (unlikely(cmd == NULL))
6918c2ecf20Sopenharmony_ci		return -ENOMEM;
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	cmd->header.id = SVGA_3D_CMD_DX_SET_SINGLE_CONSTANT_BUFFER;
6948c2ecf20Sopenharmony_ci	cmd->header.size = sizeof(cmd->body);
6958c2ecf20Sopenharmony_ci	cmd->body.slot = binding->slot;
6968c2ecf20Sopenharmony_ci	cmd->body.type = binding->shader_slot + SVGA3D_SHADERTYPE_MIN;
6978c2ecf20Sopenharmony_ci	if (rebind) {
6988c2ecf20Sopenharmony_ci		cmd->body.offsetInBytes = binding->offset;
6998c2ecf20Sopenharmony_ci		cmd->body.sizeInBytes = binding->size;
7008c2ecf20Sopenharmony_ci		cmd->body.sid = bi->res->id;
7018c2ecf20Sopenharmony_ci	} else {
7028c2ecf20Sopenharmony_ci		cmd->body.offsetInBytes = 0;
7038c2ecf20Sopenharmony_ci		cmd->body.sizeInBytes = 0;
7048c2ecf20Sopenharmony_ci		cmd->body.sid = SVGA3D_INVALID_ID;
7058c2ecf20Sopenharmony_ci	}
7068c2ecf20Sopenharmony_ci	vmw_fifo_commit(dev_priv, sizeof(*cmd));
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci	return 0;
7098c2ecf20Sopenharmony_ci}
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci/**
7128c2ecf20Sopenharmony_ci * vmw_collect_view_ids - Build view id data for a view binding command
7138c2ecf20Sopenharmony_ci * without checking which bindings actually need to be emitted
7148c2ecf20Sopenharmony_ci *
7158c2ecf20Sopenharmony_ci * @cbs: Pointer to the context's struct vmw_ctx_binding_state
7168c2ecf20Sopenharmony_ci * @bi: Pointer to where the binding info array is stored in @cbs
7178c2ecf20Sopenharmony_ci * @max_num: Maximum number of entries in the @bi array.
7188c2ecf20Sopenharmony_ci *
7198c2ecf20Sopenharmony_ci * Scans the @bi array for bindings and builds a buffer of view id data.
7208c2ecf20Sopenharmony_ci * Stops at the first non-existing binding in the @bi array.
7218c2ecf20Sopenharmony_ci * On output, @cbs->bind_cmd_count contains the number of bindings to be
7228c2ecf20Sopenharmony_ci * emitted, @cbs->bind_first_slot is set to zero, and @cbs->bind_cmd_buffer
7238c2ecf20Sopenharmony_ci * contains the command data.
7248c2ecf20Sopenharmony_ci */
7258c2ecf20Sopenharmony_cistatic void vmw_collect_view_ids(struct vmw_ctx_binding_state *cbs,
7268c2ecf20Sopenharmony_ci				 const struct vmw_ctx_bindinfo *bi,
7278c2ecf20Sopenharmony_ci				 u32 max_num)
7288c2ecf20Sopenharmony_ci{
7298c2ecf20Sopenharmony_ci	const struct vmw_ctx_bindinfo_view *biv =
7308c2ecf20Sopenharmony_ci		container_of(bi, struct vmw_ctx_bindinfo_view, bi);
7318c2ecf20Sopenharmony_ci	unsigned long i;
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci	cbs->bind_cmd_count = 0;
7348c2ecf20Sopenharmony_ci	cbs->bind_first_slot = 0;
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_ci	for (i = 0; i < max_num; ++i, ++biv) {
7378c2ecf20Sopenharmony_ci		if (!biv->bi.ctx)
7388c2ecf20Sopenharmony_ci			break;
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci		cbs->bind_cmd_buffer[cbs->bind_cmd_count++] =
7418c2ecf20Sopenharmony_ci			((biv->bi.scrubbed) ?
7428c2ecf20Sopenharmony_ci			 SVGA3D_INVALID_ID : biv->bi.res->id);
7438c2ecf20Sopenharmony_ci	}
7448c2ecf20Sopenharmony_ci}
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci/**
7478c2ecf20Sopenharmony_ci * vmw_collect_dirty_view_ids - Build view id data for a view binding command
7488c2ecf20Sopenharmony_ci *
7498c2ecf20Sopenharmony_ci * @cbs: Pointer to the context's struct vmw_ctx_binding_state
7508c2ecf20Sopenharmony_ci * @bi: Pointer to where the binding info array is stored in @cbs
7518c2ecf20Sopenharmony_ci * @dirty: Bitmap indicating which bindings need to be emitted.
7528c2ecf20Sopenharmony_ci * @max_num: Maximum number of entries in the @bi array.
7538c2ecf20Sopenharmony_ci *
7548c2ecf20Sopenharmony_ci * Scans the @bi array for bindings that need to be emitted and
7558c2ecf20Sopenharmony_ci * builds a buffer of view id data.
7568c2ecf20Sopenharmony_ci * On output, @cbs->bind_cmd_count contains the number of bindings to be
7578c2ecf20Sopenharmony_ci * emitted, @cbs->bind_first_slot indicates the index of the first emitted
7588c2ecf20Sopenharmony_ci * binding, and @cbs->bind_cmd_buffer contains the command data.
7598c2ecf20Sopenharmony_ci */
7608c2ecf20Sopenharmony_cistatic void vmw_collect_dirty_view_ids(struct vmw_ctx_binding_state *cbs,
7618c2ecf20Sopenharmony_ci				       const struct vmw_ctx_bindinfo *bi,
7628c2ecf20Sopenharmony_ci				       unsigned long *dirty,
7638c2ecf20Sopenharmony_ci				       u32 max_num)
7648c2ecf20Sopenharmony_ci{
7658c2ecf20Sopenharmony_ci	const struct vmw_ctx_bindinfo_view *biv =
7668c2ecf20Sopenharmony_ci		container_of(bi, struct vmw_ctx_bindinfo_view, bi);
7678c2ecf20Sopenharmony_ci	unsigned long i, next_bit;
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci	cbs->bind_cmd_count = 0;
7708c2ecf20Sopenharmony_ci	i = find_first_bit(dirty, max_num);
7718c2ecf20Sopenharmony_ci	next_bit = i;
7728c2ecf20Sopenharmony_ci	cbs->bind_first_slot = i;
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci	biv += i;
7758c2ecf20Sopenharmony_ci	for (; i < max_num; ++i, ++biv) {
7768c2ecf20Sopenharmony_ci		cbs->bind_cmd_buffer[cbs->bind_cmd_count++] =
7778c2ecf20Sopenharmony_ci			((!biv->bi.ctx || biv->bi.scrubbed) ?
7788c2ecf20Sopenharmony_ci			 SVGA3D_INVALID_ID : biv->bi.res->id);
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci		if (next_bit == i) {
7818c2ecf20Sopenharmony_ci			next_bit = find_next_bit(dirty, max_num, i + 1);
7828c2ecf20Sopenharmony_ci			if (next_bit >= max_num)
7838c2ecf20Sopenharmony_ci				break;
7848c2ecf20Sopenharmony_ci		}
7858c2ecf20Sopenharmony_ci	}
7868c2ecf20Sopenharmony_ci}
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ci/**
7898c2ecf20Sopenharmony_ci * vmw_binding_emit_set_sr - Issue delayed DX shader resource binding commands
7908c2ecf20Sopenharmony_ci *
7918c2ecf20Sopenharmony_ci * @cbs: Pointer to the context's struct vmw_ctx_binding_state
7928c2ecf20Sopenharmony_ci */
7938c2ecf20Sopenharmony_cistatic int vmw_emit_set_sr(struct vmw_ctx_binding_state *cbs,
7948c2ecf20Sopenharmony_ci			   int shader_slot)
7958c2ecf20Sopenharmony_ci{
7968c2ecf20Sopenharmony_ci	const struct vmw_ctx_bindinfo *loc =
7978c2ecf20Sopenharmony_ci		&cbs->per_shader[shader_slot].shader_res[0].bi;
7988c2ecf20Sopenharmony_ci	struct {
7998c2ecf20Sopenharmony_ci		SVGA3dCmdHeader header;
8008c2ecf20Sopenharmony_ci		SVGA3dCmdDXSetShaderResources body;
8018c2ecf20Sopenharmony_ci	} *cmd;
8028c2ecf20Sopenharmony_ci	size_t cmd_size, view_id_size;
8038c2ecf20Sopenharmony_ci	const struct vmw_resource *ctx = vmw_cbs_context(cbs);
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_ci	vmw_collect_dirty_view_ids(cbs, loc,
8068c2ecf20Sopenharmony_ci				   cbs->per_shader[shader_slot].dirty_sr,
8078c2ecf20Sopenharmony_ci				   SVGA3D_DX_MAX_SRVIEWS);
8088c2ecf20Sopenharmony_ci	if (cbs->bind_cmd_count == 0)
8098c2ecf20Sopenharmony_ci		return 0;
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci	view_id_size = cbs->bind_cmd_count*sizeof(uint32);
8128c2ecf20Sopenharmony_ci	cmd_size = sizeof(*cmd) + view_id_size;
8138c2ecf20Sopenharmony_ci	cmd = VMW_FIFO_RESERVE_DX(ctx->dev_priv, cmd_size, ctx->id);
8148c2ecf20Sopenharmony_ci	if (unlikely(cmd == NULL))
8158c2ecf20Sopenharmony_ci		return -ENOMEM;
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci	cmd->header.id = SVGA_3D_CMD_DX_SET_SHADER_RESOURCES;
8188c2ecf20Sopenharmony_ci	cmd->header.size = sizeof(cmd->body) + view_id_size;
8198c2ecf20Sopenharmony_ci	cmd->body.type = shader_slot + SVGA3D_SHADERTYPE_MIN;
8208c2ecf20Sopenharmony_ci	cmd->body.startView = cbs->bind_first_slot;
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci	memcpy(&cmd[1], cbs->bind_cmd_buffer, view_id_size);
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci	vmw_fifo_commit(ctx->dev_priv, cmd_size);
8258c2ecf20Sopenharmony_ci	bitmap_clear(cbs->per_shader[shader_slot].dirty_sr,
8268c2ecf20Sopenharmony_ci		     cbs->bind_first_slot, cbs->bind_cmd_count);
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_ci	return 0;
8298c2ecf20Sopenharmony_ci}
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci/**
8328c2ecf20Sopenharmony_ci * vmw_binding_emit_set_rt - Issue delayed DX rendertarget binding commands
8338c2ecf20Sopenharmony_ci *
8348c2ecf20Sopenharmony_ci * @cbs: Pointer to the context's struct vmw_ctx_binding_state
8358c2ecf20Sopenharmony_ci */
8368c2ecf20Sopenharmony_cistatic int vmw_emit_set_rt(struct vmw_ctx_binding_state *cbs)
8378c2ecf20Sopenharmony_ci{
8388c2ecf20Sopenharmony_ci	const struct vmw_ctx_bindinfo *loc = &cbs->render_targets[0].bi;
8398c2ecf20Sopenharmony_ci	struct {
8408c2ecf20Sopenharmony_ci		SVGA3dCmdHeader header;
8418c2ecf20Sopenharmony_ci		SVGA3dCmdDXSetRenderTargets body;
8428c2ecf20Sopenharmony_ci	} *cmd;
8438c2ecf20Sopenharmony_ci	size_t cmd_size, view_id_size;
8448c2ecf20Sopenharmony_ci	const struct vmw_resource *ctx = vmw_cbs_context(cbs);
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci	vmw_collect_view_ids(cbs, loc, SVGA3D_MAX_SIMULTANEOUS_RENDER_TARGETS);
8478c2ecf20Sopenharmony_ci	view_id_size = cbs->bind_cmd_count*sizeof(uint32);
8488c2ecf20Sopenharmony_ci	cmd_size = sizeof(*cmd) + view_id_size;
8498c2ecf20Sopenharmony_ci	cmd = VMW_FIFO_RESERVE_DX(ctx->dev_priv, cmd_size, ctx->id);
8508c2ecf20Sopenharmony_ci	if (unlikely(cmd == NULL))
8518c2ecf20Sopenharmony_ci		return -ENOMEM;
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	cmd->header.id = SVGA_3D_CMD_DX_SET_RENDERTARGETS;
8548c2ecf20Sopenharmony_ci	cmd->header.size = sizeof(cmd->body) + view_id_size;
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_ci	if (cbs->ds_view.bi.ctx && !cbs->ds_view.bi.scrubbed)
8578c2ecf20Sopenharmony_ci		cmd->body.depthStencilViewId = cbs->ds_view.bi.res->id;
8588c2ecf20Sopenharmony_ci	else
8598c2ecf20Sopenharmony_ci		cmd->body.depthStencilViewId = SVGA3D_INVALID_ID;
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci	memcpy(&cmd[1], cbs->bind_cmd_buffer, view_id_size);
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ci	vmw_fifo_commit(ctx->dev_priv, cmd_size);
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci	return 0;
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_ci}
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_ci/**
8708c2ecf20Sopenharmony_ci * vmw_collect_so_targets - Build SVGA3dSoTarget data for a binding command
8718c2ecf20Sopenharmony_ci * without checking which bindings actually need to be emitted
8728c2ecf20Sopenharmony_ci *
8738c2ecf20Sopenharmony_ci * @cbs: Pointer to the context's struct vmw_ctx_binding_state
8748c2ecf20Sopenharmony_ci * @bi: Pointer to where the binding info array is stored in @cbs
8758c2ecf20Sopenharmony_ci * @max_num: Maximum number of entries in the @bi array.
8768c2ecf20Sopenharmony_ci *
8778c2ecf20Sopenharmony_ci * Scans the @bi array for bindings and builds a buffer of SVGA3dSoTarget data.
8788c2ecf20Sopenharmony_ci * Stops at the first non-existing binding in the @bi array.
8798c2ecf20Sopenharmony_ci * On output, @cbs->bind_cmd_count contains the number of bindings to be
8808c2ecf20Sopenharmony_ci * emitted, @cbs->bind_first_slot is set to zero, and @cbs->bind_cmd_buffer
8818c2ecf20Sopenharmony_ci * contains the command data.
8828c2ecf20Sopenharmony_ci */
8838c2ecf20Sopenharmony_cistatic void vmw_collect_so_targets(struct vmw_ctx_binding_state *cbs,
8848c2ecf20Sopenharmony_ci				   const struct vmw_ctx_bindinfo *bi,
8858c2ecf20Sopenharmony_ci				   u32 max_num)
8868c2ecf20Sopenharmony_ci{
8878c2ecf20Sopenharmony_ci	const struct vmw_ctx_bindinfo_so_target *biso =
8888c2ecf20Sopenharmony_ci		container_of(bi, struct vmw_ctx_bindinfo_so_target, bi);
8898c2ecf20Sopenharmony_ci	unsigned long i;
8908c2ecf20Sopenharmony_ci	SVGA3dSoTarget *so_buffer = (SVGA3dSoTarget *) cbs->bind_cmd_buffer;
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	cbs->bind_cmd_count = 0;
8938c2ecf20Sopenharmony_ci	cbs->bind_first_slot = 0;
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	for (i = 0; i < max_num; ++i, ++biso, ++so_buffer,
8968c2ecf20Sopenharmony_ci		    ++cbs->bind_cmd_count) {
8978c2ecf20Sopenharmony_ci		if (!biso->bi.ctx)
8988c2ecf20Sopenharmony_ci			break;
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci		if (!biso->bi.scrubbed) {
9018c2ecf20Sopenharmony_ci			so_buffer->sid = biso->bi.res->id;
9028c2ecf20Sopenharmony_ci			so_buffer->offset = biso->offset;
9038c2ecf20Sopenharmony_ci			so_buffer->sizeInBytes = biso->size;
9048c2ecf20Sopenharmony_ci		} else {
9058c2ecf20Sopenharmony_ci			so_buffer->sid = SVGA3D_INVALID_ID;
9068c2ecf20Sopenharmony_ci			so_buffer->offset = 0;
9078c2ecf20Sopenharmony_ci			so_buffer->sizeInBytes = 0;
9088c2ecf20Sopenharmony_ci		}
9098c2ecf20Sopenharmony_ci	}
9108c2ecf20Sopenharmony_ci}
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci/**
9138c2ecf20Sopenharmony_ci * vmw_emit_set_so_target - Issue delayed streamout binding commands
9148c2ecf20Sopenharmony_ci *
9158c2ecf20Sopenharmony_ci * @cbs: Pointer to the context's struct vmw_ctx_binding_state
9168c2ecf20Sopenharmony_ci */
9178c2ecf20Sopenharmony_cistatic int vmw_emit_set_so_target(struct vmw_ctx_binding_state *cbs)
9188c2ecf20Sopenharmony_ci{
9198c2ecf20Sopenharmony_ci	const struct vmw_ctx_bindinfo *loc = &cbs->so_targets[0].bi;
9208c2ecf20Sopenharmony_ci	struct {
9218c2ecf20Sopenharmony_ci		SVGA3dCmdHeader header;
9228c2ecf20Sopenharmony_ci		SVGA3dCmdDXSetSOTargets body;
9238c2ecf20Sopenharmony_ci	} *cmd;
9248c2ecf20Sopenharmony_ci	size_t cmd_size, so_target_size;
9258c2ecf20Sopenharmony_ci	const struct vmw_resource *ctx = vmw_cbs_context(cbs);
9268c2ecf20Sopenharmony_ci
9278c2ecf20Sopenharmony_ci	vmw_collect_so_targets(cbs, loc, SVGA3D_DX_MAX_SOTARGETS);
9288c2ecf20Sopenharmony_ci	if (cbs->bind_cmd_count == 0)
9298c2ecf20Sopenharmony_ci		return 0;
9308c2ecf20Sopenharmony_ci
9318c2ecf20Sopenharmony_ci	so_target_size = cbs->bind_cmd_count*sizeof(SVGA3dSoTarget);
9328c2ecf20Sopenharmony_ci	cmd_size = sizeof(*cmd) + so_target_size;
9338c2ecf20Sopenharmony_ci	cmd = VMW_FIFO_RESERVE_DX(ctx->dev_priv, cmd_size, ctx->id);
9348c2ecf20Sopenharmony_ci	if (unlikely(cmd == NULL))
9358c2ecf20Sopenharmony_ci		return -ENOMEM;
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_ci	cmd->header.id = SVGA_3D_CMD_DX_SET_SOTARGETS;
9388c2ecf20Sopenharmony_ci	cmd->header.size = sizeof(cmd->body) + so_target_size;
9398c2ecf20Sopenharmony_ci	memcpy(&cmd[1], cbs->bind_cmd_buffer, so_target_size);
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_ci	vmw_fifo_commit(ctx->dev_priv, cmd_size);
9428c2ecf20Sopenharmony_ci
9438c2ecf20Sopenharmony_ci	return 0;
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci}
9468c2ecf20Sopenharmony_ci
9478c2ecf20Sopenharmony_ci/**
9488c2ecf20Sopenharmony_ci * vmw_binding_emit_dirty_ps - Issue delayed per shader binding commands
9498c2ecf20Sopenharmony_ci *
9508c2ecf20Sopenharmony_ci * @cbs: Pointer to the context's struct vmw_ctx_binding_state
9518c2ecf20Sopenharmony_ci *
9528c2ecf20Sopenharmony_ci */
9538c2ecf20Sopenharmony_cistatic int vmw_binding_emit_dirty_ps(struct vmw_ctx_binding_state *cbs)
9548c2ecf20Sopenharmony_ci{
9558c2ecf20Sopenharmony_ci	struct vmw_dx_shader_bindings *sb = &cbs->per_shader[0];
9568c2ecf20Sopenharmony_ci	u32 i;
9578c2ecf20Sopenharmony_ci	int ret;
9588c2ecf20Sopenharmony_ci
9598c2ecf20Sopenharmony_ci	for (i = 0; i < SVGA3D_NUM_SHADERTYPE_DX10; ++i, ++sb) {
9608c2ecf20Sopenharmony_ci		if (!test_bit(VMW_BINDING_PS_SR_BIT, &sb->dirty))
9618c2ecf20Sopenharmony_ci			continue;
9628c2ecf20Sopenharmony_ci
9638c2ecf20Sopenharmony_ci		ret = vmw_emit_set_sr(cbs, i);
9648c2ecf20Sopenharmony_ci		if (ret)
9658c2ecf20Sopenharmony_ci			break;
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_ci		__clear_bit(VMW_BINDING_PS_SR_BIT, &sb->dirty);
9688c2ecf20Sopenharmony_ci	}
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_ci	return 0;
9718c2ecf20Sopenharmony_ci}
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_ci/**
9748c2ecf20Sopenharmony_ci * vmw_collect_dirty_vbs - Build SVGA3dVertexBuffer data for a
9758c2ecf20Sopenharmony_ci * SVGA3dCmdDXSetVertexBuffers command
9768c2ecf20Sopenharmony_ci *
9778c2ecf20Sopenharmony_ci * @cbs: Pointer to the context's struct vmw_ctx_binding_state
9788c2ecf20Sopenharmony_ci * @bi: Pointer to where the binding info array is stored in @cbs
9798c2ecf20Sopenharmony_ci * @dirty: Bitmap indicating which bindings need to be emitted.
9808c2ecf20Sopenharmony_ci * @max_num: Maximum number of entries in the @bi array.
9818c2ecf20Sopenharmony_ci *
9828c2ecf20Sopenharmony_ci * Scans the @bi array for bindings that need to be emitted and
9838c2ecf20Sopenharmony_ci * builds a buffer of SVGA3dVertexBuffer data.
9848c2ecf20Sopenharmony_ci * On output, @cbs->bind_cmd_count contains the number of bindings to be
9858c2ecf20Sopenharmony_ci * emitted, @cbs->bind_first_slot indicates the index of the first emitted
9868c2ecf20Sopenharmony_ci * binding, and @cbs->bind_cmd_buffer contains the command data.
9878c2ecf20Sopenharmony_ci */
9888c2ecf20Sopenharmony_cistatic void vmw_collect_dirty_vbs(struct vmw_ctx_binding_state *cbs,
9898c2ecf20Sopenharmony_ci				  const struct vmw_ctx_bindinfo *bi,
9908c2ecf20Sopenharmony_ci				  unsigned long *dirty,
9918c2ecf20Sopenharmony_ci				  u32 max_num)
9928c2ecf20Sopenharmony_ci{
9938c2ecf20Sopenharmony_ci	const struct vmw_ctx_bindinfo_vb *biv =
9948c2ecf20Sopenharmony_ci		container_of(bi, struct vmw_ctx_bindinfo_vb, bi);
9958c2ecf20Sopenharmony_ci	unsigned long i, next_bit;
9968c2ecf20Sopenharmony_ci	SVGA3dVertexBuffer *vbs = (SVGA3dVertexBuffer *) &cbs->bind_cmd_buffer;
9978c2ecf20Sopenharmony_ci
9988c2ecf20Sopenharmony_ci	cbs->bind_cmd_count = 0;
9998c2ecf20Sopenharmony_ci	i = find_first_bit(dirty, max_num);
10008c2ecf20Sopenharmony_ci	next_bit = i;
10018c2ecf20Sopenharmony_ci	cbs->bind_first_slot = i;
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_ci	biv += i;
10048c2ecf20Sopenharmony_ci	for (; i < max_num; ++i, ++biv, ++vbs) {
10058c2ecf20Sopenharmony_ci		if (!biv->bi.ctx || biv->bi.scrubbed) {
10068c2ecf20Sopenharmony_ci			vbs->sid = SVGA3D_INVALID_ID;
10078c2ecf20Sopenharmony_ci			vbs->stride = 0;
10088c2ecf20Sopenharmony_ci			vbs->offset = 0;
10098c2ecf20Sopenharmony_ci		} else {
10108c2ecf20Sopenharmony_ci			vbs->sid = biv->bi.res->id;
10118c2ecf20Sopenharmony_ci			vbs->stride = biv->stride;
10128c2ecf20Sopenharmony_ci			vbs->offset = biv->offset;
10138c2ecf20Sopenharmony_ci		}
10148c2ecf20Sopenharmony_ci		cbs->bind_cmd_count++;
10158c2ecf20Sopenharmony_ci		if (next_bit == i) {
10168c2ecf20Sopenharmony_ci			next_bit = find_next_bit(dirty, max_num, i + 1);
10178c2ecf20Sopenharmony_ci			if (next_bit >= max_num)
10188c2ecf20Sopenharmony_ci				break;
10198c2ecf20Sopenharmony_ci		}
10208c2ecf20Sopenharmony_ci	}
10218c2ecf20Sopenharmony_ci}
10228c2ecf20Sopenharmony_ci
10238c2ecf20Sopenharmony_ci/**
10248c2ecf20Sopenharmony_ci * vmw_binding_emit_set_vb - Issue delayed vertex buffer binding commands
10258c2ecf20Sopenharmony_ci *
10268c2ecf20Sopenharmony_ci * @cbs: Pointer to the context's struct vmw_ctx_binding_state
10278c2ecf20Sopenharmony_ci *
10288c2ecf20Sopenharmony_ci */
10298c2ecf20Sopenharmony_cistatic int vmw_emit_set_vb(struct vmw_ctx_binding_state *cbs)
10308c2ecf20Sopenharmony_ci{
10318c2ecf20Sopenharmony_ci	const struct vmw_ctx_bindinfo *loc =
10328c2ecf20Sopenharmony_ci		&cbs->vertex_buffers[0].bi;
10338c2ecf20Sopenharmony_ci	struct {
10348c2ecf20Sopenharmony_ci		SVGA3dCmdHeader header;
10358c2ecf20Sopenharmony_ci		SVGA3dCmdDXSetVertexBuffers body;
10368c2ecf20Sopenharmony_ci	} *cmd;
10378c2ecf20Sopenharmony_ci	size_t cmd_size, set_vb_size;
10388c2ecf20Sopenharmony_ci	const struct vmw_resource *ctx = vmw_cbs_context(cbs);
10398c2ecf20Sopenharmony_ci
10408c2ecf20Sopenharmony_ci	vmw_collect_dirty_vbs(cbs, loc, cbs->dirty_vb,
10418c2ecf20Sopenharmony_ci			     SVGA3D_DX_MAX_VERTEXBUFFERS);
10428c2ecf20Sopenharmony_ci	if (cbs->bind_cmd_count == 0)
10438c2ecf20Sopenharmony_ci		return 0;
10448c2ecf20Sopenharmony_ci
10458c2ecf20Sopenharmony_ci	set_vb_size = cbs->bind_cmd_count*sizeof(SVGA3dVertexBuffer);
10468c2ecf20Sopenharmony_ci	cmd_size = sizeof(*cmd) + set_vb_size;
10478c2ecf20Sopenharmony_ci	cmd = VMW_FIFO_RESERVE_DX(ctx->dev_priv, cmd_size, ctx->id);
10488c2ecf20Sopenharmony_ci	if (unlikely(cmd == NULL))
10498c2ecf20Sopenharmony_ci		return -ENOMEM;
10508c2ecf20Sopenharmony_ci
10518c2ecf20Sopenharmony_ci	cmd->header.id = SVGA_3D_CMD_DX_SET_VERTEX_BUFFERS;
10528c2ecf20Sopenharmony_ci	cmd->header.size = sizeof(cmd->body) + set_vb_size;
10538c2ecf20Sopenharmony_ci	cmd->body.startBuffer = cbs->bind_first_slot;
10548c2ecf20Sopenharmony_ci
10558c2ecf20Sopenharmony_ci	memcpy(&cmd[1], cbs->bind_cmd_buffer, set_vb_size);
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_ci	vmw_fifo_commit(ctx->dev_priv, cmd_size);
10588c2ecf20Sopenharmony_ci	bitmap_clear(cbs->dirty_vb,
10598c2ecf20Sopenharmony_ci		     cbs->bind_first_slot, cbs->bind_cmd_count);
10608c2ecf20Sopenharmony_ci
10618c2ecf20Sopenharmony_ci	return 0;
10628c2ecf20Sopenharmony_ci}
10638c2ecf20Sopenharmony_ci
10648c2ecf20Sopenharmony_cistatic int vmw_emit_set_uav(struct vmw_ctx_binding_state *cbs)
10658c2ecf20Sopenharmony_ci{
10668c2ecf20Sopenharmony_ci	const struct vmw_ctx_bindinfo *loc = &cbs->ua_views[0].views[0].bi;
10678c2ecf20Sopenharmony_ci	struct {
10688c2ecf20Sopenharmony_ci		SVGA3dCmdHeader header;
10698c2ecf20Sopenharmony_ci		SVGA3dCmdDXSetUAViews body;
10708c2ecf20Sopenharmony_ci	} *cmd;
10718c2ecf20Sopenharmony_ci	size_t cmd_size, view_id_size;
10728c2ecf20Sopenharmony_ci	const struct vmw_resource *ctx = vmw_cbs_context(cbs);
10738c2ecf20Sopenharmony_ci
10748c2ecf20Sopenharmony_ci	vmw_collect_view_ids(cbs, loc, SVGA3D_MAX_UAVIEWS);
10758c2ecf20Sopenharmony_ci	view_id_size = cbs->bind_cmd_count*sizeof(uint32);
10768c2ecf20Sopenharmony_ci	cmd_size = sizeof(*cmd) + view_id_size;
10778c2ecf20Sopenharmony_ci	cmd = VMW_FIFO_RESERVE_DX(ctx->dev_priv, cmd_size, ctx->id);
10788c2ecf20Sopenharmony_ci	if (!cmd)
10798c2ecf20Sopenharmony_ci		return -ENOMEM;
10808c2ecf20Sopenharmony_ci
10818c2ecf20Sopenharmony_ci	cmd->header.id = SVGA_3D_CMD_DX_SET_UA_VIEWS;
10828c2ecf20Sopenharmony_ci	cmd->header.size = sizeof(cmd->body) + view_id_size;
10838c2ecf20Sopenharmony_ci
10848c2ecf20Sopenharmony_ci	/* Splice index is specified user-space   */
10858c2ecf20Sopenharmony_ci	cmd->body.uavSpliceIndex = cbs->ua_views[0].index;
10868c2ecf20Sopenharmony_ci
10878c2ecf20Sopenharmony_ci	memcpy(&cmd[1], cbs->bind_cmd_buffer, view_id_size);
10888c2ecf20Sopenharmony_ci
10898c2ecf20Sopenharmony_ci	vmw_fifo_commit(ctx->dev_priv, cmd_size);
10908c2ecf20Sopenharmony_ci
10918c2ecf20Sopenharmony_ci	return 0;
10928c2ecf20Sopenharmony_ci}
10938c2ecf20Sopenharmony_ci
10948c2ecf20Sopenharmony_cistatic int vmw_emit_set_cs_uav(struct vmw_ctx_binding_state *cbs)
10958c2ecf20Sopenharmony_ci{
10968c2ecf20Sopenharmony_ci	const struct vmw_ctx_bindinfo *loc = &cbs->ua_views[1].views[0].bi;
10978c2ecf20Sopenharmony_ci	struct {
10988c2ecf20Sopenharmony_ci		SVGA3dCmdHeader header;
10998c2ecf20Sopenharmony_ci		SVGA3dCmdDXSetCSUAViews body;
11008c2ecf20Sopenharmony_ci	} *cmd;
11018c2ecf20Sopenharmony_ci	size_t cmd_size, view_id_size;
11028c2ecf20Sopenharmony_ci	const struct vmw_resource *ctx = vmw_cbs_context(cbs);
11038c2ecf20Sopenharmony_ci
11048c2ecf20Sopenharmony_ci	vmw_collect_view_ids(cbs, loc, SVGA3D_MAX_UAVIEWS);
11058c2ecf20Sopenharmony_ci	view_id_size = cbs->bind_cmd_count*sizeof(uint32);
11068c2ecf20Sopenharmony_ci	cmd_size = sizeof(*cmd) + view_id_size;
11078c2ecf20Sopenharmony_ci	cmd = VMW_FIFO_RESERVE_DX(ctx->dev_priv, cmd_size, ctx->id);
11088c2ecf20Sopenharmony_ci	if (!cmd)
11098c2ecf20Sopenharmony_ci		return -ENOMEM;
11108c2ecf20Sopenharmony_ci
11118c2ecf20Sopenharmony_ci	cmd->header.id = SVGA_3D_CMD_DX_SET_CS_UA_VIEWS;
11128c2ecf20Sopenharmony_ci	cmd->header.size = sizeof(cmd->body) + view_id_size;
11138c2ecf20Sopenharmony_ci
11148c2ecf20Sopenharmony_ci	/* Start index is specified user-space */
11158c2ecf20Sopenharmony_ci	cmd->body.startIndex = cbs->ua_views[1].index;
11168c2ecf20Sopenharmony_ci
11178c2ecf20Sopenharmony_ci	memcpy(&cmd[1], cbs->bind_cmd_buffer, view_id_size);
11188c2ecf20Sopenharmony_ci
11198c2ecf20Sopenharmony_ci	vmw_fifo_commit(ctx->dev_priv, cmd_size);
11208c2ecf20Sopenharmony_ci
11218c2ecf20Sopenharmony_ci	return 0;
11228c2ecf20Sopenharmony_ci}
11238c2ecf20Sopenharmony_ci
11248c2ecf20Sopenharmony_ci/**
11258c2ecf20Sopenharmony_ci * vmw_binding_emit_dirty - Issue delayed binding commands
11268c2ecf20Sopenharmony_ci *
11278c2ecf20Sopenharmony_ci * @cbs: Pointer to the context's struct vmw_ctx_binding_state
11288c2ecf20Sopenharmony_ci *
11298c2ecf20Sopenharmony_ci * This function issues the delayed binding commands that arise from
11308c2ecf20Sopenharmony_ci * previous scrub / unscrub calls. These binding commands are typically
11318c2ecf20Sopenharmony_ci * commands that batch a number of bindings and therefore it makes sense
11328c2ecf20Sopenharmony_ci * to delay them.
11338c2ecf20Sopenharmony_ci */
11348c2ecf20Sopenharmony_cistatic int vmw_binding_emit_dirty(struct vmw_ctx_binding_state *cbs)
11358c2ecf20Sopenharmony_ci{
11368c2ecf20Sopenharmony_ci	int ret = 0;
11378c2ecf20Sopenharmony_ci	unsigned long hit = 0;
11388c2ecf20Sopenharmony_ci
11398c2ecf20Sopenharmony_ci	while ((hit = find_next_bit(&cbs->dirty, VMW_BINDING_NUM_BITS, hit))
11408c2ecf20Sopenharmony_ci	      < VMW_BINDING_NUM_BITS) {
11418c2ecf20Sopenharmony_ci
11428c2ecf20Sopenharmony_ci		switch (hit) {
11438c2ecf20Sopenharmony_ci		case VMW_BINDING_RT_BIT:
11448c2ecf20Sopenharmony_ci			ret = vmw_emit_set_rt(cbs);
11458c2ecf20Sopenharmony_ci			break;
11468c2ecf20Sopenharmony_ci		case VMW_BINDING_PS_BIT:
11478c2ecf20Sopenharmony_ci			ret = vmw_binding_emit_dirty_ps(cbs);
11488c2ecf20Sopenharmony_ci			break;
11498c2ecf20Sopenharmony_ci		case VMW_BINDING_SO_T_BIT:
11508c2ecf20Sopenharmony_ci			ret = vmw_emit_set_so_target(cbs);
11518c2ecf20Sopenharmony_ci			break;
11528c2ecf20Sopenharmony_ci		case VMW_BINDING_VB_BIT:
11538c2ecf20Sopenharmony_ci			ret = vmw_emit_set_vb(cbs);
11548c2ecf20Sopenharmony_ci			break;
11558c2ecf20Sopenharmony_ci		case VMW_BINDING_UAV_BIT:
11568c2ecf20Sopenharmony_ci			ret = vmw_emit_set_uav(cbs);
11578c2ecf20Sopenharmony_ci			break;
11588c2ecf20Sopenharmony_ci		case VMW_BINDING_CS_UAV_BIT:
11598c2ecf20Sopenharmony_ci			ret = vmw_emit_set_cs_uav(cbs);
11608c2ecf20Sopenharmony_ci			break;
11618c2ecf20Sopenharmony_ci		default:
11628c2ecf20Sopenharmony_ci			BUG();
11638c2ecf20Sopenharmony_ci		}
11648c2ecf20Sopenharmony_ci		if (ret)
11658c2ecf20Sopenharmony_ci			return ret;
11668c2ecf20Sopenharmony_ci
11678c2ecf20Sopenharmony_ci		__clear_bit(hit, &cbs->dirty);
11688c2ecf20Sopenharmony_ci		hit++;
11698c2ecf20Sopenharmony_ci	}
11708c2ecf20Sopenharmony_ci
11718c2ecf20Sopenharmony_ci	return 0;
11728c2ecf20Sopenharmony_ci}
11738c2ecf20Sopenharmony_ci
11748c2ecf20Sopenharmony_ci/**
11758c2ecf20Sopenharmony_ci * vmw_binding_scrub_sr - Schedule a dx shaderresource binding
11768c2ecf20Sopenharmony_ci * scrub from a context
11778c2ecf20Sopenharmony_ci *
11788c2ecf20Sopenharmony_ci * @bi: single binding information.
11798c2ecf20Sopenharmony_ci * @rebind: Whether to issue a bind instead of scrub command.
11808c2ecf20Sopenharmony_ci */
11818c2ecf20Sopenharmony_cistatic int vmw_binding_scrub_sr(struct vmw_ctx_bindinfo *bi, bool rebind)
11828c2ecf20Sopenharmony_ci{
11838c2ecf20Sopenharmony_ci	struct vmw_ctx_bindinfo_view *biv =
11848c2ecf20Sopenharmony_ci		container_of(bi, struct vmw_ctx_bindinfo_view, bi);
11858c2ecf20Sopenharmony_ci	struct vmw_ctx_binding_state *cbs =
11868c2ecf20Sopenharmony_ci		vmw_context_binding_state(bi->ctx);
11878c2ecf20Sopenharmony_ci
11888c2ecf20Sopenharmony_ci	__set_bit(biv->slot, cbs->per_shader[biv->shader_slot].dirty_sr);
11898c2ecf20Sopenharmony_ci	__set_bit(VMW_BINDING_PS_SR_BIT,
11908c2ecf20Sopenharmony_ci		  &cbs->per_shader[biv->shader_slot].dirty);
11918c2ecf20Sopenharmony_ci	__set_bit(VMW_BINDING_PS_BIT, &cbs->dirty);
11928c2ecf20Sopenharmony_ci
11938c2ecf20Sopenharmony_ci	return 0;
11948c2ecf20Sopenharmony_ci}
11958c2ecf20Sopenharmony_ci
11968c2ecf20Sopenharmony_ci/**
11978c2ecf20Sopenharmony_ci * vmw_binding_scrub_dx_rt - Schedule a dx rendertarget binding
11988c2ecf20Sopenharmony_ci * scrub from a context
11998c2ecf20Sopenharmony_ci *
12008c2ecf20Sopenharmony_ci * @bi: single binding information.
12018c2ecf20Sopenharmony_ci * @rebind: Whether to issue a bind instead of scrub command.
12028c2ecf20Sopenharmony_ci */
12038c2ecf20Sopenharmony_cistatic int vmw_binding_scrub_dx_rt(struct vmw_ctx_bindinfo *bi, bool rebind)
12048c2ecf20Sopenharmony_ci{
12058c2ecf20Sopenharmony_ci	struct vmw_ctx_binding_state *cbs =
12068c2ecf20Sopenharmony_ci		vmw_context_binding_state(bi->ctx);
12078c2ecf20Sopenharmony_ci
12088c2ecf20Sopenharmony_ci	__set_bit(VMW_BINDING_RT_BIT, &cbs->dirty);
12098c2ecf20Sopenharmony_ci
12108c2ecf20Sopenharmony_ci	return 0;
12118c2ecf20Sopenharmony_ci}
12128c2ecf20Sopenharmony_ci
12138c2ecf20Sopenharmony_ci/**
12148c2ecf20Sopenharmony_ci * vmw_binding_scrub_so_target - Schedule a dx streamoutput buffer binding
12158c2ecf20Sopenharmony_ci * scrub from a context
12168c2ecf20Sopenharmony_ci *
12178c2ecf20Sopenharmony_ci * @bi: single binding information.
12188c2ecf20Sopenharmony_ci * @rebind: Whether to issue a bind instead of scrub command.
12198c2ecf20Sopenharmony_ci */
12208c2ecf20Sopenharmony_cistatic int vmw_binding_scrub_so_target(struct vmw_ctx_bindinfo *bi, bool rebind)
12218c2ecf20Sopenharmony_ci{
12228c2ecf20Sopenharmony_ci	struct vmw_ctx_binding_state *cbs =
12238c2ecf20Sopenharmony_ci		vmw_context_binding_state(bi->ctx);
12248c2ecf20Sopenharmony_ci
12258c2ecf20Sopenharmony_ci	__set_bit(VMW_BINDING_SO_T_BIT, &cbs->dirty);
12268c2ecf20Sopenharmony_ci
12278c2ecf20Sopenharmony_ci	return 0;
12288c2ecf20Sopenharmony_ci}
12298c2ecf20Sopenharmony_ci
12308c2ecf20Sopenharmony_ci/**
12318c2ecf20Sopenharmony_ci * vmw_binding_scrub_vb - Schedule a dx vertex buffer binding
12328c2ecf20Sopenharmony_ci * scrub from a context
12338c2ecf20Sopenharmony_ci *
12348c2ecf20Sopenharmony_ci * @bi: single binding information.
12358c2ecf20Sopenharmony_ci * @rebind: Whether to issue a bind instead of scrub command.
12368c2ecf20Sopenharmony_ci */
12378c2ecf20Sopenharmony_cistatic int vmw_binding_scrub_vb(struct vmw_ctx_bindinfo *bi, bool rebind)
12388c2ecf20Sopenharmony_ci{
12398c2ecf20Sopenharmony_ci	struct vmw_ctx_bindinfo_vb *bivb =
12408c2ecf20Sopenharmony_ci		container_of(bi, struct vmw_ctx_bindinfo_vb, bi);
12418c2ecf20Sopenharmony_ci	struct vmw_ctx_binding_state *cbs =
12428c2ecf20Sopenharmony_ci		vmw_context_binding_state(bi->ctx);
12438c2ecf20Sopenharmony_ci
12448c2ecf20Sopenharmony_ci	__set_bit(bivb->slot, cbs->dirty_vb);
12458c2ecf20Sopenharmony_ci	__set_bit(VMW_BINDING_VB_BIT, &cbs->dirty);
12468c2ecf20Sopenharmony_ci
12478c2ecf20Sopenharmony_ci	return 0;
12488c2ecf20Sopenharmony_ci}
12498c2ecf20Sopenharmony_ci
12508c2ecf20Sopenharmony_ci/**
12518c2ecf20Sopenharmony_ci * vmw_binding_scrub_ib - scrub a dx index buffer binding from a context
12528c2ecf20Sopenharmony_ci *
12538c2ecf20Sopenharmony_ci * @bi: single binding information.
12548c2ecf20Sopenharmony_ci * @rebind: Whether to issue a bind instead of scrub command.
12558c2ecf20Sopenharmony_ci */
12568c2ecf20Sopenharmony_cistatic int vmw_binding_scrub_ib(struct vmw_ctx_bindinfo *bi, bool rebind)
12578c2ecf20Sopenharmony_ci{
12588c2ecf20Sopenharmony_ci	struct vmw_ctx_bindinfo_ib *binding =
12598c2ecf20Sopenharmony_ci		container_of(bi, typeof(*binding), bi);
12608c2ecf20Sopenharmony_ci	struct vmw_private *dev_priv = bi->ctx->dev_priv;
12618c2ecf20Sopenharmony_ci	struct {
12628c2ecf20Sopenharmony_ci		SVGA3dCmdHeader header;
12638c2ecf20Sopenharmony_ci		SVGA3dCmdDXSetIndexBuffer body;
12648c2ecf20Sopenharmony_ci	} *cmd;
12658c2ecf20Sopenharmony_ci
12668c2ecf20Sopenharmony_ci	cmd = VMW_FIFO_RESERVE_DX(dev_priv, sizeof(*cmd), bi->ctx->id);
12678c2ecf20Sopenharmony_ci	if (unlikely(cmd == NULL))
12688c2ecf20Sopenharmony_ci		return -ENOMEM;
12698c2ecf20Sopenharmony_ci
12708c2ecf20Sopenharmony_ci	cmd->header.id = SVGA_3D_CMD_DX_SET_INDEX_BUFFER;
12718c2ecf20Sopenharmony_ci	cmd->header.size = sizeof(cmd->body);
12728c2ecf20Sopenharmony_ci	if (rebind) {
12738c2ecf20Sopenharmony_ci		cmd->body.sid = bi->res->id;
12748c2ecf20Sopenharmony_ci		cmd->body.format = binding->format;
12758c2ecf20Sopenharmony_ci		cmd->body.offset = binding->offset;
12768c2ecf20Sopenharmony_ci	} else {
12778c2ecf20Sopenharmony_ci		cmd->body.sid = SVGA3D_INVALID_ID;
12788c2ecf20Sopenharmony_ci		cmd->body.format = 0;
12798c2ecf20Sopenharmony_ci		cmd->body.offset = 0;
12808c2ecf20Sopenharmony_ci	}
12818c2ecf20Sopenharmony_ci
12828c2ecf20Sopenharmony_ci	vmw_fifo_commit(dev_priv, sizeof(*cmd));
12838c2ecf20Sopenharmony_ci
12848c2ecf20Sopenharmony_ci	return 0;
12858c2ecf20Sopenharmony_ci}
12868c2ecf20Sopenharmony_ci
12878c2ecf20Sopenharmony_cistatic int vmw_binding_scrub_uav(struct vmw_ctx_bindinfo *bi, bool rebind)
12888c2ecf20Sopenharmony_ci{
12898c2ecf20Sopenharmony_ci	struct vmw_ctx_binding_state *cbs = vmw_context_binding_state(bi->ctx);
12908c2ecf20Sopenharmony_ci
12918c2ecf20Sopenharmony_ci	__set_bit(VMW_BINDING_UAV_BIT, &cbs->dirty);
12928c2ecf20Sopenharmony_ci	return 0;
12938c2ecf20Sopenharmony_ci}
12948c2ecf20Sopenharmony_ci
12958c2ecf20Sopenharmony_cistatic int vmw_binding_scrub_cs_uav(struct vmw_ctx_bindinfo *bi, bool rebind)
12968c2ecf20Sopenharmony_ci{
12978c2ecf20Sopenharmony_ci	struct vmw_ctx_binding_state *cbs = vmw_context_binding_state(bi->ctx);
12988c2ecf20Sopenharmony_ci
12998c2ecf20Sopenharmony_ci	__set_bit(VMW_BINDING_CS_UAV_BIT, &cbs->dirty);
13008c2ecf20Sopenharmony_ci	return 0;
13018c2ecf20Sopenharmony_ci}
13028c2ecf20Sopenharmony_ci
13038c2ecf20Sopenharmony_ci/**
13048c2ecf20Sopenharmony_ci * vmw_binding_scrub_so - Scrub a streamoutput binding from context.
13058c2ecf20Sopenharmony_ci * @bi: Single binding information.
13068c2ecf20Sopenharmony_ci * @rebind: Whether to issue a bind instead of scrub command.
13078c2ecf20Sopenharmony_ci */
13088c2ecf20Sopenharmony_cistatic int vmw_binding_scrub_so(struct vmw_ctx_bindinfo *bi, bool rebind)
13098c2ecf20Sopenharmony_ci{
13108c2ecf20Sopenharmony_ci	struct vmw_ctx_bindinfo_so *binding =
13118c2ecf20Sopenharmony_ci		container_of(bi, typeof(*binding), bi);
13128c2ecf20Sopenharmony_ci	struct vmw_private *dev_priv = bi->ctx->dev_priv;
13138c2ecf20Sopenharmony_ci	struct {
13148c2ecf20Sopenharmony_ci		SVGA3dCmdHeader header;
13158c2ecf20Sopenharmony_ci		SVGA3dCmdDXSetStreamOutput body;
13168c2ecf20Sopenharmony_ci	} *cmd;
13178c2ecf20Sopenharmony_ci
13188c2ecf20Sopenharmony_ci	cmd = VMW_FIFO_RESERVE_DX(dev_priv, sizeof(*cmd), bi->ctx->id);
13198c2ecf20Sopenharmony_ci	if (!cmd)
13208c2ecf20Sopenharmony_ci		return -ENOMEM;
13218c2ecf20Sopenharmony_ci
13228c2ecf20Sopenharmony_ci	cmd->header.id = SVGA_3D_CMD_DX_SET_STREAMOUTPUT;
13238c2ecf20Sopenharmony_ci	cmd->header.size = sizeof(cmd->body);
13248c2ecf20Sopenharmony_ci	cmd->body.soid = rebind ? bi->res->id : SVGA3D_INVALID_ID;
13258c2ecf20Sopenharmony_ci	vmw_fifo_commit(dev_priv, sizeof(*cmd));
13268c2ecf20Sopenharmony_ci
13278c2ecf20Sopenharmony_ci	return 0;
13288c2ecf20Sopenharmony_ci}
13298c2ecf20Sopenharmony_ci
13308c2ecf20Sopenharmony_ci/**
13318c2ecf20Sopenharmony_ci * vmw_binding_state_alloc - Allocate a struct vmw_ctx_binding_state with
13328c2ecf20Sopenharmony_ci * memory accounting.
13338c2ecf20Sopenharmony_ci *
13348c2ecf20Sopenharmony_ci * @dev_priv: Pointer to a device private structure.
13358c2ecf20Sopenharmony_ci *
13368c2ecf20Sopenharmony_ci * Returns a pointer to a newly allocated struct or an error pointer on error.
13378c2ecf20Sopenharmony_ci */
13388c2ecf20Sopenharmony_cistruct vmw_ctx_binding_state *
13398c2ecf20Sopenharmony_civmw_binding_state_alloc(struct vmw_private *dev_priv)
13408c2ecf20Sopenharmony_ci{
13418c2ecf20Sopenharmony_ci	struct vmw_ctx_binding_state *cbs;
13428c2ecf20Sopenharmony_ci	struct ttm_operation_ctx ctx = {
13438c2ecf20Sopenharmony_ci		.interruptible = false,
13448c2ecf20Sopenharmony_ci		.no_wait_gpu = false
13458c2ecf20Sopenharmony_ci	};
13468c2ecf20Sopenharmony_ci	int ret;
13478c2ecf20Sopenharmony_ci
13488c2ecf20Sopenharmony_ci	ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv), sizeof(*cbs),
13498c2ecf20Sopenharmony_ci				&ctx);
13508c2ecf20Sopenharmony_ci	if (ret)
13518c2ecf20Sopenharmony_ci		return ERR_PTR(ret);
13528c2ecf20Sopenharmony_ci
13538c2ecf20Sopenharmony_ci	cbs = vzalloc(sizeof(*cbs));
13548c2ecf20Sopenharmony_ci	if (!cbs) {
13558c2ecf20Sopenharmony_ci		ttm_mem_global_free(vmw_mem_glob(dev_priv), sizeof(*cbs));
13568c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
13578c2ecf20Sopenharmony_ci	}
13588c2ecf20Sopenharmony_ci
13598c2ecf20Sopenharmony_ci	cbs->dev_priv = dev_priv;
13608c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&cbs->list);
13618c2ecf20Sopenharmony_ci
13628c2ecf20Sopenharmony_ci	return cbs;
13638c2ecf20Sopenharmony_ci}
13648c2ecf20Sopenharmony_ci
13658c2ecf20Sopenharmony_ci/**
13668c2ecf20Sopenharmony_ci * vmw_binding_state_free - Free a struct vmw_ctx_binding_state and its
13678c2ecf20Sopenharmony_ci * memory accounting info.
13688c2ecf20Sopenharmony_ci *
13698c2ecf20Sopenharmony_ci * @cbs: Pointer to the struct vmw_ctx_binding_state to be freed.
13708c2ecf20Sopenharmony_ci */
13718c2ecf20Sopenharmony_civoid vmw_binding_state_free(struct vmw_ctx_binding_state *cbs)
13728c2ecf20Sopenharmony_ci{
13738c2ecf20Sopenharmony_ci	struct vmw_private *dev_priv = cbs->dev_priv;
13748c2ecf20Sopenharmony_ci
13758c2ecf20Sopenharmony_ci	vfree(cbs);
13768c2ecf20Sopenharmony_ci	ttm_mem_global_free(vmw_mem_glob(dev_priv), sizeof(*cbs));
13778c2ecf20Sopenharmony_ci}
13788c2ecf20Sopenharmony_ci
13798c2ecf20Sopenharmony_ci/**
13808c2ecf20Sopenharmony_ci * vmw_binding_state_list - Get the binding list of a
13818c2ecf20Sopenharmony_ci * struct vmw_ctx_binding_state
13828c2ecf20Sopenharmony_ci *
13838c2ecf20Sopenharmony_ci * @cbs: Pointer to the struct vmw_ctx_binding_state
13848c2ecf20Sopenharmony_ci *
13858c2ecf20Sopenharmony_ci * Returns the binding list which can be used to traverse through the bindings
13868c2ecf20Sopenharmony_ci * and access the resource information of all bindings.
13878c2ecf20Sopenharmony_ci */
13888c2ecf20Sopenharmony_cistruct list_head *vmw_binding_state_list(struct vmw_ctx_binding_state *cbs)
13898c2ecf20Sopenharmony_ci{
13908c2ecf20Sopenharmony_ci	return &cbs->list;
13918c2ecf20Sopenharmony_ci}
13928c2ecf20Sopenharmony_ci
13938c2ecf20Sopenharmony_ci/**
13948c2ecf20Sopenharmony_ci * vmwgfx_binding_state_reset - clear a struct vmw_ctx_binding_state
13958c2ecf20Sopenharmony_ci *
13968c2ecf20Sopenharmony_ci * @cbs: Pointer to the struct vmw_ctx_binding_state to be cleared
13978c2ecf20Sopenharmony_ci *
13988c2ecf20Sopenharmony_ci * Drops all bindings registered in @cbs. No device binding actions are
13998c2ecf20Sopenharmony_ci * performed.
14008c2ecf20Sopenharmony_ci */
14018c2ecf20Sopenharmony_civoid vmw_binding_state_reset(struct vmw_ctx_binding_state *cbs)
14028c2ecf20Sopenharmony_ci{
14038c2ecf20Sopenharmony_ci	struct vmw_ctx_bindinfo *entry, *next;
14048c2ecf20Sopenharmony_ci
14058c2ecf20Sopenharmony_ci	list_for_each_entry_safe(entry, next, &cbs->list, ctx_list)
14068c2ecf20Sopenharmony_ci		vmw_binding_drop(entry);
14078c2ecf20Sopenharmony_ci}
14088c2ecf20Sopenharmony_ci
14098c2ecf20Sopenharmony_ci/**
14108c2ecf20Sopenharmony_ci * vmw_binding_dirtying - Return whether a binding type is dirtying its resource
14118c2ecf20Sopenharmony_ci * @binding_type: The binding type
14128c2ecf20Sopenharmony_ci *
14138c2ecf20Sopenharmony_ci * Each time a resource is put on the validation list as the result of a
14148c2ecf20Sopenharmony_ci * context binding referencing it, we need to determine whether that resource
14158c2ecf20Sopenharmony_ci * will be dirtied (written to by the GPU) as a result of the corresponding
14168c2ecf20Sopenharmony_ci * GPU operation. Currently rendertarget-, depth-stencil-, stream-output-target
14178c2ecf20Sopenharmony_ci * and unordered access view bindings are capable of dirtying its resource.
14188c2ecf20Sopenharmony_ci *
14198c2ecf20Sopenharmony_ci * Return: Whether the binding type dirties the resource its binding points to.
14208c2ecf20Sopenharmony_ci */
14218c2ecf20Sopenharmony_ciu32 vmw_binding_dirtying(enum vmw_ctx_binding_type binding_type)
14228c2ecf20Sopenharmony_ci{
14238c2ecf20Sopenharmony_ci	static u32 is_binding_dirtying[vmw_ctx_binding_max] = {
14248c2ecf20Sopenharmony_ci		[vmw_ctx_binding_rt] = VMW_RES_DIRTY_SET,
14258c2ecf20Sopenharmony_ci		[vmw_ctx_binding_dx_rt] = VMW_RES_DIRTY_SET,
14268c2ecf20Sopenharmony_ci		[vmw_ctx_binding_ds] = VMW_RES_DIRTY_SET,
14278c2ecf20Sopenharmony_ci		[vmw_ctx_binding_so_target] = VMW_RES_DIRTY_SET,
14288c2ecf20Sopenharmony_ci		[vmw_ctx_binding_uav] = VMW_RES_DIRTY_SET,
14298c2ecf20Sopenharmony_ci		[vmw_ctx_binding_cs_uav] = VMW_RES_DIRTY_SET,
14308c2ecf20Sopenharmony_ci	};
14318c2ecf20Sopenharmony_ci
14328c2ecf20Sopenharmony_ci	/* Review this function as new bindings are added. */
14338c2ecf20Sopenharmony_ci	BUILD_BUG_ON(vmw_ctx_binding_max != 14);
14348c2ecf20Sopenharmony_ci	return is_binding_dirtying[binding_type];
14358c2ecf20Sopenharmony_ci}
14368c2ecf20Sopenharmony_ci
14378c2ecf20Sopenharmony_ci/*
14388c2ecf20Sopenharmony_ci * This function is unused at run-time, and only used to hold various build
14398c2ecf20Sopenharmony_ci * asserts important for code optimization assumptions.
14408c2ecf20Sopenharmony_ci */
14418c2ecf20Sopenharmony_cistatic void vmw_binding_build_asserts(void)
14428c2ecf20Sopenharmony_ci{
14438c2ecf20Sopenharmony_ci	BUILD_BUG_ON(SVGA3D_NUM_SHADERTYPE_DX10 != 3);
14448c2ecf20Sopenharmony_ci	BUILD_BUG_ON(SVGA3D_MAX_SIMULTANEOUS_RENDER_TARGETS > SVGA3D_RT_MAX);
14458c2ecf20Sopenharmony_ci	BUILD_BUG_ON(sizeof(uint32) != sizeof(u32));
14468c2ecf20Sopenharmony_ci
14478c2ecf20Sopenharmony_ci	/*
14488c2ecf20Sopenharmony_ci	 * struct vmw_ctx_binding_state::bind_cmd_buffer is used for various
14498c2ecf20Sopenharmony_ci	 * view id arrays.
14508c2ecf20Sopenharmony_ci	 */
14518c2ecf20Sopenharmony_ci	BUILD_BUG_ON(VMW_MAX_VIEW_BINDINGS < SVGA3D_RT_MAX);
14528c2ecf20Sopenharmony_ci	BUILD_BUG_ON(VMW_MAX_VIEW_BINDINGS < SVGA3D_DX_MAX_SRVIEWS);
14538c2ecf20Sopenharmony_ci	BUILD_BUG_ON(VMW_MAX_VIEW_BINDINGS < SVGA3D_DX_MAX_CONSTBUFFERS);
14548c2ecf20Sopenharmony_ci
14558c2ecf20Sopenharmony_ci	/*
14568c2ecf20Sopenharmony_ci	 * struct vmw_ctx_binding_state::bind_cmd_buffer is used for
14578c2ecf20Sopenharmony_ci	 * u32 view ids, SVGA3dSoTargets and SVGA3dVertexBuffers
14588c2ecf20Sopenharmony_ci	 */
14598c2ecf20Sopenharmony_ci	BUILD_BUG_ON(SVGA3D_DX_MAX_SOTARGETS*sizeof(SVGA3dSoTarget) >
14608c2ecf20Sopenharmony_ci		     VMW_MAX_VIEW_BINDINGS*sizeof(u32));
14618c2ecf20Sopenharmony_ci	BUILD_BUG_ON(SVGA3D_DX_MAX_VERTEXBUFFERS*sizeof(SVGA3dVertexBuffer) >
14628c2ecf20Sopenharmony_ci		     VMW_MAX_VIEW_BINDINGS*sizeof(u32));
14638c2ecf20Sopenharmony_ci}
1464