vmwgfx_resource.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. /**************************************************************************
  2. *
  3. * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. #include "vmwgfx_drv.h"
  28. #include <drm/vmwgfx_drm.h>
  29. #include <drm/ttm/ttm_object.h>
  30. #include <drm/ttm/ttm_placement.h>
  31. #include <drm/drmP.h>
  32. #include "vmwgfx_resource_priv.h"
  33. struct vmw_user_dma_buffer {
  34. struct ttm_base_object base;
  35. struct vmw_dma_buffer dma;
  36. };
  37. struct vmw_bo_user_rep {
  38. uint32_t handle;
  39. uint64_t map_handle;
  40. };
  41. struct vmw_stream {
  42. struct vmw_resource res;
  43. uint32_t stream_id;
  44. };
  45. struct vmw_user_stream {
  46. struct ttm_base_object base;
  47. struct vmw_stream stream;
  48. };
  49. static uint64_t vmw_user_stream_size;
  50. static const struct vmw_res_func vmw_stream_func = {
  51. .res_type = vmw_res_stream,
  52. .needs_backup = false,
  53. .may_evict = false,
  54. .type_name = "video streams",
  55. .backup_placement = NULL,
  56. .create = NULL,
  57. .destroy = NULL,
  58. .bind = NULL,
  59. .unbind = NULL
  60. };
  61. static inline struct vmw_dma_buffer *
  62. vmw_dma_buffer(struct ttm_buffer_object *bo)
  63. {
  64. return container_of(bo, struct vmw_dma_buffer, base);
  65. }
  66. static inline struct vmw_user_dma_buffer *
  67. vmw_user_dma_buffer(struct ttm_buffer_object *bo)
  68. {
  69. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  70. return container_of(vmw_bo, struct vmw_user_dma_buffer, dma);
  71. }
  72. struct vmw_resource *vmw_resource_reference(struct vmw_resource *res)
  73. {
  74. kref_get(&res->kref);
  75. return res;
  76. }
  77. /**
  78. * vmw_resource_release_id - release a resource id to the id manager.
  79. *
  80. * @res: Pointer to the resource.
  81. *
  82. * Release the resource id to the resource id manager and set it to -1
  83. */
  84. void vmw_resource_release_id(struct vmw_resource *res)
  85. {
  86. struct vmw_private *dev_priv = res->dev_priv;
  87. struct idr *idr = &dev_priv->res_idr[res->func->res_type];
  88. write_lock(&dev_priv->resource_lock);
  89. if (res->id != -1)
  90. idr_remove(idr, res->id);
  91. res->id = -1;
  92. write_unlock(&dev_priv->resource_lock);
  93. }
  94. static void vmw_resource_release(struct kref *kref)
  95. {
  96. struct vmw_resource *res =
  97. container_of(kref, struct vmw_resource, kref);
  98. struct vmw_private *dev_priv = res->dev_priv;
  99. int id;
  100. struct idr *idr = &dev_priv->res_idr[res->func->res_type];
  101. res->avail = false;
  102. list_del_init(&res->lru_head);
  103. write_unlock(&dev_priv->resource_lock);
  104. if (res->backup) {
  105. struct ttm_buffer_object *bo = &res->backup->base;
  106. ttm_bo_reserve(bo, false, false, false, 0);
  107. if (!list_empty(&res->mob_head) &&
  108. res->func->unbind != NULL) {
  109. struct ttm_validate_buffer val_buf;
  110. val_buf.bo = bo;
  111. res->func->unbind(res, false, &val_buf);
  112. }
  113. res->backup_dirty = false;
  114. list_del_init(&res->mob_head);
  115. ttm_bo_unreserve(bo);
  116. vmw_dmabuf_unreference(&res->backup);
  117. }
  118. if (likely(res->hw_destroy != NULL))
  119. res->hw_destroy(res);
  120. id = res->id;
  121. if (res->res_free != NULL)
  122. res->res_free(res);
  123. else
  124. kfree(res);
  125. write_lock(&dev_priv->resource_lock);
  126. if (id != -1)
  127. idr_remove(idr, id);
  128. }
  129. void vmw_resource_unreference(struct vmw_resource **p_res)
  130. {
  131. struct vmw_resource *res = *p_res;
  132. struct vmw_private *dev_priv = res->dev_priv;
  133. *p_res = NULL;
  134. write_lock(&dev_priv->resource_lock);
  135. kref_put(&res->kref, vmw_resource_release);
  136. write_unlock(&dev_priv->resource_lock);
  137. }
  138. /**
  139. * vmw_resource_alloc_id - release a resource id to the id manager.
  140. *
  141. * @res: Pointer to the resource.
  142. *
  143. * Allocate the lowest free resource from the resource manager, and set
  144. * @res->id to that id. Returns 0 on success and -ENOMEM on failure.
  145. */
  146. int vmw_resource_alloc_id(struct vmw_resource *res)
  147. {
  148. struct vmw_private *dev_priv = res->dev_priv;
  149. int ret;
  150. struct idr *idr = &dev_priv->res_idr[res->func->res_type];
  151. BUG_ON(res->id != -1);
  152. idr_preload(GFP_KERNEL);
  153. write_lock(&dev_priv->resource_lock);
  154. ret = idr_alloc(idr, res, 1, 0, GFP_NOWAIT);
  155. if (ret >= 0)
  156. res->id = ret;
  157. write_unlock(&dev_priv->resource_lock);
  158. idr_preload_end();
  159. return ret < 0 ? ret : 0;
  160. }
  161. /**
  162. * vmw_resource_init - initialize a struct vmw_resource
  163. *
  164. * @dev_priv: Pointer to a device private struct.
  165. * @res: The struct vmw_resource to initialize.
  166. * @obj_type: Resource object type.
  167. * @delay_id: Boolean whether to defer device id allocation until
  168. * the first validation.
  169. * @res_free: Resource destructor.
  170. * @func: Resource function table.
  171. */
  172. int vmw_resource_init(struct vmw_private *dev_priv, struct vmw_resource *res,
  173. bool delay_id,
  174. void (*res_free) (struct vmw_resource *res),
  175. const struct vmw_res_func *func)
  176. {
  177. kref_init(&res->kref);
  178. res->hw_destroy = NULL;
  179. res->res_free = res_free;
  180. res->avail = false;
  181. res->dev_priv = dev_priv;
  182. res->func = func;
  183. INIT_LIST_HEAD(&res->lru_head);
  184. INIT_LIST_HEAD(&res->mob_head);
  185. res->id = -1;
  186. res->backup = NULL;
  187. res->backup_offset = 0;
  188. res->backup_dirty = false;
  189. res->res_dirty = false;
  190. if (delay_id)
  191. return 0;
  192. else
  193. return vmw_resource_alloc_id(res);
  194. }
  195. /**
  196. * vmw_resource_activate
  197. *
  198. * @res: Pointer to the newly created resource
  199. * @hw_destroy: Destroy function. NULL if none.
  200. *
  201. * Activate a resource after the hardware has been made aware of it.
  202. * Set tye destroy function to @destroy. Typically this frees the
  203. * resource and destroys the hardware resources associated with it.
  204. * Activate basically means that the function vmw_resource_lookup will
  205. * find it.
  206. */
  207. void vmw_resource_activate(struct vmw_resource *res,
  208. void (*hw_destroy) (struct vmw_resource *))
  209. {
  210. struct vmw_private *dev_priv = res->dev_priv;
  211. write_lock(&dev_priv->resource_lock);
  212. res->avail = true;
  213. res->hw_destroy = hw_destroy;
  214. write_unlock(&dev_priv->resource_lock);
  215. }
  216. struct vmw_resource *vmw_resource_lookup(struct vmw_private *dev_priv,
  217. struct idr *idr, int id)
  218. {
  219. struct vmw_resource *res;
  220. read_lock(&dev_priv->resource_lock);
  221. res = idr_find(idr, id);
  222. if (res && res->avail)
  223. kref_get(&res->kref);
  224. else
  225. res = NULL;
  226. read_unlock(&dev_priv->resource_lock);
  227. if (unlikely(res == NULL))
  228. return NULL;
  229. return res;
  230. }
  231. /**
  232. * vmw_user_resource_lookup_handle - lookup a struct resource from a
  233. * TTM user-space handle and perform basic type checks
  234. *
  235. * @dev_priv: Pointer to a device private struct
  236. * @tfile: Pointer to a struct ttm_object_file identifying the caller
  237. * @handle: The TTM user-space handle
  238. * @converter: Pointer to an object describing the resource type
  239. * @p_res: On successful return the location pointed to will contain
  240. * a pointer to a refcounted struct vmw_resource.
  241. *
  242. * If the handle can't be found or is associated with an incorrect resource
  243. * type, -EINVAL will be returned.
  244. */
  245. int vmw_user_resource_lookup_handle(struct vmw_private *dev_priv,
  246. struct ttm_object_file *tfile,
  247. uint32_t handle,
  248. const struct vmw_user_resource_conv
  249. *converter,
  250. struct vmw_resource **p_res)
  251. {
  252. struct ttm_base_object *base;
  253. struct vmw_resource *res;
  254. int ret = -EINVAL;
  255. base = ttm_base_object_lookup(tfile, handle);
  256. if (unlikely(base == NULL))
  257. return -EINVAL;
  258. if (unlikely(base->object_type != converter->object_type))
  259. goto out_bad_resource;
  260. res = converter->base_obj_to_res(base);
  261. read_lock(&dev_priv->resource_lock);
  262. if (!res->avail || res->res_free != converter->res_free) {
  263. read_unlock(&dev_priv->resource_lock);
  264. goto out_bad_resource;
  265. }
  266. kref_get(&res->kref);
  267. read_unlock(&dev_priv->resource_lock);
  268. *p_res = res;
  269. ret = 0;
  270. out_bad_resource:
  271. ttm_base_object_unref(&base);
  272. return ret;
  273. }
  274. /**
  275. * Helper function that looks either a surface or dmabuf.
  276. *
  277. * The pointer this pointed at by out_surf and out_buf needs to be null.
  278. */
  279. int vmw_user_lookup_handle(struct vmw_private *dev_priv,
  280. struct ttm_object_file *tfile,
  281. uint32_t handle,
  282. struct vmw_surface **out_surf,
  283. struct vmw_dma_buffer **out_buf)
  284. {
  285. struct vmw_resource *res;
  286. int ret;
  287. BUG_ON(*out_surf || *out_buf);
  288. ret = vmw_user_resource_lookup_handle(dev_priv, tfile, handle,
  289. user_surface_converter,
  290. &res);
  291. if (!ret) {
  292. *out_surf = vmw_res_to_srf(res);
  293. return 0;
  294. }
  295. *out_surf = NULL;
  296. ret = vmw_user_dmabuf_lookup(tfile, handle, out_buf);
  297. return ret;
  298. }
  299. /**
  300. * Buffer management.
  301. */
  302. void vmw_dmabuf_bo_free(struct ttm_buffer_object *bo)
  303. {
  304. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  305. kfree(vmw_bo);
  306. }
  307. int vmw_dmabuf_init(struct vmw_private *dev_priv,
  308. struct vmw_dma_buffer *vmw_bo,
  309. size_t size, struct ttm_placement *placement,
  310. bool interruptible,
  311. void (*bo_free) (struct ttm_buffer_object *bo))
  312. {
  313. struct ttm_bo_device *bdev = &dev_priv->bdev;
  314. size_t acc_size;
  315. int ret;
  316. BUG_ON(!bo_free);
  317. acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct vmw_dma_buffer));
  318. memset(vmw_bo, 0, sizeof(*vmw_bo));
  319. INIT_LIST_HEAD(&vmw_bo->res_list);
  320. ret = ttm_bo_init(bdev, &vmw_bo->base, size,
  321. ttm_bo_type_device, placement,
  322. 0, interruptible,
  323. NULL, acc_size, NULL, bo_free);
  324. return ret;
  325. }
  326. static void vmw_user_dmabuf_destroy(struct ttm_buffer_object *bo)
  327. {
  328. struct vmw_user_dma_buffer *vmw_user_bo = vmw_user_dma_buffer(bo);
  329. ttm_base_object_kfree(vmw_user_bo, base);
  330. }
  331. static void vmw_user_dmabuf_release(struct ttm_base_object **p_base)
  332. {
  333. struct vmw_user_dma_buffer *vmw_user_bo;
  334. struct ttm_base_object *base = *p_base;
  335. struct ttm_buffer_object *bo;
  336. *p_base = NULL;
  337. if (unlikely(base == NULL))
  338. return;
  339. vmw_user_bo = container_of(base, struct vmw_user_dma_buffer, base);
  340. bo = &vmw_user_bo->dma.base;
  341. ttm_bo_unref(&bo);
  342. }
  343. /**
  344. * vmw_user_dmabuf_alloc - Allocate a user dma buffer
  345. *
  346. * @dev_priv: Pointer to a struct device private.
  347. * @tfile: Pointer to a struct ttm_object_file on which to register the user
  348. * object.
  349. * @size: Size of the dma buffer.
  350. * @shareable: Boolean whether the buffer is shareable with other open files.
  351. * @handle: Pointer to where the handle value should be assigned.
  352. * @p_dma_buf: Pointer to where the refcounted struct vmw_dma_buffer pointer
  353. * should be assigned.
  354. */
  355. int vmw_user_dmabuf_alloc(struct vmw_private *dev_priv,
  356. struct ttm_object_file *tfile,
  357. uint32_t size,
  358. bool shareable,
  359. uint32_t *handle,
  360. struct vmw_dma_buffer **p_dma_buf)
  361. {
  362. struct vmw_user_dma_buffer *user_bo;
  363. struct ttm_buffer_object *tmp;
  364. int ret;
  365. user_bo = kzalloc(sizeof(*user_bo), GFP_KERNEL);
  366. if (unlikely(user_bo == NULL)) {
  367. DRM_ERROR("Failed to allocate a buffer.\n");
  368. return -ENOMEM;
  369. }
  370. ret = vmw_dmabuf_init(dev_priv, &user_bo->dma, size,
  371. &vmw_vram_sys_placement, true,
  372. &vmw_user_dmabuf_destroy);
  373. if (unlikely(ret != 0))
  374. return ret;
  375. tmp = ttm_bo_reference(&user_bo->dma.base);
  376. ret = ttm_base_object_init(tfile,
  377. &user_bo->base,
  378. shareable,
  379. ttm_buffer_type,
  380. &vmw_user_dmabuf_release, NULL);
  381. if (unlikely(ret != 0)) {
  382. ttm_bo_unref(&tmp);
  383. goto out_no_base_object;
  384. }
  385. *p_dma_buf = &user_bo->dma;
  386. *handle = user_bo->base.hash.key;
  387. out_no_base_object:
  388. return ret;
  389. }
  390. /**
  391. * vmw_user_dmabuf_verify_access - verify access permissions on this
  392. * buffer object.
  393. *
  394. * @bo: Pointer to the buffer object being accessed
  395. * @tfile: Identifying the caller.
  396. */
  397. int vmw_user_dmabuf_verify_access(struct ttm_buffer_object *bo,
  398. struct ttm_object_file *tfile)
  399. {
  400. struct vmw_user_dma_buffer *vmw_user_bo;
  401. if (unlikely(bo->destroy != vmw_user_dmabuf_destroy))
  402. return -EPERM;
  403. vmw_user_bo = vmw_user_dma_buffer(bo);
  404. return (vmw_user_bo->base.tfile == tfile ||
  405. vmw_user_bo->base.shareable) ? 0 : -EPERM;
  406. }
  407. int vmw_dmabuf_alloc_ioctl(struct drm_device *dev, void *data,
  408. struct drm_file *file_priv)
  409. {
  410. struct vmw_private *dev_priv = vmw_priv(dev);
  411. union drm_vmw_alloc_dmabuf_arg *arg =
  412. (union drm_vmw_alloc_dmabuf_arg *)data;
  413. struct drm_vmw_alloc_dmabuf_req *req = &arg->req;
  414. struct drm_vmw_dmabuf_rep *rep = &arg->rep;
  415. struct vmw_dma_buffer *dma_buf;
  416. uint32_t handle;
  417. struct vmw_master *vmaster = vmw_master(file_priv->master);
  418. int ret;
  419. ret = ttm_read_lock(&vmaster->lock, true);
  420. if (unlikely(ret != 0))
  421. return ret;
  422. ret = vmw_user_dmabuf_alloc(dev_priv, vmw_fpriv(file_priv)->tfile,
  423. req->size, false, &handle, &dma_buf);
  424. if (unlikely(ret != 0))
  425. goto out_no_dmabuf;
  426. rep->handle = handle;
  427. rep->map_handle = dma_buf->base.addr_space_offset;
  428. rep->cur_gmr_id = handle;
  429. rep->cur_gmr_offset = 0;
  430. vmw_dmabuf_unreference(&dma_buf);
  431. out_no_dmabuf:
  432. ttm_read_unlock(&vmaster->lock);
  433. return ret;
  434. }
  435. int vmw_dmabuf_unref_ioctl(struct drm_device *dev, void *data,
  436. struct drm_file *file_priv)
  437. {
  438. struct drm_vmw_unref_dmabuf_arg *arg =
  439. (struct drm_vmw_unref_dmabuf_arg *)data;
  440. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  441. arg->handle,
  442. TTM_REF_USAGE);
  443. }
  444. int vmw_user_dmabuf_lookup(struct ttm_object_file *tfile,
  445. uint32_t handle, struct vmw_dma_buffer **out)
  446. {
  447. struct vmw_user_dma_buffer *vmw_user_bo;
  448. struct ttm_base_object *base;
  449. base = ttm_base_object_lookup(tfile, handle);
  450. if (unlikely(base == NULL)) {
  451. printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
  452. (unsigned long)handle);
  453. return -ESRCH;
  454. }
  455. if (unlikely(base->object_type != ttm_buffer_type)) {
  456. ttm_base_object_unref(&base);
  457. printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
  458. (unsigned long)handle);
  459. return -EINVAL;
  460. }
  461. vmw_user_bo = container_of(base, struct vmw_user_dma_buffer, base);
  462. (void)ttm_bo_reference(&vmw_user_bo->dma.base);
  463. ttm_base_object_unref(&base);
  464. *out = &vmw_user_bo->dma;
  465. return 0;
  466. }
  467. int vmw_user_dmabuf_reference(struct ttm_object_file *tfile,
  468. struct vmw_dma_buffer *dma_buf)
  469. {
  470. struct vmw_user_dma_buffer *user_bo;
  471. if (dma_buf->base.destroy != vmw_user_dmabuf_destroy)
  472. return -EINVAL;
  473. user_bo = container_of(dma_buf, struct vmw_user_dma_buffer, dma);
  474. return ttm_ref_object_add(tfile, &user_bo->base, TTM_REF_USAGE, NULL);
  475. }
  476. /*
  477. * Stream management
  478. */
  479. static void vmw_stream_destroy(struct vmw_resource *res)
  480. {
  481. struct vmw_private *dev_priv = res->dev_priv;
  482. struct vmw_stream *stream;
  483. int ret;
  484. DRM_INFO("%s: unref\n", __func__);
  485. stream = container_of(res, struct vmw_stream, res);
  486. ret = vmw_overlay_unref(dev_priv, stream->stream_id);
  487. WARN_ON(ret != 0);
  488. }
  489. static int vmw_stream_init(struct vmw_private *dev_priv,
  490. struct vmw_stream *stream,
  491. void (*res_free) (struct vmw_resource *res))
  492. {
  493. struct vmw_resource *res = &stream->res;
  494. int ret;
  495. ret = vmw_resource_init(dev_priv, res, false, res_free,
  496. &vmw_stream_func);
  497. if (unlikely(ret != 0)) {
  498. if (res_free == NULL)
  499. kfree(stream);
  500. else
  501. res_free(&stream->res);
  502. return ret;
  503. }
  504. ret = vmw_overlay_claim(dev_priv, &stream->stream_id);
  505. if (ret) {
  506. vmw_resource_unreference(&res);
  507. return ret;
  508. }
  509. DRM_INFO("%s: claimed\n", __func__);
  510. vmw_resource_activate(&stream->res, vmw_stream_destroy);
  511. return 0;
  512. }
  513. static void vmw_user_stream_free(struct vmw_resource *res)
  514. {
  515. struct vmw_user_stream *stream =
  516. container_of(res, struct vmw_user_stream, stream.res);
  517. struct vmw_private *dev_priv = res->dev_priv;
  518. ttm_base_object_kfree(stream, base);
  519. ttm_mem_global_free(vmw_mem_glob(dev_priv),
  520. vmw_user_stream_size);
  521. }
  522. /**
  523. * This function is called when user space has no more references on the
  524. * base object. It releases the base-object's reference on the resource object.
  525. */
  526. static void vmw_user_stream_base_release(struct ttm_base_object **p_base)
  527. {
  528. struct ttm_base_object *base = *p_base;
  529. struct vmw_user_stream *stream =
  530. container_of(base, struct vmw_user_stream, base);
  531. struct vmw_resource *res = &stream->stream.res;
  532. *p_base = NULL;
  533. vmw_resource_unreference(&res);
  534. }
  535. int vmw_stream_unref_ioctl(struct drm_device *dev, void *data,
  536. struct drm_file *file_priv)
  537. {
  538. struct vmw_private *dev_priv = vmw_priv(dev);
  539. struct vmw_resource *res;
  540. struct vmw_user_stream *stream;
  541. struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
  542. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  543. struct idr *idr = &dev_priv->res_idr[vmw_res_stream];
  544. int ret = 0;
  545. res = vmw_resource_lookup(dev_priv, idr, arg->stream_id);
  546. if (unlikely(res == NULL))
  547. return -EINVAL;
  548. if (res->res_free != &vmw_user_stream_free) {
  549. ret = -EINVAL;
  550. goto out;
  551. }
  552. stream = container_of(res, struct vmw_user_stream, stream.res);
  553. if (stream->base.tfile != tfile) {
  554. ret = -EINVAL;
  555. goto out;
  556. }
  557. ttm_ref_object_base_unref(tfile, stream->base.hash.key, TTM_REF_USAGE);
  558. out:
  559. vmw_resource_unreference(&res);
  560. return ret;
  561. }
  562. int vmw_stream_claim_ioctl(struct drm_device *dev, void *data,
  563. struct drm_file *file_priv)
  564. {
  565. struct vmw_private *dev_priv = vmw_priv(dev);
  566. struct vmw_user_stream *stream;
  567. struct vmw_resource *res;
  568. struct vmw_resource *tmp;
  569. struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
  570. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  571. struct vmw_master *vmaster = vmw_master(file_priv->master);
  572. int ret;
  573. /*
  574. * Approximate idr memory usage with 128 bytes. It will be limited
  575. * by maximum number_of streams anyway?
  576. */
  577. if (unlikely(vmw_user_stream_size == 0))
  578. vmw_user_stream_size = ttm_round_pot(sizeof(*stream)) + 128;
  579. ret = ttm_read_lock(&vmaster->lock, true);
  580. if (unlikely(ret != 0))
  581. return ret;
  582. ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
  583. vmw_user_stream_size,
  584. false, true);
  585. if (unlikely(ret != 0)) {
  586. if (ret != -ERESTARTSYS)
  587. DRM_ERROR("Out of graphics memory for stream"
  588. " creation.\n");
  589. goto out_unlock;
  590. }
  591. stream = kmalloc(sizeof(*stream), GFP_KERNEL);
  592. if (unlikely(stream == NULL)) {
  593. ttm_mem_global_free(vmw_mem_glob(dev_priv),
  594. vmw_user_stream_size);
  595. ret = -ENOMEM;
  596. goto out_unlock;
  597. }
  598. res = &stream->stream.res;
  599. stream->base.shareable = false;
  600. stream->base.tfile = NULL;
  601. /*
  602. * From here on, the destructor takes over resource freeing.
  603. */
  604. ret = vmw_stream_init(dev_priv, &stream->stream, vmw_user_stream_free);
  605. if (unlikely(ret != 0))
  606. goto out_unlock;
  607. tmp = vmw_resource_reference(res);
  608. ret = ttm_base_object_init(tfile, &stream->base, false, VMW_RES_STREAM,
  609. &vmw_user_stream_base_release, NULL);
  610. if (unlikely(ret != 0)) {
  611. vmw_resource_unreference(&tmp);
  612. goto out_err;
  613. }
  614. arg->stream_id = res->id;
  615. out_err:
  616. vmw_resource_unreference(&res);
  617. out_unlock:
  618. ttm_read_unlock(&vmaster->lock);
  619. return ret;
  620. }
  621. int vmw_user_stream_lookup(struct vmw_private *dev_priv,
  622. struct ttm_object_file *tfile,
  623. uint32_t *inout_id, struct vmw_resource **out)
  624. {
  625. struct vmw_user_stream *stream;
  626. struct vmw_resource *res;
  627. int ret;
  628. res = vmw_resource_lookup(dev_priv, &dev_priv->res_idr[vmw_res_stream],
  629. *inout_id);
  630. if (unlikely(res == NULL))
  631. return -EINVAL;
  632. if (res->res_free != &vmw_user_stream_free) {
  633. ret = -EINVAL;
  634. goto err_ref;
  635. }
  636. stream = container_of(res, struct vmw_user_stream, stream.res);
  637. if (stream->base.tfile != tfile) {
  638. ret = -EPERM;
  639. goto err_ref;
  640. }
  641. *inout_id = stream->stream.stream_id;
  642. *out = res;
  643. return 0;
  644. err_ref:
  645. vmw_resource_unreference(&res);
  646. return ret;
  647. }
  648. int vmw_dumb_create(struct drm_file *file_priv,
  649. struct drm_device *dev,
  650. struct drm_mode_create_dumb *args)
  651. {
  652. struct vmw_private *dev_priv = vmw_priv(dev);
  653. struct vmw_master *vmaster = vmw_master(file_priv->master);
  654. struct vmw_user_dma_buffer *vmw_user_bo;
  655. struct ttm_buffer_object *tmp;
  656. int ret;
  657. args->pitch = args->width * ((args->bpp + 7) / 8);
  658. args->size = args->pitch * args->height;
  659. vmw_user_bo = kzalloc(sizeof(*vmw_user_bo), GFP_KERNEL);
  660. if (vmw_user_bo == NULL)
  661. return -ENOMEM;
  662. ret = ttm_read_lock(&vmaster->lock, true);
  663. if (ret != 0) {
  664. kfree(vmw_user_bo);
  665. return ret;
  666. }
  667. ret = vmw_dmabuf_init(dev_priv, &vmw_user_bo->dma, args->size,
  668. &vmw_vram_sys_placement, true,
  669. &vmw_user_dmabuf_destroy);
  670. if (ret != 0)
  671. goto out_no_dmabuf;
  672. tmp = ttm_bo_reference(&vmw_user_bo->dma.base);
  673. ret = ttm_base_object_init(vmw_fpriv(file_priv)->tfile,
  674. &vmw_user_bo->base,
  675. false,
  676. ttm_buffer_type,
  677. &vmw_user_dmabuf_release, NULL);
  678. if (unlikely(ret != 0))
  679. goto out_no_base_object;
  680. args->handle = vmw_user_bo->base.hash.key;
  681. out_no_base_object:
  682. ttm_bo_unref(&tmp);
  683. out_no_dmabuf:
  684. ttm_read_unlock(&vmaster->lock);
  685. return ret;
  686. }
  687. int vmw_dumb_map_offset(struct drm_file *file_priv,
  688. struct drm_device *dev, uint32_t handle,
  689. uint64_t *offset)
  690. {
  691. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  692. struct vmw_dma_buffer *out_buf;
  693. int ret;
  694. ret = vmw_user_dmabuf_lookup(tfile, handle, &out_buf);
  695. if (ret != 0)
  696. return -EINVAL;
  697. *offset = out_buf->base.addr_space_offset;
  698. vmw_dmabuf_unreference(&out_buf);
  699. return 0;
  700. }
  701. int vmw_dumb_destroy(struct drm_file *file_priv,
  702. struct drm_device *dev,
  703. uint32_t handle)
  704. {
  705. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  706. handle, TTM_REF_USAGE);
  707. }
  708. /**
  709. * vmw_resource_buf_alloc - Allocate a backup buffer for a resource.
  710. *
  711. * @res: The resource for which to allocate a backup buffer.
  712. * @interruptible: Whether any sleeps during allocation should be
  713. * performed while interruptible.
  714. */
  715. static int vmw_resource_buf_alloc(struct vmw_resource *res,
  716. bool interruptible)
  717. {
  718. unsigned long size =
  719. (res->backup_size + PAGE_SIZE - 1) & PAGE_MASK;
  720. struct vmw_dma_buffer *backup;
  721. int ret;
  722. if (likely(res->backup)) {
  723. BUG_ON(res->backup->base.num_pages * PAGE_SIZE < size);
  724. return 0;
  725. }
  726. backup = kzalloc(sizeof(*backup), GFP_KERNEL);
  727. if (unlikely(backup == NULL))
  728. return -ENOMEM;
  729. ret = vmw_dmabuf_init(res->dev_priv, backup, res->backup_size,
  730. res->func->backup_placement,
  731. interruptible,
  732. &vmw_dmabuf_bo_free);
  733. if (unlikely(ret != 0))
  734. goto out_no_dmabuf;
  735. res->backup = backup;
  736. out_no_dmabuf:
  737. return ret;
  738. }
  739. /**
  740. * vmw_resource_do_validate - Make a resource up-to-date and visible
  741. * to the device.
  742. *
  743. * @res: The resource to make visible to the device.
  744. * @val_buf: Information about a buffer possibly
  745. * containing backup data if a bind operation is needed.
  746. *
  747. * On hardware resource shortage, this function returns -EBUSY and
  748. * should be retried once resources have been freed up.
  749. */
  750. static int vmw_resource_do_validate(struct vmw_resource *res,
  751. struct ttm_validate_buffer *val_buf)
  752. {
  753. int ret = 0;
  754. const struct vmw_res_func *func = res->func;
  755. if (unlikely(res->id == -1)) {
  756. ret = func->create(res);
  757. if (unlikely(ret != 0))
  758. return ret;
  759. }
  760. if (func->bind &&
  761. ((func->needs_backup && list_empty(&res->mob_head) &&
  762. val_buf->bo != NULL) ||
  763. (!func->needs_backup && val_buf->bo != NULL))) {
  764. ret = func->bind(res, val_buf);
  765. if (unlikely(ret != 0))
  766. goto out_bind_failed;
  767. if (func->needs_backup)
  768. list_add_tail(&res->mob_head, &res->backup->res_list);
  769. }
  770. /*
  771. * Only do this on write operations, and move to
  772. * vmw_resource_unreserve if it can be called after
  773. * backup buffers have been unreserved. Otherwise
  774. * sort out locking.
  775. */
  776. res->res_dirty = true;
  777. return 0;
  778. out_bind_failed:
  779. func->destroy(res);
  780. return ret;
  781. }
  782. /**
  783. * vmw_resource_unreserve - Unreserve a resource previously reserved for
  784. * command submission.
  785. *
  786. * @res: Pointer to the struct vmw_resource to unreserve.
  787. * @new_backup: Pointer to new backup buffer if command submission
  788. * switched.
  789. * @new_backup_offset: New backup offset if @new_backup is !NULL.
  790. *
  791. * Currently unreserving a resource means putting it back on the device's
  792. * resource lru list, so that it can be evicted if necessary.
  793. */
  794. void vmw_resource_unreserve(struct vmw_resource *res,
  795. struct vmw_dma_buffer *new_backup,
  796. unsigned long new_backup_offset)
  797. {
  798. struct vmw_private *dev_priv = res->dev_priv;
  799. if (!list_empty(&res->lru_head))
  800. return;
  801. if (new_backup && new_backup != res->backup) {
  802. if (res->backup) {
  803. lockdep_assert_held(&res->backup->base.resv->lock.base);
  804. list_del_init(&res->mob_head);
  805. vmw_dmabuf_unreference(&res->backup);
  806. }
  807. res->backup = vmw_dmabuf_reference(new_backup);
  808. lockdep_assert_held(&new_backup->base.resv->lock.base);
  809. list_add_tail(&res->mob_head, &new_backup->res_list);
  810. }
  811. if (new_backup)
  812. res->backup_offset = new_backup_offset;
  813. if (!res->func->may_evict)
  814. return;
  815. write_lock(&dev_priv->resource_lock);
  816. list_add_tail(&res->lru_head,
  817. &res->dev_priv->res_lru[res->func->res_type]);
  818. write_unlock(&dev_priv->resource_lock);
  819. }
  820. /**
  821. * vmw_resource_check_buffer - Check whether a backup buffer is needed
  822. * for a resource and in that case, allocate
  823. * one, reserve and validate it.
  824. *
  825. * @res: The resource for which to allocate a backup buffer.
  826. * @interruptible: Whether any sleeps during allocation should be
  827. * performed while interruptible.
  828. * @val_buf: On successful return contains data about the
  829. * reserved and validated backup buffer.
  830. */
  831. static int
  832. vmw_resource_check_buffer(struct vmw_resource *res,
  833. struct ww_acquire_ctx *ticket,
  834. bool interruptible,
  835. struct ttm_validate_buffer *val_buf)
  836. {
  837. struct list_head val_list;
  838. bool backup_dirty = false;
  839. int ret;
  840. if (unlikely(res->backup == NULL)) {
  841. ret = vmw_resource_buf_alloc(res, interruptible);
  842. if (unlikely(ret != 0))
  843. return ret;
  844. }
  845. INIT_LIST_HEAD(&val_list);
  846. val_buf->bo = ttm_bo_reference(&res->backup->base);
  847. list_add_tail(&val_buf->head, &val_list);
  848. ret = ttm_eu_reserve_buffers(ticket, &val_list);
  849. if (unlikely(ret != 0))
  850. goto out_no_reserve;
  851. if (res->func->needs_backup && list_empty(&res->mob_head))
  852. return 0;
  853. backup_dirty = res->backup_dirty;
  854. ret = ttm_bo_validate(&res->backup->base,
  855. res->func->backup_placement,
  856. true, false);
  857. if (unlikely(ret != 0))
  858. goto out_no_validate;
  859. return 0;
  860. out_no_validate:
  861. ttm_eu_backoff_reservation(ticket, &val_list);
  862. out_no_reserve:
  863. ttm_bo_unref(&val_buf->bo);
  864. if (backup_dirty)
  865. vmw_dmabuf_unreference(&res->backup);
  866. return ret;
  867. }
  868. /**
  869. * vmw_resource_reserve - Reserve a resource for command submission
  870. *
  871. * @res: The resource to reserve.
  872. *
  873. * This function takes the resource off the LRU list and make sure
  874. * a backup buffer is present for guest-backed resources. However,
  875. * the buffer may not be bound to the resource at this point.
  876. *
  877. */
  878. int vmw_resource_reserve(struct vmw_resource *res, bool no_backup)
  879. {
  880. struct vmw_private *dev_priv = res->dev_priv;
  881. int ret;
  882. write_lock(&dev_priv->resource_lock);
  883. list_del_init(&res->lru_head);
  884. write_unlock(&dev_priv->resource_lock);
  885. if (res->func->needs_backup && res->backup == NULL &&
  886. !no_backup) {
  887. ret = vmw_resource_buf_alloc(res, true);
  888. if (unlikely(ret != 0))
  889. return ret;
  890. }
  891. return 0;
  892. }
  893. /**
  894. * vmw_resource_backoff_reservation - Unreserve and unreference a
  895. * backup buffer
  896. *.
  897. * @val_buf: Backup buffer information.
  898. */
  899. static void
  900. vmw_resource_backoff_reservation(struct ww_acquire_ctx *ticket,
  901. struct ttm_validate_buffer *val_buf)
  902. {
  903. struct list_head val_list;
  904. if (likely(val_buf->bo == NULL))
  905. return;
  906. INIT_LIST_HEAD(&val_list);
  907. list_add_tail(&val_buf->head, &val_list);
  908. ttm_eu_backoff_reservation(ticket, &val_list);
  909. ttm_bo_unref(&val_buf->bo);
  910. }
  911. /**
  912. * vmw_resource_do_evict - Evict a resource, and transfer its data
  913. * to a backup buffer.
  914. *
  915. * @res: The resource to evict.
  916. */
  917. int vmw_resource_do_evict(struct vmw_resource *res)
  918. {
  919. struct ttm_validate_buffer val_buf;
  920. const struct vmw_res_func *func = res->func;
  921. struct ww_acquire_ctx ticket;
  922. int ret;
  923. BUG_ON(!func->may_evict);
  924. val_buf.bo = NULL;
  925. ret = vmw_resource_check_buffer(res, &ticket, true, &val_buf);
  926. if (unlikely(ret != 0))
  927. return ret;
  928. if (unlikely(func->unbind != NULL &&
  929. (!func->needs_backup || !list_empty(&res->mob_head)))) {
  930. ret = func->unbind(res, res->res_dirty, &val_buf);
  931. if (unlikely(ret != 0))
  932. goto out_no_unbind;
  933. list_del_init(&res->mob_head);
  934. }
  935. ret = func->destroy(res);
  936. res->backup_dirty = true;
  937. res->res_dirty = false;
  938. out_no_unbind:
  939. vmw_resource_backoff_reservation(&ticket, &val_buf);
  940. return ret;
  941. }
  942. /**
  943. * vmw_resource_validate - Make a resource up-to-date and visible
  944. * to the device.
  945. *
  946. * @res: The resource to make visible to the device.
  947. *
  948. * On succesful return, any backup DMA buffer pointed to by @res->backup will
  949. * be reserved and validated.
  950. * On hardware resource shortage, this function will repeatedly evict
  951. * resources of the same type until the validation succeeds.
  952. */
  953. int vmw_resource_validate(struct vmw_resource *res)
  954. {
  955. int ret;
  956. struct vmw_resource *evict_res;
  957. struct vmw_private *dev_priv = res->dev_priv;
  958. struct list_head *lru_list = &dev_priv->res_lru[res->func->res_type];
  959. struct ttm_validate_buffer val_buf;
  960. if (likely(!res->func->may_evict))
  961. return 0;
  962. val_buf.bo = NULL;
  963. if (res->backup)
  964. val_buf.bo = &res->backup->base;
  965. do {
  966. ret = vmw_resource_do_validate(res, &val_buf);
  967. if (likely(ret != -EBUSY))
  968. break;
  969. write_lock(&dev_priv->resource_lock);
  970. if (list_empty(lru_list) || !res->func->may_evict) {
  971. DRM_ERROR("Out of device device id entries "
  972. "for %s.\n", res->func->type_name);
  973. ret = -EBUSY;
  974. write_unlock(&dev_priv->resource_lock);
  975. break;
  976. }
  977. evict_res = vmw_resource_reference
  978. (list_first_entry(lru_list, struct vmw_resource,
  979. lru_head));
  980. list_del_init(&evict_res->lru_head);
  981. write_unlock(&dev_priv->resource_lock);
  982. vmw_resource_do_evict(evict_res);
  983. vmw_resource_unreference(&evict_res);
  984. } while (1);
  985. if (unlikely(ret != 0))
  986. goto out_no_validate;
  987. else if (!res->func->needs_backup && res->backup) {
  988. list_del_init(&res->mob_head);
  989. vmw_dmabuf_unreference(&res->backup);
  990. }
  991. return 0;
  992. out_no_validate:
  993. return ret;
  994. }
  995. /**
  996. * vmw_fence_single_bo - Utility function to fence a single TTM buffer
  997. * object without unreserving it.
  998. *
  999. * @bo: Pointer to the struct ttm_buffer_object to fence.
  1000. * @fence: Pointer to the fence. If NULL, this function will
  1001. * insert a fence into the command stream..
  1002. *
  1003. * Contrary to the ttm_eu version of this function, it takes only
  1004. * a single buffer object instead of a list, and it also doesn't
  1005. * unreserve the buffer object, which needs to be done separately.
  1006. */
  1007. void vmw_fence_single_bo(struct ttm_buffer_object *bo,
  1008. struct vmw_fence_obj *fence)
  1009. {
  1010. struct ttm_bo_device *bdev = bo->bdev;
  1011. struct ttm_bo_driver *driver = bdev->driver;
  1012. struct vmw_fence_obj *old_fence_obj;
  1013. struct vmw_private *dev_priv =
  1014. container_of(bdev, struct vmw_private, bdev);
  1015. if (fence == NULL)
  1016. vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
  1017. else
  1018. driver->sync_obj_ref(fence);
  1019. spin_lock(&bdev->fence_lock);
  1020. old_fence_obj = bo->sync_obj;
  1021. bo->sync_obj = fence;
  1022. spin_unlock(&bdev->fence_lock);
  1023. if (old_fence_obj)
  1024. vmw_fence_obj_unreference(&old_fence_obj);
  1025. }
  1026. /**
  1027. * vmw_resource_move_notify - TTM move_notify_callback
  1028. *
  1029. * @bo: The TTM buffer object about to move.
  1030. * @mem: The truct ttm_mem_reg indicating to what memory
  1031. * region the move is taking place.
  1032. *
  1033. * For now does nothing.
  1034. */
  1035. void vmw_resource_move_notify(struct ttm_buffer_object *bo,
  1036. struct ttm_mem_reg *mem)
  1037. {
  1038. }
  1039. /**
  1040. * vmw_resource_needs_backup - Return whether a resource needs a backup buffer.
  1041. *
  1042. * @res: The resource being queried.
  1043. */
  1044. bool vmw_resource_needs_backup(const struct vmw_resource *res)
  1045. {
  1046. return res->func->needs_backup;
  1047. }
  1048. /**
  1049. * vmw_resource_evict_type - Evict all resources of a specific type
  1050. *
  1051. * @dev_priv: Pointer to a device private struct
  1052. * @type: The resource type to evict
  1053. *
  1054. * To avoid thrashing starvation or as part of the hibernation sequence,
  1055. * evict all evictable resources of a specific type.
  1056. */
  1057. static void vmw_resource_evict_type(struct vmw_private *dev_priv,
  1058. enum vmw_res_type type)
  1059. {
  1060. struct list_head *lru_list = &dev_priv->res_lru[type];
  1061. struct vmw_resource *evict_res;
  1062. do {
  1063. write_lock(&dev_priv->resource_lock);
  1064. if (list_empty(lru_list))
  1065. goto out_unlock;
  1066. evict_res = vmw_resource_reference(
  1067. list_first_entry(lru_list, struct vmw_resource,
  1068. lru_head));
  1069. list_del_init(&evict_res->lru_head);
  1070. write_unlock(&dev_priv->resource_lock);
  1071. vmw_resource_do_evict(evict_res);
  1072. vmw_resource_unreference(&evict_res);
  1073. } while (1);
  1074. out_unlock:
  1075. write_unlock(&dev_priv->resource_lock);
  1076. }
  1077. /**
  1078. * vmw_resource_evict_all - Evict all evictable resources
  1079. *
  1080. * @dev_priv: Pointer to a device private struct
  1081. *
  1082. * To avoid thrashing starvation or as part of the hibernation sequence,
  1083. * evict all evictable resources. In particular this means that all
  1084. * guest-backed resources that are registered with the device are
  1085. * evicted and the OTable becomes clean.
  1086. */
  1087. void vmw_resource_evict_all(struct vmw_private *dev_priv)
  1088. {
  1089. enum vmw_res_type type;
  1090. mutex_lock(&dev_priv->cmdbuf_mutex);
  1091. for (type = 0; type < vmw_res_max; ++type)
  1092. vmw_resource_evict_type(dev_priv, type);
  1093. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1094. }