mali_kbase_js.h 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. /*
  2. *
  3. * (C) COPYRIGHT 2011-2015 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. * @file mali_kbase_js.h
  17. * Job Scheduler APIs.
  18. */
  19. #ifndef _KBASE_JS_H_
  20. #define _KBASE_JS_H_
  21. #include "mali_kbase_js_defs.h"
  22. #include "mali_kbase_js_policy.h"
  23. #include "mali_kbase_defs.h"
  24. #include "mali_kbase_debug.h"
  25. #include "mali_kbase_js_ctx_attr.h"
  26. /**
  27. * @addtogroup base_api
  28. * @{
  29. */
  30. /**
  31. * @addtogroup base_kbase_api
  32. * @{
  33. */
  34. /**
  35. * @addtogroup kbase_js Job Scheduler Internal APIs
  36. * @{
  37. *
  38. * These APIs are Internal to KBase and are available for use by the
  39. * @ref kbase_js_policy "Job Scheduler Policy APIs"
  40. */
  41. /**
  42. * @brief Initialize the Job Scheduler
  43. *
  44. * The struct kbasep_js_device_data sub-structure of \a kbdev must be zero
  45. * initialized before passing to the kbasep_js_devdata_init() function. This is
  46. * to give efficient error path code.
  47. */
  48. int kbasep_js_devdata_init(struct kbase_device * const kbdev);
  49. /**
  50. * @brief Halt the Job Scheduler.
  51. *
  52. * It is safe to call this on \a kbdev even if it the kbasep_js_device_data
  53. * sub-structure was never initialized/failed initialization, to give efficient
  54. * error-path code.
  55. *
  56. * For this to work, the struct kbasep_js_device_data sub-structure of \a kbdev must
  57. * be zero initialized before passing to the kbasep_js_devdata_init()
  58. * function. This is to give efficient error path code.
  59. *
  60. * It is a Programming Error to call this whilst there are still kbase_context
  61. * structures registered with this scheduler.
  62. *
  63. */
  64. void kbasep_js_devdata_halt(struct kbase_device *kbdev);
  65. /**
  66. * @brief Terminate the Job Scheduler
  67. *
  68. * It is safe to call this on \a kbdev even if it the kbasep_js_device_data
  69. * sub-structure was never initialized/failed initialization, to give efficient
  70. * error-path code.
  71. *
  72. * For this to work, the struct kbasep_js_device_data sub-structure of \a kbdev must
  73. * be zero initialized before passing to the kbasep_js_devdata_init()
  74. * function. This is to give efficient error path code.
  75. *
  76. * It is a Programming Error to call this whilst there are still kbase_context
  77. * structures registered with this scheduler.
  78. */
  79. void kbasep_js_devdata_term(struct kbase_device *kbdev);
  80. /**
  81. * @brief Initialize the Scheduling Component of a struct kbase_context on the Job Scheduler.
  82. *
  83. * This effectively registers a struct kbase_context with a Job Scheduler.
  84. *
  85. * It does not register any jobs owned by the struct kbase_context with the scheduler.
  86. * Those must be separately registered by kbasep_js_add_job().
  87. *
  88. * The struct kbase_context must be zero intitialized before passing to the
  89. * kbase_js_init() function. This is to give efficient error path code.
  90. */
  91. int kbasep_js_kctx_init(struct kbase_context * const kctx);
  92. /**
  93. * @brief Terminate the Scheduling Component of a struct kbase_context on the Job Scheduler
  94. *
  95. * This effectively de-registers a struct kbase_context from its Job Scheduler
  96. *
  97. * It is safe to call this on a struct kbase_context that has never had or failed
  98. * initialization of its jctx.sched_info member, to give efficient error-path
  99. * code.
  100. *
  101. * For this to work, the struct kbase_context must be zero intitialized before passing
  102. * to the kbase_js_init() function.
  103. *
  104. * It is a Programming Error to call this whilst there are still jobs
  105. * registered with this context.
  106. */
  107. void kbasep_js_kctx_term(struct kbase_context *kctx);
  108. /**
  109. * @brief Add a job chain to the Job Scheduler, and take necessary actions to
  110. * schedule the context/run the job.
  111. *
  112. * This atomically does the following:
  113. * - Update the numbers of jobs information
  114. * - Add the job to the run pool if necessary (part of init_job)
  115. *
  116. * Once this is done, then an appropriate action is taken:
  117. * - If the ctx is scheduled, it attempts to start the next job (which might be
  118. * this added job)
  119. * - Otherwise, and if this is the first job on the context, it enqueues it on
  120. * the Policy Queue
  121. *
  122. * The Policy's Queue can be updated by this in the following ways:
  123. * - In the above case that this is the first job on the context
  124. * - If the context is high priority and the context is not scheduled, then it
  125. * could cause the Policy to schedule out a low-priority context, allowing
  126. * this context to be scheduled in.
  127. *
  128. * If the context is already scheduled on the RunPool, then adding a job to it
  129. * is guarenteed not to update the Policy Queue. And so, the caller is
  130. * guarenteed to not need to try scheduling a context from the Run Pool - it
  131. * can safely assert that the result is false.
  132. *
  133. * It is a programming error to have more than U32_MAX jobs in flight at a time.
  134. *
  135. * The following locking conditions are made on the caller:
  136. * - it must \em not hold kbasep_js_kctx_info::ctx::jsctx_mutex.
  137. * - it must \em not hold kbasep_js_device_data::runpool_irq::lock (as this will be
  138. * obtained internally)
  139. * - it must \em not hold kbasep_js_device_data::runpool_mutex (as this will be
  140. * obtained internally)
  141. * - it must \em not hold kbasep_jd_device_data::queue_mutex (again, it's used internally).
  142. *
  143. * @return true indicates that the Policy Queue was updated, and so the
  144. * caller will need to try scheduling a context onto the Run Pool.
  145. * @return false indicates that no updates were made to the Policy Queue,
  146. * so no further action is required from the caller. This is \b always returned
  147. * when the context is currently scheduled.
  148. */
  149. bool kbasep_js_add_job(struct kbase_context *kctx, struct kbase_jd_atom *atom);
  150. /**
  151. * @brief Remove a job chain from the Job Scheduler, except for its 'retained state'.
  152. *
  153. * Completely removing a job requires several calls:
  154. * - kbasep_js_copy_atom_retained_state(), to capture the 'retained state' of
  155. * the atom
  156. * - kbasep_js_remove_job(), to partially remove the atom from the Job Scheduler
  157. * - kbasep_js_runpool_release_ctx_and_katom_retained_state(), to release the
  158. * remaining state held as part of the job having been run.
  159. *
  160. * In the common case of atoms completing normally, this set of actions is more optimal for spinlock purposes than having kbasep_js_remove_job() handle all of the actions.
  161. *
  162. * In the case of cancelling atoms, it is easier to call kbasep_js_remove_cancelled_job(), which handles all the necessary actions.
  163. *
  164. * It is a programming error to call this when:
  165. * - \a atom is not a job belonging to kctx.
  166. * - \a atom has already been removed from the Job Scheduler.
  167. * - \a atom is still in the runpool:
  168. * - it has not been removed with kbasep_js_policy_dequeue_job()
  169. * - or, it has not been removed with kbasep_js_policy_dequeue_job_irq()
  170. *
  171. * Do not use this for removing jobs being killed by kbase_jd_cancel() - use
  172. * kbasep_js_remove_cancelled_job() instead.
  173. *
  174. * The following locking conditions are made on the caller:
  175. * - it must hold kbasep_js_kctx_info::ctx::jsctx_mutex.
  176. *
  177. */
  178. void kbasep_js_remove_job(struct kbase_device *kbdev, struct kbase_context *kctx, struct kbase_jd_atom *atom);
  179. /**
  180. * @brief Completely remove a job chain from the Job Scheduler, in the case
  181. * where the job chain was cancelled.
  182. *
  183. * This is a variant of kbasep_js_remove_job() that takes care of removing all
  184. * of the retained state too. This is generally useful for cancelled atoms,
  185. * which need not be handled in an optimal way.
  186. *
  187. * It is a programming error to call this when:
  188. * - \a atom is not a job belonging to kctx.
  189. * - \a atom has already been removed from the Job Scheduler.
  190. * - \a atom is still in the runpool:
  191. * - it is not being killed with kbasep_jd_cancel()
  192. * - or, it has not been removed with kbasep_js_policy_dequeue_job()
  193. * - or, it has not been removed with kbasep_js_policy_dequeue_job_irq()
  194. *
  195. * The following locking conditions are made on the caller:
  196. * - it must hold kbasep_js_kctx_info::ctx::jsctx_mutex.
  197. * - it must \em not hold the kbasep_js_device_data::runpool_irq::lock, (as this will be
  198. * obtained internally)
  199. * - it must \em not hold kbasep_js_device_data::runpool_mutex (as this could be
  200. * obtained internally)
  201. *
  202. * @return true indicates that ctx attributes have changed and the caller
  203. * should call kbase_js_sched_all() to try to run more jobs
  204. * @return false otherwise
  205. */
  206. bool kbasep_js_remove_cancelled_job(struct kbase_device *kbdev,
  207. struct kbase_context *kctx,
  208. struct kbase_jd_atom *katom);
  209. /**
  210. * @brief Refcount a context as being busy, preventing it from being scheduled
  211. * out.
  212. *
  213. * @note This function can safely be called from IRQ context.
  214. *
  215. * The following locking conditions are made on the caller:
  216. * - it must \em not hold the kbasep_js_device_data::runpool_irq::lock, because
  217. * it will be used internally.
  218. *
  219. * @return value != false if the retain succeeded, and the context will not be scheduled out.
  220. * @return false if the retain failed (because the context is being/has been scheduled out).
  221. */
  222. bool kbasep_js_runpool_retain_ctx(struct kbase_device *kbdev, struct kbase_context *kctx);
  223. /**
  224. * @brief Refcount a context as being busy, preventing it from being scheduled
  225. * out.
  226. *
  227. * @note This function can safely be called from IRQ context.
  228. *
  229. * The following locks must be held by the caller:
  230. * - kbasep_js_device_data::runpool_irq::lock
  231. *
  232. * @return value != false if the retain succeeded, and the context will not be scheduled out.
  233. * @return false if the retain failed (because the context is being/has been scheduled out).
  234. */
  235. bool kbasep_js_runpool_retain_ctx_nolock(struct kbase_device *kbdev, struct kbase_context *kctx);
  236. /**
  237. * @brief Lookup a context in the Run Pool based upon its current address space
  238. * and ensure that is stays scheduled in.
  239. *
  240. * The context is refcounted as being busy to prevent it from scheduling
  241. * out. It must be released with kbasep_js_runpool_release_ctx() when it is no
  242. * longer required to stay scheduled in.
  243. *
  244. * @note This function can safely be called from IRQ context.
  245. *
  246. * The following locking conditions are made on the caller:
  247. * - it must \em not hold the kbasep_js_device_data::runpoool_irq::lock, because
  248. * it will be used internally. If the runpool_irq::lock is already held, then
  249. * the caller should use kbasep_js_runpool_lookup_ctx_nolock() instead.
  250. *
  251. * @return a valid struct kbase_context on success, which has been refcounted as being busy.
  252. * @return NULL on failure, indicating that no context was found in \a as_nr
  253. */
  254. struct kbase_context *kbasep_js_runpool_lookup_ctx(struct kbase_device *kbdev, int as_nr);
  255. /**
  256. * kbasep_js_runpool_lookup_ctx_nolock - Lookup a context in the Run Pool based
  257. * upon its current address space and ensure that is stays scheduled in.
  258. * @kbdev: Device pointer
  259. * @as_nr: Address space to lookup
  260. *
  261. * The context is refcounted as being busy to prevent it from scheduling
  262. * out. It must be released with kbasep_js_runpool_release_ctx() when it is no
  263. * longer required to stay scheduled in.
  264. *
  265. * Note: This function can safely be called from IRQ context.
  266. *
  267. * The following locking conditions are made on the caller:
  268. * - it must the kbasep_js_device_data::runpoool_irq::lock.
  269. *
  270. * Return: a valid struct kbase_context on success, which has been refcounted as
  271. * being busy.
  272. * NULL on failure, indicating that no context was found in \a as_nr
  273. */
  274. struct kbase_context *kbasep_js_runpool_lookup_ctx_nolock(
  275. struct kbase_device *kbdev, int as_nr);
  276. /**
  277. * @brief Handling the requeuing/killing of a context that was evicted from the
  278. * policy queue or runpool.
  279. *
  280. * This should be used whenever handing off a context that has been evicted
  281. * from the policy queue or the runpool:
  282. * - If the context is not dying and has jobs, it gets re-added to the policy
  283. * queue
  284. * - Otherwise, it is not added
  285. *
  286. * In addition, if the context is dying the jobs are killed asynchronously.
  287. *
  288. * In all cases, the Power Manager active reference is released
  289. * (kbase_pm_context_idle()) whenever the has_pm_ref parameter is true. \a
  290. * has_pm_ref must be set to false whenever the context was not previously in
  291. * the runpool and does not hold a Power Manager active refcount. Note that
  292. * contexts in a rollback of kbasep_js_try_schedule_head_ctx() might have an
  293. * active refcount even though they weren't in the runpool.
  294. *
  295. * The following locking conditions are made on the caller:
  296. * - it must hold kbasep_js_kctx_info::ctx::jsctx_mutex.
  297. * - it must \em not hold kbasep_jd_device_data::queue_mutex (as this will be
  298. * obtained internally)
  299. */
  300. void kbasep_js_runpool_requeue_or_kill_ctx(struct kbase_device *kbdev, struct kbase_context *kctx, bool has_pm_ref);
  301. /**
  302. * @brief Release a refcount of a context being busy, allowing it to be
  303. * scheduled out.
  304. *
  305. * When the refcount reaches zero and the context \em might be scheduled out
  306. * (depending on whether the Scheudling Policy has deemed it so, or if it has run
  307. * out of jobs).
  308. *
  309. * If the context does get scheduled out, then The following actions will be
  310. * taken as part of deschduling a context:
  311. * - For the context being descheduled:
  312. * - If the context is in the processing of dying (all the jobs are being
  313. * removed from it), then descheduling also kills off any jobs remaining in the
  314. * context.
  315. * - If the context is not dying, and any jobs remain after descheduling the
  316. * context then it is re-enqueued to the Policy's Queue.
  317. * - Otherwise, the context is still known to the scheduler, but remains absent
  318. * from the Policy Queue until a job is next added to it.
  319. * - In all descheduling cases, the Power Manager active reference (obtained
  320. * during kbasep_js_try_schedule_head_ctx()) is released (kbase_pm_context_idle()).
  321. *
  322. * Whilst the context is being descheduled, this also handles actions that
  323. * cause more atoms to be run:
  324. * - Attempt submitting atoms when the Context Attributes on the Runpool have
  325. * changed. This is because the context being scheduled out could mean that
  326. * there are more opportunities to run atoms.
  327. * - Attempt submitting to a slot that was previously blocked due to affinity
  328. * restrictions. This is usually only necessary when releasing a context
  329. * happens as part of completing a previous job, but is harmless nonetheless.
  330. * - Attempt scheduling in a new context (if one is available), and if necessary,
  331. * running a job from that new context.
  332. *
  333. * Unlike retaining a context in the runpool, this function \b cannot be called
  334. * from IRQ context.
  335. *
  336. * It is a programming error to call this on a \a kctx that is not currently
  337. * scheduled, or that already has a zero refcount.
  338. *
  339. * The following locking conditions are made on the caller:
  340. * - it must \em not hold the kbasep_js_device_data::runpool_irq::lock, because
  341. * it will be used internally.
  342. * - it must \em not hold kbasep_js_kctx_info::ctx::jsctx_mutex.
  343. * - it must \em not hold kbasep_js_device_data::runpool_mutex (as this will be
  344. * obtained internally)
  345. * - it must \em not hold the kbase_device::as[n].transaction_mutex (as this will be obtained internally)
  346. * - it must \em not hold kbasep_jd_device_data::queue_mutex (as this will be
  347. * obtained internally)
  348. *
  349. */
  350. void kbasep_js_runpool_release_ctx(struct kbase_device *kbdev, struct kbase_context *kctx);
  351. /**
  352. * @brief Variant of kbasep_js_runpool_release_ctx() that handles additional
  353. * actions from completing an atom.
  354. *
  355. * This is usually called as part of completing an atom and releasing the
  356. * refcount on the context held by the atom.
  357. *
  358. * Therefore, the extra actions carried out are part of handling actions queued
  359. * on a completed atom, namely:
  360. * - Releasing the atom's context attributes
  361. * - Retrying the submission on a particular slot, because we couldn't submit
  362. * on that slot from an IRQ handler.
  363. *
  364. * The locking conditions of this function are the same as those for
  365. * kbasep_js_runpool_release_ctx()
  366. */
  367. void kbasep_js_runpool_release_ctx_and_katom_retained_state(struct kbase_device *kbdev, struct kbase_context *kctx, struct kbasep_js_atom_retained_state *katom_retained_state);
  368. /**
  369. * @brief Variant of kbase_js_runpool_release_ctx() that assumes that
  370. * kbasep_js_device_data::runpool_mutex and
  371. * kbasep_js_kctx_info::ctx::jsctx_mutex are held by the caller, and does not
  372. * attempt to schedule new contexts.
  373. */
  374. void kbasep_js_runpool_release_ctx_nolock(struct kbase_device *kbdev,
  375. struct kbase_context *kctx);
  376. /**
  377. * @brief Schedule in a privileged context
  378. *
  379. * This schedules a context in regardless of the context priority.
  380. * If the runpool is full, a context will be forced out of the runpool and the function will wait
  381. * for the new context to be scheduled in.
  382. * The context will be kept scheduled in (and the corresponding address space reserved) until
  383. * kbasep_js_release_privileged_ctx is called).
  384. *
  385. * The following locking conditions are made on the caller:
  386. * - it must \em not hold the kbasep_js_device_data::runpool_irq::lock, because
  387. * it will be used internally.
  388. * - it must \em not hold kbasep_js_device_data::runpool_mutex (as this will be
  389. * obtained internally)
  390. * - it must \em not hold the kbase_device::as[n].transaction_mutex (as this will be obtained internally)
  391. * - it must \em not hold kbasep_jd_device_data::queue_mutex (again, it's used internally).
  392. * - it must \em not hold kbasep_js_kctx_info::ctx::jsctx_mutex, because it will
  393. * be used internally.
  394. *
  395. */
  396. void kbasep_js_schedule_privileged_ctx(struct kbase_device *kbdev, struct kbase_context *kctx);
  397. /**
  398. * @brief Release a privileged context, allowing it to be scheduled out.
  399. *
  400. * See kbasep_js_runpool_release_ctx for potential side effects.
  401. *
  402. * The following locking conditions are made on the caller:
  403. * - it must \em not hold the kbasep_js_device_data::runpool_irq::lock, because
  404. * it will be used internally.
  405. * - it must \em not hold kbasep_js_kctx_info::ctx::jsctx_mutex.
  406. * - it must \em not hold kbasep_js_device_data::runpool_mutex (as this will be
  407. * obtained internally)
  408. * - it must \em not hold the kbase_device::as[n].transaction_mutex (as this will be obtained internally)
  409. *
  410. */
  411. void kbasep_js_release_privileged_ctx(struct kbase_device *kbdev, struct kbase_context *kctx);
  412. /**
  413. * @brief Try to submit the next job on each slot
  414. *
  415. * The following locks may be used:
  416. * - kbasep_js_device_data::runpool_mutex
  417. * - kbasep_js_device_data::runpool_irq::lock
  418. */
  419. void kbase_js_try_run_jobs(struct kbase_device *kbdev);
  420. /**
  421. * @brief Suspend the job scheduler during a Power Management Suspend event.
  422. *
  423. * Causes all contexts to be removed from the runpool, and prevents any
  424. * contexts from (re)entering the runpool.
  425. *
  426. * This does not handle suspending the one privileged context: the caller must
  427. * instead do this by by suspending the GPU HW Counter Instrumentation.
  428. *
  429. * This will eventually cause all Power Management active references held by
  430. * contexts on the runpool to be released, without running any more atoms.
  431. *
  432. * The caller must then wait for all Power Mangement active refcount to become
  433. * zero before completing the suspend.
  434. *
  435. * The emptying mechanism may take some time to complete, since it can wait for
  436. * jobs to complete naturally instead of forcing them to end quickly. However,
  437. * this is bounded by the Job Scheduling Policy's Job Timeouts. Hence, this
  438. * function is guaranteed to complete in a finite time whenever the Job
  439. * Scheduling Policy implements Job Timeouts (such as those done by CFS).
  440. */
  441. void kbasep_js_suspend(struct kbase_device *kbdev);
  442. /**
  443. * @brief Resume the Job Scheduler after a Power Management Resume event.
  444. *
  445. * This restores the actions from kbasep_js_suspend():
  446. * - Schedules contexts back into the runpool
  447. * - Resumes running atoms on the GPU
  448. */
  449. void kbasep_js_resume(struct kbase_device *kbdev);
  450. /**
  451. * @brief Submit an atom to the job scheduler.
  452. *
  453. * The atom is enqueued on the context's ringbuffer. The caller must have
  454. * ensured that all dependencies can be represented in the ringbuffer.
  455. *
  456. * Caller must hold jctx->lock
  457. *
  458. * @param[in] kctx Context pointer
  459. * @param[in] atom Pointer to the atom to submit
  460. *
  461. * @return Whether the context requires to be enqueued. */
  462. bool kbase_js_dep_resolved_submit(struct kbase_context *kctx,
  463. struct kbase_jd_atom *katom);
  464. /**
  465. * jsctx_ll_flush_to_rb() - Pushes atoms from the linked list to ringbuffer.
  466. * @kctx: Context Pointer
  467. * @prio: Priority (specifies the queue together with js).
  468. * @js: Job slot (specifies the queue together with prio).
  469. *
  470. * Pushes all possible atoms from the linked list to the ringbuffer.
  471. * Number of atoms are limited to free space in the ringbuffer and
  472. * number of available atoms in the linked list.
  473. *
  474. */
  475. void jsctx_ll_flush_to_rb(struct kbase_context *kctx, int prio, int js);
  476. /**
  477. * @brief Pull an atom from a context in the job scheduler for execution.
  478. *
  479. * The atom will not be removed from the ringbuffer at this stage.
  480. *
  481. * The HW access lock must be held when calling this function.
  482. *
  483. * @param[in] kctx Context to pull from
  484. * @param[in] js Job slot to pull from
  485. * @return Pointer to an atom, or NULL if there are no atoms for this
  486. * slot that can be currently run.
  487. */
  488. struct kbase_jd_atom *kbase_js_pull(struct kbase_context *kctx, int js);
  489. /**
  490. * @brief Return an atom to the job scheduler ringbuffer.
  491. *
  492. * An atom is 'unpulled' if execution is stopped but intended to be returned to
  493. * later. The most common reason for this is that the atom has been
  494. * soft-stopped.
  495. *
  496. * Note that if multiple atoms are to be 'unpulled', they must be returned in
  497. * the reverse order to which they were originally pulled. It is a programming
  498. * error to return atoms in any other order.
  499. *
  500. * The HW access lock must be held when calling this function.
  501. *
  502. * @param[in] kctx Context pointer
  503. * @param[in] atom Pointer to the atom to unpull
  504. */
  505. void kbase_js_unpull(struct kbase_context *kctx, struct kbase_jd_atom *katom);
  506. /**
  507. * @brief Complete an atom from jd_done_worker(), removing it from the job
  508. * scheduler ringbuffer.
  509. *
  510. * If the atom failed then all dependee atoms marked for failure propagation
  511. * will also fail.
  512. *
  513. * @param[in] kctx Context pointer
  514. * @param[in] katom Pointer to the atom to complete
  515. * @return true if the context is now idle (no jobs pulled)
  516. * false otherwise
  517. */
  518. bool kbase_js_complete_atom_wq(struct kbase_context *kctx,
  519. struct kbase_jd_atom *katom);
  520. /**
  521. * @brief Complete an atom.
  522. *
  523. * Most of the work required to complete an atom will be performed by
  524. * jd_done_worker().
  525. *
  526. * The HW access lock must be held when calling this function.
  527. *
  528. * @param[in] katom Pointer to the atom to complete
  529. * @param[in] end_timestamp The time that the atom completed (may be NULL)
  530. */
  531. void kbase_js_complete_atom(struct kbase_jd_atom *katom,
  532. ktime_t *end_timestamp);
  533. /**
  534. * @brief Submit atoms from all available contexts.
  535. *
  536. * This will attempt to submit as many jobs as possible to the provided job
  537. * slots. It will exit when either all job slots are full, or all contexts have
  538. * been used.
  539. *
  540. * @param[in] kbdev Device pointer
  541. * @param[in] js_mask Mask of job slots to submit to
  542. */
  543. void kbase_js_sched(struct kbase_device *kbdev, int js_mask);
  544. /**
  545. * kbase_jd_zap_context - Attempt to deschedule a context that is being
  546. * destroyed
  547. * @kctx: Context pointer
  548. *
  549. * This will attempt to remove a context from any internal job scheduler queues
  550. * and perform any other actions to ensure a context will not be submitted
  551. * from.
  552. *
  553. * If the context is currently scheduled, then the caller must wait for all
  554. * pending jobs to complete before taking any further action.
  555. */
  556. void kbase_js_zap_context(struct kbase_context *kctx);
  557. /**
  558. * @brief Validate an atom
  559. *
  560. * This will determine whether the atom can be scheduled onto the GPU. Atoms
  561. * with invalid combinations of core requirements will be rejected.
  562. *
  563. * @param[in] kbdev Device pointer
  564. * @param[in] katom Atom to validate
  565. * @return true if atom is valid
  566. * false otherwise
  567. */
  568. bool kbase_js_is_atom_valid(struct kbase_device *kbdev,
  569. struct kbase_jd_atom *katom);
  570. /*
  571. * Helpers follow
  572. */
  573. /**
  574. * @brief Check that a context is allowed to submit jobs on this policy
  575. *
  576. * The purpose of this abstraction is to hide the underlying data size, and wrap up
  577. * the long repeated line of code.
  578. *
  579. * As with any bool, never test the return value with true.
  580. *
  581. * The caller must hold kbasep_js_device_data::runpool_irq::lock.
  582. */
  583. static inline bool kbasep_js_is_submit_allowed(struct kbasep_js_device_data *js_devdata, struct kbase_context *kctx)
  584. {
  585. u16 test_bit;
  586. /* Ensure context really is scheduled in */
  587. KBASE_DEBUG_ASSERT(kctx->as_nr != KBASEP_AS_NR_INVALID);
  588. KBASE_DEBUG_ASSERT(kctx->jctx.sched_info.ctx.is_scheduled);
  589. test_bit = (u16) (1u << kctx->as_nr);
  590. return (bool) (js_devdata->runpool_irq.submit_allowed & test_bit);
  591. }
  592. /**
  593. * @brief Allow a context to submit jobs on this policy
  594. *
  595. * The purpose of this abstraction is to hide the underlying data size, and wrap up
  596. * the long repeated line of code.
  597. *
  598. * The caller must hold kbasep_js_device_data::runpool_irq::lock.
  599. */
  600. static inline void kbasep_js_set_submit_allowed(struct kbasep_js_device_data *js_devdata, struct kbase_context *kctx)
  601. {
  602. u16 set_bit;
  603. /* Ensure context really is scheduled in */
  604. KBASE_DEBUG_ASSERT(kctx->as_nr != KBASEP_AS_NR_INVALID);
  605. KBASE_DEBUG_ASSERT(kctx->jctx.sched_info.ctx.is_scheduled);
  606. set_bit = (u16) (1u << kctx->as_nr);
  607. dev_dbg(kctx->kbdev->dev, "JS: Setting Submit Allowed on %p (as=%d)", kctx, kctx->as_nr);
  608. js_devdata->runpool_irq.submit_allowed |= set_bit;
  609. }
  610. /**
  611. * @brief Prevent a context from submitting more jobs on this policy
  612. *
  613. * The purpose of this abstraction is to hide the underlying data size, and wrap up
  614. * the long repeated line of code.
  615. *
  616. * The caller must hold kbasep_js_device_data::runpool_irq::lock.
  617. */
  618. static inline void kbasep_js_clear_submit_allowed(struct kbasep_js_device_data *js_devdata, struct kbase_context *kctx)
  619. {
  620. u16 clear_bit;
  621. u16 clear_mask;
  622. /* Ensure context really is scheduled in */
  623. KBASE_DEBUG_ASSERT(kctx->as_nr != KBASEP_AS_NR_INVALID);
  624. KBASE_DEBUG_ASSERT(kctx->jctx.sched_info.ctx.is_scheduled);
  625. clear_bit = (u16) (1u << kctx->as_nr);
  626. clear_mask = ~clear_bit;
  627. dev_dbg(kctx->kbdev->dev, "JS: Clearing Submit Allowed on %p (as=%d)", kctx, kctx->as_nr);
  628. js_devdata->runpool_irq.submit_allowed &= clear_mask;
  629. }
  630. /**
  631. * @brief Manage the 'retry_submit_on_slot' part of a kbase_jd_atom
  632. */
  633. static inline void kbasep_js_clear_job_retry_submit(struct kbase_jd_atom *atom)
  634. {
  635. atom->retry_submit_on_slot = KBASEP_JS_RETRY_SUBMIT_SLOT_INVALID;
  636. }
  637. /**
  638. * Mark a slot as requiring resubmission by carrying that information on a
  639. * completing atom.
  640. *
  641. * @note This can ASSERT in debug builds if the submit slot has been set to
  642. * something other than the current value for @a js. This is because you might
  643. * be unintentionally stopping more jobs being submitted on the old submit
  644. * slot, and that might cause a scheduling-hang.
  645. *
  646. * @note If you can guarantee that the atoms for the original slot will be
  647. * submitted on some other slot, then call kbasep_js_clear_job_retry_submit()
  648. * first to silence the ASSERT.
  649. */
  650. static inline void kbasep_js_set_job_retry_submit_slot(struct kbase_jd_atom *atom, int js)
  651. {
  652. KBASE_DEBUG_ASSERT(0 <= js && js <= BASE_JM_MAX_NR_SLOTS);
  653. KBASE_DEBUG_ASSERT((atom->retry_submit_on_slot ==
  654. KBASEP_JS_RETRY_SUBMIT_SLOT_INVALID)
  655. || (atom->retry_submit_on_slot == js));
  656. atom->retry_submit_on_slot = js;
  657. }
  658. /**
  659. * Create an initial 'invalid' atom retained state, that requires no
  660. * atom-related work to be done on releasing with
  661. * kbasep_js_runpool_release_ctx_and_katom_retained_state()
  662. */
  663. static inline void kbasep_js_atom_retained_state_init_invalid(struct kbasep_js_atom_retained_state *retained_state)
  664. {
  665. retained_state->event_code = BASE_JD_EVENT_NOT_STARTED;
  666. retained_state->core_req = KBASEP_JS_ATOM_RETAINED_STATE_CORE_REQ_INVALID;
  667. retained_state->retry_submit_on_slot = KBASEP_JS_RETRY_SUBMIT_SLOT_INVALID;
  668. }
  669. /**
  670. * Copy atom state that can be made available after jd_done_nolock() is called
  671. * on that atom.
  672. */
  673. static inline void kbasep_js_atom_retained_state_copy(struct kbasep_js_atom_retained_state *retained_state, const struct kbase_jd_atom *katom)
  674. {
  675. retained_state->event_code = katom->event_code;
  676. retained_state->core_req = katom->core_req;
  677. retained_state->retry_submit_on_slot = katom->retry_submit_on_slot;
  678. retained_state->sched_priority = katom->sched_priority;
  679. retained_state->device_nr = katom->device_nr;
  680. }
  681. /**
  682. * @brief Determine whether an atom has finished (given its retained state),
  683. * and so should be given back to userspace/removed from the system.
  684. *
  685. * Reasons for an atom not finishing include:
  686. * - Being soft-stopped (and so, the atom should be resubmitted sometime later)
  687. *
  688. * @param[in] katom_retained_state the retained state of the atom to check
  689. * @return false if the atom has not finished
  690. * @return !=false if the atom has finished
  691. */
  692. static inline bool kbasep_js_has_atom_finished(const struct kbasep_js_atom_retained_state *katom_retained_state)
  693. {
  694. return (bool) (katom_retained_state->event_code != BASE_JD_EVENT_STOPPED && katom_retained_state->event_code != BASE_JD_EVENT_REMOVED_FROM_NEXT);
  695. }
  696. /**
  697. * @brief Determine whether a struct kbasep_js_atom_retained_state is valid
  698. *
  699. * An invalid struct kbasep_js_atom_retained_state is allowed, and indicates that the
  700. * code should just ignore it.
  701. *
  702. * @param[in] katom_retained_state the atom's retained state to check
  703. * @return false if the retained state is invalid, and can be ignored
  704. * @return !=false if the retained state is valid
  705. */
  706. static inline bool kbasep_js_atom_retained_state_is_valid(const struct kbasep_js_atom_retained_state *katom_retained_state)
  707. {
  708. return (bool) (katom_retained_state->core_req != KBASEP_JS_ATOM_RETAINED_STATE_CORE_REQ_INVALID);
  709. }
  710. static inline bool kbasep_js_get_atom_retry_submit_slot(const struct kbasep_js_atom_retained_state *katom_retained_state, int *res)
  711. {
  712. int js = katom_retained_state->retry_submit_on_slot;
  713. *res = js;
  714. return (bool) (js >= 0);
  715. }
  716. #if KBASE_DEBUG_DISABLE_ASSERTS == 0
  717. /**
  718. * Debug Check the refcount of a context. Only use within ASSERTs
  719. *
  720. * Obtains kbasep_js_device_data::runpool_irq::lock
  721. *
  722. * @return negative value if the context is not scheduled in
  723. * @return current refcount of the context if it is scheduled in. The refcount
  724. * is not guarenteed to be kept constant.
  725. */
  726. static inline int kbasep_js_debug_check_ctx_refcount(struct kbase_device *kbdev, struct kbase_context *kctx)
  727. {
  728. unsigned long flags;
  729. struct kbasep_js_device_data *js_devdata;
  730. int result = -1;
  731. int as_nr;
  732. KBASE_DEBUG_ASSERT(kbdev != NULL);
  733. KBASE_DEBUG_ASSERT(kctx != NULL);
  734. js_devdata = &kbdev->js_data;
  735. spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
  736. as_nr = kctx->as_nr;
  737. if (as_nr != KBASEP_AS_NR_INVALID)
  738. result = js_devdata->runpool_irq.per_as_data[as_nr].as_busy_refcount;
  739. spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
  740. return result;
  741. }
  742. #endif // KBASE_DEBUG_DISABLE_ASSERTS == 0
  743. /**
  744. * @brief Variant of kbasep_js_runpool_lookup_ctx() that can be used when the
  745. * context is guarenteed to be already previously retained.
  746. *
  747. * It is a programming error to supply the \a as_nr of a context that has not
  748. * been previously retained/has a busy refcount of zero. The only exception is
  749. * when there is no ctx in \a as_nr (NULL returned).
  750. *
  751. * The following locking conditions are made on the caller:
  752. * - it must \em not hold the kbasep_js_device_data::runpoool_irq::lock, because
  753. * it will be used internally.
  754. *
  755. * @return a valid struct kbase_context on success, with a refcount that is guarenteed
  756. * to be non-zero and unmodified by this function.
  757. * @return NULL on failure, indicating that no context was found in \a as_nr
  758. */
  759. static inline struct kbase_context *kbasep_js_runpool_lookup_ctx_noretain(struct kbase_device *kbdev, int as_nr)
  760. {
  761. unsigned long flags;
  762. struct kbasep_js_device_data *js_devdata;
  763. struct kbase_context *found_kctx;
  764. struct kbasep_js_per_as_data *js_per_as_data;
  765. KBASE_DEBUG_ASSERT(kbdev != NULL);
  766. KBASE_DEBUG_ASSERT(0 <= as_nr && as_nr < BASE_MAX_NR_AS);
  767. js_devdata = &kbdev->js_data;
  768. js_per_as_data = &js_devdata->runpool_irq.per_as_data[as_nr];
  769. spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
  770. found_kctx = js_per_as_data->kctx;
  771. KBASE_DEBUG_ASSERT(found_kctx == NULL || js_per_as_data->as_busy_refcount > 0);
  772. spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
  773. return found_kctx;
  774. }
  775. /**
  776. * This will provide a conversion from time (us) to ticks of the gpu clock
  777. * based on the minimum available gpu frequency.
  778. * This is usually good to compute best/worst case (where the use of current
  779. * frequency is not valid due to DVFS).
  780. * e.g.: when you need the number of cycles to guarantee you won't wait for
  781. * longer than 'us' time (you might have a shorter wait).
  782. */
  783. static inline u32 kbasep_js_convert_us_to_gpu_ticks_min_freq(struct kbase_device *kbdev, u32 us)
  784. {
  785. u32 gpu_freq = kbdev->gpu_props.props.core_props.gpu_freq_khz_min;
  786. KBASE_DEBUG_ASSERT(gpu_freq != 0);
  787. return us * (gpu_freq / 1000);
  788. }
  789. /**
  790. * This will provide a conversion from time (us) to ticks of the gpu clock
  791. * based on the maximum available gpu frequency.
  792. * This is usually good to compute best/worst case (where the use of current
  793. * frequency is not valid due to DVFS).
  794. * e.g.: When you need the number of cycles to guarantee you'll wait at least
  795. * 'us' amount of time (but you might wait longer).
  796. */
  797. static inline u32 kbasep_js_convert_us_to_gpu_ticks_max_freq(struct kbase_device *kbdev, u32 us)
  798. {
  799. u32 gpu_freq = kbdev->gpu_props.props.core_props.gpu_freq_khz_max;
  800. KBASE_DEBUG_ASSERT(gpu_freq != 0);
  801. return us * (u32) (gpu_freq / 1000);
  802. }
  803. /**
  804. * This will provide a conversion from ticks of the gpu clock to time (us)
  805. * based on the minimum available gpu frequency.
  806. * This is usually good to compute best/worst case (where the use of current
  807. * frequency is not valid due to DVFS).
  808. * e.g.: When you need to know the worst-case wait that 'ticks' cycles will
  809. * take (you guarantee that you won't wait any longer than this, but it may
  810. * be shorter).
  811. */
  812. static inline u32 kbasep_js_convert_gpu_ticks_to_us_min_freq(struct kbase_device *kbdev, u32 ticks)
  813. {
  814. u32 gpu_freq = kbdev->gpu_props.props.core_props.gpu_freq_khz_min;
  815. KBASE_DEBUG_ASSERT(gpu_freq != 0);
  816. return ticks / gpu_freq * 1000;
  817. }
  818. /**
  819. * This will provide a conversion from ticks of the gpu clock to time (us)
  820. * based on the maximum available gpu frequency.
  821. * This is usually good to compute best/worst case (where the use of current
  822. * frequency is not valid due to DVFS).
  823. * e.g.: When you need to know the best-case wait for 'tick' cycles (you
  824. * guarantee to be waiting for at least this long, but it may be longer).
  825. */
  826. static inline u32 kbasep_js_convert_gpu_ticks_to_us_max_freq(struct kbase_device *kbdev, u32 ticks)
  827. {
  828. u32 gpu_freq = kbdev->gpu_props.props.core_props.gpu_freq_khz_max;
  829. KBASE_DEBUG_ASSERT(gpu_freq != 0);
  830. return ticks / gpu_freq * 1000;
  831. }
  832. /*
  833. * The following locking conditions are made on the caller:
  834. * - The caller must hold the kbasep_js_kctx_info::ctx::jsctx_mutex.
  835. * - The caller must hold the kbasep_js_device_data::runpool_mutex
  836. */
  837. static inline void kbase_js_runpool_inc_context_count(
  838. struct kbase_device *kbdev,
  839. struct kbase_context *kctx)
  840. {
  841. struct kbasep_js_device_data *js_devdata;
  842. struct kbasep_js_kctx_info *js_kctx_info;
  843. KBASE_DEBUG_ASSERT(kbdev != NULL);
  844. KBASE_DEBUG_ASSERT(kctx != NULL);
  845. js_devdata = &kbdev->js_data;
  846. js_kctx_info = &kctx->jctx.sched_info;
  847. lockdep_assert_held(&js_kctx_info->ctx.jsctx_mutex);
  848. lockdep_assert_held(&js_devdata->runpool_mutex);
  849. /* Track total contexts */
  850. KBASE_DEBUG_ASSERT(js_devdata->nr_all_contexts_running < S8_MAX);
  851. ++(js_devdata->nr_all_contexts_running);
  852. if ((js_kctx_info->ctx.flags & KBASE_CTX_FLAG_SUBMIT_DISABLED) == 0) {
  853. /* Track contexts that can submit jobs */
  854. KBASE_DEBUG_ASSERT(js_devdata->nr_user_contexts_running <
  855. S8_MAX);
  856. ++(js_devdata->nr_user_contexts_running);
  857. }
  858. }
  859. /*
  860. * The following locking conditions are made on the caller:
  861. * - The caller must hold the kbasep_js_kctx_info::ctx::jsctx_mutex.
  862. * - The caller must hold the kbasep_js_device_data::runpool_mutex
  863. */
  864. static inline void kbase_js_runpool_dec_context_count(
  865. struct kbase_device *kbdev,
  866. struct kbase_context *kctx)
  867. {
  868. struct kbasep_js_device_data *js_devdata;
  869. struct kbasep_js_kctx_info *js_kctx_info;
  870. KBASE_DEBUG_ASSERT(kbdev != NULL);
  871. KBASE_DEBUG_ASSERT(kctx != NULL);
  872. js_devdata = &kbdev->js_data;
  873. js_kctx_info = &kctx->jctx.sched_info;
  874. lockdep_assert_held(&js_kctx_info->ctx.jsctx_mutex);
  875. lockdep_assert_held(&js_devdata->runpool_mutex);
  876. /* Track total contexts */
  877. --(js_devdata->nr_all_contexts_running);
  878. KBASE_DEBUG_ASSERT(js_devdata->nr_all_contexts_running >= 0);
  879. if ((js_kctx_info->ctx.flags & KBASE_CTX_FLAG_SUBMIT_DISABLED) == 0) {
  880. /* Track contexts that can submit jobs */
  881. --(js_devdata->nr_user_contexts_running);
  882. KBASE_DEBUG_ASSERT(js_devdata->nr_user_contexts_running >= 0);
  883. }
  884. }
  885. /**
  886. * @brief Submit atoms from all available contexts to all job slots.
  887. *
  888. * This will attempt to submit as many jobs as possible. It will exit when
  889. * either all job slots are full, or all contexts have been used.
  890. *
  891. * @param[in] kbdev Device pointer
  892. */
  893. static inline void kbase_js_sched_all(struct kbase_device *kbdev)
  894. {
  895. kbase_js_sched(kbdev, (1 << kbdev->gpu_props.num_job_slots) - 1);
  896. }
  897. extern const int
  898. kbasep_js_atom_priority_to_relative[BASE_JD_NR_PRIO_LEVELS];
  899. extern const base_jd_prio
  900. kbasep_js_relative_priority_to_atom[KBASE_JS_ATOM_SCHED_PRIO_COUNT];
  901. /**
  902. * kbasep_js_atom_prio_to_sched_prio(): - Convert atom priority (base_jd_prio)
  903. * to relative ordering
  904. * @atom_prio: Priority ID to translate.
  905. *
  906. * Atom priority values for @ref base_jd_prio cannot be compared directly to
  907. * find out which are higher or lower.
  908. *
  909. * This function will convert base_jd_prio values for successively lower
  910. * priorities into a monotonically increasing sequence. That is, the lower the
  911. * base_jd_prio priority, the higher the value produced by this function. This
  912. * is in accordance with how the rest of the kernel treates priority.
  913. *
  914. * The mapping is 1:1 and the size of the valid input range is the same as the
  915. * size of the valid output range, i.e.
  916. * KBASE_JS_ATOM_SCHED_PRIO_COUNT == BASE_JD_NR_PRIO_LEVELS
  917. *
  918. * Note This must be kept in sync with BASE_JD_PRIO_<...> definitions
  919. *
  920. * Return: On success: a value in the inclusive range
  921. * 0..KBASE_JS_ATOM_SCHED_PRIO_COUNT-1. On failure:
  922. * KBASE_JS_ATOM_SCHED_PRIO_INVALID
  923. */
  924. static inline int kbasep_js_atom_prio_to_sched_prio(base_jd_prio atom_prio)
  925. {
  926. if (atom_prio >= BASE_JD_NR_PRIO_LEVELS)
  927. return KBASE_JS_ATOM_SCHED_PRIO_INVALID;
  928. return kbasep_js_atom_priority_to_relative[atom_prio];
  929. }
  930. static inline base_jd_prio kbasep_js_sched_prio_to_atom_prio(int sched_prio)
  931. {
  932. unsigned int prio_idx;
  933. KBASE_DEBUG_ASSERT(0 <= sched_prio
  934. && sched_prio < KBASE_JS_ATOM_SCHED_PRIO_COUNT);
  935. prio_idx = (unsigned int)sched_prio;
  936. return kbasep_js_relative_priority_to_atom[prio_idx];
  937. }
  938. /** @} *//* end group kbase_js */
  939. /** @} *//* end group base_kbase_api */
  940. /** @} *//* end group base_api */
  941. #endif // ifndef _KBASE_JS_H_