vmwgfx_resource.c 25 KB

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