sde_power_handle.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) "[drm:%s:%d]: " fmt, __func__, __LINE__
  7. #include <linux/clk.h>
  8. #include <linux/kernel.h>
  9. #include <linux/of.h>
  10. #include <linux/string.h>
  11. #include <linux/of_address.h>
  12. #include <linux/slab.h>
  13. #include <linux/mutex.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/sde_io_util.h>
  16. #include <linux/sde_rsc.h>
  17. #include <linux/soc/qcom/msm_mmrm.h>
  18. #include "sde_power_handle.h"
  19. #include "sde_trace.h"
  20. #include "sde_dbg.h"
  21. #define KBPS2BPS(x) ((x) * 1000ULL)
  22. /* wait for at most 2 vsync for lowest refresh rate (1hz) */
  23. #define SDE_MMRM_CB_TIMEOUT_MS 2000
  24. #define SDE_MMRM_CB_TIMEOUT_JIFFIES msecs_to_jiffies( \
  25. SDE_MMRM_CB_TIMEOUT_MS)
  26. static const char *data_bus_name[SDE_POWER_HANDLE_DBUS_ID_MAX] = {
  27. [SDE_POWER_HANDLE_DBUS_ID_MNOC] = "qcom,sde-data-bus",
  28. [SDE_POWER_HANDLE_DBUS_ID_LLCC] = "qcom,sde-llcc-bus",
  29. [SDE_POWER_HANDLE_DBUS_ID_EBI] = "qcom,sde-ebi-bus",
  30. };
  31. const char *sde_power_handle_get_dbus_name(u32 bus_id)
  32. {
  33. if (bus_id < SDE_POWER_HANDLE_DBUS_ID_MAX)
  34. return data_bus_name[bus_id];
  35. return NULL;
  36. }
  37. static int sde_power_event_trigger_locked(struct sde_power_handle *phandle,
  38. u32 event_type)
  39. {
  40. struct sde_power_event *event;
  41. int ret = -EPERM;
  42. phandle->last_event_handled = event_type;
  43. list_for_each_entry(event, &phandle->event_list, list) {
  44. if (event->event_type & event_type) {
  45. event->cb_fnc(event_type, event->usr);
  46. ret = 0;
  47. }
  48. }
  49. return ret;
  50. }
  51. static inline void sde_power_rsc_client_init(struct sde_power_handle *phandle)
  52. {
  53. /* creates the rsc client */
  54. if (!phandle->rsc_client_init) {
  55. phandle->rsc_client = sde_rsc_client_create(SDE_RSC_INDEX,
  56. "sde_power_handle", SDE_RSC_CLK_CLIENT, 0);
  57. if (IS_ERR_OR_NULL(phandle->rsc_client)) {
  58. pr_debug("sde rsc client create failed :%ld\n",
  59. PTR_ERR(phandle->rsc_client));
  60. phandle->rsc_client = NULL;
  61. }
  62. phandle->rsc_client_init = true;
  63. }
  64. }
  65. static int sde_power_rsc_update(struct sde_power_handle *phandle, bool enable)
  66. {
  67. u32 rsc_state;
  68. int ret = 0;
  69. rsc_state = enable ? SDE_RSC_CLK_STATE : SDE_RSC_IDLE_STATE;
  70. if (phandle->rsc_client)
  71. ret = sde_rsc_client_state_update(phandle->rsc_client,
  72. rsc_state, NULL, SDE_RSC_INVALID_CRTC_ID, NULL);
  73. return ret;
  74. }
  75. static int sde_power_parse_dt_supply(struct platform_device *pdev,
  76. struct dss_module_power *mp)
  77. {
  78. int i = 0, rc = 0;
  79. u32 tmp = 0;
  80. struct device_node *of_node = NULL, *supply_root_node = NULL;
  81. struct device_node *supply_node = NULL;
  82. if (!pdev || !mp) {
  83. pr_err("invalid input param pdev:%pK mp:%pK\n", pdev, mp);
  84. return -EINVAL;
  85. }
  86. of_node = pdev->dev.of_node;
  87. mp->num_vreg = 0;
  88. supply_root_node = of_get_child_by_name(of_node,
  89. "qcom,platform-supply-entries");
  90. if (!supply_root_node) {
  91. pr_debug("no supply entry present\n");
  92. return rc;
  93. }
  94. for_each_child_of_node(supply_root_node, supply_node)
  95. mp->num_vreg++;
  96. if (mp->num_vreg == 0) {
  97. pr_debug("no vreg\n");
  98. return rc;
  99. }
  100. pr_debug("vreg found. count=%d\n", mp->num_vreg);
  101. mp->vreg_config = devm_kzalloc(&pdev->dev, sizeof(struct dss_vreg) *
  102. mp->num_vreg, GFP_KERNEL);
  103. if (!mp->vreg_config) {
  104. rc = -ENOMEM;
  105. return rc;
  106. }
  107. for_each_child_of_node(supply_root_node, supply_node) {
  108. const char *st = NULL;
  109. rc = of_property_read_string(supply_node,
  110. "qcom,supply-name", &st);
  111. if (rc) {
  112. pr_err("error reading name. rc=%d\n", rc);
  113. goto error;
  114. }
  115. strlcpy(mp->vreg_config[i].vreg_name, st,
  116. sizeof(mp->vreg_config[i].vreg_name));
  117. rc = of_property_read_u32(supply_node,
  118. "qcom,supply-min-voltage", &tmp);
  119. if (rc) {
  120. pr_err("error reading min volt. rc=%d\n", rc);
  121. goto error;
  122. }
  123. mp->vreg_config[i].min_voltage = tmp;
  124. rc = of_property_read_u32(supply_node,
  125. "qcom,supply-max-voltage", &tmp);
  126. if (rc) {
  127. pr_err("error reading max volt. rc=%d\n", rc);
  128. goto error;
  129. }
  130. mp->vreg_config[i].max_voltage = tmp;
  131. rc = of_property_read_u32(supply_node,
  132. "qcom,supply-enable-load", &tmp);
  133. if (rc) {
  134. pr_err("error reading enable load. rc=%d\n", rc);
  135. goto error;
  136. }
  137. mp->vreg_config[i].enable_load = tmp;
  138. rc = of_property_read_u32(supply_node,
  139. "qcom,supply-disable-load", &tmp);
  140. if (rc) {
  141. pr_err("error reading disable load. rc=%d\n", rc);
  142. goto error;
  143. }
  144. mp->vreg_config[i].disable_load = tmp;
  145. rc = of_property_read_u32(supply_node,
  146. "qcom,supply-pre-on-sleep", &tmp);
  147. if (rc)
  148. pr_debug("error reading supply pre sleep value. rc=%d\n",
  149. rc);
  150. mp->vreg_config[i].pre_on_sleep = (!rc ? tmp : 0);
  151. rc = of_property_read_u32(supply_node,
  152. "qcom,supply-pre-off-sleep", &tmp);
  153. if (rc)
  154. pr_debug("error reading supply pre sleep value. rc=%d\n",
  155. rc);
  156. mp->vreg_config[i].pre_off_sleep = (!rc ? tmp : 0);
  157. rc = of_property_read_u32(supply_node,
  158. "qcom,supply-post-on-sleep", &tmp);
  159. if (rc)
  160. pr_debug("error reading supply post sleep value. rc=%d\n",
  161. rc);
  162. mp->vreg_config[i].post_on_sleep = (!rc ? tmp : 0);
  163. rc = of_property_read_u32(supply_node,
  164. "qcom,supply-post-off-sleep", &tmp);
  165. if (rc)
  166. pr_debug("error reading supply post sleep value. rc=%d\n",
  167. rc);
  168. mp->vreg_config[i].post_off_sleep = (!rc ? tmp : 0);
  169. pr_debug("%s min=%d, max=%d, enable=%d, disable=%d, preonsleep=%d, postonsleep=%d, preoffsleep=%d, postoffsleep=%d\n",
  170. mp->vreg_config[i].vreg_name,
  171. mp->vreg_config[i].min_voltage,
  172. mp->vreg_config[i].max_voltage,
  173. mp->vreg_config[i].enable_load,
  174. mp->vreg_config[i].disable_load,
  175. mp->vreg_config[i].pre_on_sleep,
  176. mp->vreg_config[i].post_on_sleep,
  177. mp->vreg_config[i].pre_off_sleep,
  178. mp->vreg_config[i].post_off_sleep);
  179. ++i;
  180. rc = 0;
  181. }
  182. return rc;
  183. error:
  184. if (mp->vreg_config) {
  185. devm_kfree(&pdev->dev, mp->vreg_config);
  186. mp->vreg_config = NULL;
  187. mp->num_vreg = 0;
  188. }
  189. return rc;
  190. }
  191. static int sde_power_parse_dt_clock(struct platform_device *pdev,
  192. struct dss_module_power *mp)
  193. {
  194. u32 i = 0, rc = 0;
  195. const char *clock_name;
  196. u32 clock_rate = 0;
  197. u32 clock_mmrm = 0;
  198. u32 clock_max_rate = 0;
  199. int num_clk = 0;
  200. bool is_mmrm_supported = false;
  201. if (!pdev || !mp) {
  202. pr_err("invalid input param pdev:%pK mp:%pK\n", pdev, mp);
  203. return -EINVAL;
  204. }
  205. mp->num_clk = 0;
  206. num_clk = of_property_count_strings(pdev->dev.of_node,
  207. "clock-names");
  208. if (num_clk <= 0) {
  209. pr_debug("clocks are not defined\n");
  210. goto clk_err;
  211. }
  212. mp->num_clk = num_clk;
  213. mp->clk_config = devm_kzalloc(&pdev->dev,
  214. sizeof(struct dss_clk) * num_clk, GFP_KERNEL);
  215. if (!mp->clk_config) {
  216. rc = -ENOMEM;
  217. mp->num_clk = 0;
  218. goto clk_err;
  219. }
  220. is_mmrm_supported = mmrm_client_check_scaling_supported(MMRM_CLIENT_CLOCK,
  221. MMRM_CLIENT_DOMAIN_DISPLAY);
  222. for (i = 0; i < num_clk; i++) {
  223. of_property_read_string_index(pdev->dev.of_node, "clock-names",
  224. i, &clock_name);
  225. strlcpy(mp->clk_config[i].clk_name, clock_name,
  226. sizeof(mp->clk_config[i].clk_name));
  227. of_property_read_u32_index(pdev->dev.of_node, "clock-rate",
  228. i, &clock_rate);
  229. mp->clk_config[i].rate = clock_rate;
  230. if (!clock_rate)
  231. mp->clk_config[i].type = DSS_CLK_AHB;
  232. else
  233. mp->clk_config[i].type = DSS_CLK_PCLK;
  234. clock_mmrm = 0;
  235. of_property_read_u32_index(pdev->dev.of_node, "clock-mmrm",
  236. i, &clock_mmrm);
  237. if (clock_mmrm && is_mmrm_supported) {
  238. mp->clk_config[i].type = DSS_CLK_MMRM;
  239. mp->clk_config[i].mmrm.clk_id = clock_mmrm;
  240. }
  241. pr_debug("clk[%d] clock-mmrm:%d mmrm status:%d rate:%d name:%s dev:%s\n",
  242. i, clock_mmrm, is_mmrm_supported, clock_rate, clock_name,
  243. pdev->name ? pdev->name : "<unknown>");
  244. clock_max_rate = 0;
  245. of_property_read_u32_index(pdev->dev.of_node, "clock-max-rate",
  246. i, &clock_max_rate);
  247. mp->clk_config[i].max_rate = clock_max_rate;
  248. }
  249. clk_err:
  250. return rc;
  251. }
  252. #define MAX_AXI_PORT_COUNT 3
  253. static int _sde_power_data_bus_set_quota(
  254. struct sde_power_data_bus_handle *pdbus,
  255. u64 in_ab_quota, u64 in_ib_quota)
  256. {
  257. int rc = 0, i = 0;
  258. u32 paths = pdbus->data_paths_cnt;
  259. if (!paths || paths > DATA_BUS_PATH_MAX) {
  260. pr_err("invalid data bus handle, paths %d\n", paths);
  261. return -EINVAL;
  262. }
  263. in_ab_quota = div_u64(in_ab_quota, paths);
  264. SDE_ATRACE_BEGIN("msm_bus_scale_req");
  265. for (i = 0; i < paths; i++) {
  266. if (pdbus->data_bus_hdl[i]) {
  267. rc = icc_set_bw(pdbus->data_bus_hdl[i],
  268. Bps_to_icc(in_ab_quota),
  269. Bps_to_icc(in_ib_quota));
  270. if (rc)
  271. goto err;
  272. }
  273. }
  274. pdbus->curr_val.ab = in_ab_quota;
  275. pdbus->curr_val.ib = in_ib_quota;
  276. SDE_ATRACE_END("msm_bus_scale_req");
  277. return rc;
  278. err:
  279. for (; i >= 0; --i)
  280. if (pdbus->data_bus_hdl[i])
  281. icc_set_bw(pdbus->data_bus_hdl[i],
  282. Bps_to_icc(pdbus->curr_val.ab),
  283. Bps_to_icc(pdbus->curr_val.ib));
  284. SDE_ATRACE_END("msm_bus_scale_req");
  285. pr_err("failed to set data bus vote ab=%llu ib=%llu rc=%d\n",
  286. in_ab_quota, in_ib_quota, rc);
  287. return rc;
  288. }
  289. int sde_power_data_bus_set_quota(struct sde_power_handle *phandle,
  290. u32 bus_id, u64 ab_quota, u64 ib_quota)
  291. {
  292. int rc = 0;
  293. u32 paths;
  294. if (!phandle || bus_id >= SDE_POWER_HANDLE_DBUS_ID_MAX) {
  295. pr_err("invalid parameters\n");
  296. return -EINVAL;
  297. }
  298. paths = phandle->data_bus_handle[bus_id].data_paths_cnt;
  299. if (!paths)
  300. goto skip_vote;
  301. trace_sde_perf_update_bus(bus_id, ab_quota, ib_quota, paths);
  302. mutex_lock(&phandle->phandle_lock);
  303. rc = _sde_power_data_bus_set_quota(&phandle->data_bus_handle[bus_id],
  304. ab_quota, ib_quota);
  305. mutex_unlock(&phandle->phandle_lock);
  306. skip_vote:
  307. pr_debug("bus=%d, ab=%llu, ib=%llu, paths=%d\n", bus_id, ab_quota,
  308. ib_quota, paths);
  309. return rc;
  310. }
  311. /**
  312. * sde_power_icc_get - get the interconnect path for the given bus_name
  313. * @pdev - platform device
  314. * @bus_name - bus name for the corresponding interconnect
  315. * @path - the icc_path object we want to obtain for this @bus_name (output)
  316. * @count - if given, incremented only if the path was successfully retrieved
  317. **/
  318. static int sde_power_icc_get(struct platform_device *pdev,
  319. const char *bus_name, struct icc_path **path, u32 *count)
  320. {
  321. int rc = of_property_match_string(pdev->dev.of_node,
  322. "interconnect-names", bus_name);
  323. /* bus_names are optional for any given device node, skip if missing */
  324. if (rc < 0)
  325. goto end;
  326. *path = of_icc_get(&pdev->dev, bus_name);
  327. if (IS_ERR_OR_NULL(*path)) {
  328. rc = PTR_ERR(*path);
  329. pr_err("bus %s parsing failed, rc:%d\n", bus_name, rc);
  330. *path = NULL;
  331. return rc;
  332. }
  333. if (count)
  334. (*count)++;
  335. end:
  336. pr_debug("bus %s dt node %s(%d), icc_path is %s, count:%d\n",
  337. bus_name, rc < 0 ? "missing" : "found", rc,
  338. *path ? "valid" : "NULL", count ? *count : -1);
  339. return 0;
  340. }
  341. static int sde_power_reg_bus_parse(struct platform_device *pdev,
  342. struct sde_power_reg_bus_handle *reg_bus)
  343. {
  344. const char *bus_name = "qcom,sde-reg-bus";
  345. const u32 *vec_arr = NULL;
  346. int rc, len, i, vec_idx = 0;
  347. u32 paths = 0;
  348. rc = sde_power_icc_get(pdev, bus_name, &reg_bus->reg_bus_hdl, &paths);
  349. if (rc)
  350. return rc;
  351. if (!paths) {
  352. pr_debug("%s not defined for pdev %s\n", bus_name, pdev->name ?
  353. pdev->name : "<unknown>");
  354. return 0;
  355. }
  356. vec_arr = of_get_property(pdev->dev.of_node,
  357. "qcom,sde-reg-bus,vectors-KBps", &len);
  358. if (!vec_arr) {
  359. pr_err("%s scale table property not found\n", bus_name);
  360. return -EINVAL;
  361. }
  362. if (len / sizeof(*vec_arr) != VOTE_INDEX_MAX * 2) {
  363. pr_err("wrong size for %s vector table\n", bus_name);
  364. return -EINVAL;
  365. }
  366. for (i = 0; i < VOTE_INDEX_MAX; ++i) {
  367. reg_bus->scale_table[i].ab = (u64)KBPS2BPS(be32_to_cpu(
  368. vec_arr[vec_idx++]));
  369. reg_bus->scale_table[i].ib = (u64)KBPS2BPS(be32_to_cpu(
  370. vec_arr[vec_idx++]));
  371. }
  372. return rc;
  373. }
  374. static int sde_power_mnoc_bus_parse(struct platform_device *pdev,
  375. struct sde_power_data_bus_handle *pdbus, const char *name)
  376. {
  377. int i, rc = 0;
  378. char bus_name[32];
  379. for (i = 0; i < DATA_BUS_PATH_MAX; ++i) {
  380. snprintf(bus_name, sizeof(bus_name), "%s%d", name, i);
  381. rc = sde_power_icc_get(pdev, bus_name, &pdbus->data_bus_hdl[i],
  382. &pdbus->data_paths_cnt);
  383. if (rc)
  384. break;
  385. }
  386. /* at least one databus path is required */
  387. if (!pdbus->data_paths_cnt) {
  388. pr_info("mnoc interconnect path(s) not defined, rc: %d\n", rc);
  389. } else if (rc) {
  390. pr_info("ignoring error %d for non-primary data path\n", rc);
  391. rc = 0;
  392. }
  393. return rc;
  394. }
  395. static int sde_power_bus_parse(struct platform_device *pdev,
  396. struct sde_power_handle *phandle)
  397. {
  398. int i, j, ib_quota_count, rc = 0;
  399. bool active_only = false;
  400. struct sde_power_data_bus_handle *pdbus = phandle->data_bus_handle;
  401. u32 ib_quota[SDE_POWER_HANDLE_DBUS_ID_MAX];
  402. ib_quota_count = of_property_count_u32_elems(pdev->dev.of_node, "qcom,sde-ib-bw-vote");
  403. if (ib_quota_count > 0) {
  404. if (ib_quota_count != SDE_POWER_HANDLE_DBUS_ID_MAX) {
  405. pr_err("wrong size for qcom,sde-ib-bw-vote\n");
  406. return -EINVAL;
  407. }
  408. for (i = 0; i < SDE_POWER_HANDLE_DBUS_ID_MAX; ++i) {
  409. of_property_read_u32_index(pdev->dev.of_node,
  410. "qcom,sde-ib-bw-vote", i, &ib_quota[i]);
  411. phandle->ib_quota[i] = ib_quota[i]*1000;
  412. }
  413. }
  414. /* reg bus */
  415. rc = sde_power_reg_bus_parse(pdev, &phandle->reg_bus_handle);
  416. if (rc)
  417. return rc;
  418. /* data buses */
  419. if (of_find_property(pdev->dev.of_node,
  420. "qcom,msm-bus,active-only", NULL))
  421. active_only = true;
  422. for (i = SDE_POWER_HANDLE_DBUS_ID_MNOC;
  423. i < SDE_POWER_HANDLE_DBUS_ID_MAX; ++i) {
  424. if (i == SDE_POWER_HANDLE_DBUS_ID_MNOC)
  425. rc = sde_power_mnoc_bus_parse(pdev, &pdbus[i],
  426. data_bus_name[i]);
  427. else
  428. rc = sde_power_icc_get(pdev, data_bus_name[i],
  429. &pdbus[i].data_bus_hdl[0],
  430. &pdbus[i].data_paths_cnt);
  431. if (rc)
  432. break;
  433. if (active_only) {
  434. pdbus[i].bus_active_only = true;
  435. for (j = 0; j < pdbus[i].data_paths_cnt; ++j)
  436. icc_set_tag(pdbus[i].data_bus_hdl[j],
  437. QCOM_ICC_TAG_ACTIVE_ONLY);
  438. }
  439. pr_debug("found %d paths for %s\n", pdbus[i].data_paths_cnt,
  440. data_bus_name[i]);
  441. }
  442. return rc;
  443. }
  444. static void sde_power_bus_unregister(struct sde_power_handle *phandle)
  445. {
  446. int i, j;
  447. struct sde_power_reg_bus_handle *reg_bus = &phandle->reg_bus_handle;
  448. struct sde_power_data_bus_handle *pdbus = phandle->data_bus_handle;
  449. icc_put(reg_bus->reg_bus_hdl);
  450. reg_bus->reg_bus_hdl = NULL;
  451. for (i = SDE_POWER_HANDLE_DBUS_ID_MAX - 1;
  452. i >= SDE_POWER_HANDLE_DBUS_ID_MNOC; i--) {
  453. for (j = 0; j < pdbus[i].data_paths_cnt; j++) {
  454. if (pdbus[i].data_bus_hdl[j]) {
  455. icc_put(pdbus[i].data_bus_hdl[j]);
  456. pdbus[i].data_bus_hdl[j] = NULL;
  457. }
  458. }
  459. }
  460. }
  461. static int sde_power_reg_bus_update(struct sde_power_reg_bus_handle *reg_bus,
  462. u32 usecase_ndx)
  463. {
  464. int rc = 0;
  465. u64 ab_quota, ib_quota;
  466. ab_quota = reg_bus->scale_table[usecase_ndx].ab;
  467. ib_quota = reg_bus->scale_table[usecase_ndx].ib;
  468. if (reg_bus->reg_bus_hdl) {
  469. SDE_ATRACE_BEGIN("msm_bus_scale_req");
  470. rc = icc_set_bw(reg_bus->reg_bus_hdl, Bps_to_icc(ab_quota),
  471. Bps_to_icc(ib_quota));
  472. SDE_ATRACE_END("msm_bus_scale_req");
  473. }
  474. if (rc)
  475. pr_err("failed to set reg bus vote to index %d, rc=%d\n",
  476. usecase_ndx, rc);
  477. else {
  478. reg_bus->curr_idx = usecase_ndx;
  479. pr_debug("reg-bus vote set to index=%d, ab=%llu, ib=%llu\n",
  480. usecase_ndx, ab_quota, ib_quota);
  481. }
  482. return rc;
  483. }
  484. int sde_power_mmrm_set_clk_limit(struct dss_clk *clk,
  485. struct sde_power_handle *phandle, unsigned long requested_clk)
  486. {
  487. int ret;
  488. clk->mmrm.mmrm_requested_clk = requested_clk;
  489. SDE_EVT32_VERBOSE(SDE_EVTLOG_FUNC_ENTRY,
  490. clk->mmrm.mmrm_requested_clk);
  491. ret = sde_power_event_trigger_locked(phandle,
  492. SDE_POWER_EVENT_MMRM_CALLBACK);
  493. if (ret) {
  494. /* no crtc's present, we cannot process the cb */
  495. pr_err("error cannot process mmrm cb\n");
  496. goto exit;
  497. }
  498. SDE_EVT32_VERBOSE(SDE_EVTLOG_FUNC_CASE1,
  499. clk->mmrm.mmrm_requested_clk);
  500. /* wait for the request to reduce the clk */
  501. ret = wait_event_timeout(clk->mmrm.mmrm_cb_wq,
  502. clk->mmrm.mmrm_requested_clk == 0,
  503. SDE_MMRM_CB_TIMEOUT_JIFFIES);
  504. if (!ret) {
  505. /* requested clk was not reduced, fail cb */
  506. ret = -EPERM;
  507. /* Clear the request */
  508. clk->mmrm.mmrm_requested_clk = 0;
  509. pr_err("error cannot process mmrm cb clk request\n");
  510. } else {
  511. ret = 0; // Succeed, clk was reduced
  512. }
  513. exit:
  514. SDE_EVT32(SDE_EVTLOG_FUNC_EXIT, ret);
  515. return ret;
  516. }
  517. int sde_power_mmrm_callback(
  518. struct mmrm_client_notifier_data *notifier_data)
  519. {
  520. struct dss_clk_mmrm_cb *mmrm_cb_data =
  521. (struct dss_clk_mmrm_cb *)notifier_data->pvt_data;
  522. struct sde_power_handle *phandle =
  523. (struct sde_power_handle *)mmrm_cb_data->phandle;
  524. struct dss_clk *clk = mmrm_cb_data->clk;
  525. int ret = -EPERM;
  526. if (notifier_data->cb_type == MMRM_CLIENT_RESOURCE_VALUE_CHANGE) {
  527. unsigned long requested_clk =
  528. notifier_data->cb_data.val_chng.new_val;
  529. ret = sde_power_mmrm_set_clk_limit(clk, phandle, requested_clk);
  530. if (ret)
  531. pr_err("mmrm callback error reducing clk:%lu ret:%d\n",
  532. requested_clk, ret);
  533. }
  534. return ret;
  535. }
  536. u64 sde_power_mmrm_get_requested_clk(struct sde_power_handle *phandle,
  537. char *clock_name)
  538. {
  539. struct dss_module_power *mp;
  540. u64 rate = -EINVAL;
  541. int i;
  542. if (!phandle) {
  543. pr_err("invalid input power handle\n");
  544. return -EINVAL;
  545. }
  546. mp = &phandle->mp;
  547. for (i = 0; i < mp->num_clk; i++) {
  548. if (!strcmp(mp->clk_config[i].clk_name, clock_name)) {
  549. rate = mp->clk_config[i].mmrm.mmrm_requested_clk;
  550. break;
  551. }
  552. }
  553. return rate;
  554. }
  555. int sde_power_resource_init(struct platform_device *pdev,
  556. struct sde_power_handle *phandle)
  557. {
  558. int rc = 0;
  559. struct dss_module_power *mp;
  560. if (!phandle || !pdev) {
  561. pr_err("invalid input param\n");
  562. rc = -EINVAL;
  563. goto end;
  564. }
  565. mp = &phandle->mp;
  566. phandle->dev = &pdev->dev;
  567. /* event init must happen before mmrm register */
  568. INIT_LIST_HEAD(&phandle->event_list);
  569. rc = sde_power_parse_dt_clock(pdev, mp);
  570. if (rc) {
  571. pr_err("device clock parsing failed\n");
  572. goto end;
  573. }
  574. rc = sde_power_parse_dt_supply(pdev, mp);
  575. if (rc) {
  576. pr_err("device vreg supply parsing failed\n");
  577. goto parse_vreg_err;
  578. }
  579. rc = msm_dss_get_vreg(&pdev->dev,
  580. mp->vreg_config, mp->num_vreg, 1);
  581. if (rc) {
  582. pr_err("get config failed rc=%d\n", rc);
  583. goto vreg_err;
  584. }
  585. rc = msm_dss_get_clk(&pdev->dev, mp->clk_config, mp->num_clk);
  586. if (rc) {
  587. pr_err("clock get failed rc=%d\n", rc);
  588. goto clkget_err;
  589. }
  590. rc = msm_dss_mmrm_register(&pdev->dev, mp,
  591. sde_power_mmrm_callback, (void *)phandle,
  592. &phandle->mmrm_enable);
  593. if (rc) {
  594. pr_err("mmrm register failed rc=%d\n", rc);
  595. goto clkmmrm_err;
  596. }
  597. rc = msm_dss_clk_set_rate(mp->clk_config, mp->num_clk);
  598. if (rc) {
  599. pr_err("clock set rate failed rc=%d\n", rc);
  600. goto clkset_err;
  601. }
  602. rc = sde_power_bus_parse(pdev, phandle);
  603. if (rc) {
  604. pr_err("bus parse failed rc=%d\n", rc);
  605. goto bus_err;
  606. }
  607. phandle->rsc_client = NULL;
  608. phandle->rsc_client_init = false;
  609. mutex_init(&phandle->phandle_lock);
  610. return rc;
  611. bus_err:
  612. sde_power_bus_unregister(phandle);
  613. clkset_err:
  614. msm_dss_mmrm_deregister(&pdev->dev, mp);
  615. clkmmrm_err:
  616. msm_dss_put_clk(mp->clk_config, mp->num_clk);
  617. clkget_err:
  618. msm_dss_get_vreg(&pdev->dev, mp->vreg_config, mp->num_vreg, 0);
  619. vreg_err:
  620. if (mp->vreg_config)
  621. devm_kfree(&pdev->dev, mp->vreg_config);
  622. mp->num_vreg = 0;
  623. parse_vreg_err:
  624. if (mp->clk_config)
  625. devm_kfree(&pdev->dev, mp->clk_config);
  626. mp->num_clk = 0;
  627. end:
  628. return rc;
  629. }
  630. void sde_power_resource_deinit(struct platform_device *pdev,
  631. struct sde_power_handle *phandle)
  632. {
  633. struct dss_module_power *mp;
  634. struct sde_power_event *curr_event, *next_event;
  635. if (!phandle || !pdev) {
  636. pr_err("invalid input param\n");
  637. return;
  638. }
  639. mp = &phandle->mp;
  640. mutex_lock(&phandle->phandle_lock);
  641. list_for_each_entry_safe(curr_event, next_event,
  642. &phandle->event_list, list) {
  643. pr_err("event:%d, client:%s still registered\n",
  644. curr_event->event_type,
  645. curr_event->client_name);
  646. curr_event->active = false;
  647. list_del(&curr_event->list);
  648. }
  649. mutex_unlock(&phandle->phandle_lock);
  650. sde_power_bus_unregister(phandle);
  651. msm_dss_mmrm_deregister(&pdev->dev, mp);
  652. msm_dss_put_clk(mp->clk_config, mp->num_clk);
  653. msm_dss_get_vreg(&pdev->dev, mp->vreg_config, mp->num_vreg, 0);
  654. if (mp->clk_config)
  655. devm_kfree(&pdev->dev, mp->clk_config);
  656. if (mp->vreg_config)
  657. devm_kfree(&pdev->dev, mp->vreg_config);
  658. mp->num_vreg = 0;
  659. mp->num_clk = 0;
  660. if (phandle->rsc_client)
  661. sde_rsc_client_destroy(phandle->rsc_client);
  662. }
  663. static void sde_power_mmrm_reserve(struct sde_power_handle *phandle)
  664. {
  665. int i;
  666. struct dss_module_power *mp = &phandle->mp;
  667. u64 rate = phandle->mmrm_reserve.clk_rate;
  668. if (!phandle->mmrm_enable)
  669. return;
  670. for (i = 0; i < mp->num_clk; i++) {
  671. if (!strcmp(mp->clk_config[i].clk_name, phandle->mmrm_reserve.clk_name)) {
  672. if (mp->clk_config[i].max_rate)
  673. rate = min(rate, (u64)mp->clk_config[i].max_rate);
  674. mp->clk_config[i].rate = rate;
  675. mp->clk_config[i].mmrm.flags =
  676. MMRM_CLIENT_DATA_FLAG_RESERVE_ONLY;
  677. SDE_ATRACE_BEGIN("sde_clk_set_rate");
  678. msm_dss_single_clk_set_rate(&mp->clk_config[i]);
  679. SDE_ATRACE_END("sde_clk_set_rate");
  680. break;
  681. }
  682. }
  683. }
  684. int sde_power_scale_reg_bus(struct sde_power_handle *phandle,
  685. u32 usecase_ndx, bool skip_lock)
  686. {
  687. int rc = 0;
  688. if (!phandle->reg_bus_handle.reg_bus_hdl)
  689. return 0;
  690. if (!skip_lock)
  691. mutex_lock(&phandle->phandle_lock);
  692. pr_debug("%pS: requested:%d\n",
  693. __builtin_return_address(0), usecase_ndx);
  694. rc = sde_power_reg_bus_update(&phandle->reg_bus_handle,
  695. usecase_ndx);
  696. if (!skip_lock)
  697. mutex_unlock(&phandle->phandle_lock);
  698. return rc;
  699. }
  700. int sde_power_resource_enable(struct sde_power_handle *phandle, bool enable)
  701. {
  702. int rc = 0, i = 0;
  703. struct dss_module_power *mp;
  704. if (!phandle) {
  705. pr_err("invalid input argument\n");
  706. return -EINVAL;
  707. }
  708. mp = &phandle->mp;
  709. mutex_lock(&phandle->phandle_lock);
  710. pr_debug("enable:%d\n", enable);
  711. SDE_ATRACE_BEGIN("sde_power_resource_enable");
  712. /* RSC client init */
  713. sde_power_rsc_client_init(phandle);
  714. if (enable) {
  715. sde_power_event_trigger_locked(phandle,
  716. SDE_POWER_EVENT_PRE_ENABLE);
  717. for (i = SDE_POWER_HANDLE_DBUS_ID_MNOC; i < SDE_POWER_HANDLE_DBUS_ID_MAX; i++) {
  718. if (phandle->data_bus_handle[i].data_paths_cnt > 0) {
  719. rc = _sde_power_data_bus_set_quota(
  720. &phandle->data_bus_handle[i],
  721. SDE_POWER_HANDLE_ENABLE_BUS_AB_QUOTA,
  722. phandle->ib_quota[i]);
  723. if (rc) {
  724. pr_err("failed to set data bus vote id=%d rc=%d\n",
  725. i, rc);
  726. goto vreg_err;
  727. }
  728. }
  729. }
  730. rc = msm_dss_enable_vreg(mp->vreg_config, mp->num_vreg,
  731. enable);
  732. if (rc) {
  733. pr_err("failed to enable vregs rc=%d\n", rc);
  734. goto vreg_err;
  735. }
  736. rc = sde_power_scale_reg_bus(phandle, VOTE_INDEX_LOW, true);
  737. if (rc) {
  738. pr_err("failed to set reg bus vote rc=%d\n", rc);
  739. goto reg_bus_hdl_err;
  740. }
  741. SDE_EVT32_VERBOSE(enable, SDE_EVTLOG_FUNC_CASE1);
  742. rc = sde_power_rsc_update(phandle, true);
  743. if (rc) {
  744. pr_err("failed to update rsc\n");
  745. goto rsc_err;
  746. }
  747. rc = msm_dss_enable_clk(mp->clk_config, mp->num_clk, enable);
  748. if (rc) {
  749. pr_err("clock enable failed rc:%d\n", rc);
  750. goto clk_err;
  751. }
  752. sde_power_event_trigger_locked(phandle,
  753. SDE_POWER_EVENT_POST_ENABLE);
  754. } else {
  755. sde_power_event_trigger_locked(phandle,
  756. SDE_POWER_EVENT_PRE_DISABLE);
  757. SDE_EVT32_VERBOSE(enable, SDE_EVTLOG_FUNC_CASE2);
  758. sde_power_rsc_update(phandle, false);
  759. sde_power_mmrm_reserve(phandle);
  760. msm_dss_enable_clk(mp->clk_config, mp->num_clk, enable);
  761. sde_power_scale_reg_bus(phandle, VOTE_INDEX_DISABLE, true);
  762. msm_dss_enable_vreg(mp->vreg_config, mp->num_vreg, enable);
  763. for (i = SDE_POWER_HANDLE_DBUS_ID_MAX - 1; i >= 0; i--)
  764. if (phandle->data_bus_handle[i].data_paths_cnt > 0)
  765. _sde_power_data_bus_set_quota(
  766. &phandle->data_bus_handle[i],
  767. SDE_POWER_HANDLE_DISABLE_BUS_AB_QUOTA,
  768. SDE_POWER_HANDLE_DISABLE_BUS_IB_QUOTA);
  769. sde_power_event_trigger_locked(phandle,
  770. SDE_POWER_EVENT_POST_DISABLE);
  771. }
  772. SDE_EVT32_VERBOSE(enable, SDE_EVTLOG_FUNC_EXIT);
  773. SDE_ATRACE_END("sde_power_resource_enable");
  774. mutex_unlock(&phandle->phandle_lock);
  775. return rc;
  776. clk_err:
  777. sde_power_rsc_update(phandle, false);
  778. rsc_err:
  779. sde_power_scale_reg_bus(phandle, VOTE_INDEX_DISABLE, true);
  780. reg_bus_hdl_err:
  781. msm_dss_enable_vreg(mp->vreg_config, mp->num_vreg, 0);
  782. vreg_err:
  783. for (i-- ; i >= 0 && phandle->data_bus_handle[i].data_paths_cnt > 0; i--)
  784. _sde_power_data_bus_set_quota(
  785. &phandle->data_bus_handle[i],
  786. SDE_POWER_HANDLE_DISABLE_BUS_AB_QUOTA,
  787. SDE_POWER_HANDLE_DISABLE_BUS_IB_QUOTA);
  788. SDE_ATRACE_END("sde_power_resource_enable");
  789. mutex_unlock(&phandle->phandle_lock);
  790. return rc;
  791. }
  792. int sde_power_clk_reserve_rate(struct sde_power_handle *phandle, char *clock_name, u64 rate)
  793. {
  794. if (!phandle) {
  795. pr_err("invalid input power handle\n");
  796. return -EINVAL;
  797. } else if (!phandle->mmrm_enable) {
  798. pr_debug("mmrm disabled, return early\n");
  799. return 0;
  800. }
  801. mutex_lock(&phandle->phandle_lock);
  802. phandle->mmrm_reserve.clk_rate = rate;
  803. strlcpy(phandle->mmrm_reserve.clk_name, clock_name,
  804. sizeof(phandle->mmrm_reserve.clk_name));
  805. mutex_unlock(&phandle->phandle_lock);
  806. return 0;
  807. }
  808. int sde_power_clk_set_rate(struct sde_power_handle *phandle, char *clock_name,
  809. u64 rate, u32 flags)
  810. {
  811. int i, rc = -EINVAL;
  812. struct dss_module_power *mp;
  813. if (!phandle) {
  814. pr_err("invalid input power handle\n");
  815. return -EINVAL;
  816. }
  817. /*
  818. * Return early if mmrm is disabled and the flags to reserve the mmrm
  819. * mmrm clock are set.
  820. */
  821. if (flags && !phandle->mmrm_enable) {
  822. pr_debug("mmrm disabled, return early for reserve flags\n");
  823. return 0;
  824. }
  825. mutex_lock(&phandle->phandle_lock);
  826. if (phandle->last_event_handled & SDE_POWER_EVENT_POST_DISABLE &&
  827. !flags) {
  828. pr_debug("invalid power state %u\n",
  829. phandle->last_event_handled);
  830. SDE_EVT32(phandle->last_event_handled, SDE_EVTLOG_ERROR);
  831. mutex_unlock(&phandle->phandle_lock);
  832. return -EINVAL;
  833. }
  834. mp = &phandle->mp;
  835. for (i = 0; i < mp->num_clk; i++) {
  836. if (!strcmp(mp->clk_config[i].clk_name, clock_name)) {
  837. if (mp->clk_config[i].max_rate &&
  838. (rate > mp->clk_config[i].max_rate))
  839. rate = mp->clk_config[i].max_rate;
  840. mp->clk_config[i].rate = rate;
  841. mp->clk_config[i].mmrm.flags = flags;
  842. pr_debug("set rate clk:%s rate:%lu flags:0x%x\n",
  843. clock_name, rate, flags);
  844. SDE_ATRACE_BEGIN("sde_clk_set_rate");
  845. rc = msm_dss_single_clk_set_rate(&mp->clk_config[i]);
  846. SDE_ATRACE_END("sde_clk_set_rate");
  847. break;
  848. }
  849. }
  850. mutex_unlock(&phandle->phandle_lock);
  851. return rc;
  852. }
  853. u64 sde_power_clk_get_rate(struct sde_power_handle *phandle, char *clock_name)
  854. {
  855. int i;
  856. struct dss_module_power *mp;
  857. u64 rate = -EINVAL;
  858. if (!phandle) {
  859. pr_err("invalid input power handle\n");
  860. return -EINVAL;
  861. }
  862. mp = &phandle->mp;
  863. for (i = 0; i < mp->num_clk; i++) {
  864. if (!strcmp(mp->clk_config[i].clk_name, clock_name)) {
  865. rate = clk_get_rate(mp->clk_config[i].clk);
  866. break;
  867. }
  868. }
  869. return rate;
  870. }
  871. u64 sde_power_clk_get_max_rate(struct sde_power_handle *phandle,
  872. char *clock_name)
  873. {
  874. int i;
  875. struct dss_module_power *mp;
  876. u64 rate = 0;
  877. if (!phandle) {
  878. pr_err("invalid input power handle\n");
  879. return 0;
  880. }
  881. mp = &phandle->mp;
  882. for (i = 0; i < mp->num_clk; i++) {
  883. if (!strcmp(mp->clk_config[i].clk_name, clock_name)) {
  884. rate = mp->clk_config[i].max_rate;
  885. break;
  886. }
  887. }
  888. return rate;
  889. }
  890. struct clk *sde_power_clk_get_clk(struct sde_power_handle *phandle,
  891. char *clock_name)
  892. {
  893. int i;
  894. struct dss_module_power *mp;
  895. struct clk *clk = NULL;
  896. if (!phandle) {
  897. pr_err("invalid input power handle\n");
  898. return 0;
  899. }
  900. mp = &phandle->mp;
  901. for (i = 0; i < mp->num_clk; i++) {
  902. if (!strcmp(mp->clk_config[i].clk_name, clock_name)) {
  903. clk = mp->clk_config[i].clk;
  904. break;
  905. }
  906. }
  907. return clk;
  908. }
  909. struct sde_power_event *sde_power_handle_register_event(
  910. struct sde_power_handle *phandle,
  911. u32 event_type, void (*cb_fnc)(u32 event_type, void *usr),
  912. void *usr, char *client_name)
  913. {
  914. struct sde_power_event *event;
  915. if (!phandle) {
  916. pr_err("invalid power handle\n");
  917. return ERR_PTR(-EINVAL);
  918. } else if (!cb_fnc || !event_type) {
  919. pr_err("no callback fnc or event type\n");
  920. return ERR_PTR(-EINVAL);
  921. }
  922. event = kzalloc(sizeof(struct sde_power_event), GFP_KERNEL);
  923. if (!event)
  924. return ERR_PTR(-ENOMEM);
  925. event->event_type = event_type;
  926. event->cb_fnc = cb_fnc;
  927. event->usr = usr;
  928. strlcpy(event->client_name, client_name, MAX_CLIENT_NAME_LEN);
  929. event->active = true;
  930. mutex_lock(&phandle->phandle_lock);
  931. list_add(&event->list, &phandle->event_list);
  932. mutex_unlock(&phandle->phandle_lock);
  933. return event;
  934. }
  935. void sde_power_handle_unregister_event(
  936. struct sde_power_handle *phandle,
  937. struct sde_power_event *event)
  938. {
  939. if (!phandle || !event) {
  940. pr_err("invalid phandle or event\n");
  941. } else if (!event->active) {
  942. pr_err("power handle deinit already done\n");
  943. kfree(event);
  944. } else {
  945. mutex_lock(&phandle->phandle_lock);
  946. list_del_init(&event->list);
  947. mutex_unlock(&phandle->phandle_lock);
  948. kfree(event);
  949. }
  950. }