vmwgfx_cotable.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /**************************************************************************
  3. *
  4. * Copyright 2014-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. * Treat context OTables as resources to make use of the resource
  29. * backing MOB eviction mechanism, that is used to read back the COTable
  30. * whenever the backing MOB is evicted.
  31. */
  32. #include <drm/ttm/ttm_placement.h>
  33. #include "vmwgfx_drv.h"
  34. #include "vmwgfx_resource_priv.h"
  35. #include "vmwgfx_so.h"
  36. /**
  37. * struct vmw_cotable - Context Object Table resource
  38. *
  39. * @res: struct vmw_resource we are deriving from.
  40. * @ctx: non-refcounted pointer to the owning context.
  41. * @size_read_back: Size of data read back during eviction.
  42. * @seen_entries: Seen entries in command stream for this cotable.
  43. * @type: The cotable type.
  44. * @scrubbed: Whether the cotable has been scrubbed.
  45. * @resource_list: List of resources in the cotable.
  46. */
  47. struct vmw_cotable {
  48. struct vmw_resource res;
  49. struct vmw_resource *ctx;
  50. size_t size_read_back;
  51. int seen_entries;
  52. u32 type;
  53. bool scrubbed;
  54. struct list_head resource_list;
  55. };
  56. /**
  57. * struct vmw_cotable_info - Static info about cotable types
  58. *
  59. * @min_initial_entries: Min number of initial intries at cotable allocation
  60. * for this cotable type.
  61. * @size: Size of each entry.
  62. * @unbind_func: Unbind call-back function.
  63. */
  64. struct vmw_cotable_info {
  65. u32 min_initial_entries;
  66. u32 size;
  67. void (*unbind_func)(struct vmw_private *, struct list_head *,
  68. bool);
  69. };
  70. static const struct vmw_cotable_info co_info[] = {
  71. {1, sizeof(SVGACOTableDXRTViewEntry), &vmw_view_cotable_list_destroy},
  72. {1, sizeof(SVGACOTableDXDSViewEntry), &vmw_view_cotable_list_destroy},
  73. {1, sizeof(SVGACOTableDXSRViewEntry), &vmw_view_cotable_list_destroy},
  74. {1, sizeof(SVGACOTableDXElementLayoutEntry), NULL},
  75. {1, sizeof(SVGACOTableDXBlendStateEntry), NULL},
  76. {1, sizeof(SVGACOTableDXDepthStencilEntry), NULL},
  77. {1, sizeof(SVGACOTableDXRasterizerStateEntry), NULL},
  78. {1, sizeof(SVGACOTableDXSamplerEntry), NULL},
  79. {1, sizeof(SVGACOTableDXStreamOutputEntry), &vmw_dx_streamoutput_cotable_list_scrub},
  80. {1, sizeof(SVGACOTableDXQueryEntry), NULL},
  81. {1, sizeof(SVGACOTableDXShaderEntry), &vmw_dx_shader_cotable_list_scrub},
  82. {1, sizeof(SVGACOTableDXUAViewEntry), &vmw_view_cotable_list_destroy}
  83. };
  84. /*
  85. * Cotables with bindings that we remove must be scrubbed first,
  86. * otherwise, the device will swap in an invalid context when we remove
  87. * bindings before scrubbing a cotable...
  88. */
  89. const SVGACOTableType vmw_cotable_scrub_order[] = {
  90. SVGA_COTABLE_RTVIEW,
  91. SVGA_COTABLE_DSVIEW,
  92. SVGA_COTABLE_SRVIEW,
  93. SVGA_COTABLE_DXSHADER,
  94. SVGA_COTABLE_ELEMENTLAYOUT,
  95. SVGA_COTABLE_BLENDSTATE,
  96. SVGA_COTABLE_DEPTHSTENCIL,
  97. SVGA_COTABLE_RASTERIZERSTATE,
  98. SVGA_COTABLE_SAMPLER,
  99. SVGA_COTABLE_STREAMOUTPUT,
  100. SVGA_COTABLE_DXQUERY,
  101. SVGA_COTABLE_UAVIEW,
  102. };
  103. static int vmw_cotable_bind(struct vmw_resource *res,
  104. struct ttm_validate_buffer *val_buf);
  105. static int vmw_cotable_unbind(struct vmw_resource *res,
  106. bool readback,
  107. struct ttm_validate_buffer *val_buf);
  108. static int vmw_cotable_create(struct vmw_resource *res);
  109. static int vmw_cotable_destroy(struct vmw_resource *res);
  110. static const struct vmw_res_func vmw_cotable_func = {
  111. .res_type = vmw_res_cotable,
  112. .needs_backup = true,
  113. .may_evict = true,
  114. .prio = 3,
  115. .dirty_prio = 3,
  116. .type_name = "context guest backed object tables",
  117. .backup_placement = &vmw_mob_placement,
  118. .create = vmw_cotable_create,
  119. .destroy = vmw_cotable_destroy,
  120. .bind = vmw_cotable_bind,
  121. .unbind = vmw_cotable_unbind,
  122. };
  123. /**
  124. * vmw_cotable - Convert a struct vmw_resource pointer to a struct
  125. * vmw_cotable pointer
  126. *
  127. * @res: Pointer to the resource.
  128. */
  129. static struct vmw_cotable *vmw_cotable(struct vmw_resource *res)
  130. {
  131. return container_of(res, struct vmw_cotable, res);
  132. }
  133. /**
  134. * vmw_cotable_destroy - Cotable resource destroy callback
  135. *
  136. * @res: Pointer to the cotable resource.
  137. *
  138. * There is no device cotable destroy command, so this function only
  139. * makes sure that the resource id is set to invalid.
  140. */
  141. static int vmw_cotable_destroy(struct vmw_resource *res)
  142. {
  143. res->id = -1;
  144. return 0;
  145. }
  146. /**
  147. * vmw_cotable_unscrub - Undo a cotable unscrub operation
  148. *
  149. * @res: Pointer to the cotable resource
  150. *
  151. * This function issues commands to (re)bind the cotable to
  152. * its backing mob, which needs to be validated and reserved at this point.
  153. * This is identical to bind() except the function interface looks different.
  154. */
  155. static int vmw_cotable_unscrub(struct vmw_resource *res)
  156. {
  157. struct vmw_cotable *vcotbl = vmw_cotable(res);
  158. struct vmw_private *dev_priv = res->dev_priv;
  159. struct ttm_buffer_object *bo = &res->backup->base;
  160. struct {
  161. SVGA3dCmdHeader header;
  162. SVGA3dCmdDXSetCOTable body;
  163. } *cmd;
  164. WARN_ON_ONCE(bo->resource->mem_type != VMW_PL_MOB);
  165. dma_resv_assert_held(bo->base.resv);
  166. cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
  167. if (!cmd)
  168. return -ENOMEM;
  169. WARN_ON(vcotbl->ctx->id == SVGA3D_INVALID_ID);
  170. WARN_ON(bo->resource->mem_type != VMW_PL_MOB);
  171. cmd->header.id = SVGA_3D_CMD_DX_SET_COTABLE;
  172. cmd->header.size = sizeof(cmd->body);
  173. cmd->body.cid = vcotbl->ctx->id;
  174. cmd->body.type = vcotbl->type;
  175. cmd->body.mobid = bo->resource->start;
  176. cmd->body.validSizeInBytes = vcotbl->size_read_back;
  177. vmw_cmd_commit_flush(dev_priv, sizeof(*cmd));
  178. vcotbl->scrubbed = false;
  179. return 0;
  180. }
  181. /**
  182. * vmw_cotable_bind - Undo a cotable unscrub operation
  183. *
  184. * @res: Pointer to the cotable resource
  185. * @val_buf: Pointer to a struct ttm_validate_buffer prepared by the caller
  186. * for convenience / fencing.
  187. *
  188. * This function issues commands to (re)bind the cotable to
  189. * its backing mob, which needs to be validated and reserved at this point.
  190. */
  191. static int vmw_cotable_bind(struct vmw_resource *res,
  192. struct ttm_validate_buffer *val_buf)
  193. {
  194. /*
  195. * The create() callback may have changed @res->backup without
  196. * the caller noticing, and with val_buf->bo still pointing to
  197. * the old backup buffer. Although hackish, and not used currently,
  198. * take the opportunity to correct the value here so that it's not
  199. * misused in the future.
  200. */
  201. val_buf->bo = &res->backup->base;
  202. return vmw_cotable_unscrub(res);
  203. }
  204. /**
  205. * vmw_cotable_scrub - Scrub the cotable from the device.
  206. *
  207. * @res: Pointer to the cotable resource.
  208. * @readback: Whether initiate a readback of the cotable data to the backup
  209. * buffer.
  210. *
  211. * In some situations (context swapouts) it might be desirable to make the
  212. * device forget about the cotable without performing a full unbind. A full
  213. * unbind requires reserved backup buffers and it might not be possible to
  214. * reserve them due to locking order violation issues. The vmw_cotable_scrub
  215. * function implements a partial unbind() without that requirement but with the
  216. * following restrictions.
  217. * 1) Before the cotable is again used by the GPU, vmw_cotable_unscrub() must
  218. * be called.
  219. * 2) Before the cotable backing buffer is used by the CPU, or during the
  220. * resource destruction, vmw_cotable_unbind() must be called.
  221. */
  222. int vmw_cotable_scrub(struct vmw_resource *res, bool readback)
  223. {
  224. struct vmw_cotable *vcotbl = vmw_cotable(res);
  225. struct vmw_private *dev_priv = res->dev_priv;
  226. size_t submit_size;
  227. struct {
  228. SVGA3dCmdHeader header;
  229. SVGA3dCmdDXReadbackCOTable body;
  230. } *cmd0;
  231. struct {
  232. SVGA3dCmdHeader header;
  233. SVGA3dCmdDXSetCOTable body;
  234. } *cmd1;
  235. if (vcotbl->scrubbed)
  236. return 0;
  237. if (co_info[vcotbl->type].unbind_func)
  238. co_info[vcotbl->type].unbind_func(dev_priv,
  239. &vcotbl->resource_list,
  240. readback);
  241. submit_size = sizeof(*cmd1);
  242. if (readback)
  243. submit_size += sizeof(*cmd0);
  244. cmd1 = VMW_CMD_RESERVE(dev_priv, submit_size);
  245. if (!cmd1)
  246. return -ENOMEM;
  247. vcotbl->size_read_back = 0;
  248. if (readback) {
  249. cmd0 = (void *) cmd1;
  250. cmd0->header.id = SVGA_3D_CMD_DX_READBACK_COTABLE;
  251. cmd0->header.size = sizeof(cmd0->body);
  252. cmd0->body.cid = vcotbl->ctx->id;
  253. cmd0->body.type = vcotbl->type;
  254. cmd1 = (void *) &cmd0[1];
  255. vcotbl->size_read_back = res->backup_size;
  256. }
  257. cmd1->header.id = SVGA_3D_CMD_DX_SET_COTABLE;
  258. cmd1->header.size = sizeof(cmd1->body);
  259. cmd1->body.cid = vcotbl->ctx->id;
  260. cmd1->body.type = vcotbl->type;
  261. cmd1->body.mobid = SVGA3D_INVALID_ID;
  262. cmd1->body.validSizeInBytes = 0;
  263. vmw_cmd_commit_flush(dev_priv, submit_size);
  264. vcotbl->scrubbed = true;
  265. /* Trigger a create() on next validate. */
  266. res->id = -1;
  267. return 0;
  268. }
  269. /**
  270. * vmw_cotable_unbind - Cotable resource unbind callback
  271. *
  272. * @res: Pointer to the cotable resource.
  273. * @readback: Whether to read back cotable data to the backup buffer.
  274. * @val_buf: Pointer to a struct ttm_validate_buffer prepared by the caller
  275. * for convenience / fencing.
  276. *
  277. * Unbinds the cotable from the device and fences the backup buffer.
  278. */
  279. static int vmw_cotable_unbind(struct vmw_resource *res,
  280. bool readback,
  281. struct ttm_validate_buffer *val_buf)
  282. {
  283. struct vmw_cotable *vcotbl = vmw_cotable(res);
  284. struct vmw_private *dev_priv = res->dev_priv;
  285. struct ttm_buffer_object *bo = val_buf->bo;
  286. struct vmw_fence_obj *fence;
  287. if (!vmw_resource_mob_attached(res))
  288. return 0;
  289. WARN_ON_ONCE(bo->resource->mem_type != VMW_PL_MOB);
  290. dma_resv_assert_held(bo->base.resv);
  291. mutex_lock(&dev_priv->binding_mutex);
  292. if (!vcotbl->scrubbed)
  293. vmw_dx_context_scrub_cotables(vcotbl->ctx, readback);
  294. mutex_unlock(&dev_priv->binding_mutex);
  295. (void) vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
  296. vmw_bo_fence_single(bo, fence);
  297. if (likely(fence != NULL))
  298. vmw_fence_obj_unreference(&fence);
  299. return 0;
  300. }
  301. /**
  302. * vmw_cotable_readback - Read back a cotable without unbinding.
  303. *
  304. * @res: The cotable resource.
  305. *
  306. * Reads back a cotable to its backing mob without scrubbing the MOB from
  307. * the cotable. The MOB is fenced for subsequent CPU access.
  308. */
  309. static int vmw_cotable_readback(struct vmw_resource *res)
  310. {
  311. struct vmw_cotable *vcotbl = vmw_cotable(res);
  312. struct vmw_private *dev_priv = res->dev_priv;
  313. struct {
  314. SVGA3dCmdHeader header;
  315. SVGA3dCmdDXReadbackCOTable body;
  316. } *cmd;
  317. struct vmw_fence_obj *fence;
  318. if (!vcotbl->scrubbed) {
  319. cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
  320. if (!cmd)
  321. return -ENOMEM;
  322. cmd->header.id = SVGA_3D_CMD_DX_READBACK_COTABLE;
  323. cmd->header.size = sizeof(cmd->body);
  324. cmd->body.cid = vcotbl->ctx->id;
  325. cmd->body.type = vcotbl->type;
  326. vcotbl->size_read_back = res->backup_size;
  327. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  328. }
  329. (void) vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
  330. vmw_bo_fence_single(&res->backup->base, fence);
  331. vmw_fence_obj_unreference(&fence);
  332. return 0;
  333. }
  334. /**
  335. * vmw_cotable_resize - Resize a cotable.
  336. *
  337. * @res: The cotable resource.
  338. * @new_size: The new size.
  339. *
  340. * Resizes a cotable and binds the new backup buffer.
  341. * On failure the cotable is left intact.
  342. * Important! This function may not fail once the MOB switch has been
  343. * committed to hardware. That would put the device context in an
  344. * invalid state which we can't currently recover from.
  345. */
  346. static int vmw_cotable_resize(struct vmw_resource *res, size_t new_size)
  347. {
  348. struct ttm_operation_ctx ctx = { false, false };
  349. struct vmw_private *dev_priv = res->dev_priv;
  350. struct vmw_cotable *vcotbl = vmw_cotable(res);
  351. struct vmw_buffer_object *buf, *old_buf = res->backup;
  352. struct ttm_buffer_object *bo, *old_bo = &res->backup->base;
  353. size_t old_size = res->backup_size;
  354. size_t old_size_read_back = vcotbl->size_read_back;
  355. size_t cur_size_read_back;
  356. struct ttm_bo_kmap_obj old_map, new_map;
  357. int ret;
  358. size_t i;
  359. ret = vmw_cotable_readback(res);
  360. if (ret)
  361. return ret;
  362. cur_size_read_back = vcotbl->size_read_back;
  363. vcotbl->size_read_back = old_size_read_back;
  364. /*
  365. * While device is processing, Allocate and reserve a buffer object
  366. * for the new COTable. Initially pin the buffer object to make sure
  367. * we can use tryreserve without failure.
  368. */
  369. ret = vmw_bo_create(dev_priv, new_size, &vmw_mob_placement,
  370. true, true, vmw_bo_bo_free, &buf);
  371. if (ret) {
  372. DRM_ERROR("Failed initializing new cotable MOB.\n");
  373. return ret;
  374. }
  375. bo = &buf->base;
  376. WARN_ON_ONCE(ttm_bo_reserve(bo, false, true, NULL));
  377. ret = ttm_bo_wait(old_bo, false, false);
  378. if (unlikely(ret != 0)) {
  379. DRM_ERROR("Failed waiting for cotable unbind.\n");
  380. goto out_wait;
  381. }
  382. /*
  383. * Do a page by page copy of COTables. This eliminates slow vmap()s.
  384. * This should really be a TTM utility.
  385. */
  386. for (i = 0; i < old_bo->resource->num_pages; ++i) {
  387. bool dummy;
  388. ret = ttm_bo_kmap(old_bo, i, 1, &old_map);
  389. if (unlikely(ret != 0)) {
  390. DRM_ERROR("Failed mapping old COTable on resize.\n");
  391. goto out_wait;
  392. }
  393. ret = ttm_bo_kmap(bo, i, 1, &new_map);
  394. if (unlikely(ret != 0)) {
  395. DRM_ERROR("Failed mapping new COTable on resize.\n");
  396. goto out_map_new;
  397. }
  398. memcpy(ttm_kmap_obj_virtual(&new_map, &dummy),
  399. ttm_kmap_obj_virtual(&old_map, &dummy),
  400. PAGE_SIZE);
  401. ttm_bo_kunmap(&new_map);
  402. ttm_bo_kunmap(&old_map);
  403. }
  404. /* Unpin new buffer, and switch backup buffers. */
  405. ret = ttm_bo_validate(bo, &vmw_mob_placement, &ctx);
  406. if (unlikely(ret != 0)) {
  407. DRM_ERROR("Failed validating new COTable backup buffer.\n");
  408. goto out_wait;
  409. }
  410. vmw_resource_mob_detach(res);
  411. res->backup = buf;
  412. res->backup_size = new_size;
  413. vcotbl->size_read_back = cur_size_read_back;
  414. /*
  415. * Now tell the device to switch. If this fails, then we need to
  416. * revert the full resize.
  417. */
  418. ret = vmw_cotable_unscrub(res);
  419. if (ret) {
  420. DRM_ERROR("Failed switching COTable backup buffer.\n");
  421. res->backup = old_buf;
  422. res->backup_size = old_size;
  423. vcotbl->size_read_back = old_size_read_back;
  424. vmw_resource_mob_attach(res);
  425. goto out_wait;
  426. }
  427. vmw_resource_mob_attach(res);
  428. /* Let go of the old mob. */
  429. vmw_bo_unreference(&old_buf);
  430. res->id = vcotbl->type;
  431. ret = dma_resv_reserve_fences(bo->base.resv, 1);
  432. if (unlikely(ret))
  433. goto out_wait;
  434. /* Release the pin acquired in vmw_bo_init */
  435. ttm_bo_unpin(bo);
  436. return 0;
  437. out_map_new:
  438. ttm_bo_kunmap(&old_map);
  439. out_wait:
  440. ttm_bo_unpin(bo);
  441. ttm_bo_unreserve(bo);
  442. vmw_bo_unreference(&buf);
  443. return ret;
  444. }
  445. /**
  446. * vmw_cotable_create - Cotable resource create callback
  447. *
  448. * @res: Pointer to a cotable resource.
  449. *
  450. * There is no separate create command for cotables, so this callback, which
  451. * is called before bind() in the validation sequence is instead used for two
  452. * things.
  453. * 1) Unscrub the cotable if it is scrubbed and still attached to a backup
  454. * buffer.
  455. * 2) Resize the cotable if needed.
  456. */
  457. static int vmw_cotable_create(struct vmw_resource *res)
  458. {
  459. struct vmw_cotable *vcotbl = vmw_cotable(res);
  460. size_t new_size = res->backup_size;
  461. size_t needed_size;
  462. int ret;
  463. /* Check whether we need to resize the cotable */
  464. needed_size = (vcotbl->seen_entries + 1) * co_info[vcotbl->type].size;
  465. while (needed_size > new_size)
  466. new_size *= 2;
  467. if (likely(new_size <= res->backup_size)) {
  468. if (vcotbl->scrubbed && vmw_resource_mob_attached(res)) {
  469. ret = vmw_cotable_unscrub(res);
  470. if (ret)
  471. return ret;
  472. }
  473. res->id = vcotbl->type;
  474. return 0;
  475. }
  476. return vmw_cotable_resize(res, new_size);
  477. }
  478. /**
  479. * vmw_hw_cotable_destroy - Cotable hw_destroy callback
  480. *
  481. * @res: Pointer to a cotable resource.
  482. *
  483. * The final (part of resource destruction) destroy callback.
  484. */
  485. static void vmw_hw_cotable_destroy(struct vmw_resource *res)
  486. {
  487. (void) vmw_cotable_destroy(res);
  488. }
  489. /**
  490. * vmw_cotable_free - Cotable resource destructor
  491. *
  492. * @res: Pointer to a cotable resource.
  493. */
  494. static void vmw_cotable_free(struct vmw_resource *res)
  495. {
  496. kfree(res);
  497. }
  498. /**
  499. * vmw_cotable_alloc - Create a cotable resource
  500. *
  501. * @dev_priv: Pointer to a device private struct.
  502. * @ctx: Pointer to the context resource.
  503. * The cotable resource will not add a refcount.
  504. * @type: The cotable type.
  505. */
  506. struct vmw_resource *vmw_cotable_alloc(struct vmw_private *dev_priv,
  507. struct vmw_resource *ctx,
  508. u32 type)
  509. {
  510. struct vmw_cotable *vcotbl;
  511. int ret;
  512. u32 num_entries;
  513. vcotbl = kzalloc(sizeof(*vcotbl), GFP_KERNEL);
  514. if (unlikely(!vcotbl)) {
  515. ret = -ENOMEM;
  516. goto out_no_alloc;
  517. }
  518. ret = vmw_resource_init(dev_priv, &vcotbl->res, true,
  519. vmw_cotable_free, &vmw_cotable_func);
  520. if (unlikely(ret != 0))
  521. goto out_no_init;
  522. INIT_LIST_HEAD(&vcotbl->resource_list);
  523. vcotbl->res.id = type;
  524. vcotbl->res.backup_size = PAGE_SIZE;
  525. num_entries = PAGE_SIZE / co_info[type].size;
  526. if (num_entries < co_info[type].min_initial_entries) {
  527. vcotbl->res.backup_size = co_info[type].min_initial_entries *
  528. co_info[type].size;
  529. vcotbl->res.backup_size = PFN_ALIGN(vcotbl->res.backup_size);
  530. }
  531. vcotbl->scrubbed = true;
  532. vcotbl->seen_entries = -1;
  533. vcotbl->type = type;
  534. vcotbl->ctx = ctx;
  535. vcotbl->res.hw_destroy = vmw_hw_cotable_destroy;
  536. return &vcotbl->res;
  537. out_no_init:
  538. kfree(vcotbl);
  539. out_no_alloc:
  540. return ERR_PTR(ret);
  541. }
  542. /**
  543. * vmw_cotable_notify - Notify the cotable about an item creation
  544. *
  545. * @res: Pointer to a cotable resource.
  546. * @id: Item id.
  547. */
  548. int vmw_cotable_notify(struct vmw_resource *res, int id)
  549. {
  550. struct vmw_cotable *vcotbl = vmw_cotable(res);
  551. if (id < 0 || id >= SVGA_COTABLE_MAX_IDS) {
  552. DRM_ERROR("Illegal COTable id. Type is %u. Id is %d\n",
  553. (unsigned) vcotbl->type, id);
  554. return -EINVAL;
  555. }
  556. if (vcotbl->seen_entries < id) {
  557. /* Trigger a call to create() on next validate */
  558. res->id = -1;
  559. vcotbl->seen_entries = id;
  560. }
  561. return 0;
  562. }
  563. /**
  564. * vmw_cotable_add_resource - add a view to the cotable's list of active views.
  565. *
  566. * @res: pointer struct vmw_resource representing the cotable.
  567. * @head: pointer to the struct list_head member of the resource, dedicated
  568. * to the cotable active resource list.
  569. */
  570. void vmw_cotable_add_resource(struct vmw_resource *res, struct list_head *head)
  571. {
  572. struct vmw_cotable *vcotbl =
  573. container_of(res, struct vmw_cotable, res);
  574. list_add_tail(head, &vcotbl->resource_list);
  575. }