adreno_hwsched.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2024, Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/dma-fence-array.h>
  7. #include <soc/qcom/msm_performance.h>
  8. #include "adreno.h"
  9. #include "adreno_hfi.h"
  10. #include "adreno_snapshot.h"
  11. #include "adreno_sysfs.h"
  12. #include "adreno_trace.h"
  13. #include "kgsl_timeline.h"
  14. #include <linux/msm_kgsl.h>
  15. /*
  16. * Number of commands that can be queued in a context before it sleeps
  17. *
  18. * Our code that "puts back" a command from the context is much cleaner
  19. * if we are sure that there will always be enough room in the ringbuffer
  20. * so restrict the size of the context queue to ADRENO_CONTEXT_DRAWQUEUE_SIZE - 1
  21. */
  22. static u32 _context_drawqueue_size = ADRENO_CONTEXT_DRAWQUEUE_SIZE - 1;
  23. /* Number of milliseconds to wait for the context queue to clear */
  24. static unsigned int _context_queue_wait = 10000;
  25. /*
  26. * GFT throttle parameters. If GFT recovered more than
  27. * X times in Y ms invalidate the context and do not attempt recovery.
  28. * X -> _fault_throttle_burst
  29. * Y -> _fault_throttle_time
  30. */
  31. static unsigned int _fault_throttle_time = 2000;
  32. static unsigned int _fault_throttle_burst = 3;
  33. /* Use a kmem cache to speed up allocations for dispatcher jobs */
  34. static struct kmem_cache *jobs_cache;
  35. /* Use a kmem cache to speed up allocations for inflight command objects */
  36. static struct kmem_cache *obj_cache;
  37. inline bool adreno_hwsched_context_queue_enabled(struct adreno_device *adreno_dev)
  38. {
  39. return test_bit(ADRENO_HWSCHED_CONTEXT_QUEUE, &adreno_dev->hwsched.flags);
  40. }
  41. static bool is_cmdobj(struct kgsl_drawobj *drawobj)
  42. {
  43. return (drawobj->type & CMDOBJ_TYPE);
  44. }
  45. static bool _check_context_queue(struct adreno_context *drawctxt, u32 count)
  46. {
  47. bool ret;
  48. spin_lock(&drawctxt->lock);
  49. /*
  50. * Wake up if there is room in the context or if the whole thing got
  51. * invalidated while we were asleep
  52. */
  53. if (kgsl_context_invalid(&drawctxt->base))
  54. ret = false;
  55. else
  56. ret = ((drawctxt->queued + count) < _context_drawqueue_size) ? 1 : 0;
  57. spin_unlock(&drawctxt->lock);
  58. return ret;
  59. }
  60. static void _pop_drawobj(struct adreno_context *drawctxt)
  61. {
  62. drawctxt->drawqueue_head = DRAWQUEUE_NEXT(drawctxt->drawqueue_head,
  63. ADRENO_CONTEXT_DRAWQUEUE_SIZE);
  64. drawctxt->queued--;
  65. }
  66. static int _retire_syncobj(struct adreno_device *adreno_dev,
  67. struct kgsl_drawobj_sync *syncobj, struct adreno_context *drawctxt)
  68. {
  69. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  70. if (!kgsl_drawobj_events_pending(syncobj)) {
  71. _pop_drawobj(drawctxt);
  72. kgsl_drawobj_destroy(DRAWOBJ(syncobj));
  73. return 0;
  74. }
  75. /*
  76. * If hardware fences are enabled, and this SYNCOBJ is backed by hardware fences,
  77. * send it to the GMU
  78. */
  79. if (test_bit(ADRENO_HWSCHED_HW_FENCE, &hwsched->flags) &&
  80. ((syncobj->flags & KGSL_SYNCOBJ_HW)))
  81. return 1;
  82. /*
  83. * If we got here, there are pending events for sync object.
  84. * Start the canary timer if it hasnt been started already.
  85. */
  86. if (!syncobj->timeout_jiffies) {
  87. syncobj->timeout_jiffies = jiffies + msecs_to_jiffies(5000);
  88. mod_timer(&syncobj->timer, syncobj->timeout_jiffies);
  89. }
  90. return -EAGAIN;
  91. }
  92. static bool _marker_expired(struct kgsl_drawobj_cmd *markerobj)
  93. {
  94. struct kgsl_drawobj *drawobj = DRAWOBJ(markerobj);
  95. return (drawobj->flags & KGSL_DRAWOBJ_MARKER) &&
  96. kgsl_check_timestamp(drawobj->device, drawobj->context,
  97. markerobj->marker_timestamp);
  98. }
  99. /* Only retire the timestamp. The drawobj will be destroyed later */
  100. static void _retire_timestamp_only(struct kgsl_drawobj *drawobj)
  101. {
  102. struct kgsl_context *context = drawobj->context;
  103. struct kgsl_device *device = context->device;
  104. /*
  105. * Write the start and end timestamp to the memstore to keep the
  106. * accounting sane
  107. */
  108. kgsl_sharedmem_writel(device->memstore,
  109. KGSL_MEMSTORE_OFFSET(context->id, soptimestamp),
  110. drawobj->timestamp);
  111. kgsl_sharedmem_writel(device->memstore,
  112. KGSL_MEMSTORE_OFFSET(context->id, eoptimestamp),
  113. drawobj->timestamp);
  114. msm_perf_events_update(MSM_PERF_GFX, MSM_PERF_RETIRED,
  115. pid_nr(context->proc_priv->pid),
  116. context->id, drawobj->timestamp,
  117. !!(drawobj->flags & KGSL_DRAWOBJ_END_OF_FRAME));
  118. if (drawobj->flags & KGSL_DRAWOBJ_END_OF_FRAME) {
  119. atomic64_inc(&drawobj->context->proc_priv->frame_count);
  120. atomic_inc(&drawobj->context->proc_priv->period->frames);
  121. }
  122. /* Retire pending GPU events for the object */
  123. kgsl_process_event_group(device, &context->events);
  124. }
  125. static void _retire_timestamp(struct kgsl_drawobj *drawobj)
  126. {
  127. _retire_timestamp_only(drawobj);
  128. kgsl_drawobj_destroy(drawobj);
  129. }
  130. static int _retire_markerobj(struct adreno_device *adreno_dev, struct kgsl_drawobj_cmd *cmdobj,
  131. struct adreno_context *drawctxt)
  132. {
  133. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  134. if (_marker_expired(cmdobj)) {
  135. set_bit(CMDOBJ_MARKER_EXPIRED, &cmdobj->priv);
  136. /*
  137. * There may be pending hardware fences that need to be signaled upon retiring
  138. * this MARKER object. Hence, send it to the target specific layers to trigger
  139. * the hardware fences.
  140. */
  141. if (test_bit(ADRENO_HWSCHED_HW_FENCE, &hwsched->flags)) {
  142. _retire_timestamp_only(DRAWOBJ(cmdobj));
  143. return 1;
  144. }
  145. _pop_drawobj(drawctxt);
  146. _retire_timestamp(DRAWOBJ(cmdobj));
  147. return 0;
  148. }
  149. /*
  150. * If the marker isn't expired but the SKIP bit
  151. * is set then there are real commands following
  152. * this one in the queue. This means that we
  153. * need to dispatch the command so that we can
  154. * keep the timestamp accounting correct. If
  155. * skip isn't set then we block this queue
  156. * until the dependent timestamp expires
  157. */
  158. return test_bit(CMDOBJ_SKIP, &cmdobj->priv) ? 1 : -EAGAIN;
  159. }
  160. static int _retire_timelineobj(struct kgsl_drawobj *drawobj,
  161. struct adreno_context *drawctxt)
  162. {
  163. _pop_drawobj(drawctxt);
  164. kgsl_drawobj_destroy(drawobj);
  165. return 0;
  166. }
  167. static int drawqueue_retire_bindobj(struct kgsl_drawobj *drawobj,
  168. struct adreno_context *drawctxt)
  169. {
  170. struct kgsl_drawobj_bind *bindobj = BINDOBJ(drawobj);
  171. if (test_bit(KGSL_BINDOBJ_STATE_DONE, &bindobj->state)) {
  172. _pop_drawobj(drawctxt);
  173. _retire_timestamp(drawobj);
  174. return 0;
  175. }
  176. if (!test_and_set_bit(KGSL_BINDOBJ_STATE_START, &bindobj->state)) {
  177. /*
  178. * Take a reference to the drawobj and the context because both
  179. * get referenced in the bind callback
  180. */
  181. _kgsl_context_get(&drawctxt->base);
  182. kref_get(&drawobj->refcount);
  183. kgsl_sharedmem_bind_ranges(bindobj->bind);
  184. }
  185. return -EAGAIN;
  186. }
  187. /*
  188. * Retires all expired marker and sync objs from the context
  189. * queue and returns one of the below
  190. * a) next drawobj that needs to be sent to ringbuffer
  191. * b) -EAGAIN for syncobj with syncpoints pending.
  192. * c) -EAGAIN for markerobj whose marker timestamp has not expired yet.
  193. * c) NULL for no commands remaining in drawqueue.
  194. */
  195. static struct kgsl_drawobj *_process_drawqueue_get_next_drawobj(
  196. struct adreno_device *adreno_dev, struct adreno_context *drawctxt)
  197. {
  198. struct kgsl_drawobj *drawobj;
  199. unsigned int i = drawctxt->drawqueue_head;
  200. struct kgsl_drawobj_cmd *cmdobj;
  201. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  202. int ret = 0;
  203. if (drawctxt->drawqueue_head == drawctxt->drawqueue_tail)
  204. return NULL;
  205. for (i = drawctxt->drawqueue_head; i != drawctxt->drawqueue_tail;
  206. i = DRAWQUEUE_NEXT(i, ADRENO_CONTEXT_DRAWQUEUE_SIZE)) {
  207. drawobj = drawctxt->drawqueue[i];
  208. if (!drawobj)
  209. return NULL;
  210. switch (drawobj->type) {
  211. case CMDOBJ_TYPE:
  212. cmdobj = CMDOBJ(drawobj);
  213. /* We only support one big IB inflight */
  214. if ((cmdobj->numibs > HWSCHED_MAX_DISPATCH_NUMIBS) &&
  215. hwsched->big_cmdobj)
  216. return ERR_PTR(-ENOSPC);
  217. return drawobj;
  218. case SYNCOBJ_TYPE:
  219. ret = _retire_syncobj(adreno_dev, SYNCOBJ(drawobj), drawctxt);
  220. if (ret == 1)
  221. return drawobj;
  222. break;
  223. case MARKEROBJ_TYPE:
  224. ret = _retire_markerobj(adreno_dev, CMDOBJ(drawobj), drawctxt);
  225. /* Special case where marker needs to be sent to GPU */
  226. if (ret == 1)
  227. return drawobj;
  228. break;
  229. case BINDOBJ_TYPE:
  230. ret = drawqueue_retire_bindobj(drawobj, drawctxt);
  231. break;
  232. case TIMELINEOBJ_TYPE:
  233. ret = _retire_timelineobj(drawobj, drawctxt);
  234. break;
  235. default:
  236. ret = -EINVAL;
  237. break;
  238. }
  239. if (ret)
  240. return ERR_PTR(ret);
  241. }
  242. return NULL;
  243. }
  244. /**
  245. * hwsched_dispatcher_requeue_drawobj() - Put a draw objet back on the context
  246. * queue
  247. * @drawctxt: Pointer to the adreno draw context
  248. * @drawobj: Pointer to the KGSL draw object to requeue
  249. *
  250. * Failure to submit a drawobj to the ringbuffer isn't the fault of the drawobj
  251. * being submitted so if a failure happens, push it back on the head of the
  252. * context queue to be reconsidered again unless the context got detached.
  253. */
  254. static inline int hwsched_dispatcher_requeue_drawobj(
  255. struct adreno_context *drawctxt,
  256. struct kgsl_drawobj *drawobj)
  257. {
  258. unsigned int prev;
  259. spin_lock(&drawctxt->lock);
  260. if (kgsl_context_is_bad(&drawctxt->base)) {
  261. spin_unlock(&drawctxt->lock);
  262. /* get rid of this drawobj since the context is bad */
  263. kgsl_drawobj_destroy(drawobj);
  264. return -ENOENT;
  265. }
  266. prev = drawctxt->drawqueue_head == 0 ?
  267. (ADRENO_CONTEXT_DRAWQUEUE_SIZE - 1) :
  268. (drawctxt->drawqueue_head - 1);
  269. /*
  270. * The maximum queue size always needs to be one less then the size of
  271. * the ringbuffer queue so there is "room" to put the drawobj back in
  272. */
  273. WARN_ON(prev == drawctxt->drawqueue_tail);
  274. drawctxt->drawqueue[prev] = drawobj;
  275. drawctxt->queued++;
  276. /* Reset the command queue head to reflect the newly requeued change */
  277. drawctxt->drawqueue_head = prev;
  278. if (is_cmdobj(drawobj)) {
  279. struct kgsl_drawobj_cmd *cmdobj = CMDOBJ(drawobj);
  280. cmdobj->requeue_cnt++;
  281. }
  282. spin_unlock(&drawctxt->lock);
  283. return 0;
  284. }
  285. /**
  286. * hwsched_queue_context() - Queue a context in the dispatcher list of jobs
  287. * @adreno_dev: Pointer to the adreno device structure
  288. * @drawctxt: Pointer to the adreno draw context
  289. *
  290. * Add a context to the dispatcher list of jobs.
  291. */
  292. static int hwsched_queue_context(struct adreno_device *adreno_dev,
  293. struct adreno_context *drawctxt)
  294. {
  295. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  296. struct adreno_dispatch_job *job;
  297. /* Refuse to queue a detached context */
  298. if (kgsl_context_detached(&drawctxt->base))
  299. return 0;
  300. if (!_kgsl_context_get(&drawctxt->base))
  301. return 0;
  302. job = kmem_cache_alloc(jobs_cache, GFP_ATOMIC);
  303. if (!job) {
  304. kgsl_context_put(&drawctxt->base);
  305. return -ENOMEM;
  306. }
  307. job->drawctxt = drawctxt;
  308. trace_dispatch_queue_context(drawctxt);
  309. llist_add(&job->node, &hwsched->jobs[drawctxt->base.priority]);
  310. return 0;
  311. }
  312. void adreno_hwsched_flush(struct adreno_device *adreno_dev)
  313. {
  314. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  315. kthread_flush_worker(hwsched->worker);
  316. }
  317. /**
  318. * is_marker_skip() - Check if the draw object is a MARKEROBJ_TYPE and CMDOBJ_SKIP bit is set
  319. */
  320. static bool is_marker_skip(struct kgsl_drawobj *drawobj)
  321. {
  322. struct kgsl_drawobj_cmd *cmdobj = NULL;
  323. if (drawobj->type != MARKEROBJ_TYPE)
  324. return false;
  325. cmdobj = CMDOBJ(drawobj);
  326. if (test_bit(CMDOBJ_SKIP, &cmdobj->priv))
  327. return true;
  328. return false;
  329. }
  330. static bool _abort_submission(struct adreno_device *adreno_dev)
  331. {
  332. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  333. /* We only need a single barrier before reading all the atomic variables below */
  334. smp_rmb();
  335. if (atomic_read(&adreno_dev->halt) || atomic_read(&hwsched->fault))
  336. return true;
  337. return false;
  338. }
  339. /**
  340. * sendcmd() - Send a drawobj to the GPU hardware
  341. * @dispatcher: Pointer to the adreno dispatcher struct
  342. * @drawobj: Pointer to the KGSL drawobj being sent
  343. *
  344. * Send a KGSL drawobj to the GPU hardware
  345. */
  346. static int hwsched_sendcmd(struct adreno_device *adreno_dev,
  347. struct kgsl_drawobj *drawobj)
  348. {
  349. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  350. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  351. struct kgsl_context *context = drawobj->context;
  352. int ret;
  353. struct cmd_list_obj *obj;
  354. obj = kmem_cache_alloc(obj_cache, GFP_KERNEL);
  355. if (!obj)
  356. return -ENOMEM;
  357. mutex_lock(&device->mutex);
  358. if (_abort_submission(adreno_dev)) {
  359. mutex_unlock(&device->mutex);
  360. kmem_cache_free(obj_cache, obj);
  361. return -EBUSY;
  362. }
  363. if (kgsl_context_detached(context)) {
  364. mutex_unlock(&device->mutex);
  365. kmem_cache_free(obj_cache, obj);
  366. return -ENOENT;
  367. }
  368. hwsched->inflight++;
  369. if (hwsched->inflight == 1 &&
  370. !test_bit(ADRENO_HWSCHED_POWER, &hwsched->flags)) {
  371. ret = adreno_active_count_get(adreno_dev);
  372. if (ret) {
  373. hwsched->inflight--;
  374. mutex_unlock(&device->mutex);
  375. kmem_cache_free(obj_cache, obj);
  376. return ret;
  377. }
  378. set_bit(ADRENO_HWSCHED_POWER, &hwsched->flags);
  379. }
  380. ret = hwsched->hwsched_ops->submit_drawobj(adreno_dev, drawobj);
  381. if (ret) {
  382. /*
  383. * If the first submission failed, then put back the active
  384. * count to relinquish active vote
  385. */
  386. if (hwsched->inflight == 1) {
  387. adreno_active_count_put(adreno_dev);
  388. clear_bit(ADRENO_HWSCHED_POWER, &hwsched->flags);
  389. }
  390. hwsched->inflight--;
  391. kmem_cache_free(obj_cache, obj);
  392. mutex_unlock(&device->mutex);
  393. return ret;
  394. }
  395. if ((hwsched->inflight == 1) &&
  396. !test_and_set_bit(ADRENO_HWSCHED_ACTIVE, &hwsched->flags))
  397. reinit_completion(&hwsched->idle_gate);
  398. if (is_cmdobj(drawobj)) {
  399. struct kgsl_drawobj_cmd *cmdobj = CMDOBJ(drawobj);
  400. /* If this MARKER object is already retired, we can destroy it here */
  401. if ((test_bit(CMDOBJ_MARKER_EXPIRED, &cmdobj->priv))) {
  402. kmem_cache_free(obj_cache, obj);
  403. kgsl_drawobj_destroy(drawobj);
  404. goto done;
  405. }
  406. if (cmdobj->numibs > HWSCHED_MAX_DISPATCH_NUMIBS) {
  407. hwsched->big_cmdobj = cmdobj;
  408. kref_get(&drawobj->refcount);
  409. }
  410. }
  411. obj->drawobj = drawobj;
  412. list_add_tail(&obj->node, &hwsched->cmd_list);
  413. done:
  414. mutex_unlock(&device->mutex);
  415. return 0;
  416. }
  417. /**
  418. * hwsched_sendcmds() - Send commands from a context to the GPU
  419. * @adreno_dev: Pointer to the adreno device struct
  420. * @drawctxt: Pointer to the adreno context to dispatch commands from
  421. *
  422. * Dequeue and send a burst of commands from the specified context to the GPU
  423. * Returns postive if the context needs to be put back on the pending queue
  424. * 0 if the context is empty or detached and negative on error
  425. */
  426. static int hwsched_sendcmds(struct adreno_device *adreno_dev,
  427. struct adreno_context *drawctxt)
  428. {
  429. int count = 0;
  430. int ret = 0;
  431. while (1) {
  432. struct kgsl_drawobj *drawobj;
  433. struct kgsl_drawobj_cmd *cmdobj = NULL;
  434. struct kgsl_context *context;
  435. spin_lock(&drawctxt->lock);
  436. drawobj = _process_drawqueue_get_next_drawobj(adreno_dev,
  437. drawctxt);
  438. /*
  439. * adreno_context_get_drawobj returns -EAGAIN if the current
  440. * drawobj has pending sync points so no more to do here.
  441. * When the sync points are satisfied then the context will get
  442. * reqeueued
  443. */
  444. if (IS_ERR_OR_NULL(drawobj)) {
  445. if (IS_ERR(drawobj))
  446. ret = PTR_ERR(drawobj);
  447. spin_unlock(&drawctxt->lock);
  448. break;
  449. }
  450. _pop_drawobj(drawctxt);
  451. spin_unlock(&drawctxt->lock);
  452. if (is_cmdobj(drawobj) || is_marker_skip(drawobj)) {
  453. cmdobj = CMDOBJ(drawobj);
  454. context = drawobj->context;
  455. trace_adreno_cmdbatch_ready(context->id,
  456. context->priority, drawobj->timestamp,
  457. cmdobj->requeue_cnt);
  458. }
  459. ret = hwsched_sendcmd(adreno_dev, drawobj);
  460. /*
  461. * On error from hwsched_sendcmd() try to requeue the cmdobj
  462. * unless we got back -ENOENT which means that the context has
  463. * been detached and there will be no more deliveries from here
  464. */
  465. if (ret != 0) {
  466. /* Destroy the cmdobj on -ENOENT */
  467. if (ret == -ENOENT)
  468. kgsl_drawobj_destroy(drawobj);
  469. else {
  470. /*
  471. * If we couldn't put it on dispatch queue
  472. * then return it to the context queue
  473. */
  474. int r = hwsched_dispatcher_requeue_drawobj(
  475. drawctxt, drawobj);
  476. if (r)
  477. ret = r;
  478. }
  479. break;
  480. }
  481. if (cmdobj)
  482. drawctxt->submitted_timestamp = drawobj->timestamp;
  483. count++;
  484. }
  485. /*
  486. * Wake up any snoozing threads if we have consumed any real commands
  487. * or marker commands and we have room in the context queue.
  488. */
  489. if (_check_context_queue(drawctxt, 0))
  490. wake_up_all(&drawctxt->wq);
  491. if (!ret)
  492. ret = count;
  493. /* Return error or the number of commands queued */
  494. return ret;
  495. }
  496. static void hwsched_handle_jobs_list(struct adreno_device *adreno_dev,
  497. int id, unsigned long *map, struct llist_node *list)
  498. {
  499. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  500. struct adreno_dispatch_job *job, *next;
  501. if (!list)
  502. return;
  503. /* Reverse the list so we deal with oldest submitted contexts first */
  504. list = llist_reverse_order(list);
  505. llist_for_each_entry_safe(job, next, list, node) {
  506. int ret;
  507. if (kgsl_context_is_bad(&job->drawctxt->base)) {
  508. kgsl_context_put(&job->drawctxt->base);
  509. kmem_cache_free(jobs_cache, job);
  510. continue;
  511. }
  512. /*
  513. * Due to the nature of the lockless queue the same context
  514. * might have multiple jobs on the list. We allow this so we
  515. * don't have to query the list on the producer side but on the
  516. * consumer side we only want each context to be considered
  517. * once. Use a bitmap to remember which contexts we've already
  518. * seen and quietly discard duplicate jobs
  519. */
  520. if (test_and_set_bit(job->drawctxt->base.id, map)) {
  521. kgsl_context_put(&job->drawctxt->base);
  522. kmem_cache_free(jobs_cache, job);
  523. continue;
  524. }
  525. ret = hwsched_sendcmds(adreno_dev, job->drawctxt);
  526. /*
  527. * If the context had nothing queued or the context has been
  528. * destroyed then drop the job
  529. */
  530. if (!ret || ret == -ENOENT) {
  531. kgsl_context_put(&job->drawctxt->base);
  532. kmem_cache_free(jobs_cache, job);
  533. continue;
  534. }
  535. /*
  536. * If the dispatch queue is full then requeue the job to be
  537. * considered first next time. Otherwise the context
  538. * either successfully submmitted to the GPU or another error
  539. * happened and it should go back on the regular queue
  540. */
  541. if (ret == -ENOSPC)
  542. llist_add(&job->node, &hwsched->requeue[id]);
  543. else
  544. llist_add(&job->node, &hwsched->jobs[id]);
  545. }
  546. }
  547. static void hwsched_handle_jobs(struct adreno_device *adreno_dev, int id)
  548. {
  549. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  550. unsigned long map[BITS_TO_LONGS(KGSL_MEMSTORE_MAX)];
  551. struct llist_node *requeue, *jobs;
  552. memset(map, 0, sizeof(map));
  553. requeue = llist_del_all(&hwsched->requeue[id]);
  554. jobs = llist_del_all(&hwsched->jobs[id]);
  555. hwsched_handle_jobs_list(adreno_dev, id, map, requeue);
  556. hwsched_handle_jobs_list(adreno_dev, id, map, jobs);
  557. }
  558. /**
  559. * hwsched_issuecmds() - Issue commmands from pending contexts
  560. * @adreno_dev: Pointer to the adreno device struct
  561. *
  562. * Issue as many commands as possible (up to inflight) from the pending contexts
  563. * This function assumes the dispatcher mutex has been locked.
  564. */
  565. static void hwsched_issuecmds(struct adreno_device *adreno_dev)
  566. {
  567. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  568. int i;
  569. for (i = 0; i < ARRAY_SIZE(hwsched->jobs); i++)
  570. hwsched_handle_jobs(adreno_dev, i);
  571. }
  572. void adreno_hwsched_trigger(struct adreno_device *adreno_dev)
  573. {
  574. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  575. kthread_queue_work(hwsched->worker, &hwsched->work);
  576. }
  577. static inline void _decrement_submit_now(struct kgsl_device *device)
  578. {
  579. spin_lock(&device->submit_lock);
  580. device->submit_now--;
  581. spin_unlock(&device->submit_lock);
  582. }
  583. u32 adreno_hwsched_gpu_fault(struct adreno_device *adreno_dev)
  584. {
  585. /* make sure we're reading the latest value */
  586. smp_rmb();
  587. return atomic_read(&adreno_dev->hwsched.fault);
  588. }
  589. /**
  590. * adreno_hwsched_issuecmds() - Issue commmands from pending contexts
  591. * @adreno_dev: Pointer to the adreno device struct
  592. *
  593. * Lock the dispatcher and call hwsched_issuecmds
  594. */
  595. static void adreno_hwsched_issuecmds(struct adreno_device *adreno_dev)
  596. {
  597. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  598. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  599. spin_lock(&device->submit_lock);
  600. /* If GPU state is not ACTIVE, schedule the work for later */
  601. if (device->skip_inline_submit) {
  602. spin_unlock(&device->submit_lock);
  603. goto done;
  604. }
  605. device->submit_now++;
  606. spin_unlock(&device->submit_lock);
  607. /* If the dispatcher is busy then schedule the work for later */
  608. if (!mutex_trylock(&hwsched->mutex)) {
  609. _decrement_submit_now(device);
  610. goto done;
  611. }
  612. if (!adreno_hwsched_gpu_fault(adreno_dev))
  613. hwsched_issuecmds(adreno_dev);
  614. if (hwsched->inflight > 0) {
  615. mutex_lock(&device->mutex);
  616. kgsl_pwrscale_update(device);
  617. kgsl_start_idle_timer(device);
  618. mutex_unlock(&device->mutex);
  619. }
  620. mutex_unlock(&hwsched->mutex);
  621. _decrement_submit_now(device);
  622. return;
  623. done:
  624. adreno_hwsched_trigger(adreno_dev);
  625. }
  626. /**
  627. * get_timestamp() - Return the next timestamp for the context
  628. * @drawctxt - Pointer to an adreno draw context struct
  629. * @drawobj - Pointer to a drawobj
  630. * @timestamp - Pointer to a timestamp value possibly passed from the user
  631. * @user_ts - user generated timestamp
  632. *
  633. * Assign a timestamp based on the settings of the draw context and the command
  634. * batch.
  635. */
  636. static int get_timestamp(struct adreno_context *drawctxt,
  637. struct kgsl_drawobj *drawobj, unsigned int *timestamp,
  638. unsigned int user_ts)
  639. {
  640. if (drawctxt->base.flags & KGSL_CONTEXT_USER_GENERATED_TS) {
  641. /*
  642. * User specified timestamps need to be greater than the last
  643. * issued timestamp in the context
  644. */
  645. if (timestamp_cmp(drawctxt->timestamp, user_ts) >= 0)
  646. return -ERANGE;
  647. drawctxt->timestamp = user_ts;
  648. } else
  649. drawctxt->timestamp++;
  650. *timestamp = drawctxt->timestamp;
  651. drawobj->timestamp = *timestamp;
  652. return 0;
  653. }
  654. static inline int _wait_for_room_in_context_queue(
  655. struct adreno_context *drawctxt, u32 count)
  656. {
  657. int ret = 0;
  658. /*
  659. * There is always a possibility that dispatcher may end up pushing
  660. * the last popped draw object back to the context drawqueue. Hence,
  661. * we can only queue up to _context_drawqueue_size - 1 here to make
  662. * sure we never let drawqueue->queued exceed _context_drawqueue_size.
  663. */
  664. if ((drawctxt->queued + count) > (_context_drawqueue_size - 1)) {
  665. trace_adreno_drawctxt_sleep(drawctxt);
  666. spin_unlock(&drawctxt->lock);
  667. ret = wait_event_interruptible_timeout(drawctxt->wq,
  668. _check_context_queue(drawctxt, count),
  669. msecs_to_jiffies(_context_queue_wait));
  670. spin_lock(&drawctxt->lock);
  671. trace_adreno_drawctxt_wake(drawctxt);
  672. /*
  673. * Account for the possibility that the context got invalidated
  674. * while we were sleeping
  675. */
  676. if (ret > 0)
  677. ret = kgsl_check_context_state(&drawctxt->base);
  678. else if (ret == 0)
  679. ret = -ETIMEDOUT;
  680. }
  681. return ret;
  682. }
  683. static unsigned int _check_context_state_to_queue_cmds(
  684. struct adreno_context *drawctxt, u32 count)
  685. {
  686. int ret = kgsl_check_context_state(&drawctxt->base);
  687. if (ret)
  688. return ret;
  689. return _wait_for_room_in_context_queue(drawctxt, count);
  690. }
  691. static void _queue_drawobj(struct adreno_context *drawctxt,
  692. struct kgsl_drawobj *drawobj)
  693. {
  694. struct kgsl_context *context = drawobj->context;
  695. /* Put the command into the queue */
  696. drawctxt->drawqueue[drawctxt->drawqueue_tail] = drawobj;
  697. drawctxt->drawqueue_tail = (drawctxt->drawqueue_tail + 1) %
  698. ADRENO_CONTEXT_DRAWQUEUE_SIZE;
  699. drawctxt->queued++;
  700. msm_perf_events_update(MSM_PERF_GFX, MSM_PERF_QUEUE,
  701. pid_nr(context->proc_priv->pid),
  702. context->id, drawobj->timestamp,
  703. !!(drawobj->flags & KGSL_DRAWOBJ_END_OF_FRAME));
  704. trace_adreno_cmdbatch_queued(drawobj, drawctxt->queued);
  705. }
  706. static int _queue_cmdobj(struct adreno_device *adreno_dev,
  707. struct adreno_context *drawctxt, struct kgsl_drawobj_cmd *cmdobj,
  708. uint32_t *timestamp, unsigned int user_ts)
  709. {
  710. struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
  711. u32 j;
  712. int ret;
  713. ret = get_timestamp(drawctxt, drawobj, timestamp, user_ts);
  714. if (ret)
  715. return ret;
  716. /*
  717. * If this is a real command then we need to force any markers
  718. * queued before it to dispatch to keep time linear - set the
  719. * skip bit so the commands get NOPed.
  720. */
  721. j = drawctxt->drawqueue_head;
  722. while (j != drawctxt->drawqueue_tail) {
  723. if (drawctxt->drawqueue[j]->type == MARKEROBJ_TYPE) {
  724. struct kgsl_drawobj_cmd *markerobj =
  725. CMDOBJ(drawctxt->drawqueue[j]);
  726. set_bit(CMDOBJ_SKIP, &markerobj->priv);
  727. }
  728. j = DRAWQUEUE_NEXT(j, ADRENO_CONTEXT_DRAWQUEUE_SIZE);
  729. }
  730. drawctxt->queued_timestamp = *timestamp;
  731. _queue_drawobj(drawctxt, drawobj);
  732. return 0;
  733. }
  734. static void _queue_syncobj(struct adreno_context *drawctxt,
  735. struct kgsl_drawobj_sync *syncobj, uint32_t *timestamp)
  736. {
  737. struct kgsl_drawobj *drawobj = DRAWOBJ(syncobj);
  738. *timestamp = 0;
  739. drawobj->timestamp = 0;
  740. _queue_drawobj(drawctxt, drawobj);
  741. }
  742. static int _queue_markerobj(struct adreno_device *adreno_dev,
  743. struct adreno_context *drawctxt, struct kgsl_drawobj_cmd *markerobj,
  744. u32 *timestamp, u32 user_ts)
  745. {
  746. struct kgsl_drawobj *drawobj = DRAWOBJ(markerobj);
  747. int ret;
  748. ret = get_timestamp(drawctxt, drawobj, timestamp, user_ts);
  749. if (ret)
  750. return ret;
  751. /*
  752. * See if we can fastpath this thing - if nothing is queued
  753. * and nothing is inflight retire without bothering the GPU
  754. */
  755. if (!drawctxt->queued && kgsl_check_timestamp(drawobj->device,
  756. drawobj->context, drawctxt->queued_timestamp)) {
  757. _retire_timestamp(drawobj);
  758. return 1;
  759. }
  760. /*
  761. * Remember the last queued timestamp - the marker will block
  762. * until that timestamp is expired (unless another command
  763. * comes along and forces the marker to execute)
  764. */
  765. markerobj->marker_timestamp = drawctxt->queued_timestamp;
  766. drawctxt->queued_timestamp = *timestamp;
  767. _queue_drawobj(drawctxt, drawobj);
  768. return 0;
  769. }
  770. static int _queue_bindobj(struct adreno_context *drawctxt,
  771. struct kgsl_drawobj *drawobj, u32 *timestamp, u32 user_ts)
  772. {
  773. int ret;
  774. ret = get_timestamp(drawctxt, drawobj, timestamp, user_ts);
  775. if (ret)
  776. return ret;
  777. drawctxt->queued_timestamp = *timestamp;
  778. _queue_drawobj(drawctxt, drawobj);
  779. return 0;
  780. }
  781. static void _queue_timelineobj(struct adreno_context *drawctxt,
  782. struct kgsl_drawobj *drawobj)
  783. {
  784. /*
  785. * This drawobj is not submitted to the GPU so use a timestamp of 0.
  786. * Update the timestamp through a subsequent marker to keep userspace
  787. * happy.
  788. */
  789. drawobj->timestamp = 0;
  790. _queue_drawobj(drawctxt, drawobj);
  791. }
  792. static int adreno_hwsched_queue_cmds(struct kgsl_device_private *dev_priv,
  793. struct kgsl_context *context, struct kgsl_drawobj *drawobj[],
  794. u32 count, u32 *timestamp)
  795. {
  796. struct kgsl_device *device = dev_priv->device;
  797. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  798. struct adreno_context *drawctxt = ADRENO_CONTEXT(context);
  799. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  800. struct adreno_dispatch_job *job;
  801. int ret;
  802. unsigned int i, user_ts;
  803. /*
  804. * There is always a possibility that dispatcher may end up pushing
  805. * the last popped draw object back to the context drawqueue. Hence,
  806. * we can only queue up to _context_drawqueue_size - 1 here to make
  807. * sure we never let drawqueue->queued exceed _context_drawqueue_size.
  808. */
  809. if (!count || count > _context_drawqueue_size - 1)
  810. return -EINVAL;
  811. for (i = 0; i < count; i++) {
  812. struct kgsl_drawobj_cmd *cmdobj;
  813. struct kgsl_memobj_node *ib;
  814. if (!is_cmdobj(drawobj[i]))
  815. continue;
  816. cmdobj = CMDOBJ(drawobj[i]);
  817. list_for_each_entry(ib, &cmdobj->cmdlist, node)
  818. cmdobj->numibs++;
  819. if (cmdobj->numibs > HWSCHED_MAX_IBS)
  820. return -EINVAL;
  821. }
  822. ret = kgsl_check_context_state(&drawctxt->base);
  823. if (ret)
  824. return ret;
  825. ret = adreno_verify_cmdobj(dev_priv, context, drawobj, count);
  826. if (ret)
  827. return ret;
  828. /* wait for the suspend gate */
  829. wait_for_completion(&device->halt_gate);
  830. job = kmem_cache_alloc(jobs_cache, GFP_KERNEL);
  831. if (!job)
  832. return -ENOMEM;
  833. job->drawctxt = drawctxt;
  834. spin_lock(&drawctxt->lock);
  835. ret = _check_context_state_to_queue_cmds(drawctxt, count);
  836. if (ret) {
  837. spin_unlock(&drawctxt->lock);
  838. kmem_cache_free(jobs_cache, job);
  839. return ret;
  840. }
  841. user_ts = *timestamp;
  842. /*
  843. * If there is only one drawobj in the array and it is of
  844. * type SYNCOBJ_TYPE, skip comparing user_ts as it can be 0
  845. */
  846. if (!(count == 1 && drawobj[0]->type == SYNCOBJ_TYPE) &&
  847. (drawctxt->base.flags & KGSL_CONTEXT_USER_GENERATED_TS)) {
  848. /*
  849. * User specified timestamps need to be greater than the last
  850. * issued timestamp in the context
  851. */
  852. if (timestamp_cmp(drawctxt->timestamp, user_ts) >= 0) {
  853. spin_unlock(&drawctxt->lock);
  854. kmem_cache_free(jobs_cache, job);
  855. return -ERANGE;
  856. }
  857. }
  858. for (i = 0; i < count; i++) {
  859. switch (drawobj[i]->type) {
  860. case MARKEROBJ_TYPE:
  861. ret = _queue_markerobj(adreno_dev, drawctxt,
  862. CMDOBJ(drawobj[i]),
  863. timestamp, user_ts);
  864. if (ret == 1) {
  865. spin_unlock(&drawctxt->lock);
  866. kmem_cache_free(jobs_cache, job);
  867. return 0;
  868. } else if (ret) {
  869. spin_unlock(&drawctxt->lock);
  870. kmem_cache_free(jobs_cache, job);
  871. return ret;
  872. }
  873. break;
  874. case CMDOBJ_TYPE:
  875. ret = _queue_cmdobj(adreno_dev, drawctxt,
  876. CMDOBJ(drawobj[i]),
  877. timestamp, user_ts);
  878. if (ret) {
  879. spin_unlock(&drawctxt->lock);
  880. kmem_cache_free(jobs_cache, job);
  881. return ret;
  882. }
  883. break;
  884. case SYNCOBJ_TYPE:
  885. _queue_syncobj(drawctxt, SYNCOBJ(drawobj[i]),
  886. timestamp);
  887. break;
  888. case BINDOBJ_TYPE:
  889. ret = _queue_bindobj(drawctxt, drawobj[i], timestamp,
  890. user_ts);
  891. if (ret) {
  892. spin_unlock(&drawctxt->lock);
  893. kmem_cache_free(jobs_cache, job);
  894. return ret;
  895. }
  896. break;
  897. case TIMELINEOBJ_TYPE:
  898. _queue_timelineobj(drawctxt, drawobj[i]);
  899. break;
  900. default:
  901. spin_unlock(&drawctxt->lock);
  902. kmem_cache_free(jobs_cache, job);
  903. return -EINVAL;
  904. }
  905. }
  906. adreno_track_context(adreno_dev, NULL, drawctxt);
  907. spin_unlock(&drawctxt->lock);
  908. /* Add the context to the dispatcher pending list */
  909. if (_kgsl_context_get(&drawctxt->base)) {
  910. trace_dispatch_queue_context(drawctxt);
  911. llist_add(&job->node, &hwsched->jobs[drawctxt->base.priority]);
  912. adreno_hwsched_issuecmds(adreno_dev);
  913. } else
  914. kmem_cache_free(jobs_cache, job);
  915. return 0;
  916. }
  917. void adreno_hwsched_retire_cmdobj(struct adreno_hwsched *hwsched,
  918. struct kgsl_drawobj_cmd *cmdobj)
  919. {
  920. struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
  921. struct kgsl_mem_entry *entry;
  922. struct kgsl_drawobj_profiling_buffer *profile_buffer;
  923. struct kgsl_context *context = drawobj->context;
  924. msm_perf_events_update(MSM_PERF_GFX, MSM_PERF_RETIRED,
  925. pid_nr(context->proc_priv->pid),
  926. context->id, drawobj->timestamp,
  927. !!(drawobj->flags & KGSL_DRAWOBJ_END_OF_FRAME));
  928. if (drawobj->flags & KGSL_DRAWOBJ_END_OF_FRAME) {
  929. atomic64_inc(&drawobj->context->proc_priv->frame_count);
  930. atomic_inc(&drawobj->context->proc_priv->period->frames);
  931. }
  932. entry = cmdobj->profiling_buf_entry;
  933. if (entry) {
  934. profile_buffer = kgsl_gpuaddr_to_vaddr(&entry->memdesc,
  935. cmdobj->profiling_buffer_gpuaddr);
  936. if (profile_buffer == NULL)
  937. return;
  938. kgsl_memdesc_unmap(&entry->memdesc);
  939. }
  940. trace_adreno_cmdbatch_done(drawobj->context->id,
  941. drawobj->context->priority, drawobj->timestamp);
  942. if (hwsched->big_cmdobj == cmdobj) {
  943. hwsched->big_cmdobj = NULL;
  944. kgsl_drawobj_put(drawobj);
  945. }
  946. kgsl_drawobj_destroy(drawobj);
  947. }
  948. static bool drawobj_retired(struct adreno_device *adreno_dev,
  949. struct kgsl_drawobj *drawobj)
  950. {
  951. struct adreno_context *drawctxt = ADRENO_CONTEXT(drawobj->context);
  952. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  953. struct kgsl_drawobj_cmd *cmdobj;
  954. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  955. if ((drawobj->type & SYNCOBJ_TYPE) != 0) {
  956. struct gmu_context_queue_header *hdr =
  957. drawctxt->gmu_context_queue.hostptr;
  958. if (timestamp_cmp(drawobj->timestamp, hdr->sync_obj_ts) > 0)
  959. return false;
  960. trace_adreno_syncobj_retired(drawobj->context->id, drawobj->timestamp);
  961. kgsl_drawobj_destroy(drawobj);
  962. return true;
  963. }
  964. cmdobj = CMDOBJ(drawobj);
  965. if (!kgsl_check_timestamp(device, drawobj->context,
  966. drawobj->timestamp))
  967. return false;
  968. adreno_hwsched_retire_cmdobj(hwsched, cmdobj);
  969. return true;
  970. }
  971. static void retire_drawobj_list(struct adreno_device *adreno_dev)
  972. {
  973. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  974. struct cmd_list_obj *obj, *tmp;
  975. list_for_each_entry_safe(obj, tmp, &hwsched->cmd_list, node) {
  976. struct kgsl_drawobj *drawobj = obj->drawobj;
  977. if (!drawobj_retired(adreno_dev, drawobj))
  978. continue;
  979. list_del_init(&obj->node);
  980. kmem_cache_free(obj_cache, obj);
  981. hwsched->inflight--;
  982. }
  983. }
  984. /* Take down the dispatcher and release any power states */
  985. static void hwsched_power_down(struct adreno_device *adreno_dev)
  986. {
  987. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  988. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  989. mutex_lock(&device->mutex);
  990. if (test_and_clear_bit(ADRENO_HWSCHED_ACTIVE, &hwsched->flags))
  991. complete_all(&hwsched->idle_gate);
  992. if (test_bit(ADRENO_HWSCHED_POWER, &hwsched->flags)) {
  993. adreno_active_count_put(adreno_dev);
  994. clear_bit(ADRENO_HWSCHED_POWER, &hwsched->flags);
  995. }
  996. mutex_unlock(&device->mutex);
  997. }
  998. static void adreno_hwsched_queue_context(struct adreno_device *adreno_dev,
  999. struct adreno_context *drawctxt)
  1000. {
  1001. hwsched_queue_context(adreno_dev, drawctxt);
  1002. adreno_hwsched_trigger(adreno_dev);
  1003. }
  1004. void adreno_hwsched_start(struct adreno_device *adreno_dev)
  1005. {
  1006. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  1007. complete_all(&device->halt_gate);
  1008. adreno_hwsched_trigger(adreno_dev);
  1009. }
  1010. static void change_preemption(struct adreno_device *adreno_dev, void *priv)
  1011. {
  1012. change_bit(ADRENO_DEVICE_PREEMPTION, &adreno_dev->priv);
  1013. }
  1014. static int _preemption_store(struct adreno_device *adreno_dev, bool val)
  1015. {
  1016. if (!adreno_preemption_feature_set(adreno_dev) ||
  1017. (test_bit(ADRENO_DEVICE_PREEMPTION, &adreno_dev->priv) == val))
  1018. return 0;
  1019. return adreno_power_cycle(adreno_dev, change_preemption, NULL);
  1020. }
  1021. static bool _preemption_show(struct adreno_device *adreno_dev)
  1022. {
  1023. return adreno_is_preemption_enabled(adreno_dev);
  1024. }
  1025. static unsigned int _preempt_count_show(struct adreno_device *adreno_dev)
  1026. {
  1027. const struct adreno_hwsched_ops *hwsched_ops =
  1028. adreno_dev->hwsched.hwsched_ops;
  1029. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  1030. u32 count;
  1031. mutex_lock(&device->mutex);
  1032. count = hwsched_ops->preempt_count(adreno_dev);
  1033. mutex_unlock(&device->mutex);
  1034. return count;
  1035. }
  1036. static int _ft_long_ib_detect_store(struct adreno_device *adreno_dev, bool val)
  1037. {
  1038. return adreno_power_cycle_bool(adreno_dev, &adreno_dev->long_ib_detect,
  1039. val);
  1040. }
  1041. static bool _ft_long_ib_detect_show(struct adreno_device *adreno_dev)
  1042. {
  1043. return adreno_dev->long_ib_detect;
  1044. }
  1045. static ADRENO_SYSFS_BOOL(preemption);
  1046. static ADRENO_SYSFS_RO_U32(preempt_count);
  1047. static ADRENO_SYSFS_BOOL(ft_long_ib_detect);
  1048. static const struct attribute *_hwsched_attr_list[] = {
  1049. &adreno_attr_preemption.attr.attr,
  1050. &adreno_attr_preempt_count.attr.attr,
  1051. &adreno_attr_ft_long_ib_detect.attr.attr,
  1052. NULL,
  1053. };
  1054. void adreno_hwsched_deregister_hw_fence(struct adreno_device *adreno_dev)
  1055. {
  1056. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  1057. struct adreno_hw_fence *hw_fence = &hwsched->hw_fence;
  1058. if (!test_bit(ADRENO_HWSCHED_HW_FENCE, &hwsched->flags))
  1059. return;
  1060. msm_hw_fence_deregister(hwsched->hw_fence.handle);
  1061. if (hw_fence->memdesc.sgt)
  1062. sg_free_table(hw_fence->memdesc.sgt);
  1063. memset(&hw_fence->memdesc, 0x0, sizeof(hw_fence->memdesc));
  1064. kmem_cache_destroy(hwsched->hw_fence_cache);
  1065. clear_bit(ADRENO_HWSCHED_HW_FENCE, &hwsched->flags);
  1066. }
  1067. static void adreno_hwsched_dispatcher_close(struct adreno_device *adreno_dev)
  1068. {
  1069. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  1070. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  1071. if (!IS_ERR_OR_NULL(hwsched->worker))
  1072. kthread_destroy_worker(hwsched->worker);
  1073. adreno_set_dispatch_ops(adreno_dev, NULL);
  1074. kmem_cache_destroy(jobs_cache);
  1075. kmem_cache_destroy(obj_cache);
  1076. sysfs_remove_files(&device->dev->kobj, _hwsched_attr_list);
  1077. kfree(hwsched->ctxt_bad);
  1078. adreno_hwsched_deregister_hw_fence(adreno_dev);
  1079. if (hwsched->global_ctxtq.hostptr)
  1080. kgsl_sharedmem_free(&hwsched->global_ctxtq);
  1081. }
  1082. static void force_retire_timestamp(struct kgsl_device *device,
  1083. struct kgsl_drawobj *drawobj)
  1084. {
  1085. kgsl_sharedmem_writel(device->memstore,
  1086. KGSL_MEMSTORE_OFFSET(drawobj->context->id, soptimestamp),
  1087. drawobj->timestamp);
  1088. kgsl_sharedmem_writel(device->memstore,
  1089. KGSL_MEMSTORE_OFFSET(drawobj->context->id, eoptimestamp),
  1090. drawobj->timestamp);
  1091. }
  1092. /* Return true if drawobj needs to replayed, false otherwise */
  1093. static bool drawobj_replay(struct adreno_device *adreno_dev,
  1094. struct kgsl_drawobj *drawobj)
  1095. {
  1096. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  1097. struct kgsl_drawobj_cmd *cmdobj;
  1098. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  1099. if ((drawobj->type & SYNCOBJ_TYPE) != 0) {
  1100. if (kgsl_drawobj_events_pending(SYNCOBJ(drawobj)))
  1101. return true;
  1102. trace_adreno_syncobj_retired(drawobj->context->id, drawobj->timestamp);
  1103. kgsl_drawobj_destroy(drawobj);
  1104. return false;
  1105. }
  1106. cmdobj = CMDOBJ(drawobj);
  1107. if (kgsl_check_timestamp(device, drawobj->context,
  1108. drawobj->timestamp) || kgsl_context_is_bad(drawobj->context)) {
  1109. adreno_hwsched_retire_cmdobj(hwsched, cmdobj);
  1110. return false;
  1111. }
  1112. return true;
  1113. }
  1114. void adreno_hwsched_replay(struct adreno_device *adreno_dev)
  1115. {
  1116. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  1117. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  1118. const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
  1119. struct cmd_list_obj *obj, *tmp;
  1120. u32 retired = 0;
  1121. list_for_each_entry_safe(obj, tmp, &hwsched->cmd_list, node) {
  1122. struct kgsl_drawobj *drawobj = obj->drawobj;
  1123. /*
  1124. * Get rid of retired objects or objects that belong to detached
  1125. * or invalidated contexts
  1126. */
  1127. if (drawobj_replay(adreno_dev, drawobj)) {
  1128. hwsched->hwsched_ops->submit_drawobj(adreno_dev, drawobj);
  1129. continue;
  1130. }
  1131. retired++;
  1132. list_del_init(&obj->node);
  1133. kmem_cache_free(obj_cache, obj);
  1134. hwsched->inflight--;
  1135. }
  1136. if (hwsched->recurring_cmdobj) {
  1137. u32 event;
  1138. if (kgsl_context_invalid(
  1139. hwsched->recurring_cmdobj->base.context)) {
  1140. clear_bit(CMDOBJ_RECURRING_START,
  1141. &hwsched->recurring_cmdobj->priv);
  1142. set_bit(CMDOBJ_RECURRING_STOP,
  1143. &hwsched->recurring_cmdobj->priv);
  1144. event = GPU_SSR_FATAL;
  1145. } else {
  1146. event = GPU_SSR_END;
  1147. }
  1148. gpudev->send_recurring_cmdobj(adreno_dev,
  1149. hwsched->recurring_cmdobj);
  1150. srcu_notifier_call_chain(&device->nh, event, NULL);
  1151. }
  1152. /* Signal fences */
  1153. if (retired)
  1154. kgsl_process_event_groups(device);
  1155. }
  1156. static void do_fault_header(struct adreno_device *adreno_dev,
  1157. struct kgsl_drawobj *drawobj, int fault)
  1158. {
  1159. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  1160. const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
  1161. struct adreno_context *drawctxt;
  1162. u32 status = 0, rptr = 0, wptr = 0, ib1sz = 0, ib2sz = 0;
  1163. u64 ib1base = 0, ib2base = 0;
  1164. bool gx_on = adreno_gx_is_on(adreno_dev);
  1165. u32 ctxt_id = 0, ts = 0;
  1166. int rb_id = -1;
  1167. dev_err(device->dev, "Fault id:%d and GX is %s\n", fault, gx_on ? "ON" : "OFF");
  1168. if (!gx_on && !drawobj)
  1169. return;
  1170. if (gpudev->fault_header)
  1171. return gpudev->fault_header(adreno_dev, drawobj);
  1172. if (gx_on) {
  1173. adreno_readreg(adreno_dev, ADRENO_REG_RBBM_STATUS, &status);
  1174. adreno_readreg(adreno_dev, ADRENO_REG_CP_RB_RPTR, &rptr);
  1175. adreno_readreg(adreno_dev, ADRENO_REG_CP_RB_WPTR, &wptr);
  1176. adreno_readreg64(adreno_dev, ADRENO_REG_CP_IB1_BASE,
  1177. ADRENO_REG_CP_IB1_BASE_HI, &ib1base);
  1178. adreno_readreg(adreno_dev, ADRENO_REG_CP_IB1_BUFSZ, &ib1sz);
  1179. adreno_readreg64(adreno_dev, ADRENO_REG_CP_IB2_BASE,
  1180. ADRENO_REG_CP_IB2_BASE_HI, &ib2base);
  1181. adreno_readreg(adreno_dev, ADRENO_REG_CP_IB2_BUFSZ, &ib2sz);
  1182. dev_err(device->dev,
  1183. "status %8.8X rb %4.4x/%4.4x ib1 %16.16llX/%4.4x ib2 %16.16llX/%4.4x\n",
  1184. status, rptr, wptr, ib1base, ib1sz, ib2base, ib2sz);
  1185. }
  1186. if (drawobj) {
  1187. drawctxt = ADRENO_CONTEXT(drawobj->context);
  1188. drawobj->context->last_faulted_cmd_ts = drawobj->timestamp;
  1189. drawobj->context->total_fault_count++;
  1190. ctxt_id = drawobj->context->id;
  1191. ts = drawobj->timestamp;
  1192. rb_id = adreno_get_level(drawobj->context);
  1193. pr_context(device, drawobj->context,
  1194. "ctx %u ctx_type %s ts %u policy %lX dispatch_queue=%d\n",
  1195. drawobj->context->id, kgsl_context_type(drawctxt->type),
  1196. drawobj->timestamp, CMDOBJ(drawobj)->fault_recovery,
  1197. drawobj->context->gmu_dispatch_queue);
  1198. pr_context(device, drawobj->context,
  1199. "cmdline: %s\n", drawctxt->base.proc_priv->cmdline);
  1200. }
  1201. trace_adreno_gpu_fault(ctxt_id, ts, status, rptr, wptr, ib1base, ib1sz,
  1202. ib2base, ib2sz, rb_id);
  1203. }
  1204. static struct cmd_list_obj *get_active_cmdobj_lpac(
  1205. struct adreno_device *adreno_dev)
  1206. {
  1207. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  1208. struct cmd_list_obj *obj, *tmp, *active_obj = NULL;
  1209. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  1210. u32 consumed = 0, retired = 0;
  1211. struct kgsl_drawobj *drawobj = NULL;
  1212. list_for_each_entry_safe(obj, tmp, &hwsched->cmd_list, node) {
  1213. drawobj = obj->drawobj;
  1214. if (!(kgsl_context_is_lpac(drawobj->context)))
  1215. continue;
  1216. kgsl_readtimestamp(device, drawobj->context,
  1217. KGSL_TIMESTAMP_CONSUMED, &consumed);
  1218. kgsl_readtimestamp(device, drawobj->context,
  1219. KGSL_TIMESTAMP_RETIRED, &retired);
  1220. if (!consumed)
  1221. continue;
  1222. if (consumed == retired)
  1223. continue;
  1224. /*
  1225. * Find the first submission that started but didn't finish
  1226. * We only care about one ringbuffer for LPAC so just look for the
  1227. * first unfinished submission
  1228. */
  1229. if (!active_obj)
  1230. active_obj = obj;
  1231. }
  1232. if (active_obj) {
  1233. drawobj = active_obj->drawobj;
  1234. if (kref_get_unless_zero(&drawobj->refcount)) {
  1235. struct kgsl_drawobj_cmd *cmdobj = CMDOBJ(drawobj);
  1236. set_bit(CMDOBJ_FAULT, &cmdobj->priv);
  1237. return active_obj;
  1238. }
  1239. }
  1240. return NULL;
  1241. }
  1242. static struct cmd_list_obj *get_active_cmdobj(
  1243. struct adreno_device *adreno_dev)
  1244. {
  1245. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  1246. struct cmd_list_obj *obj, *tmp, *active_obj = NULL;
  1247. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  1248. u32 consumed = 0, retired = 0, prio = UINT_MAX;
  1249. struct kgsl_drawobj *drawobj = NULL;
  1250. list_for_each_entry_safe(obj, tmp, &hwsched->cmd_list, node) {
  1251. drawobj = obj->drawobj;
  1252. /* We track LPAC separately */
  1253. if (!is_cmdobj(drawobj) || kgsl_context_is_lpac(drawobj->context))
  1254. continue;
  1255. kgsl_readtimestamp(device, drawobj->context,
  1256. KGSL_TIMESTAMP_CONSUMED, &consumed);
  1257. kgsl_readtimestamp(device, drawobj->context,
  1258. KGSL_TIMESTAMP_RETIRED, &retired);
  1259. if (!consumed)
  1260. continue;
  1261. if (consumed == retired)
  1262. continue;
  1263. /* Find the first submission that started but didn't finish */
  1264. if (!active_obj) {
  1265. active_obj = obj;
  1266. prio = adreno_get_level(drawobj->context);
  1267. continue;
  1268. }
  1269. /* Find the highest priority active submission */
  1270. if (adreno_get_level(drawobj->context) < prio) {
  1271. active_obj = obj;
  1272. prio = adreno_get_level(drawobj->context);
  1273. }
  1274. }
  1275. if (active_obj) {
  1276. struct kgsl_drawobj_cmd *cmdobj;
  1277. drawobj = active_obj->drawobj;
  1278. cmdobj = CMDOBJ(drawobj);
  1279. if (kref_get_unless_zero(&drawobj->refcount)) {
  1280. set_bit(CMDOBJ_FAULT, &cmdobj->priv);
  1281. return active_obj;
  1282. }
  1283. }
  1284. return NULL;
  1285. }
  1286. static struct cmd_list_obj *get_fault_cmdobj(struct adreno_device *adreno_dev,
  1287. u32 ctxt_id, u32 ts)
  1288. {
  1289. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  1290. struct cmd_list_obj *obj, *tmp;
  1291. list_for_each_entry_safe(obj, tmp, &hwsched->cmd_list, node) {
  1292. struct kgsl_drawobj *drawobj = obj->drawobj;
  1293. if (!is_cmdobj(drawobj))
  1294. continue;
  1295. if ((ctxt_id == drawobj->context->id) &&
  1296. (ts == drawobj->timestamp)) {
  1297. if (kref_get_unless_zero(&drawobj->refcount)) {
  1298. struct kgsl_drawobj_cmd *cmdobj = CMDOBJ(drawobj);
  1299. set_bit(CMDOBJ_FAULT, &cmdobj->priv);
  1300. return obj;
  1301. }
  1302. }
  1303. }
  1304. return NULL;
  1305. }
  1306. static bool context_is_throttled(struct kgsl_device *device,
  1307. struct kgsl_context *context)
  1308. {
  1309. if (ktime_ms_delta(ktime_get(), context->fault_time) >
  1310. _fault_throttle_time) {
  1311. context->fault_time = ktime_get();
  1312. context->fault_count = 1;
  1313. return false;
  1314. }
  1315. context->fault_count++;
  1316. if (context->fault_count > _fault_throttle_burst) {
  1317. pr_context(device, context,
  1318. "gpu fault threshold exceeded %d faults in %d msecs\n",
  1319. _fault_throttle_burst, _fault_throttle_time);
  1320. return true;
  1321. }
  1322. return false;
  1323. }
  1324. static void _print_syncobj(struct adreno_device *adreno_dev, struct kgsl_drawobj *drawobj)
  1325. {
  1326. int i, j, fence_index = 0;
  1327. struct kgsl_drawobj_sync *syncobj = SYNCOBJ(drawobj);
  1328. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  1329. for (i = 0; i < syncobj->numsyncs; i++) {
  1330. struct kgsl_drawobj_sync_event *event = &syncobj->synclist[i];
  1331. struct kgsl_sync_fence_cb *kcb = event->handle;
  1332. struct dma_fence **fences;
  1333. struct dma_fence_array *array;
  1334. u32 num_fences;
  1335. array = to_dma_fence_array(kcb->fence);
  1336. if (array != NULL) {
  1337. num_fences = array->num_fences;
  1338. fences = array->fences;
  1339. } else {
  1340. num_fences = 1;
  1341. fences = &kcb->fence;
  1342. }
  1343. for (j = 0; j < num_fences; j++, fence_index++) {
  1344. bool kgsl = is_kgsl_fence(fences[j]);
  1345. bool signaled = test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fences[j]->flags);
  1346. char value[32] = "unknown";
  1347. if (fences[j]->ops->timeline_value_str)
  1348. fences[j]->ops->timeline_value_str(fences[j], value, sizeof(value));
  1349. dev_err(device->dev,
  1350. "dma fence[%d] signaled:%d kgsl:%d ctx:%llu seqno:%llu value:%s\n",
  1351. fence_index, signaled, kgsl, fences[j]->context, fences[j]->seqno,
  1352. value);
  1353. }
  1354. }
  1355. }
  1356. static void print_fault_syncobj(struct adreno_device *adreno_dev,
  1357. u32 ctxt_id, u32 ts)
  1358. {
  1359. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  1360. struct cmd_list_obj *obj;
  1361. list_for_each_entry(obj, &hwsched->cmd_list, node) {
  1362. struct kgsl_drawobj *drawobj = obj->drawobj;
  1363. if (drawobj->type == SYNCOBJ_TYPE) {
  1364. if ((ctxt_id == drawobj->context->id) &&
  1365. (ts == drawobj->timestamp))
  1366. _print_syncobj(adreno_dev, drawobj);
  1367. }
  1368. }
  1369. }
  1370. static void adreno_hwsched_reset_and_snapshot_legacy(struct adreno_device *adreno_dev, int fault)
  1371. {
  1372. struct kgsl_drawobj *drawobj = NULL;
  1373. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  1374. struct kgsl_context *context = NULL;
  1375. struct cmd_list_obj *obj;
  1376. const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
  1377. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  1378. struct hfi_context_bad_cmd_legacy *cmd = hwsched->ctxt_bad;
  1379. if (device->state != KGSL_STATE_ACTIVE)
  1380. return;
  1381. if (hwsched->recurring_cmdobj)
  1382. srcu_notifier_call_chain(&device->nh, GPU_SSR_BEGIN, NULL);
  1383. if (cmd->error == GMU_SYNCOBJ_TIMEOUT_ERROR) {
  1384. print_fault_syncobj(adreno_dev, cmd->ctxt_id, cmd->ts);
  1385. gmu_core_fault_snapshot(device);
  1386. goto done;
  1387. }
  1388. /*
  1389. * First, try to see if the faulted command object is marked
  1390. * in case there was a context bad hfi. But, with stall-on-fault,
  1391. * we know that GMU cannot send context bad hfi. Hence, attempt
  1392. * to walk the list of active submissions to find the one that
  1393. * faulted.
  1394. */
  1395. obj = get_fault_cmdobj(adreno_dev, cmd->ctxt_id, cmd->ts);
  1396. if (!obj && (fault & ADRENO_IOMMU_PAGE_FAULT))
  1397. obj = get_active_cmdobj(adreno_dev);
  1398. if (obj) {
  1399. drawobj = obj->drawobj;
  1400. trace_adreno_cmdbatch_fault(CMDOBJ(drawobj), fault);
  1401. } else if (hwsched->recurring_cmdobj &&
  1402. hwsched->recurring_cmdobj->base.context->id == cmd->ctxt_id) {
  1403. drawobj = DRAWOBJ(hwsched->recurring_cmdobj);
  1404. trace_adreno_cmdbatch_fault(hwsched->recurring_cmdobj, fault);
  1405. if (!kref_get_unless_zero(&drawobj->refcount))
  1406. drawobj = NULL;
  1407. }
  1408. if (!drawobj) {
  1409. if (fault & ADRENO_GMU_FAULT)
  1410. gmu_core_fault_snapshot(device);
  1411. else
  1412. kgsl_device_snapshot(device, NULL, NULL, false);
  1413. goto done;
  1414. }
  1415. context = drawobj->context;
  1416. do_fault_header(adreno_dev, drawobj, fault);
  1417. kgsl_device_snapshot(device, context, NULL, false);
  1418. force_retire_timestamp(device, drawobj);
  1419. if ((context->flags & KGSL_CONTEXT_INVALIDATE_ON_FAULT) ||
  1420. (context->flags & KGSL_CONTEXT_NO_FAULT_TOLERANCE) ||
  1421. (cmd->error == GMU_GPU_SW_HANG) ||
  1422. (cmd->error == GMU_GPU_SW_FUSE_VIOLATION) ||
  1423. context_is_throttled(device, context)) {
  1424. adreno_drawctxt_set_guilty(device, context);
  1425. }
  1426. /*
  1427. * Put back the reference which we incremented while trying to find
  1428. * faulted command object
  1429. */
  1430. kgsl_drawobj_put(drawobj);
  1431. done:
  1432. memset(hwsched->ctxt_bad, 0x0, HFI_MAX_MSG_SIZE);
  1433. gpudev->reset(adreno_dev);
  1434. }
  1435. static void adreno_hwsched_reset_and_snapshot(struct adreno_device *adreno_dev, int fault)
  1436. {
  1437. struct kgsl_drawobj *drawobj = NULL;
  1438. struct kgsl_drawobj *drawobj_lpac = NULL;
  1439. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  1440. struct kgsl_context *context = NULL;
  1441. struct kgsl_context *context_lpac = NULL;
  1442. struct cmd_list_obj *obj;
  1443. struct cmd_list_obj *obj_lpac;
  1444. const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
  1445. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  1446. struct hfi_context_bad_cmd *cmd = hwsched->ctxt_bad;
  1447. if (device->state != KGSL_STATE_ACTIVE)
  1448. return;
  1449. if (hwsched->recurring_cmdobj)
  1450. srcu_notifier_call_chain(&device->nh, GPU_SSR_BEGIN, NULL);
  1451. if (cmd->error == GMU_SYNCOBJ_TIMEOUT_ERROR) {
  1452. print_fault_syncobj(adreno_dev, cmd->gc.ctxt_id, cmd->gc.ts);
  1453. gmu_core_fault_snapshot(device);
  1454. goto done;
  1455. }
  1456. /*
  1457. * First, try to see if the faulted command object is marked
  1458. * in case there was a context bad hfi. But, with stall-on-fault,
  1459. * we know that GMU cannot send context bad hfi. Hence, attempt
  1460. * to walk the list of active submissions to find the one that
  1461. * faulted.
  1462. */
  1463. obj = get_fault_cmdobj(adreno_dev, cmd->gc.ctxt_id, cmd->gc.ts);
  1464. obj_lpac = get_fault_cmdobj(adreno_dev, cmd->lpac.ctxt_id, cmd->lpac.ts);
  1465. if (!obj && (fault & ADRENO_IOMMU_PAGE_FAULT))
  1466. obj = get_active_cmdobj(adreno_dev);
  1467. if (obj) {
  1468. drawobj = obj->drawobj;
  1469. CMDOBJ(drawobj)->fault_recovery = cmd->gc.policy;
  1470. } else if (hwsched->recurring_cmdobj &&
  1471. hwsched->recurring_cmdobj->base.context->id == cmd->gc.ctxt_id) {
  1472. drawobj = DRAWOBJ(hwsched->recurring_cmdobj);
  1473. CMDOBJ(drawobj)->fault_recovery = cmd->gc.policy;
  1474. if (!kref_get_unless_zero(&drawobj->refcount))
  1475. drawobj = NULL;
  1476. }
  1477. do_fault_header(adreno_dev, drawobj, fault);
  1478. if (!obj_lpac && (fault & ADRENO_IOMMU_PAGE_FAULT))
  1479. obj_lpac = get_active_cmdobj_lpac(adreno_dev);
  1480. if (!obj && !obj_lpac) {
  1481. if (fault & ADRENO_GMU_FAULT)
  1482. gmu_core_fault_snapshot(device);
  1483. else
  1484. kgsl_device_snapshot(device, NULL, NULL, false);
  1485. goto done;
  1486. }
  1487. if (obj)
  1488. context = drawobj->context;
  1489. if (obj_lpac) {
  1490. drawobj_lpac = obj_lpac->drawobj;
  1491. CMDOBJ(drawobj_lpac)->fault_recovery = cmd->lpac.policy;
  1492. context_lpac = drawobj_lpac->context;
  1493. if (gpudev->lpac_fault_header)
  1494. gpudev->lpac_fault_header(adreno_dev, drawobj_lpac);
  1495. }
  1496. kgsl_device_snapshot(device, context, context_lpac, false);
  1497. if (drawobj) {
  1498. force_retire_timestamp(device, drawobj);
  1499. if (context && ((context->flags & KGSL_CONTEXT_INVALIDATE_ON_FAULT) ||
  1500. (context->flags & KGSL_CONTEXT_NO_FAULT_TOLERANCE) ||
  1501. (cmd->error == GMU_GPU_SW_HANG) ||
  1502. (cmd->error == GMU_GPU_SW_FUSE_VIOLATION) ||
  1503. context_is_throttled(device, context)))
  1504. adreno_drawctxt_set_guilty(device, context);
  1505. /*
  1506. * Put back the reference which we incremented while trying to find
  1507. * faulted command object
  1508. */
  1509. kgsl_drawobj_put(drawobj);
  1510. }
  1511. if (drawobj_lpac) {
  1512. force_retire_timestamp(device, drawobj_lpac);
  1513. if (context_lpac && ((context_lpac->flags & KGSL_CONTEXT_INVALIDATE_ON_FAULT) ||
  1514. (context_lpac->flags & KGSL_CONTEXT_NO_FAULT_TOLERANCE) ||
  1515. (cmd->error == GMU_GPU_SW_HANG) ||
  1516. (cmd->error == GMU_GPU_SW_FUSE_VIOLATION) ||
  1517. context_is_throttled(device, context_lpac)))
  1518. adreno_drawctxt_set_guilty(device, context_lpac);
  1519. /*
  1520. * Put back the reference which we incremented while trying to find
  1521. * faulted command object
  1522. */
  1523. kgsl_drawobj_put(drawobj_lpac);
  1524. }
  1525. done:
  1526. memset(hwsched->ctxt_bad, 0x0, HFI_MAX_MSG_SIZE);
  1527. gpudev->reset(adreno_dev);
  1528. }
  1529. static bool adreno_hwsched_do_fault(struct adreno_device *adreno_dev)
  1530. {
  1531. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  1532. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  1533. int fault;
  1534. fault = atomic_xchg(&hwsched->fault, 0);
  1535. if (fault == 0)
  1536. return false;
  1537. mutex_lock(&device->mutex);
  1538. if (test_bit(ADRENO_HWSCHED_CTX_BAD_LEGACY, &hwsched->flags))
  1539. adreno_hwsched_reset_and_snapshot_legacy(adreno_dev, fault);
  1540. else
  1541. adreno_hwsched_reset_and_snapshot(adreno_dev, fault);
  1542. adreno_hwsched_trigger(adreno_dev);
  1543. mutex_unlock(&device->mutex);
  1544. return true;
  1545. }
  1546. static void adreno_hwsched_work(struct kthread_work *work)
  1547. {
  1548. struct adreno_hwsched *hwsched = container_of(work,
  1549. struct adreno_hwsched, work);
  1550. struct adreno_device *adreno_dev = container_of(hwsched,
  1551. struct adreno_device, hwsched);
  1552. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  1553. mutex_lock(&hwsched->mutex);
  1554. if (adreno_hwsched_do_fault(adreno_dev)) {
  1555. mutex_unlock(&hwsched->mutex);
  1556. return;
  1557. }
  1558. /*
  1559. * As long as there are inflight commands, process retired comamnds from
  1560. * all drawqueues
  1561. */
  1562. retire_drawobj_list(adreno_dev);
  1563. /* Signal fences */
  1564. kgsl_process_event_groups(device);
  1565. /* Run the scheduler for to dispatch new commands */
  1566. hwsched_issuecmds(adreno_dev);
  1567. if (hwsched->inflight == 0) {
  1568. hwsched_power_down(adreno_dev);
  1569. } else {
  1570. mutex_lock(&device->mutex);
  1571. kgsl_pwrscale_update(device);
  1572. kgsl_start_idle_timer(device);
  1573. mutex_unlock(&device->mutex);
  1574. }
  1575. mutex_unlock(&hwsched->mutex);
  1576. }
  1577. void adreno_hwsched_fault(struct adreno_device *adreno_dev,
  1578. u32 fault)
  1579. {
  1580. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  1581. u32 curr = atomic_read(&hwsched->fault);
  1582. atomic_set(&hwsched->fault, curr | fault);
  1583. /* make sure fault is written before triggering dispatcher */
  1584. smp_wmb();
  1585. adreno_hwsched_trigger(adreno_dev);
  1586. }
  1587. void adreno_hwsched_clear_fault(struct adreno_device *adreno_dev)
  1588. {
  1589. atomic_set(&adreno_dev->hwsched.fault, 0);
  1590. /* make sure other CPUs see the update */
  1591. smp_wmb();
  1592. }
  1593. static bool is_tx_slot_available(struct adreno_device *adreno_dev)
  1594. {
  1595. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  1596. void *ptr = hwsched->hw_fence.mem_descriptor.virtual_addr;
  1597. struct msm_hw_fence_hfi_queue_header *hdr = (struct msm_hw_fence_hfi_queue_header *)
  1598. (ptr + sizeof(struct msm_hw_fence_hfi_queue_table_header));
  1599. u32 queue_size_dwords = hdr->queue_size / sizeof(u32);
  1600. u32 payload_size_dwords = hdr->pkt_size / sizeof(u32);
  1601. u32 free_dwords, write_idx = hdr->write_index, read_idx = hdr->read_index;
  1602. u32 reserved_dwords = atomic_read(&hwsched->hw_fence_count) * payload_size_dwords;
  1603. free_dwords = read_idx <= write_idx ?
  1604. queue_size_dwords - (write_idx - read_idx) :
  1605. read_idx - write_idx;
  1606. if (free_dwords - reserved_dwords <= payload_size_dwords)
  1607. return false;
  1608. return true;
  1609. }
  1610. static void adreno_hwsched_create_hw_fence(struct adreno_device *adreno_dev,
  1611. struct kgsl_sync_fence *kfence)
  1612. {
  1613. struct kgsl_sync_timeline *ktimeline = kfence->parent;
  1614. struct kgsl_context *context = ktimeline->context;
  1615. const struct adreno_hwsched_ops *hwsched_ops =
  1616. adreno_dev->hwsched.hwsched_ops;
  1617. if (!test_bit(ADRENO_HWSCHED_HW_FENCE, &adreno_dev->hwsched.flags))
  1618. return;
  1619. /* Do not create a hardware backed fence, if this context is bad or going away */
  1620. if (kgsl_context_is_bad(context))
  1621. return;
  1622. if (!is_tx_slot_available(adreno_dev))
  1623. return;
  1624. hwsched_ops->create_hw_fence(adreno_dev, kfence);
  1625. }
  1626. static const struct adreno_dispatch_ops hwsched_ops = {
  1627. .close = adreno_hwsched_dispatcher_close,
  1628. .queue_cmds = adreno_hwsched_queue_cmds,
  1629. .queue_context = adreno_hwsched_queue_context,
  1630. .fault = adreno_hwsched_fault,
  1631. .create_hw_fence = adreno_hwsched_create_hw_fence,
  1632. .get_fault = adreno_hwsched_gpu_fault,
  1633. };
  1634. static void hwsched_lsr_check(struct work_struct *work)
  1635. {
  1636. struct adreno_hwsched *hwsched = container_of(work,
  1637. struct adreno_hwsched, lsr_check_ws);
  1638. struct adreno_device *adreno_dev = container_of(hwsched,
  1639. struct adreno_device, hwsched);
  1640. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  1641. mutex_lock(&device->mutex);
  1642. kgsl_pwrscale_update_stats(device);
  1643. kgsl_pwrscale_update(device);
  1644. mutex_unlock(&device->mutex);
  1645. mod_timer(&hwsched->lsr_timer, jiffies + msecs_to_jiffies(10));
  1646. }
  1647. static void hwsched_lsr_timer(struct timer_list *t)
  1648. {
  1649. struct adreno_hwsched *hwsched = container_of(t, struct adreno_hwsched,
  1650. lsr_timer);
  1651. kgsl_schedule_work(&hwsched->lsr_check_ws);
  1652. }
  1653. int adreno_hwsched_init(struct adreno_device *adreno_dev,
  1654. const struct adreno_hwsched_ops *target_hwsched_ops)
  1655. {
  1656. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  1657. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  1658. int i;
  1659. memset(hwsched, 0, sizeof(*hwsched));
  1660. hwsched->ctxt_bad = kzalloc(HFI_MAX_MSG_SIZE, GFP_KERNEL);
  1661. if (!hwsched->ctxt_bad)
  1662. return -ENOMEM;
  1663. hwsched->worker = kthread_create_worker(0, "kgsl_hwsched");
  1664. if (IS_ERR(hwsched->worker)) {
  1665. kfree(hwsched->ctxt_bad);
  1666. return PTR_ERR(hwsched->worker);
  1667. }
  1668. mutex_init(&hwsched->mutex);
  1669. kthread_init_work(&hwsched->work, adreno_hwsched_work);
  1670. jobs_cache = KMEM_CACHE(adreno_dispatch_job, 0);
  1671. obj_cache = KMEM_CACHE(cmd_list_obj, 0);
  1672. INIT_LIST_HEAD(&hwsched->cmd_list);
  1673. for (i = 0; i < ARRAY_SIZE(hwsched->jobs); i++) {
  1674. init_llist_head(&hwsched->jobs[i]);
  1675. init_llist_head(&hwsched->requeue[i]);
  1676. }
  1677. sched_set_fifo(hwsched->worker->task);
  1678. WARN_ON(sysfs_create_files(&device->dev->kobj, _hwsched_attr_list));
  1679. adreno_set_dispatch_ops(adreno_dev, &hwsched_ops);
  1680. hwsched->hwsched_ops = target_hwsched_ops;
  1681. init_completion(&hwsched->idle_gate);
  1682. complete_all(&hwsched->idle_gate);
  1683. if (ADRENO_FEATURE(adreno_dev, ADRENO_LSR)) {
  1684. INIT_WORK(&hwsched->lsr_check_ws, hwsched_lsr_check);
  1685. timer_setup(&hwsched->lsr_timer, hwsched_lsr_timer, 0);
  1686. }
  1687. return 0;
  1688. }
  1689. void adreno_hwsched_parse_fault_cmdobj(struct adreno_device *adreno_dev,
  1690. struct kgsl_snapshot *snapshot)
  1691. {
  1692. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  1693. struct cmd_list_obj *obj, *tmp;
  1694. /*
  1695. * During IB parse, vmalloc is called which can sleep and
  1696. * should not be called from atomic context. Since IBs are not
  1697. * dumped during atomic snapshot, there is no need to parse it.
  1698. */
  1699. if (adreno_dev->dev.snapshot_atomic)
  1700. return;
  1701. list_for_each_entry_safe(obj, tmp, &hwsched->cmd_list, node) {
  1702. struct kgsl_drawobj *drawobj = obj->drawobj;
  1703. struct kgsl_drawobj_cmd *cmdobj;
  1704. if (!is_cmdobj(drawobj))
  1705. continue;
  1706. cmdobj = CMDOBJ(drawobj);
  1707. if (test_bit(CMDOBJ_FAULT, &cmdobj->priv)) {
  1708. struct kgsl_memobj_node *ib;
  1709. list_for_each_entry(ib, &cmdobj->cmdlist, node) {
  1710. if (drawobj->context->flags & KGSL_CONTEXT_LPAC)
  1711. adreno_parse_ib_lpac(KGSL_DEVICE(adreno_dev),
  1712. snapshot, snapshot->process_lpac,
  1713. ib->gpuaddr, ib->size >> 2);
  1714. else
  1715. adreno_parse_ib(KGSL_DEVICE(adreno_dev),
  1716. snapshot, snapshot->process,
  1717. ib->gpuaddr, ib->size >> 2);
  1718. }
  1719. clear_bit(CMDOBJ_FAULT, &cmdobj->priv);
  1720. }
  1721. }
  1722. }
  1723. static int unregister_context(int id, void *ptr, void *data)
  1724. {
  1725. struct kgsl_context *context = ptr;
  1726. struct adreno_context *drawctxt = ADRENO_CONTEXT(context);
  1727. if (drawctxt->gmu_context_queue.gmuaddr != 0) {
  1728. struct gmu_context_queue_header *header = drawctxt->gmu_context_queue.hostptr;
  1729. header->read_index = header->write_index;
  1730. /* This is to make sure GMU sees the correct indices after recovery */
  1731. mb();
  1732. }
  1733. /*
  1734. * We don't need to send the unregister hfi packet because
  1735. * we are anyway going to lose the gmu state of registered
  1736. * contexts. So just reset the flag so that the context
  1737. * registers with gmu on its first submission post slumber.
  1738. */
  1739. context->gmu_registered = false;
  1740. /*
  1741. * Consider the scenario where non-recurring submissions were made
  1742. * by a context. Here internal_timestamp of context would be non
  1743. * zero. After slumber, last retired timestamp is not held by GMU.
  1744. * If this context submits a recurring workload, the context is
  1745. * registered again, but the internal timestamp is not updated. When
  1746. * the context is unregistered in send_context_unregister_hfi(),
  1747. * we could be waiting on old internal_timestamp which is not held by
  1748. * GMU. This can result in GMU errors. Hence set internal_timestamp
  1749. * to zero when entering slumber.
  1750. */
  1751. drawctxt->internal_timestamp = 0;
  1752. return 0;
  1753. }
  1754. void adreno_hwsched_unregister_contexts(struct adreno_device *adreno_dev)
  1755. {
  1756. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  1757. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  1758. read_lock(&device->context_lock);
  1759. idr_for_each(&device->context_idr, unregister_context, NULL);
  1760. read_unlock(&device->context_lock);
  1761. if (hwsched->global_ctxtq.hostptr) {
  1762. struct gmu_context_queue_header *header = hwsched->global_ctxtq.hostptr;
  1763. header->read_index = header->write_index;
  1764. /* This is to make sure GMU sees the correct indices after recovery */
  1765. mb();
  1766. }
  1767. hwsched->global_ctxt_gmu_registered = false;
  1768. }
  1769. static int hwsched_idle(struct adreno_device *adreno_dev)
  1770. {
  1771. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  1772. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  1773. int ret;
  1774. /* Block any new submissions from being submitted */
  1775. adreno_get_gpu_halt(adreno_dev);
  1776. mutex_unlock(&device->mutex);
  1777. /*
  1778. * Flush the worker to make sure all executing
  1779. * or pending dispatcher works on worker are
  1780. * finished
  1781. */
  1782. adreno_hwsched_flush(adreno_dev);
  1783. ret = wait_for_completion_timeout(&hwsched->idle_gate,
  1784. msecs_to_jiffies(ADRENO_IDLE_TIMEOUT));
  1785. if (ret == 0) {
  1786. ret = -ETIMEDOUT;
  1787. WARN(1, "hwsched halt timeout\n");
  1788. } else if (ret < 0) {
  1789. dev_err(device->dev, "hwsched halt failed %d\n", ret);
  1790. } else {
  1791. ret = 0;
  1792. }
  1793. mutex_lock(&device->mutex);
  1794. /*
  1795. * This will allow the dispatcher to start submitting to
  1796. * hardware once device mutex is released
  1797. */
  1798. adreno_put_gpu_halt(adreno_dev);
  1799. /*
  1800. * Requeue dispatcher work to resubmit pending commands
  1801. * that may have been blocked due to this idling request
  1802. */
  1803. adreno_hwsched_trigger(adreno_dev);
  1804. return ret;
  1805. }
  1806. int adreno_hwsched_idle(struct adreno_device *adreno_dev)
  1807. {
  1808. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  1809. unsigned long wait = jiffies + msecs_to_jiffies(ADRENO_IDLE_TIMEOUT);
  1810. const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
  1811. int ret;
  1812. if (WARN_ON(!mutex_is_locked(&device->mutex)))
  1813. return -EDEADLK;
  1814. if (!kgsl_state_is_awake(device))
  1815. return 0;
  1816. ret = hwsched_idle(adreno_dev);
  1817. if (ret)
  1818. return ret;
  1819. do {
  1820. if (adreno_hwsched_gpu_fault(adreno_dev))
  1821. return -EDEADLK;
  1822. if (gpudev->hw_isidle(adreno_dev))
  1823. return 0;
  1824. } while (time_before(jiffies, wait));
  1825. /*
  1826. * Under rare conditions, preemption can cause the while loop to exit
  1827. * without checking if the gpu is idle. check one last time before we
  1828. * return failure.
  1829. */
  1830. if (adreno_hwsched_gpu_fault(adreno_dev))
  1831. return -EDEADLK;
  1832. if (gpudev->hw_isidle(adreno_dev))
  1833. return 0;
  1834. return -ETIMEDOUT;
  1835. }
  1836. void adreno_hwsched_register_hw_fence(struct adreno_device *adreno_dev)
  1837. {
  1838. struct adreno_hwsched *hwsched = &adreno_dev->hwsched;
  1839. struct adreno_hw_fence *hw_fence = &hwsched->hw_fence;
  1840. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  1841. int ret;
  1842. if (!ADRENO_FEATURE(adreno_dev, ADRENO_HW_FENCE))
  1843. return;
  1844. /* Enable hardware fences only if context queues are enabled */
  1845. if (!adreno_hwsched_context_queue_enabled(adreno_dev))
  1846. return;
  1847. if (test_bit(ADRENO_HWSCHED_HW_FENCE, &hwsched->flags))
  1848. return;
  1849. hw_fence->handle = msm_hw_fence_register(HW_FENCE_CLIENT_ID_CTX0,
  1850. &hw_fence->mem_descriptor);
  1851. if (IS_ERR_OR_NULL(hw_fence->handle)) {
  1852. dev_err(device->dev, "HW fences not supported: %d\n",
  1853. PTR_ERR_OR_ZERO(hw_fence->handle));
  1854. hw_fence->handle = NULL;
  1855. return;
  1856. }
  1857. /*
  1858. * We need to set up the memory descriptor with the physical address of the Tx/Rx Queues so
  1859. * that these buffers can be imported in to GMU VA space
  1860. */
  1861. kgsl_memdesc_init(device, &hw_fence->memdesc, 0);
  1862. hw_fence->memdesc.physaddr = hw_fence->mem_descriptor.device_addr;
  1863. hw_fence->memdesc.size = hw_fence->mem_descriptor.size;
  1864. hw_fence->memdesc.hostptr = hw_fence->mem_descriptor.virtual_addr;
  1865. ret = kgsl_memdesc_sg_dma(&hw_fence->memdesc, hw_fence->memdesc.physaddr,
  1866. hw_fence->memdesc.size);
  1867. if (ret) {
  1868. dev_err(device->dev, "Failed to setup HW fences memdesc: %d\n",
  1869. ret);
  1870. msm_hw_fence_deregister(hw_fence->handle);
  1871. hw_fence->handle = NULL;
  1872. memset(&hw_fence->memdesc, 0x0, sizeof(hw_fence->memdesc));
  1873. return;
  1874. }
  1875. hwsched->hw_fence_cache = KMEM_CACHE(adreno_hw_fence_entry, 0);
  1876. set_bit(ADRENO_HWSCHED_HW_FENCE, &hwsched->flags);
  1877. }
  1878. int adreno_hwsched_wait_ack_completion(struct adreno_device *adreno_dev,
  1879. struct device *dev, struct pending_cmd *ack,
  1880. void (*process_msgq)(struct adreno_device *adreno_dev))
  1881. {
  1882. int rc;
  1883. /* Only allow a single log in a second */
  1884. static DEFINE_RATELIMIT_STATE(_rs, HZ, 1);
  1885. static u32 unprocessed, processed;
  1886. const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
  1887. u64 start, end;
  1888. start = gpudev->read_alwayson(adreno_dev);
  1889. rc = wait_for_completion_timeout(&ack->complete,
  1890. msecs_to_jiffies(HFI_RSP_TIMEOUT));
  1891. /*
  1892. * A non-zero return value means the completion is complete, whereas zero indicates
  1893. * timeout
  1894. */
  1895. if (rc) {
  1896. /*
  1897. * If an ack goes unprocessed, keep track of processed and unprocessed acks
  1898. * because we may not log each unprocessed ack due to ratelimiting
  1899. */
  1900. if (unprocessed)
  1901. processed++;
  1902. return 0;
  1903. }
  1904. /*
  1905. * It is possible the ack came, but due to HLOS latencies in processing hfi interrupt
  1906. * and/or the f2h daemon, the ack isn't processed yet. Hence, process the msgq one last
  1907. * time.
  1908. */
  1909. process_msgq(adreno_dev);
  1910. end = gpudev->read_alwayson(adreno_dev);
  1911. if (completion_done(&ack->complete)) {
  1912. unprocessed++;
  1913. if (__ratelimit(&_rs))
  1914. dev_err(dev, "Ack unprocessed for id:%d sequence=%d count=%d/%d ticks=%llu/%llu\n",
  1915. MSG_HDR_GET_ID(ack->sent_hdr), MSG_HDR_GET_SEQNUM(ack->sent_hdr),
  1916. unprocessed, processed, start, end);
  1917. return 0;
  1918. }
  1919. dev_err(dev, "Ack timeout for id:%d sequence=%d ticks=%llu/%llu\n",
  1920. MSG_HDR_GET_ID(ack->sent_hdr), MSG_HDR_GET_SEQNUM(ack->sent_hdr), start, end);
  1921. gmu_core_fault_snapshot(KGSL_DEVICE(adreno_dev));
  1922. return -ETIMEDOUT;
  1923. }
  1924. int adreno_hwsched_ctxt_unregister_wait_completion(
  1925. struct adreno_device *adreno_dev,
  1926. struct device *dev, struct pending_cmd *ack,
  1927. void (*process_msgq)(struct adreno_device *adreno_dev),
  1928. struct hfi_unregister_ctxt_cmd *cmd)
  1929. {
  1930. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  1931. const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
  1932. int ret;
  1933. u64 start, end;
  1934. start = gpudev->read_alwayson(adreno_dev);
  1935. mutex_unlock(&device->mutex);
  1936. ret = wait_for_completion_timeout(&ack->complete,
  1937. msecs_to_jiffies(msecs_to_jiffies(30 * 1000)));
  1938. mutex_lock(&device->mutex);
  1939. if (ret)
  1940. return 0;
  1941. /*
  1942. * It is possible the ack came, but due to HLOS latencies in processing hfi interrupt
  1943. * and/or the f2h daemon, the ack isn't processed yet. Hence, process the msgq one last
  1944. * time.
  1945. */
  1946. process_msgq(adreno_dev);
  1947. end = gpudev->read_alwayson(adreno_dev);
  1948. if (completion_done(&ack->complete)) {
  1949. dev_err_ratelimited(dev,
  1950. "Ack unprocessed for context unregister seq: %d ctx: %u ts: %u ticks=%llu/%llu\n",
  1951. MSG_HDR_GET_SEQNUM(ack->sent_hdr), cmd->ctxt_id,
  1952. cmd->ts, start, end);
  1953. return 0;
  1954. }
  1955. dev_err_ratelimited(dev,
  1956. "Ack timeout for context unregister seq: %d ctx: %u ts: %u ticks=%llu/%llu\n",
  1957. MSG_HDR_GET_SEQNUM(ack->sent_hdr), cmd->ctxt_id, cmd->ts, start, end);
  1958. return -ETIMEDOUT;
  1959. }
  1960. u32 adreno_hwsched_parse_payload(struct payload_section *payload, u32 key)
  1961. {
  1962. u32 i;
  1963. /* Each key-value pair is 2 dwords */
  1964. for (i = 0; i < payload->dwords; i += 2) {
  1965. if (payload->data[i] == key)
  1966. return payload->data[i + 1];
  1967. }
  1968. return 0;
  1969. }
  1970. static void adreno_hwsched_lookup_key_value(struct adreno_device *adreno_dev,
  1971. u32 type, u32 key, u32 *ptr, u32 num_values)
  1972. {
  1973. struct hfi_context_bad_cmd *cmd = adreno_dev->hwsched.ctxt_bad;
  1974. u32 i = 0, payload_bytes;
  1975. void *start;
  1976. if (!cmd->hdr)
  1977. return;
  1978. payload_bytes = (MSG_HDR_GET_SIZE(cmd->hdr) << 2) -
  1979. offsetof(struct hfi_context_bad_cmd, payload);
  1980. start = &cmd->payload[0];
  1981. while (i < payload_bytes) {
  1982. struct payload_section *payload = start + i;
  1983. /* key-value pair is 'num_values + 1' dwords */
  1984. if ((payload->type == type) && (payload->data[i] == key)) {
  1985. u32 j = 1;
  1986. do {
  1987. ptr[j - 1] = payload->data[i + j];
  1988. j++;
  1989. } while (num_values--);
  1990. break;
  1991. }
  1992. i += struct_size(payload, data, payload->dwords);
  1993. }
  1994. }
  1995. bool adreno_hwsched_log_nonfatal_gpu_fault(struct adreno_device *adreno_dev,
  1996. struct device *dev, u32 error)
  1997. {
  1998. bool non_fatal = true;
  1999. switch (error) {
  2000. case GMU_CP_AHB_ERROR: {
  2001. u32 err_details[2];
  2002. adreno_hwsched_lookup_key_value(adreno_dev, PAYLOAD_FAULT_REGS,
  2003. KEY_CP_AHB_ERROR, err_details, 2);
  2004. dev_crit_ratelimited(dev,
  2005. "CP: AHB bus error, CP_RL_ERROR_DETAILS_0:0x%x CP_RL_ERROR_DETAILS_1:0x%x\n",
  2006. err_details[0], err_details[1]);
  2007. break;
  2008. }
  2009. case GMU_ATB_ASYNC_FIFO_OVERFLOW:
  2010. dev_crit_ratelimited(dev, "RBBM: ATB ASYNC overflow\n");
  2011. break;
  2012. case GMU_RBBM_ATB_BUF_OVERFLOW:
  2013. dev_crit_ratelimited(dev, "RBBM: ATB bus overflow\n");
  2014. break;
  2015. case GMU_UCHE_OOB_ACCESS:
  2016. dev_crit_ratelimited(dev, "UCHE: Out of bounds access\n");
  2017. break;
  2018. case GMU_UCHE_TRAP_INTR:
  2019. dev_crit_ratelimited(dev, "UCHE: Trap interrupt\n");
  2020. break;
  2021. case GMU_TSB_WRITE_ERROR: {
  2022. u32 addr[2];
  2023. adreno_hwsched_lookup_key_value(adreno_dev, PAYLOAD_FAULT_REGS,
  2024. KEY_TSB_WRITE_ERROR, addr, 2);
  2025. dev_crit_ratelimited(dev, "TSB: Write error interrupt: Address: 0x%lx MID: %lu\n",
  2026. FIELD_GET(GENMASK(16, 0), addr[1]) << 32 | addr[0],
  2027. FIELD_GET(GENMASK(31, 23), addr[1]));
  2028. break;
  2029. }
  2030. default:
  2031. non_fatal = false;
  2032. break;
  2033. }
  2034. return non_fatal;
  2035. }