vmwgfx_execbuf.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920
  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. * @staged_bindings: If @res is a context, tracks bindings set up during
  54. * the command batch. Otherwise NULL.
  55. * @new_backup_offset: New backup buffer offset if @new_backup is non-NUll.
  56. * @first_usage: Set to true the first time the resource is referenced in
  57. * the command stream.
  58. * @no_buffer_needed: Resources do not need to allocate buffer backup on
  59. * reservation. The command stream will provide one.
  60. */
  61. struct vmw_resource_val_node {
  62. struct list_head head;
  63. struct drm_hash_item hash;
  64. struct vmw_resource *res;
  65. struct vmw_dma_buffer *new_backup;
  66. struct vmw_ctx_binding_state *staged_bindings;
  67. unsigned long new_backup_offset;
  68. bool first_usage;
  69. bool no_buffer_needed;
  70. };
  71. /**
  72. * struct vmw_cmd_entry - Describe a command for the verifier
  73. *
  74. * @user_allow: Whether allowed from the execbuf ioctl.
  75. * @gb_disable: Whether disabled if guest-backed objects are available.
  76. * @gb_enable: Whether enabled iff guest-backed objects are available.
  77. */
  78. struct vmw_cmd_entry {
  79. int (*func) (struct vmw_private *, struct vmw_sw_context *,
  80. SVGA3dCmdHeader *);
  81. bool user_allow;
  82. bool gb_disable;
  83. bool gb_enable;
  84. };
  85. #define VMW_CMD_DEF(_cmd, _func, _user_allow, _gb_disable, _gb_enable) \
  86. [(_cmd) - SVGA_3D_CMD_BASE] = {(_func), (_user_allow),\
  87. (_gb_disable), (_gb_enable)}
  88. /**
  89. * vmw_resource_unreserve - unreserve resources previously reserved for
  90. * command submission.
  91. *
  92. * @list_head: list of resources to unreserve.
  93. * @backoff: Whether command submission failed.
  94. */
  95. static void vmw_resource_list_unreserve(struct list_head *list,
  96. bool backoff)
  97. {
  98. struct vmw_resource_val_node *val;
  99. list_for_each_entry(val, list, head) {
  100. struct vmw_resource *res = val->res;
  101. struct vmw_dma_buffer *new_backup =
  102. backoff ? NULL : val->new_backup;
  103. /*
  104. * Transfer staged context bindings to the
  105. * persistent context binding tracker.
  106. */
  107. if (unlikely(val->staged_bindings)) {
  108. if (!backoff) {
  109. vmw_context_binding_state_transfer
  110. (val->res, val->staged_bindings);
  111. }
  112. kfree(val->staged_bindings);
  113. val->staged_bindings = NULL;
  114. }
  115. vmw_resource_unreserve(res, new_backup,
  116. val->new_backup_offset);
  117. vmw_dmabuf_unreference(&val->new_backup);
  118. }
  119. }
  120. /**
  121. * vmw_resource_val_add - Add a resource to the software context's
  122. * resource list if it's not already on it.
  123. *
  124. * @sw_context: Pointer to the software context.
  125. * @res: Pointer to the resource.
  126. * @p_node On successful return points to a valid pointer to a
  127. * struct vmw_resource_val_node, if non-NULL on entry.
  128. */
  129. static int vmw_resource_val_add(struct vmw_sw_context *sw_context,
  130. struct vmw_resource *res,
  131. struct vmw_resource_val_node **p_node)
  132. {
  133. struct vmw_resource_val_node *node;
  134. struct drm_hash_item *hash;
  135. int ret;
  136. if (likely(drm_ht_find_item(&sw_context->res_ht, (unsigned long) res,
  137. &hash) == 0)) {
  138. node = container_of(hash, struct vmw_resource_val_node, hash);
  139. node->first_usage = false;
  140. if (unlikely(p_node != NULL))
  141. *p_node = node;
  142. return 0;
  143. }
  144. node = kzalloc(sizeof(*node), GFP_KERNEL);
  145. if (unlikely(node == NULL)) {
  146. DRM_ERROR("Failed to allocate a resource validation "
  147. "entry.\n");
  148. return -ENOMEM;
  149. }
  150. node->hash.key = (unsigned long) res;
  151. ret = drm_ht_insert_item(&sw_context->res_ht, &node->hash);
  152. if (unlikely(ret != 0)) {
  153. DRM_ERROR("Failed to initialize a resource validation "
  154. "entry.\n");
  155. kfree(node);
  156. return ret;
  157. }
  158. list_add_tail(&node->head, &sw_context->resource_list);
  159. node->res = vmw_resource_reference(res);
  160. node->first_usage = true;
  161. if (unlikely(p_node != NULL))
  162. *p_node = node;
  163. return 0;
  164. }
  165. /**
  166. * vmw_resource_context_res_add - Put resources previously bound to a context on
  167. * the validation list
  168. *
  169. * @dev_priv: Pointer to a device private structure
  170. * @sw_context: Pointer to a software context used for this command submission
  171. * @ctx: Pointer to the context resource
  172. *
  173. * This function puts all resources that were previously bound to @ctx on
  174. * the resource validation list. This is part of the context state reemission
  175. */
  176. static int vmw_resource_context_res_add(struct vmw_private *dev_priv,
  177. struct vmw_sw_context *sw_context,
  178. struct vmw_resource *ctx)
  179. {
  180. struct list_head *binding_list;
  181. struct vmw_ctx_binding *entry;
  182. int ret = 0;
  183. struct vmw_resource *res;
  184. mutex_lock(&dev_priv->binding_mutex);
  185. binding_list = vmw_context_binding_list(ctx);
  186. list_for_each_entry(entry, binding_list, ctx_list) {
  187. res = vmw_resource_reference_unless_doomed(entry->bi.res);
  188. if (unlikely(res == NULL))
  189. continue;
  190. ret = vmw_resource_val_add(sw_context, entry->bi.res, NULL);
  191. vmw_resource_unreference(&res);
  192. if (unlikely(ret != 0))
  193. break;
  194. }
  195. mutex_unlock(&dev_priv->binding_mutex);
  196. return ret;
  197. }
  198. /**
  199. * vmw_resource_relocation_add - Add a relocation to the relocation list
  200. *
  201. * @list: Pointer to head of relocation list.
  202. * @res: The resource.
  203. * @offset: Offset into the command buffer currently being parsed where the
  204. * id that needs fixup is located. Granularity is 4 bytes.
  205. */
  206. static int vmw_resource_relocation_add(struct list_head *list,
  207. const struct vmw_resource *res,
  208. unsigned long offset)
  209. {
  210. struct vmw_resource_relocation *rel;
  211. rel = kmalloc(sizeof(*rel), GFP_KERNEL);
  212. if (unlikely(rel == NULL)) {
  213. DRM_ERROR("Failed to allocate a resource relocation.\n");
  214. return -ENOMEM;
  215. }
  216. rel->res = res;
  217. rel->offset = offset;
  218. list_add_tail(&rel->head, list);
  219. return 0;
  220. }
  221. /**
  222. * vmw_resource_relocations_free - Free all relocations on a list
  223. *
  224. * @list: Pointer to the head of the relocation list.
  225. */
  226. static void vmw_resource_relocations_free(struct list_head *list)
  227. {
  228. struct vmw_resource_relocation *rel, *n;
  229. list_for_each_entry_safe(rel, n, list, head) {
  230. list_del(&rel->head);
  231. kfree(rel);
  232. }
  233. }
  234. /**
  235. * vmw_resource_relocations_apply - Apply all relocations on a list
  236. *
  237. * @cb: Pointer to the start of the command buffer bein patch. This need
  238. * not be the same buffer as the one being parsed when the relocation
  239. * list was built, but the contents must be the same modulo the
  240. * resource ids.
  241. * @list: Pointer to the head of the relocation list.
  242. */
  243. static void vmw_resource_relocations_apply(uint32_t *cb,
  244. struct list_head *list)
  245. {
  246. struct vmw_resource_relocation *rel;
  247. list_for_each_entry(rel, list, head) {
  248. if (likely(rel->res != NULL))
  249. cb[rel->offset] = rel->res->id;
  250. else
  251. cb[rel->offset] = SVGA_3D_CMD_NOP;
  252. }
  253. }
  254. static int vmw_cmd_invalid(struct vmw_private *dev_priv,
  255. struct vmw_sw_context *sw_context,
  256. SVGA3dCmdHeader *header)
  257. {
  258. return capable(CAP_SYS_ADMIN) ? : -EINVAL;
  259. }
  260. static int vmw_cmd_ok(struct vmw_private *dev_priv,
  261. struct vmw_sw_context *sw_context,
  262. SVGA3dCmdHeader *header)
  263. {
  264. return 0;
  265. }
  266. /**
  267. * vmw_bo_to_validate_list - add a bo to a validate list
  268. *
  269. * @sw_context: The software context used for this command submission batch.
  270. * @bo: The buffer object to add.
  271. * @validate_as_mob: Validate this buffer as a MOB.
  272. * @p_val_node: If non-NULL Will be updated with the validate node number
  273. * on return.
  274. *
  275. * Returns -EINVAL if the limit of number of buffer objects per command
  276. * submission is reached.
  277. */
  278. static int vmw_bo_to_validate_list(struct vmw_sw_context *sw_context,
  279. struct vmw_dma_buffer *vbo,
  280. bool validate_as_mob,
  281. uint32_t *p_val_node)
  282. {
  283. uint32_t val_node;
  284. struct vmw_validate_buffer *vval_buf;
  285. struct ttm_validate_buffer *val_buf;
  286. struct drm_hash_item *hash;
  287. int ret;
  288. if (likely(drm_ht_find_item(&sw_context->res_ht, (unsigned long) vbo,
  289. &hash) == 0)) {
  290. vval_buf = container_of(hash, struct vmw_validate_buffer,
  291. hash);
  292. if (unlikely(vval_buf->validate_as_mob != validate_as_mob)) {
  293. DRM_ERROR("Inconsistent buffer usage.\n");
  294. return -EINVAL;
  295. }
  296. val_buf = &vval_buf->base;
  297. val_node = vval_buf - sw_context->val_bufs;
  298. } else {
  299. val_node = sw_context->cur_val_buf;
  300. if (unlikely(val_node >= VMWGFX_MAX_VALIDATIONS)) {
  301. DRM_ERROR("Max number of DMA buffers per submission "
  302. "exceeded.\n");
  303. return -EINVAL;
  304. }
  305. vval_buf = &sw_context->val_bufs[val_node];
  306. vval_buf->hash.key = (unsigned long) vbo;
  307. ret = drm_ht_insert_item(&sw_context->res_ht, &vval_buf->hash);
  308. if (unlikely(ret != 0)) {
  309. DRM_ERROR("Failed to initialize a buffer validation "
  310. "entry.\n");
  311. return ret;
  312. }
  313. ++sw_context->cur_val_buf;
  314. val_buf = &vval_buf->base;
  315. val_buf->bo = ttm_bo_reference(&vbo->base);
  316. val_buf->shared = false;
  317. list_add_tail(&val_buf->head, &sw_context->validate_nodes);
  318. vval_buf->validate_as_mob = validate_as_mob;
  319. }
  320. if (p_val_node)
  321. *p_val_node = val_node;
  322. return 0;
  323. }
  324. /**
  325. * vmw_resources_reserve - Reserve all resources on the sw_context's
  326. * resource list.
  327. *
  328. * @sw_context: Pointer to the software context.
  329. *
  330. * Note that since vmware's command submission currently is protected by
  331. * the cmdbuf mutex, no fancy deadlock avoidance is required for resources,
  332. * since only a single thread at once will attempt this.
  333. */
  334. static int vmw_resources_reserve(struct vmw_sw_context *sw_context)
  335. {
  336. struct vmw_resource_val_node *val;
  337. int ret;
  338. list_for_each_entry(val, &sw_context->resource_list, head) {
  339. struct vmw_resource *res = val->res;
  340. ret = vmw_resource_reserve(res, true, val->no_buffer_needed);
  341. if (unlikely(ret != 0))
  342. return ret;
  343. if (res->backup) {
  344. struct vmw_dma_buffer *vbo = res->backup;
  345. ret = vmw_bo_to_validate_list
  346. (sw_context, vbo,
  347. vmw_resource_needs_backup(res), NULL);
  348. if (unlikely(ret != 0))
  349. return ret;
  350. }
  351. }
  352. return 0;
  353. }
  354. /**
  355. * vmw_resources_validate - Validate all resources on the sw_context's
  356. * resource list.
  357. *
  358. * @sw_context: Pointer to the software context.
  359. *
  360. * Before this function is called, all resource backup buffers must have
  361. * been validated.
  362. */
  363. static int vmw_resources_validate(struct vmw_sw_context *sw_context)
  364. {
  365. struct vmw_resource_val_node *val;
  366. int ret;
  367. list_for_each_entry(val, &sw_context->resource_list, head) {
  368. struct vmw_resource *res = val->res;
  369. ret = vmw_resource_validate(res);
  370. if (unlikely(ret != 0)) {
  371. if (ret != -ERESTARTSYS)
  372. DRM_ERROR("Failed to validate resource.\n");
  373. return ret;
  374. }
  375. }
  376. return 0;
  377. }
  378. /**
  379. * vmw_cmd_res_reloc_add - Add a resource to a software context's
  380. * relocation- and validation lists.
  381. *
  382. * @dev_priv: Pointer to a struct vmw_private identifying the device.
  383. * @sw_context: Pointer to the software context.
  384. * @res_type: Resource type.
  385. * @id_loc: Pointer to where the id that needs translation is located.
  386. * @res: Valid pointer to a struct vmw_resource.
  387. * @p_val: If non null, a pointer to the struct vmw_resource_validate_node
  388. * used for this resource is returned here.
  389. */
  390. static int vmw_cmd_res_reloc_add(struct vmw_private *dev_priv,
  391. struct vmw_sw_context *sw_context,
  392. enum vmw_res_type res_type,
  393. uint32_t *id_loc,
  394. struct vmw_resource *res,
  395. struct vmw_resource_val_node **p_val)
  396. {
  397. int ret;
  398. struct vmw_resource_val_node *node;
  399. *p_val = NULL;
  400. ret = vmw_resource_relocation_add(&sw_context->res_relocations,
  401. res,
  402. id_loc - sw_context->buf_start);
  403. if (unlikely(ret != 0))
  404. return ret;
  405. ret = vmw_resource_val_add(sw_context, res, &node);
  406. if (unlikely(ret != 0))
  407. return ret;
  408. if (res_type == vmw_res_context && dev_priv->has_mob &&
  409. node->first_usage) {
  410. /*
  411. * Put contexts first on the list to be able to exit
  412. * list traversal for contexts early.
  413. */
  414. list_del(&node->head);
  415. list_add(&node->head, &sw_context->resource_list);
  416. ret = vmw_resource_context_res_add(dev_priv, sw_context, res);
  417. if (unlikely(ret != 0))
  418. return ret;
  419. node->staged_bindings =
  420. kzalloc(sizeof(*node->staged_bindings), GFP_KERNEL);
  421. if (node->staged_bindings == NULL) {
  422. DRM_ERROR("Failed to allocate context binding "
  423. "information.\n");
  424. return -ENOMEM;
  425. }
  426. INIT_LIST_HEAD(&node->staged_bindings->list);
  427. }
  428. if (p_val)
  429. *p_val = node;
  430. return 0;
  431. }
  432. /**
  433. * vmw_cmd_res_check - Check that a resource is present and if so, put it
  434. * on the resource validate list unless it's already there.
  435. *
  436. * @dev_priv: Pointer to a device private structure.
  437. * @sw_context: Pointer to the software context.
  438. * @res_type: Resource type.
  439. * @converter: User-space visisble type specific information.
  440. * @id_loc: Pointer to the location in the command buffer currently being
  441. * parsed from where the user-space resource id handle is located.
  442. * @p_val: Pointer to pointer to resource validalidation node. Populated
  443. * on exit.
  444. */
  445. static int
  446. vmw_cmd_res_check(struct vmw_private *dev_priv,
  447. struct vmw_sw_context *sw_context,
  448. enum vmw_res_type res_type,
  449. const struct vmw_user_resource_conv *converter,
  450. uint32_t *id_loc,
  451. struct vmw_resource_val_node **p_val)
  452. {
  453. struct vmw_res_cache_entry *rcache =
  454. &sw_context->res_cache[res_type];
  455. struct vmw_resource *res;
  456. struct vmw_resource_val_node *node;
  457. int ret;
  458. if (*id_loc == SVGA3D_INVALID_ID) {
  459. if (p_val)
  460. *p_val = NULL;
  461. if (res_type == vmw_res_context) {
  462. DRM_ERROR("Illegal context invalid id.\n");
  463. return -EINVAL;
  464. }
  465. return 0;
  466. }
  467. /*
  468. * Fastpath in case of repeated commands referencing the same
  469. * resource
  470. */
  471. if (likely(rcache->valid && *id_loc == rcache->handle)) {
  472. const struct vmw_resource *res = rcache->res;
  473. rcache->node->first_usage = false;
  474. if (p_val)
  475. *p_val = rcache->node;
  476. return vmw_resource_relocation_add
  477. (&sw_context->res_relocations, res,
  478. id_loc - sw_context->buf_start);
  479. }
  480. ret = vmw_user_resource_lookup_handle(dev_priv,
  481. sw_context->fp->tfile,
  482. *id_loc,
  483. converter,
  484. &res);
  485. if (unlikely(ret != 0)) {
  486. DRM_ERROR("Could not find or use resource 0x%08x.\n",
  487. (unsigned) *id_loc);
  488. dump_stack();
  489. return ret;
  490. }
  491. rcache->valid = true;
  492. rcache->res = res;
  493. rcache->handle = *id_loc;
  494. ret = vmw_cmd_res_reloc_add(dev_priv, sw_context, res_type, id_loc,
  495. res, &node);
  496. if (unlikely(ret != 0))
  497. goto out_no_reloc;
  498. rcache->node = node;
  499. if (p_val)
  500. *p_val = node;
  501. vmw_resource_unreference(&res);
  502. return 0;
  503. out_no_reloc:
  504. BUG_ON(sw_context->error_resource != NULL);
  505. sw_context->error_resource = res;
  506. return ret;
  507. }
  508. /**
  509. * vmw_rebind_contexts - Rebind all resources previously bound to
  510. * referenced contexts.
  511. *
  512. * @sw_context: Pointer to the software context.
  513. *
  514. * Rebind context binding points that have been scrubbed because of eviction.
  515. */
  516. static int vmw_rebind_contexts(struct vmw_sw_context *sw_context)
  517. {
  518. struct vmw_resource_val_node *val;
  519. int ret;
  520. list_for_each_entry(val, &sw_context->resource_list, head) {
  521. if (unlikely(!val->staged_bindings))
  522. break;
  523. ret = vmw_context_rebind_all(val->res);
  524. if (unlikely(ret != 0)) {
  525. if (ret != -ERESTARTSYS)
  526. DRM_ERROR("Failed to rebind context.\n");
  527. return ret;
  528. }
  529. }
  530. return 0;
  531. }
  532. /**
  533. * vmw_cmd_cid_check - Check a command header for valid context information.
  534. *
  535. * @dev_priv: Pointer to a device private structure.
  536. * @sw_context: Pointer to the software context.
  537. * @header: A command header with an embedded user-space context handle.
  538. *
  539. * Convenience function: Call vmw_cmd_res_check with the user-space context
  540. * handle embedded in @header.
  541. */
  542. static int vmw_cmd_cid_check(struct vmw_private *dev_priv,
  543. struct vmw_sw_context *sw_context,
  544. SVGA3dCmdHeader *header)
  545. {
  546. struct vmw_cid_cmd {
  547. SVGA3dCmdHeader header;
  548. uint32_t cid;
  549. } *cmd;
  550. cmd = container_of(header, struct vmw_cid_cmd, header);
  551. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  552. user_context_converter, &cmd->cid, NULL);
  553. }
  554. static int vmw_cmd_set_render_target_check(struct vmw_private *dev_priv,
  555. struct vmw_sw_context *sw_context,
  556. SVGA3dCmdHeader *header)
  557. {
  558. struct vmw_sid_cmd {
  559. SVGA3dCmdHeader header;
  560. SVGA3dCmdSetRenderTarget body;
  561. } *cmd;
  562. struct vmw_resource_val_node *ctx_node;
  563. struct vmw_resource_val_node *res_node;
  564. int ret;
  565. cmd = container_of(header, struct vmw_sid_cmd, header);
  566. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  567. user_context_converter, &cmd->body.cid,
  568. &ctx_node);
  569. if (unlikely(ret != 0))
  570. return ret;
  571. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  572. user_surface_converter,
  573. &cmd->body.target.sid, &res_node);
  574. if (unlikely(ret != 0))
  575. return ret;
  576. if (dev_priv->has_mob) {
  577. struct vmw_ctx_bindinfo bi;
  578. bi.ctx = ctx_node->res;
  579. bi.res = res_node ? res_node->res : NULL;
  580. bi.bt = vmw_ctx_binding_rt;
  581. bi.i1.rt_type = cmd->body.type;
  582. return vmw_context_binding_add(ctx_node->staged_bindings, &bi);
  583. }
  584. return 0;
  585. }
  586. static int vmw_cmd_surface_copy_check(struct vmw_private *dev_priv,
  587. struct vmw_sw_context *sw_context,
  588. SVGA3dCmdHeader *header)
  589. {
  590. struct vmw_sid_cmd {
  591. SVGA3dCmdHeader header;
  592. SVGA3dCmdSurfaceCopy body;
  593. } *cmd;
  594. int ret;
  595. cmd = container_of(header, struct vmw_sid_cmd, header);
  596. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  597. user_surface_converter,
  598. &cmd->body.src.sid, NULL);
  599. if (ret)
  600. return ret;
  601. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  602. user_surface_converter,
  603. &cmd->body.dest.sid, NULL);
  604. }
  605. static int vmw_cmd_stretch_blt_check(struct vmw_private *dev_priv,
  606. struct vmw_sw_context *sw_context,
  607. SVGA3dCmdHeader *header)
  608. {
  609. struct vmw_sid_cmd {
  610. SVGA3dCmdHeader header;
  611. SVGA3dCmdSurfaceStretchBlt body;
  612. } *cmd;
  613. int ret;
  614. cmd = container_of(header, struct vmw_sid_cmd, header);
  615. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  616. user_surface_converter,
  617. &cmd->body.src.sid, NULL);
  618. if (unlikely(ret != 0))
  619. return ret;
  620. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  621. user_surface_converter,
  622. &cmd->body.dest.sid, NULL);
  623. }
  624. static int vmw_cmd_blt_surf_screen_check(struct vmw_private *dev_priv,
  625. struct vmw_sw_context *sw_context,
  626. SVGA3dCmdHeader *header)
  627. {
  628. struct vmw_sid_cmd {
  629. SVGA3dCmdHeader header;
  630. SVGA3dCmdBlitSurfaceToScreen body;
  631. } *cmd;
  632. cmd = container_of(header, struct vmw_sid_cmd, header);
  633. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  634. user_surface_converter,
  635. &cmd->body.srcImage.sid, NULL);
  636. }
  637. static int vmw_cmd_present_check(struct vmw_private *dev_priv,
  638. struct vmw_sw_context *sw_context,
  639. SVGA3dCmdHeader *header)
  640. {
  641. struct vmw_sid_cmd {
  642. SVGA3dCmdHeader header;
  643. SVGA3dCmdPresent body;
  644. } *cmd;
  645. cmd = container_of(header, struct vmw_sid_cmd, header);
  646. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  647. user_surface_converter, &cmd->body.sid,
  648. NULL);
  649. }
  650. /**
  651. * vmw_query_bo_switch_prepare - Prepare to switch pinned buffer for queries.
  652. *
  653. * @dev_priv: The device private structure.
  654. * @new_query_bo: The new buffer holding query results.
  655. * @sw_context: The software context used for this command submission.
  656. *
  657. * This function checks whether @new_query_bo is suitable for holding
  658. * query results, and if another buffer currently is pinned for query
  659. * results. If so, the function prepares the state of @sw_context for
  660. * switching pinned buffers after successful submission of the current
  661. * command batch.
  662. */
  663. static int vmw_query_bo_switch_prepare(struct vmw_private *dev_priv,
  664. struct vmw_dma_buffer *new_query_bo,
  665. struct vmw_sw_context *sw_context)
  666. {
  667. struct vmw_res_cache_entry *ctx_entry =
  668. &sw_context->res_cache[vmw_res_context];
  669. int ret;
  670. BUG_ON(!ctx_entry->valid);
  671. sw_context->last_query_ctx = ctx_entry->res;
  672. if (unlikely(new_query_bo != sw_context->cur_query_bo)) {
  673. if (unlikely(new_query_bo->base.num_pages > 4)) {
  674. DRM_ERROR("Query buffer too large.\n");
  675. return -EINVAL;
  676. }
  677. if (unlikely(sw_context->cur_query_bo != NULL)) {
  678. sw_context->needs_post_query_barrier = true;
  679. ret = vmw_bo_to_validate_list(sw_context,
  680. sw_context->cur_query_bo,
  681. dev_priv->has_mob, NULL);
  682. if (unlikely(ret != 0))
  683. return ret;
  684. }
  685. sw_context->cur_query_bo = new_query_bo;
  686. ret = vmw_bo_to_validate_list(sw_context,
  687. dev_priv->dummy_query_bo,
  688. dev_priv->has_mob, NULL);
  689. if (unlikely(ret != 0))
  690. return ret;
  691. }
  692. return 0;
  693. }
  694. /**
  695. * vmw_query_bo_switch_commit - Finalize switching pinned query buffer
  696. *
  697. * @dev_priv: The device private structure.
  698. * @sw_context: The software context used for this command submission batch.
  699. *
  700. * This function will check if we're switching query buffers, and will then,
  701. * issue a dummy occlusion query wait used as a query barrier. When the fence
  702. * object following that query wait has signaled, we are sure that all
  703. * preceding queries have finished, and the old query buffer can be unpinned.
  704. * However, since both the new query buffer and the old one are fenced with
  705. * that fence, we can do an asynchronus unpin now, and be sure that the
  706. * old query buffer won't be moved until the fence has signaled.
  707. *
  708. * As mentioned above, both the new - and old query buffers need to be fenced
  709. * using a sequence emitted *after* calling this function.
  710. */
  711. static void vmw_query_bo_switch_commit(struct vmw_private *dev_priv,
  712. struct vmw_sw_context *sw_context)
  713. {
  714. /*
  715. * The validate list should still hold references to all
  716. * contexts here.
  717. */
  718. if (sw_context->needs_post_query_barrier) {
  719. struct vmw_res_cache_entry *ctx_entry =
  720. &sw_context->res_cache[vmw_res_context];
  721. struct vmw_resource *ctx;
  722. int ret;
  723. BUG_ON(!ctx_entry->valid);
  724. ctx = ctx_entry->res;
  725. ret = vmw_fifo_emit_dummy_query(dev_priv, ctx->id);
  726. if (unlikely(ret != 0))
  727. DRM_ERROR("Out of fifo space for dummy query.\n");
  728. }
  729. if (dev_priv->pinned_bo != sw_context->cur_query_bo) {
  730. if (dev_priv->pinned_bo) {
  731. vmw_bo_pin_reserved(dev_priv->pinned_bo, false);
  732. vmw_dmabuf_unreference(&dev_priv->pinned_bo);
  733. }
  734. if (!sw_context->needs_post_query_barrier) {
  735. vmw_bo_pin_reserved(sw_context->cur_query_bo, true);
  736. /*
  737. * We pin also the dummy_query_bo buffer so that we
  738. * don't need to validate it when emitting
  739. * dummy queries in context destroy paths.
  740. */
  741. if (!dev_priv->dummy_query_bo_pinned) {
  742. vmw_bo_pin_reserved(dev_priv->dummy_query_bo,
  743. true);
  744. dev_priv->dummy_query_bo_pinned = true;
  745. }
  746. BUG_ON(sw_context->last_query_ctx == NULL);
  747. dev_priv->query_cid = sw_context->last_query_ctx->id;
  748. dev_priv->query_cid_valid = true;
  749. dev_priv->pinned_bo =
  750. vmw_dmabuf_reference(sw_context->cur_query_bo);
  751. }
  752. }
  753. }
  754. /**
  755. * vmw_translate_mob_pointer - Prepare to translate a user-space buffer
  756. * handle to a MOB id.
  757. *
  758. * @dev_priv: Pointer to a device private structure.
  759. * @sw_context: The software context used for this command batch validation.
  760. * @id: Pointer to the user-space handle to be translated.
  761. * @vmw_bo_p: Points to a location that, on successful return will carry
  762. * a reference-counted pointer to the DMA buffer identified by the
  763. * user-space handle in @id.
  764. *
  765. * This function saves information needed to translate a user-space buffer
  766. * handle to a MOB id. The translation does not take place immediately, but
  767. * during a call to vmw_apply_relocations(). This function builds a relocation
  768. * list and a list of buffers to validate. The former needs to be freed using
  769. * either vmw_apply_relocations() or vmw_free_relocations(). The latter
  770. * needs to be freed using vmw_clear_validations.
  771. */
  772. static int vmw_translate_mob_ptr(struct vmw_private *dev_priv,
  773. struct vmw_sw_context *sw_context,
  774. SVGAMobId *id,
  775. struct vmw_dma_buffer **vmw_bo_p)
  776. {
  777. struct vmw_dma_buffer *vmw_bo = NULL;
  778. uint32_t handle = *id;
  779. struct vmw_relocation *reloc;
  780. int ret;
  781. ret = vmw_user_dmabuf_lookup(sw_context->fp->tfile, handle, &vmw_bo);
  782. if (unlikely(ret != 0)) {
  783. DRM_ERROR("Could not find or use MOB buffer.\n");
  784. ret = -EINVAL;
  785. goto out_no_reloc;
  786. }
  787. if (unlikely(sw_context->cur_reloc >= VMWGFX_MAX_RELOCATIONS)) {
  788. DRM_ERROR("Max number relocations per submission"
  789. " exceeded\n");
  790. ret = -EINVAL;
  791. goto out_no_reloc;
  792. }
  793. reloc = &sw_context->relocs[sw_context->cur_reloc++];
  794. reloc->mob_loc = id;
  795. reloc->location = NULL;
  796. ret = vmw_bo_to_validate_list(sw_context, vmw_bo, true, &reloc->index);
  797. if (unlikely(ret != 0))
  798. goto out_no_reloc;
  799. *vmw_bo_p = vmw_bo;
  800. return 0;
  801. out_no_reloc:
  802. vmw_dmabuf_unreference(&vmw_bo);
  803. *vmw_bo_p = NULL;
  804. return ret;
  805. }
  806. /**
  807. * vmw_translate_guest_pointer - Prepare to translate a user-space buffer
  808. * handle to a valid SVGAGuestPtr
  809. *
  810. * @dev_priv: Pointer to a device private structure.
  811. * @sw_context: The software context used for this command batch validation.
  812. * @ptr: Pointer to the user-space handle to be translated.
  813. * @vmw_bo_p: Points to a location that, on successful return will carry
  814. * a reference-counted pointer to the DMA buffer identified by the
  815. * user-space handle in @id.
  816. *
  817. * This function saves information needed to translate a user-space buffer
  818. * handle to a valid SVGAGuestPtr. The translation does not take place
  819. * immediately, but during a call to vmw_apply_relocations().
  820. * This function builds a relocation list and a list of buffers to validate.
  821. * The former needs to be freed using either vmw_apply_relocations() or
  822. * vmw_free_relocations(). The latter needs to be freed using
  823. * vmw_clear_validations.
  824. */
  825. static int vmw_translate_guest_ptr(struct vmw_private *dev_priv,
  826. struct vmw_sw_context *sw_context,
  827. SVGAGuestPtr *ptr,
  828. struct vmw_dma_buffer **vmw_bo_p)
  829. {
  830. struct vmw_dma_buffer *vmw_bo = NULL;
  831. uint32_t handle = ptr->gmrId;
  832. struct vmw_relocation *reloc;
  833. int ret;
  834. ret = vmw_user_dmabuf_lookup(sw_context->fp->tfile, handle, &vmw_bo);
  835. if (unlikely(ret != 0)) {
  836. DRM_ERROR("Could not find or use GMR region.\n");
  837. ret = -EINVAL;
  838. goto out_no_reloc;
  839. }
  840. if (unlikely(sw_context->cur_reloc >= VMWGFX_MAX_RELOCATIONS)) {
  841. DRM_ERROR("Max number relocations per submission"
  842. " exceeded\n");
  843. ret = -EINVAL;
  844. goto out_no_reloc;
  845. }
  846. reloc = &sw_context->relocs[sw_context->cur_reloc++];
  847. reloc->location = ptr;
  848. ret = vmw_bo_to_validate_list(sw_context, vmw_bo, false, &reloc->index);
  849. if (unlikely(ret != 0))
  850. goto out_no_reloc;
  851. *vmw_bo_p = vmw_bo;
  852. return 0;
  853. out_no_reloc:
  854. vmw_dmabuf_unreference(&vmw_bo);
  855. *vmw_bo_p = NULL;
  856. return ret;
  857. }
  858. /**
  859. * vmw_cmd_begin_gb_query - validate a SVGA_3D_CMD_BEGIN_GB_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_begin_gb_query(struct vmw_private *dev_priv,
  866. struct vmw_sw_context *sw_context,
  867. SVGA3dCmdHeader *header)
  868. {
  869. struct vmw_begin_gb_query_cmd {
  870. SVGA3dCmdHeader header;
  871. SVGA3dCmdBeginGBQuery q;
  872. } *cmd;
  873. cmd = container_of(header, struct vmw_begin_gb_query_cmd,
  874. header);
  875. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  876. user_context_converter, &cmd->q.cid,
  877. NULL);
  878. }
  879. /**
  880. * vmw_cmd_begin_query - validate a SVGA_3D_CMD_BEGIN_QUERY command.
  881. *
  882. * @dev_priv: Pointer to a device private struct.
  883. * @sw_context: The software context used for this command submission.
  884. * @header: Pointer to the command header in the command stream.
  885. */
  886. static int vmw_cmd_begin_query(struct vmw_private *dev_priv,
  887. struct vmw_sw_context *sw_context,
  888. SVGA3dCmdHeader *header)
  889. {
  890. struct vmw_begin_query_cmd {
  891. SVGA3dCmdHeader header;
  892. SVGA3dCmdBeginQuery q;
  893. } *cmd;
  894. cmd = container_of(header, struct vmw_begin_query_cmd,
  895. header);
  896. if (unlikely(dev_priv->has_mob)) {
  897. struct {
  898. SVGA3dCmdHeader header;
  899. SVGA3dCmdBeginGBQuery q;
  900. } gb_cmd;
  901. BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
  902. gb_cmd.header.id = SVGA_3D_CMD_BEGIN_GB_QUERY;
  903. gb_cmd.header.size = cmd->header.size;
  904. gb_cmd.q.cid = cmd->q.cid;
  905. gb_cmd.q.type = cmd->q.type;
  906. memcpy(cmd, &gb_cmd, sizeof(*cmd));
  907. return vmw_cmd_begin_gb_query(dev_priv, sw_context, header);
  908. }
  909. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  910. user_context_converter, &cmd->q.cid,
  911. NULL);
  912. }
  913. /**
  914. * vmw_cmd_end_gb_query - validate a SVGA_3D_CMD_END_GB_QUERY command.
  915. *
  916. * @dev_priv: Pointer to a device private struct.
  917. * @sw_context: The software context used for this command submission.
  918. * @header: Pointer to the command header in the command stream.
  919. */
  920. static int vmw_cmd_end_gb_query(struct vmw_private *dev_priv,
  921. struct vmw_sw_context *sw_context,
  922. SVGA3dCmdHeader *header)
  923. {
  924. struct vmw_dma_buffer *vmw_bo;
  925. struct vmw_query_cmd {
  926. SVGA3dCmdHeader header;
  927. SVGA3dCmdEndGBQuery q;
  928. } *cmd;
  929. int ret;
  930. cmd = container_of(header, struct vmw_query_cmd, header);
  931. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  932. if (unlikely(ret != 0))
  933. return ret;
  934. ret = vmw_translate_mob_ptr(dev_priv, sw_context,
  935. &cmd->q.mobid,
  936. &vmw_bo);
  937. if (unlikely(ret != 0))
  938. return ret;
  939. ret = vmw_query_bo_switch_prepare(dev_priv, vmw_bo, sw_context);
  940. vmw_dmabuf_unreference(&vmw_bo);
  941. return ret;
  942. }
  943. /**
  944. * vmw_cmd_end_query - validate a SVGA_3D_CMD_END_QUERY command.
  945. *
  946. * @dev_priv: Pointer to a device private struct.
  947. * @sw_context: The software context used for this command submission.
  948. * @header: Pointer to the command header in the command stream.
  949. */
  950. static int vmw_cmd_end_query(struct vmw_private *dev_priv,
  951. struct vmw_sw_context *sw_context,
  952. SVGA3dCmdHeader *header)
  953. {
  954. struct vmw_dma_buffer *vmw_bo;
  955. struct vmw_query_cmd {
  956. SVGA3dCmdHeader header;
  957. SVGA3dCmdEndQuery q;
  958. } *cmd;
  959. int ret;
  960. cmd = container_of(header, struct vmw_query_cmd, header);
  961. if (dev_priv->has_mob) {
  962. struct {
  963. SVGA3dCmdHeader header;
  964. SVGA3dCmdEndGBQuery q;
  965. } gb_cmd;
  966. BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
  967. gb_cmd.header.id = SVGA_3D_CMD_END_GB_QUERY;
  968. gb_cmd.header.size = cmd->header.size;
  969. gb_cmd.q.cid = cmd->q.cid;
  970. gb_cmd.q.type = cmd->q.type;
  971. gb_cmd.q.mobid = cmd->q.guestResult.gmrId;
  972. gb_cmd.q.offset = cmd->q.guestResult.offset;
  973. memcpy(cmd, &gb_cmd, sizeof(*cmd));
  974. return vmw_cmd_end_gb_query(dev_priv, sw_context, header);
  975. }
  976. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  977. if (unlikely(ret != 0))
  978. return ret;
  979. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  980. &cmd->q.guestResult,
  981. &vmw_bo);
  982. if (unlikely(ret != 0))
  983. return ret;
  984. ret = vmw_query_bo_switch_prepare(dev_priv, vmw_bo, sw_context);
  985. vmw_dmabuf_unreference(&vmw_bo);
  986. return ret;
  987. }
  988. /**
  989. * vmw_cmd_wait_gb_query - validate a SVGA_3D_CMD_WAIT_GB_QUERY command.
  990. *
  991. * @dev_priv: Pointer to a device private struct.
  992. * @sw_context: The software context used for this command submission.
  993. * @header: Pointer to the command header in the command stream.
  994. */
  995. static int vmw_cmd_wait_gb_query(struct vmw_private *dev_priv,
  996. struct vmw_sw_context *sw_context,
  997. SVGA3dCmdHeader *header)
  998. {
  999. struct vmw_dma_buffer *vmw_bo;
  1000. struct vmw_query_cmd {
  1001. SVGA3dCmdHeader header;
  1002. SVGA3dCmdWaitForGBQuery q;
  1003. } *cmd;
  1004. int ret;
  1005. cmd = container_of(header, struct vmw_query_cmd, header);
  1006. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  1007. if (unlikely(ret != 0))
  1008. return ret;
  1009. ret = vmw_translate_mob_ptr(dev_priv, sw_context,
  1010. &cmd->q.mobid,
  1011. &vmw_bo);
  1012. if (unlikely(ret != 0))
  1013. return ret;
  1014. vmw_dmabuf_unreference(&vmw_bo);
  1015. return 0;
  1016. }
  1017. /**
  1018. * vmw_cmd_wait_query - validate a SVGA_3D_CMD_WAIT_QUERY command.
  1019. *
  1020. * @dev_priv: Pointer to a device private struct.
  1021. * @sw_context: The software context used for this command submission.
  1022. * @header: Pointer to the command header in the command stream.
  1023. */
  1024. static int vmw_cmd_wait_query(struct vmw_private *dev_priv,
  1025. struct vmw_sw_context *sw_context,
  1026. SVGA3dCmdHeader *header)
  1027. {
  1028. struct vmw_dma_buffer *vmw_bo;
  1029. struct vmw_query_cmd {
  1030. SVGA3dCmdHeader header;
  1031. SVGA3dCmdWaitForQuery q;
  1032. } *cmd;
  1033. int ret;
  1034. cmd = container_of(header, struct vmw_query_cmd, header);
  1035. if (dev_priv->has_mob) {
  1036. struct {
  1037. SVGA3dCmdHeader header;
  1038. SVGA3dCmdWaitForGBQuery q;
  1039. } gb_cmd;
  1040. BUG_ON(sizeof(gb_cmd) != sizeof(*cmd));
  1041. gb_cmd.header.id = SVGA_3D_CMD_WAIT_FOR_GB_QUERY;
  1042. gb_cmd.header.size = cmd->header.size;
  1043. gb_cmd.q.cid = cmd->q.cid;
  1044. gb_cmd.q.type = cmd->q.type;
  1045. gb_cmd.q.mobid = cmd->q.guestResult.gmrId;
  1046. gb_cmd.q.offset = cmd->q.guestResult.offset;
  1047. memcpy(cmd, &gb_cmd, sizeof(*cmd));
  1048. return vmw_cmd_wait_gb_query(dev_priv, sw_context, header);
  1049. }
  1050. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  1051. if (unlikely(ret != 0))
  1052. return ret;
  1053. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  1054. &cmd->q.guestResult,
  1055. &vmw_bo);
  1056. if (unlikely(ret != 0))
  1057. return ret;
  1058. vmw_dmabuf_unreference(&vmw_bo);
  1059. return 0;
  1060. }
  1061. static int vmw_cmd_dma(struct vmw_private *dev_priv,
  1062. struct vmw_sw_context *sw_context,
  1063. SVGA3dCmdHeader *header)
  1064. {
  1065. struct vmw_dma_buffer *vmw_bo = NULL;
  1066. struct vmw_surface *srf = NULL;
  1067. struct vmw_dma_cmd {
  1068. SVGA3dCmdHeader header;
  1069. SVGA3dCmdSurfaceDMA dma;
  1070. } *cmd;
  1071. int ret;
  1072. SVGA3dCmdSurfaceDMASuffix *suffix;
  1073. uint32_t bo_size;
  1074. cmd = container_of(header, struct vmw_dma_cmd, header);
  1075. suffix = (SVGA3dCmdSurfaceDMASuffix *)((unsigned long) &cmd->dma +
  1076. header->size - sizeof(*suffix));
  1077. /* Make sure device and verifier stays in sync. */
  1078. if (unlikely(suffix->suffixSize != sizeof(*suffix))) {
  1079. DRM_ERROR("Invalid DMA suffix size.\n");
  1080. return -EINVAL;
  1081. }
  1082. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  1083. &cmd->dma.guest.ptr,
  1084. &vmw_bo);
  1085. if (unlikely(ret != 0))
  1086. return ret;
  1087. /* Make sure DMA doesn't cross BO boundaries. */
  1088. bo_size = vmw_bo->base.num_pages * PAGE_SIZE;
  1089. if (unlikely(cmd->dma.guest.ptr.offset > bo_size)) {
  1090. DRM_ERROR("Invalid DMA offset.\n");
  1091. return -EINVAL;
  1092. }
  1093. bo_size -= cmd->dma.guest.ptr.offset;
  1094. if (unlikely(suffix->maximumOffset > bo_size))
  1095. suffix->maximumOffset = bo_size;
  1096. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1097. user_surface_converter, &cmd->dma.host.sid,
  1098. NULL);
  1099. if (unlikely(ret != 0)) {
  1100. if (unlikely(ret != -ERESTARTSYS))
  1101. DRM_ERROR("could not find surface for DMA.\n");
  1102. goto out_no_surface;
  1103. }
  1104. srf = vmw_res_to_srf(sw_context->res_cache[vmw_res_surface].res);
  1105. vmw_kms_cursor_snoop(srf, sw_context->fp->tfile, &vmw_bo->base,
  1106. header);
  1107. out_no_surface:
  1108. vmw_dmabuf_unreference(&vmw_bo);
  1109. return ret;
  1110. }
  1111. static int vmw_cmd_draw(struct vmw_private *dev_priv,
  1112. struct vmw_sw_context *sw_context,
  1113. SVGA3dCmdHeader *header)
  1114. {
  1115. struct vmw_draw_cmd {
  1116. SVGA3dCmdHeader header;
  1117. SVGA3dCmdDrawPrimitives body;
  1118. } *cmd;
  1119. SVGA3dVertexDecl *decl = (SVGA3dVertexDecl *)(
  1120. (unsigned long)header + sizeof(*cmd));
  1121. SVGA3dPrimitiveRange *range;
  1122. uint32_t i;
  1123. uint32_t maxnum;
  1124. int ret;
  1125. ret = vmw_cmd_cid_check(dev_priv, sw_context, header);
  1126. if (unlikely(ret != 0))
  1127. return ret;
  1128. cmd = container_of(header, struct vmw_draw_cmd, header);
  1129. maxnum = (header->size - sizeof(cmd->body)) / sizeof(*decl);
  1130. if (unlikely(cmd->body.numVertexDecls > maxnum)) {
  1131. DRM_ERROR("Illegal number of vertex declarations.\n");
  1132. return -EINVAL;
  1133. }
  1134. for (i = 0; i < cmd->body.numVertexDecls; ++i, ++decl) {
  1135. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1136. user_surface_converter,
  1137. &decl->array.surfaceId, NULL);
  1138. if (unlikely(ret != 0))
  1139. return ret;
  1140. }
  1141. maxnum = (header->size - sizeof(cmd->body) -
  1142. cmd->body.numVertexDecls * sizeof(*decl)) / sizeof(*range);
  1143. if (unlikely(cmd->body.numRanges > maxnum)) {
  1144. DRM_ERROR("Illegal number of index ranges.\n");
  1145. return -EINVAL;
  1146. }
  1147. range = (SVGA3dPrimitiveRange *) decl;
  1148. for (i = 0; i < cmd->body.numRanges; ++i, ++range) {
  1149. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1150. user_surface_converter,
  1151. &range->indexArray.surfaceId, NULL);
  1152. if (unlikely(ret != 0))
  1153. return ret;
  1154. }
  1155. return 0;
  1156. }
  1157. static int vmw_cmd_tex_state(struct vmw_private *dev_priv,
  1158. struct vmw_sw_context *sw_context,
  1159. SVGA3dCmdHeader *header)
  1160. {
  1161. struct vmw_tex_state_cmd {
  1162. SVGA3dCmdHeader header;
  1163. SVGA3dCmdSetTextureState state;
  1164. } *cmd;
  1165. SVGA3dTextureState *last_state = (SVGA3dTextureState *)
  1166. ((unsigned long) header + header->size + sizeof(header));
  1167. SVGA3dTextureState *cur_state = (SVGA3dTextureState *)
  1168. ((unsigned long) header + sizeof(struct vmw_tex_state_cmd));
  1169. struct vmw_resource_val_node *ctx_node;
  1170. struct vmw_resource_val_node *res_node;
  1171. int ret;
  1172. cmd = container_of(header, struct vmw_tex_state_cmd,
  1173. header);
  1174. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  1175. user_context_converter, &cmd->state.cid,
  1176. &ctx_node);
  1177. if (unlikely(ret != 0))
  1178. return ret;
  1179. for (; cur_state < last_state; ++cur_state) {
  1180. if (likely(cur_state->name != SVGA3D_TS_BIND_TEXTURE))
  1181. continue;
  1182. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1183. user_surface_converter,
  1184. &cur_state->value, &res_node);
  1185. if (unlikely(ret != 0))
  1186. return ret;
  1187. if (dev_priv->has_mob) {
  1188. struct vmw_ctx_bindinfo bi;
  1189. bi.ctx = ctx_node->res;
  1190. bi.res = res_node ? res_node->res : NULL;
  1191. bi.bt = vmw_ctx_binding_tex;
  1192. bi.i1.texture_stage = cur_state->stage;
  1193. vmw_context_binding_add(ctx_node->staged_bindings,
  1194. &bi);
  1195. }
  1196. }
  1197. return 0;
  1198. }
  1199. static int vmw_cmd_check_define_gmrfb(struct vmw_private *dev_priv,
  1200. struct vmw_sw_context *sw_context,
  1201. void *buf)
  1202. {
  1203. struct vmw_dma_buffer *vmw_bo;
  1204. int ret;
  1205. struct {
  1206. uint32_t header;
  1207. SVGAFifoCmdDefineGMRFB body;
  1208. } *cmd = buf;
  1209. ret = vmw_translate_guest_ptr(dev_priv, sw_context,
  1210. &cmd->body.ptr,
  1211. &vmw_bo);
  1212. if (unlikely(ret != 0))
  1213. return ret;
  1214. vmw_dmabuf_unreference(&vmw_bo);
  1215. return ret;
  1216. }
  1217. /**
  1218. * vmw_cmd_switch_backup - Utility function to handle backup buffer switching
  1219. *
  1220. * @dev_priv: Pointer to a device private struct.
  1221. * @sw_context: The software context being used for this batch.
  1222. * @res_type: The resource type.
  1223. * @converter: Information about user-space binding for this resource type.
  1224. * @res_id: Pointer to the user-space resource handle in the command stream.
  1225. * @buf_id: Pointer to the user-space backup buffer handle in the command
  1226. * stream.
  1227. * @backup_offset: Offset of backup into MOB.
  1228. *
  1229. * This function prepares for registering a switch of backup buffers
  1230. * in the resource metadata just prior to unreserving.
  1231. */
  1232. static int vmw_cmd_switch_backup(struct vmw_private *dev_priv,
  1233. struct vmw_sw_context *sw_context,
  1234. enum vmw_res_type res_type,
  1235. const struct vmw_user_resource_conv
  1236. *converter,
  1237. uint32_t *res_id,
  1238. uint32_t *buf_id,
  1239. unsigned long backup_offset)
  1240. {
  1241. int ret;
  1242. struct vmw_dma_buffer *dma_buf;
  1243. struct vmw_resource_val_node *val_node;
  1244. ret = vmw_cmd_res_check(dev_priv, sw_context, res_type,
  1245. converter, res_id, &val_node);
  1246. if (unlikely(ret != 0))
  1247. return ret;
  1248. ret = vmw_translate_mob_ptr(dev_priv, sw_context, buf_id, &dma_buf);
  1249. if (unlikely(ret != 0))
  1250. return ret;
  1251. if (val_node->first_usage)
  1252. val_node->no_buffer_needed = true;
  1253. vmw_dmabuf_unreference(&val_node->new_backup);
  1254. val_node->new_backup = dma_buf;
  1255. val_node->new_backup_offset = backup_offset;
  1256. return 0;
  1257. }
  1258. /**
  1259. * vmw_cmd_bind_gb_surface - Validate an SVGA_3D_CMD_BIND_GB_SURFACE
  1260. * command
  1261. *
  1262. * @dev_priv: Pointer to a device private struct.
  1263. * @sw_context: The software context being used for this batch.
  1264. * @header: Pointer to the command header in the command stream.
  1265. */
  1266. static int vmw_cmd_bind_gb_surface(struct vmw_private *dev_priv,
  1267. struct vmw_sw_context *sw_context,
  1268. SVGA3dCmdHeader *header)
  1269. {
  1270. struct vmw_bind_gb_surface_cmd {
  1271. SVGA3dCmdHeader header;
  1272. SVGA3dCmdBindGBSurface body;
  1273. } *cmd;
  1274. cmd = container_of(header, struct vmw_bind_gb_surface_cmd, header);
  1275. return vmw_cmd_switch_backup(dev_priv, sw_context, vmw_res_surface,
  1276. user_surface_converter,
  1277. &cmd->body.sid, &cmd->body.mobid,
  1278. 0);
  1279. }
  1280. /**
  1281. * vmw_cmd_update_gb_image - Validate an SVGA_3D_CMD_UPDATE_GB_IMAGE
  1282. * command
  1283. *
  1284. * @dev_priv: Pointer to a device private struct.
  1285. * @sw_context: The software context being used for this batch.
  1286. * @header: Pointer to the command header in the command stream.
  1287. */
  1288. static int vmw_cmd_update_gb_image(struct vmw_private *dev_priv,
  1289. struct vmw_sw_context *sw_context,
  1290. SVGA3dCmdHeader *header)
  1291. {
  1292. struct vmw_gb_surface_cmd {
  1293. SVGA3dCmdHeader header;
  1294. SVGA3dCmdUpdateGBImage body;
  1295. } *cmd;
  1296. cmd = container_of(header, struct vmw_gb_surface_cmd, header);
  1297. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1298. user_surface_converter,
  1299. &cmd->body.image.sid, NULL);
  1300. }
  1301. /**
  1302. * vmw_cmd_update_gb_surface - Validate an SVGA_3D_CMD_UPDATE_GB_SURFACE
  1303. * command
  1304. *
  1305. * @dev_priv: Pointer to a device private struct.
  1306. * @sw_context: The software context being used for this batch.
  1307. * @header: Pointer to the command header in the command stream.
  1308. */
  1309. static int vmw_cmd_update_gb_surface(struct vmw_private *dev_priv,
  1310. struct vmw_sw_context *sw_context,
  1311. SVGA3dCmdHeader *header)
  1312. {
  1313. struct vmw_gb_surface_cmd {
  1314. SVGA3dCmdHeader header;
  1315. SVGA3dCmdUpdateGBSurface body;
  1316. } *cmd;
  1317. cmd = container_of(header, struct vmw_gb_surface_cmd, header);
  1318. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1319. user_surface_converter,
  1320. &cmd->body.sid, NULL);
  1321. }
  1322. /**
  1323. * vmw_cmd_readback_gb_image - Validate an SVGA_3D_CMD_READBACK_GB_IMAGE
  1324. * command
  1325. *
  1326. * @dev_priv: Pointer to a device private struct.
  1327. * @sw_context: The software context being used for this batch.
  1328. * @header: Pointer to the command header in the command stream.
  1329. */
  1330. static int vmw_cmd_readback_gb_image(struct vmw_private *dev_priv,
  1331. struct vmw_sw_context *sw_context,
  1332. SVGA3dCmdHeader *header)
  1333. {
  1334. struct vmw_gb_surface_cmd {
  1335. SVGA3dCmdHeader header;
  1336. SVGA3dCmdReadbackGBImage body;
  1337. } *cmd;
  1338. cmd = container_of(header, struct vmw_gb_surface_cmd, header);
  1339. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1340. user_surface_converter,
  1341. &cmd->body.image.sid, NULL);
  1342. }
  1343. /**
  1344. * vmw_cmd_readback_gb_surface - Validate an SVGA_3D_CMD_READBACK_GB_SURFACE
  1345. * command
  1346. *
  1347. * @dev_priv: Pointer to a device private struct.
  1348. * @sw_context: The software context being used for this batch.
  1349. * @header: Pointer to the command header in the command stream.
  1350. */
  1351. static int vmw_cmd_readback_gb_surface(struct vmw_private *dev_priv,
  1352. struct vmw_sw_context *sw_context,
  1353. SVGA3dCmdHeader *header)
  1354. {
  1355. struct vmw_gb_surface_cmd {
  1356. SVGA3dCmdHeader header;
  1357. SVGA3dCmdReadbackGBSurface body;
  1358. } *cmd;
  1359. cmd = container_of(header, struct vmw_gb_surface_cmd, header);
  1360. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1361. user_surface_converter,
  1362. &cmd->body.sid, NULL);
  1363. }
  1364. /**
  1365. * vmw_cmd_invalidate_gb_image - Validate an SVGA_3D_CMD_INVALIDATE_GB_IMAGE
  1366. * command
  1367. *
  1368. * @dev_priv: Pointer to a device private struct.
  1369. * @sw_context: The software context being used for this batch.
  1370. * @header: Pointer to the command header in the command stream.
  1371. */
  1372. static int vmw_cmd_invalidate_gb_image(struct vmw_private *dev_priv,
  1373. struct vmw_sw_context *sw_context,
  1374. SVGA3dCmdHeader *header)
  1375. {
  1376. struct vmw_gb_surface_cmd {
  1377. SVGA3dCmdHeader header;
  1378. SVGA3dCmdInvalidateGBImage body;
  1379. } *cmd;
  1380. cmd = container_of(header, struct vmw_gb_surface_cmd, header);
  1381. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1382. user_surface_converter,
  1383. &cmd->body.image.sid, NULL);
  1384. }
  1385. /**
  1386. * vmw_cmd_invalidate_gb_surface - Validate an
  1387. * SVGA_3D_CMD_INVALIDATE_GB_SURFACE command
  1388. *
  1389. * @dev_priv: Pointer to a device private struct.
  1390. * @sw_context: The software context being used for this batch.
  1391. * @header: Pointer to the command header in the command stream.
  1392. */
  1393. static int vmw_cmd_invalidate_gb_surface(struct vmw_private *dev_priv,
  1394. struct vmw_sw_context *sw_context,
  1395. SVGA3dCmdHeader *header)
  1396. {
  1397. struct vmw_gb_surface_cmd {
  1398. SVGA3dCmdHeader header;
  1399. SVGA3dCmdInvalidateGBSurface body;
  1400. } *cmd;
  1401. cmd = container_of(header, struct vmw_gb_surface_cmd, header);
  1402. return vmw_cmd_res_check(dev_priv, sw_context, vmw_res_surface,
  1403. user_surface_converter,
  1404. &cmd->body.sid, NULL);
  1405. }
  1406. /**
  1407. * vmw_cmd_shader_define - Validate an SVGA_3D_CMD_SHADER_DEFINE
  1408. * command
  1409. *
  1410. * @dev_priv: Pointer to a device private struct.
  1411. * @sw_context: The software context being used for this batch.
  1412. * @header: Pointer to the command header in the command stream.
  1413. */
  1414. static int vmw_cmd_shader_define(struct vmw_private *dev_priv,
  1415. struct vmw_sw_context *sw_context,
  1416. SVGA3dCmdHeader *header)
  1417. {
  1418. struct vmw_shader_define_cmd {
  1419. SVGA3dCmdHeader header;
  1420. SVGA3dCmdDefineShader body;
  1421. } *cmd;
  1422. int ret;
  1423. size_t size;
  1424. struct vmw_resource_val_node *val;
  1425. cmd = container_of(header, struct vmw_shader_define_cmd,
  1426. header);
  1427. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  1428. user_context_converter, &cmd->body.cid,
  1429. &val);
  1430. if (unlikely(ret != 0))
  1431. return ret;
  1432. if (unlikely(!dev_priv->has_mob))
  1433. return 0;
  1434. size = cmd->header.size - sizeof(cmd->body);
  1435. ret = vmw_compat_shader_add(dev_priv,
  1436. vmw_context_res_man(val->res),
  1437. cmd->body.shid, cmd + 1,
  1438. cmd->body.type, size,
  1439. &sw_context->staged_cmd_res);
  1440. if (unlikely(ret != 0))
  1441. return ret;
  1442. return vmw_resource_relocation_add(&sw_context->res_relocations,
  1443. NULL, &cmd->header.id -
  1444. sw_context->buf_start);
  1445. return 0;
  1446. }
  1447. /**
  1448. * vmw_cmd_shader_destroy - Validate an SVGA_3D_CMD_SHADER_DESTROY
  1449. * command
  1450. *
  1451. * @dev_priv: Pointer to a device private struct.
  1452. * @sw_context: The software context being used for this batch.
  1453. * @header: Pointer to the command header in the command stream.
  1454. */
  1455. static int vmw_cmd_shader_destroy(struct vmw_private *dev_priv,
  1456. struct vmw_sw_context *sw_context,
  1457. SVGA3dCmdHeader *header)
  1458. {
  1459. struct vmw_shader_destroy_cmd {
  1460. SVGA3dCmdHeader header;
  1461. SVGA3dCmdDestroyShader body;
  1462. } *cmd;
  1463. int ret;
  1464. struct vmw_resource_val_node *val;
  1465. cmd = container_of(header, struct vmw_shader_destroy_cmd,
  1466. header);
  1467. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  1468. user_context_converter, &cmd->body.cid,
  1469. &val);
  1470. if (unlikely(ret != 0))
  1471. return ret;
  1472. if (unlikely(!dev_priv->has_mob))
  1473. return 0;
  1474. ret = vmw_compat_shader_remove(vmw_context_res_man(val->res),
  1475. cmd->body.shid,
  1476. cmd->body.type,
  1477. &sw_context->staged_cmd_res);
  1478. if (unlikely(ret != 0))
  1479. return ret;
  1480. return vmw_resource_relocation_add(&sw_context->res_relocations,
  1481. NULL, &cmd->header.id -
  1482. sw_context->buf_start);
  1483. return 0;
  1484. }
  1485. /**
  1486. * vmw_cmd_set_shader - Validate an SVGA_3D_CMD_SET_SHADER
  1487. * command
  1488. *
  1489. * @dev_priv: Pointer to a device private struct.
  1490. * @sw_context: The software context being used for this batch.
  1491. * @header: Pointer to the command header in the command stream.
  1492. */
  1493. static int vmw_cmd_set_shader(struct vmw_private *dev_priv,
  1494. struct vmw_sw_context *sw_context,
  1495. SVGA3dCmdHeader *header)
  1496. {
  1497. struct vmw_set_shader_cmd {
  1498. SVGA3dCmdHeader header;
  1499. SVGA3dCmdSetShader body;
  1500. } *cmd;
  1501. struct vmw_resource_val_node *ctx_node, *res_node = NULL;
  1502. struct vmw_ctx_bindinfo bi;
  1503. struct vmw_resource *res = NULL;
  1504. int ret;
  1505. cmd = container_of(header, struct vmw_set_shader_cmd,
  1506. header);
  1507. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  1508. user_context_converter, &cmd->body.cid,
  1509. &ctx_node);
  1510. if (unlikely(ret != 0))
  1511. return ret;
  1512. if (!dev_priv->has_mob)
  1513. return 0;
  1514. if (cmd->body.shid != SVGA3D_INVALID_ID) {
  1515. res = vmw_compat_shader_lookup
  1516. (vmw_context_res_man(ctx_node->res),
  1517. cmd->body.shid,
  1518. cmd->body.type);
  1519. if (!IS_ERR(res)) {
  1520. ret = vmw_cmd_res_reloc_add(dev_priv, sw_context,
  1521. vmw_res_shader,
  1522. &cmd->body.shid, res,
  1523. &res_node);
  1524. vmw_resource_unreference(&res);
  1525. if (unlikely(ret != 0))
  1526. return ret;
  1527. }
  1528. }
  1529. if (!res_node) {
  1530. ret = vmw_cmd_res_check(dev_priv, sw_context,
  1531. vmw_res_shader,
  1532. user_shader_converter,
  1533. &cmd->body.shid, &res_node);
  1534. if (unlikely(ret != 0))
  1535. return ret;
  1536. }
  1537. bi.ctx = ctx_node->res;
  1538. bi.res = res_node ? res_node->res : NULL;
  1539. bi.bt = vmw_ctx_binding_shader;
  1540. bi.i1.shader_type = cmd->body.type;
  1541. return vmw_context_binding_add(ctx_node->staged_bindings, &bi);
  1542. }
  1543. /**
  1544. * vmw_cmd_set_shader_const - Validate an SVGA_3D_CMD_SET_SHADER_CONST
  1545. * command
  1546. *
  1547. * @dev_priv: Pointer to a device private struct.
  1548. * @sw_context: The software context being used for this batch.
  1549. * @header: Pointer to the command header in the command stream.
  1550. */
  1551. static int vmw_cmd_set_shader_const(struct vmw_private *dev_priv,
  1552. struct vmw_sw_context *sw_context,
  1553. SVGA3dCmdHeader *header)
  1554. {
  1555. struct vmw_set_shader_const_cmd {
  1556. SVGA3dCmdHeader header;
  1557. SVGA3dCmdSetShaderConst body;
  1558. } *cmd;
  1559. int ret;
  1560. cmd = container_of(header, struct vmw_set_shader_const_cmd,
  1561. header);
  1562. ret = vmw_cmd_res_check(dev_priv, sw_context, vmw_res_context,
  1563. user_context_converter, &cmd->body.cid,
  1564. NULL);
  1565. if (unlikely(ret != 0))
  1566. return ret;
  1567. if (dev_priv->has_mob)
  1568. header->id = SVGA_3D_CMD_SET_GB_SHADERCONSTS_INLINE;
  1569. return 0;
  1570. }
  1571. /**
  1572. * vmw_cmd_bind_gb_shader - Validate an SVGA_3D_CMD_BIND_GB_SHADER
  1573. * command
  1574. *
  1575. * @dev_priv: Pointer to a device private struct.
  1576. * @sw_context: The software context being used for this batch.
  1577. * @header: Pointer to the command header in the command stream.
  1578. */
  1579. static int vmw_cmd_bind_gb_shader(struct vmw_private *dev_priv,
  1580. struct vmw_sw_context *sw_context,
  1581. SVGA3dCmdHeader *header)
  1582. {
  1583. struct vmw_bind_gb_shader_cmd {
  1584. SVGA3dCmdHeader header;
  1585. SVGA3dCmdBindGBShader body;
  1586. } *cmd;
  1587. cmd = container_of(header, struct vmw_bind_gb_shader_cmd,
  1588. header);
  1589. return vmw_cmd_switch_backup(dev_priv, sw_context, vmw_res_shader,
  1590. user_shader_converter,
  1591. &cmd->body.shid, &cmd->body.mobid,
  1592. cmd->body.offsetInBytes);
  1593. }
  1594. static int vmw_cmd_check_not_3d(struct vmw_private *dev_priv,
  1595. struct vmw_sw_context *sw_context,
  1596. void *buf, uint32_t *size)
  1597. {
  1598. uint32_t size_remaining = *size;
  1599. uint32_t cmd_id;
  1600. cmd_id = ((uint32_t *)buf)[0];
  1601. switch (cmd_id) {
  1602. case SVGA_CMD_UPDATE:
  1603. *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdUpdate);
  1604. break;
  1605. case SVGA_CMD_DEFINE_GMRFB:
  1606. *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdDefineGMRFB);
  1607. break;
  1608. case SVGA_CMD_BLIT_GMRFB_TO_SCREEN:
  1609. *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
  1610. break;
  1611. case SVGA_CMD_BLIT_SCREEN_TO_GMRFB:
  1612. *size = sizeof(uint32_t) + sizeof(SVGAFifoCmdBlitGMRFBToScreen);
  1613. break;
  1614. default:
  1615. DRM_ERROR("Unsupported SVGA command: %u.\n", cmd_id);
  1616. return -EINVAL;
  1617. }
  1618. if (*size > size_remaining) {
  1619. DRM_ERROR("Invalid SVGA command (size mismatch):"
  1620. " %u.\n", cmd_id);
  1621. return -EINVAL;
  1622. }
  1623. if (unlikely(!sw_context->kernel)) {
  1624. DRM_ERROR("Kernel only SVGA command: %u.\n", cmd_id);
  1625. return -EPERM;
  1626. }
  1627. if (cmd_id == SVGA_CMD_DEFINE_GMRFB)
  1628. return vmw_cmd_check_define_gmrfb(dev_priv, sw_context, buf);
  1629. return 0;
  1630. }
  1631. static const struct vmw_cmd_entry vmw_cmd_entries[SVGA_3D_CMD_MAX] = {
  1632. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE, &vmw_cmd_invalid,
  1633. false, false, false),
  1634. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DESTROY, &vmw_cmd_invalid,
  1635. false, false, false),
  1636. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_COPY, &vmw_cmd_surface_copy_check,
  1637. true, false, false),
  1638. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_STRETCHBLT, &vmw_cmd_stretch_blt_check,
  1639. true, false, false),
  1640. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DMA, &vmw_cmd_dma,
  1641. true, false, false),
  1642. VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DEFINE, &vmw_cmd_invalid,
  1643. false, false, false),
  1644. VMW_CMD_DEF(SVGA_3D_CMD_CONTEXT_DESTROY, &vmw_cmd_invalid,
  1645. false, false, false),
  1646. VMW_CMD_DEF(SVGA_3D_CMD_SETTRANSFORM, &vmw_cmd_cid_check,
  1647. true, false, false),
  1648. VMW_CMD_DEF(SVGA_3D_CMD_SETZRANGE, &vmw_cmd_cid_check,
  1649. true, false, false),
  1650. VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERSTATE, &vmw_cmd_cid_check,
  1651. true, false, false),
  1652. VMW_CMD_DEF(SVGA_3D_CMD_SETRENDERTARGET,
  1653. &vmw_cmd_set_render_target_check, true, false, false),
  1654. VMW_CMD_DEF(SVGA_3D_CMD_SETTEXTURESTATE, &vmw_cmd_tex_state,
  1655. true, false, false),
  1656. VMW_CMD_DEF(SVGA_3D_CMD_SETMATERIAL, &vmw_cmd_cid_check,
  1657. true, false, false),
  1658. VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTDATA, &vmw_cmd_cid_check,
  1659. true, false, false),
  1660. VMW_CMD_DEF(SVGA_3D_CMD_SETLIGHTENABLED, &vmw_cmd_cid_check,
  1661. true, false, false),
  1662. VMW_CMD_DEF(SVGA_3D_CMD_SETVIEWPORT, &vmw_cmd_cid_check,
  1663. true, false, false),
  1664. VMW_CMD_DEF(SVGA_3D_CMD_SETCLIPPLANE, &vmw_cmd_cid_check,
  1665. true, false, false),
  1666. VMW_CMD_DEF(SVGA_3D_CMD_CLEAR, &vmw_cmd_cid_check,
  1667. true, false, false),
  1668. VMW_CMD_DEF(SVGA_3D_CMD_PRESENT, &vmw_cmd_present_check,
  1669. false, false, false),
  1670. VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DEFINE, &vmw_cmd_shader_define,
  1671. true, false, false),
  1672. VMW_CMD_DEF(SVGA_3D_CMD_SHADER_DESTROY, &vmw_cmd_shader_destroy,
  1673. true, false, false),
  1674. VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER, &vmw_cmd_set_shader,
  1675. true, false, false),
  1676. VMW_CMD_DEF(SVGA_3D_CMD_SET_SHADER_CONST, &vmw_cmd_set_shader_const,
  1677. true, false, false),
  1678. VMW_CMD_DEF(SVGA_3D_CMD_DRAW_PRIMITIVES, &vmw_cmd_draw,
  1679. true, false, false),
  1680. VMW_CMD_DEF(SVGA_3D_CMD_SETSCISSORRECT, &vmw_cmd_cid_check,
  1681. true, false, false),
  1682. VMW_CMD_DEF(SVGA_3D_CMD_BEGIN_QUERY, &vmw_cmd_begin_query,
  1683. true, false, false),
  1684. VMW_CMD_DEF(SVGA_3D_CMD_END_QUERY, &vmw_cmd_end_query,
  1685. true, false, false),
  1686. VMW_CMD_DEF(SVGA_3D_CMD_WAIT_FOR_QUERY, &vmw_cmd_wait_query,
  1687. true, false, false),
  1688. VMW_CMD_DEF(SVGA_3D_CMD_PRESENT_READBACK, &vmw_cmd_ok,
  1689. true, false, false),
  1690. VMW_CMD_DEF(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN,
  1691. &vmw_cmd_blt_surf_screen_check, false, false, false),
  1692. VMW_CMD_DEF(SVGA_3D_CMD_SURFACE_DEFINE_V2, &vmw_cmd_invalid,
  1693. false, false, false),
  1694. VMW_CMD_DEF(SVGA_3D_CMD_GENERATE_MIPMAPS, &vmw_cmd_invalid,
  1695. false, false, false),
  1696. VMW_CMD_DEF(SVGA_3D_CMD_ACTIVATE_SURFACE, &vmw_cmd_invalid,
  1697. false, false, false),
  1698. VMW_CMD_DEF(SVGA_3D_CMD_DEACTIVATE_SURFACE, &vmw_cmd_invalid,
  1699. false, false, false),
  1700. VMW_CMD_DEF(SVGA_3D_CMD_SCREEN_DMA, &vmw_cmd_invalid,
  1701. false, false, false),
  1702. VMW_CMD_DEF(SVGA_3D_CMD_SET_UNITY_SURFACE_COOKIE, &vmw_cmd_invalid,
  1703. false, false, false),
  1704. VMW_CMD_DEF(SVGA_3D_CMD_OPEN_CONTEXT_SURFACE, &vmw_cmd_invalid,
  1705. false, false, false),
  1706. VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_BITBLT, &vmw_cmd_invalid,
  1707. false, false, false),
  1708. VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_TRANSBLT, &vmw_cmd_invalid,
  1709. false, false, false),
  1710. VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_STRETCHBLT, &vmw_cmd_invalid,
  1711. false, false, false),
  1712. VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_COLORFILL, &vmw_cmd_invalid,
  1713. false, false, false),
  1714. VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_ALPHABLEND, &vmw_cmd_invalid,
  1715. false, false, false),
  1716. VMW_CMD_DEF(SVGA_3D_CMD_LOGICOPS_CLEARTYPEBLEND, &vmw_cmd_invalid,
  1717. false, false, false),
  1718. VMW_CMD_DEF(SVGA_3D_CMD_SET_OTABLE_BASE, &vmw_cmd_invalid,
  1719. false, false, true),
  1720. VMW_CMD_DEF(SVGA_3D_CMD_READBACK_OTABLE, &vmw_cmd_invalid,
  1721. false, false, true),
  1722. VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_MOB, &vmw_cmd_invalid,
  1723. false, false, true),
  1724. VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_MOB, &vmw_cmd_invalid,
  1725. false, false, true),
  1726. VMW_CMD_DEF(SVGA_3D_CMD_REDEFINE_GB_MOB, &vmw_cmd_invalid,
  1727. false, false, true),
  1728. VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_MOB_MAPPING, &vmw_cmd_invalid,
  1729. false, false, true),
  1730. VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SURFACE, &vmw_cmd_invalid,
  1731. false, false, true),
  1732. VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_SURFACE, &vmw_cmd_invalid,
  1733. false, false, true),
  1734. VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SURFACE, &vmw_cmd_bind_gb_surface,
  1735. true, false, true),
  1736. VMW_CMD_DEF(SVGA_3D_CMD_COND_BIND_GB_SURFACE, &vmw_cmd_invalid,
  1737. false, false, true),
  1738. VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_IMAGE, &vmw_cmd_update_gb_image,
  1739. true, false, true),
  1740. VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_SURFACE,
  1741. &vmw_cmd_update_gb_surface, true, false, true),
  1742. VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_IMAGE,
  1743. &vmw_cmd_readback_gb_image, true, false, true),
  1744. VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_SURFACE,
  1745. &vmw_cmd_readback_gb_surface, true, false, true),
  1746. VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_IMAGE,
  1747. &vmw_cmd_invalidate_gb_image, true, false, true),
  1748. VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_SURFACE,
  1749. &vmw_cmd_invalidate_gb_surface, true, false, true),
  1750. VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_CONTEXT, &vmw_cmd_invalid,
  1751. false, false, true),
  1752. VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_CONTEXT, &vmw_cmd_invalid,
  1753. false, false, true),
  1754. VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_CONTEXT, &vmw_cmd_invalid,
  1755. false, false, true),
  1756. VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_CONTEXT, &vmw_cmd_invalid,
  1757. false, false, true),
  1758. VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_CONTEXT, &vmw_cmd_invalid,
  1759. false, false, true),
  1760. VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SHADER, &vmw_cmd_invalid,
  1761. false, false, true),
  1762. VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SHADER, &vmw_cmd_bind_gb_shader,
  1763. true, false, true),
  1764. VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_SHADER, &vmw_cmd_invalid,
  1765. false, false, true),
  1766. VMW_CMD_DEF(SVGA_3D_CMD_SET_OTABLE_BASE64, &vmw_cmd_invalid,
  1767. false, false, false),
  1768. VMW_CMD_DEF(SVGA_3D_CMD_BEGIN_GB_QUERY, &vmw_cmd_begin_gb_query,
  1769. true, false, true),
  1770. VMW_CMD_DEF(SVGA_3D_CMD_END_GB_QUERY, &vmw_cmd_end_gb_query,
  1771. true, false, true),
  1772. VMW_CMD_DEF(SVGA_3D_CMD_WAIT_FOR_GB_QUERY, &vmw_cmd_wait_gb_query,
  1773. true, false, true),
  1774. VMW_CMD_DEF(SVGA_3D_CMD_NOP, &vmw_cmd_ok,
  1775. true, false, true),
  1776. VMW_CMD_DEF(SVGA_3D_CMD_ENABLE_GART, &vmw_cmd_invalid,
  1777. false, false, true),
  1778. VMW_CMD_DEF(SVGA_3D_CMD_DISABLE_GART, &vmw_cmd_invalid,
  1779. false, false, true),
  1780. VMW_CMD_DEF(SVGA_3D_CMD_MAP_MOB_INTO_GART, &vmw_cmd_invalid,
  1781. false, false, true),
  1782. VMW_CMD_DEF(SVGA_3D_CMD_UNMAP_GART_RANGE, &vmw_cmd_invalid,
  1783. false, false, true),
  1784. VMW_CMD_DEF(SVGA_3D_CMD_DEFINE_GB_SCREENTARGET, &vmw_cmd_invalid,
  1785. false, false, true),
  1786. VMW_CMD_DEF(SVGA_3D_CMD_DESTROY_GB_SCREENTARGET, &vmw_cmd_invalid,
  1787. false, false, true),
  1788. VMW_CMD_DEF(SVGA_3D_CMD_BIND_GB_SCREENTARGET, &vmw_cmd_invalid,
  1789. false, false, true),
  1790. VMW_CMD_DEF(SVGA_3D_CMD_UPDATE_GB_SCREENTARGET, &vmw_cmd_invalid,
  1791. false, false, true),
  1792. VMW_CMD_DEF(SVGA_3D_CMD_READBACK_GB_IMAGE_PARTIAL, &vmw_cmd_invalid,
  1793. false, false, true),
  1794. VMW_CMD_DEF(SVGA_3D_CMD_INVALIDATE_GB_IMAGE_PARTIAL, &vmw_cmd_invalid,
  1795. false, false, true),
  1796. VMW_CMD_DEF(SVGA_3D_CMD_SET_GB_SHADERCONSTS_INLINE, &vmw_cmd_cid_check,
  1797. true, false, true)
  1798. };
  1799. static int vmw_cmd_check(struct vmw_private *dev_priv,
  1800. struct vmw_sw_context *sw_context,
  1801. void *buf, uint32_t *size)
  1802. {
  1803. uint32_t cmd_id;
  1804. uint32_t size_remaining = *size;
  1805. SVGA3dCmdHeader *header = (SVGA3dCmdHeader *) buf;
  1806. int ret;
  1807. const struct vmw_cmd_entry *entry;
  1808. bool gb = dev_priv->capabilities & SVGA_CAP_GBOBJECTS;
  1809. cmd_id = ((uint32_t *)buf)[0];
  1810. /* Handle any none 3D commands */
  1811. if (unlikely(cmd_id < SVGA_CMD_MAX))
  1812. return vmw_cmd_check_not_3d(dev_priv, sw_context, buf, size);
  1813. cmd_id = header->id;
  1814. *size = header->size + sizeof(SVGA3dCmdHeader);
  1815. cmd_id -= SVGA_3D_CMD_BASE;
  1816. if (unlikely(*size > size_remaining))
  1817. goto out_invalid;
  1818. if (unlikely(cmd_id >= SVGA_3D_CMD_MAX - SVGA_3D_CMD_BASE))
  1819. goto out_invalid;
  1820. entry = &vmw_cmd_entries[cmd_id];
  1821. if (unlikely(!entry->func))
  1822. goto out_invalid;
  1823. if (unlikely(!entry->user_allow && !sw_context->kernel))
  1824. goto out_privileged;
  1825. if (unlikely(entry->gb_disable && gb))
  1826. goto out_old;
  1827. if (unlikely(entry->gb_enable && !gb))
  1828. goto out_new;
  1829. ret = entry->func(dev_priv, sw_context, header);
  1830. if (unlikely(ret != 0))
  1831. goto out_invalid;
  1832. return 0;
  1833. out_invalid:
  1834. DRM_ERROR("Invalid SVGA3D command: %d\n",
  1835. cmd_id + SVGA_3D_CMD_BASE);
  1836. return -EINVAL;
  1837. out_privileged:
  1838. DRM_ERROR("Privileged SVGA3D command: %d\n",
  1839. cmd_id + SVGA_3D_CMD_BASE);
  1840. return -EPERM;
  1841. out_old:
  1842. DRM_ERROR("Deprecated (disallowed) SVGA3D command: %d\n",
  1843. cmd_id + SVGA_3D_CMD_BASE);
  1844. return -EINVAL;
  1845. out_new:
  1846. DRM_ERROR("SVGA3D command: %d not supported by virtual hardware.\n",
  1847. cmd_id + SVGA_3D_CMD_BASE);
  1848. return -EINVAL;
  1849. }
  1850. static int vmw_cmd_check_all(struct vmw_private *dev_priv,
  1851. struct vmw_sw_context *sw_context,
  1852. void *buf,
  1853. uint32_t size)
  1854. {
  1855. int32_t cur_size = size;
  1856. int ret;
  1857. sw_context->buf_start = buf;
  1858. while (cur_size > 0) {
  1859. size = cur_size;
  1860. ret = vmw_cmd_check(dev_priv, sw_context, buf, &size);
  1861. if (unlikely(ret != 0))
  1862. return ret;
  1863. buf = (void *)((unsigned long) buf + size);
  1864. cur_size -= size;
  1865. }
  1866. if (unlikely(cur_size != 0)) {
  1867. DRM_ERROR("Command verifier out of sync.\n");
  1868. return -EINVAL;
  1869. }
  1870. return 0;
  1871. }
  1872. static void vmw_free_relocations(struct vmw_sw_context *sw_context)
  1873. {
  1874. sw_context->cur_reloc = 0;
  1875. }
  1876. static void vmw_apply_relocations(struct vmw_sw_context *sw_context)
  1877. {
  1878. uint32_t i;
  1879. struct vmw_relocation *reloc;
  1880. struct ttm_validate_buffer *validate;
  1881. struct ttm_buffer_object *bo;
  1882. for (i = 0; i < sw_context->cur_reloc; ++i) {
  1883. reloc = &sw_context->relocs[i];
  1884. validate = &sw_context->val_bufs[reloc->index].base;
  1885. bo = validate->bo;
  1886. switch (bo->mem.mem_type) {
  1887. case TTM_PL_VRAM:
  1888. reloc->location->offset += bo->offset;
  1889. reloc->location->gmrId = SVGA_GMR_FRAMEBUFFER;
  1890. break;
  1891. case VMW_PL_GMR:
  1892. reloc->location->gmrId = bo->mem.start;
  1893. break;
  1894. case VMW_PL_MOB:
  1895. *reloc->mob_loc = bo->mem.start;
  1896. break;
  1897. default:
  1898. BUG();
  1899. }
  1900. }
  1901. vmw_free_relocations(sw_context);
  1902. }
  1903. /**
  1904. * vmw_resource_list_unrefererence - Free up a resource list and unreference
  1905. * all resources referenced by it.
  1906. *
  1907. * @list: The resource list.
  1908. */
  1909. static void vmw_resource_list_unreference(struct list_head *list)
  1910. {
  1911. struct vmw_resource_val_node *val, *val_next;
  1912. /*
  1913. * Drop references to resources held during command submission.
  1914. */
  1915. list_for_each_entry_safe(val, val_next, list, head) {
  1916. list_del_init(&val->head);
  1917. vmw_resource_unreference(&val->res);
  1918. if (unlikely(val->staged_bindings))
  1919. kfree(val->staged_bindings);
  1920. kfree(val);
  1921. }
  1922. }
  1923. static void vmw_clear_validations(struct vmw_sw_context *sw_context)
  1924. {
  1925. struct vmw_validate_buffer *entry, *next;
  1926. struct vmw_resource_val_node *val;
  1927. /*
  1928. * Drop references to DMA buffers held during command submission.
  1929. */
  1930. list_for_each_entry_safe(entry, next, &sw_context->validate_nodes,
  1931. base.head) {
  1932. list_del(&entry->base.head);
  1933. ttm_bo_unref(&entry->base.bo);
  1934. (void) drm_ht_remove_item(&sw_context->res_ht, &entry->hash);
  1935. sw_context->cur_val_buf--;
  1936. }
  1937. BUG_ON(sw_context->cur_val_buf != 0);
  1938. list_for_each_entry(val, &sw_context->resource_list, head)
  1939. (void) drm_ht_remove_item(&sw_context->res_ht, &val->hash);
  1940. }
  1941. int vmw_validate_single_buffer(struct vmw_private *dev_priv,
  1942. struct ttm_buffer_object *bo,
  1943. bool interruptible,
  1944. bool validate_as_mob)
  1945. {
  1946. struct vmw_dma_buffer *vbo = container_of(bo, struct vmw_dma_buffer,
  1947. base);
  1948. int ret;
  1949. if (vbo->pin_count > 0)
  1950. return 0;
  1951. if (validate_as_mob)
  1952. return ttm_bo_validate(bo, &vmw_mob_placement, interruptible,
  1953. false);
  1954. /**
  1955. * Put BO in VRAM if there is space, otherwise as a GMR.
  1956. * If there is no space in VRAM and GMR ids are all used up,
  1957. * start evicting GMRs to make room. If the DMA buffer can't be
  1958. * used as a GMR, this will return -ENOMEM.
  1959. */
  1960. ret = ttm_bo_validate(bo, &vmw_vram_gmr_placement, interruptible,
  1961. false);
  1962. if (likely(ret == 0 || ret == -ERESTARTSYS))
  1963. return ret;
  1964. /**
  1965. * If that failed, try VRAM again, this time evicting
  1966. * previous contents.
  1967. */
  1968. ret = ttm_bo_validate(bo, &vmw_vram_placement, interruptible, false);
  1969. return ret;
  1970. }
  1971. static int vmw_validate_buffers(struct vmw_private *dev_priv,
  1972. struct vmw_sw_context *sw_context)
  1973. {
  1974. struct vmw_validate_buffer *entry;
  1975. int ret;
  1976. list_for_each_entry(entry, &sw_context->validate_nodes, base.head) {
  1977. ret = vmw_validate_single_buffer(dev_priv, entry->base.bo,
  1978. true,
  1979. entry->validate_as_mob);
  1980. if (unlikely(ret != 0))
  1981. return ret;
  1982. }
  1983. return 0;
  1984. }
  1985. static int vmw_resize_cmd_bounce(struct vmw_sw_context *sw_context,
  1986. uint32_t size)
  1987. {
  1988. if (likely(sw_context->cmd_bounce_size >= size))
  1989. return 0;
  1990. if (sw_context->cmd_bounce_size == 0)
  1991. sw_context->cmd_bounce_size = VMWGFX_CMD_BOUNCE_INIT_SIZE;
  1992. while (sw_context->cmd_bounce_size < size) {
  1993. sw_context->cmd_bounce_size =
  1994. PAGE_ALIGN(sw_context->cmd_bounce_size +
  1995. (sw_context->cmd_bounce_size >> 1));
  1996. }
  1997. if (sw_context->cmd_bounce != NULL)
  1998. vfree(sw_context->cmd_bounce);
  1999. sw_context->cmd_bounce = vmalloc(sw_context->cmd_bounce_size);
  2000. if (sw_context->cmd_bounce == NULL) {
  2001. DRM_ERROR("Failed to allocate command bounce buffer.\n");
  2002. sw_context->cmd_bounce_size = 0;
  2003. return -ENOMEM;
  2004. }
  2005. return 0;
  2006. }
  2007. /**
  2008. * vmw_execbuf_fence_commands - create and submit a command stream fence
  2009. *
  2010. * Creates a fence object and submits a command stream marker.
  2011. * If this fails for some reason, We sync the fifo and return NULL.
  2012. * It is then safe to fence buffers with a NULL pointer.
  2013. *
  2014. * If @p_handle is not NULL @file_priv must also not be NULL. Creates
  2015. * a userspace handle if @p_handle is not NULL, otherwise not.
  2016. */
  2017. int vmw_execbuf_fence_commands(struct drm_file *file_priv,
  2018. struct vmw_private *dev_priv,
  2019. struct vmw_fence_obj **p_fence,
  2020. uint32_t *p_handle)
  2021. {
  2022. uint32_t sequence;
  2023. int ret;
  2024. bool synced = false;
  2025. /* p_handle implies file_priv. */
  2026. BUG_ON(p_handle != NULL && file_priv == NULL);
  2027. ret = vmw_fifo_send_fence(dev_priv, &sequence);
  2028. if (unlikely(ret != 0)) {
  2029. DRM_ERROR("Fence submission error. Syncing.\n");
  2030. synced = true;
  2031. }
  2032. if (p_handle != NULL)
  2033. ret = vmw_user_fence_create(file_priv, dev_priv->fman,
  2034. sequence, p_fence, p_handle);
  2035. else
  2036. ret = vmw_fence_create(dev_priv->fman, sequence, p_fence);
  2037. if (unlikely(ret != 0 && !synced)) {
  2038. (void) vmw_fallback_wait(dev_priv, false, false,
  2039. sequence, false,
  2040. VMW_FENCE_WAIT_TIMEOUT);
  2041. *p_fence = NULL;
  2042. }
  2043. return 0;
  2044. }
  2045. /**
  2046. * vmw_execbuf_copy_fence_user - copy fence object information to
  2047. * user-space.
  2048. *
  2049. * @dev_priv: Pointer to a vmw_private struct.
  2050. * @vmw_fp: Pointer to the struct vmw_fpriv representing the calling file.
  2051. * @ret: Return value from fence object creation.
  2052. * @user_fence_rep: User space address of a struct drm_vmw_fence_rep to
  2053. * which the information should be copied.
  2054. * @fence: Pointer to the fenc object.
  2055. * @fence_handle: User-space fence handle.
  2056. *
  2057. * This function copies fence information to user-space. If copying fails,
  2058. * The user-space struct drm_vmw_fence_rep::error member is hopefully
  2059. * left untouched, and if it's preloaded with an -EFAULT by user-space,
  2060. * the error will hopefully be detected.
  2061. * Also if copying fails, user-space will be unable to signal the fence
  2062. * object so we wait for it immediately, and then unreference the
  2063. * user-space reference.
  2064. */
  2065. void
  2066. vmw_execbuf_copy_fence_user(struct vmw_private *dev_priv,
  2067. struct vmw_fpriv *vmw_fp,
  2068. int ret,
  2069. struct drm_vmw_fence_rep __user *user_fence_rep,
  2070. struct vmw_fence_obj *fence,
  2071. uint32_t fence_handle)
  2072. {
  2073. struct drm_vmw_fence_rep fence_rep;
  2074. if (user_fence_rep == NULL)
  2075. return;
  2076. memset(&fence_rep, 0, sizeof(fence_rep));
  2077. fence_rep.error = ret;
  2078. if (ret == 0) {
  2079. BUG_ON(fence == NULL);
  2080. fence_rep.handle = fence_handle;
  2081. fence_rep.seqno = fence->base.seqno;
  2082. vmw_update_seqno(dev_priv, &dev_priv->fifo);
  2083. fence_rep.passed_seqno = dev_priv->last_read_seqno;
  2084. }
  2085. /*
  2086. * copy_to_user errors will be detected by user space not
  2087. * seeing fence_rep::error filled in. Typically
  2088. * user-space would have pre-set that member to -EFAULT.
  2089. */
  2090. ret = copy_to_user(user_fence_rep, &fence_rep,
  2091. sizeof(fence_rep));
  2092. /*
  2093. * User-space lost the fence object. We need to sync
  2094. * and unreference the handle.
  2095. */
  2096. if (unlikely(ret != 0) && (fence_rep.error == 0)) {
  2097. ttm_ref_object_base_unref(vmw_fp->tfile,
  2098. fence_handle, TTM_REF_USAGE);
  2099. DRM_ERROR("Fence copy error. Syncing.\n");
  2100. (void) vmw_fence_obj_wait(fence, false, false,
  2101. VMW_FENCE_WAIT_TIMEOUT);
  2102. }
  2103. }
  2104. /**
  2105. * vmw_execbuf_submit_fifo - Patch a command batch and submit it using
  2106. * the fifo.
  2107. *
  2108. * @dev_priv: Pointer to a device private structure.
  2109. * @kernel_commands: Pointer to the unpatched command batch.
  2110. * @command_size: Size of the unpatched command batch.
  2111. * @sw_context: Structure holding the relocation lists.
  2112. *
  2113. * Side effects: If this function returns 0, then the command batch
  2114. * pointed to by @kernel_commands will have been modified.
  2115. */
  2116. static int vmw_execbuf_submit_fifo(struct vmw_private *dev_priv,
  2117. void *kernel_commands,
  2118. u32 command_size,
  2119. struct vmw_sw_context *sw_context)
  2120. {
  2121. void *cmd = vmw_fifo_reserve(dev_priv, command_size);
  2122. if (!cmd) {
  2123. DRM_ERROR("Failed reserving fifo space for commands.\n");
  2124. return -ENOMEM;
  2125. }
  2126. vmw_apply_relocations(sw_context);
  2127. memcpy(cmd, kernel_commands, command_size);
  2128. vmw_resource_relocations_apply(cmd, &sw_context->res_relocations);
  2129. vmw_resource_relocations_free(&sw_context->res_relocations);
  2130. vmw_fifo_commit(dev_priv, command_size);
  2131. return 0;
  2132. }
  2133. /**
  2134. * vmw_execbuf_submit_cmdbuf - Patch a command batch and submit it using
  2135. * the command buffer manager.
  2136. *
  2137. * @dev_priv: Pointer to a device private structure.
  2138. * @header: Opaque handle to the command buffer allocation.
  2139. * @command_size: Size of the unpatched command batch.
  2140. * @sw_context: Structure holding the relocation lists.
  2141. *
  2142. * Side effects: If this function returns 0, then the command buffer
  2143. * represented by @header will have been modified.
  2144. */
  2145. static int vmw_execbuf_submit_cmdbuf(struct vmw_private *dev_priv,
  2146. struct vmw_cmdbuf_header *header,
  2147. u32 command_size,
  2148. struct vmw_sw_context *sw_context)
  2149. {
  2150. void *cmd = vmw_cmdbuf_reserve(dev_priv->cman, command_size,
  2151. SVGA3D_INVALID_ID, false, header);
  2152. vmw_apply_relocations(sw_context);
  2153. vmw_resource_relocations_apply(cmd, &sw_context->res_relocations);
  2154. vmw_resource_relocations_free(&sw_context->res_relocations);
  2155. vmw_cmdbuf_commit(dev_priv->cman, command_size, header, false);
  2156. return 0;
  2157. }
  2158. /**
  2159. * vmw_execbuf_cmdbuf - Prepare, if possible, a user-space command batch for
  2160. * submission using a command buffer.
  2161. *
  2162. * @dev_priv: Pointer to a device private structure.
  2163. * @user_commands: User-space pointer to the commands to be submitted.
  2164. * @command_size: Size of the unpatched command batch.
  2165. * @header: Out parameter returning the opaque pointer to the command buffer.
  2166. *
  2167. * This function checks whether we can use the command buffer manager for
  2168. * submission and if so, creates a command buffer of suitable size and
  2169. * copies the user data into that buffer.
  2170. *
  2171. * On successful return, the function returns a pointer to the data in the
  2172. * command buffer and *@header is set to non-NULL.
  2173. * If command buffers could not be used, the function will return the value
  2174. * of @kernel_commands on function call. That value may be NULL. In that case,
  2175. * the value of *@header will be set to NULL.
  2176. * If an error is encountered, the function will return a pointer error value.
  2177. * If the function is interrupted by a signal while sleeping, it will return
  2178. * -ERESTARTSYS casted to a pointer error value.
  2179. */
  2180. static void *vmw_execbuf_cmdbuf(struct vmw_private *dev_priv,
  2181. void __user *user_commands,
  2182. void *kernel_commands,
  2183. u32 command_size,
  2184. struct vmw_cmdbuf_header **header)
  2185. {
  2186. size_t cmdbuf_size;
  2187. int ret;
  2188. *header = NULL;
  2189. if (!dev_priv->cman || kernel_commands)
  2190. return kernel_commands;
  2191. if (command_size > SVGA_CB_MAX_SIZE) {
  2192. DRM_ERROR("Command buffer is too large.\n");
  2193. return ERR_PTR(-EINVAL);
  2194. }
  2195. /* If possible, add a little space for fencing. */
  2196. cmdbuf_size = command_size + 512;
  2197. cmdbuf_size = min_t(size_t, cmdbuf_size, SVGA_CB_MAX_SIZE);
  2198. kernel_commands = vmw_cmdbuf_alloc(dev_priv->cman, cmdbuf_size,
  2199. true, header);
  2200. if (IS_ERR(kernel_commands))
  2201. return kernel_commands;
  2202. ret = copy_from_user(kernel_commands, user_commands,
  2203. command_size);
  2204. if (ret) {
  2205. DRM_ERROR("Failed copying commands.\n");
  2206. vmw_cmdbuf_header_free(*header);
  2207. *header = NULL;
  2208. return ERR_PTR(-EFAULT);
  2209. }
  2210. return kernel_commands;
  2211. }
  2212. int vmw_execbuf_process(struct drm_file *file_priv,
  2213. struct vmw_private *dev_priv,
  2214. void __user *user_commands,
  2215. void *kernel_commands,
  2216. uint32_t command_size,
  2217. uint64_t throttle_us,
  2218. struct drm_vmw_fence_rep __user *user_fence_rep,
  2219. struct vmw_fence_obj **out_fence)
  2220. {
  2221. struct vmw_sw_context *sw_context = &dev_priv->ctx;
  2222. struct vmw_fence_obj *fence = NULL;
  2223. struct vmw_resource *error_resource;
  2224. struct list_head resource_list;
  2225. struct vmw_cmdbuf_header *header;
  2226. struct ww_acquire_ctx ticket;
  2227. uint32_t handle;
  2228. int ret;
  2229. if (throttle_us) {
  2230. ret = vmw_wait_lag(dev_priv, &dev_priv->fifo.marker_queue,
  2231. throttle_us);
  2232. if (ret)
  2233. return ret;
  2234. }
  2235. kernel_commands = vmw_execbuf_cmdbuf(dev_priv, user_commands,
  2236. kernel_commands, command_size,
  2237. &header);
  2238. if (IS_ERR(kernel_commands))
  2239. return PTR_ERR(kernel_commands);
  2240. ret = mutex_lock_interruptible(&dev_priv->cmdbuf_mutex);
  2241. if (ret) {
  2242. ret = -ERESTARTSYS;
  2243. goto out_free_header;
  2244. }
  2245. sw_context->kernel = false;
  2246. if (kernel_commands == NULL) {
  2247. ret = vmw_resize_cmd_bounce(sw_context, command_size);
  2248. if (unlikely(ret != 0))
  2249. goto out_unlock;
  2250. ret = copy_from_user(sw_context->cmd_bounce,
  2251. user_commands, command_size);
  2252. if (unlikely(ret != 0)) {
  2253. ret = -EFAULT;
  2254. DRM_ERROR("Failed copying commands.\n");
  2255. goto out_unlock;
  2256. }
  2257. kernel_commands = sw_context->cmd_bounce;
  2258. } else if (!header)
  2259. sw_context->kernel = true;
  2260. sw_context->fp = vmw_fpriv(file_priv);
  2261. sw_context->cur_reloc = 0;
  2262. sw_context->cur_val_buf = 0;
  2263. INIT_LIST_HEAD(&sw_context->resource_list);
  2264. sw_context->cur_query_bo = dev_priv->pinned_bo;
  2265. sw_context->last_query_ctx = NULL;
  2266. sw_context->needs_post_query_barrier = false;
  2267. memset(sw_context->res_cache, 0, sizeof(sw_context->res_cache));
  2268. INIT_LIST_HEAD(&sw_context->validate_nodes);
  2269. INIT_LIST_HEAD(&sw_context->res_relocations);
  2270. if (!sw_context->res_ht_initialized) {
  2271. ret = drm_ht_create(&sw_context->res_ht, VMW_RES_HT_ORDER);
  2272. if (unlikely(ret != 0))
  2273. goto out_unlock;
  2274. sw_context->res_ht_initialized = true;
  2275. }
  2276. INIT_LIST_HEAD(&sw_context->staged_cmd_res);
  2277. INIT_LIST_HEAD(&resource_list);
  2278. ret = vmw_cmd_check_all(dev_priv, sw_context, kernel_commands,
  2279. command_size);
  2280. if (unlikely(ret != 0))
  2281. goto out_err_nores;
  2282. ret = vmw_resources_reserve(sw_context);
  2283. if (unlikely(ret != 0))
  2284. goto out_err_nores;
  2285. ret = ttm_eu_reserve_buffers(&ticket, &sw_context->validate_nodes,
  2286. true, NULL);
  2287. if (unlikely(ret != 0))
  2288. goto out_err;
  2289. ret = vmw_validate_buffers(dev_priv, sw_context);
  2290. if (unlikely(ret != 0))
  2291. goto out_err;
  2292. ret = vmw_resources_validate(sw_context);
  2293. if (unlikely(ret != 0))
  2294. goto out_err;
  2295. ret = mutex_lock_interruptible(&dev_priv->binding_mutex);
  2296. if (unlikely(ret != 0)) {
  2297. ret = -ERESTARTSYS;
  2298. goto out_err;
  2299. }
  2300. if (dev_priv->has_mob) {
  2301. ret = vmw_rebind_contexts(sw_context);
  2302. if (unlikely(ret != 0))
  2303. goto out_unlock_binding;
  2304. }
  2305. if (!header) {
  2306. ret = vmw_execbuf_submit_fifo(dev_priv, kernel_commands,
  2307. command_size, sw_context);
  2308. } else {
  2309. ret = vmw_execbuf_submit_cmdbuf(dev_priv, header, command_size,
  2310. sw_context);
  2311. header = NULL;
  2312. }
  2313. if (ret)
  2314. goto out_unlock_binding;
  2315. vmw_query_bo_switch_commit(dev_priv, sw_context);
  2316. ret = vmw_execbuf_fence_commands(file_priv, dev_priv,
  2317. &fence,
  2318. (user_fence_rep) ? &handle : NULL);
  2319. /*
  2320. * This error is harmless, because if fence submission fails,
  2321. * vmw_fifo_send_fence will sync. The error will be propagated to
  2322. * user-space in @fence_rep
  2323. */
  2324. if (ret != 0)
  2325. DRM_ERROR("Fence submission error. Syncing.\n");
  2326. vmw_resource_list_unreserve(&sw_context->resource_list, false);
  2327. mutex_unlock(&dev_priv->binding_mutex);
  2328. ttm_eu_fence_buffer_objects(&ticket, &sw_context->validate_nodes,
  2329. (void *) fence);
  2330. if (unlikely(dev_priv->pinned_bo != NULL &&
  2331. !dev_priv->query_cid_valid))
  2332. __vmw_execbuf_release_pinned_bo(dev_priv, fence);
  2333. vmw_clear_validations(sw_context);
  2334. vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv), ret,
  2335. user_fence_rep, fence, handle);
  2336. /* Don't unreference when handing fence out */
  2337. if (unlikely(out_fence != NULL)) {
  2338. *out_fence = fence;
  2339. fence = NULL;
  2340. } else if (likely(fence != NULL)) {
  2341. vmw_fence_obj_unreference(&fence);
  2342. }
  2343. list_splice_init(&sw_context->resource_list, &resource_list);
  2344. vmw_cmdbuf_res_commit(&sw_context->staged_cmd_res);
  2345. mutex_unlock(&dev_priv->cmdbuf_mutex);
  2346. /*
  2347. * Unreference resources outside of the cmdbuf_mutex to
  2348. * avoid deadlocks in resource destruction paths.
  2349. */
  2350. vmw_resource_list_unreference(&resource_list);
  2351. return 0;
  2352. out_unlock_binding:
  2353. mutex_unlock(&dev_priv->binding_mutex);
  2354. out_err:
  2355. ttm_eu_backoff_reservation(&ticket, &sw_context->validate_nodes);
  2356. out_err_nores:
  2357. vmw_resource_list_unreserve(&sw_context->resource_list, true);
  2358. vmw_resource_relocations_free(&sw_context->res_relocations);
  2359. vmw_free_relocations(sw_context);
  2360. vmw_clear_validations(sw_context);
  2361. if (unlikely(dev_priv->pinned_bo != NULL &&
  2362. !dev_priv->query_cid_valid))
  2363. __vmw_execbuf_release_pinned_bo(dev_priv, NULL);
  2364. out_unlock:
  2365. list_splice_init(&sw_context->resource_list, &resource_list);
  2366. error_resource = sw_context->error_resource;
  2367. sw_context->error_resource = NULL;
  2368. vmw_cmdbuf_res_revert(&sw_context->staged_cmd_res);
  2369. mutex_unlock(&dev_priv->cmdbuf_mutex);
  2370. /*
  2371. * Unreference resources outside of the cmdbuf_mutex to
  2372. * avoid deadlocks in resource destruction paths.
  2373. */
  2374. vmw_resource_list_unreference(&resource_list);
  2375. if (unlikely(error_resource != NULL))
  2376. vmw_resource_unreference(&error_resource);
  2377. out_free_header:
  2378. if (header)
  2379. vmw_cmdbuf_header_free(header);
  2380. return ret;
  2381. }
  2382. /**
  2383. * vmw_execbuf_unpin_panic - Idle the fifo and unpin the query buffer.
  2384. *
  2385. * @dev_priv: The device private structure.
  2386. *
  2387. * This function is called to idle the fifo and unpin the query buffer
  2388. * if the normal way to do this hits an error, which should typically be
  2389. * extremely rare.
  2390. */
  2391. static void vmw_execbuf_unpin_panic(struct vmw_private *dev_priv)
  2392. {
  2393. DRM_ERROR("Can't unpin query buffer. Trying to recover.\n");
  2394. (void) vmw_fallback_wait(dev_priv, false, true, 0, false, 10*HZ);
  2395. vmw_bo_pin_reserved(dev_priv->pinned_bo, false);
  2396. if (dev_priv->dummy_query_bo_pinned) {
  2397. vmw_bo_pin_reserved(dev_priv->dummy_query_bo, false);
  2398. dev_priv->dummy_query_bo_pinned = false;
  2399. }
  2400. }
  2401. /**
  2402. * __vmw_execbuf_release_pinned_bo - Flush queries and unpin the pinned
  2403. * query bo.
  2404. *
  2405. * @dev_priv: The device private structure.
  2406. * @fence: If non-NULL should point to a struct vmw_fence_obj issued
  2407. * _after_ a query barrier that flushes all queries touching the current
  2408. * buffer pointed to by @dev_priv->pinned_bo
  2409. *
  2410. * This function should be used to unpin the pinned query bo, or
  2411. * as a query barrier when we need to make sure that all queries have
  2412. * finished before the next fifo command. (For example on hardware
  2413. * context destructions where the hardware may otherwise leak unfinished
  2414. * queries).
  2415. *
  2416. * This function does not return any failure codes, but make attempts
  2417. * to do safe unpinning in case of errors.
  2418. *
  2419. * The function will synchronize on the previous query barrier, and will
  2420. * thus not finish until that barrier has executed.
  2421. *
  2422. * the @dev_priv->cmdbuf_mutex needs to be held by the current thread
  2423. * before calling this function.
  2424. */
  2425. void __vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv,
  2426. struct vmw_fence_obj *fence)
  2427. {
  2428. int ret = 0;
  2429. struct list_head validate_list;
  2430. struct ttm_validate_buffer pinned_val, query_val;
  2431. struct vmw_fence_obj *lfence = NULL;
  2432. struct ww_acquire_ctx ticket;
  2433. if (dev_priv->pinned_bo == NULL)
  2434. goto out_unlock;
  2435. INIT_LIST_HEAD(&validate_list);
  2436. pinned_val.bo = ttm_bo_reference(&dev_priv->pinned_bo->base);
  2437. pinned_val.shared = false;
  2438. list_add_tail(&pinned_val.head, &validate_list);
  2439. query_val.bo = ttm_bo_reference(&dev_priv->dummy_query_bo->base);
  2440. query_val.shared = false;
  2441. list_add_tail(&query_val.head, &validate_list);
  2442. ret = ttm_eu_reserve_buffers(&ticket, &validate_list,
  2443. false, NULL);
  2444. if (unlikely(ret != 0)) {
  2445. vmw_execbuf_unpin_panic(dev_priv);
  2446. goto out_no_reserve;
  2447. }
  2448. if (dev_priv->query_cid_valid) {
  2449. BUG_ON(fence != NULL);
  2450. ret = vmw_fifo_emit_dummy_query(dev_priv, dev_priv->query_cid);
  2451. if (unlikely(ret != 0)) {
  2452. vmw_execbuf_unpin_panic(dev_priv);
  2453. goto out_no_emit;
  2454. }
  2455. dev_priv->query_cid_valid = false;
  2456. }
  2457. vmw_bo_pin_reserved(dev_priv->pinned_bo, false);
  2458. if (dev_priv->dummy_query_bo_pinned) {
  2459. vmw_bo_pin_reserved(dev_priv->dummy_query_bo, false);
  2460. dev_priv->dummy_query_bo_pinned = false;
  2461. }
  2462. if (fence == NULL) {
  2463. (void) vmw_execbuf_fence_commands(NULL, dev_priv, &lfence,
  2464. NULL);
  2465. fence = lfence;
  2466. }
  2467. ttm_eu_fence_buffer_objects(&ticket, &validate_list, (void *) fence);
  2468. if (lfence != NULL)
  2469. vmw_fence_obj_unreference(&lfence);
  2470. ttm_bo_unref(&query_val.bo);
  2471. ttm_bo_unref(&pinned_val.bo);
  2472. vmw_dmabuf_unreference(&dev_priv->pinned_bo);
  2473. DRM_INFO("Dummy query bo pin count: %d\n",
  2474. dev_priv->dummy_query_bo->pin_count);
  2475. out_unlock:
  2476. return;
  2477. out_no_emit:
  2478. ttm_eu_backoff_reservation(&ticket, &validate_list);
  2479. out_no_reserve:
  2480. ttm_bo_unref(&query_val.bo);
  2481. ttm_bo_unref(&pinned_val.bo);
  2482. vmw_dmabuf_unreference(&dev_priv->pinned_bo);
  2483. }
  2484. /**
  2485. * vmw_execbuf_release_pinned_bo - Flush queries and unpin the pinned
  2486. * query bo.
  2487. *
  2488. * @dev_priv: The device private structure.
  2489. *
  2490. * This function should be used to unpin the pinned query bo, or
  2491. * as a query barrier when we need to make sure that all queries have
  2492. * finished before the next fifo command. (For example on hardware
  2493. * context destructions where the hardware may otherwise leak unfinished
  2494. * queries).
  2495. *
  2496. * This function does not return any failure codes, but make attempts
  2497. * to do safe unpinning in case of errors.
  2498. *
  2499. * The function will synchronize on the previous query barrier, and will
  2500. * thus not finish until that barrier has executed.
  2501. */
  2502. void vmw_execbuf_release_pinned_bo(struct vmw_private *dev_priv)
  2503. {
  2504. mutex_lock(&dev_priv->cmdbuf_mutex);
  2505. if (dev_priv->query_cid_valid)
  2506. __vmw_execbuf_release_pinned_bo(dev_priv, NULL);
  2507. mutex_unlock(&dev_priv->cmdbuf_mutex);
  2508. }
  2509. int vmw_execbuf_ioctl(struct drm_device *dev, void *data,
  2510. struct drm_file *file_priv)
  2511. {
  2512. struct vmw_private *dev_priv = vmw_priv(dev);
  2513. struct drm_vmw_execbuf_arg *arg = (struct drm_vmw_execbuf_arg *)data;
  2514. int ret;
  2515. /*
  2516. * This will allow us to extend the ioctl argument while
  2517. * maintaining backwards compatibility:
  2518. * We take different code paths depending on the value of
  2519. * arg->version.
  2520. */
  2521. if (unlikely(arg->version != DRM_VMW_EXECBUF_VERSION)) {
  2522. DRM_ERROR("Incorrect execbuf version.\n");
  2523. DRM_ERROR("You're running outdated experimental "
  2524. "vmwgfx user-space drivers.");
  2525. return -EINVAL;
  2526. }
  2527. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  2528. if (unlikely(ret != 0))
  2529. return ret;
  2530. ret = vmw_execbuf_process(file_priv, dev_priv,
  2531. (void __user *)(unsigned long)arg->commands,
  2532. NULL, arg->command_size, arg->throttle_us,
  2533. (void __user *)(unsigned long)arg->fence_rep,
  2534. NULL);
  2535. ttm_read_unlock(&dev_priv->reservation_sem);
  2536. if (unlikely(ret != 0))
  2537. return ret;
  2538. vmw_kms_cursor_post_execbuf(dev_priv);
  2539. return 0;
  2540. }