vmwgfx_resource.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  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. #define VMW_RES_CONTEXT ttm_driver_type0
  33. #define VMW_RES_SURFACE ttm_driver_type1
  34. #define VMW_RES_STREAM ttm_driver_type2
  35. struct vmw_user_context {
  36. struct ttm_base_object base;
  37. struct vmw_resource res;
  38. };
  39. struct vmw_user_surface {
  40. struct ttm_base_object base;
  41. struct vmw_surface srf;
  42. };
  43. struct vmw_user_dma_buffer {
  44. struct ttm_base_object base;
  45. struct vmw_dma_buffer dma;
  46. };
  47. struct vmw_bo_user_rep {
  48. uint32_t handle;
  49. uint64_t map_handle;
  50. };
  51. struct vmw_stream {
  52. struct vmw_resource res;
  53. uint32_t stream_id;
  54. };
  55. struct vmw_user_stream {
  56. struct ttm_base_object base;
  57. struct vmw_stream stream;
  58. };
  59. static inline struct vmw_dma_buffer *
  60. vmw_dma_buffer(struct ttm_buffer_object *bo)
  61. {
  62. return container_of(bo, struct vmw_dma_buffer, base);
  63. }
  64. static inline struct vmw_user_dma_buffer *
  65. vmw_user_dma_buffer(struct ttm_buffer_object *bo)
  66. {
  67. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  68. return container_of(vmw_bo, struct vmw_user_dma_buffer, dma);
  69. }
  70. struct vmw_resource *vmw_resource_reference(struct vmw_resource *res)
  71. {
  72. kref_get(&res->kref);
  73. return res;
  74. }
  75. static void vmw_resource_release(struct kref *kref)
  76. {
  77. struct vmw_resource *res =
  78. container_of(kref, struct vmw_resource, kref);
  79. struct vmw_private *dev_priv = res->dev_priv;
  80. idr_remove(res->idr, res->id);
  81. write_unlock(&dev_priv->resource_lock);
  82. if (likely(res->hw_destroy != NULL))
  83. res->hw_destroy(res);
  84. if (res->res_free != NULL)
  85. res->res_free(res);
  86. else
  87. kfree(res);
  88. write_lock(&dev_priv->resource_lock);
  89. }
  90. void vmw_resource_unreference(struct vmw_resource **p_res)
  91. {
  92. struct vmw_resource *res = *p_res;
  93. struct vmw_private *dev_priv = res->dev_priv;
  94. *p_res = NULL;
  95. write_lock(&dev_priv->resource_lock);
  96. kref_put(&res->kref, vmw_resource_release);
  97. write_unlock(&dev_priv->resource_lock);
  98. }
  99. static int vmw_resource_init(struct vmw_private *dev_priv,
  100. struct vmw_resource *res,
  101. struct idr *idr,
  102. enum ttm_object_type obj_type,
  103. void (*res_free) (struct vmw_resource *res))
  104. {
  105. int ret;
  106. kref_init(&res->kref);
  107. res->hw_destroy = NULL;
  108. res->res_free = res_free;
  109. res->res_type = obj_type;
  110. res->idr = idr;
  111. res->avail = false;
  112. res->dev_priv = dev_priv;
  113. do {
  114. if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
  115. return -ENOMEM;
  116. write_lock(&dev_priv->resource_lock);
  117. ret = idr_get_new_above(idr, res, 1, &res->id);
  118. write_unlock(&dev_priv->resource_lock);
  119. } while (ret == -EAGAIN);
  120. return ret;
  121. }
  122. /**
  123. * vmw_resource_activate
  124. *
  125. * @res: Pointer to the newly created resource
  126. * @hw_destroy: Destroy function. NULL if none.
  127. *
  128. * Activate a resource after the hardware has been made aware of it.
  129. * Set tye destroy function to @destroy. Typically this frees the
  130. * resource and destroys the hardware resources associated with it.
  131. * Activate basically means that the function vmw_resource_lookup will
  132. * find it.
  133. */
  134. static void vmw_resource_activate(struct vmw_resource *res,
  135. void (*hw_destroy) (struct vmw_resource *))
  136. {
  137. struct vmw_private *dev_priv = res->dev_priv;
  138. write_lock(&dev_priv->resource_lock);
  139. res->avail = true;
  140. res->hw_destroy = hw_destroy;
  141. write_unlock(&dev_priv->resource_lock);
  142. }
  143. struct vmw_resource *vmw_resource_lookup(struct vmw_private *dev_priv,
  144. struct idr *idr, int id)
  145. {
  146. struct vmw_resource *res;
  147. read_lock(&dev_priv->resource_lock);
  148. res = idr_find(idr, id);
  149. if (res && res->avail)
  150. kref_get(&res->kref);
  151. else
  152. res = NULL;
  153. read_unlock(&dev_priv->resource_lock);
  154. if (unlikely(res == NULL))
  155. return NULL;
  156. return res;
  157. }
  158. /**
  159. * Context management:
  160. */
  161. static void vmw_hw_context_destroy(struct vmw_resource *res)
  162. {
  163. struct vmw_private *dev_priv = res->dev_priv;
  164. struct {
  165. SVGA3dCmdHeader header;
  166. SVGA3dCmdDestroyContext body;
  167. } *cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  168. if (unlikely(cmd == NULL)) {
  169. DRM_ERROR("Failed reserving FIFO space for surface "
  170. "destruction.\n");
  171. return;
  172. }
  173. cmd->header.id = cpu_to_le32(SVGA_3D_CMD_CONTEXT_DESTROY);
  174. cmd->header.size = cpu_to_le32(sizeof(cmd->body));
  175. cmd->body.cid = cpu_to_le32(res->id);
  176. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  177. }
  178. static int vmw_context_init(struct vmw_private *dev_priv,
  179. struct vmw_resource *res,
  180. void (*res_free) (struct vmw_resource *res))
  181. {
  182. int ret;
  183. struct {
  184. SVGA3dCmdHeader header;
  185. SVGA3dCmdDefineContext body;
  186. } *cmd;
  187. ret = vmw_resource_init(dev_priv, res, &dev_priv->context_idr,
  188. VMW_RES_CONTEXT, res_free);
  189. if (unlikely(ret != 0)) {
  190. if (res_free == NULL)
  191. kfree(res);
  192. else
  193. res_free(res);
  194. return ret;
  195. }
  196. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  197. if (unlikely(cmd == NULL)) {
  198. DRM_ERROR("Fifo reserve failed.\n");
  199. vmw_resource_unreference(&res);
  200. return -ENOMEM;
  201. }
  202. cmd->header.id = cpu_to_le32(SVGA_3D_CMD_CONTEXT_DEFINE);
  203. cmd->header.size = cpu_to_le32(sizeof(cmd->body));
  204. cmd->body.cid = cpu_to_le32(res->id);
  205. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  206. vmw_resource_activate(res, vmw_hw_context_destroy);
  207. return 0;
  208. }
  209. struct vmw_resource *vmw_context_alloc(struct vmw_private *dev_priv)
  210. {
  211. struct vmw_resource *res = kmalloc(sizeof(*res), GFP_KERNEL);
  212. int ret;
  213. if (unlikely(res == NULL))
  214. return NULL;
  215. ret = vmw_context_init(dev_priv, res, NULL);
  216. return (ret == 0) ? res : NULL;
  217. }
  218. /**
  219. * User-space context management:
  220. */
  221. static void vmw_user_context_free(struct vmw_resource *res)
  222. {
  223. struct vmw_user_context *ctx =
  224. container_of(res, struct vmw_user_context, res);
  225. kfree(ctx);
  226. }
  227. /**
  228. * This function is called when user space has no more references on the
  229. * base object. It releases the base-object's reference on the resource object.
  230. */
  231. static void vmw_user_context_base_release(struct ttm_base_object **p_base)
  232. {
  233. struct ttm_base_object *base = *p_base;
  234. struct vmw_user_context *ctx =
  235. container_of(base, struct vmw_user_context, base);
  236. struct vmw_resource *res = &ctx->res;
  237. *p_base = NULL;
  238. vmw_resource_unreference(&res);
  239. }
  240. int vmw_context_destroy_ioctl(struct drm_device *dev, void *data,
  241. struct drm_file *file_priv)
  242. {
  243. struct vmw_private *dev_priv = vmw_priv(dev);
  244. struct vmw_resource *res;
  245. struct vmw_user_context *ctx;
  246. struct drm_vmw_context_arg *arg = (struct drm_vmw_context_arg *)data;
  247. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  248. int ret = 0;
  249. res = vmw_resource_lookup(dev_priv, &dev_priv->context_idr, arg->cid);
  250. if (unlikely(res == NULL))
  251. return -EINVAL;
  252. if (res->res_free != &vmw_user_context_free) {
  253. ret = -EINVAL;
  254. goto out;
  255. }
  256. ctx = container_of(res, struct vmw_user_context, res);
  257. if (ctx->base.tfile != tfile && !ctx->base.shareable) {
  258. ret = -EPERM;
  259. goto out;
  260. }
  261. ttm_ref_object_base_unref(tfile, ctx->base.hash.key, TTM_REF_USAGE);
  262. out:
  263. vmw_resource_unreference(&res);
  264. return ret;
  265. }
  266. int vmw_context_define_ioctl(struct drm_device *dev, void *data,
  267. struct drm_file *file_priv)
  268. {
  269. struct vmw_private *dev_priv = vmw_priv(dev);
  270. struct vmw_user_context *ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  271. struct vmw_resource *res;
  272. struct vmw_resource *tmp;
  273. struct drm_vmw_context_arg *arg = (struct drm_vmw_context_arg *)data;
  274. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  275. int ret;
  276. if (unlikely(ctx == NULL))
  277. return -ENOMEM;
  278. res = &ctx->res;
  279. ctx->base.shareable = false;
  280. ctx->base.tfile = NULL;
  281. ret = vmw_context_init(dev_priv, res, vmw_user_context_free);
  282. if (unlikely(ret != 0))
  283. return ret;
  284. tmp = vmw_resource_reference(&ctx->res);
  285. ret = ttm_base_object_init(tfile, &ctx->base, false, VMW_RES_CONTEXT,
  286. &vmw_user_context_base_release, NULL);
  287. if (unlikely(ret != 0)) {
  288. vmw_resource_unreference(&tmp);
  289. goto out_err;
  290. }
  291. arg->cid = res->id;
  292. out_err:
  293. vmw_resource_unreference(&res);
  294. return ret;
  295. }
  296. int vmw_context_check(struct vmw_private *dev_priv,
  297. struct ttm_object_file *tfile,
  298. int id)
  299. {
  300. struct vmw_resource *res;
  301. int ret = 0;
  302. read_lock(&dev_priv->resource_lock);
  303. res = idr_find(&dev_priv->context_idr, id);
  304. if (res && res->avail) {
  305. struct vmw_user_context *ctx =
  306. container_of(res, struct vmw_user_context, res);
  307. if (ctx->base.tfile != tfile && !ctx->base.shareable)
  308. ret = -EPERM;
  309. } else
  310. ret = -EINVAL;
  311. read_unlock(&dev_priv->resource_lock);
  312. return ret;
  313. }
  314. /**
  315. * Surface management.
  316. */
  317. static void vmw_hw_surface_destroy(struct vmw_resource *res)
  318. {
  319. struct vmw_private *dev_priv = res->dev_priv;
  320. struct {
  321. SVGA3dCmdHeader header;
  322. SVGA3dCmdDestroySurface body;
  323. } *cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  324. if (unlikely(cmd == NULL)) {
  325. DRM_ERROR("Failed reserving FIFO space for surface "
  326. "destruction.\n");
  327. return;
  328. }
  329. cmd->header.id = cpu_to_le32(SVGA_3D_CMD_SURFACE_DESTROY);
  330. cmd->header.size = cpu_to_le32(sizeof(cmd->body));
  331. cmd->body.sid = cpu_to_le32(res->id);
  332. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  333. }
  334. void vmw_surface_res_free(struct vmw_resource *res)
  335. {
  336. struct vmw_surface *srf = container_of(res, struct vmw_surface, res);
  337. kfree(srf->sizes);
  338. kfree(srf->snooper.image);
  339. kfree(srf);
  340. }
  341. int vmw_surface_init(struct vmw_private *dev_priv,
  342. struct vmw_surface *srf,
  343. void (*res_free) (struct vmw_resource *res))
  344. {
  345. int ret;
  346. struct {
  347. SVGA3dCmdHeader header;
  348. SVGA3dCmdDefineSurface body;
  349. } *cmd;
  350. SVGA3dSize *cmd_size;
  351. struct vmw_resource *res = &srf->res;
  352. struct drm_vmw_size *src_size;
  353. size_t submit_size;
  354. uint32_t cmd_len;
  355. int i;
  356. BUG_ON(res_free == NULL);
  357. ret = vmw_resource_init(dev_priv, res, &dev_priv->surface_idr,
  358. VMW_RES_SURFACE, res_free);
  359. if (unlikely(ret != 0)) {
  360. res_free(res);
  361. return ret;
  362. }
  363. submit_size = sizeof(*cmd) + srf->num_sizes * sizeof(SVGA3dSize);
  364. cmd_len = sizeof(cmd->body) + srf->num_sizes * sizeof(SVGA3dSize);
  365. cmd = vmw_fifo_reserve(dev_priv, submit_size);
  366. if (unlikely(cmd == NULL)) {
  367. DRM_ERROR("Fifo reserve failed for create surface.\n");
  368. vmw_resource_unreference(&res);
  369. return -ENOMEM;
  370. }
  371. cmd->header.id = cpu_to_le32(SVGA_3D_CMD_SURFACE_DEFINE);
  372. cmd->header.size = cpu_to_le32(cmd_len);
  373. cmd->body.sid = cpu_to_le32(res->id);
  374. cmd->body.surfaceFlags = cpu_to_le32(srf->flags);
  375. cmd->body.format = cpu_to_le32(srf->format);
  376. for (i = 0; i < DRM_VMW_MAX_SURFACE_FACES; ++i) {
  377. cmd->body.face[i].numMipLevels =
  378. cpu_to_le32(srf->mip_levels[i]);
  379. }
  380. cmd += 1;
  381. cmd_size = (SVGA3dSize *) cmd;
  382. src_size = srf->sizes;
  383. for (i = 0; i < srf->num_sizes; ++i, cmd_size++, src_size++) {
  384. cmd_size->width = cpu_to_le32(src_size->width);
  385. cmd_size->height = cpu_to_le32(src_size->height);
  386. cmd_size->depth = cpu_to_le32(src_size->depth);
  387. }
  388. vmw_fifo_commit(dev_priv, submit_size);
  389. vmw_resource_activate(res, vmw_hw_surface_destroy);
  390. return 0;
  391. }
  392. static void vmw_user_surface_free(struct vmw_resource *res)
  393. {
  394. struct vmw_surface *srf = container_of(res, struct vmw_surface, res);
  395. struct vmw_user_surface *user_srf =
  396. container_of(srf, struct vmw_user_surface, srf);
  397. kfree(srf->sizes);
  398. kfree(srf->snooper.image);
  399. kfree(user_srf);
  400. }
  401. int vmw_user_surface_lookup(struct vmw_private *dev_priv,
  402. struct ttm_object_file *tfile,
  403. int sid, struct vmw_surface **out)
  404. {
  405. struct vmw_resource *res;
  406. struct vmw_surface *srf;
  407. struct vmw_user_surface *user_srf;
  408. res = vmw_resource_lookup(dev_priv, &dev_priv->surface_idr, sid);
  409. if (unlikely(res == NULL))
  410. return -EINVAL;
  411. if (res->res_free != &vmw_user_surface_free)
  412. return -EINVAL;
  413. srf = container_of(res, struct vmw_surface, res);
  414. user_srf = container_of(srf, struct vmw_user_surface, srf);
  415. if (user_srf->base.tfile != tfile && !user_srf->base.shareable)
  416. return -EPERM;
  417. *out = srf;
  418. return 0;
  419. }
  420. static void vmw_user_surface_base_release(struct ttm_base_object **p_base)
  421. {
  422. struct ttm_base_object *base = *p_base;
  423. struct vmw_user_surface *user_srf =
  424. container_of(base, struct vmw_user_surface, base);
  425. struct vmw_resource *res = &user_srf->srf.res;
  426. *p_base = NULL;
  427. vmw_resource_unreference(&res);
  428. }
  429. int vmw_surface_destroy_ioctl(struct drm_device *dev, void *data,
  430. struct drm_file *file_priv)
  431. {
  432. struct vmw_private *dev_priv = vmw_priv(dev);
  433. struct vmw_resource *res;
  434. struct vmw_surface *srf;
  435. struct vmw_user_surface *user_srf;
  436. struct drm_vmw_surface_arg *arg = (struct drm_vmw_surface_arg *)data;
  437. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  438. int ret = 0;
  439. res = vmw_resource_lookup(dev_priv, &dev_priv->surface_idr, arg->sid);
  440. if (unlikely(res == NULL))
  441. return -EINVAL;
  442. if (res->res_free != &vmw_user_surface_free) {
  443. ret = -EINVAL;
  444. goto out;
  445. }
  446. srf = container_of(res, struct vmw_surface, res);
  447. user_srf = container_of(srf, struct vmw_user_surface, srf);
  448. if (user_srf->base.tfile != tfile && !user_srf->base.shareable) {
  449. ret = -EPERM;
  450. goto out;
  451. }
  452. ttm_ref_object_base_unref(tfile, user_srf->base.hash.key,
  453. TTM_REF_USAGE);
  454. out:
  455. vmw_resource_unreference(&res);
  456. return ret;
  457. }
  458. int vmw_surface_define_ioctl(struct drm_device *dev, void *data,
  459. struct drm_file *file_priv)
  460. {
  461. struct vmw_private *dev_priv = vmw_priv(dev);
  462. struct vmw_user_surface *user_srf =
  463. kmalloc(sizeof(*user_srf), GFP_KERNEL);
  464. struct vmw_surface *srf;
  465. struct vmw_resource *res;
  466. struct vmw_resource *tmp;
  467. union drm_vmw_surface_create_arg *arg =
  468. (union drm_vmw_surface_create_arg *)data;
  469. struct drm_vmw_surface_create_req *req = &arg->req;
  470. struct drm_vmw_surface_arg *rep = &arg->rep;
  471. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  472. struct drm_vmw_size __user *user_sizes;
  473. int ret;
  474. int i;
  475. if (unlikely(user_srf == NULL))
  476. return -ENOMEM;
  477. srf = &user_srf->srf;
  478. res = &srf->res;
  479. srf->flags = req->flags;
  480. srf->format = req->format;
  481. memcpy(srf->mip_levels, req->mip_levels, sizeof(srf->mip_levels));
  482. srf->num_sizes = 0;
  483. for (i = 0; i < DRM_VMW_MAX_SURFACE_FACES; ++i)
  484. srf->num_sizes += srf->mip_levels[i];
  485. if (srf->num_sizes > DRM_VMW_MAX_SURFACE_FACES *
  486. DRM_VMW_MAX_MIP_LEVELS) {
  487. ret = -EINVAL;
  488. goto out_err0;
  489. }
  490. srf->sizes = kmalloc(srf->num_sizes * sizeof(*srf->sizes), GFP_KERNEL);
  491. if (unlikely(srf->sizes == NULL)) {
  492. ret = -ENOMEM;
  493. goto out_err0;
  494. }
  495. user_sizes = (struct drm_vmw_size __user *)(unsigned long)
  496. req->size_addr;
  497. ret = copy_from_user(srf->sizes, user_sizes,
  498. srf->num_sizes * sizeof(*srf->sizes));
  499. if (unlikely(ret != 0))
  500. goto out_err1;
  501. user_srf->base.shareable = false;
  502. user_srf->base.tfile = NULL;
  503. /**
  504. * From this point, the generic resource management functions
  505. * destroy the object on failure.
  506. */
  507. ret = vmw_surface_init(dev_priv, srf, vmw_user_surface_free);
  508. if (unlikely(ret != 0))
  509. return ret;
  510. tmp = vmw_resource_reference(&srf->res);
  511. ret = ttm_base_object_init(tfile, &user_srf->base,
  512. req->shareable, VMW_RES_SURFACE,
  513. &vmw_user_surface_base_release, NULL);
  514. if (unlikely(ret != 0)) {
  515. vmw_resource_unreference(&tmp);
  516. vmw_resource_unreference(&res);
  517. return ret;
  518. }
  519. if (srf->flags & (1 << 9) &&
  520. srf->num_sizes == 1 &&
  521. srf->sizes[0].width == 64 &&
  522. srf->sizes[0].height == 64 &&
  523. srf->format == SVGA3D_A8R8G8B8) {
  524. srf->snooper.image = kmalloc(64 * 64 * 4, GFP_KERNEL);
  525. /* clear the image */
  526. if (srf->snooper.image)
  527. memset(srf->snooper.image, 0x00, 64 * 64 * 4);
  528. else
  529. DRM_ERROR("Failed to allocate cursor_image\n");
  530. } else {
  531. srf->snooper.image = NULL;
  532. }
  533. srf->snooper.crtc = NULL;
  534. rep->sid = res->id;
  535. vmw_resource_unreference(&res);
  536. return 0;
  537. out_err1:
  538. kfree(srf->sizes);
  539. out_err0:
  540. kfree(user_srf);
  541. return ret;
  542. }
  543. int vmw_surface_reference_ioctl(struct drm_device *dev, void *data,
  544. struct drm_file *file_priv)
  545. {
  546. struct vmw_private *dev_priv = vmw_priv(dev);
  547. union drm_vmw_surface_reference_arg *arg =
  548. (union drm_vmw_surface_reference_arg *)data;
  549. struct drm_vmw_surface_arg *req = &arg->req;
  550. struct drm_vmw_surface_create_req *rep = &arg->rep;
  551. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  552. struct vmw_resource *res;
  553. struct vmw_surface *srf;
  554. struct vmw_user_surface *user_srf;
  555. struct drm_vmw_size __user *user_sizes;
  556. int ret;
  557. res = vmw_resource_lookup(dev_priv, &dev_priv->surface_idr, req->sid);
  558. if (unlikely(res == NULL))
  559. return -EINVAL;
  560. if (res->res_free != &vmw_user_surface_free) {
  561. ret = -EINVAL;
  562. goto out;
  563. }
  564. srf = container_of(res, struct vmw_surface, res);
  565. user_srf = container_of(srf, struct vmw_user_surface, srf);
  566. if (user_srf->base.tfile != tfile && !user_srf->base.shareable) {
  567. DRM_ERROR("Tried to reference none shareable surface\n");
  568. ret = -EPERM;
  569. goto out;
  570. }
  571. ret = ttm_ref_object_add(tfile, &user_srf->base, TTM_REF_USAGE, NULL);
  572. if (unlikely(ret != 0)) {
  573. DRM_ERROR("Could not add a reference to a surface.\n");
  574. goto out;
  575. }
  576. rep->flags = srf->flags;
  577. rep->format = srf->format;
  578. memcpy(rep->mip_levels, srf->mip_levels, sizeof(srf->mip_levels));
  579. user_sizes = (struct drm_vmw_size __user *)(unsigned long)
  580. rep->size_addr;
  581. if (user_sizes)
  582. ret = copy_to_user(user_sizes, srf->sizes,
  583. srf->num_sizes * sizeof(*srf->sizes));
  584. if (unlikely(ret != 0)) {
  585. DRM_ERROR("copy_to_user failed %p %u\n",
  586. user_sizes, srf->num_sizes);
  587. /**
  588. * FIXME: Unreference surface here?
  589. */
  590. goto out;
  591. }
  592. out:
  593. vmw_resource_unreference(&res);
  594. return ret;
  595. }
  596. int vmw_surface_check(struct vmw_private *dev_priv,
  597. struct ttm_object_file *tfile,
  598. int id)
  599. {
  600. struct vmw_resource *res;
  601. int ret = 0;
  602. read_lock(&dev_priv->resource_lock);
  603. res = idr_find(&dev_priv->surface_idr, id);
  604. if (res && res->avail) {
  605. struct vmw_surface *srf =
  606. container_of(res, struct vmw_surface, res);
  607. struct vmw_user_surface *usrf =
  608. container_of(srf, struct vmw_user_surface, srf);
  609. if (usrf->base.tfile != tfile && !usrf->base.shareable)
  610. ret = -EPERM;
  611. } else
  612. ret = -EINVAL;
  613. read_unlock(&dev_priv->resource_lock);
  614. return ret;
  615. }
  616. /**
  617. * Buffer management.
  618. */
  619. static size_t vmw_dmabuf_acc_size(struct ttm_bo_global *glob,
  620. unsigned long num_pages)
  621. {
  622. static size_t bo_user_size = ~0;
  623. size_t page_array_size =
  624. (num_pages * sizeof(void *) + PAGE_SIZE - 1) & PAGE_MASK;
  625. if (unlikely(bo_user_size == ~0)) {
  626. bo_user_size = glob->ttm_bo_extra_size +
  627. ttm_round_pot(sizeof(struct vmw_dma_buffer));
  628. }
  629. return bo_user_size + page_array_size;
  630. }
  631. void vmw_dmabuf_bo_free(struct ttm_buffer_object *bo)
  632. {
  633. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  634. struct ttm_bo_global *glob = bo->glob;
  635. struct vmw_private *dev_priv =
  636. container_of(bo->bdev, struct vmw_private, bdev);
  637. ttm_mem_global_free(glob->mem_glob, bo->acc_size);
  638. if (vmw_bo->gmr_bound) {
  639. vmw_gmr_unbind(dev_priv, vmw_bo->gmr_id);
  640. spin_lock(&glob->lru_lock);
  641. ida_remove(&dev_priv->gmr_ida, vmw_bo->gmr_id);
  642. spin_unlock(&glob->lru_lock);
  643. }
  644. kfree(vmw_bo);
  645. }
  646. int vmw_dmabuf_init(struct vmw_private *dev_priv,
  647. struct vmw_dma_buffer *vmw_bo,
  648. size_t size, struct ttm_placement *placement,
  649. bool interruptible,
  650. void (*bo_free) (struct ttm_buffer_object *bo))
  651. {
  652. struct ttm_bo_device *bdev = &dev_priv->bdev;
  653. struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
  654. size_t acc_size;
  655. int ret;
  656. BUG_ON(!bo_free);
  657. acc_size =
  658. vmw_dmabuf_acc_size(bdev->glob,
  659. (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
  660. ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
  661. if (unlikely(ret != 0)) {
  662. /* we must free the bo here as
  663. * ttm_buffer_object_init does so as well */
  664. bo_free(&vmw_bo->base);
  665. return ret;
  666. }
  667. memset(vmw_bo, 0, sizeof(*vmw_bo));
  668. INIT_LIST_HEAD(&vmw_bo->gmr_lru);
  669. INIT_LIST_HEAD(&vmw_bo->validate_list);
  670. vmw_bo->gmr_id = 0;
  671. vmw_bo->gmr_bound = false;
  672. ret = ttm_bo_init(bdev, &vmw_bo->base, size,
  673. ttm_bo_type_device, placement,
  674. 0, 0, interruptible,
  675. NULL, acc_size, bo_free);
  676. return ret;
  677. }
  678. static void vmw_user_dmabuf_destroy(struct ttm_buffer_object *bo)
  679. {
  680. struct vmw_user_dma_buffer *vmw_user_bo = vmw_user_dma_buffer(bo);
  681. struct vmw_dma_buffer *vmw_bo = &vmw_user_bo->dma;
  682. struct ttm_bo_global *glob = bo->glob;
  683. struct vmw_private *dev_priv =
  684. container_of(bo->bdev, struct vmw_private, bdev);
  685. ttm_mem_global_free(glob->mem_glob, bo->acc_size);
  686. if (vmw_bo->gmr_bound) {
  687. vmw_gmr_unbind(dev_priv, vmw_bo->gmr_id);
  688. spin_lock(&glob->lru_lock);
  689. ida_remove(&dev_priv->gmr_ida, vmw_bo->gmr_id);
  690. spin_unlock(&glob->lru_lock);
  691. }
  692. kfree(vmw_user_bo);
  693. }
  694. static void vmw_user_dmabuf_release(struct ttm_base_object **p_base)
  695. {
  696. struct vmw_user_dma_buffer *vmw_user_bo;
  697. struct ttm_base_object *base = *p_base;
  698. struct ttm_buffer_object *bo;
  699. *p_base = NULL;
  700. if (unlikely(base == NULL))
  701. return;
  702. vmw_user_bo = container_of(base, struct vmw_user_dma_buffer, base);
  703. bo = &vmw_user_bo->dma.base;
  704. ttm_bo_unref(&bo);
  705. }
  706. int vmw_dmabuf_alloc_ioctl(struct drm_device *dev, void *data,
  707. struct drm_file *file_priv)
  708. {
  709. struct vmw_private *dev_priv = vmw_priv(dev);
  710. union drm_vmw_alloc_dmabuf_arg *arg =
  711. (union drm_vmw_alloc_dmabuf_arg *)data;
  712. struct drm_vmw_alloc_dmabuf_req *req = &arg->req;
  713. struct drm_vmw_dmabuf_rep *rep = &arg->rep;
  714. struct vmw_user_dma_buffer *vmw_user_bo;
  715. struct ttm_buffer_object *tmp;
  716. struct vmw_master *vmaster = vmw_master(file_priv->master);
  717. int ret;
  718. vmw_user_bo = kzalloc(sizeof(*vmw_user_bo), GFP_KERNEL);
  719. if (unlikely(vmw_user_bo == NULL))
  720. return -ENOMEM;
  721. ret = ttm_read_lock(&vmaster->lock, true);
  722. if (unlikely(ret != 0)) {
  723. kfree(vmw_user_bo);
  724. return ret;
  725. }
  726. ret = vmw_dmabuf_init(dev_priv, &vmw_user_bo->dma, req->size,
  727. &vmw_vram_placement, true,
  728. &vmw_user_dmabuf_destroy);
  729. if (unlikely(ret != 0))
  730. return ret;
  731. tmp = ttm_bo_reference(&vmw_user_bo->dma.base);
  732. ret = ttm_base_object_init(vmw_fpriv(file_priv)->tfile,
  733. &vmw_user_bo->base,
  734. false,
  735. ttm_buffer_type,
  736. &vmw_user_dmabuf_release, NULL);
  737. if (unlikely(ret != 0)) {
  738. ttm_bo_unref(&tmp);
  739. } else {
  740. rep->handle = vmw_user_bo->base.hash.key;
  741. rep->map_handle = vmw_user_bo->dma.base.addr_space_offset;
  742. rep->cur_gmr_id = vmw_user_bo->base.hash.key;
  743. rep->cur_gmr_offset = 0;
  744. }
  745. ttm_bo_unref(&tmp);
  746. ttm_read_unlock(&vmaster->lock);
  747. return 0;
  748. }
  749. int vmw_dmabuf_unref_ioctl(struct drm_device *dev, void *data,
  750. struct drm_file *file_priv)
  751. {
  752. struct drm_vmw_unref_dmabuf_arg *arg =
  753. (struct drm_vmw_unref_dmabuf_arg *)data;
  754. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  755. arg->handle,
  756. TTM_REF_USAGE);
  757. }
  758. uint32_t vmw_dmabuf_validate_node(struct ttm_buffer_object *bo,
  759. uint32_t cur_validate_node)
  760. {
  761. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  762. if (likely(vmw_bo->on_validate_list))
  763. return vmw_bo->cur_validate_node;
  764. vmw_bo->cur_validate_node = cur_validate_node;
  765. vmw_bo->on_validate_list = true;
  766. return cur_validate_node;
  767. }
  768. void vmw_dmabuf_validate_clear(struct ttm_buffer_object *bo)
  769. {
  770. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  771. vmw_bo->on_validate_list = false;
  772. }
  773. uint32_t vmw_dmabuf_gmr(struct ttm_buffer_object *bo)
  774. {
  775. struct vmw_dma_buffer *vmw_bo;
  776. if (bo->mem.mem_type == TTM_PL_VRAM)
  777. return SVGA_GMR_FRAMEBUFFER;
  778. vmw_bo = vmw_dma_buffer(bo);
  779. return (vmw_bo->gmr_bound) ? vmw_bo->gmr_id : SVGA_GMR_NULL;
  780. }
  781. void vmw_dmabuf_set_gmr(struct ttm_buffer_object *bo, uint32_t id)
  782. {
  783. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  784. vmw_bo->gmr_bound = true;
  785. vmw_bo->gmr_id = id;
  786. }
  787. int vmw_user_dmabuf_lookup(struct ttm_object_file *tfile,
  788. uint32_t handle, struct vmw_dma_buffer **out)
  789. {
  790. struct vmw_user_dma_buffer *vmw_user_bo;
  791. struct ttm_base_object *base;
  792. base = ttm_base_object_lookup(tfile, handle);
  793. if (unlikely(base == NULL)) {
  794. printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
  795. (unsigned long)handle);
  796. return -ESRCH;
  797. }
  798. if (unlikely(base->object_type != ttm_buffer_type)) {
  799. ttm_base_object_unref(&base);
  800. printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
  801. (unsigned long)handle);
  802. return -EINVAL;
  803. }
  804. vmw_user_bo = container_of(base, struct vmw_user_dma_buffer, base);
  805. (void)ttm_bo_reference(&vmw_user_bo->dma.base);
  806. ttm_base_object_unref(&base);
  807. *out = &vmw_user_bo->dma;
  808. return 0;
  809. }
  810. /**
  811. * TODO: Implement a gmr id eviction mechanism. Currently we just fail
  812. * when we're out of ids, causing GMR space to be allocated
  813. * out of VRAM.
  814. */
  815. int vmw_gmr_id_alloc(struct vmw_private *dev_priv, uint32_t *p_id)
  816. {
  817. struct ttm_bo_global *glob = dev_priv->bdev.glob;
  818. int id;
  819. int ret;
  820. do {
  821. if (unlikely(ida_pre_get(&dev_priv->gmr_ida, GFP_KERNEL) == 0))
  822. return -ENOMEM;
  823. spin_lock(&glob->lru_lock);
  824. ret = ida_get_new(&dev_priv->gmr_ida, &id);
  825. spin_unlock(&glob->lru_lock);
  826. } while (ret == -EAGAIN);
  827. if (unlikely(ret != 0))
  828. return ret;
  829. if (unlikely(id >= dev_priv->max_gmr_ids)) {
  830. spin_lock(&glob->lru_lock);
  831. ida_remove(&dev_priv->gmr_ida, id);
  832. spin_unlock(&glob->lru_lock);
  833. return -EBUSY;
  834. }
  835. *p_id = (uint32_t) id;
  836. return 0;
  837. }
  838. /*
  839. * Stream managment
  840. */
  841. static void vmw_stream_destroy(struct vmw_resource *res)
  842. {
  843. struct vmw_private *dev_priv = res->dev_priv;
  844. struct vmw_stream *stream;
  845. int ret;
  846. DRM_INFO("%s: unref\n", __func__);
  847. stream = container_of(res, struct vmw_stream, res);
  848. ret = vmw_overlay_unref(dev_priv, stream->stream_id);
  849. WARN_ON(ret != 0);
  850. }
  851. static int vmw_stream_init(struct vmw_private *dev_priv,
  852. struct vmw_stream *stream,
  853. void (*res_free) (struct vmw_resource *res))
  854. {
  855. struct vmw_resource *res = &stream->res;
  856. int ret;
  857. ret = vmw_resource_init(dev_priv, res, &dev_priv->stream_idr,
  858. VMW_RES_STREAM, res_free);
  859. if (unlikely(ret != 0)) {
  860. if (res_free == NULL)
  861. kfree(stream);
  862. else
  863. res_free(&stream->res);
  864. return ret;
  865. }
  866. ret = vmw_overlay_claim(dev_priv, &stream->stream_id);
  867. if (ret) {
  868. vmw_resource_unreference(&res);
  869. return ret;
  870. }
  871. DRM_INFO("%s: claimed\n", __func__);
  872. vmw_resource_activate(&stream->res, vmw_stream_destroy);
  873. return 0;
  874. }
  875. /**
  876. * User-space context management:
  877. */
  878. static void vmw_user_stream_free(struct vmw_resource *res)
  879. {
  880. struct vmw_user_stream *stream =
  881. container_of(res, struct vmw_user_stream, stream.res);
  882. kfree(stream);
  883. }
  884. /**
  885. * This function is called when user space has no more references on the
  886. * base object. It releases the base-object's reference on the resource object.
  887. */
  888. static void vmw_user_stream_base_release(struct ttm_base_object **p_base)
  889. {
  890. struct ttm_base_object *base = *p_base;
  891. struct vmw_user_stream *stream =
  892. container_of(base, struct vmw_user_stream, base);
  893. struct vmw_resource *res = &stream->stream.res;
  894. *p_base = NULL;
  895. vmw_resource_unreference(&res);
  896. }
  897. int vmw_stream_unref_ioctl(struct drm_device *dev, void *data,
  898. struct drm_file *file_priv)
  899. {
  900. struct vmw_private *dev_priv = vmw_priv(dev);
  901. struct vmw_resource *res;
  902. struct vmw_user_stream *stream;
  903. struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
  904. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  905. int ret = 0;
  906. res = vmw_resource_lookup(dev_priv, &dev_priv->stream_idr, arg->stream_id);
  907. if (unlikely(res == NULL))
  908. return -EINVAL;
  909. if (res->res_free != &vmw_user_stream_free) {
  910. ret = -EINVAL;
  911. goto out;
  912. }
  913. stream = container_of(res, struct vmw_user_stream, stream.res);
  914. if (stream->base.tfile != tfile) {
  915. ret = -EINVAL;
  916. goto out;
  917. }
  918. ttm_ref_object_base_unref(tfile, stream->base.hash.key, TTM_REF_USAGE);
  919. out:
  920. vmw_resource_unreference(&res);
  921. return ret;
  922. }
  923. int vmw_stream_claim_ioctl(struct drm_device *dev, void *data,
  924. struct drm_file *file_priv)
  925. {
  926. struct vmw_private *dev_priv = vmw_priv(dev);
  927. struct vmw_user_stream *stream = kmalloc(sizeof(*stream), GFP_KERNEL);
  928. struct vmw_resource *res;
  929. struct vmw_resource *tmp;
  930. struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
  931. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  932. int ret;
  933. if (unlikely(stream == NULL))
  934. return -ENOMEM;
  935. res = &stream->stream.res;
  936. stream->base.shareable = false;
  937. stream->base.tfile = NULL;
  938. ret = vmw_stream_init(dev_priv, &stream->stream, vmw_user_stream_free);
  939. if (unlikely(ret != 0))
  940. return ret;
  941. tmp = vmw_resource_reference(res);
  942. ret = ttm_base_object_init(tfile, &stream->base, false, VMW_RES_STREAM,
  943. &vmw_user_stream_base_release, NULL);
  944. if (unlikely(ret != 0)) {
  945. vmw_resource_unreference(&tmp);
  946. goto out_err;
  947. }
  948. arg->stream_id = res->id;
  949. out_err:
  950. vmw_resource_unreference(&res);
  951. return ret;
  952. }
  953. int vmw_user_stream_lookup(struct vmw_private *dev_priv,
  954. struct ttm_object_file *tfile,
  955. uint32_t *inout_id, struct vmw_resource **out)
  956. {
  957. struct vmw_user_stream *stream;
  958. struct vmw_resource *res;
  959. int ret;
  960. res = vmw_resource_lookup(dev_priv, &dev_priv->stream_idr, *inout_id);
  961. if (unlikely(res == NULL))
  962. return -EINVAL;
  963. if (res->res_free != &vmw_user_stream_free) {
  964. ret = -EINVAL;
  965. goto err_ref;
  966. }
  967. stream = container_of(res, struct vmw_user_stream, stream.res);
  968. if (stream->base.tfile != tfile) {
  969. ret = -EPERM;
  970. goto err_ref;
  971. }
  972. *inout_id = stream->stream.stream_id;
  973. *out = res;
  974. return 0;
  975. err_ref:
  976. vmw_resource_unreference(&res);
  977. return ret;
  978. }