vmwgfx_surface.c 44 KB

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