mali_kbase_js.c 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238
  1. /*
  2. *
  3. * (C) COPYRIGHT 2011-2016 ARM Limited. All rights reserved.
  4. *
  5. * This program is free software and is provided to you under the terms of the
  6. * GNU General Public License version 2 as published by the Free Software
  7. * Foundation, and any use by you of this program is subject to the terms
  8. * of such GNU licence.
  9. *
  10. * A copy of the licence is included with the program, and can also be obtained
  11. * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  12. * Boston, MA 02110-1301, USA.
  13. *
  14. */
  15. /*
  16. * Job Scheduler Implementation
  17. */
  18. #include <mali_kbase.h>
  19. #include <mali_kbase_js.h>
  20. #if defined(CONFIG_MALI_GATOR_SUPPORT)
  21. #include <mali_kbase_gator.h>
  22. #endif
  23. #include <mali_kbase_tlstream.h>
  24. #include <mali_kbase_hw.h>
  25. #include <mali_kbase_defs.h>
  26. #include <mali_kbase_config_defaults.h>
  27. #include "mali_kbase_jm.h"
  28. #include "mali_kbase_hwaccess_jm.h"
  29. /*
  30. * Private types
  31. */
  32. /* Bitpattern indicating the result of releasing a context */
  33. enum {
  34. /* The context was descheduled - caller should try scheduling in a new
  35. * one to keep the runpool full */
  36. KBASEP_JS_RELEASE_RESULT_WAS_DESCHEDULED = (1u << 0),
  37. /* Ctx attributes were changed - caller should try scheduling all
  38. * contexts */
  39. KBASEP_JS_RELEASE_RESULT_SCHED_ALL = (1u << 1)
  40. };
  41. typedef u32 kbasep_js_release_result;
  42. const int kbasep_js_atom_priority_to_relative[BASE_JD_NR_PRIO_LEVELS] = {
  43. KBASE_JS_ATOM_SCHED_PRIO_MED, /* BASE_JD_PRIO_MEDIUM */
  44. KBASE_JS_ATOM_SCHED_PRIO_HIGH, /* BASE_JD_PRIO_HIGH */
  45. KBASE_JS_ATOM_SCHED_PRIO_LOW /* BASE_JD_PRIO_LOW */
  46. };
  47. const base_jd_prio
  48. kbasep_js_relative_priority_to_atom[KBASE_JS_ATOM_SCHED_PRIO_COUNT] = {
  49. BASE_JD_PRIO_HIGH, /* KBASE_JS_ATOM_SCHED_PRIO_HIGH */
  50. BASE_JD_PRIO_MEDIUM, /* KBASE_JS_ATOM_SCHED_PRIO_MED */
  51. BASE_JD_PRIO_LOW /* KBASE_JS_ATOM_SCHED_PRIO_LOW */
  52. };
  53. /*
  54. * Private function prototypes
  55. */
  56. static kbasep_js_release_result kbasep_js_runpool_release_ctx_internal(
  57. struct kbase_device *kbdev, struct kbase_context *kctx,
  58. struct kbasep_js_atom_retained_state *katom_retained_state);
  59. static int kbase_js_get_slot(struct kbase_device *kbdev,
  60. struct kbase_jd_atom *katom);
  61. static void kbase_js_foreach_ctx_job(struct kbase_context *kctx,
  62. kbasep_js_policy_ctx_job_cb callback);
  63. /* Helper for trace subcodes */
  64. #if KBASE_TRACE_ENABLE
  65. static int kbasep_js_trace_get_refcnt(struct kbase_device *kbdev,
  66. struct kbase_context *kctx)
  67. {
  68. unsigned long flags;
  69. struct kbasep_js_device_data *js_devdata;
  70. int as_nr;
  71. int refcnt = 0;
  72. js_devdata = &kbdev->js_data;
  73. spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
  74. as_nr = kctx->as_nr;
  75. if (as_nr != KBASEP_AS_NR_INVALID) {
  76. struct kbasep_js_per_as_data *js_per_as_data;
  77. js_per_as_data = &js_devdata->runpool_irq.per_as_data[as_nr];
  78. refcnt = js_per_as_data->as_busy_refcount;
  79. }
  80. spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
  81. return refcnt;
  82. }
  83. static int kbasep_js_trace_get_refcnt_nolock(struct kbase_device *kbdev,
  84. struct kbase_context *kctx)
  85. {
  86. struct kbasep_js_device_data *js_devdata;
  87. int as_nr;
  88. int refcnt = 0;
  89. js_devdata = &kbdev->js_data;
  90. as_nr = kctx->as_nr;
  91. if (as_nr != KBASEP_AS_NR_INVALID) {
  92. struct kbasep_js_per_as_data *js_per_as_data;
  93. js_per_as_data = &js_devdata->runpool_irq.per_as_data[as_nr];
  94. refcnt = js_per_as_data->as_busy_refcount;
  95. }
  96. return refcnt;
  97. }
  98. #else /* KBASE_TRACE_ENABLE */
  99. static int kbasep_js_trace_get_refcnt(struct kbase_device *kbdev,
  100. struct kbase_context *kctx)
  101. {
  102. CSTD_UNUSED(kbdev);
  103. CSTD_UNUSED(kctx);
  104. return 0;
  105. }
  106. static int kbasep_js_trace_get_refcnt_nolock(struct kbase_device *kbdev,
  107. struct kbase_context *kctx)
  108. {
  109. CSTD_UNUSED(kbdev);
  110. CSTD_UNUSED(kctx);
  111. return 0;
  112. }
  113. #endif /* KBASE_TRACE_ENABLE */
  114. /*
  115. * Private types
  116. */
  117. enum {
  118. JS_DEVDATA_INIT_NONE = 0,
  119. JS_DEVDATA_INIT_CONSTANTS = (1 << 0),
  120. JS_DEVDATA_INIT_POLICY = (1 << 1),
  121. JS_DEVDATA_INIT_ALL = ((1 << 2) - 1)
  122. };
  123. enum {
  124. JS_KCTX_INIT_NONE = 0,
  125. JS_KCTX_INIT_CONSTANTS = (1 << 0),
  126. JS_KCTX_INIT_POLICY = (1 << 1),
  127. JS_KCTX_INIT_ALL = ((1 << 2) - 1)
  128. };
  129. /*
  130. * Private functions
  131. */
  132. /**
  133. * core_reqs_from_jsn_features - Convert JSn_FEATURES to core requirements
  134. * @features: JSn_FEATURE register value
  135. *
  136. * Given a JSn_FEATURE register value returns the core requirements that match
  137. *
  138. * Return: Core requirement bit mask
  139. */
  140. static base_jd_core_req core_reqs_from_jsn_features(u16 features)
  141. {
  142. base_jd_core_req core_req = 0u;
  143. if ((features & JS_FEATURE_SET_VALUE_JOB) != 0)
  144. core_req |= BASE_JD_REQ_V;
  145. if ((features & JS_FEATURE_CACHE_FLUSH_JOB) != 0)
  146. core_req |= BASE_JD_REQ_CF;
  147. if ((features & JS_FEATURE_COMPUTE_JOB) != 0)
  148. core_req |= BASE_JD_REQ_CS;
  149. if ((features & JS_FEATURE_TILER_JOB) != 0)
  150. core_req |= BASE_JD_REQ_T;
  151. if ((features & JS_FEATURE_FRAGMENT_JOB) != 0)
  152. core_req |= BASE_JD_REQ_FS;
  153. return core_req;
  154. }
  155. static void kbase_js_sync_timers(struct kbase_device *kbdev)
  156. {
  157. mutex_lock(&kbdev->js_data.runpool_mutex);
  158. kbase_backend_ctx_count_changed(kbdev);
  159. mutex_unlock(&kbdev->js_data.runpool_mutex);
  160. }
  161. /* Hold the kbasep_js_device_data::runpool_irq::lock for this */
  162. bool kbasep_js_runpool_retain_ctx_nolock(struct kbase_device *kbdev,
  163. struct kbase_context *kctx)
  164. {
  165. struct kbasep_js_device_data *js_devdata;
  166. struct kbasep_js_per_as_data *js_per_as_data;
  167. bool result = false;
  168. int as_nr;
  169. KBASE_DEBUG_ASSERT(kbdev != NULL);
  170. KBASE_DEBUG_ASSERT(kctx != NULL);
  171. js_devdata = &kbdev->js_data;
  172. as_nr = kctx->as_nr;
  173. if (as_nr != KBASEP_AS_NR_INVALID) {
  174. int new_refcnt;
  175. KBASE_DEBUG_ASSERT(as_nr >= 0);
  176. js_per_as_data = &js_devdata->runpool_irq.per_as_data[as_nr];
  177. KBASE_DEBUG_ASSERT(js_per_as_data->kctx != NULL);
  178. new_refcnt = ++(js_per_as_data->as_busy_refcount);
  179. KBASE_TRACE_ADD_REFCOUNT(kbdev, JS_RETAIN_CTX_NOLOCK, kctx,
  180. NULL, 0u, new_refcnt);
  181. result = true;
  182. }
  183. return result;
  184. }
  185. /* Helper macros to access and modify jsctx_queue.indicies */
  186. #define JSCTX_GET(offset, var, mask) \
  187. ((var >> offset) & mask)
  188. /* This wraps around to correct integer size automatically. */
  189. #define JSCTX_SET(var, offset, value, mask) \
  190. (var = ((var & ~(mask << offset)) /*Clear old bits */ \
  191. | (((value) & mask) << offset))) /* Set (after masking) new bits */
  192. #define JSCTX_GET_WR_IDX(var) \
  193. JSCTX_GET(JSCTX_WR_OFFSET, var, JSCTX_RB_MASK_STORE)
  194. #define JSCTX_GET_RN_IDX(var) \
  195. JSCTX_GET(JSCTX_RN_OFFSET, var, JSCTX_RB_MASK_STORE)
  196. #define JSCTX_GET_RD_IDX(var) \
  197. JSCTX_GET(JSCTX_RD_OFFSET, var, JSCTX_RB_MASK_STORE)
  198. #define JSCTX_GET_IDX_DIFF(lower, upper) \
  199. ((upper >= lower) ? (upper - lower) : (upper+JSCTX_RB_SIZE_STORE-lower))
  200. #define JSCTX_SET_WR_IDX(var, value) \
  201. JSCTX_SET(var, JSCTX_WR_OFFSET, value, JSCTX_RB_MASK_STORE)
  202. #define JSCTX_SET_RN_IDX(var, value) \
  203. JSCTX_SET(var, JSCTX_RN_OFFSET, value, JSCTX_RB_MASK_STORE)
  204. #define JSCTX_SET_RD_IDX(var, value) \
  205. JSCTX_SET(var, JSCTX_RD_OFFSET, value, JSCTX_RB_MASK_STORE)
  206. /**
  207. * jsctx_rb_none_to_pull_prio(): - Check if there are no pullable atoms
  208. * @kctx: Pointer to kbase context with ring buffer.
  209. * @js: Job slot id to check.
  210. * @prio: Priority to check.
  211. *
  212. * Return true if there are no atoms to pull. There may be running atoms in the
  213. * ring buffer even if there are no atoms to pull. It is also possible for the
  214. * ring buffer to be full (with running atoms) when this functions returns
  215. * true.
  216. *
  217. * Return: true if there are no atoms to pull, false otherwise.
  218. */
  219. static inline bool
  220. jsctx_rb_none_to_pull_prio(struct kbase_context *kctx, int js, int prio)
  221. {
  222. struct jsctx_queue *rb = &kctx->jsctx_queue[prio][js];
  223. unsigned int var = atomic_read(&rb->indicies);
  224. return JSCTX_GET_RD_IDX(var) == JSCTX_GET_WR_IDX(var);
  225. }
  226. /**
  227. * jsctx_rb_none_to_pull(): - Check if all priority ring buffers have no
  228. * pullable atoms
  229. * @kctx: Pointer to kbase context with ring buffer.
  230. * @js: Job slot id to check.
  231. *
  232. * Caller must hold runpool_irq.lock
  233. *
  234. * Return: true if the ring buffers for all priorities have no pullable atoms,
  235. * false otherwise.
  236. */
  237. static inline bool
  238. jsctx_rb_none_to_pull(struct kbase_context *kctx, int js)
  239. {
  240. int prio;
  241. lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
  242. for (prio = 0; prio < KBASE_JS_ATOM_SCHED_PRIO_COUNT; prio++) {
  243. if (!jsctx_rb_none_to_pull_prio(kctx, js, prio))
  244. return false;
  245. }
  246. return true;
  247. }
  248. /**
  249. * jsctx_rb_is_full(): - Check if the given ringbuffer is full.
  250. * @queue: Pointer to the queue containing the ringbuffer.
  251. *
  252. * No locks explicitly required, result will always be consistent.
  253. * But depending on usage, the caller should consider jctx.lock,
  254. * for the result to remain correct.
  255. *
  256. * Return: true if the ringbuffer is full, false otherwise.
  257. */
  258. static inline bool
  259. jsctx_rb_is_full(struct jsctx_queue *queue)
  260. {
  261. unsigned int var = atomic_read(&queue->indicies);
  262. u16 rn_idx = JSCTX_GET_RN_IDX(var);
  263. u16 wr_idx = JSCTX_GET_WR_IDX(var);
  264. return JSCTX_GET_IDX_DIFF(rn_idx, wr_idx) >= JSCTX_RB_SIZE;
  265. }
  266. /**
  267. * jsctx_queue_foreach_prio(): - Execute callback for each entry in the queue.
  268. * @kctx: Pointer to kbase context with the queue.
  269. * @js: Job slot id to iterate.
  270. * @prio: Priority id to iterate.
  271. * @callback: Function pointer to callback.
  272. *
  273. * Iterate over a ring buffer and invoke @callback for each entry in buffer, and
  274. * remove the entry from the buffer.
  275. *
  276. * If entries are added to the ring buffer while this is running those entries
  277. * may, or may not be covered. To ensure that all entries in the buffer have
  278. * been enumerated when this function returns jsctx->lock must be held when
  279. * calling this function.
  280. *
  281. * The HW access lock, js_data.runpool_irq.lock, must always be held when
  282. * calling this function.
  283. */
  284. static void
  285. jsctx_queue_foreach_prio(struct kbase_context *kctx, int js, int prio,
  286. kbasep_js_policy_ctx_job_cb callback)
  287. {
  288. struct jsctx_queue *queue = &kctx->jsctx_queue[prio][js];
  289. struct kbase_jd_atom *katom;
  290. struct list_head *pos, *q;
  291. unsigned int var = atomic_read(&queue->indicies);
  292. u16 running_idx = JSCTX_GET_RN_IDX(var);
  293. u16 read_idx = JSCTX_GET_RD_IDX(var);
  294. u16 wr_idx = JSCTX_GET_WR_IDX(var);
  295. u16 i;
  296. const u16 count = JSCTX_GET_IDX_DIFF(running_idx, wr_idx);
  297. lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
  298. /* There must be no jobs currently in HW access */
  299. WARN_ON(read_idx != JSCTX_GET_RN_IDX(var));
  300. /* Invoke callback on all kbase_jd_atoms in the ring buffer, and
  301. * removes them from the buffer */
  302. for (i = 0; i < count; i++) {
  303. int id = queue->entries[read_idx & JSCTX_RB_MASK].atom_id;
  304. katom = kbase_jd_atom_from_id(kctx, id);
  305. read_idx++;
  306. callback(kctx->kbdev, katom);
  307. }
  308. atomic_set(&queue->indicies, 0);
  309. list_for_each_safe(pos, q, &queue->queue_head) {
  310. struct kbase_jd_atom *entry;
  311. entry = list_entry(pos, struct kbase_jd_atom, queue);
  312. list_del(pos);
  313. callback(kctx->kbdev, entry);
  314. }
  315. }
  316. /**
  317. * jsctx_queue_foreach(): - Execute callback for each entry in every queue
  318. * @kctx: Pointer to kbase context with queue.
  319. * @js: Job slot id to iterate.
  320. * @callback: Function pointer to callback.
  321. *
  322. * Iterate over all the different priorities, and for each call
  323. * jsctx_queue_foreach_prio() to iterate over the queue and invoke @callback
  324. * for each entry, and remove the entry from the queue.
  325. */
  326. static inline void
  327. jsctx_queue_foreach(struct kbase_context *kctx, int js,
  328. kbasep_js_policy_ctx_job_cb callback)
  329. {
  330. int prio;
  331. for (prio = 0; prio < KBASE_JS_ATOM_SCHED_PRIO_COUNT; prio++)
  332. jsctx_queue_foreach_prio(kctx, js, prio, callback);
  333. }
  334. /**
  335. * jsctx_rb_peek_prio(): - Check buffer and get next atom
  336. * @kctx: Pointer to kbase context with ring buffer.
  337. * @js: Job slot id to check.
  338. * @prio: Priority id to check.
  339. *
  340. * Check the ring buffer for the specified @js and @prio and return a pointer to
  341. * the next atom, unless the ring buffer is empty.
  342. *
  343. * Return: Pointer to next atom in buffer, or NULL if there is no atom.
  344. */
  345. static inline struct kbase_jd_atom *
  346. jsctx_rb_peek_prio(struct kbase_context *kctx, int js, int prio)
  347. {
  348. struct jsctx_queue *rb = &kctx->jsctx_queue[prio][js];
  349. int id;
  350. unsigned int var = atomic_read(&rb->indicies);
  351. lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
  352. if (JSCTX_GET_RD_IDX(var) == JSCTX_GET_WR_IDX(var))
  353. return NULL;
  354. id = rb->entries[JSCTX_GET_RD_IDX(var) & JSCTX_RB_MASK].atom_id;
  355. return kbase_jd_atom_from_id(kctx, id);
  356. }
  357. /**
  358. * jsctx_rb_peek(): - Check all priority buffers and get next atom
  359. * @kctx: Pointer to kbase context with ring buffer.
  360. * @js: Job slot id to check.
  361. *
  362. * Check the ring buffers for all priorities, starting from
  363. * KBASE_JS_ATOM_SCHED_PRIO_HIGH, for the specified @js and @prio and return a
  364. * pointer to the next atom, unless all the priority's ring buffers are empty.
  365. *
  366. * Caller must hold the runpool_irq.lock.
  367. *
  368. * Return: Pointer to next atom in buffer, or NULL if there is no atom.
  369. */
  370. static inline struct kbase_jd_atom *
  371. jsctx_rb_peek(struct kbase_context *kctx, int js)
  372. {
  373. int prio;
  374. lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
  375. for (prio = 0; prio < KBASE_JS_ATOM_SCHED_PRIO_COUNT; prio++) {
  376. struct kbase_jd_atom *katom;
  377. katom = jsctx_rb_peek_prio(kctx, js, prio);
  378. if (katom)
  379. return katom;
  380. }
  381. return NULL;
  382. }
  383. /**
  384. * jsctx_rb_peek_last(): - Check a ring buffer and get the last atom
  385. * @kctx: Pointer to kbase context with ring buffer.
  386. * @js: Job slot id to check.
  387. * @prio: Priority id to check.
  388. *
  389. * Check the ring buffer for the specified @js and @prio and return a
  390. * pointer to the last atom, unless all the priority's ring buffers are empty.
  391. *
  392. * The last atom is the atom that was added using jsctx_rb_add() most recently.
  393. *
  394. * Return: Pointer to last atom in buffer, or NULL if there is no atom.
  395. */
  396. static inline struct kbase_jd_atom *
  397. jsctx_rb_peek_last(struct kbase_context *kctx, int js, int prio)
  398. {
  399. struct jsctx_queue *rb = &kctx->jsctx_queue[prio][js];
  400. unsigned int var = atomic_read(&rb->indicies);
  401. int id;
  402. lockdep_assert_held(&kctx->jctx.lock);
  403. if (!list_empty(&rb->queue_head)) {
  404. return list_entry(rb->queue_head.prev,
  405. struct kbase_jd_atom, queue);
  406. }
  407. if (JSCTX_GET_RN_IDX(var) == JSCTX_GET_WR_IDX(var))
  408. return NULL;
  409. id = rb->entries[(JSCTX_GET_WR_IDX(var) - 1) & JSCTX_RB_MASK].atom_id;
  410. return kbase_jd_atom_from_id(kctx, id);
  411. }
  412. /**
  413. * jsctx_rb_pull(): - Mark atom in list as running
  414. * @kctx: Pointer to kbase context with ring buffer.
  415. * @katom: Pointer to katom to pull.
  416. *
  417. * Mark an atom previously obtained from jsctx_rb_peek() as running.
  418. *
  419. * @katom must currently be at the head of the ring buffer.
  420. */
  421. static inline void
  422. jsctx_rb_pull(struct kbase_context *kctx, struct kbase_jd_atom *katom)
  423. {
  424. int prio = katom->sched_priority;
  425. int js = katom->slot_nr;
  426. unsigned int oldvar, var;
  427. struct jsctx_queue *rb = &kctx->jsctx_queue[prio][js];
  428. lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
  429. /* Atoms must be pulled in the correct order. */
  430. WARN_ON(katom != jsctx_rb_peek_prio(kctx, js, prio));
  431. do {
  432. u16 rd_idx;
  433. oldvar = atomic_read(&rb->indicies);
  434. var = oldvar;
  435. rd_idx = JSCTX_GET_RD_IDX(var);
  436. JSCTX_SET_RD_IDX(var, rd_idx+1);
  437. } while (atomic_cmpxchg(&rb->indicies, oldvar, var) != oldvar);
  438. }
  439. /**
  440. * jsctx_rb_unpull(): - Undo marking of atom in list as running
  441. * @kctx: Pointer to kbase context with ring buffer.
  442. * @katom: Pointer to katom to unpull.
  443. *
  444. * Undo jsctx_rb_pull() and put @katom back in the queue.
  445. *
  446. * jsctx_rb_unpull() must be called on atoms in the same order the atoms were
  447. * pulled.
  448. */
  449. static inline void
  450. jsctx_rb_unpull(struct kbase_context *kctx, struct kbase_jd_atom *katom)
  451. {
  452. int prio = katom->sched_priority;
  453. int js = katom->slot_nr;
  454. unsigned int oldvar, var;
  455. struct jsctx_queue *rb = &kctx->jsctx_queue[prio][js];
  456. lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
  457. do {
  458. u16 rd_idx;
  459. oldvar = atomic_read(&rb->indicies);
  460. var = oldvar;
  461. rd_idx = JSCTX_GET_RD_IDX(var)-1;
  462. /* Atoms must be unpulled in correct order. */
  463. WARN_ON(rb->entries[rd_idx & JSCTX_RB_MASK].atom_id !=
  464. kbase_jd_atom_id(kctx, katom));
  465. JSCTX_SET_RD_IDX(var, rd_idx);
  466. } while (atomic_cmpxchg(&rb->indicies, oldvar, var) != oldvar);
  467. }
  468. /**
  469. * jsctx_rb_add(): - Add atom to ring buffer
  470. * @kctx: Pointer to kbase context with ring buffer.
  471. * @katom: Pointer to katom to add.
  472. *
  473. * Add @katom to the ring buffer determined by the atom's priority and job slot
  474. * number.
  475. *
  476. * If the ring buffer is full -EBUSY will be returned.
  477. *
  478. * Return: On success 0 is returned, on failure a negative error code.
  479. */
  480. static int
  481. jsctx_rb_add_atom(struct kbase_context *kctx, struct kbase_jd_atom *katom)
  482. {
  483. int prio = katom->sched_priority;
  484. int js = katom->slot_nr;
  485. struct jsctx_queue *rb = &kctx->jsctx_queue[prio][js];
  486. unsigned int oldvar, var;
  487. u16 wr_idx, running_idx, count;
  488. lockdep_assert_held(&kctx->jctx.lock);
  489. oldvar = atomic_read(&rb->indicies);
  490. var = oldvar;
  491. running_idx = JSCTX_GET_RN_IDX(var);
  492. wr_idx = JSCTX_GET_WR_IDX(var);
  493. count = JSCTX_GET_IDX_DIFF(running_idx, wr_idx);
  494. /* Check if the ring buffer is full */
  495. if (count >= JSCTX_RB_SIZE)
  496. return -EBUSY;
  497. rb->entries[wr_idx & JSCTX_RB_MASK].atom_id =
  498. kbase_jd_atom_id(kctx, katom);
  499. wr_idx++;
  500. JSCTX_SET_WR_IDX(var, wr_idx);
  501. while (atomic_cmpxchg(&rb->indicies, oldvar, var) != oldvar) {
  502. oldvar = atomic_read(&rb->indicies);
  503. var = oldvar;
  504. wr_idx = JSCTX_GET_WR_IDX(var)+1;
  505. JSCTX_SET_WR_IDX(var, wr_idx);
  506. }
  507. return 0;
  508. }
  509. /**
  510. * jsctx_rb_remove(): - Remove atom from ring buffer
  511. * @kctx: Pointer to kbase context with ring buffer.
  512. * @katom: Pointer to katom to remove.
  513. *
  514. * Remove @katom from the ring buffer.
  515. *
  516. * @katom must have been pulled from the buffer earlier by jsctx_rb_pull(), and
  517. * atoms must be removed in the same order they were pulled from the ring
  518. * buffer.
  519. */
  520. static inline void
  521. jsctx_rb_remove(struct kbase_context *kctx, struct kbase_jd_atom *katom)
  522. {
  523. int prio = katom->sched_priority;
  524. int js = katom->slot_nr;
  525. unsigned int oldvar, var;
  526. struct jsctx_queue *rb = &kctx->jsctx_queue[prio][js];
  527. lockdep_assert_held(&kctx->jctx.lock);
  528. do {
  529. unsigned int rn_idx;
  530. oldvar = atomic_read(&rb->indicies);
  531. var = oldvar;
  532. rn_idx = JSCTX_GET_RN_IDX(var);
  533. JSCTX_SET_RN_IDX(var, rn_idx+1);
  534. } while (atomic_cmpxchg(&rb->indicies, oldvar, var) != oldvar);
  535. }
  536. static void
  537. jsctx_ll_add(struct kbase_context *kctx, struct kbase_jd_atom *katom)
  538. {
  539. int prio = katom->sched_priority;
  540. int js = katom->slot_nr;
  541. struct jsctx_queue *queue = &kctx->jsctx_queue[prio][js];
  542. lockdep_assert_held(&kctx->jctx.lock);
  543. list_add_tail(&katom->queue, &queue->queue_head);
  544. }
  545. static bool kbase_js_ctx_pullable(struct kbase_context *kctx,
  546. int js,
  547. bool is_scheduled);
  548. static bool kbase_js_ctx_list_add_pullable(struct kbase_device *kbdev,
  549. struct kbase_context *kctx,
  550. int js);
  551. static bool kbase_js_ctx_list_add_unpullable(struct kbase_device *kbdev,
  552. struct kbase_context *kctx,
  553. int js);
  554. void
  555. jsctx_ll_flush_to_rb(struct kbase_context *kctx, int prio, int js)
  556. {
  557. unsigned long flags;
  558. struct list_head *pos, *q;
  559. struct jsctx_queue *queue = &kctx->jsctx_queue[prio][js];
  560. bool flushed_any = false;
  561. struct kbasep_js_device_data *js_devdata = &kctx->kbdev->js_data;
  562. bool enqueue_required = false;
  563. lockdep_assert_held(&kctx->jctx.lock);
  564. /* Early out for common case */
  565. if (list_empty(&queue->queue_head) || jsctx_rb_is_full(queue))
  566. return;
  567. mutex_lock(&js_devdata->queue_mutex);
  568. mutex_lock(&kctx->jctx.sched_info.ctx.jsctx_mutex);
  569. mutex_lock(&js_devdata->runpool_mutex);
  570. spin_lock_irqsave(&kctx->kbdev->js_data.runpool_irq.lock, flags);
  571. /* If slot will transition from unpullable to pullable then add to
  572. * pullable list */
  573. if (jsctx_rb_none_to_pull(kctx, js))
  574. enqueue_required = true;
  575. else
  576. enqueue_required = false;
  577. list_for_each_safe(pos, q, &queue->queue_head) {
  578. struct kbase_jd_atom *katom;
  579. katom = list_entry(pos, struct kbase_jd_atom, queue);
  580. KBASE_DEBUG_ASSERT(katom);
  581. if (jsctx_rb_add_atom(kctx, katom))
  582. break;
  583. katom->atom_flags &= ~KBASE_KATOM_FLAG_JSCTX_IN_LL;
  584. katom->atom_flags |= KBASE_KATOM_FLAG_JSCTX_RB_SUBMITTED;
  585. flushed_any = true;
  586. list_del(pos);
  587. }
  588. if (flushed_any) {
  589. bool timer_sync = false;
  590. if (enqueue_required) {
  591. if (kbase_js_ctx_pullable(kctx, js, false))
  592. timer_sync = kbase_js_ctx_list_add_pullable(
  593. kctx->kbdev, kctx, js);
  594. else
  595. timer_sync = kbase_js_ctx_list_add_unpullable(
  596. kctx->kbdev, kctx, js);
  597. /* If this context is active and the atom is the first
  598. * on its slot, kick the job manager to attempt to
  599. * fast-start the atom */
  600. if (kctx == kctx->kbdev->hwaccess.active_kctx)
  601. kbase_jm_try_kick(kctx->kbdev, 1 << js);
  602. spin_unlock_irqrestore(&js_devdata->runpool_irq.lock,
  603. flags);
  604. if (timer_sync)
  605. kbase_backend_ctx_count_changed(kctx->kbdev);
  606. } else {
  607. spin_unlock_irqrestore(&js_devdata->runpool_irq.lock,
  608. flags);
  609. }
  610. } else {
  611. spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
  612. }
  613. mutex_unlock(&js_devdata->runpool_mutex);
  614. mutex_unlock(&kctx->jctx.sched_info.ctx.jsctx_mutex);
  615. mutex_unlock(&js_devdata->queue_mutex);
  616. }
  617. /*
  618. * Functions private to KBase ('Protected' functions)
  619. */
  620. int kbasep_js_devdata_init(struct kbase_device * const kbdev)
  621. {
  622. struct kbasep_js_device_data *jsdd;
  623. int err;
  624. int i;
  625. u16 as_present;
  626. KBASE_DEBUG_ASSERT(kbdev != NULL);
  627. jsdd = &kbdev->js_data;
  628. KBASE_DEBUG_ASSERT(jsdd->init_status == JS_DEVDATA_INIT_NONE);
  629. /* These two must be recalculated if nr_hw_address_spaces changes
  630. * (e.g. for HW workarounds) */
  631. as_present = (1U << kbdev->nr_hw_address_spaces) - 1;
  632. kbdev->nr_user_address_spaces = kbdev->nr_hw_address_spaces;
  633. if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8987)) {
  634. bool use_workaround;
  635. use_workaround = DEFAULT_SECURE_BUT_LOSS_OF_PERFORMANCE;
  636. if (use_workaround) {
  637. dev_dbg(kbdev->dev, "GPU has HW ISSUE 8987, and driver configured for security workaround: 1 address space only");
  638. kbdev->nr_user_address_spaces = 1;
  639. }
  640. }
  641. #ifdef CONFIG_MALI_DEBUG
  642. /* Soft-stop will be disabled on a single context by default unless
  643. * softstop_always is set */
  644. jsdd->softstop_always = false;
  645. #endif /* CONFIG_MALI_DEBUG */
  646. jsdd->nr_all_contexts_running = 0;
  647. jsdd->nr_user_contexts_running = 0;
  648. jsdd->nr_contexts_pullable = 0;
  649. atomic_set(&jsdd->nr_contexts_runnable, 0);
  650. /* All ASs initially free */
  651. jsdd->as_free = as_present;
  652. /* No ctx allowed to submit */
  653. jsdd->runpool_irq.submit_allowed = 0u;
  654. memset(jsdd->runpool_irq.ctx_attr_ref_count, 0,
  655. sizeof(jsdd->runpool_irq.ctx_attr_ref_count));
  656. memset(jsdd->runpool_irq.slot_affinities, 0,
  657. sizeof(jsdd->runpool_irq.slot_affinities));
  658. memset(jsdd->runpool_irq.slot_affinity_refcount, 0,
  659. sizeof(jsdd->runpool_irq.slot_affinity_refcount));
  660. INIT_LIST_HEAD(&jsdd->suspended_soft_jobs_list);
  661. /* Config attributes */
  662. jsdd->scheduling_period_ns = DEFAULT_JS_SCHEDULING_PERIOD_NS;
  663. jsdd->soft_stop_ticks = DEFAULT_JS_SOFT_STOP_TICKS;
  664. jsdd->soft_stop_ticks_cl = DEFAULT_JS_SOFT_STOP_TICKS_CL;
  665. if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8408))
  666. jsdd->hard_stop_ticks_ss = DEFAULT_JS_HARD_STOP_TICKS_SS_8408;
  667. else
  668. jsdd->hard_stop_ticks_ss = DEFAULT_JS_HARD_STOP_TICKS_SS;
  669. jsdd->hard_stop_ticks_cl = DEFAULT_JS_HARD_STOP_TICKS_CL;
  670. jsdd->hard_stop_ticks_dumping = DEFAULT_JS_HARD_STOP_TICKS_DUMPING;
  671. if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8408))
  672. jsdd->gpu_reset_ticks_ss = DEFAULT_JS_RESET_TICKS_SS_8408;
  673. else
  674. jsdd->gpu_reset_ticks_ss = DEFAULT_JS_RESET_TICKS_SS;
  675. jsdd->gpu_reset_ticks_cl = DEFAULT_JS_RESET_TICKS_CL;
  676. jsdd->gpu_reset_ticks_dumping = DEFAULT_JS_RESET_TICKS_DUMPING;
  677. jsdd->ctx_timeslice_ns = DEFAULT_JS_CTX_TIMESLICE_NS;
  678. jsdd->cfs_ctx_runtime_init_slices =
  679. DEFAULT_JS_CFS_CTX_RUNTIME_INIT_SLICES;
  680. jsdd->cfs_ctx_runtime_min_slices =
  681. DEFAULT_JS_CFS_CTX_RUNTIME_MIN_SLICES;
  682. atomic_set(&jsdd->soft_event_timeout_ms, DEFAULT_JS_SOFT_EVENT_TIMEOUT);
  683. dev_dbg(kbdev->dev, "JS Config Attribs: ");
  684. dev_dbg(kbdev->dev, "\tscheduling_period_ns:%u",
  685. jsdd->scheduling_period_ns);
  686. dev_dbg(kbdev->dev, "\tsoft_stop_ticks:%u",
  687. jsdd->soft_stop_ticks);
  688. dev_dbg(kbdev->dev, "\tsoft_stop_ticks_cl:%u",
  689. jsdd->soft_stop_ticks_cl);
  690. dev_dbg(kbdev->dev, "\thard_stop_ticks_ss:%u",
  691. jsdd->hard_stop_ticks_ss);
  692. dev_dbg(kbdev->dev, "\thard_stop_ticks_cl:%u",
  693. jsdd->hard_stop_ticks_cl);
  694. dev_dbg(kbdev->dev, "\thard_stop_ticks_dumping:%u",
  695. jsdd->hard_stop_ticks_dumping);
  696. dev_dbg(kbdev->dev, "\tgpu_reset_ticks_ss:%u",
  697. jsdd->gpu_reset_ticks_ss);
  698. dev_dbg(kbdev->dev, "\tgpu_reset_ticks_cl:%u",
  699. jsdd->gpu_reset_ticks_cl);
  700. dev_dbg(kbdev->dev, "\tgpu_reset_ticks_dumping:%u",
  701. jsdd->gpu_reset_ticks_dumping);
  702. dev_dbg(kbdev->dev, "\tctx_timeslice_ns:%u",
  703. jsdd->ctx_timeslice_ns);
  704. dev_dbg(kbdev->dev, "\tcfs_ctx_runtime_init_slices:%u",
  705. jsdd->cfs_ctx_runtime_init_slices);
  706. dev_dbg(kbdev->dev, "\tcfs_ctx_runtime_min_slices:%u",
  707. jsdd->cfs_ctx_runtime_min_slices);
  708. dev_dbg(kbdev->dev, "\tsoft_event_timeout:%i",
  709. atomic_read(&jsdd->soft_event_timeout_ms));
  710. if (!(jsdd->soft_stop_ticks < jsdd->hard_stop_ticks_ss &&
  711. jsdd->hard_stop_ticks_ss < jsdd->gpu_reset_ticks_ss &&
  712. jsdd->soft_stop_ticks < jsdd->hard_stop_ticks_dumping &&
  713. jsdd->hard_stop_ticks_dumping <
  714. jsdd->gpu_reset_ticks_dumping)) {
  715. dev_err(kbdev->dev, "Job scheduler timeouts invalid; soft/hard/reset tick counts should be in increasing order\n");
  716. return -EINVAL;
  717. }
  718. #if KBASE_DISABLE_SCHEDULING_SOFT_STOPS
  719. dev_dbg(kbdev->dev, "Job Scheduling Policy Soft-stops disabled, ignoring value for soft_stop_ticks==%u at %uns per tick. Other soft-stops may still occur.",
  720. jsdd->soft_stop_ticks,
  721. jsdd->scheduling_period_ns);
  722. #endif
  723. #if KBASE_DISABLE_SCHEDULING_HARD_STOPS
  724. dev_dbg(kbdev->dev, "Job Scheduling Policy Hard-stops disabled, ignoring values for hard_stop_ticks_ss==%d and hard_stop_ticks_dumping==%u at %uns per tick. Other hard-stops may still occur.",
  725. jsdd->hard_stop_ticks_ss,
  726. jsdd->hard_stop_ticks_dumping,
  727. jsdd->scheduling_period_ns);
  728. #endif
  729. #if KBASE_DISABLE_SCHEDULING_SOFT_STOPS && KBASE_DISABLE_SCHEDULING_HARD_STOPS
  730. dev_dbg(kbdev->dev, "Note: The JS policy's tick timer (if coded) will still be run, but do nothing.");
  731. #endif
  732. /* setup the number of irq throttle cycles base on given time */
  733. {
  734. int time_us = kbdev->gpu_props.irq_throttle_time_us;
  735. int cycles = kbasep_js_convert_us_to_gpu_ticks_max_freq(kbdev,
  736. time_us);
  737. atomic_set(&kbdev->irq_throttle_cycles, cycles);
  738. }
  739. /* Clear the AS data, including setting NULL pointers */
  740. memset(&jsdd->runpool_irq.per_as_data[0], 0,
  741. sizeof(jsdd->runpool_irq.per_as_data));
  742. for (i = 0; i < kbdev->gpu_props.num_job_slots; ++i)
  743. jsdd->js_reqs[i] = core_reqs_from_jsn_features(
  744. kbdev->gpu_props.props.raw_props.js_features[i]);
  745. jsdd->init_status |= JS_DEVDATA_INIT_CONSTANTS;
  746. /* On error, we could continue on: providing none of the below resources
  747. * rely on the ones above */
  748. mutex_init(&jsdd->runpool_mutex);
  749. mutex_init(&jsdd->queue_mutex);
  750. spin_lock_init(&jsdd->runpool_irq.lock);
  751. sema_init(&jsdd->schedule_sem, 1);
  752. err = kbasep_js_policy_init(kbdev);
  753. if (!err)
  754. jsdd->init_status |= JS_DEVDATA_INIT_POLICY;
  755. for (i = 0; i < kbdev->gpu_props.num_job_slots; ++i) {
  756. INIT_LIST_HEAD(&jsdd->ctx_list_pullable[i]);
  757. INIT_LIST_HEAD(&jsdd->ctx_list_unpullable[i]);
  758. }
  759. /* On error, do no cleanup; this will be handled by the caller(s), since
  760. * we've designed this resource to be safe to terminate on init-fail */
  761. if (jsdd->init_status != JS_DEVDATA_INIT_ALL)
  762. return -EINVAL;
  763. return 0;
  764. }
  765. void kbasep_js_devdata_halt(struct kbase_device *kbdev)
  766. {
  767. CSTD_UNUSED(kbdev);
  768. }
  769. void kbasep_js_devdata_term(struct kbase_device *kbdev)
  770. {
  771. struct kbasep_js_device_data *js_devdata;
  772. KBASE_DEBUG_ASSERT(kbdev != NULL);
  773. js_devdata = &kbdev->js_data;
  774. if ((js_devdata->init_status & JS_DEVDATA_INIT_CONSTANTS)) {
  775. s8 zero_ctx_attr_ref_count[KBASEP_JS_CTX_ATTR_COUNT] = { 0, };
  776. /* The caller must de-register all contexts before calling this
  777. */
  778. KBASE_DEBUG_ASSERT(js_devdata->nr_all_contexts_running == 0);
  779. KBASE_DEBUG_ASSERT(memcmp(
  780. js_devdata->runpool_irq.ctx_attr_ref_count,
  781. zero_ctx_attr_ref_count,
  782. sizeof(zero_ctx_attr_ref_count)) == 0);
  783. CSTD_UNUSED(zero_ctx_attr_ref_count);
  784. }
  785. if ((js_devdata->init_status & JS_DEVDATA_INIT_POLICY))
  786. kbasep_js_policy_term(&js_devdata->policy);
  787. js_devdata->init_status = JS_DEVDATA_INIT_NONE;
  788. }
  789. int kbasep_js_kctx_init(struct kbase_context * const kctx)
  790. {
  791. struct kbase_device *kbdev;
  792. struct kbasep_js_kctx_info *js_kctx_info;
  793. int err;
  794. int i, j;
  795. KBASE_DEBUG_ASSERT(kctx != NULL);
  796. kbdev = kctx->kbdev;
  797. KBASE_DEBUG_ASSERT(kbdev != NULL);
  798. for (i = 0; i < BASE_JM_MAX_NR_SLOTS; ++i)
  799. INIT_LIST_HEAD(&kctx->jctx.sched_info.ctx.ctx_list_entry[i]);
  800. js_kctx_info = &kctx->jctx.sched_info;
  801. KBASE_DEBUG_ASSERT(js_kctx_info->init_status == JS_KCTX_INIT_NONE);
  802. js_kctx_info->ctx.nr_jobs = 0;
  803. js_kctx_info->ctx.is_scheduled = false;
  804. js_kctx_info->ctx.is_dying = false;
  805. memset(js_kctx_info->ctx.ctx_attr_ref_count, 0,
  806. sizeof(js_kctx_info->ctx.ctx_attr_ref_count));
  807. /* Initially, the context is disabled from submission until the create
  808. * flags are set */
  809. js_kctx_info->ctx.flags = KBASE_CTX_FLAG_SUBMIT_DISABLED;
  810. js_kctx_info->init_status |= JS_KCTX_INIT_CONSTANTS;
  811. /* On error, we could continue on: providing none of the below resources
  812. * rely on the ones above */
  813. mutex_init(&js_kctx_info->ctx.jsctx_mutex);
  814. init_waitqueue_head(&js_kctx_info->ctx.is_scheduled_wait);
  815. err = kbasep_js_policy_init_ctx(kbdev, kctx);
  816. if (!err)
  817. js_kctx_info->init_status |= JS_KCTX_INIT_POLICY;
  818. /* On error, do no cleanup; this will be handled by the caller(s), since
  819. * we've designed this resource to be safe to terminate on init-fail */
  820. if (js_kctx_info->init_status != JS_KCTX_INIT_ALL)
  821. return -EINVAL;
  822. for (i = 0; i < KBASE_JS_ATOM_SCHED_PRIO_COUNT; i++) {
  823. for (j = 0; j < BASE_JM_MAX_NR_SLOTS; j++) {
  824. INIT_LIST_HEAD(&kctx->jsctx_queue[i][j].queue_head);
  825. atomic_set(&kctx->jsctx_queue[i][j].indicies, 0);
  826. }
  827. }
  828. return 0;
  829. }
  830. void kbasep_js_kctx_term(struct kbase_context *kctx)
  831. {
  832. struct kbase_device *kbdev;
  833. struct kbasep_js_kctx_info *js_kctx_info;
  834. union kbasep_js_policy *js_policy;
  835. int js;
  836. bool update_ctx_count = false;
  837. KBASE_DEBUG_ASSERT(kctx != NULL);
  838. kbdev = kctx->kbdev;
  839. KBASE_DEBUG_ASSERT(kbdev != NULL);
  840. js_policy = &kbdev->js_data.policy;
  841. js_kctx_info = &kctx->jctx.sched_info;
  842. if ((js_kctx_info->init_status & JS_KCTX_INIT_CONSTANTS)) {
  843. /* The caller must de-register all jobs before calling this */
  844. KBASE_DEBUG_ASSERT(!js_kctx_info->ctx.is_scheduled);
  845. KBASE_DEBUG_ASSERT(js_kctx_info->ctx.nr_jobs == 0);
  846. }
  847. mutex_lock(&kbdev->js_data.queue_mutex);
  848. mutex_lock(&kctx->jctx.sched_info.ctx.jsctx_mutex);
  849. for (js = 0; js < kbdev->gpu_props.num_job_slots; js++)
  850. list_del_init(&kctx->jctx.sched_info.ctx.ctx_list_entry[js]);
  851. if (kctx->ctx_runnable_ref) {
  852. WARN_ON(atomic_read(&kbdev->js_data.nr_contexts_runnable) <= 0);
  853. atomic_dec(&kbdev->js_data.nr_contexts_runnable);
  854. update_ctx_count = true;
  855. kctx->ctx_runnable_ref = false;
  856. }
  857. mutex_unlock(&kctx->jctx.sched_info.ctx.jsctx_mutex);
  858. mutex_unlock(&kbdev->js_data.queue_mutex);
  859. if ((js_kctx_info->init_status & JS_KCTX_INIT_POLICY))
  860. kbasep_js_policy_term_ctx(js_policy, kctx);
  861. js_kctx_info->init_status = JS_KCTX_INIT_NONE;
  862. if (update_ctx_count) {
  863. mutex_lock(&kbdev->js_data.runpool_mutex);
  864. kbase_backend_ctx_count_changed(kbdev);
  865. mutex_unlock(&kbdev->js_data.runpool_mutex);
  866. }
  867. }
  868. /**
  869. * kbase_js_ctx_list_add_pullable - Add context to the tail of the per-slot
  870. * pullable context queue
  871. * @kbdev: Device pointer
  872. * @kctx: Context to add to queue
  873. * @js: Job slot to use
  874. *
  875. * If the context is on either the pullable or unpullable queues, then it is
  876. * removed before being added to the tail.
  877. *
  878. * This function should be used when queueing a context for the first time, or
  879. * re-queueing a context that has been pulled from.
  880. *
  881. * Caller must hold kbasep_jd_device_data.queue_mutex
  882. *
  883. * Return: true if caller should call kbase_backend_ctx_count_changed()
  884. */
  885. static bool kbase_js_ctx_list_add_pullable(struct kbase_device *kbdev,
  886. struct kbase_context *kctx,
  887. int js)
  888. {
  889. bool ret = false;
  890. lockdep_assert_held(&kbdev->js_data.queue_mutex);
  891. lockdep_assert_held(&kctx->jctx.sched_info.ctx.jsctx_mutex);
  892. if (!list_empty(&kctx->jctx.sched_info.ctx.ctx_list_entry[js]))
  893. list_del_init(&kctx->jctx.sched_info.ctx.ctx_list_entry[js]);
  894. list_add_tail(&kctx->jctx.sched_info.ctx.ctx_list_entry[js],
  895. &kbdev->js_data.ctx_list_pullable[js]);
  896. if (!kctx->slots_pullable) {
  897. kbdev->js_data.nr_contexts_pullable++;
  898. ret = true;
  899. if (!atomic_read(&kctx->atoms_pulled)) {
  900. WARN_ON(kctx->ctx_runnable_ref);
  901. kctx->ctx_runnable_ref = true;
  902. atomic_inc(&kbdev->js_data.nr_contexts_runnable);
  903. }
  904. }
  905. kctx->slots_pullable |= (1 << js);
  906. return ret;
  907. }
  908. /**
  909. * kbase_js_ctx_list_add_pullable_head - Add context to the head of the
  910. * per-slot pullable context queue
  911. * @kbdev: Device pointer
  912. * @kctx: Context to add to queue
  913. * @js: Job slot to use
  914. *
  915. * If the context is on either the pullable or unpullable queues, then it is
  916. * removed before being added to the head.
  917. *
  918. * This function should be used when a context has been scheduled, but no jobs
  919. * can currently be pulled from it.
  920. *
  921. * Caller must hold kbasep_jd_device_data.queue_mutex
  922. *
  923. * Return: true if caller should call kbase_backend_ctx_count_changed()
  924. */
  925. static bool kbase_js_ctx_list_add_pullable_head(struct kbase_device *kbdev,
  926. struct kbase_context *kctx,
  927. int js)
  928. {
  929. bool ret = false;
  930. lockdep_assert_held(&kbdev->js_data.queue_mutex);
  931. lockdep_assert_held(&kctx->jctx.sched_info.ctx.jsctx_mutex);
  932. if (!list_empty(&kctx->jctx.sched_info.ctx.ctx_list_entry[js]))
  933. list_del_init(&kctx->jctx.sched_info.ctx.ctx_list_entry[js]);
  934. list_add(&kctx->jctx.sched_info.ctx.ctx_list_entry[js],
  935. &kbdev->js_data.ctx_list_pullable[js]);
  936. if (!kctx->slots_pullable) {
  937. kbdev->js_data.nr_contexts_pullable++;
  938. ret = true;
  939. if (!atomic_read(&kctx->atoms_pulled)) {
  940. WARN_ON(kctx->ctx_runnable_ref);
  941. kctx->ctx_runnable_ref = true;
  942. atomic_inc(&kbdev->js_data.nr_contexts_runnable);
  943. }
  944. }
  945. kctx->slots_pullable |= (1 << js);
  946. return ret;
  947. }
  948. /**
  949. * kbase_js_ctx_list_add_unpullable - Add context to the tail of the per-slot
  950. * unpullable context queue
  951. * @kbdev: Device pointer
  952. * @kctx: Context to add to queue
  953. * @js: Job slot to use
  954. *
  955. * The context must already be on the per-slot pullable queue. It will be
  956. * removed from the pullable queue before being added to the unpullable queue.
  957. *
  958. * This function should be used when a context has been pulled from, and there
  959. * are no jobs remaining on the specified slot.
  960. *
  961. * Caller must hold kbasep_jd_device_data.queue_mutex
  962. *
  963. * Return: true if caller should call kbase_backend_ctx_count_changed()
  964. */
  965. static bool kbase_js_ctx_list_add_unpullable(struct kbase_device *kbdev,
  966. struct kbase_context *kctx,
  967. int js)
  968. {
  969. bool ret = false;
  970. lockdep_assert_held(&kbdev->js_data.queue_mutex);
  971. lockdep_assert_held(&kctx->jctx.sched_info.ctx.jsctx_mutex);
  972. list_move_tail(&kctx->jctx.sched_info.ctx.ctx_list_entry[js],
  973. &kbdev->js_data.ctx_list_unpullable[js]);
  974. if (kctx->slots_pullable == (1 << js)) {
  975. kbdev->js_data.nr_contexts_pullable--;
  976. ret = true;
  977. if (!atomic_read(&kctx->atoms_pulled)) {
  978. WARN_ON(!kctx->ctx_runnable_ref);
  979. kctx->ctx_runnable_ref = false;
  980. atomic_dec(&kbdev->js_data.nr_contexts_runnable);
  981. }
  982. }
  983. kctx->slots_pullable &= ~(1 << js);
  984. return ret;
  985. }
  986. /**
  987. * kbase_js_ctx_list_remove - Remove context from the per-slot pullable or
  988. * unpullable context queues
  989. * @kbdev: Device pointer
  990. * @kctx: Context to remove from queue
  991. * @js: Job slot to use
  992. *
  993. * The context must already be on one of the queues.
  994. *
  995. * This function should be used when a context has no jobs on the GPU, and no
  996. * jobs remaining for the specified slot.
  997. *
  998. * Caller must hold kbasep_jd_device_data.queue_mutex
  999. *
  1000. * Return: true if caller should call kbase_backend_ctx_count_changed()
  1001. */
  1002. static bool kbase_js_ctx_list_remove(struct kbase_device *kbdev,
  1003. struct kbase_context *kctx,
  1004. int js)
  1005. {
  1006. bool ret = false;
  1007. lockdep_assert_held(&kbdev->js_data.queue_mutex);
  1008. lockdep_assert_held(&kctx->jctx.sched_info.ctx.jsctx_mutex);
  1009. WARN_ON(list_empty(&kctx->jctx.sched_info.ctx.ctx_list_entry[js]));
  1010. list_del_init(&kctx->jctx.sched_info.ctx.ctx_list_entry[js]);
  1011. if (kctx->slots_pullable == (1 << js)) {
  1012. kbdev->js_data.nr_contexts_pullable--;
  1013. ret = true;
  1014. if (!atomic_read(&kctx->atoms_pulled)) {
  1015. WARN_ON(!kctx->ctx_runnable_ref);
  1016. kctx->ctx_runnable_ref = false;
  1017. atomic_dec(&kbdev->js_data.nr_contexts_runnable);
  1018. }
  1019. }
  1020. kctx->slots_pullable &= ~(1 << js);
  1021. return ret;
  1022. }
  1023. /**
  1024. * kbase_js_ctx_list_pop_head - Pop the head context off the per-slot pullable
  1025. * queue.
  1026. * @kbdev: Device pointer
  1027. * @js: Job slot to use
  1028. *
  1029. * Caller must hold kbasep_jd_device_data::queue_mutex
  1030. *
  1031. * Return: Context to use for specified slot.
  1032. * NULL if no contexts present for specified slot
  1033. */
  1034. static struct kbase_context *kbase_js_ctx_list_pop_head(
  1035. struct kbase_device *kbdev,
  1036. int js)
  1037. {
  1038. struct kbase_context *kctx;
  1039. lockdep_assert_held(&kbdev->js_data.queue_mutex);
  1040. if (list_empty(&kbdev->js_data.ctx_list_pullable[js]))
  1041. return NULL;
  1042. kctx = list_entry(kbdev->js_data.ctx_list_pullable[js].next,
  1043. struct kbase_context,
  1044. jctx.sched_info.ctx.ctx_list_entry[js]);
  1045. list_del_init(&kctx->jctx.sched_info.ctx.ctx_list_entry[js]);
  1046. return kctx;
  1047. }
  1048. /**
  1049. * kbase_js_ctx_pullable - Return if a context can be pulled from on the
  1050. * specified slot
  1051. * @kctx: Context pointer
  1052. * @js: Job slot to use
  1053. * @is_scheduled: true if the context is currently scheduled
  1054. *
  1055. * Caller must hold runpool_irq.lock
  1056. *
  1057. * Return: true if context can be pulled from on specified slot
  1058. * false otherwise
  1059. */
  1060. static bool kbase_js_ctx_pullable(struct kbase_context *kctx, int js,
  1061. bool is_scheduled)
  1062. {
  1063. struct kbasep_js_device_data *js_devdata;
  1064. struct kbase_jd_atom *katom;
  1065. lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
  1066. js_devdata = &kctx->kbdev->js_data;
  1067. if (is_scheduled) {
  1068. if (!kbasep_js_is_submit_allowed(js_devdata, kctx))
  1069. return false;
  1070. }
  1071. katom = jsctx_rb_peek(kctx, js);
  1072. if (!katom)
  1073. return false; /* No pullable atoms */
  1074. if (atomic_read(&katom->blocked))
  1075. return false; /* next atom blocked */
  1076. if (katom->atom_flags & KBASE_KATOM_FLAG_X_DEP_BLOCKED) {
  1077. if (katom->x_pre_dep->gpu_rb_state ==
  1078. KBASE_ATOM_GPU_RB_NOT_IN_SLOT_RB ||
  1079. katom->x_pre_dep->will_fail_event_code)
  1080. return false;
  1081. if ((katom->atom_flags & KBASE_KATOM_FLAG_FAIL_BLOCKER) &&
  1082. kbase_backend_nr_atoms_on_slot(kctx->kbdev, js))
  1083. return false;
  1084. }
  1085. return true;
  1086. }
  1087. static bool kbase_js_dep_validate(struct kbase_context *kctx,
  1088. struct kbase_jd_atom *katom)
  1089. {
  1090. struct kbase_device *kbdev = kctx->kbdev;
  1091. bool ret = true;
  1092. bool has_dep = false, has_x_dep = false;
  1093. int js = kbase_js_get_slot(kbdev, katom);
  1094. int prio = katom->sched_priority;
  1095. int i;
  1096. for (i = 0; i < 2; i++) {
  1097. struct kbase_jd_atom *dep_atom = katom->dep[i].atom;
  1098. if (dep_atom) {
  1099. int dep_js = kbase_js_get_slot(kbdev, dep_atom);
  1100. int dep_prio = dep_atom->sched_priority;
  1101. /* Dependent atom must already have been submitted */
  1102. if (!(dep_atom->atom_flags &
  1103. (KBASE_KATOM_FLAG_JSCTX_IN_LL |
  1104. KBASE_KATOM_FLAG_JSCTX_RB_SUBMITTED))){
  1105. ret = false;
  1106. break;
  1107. }
  1108. /* Dependencies with different priorities can't
  1109. be represented in the ringbuffer */
  1110. if (prio != dep_prio) {
  1111. ret = false;
  1112. break;
  1113. }
  1114. if (js == dep_js) {
  1115. /* Only one same-slot dependency can be
  1116. * represented in the ringbuffer */
  1117. if (has_dep) {
  1118. ret = false;
  1119. break;
  1120. }
  1121. has_dep = true;
  1122. } else {
  1123. /* Only one cross-slot dependency can be
  1124. * represented in the ringbuffer */
  1125. if (has_x_dep) {
  1126. ret = false;
  1127. break;
  1128. }
  1129. /* Each dependee atom can only have one
  1130. * cross-slot dependency */
  1131. if (dep_atom->x_post_dep) {
  1132. ret = false;
  1133. break;
  1134. }
  1135. /* The dependee atom can not already be in the
  1136. * HW access ringbuffer */
  1137. if (dep_atom->gpu_rb_state !=
  1138. KBASE_ATOM_GPU_RB_NOT_IN_SLOT_RB) {
  1139. ret = false;
  1140. break;
  1141. }
  1142. /* The dependee atom can not already have
  1143. * completed */
  1144. if (dep_atom->status !=
  1145. KBASE_JD_ATOM_STATE_IN_JS) {
  1146. ret = false;
  1147. break;
  1148. }
  1149. /* Cross-slot dependencies must not violate
  1150. * PRLAM-8987 affinity restrictions */
  1151. if (kbase_hw_has_issue(kbdev,
  1152. BASE_HW_ISSUE_8987) &&
  1153. (js == 2 || dep_js == 2)) {
  1154. ret = false;
  1155. break;
  1156. }
  1157. has_x_dep = true;
  1158. }
  1159. if (kbase_jd_katom_dep_type(&katom->dep[i]) ==
  1160. BASE_JD_DEP_TYPE_DATA &&
  1161. js == dep_js) {
  1162. struct kbase_jd_atom *last_atom =
  1163. jsctx_rb_peek_last(kctx, js,
  1164. prio);
  1165. /* Last atom on slot must be pre-dep for this
  1166. * atom */
  1167. if (last_atom != dep_atom) {
  1168. ret = false;
  1169. break;
  1170. }
  1171. }
  1172. /* Dependency can be represented in ringbuffers */
  1173. }
  1174. }
  1175. /* If dependencies can be represented by ringbuffer then clear them from
  1176. * atom structure */
  1177. if (ret) {
  1178. for (i = 0; i < 2; i++) {
  1179. struct kbase_jd_atom *dep_atom = katom->dep[i].atom;
  1180. if (dep_atom) {
  1181. int dep_js = kbase_js_get_slot(kbdev, dep_atom);
  1182. if ((js != dep_js) &&
  1183. (dep_atom->status !=
  1184. KBASE_JD_ATOM_STATE_COMPLETED)
  1185. && (dep_atom->status !=
  1186. KBASE_JD_ATOM_STATE_HW_COMPLETED)
  1187. && (dep_atom->status !=
  1188. KBASE_JD_ATOM_STATE_UNUSED)) {
  1189. katom->atom_flags |=
  1190. KBASE_KATOM_FLAG_X_DEP_BLOCKED;
  1191. katom->x_pre_dep = dep_atom;
  1192. dep_atom->x_post_dep = katom;
  1193. if (kbase_jd_katom_dep_type(
  1194. &katom->dep[i]) ==
  1195. BASE_JD_DEP_TYPE_DATA)
  1196. katom->atom_flags |=
  1197. KBASE_KATOM_FLAG_FAIL_BLOCKER;
  1198. }
  1199. if ((kbase_jd_katom_dep_type(&katom->dep[i])
  1200. == BASE_JD_DEP_TYPE_DATA) &&
  1201. (js == dep_js))
  1202. katom->atom_flags |=
  1203. KBASE_KATOM_FLAG_FAIL_PREV;
  1204. list_del(&katom->dep_item[i]);
  1205. kbase_jd_katom_dep_clear(&katom->dep[i]);
  1206. }
  1207. }
  1208. }
  1209. return ret;
  1210. }
  1211. bool kbasep_js_add_job(struct kbase_context *kctx,
  1212. struct kbase_jd_atom *atom)
  1213. {
  1214. unsigned long flags;
  1215. struct kbasep_js_kctx_info *js_kctx_info;
  1216. struct kbase_device *kbdev;
  1217. struct kbasep_js_device_data *js_devdata;
  1218. union kbasep_js_policy *js_policy;
  1219. bool enqueue_required = false;
  1220. bool timer_sync = false;
  1221. KBASE_DEBUG_ASSERT(kctx != NULL);
  1222. KBASE_DEBUG_ASSERT(atom != NULL);
  1223. lockdep_assert_held(&kctx->jctx.lock);
  1224. kbdev = kctx->kbdev;
  1225. js_devdata = &kbdev->js_data;
  1226. js_policy = &kbdev->js_data.policy;
  1227. js_kctx_info = &kctx->jctx.sched_info;
  1228. mutex_lock(&js_devdata->queue_mutex);
  1229. mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
  1230. /*
  1231. * Begin Runpool transaction
  1232. */
  1233. mutex_lock(&js_devdata->runpool_mutex);
  1234. /* Refcount ctx.nr_jobs */
  1235. KBASE_DEBUG_ASSERT(js_kctx_info->ctx.nr_jobs < U32_MAX);
  1236. ++(js_kctx_info->ctx.nr_jobs);
  1237. /* Setup any scheduling information */
  1238. kbasep_js_clear_job_retry_submit(atom);
  1239. /* Lock for state available during IRQ */
  1240. spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
  1241. if (!kbase_js_dep_validate(kctx, atom)) {
  1242. /* Dependencies could not be represented */
  1243. --(js_kctx_info->ctx.nr_jobs);
  1244. /* Setting atom status back to queued as it still has unresolved
  1245. * dependencies */
  1246. atom->status = KBASE_JD_ATOM_STATE_QUEUED;
  1247. spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
  1248. mutex_unlock(&js_devdata->runpool_mutex);
  1249. goto out_unlock;
  1250. }
  1251. KBASE_TIMELINE_ATOM_READY(kctx, kbase_jd_atom_id(kctx, atom));
  1252. enqueue_required = kbase_js_dep_resolved_submit(kctx, atom);
  1253. KBASE_TRACE_ADD_REFCOUNT(kbdev, JS_ADD_JOB, kctx, atom, atom->jc,
  1254. kbasep_js_trace_get_refcnt_nolock(kbdev, kctx));
  1255. /* Context Attribute Refcounting */
  1256. kbasep_js_ctx_attr_ctx_retain_atom(kbdev, kctx, atom);
  1257. if (enqueue_required) {
  1258. if (kbase_js_ctx_pullable(kctx, atom->slot_nr, false))
  1259. timer_sync = kbase_js_ctx_list_add_pullable(kbdev, kctx,
  1260. atom->slot_nr);
  1261. else
  1262. timer_sync = kbase_js_ctx_list_add_unpullable(kbdev,
  1263. kctx, atom->slot_nr);
  1264. }
  1265. /* If this context is active and the atom is the first on its slot,
  1266. * kick the job manager to attempt to fast-start the atom */
  1267. if (enqueue_required && kctx == kbdev->hwaccess.active_kctx)
  1268. kbase_jm_try_kick(kbdev, 1 << atom->slot_nr);
  1269. spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
  1270. if (timer_sync)
  1271. kbase_backend_ctx_count_changed(kbdev);
  1272. mutex_unlock(&js_devdata->runpool_mutex);
  1273. /* End runpool transaction */
  1274. if (!js_kctx_info->ctx.is_scheduled) {
  1275. if (js_kctx_info->ctx.is_dying) {
  1276. /* A job got added while/after kbase_job_zap_context()
  1277. * was called on a non-scheduled context (e.g. KDS
  1278. * dependency resolved). Kill that job by killing the
  1279. * context. */
  1280. kbasep_js_runpool_requeue_or_kill_ctx(kbdev, kctx,
  1281. false);
  1282. } else if (js_kctx_info->ctx.nr_jobs == 1) {
  1283. /* Handle Refcount going from 0 to 1: schedule the
  1284. * context on the Policy Queue */
  1285. KBASE_DEBUG_ASSERT(!js_kctx_info->ctx.is_scheduled);
  1286. dev_dbg(kbdev->dev, "JS: Enqueue Context %p", kctx);
  1287. /* Policy Queue was updated - caller must try to
  1288. * schedule the head context */
  1289. WARN_ON(!enqueue_required);
  1290. }
  1291. }
  1292. out_unlock:
  1293. mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
  1294. mutex_unlock(&js_devdata->queue_mutex);
  1295. return enqueue_required;
  1296. }
  1297. void kbasep_js_remove_job(struct kbase_device *kbdev,
  1298. struct kbase_context *kctx, struct kbase_jd_atom *atom)
  1299. {
  1300. struct kbasep_js_kctx_info *js_kctx_info;
  1301. struct kbasep_js_device_data *js_devdata;
  1302. union kbasep_js_policy *js_policy;
  1303. KBASE_DEBUG_ASSERT(kbdev != NULL);
  1304. KBASE_DEBUG_ASSERT(kctx != NULL);
  1305. KBASE_DEBUG_ASSERT(atom != NULL);
  1306. js_devdata = &kbdev->js_data;
  1307. js_policy = &kbdev->js_data.policy;
  1308. js_kctx_info = &kctx->jctx.sched_info;
  1309. KBASE_TRACE_ADD_REFCOUNT(kbdev, JS_REMOVE_JOB, kctx, atom, atom->jc,
  1310. kbasep_js_trace_get_refcnt(kbdev, kctx));
  1311. /* De-refcount ctx.nr_jobs */
  1312. KBASE_DEBUG_ASSERT(js_kctx_info->ctx.nr_jobs > 0);
  1313. --(js_kctx_info->ctx.nr_jobs);
  1314. }
  1315. bool kbasep_js_remove_cancelled_job(struct kbase_device *kbdev,
  1316. struct kbase_context *kctx, struct kbase_jd_atom *katom)
  1317. {
  1318. unsigned long flags;
  1319. struct kbasep_js_atom_retained_state katom_retained_state;
  1320. struct kbasep_js_device_data *js_devdata;
  1321. bool attr_state_changed;
  1322. KBASE_DEBUG_ASSERT(kbdev != NULL);
  1323. KBASE_DEBUG_ASSERT(kctx != NULL);
  1324. KBASE_DEBUG_ASSERT(katom != NULL);
  1325. js_devdata = &kbdev->js_data;
  1326. kbasep_js_atom_retained_state_copy(&katom_retained_state, katom);
  1327. kbasep_js_remove_job(kbdev, kctx, katom);
  1328. spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
  1329. /* The atom has 'finished' (will not be re-run), so no need to call
  1330. * kbasep_js_has_atom_finished().
  1331. *
  1332. * This is because it returns false for soft-stopped atoms, but we
  1333. * want to override that, because we're cancelling an atom regardless of
  1334. * whether it was soft-stopped or not */
  1335. attr_state_changed = kbasep_js_ctx_attr_ctx_release_atom(kbdev, kctx,
  1336. &katom_retained_state);
  1337. spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
  1338. return attr_state_changed;
  1339. }
  1340. bool kbasep_js_runpool_retain_ctx(struct kbase_device *kbdev,
  1341. struct kbase_context *kctx)
  1342. {
  1343. unsigned long flags;
  1344. struct kbasep_js_device_data *js_devdata;
  1345. bool result;
  1346. KBASE_DEBUG_ASSERT(kbdev != NULL);
  1347. js_devdata = &kbdev->js_data;
  1348. /* KBASE_TRACE_ADD_REFCOUNT( kbdev, JS_RETAIN_CTX, kctx, NULL, 0,
  1349. kbasep_js_trace_get_refcnt(kbdev, kctx)); */
  1350. spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
  1351. result = kbasep_js_runpool_retain_ctx_nolock(kbdev, kctx);
  1352. spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
  1353. return result;
  1354. }
  1355. struct kbase_context *kbasep_js_runpool_lookup_ctx(struct kbase_device *kbdev,
  1356. int as_nr)
  1357. {
  1358. unsigned long flags;
  1359. struct kbasep_js_device_data *js_devdata;
  1360. struct kbase_context *found_kctx = NULL;
  1361. struct kbasep_js_per_as_data *js_per_as_data;
  1362. KBASE_DEBUG_ASSERT(kbdev != NULL);
  1363. KBASE_DEBUG_ASSERT(0 <= as_nr && as_nr < BASE_MAX_NR_AS);
  1364. js_devdata = &kbdev->js_data;
  1365. js_per_as_data = &js_devdata->runpool_irq.per_as_data[as_nr];
  1366. spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
  1367. found_kctx = js_per_as_data->kctx;
  1368. if (found_kctx != NULL)
  1369. ++(js_per_as_data->as_busy_refcount);
  1370. spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
  1371. return found_kctx;
  1372. }
  1373. struct kbase_context *kbasep_js_runpool_lookup_ctx_nolock(
  1374. struct kbase_device *kbdev, int as_nr)
  1375. {
  1376. struct kbasep_js_device_data *js_devdata;
  1377. struct kbase_context *found_kctx = NULL;
  1378. struct kbasep_js_per_as_data *js_per_as_data;
  1379. KBASE_DEBUG_ASSERT(kbdev != NULL);
  1380. KBASE_DEBUG_ASSERT(0 <= as_nr && as_nr < BASE_MAX_NR_AS);
  1381. lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
  1382. js_devdata = &kbdev->js_data;
  1383. js_per_as_data = &js_devdata->runpool_irq.per_as_data[as_nr];
  1384. found_kctx = js_per_as_data->kctx;
  1385. if (found_kctx != NULL)
  1386. ++(js_per_as_data->as_busy_refcount);
  1387. return found_kctx;
  1388. }
  1389. /**
  1390. * kbasep_js_release_result - Try running more jobs after releasing a context
  1391. * and/or atom
  1392. *
  1393. * @kbdev: The kbase_device to operate on
  1394. * @kctx: The kbase_context to operate on
  1395. * @katom_retained_state: Retained state from the atom
  1396. * @runpool_ctx_attr_change: True if the runpool context attributes have changed
  1397. *
  1398. * This collates a set of actions that must happen whilst
  1399. * kbasep_js_device_data.runpool_irq.lock is held.
  1400. *
  1401. * This includes running more jobs when:
  1402. * - The previously released kctx caused a ctx attribute change,
  1403. * - The released atom caused a ctx attribute change,
  1404. * - Slots were previously blocked due to affinity restrictions,
  1405. * - Submission during IRQ handling failed.
  1406. *
  1407. * Return: %KBASEP_JS_RELEASE_RESULT_SCHED_ALL if context attributes were
  1408. * changed. The caller should try scheduling all contexts
  1409. */
  1410. static kbasep_js_release_result kbasep_js_run_jobs_after_ctx_and_atom_release(
  1411. struct kbase_device *kbdev,
  1412. struct kbase_context *kctx,
  1413. struct kbasep_js_atom_retained_state *katom_retained_state,
  1414. bool runpool_ctx_attr_change)
  1415. {
  1416. struct kbasep_js_device_data *js_devdata;
  1417. kbasep_js_release_result result = 0;
  1418. KBASE_DEBUG_ASSERT(kbdev != NULL);
  1419. KBASE_DEBUG_ASSERT(kctx != NULL);
  1420. KBASE_DEBUG_ASSERT(katom_retained_state != NULL);
  1421. js_devdata = &kbdev->js_data;
  1422. lockdep_assert_held(&kctx->jctx.sched_info.ctx.jsctx_mutex);
  1423. lockdep_assert_held(&js_devdata->runpool_mutex);
  1424. lockdep_assert_held(&js_devdata->runpool_irq.lock);
  1425. if (js_devdata->nr_user_contexts_running != 0) {
  1426. bool retry_submit = false;
  1427. int retry_jobslot = 0;
  1428. if (katom_retained_state)
  1429. retry_submit = kbasep_js_get_atom_retry_submit_slot(
  1430. katom_retained_state, &retry_jobslot);
  1431. if (runpool_ctx_attr_change || retry_submit) {
  1432. /* A change in runpool ctx attributes might mean we can
  1433. * run more jobs than before */
  1434. result = KBASEP_JS_RELEASE_RESULT_SCHED_ALL;
  1435. KBASE_TRACE_ADD_SLOT(kbdev, JD_DONE_TRY_RUN_NEXT_JOB,
  1436. kctx, NULL, 0u, retry_jobslot);
  1437. }
  1438. }
  1439. return result;
  1440. }
  1441. /*
  1442. * Internal function to release the reference on a ctx and an atom's "retained
  1443. * state", only taking the runpool and as transaction mutexes
  1444. *
  1445. * This also starts more jobs running in the case of an ctx-attribute state
  1446. * change
  1447. *
  1448. * This does none of the followup actions for scheduling:
  1449. * - It does not schedule in a new context
  1450. * - It does not requeue or handle dying contexts
  1451. *
  1452. * For those tasks, just call kbasep_js_runpool_release_ctx() instead
  1453. *
  1454. * Requires:
  1455. * - Context is scheduled in, and kctx->as_nr matches kctx_as_nr
  1456. * - Context has a non-zero refcount
  1457. * - Caller holds js_kctx_info->ctx.jsctx_mutex
  1458. * - Caller holds js_devdata->runpool_mutex
  1459. */
  1460. static kbasep_js_release_result kbasep_js_runpool_release_ctx_internal(
  1461. struct kbase_device *kbdev,
  1462. struct kbase_context *kctx,
  1463. struct kbasep_js_atom_retained_state *katom_retained_state)
  1464. {
  1465. unsigned long flags;
  1466. struct kbasep_js_device_data *js_devdata;
  1467. struct kbasep_js_kctx_info *js_kctx_info;
  1468. union kbasep_js_policy *js_policy;
  1469. struct kbasep_js_per_as_data *js_per_as_data;
  1470. kbasep_js_release_result release_result = 0u;
  1471. bool runpool_ctx_attr_change = false;
  1472. int kctx_as_nr;
  1473. struct kbase_as *current_as;
  1474. int new_ref_count;
  1475. KBASE_DEBUG_ASSERT(kbdev != NULL);
  1476. KBASE_DEBUG_ASSERT(kctx != NULL);
  1477. js_kctx_info = &kctx->jctx.sched_info;
  1478. js_devdata = &kbdev->js_data;
  1479. js_policy = &kbdev->js_data.policy;
  1480. /* Ensure context really is scheduled in */
  1481. KBASE_DEBUG_ASSERT(js_kctx_info->ctx.is_scheduled);
  1482. /* kctx->as_nr and js_per_as_data are only read from here. The caller's
  1483. * js_ctx_mutex provides a barrier that ensures they are up-to-date.
  1484. *
  1485. * They will not change whilst we're reading them, because the refcount
  1486. * is non-zero (and we ASSERT on that last fact).
  1487. */
  1488. kctx_as_nr = kctx->as_nr;
  1489. KBASE_DEBUG_ASSERT(kctx_as_nr != KBASEP_AS_NR_INVALID);
  1490. js_per_as_data = &js_devdata->runpool_irq.per_as_data[kctx_as_nr];
  1491. KBASE_DEBUG_ASSERT(js_per_as_data->as_busy_refcount > 0);
  1492. /*
  1493. * Transaction begins on AS and runpool_irq
  1494. *
  1495. * Assert about out calling contract
  1496. */
  1497. current_as = &kbdev->as[kctx_as_nr];
  1498. mutex_lock(&kbdev->pm.lock);
  1499. mutex_lock(&current_as->transaction_mutex);
  1500. spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
  1501. KBASE_DEBUG_ASSERT(kctx_as_nr == kctx->as_nr);
  1502. KBASE_DEBUG_ASSERT(js_per_as_data->as_busy_refcount > 0);
  1503. /* Update refcount */
  1504. new_ref_count = --(js_per_as_data->as_busy_refcount);
  1505. /* Release the atom if it finished (i.e. wasn't soft-stopped) */
  1506. if (kbasep_js_has_atom_finished(katom_retained_state))
  1507. runpool_ctx_attr_change |= kbasep_js_ctx_attr_ctx_release_atom(
  1508. kbdev, kctx, katom_retained_state);
  1509. KBASE_TRACE_ADD_REFCOUNT(kbdev, JS_RELEASE_CTX, kctx, NULL, 0u,
  1510. new_ref_count);
  1511. if (new_ref_count == 1 && kctx->jctx.sched_info.ctx.flags &
  1512. KBASE_CTX_FLAG_PRIVILEGED &&
  1513. !kbase_pm_is_suspending(kbdev)) {
  1514. /* Context is kept scheduled into an address space even when
  1515. * there are no jobs, in this case we have to handle the
  1516. * situation where all jobs have been evicted from the GPU and
  1517. * submission is disabled.
  1518. *
  1519. * At this point we re-enable submission to allow further jobs
  1520. * to be executed
  1521. */
  1522. kbasep_js_set_submit_allowed(js_devdata, kctx);
  1523. }
  1524. /* Make a set of checks to see if the context should be scheduled out */
  1525. if (new_ref_count == 0 &&
  1526. (!kbasep_js_is_submit_allowed(js_devdata, kctx) ||
  1527. kbdev->pm.suspending)) {
  1528. /* Last reference, and we've been told to remove this context
  1529. * from the Run Pool */
  1530. dev_dbg(kbdev->dev, "JS: RunPool Remove Context %p because as_busy_refcount=%d, jobs=%d, allowed=%d",
  1531. kctx, new_ref_count, js_kctx_info->ctx.nr_jobs,
  1532. kbasep_js_is_submit_allowed(js_devdata, kctx));
  1533. #if defined(CONFIG_MALI_GATOR_SUPPORT)
  1534. kbase_trace_mali_mmu_as_released(kctx->as_nr);
  1535. #endif
  1536. kbase_tlstream_tl_nret_as_ctx(&kbdev->as[kctx->as_nr], kctx);
  1537. kbase_backend_release_ctx_irq(kbdev, kctx);
  1538. if (kbdev->hwaccess.active_kctx == kctx)
  1539. kbdev->hwaccess.active_kctx = NULL;
  1540. /* Ctx Attribute handling
  1541. *
  1542. * Releasing atoms attributes must either happen before this, or
  1543. * after 'is_scheduled' is changed, otherwise we double-decount
  1544. * the attributes */
  1545. runpool_ctx_attr_change |=
  1546. kbasep_js_ctx_attr_runpool_release_ctx(kbdev, kctx);
  1547. /* Releasing the context and katom retained state can allow
  1548. * more jobs to run */
  1549. release_result |=
  1550. kbasep_js_run_jobs_after_ctx_and_atom_release(kbdev,
  1551. kctx, katom_retained_state,
  1552. runpool_ctx_attr_change);
  1553. /*
  1554. * Transaction ends on AS and runpool_irq:
  1555. *
  1556. * By this point, the AS-related data is now clear and ready
  1557. * for re-use.
  1558. *
  1559. * Since releases only occur once for each previous successful
  1560. * retain, and no more retains are allowed on this context, no
  1561. * other thread will be operating in this
  1562. * code whilst we are
  1563. */
  1564. spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
  1565. kbase_backend_release_ctx_noirq(kbdev, kctx);
  1566. mutex_unlock(&current_as->transaction_mutex);
  1567. mutex_unlock(&kbdev->pm.lock);
  1568. /* Note: Don't reuse kctx_as_nr now */
  1569. /* Synchronize with any policy timers */
  1570. kbase_backend_ctx_count_changed(kbdev);
  1571. /* update book-keeping info */
  1572. js_kctx_info->ctx.is_scheduled = false;
  1573. /* Signal any waiter that the context is not scheduled, so is
  1574. * safe for termination - once the jsctx_mutex is also dropped,
  1575. * and jobs have finished. */
  1576. wake_up(&js_kctx_info->ctx.is_scheduled_wait);
  1577. /* Queue an action to occur after we've dropped the lock */
  1578. release_result |= KBASEP_JS_RELEASE_RESULT_WAS_DESCHEDULED;
  1579. } else {
  1580. kbasep_js_run_jobs_after_ctx_and_atom_release(kbdev, kctx,
  1581. katom_retained_state, runpool_ctx_attr_change);
  1582. spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
  1583. mutex_unlock(&current_as->transaction_mutex);
  1584. mutex_unlock(&kbdev->pm.lock);
  1585. }
  1586. return release_result;
  1587. }
  1588. void kbasep_js_runpool_release_ctx_nolock(struct kbase_device *kbdev,
  1589. struct kbase_context *kctx)
  1590. {
  1591. struct kbasep_js_atom_retained_state katom_retained_state;
  1592. /* Setup a dummy katom_retained_state */
  1593. kbasep_js_atom_retained_state_init_invalid(&katom_retained_state);
  1594. kbasep_js_runpool_release_ctx_internal(kbdev, kctx,
  1595. &katom_retained_state);
  1596. }
  1597. void kbasep_js_runpool_requeue_or_kill_ctx(struct kbase_device *kbdev,
  1598. struct kbase_context *kctx, bool has_pm_ref)
  1599. {
  1600. struct kbasep_js_device_data *js_devdata;
  1601. union kbasep_js_policy *js_policy;
  1602. struct kbasep_js_kctx_info *js_kctx_info;
  1603. KBASE_DEBUG_ASSERT(kbdev != NULL);
  1604. KBASE_DEBUG_ASSERT(kctx != NULL);
  1605. js_kctx_info = &kctx->jctx.sched_info;
  1606. js_policy = &kbdev->js_data.policy;
  1607. js_devdata = &kbdev->js_data;
  1608. /* This is called if and only if you've you've detached the context from
  1609. * the Runpool or the Policy Queue, and not added it back to the Runpool
  1610. */
  1611. KBASE_DEBUG_ASSERT(!js_kctx_info->ctx.is_scheduled);
  1612. if (js_kctx_info->ctx.is_dying) {
  1613. /* Dying: don't requeue, but kill all jobs on the context. This
  1614. * happens asynchronously */
  1615. dev_dbg(kbdev->dev,
  1616. "JS: ** Killing Context %p on RunPool Remove **", kctx);
  1617. kbase_js_foreach_ctx_job(kctx, &kbase_jd_cancel);
  1618. }
  1619. }
  1620. void kbasep_js_runpool_release_ctx_and_katom_retained_state(
  1621. struct kbase_device *kbdev, struct kbase_context *kctx,
  1622. struct kbasep_js_atom_retained_state *katom_retained_state)
  1623. {
  1624. struct kbasep_js_device_data *js_devdata;
  1625. struct kbasep_js_kctx_info *js_kctx_info;
  1626. base_jd_event_code event_code;
  1627. kbasep_js_release_result release_result;
  1628. KBASE_DEBUG_ASSERT(kbdev != NULL);
  1629. KBASE_DEBUG_ASSERT(kctx != NULL);
  1630. js_kctx_info = &kctx->jctx.sched_info;
  1631. js_devdata = &kbdev->js_data;
  1632. event_code = katom_retained_state->event_code;
  1633. mutex_lock(&js_devdata->queue_mutex);
  1634. mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
  1635. mutex_lock(&js_devdata->runpool_mutex);
  1636. release_result = kbasep_js_runpool_release_ctx_internal(kbdev, kctx,
  1637. katom_retained_state);
  1638. /* Drop the runpool mutex to allow requeing kctx */
  1639. mutex_unlock(&js_devdata->runpool_mutex);
  1640. if ((release_result & KBASEP_JS_RELEASE_RESULT_WAS_DESCHEDULED) != 0u)
  1641. kbasep_js_runpool_requeue_or_kill_ctx(kbdev, kctx, true);
  1642. /* Drop the jsctx_mutex to allow scheduling in a new context */
  1643. mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
  1644. mutex_unlock(&js_devdata->queue_mutex);
  1645. if (release_result & KBASEP_JS_RELEASE_RESULT_SCHED_ALL)
  1646. kbase_js_sched_all(kbdev);
  1647. }
  1648. void kbasep_js_runpool_release_ctx(struct kbase_device *kbdev,
  1649. struct kbase_context *kctx)
  1650. {
  1651. struct kbasep_js_atom_retained_state katom_retained_state;
  1652. kbasep_js_atom_retained_state_init_invalid(&katom_retained_state);
  1653. kbasep_js_runpool_release_ctx_and_katom_retained_state(kbdev, kctx,
  1654. &katom_retained_state);
  1655. }
  1656. /* Variant of kbasep_js_runpool_release_ctx() that doesn't call into
  1657. * kbase_js_sched_all() */
  1658. static void kbasep_js_runpool_release_ctx_no_schedule(
  1659. struct kbase_device *kbdev, struct kbase_context *kctx)
  1660. {
  1661. struct kbasep_js_device_data *js_devdata;
  1662. struct kbasep_js_kctx_info *js_kctx_info;
  1663. kbasep_js_release_result release_result;
  1664. struct kbasep_js_atom_retained_state katom_retained_state_struct;
  1665. struct kbasep_js_atom_retained_state *katom_retained_state =
  1666. &katom_retained_state_struct;
  1667. KBASE_DEBUG_ASSERT(kbdev != NULL);
  1668. KBASE_DEBUG_ASSERT(kctx != NULL);
  1669. js_kctx_info = &kctx->jctx.sched_info;
  1670. js_devdata = &kbdev->js_data;
  1671. kbasep_js_atom_retained_state_init_invalid(katom_retained_state);
  1672. mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
  1673. mutex_lock(&js_devdata->runpool_mutex);
  1674. release_result = kbasep_js_runpool_release_ctx_internal(kbdev, kctx,
  1675. katom_retained_state);
  1676. /* Drop the runpool mutex to allow requeing kctx */
  1677. mutex_unlock(&js_devdata->runpool_mutex);
  1678. if ((release_result & KBASEP_JS_RELEASE_RESULT_WAS_DESCHEDULED) != 0u)
  1679. kbasep_js_runpool_requeue_or_kill_ctx(kbdev, kctx, true);
  1680. /* Drop the jsctx_mutex to allow scheduling in a new context */
  1681. mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
  1682. /* NOTE: could return release_result if the caller would like to know
  1683. * whether it should schedule a new context, but currently no callers do
  1684. */
  1685. }
  1686. /**
  1687. * kbase_js_set_timeouts - update all JS timeouts with user specified data
  1688. * @kbdev: Device pointer
  1689. *
  1690. * Timeouts are specified through the 'js_timeouts' sysfs file. If a timeout is
  1691. * set to a positive number then that becomes the new value used, if a timeout
  1692. * is negative then the default is set.
  1693. */
  1694. static void kbase_js_set_timeouts(struct kbase_device *kbdev)
  1695. {
  1696. struct kbasep_js_device_data *js_data = &kbdev->js_data;
  1697. if (kbdev->js_scheduling_period_ns < 0)
  1698. js_data->scheduling_period_ns = DEFAULT_JS_SCHEDULING_PERIOD_NS;
  1699. else if (kbdev->js_scheduling_period_ns > 0)
  1700. js_data->scheduling_period_ns = kbdev->js_scheduling_period_ns;
  1701. if (kbdev->js_soft_stop_ticks < 0)
  1702. js_data->soft_stop_ticks = DEFAULT_JS_SOFT_STOP_TICKS;
  1703. else if (kbdev->js_soft_stop_ticks > 0)
  1704. js_data->soft_stop_ticks = kbdev->js_soft_stop_ticks;
  1705. if (kbdev->js_soft_stop_ticks_cl < 0)
  1706. js_data->soft_stop_ticks_cl = DEFAULT_JS_SOFT_STOP_TICKS_CL;
  1707. else if (kbdev->js_soft_stop_ticks_cl > 0)
  1708. js_data->soft_stop_ticks_cl = kbdev->js_soft_stop_ticks_cl;
  1709. if (kbdev->js_hard_stop_ticks_ss < 0) {
  1710. if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8408))
  1711. js_data->hard_stop_ticks_ss =
  1712. DEFAULT_JS_HARD_STOP_TICKS_SS_8408;
  1713. else
  1714. js_data->hard_stop_ticks_ss =
  1715. DEFAULT_JS_HARD_STOP_TICKS_SS;
  1716. } else if (kbdev->js_hard_stop_ticks_ss > 0) {
  1717. js_data->hard_stop_ticks_ss = kbdev->js_hard_stop_ticks_ss;
  1718. }
  1719. if (kbdev->js_hard_stop_ticks_cl < 0)
  1720. js_data->hard_stop_ticks_cl = DEFAULT_JS_HARD_STOP_TICKS_CL;
  1721. else if (kbdev->js_hard_stop_ticks_cl > 0)
  1722. js_data->hard_stop_ticks_cl = kbdev->js_hard_stop_ticks_cl;
  1723. if (kbdev->js_hard_stop_ticks_dumping < 0)
  1724. js_data->hard_stop_ticks_dumping =
  1725. DEFAULT_JS_HARD_STOP_TICKS_DUMPING;
  1726. else if (kbdev->js_hard_stop_ticks_dumping > 0)
  1727. js_data->hard_stop_ticks_dumping =
  1728. kbdev->js_hard_stop_ticks_dumping;
  1729. if (kbdev->js_reset_ticks_ss < 0) {
  1730. if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8408))
  1731. js_data->gpu_reset_ticks_ss =
  1732. DEFAULT_JS_RESET_TICKS_SS_8408;
  1733. else
  1734. js_data->gpu_reset_ticks_ss = DEFAULT_JS_RESET_TICKS_SS;
  1735. } else if (kbdev->js_reset_ticks_ss > 0) {
  1736. js_data->gpu_reset_ticks_ss = kbdev->js_reset_ticks_ss;
  1737. }
  1738. if (kbdev->js_reset_ticks_cl < 0)
  1739. js_data->gpu_reset_ticks_cl = DEFAULT_JS_RESET_TICKS_CL;
  1740. else if (kbdev->js_reset_ticks_cl > 0)
  1741. js_data->gpu_reset_ticks_cl = kbdev->js_reset_ticks_cl;
  1742. if (kbdev->js_reset_ticks_dumping < 0)
  1743. js_data->gpu_reset_ticks_dumping =
  1744. DEFAULT_JS_RESET_TICKS_DUMPING;
  1745. else if (kbdev->js_reset_ticks_dumping > 0)
  1746. js_data->gpu_reset_ticks_dumping =
  1747. kbdev->js_reset_ticks_dumping;
  1748. }
  1749. static bool kbasep_js_schedule_ctx(struct kbase_device *kbdev,
  1750. struct kbase_context *kctx)
  1751. {
  1752. struct kbasep_js_device_data *js_devdata;
  1753. struct kbasep_js_kctx_info *js_kctx_info;
  1754. union kbasep_js_policy *js_policy;
  1755. struct kbase_as *new_address_space = NULL;
  1756. unsigned long flags;
  1757. bool kctx_suspended = false;
  1758. int as_nr;
  1759. js_devdata = &kbdev->js_data;
  1760. js_policy = &kbdev->js_data.policy;
  1761. js_kctx_info = &kctx->jctx.sched_info;
  1762. /* Pick available address space for this context */
  1763. as_nr = kbase_backend_find_free_address_space(kbdev, kctx);
  1764. if (as_nr == KBASEP_AS_NR_INVALID)
  1765. return false; /* No address spaces currently available */
  1766. new_address_space = &kbdev->as[as_nr];
  1767. /*
  1768. * Atomic transaction on the Context and Run Pool begins
  1769. */
  1770. mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
  1771. mutex_lock(&js_devdata->runpool_mutex);
  1772. /* Check to see if context is dying due to kbase_job_zap_context() */
  1773. if (js_kctx_info->ctx.is_dying) {
  1774. /* Roll back the transaction so far and return */
  1775. kbase_backend_release_free_address_space(kbdev, as_nr);
  1776. mutex_unlock(&js_devdata->runpool_mutex);
  1777. mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
  1778. return false;
  1779. }
  1780. KBASE_TRACE_ADD_REFCOUNT(kbdev, JS_TRY_SCHEDULE_HEAD_CTX, kctx, NULL,
  1781. 0u,
  1782. kbasep_js_trace_get_refcnt(kbdev, kctx));
  1783. if (js_devdata->nr_user_contexts_running == 0 &&
  1784. kbdev->js_timeouts_updated) {
  1785. /* Only when there are no other contexts submitting jobs:
  1786. * Latch in run-time job scheduler timeouts that were set
  1787. * through js_timeouts sysfs file */
  1788. kbase_js_set_timeouts(kbdev);
  1789. kbdev->js_timeouts_updated = false;
  1790. }
  1791. js_kctx_info->ctx.is_scheduled = true;
  1792. mutex_lock(&new_address_space->transaction_mutex);
  1793. spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
  1794. /* Assign context to previously chosen address space */
  1795. if (!kbase_backend_use_ctx(kbdev, kctx, as_nr)) {
  1796. spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
  1797. mutex_unlock(&new_address_space->transaction_mutex);
  1798. /* If address space is not pending, then kbase_backend_use_ctx()
  1799. * failed. Roll back the transaction so far and return */
  1800. if (!kctx->as_pending) {
  1801. js_kctx_info->ctx.is_scheduled = false;
  1802. kbase_backend_release_free_address_space(kbdev, as_nr);
  1803. }
  1804. mutex_unlock(&js_devdata->runpool_mutex);
  1805. mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
  1806. return false;
  1807. }
  1808. kbdev->hwaccess.active_kctx = kctx;
  1809. #if defined(CONFIG_MALI_GATOR_SUPPORT)
  1810. kbase_trace_mali_mmu_as_in_use(kctx->as_nr);
  1811. #endif
  1812. kbase_tlstream_tl_ret_as_ctx(&kbdev->as[kctx->as_nr], kctx);
  1813. /* Cause any future waiter-on-termination to wait until the context is
  1814. * descheduled */
  1815. wake_up(&js_kctx_info->ctx.is_scheduled_wait);
  1816. /* Re-check for suspending: a suspend could've occurred, and all the
  1817. * contexts could've been removed from the runpool before we took this
  1818. * lock. In this case, we don't want to allow this context to run jobs,
  1819. * we just want it out immediately.
  1820. *
  1821. * The DMB required to read the suspend flag was issued recently as part
  1822. * of the runpool_irq locking. If a suspend occurs *after* that lock was
  1823. * taken (i.e. this condition doesn't execute), then the
  1824. * kbasep_js_suspend() code will cleanup this context instead (by virtue
  1825. * of it being called strictly after the suspend flag is set, and will
  1826. * wait for this lock to drop) */
  1827. if (kbase_pm_is_suspending(kbdev)) {
  1828. /* Cause it to leave at some later point */
  1829. bool retained;
  1830. retained = kbasep_js_runpool_retain_ctx_nolock(kbdev, kctx);
  1831. KBASE_DEBUG_ASSERT(retained);
  1832. kbasep_js_clear_submit_allowed(js_devdata, kctx);
  1833. kctx_suspended = true;
  1834. }
  1835. /* Transaction complete */
  1836. spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
  1837. mutex_unlock(&new_address_space->transaction_mutex);
  1838. /* Synchronize with any policy timers */
  1839. kbase_backend_ctx_count_changed(kbdev);
  1840. mutex_unlock(&js_devdata->runpool_mutex);
  1841. mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
  1842. /* Note: after this point, the context could potentially get scheduled
  1843. * out immediately */
  1844. if (kctx_suspended) {
  1845. /* Finishing forcing out the context due to a suspend. Use a
  1846. * variant of kbasep_js_runpool_release_ctx() that doesn't
  1847. * schedule a new context, to prevent a risk of recursion back
  1848. * into this function */
  1849. kbasep_js_runpool_release_ctx_no_schedule(kbdev, kctx);
  1850. return false;
  1851. }
  1852. return true;
  1853. }
  1854. static bool kbase_js_use_ctx(struct kbase_device *kbdev,
  1855. struct kbase_context *kctx)
  1856. {
  1857. struct kbasep_js_device_data *js_devdata = &kbdev->js_data;
  1858. unsigned long flags;
  1859. spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
  1860. if (kctx->as_pending) {
  1861. /* Context waiting for AS to be assigned */
  1862. spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
  1863. return false;
  1864. }
  1865. if (kbase_backend_use_ctx_sched(kbdev, kctx)) {
  1866. /* Context already has ASID - mark as active */
  1867. kbdev->hwaccess.active_kctx = kctx;
  1868. spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
  1869. return true; /* Context already scheduled */
  1870. }
  1871. spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
  1872. return kbasep_js_schedule_ctx(kbdev, kctx);
  1873. }
  1874. void kbasep_js_schedule_privileged_ctx(struct kbase_device *kbdev,
  1875. struct kbase_context *kctx)
  1876. {
  1877. struct kbasep_js_kctx_info *js_kctx_info;
  1878. struct kbasep_js_device_data *js_devdata;
  1879. bool is_scheduled;
  1880. KBASE_DEBUG_ASSERT(kbdev != NULL);
  1881. KBASE_DEBUG_ASSERT(kctx != NULL);
  1882. js_devdata = &kbdev->js_data;
  1883. js_kctx_info = &kctx->jctx.sched_info;
  1884. /* This must never be attempted whilst suspending - i.e. it should only
  1885. * happen in response to a syscall from a user-space thread */
  1886. BUG_ON(kbase_pm_is_suspending(kbdev));
  1887. mutex_lock(&js_devdata->queue_mutex);
  1888. mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
  1889. /* Mark the context as privileged */
  1890. js_kctx_info->ctx.flags |= KBASE_CTX_FLAG_PRIVILEGED;
  1891. is_scheduled = js_kctx_info->ctx.is_scheduled;
  1892. if (!is_scheduled) {
  1893. /* Add the context to the pullable list */
  1894. if (kbase_js_ctx_list_add_pullable(kbdev, kctx, 0))
  1895. kbase_js_sync_timers(kbdev);
  1896. /* Fast-starting requires the jsctx_mutex to be dropped,
  1897. * because it works on multiple ctxs */
  1898. mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
  1899. mutex_unlock(&js_devdata->queue_mutex);
  1900. /* Try to schedule the context in */
  1901. kbase_js_sched_all(kbdev);
  1902. /* Wait for the context to be scheduled in */
  1903. wait_event(kctx->jctx.sched_info.ctx.is_scheduled_wait,
  1904. kctx->jctx.sched_info.ctx.is_scheduled);
  1905. } else {
  1906. /* Already scheduled in - We need to retain it to keep the
  1907. * corresponding address space */
  1908. kbasep_js_runpool_retain_ctx(kbdev, kctx);
  1909. mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
  1910. mutex_unlock(&js_devdata->queue_mutex);
  1911. }
  1912. }
  1913. KBASE_EXPORT_TEST_API(kbasep_js_schedule_privileged_ctx);
  1914. void kbasep_js_release_privileged_ctx(struct kbase_device *kbdev,
  1915. struct kbase_context *kctx)
  1916. {
  1917. struct kbasep_js_kctx_info *js_kctx_info;
  1918. bool pending;
  1919. KBASE_DEBUG_ASSERT(kctx != NULL);
  1920. js_kctx_info = &kctx->jctx.sched_info;
  1921. /* We don't need to use the address space anymore */
  1922. mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
  1923. js_kctx_info->ctx.flags &= (~KBASE_CTX_FLAG_PRIVILEGED);
  1924. pending = kctx->as_pending;
  1925. mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
  1926. /* Release the context - it will be scheduled out if there is no
  1927. * pending job */
  1928. if (!pending)
  1929. kbasep_js_runpool_release_ctx(kbdev, kctx);
  1930. kbase_js_sched_all(kbdev);
  1931. }
  1932. KBASE_EXPORT_TEST_API(kbasep_js_release_privileged_ctx);
  1933. void kbasep_js_suspend(struct kbase_device *kbdev)
  1934. {
  1935. unsigned long flags;
  1936. struct kbasep_js_device_data *js_devdata;
  1937. int i;
  1938. u16 retained = 0u;
  1939. int nr_privileged_ctx = 0;
  1940. KBASE_DEBUG_ASSERT(kbdev);
  1941. KBASE_DEBUG_ASSERT(kbase_pm_is_suspending(kbdev));
  1942. js_devdata = &kbdev->js_data;
  1943. spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
  1944. /* Prevent all contexts from submitting */
  1945. js_devdata->runpool_irq.submit_allowed = 0;
  1946. /* Retain each of the contexts, so we can cause it to leave even if it
  1947. * had no refcount to begin with */
  1948. for (i = BASE_MAX_NR_AS - 1; i >= 0; --i) {
  1949. struct kbasep_js_per_as_data *js_per_as_data =
  1950. &js_devdata->runpool_irq.per_as_data[i];
  1951. struct kbase_context *kctx = js_per_as_data->kctx;
  1952. retained = retained << 1;
  1953. if (kctx) {
  1954. ++(js_per_as_data->as_busy_refcount);
  1955. retained |= 1u;
  1956. /* We can only cope with up to 1 privileged context -
  1957. * the instrumented context. It'll be suspended by
  1958. * disabling instrumentation */
  1959. if (kctx->jctx.sched_info.ctx.flags &
  1960. KBASE_CTX_FLAG_PRIVILEGED)
  1961. KBASE_DEBUG_ASSERT(++nr_privileged_ctx == 1);
  1962. }
  1963. }
  1964. CSTD_UNUSED(nr_privileged_ctx);
  1965. spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
  1966. /* De-ref the previous retain to ensure each context gets pulled out
  1967. * sometime later. */
  1968. for (i = 0;
  1969. i < BASE_MAX_NR_AS;
  1970. ++i, retained = retained >> 1) {
  1971. struct kbasep_js_per_as_data *js_per_as_data =
  1972. &js_devdata->runpool_irq.per_as_data[i];
  1973. struct kbase_context *kctx = js_per_as_data->kctx;
  1974. if (retained & 1u)
  1975. kbasep_js_runpool_release_ctx(kbdev, kctx);
  1976. }
  1977. /* Caller must wait for all Power Manager active references to be
  1978. * dropped */
  1979. }
  1980. void kbasep_js_resume(struct kbase_device *kbdev)
  1981. {
  1982. struct kbasep_js_device_data *js_devdata;
  1983. int js;
  1984. KBASE_DEBUG_ASSERT(kbdev);
  1985. js_devdata = &kbdev->js_data;
  1986. KBASE_DEBUG_ASSERT(!kbase_pm_is_suspending(kbdev));
  1987. mutex_lock(&js_devdata->queue_mutex);
  1988. for (js = 0; js < kbdev->gpu_props.num_job_slots; js++) {
  1989. struct kbase_context *kctx, *n;
  1990. list_for_each_entry_safe(kctx, n,
  1991. &kbdev->js_data.ctx_list_unpullable[js],
  1992. jctx.sched_info.ctx.ctx_list_entry[js]) {
  1993. struct kbasep_js_kctx_info *js_kctx_info;
  1994. unsigned long flags;
  1995. bool timer_sync = false;
  1996. js_kctx_info = &kctx->jctx.sched_info;
  1997. mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
  1998. mutex_lock(&js_devdata->runpool_mutex);
  1999. spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
  2000. if (!js_kctx_info->ctx.is_scheduled &&
  2001. kbase_js_ctx_pullable(kctx, js, false))
  2002. timer_sync = kbase_js_ctx_list_add_pullable(
  2003. kbdev, kctx, js);
  2004. spin_unlock_irqrestore(&js_devdata->runpool_irq.lock,
  2005. flags);
  2006. if (timer_sync)
  2007. kbase_backend_ctx_count_changed(kbdev);
  2008. mutex_unlock(&js_devdata->runpool_mutex);
  2009. mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
  2010. }
  2011. }
  2012. mutex_unlock(&js_devdata->queue_mutex);
  2013. /* Restart atom processing */
  2014. kbase_js_sched_all(kbdev);
  2015. /* JS Resume complete */
  2016. }
  2017. bool kbase_js_is_atom_valid(struct kbase_device *kbdev,
  2018. struct kbase_jd_atom *katom)
  2019. {
  2020. if ((katom->core_req & BASE_JD_REQ_FS) &&
  2021. (katom->core_req & (BASE_JD_REQ_CS | BASE_JD_REQ_ONLY_COMPUTE |
  2022. BASE_JD_REQ_T)))
  2023. return false;
  2024. if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8987) &&
  2025. (katom->core_req & BASE_JD_REQ_ONLY_COMPUTE) &&
  2026. (katom->core_req & (BASE_JD_REQ_CS | BASE_JD_REQ_T)))
  2027. return false;
  2028. return true;
  2029. }
  2030. static int kbase_js_get_slot(struct kbase_device *kbdev,
  2031. struct kbase_jd_atom *katom)
  2032. {
  2033. if (katom->core_req & BASE_JD_REQ_FS)
  2034. return 0;
  2035. if (katom->core_req & BASE_JD_REQ_ONLY_COMPUTE) {
  2036. if (katom->device_nr == 1 &&
  2037. kbdev->gpu_props.num_core_groups == 2)
  2038. return 2;
  2039. if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8987))
  2040. return 2;
  2041. }
  2042. return 1;
  2043. }
  2044. bool kbase_js_dep_resolved_submit(struct kbase_context *kctx,
  2045. struct kbase_jd_atom *katom)
  2046. {
  2047. bool enqueue_required;
  2048. katom->slot_nr = kbase_js_get_slot(kctx->kbdev, katom);
  2049. lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
  2050. lockdep_assert_held(&kctx->jctx.lock);
  2051. /* If slot will transition from unpullable to pullable then add to
  2052. * pullable list */
  2053. if (jsctx_rb_none_to_pull(kctx, katom->slot_nr)) {
  2054. enqueue_required = true;
  2055. } else {
  2056. enqueue_required = false;
  2057. }
  2058. /* Check if there are lower priority jobs to soft stop */
  2059. kbase_job_slot_ctx_priority_check_locked(kctx, katom);
  2060. /* Add atom to ring buffer. */
  2061. if (jsctx_rb_add_atom(kctx, katom)) {
  2062. jsctx_ll_add(kctx, katom);
  2063. enqueue_required = false;
  2064. katom->atom_flags |= KBASE_KATOM_FLAG_JSCTX_IN_LL;
  2065. } else {
  2066. katom->atom_flags |= KBASE_KATOM_FLAG_JSCTX_RB_SUBMITTED;
  2067. }
  2068. return enqueue_required;
  2069. }
  2070. /**
  2071. * kbase_js_evict_deps - Evict dependencies of a failed atom.
  2072. * @kctx: Context pointer
  2073. * @katom: Pointer to the atom that has failed.
  2074. * @js: The job slot the katom was run on.
  2075. * @prio: Priority of the katom.
  2076. *
  2077. * Remove all post dependencies of an atom from the context ringbuffers.
  2078. *
  2079. * The original atom's event_code will be propogated to all dependent atoms.
  2080. *
  2081. * Context: Caller must hold the HW access lock
  2082. */
  2083. static void kbase_js_evict_deps(struct kbase_context *kctx,
  2084. struct kbase_jd_atom *katom, int js, int prio)
  2085. {
  2086. struct kbase_jd_atom *x_dep = katom->x_post_dep;
  2087. struct kbase_jd_atom *next_katom = jsctx_rb_peek_prio(kctx, js, prio);
  2088. lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
  2089. if (next_katom &&
  2090. (next_katom->atom_flags & KBASE_KATOM_FLAG_FAIL_PREV)) {
  2091. KBASE_DEBUG_ASSERT(next_katom->status !=
  2092. KBASE_JD_ATOM_STATE_HW_COMPLETED);
  2093. next_katom->will_fail_event_code = katom->event_code;
  2094. }
  2095. /* Has cross slot depenency. */
  2096. if (x_dep && (x_dep->atom_flags & (KBASE_KATOM_FLAG_JSCTX_IN_LL |
  2097. KBASE_KATOM_FLAG_JSCTX_RB_SUBMITTED))) {
  2098. /* Remove dependency.*/
  2099. x_dep->atom_flags &= ~KBASE_KATOM_FLAG_X_DEP_BLOCKED;
  2100. /* Fail if it had a data dependency. */
  2101. if (x_dep->atom_flags & KBASE_KATOM_FLAG_FAIL_BLOCKER) {
  2102. x_dep->will_fail_event_code = katom->event_code;
  2103. }
  2104. }
  2105. }
  2106. struct kbase_jd_atom *kbase_js_pull(struct kbase_context *kctx, int js)
  2107. {
  2108. struct kbase_jd_atom *katom;
  2109. struct kbasep_js_device_data *js_devdata;
  2110. int pulled;
  2111. KBASE_DEBUG_ASSERT(kctx);
  2112. js_devdata = &kctx->kbdev->js_data;
  2113. lockdep_assert_held(&js_devdata->runpool_irq.lock);
  2114. if (!kbasep_js_is_submit_allowed(js_devdata, kctx))
  2115. return NULL;
  2116. if (kbase_pm_is_suspending(kctx->kbdev))
  2117. return NULL;
  2118. katom = jsctx_rb_peek(kctx, js);
  2119. if (!katom)
  2120. return NULL;
  2121. if (atomic_read(&katom->blocked))
  2122. return NULL;
  2123. /* Due to ordering restrictions when unpulling atoms on failure, we do
  2124. * not allow multiple runs of fail-dep atoms from the same context to be
  2125. * present on the same slot */
  2126. if ((katom->atom_flags & KBASE_KATOM_FLAG_FAIL_PREV) &&
  2127. atomic_read(&kctx->atoms_pulled_slot[js])) {
  2128. struct kbase_jd_atom *prev_atom =
  2129. kbase_backend_inspect_tail(kctx->kbdev, js);
  2130. if (prev_atom && prev_atom->kctx != kctx)
  2131. return NULL;
  2132. }
  2133. if (katom->atom_flags & KBASE_KATOM_FLAG_X_DEP_BLOCKED) {
  2134. if (katom->x_pre_dep->gpu_rb_state ==
  2135. KBASE_ATOM_GPU_RB_NOT_IN_SLOT_RB ||
  2136. katom->x_pre_dep->will_fail_event_code)
  2137. return NULL;
  2138. if ((katom->atom_flags & KBASE_KATOM_FLAG_FAIL_BLOCKER) &&
  2139. kbase_backend_nr_atoms_on_slot(kctx->kbdev, js))
  2140. return NULL;
  2141. }
  2142. kctx->pulled = true;
  2143. pulled = atomic_inc_return(&kctx->atoms_pulled);
  2144. if (pulled == 1 && !kctx->slots_pullable) {
  2145. WARN_ON(kctx->ctx_runnable_ref);
  2146. kctx->ctx_runnable_ref = true;
  2147. atomic_inc(&kctx->kbdev->js_data.nr_contexts_runnable);
  2148. }
  2149. atomic_inc(&kctx->atoms_pulled_slot[katom->slot_nr]);
  2150. jsctx_rb_pull(kctx, katom);
  2151. kbasep_js_runpool_retain_ctx_nolock(kctx->kbdev, kctx);
  2152. katom->atom_flags |= KBASE_KATOM_FLAG_HOLDING_CTX_REF;
  2153. katom->sched_info.cfs.ticks = 0;
  2154. return katom;
  2155. }
  2156. static void js_return_worker(struct work_struct *data)
  2157. {
  2158. struct kbase_jd_atom *katom = container_of(data, struct kbase_jd_atom,
  2159. work);
  2160. struct kbase_context *kctx = katom->kctx;
  2161. struct kbase_device *kbdev = kctx->kbdev;
  2162. struct kbasep_js_device_data *js_devdata = &kbdev->js_data;
  2163. struct kbasep_js_kctx_info *js_kctx_info = &kctx->jctx.sched_info;
  2164. struct kbasep_js_atom_retained_state retained_state;
  2165. int js = katom->slot_nr;
  2166. bool timer_sync = false;
  2167. bool context_idle = false;
  2168. unsigned long flags;
  2169. base_jd_core_req core_req = katom->core_req;
  2170. u64 affinity = katom->affinity;
  2171. enum kbase_atom_coreref_state coreref_state = katom->coreref_state;
  2172. kbase_tlstream_aux_job_softstop_ex(katom);
  2173. kbase_backend_complete_wq(kbdev, katom);
  2174. if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8316))
  2175. kbase_as_poking_timer_release_atom(kbdev, kctx, katom);
  2176. kbasep_js_atom_retained_state_copy(&retained_state, katom);
  2177. mutex_lock(&js_devdata->queue_mutex);
  2178. mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
  2179. atomic_dec(&kctx->atoms_pulled);
  2180. atomic_dec(&kctx->atoms_pulled_slot[js]);
  2181. atomic_dec(&katom->blocked);
  2182. spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
  2183. if (!atomic_read(&kctx->atoms_pulled_slot[js]) &&
  2184. jsctx_rb_none_to_pull(kctx, js))
  2185. timer_sync |= kbase_js_ctx_list_remove(kbdev, kctx, js);
  2186. if (!atomic_read(&kctx->atoms_pulled)) {
  2187. if (!kctx->slots_pullable) {
  2188. WARN_ON(!kctx->ctx_runnable_ref);
  2189. kctx->ctx_runnable_ref = false;
  2190. atomic_dec(&kbdev->js_data.nr_contexts_runnable);
  2191. timer_sync = true;
  2192. }
  2193. if (kctx->as_nr != KBASEP_AS_NR_INVALID &&
  2194. !js_kctx_info->ctx.is_dying) {
  2195. int num_slots = kbdev->gpu_props.num_job_slots;
  2196. int slot;
  2197. if (!kbasep_js_is_submit_allowed(js_devdata, kctx))
  2198. kbasep_js_set_submit_allowed(js_devdata, kctx);
  2199. for (slot = 0; slot < num_slots; slot++) {
  2200. if (kbase_js_ctx_pullable(kctx, slot, true))
  2201. timer_sync |=
  2202. kbase_js_ctx_list_add_pullable(
  2203. kbdev, kctx, slot);
  2204. }
  2205. }
  2206. kbase_jm_idle_ctx(kbdev, kctx);
  2207. context_idle = true;
  2208. }
  2209. spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
  2210. if (context_idle) {
  2211. WARN_ON(!kctx->ctx_active);
  2212. kctx->ctx_active = false;
  2213. kbase_pm_context_idle(kbdev);
  2214. }
  2215. if (timer_sync)
  2216. kbase_js_sync_timers(kbdev);
  2217. mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
  2218. mutex_unlock(&js_devdata->queue_mutex);
  2219. katom->atom_flags &= ~KBASE_KATOM_FLAG_HOLDING_CTX_REF;
  2220. kbasep_js_runpool_release_ctx_and_katom_retained_state(kbdev, kctx,
  2221. &retained_state);
  2222. kbase_js_sched_all(kbdev);
  2223. kbase_backend_complete_wq_post_sched(kbdev, core_req, affinity,
  2224. coreref_state);
  2225. }
  2226. void kbase_js_unpull(struct kbase_context *kctx, struct kbase_jd_atom *katom)
  2227. {
  2228. lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
  2229. jsctx_rb_unpull(kctx, katom);
  2230. WARN_ON(work_pending(&katom->work));
  2231. /* Block re-submission until workqueue has run */
  2232. atomic_inc(&katom->blocked);
  2233. kbase_job_check_leave_disjoint(kctx->kbdev, katom);
  2234. KBASE_DEBUG_ASSERT(0 == object_is_on_stack(&katom->work));
  2235. INIT_WORK(&katom->work, js_return_worker);
  2236. queue_work(kctx->jctx.job_done_wq, &katom->work);
  2237. }
  2238. bool kbase_js_complete_atom_wq(struct kbase_context *kctx,
  2239. struct kbase_jd_atom *katom)
  2240. {
  2241. struct kbasep_js_kctx_info *js_kctx_info;
  2242. struct kbasep_js_device_data *js_devdata;
  2243. struct kbase_device *kbdev;
  2244. unsigned long flags;
  2245. bool timer_sync = false;
  2246. int atom_slot;
  2247. bool context_idle = false;
  2248. kbdev = kctx->kbdev;
  2249. atom_slot = katom->slot_nr;
  2250. js_kctx_info = &kctx->jctx.sched_info;
  2251. js_devdata = &kbdev->js_data;
  2252. lockdep_assert_held(&js_kctx_info->ctx.jsctx_mutex);
  2253. mutex_lock(&js_devdata->runpool_mutex);
  2254. spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
  2255. if (katom->atom_flags & KBASE_KATOM_FLAG_JSCTX_RB_SUBMITTED) {
  2256. jsctx_rb_remove(kctx, katom);
  2257. context_idle = !atomic_dec_return(&kctx->atoms_pulled);
  2258. atomic_dec(&kctx->atoms_pulled_slot[atom_slot]);
  2259. if (!atomic_read(&kctx->atoms_pulled) &&
  2260. !kctx->slots_pullable) {
  2261. WARN_ON(!kctx->ctx_runnable_ref);
  2262. kctx->ctx_runnable_ref = false;
  2263. atomic_dec(&kbdev->js_data.nr_contexts_runnable);
  2264. timer_sync = true;
  2265. }
  2266. }
  2267. WARN_ON(katom->atom_flags & KBASE_KATOM_FLAG_JSCTX_IN_LL);
  2268. if (!atomic_read(&kctx->atoms_pulled_slot[atom_slot]) &&
  2269. jsctx_rb_none_to_pull(kctx, atom_slot))
  2270. timer_sync |= kbase_js_ctx_list_remove(kctx->kbdev, kctx,
  2271. atom_slot);
  2272. /*
  2273. * If submission is disabled on this context (most likely due to an
  2274. * atom failure) and there are now no atoms left in the system then
  2275. * re-enable submission so that context can be scheduled again.
  2276. */
  2277. if (!kbasep_js_is_submit_allowed(js_devdata, kctx) &&
  2278. !atomic_read(&kctx->atoms_pulled) &&
  2279. !js_kctx_info->ctx.is_dying) {
  2280. int js;
  2281. kbasep_js_set_submit_allowed(js_devdata, kctx);
  2282. for (js = 0; js < kbdev->gpu_props.num_job_slots; js++) {
  2283. if (kbase_js_ctx_pullable(kctx, js, true))
  2284. timer_sync |= kbase_js_ctx_list_add_pullable(
  2285. kbdev, kctx, js);
  2286. }
  2287. } else if (katom->x_post_dep &&
  2288. kbasep_js_is_submit_allowed(js_devdata, kctx)) {
  2289. int js;
  2290. for (js = 0; js < kbdev->gpu_props.num_job_slots; js++) {
  2291. if (kbase_js_ctx_pullable(kctx, js, true))
  2292. timer_sync |= kbase_js_ctx_list_add_pullable(
  2293. kbdev, kctx, js);
  2294. }
  2295. }
  2296. /* Mark context as inactive. The pm reference will be dropped later in
  2297. * jd_done_worker().
  2298. */
  2299. if (context_idle)
  2300. kctx->ctx_active = false;
  2301. spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
  2302. if (timer_sync)
  2303. kbase_backend_ctx_count_changed(kbdev);
  2304. mutex_unlock(&js_devdata->runpool_mutex);
  2305. return context_idle;
  2306. }
  2307. void kbase_js_complete_atom(struct kbase_jd_atom *katom, ktime_t *end_timestamp)
  2308. {
  2309. u64 microseconds_spent = 0;
  2310. struct kbase_device *kbdev;
  2311. struct kbase_context *kctx = katom->kctx;
  2312. union kbasep_js_policy *js_policy;
  2313. struct kbasep_js_device_data *js_devdata;
  2314. kbdev = kctx->kbdev;
  2315. js_policy = &kbdev->js_data.policy;
  2316. js_devdata = &kbdev->js_data;
  2317. lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
  2318. if (katom->will_fail_event_code)
  2319. katom->event_code = katom->will_fail_event_code;
  2320. katom->status = KBASE_JD_ATOM_STATE_HW_COMPLETED;
  2321. if (katom->event_code != BASE_JD_EVENT_DONE) {
  2322. kbase_js_evict_deps(kctx, katom, katom->slot_nr,
  2323. katom->sched_priority);
  2324. }
  2325. #if defined(CONFIG_MALI_GATOR_SUPPORT)
  2326. kbase_trace_mali_job_slots_event(GATOR_MAKE_EVENT(GATOR_JOB_SLOT_STOP,
  2327. katom->slot_nr), NULL, 0);
  2328. #endif
  2329. kbase_tlstream_tl_nret_atom_lpu(
  2330. katom,
  2331. &kbdev->gpu_props.props.raw_props.js_features[
  2332. katom->slot_nr]);
  2333. kbase_tlstream_tl_nret_atom_as(katom, &kbdev->as[kctx->as_nr]);
  2334. kbase_tlstream_tl_nret_ctx_lpu(
  2335. kctx,
  2336. &kbdev->gpu_props.props.raw_props.js_features[
  2337. katom->slot_nr]);
  2338. /* Calculate the job's time used */
  2339. if (end_timestamp != NULL) {
  2340. /* Only calculating it for jobs that really run on the HW (e.g.
  2341. * removed from next jobs never actually ran, so really did take
  2342. * zero time) */
  2343. ktime_t tick_diff = ktime_sub(*end_timestamp,
  2344. katom->start_timestamp);
  2345. microseconds_spent = ktime_to_ns(tick_diff);
  2346. do_div(microseconds_spent, 1000);
  2347. /* Round up time spent to the minimum timer resolution */
  2348. if (microseconds_spent < KBASEP_JS_TICK_RESOLUTION_US)
  2349. microseconds_spent = KBASEP_JS_TICK_RESOLUTION_US;
  2350. }
  2351. /* Log the result of the job (completion status, and time spent). */
  2352. kbasep_js_policy_log_job_result(js_policy, katom, microseconds_spent);
  2353. kbase_jd_done(katom, katom->slot_nr, end_timestamp, 0);
  2354. /* Unblock cross dependency if present */
  2355. if (katom->x_post_dep && (katom->event_code == BASE_JD_EVENT_DONE ||
  2356. !(katom->x_post_dep->atom_flags &
  2357. KBASE_KATOM_FLAG_FAIL_BLOCKER)))
  2358. katom->x_post_dep->atom_flags &=
  2359. ~KBASE_KATOM_FLAG_X_DEP_BLOCKED;
  2360. }
  2361. void kbase_js_sched(struct kbase_device *kbdev, int js_mask)
  2362. {
  2363. struct kbasep_js_device_data *js_devdata;
  2364. union kbasep_js_policy *js_policy;
  2365. bool timer_sync = false;
  2366. js_devdata = &kbdev->js_data;
  2367. js_policy = &js_devdata->policy;
  2368. down(&js_devdata->schedule_sem);
  2369. mutex_lock(&js_devdata->queue_mutex);
  2370. while (js_mask) {
  2371. int js;
  2372. js = ffs(js_mask) - 1;
  2373. while (1) {
  2374. struct kbase_context *kctx;
  2375. unsigned long flags;
  2376. bool context_idle = false;
  2377. kctx = kbase_js_ctx_list_pop_head(kbdev, js);
  2378. if (!kctx) {
  2379. js_mask &= ~(1 << js);
  2380. break; /* No contexts on pullable list */
  2381. }
  2382. if (!kctx->ctx_active) {
  2383. context_idle = true;
  2384. if (kbase_pm_context_active_handle_suspend(
  2385. kbdev,
  2386. KBASE_PM_SUSPEND_HANDLER_DONT_INCREASE)) {
  2387. /* Suspend pending - return context to
  2388. * queue and stop scheduling */
  2389. mutex_lock(
  2390. &kctx->jctx.sched_info.ctx.jsctx_mutex);
  2391. if (kbase_js_ctx_list_add_pullable_head(
  2392. kctx->kbdev, kctx, js))
  2393. kbase_js_sync_timers(kbdev);
  2394. mutex_unlock(
  2395. &kctx->jctx.sched_info.ctx.jsctx_mutex);
  2396. mutex_unlock(&js_devdata->queue_mutex);
  2397. up(&js_devdata->schedule_sem);
  2398. return;
  2399. }
  2400. kctx->ctx_active = true;
  2401. }
  2402. if (!kbase_js_use_ctx(kbdev, kctx)) {
  2403. mutex_lock(
  2404. &kctx->jctx.sched_info.ctx.jsctx_mutex);
  2405. /* Context can not be used at this time */
  2406. spin_lock_irqsave(&js_devdata->runpool_irq.lock,
  2407. flags);
  2408. if (kctx->as_pending ||
  2409. kbase_js_ctx_pullable(kctx, js, false)
  2410. || (kctx->jctx.sched_info.ctx.flags &
  2411. KBASE_CTX_FLAG_PRIVILEGED))
  2412. timer_sync |=
  2413. kbase_js_ctx_list_add_pullable_head(
  2414. kctx->kbdev, kctx, js);
  2415. else
  2416. timer_sync |=
  2417. kbase_js_ctx_list_add_unpullable(
  2418. kctx->kbdev, kctx, js);
  2419. spin_unlock_irqrestore(
  2420. &js_devdata->runpool_irq.lock, flags);
  2421. mutex_unlock(
  2422. &kctx->jctx.sched_info.ctx.jsctx_mutex);
  2423. if (context_idle) {
  2424. WARN_ON(!kctx->ctx_active);
  2425. kctx->ctx_active = false;
  2426. kbase_pm_context_idle(kbdev);
  2427. }
  2428. /* No more jobs can be submitted on this slot */
  2429. js_mask &= ~(1 << js);
  2430. break;
  2431. }
  2432. mutex_lock(&kctx->jctx.sched_info.ctx.jsctx_mutex);
  2433. spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
  2434. kctx->pulled = false;
  2435. if (!kbase_jm_kick(kbdev, 1 << js))
  2436. /* No more jobs can be submitted on this slot */
  2437. js_mask &= ~(1 << js);
  2438. if (!kctx->pulled) {
  2439. /* Failed to pull jobs - push to head of list */
  2440. if (kbase_js_ctx_pullable(kctx, js, true))
  2441. timer_sync |=
  2442. kbase_js_ctx_list_add_pullable_head(
  2443. kctx->kbdev,
  2444. kctx, js);
  2445. else
  2446. timer_sync |=
  2447. kbase_js_ctx_list_add_unpullable(
  2448. kctx->kbdev,
  2449. kctx, js);
  2450. if (context_idle) {
  2451. kbase_jm_idle_ctx(kbdev, kctx);
  2452. spin_unlock_irqrestore(
  2453. &js_devdata->runpool_irq.lock,
  2454. flags);
  2455. WARN_ON(!kctx->ctx_active);
  2456. kctx->ctx_active = false;
  2457. kbase_pm_context_idle(kbdev);
  2458. } else {
  2459. spin_unlock_irqrestore(
  2460. &js_devdata->runpool_irq.lock,
  2461. flags);
  2462. }
  2463. mutex_unlock(
  2464. &kctx->jctx.sched_info.ctx.jsctx_mutex);
  2465. js_mask &= ~(1 << js);
  2466. break; /* Could not run atoms on this slot */
  2467. }
  2468. /* Push to back of list */
  2469. if (kbase_js_ctx_pullable(kctx, js, true))
  2470. timer_sync |= kbase_js_ctx_list_add_pullable(
  2471. kctx->kbdev, kctx, js);
  2472. else
  2473. timer_sync |= kbase_js_ctx_list_add_unpullable(
  2474. kctx->kbdev, kctx, js);
  2475. spin_unlock_irqrestore(&js_devdata->runpool_irq.lock,
  2476. flags);
  2477. mutex_unlock(&kctx->jctx.sched_info.ctx.jsctx_mutex);
  2478. }
  2479. }
  2480. if (timer_sync)
  2481. kbase_js_sync_timers(kbdev);
  2482. mutex_unlock(&js_devdata->queue_mutex);
  2483. up(&js_devdata->schedule_sem);
  2484. }
  2485. void kbase_js_zap_context(struct kbase_context *kctx)
  2486. {
  2487. struct kbase_device *kbdev = kctx->kbdev;
  2488. struct kbasep_js_device_data *js_devdata = &kbdev->js_data;
  2489. struct kbasep_js_kctx_info *js_kctx_info = &kctx->jctx.sched_info;
  2490. int js;
  2491. /*
  2492. * Critical assumption: No more submission is possible outside of the
  2493. * workqueue. This is because the OS *must* prevent U/K calls (IOCTLs)
  2494. * whilst the struct kbase_context is terminating.
  2495. */
  2496. /* First, atomically do the following:
  2497. * - mark the context as dying
  2498. * - try to evict it from the policy queue */
  2499. mutex_lock(&js_devdata->queue_mutex);
  2500. mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
  2501. js_kctx_info->ctx.is_dying = true;
  2502. dev_dbg(kbdev->dev, "Zap: Try Evict Ctx %p", kctx);
  2503. /*
  2504. * At this point we know:
  2505. * - If eviction succeeded, it was in the policy queue, but now no
  2506. * longer is
  2507. * - We must cancel the jobs here. No Power Manager active reference to
  2508. * release.
  2509. * - This happens asynchronously - kbase_jd_zap_context() will wait for
  2510. * those jobs to be killed.
  2511. * - If eviction failed, then it wasn't in the policy queue. It is one
  2512. * of the following:
  2513. * - a. it didn't have any jobs, and so is not in the Policy Queue or
  2514. * the Run Pool (not scheduled)
  2515. * - Hence, no more work required to cancel jobs. No Power Manager
  2516. * active reference to release.
  2517. * - b. it was in the middle of a scheduling transaction (and thus must
  2518. * have at least 1 job). This can happen from a syscall or a
  2519. * kernel thread. We still hold the jsctx_mutex, and so the thread
  2520. * must be waiting inside kbasep_js_try_schedule_head_ctx(),
  2521. * before checking whether the runpool is full. That thread will
  2522. * continue after we drop the mutex, and will notice the context
  2523. * is dying. It will rollback the transaction, killing all jobs at
  2524. * the same time. kbase_jd_zap_context() will wait for those jobs
  2525. * to be killed.
  2526. * - Hence, no more work required to cancel jobs, or to release the
  2527. * Power Manager active reference.
  2528. * - c. it is scheduled, and may or may not be running jobs
  2529. * - We must cause it to leave the runpool by stopping it from
  2530. * submitting any more jobs. When it finally does leave,
  2531. * kbasep_js_runpool_requeue_or_kill_ctx() will kill all remaining jobs
  2532. * (because it is dying), release the Power Manager active reference,
  2533. * and will not requeue the context in the policy queue.
  2534. * kbase_jd_zap_context() will wait for those jobs to be killed.
  2535. * - Hence, work required just to make it leave the runpool. Cancelling
  2536. * jobs and releasing the Power manager active reference will be
  2537. * handled when it leaves the runpool.
  2538. */
  2539. if (!js_kctx_info->ctx.is_scheduled) {
  2540. for (js = 0; js < kbdev->gpu_props.num_job_slots; js++) {
  2541. if (!list_empty(
  2542. &kctx->jctx.sched_info.ctx.ctx_list_entry[js]))
  2543. list_del_init(
  2544. &kctx->jctx.sched_info.ctx.ctx_list_entry[js]);
  2545. }
  2546. /* The following events require us to kill off remaining jobs
  2547. * and update PM book-keeping:
  2548. * - we evicted it correctly (it must have jobs to be in the
  2549. * Policy Queue)
  2550. *
  2551. * These events need no action, but take this path anyway:
  2552. * - Case a: it didn't have any jobs, and was never in the Queue
  2553. * - Case b: scheduling transaction will be partially rolled-
  2554. * back (this already cancels the jobs)
  2555. */
  2556. KBASE_TRACE_ADD(kbdev, JM_ZAP_NON_SCHEDULED, kctx, NULL, 0u,
  2557. js_kctx_info->ctx.is_scheduled);
  2558. dev_dbg(kbdev->dev, "Zap: Ctx %p scheduled=0", kctx);
  2559. /* Only cancel jobs when we evicted from the policy
  2560. * queue. No Power Manager active reference was held.
  2561. *
  2562. * Having is_dying set ensures that this kills, and
  2563. * doesn't requeue */
  2564. kbasep_js_runpool_requeue_or_kill_ctx(kbdev, kctx, false);
  2565. mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
  2566. mutex_unlock(&js_devdata->queue_mutex);
  2567. } else {
  2568. unsigned long flags;
  2569. bool was_retained;
  2570. /* Case c: didn't evict, but it is scheduled - it's in the Run
  2571. * Pool */
  2572. KBASE_TRACE_ADD(kbdev, JM_ZAP_SCHEDULED, kctx, NULL, 0u,
  2573. js_kctx_info->ctx.is_scheduled);
  2574. dev_dbg(kbdev->dev, "Zap: Ctx %p is in RunPool", kctx);
  2575. /* Disable the ctx from submitting any more jobs */
  2576. spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
  2577. kbasep_js_clear_submit_allowed(js_devdata, kctx);
  2578. /* Retain and (later) release the context whilst it is is now
  2579. * disallowed from submitting jobs - ensures that someone
  2580. * somewhere will be removing the context later on */
  2581. was_retained = kbasep_js_runpool_retain_ctx_nolock(kbdev, kctx);
  2582. /* Since it's scheduled and we have the jsctx_mutex, it must be
  2583. * retained successfully */
  2584. KBASE_DEBUG_ASSERT(was_retained);
  2585. dev_dbg(kbdev->dev, "Zap: Ctx %p Kill Any Running jobs", kctx);
  2586. /* Cancel any remaining running jobs for this kctx - if any.
  2587. * Submit is disallowed which takes effect immediately, so no
  2588. * more new jobs will appear after we do this. */
  2589. for (js = 0; js < kbdev->gpu_props.num_job_slots; js++)
  2590. kbase_job_slot_hardstop(kctx, js, NULL);
  2591. spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
  2592. mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
  2593. mutex_unlock(&js_devdata->queue_mutex);
  2594. dev_dbg(kbdev->dev, "Zap: Ctx %p Release (may or may not schedule out immediately)",
  2595. kctx);
  2596. kbasep_js_runpool_release_ctx(kbdev, kctx);
  2597. }
  2598. KBASE_TRACE_ADD(kbdev, JM_ZAP_DONE, kctx, NULL, 0u, 0u);
  2599. /* After this, you must wait on both the
  2600. * kbase_jd_context::zero_jobs_wait and the
  2601. * kbasep_js_kctx_info::ctx::is_scheduled_waitq - to wait for the jobs
  2602. * to be destroyed, and the context to be de-scheduled (if it was on the
  2603. * runpool).
  2604. *
  2605. * kbase_jd_zap_context() will do this. */
  2606. }
  2607. static inline int trace_get_refcnt(struct kbase_device *kbdev,
  2608. struct kbase_context *kctx)
  2609. {
  2610. struct kbasep_js_device_data *js_devdata;
  2611. int as_nr;
  2612. int refcnt = 0;
  2613. js_devdata = &kbdev->js_data;
  2614. as_nr = kctx->as_nr;
  2615. if (as_nr != KBASEP_AS_NR_INVALID) {
  2616. struct kbasep_js_per_as_data *js_per_as_data;
  2617. js_per_as_data = &js_devdata->runpool_irq.per_as_data[as_nr];
  2618. refcnt = js_per_as_data->as_busy_refcount;
  2619. }
  2620. return refcnt;
  2621. }
  2622. /**
  2623. * kbase_js_foreach_ctx_job(): - Call a function on all jobs in context
  2624. * @kctx: Pointer to context.
  2625. * @callback: Pointer to function to call for each job.
  2626. *
  2627. * Call a function on all jobs belonging to a non-queued, non-running
  2628. * context, and detach the jobs from the context as it goes.
  2629. *
  2630. * Due to the locks that might be held at the time of the call, the callback
  2631. * may need to defer work on a workqueue to complete its actions (e.g. when
  2632. * cancelling jobs)
  2633. *
  2634. * Atoms will be removed from the queue, so this must only be called when
  2635. * cancelling jobs (which occurs as part of context destruction).
  2636. *
  2637. * The locking conditions on the caller are as follows:
  2638. * - it will be holding kbasep_js_kctx_info::ctx::jsctx_mutex.
  2639. */
  2640. static void kbase_js_foreach_ctx_job(struct kbase_context *kctx,
  2641. kbasep_js_policy_ctx_job_cb callback)
  2642. {
  2643. struct kbase_device *kbdev;
  2644. struct kbasep_js_device_data *js_devdata;
  2645. unsigned long flags;
  2646. u32 js;
  2647. kbdev = kctx->kbdev;
  2648. js_devdata = &kbdev->js_data;
  2649. spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
  2650. KBASE_TRACE_ADD_REFCOUNT(kbdev, JS_POLICY_FOREACH_CTX_JOBS, kctx, NULL,
  2651. 0u, trace_get_refcnt(kbdev, kctx));
  2652. /* Invoke callback on jobs on each slot in turn */
  2653. for (js = 0; js < kbdev->gpu_props.num_job_slots; js++)
  2654. jsctx_queue_foreach(kctx, js, callback);
  2655. spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
  2656. }