vmwgfx_resource.c 42 KB

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