vmwgfx_resource.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  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_prime_object prime;
  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(ttm_base_object_type(base) != 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_prime_object_kfree(vmw_user_bo, prime);
  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,
  341. prime.base);
  342. bo = &vmw_user_bo->dma.base;
  343. ttm_bo_unref(&bo);
  344. }
  345. /**
  346. * vmw_user_dmabuf_alloc - Allocate a user dma buffer
  347. *
  348. * @dev_priv: Pointer to a struct device private.
  349. * @tfile: Pointer to a struct ttm_object_file on which to register the user
  350. * object.
  351. * @size: Size of the dma buffer.
  352. * @shareable: Boolean whether the buffer is shareable with other open files.
  353. * @handle: Pointer to where the handle value should be assigned.
  354. * @p_dma_buf: Pointer to where the refcounted struct vmw_dma_buffer pointer
  355. * should be assigned.
  356. */
  357. int vmw_user_dmabuf_alloc(struct vmw_private *dev_priv,
  358. struct ttm_object_file *tfile,
  359. uint32_t size,
  360. bool shareable,
  361. uint32_t *handle,
  362. struct vmw_dma_buffer **p_dma_buf)
  363. {
  364. struct vmw_user_dma_buffer *user_bo;
  365. struct ttm_buffer_object *tmp;
  366. int ret;
  367. user_bo = kzalloc(sizeof(*user_bo), GFP_KERNEL);
  368. if (unlikely(user_bo == NULL)) {
  369. DRM_ERROR("Failed to allocate a buffer.\n");
  370. return -ENOMEM;
  371. }
  372. ret = vmw_dmabuf_init(dev_priv, &user_bo->dma, size,
  373. &vmw_vram_sys_placement, true,
  374. &vmw_user_dmabuf_destroy);
  375. if (unlikely(ret != 0))
  376. return ret;
  377. tmp = ttm_bo_reference(&user_bo->dma.base);
  378. ret = ttm_prime_object_init(tfile,
  379. size,
  380. &user_bo->prime,
  381. shareable,
  382. ttm_buffer_type,
  383. &vmw_user_dmabuf_release, NULL);
  384. if (unlikely(ret != 0)) {
  385. ttm_bo_unref(&tmp);
  386. goto out_no_base_object;
  387. }
  388. *p_dma_buf = &user_bo->dma;
  389. *handle = user_bo->prime.base.hash.key;
  390. out_no_base_object:
  391. return ret;
  392. }
  393. /**
  394. * vmw_user_dmabuf_verify_access - verify access permissions on this
  395. * buffer object.
  396. *
  397. * @bo: Pointer to the buffer object being accessed
  398. * @tfile: Identifying the caller.
  399. */
  400. int vmw_user_dmabuf_verify_access(struct ttm_buffer_object *bo,
  401. struct ttm_object_file *tfile)
  402. {
  403. struct vmw_user_dma_buffer *vmw_user_bo;
  404. if (unlikely(bo->destroy != vmw_user_dmabuf_destroy))
  405. return -EPERM;
  406. vmw_user_bo = vmw_user_dma_buffer(bo);
  407. return (vmw_user_bo->prime.base.tfile == tfile ||
  408. vmw_user_bo->prime.base.shareable) ? 0 : -EPERM;
  409. }
  410. int vmw_dmabuf_alloc_ioctl(struct drm_device *dev, void *data,
  411. struct drm_file *file_priv)
  412. {
  413. struct vmw_private *dev_priv = vmw_priv(dev);
  414. union drm_vmw_alloc_dmabuf_arg *arg =
  415. (union drm_vmw_alloc_dmabuf_arg *)data;
  416. struct drm_vmw_alloc_dmabuf_req *req = &arg->req;
  417. struct drm_vmw_dmabuf_rep *rep = &arg->rep;
  418. struct vmw_dma_buffer *dma_buf;
  419. uint32_t handle;
  420. struct vmw_master *vmaster = vmw_master(file_priv->master);
  421. int ret;
  422. ret = ttm_read_lock(&vmaster->lock, true);
  423. if (unlikely(ret != 0))
  424. return ret;
  425. ret = vmw_user_dmabuf_alloc(dev_priv, vmw_fpriv(file_priv)->tfile,
  426. req->size, false, &handle, &dma_buf);
  427. if (unlikely(ret != 0))
  428. goto out_no_dmabuf;
  429. rep->handle = handle;
  430. rep->map_handle = drm_vma_node_offset_addr(&dma_buf->base.vma_node);
  431. rep->cur_gmr_id = handle;
  432. rep->cur_gmr_offset = 0;
  433. vmw_dmabuf_unreference(&dma_buf);
  434. out_no_dmabuf:
  435. ttm_read_unlock(&vmaster->lock);
  436. return ret;
  437. }
  438. int vmw_dmabuf_unref_ioctl(struct drm_device *dev, void *data,
  439. struct drm_file *file_priv)
  440. {
  441. struct drm_vmw_unref_dmabuf_arg *arg =
  442. (struct drm_vmw_unref_dmabuf_arg *)data;
  443. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  444. arg->handle,
  445. TTM_REF_USAGE);
  446. }
  447. int vmw_user_dmabuf_lookup(struct ttm_object_file *tfile,
  448. uint32_t handle, struct vmw_dma_buffer **out)
  449. {
  450. struct vmw_user_dma_buffer *vmw_user_bo;
  451. struct ttm_base_object *base;
  452. base = ttm_base_object_lookup(tfile, handle);
  453. if (unlikely(base == NULL)) {
  454. printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
  455. (unsigned long)handle);
  456. return -ESRCH;
  457. }
  458. if (unlikely(ttm_base_object_type(base) != ttm_buffer_type)) {
  459. ttm_base_object_unref(&base);
  460. printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
  461. (unsigned long)handle);
  462. return -EINVAL;
  463. }
  464. vmw_user_bo = container_of(base, struct vmw_user_dma_buffer,
  465. prime.base);
  466. (void)ttm_bo_reference(&vmw_user_bo->dma.base);
  467. ttm_base_object_unref(&base);
  468. *out = &vmw_user_bo->dma;
  469. return 0;
  470. }
  471. int vmw_user_dmabuf_reference(struct ttm_object_file *tfile,
  472. struct vmw_dma_buffer *dma_buf)
  473. {
  474. struct vmw_user_dma_buffer *user_bo;
  475. if (dma_buf->base.destroy != vmw_user_dmabuf_destroy)
  476. return -EINVAL;
  477. user_bo = container_of(dma_buf, struct vmw_user_dma_buffer, dma);
  478. return ttm_ref_object_add(tfile, &user_bo->prime.base,
  479. TTM_REF_USAGE, NULL);
  480. }
  481. /*
  482. * Stream management
  483. */
  484. static void vmw_stream_destroy(struct vmw_resource *res)
  485. {
  486. struct vmw_private *dev_priv = res->dev_priv;
  487. struct vmw_stream *stream;
  488. int ret;
  489. DRM_INFO("%s: unref\n", __func__);
  490. stream = container_of(res, struct vmw_stream, res);
  491. ret = vmw_overlay_unref(dev_priv, stream->stream_id);
  492. WARN_ON(ret != 0);
  493. }
  494. static int vmw_stream_init(struct vmw_private *dev_priv,
  495. struct vmw_stream *stream,
  496. void (*res_free) (struct vmw_resource *res))
  497. {
  498. struct vmw_resource *res = &stream->res;
  499. int ret;
  500. ret = vmw_resource_init(dev_priv, res, false, res_free,
  501. &vmw_stream_func);
  502. if (unlikely(ret != 0)) {
  503. if (res_free == NULL)
  504. kfree(stream);
  505. else
  506. res_free(&stream->res);
  507. return ret;
  508. }
  509. ret = vmw_overlay_claim(dev_priv, &stream->stream_id);
  510. if (ret) {
  511. vmw_resource_unreference(&res);
  512. return ret;
  513. }
  514. DRM_INFO("%s: claimed\n", __func__);
  515. vmw_resource_activate(&stream->res, vmw_stream_destroy);
  516. return 0;
  517. }
  518. static void vmw_user_stream_free(struct vmw_resource *res)
  519. {
  520. struct vmw_user_stream *stream =
  521. container_of(res, struct vmw_user_stream, stream.res);
  522. struct vmw_private *dev_priv = res->dev_priv;
  523. ttm_base_object_kfree(stream, base);
  524. ttm_mem_global_free(vmw_mem_glob(dev_priv),
  525. vmw_user_stream_size);
  526. }
  527. /**
  528. * This function is called when user space has no more references on the
  529. * base object. It releases the base-object's reference on the resource object.
  530. */
  531. static void vmw_user_stream_base_release(struct ttm_base_object **p_base)
  532. {
  533. struct ttm_base_object *base = *p_base;
  534. struct vmw_user_stream *stream =
  535. container_of(base, struct vmw_user_stream, base);
  536. struct vmw_resource *res = &stream->stream.res;
  537. *p_base = NULL;
  538. vmw_resource_unreference(&res);
  539. }
  540. int vmw_stream_unref_ioctl(struct drm_device *dev, void *data,
  541. struct drm_file *file_priv)
  542. {
  543. struct vmw_private *dev_priv = vmw_priv(dev);
  544. struct vmw_resource *res;
  545. struct vmw_user_stream *stream;
  546. struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
  547. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  548. struct idr *idr = &dev_priv->res_idr[vmw_res_stream];
  549. int ret = 0;
  550. res = vmw_resource_lookup(dev_priv, idr, arg->stream_id);
  551. if (unlikely(res == NULL))
  552. return -EINVAL;
  553. if (res->res_free != &vmw_user_stream_free) {
  554. ret = -EINVAL;
  555. goto out;
  556. }
  557. stream = container_of(res, struct vmw_user_stream, stream.res);
  558. if (stream->base.tfile != tfile) {
  559. ret = -EINVAL;
  560. goto out;
  561. }
  562. ttm_ref_object_base_unref(tfile, stream->base.hash.key, TTM_REF_USAGE);
  563. out:
  564. vmw_resource_unreference(&res);
  565. return ret;
  566. }
  567. int vmw_stream_claim_ioctl(struct drm_device *dev, void *data,
  568. struct drm_file *file_priv)
  569. {
  570. struct vmw_private *dev_priv = vmw_priv(dev);
  571. struct vmw_user_stream *stream;
  572. struct vmw_resource *res;
  573. struct vmw_resource *tmp;
  574. struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
  575. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  576. struct vmw_master *vmaster = vmw_master(file_priv->master);
  577. int ret;
  578. /*
  579. * Approximate idr memory usage with 128 bytes. It will be limited
  580. * by maximum number_of streams anyway?
  581. */
  582. if (unlikely(vmw_user_stream_size == 0))
  583. vmw_user_stream_size = ttm_round_pot(sizeof(*stream)) + 128;
  584. ret = ttm_read_lock(&vmaster->lock, true);
  585. if (unlikely(ret != 0))
  586. return ret;
  587. ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
  588. vmw_user_stream_size,
  589. false, true);
  590. if (unlikely(ret != 0)) {
  591. if (ret != -ERESTARTSYS)
  592. DRM_ERROR("Out of graphics memory for stream"
  593. " creation.\n");
  594. goto out_unlock;
  595. }
  596. stream = kmalloc(sizeof(*stream), GFP_KERNEL);
  597. if (unlikely(stream == NULL)) {
  598. ttm_mem_global_free(vmw_mem_glob(dev_priv),
  599. vmw_user_stream_size);
  600. ret = -ENOMEM;
  601. goto out_unlock;
  602. }
  603. res = &stream->stream.res;
  604. stream->base.shareable = false;
  605. stream->base.tfile = NULL;
  606. /*
  607. * From here on, the destructor takes over resource freeing.
  608. */
  609. ret = vmw_stream_init(dev_priv, &stream->stream, vmw_user_stream_free);
  610. if (unlikely(ret != 0))
  611. goto out_unlock;
  612. tmp = vmw_resource_reference(res);
  613. ret = ttm_base_object_init(tfile, &stream->base, false, VMW_RES_STREAM,
  614. &vmw_user_stream_base_release, NULL);
  615. if (unlikely(ret != 0)) {
  616. vmw_resource_unreference(&tmp);
  617. goto out_err;
  618. }
  619. arg->stream_id = res->id;
  620. out_err:
  621. vmw_resource_unreference(&res);
  622. out_unlock:
  623. ttm_read_unlock(&vmaster->lock);
  624. return ret;
  625. }
  626. int vmw_user_stream_lookup(struct vmw_private *dev_priv,
  627. struct ttm_object_file *tfile,
  628. uint32_t *inout_id, struct vmw_resource **out)
  629. {
  630. struct vmw_user_stream *stream;
  631. struct vmw_resource *res;
  632. int ret;
  633. res = vmw_resource_lookup(dev_priv, &dev_priv->res_idr[vmw_res_stream],
  634. *inout_id);
  635. if (unlikely(res == NULL))
  636. return -EINVAL;
  637. if (res->res_free != &vmw_user_stream_free) {
  638. ret = -EINVAL;
  639. goto err_ref;
  640. }
  641. stream = container_of(res, struct vmw_user_stream, stream.res);
  642. if (stream->base.tfile != tfile) {
  643. ret = -EPERM;
  644. goto err_ref;
  645. }
  646. *inout_id = stream->stream.stream_id;
  647. *out = res;
  648. return 0;
  649. err_ref:
  650. vmw_resource_unreference(&res);
  651. return ret;
  652. }
  653. /**
  654. * vmw_dumb_create - Create a dumb kms buffer
  655. *
  656. * @file_priv: Pointer to a struct drm_file identifying the caller.
  657. * @dev: Pointer to the drm device.
  658. * @args: Pointer to a struct drm_mode_create_dumb structure
  659. *
  660. * This is a driver callback for the core drm create_dumb functionality.
  661. * Note that this is very similar to the vmw_dmabuf_alloc ioctl, except
  662. * that the arguments have a different format.
  663. */
  664. int vmw_dumb_create(struct drm_file *file_priv,
  665. struct drm_device *dev,
  666. struct drm_mode_create_dumb *args)
  667. {
  668. struct vmw_private *dev_priv = vmw_priv(dev);
  669. struct vmw_master *vmaster = vmw_master(file_priv->master);
  670. struct vmw_dma_buffer *dma_buf;
  671. int ret;
  672. args->pitch = args->width * ((args->bpp + 7) / 8);
  673. args->size = args->pitch * args->height;
  674. ret = ttm_read_lock(&vmaster->lock, true);
  675. if (unlikely(ret != 0))
  676. return ret;
  677. ret = vmw_user_dmabuf_alloc(dev_priv, vmw_fpriv(file_priv)->tfile,
  678. args->size, false, &args->handle,
  679. &dma_buf);
  680. if (unlikely(ret != 0))
  681. goto out_no_dmabuf;
  682. vmw_dmabuf_unreference(&dma_buf);
  683. out_no_dmabuf:
  684. ttm_read_unlock(&vmaster->lock);
  685. return ret;
  686. }
  687. /**
  688. * vmw_dumb_map_offset - Return the address space offset of a dumb buffer
  689. *
  690. * @file_priv: Pointer to a struct drm_file identifying the caller.
  691. * @dev: Pointer to the drm device.
  692. * @handle: Handle identifying the dumb buffer.
  693. * @offset: The address space offset returned.
  694. *
  695. * This is a driver callback for the core drm dumb_map_offset functionality.
  696. */
  697. int vmw_dumb_map_offset(struct drm_file *file_priv,
  698. struct drm_device *dev, uint32_t handle,
  699. uint64_t *offset)
  700. {
  701. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  702. struct vmw_dma_buffer *out_buf;
  703. int ret;
  704. ret = vmw_user_dmabuf_lookup(tfile, handle, &out_buf);
  705. if (ret != 0)
  706. return -EINVAL;
  707. *offset = drm_vma_node_offset_addr(&out_buf->base.vma_node);
  708. vmw_dmabuf_unreference(&out_buf);
  709. return 0;
  710. }
  711. /**
  712. * vmw_dumb_destroy - Destroy a dumb boffer
  713. *
  714. * @file_priv: Pointer to a struct drm_file identifying the caller.
  715. * @dev: Pointer to the drm device.
  716. * @handle: Handle identifying the dumb buffer.
  717. *
  718. * This is a driver callback for the core drm dumb_destroy functionality.
  719. */
  720. int vmw_dumb_destroy(struct drm_file *file_priv,
  721. struct drm_device *dev,
  722. uint32_t handle)
  723. {
  724. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  725. handle, TTM_REF_USAGE);
  726. }
  727. /**
  728. * vmw_resource_buf_alloc - Allocate a backup buffer for a resource.
  729. *
  730. * @res: The resource for which to allocate a backup buffer.
  731. * @interruptible: Whether any sleeps during allocation should be
  732. * performed while interruptible.
  733. */
  734. static int vmw_resource_buf_alloc(struct vmw_resource *res,
  735. bool interruptible)
  736. {
  737. unsigned long size =
  738. (res->backup_size + PAGE_SIZE - 1) & PAGE_MASK;
  739. struct vmw_dma_buffer *backup;
  740. int ret;
  741. if (likely(res->backup)) {
  742. BUG_ON(res->backup->base.num_pages * PAGE_SIZE < size);
  743. return 0;
  744. }
  745. backup = kzalloc(sizeof(*backup), GFP_KERNEL);
  746. if (unlikely(backup == NULL))
  747. return -ENOMEM;
  748. ret = vmw_dmabuf_init(res->dev_priv, backup, res->backup_size,
  749. res->func->backup_placement,
  750. interruptible,
  751. &vmw_dmabuf_bo_free);
  752. if (unlikely(ret != 0))
  753. goto out_no_dmabuf;
  754. res->backup = backup;
  755. out_no_dmabuf:
  756. return ret;
  757. }
  758. /**
  759. * vmw_resource_do_validate - Make a resource up-to-date and visible
  760. * to the device.
  761. *
  762. * @res: The resource to make visible to the device.
  763. * @val_buf: Information about a buffer possibly
  764. * containing backup data if a bind operation is needed.
  765. *
  766. * On hardware resource shortage, this function returns -EBUSY and
  767. * should be retried once resources have been freed up.
  768. */
  769. static int vmw_resource_do_validate(struct vmw_resource *res,
  770. struct ttm_validate_buffer *val_buf)
  771. {
  772. int ret = 0;
  773. const struct vmw_res_func *func = res->func;
  774. if (unlikely(res->id == -1)) {
  775. ret = func->create(res);
  776. if (unlikely(ret != 0))
  777. return ret;
  778. }
  779. if (func->bind &&
  780. ((func->needs_backup && list_empty(&res->mob_head) &&
  781. val_buf->bo != NULL) ||
  782. (!func->needs_backup && val_buf->bo != NULL))) {
  783. ret = func->bind(res, val_buf);
  784. if (unlikely(ret != 0))
  785. goto out_bind_failed;
  786. if (func->needs_backup)
  787. list_add_tail(&res->mob_head, &res->backup->res_list);
  788. }
  789. /*
  790. * Only do this on write operations, and move to
  791. * vmw_resource_unreserve if it can be called after
  792. * backup buffers have been unreserved. Otherwise
  793. * sort out locking.
  794. */
  795. res->res_dirty = true;
  796. return 0;
  797. out_bind_failed:
  798. func->destroy(res);
  799. return ret;
  800. }
  801. /**
  802. * vmw_resource_unreserve - Unreserve a resource previously reserved for
  803. * command submission.
  804. *
  805. * @res: Pointer to the struct vmw_resource to unreserve.
  806. * @new_backup: Pointer to new backup buffer if command submission
  807. * switched.
  808. * @new_backup_offset: New backup offset if @new_backup is !NULL.
  809. *
  810. * Currently unreserving a resource means putting it back on the device's
  811. * resource lru list, so that it can be evicted if necessary.
  812. */
  813. void vmw_resource_unreserve(struct vmw_resource *res,
  814. struct vmw_dma_buffer *new_backup,
  815. unsigned long new_backup_offset)
  816. {
  817. struct vmw_private *dev_priv = res->dev_priv;
  818. if (!list_empty(&res->lru_head))
  819. return;
  820. if (new_backup && new_backup != res->backup) {
  821. if (res->backup) {
  822. lockdep_assert_held(&res->backup->base.resv->lock.base);
  823. list_del_init(&res->mob_head);
  824. vmw_dmabuf_unreference(&res->backup);
  825. }
  826. res->backup = vmw_dmabuf_reference(new_backup);
  827. lockdep_assert_held(&new_backup->base.resv->lock.base);
  828. list_add_tail(&res->mob_head, &new_backup->res_list);
  829. }
  830. if (new_backup)
  831. res->backup_offset = new_backup_offset;
  832. if (!res->func->may_evict || res->id == -1)
  833. return;
  834. write_lock(&dev_priv->resource_lock);
  835. list_add_tail(&res->lru_head,
  836. &res->dev_priv->res_lru[res->func->res_type]);
  837. write_unlock(&dev_priv->resource_lock);
  838. }
  839. /**
  840. * vmw_resource_check_buffer - Check whether a backup buffer is needed
  841. * for a resource and in that case, allocate
  842. * one, reserve and validate it.
  843. *
  844. * @res: The resource for which to allocate a backup buffer.
  845. * @interruptible: Whether any sleeps during allocation should be
  846. * performed while interruptible.
  847. * @val_buf: On successful return contains data about the
  848. * reserved and validated backup buffer.
  849. */
  850. static int
  851. vmw_resource_check_buffer(struct vmw_resource *res,
  852. bool interruptible,
  853. struct ttm_validate_buffer *val_buf)
  854. {
  855. struct list_head val_list;
  856. bool backup_dirty = false;
  857. int ret;
  858. if (unlikely(res->backup == NULL)) {
  859. ret = vmw_resource_buf_alloc(res, interruptible);
  860. if (unlikely(ret != 0))
  861. return ret;
  862. }
  863. INIT_LIST_HEAD(&val_list);
  864. val_buf->bo = ttm_bo_reference(&res->backup->base);
  865. list_add_tail(&val_buf->head, &val_list);
  866. ret = ttm_eu_reserve_buffers(NULL, &val_list);
  867. if (unlikely(ret != 0))
  868. goto out_no_reserve;
  869. if (res->func->needs_backup && list_empty(&res->mob_head))
  870. return 0;
  871. backup_dirty = res->backup_dirty;
  872. ret = ttm_bo_validate(&res->backup->base,
  873. res->func->backup_placement,
  874. true, false);
  875. if (unlikely(ret != 0))
  876. goto out_no_validate;
  877. return 0;
  878. out_no_validate:
  879. ttm_eu_backoff_reservation(NULL, &val_list);
  880. out_no_reserve:
  881. ttm_bo_unref(&val_buf->bo);
  882. if (backup_dirty)
  883. vmw_dmabuf_unreference(&res->backup);
  884. return ret;
  885. }
  886. /**
  887. * vmw_resource_reserve - Reserve a resource for command submission
  888. *
  889. * @res: The resource to reserve.
  890. *
  891. * This function takes the resource off the LRU list and make sure
  892. * a backup buffer is present for guest-backed resources. However,
  893. * the buffer may not be bound to the resource at this point.
  894. *
  895. */
  896. int vmw_resource_reserve(struct vmw_resource *res, bool no_backup)
  897. {
  898. struct vmw_private *dev_priv = res->dev_priv;
  899. int ret;
  900. write_lock(&dev_priv->resource_lock);
  901. list_del_init(&res->lru_head);
  902. write_unlock(&dev_priv->resource_lock);
  903. if (res->func->needs_backup && res->backup == NULL &&
  904. !no_backup) {
  905. ret = vmw_resource_buf_alloc(res, true);
  906. if (unlikely(ret != 0))
  907. return ret;
  908. }
  909. return 0;
  910. }
  911. /**
  912. * vmw_resource_backoff_reservation - Unreserve and unreference a
  913. * backup buffer
  914. *.
  915. * @val_buf: Backup buffer information.
  916. */
  917. static void
  918. vmw_resource_backoff_reservation(struct ttm_validate_buffer *val_buf)
  919. {
  920. struct list_head val_list;
  921. if (likely(val_buf->bo == NULL))
  922. return;
  923. INIT_LIST_HEAD(&val_list);
  924. list_add_tail(&val_buf->head, &val_list);
  925. ttm_eu_backoff_reservation(NULL, &val_list);
  926. ttm_bo_unref(&val_buf->bo);
  927. }
  928. /**
  929. * vmw_resource_do_evict - Evict a resource, and transfer its data
  930. * to a backup buffer.
  931. *
  932. * @res: The resource to evict.
  933. * @interruptible: Whether to wait interruptible.
  934. */
  935. int vmw_resource_do_evict(struct vmw_resource *res, bool interruptible)
  936. {
  937. struct ttm_validate_buffer val_buf;
  938. const struct vmw_res_func *func = res->func;
  939. int ret;
  940. BUG_ON(!func->may_evict);
  941. val_buf.bo = NULL;
  942. ret = vmw_resource_check_buffer(res, interruptible, &val_buf);
  943. if (unlikely(ret != 0))
  944. return ret;
  945. if (unlikely(func->unbind != NULL &&
  946. (!func->needs_backup || !list_empty(&res->mob_head)))) {
  947. ret = func->unbind(res, res->res_dirty, &val_buf);
  948. if (unlikely(ret != 0))
  949. goto out_no_unbind;
  950. list_del_init(&res->mob_head);
  951. }
  952. ret = func->destroy(res);
  953. res->backup_dirty = true;
  954. res->res_dirty = false;
  955. out_no_unbind:
  956. vmw_resource_backoff_reservation(&val_buf);
  957. return ret;
  958. }
  959. /**
  960. * vmw_resource_validate - Make a resource up-to-date and visible
  961. * to the device.
  962. *
  963. * @res: The resource to make visible to the device.
  964. *
  965. * On succesful return, any backup DMA buffer pointed to by @res->backup will
  966. * be reserved and validated.
  967. * On hardware resource shortage, this function will repeatedly evict
  968. * resources of the same type until the validation succeeds.
  969. */
  970. int vmw_resource_validate(struct vmw_resource *res)
  971. {
  972. int ret;
  973. struct vmw_resource *evict_res;
  974. struct vmw_private *dev_priv = res->dev_priv;
  975. struct list_head *lru_list = &dev_priv->res_lru[res->func->res_type];
  976. struct ttm_validate_buffer val_buf;
  977. unsigned err_count = 0;
  978. if (likely(!res->func->may_evict))
  979. return 0;
  980. val_buf.bo = NULL;
  981. if (res->backup)
  982. val_buf.bo = &res->backup->base;
  983. do {
  984. ret = vmw_resource_do_validate(res, &val_buf);
  985. if (likely(ret != -EBUSY))
  986. break;
  987. write_lock(&dev_priv->resource_lock);
  988. if (list_empty(lru_list) || !res->func->may_evict) {
  989. DRM_ERROR("Out of device device resources "
  990. "for %s.\n", res->func->type_name);
  991. ret = -EBUSY;
  992. write_unlock(&dev_priv->resource_lock);
  993. break;
  994. }
  995. evict_res = vmw_resource_reference
  996. (list_first_entry(lru_list, struct vmw_resource,
  997. lru_head));
  998. list_del_init(&evict_res->lru_head);
  999. write_unlock(&dev_priv->resource_lock);
  1000. ret = vmw_resource_do_evict(evict_res, true);
  1001. if (unlikely(ret != 0)) {
  1002. write_lock(&dev_priv->resource_lock);
  1003. list_add_tail(&evict_res->lru_head, lru_list);
  1004. write_unlock(&dev_priv->resource_lock);
  1005. if (ret == -ERESTARTSYS ||
  1006. ++err_count > VMW_RES_EVICT_ERR_COUNT) {
  1007. vmw_resource_unreference(&evict_res);
  1008. goto out_no_validate;
  1009. }
  1010. }
  1011. vmw_resource_unreference(&evict_res);
  1012. } while (1);
  1013. if (unlikely(ret != 0))
  1014. goto out_no_validate;
  1015. else if (!res->func->needs_backup && res->backup) {
  1016. list_del_init(&res->mob_head);
  1017. vmw_dmabuf_unreference(&res->backup);
  1018. }
  1019. return 0;
  1020. out_no_validate:
  1021. return ret;
  1022. }
  1023. /**
  1024. * vmw_fence_single_bo - Utility function to fence a single TTM buffer
  1025. * object without unreserving it.
  1026. *
  1027. * @bo: Pointer to the struct ttm_buffer_object to fence.
  1028. * @fence: Pointer to the fence. If NULL, this function will
  1029. * insert a fence into the command stream..
  1030. *
  1031. * Contrary to the ttm_eu version of this function, it takes only
  1032. * a single buffer object instead of a list, and it also doesn't
  1033. * unreserve the buffer object, which needs to be done separately.
  1034. */
  1035. void vmw_fence_single_bo(struct ttm_buffer_object *bo,
  1036. struct vmw_fence_obj *fence)
  1037. {
  1038. struct ttm_bo_device *bdev = bo->bdev;
  1039. struct ttm_bo_driver *driver = bdev->driver;
  1040. struct vmw_fence_obj *old_fence_obj;
  1041. struct vmw_private *dev_priv =
  1042. container_of(bdev, struct vmw_private, bdev);
  1043. if (fence == NULL)
  1044. vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
  1045. else
  1046. driver->sync_obj_ref(fence);
  1047. spin_lock(&bdev->fence_lock);
  1048. old_fence_obj = bo->sync_obj;
  1049. bo->sync_obj = fence;
  1050. spin_unlock(&bdev->fence_lock);
  1051. if (old_fence_obj)
  1052. vmw_fence_obj_unreference(&old_fence_obj);
  1053. }
  1054. /**
  1055. * vmw_resource_move_notify - TTM move_notify_callback
  1056. *
  1057. * @bo: The TTM buffer object about to move.
  1058. * @mem: The truct ttm_mem_reg indicating to what memory
  1059. * region the move is taking place.
  1060. *
  1061. * For now does nothing.
  1062. */
  1063. void vmw_resource_move_notify(struct ttm_buffer_object *bo,
  1064. struct ttm_mem_reg *mem)
  1065. {
  1066. }
  1067. /**
  1068. * vmw_resource_needs_backup - Return whether a resource needs a backup buffer.
  1069. *
  1070. * @res: The resource being queried.
  1071. */
  1072. bool vmw_resource_needs_backup(const struct vmw_resource *res)
  1073. {
  1074. return res->func->needs_backup;
  1075. }
  1076. /**
  1077. * vmw_resource_evict_type - Evict all resources of a specific type
  1078. *
  1079. * @dev_priv: Pointer to a device private struct
  1080. * @type: The resource type to evict
  1081. *
  1082. * To avoid thrashing starvation or as part of the hibernation sequence,
  1083. * try to evict all evictable resources of a specific type.
  1084. */
  1085. static void vmw_resource_evict_type(struct vmw_private *dev_priv,
  1086. enum vmw_res_type type)
  1087. {
  1088. struct list_head *lru_list = &dev_priv->res_lru[type];
  1089. struct vmw_resource *evict_res;
  1090. unsigned err_count = 0;
  1091. int ret;
  1092. do {
  1093. write_lock(&dev_priv->resource_lock);
  1094. if (list_empty(lru_list))
  1095. goto out_unlock;
  1096. evict_res = vmw_resource_reference(
  1097. list_first_entry(lru_list, struct vmw_resource,
  1098. lru_head));
  1099. list_del_init(&evict_res->lru_head);
  1100. write_unlock(&dev_priv->resource_lock);
  1101. ret = vmw_resource_do_evict(evict_res, false);
  1102. if (unlikely(ret != 0)) {
  1103. write_lock(&dev_priv->resource_lock);
  1104. list_add_tail(&evict_res->lru_head, lru_list);
  1105. write_unlock(&dev_priv->resource_lock);
  1106. if (++err_count > VMW_RES_EVICT_ERR_COUNT) {
  1107. vmw_resource_unreference(&evict_res);
  1108. return;
  1109. }
  1110. }
  1111. vmw_resource_unreference(&evict_res);
  1112. } while (1);
  1113. out_unlock:
  1114. write_unlock(&dev_priv->resource_lock);
  1115. }
  1116. /**
  1117. * vmw_resource_evict_all - Evict all evictable resources
  1118. *
  1119. * @dev_priv: Pointer to a device private struct
  1120. *
  1121. * To avoid thrashing starvation or as part of the hibernation sequence,
  1122. * evict all evictable resources. In particular this means that all
  1123. * guest-backed resources that are registered with the device are
  1124. * evicted and the OTable becomes clean.
  1125. */
  1126. void vmw_resource_evict_all(struct vmw_private *dev_priv)
  1127. {
  1128. enum vmw_res_type type;
  1129. mutex_lock(&dev_priv->cmdbuf_mutex);
  1130. for (type = 0; type < vmw_res_max; ++type)
  1131. vmw_resource_evict_type(dev_priv, type);
  1132. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1133. }