vmwgfx_surface.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  1. /**************************************************************************
  2. *
  3. * Copyright © 2009-2012 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_resource_priv.h"
  29. #include <ttm/ttm_placement.h>
  30. #include "svga3d_surfacedefs.h"
  31. /**
  32. * struct vmw_user_surface - User-space visible surface resource
  33. *
  34. * @base: The TTM base object handling user-space visibility.
  35. * @srf: The surface metadata.
  36. * @size: TTM accounting size for the surface.
  37. */
  38. struct vmw_user_surface {
  39. struct ttm_prime_object prime;
  40. struct vmw_surface srf;
  41. uint32_t size;
  42. };
  43. /**
  44. * struct vmw_surface_offset - Backing store mip level offset info
  45. *
  46. * @face: Surface face.
  47. * @mip: Mip level.
  48. * @bo_offset: Offset into backing store of this mip level.
  49. *
  50. */
  51. struct vmw_surface_offset {
  52. uint32_t face;
  53. uint32_t mip;
  54. uint32_t bo_offset;
  55. };
  56. static void vmw_user_surface_free(struct vmw_resource *res);
  57. static struct vmw_resource *
  58. vmw_user_surface_base_to_res(struct ttm_base_object *base);
  59. static int vmw_legacy_srf_bind(struct vmw_resource *res,
  60. struct ttm_validate_buffer *val_buf);
  61. static int vmw_legacy_srf_unbind(struct vmw_resource *res,
  62. bool readback,
  63. struct ttm_validate_buffer *val_buf);
  64. static int vmw_legacy_srf_create(struct vmw_resource *res);
  65. static int vmw_legacy_srf_destroy(struct vmw_resource *res);
  66. static int vmw_gb_surface_create(struct vmw_resource *res);
  67. static int vmw_gb_surface_bind(struct vmw_resource *res,
  68. struct ttm_validate_buffer *val_buf);
  69. static int vmw_gb_surface_unbind(struct vmw_resource *res,
  70. bool readback,
  71. struct ttm_validate_buffer *val_buf);
  72. static int vmw_gb_surface_destroy(struct vmw_resource *res);
  73. static const struct vmw_user_resource_conv user_surface_conv = {
  74. .object_type = VMW_RES_SURFACE,
  75. .base_obj_to_res = vmw_user_surface_base_to_res,
  76. .res_free = vmw_user_surface_free
  77. };
  78. const struct vmw_user_resource_conv *user_surface_converter =
  79. &user_surface_conv;
  80. static uint64_t vmw_user_surface_size;
  81. static const struct vmw_res_func vmw_legacy_surface_func = {
  82. .res_type = vmw_res_surface,
  83. .needs_backup = false,
  84. .may_evict = true,
  85. .type_name = "legacy surfaces",
  86. .backup_placement = &vmw_srf_placement,
  87. .create = &vmw_legacy_srf_create,
  88. .destroy = &vmw_legacy_srf_destroy,
  89. .bind = &vmw_legacy_srf_bind,
  90. .unbind = &vmw_legacy_srf_unbind
  91. };
  92. static const struct vmw_res_func vmw_gb_surface_func = {
  93. .res_type = vmw_res_surface,
  94. .needs_backup = true,
  95. .may_evict = true,
  96. .type_name = "guest backed surfaces",
  97. .backup_placement = &vmw_mob_placement,
  98. .create = vmw_gb_surface_create,
  99. .destroy = vmw_gb_surface_destroy,
  100. .bind = vmw_gb_surface_bind,
  101. .unbind = vmw_gb_surface_unbind
  102. };
  103. /**
  104. * struct vmw_surface_dma - SVGA3D DMA command
  105. */
  106. struct vmw_surface_dma {
  107. SVGA3dCmdHeader header;
  108. SVGA3dCmdSurfaceDMA body;
  109. SVGA3dCopyBox cb;
  110. SVGA3dCmdSurfaceDMASuffix suffix;
  111. };
  112. /**
  113. * struct vmw_surface_define - SVGA3D Surface Define command
  114. */
  115. struct vmw_surface_define {
  116. SVGA3dCmdHeader header;
  117. SVGA3dCmdDefineSurface body;
  118. };
  119. /**
  120. * struct vmw_surface_destroy - SVGA3D Surface Destroy command
  121. */
  122. struct vmw_surface_destroy {
  123. SVGA3dCmdHeader header;
  124. SVGA3dCmdDestroySurface body;
  125. };
  126. /**
  127. * vmw_surface_dma_size - Compute fifo size for a dma command.
  128. *
  129. * @srf: Pointer to a struct vmw_surface
  130. *
  131. * Computes the required size for a surface dma command for backup or
  132. * restoration of the surface represented by @srf.
  133. */
  134. static inline uint32_t vmw_surface_dma_size(const struct vmw_surface *srf)
  135. {
  136. return srf->num_sizes * sizeof(struct vmw_surface_dma);
  137. }
  138. /**
  139. * vmw_surface_define_size - Compute fifo size for a surface define command.
  140. *
  141. * @srf: Pointer to a struct vmw_surface
  142. *
  143. * Computes the required size for a surface define command for the definition
  144. * of the surface represented by @srf.
  145. */
  146. static inline uint32_t vmw_surface_define_size(const struct vmw_surface *srf)
  147. {
  148. return sizeof(struct vmw_surface_define) + srf->num_sizes *
  149. sizeof(SVGA3dSize);
  150. }
  151. /**
  152. * vmw_surface_destroy_size - Compute fifo size for a surface destroy command.
  153. *
  154. * Computes the required size for a surface destroy command for the destruction
  155. * of a hw surface.
  156. */
  157. static inline uint32_t vmw_surface_destroy_size(void)
  158. {
  159. return sizeof(struct vmw_surface_destroy);
  160. }
  161. /**
  162. * vmw_surface_destroy_encode - Encode a surface_destroy command.
  163. *
  164. * @id: The surface id
  165. * @cmd_space: Pointer to memory area in which the commands should be encoded.
  166. */
  167. static void vmw_surface_destroy_encode(uint32_t id,
  168. void *cmd_space)
  169. {
  170. struct vmw_surface_destroy *cmd = (struct vmw_surface_destroy *)
  171. cmd_space;
  172. cmd->header.id = SVGA_3D_CMD_SURFACE_DESTROY;
  173. cmd->header.size = sizeof(cmd->body);
  174. cmd->body.sid = id;
  175. }
  176. /**
  177. * vmw_surface_define_encode - Encode a surface_define command.
  178. *
  179. * @srf: Pointer to a struct vmw_surface object.
  180. * @cmd_space: Pointer to memory area in which the commands should be encoded.
  181. */
  182. static void vmw_surface_define_encode(const struct vmw_surface *srf,
  183. void *cmd_space)
  184. {
  185. struct vmw_surface_define *cmd = (struct vmw_surface_define *)
  186. cmd_space;
  187. struct drm_vmw_size *src_size;
  188. SVGA3dSize *cmd_size;
  189. uint32_t cmd_len;
  190. int i;
  191. cmd_len = sizeof(cmd->body) + srf->num_sizes * sizeof(SVGA3dSize);
  192. cmd->header.id = SVGA_3D_CMD_SURFACE_DEFINE;
  193. cmd->header.size = cmd_len;
  194. cmd->body.sid = srf->res.id;
  195. cmd->body.surfaceFlags = srf->flags;
  196. cmd->body.format = cpu_to_le32(srf->format);
  197. for (i = 0; i < DRM_VMW_MAX_SURFACE_FACES; ++i)
  198. cmd->body.face[i].numMipLevels = srf->mip_levels[i];
  199. cmd += 1;
  200. cmd_size = (SVGA3dSize *) cmd;
  201. src_size = srf->sizes;
  202. for (i = 0; i < srf->num_sizes; ++i, cmd_size++, src_size++) {
  203. cmd_size->width = src_size->width;
  204. cmd_size->height = src_size->height;
  205. cmd_size->depth = src_size->depth;
  206. }
  207. }
  208. /**
  209. * vmw_surface_dma_encode - Encode a surface_dma command.
  210. *
  211. * @srf: Pointer to a struct vmw_surface object.
  212. * @cmd_space: Pointer to memory area in which the commands should be encoded.
  213. * @ptr: Pointer to an SVGAGuestPtr indicating where the surface contents
  214. * should be placed or read from.
  215. * @to_surface: Boolean whether to DMA to the surface or from the surface.
  216. */
  217. static void vmw_surface_dma_encode(struct vmw_surface *srf,
  218. void *cmd_space,
  219. const SVGAGuestPtr *ptr,
  220. bool to_surface)
  221. {
  222. uint32_t i;
  223. struct vmw_surface_dma *cmd = (struct vmw_surface_dma *)cmd_space;
  224. const struct svga3d_surface_desc *desc =
  225. svga3dsurface_get_desc(srf->format);
  226. for (i = 0; i < srf->num_sizes; ++i) {
  227. SVGA3dCmdHeader *header = &cmd->header;
  228. SVGA3dCmdSurfaceDMA *body = &cmd->body;
  229. SVGA3dCopyBox *cb = &cmd->cb;
  230. SVGA3dCmdSurfaceDMASuffix *suffix = &cmd->suffix;
  231. const struct vmw_surface_offset *cur_offset = &srf->offsets[i];
  232. const struct drm_vmw_size *cur_size = &srf->sizes[i];
  233. header->id = SVGA_3D_CMD_SURFACE_DMA;
  234. header->size = sizeof(*body) + sizeof(*cb) + sizeof(*suffix);
  235. body->guest.ptr = *ptr;
  236. body->guest.ptr.offset += cur_offset->bo_offset;
  237. body->guest.pitch = svga3dsurface_calculate_pitch(desc,
  238. cur_size);
  239. body->host.sid = srf->res.id;
  240. body->host.face = cur_offset->face;
  241. body->host.mipmap = cur_offset->mip;
  242. body->transfer = ((to_surface) ? SVGA3D_WRITE_HOST_VRAM :
  243. SVGA3D_READ_HOST_VRAM);
  244. cb->x = 0;
  245. cb->y = 0;
  246. cb->z = 0;
  247. cb->srcx = 0;
  248. cb->srcy = 0;
  249. cb->srcz = 0;
  250. cb->w = cur_size->width;
  251. cb->h = cur_size->height;
  252. cb->d = cur_size->depth;
  253. suffix->suffixSize = sizeof(*suffix);
  254. suffix->maximumOffset =
  255. svga3dsurface_get_image_buffer_size(desc, cur_size,
  256. body->guest.pitch);
  257. suffix->flags.discard = 0;
  258. suffix->flags.unsynchronized = 0;
  259. suffix->flags.reserved = 0;
  260. ++cmd;
  261. }
  262. };
  263. /**
  264. * vmw_hw_surface_destroy - destroy a Device surface
  265. *
  266. * @res: Pointer to a struct vmw_resource embedded in a struct
  267. * vmw_surface.
  268. *
  269. * Destroys a the device surface associated with a struct vmw_surface if
  270. * any, and adjusts accounting and resource count accordingly.
  271. */
  272. static void vmw_hw_surface_destroy(struct vmw_resource *res)
  273. {
  274. struct vmw_private *dev_priv = res->dev_priv;
  275. struct vmw_surface *srf;
  276. void *cmd;
  277. if (res->func->destroy == vmw_gb_surface_destroy) {
  278. (void) vmw_gb_surface_destroy(res);
  279. return;
  280. }
  281. if (res->id != -1) {
  282. cmd = vmw_fifo_reserve(dev_priv, vmw_surface_destroy_size());
  283. if (unlikely(cmd == NULL)) {
  284. DRM_ERROR("Failed reserving FIFO space for surface "
  285. "destruction.\n");
  286. return;
  287. }
  288. vmw_surface_destroy_encode(res->id, cmd);
  289. vmw_fifo_commit(dev_priv, vmw_surface_destroy_size());
  290. /*
  291. * used_memory_size_atomic, or separate lock
  292. * to avoid taking dev_priv::cmdbuf_mutex in
  293. * the destroy path.
  294. */
  295. mutex_lock(&dev_priv->cmdbuf_mutex);
  296. srf = vmw_res_to_srf(res);
  297. dev_priv->used_memory_size -= res->backup_size;
  298. mutex_unlock(&dev_priv->cmdbuf_mutex);
  299. }
  300. vmw_3d_resource_dec(dev_priv, false);
  301. }
  302. /**
  303. * vmw_legacy_srf_create - Create a device surface as part of the
  304. * resource validation process.
  305. *
  306. * @res: Pointer to a struct vmw_surface.
  307. *
  308. * If the surface doesn't have a hw id.
  309. *
  310. * Returns -EBUSY if there wasn't sufficient device resources to
  311. * complete the validation. Retry after freeing up resources.
  312. *
  313. * May return other errors if the kernel is out of guest resources.
  314. */
  315. static int vmw_legacy_srf_create(struct vmw_resource *res)
  316. {
  317. struct vmw_private *dev_priv = res->dev_priv;
  318. struct vmw_surface *srf;
  319. uint32_t submit_size;
  320. uint8_t *cmd;
  321. int ret;
  322. if (likely(res->id != -1))
  323. return 0;
  324. srf = vmw_res_to_srf(res);
  325. if (unlikely(dev_priv->used_memory_size + res->backup_size >=
  326. dev_priv->memory_size))
  327. return -EBUSY;
  328. /*
  329. * Alloc id for the resource.
  330. */
  331. ret = vmw_resource_alloc_id(res);
  332. if (unlikely(ret != 0)) {
  333. DRM_ERROR("Failed to allocate a surface id.\n");
  334. goto out_no_id;
  335. }
  336. if (unlikely(res->id >= SVGA3D_MAX_SURFACE_IDS)) {
  337. ret = -EBUSY;
  338. goto out_no_fifo;
  339. }
  340. /*
  341. * Encode surface define- commands.
  342. */
  343. submit_size = vmw_surface_define_size(srf);
  344. cmd = vmw_fifo_reserve(dev_priv, submit_size);
  345. if (unlikely(cmd == NULL)) {
  346. DRM_ERROR("Failed reserving FIFO space for surface "
  347. "creation.\n");
  348. ret = -ENOMEM;
  349. goto out_no_fifo;
  350. }
  351. vmw_surface_define_encode(srf, cmd);
  352. vmw_fifo_commit(dev_priv, submit_size);
  353. /*
  354. * Surface memory usage accounting.
  355. */
  356. dev_priv->used_memory_size += res->backup_size;
  357. return 0;
  358. out_no_fifo:
  359. vmw_resource_release_id(res);
  360. out_no_id:
  361. return ret;
  362. }
  363. /**
  364. * vmw_legacy_srf_dma - Copy backup data to or from a legacy surface.
  365. *
  366. * @res: Pointer to a struct vmw_res embedded in a struct
  367. * vmw_surface.
  368. * @val_buf: Pointer to a struct ttm_validate_buffer containing
  369. * information about the backup buffer.
  370. * @bind: Boolean wether to DMA to the surface.
  371. *
  372. * Transfer backup data to or from a legacy surface as part of the
  373. * validation process.
  374. * May return other errors if the kernel is out of guest resources.
  375. * The backup buffer will be fenced or idle upon successful completion,
  376. * and if the surface needs persistent backup storage, the backup buffer
  377. * will also be returned reserved iff @bind is true.
  378. */
  379. static int vmw_legacy_srf_dma(struct vmw_resource *res,
  380. struct ttm_validate_buffer *val_buf,
  381. bool bind)
  382. {
  383. SVGAGuestPtr ptr;
  384. struct vmw_fence_obj *fence;
  385. uint32_t submit_size;
  386. struct vmw_surface *srf = vmw_res_to_srf(res);
  387. uint8_t *cmd;
  388. struct vmw_private *dev_priv = res->dev_priv;
  389. BUG_ON(val_buf->bo == NULL);
  390. submit_size = vmw_surface_dma_size(srf);
  391. cmd = vmw_fifo_reserve(dev_priv, submit_size);
  392. if (unlikely(cmd == NULL)) {
  393. DRM_ERROR("Failed reserving FIFO space for surface "
  394. "DMA.\n");
  395. return -ENOMEM;
  396. }
  397. vmw_bo_get_guest_ptr(val_buf->bo, &ptr);
  398. vmw_surface_dma_encode(srf, cmd, &ptr, bind);
  399. vmw_fifo_commit(dev_priv, submit_size);
  400. /*
  401. * Create a fence object and fence the backup buffer.
  402. */
  403. (void) vmw_execbuf_fence_commands(NULL, dev_priv,
  404. &fence, NULL);
  405. vmw_fence_single_bo(val_buf->bo, fence);
  406. if (likely(fence != NULL))
  407. vmw_fence_obj_unreference(&fence);
  408. return 0;
  409. }
  410. /**
  411. * vmw_legacy_srf_bind - Perform a legacy surface bind as part of the
  412. * surface validation process.
  413. *
  414. * @res: Pointer to a struct vmw_res embedded in a struct
  415. * vmw_surface.
  416. * @val_buf: Pointer to a struct ttm_validate_buffer containing
  417. * information about the backup buffer.
  418. *
  419. * This function will copy backup data to the surface if the
  420. * backup buffer is dirty.
  421. */
  422. static int vmw_legacy_srf_bind(struct vmw_resource *res,
  423. struct ttm_validate_buffer *val_buf)
  424. {
  425. if (!res->backup_dirty)
  426. return 0;
  427. return vmw_legacy_srf_dma(res, val_buf, true);
  428. }
  429. /**
  430. * vmw_legacy_srf_unbind - Perform a legacy surface unbind as part of the
  431. * surface eviction process.
  432. *
  433. * @res: Pointer to a struct vmw_res embedded in a struct
  434. * vmw_surface.
  435. * @val_buf: Pointer to a struct ttm_validate_buffer containing
  436. * information about the backup buffer.
  437. *
  438. * This function will copy backup data from the surface.
  439. */
  440. static int vmw_legacy_srf_unbind(struct vmw_resource *res,
  441. bool readback,
  442. struct ttm_validate_buffer *val_buf)
  443. {
  444. if (unlikely(readback))
  445. return vmw_legacy_srf_dma(res, val_buf, false);
  446. return 0;
  447. }
  448. /**
  449. * vmw_legacy_srf_destroy - Destroy a device surface as part of a
  450. * resource eviction process.
  451. *
  452. * @res: Pointer to a struct vmw_res embedded in a struct
  453. * vmw_surface.
  454. */
  455. static int vmw_legacy_srf_destroy(struct vmw_resource *res)
  456. {
  457. struct vmw_private *dev_priv = res->dev_priv;
  458. uint32_t submit_size;
  459. uint8_t *cmd;
  460. BUG_ON(res->id == -1);
  461. /*
  462. * Encode the dma- and surface destroy commands.
  463. */
  464. submit_size = vmw_surface_destroy_size();
  465. cmd = vmw_fifo_reserve(dev_priv, submit_size);
  466. if (unlikely(cmd == NULL)) {
  467. DRM_ERROR("Failed reserving FIFO space for surface "
  468. "eviction.\n");
  469. return -ENOMEM;
  470. }
  471. vmw_surface_destroy_encode(res->id, cmd);
  472. vmw_fifo_commit(dev_priv, submit_size);
  473. /*
  474. * Surface memory usage accounting.
  475. */
  476. dev_priv->used_memory_size -= res->backup_size;
  477. /*
  478. * Release the surface ID.
  479. */
  480. vmw_resource_release_id(res);
  481. return 0;
  482. }
  483. /**
  484. * vmw_surface_init - initialize a struct vmw_surface
  485. *
  486. * @dev_priv: Pointer to a device private struct.
  487. * @srf: Pointer to the struct vmw_surface to initialize.
  488. * @res_free: Pointer to a resource destructor used to free
  489. * the object.
  490. */
  491. static int vmw_surface_init(struct vmw_private *dev_priv,
  492. struct vmw_surface *srf,
  493. void (*res_free) (struct vmw_resource *res))
  494. {
  495. int ret;
  496. struct vmw_resource *res = &srf->res;
  497. BUG_ON(res_free == NULL);
  498. if (!dev_priv->has_mob)
  499. (void) vmw_3d_resource_inc(dev_priv, false);
  500. ret = vmw_resource_init(dev_priv, res, true, res_free,
  501. (dev_priv->has_mob) ? &vmw_gb_surface_func :
  502. &vmw_legacy_surface_func);
  503. if (unlikely(ret != 0)) {
  504. if (!dev_priv->has_mob)
  505. vmw_3d_resource_dec(dev_priv, false);
  506. res_free(res);
  507. return ret;
  508. }
  509. /*
  510. * The surface won't be visible to hardware until a
  511. * surface validate.
  512. */
  513. vmw_resource_activate(res, vmw_hw_surface_destroy);
  514. return ret;
  515. }
  516. /**
  517. * vmw_user_surface_base_to_res - TTM base object to resource converter for
  518. * user visible surfaces
  519. *
  520. * @base: Pointer to a TTM base object
  521. *
  522. * Returns the struct vmw_resource embedded in a struct vmw_surface
  523. * for the user-visible object identified by the TTM base object @base.
  524. */
  525. static struct vmw_resource *
  526. vmw_user_surface_base_to_res(struct ttm_base_object *base)
  527. {
  528. return &(container_of(base, struct vmw_user_surface,
  529. prime.base)->srf.res);
  530. }
  531. /**
  532. * vmw_user_surface_free - User visible surface resource destructor
  533. *
  534. * @res: A struct vmw_resource embedded in a struct vmw_surface.
  535. */
  536. static void vmw_user_surface_free(struct vmw_resource *res)
  537. {
  538. struct vmw_surface *srf = vmw_res_to_srf(res);
  539. struct vmw_user_surface *user_srf =
  540. container_of(srf, struct vmw_user_surface, srf);
  541. struct vmw_private *dev_priv = srf->res.dev_priv;
  542. uint32_t size = user_srf->size;
  543. kfree(srf->offsets);
  544. kfree(srf->sizes);
  545. kfree(srf->snooper.image);
  546. ttm_prime_object_kfree(user_srf, prime);
  547. ttm_mem_global_free(vmw_mem_glob(dev_priv), size);
  548. }
  549. /**
  550. * vmw_user_surface_free - User visible surface TTM base object destructor
  551. *
  552. * @p_base: Pointer to a pointer to a TTM base object
  553. * embedded in a struct vmw_user_surface.
  554. *
  555. * Drops the base object's reference on its resource, and the
  556. * pointer pointed to by *p_base is set to NULL.
  557. */
  558. static void vmw_user_surface_base_release(struct ttm_base_object **p_base)
  559. {
  560. struct ttm_base_object *base = *p_base;
  561. struct vmw_user_surface *user_srf =
  562. container_of(base, struct vmw_user_surface, prime.base);
  563. struct vmw_resource *res = &user_srf->srf.res;
  564. *p_base = NULL;
  565. vmw_resource_unreference(&res);
  566. }
  567. /**
  568. * vmw_user_surface_destroy_ioctl - Ioctl function implementing
  569. * the user surface destroy functionality.
  570. *
  571. * @dev: Pointer to a struct drm_device.
  572. * @data: Pointer to data copied from / to user-space.
  573. * @file_priv: Pointer to a drm file private structure.
  574. */
  575. int vmw_surface_destroy_ioctl(struct drm_device *dev, void *data,
  576. struct drm_file *file_priv)
  577. {
  578. struct drm_vmw_surface_arg *arg = (struct drm_vmw_surface_arg *)data;
  579. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  580. return ttm_ref_object_base_unref(tfile, arg->sid, TTM_REF_USAGE);
  581. }
  582. /**
  583. * vmw_user_surface_define_ioctl - Ioctl function implementing
  584. * the user surface define functionality.
  585. *
  586. * @dev: Pointer to a struct drm_device.
  587. * @data: Pointer to data copied from / to user-space.
  588. * @file_priv: Pointer to a drm file private structure.
  589. */
  590. int vmw_surface_define_ioctl(struct drm_device *dev, void *data,
  591. struct drm_file *file_priv)
  592. {
  593. struct vmw_private *dev_priv = vmw_priv(dev);
  594. struct vmw_user_surface *user_srf;
  595. struct vmw_surface *srf;
  596. struct vmw_resource *res;
  597. struct vmw_resource *tmp;
  598. union drm_vmw_surface_create_arg *arg =
  599. (union drm_vmw_surface_create_arg *)data;
  600. struct drm_vmw_surface_create_req *req = &arg->req;
  601. struct drm_vmw_surface_arg *rep = &arg->rep;
  602. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  603. struct drm_vmw_size __user *user_sizes;
  604. int ret;
  605. int i, j;
  606. uint32_t cur_bo_offset;
  607. struct drm_vmw_size *cur_size;
  608. struct vmw_surface_offset *cur_offset;
  609. uint32_t num_sizes;
  610. uint32_t size;
  611. struct vmw_master *vmaster = vmw_master(file_priv->master);
  612. const struct svga3d_surface_desc *desc;
  613. if (unlikely(vmw_user_surface_size == 0))
  614. vmw_user_surface_size = ttm_round_pot(sizeof(*user_srf)) +
  615. 128;
  616. num_sizes = 0;
  617. for (i = 0; i < DRM_VMW_MAX_SURFACE_FACES; ++i)
  618. num_sizes += req->mip_levels[i];
  619. if (num_sizes > DRM_VMW_MAX_SURFACE_FACES *
  620. DRM_VMW_MAX_MIP_LEVELS)
  621. return -EINVAL;
  622. size = vmw_user_surface_size + 128 +
  623. ttm_round_pot(num_sizes * sizeof(struct drm_vmw_size)) +
  624. ttm_round_pot(num_sizes * sizeof(struct vmw_surface_offset));
  625. desc = svga3dsurface_get_desc(req->format);
  626. if (unlikely(desc->block_desc == SVGA3DBLOCKDESC_NONE)) {
  627. DRM_ERROR("Invalid surface format for surface creation.\n");
  628. return -EINVAL;
  629. }
  630. ret = ttm_read_lock(&vmaster->lock, true);
  631. if (unlikely(ret != 0))
  632. return ret;
  633. ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
  634. size, false, true);
  635. if (unlikely(ret != 0)) {
  636. if (ret != -ERESTARTSYS)
  637. DRM_ERROR("Out of graphics memory for surface"
  638. " creation.\n");
  639. goto out_unlock;
  640. }
  641. user_srf = kzalloc(sizeof(*user_srf), GFP_KERNEL);
  642. if (unlikely(user_srf == NULL)) {
  643. ret = -ENOMEM;
  644. goto out_no_user_srf;
  645. }
  646. srf = &user_srf->srf;
  647. res = &srf->res;
  648. srf->flags = req->flags;
  649. srf->format = req->format;
  650. srf->scanout = req->scanout;
  651. memcpy(srf->mip_levels, req->mip_levels, sizeof(srf->mip_levels));
  652. srf->num_sizes = num_sizes;
  653. user_srf->size = size;
  654. srf->sizes = kmalloc(srf->num_sizes * sizeof(*srf->sizes), GFP_KERNEL);
  655. if (unlikely(srf->sizes == NULL)) {
  656. ret = -ENOMEM;
  657. goto out_no_sizes;
  658. }
  659. srf->offsets = kmalloc(srf->num_sizes * sizeof(*srf->offsets),
  660. GFP_KERNEL);
  661. if (unlikely(srf->sizes == NULL)) {
  662. ret = -ENOMEM;
  663. goto out_no_offsets;
  664. }
  665. user_sizes = (struct drm_vmw_size __user *)(unsigned long)
  666. req->size_addr;
  667. ret = copy_from_user(srf->sizes, user_sizes,
  668. srf->num_sizes * sizeof(*srf->sizes));
  669. if (unlikely(ret != 0)) {
  670. ret = -EFAULT;
  671. goto out_no_copy;
  672. }
  673. srf->base_size = *srf->sizes;
  674. srf->autogen_filter = SVGA3D_TEX_FILTER_NONE;
  675. srf->multisample_count = 0;
  676. cur_bo_offset = 0;
  677. cur_offset = srf->offsets;
  678. cur_size = srf->sizes;
  679. for (i = 0; i < DRM_VMW_MAX_SURFACE_FACES; ++i) {
  680. for (j = 0; j < srf->mip_levels[i]; ++j) {
  681. uint32_t stride = svga3dsurface_calculate_pitch
  682. (desc, cur_size);
  683. cur_offset->face = i;
  684. cur_offset->mip = j;
  685. cur_offset->bo_offset = cur_bo_offset;
  686. cur_bo_offset += svga3dsurface_get_image_buffer_size
  687. (desc, cur_size, stride);
  688. ++cur_offset;
  689. ++cur_size;
  690. }
  691. }
  692. res->backup_size = cur_bo_offset;
  693. if (srf->scanout &&
  694. srf->num_sizes == 1 &&
  695. srf->sizes[0].width == 64 &&
  696. srf->sizes[0].height == 64 &&
  697. srf->format == SVGA3D_A8R8G8B8) {
  698. srf->snooper.image = kmalloc(64 * 64 * 4, GFP_KERNEL);
  699. /* clear the image */
  700. if (srf->snooper.image) {
  701. memset(srf->snooper.image, 0x00, 64 * 64 * 4);
  702. } else {
  703. DRM_ERROR("Failed to allocate cursor_image\n");
  704. ret = -ENOMEM;
  705. goto out_no_copy;
  706. }
  707. } else {
  708. srf->snooper.image = NULL;
  709. }
  710. srf->snooper.crtc = NULL;
  711. user_srf->prime.base.shareable = false;
  712. user_srf->prime.base.tfile = NULL;
  713. /**
  714. * From this point, the generic resource management functions
  715. * destroy the object on failure.
  716. */
  717. ret = vmw_surface_init(dev_priv, srf, vmw_user_surface_free);
  718. if (unlikely(ret != 0))
  719. goto out_unlock;
  720. /*
  721. * A gb-aware client referencing a shared surface will
  722. * expect a backup buffer to be present.
  723. */
  724. if (dev_priv->has_mob && req->shareable) {
  725. uint32_t backup_handle;
  726. ret = vmw_user_dmabuf_alloc(dev_priv, tfile,
  727. res->backup_size,
  728. true,
  729. &backup_handle,
  730. &res->backup);
  731. if (unlikely(ret != 0)) {
  732. vmw_resource_unreference(&res);
  733. goto out_unlock;
  734. }
  735. }
  736. tmp = vmw_resource_reference(&srf->res);
  737. ret = ttm_prime_object_init(tfile, res->backup_size, &user_srf->prime,
  738. req->shareable, VMW_RES_SURFACE,
  739. &vmw_user_surface_base_release, NULL);
  740. if (unlikely(ret != 0)) {
  741. vmw_resource_unreference(&tmp);
  742. vmw_resource_unreference(&res);
  743. goto out_unlock;
  744. }
  745. rep->sid = user_srf->prime.base.hash.key;
  746. vmw_resource_unreference(&res);
  747. ttm_read_unlock(&vmaster->lock);
  748. return 0;
  749. out_no_copy:
  750. kfree(srf->offsets);
  751. out_no_offsets:
  752. kfree(srf->sizes);
  753. out_no_sizes:
  754. ttm_prime_object_kfree(user_srf, prime);
  755. out_no_user_srf:
  756. ttm_mem_global_free(vmw_mem_glob(dev_priv), size);
  757. out_unlock:
  758. ttm_read_unlock(&vmaster->lock);
  759. return ret;
  760. }
  761. /**
  762. * vmw_user_surface_define_ioctl - Ioctl function implementing
  763. * the user surface reference functionality.
  764. *
  765. * @dev: Pointer to a struct drm_device.
  766. * @data: Pointer to data copied from / to user-space.
  767. * @file_priv: Pointer to a drm file private structure.
  768. */
  769. int vmw_surface_reference_ioctl(struct drm_device *dev, void *data,
  770. struct drm_file *file_priv)
  771. {
  772. struct vmw_private *dev_priv = vmw_priv(dev);
  773. union drm_vmw_surface_reference_arg *arg =
  774. (union drm_vmw_surface_reference_arg *)data;
  775. struct drm_vmw_surface_arg *req = &arg->req;
  776. struct drm_vmw_surface_create_req *rep = &arg->rep;
  777. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  778. struct vmw_surface *srf;
  779. struct vmw_user_surface *user_srf;
  780. struct drm_vmw_size __user *user_sizes;
  781. struct ttm_base_object *base;
  782. int ret = -EINVAL;
  783. base = ttm_base_object_lookup_for_ref(dev_priv->tdev, req->sid);
  784. if (unlikely(base == NULL)) {
  785. DRM_ERROR("Could not find surface to reference.\n");
  786. return -EINVAL;
  787. }
  788. if (unlikely(ttm_base_object_type(base) != VMW_RES_SURFACE))
  789. goto out_bad_resource;
  790. user_srf = container_of(base, struct vmw_user_surface, prime.base);
  791. srf = &user_srf->srf;
  792. ret = ttm_ref_object_add(tfile, &user_srf->prime.base,
  793. TTM_REF_USAGE, NULL);
  794. if (unlikely(ret != 0)) {
  795. DRM_ERROR("Could not add a reference to a surface.\n");
  796. goto out_no_reference;
  797. }
  798. rep->flags = srf->flags;
  799. rep->format = srf->format;
  800. memcpy(rep->mip_levels, srf->mip_levels, sizeof(srf->mip_levels));
  801. user_sizes = (struct drm_vmw_size __user *)(unsigned long)
  802. rep->size_addr;
  803. if (user_sizes)
  804. ret = copy_to_user(user_sizes, &srf->base_size,
  805. sizeof(srf->base_size));
  806. if (unlikely(ret != 0)) {
  807. DRM_ERROR("copy_to_user failed %p %u\n",
  808. user_sizes, srf->num_sizes);
  809. ret = -EFAULT;
  810. }
  811. out_bad_resource:
  812. out_no_reference:
  813. ttm_base_object_unref(&base);
  814. return ret;
  815. }
  816. /**
  817. * vmw_surface_define_encode - Encode a surface_define command.
  818. *
  819. * @srf: Pointer to a struct vmw_surface object.
  820. * @cmd_space: Pointer to memory area in which the commands should be encoded.
  821. */
  822. static int vmw_gb_surface_create(struct vmw_resource *res)
  823. {
  824. struct vmw_private *dev_priv = res->dev_priv;
  825. struct vmw_surface *srf = vmw_res_to_srf(res);
  826. uint32_t cmd_len, submit_len;
  827. int ret;
  828. struct {
  829. SVGA3dCmdHeader header;
  830. SVGA3dCmdDefineGBSurface body;
  831. } *cmd;
  832. if (likely(res->id != -1))
  833. return 0;
  834. (void) vmw_3d_resource_inc(dev_priv, false);
  835. ret = vmw_resource_alloc_id(res);
  836. if (unlikely(ret != 0)) {
  837. DRM_ERROR("Failed to allocate a surface id.\n");
  838. goto out_no_id;
  839. }
  840. if (unlikely(res->id >= VMWGFX_NUM_GB_SURFACE)) {
  841. ret = -EBUSY;
  842. goto out_no_fifo;
  843. }
  844. cmd_len = sizeof(cmd->body);
  845. submit_len = sizeof(*cmd);
  846. cmd = vmw_fifo_reserve(dev_priv, submit_len);
  847. if (unlikely(cmd == NULL)) {
  848. DRM_ERROR("Failed reserving FIFO space for surface "
  849. "creation.\n");
  850. ret = -ENOMEM;
  851. goto out_no_fifo;
  852. }
  853. cmd->header.id = SVGA_3D_CMD_DEFINE_GB_SURFACE;
  854. cmd->header.size = cmd_len;
  855. cmd->body.sid = srf->res.id;
  856. cmd->body.surfaceFlags = srf->flags;
  857. cmd->body.format = cpu_to_le32(srf->format);
  858. cmd->body.numMipLevels = srf->mip_levels[0];
  859. cmd->body.multisampleCount = srf->multisample_count;
  860. cmd->body.autogenFilter = srf->autogen_filter;
  861. cmd->body.size.width = srf->base_size.width;
  862. cmd->body.size.height = srf->base_size.height;
  863. cmd->body.size.depth = srf->base_size.depth;
  864. vmw_fifo_commit(dev_priv, submit_len);
  865. return 0;
  866. out_no_fifo:
  867. vmw_resource_release_id(res);
  868. out_no_id:
  869. vmw_3d_resource_dec(dev_priv, false);
  870. return ret;
  871. }
  872. static int vmw_gb_surface_bind(struct vmw_resource *res,
  873. struct ttm_validate_buffer *val_buf)
  874. {
  875. struct vmw_private *dev_priv = res->dev_priv;
  876. struct {
  877. SVGA3dCmdHeader header;
  878. SVGA3dCmdBindGBSurface body;
  879. } *cmd1;
  880. struct {
  881. SVGA3dCmdHeader header;
  882. SVGA3dCmdUpdateGBSurface body;
  883. } *cmd2;
  884. uint32_t submit_size;
  885. struct ttm_buffer_object *bo = val_buf->bo;
  886. BUG_ON(bo->mem.mem_type != VMW_PL_MOB);
  887. submit_size = sizeof(*cmd1) + (res->backup_dirty ? sizeof(*cmd2) : 0);
  888. cmd1 = vmw_fifo_reserve(dev_priv, submit_size);
  889. if (unlikely(cmd1 == NULL)) {
  890. DRM_ERROR("Failed reserving FIFO space for surface "
  891. "binding.\n");
  892. return -ENOMEM;
  893. }
  894. cmd1->header.id = SVGA_3D_CMD_BIND_GB_SURFACE;
  895. cmd1->header.size = sizeof(cmd1->body);
  896. cmd1->body.sid = res->id;
  897. cmd1->body.mobid = bo->mem.start;
  898. if (res->backup_dirty) {
  899. cmd2 = (void *) &cmd1[1];
  900. cmd2->header.id = SVGA_3D_CMD_UPDATE_GB_SURFACE;
  901. cmd2->header.size = sizeof(cmd2->body);
  902. cmd2->body.sid = res->id;
  903. res->backup_dirty = false;
  904. }
  905. vmw_fifo_commit(dev_priv, submit_size);
  906. return 0;
  907. }
  908. static int vmw_gb_surface_unbind(struct vmw_resource *res,
  909. bool readback,
  910. struct ttm_validate_buffer *val_buf)
  911. {
  912. struct vmw_private *dev_priv = res->dev_priv;
  913. struct ttm_buffer_object *bo = val_buf->bo;
  914. struct vmw_fence_obj *fence;
  915. struct {
  916. SVGA3dCmdHeader header;
  917. SVGA3dCmdReadbackGBSurface body;
  918. } *cmd1;
  919. struct {
  920. SVGA3dCmdHeader header;
  921. SVGA3dCmdInvalidateGBSurface body;
  922. } *cmd2;
  923. struct {
  924. SVGA3dCmdHeader header;
  925. SVGA3dCmdBindGBSurface body;
  926. } *cmd3;
  927. uint32_t submit_size;
  928. uint8_t *cmd;
  929. BUG_ON(bo->mem.mem_type != VMW_PL_MOB);
  930. submit_size = sizeof(*cmd3) + (readback ? sizeof(*cmd1) : sizeof(*cmd2));
  931. cmd = vmw_fifo_reserve(dev_priv, submit_size);
  932. if (unlikely(cmd == NULL)) {
  933. DRM_ERROR("Failed reserving FIFO space for surface "
  934. "unbinding.\n");
  935. return -ENOMEM;
  936. }
  937. if (readback) {
  938. cmd1 = (void *) cmd;
  939. cmd1->header.id = SVGA_3D_CMD_READBACK_GB_SURFACE;
  940. cmd1->header.size = sizeof(cmd1->body);
  941. cmd1->body.sid = res->id;
  942. cmd3 = (void *) &cmd1[1];
  943. } else {
  944. cmd2 = (void *) cmd;
  945. cmd2->header.id = SVGA_3D_CMD_INVALIDATE_GB_SURFACE;
  946. cmd2->header.size = sizeof(cmd2->body);
  947. cmd2->body.sid = res->id;
  948. cmd3 = (void *) &cmd2[1];
  949. }
  950. cmd3->header.id = SVGA_3D_CMD_BIND_GB_SURFACE;
  951. cmd3->header.size = sizeof(cmd3->body);
  952. cmd3->body.sid = res->id;
  953. cmd3->body.mobid = SVGA3D_INVALID_ID;
  954. vmw_fifo_commit(dev_priv, submit_size);
  955. /*
  956. * Create a fence object and fence the backup buffer.
  957. */
  958. (void) vmw_execbuf_fence_commands(NULL, dev_priv,
  959. &fence, NULL);
  960. vmw_fence_single_bo(val_buf->bo, fence);
  961. if (likely(fence != NULL))
  962. vmw_fence_obj_unreference(&fence);
  963. return 0;
  964. }
  965. static int vmw_gb_surface_destroy(struct vmw_resource *res)
  966. {
  967. struct vmw_private *dev_priv = res->dev_priv;
  968. struct {
  969. SVGA3dCmdHeader header;
  970. SVGA3dCmdDestroyGBSurface body;
  971. } *cmd;
  972. if (likely(res->id == -1))
  973. return 0;
  974. mutex_lock(&dev_priv->binding_mutex);
  975. vmw_context_binding_res_list_scrub(&res->binding_head);
  976. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  977. if (unlikely(cmd == NULL)) {
  978. DRM_ERROR("Failed reserving FIFO space for surface "
  979. "destruction.\n");
  980. mutex_unlock(&dev_priv->binding_mutex);
  981. return -ENOMEM;
  982. }
  983. cmd->header.id = SVGA_3D_CMD_DESTROY_GB_SURFACE;
  984. cmd->header.size = sizeof(cmd->body);
  985. cmd->body.sid = res->id;
  986. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  987. mutex_unlock(&dev_priv->binding_mutex);
  988. vmw_resource_release_id(res);
  989. vmw_3d_resource_dec(dev_priv, false);
  990. return 0;
  991. }
  992. /**
  993. * vmw_gb_surface_define_ioctl - Ioctl function implementing
  994. * the user surface define functionality.
  995. *
  996. * @dev: Pointer to a struct drm_device.
  997. * @data: Pointer to data copied from / to user-space.
  998. * @file_priv: Pointer to a drm file private structure.
  999. */
  1000. int vmw_gb_surface_define_ioctl(struct drm_device *dev, void *data,
  1001. struct drm_file *file_priv)
  1002. {
  1003. struct vmw_private *dev_priv = vmw_priv(dev);
  1004. struct vmw_user_surface *user_srf;
  1005. struct vmw_surface *srf;
  1006. struct vmw_resource *res;
  1007. struct vmw_resource *tmp;
  1008. union drm_vmw_gb_surface_create_arg *arg =
  1009. (union drm_vmw_gb_surface_create_arg *)data;
  1010. struct drm_vmw_gb_surface_create_req *req = &arg->req;
  1011. struct drm_vmw_gb_surface_create_rep *rep = &arg->rep;
  1012. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  1013. int ret;
  1014. uint32_t size;
  1015. struct vmw_master *vmaster = vmw_master(file_priv->master);
  1016. const struct svga3d_surface_desc *desc;
  1017. uint32_t backup_handle;
  1018. if (unlikely(vmw_user_surface_size == 0))
  1019. vmw_user_surface_size = ttm_round_pot(sizeof(*user_srf)) +
  1020. 128;
  1021. size = vmw_user_surface_size + 128;
  1022. desc = svga3dsurface_get_desc(req->format);
  1023. if (unlikely(desc->block_desc == SVGA3DBLOCKDESC_NONE)) {
  1024. DRM_ERROR("Invalid surface format for surface creation.\n");
  1025. return -EINVAL;
  1026. }
  1027. ret = ttm_read_lock(&vmaster->lock, true);
  1028. if (unlikely(ret != 0))
  1029. return ret;
  1030. ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
  1031. size, false, true);
  1032. if (unlikely(ret != 0)) {
  1033. if (ret != -ERESTARTSYS)
  1034. DRM_ERROR("Out of graphics memory for surface"
  1035. " creation.\n");
  1036. goto out_unlock;
  1037. }
  1038. user_srf = kzalloc(sizeof(*user_srf), GFP_KERNEL);
  1039. if (unlikely(user_srf == NULL)) {
  1040. ret = -ENOMEM;
  1041. goto out_no_user_srf;
  1042. }
  1043. srf = &user_srf->srf;
  1044. res = &srf->res;
  1045. srf->flags = req->svga3d_flags;
  1046. srf->format = req->format;
  1047. srf->scanout = req->drm_surface_flags & drm_vmw_surface_flag_scanout;
  1048. srf->mip_levels[0] = req->mip_levels;
  1049. srf->num_sizes = 1;
  1050. srf->sizes = NULL;
  1051. srf->offsets = NULL;
  1052. user_srf->size = size;
  1053. srf->base_size = req->base_size;
  1054. srf->autogen_filter = SVGA3D_TEX_FILTER_NONE;
  1055. srf->multisample_count = req->multisample_count;
  1056. res->backup_size = svga3dsurface_get_serialized_size
  1057. (srf->format, srf->base_size, srf->mip_levels[0],
  1058. srf->flags & SVGA3D_SURFACE_CUBEMAP);
  1059. user_srf->prime.base.shareable = false;
  1060. user_srf->prime.base.tfile = NULL;
  1061. /**
  1062. * From this point, the generic resource management functions
  1063. * destroy the object on failure.
  1064. */
  1065. ret = vmw_surface_init(dev_priv, srf, vmw_user_surface_free);
  1066. if (unlikely(ret != 0))
  1067. goto out_unlock;
  1068. if (req->buffer_handle != SVGA3D_INVALID_ID) {
  1069. ret = vmw_user_dmabuf_lookup(tfile, req->buffer_handle,
  1070. &res->backup);
  1071. } else if (req->drm_surface_flags &
  1072. drm_vmw_surface_flag_create_buffer)
  1073. ret = vmw_user_dmabuf_alloc(dev_priv, tfile,
  1074. res->backup_size,
  1075. req->drm_surface_flags &
  1076. drm_vmw_surface_flag_shareable,
  1077. &backup_handle,
  1078. &res->backup);
  1079. if (unlikely(ret != 0)) {
  1080. vmw_resource_unreference(&res);
  1081. goto out_unlock;
  1082. }
  1083. tmp = vmw_resource_reference(&srf->res);
  1084. ret = ttm_prime_object_init(tfile, res->backup_size, &user_srf->prime,
  1085. req->drm_surface_flags &
  1086. drm_vmw_surface_flag_shareable,
  1087. VMW_RES_SURFACE,
  1088. &vmw_user_surface_base_release, NULL);
  1089. if (unlikely(ret != 0)) {
  1090. vmw_resource_unreference(&tmp);
  1091. vmw_resource_unreference(&res);
  1092. goto out_unlock;
  1093. }
  1094. rep->handle = user_srf->prime.base.hash.key;
  1095. rep->backup_size = res->backup_size;
  1096. if (res->backup) {
  1097. rep->buffer_map_handle =
  1098. drm_vma_node_offset_addr(&res->backup->base.vma_node);
  1099. rep->buffer_size = res->backup->base.num_pages * PAGE_SIZE;
  1100. rep->buffer_handle = backup_handle;
  1101. } else {
  1102. rep->buffer_map_handle = 0;
  1103. rep->buffer_size = 0;
  1104. rep->buffer_handle = SVGA3D_INVALID_ID;
  1105. }
  1106. vmw_resource_unreference(&res);
  1107. ttm_read_unlock(&vmaster->lock);
  1108. return 0;
  1109. out_no_user_srf:
  1110. ttm_mem_global_free(vmw_mem_glob(dev_priv), size);
  1111. out_unlock:
  1112. ttm_read_unlock(&vmaster->lock);
  1113. return ret;
  1114. }
  1115. /**
  1116. * vmw_gb_surface_reference_ioctl - Ioctl function implementing
  1117. * the user surface reference functionality.
  1118. *
  1119. * @dev: Pointer to a struct drm_device.
  1120. * @data: Pointer to data copied from / to user-space.
  1121. * @file_priv: Pointer to a drm file private structure.
  1122. */
  1123. int vmw_gb_surface_reference_ioctl(struct drm_device *dev, void *data,
  1124. struct drm_file *file_priv)
  1125. {
  1126. struct vmw_private *dev_priv = vmw_priv(dev);
  1127. union drm_vmw_gb_surface_reference_arg *arg =
  1128. (union drm_vmw_gb_surface_reference_arg *)data;
  1129. struct drm_vmw_surface_arg *req = &arg->req;
  1130. struct drm_vmw_gb_surface_ref_rep *rep = &arg->rep;
  1131. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  1132. struct vmw_surface *srf;
  1133. struct vmw_user_surface *user_srf;
  1134. struct ttm_base_object *base;
  1135. uint32_t backup_handle;
  1136. int ret = -EINVAL;
  1137. base = ttm_base_object_lookup_for_ref(dev_priv->tdev, req->sid);
  1138. if (unlikely(base == NULL)) {
  1139. DRM_ERROR("Could not find surface to reference.\n");
  1140. return -EINVAL;
  1141. }
  1142. if (unlikely(ttm_base_object_type(base) != VMW_RES_SURFACE))
  1143. goto out_bad_resource;
  1144. user_srf = container_of(base, struct vmw_user_surface, prime.base);
  1145. srf = &user_srf->srf;
  1146. if (srf->res.backup == NULL) {
  1147. DRM_ERROR("Shared GB surface is missing a backup buffer.\n");
  1148. goto out_bad_resource;
  1149. }
  1150. ret = ttm_ref_object_add(tfile, &user_srf->prime.base,
  1151. TTM_REF_USAGE, NULL);
  1152. if (unlikely(ret != 0)) {
  1153. DRM_ERROR("Could not add a reference to a GB surface.\n");
  1154. goto out_bad_resource;
  1155. }
  1156. mutex_lock(&dev_priv->cmdbuf_mutex); /* Protect res->backup */
  1157. ret = vmw_user_dmabuf_reference(tfile, srf->res.backup,
  1158. &backup_handle);
  1159. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1160. if (unlikely(ret != 0)) {
  1161. DRM_ERROR("Could not add a reference to a GB surface "
  1162. "backup buffer.\n");
  1163. (void) ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  1164. req->sid,
  1165. TTM_REF_USAGE);
  1166. goto out_bad_resource;
  1167. }
  1168. rep->creq.svga3d_flags = srf->flags;
  1169. rep->creq.format = srf->format;
  1170. rep->creq.mip_levels = srf->mip_levels[0];
  1171. rep->creq.drm_surface_flags = 0;
  1172. rep->creq.multisample_count = srf->multisample_count;
  1173. rep->creq.autogen_filter = srf->autogen_filter;
  1174. rep->creq.buffer_handle = backup_handle;
  1175. rep->creq.base_size = srf->base_size;
  1176. rep->crep.handle = user_srf->prime.base.hash.key;
  1177. rep->crep.backup_size = srf->res.backup_size;
  1178. rep->crep.buffer_handle = backup_handle;
  1179. rep->crep.buffer_map_handle =
  1180. drm_vma_node_offset_addr(&srf->res.backup->base.vma_node);
  1181. rep->crep.buffer_size = srf->res.backup->base.num_pages * PAGE_SIZE;
  1182. out_bad_resource:
  1183. ttm_base_object_unref(&base);
  1184. return ret;
  1185. }