vmwgfx_resource.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  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. vmw_3d_resource_dec(dev_priv);
  178. }
  179. static int vmw_context_init(struct vmw_private *dev_priv,
  180. struct vmw_resource *res,
  181. void (*res_free) (struct vmw_resource *res))
  182. {
  183. int ret;
  184. struct {
  185. SVGA3dCmdHeader header;
  186. SVGA3dCmdDefineContext body;
  187. } *cmd;
  188. ret = vmw_resource_init(dev_priv, res, &dev_priv->context_idr,
  189. VMW_RES_CONTEXT, res_free);
  190. if (unlikely(ret != 0)) {
  191. if (res_free == NULL)
  192. kfree(res);
  193. else
  194. res_free(res);
  195. return ret;
  196. }
  197. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  198. if (unlikely(cmd == NULL)) {
  199. DRM_ERROR("Fifo reserve failed.\n");
  200. vmw_resource_unreference(&res);
  201. return -ENOMEM;
  202. }
  203. cmd->header.id = cpu_to_le32(SVGA_3D_CMD_CONTEXT_DEFINE);
  204. cmd->header.size = cpu_to_le32(sizeof(cmd->body));
  205. cmd->body.cid = cpu_to_le32(res->id);
  206. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  207. (void) vmw_3d_resource_inc(dev_priv);
  208. vmw_resource_activate(res, vmw_hw_context_destroy);
  209. return 0;
  210. }
  211. struct vmw_resource *vmw_context_alloc(struct vmw_private *dev_priv)
  212. {
  213. struct vmw_resource *res = kmalloc(sizeof(*res), GFP_KERNEL);
  214. int ret;
  215. if (unlikely(res == NULL))
  216. return NULL;
  217. ret = vmw_context_init(dev_priv, res, NULL);
  218. return (ret == 0) ? res : NULL;
  219. }
  220. /**
  221. * User-space context management:
  222. */
  223. static void vmw_user_context_free(struct vmw_resource *res)
  224. {
  225. struct vmw_user_context *ctx =
  226. container_of(res, struct vmw_user_context, res);
  227. kfree(ctx);
  228. }
  229. /**
  230. * This function is called when user space has no more references on the
  231. * base object. It releases the base-object's reference on the resource object.
  232. */
  233. static void vmw_user_context_base_release(struct ttm_base_object **p_base)
  234. {
  235. struct ttm_base_object *base = *p_base;
  236. struct vmw_user_context *ctx =
  237. container_of(base, struct vmw_user_context, base);
  238. struct vmw_resource *res = &ctx->res;
  239. *p_base = NULL;
  240. vmw_resource_unreference(&res);
  241. }
  242. int vmw_context_destroy_ioctl(struct drm_device *dev, void *data,
  243. struct drm_file *file_priv)
  244. {
  245. struct vmw_private *dev_priv = vmw_priv(dev);
  246. struct vmw_resource *res;
  247. struct vmw_user_context *ctx;
  248. struct drm_vmw_context_arg *arg = (struct drm_vmw_context_arg *)data;
  249. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  250. int ret = 0;
  251. res = vmw_resource_lookup(dev_priv, &dev_priv->context_idr, arg->cid);
  252. if (unlikely(res == NULL))
  253. return -EINVAL;
  254. if (res->res_free != &vmw_user_context_free) {
  255. ret = -EINVAL;
  256. goto out;
  257. }
  258. ctx = container_of(res, struct vmw_user_context, res);
  259. if (ctx->base.tfile != tfile && !ctx->base.shareable) {
  260. ret = -EPERM;
  261. goto out;
  262. }
  263. ttm_ref_object_base_unref(tfile, ctx->base.hash.key, TTM_REF_USAGE);
  264. out:
  265. vmw_resource_unreference(&res);
  266. return ret;
  267. }
  268. int vmw_context_define_ioctl(struct drm_device *dev, void *data,
  269. struct drm_file *file_priv)
  270. {
  271. struct vmw_private *dev_priv = vmw_priv(dev);
  272. struct vmw_user_context *ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  273. struct vmw_resource *res;
  274. struct vmw_resource *tmp;
  275. struct drm_vmw_context_arg *arg = (struct drm_vmw_context_arg *)data;
  276. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  277. int ret;
  278. if (unlikely(ctx == NULL))
  279. return -ENOMEM;
  280. res = &ctx->res;
  281. ctx->base.shareable = false;
  282. ctx->base.tfile = NULL;
  283. ret = vmw_context_init(dev_priv, res, vmw_user_context_free);
  284. if (unlikely(ret != 0))
  285. return ret;
  286. tmp = vmw_resource_reference(&ctx->res);
  287. ret = ttm_base_object_init(tfile, &ctx->base, false, VMW_RES_CONTEXT,
  288. &vmw_user_context_base_release, NULL);
  289. if (unlikely(ret != 0)) {
  290. vmw_resource_unreference(&tmp);
  291. goto out_err;
  292. }
  293. arg->cid = res->id;
  294. out_err:
  295. vmw_resource_unreference(&res);
  296. return ret;
  297. }
  298. int vmw_context_check(struct vmw_private *dev_priv,
  299. struct ttm_object_file *tfile,
  300. int id,
  301. struct vmw_resource **p_res)
  302. {
  303. struct vmw_resource *res;
  304. int ret = 0;
  305. read_lock(&dev_priv->resource_lock);
  306. res = idr_find(&dev_priv->context_idr, id);
  307. if (res && res->avail) {
  308. struct vmw_user_context *ctx =
  309. container_of(res, struct vmw_user_context, res);
  310. if (ctx->base.tfile != tfile && !ctx->base.shareable)
  311. ret = -EPERM;
  312. if (p_res)
  313. *p_res = vmw_resource_reference(res);
  314. } else
  315. ret = -EINVAL;
  316. read_unlock(&dev_priv->resource_lock);
  317. return ret;
  318. }
  319. /**
  320. * Surface management.
  321. */
  322. static void vmw_hw_surface_destroy(struct vmw_resource *res)
  323. {
  324. struct vmw_private *dev_priv = res->dev_priv;
  325. struct {
  326. SVGA3dCmdHeader header;
  327. SVGA3dCmdDestroySurface body;
  328. } *cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  329. if (unlikely(cmd == NULL)) {
  330. DRM_ERROR("Failed reserving FIFO space for surface "
  331. "destruction.\n");
  332. return;
  333. }
  334. cmd->header.id = cpu_to_le32(SVGA_3D_CMD_SURFACE_DESTROY);
  335. cmd->header.size = cpu_to_le32(sizeof(cmd->body));
  336. cmd->body.sid = cpu_to_le32(res->id);
  337. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  338. vmw_3d_resource_dec(dev_priv);
  339. }
  340. void vmw_surface_res_free(struct vmw_resource *res)
  341. {
  342. struct vmw_surface *srf = container_of(res, struct vmw_surface, res);
  343. kfree(srf->sizes);
  344. kfree(srf->snooper.image);
  345. kfree(srf);
  346. }
  347. int vmw_surface_init(struct vmw_private *dev_priv,
  348. struct vmw_surface *srf,
  349. void (*res_free) (struct vmw_resource *res))
  350. {
  351. int ret;
  352. struct {
  353. SVGA3dCmdHeader header;
  354. SVGA3dCmdDefineSurface body;
  355. } *cmd;
  356. SVGA3dSize *cmd_size;
  357. struct vmw_resource *res = &srf->res;
  358. struct drm_vmw_size *src_size;
  359. size_t submit_size;
  360. uint32_t cmd_len;
  361. int i;
  362. BUG_ON(res_free == NULL);
  363. ret = vmw_resource_init(dev_priv, res, &dev_priv->surface_idr,
  364. VMW_RES_SURFACE, res_free);
  365. if (unlikely(ret != 0)) {
  366. res_free(res);
  367. return ret;
  368. }
  369. submit_size = sizeof(*cmd) + srf->num_sizes * sizeof(SVGA3dSize);
  370. cmd_len = sizeof(cmd->body) + srf->num_sizes * sizeof(SVGA3dSize);
  371. cmd = vmw_fifo_reserve(dev_priv, submit_size);
  372. if (unlikely(cmd == NULL)) {
  373. DRM_ERROR("Fifo reserve failed for create surface.\n");
  374. vmw_resource_unreference(&res);
  375. return -ENOMEM;
  376. }
  377. cmd->header.id = cpu_to_le32(SVGA_3D_CMD_SURFACE_DEFINE);
  378. cmd->header.size = cpu_to_le32(cmd_len);
  379. cmd->body.sid = cpu_to_le32(res->id);
  380. cmd->body.surfaceFlags = cpu_to_le32(srf->flags);
  381. cmd->body.format = cpu_to_le32(srf->format);
  382. for (i = 0; i < DRM_VMW_MAX_SURFACE_FACES; ++i) {
  383. cmd->body.face[i].numMipLevels =
  384. cpu_to_le32(srf->mip_levels[i]);
  385. }
  386. cmd += 1;
  387. cmd_size = (SVGA3dSize *) cmd;
  388. src_size = srf->sizes;
  389. for (i = 0; i < srf->num_sizes; ++i, cmd_size++, src_size++) {
  390. cmd_size->width = cpu_to_le32(src_size->width);
  391. cmd_size->height = cpu_to_le32(src_size->height);
  392. cmd_size->depth = cpu_to_le32(src_size->depth);
  393. }
  394. vmw_fifo_commit(dev_priv, submit_size);
  395. (void) vmw_3d_resource_inc(dev_priv);
  396. vmw_resource_activate(res, vmw_hw_surface_destroy);
  397. return 0;
  398. }
  399. static void vmw_user_surface_free(struct vmw_resource *res)
  400. {
  401. struct vmw_surface *srf = container_of(res, struct vmw_surface, res);
  402. struct vmw_user_surface *user_srf =
  403. container_of(srf, struct vmw_user_surface, srf);
  404. kfree(srf->sizes);
  405. kfree(srf->snooper.image);
  406. kfree(user_srf);
  407. }
  408. int vmw_user_surface_lookup_handle(struct vmw_private *dev_priv,
  409. struct ttm_object_file *tfile,
  410. uint32_t handle, struct vmw_surface **out)
  411. {
  412. struct vmw_resource *res;
  413. struct vmw_surface *srf;
  414. struct vmw_user_surface *user_srf;
  415. struct ttm_base_object *base;
  416. int ret = -EINVAL;
  417. base = ttm_base_object_lookup(tfile, handle);
  418. if (unlikely(base == NULL))
  419. return -EINVAL;
  420. if (unlikely(base->object_type != VMW_RES_SURFACE))
  421. goto out_bad_resource;
  422. user_srf = container_of(base, struct vmw_user_surface, base);
  423. srf = &user_srf->srf;
  424. res = &srf->res;
  425. read_lock(&dev_priv->resource_lock);
  426. if (!res->avail || res->res_free != &vmw_user_surface_free) {
  427. read_unlock(&dev_priv->resource_lock);
  428. goto out_bad_resource;
  429. }
  430. kref_get(&res->kref);
  431. read_unlock(&dev_priv->resource_lock);
  432. *out = srf;
  433. ret = 0;
  434. out_bad_resource:
  435. ttm_base_object_unref(&base);
  436. return ret;
  437. }
  438. static void vmw_user_surface_base_release(struct ttm_base_object **p_base)
  439. {
  440. struct ttm_base_object *base = *p_base;
  441. struct vmw_user_surface *user_srf =
  442. container_of(base, struct vmw_user_surface, base);
  443. struct vmw_resource *res = &user_srf->srf.res;
  444. *p_base = NULL;
  445. vmw_resource_unreference(&res);
  446. }
  447. int vmw_surface_destroy_ioctl(struct drm_device *dev, void *data,
  448. struct drm_file *file_priv)
  449. {
  450. struct drm_vmw_surface_arg *arg = (struct drm_vmw_surface_arg *)data;
  451. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  452. return ttm_ref_object_base_unref(tfile, arg->sid, TTM_REF_USAGE);
  453. }
  454. int vmw_surface_define_ioctl(struct drm_device *dev, void *data,
  455. struct drm_file *file_priv)
  456. {
  457. struct vmw_private *dev_priv = vmw_priv(dev);
  458. struct vmw_user_surface *user_srf =
  459. kmalloc(sizeof(*user_srf), GFP_KERNEL);
  460. struct vmw_surface *srf;
  461. struct vmw_resource *res;
  462. struct vmw_resource *tmp;
  463. union drm_vmw_surface_create_arg *arg =
  464. (union drm_vmw_surface_create_arg *)data;
  465. struct drm_vmw_surface_create_req *req = &arg->req;
  466. struct drm_vmw_surface_arg *rep = &arg->rep;
  467. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  468. struct drm_vmw_size __user *user_sizes;
  469. int ret;
  470. int i;
  471. if (unlikely(user_srf == NULL))
  472. return -ENOMEM;
  473. srf = &user_srf->srf;
  474. res = &srf->res;
  475. srf->flags = req->flags;
  476. srf->format = req->format;
  477. srf->scanout = req->scanout;
  478. memcpy(srf->mip_levels, req->mip_levels, sizeof(srf->mip_levels));
  479. srf->num_sizes = 0;
  480. for (i = 0; i < DRM_VMW_MAX_SURFACE_FACES; ++i)
  481. srf->num_sizes += srf->mip_levels[i];
  482. if (srf->num_sizes > DRM_VMW_MAX_SURFACE_FACES *
  483. DRM_VMW_MAX_MIP_LEVELS) {
  484. ret = -EINVAL;
  485. goto out_err0;
  486. }
  487. srf->sizes = kmalloc(srf->num_sizes * sizeof(*srf->sizes), GFP_KERNEL);
  488. if (unlikely(srf->sizes == NULL)) {
  489. ret = -ENOMEM;
  490. goto out_err0;
  491. }
  492. user_sizes = (struct drm_vmw_size __user *)(unsigned long)
  493. req->size_addr;
  494. ret = copy_from_user(srf->sizes, user_sizes,
  495. srf->num_sizes * sizeof(*srf->sizes));
  496. if (unlikely(ret != 0)) {
  497. ret = -EFAULT;
  498. goto out_err1;
  499. }
  500. if (srf->scanout &&
  501. srf->num_sizes == 1 &&
  502. srf->sizes[0].width == 64 &&
  503. srf->sizes[0].height == 64 &&
  504. srf->format == SVGA3D_A8R8G8B8) {
  505. /* allocate image area and clear it */
  506. srf->snooper.image = kzalloc(64 * 64 * 4, GFP_KERNEL);
  507. if (!srf->snooper.image) {
  508. DRM_ERROR("Failed to allocate cursor_image\n");
  509. ret = -ENOMEM;
  510. goto out_err1;
  511. }
  512. } else {
  513. srf->snooper.image = NULL;
  514. }
  515. srf->snooper.crtc = NULL;
  516. user_srf->base.shareable = false;
  517. user_srf->base.tfile = NULL;
  518. /**
  519. * From this point, the generic resource management functions
  520. * destroy the object on failure.
  521. */
  522. ret = vmw_surface_init(dev_priv, srf, vmw_user_surface_free);
  523. if (unlikely(ret != 0))
  524. return ret;
  525. tmp = vmw_resource_reference(&srf->res);
  526. ret = ttm_base_object_init(tfile, &user_srf->base,
  527. req->shareable, VMW_RES_SURFACE,
  528. &vmw_user_surface_base_release, NULL);
  529. if (unlikely(ret != 0)) {
  530. vmw_resource_unreference(&tmp);
  531. vmw_resource_unreference(&res);
  532. return ret;
  533. }
  534. rep->sid = user_srf->base.hash.key;
  535. if (rep->sid == SVGA3D_INVALID_ID)
  536. DRM_ERROR("Created bad Surface ID.\n");
  537. vmw_resource_unreference(&res);
  538. return 0;
  539. out_err1:
  540. kfree(srf->sizes);
  541. out_err0:
  542. kfree(user_srf);
  543. return ret;
  544. }
  545. int vmw_surface_reference_ioctl(struct drm_device *dev, void *data,
  546. struct drm_file *file_priv)
  547. {
  548. union drm_vmw_surface_reference_arg *arg =
  549. (union drm_vmw_surface_reference_arg *)data;
  550. struct drm_vmw_surface_arg *req = &arg->req;
  551. struct drm_vmw_surface_create_req *rep = &arg->rep;
  552. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  553. struct vmw_surface *srf;
  554. struct vmw_user_surface *user_srf;
  555. struct drm_vmw_size __user *user_sizes;
  556. struct ttm_base_object *base;
  557. int ret = -EINVAL;
  558. base = ttm_base_object_lookup(tfile, req->sid);
  559. if (unlikely(base == NULL)) {
  560. DRM_ERROR("Could not find surface to reference.\n");
  561. return -EINVAL;
  562. }
  563. if (unlikely(base->object_type != VMW_RES_SURFACE))
  564. goto out_bad_resource;
  565. user_srf = container_of(base, struct vmw_user_surface, base);
  566. srf = &user_srf->srf;
  567. ret = ttm_ref_object_add(tfile, &user_srf->base, TTM_REF_USAGE, NULL);
  568. if (unlikely(ret != 0)) {
  569. DRM_ERROR("Could not add a reference to a surface.\n");
  570. goto out_no_reference;
  571. }
  572. rep->flags = srf->flags;
  573. rep->format = srf->format;
  574. memcpy(rep->mip_levels, srf->mip_levels, sizeof(srf->mip_levels));
  575. user_sizes = (struct drm_vmw_size __user *)(unsigned long)
  576. rep->size_addr;
  577. if (user_sizes)
  578. ret = copy_to_user(user_sizes, srf->sizes,
  579. srf->num_sizes * sizeof(*srf->sizes));
  580. if (unlikely(ret != 0)) {
  581. DRM_ERROR("copy_to_user failed %p %u\n",
  582. user_sizes, srf->num_sizes);
  583. ret = -EFAULT;
  584. }
  585. out_bad_resource:
  586. out_no_reference:
  587. ttm_base_object_unref(&base);
  588. return ret;
  589. }
  590. int vmw_surface_check(struct vmw_private *dev_priv,
  591. struct ttm_object_file *tfile,
  592. uint32_t handle, int *id)
  593. {
  594. struct ttm_base_object *base;
  595. struct vmw_user_surface *user_srf;
  596. int ret = -EPERM;
  597. base = ttm_base_object_lookup(tfile, handle);
  598. if (unlikely(base == NULL))
  599. return -EINVAL;
  600. if (unlikely(base->object_type != VMW_RES_SURFACE))
  601. goto out_bad_surface;
  602. user_srf = container_of(base, struct vmw_user_surface, base);
  603. *id = user_srf->srf.res.id;
  604. ret = 0;
  605. out_bad_surface:
  606. /**
  607. * FIXME: May deadlock here when called from the
  608. * command parsing code.
  609. */
  610. ttm_base_object_unref(&base);
  611. return ret;
  612. }
  613. /**
  614. * Buffer management.
  615. */
  616. static size_t vmw_dmabuf_acc_size(struct ttm_bo_global *glob,
  617. unsigned long num_pages)
  618. {
  619. static size_t bo_user_size = ~0;
  620. size_t page_array_size =
  621. (num_pages * sizeof(void *) + PAGE_SIZE - 1) & PAGE_MASK;
  622. if (unlikely(bo_user_size == ~0)) {
  623. bo_user_size = glob->ttm_bo_extra_size +
  624. ttm_round_pot(sizeof(struct vmw_dma_buffer));
  625. }
  626. return bo_user_size + page_array_size;
  627. }
  628. void vmw_dmabuf_bo_free(struct ttm_buffer_object *bo)
  629. {
  630. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  631. struct ttm_bo_global *glob = bo->glob;
  632. ttm_mem_global_free(glob->mem_glob, bo->acc_size);
  633. kfree(vmw_bo);
  634. }
  635. int vmw_dmabuf_init(struct vmw_private *dev_priv,
  636. struct vmw_dma_buffer *vmw_bo,
  637. size_t size, struct ttm_placement *placement,
  638. bool interruptible,
  639. void (*bo_free) (struct ttm_buffer_object *bo))
  640. {
  641. struct ttm_bo_device *bdev = &dev_priv->bdev;
  642. struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
  643. size_t acc_size;
  644. int ret;
  645. BUG_ON(!bo_free);
  646. acc_size =
  647. vmw_dmabuf_acc_size(bdev->glob,
  648. (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
  649. ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
  650. if (unlikely(ret != 0)) {
  651. /* we must free the bo here as
  652. * ttm_buffer_object_init does so as well */
  653. bo_free(&vmw_bo->base);
  654. return ret;
  655. }
  656. memset(vmw_bo, 0, sizeof(*vmw_bo));
  657. INIT_LIST_HEAD(&vmw_bo->validate_list);
  658. ret = ttm_bo_init(bdev, &vmw_bo->base, size,
  659. ttm_bo_type_device, placement,
  660. 0, 0, interruptible,
  661. NULL, acc_size, bo_free);
  662. return ret;
  663. }
  664. static void vmw_user_dmabuf_destroy(struct ttm_buffer_object *bo)
  665. {
  666. struct vmw_user_dma_buffer *vmw_user_bo = vmw_user_dma_buffer(bo);
  667. struct ttm_bo_global *glob = bo->glob;
  668. ttm_mem_global_free(glob->mem_glob, bo->acc_size);
  669. kfree(vmw_user_bo);
  670. }
  671. static void vmw_user_dmabuf_release(struct ttm_base_object **p_base)
  672. {
  673. struct vmw_user_dma_buffer *vmw_user_bo;
  674. struct ttm_base_object *base = *p_base;
  675. struct ttm_buffer_object *bo;
  676. *p_base = NULL;
  677. if (unlikely(base == NULL))
  678. return;
  679. vmw_user_bo = container_of(base, struct vmw_user_dma_buffer, base);
  680. bo = &vmw_user_bo->dma.base;
  681. ttm_bo_unref(&bo);
  682. }
  683. int vmw_dmabuf_alloc_ioctl(struct drm_device *dev, void *data,
  684. struct drm_file *file_priv)
  685. {
  686. struct vmw_private *dev_priv = vmw_priv(dev);
  687. union drm_vmw_alloc_dmabuf_arg *arg =
  688. (union drm_vmw_alloc_dmabuf_arg *)data;
  689. struct drm_vmw_alloc_dmabuf_req *req = &arg->req;
  690. struct drm_vmw_dmabuf_rep *rep = &arg->rep;
  691. struct vmw_user_dma_buffer *vmw_user_bo;
  692. struct ttm_buffer_object *tmp;
  693. struct vmw_master *vmaster = vmw_master(file_priv->master);
  694. int ret;
  695. vmw_user_bo = kzalloc(sizeof(*vmw_user_bo), GFP_KERNEL);
  696. if (unlikely(vmw_user_bo == NULL))
  697. return -ENOMEM;
  698. ret = ttm_read_lock(&vmaster->lock, true);
  699. if (unlikely(ret != 0)) {
  700. kfree(vmw_user_bo);
  701. return ret;
  702. }
  703. ret = vmw_dmabuf_init(dev_priv, &vmw_user_bo->dma, req->size,
  704. &vmw_vram_sys_placement, true,
  705. &vmw_user_dmabuf_destroy);
  706. if (unlikely(ret != 0))
  707. goto out_no_dmabuf;
  708. tmp = ttm_bo_reference(&vmw_user_bo->dma.base);
  709. ret = ttm_base_object_init(vmw_fpriv(file_priv)->tfile,
  710. &vmw_user_bo->base,
  711. false,
  712. ttm_buffer_type,
  713. &vmw_user_dmabuf_release, NULL);
  714. if (unlikely(ret != 0))
  715. goto out_no_base_object;
  716. else {
  717. rep->handle = vmw_user_bo->base.hash.key;
  718. rep->map_handle = vmw_user_bo->dma.base.addr_space_offset;
  719. rep->cur_gmr_id = vmw_user_bo->base.hash.key;
  720. rep->cur_gmr_offset = 0;
  721. }
  722. out_no_base_object:
  723. ttm_bo_unref(&tmp);
  724. out_no_dmabuf:
  725. ttm_read_unlock(&vmaster->lock);
  726. return ret;
  727. }
  728. int vmw_dmabuf_unref_ioctl(struct drm_device *dev, void *data,
  729. struct drm_file *file_priv)
  730. {
  731. struct drm_vmw_unref_dmabuf_arg *arg =
  732. (struct drm_vmw_unref_dmabuf_arg *)data;
  733. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  734. arg->handle,
  735. TTM_REF_USAGE);
  736. }
  737. uint32_t vmw_dmabuf_validate_node(struct ttm_buffer_object *bo,
  738. uint32_t cur_validate_node)
  739. {
  740. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  741. if (likely(vmw_bo->on_validate_list))
  742. return vmw_bo->cur_validate_node;
  743. vmw_bo->cur_validate_node = cur_validate_node;
  744. vmw_bo->on_validate_list = true;
  745. return cur_validate_node;
  746. }
  747. void vmw_dmabuf_validate_clear(struct ttm_buffer_object *bo)
  748. {
  749. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  750. vmw_bo->on_validate_list = false;
  751. }
  752. int vmw_user_dmabuf_lookup(struct ttm_object_file *tfile,
  753. uint32_t handle, struct vmw_dma_buffer **out)
  754. {
  755. struct vmw_user_dma_buffer *vmw_user_bo;
  756. struct ttm_base_object *base;
  757. base = ttm_base_object_lookup(tfile, handle);
  758. if (unlikely(base == NULL)) {
  759. printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
  760. (unsigned long)handle);
  761. return -ESRCH;
  762. }
  763. if (unlikely(base->object_type != ttm_buffer_type)) {
  764. ttm_base_object_unref(&base);
  765. printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
  766. (unsigned long)handle);
  767. return -EINVAL;
  768. }
  769. vmw_user_bo = container_of(base, struct vmw_user_dma_buffer, base);
  770. (void)ttm_bo_reference(&vmw_user_bo->dma.base);
  771. ttm_base_object_unref(&base);
  772. *out = &vmw_user_bo->dma;
  773. return 0;
  774. }
  775. /*
  776. * Stream management
  777. */
  778. static void vmw_stream_destroy(struct vmw_resource *res)
  779. {
  780. struct vmw_private *dev_priv = res->dev_priv;
  781. struct vmw_stream *stream;
  782. int ret;
  783. DRM_INFO("%s: unref\n", __func__);
  784. stream = container_of(res, struct vmw_stream, res);
  785. ret = vmw_overlay_unref(dev_priv, stream->stream_id);
  786. WARN_ON(ret != 0);
  787. }
  788. static int vmw_stream_init(struct vmw_private *dev_priv,
  789. struct vmw_stream *stream,
  790. void (*res_free) (struct vmw_resource *res))
  791. {
  792. struct vmw_resource *res = &stream->res;
  793. int ret;
  794. ret = vmw_resource_init(dev_priv, res, &dev_priv->stream_idr,
  795. VMW_RES_STREAM, res_free);
  796. if (unlikely(ret != 0)) {
  797. if (res_free == NULL)
  798. kfree(stream);
  799. else
  800. res_free(&stream->res);
  801. return ret;
  802. }
  803. ret = vmw_overlay_claim(dev_priv, &stream->stream_id);
  804. if (ret) {
  805. vmw_resource_unreference(&res);
  806. return ret;
  807. }
  808. DRM_INFO("%s: claimed\n", __func__);
  809. vmw_resource_activate(&stream->res, vmw_stream_destroy);
  810. return 0;
  811. }
  812. /**
  813. * User-space context management:
  814. */
  815. static void vmw_user_stream_free(struct vmw_resource *res)
  816. {
  817. struct vmw_user_stream *stream =
  818. container_of(res, struct vmw_user_stream, stream.res);
  819. kfree(stream);
  820. }
  821. /**
  822. * This function is called when user space has no more references on the
  823. * base object. It releases the base-object's reference on the resource object.
  824. */
  825. static void vmw_user_stream_base_release(struct ttm_base_object **p_base)
  826. {
  827. struct ttm_base_object *base = *p_base;
  828. struct vmw_user_stream *stream =
  829. container_of(base, struct vmw_user_stream, base);
  830. struct vmw_resource *res = &stream->stream.res;
  831. *p_base = NULL;
  832. vmw_resource_unreference(&res);
  833. }
  834. int vmw_stream_unref_ioctl(struct drm_device *dev, void *data,
  835. struct drm_file *file_priv)
  836. {
  837. struct vmw_private *dev_priv = vmw_priv(dev);
  838. struct vmw_resource *res;
  839. struct vmw_user_stream *stream;
  840. struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
  841. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  842. int ret = 0;
  843. res = vmw_resource_lookup(dev_priv, &dev_priv->stream_idr, arg->stream_id);
  844. if (unlikely(res == NULL))
  845. return -EINVAL;
  846. if (res->res_free != &vmw_user_stream_free) {
  847. ret = -EINVAL;
  848. goto out;
  849. }
  850. stream = container_of(res, struct vmw_user_stream, stream.res);
  851. if (stream->base.tfile != tfile) {
  852. ret = -EINVAL;
  853. goto out;
  854. }
  855. ttm_ref_object_base_unref(tfile, stream->base.hash.key, TTM_REF_USAGE);
  856. out:
  857. vmw_resource_unreference(&res);
  858. return ret;
  859. }
  860. int vmw_stream_claim_ioctl(struct drm_device *dev, void *data,
  861. struct drm_file *file_priv)
  862. {
  863. struct vmw_private *dev_priv = vmw_priv(dev);
  864. struct vmw_user_stream *stream = kmalloc(sizeof(*stream), GFP_KERNEL);
  865. struct vmw_resource *res;
  866. struct vmw_resource *tmp;
  867. struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
  868. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  869. int ret;
  870. if (unlikely(stream == NULL))
  871. return -ENOMEM;
  872. res = &stream->stream.res;
  873. stream->base.shareable = false;
  874. stream->base.tfile = NULL;
  875. ret = vmw_stream_init(dev_priv, &stream->stream, vmw_user_stream_free);
  876. if (unlikely(ret != 0))
  877. return ret;
  878. tmp = vmw_resource_reference(res);
  879. ret = ttm_base_object_init(tfile, &stream->base, false, VMW_RES_STREAM,
  880. &vmw_user_stream_base_release, NULL);
  881. if (unlikely(ret != 0)) {
  882. vmw_resource_unreference(&tmp);
  883. goto out_err;
  884. }
  885. arg->stream_id = res->id;
  886. out_err:
  887. vmw_resource_unreference(&res);
  888. return ret;
  889. }
  890. int vmw_user_stream_lookup(struct vmw_private *dev_priv,
  891. struct ttm_object_file *tfile,
  892. uint32_t *inout_id, struct vmw_resource **out)
  893. {
  894. struct vmw_user_stream *stream;
  895. struct vmw_resource *res;
  896. int ret;
  897. res = vmw_resource_lookup(dev_priv, &dev_priv->stream_idr, *inout_id);
  898. if (unlikely(res == NULL))
  899. return -EINVAL;
  900. if (res->res_free != &vmw_user_stream_free) {
  901. ret = -EINVAL;
  902. goto err_ref;
  903. }
  904. stream = container_of(res, struct vmw_user_stream, stream.res);
  905. if (stream->base.tfile != tfile) {
  906. ret = -EPERM;
  907. goto err_ref;
  908. }
  909. *inout_id = stream->stream.stream_id;
  910. *out = res;
  911. return 0;
  912. err_ref:
  913. vmw_resource_unreference(&res);
  914. return ret;
  915. }