vmwgfx_resource.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  1. /**************************************************************************
  2. *
  3. * Copyright © 2009-2015 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. #include "vmwgfx_binding.h"
  34. #define VMW_RES_EVICT_ERR_COUNT 10
  35. struct vmw_user_dma_buffer {
  36. struct ttm_prime_object prime;
  37. struct vmw_dma_buffer dma;
  38. };
  39. struct vmw_bo_user_rep {
  40. uint32_t handle;
  41. uint64_t map_handle;
  42. };
  43. static inline struct vmw_dma_buffer *
  44. vmw_dma_buffer(struct ttm_buffer_object *bo)
  45. {
  46. return container_of(bo, struct vmw_dma_buffer, base);
  47. }
  48. static inline struct vmw_user_dma_buffer *
  49. vmw_user_dma_buffer(struct ttm_buffer_object *bo)
  50. {
  51. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  52. return container_of(vmw_bo, struct vmw_user_dma_buffer, dma);
  53. }
  54. struct vmw_resource *vmw_resource_reference(struct vmw_resource *res)
  55. {
  56. kref_get(&res->kref);
  57. return res;
  58. }
  59. struct vmw_resource *
  60. vmw_resource_reference_unless_doomed(struct vmw_resource *res)
  61. {
  62. return kref_get_unless_zero(&res->kref) ? res : NULL;
  63. }
  64. /**
  65. * vmw_resource_release_id - release a resource id to the id manager.
  66. *
  67. * @res: Pointer to the resource.
  68. *
  69. * Release the resource id to the resource id manager and set it to -1
  70. */
  71. void vmw_resource_release_id(struct vmw_resource *res)
  72. {
  73. struct vmw_private *dev_priv = res->dev_priv;
  74. struct idr *idr = &dev_priv->res_idr[res->func->res_type];
  75. write_lock(&dev_priv->resource_lock);
  76. if (res->id != -1)
  77. idr_remove(idr, res->id);
  78. res->id = -1;
  79. write_unlock(&dev_priv->resource_lock);
  80. }
  81. static void vmw_resource_release(struct kref *kref)
  82. {
  83. struct vmw_resource *res =
  84. container_of(kref, struct vmw_resource, kref);
  85. struct vmw_private *dev_priv = res->dev_priv;
  86. int id;
  87. struct idr *idr = &dev_priv->res_idr[res->func->res_type];
  88. write_lock(&dev_priv->resource_lock);
  89. res->avail = false;
  90. list_del_init(&res->lru_head);
  91. write_unlock(&dev_priv->resource_lock);
  92. if (res->backup) {
  93. struct ttm_buffer_object *bo = &res->backup->base;
  94. ttm_bo_reserve(bo, false, false, NULL);
  95. if (!list_empty(&res->mob_head) &&
  96. res->func->unbind != NULL) {
  97. struct ttm_validate_buffer val_buf;
  98. val_buf.bo = bo;
  99. val_buf.shared = false;
  100. res->func->unbind(res, false, &val_buf);
  101. }
  102. res->backup_dirty = false;
  103. list_del_init(&res->mob_head);
  104. ttm_bo_unreserve(bo);
  105. vmw_dmabuf_unreference(&res->backup);
  106. }
  107. if (likely(res->hw_destroy != NULL)) {
  108. mutex_lock(&dev_priv->binding_mutex);
  109. vmw_binding_res_list_kill(&res->binding_head);
  110. mutex_unlock(&dev_priv->binding_mutex);
  111. res->hw_destroy(res);
  112. }
  113. id = res->id;
  114. if (res->res_free != NULL)
  115. res->res_free(res);
  116. else
  117. kfree(res);
  118. write_lock(&dev_priv->resource_lock);
  119. if (id != -1)
  120. idr_remove(idr, id);
  121. write_unlock(&dev_priv->resource_lock);
  122. }
  123. void vmw_resource_unreference(struct vmw_resource **p_res)
  124. {
  125. struct vmw_resource *res = *p_res;
  126. *p_res = NULL;
  127. kref_put(&res->kref, vmw_resource_release);
  128. }
  129. /**
  130. * vmw_resource_alloc_id - release a resource id to the id manager.
  131. *
  132. * @res: Pointer to the resource.
  133. *
  134. * Allocate the lowest free resource from the resource manager, and set
  135. * @res->id to that id. Returns 0 on success and -ENOMEM on failure.
  136. */
  137. int vmw_resource_alloc_id(struct vmw_resource *res)
  138. {
  139. struct vmw_private *dev_priv = res->dev_priv;
  140. int ret;
  141. struct idr *idr = &dev_priv->res_idr[res->func->res_type];
  142. BUG_ON(res->id != -1);
  143. idr_preload(GFP_KERNEL);
  144. write_lock(&dev_priv->resource_lock);
  145. ret = idr_alloc(idr, res, 1, 0, GFP_NOWAIT);
  146. if (ret >= 0)
  147. res->id = ret;
  148. write_unlock(&dev_priv->resource_lock);
  149. idr_preload_end();
  150. return ret < 0 ? ret : 0;
  151. }
  152. /**
  153. * vmw_resource_init - initialize a struct vmw_resource
  154. *
  155. * @dev_priv: Pointer to a device private struct.
  156. * @res: The struct vmw_resource to initialize.
  157. * @obj_type: Resource object type.
  158. * @delay_id: Boolean whether to defer device id allocation until
  159. * the first validation.
  160. * @res_free: Resource destructor.
  161. * @func: Resource function table.
  162. */
  163. int vmw_resource_init(struct vmw_private *dev_priv, struct vmw_resource *res,
  164. bool delay_id,
  165. void (*res_free) (struct vmw_resource *res),
  166. const struct vmw_res_func *func)
  167. {
  168. kref_init(&res->kref);
  169. res->hw_destroy = NULL;
  170. res->res_free = res_free;
  171. res->avail = false;
  172. res->dev_priv = dev_priv;
  173. res->func = func;
  174. INIT_LIST_HEAD(&res->lru_head);
  175. INIT_LIST_HEAD(&res->mob_head);
  176. INIT_LIST_HEAD(&res->binding_head);
  177. res->id = -1;
  178. res->backup = NULL;
  179. res->backup_offset = 0;
  180. res->backup_dirty = false;
  181. res->res_dirty = false;
  182. if (delay_id)
  183. return 0;
  184. else
  185. return vmw_resource_alloc_id(res);
  186. }
  187. /**
  188. * vmw_resource_activate
  189. *
  190. * @res: Pointer to the newly created resource
  191. * @hw_destroy: Destroy function. NULL if none.
  192. *
  193. * Activate a resource after the hardware has been made aware of it.
  194. * Set tye destroy function to @destroy. Typically this frees the
  195. * resource and destroys the hardware resources associated with it.
  196. * Activate basically means that the function vmw_resource_lookup will
  197. * find it.
  198. */
  199. void vmw_resource_activate(struct vmw_resource *res,
  200. void (*hw_destroy) (struct vmw_resource *))
  201. {
  202. struct vmw_private *dev_priv = res->dev_priv;
  203. write_lock(&dev_priv->resource_lock);
  204. res->avail = true;
  205. res->hw_destroy = hw_destroy;
  206. write_unlock(&dev_priv->resource_lock);
  207. }
  208. /**
  209. * vmw_user_resource_lookup_handle - lookup a struct resource from a
  210. * TTM user-space handle and perform basic type checks
  211. *
  212. * @dev_priv: Pointer to a device private struct
  213. * @tfile: Pointer to a struct ttm_object_file identifying the caller
  214. * @handle: The TTM user-space handle
  215. * @converter: Pointer to an object describing the resource type
  216. * @p_res: On successful return the location pointed to will contain
  217. * a pointer to a refcounted struct vmw_resource.
  218. *
  219. * If the handle can't be found or is associated with an incorrect resource
  220. * type, -EINVAL will be returned.
  221. */
  222. int vmw_user_resource_lookup_handle(struct vmw_private *dev_priv,
  223. struct ttm_object_file *tfile,
  224. uint32_t handle,
  225. const struct vmw_user_resource_conv
  226. *converter,
  227. struct vmw_resource **p_res)
  228. {
  229. struct ttm_base_object *base;
  230. struct vmw_resource *res;
  231. int ret = -EINVAL;
  232. base = ttm_base_object_lookup(tfile, handle);
  233. if (unlikely(base == NULL))
  234. return -EINVAL;
  235. if (unlikely(ttm_base_object_type(base) != converter->object_type))
  236. goto out_bad_resource;
  237. res = converter->base_obj_to_res(base);
  238. read_lock(&dev_priv->resource_lock);
  239. if (!res->avail || res->res_free != converter->res_free) {
  240. read_unlock(&dev_priv->resource_lock);
  241. goto out_bad_resource;
  242. }
  243. kref_get(&res->kref);
  244. read_unlock(&dev_priv->resource_lock);
  245. *p_res = res;
  246. ret = 0;
  247. out_bad_resource:
  248. ttm_base_object_unref(&base);
  249. return ret;
  250. }
  251. /**
  252. * Helper function that looks either a surface or dmabuf.
  253. *
  254. * The pointer this pointed at by out_surf and out_buf needs to be null.
  255. */
  256. int vmw_user_lookup_handle(struct vmw_private *dev_priv,
  257. struct ttm_object_file *tfile,
  258. uint32_t handle,
  259. struct vmw_surface **out_surf,
  260. struct vmw_dma_buffer **out_buf)
  261. {
  262. struct vmw_resource *res;
  263. int ret;
  264. BUG_ON(*out_surf || *out_buf);
  265. ret = vmw_user_resource_lookup_handle(dev_priv, tfile, handle,
  266. user_surface_converter,
  267. &res);
  268. if (!ret) {
  269. *out_surf = vmw_res_to_srf(res);
  270. return 0;
  271. }
  272. *out_surf = NULL;
  273. ret = vmw_user_dmabuf_lookup(tfile, handle, out_buf, NULL);
  274. return ret;
  275. }
  276. /**
  277. * Buffer management.
  278. */
  279. /**
  280. * vmw_dmabuf_acc_size - Calculate the pinned memory usage of buffers
  281. *
  282. * @dev_priv: Pointer to a struct vmw_private identifying the device.
  283. * @size: The requested buffer size.
  284. * @user: Whether this is an ordinary dma buffer or a user dma buffer.
  285. */
  286. static size_t vmw_dmabuf_acc_size(struct vmw_private *dev_priv, size_t size,
  287. bool user)
  288. {
  289. static size_t struct_size, user_struct_size;
  290. size_t num_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
  291. size_t page_array_size = ttm_round_pot(num_pages * sizeof(void *));
  292. if (unlikely(struct_size == 0)) {
  293. size_t backend_size = ttm_round_pot(vmw_tt_size);
  294. struct_size = backend_size +
  295. ttm_round_pot(sizeof(struct vmw_dma_buffer));
  296. user_struct_size = backend_size +
  297. ttm_round_pot(sizeof(struct vmw_user_dma_buffer));
  298. }
  299. if (dev_priv->map_mode == vmw_dma_alloc_coherent)
  300. page_array_size +=
  301. ttm_round_pot(num_pages * sizeof(dma_addr_t));
  302. return ((user) ? user_struct_size : struct_size) +
  303. page_array_size;
  304. }
  305. void vmw_dmabuf_bo_free(struct ttm_buffer_object *bo)
  306. {
  307. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  308. kfree(vmw_bo);
  309. }
  310. static void vmw_user_dmabuf_destroy(struct ttm_buffer_object *bo)
  311. {
  312. struct vmw_user_dma_buffer *vmw_user_bo = vmw_user_dma_buffer(bo);
  313. ttm_prime_object_kfree(vmw_user_bo, prime);
  314. }
  315. int vmw_dmabuf_init(struct vmw_private *dev_priv,
  316. struct vmw_dma_buffer *vmw_bo,
  317. size_t size, struct ttm_placement *placement,
  318. bool interruptible,
  319. void (*bo_free) (struct ttm_buffer_object *bo))
  320. {
  321. struct ttm_bo_device *bdev = &dev_priv->bdev;
  322. size_t acc_size;
  323. int ret;
  324. bool user = (bo_free == &vmw_user_dmabuf_destroy);
  325. BUG_ON(!bo_free && (!user && (bo_free != vmw_dmabuf_bo_free)));
  326. acc_size = vmw_dmabuf_acc_size(dev_priv, size, user);
  327. memset(vmw_bo, 0, sizeof(*vmw_bo));
  328. INIT_LIST_HEAD(&vmw_bo->res_list);
  329. ret = ttm_bo_init(bdev, &vmw_bo->base, size,
  330. ttm_bo_type_device, placement,
  331. 0, interruptible,
  332. NULL, acc_size, NULL, NULL, bo_free);
  333. return ret;
  334. }
  335. static void vmw_user_dmabuf_release(struct ttm_base_object **p_base)
  336. {
  337. struct vmw_user_dma_buffer *vmw_user_bo;
  338. struct ttm_base_object *base = *p_base;
  339. struct ttm_buffer_object *bo;
  340. *p_base = NULL;
  341. if (unlikely(base == NULL))
  342. return;
  343. vmw_user_bo = container_of(base, struct vmw_user_dma_buffer,
  344. prime.base);
  345. bo = &vmw_user_bo->dma.base;
  346. ttm_bo_unref(&bo);
  347. }
  348. static void vmw_user_dmabuf_ref_obj_release(struct ttm_base_object *base,
  349. enum ttm_ref_type ref_type)
  350. {
  351. struct vmw_user_dma_buffer *user_bo;
  352. user_bo = container_of(base, struct vmw_user_dma_buffer, prime.base);
  353. switch (ref_type) {
  354. case TTM_REF_SYNCCPU_WRITE:
  355. ttm_bo_synccpu_write_release(&user_bo->dma.base);
  356. break;
  357. default:
  358. BUG();
  359. }
  360. }
  361. /**
  362. * vmw_user_dmabuf_alloc - Allocate a user dma buffer
  363. *
  364. * @dev_priv: Pointer to a struct device private.
  365. * @tfile: Pointer to a struct ttm_object_file on which to register the user
  366. * object.
  367. * @size: Size of the dma buffer.
  368. * @shareable: Boolean whether the buffer is shareable with other open files.
  369. * @handle: Pointer to where the handle value should be assigned.
  370. * @p_dma_buf: Pointer to where the refcounted struct vmw_dma_buffer pointer
  371. * should be assigned.
  372. */
  373. int vmw_user_dmabuf_alloc(struct vmw_private *dev_priv,
  374. struct ttm_object_file *tfile,
  375. uint32_t size,
  376. bool shareable,
  377. uint32_t *handle,
  378. struct vmw_dma_buffer **p_dma_buf,
  379. struct ttm_base_object **p_base)
  380. {
  381. struct vmw_user_dma_buffer *user_bo;
  382. struct ttm_buffer_object *tmp;
  383. int ret;
  384. user_bo = kzalloc(sizeof(*user_bo), GFP_KERNEL);
  385. if (unlikely(!user_bo)) {
  386. DRM_ERROR("Failed to allocate a buffer.\n");
  387. return -ENOMEM;
  388. }
  389. ret = vmw_dmabuf_init(dev_priv, &user_bo->dma, size,
  390. (dev_priv->has_mob) ?
  391. &vmw_sys_placement :
  392. &vmw_vram_sys_placement, true,
  393. &vmw_user_dmabuf_destroy);
  394. if (unlikely(ret != 0))
  395. return ret;
  396. tmp = ttm_bo_reference(&user_bo->dma.base);
  397. ret = ttm_prime_object_init(tfile,
  398. size,
  399. &user_bo->prime,
  400. shareable,
  401. ttm_buffer_type,
  402. &vmw_user_dmabuf_release,
  403. &vmw_user_dmabuf_ref_obj_release);
  404. if (unlikely(ret != 0)) {
  405. ttm_bo_unref(&tmp);
  406. goto out_no_base_object;
  407. }
  408. *p_dma_buf = &user_bo->dma;
  409. if (p_base) {
  410. *p_base = &user_bo->prime.base;
  411. kref_get(&(*p_base)->refcount);
  412. }
  413. *handle = user_bo->prime.base.hash.key;
  414. out_no_base_object:
  415. return ret;
  416. }
  417. /**
  418. * vmw_user_dmabuf_verify_access - verify access permissions on this
  419. * buffer object.
  420. *
  421. * @bo: Pointer to the buffer object being accessed
  422. * @tfile: Identifying the caller.
  423. */
  424. int vmw_user_dmabuf_verify_access(struct ttm_buffer_object *bo,
  425. struct ttm_object_file *tfile)
  426. {
  427. struct vmw_user_dma_buffer *vmw_user_bo;
  428. if (unlikely(bo->destroy != vmw_user_dmabuf_destroy))
  429. return -EPERM;
  430. vmw_user_bo = vmw_user_dma_buffer(bo);
  431. /* Check that the caller has opened the object. */
  432. if (likely(ttm_ref_object_exists(tfile, &vmw_user_bo->prime.base)))
  433. return 0;
  434. DRM_ERROR("Could not grant buffer access.\n");
  435. return -EPERM;
  436. }
  437. /**
  438. * vmw_user_dmabuf_synccpu_grab - Grab a struct vmw_user_dma_buffer for cpu
  439. * access, idling previous GPU operations on the buffer and optionally
  440. * blocking it for further command submissions.
  441. *
  442. * @user_bo: Pointer to the buffer object being grabbed for CPU access
  443. * @tfile: Identifying the caller.
  444. * @flags: Flags indicating how the grab should be performed.
  445. *
  446. * A blocking grab will be automatically released when @tfile is closed.
  447. */
  448. static int vmw_user_dmabuf_synccpu_grab(struct vmw_user_dma_buffer *user_bo,
  449. struct ttm_object_file *tfile,
  450. uint32_t flags)
  451. {
  452. struct ttm_buffer_object *bo = &user_bo->dma.base;
  453. bool existed;
  454. int ret;
  455. if (flags & drm_vmw_synccpu_allow_cs) {
  456. bool nonblock = !!(flags & drm_vmw_synccpu_dontblock);
  457. long lret;
  458. lret = reservation_object_wait_timeout_rcu(bo->resv, true, true,
  459. nonblock ? 0 : MAX_SCHEDULE_TIMEOUT);
  460. if (!lret)
  461. return -EBUSY;
  462. else if (lret < 0)
  463. return lret;
  464. return 0;
  465. }
  466. ret = ttm_bo_synccpu_write_grab
  467. (bo, !!(flags & drm_vmw_synccpu_dontblock));
  468. if (unlikely(ret != 0))
  469. return ret;
  470. ret = ttm_ref_object_add(tfile, &user_bo->prime.base,
  471. TTM_REF_SYNCCPU_WRITE, &existed, false);
  472. if (ret != 0 || existed)
  473. ttm_bo_synccpu_write_release(&user_bo->dma.base);
  474. return ret;
  475. }
  476. /**
  477. * vmw_user_dmabuf_synccpu_release - Release a previous grab for CPU access,
  478. * and unblock command submission on the buffer if blocked.
  479. *
  480. * @handle: Handle identifying the buffer object.
  481. * @tfile: Identifying the caller.
  482. * @flags: Flags indicating the type of release.
  483. */
  484. static int vmw_user_dmabuf_synccpu_release(uint32_t handle,
  485. struct ttm_object_file *tfile,
  486. uint32_t flags)
  487. {
  488. if (!(flags & drm_vmw_synccpu_allow_cs))
  489. return ttm_ref_object_base_unref(tfile, handle,
  490. TTM_REF_SYNCCPU_WRITE);
  491. return 0;
  492. }
  493. /**
  494. * vmw_user_dmabuf_synccpu_release - ioctl function implementing the synccpu
  495. * functionality.
  496. *
  497. * @dev: Identifies the drm device.
  498. * @data: Pointer to the ioctl argument.
  499. * @file_priv: Identifies the caller.
  500. *
  501. * This function checks the ioctl arguments for validity and calls the
  502. * relevant synccpu functions.
  503. */
  504. int vmw_user_dmabuf_synccpu_ioctl(struct drm_device *dev, void *data,
  505. struct drm_file *file_priv)
  506. {
  507. struct drm_vmw_synccpu_arg *arg =
  508. (struct drm_vmw_synccpu_arg *) data;
  509. struct vmw_dma_buffer *dma_buf;
  510. struct vmw_user_dma_buffer *user_bo;
  511. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  512. struct ttm_base_object *buffer_base;
  513. int ret;
  514. if ((arg->flags & (drm_vmw_synccpu_read | drm_vmw_synccpu_write)) == 0
  515. || (arg->flags & ~(drm_vmw_synccpu_read | drm_vmw_synccpu_write |
  516. drm_vmw_synccpu_dontblock |
  517. drm_vmw_synccpu_allow_cs)) != 0) {
  518. DRM_ERROR("Illegal synccpu flags.\n");
  519. return -EINVAL;
  520. }
  521. switch (arg->op) {
  522. case drm_vmw_synccpu_grab:
  523. ret = vmw_user_dmabuf_lookup(tfile, arg->handle, &dma_buf,
  524. &buffer_base);
  525. if (unlikely(ret != 0))
  526. return ret;
  527. user_bo = container_of(dma_buf, struct vmw_user_dma_buffer,
  528. dma);
  529. ret = vmw_user_dmabuf_synccpu_grab(user_bo, tfile, arg->flags);
  530. vmw_dmabuf_unreference(&dma_buf);
  531. ttm_base_object_unref(&buffer_base);
  532. if (unlikely(ret != 0 && ret != -ERESTARTSYS &&
  533. ret != -EBUSY)) {
  534. DRM_ERROR("Failed synccpu grab on handle 0x%08x.\n",
  535. (unsigned int) arg->handle);
  536. return ret;
  537. }
  538. break;
  539. case drm_vmw_synccpu_release:
  540. ret = vmw_user_dmabuf_synccpu_release(arg->handle, tfile,
  541. arg->flags);
  542. if (unlikely(ret != 0)) {
  543. DRM_ERROR("Failed synccpu release on handle 0x%08x.\n",
  544. (unsigned int) arg->handle);
  545. return ret;
  546. }
  547. break;
  548. default:
  549. DRM_ERROR("Invalid synccpu operation.\n");
  550. return -EINVAL;
  551. }
  552. return 0;
  553. }
  554. int vmw_dmabuf_alloc_ioctl(struct drm_device *dev, void *data,
  555. struct drm_file *file_priv)
  556. {
  557. struct vmw_private *dev_priv = vmw_priv(dev);
  558. union drm_vmw_alloc_dmabuf_arg *arg =
  559. (union drm_vmw_alloc_dmabuf_arg *)data;
  560. struct drm_vmw_alloc_dmabuf_req *req = &arg->req;
  561. struct drm_vmw_dmabuf_rep *rep = &arg->rep;
  562. struct vmw_dma_buffer *dma_buf;
  563. uint32_t handle;
  564. int ret;
  565. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  566. if (unlikely(ret != 0))
  567. return ret;
  568. ret = vmw_user_dmabuf_alloc(dev_priv, vmw_fpriv(file_priv)->tfile,
  569. req->size, false, &handle, &dma_buf,
  570. NULL);
  571. if (unlikely(ret != 0))
  572. goto out_no_dmabuf;
  573. rep->handle = handle;
  574. rep->map_handle = drm_vma_node_offset_addr(&dma_buf->base.vma_node);
  575. rep->cur_gmr_id = handle;
  576. rep->cur_gmr_offset = 0;
  577. vmw_dmabuf_unreference(&dma_buf);
  578. out_no_dmabuf:
  579. ttm_read_unlock(&dev_priv->reservation_sem);
  580. return ret;
  581. }
  582. int vmw_dmabuf_unref_ioctl(struct drm_device *dev, void *data,
  583. struct drm_file *file_priv)
  584. {
  585. struct drm_vmw_unref_dmabuf_arg *arg =
  586. (struct drm_vmw_unref_dmabuf_arg *)data;
  587. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  588. arg->handle,
  589. TTM_REF_USAGE);
  590. }
  591. int vmw_user_dmabuf_lookup(struct ttm_object_file *tfile,
  592. uint32_t handle, struct vmw_dma_buffer **out,
  593. struct ttm_base_object **p_base)
  594. {
  595. struct vmw_user_dma_buffer *vmw_user_bo;
  596. struct ttm_base_object *base;
  597. base = ttm_base_object_lookup(tfile, handle);
  598. if (unlikely(base == NULL)) {
  599. pr_err("Invalid buffer object handle 0x%08lx\n",
  600. (unsigned long)handle);
  601. return -ESRCH;
  602. }
  603. if (unlikely(ttm_base_object_type(base) != ttm_buffer_type)) {
  604. ttm_base_object_unref(&base);
  605. pr_err("Invalid buffer object handle 0x%08lx\n",
  606. (unsigned long)handle);
  607. return -EINVAL;
  608. }
  609. vmw_user_bo = container_of(base, struct vmw_user_dma_buffer,
  610. prime.base);
  611. (void)ttm_bo_reference(&vmw_user_bo->dma.base);
  612. if (p_base)
  613. *p_base = base;
  614. else
  615. ttm_base_object_unref(&base);
  616. *out = &vmw_user_bo->dma;
  617. return 0;
  618. }
  619. int vmw_user_dmabuf_reference(struct ttm_object_file *tfile,
  620. struct vmw_dma_buffer *dma_buf,
  621. uint32_t *handle)
  622. {
  623. struct vmw_user_dma_buffer *user_bo;
  624. if (dma_buf->base.destroy != vmw_user_dmabuf_destroy)
  625. return -EINVAL;
  626. user_bo = container_of(dma_buf, struct vmw_user_dma_buffer, dma);
  627. *handle = user_bo->prime.base.hash.key;
  628. return ttm_ref_object_add(tfile, &user_bo->prime.base,
  629. TTM_REF_USAGE, NULL, false);
  630. }
  631. /**
  632. * vmw_dumb_create - Create a dumb kms buffer
  633. *
  634. * @file_priv: Pointer to a struct drm_file identifying the caller.
  635. * @dev: Pointer to the drm device.
  636. * @args: Pointer to a struct drm_mode_create_dumb structure
  637. *
  638. * This is a driver callback for the core drm create_dumb functionality.
  639. * Note that this is very similar to the vmw_dmabuf_alloc ioctl, except
  640. * that the arguments have a different format.
  641. */
  642. int vmw_dumb_create(struct drm_file *file_priv,
  643. struct drm_device *dev,
  644. struct drm_mode_create_dumb *args)
  645. {
  646. struct vmw_private *dev_priv = vmw_priv(dev);
  647. struct vmw_dma_buffer *dma_buf;
  648. int ret;
  649. args->pitch = args->width * ((args->bpp + 7) / 8);
  650. args->size = args->pitch * args->height;
  651. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  652. if (unlikely(ret != 0))
  653. return ret;
  654. ret = vmw_user_dmabuf_alloc(dev_priv, vmw_fpriv(file_priv)->tfile,
  655. args->size, false, &args->handle,
  656. &dma_buf, NULL);
  657. if (unlikely(ret != 0))
  658. goto out_no_dmabuf;
  659. vmw_dmabuf_unreference(&dma_buf);
  660. out_no_dmabuf:
  661. ttm_read_unlock(&dev_priv->reservation_sem);
  662. return ret;
  663. }
  664. /**
  665. * vmw_dumb_map_offset - Return the address space offset of a dumb buffer
  666. *
  667. * @file_priv: Pointer to a struct drm_file identifying the caller.
  668. * @dev: Pointer to the drm device.
  669. * @handle: Handle identifying the dumb buffer.
  670. * @offset: The address space offset returned.
  671. *
  672. * This is a driver callback for the core drm dumb_map_offset functionality.
  673. */
  674. int vmw_dumb_map_offset(struct drm_file *file_priv,
  675. struct drm_device *dev, uint32_t handle,
  676. uint64_t *offset)
  677. {
  678. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  679. struct vmw_dma_buffer *out_buf;
  680. int ret;
  681. ret = vmw_user_dmabuf_lookup(tfile, handle, &out_buf, NULL);
  682. if (ret != 0)
  683. return -EINVAL;
  684. *offset = drm_vma_node_offset_addr(&out_buf->base.vma_node);
  685. vmw_dmabuf_unreference(&out_buf);
  686. return 0;
  687. }
  688. /**
  689. * vmw_dumb_destroy - Destroy a dumb boffer
  690. *
  691. * @file_priv: Pointer to a struct drm_file identifying the caller.
  692. * @dev: Pointer to the drm device.
  693. * @handle: Handle identifying the dumb buffer.
  694. *
  695. * This is a driver callback for the core drm dumb_destroy functionality.
  696. */
  697. int vmw_dumb_destroy(struct drm_file *file_priv,
  698. struct drm_device *dev,
  699. uint32_t handle)
  700. {
  701. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  702. handle, TTM_REF_USAGE);
  703. }
  704. /**
  705. * vmw_resource_buf_alloc - Allocate a backup buffer for a resource.
  706. *
  707. * @res: The resource for which to allocate a backup buffer.
  708. * @interruptible: Whether any sleeps during allocation should be
  709. * performed while interruptible.
  710. */
  711. static int vmw_resource_buf_alloc(struct vmw_resource *res,
  712. bool interruptible)
  713. {
  714. unsigned long size =
  715. (res->backup_size + PAGE_SIZE - 1) & PAGE_MASK;
  716. struct vmw_dma_buffer *backup;
  717. int ret;
  718. if (likely(res->backup)) {
  719. BUG_ON(res->backup->base.num_pages * PAGE_SIZE < size);
  720. return 0;
  721. }
  722. backup = kzalloc(sizeof(*backup), GFP_KERNEL);
  723. if (unlikely(!backup))
  724. return -ENOMEM;
  725. ret = vmw_dmabuf_init(res->dev_priv, backup, res->backup_size,
  726. res->func->backup_placement,
  727. interruptible,
  728. &vmw_dmabuf_bo_free);
  729. if (unlikely(ret != 0))
  730. goto out_no_dmabuf;
  731. res->backup = backup;
  732. out_no_dmabuf:
  733. return ret;
  734. }
  735. /**
  736. * vmw_resource_do_validate - Make a resource up-to-date and visible
  737. * to the device.
  738. *
  739. * @res: The resource to make visible to the device.
  740. * @val_buf: Information about a buffer possibly
  741. * containing backup data if a bind operation is needed.
  742. *
  743. * On hardware resource shortage, this function returns -EBUSY and
  744. * should be retried once resources have been freed up.
  745. */
  746. static int vmw_resource_do_validate(struct vmw_resource *res,
  747. struct ttm_validate_buffer *val_buf)
  748. {
  749. int ret = 0;
  750. const struct vmw_res_func *func = res->func;
  751. if (unlikely(res->id == -1)) {
  752. ret = func->create(res);
  753. if (unlikely(ret != 0))
  754. return ret;
  755. }
  756. if (func->bind &&
  757. ((func->needs_backup && list_empty(&res->mob_head) &&
  758. val_buf->bo != NULL) ||
  759. (!func->needs_backup && val_buf->bo != NULL))) {
  760. ret = func->bind(res, val_buf);
  761. if (unlikely(ret != 0))
  762. goto out_bind_failed;
  763. if (func->needs_backup)
  764. list_add_tail(&res->mob_head, &res->backup->res_list);
  765. }
  766. /*
  767. * Only do this on write operations, and move to
  768. * vmw_resource_unreserve if it can be called after
  769. * backup buffers have been unreserved. Otherwise
  770. * sort out locking.
  771. */
  772. res->res_dirty = true;
  773. return 0;
  774. out_bind_failed:
  775. func->destroy(res);
  776. return ret;
  777. }
  778. /**
  779. * vmw_resource_unreserve - Unreserve a resource previously reserved for
  780. * command submission.
  781. *
  782. * @res: Pointer to the struct vmw_resource to unreserve.
  783. * @switch_backup: Backup buffer has been switched.
  784. * @new_backup: Pointer to new backup buffer if command submission
  785. * switched. May be NULL.
  786. * @new_backup_offset: New backup offset if @switch_backup is true.
  787. *
  788. * Currently unreserving a resource means putting it back on the device's
  789. * resource lru list, so that it can be evicted if necessary.
  790. */
  791. void vmw_resource_unreserve(struct vmw_resource *res,
  792. bool switch_backup,
  793. struct vmw_dma_buffer *new_backup,
  794. unsigned long new_backup_offset)
  795. {
  796. struct vmw_private *dev_priv = res->dev_priv;
  797. if (!list_empty(&res->lru_head))
  798. return;
  799. if (switch_backup && new_backup != res->backup) {
  800. if (res->backup) {
  801. lockdep_assert_held(&res->backup->base.resv->lock.base);
  802. list_del_init(&res->mob_head);
  803. vmw_dmabuf_unreference(&res->backup);
  804. }
  805. if (new_backup) {
  806. res->backup = vmw_dmabuf_reference(new_backup);
  807. lockdep_assert_held(&new_backup->base.resv->lock.base);
  808. list_add_tail(&res->mob_head, &new_backup->res_list);
  809. } else {
  810. res->backup = NULL;
  811. }
  812. }
  813. if (switch_backup)
  814. res->backup_offset = new_backup_offset;
  815. if (!res->func->may_evict || res->id == -1 || res->pin_count)
  816. return;
  817. write_lock(&dev_priv->resource_lock);
  818. list_add_tail(&res->lru_head,
  819. &res->dev_priv->res_lru[res->func->res_type]);
  820. write_unlock(&dev_priv->resource_lock);
  821. }
  822. /**
  823. * vmw_resource_check_buffer - Check whether a backup buffer is needed
  824. * for a resource and in that case, allocate
  825. * one, reserve and validate it.
  826. *
  827. * @res: The resource for which to allocate a backup buffer.
  828. * @interruptible: Whether any sleeps during allocation should be
  829. * performed while interruptible.
  830. * @val_buf: On successful return contains data about the
  831. * reserved and validated backup buffer.
  832. */
  833. static int
  834. vmw_resource_check_buffer(struct vmw_resource *res,
  835. bool interruptible,
  836. struct ttm_validate_buffer *val_buf)
  837. {
  838. struct ttm_operation_ctx ctx = { true, false };
  839. struct list_head val_list;
  840. bool backup_dirty = false;
  841. int ret;
  842. if (unlikely(res->backup == NULL)) {
  843. ret = vmw_resource_buf_alloc(res, interruptible);
  844. if (unlikely(ret != 0))
  845. return ret;
  846. }
  847. INIT_LIST_HEAD(&val_list);
  848. val_buf->bo = ttm_bo_reference(&res->backup->base);
  849. val_buf->shared = false;
  850. list_add_tail(&val_buf->head, &val_list);
  851. ret = ttm_eu_reserve_buffers(NULL, &val_list, interruptible, NULL);
  852. if (unlikely(ret != 0))
  853. goto out_no_reserve;
  854. if (res->func->needs_backup && list_empty(&res->mob_head))
  855. return 0;
  856. backup_dirty = res->backup_dirty;
  857. ret = ttm_bo_validate(&res->backup->base,
  858. res->func->backup_placement,
  859. &ctx);
  860. if (unlikely(ret != 0))
  861. goto out_no_validate;
  862. return 0;
  863. out_no_validate:
  864. ttm_eu_backoff_reservation(NULL, &val_list);
  865. out_no_reserve:
  866. ttm_bo_unref(&val_buf->bo);
  867. if (backup_dirty)
  868. vmw_dmabuf_unreference(&res->backup);
  869. return ret;
  870. }
  871. /**
  872. * vmw_resource_reserve - Reserve a resource for command submission
  873. *
  874. * @res: The resource to reserve.
  875. *
  876. * This function takes the resource off the LRU list and make sure
  877. * a backup buffer is present for guest-backed resources. However,
  878. * the buffer may not be bound to the resource at this point.
  879. *
  880. */
  881. int vmw_resource_reserve(struct vmw_resource *res, bool interruptible,
  882. bool no_backup)
  883. {
  884. struct vmw_private *dev_priv = res->dev_priv;
  885. int ret;
  886. write_lock(&dev_priv->resource_lock);
  887. list_del_init(&res->lru_head);
  888. write_unlock(&dev_priv->resource_lock);
  889. if (res->func->needs_backup && res->backup == NULL &&
  890. !no_backup) {
  891. ret = vmw_resource_buf_alloc(res, interruptible);
  892. if (unlikely(ret != 0)) {
  893. DRM_ERROR("Failed to allocate a backup buffer "
  894. "of size %lu. bytes\n",
  895. (unsigned long) res->backup_size);
  896. return ret;
  897. }
  898. }
  899. return 0;
  900. }
  901. /**
  902. * vmw_resource_backoff_reservation - Unreserve and unreference a
  903. * backup buffer
  904. *.
  905. * @val_buf: Backup buffer information.
  906. */
  907. static void
  908. vmw_resource_backoff_reservation(struct ttm_validate_buffer *val_buf)
  909. {
  910. struct list_head val_list;
  911. if (likely(val_buf->bo == NULL))
  912. return;
  913. INIT_LIST_HEAD(&val_list);
  914. list_add_tail(&val_buf->head, &val_list);
  915. ttm_eu_backoff_reservation(NULL, &val_list);
  916. ttm_bo_unref(&val_buf->bo);
  917. }
  918. /**
  919. * vmw_resource_do_evict - Evict a resource, and transfer its data
  920. * to a backup buffer.
  921. *
  922. * @res: The resource to evict.
  923. * @interruptible: Whether to wait interruptible.
  924. */
  925. static int vmw_resource_do_evict(struct vmw_resource *res, bool interruptible)
  926. {
  927. struct ttm_validate_buffer val_buf;
  928. const struct vmw_res_func *func = res->func;
  929. int ret;
  930. BUG_ON(!func->may_evict);
  931. val_buf.bo = NULL;
  932. val_buf.shared = false;
  933. ret = vmw_resource_check_buffer(res, interruptible, &val_buf);
  934. if (unlikely(ret != 0))
  935. return ret;
  936. if (unlikely(func->unbind != NULL &&
  937. (!func->needs_backup || !list_empty(&res->mob_head)))) {
  938. ret = func->unbind(res, res->res_dirty, &val_buf);
  939. if (unlikely(ret != 0))
  940. goto out_no_unbind;
  941. list_del_init(&res->mob_head);
  942. }
  943. ret = func->destroy(res);
  944. res->backup_dirty = true;
  945. res->res_dirty = false;
  946. out_no_unbind:
  947. vmw_resource_backoff_reservation(&val_buf);
  948. return ret;
  949. }
  950. /**
  951. * vmw_resource_validate - Make a resource up-to-date and visible
  952. * to the device.
  953. *
  954. * @res: The resource to make visible to the device.
  955. *
  956. * On succesful return, any backup DMA buffer pointed to by @res->backup will
  957. * be reserved and validated.
  958. * On hardware resource shortage, this function will repeatedly evict
  959. * resources of the same type until the validation succeeds.
  960. */
  961. int vmw_resource_validate(struct vmw_resource *res)
  962. {
  963. int ret;
  964. struct vmw_resource *evict_res;
  965. struct vmw_private *dev_priv = res->dev_priv;
  966. struct list_head *lru_list = &dev_priv->res_lru[res->func->res_type];
  967. struct ttm_validate_buffer val_buf;
  968. unsigned err_count = 0;
  969. if (!res->func->create)
  970. return 0;
  971. val_buf.bo = NULL;
  972. val_buf.shared = false;
  973. if (res->backup)
  974. val_buf.bo = &res->backup->base;
  975. do {
  976. ret = vmw_resource_do_validate(res, &val_buf);
  977. if (likely(ret != -EBUSY))
  978. break;
  979. write_lock(&dev_priv->resource_lock);
  980. if (list_empty(lru_list) || !res->func->may_evict) {
  981. DRM_ERROR("Out of device device resources "
  982. "for %s.\n", res->func->type_name);
  983. ret = -EBUSY;
  984. write_unlock(&dev_priv->resource_lock);
  985. break;
  986. }
  987. evict_res = vmw_resource_reference
  988. (list_first_entry(lru_list, struct vmw_resource,
  989. lru_head));
  990. list_del_init(&evict_res->lru_head);
  991. write_unlock(&dev_priv->resource_lock);
  992. ret = vmw_resource_do_evict(evict_res, true);
  993. if (unlikely(ret != 0)) {
  994. write_lock(&dev_priv->resource_lock);
  995. list_add_tail(&evict_res->lru_head, lru_list);
  996. write_unlock(&dev_priv->resource_lock);
  997. if (ret == -ERESTARTSYS ||
  998. ++err_count > VMW_RES_EVICT_ERR_COUNT) {
  999. vmw_resource_unreference(&evict_res);
  1000. goto out_no_validate;
  1001. }
  1002. }
  1003. vmw_resource_unreference(&evict_res);
  1004. } while (1);
  1005. if (unlikely(ret != 0))
  1006. goto out_no_validate;
  1007. else if (!res->func->needs_backup && res->backup) {
  1008. list_del_init(&res->mob_head);
  1009. vmw_dmabuf_unreference(&res->backup);
  1010. }
  1011. return 0;
  1012. out_no_validate:
  1013. return ret;
  1014. }
  1015. /**
  1016. * vmw_fence_single_bo - Utility function to fence a single TTM buffer
  1017. * object without unreserving it.
  1018. *
  1019. * @bo: Pointer to the struct ttm_buffer_object to fence.
  1020. * @fence: Pointer to the fence. If NULL, this function will
  1021. * insert a fence into the command stream..
  1022. *
  1023. * Contrary to the ttm_eu version of this function, it takes only
  1024. * a single buffer object instead of a list, and it also doesn't
  1025. * unreserve the buffer object, which needs to be done separately.
  1026. */
  1027. void vmw_fence_single_bo(struct ttm_buffer_object *bo,
  1028. struct vmw_fence_obj *fence)
  1029. {
  1030. struct ttm_bo_device *bdev = bo->bdev;
  1031. struct vmw_private *dev_priv =
  1032. container_of(bdev, struct vmw_private, bdev);
  1033. if (fence == NULL) {
  1034. vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
  1035. reservation_object_add_excl_fence(bo->resv, &fence->base);
  1036. dma_fence_put(&fence->base);
  1037. } else
  1038. reservation_object_add_excl_fence(bo->resv, &fence->base);
  1039. }
  1040. /**
  1041. * vmw_resource_move_notify - TTM move_notify_callback
  1042. *
  1043. * @bo: The TTM buffer object about to move.
  1044. * @mem: The struct ttm_mem_reg indicating to what memory
  1045. * region the move is taking place.
  1046. *
  1047. * Evicts the Guest Backed hardware resource if the backup
  1048. * buffer is being moved out of MOB memory.
  1049. * Note that this function should not race with the resource
  1050. * validation code as long as it accesses only members of struct
  1051. * resource that remain static while bo::res is !NULL and
  1052. * while we have @bo reserved. struct resource::backup is *not* a
  1053. * static member. The resource validation code will take care
  1054. * to set @bo::res to NULL, while having @bo reserved when the
  1055. * buffer is no longer bound to the resource, so @bo:res can be
  1056. * used to determine whether there is a need to unbind and whether
  1057. * it is safe to unbind.
  1058. */
  1059. void vmw_resource_move_notify(struct ttm_buffer_object *bo,
  1060. struct ttm_mem_reg *mem)
  1061. {
  1062. struct vmw_dma_buffer *dma_buf;
  1063. if (mem == NULL)
  1064. return;
  1065. if (bo->destroy != vmw_dmabuf_bo_free &&
  1066. bo->destroy != vmw_user_dmabuf_destroy)
  1067. return;
  1068. dma_buf = container_of(bo, struct vmw_dma_buffer, base);
  1069. if (mem->mem_type != VMW_PL_MOB) {
  1070. struct vmw_resource *res, *n;
  1071. struct ttm_validate_buffer val_buf;
  1072. val_buf.bo = bo;
  1073. val_buf.shared = false;
  1074. list_for_each_entry_safe(res, n, &dma_buf->res_list, mob_head) {
  1075. if (unlikely(res->func->unbind == NULL))
  1076. continue;
  1077. (void) res->func->unbind(res, true, &val_buf);
  1078. res->backup_dirty = true;
  1079. res->res_dirty = false;
  1080. list_del_init(&res->mob_head);
  1081. }
  1082. (void) ttm_bo_wait(bo, false, false);
  1083. }
  1084. }
  1085. /**
  1086. * vmw_query_readback_all - Read back cached query states
  1087. *
  1088. * @dx_query_mob: Buffer containing the DX query MOB
  1089. *
  1090. * Read back cached states from the device if they exist. This function
  1091. * assumings binding_mutex is held.
  1092. */
  1093. int vmw_query_readback_all(struct vmw_dma_buffer *dx_query_mob)
  1094. {
  1095. struct vmw_resource *dx_query_ctx;
  1096. struct vmw_private *dev_priv;
  1097. struct {
  1098. SVGA3dCmdHeader header;
  1099. SVGA3dCmdDXReadbackAllQuery body;
  1100. } *cmd;
  1101. /* No query bound, so do nothing */
  1102. if (!dx_query_mob || !dx_query_mob->dx_query_ctx)
  1103. return 0;
  1104. dx_query_ctx = dx_query_mob->dx_query_ctx;
  1105. dev_priv = dx_query_ctx->dev_priv;
  1106. cmd = vmw_fifo_reserve_dx(dev_priv, sizeof(*cmd), dx_query_ctx->id);
  1107. if (unlikely(cmd == NULL)) {
  1108. DRM_ERROR("Failed reserving FIFO space for "
  1109. "query MOB read back.\n");
  1110. return -ENOMEM;
  1111. }
  1112. cmd->header.id = SVGA_3D_CMD_DX_READBACK_ALL_QUERY;
  1113. cmd->header.size = sizeof(cmd->body);
  1114. cmd->body.cid = dx_query_ctx->id;
  1115. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  1116. /* Triggers a rebind the next time affected context is bound */
  1117. dx_query_mob->dx_query_ctx = NULL;
  1118. return 0;
  1119. }
  1120. /**
  1121. * vmw_query_move_notify - Read back cached query states
  1122. *
  1123. * @bo: The TTM buffer object about to move.
  1124. * @mem: The memory region @bo is moving to.
  1125. *
  1126. * Called before the query MOB is swapped out to read back cached query
  1127. * states from the device.
  1128. */
  1129. void vmw_query_move_notify(struct ttm_buffer_object *bo,
  1130. struct ttm_mem_reg *mem)
  1131. {
  1132. struct vmw_dma_buffer *dx_query_mob;
  1133. struct ttm_bo_device *bdev = bo->bdev;
  1134. struct vmw_private *dev_priv;
  1135. dev_priv = container_of(bdev, struct vmw_private, bdev);
  1136. mutex_lock(&dev_priv->binding_mutex);
  1137. dx_query_mob = container_of(bo, struct vmw_dma_buffer, base);
  1138. if (mem == NULL || !dx_query_mob || !dx_query_mob->dx_query_ctx) {
  1139. mutex_unlock(&dev_priv->binding_mutex);
  1140. return;
  1141. }
  1142. /* If BO is being moved from MOB to system memory */
  1143. if (mem->mem_type == TTM_PL_SYSTEM && bo->mem.mem_type == VMW_PL_MOB) {
  1144. struct vmw_fence_obj *fence;
  1145. (void) vmw_query_readback_all(dx_query_mob);
  1146. mutex_unlock(&dev_priv->binding_mutex);
  1147. /* Create a fence and attach the BO to it */
  1148. (void) vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
  1149. vmw_fence_single_bo(bo, fence);
  1150. if (fence != NULL)
  1151. vmw_fence_obj_unreference(&fence);
  1152. (void) ttm_bo_wait(bo, false, false);
  1153. } else
  1154. mutex_unlock(&dev_priv->binding_mutex);
  1155. }
  1156. /**
  1157. * vmw_resource_needs_backup - Return whether a resource needs a backup buffer.
  1158. *
  1159. * @res: The resource being queried.
  1160. */
  1161. bool vmw_resource_needs_backup(const struct vmw_resource *res)
  1162. {
  1163. return res->func->needs_backup;
  1164. }
  1165. /**
  1166. * vmw_resource_evict_type - Evict all resources of a specific type
  1167. *
  1168. * @dev_priv: Pointer to a device private struct
  1169. * @type: The resource type to evict
  1170. *
  1171. * To avoid thrashing starvation or as part of the hibernation sequence,
  1172. * try to evict all evictable resources of a specific type.
  1173. */
  1174. static void vmw_resource_evict_type(struct vmw_private *dev_priv,
  1175. enum vmw_res_type type)
  1176. {
  1177. struct list_head *lru_list = &dev_priv->res_lru[type];
  1178. struct vmw_resource *evict_res;
  1179. unsigned err_count = 0;
  1180. int ret;
  1181. do {
  1182. write_lock(&dev_priv->resource_lock);
  1183. if (list_empty(lru_list))
  1184. goto out_unlock;
  1185. evict_res = vmw_resource_reference(
  1186. list_first_entry(lru_list, struct vmw_resource,
  1187. lru_head));
  1188. list_del_init(&evict_res->lru_head);
  1189. write_unlock(&dev_priv->resource_lock);
  1190. ret = vmw_resource_do_evict(evict_res, false);
  1191. if (unlikely(ret != 0)) {
  1192. write_lock(&dev_priv->resource_lock);
  1193. list_add_tail(&evict_res->lru_head, lru_list);
  1194. write_unlock(&dev_priv->resource_lock);
  1195. if (++err_count > VMW_RES_EVICT_ERR_COUNT) {
  1196. vmw_resource_unreference(&evict_res);
  1197. return;
  1198. }
  1199. }
  1200. vmw_resource_unreference(&evict_res);
  1201. } while (1);
  1202. out_unlock:
  1203. write_unlock(&dev_priv->resource_lock);
  1204. }
  1205. /**
  1206. * vmw_resource_evict_all - Evict all evictable resources
  1207. *
  1208. * @dev_priv: Pointer to a device private struct
  1209. *
  1210. * To avoid thrashing starvation or as part of the hibernation sequence,
  1211. * evict all evictable resources. In particular this means that all
  1212. * guest-backed resources that are registered with the device are
  1213. * evicted and the OTable becomes clean.
  1214. */
  1215. void vmw_resource_evict_all(struct vmw_private *dev_priv)
  1216. {
  1217. enum vmw_res_type type;
  1218. mutex_lock(&dev_priv->cmdbuf_mutex);
  1219. for (type = 0; type < vmw_res_max; ++type)
  1220. vmw_resource_evict_type(dev_priv, type);
  1221. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1222. }
  1223. /**
  1224. * vmw_resource_pin - Add a pin reference on a resource
  1225. *
  1226. * @res: The resource to add a pin reference on
  1227. *
  1228. * This function adds a pin reference, and if needed validates the resource.
  1229. * Having a pin reference means that the resource can never be evicted, and
  1230. * its id will never change as long as there is a pin reference.
  1231. * This function returns 0 on success and a negative error code on failure.
  1232. */
  1233. int vmw_resource_pin(struct vmw_resource *res, bool interruptible)
  1234. {
  1235. struct ttm_operation_ctx ctx = { interruptible, false };
  1236. struct vmw_private *dev_priv = res->dev_priv;
  1237. int ret;
  1238. ttm_write_lock(&dev_priv->reservation_sem, interruptible);
  1239. mutex_lock(&dev_priv->cmdbuf_mutex);
  1240. ret = vmw_resource_reserve(res, interruptible, false);
  1241. if (ret)
  1242. goto out_no_reserve;
  1243. if (res->pin_count == 0) {
  1244. struct vmw_dma_buffer *vbo = NULL;
  1245. if (res->backup) {
  1246. vbo = res->backup;
  1247. ttm_bo_reserve(&vbo->base, interruptible, false, NULL);
  1248. if (!vbo->pin_count) {
  1249. ret = ttm_bo_validate
  1250. (&vbo->base,
  1251. res->func->backup_placement,
  1252. &ctx);
  1253. if (ret) {
  1254. ttm_bo_unreserve(&vbo->base);
  1255. goto out_no_validate;
  1256. }
  1257. }
  1258. /* Do we really need to pin the MOB as well? */
  1259. vmw_bo_pin_reserved(vbo, true);
  1260. }
  1261. ret = vmw_resource_validate(res);
  1262. if (vbo)
  1263. ttm_bo_unreserve(&vbo->base);
  1264. if (ret)
  1265. goto out_no_validate;
  1266. }
  1267. res->pin_count++;
  1268. out_no_validate:
  1269. vmw_resource_unreserve(res, false, NULL, 0UL);
  1270. out_no_reserve:
  1271. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1272. ttm_write_unlock(&dev_priv->reservation_sem);
  1273. return ret;
  1274. }
  1275. /**
  1276. * vmw_resource_unpin - Remove a pin reference from a resource
  1277. *
  1278. * @res: The resource to remove a pin reference from
  1279. *
  1280. * Having a pin reference means that the resource can never be evicted, and
  1281. * its id will never change as long as there is a pin reference.
  1282. */
  1283. void vmw_resource_unpin(struct vmw_resource *res)
  1284. {
  1285. struct vmw_private *dev_priv = res->dev_priv;
  1286. int ret;
  1287. (void) ttm_read_lock(&dev_priv->reservation_sem, false);
  1288. mutex_lock(&dev_priv->cmdbuf_mutex);
  1289. ret = vmw_resource_reserve(res, false, true);
  1290. WARN_ON(ret);
  1291. WARN_ON(res->pin_count == 0);
  1292. if (--res->pin_count == 0 && res->backup) {
  1293. struct vmw_dma_buffer *vbo = res->backup;
  1294. (void) ttm_bo_reserve(&vbo->base, false, false, NULL);
  1295. vmw_bo_pin_reserved(vbo, false);
  1296. ttm_bo_unreserve(&vbo->base);
  1297. }
  1298. vmw_resource_unreserve(res, false, NULL, 0UL);
  1299. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1300. ttm_read_unlock(&dev_priv->reservation_sem);
  1301. }
  1302. /**
  1303. * vmw_res_type - Return the resource type
  1304. *
  1305. * @res: Pointer to the resource
  1306. */
  1307. enum vmw_res_type vmw_res_type(const struct vmw_resource *res)
  1308. {
  1309. return res->func->res_type;
  1310. }