sde_power_handle.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  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. #if IS_ENABLED(CONFIG_DISPLAY_SAMSUNG)
  265. pdbus->in_ab_quota = in_ab_quota;
  266. pdbus->in_ib_quota = in_ib_quota;
  267. #endif
  268. SDE_ATRACE_BEGIN("msm_bus_scale_req");
  269. for (i = 0; i < paths; i++) {
  270. if (pdbus->data_bus_hdl[i]) {
  271. rc = icc_set_bw(pdbus->data_bus_hdl[i],
  272. Bps_to_icc(in_ab_quota),
  273. Bps_to_icc(in_ib_quota));
  274. if (rc)
  275. goto err;
  276. }
  277. }
  278. pdbus->curr_val.ab = in_ab_quota;
  279. pdbus->curr_val.ib = in_ib_quota;
  280. SDE_ATRACE_END("msm_bus_scale_req");
  281. return rc;
  282. err:
  283. for (; i >= 0; --i)
  284. if (pdbus->data_bus_hdl[i])
  285. icc_set_bw(pdbus->data_bus_hdl[i],
  286. Bps_to_icc(pdbus->curr_val.ab),
  287. Bps_to_icc(pdbus->curr_val.ib));
  288. SDE_ATRACE_END("msm_bus_scale_req");
  289. pr_err("failed to set data bus vote ab=%llu ib=%llu rc=%d\n",
  290. in_ab_quota, in_ib_quota, rc);
  291. return rc;
  292. }
  293. int sde_power_data_bus_set_quota(struct sde_power_handle *phandle,
  294. u32 bus_id, u64 ab_quota, u64 ib_quota)
  295. {
  296. int rc = 0;
  297. u32 paths;
  298. if (!phandle || bus_id >= SDE_POWER_HANDLE_DBUS_ID_MAX) {
  299. pr_err("invalid parameters\n");
  300. return -EINVAL;
  301. }
  302. paths = phandle->data_bus_handle[bus_id].data_paths_cnt;
  303. if (!paths)
  304. goto skip_vote;
  305. trace_sde_perf_update_bus(bus_id, ab_quota, ib_quota, paths);
  306. mutex_lock(&phandle->phandle_lock);
  307. rc = _sde_power_data_bus_set_quota(&phandle->data_bus_handle[bus_id],
  308. ab_quota, ib_quota);
  309. mutex_unlock(&phandle->phandle_lock);
  310. skip_vote:
  311. pr_debug("bus=%d, ab=%llu, ib=%llu, paths=%d\n", bus_id, ab_quota,
  312. ib_quota, paths);
  313. return rc;
  314. }
  315. /**
  316. * sde_power_icc_get - get the interconnect path for the given bus_name
  317. * @pdev - platform device
  318. * @bus_name - bus name for the corresponding interconnect
  319. * @path - the icc_path object we want to obtain for this @bus_name (output)
  320. * @count - if given, incremented only if the path was successfully retrieved
  321. **/
  322. static int sde_power_icc_get(struct platform_device *pdev,
  323. const char *bus_name, struct icc_path **path, u32 *count)
  324. {
  325. int rc = of_property_match_string(pdev->dev.of_node,
  326. "interconnect-names", bus_name);
  327. /* bus_names are optional for any given device node, skip if missing */
  328. if (rc < 0)
  329. goto end;
  330. *path = of_icc_get(&pdev->dev, bus_name);
  331. if (IS_ERR_OR_NULL(*path)) {
  332. rc = PTR_ERR(*path);
  333. pr_err("bus %s parsing failed, rc:%d\n", bus_name, rc);
  334. *path = NULL;
  335. return rc;
  336. }
  337. if (count)
  338. (*count)++;
  339. end:
  340. pr_debug("bus %s dt node %s(%d), icc_path is %s, count:%d\n",
  341. bus_name, rc < 0 ? "missing" : "found", rc,
  342. *path ? "valid" : "NULL", count ? *count : -1);
  343. return 0;
  344. }
  345. static int sde_power_reg_bus_parse(struct platform_device *pdev,
  346. struct sde_power_reg_bus_handle *reg_bus)
  347. {
  348. const char *bus_name = "qcom,sde-reg-bus";
  349. const u32 *vec_arr = NULL;
  350. int rc, len, i, vec_idx = 0;
  351. u32 paths = 0;
  352. rc = sde_power_icc_get(pdev, bus_name, &reg_bus->reg_bus_hdl, &paths);
  353. if (rc)
  354. return rc;
  355. if (!paths) {
  356. pr_debug("%s not defined for pdev %s\n", bus_name, pdev->name ?
  357. pdev->name : "<unknown>");
  358. return 0;
  359. }
  360. vec_arr = of_get_property(pdev->dev.of_node,
  361. "qcom,sde-reg-bus,vectors-KBps", &len);
  362. if (!vec_arr) {
  363. pr_err("%s scale table property not found\n", bus_name);
  364. return -EINVAL;
  365. }
  366. if (len / sizeof(*vec_arr) != VOTE_INDEX_MAX * 2) {
  367. pr_err("wrong size for %s vector table\n", bus_name);
  368. return -EINVAL;
  369. }
  370. for (i = 0; i < VOTE_INDEX_MAX; ++i) {
  371. reg_bus->scale_table[i].ab = (u64)KBPS2BPS(be32_to_cpu(
  372. vec_arr[vec_idx++]));
  373. reg_bus->scale_table[i].ib = (u64)KBPS2BPS(be32_to_cpu(
  374. vec_arr[vec_idx++]));
  375. }
  376. return rc;
  377. }
  378. static int sde_power_mnoc_bus_parse(struct platform_device *pdev,
  379. struct sde_power_data_bus_handle *pdbus, const char *name)
  380. {
  381. int i, rc = 0;
  382. char bus_name[32];
  383. for (i = 0; i < DATA_BUS_PATH_MAX; ++i) {
  384. snprintf(bus_name, sizeof(bus_name), "%s%d", name, i);
  385. rc = sde_power_icc_get(pdev, bus_name, &pdbus->data_bus_hdl[i],
  386. &pdbus->data_paths_cnt);
  387. if (rc)
  388. break;
  389. }
  390. /* at least one databus path is required */
  391. if (!pdbus->data_paths_cnt) {
  392. pr_info("mnoc interconnect path(s) not defined, rc: %d\n", rc);
  393. } else if (rc) {
  394. pr_info("ignoring error %d for non-primary data path\n", rc);
  395. rc = 0;
  396. }
  397. return rc;
  398. }
  399. static int sde_power_bus_parse(struct platform_device *pdev,
  400. struct sde_power_handle *phandle)
  401. {
  402. int i, j, ib_quota_count, rc = 0;
  403. bool active_only = false;
  404. struct sde_power_data_bus_handle *pdbus = phandle->data_bus_handle;
  405. u32 ib_quota[SDE_POWER_HANDLE_DBUS_ID_MAX];
  406. ib_quota_count = of_property_count_u32_elems(pdev->dev.of_node, "qcom,sde-ib-bw-vote");
  407. if (ib_quota_count > 0) {
  408. if (ib_quota_count != SDE_POWER_HANDLE_DBUS_ID_MAX) {
  409. pr_err("wrong size for qcom,sde-ib-bw-vote\n");
  410. return -EINVAL;
  411. }
  412. for (i = 0; i < SDE_POWER_HANDLE_DBUS_ID_MAX; ++i) {
  413. of_property_read_u32_index(pdev->dev.of_node,
  414. "qcom,sde-ib-bw-vote", i, &ib_quota[i]);
  415. phandle->ib_quota[i] = ib_quota[i]*1000;
  416. }
  417. }
  418. /* reg bus */
  419. rc = sde_power_reg_bus_parse(pdev, &phandle->reg_bus_handle);
  420. if (rc)
  421. return rc;
  422. /* data buses */
  423. if (of_find_property(pdev->dev.of_node,
  424. "qcom,msm-bus,active-only", NULL))
  425. active_only = true;
  426. for (i = SDE_POWER_HANDLE_DBUS_ID_MNOC;
  427. i < SDE_POWER_HANDLE_DBUS_ID_MAX; ++i) {
  428. if (i == SDE_POWER_HANDLE_DBUS_ID_MNOC)
  429. rc = sde_power_mnoc_bus_parse(pdev, &pdbus[i],
  430. data_bus_name[i]);
  431. else
  432. rc = sde_power_icc_get(pdev, data_bus_name[i],
  433. &pdbus[i].data_bus_hdl[0],
  434. &pdbus[i].data_paths_cnt);
  435. if (rc)
  436. break;
  437. if (active_only) {
  438. pdbus[i].bus_active_only = true;
  439. for (j = 0; j < pdbus[i].data_paths_cnt; ++j)
  440. icc_set_tag(pdbus[i].data_bus_hdl[j],
  441. QCOM_ICC_TAG_ACTIVE_ONLY);
  442. }
  443. pr_debug("found %d paths for %s\n", pdbus[i].data_paths_cnt,
  444. data_bus_name[i]);
  445. }
  446. return rc;
  447. }
  448. static void sde_power_bus_unregister(struct sde_power_handle *phandle)
  449. {
  450. int i, j;
  451. struct sde_power_reg_bus_handle *reg_bus = &phandle->reg_bus_handle;
  452. struct sde_power_data_bus_handle *pdbus = phandle->data_bus_handle;
  453. icc_put(reg_bus->reg_bus_hdl);
  454. reg_bus->reg_bus_hdl = NULL;
  455. for (i = SDE_POWER_HANDLE_DBUS_ID_MAX - 1;
  456. i >= SDE_POWER_HANDLE_DBUS_ID_MNOC; i--) {
  457. for (j = 0; j < pdbus[i].data_paths_cnt; j++) {
  458. if (pdbus[i].data_bus_hdl[j]) {
  459. icc_put(pdbus[i].data_bus_hdl[j]);
  460. pdbus[i].data_bus_hdl[j] = NULL;
  461. }
  462. }
  463. }
  464. }
  465. static int sde_power_reg_bus_update(struct sde_power_reg_bus_handle *reg_bus,
  466. u32 usecase_ndx)
  467. {
  468. int rc = 0;
  469. u64 ab_quota, ib_quota;
  470. ab_quota = reg_bus->scale_table[usecase_ndx].ab;
  471. ib_quota = reg_bus->scale_table[usecase_ndx].ib;
  472. if (reg_bus->reg_bus_hdl) {
  473. SDE_ATRACE_BEGIN("msm_bus_scale_req");
  474. rc = icc_set_bw(reg_bus->reg_bus_hdl, Bps_to_icc(ab_quota),
  475. Bps_to_icc(ib_quota));
  476. SDE_ATRACE_END("msm_bus_scale_req");
  477. }
  478. if (rc)
  479. pr_err("failed to set reg bus vote to index %d, rc=%d\n",
  480. usecase_ndx, rc);
  481. else {
  482. reg_bus->curr_idx = usecase_ndx;
  483. pr_debug("reg-bus vote set to index=%d, ab=%llu, ib=%llu\n",
  484. usecase_ndx, ab_quota, ib_quota);
  485. }
  486. return rc;
  487. }
  488. int sde_power_mmrm_set_clk_limit(struct dss_clk *clk,
  489. struct sde_power_handle *phandle, unsigned long requested_clk)
  490. {
  491. int ret;
  492. clk->mmrm.mmrm_requested_clk = requested_clk;
  493. SDE_EVT32_VERBOSE(SDE_EVTLOG_FUNC_ENTRY,
  494. clk->mmrm.mmrm_requested_clk);
  495. ret = sde_power_event_trigger_locked(phandle,
  496. SDE_POWER_EVENT_MMRM_CALLBACK);
  497. if (ret) {
  498. /* no crtc's present, we cannot process the cb */
  499. pr_err("error cannot process mmrm cb\n");
  500. goto exit;
  501. }
  502. SDE_EVT32_VERBOSE(SDE_EVTLOG_FUNC_CASE1,
  503. clk->mmrm.mmrm_requested_clk);
  504. /* wait for the request to reduce the clk */
  505. ret = wait_event_timeout(clk->mmrm.mmrm_cb_wq,
  506. clk->mmrm.mmrm_requested_clk == 0,
  507. SDE_MMRM_CB_TIMEOUT_JIFFIES);
  508. if (!ret) {
  509. /* requested clk was not reduced, fail cb */
  510. ret = -EPERM;
  511. /* Clear the request */
  512. clk->mmrm.mmrm_requested_clk = 0;
  513. pr_err("error cannot process mmrm cb clk request\n");
  514. } else {
  515. ret = 0; // Succeed, clk was reduced
  516. }
  517. exit:
  518. SDE_EVT32(SDE_EVTLOG_FUNC_EXIT, ret);
  519. return ret;
  520. }
  521. int sde_power_mmrm_callback(
  522. struct mmrm_client_notifier_data *notifier_data)
  523. {
  524. struct dss_clk_mmrm_cb *mmrm_cb_data =
  525. (struct dss_clk_mmrm_cb *)notifier_data->pvt_data;
  526. struct sde_power_handle *phandle =
  527. (struct sde_power_handle *)mmrm_cb_data->phandle;
  528. struct dss_clk *clk = mmrm_cb_data->clk;
  529. int ret = -EPERM;
  530. if (notifier_data->cb_type == MMRM_CLIENT_RESOURCE_VALUE_CHANGE) {
  531. unsigned long requested_clk =
  532. notifier_data->cb_data.val_chng.new_val;
  533. ret = sde_power_mmrm_set_clk_limit(clk, phandle, requested_clk);
  534. if (ret)
  535. pr_err("mmrm callback error reducing clk:%lu ret:%d\n",
  536. requested_clk, ret);
  537. }
  538. return ret;
  539. }
  540. u64 sde_power_mmrm_get_requested_clk(struct sde_power_handle *phandle,
  541. char *clock_name)
  542. {
  543. struct dss_module_power *mp;
  544. u64 rate = -EINVAL;
  545. int i;
  546. if (!phandle) {
  547. pr_err("invalid input power handle\n");
  548. return -EINVAL;
  549. }
  550. mp = &phandle->mp;
  551. for (i = 0; i < mp->num_clk; i++) {
  552. if (!strcmp(mp->clk_config[i].clk_name, clock_name)) {
  553. rate = mp->clk_config[i].mmrm.mmrm_requested_clk;
  554. break;
  555. }
  556. }
  557. return rate;
  558. }
  559. int sde_power_resource_init(struct platform_device *pdev,
  560. struct sde_power_handle *phandle)
  561. {
  562. int rc = 0;
  563. struct dss_module_power *mp;
  564. if (!phandle || !pdev) {
  565. pr_err("invalid input param\n");
  566. rc = -EINVAL;
  567. goto end;
  568. }
  569. mp = &phandle->mp;
  570. phandle->dev = &pdev->dev;
  571. /* event init must happen before mmrm register */
  572. INIT_LIST_HEAD(&phandle->event_list);
  573. rc = sde_power_parse_dt_clock(pdev, mp);
  574. if (rc) {
  575. pr_err("device clock parsing failed\n");
  576. goto end;
  577. }
  578. rc = sde_power_parse_dt_supply(pdev, mp);
  579. if (rc) {
  580. pr_err("device vreg supply parsing failed\n");
  581. goto parse_vreg_err;
  582. }
  583. rc = msm_dss_get_vreg(&pdev->dev,
  584. mp->vreg_config, mp->num_vreg, 1);
  585. if (rc) {
  586. pr_err("get config failed rc=%d\n", rc);
  587. goto vreg_err;
  588. }
  589. rc = msm_dss_get_clk(&pdev->dev, mp->clk_config, mp->num_clk);
  590. if (rc) {
  591. pr_err("clock get failed rc=%d\n", rc);
  592. goto clkget_err;
  593. }
  594. rc = msm_dss_mmrm_register(&pdev->dev, mp,
  595. sde_power_mmrm_callback, (void *)phandle,
  596. &phandle->mmrm_enable);
  597. if (rc) {
  598. pr_err("mmrm register failed rc=%d\n", rc);
  599. goto clkmmrm_err;
  600. }
  601. rc = msm_dss_clk_set_rate(mp->clk_config, mp->num_clk);
  602. if (rc) {
  603. pr_err("clock set rate failed rc=%d\n", rc);
  604. goto clkset_err;
  605. }
  606. rc = sde_power_bus_parse(pdev, phandle);
  607. if (rc) {
  608. pr_err("bus parse failed rc=%d\n", rc);
  609. goto bus_err;
  610. }
  611. phandle->rsc_client = NULL;
  612. phandle->rsc_client_init = false;
  613. mutex_init(&phandle->phandle_lock);
  614. return rc;
  615. bus_err:
  616. sde_power_bus_unregister(phandle);
  617. clkset_err:
  618. msm_dss_mmrm_deregister(&pdev->dev, mp);
  619. clkmmrm_err:
  620. msm_dss_put_clk(mp->clk_config, mp->num_clk);
  621. clkget_err:
  622. msm_dss_get_vreg(&pdev->dev, mp->vreg_config, mp->num_vreg, 0);
  623. vreg_err:
  624. if (mp->vreg_config)
  625. devm_kfree(&pdev->dev, mp->vreg_config);
  626. mp->num_vreg = 0;
  627. parse_vreg_err:
  628. if (mp->clk_config)
  629. devm_kfree(&pdev->dev, mp->clk_config);
  630. mp->num_clk = 0;
  631. end:
  632. return rc;
  633. }
  634. void sde_power_resource_deinit(struct platform_device *pdev,
  635. struct sde_power_handle *phandle)
  636. {
  637. struct dss_module_power *mp;
  638. struct sde_power_event *curr_event, *next_event;
  639. if (!phandle || !pdev) {
  640. pr_err("invalid input param\n");
  641. return;
  642. }
  643. mp = &phandle->mp;
  644. mutex_lock(&phandle->phandle_lock);
  645. list_for_each_entry_safe(curr_event, next_event,
  646. &phandle->event_list, list) {
  647. pr_err("event:%d, client:%s still registered\n",
  648. curr_event->event_type,
  649. curr_event->client_name);
  650. curr_event->active = false;
  651. list_del(&curr_event->list);
  652. }
  653. mutex_unlock(&phandle->phandle_lock);
  654. sde_power_bus_unregister(phandle);
  655. msm_dss_mmrm_deregister(&pdev->dev, mp);
  656. msm_dss_put_clk(mp->clk_config, mp->num_clk);
  657. msm_dss_get_vreg(&pdev->dev, mp->vreg_config, mp->num_vreg, 0);
  658. if (mp->clk_config)
  659. devm_kfree(&pdev->dev, mp->clk_config);
  660. if (mp->vreg_config)
  661. devm_kfree(&pdev->dev, mp->vreg_config);
  662. mp->num_vreg = 0;
  663. mp->num_clk = 0;
  664. if (phandle->rsc_client)
  665. sde_rsc_client_destroy(phandle->rsc_client);
  666. }
  667. static void sde_power_mmrm_reserve(struct sde_power_handle *phandle)
  668. {
  669. int i;
  670. struct dss_module_power *mp = &phandle->mp;
  671. u64 rate = phandle->mmrm_reserve.clk_rate;
  672. if (!phandle->mmrm_enable)
  673. return;
  674. for (i = 0; i < mp->num_clk; i++) {
  675. if (!strcmp(mp->clk_config[i].clk_name, phandle->mmrm_reserve.clk_name)) {
  676. if (mp->clk_config[i].max_rate)
  677. rate = min(rate, (u64)mp->clk_config[i].max_rate);
  678. mp->clk_config[i].rate = rate;
  679. mp->clk_config[i].mmrm.flags =
  680. MMRM_CLIENT_DATA_FLAG_RESERVE_ONLY;
  681. SDE_ATRACE_BEGIN("sde_clk_set_rate");
  682. msm_dss_single_clk_set_rate(&mp->clk_config[i]);
  683. SDE_ATRACE_END("sde_clk_set_rate");
  684. break;
  685. }
  686. }
  687. }
  688. int sde_power_scale_reg_bus(struct sde_power_handle *phandle,
  689. u32 usecase_ndx, bool skip_lock)
  690. {
  691. int rc = 0;
  692. if (!phandle->reg_bus_handle.reg_bus_hdl)
  693. return 0;
  694. if (!skip_lock)
  695. mutex_lock(&phandle->phandle_lock);
  696. pr_debug("%pS: requested:%d\n",
  697. __builtin_return_address(0), usecase_ndx);
  698. rc = sde_power_reg_bus_update(&phandle->reg_bus_handle,
  699. usecase_ndx);
  700. if (!skip_lock)
  701. mutex_unlock(&phandle->phandle_lock);
  702. return rc;
  703. }
  704. int sde_power_resource_enable(struct sde_power_handle *phandle, bool enable)
  705. {
  706. int rc = 0, i = 0;
  707. struct dss_module_power *mp;
  708. if (!phandle) {
  709. pr_err("invalid input argument\n");
  710. return -EINVAL;
  711. }
  712. mp = &phandle->mp;
  713. mutex_lock(&phandle->phandle_lock);
  714. pr_debug("enable:%d\n", enable);
  715. SDE_ATRACE_BEGIN("sde_power_resource_enable");
  716. /* RSC client init */
  717. sde_power_rsc_client_init(phandle);
  718. if (enable) {
  719. sde_power_event_trigger_locked(phandle,
  720. SDE_POWER_EVENT_PRE_ENABLE);
  721. for (i = SDE_POWER_HANDLE_DBUS_ID_MNOC; i < SDE_POWER_HANDLE_DBUS_ID_MAX; i++) {
  722. if (phandle->data_bus_handle[i].data_paths_cnt > 0) {
  723. rc = _sde_power_data_bus_set_quota(
  724. &phandle->data_bus_handle[i],
  725. SDE_POWER_HANDLE_ENABLE_BUS_AB_QUOTA,
  726. phandle->ib_quota[i]);
  727. if (rc) {
  728. pr_err("failed to set data bus vote id=%d rc=%d\n",
  729. i, rc);
  730. goto vreg_err;
  731. }
  732. }
  733. }
  734. rc = msm_dss_enable_vreg(mp->vreg_config, mp->num_vreg,
  735. enable);
  736. if (rc) {
  737. pr_err("failed to enable vregs rc=%d\n", rc);
  738. goto vreg_err;
  739. }
  740. rc = sde_power_scale_reg_bus(phandle, VOTE_INDEX_LOW, true);
  741. if (rc) {
  742. pr_err("failed to set reg bus vote rc=%d\n", rc);
  743. goto reg_bus_hdl_err;
  744. }
  745. SDE_EVT32_VERBOSE(enable, SDE_EVTLOG_FUNC_CASE1);
  746. rc = sde_power_rsc_update(phandle, true);
  747. if (rc) {
  748. pr_err("failed to update rsc\n");
  749. goto rsc_err;
  750. }
  751. rc = msm_dss_enable_clk(mp->clk_config, mp->num_clk, enable);
  752. if (rc) {
  753. pr_err("clock enable failed rc:%d\n", rc);
  754. goto clk_err;
  755. }
  756. sde_power_event_trigger_locked(phandle,
  757. SDE_POWER_EVENT_POST_ENABLE);
  758. } else {
  759. sde_power_event_trigger_locked(phandle,
  760. SDE_POWER_EVENT_PRE_DISABLE);
  761. SDE_EVT32_VERBOSE(enable, SDE_EVTLOG_FUNC_CASE2);
  762. sde_power_rsc_update(phandle, false);
  763. sde_power_mmrm_reserve(phandle);
  764. msm_dss_enable_clk(mp->clk_config, mp->num_clk, enable);
  765. sde_power_scale_reg_bus(phandle, VOTE_INDEX_DISABLE, true);
  766. msm_dss_enable_vreg(mp->vreg_config, mp->num_vreg, enable);
  767. for (i = SDE_POWER_HANDLE_DBUS_ID_MAX - 1; i >= 0; i--)
  768. if (phandle->data_bus_handle[i].data_paths_cnt > 0)
  769. _sde_power_data_bus_set_quota(
  770. &phandle->data_bus_handle[i],
  771. SDE_POWER_HANDLE_DISABLE_BUS_AB_QUOTA,
  772. SDE_POWER_HANDLE_DISABLE_BUS_IB_QUOTA);
  773. sde_power_event_trigger_locked(phandle,
  774. SDE_POWER_EVENT_POST_DISABLE);
  775. }
  776. SDE_EVT32_VERBOSE(enable, SDE_EVTLOG_FUNC_EXIT);
  777. SDE_ATRACE_END("sde_power_resource_enable");
  778. mutex_unlock(&phandle->phandle_lock);
  779. return rc;
  780. clk_err:
  781. sde_power_rsc_update(phandle, false);
  782. rsc_err:
  783. sde_power_scale_reg_bus(phandle, VOTE_INDEX_DISABLE, true);
  784. reg_bus_hdl_err:
  785. msm_dss_enable_vreg(mp->vreg_config, mp->num_vreg, 0);
  786. vreg_err:
  787. for (i-- ; i >= 0 && phandle->data_bus_handle[i].data_paths_cnt > 0; i--)
  788. _sde_power_data_bus_set_quota(
  789. &phandle->data_bus_handle[i],
  790. SDE_POWER_HANDLE_DISABLE_BUS_AB_QUOTA,
  791. SDE_POWER_HANDLE_DISABLE_BUS_IB_QUOTA);
  792. SDE_ATRACE_END("sde_power_resource_enable");
  793. mutex_unlock(&phandle->phandle_lock);
  794. return rc;
  795. }
  796. int sde_power_clk_reserve_rate(struct sde_power_handle *phandle, char *clock_name, u64 rate)
  797. {
  798. if (!phandle) {
  799. pr_err("invalid input power handle\n");
  800. return -EINVAL;
  801. } else if (!phandle->mmrm_enable) {
  802. pr_debug("mmrm disabled, return early\n");
  803. return 0;
  804. }
  805. mutex_lock(&phandle->phandle_lock);
  806. phandle->mmrm_reserve.clk_rate = rate;
  807. strlcpy(phandle->mmrm_reserve.clk_name, clock_name,
  808. sizeof(phandle->mmrm_reserve.clk_name));
  809. mutex_unlock(&phandle->phandle_lock);
  810. return 0;
  811. }
  812. int sde_power_clk_set_rate(struct sde_power_handle *phandle, char *clock_name,
  813. u64 rate, u32 flags)
  814. {
  815. int i, rc = -EINVAL;
  816. struct dss_module_power *mp;
  817. if (!phandle) {
  818. pr_err("invalid input power handle\n");
  819. return -EINVAL;
  820. }
  821. /*
  822. * Return early if mmrm is disabled and the flags to reserve the mmrm
  823. * mmrm clock are set.
  824. */
  825. if (flags && !phandle->mmrm_enable) {
  826. pr_debug("mmrm disabled, return early for reserve flags\n");
  827. return 0;
  828. }
  829. mutex_lock(&phandle->phandle_lock);
  830. if (phandle->last_event_handled & SDE_POWER_EVENT_POST_DISABLE &&
  831. !flags) {
  832. pr_debug("invalid power state %u\n",
  833. phandle->last_event_handled);
  834. SDE_EVT32(phandle->last_event_handled, SDE_EVTLOG_ERROR);
  835. mutex_unlock(&phandle->phandle_lock);
  836. return -EINVAL;
  837. }
  838. mp = &phandle->mp;
  839. for (i = 0; i < mp->num_clk; i++) {
  840. if (!strcmp(mp->clk_config[i].clk_name, clock_name)) {
  841. if (mp->clk_config[i].max_rate &&
  842. (rate > mp->clk_config[i].max_rate))
  843. rate = mp->clk_config[i].max_rate;
  844. mp->clk_config[i].rate = rate;
  845. mp->clk_config[i].mmrm.flags = flags;
  846. pr_debug("set rate clk:%s rate:%lu flags:0x%x\n",
  847. clock_name, rate, flags);
  848. SDE_ATRACE_BEGIN("sde_clk_set_rate");
  849. rc = msm_dss_single_clk_set_rate(&mp->clk_config[i]);
  850. SDE_ATRACE_END("sde_clk_set_rate");
  851. break;
  852. }
  853. }
  854. mutex_unlock(&phandle->phandle_lock);
  855. return rc;
  856. }
  857. u64 sde_power_clk_get_rate(struct sde_power_handle *phandle, char *clock_name)
  858. {
  859. int i;
  860. struct dss_module_power *mp;
  861. u64 rate = -EINVAL;
  862. if (!phandle) {
  863. pr_err("invalid input power handle\n");
  864. return -EINVAL;
  865. }
  866. mp = &phandle->mp;
  867. for (i = 0; i < mp->num_clk; i++) {
  868. if (!strcmp(mp->clk_config[i].clk_name, clock_name)) {
  869. rate = clk_get_rate(mp->clk_config[i].clk);
  870. break;
  871. }
  872. }
  873. return rate;
  874. }
  875. u64 sde_power_clk_get_max_rate(struct sde_power_handle *phandle,
  876. char *clock_name)
  877. {
  878. int i;
  879. struct dss_module_power *mp;
  880. u64 rate = 0;
  881. if (!phandle) {
  882. pr_err("invalid input power handle\n");
  883. return 0;
  884. }
  885. mp = &phandle->mp;
  886. for (i = 0; i < mp->num_clk; i++) {
  887. if (!strcmp(mp->clk_config[i].clk_name, clock_name)) {
  888. rate = mp->clk_config[i].max_rate;
  889. break;
  890. }
  891. }
  892. return rate;
  893. }
  894. struct clk *sde_power_clk_get_clk(struct sde_power_handle *phandle,
  895. char *clock_name)
  896. {
  897. int i;
  898. struct dss_module_power *mp;
  899. struct clk *clk = NULL;
  900. if (!phandle) {
  901. pr_err("invalid input power handle\n");
  902. return 0;
  903. }
  904. mp = &phandle->mp;
  905. for (i = 0; i < mp->num_clk; i++) {
  906. if (!strcmp(mp->clk_config[i].clk_name, clock_name)) {
  907. clk = mp->clk_config[i].clk;
  908. break;
  909. }
  910. }
  911. return clk;
  912. }
  913. struct sde_power_event *sde_power_handle_register_event(
  914. struct sde_power_handle *phandle,
  915. u32 event_type, void (*cb_fnc)(u32 event_type, void *usr),
  916. void *usr, char *client_name)
  917. {
  918. struct sde_power_event *event;
  919. if (!phandle) {
  920. pr_err("invalid power handle\n");
  921. return ERR_PTR(-EINVAL);
  922. } else if (!cb_fnc || !event_type) {
  923. pr_err("no callback fnc or event type\n");
  924. return ERR_PTR(-EINVAL);
  925. }
  926. event = kzalloc(sizeof(struct sde_power_event), GFP_KERNEL);
  927. if (!event)
  928. return ERR_PTR(-ENOMEM);
  929. event->event_type = event_type;
  930. event->cb_fnc = cb_fnc;
  931. event->usr = usr;
  932. strlcpy(event->client_name, client_name, MAX_CLIENT_NAME_LEN);
  933. event->active = true;
  934. mutex_lock(&phandle->phandle_lock);
  935. list_add(&event->list, &phandle->event_list);
  936. mutex_unlock(&phandle->phandle_lock);
  937. return event;
  938. }
  939. void sde_power_handle_unregister_event(
  940. struct sde_power_handle *phandle,
  941. struct sde_power_event *event)
  942. {
  943. if (!phandle || !event) {
  944. pr_err("invalid phandle or event\n");
  945. } else if (!event->active) {
  946. pr_err("power handle deinit already done\n");
  947. kfree(event);
  948. } else {
  949. mutex_lock(&phandle->phandle_lock);
  950. list_del_init(&event->list);
  951. mutex_unlock(&phandle->phandle_lock);
  952. kfree(event);
  953. }
  954. }