vmwgfx_resource.c 48 KB

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