vmwgfx_validation.c 25 KB

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