vmwgfx_resource.c 47 KB

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