mali_kbase_replay.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  1. /*
  2. *
  3. * (C) COPYRIGHT 2014-2016 ARM Limited. All rights reserved.
  4. *
  5. * This program is free software and is provided to you under the terms of the
  6. * GNU General Public License version 2 as published by the Free Software
  7. * Foundation, and any use by you of this program is subject to the terms
  8. * of such GNU licence.
  9. *
  10. * A copy of the licence is included with the program, and can also be obtained
  11. * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  12. * Boston, MA 02110-1301, USA.
  13. *
  14. */
  15. /**
  16. * @file mali_kbase_replay.c
  17. * Replay soft job handlers
  18. */
  19. #include <linux/dma-mapping.h>
  20. #include <mali_kbase_config.h>
  21. #include <mali_kbase.h>
  22. #include <mali_kbase_mem.h>
  23. #include <mali_kbase_mem_linux.h>
  24. #define JOB_NOT_STARTED 0
  25. #define JOB_TYPE_NULL (1)
  26. #define JOB_TYPE_VERTEX (5)
  27. #define JOB_TYPE_TILER (7)
  28. #define JOB_TYPE_FUSED (8)
  29. #define JOB_TYPE_FRAGMENT (9)
  30. #define JOB_HEADER_32_FBD_OFFSET (31*4)
  31. #define JOB_HEADER_64_FBD_OFFSET (44*4)
  32. #define FBD_POINTER_MASK (~0x3f)
  33. #define SFBD_TILER_OFFSET (48*4)
  34. #define MFBD_TILER_OFFSET (14*4)
  35. #define FBD_HIERARCHY_WEIGHTS 8
  36. #define FBD_HIERARCHY_MASK_MASK 0x1fff
  37. #define FBD_TYPE 1
  38. #define HIERARCHY_WEIGHTS 13
  39. #define JOB_HEADER_ID_MAX 0xffff
  40. #define JOB_SOURCE_ID(status) (((status) >> 16) & 0xFFFF)
  41. #define JOB_POLYGON_LIST (0x03)
  42. struct fragment_job {
  43. struct job_descriptor_header header;
  44. u32 x[2];
  45. union {
  46. u64 _64;
  47. u32 _32;
  48. } fragment_fbd;
  49. };
  50. static void dump_job_head(struct kbase_context *kctx, char *head_str,
  51. struct job_descriptor_header *job)
  52. {
  53. #ifdef CONFIG_MALI_DEBUG
  54. dev_dbg(kctx->kbdev->dev, "%s\n", head_str);
  55. dev_dbg(kctx->kbdev->dev,
  56. "addr = %p\n"
  57. "exception_status = %x (Source ID: 0x%x Access: 0x%x Exception: 0x%x)\n"
  58. "first_incomplete_task = %x\n"
  59. "fault_pointer = %llx\n"
  60. "job_descriptor_size = %x\n"
  61. "job_type = %x\n"
  62. "job_barrier = %x\n"
  63. "_reserved_01 = %x\n"
  64. "_reserved_02 = %x\n"
  65. "_reserved_03 = %x\n"
  66. "_reserved_04/05 = %x,%x\n"
  67. "job_index = %x\n"
  68. "dependencies = %x,%x\n",
  69. job, job->exception_status,
  70. JOB_SOURCE_ID(job->exception_status),
  71. (job->exception_status >> 8) & 0x3,
  72. job->exception_status & 0xFF,
  73. job->first_incomplete_task,
  74. job->fault_pointer, job->job_descriptor_size,
  75. job->job_type, job->job_barrier, job->_reserved_01,
  76. job->_reserved_02, job->_reserved_03,
  77. job->_reserved_04, job->_reserved_05,
  78. job->job_index,
  79. job->job_dependency_index_1,
  80. job->job_dependency_index_2);
  81. if (job->job_descriptor_size)
  82. dev_dbg(kctx->kbdev->dev, "next = %llx\n",
  83. job->next_job._64);
  84. else
  85. dev_dbg(kctx->kbdev->dev, "next = %x\n",
  86. job->next_job._32);
  87. #endif // ifdef CONFIG_MALI_DEBUG
  88. }
  89. static int kbasep_replay_reset_sfbd(struct kbase_context *kctx,
  90. u64 fbd_address, u64 tiler_heap_free,
  91. u16 hierarchy_mask, u32 default_weight)
  92. {
  93. struct {
  94. u32 padding_1[1];
  95. u32 flags;
  96. u64 padding_2[2];
  97. u64 heap_free_address;
  98. u32 padding[8];
  99. u32 weights[FBD_HIERARCHY_WEIGHTS];
  100. } *fbd_tiler;
  101. struct kbase_vmap_struct map;
  102. dev_dbg(kctx->kbdev->dev, "fbd_address: %llx\n", fbd_address);
  103. fbd_tiler = kbase_vmap(kctx, fbd_address + SFBD_TILER_OFFSET,
  104. sizeof(*fbd_tiler), &map);
  105. if (!fbd_tiler) {
  106. dev_err(kctx->kbdev->dev, "kbasep_replay_reset_fbd: failed to map fbd\n");
  107. return -EINVAL;
  108. }
  109. #ifdef CONFIG_MALI_DEBUG
  110. dev_dbg(kctx->kbdev->dev,
  111. "FBD tiler:\n"
  112. "flags = %x\n"
  113. "heap_free_address = %llx\n",
  114. fbd_tiler->flags, fbd_tiler->heap_free_address);
  115. #endif // ifdef CONFIG_MALI_DEBUG
  116. if (hierarchy_mask) {
  117. u32 weights[HIERARCHY_WEIGHTS];
  118. u16 old_hierarchy_mask = fbd_tiler->flags &
  119. FBD_HIERARCHY_MASK_MASK;
  120. int i, j = 0;
  121. for (i = 0; i < HIERARCHY_WEIGHTS; i++) {
  122. if (old_hierarchy_mask & (1 << i)) {
  123. KBASE_DEBUG_ASSERT(j < FBD_HIERARCHY_WEIGHTS);
  124. weights[i] = fbd_tiler->weights[j++];
  125. } else {
  126. weights[i] = default_weight;
  127. }
  128. }
  129. dev_dbg(kctx->kbdev->dev, "Old hierarchy mask=%x New hierarchy mask=%x\n",
  130. old_hierarchy_mask, hierarchy_mask);
  131. for (i = 0; i < HIERARCHY_WEIGHTS; i++)
  132. dev_dbg(kctx->kbdev->dev, " Hierarchy weight %02d: %08x\n",
  133. i, weights[i]);
  134. j = 0;
  135. for (i = 0; i < HIERARCHY_WEIGHTS; i++) {
  136. if (hierarchy_mask & (1 << i)) {
  137. KBASE_DEBUG_ASSERT(j < FBD_HIERARCHY_WEIGHTS);
  138. dev_dbg(kctx->kbdev->dev, " Writing hierarchy level %02d (%08x) to %d\n",
  139. i, weights[i], j);
  140. fbd_tiler->weights[j++] = weights[i];
  141. }
  142. }
  143. for (; j < FBD_HIERARCHY_WEIGHTS; j++)
  144. fbd_tiler->weights[j] = 0;
  145. fbd_tiler->flags = hierarchy_mask | (1 << 16);
  146. }
  147. fbd_tiler->heap_free_address = tiler_heap_free;
  148. dev_dbg(kctx->kbdev->dev, "heap_free_address=%llx flags=%x\n",
  149. fbd_tiler->heap_free_address, fbd_tiler->flags);
  150. kbase_vunmap(kctx, &map);
  151. return 0;
  152. }
  153. static int kbasep_replay_reset_mfbd(struct kbase_context *kctx,
  154. u64 fbd_address, u64 tiler_heap_free,
  155. u16 hierarchy_mask, u32 default_weight)
  156. {
  157. struct kbase_vmap_struct map;
  158. struct {
  159. u32 padding_0;
  160. u32 flags;
  161. u64 padding_1[2];
  162. u64 heap_free_address;
  163. u64 padding_2;
  164. u32 weights[FBD_HIERARCHY_WEIGHTS];
  165. } *fbd_tiler;
  166. dev_dbg(kctx->kbdev->dev, "fbd_address: %llx\n", fbd_address);
  167. fbd_tiler = kbase_vmap(kctx, fbd_address + MFBD_TILER_OFFSET,
  168. sizeof(*fbd_tiler), &map);
  169. if (!fbd_tiler) {
  170. dev_err(kctx->kbdev->dev,
  171. "kbasep_replay_reset_fbd: failed to map fbd\n");
  172. return -EINVAL;
  173. }
  174. #ifdef CONFIG_MALI_DEBUG
  175. dev_dbg(kctx->kbdev->dev, "FBD tiler:\n"
  176. "flags = %x\n"
  177. "heap_free_address = %llx\n",
  178. fbd_tiler->flags,
  179. fbd_tiler->heap_free_address);
  180. #endif // ifdef CONFIG_MALI_DEBUG
  181. if (hierarchy_mask) {
  182. u32 weights[HIERARCHY_WEIGHTS];
  183. u16 old_hierarchy_mask = (fbd_tiler->flags) &
  184. FBD_HIERARCHY_MASK_MASK;
  185. int i, j = 0;
  186. for (i = 0; i < HIERARCHY_WEIGHTS; i++) {
  187. if (old_hierarchy_mask & (1 << i)) {
  188. KBASE_DEBUG_ASSERT(j < FBD_HIERARCHY_WEIGHTS);
  189. weights[i] = fbd_tiler->weights[j++];
  190. } else {
  191. weights[i] = default_weight;
  192. }
  193. }
  194. dev_dbg(kctx->kbdev->dev, "Old hierarchy mask=%x New hierarchy mask=%x\n",
  195. old_hierarchy_mask, hierarchy_mask);
  196. for (i = 0; i < HIERARCHY_WEIGHTS; i++)
  197. dev_dbg(kctx->kbdev->dev, " Hierarchy weight %02d: %08x\n",
  198. i, weights[i]);
  199. j = 0;
  200. for (i = 0; i < HIERARCHY_WEIGHTS; i++) {
  201. if (hierarchy_mask & (1 << i)) {
  202. KBASE_DEBUG_ASSERT(j < FBD_HIERARCHY_WEIGHTS);
  203. dev_dbg(kctx->kbdev->dev,
  204. " Writing hierarchy level %02d (%08x) to %d\n",
  205. i, weights[i], j);
  206. fbd_tiler->weights[j++] = weights[i];
  207. }
  208. }
  209. for (; j < FBD_HIERARCHY_WEIGHTS; j++)
  210. fbd_tiler->weights[j] = 0;
  211. fbd_tiler->flags = hierarchy_mask | (1 << 16);
  212. }
  213. fbd_tiler->heap_free_address = tiler_heap_free;
  214. kbase_vunmap(kctx, &map);
  215. return 0;
  216. }
  217. /**
  218. * @brief Reset the status of an FBD pointed to by a tiler job
  219. *
  220. * This performs two functions :
  221. * - Set the hierarchy mask
  222. * - Reset the tiler free heap address
  223. *
  224. * @param[in] kctx Context pointer
  225. * @param[in] job_header Address of job header to reset.
  226. * @param[in] tiler_heap_free The value to reset Tiler Heap Free to
  227. * @param[in] hierarchy_mask The hierarchy mask to use
  228. * @param[in] default_weight Default hierarchy weight to write when no other
  229. * weight is given in the FBD
  230. * @param[in] job_64 true if this job is using 64-bit
  231. * descriptors
  232. *
  233. * @return 0 on success, error code on failure
  234. */
  235. static int kbasep_replay_reset_tiler_job(struct kbase_context *kctx,
  236. u64 job_header, u64 tiler_heap_free,
  237. u16 hierarchy_mask, u32 default_weight, bool job_64)
  238. {
  239. struct kbase_vmap_struct map;
  240. u64 fbd_address;
  241. if (job_64) {
  242. u64 *job_ext;
  243. job_ext = kbase_vmap(kctx,
  244. job_header + JOB_HEADER_64_FBD_OFFSET,
  245. sizeof(*job_ext), &map);
  246. if (!job_ext) {
  247. dev_err(kctx->kbdev->dev, "kbasep_replay_reset_tiler_job: failed to map jc\n");
  248. return -EINVAL;
  249. }
  250. fbd_address = *job_ext;
  251. kbase_vunmap(kctx, &map);
  252. } else {
  253. u32 *job_ext;
  254. job_ext = kbase_vmap(kctx,
  255. job_header + JOB_HEADER_32_FBD_OFFSET,
  256. sizeof(*job_ext), &map);
  257. if (!job_ext) {
  258. dev_err(kctx->kbdev->dev, "kbasep_replay_reset_tiler_job: failed to map jc\n");
  259. return -EINVAL;
  260. }
  261. fbd_address = *job_ext;
  262. kbase_vunmap(kctx, &map);
  263. }
  264. if (fbd_address & FBD_TYPE) {
  265. return kbasep_replay_reset_mfbd(kctx,
  266. fbd_address & FBD_POINTER_MASK,
  267. tiler_heap_free,
  268. hierarchy_mask,
  269. default_weight);
  270. } else {
  271. return kbasep_replay_reset_sfbd(kctx,
  272. fbd_address & FBD_POINTER_MASK,
  273. tiler_heap_free,
  274. hierarchy_mask,
  275. default_weight);
  276. }
  277. }
  278. /**
  279. * @brief Reset the status of a job
  280. *
  281. * This performs the following functions :
  282. *
  283. * - Reset the Job Status field of each job to NOT_STARTED.
  284. * - Set the Job Type field of any Vertex Jobs to Null Job.
  285. * - For any jobs using an FBD, set the Tiler Heap Free field to the value of
  286. * the tiler_heap_free parameter, and set the hierarchy level mask to the
  287. * hier_mask parameter.
  288. * - Offset HW dependencies by the hw_job_id_offset parameter
  289. * - Set the Perform Job Barrier flag if this job is the first in the chain
  290. * - Read the address of the next job header
  291. *
  292. * @param[in] kctx Context pointer
  293. * @param[in,out] job_header Address of job header to reset. Set to address
  294. * of next job header on exit.
  295. * @param[in] prev_jc Previous job chain to link to, if this job is
  296. * the last in the chain.
  297. * @param[in] hw_job_id_offset Offset for HW job IDs
  298. * @param[in] tiler_heap_free The value to reset Tiler Heap Free to
  299. * @param[in] hierarchy_mask The hierarchy mask to use
  300. * @param[in] default_weight Default hierarchy weight to write when no other
  301. * weight is given in the FBD
  302. * @param[in] first_in_chain true if this job is the first in the chain
  303. * @param[in] fragment_chain true if this job is in the fragment chain
  304. *
  305. * @return 0 on success, error code on failure
  306. */
  307. static int kbasep_replay_reset_job(struct kbase_context *kctx,
  308. u64 *job_header, u64 prev_jc,
  309. u64 tiler_heap_free, u16 hierarchy_mask,
  310. u32 default_weight, u16 hw_job_id_offset,
  311. bool first_in_chain, bool fragment_chain)
  312. {
  313. struct fragment_job *frag_job;
  314. struct job_descriptor_header *job;
  315. u64 new_job_header;
  316. struct kbase_vmap_struct map;
  317. frag_job = kbase_vmap(kctx, *job_header, sizeof(*frag_job), &map);
  318. if (!frag_job) {
  319. dev_err(kctx->kbdev->dev,
  320. "kbasep_replay_parse_jc: failed to map jc\n");
  321. return -EINVAL;
  322. }
  323. job = &frag_job->header;
  324. dump_job_head(kctx, "Job header:", job);
  325. if (job->exception_status == JOB_NOT_STARTED && !fragment_chain) {
  326. dev_err(kctx->kbdev->dev, "Job already not started\n");
  327. goto out_unmap;
  328. }
  329. job->exception_status = JOB_NOT_STARTED;
  330. if (job->job_type == JOB_TYPE_VERTEX)
  331. job->job_type = JOB_TYPE_NULL;
  332. if (job->job_type == JOB_TYPE_FUSED) {
  333. dev_err(kctx->kbdev->dev, "Fused jobs can not be replayed\n");
  334. goto out_unmap;
  335. }
  336. if (first_in_chain)
  337. job->job_barrier = 1;
  338. if ((job->job_dependency_index_1 + hw_job_id_offset) >
  339. JOB_HEADER_ID_MAX ||
  340. (job->job_dependency_index_2 + hw_job_id_offset) >
  341. JOB_HEADER_ID_MAX ||
  342. (job->job_index + hw_job_id_offset) > JOB_HEADER_ID_MAX) {
  343. dev_err(kctx->kbdev->dev,
  344. "Job indicies/dependencies out of valid range\n");
  345. goto out_unmap;
  346. }
  347. if (job->job_dependency_index_1)
  348. job->job_dependency_index_1 += hw_job_id_offset;
  349. if (job->job_dependency_index_2)
  350. job->job_dependency_index_2 += hw_job_id_offset;
  351. job->job_index += hw_job_id_offset;
  352. if (job->job_descriptor_size) {
  353. new_job_header = job->next_job._64;
  354. if (!job->next_job._64)
  355. job->next_job._64 = prev_jc;
  356. } else {
  357. new_job_header = job->next_job._32;
  358. if (!job->next_job._32)
  359. job->next_job._32 = prev_jc;
  360. }
  361. dump_job_head(kctx, "Updated to:", job);
  362. if (job->job_type == JOB_TYPE_TILER) {
  363. bool job_64 = job->job_descriptor_size != 0;
  364. if (kbasep_replay_reset_tiler_job(kctx, *job_header,
  365. tiler_heap_free, hierarchy_mask,
  366. default_weight, job_64) != 0)
  367. goto out_unmap;
  368. } else if (job->job_type == JOB_TYPE_FRAGMENT) {
  369. u64 fbd_address;
  370. if (job->job_descriptor_size)
  371. fbd_address = frag_job->fragment_fbd._64;
  372. else
  373. fbd_address = (u64)frag_job->fragment_fbd._32;
  374. if (fbd_address & FBD_TYPE) {
  375. if (kbasep_replay_reset_mfbd(kctx,
  376. fbd_address & FBD_POINTER_MASK,
  377. tiler_heap_free,
  378. hierarchy_mask,
  379. default_weight) != 0)
  380. goto out_unmap;
  381. } else {
  382. if (kbasep_replay_reset_sfbd(kctx,
  383. fbd_address & FBD_POINTER_MASK,
  384. tiler_heap_free,
  385. hierarchy_mask,
  386. default_weight) != 0)
  387. goto out_unmap;
  388. }
  389. }
  390. kbase_vunmap(kctx, &map);
  391. *job_header = new_job_header;
  392. return 0;
  393. out_unmap:
  394. kbase_vunmap(kctx, &map);
  395. return -EINVAL;
  396. }
  397. /**
  398. * @brief Find the highest job ID in a job chain
  399. *
  400. * @param[in] kctx Context pointer
  401. * @param[in] jc Job chain start address
  402. * @param[out] hw_job_id Highest job ID in chain
  403. *
  404. * @return 0 on success, error code on failure
  405. */
  406. static int kbasep_replay_find_hw_job_id(struct kbase_context *kctx,
  407. u64 jc, u16 *hw_job_id)
  408. {
  409. while (jc) {
  410. struct job_descriptor_header *job;
  411. struct kbase_vmap_struct map;
  412. dev_dbg(kctx->kbdev->dev,
  413. "kbasep_replay_find_hw_job_id: parsing jc=%llx\n", jc);
  414. job = kbase_vmap(kctx, jc, sizeof(*job), &map);
  415. if (!job) {
  416. dev_err(kctx->kbdev->dev, "failed to map jc\n");
  417. return -EINVAL;
  418. }
  419. if (job->job_index > *hw_job_id)
  420. *hw_job_id = job->job_index;
  421. if (job->job_descriptor_size)
  422. jc = job->next_job._64;
  423. else
  424. jc = job->next_job._32;
  425. kbase_vunmap(kctx, &map);
  426. }
  427. return 0;
  428. }
  429. /**
  430. * @brief Reset the status of a number of jobs
  431. *
  432. * This function walks the provided job chain, and calls
  433. * kbasep_replay_reset_job for each job. It also links the job chain to the
  434. * provided previous job chain.
  435. *
  436. * The function will fail if any of the jobs passed already have status of
  437. * NOT_STARTED.
  438. *
  439. * @param[in] kctx Context pointer
  440. * @param[in] jc Job chain to be processed
  441. * @param[in] prev_jc Job chain to be added to. May be NULL
  442. * @param[in] tiler_heap_free The value to reset Tiler Heap Free to
  443. * @param[in] hierarchy_mask The hierarchy mask to use
  444. * @param[in] default_weight Default hierarchy weight to write when no other
  445. * weight is given in the FBD
  446. * @param[in] hw_job_id_offset Offset for HW job IDs
  447. * @param[in] fragment_chain true if this chain is the fragment chain
  448. *
  449. * @return 0 on success, error code otherwise
  450. */
  451. static int kbasep_replay_parse_jc(struct kbase_context *kctx,
  452. u64 jc, u64 prev_jc,
  453. u64 tiler_heap_free, u16 hierarchy_mask,
  454. u32 default_weight, u16 hw_job_id_offset,
  455. bool fragment_chain)
  456. {
  457. bool first_in_chain = true;
  458. int nr_jobs = 0;
  459. dev_dbg(kctx->kbdev->dev, "kbasep_replay_parse_jc: jc=%llx hw_job_id=%x\n",
  460. jc, hw_job_id_offset);
  461. while (jc) {
  462. dev_dbg(kctx->kbdev->dev, "kbasep_replay_parse_jc: parsing jc=%llx\n", jc);
  463. if (kbasep_replay_reset_job(kctx, &jc, prev_jc,
  464. tiler_heap_free, hierarchy_mask,
  465. default_weight, hw_job_id_offset,
  466. first_in_chain, fragment_chain) != 0)
  467. return -EINVAL;
  468. first_in_chain = false;
  469. nr_jobs++;
  470. if (fragment_chain &&
  471. nr_jobs >= BASE_JD_REPLAY_F_CHAIN_JOB_LIMIT) {
  472. dev_err(kctx->kbdev->dev,
  473. "Exceeded maximum number of jobs in fragment chain\n");
  474. return -EINVAL;
  475. }
  476. }
  477. return 0;
  478. }
  479. /**
  480. * @brief Reset the status of a replay job, and set up dependencies
  481. *
  482. * This performs the actions to allow the replay job to be re-run following
  483. * completion of the passed dependency.
  484. *
  485. * @param[in] katom The atom to be reset
  486. * @param[in] dep_atom The dependency to be attached to the atom
  487. */
  488. static void kbasep_replay_reset_softjob(struct kbase_jd_atom *katom,
  489. struct kbase_jd_atom *dep_atom)
  490. {
  491. katom->status = KBASE_JD_ATOM_STATE_QUEUED;
  492. kbase_jd_katom_dep_set(&katom->dep[0], dep_atom, BASE_JD_DEP_TYPE_DATA);
  493. list_add_tail(&katom->dep_item[0], &dep_atom->dep_head[0]);
  494. }
  495. /**
  496. * @brief Allocate an unused katom
  497. *
  498. * This will search the provided context for an unused katom, and will mark it
  499. * as KBASE_JD_ATOM_STATE_QUEUED.
  500. *
  501. * If no atoms are available then the function will fail.
  502. *
  503. * @param[in] kctx Context pointer
  504. * @return An atom ID, or -1 on failure
  505. */
  506. static int kbasep_allocate_katom(struct kbase_context *kctx)
  507. {
  508. struct kbase_jd_context *jctx = &kctx->jctx;
  509. int i;
  510. for (i = BASE_JD_ATOM_COUNT-1; i > 0; i--) {
  511. if (jctx->atoms[i].status == KBASE_JD_ATOM_STATE_UNUSED) {
  512. jctx->atoms[i].status = KBASE_JD_ATOM_STATE_QUEUED;
  513. dev_dbg(kctx->kbdev->dev,
  514. "kbasep_allocate_katom: Allocated atom %d\n",
  515. i);
  516. return i;
  517. }
  518. }
  519. return -1;
  520. }
  521. /**
  522. * @brief Release a katom
  523. *
  524. * This will mark the provided atom as available, and remove any dependencies.
  525. *
  526. * For use on error path.
  527. *
  528. * @param[in] kctx Context pointer
  529. * @param[in] atom_id ID of atom to release
  530. */
  531. static void kbasep_release_katom(struct kbase_context *kctx, int atom_id)
  532. {
  533. struct kbase_jd_context *jctx = &kctx->jctx;
  534. dev_dbg(kctx->kbdev->dev, "kbasep_release_katom: Released atom %d\n",
  535. atom_id);
  536. while (!list_empty(&jctx->atoms[atom_id].dep_head[0]))
  537. list_del(jctx->atoms[atom_id].dep_head[0].next);
  538. while (!list_empty(&jctx->atoms[atom_id].dep_head[1]))
  539. list_del(jctx->atoms[atom_id].dep_head[1].next);
  540. jctx->atoms[atom_id].status = KBASE_JD_ATOM_STATE_UNUSED;
  541. }
  542. static void kbasep_replay_create_atom(struct kbase_context *kctx,
  543. struct base_jd_atom_v2 *atom,
  544. int atom_nr,
  545. base_jd_prio prio)
  546. {
  547. atom->nr_extres = 0;
  548. atom->extres_list.value = NULL;
  549. atom->device_nr = 0;
  550. atom->prio = prio;
  551. atom->atom_number = atom_nr;
  552. base_jd_atom_dep_set(&atom->pre_dep[0], 0, BASE_JD_DEP_TYPE_INVALID);
  553. base_jd_atom_dep_set(&atom->pre_dep[1], 0, BASE_JD_DEP_TYPE_INVALID);
  554. atom->udata.blob[0] = 0;
  555. atom->udata.blob[1] = 0;
  556. }
  557. /**
  558. * @brief Create two atoms for the purpose of replaying jobs
  559. *
  560. * Two atoms are allocated and created. The jc pointer is not set at this
  561. * stage. The second atom has a dependency on the first. The remaining fields
  562. * are set up as follows :
  563. *
  564. * - No external resources. Any required external resources will be held by the
  565. * replay atom.
  566. * - device_nr is set to 0. This is not relevant as
  567. * BASE_JD_REQ_SPECIFIC_COHERENT_GROUP should not be set.
  568. * - Priority is inherited from the replay job.
  569. *
  570. * @param[out] t_atom Atom to use for tiler jobs
  571. * @param[out] f_atom Atom to use for fragment jobs
  572. * @param[in] prio Priority of new atom (inherited from replay soft
  573. * job)
  574. * @return 0 on success, error code on failure
  575. */
  576. static int kbasep_replay_create_atoms(struct kbase_context *kctx,
  577. struct base_jd_atom_v2 *t_atom,
  578. struct base_jd_atom_v2 *f_atom,
  579. base_jd_prio prio)
  580. {
  581. int t_atom_nr, f_atom_nr;
  582. t_atom_nr = kbasep_allocate_katom(kctx);
  583. if (t_atom_nr < 0) {
  584. dev_err(kctx->kbdev->dev, "Failed to allocate katom\n");
  585. return -EINVAL;
  586. }
  587. f_atom_nr = kbasep_allocate_katom(kctx);
  588. if (f_atom_nr < 0) {
  589. dev_err(kctx->kbdev->dev, "Failed to allocate katom\n");
  590. kbasep_release_katom(kctx, t_atom_nr);
  591. return -EINVAL;
  592. }
  593. kbasep_replay_create_atom(kctx, t_atom, t_atom_nr, prio);
  594. kbasep_replay_create_atom(kctx, f_atom, f_atom_nr, prio);
  595. base_jd_atom_dep_set(&f_atom->pre_dep[0], t_atom_nr, BASE_JD_DEP_TYPE_DATA);
  596. return 0;
  597. }
  598. #ifdef CONFIG_MALI_DEBUG
  599. static void payload_dump(struct kbase_context *kctx, base_jd_replay_payload *payload)
  600. {
  601. u64 next;
  602. dev_dbg(kctx->kbdev->dev, "Tiler jc list :\n");
  603. next = payload->tiler_jc_list;
  604. while (next) {
  605. struct kbase_vmap_struct map;
  606. base_jd_replay_jc *jc_struct;
  607. jc_struct = kbase_vmap(kctx, next, sizeof(*jc_struct), &map);
  608. if (!jc_struct)
  609. return;
  610. dev_dbg(kctx->kbdev->dev, "* jc_struct=%p jc=%llx next=%llx\n",
  611. jc_struct, jc_struct->jc, jc_struct->next);
  612. next = jc_struct->next;
  613. kbase_vunmap(kctx, &map);
  614. }
  615. }
  616. #endif // ifdef CONFIG_MALI_DEBUG
  617. /**
  618. * @brief Parse a base_jd_replay_payload provided by userspace
  619. *
  620. * This will read the payload from userspace, and parse the job chains.
  621. *
  622. * @param[in] kctx Context pointer
  623. * @param[in] replay_atom Replay soft job atom
  624. * @param[in] t_atom Atom to use for tiler jobs
  625. * @param[in] f_atom Atom to use for fragment jobs
  626. * @return 0 on success, error code on failure
  627. */
  628. static int kbasep_replay_parse_payload(struct kbase_context *kctx,
  629. struct kbase_jd_atom *replay_atom,
  630. struct base_jd_atom_v2 *t_atom,
  631. struct base_jd_atom_v2 *f_atom)
  632. {
  633. base_jd_replay_payload *payload;
  634. u64 next;
  635. u64 prev_jc = 0;
  636. u16 hw_job_id_offset = 0;
  637. int ret = -EINVAL;
  638. struct kbase_vmap_struct map;
  639. dev_dbg(kctx->kbdev->dev, "kbasep_replay_parse_payload: replay_atom->jc = %llx sizeof(payload) = %zu\n",
  640. replay_atom->jc, sizeof(payload));
  641. payload = kbase_vmap(kctx, replay_atom->jc, sizeof(*payload), &map);
  642. if (!payload) {
  643. dev_err(kctx->kbdev->dev, "kbasep_replay_parse_payload: failed to map payload into kernel space\n");
  644. return -EINVAL;
  645. }
  646. #ifdef CONFIG_MALI_DEBUG
  647. dev_dbg(kctx->kbdev->dev, "kbasep_replay_parse_payload: payload=%p\n", payload);
  648. dev_dbg(kctx->kbdev->dev, "Payload structure:\n"
  649. "tiler_jc_list = %llx\n"
  650. "fragment_jc = %llx\n"
  651. "tiler_heap_free = %llx\n"
  652. "fragment_hierarchy_mask = %x\n"
  653. "tiler_hierarchy_mask = %x\n"
  654. "hierarchy_default_weight = %x\n"
  655. "tiler_core_req = %x\n"
  656. "fragment_core_req = %x\n",
  657. payload->tiler_jc_list,
  658. payload->fragment_jc,
  659. payload->tiler_heap_free,
  660. payload->fragment_hierarchy_mask,
  661. payload->tiler_hierarchy_mask,
  662. payload->hierarchy_default_weight,
  663. payload->tiler_core_req,
  664. payload->fragment_core_req);
  665. payload_dump(kctx, payload);
  666. #endif // ifdef CONFIG_MALI_DEBUG
  667. t_atom->core_req = payload->tiler_core_req | BASEP_JD_REQ_EVENT_NEVER;
  668. f_atom->core_req = payload->fragment_core_req | BASEP_JD_REQ_EVENT_NEVER;
  669. /* Sanity check core requirements*/
  670. if (unlikely((t_atom->core_req & BASEP_JD_REQ_ATOM_TYPE &
  671. ~BASE_JD_REQ_COHERENT_GROUP) != BASE_JD_REQ_T ||
  672. (f_atom->core_req & BASEP_JD_REQ_ATOM_TYPE &
  673. ~BASE_JD_REQ_COHERENT_GROUP & ~BASE_JD_REQ_FS_AFBC) != BASE_JD_REQ_FS ||
  674. t_atom->core_req & BASE_JD_REQ_EXTERNAL_RESOURCES ||
  675. f_atom->core_req & BASE_JD_REQ_EXTERNAL_RESOURCES)) {
  676. int t_atom_type = t_atom->core_req & BASEP_JD_REQ_ATOM_TYPE & ~BASE_JD_REQ_COHERENT_GROUP;
  677. int f_atom_type = f_atom->core_req & BASEP_JD_REQ_ATOM_TYPE & ~BASE_JD_REQ_COHERENT_GROUP & ~BASE_JD_REQ_FS_AFBC;
  678. int t_has_ex_res = t_atom->core_req & BASE_JD_REQ_EXTERNAL_RESOURCES;
  679. int f_has_ex_res = f_atom->core_req & BASE_JD_REQ_EXTERNAL_RESOURCES;
  680. if (t_atom_type != BASE_JD_REQ_T) {
  681. dev_err(kctx->kbdev->dev, "Invalid core requirement: Tiler atom not a tiler job. Was: 0x%x\n Expected: 0x%x",
  682. t_atom_type, BASE_JD_REQ_T);
  683. }
  684. if (f_atom_type != BASE_JD_REQ_FS) {
  685. dev_err(kctx->kbdev->dev, "Invalid core requirement: Fragment shader atom not a fragment shader. Was 0x%x Expected: 0x%x\n",
  686. f_atom_type, BASE_JD_REQ_FS);
  687. }
  688. if (t_has_ex_res) {
  689. dev_err(kctx->kbdev->dev, "Invalid core requirement: Tiler atom has external resources.\n");
  690. }
  691. if (f_has_ex_res) {
  692. dev_err(kctx->kbdev->dev, "Invalid core requirement: Fragment shader atom has external resources.\n");
  693. }
  694. goto out;
  695. }
  696. /* Process tiler job chains */
  697. next = payload->tiler_jc_list;
  698. if (!next) {
  699. dev_err(kctx->kbdev->dev, "Invalid tiler JC list\n");
  700. goto out;
  701. }
  702. while (next) {
  703. base_jd_replay_jc *jc_struct;
  704. struct kbase_vmap_struct jc_map;
  705. u64 jc;
  706. jc_struct = kbase_vmap(kctx, next, sizeof(*jc_struct), &jc_map);
  707. if (!jc_struct) {
  708. dev_err(kctx->kbdev->dev, "Failed to map jc struct\n");
  709. goto out;
  710. }
  711. jc = jc_struct->jc;
  712. next = jc_struct->next;
  713. if (next)
  714. jc_struct->jc = 0;
  715. kbase_vunmap(kctx, &jc_map);
  716. if (jc) {
  717. u16 max_hw_job_id = 0;
  718. if (kbasep_replay_find_hw_job_id(kctx, jc,
  719. &max_hw_job_id) != 0)
  720. goto out;
  721. if (kbasep_replay_parse_jc(kctx, jc, prev_jc,
  722. payload->tiler_heap_free,
  723. payload->tiler_hierarchy_mask,
  724. payload->hierarchy_default_weight,
  725. hw_job_id_offset, false) != 0) {
  726. goto out;
  727. }
  728. hw_job_id_offset += max_hw_job_id;
  729. prev_jc = jc;
  730. }
  731. }
  732. t_atom->jc = prev_jc;
  733. /* Process fragment job chain */
  734. f_atom->jc = payload->fragment_jc;
  735. if (kbasep_replay_parse_jc(kctx, payload->fragment_jc, 0,
  736. payload->tiler_heap_free,
  737. payload->fragment_hierarchy_mask,
  738. payload->hierarchy_default_weight, 0,
  739. true) != 0) {
  740. goto out;
  741. }
  742. if (!t_atom->jc || !f_atom->jc) {
  743. dev_err(kctx->kbdev->dev, "Invalid payload\n");
  744. goto out;
  745. }
  746. dev_dbg(kctx->kbdev->dev, "t_atom->jc=%llx f_atom->jc=%llx\n",
  747. t_atom->jc, f_atom->jc);
  748. ret = 0;
  749. out:
  750. kbase_vunmap(kctx, &map);
  751. return ret;
  752. }
  753. static void kbase_replay_process_worker(struct work_struct *data)
  754. {
  755. struct kbase_jd_atom *katom;
  756. struct kbase_context *kctx;
  757. struct kbase_jd_context *jctx;
  758. bool need_to_try_schedule_context = false;
  759. struct base_jd_atom_v2 t_atom, f_atom;
  760. struct kbase_jd_atom *t_katom, *f_katom;
  761. base_jd_prio atom_prio;
  762. katom = container_of(data, struct kbase_jd_atom, work);
  763. kctx = katom->kctx;
  764. jctx = &kctx->jctx;
  765. mutex_lock(&jctx->lock);
  766. atom_prio = kbasep_js_sched_prio_to_atom_prio(katom->sched_priority);
  767. if (kbasep_replay_create_atoms(
  768. kctx, &t_atom, &f_atom, atom_prio) != 0) {
  769. katom->event_code = BASE_JD_EVENT_JOB_CANCELLED;
  770. goto out;
  771. }
  772. t_katom = &jctx->atoms[t_atom.atom_number];
  773. f_katom = &jctx->atoms[f_atom.atom_number];
  774. if (kbasep_replay_parse_payload(kctx, katom, &t_atom, &f_atom) != 0) {
  775. kbasep_release_katom(kctx, t_atom.atom_number);
  776. kbasep_release_katom(kctx, f_atom.atom_number);
  777. katom->event_code = BASE_JD_EVENT_JOB_CANCELLED;
  778. goto out;
  779. }
  780. kbasep_replay_reset_softjob(katom, f_katom);
  781. need_to_try_schedule_context |= jd_submit_atom(kctx, &t_atom, t_katom);
  782. if (t_katom->event_code == BASE_JD_EVENT_JOB_INVALID) {
  783. dev_err(kctx->kbdev->dev, "Replay failed to submit atom\n");
  784. kbasep_release_katom(kctx, f_atom.atom_number);
  785. katom->event_code = BASE_JD_EVENT_JOB_CANCELLED;
  786. goto out;
  787. }
  788. need_to_try_schedule_context |= jd_submit_atom(kctx, &f_atom, f_katom);
  789. if (f_katom->event_code == BASE_JD_EVENT_JOB_INVALID) {
  790. dev_err(kctx->kbdev->dev, "Replay failed to submit atom\n");
  791. katom->event_code = BASE_JD_EVENT_JOB_CANCELLED;
  792. goto out;
  793. }
  794. katom->event_code = BASE_JD_EVENT_DONE;
  795. out:
  796. if (katom->event_code != BASE_JD_EVENT_DONE) {
  797. kbase_disjoint_state_down(kctx->kbdev);
  798. need_to_try_schedule_context |= jd_done_nolock(katom, NULL);
  799. }
  800. if (need_to_try_schedule_context)
  801. kbase_js_sched_all(kctx->kbdev);
  802. mutex_unlock(&jctx->lock);
  803. }
  804. /**
  805. * @brief Check job replay fault
  806. *
  807. * This will read the job payload, checks fault type and source, then decides
  808. * whether replay is required.
  809. *
  810. * @param[in] katom The atom to be processed
  811. * @return true (success) if replay required or false on failure.
  812. */
  813. static bool kbase_replay_fault_check(struct kbase_jd_atom *katom)
  814. {
  815. struct kbase_context *kctx = katom->kctx;
  816. struct device *dev = kctx->kbdev->dev;
  817. base_jd_replay_payload *payload;
  818. u64 job_header;
  819. u64 job_loop_detect;
  820. struct job_descriptor_header *job;
  821. struct kbase_vmap_struct job_map;
  822. struct kbase_vmap_struct map;
  823. bool err = false;
  824. /* Replay job if fault is of type BASE_JD_EVENT_JOB_WRITE_FAULT or
  825. * if force_replay is enabled.
  826. */
  827. if (katom->event_code == BASE_JD_EVENT_TERMINATED) {
  828. return false;
  829. } else if (katom->event_code == BASE_JD_EVENT_JOB_WRITE_FAULT) {
  830. return true;
  831. } else if (katom->event_code == BASE_JD_EVENT_FORCE_REPLAY) {
  832. katom->event_code = BASE_JD_EVENT_DATA_INVALID_FAULT;
  833. return true;
  834. } else if (katom->event_code != BASE_JD_EVENT_DATA_INVALID_FAULT) {
  835. /* No replay for faults of type other than
  836. * BASE_JD_EVENT_DATA_INVALID_FAULT.
  837. */
  838. return false;
  839. }
  840. /* Job fault is BASE_JD_EVENT_DATA_INVALID_FAULT, now scan fragment jc
  841. * to find out whether the source of exception is POLYGON_LIST. Replay
  842. * is required if the source of fault is POLYGON_LIST.
  843. */
  844. payload = kbase_vmap(kctx, katom->jc, sizeof(*payload), &map);
  845. if (!payload) {
  846. dev_err(dev, "kbase_replay_fault_check: failed to map payload.\n");
  847. return false;
  848. }
  849. #ifdef CONFIG_MALI_DEBUG
  850. dev_dbg(dev, "kbase_replay_fault_check: payload=%p\n", payload);
  851. dev_dbg(dev, "\nPayload structure:\n"
  852. "fragment_jc = 0x%llx\n"
  853. "fragment_hierarchy_mask = 0x%x\n"
  854. "fragment_core_req = 0x%x\n",
  855. payload->fragment_jc,
  856. payload->fragment_hierarchy_mask,
  857. payload->fragment_core_req);
  858. #endif // ifdef CONFIG_MALI_DEBUG
  859. /* Process fragment job chain */
  860. job_header = (u64) payload->fragment_jc;
  861. job_loop_detect = job_header;
  862. while (job_header) {
  863. job = kbase_vmap(kctx, job_header, sizeof(*job), &job_map);
  864. if (!job) {
  865. dev_err(dev, "failed to map jc\n");
  866. /* unmap payload*/
  867. kbase_vunmap(kctx, &map);
  868. return false;
  869. }
  870. dump_job_head(kctx, "\njob_head structure:\n", job);
  871. /* Replay only when the polygon list reader caused the
  872. * DATA_INVALID_FAULT */
  873. if ((katom->event_code == BASE_JD_EVENT_DATA_INVALID_FAULT) &&
  874. (JOB_SOURCE_ID(job->exception_status) == JOB_POLYGON_LIST)) {
  875. err = true;
  876. kbase_vunmap(kctx, &job_map);
  877. break;
  878. }
  879. /* Move on to next fragment job in the list */
  880. if (job->job_descriptor_size)
  881. job_header = job->next_job._64;
  882. else
  883. job_header = job->next_job._32;
  884. kbase_vunmap(kctx, &job_map);
  885. /* Job chain loop detected */
  886. if (job_header == job_loop_detect)
  887. break;
  888. }
  889. /* unmap payload*/
  890. kbase_vunmap(kctx, &map);
  891. return err;
  892. }
  893. /**
  894. * @brief Process a replay job
  895. *
  896. * Called from kbase_process_soft_job.
  897. *
  898. * On exit, if the job has completed, katom->event_code will have been updated.
  899. * If the job has not completed, and is replaying jobs, then the atom status
  900. * will have been reset to KBASE_JD_ATOM_STATE_QUEUED.
  901. *
  902. * @param[in] katom The atom to be processed
  903. * @return false if the atom has completed
  904. * true if the atom is replaying jobs
  905. */
  906. bool kbase_replay_process(struct kbase_jd_atom *katom)
  907. {
  908. struct kbase_context *kctx = katom->kctx;
  909. struct kbase_jd_context *jctx = &kctx->jctx;
  910. struct kbase_device *kbdev = kctx->kbdev;
  911. /* Don't replay this atom if these issues are not present in the
  912. * hardware */
  913. if (!kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_11020) &&
  914. !kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_11024)) {
  915. dev_dbg(kbdev->dev, "Hardware does not need replay workaround");
  916. /* Signal failure to userspace */
  917. katom->event_code = BASE_JD_EVENT_JOB_INVALID;
  918. return false;
  919. }
  920. if (katom->event_code == BASE_JD_EVENT_DONE) {
  921. dev_dbg(kbdev->dev, "Previous job succeeded - not replaying\n");
  922. if (katom->retry_count)
  923. kbase_disjoint_state_down(kbdev);
  924. return false;
  925. }
  926. if (jctx->sched_info.ctx.is_dying) {
  927. dev_dbg(kbdev->dev, "Not replaying; context is dying\n");
  928. if (katom->retry_count)
  929. kbase_disjoint_state_down(kbdev);
  930. return false;
  931. }
  932. /* Check job exception type and source before replaying. */
  933. if (!kbase_replay_fault_check(katom)) {
  934. dev_dbg(kbdev->dev,
  935. "Replay cancelled on event %x\n", katom->event_code);
  936. /* katom->event_code is already set to the failure code of the
  937. * previous job.
  938. */
  939. return false;
  940. }
  941. dev_warn(kbdev->dev, "Replaying jobs retry=%d\n",
  942. katom->retry_count);
  943. katom->retry_count++;
  944. if (katom->retry_count > BASEP_JD_REPLAY_LIMIT) {
  945. dev_err(kbdev->dev, "Replay exceeded limit - failing jobs\n");
  946. kbase_disjoint_state_down(kbdev);
  947. /* katom->event_code is already set to the failure code of the
  948. previous job */
  949. return false;
  950. }
  951. /* only enter the disjoint state once for the whole time while the replay is ongoing */
  952. if (katom->retry_count == 1)
  953. kbase_disjoint_state_up(kbdev);
  954. INIT_WORK(&katom->work, kbase_replay_process_worker);
  955. queue_work(kctx->event_workq, &katom->work);
  956. return true;
  957. }