vmwgfx_binding.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /**************************************************************************
  3. *
  4. * Copyright 2015 VMware, Inc., Palo Alto, CA., USA
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. /*
  28. * This file implements the vmwgfx context binding manager,
  29. * The sole reason for having to use this code is that vmware guest
  30. * backed contexts can be swapped out to their backing mobs by the device
  31. * at any time, also swapped in at any time. At swapin time, the device
  32. * validates the context bindings to make sure they point to valid resources.
  33. * It's this outside-of-drawcall validation (that can happen at any time),
  34. * that makes this code necessary.
  35. *
  36. * We therefore need to kill any context bindings pointing to a resource
  37. * when the resource is swapped out. Furthermore, if the vmwgfx driver has
  38. * swapped out the context we can't swap it in again to kill bindings because
  39. * of backing mob reservation lockdep violations, so as part of
  40. * context swapout, also kill all bindings of a context, so that they are
  41. * already killed if a resource to which a binding points
  42. * needs to be swapped out.
  43. *
  44. * Note that a resource can be pointed to by bindings from multiple contexts,
  45. * Therefore we can't easily protect this data by a per context mutex
  46. * (unless we use deadlock-safe WW mutexes). So we use a global binding_mutex
  47. * to protect all binding manager data.
  48. *
  49. * Finally, any association between a context and a global resource
  50. * (surface, shader or even DX query) is conceptually a context binding that
  51. * needs to be tracked by this code.
  52. */
  53. #include "vmwgfx_drv.h"
  54. #include "vmwgfx_binding.h"
  55. #include "device_include/svga3d_reg.h"
  56. #include <linux/vmalloc.h>
  57. #define VMW_BINDING_RT_BIT 0
  58. #define VMW_BINDING_PS_BIT 1
  59. #define VMW_BINDING_SO_T_BIT 2
  60. #define VMW_BINDING_VB_BIT 3
  61. #define VMW_BINDING_UAV_BIT 4
  62. #define VMW_BINDING_CS_UAV_BIT 5
  63. #define VMW_BINDING_NUM_BITS 6
  64. #define VMW_BINDING_PS_SR_BIT 0
  65. /**
  66. * struct vmw_ctx_binding_state - per context binding state
  67. *
  68. * @dev_priv: Pointer to device private structure.
  69. * @list: linked list of individual active bindings.
  70. * @render_targets: Render target bindings.
  71. * @texture_units: Texture units bindings.
  72. * @ds_view: Depth-stencil view binding.
  73. * @so_targets: StreamOutput target bindings.
  74. * @vertex_buffers: Vertex buffer bindings.
  75. * @index_buffer: Index buffer binding.
  76. * @per_shader: Per shader-type bindings.
  77. * @ua_views: UAV bindings.
  78. * @so_state: StreamOutput bindings.
  79. * @dirty: Bitmap tracking per binding-type changes that have not yet
  80. * been emitted to the device.
  81. * @dirty_vb: Bitmap tracking individual vertex buffer binding changes that
  82. * have not yet been emitted to the device.
  83. * @bind_cmd_buffer: Scratch space used to construct binding commands.
  84. * @bind_cmd_count: Number of binding command data entries in @bind_cmd_buffer
  85. * @bind_first_slot: Used together with @bind_cmd_buffer to indicate the
  86. * device binding slot of the first command data entry in @bind_cmd_buffer.
  87. *
  88. * Note that this structure also provides storage space for the individual
  89. * struct vmw_ctx_binding objects, so that no dynamic allocation is needed
  90. * for individual bindings.
  91. *
  92. */
  93. struct vmw_ctx_binding_state {
  94. struct vmw_private *dev_priv;
  95. struct list_head list;
  96. struct vmw_ctx_bindinfo_view render_targets[SVGA3D_RT_MAX];
  97. struct vmw_ctx_bindinfo_tex texture_units[SVGA3D_NUM_TEXTURE_UNITS];
  98. struct vmw_ctx_bindinfo_view ds_view;
  99. struct vmw_ctx_bindinfo_so_target so_targets[SVGA3D_DX_MAX_SOTARGETS];
  100. struct vmw_ctx_bindinfo_vb vertex_buffers[SVGA3D_DX_MAX_VERTEXBUFFERS];
  101. struct vmw_ctx_bindinfo_ib index_buffer;
  102. struct vmw_dx_shader_bindings per_shader[SVGA3D_NUM_SHADERTYPE];
  103. struct vmw_ctx_bindinfo_uav ua_views[VMW_MAX_UAV_BIND_TYPE];
  104. struct vmw_ctx_bindinfo_so so_state;
  105. unsigned long dirty;
  106. DECLARE_BITMAP(dirty_vb, SVGA3D_DX_MAX_VERTEXBUFFERS);
  107. u32 bind_cmd_buffer[VMW_MAX_VIEW_BINDINGS];
  108. u32 bind_cmd_count;
  109. u32 bind_first_slot;
  110. };
  111. static int vmw_binding_scrub_shader(struct vmw_ctx_bindinfo *bi, bool rebind);
  112. static int vmw_binding_scrub_render_target(struct vmw_ctx_bindinfo *bi,
  113. bool rebind);
  114. static int vmw_binding_scrub_texture(struct vmw_ctx_bindinfo *bi, bool rebind);
  115. static int vmw_binding_scrub_cb(struct vmw_ctx_bindinfo *bi, bool rebind);
  116. static int vmw_binding_scrub_dx_rt(struct vmw_ctx_bindinfo *bi, bool rebind);
  117. static int vmw_binding_scrub_sr(struct vmw_ctx_bindinfo *bi, bool rebind);
  118. static int vmw_binding_scrub_so_target(struct vmw_ctx_bindinfo *bi, bool rebind);
  119. static int vmw_binding_emit_dirty(struct vmw_ctx_binding_state *cbs);
  120. static int vmw_binding_scrub_dx_shader(struct vmw_ctx_bindinfo *bi,
  121. bool rebind);
  122. static int vmw_binding_scrub_ib(struct vmw_ctx_bindinfo *bi, bool rebind);
  123. static int vmw_binding_scrub_vb(struct vmw_ctx_bindinfo *bi, bool rebind);
  124. static int vmw_binding_scrub_uav(struct vmw_ctx_bindinfo *bi, bool rebind);
  125. static int vmw_binding_scrub_cs_uav(struct vmw_ctx_bindinfo *bi, bool rebind);
  126. static int vmw_binding_scrub_so(struct vmw_ctx_bindinfo *bi, bool rebind);
  127. static void vmw_binding_build_asserts(void) __attribute__ ((unused));
  128. typedef int (*vmw_scrub_func)(struct vmw_ctx_bindinfo *, bool);
  129. /**
  130. * struct vmw_binding_info - Per binding type information for the binding
  131. * manager
  132. *
  133. * @size: The size of the struct binding derived from a struct vmw_ctx_bindinfo.
  134. * @offsets: array[shader_slot] of offsets to the array[slot]
  135. * of struct bindings for the binding type.
  136. * @scrub_func: Pointer to the scrub function for this binding type.
  137. *
  138. * Holds static information to help optimize the binding manager and avoid
  139. * an excessive amount of switch statements.
  140. */
  141. struct vmw_binding_info {
  142. size_t size;
  143. const size_t *offsets;
  144. vmw_scrub_func scrub_func;
  145. };
  146. /*
  147. * A number of static variables that help determine the scrub func and the
  148. * location of the struct vmw_ctx_bindinfo slots for each binding type.
  149. */
  150. static const size_t vmw_binding_shader_offsets[] = {
  151. offsetof(struct vmw_ctx_binding_state, per_shader[0].shader),
  152. offsetof(struct vmw_ctx_binding_state, per_shader[1].shader),
  153. offsetof(struct vmw_ctx_binding_state, per_shader[2].shader),
  154. offsetof(struct vmw_ctx_binding_state, per_shader[3].shader),
  155. offsetof(struct vmw_ctx_binding_state, per_shader[4].shader),
  156. offsetof(struct vmw_ctx_binding_state, per_shader[5].shader),
  157. };
  158. static const size_t vmw_binding_rt_offsets[] = {
  159. offsetof(struct vmw_ctx_binding_state, render_targets),
  160. };
  161. static const size_t vmw_binding_tex_offsets[] = {
  162. offsetof(struct vmw_ctx_binding_state, texture_units),
  163. };
  164. static const size_t vmw_binding_cb_offsets[] = {
  165. offsetof(struct vmw_ctx_binding_state, per_shader[0].const_buffers),
  166. offsetof(struct vmw_ctx_binding_state, per_shader[1].const_buffers),
  167. offsetof(struct vmw_ctx_binding_state, per_shader[2].const_buffers),
  168. offsetof(struct vmw_ctx_binding_state, per_shader[3].const_buffers),
  169. offsetof(struct vmw_ctx_binding_state, per_shader[4].const_buffers),
  170. offsetof(struct vmw_ctx_binding_state, per_shader[5].const_buffers),
  171. };
  172. static const size_t vmw_binding_dx_ds_offsets[] = {
  173. offsetof(struct vmw_ctx_binding_state, ds_view),
  174. };
  175. static const size_t vmw_binding_sr_offsets[] = {
  176. offsetof(struct vmw_ctx_binding_state, per_shader[0].shader_res),
  177. offsetof(struct vmw_ctx_binding_state, per_shader[1].shader_res),
  178. offsetof(struct vmw_ctx_binding_state, per_shader[2].shader_res),
  179. offsetof(struct vmw_ctx_binding_state, per_shader[3].shader_res),
  180. offsetof(struct vmw_ctx_binding_state, per_shader[4].shader_res),
  181. offsetof(struct vmw_ctx_binding_state, per_shader[5].shader_res),
  182. };
  183. static const size_t vmw_binding_so_target_offsets[] = {
  184. offsetof(struct vmw_ctx_binding_state, so_targets),
  185. };
  186. static const size_t vmw_binding_vb_offsets[] = {
  187. offsetof(struct vmw_ctx_binding_state, vertex_buffers),
  188. };
  189. static const size_t vmw_binding_ib_offsets[] = {
  190. offsetof(struct vmw_ctx_binding_state, index_buffer),
  191. };
  192. static const size_t vmw_binding_uav_offsets[] = {
  193. offsetof(struct vmw_ctx_binding_state, ua_views[0].views),
  194. };
  195. static const size_t vmw_binding_cs_uav_offsets[] = {
  196. offsetof(struct vmw_ctx_binding_state, ua_views[1].views),
  197. };
  198. static const size_t vmw_binding_so_offsets[] = {
  199. offsetof(struct vmw_ctx_binding_state, so_state),
  200. };
  201. static const struct vmw_binding_info vmw_binding_infos[] = {
  202. [vmw_ctx_binding_shader] = {
  203. .size = sizeof(struct vmw_ctx_bindinfo_shader),
  204. .offsets = vmw_binding_shader_offsets,
  205. .scrub_func = vmw_binding_scrub_shader},
  206. [vmw_ctx_binding_rt] = {
  207. .size = sizeof(struct vmw_ctx_bindinfo_view),
  208. .offsets = vmw_binding_rt_offsets,
  209. .scrub_func = vmw_binding_scrub_render_target},
  210. [vmw_ctx_binding_tex] = {
  211. .size = sizeof(struct vmw_ctx_bindinfo_tex),
  212. .offsets = vmw_binding_tex_offsets,
  213. .scrub_func = vmw_binding_scrub_texture},
  214. [vmw_ctx_binding_cb] = {
  215. .size = sizeof(struct vmw_ctx_bindinfo_cb),
  216. .offsets = vmw_binding_cb_offsets,
  217. .scrub_func = vmw_binding_scrub_cb},
  218. [vmw_ctx_binding_dx_shader] = {
  219. .size = sizeof(struct vmw_ctx_bindinfo_shader),
  220. .offsets = vmw_binding_shader_offsets,
  221. .scrub_func = vmw_binding_scrub_dx_shader},
  222. [vmw_ctx_binding_dx_rt] = {
  223. .size = sizeof(struct vmw_ctx_bindinfo_view),
  224. .offsets = vmw_binding_rt_offsets,
  225. .scrub_func = vmw_binding_scrub_dx_rt},
  226. [vmw_ctx_binding_sr] = {
  227. .size = sizeof(struct vmw_ctx_bindinfo_view),
  228. .offsets = vmw_binding_sr_offsets,
  229. .scrub_func = vmw_binding_scrub_sr},
  230. [vmw_ctx_binding_ds] = {
  231. .size = sizeof(struct vmw_ctx_bindinfo_view),
  232. .offsets = vmw_binding_dx_ds_offsets,
  233. .scrub_func = vmw_binding_scrub_dx_rt},
  234. [vmw_ctx_binding_so_target] = {
  235. .size = sizeof(struct vmw_ctx_bindinfo_so_target),
  236. .offsets = vmw_binding_so_target_offsets,
  237. .scrub_func = vmw_binding_scrub_so_target},
  238. [vmw_ctx_binding_vb] = {
  239. .size = sizeof(struct vmw_ctx_bindinfo_vb),
  240. .offsets = vmw_binding_vb_offsets,
  241. .scrub_func = vmw_binding_scrub_vb},
  242. [vmw_ctx_binding_ib] = {
  243. .size = sizeof(struct vmw_ctx_bindinfo_ib),
  244. .offsets = vmw_binding_ib_offsets,
  245. .scrub_func = vmw_binding_scrub_ib},
  246. [vmw_ctx_binding_uav] = {
  247. .size = sizeof(struct vmw_ctx_bindinfo_view),
  248. .offsets = vmw_binding_uav_offsets,
  249. .scrub_func = vmw_binding_scrub_uav},
  250. [vmw_ctx_binding_cs_uav] = {
  251. .size = sizeof(struct vmw_ctx_bindinfo_view),
  252. .offsets = vmw_binding_cs_uav_offsets,
  253. .scrub_func = vmw_binding_scrub_cs_uav},
  254. [vmw_ctx_binding_so] = {
  255. .size = sizeof(struct vmw_ctx_bindinfo_so),
  256. .offsets = vmw_binding_so_offsets,
  257. .scrub_func = vmw_binding_scrub_so},
  258. };
  259. /**
  260. * vmw_cbs_context - Return a pointer to the context resource of a
  261. * context binding state tracker.
  262. *
  263. * @cbs: The context binding state tracker.
  264. *
  265. * Provided there are any active bindings, this function will return an
  266. * unreferenced pointer to the context resource that owns the context
  267. * binding state tracker. If there are no active bindings, this function
  268. * will return NULL. Note that the caller must somehow ensure that a reference
  269. * is held on the context resource prior to calling this function.
  270. */
  271. static const struct vmw_resource *
  272. vmw_cbs_context(const struct vmw_ctx_binding_state *cbs)
  273. {
  274. if (list_empty(&cbs->list))
  275. return NULL;
  276. return list_first_entry(&cbs->list, struct vmw_ctx_bindinfo,
  277. ctx_list)->ctx;
  278. }
  279. /**
  280. * vmw_binding_loc - determine the struct vmw_ctx_bindinfo slot location.
  281. *
  282. * @cbs: Pointer to a struct vmw_ctx_binding state which holds the slot.
  283. * @bt: The binding type.
  284. * @shader_slot: The shader slot of the binding. If none, then set to 0.
  285. * @slot: The slot of the binding.
  286. */
  287. static struct vmw_ctx_bindinfo *
  288. vmw_binding_loc(struct vmw_ctx_binding_state *cbs,
  289. enum vmw_ctx_binding_type bt, u32 shader_slot, u32 slot)
  290. {
  291. const struct vmw_binding_info *b = &vmw_binding_infos[bt];
  292. size_t offset = b->offsets[shader_slot] + b->size*slot;
  293. return (struct vmw_ctx_bindinfo *)((u8 *) cbs + offset);
  294. }
  295. /**
  296. * vmw_binding_drop: Stop tracking a context binding
  297. *
  298. * @bi: Pointer to binding tracker storage.
  299. *
  300. * Stops tracking a context binding, and re-initializes its storage.
  301. * Typically used when the context binding is replaced with a binding to
  302. * another (or the same, for that matter) resource.
  303. */
  304. static void vmw_binding_drop(struct vmw_ctx_bindinfo *bi)
  305. {
  306. list_del(&bi->ctx_list);
  307. if (!list_empty(&bi->res_list))
  308. list_del(&bi->res_list);
  309. bi->ctx = NULL;
  310. }
  311. /**
  312. * vmw_binding_add: Start tracking a context binding
  313. *
  314. * @cbs: Pointer to the context binding state tracker.
  315. * @bi: Information about the binding to track.
  316. * @shader_slot: The shader slot of the binding.
  317. * @slot: The slot of the binding.
  318. *
  319. * Starts tracking the binding in the context binding
  320. * state structure @cbs.
  321. */
  322. void vmw_binding_add(struct vmw_ctx_binding_state *cbs,
  323. const struct vmw_ctx_bindinfo *bi,
  324. u32 shader_slot, u32 slot)
  325. {
  326. struct vmw_ctx_bindinfo *loc =
  327. vmw_binding_loc(cbs, bi->bt, shader_slot, slot);
  328. const struct vmw_binding_info *b = &vmw_binding_infos[bi->bt];
  329. if (loc->ctx != NULL)
  330. vmw_binding_drop(loc);
  331. memcpy(loc, bi, b->size);
  332. loc->scrubbed = false;
  333. list_add(&loc->ctx_list, &cbs->list);
  334. INIT_LIST_HEAD(&loc->res_list);
  335. }
  336. /**
  337. * vmw_binding_cb_offset_update: Update the offset of a cb binding
  338. *
  339. * @cbs: Pointer to the context binding state tracker.
  340. * @shader_slot: The shader slot of the binding.
  341. * @slot: The slot of the binding.
  342. * @offsetInBytes: The new offset of the binding.
  343. *
  344. * Updates the offset of an existing cb binding in the context binding
  345. * state structure @cbs.
  346. */
  347. void vmw_binding_cb_offset_update(struct vmw_ctx_binding_state *cbs,
  348. u32 shader_slot, u32 slot, u32 offsetInBytes)
  349. {
  350. struct vmw_ctx_bindinfo *loc =
  351. vmw_binding_loc(cbs, vmw_ctx_binding_cb, shader_slot, slot);
  352. struct vmw_ctx_bindinfo_cb *loc_cb =
  353. (struct vmw_ctx_bindinfo_cb *)((u8 *) loc);
  354. loc_cb->offset = offsetInBytes;
  355. }
  356. /**
  357. * vmw_binding_add_uav_index - Add UAV index for tracking.
  358. * @cbs: Pointer to the context binding state tracker.
  359. * @slot: UAV type to which bind this index.
  360. * @index: The splice index to track.
  361. */
  362. void vmw_binding_add_uav_index(struct vmw_ctx_binding_state *cbs, uint32 slot,
  363. uint32 index)
  364. {
  365. cbs->ua_views[slot].index = index;
  366. }
  367. /**
  368. * vmw_binding_transfer: Transfer a context binding tracking entry.
  369. *
  370. * @cbs: Pointer to the persistent context binding state tracker.
  371. * @from: Staged binding info built during execbuf
  372. * @bi: Information about the binding to track.
  373. *
  374. */
  375. static void vmw_binding_transfer(struct vmw_ctx_binding_state *cbs,
  376. const struct vmw_ctx_binding_state *from,
  377. const struct vmw_ctx_bindinfo *bi)
  378. {
  379. size_t offset = (unsigned long)bi - (unsigned long)from;
  380. struct vmw_ctx_bindinfo *loc = (struct vmw_ctx_bindinfo *)
  381. ((unsigned long) cbs + offset);
  382. if (loc->ctx != NULL) {
  383. WARN_ON(bi->scrubbed);
  384. vmw_binding_drop(loc);
  385. }
  386. if (bi->res != NULL) {
  387. memcpy(loc, bi, vmw_binding_infos[bi->bt].size);
  388. list_add_tail(&loc->ctx_list, &cbs->list);
  389. list_add_tail(&loc->res_list, &loc->res->binding_head);
  390. }
  391. }
  392. /**
  393. * vmw_binding_state_kill - Kill all bindings associated with a
  394. * struct vmw_ctx_binding state structure, and re-initialize the structure.
  395. *
  396. * @cbs: Pointer to the context binding state tracker.
  397. *
  398. * Emits commands to scrub all bindings associated with the
  399. * context binding state tracker. Then re-initializes the whole structure.
  400. */
  401. void vmw_binding_state_kill(struct vmw_ctx_binding_state *cbs)
  402. {
  403. struct vmw_ctx_bindinfo *entry, *next;
  404. vmw_binding_state_scrub(cbs);
  405. list_for_each_entry_safe(entry, next, &cbs->list, ctx_list)
  406. vmw_binding_drop(entry);
  407. }
  408. /**
  409. * vmw_binding_state_scrub - Scrub all bindings associated with a
  410. * struct vmw_ctx_binding state structure.
  411. *
  412. * @cbs: Pointer to the context binding state tracker.
  413. *
  414. * Emits commands to scrub all bindings associated with the
  415. * context binding state tracker.
  416. */
  417. void vmw_binding_state_scrub(struct vmw_ctx_binding_state *cbs)
  418. {
  419. struct vmw_ctx_bindinfo *entry;
  420. list_for_each_entry(entry, &cbs->list, ctx_list) {
  421. if (!entry->scrubbed) {
  422. (void) vmw_binding_infos[entry->bt].scrub_func
  423. (entry, false);
  424. entry->scrubbed = true;
  425. }
  426. }
  427. (void) vmw_binding_emit_dirty(cbs);
  428. }
  429. /**
  430. * vmw_binding_res_list_kill - Kill all bindings on a
  431. * resource binding list
  432. *
  433. * @head: list head of resource binding list
  434. *
  435. * Kills all bindings associated with a specific resource. Typically
  436. * called before the resource is destroyed.
  437. */
  438. void vmw_binding_res_list_kill(struct list_head *head)
  439. {
  440. struct vmw_ctx_bindinfo *entry, *next;
  441. vmw_binding_res_list_scrub(head);
  442. list_for_each_entry_safe(entry, next, head, res_list)
  443. vmw_binding_drop(entry);
  444. }
  445. /**
  446. * vmw_binding_res_list_scrub - Scrub all bindings on a
  447. * resource binding list
  448. *
  449. * @head: list head of resource binding list
  450. *
  451. * Scrub all bindings associated with a specific resource. Typically
  452. * called before the resource is evicted.
  453. */
  454. void vmw_binding_res_list_scrub(struct list_head *head)
  455. {
  456. struct vmw_ctx_bindinfo *entry;
  457. list_for_each_entry(entry, head, res_list) {
  458. if (!entry->scrubbed) {
  459. (void) vmw_binding_infos[entry->bt].scrub_func
  460. (entry, false);
  461. entry->scrubbed = true;
  462. }
  463. }
  464. list_for_each_entry(entry, head, res_list) {
  465. struct vmw_ctx_binding_state *cbs =
  466. vmw_context_binding_state(entry->ctx);
  467. (void) vmw_binding_emit_dirty(cbs);
  468. }
  469. }
  470. /**
  471. * vmw_binding_state_commit - Commit staged binding info
  472. *
  473. * @to: Staged binding info area to copy into to.
  474. * @from: Staged binding info built during execbuf.
  475. *
  476. * Transfers binding info from a temporary structure
  477. * (typically used by execbuf) to the persistent
  478. * structure in the context. This can be done once commands have been
  479. * submitted to hardware
  480. */
  481. void vmw_binding_state_commit(struct vmw_ctx_binding_state *to,
  482. struct vmw_ctx_binding_state *from)
  483. {
  484. struct vmw_ctx_bindinfo *entry, *next;
  485. list_for_each_entry_safe(entry, next, &from->list, ctx_list) {
  486. vmw_binding_transfer(to, from, entry);
  487. vmw_binding_drop(entry);
  488. }
  489. /* Also transfer uav splice indices */
  490. to->ua_views[0].index = from->ua_views[0].index;
  491. to->ua_views[1].index = from->ua_views[1].index;
  492. }
  493. /**
  494. * vmw_binding_rebind_all - Rebind all scrubbed bindings of a context
  495. *
  496. * @cbs: Pointer to the context binding state tracker.
  497. *
  498. * Walks through the context binding list and rebinds all scrubbed
  499. * resources.
  500. */
  501. int vmw_binding_rebind_all(struct vmw_ctx_binding_state *cbs)
  502. {
  503. struct vmw_ctx_bindinfo *entry;
  504. int ret;
  505. list_for_each_entry(entry, &cbs->list, ctx_list) {
  506. if (likely(!entry->scrubbed))
  507. continue;
  508. if ((entry->res == NULL || entry->res->id ==
  509. SVGA3D_INVALID_ID))
  510. continue;
  511. ret = vmw_binding_infos[entry->bt].scrub_func(entry, true);
  512. if (unlikely(ret != 0))
  513. return ret;
  514. entry->scrubbed = false;
  515. }
  516. return vmw_binding_emit_dirty(cbs);
  517. }
  518. /**
  519. * vmw_binding_scrub_shader - scrub a shader binding from a context.
  520. *
  521. * @bi: single binding information.
  522. * @rebind: Whether to issue a bind instead of scrub command.
  523. */
  524. static int vmw_binding_scrub_shader(struct vmw_ctx_bindinfo *bi, bool rebind)
  525. {
  526. struct vmw_ctx_bindinfo_shader *binding =
  527. container_of(bi, typeof(*binding), bi);
  528. struct vmw_private *dev_priv = bi->ctx->dev_priv;
  529. struct {
  530. SVGA3dCmdHeader header;
  531. SVGA3dCmdSetShader body;
  532. } *cmd;
  533. cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
  534. if (unlikely(cmd == NULL))
  535. return -ENOMEM;
  536. cmd->header.id = SVGA_3D_CMD_SET_SHADER;
  537. cmd->header.size = sizeof(cmd->body);
  538. cmd->body.cid = bi->ctx->id;
  539. cmd->body.type = binding->shader_slot + SVGA3D_SHADERTYPE_MIN;
  540. cmd->body.shid = ((rebind) ? bi->res->id : SVGA3D_INVALID_ID);
  541. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  542. return 0;
  543. }
  544. /**
  545. * vmw_binding_scrub_render_target - scrub a render target binding
  546. * from a context.
  547. *
  548. * @bi: single binding information.
  549. * @rebind: Whether to issue a bind instead of scrub command.
  550. */
  551. static int vmw_binding_scrub_render_target(struct vmw_ctx_bindinfo *bi,
  552. bool rebind)
  553. {
  554. struct vmw_ctx_bindinfo_view *binding =
  555. container_of(bi, typeof(*binding), bi);
  556. struct vmw_private *dev_priv = bi->ctx->dev_priv;
  557. struct {
  558. SVGA3dCmdHeader header;
  559. SVGA3dCmdSetRenderTarget body;
  560. } *cmd;
  561. cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
  562. if (unlikely(cmd == NULL))
  563. return -ENOMEM;
  564. cmd->header.id = SVGA_3D_CMD_SETRENDERTARGET;
  565. cmd->header.size = sizeof(cmd->body);
  566. cmd->body.cid = bi->ctx->id;
  567. cmd->body.type = binding->slot;
  568. cmd->body.target.sid = ((rebind) ? bi->res->id : SVGA3D_INVALID_ID);
  569. cmd->body.target.face = 0;
  570. cmd->body.target.mipmap = 0;
  571. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  572. return 0;
  573. }
  574. /**
  575. * vmw_binding_scrub_texture - scrub a texture binding from a context.
  576. *
  577. * @bi: single binding information.
  578. * @rebind: Whether to issue a bind instead of scrub command.
  579. *
  580. * TODO: Possibly complement this function with a function that takes
  581. * a list of texture bindings and combines them to a single command.
  582. */
  583. static int vmw_binding_scrub_texture(struct vmw_ctx_bindinfo *bi,
  584. bool rebind)
  585. {
  586. struct vmw_ctx_bindinfo_tex *binding =
  587. container_of(bi, typeof(*binding), bi);
  588. struct vmw_private *dev_priv = bi->ctx->dev_priv;
  589. struct {
  590. SVGA3dCmdHeader header;
  591. struct {
  592. SVGA3dCmdSetTextureState c;
  593. SVGA3dTextureState s1;
  594. } body;
  595. } *cmd;
  596. cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
  597. if (unlikely(cmd == NULL))
  598. return -ENOMEM;
  599. cmd->header.id = SVGA_3D_CMD_SETTEXTURESTATE;
  600. cmd->header.size = sizeof(cmd->body);
  601. cmd->body.c.cid = bi->ctx->id;
  602. cmd->body.s1.stage = binding->texture_stage;
  603. cmd->body.s1.name = SVGA3D_TS_BIND_TEXTURE;
  604. cmd->body.s1.value = ((rebind) ? bi->res->id : SVGA3D_INVALID_ID);
  605. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  606. return 0;
  607. }
  608. /**
  609. * vmw_binding_scrub_dx_shader - scrub a dx shader binding from a context.
  610. *
  611. * @bi: single binding information.
  612. * @rebind: Whether to issue a bind instead of scrub command.
  613. */
  614. static int vmw_binding_scrub_dx_shader(struct vmw_ctx_bindinfo *bi, bool rebind)
  615. {
  616. struct vmw_ctx_bindinfo_shader *binding =
  617. container_of(bi, typeof(*binding), bi);
  618. struct vmw_private *dev_priv = bi->ctx->dev_priv;
  619. struct {
  620. SVGA3dCmdHeader header;
  621. SVGA3dCmdDXSetShader body;
  622. } *cmd;
  623. cmd = VMW_CMD_CTX_RESERVE(dev_priv, sizeof(*cmd), bi->ctx->id);
  624. if (unlikely(cmd == NULL))
  625. return -ENOMEM;
  626. cmd->header.id = SVGA_3D_CMD_DX_SET_SHADER;
  627. cmd->header.size = sizeof(cmd->body);
  628. cmd->body.type = binding->shader_slot + SVGA3D_SHADERTYPE_MIN;
  629. cmd->body.shaderId = ((rebind) ? bi->res->id : SVGA3D_INVALID_ID);
  630. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  631. return 0;
  632. }
  633. /**
  634. * vmw_binding_scrub_cb - scrub a constant buffer binding from a context.
  635. *
  636. * @bi: single binding information.
  637. * @rebind: Whether to issue a bind instead of scrub command.
  638. */
  639. static int vmw_binding_scrub_cb(struct vmw_ctx_bindinfo *bi, bool rebind)
  640. {
  641. struct vmw_ctx_bindinfo_cb *binding =
  642. container_of(bi, typeof(*binding), bi);
  643. struct vmw_private *dev_priv = bi->ctx->dev_priv;
  644. struct {
  645. SVGA3dCmdHeader header;
  646. SVGA3dCmdDXSetSingleConstantBuffer body;
  647. } *cmd;
  648. cmd = VMW_CMD_CTX_RESERVE(dev_priv, sizeof(*cmd), bi->ctx->id);
  649. if (unlikely(cmd == NULL))
  650. return -ENOMEM;
  651. cmd->header.id = SVGA_3D_CMD_DX_SET_SINGLE_CONSTANT_BUFFER;
  652. cmd->header.size = sizeof(cmd->body);
  653. cmd->body.slot = binding->slot;
  654. cmd->body.type = binding->shader_slot + SVGA3D_SHADERTYPE_MIN;
  655. if (rebind) {
  656. cmd->body.offsetInBytes = binding->offset;
  657. cmd->body.sizeInBytes = binding->size;
  658. cmd->body.sid = bi->res->id;
  659. } else {
  660. cmd->body.offsetInBytes = 0;
  661. cmd->body.sizeInBytes = 0;
  662. cmd->body.sid = SVGA3D_INVALID_ID;
  663. }
  664. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  665. return 0;
  666. }
  667. /**
  668. * vmw_collect_view_ids - Build view id data for a view binding command
  669. * without checking which bindings actually need to be emitted
  670. *
  671. * @cbs: Pointer to the context's struct vmw_ctx_binding_state
  672. * @biv: Pointer to where the binding info array is stored in @cbs
  673. * @max_num: Maximum number of entries in the @bi array.
  674. *
  675. * Scans the @bi array for bindings and builds a buffer of view id data.
  676. * Stops at the first non-existing binding in the @bi array.
  677. * On output, @cbs->bind_cmd_count contains the number of bindings to be
  678. * emitted, @cbs->bind_first_slot is set to zero, and @cbs->bind_cmd_buffer
  679. * contains the command data.
  680. */
  681. static void vmw_collect_view_ids(struct vmw_ctx_binding_state *cbs,
  682. const struct vmw_ctx_bindinfo_view *biv,
  683. u32 max_num)
  684. {
  685. unsigned long i;
  686. cbs->bind_cmd_count = 0;
  687. cbs->bind_first_slot = 0;
  688. for (i = 0; i < max_num; ++i, ++biv) {
  689. if (!biv->bi.ctx)
  690. break;
  691. cbs->bind_cmd_buffer[cbs->bind_cmd_count++] =
  692. ((biv->bi.scrubbed) ?
  693. SVGA3D_INVALID_ID : biv->bi.res->id);
  694. }
  695. }
  696. /**
  697. * vmw_collect_dirty_view_ids - Build view id data for a view binding command
  698. *
  699. * @cbs: Pointer to the context's struct vmw_ctx_binding_state
  700. * @bi: Pointer to where the binding info array is stored in @cbs
  701. * @dirty: Bitmap indicating which bindings need to be emitted.
  702. * @max_num: Maximum number of entries in the @bi array.
  703. *
  704. * Scans the @bi array for bindings that need to be emitted and
  705. * builds a buffer of view id data.
  706. * On output, @cbs->bind_cmd_count contains the number of bindings to be
  707. * emitted, @cbs->bind_first_slot indicates the index of the first emitted
  708. * binding, and @cbs->bind_cmd_buffer contains the command data.
  709. */
  710. static void vmw_collect_dirty_view_ids(struct vmw_ctx_binding_state *cbs,
  711. const struct vmw_ctx_bindinfo *bi,
  712. unsigned long *dirty,
  713. u32 max_num)
  714. {
  715. const struct vmw_ctx_bindinfo_view *biv =
  716. container_of(bi, struct vmw_ctx_bindinfo_view, bi);
  717. unsigned long i, next_bit;
  718. cbs->bind_cmd_count = 0;
  719. i = find_first_bit(dirty, max_num);
  720. next_bit = i;
  721. cbs->bind_first_slot = i;
  722. biv += i;
  723. for (; i < max_num; ++i, ++biv) {
  724. cbs->bind_cmd_buffer[cbs->bind_cmd_count++] =
  725. ((!biv->bi.ctx || biv->bi.scrubbed) ?
  726. SVGA3D_INVALID_ID : biv->bi.res->id);
  727. if (next_bit == i) {
  728. next_bit = find_next_bit(dirty, max_num, i + 1);
  729. if (next_bit >= max_num)
  730. break;
  731. }
  732. }
  733. }
  734. /**
  735. * vmw_emit_set_sr - Issue delayed DX shader resource binding commands
  736. *
  737. * @cbs: Pointer to the context's struct vmw_ctx_binding_state
  738. * @shader_slot: The shader slot of the binding.
  739. */
  740. static int vmw_emit_set_sr(struct vmw_ctx_binding_state *cbs,
  741. int shader_slot)
  742. {
  743. const struct vmw_ctx_bindinfo *loc =
  744. &cbs->per_shader[shader_slot].shader_res[0].bi;
  745. struct {
  746. SVGA3dCmdHeader header;
  747. SVGA3dCmdDXSetShaderResources body;
  748. } *cmd;
  749. size_t cmd_size, view_id_size;
  750. const struct vmw_resource *ctx = vmw_cbs_context(cbs);
  751. vmw_collect_dirty_view_ids(cbs, loc,
  752. cbs->per_shader[shader_slot].dirty_sr,
  753. SVGA3D_DX_MAX_SRVIEWS);
  754. if (cbs->bind_cmd_count == 0)
  755. return 0;
  756. view_id_size = cbs->bind_cmd_count*sizeof(uint32);
  757. cmd_size = sizeof(*cmd) + view_id_size;
  758. cmd = VMW_CMD_CTX_RESERVE(ctx->dev_priv, cmd_size, ctx->id);
  759. if (unlikely(cmd == NULL))
  760. return -ENOMEM;
  761. cmd->header.id = SVGA_3D_CMD_DX_SET_SHADER_RESOURCES;
  762. cmd->header.size = sizeof(cmd->body) + view_id_size;
  763. cmd->body.type = shader_slot + SVGA3D_SHADERTYPE_MIN;
  764. cmd->body.startView = cbs->bind_first_slot;
  765. memcpy(&cmd[1], cbs->bind_cmd_buffer, view_id_size);
  766. vmw_cmd_commit(ctx->dev_priv, cmd_size);
  767. bitmap_clear(cbs->per_shader[shader_slot].dirty_sr,
  768. cbs->bind_first_slot, cbs->bind_cmd_count);
  769. return 0;
  770. }
  771. /**
  772. * vmw_emit_set_rt - Issue delayed DX rendertarget binding commands
  773. *
  774. * @cbs: Pointer to the context's struct vmw_ctx_binding_state
  775. */
  776. static int vmw_emit_set_rt(struct vmw_ctx_binding_state *cbs)
  777. {
  778. const struct vmw_ctx_bindinfo_view *loc = &cbs->render_targets[0];
  779. struct {
  780. SVGA3dCmdHeader header;
  781. SVGA3dCmdDXSetRenderTargets body;
  782. } *cmd;
  783. size_t cmd_size, view_id_size;
  784. const struct vmw_resource *ctx = vmw_cbs_context(cbs);
  785. vmw_collect_view_ids(cbs, loc, SVGA3D_DX_MAX_RENDER_TARGETS);
  786. view_id_size = cbs->bind_cmd_count*sizeof(uint32);
  787. cmd_size = sizeof(*cmd) + view_id_size;
  788. cmd = VMW_CMD_CTX_RESERVE(ctx->dev_priv, cmd_size, ctx->id);
  789. if (unlikely(cmd == NULL))
  790. return -ENOMEM;
  791. cmd->header.id = SVGA_3D_CMD_DX_SET_RENDERTARGETS;
  792. cmd->header.size = sizeof(cmd->body) + view_id_size;
  793. if (cbs->ds_view.bi.ctx && !cbs->ds_view.bi.scrubbed)
  794. cmd->body.depthStencilViewId = cbs->ds_view.bi.res->id;
  795. else
  796. cmd->body.depthStencilViewId = SVGA3D_INVALID_ID;
  797. memcpy(&cmd[1], cbs->bind_cmd_buffer, view_id_size);
  798. vmw_cmd_commit(ctx->dev_priv, cmd_size);
  799. return 0;
  800. }
  801. /**
  802. * vmw_collect_so_targets - Build SVGA3dSoTarget data for a binding command
  803. * without checking which bindings actually need to be emitted
  804. *
  805. * @cbs: Pointer to the context's struct vmw_ctx_binding_state
  806. * @biso: Pointer to where the binding info array is stored in @cbs
  807. * @max_num: Maximum number of entries in the @bi array.
  808. *
  809. * Scans the @bi array for bindings and builds a buffer of SVGA3dSoTarget data.
  810. * Stops at the first non-existing binding in the @bi array.
  811. * On output, @cbs->bind_cmd_count contains the number of bindings to be
  812. * emitted, @cbs->bind_first_slot is set to zero, and @cbs->bind_cmd_buffer
  813. * contains the command data.
  814. */
  815. static void vmw_collect_so_targets(struct vmw_ctx_binding_state *cbs,
  816. const struct vmw_ctx_bindinfo_so_target *biso,
  817. u32 max_num)
  818. {
  819. unsigned long i;
  820. SVGA3dSoTarget *so_buffer = (SVGA3dSoTarget *) cbs->bind_cmd_buffer;
  821. cbs->bind_cmd_count = 0;
  822. cbs->bind_first_slot = 0;
  823. for (i = 0; i < max_num; ++i, ++biso, ++so_buffer,
  824. ++cbs->bind_cmd_count) {
  825. if (!biso->bi.ctx)
  826. break;
  827. if (!biso->bi.scrubbed) {
  828. so_buffer->sid = biso->bi.res->id;
  829. so_buffer->offset = biso->offset;
  830. so_buffer->sizeInBytes = biso->size;
  831. } else {
  832. so_buffer->sid = SVGA3D_INVALID_ID;
  833. so_buffer->offset = 0;
  834. so_buffer->sizeInBytes = 0;
  835. }
  836. }
  837. }
  838. /**
  839. * vmw_emit_set_so_target - Issue delayed streamout binding commands
  840. *
  841. * @cbs: Pointer to the context's struct vmw_ctx_binding_state
  842. */
  843. static int vmw_emit_set_so_target(struct vmw_ctx_binding_state *cbs)
  844. {
  845. const struct vmw_ctx_bindinfo_so_target *loc = &cbs->so_targets[0];
  846. struct {
  847. SVGA3dCmdHeader header;
  848. SVGA3dCmdDXSetSOTargets body;
  849. } *cmd;
  850. size_t cmd_size, so_target_size;
  851. const struct vmw_resource *ctx = vmw_cbs_context(cbs);
  852. vmw_collect_so_targets(cbs, loc, SVGA3D_DX_MAX_SOTARGETS);
  853. if (cbs->bind_cmd_count == 0)
  854. return 0;
  855. so_target_size = cbs->bind_cmd_count*sizeof(SVGA3dSoTarget);
  856. cmd_size = sizeof(*cmd) + so_target_size;
  857. cmd = VMW_CMD_CTX_RESERVE(ctx->dev_priv, cmd_size, ctx->id);
  858. if (unlikely(cmd == NULL))
  859. return -ENOMEM;
  860. cmd->header.id = SVGA_3D_CMD_DX_SET_SOTARGETS;
  861. cmd->header.size = sizeof(cmd->body) + so_target_size;
  862. memcpy(&cmd[1], cbs->bind_cmd_buffer, so_target_size);
  863. vmw_cmd_commit(ctx->dev_priv, cmd_size);
  864. return 0;
  865. }
  866. /**
  867. * vmw_binding_emit_dirty_ps - Issue delayed per shader binding commands
  868. *
  869. * @cbs: Pointer to the context's struct vmw_ctx_binding_state
  870. *
  871. */
  872. static int vmw_binding_emit_dirty_ps(struct vmw_ctx_binding_state *cbs)
  873. {
  874. struct vmw_dx_shader_bindings *sb = &cbs->per_shader[0];
  875. u32 i;
  876. int ret;
  877. for (i = 0; i < SVGA3D_NUM_SHADERTYPE_DX10; ++i, ++sb) {
  878. if (!test_bit(VMW_BINDING_PS_SR_BIT, &sb->dirty))
  879. continue;
  880. ret = vmw_emit_set_sr(cbs, i);
  881. if (ret)
  882. break;
  883. __clear_bit(VMW_BINDING_PS_SR_BIT, &sb->dirty);
  884. }
  885. return 0;
  886. }
  887. /**
  888. * vmw_collect_dirty_vbs - Build SVGA3dVertexBuffer data for a
  889. * SVGA3dCmdDXSetVertexBuffers command
  890. *
  891. * @cbs: Pointer to the context's struct vmw_ctx_binding_state
  892. * @bi: Pointer to where the binding info array is stored in @cbs
  893. * @dirty: Bitmap indicating which bindings need to be emitted.
  894. * @max_num: Maximum number of entries in the @bi array.
  895. *
  896. * Scans the @bi array for bindings that need to be emitted and
  897. * builds a buffer of SVGA3dVertexBuffer data.
  898. * On output, @cbs->bind_cmd_count contains the number of bindings to be
  899. * emitted, @cbs->bind_first_slot indicates the index of the first emitted
  900. * binding, and @cbs->bind_cmd_buffer contains the command data.
  901. */
  902. static void vmw_collect_dirty_vbs(struct vmw_ctx_binding_state *cbs,
  903. const struct vmw_ctx_bindinfo *bi,
  904. unsigned long *dirty,
  905. u32 max_num)
  906. {
  907. const struct vmw_ctx_bindinfo_vb *biv =
  908. container_of(bi, struct vmw_ctx_bindinfo_vb, bi);
  909. unsigned long i, next_bit;
  910. SVGA3dVertexBuffer *vbs = (SVGA3dVertexBuffer *) &cbs->bind_cmd_buffer;
  911. cbs->bind_cmd_count = 0;
  912. i = find_first_bit(dirty, max_num);
  913. next_bit = i;
  914. cbs->bind_first_slot = i;
  915. biv += i;
  916. for (; i < max_num; ++i, ++biv, ++vbs) {
  917. if (!biv->bi.ctx || biv->bi.scrubbed) {
  918. vbs->sid = SVGA3D_INVALID_ID;
  919. vbs->stride = 0;
  920. vbs->offset = 0;
  921. } else {
  922. vbs->sid = biv->bi.res->id;
  923. vbs->stride = biv->stride;
  924. vbs->offset = biv->offset;
  925. }
  926. cbs->bind_cmd_count++;
  927. if (next_bit == i) {
  928. next_bit = find_next_bit(dirty, max_num, i + 1);
  929. if (next_bit >= max_num)
  930. break;
  931. }
  932. }
  933. }
  934. /**
  935. * vmw_emit_set_vb - Issue delayed vertex buffer binding commands
  936. *
  937. * @cbs: Pointer to the context's struct vmw_ctx_binding_state
  938. *
  939. */
  940. static int vmw_emit_set_vb(struct vmw_ctx_binding_state *cbs)
  941. {
  942. const struct vmw_ctx_bindinfo *loc =
  943. &cbs->vertex_buffers[0].bi;
  944. struct {
  945. SVGA3dCmdHeader header;
  946. SVGA3dCmdDXSetVertexBuffers body;
  947. } *cmd;
  948. size_t cmd_size, set_vb_size;
  949. const struct vmw_resource *ctx = vmw_cbs_context(cbs);
  950. vmw_collect_dirty_vbs(cbs, loc, cbs->dirty_vb,
  951. SVGA3D_DX_MAX_VERTEXBUFFERS);
  952. if (cbs->bind_cmd_count == 0)
  953. return 0;
  954. set_vb_size = cbs->bind_cmd_count*sizeof(SVGA3dVertexBuffer);
  955. cmd_size = sizeof(*cmd) + set_vb_size;
  956. cmd = VMW_CMD_CTX_RESERVE(ctx->dev_priv, cmd_size, ctx->id);
  957. if (unlikely(cmd == NULL))
  958. return -ENOMEM;
  959. cmd->header.id = SVGA_3D_CMD_DX_SET_VERTEX_BUFFERS;
  960. cmd->header.size = sizeof(cmd->body) + set_vb_size;
  961. cmd->body.startBuffer = cbs->bind_first_slot;
  962. memcpy(&cmd[1], cbs->bind_cmd_buffer, set_vb_size);
  963. vmw_cmd_commit(ctx->dev_priv, cmd_size);
  964. bitmap_clear(cbs->dirty_vb,
  965. cbs->bind_first_slot, cbs->bind_cmd_count);
  966. return 0;
  967. }
  968. static int vmw_emit_set_uav(struct vmw_ctx_binding_state *cbs)
  969. {
  970. const struct vmw_ctx_bindinfo_view *loc = &cbs->ua_views[0].views[0];
  971. struct {
  972. SVGA3dCmdHeader header;
  973. SVGA3dCmdDXSetUAViews body;
  974. } *cmd;
  975. size_t cmd_size, view_id_size;
  976. const struct vmw_resource *ctx = vmw_cbs_context(cbs);
  977. vmw_collect_view_ids(cbs, loc, vmw_max_num_uavs(cbs->dev_priv));
  978. view_id_size = cbs->bind_cmd_count*sizeof(uint32);
  979. cmd_size = sizeof(*cmd) + view_id_size;
  980. cmd = VMW_CMD_CTX_RESERVE(ctx->dev_priv, cmd_size, ctx->id);
  981. if (!cmd)
  982. return -ENOMEM;
  983. cmd->header.id = SVGA_3D_CMD_DX_SET_UA_VIEWS;
  984. cmd->header.size = sizeof(cmd->body) + view_id_size;
  985. /* Splice index is specified user-space */
  986. cmd->body.uavSpliceIndex = cbs->ua_views[0].index;
  987. memcpy(&cmd[1], cbs->bind_cmd_buffer, view_id_size);
  988. vmw_cmd_commit(ctx->dev_priv, cmd_size);
  989. return 0;
  990. }
  991. static int vmw_emit_set_cs_uav(struct vmw_ctx_binding_state *cbs)
  992. {
  993. const struct vmw_ctx_bindinfo_view *loc = &cbs->ua_views[1].views[0];
  994. struct {
  995. SVGA3dCmdHeader header;
  996. SVGA3dCmdDXSetCSUAViews body;
  997. } *cmd;
  998. size_t cmd_size, view_id_size;
  999. const struct vmw_resource *ctx = vmw_cbs_context(cbs);
  1000. vmw_collect_view_ids(cbs, loc, vmw_max_num_uavs(cbs->dev_priv));
  1001. view_id_size = cbs->bind_cmd_count*sizeof(uint32);
  1002. cmd_size = sizeof(*cmd) + view_id_size;
  1003. cmd = VMW_CMD_CTX_RESERVE(ctx->dev_priv, cmd_size, ctx->id);
  1004. if (!cmd)
  1005. return -ENOMEM;
  1006. cmd->header.id = SVGA_3D_CMD_DX_SET_CS_UA_VIEWS;
  1007. cmd->header.size = sizeof(cmd->body) + view_id_size;
  1008. /* Start index is specified user-space */
  1009. cmd->body.startIndex = cbs->ua_views[1].index;
  1010. memcpy(&cmd[1], cbs->bind_cmd_buffer, view_id_size);
  1011. vmw_cmd_commit(ctx->dev_priv, cmd_size);
  1012. return 0;
  1013. }
  1014. /**
  1015. * vmw_binding_emit_dirty - Issue delayed binding commands
  1016. *
  1017. * @cbs: Pointer to the context's struct vmw_ctx_binding_state
  1018. *
  1019. * This function issues the delayed binding commands that arise from
  1020. * previous scrub / unscrub calls. These binding commands are typically
  1021. * commands that batch a number of bindings and therefore it makes sense
  1022. * to delay them.
  1023. */
  1024. static int vmw_binding_emit_dirty(struct vmw_ctx_binding_state *cbs)
  1025. {
  1026. int ret = 0;
  1027. unsigned long hit = 0;
  1028. while ((hit = find_next_bit(&cbs->dirty, VMW_BINDING_NUM_BITS, hit))
  1029. < VMW_BINDING_NUM_BITS) {
  1030. switch (hit) {
  1031. case VMW_BINDING_RT_BIT:
  1032. ret = vmw_emit_set_rt(cbs);
  1033. break;
  1034. case VMW_BINDING_PS_BIT:
  1035. ret = vmw_binding_emit_dirty_ps(cbs);
  1036. break;
  1037. case VMW_BINDING_SO_T_BIT:
  1038. ret = vmw_emit_set_so_target(cbs);
  1039. break;
  1040. case VMW_BINDING_VB_BIT:
  1041. ret = vmw_emit_set_vb(cbs);
  1042. break;
  1043. case VMW_BINDING_UAV_BIT:
  1044. ret = vmw_emit_set_uav(cbs);
  1045. break;
  1046. case VMW_BINDING_CS_UAV_BIT:
  1047. ret = vmw_emit_set_cs_uav(cbs);
  1048. break;
  1049. default:
  1050. BUG();
  1051. }
  1052. if (ret)
  1053. return ret;
  1054. __clear_bit(hit, &cbs->dirty);
  1055. hit++;
  1056. }
  1057. return 0;
  1058. }
  1059. /**
  1060. * vmw_binding_scrub_sr - Schedule a dx shaderresource binding
  1061. * scrub from a context
  1062. *
  1063. * @bi: single binding information.
  1064. * @rebind: Whether to issue a bind instead of scrub command.
  1065. */
  1066. static int vmw_binding_scrub_sr(struct vmw_ctx_bindinfo *bi, bool rebind)
  1067. {
  1068. struct vmw_ctx_bindinfo_view *biv =
  1069. container_of(bi, struct vmw_ctx_bindinfo_view, bi);
  1070. struct vmw_ctx_binding_state *cbs =
  1071. vmw_context_binding_state(bi->ctx);
  1072. __set_bit(biv->slot, cbs->per_shader[biv->shader_slot].dirty_sr);
  1073. __set_bit(VMW_BINDING_PS_SR_BIT,
  1074. &cbs->per_shader[biv->shader_slot].dirty);
  1075. __set_bit(VMW_BINDING_PS_BIT, &cbs->dirty);
  1076. return 0;
  1077. }
  1078. /**
  1079. * vmw_binding_scrub_dx_rt - Schedule a dx rendertarget binding
  1080. * scrub from a context
  1081. *
  1082. * @bi: single binding information.
  1083. * @rebind: Whether to issue a bind instead of scrub command.
  1084. */
  1085. static int vmw_binding_scrub_dx_rt(struct vmw_ctx_bindinfo *bi, bool rebind)
  1086. {
  1087. struct vmw_ctx_binding_state *cbs =
  1088. vmw_context_binding_state(bi->ctx);
  1089. __set_bit(VMW_BINDING_RT_BIT, &cbs->dirty);
  1090. return 0;
  1091. }
  1092. /**
  1093. * vmw_binding_scrub_so_target - Schedule a dx streamoutput buffer binding
  1094. * scrub from a context
  1095. *
  1096. * @bi: single binding information.
  1097. * @rebind: Whether to issue a bind instead of scrub command.
  1098. */
  1099. static int vmw_binding_scrub_so_target(struct vmw_ctx_bindinfo *bi, bool rebind)
  1100. {
  1101. struct vmw_ctx_binding_state *cbs =
  1102. vmw_context_binding_state(bi->ctx);
  1103. __set_bit(VMW_BINDING_SO_T_BIT, &cbs->dirty);
  1104. return 0;
  1105. }
  1106. /**
  1107. * vmw_binding_scrub_vb - Schedule a dx vertex buffer binding
  1108. * scrub from a context
  1109. *
  1110. * @bi: single binding information.
  1111. * @rebind: Whether to issue a bind instead of scrub command.
  1112. */
  1113. static int vmw_binding_scrub_vb(struct vmw_ctx_bindinfo *bi, bool rebind)
  1114. {
  1115. struct vmw_ctx_bindinfo_vb *bivb =
  1116. container_of(bi, struct vmw_ctx_bindinfo_vb, bi);
  1117. struct vmw_ctx_binding_state *cbs =
  1118. vmw_context_binding_state(bi->ctx);
  1119. __set_bit(bivb->slot, cbs->dirty_vb);
  1120. __set_bit(VMW_BINDING_VB_BIT, &cbs->dirty);
  1121. return 0;
  1122. }
  1123. /**
  1124. * vmw_binding_scrub_ib - scrub a dx index buffer binding from a context
  1125. *
  1126. * @bi: single binding information.
  1127. * @rebind: Whether to issue a bind instead of scrub command.
  1128. */
  1129. static int vmw_binding_scrub_ib(struct vmw_ctx_bindinfo *bi, bool rebind)
  1130. {
  1131. struct vmw_ctx_bindinfo_ib *binding =
  1132. container_of(bi, typeof(*binding), bi);
  1133. struct vmw_private *dev_priv = bi->ctx->dev_priv;
  1134. struct {
  1135. SVGA3dCmdHeader header;
  1136. SVGA3dCmdDXSetIndexBuffer body;
  1137. } *cmd;
  1138. cmd = VMW_CMD_CTX_RESERVE(dev_priv, sizeof(*cmd), bi->ctx->id);
  1139. if (unlikely(cmd == NULL))
  1140. return -ENOMEM;
  1141. cmd->header.id = SVGA_3D_CMD_DX_SET_INDEX_BUFFER;
  1142. cmd->header.size = sizeof(cmd->body);
  1143. if (rebind) {
  1144. cmd->body.sid = bi->res->id;
  1145. cmd->body.format = binding->format;
  1146. cmd->body.offset = binding->offset;
  1147. } else {
  1148. cmd->body.sid = SVGA3D_INVALID_ID;
  1149. cmd->body.format = 0;
  1150. cmd->body.offset = 0;
  1151. }
  1152. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  1153. return 0;
  1154. }
  1155. static int vmw_binding_scrub_uav(struct vmw_ctx_bindinfo *bi, bool rebind)
  1156. {
  1157. struct vmw_ctx_binding_state *cbs = vmw_context_binding_state(bi->ctx);
  1158. __set_bit(VMW_BINDING_UAV_BIT, &cbs->dirty);
  1159. return 0;
  1160. }
  1161. static int vmw_binding_scrub_cs_uav(struct vmw_ctx_bindinfo *bi, bool rebind)
  1162. {
  1163. struct vmw_ctx_binding_state *cbs = vmw_context_binding_state(bi->ctx);
  1164. __set_bit(VMW_BINDING_CS_UAV_BIT, &cbs->dirty);
  1165. return 0;
  1166. }
  1167. /**
  1168. * vmw_binding_scrub_so - Scrub a streamoutput binding from context.
  1169. * @bi: Single binding information.
  1170. * @rebind: Whether to issue a bind instead of scrub command.
  1171. */
  1172. static int vmw_binding_scrub_so(struct vmw_ctx_bindinfo *bi, bool rebind)
  1173. {
  1174. struct vmw_ctx_bindinfo_so *binding =
  1175. container_of(bi, typeof(*binding), bi);
  1176. struct vmw_private *dev_priv = bi->ctx->dev_priv;
  1177. struct {
  1178. SVGA3dCmdHeader header;
  1179. SVGA3dCmdDXSetStreamOutput body;
  1180. } *cmd;
  1181. cmd = VMW_CMD_CTX_RESERVE(dev_priv, sizeof(*cmd), bi->ctx->id);
  1182. if (!cmd)
  1183. return -ENOMEM;
  1184. cmd->header.id = SVGA_3D_CMD_DX_SET_STREAMOUTPUT;
  1185. cmd->header.size = sizeof(cmd->body);
  1186. cmd->body.soid = rebind ? bi->res->id : SVGA3D_INVALID_ID;
  1187. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  1188. return 0;
  1189. }
  1190. /**
  1191. * vmw_binding_state_alloc - Allocate a struct vmw_ctx_binding_state.
  1192. *
  1193. * @dev_priv: Pointer to a device private structure.
  1194. *
  1195. * Returns a pointer to a newly allocated struct or an error pointer on error.
  1196. */
  1197. struct vmw_ctx_binding_state *
  1198. vmw_binding_state_alloc(struct vmw_private *dev_priv)
  1199. {
  1200. struct vmw_ctx_binding_state *cbs;
  1201. cbs = vzalloc(sizeof(*cbs));
  1202. if (!cbs) {
  1203. return ERR_PTR(-ENOMEM);
  1204. }
  1205. cbs->dev_priv = dev_priv;
  1206. INIT_LIST_HEAD(&cbs->list);
  1207. return cbs;
  1208. }
  1209. /**
  1210. * vmw_binding_state_free - Free a struct vmw_ctx_binding_state.
  1211. *
  1212. * @cbs: Pointer to the struct vmw_ctx_binding_state to be freed.
  1213. */
  1214. void vmw_binding_state_free(struct vmw_ctx_binding_state *cbs)
  1215. {
  1216. vfree(cbs);
  1217. }
  1218. /**
  1219. * vmw_binding_state_list - Get the binding list of a
  1220. * struct vmw_ctx_binding_state
  1221. *
  1222. * @cbs: Pointer to the struct vmw_ctx_binding_state
  1223. *
  1224. * Returns the binding list which can be used to traverse through the bindings
  1225. * and access the resource information of all bindings.
  1226. */
  1227. struct list_head *vmw_binding_state_list(struct vmw_ctx_binding_state *cbs)
  1228. {
  1229. return &cbs->list;
  1230. }
  1231. /**
  1232. * vmw_binding_state_reset - clear a struct vmw_ctx_binding_state
  1233. *
  1234. * @cbs: Pointer to the struct vmw_ctx_binding_state to be cleared
  1235. *
  1236. * Drops all bindings registered in @cbs. No device binding actions are
  1237. * performed.
  1238. */
  1239. void vmw_binding_state_reset(struct vmw_ctx_binding_state *cbs)
  1240. {
  1241. struct vmw_ctx_bindinfo *entry, *next;
  1242. list_for_each_entry_safe(entry, next, &cbs->list, ctx_list)
  1243. vmw_binding_drop(entry);
  1244. }
  1245. /**
  1246. * vmw_binding_dirtying - Return whether a binding type is dirtying its resource
  1247. * @binding_type: The binding type
  1248. *
  1249. * Each time a resource is put on the validation list as the result of a
  1250. * context binding referencing it, we need to determine whether that resource
  1251. * will be dirtied (written to by the GPU) as a result of the corresponding
  1252. * GPU operation. Currently rendertarget-, depth-stencil-, stream-output-target
  1253. * and unordered access view bindings are capable of dirtying its resource.
  1254. *
  1255. * Return: Whether the binding type dirties the resource its binding points to.
  1256. */
  1257. u32 vmw_binding_dirtying(enum vmw_ctx_binding_type binding_type)
  1258. {
  1259. static u32 is_binding_dirtying[vmw_ctx_binding_max] = {
  1260. [vmw_ctx_binding_rt] = VMW_RES_DIRTY_SET,
  1261. [vmw_ctx_binding_dx_rt] = VMW_RES_DIRTY_SET,
  1262. [vmw_ctx_binding_ds] = VMW_RES_DIRTY_SET,
  1263. [vmw_ctx_binding_so_target] = VMW_RES_DIRTY_SET,
  1264. [vmw_ctx_binding_uav] = VMW_RES_DIRTY_SET,
  1265. [vmw_ctx_binding_cs_uav] = VMW_RES_DIRTY_SET,
  1266. };
  1267. /* Review this function as new bindings are added. */
  1268. BUILD_BUG_ON(vmw_ctx_binding_max != 14);
  1269. return is_binding_dirtying[binding_type];
  1270. }
  1271. /*
  1272. * This function is unused at run-time, and only used to hold various build
  1273. * asserts important for code optimization assumptions.
  1274. */
  1275. static void vmw_binding_build_asserts(void)
  1276. {
  1277. BUILD_BUG_ON(SVGA3D_NUM_SHADERTYPE_DX10 != 3);
  1278. BUILD_BUG_ON(SVGA3D_DX_MAX_RENDER_TARGETS > SVGA3D_RT_MAX);
  1279. BUILD_BUG_ON(sizeof(uint32) != sizeof(u32));
  1280. /*
  1281. * struct vmw_ctx_binding_state::bind_cmd_buffer is used for various
  1282. * view id arrays.
  1283. */
  1284. BUILD_BUG_ON(VMW_MAX_VIEW_BINDINGS < SVGA3D_RT_MAX);
  1285. BUILD_BUG_ON(VMW_MAX_VIEW_BINDINGS < SVGA3D_DX_MAX_SRVIEWS);
  1286. BUILD_BUG_ON(VMW_MAX_VIEW_BINDINGS < SVGA3D_DX_MAX_CONSTBUFFERS);
  1287. /*
  1288. * struct vmw_ctx_binding_state::bind_cmd_buffer is used for
  1289. * u32 view ids, SVGA3dSoTargets and SVGA3dVertexBuffers
  1290. */
  1291. BUILD_BUG_ON(SVGA3D_DX_MAX_SOTARGETS*sizeof(SVGA3dSoTarget) >
  1292. VMW_MAX_VIEW_BINDINGS*sizeof(u32));
  1293. BUILD_BUG_ON(SVGA3D_DX_MAX_VERTEXBUFFERS*sizeof(SVGA3dVertexBuffer) >
  1294. VMW_MAX_VIEW_BINDINGS*sizeof(u32));
  1295. }