adreno_dispatch.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2013-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/slab.h>
  7. #include <linux/sysfs.h>
  8. #include <soc/qcom/msm_performance.h>
  9. #include "adreno.h"
  10. #include "adreno_sysfs.h"
  11. #include "adreno_trace.h"
  12. #include "kgsl_bus.h"
  13. #include "kgsl_eventlog.h"
  14. #include "kgsl_gmu_core.h"
  15. #include "kgsl_timeline.h"
  16. #define DRAWQUEUE_NEXT(_i, _s) (((_i) + 1) % (_s))
  17. /* Number of commands that can be queued in a context before it sleeps */
  18. static unsigned int _context_drawqueue_size = 50;
  19. /* Number of milliseconds to wait for the context queue to clear */
  20. static unsigned int _context_queue_wait = 10000;
  21. /* Number of drawobjs sent at a time from a single context */
  22. static unsigned int _context_drawobj_burst = 5;
  23. /*
  24. * GFT throttle parameters. If GFT recovered more than
  25. * X times in Y ms invalidate the context and do not attempt recovery.
  26. * X -> _fault_throttle_burst
  27. * Y -> _fault_throttle_time
  28. */
  29. static unsigned int _fault_throttle_time = 3000;
  30. static unsigned int _fault_throttle_burst = 3;
  31. /*
  32. * Maximum ringbuffer inflight for the single submitting context case - this
  33. * should be sufficiently high to keep the GPU loaded
  34. */
  35. static unsigned int _dispatcher_q_inflight_hi = 15;
  36. /*
  37. * Minimum inflight for the multiple context case - this should sufficiently low
  38. * to allow for lower latency context switching
  39. */
  40. static unsigned int _dispatcher_q_inflight_lo = 4;
  41. /* Command batch timeout (in milliseconds) */
  42. unsigned int adreno_drawobj_timeout = 2000;
  43. /* Interval for reading and comparing fault detection registers */
  44. static unsigned int _fault_timer_interval = 200;
  45. /* Use a kmem cache to speed up allocations for dispatcher jobs */
  46. static struct kmem_cache *jobs_cache;
  47. #define DRAWQUEUE_RB(_drawqueue) \
  48. ((struct adreno_ringbuffer *) \
  49. container_of((_drawqueue),\
  50. struct adreno_ringbuffer, dispatch_q))
  51. #define DRAWQUEUE(_ringbuffer) (&(_ringbuffer)->dispatch_q)
  52. static bool adreno_drawqueue_is_empty(struct adreno_dispatcher_drawqueue *drawqueue)
  53. {
  54. return (drawqueue && drawqueue->head == drawqueue->tail);
  55. }
  56. static int adreno_dispatch_retire_drawqueue(struct adreno_device *adreno_dev,
  57. struct adreno_dispatcher_drawqueue *drawqueue);
  58. static inline bool drawqueue_is_current(
  59. struct adreno_dispatcher_drawqueue *drawqueue)
  60. {
  61. struct adreno_ringbuffer *rb = DRAWQUEUE_RB(drawqueue);
  62. struct adreno_device *adreno_dev = ADRENO_RB_DEVICE(rb);
  63. return (adreno_dev->cur_rb == rb);
  64. }
  65. /*
  66. * If only one context has queued in the last 100 milliseconds increase
  67. * inflight to a high number to load up the GPU. If multiple contexts
  68. * have queued drop the inflight for better context switch latency.
  69. * If no contexts have queued what are you even doing here?
  70. */
  71. static inline int
  72. _drawqueue_inflight(struct adreno_dispatcher_drawqueue *drawqueue)
  73. {
  74. return (drawqueue->active_context_count > 1)
  75. ? _dispatcher_q_inflight_lo : _dispatcher_q_inflight_hi;
  76. }
  77. static void fault_detect_read(struct adreno_device *adreno_dev)
  78. {
  79. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  80. int i;
  81. if (!test_bit(ADRENO_DEVICE_SOFT_FAULT_DETECT, &adreno_dev->priv))
  82. return;
  83. for (i = 0; i < adreno_dev->num_ringbuffers; i++) {
  84. struct adreno_ringbuffer *rb = &(adreno_dev->ringbuffers[i]);
  85. adreno_rb_readtimestamp(adreno_dev, rb,
  86. KGSL_TIMESTAMP_RETIRED, &(rb->fault_detect_ts));
  87. }
  88. for (i = 0; i < adreno_dev->soft_ft_count; i++) {
  89. if (adreno_dev->soft_ft_regs[i])
  90. kgsl_regread(device, adreno_dev->soft_ft_regs[i],
  91. &adreno_dev->soft_ft_vals[i]);
  92. }
  93. }
  94. void adreno_dispatcher_start_fault_timer(struct adreno_device *adreno_dev)
  95. {
  96. struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
  97. if (adreno_soft_fault_detect(adreno_dev))
  98. mod_timer(&dispatcher->fault_timer,
  99. jiffies + msecs_to_jiffies(_fault_timer_interval));
  100. }
  101. /*
  102. * This takes a kgsl_device pointer so that it can be used for the function
  103. * hook in adreno.c too
  104. */
  105. void adreno_dispatcher_stop_fault_timer(struct kgsl_device *device)
  106. {
  107. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  108. struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
  109. if (ADRENO_FEATURE(adreno_dev, ADRENO_SOFT_FAULT_DETECT))
  110. del_timer_sync(&dispatcher->fault_timer);
  111. }
  112. /**
  113. * _retire_timestamp() - Retire object without sending it
  114. * to the hardware
  115. * @drawobj: Pointer to the object to retire
  116. *
  117. * In some cases ibs can be retired by the software
  118. * without going to the GPU. In those cases, update the
  119. * memstore from the CPU, kick off the event engine to handle
  120. * expired events and destroy the ib.
  121. */
  122. static void _retire_timestamp(struct kgsl_drawobj *drawobj)
  123. {
  124. struct kgsl_context *context = drawobj->context;
  125. struct adreno_context *drawctxt = ADRENO_CONTEXT(context);
  126. struct kgsl_device *device = context->device;
  127. struct adreno_ringbuffer *rb = drawctxt->rb;
  128. struct retire_info info = {0};
  129. /*
  130. * Write the start and end timestamp to the memstore to keep the
  131. * accounting sane
  132. */
  133. kgsl_sharedmem_writel(device->memstore,
  134. KGSL_MEMSTORE_OFFSET(context->id, soptimestamp),
  135. drawobj->timestamp);
  136. kgsl_sharedmem_writel(device->memstore,
  137. KGSL_MEMSTORE_OFFSET(context->id, eoptimestamp),
  138. drawobj->timestamp);
  139. drawctxt->submitted_timestamp = drawobj->timestamp;
  140. /* Retire pending GPU events for the object */
  141. kgsl_process_event_group(device, &context->events);
  142. info.inflight = -1;
  143. info.rb_id = rb->id;
  144. info.wptr = rb->wptr;
  145. info.timestamp = drawobj->timestamp;
  146. msm_perf_events_update(MSM_PERF_GFX, MSM_PERF_RETIRED,
  147. pid_nr(context->proc_priv->pid),
  148. context->id, drawobj->timestamp,
  149. !!(drawobj->flags & KGSL_DRAWOBJ_END_OF_FRAME));
  150. if (drawobj->flags & KGSL_DRAWOBJ_END_OF_FRAME) {
  151. atomic64_inc(&context->proc_priv->frame_count);
  152. atomic_inc(&context->proc_priv->period->frames);
  153. }
  154. /*
  155. * For A3xx we still get the rptr from the CP_RB_RPTR instead of
  156. * rptr scratch out address. At this point GPU clocks turned off.
  157. * So avoid reading GPU register directly for A3xx.
  158. */
  159. if (adreno_is_a3xx(ADRENO_DEVICE(device))) {
  160. trace_adreno_cmdbatch_retired(context, &info,
  161. drawobj->flags, rb->dispatch_q.inflight, 0);
  162. } else {
  163. info.rptr = adreno_get_rptr(rb);
  164. trace_adreno_cmdbatch_retired(context, &info,
  165. drawobj->flags, rb->dispatch_q.inflight, 0);
  166. }
  167. log_kgsl_cmdbatch_retired_event(context->id, drawobj->timestamp,
  168. context->priority, drawobj->flags, 0, 0);
  169. kgsl_drawobj_destroy(drawobj);
  170. }
  171. static int _check_context_queue(struct adreno_context *drawctxt, u32 count)
  172. {
  173. int ret;
  174. spin_lock(&drawctxt->lock);
  175. /*
  176. * Wake up if there is room in the context or if the whole thing got
  177. * invalidated while we were asleep
  178. */
  179. if (kgsl_context_invalid(&drawctxt->base))
  180. ret = 1;
  181. else
  182. ret = ((drawctxt->queued + count) < _context_drawqueue_size) ? 1 : 0;
  183. spin_unlock(&drawctxt->lock);
  184. return ret;
  185. }
  186. /*
  187. * return true if this is a marker command and the dependent timestamp has
  188. * retired
  189. */
  190. static bool _marker_expired(struct kgsl_drawobj_cmd *markerobj)
  191. {
  192. struct kgsl_drawobj *drawobj = DRAWOBJ(markerobj);
  193. return (drawobj->flags & KGSL_DRAWOBJ_MARKER) &&
  194. kgsl_check_timestamp(drawobj->device, drawobj->context,
  195. markerobj->marker_timestamp);
  196. }
  197. static inline void _pop_drawobj(struct adreno_context *drawctxt)
  198. {
  199. drawctxt->drawqueue_head = DRAWQUEUE_NEXT(drawctxt->drawqueue_head,
  200. ADRENO_CONTEXT_DRAWQUEUE_SIZE);
  201. drawctxt->queued--;
  202. }
  203. static int dispatch_retire_markerobj(struct kgsl_drawobj *drawobj,
  204. struct adreno_context *drawctxt)
  205. {
  206. struct kgsl_drawobj_cmd *cmdobj = CMDOBJ(drawobj);
  207. if (_marker_expired(cmdobj)) {
  208. _pop_drawobj(drawctxt);
  209. _retire_timestamp(drawobj);
  210. return 0;
  211. }
  212. /*
  213. * If the marker isn't expired but the SKIP bit
  214. * is set then there are real commands following
  215. * this one in the queue. This means that we
  216. * need to dispatch the command so that we can
  217. * keep the timestamp accounting correct. If
  218. * skip isn't set then we block this queue
  219. * until the dependent timestamp expires
  220. */
  221. return test_bit(CMDOBJ_SKIP, &cmdobj->priv) ? 1 : -EAGAIN;
  222. }
  223. static int dispatch_retire_syncobj(struct kgsl_drawobj *drawobj,
  224. struct adreno_context *drawctxt)
  225. {
  226. struct kgsl_drawobj_sync *syncobj = SYNCOBJ(drawobj);
  227. if (!kgsl_drawobj_events_pending(syncobj)) {
  228. _pop_drawobj(drawctxt);
  229. kgsl_drawobj_destroy(drawobj);
  230. return 0;
  231. }
  232. /*
  233. * If we got here, there are pending events for sync object.
  234. * Start the canary timer if it hasnt been started already.
  235. */
  236. if (!syncobj->timeout_jiffies) {
  237. syncobj->timeout_jiffies = jiffies + msecs_to_jiffies(5000);
  238. mod_timer(&syncobj->timer, syncobj->timeout_jiffies);
  239. }
  240. return -EAGAIN;
  241. }
  242. static int drawqueue_retire_timelineobj(struct kgsl_drawobj *drawobj,
  243. struct adreno_context *drawctxt)
  244. {
  245. _pop_drawobj(drawctxt);
  246. kgsl_drawobj_destroy(drawobj);
  247. return 0;
  248. }
  249. static int drawqueue_retire_bindobj(struct kgsl_drawobj *drawobj,
  250. struct adreno_context *drawctxt)
  251. {
  252. struct kgsl_drawobj_bind *bindobj = BINDOBJ(drawobj);
  253. if (test_bit(KGSL_BINDOBJ_STATE_DONE, &bindobj->state)) {
  254. _pop_drawobj(drawctxt);
  255. _retire_timestamp(drawobj);
  256. return 0;
  257. }
  258. if (!test_and_set_bit(KGSL_BINDOBJ_STATE_START, &bindobj->state)) {
  259. /*
  260. * Take a referencre to the drawobj and the context because both
  261. * get referenced in the bind callback
  262. */
  263. _kgsl_context_get(&drawctxt->base);
  264. kref_get(&drawobj->refcount);
  265. kgsl_sharedmem_bind_ranges(bindobj->bind);
  266. }
  267. return -EAGAIN;
  268. }
  269. /*
  270. * Retires all expired marker and sync objs from the context
  271. * queue and returns one of the below
  272. * a) next drawobj that needs to be sent to ringbuffer
  273. * b) -EAGAIN for syncobj with syncpoints pending.
  274. * c) -EAGAIN for markerobj whose marker timestamp has not expired yet.
  275. * c) NULL for no commands remaining in drawqueue.
  276. */
  277. static struct kgsl_drawobj *_process_drawqueue_get_next_drawobj(
  278. struct adreno_context *drawctxt)
  279. {
  280. struct kgsl_drawobj *drawobj;
  281. unsigned int i = drawctxt->drawqueue_head;
  282. if (drawctxt->drawqueue_head == drawctxt->drawqueue_tail)
  283. return NULL;
  284. for (i = drawctxt->drawqueue_head; i != drawctxt->drawqueue_tail;
  285. i = DRAWQUEUE_NEXT(i, ADRENO_CONTEXT_DRAWQUEUE_SIZE)) {
  286. int ret = 0;
  287. drawobj = drawctxt->drawqueue[i];
  288. if (!drawobj)
  289. return NULL;
  290. switch (drawobj->type) {
  291. case CMDOBJ_TYPE:
  292. return drawobj;
  293. case MARKEROBJ_TYPE:
  294. ret = dispatch_retire_markerobj(drawobj, drawctxt);
  295. /* Special case where marker needs to be sent to GPU */
  296. if (ret == 1)
  297. return drawobj;
  298. break;
  299. case SYNCOBJ_TYPE:
  300. ret = dispatch_retire_syncobj(drawobj, drawctxt);
  301. break;
  302. case BINDOBJ_TYPE:
  303. ret = drawqueue_retire_bindobj(drawobj, drawctxt);
  304. break;
  305. case TIMELINEOBJ_TYPE:
  306. ret = drawqueue_retire_timelineobj(drawobj, drawctxt);
  307. break;
  308. default:
  309. ret = -EINVAL;
  310. break;
  311. }
  312. if (ret)
  313. return ERR_PTR(ret);
  314. }
  315. return NULL;
  316. }
  317. /**
  318. * adreno_dispatcher_requeue_cmdobj() - Put a command back on the context
  319. * queue
  320. * @drawctxt: Pointer to the adreno draw context
  321. * @cmdobj: Pointer to the KGSL command object to requeue
  322. *
  323. * Failure to submit a command to the ringbuffer isn't the fault of the command
  324. * being submitted so if a failure happens, push it back on the head of the
  325. * context queue to be reconsidered again unless the context got detached.
  326. */
  327. static inline int adreno_dispatcher_requeue_cmdobj(
  328. struct adreno_context *drawctxt,
  329. struct kgsl_drawobj_cmd *cmdobj)
  330. {
  331. unsigned int prev;
  332. struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
  333. spin_lock(&drawctxt->lock);
  334. if (kgsl_context_is_bad(&drawctxt->base)) {
  335. spin_unlock(&drawctxt->lock);
  336. /* get rid of this drawobj since the context is bad */
  337. kgsl_drawobj_destroy(drawobj);
  338. return -ENOENT;
  339. }
  340. prev = drawctxt->drawqueue_head == 0 ?
  341. (ADRENO_CONTEXT_DRAWQUEUE_SIZE - 1) :
  342. (drawctxt->drawqueue_head - 1);
  343. /*
  344. * The maximum queue size always needs to be one less then the size of
  345. * the ringbuffer queue so there is "room" to put the drawobj back in
  346. */
  347. WARN_ON(prev == drawctxt->drawqueue_tail);
  348. drawctxt->drawqueue[prev] = drawobj;
  349. drawctxt->queued++;
  350. /* Reset the command queue head to reflect the newly requeued change */
  351. drawctxt->drawqueue_head = prev;
  352. cmdobj->requeue_cnt++;
  353. spin_unlock(&drawctxt->lock);
  354. return 0;
  355. }
  356. /**
  357. * dispatcher_queue_context() - Queue a context in the dispatcher pending list
  358. * @dispatcher: Pointer to the adreno dispatcher struct
  359. * @drawctxt: Pointer to the adreno draw context
  360. *
  361. * Add a context to the dispatcher pending list.
  362. */
  363. static int dispatcher_queue_context(struct adreno_device *adreno_dev,
  364. struct adreno_context *drawctxt)
  365. {
  366. struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
  367. struct adreno_dispatch_job *job;
  368. /* Refuse to queue a detached context */
  369. if (kgsl_context_detached(&drawctxt->base))
  370. return 0;
  371. if (!_kgsl_context_get(&drawctxt->base))
  372. return 0;
  373. /* This function can be called in an atomic context */
  374. job = kmem_cache_alloc(jobs_cache, GFP_ATOMIC);
  375. if (!job) {
  376. kgsl_context_put(&drawctxt->base);
  377. return -ENOMEM;
  378. }
  379. job->drawctxt = drawctxt;
  380. trace_dispatch_queue_context(drawctxt);
  381. llist_add(&job->node, &dispatcher->jobs[drawctxt->base.priority]);
  382. return 0;
  383. }
  384. /*
  385. * Real time clients may demand high BW and have strict latency requirement.
  386. * GPU bus DCVS is not fast enough to account for sudden BW requirements.
  387. * Bus hint helps to bump up the bus vote (IB) upfront for known time-critical
  388. * workloads.
  389. */
  390. static void process_rt_bus_hint(struct kgsl_device *device, bool on)
  391. {
  392. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  393. struct adreno_dispatcher_drawqueue *drawqueue =
  394. DRAWQUEUE(&adreno_dev->ringbuffers[0]);
  395. if (!adreno_is_preemption_enabled(adreno_dev) ||
  396. !device->pwrctrl.rt_bus_hint)
  397. return;
  398. if (device->pwrctrl.rt_bus_hint_active == on)
  399. return;
  400. if (on && drawqueue->inflight == 1)
  401. kgsl_bus_update(device, KGSL_BUS_VOTE_RT_HINT_ON);
  402. if (!on && drawqueue->inflight == 0)
  403. kgsl_bus_update(device, KGSL_BUS_VOTE_RT_HINT_OFF);
  404. }
  405. #define ADRENO_DRAWOBJ_PROFILE_COUNT \
  406. (PAGE_SIZE / sizeof(struct adreno_drawobj_profile_entry))
  407. /**
  408. * sendcmd() - Send a drawobj to the GPU hardware
  409. * @dispatcher: Pointer to the adreno dispatcher struct
  410. * @drawobj: Pointer to the KGSL drawobj being sent
  411. *
  412. * Send a KGSL drawobj to the GPU hardware
  413. */
  414. static int sendcmd(struct adreno_device *adreno_dev,
  415. struct kgsl_drawobj_cmd *cmdobj)
  416. {
  417. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  418. struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
  419. const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
  420. struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
  421. struct adreno_context *drawctxt = ADRENO_CONTEXT(drawobj->context);
  422. struct kgsl_context *context = drawobj->context;
  423. struct adreno_dispatcher_drawqueue *dispatch_q = &drawctxt->rb->dispatch_q;
  424. struct adreno_submit_time time;
  425. uint64_t secs = 0;
  426. unsigned long nsecs = 0;
  427. int ret;
  428. struct submission_info info = {0};
  429. mutex_lock(&device->mutex);
  430. if (adreno_gpu_halt(adreno_dev) != 0) {
  431. mutex_unlock(&device->mutex);
  432. return -EBUSY;
  433. }
  434. memset(&time, 0x0, sizeof(time));
  435. dispatcher->inflight++;
  436. dispatch_q->inflight++;
  437. if (dispatcher->inflight == 1 &&
  438. !test_bit(ADRENO_DISPATCHER_POWER, &dispatcher->priv)) {
  439. /* Time to make the donuts. Turn on the GPU */
  440. ret = adreno_active_count_get(adreno_dev);
  441. if (ret) {
  442. dispatcher->inflight--;
  443. dispatch_q->inflight--;
  444. mutex_unlock(&device->mutex);
  445. return ret;
  446. }
  447. set_bit(ADRENO_DISPATCHER_POWER, &dispatcher->priv);
  448. }
  449. if (test_bit(ADRENO_DEVICE_DRAWOBJ_PROFILE, &adreno_dev->priv)) {
  450. set_bit(CMDOBJ_PROFILE, &cmdobj->priv);
  451. cmdobj->profile_index = adreno_dev->profile_index;
  452. adreno_dev->profile_index =
  453. (adreno_dev->profile_index + 1) %
  454. ADRENO_DRAWOBJ_PROFILE_COUNT;
  455. }
  456. process_rt_bus_hint(device, true);
  457. ret = adreno_ringbuffer_submitcmd(adreno_dev, cmdobj, &time);
  458. /*
  459. * On the first command, if the submission was successful, then read the
  460. * fault registers. If it failed then turn off the GPU. Sad face.
  461. */
  462. if (dispatcher->inflight == 1) {
  463. if (ret == 0) {
  464. /* Stop fault timer before reading fault registers */
  465. adreno_dispatcher_stop_fault_timer(device);
  466. fault_detect_read(adreno_dev);
  467. /* Start the fault timer on first submission */
  468. adreno_dispatcher_start_fault_timer(adreno_dev);
  469. if (!test_and_set_bit(ADRENO_DISPATCHER_ACTIVE,
  470. &dispatcher->priv))
  471. reinit_completion(&dispatcher->idle_gate);
  472. } else {
  473. adreno_active_count_put(adreno_dev);
  474. clear_bit(ADRENO_DISPATCHER_POWER, &dispatcher->priv);
  475. }
  476. }
  477. if (ret) {
  478. dispatcher->inflight--;
  479. dispatch_q->inflight--;
  480. process_rt_bus_hint(device, false);
  481. mutex_unlock(&device->mutex);
  482. /*
  483. * Don't log a message in case of:
  484. * -ENOENT means that the context was detached before the
  485. * command was submitted
  486. * -ENOSPC means that there temporarily isn't any room in the
  487. * ringbuffer
  488. * -PROTO means that a fault is currently being worked
  489. */
  490. if (ret != -ENOENT && ret != -ENOSPC && ret != -EPROTO)
  491. dev_err(device->dev,
  492. "Unable to submit command to the ringbuffer %d\n",
  493. ret);
  494. return ret;
  495. }
  496. secs = time.ktime;
  497. nsecs = do_div(secs, 1000000000);
  498. /*
  499. * For the first submission in any given command queue update the
  500. * expected expire time - this won't actually be used / updated until
  501. * the command queue in question goes current, but universally setting
  502. * it here avoids the possibilty of some race conditions with preempt
  503. */
  504. if (dispatch_q->inflight == 1)
  505. dispatch_q->expires = jiffies +
  506. msecs_to_jiffies(adreno_drawobj_timeout);
  507. info.inflight = (int) dispatcher->inflight;
  508. info.rb_id = drawctxt->rb->id;
  509. info.rptr = adreno_get_rptr(drawctxt->rb);
  510. info.wptr = drawctxt->rb->wptr;
  511. info.gmu_dispatch_queue = -1;
  512. msm_perf_events_update(MSM_PERF_GFX, MSM_PERF_SUBMIT,
  513. pid_nr(context->proc_priv->pid),
  514. context->id, drawobj->timestamp,
  515. !!(drawobj->flags & KGSL_DRAWOBJ_END_OF_FRAME));
  516. trace_adreno_cmdbatch_submitted(drawobj, &info,
  517. time.ticks, (unsigned long) secs, nsecs / 1000,
  518. dispatch_q->inflight);
  519. log_kgsl_cmdbatch_submitted_event(context->id, drawobj->timestamp,
  520. context->priority, drawobj->flags);
  521. mutex_unlock(&device->mutex);
  522. cmdobj->submit_ticks = time.ticks;
  523. dispatch_q->cmd_q[dispatch_q->tail] = cmdobj;
  524. dispatch_q->tail = (dispatch_q->tail + 1) %
  525. ADRENO_DISPATCH_DRAWQUEUE_SIZE;
  526. /*
  527. * If we believe ourselves to be current and preemption isn't a thing,
  528. * then set up the timer. If this misses, then preemption is indeed a
  529. * thing and the timer will be set up in due time
  530. */
  531. if (adreno_in_preempt_state(adreno_dev, ADRENO_PREEMPT_NONE)) {
  532. if (drawqueue_is_current(dispatch_q))
  533. mod_timer(&dispatcher->timer, dispatch_q->expires);
  534. }
  535. /*
  536. * we just submitted something, readjust ringbuffer
  537. * execution level
  538. */
  539. if (gpudev->preemption_schedule)
  540. gpudev->preemption_schedule(adreno_dev);
  541. return 0;
  542. }
  543. /**
  544. * dispatcher_context_sendcmds() - Send commands from a context to the GPU
  545. * @adreno_dev: Pointer to the adreno device struct
  546. * @drawctxt: Pointer to the adreno context to dispatch commands from
  547. *
  548. * Dequeue and send a burst of commands from the specified context to the GPU
  549. * Returns postive if the context needs to be put back on the pending queue
  550. * 0 if the context is empty or detached and negative on error
  551. */
  552. static int dispatcher_context_sendcmds(struct adreno_device *adreno_dev,
  553. struct adreno_context *drawctxt)
  554. {
  555. struct adreno_dispatcher_drawqueue *dispatch_q =
  556. &(drawctxt->rb->dispatch_q);
  557. int count = 0;
  558. int ret = 0;
  559. int inflight = _drawqueue_inflight(dispatch_q);
  560. unsigned int timestamp;
  561. if (dispatch_q->inflight >= inflight) {
  562. spin_lock(&drawctxt->lock);
  563. _process_drawqueue_get_next_drawobj(drawctxt);
  564. spin_unlock(&drawctxt->lock);
  565. return -EBUSY;
  566. }
  567. /*
  568. * Each context can send a specific number of drawobjs per cycle
  569. */
  570. while ((count < _context_drawobj_burst) &&
  571. (dispatch_q->inflight < inflight)) {
  572. struct kgsl_drawobj *drawobj;
  573. struct kgsl_drawobj_cmd *cmdobj;
  574. struct kgsl_context *context;
  575. if (adreno_gpu_fault(adreno_dev) != 0)
  576. break;
  577. spin_lock(&drawctxt->lock);
  578. drawobj = _process_drawqueue_get_next_drawobj(drawctxt);
  579. /*
  580. * adreno_context_get_drawobj returns -EAGAIN if the current
  581. * drawobj has pending sync points so no more to do here.
  582. * When the sync points are satisfied then the context will get
  583. * reqeueued
  584. */
  585. if (IS_ERR_OR_NULL(drawobj)) {
  586. if (IS_ERR(drawobj))
  587. ret = PTR_ERR(drawobj);
  588. spin_unlock(&drawctxt->lock);
  589. break;
  590. }
  591. _pop_drawobj(drawctxt);
  592. spin_unlock(&drawctxt->lock);
  593. timestamp = drawobj->timestamp;
  594. cmdobj = CMDOBJ(drawobj);
  595. context = drawobj->context;
  596. trace_adreno_cmdbatch_ready(context->id, context->priority,
  597. drawobj->timestamp, cmdobj->requeue_cnt);
  598. ret = sendcmd(adreno_dev, cmdobj);
  599. /*
  600. * On error from sendcmd() try to requeue the cmdobj
  601. * unless we got back -ENOENT which means that the context has
  602. * been detached and there will be no more deliveries from here
  603. */
  604. if (ret != 0) {
  605. /* Destroy the cmdobj on -ENOENT */
  606. if (ret == -ENOENT)
  607. kgsl_drawobj_destroy(drawobj);
  608. else {
  609. /*
  610. * If the requeue returns an error, return that
  611. * instead of whatever sendcmd() sent us
  612. */
  613. int r = adreno_dispatcher_requeue_cmdobj(
  614. drawctxt, cmdobj);
  615. if (r)
  616. ret = r;
  617. }
  618. break;
  619. }
  620. drawctxt->submitted_timestamp = timestamp;
  621. count++;
  622. }
  623. /*
  624. * Wake up any snoozing threads if we have consumed any real commands
  625. * or marker commands and we have room in the context queue.
  626. */
  627. if (_check_context_queue(drawctxt, 0))
  628. wake_up_all(&drawctxt->wq);
  629. if (!ret)
  630. ret = count;
  631. /* Return error or the number of commands queued */
  632. return ret;
  633. }
  634. static bool adreno_gpu_stopped(struct adreno_device *adreno_dev)
  635. {
  636. return (adreno_gpu_fault(adreno_dev) || adreno_gpu_halt(adreno_dev));
  637. }
  638. static void dispatcher_handle_jobs_list(struct adreno_device *adreno_dev,
  639. int id, unsigned long *map, struct llist_node *list)
  640. {
  641. struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
  642. struct adreno_dispatch_job *job, *next;
  643. if (!list)
  644. return;
  645. /* Reverse the order so the oldest context is considered first */
  646. list = llist_reverse_order(list);
  647. llist_for_each_entry_safe(job, next, list, node) {
  648. int ret;
  649. if (kgsl_context_is_bad(&job->drawctxt->base)) {
  650. kgsl_context_put(&job->drawctxt->base);
  651. kmem_cache_free(jobs_cache, job);
  652. continue;
  653. }
  654. /*
  655. * Due to the nature of the lockless queue the same context
  656. * might have multiple jobs on the list. We allow this so we
  657. * don't have to query the list on the producer side but on the
  658. * consumer side we only want each context to be considered
  659. * once. Use a bitmap to remember which contexts we've already
  660. * seen and quietly discard duplicate jobs
  661. */
  662. if (test_and_set_bit(job->drawctxt->base.id, map)) {
  663. kgsl_context_put(&job->drawctxt->base);
  664. kmem_cache_free(jobs_cache, job);
  665. continue;
  666. }
  667. /*
  668. * If gpu is in fault or dispatcher is halted, add back the jobs
  669. * so that they are processed after recovery or when dispatcher
  670. * is resumed.
  671. */
  672. if (adreno_gpu_stopped(adreno_dev)) {
  673. llist_add(&job->node, &dispatcher->jobs[id]);
  674. continue;
  675. }
  676. ret = dispatcher_context_sendcmds(adreno_dev, job->drawctxt);
  677. /*
  678. * If the context had nothing queued or the context has been
  679. * destroyed then drop the job
  680. */
  681. if (!ret || ret == -ENOENT) {
  682. kgsl_context_put(&job->drawctxt->base);
  683. kmem_cache_free(jobs_cache, job);
  684. continue;
  685. }
  686. /*
  687. * If the ringbuffer is full then requeue the job to be
  688. * considered first next time. Otherwise the context
  689. * either successfully submmitted to the GPU or another error
  690. * happened and it should go back on the regular queue
  691. */
  692. if (ret == -EBUSY)
  693. llist_add(&job->node, &dispatcher->requeue[id]);
  694. else
  695. llist_add(&job->node, &dispatcher->jobs[id]);
  696. }
  697. }
  698. static void dispatcher_handle_jobs(struct adreno_device *adreno_dev, int id)
  699. {
  700. struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
  701. unsigned long map[BITS_TO_LONGS(KGSL_MEMSTORE_MAX)];
  702. struct llist_node *requeue, *jobs;
  703. memset(map, 0, sizeof(map));
  704. requeue = llist_del_all(&dispatcher->requeue[id]);
  705. jobs = llist_del_all(&dispatcher->jobs[id]);
  706. dispatcher_handle_jobs_list(adreno_dev, id, map, requeue);
  707. dispatcher_handle_jobs_list(adreno_dev, id, map, jobs);
  708. }
  709. /**
  710. * _adreno_dispatcher_issuecmds() - Issue commmands from pending contexts
  711. * @adreno_dev: Pointer to the adreno device struct
  712. *
  713. * Issue as many commands as possible (up to inflight) from the pending contexts
  714. * This function assumes the dispatcher mutex has been locked.
  715. */
  716. static void _adreno_dispatcher_issuecmds(struct adreno_device *adreno_dev)
  717. {
  718. struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
  719. int i;
  720. /* Leave early if the dispatcher isn't in a happy state */
  721. if (adreno_gpu_fault(adreno_dev) != 0)
  722. return;
  723. for (i = 0; i < ARRAY_SIZE(dispatcher->jobs); i++)
  724. dispatcher_handle_jobs(adreno_dev, i);
  725. }
  726. /* Update the dispatcher timers */
  727. static void _dispatcher_update_timers(struct adreno_device *adreno_dev)
  728. {
  729. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  730. struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
  731. /* Kick the idle timer */
  732. mutex_lock(&device->mutex);
  733. kgsl_pwrscale_update(device);
  734. process_rt_bus_hint(device, false);
  735. kgsl_start_idle_timer(device);
  736. mutex_unlock(&device->mutex);
  737. /* Check to see if we need to update the command timer */
  738. if (adreno_in_preempt_state(adreno_dev, ADRENO_PREEMPT_NONE)) {
  739. struct adreno_dispatcher_drawqueue *drawqueue =
  740. DRAWQUEUE(adreno_dev->cur_rb);
  741. if (!adreno_drawqueue_is_empty(drawqueue))
  742. mod_timer(&dispatcher->timer, drawqueue->expires);
  743. }
  744. }
  745. static inline void _decrement_submit_now(struct kgsl_device *device)
  746. {
  747. spin_lock(&device->submit_lock);
  748. device->submit_now--;
  749. spin_unlock(&device->submit_lock);
  750. }
  751. /**
  752. * adreno_dispatcher_issuecmds() - Issue commmands from pending contexts
  753. * @adreno_dev: Pointer to the adreno device struct
  754. *
  755. * Lock the dispatcher and call _adreno_dispatcher_issueibcmds
  756. */
  757. static void adreno_dispatcher_issuecmds(struct adreno_device *adreno_dev)
  758. {
  759. struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
  760. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  761. spin_lock(&device->submit_lock);
  762. /* If state is not ACTIVE, schedule the work for later */
  763. if (device->skip_inline_submit) {
  764. spin_unlock(&device->submit_lock);
  765. goto done;
  766. }
  767. device->submit_now++;
  768. spin_unlock(&device->submit_lock);
  769. /* If the dispatcher is busy then schedule the work for later */
  770. if (!mutex_trylock(&dispatcher->mutex)) {
  771. _decrement_submit_now(device);
  772. goto done;
  773. }
  774. _adreno_dispatcher_issuecmds(adreno_dev);
  775. if (dispatcher->inflight)
  776. _dispatcher_update_timers(adreno_dev);
  777. mutex_unlock(&dispatcher->mutex);
  778. _decrement_submit_now(device);
  779. return;
  780. done:
  781. adreno_dispatcher_schedule(device);
  782. }
  783. /**
  784. * get_timestamp() - Return the next timestamp for the context
  785. * @drawctxt - Pointer to an adreno draw context struct
  786. * @drawobj - Pointer to a drawobj
  787. * @timestamp - Pointer to a timestamp value possibly passed from the user
  788. * @user_ts - user generated timestamp
  789. *
  790. * Assign a timestamp based on the settings of the draw context and the command
  791. * batch.
  792. */
  793. static int get_timestamp(struct adreno_context *drawctxt,
  794. struct kgsl_drawobj *drawobj, unsigned int *timestamp,
  795. unsigned int user_ts)
  796. {
  797. if (drawctxt->base.flags & KGSL_CONTEXT_USER_GENERATED_TS) {
  798. /*
  799. * User specified timestamps need to be greater than the last
  800. * issued timestamp in the context
  801. */
  802. if (timestamp_cmp(drawctxt->timestamp, user_ts) >= 0)
  803. return -ERANGE;
  804. drawctxt->timestamp = user_ts;
  805. } else
  806. drawctxt->timestamp++;
  807. *timestamp = drawctxt->timestamp;
  808. drawobj->timestamp = *timestamp;
  809. return 0;
  810. }
  811. static void _set_ft_policy(struct adreno_device *adreno_dev,
  812. struct adreno_context *drawctxt,
  813. struct kgsl_drawobj_cmd *cmdobj)
  814. {
  815. /*
  816. * Set the fault tolerance policy for the command batch - assuming the
  817. * context hasn't disabled FT use the current device policy
  818. */
  819. if (drawctxt->base.flags & KGSL_CONTEXT_NO_FAULT_TOLERANCE)
  820. set_bit(KGSL_FT_DISABLE, &cmdobj->fault_policy);
  821. /*
  822. * Set the fault tolerance policy to FT_REPLAY - As context wants
  823. * to invalidate it after a replay attempt fails. This doesn't
  824. * require to execute the default FT policy.
  825. */
  826. else if (drawctxt->base.flags & KGSL_CONTEXT_INVALIDATE_ON_FAULT)
  827. set_bit(KGSL_FT_REPLAY, &cmdobj->fault_policy);
  828. else
  829. cmdobj->fault_policy = adreno_dev->ft_policy;
  830. }
  831. static void _cmdobj_set_flags(struct adreno_context *drawctxt,
  832. struct kgsl_drawobj_cmd *cmdobj)
  833. {
  834. /*
  835. * Force the preamble for this submission only - this is usually
  836. * requested by the dispatcher as part of fault recovery
  837. */
  838. if (test_and_clear_bit(ADRENO_CONTEXT_FORCE_PREAMBLE,
  839. &drawctxt->base.priv))
  840. set_bit(CMDOBJ_FORCE_PREAMBLE, &cmdobj->priv);
  841. /*
  842. * Force the premable if set from userspace in the context or
  843. * command obj flags
  844. */
  845. if ((drawctxt->base.flags & KGSL_CONTEXT_CTX_SWITCH) ||
  846. (cmdobj->base.flags & KGSL_DRAWOBJ_CTX_SWITCH))
  847. set_bit(CMDOBJ_FORCE_PREAMBLE, &cmdobj->priv);
  848. /* Skip this ib if IFH_NOP is enabled */
  849. if (drawctxt->base.flags & KGSL_CONTEXT_IFH_NOP)
  850. set_bit(CMDOBJ_SKIP, &cmdobj->priv);
  851. /*
  852. * If we are waiting for the end of frame and it hasn't appeared yet,
  853. * then mark the command obj as skipped. It will still progress
  854. * through the pipeline but it won't actually send any commands
  855. */
  856. if (test_bit(ADRENO_CONTEXT_SKIP_EOF, &drawctxt->base.priv)) {
  857. set_bit(CMDOBJ_SKIP, &cmdobj->priv);
  858. /*
  859. * If this command obj represents the EOF then clear the way
  860. * for the dispatcher to continue submitting
  861. */
  862. if (cmdobj->base.flags & KGSL_DRAWOBJ_END_OF_FRAME) {
  863. clear_bit(ADRENO_CONTEXT_SKIP_EOF,
  864. &drawctxt->base.priv);
  865. /*
  866. * Force the preamble on the next command to ensure that
  867. * the state is correct
  868. */
  869. set_bit(ADRENO_CONTEXT_FORCE_PREAMBLE,
  870. &drawctxt->base.priv);
  871. }
  872. }
  873. }
  874. static inline int _wait_for_room_in_context_queue(
  875. struct adreno_context *drawctxt, u32 count) __must_hold(&drawctxt->lock)
  876. {
  877. int ret = 0;
  878. /*
  879. * There is always a possibility that dispatcher may end up pushing
  880. * the last popped draw object back to the context drawqueue. Hence,
  881. * we can only queue up to _context_drawqueue_size - 1 here to make
  882. * sure we never let drawqueue->queued exceed _context_drawqueue_size.
  883. */
  884. if ((drawctxt->queued + count) > (_context_drawqueue_size - 1)) {
  885. trace_adreno_drawctxt_sleep(drawctxt);
  886. spin_unlock(&drawctxt->lock);
  887. ret = wait_event_interruptible_timeout(drawctxt->wq,
  888. _check_context_queue(drawctxt, count),
  889. msecs_to_jiffies(_context_queue_wait));
  890. spin_lock(&drawctxt->lock);
  891. trace_adreno_drawctxt_wake(drawctxt);
  892. /*
  893. * Account for the possibility that the context got invalidated
  894. * while we were sleeping
  895. */
  896. if (ret > 0)
  897. ret = kgsl_check_context_state(&drawctxt->base);
  898. else if (ret == 0)
  899. ret = -ETIMEDOUT;
  900. }
  901. return ret;
  902. }
  903. static unsigned int _check_context_state_to_queue_cmds(
  904. struct adreno_context *drawctxt, u32 count)
  905. {
  906. int ret = kgsl_check_context_state(&drawctxt->base);
  907. if (ret)
  908. return ret;
  909. return _wait_for_room_in_context_queue(drawctxt, count);
  910. }
  911. static void _queue_drawobj(struct adreno_context *drawctxt,
  912. struct kgsl_drawobj *drawobj)
  913. {
  914. struct kgsl_context *context = drawobj->context;
  915. /* Put the command into the queue */
  916. drawctxt->drawqueue[drawctxt->drawqueue_tail] = drawobj;
  917. drawctxt->drawqueue_tail = (drawctxt->drawqueue_tail + 1) %
  918. ADRENO_CONTEXT_DRAWQUEUE_SIZE;
  919. drawctxt->queued++;
  920. msm_perf_events_update(MSM_PERF_GFX, MSM_PERF_QUEUE,
  921. pid_nr(context->proc_priv->pid),
  922. context->id, drawobj->timestamp,
  923. !!(drawobj->flags & KGSL_DRAWOBJ_END_OF_FRAME));
  924. trace_adreno_cmdbatch_queued(drawobj, drawctxt->queued);
  925. }
  926. static int drawctxt_queue_bindobj(struct adreno_context *drawctxt,
  927. struct kgsl_drawobj *drawobj, u32 *timestamp, u32 user_ts)
  928. {
  929. int ret;
  930. ret = get_timestamp(drawctxt, drawobj, timestamp, user_ts);
  931. if (ret)
  932. return ret;
  933. drawctxt->queued_timestamp = *timestamp;
  934. _queue_drawobj(drawctxt, drawobj);
  935. return 0;
  936. }
  937. static void drawctxt_queue_timelineobj(struct adreno_context *drawctxt,
  938. struct kgsl_drawobj *drawobj)
  939. {
  940. /*
  941. * This drawobj is not submitted to the GPU so use a timestamp of 0.
  942. * Update the timestamp through a subsequent marker to keep userspace
  943. * happy.
  944. */
  945. drawobj->timestamp = 0;
  946. _queue_drawobj(drawctxt, drawobj);
  947. }
  948. static int drawctxt_queue_markerobj(struct adreno_device *adreno_dev,
  949. struct adreno_context *drawctxt, struct kgsl_drawobj *drawobj,
  950. uint32_t *timestamp, unsigned int user_ts)
  951. {
  952. struct kgsl_drawobj_cmd *markerobj = CMDOBJ(drawobj);
  953. int ret;
  954. ret = get_timestamp(drawctxt, drawobj, timestamp, user_ts);
  955. if (ret)
  956. return ret;
  957. /*
  958. * See if we can fastpath this thing - if nothing is queued
  959. * and nothing is inflight retire without bothering the GPU
  960. */
  961. if (!drawctxt->queued && kgsl_check_timestamp(drawobj->device,
  962. drawobj->context, drawctxt->queued_timestamp)) {
  963. _retire_timestamp(drawobj);
  964. return 1;
  965. }
  966. /*
  967. * Remember the last queued timestamp - the marker will block
  968. * until that timestamp is expired (unless another command
  969. * comes along and forces the marker to execute)
  970. */
  971. markerobj->marker_timestamp = drawctxt->queued_timestamp;
  972. drawctxt->queued_timestamp = *timestamp;
  973. _set_ft_policy(adreno_dev, drawctxt, markerobj);
  974. _cmdobj_set_flags(drawctxt, markerobj);
  975. _queue_drawobj(drawctxt, drawobj);
  976. return 0;
  977. }
  978. static int drawctxt_queue_cmdobj(struct adreno_device *adreno_dev,
  979. struct adreno_context *drawctxt, struct kgsl_drawobj *drawobj,
  980. uint32_t *timestamp, unsigned int user_ts)
  981. {
  982. struct kgsl_drawobj_cmd *cmdobj = CMDOBJ(drawobj);
  983. unsigned int j;
  984. int ret;
  985. ret = get_timestamp(drawctxt, drawobj, timestamp, user_ts);
  986. if (ret)
  987. return ret;
  988. /*
  989. * If this is a real command then we need to force any markers
  990. * queued before it to dispatch to keep time linear - set the
  991. * skip bit so the commands get NOPed.
  992. */
  993. j = drawctxt->drawqueue_head;
  994. while (j != drawctxt->drawqueue_tail) {
  995. if (drawctxt->drawqueue[j]->type == MARKEROBJ_TYPE) {
  996. struct kgsl_drawobj_cmd *markerobj =
  997. CMDOBJ(drawctxt->drawqueue[j]);
  998. set_bit(CMDOBJ_SKIP, &markerobj->priv);
  999. }
  1000. j = DRAWQUEUE_NEXT(j, ADRENO_CONTEXT_DRAWQUEUE_SIZE);
  1001. }
  1002. drawctxt->queued_timestamp = *timestamp;
  1003. _set_ft_policy(adreno_dev, drawctxt, cmdobj);
  1004. _cmdobj_set_flags(drawctxt, cmdobj);
  1005. _queue_drawobj(drawctxt, drawobj);
  1006. return 0;
  1007. }
  1008. static void drawctxt_queue_syncobj(struct adreno_context *drawctxt,
  1009. struct kgsl_drawobj *drawobj, uint32_t *timestamp)
  1010. {
  1011. *timestamp = 0;
  1012. drawobj->timestamp = 0;
  1013. _queue_drawobj(drawctxt, drawobj);
  1014. }
  1015. /*
  1016. * Queue a command in the context - if there isn't any room in the queue, then
  1017. * block until there is
  1018. */
  1019. static int adreno_dispatcher_queue_cmds(struct kgsl_device_private *dev_priv,
  1020. struct kgsl_context *context, struct kgsl_drawobj *drawobj[],
  1021. uint32_t count, uint32_t *timestamp)
  1022. {
  1023. struct kgsl_device *device = dev_priv->device;
  1024. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  1025. struct adreno_context *drawctxt = ADRENO_CONTEXT(context);
  1026. struct adreno_dispatcher_drawqueue *dispatch_q;
  1027. struct adreno_dispatch_job *job;
  1028. int ret;
  1029. unsigned int i, user_ts;
  1030. /*
  1031. * There is always a possibility that dispatcher may end up pushing
  1032. * the last popped draw object back to the context drawqueue. Hence,
  1033. * we can only queue up to _context_drawqueue_size - 1 here to make
  1034. * sure we never let drawqueue->queued exceed _context_drawqueue_size.
  1035. */
  1036. if (!count || count > _context_drawqueue_size - 1)
  1037. return -EINVAL;
  1038. ret = kgsl_check_context_state(&drawctxt->base);
  1039. if (ret)
  1040. return ret;
  1041. ret = adreno_verify_cmdobj(dev_priv, context, drawobj, count);
  1042. if (ret)
  1043. return ret;
  1044. /* wait for the suspend gate */
  1045. wait_for_completion(&device->halt_gate);
  1046. job = kmem_cache_alloc(jobs_cache, GFP_KERNEL);
  1047. if (!job)
  1048. return -ENOMEM;
  1049. job->drawctxt = drawctxt;
  1050. spin_lock(&drawctxt->lock);
  1051. ret = _check_context_state_to_queue_cmds(drawctxt, count);
  1052. if (ret) {
  1053. spin_unlock(&drawctxt->lock);
  1054. kmem_cache_free(jobs_cache, job);
  1055. return ret;
  1056. }
  1057. user_ts = *timestamp;
  1058. /*
  1059. * If there is only one drawobj in the array and it is of
  1060. * type SYNCOBJ_TYPE, skip comparing user_ts as it can be 0
  1061. */
  1062. if (!(count == 1 && drawobj[0]->type == SYNCOBJ_TYPE) &&
  1063. (drawctxt->base.flags & KGSL_CONTEXT_USER_GENERATED_TS)) {
  1064. /*
  1065. * User specified timestamps need to be greater than the last
  1066. * issued timestamp in the context
  1067. */
  1068. if (timestamp_cmp(drawctxt->timestamp, user_ts) >= 0) {
  1069. spin_unlock(&drawctxt->lock);
  1070. kmem_cache_free(jobs_cache, job);
  1071. return -ERANGE;
  1072. }
  1073. }
  1074. for (i = 0; i < count; i++) {
  1075. switch (drawobj[i]->type) {
  1076. case MARKEROBJ_TYPE:
  1077. ret = drawctxt_queue_markerobj(adreno_dev, drawctxt,
  1078. drawobj[i], timestamp, user_ts);
  1079. if (ret) {
  1080. spin_unlock(&drawctxt->lock);
  1081. kmem_cache_free(jobs_cache, job);
  1082. }
  1083. if (ret == 1)
  1084. goto done;
  1085. else if (ret)
  1086. return ret;
  1087. break;
  1088. case CMDOBJ_TYPE:
  1089. ret = drawctxt_queue_cmdobj(adreno_dev, drawctxt,
  1090. drawobj[i], timestamp, user_ts);
  1091. if (ret) {
  1092. spin_unlock(&drawctxt->lock);
  1093. kmem_cache_free(jobs_cache, job);
  1094. return ret;
  1095. }
  1096. break;
  1097. case SYNCOBJ_TYPE:
  1098. drawctxt_queue_syncobj(drawctxt, drawobj[i], timestamp);
  1099. break;
  1100. case BINDOBJ_TYPE:
  1101. ret = drawctxt_queue_bindobj(drawctxt, drawobj[i],
  1102. timestamp, user_ts);
  1103. if (ret) {
  1104. spin_unlock(&drawctxt->lock);
  1105. kmem_cache_free(jobs_cache, job);
  1106. return ret;
  1107. }
  1108. break;
  1109. case TIMELINEOBJ_TYPE:
  1110. drawctxt_queue_timelineobj(drawctxt, drawobj[i]);
  1111. break;
  1112. default:
  1113. spin_unlock(&drawctxt->lock);
  1114. kmem_cache_free(jobs_cache, job);
  1115. return -EINVAL;
  1116. }
  1117. }
  1118. dispatch_q = &(ADRENO_CONTEXT(drawobj[0]->context)->rb->dispatch_q);
  1119. adreno_track_context(adreno_dev, dispatch_q, drawctxt);
  1120. spin_unlock(&drawctxt->lock);
  1121. /* Add the context to the dispatcher pending list */
  1122. if (_kgsl_context_get(&drawctxt->base)) {
  1123. trace_dispatch_queue_context(drawctxt);
  1124. llist_add(&job->node,
  1125. &adreno_dev->dispatcher.jobs[drawctxt->base.priority]);
  1126. } else {
  1127. kmem_cache_free(jobs_cache, job);
  1128. goto done;
  1129. }
  1130. /*
  1131. * Only issue commands if inflight is less than burst -this prevents us
  1132. * from sitting around waiting for the mutex on a busy system - the work
  1133. * loop will schedule it for us. Inflight is mutex protected but the
  1134. * worse that can happen is that it will go to 0 after we check and if
  1135. * it goes to 0 it is because the work loop decremented it and the work
  1136. * queue will try to schedule new commands anyway.
  1137. */
  1138. if (dispatch_q->inflight < _context_drawobj_burst)
  1139. adreno_dispatcher_issuecmds(adreno_dev);
  1140. done:
  1141. if (test_and_clear_bit(ADRENO_CONTEXT_FAULT, &context->priv))
  1142. return -EPROTO;
  1143. return 0;
  1144. }
  1145. /*
  1146. * If an IB inside of the drawobj has a gpuaddr that matches the base
  1147. * passed in then zero the size which effectively skips it when it is submitted
  1148. * in the ringbuffer.
  1149. */
  1150. static void _skip_ib(struct kgsl_drawobj_cmd *cmdobj, uint64_t base)
  1151. {
  1152. struct kgsl_memobj_node *ib;
  1153. list_for_each_entry(ib, &cmdobj->cmdlist, node) {
  1154. if (ib->gpuaddr == base) {
  1155. ib->priv |= MEMOBJ_SKIP;
  1156. if (base)
  1157. return;
  1158. }
  1159. }
  1160. }
  1161. static void _skip_cmd(struct kgsl_drawobj_cmd *cmdobj,
  1162. struct kgsl_drawobj_cmd **replay, int count)
  1163. {
  1164. struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
  1165. struct adreno_context *drawctxt = ADRENO_CONTEXT(drawobj->context);
  1166. int i;
  1167. /*
  1168. * SKIPCMD policy: next IB issued for this context is tentative
  1169. * if it fails we assume that GFT failed and if it succeeds
  1170. * we mark GFT as a success.
  1171. *
  1172. * Find next commandbatch for the faulting context
  1173. * If commandbatch is found
  1174. * a) store the current commandbatch fault_policy in context's next
  1175. * commandbatch fault_policy
  1176. * b) force preamble for next commandbatch
  1177. */
  1178. for (i = 1; i < count; i++) {
  1179. if (DRAWOBJ(replay[i])->context->id == drawobj->context->id) {
  1180. replay[i]->fault_policy = replay[0]->fault_policy;
  1181. set_bit(CMDOBJ_FORCE_PREAMBLE, &replay[i]->priv);
  1182. set_bit(KGSL_FT_SKIPCMD, &replay[i]->fault_recovery);
  1183. break;
  1184. }
  1185. }
  1186. /*
  1187. * If we did not find the next cmd then
  1188. * a) set a flag for next command issued in this context
  1189. * b) store the fault_policy, this fault_policy becomes the policy of
  1190. * next command issued in this context
  1191. */
  1192. if ((i == count) && drawctxt) {
  1193. set_bit(ADRENO_CONTEXT_SKIP_CMD, &drawctxt->base.priv);
  1194. drawctxt->fault_policy = replay[0]->fault_policy;
  1195. }
  1196. /* set the flags to skip this cmdobj */
  1197. set_bit(CMDOBJ_SKIP, &cmdobj->priv);
  1198. cmdobj->fault_recovery = 0;
  1199. }
  1200. static void _skip_frame(struct kgsl_drawobj_cmd *cmdobj,
  1201. struct kgsl_drawobj_cmd **replay, int count)
  1202. {
  1203. struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
  1204. struct adreno_context *drawctxt = ADRENO_CONTEXT(drawobj->context);
  1205. int skip = 1;
  1206. int i;
  1207. for (i = 0; i < count; i++) {
  1208. struct kgsl_drawobj *replay_obj = DRAWOBJ(replay[i]);
  1209. /*
  1210. * Only operate on drawobj's that belong to the
  1211. * faulting context
  1212. */
  1213. if (replay_obj->context->id != drawobj->context->id)
  1214. continue;
  1215. /*
  1216. * Skip all the drawobjs in this context until
  1217. * the EOF flag is seen. If the EOF flag is seen then
  1218. * force the preamble for the next command.
  1219. */
  1220. if (skip) {
  1221. set_bit(CMDOBJ_SKIP, &replay[i]->priv);
  1222. if (replay_obj->flags & KGSL_DRAWOBJ_END_OF_FRAME)
  1223. skip = 0;
  1224. } else {
  1225. set_bit(CMDOBJ_FORCE_PREAMBLE, &replay[i]->priv);
  1226. return;
  1227. }
  1228. }
  1229. /*
  1230. * If the EOF flag hasn't been seen yet then set the flag in the
  1231. * drawctxt to keep looking for it
  1232. */
  1233. if (skip && drawctxt)
  1234. set_bit(ADRENO_CONTEXT_SKIP_EOF, &drawctxt->base.priv);
  1235. /*
  1236. * If we did see the EOF flag then force the preamble on for the
  1237. * next command issued on this context
  1238. */
  1239. if (!skip && drawctxt)
  1240. set_bit(ADRENO_CONTEXT_FORCE_PREAMBLE, &drawctxt->base.priv);
  1241. }
  1242. static void remove_invalidated_cmdobjs(struct kgsl_device *device,
  1243. struct kgsl_drawobj_cmd **replay, int count)
  1244. {
  1245. int i;
  1246. for (i = 0; i < count; i++) {
  1247. struct kgsl_drawobj_cmd *cmdobj = replay[i];
  1248. struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
  1249. if (cmdobj == NULL)
  1250. continue;
  1251. if (kgsl_context_is_bad(drawobj->context)) {
  1252. replay[i] = NULL;
  1253. mutex_lock(&device->mutex);
  1254. kgsl_cancel_events_timestamp(device,
  1255. &drawobj->context->events, drawobj->timestamp);
  1256. mutex_unlock(&device->mutex);
  1257. kgsl_drawobj_destroy(drawobj);
  1258. }
  1259. }
  1260. }
  1261. #define pr_fault(_d, _c, fmt, args...) \
  1262. pr_context(_d, (_c)->context, fmt, ##args)
  1263. static void adreno_fault_header(struct kgsl_device *device,
  1264. struct adreno_ringbuffer *rb, struct kgsl_drawobj_cmd *cmdobj,
  1265. int fault)
  1266. {
  1267. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  1268. struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
  1269. struct adreno_context *drawctxt =
  1270. drawobj ? ADRENO_CONTEXT(drawobj->context) : NULL;
  1271. const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
  1272. unsigned int status, rptr, wptr, ib1sz, ib2sz;
  1273. uint64_t ib1base, ib2base;
  1274. bool gx_on = adreno_gx_is_on(adreno_dev);
  1275. int id = (rb != NULL) ? rb->id : -1;
  1276. const char *type = fault & ADRENO_GMU_FAULT ? "gmu" : "gpu";
  1277. if (!gx_on) {
  1278. if (drawobj != NULL) {
  1279. pr_fault(device, drawobj,
  1280. "%s fault ctx %u ctx_type %s ts %u and GX is OFF\n",
  1281. type, drawobj->context->id,
  1282. kgsl_context_type(drawctxt->type),
  1283. drawobj->timestamp);
  1284. pr_fault(device, drawobj, "cmdline: %s\n",
  1285. drawctxt->base.proc_priv->cmdline);
  1286. } else
  1287. dev_err(device->dev, "RB[%d] : %s fault and GX is OFF\n",
  1288. id, type);
  1289. return;
  1290. }
  1291. if (gpudev->fault_header)
  1292. return gpudev->fault_header(adreno_dev, drawobj);
  1293. adreno_readreg(adreno_dev, ADRENO_REG_RBBM_STATUS, &status);
  1294. adreno_readreg(adreno_dev, ADRENO_REG_CP_RB_RPTR, &rptr);
  1295. adreno_readreg(adreno_dev, ADRENO_REG_CP_RB_WPTR, &wptr);
  1296. adreno_readreg64(adreno_dev, ADRENO_REG_CP_IB1_BASE,
  1297. ADRENO_REG_CP_IB1_BASE_HI, &ib1base);
  1298. adreno_readreg(adreno_dev, ADRENO_REG_CP_IB1_BUFSZ, &ib1sz);
  1299. adreno_readreg64(adreno_dev, ADRENO_REG_CP_IB2_BASE,
  1300. ADRENO_REG_CP_IB2_BASE_HI, &ib2base);
  1301. adreno_readreg(adreno_dev, ADRENO_REG_CP_IB2_BUFSZ, &ib2sz);
  1302. if (drawobj != NULL) {
  1303. drawctxt->base.total_fault_count++;
  1304. drawctxt->base.last_faulted_cmd_ts = drawobj->timestamp;
  1305. trace_adreno_gpu_fault(drawobj->context->id,
  1306. drawobj->timestamp,
  1307. status, rptr, wptr, ib1base, ib1sz,
  1308. ib2base, ib2sz, drawctxt->rb->id);
  1309. pr_fault(device, drawobj,
  1310. "%s fault ctx %u ctx_type %s ts %u status %8.8X rb %4.4x/%4.4x ib1 %16.16llX/%4.4x ib2 %16.16llX/%4.4x\n",
  1311. type, drawobj->context->id,
  1312. kgsl_context_type(drawctxt->type),
  1313. drawobj->timestamp, status,
  1314. rptr, wptr, ib1base, ib1sz, ib2base, ib2sz);
  1315. pr_fault(device, drawobj, "cmdline: %s\n",
  1316. drawctxt->base.proc_priv->cmdline);
  1317. if (rb != NULL)
  1318. pr_fault(device, drawobj,
  1319. "%s fault rb %d rb sw r/w %4.4x/%4.4x\n",
  1320. type, rb->id, rptr, rb->wptr);
  1321. } else {
  1322. dev_err(device->dev,
  1323. "RB[%d] : %s fault status %8.8X rb %4.4x/%4.4x ib1 %16.16llX/%4.4x ib2 %16.16llX/%4.4x\n",
  1324. id, type, status, rptr, wptr, ib1base, ib1sz, ib2base,
  1325. ib2sz);
  1326. if (rb != NULL)
  1327. dev_err(device->dev,
  1328. "RB[%d] : %s fault rb sw r/w %4.4x/%4.4x\n",
  1329. rb->id, type, rptr, rb->wptr);
  1330. }
  1331. }
  1332. void adreno_fault_skipcmd_detached(struct adreno_device *adreno_dev,
  1333. struct adreno_context *drawctxt,
  1334. struct kgsl_drawobj *drawobj)
  1335. {
  1336. if (test_bit(ADRENO_CONTEXT_SKIP_CMD, &drawctxt->base.priv) &&
  1337. kgsl_context_detached(&drawctxt->base)) {
  1338. pr_context(KGSL_DEVICE(adreno_dev), drawobj->context,
  1339. "gpu detached context %d\n", drawobj->context->id);
  1340. clear_bit(ADRENO_CONTEXT_SKIP_CMD, &drawctxt->base.priv);
  1341. }
  1342. }
  1343. /**
  1344. * process_cmdobj_fault() - Process a cmdobj for fault policies
  1345. * @device: Device on which the cmdobj caused a fault
  1346. * @replay: List of cmdobj's that are to be replayed on the device. The
  1347. * first command in the replay list is the faulting command and the remaining
  1348. * cmdobj's in the list are commands that were submitted to the same queue
  1349. * as the faulting one.
  1350. * @count: Number of cmdobj's in replay
  1351. * @base: The IB1 base at the time of fault
  1352. * @fault: The fault type
  1353. */
  1354. static void process_cmdobj_fault(struct kgsl_device *device,
  1355. struct kgsl_drawobj_cmd **replay, int count,
  1356. unsigned int base,
  1357. int fault)
  1358. {
  1359. struct kgsl_drawobj_cmd *cmdobj = replay[0];
  1360. struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
  1361. int i;
  1362. char *state = "failed";
  1363. /*
  1364. * If GFT recovered more than X times in Y ms invalidate the context
  1365. * and do not attempt recovery.
  1366. * Example: X==3 and Y==3000 ms, GPU hung at 500ms, 1700ms, 25000ms and
  1367. * 3000ms for the same context, we will not try FT and invalidate the
  1368. * context @3000ms because context triggered GFT more than 3 times in
  1369. * last 3 seconds. If a context caused recoverable GPU hangs
  1370. * where 1st and 4th gpu hang are more than 3 seconds apart we
  1371. * won't disable GFT and invalidate the context.
  1372. */
  1373. if (test_bit(KGSL_FT_THROTTLE, &cmdobj->fault_policy)) {
  1374. if (ktime_ms_delta(ktime_get(), drawobj->context->fault_time) >
  1375. _fault_throttle_time) {
  1376. drawobj->context->fault_time = ktime_get();
  1377. drawobj->context->fault_count = 1;
  1378. } else {
  1379. drawobj->context->fault_count++;
  1380. if (drawobj->context->fault_count >
  1381. _fault_throttle_burst) {
  1382. set_bit(KGSL_FT_DISABLE,
  1383. &cmdobj->fault_policy);
  1384. pr_context(device, drawobj->context,
  1385. "gpu fault threshold exceeded %d faults in %d msecs\n",
  1386. _fault_throttle_burst,
  1387. _fault_throttle_time);
  1388. }
  1389. }
  1390. }
  1391. /*
  1392. * If FT is disabled for this cmdobj invalidate immediately
  1393. */
  1394. if (test_bit(KGSL_FT_DISABLE, &cmdobj->fault_policy) ||
  1395. test_bit(KGSL_FT_TEMP_DISABLE, &cmdobj->fault_policy)) {
  1396. state = "skipped";
  1397. bitmap_zero(&cmdobj->fault_policy, BITS_PER_LONG);
  1398. }
  1399. /* If the context is detached do not run FT on context */
  1400. if (kgsl_context_detached(drawobj->context)) {
  1401. state = "detached";
  1402. bitmap_zero(&cmdobj->fault_policy, BITS_PER_LONG);
  1403. }
  1404. /*
  1405. * Set a flag so we don't print another PM dump if the cmdobj fails
  1406. * again on replay
  1407. */
  1408. set_bit(KGSL_FT_SKIP_PMDUMP, &cmdobj->fault_policy);
  1409. /*
  1410. * A hardware fault generally means something was deterministically
  1411. * wrong with the cmdobj - no point in trying to replay it
  1412. * Clear the replay bit and move on to the next policy level
  1413. */
  1414. if (fault & ADRENO_HARD_FAULT)
  1415. clear_bit(KGSL_FT_REPLAY, &(cmdobj->fault_policy));
  1416. /*
  1417. * A timeout fault means the IB timed out - clear the policy and
  1418. * invalidate - this will clear the FT_SKIP_PMDUMP bit but that is okay
  1419. * because we won't see this cmdobj again
  1420. */
  1421. if ((fault & ADRENO_TIMEOUT_FAULT) ||
  1422. (fault & ADRENO_CTX_DETATCH_TIMEOUT_FAULT))
  1423. bitmap_zero(&cmdobj->fault_policy, BITS_PER_LONG);
  1424. /*
  1425. * If the context had a GPU page fault then it is likely it would fault
  1426. * again if replayed
  1427. */
  1428. if (test_bit(KGSL_CONTEXT_PRIV_PAGEFAULT,
  1429. &drawobj->context->priv)) {
  1430. /* we'll need to resume the mmu later... */
  1431. clear_bit(KGSL_FT_REPLAY, &cmdobj->fault_policy);
  1432. clear_bit(KGSL_CONTEXT_PRIV_PAGEFAULT,
  1433. &drawobj->context->priv);
  1434. }
  1435. /*
  1436. * Execute the fault tolerance policy. Each cmdobj stores the
  1437. * current fault policy that was set when it was queued.
  1438. * As the options are tried in descending priority
  1439. * (REPLAY -> SKIPIBS -> SKIPFRAME -> NOTHING) the bits are cleared
  1440. * from the cmdobj policy so the next thing can be tried if the
  1441. * change comes around again
  1442. */
  1443. /* Replay the hanging cmdobj again */
  1444. if (test_and_clear_bit(KGSL_FT_REPLAY, &cmdobj->fault_policy)) {
  1445. trace_adreno_cmdbatch_recovery(cmdobj, BIT(KGSL_FT_REPLAY));
  1446. set_bit(KGSL_FT_REPLAY, &cmdobj->fault_recovery);
  1447. return;
  1448. }
  1449. /*
  1450. * Skip the last IB1 that was played but replay everything else.
  1451. * Note that the last IB1 might not be in the "hung" cmdobj
  1452. * because the CP may have caused a page-fault while it was prefetching
  1453. * the next IB1/IB2. walk all outstanding commands and zap the
  1454. * supposedly bad IB1 where ever it lurks.
  1455. */
  1456. if (test_and_clear_bit(KGSL_FT_SKIPIB, &cmdobj->fault_policy)) {
  1457. trace_adreno_cmdbatch_recovery(cmdobj, BIT(KGSL_FT_SKIPIB));
  1458. set_bit(KGSL_FT_SKIPIB, &cmdobj->fault_recovery);
  1459. for (i = 0; i < count; i++) {
  1460. if (replay[i] != NULL &&
  1461. DRAWOBJ(replay[i])->context->id ==
  1462. drawobj->context->id)
  1463. _skip_ib(replay[i], base);
  1464. }
  1465. return;
  1466. }
  1467. /* Skip the faulted cmdobj submission */
  1468. if (test_and_clear_bit(KGSL_FT_SKIPCMD, &cmdobj->fault_policy)) {
  1469. trace_adreno_cmdbatch_recovery(cmdobj, BIT(KGSL_FT_SKIPCMD));
  1470. /* Skip faulting cmdobj */
  1471. _skip_cmd(cmdobj, replay, count);
  1472. return;
  1473. }
  1474. if (test_and_clear_bit(KGSL_FT_SKIPFRAME, &cmdobj->fault_policy)) {
  1475. trace_adreno_cmdbatch_recovery(cmdobj,
  1476. BIT(KGSL_FT_SKIPFRAME));
  1477. set_bit(KGSL_FT_SKIPFRAME, &cmdobj->fault_recovery);
  1478. /*
  1479. * Skip all the pending cmdobj's for this context until
  1480. * the EOF frame is seen
  1481. */
  1482. _skip_frame(cmdobj, replay, count);
  1483. return;
  1484. }
  1485. /* If we get here then all the policies failed */
  1486. pr_context(device, drawobj->context, "gpu %s ctx %d ts %u\n",
  1487. state, drawobj->context->id, drawobj->timestamp);
  1488. /* Mark the context as failed and invalidate it */
  1489. adreno_drawctxt_set_guilty(device, drawobj->context);
  1490. }
  1491. /**
  1492. * recover_dispatch_q() - Recover all commands in a dispatch queue by
  1493. * resubmitting the commands
  1494. * @device: Device on which recovery is performed
  1495. * @dispatch_q: The command queue to recover
  1496. * @fault: Faults caused by the command in the dispatch q
  1497. * @base: The IB1 base during the fault
  1498. */
  1499. static void recover_dispatch_q(struct kgsl_device *device,
  1500. struct adreno_dispatcher_drawqueue *dispatch_q,
  1501. int fault,
  1502. unsigned int base)
  1503. {
  1504. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  1505. struct kgsl_drawobj_cmd **replay;
  1506. unsigned int ptr;
  1507. int first = 0;
  1508. int count = 0;
  1509. int i;
  1510. /* Allocate memory to store the inflight commands */
  1511. replay = kcalloc(dispatch_q->inflight, sizeof(*replay), GFP_KERNEL);
  1512. if (replay == NULL) {
  1513. unsigned int ptr = dispatch_q->head;
  1514. /* Recovery failed - mark everybody on this q guilty */
  1515. while (ptr != dispatch_q->tail) {
  1516. struct kgsl_drawobj_cmd *cmdobj =
  1517. dispatch_q->cmd_q[ptr];
  1518. struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
  1519. adreno_drawctxt_set_guilty(device, drawobj->context);
  1520. kgsl_drawobj_destroy(drawobj);
  1521. ptr = DRAWQUEUE_NEXT(ptr,
  1522. ADRENO_DISPATCH_DRAWQUEUE_SIZE);
  1523. }
  1524. /*
  1525. * Set the replay count to zero - this will ensure that the
  1526. * hardware gets reset but nothing else gets played
  1527. */
  1528. count = 0;
  1529. goto replay;
  1530. }
  1531. /* Copy the inflight cmdobj's into the temporary storage */
  1532. ptr = dispatch_q->head;
  1533. while (ptr != dispatch_q->tail) {
  1534. replay[count++] = dispatch_q->cmd_q[ptr];
  1535. ptr = DRAWQUEUE_NEXT(ptr, ADRENO_DISPATCH_DRAWQUEUE_SIZE);
  1536. }
  1537. if (fault && count)
  1538. process_cmdobj_fault(device, replay,
  1539. count, base, fault);
  1540. replay:
  1541. dispatch_q->inflight = 0;
  1542. dispatch_q->head = dispatch_q->tail = 0;
  1543. /* Remove any pending cmdobj's that have been invalidated */
  1544. remove_invalidated_cmdobjs(device, replay, count);
  1545. /* Replay the pending command buffers */
  1546. for (i = 0; i < count; i++) {
  1547. int ret;
  1548. if (replay[i] == NULL)
  1549. continue;
  1550. /*
  1551. * Force the preamble on the first command (if applicable) to
  1552. * avoid any strange stage issues
  1553. */
  1554. if (first == 0) {
  1555. set_bit(CMDOBJ_FORCE_PREAMBLE, &replay[i]->priv);
  1556. first = 1;
  1557. }
  1558. /*
  1559. * Force each cmdobj to wait for idle - this avoids weird
  1560. * CP parse issues
  1561. */
  1562. set_bit(CMDOBJ_WFI, &replay[i]->priv);
  1563. ret = sendcmd(adreno_dev, replay[i]);
  1564. /*
  1565. * If sending the command fails, then try to recover by
  1566. * invalidating the context
  1567. */
  1568. if (ret) {
  1569. pr_context(device, replay[i]->base.context,
  1570. "gpu reset failed ctx %u ts %u\n",
  1571. replay[i]->base.context->id,
  1572. replay[i]->base.timestamp);
  1573. /* Mark this context as guilty (failed recovery) */
  1574. adreno_drawctxt_set_guilty(device, replay[i]->base.context);
  1575. remove_invalidated_cmdobjs(device, &replay[i],
  1576. count - i);
  1577. }
  1578. }
  1579. /* Clear the fault bit */
  1580. clear_bit(ADRENO_DEVICE_FAULT, &adreno_dev->priv);
  1581. kfree(replay);
  1582. }
  1583. static void do_header_and_snapshot(struct kgsl_device *device, int fault,
  1584. struct adreno_ringbuffer *rb, struct kgsl_drawobj_cmd *cmdobj)
  1585. {
  1586. struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
  1587. /* Always dump the snapshot on a non-drawobj failure */
  1588. if (cmdobj == NULL) {
  1589. adreno_fault_header(device, rb, NULL, fault);
  1590. /* GMU snapshot will also pull a full device snapshot */
  1591. if (fault & ADRENO_GMU_FAULT)
  1592. gmu_core_fault_snapshot(device);
  1593. else
  1594. kgsl_device_snapshot(device, NULL, NULL, false);
  1595. return;
  1596. }
  1597. /* Skip everything if the PMDUMP flag is set */
  1598. if (test_bit(KGSL_FT_SKIP_PMDUMP, &cmdobj->fault_policy))
  1599. return;
  1600. /* Print the fault header */
  1601. adreno_fault_header(device, rb, cmdobj, fault);
  1602. if (!(drawobj->context->flags & KGSL_CONTEXT_NO_SNAPSHOT))
  1603. kgsl_device_snapshot(device, drawobj->context, NULL,
  1604. fault & ADRENO_GMU_FAULT);
  1605. }
  1606. static int dispatcher_do_fault(struct adreno_device *adreno_dev)
  1607. {
  1608. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  1609. const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
  1610. struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
  1611. struct adreno_dispatcher_drawqueue *dispatch_q = NULL, *dispatch_q_temp;
  1612. struct adreno_ringbuffer *rb;
  1613. struct adreno_ringbuffer *hung_rb = NULL;
  1614. unsigned int reg;
  1615. uint64_t base = 0;
  1616. struct kgsl_drawobj_cmd *cmdobj = NULL;
  1617. int ret, i;
  1618. int fault;
  1619. int halt;
  1620. bool gx_on;
  1621. fault = atomic_xchg(&dispatcher->fault, 0);
  1622. if (fault == 0)
  1623. return 0;
  1624. mutex_lock(&device->mutex);
  1625. /*
  1626. * In the very unlikely case that the power is off, do nothing - the
  1627. * state will be reset on power up and everybody will be happy
  1628. */
  1629. if (!kgsl_state_is_awake(device)) {
  1630. mutex_unlock(&device->mutex);
  1631. return 0;
  1632. }
  1633. /* Mask all GMU interrupts */
  1634. if (gmu_core_isenabled(device)) {
  1635. adreno_write_gmureg(adreno_dev,
  1636. ADRENO_REG_GMU_AO_HOST_INTERRUPT_MASK,
  1637. 0xFFFFFFFF);
  1638. adreno_write_gmureg(adreno_dev,
  1639. ADRENO_REG_GMU_GMU2HOST_INTR_MASK,
  1640. 0xFFFFFFFF);
  1641. }
  1642. gx_on = adreno_gx_is_on(adreno_dev);
  1643. /*
  1644. * On non-A3xx, Check if this function was entered after a pagefault. If so, only
  1645. * proceed if the fault handler has already run in the IRQ thread,
  1646. * else return early to give the fault handler a chance to run.
  1647. */
  1648. if (!(fault & ADRENO_IOMMU_PAGE_FAULT) &&
  1649. !adreno_is_a3xx(adreno_dev) && gx_on) {
  1650. if (adreno_smmu_is_stalled(adreno_dev)) {
  1651. mutex_unlock(&device->mutex);
  1652. dev_err(device->dev,
  1653. "SMMU is stalled without a pagefault\n");
  1654. return -EBUSY;
  1655. }
  1656. }
  1657. /* Turn off all the timers */
  1658. del_timer_sync(&dispatcher->timer);
  1659. adreno_dispatcher_stop_fault_timer(device);
  1660. /*
  1661. * Deleting uninitialized timer will block for ever on kernel debug
  1662. * disable build. Hence skip del timer if it is not initialized.
  1663. */
  1664. if (adreno_is_preemption_enabled(adreno_dev))
  1665. del_timer_sync(&adreno_dev->preempt.timer);
  1666. if (gx_on)
  1667. adreno_readreg64(adreno_dev, ADRENO_REG_CP_RB_BASE,
  1668. ADRENO_REG_CP_RB_BASE_HI, &base);
  1669. /*
  1670. * Force the CP off for anything but a hard fault to make sure it is
  1671. * good and stopped
  1672. */
  1673. if (!(fault & ADRENO_HARD_FAULT) && gx_on) {
  1674. adreno_readreg(adreno_dev, ADRENO_REG_CP_ME_CNTL, &reg);
  1675. if (adreno_is_a3xx(adreno_dev))
  1676. reg |= (1 << 27) | (1 << 28);
  1677. else if (adreno_is_a5xx(adreno_dev) || adreno_is_a6xx(adreno_dev))
  1678. reg |= 1 | (1 << 1);
  1679. else
  1680. reg = 0x0;
  1681. adreno_writereg(adreno_dev, ADRENO_REG_CP_ME_CNTL, reg);
  1682. }
  1683. /*
  1684. * retire cmdobj's from all the dispatch_q's before starting recovery
  1685. */
  1686. FOR_EACH_RINGBUFFER(adreno_dev, rb, i) {
  1687. adreno_dispatch_retire_drawqueue(adreno_dev,
  1688. &(rb->dispatch_q));
  1689. /* Select the active dispatch_q */
  1690. if (base == rb->buffer_desc->gpuaddr) {
  1691. dispatch_q = &(rb->dispatch_q);
  1692. hung_rb = rb;
  1693. if (adreno_dev->cur_rb != hung_rb) {
  1694. adreno_dev->prev_rb = adreno_dev->cur_rb;
  1695. adreno_dev->cur_rb = hung_rb;
  1696. }
  1697. }
  1698. }
  1699. if (dispatch_q && !adreno_drawqueue_is_empty(dispatch_q)) {
  1700. cmdobj = dispatch_q->cmd_q[dispatch_q->head];
  1701. trace_adreno_cmdbatch_fault(cmdobj, fault);
  1702. }
  1703. if (gx_on)
  1704. adreno_readreg64(adreno_dev, ADRENO_REG_CP_IB1_BASE,
  1705. ADRENO_REG_CP_IB1_BASE_HI, &base);
  1706. if (!test_bit(KGSL_FT_PAGEFAULT_GPUHALT_ENABLE, &device->mmu.pfpolicy)
  1707. && adreno_dev->cooperative_reset)
  1708. gmu_core_dev_cooperative_reset(device);
  1709. if (!(fault & ADRENO_GMU_FAULT_SKIP_SNAPSHOT))
  1710. do_header_and_snapshot(device, fault, hung_rb, cmdobj);
  1711. /* Turn off the KEEPALIVE vote from the ISR for hard fault */
  1712. if (gpudev->gpu_keepalive && fault & ADRENO_HARD_FAULT)
  1713. gpudev->gpu_keepalive(adreno_dev, false);
  1714. /* Terminate the stalled transaction and resume the IOMMU */
  1715. if (fault & ADRENO_IOMMU_PAGE_FAULT)
  1716. kgsl_mmu_pagefault_resume(&device->mmu, true);
  1717. /* Reset the dispatcher queue */
  1718. dispatcher->inflight = 0;
  1719. /* Remove the bus hint */
  1720. device->pwrctrl.rt_bus_hint_active = false;
  1721. /* Reset the GPU and make sure halt is not set during recovery */
  1722. halt = adreno_gpu_halt(adreno_dev);
  1723. adreno_clear_gpu_halt(adreno_dev);
  1724. /*
  1725. * If there is a stall in the ringbuffer after all commands have been
  1726. * retired then we could hit problems if contexts are waiting for
  1727. * internal timestamps that will never retire
  1728. */
  1729. if (hung_rb != NULL) {
  1730. kgsl_sharedmem_writel(device->memstore,
  1731. MEMSTORE_RB_OFFSET(hung_rb, soptimestamp),
  1732. hung_rb->timestamp);
  1733. kgsl_sharedmem_writel(device->memstore,
  1734. MEMSTORE_RB_OFFSET(hung_rb, eoptimestamp),
  1735. hung_rb->timestamp);
  1736. /* Schedule any pending events to be run */
  1737. kgsl_process_event_group(device, &hung_rb->events);
  1738. }
  1739. ret = adreno_reset(device, fault);
  1740. mutex_unlock(&device->mutex);
  1741. /* If adreno_reset() fails then what hope do we have for the future? */
  1742. BUG_ON(ret);
  1743. /* if any other fault got in until reset then ignore */
  1744. atomic_set(&dispatcher->fault, 0);
  1745. /* recover all the dispatch_q's starting with the one that hung */
  1746. if (dispatch_q)
  1747. recover_dispatch_q(device, dispatch_q, fault, base);
  1748. FOR_EACH_RINGBUFFER(adreno_dev, rb, i) {
  1749. dispatch_q_temp = &(rb->dispatch_q);
  1750. if (dispatch_q_temp != dispatch_q)
  1751. recover_dispatch_q(device, dispatch_q_temp, 0, base);
  1752. }
  1753. atomic_add(halt, &adreno_dev->halt);
  1754. return 1;
  1755. }
  1756. static inline int drawobj_consumed(struct kgsl_drawobj *drawobj,
  1757. unsigned int consumed, unsigned int retired)
  1758. {
  1759. return ((timestamp_cmp(drawobj->timestamp, consumed) >= 0) &&
  1760. (timestamp_cmp(retired, drawobj->timestamp) < 0));
  1761. }
  1762. static const char *_ft_type(enum kgsl_ft_policy_bits nr)
  1763. {
  1764. if (nr == KGSL_FT_OFF)
  1765. return "off";
  1766. else if (nr == KGSL_FT_REPLAY)
  1767. return "replay";
  1768. else if (nr == KGSL_FT_SKIPIB)
  1769. return "skipib";
  1770. else if (nr == KGSL_FT_SKIPFRAME)
  1771. return "skipfame";
  1772. else if (nr == KGSL_FT_DISABLE)
  1773. return "disable";
  1774. else if (nr == KGSL_FT_TEMP_DISABLE)
  1775. return "temp";
  1776. else if (nr == KGSL_FT_THROTTLE)
  1777. return "throttle";
  1778. else if (nr == KGSL_FT_SKIPCMD)
  1779. return "skipcmd";
  1780. return "";
  1781. }
  1782. static void _print_recovery(struct kgsl_device *device,
  1783. struct kgsl_drawobj_cmd *cmdobj)
  1784. {
  1785. int nr = find_first_bit(&cmdobj->fault_recovery, BITS_PER_LONG);
  1786. struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
  1787. pr_context(device, drawobj->context,
  1788. "gpu %s ctx %u ts %u policy %lX\n",
  1789. _ft_type(nr), drawobj->context->id, drawobj->timestamp,
  1790. cmdobj->fault_recovery);
  1791. }
  1792. static void cmdobj_profile_ticks(struct adreno_device *adreno_dev,
  1793. struct kgsl_drawobj_cmd *cmdobj, uint64_t *start, uint64_t *retire,
  1794. uint64_t *active)
  1795. {
  1796. void *ptr = adreno_dev->profile_buffer->hostptr;
  1797. struct adreno_drawobj_profile_entry *entry;
  1798. entry = (struct adreno_drawobj_profile_entry *)
  1799. (ptr + (cmdobj->profile_index * sizeof(*entry)));
  1800. /* get updated values of started and retired */
  1801. rmb();
  1802. *start = entry->started;
  1803. *retire = entry->retired;
  1804. if (ADRENO_GPUREV(adreno_dev) < 600)
  1805. *active = entry->retired - entry->started;
  1806. else
  1807. *active = entry->ctx_end - entry->ctx_start;
  1808. }
  1809. static void retire_cmdobj(struct adreno_device *adreno_dev,
  1810. struct kgsl_drawobj_cmd *cmdobj)
  1811. {
  1812. struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
  1813. struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
  1814. struct adreno_context *drawctxt = ADRENO_CONTEXT(drawobj->context);
  1815. struct adreno_ringbuffer *rb = drawctxt->rb;
  1816. struct kgsl_context *context = drawobj->context;
  1817. uint64_t start = 0, end = 0, active = 0;
  1818. struct retire_info info = {0};
  1819. if (cmdobj->fault_recovery != 0) {
  1820. set_bit(ADRENO_CONTEXT_FAULT, &drawobj->context->priv);
  1821. _print_recovery(KGSL_DEVICE(adreno_dev), cmdobj);
  1822. }
  1823. if (test_bit(CMDOBJ_PROFILE, &cmdobj->priv))
  1824. cmdobj_profile_ticks(adreno_dev, cmdobj, &start, &end, &active);
  1825. info.inflight = (int)dispatcher->inflight;
  1826. info.rb_id = rb->id;
  1827. info.wptr = rb->wptr;
  1828. info.timestamp = drawobj->timestamp;
  1829. info.sop = start;
  1830. info.eop = end;
  1831. info.active = active;
  1832. /* protected GPU work must not be reported */
  1833. if (!(context->flags & KGSL_CONTEXT_SECURE))
  1834. kgsl_work_period_update(KGSL_DEVICE(adreno_dev),
  1835. context->proc_priv->period, active);
  1836. msm_perf_events_update(MSM_PERF_GFX, MSM_PERF_RETIRED,
  1837. pid_nr(context->proc_priv->pid),
  1838. context->id, drawobj->timestamp,
  1839. !!(drawobj->flags & KGSL_DRAWOBJ_END_OF_FRAME));
  1840. if (drawobj->flags & KGSL_DRAWOBJ_END_OF_FRAME) {
  1841. atomic64_inc(&context->proc_priv->frame_count);
  1842. atomic_inc(&context->proc_priv->period->frames);
  1843. }
  1844. /*
  1845. * For A3xx we still get the rptr from the CP_RB_RPTR instead of
  1846. * rptr scratch out address. At this point GPU clocks turned off.
  1847. * So avoid reading GPU register directly for A3xx.
  1848. */
  1849. if (adreno_is_a3xx(adreno_dev)) {
  1850. trace_adreno_cmdbatch_retired(drawobj->context, &info,
  1851. drawobj->flags, rb->dispatch_q.inflight,
  1852. cmdobj->fault_recovery);
  1853. } else {
  1854. info.rptr = adreno_get_rptr(rb);
  1855. trace_adreno_cmdbatch_retired(drawobj->context, &info,
  1856. drawobj->flags, rb->dispatch_q.inflight,
  1857. cmdobj->fault_recovery);
  1858. }
  1859. log_kgsl_cmdbatch_retired_event(context->id, drawobj->timestamp,
  1860. context->priority, drawobj->flags, start, end);
  1861. drawctxt->submit_retire_ticks[drawctxt->ticks_index] =
  1862. end - cmdobj->submit_ticks;
  1863. drawctxt->ticks_index = (drawctxt->ticks_index + 1) %
  1864. SUBMIT_RETIRE_TICKS_SIZE;
  1865. trace_adreno_cmdbatch_done(drawobj->context->id,
  1866. drawobj->context->priority, drawobj->timestamp);
  1867. kgsl_drawobj_destroy(drawobj);
  1868. }
  1869. static int adreno_dispatch_retire_drawqueue(struct adreno_device *adreno_dev,
  1870. struct adreno_dispatcher_drawqueue *drawqueue)
  1871. {
  1872. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  1873. struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
  1874. int count = 0;
  1875. while (!adreno_drawqueue_is_empty(drawqueue)) {
  1876. struct kgsl_drawobj_cmd *cmdobj =
  1877. drawqueue->cmd_q[drawqueue->head];
  1878. struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
  1879. if (!kgsl_check_timestamp(device, drawobj->context,
  1880. drawobj->timestamp))
  1881. break;
  1882. retire_cmdobj(adreno_dev, cmdobj);
  1883. dispatcher->inflight--;
  1884. drawqueue->inflight--;
  1885. drawqueue->cmd_q[drawqueue->head] = NULL;
  1886. drawqueue->head = DRAWQUEUE_NEXT(drawqueue->head,
  1887. ADRENO_DISPATCH_DRAWQUEUE_SIZE);
  1888. count++;
  1889. }
  1890. return count;
  1891. }
  1892. static void _adreno_dispatch_check_timeout(struct adreno_device *adreno_dev,
  1893. struct adreno_dispatcher_drawqueue *drawqueue)
  1894. {
  1895. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  1896. struct kgsl_drawobj *drawobj =
  1897. DRAWOBJ(drawqueue->cmd_q[drawqueue->head]);
  1898. /* Don't timeout if the timer hasn't expired yet (duh) */
  1899. if (time_is_after_jiffies(drawqueue->expires))
  1900. return;
  1901. /* Don't timeout if the IB timeout is disabled globally */
  1902. if (!adreno_long_ib_detect(adreno_dev))
  1903. return;
  1904. /* Don't time out if the context has disabled it */
  1905. if (drawobj->context->flags & KGSL_CONTEXT_NO_FAULT_TOLERANCE)
  1906. return;
  1907. pr_context(device, drawobj->context, "gpu timeout ctx %u ts %u\n",
  1908. drawobj->context->id, drawobj->timestamp);
  1909. adreno_set_gpu_fault(adreno_dev, ADRENO_TIMEOUT_FAULT);
  1910. /*
  1911. * This makes sure dispatcher doesn't run endlessly in cases where
  1912. * we couldn't run recovery
  1913. */
  1914. drawqueue->expires = jiffies + msecs_to_jiffies(adreno_drawobj_timeout);
  1915. }
  1916. static int adreno_dispatch_process_drawqueue(struct adreno_device *adreno_dev,
  1917. struct adreno_dispatcher_drawqueue *drawqueue)
  1918. {
  1919. int count = adreno_dispatch_retire_drawqueue(adreno_dev, drawqueue);
  1920. /* Nothing to do if there are no pending commands */
  1921. if (adreno_drawqueue_is_empty(drawqueue))
  1922. return count;
  1923. /* Don't update the drawqueue timeout if it isn't active */
  1924. if (!drawqueue_is_current(drawqueue))
  1925. return count;
  1926. /*
  1927. * If the current ringbuffer retired any commands then universally
  1928. * reset the timeout
  1929. */
  1930. if (count) {
  1931. drawqueue->expires = jiffies +
  1932. msecs_to_jiffies(adreno_drawobj_timeout);
  1933. return count;
  1934. }
  1935. /*
  1936. * If we get here then 1) the ringbuffer is current and 2) we haven't
  1937. * retired anything. Check to see if the timeout if valid for the
  1938. * current drawobj and fault if it has expired
  1939. */
  1940. _adreno_dispatch_check_timeout(adreno_dev, drawqueue);
  1941. return 0;
  1942. }
  1943. /* Take down the dispatcher and release any power states */
  1944. static void _dispatcher_power_down(struct adreno_device *adreno_dev)
  1945. {
  1946. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  1947. struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
  1948. mutex_lock(&device->mutex);
  1949. if (test_and_clear_bit(ADRENO_DISPATCHER_ACTIVE, &dispatcher->priv))
  1950. complete_all(&dispatcher->idle_gate);
  1951. adreno_dispatcher_stop_fault_timer(device);
  1952. process_rt_bus_hint(device, false);
  1953. if (test_bit(ADRENO_DISPATCHER_POWER, &dispatcher->priv)) {
  1954. adreno_active_count_put(adreno_dev);
  1955. clear_bit(ADRENO_DISPATCHER_POWER, &dispatcher->priv);
  1956. }
  1957. mutex_unlock(&device->mutex);
  1958. }
  1959. static void adreno_dispatcher_work(struct kthread_work *work)
  1960. {
  1961. struct adreno_dispatcher *dispatcher =
  1962. container_of(work, struct adreno_dispatcher, work);
  1963. struct adreno_device *adreno_dev =
  1964. container_of(dispatcher, struct adreno_device, dispatcher);
  1965. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  1966. const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
  1967. int count = 0;
  1968. unsigned int i = 0;
  1969. mutex_lock(&dispatcher->mutex);
  1970. /*
  1971. * As long as there are inflight commands, process retired comamnds from
  1972. * all drawqueues
  1973. */
  1974. for (i = 0; i < adreno_dev->num_ringbuffers; i++) {
  1975. struct adreno_dispatcher_drawqueue *drawqueue =
  1976. DRAWQUEUE(&adreno_dev->ringbuffers[i]);
  1977. count += adreno_dispatch_process_drawqueue(adreno_dev,
  1978. drawqueue);
  1979. if (dispatcher->inflight == 0)
  1980. break;
  1981. }
  1982. kgsl_process_event_groups(device);
  1983. /*
  1984. * dispatcher_do_fault() returns 0 if no faults occurred. If that is the
  1985. * case, then clean up preemption and try to schedule more work
  1986. */
  1987. if (dispatcher_do_fault(adreno_dev) == 0) {
  1988. /* Clean up after preemption */
  1989. if (gpudev->preemption_schedule)
  1990. gpudev->preemption_schedule(adreno_dev);
  1991. /* Run the scheduler for to dispatch new commands */
  1992. _adreno_dispatcher_issuecmds(adreno_dev);
  1993. }
  1994. /*
  1995. * If there are commands pending, update the timers, otherwise release
  1996. * the power state to prepare for power down
  1997. */
  1998. if (dispatcher->inflight > 0)
  1999. _dispatcher_update_timers(adreno_dev);
  2000. else
  2001. _dispatcher_power_down(adreno_dev);
  2002. mutex_unlock(&dispatcher->mutex);
  2003. }
  2004. void adreno_dispatcher_schedule(struct kgsl_device *device)
  2005. {
  2006. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  2007. struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
  2008. kthread_queue_work(dispatcher->worker, &dispatcher->work);
  2009. }
  2010. /*
  2011. * Put a draw context on the dispatcher pending queue and schedule the
  2012. * dispatcher. This is used to reschedule changes that might have been blocked
  2013. * for sync points or other concerns
  2014. */
  2015. static void adreno_dispatcher_queue_context(struct adreno_device *adreno_dev,
  2016. struct adreno_context *drawctxt)
  2017. {
  2018. dispatcher_queue_context(adreno_dev, drawctxt);
  2019. adreno_dispatcher_schedule(KGSL_DEVICE(adreno_dev));
  2020. }
  2021. void adreno_dispatcher_fault(struct adreno_device *adreno_dev,
  2022. u32 fault)
  2023. {
  2024. adreno_set_gpu_fault(adreno_dev, fault);
  2025. adreno_dispatcher_schedule(KGSL_DEVICE(adreno_dev));
  2026. }
  2027. /*
  2028. * This is called when the timer expires - it either means the GPU is hung or
  2029. * the IB is taking too long to execute
  2030. */
  2031. static void adreno_dispatcher_timer(struct timer_list *t)
  2032. {
  2033. struct adreno_dispatcher *dispatcher = from_timer(dispatcher, t, timer);
  2034. struct adreno_device *adreno_dev = container_of(dispatcher,
  2035. struct adreno_device, dispatcher);
  2036. adreno_dispatcher_schedule(KGSL_DEVICE(adreno_dev));
  2037. }
  2038. /**
  2039. * adreno_dispatcher_start() - activate the dispatcher
  2040. * @adreno_dev: pointer to the adreno device structure
  2041. *
  2042. */
  2043. void adreno_dispatcher_start(struct kgsl_device *device)
  2044. {
  2045. complete_all(&device->halt_gate);
  2046. /* Schedule the work loop to get things going */
  2047. adreno_dispatcher_schedule(device);
  2048. }
  2049. /**
  2050. * adreno_dispatcher_stop() - stop the dispatcher
  2051. * @adreno_dev: pointer to the adreno device structure
  2052. *
  2053. * Stop the dispatcher and close all the timers
  2054. */
  2055. void adreno_dispatcher_stop(struct adreno_device *adreno_dev)
  2056. {
  2057. struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
  2058. del_timer_sync(&dispatcher->timer);
  2059. adreno_dispatcher_stop_fault_timer(KGSL_DEVICE(adreno_dev));
  2060. }
  2061. /* Return the ringbuffer that matches the draw context priority */
  2062. static struct adreno_ringbuffer *dispatch_get_rb(struct adreno_device *adreno_dev,
  2063. struct adreno_context *drawctxt)
  2064. {
  2065. int level;
  2066. /* If preemption is disabled everybody goes on the same ringbuffer */
  2067. if (!adreno_is_preemption_enabled(adreno_dev))
  2068. return &adreno_dev->ringbuffers[0];
  2069. /*
  2070. * Math to convert the priority field in context structure to an RB ID.
  2071. * Divide up the context priority based on number of ringbuffer levels.
  2072. */
  2073. level = min_t(int, drawctxt->base.priority / adreno_dev->num_ringbuffers,
  2074. adreno_dev->num_ringbuffers - 1);
  2075. return &adreno_dev->ringbuffers[level];
  2076. }
  2077. static void adreno_dispatcher_setup_context(struct adreno_device *adreno_dev,
  2078. struct adreno_context *drawctxt)
  2079. {
  2080. drawctxt->rb = dispatch_get_rb(adreno_dev, drawctxt);
  2081. }
  2082. static void change_preemption(struct adreno_device *adreno_dev, void *priv)
  2083. {
  2084. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  2085. struct kgsl_context *context;
  2086. struct adreno_context *drawctxt;
  2087. struct adreno_ringbuffer *rb;
  2088. int id, i, ret;
  2089. /* Make sure all ringbuffers are finished */
  2090. FOR_EACH_RINGBUFFER(adreno_dev, rb, i) {
  2091. ret = adreno_ringbuffer_waittimestamp(rb, rb->timestamp,
  2092. 2 * 1000);
  2093. if (ret) {
  2094. dev_err(device->dev,
  2095. "Cannot disable preemption because couldn't idle ringbuffer[%d] ret: %d\n",
  2096. rb->id, ret);
  2097. return;
  2098. }
  2099. }
  2100. change_bit(ADRENO_DEVICE_PREEMPTION, &adreno_dev->priv);
  2101. adreno_dev->cur_rb = &adreno_dev->ringbuffers[0];
  2102. adreno_dev->next_rb = NULL;
  2103. adreno_dev->prev_rb = NULL;
  2104. /* Update the ringbuffer for each draw context */
  2105. write_lock(&device->context_lock);
  2106. idr_for_each_entry(&device->context_idr, context, id) {
  2107. drawctxt = ADRENO_CONTEXT(context);
  2108. drawctxt->rb = dispatch_get_rb(adreno_dev, drawctxt);
  2109. /*
  2110. * Make sure context destroy checks against the correct
  2111. * ringbuffer's timestamp.
  2112. */
  2113. adreno_rb_readtimestamp(adreno_dev, drawctxt->rb,
  2114. KGSL_TIMESTAMP_RETIRED, &drawctxt->internal_timestamp);
  2115. }
  2116. write_unlock(&device->context_lock);
  2117. }
  2118. static int _preemption_store(struct adreno_device *adreno_dev, bool val)
  2119. {
  2120. if (!adreno_preemption_feature_set(adreno_dev) ||
  2121. (test_bit(ADRENO_DEVICE_PREEMPTION, &adreno_dev->priv) == val))
  2122. return 0;
  2123. return adreno_power_cycle(adreno_dev, change_preemption, NULL);
  2124. }
  2125. static bool _preemption_show(struct adreno_device *adreno_dev)
  2126. {
  2127. return adreno_is_preemption_enabled(adreno_dev);
  2128. }
  2129. static unsigned int _preempt_count_show(struct adreno_device *adreno_dev)
  2130. {
  2131. return adreno_dev->preempt.count;
  2132. }
  2133. static int _ft_long_ib_detect_store(struct adreno_device *adreno_dev, bool val)
  2134. {
  2135. adreno_dev->long_ib_detect = val ? true : false;
  2136. return 0;
  2137. }
  2138. static bool _ft_long_ib_detect_show(struct adreno_device *adreno_dev)
  2139. {
  2140. return adreno_dev->long_ib_detect;
  2141. }
  2142. static ADRENO_SYSFS_BOOL(preemption);
  2143. static ADRENO_SYSFS_RO_U32(preempt_count);
  2144. static ADRENO_SYSFS_BOOL(ft_long_ib_detect);
  2145. static const struct attribute *_dispatch_attr_list[] = {
  2146. &adreno_attr_preemption.attr.attr,
  2147. &adreno_attr_preempt_count.attr.attr,
  2148. &adreno_attr_ft_long_ib_detect.attr.attr,
  2149. NULL,
  2150. };
  2151. static void adreno_dispatcher_close(struct adreno_device *adreno_dev)
  2152. {
  2153. struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
  2154. int i;
  2155. struct adreno_ringbuffer *rb;
  2156. mutex_lock(&dispatcher->mutex);
  2157. del_timer_sync(&dispatcher->timer);
  2158. adreno_dispatcher_stop_fault_timer(KGSL_DEVICE(adreno_dev));
  2159. FOR_EACH_RINGBUFFER(adreno_dev, rb, i) {
  2160. struct adreno_dispatcher_drawqueue *dispatch_q =
  2161. &(rb->dispatch_q);
  2162. while (!adreno_drawqueue_is_empty(dispatch_q)) {
  2163. kgsl_drawobj_destroy(
  2164. DRAWOBJ(dispatch_q->cmd_q[dispatch_q->head]));
  2165. dispatch_q->head = (dispatch_q->head + 1)
  2166. % ADRENO_DISPATCH_DRAWQUEUE_SIZE;
  2167. }
  2168. }
  2169. mutex_unlock(&dispatcher->mutex);
  2170. kthread_destroy_worker(dispatcher->worker);
  2171. adreno_set_dispatch_ops(adreno_dev, NULL);
  2172. kobject_put(&dispatcher->kobj);
  2173. kmem_cache_destroy(jobs_cache);
  2174. clear_bit(ADRENO_DISPATCHER_INIT, &dispatcher->priv);
  2175. }
  2176. struct dispatcher_attribute {
  2177. struct attribute attr;
  2178. ssize_t (*show)(struct adreno_dispatcher *dispatcher,
  2179. struct dispatcher_attribute *attr, char *buf);
  2180. ssize_t (*store)(struct adreno_dispatcher *dispatcher,
  2181. struct dispatcher_attribute *attr, const char *buf,
  2182. size_t count);
  2183. unsigned int max;
  2184. unsigned int *value;
  2185. };
  2186. #define DISPATCHER_UINT_ATTR(_name, _mode, _max, _value) \
  2187. struct dispatcher_attribute dispatcher_attr_##_name = { \
  2188. .attr = { .name = __stringify(_name), .mode = _mode }, \
  2189. .show = _show_uint, \
  2190. .store = _store_uint, \
  2191. .max = _max, \
  2192. .value = &(_value), \
  2193. }
  2194. #define to_dispatcher_attr(_a) \
  2195. container_of((_a), struct dispatcher_attribute, attr)
  2196. #define to_dispatcher(k) container_of(k, struct adreno_dispatcher, kobj)
  2197. static ssize_t _store_uint(struct adreno_dispatcher *dispatcher,
  2198. struct dispatcher_attribute *attr,
  2199. const char *buf, size_t size)
  2200. {
  2201. unsigned int val = 0;
  2202. int ret;
  2203. ret = kstrtou32(buf, 0, &val);
  2204. if (ret)
  2205. return ret;
  2206. if (!val || (attr->max && (val > attr->max)))
  2207. return -EINVAL;
  2208. *((unsigned int *) attr->value) = val;
  2209. return size;
  2210. }
  2211. static ssize_t _show_uint(struct adreno_dispatcher *dispatcher,
  2212. struct dispatcher_attribute *attr,
  2213. char *buf)
  2214. {
  2215. return scnprintf(buf, PAGE_SIZE, "%u\n",
  2216. *((unsigned int *) attr->value));
  2217. }
  2218. static DISPATCHER_UINT_ATTR(inflight, 0644, ADRENO_DISPATCH_DRAWQUEUE_SIZE,
  2219. _dispatcher_q_inflight_hi);
  2220. static DISPATCHER_UINT_ATTR(inflight_low_latency, 0644,
  2221. ADRENO_DISPATCH_DRAWQUEUE_SIZE, _dispatcher_q_inflight_lo);
  2222. /*
  2223. * Our code that "puts back" a command from the context is much cleaner
  2224. * if we are sure that there will always be enough room in the
  2225. * ringbuffer so restrict the maximum size of the context queue to
  2226. * ADRENO_CONTEXT_DRAWQUEUE_SIZE - 1
  2227. */
  2228. static DISPATCHER_UINT_ATTR(context_drawqueue_size, 0644,
  2229. ADRENO_CONTEXT_DRAWQUEUE_SIZE - 1, _context_drawqueue_size);
  2230. static DISPATCHER_UINT_ATTR(context_burst_count, 0644, 0,
  2231. _context_drawobj_burst);
  2232. static DISPATCHER_UINT_ATTR(drawobj_timeout, 0644, 0,
  2233. adreno_drawobj_timeout);
  2234. static DISPATCHER_UINT_ATTR(context_queue_wait, 0644, 0, _context_queue_wait);
  2235. static DISPATCHER_UINT_ATTR(fault_detect_interval, 0644, 0,
  2236. _fault_timer_interval);
  2237. static DISPATCHER_UINT_ATTR(fault_throttle_time, 0644, 0,
  2238. _fault_throttle_time);
  2239. static DISPATCHER_UINT_ATTR(fault_throttle_burst, 0644, 0,
  2240. _fault_throttle_burst);
  2241. static struct attribute *dispatcher_attrs[] = {
  2242. &dispatcher_attr_inflight.attr,
  2243. &dispatcher_attr_inflight_low_latency.attr,
  2244. &dispatcher_attr_context_drawqueue_size.attr,
  2245. &dispatcher_attr_context_burst_count.attr,
  2246. &dispatcher_attr_drawobj_timeout.attr,
  2247. &dispatcher_attr_context_queue_wait.attr,
  2248. &dispatcher_attr_fault_detect_interval.attr,
  2249. &dispatcher_attr_fault_throttle_time.attr,
  2250. &dispatcher_attr_fault_throttle_burst.attr,
  2251. NULL,
  2252. };
  2253. ATTRIBUTE_GROUPS(dispatcher);
  2254. static ssize_t dispatcher_sysfs_show(struct kobject *kobj,
  2255. struct attribute *attr, char *buf)
  2256. {
  2257. struct adreno_dispatcher *dispatcher = to_dispatcher(kobj);
  2258. struct dispatcher_attribute *pattr = to_dispatcher_attr(attr);
  2259. ssize_t ret = -EIO;
  2260. if (pattr->show)
  2261. ret = pattr->show(dispatcher, pattr, buf);
  2262. return ret;
  2263. }
  2264. static ssize_t dispatcher_sysfs_store(struct kobject *kobj,
  2265. struct attribute *attr,
  2266. const char *buf, size_t count)
  2267. {
  2268. struct adreno_dispatcher *dispatcher = to_dispatcher(kobj);
  2269. struct dispatcher_attribute *pattr = to_dispatcher_attr(attr);
  2270. ssize_t ret = -EIO;
  2271. if (pattr->store)
  2272. ret = pattr->store(dispatcher, pattr, buf, count);
  2273. return ret;
  2274. }
  2275. static const struct sysfs_ops dispatcher_sysfs_ops = {
  2276. .show = dispatcher_sysfs_show,
  2277. .store = dispatcher_sysfs_store
  2278. };
  2279. static struct kobj_type ktype_dispatcher = {
  2280. .sysfs_ops = &dispatcher_sysfs_ops,
  2281. .default_groups = dispatcher_groups,
  2282. };
  2283. static const struct adreno_dispatch_ops swsched_ops = {
  2284. .close = adreno_dispatcher_close,
  2285. .queue_cmds = adreno_dispatcher_queue_cmds,
  2286. .setup_context = adreno_dispatcher_setup_context,
  2287. .queue_context = adreno_dispatcher_queue_context,
  2288. .fault = adreno_dispatcher_fault,
  2289. .get_fault = adreno_gpu_fault,
  2290. };
  2291. /**
  2292. * adreno_dispatcher_init() - Initialize the dispatcher
  2293. * @adreno_dev: pointer to the adreno device structure
  2294. *
  2295. * Initialize the dispatcher
  2296. */
  2297. int adreno_dispatcher_init(struct adreno_device *adreno_dev)
  2298. {
  2299. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  2300. struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
  2301. int ret, i;
  2302. if (test_bit(ADRENO_DISPATCHER_INIT, &dispatcher->priv))
  2303. return 0;
  2304. ret = kobject_init_and_add(&dispatcher->kobj, &ktype_dispatcher,
  2305. &device->dev->kobj, "dispatch");
  2306. if (ret)
  2307. return ret;
  2308. dispatcher->worker = kthread_create_worker(0, "kgsl_dispatcher");
  2309. if (IS_ERR(dispatcher->worker)) {
  2310. kobject_put(&dispatcher->kobj);
  2311. return PTR_ERR(dispatcher->worker);
  2312. }
  2313. WARN_ON(sysfs_create_files(&device->dev->kobj, _dispatch_attr_list));
  2314. mutex_init(&dispatcher->mutex);
  2315. timer_setup(&dispatcher->timer, adreno_dispatcher_timer, 0);
  2316. kthread_init_work(&dispatcher->work, adreno_dispatcher_work);
  2317. init_completion(&dispatcher->idle_gate);
  2318. complete_all(&dispatcher->idle_gate);
  2319. jobs_cache = KMEM_CACHE(adreno_dispatch_job, 0);
  2320. for (i = 0; i < ARRAY_SIZE(dispatcher->jobs); i++) {
  2321. init_llist_head(&dispatcher->jobs[i]);
  2322. init_llist_head(&dispatcher->requeue[i]);
  2323. }
  2324. adreno_set_dispatch_ops(adreno_dev, &swsched_ops);
  2325. sched_set_fifo(dispatcher->worker->task);
  2326. set_bit(ADRENO_DISPATCHER_INIT, &dispatcher->priv);
  2327. return 0;
  2328. }
  2329. /*
  2330. * adreno_dispatcher_idle() - Wait for dispatcher to idle
  2331. * @adreno_dev: Adreno device whose dispatcher needs to idle
  2332. *
  2333. * Signal dispatcher to stop sending more commands and complete
  2334. * the commands that have already been submitted. This function
  2335. * should not be called when dispatcher mutex is held.
  2336. * The caller must hold the device mutex.
  2337. */
  2338. int adreno_dispatcher_idle(struct adreno_device *adreno_dev)
  2339. {
  2340. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  2341. struct adreno_dispatcher *dispatcher = &adreno_dev->dispatcher;
  2342. int ret;
  2343. if (device->state != KGSL_STATE_ACTIVE)
  2344. return 0;
  2345. /*
  2346. * Ensure that this function is not called when dispatcher
  2347. * mutex is held and device is started
  2348. */
  2349. if (WARN_ON(mutex_is_locked(&dispatcher->mutex)))
  2350. return -EDEADLK;
  2351. adreno_get_gpu_halt(adreno_dev);
  2352. mutex_unlock(&device->mutex);
  2353. /*
  2354. * Flush the worker to make sure all executing
  2355. * or pending dispatcher works on worker are
  2356. * finished
  2357. */
  2358. kthread_flush_worker(dispatcher->worker);
  2359. ret = wait_for_completion_timeout(&dispatcher->idle_gate,
  2360. msecs_to_jiffies(ADRENO_IDLE_TIMEOUT));
  2361. if (ret == 0) {
  2362. ret = -ETIMEDOUT;
  2363. WARN(1, "Dispatcher halt timeout\n");
  2364. } else if (ret < 0) {
  2365. dev_err(device->dev, "Dispatcher halt failed %d\n", ret);
  2366. } else {
  2367. ret = 0;
  2368. }
  2369. mutex_lock(&device->mutex);
  2370. adreno_put_gpu_halt(adreno_dev);
  2371. /*
  2372. * requeue dispatcher work to resubmit pending commands
  2373. * that may have been blocked due to this idling request
  2374. */
  2375. adreno_dispatcher_schedule(device);
  2376. return ret;
  2377. }