vmwgfx_so.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /**************************************************************************
  3. * Copyright 2014-2015 VMware, Inc., Palo Alto, CA., USA
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sub license, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial portions
  15. * of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  20. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  21. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  23. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. **************************************************************************/
  26. #include "vmwgfx_drv.h"
  27. #include "vmwgfx_resource_priv.h"
  28. #include "vmwgfx_so.h"
  29. #include "vmwgfx_binding.h"
  30. /*
  31. * The currently only reason we need to keep track of views is that if we
  32. * destroy a hardware surface, all views pointing to it must also be destroyed,
  33. * otherwise the device will error.
  34. * So in particuar if a surface is evicted, we must destroy all views pointing
  35. * to it, and all context bindings of that view. Similarly we must restore
  36. * the view bindings, views and surfaces pointed to by the views when a
  37. * context is referenced in the command stream.
  38. */
  39. /**
  40. * struct vmw_view - view metadata
  41. *
  42. * @res: The struct vmw_resource we derive from
  43. * @ctx: Non-refcounted pointer to the context this view belongs to.
  44. * @srf: Refcounted pointer to the surface pointed to by this view.
  45. * @cotable: Refcounted pointer to the cotable holding this view.
  46. * @srf_head: List head for the surface-to-view list.
  47. * @cotable_head: List head for the cotable-to_view list.
  48. * @view_type: View type.
  49. * @view_id: User-space per context view id. Currently used also as per
  50. * context device view id.
  51. * @cmd_size: Size of the SVGA3D define view command that we've copied from the
  52. * command stream.
  53. * @committed: Whether the view is actually created or pending creation at the
  54. * device level.
  55. * @cmd: The SVGA3D define view command copied from the command stream.
  56. */
  57. struct vmw_view {
  58. struct rcu_head rcu;
  59. struct vmw_resource res;
  60. struct vmw_resource *ctx; /* Immutable */
  61. struct vmw_resource *srf; /* Immutable */
  62. struct vmw_resource *cotable; /* Immutable */
  63. struct list_head srf_head; /* Protected by binding_mutex */
  64. struct list_head cotable_head; /* Protected by binding_mutex */
  65. unsigned view_type; /* Immutable */
  66. unsigned view_id; /* Immutable */
  67. u32 cmd_size; /* Immutable */
  68. bool committed; /* Protected by binding_mutex */
  69. u32 cmd[1]; /* Immutable */
  70. };
  71. static int vmw_view_create(struct vmw_resource *res);
  72. static int vmw_view_destroy(struct vmw_resource *res);
  73. static void vmw_hw_view_destroy(struct vmw_resource *res);
  74. static void vmw_view_commit_notify(struct vmw_resource *res,
  75. enum vmw_cmdbuf_res_state state);
  76. static const struct vmw_res_func vmw_view_func = {
  77. .res_type = vmw_res_view,
  78. .needs_backup = false,
  79. .may_evict = false,
  80. .type_name = "DX view",
  81. .backup_placement = NULL,
  82. .create = vmw_view_create,
  83. .commit_notify = vmw_view_commit_notify,
  84. };
  85. /**
  86. * struct vmw_view - view define command body stub
  87. *
  88. * @view_id: The device id of the view being defined
  89. * @sid: The surface id of the view being defined
  90. *
  91. * This generic struct is used by the code to change @view_id and @sid of a
  92. * saved view define command.
  93. */
  94. struct vmw_view_define {
  95. uint32 view_id;
  96. uint32 sid;
  97. };
  98. /**
  99. * vmw_view - Convert a struct vmw_resource to a struct vmw_view
  100. *
  101. * @res: Pointer to the resource to convert.
  102. *
  103. * Returns a pointer to a struct vmw_view.
  104. */
  105. static struct vmw_view *vmw_view(struct vmw_resource *res)
  106. {
  107. return container_of(res, struct vmw_view, res);
  108. }
  109. /**
  110. * vmw_view_commit_notify - Notify that a view operation has been committed to
  111. * hardware from a user-supplied command stream.
  112. *
  113. * @res: Pointer to the view resource.
  114. * @state: Indicating whether a creation or removal has been committed.
  115. *
  116. */
  117. static void vmw_view_commit_notify(struct vmw_resource *res,
  118. enum vmw_cmdbuf_res_state state)
  119. {
  120. struct vmw_view *view = vmw_view(res);
  121. struct vmw_private *dev_priv = res->dev_priv;
  122. mutex_lock(&dev_priv->binding_mutex);
  123. if (state == VMW_CMDBUF_RES_ADD) {
  124. struct vmw_surface *srf = vmw_res_to_srf(view->srf);
  125. list_add_tail(&view->srf_head, &srf->view_list);
  126. vmw_cotable_add_resource(view->cotable, &view->cotable_head);
  127. view->committed = true;
  128. res->id = view->view_id;
  129. } else {
  130. list_del_init(&view->cotable_head);
  131. list_del_init(&view->srf_head);
  132. view->committed = false;
  133. res->id = -1;
  134. }
  135. mutex_unlock(&dev_priv->binding_mutex);
  136. }
  137. /**
  138. * vmw_view_create - Create a hardware view.
  139. *
  140. * @res: Pointer to the view resource.
  141. *
  142. * Create a hardware view. Typically used if that view has previously been
  143. * destroyed by an eviction operation.
  144. */
  145. static int vmw_view_create(struct vmw_resource *res)
  146. {
  147. struct vmw_view *view = vmw_view(res);
  148. struct vmw_surface *srf = vmw_res_to_srf(view->srf);
  149. struct vmw_private *dev_priv = res->dev_priv;
  150. struct {
  151. SVGA3dCmdHeader header;
  152. struct vmw_view_define body;
  153. } *cmd;
  154. mutex_lock(&dev_priv->binding_mutex);
  155. if (!view->committed) {
  156. mutex_unlock(&dev_priv->binding_mutex);
  157. return 0;
  158. }
  159. cmd = vmw_fifo_reserve_dx(res->dev_priv, view->cmd_size,
  160. view->ctx->id);
  161. if (!cmd) {
  162. DRM_ERROR("Failed reserving FIFO space for view creation.\n");
  163. mutex_unlock(&dev_priv->binding_mutex);
  164. return -ENOMEM;
  165. }
  166. memcpy(cmd, &view->cmd, view->cmd_size);
  167. WARN_ON(cmd->body.view_id != view->view_id);
  168. /* Sid may have changed due to surface eviction. */
  169. WARN_ON(view->srf->id == SVGA3D_INVALID_ID);
  170. cmd->body.sid = view->srf->id;
  171. vmw_fifo_commit(res->dev_priv, view->cmd_size);
  172. res->id = view->view_id;
  173. list_add_tail(&view->srf_head, &srf->view_list);
  174. vmw_cotable_add_resource(view->cotable, &view->cotable_head);
  175. mutex_unlock(&dev_priv->binding_mutex);
  176. return 0;
  177. }
  178. /**
  179. * vmw_view_destroy - Destroy a hardware view.
  180. *
  181. * @res: Pointer to the view resource.
  182. *
  183. * Destroy a hardware view. Typically used on unexpected termination of the
  184. * owning process or if the surface the view is pointing to is destroyed.
  185. */
  186. static int vmw_view_destroy(struct vmw_resource *res)
  187. {
  188. struct vmw_private *dev_priv = res->dev_priv;
  189. struct vmw_view *view = vmw_view(res);
  190. struct {
  191. SVGA3dCmdHeader header;
  192. union vmw_view_destroy body;
  193. } *cmd;
  194. WARN_ON_ONCE(!mutex_is_locked(&dev_priv->binding_mutex));
  195. vmw_binding_res_list_scrub(&res->binding_head);
  196. if (!view->committed || res->id == -1)
  197. return 0;
  198. cmd = vmw_fifo_reserve_dx(dev_priv, sizeof(*cmd), view->ctx->id);
  199. if (!cmd) {
  200. DRM_ERROR("Failed reserving FIFO space for view "
  201. "destruction.\n");
  202. return -ENOMEM;
  203. }
  204. cmd->header.id = vmw_view_destroy_cmds[view->view_type];
  205. cmd->header.size = sizeof(cmd->body);
  206. cmd->body.view_id = view->view_id;
  207. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  208. res->id = -1;
  209. list_del_init(&view->cotable_head);
  210. list_del_init(&view->srf_head);
  211. return 0;
  212. }
  213. /**
  214. * vmw_hw_view_destroy - Destroy a hardware view as part of resource cleanup.
  215. *
  216. * @res: Pointer to the view resource.
  217. *
  218. * Destroy a hardware view if it's still present.
  219. */
  220. static void vmw_hw_view_destroy(struct vmw_resource *res)
  221. {
  222. struct vmw_private *dev_priv = res->dev_priv;
  223. mutex_lock(&dev_priv->binding_mutex);
  224. WARN_ON(vmw_view_destroy(res));
  225. res->id = -1;
  226. mutex_unlock(&dev_priv->binding_mutex);
  227. }
  228. /**
  229. * vmw_view_key - Compute a view key suitable for the cmdbuf resource manager
  230. *
  231. * @user_key: The user-space id used for the view.
  232. * @view_type: The view type.
  233. *
  234. * Destroy a hardware view if it's still present.
  235. */
  236. static u32 vmw_view_key(u32 user_key, enum vmw_view_type view_type)
  237. {
  238. return user_key | (view_type << 20);
  239. }
  240. /**
  241. * vmw_view_id_ok - Basic view id and type range checks.
  242. *
  243. * @user_key: The user-space id used for the view.
  244. * @view_type: The view type.
  245. *
  246. * Checks that the view id and type (typically provided by user-space) is
  247. * valid.
  248. */
  249. static bool vmw_view_id_ok(u32 user_key, enum vmw_view_type view_type)
  250. {
  251. return (user_key < SVGA_COTABLE_MAX_IDS &&
  252. view_type < vmw_view_max);
  253. }
  254. /**
  255. * vmw_view_res_free - resource res_free callback for view resources
  256. *
  257. * @res: Pointer to a struct vmw_resource
  258. *
  259. * Frees memory and memory accounting held by a struct vmw_view.
  260. */
  261. static void vmw_view_res_free(struct vmw_resource *res)
  262. {
  263. struct vmw_view *view = vmw_view(res);
  264. size_t size = offsetof(struct vmw_view, cmd) + view->cmd_size;
  265. struct vmw_private *dev_priv = res->dev_priv;
  266. vmw_resource_unreference(&view->cotable);
  267. vmw_resource_unreference(&view->srf);
  268. kfree_rcu(view, rcu);
  269. ttm_mem_global_free(vmw_mem_glob(dev_priv), size);
  270. }
  271. /**
  272. * vmw_view_add - Create a view resource and stage it for addition
  273. * as a command buffer managed resource.
  274. *
  275. * @man: Pointer to the compat shader manager identifying the shader namespace.
  276. * @ctx: Pointer to a struct vmw_resource identifying the active context.
  277. * @srf: Pointer to a struct vmw_resource identifying the surface the view
  278. * points to.
  279. * @view_type: The view type deduced from the view create command.
  280. * @user_key: The key that is used to identify the shader. The key is
  281. * unique to the view type and to the context.
  282. * @cmd: Pointer to the view create command in the command stream.
  283. * @cmd_size: Size of the view create command in the command stream.
  284. * @list: Caller's list of staged command buffer resource actions.
  285. */
  286. int vmw_view_add(struct vmw_cmdbuf_res_manager *man,
  287. struct vmw_resource *ctx,
  288. struct vmw_resource *srf,
  289. enum vmw_view_type view_type,
  290. u32 user_key,
  291. const void *cmd,
  292. size_t cmd_size,
  293. struct list_head *list)
  294. {
  295. static const size_t vmw_view_define_sizes[] = {
  296. [vmw_view_sr] = sizeof(SVGA3dCmdDXDefineShaderResourceView),
  297. [vmw_view_rt] = sizeof(SVGA3dCmdDXDefineRenderTargetView),
  298. [vmw_view_ds] = sizeof(SVGA3dCmdDXDefineDepthStencilView)
  299. };
  300. struct vmw_private *dev_priv = ctx->dev_priv;
  301. struct vmw_resource *res;
  302. struct vmw_view *view;
  303. struct ttm_operation_ctx ttm_opt_ctx = {
  304. .interruptible = true,
  305. .no_wait_gpu = false
  306. };
  307. size_t size;
  308. int ret;
  309. if (cmd_size != vmw_view_define_sizes[view_type] +
  310. sizeof(SVGA3dCmdHeader)) {
  311. DRM_ERROR("Illegal view create command size.\n");
  312. return -EINVAL;
  313. }
  314. if (!vmw_view_id_ok(user_key, view_type)) {
  315. DRM_ERROR("Illegal view add view id.\n");
  316. return -EINVAL;
  317. }
  318. size = offsetof(struct vmw_view, cmd) + cmd_size;
  319. ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv), size, &ttm_opt_ctx);
  320. if (ret) {
  321. if (ret != -ERESTARTSYS)
  322. DRM_ERROR("Out of graphics memory for view"
  323. " creation.\n");
  324. return ret;
  325. }
  326. view = kmalloc(size, GFP_KERNEL);
  327. if (!view) {
  328. ttm_mem_global_free(vmw_mem_glob(dev_priv), size);
  329. return -ENOMEM;
  330. }
  331. res = &view->res;
  332. view->ctx = ctx;
  333. view->srf = vmw_resource_reference(srf);
  334. view->cotable = vmw_context_cotable(ctx, vmw_view_cotables[view_type]);
  335. view->view_type = view_type;
  336. view->view_id = user_key;
  337. view->cmd_size = cmd_size;
  338. view->committed = false;
  339. INIT_LIST_HEAD(&view->srf_head);
  340. INIT_LIST_HEAD(&view->cotable_head);
  341. memcpy(&view->cmd, cmd, cmd_size);
  342. ret = vmw_resource_init(dev_priv, res, true,
  343. vmw_view_res_free, &vmw_view_func);
  344. if (ret)
  345. goto out_resource_init;
  346. ret = vmw_cmdbuf_res_add(man, vmw_cmdbuf_res_view,
  347. vmw_view_key(user_key, view_type),
  348. res, list);
  349. if (ret)
  350. goto out_resource_init;
  351. res->id = view->view_id;
  352. vmw_resource_activate(res, vmw_hw_view_destroy);
  353. out_resource_init:
  354. vmw_resource_unreference(&res);
  355. return ret;
  356. }
  357. /**
  358. * vmw_view_remove - Stage a view for removal.
  359. *
  360. * @man: Pointer to the view manager identifying the shader namespace.
  361. * @user_key: The key that is used to identify the view. The key is
  362. * unique to the view type.
  363. * @view_type: View type
  364. * @list: Caller's list of staged command buffer resource actions.
  365. * @res_p: If the resource is in an already committed state, points to the
  366. * struct vmw_resource on successful return. The pointer will be
  367. * non ref-counted.
  368. */
  369. int vmw_view_remove(struct vmw_cmdbuf_res_manager *man,
  370. u32 user_key, enum vmw_view_type view_type,
  371. struct list_head *list,
  372. struct vmw_resource **res_p)
  373. {
  374. if (!vmw_view_id_ok(user_key, view_type)) {
  375. DRM_ERROR("Illegal view remove view id.\n");
  376. return -EINVAL;
  377. }
  378. return vmw_cmdbuf_res_remove(man, vmw_cmdbuf_res_view,
  379. vmw_view_key(user_key, view_type),
  380. list, res_p);
  381. }
  382. /**
  383. * vmw_view_cotable_list_destroy - Evict all views belonging to a cotable.
  384. *
  385. * @dev_priv: Pointer to a device private struct.
  386. * @list: List of views belonging to a cotable.
  387. * @readback: Unused. Needed for function interface only.
  388. *
  389. * This function evicts all views belonging to a cotable.
  390. * It must be called with the binding_mutex held, and the caller must hold
  391. * a reference to the view resource. This is typically called before the
  392. * cotable is paged out.
  393. */
  394. void vmw_view_cotable_list_destroy(struct vmw_private *dev_priv,
  395. struct list_head *list,
  396. bool readback)
  397. {
  398. struct vmw_view *entry, *next;
  399. WARN_ON_ONCE(!mutex_is_locked(&dev_priv->binding_mutex));
  400. list_for_each_entry_safe(entry, next, list, cotable_head)
  401. WARN_ON(vmw_view_destroy(&entry->res));
  402. }
  403. /**
  404. * vmw_view_surface_list_destroy - Evict all views pointing to a surface
  405. *
  406. * @dev_priv: Pointer to a device private struct.
  407. * @list: List of views pointing to a surface.
  408. *
  409. * This function evicts all views pointing to a surface. This is typically
  410. * called before the surface is evicted.
  411. */
  412. void vmw_view_surface_list_destroy(struct vmw_private *dev_priv,
  413. struct list_head *list)
  414. {
  415. struct vmw_view *entry, *next;
  416. WARN_ON_ONCE(!mutex_is_locked(&dev_priv->binding_mutex));
  417. list_for_each_entry_safe(entry, next, list, srf_head)
  418. WARN_ON(vmw_view_destroy(&entry->res));
  419. }
  420. /**
  421. * vmw_view_srf - Return a non-refcounted pointer to the surface a view is
  422. * pointing to.
  423. *
  424. * @res: pointer to a view resource.
  425. *
  426. * Note that the view itself is holding a reference, so as long
  427. * the view resource is alive, the surface resource will be.
  428. */
  429. struct vmw_resource *vmw_view_srf(struct vmw_resource *res)
  430. {
  431. return vmw_view(res)->srf;
  432. }
  433. /**
  434. * vmw_view_lookup - Look up a view.
  435. *
  436. * @man: The context's cmdbuf ref manager.
  437. * @view_type: The view type.
  438. * @user_key: The view user id.
  439. *
  440. * returns a refcounted pointer to a view or an error pointer if not found.
  441. */
  442. struct vmw_resource *vmw_view_lookup(struct vmw_cmdbuf_res_manager *man,
  443. enum vmw_view_type view_type,
  444. u32 user_key)
  445. {
  446. return vmw_cmdbuf_res_lookup(man, vmw_cmdbuf_res_view,
  447. vmw_view_key(user_key, view_type));
  448. }
  449. const u32 vmw_view_destroy_cmds[] = {
  450. [vmw_view_sr] = SVGA_3D_CMD_DX_DESTROY_SHADERRESOURCE_VIEW,
  451. [vmw_view_rt] = SVGA_3D_CMD_DX_DESTROY_RENDERTARGET_VIEW,
  452. [vmw_view_ds] = SVGA_3D_CMD_DX_DESTROY_DEPTHSTENCIL_VIEW,
  453. };
  454. const SVGACOTableType vmw_view_cotables[] = {
  455. [vmw_view_sr] = SVGA_COTABLE_SRVIEW,
  456. [vmw_view_rt] = SVGA_COTABLE_RTVIEW,
  457. [vmw_view_ds] = SVGA_COTABLE_DSVIEW,
  458. };
  459. const SVGACOTableType vmw_so_cotables[] = {
  460. [vmw_so_el] = SVGA_COTABLE_ELEMENTLAYOUT,
  461. [vmw_so_bs] = SVGA_COTABLE_BLENDSTATE,
  462. [vmw_so_ds] = SVGA_COTABLE_DEPTHSTENCIL,
  463. [vmw_so_rs] = SVGA_COTABLE_RASTERIZERSTATE,
  464. [vmw_so_ss] = SVGA_COTABLE_SAMPLER,
  465. [vmw_so_so] = SVGA_COTABLE_STREAMOUTPUT
  466. };
  467. /* To remove unused function warning */
  468. static void vmw_so_build_asserts(void) __attribute__((used));
  469. /*
  470. * This function is unused at run-time, and only used to dump various build
  471. * asserts important for code optimization assumptions.
  472. */
  473. static void vmw_so_build_asserts(void)
  474. {
  475. /* Assert that our vmw_view_cmd_to_type() function is correct. */
  476. BUILD_BUG_ON(SVGA_3D_CMD_DX_DESTROY_SHADERRESOURCE_VIEW !=
  477. SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 1);
  478. BUILD_BUG_ON(SVGA_3D_CMD_DX_DEFINE_RENDERTARGET_VIEW !=
  479. SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 2);
  480. BUILD_BUG_ON(SVGA_3D_CMD_DX_DESTROY_RENDERTARGET_VIEW !=
  481. SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 3);
  482. BUILD_BUG_ON(SVGA_3D_CMD_DX_DEFINE_DEPTHSTENCIL_VIEW !=
  483. SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 4);
  484. BUILD_BUG_ON(SVGA_3D_CMD_DX_DESTROY_DEPTHSTENCIL_VIEW !=
  485. SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 5);
  486. /* Assert that our "one body fits all" assumption is valid */
  487. BUILD_BUG_ON(sizeof(union vmw_view_destroy) != sizeof(u32));
  488. /* Assert that the view key space can hold all view ids. */
  489. BUILD_BUG_ON(SVGA_COTABLE_MAX_IDS >= ((1 << 20) - 1));
  490. /*
  491. * Assert that the offset of sid in all view define commands
  492. * is what we assume it to be.
  493. */
  494. BUILD_BUG_ON(offsetof(struct vmw_view_define, sid) !=
  495. offsetof(SVGA3dCmdDXDefineShaderResourceView, sid));
  496. BUILD_BUG_ON(offsetof(struct vmw_view_define, sid) !=
  497. offsetof(SVGA3dCmdDXDefineRenderTargetView, sid));
  498. BUILD_BUG_ON(offsetof(struct vmw_view_define, sid) !=
  499. offsetof(SVGA3dCmdDXDefineDepthStencilView, sid));
  500. }