panthor_sched.c 108 KB

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