vmwgfx_resource.c 36 KB

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