vmwgfx_resource.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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 <drm/ttm/ttm_placement.h>
  28. #include "vmwgfx_resource_priv.h"
  29. #include "vmwgfx_binding.h"
  30. #include "vmwgfx_drv.h"
  31. #define VMW_RES_EVICT_ERR_COUNT 10
  32. /**
  33. * vmw_resource_mob_attach - Mark a resource as attached to its backing mob
  34. * @res: The resource
  35. */
  36. void vmw_resource_mob_attach(struct vmw_resource *res)
  37. {
  38. struct vmw_buffer_object *backup = res->backup;
  39. struct rb_node **new = &backup->res_tree.rb_node, *parent = NULL;
  40. dma_resv_assert_held(res->backup->base.base.resv);
  41. res->used_prio = (res->res_dirty) ? res->func->dirty_prio :
  42. res->func->prio;
  43. while (*new) {
  44. struct vmw_resource *this =
  45. container_of(*new, struct vmw_resource, mob_node);
  46. parent = *new;
  47. new = (res->backup_offset < this->backup_offset) ?
  48. &((*new)->rb_left) : &((*new)->rb_right);
  49. }
  50. rb_link_node(&res->mob_node, parent, new);
  51. rb_insert_color(&res->mob_node, &backup->res_tree);
  52. vmw_bo_prio_add(backup, res->used_prio);
  53. }
  54. /**
  55. * vmw_resource_mob_detach - Mark a resource as detached from its backing mob
  56. * @res: The resource
  57. */
  58. void vmw_resource_mob_detach(struct vmw_resource *res)
  59. {
  60. struct vmw_buffer_object *backup = res->backup;
  61. dma_resv_assert_held(backup->base.base.resv);
  62. if (vmw_resource_mob_attached(res)) {
  63. rb_erase(&res->mob_node, &backup->res_tree);
  64. RB_CLEAR_NODE(&res->mob_node);
  65. vmw_bo_prio_del(backup, res->used_prio);
  66. }
  67. }
  68. struct vmw_resource *vmw_resource_reference(struct vmw_resource *res)
  69. {
  70. kref_get(&res->kref);
  71. return res;
  72. }
  73. struct vmw_resource *
  74. vmw_resource_reference_unless_doomed(struct vmw_resource *res)
  75. {
  76. return kref_get_unless_zero(&res->kref) ? res : NULL;
  77. }
  78. /**
  79. * vmw_resource_release_id - release a resource id to the id manager.
  80. *
  81. * @res: Pointer to the resource.
  82. *
  83. * Release the resource id to the resource id manager and set it to -1
  84. */
  85. void vmw_resource_release_id(struct vmw_resource *res)
  86. {
  87. struct vmw_private *dev_priv = res->dev_priv;
  88. struct idr *idr = &dev_priv->res_idr[res->func->res_type];
  89. spin_lock(&dev_priv->resource_lock);
  90. if (res->id != -1)
  91. idr_remove(idr, res->id);
  92. res->id = -1;
  93. spin_unlock(&dev_priv->resource_lock);
  94. }
  95. static void vmw_resource_release(struct kref *kref)
  96. {
  97. struct vmw_resource *res =
  98. container_of(kref, struct vmw_resource, kref);
  99. struct vmw_private *dev_priv = res->dev_priv;
  100. int id;
  101. int ret;
  102. struct idr *idr = &dev_priv->res_idr[res->func->res_type];
  103. spin_lock(&dev_priv->resource_lock);
  104. list_del_init(&res->lru_head);
  105. spin_unlock(&dev_priv->resource_lock);
  106. if (res->backup) {
  107. struct ttm_buffer_object *bo = &res->backup->base;
  108. ret = ttm_bo_reserve(bo, false, false, NULL);
  109. BUG_ON(ret);
  110. if (vmw_resource_mob_attached(res) &&
  111. res->func->unbind != NULL) {
  112. struct ttm_validate_buffer val_buf;
  113. val_buf.bo = bo;
  114. val_buf.num_shared = 0;
  115. res->func->unbind(res, false, &val_buf);
  116. }
  117. res->backup_dirty = false;
  118. vmw_resource_mob_detach(res);
  119. if (res->dirty)
  120. res->func->dirty_free(res);
  121. if (res->coherent)
  122. vmw_bo_dirty_release(res->backup);
  123. ttm_bo_unreserve(bo);
  124. vmw_bo_unreference(&res->backup);
  125. }
  126. if (likely(res->hw_destroy != NULL)) {
  127. mutex_lock(&dev_priv->binding_mutex);
  128. vmw_binding_res_list_kill(&res->binding_head);
  129. mutex_unlock(&dev_priv->binding_mutex);
  130. res->hw_destroy(res);
  131. }
  132. id = res->id;
  133. if (res->res_free != NULL)
  134. res->res_free(res);
  135. else
  136. kfree(res);
  137. spin_lock(&dev_priv->resource_lock);
  138. if (id != -1)
  139. idr_remove(idr, id);
  140. spin_unlock(&dev_priv->resource_lock);
  141. }
  142. void vmw_resource_unreference(struct vmw_resource **p_res)
  143. {
  144. struct vmw_resource *res = *p_res;
  145. *p_res = NULL;
  146. kref_put(&res->kref, vmw_resource_release);
  147. }
  148. /**
  149. * vmw_resource_alloc_id - release a resource id to the id manager.
  150. *
  151. * @res: Pointer to the resource.
  152. *
  153. * Allocate the lowest free resource from the resource manager, and set
  154. * @res->id to that id. Returns 0 on success and -ENOMEM on failure.
  155. */
  156. int vmw_resource_alloc_id(struct vmw_resource *res)
  157. {
  158. struct vmw_private *dev_priv = res->dev_priv;
  159. int ret;
  160. struct idr *idr = &dev_priv->res_idr[res->func->res_type];
  161. BUG_ON(res->id != -1);
  162. idr_preload(GFP_KERNEL);
  163. spin_lock(&dev_priv->resource_lock);
  164. ret = idr_alloc(idr, res, 1, 0, GFP_NOWAIT);
  165. if (ret >= 0)
  166. res->id = ret;
  167. spin_unlock(&dev_priv->resource_lock);
  168. idr_preload_end();
  169. return ret < 0 ? ret : 0;
  170. }
  171. /**
  172. * vmw_resource_init - initialize a struct vmw_resource
  173. *
  174. * @dev_priv: Pointer to a device private struct.
  175. * @res: The struct vmw_resource to initialize.
  176. * @delay_id: Boolean whether to defer device id allocation until
  177. * the first validation.
  178. * @res_free: Resource destructor.
  179. * @func: Resource function table.
  180. */
  181. int vmw_resource_init(struct vmw_private *dev_priv, struct vmw_resource *res,
  182. bool delay_id,
  183. void (*res_free) (struct vmw_resource *res),
  184. const struct vmw_res_func *func)
  185. {
  186. kref_init(&res->kref);
  187. res->hw_destroy = NULL;
  188. res->res_free = res_free;
  189. res->dev_priv = dev_priv;
  190. res->func = func;
  191. RB_CLEAR_NODE(&res->mob_node);
  192. INIT_LIST_HEAD(&res->lru_head);
  193. INIT_LIST_HEAD(&res->binding_head);
  194. res->id = -1;
  195. res->backup = NULL;
  196. res->backup_offset = 0;
  197. res->backup_dirty = false;
  198. res->res_dirty = false;
  199. res->coherent = false;
  200. res->used_prio = 3;
  201. res->dirty = NULL;
  202. if (delay_id)
  203. return 0;
  204. else
  205. return vmw_resource_alloc_id(res);
  206. }
  207. /**
  208. * vmw_user_resource_lookup_handle - lookup a struct resource from a
  209. * TTM user-space handle and perform basic type checks
  210. *
  211. * @dev_priv: Pointer to a device private struct
  212. * @tfile: Pointer to a struct ttm_object_file identifying the caller
  213. * @handle: The TTM user-space handle
  214. * @converter: Pointer to an object describing the resource type
  215. * @p_res: On successful return the location pointed to will contain
  216. * a pointer to a refcounted struct vmw_resource.
  217. *
  218. * If the handle can't be found or is associated with an incorrect resource
  219. * type, -EINVAL will be returned.
  220. */
  221. int vmw_user_resource_lookup_handle(struct vmw_private *dev_priv,
  222. struct ttm_object_file *tfile,
  223. uint32_t handle,
  224. const struct vmw_user_resource_conv
  225. *converter,
  226. struct vmw_resource **p_res)
  227. {
  228. struct ttm_base_object *base;
  229. struct vmw_resource *res;
  230. int ret = -EINVAL;
  231. base = ttm_base_object_lookup(tfile, handle);
  232. if (unlikely(base == NULL))
  233. return -EINVAL;
  234. if (unlikely(ttm_base_object_type(base) != converter->object_type))
  235. goto out_bad_resource;
  236. res = converter->base_obj_to_res(base);
  237. kref_get(&res->kref);
  238. *p_res = res;
  239. ret = 0;
  240. out_bad_resource:
  241. ttm_base_object_unref(&base);
  242. return ret;
  243. }
  244. /*
  245. * Helper function that looks either a surface or bo.
  246. *
  247. * The pointer this pointed at by out_surf and out_buf needs to be null.
  248. */
  249. int vmw_user_lookup_handle(struct vmw_private *dev_priv,
  250. struct drm_file *filp,
  251. uint32_t handle,
  252. struct vmw_surface **out_surf,
  253. struct vmw_buffer_object **out_buf)
  254. {
  255. struct ttm_object_file *tfile = vmw_fpriv(filp)->tfile;
  256. struct vmw_resource *res;
  257. int ret;
  258. BUG_ON(*out_surf || *out_buf);
  259. ret = vmw_user_resource_lookup_handle(dev_priv, tfile, handle,
  260. user_surface_converter,
  261. &res);
  262. if (!ret) {
  263. *out_surf = vmw_res_to_srf(res);
  264. return 0;
  265. }
  266. *out_surf = NULL;
  267. ret = vmw_user_bo_lookup(filp, handle, out_buf);
  268. return ret;
  269. }
  270. /**
  271. * vmw_resource_buf_alloc - Allocate a backup buffer for a resource.
  272. *
  273. * @res: The resource for which to allocate a backup buffer.
  274. * @interruptible: Whether any sleeps during allocation should be
  275. * performed while interruptible.
  276. */
  277. static int vmw_resource_buf_alloc(struct vmw_resource *res,
  278. bool interruptible)
  279. {
  280. unsigned long size = PFN_ALIGN(res->backup_size);
  281. struct vmw_buffer_object *backup;
  282. int ret;
  283. if (likely(res->backup)) {
  284. BUG_ON(res->backup->base.base.size < size);
  285. return 0;
  286. }
  287. ret = vmw_bo_create(res->dev_priv, res->backup_size,
  288. res->func->backup_placement,
  289. interruptible, false,
  290. &vmw_bo_bo_free, &backup);
  291. if (unlikely(ret != 0))
  292. goto out_no_bo;
  293. res->backup = backup;
  294. out_no_bo:
  295. return ret;
  296. }
  297. /**
  298. * vmw_resource_do_validate - Make a resource up-to-date and visible
  299. * to the device.
  300. *
  301. * @res: The resource to make visible to the device.
  302. * @val_buf: Information about a buffer possibly
  303. * containing backup data if a bind operation is needed.
  304. * @dirtying: Transfer dirty regions.
  305. *
  306. * On hardware resource shortage, this function returns -EBUSY and
  307. * should be retried once resources have been freed up.
  308. */
  309. static int vmw_resource_do_validate(struct vmw_resource *res,
  310. struct ttm_validate_buffer *val_buf,
  311. bool dirtying)
  312. {
  313. int ret = 0;
  314. const struct vmw_res_func *func = res->func;
  315. if (unlikely(res->id == -1)) {
  316. ret = func->create(res);
  317. if (unlikely(ret != 0))
  318. return ret;
  319. }
  320. if (func->bind &&
  321. ((func->needs_backup && !vmw_resource_mob_attached(res) &&
  322. val_buf->bo != NULL) ||
  323. (!func->needs_backup && val_buf->bo != NULL))) {
  324. ret = func->bind(res, val_buf);
  325. if (unlikely(ret != 0))
  326. goto out_bind_failed;
  327. if (func->needs_backup)
  328. vmw_resource_mob_attach(res);
  329. }
  330. /*
  331. * Handle the case where the backup mob is marked coherent but
  332. * the resource isn't.
  333. */
  334. if (func->dirty_alloc && vmw_resource_mob_attached(res) &&
  335. !res->coherent) {
  336. if (res->backup->dirty && !res->dirty) {
  337. ret = func->dirty_alloc(res);
  338. if (ret)
  339. return ret;
  340. } else if (!res->backup->dirty && res->dirty) {
  341. func->dirty_free(res);
  342. }
  343. }
  344. /*
  345. * Transfer the dirty regions to the resource and update
  346. * the resource.
  347. */
  348. if (res->dirty) {
  349. if (dirtying && !res->res_dirty) {
  350. pgoff_t start = res->backup_offset >> PAGE_SHIFT;
  351. pgoff_t end = __KERNEL_DIV_ROUND_UP
  352. (res->backup_offset + res->backup_size,
  353. PAGE_SIZE);
  354. vmw_bo_dirty_unmap(res->backup, start, end);
  355. }
  356. vmw_bo_dirty_transfer_to_res(res);
  357. return func->dirty_sync(res);
  358. }
  359. return 0;
  360. out_bind_failed:
  361. func->destroy(res);
  362. return ret;
  363. }
  364. /**
  365. * vmw_resource_unreserve - Unreserve a resource previously reserved for
  366. * command submission.
  367. *
  368. * @res: Pointer to the struct vmw_resource to unreserve.
  369. * @dirty_set: Change dirty status of the resource.
  370. * @dirty: When changing dirty status indicates the new status.
  371. * @switch_backup: Backup buffer has been switched.
  372. * @new_backup: Pointer to new backup buffer if command submission
  373. * switched. May be NULL.
  374. * @new_backup_offset: New backup offset if @switch_backup is true.
  375. *
  376. * Currently unreserving a resource means putting it back on the device's
  377. * resource lru list, so that it can be evicted if necessary.
  378. */
  379. void vmw_resource_unreserve(struct vmw_resource *res,
  380. bool dirty_set,
  381. bool dirty,
  382. bool switch_backup,
  383. struct vmw_buffer_object *new_backup,
  384. unsigned long new_backup_offset)
  385. {
  386. struct vmw_private *dev_priv = res->dev_priv;
  387. if (!list_empty(&res->lru_head))
  388. return;
  389. if (switch_backup && new_backup != res->backup) {
  390. if (res->backup) {
  391. vmw_resource_mob_detach(res);
  392. if (res->coherent)
  393. vmw_bo_dirty_release(res->backup);
  394. vmw_bo_unreference(&res->backup);
  395. }
  396. if (new_backup) {
  397. res->backup = vmw_bo_reference(new_backup);
  398. /*
  399. * The validation code should already have added a
  400. * dirty tracker here.
  401. */
  402. WARN_ON(res->coherent && !new_backup->dirty);
  403. vmw_resource_mob_attach(res);
  404. } else {
  405. res->backup = NULL;
  406. }
  407. } else if (switch_backup && res->coherent) {
  408. vmw_bo_dirty_release(res->backup);
  409. }
  410. if (switch_backup)
  411. res->backup_offset = new_backup_offset;
  412. if (dirty_set)
  413. res->res_dirty = dirty;
  414. if (!res->func->may_evict || res->id == -1 || res->pin_count)
  415. return;
  416. spin_lock(&dev_priv->resource_lock);
  417. list_add_tail(&res->lru_head,
  418. &res->dev_priv->res_lru[res->func->res_type]);
  419. spin_unlock(&dev_priv->resource_lock);
  420. }
  421. /**
  422. * vmw_resource_check_buffer - Check whether a backup buffer is needed
  423. * for a resource and in that case, allocate
  424. * one, reserve and validate it.
  425. *
  426. * @ticket: The ww acquire context to use, or NULL if trylocking.
  427. * @res: The resource for which to allocate a backup buffer.
  428. * @interruptible: Whether any sleeps during allocation should be
  429. * performed while interruptible.
  430. * @val_buf: On successful return contains data about the
  431. * reserved and validated backup buffer.
  432. */
  433. static int
  434. vmw_resource_check_buffer(struct ww_acquire_ctx *ticket,
  435. struct vmw_resource *res,
  436. bool interruptible,
  437. struct ttm_validate_buffer *val_buf)
  438. {
  439. struct ttm_operation_ctx ctx = { true, false };
  440. struct list_head val_list;
  441. bool backup_dirty = false;
  442. int ret;
  443. if (unlikely(res->backup == NULL)) {
  444. ret = vmw_resource_buf_alloc(res, interruptible);
  445. if (unlikely(ret != 0))
  446. return ret;
  447. }
  448. INIT_LIST_HEAD(&val_list);
  449. ttm_bo_get(&res->backup->base);
  450. val_buf->bo = &res->backup->base;
  451. val_buf->num_shared = 0;
  452. list_add_tail(&val_buf->head, &val_list);
  453. ret = ttm_eu_reserve_buffers(ticket, &val_list, interruptible, NULL);
  454. if (unlikely(ret != 0))
  455. goto out_no_reserve;
  456. if (res->func->needs_backup && !vmw_resource_mob_attached(res))
  457. return 0;
  458. backup_dirty = res->backup_dirty;
  459. ret = ttm_bo_validate(&res->backup->base,
  460. res->func->backup_placement,
  461. &ctx);
  462. if (unlikely(ret != 0))
  463. goto out_no_validate;
  464. return 0;
  465. out_no_validate:
  466. ttm_eu_backoff_reservation(ticket, &val_list);
  467. out_no_reserve:
  468. ttm_bo_put(val_buf->bo);
  469. val_buf->bo = NULL;
  470. if (backup_dirty)
  471. vmw_bo_unreference(&res->backup);
  472. return ret;
  473. }
  474. /*
  475. * vmw_resource_reserve - Reserve a resource for command submission
  476. *
  477. * @res: The resource to reserve.
  478. *
  479. * This function takes the resource off the LRU list and make sure
  480. * a backup buffer is present for guest-backed resources. However,
  481. * the buffer may not be bound to the resource at this point.
  482. *
  483. */
  484. int vmw_resource_reserve(struct vmw_resource *res, bool interruptible,
  485. bool no_backup)
  486. {
  487. struct vmw_private *dev_priv = res->dev_priv;
  488. int ret;
  489. spin_lock(&dev_priv->resource_lock);
  490. list_del_init(&res->lru_head);
  491. spin_unlock(&dev_priv->resource_lock);
  492. if (res->func->needs_backup && res->backup == NULL &&
  493. !no_backup) {
  494. ret = vmw_resource_buf_alloc(res, interruptible);
  495. if (unlikely(ret != 0)) {
  496. DRM_ERROR("Failed to allocate a backup buffer "
  497. "of size %lu. bytes\n",
  498. (unsigned long) res->backup_size);
  499. return ret;
  500. }
  501. }
  502. return 0;
  503. }
  504. /**
  505. * vmw_resource_backoff_reservation - Unreserve and unreference a
  506. * backup buffer
  507. *.
  508. * @ticket: The ww acquire ctx used for reservation.
  509. * @val_buf: Backup buffer information.
  510. */
  511. static void
  512. vmw_resource_backoff_reservation(struct ww_acquire_ctx *ticket,
  513. struct ttm_validate_buffer *val_buf)
  514. {
  515. struct list_head val_list;
  516. if (likely(val_buf->bo == NULL))
  517. return;
  518. INIT_LIST_HEAD(&val_list);
  519. list_add_tail(&val_buf->head, &val_list);
  520. ttm_eu_backoff_reservation(ticket, &val_list);
  521. ttm_bo_put(val_buf->bo);
  522. val_buf->bo = NULL;
  523. }
  524. /**
  525. * vmw_resource_do_evict - Evict a resource, and transfer its data
  526. * to a backup buffer.
  527. *
  528. * @ticket: The ww acquire ticket to use, or NULL if trylocking.
  529. * @res: The resource to evict.
  530. * @interruptible: Whether to wait interruptible.
  531. */
  532. static int vmw_resource_do_evict(struct ww_acquire_ctx *ticket,
  533. struct vmw_resource *res, bool interruptible)
  534. {
  535. struct ttm_validate_buffer val_buf;
  536. const struct vmw_res_func *func = res->func;
  537. int ret;
  538. BUG_ON(!func->may_evict);
  539. val_buf.bo = NULL;
  540. val_buf.num_shared = 0;
  541. ret = vmw_resource_check_buffer(ticket, res, interruptible, &val_buf);
  542. if (unlikely(ret != 0))
  543. return ret;
  544. if (unlikely(func->unbind != NULL &&
  545. (!func->needs_backup || vmw_resource_mob_attached(res)))) {
  546. ret = func->unbind(res, res->res_dirty, &val_buf);
  547. if (unlikely(ret != 0))
  548. goto out_no_unbind;
  549. vmw_resource_mob_detach(res);
  550. }
  551. ret = func->destroy(res);
  552. res->backup_dirty = true;
  553. res->res_dirty = false;
  554. out_no_unbind:
  555. vmw_resource_backoff_reservation(ticket, &val_buf);
  556. return ret;
  557. }
  558. /**
  559. * vmw_resource_validate - Make a resource up-to-date and visible
  560. * to the device.
  561. * @res: The resource to make visible to the device.
  562. * @intr: Perform waits interruptible if possible.
  563. * @dirtying: Pending GPU operation will dirty the resource
  564. *
  565. * On successful return, any backup DMA buffer pointed to by @res->backup will
  566. * be reserved and validated.
  567. * On hardware resource shortage, this function will repeatedly evict
  568. * resources of the same type until the validation succeeds.
  569. *
  570. * Return: Zero on success, -ERESTARTSYS if interrupted, negative error code
  571. * on failure.
  572. */
  573. int vmw_resource_validate(struct vmw_resource *res, bool intr,
  574. bool dirtying)
  575. {
  576. int ret;
  577. struct vmw_resource *evict_res;
  578. struct vmw_private *dev_priv = res->dev_priv;
  579. struct list_head *lru_list = &dev_priv->res_lru[res->func->res_type];
  580. struct ttm_validate_buffer val_buf;
  581. unsigned err_count = 0;
  582. if (!res->func->create)
  583. return 0;
  584. val_buf.bo = NULL;
  585. val_buf.num_shared = 0;
  586. if (res->backup)
  587. val_buf.bo = &res->backup->base;
  588. do {
  589. ret = vmw_resource_do_validate(res, &val_buf, dirtying);
  590. if (likely(ret != -EBUSY))
  591. break;
  592. spin_lock(&dev_priv->resource_lock);
  593. if (list_empty(lru_list) || !res->func->may_evict) {
  594. DRM_ERROR("Out of device device resources "
  595. "for %s.\n", res->func->type_name);
  596. ret = -EBUSY;
  597. spin_unlock(&dev_priv->resource_lock);
  598. break;
  599. }
  600. evict_res = vmw_resource_reference
  601. (list_first_entry(lru_list, struct vmw_resource,
  602. lru_head));
  603. list_del_init(&evict_res->lru_head);
  604. spin_unlock(&dev_priv->resource_lock);
  605. /* Trylock backup buffers with a NULL ticket. */
  606. ret = vmw_resource_do_evict(NULL, evict_res, intr);
  607. if (unlikely(ret != 0)) {
  608. spin_lock(&dev_priv->resource_lock);
  609. list_add_tail(&evict_res->lru_head, lru_list);
  610. spin_unlock(&dev_priv->resource_lock);
  611. if (ret == -ERESTARTSYS ||
  612. ++err_count > VMW_RES_EVICT_ERR_COUNT) {
  613. vmw_resource_unreference(&evict_res);
  614. goto out_no_validate;
  615. }
  616. }
  617. vmw_resource_unreference(&evict_res);
  618. } while (1);
  619. if (unlikely(ret != 0))
  620. goto out_no_validate;
  621. else if (!res->func->needs_backup && res->backup) {
  622. WARN_ON_ONCE(vmw_resource_mob_attached(res));
  623. vmw_bo_unreference(&res->backup);
  624. }
  625. return 0;
  626. out_no_validate:
  627. return ret;
  628. }
  629. /**
  630. * vmw_resource_unbind_list
  631. *
  632. * @vbo: Pointer to the current backing MOB.
  633. *
  634. * Evicts the Guest Backed hardware resource if the backup
  635. * buffer is being moved out of MOB memory.
  636. * Note that this function will not race with the resource
  637. * validation code, since resource validation and eviction
  638. * both require the backup buffer to be reserved.
  639. */
  640. void vmw_resource_unbind_list(struct vmw_buffer_object *vbo)
  641. {
  642. struct ttm_validate_buffer val_buf = {
  643. .bo = &vbo->base,
  644. .num_shared = 0
  645. };
  646. dma_resv_assert_held(vbo->base.base.resv);
  647. while (!RB_EMPTY_ROOT(&vbo->res_tree)) {
  648. struct rb_node *node = vbo->res_tree.rb_node;
  649. struct vmw_resource *res =
  650. container_of(node, struct vmw_resource, mob_node);
  651. if (!WARN_ON_ONCE(!res->func->unbind))
  652. (void) res->func->unbind(res, res->res_dirty, &val_buf);
  653. res->backup_dirty = true;
  654. res->res_dirty = false;
  655. vmw_resource_mob_detach(res);
  656. }
  657. (void) ttm_bo_wait(&vbo->base, false, false);
  658. }
  659. /**
  660. * vmw_query_readback_all - Read back cached query states
  661. *
  662. * @dx_query_mob: Buffer containing the DX query MOB
  663. *
  664. * Read back cached states from the device if they exist. This function
  665. * assumes binding_mutex is held.
  666. */
  667. int vmw_query_readback_all(struct vmw_buffer_object *dx_query_mob)
  668. {
  669. struct vmw_resource *dx_query_ctx;
  670. struct vmw_private *dev_priv;
  671. struct {
  672. SVGA3dCmdHeader header;
  673. SVGA3dCmdDXReadbackAllQuery body;
  674. } *cmd;
  675. /* No query bound, so do nothing */
  676. if (!dx_query_mob || !dx_query_mob->dx_query_ctx)
  677. return 0;
  678. dx_query_ctx = dx_query_mob->dx_query_ctx;
  679. dev_priv = dx_query_ctx->dev_priv;
  680. cmd = VMW_CMD_CTX_RESERVE(dev_priv, sizeof(*cmd), dx_query_ctx->id);
  681. if (unlikely(cmd == NULL))
  682. return -ENOMEM;
  683. cmd->header.id = SVGA_3D_CMD_DX_READBACK_ALL_QUERY;
  684. cmd->header.size = sizeof(cmd->body);
  685. cmd->body.cid = dx_query_ctx->id;
  686. vmw_cmd_commit(dev_priv, sizeof(*cmd));
  687. /* Triggers a rebind the next time affected context is bound */
  688. dx_query_mob->dx_query_ctx = NULL;
  689. return 0;
  690. }
  691. /**
  692. * vmw_query_move_notify - Read back cached query states
  693. *
  694. * @bo: The TTM buffer object about to move.
  695. * @old_mem: The memory region @bo is moving from.
  696. * @new_mem: The memory region @bo is moving to.
  697. *
  698. * Called before the query MOB is swapped out to read back cached query
  699. * states from the device.
  700. */
  701. void vmw_query_move_notify(struct ttm_buffer_object *bo,
  702. struct ttm_resource *old_mem,
  703. struct ttm_resource *new_mem)
  704. {
  705. struct vmw_buffer_object *dx_query_mob;
  706. struct ttm_device *bdev = bo->bdev;
  707. struct vmw_private *dev_priv;
  708. dev_priv = container_of(bdev, struct vmw_private, bdev);
  709. mutex_lock(&dev_priv->binding_mutex);
  710. /* If BO is being moved from MOB to system memory */
  711. if (new_mem->mem_type == TTM_PL_SYSTEM &&
  712. old_mem->mem_type == VMW_PL_MOB) {
  713. struct vmw_fence_obj *fence;
  714. dx_query_mob = container_of(bo, struct vmw_buffer_object, base);
  715. if (!dx_query_mob || !dx_query_mob->dx_query_ctx) {
  716. mutex_unlock(&dev_priv->binding_mutex);
  717. return;
  718. }
  719. (void) vmw_query_readback_all(dx_query_mob);
  720. mutex_unlock(&dev_priv->binding_mutex);
  721. /* Create a fence and attach the BO to it */
  722. (void) vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
  723. vmw_bo_fence_single(bo, fence);
  724. if (fence != NULL)
  725. vmw_fence_obj_unreference(&fence);
  726. (void) ttm_bo_wait(bo, false, false);
  727. } else
  728. mutex_unlock(&dev_priv->binding_mutex);
  729. }
  730. /**
  731. * vmw_resource_needs_backup - Return whether a resource needs a backup buffer.
  732. *
  733. * @res: The resource being queried.
  734. */
  735. bool vmw_resource_needs_backup(const struct vmw_resource *res)
  736. {
  737. return res->func->needs_backup;
  738. }
  739. /**
  740. * vmw_resource_evict_type - Evict all resources of a specific type
  741. *
  742. * @dev_priv: Pointer to a device private struct
  743. * @type: The resource type to evict
  744. *
  745. * To avoid thrashing starvation or as part of the hibernation sequence,
  746. * try to evict all evictable resources of a specific type.
  747. */
  748. static void vmw_resource_evict_type(struct vmw_private *dev_priv,
  749. enum vmw_res_type type)
  750. {
  751. struct list_head *lru_list = &dev_priv->res_lru[type];
  752. struct vmw_resource *evict_res;
  753. unsigned err_count = 0;
  754. int ret;
  755. struct ww_acquire_ctx ticket;
  756. do {
  757. spin_lock(&dev_priv->resource_lock);
  758. if (list_empty(lru_list))
  759. goto out_unlock;
  760. evict_res = vmw_resource_reference(
  761. list_first_entry(lru_list, struct vmw_resource,
  762. lru_head));
  763. list_del_init(&evict_res->lru_head);
  764. spin_unlock(&dev_priv->resource_lock);
  765. /* Wait lock backup buffers with a ticket. */
  766. ret = vmw_resource_do_evict(&ticket, evict_res, false);
  767. if (unlikely(ret != 0)) {
  768. spin_lock(&dev_priv->resource_lock);
  769. list_add_tail(&evict_res->lru_head, lru_list);
  770. spin_unlock(&dev_priv->resource_lock);
  771. if (++err_count > VMW_RES_EVICT_ERR_COUNT) {
  772. vmw_resource_unreference(&evict_res);
  773. return;
  774. }
  775. }
  776. vmw_resource_unreference(&evict_res);
  777. } while (1);
  778. out_unlock:
  779. spin_unlock(&dev_priv->resource_lock);
  780. }
  781. /**
  782. * vmw_resource_evict_all - Evict all evictable resources
  783. *
  784. * @dev_priv: Pointer to a device private struct
  785. *
  786. * To avoid thrashing starvation or as part of the hibernation sequence,
  787. * evict all evictable resources. In particular this means that all
  788. * guest-backed resources that are registered with the device are
  789. * evicted and the OTable becomes clean.
  790. */
  791. void vmw_resource_evict_all(struct vmw_private *dev_priv)
  792. {
  793. enum vmw_res_type type;
  794. mutex_lock(&dev_priv->cmdbuf_mutex);
  795. for (type = 0; type < vmw_res_max; ++type)
  796. vmw_resource_evict_type(dev_priv, type);
  797. mutex_unlock(&dev_priv->cmdbuf_mutex);
  798. }
  799. /*
  800. * vmw_resource_pin - Add a pin reference on a resource
  801. *
  802. * @res: The resource to add a pin reference on
  803. *
  804. * This function adds a pin reference, and if needed validates the resource.
  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. * This function returns 0 on success and a negative error code on failure.
  808. */
  809. int vmw_resource_pin(struct vmw_resource *res, bool interruptible)
  810. {
  811. struct ttm_operation_ctx ctx = { interruptible, false };
  812. struct vmw_private *dev_priv = res->dev_priv;
  813. int ret;
  814. mutex_lock(&dev_priv->cmdbuf_mutex);
  815. ret = vmw_resource_reserve(res, interruptible, false);
  816. if (ret)
  817. goto out_no_reserve;
  818. if (res->pin_count == 0) {
  819. struct vmw_buffer_object *vbo = NULL;
  820. if (res->backup) {
  821. vbo = res->backup;
  822. ret = ttm_bo_reserve(&vbo->base, interruptible, false, NULL);
  823. if (ret)
  824. goto out_no_validate;
  825. if (!vbo->base.pin_count) {
  826. ret = ttm_bo_validate
  827. (&vbo->base,
  828. res->func->backup_placement,
  829. &ctx);
  830. if (ret) {
  831. ttm_bo_unreserve(&vbo->base);
  832. goto out_no_validate;
  833. }
  834. }
  835. /* Do we really need to pin the MOB as well? */
  836. vmw_bo_pin_reserved(vbo, true);
  837. }
  838. ret = vmw_resource_validate(res, interruptible, true);
  839. if (vbo)
  840. ttm_bo_unreserve(&vbo->base);
  841. if (ret)
  842. goto out_no_validate;
  843. }
  844. res->pin_count++;
  845. out_no_validate:
  846. vmw_resource_unreserve(res, false, false, false, NULL, 0UL);
  847. out_no_reserve:
  848. mutex_unlock(&dev_priv->cmdbuf_mutex);
  849. return ret;
  850. }
  851. /**
  852. * vmw_resource_unpin - Remove a pin reference from a resource
  853. *
  854. * @res: The resource to remove a pin reference from
  855. *
  856. * Having a pin reference means that the resource can never be evicted, and
  857. * its id will never change as long as there is a pin reference.
  858. */
  859. void vmw_resource_unpin(struct vmw_resource *res)
  860. {
  861. struct vmw_private *dev_priv = res->dev_priv;
  862. int ret;
  863. mutex_lock(&dev_priv->cmdbuf_mutex);
  864. ret = vmw_resource_reserve(res, false, true);
  865. WARN_ON(ret);
  866. WARN_ON(res->pin_count == 0);
  867. if (--res->pin_count == 0 && res->backup) {
  868. struct vmw_buffer_object *vbo = res->backup;
  869. (void) ttm_bo_reserve(&vbo->base, false, false, NULL);
  870. vmw_bo_pin_reserved(vbo, false);
  871. ttm_bo_unreserve(&vbo->base);
  872. }
  873. vmw_resource_unreserve(res, false, false, false, NULL, 0UL);
  874. mutex_unlock(&dev_priv->cmdbuf_mutex);
  875. }
  876. /**
  877. * vmw_res_type - Return the resource type
  878. *
  879. * @res: Pointer to the resource
  880. */
  881. enum vmw_res_type vmw_res_type(const struct vmw_resource *res)
  882. {
  883. return res->func->res_type;
  884. }
  885. /**
  886. * vmw_resource_dirty_update - Update a resource's dirty tracker with a
  887. * sequential range of touched backing store memory.
  888. * @res: The resource.
  889. * @start: The first page touched.
  890. * @end: The last page touched + 1.
  891. */
  892. void vmw_resource_dirty_update(struct vmw_resource *res, pgoff_t start,
  893. pgoff_t end)
  894. {
  895. if (res->dirty)
  896. res->func->dirty_range_add(res, start << PAGE_SHIFT,
  897. end << PAGE_SHIFT);
  898. }
  899. /**
  900. * vmw_resources_clean - Clean resources intersecting a mob range
  901. * @vbo: The mob buffer object
  902. * @start: The mob page offset starting the range
  903. * @end: The mob page offset ending the range
  904. * @num_prefault: Returns how many pages including the first have been
  905. * cleaned and are ok to prefault
  906. */
  907. int vmw_resources_clean(struct vmw_buffer_object *vbo, pgoff_t start,
  908. pgoff_t end, pgoff_t *num_prefault)
  909. {
  910. struct rb_node *cur = vbo->res_tree.rb_node;
  911. struct vmw_resource *found = NULL;
  912. unsigned long res_start = start << PAGE_SHIFT;
  913. unsigned long res_end = end << PAGE_SHIFT;
  914. unsigned long last_cleaned = 0;
  915. /*
  916. * Find the resource with lowest backup_offset that intersects the
  917. * range.
  918. */
  919. while (cur) {
  920. struct vmw_resource *cur_res =
  921. container_of(cur, struct vmw_resource, mob_node);
  922. if (cur_res->backup_offset >= res_end) {
  923. cur = cur->rb_left;
  924. } else if (cur_res->backup_offset + cur_res->backup_size <=
  925. res_start) {
  926. cur = cur->rb_right;
  927. } else {
  928. found = cur_res;
  929. cur = cur->rb_left;
  930. /* Continue to look for resources with lower offsets */
  931. }
  932. }
  933. /*
  934. * In order of increasing backup_offset, clean dirty resources
  935. * intersecting the range.
  936. */
  937. while (found) {
  938. if (found->res_dirty) {
  939. int ret;
  940. if (!found->func->clean)
  941. return -EINVAL;
  942. ret = found->func->clean(found);
  943. if (ret)
  944. return ret;
  945. found->res_dirty = false;
  946. }
  947. last_cleaned = found->backup_offset + found->backup_size;
  948. cur = rb_next(&found->mob_node);
  949. if (!cur)
  950. break;
  951. found = container_of(cur, struct vmw_resource, mob_node);
  952. if (found->backup_offset >= res_end)
  953. break;
  954. }
  955. /*
  956. * Set number of pages allowed prefaulting and fence the buffer object
  957. */
  958. *num_prefault = 1;
  959. if (last_cleaned > res_start) {
  960. struct ttm_buffer_object *bo = &vbo->base;
  961. *num_prefault = __KERNEL_DIV_ROUND_UP(last_cleaned - res_start,
  962. PAGE_SIZE);
  963. vmw_bo_fence_single(bo, NULL);
  964. }
  965. return 0;
  966. }