i915_gem_context.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729
  1. /*
  2. * SPDX-License-Identifier: MIT
  3. *
  4. * Copyright © 2011-2012 Intel Corporation
  5. */
  6. /*
  7. * This file implements HW context support. On gen5+ a HW context consists of an
  8. * opaque GPU object which is referenced at times of context saves and restores.
  9. * With RC6 enabled, the context is also referenced as the GPU enters and exists
  10. * from RC6 (GPU has it's own internal power context, except on gen5). Though
  11. * something like a context does exist for the media ring, the code only
  12. * supports contexts for the render ring.
  13. *
  14. * In software, there is a distinction between contexts created by the user,
  15. * and the default HW context. The default HW context is used by GPU clients
  16. * that do not request setup of their own hardware context. The default
  17. * context's state is never restored to help prevent programming errors. This
  18. * would happen if a client ran and piggy-backed off another clients GPU state.
  19. * The default context only exists to give the GPU some offset to load as the
  20. * current to invoke a save of the context we actually care about. In fact, the
  21. * code could likely be constructed, albeit in a more complicated fashion, to
  22. * never use the default context, though that limits the driver's ability to
  23. * swap out, and/or destroy other contexts.
  24. *
  25. * All other contexts are created as a request by the GPU client. These contexts
  26. * store GPU state, and thus allow GPU clients to not re-emit state (and
  27. * potentially query certain state) at any time. The kernel driver makes
  28. * certain that the appropriate commands are inserted.
  29. *
  30. * The context life cycle is semi-complicated in that context BOs may live
  31. * longer than the context itself because of the way the hardware, and object
  32. * tracking works. Below is a very crude representation of the state machine
  33. * describing the context life.
  34. * refcount pincount active
  35. * S0: initial state 0 0 0
  36. * S1: context created 1 0 0
  37. * S2: context is currently running 2 1 X
  38. * S3: GPU referenced, but not current 2 0 1
  39. * S4: context is current, but destroyed 1 1 0
  40. * S5: like S3, but destroyed 1 0 1
  41. *
  42. * The most common (but not all) transitions:
  43. * S0->S1: client creates a context
  44. * S1->S2: client submits execbuf with context
  45. * S2->S3: other clients submits execbuf with context
  46. * S3->S1: context object was retired
  47. * S3->S2: clients submits another execbuf
  48. * S2->S4: context destroy called with current context
  49. * S3->S5->S0: destroy path
  50. * S4->S5->S0: destroy path on current context
  51. *
  52. * There are two confusing terms used above:
  53. * The "current context" means the context which is currently running on the
  54. * GPU. The GPU has loaded its state already and has stored away the gtt
  55. * offset of the BO. The GPU is not actively referencing the data at this
  56. * offset, but it will on the next context switch. The only way to avoid this
  57. * is to do a GPU reset.
  58. *
  59. * An "active context' is one which was previously the "current context" and is
  60. * on the active list waiting for the next context switch to occur. Until this
  61. * happens, the object must remain at the same gtt offset. It is therefore
  62. * possible to destroy a context, but it is still active.
  63. *
  64. */
  65. #include <linux/highmem.h>
  66. #include <linux/log2.h>
  67. #include <linux/nospec.h>
  68. #include <drm/drm_cache.h>
  69. #include <drm/drm_syncobj.h>
  70. #include "gt/gen6_ppgtt.h"
  71. #include "gt/intel_context.h"
  72. #include "gt/intel_context_param.h"
  73. #include "gt/intel_engine_heartbeat.h"
  74. #include "gt/intel_engine_user.h"
  75. #include "gt/intel_gpu_commands.h"
  76. #include "gt/intel_ring.h"
  77. #include "gt/shmem_utils.h"
  78. #include "pxp/intel_pxp.h"
  79. #include "i915_file_private.h"
  80. #include "i915_gem_context.h"
  81. #include "i915_trace.h"
  82. #include "i915_user_extensions.h"
  83. #define ALL_L3_SLICES(dev) (1 << NUM_L3_SLICES(dev)) - 1
  84. static struct kmem_cache *slab_luts;
  85. struct i915_lut_handle *i915_lut_handle_alloc(void)
  86. {
  87. return kmem_cache_alloc(slab_luts, GFP_KERNEL);
  88. }
  89. void i915_lut_handle_free(struct i915_lut_handle *lut)
  90. {
  91. return kmem_cache_free(slab_luts, lut);
  92. }
  93. static void lut_close(struct i915_gem_context *ctx)
  94. {
  95. struct radix_tree_iter iter;
  96. void __rcu **slot;
  97. mutex_lock(&ctx->lut_mutex);
  98. rcu_read_lock();
  99. radix_tree_for_each_slot(slot, &ctx->handles_vma, &iter, 0) {
  100. struct i915_vma *vma = rcu_dereference_raw(*slot);
  101. struct drm_i915_gem_object *obj = vma->obj;
  102. struct i915_lut_handle *lut;
  103. if (!kref_get_unless_zero(&obj->base.refcount))
  104. continue;
  105. spin_lock(&obj->lut_lock);
  106. list_for_each_entry(lut, &obj->lut_list, obj_link) {
  107. if (lut->ctx != ctx)
  108. continue;
  109. if (lut->handle != iter.index)
  110. continue;
  111. list_del(&lut->obj_link);
  112. break;
  113. }
  114. spin_unlock(&obj->lut_lock);
  115. if (&lut->obj_link != &obj->lut_list) {
  116. i915_lut_handle_free(lut);
  117. radix_tree_iter_delete(&ctx->handles_vma, &iter, slot);
  118. i915_vma_close(vma);
  119. i915_gem_object_put(obj);
  120. }
  121. i915_gem_object_put(obj);
  122. }
  123. rcu_read_unlock();
  124. mutex_unlock(&ctx->lut_mutex);
  125. }
  126. static struct intel_context *
  127. lookup_user_engine(struct i915_gem_context *ctx,
  128. unsigned long flags,
  129. const struct i915_engine_class_instance *ci)
  130. #define LOOKUP_USER_INDEX BIT(0)
  131. {
  132. int idx;
  133. if (!!(flags & LOOKUP_USER_INDEX) != i915_gem_context_user_engines(ctx))
  134. return ERR_PTR(-EINVAL);
  135. if (!i915_gem_context_user_engines(ctx)) {
  136. struct intel_engine_cs *engine;
  137. engine = intel_engine_lookup_user(ctx->i915,
  138. ci->engine_class,
  139. ci->engine_instance);
  140. if (!engine)
  141. return ERR_PTR(-EINVAL);
  142. idx = engine->legacy_idx;
  143. } else {
  144. idx = ci->engine_instance;
  145. }
  146. return i915_gem_context_get_engine(ctx, idx);
  147. }
  148. static int validate_priority(struct drm_i915_private *i915,
  149. const struct drm_i915_gem_context_param *args)
  150. {
  151. s64 priority = args->value;
  152. if (args->size)
  153. return -EINVAL;
  154. if (!(i915->caps.scheduler & I915_SCHEDULER_CAP_PRIORITY))
  155. return -ENODEV;
  156. if (priority > I915_CONTEXT_MAX_USER_PRIORITY ||
  157. priority < I915_CONTEXT_MIN_USER_PRIORITY)
  158. return -EINVAL;
  159. if (priority > I915_CONTEXT_DEFAULT_PRIORITY &&
  160. !capable(CAP_SYS_NICE))
  161. return -EPERM;
  162. return 0;
  163. }
  164. static void proto_context_close(struct drm_i915_private *i915,
  165. struct i915_gem_proto_context *pc)
  166. {
  167. int i;
  168. if (pc->pxp_wakeref)
  169. intel_runtime_pm_put(&i915->runtime_pm, pc->pxp_wakeref);
  170. if (pc->vm)
  171. i915_vm_put(pc->vm);
  172. if (pc->user_engines) {
  173. for (i = 0; i < pc->num_user_engines; i++)
  174. kfree(pc->user_engines[i].siblings);
  175. kfree(pc->user_engines);
  176. }
  177. kfree(pc);
  178. }
  179. static int proto_context_set_persistence(struct drm_i915_private *i915,
  180. struct i915_gem_proto_context *pc,
  181. bool persist)
  182. {
  183. if (persist) {
  184. /*
  185. * Only contexts that are short-lived [that will expire or be
  186. * reset] are allowed to survive past termination. We require
  187. * hangcheck to ensure that the persistent requests are healthy.
  188. */
  189. if (!i915->params.enable_hangcheck)
  190. return -EINVAL;
  191. pc->user_flags |= BIT(UCONTEXT_PERSISTENCE);
  192. } else {
  193. /* To cancel a context we use "preempt-to-idle" */
  194. if (!(i915->caps.scheduler & I915_SCHEDULER_CAP_PREEMPTION))
  195. return -ENODEV;
  196. /*
  197. * If the cancel fails, we then need to reset, cleanly!
  198. *
  199. * If the per-engine reset fails, all hope is lost! We resort
  200. * to a full GPU reset in that unlikely case, but realistically
  201. * if the engine could not reset, the full reset does not fare
  202. * much better. The damage has been done.
  203. *
  204. * However, if we cannot reset an engine by itself, we cannot
  205. * cleanup a hanging persistent context without causing
  206. * colateral damage, and we should not pretend we can by
  207. * exposing the interface.
  208. */
  209. if (!intel_has_reset_engine(to_gt(i915)))
  210. return -ENODEV;
  211. pc->user_flags &= ~BIT(UCONTEXT_PERSISTENCE);
  212. }
  213. return 0;
  214. }
  215. static int proto_context_set_protected(struct drm_i915_private *i915,
  216. struct i915_gem_proto_context *pc,
  217. bool protected)
  218. {
  219. int ret = 0;
  220. if (!protected) {
  221. pc->uses_protected_content = false;
  222. } else if (!intel_pxp_is_enabled(i915->pxp)) {
  223. ret = -ENODEV;
  224. } else if ((pc->user_flags & BIT(UCONTEXT_RECOVERABLE)) ||
  225. !(pc->user_flags & BIT(UCONTEXT_BANNABLE))) {
  226. ret = -EPERM;
  227. } else {
  228. pc->uses_protected_content = true;
  229. /*
  230. * protected context usage requires the PXP session to be up,
  231. * which in turn requires the device to be active.
  232. */
  233. pc->pxp_wakeref = intel_runtime_pm_get(&i915->runtime_pm);
  234. if (!intel_pxp_is_active(i915->pxp))
  235. ret = intel_pxp_start(i915->pxp);
  236. }
  237. return ret;
  238. }
  239. static struct i915_gem_proto_context *
  240. proto_context_create(struct drm_i915_file_private *fpriv,
  241. struct drm_i915_private *i915, unsigned int flags)
  242. {
  243. struct i915_gem_proto_context *pc, *err;
  244. pc = kzalloc(sizeof(*pc), GFP_KERNEL);
  245. if (!pc)
  246. return ERR_PTR(-ENOMEM);
  247. pc->fpriv = fpriv;
  248. pc->num_user_engines = -1;
  249. pc->user_engines = NULL;
  250. pc->user_flags = BIT(UCONTEXT_BANNABLE) |
  251. BIT(UCONTEXT_RECOVERABLE);
  252. if (i915->params.enable_hangcheck)
  253. pc->user_flags |= BIT(UCONTEXT_PERSISTENCE);
  254. pc->sched.priority = I915_PRIORITY_NORMAL;
  255. if (flags & I915_CONTEXT_CREATE_FLAGS_SINGLE_TIMELINE) {
  256. if (!HAS_EXECLISTS(i915)) {
  257. err = ERR_PTR(-EINVAL);
  258. goto proto_close;
  259. }
  260. pc->single_timeline = true;
  261. }
  262. return pc;
  263. proto_close:
  264. proto_context_close(i915, pc);
  265. return err;
  266. }
  267. static int proto_context_register_locked(struct drm_i915_file_private *fpriv,
  268. struct i915_gem_proto_context *pc,
  269. u32 *id)
  270. {
  271. int ret;
  272. void *old;
  273. lockdep_assert_held(&fpriv->proto_context_lock);
  274. ret = xa_alloc(&fpriv->context_xa, id, NULL, xa_limit_32b, GFP_KERNEL);
  275. if (ret)
  276. return ret;
  277. old = xa_store(&fpriv->proto_context_xa, *id, pc, GFP_KERNEL);
  278. if (xa_is_err(old)) {
  279. xa_erase(&fpriv->context_xa, *id);
  280. return xa_err(old);
  281. }
  282. WARN_ON(old);
  283. return 0;
  284. }
  285. static int proto_context_register(struct drm_i915_file_private *fpriv,
  286. struct i915_gem_proto_context *pc,
  287. u32 *id)
  288. {
  289. int ret;
  290. mutex_lock(&fpriv->proto_context_lock);
  291. ret = proto_context_register_locked(fpriv, pc, id);
  292. mutex_unlock(&fpriv->proto_context_lock);
  293. return ret;
  294. }
  295. static struct i915_address_space *
  296. i915_gem_vm_lookup(struct drm_i915_file_private *file_priv, u32 id)
  297. {
  298. struct i915_address_space *vm;
  299. xa_lock(&file_priv->vm_xa);
  300. vm = xa_load(&file_priv->vm_xa, id);
  301. if (vm)
  302. kref_get(&vm->ref);
  303. xa_unlock(&file_priv->vm_xa);
  304. return vm;
  305. }
  306. static int set_proto_ctx_vm(struct drm_i915_file_private *fpriv,
  307. struct i915_gem_proto_context *pc,
  308. const struct drm_i915_gem_context_param *args)
  309. {
  310. struct drm_i915_private *i915 = fpriv->i915;
  311. struct i915_address_space *vm;
  312. if (args->size)
  313. return -EINVAL;
  314. if (!HAS_FULL_PPGTT(i915))
  315. return -ENODEV;
  316. if (upper_32_bits(args->value))
  317. return -ENOENT;
  318. vm = i915_gem_vm_lookup(fpriv, args->value);
  319. if (!vm)
  320. return -ENOENT;
  321. if (pc->vm)
  322. i915_vm_put(pc->vm);
  323. pc->vm = vm;
  324. return 0;
  325. }
  326. struct set_proto_ctx_engines {
  327. struct drm_i915_private *i915;
  328. unsigned num_engines;
  329. struct i915_gem_proto_engine *engines;
  330. };
  331. static int
  332. set_proto_ctx_engines_balance(struct i915_user_extension __user *base,
  333. void *data)
  334. {
  335. struct i915_context_engines_load_balance __user *ext =
  336. container_of_user(base, typeof(*ext), base);
  337. const struct set_proto_ctx_engines *set = data;
  338. struct drm_i915_private *i915 = set->i915;
  339. struct intel_engine_cs **siblings;
  340. u16 num_siblings, idx;
  341. unsigned int n;
  342. int err;
  343. if (!HAS_EXECLISTS(i915))
  344. return -ENODEV;
  345. if (get_user(idx, &ext->engine_index))
  346. return -EFAULT;
  347. if (idx >= set->num_engines) {
  348. drm_dbg(&i915->drm, "Invalid placement value, %d >= %d\n",
  349. idx, set->num_engines);
  350. return -EINVAL;
  351. }
  352. idx = array_index_nospec(idx, set->num_engines);
  353. if (set->engines[idx].type != I915_GEM_ENGINE_TYPE_INVALID) {
  354. drm_dbg(&i915->drm,
  355. "Invalid placement[%d], already occupied\n", idx);
  356. return -EEXIST;
  357. }
  358. if (get_user(num_siblings, &ext->num_siblings))
  359. return -EFAULT;
  360. err = check_user_mbz(&ext->flags);
  361. if (err)
  362. return err;
  363. err = check_user_mbz(&ext->mbz64);
  364. if (err)
  365. return err;
  366. if (num_siblings == 0)
  367. return 0;
  368. siblings = kmalloc_array(num_siblings, sizeof(*siblings), GFP_KERNEL);
  369. if (!siblings)
  370. return -ENOMEM;
  371. for (n = 0; n < num_siblings; n++) {
  372. struct i915_engine_class_instance ci;
  373. if (copy_from_user(&ci, &ext->engines[n], sizeof(ci))) {
  374. err = -EFAULT;
  375. goto err_siblings;
  376. }
  377. siblings[n] = intel_engine_lookup_user(i915,
  378. ci.engine_class,
  379. ci.engine_instance);
  380. if (!siblings[n]) {
  381. drm_dbg(&i915->drm,
  382. "Invalid sibling[%d]: { class:%d, inst:%d }\n",
  383. n, ci.engine_class, ci.engine_instance);
  384. err = -EINVAL;
  385. goto err_siblings;
  386. }
  387. }
  388. if (num_siblings == 1) {
  389. set->engines[idx].type = I915_GEM_ENGINE_TYPE_PHYSICAL;
  390. set->engines[idx].engine = siblings[0];
  391. kfree(siblings);
  392. } else {
  393. set->engines[idx].type = I915_GEM_ENGINE_TYPE_BALANCED;
  394. set->engines[idx].num_siblings = num_siblings;
  395. set->engines[idx].siblings = siblings;
  396. }
  397. return 0;
  398. err_siblings:
  399. kfree(siblings);
  400. return err;
  401. }
  402. static int
  403. set_proto_ctx_engines_bond(struct i915_user_extension __user *base, void *data)
  404. {
  405. struct i915_context_engines_bond __user *ext =
  406. container_of_user(base, typeof(*ext), base);
  407. const struct set_proto_ctx_engines *set = data;
  408. struct drm_i915_private *i915 = set->i915;
  409. struct i915_engine_class_instance ci;
  410. struct intel_engine_cs *master;
  411. u16 idx, num_bonds;
  412. int err, n;
  413. if (GRAPHICS_VER(i915) >= 12 && !IS_TIGERLAKE(i915) &&
  414. !IS_ROCKETLAKE(i915) && !IS_ALDERLAKE_S(i915)) {
  415. drm_dbg(&i915->drm,
  416. "Bonding not supported on this platform\n");
  417. return -ENODEV;
  418. }
  419. if (get_user(idx, &ext->virtual_index))
  420. return -EFAULT;
  421. if (idx >= set->num_engines) {
  422. drm_dbg(&i915->drm,
  423. "Invalid index for virtual engine: %d >= %d\n",
  424. idx, set->num_engines);
  425. return -EINVAL;
  426. }
  427. idx = array_index_nospec(idx, set->num_engines);
  428. if (set->engines[idx].type == I915_GEM_ENGINE_TYPE_INVALID) {
  429. drm_dbg(&i915->drm, "Invalid engine at %d\n", idx);
  430. return -EINVAL;
  431. }
  432. if (set->engines[idx].type != I915_GEM_ENGINE_TYPE_PHYSICAL) {
  433. drm_dbg(&i915->drm,
  434. "Bonding with virtual engines not allowed\n");
  435. return -EINVAL;
  436. }
  437. err = check_user_mbz(&ext->flags);
  438. if (err)
  439. return err;
  440. for (n = 0; n < ARRAY_SIZE(ext->mbz64); n++) {
  441. err = check_user_mbz(&ext->mbz64[n]);
  442. if (err)
  443. return err;
  444. }
  445. if (copy_from_user(&ci, &ext->master, sizeof(ci)))
  446. return -EFAULT;
  447. master = intel_engine_lookup_user(i915,
  448. ci.engine_class,
  449. ci.engine_instance);
  450. if (!master) {
  451. drm_dbg(&i915->drm,
  452. "Unrecognised master engine: { class:%u, instance:%u }\n",
  453. ci.engine_class, ci.engine_instance);
  454. return -EINVAL;
  455. }
  456. if (intel_engine_uses_guc(master)) {
  457. drm_dbg(&i915->drm, "bonding extension not supported with GuC submission");
  458. return -ENODEV;
  459. }
  460. if (get_user(num_bonds, &ext->num_bonds))
  461. return -EFAULT;
  462. for (n = 0; n < num_bonds; n++) {
  463. struct intel_engine_cs *bond;
  464. if (copy_from_user(&ci, &ext->engines[n], sizeof(ci)))
  465. return -EFAULT;
  466. bond = intel_engine_lookup_user(i915,
  467. ci.engine_class,
  468. ci.engine_instance);
  469. if (!bond) {
  470. drm_dbg(&i915->drm,
  471. "Unrecognised engine[%d] for bonding: { class:%d, instance: %d }\n",
  472. n, ci.engine_class, ci.engine_instance);
  473. return -EINVAL;
  474. }
  475. }
  476. return 0;
  477. }
  478. static int
  479. set_proto_ctx_engines_parallel_submit(struct i915_user_extension __user *base,
  480. void *data)
  481. {
  482. struct i915_context_engines_parallel_submit __user *ext =
  483. container_of_user(base, typeof(*ext), base);
  484. const struct set_proto_ctx_engines *set = data;
  485. struct drm_i915_private *i915 = set->i915;
  486. struct i915_engine_class_instance prev_engine;
  487. u64 flags;
  488. int err = 0, n, i, j;
  489. u16 slot, width, num_siblings;
  490. struct intel_engine_cs **siblings = NULL;
  491. intel_engine_mask_t prev_mask;
  492. if (get_user(slot, &ext->engine_index))
  493. return -EFAULT;
  494. if (get_user(width, &ext->width))
  495. return -EFAULT;
  496. if (get_user(num_siblings, &ext->num_siblings))
  497. return -EFAULT;
  498. if (!intel_uc_uses_guc_submission(&to_gt(i915)->uc) &&
  499. num_siblings != 1) {
  500. drm_dbg(&i915->drm, "Only 1 sibling (%d) supported in non-GuC mode\n",
  501. num_siblings);
  502. return -EINVAL;
  503. }
  504. if (slot >= set->num_engines) {
  505. drm_dbg(&i915->drm, "Invalid placement value, %d >= %d\n",
  506. slot, set->num_engines);
  507. return -EINVAL;
  508. }
  509. if (set->engines[slot].type != I915_GEM_ENGINE_TYPE_INVALID) {
  510. drm_dbg(&i915->drm,
  511. "Invalid placement[%d], already occupied\n", slot);
  512. return -EINVAL;
  513. }
  514. if (get_user(flags, &ext->flags))
  515. return -EFAULT;
  516. if (flags) {
  517. drm_dbg(&i915->drm, "Unknown flags 0x%02llx", flags);
  518. return -EINVAL;
  519. }
  520. for (n = 0; n < ARRAY_SIZE(ext->mbz64); n++) {
  521. err = check_user_mbz(&ext->mbz64[n]);
  522. if (err)
  523. return err;
  524. }
  525. if (width < 2) {
  526. drm_dbg(&i915->drm, "Width (%d) < 2\n", width);
  527. return -EINVAL;
  528. }
  529. if (num_siblings < 1) {
  530. drm_dbg(&i915->drm, "Number siblings (%d) < 1\n",
  531. num_siblings);
  532. return -EINVAL;
  533. }
  534. siblings = kmalloc_array(num_siblings * width,
  535. sizeof(*siblings),
  536. GFP_KERNEL);
  537. if (!siblings)
  538. return -ENOMEM;
  539. /* Create contexts / engines */
  540. for (i = 0; i < width; ++i) {
  541. intel_engine_mask_t current_mask = 0;
  542. for (j = 0; j < num_siblings; ++j) {
  543. struct i915_engine_class_instance ci;
  544. n = i * num_siblings + j;
  545. if (copy_from_user(&ci, &ext->engines[n], sizeof(ci))) {
  546. err = -EFAULT;
  547. goto out_err;
  548. }
  549. siblings[n] =
  550. intel_engine_lookup_user(i915, ci.engine_class,
  551. ci.engine_instance);
  552. if (!siblings[n]) {
  553. drm_dbg(&i915->drm,
  554. "Invalid sibling[%d]: { class:%d, inst:%d }\n",
  555. n, ci.engine_class, ci.engine_instance);
  556. err = -EINVAL;
  557. goto out_err;
  558. }
  559. /*
  560. * We don't support breadcrumb handshake on these
  561. * classes
  562. */
  563. if (siblings[n]->class == RENDER_CLASS ||
  564. siblings[n]->class == COMPUTE_CLASS) {
  565. err = -EINVAL;
  566. goto out_err;
  567. }
  568. if (n) {
  569. if (prev_engine.engine_class !=
  570. ci.engine_class) {
  571. drm_dbg(&i915->drm,
  572. "Mismatched class %d, %d\n",
  573. prev_engine.engine_class,
  574. ci.engine_class);
  575. err = -EINVAL;
  576. goto out_err;
  577. }
  578. }
  579. prev_engine = ci;
  580. current_mask |= siblings[n]->logical_mask;
  581. }
  582. if (i > 0) {
  583. if (current_mask != prev_mask << 1) {
  584. drm_dbg(&i915->drm,
  585. "Non contiguous logical mask 0x%x, 0x%x\n",
  586. prev_mask, current_mask);
  587. err = -EINVAL;
  588. goto out_err;
  589. }
  590. }
  591. prev_mask = current_mask;
  592. }
  593. set->engines[slot].type = I915_GEM_ENGINE_TYPE_PARALLEL;
  594. set->engines[slot].num_siblings = num_siblings;
  595. set->engines[slot].width = width;
  596. set->engines[slot].siblings = siblings;
  597. return 0;
  598. out_err:
  599. kfree(siblings);
  600. return err;
  601. }
  602. static const i915_user_extension_fn set_proto_ctx_engines_extensions[] = {
  603. [I915_CONTEXT_ENGINES_EXT_LOAD_BALANCE] = set_proto_ctx_engines_balance,
  604. [I915_CONTEXT_ENGINES_EXT_BOND] = set_proto_ctx_engines_bond,
  605. [I915_CONTEXT_ENGINES_EXT_PARALLEL_SUBMIT] =
  606. set_proto_ctx_engines_parallel_submit,
  607. };
  608. static int set_proto_ctx_engines(struct drm_i915_file_private *fpriv,
  609. struct i915_gem_proto_context *pc,
  610. const struct drm_i915_gem_context_param *args)
  611. {
  612. struct drm_i915_private *i915 = fpriv->i915;
  613. struct set_proto_ctx_engines set = { .i915 = i915 };
  614. struct i915_context_param_engines __user *user =
  615. u64_to_user_ptr(args->value);
  616. unsigned int n;
  617. u64 extensions;
  618. int err;
  619. if (pc->num_user_engines >= 0) {
  620. drm_dbg(&i915->drm, "Cannot set engines twice");
  621. return -EINVAL;
  622. }
  623. if (args->size < sizeof(*user) ||
  624. !IS_ALIGNED(args->size - sizeof(*user), sizeof(*user->engines))) {
  625. drm_dbg(&i915->drm, "Invalid size for engine array: %d\n",
  626. args->size);
  627. return -EINVAL;
  628. }
  629. set.num_engines = (args->size - sizeof(*user)) / sizeof(*user->engines);
  630. /* RING_MASK has no shift so we can use it directly here */
  631. if (set.num_engines > I915_EXEC_RING_MASK + 1)
  632. return -EINVAL;
  633. set.engines = kmalloc_array(set.num_engines, sizeof(*set.engines), GFP_KERNEL);
  634. if (!set.engines)
  635. return -ENOMEM;
  636. for (n = 0; n < set.num_engines; n++) {
  637. struct i915_engine_class_instance ci;
  638. struct intel_engine_cs *engine;
  639. if (copy_from_user(&ci, &user->engines[n], sizeof(ci))) {
  640. kfree(set.engines);
  641. return -EFAULT;
  642. }
  643. memset(&set.engines[n], 0, sizeof(set.engines[n]));
  644. if (ci.engine_class == (u16)I915_ENGINE_CLASS_INVALID &&
  645. ci.engine_instance == (u16)I915_ENGINE_CLASS_INVALID_NONE)
  646. continue;
  647. engine = intel_engine_lookup_user(i915,
  648. ci.engine_class,
  649. ci.engine_instance);
  650. if (!engine) {
  651. drm_dbg(&i915->drm,
  652. "Invalid engine[%d]: { class:%d, instance:%d }\n",
  653. n, ci.engine_class, ci.engine_instance);
  654. kfree(set.engines);
  655. return -ENOENT;
  656. }
  657. set.engines[n].type = I915_GEM_ENGINE_TYPE_PHYSICAL;
  658. set.engines[n].engine = engine;
  659. }
  660. err = -EFAULT;
  661. if (!get_user(extensions, &user->extensions))
  662. err = i915_user_extensions(u64_to_user_ptr(extensions),
  663. set_proto_ctx_engines_extensions,
  664. ARRAY_SIZE(set_proto_ctx_engines_extensions),
  665. &set);
  666. if (err) {
  667. kfree(set.engines);
  668. return err;
  669. }
  670. pc->num_user_engines = set.num_engines;
  671. pc->user_engines = set.engines;
  672. return 0;
  673. }
  674. static int set_proto_ctx_sseu(struct drm_i915_file_private *fpriv,
  675. struct i915_gem_proto_context *pc,
  676. struct drm_i915_gem_context_param *args)
  677. {
  678. struct drm_i915_private *i915 = fpriv->i915;
  679. struct drm_i915_gem_context_param_sseu user_sseu;
  680. struct intel_sseu *sseu;
  681. int ret;
  682. if (args->size < sizeof(user_sseu))
  683. return -EINVAL;
  684. if (GRAPHICS_VER(i915) != 11)
  685. return -ENODEV;
  686. if (copy_from_user(&user_sseu, u64_to_user_ptr(args->value),
  687. sizeof(user_sseu)))
  688. return -EFAULT;
  689. if (user_sseu.rsvd)
  690. return -EINVAL;
  691. if (user_sseu.flags & ~(I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX))
  692. return -EINVAL;
  693. if (!!(user_sseu.flags & I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX) != (pc->num_user_engines >= 0))
  694. return -EINVAL;
  695. if (pc->num_user_engines >= 0) {
  696. int idx = user_sseu.engine.engine_instance;
  697. struct i915_gem_proto_engine *pe;
  698. if (idx >= pc->num_user_engines)
  699. return -EINVAL;
  700. idx = array_index_nospec(idx, pc->num_user_engines);
  701. pe = &pc->user_engines[idx];
  702. /* Only render engine supports RPCS configuration. */
  703. if (pe->engine->class != RENDER_CLASS)
  704. return -EINVAL;
  705. sseu = &pe->sseu;
  706. } else {
  707. /* Only render engine supports RPCS configuration. */
  708. if (user_sseu.engine.engine_class != I915_ENGINE_CLASS_RENDER)
  709. return -EINVAL;
  710. /* There is only one render engine */
  711. if (user_sseu.engine.engine_instance != 0)
  712. return -EINVAL;
  713. sseu = &pc->legacy_rcs_sseu;
  714. }
  715. ret = i915_gem_user_to_context_sseu(to_gt(i915), &user_sseu, sseu);
  716. if (ret)
  717. return ret;
  718. args->size = sizeof(user_sseu);
  719. return 0;
  720. }
  721. static int set_proto_ctx_param(struct drm_i915_file_private *fpriv,
  722. struct i915_gem_proto_context *pc,
  723. struct drm_i915_gem_context_param *args)
  724. {
  725. struct drm_i915_private *i915 = fpriv->i915;
  726. int ret = 0;
  727. switch (args->param) {
  728. case I915_CONTEXT_PARAM_NO_ERROR_CAPTURE:
  729. if (args->size)
  730. ret = -EINVAL;
  731. else if (args->value)
  732. pc->user_flags |= BIT(UCONTEXT_NO_ERROR_CAPTURE);
  733. else
  734. pc->user_flags &= ~BIT(UCONTEXT_NO_ERROR_CAPTURE);
  735. break;
  736. case I915_CONTEXT_PARAM_BANNABLE:
  737. if (args->size)
  738. ret = -EINVAL;
  739. else if (!capable(CAP_SYS_ADMIN) && !args->value)
  740. ret = -EPERM;
  741. else if (args->value)
  742. pc->user_flags |= BIT(UCONTEXT_BANNABLE);
  743. else if (pc->uses_protected_content)
  744. ret = -EPERM;
  745. else
  746. pc->user_flags &= ~BIT(UCONTEXT_BANNABLE);
  747. break;
  748. case I915_CONTEXT_PARAM_LOW_LATENCY:
  749. if (intel_uc_uses_guc_submission(&to_gt(i915)->uc))
  750. pc->user_flags |= BIT(UCONTEXT_LOW_LATENCY);
  751. else
  752. ret = -EINVAL;
  753. break;
  754. case I915_CONTEXT_PARAM_RECOVERABLE:
  755. if (args->size)
  756. ret = -EINVAL;
  757. else if (!args->value)
  758. pc->user_flags &= ~BIT(UCONTEXT_RECOVERABLE);
  759. else if (pc->uses_protected_content)
  760. ret = -EPERM;
  761. else
  762. pc->user_flags |= BIT(UCONTEXT_RECOVERABLE);
  763. break;
  764. case I915_CONTEXT_PARAM_PRIORITY:
  765. ret = validate_priority(fpriv->i915, args);
  766. if (!ret)
  767. pc->sched.priority = args->value;
  768. break;
  769. case I915_CONTEXT_PARAM_SSEU:
  770. ret = set_proto_ctx_sseu(fpriv, pc, args);
  771. break;
  772. case I915_CONTEXT_PARAM_VM:
  773. ret = set_proto_ctx_vm(fpriv, pc, args);
  774. break;
  775. case I915_CONTEXT_PARAM_ENGINES:
  776. ret = set_proto_ctx_engines(fpriv, pc, args);
  777. break;
  778. case I915_CONTEXT_PARAM_PERSISTENCE:
  779. if (args->size)
  780. ret = -EINVAL;
  781. else
  782. ret = proto_context_set_persistence(fpriv->i915, pc,
  783. args->value);
  784. break;
  785. case I915_CONTEXT_PARAM_PROTECTED_CONTENT:
  786. ret = proto_context_set_protected(fpriv->i915, pc,
  787. args->value);
  788. break;
  789. case I915_CONTEXT_PARAM_NO_ZEROMAP:
  790. case I915_CONTEXT_PARAM_BAN_PERIOD:
  791. case I915_CONTEXT_PARAM_RINGSIZE:
  792. case I915_CONTEXT_PARAM_CONTEXT_IMAGE:
  793. default:
  794. ret = -EINVAL;
  795. break;
  796. }
  797. return ret;
  798. }
  799. static int intel_context_set_gem(struct intel_context *ce,
  800. struct i915_gem_context *ctx,
  801. struct intel_sseu sseu)
  802. {
  803. int ret = 0;
  804. GEM_BUG_ON(rcu_access_pointer(ce->gem_context));
  805. RCU_INIT_POINTER(ce->gem_context, ctx);
  806. GEM_BUG_ON(intel_context_is_pinned(ce));
  807. if (ce->engine->class == COMPUTE_CLASS)
  808. ce->ring_size = SZ_512K;
  809. else
  810. ce->ring_size = SZ_16K;
  811. i915_vm_put(ce->vm);
  812. ce->vm = i915_gem_context_get_eb_vm(ctx);
  813. if (ctx->sched.priority >= I915_PRIORITY_NORMAL &&
  814. intel_engine_has_timeslices(ce->engine) &&
  815. intel_engine_has_semaphores(ce->engine))
  816. __set_bit(CONTEXT_USE_SEMAPHORES, &ce->flags);
  817. if (CONFIG_DRM_I915_REQUEST_TIMEOUT &&
  818. ctx->i915->params.request_timeout_ms) {
  819. unsigned int timeout_ms = ctx->i915->params.request_timeout_ms;
  820. intel_context_set_watchdog_us(ce, (u64)timeout_ms * 1000);
  821. }
  822. /* A valid SSEU has no zero fields */
  823. if (sseu.slice_mask && !WARN_ON(ce->engine->class != RENDER_CLASS))
  824. ret = intel_context_reconfigure_sseu(ce, sseu);
  825. if (test_bit(UCONTEXT_LOW_LATENCY, &ctx->user_flags))
  826. __set_bit(CONTEXT_LOW_LATENCY, &ce->flags);
  827. return ret;
  828. }
  829. static void __unpin_engines(struct i915_gem_engines *e, unsigned int count)
  830. {
  831. while (count--) {
  832. struct intel_context *ce = e->engines[count], *child;
  833. if (!ce || !test_bit(CONTEXT_PERMA_PIN, &ce->flags))
  834. continue;
  835. for_each_child(ce, child)
  836. intel_context_unpin(child);
  837. intel_context_unpin(ce);
  838. }
  839. }
  840. static void unpin_engines(struct i915_gem_engines *e)
  841. {
  842. __unpin_engines(e, e->num_engines);
  843. }
  844. static void __free_engines(struct i915_gem_engines *e, unsigned int count)
  845. {
  846. while (count--) {
  847. if (!e->engines[count])
  848. continue;
  849. intel_context_put(e->engines[count]);
  850. }
  851. kfree(e);
  852. }
  853. static void free_engines(struct i915_gem_engines *e)
  854. {
  855. __free_engines(e, e->num_engines);
  856. }
  857. static void free_engines_rcu(struct rcu_head *rcu)
  858. {
  859. struct i915_gem_engines *engines =
  860. container_of(rcu, struct i915_gem_engines, rcu);
  861. i915_sw_fence_fini(&engines->fence);
  862. free_engines(engines);
  863. }
  864. static void accumulate_runtime(struct i915_drm_client *client,
  865. struct i915_gem_engines *engines)
  866. {
  867. struct i915_gem_engines_iter it;
  868. struct intel_context *ce;
  869. if (!client)
  870. return;
  871. /* Transfer accumulated runtime to the parent GEM context. */
  872. for_each_gem_engine(ce, engines, it) {
  873. unsigned int class = ce->engine->uabi_class;
  874. GEM_BUG_ON(class >= ARRAY_SIZE(client->past_runtime));
  875. atomic64_add(intel_context_get_total_runtime_ns(ce),
  876. &client->past_runtime[class]);
  877. }
  878. }
  879. static int
  880. engines_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
  881. {
  882. struct i915_gem_engines *engines =
  883. container_of(fence, typeof(*engines), fence);
  884. struct i915_gem_context *ctx = engines->ctx;
  885. switch (state) {
  886. case FENCE_COMPLETE:
  887. if (!list_empty(&engines->link)) {
  888. unsigned long flags;
  889. spin_lock_irqsave(&ctx->stale.lock, flags);
  890. list_del(&engines->link);
  891. spin_unlock_irqrestore(&ctx->stale.lock, flags);
  892. }
  893. accumulate_runtime(ctx->client, engines);
  894. i915_gem_context_put(ctx);
  895. break;
  896. case FENCE_FREE:
  897. init_rcu_head(&engines->rcu);
  898. call_rcu(&engines->rcu, free_engines_rcu);
  899. break;
  900. }
  901. return NOTIFY_DONE;
  902. }
  903. static struct i915_gem_engines *alloc_engines(unsigned int count)
  904. {
  905. struct i915_gem_engines *e;
  906. e = kzalloc(struct_size(e, engines, count), GFP_KERNEL);
  907. if (!e)
  908. return NULL;
  909. i915_sw_fence_init(&e->fence, engines_notify);
  910. return e;
  911. }
  912. static struct i915_gem_engines *default_engines(struct i915_gem_context *ctx,
  913. struct intel_sseu rcs_sseu)
  914. {
  915. const unsigned int max = I915_NUM_ENGINES;
  916. struct intel_engine_cs *engine;
  917. struct i915_gem_engines *e, *err;
  918. e = alloc_engines(max);
  919. if (!e)
  920. return ERR_PTR(-ENOMEM);
  921. for_each_uabi_engine(engine, ctx->i915) {
  922. struct intel_context *ce;
  923. struct intel_sseu sseu = {};
  924. int ret;
  925. if (engine->legacy_idx == INVALID_ENGINE)
  926. continue;
  927. GEM_BUG_ON(engine->legacy_idx >= max);
  928. GEM_BUG_ON(e->engines[engine->legacy_idx]);
  929. ce = intel_context_create(engine);
  930. if (IS_ERR(ce)) {
  931. err = ERR_CAST(ce);
  932. goto free_engines;
  933. }
  934. e->engines[engine->legacy_idx] = ce;
  935. e->num_engines = max(e->num_engines, engine->legacy_idx + 1);
  936. if (engine->class == RENDER_CLASS)
  937. sseu = rcs_sseu;
  938. ret = intel_context_set_gem(ce, ctx, sseu);
  939. if (ret) {
  940. err = ERR_PTR(ret);
  941. goto free_engines;
  942. }
  943. }
  944. return e;
  945. free_engines:
  946. free_engines(e);
  947. return err;
  948. }
  949. static int perma_pin_contexts(struct intel_context *ce)
  950. {
  951. struct intel_context *child;
  952. int i = 0, j = 0, ret;
  953. GEM_BUG_ON(!intel_context_is_parent(ce));
  954. ret = intel_context_pin(ce);
  955. if (unlikely(ret))
  956. return ret;
  957. for_each_child(ce, child) {
  958. ret = intel_context_pin(child);
  959. if (unlikely(ret))
  960. goto unwind;
  961. ++i;
  962. }
  963. set_bit(CONTEXT_PERMA_PIN, &ce->flags);
  964. return 0;
  965. unwind:
  966. intel_context_unpin(ce);
  967. for_each_child(ce, child) {
  968. if (j++ < i)
  969. intel_context_unpin(child);
  970. else
  971. break;
  972. }
  973. return ret;
  974. }
  975. static struct i915_gem_engines *user_engines(struct i915_gem_context *ctx,
  976. unsigned int num_engines,
  977. struct i915_gem_proto_engine *pe)
  978. {
  979. struct i915_gem_engines *e, *err;
  980. unsigned int n;
  981. e = alloc_engines(num_engines);
  982. if (!e)
  983. return ERR_PTR(-ENOMEM);
  984. e->num_engines = num_engines;
  985. for (n = 0; n < num_engines; n++) {
  986. struct intel_context *ce, *child;
  987. int ret;
  988. switch (pe[n].type) {
  989. case I915_GEM_ENGINE_TYPE_PHYSICAL:
  990. ce = intel_context_create(pe[n].engine);
  991. break;
  992. case I915_GEM_ENGINE_TYPE_BALANCED:
  993. ce = intel_engine_create_virtual(pe[n].siblings,
  994. pe[n].num_siblings, 0);
  995. break;
  996. case I915_GEM_ENGINE_TYPE_PARALLEL:
  997. ce = intel_engine_create_parallel(pe[n].siblings,
  998. pe[n].num_siblings,
  999. pe[n].width);
  1000. break;
  1001. case I915_GEM_ENGINE_TYPE_INVALID:
  1002. default:
  1003. GEM_WARN_ON(pe[n].type != I915_GEM_ENGINE_TYPE_INVALID);
  1004. continue;
  1005. }
  1006. if (IS_ERR(ce)) {
  1007. err = ERR_CAST(ce);
  1008. goto free_engines;
  1009. }
  1010. e->engines[n] = ce;
  1011. ret = intel_context_set_gem(ce, ctx, pe->sseu);
  1012. if (ret) {
  1013. err = ERR_PTR(ret);
  1014. goto free_engines;
  1015. }
  1016. for_each_child(ce, child) {
  1017. ret = intel_context_set_gem(child, ctx, pe->sseu);
  1018. if (ret) {
  1019. err = ERR_PTR(ret);
  1020. goto free_engines;
  1021. }
  1022. }
  1023. /*
  1024. * XXX: Must be done after calling intel_context_set_gem as that
  1025. * function changes the ring size. The ring is allocated when
  1026. * the context is pinned. If the ring size is changed after
  1027. * allocation we have a mismatch of the ring size and will cause
  1028. * the context to hang. Presumably with a bit of reordering we
  1029. * could move the perma-pin step to the backend function
  1030. * intel_engine_create_parallel.
  1031. */
  1032. if (pe[n].type == I915_GEM_ENGINE_TYPE_PARALLEL) {
  1033. ret = perma_pin_contexts(ce);
  1034. if (ret) {
  1035. err = ERR_PTR(ret);
  1036. goto free_engines;
  1037. }
  1038. }
  1039. }
  1040. return e;
  1041. free_engines:
  1042. free_engines(e);
  1043. return err;
  1044. }
  1045. static void i915_gem_context_release_work(struct work_struct *work)
  1046. {
  1047. struct i915_gem_context *ctx = container_of(work, typeof(*ctx),
  1048. release_work);
  1049. struct i915_address_space *vm;
  1050. trace_i915_context_free(ctx);
  1051. GEM_BUG_ON(!i915_gem_context_is_closed(ctx));
  1052. spin_lock(&ctx->i915->gem.contexts.lock);
  1053. list_del(&ctx->link);
  1054. spin_unlock(&ctx->i915->gem.contexts.lock);
  1055. if (ctx->syncobj)
  1056. drm_syncobj_put(ctx->syncobj);
  1057. vm = ctx->vm;
  1058. if (vm)
  1059. i915_vm_put(vm);
  1060. if (ctx->pxp_wakeref)
  1061. intel_runtime_pm_put(&ctx->i915->runtime_pm, ctx->pxp_wakeref);
  1062. if (ctx->client)
  1063. i915_drm_client_put(ctx->client);
  1064. mutex_destroy(&ctx->engines_mutex);
  1065. mutex_destroy(&ctx->lut_mutex);
  1066. put_pid(ctx->pid);
  1067. mutex_destroy(&ctx->mutex);
  1068. kfree_rcu(ctx, rcu);
  1069. }
  1070. void i915_gem_context_release(struct kref *ref)
  1071. {
  1072. struct i915_gem_context *ctx = container_of(ref, typeof(*ctx), ref);
  1073. queue_work(ctx->i915->wq, &ctx->release_work);
  1074. }
  1075. static inline struct i915_gem_engines *
  1076. __context_engines_static(const struct i915_gem_context *ctx)
  1077. {
  1078. return rcu_dereference_protected(ctx->engines, true);
  1079. }
  1080. static void __reset_context(struct i915_gem_context *ctx,
  1081. struct intel_engine_cs *engine)
  1082. {
  1083. intel_gt_handle_error(engine->gt, engine->mask, 0,
  1084. "context closure in %s", ctx->name);
  1085. }
  1086. static bool __cancel_engine(struct intel_engine_cs *engine)
  1087. {
  1088. /*
  1089. * Send a "high priority pulse" down the engine to cause the
  1090. * current request to be momentarily preempted. (If it fails to
  1091. * be preempted, it will be reset). As we have marked our context
  1092. * as banned, any incomplete request, including any running, will
  1093. * be skipped following the preemption.
  1094. *
  1095. * If there is no hangchecking (one of the reasons why we try to
  1096. * cancel the context) and no forced preemption, there may be no
  1097. * means by which we reset the GPU and evict the persistent hog.
  1098. * Ergo if we are unable to inject a preemptive pulse that can
  1099. * kill the banned context, we fallback to doing a local reset
  1100. * instead.
  1101. */
  1102. return intel_engine_pulse(engine) == 0;
  1103. }
  1104. static struct intel_engine_cs *active_engine(struct intel_context *ce)
  1105. {
  1106. struct intel_engine_cs *engine = NULL;
  1107. struct i915_request *rq;
  1108. if (intel_context_has_inflight(ce))
  1109. return intel_context_inflight(ce);
  1110. if (!ce->timeline)
  1111. return NULL;
  1112. /*
  1113. * rq->link is only SLAB_TYPESAFE_BY_RCU, we need to hold a reference
  1114. * to the request to prevent it being transferred to a new timeline
  1115. * (and onto a new timeline->requests list).
  1116. */
  1117. rcu_read_lock();
  1118. list_for_each_entry_reverse(rq, &ce->timeline->requests, link) {
  1119. bool found;
  1120. /* timeline is already completed upto this point? */
  1121. if (!i915_request_get_rcu(rq))
  1122. break;
  1123. /* Check with the backend if the request is inflight */
  1124. found = true;
  1125. if (likely(rcu_access_pointer(rq->timeline) == ce->timeline))
  1126. found = i915_request_active_engine(rq, &engine);
  1127. i915_request_put(rq);
  1128. if (found)
  1129. break;
  1130. }
  1131. rcu_read_unlock();
  1132. return engine;
  1133. }
  1134. static void
  1135. kill_engines(struct i915_gem_engines *engines, bool exit, bool persistent)
  1136. {
  1137. struct i915_gem_engines_iter it;
  1138. struct intel_context *ce;
  1139. /*
  1140. * Map the user's engine back to the actual engines; one virtual
  1141. * engine will be mapped to multiple engines, and using ctx->engine[]
  1142. * the same engine may be have multiple instances in the user's map.
  1143. * However, we only care about pending requests, so only include
  1144. * engines on which there are incomplete requests.
  1145. */
  1146. for_each_gem_engine(ce, engines, it) {
  1147. struct intel_engine_cs *engine;
  1148. if ((exit || !persistent) && intel_context_revoke(ce))
  1149. continue; /* Already marked. */
  1150. /*
  1151. * Check the current active state of this context; if we
  1152. * are currently executing on the GPU we need to evict
  1153. * ourselves. On the other hand, if we haven't yet been
  1154. * submitted to the GPU or if everything is complete,
  1155. * we have nothing to do.
  1156. */
  1157. engine = active_engine(ce);
  1158. /* First attempt to gracefully cancel the context */
  1159. if (engine && !__cancel_engine(engine) && (exit || !persistent))
  1160. /*
  1161. * If we are unable to send a preemptive pulse to bump
  1162. * the context from the GPU, we have to resort to a full
  1163. * reset. We hope the collateral damage is worth it.
  1164. */
  1165. __reset_context(engines->ctx, engine);
  1166. }
  1167. }
  1168. static void kill_context(struct i915_gem_context *ctx)
  1169. {
  1170. struct i915_gem_engines *pos, *next;
  1171. spin_lock_irq(&ctx->stale.lock);
  1172. GEM_BUG_ON(!i915_gem_context_is_closed(ctx));
  1173. list_for_each_entry_safe(pos, next, &ctx->stale.engines, link) {
  1174. if (!i915_sw_fence_await(&pos->fence)) {
  1175. list_del_init(&pos->link);
  1176. continue;
  1177. }
  1178. spin_unlock_irq(&ctx->stale.lock);
  1179. kill_engines(pos, !ctx->i915->params.enable_hangcheck,
  1180. i915_gem_context_is_persistent(ctx));
  1181. spin_lock_irq(&ctx->stale.lock);
  1182. GEM_BUG_ON(i915_sw_fence_signaled(&pos->fence));
  1183. list_safe_reset_next(pos, next, link);
  1184. list_del_init(&pos->link); /* decouple from FENCE_COMPLETE */
  1185. i915_sw_fence_complete(&pos->fence);
  1186. }
  1187. spin_unlock_irq(&ctx->stale.lock);
  1188. }
  1189. static void engines_idle_release(struct i915_gem_context *ctx,
  1190. struct i915_gem_engines *engines)
  1191. {
  1192. struct i915_gem_engines_iter it;
  1193. struct intel_context *ce;
  1194. INIT_LIST_HEAD(&engines->link);
  1195. engines->ctx = i915_gem_context_get(ctx);
  1196. for_each_gem_engine(ce, engines, it) {
  1197. int err;
  1198. /* serialises with execbuf */
  1199. intel_context_close(ce);
  1200. if (!intel_context_pin_if_active(ce))
  1201. continue;
  1202. /* Wait until context is finally scheduled out and retired */
  1203. err = i915_sw_fence_await_active(&engines->fence,
  1204. &ce->active,
  1205. I915_ACTIVE_AWAIT_BARRIER);
  1206. intel_context_unpin(ce);
  1207. if (err)
  1208. goto kill;
  1209. }
  1210. spin_lock_irq(&ctx->stale.lock);
  1211. if (!i915_gem_context_is_closed(ctx))
  1212. list_add_tail(&engines->link, &ctx->stale.engines);
  1213. spin_unlock_irq(&ctx->stale.lock);
  1214. kill:
  1215. if (list_empty(&engines->link)) /* raced, already closed */
  1216. kill_engines(engines, true,
  1217. i915_gem_context_is_persistent(ctx));
  1218. i915_sw_fence_commit(&engines->fence);
  1219. }
  1220. static void set_closed_name(struct i915_gem_context *ctx)
  1221. {
  1222. char *s;
  1223. /* Replace '[]' with '<>' to indicate closed in debug prints */
  1224. s = strrchr(ctx->name, '[');
  1225. if (!s)
  1226. return;
  1227. *s = '<';
  1228. s = strchr(s + 1, ']');
  1229. if (s)
  1230. *s = '>';
  1231. }
  1232. static void context_close(struct i915_gem_context *ctx)
  1233. {
  1234. struct i915_drm_client *client;
  1235. /* Flush any concurrent set_engines() */
  1236. mutex_lock(&ctx->engines_mutex);
  1237. unpin_engines(__context_engines_static(ctx));
  1238. engines_idle_release(ctx, rcu_replace_pointer(ctx->engines, NULL, 1));
  1239. i915_gem_context_set_closed(ctx);
  1240. mutex_unlock(&ctx->engines_mutex);
  1241. mutex_lock(&ctx->mutex);
  1242. set_closed_name(ctx);
  1243. /*
  1244. * The LUT uses the VMA as a backpointer to unref the object,
  1245. * so we need to clear the LUT before we close all the VMA (inside
  1246. * the ppgtt).
  1247. */
  1248. lut_close(ctx);
  1249. ctx->file_priv = ERR_PTR(-EBADF);
  1250. client = ctx->client;
  1251. if (client) {
  1252. spin_lock(&client->ctx_lock);
  1253. list_del_rcu(&ctx->client_link);
  1254. spin_unlock(&client->ctx_lock);
  1255. }
  1256. mutex_unlock(&ctx->mutex);
  1257. /*
  1258. * If the user has disabled hangchecking, we can not be sure that
  1259. * the batches will ever complete after the context is closed,
  1260. * keeping the context and all resources pinned forever. So in this
  1261. * case we opt to forcibly kill off all remaining requests on
  1262. * context close.
  1263. */
  1264. kill_context(ctx);
  1265. i915_gem_context_put(ctx);
  1266. }
  1267. static int __context_set_persistence(struct i915_gem_context *ctx, bool state)
  1268. {
  1269. if (i915_gem_context_is_persistent(ctx) == state)
  1270. return 0;
  1271. if (state) {
  1272. /*
  1273. * Only contexts that are short-lived [that will expire or be
  1274. * reset] are allowed to survive past termination. We require
  1275. * hangcheck to ensure that the persistent requests are healthy.
  1276. */
  1277. if (!ctx->i915->params.enable_hangcheck)
  1278. return -EINVAL;
  1279. i915_gem_context_set_persistence(ctx);
  1280. } else {
  1281. /* To cancel a context we use "preempt-to-idle" */
  1282. if (!(ctx->i915->caps.scheduler & I915_SCHEDULER_CAP_PREEMPTION))
  1283. return -ENODEV;
  1284. /*
  1285. * If the cancel fails, we then need to reset, cleanly!
  1286. *
  1287. * If the per-engine reset fails, all hope is lost! We resort
  1288. * to a full GPU reset in that unlikely case, but realistically
  1289. * if the engine could not reset, the full reset does not fare
  1290. * much better. The damage has been done.
  1291. *
  1292. * However, if we cannot reset an engine by itself, we cannot
  1293. * cleanup a hanging persistent context without causing
  1294. * colateral damage, and we should not pretend we can by
  1295. * exposing the interface.
  1296. */
  1297. if (!intel_has_reset_engine(to_gt(ctx->i915)))
  1298. return -ENODEV;
  1299. i915_gem_context_clear_persistence(ctx);
  1300. }
  1301. return 0;
  1302. }
  1303. static struct i915_gem_context *
  1304. i915_gem_create_context(struct drm_i915_private *i915,
  1305. const struct i915_gem_proto_context *pc)
  1306. {
  1307. struct i915_gem_context *ctx;
  1308. struct i915_address_space *vm = NULL;
  1309. struct i915_gem_engines *e;
  1310. int err;
  1311. int i;
  1312. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  1313. if (!ctx)
  1314. return ERR_PTR(-ENOMEM);
  1315. kref_init(&ctx->ref);
  1316. ctx->i915 = i915;
  1317. ctx->sched = pc->sched;
  1318. mutex_init(&ctx->mutex);
  1319. INIT_LIST_HEAD(&ctx->link);
  1320. INIT_WORK(&ctx->release_work, i915_gem_context_release_work);
  1321. spin_lock_init(&ctx->stale.lock);
  1322. INIT_LIST_HEAD(&ctx->stale.engines);
  1323. if (pc->vm) {
  1324. vm = i915_vm_get(pc->vm);
  1325. } else if (HAS_FULL_PPGTT(i915)) {
  1326. struct i915_ppgtt *ppgtt;
  1327. ppgtt = i915_ppgtt_create(to_gt(i915), 0);
  1328. if (IS_ERR(ppgtt)) {
  1329. drm_dbg(&i915->drm, "PPGTT setup failed (%ld)\n",
  1330. PTR_ERR(ppgtt));
  1331. err = PTR_ERR(ppgtt);
  1332. goto err_ctx;
  1333. }
  1334. ppgtt->vm.fpriv = pc->fpriv;
  1335. vm = &ppgtt->vm;
  1336. }
  1337. if (vm)
  1338. ctx->vm = vm;
  1339. /* Assign early so intel_context_set_gem can access these flags */
  1340. ctx->user_flags = pc->user_flags;
  1341. mutex_init(&ctx->engines_mutex);
  1342. if (pc->num_user_engines >= 0) {
  1343. i915_gem_context_set_user_engines(ctx);
  1344. e = user_engines(ctx, pc->num_user_engines, pc->user_engines);
  1345. } else {
  1346. i915_gem_context_clear_user_engines(ctx);
  1347. e = default_engines(ctx, pc->legacy_rcs_sseu);
  1348. }
  1349. if (IS_ERR(e)) {
  1350. err = PTR_ERR(e);
  1351. goto err_vm;
  1352. }
  1353. RCU_INIT_POINTER(ctx->engines, e);
  1354. INIT_RADIX_TREE(&ctx->handles_vma, GFP_KERNEL);
  1355. mutex_init(&ctx->lut_mutex);
  1356. /* NB: Mark all slices as needing a remap so that when the context first
  1357. * loads it will restore whatever remap state already exists. If there
  1358. * is no remap info, it will be a NOP. */
  1359. ctx->remap_slice = ALL_L3_SLICES(i915);
  1360. for (i = 0; i < ARRAY_SIZE(ctx->hang_timestamp); i++)
  1361. ctx->hang_timestamp[i] = jiffies - CONTEXT_FAST_HANG_JIFFIES;
  1362. if (pc->single_timeline) {
  1363. err = drm_syncobj_create(&ctx->syncobj,
  1364. DRM_SYNCOBJ_CREATE_SIGNALED,
  1365. NULL);
  1366. if (err)
  1367. goto err_engines;
  1368. }
  1369. if (pc->uses_protected_content) {
  1370. ctx->pxp_wakeref = intel_runtime_pm_get(&i915->runtime_pm);
  1371. ctx->uses_protected_content = true;
  1372. }
  1373. trace_i915_context_create(ctx);
  1374. return ctx;
  1375. err_engines:
  1376. free_engines(e);
  1377. err_vm:
  1378. if (ctx->vm)
  1379. i915_vm_put(ctx->vm);
  1380. err_ctx:
  1381. kfree(ctx);
  1382. return ERR_PTR(err);
  1383. }
  1384. static void init_contexts(struct i915_gem_contexts *gc)
  1385. {
  1386. spin_lock_init(&gc->lock);
  1387. INIT_LIST_HEAD(&gc->list);
  1388. }
  1389. void i915_gem_init__contexts(struct drm_i915_private *i915)
  1390. {
  1391. init_contexts(&i915->gem.contexts);
  1392. }
  1393. /*
  1394. * Note that this implicitly consumes the ctx reference, by placing
  1395. * the ctx in the context_xa.
  1396. */
  1397. static void gem_context_register(struct i915_gem_context *ctx,
  1398. struct drm_i915_file_private *fpriv,
  1399. u32 id)
  1400. {
  1401. struct drm_i915_private *i915 = ctx->i915;
  1402. void *old;
  1403. ctx->file_priv = fpriv;
  1404. ctx->pid = get_task_pid(current, PIDTYPE_PID);
  1405. ctx->client = i915_drm_client_get(fpriv->client);
  1406. snprintf(ctx->name, sizeof(ctx->name), "%s[%d]",
  1407. current->comm, pid_nr(ctx->pid));
  1408. spin_lock(&ctx->client->ctx_lock);
  1409. list_add_tail_rcu(&ctx->client_link, &ctx->client->ctx_list);
  1410. spin_unlock(&ctx->client->ctx_lock);
  1411. spin_lock(&i915->gem.contexts.lock);
  1412. list_add_tail(&ctx->link, &i915->gem.contexts.list);
  1413. spin_unlock(&i915->gem.contexts.lock);
  1414. /* And finally expose ourselves to userspace via the idr */
  1415. old = xa_store(&fpriv->context_xa, id, ctx, GFP_KERNEL);
  1416. WARN_ON(old);
  1417. }
  1418. int i915_gem_context_open(struct drm_i915_private *i915,
  1419. struct drm_file *file)
  1420. {
  1421. struct drm_i915_file_private *file_priv = file->driver_priv;
  1422. struct i915_gem_proto_context *pc;
  1423. struct i915_gem_context *ctx;
  1424. int err;
  1425. mutex_init(&file_priv->proto_context_lock);
  1426. xa_init_flags(&file_priv->proto_context_xa, XA_FLAGS_ALLOC);
  1427. /* 0 reserved for the default context */
  1428. xa_init_flags(&file_priv->context_xa, XA_FLAGS_ALLOC1);
  1429. /* 0 reserved for invalid/unassigned ppgtt */
  1430. xa_init_flags(&file_priv->vm_xa, XA_FLAGS_ALLOC1);
  1431. pc = proto_context_create(file_priv, i915, 0);
  1432. if (IS_ERR(pc)) {
  1433. err = PTR_ERR(pc);
  1434. goto err;
  1435. }
  1436. ctx = i915_gem_create_context(i915, pc);
  1437. proto_context_close(i915, pc);
  1438. if (IS_ERR(ctx)) {
  1439. err = PTR_ERR(ctx);
  1440. goto err;
  1441. }
  1442. gem_context_register(ctx, file_priv, 0);
  1443. return 0;
  1444. err:
  1445. xa_destroy(&file_priv->vm_xa);
  1446. xa_destroy(&file_priv->context_xa);
  1447. xa_destroy(&file_priv->proto_context_xa);
  1448. mutex_destroy(&file_priv->proto_context_lock);
  1449. return err;
  1450. }
  1451. void i915_gem_context_close(struct drm_file *file)
  1452. {
  1453. struct drm_i915_file_private *file_priv = file->driver_priv;
  1454. struct i915_gem_proto_context *pc;
  1455. struct i915_address_space *vm;
  1456. struct i915_gem_context *ctx;
  1457. unsigned long idx;
  1458. xa_for_each(&file_priv->proto_context_xa, idx, pc)
  1459. proto_context_close(file_priv->i915, pc);
  1460. xa_destroy(&file_priv->proto_context_xa);
  1461. mutex_destroy(&file_priv->proto_context_lock);
  1462. xa_for_each(&file_priv->context_xa, idx, ctx)
  1463. context_close(ctx);
  1464. xa_destroy(&file_priv->context_xa);
  1465. xa_for_each(&file_priv->vm_xa, idx, vm)
  1466. i915_vm_put(vm);
  1467. xa_destroy(&file_priv->vm_xa);
  1468. }
  1469. int i915_gem_vm_create_ioctl(struct drm_device *dev, void *data,
  1470. struct drm_file *file)
  1471. {
  1472. struct drm_i915_private *i915 = to_i915(dev);
  1473. struct drm_i915_gem_vm_control *args = data;
  1474. struct drm_i915_file_private *file_priv = file->driver_priv;
  1475. struct i915_ppgtt *ppgtt;
  1476. u32 id;
  1477. int err;
  1478. if (!HAS_FULL_PPGTT(i915))
  1479. return -ENODEV;
  1480. if (args->flags)
  1481. return -EINVAL;
  1482. ppgtt = i915_ppgtt_create(to_gt(i915), 0);
  1483. if (IS_ERR(ppgtt))
  1484. return PTR_ERR(ppgtt);
  1485. if (args->extensions) {
  1486. err = i915_user_extensions(u64_to_user_ptr(args->extensions),
  1487. NULL, 0,
  1488. ppgtt);
  1489. if (err)
  1490. goto err_put;
  1491. }
  1492. err = xa_alloc(&file_priv->vm_xa, &id, &ppgtt->vm,
  1493. xa_limit_32b, GFP_KERNEL);
  1494. if (err)
  1495. goto err_put;
  1496. GEM_BUG_ON(id == 0); /* reserved for invalid/unassigned ppgtt */
  1497. args->vm_id = id;
  1498. ppgtt->vm.fpriv = file_priv;
  1499. return 0;
  1500. err_put:
  1501. i915_vm_put(&ppgtt->vm);
  1502. return err;
  1503. }
  1504. int i915_gem_vm_destroy_ioctl(struct drm_device *dev, void *data,
  1505. struct drm_file *file)
  1506. {
  1507. struct drm_i915_file_private *file_priv = file->driver_priv;
  1508. struct drm_i915_gem_vm_control *args = data;
  1509. struct i915_address_space *vm;
  1510. if (args->flags)
  1511. return -EINVAL;
  1512. if (args->extensions)
  1513. return -EINVAL;
  1514. vm = xa_erase(&file_priv->vm_xa, args->vm_id);
  1515. if (!vm)
  1516. return -ENOENT;
  1517. i915_vm_put(vm);
  1518. return 0;
  1519. }
  1520. static int get_ppgtt(struct drm_i915_file_private *file_priv,
  1521. struct i915_gem_context *ctx,
  1522. struct drm_i915_gem_context_param *args)
  1523. {
  1524. struct i915_address_space *vm;
  1525. int err;
  1526. u32 id;
  1527. if (!i915_gem_context_has_full_ppgtt(ctx))
  1528. return -ENODEV;
  1529. vm = ctx->vm;
  1530. GEM_BUG_ON(!vm);
  1531. /*
  1532. * Get a reference for the allocated handle. Once the handle is
  1533. * visible in the vm_xa table, userspace could try to close it
  1534. * from under our feet, so we need to hold the extra reference
  1535. * first.
  1536. */
  1537. i915_vm_get(vm);
  1538. err = xa_alloc(&file_priv->vm_xa, &id, vm, xa_limit_32b, GFP_KERNEL);
  1539. if (err) {
  1540. i915_vm_put(vm);
  1541. return err;
  1542. }
  1543. GEM_BUG_ON(id == 0); /* reserved for invalid/unassigned ppgtt */
  1544. args->value = id;
  1545. args->size = 0;
  1546. return err;
  1547. }
  1548. int
  1549. i915_gem_user_to_context_sseu(struct intel_gt *gt,
  1550. const struct drm_i915_gem_context_param_sseu *user,
  1551. struct intel_sseu *context)
  1552. {
  1553. const struct sseu_dev_info *device = &gt->info.sseu;
  1554. struct drm_i915_private *i915 = gt->i915;
  1555. unsigned int dev_subslice_mask = intel_sseu_get_hsw_subslices(device, 0);
  1556. /* No zeros in any field. */
  1557. if (!user->slice_mask || !user->subslice_mask ||
  1558. !user->min_eus_per_subslice || !user->max_eus_per_subslice)
  1559. return -EINVAL;
  1560. /* Max > min. */
  1561. if (user->max_eus_per_subslice < user->min_eus_per_subslice)
  1562. return -EINVAL;
  1563. /*
  1564. * Some future proofing on the types since the uAPI is wider than the
  1565. * current internal implementation.
  1566. */
  1567. if (overflows_type(user->slice_mask, context->slice_mask) ||
  1568. overflows_type(user->subslice_mask, context->subslice_mask) ||
  1569. overflows_type(user->min_eus_per_subslice,
  1570. context->min_eus_per_subslice) ||
  1571. overflows_type(user->max_eus_per_subslice,
  1572. context->max_eus_per_subslice))
  1573. return -EINVAL;
  1574. /* Check validity against hardware. */
  1575. if (user->slice_mask & ~device->slice_mask)
  1576. return -EINVAL;
  1577. if (user->subslice_mask & ~dev_subslice_mask)
  1578. return -EINVAL;
  1579. if (user->max_eus_per_subslice > device->max_eus_per_subslice)
  1580. return -EINVAL;
  1581. context->slice_mask = user->slice_mask;
  1582. context->subslice_mask = user->subslice_mask;
  1583. context->min_eus_per_subslice = user->min_eus_per_subslice;
  1584. context->max_eus_per_subslice = user->max_eus_per_subslice;
  1585. /* Part specific restrictions. */
  1586. if (GRAPHICS_VER(i915) == 11) {
  1587. unsigned int hw_s = hweight8(device->slice_mask);
  1588. unsigned int hw_ss_per_s = hweight8(dev_subslice_mask);
  1589. unsigned int req_s = hweight8(context->slice_mask);
  1590. unsigned int req_ss = hweight8(context->subslice_mask);
  1591. /*
  1592. * Only full subslice enablement is possible if more than one
  1593. * slice is turned on.
  1594. */
  1595. if (req_s > 1 && req_ss != hw_ss_per_s)
  1596. return -EINVAL;
  1597. /*
  1598. * If more than four (SScount bitfield limit) subslices are
  1599. * requested then the number has to be even.
  1600. */
  1601. if (req_ss > 4 && (req_ss & 1))
  1602. return -EINVAL;
  1603. /*
  1604. * If only one slice is enabled and subslice count is below the
  1605. * device full enablement, it must be at most half of the all
  1606. * available subslices.
  1607. */
  1608. if (req_s == 1 && req_ss < hw_ss_per_s &&
  1609. req_ss > (hw_ss_per_s / 2))
  1610. return -EINVAL;
  1611. /* ABI restriction - VME use case only. */
  1612. /* All slices or one slice only. */
  1613. if (req_s != 1 && req_s != hw_s)
  1614. return -EINVAL;
  1615. /*
  1616. * Half subslices or full enablement only when one slice is
  1617. * enabled.
  1618. */
  1619. if (req_s == 1 &&
  1620. (req_ss != hw_ss_per_s && req_ss != (hw_ss_per_s / 2)))
  1621. return -EINVAL;
  1622. /* No EU configuration changes. */
  1623. if ((user->min_eus_per_subslice !=
  1624. device->max_eus_per_subslice) ||
  1625. (user->max_eus_per_subslice !=
  1626. device->max_eus_per_subslice))
  1627. return -EINVAL;
  1628. }
  1629. return 0;
  1630. }
  1631. static int set_sseu(struct i915_gem_context *ctx,
  1632. struct drm_i915_gem_context_param *args)
  1633. {
  1634. struct drm_i915_private *i915 = ctx->i915;
  1635. struct drm_i915_gem_context_param_sseu user_sseu;
  1636. struct intel_context *ce;
  1637. struct intel_sseu sseu;
  1638. unsigned long lookup;
  1639. int ret;
  1640. if (args->size < sizeof(user_sseu))
  1641. return -EINVAL;
  1642. if (GRAPHICS_VER(i915) != 11)
  1643. return -ENODEV;
  1644. if (copy_from_user(&user_sseu, u64_to_user_ptr(args->value),
  1645. sizeof(user_sseu)))
  1646. return -EFAULT;
  1647. if (user_sseu.rsvd)
  1648. return -EINVAL;
  1649. if (user_sseu.flags & ~(I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX))
  1650. return -EINVAL;
  1651. lookup = 0;
  1652. if (user_sseu.flags & I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX)
  1653. lookup |= LOOKUP_USER_INDEX;
  1654. ce = lookup_user_engine(ctx, lookup, &user_sseu.engine);
  1655. if (IS_ERR(ce))
  1656. return PTR_ERR(ce);
  1657. /* Only render engine supports RPCS configuration. */
  1658. if (ce->engine->class != RENDER_CLASS) {
  1659. ret = -ENODEV;
  1660. goto out_ce;
  1661. }
  1662. ret = i915_gem_user_to_context_sseu(ce->engine->gt, &user_sseu, &sseu);
  1663. if (ret)
  1664. goto out_ce;
  1665. ret = intel_context_reconfigure_sseu(ce, sseu);
  1666. if (ret)
  1667. goto out_ce;
  1668. args->size = sizeof(user_sseu);
  1669. out_ce:
  1670. intel_context_put(ce);
  1671. return ret;
  1672. }
  1673. static int
  1674. set_persistence(struct i915_gem_context *ctx,
  1675. const struct drm_i915_gem_context_param *args)
  1676. {
  1677. if (args->size)
  1678. return -EINVAL;
  1679. return __context_set_persistence(ctx, args->value);
  1680. }
  1681. static int set_priority(struct i915_gem_context *ctx,
  1682. const struct drm_i915_gem_context_param *args)
  1683. {
  1684. struct i915_gem_engines_iter it;
  1685. struct intel_context *ce;
  1686. int err;
  1687. err = validate_priority(ctx->i915, args);
  1688. if (err)
  1689. return err;
  1690. ctx->sched.priority = args->value;
  1691. for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
  1692. if (!intel_engine_has_timeslices(ce->engine))
  1693. continue;
  1694. if (ctx->sched.priority >= I915_PRIORITY_NORMAL &&
  1695. intel_engine_has_semaphores(ce->engine))
  1696. intel_context_set_use_semaphores(ce);
  1697. else
  1698. intel_context_clear_use_semaphores(ce);
  1699. }
  1700. i915_gem_context_unlock_engines(ctx);
  1701. return 0;
  1702. }
  1703. static int get_protected(struct i915_gem_context *ctx,
  1704. struct drm_i915_gem_context_param *args)
  1705. {
  1706. args->size = 0;
  1707. args->value = i915_gem_context_uses_protected_content(ctx);
  1708. return 0;
  1709. }
  1710. static int set_context_image(struct i915_gem_context *ctx,
  1711. struct drm_i915_gem_context_param *args)
  1712. {
  1713. struct i915_gem_context_param_context_image user;
  1714. struct intel_context *ce;
  1715. struct file *shmem_state;
  1716. unsigned long lookup;
  1717. void *state;
  1718. int ret = 0;
  1719. if (!IS_ENABLED(CONFIG_DRM_I915_REPLAY_GPU_HANGS_API))
  1720. return -EINVAL;
  1721. if (!ctx->i915->params.enable_debug_only_api)
  1722. return -EINVAL;
  1723. if (args->size < sizeof(user))
  1724. return -EINVAL;
  1725. if (copy_from_user(&user, u64_to_user_ptr(args->value), sizeof(user)))
  1726. return -EFAULT;
  1727. if (user.mbz)
  1728. return -EINVAL;
  1729. if (user.flags & ~(I915_CONTEXT_IMAGE_FLAG_ENGINE_INDEX))
  1730. return -EINVAL;
  1731. lookup = 0;
  1732. if (user.flags & I915_CONTEXT_IMAGE_FLAG_ENGINE_INDEX)
  1733. lookup |= LOOKUP_USER_INDEX;
  1734. ce = lookup_user_engine(ctx, lookup, &user.engine);
  1735. if (IS_ERR(ce))
  1736. return PTR_ERR(ce);
  1737. if (user.size < ce->engine->context_size) {
  1738. ret = -EINVAL;
  1739. goto out_ce;
  1740. }
  1741. if (drm_WARN_ON_ONCE(&ctx->i915->drm,
  1742. test_bit(CONTEXT_ALLOC_BIT, &ce->flags))) {
  1743. /*
  1744. * This is racy but for a debug only API, if userspace is keen
  1745. * to create and configure contexts, while simultaneously using
  1746. * them from a second thread, let them suffer by potentially not
  1747. * executing with the context image they just raced to apply.
  1748. */
  1749. ret = -EBUSY;
  1750. goto out_ce;
  1751. }
  1752. state = kmalloc(ce->engine->context_size, GFP_KERNEL);
  1753. if (!state) {
  1754. ret = -ENOMEM;
  1755. goto out_ce;
  1756. }
  1757. if (copy_from_user(state, u64_to_user_ptr(user.image),
  1758. ce->engine->context_size)) {
  1759. ret = -EFAULT;
  1760. goto out_state;
  1761. }
  1762. shmem_state = shmem_create_from_data(ce->engine->name,
  1763. state, ce->engine->context_size);
  1764. if (IS_ERR(shmem_state)) {
  1765. ret = PTR_ERR(shmem_state);
  1766. goto out_state;
  1767. }
  1768. if (intel_context_set_own_state(ce)) {
  1769. ret = -EBUSY;
  1770. fput(shmem_state);
  1771. goto out_state;
  1772. }
  1773. ce->default_state = shmem_state;
  1774. args->size = sizeof(user);
  1775. out_state:
  1776. kfree(state);
  1777. out_ce:
  1778. intel_context_put(ce);
  1779. return ret;
  1780. }
  1781. static int ctx_setparam(struct drm_i915_file_private *fpriv,
  1782. struct i915_gem_context *ctx,
  1783. struct drm_i915_gem_context_param *args)
  1784. {
  1785. int ret = 0;
  1786. switch (args->param) {
  1787. case I915_CONTEXT_PARAM_NO_ERROR_CAPTURE:
  1788. if (args->size)
  1789. ret = -EINVAL;
  1790. else if (args->value)
  1791. i915_gem_context_set_no_error_capture(ctx);
  1792. else
  1793. i915_gem_context_clear_no_error_capture(ctx);
  1794. break;
  1795. case I915_CONTEXT_PARAM_BANNABLE:
  1796. if (args->size)
  1797. ret = -EINVAL;
  1798. else if (!capable(CAP_SYS_ADMIN) && !args->value)
  1799. ret = -EPERM;
  1800. else if (args->value)
  1801. i915_gem_context_set_bannable(ctx);
  1802. else if (i915_gem_context_uses_protected_content(ctx))
  1803. ret = -EPERM; /* can't clear this for protected contexts */
  1804. else
  1805. i915_gem_context_clear_bannable(ctx);
  1806. break;
  1807. case I915_CONTEXT_PARAM_RECOVERABLE:
  1808. if (args->size)
  1809. ret = -EINVAL;
  1810. else if (!args->value)
  1811. i915_gem_context_clear_recoverable(ctx);
  1812. else if (i915_gem_context_uses_protected_content(ctx))
  1813. ret = -EPERM; /* can't set this for protected contexts */
  1814. else
  1815. i915_gem_context_set_recoverable(ctx);
  1816. break;
  1817. case I915_CONTEXT_PARAM_PRIORITY:
  1818. ret = set_priority(ctx, args);
  1819. break;
  1820. case I915_CONTEXT_PARAM_SSEU:
  1821. ret = set_sseu(ctx, args);
  1822. break;
  1823. case I915_CONTEXT_PARAM_PERSISTENCE:
  1824. ret = set_persistence(ctx, args);
  1825. break;
  1826. case I915_CONTEXT_PARAM_CONTEXT_IMAGE:
  1827. ret = set_context_image(ctx, args);
  1828. break;
  1829. case I915_CONTEXT_PARAM_PROTECTED_CONTENT:
  1830. case I915_CONTEXT_PARAM_NO_ZEROMAP:
  1831. case I915_CONTEXT_PARAM_BAN_PERIOD:
  1832. case I915_CONTEXT_PARAM_RINGSIZE:
  1833. case I915_CONTEXT_PARAM_VM:
  1834. case I915_CONTEXT_PARAM_ENGINES:
  1835. default:
  1836. ret = -EINVAL;
  1837. break;
  1838. }
  1839. return ret;
  1840. }
  1841. struct create_ext {
  1842. struct i915_gem_proto_context *pc;
  1843. struct drm_i915_file_private *fpriv;
  1844. };
  1845. static int create_setparam(struct i915_user_extension __user *ext, void *data)
  1846. {
  1847. struct drm_i915_gem_context_create_ext_setparam local;
  1848. const struct create_ext *arg = data;
  1849. if (copy_from_user(&local, ext, sizeof(local)))
  1850. return -EFAULT;
  1851. if (local.param.ctx_id)
  1852. return -EINVAL;
  1853. return set_proto_ctx_param(arg->fpriv, arg->pc, &local.param);
  1854. }
  1855. static int invalid_ext(struct i915_user_extension __user *ext, void *data)
  1856. {
  1857. return -EINVAL;
  1858. }
  1859. static const i915_user_extension_fn create_extensions[] = {
  1860. [I915_CONTEXT_CREATE_EXT_SETPARAM] = create_setparam,
  1861. [I915_CONTEXT_CREATE_EXT_CLONE] = invalid_ext,
  1862. };
  1863. static bool client_is_banned(struct drm_i915_file_private *file_priv)
  1864. {
  1865. return atomic_read(&file_priv->ban_score) >= I915_CLIENT_SCORE_BANNED;
  1866. }
  1867. static inline struct i915_gem_context *
  1868. __context_lookup(struct drm_i915_file_private *file_priv, u32 id)
  1869. {
  1870. struct i915_gem_context *ctx;
  1871. rcu_read_lock();
  1872. ctx = xa_load(&file_priv->context_xa, id);
  1873. if (ctx && !kref_get_unless_zero(&ctx->ref))
  1874. ctx = NULL;
  1875. rcu_read_unlock();
  1876. return ctx;
  1877. }
  1878. static struct i915_gem_context *
  1879. finalize_create_context_locked(struct drm_i915_file_private *file_priv,
  1880. struct i915_gem_proto_context *pc, u32 id)
  1881. {
  1882. struct i915_gem_context *ctx;
  1883. void *old;
  1884. lockdep_assert_held(&file_priv->proto_context_lock);
  1885. ctx = i915_gem_create_context(file_priv->i915, pc);
  1886. if (IS_ERR(ctx))
  1887. return ctx;
  1888. /*
  1889. * One for the xarray and one for the caller. We need to grab
  1890. * the reference *prior* to making the ctx visble to userspace
  1891. * in gem_context_register(), as at any point after that
  1892. * userspace can try to race us with another thread destroying
  1893. * the context under our feet.
  1894. */
  1895. i915_gem_context_get(ctx);
  1896. gem_context_register(ctx, file_priv, id);
  1897. old = xa_erase(&file_priv->proto_context_xa, id);
  1898. GEM_BUG_ON(old != pc);
  1899. proto_context_close(file_priv->i915, pc);
  1900. return ctx;
  1901. }
  1902. struct i915_gem_context *
  1903. i915_gem_context_lookup(struct drm_i915_file_private *file_priv, u32 id)
  1904. {
  1905. struct i915_gem_proto_context *pc;
  1906. struct i915_gem_context *ctx;
  1907. ctx = __context_lookup(file_priv, id);
  1908. if (ctx)
  1909. return ctx;
  1910. mutex_lock(&file_priv->proto_context_lock);
  1911. /* Try one more time under the lock */
  1912. ctx = __context_lookup(file_priv, id);
  1913. if (!ctx) {
  1914. pc = xa_load(&file_priv->proto_context_xa, id);
  1915. if (!pc)
  1916. ctx = ERR_PTR(-ENOENT);
  1917. else
  1918. ctx = finalize_create_context_locked(file_priv, pc, id);
  1919. }
  1920. mutex_unlock(&file_priv->proto_context_lock);
  1921. return ctx;
  1922. }
  1923. int i915_gem_context_create_ioctl(struct drm_device *dev, void *data,
  1924. struct drm_file *file)
  1925. {
  1926. struct drm_i915_private *i915 = to_i915(dev);
  1927. struct drm_i915_gem_context_create_ext *args = data;
  1928. struct create_ext ext_data;
  1929. int ret;
  1930. u32 id;
  1931. if (!DRIVER_CAPS(i915)->has_logical_contexts)
  1932. return -ENODEV;
  1933. if (args->flags & I915_CONTEXT_CREATE_FLAGS_UNKNOWN)
  1934. return -EINVAL;
  1935. ret = intel_gt_terminally_wedged(to_gt(i915));
  1936. if (ret)
  1937. return ret;
  1938. ext_data.fpriv = file->driver_priv;
  1939. if (client_is_banned(ext_data.fpriv)) {
  1940. drm_dbg(&i915->drm,
  1941. "client %s[%d] banned from creating ctx\n",
  1942. current->comm, task_pid_nr(current));
  1943. return -EIO;
  1944. }
  1945. ext_data.pc = proto_context_create(file->driver_priv, i915,
  1946. args->flags);
  1947. if (IS_ERR(ext_data.pc))
  1948. return PTR_ERR(ext_data.pc);
  1949. if (args->flags & I915_CONTEXT_CREATE_FLAGS_USE_EXTENSIONS) {
  1950. ret = i915_user_extensions(u64_to_user_ptr(args->extensions),
  1951. create_extensions,
  1952. ARRAY_SIZE(create_extensions),
  1953. &ext_data);
  1954. if (ret)
  1955. goto err_pc;
  1956. }
  1957. if (GRAPHICS_VER(i915) > 12) {
  1958. struct i915_gem_context *ctx;
  1959. /* Get ourselves a context ID */
  1960. ret = xa_alloc(&ext_data.fpriv->context_xa, &id, NULL,
  1961. xa_limit_32b, GFP_KERNEL);
  1962. if (ret)
  1963. goto err_pc;
  1964. ctx = i915_gem_create_context(i915, ext_data.pc);
  1965. if (IS_ERR(ctx)) {
  1966. ret = PTR_ERR(ctx);
  1967. goto err_pc;
  1968. }
  1969. proto_context_close(i915, ext_data.pc);
  1970. gem_context_register(ctx, ext_data.fpriv, id);
  1971. } else {
  1972. ret = proto_context_register(ext_data.fpriv, ext_data.pc, &id);
  1973. if (ret < 0)
  1974. goto err_pc;
  1975. }
  1976. args->ctx_id = id;
  1977. return 0;
  1978. err_pc:
  1979. proto_context_close(i915, ext_data.pc);
  1980. return ret;
  1981. }
  1982. int i915_gem_context_destroy_ioctl(struct drm_device *dev, void *data,
  1983. struct drm_file *file)
  1984. {
  1985. struct drm_i915_gem_context_destroy *args = data;
  1986. struct drm_i915_file_private *file_priv = file->driver_priv;
  1987. struct i915_gem_proto_context *pc;
  1988. struct i915_gem_context *ctx;
  1989. if (args->pad != 0)
  1990. return -EINVAL;
  1991. if (!args->ctx_id)
  1992. return -ENOENT;
  1993. /* We need to hold the proto-context lock here to prevent races
  1994. * with finalize_create_context_locked().
  1995. */
  1996. mutex_lock(&file_priv->proto_context_lock);
  1997. ctx = xa_erase(&file_priv->context_xa, args->ctx_id);
  1998. pc = xa_erase(&file_priv->proto_context_xa, args->ctx_id);
  1999. mutex_unlock(&file_priv->proto_context_lock);
  2000. if (!ctx && !pc)
  2001. return -ENOENT;
  2002. GEM_WARN_ON(ctx && pc);
  2003. if (pc)
  2004. proto_context_close(file_priv->i915, pc);
  2005. if (ctx)
  2006. context_close(ctx);
  2007. return 0;
  2008. }
  2009. static int get_sseu(struct i915_gem_context *ctx,
  2010. struct drm_i915_gem_context_param *args)
  2011. {
  2012. struct drm_i915_gem_context_param_sseu user_sseu;
  2013. struct intel_context *ce;
  2014. unsigned long lookup;
  2015. int err;
  2016. if (args->size == 0)
  2017. goto out;
  2018. else if (args->size < sizeof(user_sseu))
  2019. return -EINVAL;
  2020. if (copy_from_user(&user_sseu, u64_to_user_ptr(args->value),
  2021. sizeof(user_sseu)))
  2022. return -EFAULT;
  2023. if (user_sseu.rsvd)
  2024. return -EINVAL;
  2025. if (user_sseu.flags & ~(I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX))
  2026. return -EINVAL;
  2027. lookup = 0;
  2028. if (user_sseu.flags & I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX)
  2029. lookup |= LOOKUP_USER_INDEX;
  2030. ce = lookup_user_engine(ctx, lookup, &user_sseu.engine);
  2031. if (IS_ERR(ce))
  2032. return PTR_ERR(ce);
  2033. err = intel_context_lock_pinned(ce); /* serialises with set_sseu */
  2034. if (err) {
  2035. intel_context_put(ce);
  2036. return err;
  2037. }
  2038. user_sseu.slice_mask = ce->sseu.slice_mask;
  2039. user_sseu.subslice_mask = ce->sseu.subslice_mask;
  2040. user_sseu.min_eus_per_subslice = ce->sseu.min_eus_per_subslice;
  2041. user_sseu.max_eus_per_subslice = ce->sseu.max_eus_per_subslice;
  2042. intel_context_unlock_pinned(ce);
  2043. intel_context_put(ce);
  2044. if (copy_to_user(u64_to_user_ptr(args->value), &user_sseu,
  2045. sizeof(user_sseu)))
  2046. return -EFAULT;
  2047. out:
  2048. args->size = sizeof(user_sseu);
  2049. return 0;
  2050. }
  2051. int i915_gem_context_getparam_ioctl(struct drm_device *dev, void *data,
  2052. struct drm_file *file)
  2053. {
  2054. struct drm_i915_file_private *file_priv = file->driver_priv;
  2055. struct drm_i915_gem_context_param *args = data;
  2056. struct i915_gem_context *ctx;
  2057. struct i915_address_space *vm;
  2058. int ret = 0;
  2059. ctx = i915_gem_context_lookup(file_priv, args->ctx_id);
  2060. if (IS_ERR(ctx))
  2061. return PTR_ERR(ctx);
  2062. switch (args->param) {
  2063. case I915_CONTEXT_PARAM_GTT_SIZE:
  2064. args->size = 0;
  2065. vm = i915_gem_context_get_eb_vm(ctx);
  2066. args->value = vm->total;
  2067. i915_vm_put(vm);
  2068. break;
  2069. case I915_CONTEXT_PARAM_NO_ERROR_CAPTURE:
  2070. args->size = 0;
  2071. args->value = i915_gem_context_no_error_capture(ctx);
  2072. break;
  2073. case I915_CONTEXT_PARAM_BANNABLE:
  2074. args->size = 0;
  2075. args->value = i915_gem_context_is_bannable(ctx);
  2076. break;
  2077. case I915_CONTEXT_PARAM_RECOVERABLE:
  2078. args->size = 0;
  2079. args->value = i915_gem_context_is_recoverable(ctx);
  2080. break;
  2081. case I915_CONTEXT_PARAM_PRIORITY:
  2082. args->size = 0;
  2083. args->value = ctx->sched.priority;
  2084. break;
  2085. case I915_CONTEXT_PARAM_SSEU:
  2086. ret = get_sseu(ctx, args);
  2087. break;
  2088. case I915_CONTEXT_PARAM_VM:
  2089. ret = get_ppgtt(file_priv, ctx, args);
  2090. break;
  2091. case I915_CONTEXT_PARAM_PERSISTENCE:
  2092. args->size = 0;
  2093. args->value = i915_gem_context_is_persistent(ctx);
  2094. break;
  2095. case I915_CONTEXT_PARAM_PROTECTED_CONTENT:
  2096. ret = get_protected(ctx, args);
  2097. break;
  2098. case I915_CONTEXT_PARAM_NO_ZEROMAP:
  2099. case I915_CONTEXT_PARAM_BAN_PERIOD:
  2100. case I915_CONTEXT_PARAM_ENGINES:
  2101. case I915_CONTEXT_PARAM_RINGSIZE:
  2102. case I915_CONTEXT_PARAM_CONTEXT_IMAGE:
  2103. default:
  2104. ret = -EINVAL;
  2105. break;
  2106. }
  2107. i915_gem_context_put(ctx);
  2108. return ret;
  2109. }
  2110. int i915_gem_context_setparam_ioctl(struct drm_device *dev, void *data,
  2111. struct drm_file *file)
  2112. {
  2113. struct drm_i915_file_private *file_priv = file->driver_priv;
  2114. struct drm_i915_gem_context_param *args = data;
  2115. struct i915_gem_proto_context *pc;
  2116. struct i915_gem_context *ctx;
  2117. int ret = 0;
  2118. mutex_lock(&file_priv->proto_context_lock);
  2119. ctx = __context_lookup(file_priv, args->ctx_id);
  2120. if (!ctx) {
  2121. pc = xa_load(&file_priv->proto_context_xa, args->ctx_id);
  2122. if (pc) {
  2123. /* Contexts should be finalized inside
  2124. * GEM_CONTEXT_CREATE starting with graphics
  2125. * version 13.
  2126. */
  2127. WARN_ON(GRAPHICS_VER(file_priv->i915) > 12);
  2128. ret = set_proto_ctx_param(file_priv, pc, args);
  2129. } else {
  2130. ret = -ENOENT;
  2131. }
  2132. }
  2133. mutex_unlock(&file_priv->proto_context_lock);
  2134. if (ctx) {
  2135. ret = ctx_setparam(file_priv, ctx, args);
  2136. i915_gem_context_put(ctx);
  2137. }
  2138. return ret;
  2139. }
  2140. int i915_gem_context_reset_stats_ioctl(struct drm_device *dev,
  2141. void *data, struct drm_file *file)
  2142. {
  2143. struct drm_i915_private *i915 = to_i915(dev);
  2144. struct drm_i915_reset_stats *args = data;
  2145. struct i915_gem_context *ctx;
  2146. if (args->flags || args->pad)
  2147. return -EINVAL;
  2148. ctx = i915_gem_context_lookup(file->driver_priv, args->ctx_id);
  2149. if (IS_ERR(ctx))
  2150. return PTR_ERR(ctx);
  2151. /*
  2152. * We opt for unserialised reads here. This may result in tearing
  2153. * in the extremely unlikely event of a GPU hang on this context
  2154. * as we are querying them. If we need that extra layer of protection,
  2155. * we should wrap the hangstats with a seqlock.
  2156. */
  2157. if (capable(CAP_SYS_ADMIN))
  2158. args->reset_count = i915_reset_count(&i915->gpu_error);
  2159. else
  2160. args->reset_count = 0;
  2161. args->batch_active = atomic_read(&ctx->guilty_count);
  2162. args->batch_pending = atomic_read(&ctx->active_count);
  2163. i915_gem_context_put(ctx);
  2164. return 0;
  2165. }
  2166. /* GEM context-engines iterator: for_each_gem_engine() */
  2167. struct intel_context *
  2168. i915_gem_engines_iter_next(struct i915_gem_engines_iter *it)
  2169. {
  2170. const struct i915_gem_engines *e = it->engines;
  2171. struct intel_context *ctx;
  2172. if (unlikely(!e))
  2173. return NULL;
  2174. do {
  2175. if (it->idx >= e->num_engines)
  2176. return NULL;
  2177. ctx = e->engines[it->idx++];
  2178. } while (!ctx);
  2179. return ctx;
  2180. }
  2181. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  2182. #include "selftests/mock_context.c"
  2183. #include "selftests/i915_gem_context.c"
  2184. #endif
  2185. void i915_gem_context_module_exit(void)
  2186. {
  2187. kmem_cache_destroy(slab_luts);
  2188. }
  2189. int __init i915_gem_context_module_init(void)
  2190. {
  2191. slab_luts = KMEM_CACHE(i915_lut_handle, 0);
  2192. if (!slab_luts)
  2193. return -ENOMEM;
  2194. if (IS_ENABLED(CONFIG_DRM_I915_REPLAY_GPU_HANGS_API)) {
  2195. pr_notice("**************************************************************\n");
  2196. pr_notice("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
  2197. pr_notice("** **\n");
  2198. if (i915_modparams.enable_debug_only_api)
  2199. pr_notice("** i915.enable_debug_only_api is intended to be set **\n");
  2200. else
  2201. pr_notice("** CONFIG_DRM_I915_REPLAY_GPU_HANGS_API builds are intended **\n");
  2202. pr_notice("** for specific userspace graphics stack developers only! **\n");
  2203. pr_notice("** **\n");
  2204. pr_notice("** If you are seeing this message please report this to the **\n");
  2205. pr_notice("** provider of your kernel build. **\n");
  2206. pr_notice("** **\n");
  2207. pr_notice("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
  2208. pr_notice("**************************************************************\n");
  2209. }
  2210. return 0;
  2211. }