vmwgfx_resource.c 45 KB

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