vmwgfx_execbuf.c 79 KB

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