vmwgfx_execbuf.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005
  1. /**************************************************************************
  2. *
  3. * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. #include "vmwgfx_drv.h"
  28. #include "vmwgfx_reg.h"
  29. #include <drm/ttm/ttm_bo_api.h>
  30. #include <drm/ttm/ttm_placement.h>
  31. #define VMW_RES_HT_ORDER 12
  32. /**
  33. * struct vmw_resource_relocation - Relocation info for resources
  34. *
  35. * @head: List head for the software context's relocation list.
  36. * @res: Non-ref-counted pointer to the resource.
  37. * @offset: Offset of 4 byte entries into the command buffer where the
  38. * id that needs fixup is located.
  39. */
  40. struct vmw_resource_relocation {
  41. struct list_head head;
  42. const struct vmw_resource *res;
  43. unsigned long offset;
  44. };
  45. /**
  46. * struct vmw_resource_val_node - Validation info for resources
  47. *
  48. * @head: List head for the software context's resource list.
  49. * @hash: Hash entry for quick resouce to val_node lookup.
  50. * @res: Ref-counted pointer to the resource.
  51. * @switch_backup: Boolean whether to switch backup buffer on unreserve.
  52. * @new_backup: Refcounted pointer to the new backup buffer.
  53. * @new_backup_offset: New backup buffer offset if @new_backup is non-NUll.
  54. * @first_usage: Set to true the first time the resource is referenced in
  55. * the command stream.
  56. * @no_buffer_needed: Resources do not need to allocate buffer backup on
  57. * reservation. The command stream will provide one.
  58. */
  59. struct vmw_resource_val_node {
  60. struct list_head head;
  61. struct drm_hash_item hash;
  62. struct vmw_resource *res;
  63. struct vmw_dma_buffer *new_backup;
  64. unsigned long new_backup_offset;
  65. bool first_usage;
  66. bool no_buffer_needed;
  67. };
  68. /**
  69. * vmw_resource_unreserve - unreserve resources previously reserved for
  70. * command submission.
  71. *
  72. * @list_head: list of resources to unreserve.
  73. * @backoff: Whether command submission failed.
  74. */
  75. static void vmw_resource_list_unreserve(struct list_head *list,
  76. bool backoff)
  77. {
  78. struct vmw_resource_val_node *val;
  79. list_for_each_entry(val, list, head) {
  80. struct vmw_resource *res = val->res;
  81. struct vmw_dma_buffer *new_backup =
  82. backoff ? NULL : val->new_backup;
  83. vmw_resource_unreserve(res, new_backup,
  84. val->new_backup_offset);
  85. vmw_dmabuf_unreference(&val->new_backup);
  86. }
  87. }
  88. /**
  89. * vmw_resource_val_add - Add a resource to the software context's
  90. * resource list if it's not already on it.
  91. *
  92. * @sw_context: Pointer to the software context.
  93. * @res: Pointer to the resource.
  94. * @p_node On successful return points to a valid pointer to a
  95. * struct vmw_resource_val_node, if non-NULL on entry.
  96. */
  97. static int vmw_resource_val_add(struct vmw_sw_context *sw_context,
  98. struct vmw_resource *res,
  99. struct vmw_resource_val_node **p_node)
  100. {
  101. struct vmw_resource_val_node *node;
  102. struct drm_hash_item *hash;
  103. int ret;
  104. if (likely(drm_ht_find_item(&sw_context->res_ht, (unsigned long) res,
  105. &hash) == 0)) {
  106. node = container_of(hash, struct vmw_resource_val_node, hash);
  107. node->first_usage = false;
  108. if (unlikely(p_node != NULL))
  109. *p_node = node;
  110. return 0;
  111. }
  112. node = kzalloc(sizeof(*node), GFP_KERNEL);
  113. if (unlikely(node == NULL)) {
  114. DRM_ERROR("Failed to allocate a resource validation "
  115. "entry.\n");
  116. return -ENOMEM;
  117. }
  118. node->hash.key = (unsigned long) res;
  119. ret = drm_ht_insert_item(&sw_context->res_ht, &node->hash);
  120. if (unlikely(ret != 0)) {
  121. DRM_ERROR("Failed to initialize a resource validation "
  122. "entry.\n");
  123. kfree(node);
  124. return ret;
  125. }
  126. list_add_tail(&node->head, &sw_context->resource_list);
  127. node->res = vmw_resource_reference(res);
  128. node->first_usage = true;
  129. if (unlikely(p_node != NULL))
  130. *p_node = node;
  131. return 0;
  132. }
  133. /**
  134. * vmw_resource_relocation_add - Add a relocation to the relocation list
  135. *
  136. * @list: Pointer to head of relocation list.
  137. * @res: The resource.
  138. * @offset: Offset into the command buffer currently being parsed where the
  139. * id that needs fixup is located. Granularity is 4 bytes.
  140. */
  141. static int vmw_resource_relocation_add(struct list_head *list,
  142. const struct vmw_resource *res,
  143. unsigned long offset)
  144. {
  145. struct vmw_resource_relocation *rel;
  146. rel = kmalloc(sizeof(*rel), GFP_KERNEL);
  147. if (unlikely(rel == NULL)) {
  148. DRM_ERROR("Failed to allocate a resource relocation.\n");
  149. return -ENOMEM;
  150. }
  151. rel->res = res;
  152. rel->offset = offset;
  153. list_add_tail(&rel->head, list);
  154. return 0;
  155. }
  156. /**
  157. * vmw_resource_relocations_free - Free all relocations on a list
  158. *
  159. * @list: Pointer to the head of the relocation list.
  160. */
  161. static void vmw_resource_relocations_free(struct list_head *list)
  162. {
  163. struct vmw_resource_relocation *rel, *n;
  164. list_for_each_entry_safe(rel, n, list, head) {
  165. list_del(&rel->head);
  166. kfree(rel);
  167. }
  168. }
  169. /**
  170. * vmw_resource_relocations_apply - Apply all relocations on a list
  171. *
  172. * @cb: Pointer to the start of the command buffer bein patch. This need
  173. * not be the same buffer as the one being parsed when the relocation
  174. * list was built, but the contents must be the same modulo the
  175. * resource ids.
  176. * @list: Pointer to the head of the relocation list.
  177. */
  178. static void vmw_resource_relocations_apply(uint32_t *cb,
  179. struct list_head *list)
  180. {
  181. struct vmw_resource_relocation *rel;
  182. list_for_each_entry(rel, list, head)
  183. cb[rel->offset] = rel->res->id;
  184. }
  185. static int vmw_cmd_invalid(struct vmw_private *dev_priv,
  186. struct vmw_sw_context *sw_context,
  187. SVGA3dCmdHeader *header)
  188. {
  189. return capable(CAP_SYS_ADMIN) ? : -EINVAL;
  190. }
  191. static int vmw_cmd_ok(struct vmw_private *dev_priv,
  192. struct vmw_sw_context *sw_context,
  193. SVGA3dCmdHeader *header)
  194. {
  195. return 0;
  196. }
  197. /**
  198. * vmw_bo_to_validate_list - add a bo to a validate list
  199. *
  200. * @sw_context: The software context used for this command submission batch.
  201. * @bo: The buffer object to add.
  202. * @validate_as_mob: Validate this buffer as a MOB.
  203. * @p_val_node: If non-NULL Will be updated with the validate node number
  204. * on return.
  205. *
  206. * Returns -EINVAL if the limit of number of buffer objects per command
  207. * submission is reached.
  208. */
  209. static int vmw_bo_to_validate_list(struct vmw_sw_context *sw_context,
  210. struct ttm_buffer_object *bo,
  211. bool validate_as_mob,
  212. uint32_t *p_val_node)
  213. {
  214. uint32_t val_node;
  215. struct vmw_validate_buffer *vval_buf;
  216. struct ttm_validate_buffer *val_buf;
  217. struct drm_hash_item *hash;
  218. int ret;
  219. if (likely(drm_ht_find_item(&sw_context->res_ht, (unsigned long) bo,
  220. &hash) == 0)) {
  221. vval_buf = container_of(hash, struct vmw_validate_buffer,
  222. hash);
  223. if (unlikely(vval_buf->validate_as_mob != validate_as_mob)) {
  224. DRM_ERROR("Inconsistent buffer usage.\n");
  225. return -EINVAL;
  226. }
  227. val_buf = &vval_buf->base;
  228. val_node = vval_buf - sw_context->val_bufs;
  229. } else {
  230. val_node = sw_context->cur_val_buf;
  231. if (unlikely(val_node >= VMWGFX_MAX_VALIDATIONS)) {
  232. DRM_ERROR("Max number of DMA buffers per submission "
  233. "exceeded.\n");
  234. return -EINVAL;
  235. }
  236. vval_buf = &sw_context->val_bufs[val_node];
  237. vval_buf->hash.key = (unsigned long) bo;
  238. ret = drm_ht_insert_item(&sw_context->res_ht, &vval_buf->hash);
  239. if (unlikely(ret != 0)) {
  240. DRM_ERROR("Failed to initialize a buffer validation "
  241. "entry.\n");
  242. return ret;
  243. }
  244. ++sw_context->cur_val_buf;
  245. val_buf = &vval_buf->base;
  246. val_buf->bo = ttm_bo_reference(bo);
  247. val_buf->reserved = false;
  248. list_add_tail(&val_buf->head, &sw_context->validate_nodes);
  249. vval_buf->validate_as_mob = validate_as_mob;
  250. }
  251. sw_context->fence_flags |= DRM_VMW_FENCE_FLAG_EXEC;
  252. if (p_val_node)
  253. *p_val_node = val_node;
  254. return 0;
  255. }
  256. /**
  257. * vmw_resources_reserve - Reserve all resources on the sw_context's
  258. * resource list.
  259. *
  260. * @sw_context: Pointer to the software context.
  261. *
  262. * Note that since vmware's command submission currently is protected by
  263. * the cmdbuf mutex, no fancy deadlock avoidance is required for resources,
  264. * since only a single thread at once will attempt this.
  265. */
  266. static int vmw_resources_reserve(struct vmw_sw_context *sw_context)
  267. {
  268. struct vmw_resource_val_node *val;
  269. int ret;
  270. list_for_each_entry(val, &sw_context->resource_list, head) {
  271. struct vmw_resource *res = val->res;
  272. ret = vmw_resource_reserve(res, val->no_buffer_needed);
  273. if (unlikely(ret != 0))
  274. return ret;
  275. if (res->backup) {
  276. struct ttm_buffer_object *bo = &res->backup->base;
  277. ret = vmw_bo_to_validate_list
  278. (sw_context, bo,
  279. vmw_resource_needs_backup(res), NULL);
  280. if (unlikely(ret != 0))
  281. return ret;
  282. }
  283. }
  284. return 0;
  285. }
  286. /**
  287. * vmw_resources_validate - Validate all resources on the sw_context's
  288. * resource list.
  289. *
  290. * @sw_context: Pointer to the software context.
  291. *
  292. * Before this function is called, all resource backup buffers must have
  293. * been validated.
  294. */
  295. static int vmw_resources_validate(struct vmw_sw_context *sw_context)
  296. {
  297. struct vmw_resource_val_node *val;
  298. int ret;
  299. list_for_each_entry(val, &sw_context->resource_list, head) {
  300. struct vmw_resource *res = val->res;
  301. ret = vmw_resource_validate(res);
  302. if (unlikely(ret != 0)) {
  303. if (ret != -ERESTARTSYS)
  304. DRM_ERROR("Failed to validate resource.\n");
  305. return ret;
  306. }
  307. }
  308. return 0;
  309. }
  310. /**
  311. * vmw_cmd_res_check - Check that a resource is present and if so, put it
  312. * on the resource validate list unless it's already there.
  313. *
  314. * @dev_priv: Pointer to a device private structure.
  315. * @sw_context: Pointer to the software context.
  316. * @res_type: Resource type.
  317. * @converter: User-space visisble type specific information.
  318. * @id: Pointer to the location in the command buffer currently being
  319. * parsed from where the user-space resource id handle is located.
  320. */
  321. static int vmw_cmd_res_check(struct vmw_private *dev_priv,
  322. struct vmw_sw_context *sw_context,
  323. enum vmw_res_type res_type,
  324. const struct vmw_user_resource_conv *converter,
  325. uint32_t *id,
  326. struct vmw_resource_val_node **p_val)
  327. {
  328. struct vmw_res_cache_entry *rcache =
  329. &sw_context->res_cache[res_type];
  330. struct vmw_resource *res;
  331. struct vmw_resource_val_node *node;
  332. int ret;
  333. if (*id == SVGA3D_INVALID_ID)
  334. return 0;
  335. /*
  336. * Fastpath in case of repeated commands referencing the same
  337. * resource
  338. */
  339. if (likely(rcache->valid && *id == rcache->handle)) {
  340. const struct vmw_resource *res = rcache->res;
  341. rcache->node->first_usage = false;
  342. if (p_val)
  343. *p_val = rcache->node;
  344. return vmw_resource_relocation_add
  345. (&sw_context->res_relocations, res,
  346. id - sw_context->buf_start);
  347. }
  348. ret = vmw_user_resource_lookup_handle(dev_priv,
  349. sw_context->tfile,
  350. *id,
  351. converter,
  352. &res);
  353. if (unlikely(ret != 0)) {
  354. DRM_ERROR("Could not find or use resource 0x%08x.\n",
  355. (unsigned) *id);
  356. dump_stack();
  357. return ret;
  358. }
  359. rcache->valid = true;
  360. rcache->res = res;
  361. rcache->handle = *id;
  362. ret = vmw_resource_relocation_add(&sw_context->res_relocations,
  363. res,
  364. id - sw_context->buf_start);
  365. if (unlikely(ret != 0))
  366. goto out_no_reloc;
  367. ret = vmw_resource_val_add(sw_context, res, &node);
  368. if (unlikely(ret != 0))
  369. goto out_no_reloc;
  370. rcache->node = node;
  371. if (p_val)
  372. *p_val = node;
  373. vmw_resource_unreference(&res);
  374. return 0;
  375. out_no_reloc:
  376. BUG_ON(sw_context->error_resource != NULL);
  377. sw_context->error_resource = res;
  378. return ret;
  379. }
  380. /**
  381. * vmw_cmd_cid_check - Check a command header for valid context information.
  382. *
  383. * @dev_priv: Pointer to a device private structure.
  384. * @sw_context: Pointer to the software context.
  385. * @header: A command header with an embedded user-space context handle.
  386. *
  387. * Convenience function: Call vmw_cmd_res_check with the user-space context
  388. * handle embedded in @header.
  389. */
  390. static int vmw_cmd_cid_check(struct vmw_private *dev_priv,
  391. struct vmw_sw_context *sw_context,
  392. SVGA3dCmdHeader *header)
  393. {
  394. struct vmw_cid_cmd {
  395. SVGA3dCmdHeader header;
  396. __le32 cid;
  397. } *cmd;
  398. cmd = container_of(header, struct vmw_cid_cmd, header);
  399. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  400. user_context_converter, &cmd->cid, NULL);
  401. }
  402. static int vmw_cmd_set_render_target_check(struct vmw_private *dev_priv,
  403. struct vmw_sw_context *sw_context,
  404. SVGA3dCmdHeader *header)
  405. {
  406. struct vmw_sid_cmd {
  407. SVGA3dCmdHeader header;
  408. SVGA3dCmdSetRenderTarget body;
  409. } *cmd;
  410. int ret;
  411. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  412. if (unlikely(ret != 0))
  413. return ret;
  414. cmd = container_of(header, struct vmw_sid_cmd, header);
  415. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  416. user_surface_converter,
  417. &cmd->body.target.sid, NULL);
  418. return ret;
  419. }
  420. static int vmw_cmd_surface_copy_check(struct vmw_private *dev_priv,
  421. struct vmw_sw_context *sw_context,
  422. SVGA3dCmdHeader *header)
  423. {
  424. struct vmw_sid_cmd {
  425. SVGA3dCmdHeader header;
  426. SVGA3dCmdSurfaceCopy body;
  427. } *cmd;
  428. int ret;
  429. cmd = container_of(header, struct vmw_sid_cmd, header);
  430. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  431. user_surface_converter,
  432. &cmd->body.src.sid, NULL);
  433. if (unlikely(ret != 0))
  434. return ret;
  435. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  436. user_surface_converter,
  437. &cmd->body.dest.sid, NULL);
  438. }
  439. static int vmw_cmd_stretch_blt_check(struct vmw_private *dev_priv,
  440. struct vmw_sw_context *sw_context,
  441. SVGA3dCmdHeader *header)
  442. {
  443. struct vmw_sid_cmd {
  444. SVGA3dCmdHeader header;
  445. SVGA3dCmdSurfaceStretchBlt body;
  446. } *cmd;
  447. int ret;
  448. cmd = container_of(header, struct vmw_sid_cmd, header);
  449. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  450. user_surface_converter,
  451. &cmd->body.src.sid, NULL);
  452. if (unlikely(ret != 0))
  453. return ret;
  454. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  455. user_surface_converter,
  456. &cmd->body.dest.sid, NULL);
  457. }
  458. static int vmw_cmd_blt_surf_screen_check(struct vmw_private *dev_priv,
  459. struct vmw_sw_context *sw_context,
  460. SVGA3dCmdHeader *header)
  461. {
  462. struct vmw_sid_cmd {
  463. SVGA3dCmdHeader header;
  464. SVGA3dCmdBlitSurfaceToScreen body;
  465. } *cmd;
  466. cmd = container_of(header, struct vmw_sid_cmd, header);
  467. if (unlikely(!sw_context->kernel)) {
  468. DRM_ERROR("Kernel only SVGA3d command: %u.\n", cmd->header.id);
  469. return -EPERM;
  470. }
  471. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  472. user_surface_converter,
  473. &cmd->body.srcImage.sid, NULL);
  474. }
  475. static int vmw_cmd_present_check(struct vmw_private *dev_priv,
  476. struct vmw_sw_context *sw_context,
  477. SVGA3dCmdHeader *header)
  478. {
  479. struct vmw_sid_cmd {
  480. SVGA3dCmdHeader header;
  481. SVGA3dCmdPresent body;
  482. } *cmd;
  483. cmd = container_of(header, struct vmw_sid_cmd, header);
  484. if (unlikely(!sw_context->kernel)) {
  485. DRM_ERROR("Kernel only SVGA3d command: %u.\n", cmd->header.id);
  486. return -EPERM;
  487. }
  488. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  489. user_surface_converter, &cmd->body.sid,
  490. NULL);
  491. }
  492. /**
  493. * vmw_query_bo_switch_prepare - Prepare to switch pinned buffer for queries.
  494. *
  495. * @dev_priv: The device private structure.
  496. * @new_query_bo: The new buffer holding query results.
  497. * @sw_context: The software context used for this command submission.
  498. *
  499. * This function checks whether @new_query_bo is suitable for holding
  500. * query results, and if another buffer currently is pinned for query
  501. * results. If so, the function prepares the state of @sw_context for
  502. * switching pinned buffers after successful submission of the current
  503. * command batch.
  504. */
  505. static int vmw_query_bo_switch_prepare(struct vmw_private *dev_priv,
  506. struct ttm_buffer_object *new_query_bo,
  507. struct vmw_sw_context *sw_context)
  508. {
  509. struct vmw_res_cache_entry *ctx_entry =
  510. &sw_context->res_cache[vmw_res_context];
  511. int ret;
  512. BUG_ON(!ctx_entry->valid);
  513. sw_context->last_query_ctx = ctx_entry->res;
  514. if (unlikely(new_query_bo != sw_context->cur_query_bo)) {
  515. if (unlikely(new_query_bo->num_pages > 4)) {
  516. DRM_ERROR("Query buffer too large.\n");
  517. return -EINVAL;
  518. }
  519. if (unlikely(sw_context->cur_query_bo != NULL)) {
  520. sw_context->needs_post_query_barrier = true;
  521. ret = vmw_bo_to_validate_list(sw_context,
  522. sw_context->cur_query_bo,
  523. dev_priv->has_mob, NULL);
  524. if (unlikely(ret != 0))
  525. return ret;
  526. }
  527. sw_context->cur_query_bo = new_query_bo;
  528. ret = vmw_bo_to_validate_list(sw_context,
  529. dev_priv->dummy_query_bo,
  530. dev_priv->has_mob, NULL);
  531. if (unlikely(ret != 0))
  532. return ret;
  533. }
  534. return 0;
  535. }
  536. /**
  537. * vmw_query_bo_switch_commit - Finalize switching pinned query buffer
  538. *
  539. * @dev_priv: The device private structure.
  540. * @sw_context: The software context used for this command submission batch.
  541. *
  542. * This function will check if we're switching query buffers, and will then,
  543. * issue a dummy occlusion query wait used as a query barrier. When the fence
  544. * object following that query wait has signaled, we are sure that all
  545. * preceding queries have finished, and the old query buffer can be unpinned.
  546. * However, since both the new query buffer and the old one are fenced with
  547. * that fence, we can do an asynchronus unpin now, and be sure that the
  548. * old query buffer won't be moved until the fence has signaled.
  549. *
  550. * As mentioned above, both the new - and old query buffers need to be fenced
  551. * using a sequence emitted *after* calling this function.
  552. */
  553. static void vmw_query_bo_switch_commit(struct vmw_private *dev_priv,
  554. struct vmw_sw_context *sw_context)
  555. {
  556. /*
  557. * The validate list should still hold references to all
  558. * contexts here.
  559. */
  560. if (sw_context->needs_post_query_barrier) {
  561. struct vmw_res_cache_entry *ctx_entry =
  562. &sw_context->res_cache[vmw_res_context];
  563. struct vmw_resource *ctx;
  564. int ret;
  565. BUG_ON(!ctx_entry->valid);
  566. ctx = ctx_entry->res;
  567. ret = vmw_fifo_emit_dummy_query(dev_priv, ctx->id);
  568. if (unlikely(ret != 0))
  569. DRM_ERROR("Out of fifo space for dummy query.\n");
  570. }
  571. if (dev_priv->pinned_bo != sw_context->cur_query_bo) {
  572. if (dev_priv->pinned_bo) {
  573. vmw_bo_pin(dev_priv->pinned_bo, false);
  574. ttm_bo_unref(&dev_priv->pinned_bo);
  575. }
  576. if (!sw_context->needs_post_query_barrier) {
  577. vmw_bo_pin(sw_context->cur_query_bo, true);
  578. /*
  579. * We pin also the dummy_query_bo buffer so that we
  580. * don't need to validate it when emitting
  581. * dummy queries in context destroy paths.
  582. */
  583. vmw_bo_pin(dev_priv->dummy_query_bo, true);
  584. dev_priv->dummy_query_bo_pinned = true;
  585. BUG_ON(sw_context->last_query_ctx == NULL);
  586. dev_priv->query_cid = sw_context->last_query_ctx->id;
  587. dev_priv->query_cid_valid = true;
  588. dev_priv->pinned_bo =
  589. ttm_bo_reference(sw_context->cur_query_bo);
  590. }
  591. }
  592. }
  593. /**
  594. * vmw_translate_mob_pointer - Prepare to translate a user-space buffer
  595. * handle to a MOB id.
  596. *
  597. * @dev_priv: Pointer to a device private structure.
  598. * @sw_context: The software context used for this command batch validation.
  599. * @id: Pointer to the user-space handle to be translated.
  600. * @vmw_bo_p: Points to a location that, on successful return will carry
  601. * a reference-counted pointer to the DMA buffer identified by the
  602. * user-space handle in @id.
  603. *
  604. * This function saves information needed to translate a user-space buffer
  605. * handle to a MOB id. The translation does not take place immediately, but
  606. * during a call to vmw_apply_relocations(). This function builds a relocation
  607. * list and a list of buffers to validate. The former needs to be freed using
  608. * either vmw_apply_relocations() or vmw_free_relocations(). The latter
  609. * needs to be freed using vmw_clear_validations.
  610. */
  611. static int vmw_translate_mob_ptr(struct vmw_private *dev_priv,
  612. struct vmw_sw_context *sw_context,
  613. SVGAMobId *id,
  614. struct vmw_dma_buffer **vmw_bo_p)
  615. {
  616. struct vmw_dma_buffer *vmw_bo = NULL;
  617. struct ttm_buffer_object *bo;
  618. uint32_t handle = *id;
  619. struct vmw_relocation *reloc;
  620. int ret;
  621. ret = vmw_user_dmabuf_lookup(sw_context->tfile, handle, &vmw_bo);
  622. if (unlikely(ret != 0)) {
  623. DRM_ERROR("Could not find or use MOB buffer.\n");
  624. return -EINVAL;
  625. }
  626. bo = &vmw_bo->base;
  627. if (unlikely(sw_context->cur_reloc >= VMWGFX_MAX_RELOCATIONS)) {
  628. DRM_ERROR("Max number relocations per submission"
  629. " exceeded\n");
  630. ret = -EINVAL;
  631. goto out_no_reloc;
  632. }
  633. reloc = &sw_context->relocs[sw_context->cur_reloc++];
  634. reloc->mob_loc = id;
  635. reloc->location = NULL;
  636. ret = vmw_bo_to_validate_list(sw_context, bo, true, &reloc->index);
  637. if (unlikely(ret != 0))
  638. goto out_no_reloc;
  639. *vmw_bo_p = vmw_bo;
  640. return 0;
  641. out_no_reloc:
  642. vmw_dmabuf_unreference(&vmw_bo);
  643. vmw_bo_p = NULL;
  644. return ret;
  645. }
  646. /**
  647. * vmw_translate_guest_pointer - Prepare to translate a user-space buffer
  648. * handle to a valid SVGAGuestPtr
  649. *
  650. * @dev_priv: Pointer to a device private structure.
  651. * @sw_context: The software context used for this command batch validation.
  652. * @ptr: Pointer to the user-space handle to be translated.
  653. * @vmw_bo_p: Points to a location that, on successful return will carry
  654. * a reference-counted pointer to the DMA buffer identified by the
  655. * user-space handle in @id.
  656. *
  657. * This function saves information needed to translate a user-space buffer
  658. * handle to a valid SVGAGuestPtr. The translation does not take place
  659. * immediately, but during a call to vmw_apply_relocations().
  660. * This function builds a relocation list and a list of buffers to validate.
  661. * The former needs to be freed using either vmw_apply_relocations() or
  662. * vmw_free_relocations(). The latter needs to be freed using
  663. * vmw_clear_validations.
  664. */
  665. static int vmw_translate_guest_ptr(struct vmw_private *dev_priv,
  666. struct vmw_sw_context *sw_context,
  667. SVGAGuestPtr *ptr,
  668. struct vmw_dma_buffer **vmw_bo_p)
  669. {
  670. struct vmw_dma_buffer *vmw_bo = NULL;
  671. struct ttm_buffer_object *bo;
  672. uint32_t handle = ptr->gmrId;
  673. struct vmw_relocation *reloc;
  674. int ret;
  675. ret = vmw_user_dmabuf_lookup(sw_context->tfile, handle, &vmw_bo);
  676. if (unlikely(ret != 0)) {
  677. DRM_ERROR("Could not find or use GMR region.\n");
  678. return -EINVAL;
  679. }
  680. bo = &vmw_bo->base;
  681. if (unlikely(sw_context->cur_reloc >= VMWGFX_MAX_RELOCATIONS)) {
  682. DRM_ERROR("Max number relocations per submission"
  683. " exceeded\n");
  684. ret = -EINVAL;
  685. goto out_no_reloc;
  686. }
  687. reloc = &sw_context->relocs[sw_context->cur_reloc++];
  688. reloc->location = ptr;
  689. ret = vmw_bo_to_validate_list(sw_context, bo, false, &reloc->index);
  690. if (unlikely(ret != 0))
  691. goto out_no_reloc;
  692. *vmw_bo_p = vmw_bo;
  693. return 0;
  694. out_no_reloc:
  695. vmw_dmabuf_unreference(&vmw_bo);
  696. vmw_bo_p = NULL;
  697. return ret;
  698. }
  699. /**
  700. * vmw_cmd_begin_gb_query - validate a SVGA_3D_CMD_BEGIN_GB_QUERY command.
  701. *
  702. * @dev_priv: Pointer to a device private struct.
  703. * @sw_context: The software context used for this command submission.
  704. * @header: Pointer to the command header in the command stream.
  705. */
  706. static int vmw_cmd_begin_gb_query(struct vmw_private *dev_priv,
  707. struct vmw_sw_context *sw_context,
  708. SVGA3dCmdHeader *header)
  709. {
  710. struct vmw_begin_gb_query_cmd {
  711. SVGA3dCmdHeader header;
  712. SVGA3dCmdBeginGBQuery q;
  713. } *cmd;
  714. cmd = container_of(header, struct vmw_begin_gb_query_cmd,
  715. header);
  716. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  717. user_context_converter, &cmd->q.cid,
  718. NULL);
  719. }
  720. /**
  721. * vmw_cmd_begin_query - validate a SVGA_3D_CMD_BEGIN_QUERY command.
  722. *
  723. * @dev_priv: Pointer to a device private struct.
  724. * @sw_context: The software context used for this command submission.
  725. * @header: Pointer to the command header in the command stream.
  726. */
  727. static int vmw_cmd_begin_query(struct vmw_private *dev_priv,
  728. struct vmw_sw_context *sw_context,
  729. SVGA3dCmdHeader *header)
  730. {
  731. struct vmw_begin_query_cmd {
  732. SVGA3dCmdHeader header;
  733. SVGA3dCmdBeginQuery q;
  734. } *cmd;
  735. cmd = container_of(header, struct vmw_begin_query_cmd,
  736. header);
  737. if (unlikely(dev_priv->has_mob)) {
  738. struct {
  739. SVGA3dCmdHeader header;
  740. SVGA3dCmdBeginGBQuery q;
  741. } gb_cmd;
  742. BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
  743. gb_cmd.header.id = SVGA_3D_CMD_BEGIN_GB_QUERY;
  744. gb_cmd.header.size = cmd->header.size;
  745. gb_cmd.q.cid = cmd->q.cid;
  746. gb_cmd.q.type = cmd->q.type;
  747. memcpy(cmd, &gb_cmd, sizeof(*cmd));
  748. return vmw_cmd_begin_gb_query(dev_priv, sw_context, header);
  749. }
  750. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  751. user_context_converter, &cmd->q.cid,
  752. NULL);
  753. }
  754. /**
  755. * vmw_cmd_end_gb_query - validate a SVGA_3D_CMD_END_GB_QUERY command.
  756. *
  757. * @dev_priv: Pointer to a device private struct.
  758. * @sw_context: The software context used for this command submission.
  759. * @header: Pointer to the command header in the command stream.
  760. */
  761. static int vmw_cmd_end_gb_query(struct vmw_private *dev_priv,
  762. struct vmw_sw_context *sw_context,
  763. SVGA3dCmdHeader *header)
  764. {
  765. struct vmw_dma_buffer *vmw_bo;
  766. struct vmw_query_cmd {
  767. SVGA3dCmdHeader header;
  768. SVGA3dCmdEndGBQuery q;
  769. } *cmd;
  770. int ret;
  771. cmd = container_of(header, struct vmw_query_cmd, header);
  772. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  773. if (unlikely(ret != 0))
  774. return ret;
  775. ret = vmw_translate_mob_ptr(dev_priv, sw_context,
  776. &cmd->q.mobid,
  777. &vmw_bo);
  778. if (unlikely(ret != 0))
  779. return ret;
  780. ret = vmw_query_bo_switch_prepare(dev_priv, &vmw_bo->base, sw_context);
  781. vmw_dmabuf_unreference(&vmw_bo);
  782. return ret;
  783. }
  784. /**
  785. * vmw_cmd_end_query - validate a SVGA_3D_CMD_END_QUERY command.
  786. *
  787. * @dev_priv: Pointer to a device private struct.
  788. * @sw_context: The software context used for this command submission.
  789. * @header: Pointer to the command header in the command stream.
  790. */
  791. static int vmw_cmd_end_query(struct vmw_private *dev_priv,
  792. struct vmw_sw_context *sw_context,
  793. SVGA3dCmdHeader *header)
  794. {
  795. struct vmw_dma_buffer *vmw_bo;
  796. struct vmw_query_cmd {
  797. SVGA3dCmdHeader header;
  798. SVGA3dCmdEndQuery q;
  799. } *cmd;
  800. int ret;
  801. cmd = container_of(header, struct vmw_query_cmd, header);
  802. if (dev_priv->has_mob) {
  803. struct {
  804. SVGA3dCmdHeader header;
  805. SVGA3dCmdEndGBQuery q;
  806. } gb_cmd;
  807. BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
  808. gb_cmd.header.id = SVGA_3D_CMD_END_GB_QUERY;
  809. gb_cmd.header.size = cmd->header.size;
  810. gb_cmd.q.cid = cmd->q.cid;
  811. gb_cmd.q.type = cmd->q.type;
  812. gb_cmd.q.mobid = cmd->q.guestResult.gmrId;
  813. gb_cmd.q.offset = cmd->q.guestResult.offset;
  814. memcpy(cmd, &gb_cmd, sizeof(*cmd));
  815. return vmw_cmd_end_gb_query(dev_priv, sw_context, header);
  816. }
  817. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  818. if (unlikely(ret != 0))
  819. return ret;
  820. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  821. &cmd->q.guestResult,
  822. &vmw_bo);
  823. if (unlikely(ret != 0))
  824. return ret;
  825. ret = vmw_query_bo_switch_prepare(dev_priv, &vmw_bo->base, sw_context);
  826. vmw_dmabuf_unreference(&vmw_bo);
  827. return ret;
  828. }
  829. /**
  830. * vmw_cmd_wait_gb_query - validate a SVGA_3D_CMD_WAIT_GB_QUERY command.
  831. *
  832. * @dev_priv: Pointer to a device private struct.
  833. * @sw_context: The software context used for this command submission.
  834. * @header: Pointer to the command header in the command stream.
  835. */
  836. static int vmw_cmd_wait_gb_query(struct vmw_private *dev_priv,
  837. struct vmw_sw_context *sw_context,
  838. SVGA3dCmdHeader *header)
  839. {
  840. struct vmw_dma_buffer *vmw_bo;
  841. struct vmw_query_cmd {
  842. SVGA3dCmdHeader header;
  843. SVGA3dCmdWaitForGBQuery q;
  844. } *cmd;
  845. int ret;
  846. cmd = container_of(header, struct vmw_query_cmd, header);
  847. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  848. if (unlikely(ret != 0))
  849. return ret;
  850. ret = vmw_translate_mob_ptr(dev_priv, sw_context,
  851. &cmd->q.mobid,
  852. &vmw_bo);
  853. if (unlikely(ret != 0))
  854. return ret;
  855. vmw_dmabuf_unreference(&vmw_bo);
  856. return 0;
  857. }
  858. /**
  859. * vmw_cmd_wait_query - validate a SVGA_3D_CMD_WAIT_QUERY command.
  860. *
  861. * @dev_priv: Pointer to a device private struct.
  862. * @sw_context: The software context used for this command submission.
  863. * @header: Pointer to the command header in the command stream.
  864. */
  865. static int vmw_cmd_wait_query(struct vmw_private *dev_priv,
  866. struct vmw_sw_context *sw_context,
  867. SVGA3dCmdHeader *header)
  868. {
  869. struct vmw_dma_buffer *vmw_bo;
  870. struct vmw_query_cmd {
  871. SVGA3dCmdHeader header;
  872. SVGA3dCmdWaitForQuery q;
  873. } *cmd;
  874. int ret;
  875. cmd = container_of(header, struct vmw_query_cmd, header);
  876. if (dev_priv->has_mob) {
  877. struct {
  878. SVGA3dCmdHeader header;
  879. SVGA3dCmdWaitForGBQuery q;
  880. } gb_cmd;
  881. BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
  882. gb_cmd.header.id = SVGA_3D_CMD_WAIT_FOR_GB_QUERY;
  883. gb_cmd.header.size = cmd->header.size;
  884. gb_cmd.q.cid = cmd->q.cid;
  885. gb_cmd.q.type = cmd->q.type;
  886. gb_cmd.q.mobid = cmd->q.guestResult.gmrId;
  887. gb_cmd.q.offset = cmd->q.guestResult.offset;
  888. memcpy(cmd, &gb_cmd, sizeof(*cmd));
  889. return vmw_cmd_wait_gb_query(dev_priv, sw_context, header);
  890. }
  891. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  892. if (unlikely(ret != 0))
  893. return ret;
  894. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  895. &cmd->q.guestResult,
  896. &vmw_bo);
  897. if (unlikely(ret != 0))
  898. return ret;
  899. vmw_dmabuf_unreference(&vmw_bo);
  900. return 0;
  901. }
  902. static int vmw_cmd_dma(struct vmw_private *dev_priv,
  903. struct vmw_sw_context *sw_context,
  904. SVGA3dCmdHeader *header)
  905. {
  906. struct vmw_dma_buffer *vmw_bo = NULL;
  907. struct vmw_surface *srf = NULL;
  908. struct vmw_dma_cmd {
  909. SVGA3dCmdHeader header;
  910. SVGA3dCmdSurfaceDMA dma;
  911. } *cmd;
  912. int ret;
  913. cmd = container_of(header, struct vmw_dma_cmd, header);
  914. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  915. &cmd->dma.guest.ptr,
  916. &vmw_bo);
  917. if (unlikely(ret != 0))
  918. return ret;
  919. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  920. user_surface_converter, &cmd->dma.host.sid,
  921. NULL);
  922. if (unlikely(ret != 0)) {
  923. if (unlikely(ret != -ERESTARTSYS))
  924. DRM_ERROR("could not find surface for DMA.\n");
  925. goto out_no_surface;
  926. }
  927. srf = vmw_res_to_srf(sw_context->res_cache[vmw_res_surface].res);
  928. vmw_kms_cursor_snoop(srf, sw_context->tfile, &vmw_bo->base, header);
  929. out_no_surface:
  930. vmw_dmabuf_unreference(&vmw_bo);
  931. return ret;
  932. }
  933. static int vmw_cmd_draw(struct vmw_private *dev_priv,
  934. struct vmw_sw_context *sw_context,
  935. SVGA3dCmdHeader *header)
  936. {
  937. struct vmw_draw_cmd {
  938. SVGA3dCmdHeader header;
  939. SVGA3dCmdDrawPrimitives body;
  940. } *cmd;
  941. SVGA3dVertexDecl *decl = (SVGA3dVertexDecl *)(
  942. (unsigned long)header + sizeof(*cmd));
  943. SVGA3dPrimitiveRange *range;
  944. uint32_t i;
  945. uint32_t maxnum;
  946. int ret;
  947. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  948. if (unlikely(ret != 0))
  949. return ret;
  950. cmd = container_of(header, struct vmw_draw_cmd, header);
  951. maxnum = (header->size - sizeof(cmd->body)) / sizeof(*decl);
  952. if (unlikely(cmd->body.numVertexDecls > maxnum)) {
  953. DRM_ERROR("Illegal number of vertex declarations.\n");
  954. return -EINVAL;
  955. }
  956. for (i = 0; i < cmd->body.numVertexDecls; ++i, ++decl) {
  957. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  958. user_surface_converter,
  959. &decl->array.surfaceId, NULL);
  960. if (unlikely(ret != 0))
  961. return ret;
  962. }
  963. maxnum = (header->size - sizeof(cmd->body) -
  964. cmd->body.numVertexDecls * sizeof(*decl)) / sizeof(*range);
  965. if (unlikely(cmd->body.numRanges > maxnum)) {
  966. DRM_ERROR("Illegal number of index ranges.\n");
  967. return -EINVAL;
  968. }
  969. range = (SVGA3dPrimitiveRange *) decl;
  970. for (i = 0; i < cmd->body.numRanges; ++i, ++range) {
  971. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  972. user_surface_converter,
  973. &range->indexArray.surfaceId, NULL);
  974. if (unlikely(ret != 0))
  975. return ret;
  976. }
  977. return 0;
  978. }
  979. static int vmw_cmd_tex_state(struct vmw_private *dev_priv,
  980. struct vmw_sw_context *sw_context,
  981. SVGA3dCmdHeader *header)
  982. {
  983. struct vmw_tex_state_cmd {
  984. SVGA3dCmdHeader header;
  985. SVGA3dCmdSetTextureState state;
  986. };
  987. SVGA3dTextureState *last_state = (SVGA3dTextureState *)
  988. ((unsigned long) header + header->size + sizeof(header));
  989. SVGA3dTextureState *cur_state = (SVGA3dTextureState *)
  990. ((unsigned long) header + sizeof(struct vmw_tex_state_cmd));
  991. int ret;
  992. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  993. if (unlikely(ret != 0))
  994. return ret;
  995. for (; cur_state < last_state; ++cur_state) {
  996. if (likely(cur_state->name != SVGA3D_TS_BIND_TEXTURE))
  997. continue;
  998. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  999. user_surface_converter,
  1000. &cur_state->value, NULL);
  1001. if (unlikely(ret != 0))
  1002. return ret;
  1003. }
  1004. return 0;
  1005. }
  1006. static int vmw_cmd_check_define_gmrfb(struct vmw_private *dev_priv,
  1007. struct vmw_sw_context *sw_context,
  1008. void *buf)
  1009. {
  1010. struct vmw_dma_buffer *vmw_bo;
  1011. int ret;
  1012. struct {
  1013. uint32_t header;
  1014. SVGAFifoCmdDefineGMRFB body;
  1015. } *cmd = buf;
  1016. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  1017. &cmd->body.ptr,
  1018. &vmw_bo);
  1019. if (unlikely(ret != 0))
  1020. return ret;
  1021. vmw_dmabuf_unreference(&vmw_bo);
  1022. return ret;
  1023. }
  1024. /**
  1025. * vmw_cmd_set_shader - Validate an SVGA_3D_CMD_SET_SHADER
  1026. * command
  1027. *
  1028. * @dev_priv: Pointer to a device private struct.
  1029. * @sw_context: The software context being used for this batch.
  1030. * @header: Pointer to the command header in the command stream.
  1031. */
  1032. static int vmw_cmd_set_shader(struct vmw_private *dev_priv,
  1033. struct vmw_sw_context *sw_context,
  1034. SVGA3dCmdHeader *header)
  1035. {
  1036. struct vmw_set_shader_cmd {
  1037. SVGA3dCmdHeader header;
  1038. SVGA3dCmdSetShader body;
  1039. } *cmd;
  1040. int ret;
  1041. cmd = container_of(header, struct vmw_set_shader_cmd,
  1042. header);
  1043. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  1044. if (unlikely(ret != 0))
  1045. return ret;
  1046. return 0;
  1047. }
  1048. static int vmw_cmd_check_not_3d(struct vmw_private *dev_priv,
  1049. struct vmw_sw_context *sw_context,
  1050. void *buf, uint32_t *size)
  1051. {
  1052. uint32_t size_remaining = *size;
  1053. uint32_t cmd_id;
  1054. cmd_id = le32_to_cpu(((uint32_t *)buf)[0]);
  1055. switch (cmd_id) {
  1056. case SVGA_CMD_UPDATE:
  1057. *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdUpdate);
  1058. break;
  1059. case SVGA_CMD_DEFINE_GMRFB:
  1060. *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdDefineGMRFB);
  1061. break;
  1062. case SVGA_CMD_BLIT_GMRFB_TO_SCREEN:
  1063. *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
  1064. break;
  1065. case SVGA_CMD_BLIT_SCREEN_TO_GMRFB:
  1066. *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
  1067. break;
  1068. default:
  1069. DRM_ERROR("Unsupported SVGA command: %u.\n", cmd_id);
  1070. return -EINVAL;
  1071. }
  1072. if (*size > size_remaining) {
  1073. DRM_ERROR("Invalid SVGA command (size mismatch):"
  1074. " %u.\n", cmd_id);
  1075. return -EINVAL;
  1076. }
  1077. if (unlikely(!sw_context->kernel)) {
  1078. DRM_ERROR("Kernel only SVGA command: %u.\n", cmd_id);
  1079. return -EPERM;
  1080. }
  1081. if (cmd_id == SVGA_CMD_DEFINE_GMRFB)
  1082. return vmw_cmd_check_define_gmrfb(dev_priv, sw_context, buf);
  1083. return 0;
  1084. }
  1085. typedef int (*vmw_cmd_func) (struct vmw_private *,
  1086. struct vmw_sw_context *,
  1087. SVGA3dCmdHeader *);
  1088. #define VMW_CMD_DEF(cmd, func) \
  1089. [cmd - SVGA_3D_CMD_BASE] = func
  1090. static vmw_cmd_func vmw_cmd_funcs[SVGA_3D_CMD_MAX] = {
  1091. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE, &vmw_cmd_invalid),
  1092. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DESTROY, &vmw_cmd_invalid),
  1093. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_COPY, &vmw_cmd_surface_copy_check),
  1094. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_STRETCHBLT, &vmw_cmd_stretch_blt_check),
  1095. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DMA, &vmw_cmd_dma),
  1096. VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DEFINE, &vmw_cmd_invalid),
  1097. VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DESTROY, &vmw_cmd_invalid),
  1098. VMW_CMD_DEF(SVGA_3D_CMD_SETTRANSFORM, &vmw_cmd_cid_check),
  1099. VMW_CMD_DEF(SVGA_3D_CMD_SETZRANGE, &vmw_cmd_cid_check),
  1100. VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERSTATE, &vmw_cmd_cid_check),
  1101. VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERTARGET,
  1102. &vmw_cmd_set_render_target_check),
  1103. VMW_CMD_DEF(SVGA_3D_CMD_SETTEXTURESTATE, &vmw_cmd_tex_state),
  1104. VMW_CMD_DEF(SVGA_3D_CMD_SETMATERIAL, &vmw_cmd_cid_check),
  1105. VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTDATA, &vmw_cmd_cid_check),
  1106. VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTENABLED, &vmw_cmd_cid_check),
  1107. VMW_CMD_DEF(SVGA_3D_CMD_SETVIEWPORT, &vmw_cmd_cid_check),
  1108. VMW_CMD_DEF(SVGA_3D_CMD_SETCLIPPLANE, &vmw_cmd_cid_check),
  1109. VMW_CMD_DEF(SVGA_3D_CMD_CLEAR, &vmw_cmd_cid_check),
  1110. VMW_CMD_DEF(SVGA_3D_CMD_PRESENT, &vmw_cmd_present_check),
  1111. VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DEFINE, &vmw_cmd_cid_check),
  1112. VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DESTROY, &vmw_cmd_cid_check),
  1113. VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER, &vmw_cmd_set_shader),
  1114. VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER_CONST, &vmw_cmd_cid_check),
  1115. VMW_CMD_DEF(SVGA_3D_CMD_DRAW_PRIMITIVES, &vmw_cmd_draw),
  1116. VMW_CMD_DEF(SVGA_3D_CMD_SETSCISSORRECT, &vmw_cmd_cid_check),
  1117. VMW_CMD_DEF(SVGA_3D_CMD_BEGIN_QUERY, &vmw_cmd_begin_query),
  1118. VMW_CMD_DEF(SVGA_3D_CMD_END_QUERY, &vmw_cmd_end_query),
  1119. VMW_CMD_DEF(SVGA_3D_CMD_WAIT_FOR_QUERY, &vmw_cmd_wait_query),
  1120. VMW_CMD_DEF(SVGA_3D_CMD_PRESENT_READBACK, &vmw_cmd_ok),
  1121. VMW_CMD_DEF(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN,
  1122. &vmw_cmd_blt_surf_screen_check),
  1123. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE_V2, &vmw_cmd_invalid),
  1124. VMW_CMD_DEF(SVGA_3D_CMD_GENERATE_MIPMAPS, &vmw_cmd_invalid),
  1125. VMW_CMD_DEF(SVGA_3D_CMD_ACTIVATE_SURFACE, &vmw_cmd_invalid),
  1126. VMW_CMD_DEF(SVGA_3D_CMD_DEACTIVATE_SURFACE, &vmw_cmd_invalid),
  1127. VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_CONTEXT, &vmw_cmd_invalid),
  1128. VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_CONTEXT, &vmw_cmd_invalid),
  1129. VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_CONTEXT, &vmw_cmd_invalid),
  1130. VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_CONTEXT, &vmw_cmd_invalid),
  1131. VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_CONTEXT, &vmw_cmd_invalid),
  1132. VMW_CMD_DEF(SVGA_3D_CMD_BEGIN_GB_QUERY, &vmw_cmd_begin_gb_query),
  1133. VMW_CMD_DEF(SVGA_3D_CMD_END_GB_QUERY, &vmw_cmd_end_gb_query),
  1134. VMW_CMD_DEF(SVGA_3D_CMD_WAIT_FOR_GB_QUERY, &vmw_cmd_wait_gb_query),
  1135. };
  1136. static int vmw_cmd_check(struct vmw_private *dev_priv,
  1137. struct vmw_sw_context *sw_context,
  1138. void *buf, uint32_t *size)
  1139. {
  1140. uint32_t cmd_id;
  1141. uint32_t size_remaining = *size;
  1142. SVGA3dCmdHeader *header = (SVGA3dCmdHeader *) buf;
  1143. int ret;
  1144. cmd_id = le32_to_cpu(((uint32_t *)buf)[0]);
  1145. /* Handle any none 3D commands */
  1146. if (unlikely(cmd_id < SVGA_CMD_MAX))
  1147. return vmw_cmd_check_not_3d(dev_priv, sw_context, buf, size);
  1148. cmd_id = le32_to_cpu(header->id);
  1149. *size = le32_to_cpu(header->size) + sizeof(SVGA3dCmdHeader);
  1150. cmd_id -= SVGA_3D_CMD_BASE;
  1151. if (unlikely(*size > size_remaining))
  1152. goto out_err;
  1153. if (unlikely(cmd_id >= SVGA_3D_CMD_MAX - SVGA_3D_CMD_BASE))
  1154. goto out_err;
  1155. ret = vmw_cmd_funcs[cmd_id](dev_priv, sw_context, header);
  1156. if (unlikely(ret != 0))
  1157. goto out_err;
  1158. return 0;
  1159. out_err:
  1160. DRM_ERROR("Illegal / Invalid SVGA3D command: %d\n",
  1161. cmd_id + SVGA_3D_CMD_BASE);
  1162. return -EINVAL;
  1163. }
  1164. static int vmw_cmd_check_all(struct vmw_private *dev_priv,
  1165. struct vmw_sw_context *sw_context,
  1166. void *buf,
  1167. uint32_t size)
  1168. {
  1169. int32_t cur_size = size;
  1170. int ret;
  1171. sw_context->buf_start = buf;
  1172. while (cur_size > 0) {
  1173. size = cur_size;
  1174. ret = vmw_cmd_check(dev_priv, sw_context, buf, &size);
  1175. if (unlikely(ret != 0))
  1176. return ret;
  1177. buf = (void *)((unsigned long) buf + size);
  1178. cur_size -= size;
  1179. }
  1180. if (unlikely(cur_size != 0)) {
  1181. DRM_ERROR("Command verifier out of sync.\n");
  1182. return -EINVAL;
  1183. }
  1184. return 0;
  1185. }
  1186. static void vmw_free_relocations(struct vmw_sw_context *sw_context)
  1187. {
  1188. sw_context->cur_reloc = 0;
  1189. }
  1190. static void vmw_apply_relocations(struct vmw_sw_context *sw_context)
  1191. {
  1192. uint32_t i;
  1193. struct vmw_relocation *reloc;
  1194. struct ttm_validate_buffer *validate;
  1195. struct ttm_buffer_object *bo;
  1196. for (i = 0; i < sw_context->cur_reloc; ++i) {
  1197. reloc = &sw_context->relocs[i];
  1198. validate = &sw_context->val_bufs[reloc->index].base;
  1199. bo = validate->bo;
  1200. switch (bo->mem.mem_type) {
  1201. case TTM_PL_VRAM:
  1202. reloc->location->offset += bo->offset;
  1203. reloc->location->gmrId = SVGA_GMR_FRAMEBUFFER;
  1204. break;
  1205. case VMW_PL_GMR:
  1206. reloc->location->gmrId = bo->mem.start;
  1207. break;
  1208. case VMW_PL_MOB:
  1209. *reloc->mob_loc = bo->mem.start;
  1210. break;
  1211. default:
  1212. BUG();
  1213. }
  1214. }
  1215. vmw_free_relocations(sw_context);
  1216. }
  1217. /**
  1218. * vmw_resource_list_unrefererence - Free up a resource list and unreference
  1219. * all resources referenced by it.
  1220. *
  1221. * @list: The resource list.
  1222. */
  1223. static void vmw_resource_list_unreference(struct list_head *list)
  1224. {
  1225. struct vmw_resource_val_node *val, *val_next;
  1226. /*
  1227. * Drop references to resources held during command submission.
  1228. */
  1229. list_for_each_entry_safe(val, val_next, list, head) {
  1230. list_del_init(&val->head);
  1231. vmw_resource_unreference(&val->res);
  1232. kfree(val);
  1233. }
  1234. }
  1235. static void vmw_clear_validations(struct vmw_sw_context *sw_context)
  1236. {
  1237. struct vmw_validate_buffer *entry, *next;
  1238. struct vmw_resource_val_node *val;
  1239. /*
  1240. * Drop references to DMA buffers held during command submission.
  1241. */
  1242. list_for_each_entry_safe(entry, next, &sw_context->validate_nodes,
  1243. base.head) {
  1244. list_del(&entry->base.head);
  1245. ttm_bo_unref(&entry->base.bo);
  1246. (void) drm_ht_remove_item(&sw_context->res_ht, &entry->hash);
  1247. sw_context->cur_val_buf--;
  1248. }
  1249. BUG_ON(sw_context->cur_val_buf != 0);
  1250. list_for_each_entry(val, &sw_context->resource_list, head)
  1251. (void) drm_ht_remove_item(&sw_context->res_ht, &val->hash);
  1252. }
  1253. static int vmw_validate_single_buffer(struct vmw_private *dev_priv,
  1254. struct ttm_buffer_object *bo,
  1255. bool validate_as_mob)
  1256. {
  1257. int ret;
  1258. /*
  1259. * Don't validate pinned buffers.
  1260. */
  1261. if (bo == dev_priv->pinned_bo ||
  1262. (bo == dev_priv->dummy_query_bo &&
  1263. dev_priv->dummy_query_bo_pinned))
  1264. return 0;
  1265. if (validate_as_mob)
  1266. return ttm_bo_validate(bo, &vmw_mob_placement, true, false);
  1267. /**
  1268. * Put BO in VRAM if there is space, otherwise as a GMR.
  1269. * If there is no space in VRAM and GMR ids are all used up,
  1270. * start evicting GMRs to make room. If the DMA buffer can't be
  1271. * used as a GMR, this will return -ENOMEM.
  1272. */
  1273. ret = ttm_bo_validate(bo, &vmw_vram_gmr_placement, true, false);
  1274. if (likely(ret == 0 || ret == -ERESTARTSYS))
  1275. return ret;
  1276. /**
  1277. * If that failed, try VRAM again, this time evicting
  1278. * previous contents.
  1279. */
  1280. DRM_INFO("Falling through to VRAM.\n");
  1281. ret = ttm_bo_validate(bo, &vmw_vram_placement, true, false);
  1282. return ret;
  1283. }
  1284. static int vmw_validate_buffers(struct vmw_private *dev_priv,
  1285. struct vmw_sw_context *sw_context)
  1286. {
  1287. struct vmw_validate_buffer *entry;
  1288. int ret;
  1289. list_for_each_entry(entry, &sw_context->validate_nodes, base.head) {
  1290. ret = vmw_validate_single_buffer(dev_priv, entry->base.bo,
  1291. entry->validate_as_mob);
  1292. if (unlikely(ret != 0))
  1293. return ret;
  1294. }
  1295. return 0;
  1296. }
  1297. static int vmw_resize_cmd_bounce(struct vmw_sw_context *sw_context,
  1298. uint32_t size)
  1299. {
  1300. if (likely(sw_context->cmd_bounce_size >= size))
  1301. return 0;
  1302. if (sw_context->cmd_bounce_size == 0)
  1303. sw_context->cmd_bounce_size = VMWGFX_CMD_BOUNCE_INIT_SIZE;
  1304. while (sw_context->cmd_bounce_size < size) {
  1305. sw_context->cmd_bounce_size =
  1306. PAGE_ALIGN(sw_context->cmd_bounce_size +
  1307. (sw_context->cmd_bounce_size >> 1));
  1308. }
  1309. if (sw_context->cmd_bounce != NULL)
  1310. vfree(sw_context->cmd_bounce);
  1311. sw_context->cmd_bounce = vmalloc(sw_context->cmd_bounce_size);
  1312. if (sw_context->cmd_bounce == NULL) {
  1313. DRM_ERROR("Failed to allocate command bounce buffer.\n");
  1314. sw_context->cmd_bounce_size = 0;
  1315. return -ENOMEM;
  1316. }
  1317. return 0;
  1318. }
  1319. /**
  1320. * vmw_execbuf_fence_commands - create and submit a command stream fence
  1321. *
  1322. * Creates a fence object and submits a command stream marker.
  1323. * If this fails for some reason, We sync the fifo and return NULL.
  1324. * It is then safe to fence buffers with a NULL pointer.
  1325. *
  1326. * If @p_handle is not NULL @file_priv must also not be NULL. Creates
  1327. * a userspace handle if @p_handle is not NULL, otherwise not.
  1328. */
  1329. int vmw_execbuf_fence_commands(struct drm_file *file_priv,
  1330. struct vmw_private *dev_priv,
  1331. struct vmw_fence_obj **p_fence,
  1332. uint32_t *p_handle)
  1333. {
  1334. uint32_t sequence;
  1335. int ret;
  1336. bool synced = false;
  1337. /* p_handle implies file_priv. */
  1338. BUG_ON(p_handle != NULL && file_priv == NULL);
  1339. ret = vmw_fifo_send_fence(dev_priv, &sequence);
  1340. if (unlikely(ret != 0)) {
  1341. DRM_ERROR("Fence submission error. Syncing.\n");
  1342. synced = true;
  1343. }
  1344. if (p_handle != NULL)
  1345. ret = vmw_user_fence_create(file_priv, dev_priv->fman,
  1346. sequence,
  1347. DRM_VMW_FENCE_FLAG_EXEC,
  1348. p_fence, p_handle);
  1349. else
  1350. ret = vmw_fence_create(dev_priv->fman, sequence,
  1351. DRM_VMW_FENCE_FLAG_EXEC,
  1352. p_fence);
  1353. if (unlikely(ret != 0 && !synced)) {
  1354. (void) vmw_fallback_wait(dev_priv, false, false,
  1355. sequence, false,
  1356. VMW_FENCE_WAIT_TIMEOUT);
  1357. *p_fence = NULL;
  1358. }
  1359. return 0;
  1360. }
  1361. /**
  1362. * vmw_execbuf_copy_fence_user - copy fence object information to
  1363. * user-space.
  1364. *
  1365. * @dev_priv: Pointer to a vmw_private struct.
  1366. * @vmw_fp: Pointer to the struct vmw_fpriv representing the calling file.
  1367. * @ret: Return value from fence object creation.
  1368. * @user_fence_rep: User space address of a struct drm_vmw_fence_rep to
  1369. * which the information should be copied.
  1370. * @fence: Pointer to the fenc object.
  1371. * @fence_handle: User-space fence handle.
  1372. *
  1373. * This function copies fence information to user-space. If copying fails,
  1374. * The user-space struct drm_vmw_fence_rep::error member is hopefully
  1375. * left untouched, and if it's preloaded with an -EFAULT by user-space,
  1376. * the error will hopefully be detected.
  1377. * Also if copying fails, user-space will be unable to signal the fence
  1378. * object so we wait for it immediately, and then unreference the
  1379. * user-space reference.
  1380. */
  1381. void
  1382. vmw_execbuf_copy_fence_user(struct vmw_private *dev_priv,
  1383. struct vmw_fpriv *vmw_fp,
  1384. int ret,
  1385. struct drm_vmw_fence_rep __user *user_fence_rep,
  1386. struct vmw_fence_obj *fence,
  1387. uint32_t fence_handle)
  1388. {
  1389. struct drm_vmw_fence_rep fence_rep;
  1390. if (user_fence_rep == NULL)
  1391. return;
  1392. memset(&fence_rep, 0, sizeof(fence_rep));
  1393. fence_rep.error = ret;
  1394. if (ret == 0) {
  1395. BUG_ON(fence == NULL);
  1396. fence_rep.handle = fence_handle;
  1397. fence_rep.seqno = fence->seqno;
  1398. vmw_update_seqno(dev_priv, &dev_priv->fifo);
  1399. fence_rep.passed_seqno = dev_priv->last_read_seqno;
  1400. }
  1401. /*
  1402. * copy_to_user errors will be detected by user space not
  1403. * seeing fence_rep::error filled in. Typically
  1404. * user-space would have pre-set that member to -EFAULT.
  1405. */
  1406. ret = copy_to_user(user_fence_rep, &fence_rep,
  1407. sizeof(fence_rep));
  1408. /*
  1409. * User-space lost the fence object. We need to sync
  1410. * and unreference the handle.
  1411. */
  1412. if (unlikely(ret != 0) && (fence_rep.error == 0)) {
  1413. ttm_ref_object_base_unref(vmw_fp->tfile,
  1414. fence_handle, TTM_REF_USAGE);
  1415. DRM_ERROR("Fence copy error. Syncing.\n");
  1416. (void) vmw_fence_obj_wait(fence, fence->signal_mask,
  1417. false, false,
  1418. VMW_FENCE_WAIT_TIMEOUT);
  1419. }
  1420. }
  1421. int vmw_execbuf_process(struct drm_file *file_priv,
  1422. struct vmw_private *dev_priv,
  1423. void __user *user_commands,
  1424. void *kernel_commands,
  1425. uint32_t command_size,
  1426. uint64_t throttle_us,
  1427. struct drm_vmw_fence_rep __user *user_fence_rep,
  1428. struct vmw_fence_obj **out_fence)
  1429. {
  1430. struct vmw_sw_context *sw_context = &dev_priv->ctx;
  1431. struct vmw_fence_obj *fence = NULL;
  1432. struct vmw_resource *error_resource;
  1433. struct list_head resource_list;
  1434. struct ww_acquire_ctx ticket;
  1435. uint32_t handle;
  1436. void *cmd;
  1437. int ret;
  1438. ret = mutex_lock_interruptible(&dev_priv->cmdbuf_mutex);
  1439. if (unlikely(ret != 0))
  1440. return -ERESTARTSYS;
  1441. if (kernel_commands == NULL) {
  1442. sw_context->kernel = false;
  1443. ret = vmw_resize_cmd_bounce(sw_context, command_size);
  1444. if (unlikely(ret != 0))
  1445. goto out_unlock;
  1446. ret = copy_from_user(sw_context->cmd_bounce,
  1447. user_commands, command_size);
  1448. if (unlikely(ret != 0)) {
  1449. ret = -EFAULT;
  1450. DRM_ERROR("Failed copying commands.\n");
  1451. goto out_unlock;
  1452. }
  1453. kernel_commands = sw_context->cmd_bounce;
  1454. } else
  1455. sw_context->kernel = true;
  1456. sw_context->tfile = vmw_fpriv(file_priv)->tfile;
  1457. sw_context->cur_reloc = 0;
  1458. sw_context->cur_val_buf = 0;
  1459. sw_context->fence_flags = 0;
  1460. INIT_LIST_HEAD(&sw_context->resource_list);
  1461. sw_context->cur_query_bo = dev_priv->pinned_bo;
  1462. sw_context->last_query_ctx = NULL;
  1463. sw_context->needs_post_query_barrier = false;
  1464. memset(sw_context->res_cache, 0, sizeof(sw_context->res_cache));
  1465. INIT_LIST_HEAD(&sw_context->validate_nodes);
  1466. INIT_LIST_HEAD(&sw_context->res_relocations);
  1467. if (!sw_context->res_ht_initialized) {
  1468. ret = drm_ht_create(&sw_context->res_ht, VMW_RES_HT_ORDER);
  1469. if (unlikely(ret != 0))
  1470. goto out_unlock;
  1471. sw_context->res_ht_initialized = true;
  1472. }
  1473. INIT_LIST_HEAD(&resource_list);
  1474. ret = vmw_cmd_check_all(dev_priv, sw_context, kernel_commands,
  1475. command_size);
  1476. if (unlikely(ret != 0))
  1477. goto out_err;
  1478. ret = vmw_resources_reserve(sw_context);
  1479. if (unlikely(ret != 0))
  1480. goto out_err;
  1481. ret = ttm_eu_reserve_buffers(&ticket, &sw_context->validate_nodes);
  1482. if (unlikely(ret != 0))
  1483. goto out_err;
  1484. ret = vmw_validate_buffers(dev_priv, sw_context);
  1485. if (unlikely(ret != 0))
  1486. goto out_err;
  1487. ret = vmw_resources_validate(sw_context);
  1488. if (unlikely(ret != 0))
  1489. goto out_err;
  1490. if (throttle_us) {
  1491. ret = vmw_wait_lag(dev_priv, &dev_priv->fifo.marker_queue,
  1492. throttle_us);
  1493. if (unlikely(ret != 0))
  1494. goto out_err;
  1495. }
  1496. cmd = vmw_fifo_reserve(dev_priv, command_size);
  1497. if (unlikely(cmd == NULL)) {
  1498. DRM_ERROR("Failed reserving fifo space for commands.\n");
  1499. ret = -ENOMEM;
  1500. goto out_err;
  1501. }
  1502. vmw_apply_relocations(sw_context);
  1503. memcpy(cmd, kernel_commands, command_size);
  1504. vmw_resource_relocations_apply(cmd, &sw_context->res_relocations);
  1505. vmw_resource_relocations_free(&sw_context->res_relocations);
  1506. vmw_fifo_commit(dev_priv, command_size);
  1507. vmw_query_bo_switch_commit(dev_priv, sw_context);
  1508. ret = vmw_execbuf_fence_commands(file_priv, dev_priv,
  1509. &fence,
  1510. (user_fence_rep) ? &handle : NULL);
  1511. /*
  1512. * This error is harmless, because if fence submission fails,
  1513. * vmw_fifo_send_fence will sync. The error will be propagated to
  1514. * user-space in @fence_rep
  1515. */
  1516. if (ret != 0)
  1517. DRM_ERROR("Fence submission error. Syncing.\n");
  1518. vmw_resource_list_unreserve(&sw_context->resource_list, false);
  1519. ttm_eu_fence_buffer_objects(&ticket, &sw_context->validate_nodes,
  1520. (void *) fence);
  1521. if (unlikely(dev_priv->pinned_bo != NULL &&
  1522. !dev_priv->query_cid_valid))
  1523. __vmw_execbuf_release_pinned_bo(dev_priv, fence);
  1524. vmw_clear_validations(sw_context);
  1525. vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv), ret,
  1526. user_fence_rep, fence, handle);
  1527. /* Don't unreference when handing fence out */
  1528. if (unlikely(out_fence != NULL)) {
  1529. *out_fence = fence;
  1530. fence = NULL;
  1531. } else if (likely(fence != NULL)) {
  1532. vmw_fence_obj_unreference(&fence);
  1533. }
  1534. list_splice_init(&sw_context->resource_list, &resource_list);
  1535. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1536. /*
  1537. * Unreference resources outside of the cmdbuf_mutex to
  1538. * avoid deadlocks in resource destruction paths.
  1539. */
  1540. vmw_resource_list_unreference(&resource_list);
  1541. return 0;
  1542. out_err:
  1543. vmw_resource_relocations_free(&sw_context->res_relocations);
  1544. vmw_free_relocations(sw_context);
  1545. ttm_eu_backoff_reservation(&ticket, &sw_context->validate_nodes);
  1546. vmw_resource_list_unreserve(&sw_context->resource_list, true);
  1547. vmw_clear_validations(sw_context);
  1548. if (unlikely(dev_priv->pinned_bo != NULL &&
  1549. !dev_priv->query_cid_valid))
  1550. __vmw_execbuf_release_pinned_bo(dev_priv, NULL);
  1551. out_unlock:
  1552. list_splice_init(&sw_context->resource_list, &resource_list);
  1553. error_resource = sw_context->error_resource;
  1554. sw_context->error_resource = NULL;
  1555. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1556. /*
  1557. * Unreference resources outside of the cmdbuf_mutex to
  1558. * avoid deadlocks in resource destruction paths.
  1559. */
  1560. vmw_resource_list_unreference(&resource_list);
  1561. if (unlikely(error_resource != NULL))
  1562. vmw_resource_unreference(&error_resource);
  1563. return ret;
  1564. }
  1565. /**
  1566. * vmw_execbuf_unpin_panic - Idle the fifo and unpin the query buffer.
  1567. *
  1568. * @dev_priv: The device private structure.
  1569. *
  1570. * This function is called to idle the fifo and unpin the query buffer
  1571. * if the normal way to do this hits an error, which should typically be
  1572. * extremely rare.
  1573. */
  1574. static void vmw_execbuf_unpin_panic(struct vmw_private *dev_priv)
  1575. {
  1576. DRM_ERROR("Can't unpin query buffer. Trying to recover.\n");
  1577. (void) vmw_fallback_wait(dev_priv, false, true, 0, false, 10*HZ);
  1578. vmw_bo_pin(dev_priv->pinned_bo, false);
  1579. vmw_bo_pin(dev_priv->dummy_query_bo, false);
  1580. dev_priv->dummy_query_bo_pinned = false;
  1581. }
  1582. /**
  1583. * __vmw_execbuf_release_pinned_bo - Flush queries and unpin the pinned
  1584. * query bo.
  1585. *
  1586. * @dev_priv: The device private structure.
  1587. * @fence: If non-NULL should point to a struct vmw_fence_obj issued
  1588. * _after_ a query barrier that flushes all queries touching the current
  1589. * buffer pointed to by @dev_priv->pinned_bo
  1590. *
  1591. * This function should be used to unpin the pinned query bo, or
  1592. * as a query barrier when we need to make sure that all queries have
  1593. * finished before the next fifo command. (For example on hardware
  1594. * context destructions where the hardware may otherwise leak unfinished
  1595. * queries).
  1596. *
  1597. * This function does not return any failure codes, but make attempts
  1598. * to do safe unpinning in case of errors.
  1599. *
  1600. * The function will synchronize on the previous query barrier, and will
  1601. * thus not finish until that barrier has executed.
  1602. *
  1603. * the @dev_priv->cmdbuf_mutex needs to be held by the current thread
  1604. * before calling this function.
  1605. */
  1606. void __vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv,
  1607. struct vmw_fence_obj *fence)
  1608. {
  1609. int ret = 0;
  1610. struct list_head validate_list;
  1611. struct ttm_validate_buffer pinned_val, query_val;
  1612. struct vmw_fence_obj *lfence = NULL;
  1613. struct ww_acquire_ctx ticket;
  1614. if (dev_priv->pinned_bo == NULL)
  1615. goto out_unlock;
  1616. INIT_LIST_HEAD(&validate_list);
  1617. pinned_val.bo = ttm_bo_reference(dev_priv->pinned_bo);
  1618. list_add_tail(&pinned_val.head, &validate_list);
  1619. query_val.bo = ttm_bo_reference(dev_priv->dummy_query_bo);
  1620. list_add_tail(&query_val.head, &validate_list);
  1621. do {
  1622. ret = ttm_eu_reserve_buffers(&ticket, &validate_list);
  1623. } while (ret == -ERESTARTSYS);
  1624. if (unlikely(ret != 0)) {
  1625. vmw_execbuf_unpin_panic(dev_priv);
  1626. goto out_no_reserve;
  1627. }
  1628. if (dev_priv->query_cid_valid) {
  1629. BUG_ON(fence != NULL);
  1630. ret = vmw_fifo_emit_dummy_query(dev_priv, dev_priv->query_cid);
  1631. if (unlikely(ret != 0)) {
  1632. vmw_execbuf_unpin_panic(dev_priv);
  1633. goto out_no_emit;
  1634. }
  1635. dev_priv->query_cid_valid = false;
  1636. }
  1637. vmw_bo_pin(dev_priv->pinned_bo, false);
  1638. vmw_bo_pin(dev_priv->dummy_query_bo, false);
  1639. dev_priv->dummy_query_bo_pinned = false;
  1640. if (fence == NULL) {
  1641. (void) vmw_execbuf_fence_commands(NULL, dev_priv, &lfence,
  1642. NULL);
  1643. fence = lfence;
  1644. }
  1645. ttm_eu_fence_buffer_objects(&ticket, &validate_list, (void *) fence);
  1646. if (lfence != NULL)
  1647. vmw_fence_obj_unreference(&lfence);
  1648. ttm_bo_unref(&query_val.bo);
  1649. ttm_bo_unref(&pinned_val.bo);
  1650. ttm_bo_unref(&dev_priv->pinned_bo);
  1651. out_unlock:
  1652. return;
  1653. out_no_emit:
  1654. ttm_eu_backoff_reservation(&ticket, &validate_list);
  1655. out_no_reserve:
  1656. ttm_bo_unref(&query_val.bo);
  1657. ttm_bo_unref(&pinned_val.bo);
  1658. ttm_bo_unref(&dev_priv->pinned_bo);
  1659. }
  1660. /**
  1661. * vmw_execbuf_release_pinned_bo - Flush queries and unpin the pinned
  1662. * query bo.
  1663. *
  1664. * @dev_priv: The device private structure.
  1665. *
  1666. * This function should be used to unpin the pinned query bo, or
  1667. * as a query barrier when we need to make sure that all queries have
  1668. * finished before the next fifo command. (For example on hardware
  1669. * context destructions where the hardware may otherwise leak unfinished
  1670. * queries).
  1671. *
  1672. * This function does not return any failure codes, but make attempts
  1673. * to do safe unpinning in case of errors.
  1674. *
  1675. * The function will synchronize on the previous query barrier, and will
  1676. * thus not finish until that barrier has executed.
  1677. */
  1678. void vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv)
  1679. {
  1680. mutex_lock(&dev_priv->cmdbuf_mutex);
  1681. if (dev_priv->query_cid_valid)
  1682. __vmw_execbuf_release_pinned_bo(dev_priv, NULL);
  1683. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1684. }
  1685. int vmw_execbuf_ioctl(struct drm_device *dev, void *data,
  1686. struct drm_file *file_priv)
  1687. {
  1688. struct vmw_private *dev_priv = vmw_priv(dev);
  1689. struct drm_vmw_execbuf_arg *arg = (struct drm_vmw_execbuf_arg *)data;
  1690. struct vmw_master *vmaster = vmw_master(file_priv->master);
  1691. int ret;
  1692. /*
  1693. * This will allow us to extend the ioctl argument while
  1694. * maintaining backwards compatibility:
  1695. * We take different code paths depending on the value of
  1696. * arg->version.
  1697. */
  1698. if (unlikely(arg->version != DRM_VMW_EXECBUF_VERSION)) {
  1699. DRM_ERROR("Incorrect execbuf version.\n");
  1700. DRM_ERROR("You're running outdated experimental "
  1701. "vmwgfx user-space drivers.");
  1702. return -EINVAL;
  1703. }
  1704. ret = ttm_read_lock(&vmaster->lock, true);
  1705. if (unlikely(ret != 0))
  1706. return ret;
  1707. ret = vmw_execbuf_process(file_priv, dev_priv,
  1708. (void __user *)(unsigned long)arg->commands,
  1709. NULL, arg->command_size, arg->throttle_us,
  1710. (void __user *)(unsigned long)arg->fence_rep,
  1711. NULL);
  1712. if (unlikely(ret != 0))
  1713. goto out_unlock;
  1714. vmw_kms_cursor_post_execbuf(dev_priv);
  1715. out_unlock:
  1716. ttm_read_unlock(&vmaster->lock);
  1717. return ret;
  1718. }