vmwgfx_resource.c 28 KB

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