sde_power_handle.c 28 KB

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