vmwgfx_context.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /**************************************************************************
  3. *
  4. * Copyright 2009-2023 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. #include <drm/ttm/ttm_placement.h>
  28. #include "vmwgfx_binding.h"
  29. #include "vmwgfx_bo.h"
  30. #include "vmwgfx_drv.h"
  31. #include "vmwgfx_resource_priv.h"
  32. struct vmw_user_context {
  33. struct ttm_base_object base;
  34. struct vmw_resource res;
  35. struct vmw_ctx_binding_state *cbs;
  36. struct vmw_cmdbuf_res_manager *man;
  37. struct vmw_resource *cotables[SVGA_COTABLE_MAX];
  38. spinlock_t cotable_lock;
  39. struct vmw_bo *dx_query_mob;
  40. };
  41. static void vmw_user_context_free(struct vmw_resource *res);
  42. static struct vmw_resource *
  43. vmw_user_context_base_to_res(struct ttm_base_object *base);
  44. static int vmw_gb_context_create(struct vmw_resource *res);
  45. static int vmw_gb_context_bind(struct vmw_resource *res,
  46. struct ttm_validate_buffer *val_buf);
  47. static int vmw_gb_context_unbind(struct vmw_resource *res,
  48. bool readback,
  49. struct ttm_validate_buffer *val_buf);
  50. static int vmw_gb_context_destroy(struct vmw_resource *res);
  51. static int vmw_dx_context_create(struct vmw_resource *res);
  52. static int vmw_dx_context_bind(struct vmw_resource *res,
  53. struct ttm_validate_buffer *val_buf);
  54. static int vmw_dx_context_unbind(struct vmw_resource *res,
  55. bool readback,
  56. struct ttm_validate_buffer *val_buf);
  57. static int vmw_dx_context_destroy(struct vmw_resource *res);
  58. static const struct vmw_user_resource_conv user_context_conv = {
  59. .object_type = VMW_RES_CONTEXT,
  60. .base_obj_to_res = vmw_user_context_base_to_res,
  61. .res_free = vmw_user_context_free
  62. };
  63. const struct vmw_user_resource_conv *user_context_converter =
  64. &user_context_conv;
  65. static const struct vmw_res_func vmw_legacy_context_func = {
  66. .res_type = vmw_res_context,
  67. .needs_guest_memory = false,
  68. .may_evict = false,
  69. .type_name = "legacy contexts",
  70. .domain = VMW_BO_DOMAIN_SYS,
  71. .busy_domain = VMW_BO_DOMAIN_SYS,
  72. .create = NULL,
  73. .destroy = NULL,
  74. .bind = NULL,
  75. .unbind = NULL
  76. };
  77. static const struct vmw_res_func vmw_gb_context_func = {
  78. .res_type = vmw_res_context,
  79. .needs_guest_memory = true,
  80. .may_evict = true,
  81. .prio = 3,
  82. .dirty_prio = 3,
  83. .type_name = "guest backed contexts",
  84. .domain = VMW_BO_DOMAIN_MOB,
  85. .busy_domain = VMW_BO_DOMAIN_MOB,
  86. .create = vmw_gb_context_create,
  87. .destroy = vmw_gb_context_destroy,
  88. .bind = vmw_gb_context_bind,
  89. .unbind = vmw_gb_context_unbind
  90. };
  91. static const struct vmw_res_func vmw_dx_context_func = {
  92. .res_type = vmw_res_dx_context,
  93. .needs_guest_memory = true,
  94. .may_evict = true,
  95. .prio = 3,
  96. .dirty_prio = 3,
  97. .type_name = "dx contexts",
  98. .domain = VMW_BO_DOMAIN_MOB,
  99. .busy_domain = VMW_BO_DOMAIN_MOB,
  100. .create = vmw_dx_context_create,
  101. .destroy = vmw_dx_context_destroy,
  102. .bind = vmw_dx_context_bind,
  103. .unbind = vmw_dx_context_unbind
  104. };
  105. /*
  106. * Context management:
  107. */
  108. static void vmw_context_cotables_unref(struct vmw_private *dev_priv,
  109. struct vmw_user_context *uctx)
  110. {
  111. struct vmw_resource *res;
  112. int i;
  113. u32 cotable_max = has_sm5_context(dev_priv) ?
  114. SVGA_COTABLE_MAX : SVGA_COTABLE_DX10_MAX;
  115. for (i = 0; i < cotable_max; ++i) {
  116. spin_lock(&uctx->cotable_lock);
  117. res = uctx->cotables[i];
  118. uctx->cotables[i] = NULL;
  119. spin_unlock(&uctx->cotable_lock);
  120. if (res)
  121. vmw_resource_unreference(&res);
  122. }
  123. }
  124. static void vmw_hw_context_destroy(struct vmw_resource *res)
  125. {
  126. struct vmw_user_context *uctx =
  127. container_of(res, struct vmw_user_context, res);
  128. struct vmw_private *dev_priv = res->dev_priv;
  129. struct {
  130. SVGA3dCmdHeader header;
  131. SVGA3dCmdDestroyContext body;
  132. } *cmd;
  133. if (res->func->destroy == vmw_gb_context_destroy ||
  134. res->func->destroy == vmw_dx_context_destroy) {
  135. mutex_lock(&dev_priv->cmdbuf_mutex);
  136. vmw_cmdbuf_res_man_destroy(uctx->man);
  137. mutex_lock(&dev_priv->binding_mutex);
  138. vmw_binding_state_kill(uctx->cbs);
  139. (void) res->func->destroy(res);
  140. mutex_unlock(&dev_priv->binding_mutex);
  141. if (dev_priv->pinned_bo != NULL &&
  142. !dev_priv->query_cid_valid)
  143. __vmw_execbuf_release_pinned_bo(dev_priv, NULL);
  144. mutex_unlock(&dev_priv->cmdbuf_mutex);
  145. vmw_context_cotables_unref(dev_priv, uctx);
  146. return;
  147. }
  148. vmw_execbuf_release_pinned_bo(dev_priv);
  149. cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
  150. if (unlikely(cmd == NULL))
  151. return;
  152. cmd->header.id = SVGA_3D_CMD_CONTEXT_DESTROY;
  153. cmd->header.size = sizeof(cmd->body);
  154. cmd->body.cid = res->id;
  155. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  156. vmw_fifo_resource_dec(dev_priv);
  157. }
  158. static int vmw_gb_context_init(struct vmw_private *dev_priv,
  159. bool dx,
  160. struct vmw_resource *res,
  161. void (*res_free)(struct vmw_resource *res))
  162. {
  163. int ret, i;
  164. struct vmw_user_context *uctx =
  165. container_of(res, struct vmw_user_context, res);
  166. res->guest_memory_size = (dx ? sizeof(SVGADXContextMobFormat) :
  167. sizeof(SVGAGBContextData));
  168. ret = vmw_resource_init(dev_priv, res, true,
  169. res_free,
  170. dx ? &vmw_dx_context_func :
  171. &vmw_gb_context_func);
  172. if (unlikely(ret != 0))
  173. goto out_err;
  174. if (dev_priv->has_mob) {
  175. uctx->man = vmw_cmdbuf_res_man_create(dev_priv);
  176. if (IS_ERR(uctx->man)) {
  177. ret = PTR_ERR(uctx->man);
  178. uctx->man = NULL;
  179. goto out_err;
  180. }
  181. }
  182. uctx->cbs = vmw_binding_state_alloc(dev_priv);
  183. if (IS_ERR(uctx->cbs)) {
  184. ret = PTR_ERR(uctx->cbs);
  185. goto out_err;
  186. }
  187. spin_lock_init(&uctx->cotable_lock);
  188. if (dx) {
  189. u32 cotable_max = has_sm5_context(dev_priv) ?
  190. SVGA_COTABLE_MAX : SVGA_COTABLE_DX10_MAX;
  191. for (i = 0; i < cotable_max; ++i) {
  192. uctx->cotables[i] = vmw_cotable_alloc(dev_priv,
  193. &uctx->res, i);
  194. if (IS_ERR(uctx->cotables[i])) {
  195. ret = PTR_ERR(uctx->cotables[i]);
  196. goto out_cotables;
  197. }
  198. }
  199. }
  200. res->hw_destroy = vmw_hw_context_destroy;
  201. return 0;
  202. out_cotables:
  203. vmw_context_cotables_unref(dev_priv, uctx);
  204. out_err:
  205. if (res_free)
  206. res_free(res);
  207. else
  208. kfree(res);
  209. return ret;
  210. }
  211. static int vmw_context_init(struct vmw_private *dev_priv,
  212. struct vmw_resource *res,
  213. void (*res_free)(struct vmw_resource *res),
  214. bool dx)
  215. {
  216. int ret;
  217. struct {
  218. SVGA3dCmdHeader header;
  219. SVGA3dCmdDefineContext body;
  220. } *cmd;
  221. if (dev_priv->has_mob)
  222. return vmw_gb_context_init(dev_priv, dx, res, res_free);
  223. ret = vmw_resource_init(dev_priv, res, false,
  224. res_free, &vmw_legacy_context_func);
  225. if (unlikely(ret != 0)) {
  226. DRM_ERROR("Failed to allocate a resource id.\n");
  227. goto out_early;
  228. }
  229. if (unlikely(res->id >= SVGA3D_HB_MAX_CONTEXT_IDS)) {
  230. DRM_ERROR("Out of hw context ids.\n");
  231. vmw_resource_unreference(&res);
  232. return -ENOMEM;
  233. }
  234. cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
  235. if (unlikely(cmd == NULL)) {
  236. vmw_resource_unreference(&res);
  237. return -ENOMEM;
  238. }
  239. cmd->header.id = SVGA_3D_CMD_CONTEXT_DEFINE;
  240. cmd->header.size = sizeof(cmd->body);
  241. cmd->body.cid = res->id;
  242. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  243. vmw_fifo_resource_inc(dev_priv);
  244. res->hw_destroy = vmw_hw_context_destroy;
  245. return 0;
  246. out_early:
  247. if (res_free == NULL)
  248. kfree(res);
  249. else
  250. res_free(res);
  251. return ret;
  252. }
  253. /*
  254. * GB context.
  255. */
  256. static int vmw_gb_context_create(struct vmw_resource *res)
  257. {
  258. struct vmw_private *dev_priv = res->dev_priv;
  259. int ret;
  260. struct {
  261. SVGA3dCmdHeader header;
  262. SVGA3dCmdDefineGBContext body;
  263. } *cmd;
  264. if (likely(res->id != -1))
  265. return 0;
  266. ret = vmw_resource_alloc_id(res);
  267. if (unlikely(ret != 0)) {
  268. DRM_ERROR("Failed to allocate a context id.\n");
  269. goto out_no_id;
  270. }
  271. if (unlikely(res->id >= VMWGFX_NUM_GB_CONTEXT)) {
  272. ret = -EBUSY;
  273. goto out_no_fifo;
  274. }
  275. cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
  276. if (unlikely(cmd == NULL)) {
  277. ret = -ENOMEM;
  278. goto out_no_fifo;
  279. }
  280. cmd->header.id = SVGA_3D_CMD_DEFINE_GB_CONTEXT;
  281. cmd->header.size = sizeof(cmd->body);
  282. cmd->body.cid = res->id;
  283. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  284. vmw_fifo_resource_inc(dev_priv);
  285. return 0;
  286. out_no_fifo:
  287. vmw_resource_release_id(res);
  288. out_no_id:
  289. return ret;
  290. }
  291. static int vmw_gb_context_bind(struct vmw_resource *res,
  292. struct ttm_validate_buffer *val_buf)
  293. {
  294. struct vmw_private *dev_priv = res->dev_priv;
  295. struct {
  296. SVGA3dCmdHeader header;
  297. SVGA3dCmdBindGBContext body;
  298. } *cmd;
  299. struct ttm_buffer_object *bo = val_buf->bo;
  300. BUG_ON(bo->resource->mem_type != VMW_PL_MOB);
  301. cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
  302. if (unlikely(cmd == NULL))
  303. return -ENOMEM;
  304. cmd->header.id = SVGA_3D_CMD_BIND_GB_CONTEXT;
  305. cmd->header.size = sizeof(cmd->body);
  306. cmd->body.cid = res->id;
  307. cmd->body.mobid = bo->resource->start;
  308. cmd->body.validContents = res->guest_memory_dirty;
  309. res->guest_memory_dirty = false;
  310. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  311. return 0;
  312. }
  313. static int vmw_gb_context_unbind(struct vmw_resource *res,
  314. bool readback,
  315. struct ttm_validate_buffer *val_buf)
  316. {
  317. struct vmw_private *dev_priv = res->dev_priv;
  318. struct ttm_buffer_object *bo = val_buf->bo;
  319. struct vmw_fence_obj *fence;
  320. struct vmw_user_context *uctx =
  321. container_of(res, struct vmw_user_context, res);
  322. struct {
  323. SVGA3dCmdHeader header;
  324. SVGA3dCmdReadbackGBContext body;
  325. } *cmd1;
  326. struct {
  327. SVGA3dCmdHeader header;
  328. SVGA3dCmdBindGBContext body;
  329. } *cmd2;
  330. uint32_t submit_size;
  331. uint8_t *cmd;
  332. BUG_ON(bo->resource->mem_type != VMW_PL_MOB);
  333. mutex_lock(&dev_priv->binding_mutex);
  334. vmw_binding_state_scrub(uctx->cbs);
  335. submit_size = sizeof(*cmd2) + (readback ? sizeof(*cmd1) : 0);
  336. cmd = VMW_CMD_RESERVE(dev_priv, submit_size);
  337. if (unlikely(cmd == NULL)) {
  338. mutex_unlock(&dev_priv->binding_mutex);
  339. return -ENOMEM;
  340. }
  341. cmd2 = (void *) cmd;
  342. if (readback) {
  343. cmd1 = (void *) cmd;
  344. cmd1->header.id = SVGA_3D_CMD_READBACK_GB_CONTEXT;
  345. cmd1->header.size = sizeof(cmd1->body);
  346. cmd1->body.cid = res->id;
  347. cmd2 = (void *) (&cmd1[1]);
  348. }
  349. cmd2->header.id = SVGA_3D_CMD_BIND_GB_CONTEXT;
  350. cmd2->header.size = sizeof(cmd2->body);
  351. cmd2->body.cid = res->id;
  352. cmd2->body.mobid = SVGA3D_INVALID_ID;
  353. vmw_cmd_commit(dev_priv, submit_size);
  354. mutex_unlock(&dev_priv->binding_mutex);
  355. /*
  356. * Create a fence object and fence the backup buffer.
  357. */
  358. (void) vmw_execbuf_fence_commands(NULL, dev_priv,
  359. &fence, NULL);
  360. vmw_bo_fence_single(bo, fence);
  361. if (likely(fence != NULL))
  362. vmw_fence_obj_unreference(&fence);
  363. return 0;
  364. }
  365. static int vmw_gb_context_destroy(struct vmw_resource *res)
  366. {
  367. struct vmw_private *dev_priv = res->dev_priv;
  368. struct {
  369. SVGA3dCmdHeader header;
  370. SVGA3dCmdDestroyGBContext body;
  371. } *cmd;
  372. if (likely(res->id == -1))
  373. return 0;
  374. cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
  375. if (unlikely(cmd == NULL))
  376. return -ENOMEM;
  377. cmd->header.id = SVGA_3D_CMD_DESTROY_GB_CONTEXT;
  378. cmd->header.size = sizeof(cmd->body);
  379. cmd->body.cid = res->id;
  380. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  381. if (dev_priv->query_cid == res->id)
  382. dev_priv->query_cid_valid = false;
  383. vmw_resource_release_id(res);
  384. vmw_fifo_resource_dec(dev_priv);
  385. return 0;
  386. }
  387. /*
  388. * DX context.
  389. */
  390. static int vmw_dx_context_create(struct vmw_resource *res)
  391. {
  392. struct vmw_private *dev_priv = res->dev_priv;
  393. int ret;
  394. struct {
  395. SVGA3dCmdHeader header;
  396. SVGA3dCmdDXDefineContext body;
  397. } *cmd;
  398. if (likely(res->id != -1))
  399. return 0;
  400. ret = vmw_resource_alloc_id(res);
  401. if (unlikely(ret != 0)) {
  402. DRM_ERROR("Failed to allocate a context id.\n");
  403. goto out_no_id;
  404. }
  405. if (unlikely(res->id >= VMWGFX_NUM_DXCONTEXT)) {
  406. ret = -EBUSY;
  407. goto out_no_fifo;
  408. }
  409. cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
  410. if (unlikely(cmd == NULL)) {
  411. ret = -ENOMEM;
  412. goto out_no_fifo;
  413. }
  414. cmd->header.id = SVGA_3D_CMD_DX_DEFINE_CONTEXT;
  415. cmd->header.size = sizeof(cmd->body);
  416. cmd->body.cid = res->id;
  417. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  418. vmw_fifo_resource_inc(dev_priv);
  419. return 0;
  420. out_no_fifo:
  421. vmw_resource_release_id(res);
  422. out_no_id:
  423. return ret;
  424. }
  425. static int vmw_dx_context_bind(struct vmw_resource *res,
  426. struct ttm_validate_buffer *val_buf)
  427. {
  428. struct vmw_private *dev_priv = res->dev_priv;
  429. struct {
  430. SVGA3dCmdHeader header;
  431. SVGA3dCmdDXBindContext body;
  432. } *cmd;
  433. struct ttm_buffer_object *bo = val_buf->bo;
  434. BUG_ON(bo->resource->mem_type != VMW_PL_MOB);
  435. cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
  436. if (unlikely(cmd == NULL))
  437. return -ENOMEM;
  438. cmd->header.id = SVGA_3D_CMD_DX_BIND_CONTEXT;
  439. cmd->header.size = sizeof(cmd->body);
  440. cmd->body.cid = res->id;
  441. cmd->body.mobid = bo->resource->start;
  442. cmd->body.validContents = res->guest_memory_dirty;
  443. res->guest_memory_dirty = false;
  444. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  445. return 0;
  446. }
  447. /**
  448. * vmw_dx_context_scrub_cotables - Scrub all bindings and
  449. * cotables from a context
  450. *
  451. * @ctx: Pointer to the context resource
  452. * @readback: Whether to save the otable contents on scrubbing.
  453. *
  454. * COtables must be unbound before their context, but unbinding requires
  455. * the backup buffer being reserved, whereas scrubbing does not.
  456. * This function scrubs all cotables of a context, potentially reading back
  457. * the contents into their backup buffers. However, scrubbing cotables
  458. * also makes the device context invalid, so scrub all bindings first so
  459. * that doesn't have to be done later with an invalid context.
  460. */
  461. void vmw_dx_context_scrub_cotables(struct vmw_resource *ctx,
  462. bool readback)
  463. {
  464. struct vmw_user_context *uctx =
  465. container_of(ctx, struct vmw_user_context, res);
  466. u32 cotable_max = has_sm5_context(ctx->dev_priv) ?
  467. SVGA_COTABLE_MAX : SVGA_COTABLE_DX10_MAX;
  468. int i;
  469. vmw_binding_state_scrub(uctx->cbs);
  470. for (i = 0; i < cotable_max; ++i) {
  471. struct vmw_resource *res;
  472. /* Avoid racing with ongoing cotable destruction. */
  473. spin_lock(&uctx->cotable_lock);
  474. res = uctx->cotables[vmw_cotable_scrub_order[i]];
  475. if (res)
  476. res = vmw_resource_reference_unless_doomed(res);
  477. spin_unlock(&uctx->cotable_lock);
  478. if (!res)
  479. continue;
  480. WARN_ON(vmw_cotable_scrub(res, readback));
  481. vmw_resource_unreference(&res);
  482. }
  483. }
  484. static int vmw_dx_context_unbind(struct vmw_resource *res,
  485. bool readback,
  486. struct ttm_validate_buffer *val_buf)
  487. {
  488. struct vmw_private *dev_priv = res->dev_priv;
  489. struct ttm_buffer_object *bo = val_buf->bo;
  490. struct vmw_fence_obj *fence;
  491. struct vmw_user_context *uctx =
  492. container_of(res, struct vmw_user_context, res);
  493. struct {
  494. SVGA3dCmdHeader header;
  495. SVGA3dCmdDXReadbackContext body;
  496. } *cmd1;
  497. struct {
  498. SVGA3dCmdHeader header;
  499. SVGA3dCmdDXBindContext body;
  500. } *cmd2;
  501. uint32_t submit_size;
  502. uint8_t *cmd;
  503. BUG_ON(bo->resource->mem_type != VMW_PL_MOB);
  504. mutex_lock(&dev_priv->binding_mutex);
  505. vmw_dx_context_scrub_cotables(res, readback);
  506. if (uctx->dx_query_mob && uctx->dx_query_mob->dx_query_ctx &&
  507. readback) {
  508. WARN_ON(uctx->dx_query_mob->dx_query_ctx != res);
  509. if (vmw_query_readback_all(uctx->dx_query_mob))
  510. DRM_ERROR("Failed to read back query states\n");
  511. }
  512. submit_size = sizeof(*cmd2) + (readback ? sizeof(*cmd1) : 0);
  513. cmd = VMW_CMD_RESERVE(dev_priv, submit_size);
  514. if (unlikely(cmd == NULL)) {
  515. mutex_unlock(&dev_priv->binding_mutex);
  516. return -ENOMEM;
  517. }
  518. cmd2 = (void *) cmd;
  519. if (readback) {
  520. cmd1 = (void *) cmd;
  521. cmd1->header.id = SVGA_3D_CMD_DX_READBACK_CONTEXT;
  522. cmd1->header.size = sizeof(cmd1->body);
  523. cmd1->body.cid = res->id;
  524. cmd2 = (void *) (&cmd1[1]);
  525. }
  526. cmd2->header.id = SVGA_3D_CMD_DX_BIND_CONTEXT;
  527. cmd2->header.size = sizeof(cmd2->body);
  528. cmd2->body.cid = res->id;
  529. cmd2->body.mobid = SVGA3D_INVALID_ID;
  530. vmw_cmd_commit(dev_priv, submit_size);
  531. mutex_unlock(&dev_priv->binding_mutex);
  532. /*
  533. * Create a fence object and fence the backup buffer.
  534. */
  535. (void) vmw_execbuf_fence_commands(NULL, dev_priv,
  536. &fence, NULL);
  537. vmw_bo_fence_single(bo, fence);
  538. if (likely(fence != NULL))
  539. vmw_fence_obj_unreference(&fence);
  540. return 0;
  541. }
  542. static int vmw_dx_context_destroy(struct vmw_resource *res)
  543. {
  544. struct vmw_private *dev_priv = res->dev_priv;
  545. struct {
  546. SVGA3dCmdHeader header;
  547. SVGA3dCmdDXDestroyContext body;
  548. } *cmd;
  549. if (likely(res->id == -1))
  550. return 0;
  551. cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
  552. if (unlikely(cmd == NULL))
  553. return -ENOMEM;
  554. cmd->header.id = SVGA_3D_CMD_DX_DESTROY_CONTEXT;
  555. cmd->header.size = sizeof(cmd->body);
  556. cmd->body.cid = res->id;
  557. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  558. if (dev_priv->query_cid == res->id)
  559. dev_priv->query_cid_valid = false;
  560. vmw_resource_release_id(res);
  561. vmw_fifo_resource_dec(dev_priv);
  562. return 0;
  563. }
  564. /*
  565. * User-space context management:
  566. */
  567. static struct vmw_resource *
  568. vmw_user_context_base_to_res(struct ttm_base_object *base)
  569. {
  570. return &(container_of(base, struct vmw_user_context, base)->res);
  571. }
  572. static void vmw_user_context_free(struct vmw_resource *res)
  573. {
  574. struct vmw_user_context *ctx =
  575. container_of(res, struct vmw_user_context, res);
  576. if (ctx->cbs)
  577. vmw_binding_state_free(ctx->cbs);
  578. (void) vmw_context_bind_dx_query(res, NULL);
  579. ttm_base_object_kfree(ctx, base);
  580. }
  581. /*
  582. * This function is called when user space has no more references on the
  583. * base object. It releases the base-object's reference on the resource object.
  584. */
  585. static void vmw_user_context_base_release(struct ttm_base_object **p_base)
  586. {
  587. struct ttm_base_object *base = *p_base;
  588. struct vmw_user_context *ctx =
  589. container_of(base, struct vmw_user_context, base);
  590. struct vmw_resource *res = &ctx->res;
  591. *p_base = NULL;
  592. vmw_resource_unreference(&res);
  593. }
  594. int vmw_context_destroy_ioctl(struct drm_device *dev, void *data,
  595. struct drm_file *file_priv)
  596. {
  597. struct drm_vmw_context_arg *arg = (struct drm_vmw_context_arg *)data;
  598. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  599. return ttm_ref_object_base_unref(tfile, arg->cid);
  600. }
  601. static int vmw_context_define(struct drm_device *dev, void *data,
  602. struct drm_file *file_priv, bool dx)
  603. {
  604. struct vmw_private *dev_priv = vmw_priv(dev);
  605. struct vmw_user_context *ctx;
  606. struct vmw_resource *res;
  607. struct vmw_resource *tmp;
  608. struct drm_vmw_context_arg *arg = (struct drm_vmw_context_arg *)data;
  609. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  610. int ret;
  611. if (!has_sm4_context(dev_priv) && dx) {
  612. VMW_DEBUG_USER("DX contexts not supported by device.\n");
  613. return -EINVAL;
  614. }
  615. ctx = kzalloc_obj(*ctx);
  616. if (unlikely(!ctx)) {
  617. ret = -ENOMEM;
  618. goto out_ret;
  619. }
  620. res = &ctx->res;
  621. ctx->base.shareable = false;
  622. ctx->base.tfile = NULL;
  623. /*
  624. * From here on, the destructor takes over resource freeing.
  625. */
  626. ret = vmw_context_init(dev_priv, res, vmw_user_context_free, dx);
  627. if (unlikely(ret != 0))
  628. goto out_ret;
  629. tmp = vmw_resource_reference(&ctx->res);
  630. ret = ttm_base_object_init(tfile, &ctx->base, false, VMW_RES_CONTEXT,
  631. &vmw_user_context_base_release);
  632. if (unlikely(ret != 0)) {
  633. vmw_resource_unreference(&tmp);
  634. goto out_err;
  635. }
  636. arg->cid = ctx->base.handle;
  637. out_err:
  638. vmw_resource_unreference(&res);
  639. out_ret:
  640. return ret;
  641. }
  642. int vmw_context_define_ioctl(struct drm_device *dev, void *data,
  643. struct drm_file *file_priv)
  644. {
  645. return vmw_context_define(dev, data, file_priv, false);
  646. }
  647. int vmw_extended_context_define_ioctl(struct drm_device *dev, void *data,
  648. struct drm_file *file_priv)
  649. {
  650. union drm_vmw_extended_context_arg *arg = (typeof(arg)) data;
  651. struct drm_vmw_context_arg *rep = &arg->rep;
  652. switch (arg->req) {
  653. case drm_vmw_context_legacy:
  654. return vmw_context_define(dev, rep, file_priv, false);
  655. case drm_vmw_context_dx:
  656. return vmw_context_define(dev, rep, file_priv, true);
  657. default:
  658. break;
  659. }
  660. return -EINVAL;
  661. }
  662. /**
  663. * vmw_context_binding_list - Return a list of context bindings
  664. *
  665. * @ctx: The context resource
  666. *
  667. * Returns the current list of bindings of the given context. Note that
  668. * this list becomes stale as soon as the dev_priv::binding_mutex is unlocked.
  669. */
  670. struct list_head *vmw_context_binding_list(struct vmw_resource *ctx)
  671. {
  672. struct vmw_user_context *uctx =
  673. container_of(ctx, struct vmw_user_context, res);
  674. return vmw_binding_state_list(uctx->cbs);
  675. }
  676. struct vmw_cmdbuf_res_manager *vmw_context_res_man(struct vmw_resource *ctx)
  677. {
  678. return container_of(ctx, struct vmw_user_context, res)->man;
  679. }
  680. struct vmw_resource *vmw_context_cotable(struct vmw_resource *ctx,
  681. SVGACOTableType cotable_type)
  682. {
  683. u32 cotable_max = has_sm5_context(ctx->dev_priv) ?
  684. SVGA_COTABLE_MAX : SVGA_COTABLE_DX10_MAX;
  685. if (cotable_type >= cotable_max)
  686. return ERR_PTR(-EINVAL);
  687. return container_of(ctx, struct vmw_user_context, res)->
  688. cotables[cotable_type];
  689. }
  690. /**
  691. * vmw_context_binding_state -
  692. * Return a pointer to a context binding state structure
  693. *
  694. * @ctx: The context resource
  695. *
  696. * Returns the current state of bindings of the given context. Note that
  697. * this state becomes stale as soon as the dev_priv::binding_mutex is unlocked.
  698. */
  699. struct vmw_ctx_binding_state *
  700. vmw_context_binding_state(struct vmw_resource *ctx)
  701. {
  702. return container_of(ctx, struct vmw_user_context, res)->cbs;
  703. }
  704. /**
  705. * vmw_context_bind_dx_query -
  706. * Sets query MOB for the context. If @mob is NULL, then this function will
  707. * remove the association between the MOB and the context. This function
  708. * assumes the binding_mutex is held.
  709. *
  710. * @ctx_res: The context resource
  711. * @mob: a reference to the query MOB
  712. *
  713. * Returns -EINVAL if a MOB has already been set and does not match the one
  714. * specified in the parameter. 0 otherwise.
  715. */
  716. int vmw_context_bind_dx_query(struct vmw_resource *ctx_res,
  717. struct vmw_bo *mob)
  718. {
  719. struct vmw_user_context *uctx =
  720. container_of(ctx_res, struct vmw_user_context, res);
  721. if (mob == NULL) {
  722. if (uctx->dx_query_mob) {
  723. uctx->dx_query_mob->dx_query_ctx = NULL;
  724. vmw_bo_unreference(&uctx->dx_query_mob);
  725. uctx->dx_query_mob = NULL;
  726. }
  727. return 0;
  728. }
  729. /* Can only have one MOB per context for queries */
  730. if (uctx->dx_query_mob && uctx->dx_query_mob != mob)
  731. return -EINVAL;
  732. mob->dx_query_ctx = ctx_res;
  733. if (!uctx->dx_query_mob)
  734. uctx->dx_query_mob = vmw_bo_reference(mob);
  735. return 0;
  736. }
  737. /**
  738. * vmw_context_get_dx_query_mob - Returns non-counted reference to DX query mob
  739. *
  740. * @ctx_res: The context resource
  741. */
  742. struct vmw_bo *
  743. vmw_context_get_dx_query_mob(struct vmw_resource *ctx_res)
  744. {
  745. struct vmw_user_context *uctx =
  746. container_of(ctx_res, struct vmw_user_context, res);
  747. return uctx->dx_query_mob;
  748. }