panthor_sched.c 118 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187
  1. // SPDX-License-Identifier: GPL-2.0 or MIT
  2. /* Copyright 2023 Collabora ltd. */
  3. #include <drm/drm_drv.h>
  4. #include <drm/drm_exec.h>
  5. #include <drm/drm_gem_shmem_helper.h>
  6. #include <drm/drm_managed.h>
  7. #include <drm/drm_print.h>
  8. #include <drm/gpu_scheduler.h>
  9. #include <drm/panthor_drm.h>
  10. #include <linux/build_bug.h>
  11. #include <linux/cleanup.h>
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/dma-resv.h>
  16. #include <linux/firmware.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/iopoll.h>
  20. #include <linux/iosys-map.h>
  21. #include <linux/module.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/rcupdate.h>
  25. #include "panthor_devfreq.h"
  26. #include "panthor_device.h"
  27. #include "panthor_fw.h"
  28. #include "panthor_gem.h"
  29. #include "panthor_gpu.h"
  30. #include "panthor_heap.h"
  31. #include "panthor_mmu.h"
  32. #include "panthor_regs.h"
  33. #include "panthor_sched.h"
  34. /**
  35. * DOC: Scheduler
  36. *
  37. * Mali CSF hardware adopts a firmware-assisted scheduling model, where
  38. * the firmware takes care of scheduling aspects, to some extent.
  39. *
  40. * The scheduling happens at the scheduling group level, each group
  41. * contains 1 to N queues (N is FW/hardware dependent, and exposed
  42. * through the firmware interface). Each queue is assigned a command
  43. * stream ring buffer, which serves as a way to get jobs submitted to
  44. * the GPU, among other things.
  45. *
  46. * The firmware can schedule a maximum of M groups (M is FW/hardware
  47. * dependent, and exposed through the firmware interface). Passed
  48. * this maximum number of groups, the kernel must take care of
  49. * rotating the groups passed to the firmware so every group gets
  50. * a chance to have his queues scheduled for execution.
  51. *
  52. * The current implementation only supports with kernel-mode queues.
  53. * In other terms, userspace doesn't have access to the ring-buffer.
  54. * Instead, userspace passes indirect command stream buffers that are
  55. * called from the queue ring-buffer by the kernel using a pre-defined
  56. * sequence of command stream instructions to ensure the userspace driver
  57. * always gets consistent results (cache maintenance,
  58. * synchronization, ...).
  59. *
  60. * We rely on the drm_gpu_scheduler framework to deal with job
  61. * dependencies and submission. As any other driver dealing with a
  62. * FW-scheduler, we use the 1:1 entity:scheduler mode, such that each
  63. * entity has its own job scheduler. When a job is ready to be executed
  64. * (all its dependencies are met), it is pushed to the appropriate
  65. * queue ring-buffer, and the group is scheduled for execution if it
  66. * wasn't already active.
  67. *
  68. * Kernel-side group scheduling is timeslice-based. When we have less
  69. * groups than there are slots, the periodic tick is disabled and we
  70. * just let the FW schedule the active groups. When there are more
  71. * groups than slots, we let each group a chance to execute stuff for
  72. * a given amount of time, and then re-evaluate and pick new groups
  73. * to schedule. The group selection algorithm is based on
  74. * priority+round-robin.
  75. *
  76. * Even though user-mode queues is out of the scope right now, the
  77. * current design takes them into account by avoiding any guess on the
  78. * group/queue state that would be based on information we wouldn't have
  79. * if userspace was in charge of the ring-buffer. That's also one of the
  80. * reason we don't do 'cooperative' scheduling (encoding FW group slot
  81. * reservation as dma_fence that would be returned from the
  82. * drm_gpu_scheduler::prepare_job() hook, and treating group rotation as
  83. * a queue of waiters, ordered by job submission order). This approach
  84. * would work for kernel-mode queues, but would make user-mode queues a
  85. * lot more complicated to retrofit.
  86. */
  87. #define JOB_TIMEOUT_MS 5000
  88. #define MAX_CSG_PRIO 0xf
  89. #define NUM_INSTRS_PER_CACHE_LINE (64 / sizeof(u64))
  90. #define MAX_INSTRS_PER_JOB 24
  91. struct panthor_group;
  92. /**
  93. * struct panthor_csg_slot - Command stream group slot
  94. *
  95. * This represents a FW slot for a scheduling group.
  96. */
  97. struct panthor_csg_slot {
  98. /** @group: Scheduling group bound to this slot. */
  99. struct panthor_group *group;
  100. /** @priority: Group priority. */
  101. u8 priority;
  102. };
  103. /**
  104. * enum panthor_csg_priority - Group priority
  105. */
  106. enum panthor_csg_priority {
  107. /** @PANTHOR_CSG_PRIORITY_LOW: Low priority group. */
  108. PANTHOR_CSG_PRIORITY_LOW = 0,
  109. /** @PANTHOR_CSG_PRIORITY_MEDIUM: Medium priority group. */
  110. PANTHOR_CSG_PRIORITY_MEDIUM,
  111. /** @PANTHOR_CSG_PRIORITY_HIGH: High priority group. */
  112. PANTHOR_CSG_PRIORITY_HIGH,
  113. /**
  114. * @PANTHOR_CSG_PRIORITY_RT: Real-time priority group.
  115. *
  116. * Real-time priority allows one to preempt scheduling of other
  117. * non-real-time groups. When such a group becomes executable,
  118. * it will evict the group with the lowest non-rt priority if
  119. * there's no free group slot available.
  120. */
  121. PANTHOR_CSG_PRIORITY_RT,
  122. /** @PANTHOR_CSG_PRIORITY_COUNT: Number of priority levels. */
  123. PANTHOR_CSG_PRIORITY_COUNT,
  124. };
  125. /**
  126. * struct panthor_scheduler - Object used to manage the scheduler
  127. */
  128. struct panthor_scheduler {
  129. /** @ptdev: Device. */
  130. struct panthor_device *ptdev;
  131. /**
  132. * @wq: Workqueue used by our internal scheduler logic and
  133. * drm_gpu_scheduler.
  134. *
  135. * Used for the scheduler tick, group update or other kind of FW
  136. * event processing that can't be handled in the threaded interrupt
  137. * path. Also passed to the drm_gpu_scheduler instances embedded
  138. * in panthor_queue.
  139. */
  140. struct workqueue_struct *wq;
  141. /**
  142. * @heap_alloc_wq: Workqueue used to schedule tiler_oom works.
  143. *
  144. * We have a queue dedicated to heap chunk allocation works to avoid
  145. * blocking the rest of the scheduler if the allocation tries to
  146. * reclaim memory.
  147. */
  148. struct workqueue_struct *heap_alloc_wq;
  149. /** @tick_work: Work executed on a scheduling tick. */
  150. struct delayed_work tick_work;
  151. /**
  152. * @sync_upd_work: Work used to process synchronization object updates.
  153. *
  154. * We use this work to unblock queues/groups that were waiting on a
  155. * synchronization object.
  156. */
  157. struct work_struct sync_upd_work;
  158. /**
  159. * @fw_events_work: Work used to process FW events outside the interrupt path.
  160. *
  161. * Even if the interrupt is threaded, we need any event processing
  162. * that require taking the panthor_scheduler::lock to be processed
  163. * outside the interrupt path so we don't block the tick logic when
  164. * it calls panthor_fw_{csg,wait}_wait_acks(). Since most of the
  165. * event processing requires taking this lock, we just delegate all
  166. * FW event processing to the scheduler workqueue.
  167. */
  168. struct work_struct fw_events_work;
  169. /**
  170. * @fw_events: Bitmask encoding pending FW events.
  171. */
  172. atomic_t fw_events;
  173. /**
  174. * @resched_target: When the next tick should occur.
  175. *
  176. * Expressed in jiffies.
  177. */
  178. u64 resched_target;
  179. /**
  180. * @last_tick: When the last tick occurred.
  181. *
  182. * Expressed in jiffies.
  183. */
  184. u64 last_tick;
  185. /** @tick_period: Tick period in jiffies. */
  186. u64 tick_period;
  187. /**
  188. * @lock: Lock protecting access to all the scheduler fields.
  189. *
  190. * Should be taken in the tick work, the irq handler, and anywhere the @groups
  191. * fields are touched.
  192. */
  193. struct mutex lock;
  194. /** @groups: Various lists used to classify groups. */
  195. struct {
  196. /**
  197. * @runnable: Runnable group lists.
  198. *
  199. * When a group has queues that want to execute something,
  200. * its panthor_group::run_node should be inserted here.
  201. *
  202. * One list per-priority.
  203. */
  204. struct list_head runnable[PANTHOR_CSG_PRIORITY_COUNT];
  205. /**
  206. * @idle: Idle group lists.
  207. *
  208. * When all queues of a group are idle (either because they
  209. * have nothing to execute, or because they are blocked), the
  210. * panthor_group::run_node field should be inserted here.
  211. *
  212. * One list per-priority.
  213. */
  214. struct list_head idle[PANTHOR_CSG_PRIORITY_COUNT];
  215. /**
  216. * @waiting: List of groups whose queues are blocked on a
  217. * synchronization object.
  218. *
  219. * Insert panthor_group::wait_node here when a group is waiting
  220. * for synchronization objects to be signaled.
  221. *
  222. * This list is evaluated in the @sync_upd_work work.
  223. */
  224. struct list_head waiting;
  225. } groups;
  226. /**
  227. * @csg_slots: FW command stream group slots.
  228. */
  229. struct panthor_csg_slot csg_slots[MAX_CSGS];
  230. /** @csg_slot_count: Number of command stream group slots exposed by the FW. */
  231. u32 csg_slot_count;
  232. /** @cs_slot_count: Number of command stream slot per group slot exposed by the FW. */
  233. u32 cs_slot_count;
  234. /** @as_slot_count: Number of address space slots supported by the MMU. */
  235. u32 as_slot_count;
  236. /** @used_csg_slot_count: Number of command stream group slot currently used. */
  237. u32 used_csg_slot_count;
  238. /** @sb_slot_count: Number of scoreboard slots. */
  239. u32 sb_slot_count;
  240. /**
  241. * @might_have_idle_groups: True if an active group might have become idle.
  242. *
  243. * This will force a tick, so other runnable groups can be scheduled if one
  244. * or more active groups became idle.
  245. */
  246. bool might_have_idle_groups;
  247. /** @pm: Power management related fields. */
  248. struct {
  249. /** @has_ref: True if the scheduler owns a runtime PM reference. */
  250. bool has_ref;
  251. } pm;
  252. /** @reset: Reset related fields. */
  253. struct {
  254. /** @lock: Lock protecting the other reset fields. */
  255. struct mutex lock;
  256. /**
  257. * @in_progress: True if a reset is in progress.
  258. *
  259. * Set to true in panthor_sched_pre_reset() and back to false in
  260. * panthor_sched_post_reset().
  261. */
  262. atomic_t in_progress;
  263. /**
  264. * @stopped_groups: List containing all groups that were stopped
  265. * before a reset.
  266. *
  267. * Insert panthor_group::run_node in the pre_reset path.
  268. */
  269. struct list_head stopped_groups;
  270. } reset;
  271. };
  272. /**
  273. * struct panthor_syncobj_32b - 32-bit FW synchronization object
  274. */
  275. struct panthor_syncobj_32b {
  276. /** @seqno: Sequence number. */
  277. u32 seqno;
  278. /**
  279. * @status: Status.
  280. *
  281. * Not zero on failure.
  282. */
  283. u32 status;
  284. };
  285. /**
  286. * struct panthor_syncobj_64b - 64-bit FW synchronization object
  287. */
  288. struct panthor_syncobj_64b {
  289. /** @seqno: Sequence number. */
  290. u64 seqno;
  291. /**
  292. * @status: Status.
  293. *
  294. * Not zero on failure.
  295. */
  296. u32 status;
  297. /** @pad: MBZ. */
  298. u32 pad;
  299. };
  300. /**
  301. * struct panthor_queue - Execution queue
  302. */
  303. struct panthor_queue {
  304. /** @scheduler: DRM scheduler used for this queue. */
  305. struct drm_gpu_scheduler scheduler;
  306. /** @entity: DRM scheduling entity used for this queue. */
  307. struct drm_sched_entity entity;
  308. /** @name: DRM scheduler name for this queue. */
  309. char *name;
  310. /** @timeout: Queue timeout related fields. */
  311. struct {
  312. /** @timeout.work: Work executed when a queue timeout occurs. */
  313. struct delayed_work work;
  314. /**
  315. * @timeout.remaining: Time remaining before a queue timeout.
  316. *
  317. * When the timer is running, this value is set to MAX_SCHEDULE_TIMEOUT.
  318. * When the timer is suspended, it's set to the time remaining when the
  319. * timer was suspended.
  320. */
  321. unsigned long remaining;
  322. } timeout;
  323. /**
  324. * @doorbell_id: Doorbell assigned to this queue.
  325. *
  326. * Right now, all groups share the same doorbell, and the doorbell ID
  327. * is assigned to group_slot + 1 when the group is assigned a slot. But
  328. * we might decide to provide fine grained doorbell assignment at some
  329. * point, so don't have to wake up all queues in a group every time one
  330. * of them is updated.
  331. */
  332. u8 doorbell_id;
  333. /**
  334. * @priority: Priority of the queue inside the group.
  335. *
  336. * Must be less than 16 (Only 4 bits available).
  337. */
  338. u8 priority;
  339. #define CSF_MAX_QUEUE_PRIO GENMASK(3, 0)
  340. /** @ringbuf: Command stream ring-buffer. */
  341. struct panthor_kernel_bo *ringbuf;
  342. /** @iface: Firmware interface. */
  343. struct {
  344. /** @mem: FW memory allocated for this interface. */
  345. struct panthor_kernel_bo *mem;
  346. /** @input: Input interface. */
  347. struct panthor_fw_ringbuf_input_iface *input;
  348. /** @output: Output interface. */
  349. const struct panthor_fw_ringbuf_output_iface *output;
  350. /** @input_fw_va: FW virtual address of the input interface buffer. */
  351. u32 input_fw_va;
  352. /** @output_fw_va: FW virtual address of the output interface buffer. */
  353. u32 output_fw_va;
  354. } iface;
  355. /**
  356. * @syncwait: Stores information about the synchronization object this
  357. * queue is waiting on.
  358. */
  359. struct {
  360. /** @gpu_va: GPU address of the synchronization object. */
  361. u64 gpu_va;
  362. /** @ref: Reference value to compare against. */
  363. u64 ref;
  364. /** @gt: True if this is a greater-than test. */
  365. bool gt;
  366. /** @sync64: True if this is a 64-bit sync object. */
  367. bool sync64;
  368. /** @bo: Buffer object holding the synchronization object. */
  369. struct drm_gem_object *obj;
  370. /** @offset: Offset of the synchronization object inside @bo. */
  371. u64 offset;
  372. /**
  373. * @kmap: Kernel mapping of the buffer object holding the
  374. * synchronization object.
  375. */
  376. void *kmap;
  377. } syncwait;
  378. /** @fence_ctx: Fence context fields. */
  379. struct {
  380. /** @lock: Used to protect access to all fences allocated by this context. */
  381. spinlock_t lock;
  382. /**
  383. * @id: Fence context ID.
  384. *
  385. * Allocated with dma_fence_context_alloc().
  386. */
  387. u64 id;
  388. /** @seqno: Sequence number of the last initialized fence. */
  389. atomic64_t seqno;
  390. /**
  391. * @last_fence: Fence of the last submitted job.
  392. *
  393. * We return this fence when we get an empty command stream.
  394. * This way, we are guaranteed that all earlier jobs have completed
  395. * when drm_sched_job::s_fence::finished without having to feed
  396. * the CS ring buffer with a dummy job that only signals the fence.
  397. */
  398. struct dma_fence *last_fence;
  399. /**
  400. * @in_flight_jobs: List containing all in-flight jobs.
  401. *
  402. * Used to keep track and signal panthor_job::done_fence when the
  403. * synchronization object attached to the queue is signaled.
  404. */
  405. struct list_head in_flight_jobs;
  406. } fence_ctx;
  407. /** @profiling: Job profiling data slots and access information. */
  408. struct {
  409. /** @slots: Kernel BO holding the slots. */
  410. struct panthor_kernel_bo *slots;
  411. /** @slot_count: Number of jobs ringbuffer can hold at once. */
  412. u32 slot_count;
  413. /** @seqno: Index of the next available profiling information slot. */
  414. u32 seqno;
  415. } profiling;
  416. };
  417. /**
  418. * enum panthor_group_state - Scheduling group state.
  419. */
  420. enum panthor_group_state {
  421. /** @PANTHOR_CS_GROUP_CREATED: Group was created, but not scheduled yet. */
  422. PANTHOR_CS_GROUP_CREATED,
  423. /** @PANTHOR_CS_GROUP_ACTIVE: Group is currently scheduled. */
  424. PANTHOR_CS_GROUP_ACTIVE,
  425. /**
  426. * @PANTHOR_CS_GROUP_SUSPENDED: Group was scheduled at least once, but is
  427. * inactive/suspended right now.
  428. */
  429. PANTHOR_CS_GROUP_SUSPENDED,
  430. /**
  431. * @PANTHOR_CS_GROUP_TERMINATED: Group was terminated.
  432. *
  433. * Can no longer be scheduled. The only allowed action is a destruction.
  434. */
  435. PANTHOR_CS_GROUP_TERMINATED,
  436. /**
  437. * @PANTHOR_CS_GROUP_UNKNOWN_STATE: Group is an unknown state.
  438. *
  439. * The FW returned an inconsistent state. The group is flagged unusable
  440. * and can no longer be scheduled. The only allowed action is a
  441. * destruction.
  442. *
  443. * When that happens, we also schedule a FW reset, to start from a fresh
  444. * state.
  445. */
  446. PANTHOR_CS_GROUP_UNKNOWN_STATE,
  447. };
  448. /**
  449. * struct panthor_group - Scheduling group object
  450. */
  451. struct panthor_group {
  452. /** @refcount: Reference count */
  453. struct kref refcount;
  454. /** @ptdev: Device. */
  455. struct panthor_device *ptdev;
  456. /** @vm: VM bound to the group. */
  457. struct panthor_vm *vm;
  458. /** @compute_core_mask: Mask of shader cores that can be used for compute jobs. */
  459. u64 compute_core_mask;
  460. /** @fragment_core_mask: Mask of shader cores that can be used for fragment jobs. */
  461. u64 fragment_core_mask;
  462. /** @tiler_core_mask: Mask of tiler cores that can be used for tiler jobs. */
  463. u64 tiler_core_mask;
  464. /** @max_compute_cores: Maximum number of shader cores used for compute jobs. */
  465. u8 max_compute_cores;
  466. /** @max_fragment_cores: Maximum number of shader cores used for fragment jobs. */
  467. u8 max_fragment_cores;
  468. /** @max_tiler_cores: Maximum number of tiler cores used for tiler jobs. */
  469. u8 max_tiler_cores;
  470. /** @priority: Group priority (check panthor_csg_priority). */
  471. u8 priority;
  472. /** @blocked_queues: Bitmask reflecting the blocked queues. */
  473. u32 blocked_queues;
  474. /** @idle_queues: Bitmask reflecting the idle queues. */
  475. u32 idle_queues;
  476. /** @fatal_lock: Lock used to protect access to fatal fields. */
  477. spinlock_t fatal_lock;
  478. /** @fatal_queues: Bitmask reflecting the queues that hit a fatal exception. */
  479. u32 fatal_queues;
  480. /** @tiler_oom: Mask of queues that have a tiler OOM event to process. */
  481. atomic_t tiler_oom;
  482. /** @queue_count: Number of queues in this group. */
  483. u32 queue_count;
  484. /** @queues: Queues owned by this group. */
  485. struct panthor_queue *queues[MAX_CS_PER_CSG];
  486. /**
  487. * @csg_id: ID of the FW group slot.
  488. *
  489. * -1 when the group is not scheduled/active.
  490. */
  491. int csg_id;
  492. /**
  493. * @destroyed: True when the group has been destroyed.
  494. *
  495. * If a group is destroyed it becomes useless: no further jobs can be submitted
  496. * to its queues. We simply wait for all references to be dropped so we can
  497. * release the group object.
  498. */
  499. bool destroyed;
  500. /**
  501. * @timedout: True when a timeout occurred on any of the queues owned by
  502. * this group.
  503. *
  504. * Timeouts can be reported by drm_sched or by the FW. If a reset is required,
  505. * and the group can't be suspended, this also leads to a timeout. In any case,
  506. * any timeout situation is unrecoverable, and the group becomes useless. We
  507. * simply wait for all references to be dropped so we can release the group
  508. * object.
  509. */
  510. bool timedout;
  511. /**
  512. * @innocent: True when the group becomes unusable because the group suspension
  513. * failed during a reset.
  514. *
  515. * Sometimes the FW was put in a bad state by other groups, causing the group
  516. * suspension happening in the reset path to fail. In that case, we consider the
  517. * group innocent.
  518. */
  519. bool innocent;
  520. /**
  521. * @syncobjs: Pool of per-queue synchronization objects.
  522. *
  523. * One sync object per queue. The position of the sync object is
  524. * determined by the queue index.
  525. */
  526. struct panthor_kernel_bo *syncobjs;
  527. /** @fdinfo: Per-file info exposed through /proc/<process>/fdinfo */
  528. struct {
  529. /** @data: Total sampled values for jobs in queues from this group. */
  530. struct panthor_gpu_usage data;
  531. /**
  532. * @fdinfo.lock: Spinlock to govern concurrent access from drm file's fdinfo
  533. * callback and job post-completion processing function
  534. */
  535. spinlock_t lock;
  536. /** @fdinfo.kbo_sizes: Aggregate size of private kernel BO's held by the group. */
  537. size_t kbo_sizes;
  538. } fdinfo;
  539. /** @task_info: Info of current->group_leader that created the group. */
  540. struct {
  541. /** @task_info.pid: pid of current->group_leader */
  542. pid_t pid;
  543. /** @task_info.comm: comm of current->group_leader */
  544. char comm[TASK_COMM_LEN];
  545. } task_info;
  546. /** @state: Group state. */
  547. enum panthor_group_state state;
  548. /**
  549. * @suspend_buf: Suspend buffer.
  550. *
  551. * Stores the state of the group and its queues when a group is suspended.
  552. * Used at resume time to restore the group in its previous state.
  553. *
  554. * The size of the suspend buffer is exposed through the FW interface.
  555. */
  556. struct panthor_kernel_bo *suspend_buf;
  557. /**
  558. * @protm_suspend_buf: Protection mode suspend buffer.
  559. *
  560. * Stores the state of the group and its queues when a group that's in
  561. * protection mode is suspended.
  562. *
  563. * Used at resume time to restore the group in its previous state.
  564. *
  565. * The size of the protection mode suspend buffer is exposed through the
  566. * FW interface.
  567. */
  568. struct panthor_kernel_bo *protm_suspend_buf;
  569. /** @sync_upd_work: Work used to check/signal job fences. */
  570. struct work_struct sync_upd_work;
  571. /** @tiler_oom_work: Work used to process tiler OOM events happening on this group. */
  572. struct work_struct tiler_oom_work;
  573. /** @term_work: Work used to finish the group termination procedure. */
  574. struct work_struct term_work;
  575. /**
  576. * @release_work: Work used to release group resources.
  577. *
  578. * We need to postpone the group release to avoid a deadlock when
  579. * the last ref is released in the tick work.
  580. */
  581. struct work_struct release_work;
  582. /**
  583. * @run_node: Node used to insert the group in the
  584. * panthor_group::groups::{runnable,idle} and
  585. * panthor_group::reset.stopped_groups lists.
  586. */
  587. struct list_head run_node;
  588. /**
  589. * @wait_node: Node used to insert the group in the
  590. * panthor_group::groups::waiting list.
  591. */
  592. struct list_head wait_node;
  593. };
  594. struct panthor_job_profiling_data {
  595. struct {
  596. u64 before;
  597. u64 after;
  598. } cycles;
  599. struct {
  600. u64 before;
  601. u64 after;
  602. } time;
  603. };
  604. /**
  605. * group_queue_work() - Queue a group work
  606. * @group: Group to queue the work for.
  607. * @wname: Work name.
  608. *
  609. * Grabs a ref and queue a work item to the scheduler workqueue. If
  610. * the work was already queued, we release the reference we grabbed.
  611. *
  612. * Work callbacks must release the reference we grabbed here.
  613. */
  614. #define group_queue_work(group, wname) \
  615. do { \
  616. group_get(group); \
  617. if (!queue_work((group)->ptdev->scheduler->wq, &(group)->wname ## _work)) \
  618. group_put(group); \
  619. } while (0)
  620. /**
  621. * sched_queue_work() - Queue a scheduler work.
  622. * @sched: Scheduler object.
  623. * @wname: Work name.
  624. *
  625. * Conditionally queues a scheduler work if no reset is pending/in-progress.
  626. */
  627. #define sched_queue_work(sched, wname) \
  628. do { \
  629. if (!atomic_read(&(sched)->reset.in_progress) && \
  630. !panthor_device_reset_is_pending((sched)->ptdev)) \
  631. queue_work((sched)->wq, &(sched)->wname ## _work); \
  632. } while (0)
  633. /**
  634. * sched_queue_delayed_work() - Queue a scheduler delayed work.
  635. * @sched: Scheduler object.
  636. * @wname: Work name.
  637. * @delay: Work delay in jiffies.
  638. *
  639. * Conditionally queues a scheduler delayed work if no reset is
  640. * pending/in-progress.
  641. */
  642. #define sched_queue_delayed_work(sched, wname, delay) \
  643. do { \
  644. if (!atomic_read(&sched->reset.in_progress) && \
  645. !panthor_device_reset_is_pending((sched)->ptdev)) \
  646. mod_delayed_work((sched)->wq, &(sched)->wname ## _work, delay); \
  647. } while (0)
  648. /*
  649. * We currently set the maximum of groups per file to an arbitrary low value.
  650. * But this can be updated if we need more.
  651. */
  652. #define MAX_GROUPS_PER_POOL 128
  653. /*
  654. * Mark added on an entry of group pool Xarray to identify if the group has
  655. * been fully initialized and can be accessed elsewhere in the driver code.
  656. */
  657. #define GROUP_REGISTERED XA_MARK_1
  658. /**
  659. * struct panthor_group_pool - Group pool
  660. *
  661. * Each file get assigned a group pool.
  662. */
  663. struct panthor_group_pool {
  664. /** @xa: Xarray used to manage group handles. */
  665. struct xarray xa;
  666. };
  667. /**
  668. * struct panthor_job - Used to manage GPU job
  669. */
  670. struct panthor_job {
  671. /** @base: Inherit from drm_sched_job. */
  672. struct drm_sched_job base;
  673. /** @refcount: Reference count. */
  674. struct kref refcount;
  675. /** @group: Group of the queue this job will be pushed to. */
  676. struct panthor_group *group;
  677. /** @queue_idx: Index of the queue inside @group. */
  678. u32 queue_idx;
  679. /** @call_info: Information about the userspace command stream call. */
  680. struct {
  681. /** @start: GPU address of the userspace command stream. */
  682. u64 start;
  683. /** @size: Size of the userspace command stream. */
  684. u32 size;
  685. /**
  686. * @latest_flush: Flush ID at the time the userspace command
  687. * stream was built.
  688. *
  689. * Needed for the flush reduction mechanism.
  690. */
  691. u32 latest_flush;
  692. } call_info;
  693. /** @ringbuf: Position of this job is in the ring buffer. */
  694. struct {
  695. /** @start: Start offset. */
  696. u64 start;
  697. /** @end: End offset. */
  698. u64 end;
  699. } ringbuf;
  700. /**
  701. * @node: Used to insert the job in the panthor_queue::fence_ctx::in_flight_jobs
  702. * list.
  703. */
  704. struct list_head node;
  705. /** @done_fence: Fence signaled when the job is finished or cancelled. */
  706. struct dma_fence *done_fence;
  707. /** @profiling: Job profiling information. */
  708. struct {
  709. /** @mask: Current device job profiling enablement bitmask. */
  710. u32 mask;
  711. /** @slot: Job index in the profiling slots BO. */
  712. u32 slot;
  713. } profiling;
  714. };
  715. static void
  716. panthor_queue_put_syncwait_obj(struct panthor_queue *queue)
  717. {
  718. if (queue->syncwait.kmap) {
  719. struct iosys_map map = IOSYS_MAP_INIT_VADDR(queue->syncwait.kmap);
  720. drm_gem_vunmap(queue->syncwait.obj, &map);
  721. queue->syncwait.kmap = NULL;
  722. }
  723. drm_gem_object_put(queue->syncwait.obj);
  724. queue->syncwait.obj = NULL;
  725. }
  726. static void *
  727. panthor_queue_get_syncwait_obj(struct panthor_group *group, struct panthor_queue *queue)
  728. {
  729. struct panthor_device *ptdev = group->ptdev;
  730. struct panthor_gem_object *bo;
  731. struct iosys_map map;
  732. int ret;
  733. if (queue->syncwait.kmap) {
  734. bo = container_of(queue->syncwait.obj,
  735. struct panthor_gem_object, base.base);
  736. goto out_sync;
  737. }
  738. bo = panthor_vm_get_bo_for_va(group->vm,
  739. queue->syncwait.gpu_va,
  740. &queue->syncwait.offset);
  741. if (drm_WARN_ON(&ptdev->base, IS_ERR_OR_NULL(bo)))
  742. goto err_put_syncwait_obj;
  743. queue->syncwait.obj = &bo->base.base;
  744. ret = drm_gem_vmap(queue->syncwait.obj, &map);
  745. if (drm_WARN_ON(&ptdev->base, ret))
  746. goto err_put_syncwait_obj;
  747. queue->syncwait.kmap = map.vaddr;
  748. if (drm_WARN_ON(&ptdev->base, !queue->syncwait.kmap))
  749. goto err_put_syncwait_obj;
  750. out_sync:
  751. /* Make sure the CPU caches are invalidated before the seqno is read.
  752. * panthor_gem_sync() is a NOP if map_wc=true, so no need to check
  753. * it here.
  754. */
  755. panthor_gem_sync(&bo->base.base,
  756. DRM_PANTHOR_BO_SYNC_CPU_CACHE_FLUSH_AND_INVALIDATE,
  757. queue->syncwait.offset,
  758. queue->syncwait.sync64 ?
  759. sizeof(struct panthor_syncobj_64b) :
  760. sizeof(struct panthor_syncobj_32b));
  761. return queue->syncwait.kmap + queue->syncwait.offset;
  762. err_put_syncwait_obj:
  763. panthor_queue_put_syncwait_obj(queue);
  764. return NULL;
  765. }
  766. static void group_free_queue(struct panthor_group *group, struct panthor_queue *queue)
  767. {
  768. if (IS_ERR_OR_NULL(queue))
  769. return;
  770. /* Disable the timeout before tearing down drm_sched components. */
  771. disable_delayed_work_sync(&queue->timeout.work);
  772. if (queue->entity.fence_context)
  773. drm_sched_entity_destroy(&queue->entity);
  774. if (queue->scheduler.ops)
  775. drm_sched_fini(&queue->scheduler);
  776. kfree(queue->name);
  777. panthor_queue_put_syncwait_obj(queue);
  778. panthor_kernel_bo_destroy(queue->ringbuf);
  779. panthor_kernel_bo_destroy(queue->iface.mem);
  780. panthor_kernel_bo_destroy(queue->profiling.slots);
  781. /* Release the last_fence we were holding, if any. */
  782. dma_fence_put(queue->fence_ctx.last_fence);
  783. kfree(queue);
  784. }
  785. static void group_release_work(struct work_struct *work)
  786. {
  787. struct panthor_group *group = container_of(work,
  788. struct panthor_group,
  789. release_work);
  790. u32 i;
  791. /* dma-fences may still be accessing group->queues under rcu lock. */
  792. synchronize_rcu();
  793. for (i = 0; i < group->queue_count; i++)
  794. group_free_queue(group, group->queues[i]);
  795. panthor_kernel_bo_destroy(group->suspend_buf);
  796. panthor_kernel_bo_destroy(group->protm_suspend_buf);
  797. panthor_kernel_bo_destroy(group->syncobjs);
  798. panthor_vm_put(group->vm);
  799. kfree(group);
  800. }
  801. static void group_release(struct kref *kref)
  802. {
  803. struct panthor_group *group = container_of(kref,
  804. struct panthor_group,
  805. refcount);
  806. struct panthor_device *ptdev = group->ptdev;
  807. drm_WARN_ON(&ptdev->base, group->csg_id >= 0);
  808. drm_WARN_ON(&ptdev->base, !list_empty(&group->run_node));
  809. drm_WARN_ON(&ptdev->base, !list_empty(&group->wait_node));
  810. queue_work(panthor_cleanup_wq, &group->release_work);
  811. }
  812. static void group_put(struct panthor_group *group)
  813. {
  814. if (group)
  815. kref_put(&group->refcount, group_release);
  816. }
  817. static struct panthor_group *
  818. group_get(struct panthor_group *group)
  819. {
  820. if (group)
  821. kref_get(&group->refcount);
  822. return group;
  823. }
  824. /**
  825. * group_bind_locked() - Bind a group to a group slot
  826. * @group: Group.
  827. * @csg_id: Slot.
  828. *
  829. * Return: 0 on success, a negative error code otherwise.
  830. */
  831. static int
  832. group_bind_locked(struct panthor_group *group, u32 csg_id)
  833. {
  834. struct panthor_device *ptdev = group->ptdev;
  835. struct panthor_csg_slot *csg_slot;
  836. int ret;
  837. lockdep_assert_held(&ptdev->scheduler->lock);
  838. if (drm_WARN_ON(&ptdev->base, group->csg_id != -1 || csg_id >= MAX_CSGS ||
  839. ptdev->scheduler->csg_slots[csg_id].group))
  840. return -EINVAL;
  841. ret = panthor_vm_active(group->vm);
  842. if (ret)
  843. return ret;
  844. csg_slot = &ptdev->scheduler->csg_slots[csg_id];
  845. group_get(group);
  846. group->csg_id = csg_id;
  847. /* Dummy doorbell allocation: doorbell is assigned to the group and
  848. * all queues use the same doorbell.
  849. *
  850. * TODO: Implement LRU-based doorbell assignment, so the most often
  851. * updated queues get their own doorbell, thus avoiding useless checks
  852. * on queues belonging to the same group that are rarely updated.
  853. */
  854. for (u32 i = 0; i < group->queue_count; i++)
  855. group->queues[i]->doorbell_id = csg_id + 1;
  856. csg_slot->group = group;
  857. return 0;
  858. }
  859. /**
  860. * group_unbind_locked() - Unbind a group from a slot.
  861. * @group: Group to unbind.
  862. *
  863. * Return: 0 on success, a negative error code otherwise.
  864. */
  865. static int
  866. group_unbind_locked(struct panthor_group *group)
  867. {
  868. struct panthor_device *ptdev = group->ptdev;
  869. struct panthor_csg_slot *slot;
  870. lockdep_assert_held(&ptdev->scheduler->lock);
  871. if (drm_WARN_ON(&ptdev->base, group->csg_id < 0 || group->csg_id >= MAX_CSGS))
  872. return -EINVAL;
  873. if (drm_WARN_ON(&ptdev->base, group->state == PANTHOR_CS_GROUP_ACTIVE))
  874. return -EINVAL;
  875. slot = &ptdev->scheduler->csg_slots[group->csg_id];
  876. panthor_vm_idle(group->vm);
  877. group->csg_id = -1;
  878. /* Tiler OOM events will be re-issued next time the group is scheduled. */
  879. atomic_set(&group->tiler_oom, 0);
  880. cancel_work(&group->tiler_oom_work);
  881. for (u32 i = 0; i < group->queue_count; i++)
  882. group->queues[i]->doorbell_id = -1;
  883. slot->group = NULL;
  884. group_put(group);
  885. return 0;
  886. }
  887. static bool
  888. group_is_idle(struct panthor_group *group)
  889. {
  890. u32 inactive_queues = group->idle_queues | group->blocked_queues;
  891. return hweight32(inactive_queues) == group->queue_count;
  892. }
  893. static bool
  894. group_can_run(struct panthor_group *group)
  895. {
  896. return group->state != PANTHOR_CS_GROUP_TERMINATED &&
  897. group->state != PANTHOR_CS_GROUP_UNKNOWN_STATE &&
  898. !group->destroyed && group->fatal_queues == 0 &&
  899. !group->timedout;
  900. }
  901. static bool
  902. queue_timeout_is_suspended(struct panthor_queue *queue)
  903. {
  904. /* When running, the remaining time is set to MAX_SCHEDULE_TIMEOUT. */
  905. return queue->timeout.remaining != MAX_SCHEDULE_TIMEOUT;
  906. }
  907. static void
  908. queue_reset_timeout_locked(struct panthor_queue *queue)
  909. {
  910. lockdep_assert_held(&queue->fence_ctx.lock);
  911. if (!queue_timeout_is_suspended(queue)) {
  912. mod_delayed_work(queue->scheduler.timeout_wq,
  913. &queue->timeout.work,
  914. msecs_to_jiffies(JOB_TIMEOUT_MS));
  915. }
  916. }
  917. static void
  918. queue_suspend_timeout_locked(struct panthor_queue *queue)
  919. {
  920. unsigned long qtimeout, now;
  921. struct panthor_group *group;
  922. struct panthor_job *job;
  923. bool timer_was_active;
  924. lockdep_assert_held(&queue->fence_ctx.lock);
  925. /* Already suspended, nothing to do. */
  926. if (queue_timeout_is_suspended(queue))
  927. return;
  928. job = list_first_entry_or_null(&queue->fence_ctx.in_flight_jobs,
  929. struct panthor_job, node);
  930. group = job ? job->group : NULL;
  931. /* If the queue is blocked and the group is idle, we want the timer to
  932. * keep running because the group can't be unblocked by other queues,
  933. * so it has to come from an external source, and we want to timebox
  934. * this external signalling.
  935. */
  936. if (group && group_can_run(group) &&
  937. (group->blocked_queues & BIT(job->queue_idx)) &&
  938. group_is_idle(group))
  939. return;
  940. now = jiffies;
  941. qtimeout = queue->timeout.work.timer.expires;
  942. /* Cancel the timer. */
  943. timer_was_active = cancel_delayed_work(&queue->timeout.work);
  944. if (!timer_was_active || !job)
  945. queue->timeout.remaining = msecs_to_jiffies(JOB_TIMEOUT_MS);
  946. else if (time_after(qtimeout, now))
  947. queue->timeout.remaining = qtimeout - now;
  948. else
  949. queue->timeout.remaining = 0;
  950. if (WARN_ON_ONCE(queue->timeout.remaining > msecs_to_jiffies(JOB_TIMEOUT_MS)))
  951. queue->timeout.remaining = msecs_to_jiffies(JOB_TIMEOUT_MS);
  952. }
  953. static void
  954. queue_suspend_timeout(struct panthor_queue *queue)
  955. {
  956. spin_lock(&queue->fence_ctx.lock);
  957. queue_suspend_timeout_locked(queue);
  958. spin_unlock(&queue->fence_ctx.lock);
  959. }
  960. static void
  961. queue_resume_timeout(struct panthor_queue *queue)
  962. {
  963. spin_lock(&queue->fence_ctx.lock);
  964. if (queue_timeout_is_suspended(queue)) {
  965. mod_delayed_work(queue->scheduler.timeout_wq,
  966. &queue->timeout.work,
  967. queue->timeout.remaining);
  968. queue->timeout.remaining = MAX_SCHEDULE_TIMEOUT;
  969. }
  970. spin_unlock(&queue->fence_ctx.lock);
  971. }
  972. /**
  973. * cs_slot_prog_locked() - Program a queue slot
  974. * @ptdev: Device.
  975. * @csg_id: Group slot ID.
  976. * @cs_id: Queue slot ID.
  977. *
  978. * Program a queue slot with the queue information so things can start being
  979. * executed on this queue.
  980. *
  981. * The group slot must have a group bound to it already (group_bind_locked()).
  982. */
  983. static void
  984. cs_slot_prog_locked(struct panthor_device *ptdev, u32 csg_id, u32 cs_id)
  985. {
  986. struct panthor_queue *queue = ptdev->scheduler->csg_slots[csg_id].group->queues[cs_id];
  987. struct panthor_fw_cs_iface *cs_iface = panthor_fw_get_cs_iface(ptdev, csg_id, cs_id);
  988. lockdep_assert_held(&ptdev->scheduler->lock);
  989. queue->iface.input->extract = queue->iface.output->extract;
  990. drm_WARN_ON(&ptdev->base, queue->iface.input->insert < queue->iface.input->extract);
  991. cs_iface->input->ringbuf_base = panthor_kernel_bo_gpuva(queue->ringbuf);
  992. cs_iface->input->ringbuf_size = panthor_kernel_bo_size(queue->ringbuf);
  993. cs_iface->input->ringbuf_input = queue->iface.input_fw_va;
  994. cs_iface->input->ringbuf_output = queue->iface.output_fw_va;
  995. cs_iface->input->config = CS_CONFIG_PRIORITY(queue->priority) |
  996. CS_CONFIG_DOORBELL(queue->doorbell_id);
  997. cs_iface->input->ack_irq_mask = ~0;
  998. panthor_fw_update_reqs(cs_iface, req,
  999. CS_IDLE_SYNC_WAIT |
  1000. CS_IDLE_EMPTY |
  1001. CS_STATE_START,
  1002. CS_IDLE_SYNC_WAIT |
  1003. CS_IDLE_EMPTY |
  1004. CS_STATE_MASK);
  1005. if (queue->iface.input->insert != queue->iface.input->extract)
  1006. queue_resume_timeout(queue);
  1007. }
  1008. /**
  1009. * cs_slot_reset_locked() - Reset a queue slot
  1010. * @ptdev: Device.
  1011. * @csg_id: Group slot.
  1012. * @cs_id: Queue slot.
  1013. *
  1014. * Change the queue slot state to STOP and suspend the queue timeout if
  1015. * the queue is not blocked.
  1016. *
  1017. * The group slot must have a group bound to it (group_bind_locked()).
  1018. */
  1019. static int
  1020. cs_slot_reset_locked(struct panthor_device *ptdev, u32 csg_id, u32 cs_id)
  1021. {
  1022. struct panthor_fw_cs_iface *cs_iface = panthor_fw_get_cs_iface(ptdev, csg_id, cs_id);
  1023. struct panthor_group *group = ptdev->scheduler->csg_slots[csg_id].group;
  1024. struct panthor_queue *queue = group->queues[cs_id];
  1025. lockdep_assert_held(&ptdev->scheduler->lock);
  1026. panthor_fw_update_reqs(cs_iface, req,
  1027. CS_STATE_STOP,
  1028. CS_STATE_MASK);
  1029. queue_suspend_timeout(queue);
  1030. return 0;
  1031. }
  1032. /**
  1033. * csg_slot_sync_priority_locked() - Synchronize the group slot priority
  1034. * @ptdev: Device.
  1035. * @csg_id: Group slot ID.
  1036. *
  1037. * Group slot priority update happens asynchronously. When we receive a
  1038. * %CSG_ENDPOINT_CONFIG, we know the update is effective, and can
  1039. * reflect it to our panthor_csg_slot object.
  1040. */
  1041. static void
  1042. csg_slot_sync_priority_locked(struct panthor_device *ptdev, u32 csg_id)
  1043. {
  1044. struct panthor_csg_slot *csg_slot = &ptdev->scheduler->csg_slots[csg_id];
  1045. struct panthor_fw_csg_iface *csg_iface;
  1046. u64 endpoint_req;
  1047. lockdep_assert_held(&ptdev->scheduler->lock);
  1048. csg_iface = panthor_fw_get_csg_iface(ptdev, csg_id);
  1049. endpoint_req = panthor_fw_csg_endpoint_req_get(ptdev, csg_iface);
  1050. csg_slot->priority = CSG_EP_REQ_PRIORITY_GET(endpoint_req);
  1051. }
  1052. /**
  1053. * cs_slot_sync_queue_state_locked() - Synchronize the queue slot priority
  1054. * @ptdev: Device.
  1055. * @csg_id: Group slot.
  1056. * @cs_id: Queue slot.
  1057. *
  1058. * Queue state is updated on group suspend or STATUS_UPDATE event.
  1059. */
  1060. static void
  1061. cs_slot_sync_queue_state_locked(struct panthor_device *ptdev, u32 csg_id, u32 cs_id)
  1062. {
  1063. struct panthor_group *group = ptdev->scheduler->csg_slots[csg_id].group;
  1064. struct panthor_queue *queue = group->queues[cs_id];
  1065. struct panthor_fw_cs_iface *cs_iface =
  1066. panthor_fw_get_cs_iface(group->ptdev, csg_id, cs_id);
  1067. u32 status_wait_cond;
  1068. switch (cs_iface->output->status_blocked_reason) {
  1069. case CS_STATUS_BLOCKED_REASON_UNBLOCKED:
  1070. if (queue->iface.input->insert == queue->iface.output->extract &&
  1071. cs_iface->output->status_scoreboards == 0)
  1072. group->idle_queues |= BIT(cs_id);
  1073. break;
  1074. case CS_STATUS_BLOCKED_REASON_SYNC_WAIT:
  1075. if (list_empty(&group->wait_node)) {
  1076. list_move_tail(&group->wait_node,
  1077. &group->ptdev->scheduler->groups.waiting);
  1078. }
  1079. /* The queue is only blocked if there's no deferred operation
  1080. * pending, which can be checked through the scoreboard status.
  1081. */
  1082. if (!cs_iface->output->status_scoreboards)
  1083. group->blocked_queues |= BIT(cs_id);
  1084. queue->syncwait.gpu_va = cs_iface->output->status_wait_sync_ptr;
  1085. queue->syncwait.ref = cs_iface->output->status_wait_sync_value;
  1086. status_wait_cond = cs_iface->output->status_wait & CS_STATUS_WAIT_SYNC_COND_MASK;
  1087. queue->syncwait.gt = status_wait_cond == CS_STATUS_WAIT_SYNC_COND_GT;
  1088. if (cs_iface->output->status_wait & CS_STATUS_WAIT_SYNC_64B) {
  1089. u64 sync_val_hi = cs_iface->output->status_wait_sync_value_hi;
  1090. queue->syncwait.sync64 = true;
  1091. queue->syncwait.ref |= sync_val_hi << 32;
  1092. } else {
  1093. queue->syncwait.sync64 = false;
  1094. }
  1095. break;
  1096. default:
  1097. /* Other reasons are not blocking. Consider the queue as runnable
  1098. * in those cases.
  1099. */
  1100. break;
  1101. }
  1102. }
  1103. static void
  1104. csg_slot_sync_queues_state_locked(struct panthor_device *ptdev, u32 csg_id)
  1105. {
  1106. struct panthor_csg_slot *csg_slot = &ptdev->scheduler->csg_slots[csg_id];
  1107. struct panthor_group *group = csg_slot->group;
  1108. u32 i;
  1109. lockdep_assert_held(&ptdev->scheduler->lock);
  1110. group->idle_queues = 0;
  1111. group->blocked_queues = 0;
  1112. for (i = 0; i < group->queue_count; i++) {
  1113. if (group->queues[i])
  1114. cs_slot_sync_queue_state_locked(ptdev, csg_id, i);
  1115. }
  1116. }
  1117. static void
  1118. csg_slot_sync_state_locked(struct panthor_device *ptdev, u32 csg_id)
  1119. {
  1120. struct panthor_csg_slot *csg_slot = &ptdev->scheduler->csg_slots[csg_id];
  1121. struct panthor_fw_csg_iface *csg_iface;
  1122. struct panthor_group *group;
  1123. enum panthor_group_state new_state, old_state;
  1124. u32 csg_state;
  1125. lockdep_assert_held(&ptdev->scheduler->lock);
  1126. csg_iface = panthor_fw_get_csg_iface(ptdev, csg_id);
  1127. group = csg_slot->group;
  1128. if (!group)
  1129. return;
  1130. old_state = group->state;
  1131. csg_state = csg_iface->output->ack & CSG_STATE_MASK;
  1132. switch (csg_state) {
  1133. case CSG_STATE_START:
  1134. case CSG_STATE_RESUME:
  1135. new_state = PANTHOR_CS_GROUP_ACTIVE;
  1136. break;
  1137. case CSG_STATE_TERMINATE:
  1138. new_state = PANTHOR_CS_GROUP_TERMINATED;
  1139. break;
  1140. case CSG_STATE_SUSPEND:
  1141. new_state = PANTHOR_CS_GROUP_SUSPENDED;
  1142. break;
  1143. default:
  1144. /* The unknown state might be caused by a FW state corruption,
  1145. * which means the group metadata can't be trusted anymore, and
  1146. * the SUSPEND operation might propagate the corruption to the
  1147. * suspend buffers. Flag the group state as unknown to make
  1148. * sure it's unusable after that point.
  1149. */
  1150. drm_err(&ptdev->base, "Invalid state on CSG %d (state=%d)",
  1151. csg_id, csg_state);
  1152. new_state = PANTHOR_CS_GROUP_UNKNOWN_STATE;
  1153. break;
  1154. }
  1155. if (old_state == new_state)
  1156. return;
  1157. /* The unknown state might be caused by a FW issue, reset the FW to
  1158. * take a fresh start.
  1159. */
  1160. if (new_state == PANTHOR_CS_GROUP_UNKNOWN_STATE)
  1161. panthor_device_schedule_reset(ptdev);
  1162. if (new_state == PANTHOR_CS_GROUP_SUSPENDED)
  1163. csg_slot_sync_queues_state_locked(ptdev, csg_id);
  1164. if (old_state == PANTHOR_CS_GROUP_ACTIVE) {
  1165. u32 i;
  1166. /* Reset the queue slots so we start from a clean
  1167. * state when starting/resuming a new group on this
  1168. * CSG slot. No wait needed here, and no ringbell
  1169. * either, since the CS slot will only be re-used
  1170. * on the next CSG start operation.
  1171. */
  1172. for (i = 0; i < group->queue_count; i++) {
  1173. if (group->queues[i])
  1174. cs_slot_reset_locked(ptdev, csg_id, i);
  1175. }
  1176. }
  1177. group->state = new_state;
  1178. }
  1179. static int
  1180. csg_slot_prog_locked(struct panthor_device *ptdev, u32 csg_id, u32 priority)
  1181. {
  1182. struct panthor_fw_csg_iface *csg_iface;
  1183. struct panthor_csg_slot *csg_slot;
  1184. struct panthor_group *group;
  1185. u32 queue_mask = 0, i;
  1186. u64 endpoint_req;
  1187. lockdep_assert_held(&ptdev->scheduler->lock);
  1188. if (priority > MAX_CSG_PRIO)
  1189. return -EINVAL;
  1190. if (drm_WARN_ON(&ptdev->base, csg_id >= MAX_CSGS))
  1191. return -EINVAL;
  1192. csg_slot = &ptdev->scheduler->csg_slots[csg_id];
  1193. group = csg_slot->group;
  1194. if (!group || group->state == PANTHOR_CS_GROUP_ACTIVE)
  1195. return 0;
  1196. csg_iface = panthor_fw_get_csg_iface(group->ptdev, csg_id);
  1197. for (i = 0; i < group->queue_count; i++) {
  1198. if (group->queues[i]) {
  1199. cs_slot_prog_locked(ptdev, csg_id, i);
  1200. queue_mask |= BIT(i);
  1201. }
  1202. }
  1203. csg_iface->input->allow_compute = group->compute_core_mask;
  1204. csg_iface->input->allow_fragment = group->fragment_core_mask;
  1205. csg_iface->input->allow_other = group->tiler_core_mask;
  1206. endpoint_req = CSG_EP_REQ_COMPUTE(group->max_compute_cores) |
  1207. CSG_EP_REQ_FRAGMENT(group->max_fragment_cores) |
  1208. CSG_EP_REQ_TILER(group->max_tiler_cores) |
  1209. CSG_EP_REQ_PRIORITY(priority);
  1210. panthor_fw_csg_endpoint_req_set(ptdev, csg_iface, endpoint_req);
  1211. csg_iface->input->config = panthor_vm_as(group->vm);
  1212. if (group->suspend_buf)
  1213. csg_iface->input->suspend_buf = panthor_kernel_bo_gpuva(group->suspend_buf);
  1214. else
  1215. csg_iface->input->suspend_buf = 0;
  1216. if (group->protm_suspend_buf) {
  1217. csg_iface->input->protm_suspend_buf =
  1218. panthor_kernel_bo_gpuva(group->protm_suspend_buf);
  1219. } else {
  1220. csg_iface->input->protm_suspend_buf = 0;
  1221. }
  1222. csg_iface->input->ack_irq_mask = ~0;
  1223. panthor_fw_toggle_reqs(csg_iface, doorbell_req, doorbell_ack, queue_mask);
  1224. return 0;
  1225. }
  1226. static void
  1227. cs_slot_process_fatal_event_locked(struct panthor_device *ptdev,
  1228. u32 csg_id, u32 cs_id)
  1229. {
  1230. struct panthor_scheduler *sched = ptdev->scheduler;
  1231. struct panthor_csg_slot *csg_slot = &sched->csg_slots[csg_id];
  1232. struct panthor_group *group = csg_slot->group;
  1233. struct panthor_fw_cs_iface *cs_iface;
  1234. u32 fatal;
  1235. u64 info;
  1236. lockdep_assert_held(&sched->lock);
  1237. cs_iface = panthor_fw_get_cs_iface(ptdev, csg_id, cs_id);
  1238. fatal = cs_iface->output->fatal;
  1239. info = cs_iface->output->fatal_info;
  1240. if (group) {
  1241. drm_warn(&ptdev->base, "CS_FATAL: pid=%d, comm=%s\n",
  1242. group->task_info.pid, group->task_info.comm);
  1243. group->fatal_queues |= BIT(cs_id);
  1244. }
  1245. if (CS_EXCEPTION_TYPE(fatal) == DRM_PANTHOR_EXCEPTION_CS_UNRECOVERABLE) {
  1246. /* If this exception is unrecoverable, queue a reset, and make
  1247. * sure we stop scheduling groups until the reset has happened.
  1248. */
  1249. panthor_device_schedule_reset(ptdev);
  1250. cancel_delayed_work(&sched->tick_work);
  1251. } else {
  1252. sched_queue_delayed_work(sched, tick, 0);
  1253. }
  1254. drm_warn(&ptdev->base,
  1255. "CSG slot %d CS slot: %d\n"
  1256. "CS_FATAL.EXCEPTION_TYPE: 0x%x (%s)\n"
  1257. "CS_FATAL.EXCEPTION_DATA: 0x%x\n"
  1258. "CS_FATAL_INFO.EXCEPTION_DATA: 0x%llx\n",
  1259. csg_id, cs_id,
  1260. (unsigned int)CS_EXCEPTION_TYPE(fatal),
  1261. panthor_exception_name(ptdev, CS_EXCEPTION_TYPE(fatal)),
  1262. (unsigned int)CS_EXCEPTION_DATA(fatal),
  1263. info);
  1264. }
  1265. static void
  1266. cs_slot_process_fault_event_locked(struct panthor_device *ptdev,
  1267. u32 csg_id, u32 cs_id)
  1268. {
  1269. struct panthor_scheduler *sched = ptdev->scheduler;
  1270. struct panthor_csg_slot *csg_slot = &sched->csg_slots[csg_id];
  1271. struct panthor_group *group = csg_slot->group;
  1272. struct panthor_queue *queue = group && cs_id < group->queue_count ?
  1273. group->queues[cs_id] : NULL;
  1274. struct panthor_fw_cs_iface *cs_iface;
  1275. u32 fault;
  1276. u64 info;
  1277. lockdep_assert_held(&sched->lock);
  1278. cs_iface = panthor_fw_get_cs_iface(ptdev, csg_id, cs_id);
  1279. fault = cs_iface->output->fault;
  1280. info = cs_iface->output->fault_info;
  1281. if (queue) {
  1282. u64 cs_extract = queue->iface.output->extract;
  1283. struct panthor_job *job;
  1284. spin_lock(&queue->fence_ctx.lock);
  1285. list_for_each_entry(job, &queue->fence_ctx.in_flight_jobs, node) {
  1286. if (cs_extract >= job->ringbuf.end)
  1287. continue;
  1288. if (cs_extract < job->ringbuf.start)
  1289. break;
  1290. dma_fence_set_error(job->done_fence, -EINVAL);
  1291. }
  1292. spin_unlock(&queue->fence_ctx.lock);
  1293. }
  1294. if (group) {
  1295. drm_warn(&ptdev->base, "CS_FAULT: pid=%d, comm=%s\n",
  1296. group->task_info.pid, group->task_info.comm);
  1297. }
  1298. drm_warn(&ptdev->base,
  1299. "CSG slot %d CS slot: %d\n"
  1300. "CS_FAULT.EXCEPTION_TYPE: 0x%x (%s)\n"
  1301. "CS_FAULT.EXCEPTION_DATA: 0x%x\n"
  1302. "CS_FAULT_INFO.EXCEPTION_DATA: 0x%llx\n",
  1303. csg_id, cs_id,
  1304. (unsigned int)CS_EXCEPTION_TYPE(fault),
  1305. panthor_exception_name(ptdev, CS_EXCEPTION_TYPE(fault)),
  1306. (unsigned int)CS_EXCEPTION_DATA(fault),
  1307. info);
  1308. }
  1309. static int group_process_tiler_oom(struct panthor_group *group, u32 cs_id)
  1310. {
  1311. struct panthor_device *ptdev = group->ptdev;
  1312. struct panthor_scheduler *sched = ptdev->scheduler;
  1313. u32 renderpasses_in_flight, pending_frag_count;
  1314. struct panthor_heap_pool *heaps = NULL;
  1315. u64 heap_address, new_chunk_va = 0;
  1316. u32 vt_start, vt_end, frag_end;
  1317. int ret, csg_id;
  1318. mutex_lock(&sched->lock);
  1319. csg_id = group->csg_id;
  1320. if (csg_id >= 0) {
  1321. struct panthor_fw_cs_iface *cs_iface;
  1322. cs_iface = panthor_fw_get_cs_iface(ptdev, csg_id, cs_id);
  1323. heaps = panthor_vm_get_heap_pool(group->vm, false);
  1324. heap_address = cs_iface->output->heap_address;
  1325. vt_start = cs_iface->output->heap_vt_start;
  1326. vt_end = cs_iface->output->heap_vt_end;
  1327. frag_end = cs_iface->output->heap_frag_end;
  1328. renderpasses_in_flight = vt_start - frag_end;
  1329. pending_frag_count = vt_end - frag_end;
  1330. }
  1331. mutex_unlock(&sched->lock);
  1332. /* The group got scheduled out, we stop here. We will get a new tiler OOM event
  1333. * when it's scheduled again.
  1334. */
  1335. if (unlikely(csg_id < 0))
  1336. return 0;
  1337. if (IS_ERR(heaps) || frag_end > vt_end || vt_end >= vt_start) {
  1338. ret = -EINVAL;
  1339. } else {
  1340. /* We do the allocation without holding the scheduler lock to avoid
  1341. * blocking the scheduling.
  1342. */
  1343. ret = panthor_heap_grow(heaps, heap_address,
  1344. renderpasses_in_flight,
  1345. pending_frag_count, &new_chunk_va);
  1346. }
  1347. /* If the heap context doesn't have memory for us, we want to let the
  1348. * FW try to reclaim memory by waiting for fragment jobs to land or by
  1349. * executing the tiler OOM exception handler, which is supposed to
  1350. * implement incremental rendering.
  1351. */
  1352. if (ret && ret != -ENOMEM) {
  1353. drm_warn(&ptdev->base, "Failed to extend the tiler heap\n");
  1354. group->fatal_queues |= BIT(cs_id);
  1355. sched_queue_delayed_work(sched, tick, 0);
  1356. goto out_put_heap_pool;
  1357. }
  1358. mutex_lock(&sched->lock);
  1359. csg_id = group->csg_id;
  1360. if (csg_id >= 0) {
  1361. struct panthor_fw_csg_iface *csg_iface;
  1362. struct panthor_fw_cs_iface *cs_iface;
  1363. csg_iface = panthor_fw_get_csg_iface(ptdev, csg_id);
  1364. cs_iface = panthor_fw_get_cs_iface(ptdev, csg_id, cs_id);
  1365. cs_iface->input->heap_start = new_chunk_va;
  1366. cs_iface->input->heap_end = new_chunk_va;
  1367. panthor_fw_update_reqs(cs_iface, req, cs_iface->output->ack, CS_TILER_OOM);
  1368. panthor_fw_toggle_reqs(csg_iface, doorbell_req, doorbell_ack, BIT(cs_id));
  1369. panthor_fw_ring_csg_doorbells(ptdev, BIT(csg_id));
  1370. }
  1371. mutex_unlock(&sched->lock);
  1372. /* We allocated a chunck, but couldn't link it to the heap
  1373. * context because the group was scheduled out while we were
  1374. * allocating memory. We need to return this chunk to the heap.
  1375. */
  1376. if (unlikely(csg_id < 0 && new_chunk_va))
  1377. panthor_heap_return_chunk(heaps, heap_address, new_chunk_va);
  1378. ret = 0;
  1379. out_put_heap_pool:
  1380. panthor_heap_pool_put(heaps);
  1381. return ret;
  1382. }
  1383. static void group_tiler_oom_work(struct work_struct *work)
  1384. {
  1385. struct panthor_group *group =
  1386. container_of(work, struct panthor_group, tiler_oom_work);
  1387. u32 tiler_oom = atomic_xchg(&group->tiler_oom, 0);
  1388. while (tiler_oom) {
  1389. u32 cs_id = ffs(tiler_oom) - 1;
  1390. group_process_tiler_oom(group, cs_id);
  1391. tiler_oom &= ~BIT(cs_id);
  1392. }
  1393. group_put(group);
  1394. }
  1395. static void
  1396. cs_slot_process_tiler_oom_event_locked(struct panthor_device *ptdev,
  1397. u32 csg_id, u32 cs_id)
  1398. {
  1399. struct panthor_scheduler *sched = ptdev->scheduler;
  1400. struct panthor_csg_slot *csg_slot = &sched->csg_slots[csg_id];
  1401. struct panthor_group *group = csg_slot->group;
  1402. lockdep_assert_held(&sched->lock);
  1403. if (drm_WARN_ON(&ptdev->base, !group))
  1404. return;
  1405. atomic_or(BIT(cs_id), &group->tiler_oom);
  1406. /* We don't use group_queue_work() here because we want to queue the
  1407. * work item to the heap_alloc_wq.
  1408. */
  1409. group_get(group);
  1410. if (!queue_work(sched->heap_alloc_wq, &group->tiler_oom_work))
  1411. group_put(group);
  1412. }
  1413. static bool cs_slot_process_irq_locked(struct panthor_device *ptdev,
  1414. u32 csg_id, u32 cs_id)
  1415. {
  1416. struct panthor_fw_cs_iface *cs_iface;
  1417. u32 req, ack, events;
  1418. lockdep_assert_held(&ptdev->scheduler->lock);
  1419. cs_iface = panthor_fw_get_cs_iface(ptdev, csg_id, cs_id);
  1420. req = cs_iface->input->req;
  1421. ack = cs_iface->output->ack;
  1422. events = (req ^ ack) & CS_EVT_MASK;
  1423. if (events & CS_FATAL)
  1424. cs_slot_process_fatal_event_locked(ptdev, csg_id, cs_id);
  1425. if (events & CS_FAULT)
  1426. cs_slot_process_fault_event_locked(ptdev, csg_id, cs_id);
  1427. if (events & CS_TILER_OOM)
  1428. cs_slot_process_tiler_oom_event_locked(ptdev, csg_id, cs_id);
  1429. /* We don't acknowledge the TILER_OOM event since its handling is
  1430. * deferred to a separate work.
  1431. */
  1432. panthor_fw_update_reqs(cs_iface, req, ack, CS_FATAL | CS_FAULT);
  1433. return (events & (CS_FAULT | CS_TILER_OOM)) != 0;
  1434. }
  1435. static void csg_slot_process_idle_event_locked(struct panthor_device *ptdev, u32 csg_id)
  1436. {
  1437. struct panthor_scheduler *sched = ptdev->scheduler;
  1438. lockdep_assert_held(&sched->lock);
  1439. sched->might_have_idle_groups = true;
  1440. /* Schedule a tick so we can evict idle groups and schedule non-idle
  1441. * ones. This will also update runtime PM and devfreq busy/idle states,
  1442. * so the device can lower its frequency or get suspended.
  1443. */
  1444. sched_queue_delayed_work(sched, tick, 0);
  1445. }
  1446. static void csg_slot_sync_update_locked(struct panthor_device *ptdev,
  1447. u32 csg_id)
  1448. {
  1449. struct panthor_csg_slot *csg_slot = &ptdev->scheduler->csg_slots[csg_id];
  1450. struct panthor_group *group = csg_slot->group;
  1451. lockdep_assert_held(&ptdev->scheduler->lock);
  1452. if (group)
  1453. group_queue_work(group, sync_upd);
  1454. sched_queue_work(ptdev->scheduler, sync_upd);
  1455. }
  1456. static void
  1457. csg_slot_process_progress_timer_event_locked(struct panthor_device *ptdev, u32 csg_id)
  1458. {
  1459. struct panthor_scheduler *sched = ptdev->scheduler;
  1460. struct panthor_csg_slot *csg_slot = &sched->csg_slots[csg_id];
  1461. struct panthor_group *group = csg_slot->group;
  1462. lockdep_assert_held(&sched->lock);
  1463. group = csg_slot->group;
  1464. if (!drm_WARN_ON(&ptdev->base, !group)) {
  1465. drm_warn(&ptdev->base, "CSG_PROGRESS_TIMER_EVENT: pid=%d, comm=%s\n",
  1466. group->task_info.pid, group->task_info.comm);
  1467. group->timedout = true;
  1468. }
  1469. drm_warn(&ptdev->base, "CSG slot %d progress timeout\n", csg_id);
  1470. sched_queue_delayed_work(sched, tick, 0);
  1471. }
  1472. static void sched_process_csg_irq_locked(struct panthor_device *ptdev, u32 csg_id)
  1473. {
  1474. u32 req, ack, cs_irq_req, cs_irq_ack, cs_irqs, csg_events;
  1475. struct panthor_fw_csg_iface *csg_iface;
  1476. u32 ring_cs_db_mask = 0;
  1477. lockdep_assert_held(&ptdev->scheduler->lock);
  1478. if (drm_WARN_ON(&ptdev->base, csg_id >= ptdev->scheduler->csg_slot_count))
  1479. return;
  1480. csg_iface = panthor_fw_get_csg_iface(ptdev, csg_id);
  1481. req = READ_ONCE(csg_iface->input->req);
  1482. ack = READ_ONCE(csg_iface->output->ack);
  1483. cs_irq_req = READ_ONCE(csg_iface->output->cs_irq_req);
  1484. cs_irq_ack = READ_ONCE(csg_iface->input->cs_irq_ack);
  1485. csg_events = (req ^ ack) & CSG_EVT_MASK;
  1486. /* There may not be any pending CSG/CS interrupts to process */
  1487. if (req == ack && cs_irq_req == cs_irq_ack)
  1488. return;
  1489. /* Immediately set IRQ_ACK bits to be same as the IRQ_REQ bits before
  1490. * examining the CS_ACK & CS_REQ bits. This would ensure that Host
  1491. * doesn't miss an interrupt for the CS in the race scenario where
  1492. * whilst Host is servicing an interrupt for the CS, firmware sends
  1493. * another interrupt for that CS.
  1494. */
  1495. csg_iface->input->cs_irq_ack = cs_irq_req;
  1496. panthor_fw_update_reqs(csg_iface, req, ack,
  1497. CSG_SYNC_UPDATE |
  1498. CSG_IDLE |
  1499. CSG_PROGRESS_TIMER_EVENT);
  1500. if (csg_events & CSG_IDLE)
  1501. csg_slot_process_idle_event_locked(ptdev, csg_id);
  1502. if (csg_events & CSG_PROGRESS_TIMER_EVENT)
  1503. csg_slot_process_progress_timer_event_locked(ptdev, csg_id);
  1504. cs_irqs = cs_irq_req ^ cs_irq_ack;
  1505. while (cs_irqs) {
  1506. u32 cs_id = ffs(cs_irqs) - 1;
  1507. if (cs_slot_process_irq_locked(ptdev, csg_id, cs_id))
  1508. ring_cs_db_mask |= BIT(cs_id);
  1509. cs_irqs &= ~BIT(cs_id);
  1510. }
  1511. if (csg_events & CSG_SYNC_UPDATE)
  1512. csg_slot_sync_update_locked(ptdev, csg_id);
  1513. if (ring_cs_db_mask)
  1514. panthor_fw_toggle_reqs(csg_iface, doorbell_req, doorbell_ack, ring_cs_db_mask);
  1515. panthor_fw_ring_csg_doorbells(ptdev, BIT(csg_id));
  1516. }
  1517. static void sched_process_idle_event_locked(struct panthor_device *ptdev)
  1518. {
  1519. struct panthor_fw_global_iface *glb_iface = panthor_fw_get_glb_iface(ptdev);
  1520. lockdep_assert_held(&ptdev->scheduler->lock);
  1521. /* Acknowledge the idle event and schedule a tick. */
  1522. panthor_fw_update_reqs(glb_iface, req, glb_iface->output->ack, GLB_IDLE);
  1523. sched_queue_delayed_work(ptdev->scheduler, tick, 0);
  1524. }
  1525. /**
  1526. * sched_process_global_irq_locked() - Process the scheduling part of a global IRQ
  1527. * @ptdev: Device.
  1528. */
  1529. static void sched_process_global_irq_locked(struct panthor_device *ptdev)
  1530. {
  1531. struct panthor_fw_global_iface *glb_iface = panthor_fw_get_glb_iface(ptdev);
  1532. u32 req, ack, evts;
  1533. lockdep_assert_held(&ptdev->scheduler->lock);
  1534. req = READ_ONCE(glb_iface->input->req);
  1535. ack = READ_ONCE(glb_iface->output->ack);
  1536. evts = (req ^ ack) & GLB_EVT_MASK;
  1537. if (evts & GLB_IDLE)
  1538. sched_process_idle_event_locked(ptdev);
  1539. }
  1540. static void process_fw_events_work(struct work_struct *work)
  1541. {
  1542. struct panthor_scheduler *sched = container_of(work, struct panthor_scheduler,
  1543. fw_events_work);
  1544. u32 events = atomic_xchg(&sched->fw_events, 0);
  1545. struct panthor_device *ptdev = sched->ptdev;
  1546. mutex_lock(&sched->lock);
  1547. if (events & JOB_INT_GLOBAL_IF) {
  1548. sched_process_global_irq_locked(ptdev);
  1549. events &= ~JOB_INT_GLOBAL_IF;
  1550. }
  1551. while (events) {
  1552. u32 csg_id = ffs(events) - 1;
  1553. sched_process_csg_irq_locked(ptdev, csg_id);
  1554. events &= ~BIT(csg_id);
  1555. }
  1556. mutex_unlock(&sched->lock);
  1557. }
  1558. /**
  1559. * panthor_sched_report_fw_events() - Report FW events to the scheduler.
  1560. */
  1561. void panthor_sched_report_fw_events(struct panthor_device *ptdev, u32 events)
  1562. {
  1563. if (!ptdev->scheduler)
  1564. return;
  1565. atomic_or(events, &ptdev->scheduler->fw_events);
  1566. sched_queue_work(ptdev->scheduler, fw_events);
  1567. }
  1568. static const char *fence_get_driver_name(struct dma_fence *fence)
  1569. {
  1570. return "panthor";
  1571. }
  1572. static const char *queue_fence_get_timeline_name(struct dma_fence *fence)
  1573. {
  1574. return "queue-fence";
  1575. }
  1576. static const struct dma_fence_ops panthor_queue_fence_ops = {
  1577. .get_driver_name = fence_get_driver_name,
  1578. .get_timeline_name = queue_fence_get_timeline_name,
  1579. };
  1580. struct panthor_csg_slots_upd_ctx {
  1581. u32 update_mask;
  1582. u32 timedout_mask;
  1583. struct {
  1584. u32 value;
  1585. u32 mask;
  1586. } requests[MAX_CSGS];
  1587. };
  1588. static void csgs_upd_ctx_init(struct panthor_csg_slots_upd_ctx *ctx)
  1589. {
  1590. memset(ctx, 0, sizeof(*ctx));
  1591. }
  1592. static void csgs_upd_ctx_queue_reqs(struct panthor_device *ptdev,
  1593. struct panthor_csg_slots_upd_ctx *ctx,
  1594. u32 csg_id, u32 value, u32 mask)
  1595. {
  1596. if (drm_WARN_ON(&ptdev->base, !mask) ||
  1597. drm_WARN_ON(&ptdev->base, csg_id >= ptdev->scheduler->csg_slot_count))
  1598. return;
  1599. ctx->requests[csg_id].value = (ctx->requests[csg_id].value & ~mask) | (value & mask);
  1600. ctx->requests[csg_id].mask |= mask;
  1601. ctx->update_mask |= BIT(csg_id);
  1602. }
  1603. static int csgs_upd_ctx_apply_locked(struct panthor_device *ptdev,
  1604. struct panthor_csg_slots_upd_ctx *ctx)
  1605. {
  1606. struct panthor_scheduler *sched = ptdev->scheduler;
  1607. u32 update_slots = ctx->update_mask;
  1608. lockdep_assert_held(&sched->lock);
  1609. if (!ctx->update_mask)
  1610. return 0;
  1611. while (update_slots) {
  1612. struct panthor_fw_csg_iface *csg_iface;
  1613. u32 csg_id = ffs(update_slots) - 1;
  1614. update_slots &= ~BIT(csg_id);
  1615. csg_iface = panthor_fw_get_csg_iface(ptdev, csg_id);
  1616. panthor_fw_update_reqs(csg_iface, req,
  1617. ctx->requests[csg_id].value,
  1618. ctx->requests[csg_id].mask);
  1619. }
  1620. panthor_fw_ring_csg_doorbells(ptdev, ctx->update_mask);
  1621. update_slots = ctx->update_mask;
  1622. while (update_slots) {
  1623. struct panthor_fw_csg_iface *csg_iface;
  1624. u32 csg_id = ffs(update_slots) - 1;
  1625. u32 req_mask = ctx->requests[csg_id].mask, acked;
  1626. int ret;
  1627. update_slots &= ~BIT(csg_id);
  1628. csg_iface = panthor_fw_get_csg_iface(ptdev, csg_id);
  1629. ret = panthor_fw_csg_wait_acks(ptdev, csg_id, req_mask, &acked, 100);
  1630. if (acked & CSG_ENDPOINT_CONFIG)
  1631. csg_slot_sync_priority_locked(ptdev, csg_id);
  1632. if (acked & CSG_STATE_MASK)
  1633. csg_slot_sync_state_locked(ptdev, csg_id);
  1634. if (acked & CSG_STATUS_UPDATE)
  1635. csg_slot_sync_queues_state_locked(ptdev, csg_id);
  1636. if (ret && acked != req_mask &&
  1637. ((csg_iface->input->req ^ csg_iface->output->ack) & req_mask) != 0) {
  1638. drm_err(&ptdev->base, "CSG %d update request timedout", csg_id);
  1639. ctx->timedout_mask |= BIT(csg_id);
  1640. }
  1641. }
  1642. if (ctx->timedout_mask)
  1643. return -ETIMEDOUT;
  1644. return 0;
  1645. }
  1646. struct panthor_sched_tick_ctx {
  1647. struct list_head old_groups[PANTHOR_CSG_PRIORITY_COUNT];
  1648. struct list_head groups[PANTHOR_CSG_PRIORITY_COUNT];
  1649. u32 idle_group_count;
  1650. u32 group_count;
  1651. struct panthor_vm *vms[MAX_CS_PER_CSG];
  1652. u32 as_count;
  1653. bool immediate_tick;
  1654. bool stop_tick;
  1655. u32 csg_upd_failed_mask;
  1656. };
  1657. static bool
  1658. tick_ctx_is_full(const struct panthor_scheduler *sched,
  1659. const struct panthor_sched_tick_ctx *ctx)
  1660. {
  1661. return ctx->group_count == sched->csg_slot_count;
  1662. }
  1663. static void
  1664. tick_ctx_pick_groups_from_list(const struct panthor_scheduler *sched,
  1665. struct panthor_sched_tick_ctx *ctx,
  1666. struct list_head *queue,
  1667. bool skip_idle_groups,
  1668. bool owned_by_tick_ctx)
  1669. {
  1670. struct panthor_group *group, *tmp;
  1671. if (tick_ctx_is_full(sched, ctx))
  1672. return;
  1673. list_for_each_entry_safe(group, tmp, queue, run_node) {
  1674. u32 i;
  1675. if (!group_can_run(group))
  1676. continue;
  1677. if (skip_idle_groups && group_is_idle(group))
  1678. continue;
  1679. for (i = 0; i < ctx->as_count; i++) {
  1680. if (ctx->vms[i] == group->vm)
  1681. break;
  1682. }
  1683. if (i == ctx->as_count && ctx->as_count == sched->as_slot_count)
  1684. continue;
  1685. if (!owned_by_tick_ctx)
  1686. group_get(group);
  1687. ctx->group_count++;
  1688. /* If we have more than one active group with the same priority,
  1689. * we need to keep ticking to rotate the CSG priority.
  1690. */
  1691. if (group_is_idle(group))
  1692. ctx->idle_group_count++;
  1693. else if (!list_empty(&ctx->groups[group->priority]))
  1694. ctx->stop_tick = false;
  1695. list_move_tail(&group->run_node, &ctx->groups[group->priority]);
  1696. if (i == ctx->as_count)
  1697. ctx->vms[ctx->as_count++] = group->vm;
  1698. if (tick_ctx_is_full(sched, ctx))
  1699. return;
  1700. }
  1701. }
  1702. static void
  1703. tick_ctx_insert_old_group(struct panthor_scheduler *sched,
  1704. struct panthor_sched_tick_ctx *ctx,
  1705. struct panthor_group *group)
  1706. {
  1707. struct panthor_csg_slot *csg_slot = &sched->csg_slots[group->csg_id];
  1708. struct panthor_group *other_group;
  1709. /* Class groups in descending priority order so we can easily rotate. */
  1710. list_for_each_entry(other_group,
  1711. &ctx->old_groups[csg_slot->group->priority],
  1712. run_node) {
  1713. struct panthor_csg_slot *other_csg_slot = &sched->csg_slots[other_group->csg_id];
  1714. /* Our group has a higher prio than the one we're testing against,
  1715. * place it just before.
  1716. */
  1717. if (csg_slot->priority > other_csg_slot->priority) {
  1718. list_add_tail(&group->run_node, &other_group->run_node);
  1719. return;
  1720. }
  1721. }
  1722. list_add_tail(&group->run_node, &ctx->old_groups[group->priority]);
  1723. }
  1724. static void
  1725. tick_ctx_init(struct panthor_scheduler *sched,
  1726. struct panthor_sched_tick_ctx *ctx)
  1727. {
  1728. struct panthor_device *ptdev = sched->ptdev;
  1729. struct panthor_csg_slots_upd_ctx upd_ctx;
  1730. int ret;
  1731. u32 i;
  1732. memset(ctx, 0, sizeof(*ctx));
  1733. csgs_upd_ctx_init(&upd_ctx);
  1734. ctx->stop_tick = true;
  1735. for (i = 0; i < ARRAY_SIZE(ctx->groups); i++) {
  1736. INIT_LIST_HEAD(&ctx->groups[i]);
  1737. INIT_LIST_HEAD(&ctx->old_groups[i]);
  1738. }
  1739. for (i = 0; i < sched->csg_slot_count; i++) {
  1740. struct panthor_csg_slot *csg_slot = &sched->csg_slots[i];
  1741. struct panthor_group *group = csg_slot->group;
  1742. struct panthor_fw_csg_iface *csg_iface;
  1743. if (!group)
  1744. continue;
  1745. csg_iface = panthor_fw_get_csg_iface(ptdev, i);
  1746. group_get(group);
  1747. /* If there was unhandled faults on the VM, force processing of
  1748. * CSG IRQs, so we can flag the faulty queue.
  1749. */
  1750. if (panthor_vm_has_unhandled_faults(group->vm)) {
  1751. sched_process_csg_irq_locked(ptdev, i);
  1752. /* No fatal fault reported, flag all queues as faulty. */
  1753. if (!group->fatal_queues)
  1754. group->fatal_queues |= GENMASK(group->queue_count - 1, 0);
  1755. }
  1756. tick_ctx_insert_old_group(sched, ctx, group);
  1757. csgs_upd_ctx_queue_reqs(ptdev, &upd_ctx, i,
  1758. csg_iface->output->ack ^ CSG_STATUS_UPDATE,
  1759. CSG_STATUS_UPDATE);
  1760. }
  1761. ret = csgs_upd_ctx_apply_locked(ptdev, &upd_ctx);
  1762. if (ret) {
  1763. panthor_device_schedule_reset(ptdev);
  1764. ctx->csg_upd_failed_mask |= upd_ctx.timedout_mask;
  1765. }
  1766. }
  1767. static void
  1768. group_term_post_processing(struct panthor_group *group)
  1769. {
  1770. struct panthor_job *job, *tmp;
  1771. LIST_HEAD(faulty_jobs);
  1772. bool cookie;
  1773. u32 i = 0;
  1774. if (drm_WARN_ON(&group->ptdev->base, group_can_run(group)))
  1775. return;
  1776. cookie = dma_fence_begin_signalling();
  1777. for (i = 0; i < group->queue_count; i++) {
  1778. struct panthor_queue *queue = group->queues[i];
  1779. struct panthor_syncobj_64b *syncobj;
  1780. int err;
  1781. if (group->fatal_queues & BIT(i))
  1782. err = -EINVAL;
  1783. else if (group->timedout)
  1784. err = -ETIMEDOUT;
  1785. else
  1786. err = -ECANCELED;
  1787. if (!queue)
  1788. continue;
  1789. spin_lock(&queue->fence_ctx.lock);
  1790. list_for_each_entry_safe(job, tmp, &queue->fence_ctx.in_flight_jobs, node) {
  1791. list_move_tail(&job->node, &faulty_jobs);
  1792. dma_fence_set_error(job->done_fence, err);
  1793. dma_fence_signal_locked(job->done_fence);
  1794. }
  1795. spin_unlock(&queue->fence_ctx.lock);
  1796. /* Manually update the syncobj seqno to unblock waiters. */
  1797. syncobj = group->syncobjs->kmap + (i * sizeof(*syncobj));
  1798. syncobj->status = ~0;
  1799. syncobj->seqno = atomic64_read(&queue->fence_ctx.seqno);
  1800. sched_queue_work(group->ptdev->scheduler, sync_upd);
  1801. }
  1802. dma_fence_end_signalling(cookie);
  1803. list_for_each_entry_safe(job, tmp, &faulty_jobs, node) {
  1804. list_del_init(&job->node);
  1805. panthor_job_put(&job->base);
  1806. }
  1807. }
  1808. static void group_term_work(struct work_struct *work)
  1809. {
  1810. struct panthor_group *group =
  1811. container_of(work, struct panthor_group, term_work);
  1812. group_term_post_processing(group);
  1813. group_put(group);
  1814. }
  1815. static void
  1816. tick_ctx_cleanup(struct panthor_scheduler *sched,
  1817. struct panthor_sched_tick_ctx *ctx)
  1818. {
  1819. struct panthor_device *ptdev = sched->ptdev;
  1820. struct panthor_group *group, *tmp;
  1821. u32 i;
  1822. for (i = 0; i < ARRAY_SIZE(ctx->old_groups); i++) {
  1823. list_for_each_entry_safe(group, tmp, &ctx->old_groups[i], run_node) {
  1824. /* If everything went fine, we should only have groups
  1825. * to be terminated in the old_groups lists.
  1826. */
  1827. drm_WARN_ON(&ptdev->base, !ctx->csg_upd_failed_mask &&
  1828. group_can_run(group));
  1829. if (!group_can_run(group)) {
  1830. list_del_init(&group->run_node);
  1831. list_del_init(&group->wait_node);
  1832. group_queue_work(group, term);
  1833. } else if (group->csg_id >= 0) {
  1834. list_del_init(&group->run_node);
  1835. } else {
  1836. list_move(&group->run_node,
  1837. group_is_idle(group) ?
  1838. &sched->groups.idle[group->priority] :
  1839. &sched->groups.runnable[group->priority]);
  1840. }
  1841. group_put(group);
  1842. }
  1843. }
  1844. for (i = 0; i < ARRAY_SIZE(ctx->groups); i++) {
  1845. /* If everything went fine, the groups to schedule lists should
  1846. * be empty.
  1847. */
  1848. drm_WARN_ON(&ptdev->base,
  1849. !ctx->csg_upd_failed_mask && !list_empty(&ctx->groups[i]));
  1850. list_for_each_entry_safe(group, tmp, &ctx->groups[i], run_node) {
  1851. if (group->csg_id >= 0) {
  1852. list_del_init(&group->run_node);
  1853. } else {
  1854. list_move(&group->run_node,
  1855. group_is_idle(group) ?
  1856. &sched->groups.idle[group->priority] :
  1857. &sched->groups.runnable[group->priority]);
  1858. }
  1859. group_put(group);
  1860. }
  1861. }
  1862. }
  1863. static void
  1864. tick_ctx_apply(struct panthor_scheduler *sched, struct panthor_sched_tick_ctx *ctx)
  1865. {
  1866. struct panthor_group *group, *tmp;
  1867. struct panthor_device *ptdev = sched->ptdev;
  1868. struct panthor_csg_slot *csg_slot;
  1869. int prio, new_csg_prio = MAX_CSG_PRIO, i;
  1870. u32 free_csg_slots = 0;
  1871. struct panthor_csg_slots_upd_ctx upd_ctx;
  1872. int ret;
  1873. csgs_upd_ctx_init(&upd_ctx);
  1874. for (prio = PANTHOR_CSG_PRIORITY_COUNT - 1; prio >= 0; prio--) {
  1875. /* Suspend or terminate evicted groups. */
  1876. list_for_each_entry(group, &ctx->old_groups[prio], run_node) {
  1877. bool term = !group_can_run(group);
  1878. int csg_id = group->csg_id;
  1879. if (drm_WARN_ON(&ptdev->base, csg_id < 0))
  1880. continue;
  1881. csg_slot = &sched->csg_slots[csg_id];
  1882. csgs_upd_ctx_queue_reqs(ptdev, &upd_ctx, csg_id,
  1883. term ? CSG_STATE_TERMINATE : CSG_STATE_SUSPEND,
  1884. CSG_STATE_MASK);
  1885. }
  1886. /* Update priorities on already running groups. */
  1887. list_for_each_entry(group, &ctx->groups[prio], run_node) {
  1888. struct panthor_fw_csg_iface *csg_iface;
  1889. int csg_id = group->csg_id;
  1890. if (csg_id < 0) {
  1891. new_csg_prio--;
  1892. continue;
  1893. }
  1894. csg_slot = &sched->csg_slots[csg_id];
  1895. csg_iface = panthor_fw_get_csg_iface(ptdev, csg_id);
  1896. if (csg_slot->priority == new_csg_prio) {
  1897. new_csg_prio--;
  1898. continue;
  1899. }
  1900. panthor_fw_csg_endpoint_req_update(ptdev, csg_iface,
  1901. CSG_EP_REQ_PRIORITY(new_csg_prio),
  1902. CSG_EP_REQ_PRIORITY_MASK);
  1903. csgs_upd_ctx_queue_reqs(ptdev, &upd_ctx, csg_id,
  1904. csg_iface->output->ack ^ CSG_ENDPOINT_CONFIG,
  1905. CSG_ENDPOINT_CONFIG);
  1906. new_csg_prio--;
  1907. }
  1908. }
  1909. ret = csgs_upd_ctx_apply_locked(ptdev, &upd_ctx);
  1910. if (ret) {
  1911. panthor_device_schedule_reset(ptdev);
  1912. ctx->csg_upd_failed_mask |= upd_ctx.timedout_mask;
  1913. return;
  1914. }
  1915. /* Unbind evicted groups. */
  1916. for (prio = PANTHOR_CSG_PRIORITY_COUNT - 1; prio >= 0; prio--) {
  1917. list_for_each_entry(group, &ctx->old_groups[prio], run_node) {
  1918. /* This group is gone. Process interrupts to clear
  1919. * any pending interrupts before we start the new
  1920. * group.
  1921. */
  1922. if (group->csg_id >= 0)
  1923. sched_process_csg_irq_locked(ptdev, group->csg_id);
  1924. group_unbind_locked(group);
  1925. }
  1926. }
  1927. for (i = 0; i < sched->csg_slot_count; i++) {
  1928. if (!sched->csg_slots[i].group)
  1929. free_csg_slots |= BIT(i);
  1930. }
  1931. csgs_upd_ctx_init(&upd_ctx);
  1932. new_csg_prio = MAX_CSG_PRIO;
  1933. /* Start new groups. */
  1934. for (prio = PANTHOR_CSG_PRIORITY_COUNT - 1; prio >= 0; prio--) {
  1935. list_for_each_entry(group, &ctx->groups[prio], run_node) {
  1936. int csg_id = group->csg_id;
  1937. struct panthor_fw_csg_iface *csg_iface;
  1938. if (csg_id >= 0) {
  1939. new_csg_prio--;
  1940. continue;
  1941. }
  1942. csg_id = ffs(free_csg_slots) - 1;
  1943. if (drm_WARN_ON(&ptdev->base, csg_id < 0))
  1944. break;
  1945. csg_iface = panthor_fw_get_csg_iface(ptdev, csg_id);
  1946. csg_slot = &sched->csg_slots[csg_id];
  1947. group_bind_locked(group, csg_id);
  1948. csg_slot_prog_locked(ptdev, csg_id, new_csg_prio--);
  1949. csgs_upd_ctx_queue_reqs(ptdev, &upd_ctx, csg_id,
  1950. group->state == PANTHOR_CS_GROUP_SUSPENDED ?
  1951. CSG_STATE_RESUME : CSG_STATE_START,
  1952. CSG_STATE_MASK);
  1953. csgs_upd_ctx_queue_reqs(ptdev, &upd_ctx, csg_id,
  1954. csg_iface->output->ack ^ CSG_ENDPOINT_CONFIG,
  1955. CSG_ENDPOINT_CONFIG);
  1956. free_csg_slots &= ~BIT(csg_id);
  1957. }
  1958. }
  1959. ret = csgs_upd_ctx_apply_locked(ptdev, &upd_ctx);
  1960. if (ret) {
  1961. panthor_device_schedule_reset(ptdev);
  1962. ctx->csg_upd_failed_mask |= upd_ctx.timedout_mask;
  1963. return;
  1964. }
  1965. for (prio = PANTHOR_CSG_PRIORITY_COUNT - 1; prio >= 0; prio--) {
  1966. list_for_each_entry_safe(group, tmp, &ctx->groups[prio], run_node) {
  1967. list_del_init(&group->run_node);
  1968. /* If the group has been destroyed while we were
  1969. * scheduling, ask for an immediate tick to
  1970. * re-evaluate as soon as possible and get rid of
  1971. * this dangling group.
  1972. */
  1973. if (group->destroyed)
  1974. ctx->immediate_tick = true;
  1975. group_put(group);
  1976. }
  1977. /* Return evicted groups to the idle or run queues. Groups
  1978. * that can no longer be run (because they've been destroyed
  1979. * or experienced an unrecoverable error) will be scheduled
  1980. * for destruction in tick_ctx_cleanup().
  1981. */
  1982. list_for_each_entry_safe(group, tmp, &ctx->old_groups[prio], run_node) {
  1983. if (!group_can_run(group))
  1984. continue;
  1985. if (group_is_idle(group))
  1986. list_move_tail(&group->run_node, &sched->groups.idle[prio]);
  1987. else
  1988. list_move_tail(&group->run_node, &sched->groups.runnable[prio]);
  1989. group_put(group);
  1990. }
  1991. }
  1992. sched->used_csg_slot_count = ctx->group_count;
  1993. sched->might_have_idle_groups = ctx->idle_group_count > 0;
  1994. }
  1995. static u64
  1996. tick_ctx_update_resched_target(struct panthor_scheduler *sched,
  1997. const struct panthor_sched_tick_ctx *ctx)
  1998. {
  1999. u64 resched_target;
  2000. if (ctx->stop_tick)
  2001. goto no_tick;
  2002. resched_target = sched->last_tick + sched->tick_period;
  2003. if (time_before64(sched->resched_target, sched->last_tick) ||
  2004. time_before64(resched_target, sched->resched_target))
  2005. sched->resched_target = resched_target;
  2006. return sched->resched_target - sched->last_tick;
  2007. no_tick:
  2008. sched->resched_target = U64_MAX;
  2009. return U64_MAX;
  2010. }
  2011. static void tick_work(struct work_struct *work)
  2012. {
  2013. struct panthor_scheduler *sched = container_of(work, struct panthor_scheduler,
  2014. tick_work.work);
  2015. struct panthor_device *ptdev = sched->ptdev;
  2016. struct panthor_sched_tick_ctx ctx;
  2017. u64 resched_target = sched->resched_target;
  2018. u64 remaining_jiffies = 0, resched_delay;
  2019. u64 now = get_jiffies_64();
  2020. int prio, ret, cookie;
  2021. bool full_tick;
  2022. if (!drm_dev_enter(&ptdev->base, &cookie))
  2023. return;
  2024. ret = panthor_device_resume_and_get(ptdev);
  2025. if (drm_WARN_ON(&ptdev->base, ret))
  2026. goto out_dev_exit;
  2027. /* If the tick is stopped, calculate when the next tick would be */
  2028. if (resched_target == U64_MAX)
  2029. resched_target = sched->last_tick + sched->tick_period;
  2030. if (time_before64(now, resched_target))
  2031. remaining_jiffies = resched_target - now;
  2032. full_tick = remaining_jiffies == 0;
  2033. mutex_lock(&sched->lock);
  2034. if (panthor_device_reset_is_pending(sched->ptdev))
  2035. goto out_unlock;
  2036. tick_ctx_init(sched, &ctx);
  2037. if (ctx.csg_upd_failed_mask)
  2038. goto out_cleanup_ctx;
  2039. if (!full_tick) {
  2040. /* Scheduling forced in the middle of a tick. Only RT groups
  2041. * can preempt non-RT ones. Currently running RT groups can't be
  2042. * preempted.
  2043. */
  2044. for (prio = PANTHOR_CSG_PRIORITY_COUNT - 1;
  2045. prio >= 0 && !tick_ctx_is_full(sched, &ctx);
  2046. prio--) {
  2047. tick_ctx_pick_groups_from_list(sched, &ctx, &ctx.old_groups[prio],
  2048. true, true);
  2049. if (prio == PANTHOR_CSG_PRIORITY_RT) {
  2050. tick_ctx_pick_groups_from_list(sched, &ctx,
  2051. &sched->groups.runnable[prio],
  2052. true, false);
  2053. }
  2054. }
  2055. }
  2056. /* First pick non-idle groups */
  2057. for (prio = PANTHOR_CSG_PRIORITY_COUNT - 1;
  2058. prio >= 0 && !tick_ctx_is_full(sched, &ctx);
  2059. prio--) {
  2060. struct panthor_group *old_highest_prio_group =
  2061. list_first_entry_or_null(&ctx.old_groups[prio],
  2062. struct panthor_group, run_node);
  2063. /* Pull out the group with the highest prio for rotation. */
  2064. if (old_highest_prio_group)
  2065. list_del(&old_highest_prio_group->run_node);
  2066. /* Re-insert old active groups so they get a chance to run with higher prio. */
  2067. tick_ctx_pick_groups_from_list(sched, &ctx, &ctx.old_groups[prio], true, true);
  2068. /* Fill the remaining slots with runnable groups. */
  2069. tick_ctx_pick_groups_from_list(sched, &ctx, &sched->groups.runnable[prio],
  2070. true, false);
  2071. /* Re-insert the old group with the highest prio, and give it a chance to be
  2072. * scheduled again (but with a lower prio) if there's room left.
  2073. */
  2074. if (old_highest_prio_group) {
  2075. list_add_tail(&old_highest_prio_group->run_node, &ctx.old_groups[prio]);
  2076. tick_ctx_pick_groups_from_list(sched, &ctx, &ctx.old_groups[prio],
  2077. true, true);
  2078. }
  2079. }
  2080. /* If we have free CSG slots left, pick idle groups */
  2081. for (prio = PANTHOR_CSG_PRIORITY_COUNT - 1;
  2082. prio >= 0 && !tick_ctx_is_full(sched, &ctx);
  2083. prio--) {
  2084. /* Check the old_group queue first to avoid reprogramming the slots */
  2085. tick_ctx_pick_groups_from_list(sched, &ctx, &ctx.old_groups[prio], false, true);
  2086. tick_ctx_pick_groups_from_list(sched, &ctx, &sched->groups.idle[prio],
  2087. false, false);
  2088. }
  2089. tick_ctx_apply(sched, &ctx);
  2090. if (ctx.csg_upd_failed_mask)
  2091. goto out_cleanup_ctx;
  2092. if (ctx.idle_group_count == ctx.group_count) {
  2093. panthor_devfreq_record_idle(sched->ptdev);
  2094. if (sched->pm.has_ref) {
  2095. pm_runtime_put_autosuspend(ptdev->base.dev);
  2096. sched->pm.has_ref = false;
  2097. }
  2098. } else {
  2099. panthor_devfreq_record_busy(sched->ptdev);
  2100. if (!sched->pm.has_ref) {
  2101. pm_runtime_get(ptdev->base.dev);
  2102. sched->pm.has_ref = true;
  2103. }
  2104. }
  2105. sched->last_tick = now;
  2106. resched_delay = tick_ctx_update_resched_target(sched, &ctx);
  2107. if (ctx.immediate_tick)
  2108. resched_delay = 0;
  2109. if (resched_delay != U64_MAX)
  2110. sched_queue_delayed_work(sched, tick, resched_delay);
  2111. out_cleanup_ctx:
  2112. tick_ctx_cleanup(sched, &ctx);
  2113. out_unlock:
  2114. mutex_unlock(&sched->lock);
  2115. pm_runtime_mark_last_busy(ptdev->base.dev);
  2116. pm_runtime_put_autosuspend(ptdev->base.dev);
  2117. out_dev_exit:
  2118. drm_dev_exit(cookie);
  2119. }
  2120. static int panthor_queue_eval_syncwait(struct panthor_group *group, u8 queue_idx)
  2121. {
  2122. struct panthor_queue *queue = group->queues[queue_idx];
  2123. union {
  2124. struct panthor_syncobj_64b sync64;
  2125. struct panthor_syncobj_32b sync32;
  2126. } *syncobj;
  2127. bool result;
  2128. u64 value;
  2129. syncobj = panthor_queue_get_syncwait_obj(group, queue);
  2130. if (!syncobj)
  2131. return -EINVAL;
  2132. value = queue->syncwait.sync64 ?
  2133. syncobj->sync64.seqno :
  2134. syncobj->sync32.seqno;
  2135. if (queue->syncwait.gt)
  2136. result = value > queue->syncwait.ref;
  2137. else
  2138. result = value <= queue->syncwait.ref;
  2139. if (result)
  2140. panthor_queue_put_syncwait_obj(queue);
  2141. return result;
  2142. }
  2143. static void sync_upd_work(struct work_struct *work)
  2144. {
  2145. struct panthor_scheduler *sched = container_of(work,
  2146. struct panthor_scheduler,
  2147. sync_upd_work);
  2148. struct panthor_group *group, *tmp;
  2149. bool immediate_tick = false;
  2150. mutex_lock(&sched->lock);
  2151. list_for_each_entry_safe(group, tmp, &sched->groups.waiting, wait_node) {
  2152. u32 tested_queues = group->blocked_queues;
  2153. u32 unblocked_queues = 0;
  2154. while (tested_queues) {
  2155. u32 cs_id = ffs(tested_queues) - 1;
  2156. int ret;
  2157. ret = panthor_queue_eval_syncwait(group, cs_id);
  2158. drm_WARN_ON(&group->ptdev->base, ret < 0);
  2159. if (ret)
  2160. unblocked_queues |= BIT(cs_id);
  2161. tested_queues &= ~BIT(cs_id);
  2162. }
  2163. if (unblocked_queues) {
  2164. group->blocked_queues &= ~unblocked_queues;
  2165. if (group->csg_id < 0) {
  2166. list_move(&group->run_node,
  2167. &sched->groups.runnable[group->priority]);
  2168. if (group->priority == PANTHOR_CSG_PRIORITY_RT)
  2169. immediate_tick = true;
  2170. }
  2171. }
  2172. if (!group->blocked_queues)
  2173. list_del_init(&group->wait_node);
  2174. }
  2175. mutex_unlock(&sched->lock);
  2176. if (immediate_tick)
  2177. sched_queue_delayed_work(sched, tick, 0);
  2178. }
  2179. static void sched_resume_tick(struct panthor_device *ptdev)
  2180. {
  2181. struct panthor_scheduler *sched = ptdev->scheduler;
  2182. u64 delay_jiffies, now;
  2183. drm_WARN_ON(&ptdev->base, sched->resched_target != U64_MAX);
  2184. /* Scheduler tick was off, recalculate the resched_target based on the
  2185. * last tick event, and queue the scheduler work.
  2186. */
  2187. now = get_jiffies_64();
  2188. sched->resched_target = sched->last_tick + sched->tick_period;
  2189. if (sched->used_csg_slot_count == sched->csg_slot_count &&
  2190. time_before64(now, sched->resched_target))
  2191. delay_jiffies = min_t(unsigned long, sched->resched_target - now, ULONG_MAX);
  2192. else
  2193. delay_jiffies = 0;
  2194. sched_queue_delayed_work(sched, tick, delay_jiffies);
  2195. }
  2196. static void group_schedule_locked(struct panthor_group *group, u32 queue_mask)
  2197. {
  2198. struct panthor_device *ptdev = group->ptdev;
  2199. struct panthor_scheduler *sched = ptdev->scheduler;
  2200. struct list_head *queue = &sched->groups.runnable[group->priority];
  2201. bool was_idle;
  2202. if (!group_can_run(group))
  2203. return;
  2204. /* All updated queues are blocked, no need to wake up the scheduler. */
  2205. if ((queue_mask & group->blocked_queues) == queue_mask)
  2206. return;
  2207. was_idle = group_is_idle(group);
  2208. group->idle_queues &= ~queue_mask;
  2209. /* Don't mess up with the lists if we're in a middle of a reset. */
  2210. if (atomic_read(&sched->reset.in_progress))
  2211. return;
  2212. if (was_idle && !group_is_idle(group))
  2213. list_move_tail(&group->run_node, queue);
  2214. /* RT groups are preemptive. */
  2215. if (group->priority == PANTHOR_CSG_PRIORITY_RT) {
  2216. sched_queue_delayed_work(sched, tick, 0);
  2217. return;
  2218. }
  2219. /* Some groups might be idle, force an immediate tick to
  2220. * re-evaluate.
  2221. */
  2222. if (sched->might_have_idle_groups) {
  2223. sched_queue_delayed_work(sched, tick, 0);
  2224. return;
  2225. }
  2226. /* Scheduler is ticking, nothing to do. */
  2227. if (sched->resched_target != U64_MAX) {
  2228. /* If there are free slots, force immediating ticking. */
  2229. if (sched->used_csg_slot_count < sched->csg_slot_count)
  2230. sched_queue_delayed_work(sched, tick, 0);
  2231. return;
  2232. }
  2233. /* Scheduler tick was off, recalculate the resched_target based on the
  2234. * last tick event, and queue the scheduler work.
  2235. */
  2236. sched_resume_tick(ptdev);
  2237. }
  2238. static void queue_stop(struct panthor_queue *queue,
  2239. struct panthor_job *bad_job)
  2240. {
  2241. disable_delayed_work_sync(&queue->timeout.work);
  2242. drm_sched_stop(&queue->scheduler, bad_job ? &bad_job->base : NULL);
  2243. }
  2244. static void queue_start(struct panthor_queue *queue)
  2245. {
  2246. struct panthor_job *job;
  2247. /* Re-assign the parent fences. */
  2248. list_for_each_entry(job, &queue->scheduler.pending_list, base.list)
  2249. job->base.s_fence->parent = dma_fence_get(job->done_fence);
  2250. enable_delayed_work(&queue->timeout.work);
  2251. drm_sched_start(&queue->scheduler, 0);
  2252. }
  2253. static void panthor_group_stop(struct panthor_group *group)
  2254. {
  2255. struct panthor_scheduler *sched = group->ptdev->scheduler;
  2256. lockdep_assert_held(&sched->reset.lock);
  2257. for (u32 i = 0; i < group->queue_count; i++)
  2258. queue_stop(group->queues[i], NULL);
  2259. group_get(group);
  2260. list_move_tail(&group->run_node, &sched->reset.stopped_groups);
  2261. }
  2262. static void panthor_group_start(struct panthor_group *group)
  2263. {
  2264. struct panthor_scheduler *sched = group->ptdev->scheduler;
  2265. lockdep_assert_held(&group->ptdev->scheduler->reset.lock);
  2266. for (u32 i = 0; i < group->queue_count; i++)
  2267. queue_start(group->queues[i]);
  2268. if (group_can_run(group)) {
  2269. list_move_tail(&group->run_node,
  2270. group_is_idle(group) ?
  2271. &sched->groups.idle[group->priority] :
  2272. &sched->groups.runnable[group->priority]);
  2273. } else {
  2274. list_del_init(&group->run_node);
  2275. list_del_init(&group->wait_node);
  2276. group_queue_work(group, term);
  2277. }
  2278. group_put(group);
  2279. }
  2280. /**
  2281. * panthor_sched_report_mmu_fault() - Report MMU faults to the scheduler.
  2282. */
  2283. void panthor_sched_report_mmu_fault(struct panthor_device *ptdev)
  2284. {
  2285. /* Force a tick to immediately kill faulty groups. */
  2286. if (ptdev->scheduler)
  2287. sched_queue_delayed_work(ptdev->scheduler, tick, 0);
  2288. }
  2289. void panthor_sched_prepare_for_vm_destruction(struct panthor_device *ptdev)
  2290. {
  2291. /* FW can write out internal state, like the heap context, during CSG
  2292. * suspend. It is therefore important that the scheduler has fully
  2293. * evicted any pending and related groups before VM destruction can
  2294. * safely continue. Failure to do so can lead to GPU page faults.
  2295. * A controlled termination of a Panthor instance involves destroying
  2296. * the group(s) before the VM. This means any relevant group eviction
  2297. * has already been initiated by this point, and we just need to
  2298. * ensure that any pending tick_work() has been completed.
  2299. */
  2300. flush_work(&ptdev->scheduler->tick_work.work);
  2301. }
  2302. void panthor_sched_resume(struct panthor_device *ptdev)
  2303. {
  2304. /* Force a tick to re-evaluate after a resume. */
  2305. sched_queue_delayed_work(ptdev->scheduler, tick, 0);
  2306. }
  2307. void panthor_sched_suspend(struct panthor_device *ptdev)
  2308. {
  2309. struct panthor_scheduler *sched = ptdev->scheduler;
  2310. struct panthor_csg_slots_upd_ctx upd_ctx;
  2311. u32 suspended_slots;
  2312. u32 i;
  2313. mutex_lock(&sched->lock);
  2314. csgs_upd_ctx_init(&upd_ctx);
  2315. for (i = 0; i < sched->csg_slot_count; i++) {
  2316. struct panthor_csg_slot *csg_slot = &sched->csg_slots[i];
  2317. if (csg_slot->group) {
  2318. csgs_upd_ctx_queue_reqs(ptdev, &upd_ctx, i,
  2319. group_can_run(csg_slot->group) ?
  2320. CSG_STATE_SUSPEND : CSG_STATE_TERMINATE,
  2321. CSG_STATE_MASK);
  2322. }
  2323. }
  2324. suspended_slots = upd_ctx.update_mask;
  2325. csgs_upd_ctx_apply_locked(ptdev, &upd_ctx);
  2326. suspended_slots &= ~upd_ctx.timedout_mask;
  2327. if (upd_ctx.timedout_mask) {
  2328. u32 slot_mask = upd_ctx.timedout_mask;
  2329. drm_err(&ptdev->base, "CSG suspend failed, escalating to termination");
  2330. csgs_upd_ctx_init(&upd_ctx);
  2331. while (slot_mask) {
  2332. u32 csg_id = ffs(slot_mask) - 1;
  2333. struct panthor_csg_slot *csg_slot = &sched->csg_slots[csg_id];
  2334. /* If the group was still usable before that point, we consider
  2335. * it innocent.
  2336. */
  2337. if (group_can_run(csg_slot->group))
  2338. csg_slot->group->innocent = true;
  2339. /* We consider group suspension failures as fatal and flag the
  2340. * group as unusable by setting timedout=true.
  2341. */
  2342. csg_slot->group->timedout = true;
  2343. csgs_upd_ctx_queue_reqs(ptdev, &upd_ctx, csg_id,
  2344. CSG_STATE_TERMINATE,
  2345. CSG_STATE_MASK);
  2346. slot_mask &= ~BIT(csg_id);
  2347. }
  2348. csgs_upd_ctx_apply_locked(ptdev, &upd_ctx);
  2349. slot_mask = upd_ctx.timedout_mask;
  2350. while (slot_mask) {
  2351. u32 csg_id = ffs(slot_mask) - 1;
  2352. struct panthor_csg_slot *csg_slot = &sched->csg_slots[csg_id];
  2353. struct panthor_group *group = csg_slot->group;
  2354. /* Terminate command timedout, but the soft-reset will
  2355. * automatically terminate all active groups, so let's
  2356. * force the state to halted here.
  2357. */
  2358. if (group->state != PANTHOR_CS_GROUP_TERMINATED) {
  2359. group->state = PANTHOR_CS_GROUP_TERMINATED;
  2360. /* Reset the queue slots manually if the termination
  2361. * request failed.
  2362. */
  2363. for (i = 0; i < group->queue_count; i++) {
  2364. if (group->queues[i])
  2365. cs_slot_reset_locked(ptdev, csg_id, i);
  2366. }
  2367. }
  2368. slot_mask &= ~BIT(csg_id);
  2369. }
  2370. }
  2371. /* Flush L2 and LSC caches to make sure suspend state is up-to-date.
  2372. * If the flush fails, flag all queues for termination.
  2373. */
  2374. if (suspended_slots) {
  2375. bool flush_caches_failed = false;
  2376. u32 slot_mask = suspended_slots;
  2377. if (panthor_gpu_flush_caches(ptdev, CACHE_CLEAN, CACHE_CLEAN, 0))
  2378. flush_caches_failed = true;
  2379. while (slot_mask) {
  2380. u32 csg_id = ffs(slot_mask) - 1;
  2381. struct panthor_csg_slot *csg_slot = &sched->csg_slots[csg_id];
  2382. if (flush_caches_failed)
  2383. csg_slot->group->state = PANTHOR_CS_GROUP_TERMINATED;
  2384. else
  2385. csg_slot_sync_update_locked(ptdev, csg_id);
  2386. slot_mask &= ~BIT(csg_id);
  2387. }
  2388. }
  2389. for (i = 0; i < sched->csg_slot_count; i++) {
  2390. struct panthor_csg_slot *csg_slot = &sched->csg_slots[i];
  2391. struct panthor_group *group = csg_slot->group;
  2392. if (!group)
  2393. continue;
  2394. group_get(group);
  2395. if (group->csg_id >= 0)
  2396. sched_process_csg_irq_locked(ptdev, group->csg_id);
  2397. group_unbind_locked(group);
  2398. drm_WARN_ON(&group->ptdev->base, !list_empty(&group->run_node));
  2399. if (group_can_run(group)) {
  2400. list_add(&group->run_node,
  2401. &sched->groups.idle[group->priority]);
  2402. } else {
  2403. /* We don't bother stopping the scheduler if the group is
  2404. * faulty, the group termination work will finish the job.
  2405. */
  2406. list_del_init(&group->wait_node);
  2407. group_queue_work(group, term);
  2408. }
  2409. group_put(group);
  2410. }
  2411. mutex_unlock(&sched->lock);
  2412. }
  2413. void panthor_sched_pre_reset(struct panthor_device *ptdev)
  2414. {
  2415. struct panthor_scheduler *sched = ptdev->scheduler;
  2416. struct panthor_group *group, *group_tmp;
  2417. u32 i;
  2418. mutex_lock(&sched->reset.lock);
  2419. atomic_set(&sched->reset.in_progress, true);
  2420. /* Cancel all scheduler works. Once this is done, these works can't be
  2421. * scheduled again until the reset operation is complete.
  2422. */
  2423. cancel_work_sync(&sched->sync_upd_work);
  2424. cancel_delayed_work_sync(&sched->tick_work);
  2425. panthor_sched_suspend(ptdev);
  2426. /* Stop all groups that might still accept jobs, so we don't get passed
  2427. * new jobs while we're resetting.
  2428. */
  2429. for (i = 0; i < ARRAY_SIZE(sched->groups.runnable); i++) {
  2430. list_for_each_entry_safe(group, group_tmp, &sched->groups.runnable[i], run_node)
  2431. panthor_group_stop(group);
  2432. }
  2433. for (i = 0; i < ARRAY_SIZE(sched->groups.idle); i++) {
  2434. list_for_each_entry_safe(group, group_tmp, &sched->groups.idle[i], run_node)
  2435. panthor_group_stop(group);
  2436. }
  2437. mutex_unlock(&sched->reset.lock);
  2438. }
  2439. void panthor_sched_post_reset(struct panthor_device *ptdev, bool reset_failed)
  2440. {
  2441. struct panthor_scheduler *sched = ptdev->scheduler;
  2442. struct panthor_group *group, *group_tmp;
  2443. mutex_lock(&sched->reset.lock);
  2444. list_for_each_entry_safe(group, group_tmp, &sched->reset.stopped_groups, run_node) {
  2445. /* Consider all previously running group as terminated if the
  2446. * reset failed.
  2447. */
  2448. if (reset_failed)
  2449. group->state = PANTHOR_CS_GROUP_TERMINATED;
  2450. panthor_group_start(group);
  2451. }
  2452. /* We're done resetting the GPU, clear the reset.in_progress bit so we can
  2453. * kick the scheduler.
  2454. */
  2455. atomic_set(&sched->reset.in_progress, false);
  2456. mutex_unlock(&sched->reset.lock);
  2457. /* No need to queue a tick and update syncs if the reset failed. */
  2458. if (!reset_failed) {
  2459. sched_queue_delayed_work(sched, tick, 0);
  2460. sched_queue_work(sched, sync_upd);
  2461. }
  2462. }
  2463. static void update_fdinfo_stats(struct panthor_job *job)
  2464. {
  2465. struct panthor_group *group = job->group;
  2466. struct panthor_queue *queue = group->queues[job->queue_idx];
  2467. struct panthor_gpu_usage *fdinfo = &group->fdinfo.data;
  2468. struct panthor_job_profiling_data *slots = queue->profiling.slots->kmap;
  2469. struct panthor_job_profiling_data *data = &slots[job->profiling.slot];
  2470. scoped_guard(spinlock, &group->fdinfo.lock) {
  2471. if (job->profiling.mask & PANTHOR_DEVICE_PROFILING_CYCLES)
  2472. fdinfo->cycles += data->cycles.after - data->cycles.before;
  2473. if (job->profiling.mask & PANTHOR_DEVICE_PROFILING_TIMESTAMP)
  2474. fdinfo->time += data->time.after - data->time.before;
  2475. }
  2476. }
  2477. void panthor_fdinfo_gather_group_samples(struct panthor_file *pfile)
  2478. {
  2479. struct panthor_group_pool *gpool = pfile->groups;
  2480. struct panthor_group *group;
  2481. unsigned long i;
  2482. if (IS_ERR_OR_NULL(gpool))
  2483. return;
  2484. xa_lock(&gpool->xa);
  2485. xa_for_each_marked(&gpool->xa, i, group, GROUP_REGISTERED) {
  2486. guard(spinlock)(&group->fdinfo.lock);
  2487. pfile->stats.cycles += group->fdinfo.data.cycles;
  2488. pfile->stats.time += group->fdinfo.data.time;
  2489. group->fdinfo.data.cycles = 0;
  2490. group->fdinfo.data.time = 0;
  2491. }
  2492. xa_unlock(&gpool->xa);
  2493. }
  2494. static bool queue_check_job_completion(struct panthor_queue *queue)
  2495. {
  2496. struct panthor_syncobj_64b *syncobj = NULL;
  2497. struct panthor_job *job, *job_tmp;
  2498. bool cookie, progress = false;
  2499. LIST_HEAD(done_jobs);
  2500. cookie = dma_fence_begin_signalling();
  2501. spin_lock(&queue->fence_ctx.lock);
  2502. list_for_each_entry_safe(job, job_tmp, &queue->fence_ctx.in_flight_jobs, node) {
  2503. if (!syncobj) {
  2504. struct panthor_group *group = job->group;
  2505. syncobj = group->syncobjs->kmap +
  2506. (job->queue_idx * sizeof(*syncobj));
  2507. }
  2508. if (syncobj->seqno < job->done_fence->seqno)
  2509. break;
  2510. list_move_tail(&job->node, &done_jobs);
  2511. dma_fence_signal_locked(job->done_fence);
  2512. }
  2513. if (list_empty(&queue->fence_ctx.in_flight_jobs)) {
  2514. /* If we have no job left, we cancel the timer, and reset remaining
  2515. * time to its default so it can be restarted next time
  2516. * queue_resume_timeout() is called.
  2517. */
  2518. queue_suspend_timeout_locked(queue);
  2519. /* If there's no job pending, we consider it progress to avoid a
  2520. * spurious timeout if the timeout handler and the sync update
  2521. * handler raced.
  2522. */
  2523. progress = true;
  2524. } else if (!list_empty(&done_jobs)) {
  2525. queue_reset_timeout_locked(queue);
  2526. progress = true;
  2527. }
  2528. spin_unlock(&queue->fence_ctx.lock);
  2529. dma_fence_end_signalling(cookie);
  2530. list_for_each_entry_safe(job, job_tmp, &done_jobs, node) {
  2531. if (job->profiling.mask)
  2532. update_fdinfo_stats(job);
  2533. list_del_init(&job->node);
  2534. panthor_job_put(&job->base);
  2535. }
  2536. return progress;
  2537. }
  2538. static void group_sync_upd_work(struct work_struct *work)
  2539. {
  2540. struct panthor_group *group =
  2541. container_of(work, struct panthor_group, sync_upd_work);
  2542. u32 queue_idx;
  2543. bool cookie;
  2544. cookie = dma_fence_begin_signalling();
  2545. for (queue_idx = 0; queue_idx < group->queue_count; queue_idx++) {
  2546. struct panthor_queue *queue = group->queues[queue_idx];
  2547. if (!queue)
  2548. continue;
  2549. queue_check_job_completion(queue);
  2550. }
  2551. dma_fence_end_signalling(cookie);
  2552. group_put(group);
  2553. }
  2554. struct panthor_job_ringbuf_instrs {
  2555. u64 buffer[MAX_INSTRS_PER_JOB];
  2556. u32 count;
  2557. };
  2558. struct panthor_job_instr {
  2559. u32 profile_mask;
  2560. u64 instr;
  2561. };
  2562. #define JOB_INSTR(__prof, __instr) \
  2563. { \
  2564. .profile_mask = __prof, \
  2565. .instr = __instr, \
  2566. }
  2567. static void
  2568. copy_instrs_to_ringbuf(struct panthor_queue *queue,
  2569. struct panthor_job *job,
  2570. struct panthor_job_ringbuf_instrs *instrs)
  2571. {
  2572. u64 ringbuf_size = panthor_kernel_bo_size(queue->ringbuf);
  2573. u64 start = job->ringbuf.start & (ringbuf_size - 1);
  2574. u64 size, written;
  2575. /*
  2576. * We need to write a whole slot, including any trailing zeroes
  2577. * that may come at the end of it. Also, because instrs.buffer has
  2578. * been zero-initialised, there's no need to pad it with 0's
  2579. */
  2580. instrs->count = ALIGN(instrs->count, NUM_INSTRS_PER_CACHE_LINE);
  2581. size = instrs->count * sizeof(u64);
  2582. WARN_ON(size > ringbuf_size);
  2583. written = min(ringbuf_size - start, size);
  2584. memcpy(queue->ringbuf->kmap + start, instrs->buffer, written);
  2585. if (written < size)
  2586. memcpy(queue->ringbuf->kmap,
  2587. &instrs->buffer[written / sizeof(u64)],
  2588. size - written);
  2589. }
  2590. struct panthor_job_cs_params {
  2591. u32 profile_mask;
  2592. u64 addr_reg; u64 val_reg;
  2593. u64 cycle_reg; u64 time_reg;
  2594. u64 sync_addr; u64 times_addr;
  2595. u64 cs_start; u64 cs_size;
  2596. u32 last_flush; u32 waitall_mask;
  2597. };
  2598. static void
  2599. get_job_cs_params(struct panthor_job *job, struct panthor_job_cs_params *params)
  2600. {
  2601. struct panthor_group *group = job->group;
  2602. struct panthor_queue *queue = group->queues[job->queue_idx];
  2603. struct panthor_device *ptdev = group->ptdev;
  2604. struct panthor_scheduler *sched = ptdev->scheduler;
  2605. params->addr_reg = ptdev->csif_info.cs_reg_count -
  2606. ptdev->csif_info.unpreserved_cs_reg_count;
  2607. params->val_reg = params->addr_reg + 2;
  2608. params->cycle_reg = params->addr_reg;
  2609. params->time_reg = params->val_reg;
  2610. params->sync_addr = panthor_kernel_bo_gpuva(group->syncobjs) +
  2611. job->queue_idx * sizeof(struct panthor_syncobj_64b);
  2612. params->times_addr = panthor_kernel_bo_gpuva(queue->profiling.slots) +
  2613. (job->profiling.slot * sizeof(struct panthor_job_profiling_data));
  2614. params->waitall_mask = GENMASK(sched->sb_slot_count - 1, 0);
  2615. params->cs_start = job->call_info.start;
  2616. params->cs_size = job->call_info.size;
  2617. params->last_flush = job->call_info.latest_flush;
  2618. params->profile_mask = job->profiling.mask;
  2619. }
  2620. #define JOB_INSTR_ALWAYS(instr) \
  2621. JOB_INSTR(PANTHOR_DEVICE_PROFILING_DISABLED, (instr))
  2622. #define JOB_INSTR_TIMESTAMP(instr) \
  2623. JOB_INSTR(PANTHOR_DEVICE_PROFILING_TIMESTAMP, (instr))
  2624. #define JOB_INSTR_CYCLES(instr) \
  2625. JOB_INSTR(PANTHOR_DEVICE_PROFILING_CYCLES, (instr))
  2626. static void
  2627. prepare_job_instrs(const struct panthor_job_cs_params *params,
  2628. struct panthor_job_ringbuf_instrs *instrs)
  2629. {
  2630. const struct panthor_job_instr instr_seq[] = {
  2631. /* MOV32 rX+2, cs.latest_flush */
  2632. JOB_INSTR_ALWAYS((2ull << 56) | (params->val_reg << 48) | params->last_flush),
  2633. /* FLUSH_CACHE2.clean_inv_all.no_wait.signal(0) rX+2 */
  2634. JOB_INSTR_ALWAYS((36ull << 56) | (0ull << 48) | (params->val_reg << 40) |
  2635. (0 << 16) | 0x233),
  2636. /* MOV48 rX:rX+1, cycles_offset */
  2637. JOB_INSTR_CYCLES((1ull << 56) | (params->cycle_reg << 48) |
  2638. (params->times_addr +
  2639. offsetof(struct panthor_job_profiling_data, cycles.before))),
  2640. /* STORE_STATE cycles */
  2641. JOB_INSTR_CYCLES((40ull << 56) | (params->cycle_reg << 40) | (1ll << 32)),
  2642. /* MOV48 rX:rX+1, time_offset */
  2643. JOB_INSTR_TIMESTAMP((1ull << 56) | (params->time_reg << 48) |
  2644. (params->times_addr +
  2645. offsetof(struct panthor_job_profiling_data, time.before))),
  2646. /* STORE_STATE timer */
  2647. JOB_INSTR_TIMESTAMP((40ull << 56) | (params->time_reg << 40) | (0ll << 32)),
  2648. /* MOV48 rX:rX+1, cs.start */
  2649. JOB_INSTR_ALWAYS((1ull << 56) | (params->addr_reg << 48) | params->cs_start),
  2650. /* MOV32 rX+2, cs.size */
  2651. JOB_INSTR_ALWAYS((2ull << 56) | (params->val_reg << 48) | params->cs_size),
  2652. /* WAIT(0) => waits for FLUSH_CACHE2 instruction */
  2653. JOB_INSTR_ALWAYS((3ull << 56) | (1 << 16)),
  2654. /* CALL rX:rX+1, rX+2 */
  2655. JOB_INSTR_ALWAYS((32ull << 56) | (params->addr_reg << 40) |
  2656. (params->val_reg << 32)),
  2657. /* MOV48 rX:rX+1, cycles_offset */
  2658. JOB_INSTR_CYCLES((1ull << 56) | (params->cycle_reg << 48) |
  2659. (params->times_addr +
  2660. offsetof(struct panthor_job_profiling_data, cycles.after))),
  2661. /* STORE_STATE cycles */
  2662. JOB_INSTR_CYCLES((40ull << 56) | (params->cycle_reg << 40) | (1ll << 32)),
  2663. /* MOV48 rX:rX+1, time_offset */
  2664. JOB_INSTR_TIMESTAMP((1ull << 56) | (params->time_reg << 48) |
  2665. (params->times_addr +
  2666. offsetof(struct panthor_job_profiling_data, time.after))),
  2667. /* STORE_STATE timer */
  2668. JOB_INSTR_TIMESTAMP((40ull << 56) | (params->time_reg << 40) | (0ll << 32)),
  2669. /* MOV48 rX:rX+1, sync_addr */
  2670. JOB_INSTR_ALWAYS((1ull << 56) | (params->addr_reg << 48) | params->sync_addr),
  2671. /* MOV48 rX+2, #1 */
  2672. JOB_INSTR_ALWAYS((1ull << 56) | (params->val_reg << 48) | 1),
  2673. /* WAIT(all) */
  2674. JOB_INSTR_ALWAYS((3ull << 56) | (params->waitall_mask << 16)),
  2675. /* SYNC_ADD64.system_scope.propage_err.nowait rX:rX+1, rX+2*/
  2676. JOB_INSTR_ALWAYS((51ull << 56) | (0ull << 48) | (params->addr_reg << 40) |
  2677. (params->val_reg << 32) | (0 << 16) | 1),
  2678. /* ERROR_BARRIER, so we can recover from faults at job boundaries. */
  2679. JOB_INSTR_ALWAYS((47ull << 56)),
  2680. };
  2681. u32 pad;
  2682. instrs->count = 0;
  2683. /* NEED to be cacheline aligned to please the prefetcher. */
  2684. static_assert(sizeof(instrs->buffer) % 64 == 0,
  2685. "panthor_job_ringbuf_instrs::buffer is not aligned on a cacheline");
  2686. /* Make sure we have enough storage to store the whole sequence. */
  2687. static_assert(ALIGN(ARRAY_SIZE(instr_seq), NUM_INSTRS_PER_CACHE_LINE) ==
  2688. ARRAY_SIZE(instrs->buffer),
  2689. "instr_seq vs panthor_job_ringbuf_instrs::buffer size mismatch");
  2690. for (u32 i = 0; i < ARRAY_SIZE(instr_seq); i++) {
  2691. /* If the profile mask of this instruction is not enabled, skip it. */
  2692. if (instr_seq[i].profile_mask &&
  2693. !(instr_seq[i].profile_mask & params->profile_mask))
  2694. continue;
  2695. instrs->buffer[instrs->count++] = instr_seq[i].instr;
  2696. }
  2697. pad = ALIGN(instrs->count, NUM_INSTRS_PER_CACHE_LINE);
  2698. memset(&instrs->buffer[instrs->count], 0,
  2699. (pad - instrs->count) * sizeof(instrs->buffer[0]));
  2700. instrs->count = pad;
  2701. }
  2702. static u32 calc_job_credits(u32 profile_mask)
  2703. {
  2704. struct panthor_job_ringbuf_instrs instrs;
  2705. struct panthor_job_cs_params params = {
  2706. .profile_mask = profile_mask,
  2707. };
  2708. prepare_job_instrs(&params, &instrs);
  2709. return instrs.count;
  2710. }
  2711. static struct dma_fence *
  2712. queue_run_job(struct drm_sched_job *sched_job)
  2713. {
  2714. struct panthor_job *job = container_of(sched_job, struct panthor_job, base);
  2715. struct panthor_group *group = job->group;
  2716. struct panthor_queue *queue = group->queues[job->queue_idx];
  2717. struct panthor_device *ptdev = group->ptdev;
  2718. struct panthor_scheduler *sched = ptdev->scheduler;
  2719. struct panthor_job_ringbuf_instrs instrs;
  2720. struct panthor_job_cs_params cs_params;
  2721. struct dma_fence *done_fence;
  2722. int ret;
  2723. /* Stream size is zero, nothing to do except making sure all previously
  2724. * submitted jobs are done before we signal the
  2725. * drm_sched_job::s_fence::finished fence.
  2726. */
  2727. if (!job->call_info.size) {
  2728. job->done_fence = dma_fence_get(queue->fence_ctx.last_fence);
  2729. return dma_fence_get(job->done_fence);
  2730. }
  2731. ret = panthor_device_resume_and_get(ptdev);
  2732. if (drm_WARN_ON(&ptdev->base, ret))
  2733. return ERR_PTR(ret);
  2734. mutex_lock(&sched->lock);
  2735. if (!group_can_run(group)) {
  2736. done_fence = ERR_PTR(-ECANCELED);
  2737. goto out_unlock;
  2738. }
  2739. dma_fence_init(job->done_fence,
  2740. &panthor_queue_fence_ops,
  2741. &queue->fence_ctx.lock,
  2742. queue->fence_ctx.id,
  2743. atomic64_inc_return(&queue->fence_ctx.seqno));
  2744. job->profiling.slot = queue->profiling.seqno++;
  2745. if (queue->profiling.seqno == queue->profiling.slot_count)
  2746. queue->profiling.seqno = 0;
  2747. job->ringbuf.start = queue->iface.input->insert;
  2748. get_job_cs_params(job, &cs_params);
  2749. prepare_job_instrs(&cs_params, &instrs);
  2750. copy_instrs_to_ringbuf(queue, job, &instrs);
  2751. job->ringbuf.end = job->ringbuf.start + (instrs.count * sizeof(u64));
  2752. panthor_job_get(&job->base);
  2753. spin_lock(&queue->fence_ctx.lock);
  2754. list_add_tail(&job->node, &queue->fence_ctx.in_flight_jobs);
  2755. spin_unlock(&queue->fence_ctx.lock);
  2756. /* Make sure the ring buffer is updated before the INSERT
  2757. * register.
  2758. */
  2759. wmb();
  2760. queue->iface.input->extract = queue->iface.output->extract;
  2761. queue->iface.input->insert = job->ringbuf.end;
  2762. if (group->csg_id < 0) {
  2763. group_schedule_locked(group, BIT(job->queue_idx));
  2764. } else {
  2765. u32 queue_mask = BIT(job->queue_idx);
  2766. bool resume_tick = group_is_idle(group) &&
  2767. (group->idle_queues & queue_mask) &&
  2768. !(group->blocked_queues & queue_mask) &&
  2769. sched->resched_target == U64_MAX;
  2770. /* We just added something to the queue, so it's no longer idle. */
  2771. group->idle_queues &= ~queue_mask;
  2772. if (resume_tick)
  2773. sched_resume_tick(ptdev);
  2774. gpu_write(ptdev, CSF_DOORBELL(queue->doorbell_id), 1);
  2775. if (!sched->pm.has_ref &&
  2776. !(group->blocked_queues & BIT(job->queue_idx))) {
  2777. pm_runtime_get(ptdev->base.dev);
  2778. sched->pm.has_ref = true;
  2779. }
  2780. queue_resume_timeout(queue);
  2781. panthor_devfreq_record_busy(sched->ptdev);
  2782. }
  2783. /* Update the last fence. */
  2784. dma_fence_put(queue->fence_ctx.last_fence);
  2785. queue->fence_ctx.last_fence = dma_fence_get(job->done_fence);
  2786. done_fence = dma_fence_get(job->done_fence);
  2787. out_unlock:
  2788. mutex_unlock(&sched->lock);
  2789. pm_runtime_mark_last_busy(ptdev->base.dev);
  2790. pm_runtime_put_autosuspend(ptdev->base.dev);
  2791. return done_fence;
  2792. }
  2793. static enum drm_gpu_sched_stat
  2794. queue_timedout_job(struct drm_sched_job *sched_job)
  2795. {
  2796. struct panthor_job *job = container_of(sched_job, struct panthor_job, base);
  2797. struct panthor_group *group = job->group;
  2798. struct panthor_device *ptdev = group->ptdev;
  2799. struct panthor_scheduler *sched = ptdev->scheduler;
  2800. struct panthor_queue *queue = group->queues[job->queue_idx];
  2801. drm_warn(&ptdev->base, "job timeout: pid=%d, comm=%s, seqno=%llu\n",
  2802. group->task_info.pid, group->task_info.comm, job->done_fence->seqno);
  2803. drm_WARN_ON(&ptdev->base, atomic_read(&sched->reset.in_progress));
  2804. queue_stop(queue, job);
  2805. mutex_lock(&sched->lock);
  2806. group->timedout = true;
  2807. if (group->csg_id >= 0) {
  2808. sched_queue_delayed_work(ptdev->scheduler, tick, 0);
  2809. } else {
  2810. /* Remove from the run queues, so the scheduler can't
  2811. * pick the group on the next tick.
  2812. */
  2813. list_del_init(&group->run_node);
  2814. list_del_init(&group->wait_node);
  2815. group_queue_work(group, term);
  2816. }
  2817. mutex_unlock(&sched->lock);
  2818. queue_start(queue);
  2819. return DRM_GPU_SCHED_STAT_RESET;
  2820. }
  2821. static void queue_free_job(struct drm_sched_job *sched_job)
  2822. {
  2823. drm_sched_job_cleanup(sched_job);
  2824. panthor_job_put(sched_job);
  2825. }
  2826. static const struct drm_sched_backend_ops panthor_queue_sched_ops = {
  2827. .run_job = queue_run_job,
  2828. .timedout_job = queue_timedout_job,
  2829. .free_job = queue_free_job,
  2830. };
  2831. static u32 calc_profiling_ringbuf_num_slots(struct panthor_device *ptdev,
  2832. u32 cs_ringbuf_size)
  2833. {
  2834. u32 min_profiled_job_instrs = U32_MAX;
  2835. u32 last_flag = fls(PANTHOR_DEVICE_PROFILING_ALL);
  2836. /*
  2837. * We want to calculate the minimum size of a profiled job's CS,
  2838. * because since they need additional instructions for the sampling
  2839. * of performance metrics, they might take up further slots in
  2840. * the queue's ringbuffer. This means we might not need as many job
  2841. * slots for keeping track of their profiling information. What we
  2842. * need is the maximum number of slots we should allocate to this end,
  2843. * which matches the maximum number of profiled jobs we can place
  2844. * simultaneously in the queue's ring buffer.
  2845. * That has to be calculated separately for every single job profiling
  2846. * flag, but not in the case job profiling is disabled, since unprofiled
  2847. * jobs don't need to keep track of this at all.
  2848. */
  2849. for (u32 i = 0; i < last_flag; i++) {
  2850. min_profiled_job_instrs =
  2851. min(min_profiled_job_instrs, calc_job_credits(BIT(i)));
  2852. }
  2853. return DIV_ROUND_UP(cs_ringbuf_size, min_profiled_job_instrs * sizeof(u64));
  2854. }
  2855. static void queue_timeout_work(struct work_struct *work)
  2856. {
  2857. struct panthor_queue *queue = container_of(work, struct panthor_queue,
  2858. timeout.work.work);
  2859. bool progress;
  2860. progress = queue_check_job_completion(queue);
  2861. if (!progress)
  2862. drm_sched_fault(&queue->scheduler);
  2863. }
  2864. static struct panthor_queue *
  2865. group_create_queue(struct panthor_group *group,
  2866. const struct drm_panthor_queue_create *args,
  2867. u64 drm_client_id, u32 gid, u32 qid)
  2868. {
  2869. struct drm_sched_init_args sched_args = {
  2870. .ops = &panthor_queue_sched_ops,
  2871. .submit_wq = group->ptdev->scheduler->wq,
  2872. .num_rqs = 1,
  2873. /*
  2874. * The credit limit argument tells us the total number of
  2875. * instructions across all CS slots in the ringbuffer, with
  2876. * some jobs requiring twice as many as others, depending on
  2877. * their profiling status.
  2878. */
  2879. .credit_limit = args->ringbuf_size / sizeof(u64),
  2880. .timeout = MAX_SCHEDULE_TIMEOUT,
  2881. .timeout_wq = group->ptdev->reset.wq,
  2882. .dev = group->ptdev->base.dev,
  2883. };
  2884. struct drm_gpu_scheduler *drm_sched;
  2885. struct panthor_queue *queue;
  2886. int ret;
  2887. if (args->pad[0] || args->pad[1] || args->pad[2])
  2888. return ERR_PTR(-EINVAL);
  2889. if (args->ringbuf_size < SZ_4K || args->ringbuf_size > SZ_64K ||
  2890. !is_power_of_2(args->ringbuf_size))
  2891. return ERR_PTR(-EINVAL);
  2892. if (args->priority > CSF_MAX_QUEUE_PRIO)
  2893. return ERR_PTR(-EINVAL);
  2894. queue = kzalloc_obj(*queue);
  2895. if (!queue)
  2896. return ERR_PTR(-ENOMEM);
  2897. queue->timeout.remaining = msecs_to_jiffies(JOB_TIMEOUT_MS);
  2898. INIT_DELAYED_WORK(&queue->timeout.work, queue_timeout_work);
  2899. queue->fence_ctx.id = dma_fence_context_alloc(1);
  2900. spin_lock_init(&queue->fence_ctx.lock);
  2901. INIT_LIST_HEAD(&queue->fence_ctx.in_flight_jobs);
  2902. queue->priority = args->priority;
  2903. queue->ringbuf = panthor_kernel_bo_create(group->ptdev, group->vm,
  2904. args->ringbuf_size,
  2905. DRM_PANTHOR_BO_NO_MMAP,
  2906. DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC |
  2907. DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED,
  2908. PANTHOR_VM_KERNEL_AUTO_VA,
  2909. "CS ring buffer");
  2910. if (IS_ERR(queue->ringbuf)) {
  2911. ret = PTR_ERR(queue->ringbuf);
  2912. goto err_free_queue;
  2913. }
  2914. ret = panthor_kernel_bo_vmap(queue->ringbuf);
  2915. if (ret)
  2916. goto err_free_queue;
  2917. queue->iface.mem = panthor_fw_alloc_queue_iface_mem(group->ptdev,
  2918. &queue->iface.input,
  2919. &queue->iface.output,
  2920. &queue->iface.input_fw_va,
  2921. &queue->iface.output_fw_va);
  2922. if (IS_ERR(queue->iface.mem)) {
  2923. ret = PTR_ERR(queue->iface.mem);
  2924. goto err_free_queue;
  2925. }
  2926. queue->profiling.slot_count =
  2927. calc_profiling_ringbuf_num_slots(group->ptdev, args->ringbuf_size);
  2928. queue->profiling.slots =
  2929. panthor_kernel_bo_create(group->ptdev, group->vm,
  2930. queue->profiling.slot_count *
  2931. sizeof(struct panthor_job_profiling_data),
  2932. DRM_PANTHOR_BO_NO_MMAP,
  2933. DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC |
  2934. DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED,
  2935. PANTHOR_VM_KERNEL_AUTO_VA,
  2936. "Group job stats");
  2937. if (IS_ERR(queue->profiling.slots)) {
  2938. ret = PTR_ERR(queue->profiling.slots);
  2939. goto err_free_queue;
  2940. }
  2941. ret = panthor_kernel_bo_vmap(queue->profiling.slots);
  2942. if (ret)
  2943. goto err_free_queue;
  2944. /* assign a unique name */
  2945. queue->name = kasprintf(GFP_KERNEL, "panthor-queue-%llu-%u-%u", drm_client_id, gid, qid);
  2946. if (!queue->name) {
  2947. ret = -ENOMEM;
  2948. goto err_free_queue;
  2949. }
  2950. sched_args.name = queue->name;
  2951. ret = drm_sched_init(&queue->scheduler, &sched_args);
  2952. if (ret)
  2953. goto err_free_queue;
  2954. drm_sched = &queue->scheduler;
  2955. ret = drm_sched_entity_init(&queue->entity, 0, &drm_sched, 1, NULL);
  2956. if (ret)
  2957. goto err_free_queue;
  2958. return queue;
  2959. err_free_queue:
  2960. group_free_queue(group, queue);
  2961. return ERR_PTR(ret);
  2962. }
  2963. static void group_init_task_info(struct panthor_group *group)
  2964. {
  2965. struct task_struct *task = current->group_leader;
  2966. group->task_info.pid = task->pid;
  2967. get_task_comm(group->task_info.comm, task);
  2968. }
  2969. static void add_group_kbo_sizes(struct panthor_device *ptdev,
  2970. struct panthor_group *group)
  2971. {
  2972. struct panthor_queue *queue;
  2973. int i;
  2974. if (drm_WARN_ON(&ptdev->base, IS_ERR_OR_NULL(group)))
  2975. return;
  2976. if (drm_WARN_ON(&ptdev->base, ptdev != group->ptdev))
  2977. return;
  2978. group->fdinfo.kbo_sizes += group->suspend_buf->obj->size;
  2979. group->fdinfo.kbo_sizes += group->protm_suspend_buf->obj->size;
  2980. group->fdinfo.kbo_sizes += group->syncobjs->obj->size;
  2981. for (i = 0; i < group->queue_count; i++) {
  2982. queue = group->queues[i];
  2983. group->fdinfo.kbo_sizes += queue->ringbuf->obj->size;
  2984. group->fdinfo.kbo_sizes += queue->iface.mem->obj->size;
  2985. group->fdinfo.kbo_sizes += queue->profiling.slots->obj->size;
  2986. }
  2987. }
  2988. #define MAX_GROUPS_PER_POOL 128
  2989. int panthor_group_create(struct panthor_file *pfile,
  2990. const struct drm_panthor_group_create *group_args,
  2991. const struct drm_panthor_queue_create *queue_args,
  2992. u64 drm_client_id)
  2993. {
  2994. struct panthor_device *ptdev = pfile->ptdev;
  2995. struct panthor_group_pool *gpool = pfile->groups;
  2996. struct panthor_scheduler *sched = ptdev->scheduler;
  2997. struct panthor_fw_csg_iface *csg_iface = panthor_fw_get_csg_iface(ptdev, 0);
  2998. struct panthor_group *group = NULL;
  2999. u32 gid, i, suspend_size;
  3000. int ret;
  3001. if (group_args->pad)
  3002. return -EINVAL;
  3003. if (group_args->priority >= PANTHOR_CSG_PRIORITY_COUNT)
  3004. return -EINVAL;
  3005. if ((group_args->compute_core_mask & ~ptdev->gpu_info.shader_present) ||
  3006. (group_args->fragment_core_mask & ~ptdev->gpu_info.shader_present) ||
  3007. (group_args->tiler_core_mask & ~ptdev->gpu_info.tiler_present))
  3008. return -EINVAL;
  3009. if (hweight64(group_args->compute_core_mask) < group_args->max_compute_cores ||
  3010. hweight64(group_args->fragment_core_mask) < group_args->max_fragment_cores ||
  3011. hweight64(group_args->tiler_core_mask) < group_args->max_tiler_cores)
  3012. return -EINVAL;
  3013. group = kzalloc_obj(*group);
  3014. if (!group)
  3015. return -ENOMEM;
  3016. spin_lock_init(&group->fatal_lock);
  3017. kref_init(&group->refcount);
  3018. group->state = PANTHOR_CS_GROUP_CREATED;
  3019. group->csg_id = -1;
  3020. group->ptdev = ptdev;
  3021. group->max_compute_cores = group_args->max_compute_cores;
  3022. group->compute_core_mask = group_args->compute_core_mask;
  3023. group->max_fragment_cores = group_args->max_fragment_cores;
  3024. group->fragment_core_mask = group_args->fragment_core_mask;
  3025. group->max_tiler_cores = group_args->max_tiler_cores;
  3026. group->tiler_core_mask = group_args->tiler_core_mask;
  3027. group->priority = group_args->priority;
  3028. INIT_LIST_HEAD(&group->wait_node);
  3029. INIT_LIST_HEAD(&group->run_node);
  3030. INIT_WORK(&group->term_work, group_term_work);
  3031. INIT_WORK(&group->sync_upd_work, group_sync_upd_work);
  3032. INIT_WORK(&group->tiler_oom_work, group_tiler_oom_work);
  3033. INIT_WORK(&group->release_work, group_release_work);
  3034. group->vm = panthor_vm_pool_get_vm(pfile->vms, group_args->vm_id);
  3035. if (!group->vm) {
  3036. ret = -EINVAL;
  3037. goto err_put_group;
  3038. }
  3039. suspend_size = csg_iface->control->suspend_size;
  3040. group->suspend_buf = panthor_fw_alloc_suspend_buf_mem(ptdev, suspend_size);
  3041. if (IS_ERR(group->suspend_buf)) {
  3042. ret = PTR_ERR(group->suspend_buf);
  3043. group->suspend_buf = NULL;
  3044. goto err_put_group;
  3045. }
  3046. suspend_size = csg_iface->control->protm_suspend_size;
  3047. group->protm_suspend_buf = panthor_fw_alloc_suspend_buf_mem(ptdev, suspend_size);
  3048. if (IS_ERR(group->protm_suspend_buf)) {
  3049. ret = PTR_ERR(group->protm_suspend_buf);
  3050. group->protm_suspend_buf = NULL;
  3051. goto err_put_group;
  3052. }
  3053. group->syncobjs = panthor_kernel_bo_create(ptdev, group->vm,
  3054. group_args->queues.count *
  3055. sizeof(struct panthor_syncobj_64b),
  3056. DRM_PANTHOR_BO_NO_MMAP,
  3057. DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC |
  3058. DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED,
  3059. PANTHOR_VM_KERNEL_AUTO_VA,
  3060. "Group sync objects");
  3061. if (IS_ERR(group->syncobjs)) {
  3062. ret = PTR_ERR(group->syncobjs);
  3063. goto err_put_group;
  3064. }
  3065. ret = panthor_kernel_bo_vmap(group->syncobjs);
  3066. if (ret)
  3067. goto err_put_group;
  3068. memset(group->syncobjs->kmap, 0,
  3069. group_args->queues.count * sizeof(struct panthor_syncobj_64b));
  3070. ret = xa_alloc(&gpool->xa, &gid, group, XA_LIMIT(1, MAX_GROUPS_PER_POOL), GFP_KERNEL);
  3071. if (ret)
  3072. goto err_put_group;
  3073. for (i = 0; i < group_args->queues.count; i++) {
  3074. group->queues[i] = group_create_queue(group, &queue_args[i], drm_client_id, gid, i);
  3075. if (IS_ERR(group->queues[i])) {
  3076. ret = PTR_ERR(group->queues[i]);
  3077. group->queues[i] = NULL;
  3078. goto err_erase_gid;
  3079. }
  3080. group->queue_count++;
  3081. }
  3082. group->idle_queues = GENMASK(group->queue_count - 1, 0);
  3083. mutex_lock(&sched->reset.lock);
  3084. if (atomic_read(&sched->reset.in_progress)) {
  3085. panthor_group_stop(group);
  3086. } else {
  3087. mutex_lock(&sched->lock);
  3088. list_add_tail(&group->run_node,
  3089. &sched->groups.idle[group->priority]);
  3090. mutex_unlock(&sched->lock);
  3091. }
  3092. mutex_unlock(&sched->reset.lock);
  3093. add_group_kbo_sizes(group->ptdev, group);
  3094. spin_lock_init(&group->fdinfo.lock);
  3095. group_init_task_info(group);
  3096. xa_set_mark(&gpool->xa, gid, GROUP_REGISTERED);
  3097. return gid;
  3098. err_erase_gid:
  3099. xa_erase(&gpool->xa, gid);
  3100. err_put_group:
  3101. group_put(group);
  3102. return ret;
  3103. }
  3104. int panthor_group_destroy(struct panthor_file *pfile, u32 group_handle)
  3105. {
  3106. struct panthor_group_pool *gpool = pfile->groups;
  3107. struct panthor_device *ptdev = pfile->ptdev;
  3108. struct panthor_scheduler *sched = ptdev->scheduler;
  3109. struct panthor_group *group;
  3110. if (!xa_get_mark(&gpool->xa, group_handle, GROUP_REGISTERED))
  3111. return -EINVAL;
  3112. group = xa_erase(&gpool->xa, group_handle);
  3113. if (!group)
  3114. return -EINVAL;
  3115. mutex_lock(&sched->reset.lock);
  3116. mutex_lock(&sched->lock);
  3117. group->destroyed = true;
  3118. if (group->csg_id >= 0) {
  3119. sched_queue_delayed_work(sched, tick, 0);
  3120. } else if (!atomic_read(&sched->reset.in_progress)) {
  3121. /* Remove from the run queues, so the scheduler can't
  3122. * pick the group on the next tick.
  3123. */
  3124. list_del_init(&group->run_node);
  3125. list_del_init(&group->wait_node);
  3126. group_queue_work(group, term);
  3127. }
  3128. mutex_unlock(&sched->lock);
  3129. mutex_unlock(&sched->reset.lock);
  3130. group_put(group);
  3131. return 0;
  3132. }
  3133. static struct panthor_group *group_from_handle(struct panthor_group_pool *pool,
  3134. unsigned long group_handle)
  3135. {
  3136. struct panthor_group *group;
  3137. xa_lock(&pool->xa);
  3138. group = group_get(xa_find(&pool->xa, &group_handle, group_handle, GROUP_REGISTERED));
  3139. xa_unlock(&pool->xa);
  3140. return group;
  3141. }
  3142. int panthor_group_get_state(struct panthor_file *pfile,
  3143. struct drm_panthor_group_get_state *get_state)
  3144. {
  3145. struct panthor_group_pool *gpool = pfile->groups;
  3146. struct panthor_device *ptdev = pfile->ptdev;
  3147. struct panthor_scheduler *sched = ptdev->scheduler;
  3148. struct panthor_group *group;
  3149. if (get_state->pad)
  3150. return -EINVAL;
  3151. group = group_from_handle(gpool, get_state->group_handle);
  3152. if (!group)
  3153. return -EINVAL;
  3154. memset(get_state, 0, sizeof(*get_state));
  3155. mutex_lock(&sched->lock);
  3156. if (group->timedout)
  3157. get_state->state |= DRM_PANTHOR_GROUP_STATE_TIMEDOUT;
  3158. if (group->fatal_queues) {
  3159. get_state->state |= DRM_PANTHOR_GROUP_STATE_FATAL_FAULT;
  3160. get_state->fatal_queues = group->fatal_queues;
  3161. }
  3162. if (group->innocent)
  3163. get_state->state |= DRM_PANTHOR_GROUP_STATE_INNOCENT;
  3164. mutex_unlock(&sched->lock);
  3165. group_put(group);
  3166. return 0;
  3167. }
  3168. int panthor_group_pool_create(struct panthor_file *pfile)
  3169. {
  3170. struct panthor_group_pool *gpool;
  3171. gpool = kzalloc_obj(*gpool);
  3172. if (!gpool)
  3173. return -ENOMEM;
  3174. xa_init_flags(&gpool->xa, XA_FLAGS_ALLOC1);
  3175. pfile->groups = gpool;
  3176. return 0;
  3177. }
  3178. void panthor_group_pool_destroy(struct panthor_file *pfile)
  3179. {
  3180. struct panthor_group_pool *gpool = pfile->groups;
  3181. struct panthor_group *group;
  3182. unsigned long i;
  3183. if (IS_ERR_OR_NULL(gpool))
  3184. return;
  3185. xa_for_each(&gpool->xa, i, group)
  3186. panthor_group_destroy(pfile, i);
  3187. xa_destroy(&gpool->xa);
  3188. kfree(gpool);
  3189. pfile->groups = NULL;
  3190. }
  3191. /**
  3192. * panthor_fdinfo_gather_group_mem_info() - Retrieve aggregate size of all private kernel BO's
  3193. * belonging to all the groups owned by an open Panthor file
  3194. * @pfile: File.
  3195. * @stats: Memory statistics to be updated.
  3196. *
  3197. */
  3198. void
  3199. panthor_fdinfo_gather_group_mem_info(struct panthor_file *pfile,
  3200. struct drm_memory_stats *stats)
  3201. {
  3202. struct panthor_group_pool *gpool = pfile->groups;
  3203. struct panthor_group *group;
  3204. unsigned long i;
  3205. if (IS_ERR_OR_NULL(gpool))
  3206. return;
  3207. xa_lock(&gpool->xa);
  3208. xa_for_each_marked(&gpool->xa, i, group, GROUP_REGISTERED) {
  3209. stats->resident += group->fdinfo.kbo_sizes;
  3210. if (group->csg_id >= 0)
  3211. stats->active += group->fdinfo.kbo_sizes;
  3212. }
  3213. xa_unlock(&gpool->xa);
  3214. }
  3215. static void job_release(struct kref *ref)
  3216. {
  3217. struct panthor_job *job = container_of(ref, struct panthor_job, refcount);
  3218. drm_WARN_ON(&job->group->ptdev->base, !list_empty(&job->node));
  3219. if (job->base.s_fence)
  3220. drm_sched_job_cleanup(&job->base);
  3221. if (job->done_fence && job->done_fence->ops)
  3222. dma_fence_put(job->done_fence);
  3223. else
  3224. dma_fence_free(job->done_fence);
  3225. group_put(job->group);
  3226. kfree(job);
  3227. }
  3228. struct drm_sched_job *panthor_job_get(struct drm_sched_job *sched_job)
  3229. {
  3230. if (sched_job) {
  3231. struct panthor_job *job = container_of(sched_job, struct panthor_job, base);
  3232. kref_get(&job->refcount);
  3233. }
  3234. return sched_job;
  3235. }
  3236. void panthor_job_put(struct drm_sched_job *sched_job)
  3237. {
  3238. struct panthor_job *job = container_of(sched_job, struct panthor_job, base);
  3239. if (sched_job)
  3240. kref_put(&job->refcount, job_release);
  3241. }
  3242. struct panthor_vm *panthor_job_vm(struct drm_sched_job *sched_job)
  3243. {
  3244. struct panthor_job *job = container_of(sched_job, struct panthor_job, base);
  3245. return job->group->vm;
  3246. }
  3247. struct drm_sched_job *
  3248. panthor_job_create(struct panthor_file *pfile,
  3249. u16 group_handle,
  3250. const struct drm_panthor_queue_submit *qsubmit,
  3251. u64 drm_client_id)
  3252. {
  3253. struct panthor_group_pool *gpool = pfile->groups;
  3254. struct panthor_job *job;
  3255. u32 credits;
  3256. int ret;
  3257. if (qsubmit->pad)
  3258. return ERR_PTR(-EINVAL);
  3259. /* If stream_addr is zero, so stream_size should be. */
  3260. if ((qsubmit->stream_size == 0) != (qsubmit->stream_addr == 0))
  3261. return ERR_PTR(-EINVAL);
  3262. /* Make sure the address is aligned on 64-byte (cacheline) and the size is
  3263. * aligned on 8-byte (instruction size).
  3264. */
  3265. if ((qsubmit->stream_addr & 63) || (qsubmit->stream_size & 7))
  3266. return ERR_PTR(-EINVAL);
  3267. /* bits 24:30 must be zero. */
  3268. if (qsubmit->latest_flush & GENMASK(30, 24))
  3269. return ERR_PTR(-EINVAL);
  3270. job = kzalloc_obj(*job);
  3271. if (!job)
  3272. return ERR_PTR(-ENOMEM);
  3273. kref_init(&job->refcount);
  3274. job->queue_idx = qsubmit->queue_index;
  3275. job->call_info.size = qsubmit->stream_size;
  3276. job->call_info.start = qsubmit->stream_addr;
  3277. job->call_info.latest_flush = qsubmit->latest_flush;
  3278. INIT_LIST_HEAD(&job->node);
  3279. job->group = group_from_handle(gpool, group_handle);
  3280. if (!job->group) {
  3281. ret = -EINVAL;
  3282. goto err_put_job;
  3283. }
  3284. if (!group_can_run(job->group)) {
  3285. ret = -EINVAL;
  3286. goto err_put_job;
  3287. }
  3288. if (job->queue_idx >= job->group->queue_count ||
  3289. !job->group->queues[job->queue_idx]) {
  3290. ret = -EINVAL;
  3291. goto err_put_job;
  3292. }
  3293. /* Empty command streams don't need a fence, they'll pick the one from
  3294. * the previously submitted job.
  3295. */
  3296. if (job->call_info.size) {
  3297. job->done_fence = kzalloc_obj(*job->done_fence);
  3298. if (!job->done_fence) {
  3299. ret = -ENOMEM;
  3300. goto err_put_job;
  3301. }
  3302. }
  3303. job->profiling.mask = pfile->ptdev->profile_mask;
  3304. credits = calc_job_credits(job->profiling.mask);
  3305. if (credits == 0) {
  3306. ret = -EINVAL;
  3307. goto err_put_job;
  3308. }
  3309. ret = drm_sched_job_init(&job->base,
  3310. &job->group->queues[job->queue_idx]->entity,
  3311. credits, job->group, drm_client_id);
  3312. if (ret)
  3313. goto err_put_job;
  3314. return &job->base;
  3315. err_put_job:
  3316. panthor_job_put(&job->base);
  3317. return ERR_PTR(ret);
  3318. }
  3319. void panthor_job_update_resvs(struct drm_exec *exec, struct drm_sched_job *sched_job)
  3320. {
  3321. struct panthor_job *job = container_of(sched_job, struct panthor_job, base);
  3322. panthor_vm_update_resvs(job->group->vm, exec, &sched_job->s_fence->finished,
  3323. DMA_RESV_USAGE_BOOKKEEP, DMA_RESV_USAGE_BOOKKEEP);
  3324. }
  3325. void panthor_sched_unplug(struct panthor_device *ptdev)
  3326. {
  3327. struct panthor_scheduler *sched = ptdev->scheduler;
  3328. disable_delayed_work_sync(&sched->tick_work);
  3329. disable_work_sync(&sched->fw_events_work);
  3330. disable_work_sync(&sched->sync_upd_work);
  3331. mutex_lock(&sched->lock);
  3332. if (sched->pm.has_ref) {
  3333. pm_runtime_put(ptdev->base.dev);
  3334. sched->pm.has_ref = false;
  3335. }
  3336. mutex_unlock(&sched->lock);
  3337. }
  3338. static void panthor_sched_fini(struct drm_device *ddev, void *res)
  3339. {
  3340. struct panthor_scheduler *sched = res;
  3341. int prio;
  3342. if (!sched || !sched->csg_slot_count)
  3343. return;
  3344. if (sched->wq)
  3345. destroy_workqueue(sched->wq);
  3346. if (sched->heap_alloc_wq)
  3347. destroy_workqueue(sched->heap_alloc_wq);
  3348. for (prio = PANTHOR_CSG_PRIORITY_COUNT - 1; prio >= 0; prio--) {
  3349. drm_WARN_ON(ddev, !list_empty(&sched->groups.runnable[prio]));
  3350. drm_WARN_ON(ddev, !list_empty(&sched->groups.idle[prio]));
  3351. }
  3352. drm_WARN_ON(ddev, !list_empty(&sched->groups.waiting));
  3353. }
  3354. int panthor_sched_init(struct panthor_device *ptdev)
  3355. {
  3356. struct panthor_fw_global_iface *glb_iface = panthor_fw_get_glb_iface(ptdev);
  3357. struct panthor_fw_csg_iface *csg_iface = panthor_fw_get_csg_iface(ptdev, 0);
  3358. struct panthor_fw_cs_iface *cs_iface = panthor_fw_get_cs_iface(ptdev, 0, 0);
  3359. struct panthor_scheduler *sched;
  3360. u32 gpu_as_count, num_groups;
  3361. int prio, ret;
  3362. sched = drmm_kzalloc(&ptdev->base, sizeof(*sched), GFP_KERNEL);
  3363. if (!sched)
  3364. return -ENOMEM;
  3365. /* The highest bit in JOB_INT_* is reserved for globabl IRQs. That
  3366. * leaves 31 bits for CSG IRQs, hence the MAX_CSGS clamp here.
  3367. */
  3368. num_groups = min_t(u32, MAX_CSGS, glb_iface->control->group_num);
  3369. /* The FW-side scheduler might deadlock if two groups with the same
  3370. * priority try to access a set of resources that overlaps, with part
  3371. * of the resources being allocated to one group and the other part to
  3372. * the other group, both groups waiting for the remaining resources to
  3373. * be allocated. To avoid that, it is recommended to assign each CSG a
  3374. * different priority. In theory we could allow several groups to have
  3375. * the same CSG priority if they don't request the same resources, but
  3376. * that makes the scheduling logic more complicated, so let's clamp
  3377. * the number of CSG slots to MAX_CSG_PRIO + 1 for now.
  3378. */
  3379. num_groups = min_t(u32, MAX_CSG_PRIO + 1, num_groups);
  3380. /* We need at least one AS for the MCU and one for the GPU contexts. */
  3381. gpu_as_count = hweight32(ptdev->gpu_info.as_present & GENMASK(31, 1));
  3382. if (!gpu_as_count) {
  3383. drm_err(&ptdev->base, "Not enough AS (%d, expected at least 2)",
  3384. gpu_as_count + 1);
  3385. return -EINVAL;
  3386. }
  3387. sched->ptdev = ptdev;
  3388. sched->sb_slot_count = CS_FEATURES_SCOREBOARDS(cs_iface->control->features);
  3389. sched->csg_slot_count = num_groups;
  3390. sched->cs_slot_count = csg_iface->control->stream_num;
  3391. sched->as_slot_count = gpu_as_count;
  3392. ptdev->csif_info.csg_slot_count = sched->csg_slot_count;
  3393. ptdev->csif_info.cs_slot_count = sched->cs_slot_count;
  3394. ptdev->csif_info.scoreboard_slot_count = sched->sb_slot_count;
  3395. sched->last_tick = 0;
  3396. sched->resched_target = U64_MAX;
  3397. sched->tick_period = msecs_to_jiffies(10);
  3398. INIT_DELAYED_WORK(&sched->tick_work, tick_work);
  3399. INIT_WORK(&sched->sync_upd_work, sync_upd_work);
  3400. INIT_WORK(&sched->fw_events_work, process_fw_events_work);
  3401. ret = drmm_mutex_init(&ptdev->base, &sched->lock);
  3402. if (ret)
  3403. return ret;
  3404. for (prio = PANTHOR_CSG_PRIORITY_COUNT - 1; prio >= 0; prio--) {
  3405. INIT_LIST_HEAD(&sched->groups.runnable[prio]);
  3406. INIT_LIST_HEAD(&sched->groups.idle[prio]);
  3407. }
  3408. INIT_LIST_HEAD(&sched->groups.waiting);
  3409. ret = drmm_mutex_init(&ptdev->base, &sched->reset.lock);
  3410. if (ret)
  3411. return ret;
  3412. INIT_LIST_HEAD(&sched->reset.stopped_groups);
  3413. /* sched->heap_alloc_wq will be used for heap chunk allocation on
  3414. * tiler OOM events, which means we can't use the same workqueue for
  3415. * the scheduler because works queued by the scheduler are in
  3416. * the dma-signalling path. Allocate a dedicated heap_alloc_wq to
  3417. * work around this limitation.
  3418. *
  3419. * FIXME: Ultimately, what we need is a failable/non-blocking GEM
  3420. * allocation path that we can call when a heap OOM is reported. The
  3421. * FW is smart enough to fall back on other methods if the kernel can't
  3422. * allocate memory, and fail the tiling job if none of these
  3423. * countermeasures worked.
  3424. *
  3425. * Set WQ_MEM_RECLAIM on sched->wq to unblock the situation when the
  3426. * system is running out of memory.
  3427. */
  3428. sched->heap_alloc_wq = alloc_workqueue("panthor-heap-alloc", WQ_UNBOUND, 0);
  3429. sched->wq = alloc_workqueue("panthor-csf-sched", WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
  3430. if (!sched->wq || !sched->heap_alloc_wq) {
  3431. panthor_sched_fini(&ptdev->base, sched);
  3432. drm_err(&ptdev->base, "Failed to allocate the workqueues");
  3433. return -ENOMEM;
  3434. }
  3435. ret = drmm_add_action_or_reset(&ptdev->base, panthor_sched_fini, sched);
  3436. if (ret)
  3437. return ret;
  3438. ptdev->scheduler = sched;
  3439. return 0;
  3440. }