resources.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
  4. */
  5. /* Copyright (c) 2022-2023. Qualcomm Innovation Center, Inc. All rights reserved. */
  6. #include <linux/sort.h>
  7. #include <linux/clk.h>
  8. #include <linux/reset.h>
  9. #include <linux/interconnect.h>
  10. #include <linux/soc/qcom/llcc-qcom.h>
  11. #ifdef CONFIG_MSM_MMRM
  12. #include <linux/soc/qcom/msm_mmrm.h>
  13. #endif
  14. #include "msm_vidc_core.h"
  15. #include "msm_vidc_power.h"
  16. #include "msm_vidc_debug.h"
  17. #include "msm_vidc_driver.h"
  18. #include "msm_vidc_platform.h"
  19. #include "venus_hfi.h"
  20. /* Less than 50MBps is treated as trivial BW change */
  21. #define TRIVIAL_BW_THRESHOLD 50000
  22. #define TRIVIAL_BW_CHANGE(a, b) \
  23. ((a) > (b) ? (a) - (b) < TRIVIAL_BW_THRESHOLD : \
  24. (b) - (a) < TRIVIAL_BW_THRESHOLD)
  25. enum reset_state {
  26. INIT = 1,
  27. ASSERT,
  28. DEASSERT,
  29. };
  30. static void __fatal_error(bool fatal)
  31. {
  32. WARN_ON(fatal);
  33. }
  34. static void devm_llcc_release(struct device *dev, void *res)
  35. {
  36. d_vpr_h("%s()\n", __func__);
  37. llcc_slice_putd(*(struct llcc_slice_desc **)res);
  38. }
  39. static struct llcc_slice_desc *devm_llcc_get(struct device *dev, u32 id)
  40. {
  41. struct llcc_slice_desc **ptr, *llcc;
  42. ptr = devres_alloc(devm_llcc_release, sizeof(*ptr), GFP_KERNEL);
  43. if (!ptr)
  44. return ERR_PTR(-ENOMEM);
  45. llcc = llcc_slice_getd(id);
  46. if (!IS_ERR(llcc)) {
  47. *ptr = llcc;
  48. devres_add(dev, ptr);
  49. } else {
  50. devres_free(ptr);
  51. }
  52. return llcc;
  53. }
  54. #ifdef CONFIG_MSM_MMRM
  55. static void devm_mmrm_release(struct device *dev, void *res)
  56. {
  57. d_vpr_h("%s()\n", __func__);
  58. mmrm_client_deregister(*(struct mmrm_client **)res);
  59. }
  60. static struct mmrm_client *devm_mmrm_get(struct device *dev, struct mmrm_client_desc *desc)
  61. {
  62. struct mmrm_client **ptr, *mmrm;
  63. ptr = devres_alloc(devm_mmrm_release, sizeof(*ptr), GFP_KERNEL);
  64. if (!ptr)
  65. return ERR_PTR(-ENOMEM);
  66. mmrm = mmrm_client_register(desc);
  67. if (!IS_ERR(mmrm)) {
  68. *ptr = mmrm;
  69. devres_add(dev, ptr);
  70. } else {
  71. devres_free(ptr);
  72. }
  73. return mmrm;
  74. }
  75. #endif
  76. /* A comparator to compare loads (needed later on) */
  77. static inline int cmp(const void *a, const void *b)
  78. {
  79. /* want to sort in reverse so flip the comparison */
  80. return ((struct freq_table *)b)->freq -
  81. ((struct freq_table *)a)->freq;
  82. }
  83. static int __init_register_base(struct msm_vidc_core *core)
  84. {
  85. struct msm_vidc_resource *res;
  86. if (!core || !core->pdev || !core->resource) {
  87. d_vpr_e("%s: invalid params\n", __func__);
  88. return -EINVAL;
  89. }
  90. res = core->resource;
  91. res->register_base_addr = devm_platform_ioremap_resource(core->pdev, 0);
  92. if (IS_ERR(res->register_base_addr)) {
  93. d_vpr_e("%s: map reg addr failed %ld\n",
  94. __func__, PTR_ERR(res->register_base_addr));
  95. return -EINVAL;
  96. }
  97. d_vpr_h("%s: reg_base %#x\n", __func__, res->register_base_addr);
  98. return 0;
  99. }
  100. static int __init_irq(struct msm_vidc_core *core)
  101. {
  102. struct msm_vidc_resource *res;
  103. #if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 16, 0))
  104. struct resource *kres;
  105. #endif
  106. int rc = 0;
  107. if (!core || !core->pdev || !core->resource) {
  108. d_vpr_e("%s: invalid params\n", __func__);
  109. return -EINVAL;
  110. }
  111. res = core->resource;
  112. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 16, 0))
  113. res->irq = platform_get_irq(core->pdev, 0);
  114. #else
  115. kres = platform_get_resource(core->pdev, IORESOURCE_IRQ, 0);
  116. res->irq = kres ? kres->start : -1;
  117. #endif
  118. if (res->irq < 0)
  119. d_vpr_e("%s: get irq failed, %d\n", __func__, res->irq);
  120. d_vpr_h("%s: irq %d\n", __func__, res->irq);
  121. rc = devm_request_threaded_irq(&core->pdev->dev, res->irq, venus_hfi_isr,
  122. venus_hfi_isr_handler, IRQF_TRIGGER_HIGH, "msm-vidc", core);
  123. if (rc) {
  124. d_vpr_e("%s: Failed to allocate venus IRQ\n", __func__);
  125. return rc;
  126. }
  127. disable_irq_nosync(res->irq);
  128. return rc;
  129. }
  130. static int __init_bus(struct msm_vidc_core *core)
  131. {
  132. const struct bw_table *bus_tbl;
  133. struct bus_set *interconnects;
  134. struct bus_info *binfo = NULL;
  135. u32 bus_count = 0, cnt = 0;
  136. int rc = 0;
  137. if (!core || !core->resource || !core->platform) {
  138. d_vpr_e("%s: invalid params\n", __func__);
  139. return -EINVAL;
  140. }
  141. interconnects = &core->resource->bus_set;
  142. bus_tbl = core->platform->data.bw_tbl;
  143. bus_count = core->platform->data.bw_tbl_size;
  144. if (!bus_tbl || !bus_count) {
  145. d_vpr_e("%s: invalid bus tbl %#x or count %d\n",
  146. __func__, bus_tbl, bus_count);
  147. return -EINVAL;
  148. }
  149. /* allocate bus_set */
  150. interconnects->bus_tbl = devm_kzalloc(&core->pdev->dev,
  151. sizeof(*interconnects->bus_tbl) * bus_count, GFP_KERNEL);
  152. if (!interconnects->bus_tbl) {
  153. d_vpr_e("%s: failed to alloc memory for bus table\n", __func__);
  154. return -ENOMEM;
  155. }
  156. interconnects->count = bus_count;
  157. /* populate bus field from platform data */
  158. for (cnt = 0; cnt < interconnects->count; cnt++) {
  159. interconnects->bus_tbl[cnt].name = bus_tbl[cnt].name;
  160. interconnects->bus_tbl[cnt].min_kbps = bus_tbl[cnt].min_kbps;
  161. interconnects->bus_tbl[cnt].max_kbps = bus_tbl[cnt].max_kbps;
  162. }
  163. /* print bus fields */
  164. venus_hfi_for_each_bus(core, binfo) {
  165. d_vpr_h("%s: name %s min_kbps %u max_kbps %u\n",
  166. __func__, binfo->name, binfo->min_kbps, binfo->max_kbps);
  167. }
  168. /* get interconnect handle */
  169. venus_hfi_for_each_bus(core, binfo) {
  170. if (!strcmp(binfo->name, "venus-llcc")) {
  171. if (msm_vidc_syscache_disable) {
  172. d_vpr_h("%s: skipping LLC bus init: %s\n", __func__,
  173. binfo->name);
  174. continue;
  175. }
  176. }
  177. binfo->icc = devm_of_icc_get(&core->pdev->dev, binfo->name);
  178. if (IS_ERR_OR_NULL(binfo->icc)) {
  179. d_vpr_e("%s: failed to get bus: %s\n", __func__, binfo->name);
  180. rc = PTR_ERR(binfo->icc) ?
  181. PTR_ERR(binfo->icc) : -EBADHANDLE;
  182. binfo->icc = NULL;
  183. return rc;
  184. }
  185. }
  186. return rc;
  187. }
  188. static int __init_regulators(struct msm_vidc_core *core)
  189. {
  190. const struct regulator_table *regulator_tbl;
  191. struct regulator_set *regulators;
  192. struct regulator_info *rinfo = NULL;
  193. u32 regulator_count = 0, cnt = 0;
  194. int rc = 0;
  195. if (!core || !core->resource || !core->platform) {
  196. d_vpr_e("%s: invalid params\n", __func__);
  197. return -EINVAL;
  198. }
  199. regulators = &core->resource->regulator_set;
  200. /* skip init if regulators not supported */
  201. if (!is_regulator_supported(core)) {
  202. d_vpr_h("%s: regulators are not available in database\n", __func__);
  203. return 0;
  204. }
  205. regulator_tbl = core->platform->data.regulator_tbl;
  206. regulator_count = core->platform->data.regulator_tbl_size;
  207. if (!regulator_tbl || !regulator_count) {
  208. d_vpr_e("%s: invalid regulator tbl %#x or count %d\n",
  209. __func__, regulator_tbl, regulator_count);
  210. return -EINVAL;
  211. }
  212. /* allocate regulator_set */
  213. regulators->regulator_tbl = devm_kzalloc(&core->pdev->dev,
  214. sizeof(*regulators->regulator_tbl) * regulator_count, GFP_KERNEL);
  215. if (!regulators->regulator_tbl) {
  216. d_vpr_e("%s: failed to alloc memory for regulator table\n", __func__);
  217. return -ENOMEM;
  218. }
  219. regulators->count = regulator_count;
  220. /* populate regulator fields */
  221. for (cnt = 0; cnt < regulators->count; cnt++) {
  222. regulators->regulator_tbl[cnt].name = regulator_tbl[cnt].name;
  223. regulators->regulator_tbl[cnt].hw_power_collapse = regulator_tbl[cnt].hw_trigger;
  224. }
  225. /* print regulator fields */
  226. venus_hfi_for_each_regulator(core, rinfo) {
  227. d_vpr_h("%s: name %s hw_power_collapse %d\n",
  228. __func__, rinfo->name, rinfo->hw_power_collapse);
  229. }
  230. /* get regulator handle */
  231. venus_hfi_for_each_regulator(core, rinfo) {
  232. rinfo->regulator = devm_regulator_get(&core->pdev->dev, rinfo->name);
  233. if (IS_ERR_OR_NULL(rinfo->regulator)) {
  234. rc = PTR_ERR(rinfo->regulator) ?
  235. PTR_ERR(rinfo->regulator) : -EBADHANDLE;
  236. d_vpr_e("%s: failed to get regulator: %s\n", __func__, rinfo->name);
  237. rinfo->regulator = NULL;
  238. return rc;
  239. }
  240. }
  241. return rc;
  242. }
  243. static int __init_clocks(struct msm_vidc_core *core)
  244. {
  245. struct clock_residency *residency = NULL;
  246. const struct clk_table *clk_tbl;
  247. struct freq_table *freq_tbl;
  248. struct clock_set *clocks;
  249. struct clock_info *cinfo = NULL;
  250. u32 clk_count = 0, freq_count = 0;
  251. int fcnt = 0, cnt = 0, rc = 0;
  252. if (!core || !core->resource || !core->platform) {
  253. d_vpr_e("%s: invalid params\n", __func__);
  254. return -EINVAL;
  255. }
  256. clocks = &core->resource->clock_set;
  257. clk_tbl = core->platform->data.clk_tbl;
  258. clk_count = core->platform->data.clk_tbl_size;
  259. if (!clk_tbl || !clk_count) {
  260. d_vpr_e("%s: invalid clock tbl %#x or count %d\n",
  261. __func__, clk_tbl, clk_count);
  262. return -EINVAL;
  263. }
  264. /* allocate clock_set */
  265. clocks->clock_tbl = devm_kzalloc(&core->pdev->dev,
  266. sizeof(*clocks->clock_tbl) * clk_count, GFP_KERNEL);
  267. if (!clocks->clock_tbl) {
  268. d_vpr_e("%s: failed to alloc memory for clock table\n", __func__);
  269. return -ENOMEM;
  270. }
  271. clocks->count = clk_count;
  272. /* populate clock field from platform data */
  273. for (cnt = 0; cnt < clocks->count; cnt++) {
  274. clocks->clock_tbl[cnt].name = clk_tbl[cnt].name;
  275. clocks->clock_tbl[cnt].clk_id = clk_tbl[cnt].clk_id;
  276. clocks->clock_tbl[cnt].has_scaling = clk_tbl[cnt].scaling;
  277. }
  278. freq_tbl = core->platform->data.freq_tbl;
  279. freq_count = core->platform->data.freq_tbl_size;
  280. /* populate clk residency stats table */
  281. for (cnt = 0; cnt < clocks->count; cnt++) {
  282. /* initialize residency_list */
  283. INIT_LIST_HEAD(&clocks->clock_tbl[cnt].residency_list);
  284. /* skip if scaling not supported */
  285. if (!clocks->clock_tbl[cnt].has_scaling)
  286. continue;
  287. for (fcnt = 0; fcnt < freq_count; fcnt++) {
  288. residency = devm_kzalloc(&core->pdev->dev,
  289. sizeof(struct clock_residency), GFP_KERNEL);
  290. if (!residency) {
  291. d_vpr_e("%s: failed to alloc clk residency stat node\n", __func__);
  292. return -ENOMEM;
  293. }
  294. if (!freq_tbl) {
  295. d_vpr_e("%s: invalid freq tbl %#x\n", __func__, freq_tbl);
  296. return -EINVAL;
  297. }
  298. /* update residency node */
  299. residency->rate = freq_tbl[fcnt].freq;
  300. residency->start_time_us = 0;
  301. residency->total_time_us = 0;
  302. INIT_LIST_HEAD(&residency->list);
  303. /* add entry into residency_list */
  304. list_add_tail(&residency->list, &clocks->clock_tbl[cnt].residency_list);
  305. }
  306. }
  307. /* print clock fields */
  308. venus_hfi_for_each_clock(core, cinfo) {
  309. d_vpr_h("%s: clock name %s clock id %#x scaling %d\n",
  310. __func__, cinfo->name, cinfo->clk_id, cinfo->has_scaling);
  311. }
  312. /* get clock handle */
  313. venus_hfi_for_each_clock(core, cinfo) {
  314. cinfo->clk = devm_clk_get(&core->pdev->dev, cinfo->name);
  315. if (IS_ERR_OR_NULL(cinfo->clk)) {
  316. d_vpr_e("%s: failed to get clock: %s\n", __func__, cinfo->name);
  317. rc = PTR_ERR(cinfo->clk) ?
  318. PTR_ERR(cinfo->clk) : -EINVAL;
  319. cinfo->clk = NULL;
  320. return rc;
  321. }
  322. }
  323. return rc;
  324. }
  325. static int __clock_set_flag(struct msm_vidc_core *core,
  326. const char *name, enum branch_mem_flags flag)
  327. {
  328. struct clock_info *cinfo = NULL;
  329. bool found = false;
  330. /* get clock handle */
  331. venus_hfi_for_each_clock(core, cinfo) {
  332. if (strcmp(cinfo->name, name))
  333. continue;
  334. found = true;
  335. qcom_clk_set_flags(cinfo->clk, flag);
  336. d_vpr_h("%s: set flag %d on clock %s\n", __func__, flag, name);
  337. break;
  338. }
  339. if (!found) {
  340. d_vpr_e("%s: failed to find clock: %s\n", __func__, name);
  341. return -EINVAL;
  342. }
  343. return 0;
  344. }
  345. static int __init_reset_clocks(struct msm_vidc_core *core)
  346. {
  347. const struct clk_rst_table *rst_tbl;
  348. struct reset_set *rsts;
  349. struct reset_info *rinfo = NULL;
  350. u32 rst_count = 0, cnt = 0;
  351. int rc = 0;
  352. if (!core || !core->resource || !core->platform) {
  353. d_vpr_e("%s: invalid params\n", __func__);
  354. return -EINVAL;
  355. }
  356. rsts = &core->resource->reset_set;
  357. rst_tbl = core->platform->data.clk_rst_tbl;
  358. rst_count = core->platform->data.clk_rst_tbl_size;
  359. if (!rst_tbl || !rst_count) {
  360. d_vpr_e("%s: invalid reset tbl %#x or count %d\n",
  361. __func__, rst_tbl, rst_count);
  362. return -EINVAL;
  363. }
  364. /* allocate reset_set */
  365. rsts->reset_tbl = devm_kzalloc(&core->pdev->dev,
  366. sizeof(*rsts->reset_tbl) * rst_count, GFP_KERNEL);
  367. if (!rsts->reset_tbl) {
  368. d_vpr_e("%s: failed to alloc memory for reset table\n", __func__);
  369. return -ENOMEM;
  370. }
  371. rsts->count = rst_count;
  372. /* populate clock field from platform data */
  373. for (cnt = 0; cnt < rsts->count; cnt++) {
  374. rsts->reset_tbl[cnt].name = rst_tbl[cnt].name;
  375. rsts->reset_tbl[cnt].exclusive_release = rst_tbl[cnt].exclusive_release;
  376. }
  377. /* print reset clock fields */
  378. venus_hfi_for_each_reset_clock(core, rinfo) {
  379. d_vpr_h("%s: reset clk %s, exclusive %d\n",
  380. __func__, rinfo->name, rinfo->exclusive_release);
  381. }
  382. /* get reset clock handle */
  383. venus_hfi_for_each_reset_clock(core, rinfo) {
  384. if (rinfo->exclusive_release)
  385. rinfo->rst = devm_reset_control_get_exclusive_released(
  386. &core->pdev->dev, rinfo->name);
  387. else
  388. rinfo->rst = devm_reset_control_get(&core->pdev->dev, rinfo->name);
  389. if (IS_ERR_OR_NULL(rinfo->rst)) {
  390. d_vpr_e("%s: failed to get reset clock: %s\n", __func__, rinfo->name);
  391. rc = PTR_ERR(rinfo->rst) ?
  392. PTR_ERR(rinfo->rst) : -EINVAL;
  393. rinfo->rst = NULL;
  394. return rc;
  395. }
  396. }
  397. return rc;
  398. }
  399. static int __init_subcaches(struct msm_vidc_core *core)
  400. {
  401. const struct subcache_table *llcc_tbl;
  402. struct subcache_set *caches;
  403. struct subcache_info *sinfo = NULL;
  404. u32 llcc_count = 0, cnt = 0;
  405. int rc = 0;
  406. if (!core || !core->resource || !core->platform) {
  407. d_vpr_e("%s: invalid params\n", __func__);
  408. return -EINVAL;
  409. }
  410. caches = &core->resource->subcache_set;
  411. /* skip init if subcache not available */
  412. if (!is_sys_cache_present(core))
  413. return 0;
  414. llcc_tbl = core->platform->data.subcache_tbl;
  415. llcc_count = core->platform->data.subcache_tbl_size;
  416. if (!llcc_tbl || !llcc_count) {
  417. d_vpr_e("%s: invalid llcc tbl %#x or count %d\n",
  418. __func__, llcc_tbl, llcc_count);
  419. return -EINVAL;
  420. }
  421. /* allocate clock_set */
  422. caches->subcache_tbl = devm_kzalloc(&core->pdev->dev,
  423. sizeof(*caches->subcache_tbl) * llcc_count, GFP_KERNEL);
  424. if (!caches->subcache_tbl) {
  425. d_vpr_e("%s: failed to alloc memory for subcache table\n", __func__);
  426. return -ENOMEM;
  427. }
  428. caches->count = llcc_count;
  429. /* populate subcache fields from platform data */
  430. for (cnt = 0; cnt < caches->count; cnt++) {
  431. caches->subcache_tbl[cnt].name = llcc_tbl[cnt].name;
  432. caches->subcache_tbl[cnt].llcc_id = llcc_tbl[cnt].llcc_id;
  433. }
  434. /* print subcache fields */
  435. venus_hfi_for_each_subcache(core, sinfo) {
  436. d_vpr_h("%s: name %s subcache id %d\n",
  437. __func__, sinfo->name, sinfo->llcc_id);
  438. }
  439. /* get subcache/llcc handle */
  440. venus_hfi_for_each_subcache(core, sinfo) {
  441. sinfo->subcache = devm_llcc_get(&core->pdev->dev, sinfo->llcc_id);
  442. if (IS_ERR_OR_NULL(sinfo->subcache)) {
  443. d_vpr_e("%s: failed to get subcache: %d\n", __func__, sinfo->llcc_id);
  444. rc = PTR_ERR(sinfo->subcache) ?
  445. PTR_ERR(sinfo->subcache) : -EBADHANDLE;
  446. sinfo->subcache = NULL;
  447. return rc;
  448. }
  449. }
  450. return rc;
  451. }
  452. static int __init_freq_table(struct msm_vidc_core *core)
  453. {
  454. struct freq_table *freq_tbl;
  455. struct freq_set *clks;
  456. u32 freq_count = 0, cnt = 0;
  457. int rc = 0;
  458. if (!core || !core->resource || !core->platform) {
  459. d_vpr_e("%s: invalid params\n", __func__);
  460. return -EINVAL;
  461. }
  462. clks = &core->resource->freq_set;
  463. freq_tbl = core->platform->data.freq_tbl;
  464. freq_count = core->platform->data.freq_tbl_size;
  465. if (!freq_tbl || !freq_count) {
  466. d_vpr_e("%s: invalid freq tbl %#x or count %d\n",
  467. __func__, freq_tbl, freq_count);
  468. return -EINVAL;
  469. }
  470. /* allocate freq_set */
  471. clks->freq_tbl = devm_kzalloc(&core->pdev->dev,
  472. sizeof(*clks->freq_tbl) * freq_count, GFP_KERNEL);
  473. if (!clks->freq_tbl) {
  474. d_vpr_e("%s: failed to alloc memory for freq table\n", __func__);
  475. return -ENOMEM;
  476. }
  477. clks->count = freq_count;
  478. /* populate freq field from platform data */
  479. for (cnt = 0; cnt < clks->count; cnt++)
  480. clks->freq_tbl[cnt].freq = freq_tbl[cnt].freq;
  481. /* sort freq table */
  482. sort(clks->freq_tbl, clks->count, sizeof(*clks->freq_tbl), cmp, NULL);
  483. /* print freq field freq_set */
  484. d_vpr_h("%s: updated freq table\n", __func__);
  485. for (cnt = 0; cnt < clks->count; cnt++)
  486. d_vpr_h("%s:\t %lu\n", __func__, clks->freq_tbl[cnt].freq);
  487. return rc;
  488. }
  489. static int __init_context_banks(struct msm_vidc_core *core)
  490. {
  491. const struct context_bank_table *cb_tbl;
  492. struct context_bank_set *cbs;
  493. struct context_bank_info *cbinfo = NULL;
  494. u32 cb_count = 0, cnt = 0;
  495. int rc = 0;
  496. if (!core || !core->resource || !core->platform) {
  497. d_vpr_e("%s: invalid params\n", __func__);
  498. return -EINVAL;
  499. }
  500. cbs = &core->resource->context_bank_set;
  501. cb_tbl = core->platform->data.context_bank_tbl;
  502. cb_count = core->platform->data.context_bank_tbl_size;
  503. if (!cb_tbl || !cb_count) {
  504. d_vpr_e("%s: invalid context bank tbl %#x or count %d\n",
  505. __func__, cb_tbl, cb_count);
  506. return -EINVAL;
  507. }
  508. /* allocate context_bank table */
  509. cbs->context_bank_tbl = devm_kzalloc(&core->pdev->dev,
  510. sizeof(*cbs->context_bank_tbl) * cb_count, GFP_KERNEL);
  511. if (!cbs->context_bank_tbl) {
  512. d_vpr_e("%s: failed to alloc memory for context_bank table\n", __func__);
  513. return -ENOMEM;
  514. }
  515. cbs->count = cb_count;
  516. /**
  517. * populate context bank field from platform data except
  518. * dev & domain which are assigned as part of context bank
  519. * probe sequence
  520. */
  521. for (cnt = 0; cnt < cbs->count; cnt++) {
  522. cbs->context_bank_tbl[cnt].name = cb_tbl[cnt].name;
  523. cbs->context_bank_tbl[cnt].addr_range.start = cb_tbl[cnt].start;
  524. cbs->context_bank_tbl[cnt].addr_range.size = cb_tbl[cnt].size;
  525. cbs->context_bank_tbl[cnt].secure = cb_tbl[cnt].secure;
  526. cbs->context_bank_tbl[cnt].dma_coherant = cb_tbl[cnt].dma_coherant;
  527. cbs->context_bank_tbl[cnt].region = cb_tbl[cnt].region;
  528. cbs->context_bank_tbl[cnt].dma_mask = cb_tbl[cnt].dma_mask;
  529. }
  530. /* print context_bank fiels */
  531. venus_hfi_for_each_context_bank(core, cbinfo) {
  532. d_vpr_h("%s: name %s addr start %#x size %#x secure %d "
  533. "coherant %d region %d dma_mask %llu\n",
  534. __func__, cbinfo->name, cbinfo->addr_range.start,
  535. cbinfo->addr_range.size, cbinfo->secure,
  536. cbinfo->dma_coherant, cbinfo->region, cbinfo->dma_mask);
  537. }
  538. return rc;
  539. }
  540. static int __init_device_region(struct msm_vidc_core *core)
  541. {
  542. const struct device_region_table *dev_reg_tbl;
  543. struct device_region_set *dev_set;
  544. struct device_region_info *dev_reg_info;
  545. u32 dev_reg_count = 0, cnt = 0;
  546. int rc = 0;
  547. if (!core || !core->resource || !core->platform) {
  548. d_vpr_e("%s: invalid params\n", __func__);
  549. return -EINVAL;
  550. }
  551. dev_set = &core->resource->device_region_set;
  552. dev_reg_tbl = core->platform->data.dev_reg_tbl;
  553. dev_reg_count = core->platform->data.dev_reg_tbl_size;
  554. if (!dev_reg_tbl || !dev_reg_count) {
  555. d_vpr_h("%s: device regions not available\n", __func__);
  556. return 0;
  557. }
  558. /* allocate device region table */
  559. dev_set->device_region_tbl = devm_kzalloc(&core->pdev->dev,
  560. sizeof(*dev_set->device_region_tbl) * dev_reg_count, GFP_KERNEL);
  561. if (!dev_set->device_region_tbl) {
  562. d_vpr_e("%s: failed to alloc memory for device region table\n", __func__);
  563. return -ENOMEM;
  564. }
  565. dev_set->count = dev_reg_count;
  566. /* populate device region fields from platform data */
  567. for (cnt = 0; cnt < dev_set->count; cnt++) {
  568. dev_set->device_region_tbl[cnt].name = dev_reg_tbl[cnt].name;
  569. dev_set->device_region_tbl[cnt].phy_addr = dev_reg_tbl[cnt].phy_addr;
  570. dev_set->device_region_tbl[cnt].size = dev_reg_tbl[cnt].size;
  571. dev_set->device_region_tbl[cnt].dev_addr = dev_reg_tbl[cnt].dev_addr;
  572. dev_set->device_region_tbl[cnt].region = dev_reg_tbl[cnt].region;
  573. }
  574. /* print device region fields */
  575. venus_hfi_for_each_device_region(core, dev_reg_info) {
  576. d_vpr_h("%s: name %s phy_addr %#x size %#x dev_addr %#x dev_region %d\n",
  577. __func__, dev_reg_info->name, dev_reg_info->phy_addr, dev_reg_info->size,
  578. dev_reg_info->dev_addr, dev_reg_info->region);
  579. }
  580. return rc;
  581. }
  582. #ifdef CONFIG_MSM_MMRM
  583. static int __register_mmrm(struct msm_vidc_core *core)
  584. {
  585. int rc = 0;
  586. struct clock_info *cl;
  587. if (!core || !core->platform) {
  588. d_vpr_e("%s: invalid params\n", __func__);
  589. return -EINVAL;
  590. }
  591. /* skip if platform does not support mmrm */
  592. if (!is_mmrm_supported(core)) {
  593. d_vpr_h("%s: MMRM not supported\n", __func__);
  594. return 0;
  595. }
  596. /* get mmrm handle for each clock sources */
  597. venus_hfi_for_each_clock(core, cl) {
  598. struct mmrm_client_desc desc;
  599. char *name = (char *)desc.client_info.desc.name;
  600. // TODO: set notifier data vals
  601. struct mmrm_client_notifier_data notifier_data = {
  602. MMRM_CLIENT_RESOURCE_VALUE_CHANGE,
  603. {{0, 0}},
  604. NULL};
  605. // TODO: add callback fn
  606. desc.notifier_callback_fn = NULL;
  607. if (!cl->has_scaling)
  608. continue;
  609. if (IS_ERR_OR_NULL(cl->clk)) {
  610. d_vpr_e("%s: Invalid clock: %s\n", __func__, cl->name);
  611. return PTR_ERR(cl->clk) ? PTR_ERR(cl->clk) : -EINVAL;
  612. }
  613. desc.client_type = MMRM_CLIENT_CLOCK;
  614. desc.client_info.desc.client_domain = MMRM_CLIENT_DOMAIN_VIDEO;
  615. desc.client_info.desc.client_id = cl->clk_id;
  616. strscpy(name, cl->name, sizeof(desc.client_info.desc.name));
  617. desc.client_info.desc.clk = cl->clk;
  618. desc.priority = MMRM_CLIENT_PRIOR_LOW;
  619. desc.pvt_data = notifier_data.pvt_data;
  620. d_vpr_h("%s: domain(%d) cid(%d) name(%s) clk(%pK)\n",
  621. __func__,
  622. desc.client_info.desc.client_domain,
  623. desc.client_info.desc.client_id,
  624. desc.client_info.desc.name,
  625. desc.client_info.desc.clk);
  626. d_vpr_h("%s: type(%d) pri(%d) pvt(%pK) notifier(%pK)\n",
  627. __func__,
  628. desc.client_type,
  629. desc.priority,
  630. desc.pvt_data,
  631. desc.notifier_callback_fn);
  632. cl->mmrm_client = devm_mmrm_get(&core->pdev->dev, &desc);
  633. if (!cl->mmrm_client) {
  634. d_vpr_e("%s: Failed to register clk(%s): %d\n",
  635. __func__, cl->name, rc);
  636. return -EINVAL;
  637. }
  638. }
  639. return rc;
  640. }
  641. #else
  642. static int __register_mmrm(struct msm_vidc_core *core)
  643. {
  644. return 0;
  645. }
  646. #endif
  647. static int __acquire_regulator(struct msm_vidc_core *core,
  648. struct regulator_info *rinfo)
  649. {
  650. int rc = 0;
  651. if (!core || !rinfo) {
  652. d_vpr_e("%s: invalid params\n", __func__);
  653. return -EINVAL;
  654. }
  655. if (rinfo->hw_power_collapse) {
  656. if (!rinfo->regulator) {
  657. d_vpr_e("%s: invalid regulator\n", __func__);
  658. rc = -EINVAL;
  659. goto exit;
  660. }
  661. if (regulator_get_mode(rinfo->regulator) ==
  662. REGULATOR_MODE_NORMAL) {
  663. /* clear handoff from core sub_state */
  664. msm_vidc_change_core_sub_state(core,
  665. CORE_SUBSTATE_GDSC_HANDOFF, 0, __func__);
  666. d_vpr_h("Skip acquire regulator %s\n", rinfo->name);
  667. goto exit;
  668. }
  669. rc = regulator_set_mode(rinfo->regulator,
  670. REGULATOR_MODE_NORMAL);
  671. if (rc) {
  672. /*
  673. * This is somewhat fatal, but nothing we can do
  674. * about it. We can't disable the regulator w/o
  675. * getting it back under s/w control
  676. */
  677. d_vpr_e("Failed to acquire regulator control: %s\n",
  678. rinfo->name);
  679. goto exit;
  680. } else {
  681. /* reset handoff from core sub_state */
  682. msm_vidc_change_core_sub_state(core,
  683. CORE_SUBSTATE_GDSC_HANDOFF, 0, __func__);
  684. d_vpr_h("Acquired regulator control from HW: %s\n",
  685. rinfo->name);
  686. }
  687. if (!regulator_is_enabled(rinfo->regulator)) {
  688. d_vpr_e("%s: Regulator is not enabled %s\n",
  689. __func__, rinfo->name);
  690. __fatal_error(true);
  691. }
  692. }
  693. exit:
  694. return rc;
  695. }
  696. static int __acquire_regulators(struct msm_vidc_core *core)
  697. {
  698. int rc = 0;
  699. struct regulator_info *rinfo;
  700. venus_hfi_for_each_regulator(core, rinfo)
  701. __acquire_regulator(core, rinfo);
  702. return rc;
  703. }
  704. static int __hand_off_regulator(struct msm_vidc_core *core,
  705. struct regulator_info *rinfo)
  706. {
  707. int rc = 0;
  708. if (rinfo->hw_power_collapse) {
  709. if (!rinfo->regulator) {
  710. d_vpr_e("%s: invalid regulator\n", __func__);
  711. return -EINVAL;
  712. }
  713. rc = regulator_set_mode(rinfo->regulator,
  714. REGULATOR_MODE_FAST);
  715. if (rc) {
  716. d_vpr_e("Failed to hand off regulator control: %s\n",
  717. rinfo->name);
  718. return rc;
  719. } else {
  720. /* set handoff done in core sub_state */
  721. msm_vidc_change_core_sub_state(core,
  722. 0, CORE_SUBSTATE_GDSC_HANDOFF, __func__);
  723. d_vpr_h("Hand off regulator control to HW: %s\n",
  724. rinfo->name);
  725. }
  726. if (!regulator_is_enabled(rinfo->regulator)) {
  727. d_vpr_e("%s: Regulator is not enabled %s\n",
  728. __func__, rinfo->name);
  729. __fatal_error(true);
  730. }
  731. }
  732. return rc;
  733. }
  734. static int __hand_off_regulators(struct msm_vidc_core *core)
  735. {
  736. struct regulator_info *rinfo;
  737. int rc = 0, c = 0;
  738. venus_hfi_for_each_regulator(core, rinfo) {
  739. rc = __hand_off_regulator(core, rinfo);
  740. /*
  741. * If one regulator hand off failed, driver should take
  742. * the control for other regulators back.
  743. */
  744. if (rc)
  745. goto err_reg_handoff_failed;
  746. c++;
  747. }
  748. return rc;
  749. err_reg_handoff_failed:
  750. venus_hfi_for_each_regulator_reverse_continue(core, rinfo, c)
  751. __acquire_regulator(core, rinfo);
  752. return rc;
  753. }
  754. static int __disable_regulator(struct msm_vidc_core *core, const char *reg_name)
  755. {
  756. int rc = 0;
  757. struct regulator_info *rinfo;
  758. bool found;
  759. if (!core || !reg_name) {
  760. d_vpr_e("%s: invalid params\n", __func__);
  761. return -EINVAL;
  762. }
  763. found = false;
  764. venus_hfi_for_each_regulator(core, rinfo) {
  765. if (!rinfo->regulator) {
  766. d_vpr_e("%s: invalid regulator %s\n",
  767. __func__, rinfo->name);
  768. return -EINVAL;
  769. }
  770. if (strcmp(rinfo->name, reg_name))
  771. continue;
  772. found = true;
  773. rc = __acquire_regulator(core, rinfo);
  774. if (rc) {
  775. d_vpr_e("%s: failed to acquire %s, rc = %d\n",
  776. __func__, rinfo->name, rc);
  777. /* Bring attention to this issue */
  778. WARN_ON(true);
  779. return rc;
  780. }
  781. /* reset handoff done from core sub_state */
  782. msm_vidc_change_core_sub_state(core, CORE_SUBSTATE_GDSC_HANDOFF, 0, __func__);
  783. rc = regulator_disable(rinfo->regulator);
  784. if (rc) {
  785. d_vpr_e("%s: failed to disable %s, rc = %d\n",
  786. __func__, rinfo->name, rc);
  787. return rc;
  788. }
  789. d_vpr_h("%s: disabled regulator %s\n", __func__, rinfo->name);
  790. break;
  791. }
  792. if (!found) {
  793. d_vpr_e("%s: regulator %s not found\n", __func__, reg_name);
  794. return -EINVAL;
  795. }
  796. return rc;
  797. }
  798. static int __enable_regulator(struct msm_vidc_core *core, const char *reg_name)
  799. {
  800. int rc = 0;
  801. struct regulator_info *rinfo;
  802. bool found;
  803. if (!core || !reg_name) {
  804. d_vpr_e("%s: invalid params\n", __func__);
  805. return -EINVAL;
  806. }
  807. found = false;
  808. venus_hfi_for_each_regulator(core, rinfo) {
  809. if (!rinfo->regulator) {
  810. d_vpr_e("%s: invalid regulator %s\n",
  811. __func__, rinfo->name);
  812. return -EINVAL;
  813. }
  814. if (strcmp(rinfo->name, reg_name))
  815. continue;
  816. found = true;
  817. rc = regulator_enable(rinfo->regulator);
  818. if (rc) {
  819. d_vpr_e("%s: failed to enable %s, rc = %d\n",
  820. __func__, rinfo->name, rc);
  821. return rc;
  822. }
  823. if (!regulator_is_enabled(rinfo->regulator)) {
  824. d_vpr_e("%s: regulator %s not enabled\n",
  825. __func__, rinfo->name);
  826. regulator_disable(rinfo->regulator);
  827. return -EINVAL;
  828. }
  829. d_vpr_h("%s: enabled regulator %s\n", __func__, rinfo->name);
  830. break;
  831. }
  832. if (!found) {
  833. d_vpr_e("%s: regulator %s not found\n", __func__, reg_name);
  834. return -EINVAL;
  835. }
  836. return rc;
  837. }
  838. static int __disable_subcaches(struct msm_vidc_core *core)
  839. {
  840. struct subcache_info *sinfo;
  841. int rc = 0;
  842. if (msm_vidc_syscache_disable || !is_sys_cache_present(core))
  843. return 0;
  844. /* De-activate subcaches */
  845. venus_hfi_for_each_subcache_reverse(core, sinfo) {
  846. if (!sinfo->isactive)
  847. continue;
  848. d_vpr_h("%s: De-activate subcache %s\n", __func__, sinfo->name);
  849. rc = llcc_slice_deactivate(sinfo->subcache);
  850. if (rc) {
  851. d_vpr_e("Failed to de-activate %s: %d\n",
  852. sinfo->name, rc);
  853. }
  854. sinfo->isactive = false;
  855. }
  856. return 0;
  857. }
  858. static int __enable_subcaches(struct msm_vidc_core *core)
  859. {
  860. int rc = 0;
  861. u32 c = 0;
  862. struct subcache_info *sinfo;
  863. if (msm_vidc_syscache_disable || !is_sys_cache_present(core))
  864. return 0;
  865. /* Activate subcaches */
  866. venus_hfi_for_each_subcache(core, sinfo) {
  867. rc = llcc_slice_activate(sinfo->subcache);
  868. if (rc) {
  869. d_vpr_e("Failed to activate %s: %d\n", sinfo->name, rc);
  870. __fatal_error(true);
  871. goto err_activate_fail;
  872. }
  873. sinfo->isactive = true;
  874. d_vpr_h("Activated subcache %s\n", sinfo->name);
  875. c++;
  876. }
  877. d_vpr_h("Activated %d Subcaches to Venus\n", c);
  878. return 0;
  879. err_activate_fail:
  880. __disable_subcaches(core);
  881. return rc;
  882. }
  883. static int llcc_enable(struct msm_vidc_core *core, bool enable)
  884. {
  885. int ret;
  886. if (enable)
  887. ret = __enable_subcaches(core);
  888. else
  889. ret = __disable_subcaches(core);
  890. return ret;
  891. }
  892. static int __vote_bandwidth(struct bus_info *bus, unsigned long bw_kbps)
  893. {
  894. int rc = 0;
  895. if (!bus->icc) {
  896. d_vpr_e("%s: invalid bus\n", __func__);
  897. return -EINVAL;
  898. }
  899. d_vpr_p("Voting bus %s to ab %lu kBps\n", bus->name, bw_kbps);
  900. rc = icc_set_bw(bus->icc, bw_kbps, 0);
  901. if (rc)
  902. d_vpr_e("Failed voting bus %s to ab %lu, rc=%d\n",
  903. bus->name, bw_kbps, rc);
  904. return rc;
  905. }
  906. static int __unvote_buses(struct msm_vidc_core *core)
  907. {
  908. int rc = 0;
  909. struct bus_info *bus = NULL;
  910. if (!core) {
  911. d_vpr_e("%s: invalid params\n", __func__);
  912. return -EINVAL;
  913. }
  914. core->power.bw_ddr = 0;
  915. core->power.bw_llcc = 0;
  916. venus_hfi_for_each_bus(core, bus) {
  917. rc = __vote_bandwidth(bus, 0);
  918. if (rc)
  919. goto err_unknown_device;
  920. }
  921. err_unknown_device:
  922. return rc;
  923. }
  924. static int __vote_buses(struct msm_vidc_core *core,
  925. unsigned long bw_ddr, unsigned long bw_llcc)
  926. {
  927. int rc = 0;
  928. struct bus_info *bus = NULL;
  929. unsigned long bw_kbps = 0, bw_prev = 0;
  930. enum vidc_bus_type type;
  931. if (!core) {
  932. d_vpr_e("%s: invalid params\n", __func__);
  933. return -EINVAL;
  934. }
  935. venus_hfi_for_each_bus(core, bus) {
  936. if (bus && bus->icc) {
  937. type = get_type_frm_name(bus->name);
  938. if (type == DDR) {
  939. bw_kbps = bw_ddr;
  940. bw_prev = core->power.bw_ddr;
  941. } else if (type == LLCC) {
  942. bw_kbps = bw_llcc;
  943. bw_prev = core->power.bw_llcc;
  944. } else {
  945. bw_kbps = bus->max_kbps;
  946. bw_prev = core->power.bw_ddr ?
  947. bw_kbps : 0;
  948. }
  949. /* ensure freq is within limits */
  950. bw_kbps = clamp_t(typeof(bw_kbps), bw_kbps,
  951. bus->min_kbps, bus->max_kbps);
  952. if (TRIVIAL_BW_CHANGE(bw_kbps, bw_prev) && bw_prev) {
  953. d_vpr_l("Skip voting bus %s to %lu kBps\n",
  954. bus->name, bw_kbps);
  955. continue;
  956. }
  957. rc = __vote_bandwidth(bus, bw_kbps);
  958. if (type == DDR)
  959. core->power.bw_ddr = bw_kbps;
  960. else if (type == LLCC)
  961. core->power.bw_llcc = bw_kbps;
  962. } else {
  963. d_vpr_e("No BUS to Vote\n");
  964. }
  965. }
  966. return rc;
  967. }
  968. static int set_bw(struct msm_vidc_core *core, unsigned long bw_ddr,
  969. unsigned long bw_llcc)
  970. {
  971. if (!bw_ddr && !bw_llcc)
  972. return __unvote_buses(core);
  973. return __vote_buses(core, bw_ddr, bw_llcc);
  974. }
  975. static int print_residency_stats(struct msm_vidc_core *core, struct clock_info *cl)
  976. {
  977. struct clock_residency *residency = NULL;
  978. u64 total_time_us = 0;
  979. int rc = 0;
  980. if (!core || !cl) {
  981. d_vpr_e("%s: invalid params\n", __func__);
  982. return -EINVAL;
  983. }
  984. /* skip if scaling not supported */
  985. if (!cl->has_scaling)
  986. return 0;
  987. /* grand total residency time */
  988. list_for_each_entry(residency, &cl->residency_list, list)
  989. total_time_us += residency->total_time_us;
  990. /* sanity check to avoid divide by 0 */
  991. total_time_us = (total_time_us > 0) ? total_time_us : 1;
  992. /* print residency percent for each clock */
  993. list_for_each_entry(residency, &cl->residency_list, list) {
  994. d_vpr_h("%s: %s clock rate [%d] total %lluus residency %u%%\n",
  995. __func__, cl->name, residency->rate, residency->total_time_us,
  996. residency->total_time_us * 100 / total_time_us);
  997. }
  998. return rc;
  999. }
  1000. static int reset_residency_stats(struct msm_vidc_core *core, struct clock_info *cl)
  1001. {
  1002. struct clock_residency *residency = NULL;
  1003. int rc = 0;
  1004. if (!core || !cl) {
  1005. d_vpr_e("%s: invalid params\n", __func__);
  1006. return -EINVAL;
  1007. }
  1008. /* skip if scaling not supported */
  1009. if (!cl->has_scaling)
  1010. return 0;
  1011. d_vpr_h("%s: reset %s residency stats\n", __func__, cl->name);
  1012. /* reset clock residency stats */
  1013. list_for_each_entry(residency, &cl->residency_list, list) {
  1014. residency->start_time_us = 0;
  1015. residency->total_time_us = 0;
  1016. }
  1017. return rc;
  1018. }
  1019. static struct clock_residency *get_residency_stats(struct clock_info *cl, u64 rate)
  1020. {
  1021. struct clock_residency *residency = NULL;
  1022. bool found = false;
  1023. if (!cl) {
  1024. d_vpr_e("%s: invalid params\n", __func__);
  1025. return NULL;
  1026. }
  1027. list_for_each_entry(residency, &cl->residency_list, list) {
  1028. if (residency->rate == rate) {
  1029. found = true;
  1030. break;
  1031. }
  1032. }
  1033. return found ? residency : NULL;
  1034. }
  1035. static int update_residency_stats(
  1036. struct msm_vidc_core *core, struct clock_info *cl, u64 rate)
  1037. {
  1038. struct clock_residency *cur_residency = NULL, *prev_residency = NULL;
  1039. u64 cur_time_us = 0;
  1040. int rc = 0;
  1041. if (!core || !cl) {
  1042. d_vpr_e("%s: invalid params\n", __func__);
  1043. return -EINVAL;
  1044. }
  1045. /* skip update if scaling not supported */
  1046. if (!cl->has_scaling)
  1047. return 0;
  1048. /* skip update if rate not changed */
  1049. if (rate == cl->prev)
  1050. return 0;
  1051. /* get current time in ns */
  1052. cur_time_us = ktime_get_ns() / 1000;
  1053. /* update previous rate residency end or total time */
  1054. prev_residency = get_residency_stats(cl, cl->prev);
  1055. if (prev_residency) {
  1056. if (prev_residency->start_time_us)
  1057. prev_residency->total_time_us = cur_time_us - prev_residency->start_time_us;
  1058. /* reset start time us */
  1059. prev_residency->start_time_us = 0;
  1060. }
  1061. /* clk disable case - no need to update new entry */
  1062. if (rate == 0)
  1063. return 0;
  1064. /* check if rate entry is present */
  1065. cur_residency = get_residency_stats(cl, rate);
  1066. if (!cur_residency) {
  1067. d_vpr_e("%s: entry not found. rate %llu\n", __func__, rate);
  1068. return -EINVAL;
  1069. }
  1070. /* update residency start time for current rate/freq */
  1071. cur_residency->start_time_us = cur_time_us;
  1072. return rc;
  1073. }
  1074. #ifdef CONFIG_MSM_MMRM
  1075. static int __set_clk_rate(struct msm_vidc_core *core, struct clock_info *cl,
  1076. u64 rate)
  1077. {
  1078. int rc = 0;
  1079. struct mmrm_client_data client_data;
  1080. struct mmrm_client *client;
  1081. u64 srate;
  1082. /* not registered */
  1083. if (!core || !cl || !core->platform) {
  1084. d_vpr_e("%s: invalid params\n", __func__);
  1085. return -EINVAL;
  1086. }
  1087. if (is_mmrm_supported(core) && !cl->mmrm_client) {
  1088. d_vpr_e("%s: invalid mmrm client\n", __func__);
  1089. return -EINVAL;
  1090. }
  1091. /* update clock residency stats */
  1092. update_residency_stats(core, cl, rate);
  1093. /*
  1094. * This conversion is necessary since we are scaling clock values based on
  1095. * the branch clock. However, mmrm driver expects source clock to be registered
  1096. * and used for scaling.
  1097. * TODO: Remove this scaling if using source clock instead of branch clock.
  1098. */
  1099. srate = rate * MSM_VIDC_CLOCK_SOURCE_SCALING_RATIO;
  1100. /* bail early if requested clk rate is not changed */
  1101. if (rate == cl->prev)
  1102. return 0;
  1103. d_vpr_p("Scaling clock %s to %llu, prev %llu\n",
  1104. cl->name, srate, cl->prev * MSM_VIDC_CLOCK_SOURCE_SCALING_RATIO);
  1105. if (is_mmrm_supported(core)) {
  1106. /* set clock rate to mmrm driver */
  1107. client = cl->mmrm_client;
  1108. memset(&client_data, 0, sizeof(client_data));
  1109. client_data.num_hw_blocks = 1;
  1110. rc = mmrm_client_set_value(client, &client_data, srate);
  1111. if (rc) {
  1112. d_vpr_e("%s: Failed to set mmrm clock rate %llu %s: %d\n",
  1113. __func__, srate, cl->name, rc);
  1114. return rc;
  1115. }
  1116. } else {
  1117. /* set clock rate to clock driver */
  1118. rc = clk_set_rate(cl->clk, srate);
  1119. if (rc) {
  1120. d_vpr_e("%s: Failed to set clock rate %llu %s: %d\n",
  1121. __func__, srate, cl->name, rc);
  1122. return rc;
  1123. }
  1124. }
  1125. cl->prev = rate;
  1126. return rc;
  1127. }
  1128. #else
  1129. static int __set_clk_rate(struct msm_vidc_core *core, struct clock_info *cl,
  1130. u64 rate)
  1131. {
  1132. u64 srate;
  1133. int rc = 0;
  1134. /* not registered */
  1135. if (!core || !cl || !core->capabilities) {
  1136. d_vpr_e("%s: invalid params\n", __func__);
  1137. return -EINVAL;
  1138. }
  1139. /* update clock residency stats */
  1140. update_residency_stats(core, cl, rate);
  1141. /*
  1142. * This conversion is necessary since we are scaling clock values based on
  1143. * the branch clock. However, mmrm driver expects source clock to be registered
  1144. * and used for scaling.
  1145. * TODO: Remove this scaling if using source clock instead of branch clock.
  1146. */
  1147. srate = rate * MSM_VIDC_CLOCK_SOURCE_SCALING_RATIO;
  1148. /* bail early if requested clk rate is not changed */
  1149. if (rate == cl->prev)
  1150. return 0;
  1151. d_vpr_p("Scaling clock %s to %llu, prev %llu\n",
  1152. cl->name, srate, cl->prev * MSM_VIDC_CLOCK_SOURCE_SCALING_RATIO);
  1153. rc = clk_set_rate(cl->clk, srate);
  1154. if (rc) {
  1155. d_vpr_e("%s: Failed to set clock rate %llu %s: %d\n",
  1156. __func__, srate, cl->name, rc);
  1157. return rc;
  1158. }
  1159. cl->prev = rate;
  1160. return rc;
  1161. }
  1162. #endif
  1163. static int __set_clocks(struct msm_vidc_core *core, u64 freq)
  1164. {
  1165. int rc = 0;
  1166. struct clock_info *cl;
  1167. venus_hfi_for_each_clock(core, cl) {
  1168. if (cl->has_scaling) {
  1169. rc = __set_clk_rate(core, cl, freq);
  1170. if (rc)
  1171. return rc;
  1172. }
  1173. }
  1174. return 0;
  1175. }
  1176. static int __disable_unprepare_clock(struct msm_vidc_core *core,
  1177. const char *clk_name)
  1178. {
  1179. int rc = 0;
  1180. struct clock_info *cl;
  1181. bool found;
  1182. if (!core || !clk_name) {
  1183. d_vpr_e("%s: invalid params\n", __func__);
  1184. return -EINVAL;
  1185. }
  1186. found = false;
  1187. venus_hfi_for_each_clock(core, cl) {
  1188. if (!cl->clk) {
  1189. d_vpr_e("%s: invalid clock %s\n", __func__, cl->name);
  1190. return -EINVAL;
  1191. }
  1192. if (strcmp(cl->name, clk_name))
  1193. continue;
  1194. found = true;
  1195. clk_disable_unprepare(cl->clk);
  1196. if (cl->has_scaling)
  1197. __set_clk_rate(core, cl, 0);
  1198. cl->prev = 0;
  1199. d_vpr_h("%s: clock %s disable unprepared\n", __func__, cl->name);
  1200. break;
  1201. }
  1202. if (!found) {
  1203. d_vpr_e("%s: clock %s not found\n", __func__, clk_name);
  1204. return -EINVAL;
  1205. }
  1206. return rc;
  1207. }
  1208. static int __prepare_enable_clock(struct msm_vidc_core *core,
  1209. const char *clk_name)
  1210. {
  1211. int rc = 0;
  1212. struct clock_info *cl;
  1213. bool found;
  1214. u64 rate = 0;
  1215. if (!core || !clk_name) {
  1216. d_vpr_e("%s: invalid params\n", __func__);
  1217. return -EINVAL;
  1218. }
  1219. found = false;
  1220. venus_hfi_for_each_clock(core, cl) {
  1221. if (!cl->clk) {
  1222. d_vpr_e("%s: invalid clock\n", __func__);
  1223. return -EINVAL;
  1224. }
  1225. if (strcmp(cl->name, clk_name))
  1226. continue;
  1227. found = true;
  1228. /*
  1229. * For the clocks we control, set the rate prior to preparing
  1230. * them. Since we don't really have a load at this point, scale
  1231. * it to the lowest frequency possible
  1232. */
  1233. if (cl->has_scaling) {
  1234. /* reset clk residency stats */
  1235. reset_residency_stats(core, cl);
  1236. rate = clk_round_rate(cl->clk, 0);
  1237. /**
  1238. * source clock is already multipled with scaling ratio and __set_clk_rate
  1239. * attempts to multiply again. So divide scaling ratio before calling
  1240. * __set_clk_rate.
  1241. */
  1242. rate = rate / MSM_VIDC_CLOCK_SOURCE_SCALING_RATIO;
  1243. __set_clk_rate(core, cl, rate);
  1244. }
  1245. rc = clk_prepare_enable(cl->clk);
  1246. if (rc) {
  1247. d_vpr_e("%s: failed to enable clock %s\n",
  1248. __func__, cl->name);
  1249. return rc;
  1250. }
  1251. if (!__clk_is_enabled(cl->clk)) {
  1252. d_vpr_e("%s: clock %s not enabled\n",
  1253. __func__, cl->name);
  1254. clk_disable_unprepare(cl->clk);
  1255. if (cl->has_scaling)
  1256. __set_clk_rate(core, cl, 0);
  1257. return -EINVAL;
  1258. }
  1259. d_vpr_h("%s: clock %s prepare enabled\n", __func__, cl->name);
  1260. break;
  1261. }
  1262. if (!found) {
  1263. d_vpr_e("%s: clock %s not found\n", __func__, clk_name);
  1264. return -EINVAL;
  1265. }
  1266. return rc;
  1267. }
  1268. static int __init_resources(struct msm_vidc_core *core)
  1269. {
  1270. int rc = 0;
  1271. rc = __init_register_base(core);
  1272. if (rc)
  1273. return rc;
  1274. rc = __init_irq(core);
  1275. if (rc)
  1276. return rc;
  1277. rc = __init_bus(core);
  1278. if (rc)
  1279. return rc;
  1280. rc = __init_regulators(core);
  1281. if (rc)
  1282. return rc;
  1283. rc = __init_clocks(core);
  1284. if (rc)
  1285. return rc;
  1286. rc = __init_reset_clocks(core);
  1287. if (rc)
  1288. return rc;
  1289. rc = __init_subcaches(core);
  1290. if (rc)
  1291. return rc;
  1292. rc = __init_freq_table(core);
  1293. if (rc)
  1294. return rc;
  1295. rc = __init_context_banks(core);
  1296. if (rc)
  1297. return rc;
  1298. rc = __init_device_region(core);
  1299. if (rc)
  1300. return rc;
  1301. rc = __register_mmrm(core);
  1302. if (rc)
  1303. return rc;
  1304. return rc;
  1305. }
  1306. static int __reset_control_acquire_name(struct msm_vidc_core *core,
  1307. const char *name)
  1308. {
  1309. struct reset_info *rcinfo = NULL;
  1310. int rc = 0;
  1311. bool found = false;
  1312. venus_hfi_for_each_reset_clock(core, rcinfo) {
  1313. if (strcmp(rcinfo->name, name))
  1314. continue;
  1315. /* this function is valid only for exclusive_release reset clocks*/
  1316. if (!rcinfo->exclusive_release) {
  1317. d_vpr_e("%s: unsupported reset control (%s), exclusive %d\n",
  1318. __func__, name, rcinfo->exclusive_release);
  1319. return -EINVAL;
  1320. }
  1321. found = true;
  1322. /* reset_control_acquire is exposed in kernel version 6 */
  1323. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(6, 0, 0))
  1324. rc = reset_control_acquire(rcinfo->rst);
  1325. #else
  1326. rc = -EINVAL;
  1327. #endif
  1328. if (rc)
  1329. d_vpr_e("%s: failed to acquire reset control (%s), rc = %d\n",
  1330. __func__, rcinfo->name, rc);
  1331. else
  1332. d_vpr_h("%s: acquire reset control (%s)\n",
  1333. __func__, rcinfo->name);
  1334. break;
  1335. }
  1336. if (!found) {
  1337. d_vpr_e("%s: reset control (%s) not found\n", __func__, name);
  1338. rc = -EINVAL;
  1339. }
  1340. return rc;
  1341. }
  1342. static int __reset_control_release_name(struct msm_vidc_core *core,
  1343. const char *name)
  1344. {
  1345. struct reset_info *rcinfo = NULL;
  1346. int rc = 0;
  1347. bool found = false;
  1348. venus_hfi_for_each_reset_clock(core, rcinfo) {
  1349. if (strcmp(rcinfo->name, name))
  1350. continue;
  1351. /* this function is valid only for exclusive_release reset clocks*/
  1352. if (!rcinfo->exclusive_release) {
  1353. d_vpr_e("%s: unsupported reset control (%s), exclusive %d\n",
  1354. __func__, name, rcinfo->exclusive_release);
  1355. return -EINVAL;
  1356. }
  1357. found = true;
  1358. /* reset_control_release exposed in kernel version 6 */
  1359. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(6, 0, 0))
  1360. reset_control_release(rcinfo->rst);
  1361. #else
  1362. rc = -EINVAL;
  1363. #endif
  1364. if (rc)
  1365. d_vpr_e("%s: release reset control (%s) failed\n",
  1366. __func__, rcinfo->name);
  1367. else
  1368. d_vpr_h("%s: release reset control (%s) done\n",
  1369. __func__, rcinfo->name);
  1370. break;
  1371. }
  1372. if (!found) {
  1373. d_vpr_e("%s: reset control (%s) not found\n", __func__, name);
  1374. rc = -EINVAL;
  1375. }
  1376. return rc;
  1377. }
  1378. static int __reset_control_assert_name(struct msm_vidc_core *core,
  1379. const char *name)
  1380. {
  1381. struct reset_info *rcinfo = NULL;
  1382. int rc = 0;
  1383. bool found = false;
  1384. venus_hfi_for_each_reset_clock(core, rcinfo) {
  1385. if (strcmp(rcinfo->name, name))
  1386. continue;
  1387. found = true;
  1388. rc = reset_control_assert(rcinfo->rst);
  1389. if (rc)
  1390. d_vpr_e("%s: failed to assert reset control (%s), rc = %d\n",
  1391. __func__, rcinfo->name, rc);
  1392. else
  1393. d_vpr_h("%s: assert reset control (%s)\n",
  1394. __func__, rcinfo->name);
  1395. break;
  1396. }
  1397. if (!found) {
  1398. d_vpr_e("%s: reset control (%s) not found\n", __func__, name);
  1399. rc = -EINVAL;
  1400. }
  1401. return rc;
  1402. }
  1403. static int __reset_control_deassert_name(struct msm_vidc_core *core,
  1404. const char *name)
  1405. {
  1406. struct reset_info *rcinfo = NULL;
  1407. int rc = 0;
  1408. bool found = false;
  1409. venus_hfi_for_each_reset_clock(core, rcinfo) {
  1410. if (strcmp(rcinfo->name, name))
  1411. continue;
  1412. found = true;
  1413. rc = reset_control_deassert(rcinfo->rst);
  1414. if (rc)
  1415. d_vpr_e("%s: deassert reset control for (%s) failed, rc %d\n",
  1416. __func__, rcinfo->name, rc);
  1417. else
  1418. d_vpr_h("%s: deassert reset control (%s)\n",
  1419. __func__, rcinfo->name);
  1420. break;
  1421. }
  1422. if (!found) {
  1423. d_vpr_e("%s: reset control (%s) not found\n", __func__, name);
  1424. rc = -EINVAL;
  1425. }
  1426. return rc;
  1427. }
  1428. static int __reset_control_deassert(struct msm_vidc_core *core)
  1429. {
  1430. struct reset_info *rcinfo = NULL;
  1431. int rc = 0;
  1432. venus_hfi_for_each_reset_clock(core, rcinfo) {
  1433. rc = reset_control_deassert(rcinfo->rst);
  1434. if (rc) {
  1435. d_vpr_e("%s: deassert reset control failed. rc = %d\n", __func__, rc);
  1436. continue;
  1437. }
  1438. d_vpr_h("%s: deassert reset control %s\n", __func__, rcinfo->name);
  1439. }
  1440. return rc;
  1441. }
  1442. static int __reset_control_assert(struct msm_vidc_core *core)
  1443. {
  1444. struct reset_info *rcinfo = NULL;
  1445. int rc = 0, cnt = 0;
  1446. venus_hfi_for_each_reset_clock(core, rcinfo) {
  1447. if (!rcinfo->rst) {
  1448. d_vpr_e("%s: invalid reset clock %s\n",
  1449. __func__, rcinfo->name);
  1450. return -EINVAL;
  1451. }
  1452. rc = reset_control_assert(rcinfo->rst);
  1453. if (rc) {
  1454. d_vpr_e("%s: failed to assert reset control %s, rc = %d\n",
  1455. __func__, rcinfo->name, rc);
  1456. goto deassert_reset_control;
  1457. }
  1458. cnt++;
  1459. d_vpr_h("%s: assert reset control %s, count %d\n", __func__, rcinfo->name, cnt);
  1460. usleep_range(1000, 1100);
  1461. }
  1462. return rc;
  1463. deassert_reset_control:
  1464. venus_hfi_for_each_reset_clock_reverse_continue(core, rcinfo, cnt) {
  1465. d_vpr_e("%s: deassert reset control %s\n", __func__, rcinfo->name);
  1466. reset_control_deassert(rcinfo->rst);
  1467. }
  1468. return rc;
  1469. }
  1470. static int __reset_ahb2axi_bridge(struct msm_vidc_core *core)
  1471. {
  1472. int rc = 0;
  1473. rc = __reset_control_assert(core);
  1474. if (rc)
  1475. return rc;
  1476. rc = __reset_control_deassert(core);
  1477. if (rc)
  1478. return rc;
  1479. return rc;
  1480. }
  1481. static int __print_clock_residency_stats(struct msm_vidc_core *core)
  1482. {
  1483. struct clock_info *cl;
  1484. int rc = 0;
  1485. if (!core) {
  1486. d_vpr_e("%s: invalid params\n", __func__);
  1487. return -EINVAL;
  1488. }
  1489. venus_hfi_for_each_clock(core, cl) {
  1490. /* skip if scaling not supported */
  1491. if (!cl->has_scaling)
  1492. continue;
  1493. /* print clock residency stats */
  1494. print_residency_stats(core, cl);
  1495. }
  1496. return rc;
  1497. }
  1498. static int __reset_clock_residency_stats(struct msm_vidc_core *core)
  1499. {
  1500. struct clock_info *cl;
  1501. int rc = 0;
  1502. if (!core) {
  1503. d_vpr_e("%s: invalid params\n", __func__);
  1504. return -EINVAL;
  1505. }
  1506. venus_hfi_for_each_clock(core, cl) {
  1507. /* skip if scaling not supported */
  1508. if (!cl->has_scaling)
  1509. continue;
  1510. /* reset clock residency stats */
  1511. reset_residency_stats(core, cl);
  1512. }
  1513. return rc;
  1514. }
  1515. static const struct msm_vidc_resources_ops res_ops = {
  1516. .init = __init_resources,
  1517. .reset_bridge = __reset_ahb2axi_bridge,
  1518. .reset_control_acquire = __reset_control_acquire_name,
  1519. .reset_control_release = __reset_control_release_name,
  1520. .reset_control_assert = __reset_control_assert_name,
  1521. .reset_control_deassert = __reset_control_deassert_name,
  1522. .gdsc_on = __enable_regulator,
  1523. .gdsc_off = __disable_regulator,
  1524. .gdsc_hw_ctrl = __hand_off_regulators,
  1525. .gdsc_sw_ctrl = __acquire_regulators,
  1526. .llcc = llcc_enable,
  1527. .set_bw = set_bw,
  1528. .set_clks = __set_clocks,
  1529. .clk_enable = __prepare_enable_clock,
  1530. .clk_disable = __disable_unprepare_clock,
  1531. .clk_set_flag = __clock_set_flag,
  1532. .clk_print_residency_stats = __print_clock_residency_stats,
  1533. .clk_reset_residency_stats = __reset_clock_residency_stats,
  1534. };
  1535. const struct msm_vidc_resources_ops *get_resources_ops(void)
  1536. {
  1537. return &res_ops;
  1538. }