dpu_rm.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2023-2024 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) "[drm:%s] " fmt, __func__
  7. #include "dpu_kms.h"
  8. #include "dpu_hw_lm.h"
  9. #include "dpu_hw_ctl.h"
  10. #include "dpu_hw_cdm.h"
  11. #include "dpu_hw_cwb.h"
  12. #include "dpu_hw_pingpong.h"
  13. #include "dpu_hw_sspp.h"
  14. #include "dpu_hw_intf.h"
  15. #include "dpu_hw_wb.h"
  16. #include "dpu_hw_dspp.h"
  17. #include "dpu_hw_merge3d.h"
  18. #include "dpu_hw_dsc.h"
  19. #include "dpu_encoder.h"
  20. #include "dpu_trace.h"
  21. static inline bool reserved_by_other(uint32_t *res_map, int idx,
  22. uint32_t crtc_id)
  23. {
  24. return res_map[idx] && res_map[idx] != crtc_id;
  25. }
  26. /**
  27. * dpu_rm_init - Read hardware catalog and create reservation tracking objects
  28. * for all HW blocks.
  29. * @dev: Corresponding device for devres management
  30. * @rm: DPU Resource Manager handle
  31. * @cat: Pointer to hardware catalog
  32. * @mdss_data: Pointer to MDSS / UBWC configuration
  33. * @mmio: mapped register io address of MDP
  34. * @return: 0 on Success otherwise -ERROR
  35. */
  36. int dpu_rm_init(struct drm_device *dev,
  37. struct dpu_rm *rm,
  38. const struct dpu_mdss_cfg *cat,
  39. const struct qcom_ubwc_cfg_data *mdss_data,
  40. void __iomem *mmio)
  41. {
  42. int rc, i;
  43. if (!rm || !cat || !mmio) {
  44. DPU_ERROR("invalid kms\n");
  45. return -EINVAL;
  46. }
  47. /* Clear, setup lists */
  48. memset(rm, 0, sizeof(*rm));
  49. rm->has_legacy_ctls = (cat->mdss_ver->core_major_ver < 5);
  50. /* Interrogate HW catalog and create tracking items for hw blocks */
  51. for (i = 0; i < cat->mixer_count; i++) {
  52. struct dpu_hw_mixer *hw;
  53. const struct dpu_lm_cfg *lm = &cat->mixer[i];
  54. hw = dpu_hw_lm_init(dev, lm, mmio, cat->mdss_ver);
  55. if (IS_ERR(hw)) {
  56. rc = PTR_ERR(hw);
  57. DPU_ERROR("failed lm object creation: err %d\n", rc);
  58. goto fail;
  59. }
  60. rm->mixer_blks[lm->id - LM_0] = &hw->base;
  61. }
  62. for (i = 0; i < cat->merge_3d_count; i++) {
  63. struct dpu_hw_merge_3d *hw;
  64. const struct dpu_merge_3d_cfg *merge_3d = &cat->merge_3d[i];
  65. hw = dpu_hw_merge_3d_init(dev, merge_3d, mmio);
  66. if (IS_ERR(hw)) {
  67. rc = PTR_ERR(hw);
  68. DPU_ERROR("failed merge_3d object creation: err %d\n",
  69. rc);
  70. goto fail;
  71. }
  72. rm->merge_3d_blks[merge_3d->id - MERGE_3D_0] = &hw->base;
  73. }
  74. for (i = 0; i < cat->pingpong_count; i++) {
  75. struct dpu_hw_pingpong *hw;
  76. const struct dpu_pingpong_cfg *pp = &cat->pingpong[i];
  77. hw = dpu_hw_pingpong_init(dev, pp, mmio, cat->mdss_ver);
  78. if (IS_ERR(hw)) {
  79. rc = PTR_ERR(hw);
  80. DPU_ERROR("failed pingpong object creation: err %d\n",
  81. rc);
  82. goto fail;
  83. }
  84. if (pp->merge_3d && pp->merge_3d < MERGE_3D_MAX)
  85. hw->merge_3d = to_dpu_hw_merge_3d(rm->merge_3d_blks[pp->merge_3d - MERGE_3D_0]);
  86. rm->pingpong_blks[pp->id - PINGPONG_0] = &hw->base;
  87. }
  88. for (i = 0; i < cat->intf_count; i++) {
  89. struct dpu_hw_intf *hw;
  90. const struct dpu_intf_cfg *intf = &cat->intf[i];
  91. hw = dpu_hw_intf_init(dev, intf, mmio, cat->mdss_ver);
  92. if (IS_ERR(hw)) {
  93. rc = PTR_ERR(hw);
  94. DPU_ERROR("failed intf object creation: err %d\n", rc);
  95. goto fail;
  96. }
  97. rm->hw_intf[intf->id - INTF_0] = hw;
  98. }
  99. for (i = 0; i < cat->wb_count; i++) {
  100. struct dpu_hw_wb *hw;
  101. const struct dpu_wb_cfg *wb = &cat->wb[i];
  102. hw = dpu_hw_wb_init(dev, wb, mmio, cat->mdss_ver);
  103. if (IS_ERR(hw)) {
  104. rc = PTR_ERR(hw);
  105. DPU_ERROR("failed wb object creation: err %d\n", rc);
  106. goto fail;
  107. }
  108. rm->hw_wb[wb->id - WB_0] = hw;
  109. }
  110. for (i = 0; i < cat->cwb_count; i++) {
  111. struct dpu_hw_cwb *hw;
  112. const struct dpu_cwb_cfg *cwb = &cat->cwb[i];
  113. hw = dpu_hw_cwb_init(dev, cwb, mmio);
  114. if (IS_ERR(hw)) {
  115. rc = PTR_ERR(hw);
  116. DPU_ERROR("failed cwb object creation: err %d\n", rc);
  117. goto fail;
  118. }
  119. rm->cwb_blks[cwb->id - CWB_0] = &hw->base;
  120. }
  121. for (i = 0; i < cat->ctl_count; i++) {
  122. struct dpu_hw_ctl *hw;
  123. const struct dpu_ctl_cfg *ctl = &cat->ctl[i];
  124. hw = dpu_hw_ctl_init(dev, ctl, mmio, cat->mdss_ver, cat->mixer_count, cat->mixer);
  125. if (IS_ERR(hw)) {
  126. rc = PTR_ERR(hw);
  127. DPU_ERROR("failed ctl object creation: err %d\n", rc);
  128. goto fail;
  129. }
  130. rm->ctl_blks[ctl->id - CTL_0] = &hw->base;
  131. }
  132. for (i = 0; i < cat->dspp_count; i++) {
  133. struct dpu_hw_dspp *hw;
  134. const struct dpu_dspp_cfg *dspp = &cat->dspp[i];
  135. hw = dpu_hw_dspp_init(dev, dspp, mmio);
  136. if (IS_ERR(hw)) {
  137. rc = PTR_ERR(hw);
  138. DPU_ERROR("failed dspp object creation: err %d\n", rc);
  139. goto fail;
  140. }
  141. rm->dspp_blks[dspp->id - DSPP_0] = &hw->base;
  142. }
  143. for (i = 0; i < cat->dsc_count; i++) {
  144. struct dpu_hw_dsc *hw;
  145. const struct dpu_dsc_cfg *dsc = &cat->dsc[i];
  146. if (cat->mdss_ver->core_major_ver >= 7)
  147. hw = dpu_hw_dsc_init_1_2(dev, dsc, mmio);
  148. else
  149. hw = dpu_hw_dsc_init(dev, dsc, mmio, cat->mdss_ver);
  150. if (IS_ERR(hw)) {
  151. rc = PTR_ERR(hw);
  152. DPU_ERROR("failed dsc object creation: err %d\n", rc);
  153. goto fail;
  154. }
  155. rm->dsc_blks[dsc->id - DSC_0] = &hw->base;
  156. }
  157. for (i = 0; i < cat->sspp_count; i++) {
  158. struct dpu_hw_sspp *hw;
  159. const struct dpu_sspp_cfg *sspp = &cat->sspp[i];
  160. hw = dpu_hw_sspp_init(dev, sspp, mmio, mdss_data, cat->mdss_ver);
  161. if (IS_ERR(hw)) {
  162. rc = PTR_ERR(hw);
  163. DPU_ERROR("failed sspp object creation: err %d\n", rc);
  164. goto fail;
  165. }
  166. rm->hw_sspp[sspp->id - SSPP_NONE] = hw;
  167. }
  168. if (cat->cdm) {
  169. struct dpu_hw_cdm *hw;
  170. hw = dpu_hw_cdm_init(dev, cat->cdm, mmio, cat->mdss_ver);
  171. if (IS_ERR(hw)) {
  172. rc = PTR_ERR(hw);
  173. DPU_ERROR("failed cdm object creation: err %d\n", rc);
  174. goto fail;
  175. }
  176. rm->cdm_blk = &hw->base;
  177. }
  178. return 0;
  179. fail:
  180. return rc ? rc : -EFAULT;
  181. }
  182. static bool _dpu_rm_needs_split_display(const struct msm_display_topology *top)
  183. {
  184. return top->num_intf > 1;
  185. }
  186. /**
  187. * _dpu_rm_get_lm_peer - get the id of a mixer which is a peer of the primary
  188. * @rm: dpu resource manager handle
  189. * @primary_idx: index of primary mixer in rm->mixer_blks[]
  190. *
  191. * Returns: lm peer mixed id on success or %-EINVAL on error
  192. */
  193. static int _dpu_rm_get_lm_peer(struct dpu_rm *rm, int primary_idx)
  194. {
  195. const struct dpu_lm_cfg *prim_lm_cfg;
  196. prim_lm_cfg = to_dpu_hw_mixer(rm->mixer_blks[primary_idx])->cap;
  197. if (prim_lm_cfg->lm_pair >= LM_0 && prim_lm_cfg->lm_pair < LM_MAX)
  198. return prim_lm_cfg->lm_pair - LM_0;
  199. return -EINVAL;
  200. }
  201. static int _dpu_rm_reserve_cwb_mux_and_pingpongs(struct dpu_rm *rm,
  202. struct dpu_global_state *global_state,
  203. uint32_t crtc_id,
  204. struct msm_display_topology *topology)
  205. {
  206. int num_cwb_mux = topology->num_lm, cwb_mux_count = 0;
  207. int cwb_pp_start_idx = PINGPONG_CWB_0 - PINGPONG_0;
  208. int cwb_pp_idx[MAX_BLOCKS];
  209. int cwb_mux_idx[MAX_BLOCKS];
  210. /*
  211. * Reserve additional dedicated CWB PINGPONG blocks and muxes for each
  212. * mixer
  213. *
  214. * TODO: add support reserving resources for platforms with no
  215. * PINGPONG_CWB
  216. */
  217. for (int i = 0; i < ARRAY_SIZE(rm->mixer_blks) &&
  218. cwb_mux_count < num_cwb_mux; i++) {
  219. for (int j = 0; j < ARRAY_SIZE(rm->cwb_blks); j++) {
  220. /*
  221. * Odd LMs must be assigned to odd CWB muxes and even
  222. * LMs with even CWB muxes.
  223. *
  224. * Since the RM HW block array index is based on the HW
  225. * block ids, we can also use the array index to enforce
  226. * the odd/even rule. See dpu_rm_init() for more
  227. * information
  228. */
  229. if (reserved_by_other(global_state->cwb_to_crtc_id, j, crtc_id) ||
  230. i % 2 != j % 2)
  231. continue;
  232. cwb_mux_idx[cwb_mux_count] = j;
  233. cwb_pp_idx[cwb_mux_count] = j + cwb_pp_start_idx;
  234. cwb_mux_count++;
  235. break;
  236. }
  237. }
  238. if (cwb_mux_count != num_cwb_mux) {
  239. DPU_ERROR("Unable to reserve all CWB PINGPONGs\n");
  240. return -ENAVAIL;
  241. }
  242. for (int i = 0; i < cwb_mux_count; i++) {
  243. global_state->pingpong_to_crtc_id[cwb_pp_idx[i]] = crtc_id;
  244. global_state->cwb_to_crtc_id[cwb_mux_idx[i]] = crtc_id;
  245. }
  246. return 0;
  247. }
  248. /**
  249. * _dpu_rm_check_lm_and_get_connected_blks - check if proposed layer mixer meets
  250. * proposed use case requirements, incl. hardwired dependent blocks like
  251. * pingpong
  252. * @rm: dpu resource manager handle
  253. * @global_state: resources shared across multiple kms objects
  254. * @crtc_id: crtc id requesting for allocation
  255. * @lm_idx: index of proposed layer mixer in rm->mixer_blks[], function checks
  256. * if lm, and all other hardwired blocks connected to the lm (pp) is
  257. * available and appropriate
  258. * @pp_idx: output parameter, index of pingpong block attached to the layer
  259. * mixer in rm->pingpong_blks[].
  260. * @dspp_idx: output parameter, index of dspp block attached to the layer
  261. * mixer in rm->dspp_blks[].
  262. * @topology: selected topology for the display
  263. * Return: true if lm matches all requirements, false otherwise
  264. */
  265. static bool _dpu_rm_check_lm_and_get_connected_blks(struct dpu_rm *rm,
  266. struct dpu_global_state *global_state,
  267. uint32_t crtc_id, int lm_idx, int *pp_idx, int *dspp_idx,
  268. struct msm_display_topology *topology)
  269. {
  270. const struct dpu_lm_cfg *lm_cfg;
  271. int idx;
  272. /* Already reserved? */
  273. if (reserved_by_other(global_state->mixer_to_crtc_id, lm_idx, crtc_id)) {
  274. DPU_DEBUG("LM_%d already reserved\n", lm_idx);
  275. return false;
  276. }
  277. lm_cfg = to_dpu_hw_mixer(rm->mixer_blks[lm_idx])->cap;
  278. idx = lm_cfg->pingpong - PINGPONG_0;
  279. if (idx < 0 || idx >= ARRAY_SIZE(rm->pingpong_blks) || !rm->pingpong_blks[idx]) {
  280. DPU_ERROR("LM_%d, invalid PP_%d\n", lm_idx, idx);
  281. return false;
  282. }
  283. if (reserved_by_other(global_state->pingpong_to_crtc_id, idx, crtc_id)) {
  284. DPU_DEBUG("LM_%d PP_%d already reserved\n", lm_idx, idx);
  285. return false;
  286. }
  287. *pp_idx = idx;
  288. if (!topology->num_dspp)
  289. return true;
  290. idx = lm_cfg->dspp - DSPP_0;
  291. if (idx < 0 || idx >= ARRAY_SIZE(rm->dspp_blks) || !rm->dspp_blks[idx]) {
  292. DPU_ERROR("LM_%d, invalid DSPP_%d\n", lm_idx, idx);
  293. return false;
  294. }
  295. if (reserved_by_other(global_state->dspp_to_crtc_id, idx, crtc_id)) {
  296. DPU_DEBUG("LM_%d DSPP_%d already reserved\n", lm_idx, idx);
  297. return false;
  298. }
  299. *dspp_idx = idx;
  300. return true;
  301. }
  302. static int _dpu_rm_reserve_lms(struct dpu_rm *rm,
  303. struct dpu_global_state *global_state,
  304. uint32_t crtc_id,
  305. struct msm_display_topology *topology)
  306. {
  307. int lm_idx[MAX_BLOCKS];
  308. int pp_idx[MAX_BLOCKS];
  309. int dspp_idx[MAX_BLOCKS] = {0};
  310. int i, lm_count = 0;
  311. if (!topology->num_lm) {
  312. DPU_ERROR("zero LMs in topology\n");
  313. return -EINVAL;
  314. }
  315. /* Find a primary mixer */
  316. for (i = 0; i < ARRAY_SIZE(rm->mixer_blks) &&
  317. lm_count < topology->num_lm; i++) {
  318. if (!rm->mixer_blks[i])
  319. continue;
  320. /*
  321. * Reset lm_count to an even index. This will drop the previous
  322. * primary mixer if failed to find its peer.
  323. */
  324. lm_count &= ~1;
  325. lm_idx[lm_count] = i;
  326. if (!_dpu_rm_check_lm_and_get_connected_blks(rm, global_state,
  327. crtc_id, i, &pp_idx[lm_count],
  328. &dspp_idx[lm_count], topology)) {
  329. continue;
  330. }
  331. ++lm_count;
  332. /* Valid primary mixer found, find matching peers */
  333. if (lm_count < topology->num_lm) {
  334. int j = _dpu_rm_get_lm_peer(rm, i);
  335. /* ignore the peer if there is an error or if the peer was already processed */
  336. if (j < 0 || j < i)
  337. continue;
  338. if (!rm->mixer_blks[j])
  339. continue;
  340. if (!_dpu_rm_check_lm_and_get_connected_blks(rm,
  341. global_state, crtc_id, j,
  342. &pp_idx[lm_count], &dspp_idx[lm_count],
  343. topology)) {
  344. continue;
  345. }
  346. lm_idx[lm_count] = j;
  347. ++lm_count;
  348. }
  349. }
  350. if (lm_count != topology->num_lm) {
  351. DPU_DEBUG("unable to find appropriate mixers\n");
  352. return -ENAVAIL;
  353. }
  354. for (i = 0; i < lm_count; i++) {
  355. global_state->mixer_to_crtc_id[lm_idx[i]] = crtc_id;
  356. global_state->pingpong_to_crtc_id[pp_idx[i]] = crtc_id;
  357. global_state->dspp_to_crtc_id[dspp_idx[i]] =
  358. topology->num_dspp ? crtc_id : 0;
  359. trace_dpu_rm_reserve_lms(lm_idx[i] + LM_0, crtc_id,
  360. pp_idx[i] + PINGPONG_0);
  361. }
  362. return 0;
  363. }
  364. static int _dpu_rm_reserve_ctls(
  365. struct dpu_rm *rm,
  366. struct dpu_global_state *global_state,
  367. uint32_t crtc_id,
  368. const struct msm_display_topology *top)
  369. {
  370. int ctl_idx[MAX_BLOCKS];
  371. int i = 0, j, num_ctls;
  372. bool needs_split_display;
  373. if (rm->has_legacy_ctls) {
  374. /*
  375. * TODO: check if there is a need for special handling if
  376. * DPU < 5.0 get CWB support.
  377. */
  378. num_ctls = top->num_intf;
  379. needs_split_display = _dpu_rm_needs_split_display(top);
  380. } else {
  381. /* use single CTL */
  382. num_ctls = 1;
  383. needs_split_display = false;
  384. }
  385. for (j = 0; j < ARRAY_SIZE(rm->ctl_blks); j++) {
  386. const struct dpu_hw_ctl *ctl;
  387. unsigned long features;
  388. bool has_split_display;
  389. if (!rm->ctl_blks[j])
  390. continue;
  391. if (reserved_by_other(global_state->ctl_to_crtc_id, j, crtc_id))
  392. continue;
  393. ctl = to_dpu_hw_ctl(rm->ctl_blks[j]);
  394. features = ctl->caps->features;
  395. has_split_display = BIT(DPU_CTL_SPLIT_DISPLAY) & features;
  396. DPU_DEBUG("CTL_%d caps 0x%lX\n", j, features);
  397. if (needs_split_display != has_split_display)
  398. continue;
  399. ctl_idx[i] = j;
  400. DPU_DEBUG("CTL_%d match\n", j);
  401. if (++i == num_ctls)
  402. break;
  403. }
  404. if (i != num_ctls)
  405. return -ENAVAIL;
  406. for (i = 0; i < ARRAY_SIZE(ctl_idx) && i < num_ctls; i++) {
  407. global_state->ctl_to_crtc_id[ctl_idx[i]] = crtc_id;
  408. trace_dpu_rm_reserve_ctls(i + CTL_0, crtc_id);
  409. }
  410. return 0;
  411. }
  412. static int _dpu_rm_pingpong_next_index(struct dpu_global_state *global_state,
  413. int start,
  414. uint32_t crtc_id)
  415. {
  416. int i;
  417. for (i = start; i < (PINGPONG_MAX - PINGPONG_0); i++) {
  418. if (global_state->pingpong_to_crtc_id[i] == crtc_id)
  419. return i;
  420. }
  421. return -ENAVAIL;
  422. }
  423. static int _dpu_rm_pingpong_dsc_check(int dsc_idx, int pp_idx)
  424. {
  425. /*
  426. * DSC with even index must be used with the PINGPONG with even index
  427. * DSC with odd index must be used with the PINGPONG with odd index
  428. */
  429. if ((dsc_idx & 0x01) != (pp_idx & 0x01))
  430. return -ENAVAIL;
  431. return 0;
  432. }
  433. static int _dpu_rm_dsc_alloc(struct dpu_rm *rm,
  434. struct dpu_global_state *global_state,
  435. uint32_t crtc_id,
  436. const struct msm_display_topology *top)
  437. {
  438. int num_dsc = 0;
  439. int pp_idx = 0;
  440. int dsc_idx;
  441. int ret;
  442. for (dsc_idx = 0; dsc_idx < ARRAY_SIZE(rm->dsc_blks) &&
  443. num_dsc < top->num_dsc; dsc_idx++) {
  444. if (!rm->dsc_blks[dsc_idx])
  445. continue;
  446. if (reserved_by_other(global_state->dsc_to_crtc_id, dsc_idx, crtc_id))
  447. continue;
  448. pp_idx = _dpu_rm_pingpong_next_index(global_state, pp_idx, crtc_id);
  449. if (pp_idx < 0)
  450. return -ENAVAIL;
  451. ret = _dpu_rm_pingpong_dsc_check(dsc_idx, pp_idx);
  452. if (ret)
  453. return -ENAVAIL;
  454. global_state->dsc_to_crtc_id[dsc_idx] = crtc_id;
  455. num_dsc++;
  456. pp_idx++;
  457. }
  458. if (num_dsc < top->num_dsc) {
  459. DPU_ERROR("DSC allocation failed num_dsc=%d required=%d\n",
  460. num_dsc, top->num_dsc);
  461. return -ENAVAIL;
  462. }
  463. return 0;
  464. }
  465. static int _dpu_rm_dsc_alloc_pair(struct dpu_rm *rm,
  466. struct dpu_global_state *global_state,
  467. uint32_t crtc_id,
  468. const struct msm_display_topology *top)
  469. {
  470. int num_dsc = 0;
  471. int dsc_idx, pp_idx = 0;
  472. int ret;
  473. /* only start from even dsc index */
  474. for (dsc_idx = 0; dsc_idx < ARRAY_SIZE(rm->dsc_blks) &&
  475. num_dsc < top->num_dsc; dsc_idx += 2) {
  476. if (!rm->dsc_blks[dsc_idx] ||
  477. !rm->dsc_blks[dsc_idx + 1])
  478. continue;
  479. /* consective dsc index to be paired */
  480. if (reserved_by_other(global_state->dsc_to_crtc_id, dsc_idx, crtc_id) ||
  481. reserved_by_other(global_state->dsc_to_crtc_id, dsc_idx + 1, crtc_id))
  482. continue;
  483. pp_idx = _dpu_rm_pingpong_next_index(global_state, pp_idx, crtc_id);
  484. if (pp_idx < 0)
  485. return -ENAVAIL;
  486. ret = _dpu_rm_pingpong_dsc_check(dsc_idx, pp_idx);
  487. if (ret) {
  488. pp_idx = 0;
  489. continue;
  490. }
  491. pp_idx = _dpu_rm_pingpong_next_index(global_state, pp_idx + 1, crtc_id);
  492. if (pp_idx < 0)
  493. return -ENAVAIL;
  494. ret = _dpu_rm_pingpong_dsc_check(dsc_idx + 1, pp_idx);
  495. if (ret) {
  496. pp_idx = 0;
  497. continue;
  498. }
  499. global_state->dsc_to_crtc_id[dsc_idx] = crtc_id;
  500. global_state->dsc_to_crtc_id[dsc_idx + 1] = crtc_id;
  501. num_dsc += 2;
  502. pp_idx++; /* start for next pair */
  503. }
  504. if (num_dsc < top->num_dsc) {
  505. DPU_ERROR("DSC allocation failed num_dsc=%d required=%d\n",
  506. num_dsc, top->num_dsc);
  507. return -ENAVAIL;
  508. }
  509. return 0;
  510. }
  511. static int _dpu_rm_reserve_dsc(struct dpu_rm *rm,
  512. struct dpu_global_state *global_state,
  513. uint32_t crtc_id,
  514. const struct msm_display_topology *top)
  515. {
  516. if (!top->num_dsc || !top->num_intf)
  517. return 0;
  518. /*
  519. * Facts:
  520. * 1) no pingpong split (two layer mixers shared one pingpong)
  521. * 2) DSC pair starts from even index, such as index(0,1), (2,3), etc
  522. * 3) even PINGPONG connects to even DSC
  523. * 4) odd PINGPONG connects to odd DSC
  524. * 5) pair: encoder +--> pp_idx_0 --> dsc_idx_0
  525. * +--> pp_idx_1 --> dsc_idx_1
  526. */
  527. /* num_dsc should be either 1, 2 or 4 */
  528. if (top->num_dsc > top->num_intf) /* merge mode */
  529. return _dpu_rm_dsc_alloc_pair(rm, global_state, crtc_id, top);
  530. else
  531. return _dpu_rm_dsc_alloc(rm, global_state, crtc_id, top);
  532. return 0;
  533. }
  534. static int _dpu_rm_reserve_cdm(struct dpu_rm *rm,
  535. struct dpu_global_state *global_state,
  536. uint32_t crtc_id,
  537. int num_cdm)
  538. {
  539. /* try allocating only one CDM block */
  540. if (!rm->cdm_blk) {
  541. DPU_ERROR("CDM block does not exist\n");
  542. return -EIO;
  543. }
  544. if (num_cdm > 1) {
  545. DPU_ERROR("More than 1 INTF requesting CDM\n");
  546. return -EINVAL;
  547. }
  548. if (global_state->cdm_to_crtc_id) {
  549. DPU_ERROR("CDM_0 is already allocated\n");
  550. return -EIO;
  551. }
  552. global_state->cdm_to_crtc_id = crtc_id;
  553. return 0;
  554. }
  555. static int _dpu_rm_make_reservation(
  556. struct dpu_rm *rm,
  557. struct dpu_global_state *global_state,
  558. uint32_t crtc_id,
  559. struct msm_display_topology *topology)
  560. {
  561. int ret;
  562. ret = _dpu_rm_reserve_lms(rm, global_state, crtc_id, topology);
  563. if (ret) {
  564. DPU_ERROR("unable to find appropriate mixers\n");
  565. return ret;
  566. }
  567. if (topology->cwb_enabled) {
  568. ret = _dpu_rm_reserve_cwb_mux_and_pingpongs(rm, global_state,
  569. crtc_id, topology);
  570. if (ret)
  571. return ret;
  572. }
  573. ret = _dpu_rm_reserve_ctls(rm, global_state, crtc_id,
  574. topology);
  575. if (ret) {
  576. DPU_ERROR("unable to find appropriate CTL\n");
  577. return ret;
  578. }
  579. ret = _dpu_rm_reserve_dsc(rm, global_state, crtc_id, topology);
  580. if (ret)
  581. return ret;
  582. if (topology->num_cdm > 0) {
  583. ret = _dpu_rm_reserve_cdm(rm, global_state, crtc_id, topology->num_cdm);
  584. if (ret) {
  585. DPU_ERROR("unable to find CDM blk\n");
  586. return ret;
  587. }
  588. }
  589. return ret;
  590. }
  591. static void _dpu_rm_clear_mapping(uint32_t *res_mapping, int cnt,
  592. uint32_t crtc_id)
  593. {
  594. int i;
  595. for (i = 0; i < cnt; i++) {
  596. if (res_mapping[i] == crtc_id)
  597. res_mapping[i] = 0;
  598. }
  599. }
  600. /**
  601. * dpu_rm_release - Given the encoder for the display chain, release any
  602. * HW blocks previously reserved for that use case.
  603. * @global_state: resources shared across multiple kms objects
  604. * @crtc: DRM CRTC handle
  605. * @return: 0 on Success otherwise -ERROR
  606. */
  607. void dpu_rm_release(struct dpu_global_state *global_state,
  608. struct drm_crtc *crtc)
  609. {
  610. uint32_t crtc_id = crtc->base.id;
  611. _dpu_rm_clear_mapping(global_state->pingpong_to_crtc_id,
  612. ARRAY_SIZE(global_state->pingpong_to_crtc_id), crtc_id);
  613. _dpu_rm_clear_mapping(global_state->mixer_to_crtc_id,
  614. ARRAY_SIZE(global_state->mixer_to_crtc_id), crtc_id);
  615. _dpu_rm_clear_mapping(global_state->ctl_to_crtc_id,
  616. ARRAY_SIZE(global_state->ctl_to_crtc_id), crtc_id);
  617. _dpu_rm_clear_mapping(global_state->dsc_to_crtc_id,
  618. ARRAY_SIZE(global_state->dsc_to_crtc_id), crtc_id);
  619. _dpu_rm_clear_mapping(global_state->dspp_to_crtc_id,
  620. ARRAY_SIZE(global_state->dspp_to_crtc_id), crtc_id);
  621. _dpu_rm_clear_mapping(&global_state->cdm_to_crtc_id, 1, crtc_id);
  622. _dpu_rm_clear_mapping(global_state->cwb_to_crtc_id,
  623. ARRAY_SIZE(global_state->cwb_to_crtc_id), crtc_id);
  624. }
  625. /**
  626. * dpu_rm_reserve - Given a CRTC->Encoder->Connector display chain, analyze
  627. * the use connections and user requirements, specified through related
  628. * topology control properties, and reserve hardware blocks to that
  629. * display chain.
  630. * HW blocks can then be accessed through dpu_rm_get_* functions.
  631. * HW Reservations should be released via dpu_rm_release_hw.
  632. * @rm: DPU Resource Manager handle
  633. * @global_state: resources shared across multiple kms objects
  634. * @crtc: DRM CRTC handle
  635. * @topology: Pointer to topology info for the display
  636. * @return: 0 on Success otherwise -ERROR
  637. */
  638. int dpu_rm_reserve(
  639. struct dpu_rm *rm,
  640. struct dpu_global_state *global_state,
  641. struct drm_crtc *crtc,
  642. struct msm_display_topology *topology)
  643. {
  644. int ret;
  645. if (IS_ERR(global_state)) {
  646. DPU_ERROR("failed to global state\n");
  647. return PTR_ERR(global_state);
  648. }
  649. DRM_DEBUG_KMS("reserving hw for crtc %d\n", crtc->base.id);
  650. DRM_DEBUG_KMS("num_lm: %d num_dsc: %d num_intf: %d\n",
  651. topology->num_lm, topology->num_dsc,
  652. topology->num_intf);
  653. ret = _dpu_rm_make_reservation(rm, global_state, crtc->base.id, topology);
  654. if (ret)
  655. DPU_ERROR("failed to reserve hw resources: %d\n", ret);
  656. return ret;
  657. }
  658. static struct dpu_hw_sspp *dpu_rm_try_sspp(struct dpu_rm *rm,
  659. struct dpu_global_state *global_state,
  660. struct drm_crtc *crtc,
  661. struct dpu_rm_sspp_requirements *reqs,
  662. unsigned int type)
  663. {
  664. uint32_t crtc_id = crtc->base.id;
  665. struct dpu_hw_sspp *hw_sspp;
  666. int i;
  667. for (i = 0; i < ARRAY_SIZE(rm->hw_sspp); i++) {
  668. if (!rm->hw_sspp[i])
  669. continue;
  670. if (global_state->sspp_to_crtc_id[i])
  671. continue;
  672. hw_sspp = rm->hw_sspp[i];
  673. if (hw_sspp->cap->type != type)
  674. continue;
  675. if (reqs->scale && !hw_sspp->cap->sblk->scaler_blk.len)
  676. continue;
  677. // TODO: QSEED2 and RGB scalers are not yet supported
  678. if (reqs->scale && !hw_sspp->ops.setup_scaler)
  679. continue;
  680. if (reqs->yuv && !hw_sspp->cap->sblk->csc_blk.len)
  681. continue;
  682. if (reqs->rot90 && !(hw_sspp->cap->features & DPU_SSPP_INLINE_ROTATION))
  683. continue;
  684. global_state->sspp_to_crtc_id[i] = crtc_id;
  685. return rm->hw_sspp[i];
  686. }
  687. return NULL;
  688. }
  689. /**
  690. * dpu_rm_reserve_sspp - Reserve the required SSPP for the provided CRTC
  691. * @rm: DPU Resource Manager handle
  692. * @global_state: private global state
  693. * @crtc: DRM CRTC handle
  694. * @reqs: SSPP required features
  695. */
  696. struct dpu_hw_sspp *dpu_rm_reserve_sspp(struct dpu_rm *rm,
  697. struct dpu_global_state *global_state,
  698. struct drm_crtc *crtc,
  699. struct dpu_rm_sspp_requirements *reqs)
  700. {
  701. struct dpu_hw_sspp *hw_sspp = NULL;
  702. if (!reqs->scale && !reqs->yuv)
  703. hw_sspp = dpu_rm_try_sspp(rm, global_state, crtc, reqs, SSPP_TYPE_DMA);
  704. if (!hw_sspp && !reqs->yuv)
  705. hw_sspp = dpu_rm_try_sspp(rm, global_state, crtc, reqs, SSPP_TYPE_RGB);
  706. if (!hw_sspp)
  707. hw_sspp = dpu_rm_try_sspp(rm, global_state, crtc, reqs, SSPP_TYPE_VIG);
  708. return hw_sspp;
  709. }
  710. /**
  711. * dpu_rm_release_all_sspp - Given the CRTC, release all SSPP
  712. * blocks previously reserved for that use case.
  713. * @global_state: resources shared across multiple kms objects
  714. * @crtc: DRM CRTC handle
  715. */
  716. void dpu_rm_release_all_sspp(struct dpu_global_state *global_state,
  717. struct drm_crtc *crtc)
  718. {
  719. uint32_t crtc_id = crtc->base.id;
  720. _dpu_rm_clear_mapping(global_state->sspp_to_crtc_id,
  721. ARRAY_SIZE(global_state->sspp_to_crtc_id), crtc_id);
  722. }
  723. static char *dpu_hw_blk_type_name[] = {
  724. [DPU_HW_BLK_TOP] = "TOP",
  725. [DPU_HW_BLK_SSPP] = "SSPP",
  726. [DPU_HW_BLK_LM] = "LM",
  727. [DPU_HW_BLK_CTL] = "CTL",
  728. [DPU_HW_BLK_PINGPONG] = "pingpong",
  729. [DPU_HW_BLK_INTF] = "INTF",
  730. [DPU_HW_BLK_WB] = "WB",
  731. [DPU_HW_BLK_DSPP] = "DSPP",
  732. [DPU_HW_BLK_MERGE_3D] = "merge_3d",
  733. [DPU_HW_BLK_DSC] = "DSC",
  734. [DPU_HW_BLK_CDM] = "CDM",
  735. [DPU_HW_BLK_MAX] = "unknown",
  736. };
  737. /**
  738. * dpu_rm_get_assigned_resources - Get hw resources of the given type that are
  739. * assigned to this encoder
  740. * @rm: DPU Resource Manager handle
  741. * @global_state: resources shared across multiple kms objects
  742. * @crtc: DRM CRTC handle
  743. * @type: resource type to return data for
  744. * @blks: pointer to the array to be filled by HW resources
  745. * @blks_size: size of the @blks array
  746. */
  747. int dpu_rm_get_assigned_resources(struct dpu_rm *rm,
  748. struct dpu_global_state *global_state, struct drm_crtc *crtc,
  749. enum dpu_hw_blk_type type, struct dpu_hw_blk **blks, int blks_size)
  750. {
  751. uint32_t crtc_id = crtc->base.id;
  752. struct dpu_hw_blk **hw_blks;
  753. uint32_t *hw_to_crtc_id;
  754. int i, num_blks, max_blks;
  755. switch (type) {
  756. case DPU_HW_BLK_PINGPONG:
  757. case DPU_HW_BLK_DCWB_PINGPONG:
  758. hw_blks = rm->pingpong_blks;
  759. hw_to_crtc_id = global_state->pingpong_to_crtc_id;
  760. max_blks = ARRAY_SIZE(rm->pingpong_blks);
  761. break;
  762. case DPU_HW_BLK_LM:
  763. hw_blks = rm->mixer_blks;
  764. hw_to_crtc_id = global_state->mixer_to_crtc_id;
  765. max_blks = ARRAY_SIZE(rm->mixer_blks);
  766. break;
  767. case DPU_HW_BLK_CTL:
  768. hw_blks = rm->ctl_blks;
  769. hw_to_crtc_id = global_state->ctl_to_crtc_id;
  770. max_blks = ARRAY_SIZE(rm->ctl_blks);
  771. break;
  772. case DPU_HW_BLK_DSPP:
  773. hw_blks = rm->dspp_blks;
  774. hw_to_crtc_id = global_state->dspp_to_crtc_id;
  775. max_blks = ARRAY_SIZE(rm->dspp_blks);
  776. break;
  777. case DPU_HW_BLK_DSC:
  778. hw_blks = rm->dsc_blks;
  779. hw_to_crtc_id = global_state->dsc_to_crtc_id;
  780. max_blks = ARRAY_SIZE(rm->dsc_blks);
  781. break;
  782. case DPU_HW_BLK_CDM:
  783. hw_blks = &rm->cdm_blk;
  784. hw_to_crtc_id = &global_state->cdm_to_crtc_id;
  785. max_blks = 1;
  786. break;
  787. case DPU_HW_BLK_CWB:
  788. hw_blks = rm->cwb_blks;
  789. hw_to_crtc_id = global_state->cwb_to_crtc_id;
  790. max_blks = ARRAY_SIZE(rm->cwb_blks);
  791. break;
  792. default:
  793. DPU_ERROR("blk type %d not managed by rm\n", type);
  794. return 0;
  795. }
  796. num_blks = 0;
  797. for (i = 0; i < max_blks; i++) {
  798. if (hw_to_crtc_id[i] != crtc_id)
  799. continue;
  800. if (type == DPU_HW_BLK_PINGPONG) {
  801. struct dpu_hw_pingpong *pp = to_dpu_hw_pingpong(hw_blks[i]);
  802. if (pp->idx >= PINGPONG_CWB_0)
  803. continue;
  804. }
  805. if (type == DPU_HW_BLK_DCWB_PINGPONG) {
  806. struct dpu_hw_pingpong *pp = to_dpu_hw_pingpong(hw_blks[i]);
  807. if (pp->idx < PINGPONG_CWB_0)
  808. continue;
  809. }
  810. if (num_blks == blks_size) {
  811. DPU_ERROR("More than %d %s assigned to crtc %d\n",
  812. blks_size, dpu_hw_blk_type_name[type], crtc_id);
  813. break;
  814. }
  815. if (!hw_blks[i]) {
  816. DPU_ERROR("%s unavailable to assign to crtc %d\n",
  817. dpu_hw_blk_type_name[type], crtc_id);
  818. break;
  819. }
  820. blks[num_blks++] = hw_blks[i];
  821. }
  822. return num_blks;
  823. }
  824. static void dpu_rm_print_state_helper(struct drm_printer *p,
  825. struct dpu_hw_blk *blk,
  826. uint32_t mapping)
  827. {
  828. if (!blk)
  829. drm_puts(p, "- ");
  830. else if (!mapping)
  831. drm_puts(p, "# ");
  832. else
  833. drm_printf(p, "%d ", mapping);
  834. }
  835. /**
  836. * dpu_rm_print_state - output the RM private state
  837. * @p: DRM printer
  838. * @global_state: global state
  839. */
  840. void dpu_rm_print_state(struct drm_printer *p,
  841. const struct dpu_global_state *global_state)
  842. {
  843. const struct dpu_rm *rm = global_state->rm;
  844. int i;
  845. drm_puts(p, "resource mapping:\n");
  846. drm_puts(p, "\tpingpong=");
  847. for (i = 0; i < ARRAY_SIZE(global_state->pingpong_to_crtc_id); i++)
  848. dpu_rm_print_state_helper(p, rm->pingpong_blks[i],
  849. global_state->pingpong_to_crtc_id[i]);
  850. drm_puts(p, "\n");
  851. drm_puts(p, "\tmixer=");
  852. for (i = 0; i < ARRAY_SIZE(global_state->mixer_to_crtc_id); i++)
  853. dpu_rm_print_state_helper(p, rm->mixer_blks[i],
  854. global_state->mixer_to_crtc_id[i]);
  855. drm_puts(p, "\n");
  856. drm_puts(p, "\tctl=");
  857. for (i = 0; i < ARRAY_SIZE(global_state->ctl_to_crtc_id); i++)
  858. dpu_rm_print_state_helper(p, rm->ctl_blks[i],
  859. global_state->ctl_to_crtc_id[i]);
  860. drm_puts(p, "\n");
  861. drm_puts(p, "\tdspp=");
  862. for (i = 0; i < ARRAY_SIZE(global_state->dspp_to_crtc_id); i++)
  863. dpu_rm_print_state_helper(p, rm->dspp_blks[i],
  864. global_state->dspp_to_crtc_id[i]);
  865. drm_puts(p, "\n");
  866. drm_puts(p, "\tdsc=");
  867. for (i = 0; i < ARRAY_SIZE(global_state->dsc_to_crtc_id); i++)
  868. dpu_rm_print_state_helper(p, rm->dsc_blks[i],
  869. global_state->dsc_to_crtc_id[i]);
  870. drm_puts(p, "\n");
  871. drm_puts(p, "\tcdm=");
  872. dpu_rm_print_state_helper(p, rm->cdm_blk,
  873. global_state->cdm_to_crtc_id);
  874. drm_puts(p, "\n");
  875. drm_puts(p, "\tsspp=");
  876. /* skip SSPP_NONE and start from the next index */
  877. for (i = SSPP_NONE + 1; i < ARRAY_SIZE(global_state->sspp_to_crtc_id); i++)
  878. dpu_rm_print_state_helper(p, rm->hw_sspp[i] ? &rm->hw_sspp[i]->base : NULL,
  879. global_state->sspp_to_crtc_id[i]);
  880. drm_puts(p, "\n");
  881. drm_puts(p, "\tcwb=");
  882. for (i = 0; i < ARRAY_SIZE(global_state->cwb_to_crtc_id); i++)
  883. dpu_rm_print_state_helper(p, rm->cwb_blks[i],
  884. global_state->cwb_to_crtc_id[i]);
  885. drm_puts(p, "\n");
  886. }