vmwgfx_resource.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993
  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 "vmwgfx_drm.h"
  29. #include "ttm/ttm_object.h"
  30. #include "ttm/ttm_placement.h"
  31. #include "drmP.h"
  32. struct vmw_user_context {
  33. struct ttm_base_object base;
  34. struct vmw_resource res;
  35. };
  36. struct vmw_user_surface {
  37. struct ttm_base_object base;
  38. struct vmw_surface srf;
  39. uint32_t size;
  40. };
  41. struct vmw_user_dma_buffer {
  42. struct ttm_base_object base;
  43. struct vmw_dma_buffer dma;
  44. };
  45. struct vmw_bo_user_rep {
  46. uint32_t handle;
  47. uint64_t map_handle;
  48. };
  49. struct vmw_stream {
  50. struct vmw_resource res;
  51. uint32_t stream_id;
  52. };
  53. struct vmw_user_stream {
  54. struct ttm_base_object base;
  55. struct vmw_stream stream;
  56. };
  57. struct vmw_surface_offset {
  58. uint32_t face;
  59. uint32_t mip;
  60. uint32_t bo_offset;
  61. };
  62. static uint64_t vmw_user_context_size;
  63. static uint64_t vmw_user_surface_size;
  64. static uint64_t vmw_user_stream_size;
  65. static inline struct vmw_dma_buffer *
  66. vmw_dma_buffer(struct ttm_buffer_object *bo)
  67. {
  68. return container_of(bo, struct vmw_dma_buffer, base);
  69. }
  70. static inline struct vmw_user_dma_buffer *
  71. vmw_user_dma_buffer(struct ttm_buffer_object *bo)
  72. {
  73. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  74. return container_of(vmw_bo, struct vmw_user_dma_buffer, dma);
  75. }
  76. struct vmw_resource *vmw_resource_reference(struct vmw_resource *res)
  77. {
  78. kref_get(&res->kref);
  79. return res;
  80. }
  81. /**
  82. * vmw_resource_release_id - release a resource id to the id manager.
  83. *
  84. * @res: Pointer to the resource.
  85. *
  86. * Release the resource id to the resource id manager and set it to -1
  87. */
  88. static void vmw_resource_release_id(struct vmw_resource *res)
  89. {
  90. struct vmw_private *dev_priv = res->dev_priv;
  91. write_lock(&dev_priv->resource_lock);
  92. if (res->id != -1)
  93. idr_remove(res->idr, res->id);
  94. res->id = -1;
  95. write_unlock(&dev_priv->resource_lock);
  96. }
  97. static void vmw_resource_release(struct kref *kref)
  98. {
  99. struct vmw_resource *res =
  100. container_of(kref, struct vmw_resource, kref);
  101. struct vmw_private *dev_priv = res->dev_priv;
  102. int id = res->id;
  103. struct idr *idr = res->idr;
  104. res->avail = false;
  105. if (res->remove_from_lists != NULL)
  106. res->remove_from_lists(res);
  107. write_unlock(&dev_priv->resource_lock);
  108. if (likely(res->hw_destroy != NULL))
  109. res->hw_destroy(res);
  110. if (res->res_free != NULL)
  111. res->res_free(res);
  112. else
  113. kfree(res);
  114. write_lock(&dev_priv->resource_lock);
  115. if (id != -1)
  116. idr_remove(idr, id);
  117. }
  118. void vmw_resource_unreference(struct vmw_resource **p_res)
  119. {
  120. struct vmw_resource *res = *p_res;
  121. struct vmw_private *dev_priv = res->dev_priv;
  122. *p_res = NULL;
  123. write_lock(&dev_priv->resource_lock);
  124. kref_put(&res->kref, vmw_resource_release);
  125. write_unlock(&dev_priv->resource_lock);
  126. }
  127. /**
  128. * vmw_resource_alloc_id - release a resource id to the id manager.
  129. *
  130. * @dev_priv: Pointer to the device private structure.
  131. * @res: Pointer to the resource.
  132. *
  133. * Allocate the lowest free resource from the resource manager, and set
  134. * @res->id to that id. Returns 0 on success and -ENOMEM on failure.
  135. */
  136. static int vmw_resource_alloc_id(struct vmw_private *dev_priv,
  137. struct vmw_resource *res)
  138. {
  139. int ret;
  140. BUG_ON(res->id != -1);
  141. do {
  142. if (unlikely(idr_pre_get(res->idr, GFP_KERNEL) == 0))
  143. return -ENOMEM;
  144. write_lock(&dev_priv->resource_lock);
  145. ret = idr_get_new_above(res->idr, res, 1, &res->id);
  146. write_unlock(&dev_priv->resource_lock);
  147. } while (ret == -EAGAIN);
  148. return ret;
  149. }
  150. static int vmw_resource_init(struct vmw_private *dev_priv,
  151. struct vmw_resource *res,
  152. struct idr *idr,
  153. enum ttm_object_type obj_type,
  154. bool delay_id,
  155. void (*res_free) (struct vmw_resource *res),
  156. void (*remove_from_lists)
  157. (struct vmw_resource *res))
  158. {
  159. kref_init(&res->kref);
  160. res->hw_destroy = NULL;
  161. res->res_free = res_free;
  162. res->remove_from_lists = remove_from_lists;
  163. res->res_type = obj_type;
  164. res->idr = idr;
  165. res->avail = false;
  166. res->dev_priv = dev_priv;
  167. INIT_LIST_HEAD(&res->query_head);
  168. INIT_LIST_HEAD(&res->validate_head);
  169. res->id = -1;
  170. if (delay_id)
  171. return 0;
  172. else
  173. return vmw_resource_alloc_id(dev_priv, res);
  174. }
  175. /**
  176. * vmw_resource_activate
  177. *
  178. * @res: Pointer to the newly created resource
  179. * @hw_destroy: Destroy function. NULL if none.
  180. *
  181. * Activate a resource after the hardware has been made aware of it.
  182. * Set tye destroy function to @destroy. Typically this frees the
  183. * resource and destroys the hardware resources associated with it.
  184. * Activate basically means that the function vmw_resource_lookup will
  185. * find it.
  186. */
  187. static void vmw_resource_activate(struct vmw_resource *res,
  188. void (*hw_destroy) (struct vmw_resource *))
  189. {
  190. struct vmw_private *dev_priv = res->dev_priv;
  191. write_lock(&dev_priv->resource_lock);
  192. res->avail = true;
  193. res->hw_destroy = hw_destroy;
  194. write_unlock(&dev_priv->resource_lock);
  195. }
  196. struct vmw_resource *vmw_resource_lookup(struct vmw_private *dev_priv,
  197. struct idr *idr, int id)
  198. {
  199. struct vmw_resource *res;
  200. read_lock(&dev_priv->resource_lock);
  201. res = idr_find(idr, id);
  202. if (res && res->avail)
  203. kref_get(&res->kref);
  204. else
  205. res = NULL;
  206. read_unlock(&dev_priv->resource_lock);
  207. if (unlikely(res == NULL))
  208. return NULL;
  209. return res;
  210. }
  211. /**
  212. * Context management:
  213. */
  214. static void vmw_hw_context_destroy(struct vmw_resource *res)
  215. {
  216. struct vmw_private *dev_priv = res->dev_priv;
  217. struct {
  218. SVGA3dCmdHeader header;
  219. SVGA3dCmdDestroyContext body;
  220. } *cmd;
  221. vmw_execbuf_release_pinned_bo(dev_priv, true, res->id);
  222. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  223. if (unlikely(cmd == NULL)) {
  224. DRM_ERROR("Failed reserving FIFO space for surface "
  225. "destruction.\n");
  226. return;
  227. }
  228. cmd->header.id = cpu_to_le32(SVGA_3D_CMD_CONTEXT_DESTROY);
  229. cmd->header.size = cpu_to_le32(sizeof(cmd->body));
  230. cmd->body.cid = cpu_to_le32(res->id);
  231. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  232. vmw_3d_resource_dec(dev_priv, false);
  233. }
  234. static int vmw_context_init(struct vmw_private *dev_priv,
  235. struct vmw_resource *res,
  236. void (*res_free) (struct vmw_resource *res))
  237. {
  238. int ret;
  239. struct {
  240. SVGA3dCmdHeader header;
  241. SVGA3dCmdDefineContext body;
  242. } *cmd;
  243. ret = vmw_resource_init(dev_priv, res, &dev_priv->context_idr,
  244. VMW_RES_CONTEXT, false, res_free, NULL);
  245. if (unlikely(ret != 0)) {
  246. DRM_ERROR("Failed to allocate a resource id.\n");
  247. goto out_early;
  248. }
  249. if (unlikely(res->id >= SVGA3D_MAX_CONTEXT_IDS)) {
  250. DRM_ERROR("Out of hw context ids.\n");
  251. vmw_resource_unreference(&res);
  252. return -ENOMEM;
  253. }
  254. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  255. if (unlikely(cmd == NULL)) {
  256. DRM_ERROR("Fifo reserve failed.\n");
  257. vmw_resource_unreference(&res);
  258. return -ENOMEM;
  259. }
  260. cmd->header.id = cpu_to_le32(SVGA_3D_CMD_CONTEXT_DEFINE);
  261. cmd->header.size = cpu_to_le32(sizeof(cmd->body));
  262. cmd->body.cid = cpu_to_le32(res->id);
  263. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  264. (void) vmw_3d_resource_inc(dev_priv, false);
  265. vmw_resource_activate(res, vmw_hw_context_destroy);
  266. return 0;
  267. out_early:
  268. if (res_free == NULL)
  269. kfree(res);
  270. else
  271. res_free(res);
  272. return ret;
  273. }
  274. struct vmw_resource *vmw_context_alloc(struct vmw_private *dev_priv)
  275. {
  276. struct vmw_resource *res = kmalloc(sizeof(*res), GFP_KERNEL);
  277. int ret;
  278. if (unlikely(res == NULL))
  279. return NULL;
  280. ret = vmw_context_init(dev_priv, res, NULL);
  281. return (ret == 0) ? res : NULL;
  282. }
  283. /**
  284. * User-space context management:
  285. */
  286. static void vmw_user_context_free(struct vmw_resource *res)
  287. {
  288. struct vmw_user_context *ctx =
  289. container_of(res, struct vmw_user_context, res);
  290. struct vmw_private *dev_priv = res->dev_priv;
  291. kfree(ctx);
  292. ttm_mem_global_free(vmw_mem_glob(dev_priv),
  293. vmw_user_context_size);
  294. }
  295. /**
  296. * This function is called when user space has no more references on the
  297. * base object. It releases the base-object's reference on the resource object.
  298. */
  299. static void vmw_user_context_base_release(struct ttm_base_object **p_base)
  300. {
  301. struct ttm_base_object *base = *p_base;
  302. struct vmw_user_context *ctx =
  303. container_of(base, struct vmw_user_context, base);
  304. struct vmw_resource *res = &ctx->res;
  305. *p_base = NULL;
  306. vmw_resource_unreference(&res);
  307. }
  308. int vmw_context_destroy_ioctl(struct drm_device *dev, void *data,
  309. struct drm_file *file_priv)
  310. {
  311. struct vmw_private *dev_priv = vmw_priv(dev);
  312. struct vmw_resource *res;
  313. struct vmw_user_context *ctx;
  314. struct drm_vmw_context_arg *arg = (struct drm_vmw_context_arg *)data;
  315. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  316. int ret = 0;
  317. res = vmw_resource_lookup(dev_priv, &dev_priv->context_idr, arg->cid);
  318. if (unlikely(res == NULL))
  319. return -EINVAL;
  320. if (res->res_free != &vmw_user_context_free) {
  321. ret = -EINVAL;
  322. goto out;
  323. }
  324. ctx = container_of(res, struct vmw_user_context, res);
  325. if (ctx->base.tfile != tfile && !ctx->base.shareable) {
  326. ret = -EPERM;
  327. goto out;
  328. }
  329. ttm_ref_object_base_unref(tfile, ctx->base.hash.key, TTM_REF_USAGE);
  330. out:
  331. vmw_resource_unreference(&res);
  332. return ret;
  333. }
  334. int vmw_context_define_ioctl(struct drm_device *dev, void *data,
  335. struct drm_file *file_priv)
  336. {
  337. struct vmw_private *dev_priv = vmw_priv(dev);
  338. struct vmw_user_context *ctx;
  339. struct vmw_resource *res;
  340. struct vmw_resource *tmp;
  341. struct drm_vmw_context_arg *arg = (struct drm_vmw_context_arg *)data;
  342. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  343. struct vmw_master *vmaster = vmw_master(file_priv->master);
  344. int ret;
  345. /*
  346. * Approximate idr memory usage with 128 bytes. It will be limited
  347. * by maximum number_of contexts anyway.
  348. */
  349. if (unlikely(vmw_user_context_size == 0))
  350. vmw_user_context_size = ttm_round_pot(sizeof(*ctx)) + 128;
  351. ret = ttm_read_lock(&vmaster->lock, true);
  352. if (unlikely(ret != 0))
  353. return ret;
  354. ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
  355. vmw_user_context_size,
  356. false, true);
  357. if (unlikely(ret != 0)) {
  358. if (ret != -ERESTARTSYS)
  359. DRM_ERROR("Out of graphics memory for context"
  360. " creation.\n");
  361. goto out_unlock;
  362. }
  363. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  364. if (unlikely(ctx == NULL)) {
  365. ttm_mem_global_free(vmw_mem_glob(dev_priv),
  366. vmw_user_context_size);
  367. ret = -ENOMEM;
  368. goto out_unlock;
  369. }
  370. res = &ctx->res;
  371. ctx->base.shareable = false;
  372. ctx->base.tfile = NULL;
  373. /*
  374. * From here on, the destructor takes over resource freeing.
  375. */
  376. ret = vmw_context_init(dev_priv, res, vmw_user_context_free);
  377. if (unlikely(ret != 0))
  378. goto out_unlock;
  379. tmp = vmw_resource_reference(&ctx->res);
  380. ret = ttm_base_object_init(tfile, &ctx->base, false, VMW_RES_CONTEXT,
  381. &vmw_user_context_base_release, NULL);
  382. if (unlikely(ret != 0)) {
  383. vmw_resource_unreference(&tmp);
  384. goto out_err;
  385. }
  386. arg->cid = res->id;
  387. out_err:
  388. vmw_resource_unreference(&res);
  389. out_unlock:
  390. ttm_read_unlock(&vmaster->lock);
  391. return ret;
  392. }
  393. int vmw_context_check(struct vmw_private *dev_priv,
  394. struct ttm_object_file *tfile,
  395. int id,
  396. struct vmw_resource **p_res)
  397. {
  398. struct vmw_resource *res;
  399. int ret = 0;
  400. read_lock(&dev_priv->resource_lock);
  401. res = idr_find(&dev_priv->context_idr, id);
  402. if (res && res->avail) {
  403. struct vmw_user_context *ctx =
  404. container_of(res, struct vmw_user_context, res);
  405. if (ctx->base.tfile != tfile && !ctx->base.shareable)
  406. ret = -EPERM;
  407. if (p_res)
  408. *p_res = vmw_resource_reference(res);
  409. } else
  410. ret = -EINVAL;
  411. read_unlock(&dev_priv->resource_lock);
  412. return ret;
  413. }
  414. struct vmw_bpp {
  415. uint8_t bpp;
  416. uint8_t s_bpp;
  417. };
  418. /*
  419. * Size table for the supported SVGA3D surface formats. It consists of
  420. * two values. The bpp value and the s_bpp value which is short for
  421. * "stride bits per pixel" The values are given in such a way that the
  422. * minimum stride for the image is calculated using
  423. *
  424. * min_stride = w*s_bpp
  425. *
  426. * and the total memory requirement for the image is
  427. *
  428. * h*min_stride*bpp/s_bpp
  429. *
  430. */
  431. static const struct vmw_bpp vmw_sf_bpp[] = {
  432. [SVGA3D_FORMAT_INVALID] = {0, 0},
  433. [SVGA3D_X8R8G8B8] = {32, 32},
  434. [SVGA3D_A8R8G8B8] = {32, 32},
  435. [SVGA3D_R5G6B5] = {16, 16},
  436. [SVGA3D_X1R5G5B5] = {16, 16},
  437. [SVGA3D_A1R5G5B5] = {16, 16},
  438. [SVGA3D_A4R4G4B4] = {16, 16},
  439. [SVGA3D_Z_D32] = {32, 32},
  440. [SVGA3D_Z_D16] = {16, 16},
  441. [SVGA3D_Z_D24S8] = {32, 32},
  442. [SVGA3D_Z_D15S1] = {16, 16},
  443. [SVGA3D_LUMINANCE8] = {8, 8},
  444. [SVGA3D_LUMINANCE4_ALPHA4] = {8, 8},
  445. [SVGA3D_LUMINANCE16] = {16, 16},
  446. [SVGA3D_LUMINANCE8_ALPHA8] = {16, 16},
  447. [SVGA3D_DXT1] = {4, 16},
  448. [SVGA3D_DXT2] = {8, 32},
  449. [SVGA3D_DXT3] = {8, 32},
  450. [SVGA3D_DXT4] = {8, 32},
  451. [SVGA3D_DXT5] = {8, 32},
  452. [SVGA3D_BUMPU8V8] = {16, 16},
  453. [SVGA3D_BUMPL6V5U5] = {16, 16},
  454. [SVGA3D_BUMPX8L8V8U8] = {32, 32},
  455. [SVGA3D_ARGB_S10E5] = {16, 16},
  456. [SVGA3D_ARGB_S23E8] = {32, 32},
  457. [SVGA3D_A2R10G10B10] = {32, 32},
  458. [SVGA3D_V8U8] = {16, 16},
  459. [SVGA3D_Q8W8V8U8] = {32, 32},
  460. [SVGA3D_CxV8U8] = {16, 16},
  461. [SVGA3D_X8L8V8U8] = {32, 32},
  462. [SVGA3D_A2W10V10U10] = {32, 32},
  463. [SVGA3D_ALPHA8] = {8, 8},
  464. [SVGA3D_R_S10E5] = {16, 16},
  465. [SVGA3D_R_S23E8] = {32, 32},
  466. [SVGA3D_RG_S10E5] = {16, 16},
  467. [SVGA3D_RG_S23E8] = {32, 32},
  468. [SVGA3D_BUFFER] = {8, 8},
  469. [SVGA3D_Z_D24X8] = {32, 32},
  470. [SVGA3D_V16U16] = {32, 32},
  471. [SVGA3D_G16R16] = {32, 32},
  472. [SVGA3D_A16B16G16R16] = {64, 64},
  473. [SVGA3D_UYVY] = {12, 12},
  474. [SVGA3D_YUY2] = {12, 12},
  475. [SVGA3D_NV12] = {12, 8},
  476. [SVGA3D_AYUV] = {32, 32},
  477. [SVGA3D_BC4_UNORM] = {4, 16},
  478. [SVGA3D_BC5_UNORM] = {8, 32},
  479. [SVGA3D_Z_DF16] = {16, 16},
  480. [SVGA3D_Z_DF24] = {24, 24},
  481. [SVGA3D_Z_D24S8_INT] = {32, 32}
  482. };
  483. /**
  484. * Surface management.
  485. */
  486. struct vmw_surface_dma {
  487. SVGA3dCmdHeader header;
  488. SVGA3dCmdSurfaceDMA body;
  489. SVGA3dCopyBox cb;
  490. SVGA3dCmdSurfaceDMASuffix suffix;
  491. };
  492. struct vmw_surface_define {
  493. SVGA3dCmdHeader header;
  494. SVGA3dCmdDefineSurface body;
  495. };
  496. struct vmw_surface_destroy {
  497. SVGA3dCmdHeader header;
  498. SVGA3dCmdDestroySurface body;
  499. };
  500. /**
  501. * vmw_surface_dma_size - Compute fifo size for a dma command.
  502. *
  503. * @srf: Pointer to a struct vmw_surface
  504. *
  505. * Computes the required size for a surface dma command for backup or
  506. * restoration of the surface represented by @srf.
  507. */
  508. static inline uint32_t vmw_surface_dma_size(const struct vmw_surface *srf)
  509. {
  510. return srf->num_sizes * sizeof(struct vmw_surface_dma);
  511. }
  512. /**
  513. * vmw_surface_define_size - Compute fifo size for a surface define command.
  514. *
  515. * @srf: Pointer to a struct vmw_surface
  516. *
  517. * Computes the required size for a surface define command for the definition
  518. * of the surface represented by @srf.
  519. */
  520. static inline uint32_t vmw_surface_define_size(const struct vmw_surface *srf)
  521. {
  522. return sizeof(struct vmw_surface_define) + srf->num_sizes *
  523. sizeof(SVGA3dSize);
  524. }
  525. /**
  526. * vmw_surface_destroy_size - Compute fifo size for a surface destroy command.
  527. *
  528. * Computes the required size for a surface destroy command for the destruction
  529. * of a hw surface.
  530. */
  531. static inline uint32_t vmw_surface_destroy_size(void)
  532. {
  533. return sizeof(struct vmw_surface_destroy);
  534. }
  535. /**
  536. * vmw_surface_destroy_encode - Encode a surface_destroy command.
  537. *
  538. * @id: The surface id
  539. * @cmd_space: Pointer to memory area in which the commands should be encoded.
  540. */
  541. static void vmw_surface_destroy_encode(uint32_t id,
  542. void *cmd_space)
  543. {
  544. struct vmw_surface_destroy *cmd = (struct vmw_surface_destroy *)
  545. cmd_space;
  546. cmd->header.id = SVGA_3D_CMD_SURFACE_DESTROY;
  547. cmd->header.size = sizeof(cmd->body);
  548. cmd->body.sid = id;
  549. }
  550. /**
  551. * vmw_surface_define_encode - Encode a surface_define command.
  552. *
  553. * @srf: Pointer to a struct vmw_surface object.
  554. * @cmd_space: Pointer to memory area in which the commands should be encoded.
  555. */
  556. static void vmw_surface_define_encode(const struct vmw_surface *srf,
  557. void *cmd_space)
  558. {
  559. struct vmw_surface_define *cmd = (struct vmw_surface_define *)
  560. cmd_space;
  561. struct drm_vmw_size *src_size;
  562. SVGA3dSize *cmd_size;
  563. uint32_t cmd_len;
  564. int i;
  565. cmd_len = sizeof(cmd->body) + srf->num_sizes * sizeof(SVGA3dSize);
  566. cmd->header.id = SVGA_3D_CMD_SURFACE_DEFINE;
  567. cmd->header.size = cmd_len;
  568. cmd->body.sid = srf->res.id;
  569. cmd->body.surfaceFlags = srf->flags;
  570. cmd->body.format = cpu_to_le32(srf->format);
  571. for (i = 0; i < DRM_VMW_MAX_SURFACE_FACES; ++i)
  572. cmd->body.face[i].numMipLevels = srf->mip_levels[i];
  573. cmd += 1;
  574. cmd_size = (SVGA3dSize *) cmd;
  575. src_size = srf->sizes;
  576. for (i = 0; i < srf->num_sizes; ++i, cmd_size++, src_size++) {
  577. cmd_size->width = src_size->width;
  578. cmd_size->height = src_size->height;
  579. cmd_size->depth = src_size->depth;
  580. }
  581. }
  582. /**
  583. * vmw_surface_dma_encode - Encode a surface_dma command.
  584. *
  585. * @srf: Pointer to a struct vmw_surface object.
  586. * @cmd_space: Pointer to memory area in which the commands should be encoded.
  587. * @ptr: Pointer to an SVGAGuestPtr indicating where the surface contents
  588. * should be placed or read from.
  589. * @to_surface: Boolean whether to DMA to the surface or from the surface.
  590. */
  591. static void vmw_surface_dma_encode(struct vmw_surface *srf,
  592. void *cmd_space,
  593. const SVGAGuestPtr *ptr,
  594. bool to_surface)
  595. {
  596. uint32_t i;
  597. uint32_t bpp = vmw_sf_bpp[srf->format].bpp;
  598. uint32_t stride_bpp = vmw_sf_bpp[srf->format].s_bpp;
  599. struct vmw_surface_dma *cmd = (struct vmw_surface_dma *)cmd_space;
  600. for (i = 0; i < srf->num_sizes; ++i) {
  601. SVGA3dCmdHeader *header = &cmd->header;
  602. SVGA3dCmdSurfaceDMA *body = &cmd->body;
  603. SVGA3dCopyBox *cb = &cmd->cb;
  604. SVGA3dCmdSurfaceDMASuffix *suffix = &cmd->suffix;
  605. const struct vmw_surface_offset *cur_offset = &srf->offsets[i];
  606. const struct drm_vmw_size *cur_size = &srf->sizes[i];
  607. header->id = SVGA_3D_CMD_SURFACE_DMA;
  608. header->size = sizeof(*body) + sizeof(*cb) + sizeof(*suffix);
  609. body->guest.ptr = *ptr;
  610. body->guest.ptr.offset += cur_offset->bo_offset;
  611. body->guest.pitch = (cur_size->width * stride_bpp + 7) >> 3;
  612. body->host.sid = srf->res.id;
  613. body->host.face = cur_offset->face;
  614. body->host.mipmap = cur_offset->mip;
  615. body->transfer = ((to_surface) ? SVGA3D_WRITE_HOST_VRAM :
  616. SVGA3D_READ_HOST_VRAM);
  617. cb->x = 0;
  618. cb->y = 0;
  619. cb->z = 0;
  620. cb->srcx = 0;
  621. cb->srcy = 0;
  622. cb->srcz = 0;
  623. cb->w = cur_size->width;
  624. cb->h = cur_size->height;
  625. cb->d = cur_size->depth;
  626. suffix->suffixSize = sizeof(*suffix);
  627. suffix->maximumOffset = body->guest.pitch*cur_size->height*
  628. cur_size->depth*bpp / stride_bpp;
  629. suffix->flags.discard = 0;
  630. suffix->flags.unsynchronized = 0;
  631. suffix->flags.reserved = 0;
  632. ++cmd;
  633. }
  634. };
  635. static void vmw_hw_surface_destroy(struct vmw_resource *res)
  636. {
  637. struct vmw_private *dev_priv = res->dev_priv;
  638. struct vmw_surface *srf;
  639. void *cmd;
  640. if (res->id != -1) {
  641. cmd = vmw_fifo_reserve(dev_priv, vmw_surface_destroy_size());
  642. if (unlikely(cmd == NULL)) {
  643. DRM_ERROR("Failed reserving FIFO space for surface "
  644. "destruction.\n");
  645. return;
  646. }
  647. vmw_surface_destroy_encode(res->id, cmd);
  648. vmw_fifo_commit(dev_priv, vmw_surface_destroy_size());
  649. /*
  650. * used_memory_size_atomic, or separate lock
  651. * to avoid taking dev_priv::cmdbuf_mutex in
  652. * the destroy path.
  653. */
  654. mutex_lock(&dev_priv->cmdbuf_mutex);
  655. srf = container_of(res, struct vmw_surface, res);
  656. dev_priv->used_memory_size -= srf->backup_size;
  657. mutex_unlock(&dev_priv->cmdbuf_mutex);
  658. }
  659. vmw_3d_resource_dec(dev_priv, false);
  660. }
  661. void vmw_surface_res_free(struct vmw_resource *res)
  662. {
  663. struct vmw_surface *srf = container_of(res, struct vmw_surface, res);
  664. if (srf->backup)
  665. ttm_bo_unref(&srf->backup);
  666. kfree(srf->offsets);
  667. kfree(srf->sizes);
  668. kfree(srf->snooper.image);
  669. kfree(srf);
  670. }
  671. /**
  672. * vmw_surface_do_validate - make a surface available to the device.
  673. *
  674. * @dev_priv: Pointer to a device private struct.
  675. * @srf: Pointer to a struct vmw_surface.
  676. *
  677. * If the surface doesn't have a hw id, allocate one, and optionally
  678. * DMA the backed up surface contents to the device.
  679. *
  680. * Returns -EBUSY if there wasn't sufficient device resources to
  681. * complete the validation. Retry after freeing up resources.
  682. *
  683. * May return other errors if the kernel is out of guest resources.
  684. */
  685. int vmw_surface_do_validate(struct vmw_private *dev_priv,
  686. struct vmw_surface *srf)
  687. {
  688. struct vmw_resource *res = &srf->res;
  689. struct list_head val_list;
  690. struct ttm_validate_buffer val_buf;
  691. uint32_t submit_size;
  692. uint8_t *cmd;
  693. int ret;
  694. if (likely(res->id != -1))
  695. return 0;
  696. if (unlikely(dev_priv->used_memory_size + srf->backup_size >=
  697. dev_priv->memory_size))
  698. return -EBUSY;
  699. /*
  700. * Reserve- and validate the backup DMA bo.
  701. */
  702. if (srf->backup) {
  703. INIT_LIST_HEAD(&val_list);
  704. val_buf.bo = ttm_bo_reference(srf->backup);
  705. val_buf.new_sync_obj_arg = (void *)((unsigned long)
  706. DRM_VMW_FENCE_FLAG_EXEC);
  707. list_add_tail(&val_buf.head, &val_list);
  708. ret = ttm_eu_reserve_buffers(&val_list);
  709. if (unlikely(ret != 0))
  710. goto out_no_reserve;
  711. ret = ttm_bo_validate(srf->backup, &vmw_srf_placement,
  712. true, false, false);
  713. if (unlikely(ret != 0))
  714. goto out_no_validate;
  715. }
  716. /*
  717. * Alloc id for the resource.
  718. */
  719. ret = vmw_resource_alloc_id(dev_priv, res);
  720. if (unlikely(ret != 0)) {
  721. DRM_ERROR("Failed to allocate a surface id.\n");
  722. goto out_no_id;
  723. }
  724. if (unlikely(res->id >= SVGA3D_MAX_SURFACE_IDS)) {
  725. ret = -EBUSY;
  726. goto out_no_fifo;
  727. }
  728. /*
  729. * Encode surface define- and dma commands.
  730. */
  731. submit_size = vmw_surface_define_size(srf);
  732. if (srf->backup)
  733. submit_size += vmw_surface_dma_size(srf);
  734. cmd = vmw_fifo_reserve(dev_priv, submit_size);
  735. if (unlikely(cmd == NULL)) {
  736. DRM_ERROR("Failed reserving FIFO space for surface "
  737. "validation.\n");
  738. ret = -ENOMEM;
  739. goto out_no_fifo;
  740. }
  741. vmw_surface_define_encode(srf, cmd);
  742. if (srf->backup) {
  743. SVGAGuestPtr ptr;
  744. cmd += vmw_surface_define_size(srf);
  745. vmw_bo_get_guest_ptr(srf->backup, &ptr);
  746. vmw_surface_dma_encode(srf, cmd, &ptr, true);
  747. }
  748. vmw_fifo_commit(dev_priv, submit_size);
  749. /*
  750. * Create a fence object and fence the backup buffer.
  751. */
  752. if (srf->backup) {
  753. struct vmw_fence_obj *fence;
  754. (void) vmw_execbuf_fence_commands(NULL, dev_priv,
  755. &fence, NULL);
  756. ttm_eu_fence_buffer_objects(&val_list, fence);
  757. if (likely(fence != NULL))
  758. vmw_fence_obj_unreference(&fence);
  759. ttm_bo_unref(&val_buf.bo);
  760. ttm_bo_unref(&srf->backup);
  761. }
  762. /*
  763. * Surface memory usage accounting.
  764. */
  765. dev_priv->used_memory_size += srf->backup_size;
  766. return 0;
  767. out_no_fifo:
  768. vmw_resource_release_id(res);
  769. out_no_id:
  770. out_no_validate:
  771. if (srf->backup)
  772. ttm_eu_backoff_reservation(&val_list);
  773. out_no_reserve:
  774. if (srf->backup)
  775. ttm_bo_unref(&val_buf.bo);
  776. return ret;
  777. }
  778. /**
  779. * vmw_surface_evict - Evict a hw surface.
  780. *
  781. * @dev_priv: Pointer to a device private struct.
  782. * @srf: Pointer to a struct vmw_surface
  783. *
  784. * DMA the contents of a hw surface to a backup guest buffer object,
  785. * and destroy the hw surface, releasing its id.
  786. */
  787. int vmw_surface_evict(struct vmw_private *dev_priv,
  788. struct vmw_surface *srf)
  789. {
  790. struct vmw_resource *res = &srf->res;
  791. struct list_head val_list;
  792. struct ttm_validate_buffer val_buf;
  793. uint32_t submit_size;
  794. uint8_t *cmd;
  795. int ret;
  796. struct vmw_fence_obj *fence;
  797. SVGAGuestPtr ptr;
  798. BUG_ON(res->id == -1);
  799. /*
  800. * Create a surface backup buffer object.
  801. */
  802. if (!srf->backup) {
  803. ret = ttm_bo_create(&dev_priv->bdev, srf->backup_size,
  804. ttm_bo_type_device,
  805. &vmw_srf_placement, 0, 0, true,
  806. NULL, &srf->backup);
  807. if (unlikely(ret != 0))
  808. return ret;
  809. }
  810. /*
  811. * Reserve- and validate the backup DMA bo.
  812. */
  813. INIT_LIST_HEAD(&val_list);
  814. val_buf.bo = ttm_bo_reference(srf->backup);
  815. val_buf.new_sync_obj_arg = (void *)(unsigned long)
  816. DRM_VMW_FENCE_FLAG_EXEC;
  817. list_add_tail(&val_buf.head, &val_list);
  818. ret = ttm_eu_reserve_buffers(&val_list);
  819. if (unlikely(ret != 0))
  820. goto out_no_reserve;
  821. ret = ttm_bo_validate(srf->backup, &vmw_srf_placement,
  822. true, false, false);
  823. if (unlikely(ret != 0))
  824. goto out_no_validate;
  825. /*
  826. * Encode the dma- and surface destroy commands.
  827. */
  828. submit_size = vmw_surface_dma_size(srf) + vmw_surface_destroy_size();
  829. cmd = vmw_fifo_reserve(dev_priv, submit_size);
  830. if (unlikely(cmd == NULL)) {
  831. DRM_ERROR("Failed reserving FIFO space for surface "
  832. "eviction.\n");
  833. ret = -ENOMEM;
  834. goto out_no_fifo;
  835. }
  836. vmw_bo_get_guest_ptr(srf->backup, &ptr);
  837. vmw_surface_dma_encode(srf, cmd, &ptr, false);
  838. cmd += vmw_surface_dma_size(srf);
  839. vmw_surface_destroy_encode(res->id, cmd);
  840. vmw_fifo_commit(dev_priv, submit_size);
  841. /*
  842. * Surface memory usage accounting.
  843. */
  844. dev_priv->used_memory_size -= srf->backup_size;
  845. /*
  846. * Create a fence object and fence the DMA buffer.
  847. */
  848. (void) vmw_execbuf_fence_commands(NULL, dev_priv,
  849. &fence, NULL);
  850. ttm_eu_fence_buffer_objects(&val_list, fence);
  851. if (likely(fence != NULL))
  852. vmw_fence_obj_unreference(&fence);
  853. ttm_bo_unref(&val_buf.bo);
  854. /*
  855. * Release the surface ID.
  856. */
  857. vmw_resource_release_id(res);
  858. return 0;
  859. out_no_fifo:
  860. out_no_validate:
  861. if (srf->backup)
  862. ttm_eu_backoff_reservation(&val_list);
  863. out_no_reserve:
  864. ttm_bo_unref(&val_buf.bo);
  865. ttm_bo_unref(&srf->backup);
  866. return ret;
  867. }
  868. /**
  869. * vmw_surface_validate - make a surface available to the device, evicting
  870. * other surfaces if needed.
  871. *
  872. * @dev_priv: Pointer to a device private struct.
  873. * @srf: Pointer to a struct vmw_surface.
  874. *
  875. * Try to validate a surface and if it fails due to limited device resources,
  876. * repeatedly try to evict other surfaces until the request can be
  877. * acommodated.
  878. *
  879. * May return errors if out of resources.
  880. */
  881. int vmw_surface_validate(struct vmw_private *dev_priv,
  882. struct vmw_surface *srf)
  883. {
  884. int ret;
  885. struct vmw_surface *evict_srf;
  886. do {
  887. write_lock(&dev_priv->resource_lock);
  888. list_del_init(&srf->lru_head);
  889. write_unlock(&dev_priv->resource_lock);
  890. ret = vmw_surface_do_validate(dev_priv, srf);
  891. if (likely(ret != -EBUSY))
  892. break;
  893. write_lock(&dev_priv->resource_lock);
  894. if (list_empty(&dev_priv->surface_lru)) {
  895. DRM_ERROR("Out of device memory for surfaces.\n");
  896. ret = -EBUSY;
  897. write_unlock(&dev_priv->resource_lock);
  898. break;
  899. }
  900. evict_srf = vmw_surface_reference
  901. (list_first_entry(&dev_priv->surface_lru,
  902. struct vmw_surface,
  903. lru_head));
  904. list_del_init(&evict_srf->lru_head);
  905. write_unlock(&dev_priv->resource_lock);
  906. (void) vmw_surface_evict(dev_priv, evict_srf);
  907. vmw_surface_unreference(&evict_srf);
  908. } while (1);
  909. if (unlikely(ret != 0 && srf->res.id != -1)) {
  910. write_lock(&dev_priv->resource_lock);
  911. list_add_tail(&srf->lru_head, &dev_priv->surface_lru);
  912. write_unlock(&dev_priv->resource_lock);
  913. }
  914. return ret;
  915. }
  916. /**
  917. * vmw_surface_remove_from_lists - Remove surface resources from lookup lists
  918. *
  919. * @res: Pointer to a struct vmw_resource embedded in a struct vmw_surface
  920. *
  921. * As part of the resource destruction, remove the surface from any
  922. * lookup lists.
  923. */
  924. static void vmw_surface_remove_from_lists(struct vmw_resource *res)
  925. {
  926. struct vmw_surface *srf = container_of(res, struct vmw_surface, res);
  927. list_del_init(&srf->lru_head);
  928. }
  929. int vmw_surface_init(struct vmw_private *dev_priv,
  930. struct vmw_surface *srf,
  931. void (*res_free) (struct vmw_resource *res))
  932. {
  933. int ret;
  934. struct vmw_resource *res = &srf->res;
  935. BUG_ON(res_free == NULL);
  936. INIT_LIST_HEAD(&srf->lru_head);
  937. ret = vmw_resource_init(dev_priv, res, &dev_priv->surface_idr,
  938. VMW_RES_SURFACE, true, res_free,
  939. vmw_surface_remove_from_lists);
  940. if (unlikely(ret != 0))
  941. res_free(res);
  942. /*
  943. * The surface won't be visible to hardware until a
  944. * surface validate.
  945. */
  946. (void) vmw_3d_resource_inc(dev_priv, false);
  947. vmw_resource_activate(res, vmw_hw_surface_destroy);
  948. return ret;
  949. }
  950. static void vmw_user_surface_free(struct vmw_resource *res)
  951. {
  952. struct vmw_surface *srf = container_of(res, struct vmw_surface, res);
  953. struct vmw_user_surface *user_srf =
  954. container_of(srf, struct vmw_user_surface, srf);
  955. struct vmw_private *dev_priv = srf->res.dev_priv;
  956. uint32_t size = user_srf->size;
  957. if (srf->backup)
  958. ttm_bo_unref(&srf->backup);
  959. kfree(srf->offsets);
  960. kfree(srf->sizes);
  961. kfree(srf->snooper.image);
  962. kfree(user_srf);
  963. ttm_mem_global_free(vmw_mem_glob(dev_priv), size);
  964. }
  965. /**
  966. * vmw_resource_unreserve - unreserve resources previously reserved for
  967. * command submission.
  968. *
  969. * @list_head: list of resources to unreserve.
  970. *
  971. * Currently only surfaces are considered, and unreserving a surface
  972. * means putting it back on the device's surface lru list,
  973. * so that it can be evicted if necessary.
  974. * This function traverses the resource list and
  975. * checks whether resources are surfaces, and in that case puts them back
  976. * on the device's surface LRU list.
  977. */
  978. void vmw_resource_unreserve(struct list_head *list)
  979. {
  980. struct vmw_resource *res;
  981. struct vmw_surface *srf;
  982. rwlock_t *lock = NULL;
  983. list_for_each_entry(res, list, validate_head) {
  984. if (res->res_free != &vmw_surface_res_free &&
  985. res->res_free != &vmw_user_surface_free)
  986. continue;
  987. if (unlikely(lock == NULL)) {
  988. lock = &res->dev_priv->resource_lock;
  989. write_lock(lock);
  990. }
  991. srf = container_of(res, struct vmw_surface, res);
  992. list_del_init(&srf->lru_head);
  993. list_add_tail(&srf->lru_head, &res->dev_priv->surface_lru);
  994. }
  995. if (lock != NULL)
  996. write_unlock(lock);
  997. }
  998. /**
  999. * Helper function that looks either a surface or dmabuf.
  1000. *
  1001. * The pointer this pointed at by out_surf and out_buf needs to be null.
  1002. */
  1003. int vmw_user_lookup_handle(struct vmw_private *dev_priv,
  1004. struct ttm_object_file *tfile,
  1005. uint32_t handle,
  1006. struct vmw_surface **out_surf,
  1007. struct vmw_dma_buffer **out_buf)
  1008. {
  1009. int ret;
  1010. BUG_ON(*out_surf || *out_buf);
  1011. ret = vmw_user_surface_lookup_handle(dev_priv, tfile, handle, out_surf);
  1012. if (!ret)
  1013. return 0;
  1014. ret = vmw_user_dmabuf_lookup(tfile, handle, out_buf);
  1015. return ret;
  1016. }
  1017. int vmw_user_surface_lookup_handle(struct vmw_private *dev_priv,
  1018. struct ttm_object_file *tfile,
  1019. uint32_t handle, struct vmw_surface **out)
  1020. {
  1021. struct vmw_resource *res;
  1022. struct vmw_surface *srf;
  1023. struct vmw_user_surface *user_srf;
  1024. struct ttm_base_object *base;
  1025. int ret = -EINVAL;
  1026. base = ttm_base_object_lookup(tfile, handle);
  1027. if (unlikely(base == NULL))
  1028. return -EINVAL;
  1029. if (unlikely(base->object_type != VMW_RES_SURFACE))
  1030. goto out_bad_resource;
  1031. user_srf = container_of(base, struct vmw_user_surface, base);
  1032. srf = &user_srf->srf;
  1033. res = &srf->res;
  1034. read_lock(&dev_priv->resource_lock);
  1035. if (!res->avail || res->res_free != &vmw_user_surface_free) {
  1036. read_unlock(&dev_priv->resource_lock);
  1037. goto out_bad_resource;
  1038. }
  1039. kref_get(&res->kref);
  1040. read_unlock(&dev_priv->resource_lock);
  1041. *out = srf;
  1042. ret = 0;
  1043. out_bad_resource:
  1044. ttm_base_object_unref(&base);
  1045. return ret;
  1046. }
  1047. static void vmw_user_surface_base_release(struct ttm_base_object **p_base)
  1048. {
  1049. struct ttm_base_object *base = *p_base;
  1050. struct vmw_user_surface *user_srf =
  1051. container_of(base, struct vmw_user_surface, base);
  1052. struct vmw_resource *res = &user_srf->srf.res;
  1053. *p_base = NULL;
  1054. vmw_resource_unreference(&res);
  1055. }
  1056. int vmw_surface_destroy_ioctl(struct drm_device *dev, void *data,
  1057. struct drm_file *file_priv)
  1058. {
  1059. struct drm_vmw_surface_arg *arg = (struct drm_vmw_surface_arg *)data;
  1060. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  1061. return ttm_ref_object_base_unref(tfile, arg->sid, TTM_REF_USAGE);
  1062. }
  1063. int vmw_surface_define_ioctl(struct drm_device *dev, void *data,
  1064. struct drm_file *file_priv)
  1065. {
  1066. struct vmw_private *dev_priv = vmw_priv(dev);
  1067. struct vmw_user_surface *user_srf;
  1068. struct vmw_surface *srf;
  1069. struct vmw_resource *res;
  1070. struct vmw_resource *tmp;
  1071. union drm_vmw_surface_create_arg *arg =
  1072. (union drm_vmw_surface_create_arg *)data;
  1073. struct drm_vmw_surface_create_req *req = &arg->req;
  1074. struct drm_vmw_surface_arg *rep = &arg->rep;
  1075. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  1076. struct drm_vmw_size __user *user_sizes;
  1077. int ret;
  1078. int i, j;
  1079. uint32_t cur_bo_offset;
  1080. struct drm_vmw_size *cur_size;
  1081. struct vmw_surface_offset *cur_offset;
  1082. uint32_t stride_bpp;
  1083. uint32_t bpp;
  1084. uint32_t num_sizes;
  1085. uint32_t size;
  1086. struct vmw_master *vmaster = vmw_master(file_priv->master);
  1087. if (unlikely(vmw_user_surface_size == 0))
  1088. vmw_user_surface_size = ttm_round_pot(sizeof(*user_srf)) +
  1089. 128;
  1090. num_sizes = 0;
  1091. for (i = 0; i < DRM_VMW_MAX_SURFACE_FACES; ++i)
  1092. num_sizes += req->mip_levels[i];
  1093. if (num_sizes > DRM_VMW_MAX_SURFACE_FACES *
  1094. DRM_VMW_MAX_MIP_LEVELS)
  1095. return -EINVAL;
  1096. size = vmw_user_surface_size + 128 +
  1097. ttm_round_pot(num_sizes * sizeof(struct drm_vmw_size)) +
  1098. ttm_round_pot(num_sizes * sizeof(struct vmw_surface_offset));
  1099. ret = ttm_read_lock(&vmaster->lock, true);
  1100. if (unlikely(ret != 0))
  1101. return ret;
  1102. ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
  1103. size, false, true);
  1104. if (unlikely(ret != 0)) {
  1105. if (ret != -ERESTARTSYS)
  1106. DRM_ERROR("Out of graphics memory for surface"
  1107. " creation.\n");
  1108. goto out_unlock;
  1109. }
  1110. user_srf = kmalloc(sizeof(*user_srf), GFP_KERNEL);
  1111. if (unlikely(user_srf == NULL)) {
  1112. ret = -ENOMEM;
  1113. goto out_no_user_srf;
  1114. }
  1115. srf = &user_srf->srf;
  1116. res = &srf->res;
  1117. srf->flags = req->flags;
  1118. srf->format = req->format;
  1119. srf->scanout = req->scanout;
  1120. srf->backup = NULL;
  1121. memcpy(srf->mip_levels, req->mip_levels, sizeof(srf->mip_levels));
  1122. srf->num_sizes = num_sizes;
  1123. user_srf->size = size;
  1124. srf->sizes = kmalloc(srf->num_sizes * sizeof(*srf->sizes), GFP_KERNEL);
  1125. if (unlikely(srf->sizes == NULL)) {
  1126. ret = -ENOMEM;
  1127. goto out_no_sizes;
  1128. }
  1129. srf->offsets = kmalloc(srf->num_sizes * sizeof(*srf->offsets),
  1130. GFP_KERNEL);
  1131. if (unlikely(srf->sizes == NULL)) {
  1132. ret = -ENOMEM;
  1133. goto out_no_offsets;
  1134. }
  1135. user_sizes = (struct drm_vmw_size __user *)(unsigned long)
  1136. req->size_addr;
  1137. ret = copy_from_user(srf->sizes, user_sizes,
  1138. srf->num_sizes * sizeof(*srf->sizes));
  1139. if (unlikely(ret != 0)) {
  1140. ret = -EFAULT;
  1141. goto out_no_copy;
  1142. }
  1143. cur_bo_offset = 0;
  1144. cur_offset = srf->offsets;
  1145. cur_size = srf->sizes;
  1146. bpp = vmw_sf_bpp[srf->format].bpp;
  1147. stride_bpp = vmw_sf_bpp[srf->format].s_bpp;
  1148. for (i = 0; i < DRM_VMW_MAX_SURFACE_FACES; ++i) {
  1149. for (j = 0; j < srf->mip_levels[i]; ++j) {
  1150. uint32_t stride =
  1151. (cur_size->width * stride_bpp + 7) >> 3;
  1152. cur_offset->face = i;
  1153. cur_offset->mip = j;
  1154. cur_offset->bo_offset = cur_bo_offset;
  1155. cur_bo_offset += stride * cur_size->height *
  1156. cur_size->depth * bpp / stride_bpp;
  1157. ++cur_offset;
  1158. ++cur_size;
  1159. }
  1160. }
  1161. srf->backup_size = cur_bo_offset;
  1162. if (srf->scanout &&
  1163. srf->num_sizes == 1 &&
  1164. srf->sizes[0].width == 64 &&
  1165. srf->sizes[0].height == 64 &&
  1166. srf->format == SVGA3D_A8R8G8B8) {
  1167. /* allocate image area and clear it */
  1168. srf->snooper.image = kzalloc(64 * 64 * 4, GFP_KERNEL);
  1169. if (!srf->snooper.image) {
  1170. DRM_ERROR("Failed to allocate cursor_image\n");
  1171. ret = -ENOMEM;
  1172. goto out_no_copy;
  1173. }
  1174. } else {
  1175. srf->snooper.image = NULL;
  1176. }
  1177. srf->snooper.crtc = NULL;
  1178. user_srf->base.shareable = false;
  1179. user_srf->base.tfile = NULL;
  1180. /**
  1181. * From this point, the generic resource management functions
  1182. * destroy the object on failure.
  1183. */
  1184. ret = vmw_surface_init(dev_priv, srf, vmw_user_surface_free);
  1185. if (unlikely(ret != 0))
  1186. goto out_unlock;
  1187. tmp = vmw_resource_reference(&srf->res);
  1188. ret = ttm_base_object_init(tfile, &user_srf->base,
  1189. req->shareable, VMW_RES_SURFACE,
  1190. &vmw_user_surface_base_release, NULL);
  1191. if (unlikely(ret != 0)) {
  1192. vmw_resource_unreference(&tmp);
  1193. vmw_resource_unreference(&res);
  1194. goto out_unlock;
  1195. }
  1196. rep->sid = user_srf->base.hash.key;
  1197. if (rep->sid == SVGA3D_INVALID_ID)
  1198. DRM_ERROR("Created bad Surface ID.\n");
  1199. vmw_resource_unreference(&res);
  1200. ttm_read_unlock(&vmaster->lock);
  1201. return 0;
  1202. out_no_copy:
  1203. kfree(srf->offsets);
  1204. out_no_offsets:
  1205. kfree(srf->sizes);
  1206. out_no_sizes:
  1207. kfree(user_srf);
  1208. out_no_user_srf:
  1209. ttm_mem_global_free(vmw_mem_glob(dev_priv), size);
  1210. out_unlock:
  1211. ttm_read_unlock(&vmaster->lock);
  1212. return ret;
  1213. }
  1214. int vmw_surface_reference_ioctl(struct drm_device *dev, void *data,
  1215. struct drm_file *file_priv)
  1216. {
  1217. union drm_vmw_surface_reference_arg *arg =
  1218. (union drm_vmw_surface_reference_arg *)data;
  1219. struct drm_vmw_surface_arg *req = &arg->req;
  1220. struct drm_vmw_surface_create_req *rep = &arg->rep;
  1221. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  1222. struct vmw_surface *srf;
  1223. struct vmw_user_surface *user_srf;
  1224. struct drm_vmw_size __user *user_sizes;
  1225. struct ttm_base_object *base;
  1226. int ret = -EINVAL;
  1227. base = ttm_base_object_lookup(tfile, req->sid);
  1228. if (unlikely(base == NULL)) {
  1229. DRM_ERROR("Could not find surface to reference.\n");
  1230. return -EINVAL;
  1231. }
  1232. if (unlikely(base->object_type != VMW_RES_SURFACE))
  1233. goto out_bad_resource;
  1234. user_srf = container_of(base, struct vmw_user_surface, base);
  1235. srf = &user_srf->srf;
  1236. ret = ttm_ref_object_add(tfile, &user_srf->base, TTM_REF_USAGE, NULL);
  1237. if (unlikely(ret != 0)) {
  1238. DRM_ERROR("Could not add a reference to a surface.\n");
  1239. goto out_no_reference;
  1240. }
  1241. rep->flags = srf->flags;
  1242. rep->format = srf->format;
  1243. memcpy(rep->mip_levels, srf->mip_levels, sizeof(srf->mip_levels));
  1244. user_sizes = (struct drm_vmw_size __user *)(unsigned long)
  1245. rep->size_addr;
  1246. if (user_sizes)
  1247. ret = copy_to_user(user_sizes, srf->sizes,
  1248. srf->num_sizes * sizeof(*srf->sizes));
  1249. if (unlikely(ret != 0)) {
  1250. DRM_ERROR("copy_to_user failed %p %u\n",
  1251. user_sizes, srf->num_sizes);
  1252. ret = -EFAULT;
  1253. }
  1254. out_bad_resource:
  1255. out_no_reference:
  1256. ttm_base_object_unref(&base);
  1257. return ret;
  1258. }
  1259. int vmw_surface_check(struct vmw_private *dev_priv,
  1260. struct ttm_object_file *tfile,
  1261. uint32_t handle, int *id)
  1262. {
  1263. struct ttm_base_object *base;
  1264. struct vmw_user_surface *user_srf;
  1265. int ret = -EPERM;
  1266. base = ttm_base_object_lookup(tfile, handle);
  1267. if (unlikely(base == NULL))
  1268. return -EINVAL;
  1269. if (unlikely(base->object_type != VMW_RES_SURFACE))
  1270. goto out_bad_surface;
  1271. user_srf = container_of(base, struct vmw_user_surface, base);
  1272. *id = user_srf->srf.res.id;
  1273. ret = 0;
  1274. out_bad_surface:
  1275. /**
  1276. * FIXME: May deadlock here when called from the
  1277. * command parsing code.
  1278. */
  1279. ttm_base_object_unref(&base);
  1280. return ret;
  1281. }
  1282. /**
  1283. * Buffer management.
  1284. */
  1285. void vmw_dmabuf_bo_free(struct ttm_buffer_object *bo)
  1286. {
  1287. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  1288. kfree(vmw_bo);
  1289. }
  1290. int vmw_dmabuf_init(struct vmw_private *dev_priv,
  1291. struct vmw_dma_buffer *vmw_bo,
  1292. size_t size, struct ttm_placement *placement,
  1293. bool interruptible,
  1294. void (*bo_free) (struct ttm_buffer_object *bo))
  1295. {
  1296. struct ttm_bo_device *bdev = &dev_priv->bdev;
  1297. size_t acc_size;
  1298. int ret;
  1299. BUG_ON(!bo_free);
  1300. acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct vmw_dma_buffer));
  1301. memset(vmw_bo, 0, sizeof(*vmw_bo));
  1302. INIT_LIST_HEAD(&vmw_bo->validate_list);
  1303. ret = ttm_bo_init(bdev, &vmw_bo->base, size,
  1304. ttm_bo_type_device, placement,
  1305. 0, 0, interruptible,
  1306. NULL, acc_size, bo_free);
  1307. return ret;
  1308. }
  1309. static void vmw_user_dmabuf_destroy(struct ttm_buffer_object *bo)
  1310. {
  1311. struct vmw_user_dma_buffer *vmw_user_bo = vmw_user_dma_buffer(bo);
  1312. kfree(vmw_user_bo);
  1313. }
  1314. static void vmw_user_dmabuf_release(struct ttm_base_object **p_base)
  1315. {
  1316. struct vmw_user_dma_buffer *vmw_user_bo;
  1317. struct ttm_base_object *base = *p_base;
  1318. struct ttm_buffer_object *bo;
  1319. *p_base = NULL;
  1320. if (unlikely(base == NULL))
  1321. return;
  1322. vmw_user_bo = container_of(base, struct vmw_user_dma_buffer, base);
  1323. bo = &vmw_user_bo->dma.base;
  1324. ttm_bo_unref(&bo);
  1325. }
  1326. int vmw_dmabuf_alloc_ioctl(struct drm_device *dev, void *data,
  1327. struct drm_file *file_priv)
  1328. {
  1329. struct vmw_private *dev_priv = vmw_priv(dev);
  1330. union drm_vmw_alloc_dmabuf_arg *arg =
  1331. (union drm_vmw_alloc_dmabuf_arg *)data;
  1332. struct drm_vmw_alloc_dmabuf_req *req = &arg->req;
  1333. struct drm_vmw_dmabuf_rep *rep = &arg->rep;
  1334. struct vmw_user_dma_buffer *vmw_user_bo;
  1335. struct ttm_buffer_object *tmp;
  1336. struct vmw_master *vmaster = vmw_master(file_priv->master);
  1337. int ret;
  1338. vmw_user_bo = kzalloc(sizeof(*vmw_user_bo), GFP_KERNEL);
  1339. if (unlikely(vmw_user_bo == NULL))
  1340. return -ENOMEM;
  1341. ret = ttm_read_lock(&vmaster->lock, true);
  1342. if (unlikely(ret != 0)) {
  1343. kfree(vmw_user_bo);
  1344. return ret;
  1345. }
  1346. ret = vmw_dmabuf_init(dev_priv, &vmw_user_bo->dma, req->size,
  1347. &vmw_vram_sys_placement, true,
  1348. &vmw_user_dmabuf_destroy);
  1349. if (unlikely(ret != 0))
  1350. goto out_no_dmabuf;
  1351. tmp = ttm_bo_reference(&vmw_user_bo->dma.base);
  1352. ret = ttm_base_object_init(vmw_fpriv(file_priv)->tfile,
  1353. &vmw_user_bo->base,
  1354. false,
  1355. ttm_buffer_type,
  1356. &vmw_user_dmabuf_release, NULL);
  1357. if (unlikely(ret != 0))
  1358. goto out_no_base_object;
  1359. else {
  1360. rep->handle = vmw_user_bo->base.hash.key;
  1361. rep->map_handle = vmw_user_bo->dma.base.addr_space_offset;
  1362. rep->cur_gmr_id = vmw_user_bo->base.hash.key;
  1363. rep->cur_gmr_offset = 0;
  1364. }
  1365. out_no_base_object:
  1366. ttm_bo_unref(&tmp);
  1367. out_no_dmabuf:
  1368. ttm_read_unlock(&vmaster->lock);
  1369. return ret;
  1370. }
  1371. int vmw_dmabuf_unref_ioctl(struct drm_device *dev, void *data,
  1372. struct drm_file *file_priv)
  1373. {
  1374. struct drm_vmw_unref_dmabuf_arg *arg =
  1375. (struct drm_vmw_unref_dmabuf_arg *)data;
  1376. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  1377. arg->handle,
  1378. TTM_REF_USAGE);
  1379. }
  1380. uint32_t vmw_dmabuf_validate_node(struct ttm_buffer_object *bo,
  1381. uint32_t cur_validate_node)
  1382. {
  1383. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  1384. if (likely(vmw_bo->on_validate_list))
  1385. return vmw_bo->cur_validate_node;
  1386. vmw_bo->cur_validate_node = cur_validate_node;
  1387. vmw_bo->on_validate_list = true;
  1388. return cur_validate_node;
  1389. }
  1390. void vmw_dmabuf_validate_clear(struct ttm_buffer_object *bo)
  1391. {
  1392. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  1393. vmw_bo->on_validate_list = false;
  1394. }
  1395. int vmw_user_dmabuf_lookup(struct ttm_object_file *tfile,
  1396. uint32_t handle, struct vmw_dma_buffer **out)
  1397. {
  1398. struct vmw_user_dma_buffer *vmw_user_bo;
  1399. struct ttm_base_object *base;
  1400. base = ttm_base_object_lookup(tfile, handle);
  1401. if (unlikely(base == NULL)) {
  1402. printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
  1403. (unsigned long)handle);
  1404. return -ESRCH;
  1405. }
  1406. if (unlikely(base->object_type != ttm_buffer_type)) {
  1407. ttm_base_object_unref(&base);
  1408. printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
  1409. (unsigned long)handle);
  1410. return -EINVAL;
  1411. }
  1412. vmw_user_bo = container_of(base, struct vmw_user_dma_buffer, base);
  1413. (void)ttm_bo_reference(&vmw_user_bo->dma.base);
  1414. ttm_base_object_unref(&base);
  1415. *out = &vmw_user_bo->dma;
  1416. return 0;
  1417. }
  1418. /*
  1419. * Stream management
  1420. */
  1421. static void vmw_stream_destroy(struct vmw_resource *res)
  1422. {
  1423. struct vmw_private *dev_priv = res->dev_priv;
  1424. struct vmw_stream *stream;
  1425. int ret;
  1426. DRM_INFO("%s: unref\n", __func__);
  1427. stream = container_of(res, struct vmw_stream, res);
  1428. ret = vmw_overlay_unref(dev_priv, stream->stream_id);
  1429. WARN_ON(ret != 0);
  1430. }
  1431. static int vmw_stream_init(struct vmw_private *dev_priv,
  1432. struct vmw_stream *stream,
  1433. void (*res_free) (struct vmw_resource *res))
  1434. {
  1435. struct vmw_resource *res = &stream->res;
  1436. int ret;
  1437. ret = vmw_resource_init(dev_priv, res, &dev_priv->stream_idr,
  1438. VMW_RES_STREAM, false, res_free, NULL);
  1439. if (unlikely(ret != 0)) {
  1440. if (res_free == NULL)
  1441. kfree(stream);
  1442. else
  1443. res_free(&stream->res);
  1444. return ret;
  1445. }
  1446. ret = vmw_overlay_claim(dev_priv, &stream->stream_id);
  1447. if (ret) {
  1448. vmw_resource_unreference(&res);
  1449. return ret;
  1450. }
  1451. DRM_INFO("%s: claimed\n", __func__);
  1452. vmw_resource_activate(&stream->res, vmw_stream_destroy);
  1453. return 0;
  1454. }
  1455. /**
  1456. * User-space context management:
  1457. */
  1458. static void vmw_user_stream_free(struct vmw_resource *res)
  1459. {
  1460. struct vmw_user_stream *stream =
  1461. container_of(res, struct vmw_user_stream, stream.res);
  1462. struct vmw_private *dev_priv = res->dev_priv;
  1463. kfree(stream);
  1464. ttm_mem_global_free(vmw_mem_glob(dev_priv),
  1465. vmw_user_stream_size);
  1466. }
  1467. /**
  1468. * This function is called when user space has no more references on the
  1469. * base object. It releases the base-object's reference on the resource object.
  1470. */
  1471. static void vmw_user_stream_base_release(struct ttm_base_object **p_base)
  1472. {
  1473. struct ttm_base_object *base = *p_base;
  1474. struct vmw_user_stream *stream =
  1475. container_of(base, struct vmw_user_stream, base);
  1476. struct vmw_resource *res = &stream->stream.res;
  1477. *p_base = NULL;
  1478. vmw_resource_unreference(&res);
  1479. }
  1480. int vmw_stream_unref_ioctl(struct drm_device *dev, void *data,
  1481. struct drm_file *file_priv)
  1482. {
  1483. struct vmw_private *dev_priv = vmw_priv(dev);
  1484. struct vmw_resource *res;
  1485. struct vmw_user_stream *stream;
  1486. struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
  1487. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  1488. int ret = 0;
  1489. res = vmw_resource_lookup(dev_priv, &dev_priv->stream_idr, arg->stream_id);
  1490. if (unlikely(res == NULL))
  1491. return -EINVAL;
  1492. if (res->res_free != &vmw_user_stream_free) {
  1493. ret = -EINVAL;
  1494. goto out;
  1495. }
  1496. stream = container_of(res, struct vmw_user_stream, stream.res);
  1497. if (stream->base.tfile != tfile) {
  1498. ret = -EINVAL;
  1499. goto out;
  1500. }
  1501. ttm_ref_object_base_unref(tfile, stream->base.hash.key, TTM_REF_USAGE);
  1502. out:
  1503. vmw_resource_unreference(&res);
  1504. return ret;
  1505. }
  1506. int vmw_stream_claim_ioctl(struct drm_device *dev, void *data,
  1507. struct drm_file *file_priv)
  1508. {
  1509. struct vmw_private *dev_priv = vmw_priv(dev);
  1510. struct vmw_user_stream *stream;
  1511. struct vmw_resource *res;
  1512. struct vmw_resource *tmp;
  1513. struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
  1514. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  1515. struct vmw_master *vmaster = vmw_master(file_priv->master);
  1516. int ret;
  1517. /*
  1518. * Approximate idr memory usage with 128 bytes. It will be limited
  1519. * by maximum number_of streams anyway?
  1520. */
  1521. if (unlikely(vmw_user_stream_size == 0))
  1522. vmw_user_stream_size = ttm_round_pot(sizeof(*stream)) + 128;
  1523. ret = ttm_read_lock(&vmaster->lock, true);
  1524. if (unlikely(ret != 0))
  1525. return ret;
  1526. ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
  1527. vmw_user_stream_size,
  1528. false, true);
  1529. if (unlikely(ret != 0)) {
  1530. if (ret != -ERESTARTSYS)
  1531. DRM_ERROR("Out of graphics memory for stream"
  1532. " creation.\n");
  1533. goto out_unlock;
  1534. }
  1535. stream = kmalloc(sizeof(*stream), GFP_KERNEL);
  1536. if (unlikely(stream == NULL)) {
  1537. ttm_mem_global_free(vmw_mem_glob(dev_priv),
  1538. vmw_user_stream_size);
  1539. ret = -ENOMEM;
  1540. goto out_unlock;
  1541. }
  1542. res = &stream->stream.res;
  1543. stream->base.shareable = false;
  1544. stream->base.tfile = NULL;
  1545. /*
  1546. * From here on, the destructor takes over resource freeing.
  1547. */
  1548. ret = vmw_stream_init(dev_priv, &stream->stream, vmw_user_stream_free);
  1549. if (unlikely(ret != 0))
  1550. goto out_unlock;
  1551. tmp = vmw_resource_reference(res);
  1552. ret = ttm_base_object_init(tfile, &stream->base, false, VMW_RES_STREAM,
  1553. &vmw_user_stream_base_release, NULL);
  1554. if (unlikely(ret != 0)) {
  1555. vmw_resource_unreference(&tmp);
  1556. goto out_err;
  1557. }
  1558. arg->stream_id = res->id;
  1559. out_err:
  1560. vmw_resource_unreference(&res);
  1561. out_unlock:
  1562. ttm_read_unlock(&vmaster->lock);
  1563. return ret;
  1564. }
  1565. int vmw_user_stream_lookup(struct vmw_private *dev_priv,
  1566. struct ttm_object_file *tfile,
  1567. uint32_t *inout_id, struct vmw_resource **out)
  1568. {
  1569. struct vmw_user_stream *stream;
  1570. struct vmw_resource *res;
  1571. int ret;
  1572. res = vmw_resource_lookup(dev_priv, &dev_priv->stream_idr, *inout_id);
  1573. if (unlikely(res == NULL))
  1574. return -EINVAL;
  1575. if (res->res_free != &vmw_user_stream_free) {
  1576. ret = -EINVAL;
  1577. goto err_ref;
  1578. }
  1579. stream = container_of(res, struct vmw_user_stream, stream.res);
  1580. if (stream->base.tfile != tfile) {
  1581. ret = -EPERM;
  1582. goto err_ref;
  1583. }
  1584. *inout_id = stream->stream.stream_id;
  1585. *out = res;
  1586. return 0;
  1587. err_ref:
  1588. vmw_resource_unreference(&res);
  1589. return ret;
  1590. }
  1591. int vmw_dumb_create(struct drm_file *file_priv,
  1592. struct drm_device *dev,
  1593. struct drm_mode_create_dumb *args)
  1594. {
  1595. struct vmw_private *dev_priv = vmw_priv(dev);
  1596. struct vmw_master *vmaster = vmw_master(file_priv->master);
  1597. struct vmw_user_dma_buffer *vmw_user_bo;
  1598. struct ttm_buffer_object *tmp;
  1599. int ret;
  1600. args->pitch = args->width * ((args->bpp + 7) / 8);
  1601. args->size = args->pitch * args->height;
  1602. vmw_user_bo = kzalloc(sizeof(*vmw_user_bo), GFP_KERNEL);
  1603. if (vmw_user_bo == NULL)
  1604. return -ENOMEM;
  1605. ret = ttm_read_lock(&vmaster->lock, true);
  1606. if (ret != 0) {
  1607. kfree(vmw_user_bo);
  1608. return ret;
  1609. }
  1610. ret = vmw_dmabuf_init(dev_priv, &vmw_user_bo->dma, args->size,
  1611. &vmw_vram_sys_placement, true,
  1612. &vmw_user_dmabuf_destroy);
  1613. if (ret != 0)
  1614. goto out_no_dmabuf;
  1615. tmp = ttm_bo_reference(&vmw_user_bo->dma.base);
  1616. ret = ttm_base_object_init(vmw_fpriv(file_priv)->tfile,
  1617. &vmw_user_bo->base,
  1618. false,
  1619. ttm_buffer_type,
  1620. &vmw_user_dmabuf_release, NULL);
  1621. if (unlikely(ret != 0))
  1622. goto out_no_base_object;
  1623. args->handle = vmw_user_bo->base.hash.key;
  1624. out_no_base_object:
  1625. ttm_bo_unref(&tmp);
  1626. out_no_dmabuf:
  1627. ttm_read_unlock(&vmaster->lock);
  1628. return ret;
  1629. }
  1630. int vmw_dumb_map_offset(struct drm_file *file_priv,
  1631. struct drm_device *dev, uint32_t handle,
  1632. uint64_t *offset)
  1633. {
  1634. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  1635. struct vmw_dma_buffer *out_buf;
  1636. int ret;
  1637. ret = vmw_user_dmabuf_lookup(tfile, handle, &out_buf);
  1638. if (ret != 0)
  1639. return -EINVAL;
  1640. *offset = out_buf->base.addr_space_offset;
  1641. vmw_dmabuf_unreference(&out_buf);
  1642. return 0;
  1643. }
  1644. int vmw_dumb_destroy(struct drm_file *file_priv,
  1645. struct drm_device *dev,
  1646. uint32_t handle)
  1647. {
  1648. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  1649. handle, TTM_REF_USAGE);
  1650. }