vmwgfx_resource.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  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. bool interruptible,
  835. struct ttm_validate_buffer *val_buf)
  836. {
  837. struct list_head val_list;
  838. bool backup_dirty = false;
  839. int ret;
  840. if (unlikely(res->backup == NULL)) {
  841. ret = vmw_resource_buf_alloc(res, interruptible);
  842. if (unlikely(ret != 0))
  843. return ret;
  844. }
  845. INIT_LIST_HEAD(&val_list);
  846. val_buf->bo = ttm_bo_reference(&res->backup->base);
  847. list_add_tail(&val_buf->head, &val_list);
  848. ret = ttm_eu_reserve_buffers(NULL, &val_list);
  849. if (unlikely(ret != 0))
  850. goto out_no_reserve;
  851. if (res->func->needs_backup && list_empty(&res->mob_head))
  852. return 0;
  853. backup_dirty = res->backup_dirty;
  854. ret = ttm_bo_validate(&res->backup->base,
  855. res->func->backup_placement,
  856. true, false);
  857. if (unlikely(ret != 0))
  858. goto out_no_validate;
  859. return 0;
  860. out_no_validate:
  861. ttm_eu_backoff_reservation(NULL, &val_list);
  862. out_no_reserve:
  863. ttm_bo_unref(&val_buf->bo);
  864. if (backup_dirty)
  865. vmw_dmabuf_unreference(&res->backup);
  866. return ret;
  867. }
  868. /**
  869. * vmw_resource_reserve - Reserve a resource for command submission
  870. *
  871. * @res: The resource to reserve.
  872. *
  873. * This function takes the resource off the LRU list and make sure
  874. * a backup buffer is present for guest-backed resources. However,
  875. * the buffer may not be bound to the resource at this point.
  876. *
  877. */
  878. int vmw_resource_reserve(struct vmw_resource *res, bool no_backup)
  879. {
  880. struct vmw_private *dev_priv = res->dev_priv;
  881. int ret;
  882. write_lock(&dev_priv->resource_lock);
  883. list_del_init(&res->lru_head);
  884. write_unlock(&dev_priv->resource_lock);
  885. if (res->func->needs_backup && res->backup == NULL &&
  886. !no_backup) {
  887. ret = vmw_resource_buf_alloc(res, true);
  888. if (unlikely(ret != 0))
  889. return ret;
  890. }
  891. return 0;
  892. }
  893. /**
  894. * vmw_resource_backoff_reservation - Unreserve and unreference a
  895. * backup buffer
  896. *.
  897. * @val_buf: Backup buffer information.
  898. */
  899. static void
  900. vmw_resource_backoff_reservation(struct ttm_validate_buffer *val_buf)
  901. {
  902. struct list_head val_list;
  903. if (likely(val_buf->bo == NULL))
  904. return;
  905. INIT_LIST_HEAD(&val_list);
  906. list_add_tail(&val_buf->head, &val_list);
  907. ttm_eu_backoff_reservation(NULL, &val_list);
  908. ttm_bo_unref(&val_buf->bo);
  909. }
  910. /**
  911. * vmw_resource_do_evict - Evict a resource, and transfer its data
  912. * to a backup buffer.
  913. *
  914. * @res: The resource to evict.
  915. * @interruptible: Whether to wait interruptible.
  916. */
  917. int vmw_resource_do_evict(struct vmw_resource *res, bool interruptible)
  918. {
  919. struct ttm_validate_buffer val_buf;
  920. const struct vmw_res_func *func = res->func;
  921. int ret;
  922. BUG_ON(!func->may_evict);
  923. val_buf.bo = NULL;
  924. ret = vmw_resource_check_buffer(res, interruptible, &val_buf);
  925. if (unlikely(ret != 0))
  926. return ret;
  927. if (unlikely(func->unbind != NULL &&
  928. (!func->needs_backup || !list_empty(&res->mob_head)))) {
  929. ret = func->unbind(res, res->res_dirty, &val_buf);
  930. if (unlikely(ret != 0))
  931. goto out_no_unbind;
  932. list_del_init(&res->mob_head);
  933. }
  934. ret = func->destroy(res);
  935. res->backup_dirty = true;
  936. res->res_dirty = false;
  937. out_no_unbind:
  938. vmw_resource_backoff_reservation(&val_buf);
  939. return ret;
  940. }
  941. /**
  942. * vmw_resource_validate - Make a resource up-to-date and visible
  943. * to the device.
  944. *
  945. * @res: The resource to make visible to the device.
  946. *
  947. * On succesful return, any backup DMA buffer pointed to by @res->backup will
  948. * be reserved and validated.
  949. * On hardware resource shortage, this function will repeatedly evict
  950. * resources of the same type until the validation succeeds.
  951. */
  952. int vmw_resource_validate(struct vmw_resource *res)
  953. {
  954. int ret;
  955. struct vmw_resource *evict_res;
  956. struct vmw_private *dev_priv = res->dev_priv;
  957. struct list_head *lru_list = &dev_priv->res_lru[res->func->res_type];
  958. struct ttm_validate_buffer val_buf;
  959. unsigned err_count = 0;
  960. if (likely(!res->func->may_evict))
  961. return 0;
  962. val_buf.bo = NULL;
  963. if (res->backup)
  964. val_buf.bo = &res->backup->base;
  965. do {
  966. ret = vmw_resource_do_validate(res, &val_buf);
  967. if (likely(ret != -EBUSY))
  968. break;
  969. write_lock(&dev_priv->resource_lock);
  970. if (list_empty(lru_list) || !res->func->may_evict) {
  971. DRM_ERROR("Out of device device resources "
  972. "for %s.\n", res->func->type_name);
  973. ret = -EBUSY;
  974. write_unlock(&dev_priv->resource_lock);
  975. break;
  976. }
  977. evict_res = vmw_resource_reference
  978. (list_first_entry(lru_list, struct vmw_resource,
  979. lru_head));
  980. list_del_init(&evict_res->lru_head);
  981. write_unlock(&dev_priv->resource_lock);
  982. ret = vmw_resource_do_evict(evict_res, true);
  983. if (unlikely(ret != 0)) {
  984. write_lock(&dev_priv->resource_lock);
  985. list_add_tail(&evict_res->lru_head, lru_list);
  986. write_unlock(&dev_priv->resource_lock);
  987. if (ret == -ERESTARTSYS ||
  988. ++err_count > VMW_RES_EVICT_ERR_COUNT) {
  989. vmw_resource_unreference(&evict_res);
  990. goto out_no_validate;
  991. }
  992. }
  993. vmw_resource_unreference(&evict_res);
  994. } while (1);
  995. if (unlikely(ret != 0))
  996. goto out_no_validate;
  997. else if (!res->func->needs_backup && res->backup) {
  998. list_del_init(&res->mob_head);
  999. vmw_dmabuf_unreference(&res->backup);
  1000. }
  1001. return 0;
  1002. out_no_validate:
  1003. return ret;
  1004. }
  1005. /**
  1006. * vmw_fence_single_bo - Utility function to fence a single TTM buffer
  1007. * object without unreserving it.
  1008. *
  1009. * @bo: Pointer to the struct ttm_buffer_object to fence.
  1010. * @fence: Pointer to the fence. If NULL, this function will
  1011. * insert a fence into the command stream..
  1012. *
  1013. * Contrary to the ttm_eu version of this function, it takes only
  1014. * a single buffer object instead of a list, and it also doesn't
  1015. * unreserve the buffer object, which needs to be done separately.
  1016. */
  1017. void vmw_fence_single_bo(struct ttm_buffer_object *bo,
  1018. struct vmw_fence_obj *fence)
  1019. {
  1020. struct ttm_bo_device *bdev = bo->bdev;
  1021. struct ttm_bo_driver *driver = bdev->driver;
  1022. struct vmw_fence_obj *old_fence_obj;
  1023. struct vmw_private *dev_priv =
  1024. container_of(bdev, struct vmw_private, bdev);
  1025. if (fence == NULL)
  1026. vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
  1027. else
  1028. driver->sync_obj_ref(fence);
  1029. spin_lock(&bdev->fence_lock);
  1030. old_fence_obj = bo->sync_obj;
  1031. bo->sync_obj = fence;
  1032. spin_unlock(&bdev->fence_lock);
  1033. if (old_fence_obj)
  1034. vmw_fence_obj_unreference(&old_fence_obj);
  1035. }
  1036. /**
  1037. * vmw_resource_move_notify - TTM move_notify_callback
  1038. *
  1039. * @bo: The TTM buffer object about to move.
  1040. * @mem: The truct ttm_mem_reg indicating to what memory
  1041. * region the move is taking place.
  1042. *
  1043. * For now does nothing.
  1044. */
  1045. void vmw_resource_move_notify(struct ttm_buffer_object *bo,
  1046. struct ttm_mem_reg *mem)
  1047. {
  1048. }
  1049. /**
  1050. * vmw_resource_needs_backup - Return whether a resource needs a backup buffer.
  1051. *
  1052. * @res: The resource being queried.
  1053. */
  1054. bool vmw_resource_needs_backup(const struct vmw_resource *res)
  1055. {
  1056. return res->func->needs_backup;
  1057. }
  1058. /**
  1059. * vmw_resource_evict_type - Evict all resources of a specific type
  1060. *
  1061. * @dev_priv: Pointer to a device private struct
  1062. * @type: The resource type to evict
  1063. *
  1064. * To avoid thrashing starvation or as part of the hibernation sequence,
  1065. * try to evict all evictable resources of a specific type.
  1066. */
  1067. static void vmw_resource_evict_type(struct vmw_private *dev_priv,
  1068. enum vmw_res_type type)
  1069. {
  1070. struct list_head *lru_list = &dev_priv->res_lru[type];
  1071. struct vmw_resource *evict_res;
  1072. unsigned err_count = 0;
  1073. int ret;
  1074. do {
  1075. write_lock(&dev_priv->resource_lock);
  1076. if (list_empty(lru_list))
  1077. goto out_unlock;
  1078. evict_res = vmw_resource_reference(
  1079. list_first_entry(lru_list, struct vmw_resource,
  1080. lru_head));
  1081. list_del_init(&evict_res->lru_head);
  1082. write_unlock(&dev_priv->resource_lock);
  1083. ret = vmw_resource_do_evict(evict_res, false);
  1084. if (unlikely(ret != 0)) {
  1085. write_lock(&dev_priv->resource_lock);
  1086. list_add_tail(&evict_res->lru_head, lru_list);
  1087. write_unlock(&dev_priv->resource_lock);
  1088. if (++err_count > VMW_RES_EVICT_ERR_COUNT) {
  1089. vmw_resource_unreference(&evict_res);
  1090. return;
  1091. }
  1092. }
  1093. vmw_resource_unreference(&evict_res);
  1094. } while (1);
  1095. out_unlock:
  1096. write_unlock(&dev_priv->resource_lock);
  1097. }
  1098. /**
  1099. * vmw_resource_evict_all - Evict all evictable resources
  1100. *
  1101. * @dev_priv: Pointer to a device private struct
  1102. *
  1103. * To avoid thrashing starvation or as part of the hibernation sequence,
  1104. * evict all evictable resources. In particular this means that all
  1105. * guest-backed resources that are registered with the device are
  1106. * evicted and the OTable becomes clean.
  1107. */
  1108. void vmw_resource_evict_all(struct vmw_private *dev_priv)
  1109. {
  1110. enum vmw_res_type type;
  1111. mutex_lock(&dev_priv->cmdbuf_mutex);
  1112. for (type = 0; type < vmw_res_max; ++type)
  1113. vmw_resource_evict_type(dev_priv, type);
  1114. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1115. }