vmwgfx_so.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /**************************************************************************
  2. * Copyright © 2014-2015 VMware, Inc., Palo Alto, CA., USA
  3. * All Rights Reserved.
  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. size_t size;
  304. int ret;
  305. if (cmd_size != vmw_view_define_sizes[view_type] +
  306. sizeof(SVGA3dCmdHeader)) {
  307. DRM_ERROR("Illegal view create command size.\n");
  308. return -EINVAL;
  309. }
  310. if (!vmw_view_id_ok(user_key, view_type)) {
  311. DRM_ERROR("Illegal view add view id.\n");
  312. return -EINVAL;
  313. }
  314. size = offsetof(struct vmw_view, cmd) + cmd_size;
  315. ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv), size, false, true);
  316. if (ret) {
  317. if (ret != -ERESTARTSYS)
  318. DRM_ERROR("Out of graphics memory for view"
  319. " creation.\n");
  320. return ret;
  321. }
  322. view = kmalloc(size, GFP_KERNEL);
  323. if (!view) {
  324. ttm_mem_global_free(vmw_mem_glob(dev_priv), size);
  325. return -ENOMEM;
  326. }
  327. res = &view->res;
  328. view->ctx = ctx;
  329. view->srf = vmw_resource_reference(srf);
  330. view->cotable = vmw_context_cotable(ctx, vmw_view_cotables[view_type]);
  331. view->view_type = view_type;
  332. view->view_id = user_key;
  333. view->cmd_size = cmd_size;
  334. view->committed = false;
  335. INIT_LIST_HEAD(&view->srf_head);
  336. INIT_LIST_HEAD(&view->cotable_head);
  337. memcpy(&view->cmd, cmd, cmd_size);
  338. ret = vmw_resource_init(dev_priv, res, true,
  339. vmw_view_res_free, &vmw_view_func);
  340. if (ret)
  341. goto out_resource_init;
  342. ret = vmw_cmdbuf_res_add(man, vmw_cmdbuf_res_view,
  343. vmw_view_key(user_key, view_type),
  344. res, list);
  345. if (ret)
  346. goto out_resource_init;
  347. res->id = view->view_id;
  348. vmw_resource_activate(res, vmw_hw_view_destroy);
  349. out_resource_init:
  350. vmw_resource_unreference(&res);
  351. return ret;
  352. }
  353. /**
  354. * vmw_view_remove - Stage a view for removal.
  355. *
  356. * @man: Pointer to the view manager identifying the shader namespace.
  357. * @user_key: The key that is used to identify the view. The key is
  358. * unique to the view type.
  359. * @view_type: View type
  360. * @list: Caller's list of staged command buffer resource actions.
  361. * @res_p: If the resource is in an already committed state, points to the
  362. * struct vmw_resource on successful return. The pointer will be
  363. * non ref-counted.
  364. */
  365. int vmw_view_remove(struct vmw_cmdbuf_res_manager *man,
  366. u32 user_key, enum vmw_view_type view_type,
  367. struct list_head *list,
  368. struct vmw_resource **res_p)
  369. {
  370. if (!vmw_view_id_ok(user_key, view_type)) {
  371. DRM_ERROR("Illegal view remove view id.\n");
  372. return -EINVAL;
  373. }
  374. return vmw_cmdbuf_res_remove(man, vmw_cmdbuf_res_view,
  375. vmw_view_key(user_key, view_type),
  376. list, res_p);
  377. }
  378. /**
  379. * vmw_view_cotable_list_destroy - Evict all views belonging to a cotable.
  380. *
  381. * @dev_priv: Pointer to a device private struct.
  382. * @list: List of views belonging to a cotable.
  383. * @readback: Unused. Needed for function interface only.
  384. *
  385. * This function evicts all views belonging to a cotable.
  386. * It must be called with the binding_mutex held, and the caller must hold
  387. * a reference to the view resource. This is typically called before the
  388. * cotable is paged out.
  389. */
  390. void vmw_view_cotable_list_destroy(struct vmw_private *dev_priv,
  391. struct list_head *list,
  392. bool readback)
  393. {
  394. struct vmw_view *entry, *next;
  395. WARN_ON_ONCE(!mutex_is_locked(&dev_priv->binding_mutex));
  396. list_for_each_entry_safe(entry, next, list, cotable_head)
  397. WARN_ON(vmw_view_destroy(&entry->res));
  398. }
  399. /**
  400. * vmw_view_surface_list_destroy - Evict all views pointing to a surface
  401. *
  402. * @dev_priv: Pointer to a device private struct.
  403. * @list: List of views pointing to a surface.
  404. *
  405. * This function evicts all views pointing to a surface. This is typically
  406. * called before the surface is evicted.
  407. */
  408. void vmw_view_surface_list_destroy(struct vmw_private *dev_priv,
  409. struct list_head *list)
  410. {
  411. struct vmw_view *entry, *next;
  412. WARN_ON_ONCE(!mutex_is_locked(&dev_priv->binding_mutex));
  413. list_for_each_entry_safe(entry, next, list, srf_head)
  414. WARN_ON(vmw_view_destroy(&entry->res));
  415. }
  416. /**
  417. * vmw_view_srf - Return a non-refcounted pointer to the surface a view is
  418. * pointing to.
  419. *
  420. * @res: pointer to a view resource.
  421. *
  422. * Note that the view itself is holding a reference, so as long
  423. * the view resource is alive, the surface resource will be.
  424. */
  425. struct vmw_resource *vmw_view_srf(struct vmw_resource *res)
  426. {
  427. return vmw_view(res)->srf;
  428. }
  429. /**
  430. * vmw_view_lookup - Look up a view.
  431. *
  432. * @man: The context's cmdbuf ref manager.
  433. * @view_type: The view type.
  434. * @user_key: The view user id.
  435. *
  436. * returns a refcounted pointer to a view or an error pointer if not found.
  437. */
  438. struct vmw_resource *vmw_view_lookup(struct vmw_cmdbuf_res_manager *man,
  439. enum vmw_view_type view_type,
  440. u32 user_key)
  441. {
  442. return vmw_cmdbuf_res_lookup(man, vmw_cmdbuf_res_view,
  443. vmw_view_key(user_key, view_type));
  444. }
  445. const u32 vmw_view_destroy_cmds[] = {
  446. [vmw_view_sr] = SVGA_3D_CMD_DX_DESTROY_SHADERRESOURCE_VIEW,
  447. [vmw_view_rt] = SVGA_3D_CMD_DX_DESTROY_RENDERTARGET_VIEW,
  448. [vmw_view_ds] = SVGA_3D_CMD_DX_DESTROY_DEPTHSTENCIL_VIEW,
  449. };
  450. const SVGACOTableType vmw_view_cotables[] = {
  451. [vmw_view_sr] = SVGA_COTABLE_SRVIEW,
  452. [vmw_view_rt] = SVGA_COTABLE_RTVIEW,
  453. [vmw_view_ds] = SVGA_COTABLE_DSVIEW,
  454. };
  455. const SVGACOTableType vmw_so_cotables[] = {
  456. [vmw_so_el] = SVGA_COTABLE_ELEMENTLAYOUT,
  457. [vmw_so_bs] = SVGA_COTABLE_BLENDSTATE,
  458. [vmw_so_ds] = SVGA_COTABLE_DEPTHSTENCIL,
  459. [vmw_so_rs] = SVGA_COTABLE_RASTERIZERSTATE,
  460. [vmw_so_ss] = SVGA_COTABLE_SAMPLER,
  461. [vmw_so_so] = SVGA_COTABLE_STREAMOUTPUT
  462. };
  463. /* To remove unused function warning */
  464. static void vmw_so_build_asserts(void) __attribute__((used));
  465. /*
  466. * This function is unused at run-time, and only used to dump various build
  467. * asserts important for code optimization assumptions.
  468. */
  469. static void vmw_so_build_asserts(void)
  470. {
  471. /* Assert that our vmw_view_cmd_to_type() function is correct. */
  472. BUILD_BUG_ON(SVGA_3D_CMD_DX_DESTROY_SHADERRESOURCE_VIEW !=
  473. SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 1);
  474. BUILD_BUG_ON(SVGA_3D_CMD_DX_DEFINE_RENDERTARGET_VIEW !=
  475. SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 2);
  476. BUILD_BUG_ON(SVGA_3D_CMD_DX_DESTROY_RENDERTARGET_VIEW !=
  477. SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 3);
  478. BUILD_BUG_ON(SVGA_3D_CMD_DX_DEFINE_DEPTHSTENCIL_VIEW !=
  479. SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 4);
  480. BUILD_BUG_ON(SVGA_3D_CMD_DX_DESTROY_DEPTHSTENCIL_VIEW !=
  481. SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 5);
  482. /* Assert that our "one body fits all" assumption is valid */
  483. BUILD_BUG_ON(sizeof(union vmw_view_destroy) != sizeof(u32));
  484. /* Assert that the view key space can hold all view ids. */
  485. BUILD_BUG_ON(SVGA_COTABLE_MAX_IDS >= ((1 << 20) - 1));
  486. /*
  487. * Assert that the offset of sid in all view define commands
  488. * is what we assume it to be.
  489. */
  490. BUILD_BUG_ON(offsetof(struct vmw_view_define, sid) !=
  491. offsetof(SVGA3dCmdDXDefineShaderResourceView, sid));
  492. BUILD_BUG_ON(offsetof(struct vmw_view_define, sid) !=
  493. offsetof(SVGA3dCmdDXDefineRenderTargetView, sid));
  494. BUILD_BUG_ON(offsetof(struct vmw_view_define, sid) !=
  495. offsetof(SVGA3dCmdDXDefineDepthStencilView, sid));
  496. }