vmwgfx_resource.c 27 KB

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