vmwgfx_validation.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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. vmw_bo_reference(vbo);
  238. val_buf->bo = &vbo->tbo;
  239. val_buf->num_shared = 0;
  240. list_add_tail(&val_buf->head, &ctx->bo_list);
  241. }
  242. return 0;
  243. }
  244. /**
  245. * vmw_validation_add_resource - Add a resource to the validation context.
  246. * @ctx: The validation context.
  247. * @res: The resource.
  248. * @priv_size: Size of private, additional metadata.
  249. * @dirty: Whether to change dirty status.
  250. * @p_node: Output pointer of additional metadata address.
  251. * @first_usage: Whether this was the first time this resource was seen.
  252. *
  253. * Return: Zero on success, negative error code otherwise.
  254. */
  255. int vmw_validation_add_resource(struct vmw_validation_context *ctx,
  256. struct vmw_resource *res,
  257. size_t priv_size,
  258. u32 dirty,
  259. void **p_node,
  260. bool *first_usage)
  261. {
  262. struct vmw_validation_res_node *node;
  263. node = vmw_validation_find_res_dup(ctx, res);
  264. if (node) {
  265. node->first_usage = 0;
  266. goto out_fill;
  267. }
  268. node = vmw_validation_mem_alloc(ctx, sizeof(*node) + priv_size);
  269. if (!node) {
  270. VMW_DEBUG_USER("Failed to allocate a resource validation entry.\n");
  271. return -ENOMEM;
  272. }
  273. if (ctx->sw_context) {
  274. node->hash.key = (unsigned long) res;
  275. hash_add_rcu(ctx->sw_context->res_ht, &node->hash.head, node->hash.key);
  276. }
  277. node->res = vmw_resource_reference_unless_doomed(res);
  278. if (!node->res) {
  279. hash_del_rcu(&node->hash.head);
  280. return -ESRCH;
  281. }
  282. node->first_usage = 1;
  283. if (!res->dev_priv->has_mob) {
  284. list_add_tail(&node->head, &ctx->resource_list);
  285. } else {
  286. switch (vmw_res_type(res)) {
  287. case vmw_res_context:
  288. case vmw_res_dx_context:
  289. list_add(&node->head, &ctx->resource_ctx_list);
  290. break;
  291. case vmw_res_cotable:
  292. list_add_tail(&node->head, &ctx->resource_ctx_list);
  293. break;
  294. default:
  295. list_add_tail(&node->head, &ctx->resource_list);
  296. break;
  297. }
  298. }
  299. out_fill:
  300. if (dirty) {
  301. node->dirty_set = 1;
  302. /* Overwriting previous information here is intentional! */
  303. node->dirty = (dirty & VMW_RES_DIRTY_SET) ? 1 : 0;
  304. }
  305. if (first_usage)
  306. *first_usage = node->first_usage;
  307. if (p_node)
  308. *p_node = &node->private;
  309. return 0;
  310. }
  311. /**
  312. * vmw_validation_res_set_dirty - Register a resource dirty set or clear during
  313. * validation.
  314. * @ctx: The validation context.
  315. * @val_private: The additional meta-data pointer returned when the
  316. * resource was registered with the validation context. Used to identify
  317. * the resource.
  318. * @dirty: Dirty information VMW_RES_DIRTY_XX
  319. */
  320. void vmw_validation_res_set_dirty(struct vmw_validation_context *ctx,
  321. void *val_private, u32 dirty)
  322. {
  323. struct vmw_validation_res_node *val;
  324. if (!dirty)
  325. return;
  326. val = container_of(val_private, typeof(*val), private);
  327. val->dirty_set = 1;
  328. /* Overwriting previous information here is intentional! */
  329. val->dirty = (dirty & VMW_RES_DIRTY_SET) ? 1 : 0;
  330. }
  331. /**
  332. * vmw_validation_res_switch_backup - Register a backup MOB switch during
  333. * validation.
  334. * @ctx: The validation context.
  335. * @val_private: The additional meta-data pointer returned when the
  336. * resource was registered with the validation context. Used to identify
  337. * the resource.
  338. * @vbo: The new backup buffer object MOB. This buffer object needs to have
  339. * already been registered with the validation context.
  340. * @guest_memory_offset: Offset into the new backup MOB.
  341. */
  342. void vmw_validation_res_switch_backup(struct vmw_validation_context *ctx,
  343. void *val_private,
  344. struct vmw_bo *vbo,
  345. unsigned long guest_memory_offset)
  346. {
  347. struct vmw_validation_res_node *val;
  348. val = container_of(val_private, typeof(*val), private);
  349. val->switching_guest_memory_bo = 1;
  350. if (val->first_usage)
  351. val->no_buffer_needed = 1;
  352. val->new_guest_memory_bo = vbo;
  353. val->new_guest_memory_offset = guest_memory_offset;
  354. }
  355. /**
  356. * vmw_validation_res_reserve - Reserve all resources registered with this
  357. * validation context.
  358. * @ctx: The validation context.
  359. * @intr: Use interruptible waits when possible.
  360. *
  361. * Return: Zero on success, -ERESTARTSYS if interrupted. Negative error
  362. * code on failure.
  363. */
  364. int vmw_validation_res_reserve(struct vmw_validation_context *ctx,
  365. bool intr)
  366. {
  367. struct vmw_validation_res_node *val;
  368. int ret = 0;
  369. list_splice_init(&ctx->resource_ctx_list, &ctx->resource_list);
  370. list_for_each_entry(val, &ctx->resource_list, head) {
  371. struct vmw_resource *res = val->res;
  372. ret = vmw_resource_reserve(res, intr, val->no_buffer_needed);
  373. if (ret)
  374. goto out_unreserve;
  375. val->reserved = 1;
  376. if (res->guest_memory_bo) {
  377. struct vmw_bo *vbo = res->guest_memory_bo;
  378. vmw_bo_placement_set(vbo,
  379. res->func->domain,
  380. res->func->busy_domain);
  381. ret = vmw_validation_add_bo(ctx, vbo);
  382. if (ret)
  383. goto out_unreserve;
  384. }
  385. if (val->switching_guest_memory_bo && val->new_guest_memory_bo &&
  386. res->coherent) {
  387. struct vmw_validation_bo_node *bo_node =
  388. vmw_validation_find_bo_dup(ctx,
  389. val->new_guest_memory_bo);
  390. if (WARN_ON(!bo_node)) {
  391. ret = -EINVAL;
  392. goto out_unreserve;
  393. }
  394. bo_node->coherent_count++;
  395. }
  396. }
  397. return 0;
  398. out_unreserve:
  399. vmw_validation_res_unreserve(ctx, true);
  400. return ret;
  401. }
  402. /**
  403. * vmw_validation_res_unreserve - Unreserve all reserved resources
  404. * registered with this validation context.
  405. * @ctx: The validation context.
  406. * @backoff: Whether this is a backoff- of a commit-type operation. This
  407. * is used to determine whether to switch backup MOBs or not.
  408. */
  409. void vmw_validation_res_unreserve(struct vmw_validation_context *ctx,
  410. bool backoff)
  411. {
  412. struct vmw_validation_res_node *val;
  413. list_splice_init(&ctx->resource_ctx_list, &ctx->resource_list);
  414. if (backoff)
  415. list_for_each_entry(val, &ctx->resource_list, head) {
  416. if (val->reserved)
  417. vmw_resource_unreserve(val->res,
  418. false, false, false,
  419. NULL, 0);
  420. }
  421. else
  422. list_for_each_entry(val, &ctx->resource_list, head) {
  423. if (val->reserved)
  424. vmw_resource_unreserve(val->res,
  425. val->dirty_set,
  426. val->dirty,
  427. val->switching_guest_memory_bo,
  428. val->new_guest_memory_bo,
  429. val->new_guest_memory_offset);
  430. }
  431. }
  432. /**
  433. * vmw_validation_bo_validate_single - Validate a single buffer object.
  434. * @bo: The TTM buffer object base.
  435. * @interruptible: Whether to perform waits interruptible if possible.
  436. *
  437. * Return: Zero on success, -ERESTARTSYS if interrupted. Negative error
  438. * code on failure.
  439. */
  440. static int vmw_validation_bo_validate_single(struct ttm_buffer_object *bo,
  441. bool interruptible)
  442. {
  443. struct vmw_bo *vbo = to_vmw_bo(&bo->base);
  444. struct ttm_operation_ctx ctx = {
  445. .interruptible = interruptible,
  446. .no_wait_gpu = false
  447. };
  448. int ret;
  449. if (atomic_read(&vbo->cpu_writers))
  450. return -EBUSY;
  451. if (vbo->tbo.pin_count > 0)
  452. return 0;
  453. ret = ttm_bo_validate(bo, &vbo->placement, &ctx);
  454. if (ret == 0 || ret == -ERESTARTSYS)
  455. return ret;
  456. /*
  457. * If that failed, try again, this time evicting
  458. * previous contents.
  459. */
  460. ctx.allow_res_evict = true;
  461. return ttm_bo_validate(bo, &vbo->placement, &ctx);
  462. }
  463. /**
  464. * vmw_validation_bo_validate - Validate all buffer objects registered with
  465. * the validation context.
  466. * @ctx: The validation context.
  467. * @intr: Whether to perform waits interruptible if possible.
  468. *
  469. * Return: Zero on success, -ERESTARTSYS if interrupted,
  470. * negative error code on failure.
  471. */
  472. int vmw_validation_bo_validate(struct vmw_validation_context *ctx, bool intr)
  473. {
  474. struct vmw_validation_bo_node *entry;
  475. int ret;
  476. list_for_each_entry(entry, &ctx->bo_list, base.head) {
  477. struct vmw_bo *vbo = to_vmw_bo(&entry->base.bo->base);
  478. ret = vmw_validation_bo_validate_single(entry->base.bo, intr);
  479. if (ret)
  480. return ret;
  481. /*
  482. * Rather than having the resource code allocating the bo
  483. * dirty tracker in resource_unreserve() where we can't fail,
  484. * Do it here when validating the buffer object.
  485. */
  486. if (entry->coherent_count) {
  487. unsigned int coherent_count = entry->coherent_count;
  488. while (coherent_count) {
  489. ret = vmw_bo_dirty_add(vbo);
  490. if (ret)
  491. return ret;
  492. coherent_count--;
  493. }
  494. entry->coherent_count -= coherent_count;
  495. }
  496. if (vbo->dirty)
  497. vmw_bo_dirty_scan(vbo);
  498. }
  499. return 0;
  500. }
  501. /**
  502. * vmw_validation_res_validate - Validate all resources registered with the
  503. * validation context.
  504. * @ctx: The validation context.
  505. * @intr: Whether to perform waits interruptible if possible.
  506. *
  507. * Before this function is called, all resource backup buffers must have
  508. * been validated.
  509. *
  510. * Return: Zero on success, -ERESTARTSYS if interrupted,
  511. * negative error code on failure.
  512. */
  513. int vmw_validation_res_validate(struct vmw_validation_context *ctx, bool intr)
  514. {
  515. struct vmw_validation_res_node *val;
  516. int ret;
  517. list_for_each_entry(val, &ctx->resource_list, head) {
  518. struct vmw_resource *res = val->res;
  519. struct vmw_bo *backup = res->guest_memory_bo;
  520. ret = vmw_resource_validate(res, intr, val->dirty_set &&
  521. val->dirty);
  522. if (ret) {
  523. if (ret != -ERESTARTSYS)
  524. DRM_ERROR("Failed to validate resource.\n");
  525. return ret;
  526. }
  527. /* Check if the resource switched backup buffer */
  528. if (backup && res->guest_memory_bo && backup != res->guest_memory_bo) {
  529. struct vmw_bo *vbo = res->guest_memory_bo;
  530. vmw_bo_placement_set(vbo, res->func->domain,
  531. res->func->busy_domain);
  532. ret = vmw_validation_add_bo(ctx, vbo);
  533. if (ret)
  534. return ret;
  535. }
  536. }
  537. return 0;
  538. }
  539. /**
  540. * vmw_validation_drop_ht - Reset the hash table used for duplicate finding
  541. * and unregister it from this validation context.
  542. * @ctx: The validation context.
  543. *
  544. * The hash table used for duplicate finding is an expensive resource and
  545. * may be protected by mutexes that may cause deadlocks during resource
  546. * unreferencing if held. After resource- and buffer object registering,
  547. * there is no longer any use for this hash table, so allow freeing it
  548. * either to shorten any mutex locking time, or before resources- and
  549. * buffer objects are freed during validation context cleanup.
  550. */
  551. void vmw_validation_drop_ht(struct vmw_validation_context *ctx)
  552. {
  553. struct vmw_validation_bo_node *entry;
  554. struct vmw_validation_res_node *val;
  555. if (!ctx->sw_context)
  556. return;
  557. list_for_each_entry(entry, &ctx->bo_list, base.head)
  558. hash_del_rcu(&entry->hash.head);
  559. list_for_each_entry(val, &ctx->resource_list, head)
  560. hash_del_rcu(&val->hash.head);
  561. list_for_each_entry(val, &ctx->resource_ctx_list, head)
  562. hash_del_rcu(&val->hash.head);
  563. ctx->sw_context = NULL;
  564. }
  565. /**
  566. * vmw_validation_unref_lists - Unregister previously registered buffer
  567. * object and resources.
  568. * @ctx: The validation context.
  569. *
  570. * Note that this function may cause buffer object- and resource destructors
  571. * to be invoked.
  572. */
  573. void vmw_validation_unref_lists(struct vmw_validation_context *ctx)
  574. {
  575. struct vmw_validation_bo_node *entry;
  576. struct vmw_validation_res_node *val;
  577. list_for_each_entry(entry, &ctx->bo_list, base.head) {
  578. drm_gem_object_put(&entry->base.bo->base);
  579. entry->base.bo = NULL;
  580. }
  581. list_splice_init(&ctx->resource_ctx_list, &ctx->resource_list);
  582. list_for_each_entry(val, &ctx->resource_list, head)
  583. vmw_resource_unreference(&val->res);
  584. /*
  585. * No need to detach each list entry since they are all freed with
  586. * vmw_validation_free_mem. Just make the inaccessible.
  587. */
  588. INIT_LIST_HEAD(&ctx->bo_list);
  589. INIT_LIST_HEAD(&ctx->resource_list);
  590. vmw_validation_mem_free(ctx);
  591. }
  592. /**
  593. * vmw_validation_prepare - Prepare a validation context for command
  594. * submission.
  595. * @ctx: The validation context.
  596. * @mutex: The mutex used to protect resource reservation.
  597. * @intr: Whether to perform waits interruptible if possible.
  598. *
  599. * Note that the single reservation mutex @mutex is an unfortunate
  600. * construct. Ideally resource reservation should be moved to per-resource
  601. * ww_mutexes.
  602. * If this functions doesn't return Zero to indicate success, all resources
  603. * are left unreserved but still referenced.
  604. * Return: Zero on success, -ERESTARTSYS if interrupted, negative error code
  605. * on error.
  606. */
  607. int vmw_validation_prepare(struct vmw_validation_context *ctx,
  608. struct mutex *mutex,
  609. bool intr)
  610. {
  611. int ret = 0;
  612. if (mutex) {
  613. if (intr)
  614. ret = mutex_lock_interruptible(mutex);
  615. else
  616. mutex_lock(mutex);
  617. if (ret)
  618. return -ERESTARTSYS;
  619. }
  620. ctx->res_mutex = mutex;
  621. ret = vmw_validation_res_reserve(ctx, intr);
  622. if (ret)
  623. goto out_no_res_reserve;
  624. ret = vmw_validation_bo_reserve(ctx, intr);
  625. if (ret)
  626. goto out_no_bo_reserve;
  627. ret = vmw_validation_bo_validate(ctx, intr);
  628. if (ret)
  629. goto out_no_validate;
  630. ret = vmw_validation_res_validate(ctx, intr);
  631. if (ret)
  632. goto out_no_validate;
  633. return 0;
  634. out_no_validate:
  635. vmw_validation_bo_backoff(ctx);
  636. out_no_bo_reserve:
  637. vmw_validation_res_unreserve(ctx, true);
  638. out_no_res_reserve:
  639. if (mutex)
  640. mutex_unlock(mutex);
  641. return ret;
  642. }
  643. /**
  644. * vmw_validation_revert - Revert validation actions if command submission
  645. * failed.
  646. *
  647. * @ctx: The validation context.
  648. *
  649. * The caller still needs to unref resources after a call to this function.
  650. */
  651. void vmw_validation_revert(struct vmw_validation_context *ctx)
  652. {
  653. vmw_validation_bo_backoff(ctx);
  654. vmw_validation_res_unreserve(ctx, true);
  655. if (ctx->res_mutex)
  656. mutex_unlock(ctx->res_mutex);
  657. vmw_validation_unref_lists(ctx);
  658. }
  659. /**
  660. * vmw_validation_done - Commit validation actions after command submission
  661. * success.
  662. * @ctx: The validation context.
  663. * @fence: Fence with which to fence all buffer objects taking part in the
  664. * command submission.
  665. *
  666. * The caller does NOT need to unref resources after a call to this function.
  667. */
  668. void vmw_validation_done(struct vmw_validation_context *ctx,
  669. struct vmw_fence_obj *fence)
  670. {
  671. vmw_validation_bo_fence(ctx, fence);
  672. vmw_validation_res_unreserve(ctx, false);
  673. if (ctx->res_mutex)
  674. mutex_unlock(ctx->res_mutex);
  675. vmw_validation_unref_lists(ctx);
  676. }
  677. /**
  678. * vmw_validation_preload_bo - Preload the validation memory allocator for a
  679. * call to vmw_validation_add_bo().
  680. * @ctx: Pointer to the validation context.
  681. *
  682. * Iff this function returns successfully, the next call to
  683. * vmw_validation_add_bo() is guaranteed not to sleep. An error is not fatal
  684. * but voids the guarantee.
  685. *
  686. * Returns: Zero if successful, %-EINVAL otherwise.
  687. */
  688. int vmw_validation_preload_bo(struct vmw_validation_context *ctx)
  689. {
  690. unsigned int size = sizeof(struct vmw_validation_bo_node);
  691. if (!vmw_validation_mem_alloc(ctx, size))
  692. return -ENOMEM;
  693. ctx->mem_size_left += size;
  694. return 0;
  695. }
  696. /**
  697. * vmw_validation_preload_res - Preload the validation memory allocator for a
  698. * call to vmw_validation_add_res().
  699. * @ctx: Pointer to the validation context.
  700. * @size: Size of the validation node extra data. See below.
  701. *
  702. * Iff this function returns successfully, the next call to
  703. * vmw_validation_add_res() with the same or smaller @size is guaranteed not to
  704. * sleep. An error is not fatal but voids the guarantee.
  705. *
  706. * Returns: Zero if successful, %-EINVAL otherwise.
  707. */
  708. int vmw_validation_preload_res(struct vmw_validation_context *ctx,
  709. unsigned int size)
  710. {
  711. size = vmw_validation_align(sizeof(struct vmw_validation_res_node) +
  712. size) +
  713. vmw_validation_align(sizeof(struct vmw_validation_bo_node));
  714. if (!vmw_validation_mem_alloc(ctx, size))
  715. return -ENOMEM;
  716. ctx->mem_size_left += size;
  717. return 0;
  718. }
  719. /**
  720. * vmw_validation_bo_backoff - Unreserve buffer objects registered with a
  721. * validation context
  722. * @ctx: The validation context
  723. *
  724. * This function unreserves the buffer objects previously reserved using
  725. * vmw_validation_bo_reserve. It's typically used as part of an error path
  726. */
  727. void vmw_validation_bo_backoff(struct vmw_validation_context *ctx)
  728. {
  729. struct vmw_validation_bo_node *entry;
  730. /*
  731. * Switching coherent resource backup buffers failed.
  732. * Release corresponding buffer object dirty trackers.
  733. */
  734. list_for_each_entry(entry, &ctx->bo_list, base.head) {
  735. if (entry->coherent_count) {
  736. unsigned int coherent_count = entry->coherent_count;
  737. struct vmw_bo *vbo = to_vmw_bo(&entry->base.bo->base);
  738. while (coherent_count--)
  739. vmw_bo_dirty_release(vbo);
  740. }
  741. }
  742. ttm_eu_backoff_reservation(&ctx->ticket, &ctx->bo_list);
  743. }