vmwgfx_resource.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /**************************************************************************
  3. *
  4. * Copyright 2009-2015 VMware, Inc., Palo Alto, CA., USA
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. #include "vmwgfx_drv.h"
  28. #include <drm/vmwgfx_drm.h>
  29. #include <drm/ttm/ttm_placement.h>
  30. #include <drm/drmP.h>
  31. #include "vmwgfx_resource_priv.h"
  32. #include "vmwgfx_binding.h"
  33. #define VMW_RES_EVICT_ERR_COUNT 10
  34. struct vmw_resource *vmw_resource_reference(struct vmw_resource *res)
  35. {
  36. kref_get(&res->kref);
  37. return res;
  38. }
  39. struct vmw_resource *
  40. vmw_resource_reference_unless_doomed(struct vmw_resource *res)
  41. {
  42. return kref_get_unless_zero(&res->kref) ? res : NULL;
  43. }
  44. /**
  45. * vmw_resource_release_id - release a resource id to the id manager.
  46. *
  47. * @res: Pointer to the resource.
  48. *
  49. * Release the resource id to the resource id manager and set it to -1
  50. */
  51. void vmw_resource_release_id(struct vmw_resource *res)
  52. {
  53. struct vmw_private *dev_priv = res->dev_priv;
  54. struct idr *idr = &dev_priv->res_idr[res->func->res_type];
  55. write_lock(&dev_priv->resource_lock);
  56. if (res->id != -1)
  57. idr_remove(idr, res->id);
  58. res->id = -1;
  59. write_unlock(&dev_priv->resource_lock);
  60. }
  61. static void vmw_resource_release(struct kref *kref)
  62. {
  63. struct vmw_resource *res =
  64. container_of(kref, struct vmw_resource, kref);
  65. struct vmw_private *dev_priv = res->dev_priv;
  66. int id;
  67. struct idr *idr = &dev_priv->res_idr[res->func->res_type];
  68. write_lock(&dev_priv->resource_lock);
  69. res->avail = false;
  70. list_del_init(&res->lru_head);
  71. write_unlock(&dev_priv->resource_lock);
  72. if (res->backup) {
  73. struct ttm_buffer_object *bo = &res->backup->base;
  74. ttm_bo_reserve(bo, false, false, NULL);
  75. if (!list_empty(&res->mob_head) &&
  76. res->func->unbind != NULL) {
  77. struct ttm_validate_buffer val_buf;
  78. val_buf.bo = bo;
  79. val_buf.shared = false;
  80. res->func->unbind(res, false, &val_buf);
  81. }
  82. res->backup_dirty = false;
  83. list_del_init(&res->mob_head);
  84. ttm_bo_unreserve(bo);
  85. vmw_bo_unreference(&res->backup);
  86. }
  87. if (likely(res->hw_destroy != NULL)) {
  88. mutex_lock(&dev_priv->binding_mutex);
  89. vmw_binding_res_list_kill(&res->binding_head);
  90. mutex_unlock(&dev_priv->binding_mutex);
  91. res->hw_destroy(res);
  92. }
  93. id = res->id;
  94. if (res->res_free != NULL)
  95. res->res_free(res);
  96. else
  97. kfree(res);
  98. write_lock(&dev_priv->resource_lock);
  99. if (id != -1)
  100. idr_remove(idr, id);
  101. write_unlock(&dev_priv->resource_lock);
  102. }
  103. void vmw_resource_unreference(struct vmw_resource **p_res)
  104. {
  105. struct vmw_resource *res = *p_res;
  106. *p_res = NULL;
  107. kref_put(&res->kref, vmw_resource_release);
  108. }
  109. /**
  110. * vmw_resource_alloc_id - release a resource id to the id manager.
  111. *
  112. * @res: Pointer to the resource.
  113. *
  114. * Allocate the lowest free resource from the resource manager, and set
  115. * @res->id to that id. Returns 0 on success and -ENOMEM on failure.
  116. */
  117. int vmw_resource_alloc_id(struct vmw_resource *res)
  118. {
  119. struct vmw_private *dev_priv = res->dev_priv;
  120. int ret;
  121. struct idr *idr = &dev_priv->res_idr[res->func->res_type];
  122. BUG_ON(res->id != -1);
  123. idr_preload(GFP_KERNEL);
  124. write_lock(&dev_priv->resource_lock);
  125. ret = idr_alloc(idr, res, 1, 0, GFP_NOWAIT);
  126. if (ret >= 0)
  127. res->id = ret;
  128. write_unlock(&dev_priv->resource_lock);
  129. idr_preload_end();
  130. return ret < 0 ? ret : 0;
  131. }
  132. /**
  133. * vmw_resource_init - initialize a struct vmw_resource
  134. *
  135. * @dev_priv: Pointer to a device private struct.
  136. * @res: The struct vmw_resource to initialize.
  137. * @obj_type: Resource object type.
  138. * @delay_id: Boolean whether to defer device id allocation until
  139. * the first validation.
  140. * @res_free: Resource destructor.
  141. * @func: Resource function table.
  142. */
  143. int vmw_resource_init(struct vmw_private *dev_priv, struct vmw_resource *res,
  144. bool delay_id,
  145. void (*res_free) (struct vmw_resource *res),
  146. const struct vmw_res_func *func)
  147. {
  148. kref_init(&res->kref);
  149. res->hw_destroy = NULL;
  150. res->res_free = res_free;
  151. res->avail = false;
  152. res->dev_priv = dev_priv;
  153. res->func = func;
  154. INIT_LIST_HEAD(&res->lru_head);
  155. INIT_LIST_HEAD(&res->mob_head);
  156. INIT_LIST_HEAD(&res->binding_head);
  157. res->id = -1;
  158. res->backup = NULL;
  159. res->backup_offset = 0;
  160. res->backup_dirty = false;
  161. res->res_dirty = false;
  162. if (delay_id)
  163. return 0;
  164. else
  165. return vmw_resource_alloc_id(res);
  166. }
  167. /**
  168. * vmw_resource_activate
  169. *
  170. * @res: Pointer to the newly created resource
  171. * @hw_destroy: Destroy function. NULL if none.
  172. *
  173. * Activate a resource after the hardware has been made aware of it.
  174. * Set tye destroy function to @destroy. Typically this frees the
  175. * resource and destroys the hardware resources associated with it.
  176. * Activate basically means that the function vmw_resource_lookup will
  177. * find it.
  178. */
  179. void vmw_resource_activate(struct vmw_resource *res,
  180. void (*hw_destroy) (struct vmw_resource *))
  181. {
  182. struct vmw_private *dev_priv = res->dev_priv;
  183. write_lock(&dev_priv->resource_lock);
  184. res->avail = true;
  185. res->hw_destroy = hw_destroy;
  186. write_unlock(&dev_priv->resource_lock);
  187. }
  188. /**
  189. * vmw_user_resource_lookup_handle - lookup a struct resource from a
  190. * TTM user-space handle and perform basic type checks
  191. *
  192. * @dev_priv: Pointer to a device private struct
  193. * @tfile: Pointer to a struct ttm_object_file identifying the caller
  194. * @handle: The TTM user-space handle
  195. * @converter: Pointer to an object describing the resource type
  196. * @p_res: On successful return the location pointed to will contain
  197. * a pointer to a refcounted struct vmw_resource.
  198. *
  199. * If the handle can't be found or is associated with an incorrect resource
  200. * type, -EINVAL will be returned.
  201. */
  202. int vmw_user_resource_lookup_handle(struct vmw_private *dev_priv,
  203. struct ttm_object_file *tfile,
  204. uint32_t handle,
  205. const struct vmw_user_resource_conv
  206. *converter,
  207. struct vmw_resource **p_res)
  208. {
  209. struct ttm_base_object *base;
  210. struct vmw_resource *res;
  211. int ret = -EINVAL;
  212. base = ttm_base_object_lookup(tfile, handle);
  213. if (unlikely(base == NULL))
  214. return -EINVAL;
  215. if (unlikely(ttm_base_object_type(base) != converter->object_type))
  216. goto out_bad_resource;
  217. res = converter->base_obj_to_res(base);
  218. read_lock(&dev_priv->resource_lock);
  219. if (!res->avail || res->res_free != converter->res_free) {
  220. read_unlock(&dev_priv->resource_lock);
  221. goto out_bad_resource;
  222. }
  223. kref_get(&res->kref);
  224. read_unlock(&dev_priv->resource_lock);
  225. *p_res = res;
  226. ret = 0;
  227. out_bad_resource:
  228. ttm_base_object_unref(&base);
  229. return ret;
  230. }
  231. /**
  232. * Helper function that looks either a surface or bo.
  233. *
  234. * The pointer this pointed at by out_surf and out_buf needs to be null.
  235. */
  236. int vmw_user_lookup_handle(struct vmw_private *dev_priv,
  237. struct ttm_object_file *tfile,
  238. uint32_t handle,
  239. struct vmw_surface **out_surf,
  240. struct vmw_buffer_object **out_buf)
  241. {
  242. struct vmw_resource *res;
  243. int ret;
  244. BUG_ON(*out_surf || *out_buf);
  245. ret = vmw_user_resource_lookup_handle(dev_priv, tfile, handle,
  246. user_surface_converter,
  247. &res);
  248. if (!ret) {
  249. *out_surf = vmw_res_to_srf(res);
  250. return 0;
  251. }
  252. *out_surf = NULL;
  253. ret = vmw_user_bo_lookup(tfile, handle, out_buf, NULL);
  254. return ret;
  255. }
  256. /**
  257. * vmw_resource_buf_alloc - Allocate a backup buffer for a resource.
  258. *
  259. * @res: The resource for which to allocate a backup buffer.
  260. * @interruptible: Whether any sleeps during allocation should be
  261. * performed while interruptible.
  262. */
  263. static int vmw_resource_buf_alloc(struct vmw_resource *res,
  264. bool interruptible)
  265. {
  266. unsigned long size =
  267. (res->backup_size + PAGE_SIZE - 1) & PAGE_MASK;
  268. struct vmw_buffer_object *backup;
  269. int ret;
  270. if (likely(res->backup)) {
  271. BUG_ON(res->backup->base.num_pages * PAGE_SIZE < size);
  272. return 0;
  273. }
  274. backup = kzalloc(sizeof(*backup), GFP_KERNEL);
  275. if (unlikely(!backup))
  276. return -ENOMEM;
  277. ret = vmw_bo_init(res->dev_priv, backup, res->backup_size,
  278. res->func->backup_placement,
  279. interruptible,
  280. &vmw_bo_bo_free);
  281. if (unlikely(ret != 0))
  282. goto out_no_bo;
  283. res->backup = backup;
  284. out_no_bo:
  285. return ret;
  286. }
  287. /**
  288. * vmw_resource_do_validate - Make a resource up-to-date and visible
  289. * to the device.
  290. *
  291. * @res: The resource to make visible to the device.
  292. * @val_buf: Information about a buffer possibly
  293. * containing backup data if a bind operation is needed.
  294. *
  295. * On hardware resource shortage, this function returns -EBUSY and
  296. * should be retried once resources have been freed up.
  297. */
  298. static int vmw_resource_do_validate(struct vmw_resource *res,
  299. struct ttm_validate_buffer *val_buf)
  300. {
  301. int ret = 0;
  302. const struct vmw_res_func *func = res->func;
  303. if (unlikely(res->id == -1)) {
  304. ret = func->create(res);
  305. if (unlikely(ret != 0))
  306. return ret;
  307. }
  308. if (func->bind &&
  309. ((func->needs_backup && list_empty(&res->mob_head) &&
  310. val_buf->bo != NULL) ||
  311. (!func->needs_backup && val_buf->bo != NULL))) {
  312. ret = func->bind(res, val_buf);
  313. if (unlikely(ret != 0))
  314. goto out_bind_failed;
  315. if (func->needs_backup)
  316. list_add_tail(&res->mob_head, &res->backup->res_list);
  317. }
  318. /*
  319. * Only do this on write operations, and move to
  320. * vmw_resource_unreserve if it can be called after
  321. * backup buffers have been unreserved. Otherwise
  322. * sort out locking.
  323. */
  324. res->res_dirty = true;
  325. return 0;
  326. out_bind_failed:
  327. func->destroy(res);
  328. return ret;
  329. }
  330. /**
  331. * vmw_resource_unreserve - Unreserve a resource previously reserved for
  332. * command submission.
  333. *
  334. * @res: Pointer to the struct vmw_resource to unreserve.
  335. * @switch_backup: Backup buffer has been switched.
  336. * @new_backup: Pointer to new backup buffer if command submission
  337. * switched. May be NULL.
  338. * @new_backup_offset: New backup offset if @switch_backup is true.
  339. *
  340. * Currently unreserving a resource means putting it back on the device's
  341. * resource lru list, so that it can be evicted if necessary.
  342. */
  343. void vmw_resource_unreserve(struct vmw_resource *res,
  344. bool switch_backup,
  345. struct vmw_buffer_object *new_backup,
  346. unsigned long new_backup_offset)
  347. {
  348. struct vmw_private *dev_priv = res->dev_priv;
  349. if (!list_empty(&res->lru_head))
  350. return;
  351. if (switch_backup && new_backup != res->backup) {
  352. if (res->backup) {
  353. lockdep_assert_held(&res->backup->base.resv->lock.base);
  354. list_del_init(&res->mob_head);
  355. vmw_bo_unreference(&res->backup);
  356. }
  357. if (new_backup) {
  358. res->backup = vmw_bo_reference(new_backup);
  359. lockdep_assert_held(&new_backup->base.resv->lock.base);
  360. list_add_tail(&res->mob_head, &new_backup->res_list);
  361. } else {
  362. res->backup = NULL;
  363. }
  364. }
  365. if (switch_backup)
  366. res->backup_offset = new_backup_offset;
  367. if (!res->func->may_evict || res->id == -1 || res->pin_count)
  368. return;
  369. write_lock(&dev_priv->resource_lock);
  370. list_add_tail(&res->lru_head,
  371. &res->dev_priv->res_lru[res->func->res_type]);
  372. write_unlock(&dev_priv->resource_lock);
  373. }
  374. /**
  375. * vmw_resource_check_buffer - Check whether a backup buffer is needed
  376. * for a resource and in that case, allocate
  377. * one, reserve and validate it.
  378. *
  379. * @ticket: The ww aqcquire context to use, or NULL if trylocking.
  380. * @res: The resource for which to allocate a backup buffer.
  381. * @interruptible: Whether any sleeps during allocation should be
  382. * performed while interruptible.
  383. * @val_buf: On successful return contains data about the
  384. * reserved and validated backup buffer.
  385. */
  386. static int
  387. vmw_resource_check_buffer(struct ww_acquire_ctx *ticket,
  388. struct vmw_resource *res,
  389. bool interruptible,
  390. struct ttm_validate_buffer *val_buf)
  391. {
  392. struct ttm_operation_ctx ctx = { true, false };
  393. struct list_head val_list;
  394. bool backup_dirty = false;
  395. int ret;
  396. if (unlikely(res->backup == NULL)) {
  397. ret = vmw_resource_buf_alloc(res, interruptible);
  398. if (unlikely(ret != 0))
  399. return ret;
  400. }
  401. INIT_LIST_HEAD(&val_list);
  402. val_buf->bo = ttm_bo_reference(&res->backup->base);
  403. val_buf->shared = false;
  404. list_add_tail(&val_buf->head, &val_list);
  405. ret = ttm_eu_reserve_buffers(ticket, &val_list, interruptible, NULL);
  406. if (unlikely(ret != 0))
  407. goto out_no_reserve;
  408. if (res->func->needs_backup && list_empty(&res->mob_head))
  409. return 0;
  410. backup_dirty = res->backup_dirty;
  411. ret = ttm_bo_validate(&res->backup->base,
  412. res->func->backup_placement,
  413. &ctx);
  414. if (unlikely(ret != 0))
  415. goto out_no_validate;
  416. return 0;
  417. out_no_validate:
  418. ttm_eu_backoff_reservation(ticket, &val_list);
  419. out_no_reserve:
  420. ttm_bo_unref(&val_buf->bo);
  421. if (backup_dirty)
  422. vmw_bo_unreference(&res->backup);
  423. return ret;
  424. }
  425. /**
  426. * vmw_resource_reserve - Reserve a resource for command submission
  427. *
  428. * @res: The resource to reserve.
  429. *
  430. * This function takes the resource off the LRU list and make sure
  431. * a backup buffer is present for guest-backed resources. However,
  432. * the buffer may not be bound to the resource at this point.
  433. *
  434. */
  435. int vmw_resource_reserve(struct vmw_resource *res, bool interruptible,
  436. bool no_backup)
  437. {
  438. struct vmw_private *dev_priv = res->dev_priv;
  439. int ret;
  440. write_lock(&dev_priv->resource_lock);
  441. list_del_init(&res->lru_head);
  442. write_unlock(&dev_priv->resource_lock);
  443. if (res->func->needs_backup && res->backup == NULL &&
  444. !no_backup) {
  445. ret = vmw_resource_buf_alloc(res, interruptible);
  446. if (unlikely(ret != 0)) {
  447. DRM_ERROR("Failed to allocate a backup buffer "
  448. "of size %lu. bytes\n",
  449. (unsigned long) res->backup_size);
  450. return ret;
  451. }
  452. }
  453. return 0;
  454. }
  455. /**
  456. * vmw_resource_backoff_reservation - Unreserve and unreference a
  457. * backup buffer
  458. *.
  459. * @ticket: The ww acquire ctx used for reservation.
  460. * @val_buf: Backup buffer information.
  461. */
  462. static void
  463. vmw_resource_backoff_reservation(struct ww_acquire_ctx *ticket,
  464. struct ttm_validate_buffer *val_buf)
  465. {
  466. struct list_head val_list;
  467. if (likely(val_buf->bo == NULL))
  468. return;
  469. INIT_LIST_HEAD(&val_list);
  470. list_add_tail(&val_buf->head, &val_list);
  471. ttm_eu_backoff_reservation(ticket, &val_list);
  472. ttm_bo_unref(&val_buf->bo);
  473. }
  474. /**
  475. * vmw_resource_do_evict - Evict a resource, and transfer its data
  476. * to a backup buffer.
  477. *
  478. * @ticket: The ww acquire ticket to use, or NULL if trylocking.
  479. * @res: The resource to evict.
  480. * @interruptible: Whether to wait interruptible.
  481. */
  482. static int vmw_resource_do_evict(struct ww_acquire_ctx *ticket,
  483. struct vmw_resource *res, bool interruptible)
  484. {
  485. struct ttm_validate_buffer val_buf;
  486. const struct vmw_res_func *func = res->func;
  487. int ret;
  488. BUG_ON(!func->may_evict);
  489. val_buf.bo = NULL;
  490. val_buf.shared = false;
  491. ret = vmw_resource_check_buffer(ticket, res, interruptible, &val_buf);
  492. if (unlikely(ret != 0))
  493. return ret;
  494. if (unlikely(func->unbind != NULL &&
  495. (!func->needs_backup || !list_empty(&res->mob_head)))) {
  496. ret = func->unbind(res, res->res_dirty, &val_buf);
  497. if (unlikely(ret != 0))
  498. goto out_no_unbind;
  499. list_del_init(&res->mob_head);
  500. }
  501. ret = func->destroy(res);
  502. res->backup_dirty = true;
  503. res->res_dirty = false;
  504. out_no_unbind:
  505. vmw_resource_backoff_reservation(ticket, &val_buf);
  506. return ret;
  507. }
  508. /**
  509. * vmw_resource_validate - Make a resource up-to-date and visible
  510. * to the device.
  511. *
  512. * @res: The resource to make visible to the device.
  513. *
  514. * On succesful return, any backup DMA buffer pointed to by @res->backup will
  515. * be reserved and validated.
  516. * On hardware resource shortage, this function will repeatedly evict
  517. * resources of the same type until the validation succeeds.
  518. */
  519. int vmw_resource_validate(struct vmw_resource *res)
  520. {
  521. int ret;
  522. struct vmw_resource *evict_res;
  523. struct vmw_private *dev_priv = res->dev_priv;
  524. struct list_head *lru_list = &dev_priv->res_lru[res->func->res_type];
  525. struct ttm_validate_buffer val_buf;
  526. unsigned err_count = 0;
  527. if (!res->func->create)
  528. return 0;
  529. val_buf.bo = NULL;
  530. val_buf.shared = false;
  531. if (res->backup)
  532. val_buf.bo = &res->backup->base;
  533. do {
  534. ret = vmw_resource_do_validate(res, &val_buf);
  535. if (likely(ret != -EBUSY))
  536. break;
  537. write_lock(&dev_priv->resource_lock);
  538. if (list_empty(lru_list) || !res->func->may_evict) {
  539. DRM_ERROR("Out of device device resources "
  540. "for %s.\n", res->func->type_name);
  541. ret = -EBUSY;
  542. write_unlock(&dev_priv->resource_lock);
  543. break;
  544. }
  545. evict_res = vmw_resource_reference
  546. (list_first_entry(lru_list, struct vmw_resource,
  547. lru_head));
  548. list_del_init(&evict_res->lru_head);
  549. write_unlock(&dev_priv->resource_lock);
  550. /* Trylock backup buffers with a NULL ticket. */
  551. ret = vmw_resource_do_evict(NULL, evict_res, true);
  552. if (unlikely(ret != 0)) {
  553. write_lock(&dev_priv->resource_lock);
  554. list_add_tail(&evict_res->lru_head, lru_list);
  555. write_unlock(&dev_priv->resource_lock);
  556. if (ret == -ERESTARTSYS ||
  557. ++err_count > VMW_RES_EVICT_ERR_COUNT) {
  558. vmw_resource_unreference(&evict_res);
  559. goto out_no_validate;
  560. }
  561. }
  562. vmw_resource_unreference(&evict_res);
  563. } while (1);
  564. if (unlikely(ret != 0))
  565. goto out_no_validate;
  566. else if (!res->func->needs_backup && res->backup) {
  567. list_del_init(&res->mob_head);
  568. vmw_bo_unreference(&res->backup);
  569. }
  570. return 0;
  571. out_no_validate:
  572. return ret;
  573. }
  574. /**
  575. * vmw_resource_unbind_list
  576. *
  577. * @vbo: Pointer to the current backing MOB.
  578. *
  579. * Evicts the Guest Backed hardware resource if the backup
  580. * buffer is being moved out of MOB memory.
  581. * Note that this function will not race with the resource
  582. * validation code, since resource validation and eviction
  583. * both require the backup buffer to be reserved.
  584. */
  585. void vmw_resource_unbind_list(struct vmw_buffer_object *vbo)
  586. {
  587. struct vmw_resource *res, *next;
  588. struct ttm_validate_buffer val_buf = {
  589. .bo = &vbo->base,
  590. .shared = false
  591. };
  592. lockdep_assert_held(&vbo->base.resv->lock.base);
  593. list_for_each_entry_safe(res, next, &vbo->res_list, mob_head) {
  594. if (!res->func->unbind)
  595. continue;
  596. (void) res->func->unbind(res, true, &val_buf);
  597. res->backup_dirty = true;
  598. res->res_dirty = false;
  599. list_del_init(&res->mob_head);
  600. }
  601. (void) ttm_bo_wait(&vbo->base, false, false);
  602. }
  603. /**
  604. * vmw_query_readback_all - Read back cached query states
  605. *
  606. * @dx_query_mob: Buffer containing the DX query MOB
  607. *
  608. * Read back cached states from the device if they exist. This function
  609. * assumings binding_mutex is held.
  610. */
  611. int vmw_query_readback_all(struct vmw_buffer_object *dx_query_mob)
  612. {
  613. struct vmw_resource *dx_query_ctx;
  614. struct vmw_private *dev_priv;
  615. struct {
  616. SVGA3dCmdHeader header;
  617. SVGA3dCmdDXReadbackAllQuery body;
  618. } *cmd;
  619. /* No query bound, so do nothing */
  620. if (!dx_query_mob || !dx_query_mob->dx_query_ctx)
  621. return 0;
  622. dx_query_ctx = dx_query_mob->dx_query_ctx;
  623. dev_priv = dx_query_ctx->dev_priv;
  624. cmd = vmw_fifo_reserve_dx(dev_priv, sizeof(*cmd), dx_query_ctx->id);
  625. if (unlikely(cmd == NULL)) {
  626. DRM_ERROR("Failed reserving FIFO space for "
  627. "query MOB read back.\n");
  628. return -ENOMEM;
  629. }
  630. cmd->header.id = SVGA_3D_CMD_DX_READBACK_ALL_QUERY;
  631. cmd->header.size = sizeof(cmd->body);
  632. cmd->body.cid = dx_query_ctx->id;
  633. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  634. /* Triggers a rebind the next time affected context is bound */
  635. dx_query_mob->dx_query_ctx = NULL;
  636. return 0;
  637. }
  638. /**
  639. * vmw_query_move_notify - Read back cached query states
  640. *
  641. * @bo: The TTM buffer object about to move.
  642. * @mem: The memory region @bo is moving to.
  643. *
  644. * Called before the query MOB is swapped out to read back cached query
  645. * states from the device.
  646. */
  647. void vmw_query_move_notify(struct ttm_buffer_object *bo,
  648. struct ttm_mem_reg *mem)
  649. {
  650. struct vmw_buffer_object *dx_query_mob;
  651. struct ttm_bo_device *bdev = bo->bdev;
  652. struct vmw_private *dev_priv;
  653. dev_priv = container_of(bdev, struct vmw_private, bdev);
  654. mutex_lock(&dev_priv->binding_mutex);
  655. dx_query_mob = container_of(bo, struct vmw_buffer_object, base);
  656. if (mem == NULL || !dx_query_mob || !dx_query_mob->dx_query_ctx) {
  657. mutex_unlock(&dev_priv->binding_mutex);
  658. return;
  659. }
  660. /* If BO is being moved from MOB to system memory */
  661. if (mem->mem_type == TTM_PL_SYSTEM && bo->mem.mem_type == VMW_PL_MOB) {
  662. struct vmw_fence_obj *fence;
  663. (void) vmw_query_readback_all(dx_query_mob);
  664. mutex_unlock(&dev_priv->binding_mutex);
  665. /* Create a fence and attach the BO to it */
  666. (void) vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
  667. vmw_bo_fence_single(bo, fence);
  668. if (fence != NULL)
  669. vmw_fence_obj_unreference(&fence);
  670. (void) ttm_bo_wait(bo, false, false);
  671. } else
  672. mutex_unlock(&dev_priv->binding_mutex);
  673. }
  674. /**
  675. * vmw_resource_needs_backup - Return whether a resource needs a backup buffer.
  676. *
  677. * @res: The resource being queried.
  678. */
  679. bool vmw_resource_needs_backup(const struct vmw_resource *res)
  680. {
  681. return res->func->needs_backup;
  682. }
  683. /**
  684. * vmw_resource_evict_type - Evict all resources of a specific type
  685. *
  686. * @dev_priv: Pointer to a device private struct
  687. * @type: The resource type to evict
  688. *
  689. * To avoid thrashing starvation or as part of the hibernation sequence,
  690. * try to evict all evictable resources of a specific type.
  691. */
  692. static void vmw_resource_evict_type(struct vmw_private *dev_priv,
  693. enum vmw_res_type type)
  694. {
  695. struct list_head *lru_list = &dev_priv->res_lru[type];
  696. struct vmw_resource *evict_res;
  697. unsigned err_count = 0;
  698. int ret;
  699. struct ww_acquire_ctx ticket;
  700. do {
  701. write_lock(&dev_priv->resource_lock);
  702. if (list_empty(lru_list))
  703. goto out_unlock;
  704. evict_res = vmw_resource_reference(
  705. list_first_entry(lru_list, struct vmw_resource,
  706. lru_head));
  707. list_del_init(&evict_res->lru_head);
  708. write_unlock(&dev_priv->resource_lock);
  709. /* Wait lock backup buffers with a ticket. */
  710. ret = vmw_resource_do_evict(&ticket, evict_res, false);
  711. if (unlikely(ret != 0)) {
  712. write_lock(&dev_priv->resource_lock);
  713. list_add_tail(&evict_res->lru_head, lru_list);
  714. write_unlock(&dev_priv->resource_lock);
  715. if (++err_count > VMW_RES_EVICT_ERR_COUNT) {
  716. vmw_resource_unreference(&evict_res);
  717. return;
  718. }
  719. }
  720. vmw_resource_unreference(&evict_res);
  721. } while (1);
  722. out_unlock:
  723. write_unlock(&dev_priv->resource_lock);
  724. }
  725. /**
  726. * vmw_resource_evict_all - Evict all evictable resources
  727. *
  728. * @dev_priv: Pointer to a device private struct
  729. *
  730. * To avoid thrashing starvation or as part of the hibernation sequence,
  731. * evict all evictable resources. In particular this means that all
  732. * guest-backed resources that are registered with the device are
  733. * evicted and the OTable becomes clean.
  734. */
  735. void vmw_resource_evict_all(struct vmw_private *dev_priv)
  736. {
  737. enum vmw_res_type type;
  738. mutex_lock(&dev_priv->cmdbuf_mutex);
  739. for (type = 0; type < vmw_res_max; ++type)
  740. vmw_resource_evict_type(dev_priv, type);
  741. mutex_unlock(&dev_priv->cmdbuf_mutex);
  742. }
  743. /**
  744. * vmw_resource_pin - Add a pin reference on a resource
  745. *
  746. * @res: The resource to add a pin reference on
  747. *
  748. * This function adds a pin reference, and if needed validates the resource.
  749. * Having a pin reference means that the resource can never be evicted, and
  750. * its id will never change as long as there is a pin reference.
  751. * This function returns 0 on success and a negative error code on failure.
  752. */
  753. int vmw_resource_pin(struct vmw_resource *res, bool interruptible)
  754. {
  755. struct ttm_operation_ctx ctx = { interruptible, false };
  756. struct vmw_private *dev_priv = res->dev_priv;
  757. int ret;
  758. ttm_write_lock(&dev_priv->reservation_sem, interruptible);
  759. mutex_lock(&dev_priv->cmdbuf_mutex);
  760. ret = vmw_resource_reserve(res, interruptible, false);
  761. if (ret)
  762. goto out_no_reserve;
  763. if (res->pin_count == 0) {
  764. struct vmw_buffer_object *vbo = NULL;
  765. if (res->backup) {
  766. vbo = res->backup;
  767. ttm_bo_reserve(&vbo->base, interruptible, false, NULL);
  768. if (!vbo->pin_count) {
  769. ret = ttm_bo_validate
  770. (&vbo->base,
  771. res->func->backup_placement,
  772. &ctx);
  773. if (ret) {
  774. ttm_bo_unreserve(&vbo->base);
  775. goto out_no_validate;
  776. }
  777. }
  778. /* Do we really need to pin the MOB as well? */
  779. vmw_bo_pin_reserved(vbo, true);
  780. }
  781. ret = vmw_resource_validate(res);
  782. if (vbo)
  783. ttm_bo_unreserve(&vbo->base);
  784. if (ret)
  785. goto out_no_validate;
  786. }
  787. res->pin_count++;
  788. out_no_validate:
  789. vmw_resource_unreserve(res, false, NULL, 0UL);
  790. out_no_reserve:
  791. mutex_unlock(&dev_priv->cmdbuf_mutex);
  792. ttm_write_unlock(&dev_priv->reservation_sem);
  793. return ret;
  794. }
  795. /**
  796. * vmw_resource_unpin - Remove a pin reference from a resource
  797. *
  798. * @res: The resource to remove a pin reference from
  799. *
  800. * Having a pin reference means that the resource can never be evicted, and
  801. * its id will never change as long as there is a pin reference.
  802. */
  803. void vmw_resource_unpin(struct vmw_resource *res)
  804. {
  805. struct vmw_private *dev_priv = res->dev_priv;
  806. int ret;
  807. (void) ttm_read_lock(&dev_priv->reservation_sem, false);
  808. mutex_lock(&dev_priv->cmdbuf_mutex);
  809. ret = vmw_resource_reserve(res, false, true);
  810. WARN_ON(ret);
  811. WARN_ON(res->pin_count == 0);
  812. if (--res->pin_count == 0 && res->backup) {
  813. struct vmw_buffer_object *vbo = res->backup;
  814. (void) ttm_bo_reserve(&vbo->base, false, false, NULL);
  815. vmw_bo_pin_reserved(vbo, false);
  816. ttm_bo_unreserve(&vbo->base);
  817. }
  818. vmw_resource_unreserve(res, false, NULL, 0UL);
  819. mutex_unlock(&dev_priv->cmdbuf_mutex);
  820. ttm_read_unlock(&dev_priv->reservation_sem);
  821. }
  822. /**
  823. * vmw_res_type - Return the resource type
  824. *
  825. * @res: Pointer to the resource
  826. */
  827. enum vmw_res_type vmw_res_type(const struct vmw_resource *res)
  828. {
  829. return res->func->res_type;
  830. }