msm_cvp_res_parse.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/iommu.h>
  6. #include <linux/of.h>
  7. #include <linux/slab.h>
  8. #include <linux/sort.h>
  9. #include <linux/of_reserved_mem.h>
  10. #include "msm_cvp_debug.h"
  11. #include "msm_cvp_resources.h"
  12. #include "msm_cvp_res_parse.h"
  13. #include "soc/qcom/secure_buffer.h"
  14. enum clock_properties {
  15. CLOCK_PROP_HAS_SCALING = 1 << 0,
  16. CLOCK_PROP_HAS_MEM_RETENTION = 1 << 1,
  17. };
  18. #define PERF_GOV "performance"
  19. static inline struct device *msm_iommu_get_ctx(const char *ctx_name)
  20. {
  21. return NULL;
  22. }
  23. static size_t get_u32_array_num_elements(struct device_node *np,
  24. char *name)
  25. {
  26. int len;
  27. size_t num_elements = 0;
  28. if (!of_get_property(np, name, &len)) {
  29. dprintk(CVP_ERR, "Failed to read %s from device tree\n",
  30. name);
  31. goto fail_read;
  32. }
  33. num_elements = len / sizeof(u32);
  34. if (num_elements <= 0) {
  35. dprintk(CVP_ERR, "%s not specified in device tree\n",
  36. name);
  37. goto fail_read;
  38. }
  39. return num_elements;
  40. fail_read:
  41. return 0;
  42. }
  43. static inline void msm_cvp_free_allowed_clocks_table(
  44. struct msm_cvp_platform_resources *res)
  45. {
  46. res->allowed_clks_tbl = NULL;
  47. }
  48. static inline void msm_cvp_free_cycles_per_mb_table(
  49. struct msm_cvp_platform_resources *res)
  50. {
  51. res->clock_freq_tbl.clk_prof_entries = NULL;
  52. }
  53. static inline void msm_cvp_free_reg_table(
  54. struct msm_cvp_platform_resources *res)
  55. {
  56. res->reg_set.reg_tbl = NULL;
  57. }
  58. static inline void msm_cvp_free_qdss_addr_table(
  59. struct msm_cvp_platform_resources *res)
  60. {
  61. res->qdss_addr_set.addr_tbl = NULL;
  62. }
  63. static inline void msm_cvp_free_bus_vectors(
  64. struct msm_cvp_platform_resources *res)
  65. {
  66. kfree(res->bus_set.bus_tbl);
  67. res->bus_set.bus_tbl = NULL;
  68. res->bus_set.count = 0;
  69. }
  70. static inline void msm_cvp_free_regulator_table(
  71. struct msm_cvp_platform_resources *res)
  72. {
  73. int c = 0;
  74. for (c = 0; c < res->regulator_set.count; ++c) {
  75. struct regulator_info *rinfo =
  76. &res->regulator_set.regulator_tbl[c];
  77. rinfo->name = NULL;
  78. }
  79. res->regulator_set.regulator_tbl = NULL;
  80. res->regulator_set.count = 0;
  81. }
  82. static inline void msm_cvp_free_clock_table(
  83. struct msm_cvp_platform_resources *res)
  84. {
  85. res->clock_set.clock_tbl = NULL;
  86. res->clock_set.count = 0;
  87. }
  88. void msm_cvp_free_platform_resources(
  89. struct msm_cvp_platform_resources *res)
  90. {
  91. msm_cvp_free_clock_table(res);
  92. msm_cvp_free_regulator_table(res);
  93. msm_cvp_free_allowed_clocks_table(res);
  94. msm_cvp_free_reg_table(res);
  95. msm_cvp_free_qdss_addr_table(res);
  96. msm_cvp_free_bus_vectors(res);
  97. }
  98. static int msm_cvp_load_ipcc_regs(struct msm_cvp_platform_resources *res)
  99. {
  100. int ret = 0;
  101. unsigned int reg_config[2];
  102. struct platform_device *pdev = res->pdev;
  103. ret = of_property_read_u32_array(pdev->dev.of_node, "qcom,ipcc-reg",
  104. reg_config, 2);
  105. if (ret) {
  106. dprintk(CVP_ERR, "Failed to read ipcc reg: %d\n", ret);
  107. return ret;
  108. }
  109. res->ipcc_reg_base = reg_config[0];
  110. res->ipcc_reg_size = reg_config[1];
  111. return ret;
  112. }
  113. static int msm_cvp_load_gcc_regs(struct msm_cvp_platform_resources *res)
  114. {
  115. int ret = 0;
  116. unsigned int reg_config[2];
  117. struct platform_device *pdev = res->pdev;
  118. ret = of_property_read_u32_array(pdev->dev.of_node, "qcom,gcc-reg",
  119. reg_config, 2);
  120. if (ret) {
  121. dprintk(CVP_WARN, "No gcc reg configured: %d\n", ret);
  122. return ret;
  123. }
  124. res->gcc_reg_base = reg_config[0];
  125. res->gcc_reg_size = reg_config[1];
  126. return ret;
  127. }
  128. static int msm_cvp_load_reg_table(struct msm_cvp_platform_resources *res)
  129. {
  130. struct reg_set *reg_set;
  131. struct platform_device *pdev = res->pdev;
  132. int i;
  133. int rc = 0;
  134. if (!of_find_property(pdev->dev.of_node, "qcom,reg-presets", NULL)) {
  135. /*
  136. * qcom,reg-presets is an optional property. It likely won't be
  137. * present if we don't have any register settings to program
  138. */
  139. dprintk(CVP_CORE, "qcom,reg-presets not found\n");
  140. return 0;
  141. }
  142. reg_set = &res->reg_set;
  143. reg_set->count = get_u32_array_num_elements(pdev->dev.of_node,
  144. "qcom,reg-presets");
  145. reg_set->count /= sizeof(*reg_set->reg_tbl) / sizeof(u32);
  146. if (!reg_set->count) {
  147. dprintk(CVP_CORE, "no elements in reg set\n");
  148. return rc;
  149. }
  150. reg_set->reg_tbl = devm_kzalloc(&pdev->dev, reg_set->count *
  151. sizeof(*(reg_set->reg_tbl)), GFP_KERNEL);
  152. if (!reg_set->reg_tbl) {
  153. dprintk(CVP_ERR, "%s Failed to alloc register table\n",
  154. __func__);
  155. return -ENOMEM;
  156. }
  157. if (of_property_read_u32_array(pdev->dev.of_node, "qcom,reg-presets",
  158. (u32 *)reg_set->reg_tbl, reg_set->count * 2)) {
  159. dprintk(CVP_ERR, "Failed to read register table\n");
  160. msm_cvp_free_reg_table(res);
  161. return -EINVAL;
  162. }
  163. for (i = 0; i < reg_set->count; i++) {
  164. dprintk(CVP_CORE,
  165. "reg = %x, value = %x\n",
  166. reg_set->reg_tbl[i].reg,
  167. reg_set->reg_tbl[i].value
  168. );
  169. }
  170. return rc;
  171. }
  172. static int msm_cvp_load_qdss_table(struct msm_cvp_platform_resources *res)
  173. {
  174. struct addr_set *qdss_addr_set;
  175. struct platform_device *pdev = res->pdev;
  176. int i;
  177. int rc = 0;
  178. if (!of_find_property(pdev->dev.of_node, "qcom,qdss-presets", NULL)) {
  179. /*
  180. * qcom,qdss-presets is an optional property. It likely won't be
  181. * present if we don't have any register settings to program
  182. */
  183. dprintk(CVP_CORE, "qcom,qdss-presets not found\n");
  184. return rc;
  185. }
  186. qdss_addr_set = &res->qdss_addr_set;
  187. qdss_addr_set->count = get_u32_array_num_elements(pdev->dev.of_node,
  188. "qcom,qdss-presets");
  189. qdss_addr_set->count /= sizeof(*qdss_addr_set->addr_tbl) / sizeof(u32);
  190. if (!qdss_addr_set->count) {
  191. dprintk(CVP_CORE, "no elements in qdss reg set\n");
  192. return rc;
  193. }
  194. qdss_addr_set->addr_tbl = devm_kzalloc(&pdev->dev,
  195. qdss_addr_set->count * sizeof(*qdss_addr_set->addr_tbl),
  196. GFP_KERNEL);
  197. if (!qdss_addr_set->addr_tbl) {
  198. dprintk(CVP_ERR, "%s Failed to alloc register table\n",
  199. __func__);
  200. rc = -ENOMEM;
  201. goto err_qdss_addr_tbl;
  202. }
  203. rc = of_property_read_u32_array(pdev->dev.of_node, "qcom,qdss-presets",
  204. (u32 *)qdss_addr_set->addr_tbl, qdss_addr_set->count * 2);
  205. if (rc) {
  206. dprintk(CVP_ERR, "Failed to read qdss address table\n");
  207. msm_cvp_free_qdss_addr_table(res);
  208. rc = -EINVAL;
  209. goto err_qdss_addr_tbl;
  210. }
  211. for (i = 0; i < qdss_addr_set->count; i++) {
  212. dprintk(CVP_CORE, "qdss addr = %x, value = %x\n",
  213. qdss_addr_set->addr_tbl[i].start,
  214. qdss_addr_set->addr_tbl[i].size);
  215. }
  216. err_qdss_addr_tbl:
  217. return rc;
  218. }
  219. static int msm_cvp_load_subcache_info(struct msm_cvp_platform_resources *res)
  220. {
  221. int rc = 0, num_subcaches = 0, c;
  222. struct platform_device *pdev = res->pdev;
  223. struct subcache_set *subcaches = &res->subcache_set;
  224. num_subcaches = of_property_count_strings(pdev->dev.of_node,
  225. "cache-slice-names");
  226. if (num_subcaches <= 0) {
  227. dprintk(CVP_CORE, "No subcaches found\n");
  228. goto err_load_subcache_table_fail;
  229. }
  230. subcaches->subcache_tbl = devm_kzalloc(&pdev->dev,
  231. sizeof(*subcaches->subcache_tbl) * num_subcaches, GFP_KERNEL);
  232. if (!subcaches->subcache_tbl) {
  233. dprintk(CVP_ERR,
  234. "Failed to allocate memory for subcache tbl\n");
  235. rc = -ENOMEM;
  236. goto err_load_subcache_table_fail;
  237. }
  238. subcaches->count = num_subcaches;
  239. dprintk(CVP_CORE, "Found %d subcaches\n", num_subcaches);
  240. for (c = 0; c < num_subcaches; ++c) {
  241. struct subcache_info *vsc = &res->subcache_set.subcache_tbl[c];
  242. of_property_read_string_index(pdev->dev.of_node,
  243. "cache-slice-names", c, &vsc->name);
  244. }
  245. res->sys_cache_present = true;
  246. return 0;
  247. err_load_subcache_table_fail:
  248. res->sys_cache_present = false;
  249. subcaches->count = 0;
  250. subcaches->subcache_tbl = NULL;
  251. return rc;
  252. }
  253. /**
  254. * msm_cvp_load_u32_table() - load dtsi table entries
  255. * @pdev: A pointer to the platform device.
  256. * @of_node: A pointer to the device node.
  257. * @table_name: A pointer to the dtsi table entry name.
  258. * @struct_size: The size of the structure which is nothing but
  259. * a single entry in the dtsi table.
  260. * @table: A pointer to the table pointer which needs to be
  261. * filled by the dtsi table entries.
  262. * @num_elements: Number of elements pointer which needs to be filled
  263. * with the number of elements in the table.
  264. *
  265. * This is a generic implementation to load single or multiple array
  266. * table from dtsi. The array elements should be of size equal to u32.
  267. *
  268. * Return: Return '0' for success else appropriate error value.
  269. */
  270. int msm_cvp_load_u32_table(struct platform_device *pdev,
  271. struct device_node *of_node, char *table_name, int struct_size,
  272. u32 **table, u32 *num_elements)
  273. {
  274. int rc = 0, num_elemts = 0;
  275. u32 *ptbl = NULL;
  276. if (!of_find_property(of_node, table_name, NULL)) {
  277. dprintk(CVP_CORE, "%s not found\n", table_name);
  278. return 0;
  279. }
  280. num_elemts = get_u32_array_num_elements(of_node, table_name);
  281. if (!num_elemts) {
  282. dprintk(CVP_ERR, "no elements in %s\n", table_name);
  283. return 0;
  284. }
  285. num_elemts /= struct_size / sizeof(u32);
  286. ptbl = devm_kzalloc(&pdev->dev, num_elemts * struct_size, GFP_KERNEL);
  287. if (!ptbl) {
  288. dprintk(CVP_ERR, "Failed to alloc table %s\n", table_name);
  289. return -ENOMEM;
  290. }
  291. if (of_property_read_u32_array(of_node, table_name, ptbl,
  292. num_elemts * struct_size / sizeof(u32))) {
  293. dprintk(CVP_ERR, "Failed to read %s\n", table_name);
  294. return -EINVAL;
  295. }
  296. *table = ptbl;
  297. if (num_elements)
  298. *num_elements = num_elemts;
  299. return rc;
  300. }
  301. EXPORT_SYMBOL(msm_cvp_load_u32_table);
  302. /* A comparator to compare loads (needed later on) */
  303. static int cmp(const void *a, const void *b)
  304. {
  305. return ((struct allowed_clock_rates_table *)a)->clock_rate -
  306. ((struct allowed_clock_rates_table *)b)->clock_rate;
  307. }
  308. static int msm_cvp_load_allowed_clocks_table(
  309. struct msm_cvp_platform_resources *res)
  310. {
  311. int rc = 0;
  312. struct platform_device *pdev = res->pdev;
  313. if (!of_find_property(pdev->dev.of_node,
  314. "qcom,allowed-clock-rates", NULL)) {
  315. dprintk(CVP_CORE, "qcom,allowed-clock-rates not found\n");
  316. return 0;
  317. }
  318. rc = msm_cvp_load_u32_table(pdev, pdev->dev.of_node,
  319. "qcom,allowed-clock-rates",
  320. sizeof(*res->allowed_clks_tbl),
  321. (u32 **)&res->allowed_clks_tbl,
  322. &res->allowed_clks_tbl_size);
  323. if (rc) {
  324. dprintk(CVP_ERR,
  325. "%s: failed to read allowed clocks table\n", __func__);
  326. return rc;
  327. }
  328. sort(res->allowed_clks_tbl, res->allowed_clks_tbl_size,
  329. sizeof(*res->allowed_clks_tbl), cmp, NULL);
  330. return 0;
  331. }
  332. static int msm_cvp_populate_mem_cdsp(struct device *dev,
  333. struct msm_cvp_platform_resources *res)
  334. {
  335. struct device_node *mem_node;
  336. int ret;
  337. mem_node = of_parse_phandle(dev->of_node, "memory-region", 0);
  338. if (mem_node) {
  339. ret = of_reserved_mem_device_init_by_idx(dev,
  340. dev->of_node, 0);
  341. of_node_put(dev->of_node);
  342. if (ret) {
  343. dprintk(CVP_ERR,
  344. "Failed to initialize reserved mem, ret %d\n",
  345. ret);
  346. return ret;
  347. }
  348. }
  349. res->mem_cdsp.dev = dev;
  350. return 0;
  351. }
  352. static int msm_cvp_populate_bus(struct device *dev,
  353. struct msm_cvp_platform_resources *res)
  354. {
  355. struct bus_set *buses = &res->bus_set;
  356. const char *temp_name = NULL;
  357. struct bus_info *bus = NULL, *temp_table;
  358. u32 range[2];
  359. int rc = 0;
  360. temp_table = krealloc(buses->bus_tbl, sizeof(*temp_table) *
  361. (buses->count + 1), GFP_KERNEL);
  362. if (!temp_table) {
  363. dprintk(CVP_ERR, "%s: Failed to allocate memory", __func__);
  364. rc = -ENOMEM;
  365. goto err_bus;
  366. }
  367. buses->bus_tbl = temp_table;
  368. bus = &buses->bus_tbl[buses->count];
  369. memset(bus, 0x0, sizeof(struct bus_info));
  370. rc = of_property_read_string(dev->of_node, "label", &temp_name);
  371. if (rc) {
  372. dprintk(CVP_ERR, "'label' not found in node\n");
  373. goto err_bus;
  374. }
  375. /* need a non-const version of name, hence copying it over */
  376. bus->name = devm_kstrdup(dev, temp_name, GFP_KERNEL);
  377. if (!bus->name) {
  378. rc = -ENOMEM;
  379. goto err_bus;
  380. }
  381. rc = of_property_read_u32(dev->of_node, "qcom,bus-master",
  382. &bus->master);
  383. if (rc) {
  384. dprintk(CVP_ERR, "'qcom,bus-master' not found in node\n");
  385. goto err_bus;
  386. }
  387. rc = of_property_read_u32(dev->of_node, "qcom,bus-slave", &bus->slave);
  388. if (rc) {
  389. dprintk(CVP_ERR, "'qcom,bus-slave' not found in node\n");
  390. goto err_bus;
  391. }
  392. rc = of_property_read_string(dev->of_node, "qcom,bus-governor",
  393. &bus->governor);
  394. if (rc) {
  395. rc = 0;
  396. dprintk(CVP_CORE,
  397. "'qcom,bus-governor' not found, default to performance governor\n");
  398. bus->governor = PERF_GOV;
  399. }
  400. if (!strcmp(bus->governor, PERF_GOV))
  401. bus->is_prfm_gov_used = true;
  402. rc = of_property_read_u32_array(dev->of_node, "qcom,bus-range-kbps",
  403. range, ARRAY_SIZE(range));
  404. if (rc) {
  405. rc = 0;
  406. dprintk(CVP_CORE,
  407. "'qcom,range' not found defaulting to <0 INT_MAX>\n");
  408. range[0] = 0;
  409. range[1] = INT_MAX;
  410. }
  411. bus->range[0] = range[0]; /* min */
  412. bus->range[1] = range[1]; /* max */
  413. buses->count++;
  414. bus->dev = dev;
  415. dprintk(CVP_CORE, "Found bus %s [%d->%d] with governor %s\n",
  416. bus->name, bus->master, bus->slave, bus->governor);
  417. err_bus:
  418. return rc;
  419. }
  420. static int msm_cvp_load_regulator_table(
  421. struct msm_cvp_platform_resources *res)
  422. {
  423. int rc = 0;
  424. struct platform_device *pdev = res->pdev;
  425. struct regulator_set *regulators = &res->regulator_set;
  426. struct device_node *domains_parent_node = NULL;
  427. struct property *domains_property = NULL;
  428. int reg_count = 0;
  429. regulators->count = 0;
  430. regulators->regulator_tbl = NULL;
  431. domains_parent_node = pdev->dev.of_node;
  432. for_each_property_of_node(domains_parent_node, domains_property) {
  433. const char *search_string = "-supply";
  434. char *supply;
  435. bool matched = false;
  436. /* check if current property is possibly a regulator */
  437. supply = strnstr(domains_property->name, search_string,
  438. strlen(domains_property->name) + 1);
  439. matched = supply && (*(supply + strlen(search_string)) == '\0');
  440. if (!matched)
  441. continue;
  442. reg_count++;
  443. }
  444. regulators->regulator_tbl = devm_kzalloc(&pdev->dev,
  445. sizeof(*regulators->regulator_tbl) *
  446. reg_count, GFP_KERNEL);
  447. if (!regulators->regulator_tbl) {
  448. rc = -ENOMEM;
  449. dprintk(CVP_ERR,
  450. "Failed to alloc memory for regulator table\n");
  451. goto err_reg_tbl_alloc;
  452. }
  453. for_each_property_of_node(domains_parent_node, domains_property) {
  454. const char *search_string = "-supply";
  455. char *supply;
  456. bool matched = false;
  457. struct device_node *regulator_node = NULL;
  458. struct regulator_info *rinfo = NULL;
  459. /* check if current property is possibly a regulator */
  460. supply = strnstr(domains_property->name, search_string,
  461. strlen(domains_property->name) + 1);
  462. matched = supply && (supply[strlen(search_string)] == '\0');
  463. if (!matched)
  464. continue;
  465. /* make sure prop isn't being misused */
  466. regulator_node = of_parse_phandle(domains_parent_node,
  467. domains_property->name, 0);
  468. if (IS_ERR(regulator_node)) {
  469. dprintk(CVP_WARN, "%s is not a phandle\n",
  470. domains_property->name);
  471. continue;
  472. }
  473. regulators->count++;
  474. /* populate regulator info */
  475. rinfo = &regulators->regulator_tbl[regulators->count - 1];
  476. rinfo->name = devm_kzalloc(&pdev->dev,
  477. (supply - domains_property->name) + 1, GFP_KERNEL);
  478. if (!rinfo->name) {
  479. rc = -ENOMEM;
  480. dprintk(CVP_ERR,
  481. "Failed to alloc memory for regulator name\n");
  482. goto err_reg_name_alloc;
  483. }
  484. strlcpy(rinfo->name, domains_property->name,
  485. (supply - domains_property->name) + 1);
  486. rinfo->has_hw_power_collapse = of_property_read_bool(
  487. regulator_node, "qcom,support-hw-trigger");
  488. dprintk(CVP_CORE, "Found regulator %s: h/w collapse = %s\n",
  489. rinfo->name,
  490. rinfo->has_hw_power_collapse ? "yes" : "no");
  491. }
  492. if (!regulators->count)
  493. dprintk(CVP_CORE, "No regulators found");
  494. return 0;
  495. err_reg_name_alloc:
  496. err_reg_tbl_alloc:
  497. msm_cvp_free_regulator_table(res);
  498. return rc;
  499. }
  500. static int msm_cvp_load_clock_table(
  501. struct msm_cvp_platform_resources *res)
  502. {
  503. int rc = 0, num_clocks = 0, c = 0;
  504. struct platform_device *pdev = res->pdev;
  505. int *clock_ids = NULL;
  506. int *clock_props = NULL;
  507. struct clock_set *clocks = &res->clock_set;
  508. num_clocks = of_property_count_strings(pdev->dev.of_node,
  509. "clock-names");
  510. if (num_clocks <= 0) {
  511. dprintk(CVP_CORE, "No clocks found\n");
  512. clocks->count = 0;
  513. rc = 0;
  514. goto err_load_clk_table_fail;
  515. }
  516. clock_ids = devm_kzalloc(&pdev->dev, num_clocks *
  517. sizeof(*clock_ids), GFP_KERNEL);
  518. if (!clock_ids) {
  519. dprintk(CVP_ERR, "No memory to read clock ids\n");
  520. rc = -ENOMEM;
  521. goto err_load_clk_table_fail;
  522. }
  523. rc = of_property_read_u32_array(pdev->dev.of_node,
  524. "clock-ids", clock_ids,
  525. num_clocks);
  526. if (rc) {
  527. dprintk(CVP_CORE, "Failed to read clock ids: %d\n", rc);
  528. msm_cvp_mmrm_enabled = false;
  529. dprintk(CVP_CORE, "flag msm_cvp_mmrm_enabled disabled\n");
  530. }
  531. clock_props = devm_kzalloc(&pdev->dev, num_clocks *
  532. sizeof(*clock_props), GFP_KERNEL);
  533. if (!clock_props) {
  534. dprintk(CVP_ERR, "No memory to read clock properties\n");
  535. rc = -ENOMEM;
  536. goto err_load_clk_table_fail;
  537. }
  538. rc = of_property_read_u32_array(pdev->dev.of_node,
  539. "qcom,clock-configs", clock_props,
  540. num_clocks);
  541. if (rc) {
  542. dprintk(CVP_ERR, "Failed to read clock properties: %d\n", rc);
  543. goto err_load_clk_prop_fail;
  544. }
  545. clocks->clock_tbl = devm_kzalloc(&pdev->dev, sizeof(*clocks->clock_tbl)
  546. * num_clocks, GFP_KERNEL);
  547. if (!clocks->clock_tbl) {
  548. dprintk(CVP_ERR, "Failed to allocate memory for clock tbl\n");
  549. rc = -ENOMEM;
  550. goto err_load_clk_prop_fail;
  551. }
  552. clocks->count = num_clocks;
  553. dprintk(CVP_CORE, "Found %d clocks\n", num_clocks);
  554. for (c = 0; c < num_clocks; ++c) {
  555. struct clock_info *vc = &res->clock_set.clock_tbl[c];
  556. of_property_read_string_index(pdev->dev.of_node,
  557. "clock-names", c, &vc->name);
  558. if (msm_cvp_mmrm_enabled == true)
  559. vc->clk_id = clock_ids[c];
  560. if (clock_props[c] & CLOCK_PROP_HAS_SCALING) {
  561. vc->has_scaling = true;
  562. } else {
  563. vc->count = 0;
  564. vc->has_scaling = false;
  565. }
  566. if (clock_props[c] & CLOCK_PROP_HAS_MEM_RETENTION)
  567. vc->has_mem_retention = true;
  568. else
  569. vc->has_mem_retention = false;
  570. dprintk(CVP_CORE, "Found clock %s id %d: scale-able = %s\n",
  571. vc->name, vc->clk_id, vc->count ? "yes" : "no");
  572. }
  573. return 0;
  574. err_load_clk_prop_fail:
  575. err_load_clk_table_fail:
  576. return rc;
  577. }
  578. #define MAX_CLK_RESETS 5
  579. static int msm_cvp_load_reset_table(
  580. struct msm_cvp_platform_resources *res)
  581. {
  582. struct platform_device *pdev = res->pdev;
  583. struct reset_set *rst = &res->reset_set;
  584. int num_clocks = 0, c = 0, ret = 0;
  585. int pwr_stats[MAX_CLK_RESETS];
  586. num_clocks = of_property_count_strings(pdev->dev.of_node,
  587. "reset-names");
  588. if (num_clocks <= 0 || num_clocks > MAX_CLK_RESETS) {
  589. dprintk(CVP_ERR, "Num reset clocks out of range\n");
  590. rst->count = 0;
  591. return 0;
  592. }
  593. rst->reset_tbl = devm_kcalloc(&pdev->dev, num_clocks,
  594. sizeof(*rst->reset_tbl), GFP_KERNEL);
  595. if (!rst->reset_tbl)
  596. return -ENOMEM;
  597. rst->count = num_clocks;
  598. dprintk(CVP_CORE, "Found %d reset clocks\n", num_clocks);
  599. ret = of_property_read_u32_array(pdev->dev.of_node,
  600. "reset-power-status", pwr_stats,
  601. num_clocks);
  602. if (ret) {
  603. dprintk(CVP_ERR, "Failed to read reset pwr state: %d\n", ret);
  604. devm_kfree(&pdev->dev, rst->reset_tbl);
  605. return ret;
  606. }
  607. for (c = 0; c < num_clocks; ++c) {
  608. struct reset_info *rc = &res->reset_set.reset_tbl[c];
  609. of_property_read_string_index(pdev->dev.of_node,
  610. "reset-names", c, &rc->name);
  611. rc->required_state = pwr_stats[c];
  612. }
  613. return 0;
  614. }
  615. static int find_key_value(struct msm_cvp_platform_data *platform_data,
  616. const char *key)
  617. {
  618. int i = 0;
  619. struct msm_cvp_common_data *common_data = platform_data->common_data;
  620. int size = platform_data->common_data_length;
  621. for (i = 0; i < size; i++) {
  622. if (!strcmp(common_data[i].key, key))
  623. return common_data[i].value;
  624. }
  625. return 0;
  626. }
  627. int cvp_read_platform_resources_from_drv_data(
  628. struct msm_cvp_core *core)
  629. {
  630. struct msm_cvp_platform_data *platform_data;
  631. struct msm_cvp_platform_resources *res;
  632. int rc = 0;
  633. if (!core || !core->platform_data) {
  634. dprintk(CVP_ERR, "%s Invalid data\n", __func__);
  635. return -ENOENT;
  636. }
  637. platform_data = core->platform_data;
  638. res = &core->resources;
  639. res->sku_version = platform_data->sku_version;
  640. res->fw_name = "evass";
  641. dprintk(CVP_CORE, "Firmware filename: %s\n", res->fw_name);
  642. res->auto_pil = find_key_value(platform_data,
  643. "qcom,auto-pil");
  644. res->dsp_enabled = find_key_value(platform_data,
  645. "qcom,dsp-enabled");
  646. res->max_load = find_key_value(platform_data,
  647. "qcom,max-hw-load");
  648. res->sw_power_collapsible = find_key_value(platform_data,
  649. "qcom,sw-power-collapse");
  650. res->never_unload_fw = find_key_value(platform_data,
  651. "qcom,never-unload-fw");
  652. res->debug_timeout = find_key_value(platform_data,
  653. "qcom,debug-timeout");
  654. res->pm_qos_latency_us = find_key_value(platform_data,
  655. "qcom,pm-qos-latency-us");
  656. res->max_secure_inst_count = find_key_value(platform_data,
  657. "qcom,max-secure-instances");
  658. res->thermal_mitigable = find_key_value(platform_data,
  659. "qcom,enable-thermal-mitigation");
  660. res->msm_cvp_pwr_collapse_delay = find_key_value(platform_data,
  661. "qcom,power-collapse-delay");
  662. res->msm_cvp_firmware_unload_delay = find_key_value(platform_data,
  663. "qcom,fw-unload-delay");
  664. res->msm_cvp_hw_rsp_timeout = find_key_value(platform_data,
  665. "qcom,hw-resp-timeout");
  666. res->msm_cvp_dsp_rsp_timeout = find_key_value(platform_data,
  667. "qcom,dsp-resp-timeout");
  668. res->non_fatal_pagefaults = find_key_value(platform_data,
  669. "qcom,domain-attr-non-fatal-faults");
  670. res->vpu_ver = platform_data->vpu_ver;
  671. res->ubwc_config = platform_data->ubwc_config;
  672. return rc;
  673. }
  674. int cvp_read_platform_resources_from_dt(
  675. struct msm_cvp_platform_resources *res)
  676. {
  677. struct platform_device *pdev = res->pdev;
  678. struct resource *kres = NULL;
  679. int rc = 0;
  680. uint32_t firmware_base = 0;
  681. if (!pdev->dev.of_node) {
  682. dprintk(CVP_ERR, "DT node not found\n");
  683. return -ENOENT;
  684. }
  685. INIT_LIST_HEAD(&res->context_banks);
  686. res->firmware_base = (phys_addr_t)firmware_base;
  687. kres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  688. res->register_base = kres ? kres->start : -1;
  689. res->register_size = kres ? (kres->end + 1 - kres->start) : -1;
  690. kres = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  691. res->irq = kres ? kres->start : -1;
  692. rc = msm_cvp_load_subcache_info(res);
  693. if (rc)
  694. dprintk(CVP_WARN, "Failed to load subcache info: %d\n", rc);
  695. rc = msm_cvp_load_qdss_table(res);
  696. if (rc)
  697. dprintk(CVP_WARN, "Failed to load qdss reg table: %d\n", rc);
  698. rc = msm_cvp_load_reg_table(res);
  699. if (rc) {
  700. dprintk(CVP_ERR, "Failed to load reg table: %d\n", rc);
  701. goto err_load_reg_table;
  702. }
  703. rc = msm_cvp_load_ipcc_regs(res);
  704. if (rc)
  705. dprintk(CVP_ERR, "Failed to load IPCC regs: %d\n", rc);
  706. rc = msm_cvp_load_gcc_regs(res);
  707. rc = msm_cvp_load_regulator_table(res);
  708. if (rc) {
  709. dprintk(CVP_ERR, "Failed to load list of regulators %d\n", rc);
  710. goto err_load_regulator_table;
  711. }
  712. rc = msm_cvp_load_clock_table(res);
  713. if (rc) {
  714. dprintk(CVP_ERR,
  715. "Failed to load clock table: %d\n", rc);
  716. goto err_load_clock_table;
  717. }
  718. rc = msm_cvp_load_allowed_clocks_table(res);
  719. if (rc) {
  720. dprintk(CVP_ERR,
  721. "Failed to load allowed clocks table: %d\n", rc);
  722. goto err_load_allowed_clocks_table;
  723. }
  724. rc = msm_cvp_load_reset_table(res);
  725. if (rc) {
  726. dprintk(CVP_ERR,
  727. "Failed to load reset table: %d\n", rc);
  728. goto err_load_reset_table;
  729. }
  730. res->use_non_secure_pil = of_property_read_bool(pdev->dev.of_node,
  731. "qcom,use-non-secure-pil");
  732. if (res->use_non_secure_pil || !is_iommu_present(res)) {
  733. of_property_read_u32(pdev->dev.of_node, "qcom,fw-bias",
  734. &firmware_base);
  735. res->firmware_base = (phys_addr_t)firmware_base;
  736. dprintk(CVP_CORE,
  737. "Using fw-bias : %pa", &res->firmware_base);
  738. }
  739. return rc;
  740. err_load_reset_table:
  741. msm_cvp_free_allowed_clocks_table(res);
  742. err_load_allowed_clocks_table:
  743. msm_cvp_free_clock_table(res);
  744. err_load_clock_table:
  745. msm_cvp_free_regulator_table(res);
  746. err_load_regulator_table:
  747. msm_cvp_free_reg_table(res);
  748. err_load_reg_table:
  749. return rc;
  750. }
  751. static int msm_cvp_setup_context_bank(struct msm_cvp_platform_resources *res,
  752. struct context_bank_info *cb, struct device *dev)
  753. {
  754. int rc = 0;
  755. struct bus_type *bus;
  756. if (!dev || !cb || !res) {
  757. dprintk(CVP_ERR,
  758. "%s: Invalid Input params\n", __func__);
  759. return -EINVAL;
  760. }
  761. cb->dev = dev;
  762. bus = cb->dev->bus;
  763. if (IS_ERR_OR_NULL(bus)) {
  764. dprintk(CVP_ERR, "%s - failed to get bus type\n", __func__);
  765. rc = PTR_ERR(bus) ?: -ENODEV;
  766. goto remove_cb;
  767. }
  768. /*
  769. * configure device segment size and segment boundary to ensure
  770. * iommu mapping returns one mapping (which is required for partial
  771. * cache operations)
  772. */
  773. if (!dev->dma_parms)
  774. dev->dma_parms =
  775. devm_kzalloc(dev, sizeof(*dev->dma_parms), GFP_KERNEL);
  776. dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
  777. dma_set_seg_boundary(dev, DMA_BIT_MASK(64));
  778. dprintk(CVP_CORE, "Attached %s and created mapping\n", dev_name(dev));
  779. dprintk(CVP_CORE,
  780. "Context bank name:%s, buffer_type: %#x, is_secure: %d, address range start: %#x, size: %#x, dev: %pK",
  781. cb->name, cb->buffer_type, cb->is_secure, cb->addr_range.start,
  782. cb->addr_range.size, cb->dev);
  783. return rc;
  784. remove_cb:
  785. return rc;
  786. }
  787. int msm_cvp_smmu_fault_handler(struct iommu_domain *domain,
  788. struct device *dev, unsigned long iova, int flags, void *token)
  789. {
  790. struct msm_cvp_core *core = token;
  791. struct msm_cvp_inst *inst;
  792. u32 *pfaddr = &core->last_fault_addr;
  793. if (!domain || !core) {
  794. dprintk(CVP_ERR, "%s - invalid param %pK %pK\n",
  795. __func__, domain, core);
  796. return -EINVAL;
  797. }
  798. if (core->smmu_fault_handled) {
  799. if (core->resources.non_fatal_pagefaults) {
  800. WARN_ONCE(1, "%s: non-fatal pagefault address: %lx\n",
  801. __func__, iova);
  802. *pfaddr = (*pfaddr == 0) ? iova : (*pfaddr);
  803. return 0;
  804. }
  805. }
  806. dprintk(CVP_ERR, "%s - faulting address: %lx\n", __func__, iova);
  807. mutex_lock(&core->lock);
  808. list_for_each_entry(inst, &core->instances, list) {
  809. msm_cvp_print_inst_bufs(inst);
  810. }
  811. core->smmu_fault_handled = true;
  812. mutex_unlock(&core->lock);
  813. /*
  814. * Return -EINVAL to elicit the default behaviour of smmu driver.
  815. * If we return -ENOSYS, then smmu driver assumes page fault handler
  816. * is not installed and prints a list of useful debug information like
  817. * FAR, SID etc. This information is not printed if we return 0.
  818. */
  819. return -ENOSYS;
  820. }
  821. static int msm_cvp_populate_context_bank(struct device *dev,
  822. struct msm_cvp_core *core)
  823. {
  824. int rc = 0;
  825. struct context_bank_info *cb = NULL;
  826. struct device_node *np = NULL;
  827. if (!dev || !core) {
  828. dprintk(CVP_ERR, "%s - invalid inputs\n", __func__);
  829. return -EINVAL;
  830. }
  831. np = dev->of_node;
  832. cb = devm_kzalloc(dev, sizeof(*cb), GFP_KERNEL);
  833. if (!cb) {
  834. dprintk(CVP_ERR, "%s - Failed to allocate cb\n", __func__);
  835. return -ENOMEM;
  836. }
  837. INIT_LIST_HEAD(&cb->list);
  838. list_add_tail(&cb->list, &core->resources.context_banks);
  839. rc = of_property_read_string(np, "label", &cb->name);
  840. if (rc) {
  841. dprintk(CVP_CORE,
  842. "Failed to read cb label from device tree\n");
  843. rc = 0;
  844. }
  845. dprintk(CVP_CORE, "%s: context bank has name %s\n", __func__, cb->name);
  846. rc = of_property_read_u32_array(np, "qcom,iommu-dma-addr-pool",
  847. (u32 *)&cb->addr_range, 2);
  848. if (rc) {
  849. dprintk(CVP_ERR,
  850. "Could not read addr pool for context bank : %s %d\n",
  851. cb->name, rc);
  852. goto err_setup_cb;
  853. }
  854. cb->is_secure = of_property_read_bool(np, "qcom,iommu-vmid");
  855. dprintk(CVP_CORE, "context bank %s : secure = %d\n",
  856. cb->name, cb->is_secure);
  857. /* setup buffer type for each sub device*/
  858. rc = of_property_read_u32(np, "buffer-types", &cb->buffer_type);
  859. if (rc) {
  860. dprintk(CVP_ERR, "failed to load buffer_type info %d\n", rc);
  861. rc = -ENOENT;
  862. goto err_setup_cb;
  863. }
  864. dprintk(CVP_CORE,
  865. "context bank %s address start = %x address size = %x buffer_type = %x\n",
  866. cb->name, cb->addr_range.start,
  867. cb->addr_range.size, cb->buffer_type);
  868. cb->domain = iommu_get_domain_for_dev(dev);
  869. if (IS_ERR_OR_NULL(cb->domain)) {
  870. dprintk(CVP_ERR, "Create domain failed\n");
  871. rc = -ENODEV;
  872. goto err_setup_cb;
  873. }
  874. rc = msm_cvp_setup_context_bank(&core->resources, cb, dev);
  875. if (rc) {
  876. dprintk(CVP_ERR, "Cannot setup context bank %d\n", rc);
  877. goto err_setup_cb;
  878. }
  879. iommu_set_fault_handler(cb->domain,
  880. msm_cvp_smmu_fault_handler, (void *)core);
  881. return 0;
  882. err_setup_cb:
  883. list_del(&cb->list);
  884. return rc;
  885. }
  886. int cvp_read_context_bank_resources_from_dt(struct platform_device *pdev)
  887. {
  888. struct msm_cvp_core *core;
  889. int rc = 0;
  890. if (!pdev) {
  891. dprintk(CVP_ERR, "Invalid platform device\n");
  892. return -EINVAL;
  893. } else if (!pdev->dev.parent) {
  894. dprintk(CVP_ERR, "Failed to find a parent for %s\n",
  895. dev_name(&pdev->dev));
  896. return -ENODEV;
  897. }
  898. core = dev_get_drvdata(pdev->dev.parent);
  899. if (!core) {
  900. dprintk(CVP_ERR, "Failed to find cookie in parent device %s",
  901. dev_name(pdev->dev.parent));
  902. return -EINVAL;
  903. }
  904. rc = msm_cvp_populate_context_bank(&pdev->dev, core);
  905. if (rc)
  906. dprintk(CVP_ERR, "Failed to probe context bank\n");
  907. else
  908. dprintk(CVP_CORE, "Successfully probed context bank\n");
  909. return rc;
  910. }
  911. int cvp_read_bus_resources_from_dt(struct platform_device *pdev)
  912. {
  913. struct msm_cvp_core *core;
  914. if (!pdev) {
  915. dprintk(CVP_ERR, "Invalid platform device\n");
  916. return -EINVAL;
  917. } else if (!pdev->dev.parent) {
  918. dprintk(CVP_ERR, "Failed to find a parent for %s\n",
  919. dev_name(&pdev->dev));
  920. return -ENODEV;
  921. }
  922. core = dev_get_drvdata(pdev->dev.parent);
  923. if (!core) {
  924. dprintk(CVP_ERR, "Failed to find cookie in parent device %s",
  925. dev_name(pdev->dev.parent));
  926. return -EINVAL;
  927. }
  928. return msm_cvp_populate_bus(&pdev->dev, &core->resources);
  929. }
  930. int cvp_read_mem_cdsp_resources_from_dt(struct platform_device *pdev)
  931. {
  932. struct msm_cvp_core *core;
  933. if (!pdev) {
  934. dprintk(CVP_ERR, "%s: invalid platform device\n", __func__);
  935. return -EINVAL;
  936. } else if (!pdev->dev.parent) {
  937. dprintk(CVP_ERR, "Failed to find a parent for %s\n",
  938. dev_name(&pdev->dev));
  939. return -ENODEV;
  940. }
  941. core = dev_get_drvdata(pdev->dev.parent);
  942. if (!core) {
  943. dprintk(CVP_ERR, "Failed to find cookie in parent device %s",
  944. dev_name(pdev->dev.parent));
  945. return -EINVAL;
  946. }
  947. return msm_cvp_populate_mem_cdsp(&pdev->dev, &core->resources);
  948. }