msm_cvp_res_parse.c 28 KB

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