vmwgfx_resource.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792
  1. /**************************************************************************
  2. *
  3. * Copyright © 2009-2015 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. #include "vmwgfx_drv.h"
  28. #include <drm/vmwgfx_drm.h>
  29. #include <drm/ttm/ttm_object.h>
  30. #include <drm/ttm/ttm_placement.h>
  31. #include <drm/drmP.h>
  32. #include "vmwgfx_resource_priv.h"
  33. #include "vmwgfx_binding.h"
  34. #define VMW_RES_EVICT_ERR_COUNT 10
  35. struct vmw_user_dma_buffer {
  36. struct ttm_prime_object prime;
  37. struct vmw_dma_buffer dma;
  38. };
  39. struct vmw_bo_user_rep {
  40. uint32_t handle;
  41. uint64_t map_handle;
  42. };
  43. 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, 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, NULL);
  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. struct ttm_base_object **p_base)
  413. {
  414. struct vmw_user_dma_buffer *user_bo;
  415. struct ttm_buffer_object *tmp;
  416. int ret;
  417. user_bo = kzalloc(sizeof(*user_bo), GFP_KERNEL);
  418. if (unlikely(user_bo == NULL)) {
  419. DRM_ERROR("Failed to allocate a buffer.\n");
  420. return -ENOMEM;
  421. }
  422. ret = vmw_dmabuf_init(dev_priv, &user_bo->dma, size,
  423. (dev_priv->has_mob) ?
  424. &vmw_sys_placement :
  425. &vmw_vram_sys_placement, true,
  426. &vmw_user_dmabuf_destroy);
  427. if (unlikely(ret != 0))
  428. return ret;
  429. tmp = ttm_bo_reference(&user_bo->dma.base);
  430. ret = ttm_prime_object_init(tfile,
  431. size,
  432. &user_bo->prime,
  433. shareable,
  434. ttm_buffer_type,
  435. &vmw_user_dmabuf_release,
  436. &vmw_user_dmabuf_ref_obj_release);
  437. if (unlikely(ret != 0)) {
  438. ttm_bo_unref(&tmp);
  439. goto out_no_base_object;
  440. }
  441. *p_dma_buf = &user_bo->dma;
  442. if (p_base) {
  443. *p_base = &user_bo->prime.base;
  444. kref_get(&(*p_base)->refcount);
  445. }
  446. *handle = user_bo->prime.base.hash.key;
  447. out_no_base_object:
  448. return ret;
  449. }
  450. /**
  451. * vmw_user_dmabuf_verify_access - verify access permissions on this
  452. * buffer object.
  453. *
  454. * @bo: Pointer to the buffer object being accessed
  455. * @tfile: Identifying the caller.
  456. */
  457. int vmw_user_dmabuf_verify_access(struct ttm_buffer_object *bo,
  458. struct ttm_object_file *tfile)
  459. {
  460. struct vmw_user_dma_buffer *vmw_user_bo;
  461. if (unlikely(bo->destroy != vmw_user_dmabuf_destroy))
  462. return -EPERM;
  463. vmw_user_bo = vmw_user_dma_buffer(bo);
  464. /* Check that the caller has opened the object. */
  465. if (likely(ttm_ref_object_exists(tfile, &vmw_user_bo->prime.base)))
  466. return 0;
  467. DRM_ERROR("Could not grant buffer access.\n");
  468. return -EPERM;
  469. }
  470. /**
  471. * vmw_user_dmabuf_synccpu_grab - Grab a struct vmw_user_dma_buffer for cpu
  472. * access, idling previous GPU operations on the buffer and optionally
  473. * blocking it for further command submissions.
  474. *
  475. * @user_bo: Pointer to the buffer object being grabbed for CPU access
  476. * @tfile: Identifying the caller.
  477. * @flags: Flags indicating how the grab should be performed.
  478. *
  479. * A blocking grab will be automatically released when @tfile is closed.
  480. */
  481. static int vmw_user_dmabuf_synccpu_grab(struct vmw_user_dma_buffer *user_bo,
  482. struct ttm_object_file *tfile,
  483. uint32_t flags)
  484. {
  485. struct ttm_buffer_object *bo = &user_bo->dma.base;
  486. bool existed;
  487. int ret;
  488. if (flags & drm_vmw_synccpu_allow_cs) {
  489. bool nonblock = !!(flags & drm_vmw_synccpu_dontblock);
  490. long lret;
  491. lret = reservation_object_wait_timeout_rcu(bo->resv, true, true,
  492. nonblock ? 0 : MAX_SCHEDULE_TIMEOUT);
  493. if (!lret)
  494. return -EBUSY;
  495. else if (lret < 0)
  496. return lret;
  497. return 0;
  498. }
  499. ret = ttm_bo_synccpu_write_grab
  500. (bo, !!(flags & drm_vmw_synccpu_dontblock));
  501. if (unlikely(ret != 0))
  502. return ret;
  503. ret = ttm_ref_object_add(tfile, &user_bo->prime.base,
  504. TTM_REF_SYNCCPU_WRITE, &existed);
  505. if (ret != 0 || existed)
  506. ttm_bo_synccpu_write_release(&user_bo->dma.base);
  507. return ret;
  508. }
  509. /**
  510. * vmw_user_dmabuf_synccpu_release - Release a previous grab for CPU access,
  511. * and unblock command submission on the buffer if blocked.
  512. *
  513. * @handle: Handle identifying the buffer object.
  514. * @tfile: Identifying the caller.
  515. * @flags: Flags indicating the type of release.
  516. */
  517. static int vmw_user_dmabuf_synccpu_release(uint32_t handle,
  518. struct ttm_object_file *tfile,
  519. uint32_t flags)
  520. {
  521. if (!(flags & drm_vmw_synccpu_allow_cs))
  522. return ttm_ref_object_base_unref(tfile, handle,
  523. TTM_REF_SYNCCPU_WRITE);
  524. return 0;
  525. }
  526. /**
  527. * vmw_user_dmabuf_synccpu_release - ioctl function implementing the synccpu
  528. * functionality.
  529. *
  530. * @dev: Identifies the drm device.
  531. * @data: Pointer to the ioctl argument.
  532. * @file_priv: Identifies the caller.
  533. *
  534. * This function checks the ioctl arguments for validity and calls the
  535. * relevant synccpu functions.
  536. */
  537. int vmw_user_dmabuf_synccpu_ioctl(struct drm_device *dev, void *data,
  538. struct drm_file *file_priv)
  539. {
  540. struct drm_vmw_synccpu_arg *arg =
  541. (struct drm_vmw_synccpu_arg *) data;
  542. struct vmw_dma_buffer *dma_buf;
  543. struct vmw_user_dma_buffer *user_bo;
  544. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  545. struct ttm_base_object *buffer_base;
  546. int ret;
  547. if ((arg->flags & (drm_vmw_synccpu_read | drm_vmw_synccpu_write)) == 0
  548. || (arg->flags & ~(drm_vmw_synccpu_read | drm_vmw_synccpu_write |
  549. drm_vmw_synccpu_dontblock |
  550. drm_vmw_synccpu_allow_cs)) != 0) {
  551. DRM_ERROR("Illegal synccpu flags.\n");
  552. return -EINVAL;
  553. }
  554. switch (arg->op) {
  555. case drm_vmw_synccpu_grab:
  556. ret = vmw_user_dmabuf_lookup(tfile, arg->handle, &dma_buf,
  557. &buffer_base);
  558. if (unlikely(ret != 0))
  559. return ret;
  560. user_bo = container_of(dma_buf, struct vmw_user_dma_buffer,
  561. dma);
  562. ret = vmw_user_dmabuf_synccpu_grab(user_bo, tfile, arg->flags);
  563. vmw_dmabuf_unreference(&dma_buf);
  564. ttm_base_object_unref(&buffer_base);
  565. if (unlikely(ret != 0 && ret != -ERESTARTSYS &&
  566. ret != -EBUSY)) {
  567. DRM_ERROR("Failed synccpu grab on handle 0x%08x.\n",
  568. (unsigned int) arg->handle);
  569. return ret;
  570. }
  571. break;
  572. case drm_vmw_synccpu_release:
  573. ret = vmw_user_dmabuf_synccpu_release(arg->handle, tfile,
  574. arg->flags);
  575. if (unlikely(ret != 0)) {
  576. DRM_ERROR("Failed synccpu release on handle 0x%08x.\n",
  577. (unsigned int) arg->handle);
  578. return ret;
  579. }
  580. break;
  581. default:
  582. DRM_ERROR("Invalid synccpu operation.\n");
  583. return -EINVAL;
  584. }
  585. return 0;
  586. }
  587. int vmw_dmabuf_alloc_ioctl(struct drm_device *dev, void *data,
  588. struct drm_file *file_priv)
  589. {
  590. struct vmw_private *dev_priv = vmw_priv(dev);
  591. union drm_vmw_alloc_dmabuf_arg *arg =
  592. (union drm_vmw_alloc_dmabuf_arg *)data;
  593. struct drm_vmw_alloc_dmabuf_req *req = &arg->req;
  594. struct drm_vmw_dmabuf_rep *rep = &arg->rep;
  595. struct vmw_dma_buffer *dma_buf;
  596. uint32_t handle;
  597. int ret;
  598. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  599. if (unlikely(ret != 0))
  600. return ret;
  601. ret = vmw_user_dmabuf_alloc(dev_priv, vmw_fpriv(file_priv)->tfile,
  602. req->size, false, &handle, &dma_buf,
  603. NULL);
  604. if (unlikely(ret != 0))
  605. goto out_no_dmabuf;
  606. rep->handle = handle;
  607. rep->map_handle = drm_vma_node_offset_addr(&dma_buf->base.vma_node);
  608. rep->cur_gmr_id = handle;
  609. rep->cur_gmr_offset = 0;
  610. vmw_dmabuf_unreference(&dma_buf);
  611. out_no_dmabuf:
  612. ttm_read_unlock(&dev_priv->reservation_sem);
  613. return ret;
  614. }
  615. int vmw_dmabuf_unref_ioctl(struct drm_device *dev, void *data,
  616. struct drm_file *file_priv)
  617. {
  618. struct drm_vmw_unref_dmabuf_arg *arg =
  619. (struct drm_vmw_unref_dmabuf_arg *)data;
  620. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  621. arg->handle,
  622. TTM_REF_USAGE);
  623. }
  624. int vmw_user_dmabuf_lookup(struct ttm_object_file *tfile,
  625. uint32_t handle, struct vmw_dma_buffer **out,
  626. struct ttm_base_object **p_base)
  627. {
  628. struct vmw_user_dma_buffer *vmw_user_bo;
  629. struct ttm_base_object *base;
  630. base = ttm_base_object_lookup(tfile, handle);
  631. if (unlikely(base == NULL)) {
  632. printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
  633. (unsigned long)handle);
  634. return -ESRCH;
  635. }
  636. if (unlikely(ttm_base_object_type(base) != ttm_buffer_type)) {
  637. ttm_base_object_unref(&base);
  638. printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
  639. (unsigned long)handle);
  640. return -EINVAL;
  641. }
  642. vmw_user_bo = container_of(base, struct vmw_user_dma_buffer,
  643. prime.base);
  644. (void)ttm_bo_reference(&vmw_user_bo->dma.base);
  645. if (p_base)
  646. *p_base = base;
  647. else
  648. ttm_base_object_unref(&base);
  649. *out = &vmw_user_bo->dma;
  650. return 0;
  651. }
  652. int vmw_user_dmabuf_reference(struct ttm_object_file *tfile,
  653. struct vmw_dma_buffer *dma_buf,
  654. uint32_t *handle)
  655. {
  656. struct vmw_user_dma_buffer *user_bo;
  657. if (dma_buf->base.destroy != vmw_user_dmabuf_destroy)
  658. return -EINVAL;
  659. user_bo = container_of(dma_buf, struct vmw_user_dma_buffer, dma);
  660. *handle = user_bo->prime.base.hash.key;
  661. return ttm_ref_object_add(tfile, &user_bo->prime.base,
  662. TTM_REF_USAGE, NULL);
  663. }
  664. /*
  665. * Stream management
  666. */
  667. static void vmw_stream_destroy(struct vmw_resource *res)
  668. {
  669. struct vmw_private *dev_priv = res->dev_priv;
  670. struct vmw_stream *stream;
  671. int ret;
  672. DRM_INFO("%s: unref\n", __func__);
  673. stream = container_of(res, struct vmw_stream, res);
  674. ret = vmw_overlay_unref(dev_priv, stream->stream_id);
  675. WARN_ON(ret != 0);
  676. }
  677. static int vmw_stream_init(struct vmw_private *dev_priv,
  678. struct vmw_stream *stream,
  679. void (*res_free) (struct vmw_resource *res))
  680. {
  681. struct vmw_resource *res = &stream->res;
  682. int ret;
  683. ret = vmw_resource_init(dev_priv, res, false, res_free,
  684. &vmw_stream_func);
  685. if (unlikely(ret != 0)) {
  686. if (res_free == NULL)
  687. kfree(stream);
  688. else
  689. res_free(&stream->res);
  690. return ret;
  691. }
  692. ret = vmw_overlay_claim(dev_priv, &stream->stream_id);
  693. if (ret) {
  694. vmw_resource_unreference(&res);
  695. return ret;
  696. }
  697. DRM_INFO("%s: claimed\n", __func__);
  698. vmw_resource_activate(&stream->res, vmw_stream_destroy);
  699. return 0;
  700. }
  701. static void vmw_user_stream_free(struct vmw_resource *res)
  702. {
  703. struct vmw_user_stream *stream =
  704. container_of(res, struct vmw_user_stream, stream.res);
  705. struct vmw_private *dev_priv = res->dev_priv;
  706. ttm_base_object_kfree(stream, base);
  707. ttm_mem_global_free(vmw_mem_glob(dev_priv),
  708. vmw_user_stream_size);
  709. }
  710. /**
  711. * This function is called when user space has no more references on the
  712. * base object. It releases the base-object's reference on the resource object.
  713. */
  714. static void vmw_user_stream_base_release(struct ttm_base_object **p_base)
  715. {
  716. struct ttm_base_object *base = *p_base;
  717. struct vmw_user_stream *stream =
  718. container_of(base, struct vmw_user_stream, base);
  719. struct vmw_resource *res = &stream->stream.res;
  720. *p_base = NULL;
  721. vmw_resource_unreference(&res);
  722. }
  723. int vmw_stream_unref_ioctl(struct drm_device *dev, void *data,
  724. struct drm_file *file_priv)
  725. {
  726. struct vmw_private *dev_priv = vmw_priv(dev);
  727. struct vmw_resource *res;
  728. struct vmw_user_stream *stream;
  729. struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
  730. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  731. struct idr *idr = &dev_priv->res_idr[vmw_res_stream];
  732. int ret = 0;
  733. res = vmw_resource_lookup(dev_priv, idr, arg->stream_id);
  734. if (unlikely(res == NULL))
  735. return -EINVAL;
  736. if (res->res_free != &vmw_user_stream_free) {
  737. ret = -EINVAL;
  738. goto out;
  739. }
  740. stream = container_of(res, struct vmw_user_stream, stream.res);
  741. if (stream->base.tfile != tfile) {
  742. ret = -EINVAL;
  743. goto out;
  744. }
  745. ttm_ref_object_base_unref(tfile, stream->base.hash.key, TTM_REF_USAGE);
  746. out:
  747. vmw_resource_unreference(&res);
  748. return ret;
  749. }
  750. int vmw_stream_claim_ioctl(struct drm_device *dev, void *data,
  751. struct drm_file *file_priv)
  752. {
  753. struct vmw_private *dev_priv = vmw_priv(dev);
  754. struct vmw_user_stream *stream;
  755. struct vmw_resource *res;
  756. struct vmw_resource *tmp;
  757. struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
  758. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  759. int ret;
  760. /*
  761. * Approximate idr memory usage with 128 bytes. It will be limited
  762. * by maximum number_of streams anyway?
  763. */
  764. if (unlikely(vmw_user_stream_size == 0))
  765. vmw_user_stream_size = ttm_round_pot(sizeof(*stream)) + 128;
  766. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  767. if (unlikely(ret != 0))
  768. return ret;
  769. ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
  770. vmw_user_stream_size,
  771. false, true);
  772. ttm_read_unlock(&dev_priv->reservation_sem);
  773. if (unlikely(ret != 0)) {
  774. if (ret != -ERESTARTSYS)
  775. DRM_ERROR("Out of graphics memory for stream"
  776. " creation.\n");
  777. goto out_ret;
  778. }
  779. stream = kmalloc(sizeof(*stream), GFP_KERNEL);
  780. if (unlikely(stream == NULL)) {
  781. ttm_mem_global_free(vmw_mem_glob(dev_priv),
  782. vmw_user_stream_size);
  783. ret = -ENOMEM;
  784. goto out_ret;
  785. }
  786. res = &stream->stream.res;
  787. stream->base.shareable = false;
  788. stream->base.tfile = NULL;
  789. /*
  790. * From here on, the destructor takes over resource freeing.
  791. */
  792. ret = vmw_stream_init(dev_priv, &stream->stream, vmw_user_stream_free);
  793. if (unlikely(ret != 0))
  794. goto out_ret;
  795. tmp = vmw_resource_reference(res);
  796. ret = ttm_base_object_init(tfile, &stream->base, false, VMW_RES_STREAM,
  797. &vmw_user_stream_base_release, NULL);
  798. if (unlikely(ret != 0)) {
  799. vmw_resource_unreference(&tmp);
  800. goto out_err;
  801. }
  802. arg->stream_id = res->id;
  803. out_err:
  804. vmw_resource_unreference(&res);
  805. out_ret:
  806. return ret;
  807. }
  808. int vmw_user_stream_lookup(struct vmw_private *dev_priv,
  809. struct ttm_object_file *tfile,
  810. uint32_t *inout_id, struct vmw_resource **out)
  811. {
  812. struct vmw_user_stream *stream;
  813. struct vmw_resource *res;
  814. int ret;
  815. res = vmw_resource_lookup(dev_priv, &dev_priv->res_idr[vmw_res_stream],
  816. *inout_id);
  817. if (unlikely(res == NULL))
  818. return -EINVAL;
  819. if (res->res_free != &vmw_user_stream_free) {
  820. ret = -EINVAL;
  821. goto err_ref;
  822. }
  823. stream = container_of(res, struct vmw_user_stream, stream.res);
  824. if (stream->base.tfile != tfile) {
  825. ret = -EPERM;
  826. goto err_ref;
  827. }
  828. *inout_id = stream->stream.stream_id;
  829. *out = res;
  830. return 0;
  831. err_ref:
  832. vmw_resource_unreference(&res);
  833. return ret;
  834. }
  835. /**
  836. * vmw_dumb_create - Create a dumb kms buffer
  837. *
  838. * @file_priv: Pointer to a struct drm_file identifying the caller.
  839. * @dev: Pointer to the drm device.
  840. * @args: Pointer to a struct drm_mode_create_dumb structure
  841. *
  842. * This is a driver callback for the core drm create_dumb functionality.
  843. * Note that this is very similar to the vmw_dmabuf_alloc ioctl, except
  844. * that the arguments have a different format.
  845. */
  846. int vmw_dumb_create(struct drm_file *file_priv,
  847. struct drm_device *dev,
  848. struct drm_mode_create_dumb *args)
  849. {
  850. struct vmw_private *dev_priv = vmw_priv(dev);
  851. struct vmw_dma_buffer *dma_buf;
  852. int ret;
  853. args->pitch = args->width * ((args->bpp + 7) / 8);
  854. args->size = args->pitch * args->height;
  855. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  856. if (unlikely(ret != 0))
  857. return ret;
  858. ret = vmw_user_dmabuf_alloc(dev_priv, vmw_fpriv(file_priv)->tfile,
  859. args->size, false, &args->handle,
  860. &dma_buf, NULL);
  861. if (unlikely(ret != 0))
  862. goto out_no_dmabuf;
  863. vmw_dmabuf_unreference(&dma_buf);
  864. out_no_dmabuf:
  865. ttm_read_unlock(&dev_priv->reservation_sem);
  866. return ret;
  867. }
  868. /**
  869. * vmw_dumb_map_offset - Return the address space offset of a dumb buffer
  870. *
  871. * @file_priv: Pointer to a struct drm_file identifying the caller.
  872. * @dev: Pointer to the drm device.
  873. * @handle: Handle identifying the dumb buffer.
  874. * @offset: The address space offset returned.
  875. *
  876. * This is a driver callback for the core drm dumb_map_offset functionality.
  877. */
  878. int vmw_dumb_map_offset(struct drm_file *file_priv,
  879. struct drm_device *dev, uint32_t handle,
  880. uint64_t *offset)
  881. {
  882. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  883. struct vmw_dma_buffer *out_buf;
  884. int ret;
  885. ret = vmw_user_dmabuf_lookup(tfile, handle, &out_buf, NULL);
  886. if (ret != 0)
  887. return -EINVAL;
  888. *offset = drm_vma_node_offset_addr(&out_buf->base.vma_node);
  889. vmw_dmabuf_unreference(&out_buf);
  890. return 0;
  891. }
  892. /**
  893. * vmw_dumb_destroy - Destroy a dumb boffer
  894. *
  895. * @file_priv: Pointer to a struct drm_file identifying the caller.
  896. * @dev: Pointer to the drm device.
  897. * @handle: Handle identifying the dumb buffer.
  898. *
  899. * This is a driver callback for the core drm dumb_destroy functionality.
  900. */
  901. int vmw_dumb_destroy(struct drm_file *file_priv,
  902. struct drm_device *dev,
  903. uint32_t handle)
  904. {
  905. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  906. handle, TTM_REF_USAGE);
  907. }
  908. /**
  909. * vmw_resource_buf_alloc - Allocate a backup buffer for a resource.
  910. *
  911. * @res: The resource for which to allocate a backup buffer.
  912. * @interruptible: Whether any sleeps during allocation should be
  913. * performed while interruptible.
  914. */
  915. static int vmw_resource_buf_alloc(struct vmw_resource *res,
  916. bool interruptible)
  917. {
  918. unsigned long size =
  919. (res->backup_size + PAGE_SIZE - 1) & PAGE_MASK;
  920. struct vmw_dma_buffer *backup;
  921. int ret;
  922. if (likely(res->backup)) {
  923. BUG_ON(res->backup->base.num_pages * PAGE_SIZE < size);
  924. return 0;
  925. }
  926. backup = kzalloc(sizeof(*backup), GFP_KERNEL);
  927. if (unlikely(backup == NULL))
  928. return -ENOMEM;
  929. ret = vmw_dmabuf_init(res->dev_priv, backup, res->backup_size,
  930. res->func->backup_placement,
  931. interruptible,
  932. &vmw_dmabuf_bo_free);
  933. if (unlikely(ret != 0))
  934. goto out_no_dmabuf;
  935. res->backup = backup;
  936. out_no_dmabuf:
  937. return ret;
  938. }
  939. /**
  940. * vmw_resource_do_validate - Make a resource up-to-date and visible
  941. * to the device.
  942. *
  943. * @res: The resource to make visible to the device.
  944. * @val_buf: Information about a buffer possibly
  945. * containing backup data if a bind operation is needed.
  946. *
  947. * On hardware resource shortage, this function returns -EBUSY and
  948. * should be retried once resources have been freed up.
  949. */
  950. static int vmw_resource_do_validate(struct vmw_resource *res,
  951. struct ttm_validate_buffer *val_buf)
  952. {
  953. int ret = 0;
  954. const struct vmw_res_func *func = res->func;
  955. if (unlikely(res->id == -1)) {
  956. ret = func->create(res);
  957. if (unlikely(ret != 0))
  958. return ret;
  959. }
  960. if (func->bind &&
  961. ((func->needs_backup && list_empty(&res->mob_head) &&
  962. val_buf->bo != NULL) ||
  963. (!func->needs_backup && val_buf->bo != NULL))) {
  964. ret = func->bind(res, val_buf);
  965. if (unlikely(ret != 0))
  966. goto out_bind_failed;
  967. if (func->needs_backup)
  968. list_add_tail(&res->mob_head, &res->backup->res_list);
  969. }
  970. /*
  971. * Only do this on write operations, and move to
  972. * vmw_resource_unreserve if it can be called after
  973. * backup buffers have been unreserved. Otherwise
  974. * sort out locking.
  975. */
  976. res->res_dirty = true;
  977. return 0;
  978. out_bind_failed:
  979. func->destroy(res);
  980. return ret;
  981. }
  982. /**
  983. * vmw_resource_unreserve - Unreserve a resource previously reserved for
  984. * command submission.
  985. *
  986. * @res: Pointer to the struct vmw_resource to unreserve.
  987. * @switch_backup: Backup buffer has been switched.
  988. * @new_backup: Pointer to new backup buffer if command submission
  989. * switched. May be NULL.
  990. * @new_backup_offset: New backup offset if @switch_backup is true.
  991. *
  992. * Currently unreserving a resource means putting it back on the device's
  993. * resource lru list, so that it can be evicted if necessary.
  994. */
  995. void vmw_resource_unreserve(struct vmw_resource *res,
  996. bool switch_backup,
  997. struct vmw_dma_buffer *new_backup,
  998. unsigned long new_backup_offset)
  999. {
  1000. struct vmw_private *dev_priv = res->dev_priv;
  1001. if (!list_empty(&res->lru_head))
  1002. return;
  1003. if (switch_backup && new_backup != res->backup) {
  1004. if (res->backup) {
  1005. lockdep_assert_held(&res->backup->base.resv->lock.base);
  1006. list_del_init(&res->mob_head);
  1007. vmw_dmabuf_unreference(&res->backup);
  1008. }
  1009. if (new_backup) {
  1010. res->backup = vmw_dmabuf_reference(new_backup);
  1011. lockdep_assert_held(&new_backup->base.resv->lock.base);
  1012. list_add_tail(&res->mob_head, &new_backup->res_list);
  1013. } else {
  1014. res->backup = NULL;
  1015. }
  1016. }
  1017. if (switch_backup)
  1018. res->backup_offset = new_backup_offset;
  1019. if (!res->func->may_evict || res->id == -1 || res->pin_count)
  1020. return;
  1021. write_lock(&dev_priv->resource_lock);
  1022. list_add_tail(&res->lru_head,
  1023. &res->dev_priv->res_lru[res->func->res_type]);
  1024. write_unlock(&dev_priv->resource_lock);
  1025. }
  1026. /**
  1027. * vmw_resource_check_buffer - Check whether a backup buffer is needed
  1028. * for a resource and in that case, allocate
  1029. * one, reserve and validate it.
  1030. *
  1031. * @res: The resource for which to allocate a backup buffer.
  1032. * @interruptible: Whether any sleeps during allocation should be
  1033. * performed while interruptible.
  1034. * @val_buf: On successful return contains data about the
  1035. * reserved and validated backup buffer.
  1036. */
  1037. static int
  1038. vmw_resource_check_buffer(struct vmw_resource *res,
  1039. bool interruptible,
  1040. struct ttm_validate_buffer *val_buf)
  1041. {
  1042. struct list_head val_list;
  1043. bool backup_dirty = false;
  1044. int ret;
  1045. if (unlikely(res->backup == NULL)) {
  1046. ret = vmw_resource_buf_alloc(res, interruptible);
  1047. if (unlikely(ret != 0))
  1048. return ret;
  1049. }
  1050. INIT_LIST_HEAD(&val_list);
  1051. val_buf->bo = ttm_bo_reference(&res->backup->base);
  1052. val_buf->shared = false;
  1053. list_add_tail(&val_buf->head, &val_list);
  1054. ret = ttm_eu_reserve_buffers(NULL, &val_list, interruptible, NULL);
  1055. if (unlikely(ret != 0))
  1056. goto out_no_reserve;
  1057. if (res->func->needs_backup && list_empty(&res->mob_head))
  1058. return 0;
  1059. backup_dirty = res->backup_dirty;
  1060. ret = ttm_bo_validate(&res->backup->base,
  1061. res->func->backup_placement,
  1062. true, false);
  1063. if (unlikely(ret != 0))
  1064. goto out_no_validate;
  1065. return 0;
  1066. out_no_validate:
  1067. ttm_eu_backoff_reservation(NULL, &val_list);
  1068. out_no_reserve:
  1069. ttm_bo_unref(&val_buf->bo);
  1070. if (backup_dirty)
  1071. vmw_dmabuf_unreference(&res->backup);
  1072. return ret;
  1073. }
  1074. /**
  1075. * vmw_resource_reserve - Reserve a resource for command submission
  1076. *
  1077. * @res: The resource to reserve.
  1078. *
  1079. * This function takes the resource off the LRU list and make sure
  1080. * a backup buffer is present for guest-backed resources. However,
  1081. * the buffer may not be bound to the resource at this point.
  1082. *
  1083. */
  1084. int vmw_resource_reserve(struct vmw_resource *res, bool interruptible,
  1085. bool no_backup)
  1086. {
  1087. struct vmw_private *dev_priv = res->dev_priv;
  1088. int ret;
  1089. write_lock(&dev_priv->resource_lock);
  1090. list_del_init(&res->lru_head);
  1091. write_unlock(&dev_priv->resource_lock);
  1092. if (res->func->needs_backup && res->backup == NULL &&
  1093. !no_backup) {
  1094. ret = vmw_resource_buf_alloc(res, interruptible);
  1095. if (unlikely(ret != 0)) {
  1096. DRM_ERROR("Failed to allocate a backup buffer "
  1097. "of size %lu. bytes\n",
  1098. (unsigned long) res->backup_size);
  1099. return ret;
  1100. }
  1101. }
  1102. return 0;
  1103. }
  1104. /**
  1105. * vmw_resource_backoff_reservation - Unreserve and unreference a
  1106. * backup buffer
  1107. *.
  1108. * @val_buf: Backup buffer information.
  1109. */
  1110. static void
  1111. vmw_resource_backoff_reservation(struct ttm_validate_buffer *val_buf)
  1112. {
  1113. struct list_head val_list;
  1114. if (likely(val_buf->bo == NULL))
  1115. return;
  1116. INIT_LIST_HEAD(&val_list);
  1117. list_add_tail(&val_buf->head, &val_list);
  1118. ttm_eu_backoff_reservation(NULL, &val_list);
  1119. ttm_bo_unref(&val_buf->bo);
  1120. }
  1121. /**
  1122. * vmw_resource_do_evict - Evict a resource, and transfer its data
  1123. * to a backup buffer.
  1124. *
  1125. * @res: The resource to evict.
  1126. * @interruptible: Whether to wait interruptible.
  1127. */
  1128. static int vmw_resource_do_evict(struct vmw_resource *res, bool interruptible)
  1129. {
  1130. struct ttm_validate_buffer val_buf;
  1131. const struct vmw_res_func *func = res->func;
  1132. int ret;
  1133. BUG_ON(!func->may_evict);
  1134. val_buf.bo = NULL;
  1135. val_buf.shared = false;
  1136. ret = vmw_resource_check_buffer(res, interruptible, &val_buf);
  1137. if (unlikely(ret != 0))
  1138. return ret;
  1139. if (unlikely(func->unbind != NULL &&
  1140. (!func->needs_backup || !list_empty(&res->mob_head)))) {
  1141. ret = func->unbind(res, res->res_dirty, &val_buf);
  1142. if (unlikely(ret != 0))
  1143. goto out_no_unbind;
  1144. list_del_init(&res->mob_head);
  1145. }
  1146. ret = func->destroy(res);
  1147. res->backup_dirty = true;
  1148. res->res_dirty = false;
  1149. out_no_unbind:
  1150. vmw_resource_backoff_reservation(&val_buf);
  1151. return ret;
  1152. }
  1153. /**
  1154. * vmw_resource_validate - Make a resource up-to-date and visible
  1155. * to the device.
  1156. *
  1157. * @res: The resource to make visible to the device.
  1158. *
  1159. * On succesful return, any backup DMA buffer pointed to by @res->backup will
  1160. * be reserved and validated.
  1161. * On hardware resource shortage, this function will repeatedly evict
  1162. * resources of the same type until the validation succeeds.
  1163. */
  1164. int vmw_resource_validate(struct vmw_resource *res)
  1165. {
  1166. int ret;
  1167. struct vmw_resource *evict_res;
  1168. struct vmw_private *dev_priv = res->dev_priv;
  1169. struct list_head *lru_list = &dev_priv->res_lru[res->func->res_type];
  1170. struct ttm_validate_buffer val_buf;
  1171. unsigned err_count = 0;
  1172. if (!res->func->create)
  1173. return 0;
  1174. val_buf.bo = NULL;
  1175. val_buf.shared = false;
  1176. if (res->backup)
  1177. val_buf.bo = &res->backup->base;
  1178. do {
  1179. ret = vmw_resource_do_validate(res, &val_buf);
  1180. if (likely(ret != -EBUSY))
  1181. break;
  1182. write_lock(&dev_priv->resource_lock);
  1183. if (list_empty(lru_list) || !res->func->may_evict) {
  1184. DRM_ERROR("Out of device device resources "
  1185. "for %s.\n", res->func->type_name);
  1186. ret = -EBUSY;
  1187. write_unlock(&dev_priv->resource_lock);
  1188. break;
  1189. }
  1190. evict_res = vmw_resource_reference
  1191. (list_first_entry(lru_list, struct vmw_resource,
  1192. lru_head));
  1193. list_del_init(&evict_res->lru_head);
  1194. write_unlock(&dev_priv->resource_lock);
  1195. ret = vmw_resource_do_evict(evict_res, true);
  1196. if (unlikely(ret != 0)) {
  1197. write_lock(&dev_priv->resource_lock);
  1198. list_add_tail(&evict_res->lru_head, lru_list);
  1199. write_unlock(&dev_priv->resource_lock);
  1200. if (ret == -ERESTARTSYS ||
  1201. ++err_count > VMW_RES_EVICT_ERR_COUNT) {
  1202. vmw_resource_unreference(&evict_res);
  1203. goto out_no_validate;
  1204. }
  1205. }
  1206. vmw_resource_unreference(&evict_res);
  1207. } while (1);
  1208. if (unlikely(ret != 0))
  1209. goto out_no_validate;
  1210. else if (!res->func->needs_backup && res->backup) {
  1211. list_del_init(&res->mob_head);
  1212. vmw_dmabuf_unreference(&res->backup);
  1213. }
  1214. return 0;
  1215. out_no_validate:
  1216. return ret;
  1217. }
  1218. /**
  1219. * vmw_fence_single_bo - Utility function to fence a single TTM buffer
  1220. * object without unreserving it.
  1221. *
  1222. * @bo: Pointer to the struct ttm_buffer_object to fence.
  1223. * @fence: Pointer to the fence. If NULL, this function will
  1224. * insert a fence into the command stream..
  1225. *
  1226. * Contrary to the ttm_eu version of this function, it takes only
  1227. * a single buffer object instead of a list, and it also doesn't
  1228. * unreserve the buffer object, which needs to be done separately.
  1229. */
  1230. void vmw_fence_single_bo(struct ttm_buffer_object *bo,
  1231. struct vmw_fence_obj *fence)
  1232. {
  1233. struct ttm_bo_device *bdev = bo->bdev;
  1234. struct vmw_private *dev_priv =
  1235. container_of(bdev, struct vmw_private, bdev);
  1236. if (fence == NULL) {
  1237. vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
  1238. reservation_object_add_excl_fence(bo->resv, &fence->base);
  1239. dma_fence_put(&fence->base);
  1240. } else
  1241. reservation_object_add_excl_fence(bo->resv, &fence->base);
  1242. }
  1243. /**
  1244. * vmw_resource_move_notify - TTM move_notify_callback
  1245. *
  1246. * @bo: The TTM buffer object about to move.
  1247. * @mem: The struct ttm_mem_reg indicating to what memory
  1248. * region the move is taking place.
  1249. *
  1250. * Evicts the Guest Backed hardware resource if the backup
  1251. * buffer is being moved out of MOB memory.
  1252. * Note that this function should not race with the resource
  1253. * validation code as long as it accesses only members of struct
  1254. * resource that remain static while bo::res is !NULL and
  1255. * while we have @bo reserved. struct resource::backup is *not* a
  1256. * static member. The resource validation code will take care
  1257. * to set @bo::res to NULL, while having @bo reserved when the
  1258. * buffer is no longer bound to the resource, so @bo:res can be
  1259. * used to determine whether there is a need to unbind and whether
  1260. * it is safe to unbind.
  1261. */
  1262. void vmw_resource_move_notify(struct ttm_buffer_object *bo,
  1263. struct ttm_mem_reg *mem)
  1264. {
  1265. struct vmw_dma_buffer *dma_buf;
  1266. if (mem == NULL)
  1267. return;
  1268. if (bo->destroy != vmw_dmabuf_bo_free &&
  1269. bo->destroy != vmw_user_dmabuf_destroy)
  1270. return;
  1271. dma_buf = container_of(bo, struct vmw_dma_buffer, base);
  1272. if (mem->mem_type != VMW_PL_MOB) {
  1273. struct vmw_resource *res, *n;
  1274. struct ttm_validate_buffer val_buf;
  1275. val_buf.bo = bo;
  1276. val_buf.shared = false;
  1277. list_for_each_entry_safe(res, n, &dma_buf->res_list, mob_head) {
  1278. if (unlikely(res->func->unbind == NULL))
  1279. continue;
  1280. (void) res->func->unbind(res, true, &val_buf);
  1281. res->backup_dirty = true;
  1282. res->res_dirty = false;
  1283. list_del_init(&res->mob_head);
  1284. }
  1285. (void) ttm_bo_wait(bo, false, false);
  1286. }
  1287. }
  1288. /**
  1289. * vmw_query_readback_all - Read back cached query states
  1290. *
  1291. * @dx_query_mob: Buffer containing the DX query MOB
  1292. *
  1293. * Read back cached states from the device if they exist. This function
  1294. * assumings binding_mutex is held.
  1295. */
  1296. int vmw_query_readback_all(struct vmw_dma_buffer *dx_query_mob)
  1297. {
  1298. struct vmw_resource *dx_query_ctx;
  1299. struct vmw_private *dev_priv;
  1300. struct {
  1301. SVGA3dCmdHeader header;
  1302. SVGA3dCmdDXReadbackAllQuery body;
  1303. } *cmd;
  1304. /* No query bound, so do nothing */
  1305. if (!dx_query_mob || !dx_query_mob->dx_query_ctx)
  1306. return 0;
  1307. dx_query_ctx = dx_query_mob->dx_query_ctx;
  1308. dev_priv = dx_query_ctx->dev_priv;
  1309. cmd = vmw_fifo_reserve_dx(dev_priv, sizeof(*cmd), dx_query_ctx->id);
  1310. if (unlikely(cmd == NULL)) {
  1311. DRM_ERROR("Failed reserving FIFO space for "
  1312. "query MOB read back.\n");
  1313. return -ENOMEM;
  1314. }
  1315. cmd->header.id = SVGA_3D_CMD_DX_READBACK_ALL_QUERY;
  1316. cmd->header.size = sizeof(cmd->body);
  1317. cmd->body.cid = dx_query_ctx->id;
  1318. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  1319. /* Triggers a rebind the next time affected context is bound */
  1320. dx_query_mob->dx_query_ctx = NULL;
  1321. return 0;
  1322. }
  1323. /**
  1324. * vmw_query_move_notify - Read back cached query states
  1325. *
  1326. * @bo: The TTM buffer object about to move.
  1327. * @mem: The memory region @bo is moving to.
  1328. *
  1329. * Called before the query MOB is swapped out to read back cached query
  1330. * states from the device.
  1331. */
  1332. void vmw_query_move_notify(struct ttm_buffer_object *bo,
  1333. struct ttm_mem_reg *mem)
  1334. {
  1335. struct vmw_dma_buffer *dx_query_mob;
  1336. struct ttm_bo_device *bdev = bo->bdev;
  1337. struct vmw_private *dev_priv;
  1338. dev_priv = container_of(bdev, struct vmw_private, bdev);
  1339. mutex_lock(&dev_priv->binding_mutex);
  1340. dx_query_mob = container_of(bo, struct vmw_dma_buffer, base);
  1341. if (mem == NULL || !dx_query_mob || !dx_query_mob->dx_query_ctx) {
  1342. mutex_unlock(&dev_priv->binding_mutex);
  1343. return;
  1344. }
  1345. /* If BO is being moved from MOB to system memory */
  1346. if (mem->mem_type == TTM_PL_SYSTEM && bo->mem.mem_type == VMW_PL_MOB) {
  1347. struct vmw_fence_obj *fence;
  1348. (void) vmw_query_readback_all(dx_query_mob);
  1349. mutex_unlock(&dev_priv->binding_mutex);
  1350. /* Create a fence and attach the BO to it */
  1351. (void) vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
  1352. vmw_fence_single_bo(bo, fence);
  1353. if (fence != NULL)
  1354. vmw_fence_obj_unreference(&fence);
  1355. (void) ttm_bo_wait(bo, false, false);
  1356. } else
  1357. mutex_unlock(&dev_priv->binding_mutex);
  1358. }
  1359. /**
  1360. * vmw_resource_needs_backup - Return whether a resource needs a backup buffer.
  1361. *
  1362. * @res: The resource being queried.
  1363. */
  1364. bool vmw_resource_needs_backup(const struct vmw_resource *res)
  1365. {
  1366. return res->func->needs_backup;
  1367. }
  1368. /**
  1369. * vmw_resource_evict_type - Evict all resources of a specific type
  1370. *
  1371. * @dev_priv: Pointer to a device private struct
  1372. * @type: The resource type to evict
  1373. *
  1374. * To avoid thrashing starvation or as part of the hibernation sequence,
  1375. * try to evict all evictable resources of a specific type.
  1376. */
  1377. static void vmw_resource_evict_type(struct vmw_private *dev_priv,
  1378. enum vmw_res_type type)
  1379. {
  1380. struct list_head *lru_list = &dev_priv->res_lru[type];
  1381. struct vmw_resource *evict_res;
  1382. unsigned err_count = 0;
  1383. int ret;
  1384. do {
  1385. write_lock(&dev_priv->resource_lock);
  1386. if (list_empty(lru_list))
  1387. goto out_unlock;
  1388. evict_res = vmw_resource_reference(
  1389. list_first_entry(lru_list, struct vmw_resource,
  1390. lru_head));
  1391. list_del_init(&evict_res->lru_head);
  1392. write_unlock(&dev_priv->resource_lock);
  1393. ret = vmw_resource_do_evict(evict_res, false);
  1394. if (unlikely(ret != 0)) {
  1395. write_lock(&dev_priv->resource_lock);
  1396. list_add_tail(&evict_res->lru_head, lru_list);
  1397. write_unlock(&dev_priv->resource_lock);
  1398. if (++err_count > VMW_RES_EVICT_ERR_COUNT) {
  1399. vmw_resource_unreference(&evict_res);
  1400. return;
  1401. }
  1402. }
  1403. vmw_resource_unreference(&evict_res);
  1404. } while (1);
  1405. out_unlock:
  1406. write_unlock(&dev_priv->resource_lock);
  1407. }
  1408. /**
  1409. * vmw_resource_evict_all - Evict all evictable resources
  1410. *
  1411. * @dev_priv: Pointer to a device private struct
  1412. *
  1413. * To avoid thrashing starvation or as part of the hibernation sequence,
  1414. * evict all evictable resources. In particular this means that all
  1415. * guest-backed resources that are registered with the device are
  1416. * evicted and the OTable becomes clean.
  1417. */
  1418. void vmw_resource_evict_all(struct vmw_private *dev_priv)
  1419. {
  1420. enum vmw_res_type type;
  1421. mutex_lock(&dev_priv->cmdbuf_mutex);
  1422. for (type = 0; type < vmw_res_max; ++type)
  1423. vmw_resource_evict_type(dev_priv, type);
  1424. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1425. }
  1426. /**
  1427. * vmw_resource_pin - Add a pin reference on a resource
  1428. *
  1429. * @res: The resource to add a pin reference on
  1430. *
  1431. * This function adds a pin reference, and if needed validates the resource.
  1432. * Having a pin reference means that the resource can never be evicted, and
  1433. * its id will never change as long as there is a pin reference.
  1434. * This function returns 0 on success and a negative error code on failure.
  1435. */
  1436. int vmw_resource_pin(struct vmw_resource *res, bool interruptible)
  1437. {
  1438. struct vmw_private *dev_priv = res->dev_priv;
  1439. int ret;
  1440. ttm_write_lock(&dev_priv->reservation_sem, interruptible);
  1441. mutex_lock(&dev_priv->cmdbuf_mutex);
  1442. ret = vmw_resource_reserve(res, interruptible, false);
  1443. if (ret)
  1444. goto out_no_reserve;
  1445. if (res->pin_count == 0) {
  1446. struct vmw_dma_buffer *vbo = NULL;
  1447. if (res->backup) {
  1448. vbo = res->backup;
  1449. ttm_bo_reserve(&vbo->base, interruptible, false, NULL);
  1450. if (!vbo->pin_count) {
  1451. ret = ttm_bo_validate
  1452. (&vbo->base,
  1453. res->func->backup_placement,
  1454. interruptible, false);
  1455. if (ret) {
  1456. ttm_bo_unreserve(&vbo->base);
  1457. goto out_no_validate;
  1458. }
  1459. }
  1460. /* Do we really need to pin the MOB as well? */
  1461. vmw_bo_pin_reserved(vbo, true);
  1462. }
  1463. ret = vmw_resource_validate(res);
  1464. if (vbo)
  1465. ttm_bo_unreserve(&vbo->base);
  1466. if (ret)
  1467. goto out_no_validate;
  1468. }
  1469. res->pin_count++;
  1470. out_no_validate:
  1471. vmw_resource_unreserve(res, false, NULL, 0UL);
  1472. out_no_reserve:
  1473. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1474. ttm_write_unlock(&dev_priv->reservation_sem);
  1475. return ret;
  1476. }
  1477. /**
  1478. * vmw_resource_unpin - Remove a pin reference from a resource
  1479. *
  1480. * @res: The resource to remove a pin reference from
  1481. *
  1482. * Having a pin reference means that the resource can never be evicted, and
  1483. * its id will never change as long as there is a pin reference.
  1484. */
  1485. void vmw_resource_unpin(struct vmw_resource *res)
  1486. {
  1487. struct vmw_private *dev_priv = res->dev_priv;
  1488. int ret;
  1489. ttm_read_lock(&dev_priv->reservation_sem, false);
  1490. mutex_lock(&dev_priv->cmdbuf_mutex);
  1491. ret = vmw_resource_reserve(res, false, true);
  1492. WARN_ON(ret);
  1493. WARN_ON(res->pin_count == 0);
  1494. if (--res->pin_count == 0 && res->backup) {
  1495. struct vmw_dma_buffer *vbo = res->backup;
  1496. ttm_bo_reserve(&vbo->base, false, false, NULL);
  1497. vmw_bo_pin_reserved(vbo, false);
  1498. ttm_bo_unreserve(&vbo->base);
  1499. }
  1500. vmw_resource_unreserve(res, false, NULL, 0UL);
  1501. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1502. ttm_read_unlock(&dev_priv->reservation_sem);
  1503. }
  1504. /**
  1505. * vmw_res_type - Return the resource type
  1506. *
  1507. * @res: Pointer to the resource
  1508. */
  1509. enum vmw_res_type vmw_res_type(const struct vmw_resource *res)
  1510. {
  1511. return res->func->res_type;
  1512. }