vmwgfx_resource.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  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. int vmw_dumb_create(struct drm_file *file_priv,
  654. struct drm_device *dev,
  655. struct drm_mode_create_dumb *args)
  656. {
  657. struct vmw_private *dev_priv = vmw_priv(dev);
  658. struct vmw_master *vmaster = vmw_master(file_priv->master);
  659. struct vmw_user_dma_buffer *vmw_user_bo;
  660. struct ttm_buffer_object *tmp;
  661. int ret;
  662. args->pitch = args->width * ((args->bpp + 7) / 8);
  663. args->size = args->pitch * args->height;
  664. vmw_user_bo = kzalloc(sizeof(*vmw_user_bo), GFP_KERNEL);
  665. if (vmw_user_bo == NULL)
  666. return -ENOMEM;
  667. ret = ttm_read_lock(&vmaster->lock, true);
  668. if (ret != 0) {
  669. kfree(vmw_user_bo);
  670. return ret;
  671. }
  672. ret = vmw_dmabuf_init(dev_priv, &vmw_user_bo->dma, args->size,
  673. &vmw_vram_sys_placement, true,
  674. &vmw_user_dmabuf_destroy);
  675. if (ret != 0)
  676. goto out_no_dmabuf;
  677. tmp = ttm_bo_reference(&vmw_user_bo->dma.base);
  678. ret = ttm_prime_object_init(vmw_fpriv(file_priv)->tfile,
  679. args->size,
  680. &vmw_user_bo->prime,
  681. false,
  682. ttm_buffer_type,
  683. &vmw_user_dmabuf_release, NULL);
  684. if (unlikely(ret != 0))
  685. goto out_no_base_object;
  686. args->handle = vmw_user_bo->prime.base.hash.key;
  687. out_no_base_object:
  688. ttm_bo_unref(&tmp);
  689. out_no_dmabuf:
  690. ttm_read_unlock(&vmaster->lock);
  691. return ret;
  692. }
  693. int vmw_dumb_map_offset(struct drm_file *file_priv,
  694. struct drm_device *dev, uint32_t handle,
  695. uint64_t *offset)
  696. {
  697. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  698. struct vmw_dma_buffer *out_buf;
  699. int ret;
  700. ret = vmw_user_dmabuf_lookup(tfile, handle, &out_buf);
  701. if (ret != 0)
  702. return -EINVAL;
  703. *offset = drm_vma_node_offset_addr(&out_buf->base.vma_node);
  704. vmw_dmabuf_unreference(&out_buf);
  705. return 0;
  706. }
  707. int vmw_dumb_destroy(struct drm_file *file_priv,
  708. struct drm_device *dev,
  709. uint32_t handle)
  710. {
  711. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  712. handle, TTM_REF_USAGE);
  713. }
  714. /**
  715. * vmw_resource_buf_alloc - Allocate a backup buffer for a resource.
  716. *
  717. * @res: The resource for which to allocate a backup buffer.
  718. * @interruptible: Whether any sleeps during allocation should be
  719. * performed while interruptible.
  720. */
  721. static int vmw_resource_buf_alloc(struct vmw_resource *res,
  722. bool interruptible)
  723. {
  724. unsigned long size =
  725. (res->backup_size + PAGE_SIZE - 1) & PAGE_MASK;
  726. struct vmw_dma_buffer *backup;
  727. int ret;
  728. if (likely(res->backup)) {
  729. BUG_ON(res->backup->base.num_pages * PAGE_SIZE < size);
  730. return 0;
  731. }
  732. backup = kzalloc(sizeof(*backup), GFP_KERNEL);
  733. if (unlikely(backup == NULL))
  734. return -ENOMEM;
  735. ret = vmw_dmabuf_init(res->dev_priv, backup, res->backup_size,
  736. res->func->backup_placement,
  737. interruptible,
  738. &vmw_dmabuf_bo_free);
  739. if (unlikely(ret != 0))
  740. goto out_no_dmabuf;
  741. res->backup = backup;
  742. out_no_dmabuf:
  743. return ret;
  744. }
  745. /**
  746. * vmw_resource_do_validate - Make a resource up-to-date and visible
  747. * to the device.
  748. *
  749. * @res: The resource to make visible to the device.
  750. * @val_buf: Information about a buffer possibly
  751. * containing backup data if a bind operation is needed.
  752. *
  753. * On hardware resource shortage, this function returns -EBUSY and
  754. * should be retried once resources have been freed up.
  755. */
  756. static int vmw_resource_do_validate(struct vmw_resource *res,
  757. struct ttm_validate_buffer *val_buf)
  758. {
  759. int ret = 0;
  760. const struct vmw_res_func *func = res->func;
  761. if (unlikely(res->id == -1)) {
  762. ret = func->create(res);
  763. if (unlikely(ret != 0))
  764. return ret;
  765. }
  766. if (func->bind &&
  767. ((func->needs_backup && list_empty(&res->mob_head) &&
  768. val_buf->bo != NULL) ||
  769. (!func->needs_backup && val_buf->bo != NULL))) {
  770. ret = func->bind(res, val_buf);
  771. if (unlikely(ret != 0))
  772. goto out_bind_failed;
  773. if (func->needs_backup)
  774. list_add_tail(&res->mob_head, &res->backup->res_list);
  775. }
  776. /*
  777. * Only do this on write operations, and move to
  778. * vmw_resource_unreserve if it can be called after
  779. * backup buffers have been unreserved. Otherwise
  780. * sort out locking.
  781. */
  782. res->res_dirty = true;
  783. return 0;
  784. out_bind_failed:
  785. func->destroy(res);
  786. return ret;
  787. }
  788. /**
  789. * vmw_resource_unreserve - Unreserve a resource previously reserved for
  790. * command submission.
  791. *
  792. * @res: Pointer to the struct vmw_resource to unreserve.
  793. * @new_backup: Pointer to new backup buffer if command submission
  794. * switched.
  795. * @new_backup_offset: New backup offset if @new_backup is !NULL.
  796. *
  797. * Currently unreserving a resource means putting it back on the device's
  798. * resource lru list, so that it can be evicted if necessary.
  799. */
  800. void vmw_resource_unreserve(struct vmw_resource *res,
  801. struct vmw_dma_buffer *new_backup,
  802. unsigned long new_backup_offset)
  803. {
  804. struct vmw_private *dev_priv = res->dev_priv;
  805. if (!list_empty(&res->lru_head))
  806. return;
  807. if (new_backup && new_backup != res->backup) {
  808. if (res->backup) {
  809. lockdep_assert_held(&res->backup->base.resv->lock.base);
  810. list_del_init(&res->mob_head);
  811. vmw_dmabuf_unreference(&res->backup);
  812. }
  813. res->backup = vmw_dmabuf_reference(new_backup);
  814. lockdep_assert_held(&new_backup->base.resv->lock.base);
  815. list_add_tail(&res->mob_head, &new_backup->res_list);
  816. }
  817. if (new_backup)
  818. res->backup_offset = new_backup_offset;
  819. if (!res->func->may_evict || res->id == -1)
  820. return;
  821. write_lock(&dev_priv->resource_lock);
  822. list_add_tail(&res->lru_head,
  823. &res->dev_priv->res_lru[res->func->res_type]);
  824. write_unlock(&dev_priv->resource_lock);
  825. }
  826. /**
  827. * vmw_resource_check_buffer - Check whether a backup buffer is needed
  828. * for a resource and in that case, allocate
  829. * one, reserve and validate it.
  830. *
  831. * @res: The resource for which to allocate a backup buffer.
  832. * @interruptible: Whether any sleeps during allocation should be
  833. * performed while interruptible.
  834. * @val_buf: On successful return contains data about the
  835. * reserved and validated backup buffer.
  836. */
  837. static int
  838. vmw_resource_check_buffer(struct vmw_resource *res,
  839. bool interruptible,
  840. struct ttm_validate_buffer *val_buf)
  841. {
  842. struct list_head val_list;
  843. bool backup_dirty = false;
  844. int ret;
  845. if (unlikely(res->backup == NULL)) {
  846. ret = vmw_resource_buf_alloc(res, interruptible);
  847. if (unlikely(ret != 0))
  848. return ret;
  849. }
  850. INIT_LIST_HEAD(&val_list);
  851. val_buf->bo = ttm_bo_reference(&res->backup->base);
  852. list_add_tail(&val_buf->head, &val_list);
  853. ret = ttm_eu_reserve_buffers(NULL, &val_list);
  854. if (unlikely(ret != 0))
  855. goto out_no_reserve;
  856. if (res->func->needs_backup && list_empty(&res->mob_head))
  857. return 0;
  858. backup_dirty = res->backup_dirty;
  859. ret = ttm_bo_validate(&res->backup->base,
  860. res->func->backup_placement,
  861. true, false);
  862. if (unlikely(ret != 0))
  863. goto out_no_validate;
  864. return 0;
  865. out_no_validate:
  866. ttm_eu_backoff_reservation(NULL, &val_list);
  867. out_no_reserve:
  868. ttm_bo_unref(&val_buf->bo);
  869. if (backup_dirty)
  870. vmw_dmabuf_unreference(&res->backup);
  871. return ret;
  872. }
  873. /**
  874. * vmw_resource_reserve - Reserve a resource for command submission
  875. *
  876. * @res: The resource to reserve.
  877. *
  878. * This function takes the resource off the LRU list and make sure
  879. * a backup buffer is present for guest-backed resources. However,
  880. * the buffer may not be bound to the resource at this point.
  881. *
  882. */
  883. int vmw_resource_reserve(struct vmw_resource *res, bool no_backup)
  884. {
  885. struct vmw_private *dev_priv = res->dev_priv;
  886. int ret;
  887. write_lock(&dev_priv->resource_lock);
  888. list_del_init(&res->lru_head);
  889. write_unlock(&dev_priv->resource_lock);
  890. if (res->func->needs_backup && res->backup == NULL &&
  891. !no_backup) {
  892. ret = vmw_resource_buf_alloc(res, true);
  893. if (unlikely(ret != 0))
  894. return ret;
  895. }
  896. return 0;
  897. }
  898. /**
  899. * vmw_resource_backoff_reservation - Unreserve and unreference a
  900. * backup buffer
  901. *.
  902. * @val_buf: Backup buffer information.
  903. */
  904. static void
  905. vmw_resource_backoff_reservation(struct ttm_validate_buffer *val_buf)
  906. {
  907. struct list_head val_list;
  908. if (likely(val_buf->bo == NULL))
  909. return;
  910. INIT_LIST_HEAD(&val_list);
  911. list_add_tail(&val_buf->head, &val_list);
  912. ttm_eu_backoff_reservation(NULL, &val_list);
  913. ttm_bo_unref(&val_buf->bo);
  914. }
  915. /**
  916. * vmw_resource_do_evict - Evict a resource, and transfer its data
  917. * to a backup buffer.
  918. *
  919. * @res: The resource to evict.
  920. * @interruptible: Whether to wait interruptible.
  921. */
  922. int vmw_resource_do_evict(struct vmw_resource *res, bool interruptible)
  923. {
  924. struct ttm_validate_buffer val_buf;
  925. const struct vmw_res_func *func = res->func;
  926. int ret;
  927. BUG_ON(!func->may_evict);
  928. val_buf.bo = NULL;
  929. ret = vmw_resource_check_buffer(res, interruptible, &val_buf);
  930. if (unlikely(ret != 0))
  931. return ret;
  932. if (unlikely(func->unbind != NULL &&
  933. (!func->needs_backup || !list_empty(&res->mob_head)))) {
  934. ret = func->unbind(res, res->res_dirty, &val_buf);
  935. if (unlikely(ret != 0))
  936. goto out_no_unbind;
  937. list_del_init(&res->mob_head);
  938. }
  939. ret = func->destroy(res);
  940. res->backup_dirty = true;
  941. res->res_dirty = false;
  942. out_no_unbind:
  943. vmw_resource_backoff_reservation(&val_buf);
  944. return ret;
  945. }
  946. /**
  947. * vmw_resource_validate - Make a resource up-to-date and visible
  948. * to the device.
  949. *
  950. * @res: The resource to make visible to the device.
  951. *
  952. * On succesful return, any backup DMA buffer pointed to by @res->backup will
  953. * be reserved and validated.
  954. * On hardware resource shortage, this function will repeatedly evict
  955. * resources of the same type until the validation succeeds.
  956. */
  957. int vmw_resource_validate(struct vmw_resource *res)
  958. {
  959. int ret;
  960. struct vmw_resource *evict_res;
  961. struct vmw_private *dev_priv = res->dev_priv;
  962. struct list_head *lru_list = &dev_priv->res_lru[res->func->res_type];
  963. struct ttm_validate_buffer val_buf;
  964. unsigned err_count = 0;
  965. if (likely(!res->func->may_evict))
  966. return 0;
  967. val_buf.bo = NULL;
  968. if (res->backup)
  969. val_buf.bo = &res->backup->base;
  970. do {
  971. ret = vmw_resource_do_validate(res, &val_buf);
  972. if (likely(ret != -EBUSY))
  973. break;
  974. write_lock(&dev_priv->resource_lock);
  975. if (list_empty(lru_list) || !res->func->may_evict) {
  976. DRM_ERROR("Out of device device resources "
  977. "for %s.\n", res->func->type_name);
  978. ret = -EBUSY;
  979. write_unlock(&dev_priv->resource_lock);
  980. break;
  981. }
  982. evict_res = vmw_resource_reference
  983. (list_first_entry(lru_list, struct vmw_resource,
  984. lru_head));
  985. list_del_init(&evict_res->lru_head);
  986. write_unlock(&dev_priv->resource_lock);
  987. ret = vmw_resource_do_evict(evict_res, true);
  988. if (unlikely(ret != 0)) {
  989. write_lock(&dev_priv->resource_lock);
  990. list_add_tail(&evict_res->lru_head, lru_list);
  991. write_unlock(&dev_priv->resource_lock);
  992. if (ret == -ERESTARTSYS ||
  993. ++err_count > VMW_RES_EVICT_ERR_COUNT) {
  994. vmw_resource_unreference(&evict_res);
  995. goto out_no_validate;
  996. }
  997. }
  998. vmw_resource_unreference(&evict_res);
  999. } while (1);
  1000. if (unlikely(ret != 0))
  1001. goto out_no_validate;
  1002. else if (!res->func->needs_backup && res->backup) {
  1003. list_del_init(&res->mob_head);
  1004. vmw_dmabuf_unreference(&res->backup);
  1005. }
  1006. return 0;
  1007. out_no_validate:
  1008. return ret;
  1009. }
  1010. /**
  1011. * vmw_fence_single_bo - Utility function to fence a single TTM buffer
  1012. * object without unreserving it.
  1013. *
  1014. * @bo: Pointer to the struct ttm_buffer_object to fence.
  1015. * @fence: Pointer to the fence. If NULL, this function will
  1016. * insert a fence into the command stream..
  1017. *
  1018. * Contrary to the ttm_eu version of this function, it takes only
  1019. * a single buffer object instead of a list, and it also doesn't
  1020. * unreserve the buffer object, which needs to be done separately.
  1021. */
  1022. void vmw_fence_single_bo(struct ttm_buffer_object *bo,
  1023. struct vmw_fence_obj *fence)
  1024. {
  1025. struct ttm_bo_device *bdev = bo->bdev;
  1026. struct ttm_bo_driver *driver = bdev->driver;
  1027. struct vmw_fence_obj *old_fence_obj;
  1028. struct vmw_private *dev_priv =
  1029. container_of(bdev, struct vmw_private, bdev);
  1030. if (fence == NULL)
  1031. vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
  1032. else
  1033. driver->sync_obj_ref(fence);
  1034. spin_lock(&bdev->fence_lock);
  1035. old_fence_obj = bo->sync_obj;
  1036. bo->sync_obj = fence;
  1037. spin_unlock(&bdev->fence_lock);
  1038. if (old_fence_obj)
  1039. vmw_fence_obj_unreference(&old_fence_obj);
  1040. }
  1041. /**
  1042. * vmw_resource_move_notify - TTM move_notify_callback
  1043. *
  1044. * @bo: The TTM buffer object about to move.
  1045. * @mem: The truct ttm_mem_reg indicating to what memory
  1046. * region the move is taking place.
  1047. *
  1048. * For now does nothing.
  1049. */
  1050. void vmw_resource_move_notify(struct ttm_buffer_object *bo,
  1051. struct ttm_mem_reg *mem)
  1052. {
  1053. }
  1054. /**
  1055. * vmw_resource_needs_backup - Return whether a resource needs a backup buffer.
  1056. *
  1057. * @res: The resource being queried.
  1058. */
  1059. bool vmw_resource_needs_backup(const struct vmw_resource *res)
  1060. {
  1061. return res->func->needs_backup;
  1062. }
  1063. /**
  1064. * vmw_resource_evict_type - Evict all resources of a specific type
  1065. *
  1066. * @dev_priv: Pointer to a device private struct
  1067. * @type: The resource type to evict
  1068. *
  1069. * To avoid thrashing starvation or as part of the hibernation sequence,
  1070. * try to evict all evictable resources of a specific type.
  1071. */
  1072. static void vmw_resource_evict_type(struct vmw_private *dev_priv,
  1073. enum vmw_res_type type)
  1074. {
  1075. struct list_head *lru_list = &dev_priv->res_lru[type];
  1076. struct vmw_resource *evict_res;
  1077. unsigned err_count = 0;
  1078. int ret;
  1079. do {
  1080. write_lock(&dev_priv->resource_lock);
  1081. if (list_empty(lru_list))
  1082. goto out_unlock;
  1083. evict_res = vmw_resource_reference(
  1084. list_first_entry(lru_list, struct vmw_resource,
  1085. lru_head));
  1086. list_del_init(&evict_res->lru_head);
  1087. write_unlock(&dev_priv->resource_lock);
  1088. ret = vmw_resource_do_evict(evict_res, false);
  1089. if (unlikely(ret != 0)) {
  1090. write_lock(&dev_priv->resource_lock);
  1091. list_add_tail(&evict_res->lru_head, lru_list);
  1092. write_unlock(&dev_priv->resource_lock);
  1093. if (++err_count > VMW_RES_EVICT_ERR_COUNT) {
  1094. vmw_resource_unreference(&evict_res);
  1095. return;
  1096. }
  1097. }
  1098. vmw_resource_unreference(&evict_res);
  1099. } while (1);
  1100. out_unlock:
  1101. write_unlock(&dev_priv->resource_lock);
  1102. }
  1103. /**
  1104. * vmw_resource_evict_all - Evict all evictable resources
  1105. *
  1106. * @dev_priv: Pointer to a device private struct
  1107. *
  1108. * To avoid thrashing starvation or as part of the hibernation sequence,
  1109. * evict all evictable resources. In particular this means that all
  1110. * guest-backed resources that are registered with the device are
  1111. * evicted and the OTable becomes clean.
  1112. */
  1113. void vmw_resource_evict_all(struct vmw_private *dev_priv)
  1114. {
  1115. enum vmw_res_type type;
  1116. mutex_lock(&dev_priv->cmdbuf_mutex);
  1117. for (type = 0; type < vmw_res_max; ++type)
  1118. vmw_resource_evict_type(dev_priv, type);
  1119. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1120. }