vmwgfx_resource.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  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_handle(struct vmw_private *dev_priv,
  402. struct ttm_object_file *tfile,
  403. uint32_t handle, struct vmw_surface **out)
  404. {
  405. struct vmw_resource *res;
  406. struct vmw_surface *srf;
  407. struct vmw_user_surface *user_srf;
  408. struct ttm_base_object *base;
  409. int ret = -EINVAL;
  410. base = ttm_base_object_lookup(tfile, handle);
  411. if (unlikely(base == NULL))
  412. return -EINVAL;
  413. if (unlikely(base->object_type != VMW_RES_SURFACE))
  414. goto out_bad_resource;
  415. user_srf = container_of(base, struct vmw_user_surface, base);
  416. srf = &user_srf->srf;
  417. res = &srf->res;
  418. read_lock(&dev_priv->resource_lock);
  419. if (!res->avail || res->res_free != &vmw_user_surface_free) {
  420. read_unlock(&dev_priv->resource_lock);
  421. goto out_bad_resource;
  422. }
  423. kref_get(&res->kref);
  424. read_unlock(&dev_priv->resource_lock);
  425. *out = srf;
  426. ret = 0;
  427. out_bad_resource:
  428. ttm_base_object_unref(&base);
  429. return ret;
  430. }
  431. static void vmw_user_surface_base_release(struct ttm_base_object **p_base)
  432. {
  433. struct ttm_base_object *base = *p_base;
  434. struct vmw_user_surface *user_srf =
  435. container_of(base, struct vmw_user_surface, base);
  436. struct vmw_resource *res = &user_srf->srf.res;
  437. *p_base = NULL;
  438. vmw_resource_unreference(&res);
  439. }
  440. int vmw_surface_destroy_ioctl(struct drm_device *dev, void *data,
  441. struct drm_file *file_priv)
  442. {
  443. struct drm_vmw_surface_arg *arg = (struct drm_vmw_surface_arg *)data;
  444. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  445. return ttm_ref_object_base_unref(tfile, arg->sid, TTM_REF_USAGE);
  446. }
  447. int vmw_surface_define_ioctl(struct drm_device *dev, void *data,
  448. struct drm_file *file_priv)
  449. {
  450. struct vmw_private *dev_priv = vmw_priv(dev);
  451. struct vmw_user_surface *user_srf =
  452. kmalloc(sizeof(*user_srf), GFP_KERNEL);
  453. struct vmw_surface *srf;
  454. struct vmw_resource *res;
  455. struct vmw_resource *tmp;
  456. union drm_vmw_surface_create_arg *arg =
  457. (union drm_vmw_surface_create_arg *)data;
  458. struct drm_vmw_surface_create_req *req = &arg->req;
  459. struct drm_vmw_surface_arg *rep = &arg->rep;
  460. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  461. struct drm_vmw_size __user *user_sizes;
  462. int ret;
  463. int i;
  464. if (unlikely(user_srf == NULL))
  465. return -ENOMEM;
  466. srf = &user_srf->srf;
  467. res = &srf->res;
  468. srf->flags = req->flags;
  469. srf->format = req->format;
  470. srf->scanout = req->scanout;
  471. memcpy(srf->mip_levels, req->mip_levels, sizeof(srf->mip_levels));
  472. srf->num_sizes = 0;
  473. for (i = 0; i < DRM_VMW_MAX_SURFACE_FACES; ++i)
  474. srf->num_sizes += srf->mip_levels[i];
  475. if (srf->num_sizes > DRM_VMW_MAX_SURFACE_FACES *
  476. DRM_VMW_MAX_MIP_LEVELS) {
  477. ret = -EINVAL;
  478. goto out_err0;
  479. }
  480. srf->sizes = kmalloc(srf->num_sizes * sizeof(*srf->sizes), GFP_KERNEL);
  481. if (unlikely(srf->sizes == NULL)) {
  482. ret = -ENOMEM;
  483. goto out_err0;
  484. }
  485. user_sizes = (struct drm_vmw_size __user *)(unsigned long)
  486. req->size_addr;
  487. ret = copy_from_user(srf->sizes, user_sizes,
  488. srf->num_sizes * sizeof(*srf->sizes));
  489. if (unlikely(ret != 0))
  490. goto out_err1;
  491. if (srf->scanout &&
  492. srf->num_sizes == 1 &&
  493. srf->sizes[0].width == 64 &&
  494. srf->sizes[0].height == 64 &&
  495. srf->format == SVGA3D_A8R8G8B8) {
  496. srf->snooper.image = kmalloc(64 * 64 * 4, GFP_KERNEL);
  497. /* clear the image */
  498. if (srf->snooper.image) {
  499. memset(srf->snooper.image, 0x00, 64 * 64 * 4);
  500. } else {
  501. DRM_ERROR("Failed to allocate cursor_image\n");
  502. ret = -ENOMEM;
  503. goto out_err1;
  504. }
  505. } else {
  506. srf->snooper.image = NULL;
  507. }
  508. srf->snooper.crtc = NULL;
  509. user_srf->base.shareable = false;
  510. user_srf->base.tfile = NULL;
  511. /**
  512. * From this point, the generic resource management functions
  513. * destroy the object on failure.
  514. */
  515. ret = vmw_surface_init(dev_priv, srf, vmw_user_surface_free);
  516. if (unlikely(ret != 0))
  517. return ret;
  518. tmp = vmw_resource_reference(&srf->res);
  519. ret = ttm_base_object_init(tfile, &user_srf->base,
  520. req->shareable, VMW_RES_SURFACE,
  521. &vmw_user_surface_base_release, NULL);
  522. if (unlikely(ret != 0)) {
  523. vmw_resource_unreference(&tmp);
  524. vmw_resource_unreference(&res);
  525. return ret;
  526. }
  527. rep->sid = user_srf->base.hash.key;
  528. if (rep->sid == SVGA3D_INVALID_ID)
  529. DRM_ERROR("Created bad Surface ID.\n");
  530. vmw_resource_unreference(&res);
  531. return 0;
  532. out_err1:
  533. kfree(srf->sizes);
  534. out_err0:
  535. kfree(user_srf);
  536. return ret;
  537. }
  538. int vmw_surface_reference_ioctl(struct drm_device *dev, void *data,
  539. struct drm_file *file_priv)
  540. {
  541. union drm_vmw_surface_reference_arg *arg =
  542. (union drm_vmw_surface_reference_arg *)data;
  543. struct drm_vmw_surface_arg *req = &arg->req;
  544. struct drm_vmw_surface_create_req *rep = &arg->rep;
  545. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  546. struct vmw_surface *srf;
  547. struct vmw_user_surface *user_srf;
  548. struct drm_vmw_size __user *user_sizes;
  549. struct ttm_base_object *base;
  550. int ret = -EINVAL;
  551. base = ttm_base_object_lookup(tfile, req->sid);
  552. if (unlikely(base == NULL)) {
  553. DRM_ERROR("Could not find surface to reference.\n");
  554. return -EINVAL;
  555. }
  556. if (unlikely(base->object_type != VMW_RES_SURFACE))
  557. goto out_bad_resource;
  558. user_srf = container_of(base, struct vmw_user_surface, base);
  559. srf = &user_srf->srf;
  560. ret = ttm_ref_object_add(tfile, &user_srf->base, TTM_REF_USAGE, NULL);
  561. if (unlikely(ret != 0)) {
  562. DRM_ERROR("Could not add a reference to a surface.\n");
  563. goto out_no_reference;
  564. }
  565. rep->flags = srf->flags;
  566. rep->format = srf->format;
  567. memcpy(rep->mip_levels, srf->mip_levels, sizeof(srf->mip_levels));
  568. user_sizes = (struct drm_vmw_size __user *)(unsigned long)
  569. rep->size_addr;
  570. if (user_sizes)
  571. ret = copy_to_user(user_sizes, srf->sizes,
  572. srf->num_sizes * sizeof(*srf->sizes));
  573. if (unlikely(ret != 0))
  574. DRM_ERROR("copy_to_user failed %p %u\n",
  575. user_sizes, srf->num_sizes);
  576. out_bad_resource:
  577. out_no_reference:
  578. ttm_base_object_unref(&base);
  579. return ret;
  580. }
  581. int vmw_surface_check(struct vmw_private *dev_priv,
  582. struct ttm_object_file *tfile,
  583. uint32_t handle, int *id)
  584. {
  585. struct ttm_base_object *base;
  586. struct vmw_user_surface *user_srf;
  587. int ret = -EPERM;
  588. base = ttm_base_object_lookup(tfile, handle);
  589. if (unlikely(base == NULL))
  590. return -EINVAL;
  591. if (unlikely(base->object_type != VMW_RES_SURFACE))
  592. goto out_bad_surface;
  593. user_srf = container_of(base, struct vmw_user_surface, base);
  594. *id = user_srf->srf.res.id;
  595. ret = 0;
  596. out_bad_surface:
  597. /**
  598. * FIXME: May deadlock here when called from the
  599. * command parsing code.
  600. */
  601. ttm_base_object_unref(&base);
  602. return ret;
  603. }
  604. /**
  605. * Buffer management.
  606. */
  607. static size_t vmw_dmabuf_acc_size(struct ttm_bo_global *glob,
  608. unsigned long num_pages)
  609. {
  610. static size_t bo_user_size = ~0;
  611. size_t page_array_size =
  612. (num_pages * sizeof(void *) + PAGE_SIZE - 1) & PAGE_MASK;
  613. if (unlikely(bo_user_size == ~0)) {
  614. bo_user_size = glob->ttm_bo_extra_size +
  615. ttm_round_pot(sizeof(struct vmw_dma_buffer));
  616. }
  617. return bo_user_size + page_array_size;
  618. }
  619. void vmw_dmabuf_gmr_unbind(struct ttm_buffer_object *bo)
  620. {
  621. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  622. struct ttm_bo_global *glob = bo->glob;
  623. struct vmw_private *dev_priv =
  624. container_of(bo->bdev, struct vmw_private, bdev);
  625. if (vmw_bo->gmr_bound) {
  626. vmw_gmr_unbind(dev_priv, vmw_bo->gmr_id);
  627. spin_lock(&glob->lru_lock);
  628. ida_remove(&dev_priv->gmr_ida, vmw_bo->gmr_id);
  629. spin_unlock(&glob->lru_lock);
  630. vmw_bo->gmr_bound = false;
  631. }
  632. }
  633. void vmw_dmabuf_bo_free(struct ttm_buffer_object *bo)
  634. {
  635. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  636. struct ttm_bo_global *glob = bo->glob;
  637. vmw_dmabuf_gmr_unbind(bo);
  638. ttm_mem_global_free(glob->mem_glob, bo->acc_size);
  639. kfree(vmw_bo);
  640. }
  641. int vmw_dmabuf_init(struct vmw_private *dev_priv,
  642. struct vmw_dma_buffer *vmw_bo,
  643. size_t size, struct ttm_placement *placement,
  644. bool interruptible,
  645. void (*bo_free) (struct ttm_buffer_object *bo))
  646. {
  647. struct ttm_bo_device *bdev = &dev_priv->bdev;
  648. struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
  649. size_t acc_size;
  650. int ret;
  651. BUG_ON(!bo_free);
  652. acc_size =
  653. vmw_dmabuf_acc_size(bdev->glob,
  654. (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
  655. ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
  656. if (unlikely(ret != 0)) {
  657. /* we must free the bo here as
  658. * ttm_buffer_object_init does so as well */
  659. bo_free(&vmw_bo->base);
  660. return ret;
  661. }
  662. memset(vmw_bo, 0, sizeof(*vmw_bo));
  663. INIT_LIST_HEAD(&vmw_bo->gmr_lru);
  664. INIT_LIST_HEAD(&vmw_bo->validate_list);
  665. vmw_bo->gmr_id = 0;
  666. vmw_bo->gmr_bound = false;
  667. ret = ttm_bo_init(bdev, &vmw_bo->base, size,
  668. ttm_bo_type_device, placement,
  669. 0, 0, interruptible,
  670. NULL, acc_size, bo_free);
  671. return ret;
  672. }
  673. static void vmw_user_dmabuf_destroy(struct ttm_buffer_object *bo)
  674. {
  675. struct vmw_user_dma_buffer *vmw_user_bo = vmw_user_dma_buffer(bo);
  676. struct ttm_bo_global *glob = bo->glob;
  677. vmw_dmabuf_gmr_unbind(bo);
  678. ttm_mem_global_free(glob->mem_glob, bo->acc_size);
  679. kfree(vmw_user_bo);
  680. }
  681. static void vmw_user_dmabuf_release(struct ttm_base_object **p_base)
  682. {
  683. struct vmw_user_dma_buffer *vmw_user_bo;
  684. struct ttm_base_object *base = *p_base;
  685. struct ttm_buffer_object *bo;
  686. *p_base = NULL;
  687. if (unlikely(base == NULL))
  688. return;
  689. vmw_user_bo = container_of(base, struct vmw_user_dma_buffer, base);
  690. bo = &vmw_user_bo->dma.base;
  691. ttm_bo_unref(&bo);
  692. }
  693. int vmw_dmabuf_alloc_ioctl(struct drm_device *dev, void *data,
  694. struct drm_file *file_priv)
  695. {
  696. struct vmw_private *dev_priv = vmw_priv(dev);
  697. union drm_vmw_alloc_dmabuf_arg *arg =
  698. (union drm_vmw_alloc_dmabuf_arg *)data;
  699. struct drm_vmw_alloc_dmabuf_req *req = &arg->req;
  700. struct drm_vmw_dmabuf_rep *rep = &arg->rep;
  701. struct vmw_user_dma_buffer *vmw_user_bo;
  702. struct ttm_buffer_object *tmp;
  703. struct vmw_master *vmaster = vmw_master(file_priv->master);
  704. int ret;
  705. vmw_user_bo = kzalloc(sizeof(*vmw_user_bo), GFP_KERNEL);
  706. if (unlikely(vmw_user_bo == NULL))
  707. return -ENOMEM;
  708. ret = ttm_read_lock(&vmaster->lock, true);
  709. if (unlikely(ret != 0)) {
  710. kfree(vmw_user_bo);
  711. return ret;
  712. }
  713. ret = vmw_dmabuf_init(dev_priv, &vmw_user_bo->dma, req->size,
  714. &vmw_vram_sys_placement, true,
  715. &vmw_user_dmabuf_destroy);
  716. if (unlikely(ret != 0))
  717. return ret;
  718. tmp = ttm_bo_reference(&vmw_user_bo->dma.base);
  719. ret = ttm_base_object_init(vmw_fpriv(file_priv)->tfile,
  720. &vmw_user_bo->base,
  721. false,
  722. ttm_buffer_type,
  723. &vmw_user_dmabuf_release, NULL);
  724. if (unlikely(ret != 0)) {
  725. ttm_bo_unref(&tmp);
  726. } else {
  727. rep->handle = vmw_user_bo->base.hash.key;
  728. rep->map_handle = vmw_user_bo->dma.base.addr_space_offset;
  729. rep->cur_gmr_id = vmw_user_bo->base.hash.key;
  730. rep->cur_gmr_offset = 0;
  731. }
  732. ttm_bo_unref(&tmp);
  733. ttm_read_unlock(&vmaster->lock);
  734. return 0;
  735. }
  736. int vmw_dmabuf_unref_ioctl(struct drm_device *dev, void *data,
  737. struct drm_file *file_priv)
  738. {
  739. struct drm_vmw_unref_dmabuf_arg *arg =
  740. (struct drm_vmw_unref_dmabuf_arg *)data;
  741. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  742. arg->handle,
  743. TTM_REF_USAGE);
  744. }
  745. uint32_t vmw_dmabuf_validate_node(struct ttm_buffer_object *bo,
  746. uint32_t cur_validate_node)
  747. {
  748. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  749. if (likely(vmw_bo->on_validate_list))
  750. return vmw_bo->cur_validate_node;
  751. vmw_bo->cur_validate_node = cur_validate_node;
  752. vmw_bo->on_validate_list = true;
  753. return cur_validate_node;
  754. }
  755. void vmw_dmabuf_validate_clear(struct ttm_buffer_object *bo)
  756. {
  757. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  758. vmw_bo->on_validate_list = false;
  759. }
  760. uint32_t vmw_dmabuf_gmr(struct ttm_buffer_object *bo)
  761. {
  762. struct vmw_dma_buffer *vmw_bo;
  763. if (bo->mem.mem_type == TTM_PL_VRAM)
  764. return SVGA_GMR_FRAMEBUFFER;
  765. vmw_bo = vmw_dma_buffer(bo);
  766. return (vmw_bo->gmr_bound) ? vmw_bo->gmr_id : SVGA_GMR_NULL;
  767. }
  768. void vmw_dmabuf_set_gmr(struct ttm_buffer_object *bo, uint32_t id)
  769. {
  770. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  771. vmw_bo->gmr_bound = true;
  772. vmw_bo->gmr_id = id;
  773. }
  774. int vmw_user_dmabuf_lookup(struct ttm_object_file *tfile,
  775. uint32_t handle, struct vmw_dma_buffer **out)
  776. {
  777. struct vmw_user_dma_buffer *vmw_user_bo;
  778. struct ttm_base_object *base;
  779. base = ttm_base_object_lookup(tfile, handle);
  780. if (unlikely(base == NULL)) {
  781. printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
  782. (unsigned long)handle);
  783. return -ESRCH;
  784. }
  785. if (unlikely(base->object_type != ttm_buffer_type)) {
  786. ttm_base_object_unref(&base);
  787. printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
  788. (unsigned long)handle);
  789. return -EINVAL;
  790. }
  791. vmw_user_bo = container_of(base, struct vmw_user_dma_buffer, base);
  792. (void)ttm_bo_reference(&vmw_user_bo->dma.base);
  793. ttm_base_object_unref(&base);
  794. *out = &vmw_user_bo->dma;
  795. return 0;
  796. }
  797. /**
  798. * TODO: Implement a gmr id eviction mechanism. Currently we just fail
  799. * when we're out of ids, causing GMR space to be allocated
  800. * out of VRAM.
  801. */
  802. int vmw_gmr_id_alloc(struct vmw_private *dev_priv, uint32_t *p_id)
  803. {
  804. struct ttm_bo_global *glob = dev_priv->bdev.glob;
  805. int id;
  806. int ret;
  807. do {
  808. if (unlikely(ida_pre_get(&dev_priv->gmr_ida, GFP_KERNEL) == 0))
  809. return -ENOMEM;
  810. spin_lock(&glob->lru_lock);
  811. ret = ida_get_new(&dev_priv->gmr_ida, &id);
  812. spin_unlock(&glob->lru_lock);
  813. } while (ret == -EAGAIN);
  814. if (unlikely(ret != 0))
  815. return ret;
  816. if (unlikely(id >= dev_priv->max_gmr_ids)) {
  817. spin_lock(&glob->lru_lock);
  818. ida_remove(&dev_priv->gmr_ida, id);
  819. spin_unlock(&glob->lru_lock);
  820. return -EBUSY;
  821. }
  822. *p_id = (uint32_t) id;
  823. return 0;
  824. }
  825. /*
  826. * Stream managment
  827. */
  828. static void vmw_stream_destroy(struct vmw_resource *res)
  829. {
  830. struct vmw_private *dev_priv = res->dev_priv;
  831. struct vmw_stream *stream;
  832. int ret;
  833. DRM_INFO("%s: unref\n", __func__);
  834. stream = container_of(res, struct vmw_stream, res);
  835. ret = vmw_overlay_unref(dev_priv, stream->stream_id);
  836. WARN_ON(ret != 0);
  837. }
  838. static int vmw_stream_init(struct vmw_private *dev_priv,
  839. struct vmw_stream *stream,
  840. void (*res_free) (struct vmw_resource *res))
  841. {
  842. struct vmw_resource *res = &stream->res;
  843. int ret;
  844. ret = vmw_resource_init(dev_priv, res, &dev_priv->stream_idr,
  845. VMW_RES_STREAM, res_free);
  846. if (unlikely(ret != 0)) {
  847. if (res_free == NULL)
  848. kfree(stream);
  849. else
  850. res_free(&stream->res);
  851. return ret;
  852. }
  853. ret = vmw_overlay_claim(dev_priv, &stream->stream_id);
  854. if (ret) {
  855. vmw_resource_unreference(&res);
  856. return ret;
  857. }
  858. DRM_INFO("%s: claimed\n", __func__);
  859. vmw_resource_activate(&stream->res, vmw_stream_destroy);
  860. return 0;
  861. }
  862. /**
  863. * User-space context management:
  864. */
  865. static void vmw_user_stream_free(struct vmw_resource *res)
  866. {
  867. struct vmw_user_stream *stream =
  868. container_of(res, struct vmw_user_stream, stream.res);
  869. kfree(stream);
  870. }
  871. /**
  872. * This function is called when user space has no more references on the
  873. * base object. It releases the base-object's reference on the resource object.
  874. */
  875. static void vmw_user_stream_base_release(struct ttm_base_object **p_base)
  876. {
  877. struct ttm_base_object *base = *p_base;
  878. struct vmw_user_stream *stream =
  879. container_of(base, struct vmw_user_stream, base);
  880. struct vmw_resource *res = &stream->stream.res;
  881. *p_base = NULL;
  882. vmw_resource_unreference(&res);
  883. }
  884. int vmw_stream_unref_ioctl(struct drm_device *dev, void *data,
  885. struct drm_file *file_priv)
  886. {
  887. struct vmw_private *dev_priv = vmw_priv(dev);
  888. struct vmw_resource *res;
  889. struct vmw_user_stream *stream;
  890. struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
  891. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  892. int ret = 0;
  893. res = vmw_resource_lookup(dev_priv, &dev_priv->stream_idr, arg->stream_id);
  894. if (unlikely(res == NULL))
  895. return -EINVAL;
  896. if (res->res_free != &vmw_user_stream_free) {
  897. ret = -EINVAL;
  898. goto out;
  899. }
  900. stream = container_of(res, struct vmw_user_stream, stream.res);
  901. if (stream->base.tfile != tfile) {
  902. ret = -EINVAL;
  903. goto out;
  904. }
  905. ttm_ref_object_base_unref(tfile, stream->base.hash.key, TTM_REF_USAGE);
  906. out:
  907. vmw_resource_unreference(&res);
  908. return ret;
  909. }
  910. int vmw_stream_claim_ioctl(struct drm_device *dev, void *data,
  911. struct drm_file *file_priv)
  912. {
  913. struct vmw_private *dev_priv = vmw_priv(dev);
  914. struct vmw_user_stream *stream = kmalloc(sizeof(*stream), GFP_KERNEL);
  915. struct vmw_resource *res;
  916. struct vmw_resource *tmp;
  917. struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
  918. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  919. int ret;
  920. if (unlikely(stream == NULL))
  921. return -ENOMEM;
  922. res = &stream->stream.res;
  923. stream->base.shareable = false;
  924. stream->base.tfile = NULL;
  925. ret = vmw_stream_init(dev_priv, &stream->stream, vmw_user_stream_free);
  926. if (unlikely(ret != 0))
  927. return ret;
  928. tmp = vmw_resource_reference(res);
  929. ret = ttm_base_object_init(tfile, &stream->base, false, VMW_RES_STREAM,
  930. &vmw_user_stream_base_release, NULL);
  931. if (unlikely(ret != 0)) {
  932. vmw_resource_unreference(&tmp);
  933. goto out_err;
  934. }
  935. arg->stream_id = res->id;
  936. out_err:
  937. vmw_resource_unreference(&res);
  938. return ret;
  939. }
  940. int vmw_user_stream_lookup(struct vmw_private *dev_priv,
  941. struct ttm_object_file *tfile,
  942. uint32_t *inout_id, struct vmw_resource **out)
  943. {
  944. struct vmw_user_stream *stream;
  945. struct vmw_resource *res;
  946. int ret;
  947. res = vmw_resource_lookup(dev_priv, &dev_priv->stream_idr, *inout_id);
  948. if (unlikely(res == NULL))
  949. return -EINVAL;
  950. if (res->res_free != &vmw_user_stream_free) {
  951. ret = -EINVAL;
  952. goto err_ref;
  953. }
  954. stream = container_of(res, struct vmw_user_stream, stream.res);
  955. if (stream->base.tfile != tfile) {
  956. ret = -EPERM;
  957. goto err_ref;
  958. }
  959. *inout_id = stream->stream.stream_id;
  960. *out = res;
  961. return 0;
  962. err_ref:
  963. vmw_resource_unreference(&res);
  964. return ret;
  965. }