vmwgfx_resource.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  1. /**************************************************************************
  2. *
  3. * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  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_object.h>
  30. #include <drm/ttm/ttm_placement.h>
  31. #include <drm/drmP.h>
  32. #include "vmwgfx_resource_priv.h"
  33. #define VMW_RES_EVICT_ERR_COUNT 10
  34. struct vmw_user_dma_buffer {
  35. struct ttm_base_object base;
  36. struct vmw_dma_buffer dma;
  37. };
  38. struct vmw_bo_user_rep {
  39. uint32_t handle;
  40. uint64_t map_handle;
  41. };
  42. struct vmw_stream {
  43. struct vmw_resource res;
  44. uint32_t stream_id;
  45. };
  46. struct vmw_user_stream {
  47. struct ttm_base_object base;
  48. struct vmw_stream stream;
  49. };
  50. static uint64_t vmw_user_stream_size;
  51. static const struct vmw_res_func vmw_stream_func = {
  52. .res_type = vmw_res_stream,
  53. .needs_backup = false,
  54. .may_evict = false,
  55. .type_name = "video streams",
  56. .backup_placement = NULL,
  57. .create = NULL,
  58. .destroy = NULL,
  59. .bind = NULL,
  60. .unbind = NULL
  61. };
  62. static inline struct vmw_dma_buffer *
  63. vmw_dma_buffer(struct ttm_buffer_object *bo)
  64. {
  65. return container_of(bo, struct vmw_dma_buffer, base);
  66. }
  67. static inline struct vmw_user_dma_buffer *
  68. vmw_user_dma_buffer(struct ttm_buffer_object *bo)
  69. {
  70. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  71. return container_of(vmw_bo, struct vmw_user_dma_buffer, dma);
  72. }
  73. struct vmw_resource *vmw_resource_reference(struct vmw_resource *res)
  74. {
  75. kref_get(&res->kref);
  76. return res;
  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. write_lock(&dev_priv->resource_lock);
  90. if (res->id != -1)
  91. idr_remove(idr, res->id);
  92. res->id = -1;
  93. write_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. struct idr *idr = &dev_priv->res_idr[res->func->res_type];
  102. res->avail = false;
  103. list_del_init(&res->lru_head);
  104. write_unlock(&dev_priv->resource_lock);
  105. if (res->backup) {
  106. struct ttm_buffer_object *bo = &res->backup->base;
  107. ttm_bo_reserve(bo, false, false, false, 0);
  108. if (!list_empty(&res->mob_head) &&
  109. res->func->unbind != NULL) {
  110. struct ttm_validate_buffer val_buf;
  111. val_buf.bo = bo;
  112. res->func->unbind(res, false, &val_buf);
  113. }
  114. res->backup_dirty = false;
  115. list_del_init(&res->mob_head);
  116. ttm_bo_unreserve(bo);
  117. vmw_dmabuf_unreference(&res->backup);
  118. }
  119. if (likely(res->hw_destroy != NULL))
  120. res->hw_destroy(res);
  121. id = res->id;
  122. if (res->res_free != NULL)
  123. res->res_free(res);
  124. else
  125. kfree(res);
  126. write_lock(&dev_priv->resource_lock);
  127. if (id != -1)
  128. idr_remove(idr, id);
  129. }
  130. void vmw_resource_unreference(struct vmw_resource **p_res)
  131. {
  132. struct vmw_resource *res = *p_res;
  133. struct vmw_private *dev_priv = res->dev_priv;
  134. *p_res = NULL;
  135. write_lock(&dev_priv->resource_lock);
  136. kref_put(&res->kref, vmw_resource_release);
  137. write_unlock(&dev_priv->resource_lock);
  138. }
  139. /**
  140. * vmw_resource_alloc_id - release a resource id to the id manager.
  141. *
  142. * @res: Pointer to the resource.
  143. *
  144. * Allocate the lowest free resource from the resource manager, and set
  145. * @res->id to that id. Returns 0 on success and -ENOMEM on failure.
  146. */
  147. int vmw_resource_alloc_id(struct vmw_resource *res)
  148. {
  149. struct vmw_private *dev_priv = res->dev_priv;
  150. int ret;
  151. struct idr *idr = &dev_priv->res_idr[res->func->res_type];
  152. BUG_ON(res->id != -1);
  153. idr_preload(GFP_KERNEL);
  154. write_lock(&dev_priv->resource_lock);
  155. ret = idr_alloc(idr, res, 1, 0, GFP_NOWAIT);
  156. if (ret >= 0)
  157. res->id = ret;
  158. write_unlock(&dev_priv->resource_lock);
  159. idr_preload_end();
  160. return ret < 0 ? ret : 0;
  161. }
  162. /**
  163. * vmw_resource_init - initialize a struct vmw_resource
  164. *
  165. * @dev_priv: Pointer to a device private struct.
  166. * @res: The struct vmw_resource to initialize.
  167. * @obj_type: Resource object type.
  168. * @delay_id: Boolean whether to defer device id allocation until
  169. * the first validation.
  170. * @res_free: Resource destructor.
  171. * @func: Resource function table.
  172. */
  173. int vmw_resource_init(struct vmw_private *dev_priv, struct vmw_resource *res,
  174. bool delay_id,
  175. void (*res_free) (struct vmw_resource *res),
  176. const struct vmw_res_func *func)
  177. {
  178. kref_init(&res->kref);
  179. res->hw_destroy = NULL;
  180. res->res_free = res_free;
  181. res->avail = false;
  182. res->dev_priv = dev_priv;
  183. res->func = func;
  184. INIT_LIST_HEAD(&res->lru_head);
  185. INIT_LIST_HEAD(&res->mob_head);
  186. res->id = -1;
  187. res->backup = NULL;
  188. res->backup_offset = 0;
  189. res->backup_dirty = false;
  190. res->res_dirty = false;
  191. if (delay_id)
  192. return 0;
  193. else
  194. return vmw_resource_alloc_id(res);
  195. }
  196. /**
  197. * vmw_resource_activate
  198. *
  199. * @res: Pointer to the newly created resource
  200. * @hw_destroy: Destroy function. NULL if none.
  201. *
  202. * Activate a resource after the hardware has been made aware of it.
  203. * Set tye destroy function to @destroy. Typically this frees the
  204. * resource and destroys the hardware resources associated with it.
  205. * Activate basically means that the function vmw_resource_lookup will
  206. * find it.
  207. */
  208. void vmw_resource_activate(struct vmw_resource *res,
  209. void (*hw_destroy) (struct vmw_resource *))
  210. {
  211. struct vmw_private *dev_priv = res->dev_priv;
  212. write_lock(&dev_priv->resource_lock);
  213. res->avail = true;
  214. res->hw_destroy = hw_destroy;
  215. write_unlock(&dev_priv->resource_lock);
  216. }
  217. struct vmw_resource *vmw_resource_lookup(struct vmw_private *dev_priv,
  218. struct idr *idr, int id)
  219. {
  220. struct vmw_resource *res;
  221. read_lock(&dev_priv->resource_lock);
  222. res = idr_find(idr, id);
  223. if (res && res->avail)
  224. kref_get(&res->kref);
  225. else
  226. res = NULL;
  227. read_unlock(&dev_priv->resource_lock);
  228. if (unlikely(res == NULL))
  229. return NULL;
  230. return res;
  231. }
  232. /**
  233. * vmw_user_resource_lookup_handle - lookup a struct resource from a
  234. * TTM user-space handle and perform basic type checks
  235. *
  236. * @dev_priv: Pointer to a device private struct
  237. * @tfile: Pointer to a struct ttm_object_file identifying the caller
  238. * @handle: The TTM user-space handle
  239. * @converter: Pointer to an object describing the resource type
  240. * @p_res: On successful return the location pointed to will contain
  241. * a pointer to a refcounted struct vmw_resource.
  242. *
  243. * If the handle can't be found or is associated with an incorrect resource
  244. * type, -EINVAL will be returned.
  245. */
  246. int vmw_user_resource_lookup_handle(struct vmw_private *dev_priv,
  247. struct ttm_object_file *tfile,
  248. uint32_t handle,
  249. const struct vmw_user_resource_conv
  250. *converter,
  251. struct vmw_resource **p_res)
  252. {
  253. struct ttm_base_object *base;
  254. struct vmw_resource *res;
  255. int ret = -EINVAL;
  256. base = ttm_base_object_lookup(tfile, handle);
  257. if (unlikely(base == NULL))
  258. return -EINVAL;
  259. if (unlikely(base->object_type != converter->object_type))
  260. goto out_bad_resource;
  261. res = converter->base_obj_to_res(base);
  262. read_lock(&dev_priv->resource_lock);
  263. if (!res->avail || res->res_free != converter->res_free) {
  264. read_unlock(&dev_priv->resource_lock);
  265. goto out_bad_resource;
  266. }
  267. kref_get(&res->kref);
  268. read_unlock(&dev_priv->resource_lock);
  269. *p_res = res;
  270. ret = 0;
  271. out_bad_resource:
  272. ttm_base_object_unref(&base);
  273. return ret;
  274. }
  275. /**
  276. * Helper function that looks either a surface or dmabuf.
  277. *
  278. * The pointer this pointed at by out_surf and out_buf needs to be null.
  279. */
  280. int vmw_user_lookup_handle(struct vmw_private *dev_priv,
  281. struct ttm_object_file *tfile,
  282. uint32_t handle,
  283. struct vmw_surface **out_surf,
  284. struct vmw_dma_buffer **out_buf)
  285. {
  286. struct vmw_resource *res;
  287. int ret;
  288. BUG_ON(*out_surf || *out_buf);
  289. ret = vmw_user_resource_lookup_handle(dev_priv, tfile, handle,
  290. user_surface_converter,
  291. &res);
  292. if (!ret) {
  293. *out_surf = vmw_res_to_srf(res);
  294. return 0;
  295. }
  296. *out_surf = NULL;
  297. ret = vmw_user_dmabuf_lookup(tfile, handle, out_buf);
  298. return ret;
  299. }
  300. /**
  301. * Buffer management.
  302. */
  303. void vmw_dmabuf_bo_free(struct ttm_buffer_object *bo)
  304. {
  305. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  306. kfree(vmw_bo);
  307. }
  308. int vmw_dmabuf_init(struct vmw_private *dev_priv,
  309. struct vmw_dma_buffer *vmw_bo,
  310. size_t size, struct ttm_placement *placement,
  311. bool interruptible,
  312. void (*bo_free) (struct ttm_buffer_object *bo))
  313. {
  314. struct ttm_bo_device *bdev = &dev_priv->bdev;
  315. size_t acc_size;
  316. int ret;
  317. BUG_ON(!bo_free);
  318. acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct vmw_dma_buffer));
  319. memset(vmw_bo, 0, sizeof(*vmw_bo));
  320. INIT_LIST_HEAD(&vmw_bo->res_list);
  321. ret = ttm_bo_init(bdev, &vmw_bo->base, size,
  322. ttm_bo_type_device, placement,
  323. 0, interruptible,
  324. NULL, acc_size, NULL, bo_free);
  325. return ret;
  326. }
  327. static void vmw_user_dmabuf_destroy(struct ttm_buffer_object *bo)
  328. {
  329. struct vmw_user_dma_buffer *vmw_user_bo = vmw_user_dma_buffer(bo);
  330. ttm_base_object_kfree(vmw_user_bo, base);
  331. }
  332. static void vmw_user_dmabuf_release(struct ttm_base_object **p_base)
  333. {
  334. struct vmw_user_dma_buffer *vmw_user_bo;
  335. struct ttm_base_object *base = *p_base;
  336. struct ttm_buffer_object *bo;
  337. *p_base = NULL;
  338. if (unlikely(base == NULL))
  339. return;
  340. vmw_user_bo = container_of(base, struct vmw_user_dma_buffer, base);
  341. bo = &vmw_user_bo->dma.base;
  342. ttm_bo_unref(&bo);
  343. }
  344. /**
  345. * vmw_user_dmabuf_alloc - Allocate a user dma buffer
  346. *
  347. * @dev_priv: Pointer to a struct device private.
  348. * @tfile: Pointer to a struct ttm_object_file on which to register the user
  349. * object.
  350. * @size: Size of the dma buffer.
  351. * @shareable: Boolean whether the buffer is shareable with other open files.
  352. * @handle: Pointer to where the handle value should be assigned.
  353. * @p_dma_buf: Pointer to where the refcounted struct vmw_dma_buffer pointer
  354. * should be assigned.
  355. */
  356. int vmw_user_dmabuf_alloc(struct vmw_private *dev_priv,
  357. struct ttm_object_file *tfile,
  358. uint32_t size,
  359. bool shareable,
  360. uint32_t *handle,
  361. struct vmw_dma_buffer **p_dma_buf)
  362. {
  363. struct vmw_user_dma_buffer *user_bo;
  364. struct ttm_buffer_object *tmp;
  365. int ret;
  366. user_bo = kzalloc(sizeof(*user_bo), GFP_KERNEL);
  367. if (unlikely(user_bo == NULL)) {
  368. DRM_ERROR("Failed to allocate a buffer.\n");
  369. return -ENOMEM;
  370. }
  371. ret = vmw_dmabuf_init(dev_priv, &user_bo->dma, size,
  372. &vmw_vram_sys_placement, true,
  373. &vmw_user_dmabuf_destroy);
  374. if (unlikely(ret != 0))
  375. return ret;
  376. tmp = ttm_bo_reference(&user_bo->dma.base);
  377. ret = ttm_base_object_init(tfile,
  378. &user_bo->base,
  379. shareable,
  380. ttm_buffer_type,
  381. &vmw_user_dmabuf_release, NULL);
  382. if (unlikely(ret != 0)) {
  383. ttm_bo_unref(&tmp);
  384. goto out_no_base_object;
  385. }
  386. *p_dma_buf = &user_bo->dma;
  387. *handle = user_bo->base.hash.key;
  388. out_no_base_object:
  389. return ret;
  390. }
  391. /**
  392. * vmw_user_dmabuf_verify_access - verify access permissions on this
  393. * buffer object.
  394. *
  395. * @bo: Pointer to the buffer object being accessed
  396. * @tfile: Identifying the caller.
  397. */
  398. int vmw_user_dmabuf_verify_access(struct ttm_buffer_object *bo,
  399. struct ttm_object_file *tfile)
  400. {
  401. struct vmw_user_dma_buffer *vmw_user_bo;
  402. if (unlikely(bo->destroy != vmw_user_dmabuf_destroy))
  403. return -EPERM;
  404. vmw_user_bo = vmw_user_dma_buffer(bo);
  405. return (vmw_user_bo->base.tfile == tfile ||
  406. vmw_user_bo->base.shareable) ? 0 : -EPERM;
  407. }
  408. int vmw_dmabuf_alloc_ioctl(struct drm_device *dev, void *data,
  409. struct drm_file *file_priv)
  410. {
  411. struct vmw_private *dev_priv = vmw_priv(dev);
  412. union drm_vmw_alloc_dmabuf_arg *arg =
  413. (union drm_vmw_alloc_dmabuf_arg *)data;
  414. struct drm_vmw_alloc_dmabuf_req *req = &arg->req;
  415. struct drm_vmw_dmabuf_rep *rep = &arg->rep;
  416. struct vmw_dma_buffer *dma_buf;
  417. uint32_t handle;
  418. struct vmw_master *vmaster = vmw_master(file_priv->master);
  419. int ret;
  420. ret = ttm_read_lock(&vmaster->lock, true);
  421. if (unlikely(ret != 0))
  422. return ret;
  423. ret = vmw_user_dmabuf_alloc(dev_priv, vmw_fpriv(file_priv)->tfile,
  424. req->size, false, &handle, &dma_buf);
  425. if (unlikely(ret != 0))
  426. goto out_no_dmabuf;
  427. rep->handle = handle;
  428. rep->map_handle = drm_vma_node_offset_addr(&dma_buf->base.vma_node);
  429. rep->cur_gmr_id = handle;
  430. rep->cur_gmr_offset = 0;
  431. vmw_dmabuf_unreference(&dma_buf);
  432. out_no_dmabuf:
  433. ttm_read_unlock(&vmaster->lock);
  434. return ret;
  435. }
  436. int vmw_dmabuf_unref_ioctl(struct drm_device *dev, void *data,
  437. struct drm_file *file_priv)
  438. {
  439. struct drm_vmw_unref_dmabuf_arg *arg =
  440. (struct drm_vmw_unref_dmabuf_arg *)data;
  441. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  442. arg->handle,
  443. TTM_REF_USAGE);
  444. }
  445. int vmw_user_dmabuf_lookup(struct ttm_object_file *tfile,
  446. uint32_t handle, struct vmw_dma_buffer **out)
  447. {
  448. struct vmw_user_dma_buffer *vmw_user_bo;
  449. struct ttm_base_object *base;
  450. base = ttm_base_object_lookup(tfile, handle);
  451. if (unlikely(base == NULL)) {
  452. printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
  453. (unsigned long)handle);
  454. return -ESRCH;
  455. }
  456. if (unlikely(base->object_type != ttm_buffer_type)) {
  457. ttm_base_object_unref(&base);
  458. printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
  459. (unsigned long)handle);
  460. return -EINVAL;
  461. }
  462. vmw_user_bo = container_of(base, struct vmw_user_dma_buffer, base);
  463. (void)ttm_bo_reference(&vmw_user_bo->dma.base);
  464. ttm_base_object_unref(&base);
  465. *out = &vmw_user_bo->dma;
  466. return 0;
  467. }
  468. int vmw_user_dmabuf_reference(struct ttm_object_file *tfile,
  469. struct vmw_dma_buffer *dma_buf)
  470. {
  471. struct vmw_user_dma_buffer *user_bo;
  472. if (dma_buf->base.destroy != vmw_user_dmabuf_destroy)
  473. return -EINVAL;
  474. user_bo = container_of(dma_buf, struct vmw_user_dma_buffer, dma);
  475. return ttm_ref_object_add(tfile, &user_bo->base, TTM_REF_USAGE, NULL);
  476. }
  477. /*
  478. * Stream management
  479. */
  480. static void vmw_stream_destroy(struct vmw_resource *res)
  481. {
  482. struct vmw_private *dev_priv = res->dev_priv;
  483. struct vmw_stream *stream;
  484. int ret;
  485. DRM_INFO("%s: unref\n", __func__);
  486. stream = container_of(res, struct vmw_stream, res);
  487. ret = vmw_overlay_unref(dev_priv, stream->stream_id);
  488. WARN_ON(ret != 0);
  489. }
  490. static int vmw_stream_init(struct vmw_private *dev_priv,
  491. struct vmw_stream *stream,
  492. void (*res_free) (struct vmw_resource *res))
  493. {
  494. struct vmw_resource *res = &stream->res;
  495. int ret;
  496. ret = vmw_resource_init(dev_priv, res, false, res_free,
  497. &vmw_stream_func);
  498. if (unlikely(ret != 0)) {
  499. if (res_free == NULL)
  500. kfree(stream);
  501. else
  502. res_free(&stream->res);
  503. return ret;
  504. }
  505. ret = vmw_overlay_claim(dev_priv, &stream->stream_id);
  506. if (ret) {
  507. vmw_resource_unreference(&res);
  508. return ret;
  509. }
  510. DRM_INFO("%s: claimed\n", __func__);
  511. vmw_resource_activate(&stream->res, vmw_stream_destroy);
  512. return 0;
  513. }
  514. static void vmw_user_stream_free(struct vmw_resource *res)
  515. {
  516. struct vmw_user_stream *stream =
  517. container_of(res, struct vmw_user_stream, stream.res);
  518. struct vmw_private *dev_priv = res->dev_priv;
  519. ttm_base_object_kfree(stream, base);
  520. ttm_mem_global_free(vmw_mem_glob(dev_priv),
  521. vmw_user_stream_size);
  522. }
  523. /**
  524. * This function is called when user space has no more references on the
  525. * base object. It releases the base-object's reference on the resource object.
  526. */
  527. static void vmw_user_stream_base_release(struct ttm_base_object **p_base)
  528. {
  529. struct ttm_base_object *base = *p_base;
  530. struct vmw_user_stream *stream =
  531. container_of(base, struct vmw_user_stream, base);
  532. struct vmw_resource *res = &stream->stream.res;
  533. *p_base = NULL;
  534. vmw_resource_unreference(&res);
  535. }
  536. int vmw_stream_unref_ioctl(struct drm_device *dev, void *data,
  537. struct drm_file *file_priv)
  538. {
  539. struct vmw_private *dev_priv = vmw_priv(dev);
  540. struct vmw_resource *res;
  541. struct vmw_user_stream *stream;
  542. struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
  543. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  544. struct idr *idr = &dev_priv->res_idr[vmw_res_stream];
  545. int ret = 0;
  546. res = vmw_resource_lookup(dev_priv, idr, arg->stream_id);
  547. if (unlikely(res == NULL))
  548. return -EINVAL;
  549. if (res->res_free != &vmw_user_stream_free) {
  550. ret = -EINVAL;
  551. goto out;
  552. }
  553. stream = container_of(res, struct vmw_user_stream, stream.res);
  554. if (stream->base.tfile != tfile) {
  555. ret = -EINVAL;
  556. goto out;
  557. }
  558. ttm_ref_object_base_unref(tfile, stream->base.hash.key, TTM_REF_USAGE);
  559. out:
  560. vmw_resource_unreference(&res);
  561. return ret;
  562. }
  563. int vmw_stream_claim_ioctl(struct drm_device *dev, void *data,
  564. struct drm_file *file_priv)
  565. {
  566. struct vmw_private *dev_priv = vmw_priv(dev);
  567. struct vmw_user_stream *stream;
  568. struct vmw_resource *res;
  569. struct vmw_resource *tmp;
  570. struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
  571. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  572. struct vmw_master *vmaster = vmw_master(file_priv->master);
  573. int ret;
  574. /*
  575. * Approximate idr memory usage with 128 bytes. It will be limited
  576. * by maximum number_of streams anyway?
  577. */
  578. if (unlikely(vmw_user_stream_size == 0))
  579. vmw_user_stream_size = ttm_round_pot(sizeof(*stream)) + 128;
  580. ret = ttm_read_lock(&vmaster->lock, true);
  581. if (unlikely(ret != 0))
  582. return ret;
  583. ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
  584. vmw_user_stream_size,
  585. false, true);
  586. if (unlikely(ret != 0)) {
  587. if (ret != -ERESTARTSYS)
  588. DRM_ERROR("Out of graphics memory for stream"
  589. " creation.\n");
  590. goto out_unlock;
  591. }
  592. stream = kmalloc(sizeof(*stream), GFP_KERNEL);
  593. if (unlikely(stream == NULL)) {
  594. ttm_mem_global_free(vmw_mem_glob(dev_priv),
  595. vmw_user_stream_size);
  596. ret = -ENOMEM;
  597. goto out_unlock;
  598. }
  599. res = &stream->stream.res;
  600. stream->base.shareable = false;
  601. stream->base.tfile = NULL;
  602. /*
  603. * From here on, the destructor takes over resource freeing.
  604. */
  605. ret = vmw_stream_init(dev_priv, &stream->stream, vmw_user_stream_free);
  606. if (unlikely(ret != 0))
  607. goto out_unlock;
  608. tmp = vmw_resource_reference(res);
  609. ret = ttm_base_object_init(tfile, &stream->base, false, VMW_RES_STREAM,
  610. &vmw_user_stream_base_release, NULL);
  611. if (unlikely(ret != 0)) {
  612. vmw_resource_unreference(&tmp);
  613. goto out_err;
  614. }
  615. arg->stream_id = res->id;
  616. out_err:
  617. vmw_resource_unreference(&res);
  618. out_unlock:
  619. ttm_read_unlock(&vmaster->lock);
  620. return ret;
  621. }
  622. int vmw_user_stream_lookup(struct vmw_private *dev_priv,
  623. struct ttm_object_file *tfile,
  624. uint32_t *inout_id, struct vmw_resource **out)
  625. {
  626. struct vmw_user_stream *stream;
  627. struct vmw_resource *res;
  628. int ret;
  629. res = vmw_resource_lookup(dev_priv, &dev_priv->res_idr[vmw_res_stream],
  630. *inout_id);
  631. if (unlikely(res == NULL))
  632. return -EINVAL;
  633. if (res->res_free != &vmw_user_stream_free) {
  634. ret = -EINVAL;
  635. goto err_ref;
  636. }
  637. stream = container_of(res, struct vmw_user_stream, stream.res);
  638. if (stream->base.tfile != tfile) {
  639. ret = -EPERM;
  640. goto err_ref;
  641. }
  642. *inout_id = stream->stream.stream_id;
  643. *out = res;
  644. return 0;
  645. err_ref:
  646. vmw_resource_unreference(&res);
  647. return ret;
  648. }
  649. int vmw_dumb_create(struct drm_file *file_priv,
  650. struct drm_device *dev,
  651. struct drm_mode_create_dumb *args)
  652. {
  653. struct vmw_private *dev_priv = vmw_priv(dev);
  654. struct vmw_master *vmaster = vmw_master(file_priv->master);
  655. struct vmw_user_dma_buffer *vmw_user_bo;
  656. struct ttm_buffer_object *tmp;
  657. int ret;
  658. args->pitch = args->width * ((args->bpp + 7) / 8);
  659. args->size = args->pitch * args->height;
  660. vmw_user_bo = kzalloc(sizeof(*vmw_user_bo), GFP_KERNEL);
  661. if (vmw_user_bo == NULL)
  662. return -ENOMEM;
  663. ret = ttm_read_lock(&vmaster->lock, true);
  664. if (ret != 0) {
  665. kfree(vmw_user_bo);
  666. return ret;
  667. }
  668. ret = vmw_dmabuf_init(dev_priv, &vmw_user_bo->dma, args->size,
  669. &vmw_vram_sys_placement, true,
  670. &vmw_user_dmabuf_destroy);
  671. if (ret != 0)
  672. goto out_no_dmabuf;
  673. tmp = ttm_bo_reference(&vmw_user_bo->dma.base);
  674. ret = ttm_base_object_init(vmw_fpriv(file_priv)->tfile,
  675. &vmw_user_bo->base,
  676. false,
  677. ttm_buffer_type,
  678. &vmw_user_dmabuf_release, NULL);
  679. if (unlikely(ret != 0))
  680. goto out_no_base_object;
  681. args->handle = vmw_user_bo->base.hash.key;
  682. out_no_base_object:
  683. ttm_bo_unref(&tmp);
  684. out_no_dmabuf:
  685. ttm_read_unlock(&vmaster->lock);
  686. return ret;
  687. }
  688. int vmw_dumb_map_offset(struct drm_file *file_priv,
  689. struct drm_device *dev, uint32_t handle,
  690. uint64_t *offset)
  691. {
  692. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  693. struct vmw_dma_buffer *out_buf;
  694. int ret;
  695. ret = vmw_user_dmabuf_lookup(tfile, handle, &out_buf);
  696. if (ret != 0)
  697. return -EINVAL;
  698. *offset = drm_vma_node_offset_addr(&out_buf->base.vma_node);
  699. vmw_dmabuf_unreference(&out_buf);
  700. return 0;
  701. }
  702. int vmw_dumb_destroy(struct drm_file *file_priv,
  703. struct drm_device *dev,
  704. uint32_t handle)
  705. {
  706. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  707. handle, TTM_REF_USAGE);
  708. }
  709. /**
  710. * vmw_resource_buf_alloc - Allocate a backup buffer for a resource.
  711. *
  712. * @res: The resource for which to allocate a backup buffer.
  713. * @interruptible: Whether any sleeps during allocation should be
  714. * performed while interruptible.
  715. */
  716. static int vmw_resource_buf_alloc(struct vmw_resource *res,
  717. bool interruptible)
  718. {
  719. unsigned long size =
  720. (res->backup_size + PAGE_SIZE - 1) & PAGE_MASK;
  721. struct vmw_dma_buffer *backup;
  722. int ret;
  723. if (likely(res->backup)) {
  724. BUG_ON(res->backup->base.num_pages * PAGE_SIZE < size);
  725. return 0;
  726. }
  727. backup = kzalloc(sizeof(*backup), GFP_KERNEL);
  728. if (unlikely(backup == NULL))
  729. return -ENOMEM;
  730. ret = vmw_dmabuf_init(res->dev_priv, backup, res->backup_size,
  731. res->func->backup_placement,
  732. interruptible,
  733. &vmw_dmabuf_bo_free);
  734. if (unlikely(ret != 0))
  735. goto out_no_dmabuf;
  736. res->backup = backup;
  737. out_no_dmabuf:
  738. return ret;
  739. }
  740. /**
  741. * vmw_resource_do_validate - Make a resource up-to-date and visible
  742. * to the device.
  743. *
  744. * @res: The resource to make visible to the device.
  745. * @val_buf: Information about a buffer possibly
  746. * containing backup data if a bind operation is needed.
  747. *
  748. * On hardware resource shortage, this function returns -EBUSY and
  749. * should be retried once resources have been freed up.
  750. */
  751. static int vmw_resource_do_validate(struct vmw_resource *res,
  752. struct ttm_validate_buffer *val_buf)
  753. {
  754. int ret = 0;
  755. const struct vmw_res_func *func = res->func;
  756. if (unlikely(res->id == -1)) {
  757. ret = func->create(res);
  758. if (unlikely(ret != 0))
  759. return ret;
  760. }
  761. if (func->bind &&
  762. ((func->needs_backup && list_empty(&res->mob_head) &&
  763. val_buf->bo != NULL) ||
  764. (!func->needs_backup && val_buf->bo != NULL))) {
  765. ret = func->bind(res, val_buf);
  766. if (unlikely(ret != 0))
  767. goto out_bind_failed;
  768. if (func->needs_backup)
  769. list_add_tail(&res->mob_head, &res->backup->res_list);
  770. }
  771. /*
  772. * Only do this on write operations, and move to
  773. * vmw_resource_unreserve if it can be called after
  774. * backup buffers have been unreserved. Otherwise
  775. * sort out locking.
  776. */
  777. res->res_dirty = true;
  778. return 0;
  779. out_bind_failed:
  780. func->destroy(res);
  781. return ret;
  782. }
  783. /**
  784. * vmw_resource_unreserve - Unreserve a resource previously reserved for
  785. * command submission.
  786. *
  787. * @res: Pointer to the struct vmw_resource to unreserve.
  788. * @new_backup: Pointer to new backup buffer if command submission
  789. * switched.
  790. * @new_backup_offset: New backup offset if @new_backup is !NULL.
  791. *
  792. * Currently unreserving a resource means putting it back on the device's
  793. * resource lru list, so that it can be evicted if necessary.
  794. */
  795. void vmw_resource_unreserve(struct vmw_resource *res,
  796. struct vmw_dma_buffer *new_backup,
  797. unsigned long new_backup_offset)
  798. {
  799. struct vmw_private *dev_priv = res->dev_priv;
  800. if (!list_empty(&res->lru_head))
  801. return;
  802. if (new_backup && new_backup != res->backup) {
  803. if (res->backup) {
  804. lockdep_assert_held(&res->backup->base.resv->lock.base);
  805. list_del_init(&res->mob_head);
  806. vmw_dmabuf_unreference(&res->backup);
  807. }
  808. res->backup = vmw_dmabuf_reference(new_backup);
  809. lockdep_assert_held(&new_backup->base.resv->lock.base);
  810. list_add_tail(&res->mob_head, &new_backup->res_list);
  811. }
  812. if (new_backup)
  813. res->backup_offset = new_backup_offset;
  814. if (!res->func->may_evict || res->id == -1)
  815. return;
  816. write_lock(&dev_priv->resource_lock);
  817. list_add_tail(&res->lru_head,
  818. &res->dev_priv->res_lru[res->func->res_type]);
  819. write_unlock(&dev_priv->resource_lock);
  820. }
  821. /**
  822. * vmw_resource_check_buffer - Check whether a backup buffer is needed
  823. * for a resource and in that case, allocate
  824. * one, reserve and validate it.
  825. *
  826. * @res: The resource for which to allocate a backup buffer.
  827. * @interruptible: Whether any sleeps during allocation should be
  828. * performed while interruptible.
  829. * @val_buf: On successful return contains data about the
  830. * reserved and validated backup buffer.
  831. */
  832. static int
  833. vmw_resource_check_buffer(struct vmw_resource *res,
  834. struct ww_acquire_ctx *ticket,
  835. bool interruptible,
  836. struct ttm_validate_buffer *val_buf)
  837. {
  838. struct list_head val_list;
  839. bool backup_dirty = false;
  840. int ret;
  841. if (unlikely(res->backup == NULL)) {
  842. ret = vmw_resource_buf_alloc(res, interruptible);
  843. if (unlikely(ret != 0))
  844. return ret;
  845. }
  846. INIT_LIST_HEAD(&val_list);
  847. val_buf->bo = ttm_bo_reference(&res->backup->base);
  848. list_add_tail(&val_buf->head, &val_list);
  849. ret = ttm_eu_reserve_buffers(ticket, &val_list);
  850. if (unlikely(ret != 0))
  851. goto out_no_reserve;
  852. if (res->func->needs_backup && list_empty(&res->mob_head))
  853. return 0;
  854. backup_dirty = res->backup_dirty;
  855. ret = ttm_bo_validate(&res->backup->base,
  856. res->func->backup_placement,
  857. true, false);
  858. if (unlikely(ret != 0))
  859. goto out_no_validate;
  860. return 0;
  861. out_no_validate:
  862. ttm_eu_backoff_reservation(ticket, &val_list);
  863. out_no_reserve:
  864. ttm_bo_unref(&val_buf->bo);
  865. if (backup_dirty)
  866. vmw_dmabuf_unreference(&res->backup);
  867. return ret;
  868. }
  869. /**
  870. * vmw_resource_reserve - Reserve a resource for command submission
  871. *
  872. * @res: The resource to reserve.
  873. *
  874. * This function takes the resource off the LRU list and make sure
  875. * a backup buffer is present for guest-backed resources. However,
  876. * the buffer may not be bound to the resource at this point.
  877. *
  878. */
  879. int vmw_resource_reserve(struct vmw_resource *res, bool no_backup)
  880. {
  881. struct vmw_private *dev_priv = res->dev_priv;
  882. int ret;
  883. write_lock(&dev_priv->resource_lock);
  884. list_del_init(&res->lru_head);
  885. write_unlock(&dev_priv->resource_lock);
  886. if (res->func->needs_backup && res->backup == NULL &&
  887. !no_backup) {
  888. ret = vmw_resource_buf_alloc(res, true);
  889. if (unlikely(ret != 0))
  890. return ret;
  891. }
  892. return 0;
  893. }
  894. /**
  895. * vmw_resource_backoff_reservation - Unreserve and unreference a
  896. * backup buffer
  897. *.
  898. * @val_buf: Backup buffer information.
  899. */
  900. static void
  901. vmw_resource_backoff_reservation(struct ww_acquire_ctx *ticket,
  902. struct ttm_validate_buffer *val_buf)
  903. {
  904. struct list_head val_list;
  905. if (likely(val_buf->bo == NULL))
  906. return;
  907. INIT_LIST_HEAD(&val_list);
  908. list_add_tail(&val_buf->head, &val_list);
  909. ttm_eu_backoff_reservation(ticket, &val_list);
  910. ttm_bo_unref(&val_buf->bo);
  911. }
  912. /**
  913. * vmw_resource_do_evict - Evict a resource, and transfer its data
  914. * to a backup buffer.
  915. *
  916. * @res: The resource to evict.
  917. * @interruptible: Whether to wait interruptible.
  918. */
  919. int vmw_resource_do_evict(struct vmw_resource *res, bool interruptible)
  920. {
  921. struct ttm_validate_buffer val_buf;
  922. const struct vmw_res_func *func = res->func;
  923. struct ww_acquire_ctx ticket;
  924. int ret;
  925. BUG_ON(!func->may_evict);
  926. val_buf.bo = NULL;
  927. ret = vmw_resource_check_buffer(res, &ticket, interruptible,
  928. &val_buf);
  929. if (unlikely(ret != 0))
  930. return ret;
  931. if (unlikely(func->unbind != NULL &&
  932. (!func->needs_backup || !list_empty(&res->mob_head)))) {
  933. ret = func->unbind(res, res->res_dirty, &val_buf);
  934. if (unlikely(ret != 0))
  935. goto out_no_unbind;
  936. list_del_init(&res->mob_head);
  937. }
  938. ret = func->destroy(res);
  939. res->backup_dirty = true;
  940. res->res_dirty = false;
  941. out_no_unbind:
  942. vmw_resource_backoff_reservation(&ticket, &val_buf);
  943. return ret;
  944. }
  945. /**
  946. * vmw_resource_validate - Make a resource up-to-date and visible
  947. * to the device.
  948. *
  949. * @res: The resource to make visible to the device.
  950. *
  951. * On succesful return, any backup DMA buffer pointed to by @res->backup will
  952. * be reserved and validated.
  953. * On hardware resource shortage, this function will repeatedly evict
  954. * resources of the same type until the validation succeeds.
  955. */
  956. int vmw_resource_validate(struct vmw_resource *res)
  957. {
  958. int ret;
  959. struct vmw_resource *evict_res;
  960. struct vmw_private *dev_priv = res->dev_priv;
  961. struct list_head *lru_list = &dev_priv->res_lru[res->func->res_type];
  962. struct ttm_validate_buffer val_buf;
  963. unsigned err_count = 0;
  964. if (likely(!res->func->may_evict))
  965. return 0;
  966. val_buf.bo = NULL;
  967. if (res->backup)
  968. val_buf.bo = &res->backup->base;
  969. do {
  970. ret = vmw_resource_do_validate(res, &val_buf);
  971. if (likely(ret != -EBUSY))
  972. break;
  973. write_lock(&dev_priv->resource_lock);
  974. if (list_empty(lru_list) || !res->func->may_evict) {
  975. DRM_ERROR("Out of device device resources "
  976. "for %s.\n", res->func->type_name);
  977. ret = -EBUSY;
  978. write_unlock(&dev_priv->resource_lock);
  979. break;
  980. }
  981. evict_res = vmw_resource_reference
  982. (list_first_entry(lru_list, struct vmw_resource,
  983. lru_head));
  984. list_del_init(&evict_res->lru_head);
  985. write_unlock(&dev_priv->resource_lock);
  986. ret = vmw_resource_do_evict(evict_res, true);
  987. if (unlikely(ret != 0)) {
  988. write_lock(&dev_priv->resource_lock);
  989. list_add_tail(&evict_res->lru_head, lru_list);
  990. write_unlock(&dev_priv->resource_lock);
  991. if (ret == -ERESTARTSYS ||
  992. ++err_count > VMW_RES_EVICT_ERR_COUNT) {
  993. vmw_resource_unreference(&evict_res);
  994. goto out_no_validate;
  995. }
  996. }
  997. vmw_resource_unreference(&evict_res);
  998. } while (1);
  999. if (unlikely(ret != 0))
  1000. goto out_no_validate;
  1001. else if (!res->func->needs_backup && res->backup) {
  1002. list_del_init(&res->mob_head);
  1003. vmw_dmabuf_unreference(&res->backup);
  1004. }
  1005. return 0;
  1006. out_no_validate:
  1007. return ret;
  1008. }
  1009. /**
  1010. * vmw_fence_single_bo - Utility function to fence a single TTM buffer
  1011. * object without unreserving it.
  1012. *
  1013. * @bo: Pointer to the struct ttm_buffer_object to fence.
  1014. * @fence: Pointer to the fence. If NULL, this function will
  1015. * insert a fence into the command stream..
  1016. *
  1017. * Contrary to the ttm_eu version of this function, it takes only
  1018. * a single buffer object instead of a list, and it also doesn't
  1019. * unreserve the buffer object, which needs to be done separately.
  1020. */
  1021. void vmw_fence_single_bo(struct ttm_buffer_object *bo,
  1022. struct vmw_fence_obj *fence)
  1023. {
  1024. struct ttm_bo_device *bdev = bo->bdev;
  1025. struct ttm_bo_driver *driver = bdev->driver;
  1026. struct vmw_fence_obj *old_fence_obj;
  1027. struct vmw_private *dev_priv =
  1028. container_of(bdev, struct vmw_private, bdev);
  1029. if (fence == NULL)
  1030. vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
  1031. else
  1032. driver->sync_obj_ref(fence);
  1033. spin_lock(&bdev->fence_lock);
  1034. old_fence_obj = bo->sync_obj;
  1035. bo->sync_obj = fence;
  1036. spin_unlock(&bdev->fence_lock);
  1037. if (old_fence_obj)
  1038. vmw_fence_obj_unreference(&old_fence_obj);
  1039. }
  1040. /**
  1041. * vmw_resource_move_notify - TTM move_notify_callback
  1042. *
  1043. * @bo: The TTM buffer object about to move.
  1044. * @mem: The truct ttm_mem_reg indicating to what memory
  1045. * region the move is taking place.
  1046. *
  1047. * For now does nothing.
  1048. */
  1049. void vmw_resource_move_notify(struct ttm_buffer_object *bo,
  1050. struct ttm_mem_reg *mem)
  1051. {
  1052. }
  1053. /**
  1054. * vmw_resource_needs_backup - Return whether a resource needs a backup buffer.
  1055. *
  1056. * @res: The resource being queried.
  1057. */
  1058. bool vmw_resource_needs_backup(const struct vmw_resource *res)
  1059. {
  1060. return res->func->needs_backup;
  1061. }
  1062. /**
  1063. * vmw_resource_evict_type - Evict all resources of a specific type
  1064. *
  1065. * @dev_priv: Pointer to a device private struct
  1066. * @type: The resource type to evict
  1067. *
  1068. * To avoid thrashing starvation or as part of the hibernation sequence,
  1069. * try to evict all evictable resources of a specific type.
  1070. */
  1071. static void vmw_resource_evict_type(struct vmw_private *dev_priv,
  1072. enum vmw_res_type type)
  1073. {
  1074. struct list_head *lru_list = &dev_priv->res_lru[type];
  1075. struct vmw_resource *evict_res;
  1076. unsigned err_count = 0;
  1077. int ret;
  1078. do {
  1079. write_lock(&dev_priv->resource_lock);
  1080. if (list_empty(lru_list))
  1081. goto out_unlock;
  1082. evict_res = vmw_resource_reference(
  1083. list_first_entry(lru_list, struct vmw_resource,
  1084. lru_head));
  1085. list_del_init(&evict_res->lru_head);
  1086. write_unlock(&dev_priv->resource_lock);
  1087. ret = vmw_resource_do_evict(evict_res, false);
  1088. if (unlikely(ret != 0)) {
  1089. write_lock(&dev_priv->resource_lock);
  1090. list_add_tail(&evict_res->lru_head, lru_list);
  1091. write_unlock(&dev_priv->resource_lock);
  1092. if (++err_count > VMW_RES_EVICT_ERR_COUNT) {
  1093. vmw_resource_unreference(&evict_res);
  1094. return;
  1095. }
  1096. }
  1097. vmw_resource_unreference(&evict_res);
  1098. } while (1);
  1099. out_unlock:
  1100. write_unlock(&dev_priv->resource_lock);
  1101. }
  1102. /**
  1103. * vmw_resource_evict_all - Evict all evictable resources
  1104. *
  1105. * @dev_priv: Pointer to a device private struct
  1106. *
  1107. * To avoid thrashing starvation or as part of the hibernation sequence,
  1108. * evict all evictable resources. In particular this means that all
  1109. * guest-backed resources that are registered with the device are
  1110. * evicted and the OTable becomes clean.
  1111. */
  1112. void vmw_resource_evict_all(struct vmw_private *dev_priv)
  1113. {
  1114. enum vmw_res_type type;
  1115. mutex_lock(&dev_priv->cmdbuf_mutex);
  1116. for (type = 0; type < vmw_res_max; ++type)
  1117. vmw_resource_evict_type(dev_priv, type);
  1118. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1119. }