vmwgfx_validation.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /**************************************************************************
  3. *
  4. * Copyright © 2018 - 2023 VMware, Inc., Palo Alto, CA., USA
  5. * All Rights Reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sub license, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the
  16. * next paragraph) shall be included in all copies or substantial portions
  17. * of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  22. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  23. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  24. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  25. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. **************************************************************************/
  28. #include "vmwgfx_bo.h"
  29. #include "vmwgfx_drv.h"
  30. #include "vmwgfx_resource_priv.h"
  31. #include "vmwgfx_validation.h"
  32. #include <linux/slab.h>
  33. /**
  34. * struct vmw_validation_bo_node - Buffer object validation metadata.
  35. * @base: Metadata used for TTM reservation- and validation.
  36. * @hash: A hash entry used for the duplicate detection hash table.
  37. * @coherent_count: If switching backup buffers, number of new coherent
  38. * resources that will have this buffer as a backup buffer.
  39. *
  40. * Bit fields are used since these structures are allocated and freed in
  41. * large numbers and space conservation is desired.
  42. */
  43. struct vmw_validation_bo_node {
  44. struct ttm_validate_buffer base;
  45. struct vmwgfx_hash_item hash;
  46. unsigned int coherent_count;
  47. };
  48. /**
  49. * struct vmw_validation_res_node - Resource validation metadata.
  50. * @head: List head for the resource validation list.
  51. * @hash: A hash entry used for the duplicate detection hash table.
  52. * @res: Reference counted resource pointer.
  53. * @new_guest_memory_bo: Non ref-counted pointer to new guest memory buffer
  54. * to be assigned to a resource.
  55. * @new_guest_memory_offset: Offset into the new backup mob for resources
  56. * that can share MOBs.
  57. * @no_buffer_needed: Kernel does not need to allocate a MOB during validation,
  58. * the command stream provides a mob bind operation.
  59. * @switching_guest_memory_bo: The validation process is switching backup MOB.
  60. * @first_usage: True iff the resource has been seen only once in the current
  61. * validation batch.
  62. * @reserved: Whether the resource is currently reserved by this process.
  63. * @dirty_set: Change dirty status of the resource.
  64. * @dirty: Dirty information VMW_RES_DIRTY_XX.
  65. * @private: Optionally additional memory for caller-private data.
  66. *
  67. * Bit fields are used since these structures are allocated and freed in
  68. * large numbers and space conservation is desired.
  69. */
  70. struct vmw_validation_res_node {
  71. struct list_head head;
  72. struct vmwgfx_hash_item hash;
  73. struct vmw_resource *res;
  74. struct vmw_bo *new_guest_memory_bo;
  75. unsigned long new_guest_memory_offset;
  76. u32 no_buffer_needed : 1;
  77. u32 switching_guest_memory_bo : 1;
  78. u32 first_usage : 1;
  79. u32 reserved : 1;
  80. u32 dirty : 1;
  81. u32 dirty_set : 1;
  82. unsigned long private[];
  83. };
  84. /**
  85. * vmw_validation_mem_alloc - Allocate kernel memory from the validation
  86. * context based allocator
  87. * @ctx: The validation context
  88. * @size: The number of bytes to allocated.
  89. *
  90. * The memory allocated may not exceed PAGE_SIZE, and the returned
  91. * address is aligned to sizeof(long). All memory allocated this way is
  92. * reclaimed after validation when calling any of the exported functions:
  93. * vmw_validation_unref_lists()
  94. * vmw_validation_revert()
  95. * vmw_validation_done()
  96. *
  97. * Return: Pointer to the allocated memory on success. NULL on failure.
  98. */
  99. void *vmw_validation_mem_alloc(struct vmw_validation_context *ctx,
  100. unsigned int size)
  101. {
  102. void *addr;
  103. size = vmw_validation_align(size);
  104. if (size > PAGE_SIZE)
  105. return NULL;
  106. if (ctx->mem_size_left < size) {
  107. struct page *page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  108. if (!page)
  109. return NULL;
  110. list_add_tail(&page->lru, &ctx->page_list);
  111. ctx->page_address = page_address(page);
  112. ctx->mem_size_left = PAGE_SIZE;
  113. }
  114. addr = (void *) (ctx->page_address + (PAGE_SIZE - ctx->mem_size_left));
  115. ctx->mem_size_left -= size;
  116. return addr;
  117. }
  118. /**
  119. * vmw_validation_mem_free - Free all memory allocated using
  120. * vmw_validation_mem_alloc()
  121. * @ctx: The validation context
  122. *
  123. * All memory previously allocated for this context using
  124. * vmw_validation_mem_alloc() is freed.
  125. */
  126. static void vmw_validation_mem_free(struct vmw_validation_context *ctx)
  127. {
  128. struct page *entry, *next;
  129. list_for_each_entry_safe(entry, next, &ctx->page_list, lru) {
  130. list_del_init(&entry->lru);
  131. __free_page(entry);
  132. }
  133. ctx->mem_size_left = 0;
  134. }
  135. /**
  136. * vmw_validation_find_bo_dup - Find a duplicate buffer object entry in the
  137. * validation context's lists.
  138. * @ctx: The validation context to search.
  139. * @vbo: The buffer object to search for.
  140. *
  141. * Return: Pointer to the struct vmw_validation_bo_node referencing the
  142. * duplicate, or NULL if none found.
  143. */
  144. static struct vmw_validation_bo_node *
  145. vmw_validation_find_bo_dup(struct vmw_validation_context *ctx,
  146. struct vmw_bo *vbo)
  147. {
  148. struct vmw_validation_bo_node *bo_node = NULL;
  149. if (!ctx->merge_dups)
  150. return NULL;
  151. if (ctx->sw_context) {
  152. struct vmwgfx_hash_item *hash;
  153. unsigned long key = (unsigned long) vbo;
  154. hash_for_each_possible_rcu(ctx->sw_context->res_ht, hash, head, key) {
  155. if (hash->key == key) {
  156. bo_node = container_of(hash, typeof(*bo_node), hash);
  157. break;
  158. }
  159. }
  160. } else {
  161. struct vmw_validation_bo_node *entry;
  162. list_for_each_entry(entry, &ctx->bo_list, base.head) {
  163. if (entry->base.bo == &vbo->tbo) {
  164. bo_node = entry;
  165. break;
  166. }
  167. }
  168. }
  169. return bo_node;
  170. }
  171. /**
  172. * vmw_validation_find_res_dup - Find a duplicate resource entry in the
  173. * validation context's lists.
  174. * @ctx: The validation context to search.
  175. * @res: Reference counted resource pointer.
  176. *
  177. * Return: Pointer to the struct vmw_validation_bo_node referencing the
  178. * duplicate, or NULL if none found.
  179. */
  180. static struct vmw_validation_res_node *
  181. vmw_validation_find_res_dup(struct vmw_validation_context *ctx,
  182. struct vmw_resource *res)
  183. {
  184. struct vmw_validation_res_node *res_node = NULL;
  185. if (!ctx->merge_dups)
  186. return NULL;
  187. if (ctx->sw_context) {
  188. struct vmwgfx_hash_item *hash;
  189. unsigned long key = (unsigned long) res;
  190. hash_for_each_possible_rcu(ctx->sw_context->res_ht, hash, head, key) {
  191. if (hash->key == key) {
  192. res_node = container_of(hash, typeof(*res_node), hash);
  193. break;
  194. }
  195. }
  196. } else {
  197. struct vmw_validation_res_node *entry;
  198. list_for_each_entry(entry, &ctx->resource_ctx_list, head) {
  199. if (entry->res == res) {
  200. res_node = entry;
  201. goto out;
  202. }
  203. }
  204. list_for_each_entry(entry, &ctx->resource_list, head) {
  205. if (entry->res == res) {
  206. res_node = entry;
  207. break;
  208. }
  209. }
  210. }
  211. out:
  212. return res_node;
  213. }
  214. /**
  215. * vmw_validation_add_bo - Add a buffer object to the validation context.
  216. * @ctx: The validation context.
  217. * @vbo: The buffer object.
  218. *
  219. * Return: Zero on success, negative error code otherwise.
  220. */
  221. int vmw_validation_add_bo(struct vmw_validation_context *ctx,
  222. struct vmw_bo *vbo)
  223. {
  224. struct vmw_validation_bo_node *bo_node;
  225. bo_node = vmw_validation_find_bo_dup(ctx, vbo);
  226. if (!bo_node) {
  227. struct ttm_validate_buffer *val_buf;
  228. bo_node = vmw_validation_mem_alloc(ctx, sizeof(*bo_node));
  229. if (!bo_node)
  230. return -ENOMEM;
  231. if (ctx->sw_context) {
  232. bo_node->hash.key = (unsigned long) vbo;
  233. hash_add_rcu(ctx->sw_context->res_ht, &bo_node->hash.head,
  234. bo_node->hash.key);
  235. }
  236. val_buf = &bo_node->base;
  237. val_buf->bo = ttm_bo_get_unless_zero(&vbo->tbo);
  238. if (!val_buf->bo)
  239. return -ESRCH;
  240. val_buf->num_shared = 0;
  241. list_add_tail(&val_buf->head, &ctx->bo_list);
  242. }
  243. return 0;
  244. }
  245. /**
  246. * vmw_validation_add_resource - Add a resource to the validation context.
  247. * @ctx: The validation context.
  248. * @res: The resource.
  249. * @priv_size: Size of private, additional metadata.
  250. * @dirty: Whether to change dirty status.
  251. * @p_node: Output pointer of additional metadata address.
  252. * @first_usage: Whether this was the first time this resource was seen.
  253. *
  254. * Return: Zero on success, negative error code otherwise.
  255. */
  256. int vmw_validation_add_resource(struct vmw_validation_context *ctx,
  257. struct vmw_resource *res,
  258. size_t priv_size,
  259. u32 dirty,
  260. void **p_node,
  261. bool *first_usage)
  262. {
  263. struct vmw_validation_res_node *node;
  264. node = vmw_validation_find_res_dup(ctx, res);
  265. if (node) {
  266. node->first_usage = 0;
  267. goto out_fill;
  268. }
  269. node = vmw_validation_mem_alloc(ctx, sizeof(*node) + priv_size);
  270. if (!node) {
  271. VMW_DEBUG_USER("Failed to allocate a resource validation entry.\n");
  272. return -ENOMEM;
  273. }
  274. if (ctx->sw_context) {
  275. node->hash.key = (unsigned long) res;
  276. hash_add_rcu(ctx->sw_context->res_ht, &node->hash.head, node->hash.key);
  277. }
  278. node->res = vmw_resource_reference_unless_doomed(res);
  279. if (!node->res) {
  280. hash_del_rcu(&node->hash.head);
  281. return -ESRCH;
  282. }
  283. node->first_usage = 1;
  284. if (!res->dev_priv->has_mob) {
  285. list_add_tail(&node->head, &ctx->resource_list);
  286. } else {
  287. switch (vmw_res_type(res)) {
  288. case vmw_res_context:
  289. case vmw_res_dx_context:
  290. list_add(&node->head, &ctx->resource_ctx_list);
  291. break;
  292. case vmw_res_cotable:
  293. list_add_tail(&node->head, &ctx->resource_ctx_list);
  294. break;
  295. default:
  296. list_add_tail(&node->head, &ctx->resource_list);
  297. break;
  298. }
  299. }
  300. out_fill:
  301. if (dirty) {
  302. node->dirty_set = 1;
  303. /* Overwriting previous information here is intentional! */
  304. node->dirty = (dirty & VMW_RES_DIRTY_SET) ? 1 : 0;
  305. }
  306. if (first_usage)
  307. *first_usage = node->first_usage;
  308. if (p_node)
  309. *p_node = &node->private;
  310. return 0;
  311. }
  312. /**
  313. * vmw_validation_res_set_dirty - Register a resource dirty set or clear during
  314. * validation.
  315. * @ctx: The validation context.
  316. * @val_private: The additional meta-data pointer returned when the
  317. * resource was registered with the validation context. Used to identify
  318. * the resource.
  319. * @dirty: Dirty information VMW_RES_DIRTY_XX
  320. */
  321. void vmw_validation_res_set_dirty(struct vmw_validation_context *ctx,
  322. void *val_private, u32 dirty)
  323. {
  324. struct vmw_validation_res_node *val;
  325. if (!dirty)
  326. return;
  327. val = container_of(val_private, typeof(*val), private);
  328. val->dirty_set = 1;
  329. /* Overwriting previous information here is intentional! */
  330. val->dirty = (dirty & VMW_RES_DIRTY_SET) ? 1 : 0;
  331. }
  332. /**
  333. * vmw_validation_res_switch_backup - Register a backup MOB switch during
  334. * validation.
  335. * @ctx: The validation context.
  336. * @val_private: The additional meta-data pointer returned when the
  337. * resource was registered with the validation context. Used to identify
  338. * the resource.
  339. * @vbo: The new backup buffer object MOB. This buffer object needs to have
  340. * already been registered with the validation context.
  341. * @guest_memory_offset: Offset into the new backup MOB.
  342. */
  343. void vmw_validation_res_switch_backup(struct vmw_validation_context *ctx,
  344. void *val_private,
  345. struct vmw_bo *vbo,
  346. unsigned long guest_memory_offset)
  347. {
  348. struct vmw_validation_res_node *val;
  349. val = container_of(val_private, typeof(*val), private);
  350. val->switching_guest_memory_bo = 1;
  351. if (val->first_usage)
  352. val->no_buffer_needed = 1;
  353. val->new_guest_memory_bo = vbo;
  354. val->new_guest_memory_offset = guest_memory_offset;
  355. }
  356. /**
  357. * vmw_validation_res_reserve - Reserve all resources registered with this
  358. * validation context.
  359. * @ctx: The validation context.
  360. * @intr: Use interruptible waits when possible.
  361. *
  362. * Return: Zero on success, -ERESTARTSYS if interrupted. Negative error
  363. * code on failure.
  364. */
  365. int vmw_validation_res_reserve(struct vmw_validation_context *ctx,
  366. bool intr)
  367. {
  368. struct vmw_validation_res_node *val;
  369. int ret = 0;
  370. list_splice_init(&ctx->resource_ctx_list, &ctx->resource_list);
  371. list_for_each_entry(val, &ctx->resource_list, head) {
  372. struct vmw_resource *res = val->res;
  373. ret = vmw_resource_reserve(res, intr, val->no_buffer_needed);
  374. if (ret)
  375. goto out_unreserve;
  376. val->reserved = 1;
  377. if (res->guest_memory_bo) {
  378. struct vmw_bo *vbo = res->guest_memory_bo;
  379. vmw_bo_placement_set(vbo,
  380. res->func->domain,
  381. res->func->busy_domain);
  382. ret = vmw_validation_add_bo(ctx, vbo);
  383. if (ret)
  384. goto out_unreserve;
  385. }
  386. if (val->switching_guest_memory_bo && val->new_guest_memory_bo &&
  387. res->coherent) {
  388. struct vmw_validation_bo_node *bo_node =
  389. vmw_validation_find_bo_dup(ctx,
  390. val->new_guest_memory_bo);
  391. if (WARN_ON(!bo_node)) {
  392. ret = -EINVAL;
  393. goto out_unreserve;
  394. }
  395. bo_node->coherent_count++;
  396. }
  397. }
  398. return 0;
  399. out_unreserve:
  400. vmw_validation_res_unreserve(ctx, true);
  401. return ret;
  402. }
  403. /**
  404. * vmw_validation_res_unreserve - Unreserve all reserved resources
  405. * registered with this validation context.
  406. * @ctx: The validation context.
  407. * @backoff: Whether this is a backoff- of a commit-type operation. This
  408. * is used to determine whether to switch backup MOBs or not.
  409. */
  410. void vmw_validation_res_unreserve(struct vmw_validation_context *ctx,
  411. bool backoff)
  412. {
  413. struct vmw_validation_res_node *val;
  414. list_splice_init(&ctx->resource_ctx_list, &ctx->resource_list);
  415. if (backoff)
  416. list_for_each_entry(val, &ctx->resource_list, head) {
  417. if (val->reserved)
  418. vmw_resource_unreserve(val->res,
  419. false, false, false,
  420. NULL, 0);
  421. }
  422. else
  423. list_for_each_entry(val, &ctx->resource_list, head) {
  424. if (val->reserved)
  425. vmw_resource_unreserve(val->res,
  426. val->dirty_set,
  427. val->dirty,
  428. val->switching_guest_memory_bo,
  429. val->new_guest_memory_bo,
  430. val->new_guest_memory_offset);
  431. }
  432. }
  433. /**
  434. * vmw_validation_bo_validate_single - Validate a single buffer object.
  435. * @bo: The TTM buffer object base.
  436. * @interruptible: Whether to perform waits interruptible if possible.
  437. *
  438. * Return: Zero on success, -ERESTARTSYS if interrupted. Negative error
  439. * code on failure.
  440. */
  441. static int vmw_validation_bo_validate_single(struct ttm_buffer_object *bo,
  442. bool interruptible)
  443. {
  444. struct vmw_bo *vbo = to_vmw_bo(&bo->base);
  445. struct ttm_operation_ctx ctx = {
  446. .interruptible = interruptible,
  447. .no_wait_gpu = false
  448. };
  449. int ret;
  450. if (atomic_read(&vbo->cpu_writers))
  451. return -EBUSY;
  452. if (vbo->tbo.pin_count > 0)
  453. return 0;
  454. ret = ttm_bo_validate(bo, &vbo->placement, &ctx);
  455. if (ret == 0 || ret == -ERESTARTSYS)
  456. return ret;
  457. /*
  458. * If that failed, try again, this time evicting
  459. * previous contents.
  460. */
  461. ctx.allow_res_evict = true;
  462. return ttm_bo_validate(bo, &vbo->placement, &ctx);
  463. }
  464. /**
  465. * vmw_validation_bo_validate - Validate all buffer objects registered with
  466. * the validation context.
  467. * @ctx: The validation context.
  468. * @intr: Whether to perform waits interruptible if possible.
  469. *
  470. * Return: Zero on success, -ERESTARTSYS if interrupted,
  471. * negative error code on failure.
  472. */
  473. int vmw_validation_bo_validate(struct vmw_validation_context *ctx, bool intr)
  474. {
  475. struct vmw_validation_bo_node *entry;
  476. int ret;
  477. list_for_each_entry(entry, &ctx->bo_list, base.head) {
  478. struct vmw_bo *vbo = to_vmw_bo(&entry->base.bo->base);
  479. ret = vmw_validation_bo_validate_single(entry->base.bo, intr);
  480. if (ret)
  481. return ret;
  482. /*
  483. * Rather than having the resource code allocating the bo
  484. * dirty tracker in resource_unreserve() where we can't fail,
  485. * Do it here when validating the buffer object.
  486. */
  487. if (entry->coherent_count) {
  488. unsigned int coherent_count = entry->coherent_count;
  489. while (coherent_count) {
  490. ret = vmw_bo_dirty_add(vbo);
  491. if (ret)
  492. return ret;
  493. coherent_count--;
  494. }
  495. entry->coherent_count -= coherent_count;
  496. }
  497. if (vbo->dirty)
  498. vmw_bo_dirty_scan(vbo);
  499. }
  500. return 0;
  501. }
  502. /**
  503. * vmw_validation_res_validate - Validate all resources registered with the
  504. * validation context.
  505. * @ctx: The validation context.
  506. * @intr: Whether to perform waits interruptible if possible.
  507. *
  508. * Before this function is called, all resource backup buffers must have
  509. * been validated.
  510. *
  511. * Return: Zero on success, -ERESTARTSYS if interrupted,
  512. * negative error code on failure.
  513. */
  514. int vmw_validation_res_validate(struct vmw_validation_context *ctx, bool intr)
  515. {
  516. struct vmw_validation_res_node *val;
  517. int ret;
  518. list_for_each_entry(val, &ctx->resource_list, head) {
  519. struct vmw_resource *res = val->res;
  520. struct vmw_bo *backup = res->guest_memory_bo;
  521. ret = vmw_resource_validate(res, intr, val->dirty_set &&
  522. val->dirty);
  523. if (ret) {
  524. if (ret != -ERESTARTSYS)
  525. DRM_ERROR("Failed to validate resource.\n");
  526. return ret;
  527. }
  528. /* Check if the resource switched backup buffer */
  529. if (backup && res->guest_memory_bo && backup != res->guest_memory_bo) {
  530. struct vmw_bo *vbo = res->guest_memory_bo;
  531. vmw_bo_placement_set(vbo, res->func->domain,
  532. res->func->busy_domain);
  533. ret = vmw_validation_add_bo(ctx, vbo);
  534. if (ret)
  535. return ret;
  536. }
  537. }
  538. return 0;
  539. }
  540. /**
  541. * vmw_validation_drop_ht - Reset the hash table used for duplicate finding
  542. * and unregister it from this validation context.
  543. * @ctx: The validation context.
  544. *
  545. * The hash table used for duplicate finding is an expensive resource and
  546. * may be protected by mutexes that may cause deadlocks during resource
  547. * unreferencing if held. After resource- and buffer object registering,
  548. * there is no longer any use for this hash table, so allow freeing it
  549. * either to shorten any mutex locking time, or before resources- and
  550. * buffer objects are freed during validation context cleanup.
  551. */
  552. void vmw_validation_drop_ht(struct vmw_validation_context *ctx)
  553. {
  554. struct vmw_validation_bo_node *entry;
  555. struct vmw_validation_res_node *val;
  556. if (!ctx->sw_context)
  557. return;
  558. list_for_each_entry(entry, &ctx->bo_list, base.head)
  559. hash_del_rcu(&entry->hash.head);
  560. list_for_each_entry(val, &ctx->resource_list, head)
  561. hash_del_rcu(&val->hash.head);
  562. list_for_each_entry(val, &ctx->resource_ctx_list, head)
  563. hash_del_rcu(&val->hash.head);
  564. ctx->sw_context = NULL;
  565. }
  566. /**
  567. * vmw_validation_unref_lists - Unregister previously registered buffer
  568. * object and resources.
  569. * @ctx: The validation context.
  570. *
  571. * Note that this function may cause buffer object- and resource destructors
  572. * to be invoked.
  573. */
  574. void vmw_validation_unref_lists(struct vmw_validation_context *ctx)
  575. {
  576. struct vmw_validation_bo_node *entry;
  577. struct vmw_validation_res_node *val;
  578. list_for_each_entry(entry, &ctx->bo_list, base.head) {
  579. ttm_bo_put(entry->base.bo);
  580. entry->base.bo = NULL;
  581. }
  582. list_splice_init(&ctx->resource_ctx_list, &ctx->resource_list);
  583. list_for_each_entry(val, &ctx->resource_list, head)
  584. vmw_resource_unreference(&val->res);
  585. /*
  586. * No need to detach each list entry since they are all freed with
  587. * vmw_validation_free_mem. Just make the inaccessible.
  588. */
  589. INIT_LIST_HEAD(&ctx->bo_list);
  590. INIT_LIST_HEAD(&ctx->resource_list);
  591. vmw_validation_mem_free(ctx);
  592. }
  593. /**
  594. * vmw_validation_prepare - Prepare a validation context for command
  595. * submission.
  596. * @ctx: The validation context.
  597. * @mutex: The mutex used to protect resource reservation.
  598. * @intr: Whether to perform waits interruptible if possible.
  599. *
  600. * Note that the single reservation mutex @mutex is an unfortunate
  601. * construct. Ideally resource reservation should be moved to per-resource
  602. * ww_mutexes.
  603. * If this functions doesn't return Zero to indicate success, all resources
  604. * are left unreserved but still referenced.
  605. * Return: Zero on success, -ERESTARTSYS if interrupted, negative error code
  606. * on error.
  607. */
  608. int vmw_validation_prepare(struct vmw_validation_context *ctx,
  609. struct mutex *mutex,
  610. bool intr)
  611. {
  612. int ret = 0;
  613. if (mutex) {
  614. if (intr)
  615. ret = mutex_lock_interruptible(mutex);
  616. else
  617. mutex_lock(mutex);
  618. if (ret)
  619. return -ERESTARTSYS;
  620. }
  621. ctx->res_mutex = mutex;
  622. ret = vmw_validation_res_reserve(ctx, intr);
  623. if (ret)
  624. goto out_no_res_reserve;
  625. ret = vmw_validation_bo_reserve(ctx, intr);
  626. if (ret)
  627. goto out_no_bo_reserve;
  628. ret = vmw_validation_bo_validate(ctx, intr);
  629. if (ret)
  630. goto out_no_validate;
  631. ret = vmw_validation_res_validate(ctx, intr);
  632. if (ret)
  633. goto out_no_validate;
  634. return 0;
  635. out_no_validate:
  636. vmw_validation_bo_backoff(ctx);
  637. out_no_bo_reserve:
  638. vmw_validation_res_unreserve(ctx, true);
  639. out_no_res_reserve:
  640. if (mutex)
  641. mutex_unlock(mutex);
  642. return ret;
  643. }
  644. /**
  645. * vmw_validation_revert - Revert validation actions if command submission
  646. * failed.
  647. *
  648. * @ctx: The validation context.
  649. *
  650. * The caller still needs to unref resources after a call to this function.
  651. */
  652. void vmw_validation_revert(struct vmw_validation_context *ctx)
  653. {
  654. vmw_validation_bo_backoff(ctx);
  655. vmw_validation_res_unreserve(ctx, true);
  656. if (ctx->res_mutex)
  657. mutex_unlock(ctx->res_mutex);
  658. vmw_validation_unref_lists(ctx);
  659. }
  660. /**
  661. * vmw_validation_done - Commit validation actions after command submission
  662. * success.
  663. * @ctx: The validation context.
  664. * @fence: Fence with which to fence all buffer objects taking part in the
  665. * command submission.
  666. *
  667. * The caller does NOT need to unref resources after a call to this function.
  668. */
  669. void vmw_validation_done(struct vmw_validation_context *ctx,
  670. struct vmw_fence_obj *fence)
  671. {
  672. vmw_validation_bo_fence(ctx, fence);
  673. vmw_validation_res_unreserve(ctx, false);
  674. if (ctx->res_mutex)
  675. mutex_unlock(ctx->res_mutex);
  676. vmw_validation_unref_lists(ctx);
  677. }
  678. /**
  679. * vmw_validation_preload_bo - Preload the validation memory allocator for a
  680. * call to vmw_validation_add_bo().
  681. * @ctx: Pointer to the validation context.
  682. *
  683. * Iff this function returns successfully, the next call to
  684. * vmw_validation_add_bo() is guaranteed not to sleep. An error is not fatal
  685. * but voids the guarantee.
  686. *
  687. * Returns: Zero if successful, %-EINVAL otherwise.
  688. */
  689. int vmw_validation_preload_bo(struct vmw_validation_context *ctx)
  690. {
  691. unsigned int size = sizeof(struct vmw_validation_bo_node);
  692. if (!vmw_validation_mem_alloc(ctx, size))
  693. return -ENOMEM;
  694. ctx->mem_size_left += size;
  695. return 0;
  696. }
  697. /**
  698. * vmw_validation_preload_res - Preload the validation memory allocator for a
  699. * call to vmw_validation_add_res().
  700. * @ctx: Pointer to the validation context.
  701. * @size: Size of the validation node extra data. See below.
  702. *
  703. * Iff this function returns successfully, the next call to
  704. * vmw_validation_add_res() with the same or smaller @size is guaranteed not to
  705. * sleep. An error is not fatal but voids the guarantee.
  706. *
  707. * Returns: Zero if successful, %-EINVAL otherwise.
  708. */
  709. int vmw_validation_preload_res(struct vmw_validation_context *ctx,
  710. unsigned int size)
  711. {
  712. size = vmw_validation_align(sizeof(struct vmw_validation_res_node) +
  713. size) +
  714. vmw_validation_align(sizeof(struct vmw_validation_bo_node));
  715. if (!vmw_validation_mem_alloc(ctx, size))
  716. return -ENOMEM;
  717. ctx->mem_size_left += size;
  718. return 0;
  719. }
  720. /**
  721. * vmw_validation_bo_backoff - Unreserve buffer objects registered with a
  722. * validation context
  723. * @ctx: The validation context
  724. *
  725. * This function unreserves the buffer objects previously reserved using
  726. * vmw_validation_bo_reserve. It's typically used as part of an error path
  727. */
  728. void vmw_validation_bo_backoff(struct vmw_validation_context *ctx)
  729. {
  730. struct vmw_validation_bo_node *entry;
  731. /*
  732. * Switching coherent resource backup buffers failed.
  733. * Release corresponding buffer object dirty trackers.
  734. */
  735. list_for_each_entry(entry, &ctx->bo_list, base.head) {
  736. if (entry->coherent_count) {
  737. unsigned int coherent_count = entry->coherent_count;
  738. struct vmw_bo *vbo = to_vmw_bo(&entry->base.bo->base);
  739. while (coherent_count--)
  740. vmw_bo_dirty_release(vbo);
  741. }
  742. }
  743. ttm_eu_backoff_reservation(&ctx->ticket, &ctx->bo_list);
  744. }