pvr_queue.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. // SPDX-License-Identifier: GPL-2.0-only OR MIT
  2. /* Copyright (c) 2023 Imagination Technologies Ltd. */
  3. #include <drm/drm_managed.h>
  4. #include <drm/gpu_scheduler.h>
  5. #include "pvr_cccb.h"
  6. #include "pvr_context.h"
  7. #include "pvr_device.h"
  8. #include "pvr_drv.h"
  9. #include "pvr_job.h"
  10. #include "pvr_queue.h"
  11. #include "pvr_vm.h"
  12. #include "pvr_rogue_fwif_client.h"
  13. #define MAX_DEADLINE_MS 30000
  14. #define CTX_COMPUTE_CCCB_SIZE_LOG2 15
  15. #define CTX_FRAG_CCCB_SIZE_LOG2 15
  16. #define CTX_GEOM_CCCB_SIZE_LOG2 15
  17. #define CTX_TRANSFER_CCCB_SIZE_LOG2 15
  18. static int get_xfer_ctx_state_size(struct pvr_device *pvr_dev)
  19. {
  20. u32 num_isp_store_registers;
  21. if (PVR_HAS_FEATURE(pvr_dev, xe_memory_hierarchy)) {
  22. num_isp_store_registers = 1;
  23. } else {
  24. int err;
  25. err = PVR_FEATURE_VALUE(pvr_dev, num_isp_ipp_pipes, &num_isp_store_registers);
  26. if (WARN_ON(err))
  27. return err;
  28. }
  29. return sizeof(struct rogue_fwif_frag_ctx_state) +
  30. (num_isp_store_registers *
  31. sizeof(((struct rogue_fwif_frag_ctx_state *)0)->frag_reg_isp_store[0]));
  32. }
  33. static int get_frag_ctx_state_size(struct pvr_device *pvr_dev)
  34. {
  35. u32 num_isp_store_registers;
  36. int err;
  37. if (PVR_HAS_FEATURE(pvr_dev, xe_memory_hierarchy)) {
  38. err = PVR_FEATURE_VALUE(pvr_dev, num_raster_pipes, &num_isp_store_registers);
  39. if (WARN_ON(err))
  40. return err;
  41. if (PVR_HAS_FEATURE(pvr_dev, gpu_multicore_support)) {
  42. u32 xpu_max_slaves;
  43. err = PVR_FEATURE_VALUE(pvr_dev, xpu_max_slaves, &xpu_max_slaves);
  44. if (WARN_ON(err))
  45. return err;
  46. num_isp_store_registers *= (1 + xpu_max_slaves);
  47. }
  48. } else {
  49. err = PVR_FEATURE_VALUE(pvr_dev, num_isp_ipp_pipes, &num_isp_store_registers);
  50. if (WARN_ON(err))
  51. return err;
  52. }
  53. return sizeof(struct rogue_fwif_frag_ctx_state) +
  54. (num_isp_store_registers *
  55. sizeof(((struct rogue_fwif_frag_ctx_state *)0)->frag_reg_isp_store[0]));
  56. }
  57. static int get_ctx_state_size(struct pvr_device *pvr_dev, enum drm_pvr_job_type type)
  58. {
  59. switch (type) {
  60. case DRM_PVR_JOB_TYPE_GEOMETRY:
  61. return sizeof(struct rogue_fwif_geom_ctx_state);
  62. case DRM_PVR_JOB_TYPE_FRAGMENT:
  63. return get_frag_ctx_state_size(pvr_dev);
  64. case DRM_PVR_JOB_TYPE_COMPUTE:
  65. return sizeof(struct rogue_fwif_compute_ctx_state);
  66. case DRM_PVR_JOB_TYPE_TRANSFER_FRAG:
  67. return get_xfer_ctx_state_size(pvr_dev);
  68. }
  69. WARN(1, "Invalid queue type");
  70. return -EINVAL;
  71. }
  72. static u32 get_ctx_offset(enum drm_pvr_job_type type)
  73. {
  74. switch (type) {
  75. case DRM_PVR_JOB_TYPE_GEOMETRY:
  76. return offsetof(struct rogue_fwif_fwrendercontext, geom_context);
  77. case DRM_PVR_JOB_TYPE_FRAGMENT:
  78. return offsetof(struct rogue_fwif_fwrendercontext, frag_context);
  79. case DRM_PVR_JOB_TYPE_COMPUTE:
  80. return offsetof(struct rogue_fwif_fwcomputecontext, cdm_context);
  81. case DRM_PVR_JOB_TYPE_TRANSFER_FRAG:
  82. return offsetof(struct rogue_fwif_fwtransfercontext, tq_context);
  83. }
  84. return 0;
  85. }
  86. static const char *
  87. pvr_queue_fence_get_driver_name(struct dma_fence *f)
  88. {
  89. return PVR_DRIVER_NAME;
  90. }
  91. static void pvr_queue_fence_release_work(struct work_struct *w)
  92. {
  93. struct pvr_queue_fence *fence = container_of(w, struct pvr_queue_fence, release_work);
  94. pvr_context_put(fence->queue->ctx);
  95. dma_fence_free(&fence->base);
  96. }
  97. static void pvr_queue_fence_release(struct dma_fence *f)
  98. {
  99. struct pvr_queue_fence *fence = container_of(f, struct pvr_queue_fence, base);
  100. struct pvr_device *pvr_dev = fence->queue->ctx->pvr_dev;
  101. queue_work(pvr_dev->sched_wq, &fence->release_work);
  102. }
  103. static const char *
  104. pvr_queue_job_fence_get_timeline_name(struct dma_fence *f)
  105. {
  106. struct pvr_queue_fence *fence = container_of(f, struct pvr_queue_fence, base);
  107. switch (fence->queue->type) {
  108. case DRM_PVR_JOB_TYPE_GEOMETRY:
  109. return "geometry";
  110. case DRM_PVR_JOB_TYPE_FRAGMENT:
  111. return "fragment";
  112. case DRM_PVR_JOB_TYPE_COMPUTE:
  113. return "compute";
  114. case DRM_PVR_JOB_TYPE_TRANSFER_FRAG:
  115. return "transfer";
  116. }
  117. WARN(1, "Invalid queue type");
  118. return "invalid";
  119. }
  120. static const char *
  121. pvr_queue_cccb_fence_get_timeline_name(struct dma_fence *f)
  122. {
  123. struct pvr_queue_fence *fence = container_of(f, struct pvr_queue_fence, base);
  124. switch (fence->queue->type) {
  125. case DRM_PVR_JOB_TYPE_GEOMETRY:
  126. return "geometry-cccb";
  127. case DRM_PVR_JOB_TYPE_FRAGMENT:
  128. return "fragment-cccb";
  129. case DRM_PVR_JOB_TYPE_COMPUTE:
  130. return "compute-cccb";
  131. case DRM_PVR_JOB_TYPE_TRANSFER_FRAG:
  132. return "transfer-cccb";
  133. }
  134. WARN(1, "Invalid queue type");
  135. return "invalid";
  136. }
  137. static const struct dma_fence_ops pvr_queue_job_fence_ops = {
  138. .get_driver_name = pvr_queue_fence_get_driver_name,
  139. .get_timeline_name = pvr_queue_job_fence_get_timeline_name,
  140. .release = pvr_queue_fence_release,
  141. };
  142. /**
  143. * to_pvr_queue_job_fence() - Return a pvr_queue_fence object if the fence is
  144. * backed by a UFO.
  145. * @f: The dma_fence to turn into a pvr_queue_fence.
  146. *
  147. * Return:
  148. * * A non-NULL pvr_queue_fence object if the dma_fence is backed by a UFO, or
  149. * * NULL otherwise.
  150. */
  151. static struct pvr_queue_fence *
  152. to_pvr_queue_job_fence(struct dma_fence *f)
  153. {
  154. struct drm_sched_fence *sched_fence = to_drm_sched_fence(f);
  155. if (sched_fence)
  156. f = sched_fence->parent;
  157. if (f && f->ops == &pvr_queue_job_fence_ops)
  158. return container_of(f, struct pvr_queue_fence, base);
  159. return NULL;
  160. }
  161. static const struct dma_fence_ops pvr_queue_cccb_fence_ops = {
  162. .get_driver_name = pvr_queue_fence_get_driver_name,
  163. .get_timeline_name = pvr_queue_cccb_fence_get_timeline_name,
  164. .release = pvr_queue_fence_release,
  165. };
  166. /**
  167. * pvr_queue_fence_put() - Put wrapper for pvr_queue_fence objects.
  168. * @f: The dma_fence object to put.
  169. *
  170. * If the pvr_queue_fence has been initialized, we call dma_fence_put(),
  171. * otherwise we free the object with dma_fence_free(). This allows us
  172. * to do the right thing before and after pvr_queue_fence_init() had been
  173. * called.
  174. */
  175. static void pvr_queue_fence_put(struct dma_fence *f)
  176. {
  177. if (!f)
  178. return;
  179. if (WARN_ON(f->ops &&
  180. f->ops != &pvr_queue_cccb_fence_ops &&
  181. f->ops != &pvr_queue_job_fence_ops))
  182. return;
  183. /* If the fence hasn't been initialized yet, free the object directly. */
  184. if (f->ops)
  185. dma_fence_put(f);
  186. else
  187. dma_fence_free(f);
  188. }
  189. /**
  190. * pvr_queue_fence_alloc() - Allocate a pvr_queue_fence fence object
  191. *
  192. * Call this function to allocate job CCCB and done fences. This only
  193. * allocates the objects. Initialization happens when the underlying
  194. * dma_fence object is to be returned to drm_sched (in prepare_job() or
  195. * run_job()).
  196. *
  197. * Return:
  198. * * A valid pointer if the allocation succeeds, or
  199. * * NULL if the allocation fails.
  200. */
  201. static struct dma_fence *
  202. pvr_queue_fence_alloc(void)
  203. {
  204. struct pvr_queue_fence *fence;
  205. fence = kzalloc(sizeof(*fence), GFP_KERNEL);
  206. if (!fence)
  207. return NULL;
  208. return &fence->base;
  209. }
  210. /**
  211. * pvr_queue_fence_init() - Initializes a pvr_queue_fence object.
  212. * @f: The fence to initialize
  213. * @queue: The queue this fence belongs to.
  214. * @fence_ops: The fence operations.
  215. * @fence_ctx: The fence context.
  216. *
  217. * Wrapper around dma_fence_init() that takes care of initializing the
  218. * pvr_queue_fence::queue field too.
  219. */
  220. static void
  221. pvr_queue_fence_init(struct dma_fence *f,
  222. struct pvr_queue *queue,
  223. const struct dma_fence_ops *fence_ops,
  224. struct pvr_queue_fence_ctx *fence_ctx)
  225. {
  226. struct pvr_queue_fence *fence = container_of(f, struct pvr_queue_fence, base);
  227. pvr_context_get(queue->ctx);
  228. fence->queue = queue;
  229. INIT_WORK(&fence->release_work, pvr_queue_fence_release_work);
  230. dma_fence_init(&fence->base, fence_ops,
  231. &fence_ctx->lock, fence_ctx->id,
  232. atomic_inc_return(&fence_ctx->seqno));
  233. }
  234. /**
  235. * pvr_queue_cccb_fence_init() - Initializes a CCCB fence object.
  236. * @fence: The fence to initialize.
  237. * @queue: The queue this fence belongs to.
  238. *
  239. * Initializes a fence that can be used to wait for CCCB space.
  240. *
  241. * Should be called in the ::prepare_job() path, so the fence returned to
  242. * drm_sched is valid.
  243. */
  244. static void
  245. pvr_queue_cccb_fence_init(struct dma_fence *fence, struct pvr_queue *queue)
  246. {
  247. pvr_queue_fence_init(fence, queue, &pvr_queue_cccb_fence_ops,
  248. &queue->cccb_fence_ctx.base);
  249. }
  250. /**
  251. * pvr_queue_job_fence_init() - Initializes a job done fence object.
  252. * @fence: The fence to initialize.
  253. * @queue: The queue this fence belongs to.
  254. *
  255. * Initializes a fence that will be signaled when the GPU is done executing
  256. * a job.
  257. *
  258. * Should be called *before* the ::run_job() path, so the fence is initialised
  259. * before being placed in the pending_list.
  260. */
  261. static void
  262. pvr_queue_job_fence_init(struct dma_fence *fence, struct pvr_queue *queue)
  263. {
  264. if (!fence->ops)
  265. pvr_queue_fence_init(fence, queue, &pvr_queue_job_fence_ops,
  266. &queue->job_fence_ctx);
  267. }
  268. /**
  269. * pvr_queue_fence_ctx_init() - Queue fence context initialization.
  270. * @fence_ctx: The context to initialize
  271. */
  272. static void
  273. pvr_queue_fence_ctx_init(struct pvr_queue_fence_ctx *fence_ctx)
  274. {
  275. spin_lock_init(&fence_ctx->lock);
  276. fence_ctx->id = dma_fence_context_alloc(1);
  277. atomic_set(&fence_ctx->seqno, 0);
  278. }
  279. static u32 ufo_cmds_size(u32 elem_count)
  280. {
  281. /* We can pass at most ROGUE_FWIF_CCB_CMD_MAX_UFOS per UFO-related command. */
  282. u32 full_cmd_count = elem_count / ROGUE_FWIF_CCB_CMD_MAX_UFOS;
  283. u32 remaining_elems = elem_count % ROGUE_FWIF_CCB_CMD_MAX_UFOS;
  284. u32 size = full_cmd_count *
  285. pvr_cccb_get_size_of_cmd_with_hdr(ROGUE_FWIF_CCB_CMD_MAX_UFOS *
  286. sizeof(struct rogue_fwif_ufo));
  287. if (remaining_elems) {
  288. size += pvr_cccb_get_size_of_cmd_with_hdr(remaining_elems *
  289. sizeof(struct rogue_fwif_ufo));
  290. }
  291. return size;
  292. }
  293. static u32 job_cmds_size(struct pvr_job *job, u32 ufo_wait_count)
  294. {
  295. /* One UFO cmd for the fence signaling, one UFO cmd per native fence native,
  296. * and a command for the job itself.
  297. */
  298. return ufo_cmds_size(1) + ufo_cmds_size(ufo_wait_count) +
  299. pvr_cccb_get_size_of_cmd_with_hdr(job->cmd_len);
  300. }
  301. /**
  302. * job_count_remaining_native_deps() - Count the number of non-signaled native dependencies.
  303. * @job: Job to operate on.
  304. *
  305. * Returns: Number of non-signaled native deps remaining.
  306. */
  307. static unsigned long job_count_remaining_native_deps(struct pvr_job *job)
  308. {
  309. unsigned long remaining_count = 0;
  310. struct dma_fence *fence = NULL;
  311. unsigned long index;
  312. xa_for_each(&job->base.dependencies, index, fence) {
  313. struct pvr_queue_fence *jfence;
  314. jfence = to_pvr_queue_job_fence(fence);
  315. if (!jfence)
  316. continue;
  317. if (!dma_fence_is_signaled(&jfence->base))
  318. remaining_count++;
  319. }
  320. return remaining_count;
  321. }
  322. /**
  323. * pvr_queue_get_job_cccb_fence() - Get the CCCB fence attached to a job.
  324. * @queue: The queue this job will be submitted to.
  325. * @job: The job to get the CCCB fence on.
  326. *
  327. * The CCCB fence is a synchronization primitive allowing us to delay job
  328. * submission until there's enough space in the CCCB to submit the job.
  329. *
  330. * Return:
  331. * * NULL if there's enough space in the CCCB to submit this job, or
  332. * * A valid dma_fence object otherwise.
  333. */
  334. static struct dma_fence *
  335. pvr_queue_get_job_cccb_fence(struct pvr_queue *queue, struct pvr_job *job)
  336. {
  337. struct pvr_queue_fence *cccb_fence;
  338. unsigned int native_deps_remaining;
  339. /* If the fence is NULL, that means we already checked that we had
  340. * enough space in the cccb for our job.
  341. */
  342. if (!job->cccb_fence)
  343. return NULL;
  344. mutex_lock(&queue->cccb_fence_ctx.job_lock);
  345. /* Count remaining native dependencies and check if the job fits in the CCCB. */
  346. native_deps_remaining = job_count_remaining_native_deps(job);
  347. if (pvr_cccb_cmdseq_fits(&queue->cccb, job_cmds_size(job, native_deps_remaining))) {
  348. pvr_queue_fence_put(job->cccb_fence);
  349. job->cccb_fence = NULL;
  350. goto out_unlock;
  351. }
  352. /* There should be no job attached to the CCCB fence context:
  353. * drm_sched_entity guarantees that jobs are submitted one at a time.
  354. */
  355. if (WARN_ON(queue->cccb_fence_ctx.job))
  356. pvr_job_put(queue->cccb_fence_ctx.job);
  357. queue->cccb_fence_ctx.job = pvr_job_get(job);
  358. /* Initialize the fence before returning it. */
  359. cccb_fence = container_of(job->cccb_fence, struct pvr_queue_fence, base);
  360. if (!WARN_ON(cccb_fence->queue))
  361. pvr_queue_cccb_fence_init(job->cccb_fence, queue);
  362. out_unlock:
  363. mutex_unlock(&queue->cccb_fence_ctx.job_lock);
  364. return dma_fence_get(job->cccb_fence);
  365. }
  366. /**
  367. * pvr_queue_get_job_kccb_fence() - Get the KCCB fence attached to a job.
  368. * @queue: The queue this job will be submitted to.
  369. * @job: The job to get the KCCB fence on.
  370. *
  371. * The KCCB fence is a synchronization primitive allowing us to delay job
  372. * submission until there's enough space in the KCCB to submit the job.
  373. *
  374. * Return:
  375. * * NULL if there's enough space in the KCCB to submit this job, or
  376. * * A valid dma_fence object otherwise.
  377. */
  378. static struct dma_fence *
  379. pvr_queue_get_job_kccb_fence(struct pvr_queue *queue, struct pvr_job *job)
  380. {
  381. struct pvr_device *pvr_dev = queue->ctx->pvr_dev;
  382. struct dma_fence *kccb_fence = NULL;
  383. /* If the fence is NULL, that means we already checked that we had
  384. * enough space in the KCCB for our job.
  385. */
  386. if (!job->kccb_fence)
  387. return NULL;
  388. if (!WARN_ON(job->kccb_fence->ops)) {
  389. kccb_fence = pvr_kccb_reserve_slot(pvr_dev, job->kccb_fence);
  390. job->kccb_fence = NULL;
  391. }
  392. return kccb_fence;
  393. }
  394. static struct dma_fence *
  395. pvr_queue_get_paired_frag_job_dep(struct pvr_queue *queue, struct pvr_job *job)
  396. {
  397. struct pvr_job *frag_job = job->type == DRM_PVR_JOB_TYPE_GEOMETRY ?
  398. job->paired_job : NULL;
  399. struct dma_fence *f;
  400. unsigned long index;
  401. if (!frag_job)
  402. return NULL;
  403. xa_for_each(&frag_job->base.dependencies, index, f) {
  404. /* Skip already signaled fences. */
  405. if (dma_fence_is_signaled(f))
  406. continue;
  407. /* Skip our own fence. */
  408. if (f == &job->base.s_fence->scheduled)
  409. continue;
  410. return dma_fence_get(f);
  411. }
  412. return frag_job->base.sched->ops->prepare_job(&frag_job->base, &queue->entity);
  413. }
  414. /**
  415. * pvr_queue_prepare_job() - Return the next internal dependencies expressed as a dma_fence.
  416. * @sched_job: The job to query the next internal dependency on
  417. * @s_entity: The entity this job is queue on.
  418. *
  419. * After iterating over drm_sched_job::dependencies, drm_sched let the driver return
  420. * its own internal dependencies. We use this function to return our internal dependencies.
  421. */
  422. static struct dma_fence *
  423. pvr_queue_prepare_job(struct drm_sched_job *sched_job,
  424. struct drm_sched_entity *s_entity)
  425. {
  426. struct pvr_job *job = container_of(sched_job, struct pvr_job, base);
  427. struct pvr_queue *queue = container_of(s_entity, struct pvr_queue, entity);
  428. struct dma_fence *internal_dep = NULL;
  429. /*
  430. * Initialize the done_fence, so we can signal it. This must be done
  431. * here because otherwise by the time of run_job() the job will end up
  432. * in the pending list without a valid fence.
  433. */
  434. if (job->type == DRM_PVR_JOB_TYPE_FRAGMENT && job->paired_job) {
  435. /*
  436. * This will be called on a paired fragment job after being
  437. * submitted to firmware. We can tell if this is the case and
  438. * bail early from whether run_job() has been called on the
  439. * geometry job, which would issue a pm ref.
  440. */
  441. if (job->paired_job->has_pm_ref)
  442. return NULL;
  443. /*
  444. * In this case we need to use the job's own ctx to initialise
  445. * the done_fence. The other steps are done in the ctx of the
  446. * paired geometry job.
  447. */
  448. pvr_queue_job_fence_init(job->done_fence,
  449. job->ctx->queues.fragment);
  450. } else {
  451. pvr_queue_job_fence_init(job->done_fence, queue);
  452. }
  453. /* CCCB fence is used to make sure we have enough space in the CCCB to
  454. * submit our commands.
  455. */
  456. internal_dep = pvr_queue_get_job_cccb_fence(queue, job);
  457. /* KCCB fence is used to make sure we have a KCCB slot to queue our
  458. * CMD_KICK.
  459. */
  460. if (!internal_dep)
  461. internal_dep = pvr_queue_get_job_kccb_fence(queue, job);
  462. /* Any extra internal dependency should be added here, using the following
  463. * pattern:
  464. *
  465. * if (!internal_dep)
  466. * internal_dep = pvr_queue_get_job_xxxx_fence(queue, job);
  467. */
  468. /* The paired job fence should come last, when everything else is ready. */
  469. if (!internal_dep)
  470. internal_dep = pvr_queue_get_paired_frag_job_dep(queue, job);
  471. return internal_dep;
  472. }
  473. /**
  474. * pvr_queue_update_active_state_locked() - Update the queue active state.
  475. * @queue: Queue to update the state on.
  476. *
  477. * Locked version of pvr_queue_update_active_state(). Must be called with
  478. * pvr_device::queue::lock held.
  479. */
  480. static void pvr_queue_update_active_state_locked(struct pvr_queue *queue)
  481. {
  482. struct pvr_device *pvr_dev = queue->ctx->pvr_dev;
  483. lockdep_assert_held(&pvr_dev->queues.lock);
  484. /* The queue is temporary out of any list when it's being reset,
  485. * we don't want a call to pvr_queue_update_active_state_locked()
  486. * to re-insert it behind our back.
  487. */
  488. if (list_empty(&queue->node))
  489. return;
  490. if (!atomic_read(&queue->in_flight_job_count))
  491. list_move_tail(&queue->node, &pvr_dev->queues.idle);
  492. else
  493. list_move_tail(&queue->node, &pvr_dev->queues.active);
  494. }
  495. /**
  496. * pvr_queue_update_active_state() - Update the queue active state.
  497. * @queue: Queue to update the state on.
  498. *
  499. * Active state is based on the in_flight_job_count value.
  500. *
  501. * Updating the active state implies moving the queue in or out of the
  502. * active queue list, which also defines whether the queue is checked
  503. * or not when a FW event is received.
  504. *
  505. * This function should be called any time a job is submitted or it done
  506. * fence is signaled.
  507. */
  508. static void pvr_queue_update_active_state(struct pvr_queue *queue)
  509. {
  510. struct pvr_device *pvr_dev = queue->ctx->pvr_dev;
  511. mutex_lock(&pvr_dev->queues.lock);
  512. pvr_queue_update_active_state_locked(queue);
  513. mutex_unlock(&pvr_dev->queues.lock);
  514. }
  515. static void pvr_queue_submit_job_to_cccb(struct pvr_job *job)
  516. {
  517. struct pvr_queue *queue = container_of(job->base.sched, struct pvr_queue, scheduler);
  518. struct rogue_fwif_ufo ufos[ROGUE_FWIF_CCB_CMD_MAX_UFOS];
  519. struct pvr_cccb *cccb = &queue->cccb;
  520. struct pvr_queue_fence *jfence;
  521. struct dma_fence *fence;
  522. unsigned long index;
  523. u32 ufo_count = 0;
  524. /* We need to add the queue to the active list before updating the CCCB,
  525. * otherwise we might miss the FW event informing us that something
  526. * happened on this queue.
  527. */
  528. atomic_inc(&queue->in_flight_job_count);
  529. pvr_queue_update_active_state(queue);
  530. xa_for_each(&job->base.dependencies, index, fence) {
  531. jfence = to_pvr_queue_job_fence(fence);
  532. if (!jfence)
  533. continue;
  534. /* Skip the partial render fence, we will place it at the end. */
  535. if (job->type == DRM_PVR_JOB_TYPE_FRAGMENT && job->paired_job &&
  536. &job->paired_job->base.s_fence->scheduled == fence)
  537. continue;
  538. if (dma_fence_is_signaled(&jfence->base))
  539. continue;
  540. pvr_fw_object_get_fw_addr(jfence->queue->timeline_ufo.fw_obj,
  541. &ufos[ufo_count].addr);
  542. ufos[ufo_count++].value = jfence->base.seqno;
  543. if (ufo_count == ARRAY_SIZE(ufos)) {
  544. pvr_cccb_write_command_with_header(cccb, ROGUE_FWIF_CCB_CMD_TYPE_FENCE_PR,
  545. sizeof(ufos), ufos, 0, 0);
  546. ufo_count = 0;
  547. }
  548. }
  549. /* Partial render fence goes last. */
  550. if (job->type == DRM_PVR_JOB_TYPE_FRAGMENT && job->paired_job) {
  551. jfence = to_pvr_queue_job_fence(job->paired_job->done_fence);
  552. if (!WARN_ON(!jfence)) {
  553. pvr_fw_object_get_fw_addr(jfence->queue->timeline_ufo.fw_obj,
  554. &ufos[ufo_count].addr);
  555. ufos[ufo_count++].value = job->paired_job->done_fence->seqno;
  556. }
  557. }
  558. if (ufo_count) {
  559. pvr_cccb_write_command_with_header(cccb, ROGUE_FWIF_CCB_CMD_TYPE_FENCE_PR,
  560. sizeof(ufos[0]) * ufo_count, ufos, 0, 0);
  561. }
  562. if (job->type == DRM_PVR_JOB_TYPE_GEOMETRY && job->paired_job) {
  563. struct rogue_fwif_cmd_geom *cmd = job->cmd;
  564. /* Reference value for the partial render test is the current queue fence
  565. * seqno minus one.
  566. */
  567. pvr_fw_object_get_fw_addr(queue->timeline_ufo.fw_obj,
  568. &cmd->partial_render_geom_frag_fence.addr);
  569. cmd->partial_render_geom_frag_fence.value = job->done_fence->seqno - 1;
  570. }
  571. /* Submit job to FW */
  572. pvr_cccb_write_command_with_header(cccb, job->fw_ccb_cmd_type, job->cmd_len, job->cmd,
  573. job->id, job->id);
  574. /* Signal the job fence. */
  575. pvr_fw_object_get_fw_addr(queue->timeline_ufo.fw_obj, &ufos[0].addr);
  576. ufos[0].value = job->done_fence->seqno;
  577. pvr_cccb_write_command_with_header(cccb, ROGUE_FWIF_CCB_CMD_TYPE_UPDATE,
  578. sizeof(ufos[0]), ufos, 0, 0);
  579. }
  580. /**
  581. * pvr_queue_run_job() - Submit a job to the FW.
  582. * @sched_job: The job to submit.
  583. *
  584. * This function is called when all non-native dependencies have been met and
  585. * when the commands resulting from this job are guaranteed to fit in the CCCB.
  586. */
  587. static struct dma_fence *pvr_queue_run_job(struct drm_sched_job *sched_job)
  588. {
  589. struct pvr_job *job = container_of(sched_job, struct pvr_job, base);
  590. struct pvr_device *pvr_dev = job->pvr_dev;
  591. int err;
  592. /* The fragment job is issued along the geometry job when we use combined
  593. * geom+frag kicks. When we get there, we should simply return the
  594. * done_fence that's been initialized earlier.
  595. */
  596. if (job->paired_job && job->type == DRM_PVR_JOB_TYPE_FRAGMENT &&
  597. job->done_fence->ops) {
  598. return dma_fence_get(job->done_fence);
  599. }
  600. /* The only kind of jobs that can be paired are geometry and fragment, and
  601. * we bail out early if we see a fragment job that's paired with a geomtry
  602. * job.
  603. * Paired jobs must also target the same context and point to the same
  604. * HWRT.
  605. */
  606. if (WARN_ON(job->paired_job &&
  607. (job->type != DRM_PVR_JOB_TYPE_GEOMETRY ||
  608. job->paired_job->type != DRM_PVR_JOB_TYPE_FRAGMENT ||
  609. job->hwrt != job->paired_job->hwrt ||
  610. job->ctx != job->paired_job->ctx)))
  611. return ERR_PTR(-EINVAL);
  612. err = pvr_job_get_pm_ref(job);
  613. if (WARN_ON(err))
  614. return ERR_PTR(err);
  615. if (job->paired_job) {
  616. err = pvr_job_get_pm_ref(job->paired_job);
  617. if (WARN_ON(err))
  618. return ERR_PTR(err);
  619. }
  620. /* Submit our job to the CCCB */
  621. pvr_queue_submit_job_to_cccb(job);
  622. if (job->paired_job) {
  623. struct pvr_job *geom_job = job;
  624. struct pvr_job *frag_job = job->paired_job;
  625. struct pvr_queue *geom_queue = job->ctx->queues.geometry;
  626. struct pvr_queue *frag_queue = job->ctx->queues.fragment;
  627. /* Submit the fragment job along the geometry job and send a combined kick. */
  628. pvr_queue_submit_job_to_cccb(frag_job);
  629. pvr_cccb_send_kccb_combined_kick(pvr_dev,
  630. &geom_queue->cccb, &frag_queue->cccb,
  631. pvr_context_get_fw_addr(geom_job->ctx) +
  632. geom_queue->ctx_offset,
  633. pvr_context_get_fw_addr(frag_job->ctx) +
  634. frag_queue->ctx_offset,
  635. job->hwrt,
  636. frag_job->fw_ccb_cmd_type ==
  637. ROGUE_FWIF_CCB_CMD_TYPE_FRAG_PR);
  638. } else {
  639. struct pvr_queue *queue = container_of(job->base.sched,
  640. struct pvr_queue, scheduler);
  641. pvr_cccb_send_kccb_kick(pvr_dev, &queue->cccb,
  642. pvr_context_get_fw_addr(job->ctx) + queue->ctx_offset,
  643. job->hwrt);
  644. }
  645. return dma_fence_get(job->done_fence);
  646. }
  647. static void pvr_queue_stop(struct pvr_queue *queue, struct pvr_job *bad_job)
  648. {
  649. drm_sched_stop(&queue->scheduler, bad_job ? &bad_job->base : NULL);
  650. }
  651. static void pvr_queue_start(struct pvr_queue *queue)
  652. {
  653. struct pvr_job *job;
  654. /* Make sure we CPU-signal the UFO object, so other queues don't get
  655. * blocked waiting on it.
  656. */
  657. *queue->timeline_ufo.value = atomic_read(&queue->job_fence_ctx.seqno);
  658. list_for_each_entry(job, &queue->scheduler.pending_list, base.list) {
  659. if (dma_fence_is_signaled(job->done_fence)) {
  660. /* Jobs might have completed after drm_sched_stop() was called.
  661. * In that case, re-assign the parent field to the done_fence.
  662. */
  663. WARN_ON(job->base.s_fence->parent);
  664. job->base.s_fence->parent = dma_fence_get(job->done_fence);
  665. } else {
  666. /* If we had unfinished jobs, flag the entity as guilty so no
  667. * new job can be submitted.
  668. */
  669. atomic_set(&queue->ctx->faulty, 1);
  670. }
  671. }
  672. drm_sched_start(&queue->scheduler);
  673. }
  674. /**
  675. * pvr_queue_timedout_job() - Handle a job timeout event.
  676. * @s_job: The job this timeout occurred on.
  677. *
  678. * FIXME: We don't do anything here to unblock the situation, we just stop+start
  679. * the scheduler, and re-assign parent fences in the middle.
  680. *
  681. * Return:
  682. * * DRM_GPU_SCHED_STAT_NOMINAL.
  683. */
  684. static enum drm_gpu_sched_stat
  685. pvr_queue_timedout_job(struct drm_sched_job *s_job)
  686. {
  687. struct drm_gpu_scheduler *sched = s_job->sched;
  688. struct pvr_queue *queue = container_of(sched, struct pvr_queue, scheduler);
  689. struct pvr_device *pvr_dev = queue->ctx->pvr_dev;
  690. struct pvr_job *job;
  691. u32 job_count = 0;
  692. dev_err(sched->dev, "Job timeout\n");
  693. /* Before we stop the scheduler, make sure the queue is out of any list, so
  694. * any call to pvr_queue_update_active_state_locked() that might happen
  695. * until the scheduler is really stopped doesn't end up re-inserting the
  696. * queue in the active list. This would cause
  697. * pvr_queue_signal_done_fences() and drm_sched_stop() to race with each
  698. * other when accessing the pending_list, since drm_sched_stop() doesn't
  699. * grab the job_list_lock when modifying the list (it's assuming the
  700. * only other accessor is the scheduler, and it's safe to not grab the
  701. * lock since it's stopped).
  702. */
  703. mutex_lock(&pvr_dev->queues.lock);
  704. list_del_init(&queue->node);
  705. mutex_unlock(&pvr_dev->queues.lock);
  706. drm_sched_stop(sched, s_job);
  707. /* Re-assign job parent fences. */
  708. list_for_each_entry(job, &sched->pending_list, base.list) {
  709. job->base.s_fence->parent = dma_fence_get(job->done_fence);
  710. job_count++;
  711. }
  712. WARN_ON(atomic_read(&queue->in_flight_job_count) != job_count);
  713. /* Re-insert the queue in the proper list, and kick a queue processing
  714. * operation if there were jobs pending.
  715. */
  716. mutex_lock(&pvr_dev->queues.lock);
  717. if (!job_count) {
  718. list_move_tail(&queue->node, &pvr_dev->queues.idle);
  719. } else {
  720. atomic_set(&queue->in_flight_job_count, job_count);
  721. list_move_tail(&queue->node, &pvr_dev->queues.active);
  722. pvr_queue_process(queue);
  723. }
  724. mutex_unlock(&pvr_dev->queues.lock);
  725. drm_sched_start(sched);
  726. return DRM_GPU_SCHED_STAT_NOMINAL;
  727. }
  728. /**
  729. * pvr_queue_free_job() - Release the reference the scheduler had on a job object.
  730. * @sched_job: Job object to free.
  731. */
  732. static void pvr_queue_free_job(struct drm_sched_job *sched_job)
  733. {
  734. struct pvr_job *job = container_of(sched_job, struct pvr_job, base);
  735. drm_sched_job_cleanup(sched_job);
  736. if (job->type == DRM_PVR_JOB_TYPE_FRAGMENT && job->paired_job)
  737. pvr_job_put(job->paired_job);
  738. job->paired_job = NULL;
  739. pvr_job_put(job);
  740. }
  741. static const struct drm_sched_backend_ops pvr_queue_sched_ops = {
  742. .prepare_job = pvr_queue_prepare_job,
  743. .run_job = pvr_queue_run_job,
  744. .timedout_job = pvr_queue_timedout_job,
  745. .free_job = pvr_queue_free_job,
  746. };
  747. /**
  748. * pvr_queue_fence_is_ufo_backed() - Check if a dma_fence is backed by a UFO object
  749. * @f: Fence to test.
  750. *
  751. * A UFO-backed fence is a fence that can be signaled or waited upon FW-side.
  752. * pvr_job::done_fence objects are backed by the timeline UFO attached to the queue
  753. * they are pushed to, but those fences are not directly exposed to the outside
  754. * world, so we also need to check if the fence we're being passed is a
  755. * drm_sched_fence that was coming from our driver.
  756. */
  757. bool pvr_queue_fence_is_ufo_backed(struct dma_fence *f)
  758. {
  759. struct drm_sched_fence *sched_fence = f ? to_drm_sched_fence(f) : NULL;
  760. if (sched_fence &&
  761. sched_fence->sched->ops == &pvr_queue_sched_ops)
  762. return true;
  763. if (f && f->ops == &pvr_queue_job_fence_ops)
  764. return true;
  765. return false;
  766. }
  767. /**
  768. * pvr_queue_signal_done_fences() - Signal done fences.
  769. * @queue: Queue to check.
  770. *
  771. * Signal done fences of jobs whose seqno is less than the current value of
  772. * the UFO object attached to the queue.
  773. */
  774. static void
  775. pvr_queue_signal_done_fences(struct pvr_queue *queue)
  776. {
  777. struct pvr_job *job, *tmp_job;
  778. u32 cur_seqno;
  779. spin_lock(&queue->scheduler.job_list_lock);
  780. cur_seqno = *queue->timeline_ufo.value;
  781. list_for_each_entry_safe(job, tmp_job, &queue->scheduler.pending_list, base.list) {
  782. if ((int)(cur_seqno - lower_32_bits(job->done_fence->seqno)) < 0)
  783. break;
  784. if (!dma_fence_is_signaled(job->done_fence)) {
  785. dma_fence_signal(job->done_fence);
  786. pvr_job_release_pm_ref(job);
  787. atomic_dec(&queue->in_flight_job_count);
  788. }
  789. }
  790. spin_unlock(&queue->scheduler.job_list_lock);
  791. }
  792. /**
  793. * pvr_queue_check_job_waiting_for_cccb_space() - Check if the job waiting for CCCB space
  794. * can be unblocked
  795. * pushed to the CCCB
  796. * @queue: Queue to check
  797. *
  798. * If we have a job waiting for CCCB, and this job now fits in the CCCB, we signal
  799. * its CCCB fence, which should kick drm_sched.
  800. */
  801. static void
  802. pvr_queue_check_job_waiting_for_cccb_space(struct pvr_queue *queue)
  803. {
  804. struct pvr_queue_fence *cccb_fence;
  805. u32 native_deps_remaining;
  806. struct pvr_job *job;
  807. mutex_lock(&queue->cccb_fence_ctx.job_lock);
  808. job = queue->cccb_fence_ctx.job;
  809. if (!job)
  810. goto out_unlock;
  811. /* If we have a job attached to the CCCB fence context, its CCCB fence
  812. * shouldn't be NULL.
  813. */
  814. if (WARN_ON(!job->cccb_fence)) {
  815. job = NULL;
  816. goto out_unlock;
  817. }
  818. /* If we get there, CCCB fence has to be initialized. */
  819. cccb_fence = container_of(job->cccb_fence, struct pvr_queue_fence, base);
  820. if (WARN_ON(!cccb_fence->queue)) {
  821. job = NULL;
  822. goto out_unlock;
  823. }
  824. /* Evict signaled dependencies before checking for CCCB space.
  825. * If the job fits, signal the CCCB fence, this should unblock
  826. * the drm_sched_entity.
  827. */
  828. native_deps_remaining = job_count_remaining_native_deps(job);
  829. if (!pvr_cccb_cmdseq_fits(&queue->cccb, job_cmds_size(job, native_deps_remaining))) {
  830. job = NULL;
  831. goto out_unlock;
  832. }
  833. dma_fence_signal(job->cccb_fence);
  834. pvr_queue_fence_put(job->cccb_fence);
  835. job->cccb_fence = NULL;
  836. queue->cccb_fence_ctx.job = NULL;
  837. out_unlock:
  838. mutex_unlock(&queue->cccb_fence_ctx.job_lock);
  839. pvr_job_put(job);
  840. }
  841. /**
  842. * pvr_queue_process() - Process events that happened on a queue.
  843. * @queue: Queue to check
  844. *
  845. * Signal job fences and check if jobs waiting for CCCB space can be unblocked.
  846. */
  847. void pvr_queue_process(struct pvr_queue *queue)
  848. {
  849. lockdep_assert_held(&queue->ctx->pvr_dev->queues.lock);
  850. pvr_queue_check_job_waiting_for_cccb_space(queue);
  851. pvr_queue_signal_done_fences(queue);
  852. pvr_queue_update_active_state_locked(queue);
  853. }
  854. static u32 get_dm_type(struct pvr_queue *queue)
  855. {
  856. switch (queue->type) {
  857. case DRM_PVR_JOB_TYPE_GEOMETRY:
  858. return PVR_FWIF_DM_GEOM;
  859. case DRM_PVR_JOB_TYPE_TRANSFER_FRAG:
  860. case DRM_PVR_JOB_TYPE_FRAGMENT:
  861. return PVR_FWIF_DM_FRAG;
  862. case DRM_PVR_JOB_TYPE_COMPUTE:
  863. return PVR_FWIF_DM_CDM;
  864. }
  865. return ~0;
  866. }
  867. /**
  868. * init_fw_context() - Initializes the queue part of a FW context.
  869. * @queue: Queue object to initialize the FW context for.
  870. * @fw_ctx_map: The FW context CPU mapping.
  871. *
  872. * FW contexts are containing various states, one of them being a per-queue state
  873. * that needs to be initialized for each queue being exposed by a context. This
  874. * function takes care of that.
  875. */
  876. static void init_fw_context(struct pvr_queue *queue, void *fw_ctx_map)
  877. {
  878. struct pvr_context *ctx = queue->ctx;
  879. struct pvr_fw_object *fw_mem_ctx_obj = pvr_vm_get_fw_mem_context(ctx->vm_ctx);
  880. struct rogue_fwif_fwcommoncontext *cctx_fw;
  881. struct pvr_cccb *cccb = &queue->cccb;
  882. cctx_fw = fw_ctx_map + queue->ctx_offset;
  883. cctx_fw->ccbctl_fw_addr = cccb->ctrl_fw_addr;
  884. cctx_fw->ccb_fw_addr = cccb->cccb_fw_addr;
  885. cctx_fw->dm = get_dm_type(queue);
  886. cctx_fw->priority = ctx->priority;
  887. cctx_fw->priority_seq_num = 0;
  888. cctx_fw->max_deadline_ms = MAX_DEADLINE_MS;
  889. cctx_fw->pid = task_tgid_nr(current);
  890. cctx_fw->server_common_context_id = ctx->ctx_id;
  891. pvr_fw_object_get_fw_addr(fw_mem_ctx_obj, &cctx_fw->fw_mem_context_fw_addr);
  892. pvr_fw_object_get_fw_addr(queue->reg_state_obj, &cctx_fw->context_state_addr);
  893. }
  894. /**
  895. * pvr_queue_cleanup_fw_context() - Wait for the FW context to be idle and clean it up.
  896. * @queue: Queue on FW context to clean up.
  897. *
  898. * Return:
  899. * * 0 on success,
  900. * * Any error returned by pvr_fw_structure_cleanup() otherwise.
  901. */
  902. static int pvr_queue_cleanup_fw_context(struct pvr_queue *queue)
  903. {
  904. if (!queue->ctx->fw_obj)
  905. return 0;
  906. return pvr_fw_structure_cleanup(queue->ctx->pvr_dev,
  907. ROGUE_FWIF_CLEANUP_FWCOMMONCONTEXT,
  908. queue->ctx->fw_obj, queue->ctx_offset);
  909. }
  910. /**
  911. * pvr_queue_job_init() - Initialize queue related fields in a pvr_job object.
  912. * @job: The job to initialize.
  913. *
  914. * Bind the job to a queue and allocate memory to guarantee pvr_queue_job_arm()
  915. * and pvr_queue_job_push() can't fail. We also make sure the context type is
  916. * valid and the job can fit in the CCCB.
  917. *
  918. * Return:
  919. * * 0 on success, or
  920. * * An error code if something failed.
  921. */
  922. int pvr_queue_job_init(struct pvr_job *job)
  923. {
  924. /* Fragment jobs need at least one native fence wait on the geometry job fence. */
  925. u32 min_native_dep_count = job->type == DRM_PVR_JOB_TYPE_FRAGMENT ? 1 : 0;
  926. struct pvr_queue *queue;
  927. int err;
  928. if (atomic_read(&job->ctx->faulty))
  929. return -EIO;
  930. queue = pvr_context_get_queue_for_job(job->ctx, job->type);
  931. if (!queue)
  932. return -EINVAL;
  933. if (!pvr_cccb_cmdseq_can_fit(&queue->cccb, job_cmds_size(job, min_native_dep_count)))
  934. return -E2BIG;
  935. err = drm_sched_job_init(&job->base, &queue->entity, 1, THIS_MODULE);
  936. if (err)
  937. return err;
  938. job->cccb_fence = pvr_queue_fence_alloc();
  939. job->kccb_fence = pvr_kccb_fence_alloc();
  940. job->done_fence = pvr_queue_fence_alloc();
  941. if (!job->cccb_fence || !job->kccb_fence || !job->done_fence)
  942. return -ENOMEM;
  943. return 0;
  944. }
  945. /**
  946. * pvr_queue_job_arm() - Arm a job object.
  947. * @job: The job to arm.
  948. *
  949. * Initializes fences and return the drm_sched finished fence so it can
  950. * be exposed to the outside world. Once this function is called, you should
  951. * make sure the job is pushed using pvr_queue_job_push(), or guarantee that
  952. * no one grabbed a reference to the returned fence. The latter can happen if
  953. * we do multi-job submission, and something failed when creating/initializing
  954. * a job. In that case, we know the fence didn't leave the driver, and we
  955. * can thus guarantee nobody will wait on an dead fence object.
  956. *
  957. * Return:
  958. * * A dma_fence object.
  959. */
  960. struct dma_fence *pvr_queue_job_arm(struct pvr_job *job)
  961. {
  962. drm_sched_job_arm(&job->base);
  963. return &job->base.s_fence->finished;
  964. }
  965. /**
  966. * pvr_queue_job_cleanup() - Cleanup fence/scheduler related fields in the job object.
  967. * @job: The job to cleanup.
  968. *
  969. * Should be called in the job release path.
  970. */
  971. void pvr_queue_job_cleanup(struct pvr_job *job)
  972. {
  973. pvr_queue_fence_put(job->done_fence);
  974. pvr_queue_fence_put(job->cccb_fence);
  975. pvr_kccb_fence_put(job->kccb_fence);
  976. if (job->base.s_fence)
  977. drm_sched_job_cleanup(&job->base);
  978. }
  979. /**
  980. * pvr_queue_job_push() - Push a job to its queue.
  981. * @job: The job to push.
  982. *
  983. * Must be called after pvr_queue_job_init() and after all dependencies
  984. * have been added to the job. This will effectively queue the job to
  985. * the drm_sched_entity attached to the queue. We grab a reference on
  986. * the job object, so the caller is free to drop its reference when it's
  987. * done accessing the job object.
  988. */
  989. void pvr_queue_job_push(struct pvr_job *job)
  990. {
  991. struct pvr_queue *queue = container_of(job->base.sched, struct pvr_queue, scheduler);
  992. /* Keep track of the last queued job scheduled fence for combined submit. */
  993. dma_fence_put(queue->last_queued_job_scheduled_fence);
  994. queue->last_queued_job_scheduled_fence = dma_fence_get(&job->base.s_fence->scheduled);
  995. pvr_job_get(job);
  996. drm_sched_entity_push_job(&job->base);
  997. }
  998. static void reg_state_init(void *cpu_ptr, void *priv)
  999. {
  1000. struct pvr_queue *queue = priv;
  1001. if (queue->type == DRM_PVR_JOB_TYPE_GEOMETRY) {
  1002. struct rogue_fwif_geom_ctx_state *geom_ctx_state_fw = cpu_ptr;
  1003. geom_ctx_state_fw->geom_core[0].geom_reg_vdm_call_stack_pointer_init =
  1004. queue->callstack_addr;
  1005. }
  1006. }
  1007. /**
  1008. * pvr_queue_create() - Create a queue object.
  1009. * @ctx: The context this queue will be attached to.
  1010. * @type: The type of jobs being pushed to this queue.
  1011. * @args: The arguments passed to the context creation function.
  1012. * @fw_ctx_map: CPU mapping of the FW context object.
  1013. *
  1014. * Create a queue object that will be used to queue and track jobs.
  1015. *
  1016. * Return:
  1017. * * A valid pointer to a pvr_queue object, or
  1018. * * An error pointer if the creation/initialization failed.
  1019. */
  1020. struct pvr_queue *pvr_queue_create(struct pvr_context *ctx,
  1021. enum drm_pvr_job_type type,
  1022. struct drm_pvr_ioctl_create_context_args *args,
  1023. void *fw_ctx_map)
  1024. {
  1025. static const struct {
  1026. u32 cccb_size;
  1027. const char *name;
  1028. } props[] = {
  1029. [DRM_PVR_JOB_TYPE_GEOMETRY] = {
  1030. .cccb_size = CTX_GEOM_CCCB_SIZE_LOG2,
  1031. .name = "geometry",
  1032. },
  1033. [DRM_PVR_JOB_TYPE_FRAGMENT] = {
  1034. .cccb_size = CTX_FRAG_CCCB_SIZE_LOG2,
  1035. .name = "fragment"
  1036. },
  1037. [DRM_PVR_JOB_TYPE_COMPUTE] = {
  1038. .cccb_size = CTX_COMPUTE_CCCB_SIZE_LOG2,
  1039. .name = "compute"
  1040. },
  1041. [DRM_PVR_JOB_TYPE_TRANSFER_FRAG] = {
  1042. .cccb_size = CTX_TRANSFER_CCCB_SIZE_LOG2,
  1043. .name = "transfer_frag"
  1044. },
  1045. };
  1046. struct pvr_device *pvr_dev = ctx->pvr_dev;
  1047. struct drm_gpu_scheduler *sched;
  1048. struct pvr_queue *queue;
  1049. int ctx_state_size, err;
  1050. void *cpu_map;
  1051. if (WARN_ON(type >= sizeof(props)))
  1052. return ERR_PTR(-EINVAL);
  1053. switch (ctx->type) {
  1054. case DRM_PVR_CTX_TYPE_RENDER:
  1055. if (type != DRM_PVR_JOB_TYPE_GEOMETRY &&
  1056. type != DRM_PVR_JOB_TYPE_FRAGMENT)
  1057. return ERR_PTR(-EINVAL);
  1058. break;
  1059. case DRM_PVR_CTX_TYPE_COMPUTE:
  1060. if (type != DRM_PVR_JOB_TYPE_COMPUTE)
  1061. return ERR_PTR(-EINVAL);
  1062. break;
  1063. case DRM_PVR_CTX_TYPE_TRANSFER_FRAG:
  1064. if (type != DRM_PVR_JOB_TYPE_TRANSFER_FRAG)
  1065. return ERR_PTR(-EINVAL);
  1066. break;
  1067. default:
  1068. return ERR_PTR(-EINVAL);
  1069. }
  1070. ctx_state_size = get_ctx_state_size(pvr_dev, type);
  1071. if (ctx_state_size < 0)
  1072. return ERR_PTR(ctx_state_size);
  1073. queue = kzalloc(sizeof(*queue), GFP_KERNEL);
  1074. if (!queue)
  1075. return ERR_PTR(-ENOMEM);
  1076. queue->type = type;
  1077. queue->ctx_offset = get_ctx_offset(type);
  1078. queue->ctx = ctx;
  1079. queue->callstack_addr = args->callstack_addr;
  1080. sched = &queue->scheduler;
  1081. INIT_LIST_HEAD(&queue->node);
  1082. mutex_init(&queue->cccb_fence_ctx.job_lock);
  1083. pvr_queue_fence_ctx_init(&queue->cccb_fence_ctx.base);
  1084. pvr_queue_fence_ctx_init(&queue->job_fence_ctx);
  1085. err = pvr_cccb_init(pvr_dev, &queue->cccb, props[type].cccb_size, props[type].name);
  1086. if (err)
  1087. goto err_free_queue;
  1088. err = pvr_fw_object_create(pvr_dev, ctx_state_size,
  1089. PVR_BO_FW_FLAGS_DEVICE_UNCACHED,
  1090. reg_state_init, queue, &queue->reg_state_obj);
  1091. if (err)
  1092. goto err_cccb_fini;
  1093. init_fw_context(queue, fw_ctx_map);
  1094. if (type != DRM_PVR_JOB_TYPE_GEOMETRY && type != DRM_PVR_JOB_TYPE_FRAGMENT &&
  1095. args->callstack_addr) {
  1096. err = -EINVAL;
  1097. goto err_release_reg_state;
  1098. }
  1099. cpu_map = pvr_fw_object_create_and_map(pvr_dev, sizeof(*queue->timeline_ufo.value),
  1100. PVR_BO_FW_FLAGS_DEVICE_UNCACHED,
  1101. NULL, NULL, &queue->timeline_ufo.fw_obj);
  1102. if (IS_ERR(cpu_map)) {
  1103. err = PTR_ERR(cpu_map);
  1104. goto err_release_reg_state;
  1105. }
  1106. queue->timeline_ufo.value = cpu_map;
  1107. err = drm_sched_init(&queue->scheduler,
  1108. &pvr_queue_sched_ops,
  1109. pvr_dev->sched_wq, 1, 64 * 1024, 1,
  1110. msecs_to_jiffies(500),
  1111. pvr_dev->sched_wq, NULL, "pvr-queue",
  1112. pvr_dev->base.dev);
  1113. if (err)
  1114. goto err_release_ufo;
  1115. err = drm_sched_entity_init(&queue->entity,
  1116. DRM_SCHED_PRIORITY_KERNEL,
  1117. &sched, 1, &ctx->faulty);
  1118. if (err)
  1119. goto err_sched_fini;
  1120. mutex_lock(&pvr_dev->queues.lock);
  1121. list_add_tail(&queue->node, &pvr_dev->queues.idle);
  1122. mutex_unlock(&pvr_dev->queues.lock);
  1123. return queue;
  1124. err_sched_fini:
  1125. drm_sched_fini(&queue->scheduler);
  1126. err_release_ufo:
  1127. pvr_fw_object_unmap_and_destroy(queue->timeline_ufo.fw_obj);
  1128. err_release_reg_state:
  1129. pvr_fw_object_destroy(queue->reg_state_obj);
  1130. err_cccb_fini:
  1131. pvr_cccb_fini(&queue->cccb);
  1132. err_free_queue:
  1133. mutex_destroy(&queue->cccb_fence_ctx.job_lock);
  1134. kfree(queue);
  1135. return ERR_PTR(err);
  1136. }
  1137. void pvr_queue_device_pre_reset(struct pvr_device *pvr_dev)
  1138. {
  1139. struct pvr_queue *queue;
  1140. mutex_lock(&pvr_dev->queues.lock);
  1141. list_for_each_entry(queue, &pvr_dev->queues.idle, node)
  1142. pvr_queue_stop(queue, NULL);
  1143. list_for_each_entry(queue, &pvr_dev->queues.active, node)
  1144. pvr_queue_stop(queue, NULL);
  1145. mutex_unlock(&pvr_dev->queues.lock);
  1146. }
  1147. void pvr_queue_device_post_reset(struct pvr_device *pvr_dev)
  1148. {
  1149. struct pvr_queue *queue;
  1150. mutex_lock(&pvr_dev->queues.lock);
  1151. list_for_each_entry(queue, &pvr_dev->queues.active, node)
  1152. pvr_queue_start(queue);
  1153. list_for_each_entry(queue, &pvr_dev->queues.idle, node)
  1154. pvr_queue_start(queue);
  1155. mutex_unlock(&pvr_dev->queues.lock);
  1156. }
  1157. /**
  1158. * pvr_queue_kill() - Kill a queue.
  1159. * @queue: The queue to kill.
  1160. *
  1161. * Kill the queue so no new jobs can be pushed. Should be called when the
  1162. * context handle is destroyed. The queue object might last longer if jobs
  1163. * are still in flight and holding a reference to the context this queue
  1164. * belongs to.
  1165. */
  1166. void pvr_queue_kill(struct pvr_queue *queue)
  1167. {
  1168. drm_sched_entity_destroy(&queue->entity);
  1169. dma_fence_put(queue->last_queued_job_scheduled_fence);
  1170. queue->last_queued_job_scheduled_fence = NULL;
  1171. }
  1172. /**
  1173. * pvr_queue_destroy() - Destroy a queue.
  1174. * @queue: The queue to destroy.
  1175. *
  1176. * Cleanup the queue and free the resources attached to it. Should be
  1177. * called from the context release function.
  1178. */
  1179. void pvr_queue_destroy(struct pvr_queue *queue)
  1180. {
  1181. if (!queue)
  1182. return;
  1183. mutex_lock(&queue->ctx->pvr_dev->queues.lock);
  1184. list_del_init(&queue->node);
  1185. mutex_unlock(&queue->ctx->pvr_dev->queues.lock);
  1186. drm_sched_fini(&queue->scheduler);
  1187. drm_sched_entity_fini(&queue->entity);
  1188. if (WARN_ON(queue->last_queued_job_scheduled_fence))
  1189. dma_fence_put(queue->last_queued_job_scheduled_fence);
  1190. pvr_queue_cleanup_fw_context(queue);
  1191. pvr_fw_object_unmap_and_destroy(queue->timeline_ufo.fw_obj);
  1192. pvr_fw_object_destroy(queue->reg_state_obj);
  1193. pvr_cccb_fini(&queue->cccb);
  1194. mutex_destroy(&queue->cccb_fence_ctx.job_lock);
  1195. kfree(queue);
  1196. }
  1197. /**
  1198. * pvr_queue_device_init() - Device-level initialization of queue related fields.
  1199. * @pvr_dev: The device to initialize.
  1200. *
  1201. * Initializes all fields related to queue management in pvr_device.
  1202. *
  1203. * Return:
  1204. * * 0 on success, or
  1205. * * An error code on failure.
  1206. */
  1207. int pvr_queue_device_init(struct pvr_device *pvr_dev)
  1208. {
  1209. int err;
  1210. INIT_LIST_HEAD(&pvr_dev->queues.active);
  1211. INIT_LIST_HEAD(&pvr_dev->queues.idle);
  1212. err = drmm_mutex_init(from_pvr_device(pvr_dev), &pvr_dev->queues.lock);
  1213. if (err)
  1214. return err;
  1215. pvr_dev->sched_wq = alloc_workqueue("powervr-sched", WQ_UNBOUND, 0);
  1216. if (!pvr_dev->sched_wq)
  1217. return -ENOMEM;
  1218. return 0;
  1219. }
  1220. /**
  1221. * pvr_queue_device_fini() - Device-level cleanup of queue related fields.
  1222. * @pvr_dev: The device to cleanup.
  1223. *
  1224. * Cleanup/free all queue-related resources attached to a pvr_device object.
  1225. */
  1226. void pvr_queue_device_fini(struct pvr_device *pvr_dev)
  1227. {
  1228. destroy_workqueue(pvr_dev->sched_wq);
  1229. }